1 // SPDX-License-Identifier: GPL-2.0-only 2 3 #define pr_fmt(fmt) "DMI: " fmt 4 5 #include <linux/types.h> 6 #include <linux/string.h> 7 #include <linux/init.h> 8 #include <linux/module.h> 9 #include <linux/ctype.h> 10 #include <linux/dmi.h> 11 #include <linux/efi.h> 12 #include <linux/memblock.h> 13 #include <linux/random.h> 14 #include <asm/dmi.h> 15 #include <linux/unaligned.h> 16 17 #ifndef SMBIOS_ENTRY_POINT_SCAN_START 18 #define SMBIOS_ENTRY_POINT_SCAN_START 0xF0000 19 #endif 20 21 struct kobject *dmi_kobj; 22 EXPORT_SYMBOL_GPL(dmi_kobj); 23 24 /* 25 * DMI stands for "Desktop Management Interface". It is part 26 * of and an antecedent to, SMBIOS, which stands for System 27 * Management BIOS. See further: https://www.dmtf.org/standards 28 */ 29 static const char dmi_empty_string[] = ""; 30 31 static u32 dmi_ver __initdata; 32 static u32 dmi_len; 33 static u16 dmi_num; 34 static u8 smbios_entry_point[32]; 35 static int smbios_entry_point_size; 36 37 /* DMI system identification string used during boot */ 38 static char dmi_ids_string[128] __initdata; 39 40 static struct dmi_memdev_info { 41 const char *device; 42 const char *bank; 43 u64 size; /* bytes */ 44 u16 handle; 45 u8 type; /* DDR2, DDR3, DDR4 etc */ 46 } *dmi_memdev; 47 static int dmi_memdev_nr; 48 static int dmi_memdev_populated_nr __initdata; 49 50 const char *dmi_string_nosave(const struct dmi_header *dm, u8 s) 51 { 52 const u8 *bp = ((u8 *) dm) + dm->length; 53 const u8 *nsp; 54 55 if (s) { 56 while (--s > 0 && *bp) 57 bp += strlen(bp) + 1; 58 59 /* Strings containing only spaces are considered empty */ 60 nsp = bp; 61 while (*nsp == ' ') 62 nsp++; 63 if (*nsp != '\0') 64 return bp; 65 } 66 67 return dmi_empty_string; 68 } 69 EXPORT_SYMBOL_GPL(dmi_string_nosave); 70 71 static const char * __init dmi_string(const struct dmi_header *dm, u8 s) 72 { 73 const char *bp = dmi_string_nosave(dm, s); 74 char *str; 75 size_t len; 76 77 if (bp == dmi_empty_string) 78 return dmi_empty_string; 79 80 len = strlen(bp) + 1; 81 str = dmi_alloc(len); 82 if (str != NULL) 83 strcpy(str, bp); 84 85 return str; 86 } 87 88 /* 89 * We have to be cautious here. We have seen BIOSes with DMI pointers 90 * pointing to completely the wrong place for example 91 */ 92 static void dmi_decode_table(u8 *buf, 93 void (*decode)(const struct dmi_header *, void *), 94 void *private_data) 95 { 96 u8 *data = buf; 97 int i = 0; 98 99 /* 100 * Stop when we have seen all the items the table claimed to have 101 * (SMBIOS < 3.0 only) OR we reach an end-of-table marker (SMBIOS 102 * >= 3.0 only) OR we run off the end of the table (should never 103 * happen but sometimes does on bogus implementations.) 104 */ 105 while ((!dmi_num || i < dmi_num) && 106 (data - buf + sizeof(struct dmi_header)) <= dmi_len) { 107 const struct dmi_header *dm = (const struct dmi_header *)data; 108 109 /* 110 * If a short entry is found (less than 4 bytes), not only it 111 * is invalid, but we cannot reliably locate the next entry. 112 */ 113 if (dm->length < sizeof(struct dmi_header)) { 114 pr_warn(FW_BUG 115 "Corrupted DMI table, offset %zd (only %d entries processed)\n", 116 data - buf, i); 117 break; 118 } 119 120 /* 121 * We want to know the total length (formatted area and 122 * strings) before decoding to make sure we won't run off the 123 * table in dmi_decode or dmi_string 124 */ 125 data += dm->length; 126 while ((data - buf < dmi_len - 1) && (data[0] || data[1])) 127 data++; 128 if (data - buf < dmi_len - 1) 129 decode(dm, private_data); 130 131 data += 2; 132 i++; 133 134 /* 135 * 7.45 End-of-Table (Type 127) [SMBIOS reference spec v3.0.0] 136 * For tables behind a 64-bit entry point, we have no item 137 * count and no exact table length, so stop on end-of-table 138 * marker. For tables behind a 32-bit entry point, we have 139 * seen OEM structures behind the end-of-table marker on 140 * some systems, so don't trust it. 141 */ 142 if (!dmi_num && dm->type == DMI_ENTRY_END_OF_TABLE) 143 break; 144 } 145 146 /* Trim DMI table length if needed */ 147 if (dmi_len > data - buf) 148 dmi_len = data - buf; 149 } 150 151 static phys_addr_t dmi_base; 152 153 static int __init dmi_walk_early(void (*decode)(const struct dmi_header *, 154 void *)) 155 { 156 u8 *buf; 157 u32 orig_dmi_len = dmi_len; 158 159 buf = dmi_early_remap(dmi_base, orig_dmi_len); 160 if (buf == NULL) 161 return -ENOMEM; 162 163 dmi_decode_table(buf, decode, NULL); 164 165 add_device_randomness(buf, dmi_len); 166 167 dmi_early_unmap(buf, orig_dmi_len); 168 return 0; 169 } 170 171 static int __init dmi_checksum(const u8 *buf, u8 len) 172 { 173 u8 sum = 0; 174 int a; 175 176 for (a = 0; a < len; a++) 177 sum += buf[a]; 178 179 return sum == 0; 180 } 181 182 static const char *dmi_ident[DMI_STRING_MAX]; 183 static LIST_HEAD(dmi_devices); 184 int dmi_available; 185 EXPORT_SYMBOL_GPL(dmi_available); 186 187 /* 188 * Save a DMI string 189 */ 190 static void __init dmi_save_ident(const struct dmi_header *dm, int slot, 191 int string) 192 { 193 const char *d = (const char *) dm; 194 const char *p; 195 196 if (dmi_ident[slot] || dm->length <= string) 197 return; 198 199 p = dmi_string(dm, d[string]); 200 if (p == NULL) 201 return; 202 203 dmi_ident[slot] = p; 204 } 205 206 static void __init dmi_save_release(const struct dmi_header *dm, int slot, 207 int index) 208 { 209 const u8 *minor, *major; 210 char *s; 211 212 /* If the table doesn't have the field, let's return */ 213 if (dmi_ident[slot] || dm->length < index) 214 return; 215 216 minor = (u8 *) dm + index; 217 major = (u8 *) dm + index - 1; 218 219 /* As per the spec, if the system doesn't support this field, 220 * the value is FF 221 */ 222 if (*major == 0xFF && *minor == 0xFF) 223 return; 224 225 s = dmi_alloc(8); 226 if (!s) 227 return; 228 229 sprintf(s, "%u.%u", *major, *minor); 230 231 dmi_ident[slot] = s; 232 } 233 234 static void __init dmi_save_uuid(const struct dmi_header *dm, int slot, 235 int index) 236 { 237 const u8 *d; 238 char *s; 239 int is_ff = 1, is_00 = 1, i; 240 241 if (dmi_ident[slot] || dm->length < index + 16) 242 return; 243 244 d = (u8 *) dm + index; 245 for (i = 0; i < 16 && (is_ff || is_00); i++) { 246 if (d[i] != 0x00) 247 is_00 = 0; 248 if (d[i] != 0xFF) 249 is_ff = 0; 250 } 251 252 if (is_ff || is_00) 253 return; 254 255 s = dmi_alloc(16*2+4+1); 256 if (!s) 257 return; 258 259 /* 260 * As of version 2.6 of the SMBIOS specification, the first 3 fields of 261 * the UUID are supposed to be little-endian encoded. The specification 262 * says that this is the defacto standard. 263 */ 264 if (dmi_ver >= 0x020600) 265 sprintf(s, "%pUl", d); 266 else 267 sprintf(s, "%pUb", d); 268 269 dmi_ident[slot] = s; 270 } 271 272 static void __init dmi_save_type(const struct dmi_header *dm, int slot, 273 int index) 274 { 275 const u8 *d; 276 char *s; 277 278 if (dmi_ident[slot] || dm->length <= index) 279 return; 280 281 s = dmi_alloc(4); 282 if (!s) 283 return; 284 285 d = (u8 *) dm + index; 286 sprintf(s, "%u", *d & 0x7F); 287 dmi_ident[slot] = s; 288 } 289 290 static void __init dmi_save_one_device(int type, const char *name) 291 { 292 struct dmi_device *dev; 293 294 /* No duplicate device */ 295 if (dmi_find_device(type, name, NULL)) 296 return; 297 298 dev = dmi_alloc(sizeof(*dev) + strlen(name) + 1); 299 if (!dev) 300 return; 301 302 dev->type = type; 303 strcpy((char *)(dev + 1), name); 304 dev->name = (char *)(dev + 1); 305 dev->device_data = NULL; 306 list_add(&dev->list, &dmi_devices); 307 } 308 309 static void __init dmi_save_devices(const struct dmi_header *dm) 310 { 311 int i, count = (dm->length - sizeof(struct dmi_header)) / 2; 312 313 for (i = 0; i < count; i++) { 314 const char *d = (char *)(dm + 1) + (i * 2); 315 316 /* Skip disabled device */ 317 if ((*d & 0x80) == 0) 318 continue; 319 320 dmi_save_one_device(*d & 0x7f, dmi_string_nosave(dm, *(d + 1))); 321 } 322 } 323 324 static void __init dmi_save_oem_strings_devices(const struct dmi_header *dm) 325 { 326 int i, count; 327 struct dmi_device *dev; 328 329 if (dm->length < 0x05) 330 return; 331 332 count = *(u8 *)(dm + 1); 333 for (i = 1; i <= count; i++) { 334 const char *devname = dmi_string(dm, i); 335 336 if (devname == dmi_empty_string) 337 continue; 338 339 dev = dmi_alloc(sizeof(*dev)); 340 if (!dev) 341 break; 342 343 dev->type = DMI_DEV_TYPE_OEM_STRING; 344 dev->name = devname; 345 dev->device_data = NULL; 346 347 list_add(&dev->list, &dmi_devices); 348 } 349 } 350 351 static void __init dmi_save_ipmi_device(const struct dmi_header *dm) 352 { 353 struct dmi_device *dev; 354 void *data; 355 356 data = dmi_alloc(dm->length); 357 if (data == NULL) 358 return; 359 360 memcpy(data, dm, dm->length); 361 362 dev = dmi_alloc(sizeof(*dev)); 363 if (!dev) 364 return; 365 366 dev->type = DMI_DEV_TYPE_IPMI; 367 dev->name = "IPMI controller"; 368 dev->device_data = data; 369 370 list_add_tail(&dev->list, &dmi_devices); 371 } 372 373 static void __init dmi_save_dev_pciaddr(int instance, int segment, int bus, 374 int devfn, const char *name, int type) 375 { 376 struct dmi_dev_onboard *dev; 377 378 /* Ignore invalid values */ 379 if (type == DMI_DEV_TYPE_DEV_SLOT && 380 segment == 0xFFFF && bus == 0xFF && devfn == 0xFF) 381 return; 382 383 dev = dmi_alloc(sizeof(*dev) + strlen(name) + 1); 384 if (!dev) 385 return; 386 387 dev->instance = instance; 388 dev->segment = segment; 389 dev->bus = bus; 390 dev->devfn = devfn; 391 392 strcpy((char *)&dev[1], name); 393 dev->dev.type = type; 394 dev->dev.name = (char *)&dev[1]; 395 dev->dev.device_data = dev; 396 397 list_add(&dev->dev.list, &dmi_devices); 398 } 399 400 static void __init dmi_save_extended_devices(const struct dmi_header *dm) 401 { 402 const char *name; 403 const u8 *d = (u8 *)dm; 404 405 if (dm->length < 0x0B) 406 return; 407 408 /* Skip disabled device */ 409 if ((d[0x5] & 0x80) == 0) 410 return; 411 412 name = dmi_string_nosave(dm, d[0x4]); 413 dmi_save_dev_pciaddr(d[0x6], *(u16 *)(d + 0x7), d[0x9], d[0xA], name, 414 DMI_DEV_TYPE_DEV_ONBOARD); 415 dmi_save_one_device(d[0x5] & 0x7f, name); 416 } 417 418 static void __init dmi_save_system_slot(const struct dmi_header *dm) 419 { 420 const u8 *d = (u8 *)dm; 421 422 /* Need SMBIOS 2.6+ structure */ 423 if (dm->length < 0x11) 424 return; 425 dmi_save_dev_pciaddr(*(u16 *)(d + 0x9), *(u16 *)(d + 0xD), d[0xF], 426 d[0x10], dmi_string_nosave(dm, d[0x4]), 427 DMI_DEV_TYPE_DEV_SLOT); 428 } 429 430 static void __init count_mem_devices(const struct dmi_header *dm, void *v) 431 { 432 if (dm->type != DMI_ENTRY_MEM_DEVICE) 433 return; 434 dmi_memdev_nr++; 435 } 436 437 static void __init save_mem_devices(const struct dmi_header *dm, void *v) 438 { 439 const char *d = (const char *)dm; 440 static int nr; 441 u64 bytes; 442 u16 size; 443 444 if (dm->type != DMI_ENTRY_MEM_DEVICE || dm->length < 0x13) 445 return; 446 if (nr >= dmi_memdev_nr) { 447 pr_warn(FW_BUG "Too many DIMM entries in SMBIOS table\n"); 448 return; 449 } 450 dmi_memdev[nr].handle = get_unaligned(&dm->handle); 451 dmi_memdev[nr].device = dmi_string(dm, d[0x10]); 452 dmi_memdev[nr].bank = dmi_string(dm, d[0x11]); 453 dmi_memdev[nr].type = d[0x12]; 454 455 size = get_unaligned((u16 *)&d[0xC]); 456 if (size == 0) 457 bytes = 0; 458 else if (size == 0xffff) 459 bytes = ~0ull; 460 else if (size & 0x8000) 461 bytes = (u64)(size & 0x7fff) << 10; 462 else if (size != 0x7fff || dm->length < 0x20) 463 bytes = (u64)size << 20; 464 else 465 bytes = (u64)get_unaligned((u32 *)&d[0x1C]) << 20; 466 467 if (bytes) 468 dmi_memdev_populated_nr++; 469 470 dmi_memdev[nr].size = bytes; 471 nr++; 472 } 473 474 static void __init dmi_memdev_walk(void) 475 { 476 if (dmi_walk_early(count_mem_devices) == 0 && dmi_memdev_nr) { 477 dmi_memdev = dmi_alloc(sizeof(*dmi_memdev) * dmi_memdev_nr); 478 if (dmi_memdev) 479 dmi_walk_early(save_mem_devices); 480 } 481 } 482 483 /* 484 * Process a DMI table entry. Right now all we care about are the BIOS 485 * and machine entries. For 2.5 we should pull the smbus controller info 486 * out of here. 487 */ 488 static void __init dmi_decode(const struct dmi_header *dm, void *dummy) 489 { 490 switch (dm->type) { 491 case DMI_ENTRY_BIOS: 492 dmi_save_ident(dm, DMI_BIOS_VENDOR, 4); 493 dmi_save_ident(dm, DMI_BIOS_VERSION, 5); 494 dmi_save_ident(dm, DMI_BIOS_DATE, 8); 495 dmi_save_release(dm, DMI_BIOS_RELEASE, 21); 496 dmi_save_release(dm, DMI_EC_FIRMWARE_RELEASE, 23); 497 break; 498 case DMI_ENTRY_SYSTEM: 499 dmi_save_ident(dm, DMI_SYS_VENDOR, 4); 500 dmi_save_ident(dm, DMI_PRODUCT_NAME, 5); 501 dmi_save_ident(dm, DMI_PRODUCT_VERSION, 6); 502 dmi_save_ident(dm, DMI_PRODUCT_SERIAL, 7); 503 dmi_save_uuid(dm, DMI_PRODUCT_UUID, 8); 504 dmi_save_ident(dm, DMI_PRODUCT_SKU, 25); 505 dmi_save_ident(dm, DMI_PRODUCT_FAMILY, 26); 506 break; 507 case DMI_ENTRY_BASEBOARD: 508 dmi_save_ident(dm, DMI_BOARD_VENDOR, 4); 509 dmi_save_ident(dm, DMI_BOARD_NAME, 5); 510 dmi_save_ident(dm, DMI_BOARD_VERSION, 6); 511 dmi_save_ident(dm, DMI_BOARD_SERIAL, 7); 512 dmi_save_ident(dm, DMI_BOARD_ASSET_TAG, 8); 513 break; 514 case DMI_ENTRY_CHASSIS: 515 dmi_save_ident(dm, DMI_CHASSIS_VENDOR, 4); 516 dmi_save_type(dm, DMI_CHASSIS_TYPE, 5); 517 dmi_save_ident(dm, DMI_CHASSIS_VERSION, 6); 518 dmi_save_ident(dm, DMI_CHASSIS_SERIAL, 7); 519 dmi_save_ident(dm, DMI_CHASSIS_ASSET_TAG, 8); 520 break; 521 case DMI_ENTRY_SYSTEM_SLOT: 522 dmi_save_system_slot(dm); 523 break; 524 case DMI_ENTRY_ONBOARD_DEVICE: 525 dmi_save_devices(dm); 526 break; 527 case DMI_ENTRY_OEMSTRINGS: 528 dmi_save_oem_strings_devices(dm); 529 break; 530 case DMI_ENTRY_IPMI_DEV: 531 dmi_save_ipmi_device(dm); 532 break; 533 case DMI_ENTRY_ONBOARD_DEV_EXT: 534 dmi_save_extended_devices(dm); 535 } 536 } 537 538 static int __init print_filtered(char *buf, size_t len, const char *info) 539 { 540 int c = 0; 541 const char *p; 542 543 if (!info) 544 return c; 545 546 for (p = info; *p; p++) 547 if (isprint(*p)) 548 c += scnprintf(buf + c, len - c, "%c", *p); 549 else 550 c += scnprintf(buf + c, len - c, "\\x%02x", *p & 0xff); 551 return c; 552 } 553 554 static void __init dmi_format_ids(char *buf, size_t len) 555 { 556 int c = 0; 557 const char *board; /* Board Name is optional */ 558 559 c += print_filtered(buf + c, len - c, 560 dmi_get_system_info(DMI_SYS_VENDOR)); 561 c += scnprintf(buf + c, len - c, " "); 562 c += print_filtered(buf + c, len - c, 563 dmi_get_system_info(DMI_PRODUCT_NAME)); 564 565 board = dmi_get_system_info(DMI_BOARD_NAME); 566 if (board) { 567 c += scnprintf(buf + c, len - c, "/"); 568 c += print_filtered(buf + c, len - c, board); 569 } 570 c += scnprintf(buf + c, len - c, ", BIOS "); 571 c += print_filtered(buf + c, len - c, 572 dmi_get_system_info(DMI_BIOS_VERSION)); 573 c += scnprintf(buf + c, len - c, " "); 574 c += print_filtered(buf + c, len - c, 575 dmi_get_system_info(DMI_BIOS_DATE)); 576 } 577 578 /* 579 * Check for DMI/SMBIOS headers in the system firmware image. Any 580 * SMBIOS header must start 16 bytes before the DMI header, so take a 581 * 32 byte buffer and check for DMI at offset 16 and SMBIOS at offset 582 * 0. If the DMI header is present, set dmi_ver accordingly (SMBIOS 583 * takes precedence) and return 0. Otherwise return 1. 584 */ 585 static int __init dmi_present(const u8 *buf) 586 { 587 u32 smbios_ver; 588 589 /* 590 * The size of this structure is 31 bytes, but we also accept value 591 * 30 due to a mistake in SMBIOS specification version 2.1. 592 */ 593 if (memcmp(buf, "_SM_", 4) == 0 && 594 buf[5] >= 30 && buf[5] <= 32 && 595 dmi_checksum(buf, buf[5])) { 596 smbios_ver = get_unaligned_be16(buf + 6); 597 smbios_entry_point_size = buf[5]; 598 memcpy(smbios_entry_point, buf, smbios_entry_point_size); 599 600 /* Some BIOS report weird SMBIOS version, fix that up */ 601 switch (smbios_ver) { 602 case 0x021F: 603 case 0x0221: 604 pr_debug("SMBIOS version fixup (2.%d->2.%d)\n", 605 smbios_ver & 0xFF, 3); 606 smbios_ver = 0x0203; 607 break; 608 case 0x0233: 609 pr_debug("SMBIOS version fixup (2.%d->2.%d)\n", 51, 6); 610 smbios_ver = 0x0206; 611 break; 612 } 613 } else { 614 smbios_ver = 0; 615 } 616 617 buf += 16; 618 619 if (memcmp(buf, "_DMI_", 5) == 0 && dmi_checksum(buf, 15)) { 620 if (smbios_ver) 621 dmi_ver = smbios_ver; 622 else 623 dmi_ver = (buf[14] & 0xF0) << 4 | (buf[14] & 0x0F); 624 dmi_ver <<= 8; 625 dmi_num = get_unaligned_le16(buf + 12); 626 dmi_len = get_unaligned_le16(buf + 6); 627 dmi_base = get_unaligned_le32(buf + 8); 628 629 if (dmi_walk_early(dmi_decode) == 0) { 630 if (smbios_ver) { 631 pr_info("SMBIOS %d.%d present.\n", 632 dmi_ver >> 16, (dmi_ver >> 8) & 0xFF); 633 } else { 634 smbios_entry_point_size = 15; 635 memcpy(smbios_entry_point, buf, 636 smbios_entry_point_size); 637 pr_info("Legacy DMI %d.%d present.\n", 638 dmi_ver >> 16, (dmi_ver >> 8) & 0xFF); 639 } 640 dmi_format_ids(dmi_ids_string, sizeof(dmi_ids_string)); 641 pr_info("%s\n", dmi_ids_string); 642 return 0; 643 } 644 } 645 646 return 1; 647 } 648 649 /* 650 * Check for the SMBIOS 3.0 64-bit entry point signature. Unlike the legacy 651 * 32-bit entry point, there is no embedded DMI header (_DMI_) in here. 652 */ 653 static int __init dmi_smbios3_present(const u8 *buf) 654 { 655 if (memcmp(buf, "_SM3_", 5) == 0 && 656 buf[6] >= 24 && buf[6] <= 32 && 657 dmi_checksum(buf, buf[6])) { 658 dmi_ver = get_unaligned_be24(buf + 7); 659 dmi_num = 0; /* No longer specified */ 660 dmi_len = get_unaligned_le32(buf + 12); 661 dmi_base = get_unaligned_le64(buf + 16); 662 smbios_entry_point_size = buf[6]; 663 memcpy(smbios_entry_point, buf, smbios_entry_point_size); 664 665 if (dmi_walk_early(dmi_decode) == 0) { 666 pr_info("SMBIOS %d.%d.%d present.\n", 667 dmi_ver >> 16, (dmi_ver >> 8) & 0xFF, 668 dmi_ver & 0xFF); 669 dmi_format_ids(dmi_ids_string, sizeof(dmi_ids_string)); 670 pr_info("%s\n", dmi_ids_string); 671 return 0; 672 } 673 } 674 return 1; 675 } 676 677 static void __init dmi_scan_machine(void) 678 { 679 char __iomem *p, *q; 680 char buf[32]; 681 682 if (efi_enabled(EFI_CONFIG_TABLES)) { 683 /* 684 * According to the DMTF SMBIOS reference spec v3.0.0, it is 685 * allowed to define both the 64-bit entry point (smbios3) and 686 * the 32-bit entry point (smbios), in which case they should 687 * either both point to the same SMBIOS structure table, or the 688 * table pointed to by the 64-bit entry point should contain a 689 * superset of the table contents pointed to by the 32-bit entry 690 * point (section 5.2) 691 * This implies that the 64-bit entry point should have 692 * precedence if it is defined and supported by the OS. If we 693 * have the 64-bit entry point, but fail to decode it, fall 694 * back to the legacy one (if available) 695 */ 696 if (efi.smbios3 != EFI_INVALID_TABLE_ADDR) { 697 p = dmi_early_remap(efi.smbios3, 32); 698 if (p == NULL) 699 goto error; 700 memcpy_fromio(buf, p, 32); 701 dmi_early_unmap(p, 32); 702 703 if (!dmi_smbios3_present(buf)) { 704 dmi_available = 1; 705 return; 706 } 707 } 708 if (efi.smbios == EFI_INVALID_TABLE_ADDR) 709 goto error; 710 711 /* This is called as a core_initcall() because it isn't 712 * needed during early boot. This also means we can 713 * iounmap the space when we're done with it. 714 */ 715 p = dmi_early_remap(efi.smbios, 32); 716 if (p == NULL) 717 goto error; 718 memcpy_fromio(buf, p, 32); 719 dmi_early_unmap(p, 32); 720 721 if (!dmi_present(buf)) { 722 dmi_available = 1; 723 return; 724 } 725 } else if (IS_ENABLED(CONFIG_DMI_SCAN_MACHINE_NON_EFI_FALLBACK)) { 726 p = dmi_early_remap(SMBIOS_ENTRY_POINT_SCAN_START, 0x10000); 727 if (p == NULL) 728 goto error; 729 730 /* 731 * Same logic as above, look for a 64-bit entry point 732 * first, and if not found, fall back to 32-bit entry point. 733 */ 734 memcpy_fromio(buf, p, 16); 735 for (q = p + 16; q < p + 0x10000; q += 16) { 736 memcpy_fromio(buf + 16, q, 16); 737 if (!dmi_smbios3_present(buf)) { 738 dmi_available = 1; 739 dmi_early_unmap(p, 0x10000); 740 return; 741 } 742 memcpy(buf, buf + 16, 16); 743 } 744 745 /* 746 * Iterate over all possible DMI header addresses q. 747 * Maintain the 32 bytes around q in buf. On the 748 * first iteration, substitute zero for the 749 * out-of-range bytes so there is no chance of falsely 750 * detecting an SMBIOS header. 751 */ 752 memset(buf, 0, 16); 753 for (q = p; q < p + 0x10000; q += 16) { 754 memcpy_fromio(buf + 16, q, 16); 755 if (!dmi_present(buf)) { 756 dmi_available = 1; 757 dmi_early_unmap(p, 0x10000); 758 return; 759 } 760 memcpy(buf, buf + 16, 16); 761 } 762 dmi_early_unmap(p, 0x10000); 763 } 764 error: 765 pr_info("not present or invalid.\n"); 766 } 767 768 static __ro_after_init BIN_ATTR_SIMPLE_ADMIN_RO(smbios_entry_point); 769 static __ro_after_init BIN_ATTR_SIMPLE_ADMIN_RO(DMI); 770 771 static int __init dmi_init(void) 772 { 773 struct kobject *tables_kobj; 774 u8 *dmi_table; 775 int ret = -ENOMEM; 776 777 if (!dmi_available) 778 return 0; 779 780 /* 781 * Set up dmi directory at /sys/firmware/dmi. This entry should stay 782 * even after farther error, as it can be used by other modules like 783 * dmi-sysfs. 784 */ 785 dmi_kobj = kobject_create_and_add("dmi", firmware_kobj); 786 if (!dmi_kobj) 787 goto err; 788 789 tables_kobj = kobject_create_and_add("tables", dmi_kobj); 790 if (!tables_kobj) 791 goto err; 792 793 dmi_table = dmi_remap(dmi_base, dmi_len); 794 if (!dmi_table) 795 goto err_tables; 796 797 bin_attr_smbios_entry_point.size = smbios_entry_point_size; 798 bin_attr_smbios_entry_point.private = smbios_entry_point; 799 ret = sysfs_create_bin_file(tables_kobj, &bin_attr_smbios_entry_point); 800 if (ret) 801 goto err_unmap; 802 803 bin_attr_DMI.size = dmi_len; 804 bin_attr_DMI.private = dmi_table; 805 ret = sysfs_create_bin_file(tables_kobj, &bin_attr_DMI); 806 if (!ret) 807 return 0; 808 809 sysfs_remove_bin_file(tables_kobj, 810 &bin_attr_smbios_entry_point); 811 err_unmap: 812 dmi_unmap(dmi_table); 813 err_tables: 814 kobject_del(tables_kobj); 815 kobject_put(tables_kobj); 816 err: 817 pr_err("Firmware registration failed.\n"); 818 819 return ret; 820 } 821 subsys_initcall(dmi_init); 822 823 /** 824 * dmi_setup - scan and setup DMI system information 825 * 826 * Scan the DMI system information. This setups DMI identifiers 827 * (dmi_system_id) for printing it out on task dumps and prepares 828 * DIMM entry information (dmi_memdev_info) from the SMBIOS table 829 * for using this when reporting memory errors. 830 */ 831 void __init dmi_setup(void) 832 { 833 dmi_scan_machine(); 834 if (!dmi_available) 835 return; 836 837 dmi_memdev_walk(); 838 pr_info("Memory slots populated: %d/%d\n", 839 dmi_memdev_populated_nr, dmi_memdev_nr); 840 dump_stack_set_arch_desc("%s", dmi_ids_string); 841 } 842 843 /** 844 * dmi_matches - check if dmi_system_id structure matches system DMI data 845 * @dmi: pointer to the dmi_system_id structure to check 846 */ 847 static bool dmi_matches(const struct dmi_system_id *dmi) 848 { 849 int i; 850 851 for (i = 0; i < ARRAY_SIZE(dmi->matches); i++) { 852 int s = dmi->matches[i].slot; 853 if (s == DMI_NONE) 854 break; 855 if (s == DMI_OEM_STRING) { 856 /* DMI_OEM_STRING must be exact match */ 857 const struct dmi_device *valid; 858 859 valid = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, 860 dmi->matches[i].substr, NULL); 861 if (valid) 862 continue; 863 } else if (dmi_ident[s]) { 864 if (dmi->matches[i].exact_match) { 865 if (!strcmp(dmi_ident[s], 866 dmi->matches[i].substr)) 867 continue; 868 } else { 869 if (strstr(dmi_ident[s], 870 dmi->matches[i].substr)) 871 continue; 872 } 873 } 874 875 /* No match */ 876 return false; 877 } 878 return true; 879 } 880 881 /** 882 * dmi_is_end_of_table - check for end-of-table marker 883 * @dmi: pointer to the dmi_system_id structure to check 884 */ 885 static bool dmi_is_end_of_table(const struct dmi_system_id *dmi) 886 { 887 return dmi->matches[0].slot == DMI_NONE; 888 } 889 890 /** 891 * dmi_check_system - check system DMI data 892 * @list: array of dmi_system_id structures to match against 893 * All non-null elements of the list must match 894 * their slot's (field index's) data (i.e., each 895 * list string must be a substring of the specified 896 * DMI slot's string data) to be considered a 897 * successful match. 898 * 899 * Walk the blacklist table running matching functions until someone 900 * returns non zero or we hit the end. Callback function is called for 901 * each successful match. Returns the number of matches. 902 * 903 * dmi_setup must be called before this function is called. 904 */ 905 int dmi_check_system(const struct dmi_system_id *list) 906 { 907 int count = 0; 908 const struct dmi_system_id *d; 909 910 for (d = list; !dmi_is_end_of_table(d); d++) 911 if (dmi_matches(d)) { 912 count++; 913 if (d->callback && d->callback(d)) 914 break; 915 } 916 917 return count; 918 } 919 EXPORT_SYMBOL(dmi_check_system); 920 921 /** 922 * dmi_first_match - find dmi_system_id structure matching system DMI data 923 * @list: array of dmi_system_id structures to match against 924 * All non-null elements of the list must match 925 * their slot's (field index's) data (i.e., each 926 * list string must be a substring of the specified 927 * DMI slot's string data) to be considered a 928 * successful match. 929 * 930 * Walk the blacklist table until the first match is found. Return the 931 * pointer to the matching entry or NULL if there's no match. 932 * 933 * dmi_setup must be called before this function is called. 934 */ 935 const struct dmi_system_id *dmi_first_match(const struct dmi_system_id *list) 936 { 937 const struct dmi_system_id *d; 938 939 for (d = list; !dmi_is_end_of_table(d); d++) 940 if (dmi_matches(d)) 941 return d; 942 943 return NULL; 944 } 945 EXPORT_SYMBOL(dmi_first_match); 946 947 /** 948 * dmi_get_system_info - return DMI data value 949 * @field: data index (see enum dmi_field) 950 * 951 * Returns one DMI data value, can be used to perform 952 * complex DMI data checks. 953 */ 954 const char *dmi_get_system_info(int field) 955 { 956 return dmi_ident[field]; 957 } 958 EXPORT_SYMBOL(dmi_get_system_info); 959 960 /** 961 * dmi_name_in_serial - Check if string is in the DMI product serial information 962 * @str: string to check for 963 */ 964 int dmi_name_in_serial(const char *str) 965 { 966 int f = DMI_PRODUCT_SERIAL; 967 if (dmi_ident[f] && strstr(dmi_ident[f], str)) 968 return 1; 969 return 0; 970 } 971 972 /** 973 * dmi_name_in_vendors - Check if string is in the DMI system or board vendor name 974 * @str: Case sensitive Name 975 */ 976 int dmi_name_in_vendors(const char *str) 977 { 978 static int fields[] = { DMI_SYS_VENDOR, DMI_BOARD_VENDOR, DMI_NONE }; 979 int i; 980 for (i = 0; fields[i] != DMI_NONE; i++) { 981 int f = fields[i]; 982 if (dmi_ident[f] && strstr(dmi_ident[f], str)) 983 return 1; 984 } 985 return 0; 986 } 987 EXPORT_SYMBOL(dmi_name_in_vendors); 988 989 /** 990 * dmi_find_device - find onboard device by type/name 991 * @type: device type or %DMI_DEV_TYPE_ANY to match all device types 992 * @name: device name string or %NULL to match all 993 * @from: previous device found in search, or %NULL for new search. 994 * 995 * Iterates through the list of known onboard devices. If a device is 996 * found with a matching @type and @name, a pointer to its device 997 * structure is returned. Otherwise, %NULL is returned. 998 * A new search is initiated by passing %NULL as the @from argument. 999 * If @from is not %NULL, searches continue from next device. 1000 */ 1001 const struct dmi_device *dmi_find_device(int type, const char *name, 1002 const struct dmi_device *from) 1003 { 1004 const struct list_head *head = from ? &from->list : &dmi_devices; 1005 struct list_head *d; 1006 1007 for (d = head->next; d != &dmi_devices; d = d->next) { 1008 const struct dmi_device *dev = 1009 list_entry(d, struct dmi_device, list); 1010 1011 if (((type == DMI_DEV_TYPE_ANY) || (dev->type == type)) && 1012 ((name == NULL) || (strcmp(dev->name, name) == 0))) 1013 return dev; 1014 } 1015 1016 return NULL; 1017 } 1018 EXPORT_SYMBOL(dmi_find_device); 1019 1020 /** 1021 * dmi_get_date - parse a DMI date 1022 * @field: data index (see enum dmi_field) 1023 * @yearp: optional out parameter for the year 1024 * @monthp: optional out parameter for the month 1025 * @dayp: optional out parameter for the day 1026 * 1027 * The date field is assumed to be in the form resembling 1028 * [mm[/dd]]/yy[yy] and the result is stored in the out 1029 * parameters any or all of which can be omitted. 1030 * 1031 * If the field doesn't exist, all out parameters are set to zero 1032 * and false is returned. Otherwise, true is returned with any 1033 * invalid part of date set to zero. 1034 * 1035 * On return, year, month and day are guaranteed to be in the 1036 * range of [0,9999], [0,12] and [0,31] respectively. 1037 */ 1038 bool dmi_get_date(int field, int *yearp, int *monthp, int *dayp) 1039 { 1040 int year = 0, month = 0, day = 0; 1041 bool exists; 1042 const char *s, *y; 1043 char *e; 1044 1045 s = dmi_get_system_info(field); 1046 exists = s; 1047 if (!exists) 1048 goto out; 1049 1050 /* 1051 * Determine year first. We assume the date string resembles 1052 * mm/dd/yy[yy] but the original code extracted only the year 1053 * from the end. Keep the behavior in the spirit of no 1054 * surprises. 1055 */ 1056 y = strrchr(s, '/'); 1057 if (!y) 1058 goto out; 1059 1060 y++; 1061 year = simple_strtoul(y, &e, 10); 1062 if (y != e && year < 100) { /* 2-digit year */ 1063 year += 1900; 1064 if (year < 1996) /* no dates < spec 1.0 */ 1065 year += 100; 1066 } 1067 if (year > 9999) /* year should fit in %04d */ 1068 year = 0; 1069 1070 /* parse the mm and dd */ 1071 month = simple_strtoul(s, &e, 10); 1072 if (s == e || *e != '/' || !month || month > 12) { 1073 month = 0; 1074 goto out; 1075 } 1076 1077 s = e + 1; 1078 day = simple_strtoul(s, &e, 10); 1079 if (s == y || s == e || *e != '/' || day > 31) 1080 day = 0; 1081 out: 1082 if (yearp) 1083 *yearp = year; 1084 if (monthp) 1085 *monthp = month; 1086 if (dayp) 1087 *dayp = day; 1088 return exists; 1089 } 1090 EXPORT_SYMBOL(dmi_get_date); 1091 1092 /** 1093 * dmi_get_bios_year - get a year out of DMI_BIOS_DATE field 1094 * 1095 * Returns year on success, -ENXIO if DMI is not selected, 1096 * or a different negative error code if DMI field is not present 1097 * or not parseable. 1098 */ 1099 int dmi_get_bios_year(void) 1100 { 1101 bool exists; 1102 int year; 1103 1104 exists = dmi_get_date(DMI_BIOS_DATE, &year, NULL, NULL); 1105 if (!exists) 1106 return -ENODATA; 1107 1108 return year ? year : -ERANGE; 1109 } 1110 EXPORT_SYMBOL(dmi_get_bios_year); 1111 1112 /** 1113 * dmi_walk - Walk the DMI table and get called back for every record 1114 * @decode: Callback function 1115 * @private_data: Private data to be passed to the callback function 1116 * 1117 * Returns 0 on success, -ENXIO if DMI is not selected or not present, 1118 * or a different negative error code if DMI walking fails. 1119 */ 1120 int dmi_walk(void (*decode)(const struct dmi_header *, void *), 1121 void *private_data) 1122 { 1123 u8 *buf; 1124 1125 if (!dmi_available) 1126 return -ENXIO; 1127 1128 buf = dmi_remap(dmi_base, dmi_len); 1129 if (buf == NULL) 1130 return -ENOMEM; 1131 1132 dmi_decode_table(buf, decode, private_data); 1133 1134 dmi_unmap(buf); 1135 return 0; 1136 } 1137 EXPORT_SYMBOL_GPL(dmi_walk); 1138 1139 /** 1140 * dmi_match - compare a string to the dmi field (if exists) 1141 * @f: DMI field identifier 1142 * @str: string to compare the DMI field to 1143 * 1144 * Returns true if the requested field equals to the str (including NULL). 1145 */ 1146 bool dmi_match(enum dmi_field f, const char *str) 1147 { 1148 const char *info = dmi_get_system_info(f); 1149 1150 if (info == NULL || str == NULL) 1151 return info == str; 1152 1153 return !strcmp(info, str); 1154 } 1155 EXPORT_SYMBOL_GPL(dmi_match); 1156 1157 void dmi_memdev_name(u16 handle, const char **bank, const char **device) 1158 { 1159 int n; 1160 1161 if (dmi_memdev == NULL) 1162 return; 1163 1164 for (n = 0; n < dmi_memdev_nr; n++) { 1165 if (handle == dmi_memdev[n].handle) { 1166 *bank = dmi_memdev[n].bank; 1167 *device = dmi_memdev[n].device; 1168 break; 1169 } 1170 } 1171 } 1172 EXPORT_SYMBOL_GPL(dmi_memdev_name); 1173 1174 u64 dmi_memdev_size(u16 handle) 1175 { 1176 int n; 1177 1178 if (dmi_memdev) { 1179 for (n = 0; n < dmi_memdev_nr; n++) { 1180 if (handle == dmi_memdev[n].handle) 1181 return dmi_memdev[n].size; 1182 } 1183 } 1184 return ~0ull; 1185 } 1186 EXPORT_SYMBOL_GPL(dmi_memdev_size); 1187 1188 /** 1189 * dmi_memdev_type - get the memory type 1190 * @handle: DMI structure handle 1191 * 1192 * Return the DMI memory type of the module in the slot associated with the 1193 * given DMI handle, or 0x0 if no such DMI handle exists. 1194 */ 1195 u8 dmi_memdev_type(u16 handle) 1196 { 1197 int n; 1198 1199 if (dmi_memdev) { 1200 for (n = 0; n < dmi_memdev_nr; n++) { 1201 if (handle == dmi_memdev[n].handle) 1202 return dmi_memdev[n].type; 1203 } 1204 } 1205 return 0x0; /* Not a valid value */ 1206 } 1207 EXPORT_SYMBOL_GPL(dmi_memdev_type); 1208 1209 /** 1210 * dmi_memdev_handle - get the DMI handle of a memory slot 1211 * @slot: slot number 1212 * 1213 * Return the DMI handle associated with a given memory slot, or %0xFFFF 1214 * if there is no such slot. 1215 */ 1216 u16 dmi_memdev_handle(int slot) 1217 { 1218 if (dmi_memdev && slot >= 0 && slot < dmi_memdev_nr) 1219 return dmi_memdev[slot].handle; 1220 1221 return 0xffff; /* Not a valid value */ 1222 } 1223 EXPORT_SYMBOL_GPL(dmi_memdev_handle); 1224