xref: /freebsd/sys/i386/bios/smapi.c (revision 848ee2a3a8b47c9905fc51fefcf60eb371edbb98)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2003 Matthew N. Dodd <winter@jurai.net>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/malloc.h>
36 
37 #include <sys/module.h>
38 #include <sys/bus.h>
39 #include <sys/conf.h>
40 
41 #include <machine/bus.h>
42 #include <machine/resource.h>
43 #include <sys/rman.h>
44 
45 /* And all this for BIOS_PADDRTOVADDR() */
46 #include <vm/vm.h>
47 #include <vm/vm_param.h>
48 #include <vm/pmap.h>
49 #include <machine/md_var.h>
50 #include <machine/pc/bios.h>
51 
52 #include <machine/smapi.h>
53 
54 #define	SMAPI_START	0xf0000
55 #define	SMAPI_STEP	0x10
56 #define	SMAPI_OFF	0
57 #define	SMAPI_LEN	4
58 #define	SMAPI_SIG	"$SMB"
59 
60 #define	RES2HDR(res)	((struct smapi_bios_header *)rman_get_virtual(res))
61 #define	ADDR2HDR(addr)	((struct smapi_bios_header *)BIOS_PADDRTOVADDR(addr))
62 
63 struct smapi_softc {
64 	struct cdev *		cdev;
65 	device_t		dev;
66 	struct resource *	res;
67 	int			rid;
68 
69 	u_int32_t		smapi32_entry;
70 
71 	struct smapi_bios_header *header;
72 };
73 
74 extern u_long smapi32_offset;
75 extern u_short smapi32_segment;
76 
77 devclass_t smapi_devclass;
78 
79 static d_ioctl_t smapi_ioctl;
80 
81 static struct cdevsw smapi_cdevsw = {
82 	.d_version =	D_VERSION,
83 	.d_ioctl =	smapi_ioctl,
84 	.d_name =	"smapi",
85 	.d_flags =	D_NEEDGIANT,
86 };
87 
88 static void	smapi_identify(driver_t *, device_t);
89 static int	smapi_probe(device_t);
90 static int	smapi_attach(device_t);
91 static int	smapi_detach(device_t);
92 static int	smapi_modevent(module_t, int, void *);
93 
94 static int	smapi_header_cksum(struct smapi_bios_header *);
95 
96 extern int	smapi32(struct smapi_bios_parameter *,
97 		    struct smapi_bios_parameter *);
98 extern int	smapi32_new(u_long, u_short,
99 		    struct smapi_bios_parameter *,
100 		    struct smapi_bios_parameter *);
101 
102 static int
103 smapi_ioctl (struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
104 {
105 	struct smapi_softc *sc;
106 	int error;
107 
108 	error = 0;
109 	sc = devclass_get_softc(smapi_devclass, dev2unit(dev));
110         if (sc == NULL) {
111                 error = ENXIO;
112                 goto fail;
113         }
114 
115 	switch (cmd) {
116 	case SMAPIOGHEADER:
117 		bcopy((caddr_t)sc->header, data,
118 				sizeof(struct smapi_bios_header));
119 		error = 0;
120 		break;
121 	case SMAPIOCGFUNCTION:
122 		smapi32_offset = sc->smapi32_entry;
123 		error = smapi32((struct smapi_bios_parameter *)data,
124 				(struct smapi_bios_parameter *)data);
125 		break;
126 	default:
127 		error = ENOTTY;
128 	}
129 
130 fail:
131 	return (error);
132 }
133 
134 static int
135 smapi_header_cksum (struct smapi_bios_header *header)
136 {
137 	u_int8_t *ptr;
138 	u_int8_t cksum;
139 	int i;
140 
141 	ptr = (u_int8_t *)header;
142 	cksum = 0;
143 	for (i = 0; i < header->length; i++) {
144 		cksum += ptr[i];
145 	}
146 
147 	return (cksum);
148 }
149 
150 static void
151 smapi_identify (driver_t *driver, device_t parent)
152 {
153 	device_t child;
154 	u_int32_t addr;
155 	int length;
156 	int rid;
157 
158 	if (!device_is_alive(parent))
159 		return;
160 
161 	addr = bios_sigsearch(SMAPI_START, SMAPI_SIG, SMAPI_LEN,
162                               SMAPI_STEP, SMAPI_OFF);
163 	if (addr != 0) {
164 		rid = 0;
165 		length = ADDR2HDR(addr)->length;
166 
167 		child = BUS_ADD_CHILD(parent, 5, "smapi", -1);
168 		device_set_driver(child, driver);
169 		bus_set_resource(child, SYS_RES_MEMORY, rid, addr, length);
170 		device_set_desc(child, "SMAPI BIOS");
171 	}
172 
173 	return;
174 }
175 
176 static int
177 smapi_probe (device_t dev)
178 {
179 	struct resource *res;
180 	int rid;
181 	int error;
182 
183 	error = 0;
184 	rid = 0;
185 	res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
186 	if (res == NULL) {
187 		device_printf(dev, "Unable to allocate memory resource.\n");
188 		error = ENOMEM;
189 		goto bad;
190 	}
191 
192 	if (smapi_header_cksum(RES2HDR(res))) {
193 		device_printf(dev, "SMAPI header checksum failed.\n");
194 		error = ENXIO;
195 		goto bad;
196 	}
197 
198 bad:
199 	if (res)
200 		bus_release_resource(dev, SYS_RES_MEMORY, rid, res);
201 	return (error);
202 }
203 
204 static int
205 smapi_attach (device_t dev)
206 {
207 	struct smapi_softc *sc;
208 	int error;
209 
210 	sc = device_get_softc(dev);
211 	error = 0;
212 
213 	sc->dev = dev;
214 	sc->rid = 0;
215 	sc->res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->rid,
216 		 RF_ACTIVE);
217 	if (sc->res == NULL) {
218 		device_printf(dev, "Unable to allocate memory resource.\n");
219 		error = ENOMEM;
220 		goto bad;
221 	}
222 	sc->header = (struct smapi_bios_header *)rman_get_virtual(sc->res);
223 	sc->smapi32_entry = (u_int32_t)BIOS_PADDRTOVADDR(
224 					sc->header->prot32_segment +
225 					sc->header->prot32_offset);
226 
227         sc->cdev = make_dev(&smapi_cdevsw,
228 			device_get_unit(sc->dev),
229 			UID_ROOT, GID_WHEEL, 0600,
230 			"%s%d",
231 			smapi_cdevsw.d_name,
232 			device_get_unit(sc->dev));
233 
234 	device_printf(dev, "Version: %d.%02d, Length: %d, Checksum: 0x%02x\n",
235 		bcd2bin(sc->header->version_major),
236 		bcd2bin(sc->header->version_minor),
237 		sc->header->length,
238 		sc->header->checksum);
239 	device_printf(dev, "Information=0x%b\n",
240 		sc->header->information,
241 		"\020"
242 		"\001REAL_VM86"
243 		"\002PROTECTED_16"
244 		"\003PROTECTED_32");
245 
246 	if (bootverbose) {
247 		if (sc->header->information & SMAPI_REAL_VM86)
248 			device_printf(dev, "Real/VM86 mode: Segment 0x%04x, Offset 0x%04x\n",
249 				sc->header->real16_segment,
250 				sc->header->real16_offset);
251 		if (sc->header->information & SMAPI_PROT_16BIT)
252 			device_printf(dev, "16-bit Protected mode: Segment 0x%08x, Offset 0x%04x\n",
253 				sc->header->prot16_segment,
254 				sc->header->prot16_offset);
255 		if (sc->header->information & SMAPI_PROT_32BIT)
256 			device_printf(dev, "32-bit Protected mode: Segment 0x%08x, Offset 0x%08x\n",
257 				sc->header->prot32_segment,
258 				sc->header->prot32_offset);
259 	}
260 
261 	return (0);
262 bad:
263 	if (sc->res)
264 		bus_release_resource(dev, SYS_RES_MEMORY, sc->rid, sc->res);
265 	return (error);
266 }
267 
268 static int
269 smapi_detach (device_t dev)
270 {
271 	struct smapi_softc *sc;
272 
273 	sc = device_get_softc(dev);
274 
275 	destroy_dev(sc->cdev);
276 
277 	if (sc->res)
278 		bus_release_resource(dev, SYS_RES_MEMORY, sc->rid, sc->res);
279 
280 	return (0);
281 }
282 
283 static int
284 smapi_modevent (module_t mod, int what, void *arg)
285 {
286 	device_t *	devs;
287 	int		count;
288 	int		i;
289 
290 	switch (what) {
291 	case MOD_LOAD:
292 		break;
293 	case MOD_UNLOAD:
294 		devclass_get_devices(smapi_devclass, &devs, &count);
295 		for (i = 0; i < count; i++) {
296 			device_delete_child(device_get_parent(devs[i]), devs[i]);
297 		}
298 		free(devs, M_TEMP);
299 		break;
300 	default:
301 		break;
302 	}
303 
304 	return (0);
305 }
306 
307 static device_method_t smapi_methods[] = {
308 	/* Device interface */
309 	DEVMETHOD(device_identify,      smapi_identify),
310 	DEVMETHOD(device_probe,         smapi_probe),
311 	DEVMETHOD(device_attach,        smapi_attach),
312 	DEVMETHOD(device_detach,        smapi_detach),
313 	{ 0, 0 }
314 };
315 
316 static driver_t smapi_driver = {
317 	"smapi",
318 	smapi_methods,
319 	sizeof(struct smapi_softc),
320 };
321 
322 DRIVER_MODULE(smapi, nexus, smapi_driver, smapi_devclass, smapi_modevent, 0);
323 MODULE_VERSION(smapi, 1);
324