2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2006, 2008 Stanislav Sedov <stas@FreeBSD.org>.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
40 #include <sys/types.h>
43 #include <sys/ioctl.h>
44 #include <sys/ioccom.h>
45 #include <sys/cpuctl.h>
47 #include <machine/cpufunc.h>
48 #include <machine/specialreg.h>
50 #include "cpucontrol.h"
53 #define DEFAULT_UCODE_SIZE 2000 /* Size of update data if not specified. */
60 cpuctl_cpuid_args_t idargs
= {
64 error
= ioctl(fd
, CPUCTL_CPUID
, &idargs
);
69 ((uint32_t *)vendor
)[0] = idargs
.data
[1];
70 ((uint32_t *)vendor
)[1] = idargs
.data
[3];
71 ((uint32_t *)vendor
)[2] = idargs
.data
[2];
73 if (strncmp(vendor
, INTEL_VENDOR_ID
, sizeof(INTEL_VENDOR_ID
)) != 0)
79 intel_update(const char *dev
, const char *path
)
88 intel_fw_header_t
*fw_header
;
89 intel_cpu_signature_t
*ext_table
;
90 intel_ext_header_t
*ext_header
;
91 uint32_t sig
, signature
, flags
;
94 size_t ext_table_size
;
96 size_t data_size
, total_size
;
97 cpuctl_msr_args_t msrargs
= {
98 .msr
= MSR_IA32_PLATFORM_ID
,
100 cpuctl_cpuid_args_t idargs
= {
101 .level
= 1, /* Signature. */
103 cpuctl_update_args_t args
;
110 fw_image
= MAP_FAILED
;
113 devfd
= open(dev
, O_RDWR
);
115 WARN(0, "could not open %s for writing", dev
);
118 error
= ioctl(devfd
, CPUCTL_CPUID
, &idargs
);
120 WARN(0, "ioctl(%s)", dev
);
123 signature
= idargs
.data
[0];
124 error
= ioctl(devfd
, CPUCTL_RDMSR
, &msrargs
);
126 WARN(0, "ioctl(%s)", dev
);
131 * MSR_IA32_PLATFORM_ID contains flag in BCD in bits 52-50.
133 flags
= 1 << ((msrargs
.data
>> 50) & 7);
134 msrargs
.msr
= MSR_BIOS_SIGN
;
135 error
= ioctl(devfd
, CPUCTL_RDMSR
, &msrargs
);
137 WARN(0, "ioctl(%s)", dev
);
140 revision
= msrargs
.data
>> 32; /* Revision in the high dword. */
141 WARNX(2, "found cpu type %#x family %#x model %#x stepping %#x.",
142 (signature
>> 12) & 0x03, (signature
>> 8) & 0x0f,
143 (signature
>> 4) & 0x0f, (signature
>> 0) & 0x0f);
145 * Open firmware image.
147 fd
= open(path
, O_RDONLY
, 0);
149 WARN(0, "open(%s)", path
);
152 error
= fstat(fd
, &st
);
154 WARN(0, "fstat(%s)", path
);
157 if (st
.st_size
< 0 || (unsigned)st
.st_size
< sizeof(*fw_header
)) {
158 WARNX(2, "file too short: %s", path
);
163 * mmap the whole image.
165 fw_image
= (uint32_t *)mmap(NULL
, st
.st_size
, PROT_READ
,
167 if (fw_image
== MAP_FAILED
) {
168 WARN(0, "mmap(%s)", path
);
171 fw_header
= (intel_fw_header_t
*)fw_image
;
172 if (fw_header
->header_version
!= INTEL_HEADER_VERSION
||
173 fw_header
->loader_revision
!= INTEL_LOADER_REVISION
) {
174 WARNX(2, "%s is not a valid intel firmware: version mismatch",
179 * According to spec, if data_size == 0, then size of ucode = 2000.
181 if (fw_header
->data_size
== 0)
182 data_size
= DEFAULT_UCODE_SIZE
;
184 data_size
= fw_header
->data_size
;
185 if (fw_header
->total_size
== 0)
186 total_size
= data_size
+ sizeof(*fw_header
);
188 total_size
= fw_header
->total_size
;
189 if (total_size
> (unsigned)st
.st_size
|| st
.st_size
< 0) {
190 WARNX(2, "file too short: %s", path
);
193 payload_size
= data_size
+ sizeof(*fw_header
);
196 * Check the primary checksum.
199 for (i
= 0; i
< (payload_size
/ sizeof(uint32_t)); i
++)
200 sum
+= *((uint32_t *)fw_image
+ i
);
202 WARNX(2, "%s: update data checksum invalid", path
);
207 * Check if there is an extended signature table.
209 ext_size
= total_size
- payload_size
;
212 if (ext_size
> (signed)sizeof(*ext_header
)) {
214 (intel_ext_header_t
*)((char *)fw_image
+ payload_size
);
215 ext_table
= (intel_cpu_signature_t
*)(ext_header
+ 1);
218 * Check the extended table size.
220 ext_table_size
= sizeof(*ext_header
) +
221 ext_header
->sig_count
* sizeof(*ext_table
);
222 if (ext_table_size
+ payload_size
> total_size
) {
223 WARNX(2, "%s: broken extended signature table", path
);
228 * Check the extended table signature.
231 for (i
= 0; i
< (ext_table_size
/ sizeof(uint32_t)); i
++)
232 sum
+= *((uint32_t *)ext_header
+ i
);
235 "%s: extended signature table checksum invalid",
243 fw_data
= fw_header
+ 1; /* Pointer to the update data. */
246 * Check if the given image is ok for this cpu.
248 if (signature
== fw_header
->cpu_signature
&&
249 (flags
& fw_header
->cpu_flags
) != 0)
251 else if (have_ext_table
!= 0) {
252 for (i
= 0; i
< ext_header
->sig_count
; i
++) {
253 sig
= ext_table
[i
].cpu_signature
;
254 if (signature
== sig
&&
255 (flags
& ext_table
[i
].cpu_flags
) != 0)
262 if (revision
>= fw_header
->revision
) {
263 WARNX(1, "skipping %s of rev %#x: up to date",
264 path
, fw_header
->revision
);
267 fprintf(stderr
, "%s: updating cpu %s from rev %#x to rev %#x... ",
268 path
, dev
, revision
, fw_header
->revision
);
270 args
.size
= data_size
;
271 error
= ioctl(devfd
, CPUCTL_UPDATE
, &args
);
274 fprintf(stderr
, "failed.\n");
279 fprintf(stderr
, "done.\n");
282 if (fw_image
!= MAP_FAILED
)
283 if (munmap(fw_image
, st
.st_size
) != 0)
284 warn("munmap(%s)", path
);