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