smbios.c revision 1.3.4.2 1 1.3.4.2 thorpej /* $NetBSD: smbios.c,v 1.3.4.2 2021/08/01 22:42:21 thorpej Exp $ */
2 1.3.4.2 thorpej
3 1.3.4.2 thorpej /*
4 1.3.4.2 thorpej * Copyright (c) 1999 The NetBSD Foundation, Inc.
5 1.3.4.2 thorpej * All rights reserved.
6 1.3.4.2 thorpej *
7 1.3.4.2 thorpej * This code is derived from software contributed to The NetBSD Foundation
8 1.3.4.2 thorpej * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 1.3.4.2 thorpej * NASA Ames Research Center.
10 1.3.4.2 thorpej *
11 1.3.4.2 thorpej * Redistribution and use in source and binary forms, with or without
12 1.3.4.2 thorpej * modification, are permitted provided that the following conditions
13 1.3.4.2 thorpej * are met:
14 1.3.4.2 thorpej * 1. Redistributions of source code must retain the above copyright
15 1.3.4.2 thorpej * notice, this list of conditions and the following disclaimer.
16 1.3.4.2 thorpej * 2. Redistributions in binary form must reproduce the above copyright
17 1.3.4.2 thorpej * notice, this list of conditions and the following disclaimer in the
18 1.3.4.2 thorpej * documentation and/or other materials provided with the distribution.
19 1.3.4.2 thorpej *
20 1.3.4.2 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 1.3.4.2 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 1.3.4.2 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 1.3.4.2 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 1.3.4.2 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 1.3.4.2 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 1.3.4.2 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 1.3.4.2 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 1.3.4.2 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 1.3.4.2 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 1.3.4.2 thorpej * POSSIBILITY OF SUCH DAMAGE.
31 1.3.4.2 thorpej */
32 1.3.4.2 thorpej
33 1.3.4.2 thorpej /*
34 1.3.4.2 thorpej * Copyright (c) 1999, by UCHIYAMA Yasushi
35 1.3.4.2 thorpej * All rights reserved.
36 1.3.4.2 thorpej *
37 1.3.4.2 thorpej * Redistribution and use in source and binary forms, with or without
38 1.3.4.2 thorpej * modification, are permitted provided that the following conditions
39 1.3.4.2 thorpej * are met:
40 1.3.4.2 thorpej * 1. Redistributions of source code must retain the above copyright
41 1.3.4.2 thorpej * notice, this list of conditions and the following disclaimer.
42 1.3.4.2 thorpej * 2. The name of the developer may NOT be used to endorse or promote products
43 1.3.4.2 thorpej * derived from this software without specific prior written permission.
44 1.3.4.2 thorpej *
45 1.3.4.2 thorpej * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
46 1.3.4.2 thorpej * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 1.3.4.2 thorpej * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 1.3.4.2 thorpej * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
49 1.3.4.2 thorpej * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 1.3.4.2 thorpej * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 1.3.4.2 thorpej * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 1.3.4.2 thorpej * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 1.3.4.2 thorpej * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 1.3.4.2 thorpej * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55 1.3.4.2 thorpej * SUCH DAMAGE.
56 1.3.4.2 thorpej */
57 1.3.4.2 thorpej
58 1.3.4.2 thorpej /*
59 1.3.4.2 thorpej * Copyright (c) 1997-2001 Michael Shalayeff
60 1.3.4.2 thorpej * All rights reserved.
61 1.3.4.2 thorpej *
62 1.3.4.2 thorpej * Redistribution and use in source and binary forms, with or without
63 1.3.4.2 thorpej * modification, are permitted provided that the following conditions
64 1.3.4.2 thorpej * are met:
65 1.3.4.2 thorpej * 1. Redistributions of source code must retain the above copyright
66 1.3.4.2 thorpej * notice, this list of conditions and the following disclaimer.
67 1.3.4.2 thorpej * 2. Redistributions in binary form must reproduce the above copyright
68 1.3.4.2 thorpej * notice, this list of conditions and the following disclaimer in the
69 1.3.4.2 thorpej * documentation and/or other materials provided with the distribution.
70 1.3.4.2 thorpej *
71 1.3.4.2 thorpej * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
72 1.3.4.2 thorpej * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
73 1.3.4.2 thorpej * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
74 1.3.4.2 thorpej * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT,
75 1.3.4.2 thorpej * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
76 1.3.4.2 thorpej * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
77 1.3.4.2 thorpej * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
78 1.3.4.2 thorpej * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
79 1.3.4.2 thorpej * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
80 1.3.4.2 thorpej * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
81 1.3.4.2 thorpej * THE POSSIBILITY OF SUCH DAMAGE.
82 1.3.4.2 thorpej */
83 1.3.4.2 thorpej
84 1.3.4.2 thorpej /*
85 1.3.4.2 thorpej * Basic interface to System Management BIOS (SMBIOS) tables.
86 1.3.4.2 thorpej */
87 1.3.4.2 thorpej
88 1.3.4.2 thorpej #include <sys/cdefs.h>
89 1.3.4.2 thorpej __KERNEL_RCSID(0, "$NetBSD: smbios.c,v 1.3.4.2 2021/08/01 22:42:21 thorpej Exp $");
90 1.3.4.2 thorpej
91 1.3.4.2 thorpej #include <sys/param.h>
92 1.3.4.2 thorpej #include <sys/conf.h>
93 1.3.4.2 thorpej #include <sys/systm.h>
94 1.3.4.2 thorpej #include <sys/device.h>
95 1.3.4.2 thorpej
96 1.3.4.2 thorpej #include <uvm/uvm_extern.h>
97 1.3.4.2 thorpej
98 1.3.4.2 thorpej #include <dev/smbiosvar.h>
99 1.3.4.2 thorpej
100 1.3.4.2 thorpej #define SMBIOS_MAKESIG(a, b, c, d) \
101 1.3.4.2 thorpej ((a) | ((b) << 8) | ((c) << 16) | ((d) << 24))
102 1.3.4.2 thorpej
103 1.3.4.2 thorpej struct smbios_entry smbios_entry;
104 1.3.4.2 thorpej
105 1.3.4.2 thorpej static dev_type_read(smbios_read);
106 1.3.4.2 thorpej
107 1.3.4.2 thorpej const struct cdevsw smbios_cdevsw = {
108 1.3.4.2 thorpej .d_open = nullopen,
109 1.3.4.2 thorpej .d_close = nullclose,
110 1.3.4.2 thorpej .d_read = smbios_read,
111 1.3.4.2 thorpej .d_write = nowrite,
112 1.3.4.2 thorpej .d_ioctl = noioctl,
113 1.3.4.2 thorpej .d_stop = nostop,
114 1.3.4.2 thorpej .d_tty = notty,
115 1.3.4.2 thorpej .d_poll = nopoll,
116 1.3.4.2 thorpej .d_mmap = nommap,
117 1.3.4.2 thorpej .d_kqfilter = nokqfilter,
118 1.3.4.2 thorpej .d_discard = nodiscard,
119 1.3.4.2 thorpej .d_flag = D_OTHER | D_MPSAFE,
120 1.3.4.2 thorpej };
121 1.3.4.2 thorpej
122 1.3.4.2 thorpej static void *
123 1.3.4.2 thorpej smbios_map_memory(paddr_t pa, size_t size)
124 1.3.4.2 thorpej {
125 1.3.4.2 thorpej paddr_t spa, epa, curpa;
126 1.3.4.2 thorpej vaddr_t va, curva;
127 1.3.4.2 thorpej
128 1.3.4.2 thorpej spa = trunc_page(pa);
129 1.3.4.2 thorpej epa = round_page(pa + size);
130 1.3.4.2 thorpej
131 1.3.4.2 thorpej va = uvm_km_alloc(kernel_map, epa - spa, 0, UVM_KMF_VAONLY);
132 1.3.4.2 thorpej if (va == 0) {
133 1.3.4.2 thorpej return NULL;
134 1.3.4.2 thorpej }
135 1.3.4.2 thorpej
136 1.3.4.2 thorpej for (curpa = spa, curva = va; curpa < epa; curpa += PAGE_SIZE, curva += PAGE_SIZE) {
137 1.3.4.2 thorpej pmap_kenter_pa(curva, curpa, VM_PROT_READ, PMAP_WRITE_BACK);
138 1.3.4.2 thorpej }
139 1.3.4.2 thorpej pmap_update(pmap_kernel());
140 1.3.4.2 thorpej
141 1.3.4.2 thorpej return (void *)(uintptr_t)(va + (pa - spa));
142 1.3.4.2 thorpej }
143 1.3.4.2 thorpej
144 1.3.4.2 thorpej static void
145 1.3.4.2 thorpej smbios_unmap_memory(void *va, size_t size)
146 1.3.4.2 thorpej {
147 1.3.4.2 thorpej vaddr_t ova;
148 1.3.4.2 thorpej vsize_t osz;
149 1.3.4.2 thorpej
150 1.3.4.2 thorpej ova = trunc_page((vaddr_t)va);
151 1.3.4.2 thorpej osz = round_page((vaddr_t)va + size) - ova;
152 1.3.4.2 thorpej
153 1.3.4.2 thorpej pmap_kremove(ova, osz);
154 1.3.4.2 thorpej pmap_update(pmap_kernel());
155 1.3.4.2 thorpej uvm_km_free(kernel_map, ova, osz, UVM_KMF_VAONLY);
156 1.3.4.2 thorpej }
157 1.3.4.2 thorpej
158 1.3.4.2 thorpej /*
159 1.3.4.2 thorpej * smbios_read --
160 1.3.4.2 thorpej *
161 1.3.4.2 thorpej * Read data from an SMBIOS table that resides in physical memory.
162 1.3.4.2 thorpej */
163 1.3.4.2 thorpej static int
164 1.3.4.2 thorpej smbios_read(dev_t dev, struct uio *uio, int flag)
165 1.3.4.2 thorpej {
166 1.3.4.2 thorpej paddr_t pa;
167 1.3.4.2 thorpej uint8_t *data;
168 1.3.4.2 thorpej size_t len;
169 1.3.4.2 thorpej int error;
170 1.3.4.2 thorpej
171 1.3.4.2 thorpej if (smbios_entry.addr == NULL) {
172 1.3.4.2 thorpej return EIO;
173 1.3.4.2 thorpej }
174 1.3.4.2 thorpej if (uio->uio_rw != UIO_READ) {
175 1.3.4.2 thorpej return EPERM;
176 1.3.4.2 thorpej }
177 1.3.4.2 thorpej
178 1.3.4.2 thorpej pa = uio->uio_offset;
179 1.3.4.2 thorpej if (pa == smbios_entry.hdrphys) {
180 1.3.4.2 thorpej /* SMBIOS header */
181 1.3.4.2 thorpej len = uimin(0x20, uio->uio_resid);
182 1.3.4.2 thorpej
183 1.3.4.2 thorpej } else {
184 1.3.4.2 thorpej /* Table data */
185 1.3.4.2 thorpej if (pa < smbios_entry.tabphys ||
186 1.3.4.2 thorpej pa >= smbios_entry.tabphys + smbios_entry.len) {
187 1.3.4.2 thorpej return EFAULT;
188 1.3.4.2 thorpej }
189 1.3.4.2 thorpej len = uimin(smbios_entry.len - (pa - smbios_entry.tabphys),
190 1.3.4.2 thorpej uio->uio_resid);
191 1.3.4.2 thorpej }
192 1.3.4.2 thorpej
193 1.3.4.2 thorpej data = smbios_map_memory(pa, len);
194 1.3.4.2 thorpej if (data == NULL) {
195 1.3.4.2 thorpej return ENOMEM;
196 1.3.4.2 thorpej }
197 1.3.4.2 thorpej error = uiomove(data, len, uio);
198 1.3.4.2 thorpej smbios_unmap_memory(data, len);
199 1.3.4.2 thorpej
200 1.3.4.2 thorpej return error;
201 1.3.4.2 thorpej }
202 1.3.4.2 thorpej
203 1.3.4.2 thorpej int
204 1.3.4.2 thorpej smbios2_check_header(const uint8_t *p)
205 1.3.4.2 thorpej {
206 1.3.4.2 thorpej const struct smbhdr *sh = (const struct smbhdr *)p;
207 1.3.4.2 thorpej uint8_t chksum;
208 1.3.4.2 thorpej int i;
209 1.3.4.2 thorpej
210 1.3.4.2 thorpej if (sh->sig != SMBIOS_MAKESIG('_', 'S', 'M', '_'))
211 1.3.4.2 thorpej return 0;
212 1.3.4.2 thorpej i = sh->len;
213 1.3.4.2 thorpej for (chksum = 0; i--; )
214 1.3.4.2 thorpej chksum += p[i];
215 1.3.4.2 thorpej if (chksum != 0)
216 1.3.4.2 thorpej return 0;
217 1.3.4.2 thorpej p += 0x10;
218 1.3.4.2 thorpej if (p[0] != '_' || p[1] != 'D' || p[2] != 'M' ||
219 1.3.4.2 thorpej p[3] != 'I' || p[4] != '_')
220 1.3.4.2 thorpej return 0;
221 1.3.4.2 thorpej for (chksum = 0, i = 0xf; i--; )
222 1.3.4.2 thorpej chksum += p[i];
223 1.3.4.2 thorpej if (chksum != 0)
224 1.3.4.2 thorpej return 0;
225 1.3.4.2 thorpej
226 1.3.4.2 thorpej return 1;
227 1.3.4.2 thorpej }
228 1.3.4.2 thorpej
229 1.3.4.2 thorpej int
230 1.3.4.2 thorpej smbios3_check_header(const uint8_t *p)
231 1.3.4.2 thorpej {
232 1.3.4.2 thorpej const struct smb3hdr *sh = (const struct smb3hdr *)p;
233 1.3.4.2 thorpej uint8_t chksum;
234 1.3.4.2 thorpej int i;
235 1.3.4.2 thorpej
236 1.3.4.2 thorpej if (p[0] != '_' || p[1] != 'S' || p[2] != 'M' ||
237 1.3.4.2 thorpej p[3] != '3' || p[4] != '_')
238 1.3.4.2 thorpej return 0;
239 1.3.4.2 thorpej i = sh->len;
240 1.3.4.2 thorpej for (chksum = 0; i--; )
241 1.3.4.2 thorpej chksum += p[i];
242 1.3.4.2 thorpej if (chksum != 0)
243 1.3.4.2 thorpej return 0;
244 1.3.4.2 thorpej if (sh->eprev != SMBIOS3_EPREV_3_0)
245 1.3.4.2 thorpej return 0;
246 1.3.4.2 thorpej
247 1.3.4.2 thorpej return 1;
248 1.3.4.2 thorpej }
249 1.3.4.2 thorpej
250 1.3.4.2 thorpej /*
251 1.3.4.2 thorpej * smbios_find_table() takes a caller supplied smbios struct type and
252 1.3.4.2 thorpej * a pointer to a handle (struct smbtable) returning one if the structure
253 1.3.4.2 thorpej * is successfully located and zero otherwise. Callers should take care
254 1.3.4.2 thorpej * to initilize the cookie field of the smbtable structure to zero before
255 1.3.4.2 thorpej * the first invocation of this function.
256 1.3.4.2 thorpej * Multiple tables of the same type can be located by repeadtly calling
257 1.3.4.2 thorpej * smbios_find_table with the same arguments.
258 1.3.4.2 thorpej */
259 1.3.4.2 thorpej int
260 1.3.4.2 thorpej smbios_find_table(uint8_t type, struct smbtable *st)
261 1.3.4.2 thorpej {
262 1.3.4.2 thorpej uint8_t *va, *end;
263 1.3.4.2 thorpej struct smbtblhdr *hdr;
264 1.3.4.2 thorpej int ret = 0, tcount = 1;
265 1.3.4.2 thorpej
266 1.3.4.2 thorpej if (smbios_entry.addr == NULL) {
267 1.3.4.2 thorpej return 0;
268 1.3.4.2 thorpej }
269 1.3.4.2 thorpej
270 1.3.4.2 thorpej va = smbios_entry.addr;
271 1.3.4.2 thorpej end = va + smbios_entry.len;
272 1.3.4.2 thorpej
273 1.3.4.2 thorpej /*
274 1.3.4.2 thorpej * The cookie field of the smtable structure is used to locate
275 1.3.4.2 thorpej * multiple instances of a table of an arbitrary type. Following the
276 1.3.4.2 thorpej * sucessful location of a table, the type is encoded as bits 0:7 of
277 1.3.4.2 thorpej * the cookie value, the offset in terms of the number of structures
278 1.3.4.2 thorpej * preceding that referenced by the handle is encoded in bits 15:31.
279 1.3.4.2 thorpej */
280 1.3.4.2 thorpej if ((st->cookie & 0xfff) == type && st->cookie >> 16) {
281 1.3.4.2 thorpej if ((uint8_t *)st->hdr >= va && (uint8_t *)st->hdr < end) {
282 1.3.4.2 thorpej hdr = st->hdr;
283 1.3.4.2 thorpej if (hdr->type == type) {
284 1.3.4.2 thorpej va = (uint8_t *)hdr + hdr->size;
285 1.3.4.2 thorpej for (; va + 1 < end; va++)
286 1.3.4.2 thorpej if (*va == 0 && *(va + 1) == 0)
287 1.3.4.2 thorpej break;
288 1.3.4.2 thorpej va+= 2;
289 1.3.4.2 thorpej tcount = st->cookie >> 16;
290 1.3.4.2 thorpej }
291 1.3.4.2 thorpej }
292 1.3.4.2 thorpej }
293 1.3.4.2 thorpej for (; va + sizeof(struct smbtblhdr) < end && tcount <=
294 1.3.4.2 thorpej smbios_entry.count; tcount++) {
295 1.3.4.2 thorpej hdr = (struct smbtblhdr *)va;
296 1.3.4.2 thorpej if (hdr->type == type) {
297 1.3.4.2 thorpej ret = 1;
298 1.3.4.2 thorpej st->hdr = hdr;
299 1.3.4.2 thorpej st->tblhdr = va + sizeof(struct smbtblhdr);
300 1.3.4.2 thorpej st->cookie = (tcount + 1) << 16 | type;
301 1.3.4.2 thorpej break;
302 1.3.4.2 thorpej }
303 1.3.4.2 thorpej if (hdr->type == SMBIOS_TYPE_EOT)
304 1.3.4.2 thorpej break;
305 1.3.4.2 thorpej va+= hdr->size;
306 1.3.4.2 thorpej for (; va + 1 < end; va++)
307 1.3.4.2 thorpej if (*va == 0 && *(va + 1) == 0)
308 1.3.4.2 thorpej break;
309 1.3.4.2 thorpej va+=2;
310 1.3.4.2 thorpej }
311 1.3.4.2 thorpej
312 1.3.4.2 thorpej return ret;
313 1.3.4.2 thorpej }
314 1.3.4.2 thorpej
315 1.3.4.2 thorpej char *
316 1.3.4.2 thorpej smbios_get_string(struct smbtable *st, uint8_t indx, char *dest, size_t len)
317 1.3.4.2 thorpej {
318 1.3.4.2 thorpej uint8_t *va, *end;
319 1.3.4.2 thorpej char *ret = NULL;
320 1.3.4.2 thorpej int i;
321 1.3.4.2 thorpej
322 1.3.4.2 thorpej KASSERT(smbios_entry.addr != NULL);
323 1.3.4.2 thorpej
324 1.3.4.2 thorpej va = (uint8_t *)st->hdr + st->hdr->size;
325 1.3.4.2 thorpej end = smbios_entry.addr + smbios_entry.len;
326 1.3.4.2 thorpej for (i = 1; va < end && i < indx && *va; i++)
327 1.3.4.2 thorpej while (*va++)
328 1.3.4.2 thorpej ;
329 1.3.4.2 thorpej if (i == indx) {
330 1.3.4.2 thorpej if (va + len < end) {
331 1.3.4.2 thorpej ret = dest;
332 1.3.4.2 thorpej memcpy(ret, va, len);
333 1.3.4.2 thorpej ret[len - 1] = '\0';
334 1.3.4.2 thorpej }
335 1.3.4.2 thorpej }
336 1.3.4.2 thorpej
337 1.3.4.2 thorpej return ret;
338 1.3.4.2 thorpej }
339