1/*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2011 Fabien Thomas <fabient@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 "via.h"
50
51int
52via_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
76void
77via_update(const struct ucode_update_params *params)
78{
79	int devfd;
80	const char *dev, *path;
81	const uint32_t *fw_image;
82	uint32_t sum;
83	unsigned int i;
84	size_t payload_size;
85	const via_fw_header_t *fw_header;
86	uint32_t signature;
87	int32_t revision;
88	const 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	dev = params->dev_path;
100	path = params->fw_path;
101	devfd = params->devfd;
102	fw_image = params->fwimage;
103
104	assert(path);
105	assert(dev);
106
107	error = ioctl(devfd, CPUCTL_CPUID, &idargs);
108	if (error < 0) {
109		WARN(0, "ioctl(%s)", dev);
110		goto fail;
111	}
112	signature = idargs.data[0];
113	error = ioctl(devfd, CPUCTL_RDMSR, &msrargs);
114	if (error < 0) {
115		WARN(0, "ioctl(%s)", dev);
116		goto fail;
117	}
118
119	/*
120	 * MSR_IA32_PLATFORM_ID contains flag in BCD in bits 52-50.
121	 */
122	msrargs.msr = MSR_BIOS_SIGN;
123	error = ioctl(devfd, CPUCTL_RDMSR, &msrargs);
124	if (error < 0) {
125		WARN(0, "ioctl(%s)", dev);
126		goto fail;
127	}
128	revision = msrargs.data >> 32; /* Revision in the high dword. */
129	WARNX(2, "found cpu type %#x family %#x model %#x stepping %#x.",
130	    (signature >> 12) & 0x03, (signature >> 8) & 0x0f,
131	    (signature >> 4) & 0x0f, (signature >> 0) & 0x0f);
132
133	if (params->fwsize < sizeof(*fw_header)) {
134		WARNX(2, "file too short: %s", path);
135		goto fail;
136	}
137
138	fw_header = (const via_fw_header_t *)fw_image;
139	if (fw_header->signature != VIA_HEADER_SIGNATURE ||
140	    fw_header->loader_revision != VIA_LOADER_REVISION) {
141		WARNX(2, "%s is not a valid via firmware: version mismatch",
142		    path);
143		goto fail;
144	}
145	data_size = fw_header->data_size;
146	total_size = fw_header->total_size;
147	if (total_size > params->fwsize) {
148		WARNX(2, "file too short: %s", path);
149		goto fail;
150	}
151	payload_size = data_size + sizeof(*fw_header);
152
153	/*
154	 * Check the primary checksum.
155	 */
156	sum = 0;
157	for (i = 0; i < (payload_size / sizeof(uint32_t)); i++)
158		sum += *((const uint32_t *)fw_image + i);
159	if (sum != 0) {
160		WARNX(2, "%s: update data checksum invalid", path);
161		goto fail;
162	}
163
164	fw_data = fw_header + 1; /* Pointer to the update data. */
165
166	/*
167	 * Check if the given image is ok for this cpu.
168	 */
169	if (signature != fw_header->cpu_signature)
170		goto fail;
171
172	if (fw_header->revision != 0 && revision >= fw_header->revision) {
173		WARNX(1, "skipping %s of rev %#x: up to date",
174		    path, fw_header->revision);
175		goto fail;
176	}
177	fprintf(stderr, "%s: updating cpu %s from rev %#x to rev %#x... ",
178			path, dev, revision, fw_header->revision);
179	args.data = __DECONST(void *, fw_data);
180	args.size = data_size;
181	error = ioctl(devfd, CPUCTL_UPDATE, &args);
182	if (error < 0) {
183               error = errno;
184	       fprintf(stderr, "failed.\n");
185               errno = error;
186	       WARN(0, "ioctl()");
187	       goto fail;
188	}
189	fprintf(stderr, "done.\n");
190
191fail:
192	return;
193}
194