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