xref: /freebsd/sys/i386/bios/smapi.c (revision 7660b554bc59a07be0431c17e0e33815818baa69)
1 /*-
2  * Copyright (c) 2003 Matthew N. Dodd <winter@jurai.net>
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 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
33 
34 #include <sys/module.h>
35 #include <sys/bus.h>
36 #include <sys/conf.h>
37 
38 #include <machine/bus.h>
39 #include <machine/resource.h>
40 #include <sys/rman.h>
41 
42 /* And all this for BIOS_PADDRTOVADDR() */
43 #include <vm/vm.h>
44 #include <vm/pmap.h>
45 #include <machine/md_var.h>
46 #include <machine/pc/bios.h>
47 
48 #include <machine/smapi.h>
49 
50 #define	SMAPI_START	0xf0000
51 #define	SMAPI_STEP	0x10
52 #define	SMAPI_OFF	0
53 #define	SMAPI_LEN	4
54 #define	SMAPI_SIG	"$SMB"
55 
56 #define	RES2HDR(res)	((struct smapi_bios_header *)rman_get_virtual(res))
57 #define	ADDR2HDR(addr)	((struct smapi_bios_header *)BIOS_PADDRTOVADDR(addr))
58 
59 struct smapi_softc {
60 	dev_t			cdev;
61 	device_t		dev;
62 	struct resource *	res;
63 	int			rid;
64 
65 	u_int32_t		smapi32_entry;
66 
67 	struct smapi_bios_header *header;
68 };
69 
70 extern u_long smapi32_offset;
71 extern u_short smapi32_segment;
72 
73 devclass_t smapi_devclass;
74 
75 static d_ioctl_t smapi_ioctl;
76 
77 static struct cdevsw smapi_cdevsw = {
78 	.d_open =	nullopen,
79 	.d_close =	nullclose,
80 	.d_ioctl =	smapi_ioctl,
81 	.d_name =	"smapi",
82 	.d_maj =	MAJOR_AUTO,
83 	.d_flags =	D_MEM,
84 };
85 
86 static void	smapi_identify		(driver_t *, device_t);
87 static int	smapi_probe		(device_t);
88 static int	smapi_attach		(device_t);
89 static int	smapi_detach            (device_t);
90 static int	smapi_modevent		(module_t, int, void *);
91 
92 static int	smapi_header_cksum	(struct smapi_bios_header *);
93 
94 extern int	smapi32			(struct smapi_bios_parameter *,
95 					 struct smapi_bios_parameter *);
96 extern int	smapi32_new		(u_long, u_short,
97 					 struct smapi_bios_parameter *,
98 					 struct smapi_bios_parameter *);
99 
100 static int
101 smapi_ioctl (dev, cmd, data, fflag, td)
102 	dev_t		dev;
103 	u_long		cmd;
104 	caddr_t		data;
105 	int		fflag;
106 	d_thread_t *	td;
107 {
108 	struct smapi_softc *sc;
109 	int error;
110 
111 	error = 0;
112 	sc = devclass_get_softc(smapi_devclass, minor(dev));
113         if (sc == NULL) {
114                 error = ENXIO;
115                 goto fail;
116         }
117 
118 	switch (cmd) {
119 	case SMAPIOGHEADER:
120 		bcopy((caddr_t)sc->header, data,
121 				sizeof(struct smapi_bios_header));
122 		error = 0;
123 		break;
124 	case SMAPIOCGFUNCTION:
125 		smapi32_offset = sc->smapi32_entry;
126 		error = smapi32((struct smapi_bios_parameter *)data,
127 				(struct smapi_bios_parameter *)data);
128 		break;
129 	default:
130 		error = ENOTTY;
131 	}
132 
133 fail:
134 	return (error);
135 }
136 
137 static int
138 smapi_header_cksum (struct smapi_bios_header *header)
139 {
140 	u_int8_t *ptr;
141 	u_int8_t cksum;
142 	int i;
143 
144 	ptr = (u_int8_t *)header;
145 	cksum = 0;
146 	for (i = 0; i < header->length; i++) {
147 		cksum += ptr[i];
148 	}
149 
150 	return (cksum);
151 }
152 
153 static void
154 smapi_identify (driver_t *driver, device_t parent)
155 {
156 	device_t child;
157 	u_int32_t addr;
158 	int length;
159 	int rid;
160 
161 	if (!device_is_alive(parent))
162 		return;
163 
164 	addr = bios_sigsearch(SMAPI_START, SMAPI_SIG, SMAPI_LEN,
165                               SMAPI_STEP, SMAPI_OFF);
166 	if (addr != 0) {
167 		rid = 0;
168 		length = ADDR2HDR(addr)->length;
169 
170 		child = BUS_ADD_CHILD(parent, 0, "smapi", -1);
171 		device_set_driver(child, driver);
172 		bus_set_resource(child, SYS_RES_MEMORY, rid, addr, length);
173 		device_set_desc(child, "SMAPI BIOS");
174 	}
175 
176 	return;
177 }
178 
179 static int
180 smapi_probe (device_t dev)
181 {
182 	struct resource *res;
183 	int rid;
184 	int error;
185 
186 	error = 0;
187 	rid = 0;
188 	res = bus_alloc_resource(dev, SYS_RES_MEMORY, &rid,
189 		0ul, ~0ul, 1, RF_ACTIVE);
190 	if (res == NULL) {
191 		device_printf(dev, "Unable to allocate memory resource.\n");
192 		error = ENOMEM;
193 		goto bad;
194 	}
195 
196 	if (smapi_header_cksum(RES2HDR(res))) {
197 		device_printf(dev, "SMAPI header checksum failed.\n");
198 		error = ENXIO;
199 		goto bad;
200 	}
201 
202 bad:
203 	if (res)
204 		bus_release_resource(dev, SYS_RES_MEMORY, rid, res);
205 	return (error);
206 }
207 
208 static int
209 smapi_attach (device_t dev)
210 {
211 	struct smapi_softc *sc;
212 	int error;
213 
214 	sc = device_get_softc(dev);
215 	error = 0;
216 
217 	sc->dev = dev;
218 	sc->rid = 0;
219 	sc->res = bus_alloc_resource(dev, SYS_RES_MEMORY, &sc->rid,
220 		0ul, ~0ul, 1, RF_ACTIVE);
221 	if (sc->res == NULL) {
222 		device_printf(dev, "Unable to allocate memory resource.\n");
223 		error = ENOMEM;
224 		goto bad;
225 	}
226 	sc->header = (struct smapi_bios_header *)rman_get_virtual(sc->res);
227 	sc->smapi32_entry = (u_int32_t)BIOS_PADDRTOVADDR(
228 					sc->header->prot32_segment +
229 					sc->header->prot32_offset);
230 
231         sc->cdev = make_dev(&smapi_cdevsw,
232 			device_get_unit(sc->dev),
233 			UID_ROOT, GID_WHEEL, 0600,
234 			"%s%d",
235 			smapi_cdevsw.d_name,
236 			device_get_unit(sc->dev));
237 
238 	device_printf(dev, "Version: %d.%02d, Length: %d, Checksum: 0x%02x\n",
239 		bcd2bin(sc->header->version_major),
240 		bcd2bin(sc->header->version_minor),
241 		sc->header->length,
242 		sc->header->checksum);
243 	device_printf(dev, "Information=0x%b\n",
244 		sc->header->information,
245 		"\020"
246 		"\001REAL_VM86"
247 		"\002PROTECTED_16"
248 		"\003PROTECTED_32");
249 
250 	if (bootverbose) {
251 		if (sc->header->information & SMAPI_REAL_VM86)
252 			device_printf(dev, "Real/VM86 mode: Segment 0x%04x, Offset 0x%04x\n",
253 				sc->header->real16_segment,
254 				sc->header->real16_offset);
255 		if (sc->header->information & SMAPI_PROT_16BIT)
256 			device_printf(dev, "16-bit Protected mode: Segment 0x%08x, Offset 0x%04x\n",
257 				sc->header->prot16_segment,
258 				sc->header->prot16_offset);
259 		if (sc->header->information & SMAPI_PROT_32BIT)
260 			device_printf(dev, "32-bit Protected mode: Segment 0x%08x, Offset 0x%08x\n",
261 				sc->header->prot32_segment,
262 				sc->header->prot32_offset);
263 	}
264 
265 	return (0);
266 bad:
267 	if (sc->res)
268 		bus_release_resource(dev, SYS_RES_MEMORY, sc->rid, sc->res);
269 	return (error);
270 }
271 
272 static int
273 smapi_detach (device_t dev)
274 {
275 	struct smapi_softc *sc;
276 
277 	sc = device_get_softc(dev);
278 
279 	destroy_dev(sc->cdev);
280 
281 	if (sc->res)
282 		bus_release_resource(dev, SYS_RES_MEMORY, sc->rid, sc->res);
283 
284 	return (0);
285 }
286 
287 static int
288 smapi_modevent (mod, what, arg)
289         module_t        mod;
290         int             what;
291         void *          arg;
292 {
293 	device_t *	devs;
294 	int		count;
295 	int		i;
296 
297 	switch (what) {
298 	case MOD_LOAD:
299 		break;
300 	case MOD_UNLOAD:
301 		devclass_get_devices(smapi_devclass, &devs, &count);
302 		for (i = 0; i < count; i++) {
303 			device_delete_child(device_get_parent(devs[i]), devs[i]);
304 		}
305 		break;
306 	default:
307 		break;
308 	}
309 
310 	return (0);
311 }
312 
313 static device_method_t smapi_methods[] = {
314 	/* Device interface */
315 	DEVMETHOD(device_identify,      smapi_identify),
316 	DEVMETHOD(device_probe,         smapi_probe),
317 	DEVMETHOD(device_attach,        smapi_attach),
318 	DEVMETHOD(device_detach,        smapi_detach),
319 	{ 0, 0 }
320 };
321 
322 static driver_t smapi_driver = {
323 	"smapi",
324 	smapi_methods,
325 	sizeof(struct smapi_softc),
326 };
327 
328 DRIVER_MODULE(smapi, nexus, smapi_driver, smapi_devclass, smapi_modevent, 0);
329 MODULE_VERSION(smapi, 1);
330