1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 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 __FBSDID("$FreeBSD$"); 30 31 #include <assert.h> 32 #include <stdio.h> 33 #include <stdlib.h> 34 #include <string.h> 35 #include <unistd.h> 36 #include <fcntl.h> 37 #include <err.h> 38 39 #include <sys/types.h> 40 #include <sys/stat.h> 41 #include <sys/mman.h> 42 #include <sys/ioctl.h> 43 #include <sys/ioccom.h> 44 #include <sys/cpuctl.h> 45 46 #include <machine/cpufunc.h> 47 #include <machine/specialreg.h> 48 49 #include "cpucontrol.h" 50 #include "amd.h" 51 52 int 53 amd_probe(int fd) 54 { 55 char vendor[13]; 56 int error; 57 cpuctl_cpuid_args_t idargs = { 58 .level = 0, 59 }; 60 61 error = ioctl(fd, CPUCTL_CPUID, &idargs); 62 if (error < 0) { 63 WARN(0, "ioctl()"); 64 return (1); 65 } 66 ((uint32_t *)vendor)[0] = idargs.data[1]; 67 ((uint32_t *)vendor)[1] = idargs.data[3]; 68 ((uint32_t *)vendor)[2] = idargs.data[2]; 69 vendor[12] = '\0'; 70 if (strncmp(vendor, AMD_VENDOR_ID, sizeof(AMD_VENDOR_ID)) != 0) 71 return (1); 72 return (0); 73 } 74 75 void 76 amd_update(const char *dev, const char *path) 77 { 78 int fd, devfd; 79 unsigned int i; 80 struct stat st; 81 uint32_t *fw_image; 82 amd_fw_header_t *fw_header; 83 uint32_t sum; 84 uint32_t signature; 85 uint32_t *fw_data; 86 size_t fw_size; 87 cpuctl_cpuid_args_t idargs = { 88 .level = 1, /* Request signature. */ 89 }; 90 cpuctl_update_args_t args; 91 int error; 92 93 assert(path); 94 assert(dev); 95 96 fd = -1; 97 fw_image = MAP_FAILED; 98 devfd = open(dev, O_RDWR); 99 if (devfd < 0) { 100 WARN(0, "could not open %s for writing", dev); 101 return; 102 } 103 error = ioctl(devfd, CPUCTL_CPUID, &idargs); 104 if (error < 0) { 105 WARN(0, "ioctl()"); 106 goto fail; 107 } 108 signature = idargs.data[0]; 109 WARNX(2, "found cpu family %#x model %#x " 110 "stepping %#x extfamily %#x extmodel %#x.", 111 (signature >> 8) & 0x0f, (signature >> 4) & 0x0f, 112 (signature >> 0) & 0x0f, (signature >> 20) & 0xff, 113 (signature >> 16) & 0x0f); 114 115 /* 116 * Open the firmware file. 117 */ 118 fd = open(path, O_RDONLY, 0); 119 if (fd < 0) { 120 WARN(0, "open(%s)", path); 121 goto fail; 122 } 123 error = fstat(fd, &st); 124 if (error != 0) { 125 WARN(0, "fstat(%s)", path); 126 goto fail; 127 } 128 if (st.st_size < 0 || (unsigned)st.st_size < sizeof(*fw_header)) { 129 WARNX(2, "file too short: %s", path); 130 goto fail; 131 } 132 /* 133 * mmap the whole image. 134 */ 135 fw_image = (uint32_t *)mmap(NULL, st.st_size, PROT_READ, 136 MAP_PRIVATE, fd, 0); 137 if (fw_image == MAP_FAILED) { 138 WARN(0, "mmap(%s)", path); 139 goto fail; 140 } 141 fw_header = (amd_fw_header_t *)fw_image; 142 if ((fw_header->magic >> 8) != AMD_MAGIC) { 143 WARNX(2, "%s is not a valid amd firmware: version mismatch", 144 path); 145 goto fail; 146 } 147 fw_data = (uint32_t *)(fw_header + 1); 148 fw_size = (st.st_size - sizeof(*fw_header)) / sizeof(uint32_t); 149 150 /* 151 * Check the primary checksum. 152 */ 153 sum = 0; 154 for (i = 0; i < fw_size; i++) 155 sum += fw_data[i]; 156 if (sum != fw_header->checksum) { 157 WARNX(2, "%s: update data checksum invalid", path); 158 goto fail; 159 } 160 if (signature == fw_header->signature) { 161 fprintf(stderr, "%s: updating cpu %s... ", path, dev); 162 163 args.data = fw_image; 164 args.size = st.st_size; 165 error = ioctl(devfd, CPUCTL_UPDATE, &args); 166 if (error < 0) { 167 fprintf(stderr, "failed.\n"); 168 warn("ioctl()"); 169 goto fail; 170 } 171 fprintf(stderr, "done.\n"); 172 } 173 174 fail: 175 if (fd >= 0) 176 close(fd); 177 if (devfd >= 0) 178 close(devfd); 179 if (fw_image != MAP_FAILED) 180 if(munmap(fw_image, st.st_size) != 0) 181 warn("munmap(%s)", path); 182 return; 183 } 184