xref: /freebsd/sys/dev/smbios/smbios.c (revision 5ac70383c8b32eeec80426e837960977971c7c2b)
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 #include <sys/socket.h>
37 
38 #include <sys/module.h>
39 #include <sys/bus.h>
40 
41 #include <machine/bus.h>
42 #include <machine/resource.h>
43 #include <sys/rman.h>
44 
45 #include <vm/vm.h>
46 #include <vm/vm_param.h>
47 #include <vm/pmap.h>
48 #include <machine/md_var.h>
49 #if defined(__amd64__) || defined(__i386__)
50 #include <machine/pc/bios.h>
51 #endif
52 #include <dev/smbios/smbios.h>
53 
54 /*
55  * System Management BIOS Reference Specification, v2.4 Final
56  * http://www.dmtf.org/standards/published_documents/DSP0134.pdf
57  */
58 
59 struct smbios_softc {
60 	device_t		dev;
61 	struct resource *	res;
62 	int			rid;
63 
64 	struct smbios_eps *	eps;
65 };
66 
67 #define	RES2EPS(res)	((struct smbios_eps *)rman_get_virtual(res))
68 
69 static devclass_t	smbios_devclass;
70 
71 static void	smbios_identify	(driver_t *, device_t);
72 static int	smbios_probe	(device_t);
73 static int	smbios_attach	(device_t);
74 static int	smbios_detach	(device_t);
75 static int	smbios_modevent	(module_t, int, void *);
76 
77 static int	smbios_cksum	(struct smbios_eps *);
78 
79 static void
80 smbios_identify (driver_t *driver, device_t parent)
81 {
82 	struct smbios_eps *eps;
83 	device_t child;
84 	vm_paddr_t addr;
85 	int length;
86 	int rid;
87 
88 	if (!device_is_alive(parent))
89 		return;
90 
91 #if defined(__amd64__) || defined(__i386__)
92 	addr = bios_sigsearch(SMBIOS_START, SMBIOS_SIG, SMBIOS_LEN,
93 	    SMBIOS_STEP, SMBIOS_OFF);
94 #else
95 	addr = 0;
96 #endif
97 
98 	if (addr != 0) {
99 		eps = pmap_mapbios(addr, 0x1f);
100 		rid = 0;
101 		length = eps->length;
102 
103 		if (length != 0x1f) {
104 			u_int8_t major, minor;
105 
106 			major = eps->major_version;
107 			minor = eps->minor_version;
108 
109 			/* SMBIOS v2.1 implementation might use 0x1e. */
110 			if (length == 0x1e && major == 2 && minor == 1)
111 				length = 0x1f;
112 			else
113 				return;
114 		}
115 
116 		child = BUS_ADD_CHILD(parent, 5, "smbios", -1);
117 		device_set_driver(child, driver);
118 		bus_set_resource(child, SYS_RES_MEMORY, rid, addr, length);
119 		device_set_desc(child, "System Management BIOS");
120 		pmap_unmapbios((vm_offset_t)eps, 0x1f);
121 	}
122 
123 	return;
124 }
125 
126 static int
127 smbios_probe (device_t dev)
128 {
129 	struct resource *res;
130 	int rid;
131 	int error;
132 
133 	error = 0;
134 	rid = 0;
135 	res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
136 	if (res == NULL) {
137 		device_printf(dev, "Unable to allocate memory resource.\n");
138 		error = ENOMEM;
139 		goto bad;
140 	}
141 
142 	if (smbios_cksum(RES2EPS(res))) {
143 		device_printf(dev, "SMBIOS checksum failed.\n");
144 		error = ENXIO;
145 		goto bad;
146 	}
147 
148 bad:
149 	if (res)
150 		bus_release_resource(dev, SYS_RES_MEMORY, rid, res);
151 	return (error);
152 }
153 
154 static int
155 smbios_attach (device_t dev)
156 {
157 	struct smbios_softc *sc;
158 	int error;
159 
160 	sc = device_get_softc(dev);
161 	error = 0;
162 
163 	sc->dev = dev;
164 	sc->rid = 0;
165 	sc->res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->rid,
166 		RF_ACTIVE);
167 	if (sc->res == NULL) {
168 		device_printf(dev, "Unable to allocate memory resource.\n");
169 		error = ENOMEM;
170 		goto bad;
171 	}
172 	sc->eps = RES2EPS(sc->res);
173 
174 	device_printf(dev, "Version: %u.%u",
175 	    sc->eps->major_version, sc->eps->minor_version);
176 	if (bcd2bin(sc->eps->BCD_revision))
177 		printf(", BCD Revision: %u.%u",
178 			bcd2bin(sc->eps->BCD_revision >> 4),
179 			bcd2bin(sc->eps->BCD_revision & 0x0f));
180 	printf("\n");
181 
182 	return (0);
183 bad:
184 	if (sc->res)
185 		bus_release_resource(dev, SYS_RES_MEMORY, sc->rid, sc->res);
186 	return (error);
187 }
188 
189 static int
190 smbios_detach (device_t dev)
191 {
192 	struct smbios_softc *sc;
193 
194 	sc = device_get_softc(dev);
195 
196 	if (sc->res)
197 		bus_release_resource(dev, SYS_RES_MEMORY, sc->rid, sc->res);
198 
199 	return (0);
200 }
201 
202 static int
203 smbios_modevent (mod, what, arg)
204         module_t        mod;
205         int             what;
206         void *          arg;
207 {
208 	device_t *	devs;
209 	int		count;
210 	int		i;
211 
212 	switch (what) {
213 	case MOD_LOAD:
214 		break;
215 	case MOD_UNLOAD:
216 		devclass_get_devices(smbios_devclass, &devs, &count);
217 		for (i = 0; i < count; i++) {
218 			device_delete_child(device_get_parent(devs[i]), devs[i]);
219 		}
220 		free(devs, M_TEMP);
221 		break;
222 	default:
223 		break;
224 	}
225 
226 	return (0);
227 }
228 
229 static device_method_t smbios_methods[] = {
230 	/* Device interface */
231 	DEVMETHOD(device_identify,      smbios_identify),
232 	DEVMETHOD(device_probe,         smbios_probe),
233 	DEVMETHOD(device_attach,        smbios_attach),
234 	DEVMETHOD(device_detach,        smbios_detach),
235 	{ 0, 0 }
236 };
237 
238 static driver_t smbios_driver = {
239 	"smbios",
240 	smbios_methods,
241 	sizeof(struct smbios_softc),
242 };
243 
244 DRIVER_MODULE(smbios, nexus, smbios_driver, smbios_devclass, smbios_modevent, 0);
245 MODULE_VERSION(smbios, 1);
246 
247 static int
248 smbios_cksum (struct smbios_eps *e)
249 {
250 	u_int8_t *ptr;
251 	u_int8_t cksum;
252 	int i;
253 
254 	ptr = (u_int8_t *)e;
255 	cksum = 0;
256 	for (i = 0; i < e->length; i++) {
257 		cksum += ptr[i];
258 	}
259 
260 	return (cksum);
261 }
262