xref: /freebsd/usr.sbin/cpucontrol/amd.c (revision a90b9d0159070121c221b966469c3e36d912bf82)
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 
37 #include <sys/types.h>
38 #include <sys/stat.h>
39 #include <sys/mman.h>
40 #include <sys/ioctl.h>
41 #include <sys/ioccom.h>
42 #include <sys/cpuctl.h>
43 
44 #include <machine/cpufunc.h>
45 #include <machine/specialreg.h>
46 
47 #include "cpucontrol.h"
48 #include "amd.h"
49 
50 int
51 amd_probe(int fd)
52 {
53 	char vendor[13];
54 	int error;
55 	cpuctl_cpuid_args_t idargs = {
56 		.level  = 0,
57 	};
58 
59 	error = ioctl(fd, CPUCTL_CPUID, &idargs);
60 	if (error < 0) {
61 		WARN(0, "ioctl()");
62 		return (1);
63 	}
64 	((uint32_t *)vendor)[0] = idargs.data[1];
65 	((uint32_t *)vendor)[1] = idargs.data[3];
66 	((uint32_t *)vendor)[2] = idargs.data[2];
67 	vendor[12] = '\0';
68 	if (strncmp(vendor, AMD_VENDOR_ID, sizeof(AMD_VENDOR_ID)) != 0)
69 		return (1);
70 	return (0);
71 }
72 
73 void
74 amd_update(const struct ucode_update_params *params)
75 {
76 	int devfd;
77 	unsigned int i;
78 	const char *dev, *path;
79 	const uint32_t *fw_image;
80 	const amd_fw_header_t *fw_header;
81 	uint32_t sum;
82 	uint32_t signature;
83 	const uint32_t *fw_data;
84 	size_t fw_size;
85 	cpuctl_cpuid_args_t idargs = {
86 		.level  = 1,	/* Request signature. */
87 	};
88 	cpuctl_update_args_t args;
89 	int error;
90 
91 	dev = params->dev_path;
92 	path = params->fw_path;
93 	devfd = params->devfd;
94 	fw_image = params->fwimage;
95 
96 	assert(path);
97 	assert(dev);
98 
99 	error = ioctl(devfd, CPUCTL_CPUID, &idargs);
100 	if (error < 0) {
101 		WARN(0, "ioctl()");
102 		goto fail;
103 	}
104 	signature = idargs.data[0];
105 	WARNX(2, "found cpu family %#x model %#x "
106 	    "stepping %#x extfamily %#x extmodel %#x.",
107 	    (signature >> 8) & 0x0f, (signature >> 4) & 0x0f,
108 	    (signature >> 0) & 0x0f, (signature >> 20) & 0xff,
109 	    (signature >> 16) & 0x0f);
110 
111 	/*
112 	 * Open the firmware file.
113 	 */
114 	if (params->fwsize < sizeof(*fw_header)) {
115 		WARNX(2, "file too short: %s", path);
116 		goto fail;
117 	}
118 	fw_header = (const amd_fw_header_t *)fw_image;
119 	if ((fw_header->magic >> 8) != AMD_MAGIC) {
120 		WARNX(2, "%s is not a valid amd firmware: version mismatch",
121 		    path);
122 		goto fail;
123 	}
124 	fw_data = (const uint32_t *)(fw_header + 1);
125 	fw_size = (params->fwsize - sizeof(*fw_header)) / sizeof(uint32_t);
126 
127 	/*
128 	 * Check the primary checksum.
129 	 */
130 	sum = 0;
131 	for (i = 0; i < fw_size; i++)
132 		sum += fw_data[i];
133 	if (sum != fw_header->checksum) {
134 		WARNX(2, "%s: update data checksum invalid", path);
135 		goto fail;
136 	}
137 	if (signature == fw_header->signature) {
138 		fprintf(stderr, "%s: updating cpu %s... ", path, dev);
139 
140 		args.data = __DECONST(void *, fw_image);
141 		args.size = params->fwsize;
142 		error = ioctl(devfd, CPUCTL_UPDATE, &args);
143 		if (error < 0) {
144 			fprintf(stderr, "failed.\n");
145 			warn("ioctl()");
146 			goto fail;
147 		}
148 		fprintf(stderr, "done.\n");
149 	}
150 
151 fail:
152 	return;
153 }
154