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$"); 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/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 "intel.h" 49 50 #define DEFAULT_UCODE_SIZE 2000 /* Size of update data if not specified. */ 51 52 int 53 intel_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, INTEL_VENDOR_ID, sizeof(INTEL_VENDOR_ID)) != 0) 71 return (1); 72 return (0); 73 } 74 75 void 76 intel_update(const char *dev, const char *path) 77 { 78 int fd, devfd; 79 struct stat st; 80 uint32_t *fw_image; 81 int have_ext_table; 82 uint32_t sum; 83 unsigned int i; 84 size_t payload_size; 85 intel_fw_header_t *fw_header; 86 intel_cpu_signature_t *ext_table; 87 intel_ext_header_t *ext_header; 88 uint32_t signature, flags; 89 int32_t revision; 90 ssize_t ext_size; 91 size_t ext_table_size; 92 void *fw_data; 93 size_t data_size, total_size; 94 cpuctl_msr_args_t msrargs = { 95 .msr = MSR_IA32_PLATFORM_ID, 96 }; 97 cpuctl_cpuid_args_t idargs = { 98 .level = 1, /* Signature. */ 99 }; 100 cpuctl_update_args_t args; 101 int error; 102 103 assert(path); 104 assert(dev); 105 106 fd = -1; 107 fw_image = MAP_FAILED; 108 ext_table = NULL; 109 ext_header = NULL; 110 devfd = open(dev, O_RDWR); 111 if (devfd < 0) { 112 WARN(0, "could not open %s for writing", dev); 113 return; 114 } 115 error = ioctl(devfd, CPUCTL_CPUID, &idargs); 116 if (error < 0) { 117 WARN(0, "ioctl(%s)", dev); 118 goto fail; 119 } 120 signature = idargs.data[0]; 121 error = ioctl(devfd, CPUCTL_RDMSR, &msrargs); 122 if (error < 0) { 123 WARN(0, "ioctl(%s)", dev); 124 goto fail; 125 } 126 127 /* 128 * MSR_IA32_PLATFORM_ID contains flag in BCD in bits 52-50. 129 */ 130 flags = 1 << ((msrargs.data >> 50) & 7); 131 msrargs.msr = MSR_BIOS_SIGN; 132 error = ioctl(devfd, CPUCTL_RDMSR, &msrargs); 133 if (error < 0) { 134 WARN(0, "ioctl(%s)", dev); 135 goto fail; 136 } 137 revision = msrargs.data >> 32; /* Revision in the high dword. */ 138 WARNX(2, "found cpu type %#x family %#x model %#x stepping %#x.", 139 (signature >> 12) & 0x03, (signature >> 8) & 0x0f, 140 (signature >> 4) & 0x0f, (signature >> 0) & 0x0f); 141 /* 142 * Open firmware image. 143 */ 144 fd = open(path, O_RDONLY, 0); 145 if (fd < 0) { 146 WARN(0, "open(%s)", path); 147 return; 148 } 149 error = fstat(fd, &st); 150 if (error != 0) { 151 WARN(0, "fstat(%s)", path); 152 goto fail; 153 } 154 if (st.st_size < 0 || (unsigned)st.st_size < sizeof(*fw_header)) { 155 WARNX(2, "file too short: %s", path); 156 goto fail; 157 } 158 159 /* 160 * mmap the whole image. 161 */ 162 fw_image = (uint32_t *)mmap(NULL, st.st_size, PROT_READ, 163 MAP_PRIVATE, fd, 0); 164 if (fw_image == MAP_FAILED) { 165 WARN(0, "mmap(%s)", path); 166 goto fail; 167 } 168 fw_header = (intel_fw_header_t *)fw_image; 169 if (fw_header->header_version != INTEL_HEADER_VERSION || 170 fw_header->loader_revision != INTEL_LOADER_REVISION) { 171 WARNX(2, "%s is not a valid intel firmware: version mismatch", 172 path); 173 goto fail; 174 } 175 /* 176 * According to spec, if data_size == 0, then size of ucode = 2000. 177 */ 178 if (fw_header->data_size == 0) 179 data_size = DEFAULT_UCODE_SIZE; 180 else 181 data_size = fw_header->data_size; 182 if (fw_header->total_size == 0) 183 total_size = data_size + sizeof(*fw_header); 184 else 185 total_size = fw_header->total_size; 186 if (total_size > (unsigned)st.st_size || st.st_size < 0) { 187 WARNX(2, "file too short: %s", path); 188 goto fail; 189 } 190 payload_size = data_size + sizeof(*fw_header); 191 192 /* 193 * Check the primary checksum. 194 */ 195 sum = 0; 196 for (i = 0; i < (payload_size / sizeof(uint32_t)); i++) 197 sum += *((uint32_t *)fw_image + i); 198 if (sum != 0) { 199 WARNX(2, "%s: update data checksum invalid", path); 200 goto fail; 201 } 202 203 /* 204 * Check if there is an extended signature table. 205 */ 206 ext_size = total_size - payload_size; 207 have_ext_table = 0; 208 209 if (ext_size > (signed)sizeof(*ext_header)) { 210 ext_header = 211 (intel_ext_header_t *)((char *)fw_image + payload_size); 212 ext_table = (intel_cpu_signature_t *)(ext_header + 1); 213 214 /* 215 * Check the extended table size. 216 */ 217 ext_table_size = sizeof(*ext_header) + 218 ext_header->sig_count * sizeof(*ext_table); 219 if (ext_table_size + payload_size > total_size) { 220 WARNX(2, "%s: broken extended signature table", path); 221 goto no_table; 222 } 223 224 /* 225 * Check the extended table signature. 226 */ 227 sum = 0; 228 for (i = 0; i < (ext_table_size / sizeof(uint32_t)); i++) 229 sum += *((uint32_t *)ext_header + i); 230 if (sum != 0) { 231 WARNX(2, "%s: extended signature table checksum invalid", 232 path); 233 goto no_table; 234 } 235 have_ext_table = 1; 236 } 237 238 no_table: 239 fw_data = fw_header + 1; /* Pointer to the update data. */ 240 241 /* 242 * Check if the given image is ok for this cpu. 243 */ 244 if (signature == fw_header->cpu_signature && 245 (flags & fw_header->cpu_flags) != 0) 246 goto matched; 247 else if (have_ext_table != 0) { 248 for (i = 0; i < ext_header->sig_count; i++) { 249 uint32_t sig = ext_table[i].cpu_signature; 250 if (signature == sig && 251 (flags & ext_table[i].cpu_flags) != 0) 252 goto matched; 253 } 254 } else 255 goto fail; 256 257 matched: 258 if (revision >= fw_header->revision) { 259 WARNX(1, "skipping %s of rev %#x: up to date", 260 path, fw_header->revision); 261 return; 262 } 263 fprintf(stderr, "%s: updating cpu %s from rev %#x to rev %#x... ", 264 path, dev, revision, fw_header->revision); 265 args.data = fw_data; 266 args.size = data_size; 267 error = ioctl(devfd, CPUCTL_UPDATE, &args); 268 if (error < 0) { 269 fprintf(stderr, "failed.\n"); 270 WARN(0, "ioctl()"); 271 goto fail; 272 } 273 fprintf(stderr, "done.\n"); 274 275 fail: 276 if (fw_image != MAP_FAILED) 277 if (munmap(fw_image, st.st_size) != 0) 278 warn("munmap(%s)", path); 279 if (devfd >= 0) 280 close(devfd); 281 if (fd >= 0) 282 close(fd); 283 return; 284 } 285