xref: /freebsd/sys/i386/i386/bios.c (revision 4f29da19bd44f0e99f021510460a81bf754c21d2)
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 bios_oem_strings(struct bios_oem *oem, u_char *buffer, size_t maxlen)
479 {
480 	size_t idx = 0;
481 	struct bios_oem_signature *sig;
482 	u_int from, to;
483 	u_char c, *s, *se, *str, *bios_str;
484 	size_t i, off, len, tot;
485 
486 	if ( !oem || !buffer || maxlen<2 )
487 		return(-1);
488 
489 	sig = oem->signature;
490 	if (!sig)
491 		return(-2);
492 
493 	from = oem->range.from;
494 	to = oem->range.to;
495 	if ( (to<=from) || (from<BIOS_START) || (to>(BIOS_START+BIOS_SIZE)) )
496 		return(-3);
497 
498 	while (sig->anchor != NULL) {
499 		str = sig->anchor;
500 		len = strlen(str);
501 		off = sig->offset;
502 		tot = sig->totlen;
503 		/* make sure offset doesn't go beyond bios area */
504 		if ( (to+off)>(BIOS_START+BIOS_SIZE) ||
505 					((from+off)<BIOS_START) ) {
506 			printf("sys/i386/i386/bios.c: sig '%s' "
507 				"from 0x%0x to 0x%0x offset %d "
508 				"out of BIOS bounds 0x%0x - 0x%0x\n",
509 				str, from, to, off,
510 				BIOS_START, BIOS_START+BIOS_SIZE);
511 			return(-4);
512 		}
513 		/* make sure we don't overrun return buffer */
514 		if (idx + tot > maxlen - 1) {
515 			printf("sys/i386/i386/bios.c: sig '%s' "
516 				"idx %d + tot %d = %d > maxlen-1 %d\n",
517 				str, idx, tot, idx+tot, maxlen-1);
518 			return(-5);
519 		}
520 		bios_str = NULL;
521 		s = (u_char *)BIOS_PADDRTOVADDR(from);
522 		se = (u_char *)BIOS_PADDRTOVADDR(to-len);
523 		for (; s<se; s++) {
524 			if (!memcmp(str, s, len)) {
525 				bios_str = s;
526 				break;
527 			}
528 		}
529 		/*
530 		*  store pretty version of totlen bytes of bios string with
531 		*  given offset; 0x20 - 0x7E are printable; uniquify spaces
532 		*/
533 		if (bios_str) {
534 			for (i=0; i<tot; i++) {
535 				c = bios_str[i+off];
536 				if ( (c < 0x20) || (c > 0x7E) )
537 					c = ' ';
538 				if (idx == 0) {
539 					if (c != ' ')
540 						buffer[idx++] = c;
541 				} else if ( (c != ' ') ||
542 					((c == ' ') && (buffer[idx-1] != ' ')) )
543 						buffer[idx++] = c;
544 			}
545 		}
546 		sig++;
547 	}
548 	/* remove a final trailing space */
549 	if ( (idx > 1) && (buffer[idx-1] == ' ') )
550 		idx--;
551 	buffer[idx] = '\0';
552 	return (idx);
553 }
554 
555 #ifdef DEV_ISA
556 /*
557  * PnP BIOS interface; enumerate devices only known to the system
558  * BIOS and save information about them for later use.
559  */
560 
561 struct pnp_sysdev
562 {
563     u_int16_t	size;
564     u_int8_t	handle;
565     u_int32_t	devid;
566     u_int8_t	type[3];
567     u_int16_t	attrib;
568 #define PNPATTR_NODISABLE	(1<<0)	/* can't be disabled */
569 #define PNPATTR_NOCONFIG	(1<<1)	/* can't be configured */
570 #define PNPATTR_OUTPUT		(1<<2)	/* can be primary output */
571 #define PNPATTR_INPUT		(1<<3)	/* can be primary input */
572 #define PNPATTR_BOOTABLE	(1<<4)	/* can be booted from */
573 #define PNPATTR_DOCK		(1<<5)	/* is a docking station */
574 #define PNPATTR_REMOVEABLE	(1<<6)	/* device is removeable */
575 #define PNPATTR_CONFIG_STATIC	(0)
576 #define PNPATTR_CONFIG_DYNAMIC	(1)
577 #define PNPATTR_CONFIG_DYNONLY	(3)
578 #define PNPATTR_CONFIG(a)	(((a) >> 7) & 0x3)
579     /* device-specific data comes here */
580     u_int8_t	devdata[0];
581 } __packed;
582 
583 /* We have to cluster arguments within a 64k range for the bios16 call */
584 struct pnp_sysdevargs
585 {
586     u_int16_t	next;
587     struct pnp_sysdev node;
588 };
589 
590 /*
591  * This function is called after the bus has assigned resource
592  * locations for a logical device.
593  */
594 static void
595 pnpbios_set_config(void *arg, struct isa_config *config, int enable)
596 {
597 }
598 
599 /*
600  * Quiz the PnP BIOS, build a list of PNP IDs and resource data.
601  */
602 static void
603 pnpbios_identify(driver_t *driver, device_t parent)
604 {
605     struct PnPBIOS_table	*pt = PnPBIOStable;
606     struct bios_args		args;
607     struct pnp_sysdev		*pd;
608     struct pnp_sysdevargs	*pda;
609     u_int16_t			ndevs, bigdev;
610     int				error, currdev;
611     u_int8_t			*devnodebuf, tag;
612     u_int32_t			*devid, *compid;
613     int				idx, left;
614     device_t			dev;
615 
616     /* no PnP BIOS information */
617     if (pt == NULL)
618 	return;
619 
620     /* Check to see if ACPI is already active. */
621     dev = devclass_get_device(devclass_find("acpi"), 0);
622     if (dev != NULL && device_is_attached(dev))
623 	return;
624 
625     /* get count of PnP devices */
626     bzero(&args, sizeof(args));
627     args.seg.code16.base = BIOS_PADDRTOVADDR(pt->pmentrybase);
628     args.seg.code16.limit = 0xffff;		/* XXX ? */
629     args.seg.data.base = BIOS_PADDRTOVADDR(pt->pmdataseg);
630     args.seg.data.limit = 0xffff;
631     args.entry = pt->pmentryoffset;
632 
633     if ((error = bios16(&args, PNP_COUNT_DEVNODES, &ndevs, &bigdev)) || (args.r.eax & 0xff)) {
634 	printf("pnpbios: error %d/%x getting device count/size limit\n", error, args.r.eax);
635 	return;
636     }
637     ndevs &= 0xff;				/* clear high byte garbage */
638     if (bootverbose)
639 	printf("pnpbios: %d devices, largest %d bytes\n", ndevs, bigdev);
640 
641     devnodebuf = malloc(bigdev + (sizeof(struct pnp_sysdevargs) - sizeof(struct pnp_sysdev)),
642 			M_DEVBUF, M_NOWAIT);
643     if (devnodebuf == NULL) {
644 	printf("pnpbios: cannot allocate memory, bailing\n");
645 	return;
646     }
647     pda = (struct pnp_sysdevargs *)devnodebuf;
648     pd = &pda->node;
649 
650     for (currdev = 0, left = ndevs; (currdev != 0xff) && (left > 0); left--) {
651 
652 	bzero(pd, bigdev);
653 	pda->next = currdev;
654 	/* get current configuration */
655 	if ((error = bios16(&args, PNP_GET_DEVNODE, &pda->next, &pda->node, 1))) {
656 	    printf("pnpbios: error %d making BIOS16 call\n", error);
657 	    break;
658 	}
659 	if ((error = (args.r.eax & 0xff))) {
660 	    if (bootverbose)
661 		printf("pnpbios: %s 0x%x fetching node %d\n", error & 0x80 ? "error" : "warning", error, currdev);
662 	    if (error & 0x80)
663 		break;
664 	}
665 	currdev = pda->next;
666 	if (pd->size < sizeof(struct pnp_sysdev)) {
667 	    printf("pnpbios: bogus system node data, aborting scan\n");
668 	    break;
669 	}
670 
671 	/*
672 	 * Ignore PICs so that we don't have to worry about the PICs
673 	 * claiming IRQs to prevent their use.  The PIC drivers
674 	 * already ensure that invalid IRQs are not used.
675 	 */
676 	if (!strcmp(pnp_eisaformat(pd->devid), "PNP0000"))	/* ISA PIC */
677 	    continue;
678 	if (!strcmp(pnp_eisaformat(pd->devid), "PNP0003"))	/* APIC */
679 	    continue;
680 
681 	/* Add the device and parse its resources */
682 	dev = BUS_ADD_CHILD(parent, ISA_ORDER_PNPBIOS, NULL, -1);
683 	isa_set_vendorid(dev, pd->devid);
684 	isa_set_logicalid(dev, pd->devid);
685 	/*
686 	 * It appears that some PnP BIOS doesn't allow us to re-enable
687 	 * the embedded system device once it is disabled.  We shall
688 	 * mark all system device nodes as "cannot be disabled", regardless
689 	 * of actual settings in the device attribute byte.
690 	 * XXX
691 	isa_set_configattr(dev,
692 	    ((pd->attrib & PNPATTR_NODISABLE) ?  0 : ISACFGATTR_CANDISABLE) |
693 	    ((!(pd->attrib & PNPATTR_NOCONFIG) &&
694 		PNPATTR_CONFIG(pd->attrib) != PNPATTR_CONFIG_STATIC)
695 		? ISACFGATTR_DYNAMIC : 0));
696 	 */
697 	isa_set_configattr(dev,
698 	    (!(pd->attrib & PNPATTR_NOCONFIG) &&
699 		PNPATTR_CONFIG(pd->attrib) != PNPATTR_CONFIG_STATIC)
700 		? ISACFGATTR_DYNAMIC : 0);
701 	ISA_SET_CONFIG_CALLBACK(parent, dev, pnpbios_set_config, 0);
702 	pnp_parse_resources(dev, &pd->devdata[0],
703 			    pd->size - sizeof(struct pnp_sysdev), 0);
704 	if (!device_get_desc(dev))
705 	    device_set_desc_copy(dev, pnp_eisaformat(pd->devid));
706 
707 	/* Find device IDs */
708 	devid = &pd->devid;
709 	compid = NULL;
710 
711 	/* look for a compatible device ID too */
712 	left = pd->size - sizeof(struct pnp_sysdev);
713 	idx = 0;
714 	while (idx < left) {
715 	    tag = pd->devdata[idx++];
716 	    if (PNP_RES_TYPE(tag) == 0) {
717 		/* Small resource */
718 		switch (PNP_SRES_NUM(tag)) {
719 		case PNP_TAG_COMPAT_DEVICE:
720 		    compid = (u_int32_t *)(pd->devdata + idx);
721 		    if (bootverbose)
722 			printf("pnpbios: node %d compat ID 0x%08x\n", pd->handle, *compid);
723 		    /* FALLTHROUGH */
724 		case PNP_TAG_END:
725 		    idx = left;
726 		    break;
727 		default:
728 		    idx += PNP_SRES_LEN(tag);
729 		    break;
730 		}
731 	    } else
732 		/* Large resource, skip it */
733 		idx += *(u_int16_t *)(pd->devdata + idx) + 2;
734 	}
735 	if (bootverbose) {
736 	    printf("pnpbios: handle %d device ID %s (%08x)",
737 		   pd->handle, pnp_eisaformat(*devid), *devid);
738 	    if (compid != NULL)
739 		printf(" compat ID %s (%08x)",
740 		       pnp_eisaformat(*compid), *compid);
741 	    printf("\n");
742 	}
743     }
744 }
745 
746 static device_method_t pnpbios_methods[] = {
747 	/* Device interface */
748 	DEVMETHOD(device_identify,	pnpbios_identify),
749 
750 	{ 0, 0 }
751 };
752 
753 static driver_t pnpbios_driver = {
754 	"pnpbios",
755 	pnpbios_methods,
756 	1,			/* no softc */
757 };
758 
759 static devclass_t pnpbios_devclass;
760 
761 DRIVER_MODULE(pnpbios, isa, pnpbios_driver, pnpbios_devclass, 0, 0);
762 #endif /* DEV_ISA */
763