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 <sys/endian.h> 32 33 #define PTOV(x) ptov(x) 34 35 /* Only enable 64-bit entry point if it makes sense */ 36 #if __SIZEOF_POINTER__ > 4 37 #define HAS_SMBV3 1 38 #endif 39 40 /* 41 * Detect SMBIOS and export information about the SMBIOS into the 42 * environment. 43 * 44 * System Management BIOS Reference Specification, v2.6 Final 45 * http://www.dmtf.org/standards/published_documents/DSP0134_2.6.0.pdf 46 * 47 * System Management BIOS (SMBIOS) Reference Specification, 3.6.0 48 * https://www.dmtf.org/sites/default/files/standards/documents/DSP0134_3.6.0.pdf 49 */ 50 51 /* 52 * The first quoted paragraph below can also be found in section 2.1.1 SMBIOS 53 * Structure Table Entry Point of System Management BIOS Reference 54 * Specification, v2.6 Final 55 * 56 * (From System Management BIOS (SMBIOS) Reference Specification, 3.6.0) 57 * 5.2.1 SMBIOS 2.1 (32-bit) Entry Point 58 * 59 * "On non-UEFI systems, the 32-bit SMBIOS Entry Point structure, can be 60 * located by application software by searching for the anchor-string on 61 * paragraph (16-byte) boundaries within the physical memory address 62 * range 000F0000h to 000FFFFFh. This entry point encapsulates an intermediate 63 * anchor string that is used by some existing DMI browsers. 64 * 65 * On UEFI-based systems, the SMBIOS Entry Point structure can be located by 66 * looking in the EFI Configuration Table for the SMBIOS GUID 67 * (SMBIOS_TABLE_GUID, {EB9D2D31-2D88-11D3-9A16-0090273FC14D}) and using the 68 * associated pointer. See section 4.6 of the UEFI Specification for details. 69 * See section 2.3 of the UEFI Specification for how to report the containing 70 * memory type. 71 * 72 * NOTE While the SMBIOS Major and Minor Versions (offsets 06h and 07h) 73 * currently duplicate the information that is present in the SMBIOS BCD 74 * Revision (offset 1Eh), they provide a path for future growth in this 75 * specification. The BCD Revision, for example, provides only a single digit 76 * for each of the major and minor version numbers." 77 * 78 * 5.2.2 SMBIOS 860 3.0 (64-bit) Entry Point 79 * 80 * "On non-UEFI systems, the 64-bit SMBIOS Entry Point structure can be located 81 * by application software by searching for the anchor-string on paragraph 82 * (16-byte) boundaries within the physical memory address range 000F0000h to 83 * 000FFFFFh. 84 * 85 * On UEFI-based systems, the SMBIOS Entry Point structure can be located by 86 * looking in the EFI Configuration Table for the SMBIOS 3.x GUID 87 * (SMBIOS3_TABLE_GUID, {F2FD1544-9794-4A2C-992E-E5BBCF20E394}) and using the 88 * associated pointer. See section 4.6 of the UEFI Specification for details. 89 * See section 2.3 of the UEFI Specification for how to report the containing 90 * memory type." 91 */ 92 #define SMBIOS_START 0xf0000 93 #define SMBIOS_LENGTH 0x10000 94 #define SMBIOS_STEP 0x10 95 #define SMBIOS_SIG "_SM_" 96 #define SMBIOS3_SIG "_SM3_" 97 #define SMBIOS_DMI_SIG "_DMI_" 98 99 #define SMBIOS_GET8(base, off) (*(uint8_t *)((base) + (off))) 100 #define SMBIOS_GET16(base, off) (*(uint16_t *)((base) + (off))) 101 #define SMBIOS_GET32(base, off) (*(uint32_t *)((base) + (off))) 102 #define SMBIOS_GET64(base, off) (*(uint64_t *)((base) + (off))) 103 104 #define SMBIOS_GETLEN(base) SMBIOS_GET8(base, 0x01) 105 #define SMBIOS_GETSTR(base) ((base) + SMBIOS_GETLEN(base)) 106 107 struct smbios_attr { 108 int probed; 109 caddr_t addr; 110 size_t length; 111 size_t count; 112 int major; 113 int minor; 114 int ver; 115 const char* bios_vendor; 116 const char* maker; 117 const char* product; 118 uint32_t enabled_memory; 119 uint32_t old_enabled_memory; 120 uint8_t enabled_sockets; 121 uint8_t populated_sockets; 122 }; 123 124 static struct smbios_attr smbios; 125 #ifdef HAS_SMBV3 126 static int isv3; 127 #endif 128 129 static uint8_t 130 smbios_checksum(const caddr_t addr, const uint8_t len) 131 { 132 uint8_t sum; 133 int i; 134 135 for (sum = 0, i = 0; i < len; i++) 136 sum += SMBIOS_GET8(addr, i); 137 return (sum); 138 } 139 140 static caddr_t 141 smbios_sigsearch(const caddr_t addr, const uint32_t len) 142 { 143 caddr_t cp; 144 145 /* Search on 16-byte boundaries. */ 146 for (cp = addr; cp < addr + len; cp += SMBIOS_STEP) { 147 /* v2.1, 32-bit Entry point */ 148 if (strncmp(cp, SMBIOS_SIG, sizeof(SMBIOS_SIG) - 1) == 0 && 149 smbios_checksum(cp, SMBIOS_GET8(cp, 0x05)) == 0 && 150 strncmp(cp + 0x10, SMBIOS_DMI_SIG, 5) == 0 && 151 smbios_checksum(cp + 0x10, 0x0f) == 0) 152 return (cp); 153 154 #ifdef HAS_SMBV3 155 /* v3.0, 64-bit Entry point */ 156 if (strncmp(cp, SMBIOS3_SIG, sizeof(SMBIOS3_SIG) - 1) == 0 && 157 smbios_checksum(cp, SMBIOS_GET8(cp, 0x06)) == 0) { 158 isv3 = 1; 159 return (cp); 160 } 161 #endif 162 } 163 return (NULL); 164 } 165 166 static const char* 167 smbios_getstring(caddr_t addr, const int offset) 168 { 169 caddr_t cp; 170 int i, idx; 171 172 idx = SMBIOS_GET8(addr, offset); 173 if (idx != 0) { 174 cp = SMBIOS_GETSTR(addr); 175 for (i = 1; i < idx; i++) 176 cp += strlen(cp) + 1; 177 return cp; 178 } 179 return (NULL); 180 } 181 182 static void 183 smbios_setenv(const char *name, caddr_t addr, const int offset) 184 { 185 const char* val; 186 187 val = smbios_getstring(addr, offset); 188 if (val != NULL) 189 setenv(name, val, 1); 190 } 191 192 #ifdef SMBIOS_SERIAL_NUMBERS 193 194 #define UUID_SIZE 16 195 #define UUID_TYPE uint32_t 196 #define UUID_STEP sizeof(UUID_TYPE) 197 #define UUID_ALL_BITS (UUID_SIZE / UUID_STEP) 198 #define UUID_GET(base, off) (*(UUID_TYPE *)((base) + (off))) 199 200 static void 201 smbios_setuuid(const char *name, const caddr_t addr, const int ver) 202 { 203 char uuid[37]; 204 int byteorder, i, ones, zeros; 205 UUID_TYPE n; 206 uint32_t f1; 207 uint16_t f2, f3; 208 209 for (i = 0, ones = 0, zeros = 0; i < UUID_SIZE; i += UUID_STEP) { 210 n = UUID_GET(addr, i) + 1; 211 if (zeros == 0 && n == 0) 212 ones++; 213 else if (ones == 0 && n == 1) 214 zeros++; 215 else 216 break; 217 } 218 219 if (ones != UUID_ALL_BITS && zeros != UUID_ALL_BITS) { 220 /* 221 * 3.3.2.1 System UUID 222 * 223 * "Although RFC 4122 recommends network byte order for all 224 * fields, the PC industry (including the ACPI, UEFI, and 225 * Microsoft specifications) has consistently used 226 * little-endian byte encoding for the first three fields: 227 * time_low, time_mid, time_hi_and_version. The same encoding, 228 * also known as wire format, should also be used for the 229 * SMBIOS representation of the UUID." 230 * 231 * Note: We use network byte order for backward compatibility 232 * unless SMBIOS version is 2.6+ or little-endian is forced. 233 */ 234 #if defined(SMBIOS_LITTLE_ENDIAN_UUID) 235 byteorder = LITTLE_ENDIAN; 236 #elif defined(SMBIOS_NETWORK_ENDIAN_UUID) 237 byteorder = BIG_ENDIAN; 238 #else 239 byteorder = ver < 0x0206 ? BIG_ENDIAN : LITTLE_ENDIAN; 240 #endif 241 if (byteorder != LITTLE_ENDIAN) { 242 f1 = ntohl(SMBIOS_GET32(addr, 0)); 243 f2 = ntohs(SMBIOS_GET16(addr, 4)); 244 f3 = ntohs(SMBIOS_GET16(addr, 6)); 245 } else { 246 f1 = le32toh(SMBIOS_GET32(addr, 0)); 247 f2 = le16toh(SMBIOS_GET16(addr, 4)); 248 f3 = le16toh(SMBIOS_GET16(addr, 6)); 249 } 250 sprintf(uuid, 251 "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x", 252 f1, f2, f3, SMBIOS_GET8(addr, 8), SMBIOS_GET8(addr, 9), 253 SMBIOS_GET8(addr, 10), SMBIOS_GET8(addr, 11), 254 SMBIOS_GET8(addr, 12), SMBIOS_GET8(addr, 13), 255 SMBIOS_GET8(addr, 14), SMBIOS_GET8(addr, 15)); 256 setenv(name, uuid, 1); 257 } 258 } 259 260 #undef UUID_SIZE 261 #undef UUID_TYPE 262 #undef UUID_STEP 263 #undef UUID_ALL_BITS 264 #undef UUID_GET 265 266 #endif 267 268 static const char * 269 smbios_parse_chassis_type(caddr_t addr) 270 { 271 int type; 272 273 type = SMBIOS_GET8(addr, 0x5); 274 switch (type) { 275 case 0x1: 276 return ("Other"); 277 case 0x2: 278 return ("Unknown"); 279 case 0x3: 280 return ("Desktop"); 281 case 0x4: 282 return ("Low Profile Desktop"); 283 case 0x5: 284 return ("Pizza Box"); 285 case 0x6: 286 return ("Mini Tower"); 287 case 0x7: 288 return ("Tower"); 289 case 0x8: 290 return ("Portable"); 291 case 0x9: 292 return ("Laptop"); 293 case 0xA: 294 return ("Notebook"); 295 case 0xB: 296 return ("Hand Held"); 297 case 0xC: 298 return ("Docking Station"); 299 case 0xD: 300 return ("All in One"); 301 case 0xE: 302 return ("Sub Notebook"); 303 case 0xF: 304 return ("Lunch Box"); 305 case 0x10: 306 return ("Space-saving"); 307 case 0x11: 308 return ("Main Server Chassis"); 309 case 0x12: 310 return ("Expansion Chassis"); 311 case 0x13: 312 return ("SubChassis"); 313 case 0x14: 314 return ("Bus Expansion Chassis"); 315 case 0x15: 316 return ("Peripheral Chassis"); 317 case 0x16: 318 return ("RAID Chassis"); 319 case 0x17: 320 return ("Rack Mount Chassis"); 321 case 0x18: 322 return ("Sealed-case PC"); 323 case 0x19: 324 return ("Multi-system chassis"); 325 case 0x1A: 326 return ("Compact PCI"); 327 case 0x1B: 328 return ("Advanced TCA"); 329 case 0x1C: 330 return ("Blade"); 331 case 0x1D: 332 return ("Blade Enclosure"); 333 case 0x1E: 334 return ("Tablet"); 335 case 0x1F: 336 return ("Convertible"); 337 case 0x20: 338 return ("Detachable"); 339 case 0x21: 340 return ("IoT Gateway"); 341 case 0x22: 342 return ("Embedded PC"); 343 case 0x23: 344 return ("Mini PC"); 345 case 0x24: 346 return ("Stick PC"); 347 } 348 349 return ("Undefined"); 350 } 351 352 static caddr_t 353 smbios_parse_table(const caddr_t addr) 354 { 355 caddr_t cp; 356 int proc, size, osize, type; 357 uint8_t bios_minor, bios_major; 358 char buf[16]; 359 360 type = SMBIOS_GET8(addr, 0); /* 3.1.2 Structure Header Format */ 361 switch(type) { 362 case 0: /* 3.3.1 BIOS Information (Type 0) */ 363 smbios_setenv("smbios.bios.vendor", addr, 0x04); 364 smbios_setenv("smbios.bios.version", addr, 0x05); 365 smbios_setenv("smbios.bios.reldate", addr, 0x08); 366 bios_major = SMBIOS_GET8(addr, 0x14); 367 bios_minor = SMBIOS_GET8(addr, 0x15); 368 if (bios_minor != 0xFF && bios_major != 0xFF) { 369 snprintf(buf, sizeof(buf), "%u.%u", 370 bios_major, bios_minor); 371 setenv("smbios.bios.revision", buf, 1); 372 } 373 break; 374 375 case 1: /* 3.3.2 System Information (Type 1) */ 376 smbios_setenv("smbios.system.maker", addr, 0x04); 377 smbios_setenv("smbios.system.product", addr, 0x05); 378 smbios_setenv("smbios.system.version", addr, 0x06); 379 #ifdef SMBIOS_SERIAL_NUMBERS 380 smbios_setenv("smbios.system.serial", addr, 0x07); 381 smbios_setuuid("smbios.system.uuid", addr + 0x08, smbios.ver); 382 #endif 383 if (smbios.major > 2 || 384 (smbios.major == 2 && smbios.minor >= 4)) { 385 smbios_setenv("smbios.system.sku", addr, 0x19); 386 smbios_setenv("smbios.system.family", addr, 0x1a); 387 } 388 break; 389 390 case 2: /* 3.3.3 Base Board (or Module) Information (Type 2) */ 391 smbios_setenv("smbios.planar.maker", addr, 0x04); 392 smbios_setenv("smbios.planar.product", addr, 0x05); 393 smbios_setenv("smbios.planar.version", addr, 0x06); 394 #ifdef SMBIOS_SERIAL_NUMBERS 395 smbios_setenv("smbios.planar.serial", addr, 0x07); 396 smbios_setenv("smbios.planar.tag", addr, 0x08); 397 #endif 398 smbios_setenv("smbios.planar.location", addr, 0x0a); 399 break; 400 401 case 3: /* 3.3.4 System Enclosure or Chassis (Type 3) */ 402 smbios_setenv("smbios.chassis.maker", addr, 0x04); 403 setenv("smbios.chassis.type", smbios_parse_chassis_type(addr), 1); 404 smbios_setenv("smbios.chassis.version", addr, 0x06); 405 #ifdef SMBIOS_SERIAL_NUMBERS 406 smbios_setenv("smbios.chassis.serial", addr, 0x07); 407 smbios_setenv("smbios.chassis.tag", addr, 0x08); 408 #endif 409 break; 410 411 case 4: /* 3.3.5 Processor Information (Type 4) */ 412 /* 413 * Offset 18h: Processor Status 414 * 415 * Bit 7 Reserved, must be 0 416 * Bit 6 CPU Socket Populated 417 * 1 - CPU Socket Populated 418 * 0 - CPU Socket Unpopulated 419 * Bit 5:3 Reserved, must be zero 420 * Bit 2:0 CPU Status 421 * 0h - Unknown 422 * 1h - CPU Enabled 423 * 2h - CPU Disabled by User via BIOS Setup 424 * 3h - CPU Disabled by BIOS (POST Error) 425 * 4h - CPU is Idle, waiting to be enabled 426 * 5-6h - Reserved 427 * 7h - Other 428 */ 429 proc = SMBIOS_GET8(addr, 0x18); 430 if ((proc & 0x07) == 1) 431 smbios.enabled_sockets++; 432 if ((proc & 0x40) != 0) 433 smbios.populated_sockets++; 434 break; 435 436 case 6: /* 3.3.7 Memory Module Information (Type 6, Obsolete) */ 437 /* 438 * Offset 0Ah: Enabled Size 439 * 440 * Bit 7 Bank connection 441 * 1 - Double-bank connection 442 * 0 - Single-bank connection 443 * Bit 6:0 Size (n), where 2**n is the size in MB 444 * 7Dh - Not determinable (Installed Size only) 445 * 7Eh - Module is installed, but no memory 446 * has been enabled 447 * 7Fh - Not installed 448 */ 449 osize = SMBIOS_GET8(addr, 0x0a) & 0x7f; 450 if (osize > 0 && osize < 22) 451 smbios.old_enabled_memory += 1 << (osize + 10); 452 break; 453 454 case 17: /* 3.3.18 Memory Device (Type 17) */ 455 /* 456 * Offset 0Ch: Size 457 * 458 * Bit 15 Granularity 459 * 1 - Value is in kilobytes units 460 * 0 - Value is in megabytes units 461 * Bit 14:0 Size 462 */ 463 size = SMBIOS_GET16(addr, 0x0c); 464 if (size != 0 && size != 0xffff) 465 smbios.enabled_memory += (size & 0x8000) != 0 ? 466 (size & 0x7fff) : (size << 10); 467 break; 468 469 default: /* skip other types */ 470 break; 471 } 472 473 /* Find structure terminator. */ 474 cp = SMBIOS_GETSTR(addr); 475 while (SMBIOS_GET16(cp, 0) != 0) 476 cp++; 477 478 return (cp + 2); 479 } 480 481 static caddr_t 482 smbios_find_struct(int type) 483 { 484 caddr_t dmi; 485 size_t i; 486 487 if (smbios.addr == NULL) 488 return (NULL); 489 490 for (dmi = smbios.addr, i = 0; 491 dmi < smbios.addr + smbios.length && i < smbios.count; i++) { 492 if (SMBIOS_GET8(dmi, 0) == type) 493 return dmi; 494 /* Find structure terminator. */ 495 dmi = SMBIOS_GETSTR(dmi); 496 while (SMBIOS_GET16(dmi, 0) != 0) 497 dmi++; 498 dmi += 2; 499 } 500 501 return (NULL); 502 } 503 504 static void 505 smbios_probe(const caddr_t addr) 506 { 507 caddr_t saddr, info; 508 uintptr_t paddr; 509 int maj_off; 510 int min_off; 511 512 if (smbios.probed) 513 return; 514 smbios.probed = 1; 515 516 /* Search signatures and validate checksums. */ 517 saddr = smbios_sigsearch(addr ? addr : PTOV(SMBIOS_START), 518 SMBIOS_LENGTH); 519 if (saddr == NULL) 520 return; 521 522 #ifdef HAS_SMBV3 523 if (isv3) { 524 smbios.length = SMBIOS_GET32(saddr, 0x0c); /* Structure Table Length */ 525 paddr = SMBIOS_GET64(saddr, 0x10); /* Structure Table Address */ 526 smbios.count = -1; /* not present in V3 */ 527 smbios.ver = 0; /* not present in V3 */ 528 maj_off = 0x07; 529 min_off = 0x08; 530 } else 531 #endif 532 { 533 smbios.length = SMBIOS_GET16(saddr, 0x16); /* Structure Table Length */ 534 paddr = SMBIOS_GET32(saddr, 0x18); /* Structure Table Address */ 535 smbios.count = SMBIOS_GET16(saddr, 0x1c); /* No of SMBIOS Structures */ 536 smbios.ver = SMBIOS_GET8(saddr, 0x1e); /* SMBIOS BCD Revision */ 537 maj_off = 0x06; 538 min_off = 0x07; 539 } 540 541 542 if (smbios.ver != 0) { 543 smbios.major = smbios.ver >> 4; 544 smbios.minor = smbios.ver & 0x0f; 545 if (smbios.major > 9 || smbios.minor > 9) 546 smbios.ver = 0; 547 } 548 if (smbios.ver == 0) { 549 smbios.major = SMBIOS_GET8(saddr, maj_off);/* SMBIOS Major Version */ 550 smbios.minor = SMBIOS_GET8(saddr, min_off);/* SMBIOS Minor Version */ 551 } 552 smbios.ver = (smbios.major << 8) | smbios.minor; 553 smbios.addr = PTOV(paddr); 554 555 /* Get system information from SMBIOS */ 556 info = smbios_find_struct(0x00); 557 if (info != NULL) { 558 smbios.bios_vendor = smbios_getstring(info, 0x04); 559 } 560 info = smbios_find_struct(0x01); 561 if (info != NULL) { 562 smbios.maker = smbios_getstring(info, 0x04); 563 smbios.product = smbios_getstring(info, 0x05); 564 } 565 } 566 567 void 568 smbios_detect(const caddr_t addr) 569 { 570 char buf[16]; 571 caddr_t dmi; 572 size_t i; 573 574 smbios_probe(addr); 575 if (smbios.addr == NULL) 576 return; 577 578 for (dmi = smbios.addr, i = 0; 579 dmi < smbios.addr + smbios.length && i < smbios.count; i++) 580 dmi = smbios_parse_table(dmi); 581 582 sprintf(buf, "%d.%d", smbios.major, smbios.minor); 583 setenv("smbios.version", buf, 1); 584 if (smbios.enabled_memory > 0 || smbios.old_enabled_memory > 0) { 585 sprintf(buf, "%u", smbios.enabled_memory > 0 ? 586 smbios.enabled_memory : smbios.old_enabled_memory); 587 setenv("smbios.memory.enabled", buf, 1); 588 } 589 if (smbios.enabled_sockets > 0) { 590 sprintf(buf, "%u", smbios.enabled_sockets); 591 setenv("smbios.socket.enabled", buf, 1); 592 } 593 if (smbios.populated_sockets > 0) { 594 sprintf(buf, "%u", smbios.populated_sockets); 595 setenv("smbios.socket.populated", buf, 1); 596 } 597 } 598 599 static int 600 smbios_match_str(const char* s1, const char* s2) 601 { 602 return (s1 == NULL || (s2 != NULL && !strcmp(s1, s2))); 603 } 604 605 int 606 smbios_match(const char* bios_vendor, const char* maker, 607 const char* product) 608 { 609 /* XXXRP currently, only called from non-EFI. */ 610 smbios_probe(NULL); 611 return (smbios_match_str(bios_vendor, smbios.bios_vendor) && 612 smbios_match_str(maker, smbios.maker) && 613 smbios_match_str(product, smbios.product)); 614 } 615