2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2011 Fabien Thomas <fabient@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"
58 cpuctl_cpuid_args_t idargs
= {
62 error
= ioctl(fd
, CPUCTL_CPUID
, &idargs
);
67 ((uint32_t *)vendor
)[0] = idargs
.data
[1];
68 ((uint32_t *)vendor
)[1] = idargs
.data
[3];
69 ((uint32_t *)vendor
)[2] = idargs
.data
[2];
71 if (strncmp(vendor
, CENTAUR_VENDOR_ID
, sizeof(CENTAUR_VENDOR_ID
)) != 0)
74 /* TODO: detect Nano CPU. */
79 via_update(const char *dev
, const char *path
)
87 via_fw_header_t
*fw_header
;
91 size_t data_size
, total_size
;
92 cpuctl_msr_args_t msrargs
= {
93 .msr
= MSR_IA32_PLATFORM_ID
,
95 cpuctl_cpuid_args_t idargs
= {
96 .level
= 1, /* Signature. */
98 cpuctl_update_args_t args
;
106 fw_image
= MAP_FAILED
;
107 devfd
= open(dev
, O_RDWR
);
109 WARN(0, "could not open %s for writing", dev
);
112 error
= ioctl(devfd
, CPUCTL_CPUID
, &idargs
);
114 WARN(0, "ioctl(%s)", dev
);
117 signature
= idargs
.data
[0];
118 error
= ioctl(devfd
, CPUCTL_RDMSR
, &msrargs
);
120 WARN(0, "ioctl(%s)", dev
);
125 * MSR_IA32_PLATFORM_ID contains flag in BCD in bits 52-50.
127 msrargs
.msr
= MSR_BIOS_SIGN
;
128 error
= ioctl(devfd
, CPUCTL_RDMSR
, &msrargs
);
130 WARN(0, "ioctl(%s)", dev
);
133 revision
= msrargs
.data
>> 32; /* Revision in the high dword. */
134 WARNX(2, "found cpu type %#x family %#x model %#x stepping %#x.",
135 (signature
>> 12) & 0x03, (signature
>> 8) & 0x0f,
136 (signature
>> 4) & 0x0f, (signature
>> 0) & 0x0f);
138 * Open firmware image.
140 fd
= open(path
, O_RDONLY
, 0);
142 WARN(0, "open(%s)", path
);
145 error
= fstat(fd
, &st
);
147 WARN(0, "fstat(%s)", path
);
150 if (st
.st_size
< 0 || (unsigned)st
.st_size
< sizeof(*fw_header
)) {
151 WARNX(2, "file too short: %s", path
);
156 * mmap the whole image.
158 fw_image
= (uint32_t *)mmap(NULL
, st
.st_size
, PROT_READ
,
160 if (fw_image
== MAP_FAILED
) {
161 WARN(0, "mmap(%s)", path
);
164 fw_header
= (via_fw_header_t
*)fw_image
;
165 if (fw_header
->signature
!= VIA_HEADER_SIGNATURE
||
166 fw_header
->loader_revision
!= VIA_LOADER_REVISION
) {
167 WARNX(2, "%s is not a valid via firmware: version mismatch",
171 data_size
= fw_header
->data_size
;
172 total_size
= fw_header
->total_size
;
173 if (total_size
> (unsigned)st
.st_size
|| st
.st_size
< 0) {
174 WARNX(2, "file too short: %s", path
);
177 payload_size
= data_size
+ sizeof(*fw_header
);
180 * Check the primary checksum.
183 for (i
= 0; i
< (payload_size
/ sizeof(uint32_t)); i
++)
184 sum
+= *((uint32_t *)fw_image
+ i
);
186 WARNX(2, "%s: update data checksum invalid", path
);
190 fw_data
= fw_header
+ 1; /* Pointer to the update data. */
193 * Check if the given image is ok for this cpu.
195 if (signature
!= fw_header
->cpu_signature
)
198 if (fw_header
->revision
!= 0 && revision
>= fw_header
->revision
) {
199 WARNX(1, "skipping %s of rev %#x: up to date",
200 path
, fw_header
->revision
);
203 fprintf(stderr
, "%s: updating cpu %s from rev %#x to rev %#x... ",
204 path
, dev
, revision
, fw_header
->revision
);
206 args
.size
= data_size
;
207 error
= ioctl(devfd
, CPUCTL_UPDATE
, &args
);
210 fprintf(stderr
, "failed.\n");
215 fprintf(stderr
, "done.\n");
218 if (fw_image
!= MAP_FAILED
)
219 if (munmap(fw_image
, st
.st_size
) != 0)
220 warn("munmap(%s)", path
);