2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2006, 2008 Stanislav Sedov <stas@FreeBSD.org>.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
39 #include <sys/types.h>
42 #include <sys/ioctl.h>
43 #include <sys/ioccom.h>
44 #include <sys/cpuctl.h>
46 #include <machine/cpufunc.h>
47 #include <machine/specialreg.h>
49 #include "cpucontrol.h"
57 cpuctl_cpuid_args_t idargs = {
61 error = ioctl(fd, CPUCTL_CPUID, &idargs);
66 ((uint32_t *)vendor)[0] = idargs.data[1];
67 ((uint32_t *)vendor)[1] = idargs.data[3];
68 ((uint32_t *)vendor)[2] = idargs.data[2];
70 if (strncmp(vendor, AMD_VENDOR_ID, sizeof(AMD_VENDOR_ID)) != 0)
76 amd_update(const struct ucode_update_params *params)
80 const char *dev, *path;
81 const uint32_t *fw_image;
82 const amd_fw_header_t *fw_header;
85 const uint32_t *fw_data;
87 cpuctl_cpuid_args_t idargs = {
88 .level = 1, /* Request signature. */
90 cpuctl_update_args_t args;
93 dev = params->dev_path;
94 path = params->fw_path;
95 devfd = params->devfd;
96 fw_image = params->fwimage;
101 error = ioctl(devfd, CPUCTL_CPUID, &idargs);
106 signature = idargs.data[0];
107 WARNX(2, "found cpu family %#x model %#x "
108 "stepping %#x extfamily %#x extmodel %#x.",
109 (signature >> 8) & 0x0f, (signature >> 4) & 0x0f,
110 (signature >> 0) & 0x0f, (signature >> 20) & 0xff,
111 (signature >> 16) & 0x0f);
114 * Open the firmware file.
116 if (params->fwsize < sizeof(*fw_header)) {
117 WARNX(2, "file too short: %s", path);
120 fw_header = (const amd_fw_header_t *)fw_image;
121 if ((fw_header->magic >> 8) != AMD_MAGIC) {
122 WARNX(2, "%s is not a valid amd firmware: version mismatch",
126 fw_data = (const uint32_t *)(fw_header + 1);
127 fw_size = (params->fwsize - sizeof(*fw_header)) / sizeof(uint32_t);
130 * Check the primary checksum.
133 for (i = 0; i < fw_size; i++)
135 if (sum != fw_header->checksum) {
136 WARNX(2, "%s: update data checksum invalid", path);
139 if (signature == fw_header->signature) {
140 fprintf(stderr, "%s: updating cpu %s... ", path, dev);
142 args.data = __DECONST(void *, fw_image);
143 args.size = params->fwsize;
144 error = ioctl(devfd, CPUCTL_UPDATE, &args);
146 fprintf(stderr, "failed.\n");
150 fprintf(stderr, "done.\n");