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