xref: /freebsd/sys/i386/i386/bios.c (revision 5370c80e0e6aa7aaaf53e62ec4010cee92440a8f)
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 = kern_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 = kern_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 	    va_end(ap);
376 	    return (EINVAL);
377 	}
378     }
379     va_end(ap);
380 
381     if (flags & BIOSARGS_FLAG) {
382 	if (arg_end - arg_start > ctob(16))
383 	    return (EACCES);
384 	args->seg.args.base = arg_start;
385 	args->seg.args.limit = 0xffff;
386     }
387 
388     args->seg.code32.base = (u_int)&bios16_jmp & PG_FRAME;
389     args->seg.code32.limit = 0xffff;
390 
391     ptd = (pd_entry_t *)rcr3();
392 #if defined(PAE) || defined(PAE_TABLES)
393     if (ptd == IdlePDPT)
394 #else
395     if (ptd == IdlePTD)
396 #endif
397     {
398 	/*
399 	 * no page table, so create one and install it.
400 	 */
401 	pte = (pt_entry_t *)malloc(PAGE_SIZE, M_TEMP, M_WAITOK);
402 	ptd = (pd_entry_t *)((u_int)IdlePTD + KERNBASE);
403 	*pte = (vm86pa - PAGE_SIZE) | PG_RW | PG_V;
404 	*ptd = vtophys(pte) | PG_RW | PG_V;
405     } else {
406 	/*
407 	 * this is a user-level page table
408 	 */
409 	pte = PTmap;
410 	*pte = (vm86pa - PAGE_SIZE) | PG_RW | PG_V;
411     }
412     pmap_invalidate_all(kernel_pmap);	/* XXX insurance for now */
413 
414     stack_top = stack;
415     va_start(ap, fmt);
416     for (p = fmt; p && *p; p++) {
417 	switch (*p) {
418 	case 'p':			/* 32-bit pointer */
419 	    i = va_arg(ap, u_int);
420 	    *(u_int *)stack = (i - arg_start) |
421 		(GSEL(GBIOSARGS_SEL, SEL_KPL) << 16);
422 	    stack += 4;
423 	    break;
424 
425 	case 'i':			/* 32-bit integer */
426 	    i = va_arg(ap, u_int);
427 	    *(u_int *)stack = i;
428 	    stack += 4;
429 	    break;
430 
431 	case 'U':			/* 16-bit selector */
432 	    *(u_short *)stack = GSEL(GBIOSUTIL_SEL, SEL_KPL);
433 	    stack += 2;
434 	    break;
435 
436 	case 'D':			/* 16-bit selector */
437 	    *(u_short *)stack = GSEL(GBIOSDATA_SEL, SEL_KPL);
438 	    stack += 2;
439 	    break;
440 
441 	case 'C':			/* 16-bit selector */
442 	    *(u_short *)stack = GSEL(GBIOSCODE16_SEL, SEL_KPL);
443 	    stack += 2;
444 	    break;
445 
446 	case 's':			/* 16-bit integer passed as an int */
447 	    i = va_arg(ap, int);
448 	    *(u_short *)stack = i;
449 	    stack += 2;
450 	    break;
451 
452 	default:
453 	    va_end(ap);
454 	    return (EINVAL);
455 	}
456     }
457     va_end(ap);
458 
459     set_bios_selectors(&args->seg, flags);
460     bioscall_vector.vec16.offset = (u_short)args->entry;
461     bioscall_vector.vec16.segment = GSEL(GBIOSCODE16_SEL, SEL_KPL);
462 
463     i = bios16_call(&args->r, stack_top);
464 
465     if (pte == PTmap) {
466 	*pte = 0;			/* remove entry */
467 	/*
468 	 * XXX only needs to be invlpg(0) but that doesn't work on the 386
469 	 */
470 	pmap_invalidate_all(kernel_pmap);
471     } else {
472 	*ptd = 0;			/* remove page table */
473 	/*
474 	 * XXX only needs to be invlpg(0) but that doesn't work on the 386
475 	 */
476 	pmap_invalidate_all(kernel_pmap);
477 	free(pte, M_TEMP);		/* ... and free it */
478     }
479     return (i);
480 }
481 
482 int
483 bios_oem_strings(struct bios_oem *oem, u_char *buffer, size_t maxlen)
484 {
485 	size_t idx = 0;
486 	struct bios_oem_signature *sig;
487 	u_int from, to;
488 	u_char c, *s, *se, *str, *bios_str;
489 	size_t i, off, len, tot;
490 
491 	if ( !oem || !buffer || maxlen<2 )
492 		return(-1);
493 
494 	sig = oem->signature;
495 	if (!sig)
496 		return(-2);
497 
498 	from = oem->range.from;
499 	to = oem->range.to;
500 	if ( (to<=from) || (from<BIOS_START) || (to>(BIOS_START+BIOS_SIZE)) )
501 		return(-3);
502 
503 	while (sig->anchor != NULL) {
504 		str = sig->anchor;
505 		len = strlen(str);
506 		off = sig->offset;
507 		tot = sig->totlen;
508 		/* make sure offset doesn't go beyond bios area */
509 		if ( (to+off)>(BIOS_START+BIOS_SIZE) ||
510 					((from+off)<BIOS_START) ) {
511 			printf("sys/i386/i386/bios.c: sig '%s' "
512 				"from 0x%0x to 0x%0x offset %d "
513 				"out of BIOS bounds 0x%0x - 0x%0x\n",
514 				str, from, to, off,
515 				BIOS_START, BIOS_START+BIOS_SIZE);
516 			return(-4);
517 		}
518 		/* make sure we don't overrun return buffer */
519 		if (idx + tot > maxlen - 1) {
520 			printf("sys/i386/i386/bios.c: sig '%s' "
521 				"idx %d + tot %d = %d > maxlen-1 %d\n",
522 				str, idx, tot, idx+tot, maxlen-1);
523 			return(-5);
524 		}
525 		bios_str = NULL;
526 		s = (u_char *)BIOS_PADDRTOVADDR(from);
527 		se = (u_char *)BIOS_PADDRTOVADDR(to-len);
528 		for (; s<se; s++) {
529 			if (!bcmp(str, s, len)) {
530 				bios_str = s;
531 				break;
532 			}
533 		}
534 		/*
535 		*  store pretty version of totlen bytes of bios string with
536 		*  given offset; 0x20 - 0x7E are printable; uniquify spaces
537 		*/
538 		if (bios_str) {
539 			for (i=0; i<tot; i++) {
540 				c = bios_str[i+off];
541 				if ( (c < 0x20) || (c > 0x7E) )
542 					c = ' ';
543 				if (idx == 0) {
544 					if (c != ' ')
545 						buffer[idx++] = c;
546 				} else if ( (c != ' ') ||
547 					((c == ' ') && (buffer[idx-1] != ' ')) )
548 						buffer[idx++] = c;
549 			}
550 		}
551 		sig++;
552 	}
553 	/* remove a final trailing space */
554 	if ( (idx > 1) && (buffer[idx-1] == ' ') )
555 		idx--;
556 	buffer[idx] = '\0';
557 	return (idx);
558 }
559 
560 #ifdef DEV_ISA
561 /*
562  * PnP BIOS interface; enumerate devices only known to the system
563  * BIOS and save information about them for later use.
564  */
565 
566 struct pnp_sysdev
567 {
568     u_int16_t	size;
569     u_int8_t	handle;
570     u_int32_t	devid;
571     u_int8_t	type[3];
572     u_int16_t	attrib;
573 #define PNPATTR_NODISABLE	(1<<0)	/* can't be disabled */
574 #define PNPATTR_NOCONFIG	(1<<1)	/* can't be configured */
575 #define PNPATTR_OUTPUT		(1<<2)	/* can be primary output */
576 #define PNPATTR_INPUT		(1<<3)	/* can be primary input */
577 #define PNPATTR_BOOTABLE	(1<<4)	/* can be booted from */
578 #define PNPATTR_DOCK		(1<<5)	/* is a docking station */
579 #define PNPATTR_REMOVEABLE	(1<<6)	/* device is removeable */
580 #define PNPATTR_CONFIG_STATIC	(0)
581 #define PNPATTR_CONFIG_DYNAMIC	(1)
582 #define PNPATTR_CONFIG_DYNONLY	(3)
583 #define PNPATTR_CONFIG(a)	(((a) >> 7) & 0x3)
584     /* device-specific data comes here */
585     u_int8_t	devdata[0];
586 } __packed;
587 
588 /* We have to cluster arguments within a 64k range for the bios16 call */
589 struct pnp_sysdevargs
590 {
591     u_int16_t	next;
592     struct pnp_sysdev node;
593 };
594 
595 /*
596  * This function is called after the bus has assigned resource
597  * locations for a logical device.
598  */
599 static void
600 pnpbios_set_config(void *arg, struct isa_config *config, int enable)
601 {
602 }
603 
604 /*
605  * Quiz the PnP BIOS, build a list of PNP IDs and resource data.
606  */
607 static void
608 pnpbios_identify(driver_t *driver, device_t parent)
609 {
610     struct PnPBIOS_table	*pt = PnPBIOStable;
611     struct bios_args		args;
612     struct pnp_sysdev		*pd;
613     struct pnp_sysdevargs	*pda;
614     u_int16_t			ndevs, bigdev;
615     int				error, currdev;
616     u_int8_t			*devnodebuf, tag;
617     u_int32_t			*devid, *compid;
618     int				idx, left;
619     device_t			dev;
620 
621     /* no PnP BIOS information */
622     if (pt == NULL)
623 	return;
624 
625     /* Check to see if ACPI is already active. */
626     dev = devclass_get_device(devclass_find("acpi"), 0);
627     if (dev != NULL && device_is_attached(dev))
628 	return;
629 
630     /* get count of PnP devices */
631     bzero(&args, sizeof(args));
632     args.seg.code16.base = BIOS_PADDRTOVADDR(pt->pmentrybase);
633     args.seg.code16.limit = 0xffff;		/* XXX ? */
634     args.seg.data.base = BIOS_PADDRTOVADDR(pt->pmdataseg);
635     args.seg.data.limit = 0xffff;
636     args.entry = pt->pmentryoffset;
637 
638     if ((error = bios16(&args, PNP_COUNT_DEVNODES, &ndevs, &bigdev)) || (args.r.eax & 0xff)) {
639 	printf("pnpbios: error %d/%x getting device count/size limit\n", error, args.r.eax);
640 	return;
641     }
642     ndevs &= 0xff;				/* clear high byte garbage */
643     if (bootverbose)
644 	printf("pnpbios: %d devices, largest %d bytes\n", ndevs, bigdev);
645 
646     devnodebuf = malloc(bigdev + (sizeof(struct pnp_sysdevargs) - sizeof(struct pnp_sysdev)),
647 			M_DEVBUF, M_NOWAIT);
648     if (devnodebuf == NULL) {
649 	printf("pnpbios: cannot allocate memory, bailing\n");
650 	return;
651     }
652     pda = (struct pnp_sysdevargs *)devnodebuf;
653     pd = &pda->node;
654 
655     for (currdev = 0, left = ndevs; (currdev != 0xff) && (left > 0); left--) {
656 
657 	bzero(pd, bigdev);
658 	pda->next = currdev;
659 	/* get current configuration */
660 	if ((error = bios16(&args, PNP_GET_DEVNODE, &pda->next, &pda->node, 1))) {
661 	    printf("pnpbios: error %d making BIOS16 call\n", error);
662 	    break;
663 	}
664 	if ((error = (args.r.eax & 0xff))) {
665 	    if (bootverbose)
666 		printf("pnpbios: %s 0x%x fetching node %d\n", error & 0x80 ? "error" : "warning", error, currdev);
667 	    if (error & 0x80)
668 		break;
669 	}
670 	currdev = pda->next;
671 	if (pd->size < sizeof(struct pnp_sysdev)) {
672 	    printf("pnpbios: bogus system node data, aborting scan\n");
673 	    break;
674 	}
675 
676 	/*
677 	 * Ignore PICs so that we don't have to worry about the PICs
678 	 * claiming IRQs to prevent their use.  The PIC drivers
679 	 * already ensure that invalid IRQs are not used.
680 	 */
681 	if (!strcmp(pnp_eisaformat(pd->devid), "PNP0000"))	/* ISA PIC */
682 	    continue;
683 	if (!strcmp(pnp_eisaformat(pd->devid), "PNP0003"))	/* APIC */
684 	    continue;
685 
686 	/* Add the device and parse its resources */
687 	dev = BUS_ADD_CHILD(parent, ISA_ORDER_PNPBIOS, NULL, -1);
688 	isa_set_vendorid(dev, pd->devid);
689 	isa_set_logicalid(dev, pd->devid);
690 	/*
691 	 * It appears that some PnP BIOS doesn't allow us to re-enable
692 	 * the embedded system device once it is disabled.  We shall
693 	 * mark all system device nodes as "cannot be disabled", regardless
694 	 * of actual settings in the device attribute byte.
695 	 * XXX
696 	isa_set_configattr(dev,
697 	    ((pd->attrib & PNPATTR_NODISABLE) ?  0 : ISACFGATTR_CANDISABLE) |
698 	    ((!(pd->attrib & PNPATTR_NOCONFIG) &&
699 		PNPATTR_CONFIG(pd->attrib) != PNPATTR_CONFIG_STATIC)
700 		? ISACFGATTR_DYNAMIC : 0));
701 	 */
702 	isa_set_configattr(dev,
703 	    (!(pd->attrib & PNPATTR_NOCONFIG) &&
704 		PNPATTR_CONFIG(pd->attrib) != PNPATTR_CONFIG_STATIC)
705 		? ISACFGATTR_DYNAMIC : 0);
706 	isa_set_pnpbios_handle(dev, pd->handle);
707 	ISA_SET_CONFIG_CALLBACK(parent, dev, pnpbios_set_config, 0);
708 	pnp_parse_resources(dev, &pd->devdata[0],
709 	    pd->size - sizeof(struct pnp_sysdev), 0);
710 	if (!device_get_desc(dev))
711 	    device_set_desc_copy(dev, pnp_eisaformat(pd->devid));
712 
713 	/* Find device IDs */
714 	devid = &pd->devid;
715 	compid = NULL;
716 
717 	/* look for a compatible device ID too */
718 	left = pd->size - sizeof(struct pnp_sysdev);
719 	idx = 0;
720 	while (idx < left) {
721 	    tag = pd->devdata[idx++];
722 	    if (PNP_RES_TYPE(tag) == 0) {
723 		/* Small resource */
724 		switch (PNP_SRES_NUM(tag)) {
725 		case PNP_TAG_COMPAT_DEVICE:
726 		    compid = (u_int32_t *)(pd->devdata + idx);
727 		    if (bootverbose)
728 			printf("pnpbios: node %d compat ID 0x%08x\n", pd->handle, *compid);
729 		    /* FALLTHROUGH */
730 		case PNP_TAG_END:
731 		    idx = left;
732 		    break;
733 		default:
734 		    idx += PNP_SRES_LEN(tag);
735 		    break;
736 		}
737 	    } else
738 		/* Large resource, skip it */
739 		idx += *(u_int16_t *)(pd->devdata + idx) + 2;
740 	}
741 	if (bootverbose) {
742 	    printf("pnpbios: handle %d device ID %s (%08x)",
743 		   pd->handle, pnp_eisaformat(*devid), *devid);
744 	    if (compid != NULL)
745 		printf(" compat ID %s (%08x)",
746 		       pnp_eisaformat(*compid), *compid);
747 	    printf("\n");
748 	}
749     }
750 }
751 
752 static device_method_t pnpbios_methods[] = {
753 	/* Device interface */
754 	DEVMETHOD(device_identify,	pnpbios_identify),
755 
756 	{ 0, 0 }
757 };
758 
759 static driver_t pnpbios_driver = {
760 	"pnpbios",
761 	pnpbios_methods,
762 	1,			/* no softc */
763 };
764 
765 static devclass_t pnpbios_devclass;
766 
767 DRIVER_MODULE(pnpbios, isa, pnpbios_driver, pnpbios_devclass, 0, 0);
768 #endif /* DEV_ISA */
769