1 /*- 2 * Copyright (c) 2006, 2008 Stanislav Sedov <stas@FreeBSD.org>. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 */ 25 26 #include <sys/cdefs.h> 27 __FBSDID("$FreeBSD: release/10.0.0/usr.sbin/cpucontrol/amd.c 236504 2012-06-03 08:30:00Z avg $"); 28 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/cpuctl.h> 42 43 #include <machine/cpufunc.h> 44 #include <machine/specialreg.h> 45 46 #include "cpucontrol.h" 47 #include "amd.h" 48 49 int 50 amd_probe(int fd) 51 { 52 char vendor[13]; 53 int error; 54 cpuctl_cpuid_args_t idargs = { 55 .level = 0, 56 }; 57 58 error = ioctl(fd, CPUCTL_CPUID, &idargs); 59 if (error < 0) { 60 WARN(0, "ioctl()"); 61 return (1); 62 } 63 ((uint32_t *)vendor)[0] = idargs.data[1]; 64 ((uint32_t *)vendor)[1] = idargs.data[3]; 65 ((uint32_t *)vendor)[2] = idargs.data[2]; 66 vendor[12] = '\0'; 67 if (strncmp(vendor, AMD_VENDOR_ID, sizeof(AMD_VENDOR_ID)) != 0) 68 return (1); 69 return (0); 70 } 71 72 void 73 amd_update(const char *dev, const char *path) 74 { 75 int fd, devfd; 76 unsigned int i; 77 struct stat st; 78 uint32_t *fw_image; 79 amd_fw_header_t *fw_header; 80 uint32_t sum; 81 uint32_t signature; 82 uint32_t *fw_data; 83 size_t fw_size; 84 cpuctl_cpuid_args_t idargs = { 85 .level = 1, /* Request signature. */ 86 }; 87 cpuctl_update_args_t args; 88 int error; 89 90 assert(path); 91 assert(dev); 92 93 fd = -1; 94 fw_image = MAP_FAILED; 95 devfd = open(dev, O_RDWR); 96 if (devfd < 0) { 97 WARN(0, "could not open %s for writing", dev); 98 return; 99 } 100 error = ioctl(devfd, CPUCTL_CPUID, &idargs); 101 if (error < 0) { 102 WARN(0, "ioctl()"); 103 goto fail; 104 } 105 signature = idargs.data[0]; 106 WARNX(2, "found cpu family %#x model %#x " 107 "stepping %#x extfamily %#x extmodel %#x.", 108 (signature >> 8) & 0x0f, (signature >> 4) & 0x0f, 109 (signature >> 0) & 0x0f, (signature >> 20) & 0xff, 110 (signature >> 16) & 0x0f); 111 112 /* 113 * Open the firmware file. 114 */ 115 fd = open(path, O_RDONLY, 0); 116 if (fd < 0) { 117 WARN(0, "open(%s)", path); 118 goto fail; 119 } 120 error = fstat(fd, &st); 121 if (error != 0) { 122 WARN(0, "fstat(%s)", path); 123 goto fail; 124 } 125 if (st.st_size < 0 || (unsigned)st.st_size < sizeof(*fw_header)) { 126 WARNX(2, "file too short: %s", path); 127 goto fail; 128 } 129 /* 130 * mmap the whole image. 131 */ 132 fw_image = (uint32_t *)mmap(NULL, st.st_size, PROT_READ, 133 MAP_PRIVATE, fd, 0); 134 if (fw_image == MAP_FAILED) { 135 WARN(0, "mmap(%s)", path); 136 goto fail; 137 } 138 fw_header = (amd_fw_header_t *)fw_image; 139 if ((fw_header->magic >> 8) != AMD_MAGIC) { 140 WARNX(2, "%s is not a valid amd firmware: version mismatch", 141 path); 142 goto fail; 143 } 144 fw_data = (uint32_t *)(fw_header + 1); 145 fw_size = (st.st_size - sizeof(*fw_header)) / sizeof(uint32_t); 146 147 /* 148 * Check the primary checksum. 149 */ 150 sum = 0; 151 for (i = 0; i < fw_size; i++) 152 sum += fw_data[i]; 153 if (sum != fw_header->checksum) { 154 WARNX(2, "%s: update data checksum invalid", path); 155 goto fail; 156 } 157 if (signature == fw_header->signature) { 158 fprintf(stderr, "%s: updating cpu %s... ", path, dev); 159 160 args.data = fw_image; 161 args.size = st.st_size; 162 error = ioctl(devfd, CPUCTL_UPDATE, &args); 163 if (error < 0) { 164 fprintf(stderr, "failed.\n"); 165 warn("ioctl()"); 166 goto fail; 167 } 168 fprintf(stderr, "done.\n"); 169 } 170 171 fail: 172 if (fd >= 0) 173 close(fd); 174 if (devfd >= 0) 175 close(devfd); 176 if (fw_image != MAP_FAILED) 177 if(munmap(fw_image, st.st_size) != 0) 178 warn("munmap(%s)", path); 179 return; 180 } 181