bios.c revision 50313
1/*- 2 * Copyright (c) 1997 Michael Smith 3 * Copyright (c) 1998 Jonathan Lemon 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 * 27 * $Id: bios.c,v 1.18 1999/08/20 21:08:41 msmith Exp $ 28 */ 29 30/* 31 * Code for dealing with the BIOS in x86 PC systems. 32 */ 33 34#include <sys/param.h> 35#include <sys/proc.h> 36#include <sys/systm.h> 37#include <sys/kernel.h> 38#include <sys/malloc.h> 39#include <vm/vm.h> 40#include <vm/pmap.h> 41#include <machine/md_var.h> 42#include <machine/segments.h> 43#include <machine/stdarg.h> 44#include <machine/tss.h> 45#include <machine/vmparam.h> 46#include <machine/pc/bios.h> 47#include <i386/isa/isa_device.h> 48#include <i386/isa/pnp.h> 49 50#define BIOS_START 0xe0000 51#define BIOS_SIZE 0x20000 52 53/* exported lookup results */ 54struct bios32_SDentry PCIbios = {entry : 0}; 55struct PnPBIOS_table *PnPBIOStable = 0; 56 57static u_int bios32_SDCI = 0; 58 59/* start fairly early */ 60static void bios32_init(void *junk); 61SYSINIT(bios32, SI_SUB_CPU, SI_ORDER_ANY, bios32_init, NULL); 62 63static void pnpbios_scan(void); 64static char *pnp_eisaformat(u_int8_t *data); 65 66 67/* 68 * bios32_init 69 * 70 * Locate various bios32 entities. 71 */ 72static void 73bios32_init(void *junk) 74{ 75 u_long sigaddr; 76 struct bios32_SDheader *sdh; 77 struct PnPBIOS_table *pt; 78 u_int8_t ck, *cv; 79 int i; 80 81 82 /* 83 * BIOS32 Service Directory 84 */ 85 86 /* look for the signature */ 87 if ((sigaddr = bios_sigsearch(0, "_32_", 4, 16, 0)) != 0) { 88 89 /* get a virtual pointer to the structure */ 90 sdh = (struct bios32_SDheader *)(uintptr_t)BIOS_PADDRTOVADDR(sigaddr); 91 for (cv = (u_int8_t *)sdh, ck = 0, i = 0; i < (sdh->len * 16); i++) { 92 ck += cv[i]; 93 } 94 /* If checksum is OK, enable use of the entrypoint */ 95 if ((ck == 0) && (sdh->entry < (BIOS_START + BIOS_SIZE))) { 96 bios32_SDCI = BIOS_PADDRTOVADDR(sdh->entry); 97 if (bootverbose) { 98 printf("bios32: Found BIOS32 Service Directory header at %p\n", sdh); 99 printf("bios32: Entry = 0x%x (%x) Rev = %d Len = %d\n", 100 sdh->entry, bios32_SDCI, sdh->revision, sdh->len); 101 } 102 /* See if there's a PCI BIOS entrypoint here */ 103 PCIbios.ident.id = 0x49435024; /* PCI systems should have this */ 104 if (!bios32_SDlookup(&PCIbios) && bootverbose) 105 printf("pcibios: PCI BIOS entry at 0x%x\n", PCIbios.entry); 106 } else { 107 printf("bios32: Bad BIOS32 Service Directory\n"); 108 } 109 } 110 111 /* 112 * PnP BIOS 113 */ 114 if ((sigaddr = bios_sigsearch(0, "$PnP", 4, 16, 0)) != 0) { 115 116 /* get a virtual pointer to the structure */ 117 pt = (struct PnPBIOS_table *)(uintptr_t)BIOS_PADDRTOVADDR(sigaddr); 118 for (cv = (u_int8_t *)pt, ck = 0, i = 0; i < pt->len; i++) { 119 ck += cv[i]; 120 } 121 /* If checksum is OK, enable use of the entrypoint */ 122 if (ck == 0) { 123 PnPBIOStable = pt; 124 if (bootverbose) { 125 printf("pnpbios: Found PnP BIOS data at %p\n", pt); 126 printf("pnpbios: Entry = %x:%x Rev = %d.%d\n", 127 pt->pmentrybase, pt->pmentryoffset, pt->version >> 4, pt->version & 0xf); 128 if ((pt->control & 0x3) == 0x01) 129 printf("pnpbios: Event flag at %x\n", pt->evflagaddr); 130 if (pt->oemdevid != 0) 131 printf("pnpbios: OEM ID %x\n", pt->oemdevid); 132 133 } 134 pnpbios_scan(); 135 } else { 136 printf("pnpbios: Bad PnP BIOS data checksum\n"); 137 } 138 } 139 140 if (bootverbose) { 141 /* look for other know signatures */ 142 printf("Other BIOS signatures found:\n"); 143 printf("ACPI: %08x\n", bios_sigsearch(0, "RST PTR", 8, 16, 0)); 144 } 145} 146 147/* 148 * bios32_SDlookup 149 * 150 * Query the BIOS32 Service Directory for the service named in (ent), 151 * returns nonzero if the lookup fails. The caller must fill in 152 * (ent->ident), the remainder are populated on a successful lookup. 153 */ 154int 155bios32_SDlookup(struct bios32_SDentry *ent) 156{ 157 struct bios_regs args; 158 159 if (bios32_SDCI == 0) 160 return (1); 161 162 args.eax = ent->ident.id; /* set up arguments */ 163 args.ebx = args.ecx = args.edx = 0; 164 bios32(&args, bios32_SDCI, GSEL(GCODE_SEL, SEL_KPL)); 165 if ((args.eax & 0xff) == 0) { /* success? */ 166 ent->base = args.ebx; 167 ent->len = args.ecx; 168 ent->entry = args.edx; 169 return (0); /* all OK */ 170 } 171 return (1); /* failed */ 172} 173 174 175/* 176 * bios_sigsearch 177 * 178 * Search some or all of the BIOS region for a signature string. 179 * 180 * (start) Optional offset returned from this function 181 * (for searching for multiple matches), or NULL 182 * to start the search from the base of the BIOS. 183 * Note that this will be a _physical_ address in 184 * the range 0xe0000 - 0xfffff. 185 * (sig) is a pointer to the byte(s) of the signature. 186 * (siglen) number of bytes in the signature. 187 * (paralen) signature paragraph (alignment) size. 188 * (sigofs) offset of the signature within the paragraph. 189 * 190 * Returns the _physical_ address of the found signature, 0 if the 191 * signature was not found. 192 */ 193 194u_int32_t 195bios_sigsearch(u_int32_t start, u_char *sig, int siglen, int paralen, int sigofs) 196{ 197 u_char *sp, *end; 198 199 /* compute the starting address */ 200 if ((start >= BIOS_START) && (start <= (BIOS_START + BIOS_SIZE))) { 201 sp = (char *)BIOS_PADDRTOVADDR(start); 202 } else if (start == 0) { 203 sp = (char *)BIOS_PADDRTOVADDR(BIOS_START); 204 } else { 205 return 0; /* bogus start address */ 206 } 207 208 /* compute the end address */ 209 end = (u_char *)BIOS_PADDRTOVADDR(BIOS_START + BIOS_SIZE); 210 211 /* loop searching */ 212 while ((sp + sigofs + siglen) < end) { 213 214 /* compare here */ 215 if (!bcmp(sp + sigofs, sig, siglen)) { 216 /* convert back to physical address */ 217 return((u_int32_t)BIOS_VADDRTOPADDR(sp)); 218 } 219 sp += paralen; 220 } 221 return(0); 222} 223 224/* 225 * do not staticize, used by bioscall.s 226 */ 227union { 228 struct { 229 u_short offset; 230 u_short segment; 231 } vec16; 232 struct { 233 u_int offset; 234 u_short segment; 235 } vec32; 236} bioscall_vector; /* bios jump vector */ 237 238void 239set_bios_selectors(struct bios_segments *seg, int flags) 240{ 241 static u_int curgen = 1; 242 struct soft_segment_descriptor ssd = { 243 0, /* segment base address (overwritten) */ 244 0, /* length (overwritten) */ 245 SDT_MEMERA, /* segment type (overwritten) */ 246 0, /* priority level */ 247 1, /* descriptor present */ 248 0, 0, 249 1, /* descriptor size (overwritten) */ 250 0 /* granularity == byte units */ 251 }; 252 253 if (seg->generation == curgen) 254 return; 255 if (++curgen == 0) 256 curgen = 1; 257 seg->generation = curgen; 258 259 ssd.ssd_base = seg->code32.base; 260 ssd.ssd_limit = seg->code32.limit; 261 ssdtosd(&ssd, &gdt[GBIOSCODE32_SEL].sd); 262 263 ssd.ssd_def32 = 0; 264 if (flags & BIOSCODE_FLAG) { 265 ssd.ssd_base = seg->code16.base; 266 ssd.ssd_limit = seg->code16.limit; 267 ssdtosd(&ssd, &gdt[GBIOSCODE16_SEL].sd); 268 } 269 270 ssd.ssd_type = SDT_MEMRWA; 271 if (flags & BIOSDATA_FLAG) { 272 ssd.ssd_base = seg->data.base; 273 ssd.ssd_limit = seg->data.limit; 274 ssdtosd(&ssd, &gdt[GBIOSDATA_SEL].sd); 275 } 276 277 if (flags & BIOSUTIL_FLAG) { 278 ssd.ssd_base = seg->util.base; 279 ssd.ssd_limit = seg->util.limit; 280 ssdtosd(&ssd, &gdt[GBIOSUTIL_SEL].sd); 281 } 282 283 if (flags & BIOSARGS_FLAG) { 284 ssd.ssd_base = seg->args.base; 285 ssd.ssd_limit = seg->args.limit; 286 ssdtosd(&ssd, &gdt[GBIOSARGS_SEL].sd); 287 } 288} 289 290extern int vm86pa; 291 292/* 293 * this routine is really greedy with selectors, and uses 5: 294 * 295 * 32-bit code selector: to return to kernel 296 * 16-bit code selector: for running code 297 * data selector: for 16-bit data 298 * util selector: extra utility selector 299 * args selector: to handle pointers 300 * 301 * the util selector is set from the util16 entry in bios16_args, if a 302 * "U" specifier is seen. 303 * 304 * See <machine/pc/bios.h> for description of format specifiers 305 */ 306int 307bios16(struct bios_args *args, char *fmt, ...) 308{ 309 char *p, *stack, *stack_top; 310 va_list ap; 311 int flags = BIOSCODE_FLAG | BIOSDATA_FLAG; 312 u_int i, arg_start, arg_end; 313 u_int *pte, *ptd; 314 315 arg_start = 0xffffffff; 316 arg_end = 0; 317 318 /* 319 * Some BIOS entrypoints attempt to copy the largest-case 320 * argument frame (in order to generalise handling for 321 * different entry types). If our argument frame is 322 * smaller than this, the BIOS will reach off the top of 323 * our constructed stack segment. Pad the top of the stack 324 * with some garbage to avoid this. 325 */ 326 stack = (caddr_t)PAGE_SIZE - 32; 327 328 va_start(ap, fmt); 329 for (p = fmt; p && *p; p++) { 330 switch (*p) { 331 case 'p': /* 32-bit pointer */ 332 i = va_arg(ap, u_int); 333 arg_start = min(arg_start, i); 334 arg_end = max(arg_end, i); 335 flags |= BIOSARGS_FLAG; 336 stack -= 4; 337 break; 338 339 case 'i': /* 32-bit integer */ 340 i = va_arg(ap, u_int); 341 stack -= 4; 342 break; 343 344 case 'U': /* 16-bit selector */ 345 flags |= BIOSUTIL_FLAG; 346 /* FALLTHROUGH */ 347 case 'D': /* 16-bit selector */ 348 case 'C': /* 16-bit selector */ 349 stack -= 2; 350 break; 351 352 case 's': /* 16-bit integer */ 353 i = va_arg(ap, u_short); 354 stack -= 2; 355 break; 356 357 default: 358 return (EINVAL); 359 } 360 } 361 362 if (flags & BIOSARGS_FLAG) { 363 if (arg_end - arg_start > ctob(16)) 364 return (EACCES); 365 args->seg.args.base = arg_start; 366 args->seg.args.limit = 0xffff; 367 } 368 369 args->seg.code32.base = (u_int)&bios16_call & PG_FRAME; 370 args->seg.code32.limit = 0xffff; 371 372 ptd = (u_int *)rcr3(); 373 if (ptd == IdlePTD) { 374 /* 375 * no page table, so create one and install it. 376 */ 377 pte = (u_int *)malloc(PAGE_SIZE, M_TEMP, M_WAITOK); 378 ptd = (u_int *)((u_int)ptd + KERNBASE); 379 *ptd = vtophys(pte) | PG_RW | PG_V; 380 } else { 381 /* 382 * this is a user-level page table 383 */ 384 pte = (u_int *)&PTmap; 385 } 386 /* 387 * install pointer to page 0. we don't need to flush the tlb, 388 * since there should not be a previous mapping for page 0. 389 */ 390 *pte = (vm86pa - PAGE_SIZE) | PG_RW | PG_V; 391 392 stack_top = stack; 393 va_start(ap, fmt); 394 for (p = fmt; p && *p; p++) { 395 switch (*p) { 396 case 'p': /* 32-bit pointer */ 397 i = va_arg(ap, u_int); 398 *(u_int *)stack = (i - arg_start) | 399 (GSEL(GBIOSARGS_SEL, SEL_KPL) << 16); 400 stack += 4; 401 break; 402 403 case 'i': /* 32-bit integer */ 404 i = va_arg(ap, u_int); 405 *(u_int *)stack = i; 406 stack += 4; 407 break; 408 409 case 'U': /* 16-bit selector */ 410 *(u_short *)stack = GSEL(GBIOSUTIL_SEL, SEL_KPL); 411 stack += 2; 412 break; 413 414 case 'D': /* 16-bit selector */ 415 *(u_short *)stack = GSEL(GBIOSDATA_SEL, SEL_KPL); 416 stack += 2; 417 break; 418 419 case 'C': /* 16-bit selector */ 420 *(u_short *)stack = GSEL(GBIOSCODE16_SEL, SEL_KPL); 421 stack += 2; 422 break; 423 424 case 's': /* 16-bit integer */ 425 i = va_arg(ap, u_short); 426 *(u_short *)stack = i; 427 stack += 2; 428 break; 429 430 default: 431 return (EINVAL); 432 } 433 } 434 435 args->seg.generation = 0; /* reload selectors */ 436 set_bios_selectors(&args->seg, flags); 437 bioscall_vector.vec16.offset = (u_short)args->entry; 438 bioscall_vector.vec16.segment = GSEL(GBIOSCODE16_SEL, SEL_KPL); 439 440 i = bios16_call(&args->r, stack_top); 441 442 if (pte == (u_int *)&PTmap) { 443 *pte = 0; /* remove entry */ 444 } else { 445 *ptd = 0; /* remove page table */ 446 free(pte, M_TEMP); /* ... and free it */ 447 } 448 449 /* 450 * XXX only needs to be invlpg(0) but that doesn't work on the 386 451 */ 452 invltlb(); 453 454 return (i); 455} 456 457/* 458 * PnP BIOS interface; enumerate devices only known to the system 459 * BIOS and save information about them for later use. 460 */ 461 462struct pnp_sysdev 463{ 464 u_int16_t size; 465 u_int8_t handle; 466 u_int32_t devid; 467 u_int8_t type[3]; 468 u_int16_t attrib; 469#define PNPATTR_NODISABLE (1<<0) /* can't be disabled */ 470#define PNPATTR_NOCONFIG (1<<1) /* can't be configured */ 471#define PNPATTR_OUTPUT (1<<2) /* can be primary output */ 472#define PNPATTR_INPUT (1<<3) /* can be primary input */ 473#define PNPATTR_BOOTABLE (1<<4) /* can be booted from */ 474#define PNPATTR_DOCK (1<<5) /* is a docking station */ 475#define PNPATTR_REMOVEABLE (1<<6) /* device is removeable */ 476#define PNPATTR_CONFIG_STATIC 0x00 477#define PNPATTR_CONFIG_DYNAMIC 0x07 478#define PNPATTR_CONFIG_DYNONLY 0x17 479 /* device-specific data comes here */ 480 u_int8_t devdata[0]; 481} __attribute__ ((packed)); 482 483/* We have to cluster arguments within a 64k range for the bios16 call */ 484struct pnp_sysdevargs 485{ 486 u_int16_t next; 487 struct pnp_sysdev node; 488}; 489 490/* 491 * Quiz the PnP BIOS, build a list of PNP IDs and resource data. 492 */ 493static void 494pnpbios_scan(void) 495{ 496 struct PnPBIOS_table *pt = PnPBIOStable; 497 struct bios_args args; 498 struct pnp_sysdev *pd; 499 struct pnp_sysdevargs *pda; 500 u_int16_t ndevs, bigdev; 501 int error, currdev; 502 u_int8_t *devnodebuf, tag; 503 u_int32_t *devid, *compid; 504 int idx, left; 505 506 /* no PnP BIOS information */ 507 if (pt == NULL) 508 return; 509 510 bzero(&args, sizeof(args)); 511 args.seg.code16.base = BIOS_PADDRTOVADDR(pt->pmentrybase); 512 args.seg.code16.limit = 0xffff; /* XXX ? */ 513 args.seg.data.base = BIOS_PADDRTOVADDR(pt->pmdataseg); 514 args.seg.data.limit = 0xffff; 515 args.entry = pt->pmentryoffset; 516 517 if ((error = bios16(&args, PNP_COUNT_DEVNODES, &ndevs, &bigdev)) || (args.r.eax & 0xff)) 518 printf("pnpbios: error %d/%x getting device count/size limit\n", error, args.r.eax); 519 if (bootverbose) 520 printf("pnpbios: %d devices, largest %d bytes\n", ndevs, bigdev); 521 522 devnodebuf = malloc(bigdev + (sizeof(struct pnp_sysdevargs) - sizeof(struct pnp_sysdev)), 523 M_DEVBUF, M_NOWAIT); 524 pda = (struct pnp_sysdevargs *)devnodebuf; 525 pd = &pda->node; 526 527 for (currdev = 0, left = ndevs; (currdev != 0xff) && (left > 0); left--) { 528 529 bzero(pd, bigdev); 530 pda->next = currdev; 531 /* get current configuration */ 532 if ((error = bios16(&args, PNP_GET_DEVNODE, &pda->next, &pda->node, (u_int16_t)1))) { 533 printf("pnpbios: error %d making BIOS16 call\n", error); 534 break; 535 } 536 if ((error = (args.r.eax & 0xff))) { 537 if (bootverbose) 538 printf("pnpbios: %s 0x%x fetching node %d\n", error & 0x80 ? "error" : "warning", error, currdev); 539 if (error & 0x80) 540 break; 541 } 542 currdev = pda->next; 543 if (pd->size < sizeof(struct pnp_sysdev)) { 544 printf("pnpbios: bogus system node data, aborting scan\n"); 545 break; 546 } 547 548 /* Find device IDs */ 549 devid = &pd->devid; 550 compid = NULL; 551 552 /* look for a compatible device ID too */ 553 left = pd->size - sizeof(struct pnp_sysdev); 554 idx = 0; 555 while (idx < left) { 556 tag = pd->devdata[idx++]; 557 if (PNP_RES_TYPE(tag) == 0) { 558 /* Small resource */ 559 switch (PNP_SRES_NUM(tag)) { 560 case COMP_DEVICE_ID: 561 compid = (u_int32_t *)(pd->devdata + idx); 562 if (bootverbose) 563 printf("pnpbios: node %d compat ID 0x%08x\n", pd->handle, *compid); 564 /* FALLTHROUGH */ 565 case END_TAG: 566 idx = left; 567 break; 568 default: 569 idx += PNP_SRES_LEN(tag); 570 break; 571 } 572 } else 573 /* Large resource, skip it */ 574 idx += *(u_int16_t *)(pd->devdata + idx) + 2; 575 } 576 if (bootverbose) { 577 printf("pnpbios: handle %d device ID %s (%08x)", 578 pd->handle, pnp_eisaformat((u_int8_t *)devid), *devid); 579 if (compid != NULL) 580 printf(" compat ID %s (%08x)", 581 pnp_eisaformat((u_int8_t *)compid), *compid); 582 printf("\n"); 583 } 584 } 585} 586 587/* XXX should be somewhere else */ 588static char * 589pnp_eisaformat(u_int8_t *data) 590{ 591 static char idbuf[8]; 592 const char hextoascii[] = "0123456789abcdef"; 593 594 idbuf[0] = '@' + ((data[0] & 0x7c) >> 2); 595 idbuf[1] = '@' + (((data[0] & 0x3) << 3) + ((data[1] & 0xe0) >> 5)); 596 idbuf[2] = '@' + (data[1] & 0x1f); 597 idbuf[3] = hextoascii[(data[2] >> 4)]; 598 idbuf[4] = hextoascii[(data[2] & 0xf)]; 599 idbuf[5] = hextoascii[(data[3] >> 4)]; 600 idbuf[6] = hextoascii[(data[3] & 0xf)]; 601 idbuf[7] = 0; 602 return(idbuf); 603} 604