1 /*- 2 * Copyright (c) 2011 Fabien Thomas <fabient@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 "via.h" 49 50 int 51 via_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, CENTAUR_VENDOR_ID, sizeof(CENTAUR_VENDOR_ID)) != 0) 69 return (1); 70 71 /* TODO: detect Nano CPU. */ 72 return (0); 73 } 74 75 void 76 via_update(const char *dev, const char *path) 77 { 78 int fd, devfd; 79 struct stat st; 80 uint32_t *fw_image; 81 uint32_t sum; 82 unsigned int i; 83 size_t payload_size; 84 via_fw_header_t *fw_header; 85 uint32_t signature, flags; 86 int32_t revision; 87 void *fw_data; 88 size_t data_size, total_size; 89 cpuctl_msr_args_t msrargs = { 90 .msr = MSR_IA32_PLATFORM_ID, 91 }; 92 cpuctl_cpuid_args_t idargs = { 93 .level = 1, /* Signature. */ 94 }; 95 cpuctl_update_args_t args; 96 int error; 97 98 assert(path); 99 assert(dev); 100 101 fd = -1; 102 devfd = -1; 103 fw_image = MAP_FAILED; 104 devfd = open(dev, O_RDWR); 105 if (devfd < 0) { 106 WARN(0, "could not open %s for writing", dev); 107 return; 108 } 109 error = ioctl(devfd, CPUCTL_CPUID, &idargs); 110 if (error < 0) { 111 WARN(0, "ioctl(%s)", dev); 112 goto fail; 113 } 114 signature = idargs.data[0]; 115 error = ioctl(devfd, CPUCTL_RDMSR, &msrargs); 116 if (error < 0) { 117 WARN(0, "ioctl(%s)", dev); 118 goto fail; 119 } 120 121 /* 122 * MSR_IA32_PLATFORM_ID contains flag in BCD in bits 52-50. 123 */ 124 flags = 1 << ((msrargs.data >> 50) & 7); 125 msrargs.msr = MSR_BIOS_SIGN; 126 error = ioctl(devfd, CPUCTL_RDMSR, &msrargs); 127 if (error < 0) { 128 WARN(0, "ioctl(%s)", dev); 129 goto fail; 130 } 131 revision = msrargs.data >> 32; /* Revision in the high dword. */ 132 WARNX(2, "found cpu type %#x family %#x model %#x stepping %#x.", 133 (signature >> 12) & 0x03, (signature >> 8) & 0x0f, 134 (signature >> 4) & 0x0f, (signature >> 0) & 0x0f); 135 /* 136 * Open firmware image. 137 */ 138 fd = open(path, O_RDONLY, 0); 139 if (fd < 0) { 140 WARN(0, "open(%s)", path); 141 return; 142 } 143 error = fstat(fd, &st); 144 if (error != 0) { 145 WARN(0, "fstat(%s)", path); 146 goto fail; 147 } 148 if (st.st_size < 0 || (unsigned)st.st_size < sizeof(*fw_header)) { 149 WARNX(2, "file too short: %s", path); 150 goto fail; 151 } 152 153 /* 154 * mmap the whole image. 155 */ 156 fw_image = (uint32_t *)mmap(NULL, st.st_size, PROT_READ, 157 MAP_PRIVATE, fd, 0); 158 if (fw_image == MAP_FAILED) { 159 WARN(0, "mmap(%s)", path); 160 goto fail; 161 } 162 fw_header = (via_fw_header_t *)fw_image; 163 if (fw_header->signature != VIA_HEADER_SIGNATURE || 164 fw_header->loader_revision != VIA_LOADER_REVISION) { 165 WARNX(2, "%s is not a valid via firmware: version mismatch", 166 path); 167 goto fail; 168 } 169 data_size = fw_header->data_size; 170 total_size = fw_header->total_size; 171 if (total_size > (unsigned)st.st_size || st.st_size < 0) { 172 WARNX(2, "file too short: %s", path); 173 goto fail; 174 } 175 payload_size = data_size + sizeof(*fw_header); 176 177 /* 178 * Check the primary checksum. 179 */ 180 sum = 0; 181 for (i = 0; i < (payload_size / sizeof(uint32_t)); i++) 182 sum += *((uint32_t *)fw_image + i); 183 if (sum != 0) { 184 WARNX(2, "%s: update data checksum invalid", path); 185 goto fail; 186 } 187 188 fw_data = fw_header + 1; /* Pointer to the update data. */ 189 190 /* 191 * Check if the given image is ok for this cpu. 192 */ 193 if (signature != fw_header->cpu_signature) 194 goto fail; 195 196 if (fw_header->revision != 0 && revision >= fw_header->revision) { 197 WARNX(1, "skipping %s of rev %#x: up to date", 198 path, fw_header->revision); 199 goto fail; 200 } 201 fprintf(stderr, "%s: updating cpu %s from rev %#x to rev %#x... ", 202 path, dev, revision, fw_header->revision); 203 args.data = fw_data; 204 args.size = data_size; 205 error = ioctl(devfd, CPUCTL_UPDATE, &args); 206 if (error < 0) { 207 fprintf(stderr, "failed.\n"); 208 WARN(0, "ioctl()"); 209 goto fail; 210 } 211 fprintf(stderr, "done.\n"); 212 213 fail: 214 if (fw_image != MAP_FAILED) 215 if (munmap(fw_image, st.st_size) != 0) 216 warn("munmap(%s)", path); 217 if (devfd >= 0) 218 close(devfd); 219 if (fd >= 0) 220 close(fd); 221 return; 222 } 223