]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/cpucontrol/via.c
THIS BRANCH IS OBSOLETE, PLEASE READ:
[FreeBSD/FreeBSD.git] / usr.sbin / cpucontrol / via.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2011 Fabien Thomas <fabient@FreeBSD.org>.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
15  *
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.
26  */
27
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30
31 #include <assert.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <unistd.h>
36 #include <fcntl.h>
37 #include <err.h>
38 #include <errno.h>
39
40 #include <sys/types.h>
41 #include <sys/stat.h>
42 #include <sys/mman.h>
43 #include <sys/ioctl.h>
44 #include <sys/ioccom.h>
45 #include <sys/cpuctl.h>
46
47 #include <machine/cpufunc.h>
48 #include <machine/specialreg.h>
49
50 #include "cpucontrol.h"
51 #include "via.h"
52
53 int
54 via_probe(int fd)
55 {
56         char vendor[13];
57         int error;
58         cpuctl_cpuid_args_t idargs = {
59                 .level  = 0,
60         };
61
62         error = ioctl(fd, CPUCTL_CPUID, &idargs);
63         if (error < 0) {
64                 WARN(0, "ioctl()");
65                 return (1);
66         }
67         ((uint32_t *)vendor)[0] = idargs.data[1];
68         ((uint32_t *)vendor)[1] = idargs.data[3];
69         ((uint32_t *)vendor)[2] = idargs.data[2];
70         vendor[12] = '\0';
71         if (strncmp(vendor, CENTAUR_VENDOR_ID, sizeof(CENTAUR_VENDOR_ID)) != 0)
72                 return (1);
73
74         /* TODO: detect Nano CPU. */
75         return (0);
76 }
77
78 void
79 via_update(const struct ucode_update_params *params)
80 {
81         int devfd;
82         const char *dev, *path;
83         const uint32_t *fw_image;
84         uint32_t sum;
85         unsigned int i;
86         size_t payload_size;
87         const via_fw_header_t *fw_header;
88         uint32_t signature;
89         int32_t revision;
90         const void *fw_data;
91         size_t data_size, total_size;
92         cpuctl_msr_args_t msrargs = {
93                 .msr = MSR_IA32_PLATFORM_ID,
94         };
95         cpuctl_cpuid_args_t idargs = {
96                 .level  = 1,    /* Signature. */
97         };
98         cpuctl_update_args_t args;
99         int error;
100
101         dev = params->dev_path;
102         path = params->fw_path;
103         devfd = params->devfd;
104         fw_image = params->fwimage;
105
106         assert(path);
107         assert(dev);
108
109         error = ioctl(devfd, CPUCTL_CPUID, &idargs);
110         if (error < 0) {
111                 WARN(0, "ioctl(%s)", dev);
112                 goto fail;
113         }
114         signature = idargs.data[0];
115         error = ioctl(devfd, CPUCTL_RDMSR, &msrargs);
116         if (error < 0) {
117                 WARN(0, "ioctl(%s)", dev);
118                 goto fail;
119         }
120
121         /*
122          * MSR_IA32_PLATFORM_ID contains flag in BCD in bits 52-50.
123          */
124         msrargs.msr = MSR_BIOS_SIGN;
125         error = ioctl(devfd, CPUCTL_RDMSR, &msrargs);
126         if (error < 0) {
127                 WARN(0, "ioctl(%s)", dev);
128                 goto fail;
129         }
130         revision = msrargs.data >> 32; /* Revision in the high dword. */
131         WARNX(2, "found cpu type %#x family %#x model %#x stepping %#x.",
132             (signature >> 12) & 0x03, (signature >> 8) & 0x0f,
133             (signature >> 4) & 0x0f, (signature >> 0) & 0x0f);
134
135         if (params->fwsize < sizeof(*fw_header)) {
136                 WARNX(2, "file too short: %s", path);
137                 goto fail;
138         }
139
140         fw_header = (const via_fw_header_t *)fw_image;
141         if (fw_header->signature != VIA_HEADER_SIGNATURE ||
142             fw_header->loader_revision != VIA_LOADER_REVISION) {
143                 WARNX(2, "%s is not a valid via firmware: version mismatch",
144                     path);
145                 goto fail;
146         }
147         data_size = fw_header->data_size;
148         total_size = fw_header->total_size;
149         if (total_size > params->fwsize) {
150                 WARNX(2, "file too short: %s", path);
151                 goto fail;
152         }
153         payload_size = data_size + sizeof(*fw_header);
154
155         /*
156          * Check the primary checksum.
157          */
158         sum = 0;
159         for (i = 0; i < (payload_size / sizeof(uint32_t)); i++)
160                 sum += *((const uint32_t *)fw_image + i);
161         if (sum != 0) {
162                 WARNX(2, "%s: update data checksum invalid", path);
163                 goto fail;
164         }
165
166         fw_data = fw_header + 1; /* Pointer to the update data. */
167
168         /*
169          * Check if the given image is ok for this cpu.
170          */
171         if (signature != fw_header->cpu_signature)
172                 goto fail;
173
174         if (fw_header->revision != 0 && revision >= fw_header->revision) {
175                 WARNX(1, "skipping %s of rev %#x: up to date",
176                     path, fw_header->revision);
177                 goto fail;
178         }
179         fprintf(stderr, "%s: updating cpu %s from rev %#x to rev %#x... ",
180                         path, dev, revision, fw_header->revision);
181         args.data = __DECONST(void *, fw_data);
182         args.size = data_size;
183         error = ioctl(devfd, CPUCTL_UPDATE, &args);
184         if (error < 0) {
185                error = errno;
186                fprintf(stderr, "failed.\n");
187                errno = error;
188                WARN(0, "ioctl()");
189                goto fail;
190         }
191         fprintf(stderr, "done.\n");
192
193 fail:
194         return;
195 }