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