xref: /freebsd/usr.sbin/cpucontrol/via.c (revision f386f04f11679fd31731bce42208bb4363b79e75)
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;
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 	msrargs.msr = MSR_BIOS_SIGN;
126 	error = ioctl(devfd, CPUCTL_RDMSR, &msrargs);
127 	if (error < 0) {
128 		WARN(0, "ioctl(%s)", dev);
129 		goto fail;
130 	}
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);
135 	/*
136 	 * Open firmware image.
137 	 */
138 	fd = open(path, O_RDONLY, 0);
139 	if (fd < 0) {
140 		WARN(0, "open(%s)", path);
141 		return;
142 	}
143 	error = fstat(fd, &st);
144 	if (error != 0) {
145 		WARN(0, "fstat(%s)", path);
146 		goto fail;
147 	}
148 	if (st.st_size < 0 || (unsigned)st.st_size < sizeof(*fw_header)) {
149 		WARNX(2, "file too short: %s", path);
150 		goto fail;
151 	}
152 
153 	/*
154 	 * mmap the whole image.
155 	 */
156 	fw_image = (uint32_t *)mmap(NULL, st.st_size, PROT_READ,
157 	    MAP_PRIVATE, fd, 0);
158 	if  (fw_image == MAP_FAILED) {
159 		WARN(0, "mmap(%s)", path);
160 		goto fail;
161 	}
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",
166 		    path);
167 		goto fail;
168 	}
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);
173 		goto fail;
174 	}
175 	payload_size = data_size + sizeof(*fw_header);
176 
177 	/*
178 	 * Check the primary checksum.
179 	 */
180 	sum = 0;
181 	for (i = 0; i < (payload_size / sizeof(uint32_t)); i++)
182 		sum += *((uint32_t *)fw_image + i);
183 	if (sum != 0) {
184 		WARNX(2, "%s: update data checksum invalid", path);
185 		goto fail;
186 	}
187 
188 	fw_data = fw_header + 1; /* Pointer to the update data. */
189 
190 	/*
191 	 * Check if the given image is ok for this cpu.
192 	 */
193 	if (signature != fw_header->cpu_signature)
194 		goto fail;
195 
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);
199 		goto fail;
200 	}
201 	fprintf(stderr, "%s: updating cpu %s from rev %#x to rev %#x... ",
202 			path, dev, revision, fw_header->revision);
203 	args.data = fw_data;
204 	args.size = data_size;
205 	error = ioctl(devfd, CPUCTL_UPDATE, &args);
206 	if (error < 0) {
207                error = errno;
208 		fprintf(stderr, "failed.\n");
209                errno = error;
210 		WARN(0, "ioctl()");
211 		goto fail;
212 	}
213 	fprintf(stderr, "done.\n");
214 
215 fail:
216 	if (fw_image != MAP_FAILED)
217 		if (munmap(fw_image, st.st_size) != 0)
218 			warn("munmap(%s)", path);
219 	if (devfd >= 0)
220 		close(devfd);
221 	if (fd >= 0)
222 		close(fd);
223 	return;
224 }
225