1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 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 __FBSDID("$FreeBSD$"); 31 32 #include <sys/param.h> 33 #include <sys/systm.h> 34 #include <sys/kernel.h> 35 #include <sys/malloc.h> 36 37 #include <sys/module.h> 38 #include <sys/bus.h> 39 #include <sys/conf.h> 40 41 #include <machine/bus.h> 42 #include <machine/resource.h> 43 #include <sys/rman.h> 44 45 /* And all this for BIOS_PADDRTOVADDR() */ 46 #include <vm/vm.h> 47 #include <vm/vm_param.h> 48 #include <vm/pmap.h> 49 #include <machine/md_var.h> 50 #include <machine/pc/bios.h> 51 52 #include <machine/smapi.h> 53 54 #define SMAPI_START 0xf0000 55 #define SMAPI_STEP 0x10 56 #define SMAPI_OFF 0 57 #define SMAPI_LEN 4 58 #define SMAPI_SIG "$SMB" 59 60 #define RES2HDR(res) ((struct smapi_bios_header *)rman_get_virtual(res)) 61 #define ADDR2HDR(addr) ((struct smapi_bios_header *)BIOS_PADDRTOVADDR(addr)) 62 63 struct smapi_softc { 64 struct cdev * cdev; 65 device_t dev; 66 struct resource * res; 67 int rid; 68 69 u_int32_t smapi32_entry; 70 71 struct smapi_bios_header *header; 72 }; 73 74 extern u_long smapi32_offset; 75 extern u_short smapi32_segment; 76 77 static d_ioctl_t smapi_ioctl; 78 79 static struct cdevsw smapi_cdevsw = { 80 .d_version = D_VERSION, 81 .d_ioctl = smapi_ioctl, 82 .d_name = "smapi", 83 .d_flags = D_NEEDGIANT, 84 }; 85 86 static void smapi_identify(driver_t *, device_t); 87 static int smapi_probe(device_t); 88 static int smapi_attach(device_t); 89 static int smapi_detach(device_t); 90 static int smapi_modevent(module_t, int, void *); 91 92 static int smapi_header_cksum(struct smapi_bios_header *); 93 94 extern int smapi32(struct smapi_bios_parameter *, 95 struct smapi_bios_parameter *); 96 extern int smapi32_new(u_long, u_short, 97 struct smapi_bios_parameter *, 98 struct smapi_bios_parameter *); 99 100 static int 101 smapi_ioctl (struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td) 102 { 103 struct smapi_softc *sc = dev->si_drv1; 104 int error; 105 106 switch (cmd) { 107 case SMAPIOGHEADER: 108 bcopy((caddr_t)sc->header, data, 109 sizeof(struct smapi_bios_header)); 110 error = 0; 111 break; 112 case SMAPIOCGFUNCTION: 113 smapi32_offset = sc->smapi32_entry; 114 error = smapi32((struct smapi_bios_parameter *)data, 115 (struct smapi_bios_parameter *)data); 116 break; 117 default: 118 error = ENOTTY; 119 } 120 121 return (error); 122 } 123 124 static int 125 smapi_header_cksum (struct smapi_bios_header *header) 126 { 127 u_int8_t *ptr; 128 u_int8_t cksum; 129 int i; 130 131 ptr = (u_int8_t *)header; 132 cksum = 0; 133 for (i = 0; i < header->length; i++) { 134 cksum += ptr[i]; 135 } 136 137 return (cksum); 138 } 139 140 static void 141 smapi_identify (driver_t *driver, device_t parent) 142 { 143 device_t child; 144 u_int32_t addr; 145 int length; 146 int rid; 147 148 if (!device_is_alive(parent)) 149 return; 150 151 addr = bios_sigsearch(SMAPI_START, SMAPI_SIG, SMAPI_LEN, 152 SMAPI_STEP, SMAPI_OFF); 153 if (addr != 0) { 154 rid = 0; 155 length = ADDR2HDR(addr)->length; 156 157 child = BUS_ADD_CHILD(parent, 5, "smapi", -1); 158 device_set_driver(child, driver); 159 bus_set_resource(child, SYS_RES_MEMORY, rid, addr, length); 160 device_set_desc(child, "SMAPI BIOS"); 161 } 162 163 return; 164 } 165 166 static int 167 smapi_probe (device_t dev) 168 { 169 struct resource *res; 170 int rid; 171 int error; 172 173 error = 0; 174 rid = 0; 175 res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE); 176 if (res == NULL) { 177 device_printf(dev, "Unable to allocate memory resource.\n"); 178 error = ENOMEM; 179 goto bad; 180 } 181 182 if (smapi_header_cksum(RES2HDR(res))) { 183 device_printf(dev, "SMAPI header checksum failed.\n"); 184 error = ENXIO; 185 goto bad; 186 } 187 188 bad: 189 if (res) 190 bus_release_resource(dev, SYS_RES_MEMORY, rid, res); 191 return (error); 192 } 193 194 static int 195 smapi_attach (device_t dev) 196 { 197 struct make_dev_args args; 198 struct smapi_softc *sc; 199 int error; 200 201 sc = device_get_softc(dev); 202 error = 0; 203 204 sc->dev = dev; 205 sc->rid = 0; 206 sc->res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->rid, 207 RF_ACTIVE); 208 if (sc->res == NULL) { 209 device_printf(dev, "Unable to allocate memory resource.\n"); 210 error = ENOMEM; 211 goto bad; 212 } 213 sc->header = (struct smapi_bios_header *)rman_get_virtual(sc->res); 214 sc->smapi32_entry = (u_int32_t)BIOS_PADDRTOVADDR( 215 sc->header->prot32_segment + 216 sc->header->prot32_offset); 217 218 make_dev_args_init(&args); 219 args.mda_devsw = &smapi_cdevsw; 220 args.mda_uid = UID_ROOT; 221 args.mda_gid = GID_WHEEL; 222 args.mda_mode = 0600; 223 args.mda_si_drv1 = sc; 224 error = make_dev_s(&args, &sc->cdev, "%s%d", 225 smapi_cdevsw.d_name, 226 device_get_unit(sc->dev)); 227 if (error != 0) 228 goto bad; 229 230 device_printf(dev, "Version: %d.%02d, Length: %d, Checksum: 0x%02x\n", 231 bcd2bin(sc->header->version_major), 232 bcd2bin(sc->header->version_minor), 233 sc->header->length, 234 sc->header->checksum); 235 device_printf(dev, "Information=0x%b\n", 236 sc->header->information, 237 "\020" 238 "\001REAL_VM86" 239 "\002PROTECTED_16" 240 "\003PROTECTED_32"); 241 242 if (bootverbose) { 243 if (sc->header->information & SMAPI_REAL_VM86) 244 device_printf(dev, "Real/VM86 mode: Segment 0x%04x, Offset 0x%04x\n", 245 sc->header->real16_segment, 246 sc->header->real16_offset); 247 if (sc->header->information & SMAPI_PROT_16BIT) 248 device_printf(dev, "16-bit Protected mode: Segment 0x%08x, Offset 0x%04x\n", 249 sc->header->prot16_segment, 250 sc->header->prot16_offset); 251 if (sc->header->information & SMAPI_PROT_32BIT) 252 device_printf(dev, "32-bit Protected mode: Segment 0x%08x, Offset 0x%08x\n", 253 sc->header->prot32_segment, 254 sc->header->prot32_offset); 255 } 256 257 return (0); 258 bad: 259 if (sc->res) 260 bus_release_resource(dev, SYS_RES_MEMORY, sc->rid, sc->res); 261 return (error); 262 } 263 264 static int 265 smapi_detach (device_t dev) 266 { 267 struct smapi_softc *sc; 268 269 sc = device_get_softc(dev); 270 271 destroy_dev(sc->cdev); 272 273 if (sc->res) 274 bus_release_resource(dev, SYS_RES_MEMORY, sc->rid, sc->res); 275 276 return (0); 277 } 278 279 static device_method_t smapi_methods[] = { 280 /* Device interface */ 281 DEVMETHOD(device_identify, smapi_identify), 282 DEVMETHOD(device_probe, smapi_probe), 283 DEVMETHOD(device_attach, smapi_attach), 284 DEVMETHOD(device_detach, smapi_detach), 285 { 0, 0 } 286 }; 287 288 static driver_t smapi_driver = { 289 "smapi", 290 smapi_methods, 291 sizeof(struct smapi_softc), 292 }; 293 294 static int 295 smapi_modevent (module_t mod, int what, void *arg) 296 { 297 device_t * devs; 298 int count; 299 int i; 300 301 switch (what) { 302 case MOD_LOAD: 303 break; 304 case MOD_UNLOAD: 305 devclass_get_devices(devclass_find(smapi_driver.name), &devs, 306 &count); 307 for (i = 0; i < count; i++) { 308 device_delete_child(device_get_parent(devs[i]), devs[i]); 309 } 310 free(devs, M_TEMP); 311 break; 312 default: 313 break; 314 } 315 316 return (0); 317 } 318 319 DRIVER_MODULE(smapi, nexus, smapi_driver, smapi_modevent, NULL); 320 MODULE_VERSION(smapi, 1); 321