1 #include <linux/types.h>
2 #include <linux/string.h>
3 #include <linux/init.h>
4 #include <linux/module.h>
5 #include <linux/ctype.h>
8 #include <linux/bootmem.h>
9 #include <linux/random.h>
11 #include <asm/unaligned.h>
14 * DMI stands for "Desktop Management Interface". It is part
15 * of and an antecedent to, SMBIOS, which stands for System
16 * Management BIOS. See further: http://www.dmtf.org/standards
18 static const char dmi_empty_string[] = " ";
20 static u32 dmi_ver __initdata;
24 * Catch too early calls to dmi_check_system():
26 static int dmi_initialized;
28 /* DMI system identification string used during boot */
29 static char dmi_ids_string[128] __initdata;
31 static struct dmi_memdev_info {
36 static int dmi_memdev_nr;
38 static const char * __init dmi_string_nosave(const struct dmi_header *dm, u8 s)
40 const u8 *bp = ((u8 *) dm) + dm->length;
44 while (s > 0 && *bp) {
50 size_t len = strlen(bp)+1;
51 size_t cmp_len = len > 8 ? 8 : len;
53 if (!memcmp(bp, dmi_empty_string, cmp_len))
54 return dmi_empty_string;
62 static const char * __init dmi_string(const struct dmi_header *dm, u8 s)
64 const char *bp = dmi_string_nosave(dm, s);
68 if (bp == dmi_empty_string)
69 return dmi_empty_string;
80 * We have to be cautious here. We have seen BIOSes with DMI pointers
81 * pointing to completely the wrong place for example
83 static void dmi_table(u8 *buf,
84 void (*decode)(const struct dmi_header *, void *),
91 * Stop when we have seen all the items the table claimed to have
92 * (SMBIOS < 3.0 only) OR we reach an end-of-table marker (SMBIOS
93 * >= 3.0 only) OR we run off the end of the table (should never
94 * happen but sometimes does on bogus implementations.)
96 while ((!dmi_num || i < dmi_num) &&
97 (data - buf + sizeof(struct dmi_header)) <= dmi_len) {
98 const struct dmi_header *dm = (const struct dmi_header *)data;
101 * We want to know the total length (formatted area and
102 * strings) before decoding to make sure we won't run off the
103 * table in dmi_decode or dmi_string
106 while ((data - buf < dmi_len - 1) && (data[0] || data[1]))
108 if (data - buf < dmi_len - 1)
109 decode(dm, private_data);
112 * 7.45 End-of-Table (Type 127) [SMBIOS reference spec v3.0.0]
113 * For tables behind a 64-bit entry point, we have no item
114 * count and no exact table length, so stop on end-of-table
115 * marker. For tables behind a 32-bit entry point, we have
116 * seen OEM structures behind the end-of-table marker on
117 * some systems, so don't trust it.
119 if (!dmi_num && dm->type == DMI_ENTRY_END_OF_TABLE)
127 static phys_addr_t dmi_base;
129 static int __init dmi_walk_early(void (*decode)(const struct dmi_header *,
134 buf = dmi_early_remap(dmi_base, dmi_len);
138 dmi_table(buf, decode, NULL);
140 add_device_randomness(buf, dmi_len);
142 dmi_early_unmap(buf, dmi_len);
146 static int __init dmi_checksum(const u8 *buf, u8 len)
151 for (a = 0; a < len; a++)
157 static const char *dmi_ident[DMI_STRING_MAX];
158 static LIST_HEAD(dmi_devices);
164 static void __init dmi_save_ident(const struct dmi_header *dm, int slot,
167 const char *d = (const char *) dm;
173 p = dmi_string(dm, d[string]);
180 static void __init dmi_save_uuid(const struct dmi_header *dm, int slot,
183 const u8 *d = (u8 *) dm + index;
185 int is_ff = 1, is_00 = 1, i;
190 for (i = 0; i < 16 && (is_ff || is_00); i++) {
200 s = dmi_alloc(16*2+4+1);
205 * As of version 2.6 of the SMBIOS specification, the first 3 fields of
206 * the UUID are supposed to be little-endian encoded. The specification
207 * says that this is the defacto standard.
209 if (dmi_ver >= 0x020600)
210 sprintf(s, "%pUL", d);
212 sprintf(s, "%pUB", d);
217 static void __init dmi_save_type(const struct dmi_header *dm, int slot,
220 const u8 *d = (u8 *) dm + index;
230 sprintf(s, "%u", *d & 0x7F);
234 static void __init dmi_save_one_device(int type, const char *name)
236 struct dmi_device *dev;
238 /* No duplicate device */
239 if (dmi_find_device(type, name, NULL))
242 dev = dmi_alloc(sizeof(*dev) + strlen(name) + 1);
247 strcpy((char *)(dev + 1), name);
248 dev->name = (char *)(dev + 1);
249 dev->device_data = NULL;
250 list_add(&dev->list, &dmi_devices);
253 static void __init dmi_save_devices(const struct dmi_header *dm)
255 int i, count = (dm->length - sizeof(struct dmi_header)) / 2;
257 for (i = 0; i < count; i++) {
258 const char *d = (char *)(dm + 1) + (i * 2);
260 /* Skip disabled device */
261 if ((*d & 0x80) == 0)
264 dmi_save_one_device(*d & 0x7f, dmi_string_nosave(dm, *(d + 1)));
268 static void __init dmi_save_oem_strings_devices(const struct dmi_header *dm)
270 int i, count = *(u8 *)(dm + 1);
271 struct dmi_device *dev;
273 for (i = 1; i <= count; i++) {
274 const char *devname = dmi_string(dm, i);
276 if (devname == dmi_empty_string)
279 dev = dmi_alloc(sizeof(*dev));
283 dev->type = DMI_DEV_TYPE_OEM_STRING;
285 dev->device_data = NULL;
287 list_add(&dev->list, &dmi_devices);
291 static void __init dmi_save_ipmi_device(const struct dmi_header *dm)
293 struct dmi_device *dev;
296 data = dmi_alloc(dm->length);
300 memcpy(data, dm, dm->length);
302 dev = dmi_alloc(sizeof(*dev));
306 dev->type = DMI_DEV_TYPE_IPMI;
307 dev->name = "IPMI controller";
308 dev->device_data = data;
310 list_add_tail(&dev->list, &dmi_devices);
313 static void __init dmi_save_dev_onboard(int instance, int segment, int bus,
314 int devfn, const char *name)
316 struct dmi_dev_onboard *onboard_dev;
318 onboard_dev = dmi_alloc(sizeof(*onboard_dev) + strlen(name) + 1);
322 onboard_dev->instance = instance;
323 onboard_dev->segment = segment;
324 onboard_dev->bus = bus;
325 onboard_dev->devfn = devfn;
327 strcpy((char *)&onboard_dev[1], name);
328 onboard_dev->dev.type = DMI_DEV_TYPE_DEV_ONBOARD;
329 onboard_dev->dev.name = (char *)&onboard_dev[1];
330 onboard_dev->dev.device_data = onboard_dev;
332 list_add(&onboard_dev->dev.list, &dmi_devices);
335 static void __init dmi_save_extended_devices(const struct dmi_header *dm)
337 const u8 *d = (u8 *) dm + 5;
339 /* Skip disabled device */
340 if ((*d & 0x80) == 0)
343 dmi_save_dev_onboard(*(d+1), *(u16 *)(d+2), *(d+4), *(d+5),
344 dmi_string_nosave(dm, *(d-1)));
345 dmi_save_one_device(*d & 0x7f, dmi_string_nosave(dm, *(d - 1)));
348 static void __init count_mem_devices(const struct dmi_header *dm, void *v)
350 if (dm->type != DMI_ENTRY_MEM_DEVICE)
355 static void __init save_mem_devices(const struct dmi_header *dm, void *v)
357 const char *d = (const char *)dm;
360 if (dm->type != DMI_ENTRY_MEM_DEVICE)
362 if (nr >= dmi_memdev_nr) {
363 pr_warn(FW_BUG "Too many DIMM entries in SMBIOS table\n");
366 dmi_memdev[nr].handle = get_unaligned(&dm->handle);
367 dmi_memdev[nr].device = dmi_string(dm, d[0x10]);
368 dmi_memdev[nr].bank = dmi_string(dm, d[0x11]);
372 void __init dmi_memdev_walk(void)
377 if (dmi_walk_early(count_mem_devices) == 0 && dmi_memdev_nr) {
378 dmi_memdev = dmi_alloc(sizeof(*dmi_memdev) * dmi_memdev_nr);
380 dmi_walk_early(save_mem_devices);
385 * Process a DMI table entry. Right now all we care about are the BIOS
386 * and machine entries. For 2.5 we should pull the smbus controller info
389 static void __init dmi_decode(const struct dmi_header *dm, void *dummy)
392 case 0: /* BIOS Information */
393 dmi_save_ident(dm, DMI_BIOS_VENDOR, 4);
394 dmi_save_ident(dm, DMI_BIOS_VERSION, 5);
395 dmi_save_ident(dm, DMI_BIOS_DATE, 8);
397 case 1: /* System Information */
398 dmi_save_ident(dm, DMI_SYS_VENDOR, 4);
399 dmi_save_ident(dm, DMI_PRODUCT_NAME, 5);
400 dmi_save_ident(dm, DMI_PRODUCT_VERSION, 6);
401 dmi_save_ident(dm, DMI_PRODUCT_SERIAL, 7);
402 dmi_save_uuid(dm, DMI_PRODUCT_UUID, 8);
404 case 2: /* Base Board Information */
405 dmi_save_ident(dm, DMI_BOARD_VENDOR, 4);
406 dmi_save_ident(dm, DMI_BOARD_NAME, 5);
407 dmi_save_ident(dm, DMI_BOARD_VERSION, 6);
408 dmi_save_ident(dm, DMI_BOARD_SERIAL, 7);
409 dmi_save_ident(dm, DMI_BOARD_ASSET_TAG, 8);
411 case 3: /* Chassis Information */
412 dmi_save_ident(dm, DMI_CHASSIS_VENDOR, 4);
413 dmi_save_type(dm, DMI_CHASSIS_TYPE, 5);
414 dmi_save_ident(dm, DMI_CHASSIS_VERSION, 6);
415 dmi_save_ident(dm, DMI_CHASSIS_SERIAL, 7);
416 dmi_save_ident(dm, DMI_CHASSIS_ASSET_TAG, 8);
418 case 10: /* Onboard Devices Information */
419 dmi_save_devices(dm);
421 case 11: /* OEM Strings */
422 dmi_save_oem_strings_devices(dm);
424 case 38: /* IPMI Device Information */
425 dmi_save_ipmi_device(dm);
427 case 41: /* Onboard Devices Extended Information */
428 dmi_save_extended_devices(dm);
432 static int __init print_filtered(char *buf, size_t len, const char *info)
440 for (p = info; *p; p++)
442 c += scnprintf(buf + c, len - c, "%c", *p);
444 c += scnprintf(buf + c, len - c, "\\x%02x", *p & 0xff);
448 static void __init dmi_format_ids(char *buf, size_t len)
451 const char *board; /* Board Name is optional */
453 c += print_filtered(buf + c, len - c,
454 dmi_get_system_info(DMI_SYS_VENDOR));
455 c += scnprintf(buf + c, len - c, " ");
456 c += print_filtered(buf + c, len - c,
457 dmi_get_system_info(DMI_PRODUCT_NAME));
459 board = dmi_get_system_info(DMI_BOARD_NAME);
461 c += scnprintf(buf + c, len - c, "/");
462 c += print_filtered(buf + c, len - c, board);
464 c += scnprintf(buf + c, len - c, ", BIOS ");
465 c += print_filtered(buf + c, len - c,
466 dmi_get_system_info(DMI_BIOS_VERSION));
467 c += scnprintf(buf + c, len - c, " ");
468 c += print_filtered(buf + c, len - c,
469 dmi_get_system_info(DMI_BIOS_DATE));
473 * Check for DMI/SMBIOS headers in the system firmware image. Any
474 * SMBIOS header must start 16 bytes before the DMI header, so take a
475 * 32 byte buffer and check for DMI at offset 16 and SMBIOS at offset
476 * 0. If the DMI header is present, set dmi_ver accordingly (SMBIOS
477 * takes precedence) and return 0. Otherwise return 1.
479 static int __init dmi_present(const u8 *buf)
483 if (memcmp(buf, "_SM_", 4) == 0 &&
484 buf[5] < 32 && dmi_checksum(buf, buf[5])) {
485 smbios_ver = get_unaligned_be16(buf + 6);
487 /* Some BIOS report weird SMBIOS version, fix that up */
488 switch (smbios_ver) {
491 pr_debug("SMBIOS version fixup(2.%d->2.%d)\n",
492 smbios_ver & 0xFF, 3);
496 pr_debug("SMBIOS version fixup(2.%d->2.%d)\n", 51, 6);
506 if (memcmp(buf, "_DMI_", 5) == 0 && dmi_checksum(buf, 15)) {
508 dmi_ver = smbios_ver;
510 dmi_ver = (buf[14] & 0xF0) << 4 | (buf[14] & 0x0F);
511 dmi_num = get_unaligned_le16(buf + 12);
512 dmi_len = get_unaligned_le16(buf + 6);
513 dmi_base = get_unaligned_le32(buf + 8);
515 if (dmi_walk_early(dmi_decode) == 0) {
517 pr_info("SMBIOS %d.%d present.\n",
518 dmi_ver >> 8, dmi_ver & 0xFF);
520 pr_info("Legacy DMI %d.%d present.\n",
521 dmi_ver >> 8, dmi_ver & 0xFF);
524 dmi_format_ids(dmi_ids_string, sizeof(dmi_ids_string));
525 printk(KERN_DEBUG "DMI: %s\n", dmi_ids_string);
534 * Check for the SMBIOS 3.0 64-bit entry point signature. Unlike the legacy
535 * 32-bit entry point, there is no embedded DMI header (_DMI_) in here.
537 static int __init dmi_smbios3_present(const u8 *buf)
539 if (memcmp(buf, "_SM3_", 5) == 0 &&
540 buf[6] < 32 && dmi_checksum(buf, buf[6])) {
541 dmi_ver = get_unaligned_be32(buf + 6);
543 dmi_num = 0; /* No longer specified */
544 dmi_len = get_unaligned_le32(buf + 12);
545 dmi_base = get_unaligned_le64(buf + 16);
547 if (dmi_walk_early(dmi_decode) == 0) {
548 pr_info("SMBIOS %d.%d.%d present.\n",
549 dmi_ver >> 16, (dmi_ver >> 8) & 0xFF,
551 dmi_format_ids(dmi_ids_string, sizeof(dmi_ids_string));
552 pr_debug("DMI: %s\n", dmi_ids_string);
559 void __init dmi_scan_machine(void)
564 if (efi_enabled(EFI_CONFIG_TABLES)) {
566 * According to the DMTF SMBIOS reference spec v3.0.0, it is
567 * allowed to define both the 64-bit entry point (smbios3) and
568 * the 32-bit entry point (smbios), in which case they should
569 * either both point to the same SMBIOS structure table, or the
570 * table pointed to by the 64-bit entry point should contain a
571 * superset of the table contents pointed to by the 32-bit entry
572 * point (section 5.2)
573 * This implies that the 64-bit entry point should have
574 * precedence if it is defined and supported by the OS. If we
575 * have the 64-bit entry point, but fail to decode it, fall
576 * back to the legacy one (if available)
578 if (efi.smbios3 != EFI_INVALID_TABLE_ADDR) {
579 p = dmi_early_remap(efi.smbios3, 32);
582 memcpy_fromio(buf, p, 32);
583 dmi_early_unmap(p, 32);
585 if (!dmi_smbios3_present(buf)) {
590 if (efi.smbios == EFI_INVALID_TABLE_ADDR)
593 /* This is called as a core_initcall() because it isn't
594 * needed during early boot. This also means we can
595 * iounmap the space when we're done with it.
597 p = dmi_early_remap(efi.smbios, 32);
600 memcpy_fromio(buf, p, 32);
601 dmi_early_unmap(p, 32);
603 if (!dmi_present(buf)) {
607 } else if (IS_ENABLED(CONFIG_DMI_SCAN_MACHINE_NON_EFI_FALLBACK)) {
608 p = dmi_early_remap(0xF0000, 0x10000);
613 * Iterate over all possible DMI header addresses q.
614 * Maintain the 32 bytes around q in buf. On the
615 * first iteration, substitute zero for the
616 * out-of-range bytes so there is no chance of falsely
617 * detecting an SMBIOS header.
620 for (q = p; q < p + 0x10000; q += 16) {
621 memcpy_fromio(buf + 16, q, 16);
622 if (!dmi_smbios3_present(buf) || !dmi_present(buf)) {
624 dmi_early_unmap(p, 0x10000);
627 memcpy(buf, buf + 16, 16);
629 dmi_early_unmap(p, 0x10000);
632 pr_info("DMI not present or invalid.\n");
638 * dmi_set_dump_stack_arch_desc - set arch description for dump_stack()
640 * Invoke dump_stack_set_arch_desc() with DMI system information so that
641 * DMI identifiers are printed out on task dumps. Arch boot code should
642 * call this function after dmi_scan_machine() if it wants to print out DMI
643 * identifiers on task dumps.
645 void __init dmi_set_dump_stack_arch_desc(void)
647 dump_stack_set_arch_desc("%s", dmi_ids_string);
651 * dmi_matches - check if dmi_system_id structure matches system DMI data
652 * @dmi: pointer to the dmi_system_id structure to check
654 static bool dmi_matches(const struct dmi_system_id *dmi)
658 WARN(!dmi_initialized, KERN_ERR "dmi check: not initialized yet.\n");
660 for (i = 0; i < ARRAY_SIZE(dmi->matches); i++) {
661 int s = dmi->matches[i].slot;
665 if (!dmi->matches[i].exact_match &&
666 strstr(dmi_ident[s], dmi->matches[i].substr))
668 else if (dmi->matches[i].exact_match &&
669 !strcmp(dmi_ident[s], dmi->matches[i].substr))
680 * dmi_is_end_of_table - check for end-of-table marker
681 * @dmi: pointer to the dmi_system_id structure to check
683 static bool dmi_is_end_of_table(const struct dmi_system_id *dmi)
685 return dmi->matches[0].slot == DMI_NONE;
689 * dmi_check_system - check system DMI data
690 * @list: array of dmi_system_id structures to match against
691 * All non-null elements of the list must match
692 * their slot's (field index's) data (i.e., each
693 * list string must be a substring of the specified
694 * DMI slot's string data) to be considered a
697 * Walk the blacklist table running matching functions until someone
698 * returns non zero or we hit the end. Callback function is called for
699 * each successful match. Returns the number of matches.
701 int dmi_check_system(const struct dmi_system_id *list)
704 const struct dmi_system_id *d;
706 for (d = list; !dmi_is_end_of_table(d); d++)
707 if (dmi_matches(d)) {
709 if (d->callback && d->callback(d))
715 EXPORT_SYMBOL(dmi_check_system);
718 * dmi_first_match - find dmi_system_id structure matching system DMI data
719 * @list: array of dmi_system_id structures to match against
720 * All non-null elements of the list must match
721 * their slot's (field index's) data (i.e., each
722 * list string must be a substring of the specified
723 * DMI slot's string data) to be considered a
726 * Walk the blacklist table until the first match is found. Return the
727 * pointer to the matching entry or NULL if there's no match.
729 const struct dmi_system_id *dmi_first_match(const struct dmi_system_id *list)
731 const struct dmi_system_id *d;
733 for (d = list; !dmi_is_end_of_table(d); d++)
739 EXPORT_SYMBOL(dmi_first_match);
742 * dmi_get_system_info - return DMI data value
743 * @field: data index (see enum dmi_field)
745 * Returns one DMI data value, can be used to perform
746 * complex DMI data checks.
748 const char *dmi_get_system_info(int field)
750 return dmi_ident[field];
752 EXPORT_SYMBOL(dmi_get_system_info);
755 * dmi_name_in_serial - Check if string is in the DMI product serial information
756 * @str: string to check for
758 int dmi_name_in_serial(const char *str)
760 int f = DMI_PRODUCT_SERIAL;
761 if (dmi_ident[f] && strstr(dmi_ident[f], str))
767 * dmi_name_in_vendors - Check if string is in the DMI system or board vendor name
768 * @str: Case sensitive Name
770 int dmi_name_in_vendors(const char *str)
772 static int fields[] = { DMI_SYS_VENDOR, DMI_BOARD_VENDOR, DMI_NONE };
774 for (i = 0; fields[i] != DMI_NONE; i++) {
776 if (dmi_ident[f] && strstr(dmi_ident[f], str))
781 EXPORT_SYMBOL(dmi_name_in_vendors);
784 * dmi_find_device - find onboard device by type/name
785 * @type: device type or %DMI_DEV_TYPE_ANY to match all device types
786 * @name: device name string or %NULL to match all
787 * @from: previous device found in search, or %NULL for new search.
789 * Iterates through the list of known onboard devices. If a device is
790 * found with a matching @vendor and @device, a pointer to its device
791 * structure is returned. Otherwise, %NULL is returned.
792 * A new search is initiated by passing %NULL as the @from argument.
793 * If @from is not %NULL, searches continue from next device.
795 const struct dmi_device *dmi_find_device(int type, const char *name,
796 const struct dmi_device *from)
798 const struct list_head *head = from ? &from->list : &dmi_devices;
801 for (d = head->next; d != &dmi_devices; d = d->next) {
802 const struct dmi_device *dev =
803 list_entry(d, struct dmi_device, list);
805 if (((type == DMI_DEV_TYPE_ANY) || (dev->type == type)) &&
806 ((name == NULL) || (strcmp(dev->name, name) == 0)))
812 EXPORT_SYMBOL(dmi_find_device);
815 * dmi_get_date - parse a DMI date
816 * @field: data index (see enum dmi_field)
817 * @yearp: optional out parameter for the year
818 * @monthp: optional out parameter for the month
819 * @dayp: optional out parameter for the day
821 * The date field is assumed to be in the form resembling
822 * [mm[/dd]]/yy[yy] and the result is stored in the out
823 * parameters any or all of which can be omitted.
825 * If the field doesn't exist, all out parameters are set to zero
826 * and false is returned. Otherwise, true is returned with any
827 * invalid part of date set to zero.
829 * On return, year, month and day are guaranteed to be in the
830 * range of [0,9999], [0,12] and [0,31] respectively.
832 bool dmi_get_date(int field, int *yearp, int *monthp, int *dayp)
834 int year = 0, month = 0, day = 0;
839 s = dmi_get_system_info(field);
845 * Determine year first. We assume the date string resembles
846 * mm/dd/yy[yy] but the original code extracted only the year
847 * from the end. Keep the behavior in the spirit of no
855 year = simple_strtoul(y, &e, 10);
856 if (y != e && year < 100) { /* 2-digit year */
858 if (year < 1996) /* no dates < spec 1.0 */
861 if (year > 9999) /* year should fit in %04d */
864 /* parse the mm and dd */
865 month = simple_strtoul(s, &e, 10);
866 if (s == e || *e != '/' || !month || month > 12) {
872 day = simple_strtoul(s, &e, 10);
873 if (s == y || s == e || *e != '/' || day > 31)
884 EXPORT_SYMBOL(dmi_get_date);
887 * dmi_walk - Walk the DMI table and get called back for every record
888 * @decode: Callback function
889 * @private_data: Private data to be passed to the callback function
891 * Returns -1 when the DMI table can't be reached, 0 on success.
893 int dmi_walk(void (*decode)(const struct dmi_header *, void *),
901 buf = dmi_remap(dmi_base, dmi_len);
905 dmi_table(buf, decode, private_data);
910 EXPORT_SYMBOL_GPL(dmi_walk);
913 * dmi_match - compare a string to the dmi field (if exists)
914 * @f: DMI field identifier
915 * @str: string to compare the DMI field to
917 * Returns true if the requested field equals to the str (including NULL).
919 bool dmi_match(enum dmi_field f, const char *str)
921 const char *info = dmi_get_system_info(f);
923 if (info == NULL || str == NULL)
926 return !strcmp(info, str);
928 EXPORT_SYMBOL_GPL(dmi_match);
930 void dmi_memdev_name(u16 handle, const char **bank, const char **device)
934 if (dmi_memdev == NULL)
937 for (n = 0; n < dmi_memdev_nr; n++) {
938 if (handle == dmi_memdev[n].handle) {
939 *bank = dmi_memdev[n].bank;
940 *device = dmi_memdev[n].device;
945 EXPORT_SYMBOL_GPL(dmi_memdev_name);