2 * Intel CPU Microcode Update Driver for Linux
4 * Copyright (C) 2012 Fenghua Yu <fenghua.yu@intel.com>
5 * H Peter Anvin" <hpa@zytor.com>
7 * This driver allows to upgrade microcode on Intel processors
8 * belonging to IA-32 family - PentiumPro, Pentium II,
9 * Pentium III, Xeon, Pentium 4, etc.
11 * Reference: Section 8.11 of Volume 3a, IA-32 Intel? Architecture
12 * Software Developer's Manual
13 * Order Number 253668 or free download from:
15 * http://developer.intel.com/Assets/PDF/manual/253668.pdf
17 * For more information, go to http://www.urbanmyth.org/microcode
19 * This program is free software; you can redistribute it and/or
20 * modify it under the terms of the GNU General Public License
21 * as published by the Free Software Foundation; either version
22 * 2 of the License, or (at your option) any later version.
25 #include <linux/firmware.h>
26 #include <linux/uaccess.h>
27 #include <linux/kernel.h>
28 #include <linux/module.h>
30 #include <asm/microcode_intel.h>
31 #include <asm/processor.h>
35 update_match_cpu(unsigned int csig
, unsigned int cpf
,
36 unsigned int sig
, unsigned int pf
)
38 return (!sigmatch(sig
, csig
, pf
, cpf
)) ? 0 : 1;
42 update_match_revision(struct microcode_header_intel
*mc_header
, int rev
)
44 return (mc_header
->rev
<= rev
) ? 0 : 1;
47 int microcode_sanity_check(void *mc
, int print_err
)
49 unsigned long total_size
, data_size
, ext_table_size
;
50 struct microcode_header_intel
*mc_header
= mc
;
51 struct extended_sigtable
*ext_header
= NULL
;
52 int sum
, orig_sum
, ext_sigcount
= 0, i
;
53 struct extended_signature
*ext_sig
;
55 total_size
= get_totalsize(mc_header
);
56 data_size
= get_datasize(mc_header
);
58 if (data_size
+ MC_HEADER_SIZE
> total_size
) {
60 pr_err("error! Bad data size in microcode data file\n");
64 if (mc_header
->ldrver
!= 1 || mc_header
->hdrver
!= 1) {
66 pr_err("error! Unknown microcode update format\n");
69 ext_table_size
= total_size
- (MC_HEADER_SIZE
+ data_size
);
71 if ((ext_table_size
< EXT_HEADER_SIZE
)
72 || ((ext_table_size
- EXT_HEADER_SIZE
) % EXT_SIGNATURE_SIZE
)) {
74 pr_err("error! Small exttable size in microcode data file\n");
77 ext_header
= mc
+ MC_HEADER_SIZE
+ data_size
;
78 if (ext_table_size
!= exttable_size(ext_header
)) {
80 pr_err("error! Bad exttable size in microcode data file\n");
83 ext_sigcount
= ext_header
->count
;
86 /* check extended table checksum */
88 int ext_table_sum
= 0;
89 int *ext_tablep
= (int *)ext_header
;
91 i
= ext_table_size
/ DWSIZE
;
93 ext_table_sum
+= ext_tablep
[i
];
96 pr_warn("aborting, bad extended signature table checksum\n");
101 /* calculate the checksum */
103 i
= (MC_HEADER_SIZE
+ data_size
) / DWSIZE
;
105 orig_sum
+= ((int *)mc
)[i
];
108 pr_err("aborting, bad checksum\n");
113 /* check extended signature checksum */
114 for (i
= 0; i
< ext_sigcount
; i
++) {
115 ext_sig
= (void *)ext_header
+ EXT_HEADER_SIZE
+
116 EXT_SIGNATURE_SIZE
* i
;
118 - (mc_header
->sig
+ mc_header
->pf
+ mc_header
->cksum
)
119 + (ext_sig
->sig
+ ext_sig
->pf
+ ext_sig
->cksum
);
122 pr_err("aborting, bad checksum\n");
128 EXPORT_SYMBOL_GPL(microcode_sanity_check
);
131 * return 0 - no update found
132 * return 1 - found update
134 int get_matching_sig(unsigned int csig
, int cpf
, void *mc
, int rev
)
136 struct microcode_header_intel
*mc_header
= mc
;
137 struct extended_sigtable
*ext_header
;
138 unsigned long total_size
= get_totalsize(mc_header
);
140 struct extended_signature
*ext_sig
;
142 if (update_match_cpu(csig
, cpf
, mc_header
->sig
, mc_header
->pf
))
145 /* Look for ext. headers: */
146 if (total_size
<= get_datasize(mc_header
) + MC_HEADER_SIZE
)
149 ext_header
= mc
+ get_datasize(mc_header
) + MC_HEADER_SIZE
;
150 ext_sigcount
= ext_header
->count
;
151 ext_sig
= (void *)ext_header
+ EXT_HEADER_SIZE
;
153 for (i
= 0; i
< ext_sigcount
; i
++) {
154 if (update_match_cpu(csig
, cpf
, ext_sig
->sig
, ext_sig
->pf
))
162 * return 0 - no update found
163 * return 1 - found update
165 int get_matching_microcode(unsigned int csig
, int cpf
, void *mc
, int rev
)
167 struct microcode_header_intel
*mc_header
= mc
;
169 if (!update_match_revision(mc_header
, rev
))
172 return get_matching_sig(csig
, cpf
, mc
, rev
);
174 EXPORT_SYMBOL_GPL(get_matching_microcode
);