xref: /freebsd/usr.sbin/cpucontrol/intel.c (revision e08e9e999091f86081377b7cedc3fd2fe2ab70fc)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2006, 2008 Stanislav Sedov <stas@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 "intel.h"
52 
53 #define	DEFAULT_UCODE_SIZE	2000 /* Size of update data if not specified. */
54 
55 int
56 intel_probe(int fd)
57 {
58 	char vendor[13];
59 	int error;
60 	cpuctl_cpuid_args_t idargs = {
61 		.level  = 0,
62 	};
63 
64 	error = ioctl(fd, CPUCTL_CPUID, &idargs);
65 	if (error < 0) {
66 		WARN(0, "ioctl()");
67 		return (1);
68 	}
69 	((uint32_t *)vendor)[0] = idargs.data[1];
70 	((uint32_t *)vendor)[1] = idargs.data[3];
71 	((uint32_t *)vendor)[2] = idargs.data[2];
72 	vendor[12] = '\0';
73 	if (strncmp(vendor, INTEL_VENDOR_ID, sizeof(INTEL_VENDOR_ID)) != 0)
74 		return (1);
75 	return (0);
76 }
77 
78 void
79 intel_update(const char *dev, const char *path)
80 {
81 	int fd, devfd;
82 	struct stat st;
83 	uint32_t *fw_image;
84 	int have_ext_table;
85 	uint32_t sum;
86 	unsigned int i;
87 	size_t payload_size;
88 	intel_fw_header_t *fw_header;
89 	intel_cpu_signature_t *ext_table;
90 	intel_ext_header_t *ext_header;
91 	uint32_t sig, signature, flags;
92 	int32_t revision;
93 	ssize_t ext_size;
94 	size_t ext_table_size;
95 	void *fw_data;
96 	size_t data_size, total_size;
97 	cpuctl_msr_args_t msrargs = {
98 		.msr = MSR_BIOS_SIGN,
99 		.data = 0,
100 	};
101 	cpuctl_cpuid_args_t idargs = {
102 		.level  = 1,	/* Signature. */
103 	};
104 	cpuctl_update_args_t args;
105 	int error;
106 
107 	assert(path);
108 	assert(dev);
109 
110 	fd = -1;
111 	fw_image = MAP_FAILED;
112 	ext_table = NULL;
113 	ext_header = NULL;
114 	devfd = open(dev, O_RDWR);
115 	if (devfd < 0) {
116 		WARN(0, "could not open %s for writing", dev);
117 		return;
118 	}
119 	error = ioctl(devfd, CPUCTL_WRMSR, &msrargs);
120 	if (error < 0) {
121 		WARN(0, "ioctl(%s)", dev);
122 		goto fail;
123 	}
124 	error = ioctl(devfd, CPUCTL_CPUID, &idargs);
125 	if (error < 0) {
126 		WARN(0, "ioctl(%s)", dev);
127 		goto fail;
128 	}
129 	signature = idargs.data[0];
130 	msrargs.msr = MSR_IA32_PLATFORM_ID;
131 	error = ioctl(devfd, CPUCTL_RDMSR, &msrargs);
132 	if (error < 0) {
133 		WARN(0, "ioctl(%s)", dev);
134 		goto fail;
135 	}
136 
137 	/*
138 	 * MSR_IA32_PLATFORM_ID contains flag in BCD in bits 52-50.
139 	 */
140 	flags = 1 << ((msrargs.data >> 50) & 7);
141 	msrargs.msr = MSR_BIOS_SIGN;
142 	error = ioctl(devfd, CPUCTL_RDMSR, &msrargs);
143 	if (error < 0) {
144 		WARN(0, "ioctl(%s)", dev);
145 		goto fail;
146 	}
147 	revision = msrargs.data >> 32; /* Revision in the high dword. */
148 	WARNX(2, "found cpu type %#x family %#x model %#x stepping %#x.",
149 	    (signature >> 12) & 0x03, (signature >> 8) & 0x0f,
150 	    (signature >> 4) & 0x0f, (signature >> 0) & 0x0f);
151 	/*
152 	 * Open firmware image.
153 	 */
154 	fd = open(path, O_RDONLY, 0);
155 	if (fd < 0) {
156 		WARN(0, "open(%s)", path);
157 		goto fail;
158 	}
159 	error = fstat(fd, &st);
160 	if (error != 0) {
161 		WARN(0, "fstat(%s)", path);
162 		goto fail;
163 	}
164 	if (st.st_size < 0 || (unsigned)st.st_size < sizeof(*fw_header)) {
165 		WARNX(2, "file too short: %s", path);
166 		goto fail;
167 	}
168 
169 	/*
170 	 * mmap the whole image.
171 	 */
172 	fw_image = (uint32_t *)mmap(NULL, st.st_size, PROT_READ,
173 	    MAP_PRIVATE, fd, 0);
174 	if  (fw_image == MAP_FAILED) {
175 		WARN(0, "mmap(%s)", path);
176 		goto fail;
177 	}
178 	fw_header = (intel_fw_header_t *)fw_image;
179 	if (fw_header->header_version != INTEL_HEADER_VERSION ||
180 	    fw_header->loader_revision != INTEL_LOADER_REVISION) {
181 		WARNX(2, "%s is not a valid intel firmware: version mismatch",
182 		    path);
183 		goto fail;
184 	}
185 	/*
186 	 * According to spec, if data_size == 0, then size of ucode = 2000.
187 	 */
188 	if (fw_header->data_size == 0)
189 		data_size = DEFAULT_UCODE_SIZE;
190 	else
191 		data_size = fw_header->data_size;
192 	if (fw_header->total_size == 0)
193 		total_size = data_size + sizeof(*fw_header);
194 	else
195 		total_size = fw_header->total_size;
196 	if (total_size > (unsigned)st.st_size || st.st_size < 0) {
197 		WARNX(2, "file too short: %s", path);
198 		goto fail;
199 	}
200 	payload_size = data_size + sizeof(*fw_header);
201 
202 	/*
203 	 * Check the primary checksum.
204 	 */
205 	sum = 0;
206 	for (i = 0; i < (payload_size / sizeof(uint32_t)); i++)
207 		sum += *((uint32_t *)fw_image + i);
208 	if (sum != 0) {
209 		WARNX(2, "%s: update data checksum invalid", path);
210 		goto fail;
211 	}
212 
213 	/*
214 	 * Check if there is an extended signature table.
215 	 */
216 	ext_size = total_size - payload_size;
217 	have_ext_table = 0;
218 
219 	if (ext_size > (signed)sizeof(*ext_header)) {
220 		ext_header =
221 		    (intel_ext_header_t *)((char *)fw_image + payload_size);
222 		ext_table = (intel_cpu_signature_t *)(ext_header + 1);
223 
224 		/*
225 		 * Check the extended table size.
226 		 */
227 		ext_table_size = sizeof(*ext_header) +
228 		    ext_header->sig_count * sizeof(*ext_table);
229 		if (ext_table_size + payload_size > total_size) {
230 			WARNX(2, "%s: broken extended signature table", path);
231 			goto no_table;
232 		}
233 
234 		/*
235 		 * Check the extended table signature.
236 		 */
237 		sum = 0;
238 		for (i = 0; i < (ext_table_size / sizeof(uint32_t)); i++)
239 			sum += *((uint32_t *)ext_header + i);
240 		if (sum != 0) {
241 			WARNX(2,
242 			    "%s: extended signature table checksum invalid",
243 			    path);
244 			goto no_table;
245 		}
246 		have_ext_table = 1;
247 	}
248 
249 no_table:
250 	fw_data = fw_header + 1; /* Pointer to the update data. */
251 
252 	/*
253 	 * Check if the given image is ok for this cpu.
254 	 */
255 	if (signature == fw_header->cpu_signature &&
256 	    (flags & fw_header->cpu_flags) != 0)
257 		goto matched;
258 	else if (have_ext_table != 0) {
259 		for (i = 0; i < ext_header->sig_count; i++) {
260 			sig = ext_table[i].cpu_signature;
261 			if (signature == sig &&
262 			    (flags & ext_table[i].cpu_flags) != 0)
263 				goto matched;
264 		}
265 	} else
266 		goto fail;
267 
268 matched:
269 	if (revision >= fw_header->revision) {
270 		WARNX(1, "skipping %s of rev %#x: up to date",
271 		    path, fw_header->revision);
272 		goto fail;
273 	}
274 	fprintf(stderr, "%s: updating cpu %s from rev %#x to rev %#x... ",
275 	    path, dev, revision, fw_header->revision);
276 	args.data = fw_data;
277 	args.size = data_size;
278 	error = ioctl(devfd, CPUCTL_UPDATE, &args);
279 	if (error < 0) {
280 		error = errno;
281 		fprintf(stderr, "failed.\n");
282 		errno = error;
283 		WARN(0, "ioctl()");
284 		goto fail;
285 	}
286 	fprintf(stderr, "done.\n");
287 
288 fail:
289 	if (fw_image != MAP_FAILED)
290 		if (munmap(fw_image, st.st_size) != 0)
291 			warn("munmap(%s)", path);
292 	if (devfd >= 0)
293 		close(devfd);
294 	if (fd >= 0)
295 		close(fd);
296 }
297