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