1228436Sfabient/*- 2228436Sfabient * Copyright (c) 2011 Fabien Thomas <fabient@FreeBSD.org>. 3228436Sfabient * All rights reserved. 4228436Sfabient * 5228436Sfabient * Redistribution and use in source and binary forms, with or without 6228436Sfabient * modification, are permitted provided that the following conditions 7228436Sfabient * are met: 8228436Sfabient * 1. Redistributions of source code must retain the above copyright 9228436Sfabient * notice, this list of conditions and the following disclaimer. 10228436Sfabient * 2. Redistributions in binary form must reproduce the above copyright 11228436Sfabient * notice, this list of conditions and the following disclaimer in the 12228436Sfabient * documentation and/or other materials provided with the distribution. 13228436Sfabient * 14228436Sfabient * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 15228436Sfabient * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16228436Sfabient * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17228436Sfabient * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 18228436Sfabient * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 19228436Sfabient * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 20228436Sfabient * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 21228436Sfabient * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22228436Sfabient * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23228436Sfabient * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24228436Sfabient */ 25228436Sfabient 26228436Sfabient#include <sys/cdefs.h> 27228436Sfabient__FBSDID("$FreeBSD$"); 28228436Sfabient 29228436Sfabient#include <assert.h> 30228436Sfabient#include <stdio.h> 31228436Sfabient#include <stdlib.h> 32228436Sfabient#include <string.h> 33228436Sfabient#include <unistd.h> 34228436Sfabient#include <fcntl.h> 35228436Sfabient#include <err.h> 36245491Seadler#include <errno.h> 37228436Sfabient 38228436Sfabient#include <sys/types.h> 39228436Sfabient#include <sys/stat.h> 40228436Sfabient#include <sys/mman.h> 41228436Sfabient#include <sys/ioctl.h> 42228436Sfabient#include <sys/ioccom.h> 43228436Sfabient#include <sys/cpuctl.h> 44228436Sfabient 45228436Sfabient#include <machine/cpufunc.h> 46228436Sfabient#include <machine/specialreg.h> 47228436Sfabient 48228436Sfabient#include "cpucontrol.h" 49228436Sfabient#include "via.h" 50228436Sfabient 51228436Sfabientint 52228436Sfabientvia_probe(int fd) 53228436Sfabient{ 54228436Sfabient char vendor[13]; 55228436Sfabient int error; 56228436Sfabient cpuctl_cpuid_args_t idargs = { 57228436Sfabient .level = 0, 58228436Sfabient }; 59228436Sfabient 60228436Sfabient error = ioctl(fd, CPUCTL_CPUID, &idargs); 61228436Sfabient if (error < 0) { 62228436Sfabient WARN(0, "ioctl()"); 63228436Sfabient return (1); 64228436Sfabient } 65228436Sfabient ((uint32_t *)vendor)[0] = idargs.data[1]; 66228436Sfabient ((uint32_t *)vendor)[1] = idargs.data[3]; 67228436Sfabient ((uint32_t *)vendor)[2] = idargs.data[2]; 68228436Sfabient vendor[12] = '\0'; 69228436Sfabient if (strncmp(vendor, CENTAUR_VENDOR_ID, sizeof(CENTAUR_VENDOR_ID)) != 0) 70228436Sfabient return (1); 71228436Sfabient 72228436Sfabient /* TODO: detect Nano CPU. */ 73228436Sfabient return (0); 74228436Sfabient} 75228436Sfabient 76228436Sfabientvoid 77228436Sfabientvia_update(const char *dev, const char *path) 78228436Sfabient{ 79228436Sfabient int fd, devfd; 80228436Sfabient struct stat st; 81228436Sfabient uint32_t *fw_image; 82228436Sfabient uint32_t sum; 83228436Sfabient unsigned int i; 84228436Sfabient size_t payload_size; 85228436Sfabient via_fw_header_t *fw_header; 86230360Seadler uint32_t signature; 87228436Sfabient int32_t revision; 88228436Sfabient void *fw_data; 89228436Sfabient size_t data_size, total_size; 90228436Sfabient cpuctl_msr_args_t msrargs = { 91228436Sfabient .msr = MSR_IA32_PLATFORM_ID, 92228436Sfabient }; 93228436Sfabient cpuctl_cpuid_args_t idargs = { 94228436Sfabient .level = 1, /* Signature. */ 95228436Sfabient }; 96228436Sfabient cpuctl_update_args_t args; 97228436Sfabient int error; 98228436Sfabient 99228436Sfabient assert(path); 100228436Sfabient assert(dev); 101228436Sfabient 102228436Sfabient fd = -1; 103228436Sfabient devfd = -1; 104228436Sfabient fw_image = MAP_FAILED; 105228436Sfabient devfd = open(dev, O_RDWR); 106228436Sfabient if (devfd < 0) { 107228436Sfabient WARN(0, "could not open %s for writing", dev); 108228436Sfabient return; 109228436Sfabient } 110228436Sfabient error = ioctl(devfd, CPUCTL_CPUID, &idargs); 111228436Sfabient if (error < 0) { 112228436Sfabient WARN(0, "ioctl(%s)", dev); 113228436Sfabient goto fail; 114228436Sfabient } 115228436Sfabient signature = idargs.data[0]; 116228436Sfabient error = ioctl(devfd, CPUCTL_RDMSR, &msrargs); 117228436Sfabient if (error < 0) { 118228436Sfabient WARN(0, "ioctl(%s)", dev); 119228436Sfabient goto fail; 120228436Sfabient } 121228436Sfabient 122228436Sfabient /* 123228436Sfabient * MSR_IA32_PLATFORM_ID contains flag in BCD in bits 52-50. 124228436Sfabient */ 125228436Sfabient msrargs.msr = MSR_BIOS_SIGN; 126228436Sfabient error = ioctl(devfd, CPUCTL_RDMSR, &msrargs); 127228436Sfabient if (error < 0) { 128228436Sfabient WARN(0, "ioctl(%s)", dev); 129228436Sfabient goto fail; 130228436Sfabient } 131228436Sfabient revision = msrargs.data >> 32; /* Revision in the high dword. */ 132228436Sfabient WARNX(2, "found cpu type %#x family %#x model %#x stepping %#x.", 133228436Sfabient (signature >> 12) & 0x03, (signature >> 8) & 0x0f, 134228436Sfabient (signature >> 4) & 0x0f, (signature >> 0) & 0x0f); 135228436Sfabient /* 136228436Sfabient * Open firmware image. 137228436Sfabient */ 138228436Sfabient fd = open(path, O_RDONLY, 0); 139228436Sfabient if (fd < 0) { 140228436Sfabient WARN(0, "open(%s)", path); 141228436Sfabient return; 142228436Sfabient } 143228436Sfabient error = fstat(fd, &st); 144228436Sfabient if (error != 0) { 145228436Sfabient WARN(0, "fstat(%s)", path); 146228436Sfabient goto fail; 147228436Sfabient } 148228436Sfabient if (st.st_size < 0 || (unsigned)st.st_size < sizeof(*fw_header)) { 149228436Sfabient WARNX(2, "file too short: %s", path); 150228436Sfabient goto fail; 151228436Sfabient } 152228436Sfabient 153228436Sfabient /* 154228436Sfabient * mmap the whole image. 155228436Sfabient */ 156228436Sfabient fw_image = (uint32_t *)mmap(NULL, st.st_size, PROT_READ, 157228436Sfabient MAP_PRIVATE, fd, 0); 158228436Sfabient if (fw_image == MAP_FAILED) { 159228436Sfabient WARN(0, "mmap(%s)", path); 160228436Sfabient goto fail; 161228436Sfabient } 162228436Sfabient fw_header = (via_fw_header_t *)fw_image; 163228436Sfabient if (fw_header->signature != VIA_HEADER_SIGNATURE || 164228436Sfabient fw_header->loader_revision != VIA_LOADER_REVISION) { 165228436Sfabient WARNX(2, "%s is not a valid via firmware: version mismatch", 166228436Sfabient path); 167228436Sfabient goto fail; 168228436Sfabient } 169228436Sfabient data_size = fw_header->data_size; 170228436Sfabient total_size = fw_header->total_size; 171228436Sfabient if (total_size > (unsigned)st.st_size || st.st_size < 0) { 172228436Sfabient WARNX(2, "file too short: %s", path); 173228436Sfabient goto fail; 174228436Sfabient } 175228436Sfabient payload_size = data_size + sizeof(*fw_header); 176228436Sfabient 177228436Sfabient /* 178228436Sfabient * Check the primary checksum. 179228436Sfabient */ 180228436Sfabient sum = 0; 181228436Sfabient for (i = 0; i < (payload_size / sizeof(uint32_t)); i++) 182228436Sfabient sum += *((uint32_t *)fw_image + i); 183228436Sfabient if (sum != 0) { 184228436Sfabient WARNX(2, "%s: update data checksum invalid", path); 185228436Sfabient goto fail; 186228436Sfabient } 187228436Sfabient 188228436Sfabient fw_data = fw_header + 1; /* Pointer to the update data. */ 189228436Sfabient 190228436Sfabient /* 191228436Sfabient * Check if the given image is ok for this cpu. 192228436Sfabient */ 193228436Sfabient if (signature != fw_header->cpu_signature) 194228436Sfabient goto fail; 195228436Sfabient 196228436Sfabient if (fw_header->revision != 0 && revision >= fw_header->revision) { 197228436Sfabient WARNX(1, "skipping %s of rev %#x: up to date", 198228436Sfabient path, fw_header->revision); 199228436Sfabient goto fail; 200228436Sfabient } 201228436Sfabient fprintf(stderr, "%s: updating cpu %s from rev %#x to rev %#x... ", 202228436Sfabient path, dev, revision, fw_header->revision); 203228436Sfabient args.data = fw_data; 204228436Sfabient args.size = data_size; 205228436Sfabient error = ioctl(devfd, CPUCTL_UPDATE, &args); 206228436Sfabient if (error < 0) { 207245491Seadler error = errno; 208228436Sfabient fprintf(stderr, "failed.\n"); 209245491Seadler errno = error; 210228436Sfabient WARN(0, "ioctl()"); 211228436Sfabient goto fail; 212228436Sfabient } 213228436Sfabient fprintf(stderr, "done.\n"); 214228436Sfabient 215228436Sfabientfail: 216228436Sfabient if (fw_image != MAP_FAILED) 217228436Sfabient if (munmap(fw_image, st.st_size) != 0) 218228436Sfabient warn("munmap(%s)", path); 219228436Sfabient if (devfd >= 0) 220228436Sfabient close(devfd); 221228436Sfabient if (fd >= 0) 222228436Sfabient close(fd); 223228436Sfabient return; 224228436Sfabient} 225