1181430Sstas/*-
2181430Sstas * Copyright (c) 2006, 2008 Stanislav Sedov <stas@FreeBSD.org>.
3181430Sstas * All rights reserved.
4181430Sstas *
5181430Sstas * Redistribution and use in source and binary forms, with or without
6181430Sstas * modification, are permitted provided that the following conditions
7181430Sstas * are met:
8181430Sstas * 1. Redistributions of source code must retain the above copyright
9181430Sstas *    notice, this list of conditions and the following disclaimer.
10181430Sstas * 2. Redistributions in binary form must reproduce the above copyright
11181430Sstas *    notice, this list of conditions and the following disclaimer in the
12181430Sstas *    documentation and/or other materials provided with the distribution.
13181430Sstas *
14181430Sstas * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15181430Sstas * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16181430Sstas * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17181430Sstas * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18181430Sstas * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19181430Sstas * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20181430Sstas * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21181430Sstas * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22181430Sstas * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23181430Sstas * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24181430Sstas */
25181430Sstas
26181430Sstas#include <sys/cdefs.h>
27181430Sstas__FBSDID("$FreeBSD$");
28181430Sstas
29181430Sstas#include <assert.h>
30181430Sstas#include <stdio.h>
31181430Sstas#include <stdlib.h>
32181430Sstas#include <string.h>
33181430Sstas#include <unistd.h>
34181430Sstas#include <fcntl.h>
35181430Sstas#include <err.h>
36245491Seadler#include <errno.h>
37181430Sstas
38181430Sstas#include <sys/types.h>
39181430Sstas#include <sys/stat.h>
40181430Sstas#include <sys/mman.h>
41181430Sstas#include <sys/ioctl.h>
42181430Sstas#include <sys/ioccom.h>
43181430Sstas#include <sys/cpuctl.h>
44181430Sstas
45181430Sstas#include <machine/cpufunc.h>
46181430Sstas#include <machine/specialreg.h>
47181430Sstas
48181430Sstas#include "cpucontrol.h"
49181430Sstas#include "intel.h"
50181430Sstas
51181430Sstas#define	DEFAULT_UCODE_SIZE	2000 /* Size of update data if not specified. */
52181430Sstas
53181430Sstasint
54181430Sstasintel_probe(int fd)
55181430Sstas{
56181430Sstas	char vendor[13];
57181430Sstas	int error;
58181430Sstas	cpuctl_cpuid_args_t idargs = {
59181430Sstas		.level  = 0,
60181430Sstas	};
61181430Sstas
62181430Sstas	error = ioctl(fd, CPUCTL_CPUID, &idargs);
63181430Sstas	if (error < 0) {
64181430Sstas		WARN(0, "ioctl()");
65181430Sstas		return (1);
66181430Sstas	}
67181430Sstas	((uint32_t *)vendor)[0] = idargs.data[1];
68181430Sstas	((uint32_t *)vendor)[1] = idargs.data[3];
69181430Sstas	((uint32_t *)vendor)[2] = idargs.data[2];
70181430Sstas	vendor[12] = '\0';
71181430Sstas	if (strncmp(vendor, INTEL_VENDOR_ID, sizeof(INTEL_VENDOR_ID)) != 0)
72181430Sstas		return (1);
73181430Sstas	return (0);
74181430Sstas}
75181430Sstas
76181430Sstasvoid
77181430Sstasintel_update(const char *dev, const char *path)
78181430Sstas{
79181430Sstas	int fd, devfd;
80181430Sstas	struct stat st;
81181430Sstas	uint32_t *fw_image;
82181430Sstas	int have_ext_table;
83181430Sstas	uint32_t sum;
84181430Sstas	unsigned int i;
85181430Sstas	size_t payload_size;
86181430Sstas	intel_fw_header_t *fw_header;
87181430Sstas	intel_cpu_signature_t *ext_table;
88181430Sstas	intel_ext_header_t *ext_header;
89181430Sstas	uint32_t signature, flags;
90181430Sstas	int32_t revision;
91181430Sstas	ssize_t ext_size;
92181430Sstas	size_t ext_table_size;
93181430Sstas	void *fw_data;
94181430Sstas	size_t data_size, total_size;
95181430Sstas	cpuctl_msr_args_t msrargs = {
96181430Sstas		.msr = MSR_IA32_PLATFORM_ID,
97181430Sstas	};
98181430Sstas	cpuctl_cpuid_args_t idargs = {
99181430Sstas		.level  = 1,	/* Signature. */
100181430Sstas	};
101181430Sstas	cpuctl_update_args_t args;
102181430Sstas	int error;
103181430Sstas
104181430Sstas	assert(path);
105181430Sstas	assert(dev);
106181430Sstas
107181430Sstas	fd = -1;
108181430Sstas	fw_image = MAP_FAILED;
109181430Sstas	ext_table = NULL;
110181430Sstas	ext_header = NULL;
111181430Sstas	devfd = open(dev, O_RDWR);
112181430Sstas	if (devfd < 0) {
113181430Sstas		WARN(0, "could not open %s for writing", dev);
114181430Sstas		return;
115181430Sstas	}
116181430Sstas	error = ioctl(devfd, CPUCTL_CPUID, &idargs);
117181430Sstas	if (error < 0) {
118181430Sstas		WARN(0, "ioctl(%s)", dev);
119181430Sstas		goto fail;
120181430Sstas	}
121181430Sstas	signature = idargs.data[0];
122181430Sstas	error = ioctl(devfd, CPUCTL_RDMSR, &msrargs);
123181430Sstas	if (error < 0) {
124181430Sstas		WARN(0, "ioctl(%s)", dev);
125181430Sstas		goto fail;
126181430Sstas	}
127181430Sstas
128181430Sstas	/*
129181430Sstas	 * MSR_IA32_PLATFORM_ID contains flag in BCD in bits 52-50.
130181430Sstas	 */
131181430Sstas	flags = 1 << ((msrargs.data >> 50) & 7);
132181430Sstas	msrargs.msr = MSR_BIOS_SIGN;
133181430Sstas	error = ioctl(devfd, CPUCTL_RDMSR, &msrargs);
134181430Sstas	if (error < 0) {
135181430Sstas		WARN(0, "ioctl(%s)", dev);
136181430Sstas		goto fail;
137181430Sstas	}
138181430Sstas	revision = msrargs.data >> 32; /* Revision in the high dword. */
139181430Sstas	WARNX(2, "found cpu type %#x family %#x model %#x stepping %#x.",
140181430Sstas	    (signature >> 12) & 0x03, (signature >> 8) & 0x0f,
141181430Sstas	    (signature >> 4) & 0x0f, (signature >> 0) & 0x0f);
142181430Sstas	/*
143181430Sstas	 * Open firmware image.
144181430Sstas	 */
145181430Sstas	fd = open(path, O_RDONLY, 0);
146181430Sstas	if (fd < 0) {
147181430Sstas		WARN(0, "open(%s)", path);
148181430Sstas		return;
149181430Sstas	}
150181430Sstas	error = fstat(fd, &st);
151181430Sstas	if (error != 0) {
152181430Sstas		WARN(0, "fstat(%s)", path);
153181430Sstas		goto fail;
154181430Sstas	}
155181430Sstas	if (st.st_size < 0 || (unsigned)st.st_size < sizeof(*fw_header)) {
156181430Sstas		WARNX(2, "file too short: %s", path);
157181430Sstas		goto fail;
158181430Sstas	}
159181430Sstas
160181430Sstas	/*
161181430Sstas	 * mmap the whole image.
162181430Sstas	 */
163181430Sstas	fw_image = (uint32_t *)mmap(NULL, st.st_size, PROT_READ,
164181430Sstas	    MAP_PRIVATE, fd, 0);
165181430Sstas	if  (fw_image == MAP_FAILED) {
166181430Sstas		WARN(0, "mmap(%s)", path);
167181430Sstas		goto fail;
168181430Sstas	}
169181430Sstas	fw_header = (intel_fw_header_t *)fw_image;
170181430Sstas	if (fw_header->header_version != INTEL_HEADER_VERSION ||
171181430Sstas	    fw_header->loader_revision != INTEL_LOADER_REVISION) {
172181430Sstas		WARNX(2, "%s is not a valid intel firmware: version mismatch",
173181430Sstas		    path);
174181430Sstas		goto fail;
175181430Sstas	}
176181430Sstas	/*
177181430Sstas	 * According to spec, if data_size == 0, then size of ucode = 2000.
178181430Sstas	 */
179181430Sstas	if (fw_header->data_size == 0)
180181430Sstas		data_size = DEFAULT_UCODE_SIZE;
181181430Sstas	else
182181430Sstas		data_size = fw_header->data_size;
183181430Sstas	if (fw_header->total_size == 0)
184181430Sstas		total_size = data_size + sizeof(*fw_header);
185181430Sstas	else
186181430Sstas		total_size = fw_header->total_size;
187181430Sstas	if (total_size > (unsigned)st.st_size || st.st_size < 0) {
188181430Sstas		WARNX(2, "file too short: %s", path);
189181430Sstas		goto fail;
190181430Sstas	}
191181430Sstas	payload_size = data_size + sizeof(*fw_header);
192181430Sstas
193181430Sstas	/*
194181430Sstas	 * Check the primary checksum.
195181430Sstas	 */
196181430Sstas	sum = 0;
197181430Sstas	for (i = 0; i < (payload_size / sizeof(uint32_t)); i++)
198181430Sstas		sum += *((uint32_t *)fw_image + i);
199181430Sstas	if (sum != 0) {
200181430Sstas		WARNX(2, "%s: update data checksum invalid", path);
201181430Sstas		goto fail;
202181430Sstas	}
203181430Sstas
204181430Sstas	/*
205181430Sstas	 * Check if there is an extended signature table.
206181430Sstas	 */
207181430Sstas	ext_size = total_size - payload_size;
208181430Sstas	have_ext_table = 0;
209181430Sstas
210181430Sstas	if (ext_size > (signed)sizeof(*ext_header)) {
211181430Sstas		ext_header =
212181430Sstas		    (intel_ext_header_t *)((char *)fw_image + payload_size);
213181430Sstas		ext_table = (intel_cpu_signature_t *)(ext_header + 1);
214181430Sstas
215181430Sstas		/*
216181430Sstas		 * Check the extended table size.
217181430Sstas		 */
218181430Sstas		ext_table_size = sizeof(*ext_header) +
219181430Sstas		    ext_header->sig_count * sizeof(*ext_table);
220181430Sstas		if (ext_table_size + payload_size > total_size) {
221181430Sstas			WARNX(2, "%s: broken extended signature table", path);
222181430Sstas			goto no_table;
223181430Sstas		}
224181430Sstas
225181430Sstas		/*
226181430Sstas		 * Check the extended table signature.
227181430Sstas		 */
228181430Sstas		sum = 0;
229181430Sstas		for (i = 0; i < (ext_table_size / sizeof(uint32_t)); i++)
230181430Sstas			sum += *((uint32_t *)ext_header + i);
231181430Sstas		if (sum != 0) {
232181430Sstas			WARNX(2, "%s: extended signature table checksum invalid",
233181430Sstas			    path);
234181430Sstas			goto no_table;
235181430Sstas		}
236181430Sstas		have_ext_table = 1;
237181430Sstas	}
238181430Sstas
239181430Sstasno_table:
240181430Sstas	fw_data = fw_header + 1; /* Pointer to the update data. */
241181430Sstas
242181430Sstas	/*
243181430Sstas	 * Check if the given image is ok for this cpu.
244181430Sstas	 */
245181430Sstas	if (signature == fw_header->cpu_signature &&
246181430Sstas	    (flags & fw_header->cpu_flags) != 0)
247181430Sstas			goto matched;
248181430Sstas	else if (have_ext_table != 0) {
249181430Sstas		for (i = 0; i < ext_header->sig_count; i++) {
250181430Sstas			uint32_t sig = ext_table[i].cpu_signature;
251181430Sstas			if (signature == sig &&
252181430Sstas			    (flags & ext_table[i].cpu_flags) != 0)
253181430Sstas				goto matched;
254181430Sstas		}
255181430Sstas	} else
256181430Sstas		goto fail;
257181430Sstas
258181430Sstasmatched:
259181430Sstas	if (revision >= fw_header->revision) {
260181430Sstas		WARNX(1, "skipping %s of rev %#x: up to date",
261181430Sstas		    path, fw_header->revision);
262181430Sstas		return;
263181430Sstas	}
264181430Sstas	fprintf(stderr, "%s: updating cpu %s from rev %#x to rev %#x... ",
265181430Sstas			path, dev, revision, fw_header->revision);
266181430Sstas	args.data = fw_data;
267181430Sstas	args.size = data_size;
268181430Sstas	error = ioctl(devfd, CPUCTL_UPDATE, &args);
269181430Sstas	if (error < 0) {
270245491Seadler               error = errno;
271181430Sstas		fprintf(stderr, "failed.\n");
272245491Seadler               errno = error;
273181430Sstas		WARN(0, "ioctl()");
274181430Sstas		goto fail;
275181430Sstas	}
276181430Sstas	fprintf(stderr, "done.\n");
277181430Sstas
278181430Sstasfail:
279181430Sstas	if (fw_image != MAP_FAILED)
280181430Sstas		if (munmap(fw_image, st.st_size) != 0)
281181430Sstas			warn("munmap(%s)", path);
282181430Sstas	if (devfd >= 0)
283181430Sstas		close(devfd);
284181430Sstas	if (fd >= 0)
285181430Sstas		close(fd);
286181430Sstas	return;
287181430Sstas}
288