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