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