xref: /freebsd/sys/i386/i386/bios.c (revision cb5f885bdf308bbf48228588f62cf59a27e2e82f)
1d561028dSMike Smith /*-
2d561028dSMike Smith  * Copyright (c) 1997 Michael Smith
3496027bfSMike Smith  * Copyright (c) 1998 Jonathan Lemon
4d561028dSMike Smith  * All rights reserved.
5d561028dSMike Smith  *
6d561028dSMike Smith  * Redistribution and use in source and binary forms, with or without
7d561028dSMike Smith  * modification, are permitted provided that the following conditions
8d561028dSMike Smith  * are met:
9d561028dSMike Smith  * 1. Redistributions of source code must retain the above copyright
10d561028dSMike Smith  *    notice, this list of conditions and the following disclaimer.
11d561028dSMike Smith  * 2. Redistributions in binary form must reproduce the above copyright
12d561028dSMike Smith  *    notice, this list of conditions and the following disclaimer in the
13d561028dSMike Smith  *    documentation and/or other materials provided with the distribution.
14d561028dSMike Smith  *
15d561028dSMike Smith  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16d561028dSMike Smith  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17d561028dSMike Smith  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18d561028dSMike Smith  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19d561028dSMike Smith  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20d561028dSMike Smith  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21d561028dSMike Smith  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22d561028dSMike Smith  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23d561028dSMike Smith  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24d561028dSMike Smith  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25d561028dSMike Smith  * SUCH DAMAGE.
26d561028dSMike Smith  *
273cc31bc2SMike Smith  *      $Id: bios.c,v 1.14 1999/07/29 06:48:26 msmith Exp $
28d561028dSMike Smith  */
29d561028dSMike Smith 
30d561028dSMike Smith /*
31d561028dSMike Smith  * Code for dealing with the BIOS in x86 PC systems.
32d561028dSMike Smith  */
33d561028dSMike Smith 
34d561028dSMike Smith #include <sys/param.h>
35496027bfSMike Smith #include <sys/proc.h>
36d561028dSMike Smith #include <sys/systm.h>
37d561028dSMike Smith #include <sys/kernel.h>
38496027bfSMike Smith #include <sys/malloc.h>
39d561028dSMike Smith #include <vm/vm.h>
40d561028dSMike Smith #include <vm/pmap.h>
41d561028dSMike Smith #include <machine/md_var.h>
42496027bfSMike Smith #include <machine/segments.h>
43496027bfSMike Smith #include <machine/stdarg.h>
44496027bfSMike Smith #include <machine/tss.h>
45496027bfSMike Smith #include <machine/vmparam.h>
46d561028dSMike Smith #include <machine/pc/bios.h>
47cb5f885bSMike Smith #include <i386/isa/isa_device.h>
48cb5f885bSMike Smith #include <i386/isa/pnp.h>
49d561028dSMike Smith 
50d561028dSMike Smith #define BIOS_START	0xe0000
51d561028dSMike Smith #define BIOS_SIZE	0x20000
52d561028dSMike Smith 
53d561028dSMike Smith /* exported lookup results */
54d561028dSMike Smith struct bios32_SDentry		PCIbios = {entry : 0};
55cb5f885bSMike Smith struct SMBIOS_table		*SMBIOStable = 0;
56cb5f885bSMike Smith struct PnPBIOS_table		*PnPBIOStable = 0;
57d561028dSMike Smith 
58496027bfSMike Smith static u_int			bios32_SDCI = 0;
59d561028dSMike Smith 
60d561028dSMike Smith /* start fairly early */
61cb5f885bSMike Smith static void			bios32_init(void *junk);
62d561028dSMike Smith SYSINIT(bios32, SI_SUB_CPU, SI_ORDER_ANY, bios32_init, NULL);
63d561028dSMike Smith 
64cb5f885bSMike Smith static void	pnpbios_scan(void);
65cb5f885bSMike Smith static char 	*pnp_eisaformat(u_int8_t *data);
66cb5f885bSMike Smith 
67cb5f885bSMike Smith 
68d561028dSMike Smith /*
69d561028dSMike Smith  * bios32_init
70d561028dSMike Smith  *
71d561028dSMike Smith  * Locate various bios32 entities.
72d561028dSMike Smith  */
73d561028dSMike Smith static void
74d561028dSMike Smith bios32_init(void *junk)
75d561028dSMike Smith {
76d561028dSMike Smith     u_long			sigaddr;
77d561028dSMike Smith     struct bios32_SDheader	*sdh;
78d561028dSMike Smith     struct SMBIOS_table		*sbt;
79cb5f885bSMike Smith     struct PnPBIOS_table	*pt;
80d561028dSMike Smith     u_int8_t			ck, *cv;
81d561028dSMike Smith     int				i;
82d561028dSMike Smith 
83d561028dSMike Smith 
84d561028dSMike Smith     /*
85d561028dSMike Smith      * BIOS32 Service Directory
86d561028dSMike Smith      */
87d561028dSMike Smith 
88d561028dSMike Smith     /* look for the signature */
89d561028dSMike Smith     if ((sigaddr = bios_sigsearch(0, "_32_", 4, 16, 0)) != 0) {
90d561028dSMike Smith 
91d561028dSMike Smith 	/* get a virtual pointer to the structure */
92fc93c1bdSBruce Evans 	sdh = (struct bios32_SDheader *)(uintptr_t)BIOS_PADDRTOVADDR(sigaddr);
93d561028dSMike Smith 	for (cv = (u_int8_t *)sdh, ck = 0, i = 0; i < (sdh->len * 16); i++) {
94d561028dSMike Smith 	    ck += cv[i];
95d561028dSMike Smith 	}
96d561028dSMike Smith 	/* If checksum is OK, enable use of the entrypoint */
97d20358b3SMike Smith 	if ((ck == 0) && (sdh->entry < (BIOS_START + BIOS_SIZE))) {
98496027bfSMike Smith 	    bios32_SDCI = BIOS_PADDRTOVADDR(sdh->entry);
99d561028dSMike Smith 	    if (bootverbose) {
100cb5f885bSMike Smith 		printf("bios32: Found BIOS32 Service Directory header at %p\n", sdh);
101cb5f885bSMike Smith 		printf("bios32: Entry = 0x%x (%x)  Rev = %d  Len = %d\n",
102d561028dSMike Smith 		       sdh->entry, bios32_SDCI, sdh->revision, sdh->len);
103d561028dSMike Smith 	    }
104d561028dSMike Smith 	    /* See if there's a PCI BIOS entrypoint here */
105d561028dSMike Smith 	    PCIbios.ident.id = 0x49435024;	/* PCI systems should have this */
106d561028dSMike Smith 	    if (!bios32_SDlookup(&PCIbios) && bootverbose)
107cb5f885bSMike Smith 		printf("pcibios: PCI BIOS entry at 0x%x\n", PCIbios.entry);
108d561028dSMike Smith 	} else {
109cb5f885bSMike Smith 	    printf("bios32: Bad BIOS32 Service Directory\n");
110d561028dSMike Smith 	}
111d561028dSMike Smith     }
112d561028dSMike Smith 
113d561028dSMike Smith     /*
114d561028dSMike Smith      * System Management BIOS
115d561028dSMike Smith      */
116d561028dSMike Smith     /* look for the SMBIOS signature */
117d561028dSMike Smith     if ((sigaddr = bios_sigsearch(0, "_SM_", 4, 16, 0)) != 0) {
118d561028dSMike Smith 
119d561028dSMike Smith 	/* get a virtual pointer to the structure */
120fc93c1bdSBruce Evans 	sbt = (struct SMBIOS_table *)(uintptr_t)BIOS_PADDRTOVADDR(sigaddr);
121d561028dSMike Smith 	for (cv = (u_int8_t *)sbt, ck = 0, i = 0; i < sbt->len; i++) {
122d561028dSMike Smith 	    ck += cv[i];
123d561028dSMike Smith 	}
124d561028dSMike Smith 	/* if checksum is OK, we have action */
125d561028dSMike Smith 	if (ck == 0) {
126d561028dSMike Smith 	    SMBIOStable = sbt;		/* save reference */
127d561028dSMike Smith 	    if (bootverbose) {
128cb5f885bSMike Smith 		printf("smbios: SMBIOS header at %p\n", sbt);
129cb5f885bSMike Smith 		printf("smbios: Version %d.%d\n", sbt->major, sbt->minor);
130cb5f885bSMike Smith 		printf("smbios: Table at 0x%x, %d entries, %d bytes, largest entry %d bytes\n",
1319deacac3SMike Smith 		       sbt->dmi.st_base, (int)sbt->dmi.st_entries, (int)sbt->dmi.st_size,
1322a1b9e9bSPeter Wemm 		       (int)sbt->st_maxsize);
133d561028dSMike Smith 	    }
134d561028dSMike Smith 	} else {
135cb5f885bSMike Smith 	    printf("smbios: Bad SMBIOS table checksum\n");
136d561028dSMike Smith 	}
137d561028dSMike Smith 
138d561028dSMike Smith     }
13976cf257bSPoul-Henning Kamp 
140cb5f885bSMike Smith     /*
141cb5f885bSMike Smith      * PnP BIOS
142cb5f885bSMike Smith      */
143cb5f885bSMike Smith     if ((sigaddr = bios_sigsearch(0, "$PnP", 4, 16, 0)) != 0) {
144d561028dSMike Smith 
145d561028dSMike Smith 	/* get a virtual pointer to the structure */
146cb5f885bSMike Smith 	pt = (struct PnPBIOS_table *)(uintptr_t)BIOS_PADDRTOVADDR(sigaddr);
147cb5f885bSMike Smith 	for (cv = (u_int8_t *)pt, ck = 0, i = 0; i < pt->len; i++) {
148d561028dSMike Smith 	    ck += cv[i];
149d561028dSMike Smith 	}
150cb5f885bSMike Smith 	/* If checksum is OK, enable use of the entrypoint */
151d561028dSMike Smith 	if (ck == 0) {
152cb5f885bSMike Smith 	    PnPBIOStable = pt;
153d561028dSMike Smith 	    if (bootverbose) {
154cb5f885bSMike Smith 		printf("pnpbios: Found PnP BIOS data at %p\n", pt);
155cb5f885bSMike Smith 		printf("pnpbios: Entry = %x:%x  Rev = %d.%d\n",
156cb5f885bSMike Smith 		       pt->pmentrybase, pt->pmentryoffset, pt->version >> 4, pt->version & 0xf);
157cb5f885bSMike Smith 		if ((pt->control & 0x3) == 0x01)
158cb5f885bSMike Smith 		    printf("pnpbios: Event flag at %x\n", pt->evflagaddr);
159cb5f885bSMike Smith 		if (pt->oemdevid != 0)
160cb5f885bSMike Smith 		    printf("pnpbios: OEM ID %x\n", pt->oemdevid);
161cb5f885bSMike Smith 
162d561028dSMike Smith 	    }
163cb5f885bSMike Smith 	    pnpbios_scan();
164d561028dSMike Smith 	} else {
165cb5f885bSMike Smith 	    printf("pnpbios: Bad PnP BIOS data checksum\n");
166d561028dSMike Smith 	}
167d561028dSMike Smith     }
168cb5f885bSMike Smith 
16976cf257bSPoul-Henning Kamp     if (bootverbose) {
17076cf257bSPoul-Henning Kamp 	    /* look for other know signatures */
17176cf257bSPoul-Henning Kamp 	    printf("Other BIOS signatures found:\n");
172446a1069SMike Smith 	    printf("ACPI: %08x\n", bios_sigsearch(0, "RST PTR", 8, 16, 0));
17376cf257bSPoul-Henning Kamp     }
174d561028dSMike Smith }
175d561028dSMike Smith 
176d561028dSMike Smith /*
177d561028dSMike Smith  * bios32_SDlookup
178d561028dSMike Smith  *
179d561028dSMike Smith  * Query the BIOS32 Service Directory for the service named in (ent),
180d561028dSMike Smith  * returns nonzero if the lookup fails.  The caller must fill in
181d561028dSMike Smith  * (ent->ident), the remainder are populated on a successful lookup.
182d561028dSMike Smith  */
183d561028dSMike Smith int
184d561028dSMike Smith bios32_SDlookup(struct bios32_SDentry *ent)
185d561028dSMike Smith {
186496027bfSMike Smith     struct bios_regs args;
187d561028dSMike Smith 
188496027bfSMike Smith     if (bios32_SDCI == 0)
189496027bfSMike Smith 	return (1);
190d561028dSMike Smith 
191d561028dSMike Smith     args.eax = ent->ident.id;		/* set up arguments */
192d561028dSMike Smith     args.ebx = args.ecx = args.edx = 0;
193496027bfSMike Smith     bios32(&args, bios32_SDCI, GSEL(GCODE_SEL, SEL_KPL));
194d561028dSMike Smith     if ((args.eax & 0xff) == 0) {	/* success? */
195d561028dSMike Smith 	ent->base = args.ebx;
196d561028dSMike Smith 	ent->len = args.ecx;
197d561028dSMike Smith 	ent->entry = args.edx;
198d561028dSMike Smith 	return (0);			/* all OK */
199d561028dSMike Smith     }
200d561028dSMike Smith     return (1);				/* failed */
201d561028dSMike Smith }
202d561028dSMike Smith 
203496027bfSMike Smith 
204d561028dSMike Smith /*
205d561028dSMike Smith  * bios_sigsearch
206d561028dSMike Smith  *
207d561028dSMike Smith  * Search some or all of the BIOS region for a signature string.
208d561028dSMike Smith  *
209d561028dSMike Smith  * (start)	Optional offset returned from this function
210d561028dSMike Smith  *		(for searching for multiple matches), or NULL
211d561028dSMike Smith  *		to start the search from the base of the BIOS.
212d561028dSMike Smith  *		Note that this will be a _physical_ address in
213d561028dSMike Smith  *		the range 0xe0000 - 0xfffff.
214d561028dSMike Smith  * (sig)	is a pointer to the byte(s) of the signature.
215d561028dSMike Smith  * (siglen)	number of bytes in the signature.
216d561028dSMike Smith  * (paralen)	signature paragraph (alignment) size.
217d561028dSMike Smith  * (sigofs)	offset of the signature within the paragraph.
218d561028dSMike Smith  *
219d561028dSMike Smith  * Returns the _physical_ address of the found signature, 0 if the
220d561028dSMike Smith  * signature was not found.
221d561028dSMike Smith  */
222d561028dSMike Smith 
223d561028dSMike Smith u_int32_t
224d561028dSMike Smith bios_sigsearch(u_int32_t start, u_char *sig, int siglen, int paralen, int sigofs)
225d561028dSMike Smith {
226d561028dSMike Smith     u_char	*sp, *end;
227d561028dSMike Smith 
228d561028dSMike Smith     /* compute the starting address */
229d561028dSMike Smith     if ((start >= BIOS_START) && (start <= (BIOS_START + BIOS_SIZE))) {
230d561028dSMike Smith 	sp = (char *)BIOS_PADDRTOVADDR(start);
231d561028dSMike Smith     } else if (start == 0) {
232d561028dSMike Smith 	sp = (char *)BIOS_PADDRTOVADDR(BIOS_START);
233d561028dSMike Smith     } else {
234d561028dSMike Smith 	return 0;				/* bogus start address */
235d561028dSMike Smith     }
236d561028dSMike Smith 
237d561028dSMike Smith     /* compute the end address */
238d561028dSMike Smith     end = (u_char *)BIOS_PADDRTOVADDR(BIOS_START + BIOS_SIZE);
239d561028dSMike Smith 
240d561028dSMike Smith     /* loop searching */
241d561028dSMike Smith     while ((sp + sigofs + siglen) < end) {
242d561028dSMike Smith 
243d561028dSMike Smith 	/* compare here */
244c7a2b294SMike Smith 	if (!bcmp(sp + sigofs, sig, siglen)) {
245d561028dSMike Smith 	    /* convert back to physical address */
246d561028dSMike Smith 	    return((u_int32_t)BIOS_VADDRTOPADDR(sp));
247d561028dSMike Smith 	}
248d561028dSMike Smith 	sp += paralen;
249d561028dSMike Smith     }
250d561028dSMike Smith     return(0);
251d561028dSMike Smith }
252d561028dSMike Smith 
253496027bfSMike Smith /*
254496027bfSMike Smith  * do not staticize, used by bioscall.s
255496027bfSMike Smith  */
256496027bfSMike Smith union {
257496027bfSMike Smith     struct {
258496027bfSMike Smith 	u_short	offset;
259496027bfSMike Smith 	u_short	segment;
260496027bfSMike Smith     } vec16;
261496027bfSMike Smith     struct {
262496027bfSMike Smith 	u_int	offset;
263496027bfSMike Smith 	u_short	segment;
264496027bfSMike Smith     } vec32;
265496027bfSMike Smith } bioscall_vector;			/* bios jump vector */
266496027bfSMike Smith 
267496027bfSMike Smith void
268496027bfSMike Smith set_bios_selectors(struct bios_segments *seg, int flags)
269496027bfSMike Smith {
270496027bfSMike Smith     static u_int curgen = 1;
271496027bfSMike Smith     struct soft_segment_descriptor ssd = {
272496027bfSMike Smith 	0,			/* segment base address (overwritten) */
273496027bfSMike Smith 	0,			/* length (overwritten) */
274496027bfSMike Smith 	SDT_MEMERA,		/* segment type (overwritten) */
275496027bfSMike Smith 	0,			/* priority level */
276496027bfSMike Smith 	1,			/* descriptor present */
277496027bfSMike Smith 	0, 0,
278496027bfSMike Smith 	1,			/* descriptor size (overwritten) */
279496027bfSMike Smith 	0			/* granularity == byte units */
280496027bfSMike Smith     };
281496027bfSMike Smith 
282496027bfSMike Smith     if (seg->generation == curgen)
283496027bfSMike Smith 	return;
284496027bfSMike Smith     if (++curgen == 0)
285496027bfSMike Smith 	curgen = 1;
286496027bfSMike Smith     seg->generation = curgen;
287496027bfSMike Smith 
288496027bfSMike Smith     ssd.ssd_base = seg->code32.base;
289496027bfSMike Smith     ssd.ssd_limit = seg->code32.limit;
290496027bfSMike Smith     ssdtosd(&ssd, &gdt[GBIOSCODE32_SEL].sd);
291496027bfSMike Smith 
292496027bfSMike Smith     ssd.ssd_def32 = 0;
293496027bfSMike Smith     if (flags & BIOSCODE_FLAG) {
294496027bfSMike Smith 	ssd.ssd_base = seg->code16.base;
295496027bfSMike Smith 	ssd.ssd_limit = seg->code16.limit;
296496027bfSMike Smith 	ssdtosd(&ssd, &gdt[GBIOSCODE16_SEL].sd);
297496027bfSMike Smith     }
298496027bfSMike Smith 
299496027bfSMike Smith     ssd.ssd_type = SDT_MEMRWA;
300496027bfSMike Smith     if (flags & BIOSDATA_FLAG) {
301496027bfSMike Smith 	ssd.ssd_base = seg->data.base;
302496027bfSMike Smith 	ssd.ssd_limit = seg->data.limit;
303496027bfSMike Smith 	ssdtosd(&ssd, &gdt[GBIOSDATA_SEL].sd);
304496027bfSMike Smith     }
305496027bfSMike Smith 
306496027bfSMike Smith     if (flags & BIOSUTIL_FLAG) {
307496027bfSMike Smith 	ssd.ssd_base = seg->util.base;
308496027bfSMike Smith 	ssd.ssd_limit = seg->util.limit;
309496027bfSMike Smith 	ssdtosd(&ssd, &gdt[GBIOSUTIL_SEL].sd);
310496027bfSMike Smith     }
311496027bfSMike Smith 
312496027bfSMike Smith     if (flags & BIOSARGS_FLAG) {
313496027bfSMike Smith 	ssd.ssd_base = seg->args.base;
314496027bfSMike Smith 	ssd.ssd_limit = seg->args.limit;
315496027bfSMike Smith 	ssdtosd(&ssd, &gdt[GBIOSARGS_SEL].sd);
316496027bfSMike Smith     }
317496027bfSMike Smith }
318496027bfSMike Smith 
319496027bfSMike Smith /*
320496027bfSMike Smith  * for pointers, we don't know how much space is supposed to be allocated,
321496027bfSMike Smith  * so we assume a minimum size of 256 bytes.  If more than this is needed,
322496027bfSMike Smith  * then this can be revisited, such as adding a length specifier.
323496027bfSMike Smith  */
324496027bfSMike Smith #define	ASSUMED_ARGSIZE		256
325496027bfSMike Smith 
326496027bfSMike Smith extern int vm86pa;
327496027bfSMike Smith 
328496027bfSMike Smith /*
329496027bfSMike Smith  * this routine is really greedy with selectors, and uses 5:
330496027bfSMike Smith  *
331496027bfSMike Smith  * 32-bit code selector:	to return to kernel
332496027bfSMike Smith  * 16-bit code selector:	for running code
333496027bfSMike Smith  *        data selector:	for 16-bit data
334496027bfSMike Smith  *        util selector:	extra utility selector
335496027bfSMike Smith  *        args selector:	to handle pointers
336496027bfSMike Smith  *
337496027bfSMike Smith  * the util selector is set from the util16 entry in bios16_args, if a
338496027bfSMike Smith  * "U" specifier is seen.
339496027bfSMike Smith  *
340496027bfSMike Smith  * See <machine/pc/bios.h> for description of format specifiers
341496027bfSMike Smith  */
342496027bfSMike Smith int
343496027bfSMike Smith bios16(struct bios_args *args, char *fmt, ...)
344496027bfSMike Smith {
345496027bfSMike Smith     char	*p, *stack, *stack_top;
346496027bfSMike Smith     va_list 	ap;
347496027bfSMike Smith     int 	flags = BIOSCODE_FLAG | BIOSDATA_FLAG;
348496027bfSMike Smith     u_int 	i, arg_start, arg_end;
349496027bfSMike Smith     u_int 	*pte, *ptd;
350496027bfSMike Smith 
351496027bfSMike Smith     arg_start = 0xffffffff;
352496027bfSMike Smith     arg_end = 0;
353496027bfSMike Smith 
354496027bfSMike Smith     stack = (caddr_t)PAGE_SIZE;
355496027bfSMike Smith     va_start(ap, fmt);
356496027bfSMike Smith     for (p = fmt; p && *p; p++) {
357496027bfSMike Smith 	switch (*p) {
358496027bfSMike Smith 	case 'p':			/* 32-bit pointer */
359496027bfSMike Smith 	    i = va_arg(ap, u_int);
360496027bfSMike Smith 	    arg_start = min(arg_start, i);
361496027bfSMike Smith 	    arg_end = max(arg_end, i + ASSUMED_ARGSIZE);
362496027bfSMike Smith 	    flags |= BIOSARGS_FLAG;
363496027bfSMike Smith 	    stack -= 4;
364496027bfSMike Smith 	    break;
365496027bfSMike Smith 
366496027bfSMike Smith 	case 'i':			/* 32-bit integer */
367496027bfSMike Smith 	    i = va_arg(ap, u_int);
368496027bfSMike Smith 	    stack -= 4;
369496027bfSMike Smith 	    break;
370496027bfSMike Smith 
371496027bfSMike Smith 	case 'U':			/* 16-bit selector */
372496027bfSMike Smith 	    flags |= BIOSUTIL_FLAG;
373496027bfSMike Smith 	    /* FALLTHROUGH */
374496027bfSMike Smith 	case 'D':			/* 16-bit selector */
375496027bfSMike Smith 	case 'C':			/* 16-bit selector */
376cb5f885bSMike Smith 	    stack -= 2;
377cb5f885bSMike Smith 	    break;
378cb5f885bSMike Smith 
379496027bfSMike Smith 	case 's':			/* 16-bit integer */
380496027bfSMike Smith 	    i = va_arg(ap, u_short);
381496027bfSMike Smith 	    stack -= 2;
382496027bfSMike Smith 	    break;
383496027bfSMike Smith 
384496027bfSMike Smith 	default:
385496027bfSMike Smith 	    return (EINVAL);
386496027bfSMike Smith 	}
387496027bfSMike Smith     }
388496027bfSMike Smith 
389496027bfSMike Smith     if (flags & BIOSARGS_FLAG) {
390496027bfSMike Smith 	if (arg_end - arg_start > ctob(16))
391496027bfSMike Smith 	    return (EACCES);
392496027bfSMike Smith 	args->seg.args.base = arg_start;
393496027bfSMike Smith 	args->seg.args.limit = arg_end - arg_start;
394496027bfSMike Smith     }
395496027bfSMike Smith 
396496027bfSMike Smith     args->seg.code32.base = (u_int)&bios16_call & PG_FRAME;
397496027bfSMike Smith     args->seg.code32.limit = 0xffff;
398496027bfSMike Smith 
399496027bfSMike Smith     ptd = (u_int *)rcr3();
4003cc31bc2SMike Smith     if (ptd == IdlePTD) {
401496027bfSMike Smith 	/*
402496027bfSMike Smith 	 * no page table, so create one and install it.
403496027bfSMike Smith 	 */
404496027bfSMike Smith 	pte = (u_int *)malloc(PAGE_SIZE, M_TEMP, M_WAITOK);
405496027bfSMike Smith 	ptd = (u_int *)((u_int)ptd + KERNBASE);
406496027bfSMike Smith 	*ptd = vtophys(pte) | PG_RW | PG_V;
407496027bfSMike Smith     } else {
408496027bfSMike Smith 	/*
409496027bfSMike Smith 	 * this is a user-level page table
410496027bfSMike Smith 	 */
411496027bfSMike Smith 	pte = (u_int *)&PTmap;
412496027bfSMike Smith     }
413496027bfSMike Smith     /*
414496027bfSMike Smith      * install pointer to page 0.  we don't need to flush the tlb,
415496027bfSMike Smith      * since there should not be a previous mapping for page 0.
416496027bfSMike Smith      */
417496027bfSMike Smith     *pte = (vm86pa - PAGE_SIZE) | PG_RW | PG_V;
418496027bfSMike Smith 
419496027bfSMike Smith     stack_top = stack;
420496027bfSMike Smith     va_start(ap, fmt);
421496027bfSMike Smith     for (p = fmt; p && *p; p++) {
422496027bfSMike Smith 	switch (*p) {
423496027bfSMike Smith 	case 'p':			/* 32-bit pointer */
424496027bfSMike Smith 	    i = va_arg(ap, u_int);
425496027bfSMike Smith 	    *(u_int *)stack = (i - arg_start) |
426496027bfSMike Smith 		(GSEL(GBIOSARGS_SEL, SEL_KPL) << 16);
427496027bfSMike Smith 	    stack += 4;
428496027bfSMike Smith 	    break;
429496027bfSMike Smith 
430496027bfSMike Smith 	case 'i':			/* 32-bit integer */
431496027bfSMike Smith 	    i = va_arg(ap, u_int);
432496027bfSMike Smith 	    *(u_int *)stack = i;
433496027bfSMike Smith 	    stack += 4;
434496027bfSMike Smith 	    break;
435496027bfSMike Smith 
436496027bfSMike Smith 	case 'U':			/* 16-bit selector */
437496027bfSMike Smith 	    *(u_short *)stack = GSEL(GBIOSUTIL_SEL, SEL_KPL);
438496027bfSMike Smith 	    stack += 2;
439496027bfSMike Smith 	    break;
440496027bfSMike Smith 
441496027bfSMike Smith 	case 'D':			/* 16-bit selector */
442496027bfSMike Smith 	    *(u_short *)stack = GSEL(GBIOSDATA_SEL, SEL_KPL);
443496027bfSMike Smith 	    stack += 2;
444496027bfSMike Smith 	    break;
445496027bfSMike Smith 
446496027bfSMike Smith 	case 'C':			/* 16-bit selector */
447496027bfSMike Smith 	    *(u_short *)stack = GSEL(GBIOSCODE16_SEL, SEL_KPL);
448496027bfSMike Smith 	    stack += 2;
449496027bfSMike Smith 	    break;
450496027bfSMike Smith 
451496027bfSMike Smith 	case 's':			/* 16-bit integer */
452496027bfSMike Smith 	    i = va_arg(ap, u_short);
453496027bfSMike Smith 	    *(u_short *)stack = i;
454496027bfSMike Smith 	    stack += 2;
455496027bfSMike Smith 	    break;
456496027bfSMike Smith 
457496027bfSMike Smith 	default:
458496027bfSMike Smith 	    return (EINVAL);
459496027bfSMike Smith 	}
460496027bfSMike Smith     }
461496027bfSMike Smith 
462496027bfSMike Smith     args->seg.generation = 0;			/* reload selectors */
463496027bfSMike Smith     set_bios_selectors(&args->seg, flags);
464496027bfSMike Smith     bioscall_vector.vec16.offset = (u_short)args->entry;
465496027bfSMike Smith     bioscall_vector.vec16.segment = GSEL(GBIOSCODE16_SEL, SEL_KPL);
466496027bfSMike Smith 
467496027bfSMike Smith     i = bios16_call(&args->r, stack_top);
468496027bfSMike Smith 
469496027bfSMike Smith     if (pte == (u_int *)&PTmap) {
470496027bfSMike Smith 	*pte = 0;			/* remove entry */
471496027bfSMike Smith     } else {
472496027bfSMike Smith 	*ptd = 0;			/* remove page table */
473496027bfSMike Smith 	free(pte, M_TEMP);		/* ... and free it */
474496027bfSMike Smith     }
475d561028dSMike Smith 
476496027bfSMike Smith     /*
477496027bfSMike Smith      * XXX only needs to be invlpg(0) but that doesn't work on the 386
478496027bfSMike Smith      */
479496027bfSMike Smith     invltlb();
480496027bfSMike Smith 
481496027bfSMike Smith     return (i);
482496027bfSMike Smith }
483cb5f885bSMike Smith 
484cb5f885bSMike Smith /*
485cb5f885bSMike Smith  * PnP BIOS interface; enumerate devices only known to the system
486cb5f885bSMike Smith  * BIOS and save information about them for later use.
487cb5f885bSMike Smith  */
488cb5f885bSMike Smith 
489cb5f885bSMike Smith struct pnp_sysdev
490cb5f885bSMike Smith {
491cb5f885bSMike Smith     u_int16_t	size;
492cb5f885bSMike Smith     u_int8_t	handle;
493cb5f885bSMike Smith     u_int32_t	devid;
494cb5f885bSMike Smith     u_int8_t	type[3];
495cb5f885bSMike Smith     u_int16_t	attrib;
496cb5f885bSMike Smith #define PNPATTR_NODISABLE	(1<<0)	/* can't be disabled */
497cb5f885bSMike Smith #define PNPATTR_NOCONFIG	(1<<1)	/* can't be configured */
498cb5f885bSMike Smith #define PNPATTR_OUTPUT		(1<<2)	/* can be primary output */
499cb5f885bSMike Smith #define PNPATTR_INPUT		(1<<3)	/* can be primary input */
500cb5f885bSMike Smith #define PNPATTR_BOOTABLE	(1<<4)	/* can be booted from */
501cb5f885bSMike Smith #define PNPATTR_DOCK		(1<<5)	/* is a docking station */
502cb5f885bSMike Smith #define PNPATTR_REMOVEABLE	(1<<6)	/* device is removeable */
503cb5f885bSMike Smith #define PNPATTR_CONFIG_STATIC	0x00
504cb5f885bSMike Smith #define PNPATTR_CONFIG_DYNAMIC	0x07
505cb5f885bSMike Smith #define PNPATTR_CONFIG_DYNONLY	0x17
506cb5f885bSMike Smith     /* device-specific data comes here */
507cb5f885bSMike Smith     u_int8_t	devdata[0];
508cb5f885bSMike Smith } __attribute__ ((packed));
509cb5f885bSMike Smith 
510cb5f885bSMike Smith /* We have to cluster arguments within a 64k range for the bios16 call */
511cb5f885bSMike Smith struct pnp_sysdevargs
512cb5f885bSMike Smith {
513cb5f885bSMike Smith     u_int16_t	next;
514cb5f885bSMike Smith     struct pnp_sysdev node;
515cb5f885bSMike Smith };
516cb5f885bSMike Smith 
517cb5f885bSMike Smith /*
518cb5f885bSMike Smith  * Quiz the PnP BIOS, build a list of PNP IDs and resource data.
519cb5f885bSMike Smith  */
520cb5f885bSMike Smith static void
521cb5f885bSMike Smith pnpbios_scan(void)
522cb5f885bSMike Smith {
523cb5f885bSMike Smith     struct PnPBIOS_table	*pt = PnPBIOStable;
524cb5f885bSMike Smith     struct bios_args		args;
525cb5f885bSMike Smith     struct pnp_sysdev		*pd;
526cb5f885bSMike Smith     struct pnp_sysdevargs	*pda;
527cb5f885bSMike Smith     u_int16_t			ndevs, bigdev;
528cb5f885bSMike Smith     int				error, currdev;
529cb5f885bSMike Smith     u_int8_t			*devnodebuf, tag;
530cb5f885bSMike Smith     u_int32_t			*devid, *compid;
531cb5f885bSMike Smith     int				idx, left;
532cb5f885bSMike Smith 
533cb5f885bSMike Smith     /* no PnP BIOS information */
534cb5f885bSMike Smith     if (pt == NULL)
535cb5f885bSMike Smith 	return;
536cb5f885bSMike Smith 
537cb5f885bSMike Smith     bzero(&args, sizeof(args));
538cb5f885bSMike Smith     args.seg.code16.base = BIOS_PADDRTOVADDR(pt->pmentrybase);
539cb5f885bSMike Smith     args.seg.code16.limit = 0xffff;		/* XXX ? */
540cb5f885bSMike Smith     args.seg.data.base = BIOS_PADDRTOVADDR(pt->pmdataseg);
541cb5f885bSMike Smith     args.seg.data.limit = 0xffff;
542cb5f885bSMike Smith     args.entry = pt->pmentryoffset;
543cb5f885bSMike Smith 
544cb5f885bSMike Smith     if ((error = bios16(&args, PNP_COUNT_DEVNODES, &ndevs, &bigdev)) || (args.r.eax & 0xff))
545cb5f885bSMike Smith 	printf("pnpbios: error %d/%x getting device count/size limit\n", error, args.r.eax);
546cb5f885bSMike Smith     if (bootverbose)
547cb5f885bSMike Smith 	printf("pnpbios: %d devices, largest %d bytes\n", ndevs, bigdev);
548cb5f885bSMike Smith 
549cb5f885bSMike Smith     devnodebuf = malloc(bigdev + (sizeof(struct pnp_sysdevargs) - sizeof(struct pnp_sysdev)),
550cb5f885bSMike Smith 			M_DEVBUF, M_NOWAIT);
551cb5f885bSMike Smith     pda = (struct pnp_sysdevargs *)devnodebuf;
552cb5f885bSMike Smith     pd = &pda->node;
553cb5f885bSMike Smith 
554cb5f885bSMike Smith     for (currdev = 0, left = ndevs; (currdev != 0xff) && (left > 0); left--) {
555cb5f885bSMike Smith 
556cb5f885bSMike Smith 	bzero(pd, bigdev);
557cb5f885bSMike Smith 	pda->next = currdev;
558cb5f885bSMike Smith 	/* get current configuration */
559cb5f885bSMike Smith 	if ((error = bios16(&args, PNP_GET_DEVNODE, &pda->next, &pda->node, (u_int16_t)1))) {
560cb5f885bSMike Smith 	    printf("pnpbios: error %d making BIOS16 call\n", error);
561cb5f885bSMike Smith 	    break;
562cb5f885bSMike Smith 	}
563cb5f885bSMike Smith 	if ((error = (args.r.eax & 0xff))) {
564cb5f885bSMike Smith 	    if (bootverbose)
565cb5f885bSMike Smith 		printf("pnpbios: %s 0x%x fetching node %d\n", error & 0x80 ? "error" : "warning", error, currdev);
566cb5f885bSMike Smith 	    if (error & 0x80)
567cb5f885bSMike Smith 		break;
568cb5f885bSMike Smith 	}
569cb5f885bSMike Smith 	currdev = pda->next;
570cb5f885bSMike Smith 	if (pd->size < sizeof(struct pnp_sysdev)) {
571cb5f885bSMike Smith 	    printf("pnpbios: bogus system node data, aborting scan\n");
572cb5f885bSMike Smith 	    break;
573cb5f885bSMike Smith 	}
574cb5f885bSMike Smith 
575cb5f885bSMike Smith 	/* Find device IDs */
576cb5f885bSMike Smith 	devid = &pd->devid;
577cb5f885bSMike Smith 	compid = NULL;
578cb5f885bSMike Smith 
579cb5f885bSMike Smith 	/* look for a compatible device ID too */
580cb5f885bSMike Smith 	left = pd->size - sizeof(struct pnp_sysdev);
581cb5f885bSMike Smith 	idx = 0;
582cb5f885bSMike Smith 	while (idx < left) {
583cb5f885bSMike Smith 	    tag = pd->devdata[idx++];
584cb5f885bSMike Smith 	    if (PNP_RES_TYPE(tag) == 0) {
585cb5f885bSMike Smith 		/* Small resource */
586cb5f885bSMike Smith 		switch (PNP_SRES_NUM(tag)) {
587cb5f885bSMike Smith 		case COMP_DEVICE_ID:
588cb5f885bSMike Smith 		    compid = (u_int32_t *)(pd->devdata + idx);
589cb5f885bSMike Smith 		    if (bootverbose)
590cb5f885bSMike Smith 			printf("pnpbios: node %d compat ID 0x%08x\n", pd->handle, *compid);
591cb5f885bSMike Smith 		    /* FALLTHROUGH */
592cb5f885bSMike Smith 		case END_TAG:
593cb5f885bSMike Smith 		    idx = left;
594cb5f885bSMike Smith 		    break;
595cb5f885bSMike Smith 		default:
596cb5f885bSMike Smith 		    idx += PNP_SRES_LEN(tag);
597cb5f885bSMike Smith 		    break;
598cb5f885bSMike Smith 		}
599cb5f885bSMike Smith 	    } else
600cb5f885bSMike Smith 		/* Large resource, skip it */
601cb5f885bSMike Smith 		idx += *(u_int16_t *)(pd->devdata + idx) + 2;
602cb5f885bSMike Smith 	}
603cb5f885bSMike Smith 	if (bootverbose) {
604cb5f885bSMike Smith 	    printf("pnpbios: handle %d device ID %s (%08x)",
605cb5f885bSMike Smith 		   pd->handle, pnp_eisaformat((u_int8_t *)devid), *devid);
606cb5f885bSMike Smith 	    if (compid != NULL)
607cb5f885bSMike Smith 		printf(" compat ID %s (%08x)",
608cb5f885bSMike Smith 		       pnp_eisaformat((u_int8_t *)compid), *compid);
609cb5f885bSMike Smith 	    printf("\n");
610cb5f885bSMike Smith 	}
611cb5f885bSMike Smith     }
612cb5f885bSMike Smith }
613cb5f885bSMike Smith 
614cb5f885bSMike Smith /* XXX should be somewhere else */
615cb5f885bSMike Smith static char *
616cb5f885bSMike Smith pnp_eisaformat(u_int8_t *data)
617cb5f885bSMike Smith {
618cb5f885bSMike Smith     static char idbuf[8];
619cb5f885bSMike Smith     const char  hextoascii[] = "0123456789abcdef";
620cb5f885bSMike Smith 
621cb5f885bSMike Smith     idbuf[0] = '@' + ((data[0] & 0x7c) >> 2);
622cb5f885bSMike Smith     idbuf[1] = '@' + (((data[0] & 0x3) << 3) + ((data[1] & 0xe0) >> 5));
623cb5f885bSMike Smith     idbuf[2] = '@' + (data[1] & 0x1f);
624cb5f885bSMike Smith     idbuf[3] = hextoascii[(data[2] >> 4)];
625cb5f885bSMike Smith     idbuf[4] = hextoascii[(data[2] & 0xf)];
626cb5f885bSMike Smith     idbuf[5] = hextoascii[(data[3] >> 4)];
627cb5f885bSMike Smith     idbuf[6] = hextoascii[(data[3] & 0xf)];
628cb5f885bSMike Smith     idbuf[7] = 0;
629cb5f885bSMike Smith     return(idbuf);
630cb5f885bSMike Smith }
631