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; 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 msrargs.msr = MSR_BIOS_SIGN; 125 error = ioctl(devfd, CPUCTL_RDMSR, &msrargs); 126 if (error < 0) { 127 WARN(0, "ioctl(%s)", dev); 128 goto fail; 129 } 130 revision = msrargs.data >> 32; /* Revision in the high dword. */ 131 WARNX(2, "found cpu type %#x family %#x model %#x stepping %#x.", 132 (signature >> 12) & 0x03, (signature >> 8) & 0x0f, 133 (signature >> 4) & 0x0f, (signature >> 0) & 0x0f); 134 /* 135 * Open firmware image. 136 */ 137 fd = open(path, O_RDONLY, 0); 138 if (fd < 0) { 139 WARN(0, "open(%s)", path); 140 return; 141 } 142 error = fstat(fd, &st); 143 if (error != 0) { 144 WARN(0, "fstat(%s)", path); 145 goto fail; 146 } 147 if (st.st_size < 0 || (unsigned)st.st_size < sizeof(*fw_header)) { 148 WARNX(2, "file too short: %s", path); 149 goto fail; 150 } 151 152 /* 153 * mmap the whole image. 154 */ 155 fw_image = (uint32_t *)mmap(NULL, st.st_size, PROT_READ, 156 MAP_PRIVATE, fd, 0); 157 if (fw_image == MAP_FAILED) { 158 WARN(0, "mmap(%s)", path); 159 goto fail; 160 } 161 fw_header = (via_fw_header_t *)fw_image; 162 if (fw_header->signature != VIA_HEADER_SIGNATURE || 163 fw_header->loader_revision != VIA_LOADER_REVISION) { 164 WARNX(2, "%s is not a valid via firmware: version mismatch", 165 path); 166 goto fail; 167 } 168 data_size = fw_header->data_size; 169 total_size = fw_header->total_size; 170 if (total_size > (unsigned)st.st_size || st.st_size < 0) { 171 WARNX(2, "file too short: %s", path); 172 goto fail; 173 } 174 payload_size = data_size + sizeof(*fw_header); 175 176 /* 177 * Check the primary checksum. 178 */ 179 sum = 0; 180 for (i = 0; i < (payload_size / sizeof(uint32_t)); i++) 181 sum += *((uint32_t *)fw_image + i); 182 if (sum != 0) { 183 WARNX(2, "%s: update data checksum invalid", path); 184 goto fail; 185 } 186 187 fw_data = fw_header + 1; /* Pointer to the update data. */ 188 189 /* 190 * Check if the given image is ok for this cpu. 191 */ 192 if (signature != fw_header->cpu_signature) 193 goto fail; 194 195 if (fw_header->revision != 0 && revision >= fw_header->revision) { 196 WARNX(1, "skipping %s of rev %#x: up to date", 197 path, fw_header->revision); 198 goto fail; 199 } 200 fprintf(stderr, "%s: updating cpu %s from rev %#x to rev %#x... ", 201 path, dev, revision, fw_header->revision); 202 args.data = fw_data; 203 args.size = data_size; 204 error = ioctl(devfd, CPUCTL_UPDATE, &args); 205 if (error < 0) { 206 fprintf(stderr, "failed.\n"); 207 WARN(0, "ioctl()"); 208 goto fail; 209 } 210 fprintf(stderr, "done.\n"); 211 212 fail: 213 if (fw_image != MAP_FAILED) 214 if (munmap(fw_image, st.st_size) != 0) 215 warn("munmap(%s)", path); 216 if (devfd >= 0) 217 close(devfd); 218 if (fd >= 0) 219 close(fd); 220 return; 221 } 222