xref: /freebsd/usr.sbin/cpucontrol/intel.c (revision f7c32ed617858bcd22f8d1b03199099d50125721)
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 struct ucode_update_params *params)
80 {
81 	int devfd;
82 	const char *dev, *path;
83 	const uint32_t *fw_image;
84 	int have_ext_table;
85 	uint32_t sum;
86 	unsigned int i;
87 	size_t payload_size;
88 	const intel_fw_header_t *fw_header;
89 	const intel_cpu_signature_t *ext_table;
90 	const 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 	const 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 	dev = params->dev_path;
108 	path = params->fw_path;
109 	devfd = params->devfd;
110 	fw_image = params->fwimage;
111 
112 	assert(path);
113 	assert(dev);
114 
115 	ext_table = NULL;
116 	ext_header = NULL;
117 
118 	error = ioctl(devfd, CPUCTL_WRMSR, &msrargs);
119 	if (error < 0) {
120 		WARN(0, "ioctl(%s)", dev);
121 		goto fail;
122 	}
123 	error = ioctl(devfd, CPUCTL_CPUID, &idargs);
124 	if (error < 0) {
125 		WARN(0, "ioctl(%s)", dev);
126 		goto fail;
127 	}
128 	signature = idargs.data[0];
129 	msrargs.msr = MSR_IA32_PLATFORM_ID;
130 	error = ioctl(devfd, CPUCTL_RDMSR, &msrargs);
131 	if (error < 0) {
132 		WARN(0, "ioctl(%s)", dev);
133 		goto fail;
134 	}
135 
136 	/*
137 	 * MSR_IA32_PLATFORM_ID contains flag in BCD in bits 52-50.
138 	 */
139 	flags = 1 << ((msrargs.data >> 50) & 7);
140 	msrargs.msr = MSR_BIOS_SIGN;
141 	error = ioctl(devfd, CPUCTL_RDMSR, &msrargs);
142 	if (error < 0) {
143 		WARN(0, "ioctl(%s)", dev);
144 		goto fail;
145 	}
146 	revision = msrargs.data >> 32; /* Revision in the high dword. */
147 	WARNX(2, "found cpu type %#x family %#x model %#x stepping %#x.",
148 	    (signature >> 12) & 0x03, (signature >> 8) & 0x0f,
149 	    (signature >> 4) & 0x0f, (signature >> 0) & 0x0f);
150 	/*
151 	 * Open firmware image.
152 	 */
153 	if (params->fwsize < sizeof(*fw_header)) {
154 		WARNX(2, "file too short: %s", path);
155 		goto fail;
156 	}
157 
158 	fw_header = (const intel_fw_header_t *)fw_image;
159 	if (fw_header->header_version != INTEL_HEADER_VERSION ||
160 	    fw_header->loader_revision != INTEL_LOADER_REVISION) {
161 		WARNX(2, "%s is not a valid intel firmware: version mismatch",
162 		    path);
163 		goto fail;
164 	}
165 	/*
166 	 * According to spec, if data_size == 0, then size of ucode = 2000.
167 	 */
168 	if (fw_header->data_size == 0)
169 		data_size = DEFAULT_UCODE_SIZE;
170 	else
171 		data_size = fw_header->data_size;
172 	if (fw_header->total_size == 0)
173 		total_size = data_size + sizeof(*fw_header);
174 	else
175 		total_size = fw_header->total_size;
176 	if (total_size > params->fwsize) {
177 		WARNX(2, "file too short: %s", path);
178 		goto fail;
179 	}
180 	payload_size = data_size + sizeof(*fw_header);
181 
182 	/*
183 	 * Check the primary checksum.
184 	 */
185 	sum = 0;
186 	for (i = 0; i < (payload_size / sizeof(uint32_t)); i++)
187 		sum += *((const uint32_t *)fw_image + i);
188 	if (sum != 0) {
189 		WARNX(2, "%s: update data checksum invalid", path);
190 		goto fail;
191 	}
192 
193 	/*
194 	 * Check if there is an extended signature table.
195 	 */
196 	ext_size = total_size - payload_size;
197 	have_ext_table = 0;
198 
199 	if (ext_size > (signed)sizeof(*ext_header)) {
200 		ext_header = (const intel_ext_header_t *)
201 		    ((const char *)fw_image + payload_size);
202 		ext_table = (const intel_cpu_signature_t *)(ext_header + 1);
203 
204 		/*
205 		 * Check the extended table size.
206 		 */
207 		ext_table_size = sizeof(*ext_header) +
208 		    ext_header->sig_count * sizeof(*ext_table);
209 		if (ext_table_size + payload_size > total_size) {
210 			WARNX(2, "%s: broken extended signature table", path);
211 			goto no_table;
212 		}
213 
214 		/*
215 		 * Check the extended table signature.
216 		 */
217 		sum = 0;
218 		for (i = 0; i < (ext_table_size / sizeof(uint32_t)); i++)
219 			sum += *((const uint32_t *)ext_header + i);
220 		if (sum != 0) {
221 			WARNX(2,
222 			    "%s: extended signature table checksum invalid",
223 			    path);
224 			goto no_table;
225 		}
226 		have_ext_table = 1;
227 	}
228 
229 no_table:
230 	fw_data = fw_header + 1; /* Pointer to the update data. */
231 
232 	/*
233 	 * Check if the given image is ok for this cpu.
234 	 */
235 	if (signature == fw_header->cpu_signature &&
236 	    (flags & fw_header->cpu_flags) != 0)
237 		goto matched;
238 	else if (have_ext_table != 0) {
239 		for (i = 0; i < ext_header->sig_count; i++) {
240 			sig = ext_table[i].cpu_signature;
241 			if (signature == sig &&
242 			    (flags & ext_table[i].cpu_flags) != 0)
243 				goto matched;
244 		}
245 	}
246 	goto fail;
247 
248 matched:
249 	if (revision >= fw_header->revision) {
250 		WARNX(1, "skipping %s of rev %#x: up to date",
251 		    path, fw_header->revision);
252 		goto fail;
253 	}
254 	fprintf(stderr, "%s: updating cpu %s from rev %#x to rev %#x... ",
255 	    path, dev, revision, fw_header->revision);
256 	args.data = __DECONST(void *, fw_data);
257 	args.size = data_size;
258 	error = ioctl(devfd, CPUCTL_UPDATE, &args);
259 	if (error < 0) {
260 		error = errno;
261 		fprintf(stderr, "failed.\n");
262 		errno = error;
263 		WARN(0, "ioctl()");
264 		goto fail;
265 	}
266 	fprintf(stderr, "done.\n");
267 
268 fail:
269 	return;
270 }
271