1/*-
2 * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * $FreeBSD: head/sys/boot/i386/libi386/biospnp.c 103870 2002-09-23 18:54:32Z alfred $
27 */
28
29/*
30 * PnP BIOS enumerator.
31 */
32
33#include <stand.h>
34#include <machine/stdarg.h>
35#include <bootstrap.h>
36#include <isapnp.h>
37#include <btxv86.h>
38
39
40static int biospnp_init(void);
41static void biospnp_enumerate(void);
42
43struct pnphandler biospnphandler =
44{
45 "PnP BIOS",
46 biospnp_enumerate
47};
48
49struct pnp_ICstructure
50{
51 u_int8_t pnp_signature[4] __packed;
52 u_int8_t pnp_version __packed;
53 u_int8_t pnp_length __packed;
54 u_int16_t pnp_BIOScontrol __packed;
55 u_int8_t pnp_checksum __packed;
56 u_int32_t pnp_eventflag __packed;
57 u_int16_t pnp_rmip __packed;
58 u_int16_t pnp_rmcs __packed;
59 u_int16_t pnp_pmip __packed;
60 u_int32_t pnp_pmcs __packed;
61 u_int8_t pnp_OEMdev[4] __packed;
62 u_int16_t pnp_rmds __packed;
63 u_int32_t pnp_pmds __packed;
64};
65
66struct pnp_devNode
67{
68 u_int16_t dn_size __packed;
69 u_int8_t dn_handle __packed;
70 u_int8_t dn_id[4] __packed;
71 u_int8_t dn_type[3] __packed;
72 u_int16_t dn_attrib __packed;
73 u_int8_t dn_data[1] __packed;
74};
75
76struct pnp_isaConfiguration
77{
78 u_int8_t ic_revision __packed;
79 u_int8_t ic_nCSN __packed;
80 u_int16_t ic_rdport __packed;
81 u_int16_t ic_reserved __packed;
82};
83
84static struct pnp_ICstructure *pnp_Icheck = NULL;
85static u_int16_t pnp_NumNodes;
86static u_int16_t pnp_NodeSize;
87
88static void biospnp_scanresdata(struct pnpinfo *pi, struct pnp_devNode *dn);
89static int biospnp_call(int func, const char *fmt, ...);
90
91#define vsegofs(vptr) (((u_int32_t)VTOPSEG(vptr) << 16) + VTOPOFF(vptr))
92
93typedef void v86bios_t(u_int32_t, u_int32_t, u_int32_t, u_int32_t);
94v86bios_t *v86bios = (v86bios_t *)v86int;
95
96#define biospnp_f00(NumNodes, NodeSize) biospnp_call(0x00, "ll", NumNodes, NodeSize)
97#define biospnp_f01(Node, devNodeBuffer, Control) biospnp_call(0x01, "llw", Node, devNodeBuffer, Control)
98#define biospnp_f40(Configuration) biospnp_call(0x40, "l", Configuration)
99
100/* PnP BIOS return codes */
101#define PNP_SUCCESS 0x00
102#define PNP_FUNCTION_NOT_SUPPORTED 0x80
103
104/*
105 * Initialisation: locate the PnP BIOS, test that we can call it.
106 * Returns nonzero if the PnP BIOS is not usable on this system.
107 */
108static int
109biospnp_init(void)
110{
111 struct pnp_isaConfiguration icfg;
112 char *sigptr;
113 int result;
114
115 /* Search for the $PnP signature */
116 pnp_Icheck = NULL;
117 for (sigptr = PTOV(0xf0000); sigptr < PTOV(0xfffff); sigptr += 16)
118 if (!bcmp(sigptr, "$PnP", 4)) {
119 pnp_Icheck = (struct pnp_ICstructure *)sigptr;
120 break;
121 }
122
123 /* No signature, no BIOS */
124 if (pnp_Icheck == NULL)
125 return(1);
126
127 /*
128 * Fetch the system table parameters as a test of the BIOS
129 */
130 result = biospnp_f00(vsegofs(&pnp_NumNodes), vsegofs(&pnp_NodeSize));
131 if (result != PNP_SUCCESS) {
132 return(1);
133 }
134
135 /*
136 * Look for the PnP ISA configuration table
137 */
138 result = biospnp_f40(vsegofs(&icfg));
139 switch (result) {
140 case PNP_SUCCESS:
141 /* If the BIOS found some PnP devices, take its hint for the read port */
142 if ((icfg.ic_revision == 1) && (icfg.ic_nCSN > 0))
143 isapnp_readport = icfg.ic_rdport;
144 break;
145 case PNP_FUNCTION_NOT_SUPPORTED:
146 /* The BIOS says there is no ISA bus (should we trust that this works?) */
147 printf("PnP BIOS claims no ISA bus\n");
148 isapnp_readport = -1;
149 break;
150 }
151 return(0);
152}
153
154static void
155biospnp_enumerate(void)
156{
157 u_int8_t Node;
158 struct pnp_devNode *devNodeBuffer;
159 int result;
160 struct pnpinfo *pi;
161 int count;
162
163 /* Init/check state */
164 if (biospnp_init())
165 return;
166
167 devNodeBuffer = (struct pnp_devNode *)malloc(pnp_NodeSize);
168 Node = 0;
169 count = 1000;
170 while((Node != 0xff) && (count-- > 0)) {
171 result = biospnp_f01(vsegofs(&Node), vsegofs(devNodeBuffer), 0x1);
172 if (result != PNP_SUCCESS) {
173 printf("PnP BIOS node %d: error 0x%x\n", Node, result);
174 } else {
175 pi = pnp_allocinfo();
176 pnp_addident(pi, pnp_eisaformat(devNodeBuffer->dn_id));
177 biospnp_scanresdata(pi, devNodeBuffer);
178 pnp_addinfo(pi);
179 }
180 }
181}
182
183/*
184 * Scan the resource data in the node's data area for compatible device IDs
185 * and descriptions.
186 */
187static void
188biospnp_scanresdata(struct pnpinfo *pi, struct pnp_devNode *dn)
189{
190 u_int tag, i, rlen, dlen;
191 u_int8_t *p;
192 char *str;
193
194 p = dn->dn_data; /* point to resource data */
195 dlen = dn->dn_size - (p - (u_int8_t *)dn); /* length of resource data */
196
197 for (i = 0; i < dlen; i+= rlen) {
198 tag = p[i];
199 i++;
200 if (PNP_RES_TYPE(tag) == 0) {
201 rlen = PNP_SRES_LEN(tag);
202 /* small resource */
203 switch (PNP_SRES_NUM(tag)) {
204
205 case COMP_DEVICE_ID:
206 /* got a compatible device ID */
207 pnp_addident(pi, pnp_eisaformat(p + i));
208 break;
209
210 case END_TAG:
211 return;
212 }
213 } else {
214 /* large resource */
215 rlen = *(u_int16_t *)(p + i);
216 i += sizeof(u_int16_t);
217
218 switch(PNP_LRES_NUM(tag)) {
219
220 case ID_STRING_ANSI:
221 str = malloc(rlen + 1);
222 bcopy(p + i, str, rlen);
223 str[rlen] = 0;
224 if (pi->pi_desc == NULL) {
225 pi->pi_desc = str;
226 } else {
227 free(str);
228 }
229 break;
230 }
231 }
232 }
233}
234
235
236/*
237 * Make a 16-bit realmode PnP BIOS call.
238 *
239 * The first argument passed is the function number, the last is the
240 * BIOS data segment selector. Intermediate arguments may be 16 or
241 * 32 bytes in length, and are described by the format string.
242 *
243 * Arguments to the BIOS functions must be packed on the stack, hence
244 * this evil.
245 */
246static int
247biospnp_call(int func, const char *fmt, ...)
248{
249 va_list ap;
250 const char *p;
251 u_int8_t *argp;
252 u_int32_t args[4];
253 u_int32_t i;
254
255 /* function number first */
256 argp = (u_int8_t *)args;
257 *(u_int16_t *)argp = func;
258 argp += sizeof(u_int16_t);
259
260 /* take args according to format */
261 va_start(ap, fmt);
262 for (p = fmt; *p != 0; p++) {
263 switch(*p) {
264
265 case 'w':
266 i = va_arg(ap, uint);
267 *(u_int16_t *)argp = i;
268 argp += sizeof(u_int16_t);
269 break;
270
271 case 'l':
272 i = va_arg(ap, u_int32_t);
273 *(u_int32_t *)argp = i;
274 argp += sizeof(u_int32_t);
275 break;
276 }
277 }
278
279 /* BIOS segment last */
280 *(u_int16_t *)argp = pnp_Icheck->pnp_rmds;
281 argp += sizeof(u_int16_t);
282
283 /* prepare for call */
284 v86.ctl = V86_ADDR | V86_CALLF;
285 v86.addr = ((u_int32_t)pnp_Icheck->pnp_rmcs << 16) + pnp_Icheck->pnp_rmip;
286
287 /* call with packed stack and return */
288 v86bios(args[0], args[1], args[2], args[3]);
289 return(v86.eax & 0xffff);
290}
2 * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * $FreeBSD: head/sys/boot/i386/libi386/biospnp.c 103870 2002-09-23 18:54:32Z alfred $
27 */
28
29/*
30 * PnP BIOS enumerator.
31 */
32
33#include <stand.h>
34#include <machine/stdarg.h>
35#include <bootstrap.h>
36#include <isapnp.h>
37#include <btxv86.h>
38
39
40static int biospnp_init(void);
41static void biospnp_enumerate(void);
42
43struct pnphandler biospnphandler =
44{
45 "PnP BIOS",
46 biospnp_enumerate
47};
48
49struct pnp_ICstructure
50{
51 u_int8_t pnp_signature[4] __packed;
52 u_int8_t pnp_version __packed;
53 u_int8_t pnp_length __packed;
54 u_int16_t pnp_BIOScontrol __packed;
55 u_int8_t pnp_checksum __packed;
56 u_int32_t pnp_eventflag __packed;
57 u_int16_t pnp_rmip __packed;
58 u_int16_t pnp_rmcs __packed;
59 u_int16_t pnp_pmip __packed;
60 u_int32_t pnp_pmcs __packed;
61 u_int8_t pnp_OEMdev[4] __packed;
62 u_int16_t pnp_rmds __packed;
63 u_int32_t pnp_pmds __packed;
64};
65
66struct pnp_devNode
67{
68 u_int16_t dn_size __packed;
69 u_int8_t dn_handle __packed;
70 u_int8_t dn_id[4] __packed;
71 u_int8_t dn_type[3] __packed;
72 u_int16_t dn_attrib __packed;
73 u_int8_t dn_data[1] __packed;
74};
75
76struct pnp_isaConfiguration
77{
78 u_int8_t ic_revision __packed;
79 u_int8_t ic_nCSN __packed;
80 u_int16_t ic_rdport __packed;
81 u_int16_t ic_reserved __packed;
82};
83
84static struct pnp_ICstructure *pnp_Icheck = NULL;
85static u_int16_t pnp_NumNodes;
86static u_int16_t pnp_NodeSize;
87
88static void biospnp_scanresdata(struct pnpinfo *pi, struct pnp_devNode *dn);
89static int biospnp_call(int func, const char *fmt, ...);
90
91#define vsegofs(vptr) (((u_int32_t)VTOPSEG(vptr) << 16) + VTOPOFF(vptr))
92
93typedef void v86bios_t(u_int32_t, u_int32_t, u_int32_t, u_int32_t);
94v86bios_t *v86bios = (v86bios_t *)v86int;
95
96#define biospnp_f00(NumNodes, NodeSize) biospnp_call(0x00, "ll", NumNodes, NodeSize)
97#define biospnp_f01(Node, devNodeBuffer, Control) biospnp_call(0x01, "llw", Node, devNodeBuffer, Control)
98#define biospnp_f40(Configuration) biospnp_call(0x40, "l", Configuration)
99
100/* PnP BIOS return codes */
101#define PNP_SUCCESS 0x00
102#define PNP_FUNCTION_NOT_SUPPORTED 0x80
103
104/*
105 * Initialisation: locate the PnP BIOS, test that we can call it.
106 * Returns nonzero if the PnP BIOS is not usable on this system.
107 */
108static int
109biospnp_init(void)
110{
111 struct pnp_isaConfiguration icfg;
112 char *sigptr;
113 int result;
114
115 /* Search for the $PnP signature */
116 pnp_Icheck = NULL;
117 for (sigptr = PTOV(0xf0000); sigptr < PTOV(0xfffff); sigptr += 16)
118 if (!bcmp(sigptr, "$PnP", 4)) {
119 pnp_Icheck = (struct pnp_ICstructure *)sigptr;
120 break;
121 }
122
123 /* No signature, no BIOS */
124 if (pnp_Icheck == NULL)
125 return(1);
126
127 /*
128 * Fetch the system table parameters as a test of the BIOS
129 */
130 result = biospnp_f00(vsegofs(&pnp_NumNodes), vsegofs(&pnp_NodeSize));
131 if (result != PNP_SUCCESS) {
132 return(1);
133 }
134
135 /*
136 * Look for the PnP ISA configuration table
137 */
138 result = biospnp_f40(vsegofs(&icfg));
139 switch (result) {
140 case PNP_SUCCESS:
141 /* If the BIOS found some PnP devices, take its hint for the read port */
142 if ((icfg.ic_revision == 1) && (icfg.ic_nCSN > 0))
143 isapnp_readport = icfg.ic_rdport;
144 break;
145 case PNP_FUNCTION_NOT_SUPPORTED:
146 /* The BIOS says there is no ISA bus (should we trust that this works?) */
147 printf("PnP BIOS claims no ISA bus\n");
148 isapnp_readport = -1;
149 break;
150 }
151 return(0);
152}
153
154static void
155biospnp_enumerate(void)
156{
157 u_int8_t Node;
158 struct pnp_devNode *devNodeBuffer;
159 int result;
160 struct pnpinfo *pi;
161 int count;
162
163 /* Init/check state */
164 if (biospnp_init())
165 return;
166
167 devNodeBuffer = (struct pnp_devNode *)malloc(pnp_NodeSize);
168 Node = 0;
169 count = 1000;
170 while((Node != 0xff) && (count-- > 0)) {
171 result = biospnp_f01(vsegofs(&Node), vsegofs(devNodeBuffer), 0x1);
172 if (result != PNP_SUCCESS) {
173 printf("PnP BIOS node %d: error 0x%x\n", Node, result);
174 } else {
175 pi = pnp_allocinfo();
176 pnp_addident(pi, pnp_eisaformat(devNodeBuffer->dn_id));
177 biospnp_scanresdata(pi, devNodeBuffer);
178 pnp_addinfo(pi);
179 }
180 }
181}
182
183/*
184 * Scan the resource data in the node's data area for compatible device IDs
185 * and descriptions.
186 */
187static void
188biospnp_scanresdata(struct pnpinfo *pi, struct pnp_devNode *dn)
189{
190 u_int tag, i, rlen, dlen;
191 u_int8_t *p;
192 char *str;
193
194 p = dn->dn_data; /* point to resource data */
195 dlen = dn->dn_size - (p - (u_int8_t *)dn); /* length of resource data */
196
197 for (i = 0; i < dlen; i+= rlen) {
198 tag = p[i];
199 i++;
200 if (PNP_RES_TYPE(tag) == 0) {
201 rlen = PNP_SRES_LEN(tag);
202 /* small resource */
203 switch (PNP_SRES_NUM(tag)) {
204
205 case COMP_DEVICE_ID:
206 /* got a compatible device ID */
207 pnp_addident(pi, pnp_eisaformat(p + i));
208 break;
209
210 case END_TAG:
211 return;
212 }
213 } else {
214 /* large resource */
215 rlen = *(u_int16_t *)(p + i);
216 i += sizeof(u_int16_t);
217
218 switch(PNP_LRES_NUM(tag)) {
219
220 case ID_STRING_ANSI:
221 str = malloc(rlen + 1);
222 bcopy(p + i, str, rlen);
223 str[rlen] = 0;
224 if (pi->pi_desc == NULL) {
225 pi->pi_desc = str;
226 } else {
227 free(str);
228 }
229 break;
230 }
231 }
232 }
233}
234
235
236/*
237 * Make a 16-bit realmode PnP BIOS call.
238 *
239 * The first argument passed is the function number, the last is the
240 * BIOS data segment selector. Intermediate arguments may be 16 or
241 * 32 bytes in length, and are described by the format string.
242 *
243 * Arguments to the BIOS functions must be packed on the stack, hence
244 * this evil.
245 */
246static int
247biospnp_call(int func, const char *fmt, ...)
248{
249 va_list ap;
250 const char *p;
251 u_int8_t *argp;
252 u_int32_t args[4];
253 u_int32_t i;
254
255 /* function number first */
256 argp = (u_int8_t *)args;
257 *(u_int16_t *)argp = func;
258 argp += sizeof(u_int16_t);
259
260 /* take args according to format */
261 va_start(ap, fmt);
262 for (p = fmt; *p != 0; p++) {
263 switch(*p) {
264
265 case 'w':
266 i = va_arg(ap, uint);
267 *(u_int16_t *)argp = i;
268 argp += sizeof(u_int16_t);
269 break;
270
271 case 'l':
272 i = va_arg(ap, u_int32_t);
273 *(u_int32_t *)argp = i;
274 argp += sizeof(u_int32_t);
275 break;
276 }
277 }
278
279 /* BIOS segment last */
280 *(u_int16_t *)argp = pnp_Icheck->pnp_rmds;
281 argp += sizeof(u_int16_t);
282
283 /* prepare for call */
284 v86.ctl = V86_ADDR | V86_CALLF;
285 v86.addr = ((u_int32_t)pnp_Icheck->pnp_rmcs << 16) + pnp_Icheck->pnp_rmip;
286
287 /* call with packed stack and return */
288 v86bios(args[0], args[1], args[2], args[3]);
289 return(v86.eax & 0xffff);
290}