2 * Copyright (c) 2011 Fabien Thomas <fabient@FreeBSD.org>.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD: release/10.0.0/usr.sbin/cpucontrol/via.c 245491 2013-01-16 05:00:51Z eadler $");
38 #include <sys/types.h>
41 #include <sys/ioctl.h>
42 #include <sys/ioccom.h>
43 #include <sys/cpuctl.h>
45 #include <machine/cpufunc.h>
46 #include <machine/specialreg.h>
48 #include "cpucontrol.h"
56 cpuctl_cpuid_args_t idargs
= {
60 error
= ioctl(fd
, CPUCTL_CPUID
, &idargs
);
65 ((uint32_t *)vendor
)[0] = idargs
.data
[1];
66 ((uint32_t *)vendor
)[1] = idargs
.data
[3];
67 ((uint32_t *)vendor
)[2] = idargs
.data
[2];
69 if (strncmp(vendor
, CENTAUR_VENDOR_ID
, sizeof(CENTAUR_VENDOR_ID
)) != 0)
72 /* TODO: detect Nano CPU. */
77 via_update(const char *dev
, const char *path
)
85 via_fw_header_t
*fw_header
;
89 size_t data_size
, total_size
;
90 cpuctl_msr_args_t msrargs
= {
91 .msr
= MSR_IA32_PLATFORM_ID
,
93 cpuctl_cpuid_args_t idargs
= {
94 .level
= 1, /* Signature. */
96 cpuctl_update_args_t args
;
104 fw_image
= MAP_FAILED
;
105 devfd
= open(dev
, O_RDWR
);
107 WARN(0, "could not open %s for writing", dev
);
110 error
= ioctl(devfd
, CPUCTL_CPUID
, &idargs
);
112 WARN(0, "ioctl(%s)", dev
);
115 signature
= idargs
.data
[0];
116 error
= ioctl(devfd
, CPUCTL_RDMSR
, &msrargs
);
118 WARN(0, "ioctl(%s)", dev
);
123 * MSR_IA32_PLATFORM_ID contains flag in BCD in bits 52-50.
125 msrargs
.msr
= MSR_BIOS_SIGN
;
126 error
= ioctl(devfd
, CPUCTL_RDMSR
, &msrargs
);
128 WARN(0, "ioctl(%s)", dev
);
131 revision
= msrargs
.data
>> 32; /* Revision in the high dword. */
132 WARNX(2, "found cpu type %#x family %#x model %#x stepping %#x.",
133 (signature
>> 12) & 0x03, (signature
>> 8) & 0x0f,
134 (signature
>> 4) & 0x0f, (signature
>> 0) & 0x0f);
136 * Open firmware image.
138 fd
= open(path
, O_RDONLY
, 0);
140 WARN(0, "open(%s)", path
);
143 error
= fstat(fd
, &st
);
145 WARN(0, "fstat(%s)", path
);
148 if (st
.st_size
< 0 || (unsigned)st
.st_size
< sizeof(*fw_header
)) {
149 WARNX(2, "file too short: %s", path
);
154 * mmap the whole image.
156 fw_image
= (uint32_t *)mmap(NULL
, st
.st_size
, PROT_READ
,
158 if (fw_image
== MAP_FAILED
) {
159 WARN(0, "mmap(%s)", path
);
162 fw_header
= (via_fw_header_t
*)fw_image
;
163 if (fw_header
->signature
!= VIA_HEADER_SIGNATURE
||
164 fw_header
->loader_revision
!= VIA_LOADER_REVISION
) {
165 WARNX(2, "%s is not a valid via firmware: version mismatch",
169 data_size
= fw_header
->data_size
;
170 total_size
= fw_header
->total_size
;
171 if (total_size
> (unsigned)st
.st_size
|| st
.st_size
< 0) {
172 WARNX(2, "file too short: %s", path
);
175 payload_size
= data_size
+ sizeof(*fw_header
);
178 * Check the primary checksum.
181 for (i
= 0; i
< (payload_size
/ sizeof(uint32_t)); i
++)
182 sum
+= *((uint32_t *)fw_image
+ i
);
184 WARNX(2, "%s: update data checksum invalid", path
);
188 fw_data
= fw_header
+ 1; /* Pointer to the update data. */
191 * Check if the given image is ok for this cpu.
193 if (signature
!= fw_header
->cpu_signature
)
196 if (fw_header
->revision
!= 0 && revision
>= fw_header
->revision
) {
197 WARNX(1, "skipping %s of rev %#x: up to date",
198 path
, fw_header
->revision
);
201 fprintf(stderr
, "%s: updating cpu %s from rev %#x to rev %#x... ",
202 path
, dev
, revision
, fw_header
->revision
);
204 args
.size
= data_size
;
205 error
= ioctl(devfd
, CPUCTL_UPDATE
, &args
);
208 fprintf(stderr
, "failed.\n");
213 fprintf(stderr
, "done.\n");
216 if (fw_image
!= MAP_FAILED
)
217 if (munmap(fw_image
, st
.st_size
) != 0)
218 warn("munmap(%s)", path
);