xref: /freebsd/sys/i386/bios/smapi.c (revision 83c0a9e839e2430617ae511cf80e150a8984d414)
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 = dev->si_drv1;
106 	int error;
107 
108 	switch (cmd) {
109 	case SMAPIOGHEADER:
110 		bcopy((caddr_t)sc->header, data,
111 				sizeof(struct smapi_bios_header));
112 		error = 0;
113 		break;
114 	case SMAPIOCGFUNCTION:
115 		smapi32_offset = sc->smapi32_entry;
116 		error = smapi32((struct smapi_bios_parameter *)data,
117 				(struct smapi_bios_parameter *)data);
118 		break;
119 	default:
120 		error = ENOTTY;
121 	}
122 
123 	return (error);
124 }
125 
126 static int
127 smapi_header_cksum (struct smapi_bios_header *header)
128 {
129 	u_int8_t *ptr;
130 	u_int8_t cksum;
131 	int i;
132 
133 	ptr = (u_int8_t *)header;
134 	cksum = 0;
135 	for (i = 0; i < header->length; i++) {
136 		cksum += ptr[i];
137 	}
138 
139 	return (cksum);
140 }
141 
142 static void
143 smapi_identify (driver_t *driver, device_t parent)
144 {
145 	device_t child;
146 	u_int32_t addr;
147 	int length;
148 	int rid;
149 
150 	if (!device_is_alive(parent))
151 		return;
152 
153 	addr = bios_sigsearch(SMAPI_START, SMAPI_SIG, SMAPI_LEN,
154                               SMAPI_STEP, SMAPI_OFF);
155 	if (addr != 0) {
156 		rid = 0;
157 		length = ADDR2HDR(addr)->length;
158 
159 		child = BUS_ADD_CHILD(parent, 5, "smapi", -1);
160 		device_set_driver(child, driver);
161 		bus_set_resource(child, SYS_RES_MEMORY, rid, addr, length);
162 		device_set_desc(child, "SMAPI BIOS");
163 	}
164 
165 	return;
166 }
167 
168 static int
169 smapi_probe (device_t dev)
170 {
171 	struct resource *res;
172 	int rid;
173 	int error;
174 
175 	error = 0;
176 	rid = 0;
177 	res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
178 	if (res == NULL) {
179 		device_printf(dev, "Unable to allocate memory resource.\n");
180 		error = ENOMEM;
181 		goto bad;
182 	}
183 
184 	if (smapi_header_cksum(RES2HDR(res))) {
185 		device_printf(dev, "SMAPI header checksum failed.\n");
186 		error = ENXIO;
187 		goto bad;
188 	}
189 
190 bad:
191 	if (res)
192 		bus_release_resource(dev, SYS_RES_MEMORY, rid, res);
193 	return (error);
194 }
195 
196 static int
197 smapi_attach (device_t dev)
198 {
199 	struct make_dev_args args;
200 	struct smapi_softc *sc;
201 	int error;
202 
203 	sc = device_get_softc(dev);
204 	error = 0;
205 
206 	sc->dev = dev;
207 	sc->rid = 0;
208 	sc->res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->rid,
209 		 RF_ACTIVE);
210 	if (sc->res == NULL) {
211 		device_printf(dev, "Unable to allocate memory resource.\n");
212 		error = ENOMEM;
213 		goto bad;
214 	}
215 	sc->header = (struct smapi_bios_header *)rman_get_virtual(sc->res);
216 	sc->smapi32_entry = (u_int32_t)BIOS_PADDRTOVADDR(
217 					sc->header->prot32_segment +
218 					sc->header->prot32_offset);
219 
220 	make_dev_args_init(&args);
221 	args.mda_devsw = &smapi_cdevsw;
222 	args.mda_uid = UID_ROOT;
223 	args.mda_gid = GID_WHEEL;
224 	args.mda_mode = 0600;
225 	args.mda_si_drv1 = sc;
226         error = make_dev_s(&args, &sc->cdev, "%s%d",
227 			smapi_cdevsw.d_name,
228 			device_get_unit(sc->dev));
229 	if (error != 0)
230 		goto bad;
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 device_method_t smapi_methods[] = {
282 	/* Device interface */
283 	DEVMETHOD(device_identify,      smapi_identify),
284 	DEVMETHOD(device_probe,         smapi_probe),
285 	DEVMETHOD(device_attach,        smapi_attach),
286 	DEVMETHOD(device_detach,        smapi_detach),
287 	{ 0, 0 }
288 };
289 
290 static driver_t smapi_driver = {
291 	"smapi",
292 	smapi_methods,
293 	sizeof(struct smapi_softc),
294 };
295 
296 static int
297 smapi_modevent (module_t mod, int what, void *arg)
298 {
299 	device_t *	devs;
300 	int		count;
301 	int		i;
302 
303 	switch (what) {
304 	case MOD_LOAD:
305 		break;
306 	case MOD_UNLOAD:
307 		devclass_get_devices(devclass_find(smapi_driver.name), &devs,
308 		    &count);
309 		for (i = 0; i < count; i++) {
310 			device_delete_child(device_get_parent(devs[i]), devs[i]);
311 		}
312 		free(devs, M_TEMP);
313 		break;
314 	default:
315 		break;
316 	}
317 
318 	return (0);
319 }
320 
321 DRIVER_MODULE(smapi, nexus, smapi_driver, smapi_devclass, smapi_modevent, 0);
322 MODULE_VERSION(smapi, 1);
323