1 /*-
2 * Copyright (c) 2005-2009 Jung-uk Kim <jkim@FreeBSD.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include <stand.h>
31 #include <bootstrap.h>
32 #include <sys/endian.h>
33
34 #ifdef EFI
35 /* In EFI, we don't need PTOV(). */
36 #define PTOV(x) (caddr_t)(x)
37 #else
38 #include "btxv86.h"
39 #endif
40 #include "smbios.h"
41
42 /*
43 * Detect SMBIOS and export information about the SMBIOS into the
44 * environment.
45 *
46 * System Management BIOS Reference Specification, v2.6 Final
47 * http://www.dmtf.org/standards/published_documents/DSP0134_2.6.0.pdf
48 * System Management BIOS (SMBIOS) Reference Specification, v3.1.0
49 * http://www.dmtf.org/sites/default/files/standards/documents/DSP0134_3.1.0.pdf
50 */
51
52 /*
53 * 2.1.1 SMBIOS Structure Table Entry Point
54 *
55 * "On non-EFI systems, the SMBIOS Entry Point structure, described below, can
56 * be located by application software by searching for the anchor-string on
57 * paragraph (16-byte) boundaries within the physical memory address range
58 * 000F0000h to 000FFFFFh. This entry point encapsulates an intermediate anchor
59 * string that is used by some existing DMI browsers."
60 */
61 #define SMBIOS_START 0xf0000
62 #define SMBIOS_LENGTH 0x10000
63 #define SMBIOS_STEP 0x10
64 #define SMBIOS_SIG "_SM_"
65 #define SMBIOS_SIG_LEN (4)
66 #define SMBIOS3_SIG "_SM3_"
67 #define SMBIOS3_SIG_LEN (5)
68 #define SMBIOS_DMI_SIG "_DMI_"
69 #define SMBIOS_DMI_SIG_LEN (5)
70
71 #define SMBIOS_GET8(base, off) (*(uint8_t *)((base) + (off)))
72 #define SMBIOS_GET16(base, off) (*(uint16_t *)((base) + (off)))
73 #define SMBIOS_GET32(base, off) (*(uint32_t *)((base) + (off)))
74 #define SMBIOS_GET64(base, off) (*(uint64_t *)((base) + (off)))
75
76 #define SMBIOS_GETLEN(base) SMBIOS_GET8(base, 0x01)
77 #define SMBIOS_GETSTR(base) ((base) + SMBIOS_GETLEN(base))
78
79 struct smbios_attr {
80 int probed;
81 caddr_t addr;
82 size_t length;
83 size_t count;
84 int major;
85 int minor;
86 int ver;
87 const char* bios_vendor;
88 const char* maker;
89 const char* product;
90 uint32_t enabled_memory;
91 uint32_t old_enabled_memory;
92 uint8_t enabled_sockets;
93 uint8_t populated_sockets;
94 };
95
96 static struct smbios_attr smbios;
97
98 static uint8_t
smbios_checksum(const caddr_t addr,const uint8_t len)99 smbios_checksum(const caddr_t addr, const uint8_t len)
100 {
101 uint8_t sum;
102 int i;
103
104 for (sum = 0, i = 0; i < len; i++)
105 sum += SMBIOS_GET8(addr, i);
106 return (sum);
107 }
108
109 static caddr_t
smbios_sigsearch(const caddr_t addr,const uint32_t len)110 smbios_sigsearch(const caddr_t addr, const uint32_t len)
111 {
112 caddr_t cp;
113 uintptr_t paddr;
114
115 /* Search on 16-byte boundaries. */
116 for (cp = addr; cp < addr + len; cp += SMBIOS_STEP) {
117 if (strncmp(cp, SMBIOS_SIG, SMBIOS_SIG_LEN) == 0 &&
118 smbios_checksum(cp, SMBIOS_GET8(cp, 0x05)) == 0 &&
119 strncmp(cp + 0x10, SMBIOS_DMI_SIG, SMBIOS_DMI_SIG_LEN) == 0
120 && smbios_checksum(cp + 0x10, 0x0f) == 0) {
121
122 /* Structure Table Length */
123 smbios.length = SMBIOS_GET16(cp, 0x16);
124 /* Structure Table Address */
125 paddr = SMBIOS_GET32(cp, 0x18);
126 /* No of SMBIOS Structures */
127 smbios.count = SMBIOS_GET16(cp, 0x1c);
128 /* SMBIOS BCD Revision */
129 smbios.ver = SMBIOS_GET8(cp, 0x1e);
130 if (smbios.ver != 0) {
131 smbios.major = smbios.ver >> 4;
132 smbios.minor = smbios.ver & 0x0f;
133 if (smbios.major > 9 || smbios.minor > 9)
134 smbios.ver = 0;
135 }
136 if (smbios.ver == 0) {
137 /* SMBIOS Major Version */
138 smbios.major = SMBIOS_GET8(cp, 0x06);
139 /* SMBIOS Minor Version */
140 smbios.minor = SMBIOS_GET8(cp, 0x07);
141 }
142 smbios.ver = (smbios.major << 8) | smbios.minor;
143 smbios.addr = PTOV(paddr);
144 return (cp);
145 }
146 #ifdef _LP64
147 /*
148 * Check for the SMBIOS 64-bit entry point introduced in
149 * version 3.0.
150 *
151 * The table address is a 64-bit physical address that may
152 * appear at any 64-bit address. We only search for
153 * the 64-bit entry point when running a 64-bit application.
154 */
155 if (strncmp(cp, SMBIOS3_SIG, SMBIOS3_SIG_LEN) == 0 &&
156 smbios_checksum(cp, SMBIOS_GET8(cp, 0x06)) == 0) {
157
158 /* SMBIOS Major Version */
159 smbios.major = SMBIOS_GET8(cp, 0x07);
160 /* SMBIOS Minor Version */
161 smbios.minor = SMBIOS_GET8(cp, 0x08);
162 /* Entry Point Revision */
163 smbios.ver = SMBIOS_GET8(cp, 0x0a);
164 /* Structure Table maximum size */
165 smbios.length = SMBIOS_GET32(cp, 0x0c);
166 /* Structure Table Address */
167 paddr = SMBIOS_GET64(cp, 0x10);
168 smbios.addr = PTOV(paddr);
169 /*
170 * Calculate upper limit for structure count,
171 * use size of table header (4 bytes).
172 */
173 smbios.count = smbios.length / 4;
174 return (cp);
175 }
176 #endif
177 }
178 return (NULL);
179 }
180
181 static const char*
smbios_getstring(caddr_t addr,const int offset)182 smbios_getstring(caddr_t addr, const int offset)
183 {
184 caddr_t cp;
185 int i, idx;
186
187 idx = SMBIOS_GET8(addr, offset);
188 if (idx != 0) {
189 cp = SMBIOS_GETSTR(addr);
190 for (i = 1; i < idx; i++)
191 cp += strlen(cp) + 1;
192 return cp;
193 }
194 return (NULL);
195 }
196
197 static void
smbios_setenv(const char * name,caddr_t addr,const int offset)198 smbios_setenv(const char *name, caddr_t addr, const int offset)
199 {
200 const char* val;
201
202 val = smbios_getstring(addr, offset);
203 if (val != NULL)
204 setenv(name, val, 1);
205 }
206
207 #ifdef SMBIOS_SERIAL_NUMBERS
208
209 #define UUID_SIZE 16
210 #define UUID_TYPE uint32_t
211 #define UUID_STEP sizeof(UUID_TYPE)
212 #define UUID_ALL_BITS (UUID_SIZE / UUID_STEP)
213 #define UUID_GET(base, off) (*(UUID_TYPE *)((base) + (off)))
214
215 static void
smbios_setuuid(const char * name,const caddr_t addr,const int ver)216 smbios_setuuid(const char *name, const caddr_t addr, const int ver)
217 {
218 char uuid[37];
219 int byteorder, i, ones, zeros;
220 UUID_TYPE n;
221 uint32_t f1;
222 uint16_t f2, f3;
223
224 for (i = 0, ones = 0, zeros = 0; i < UUID_SIZE; i += UUID_STEP) {
225 n = UUID_GET(addr, i) + 1;
226 if (zeros == 0 && n == 0)
227 ones++;
228 else if (ones == 0 && n == 1)
229 zeros++;
230 else
231 break;
232 }
233
234 if (ones != UUID_ALL_BITS && zeros != UUID_ALL_BITS) {
235 /*
236 * 3.3.2.1 System UUID
237 *
238 * "Although RFC 4122 recommends network byte order for all
239 * fields, the PC industry (including the ACPI, UEFI, and
240 * Microsoft specifications) has consistently used
241 * little-endian byte encoding for the first three fields:
242 * time_low, time_mid, time_hi_and_version. The same encoding,
243 * also known as wire format, should also be used for the
244 * SMBIOS representation of the UUID."
245 *
246 * Note: We use network byte order for backward compatibility
247 * unless SMBIOS version is 2.6+ or little-endian is forced.
248 */
249 #if defined(SMBIOS_LITTLE_ENDIAN_UUID)
250 byteorder = LITTLE_ENDIAN;
251 #elif defined(SMBIOS_NETWORK_ENDIAN_UUID)
252 byteorder = BIG_ENDIAN;
253 #else
254 byteorder = ver < 0x0206 ? BIG_ENDIAN : LITTLE_ENDIAN;
255 #endif
256 if (byteorder != LITTLE_ENDIAN) {
257 f1 = ntohl(SMBIOS_GET32(addr, 0));
258 f2 = ntohs(SMBIOS_GET16(addr, 4));
259 f3 = ntohs(SMBIOS_GET16(addr, 6));
260 } else {
261 f1 = le32toh(SMBIOS_GET32(addr, 0));
262 f2 = le16toh(SMBIOS_GET16(addr, 4));
263 f3 = le16toh(SMBIOS_GET16(addr, 6));
264 }
265 sprintf(uuid,
266 "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
267 f1, f2, f3, SMBIOS_GET8(addr, 8), SMBIOS_GET8(addr, 9),
268 SMBIOS_GET8(addr, 10), SMBIOS_GET8(addr, 11),
269 SMBIOS_GET8(addr, 12), SMBIOS_GET8(addr, 13),
270 SMBIOS_GET8(addr, 14), SMBIOS_GET8(addr, 15));
271 setenv(name, uuid, 1);
272 }
273 }
274
275 #undef UUID_SIZE
276 #undef UUID_TYPE
277 #undef UUID_STEP
278 #undef UUID_ALL_BITS
279 #undef UUID_GET
280
281 #endif
282
283 static caddr_t
smbios_parse_table(const caddr_t addr)284 smbios_parse_table(const caddr_t addr)
285 {
286 caddr_t cp;
287 int proc, size, osize, type;
288
289 type = SMBIOS_GET8(addr, 0); /* 3.1.2 Structure Header Format */
290 switch(type) {
291 case 0: /* 3.3.1 BIOS Information (Type 0) */
292 smbios_setenv("smbios.bios.vendor", addr, 0x04);
293 smbios_setenv("smbios.bios.version", addr, 0x05);
294 smbios_setenv("smbios.bios.reldate", addr, 0x08);
295 break;
296
297 case 1: /* 3.3.2 System Information (Type 1) */
298 smbios_setenv("smbios.system.maker", addr, 0x04);
299 smbios_setenv("smbios.system.product", addr, 0x05);
300 smbios_setenv("smbios.system.version", addr, 0x06);
301 #ifdef SMBIOS_SERIAL_NUMBERS
302 smbios_setenv("smbios.system.serial", addr, 0x07);
303 smbios_setuuid("smbios.system.uuid", addr + 0x08, smbios.ver);
304 #endif
305 if (smbios.major > 2 ||
306 (smbios.major == 2 && smbios.minor >= 4)) {
307 smbios_setenv("smbios.system.sku", addr, 0x19);
308 smbios_setenv("smbios.system.family", addr, 0x1a);
309 }
310 break;
311
312 case 2: /* 3.3.3 Base Board (or Module) Information (Type 2) */
313 smbios_setenv("smbios.planar.maker", addr, 0x04);
314 smbios_setenv("smbios.planar.product", addr, 0x05);
315 smbios_setenv("smbios.planar.version", addr, 0x06);
316 #ifdef SMBIOS_SERIAL_NUMBERS
317 smbios_setenv("smbios.planar.serial", addr, 0x07);
318 smbios_setenv("smbios.planar.tag", addr, 0x08);
319 #endif
320 smbios_setenv("smbios.planar.location", addr, 0x0a);
321 break;
322
323 case 3: /* 3.3.4 System Enclosure or Chassis (Type 3) */
324 smbios_setenv("smbios.chassis.maker", addr, 0x04);
325 smbios_setenv("smbios.chassis.version", addr, 0x06);
326 #ifdef SMBIOS_SERIAL_NUMBERS
327 smbios_setenv("smbios.chassis.serial", addr, 0x07);
328 smbios_setenv("smbios.chassis.tag", addr, 0x08);
329 #endif
330 break;
331
332 case 4: /* 3.3.5 Processor Information (Type 4) */
333 /*
334 * Offset 18h: Processor Status
335 *
336 * Bit 7 Reserved, must be 0
337 * Bit 6 CPU Socket Populated
338 * 1 - CPU Socket Populated
339 * 0 - CPU Socket Unpopulated
340 * Bit 5:3 Reserved, must be zero
341 * Bit 2:0 CPU Status
342 * 0h - Unknown
343 * 1h - CPU Enabled
344 * 2h - CPU Disabled by User via BIOS Setup
345 * 3h - CPU Disabled by BIOS (POST Error)
346 * 4h - CPU is Idle, waiting to be enabled
347 * 5-6h - Reserved
348 * 7h - Other
349 */
350 proc = SMBIOS_GET8(addr, 0x18);
351 if ((proc & 0x07) == 1)
352 smbios.enabled_sockets++;
353 if ((proc & 0x40) != 0)
354 smbios.populated_sockets++;
355 break;
356
357 case 6: /* 3.3.7 Memory Module Information (Type 6, Obsolete) */
358 /*
359 * Offset 0Ah: Enabled Size
360 *
361 * Bit 7 Bank connection
362 * 1 - Double-bank connection
363 * 0 - Single-bank connection
364 * Bit 6:0 Size (n), where 2**n is the size in MB
365 * 7Dh - Not determinable (Installed Size only)
366 * 7Eh - Module is installed, but no memory
367 * has been enabled
368 * 7Fh - Not installed
369 */
370 osize = SMBIOS_GET8(addr, 0x0a) & 0x7f;
371 if (osize > 0 && osize < 22)
372 smbios.old_enabled_memory += 1 << (osize + 10);
373 break;
374
375 case 17: /* 3.3.18 Memory Device (Type 17) */
376 /*
377 * Offset 0Ch: Size
378 *
379 * Bit 15 Granularity
380 * 1 - Value is in kilobytes units
381 * 0 - Value is in megabytes units
382 * Bit 14:0 Size
383 */
384 size = SMBIOS_GET16(addr, 0x0c);
385 if (size != 0 && size != 0xffff)
386 smbios.enabled_memory += (size & 0x8000) != 0 ?
387 (size & 0x7fff) : (size << 10);
388 break;
389
390 default: /* skip other types */
391 break;
392 }
393
394 /* Find structure terminator. */
395 cp = SMBIOS_GETSTR(addr);
396 while (SMBIOS_GET16(cp, 0) != 0)
397 cp++;
398
399 return (cp + 2);
400 }
401
402 static caddr_t
smbios_find_struct(int type)403 smbios_find_struct(int type)
404 {
405 caddr_t dmi;
406 size_t i;
407
408 if (smbios.addr == NULL)
409 return (NULL);
410
411 for (dmi = smbios.addr, i = 0;
412 dmi < smbios.addr + smbios.length && i < smbios.count; i++) {
413 if (SMBIOS_GET8(dmi, 0) == type)
414 return dmi;
415 /* Find structure terminator. */
416 dmi = SMBIOS_GETSTR(dmi);
417 while (SMBIOS_GET16(dmi, 0) != 0)
418 dmi++;
419 dmi += 2;
420 }
421
422 return (NULL);
423 }
424
425 static void
smbios_probe(const caddr_t addr)426 smbios_probe(const caddr_t addr)
427 {
428 caddr_t info;
429 const caddr_t paddr = addr != NULL ? addr : PTOV(SMBIOS_START);
430
431 if (smbios.probed)
432 return;
433 smbios.probed = 1;
434
435 /* Search signatures and validate checksums. */
436 if (smbios_sigsearch(paddr, SMBIOS_LENGTH) == NULL)
437 return;
438
439 /* Get system information from SMBIOS */
440 info = smbios_find_struct(0x00);
441 if (info != NULL) {
442 smbios.bios_vendor = smbios_getstring(info, 0x04);
443 }
444 info = smbios_find_struct(0x01);
445 if (info != NULL) {
446 smbios.maker = smbios_getstring(info, 0x04);
447 smbios.product = smbios_getstring(info, 0x05);
448 }
449 }
450
451 void
smbios_detect(const caddr_t addr)452 smbios_detect(const caddr_t addr)
453 {
454 char buf[16];
455 caddr_t dmi;
456 size_t i;
457
458 smbios_probe(addr);
459 if (smbios.addr == NULL)
460 return;
461
462 for (dmi = smbios.addr, i = 0;
463 dmi < smbios.addr + smbios.length && i < smbios.count; i++)
464 dmi = smbios_parse_table(dmi);
465
466 sprintf(buf, "%d.%d", smbios.major, smbios.minor);
467 setenv("smbios.version", buf, 1);
468 if (smbios.enabled_memory > 0 || smbios.old_enabled_memory > 0) {
469 sprintf(buf, "%u", smbios.enabled_memory > 0 ?
470 smbios.enabled_memory : smbios.old_enabled_memory);
471 setenv("smbios.memory.enabled", buf, 1);
472 }
473 if (smbios.enabled_sockets > 0) {
474 sprintf(buf, "%u", smbios.enabled_sockets);
475 setenv("smbios.socket.enabled", buf, 1);
476 }
477 if (smbios.populated_sockets > 0) {
478 sprintf(buf, "%u", smbios.populated_sockets);
479 setenv("smbios.socket.populated", buf, 1);
480 }
481 }
482
483 static int
smbios_match_str(const char * s1,const char * s2)484 smbios_match_str(const char* s1, const char* s2)
485 {
486 return (s1 == NULL || (s2 != NULL && !strcmp(s1, s2)));
487 }
488
489 int
smbios_match(const char * bios_vendor,const char * maker,const char * product)490 smbios_match(const char* bios_vendor, const char* maker,
491 const char* product)
492 {
493 /* XXXRP currently, only called from non-EFI. */
494 smbios_probe(NULL);
495 return (smbios_match_str(bios_vendor, smbios.bios_vendor) &&
496 smbios_match_str(maker, smbios.maker) &&
497 smbios_match_str(product, smbios.product));
498 }
499