xref: /freebsd/sys/i386/i386/bios.c (revision 39beb93c)
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 
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 /*
32  * Code for dealing with the BIOS in x86 PC systems.
33  */
34 
35 #include "opt_isa.h"
36 
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/malloc.h>
41 #include <sys/module.h>
42 #include <sys/bus.h>
43 #include <sys/pcpu.h>
44 #include <vm/vm.h>
45 #include <vm/pmap.h>
46 #include <machine/md_var.h>
47 #include <machine/segments.h>
48 #include <machine/stdarg.h>
49 #include <machine/vmparam.h>
50 #include <machine/pc/bios.h>
51 #ifdef DEV_ISA
52 #include <isa/isavar.h>
53 #include <isa/pnpreg.h>
54 #include <isa/pnpvar.h>
55 #endif
56 
57 #define BIOS_START	0xe0000
58 #define BIOS_SIZE	0x20000
59 
60 /* exported lookup results */
61 struct bios32_SDentry		PCIbios;
62 
63 static struct PnPBIOS_table	*PnPBIOStable;
64 
65 static u_int			bios32_SDCI;
66 
67 /* start fairly early */
68 static void			bios32_init(void *junk);
69 SYSINIT(bios32, SI_SUB_CPU, SI_ORDER_ANY, bios32_init, NULL);
70 
71 /*
72  * bios32_init
73  *
74  * Locate various bios32 entities.
75  */
76 static void
77 bios32_init(void *junk)
78 {
79     u_long			sigaddr;
80     struct bios32_SDheader	*sdh;
81     struct PnPBIOS_table	*pt;
82     u_int8_t			ck, *cv;
83     int				i;
84     char			*p;
85 
86     /*
87      * BIOS32 Service Directory, PCI BIOS
88      */
89 
90     /* look for the signature */
91     if ((sigaddr = bios_sigsearch(0, "_32_", 4, 16, 0)) != 0) {
92 
93 	/* get a virtual pointer to the structure */
94 	sdh = (struct bios32_SDheader *)(uintptr_t)BIOS_PADDRTOVADDR(sigaddr);
95 	for (cv = (u_int8_t *)sdh, ck = 0, i = 0; i < (sdh->len * 16); i++) {
96 	    ck += cv[i];
97 	}
98 	/* If checksum is OK, enable use of the entrypoint */
99 	if ((ck == 0) && (BIOS_START <= sdh->entry ) &&
100 	    (sdh->entry < (BIOS_START + BIOS_SIZE))) {
101 	    bios32_SDCI = BIOS_PADDRTOVADDR(sdh->entry);
102 	    if (bootverbose) {
103 		printf("bios32: Found BIOS32 Service Directory header at %p\n", sdh);
104 		printf("bios32: Entry = 0x%x (%x)  Rev = %d  Len = %d\n",
105 		       sdh->entry, bios32_SDCI, sdh->revision, sdh->len);
106 	    }
107 
108 	    /* Allow user override of PCI BIOS search */
109 	    if (((p = getenv("machdep.bios.pci")) == NULL) || strcmp(p, "disable")) {
110 
111 		/* See if there's a PCI BIOS entrypoint here */
112 		PCIbios.ident.id = 0x49435024;	/* PCI systems should have this */
113 		if (!bios32_SDlookup(&PCIbios) && bootverbose)
114 		    printf("pcibios: PCI BIOS entry at 0x%x+0x%x\n", PCIbios.base, PCIbios.entry);
115 	    }
116 	    if (p != NULL)
117 		    freeenv(p);
118 	} else {
119 	    printf("bios32: Bad BIOS32 Service Directory\n");
120 	}
121     }
122 
123     /*
124      * PnP BIOS
125      *
126      * Allow user override of PnP BIOS search
127      */
128     if ((((p = getenv("machdep.bios.pnp")) == NULL) || strcmp(p, "disable")) &&
129 	((sigaddr = bios_sigsearch(0, "$PnP", 4, 16, 0)) != 0)) {
130 
131 	/* get a virtual pointer to the structure */
132 	pt = (struct PnPBIOS_table *)(uintptr_t)BIOS_PADDRTOVADDR(sigaddr);
133 	for (cv = (u_int8_t *)pt, ck = 0, i = 0; i < pt->len; i++) {
134 	    ck += cv[i];
135 	}
136 	/* If checksum is OK, enable use of the entrypoint */
137 	if (ck == 0) {
138 	    PnPBIOStable = pt;
139 	    if (bootverbose) {
140 		printf("pnpbios: Found PnP BIOS data at %p\n", pt);
141 		printf("pnpbios: Entry = %x:%x  Rev = %d.%d\n",
142 		       pt->pmentrybase, pt->pmentryoffset, pt->version >> 4, pt->version & 0xf);
143 		if ((pt->control & 0x3) == 0x01)
144 		    printf("pnpbios: Event flag at %x\n", pt->evflagaddr);
145 		if (pt->oemdevid != 0)
146 		    printf("pnpbios: OEM ID %x\n", pt->oemdevid);
147 
148 	    }
149 	} else {
150 	    printf("pnpbios: Bad PnP BIOS data checksum\n");
151 	}
152     }
153     if (p != NULL)
154 	    freeenv(p);
155     if (bootverbose) {
156 	    /* look for other know signatures */
157 	    printf("Other BIOS signatures found:\n");
158     }
159 }
160 
161 /*
162  * bios32_SDlookup
163  *
164  * Query the BIOS32 Service Directory for the service named in (ent),
165  * returns nonzero if the lookup fails.  The caller must fill in
166  * (ent->ident), the remainder are populated on a successful lookup.
167  */
168 int
169 bios32_SDlookup(struct bios32_SDentry *ent)
170 {
171     struct bios_regs args;
172 
173     if (bios32_SDCI == 0)
174 	return (1);
175 
176     args.eax = ent->ident.id;		/* set up arguments */
177     args.ebx = args.ecx = args.edx = 0;
178     bios32(&args, bios32_SDCI, GSEL(GCODE_SEL, SEL_KPL));
179     if ((args.eax & 0xff) == 0) {	/* success? */
180 	ent->base = args.ebx;
181 	ent->len = args.ecx;
182 	ent->entry = args.edx;
183 	ent->ventry = BIOS_PADDRTOVADDR(ent->base + ent->entry);
184 	return (0);			/* all OK */
185     }
186     return (1);				/* failed */
187 }
188 
189 
190 /*
191  * bios_sigsearch
192  *
193  * Search some or all of the BIOS region for a signature string.
194  *
195  * (start)	Optional offset returned from this function
196  *		(for searching for multiple matches), or NULL
197  *		to start the search from the base of the BIOS.
198  *		Note that this will be a _physical_ address in
199  *		the range 0xe0000 - 0xfffff.
200  * (sig)	is a pointer to the byte(s) of the signature.
201  * (siglen)	number of bytes in the signature.
202  * (paralen)	signature paragraph (alignment) size.
203  * (sigofs)	offset of the signature within the paragraph.
204  *
205  * Returns the _physical_ address of the found signature, 0 if the
206  * signature was not found.
207  */
208 
209 u_int32_t
210 bios_sigsearch(u_int32_t start, u_char *sig, int siglen, int paralen, int sigofs)
211 {
212     u_char	*sp, *end;
213 
214     /* compute the starting address */
215     if ((start >= BIOS_START) && (start <= (BIOS_START + BIOS_SIZE))) {
216 	sp = (char *)BIOS_PADDRTOVADDR(start);
217     } else if (start == 0) {
218 	sp = (char *)BIOS_PADDRTOVADDR(BIOS_START);
219     } else {
220 	return 0;				/* bogus start address */
221     }
222 
223     /* compute the end address */
224     end = (u_char *)BIOS_PADDRTOVADDR(BIOS_START + BIOS_SIZE);
225 
226     /* loop searching */
227     while ((sp + sigofs + siglen) < end) {
228 
229 	/* compare here */
230 	if (!bcmp(sp + sigofs, sig, siglen)) {
231 	    /* convert back to physical address */
232 	    return((u_int32_t)BIOS_VADDRTOPADDR(sp));
233 	}
234 	sp += paralen;
235     }
236     return(0);
237 }
238 
239 /*
240  * do not staticize, used by bioscall.s
241  */
242 union {
243     struct {
244 	u_short	offset;
245 	u_short	segment;
246     } vec16;
247     struct {
248 	u_int	offset;
249 	u_short	segment;
250     } vec32;
251 } bioscall_vector;			/* bios jump vector */
252 
253 void
254 set_bios_selectors(struct bios_segments *seg, int flags)
255 {
256     struct soft_segment_descriptor ssd = {
257 	0,			/* segment base address (overwritten) */
258 	0,			/* length (overwritten) */
259 	SDT_MEMERA,		/* segment type (overwritten) */
260 	0,			/* priority level */
261 	1,			/* descriptor present */
262 	0, 0,
263 	1,			/* descriptor size (overwritten) */
264 	0			/* granularity == byte units */
265     };
266     union descriptor *p_gdt;
267 
268 #ifdef SMP
269     p_gdt = &gdt[PCPU_GET(cpuid) * NGDT];
270 #else
271     p_gdt = gdt;
272 #endif
273 
274     ssd.ssd_base = seg->code32.base;
275     ssd.ssd_limit = seg->code32.limit;
276     ssdtosd(&ssd, &p_gdt[GBIOSCODE32_SEL].sd);
277 
278     ssd.ssd_def32 = 0;
279     if (flags & BIOSCODE_FLAG) {
280 	ssd.ssd_base = seg->code16.base;
281 	ssd.ssd_limit = seg->code16.limit;
282 	ssdtosd(&ssd, &p_gdt[GBIOSCODE16_SEL].sd);
283     }
284 
285     ssd.ssd_type = SDT_MEMRWA;
286     if (flags & BIOSDATA_FLAG) {
287 	ssd.ssd_base = seg->data.base;
288 	ssd.ssd_limit = seg->data.limit;
289 	ssdtosd(&ssd, &p_gdt[GBIOSDATA_SEL].sd);
290     }
291 
292     if (flags & BIOSUTIL_FLAG) {
293 	ssd.ssd_base = seg->util.base;
294 	ssd.ssd_limit = seg->util.limit;
295 	ssdtosd(&ssd, &p_gdt[GBIOSUTIL_SEL].sd);
296     }
297 
298     if (flags & BIOSARGS_FLAG) {
299 	ssd.ssd_base = seg->args.base;
300 	ssd.ssd_limit = seg->args.limit;
301 	ssdtosd(&ssd, &p_gdt[GBIOSARGS_SEL].sd);
302     }
303 }
304 
305 extern int vm86pa;
306 extern void bios16_jmp(void);
307 
308 /*
309  * this routine is really greedy with selectors, and uses 5:
310  *
311  * 32-bit code selector:	to return to kernel
312  * 16-bit code selector:	for running code
313  *        data selector:	for 16-bit data
314  *        util selector:	extra utility selector
315  *        args selector:	to handle pointers
316  *
317  * the util selector is set from the util16 entry in bios16_args, if a
318  * "U" specifier is seen.
319  *
320  * See <machine/pc/bios.h> for description of format specifiers
321  */
322 int
323 bios16(struct bios_args *args, char *fmt, ...)
324 {
325     char	*p, *stack, *stack_top;
326     va_list 	ap;
327     int 	flags = BIOSCODE_FLAG | BIOSDATA_FLAG;
328     u_int 	i, arg_start, arg_end;
329     pt_entry_t	*pte;
330     pd_entry_t	*ptd;
331 
332     arg_start = 0xffffffff;
333     arg_end = 0;
334 
335     /*
336      * Some BIOS entrypoints attempt to copy the largest-case
337      * argument frame (in order to generalise handling for
338      * different entry types).  If our argument frame is
339      * smaller than this, the BIOS will reach off the top of
340      * our constructed stack segment.  Pad the top of the stack
341      * with some garbage to avoid this.
342      */
343     stack = (caddr_t)PAGE_SIZE - 32;
344 
345     va_start(ap, fmt);
346     for (p = fmt; p && *p; p++) {
347 	switch (*p) {
348 	case 'p':			/* 32-bit pointer */
349 	    i = va_arg(ap, u_int);
350 	    arg_start = min(arg_start, i);
351 	    arg_end = max(arg_end, i);
352 	    flags |= BIOSARGS_FLAG;
353 	    stack -= 4;
354 	    break;
355 
356 	case 'i':			/* 32-bit integer */
357 	    i = va_arg(ap, u_int);
358 	    stack -= 4;
359 	    break;
360 
361 	case 'U':			/* 16-bit selector */
362 	    flags |= BIOSUTIL_FLAG;
363 	    /* FALLTHROUGH */
364 	case 'D':			/* 16-bit selector */
365 	case 'C':			/* 16-bit selector */
366 	    stack -= 2;
367 	    break;
368 
369 	case 's':			/* 16-bit integer passed as an int */
370 	    i = va_arg(ap, int);
371 	    stack -= 2;
372 	    break;
373 
374 	default:
375 	    return (EINVAL);
376 	}
377     }
378 
379     if (flags & BIOSARGS_FLAG) {
380 	if (arg_end - arg_start > ctob(16))
381 	    return (EACCES);
382 	args->seg.args.base = arg_start;
383 	args->seg.args.limit = 0xffff;
384     }
385 
386     args->seg.code32.base = (u_int)&bios16_jmp & PG_FRAME;
387     args->seg.code32.limit = 0xffff;
388 
389     ptd = (pd_entry_t *)rcr3();
390 #ifdef PAE
391     if (ptd == IdlePDPT)
392 #else
393     if (ptd == IdlePTD)
394 #endif
395     {
396 	/*
397 	 * no page table, so create one and install it.
398 	 */
399 	pte = (pt_entry_t *)malloc(PAGE_SIZE, M_TEMP, M_WAITOK);
400 	ptd = (pd_entry_t *)((u_int)IdlePTD + KERNBASE);
401 	*pte = (vm86pa - PAGE_SIZE) | PG_RW | PG_V;
402 	*ptd = vtophys(pte) | PG_RW | PG_V;
403     } else {
404 	/*
405 	 * this is a user-level page table
406 	 */
407 	pte = PTmap;
408 	*pte = (vm86pa - PAGE_SIZE) | PG_RW | PG_V;
409     }
410     pmap_invalidate_all(kernel_pmap);	/* XXX insurance for now */
411 
412     stack_top = stack;
413     va_start(ap, fmt);
414     for (p = fmt; p && *p; p++) {
415 	switch (*p) {
416 	case 'p':			/* 32-bit pointer */
417 	    i = va_arg(ap, u_int);
418 	    *(u_int *)stack = (i - arg_start) |
419 		(GSEL(GBIOSARGS_SEL, SEL_KPL) << 16);
420 	    stack += 4;
421 	    break;
422 
423 	case 'i':			/* 32-bit integer */
424 	    i = va_arg(ap, u_int);
425 	    *(u_int *)stack = i;
426 	    stack += 4;
427 	    break;
428 
429 	case 'U':			/* 16-bit selector */
430 	    *(u_short *)stack = GSEL(GBIOSUTIL_SEL, SEL_KPL);
431 	    stack += 2;
432 	    break;
433 
434 	case 'D':			/* 16-bit selector */
435 	    *(u_short *)stack = GSEL(GBIOSDATA_SEL, SEL_KPL);
436 	    stack += 2;
437 	    break;
438 
439 	case 'C':			/* 16-bit selector */
440 	    *(u_short *)stack = GSEL(GBIOSCODE16_SEL, SEL_KPL);
441 	    stack += 2;
442 	    break;
443 
444 	case 's':			/* 16-bit integer passed as an int */
445 	    i = va_arg(ap, int);
446 	    *(u_short *)stack = i;
447 	    stack += 2;
448 	    break;
449 
450 	default:
451 	    return (EINVAL);
452 	}
453     }
454 
455     set_bios_selectors(&args->seg, flags);
456     bioscall_vector.vec16.offset = (u_short)args->entry;
457     bioscall_vector.vec16.segment = GSEL(GBIOSCODE16_SEL, SEL_KPL);
458 
459     i = bios16_call(&args->r, stack_top);
460 
461     if (pte == PTmap) {
462 	*pte = 0;			/* remove entry */
463 	/*
464 	 * XXX only needs to be invlpg(0) but that doesn't work on the 386
465 	 */
466 	pmap_invalidate_all(kernel_pmap);
467     } else {
468 	*ptd = 0;			/* remove page table */
469 	/*
470 	 * XXX only needs to be invlpg(0) but that doesn't work on the 386
471 	 */
472 	pmap_invalidate_all(kernel_pmap);
473 	free(pte, M_TEMP);		/* ... and free it */
474     }
475     return (i);
476 }
477 
478 int
479 bios_oem_strings(struct bios_oem *oem, u_char *buffer, size_t maxlen)
480 {
481 	size_t idx = 0;
482 	struct bios_oem_signature *sig;
483 	u_int from, to;
484 	u_char c, *s, *se, *str, *bios_str;
485 	size_t i, off, len, tot;
486 
487 	if ( !oem || !buffer || maxlen<2 )
488 		return(-1);
489 
490 	sig = oem->signature;
491 	if (!sig)
492 		return(-2);
493 
494 	from = oem->range.from;
495 	to = oem->range.to;
496 	if ( (to<=from) || (from<BIOS_START) || (to>(BIOS_START+BIOS_SIZE)) )
497 		return(-3);
498 
499 	while (sig->anchor != NULL) {
500 		str = sig->anchor;
501 		len = strlen(str);
502 		off = sig->offset;
503 		tot = sig->totlen;
504 		/* make sure offset doesn't go beyond bios area */
505 		if ( (to+off)>(BIOS_START+BIOS_SIZE) ||
506 					((from+off)<BIOS_START) ) {
507 			printf("sys/i386/i386/bios.c: sig '%s' "
508 				"from 0x%0x to 0x%0x offset %d "
509 				"out of BIOS bounds 0x%0x - 0x%0x\n",
510 				str, from, to, off,
511 				BIOS_START, BIOS_START+BIOS_SIZE);
512 			return(-4);
513 		}
514 		/* make sure we don't overrun return buffer */
515 		if (idx + tot > maxlen - 1) {
516 			printf("sys/i386/i386/bios.c: sig '%s' "
517 				"idx %d + tot %d = %d > maxlen-1 %d\n",
518 				str, idx, tot, idx+tot, maxlen-1);
519 			return(-5);
520 		}
521 		bios_str = NULL;
522 		s = (u_char *)BIOS_PADDRTOVADDR(from);
523 		se = (u_char *)BIOS_PADDRTOVADDR(to-len);
524 		for (; s<se; s++) {
525 			if (!bcmp(str, s, len)) {
526 				bios_str = s;
527 				break;
528 			}
529 		}
530 		/*
531 		*  store pretty version of totlen bytes of bios string with
532 		*  given offset; 0x20 - 0x7E are printable; uniquify spaces
533 		*/
534 		if (bios_str) {
535 			for (i=0; i<tot; i++) {
536 				c = bios_str[i+off];
537 				if ( (c < 0x20) || (c > 0x7E) )
538 					c = ' ';
539 				if (idx == 0) {
540 					if (c != ' ')
541 						buffer[idx++] = c;
542 				} else if ( (c != ' ') ||
543 					((c == ' ') && (buffer[idx-1] != ' ')) )
544 						buffer[idx++] = c;
545 			}
546 		}
547 		sig++;
548 	}
549 	/* remove a final trailing space */
550 	if ( (idx > 1) && (buffer[idx-1] == ' ') )
551 		idx--;
552 	buffer[idx] = '\0';
553 	return (idx);
554 }
555 
556 #ifdef DEV_ISA
557 /*
558  * PnP BIOS interface; enumerate devices only known to the system
559  * BIOS and save information about them for later use.
560  */
561 
562 struct pnp_sysdev
563 {
564     u_int16_t	size;
565     u_int8_t	handle;
566     u_int32_t	devid;
567     u_int8_t	type[3];
568     u_int16_t	attrib;
569 #define PNPATTR_NODISABLE	(1<<0)	/* can't be disabled */
570 #define PNPATTR_NOCONFIG	(1<<1)	/* can't be configured */
571 #define PNPATTR_OUTPUT		(1<<2)	/* can be primary output */
572 #define PNPATTR_INPUT		(1<<3)	/* can be primary input */
573 #define PNPATTR_BOOTABLE	(1<<4)	/* can be booted from */
574 #define PNPATTR_DOCK		(1<<5)	/* is a docking station */
575 #define PNPATTR_REMOVEABLE	(1<<6)	/* device is removeable */
576 #define PNPATTR_CONFIG_STATIC	(0)
577 #define PNPATTR_CONFIG_DYNAMIC	(1)
578 #define PNPATTR_CONFIG_DYNONLY	(3)
579 #define PNPATTR_CONFIG(a)	(((a) >> 7) & 0x3)
580     /* device-specific data comes here */
581     u_int8_t	devdata[0];
582 } __packed;
583 
584 /* We have to cluster arguments within a 64k range for the bios16 call */
585 struct pnp_sysdevargs
586 {
587     u_int16_t	next;
588     struct pnp_sysdev node;
589 };
590 
591 /*
592  * This function is called after the bus has assigned resource
593  * locations for a logical device.
594  */
595 static void
596 pnpbios_set_config(void *arg, struct isa_config *config, int enable)
597 {
598 }
599 
600 /*
601  * Quiz the PnP BIOS, build a list of PNP IDs and resource data.
602  */
603 static void
604 pnpbios_identify(driver_t *driver, device_t parent)
605 {
606     struct PnPBIOS_table	*pt = PnPBIOStable;
607     struct bios_args		args;
608     struct pnp_sysdev		*pd;
609     struct pnp_sysdevargs	*pda;
610     u_int16_t			ndevs, bigdev;
611     int				error, currdev;
612     u_int8_t			*devnodebuf, tag;
613     u_int32_t			*devid, *compid;
614     int				idx, left;
615     device_t			dev;
616 
617     /* no PnP BIOS information */
618     if (pt == NULL)
619 	return;
620 
621     /* Check to see if ACPI is already active. */
622     dev = devclass_get_device(devclass_find("acpi"), 0);
623     if (dev != NULL && device_is_attached(dev))
624 	return;
625 
626     /* get count of PnP devices */
627     bzero(&args, sizeof(args));
628     args.seg.code16.base = BIOS_PADDRTOVADDR(pt->pmentrybase);
629     args.seg.code16.limit = 0xffff;		/* XXX ? */
630     args.seg.data.base = BIOS_PADDRTOVADDR(pt->pmdataseg);
631     args.seg.data.limit = 0xffff;
632     args.entry = pt->pmentryoffset;
633 
634     if ((error = bios16(&args, PNP_COUNT_DEVNODES, &ndevs, &bigdev)) || (args.r.eax & 0xff)) {
635 	printf("pnpbios: error %d/%x getting device count/size limit\n", error, args.r.eax);
636 	return;
637     }
638     ndevs &= 0xff;				/* clear high byte garbage */
639     if (bootverbose)
640 	printf("pnpbios: %d devices, largest %d bytes\n", ndevs, bigdev);
641 
642     devnodebuf = malloc(bigdev + (sizeof(struct pnp_sysdevargs) - sizeof(struct pnp_sysdev)),
643 			M_DEVBUF, M_NOWAIT);
644     if (devnodebuf == NULL) {
645 	printf("pnpbios: cannot allocate memory, bailing\n");
646 	return;
647     }
648     pda = (struct pnp_sysdevargs *)devnodebuf;
649     pd = &pda->node;
650 
651     for (currdev = 0, left = ndevs; (currdev != 0xff) && (left > 0); left--) {
652 
653 	bzero(pd, bigdev);
654 	pda->next = currdev;
655 	/* get current configuration */
656 	if ((error = bios16(&args, PNP_GET_DEVNODE, &pda->next, &pda->node, 1))) {
657 	    printf("pnpbios: error %d making BIOS16 call\n", error);
658 	    break;
659 	}
660 	if ((error = (args.r.eax & 0xff))) {
661 	    if (bootverbose)
662 		printf("pnpbios: %s 0x%x fetching node %d\n", error & 0x80 ? "error" : "warning", error, currdev);
663 	    if (error & 0x80)
664 		break;
665 	}
666 	currdev = pda->next;
667 	if (pd->size < sizeof(struct pnp_sysdev)) {
668 	    printf("pnpbios: bogus system node data, aborting scan\n");
669 	    break;
670 	}
671 
672 	/*
673 	 * Ignore PICs so that we don't have to worry about the PICs
674 	 * claiming IRQs to prevent their use.  The PIC drivers
675 	 * already ensure that invalid IRQs are not used.
676 	 */
677 	if (!strcmp(pnp_eisaformat(pd->devid), "PNP0000"))	/* ISA PIC */
678 	    continue;
679 	if (!strcmp(pnp_eisaformat(pd->devid), "PNP0003"))	/* APIC */
680 	    continue;
681 
682 	/* Add the device and parse its resources */
683 	dev = BUS_ADD_CHILD(parent, ISA_ORDER_PNPBIOS, NULL, -1);
684 	isa_set_vendorid(dev, pd->devid);
685 	isa_set_logicalid(dev, pd->devid);
686 	/*
687 	 * It appears that some PnP BIOS doesn't allow us to re-enable
688 	 * the embedded system device once it is disabled.  We shall
689 	 * mark all system device nodes as "cannot be disabled", regardless
690 	 * of actual settings in the device attribute byte.
691 	 * XXX
692 	isa_set_configattr(dev,
693 	    ((pd->attrib & PNPATTR_NODISABLE) ?  0 : ISACFGATTR_CANDISABLE) |
694 	    ((!(pd->attrib & PNPATTR_NOCONFIG) &&
695 		PNPATTR_CONFIG(pd->attrib) != PNPATTR_CONFIG_STATIC)
696 		? ISACFGATTR_DYNAMIC : 0));
697 	 */
698 	isa_set_configattr(dev,
699 	    (!(pd->attrib & PNPATTR_NOCONFIG) &&
700 		PNPATTR_CONFIG(pd->attrib) != PNPATTR_CONFIG_STATIC)
701 		? ISACFGATTR_DYNAMIC : 0);
702 	isa_set_pnpbios_handle(dev, pd->handle);
703 	ISA_SET_CONFIG_CALLBACK(parent, dev, pnpbios_set_config, 0);
704 	pnp_parse_resources(dev, &pd->devdata[0],
705 	    pd->size - sizeof(struct pnp_sysdev), 0);
706 	if (!device_get_desc(dev))
707 	    device_set_desc_copy(dev, pnp_eisaformat(pd->devid));
708 
709 	/* Find device IDs */
710 	devid = &pd->devid;
711 	compid = NULL;
712 
713 	/* look for a compatible device ID too */
714 	left = pd->size - sizeof(struct pnp_sysdev);
715 	idx = 0;
716 	while (idx < left) {
717 	    tag = pd->devdata[idx++];
718 	    if (PNP_RES_TYPE(tag) == 0) {
719 		/* Small resource */
720 		switch (PNP_SRES_NUM(tag)) {
721 		case PNP_TAG_COMPAT_DEVICE:
722 		    compid = (u_int32_t *)(pd->devdata + idx);
723 		    if (bootverbose)
724 			printf("pnpbios: node %d compat ID 0x%08x\n", pd->handle, *compid);
725 		    /* FALLTHROUGH */
726 		case PNP_TAG_END:
727 		    idx = left;
728 		    break;
729 		default:
730 		    idx += PNP_SRES_LEN(tag);
731 		    break;
732 		}
733 	    } else
734 		/* Large resource, skip it */
735 		idx += *(u_int16_t *)(pd->devdata + idx) + 2;
736 	}
737 	if (bootverbose) {
738 	    printf("pnpbios: handle %d device ID %s (%08x)",
739 		   pd->handle, pnp_eisaformat(*devid), *devid);
740 	    if (compid != NULL)
741 		printf(" compat ID %s (%08x)",
742 		       pnp_eisaformat(*compid), *compid);
743 	    printf("\n");
744 	}
745     }
746 }
747 
748 static device_method_t pnpbios_methods[] = {
749 	/* Device interface */
750 	DEVMETHOD(device_identify,	pnpbios_identify),
751 
752 	{ 0, 0 }
753 };
754 
755 static driver_t pnpbios_driver = {
756 	"pnpbios",
757 	pnpbios_methods,
758 	1,			/* no softc */
759 };
760 
761 static devclass_t pnpbios_devclass;
762 
763 DRIVER_MODULE(pnpbios, isa, pnpbios_driver, pnpbios_devclass, 0, 0);
764 #endif /* DEV_ISA */
765