1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2003 Matthew N. Dodd <winter@jurai.net> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 #include <sys/param.h> 31 #include <sys/systm.h> 32 #include <sys/kernel.h> 33 #include <sys/malloc.h> 34 #include <sys/socket.h> 35 #include <sys/efi.h> 36 37 #include <sys/module.h> 38 #include <sys/bus.h> 39 40 #include <machine/bus.h> 41 #include <machine/resource.h> 42 #include <sys/rman.h> 43 44 #include <vm/vm.h> 45 #include <vm/vm_param.h> 46 #include <vm/pmap.h> 47 #include <machine/md_var.h> 48 #if defined(__amd64__) || defined(__i386__) 49 #include <machine/pc/bios.h> 50 #endif 51 #include <dev/smbios/smbios.h> 52 53 /* 54 * System Management BIOS Reference Specification, v2.4 Final 55 * http://www.dmtf.org/standards/published_documents/DSP0134.pdf 56 */ 57 58 struct smbios_softc { 59 device_t dev; 60 struct resource * res; 61 int rid; 62 63 struct smbios_eps * eps; 64 }; 65 66 #define RES2EPS(res) ((struct smbios_eps *)rman_get_virtual(res)) 67 68 static void smbios_identify (driver_t *, device_t); 69 static int smbios_probe (device_t); 70 static int smbios_attach (device_t); 71 static int smbios_detach (device_t); 72 static int smbios_modevent (module_t, int, void *); 73 74 static int smbios_cksum (struct smbios_eps *); 75 76 static void 77 smbios_identify (driver_t *driver, device_t parent) 78 { 79 #ifdef ARCH_MAY_USE_EFI 80 struct uuid efi_smbios = EFI_TABLE_SMBIOS; 81 void *addr_efi; 82 #endif 83 struct smbios_eps *eps; 84 device_t child; 85 vm_paddr_t addr = 0; 86 int length; 87 int rid; 88 89 if (!device_is_alive(parent)) 90 return; 91 92 #ifdef ARCH_MAY_USE_EFI 93 if (!efi_get_table(&efi_smbios, &addr_efi)) 94 addr = (vm_paddr_t)addr_efi; 95 #endif 96 97 #if defined(__amd64__) || defined(__i386__) 98 if (addr == 0) 99 addr = bios_sigsearch(SMBIOS_START, SMBIOS_SIG, SMBIOS_LEN, 100 SMBIOS_STEP, SMBIOS_OFF); 101 #endif 102 103 if (addr != 0) { 104 eps = pmap_mapbios(addr, 0x1f); 105 rid = 0; 106 length = eps->length; 107 108 if (length != 0x1f) { 109 u_int8_t major, minor; 110 111 major = eps->major_version; 112 minor = eps->minor_version; 113 114 /* SMBIOS v2.1 implementation might use 0x1e. */ 115 if (length == 0x1e && major == 2 && minor == 1) 116 length = 0x1f; 117 else 118 return; 119 } 120 121 child = BUS_ADD_CHILD(parent, 5, "smbios", -1); 122 device_set_driver(child, driver); 123 bus_set_resource(child, SYS_RES_MEMORY, rid, addr, length); 124 device_set_desc(child, "System Management BIOS"); 125 pmap_unmapbios(eps, 0x1f); 126 } 127 128 return; 129 } 130 131 static int 132 smbios_probe (device_t dev) 133 { 134 struct resource *res; 135 int rid; 136 int error; 137 138 error = 0; 139 rid = 0; 140 res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE); 141 if (res == NULL) { 142 device_printf(dev, "Unable to allocate memory resource.\n"); 143 error = ENOMEM; 144 goto bad; 145 } 146 147 if (smbios_cksum(RES2EPS(res))) { 148 device_printf(dev, "SMBIOS checksum failed.\n"); 149 error = ENXIO; 150 goto bad; 151 } 152 153 bad: 154 if (res) 155 bus_release_resource(dev, SYS_RES_MEMORY, rid, res); 156 return (error); 157 } 158 159 static int 160 smbios_attach (device_t dev) 161 { 162 struct smbios_softc *sc; 163 int error; 164 165 sc = device_get_softc(dev); 166 error = 0; 167 168 sc->dev = dev; 169 sc->rid = 0; 170 sc->res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->rid, 171 RF_ACTIVE); 172 if (sc->res == NULL) { 173 device_printf(dev, "Unable to allocate memory resource.\n"); 174 error = ENOMEM; 175 goto bad; 176 } 177 sc->eps = RES2EPS(sc->res); 178 179 device_printf(dev, "Version: %u.%u", 180 sc->eps->major_version, sc->eps->minor_version); 181 if (bcd2bin(sc->eps->BCD_revision)) 182 printf(", BCD Revision: %u.%u", 183 bcd2bin(sc->eps->BCD_revision >> 4), 184 bcd2bin(sc->eps->BCD_revision & 0x0f)); 185 printf("\n"); 186 187 return (0); 188 bad: 189 if (sc->res) 190 bus_release_resource(dev, SYS_RES_MEMORY, sc->rid, sc->res); 191 return (error); 192 } 193 194 static int 195 smbios_detach (device_t dev) 196 { 197 struct smbios_softc *sc; 198 199 sc = device_get_softc(dev); 200 201 if (sc->res) 202 bus_release_resource(dev, SYS_RES_MEMORY, sc->rid, sc->res); 203 204 return (0); 205 } 206 207 static int 208 smbios_modevent (module_t mod, int what, void *arg) 209 { 210 device_t * devs; 211 int count; 212 int i; 213 214 switch (what) { 215 case MOD_LOAD: 216 break; 217 case MOD_UNLOAD: 218 devclass_get_devices(devclass_find("smbios"), &devs, &count); 219 for (i = 0; i < count; i++) { 220 device_delete_child(device_get_parent(devs[i]), devs[i]); 221 } 222 free(devs, M_TEMP); 223 break; 224 default: 225 break; 226 } 227 228 return (0); 229 } 230 231 static device_method_t smbios_methods[] = { 232 /* Device interface */ 233 DEVMETHOD(device_identify, smbios_identify), 234 DEVMETHOD(device_probe, smbios_probe), 235 DEVMETHOD(device_attach, smbios_attach), 236 DEVMETHOD(device_detach, smbios_detach), 237 { 0, 0 } 238 }; 239 240 static driver_t smbios_driver = { 241 "smbios", 242 smbios_methods, 243 sizeof(struct smbios_softc), 244 }; 245 246 DRIVER_MODULE(smbios, nexus, smbios_driver, smbios_modevent, NULL); 247 #ifdef ARCH_MAY_USE_EFI 248 MODULE_DEPEND(smbios, efirt, 1, 1, 1); 249 #endif 250 MODULE_VERSION(smbios, 1); 251 252 static int 253 smbios_cksum (struct smbios_eps *e) 254 { 255 u_int8_t *ptr; 256 u_int8_t cksum; 257 int i; 258 259 ptr = (u_int8_t *)e; 260 cksum = 0; 261 for (i = 0; i < e->length; i++) { 262 cksum += ptr[i]; 263 } 264 265 return (cksum); 266 } 267