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>
13 * DMI stands for "Desktop Management Interface". It is part
14 * of and an antecedent to, SMBIOS, which stands for System
15 * Management BIOS. See further: http://www.dmtf.org/standards
17 static const char dmi_empty_string[] = "";
19 static u16 __initdata dmi_ver;
21 * Catch too early calls to dmi_check_system():
23 static int dmi_initialized;
25 static const char * __init dmi_string_nosave(const struct dmi_header *dm, u8 s)
27 const u8 *bp = ((u8 *) dm) + dm->length;
31 while (--s > 0 && *bp)
34 /* Strings containing only spaces are considered empty */
42 return dmi_empty_string;
45 static const char * __init dmi_string(const struct dmi_header *dm, u8 s)
47 const char *bp = dmi_string_nosave(dm, s);
51 if (bp == dmi_empty_string)
52 return dmi_empty_string;
59 printk(KERN_ERR "dmi_string: cannot allocate %Zu bytes.\n", len);
65 * We have to be cautious here. We have seen BIOSes with DMI pointers
66 * pointing to completely the wrong place for example
68 static void dmi_table(u8 *buf, int len, int num,
69 void (*decode)(const struct dmi_header *, void *),
76 * Stop when we see all the items the table claimed to have
77 * OR we run off the end of the table (also happens)
79 while ((i < num) && (data - buf + sizeof(struct dmi_header)) <= len) {
80 const struct dmi_header *dm = (const struct dmi_header *)data;
83 * We want to know the total length (formatted area and
84 * strings) before decoding to make sure we won't run off the
85 * table in dmi_decode or dmi_string
88 while ((data - buf < len - 1) && (data[0] || data[1]))
90 if (data - buf < len - 1)
91 decode(dm, private_data);
101 static int __init dmi_walk_early(void (*decode)(const struct dmi_header *,
106 buf = dmi_ioremap(dmi_base, dmi_len);
110 dmi_table(buf, dmi_len, dmi_num, decode, NULL);
112 add_device_randomness(buf, dmi_len);
114 dmi_iounmap(buf, dmi_len);
118 static int __init dmi_checksum(const u8 *buf, u8 len)
123 for (a = 0; a < len; a++)
129 static const char *dmi_ident[DMI_STRING_MAX];
130 static LIST_HEAD(dmi_devices);
136 static void __init dmi_save_ident(const struct dmi_header *dm, int slot, int string)
138 const char *d = (const char*) dm;
144 p = dmi_string(dm, d[string]);
151 static void __init dmi_save_uuid(const struct dmi_header *dm, int slot, int index)
153 const u8 *d = (u8*) dm + index;
155 int is_ff = 1, is_00 = 1, i;
160 for (i = 0; i < 16 && (is_ff || is_00); i++) {
170 s = dmi_alloc(16*2+4+1);
175 * As of version 2.6 of the SMBIOS specification, the first 3 fields of
176 * the UUID are supposed to be little-endian encoded. The specification
177 * says that this is the defacto standard.
179 if (dmi_ver >= 0x0206)
180 sprintf(s, "%pUL", d);
182 sprintf(s, "%pUB", d);
187 static void __init dmi_save_type(const struct dmi_header *dm, int slot, int index)
189 const u8 *d = (u8*) dm + index;
199 sprintf(s, "%u", *d & 0x7F);
203 static void __init dmi_save_one_device(int type, const char *name)
205 struct dmi_device *dev;
207 /* No duplicate device */
208 if (dmi_find_device(type, name, NULL))
211 dev = dmi_alloc(sizeof(*dev) + strlen(name) + 1);
213 printk(KERN_ERR "dmi_save_one_device: out of memory.\n");
218 strcpy((char *)(dev + 1), name);
219 dev->name = (char *)(dev + 1);
220 dev->device_data = NULL;
221 list_add(&dev->list, &dmi_devices);
224 static void __init dmi_save_devices(const struct dmi_header *dm)
226 int i, count = (dm->length - sizeof(struct dmi_header)) / 2;
228 for (i = 0; i < count; i++) {
229 const char *d = (char *)(dm + 1) + (i * 2);
231 /* Skip disabled device */
232 if ((*d & 0x80) == 0)
235 dmi_save_one_device(*d & 0x7f, dmi_string_nosave(dm, *(d + 1)));
239 static void __init dmi_save_oem_strings_devices(const struct dmi_header *dm)
241 int i, count = *(u8 *)(dm + 1);
242 struct dmi_device *dev;
244 for (i = 1; i <= count; i++) {
245 const char *devname = dmi_string(dm, i);
247 if (devname == dmi_empty_string)
250 dev = dmi_alloc(sizeof(*dev));
253 "dmi_save_oem_strings_devices: out of memory.\n");
257 dev->type = DMI_DEV_TYPE_OEM_STRING;
259 dev->device_data = NULL;
261 list_add(&dev->list, &dmi_devices);
265 static void __init dmi_save_ipmi_device(const struct dmi_header *dm)
267 struct dmi_device *dev;
270 data = dmi_alloc(dm->length);
272 printk(KERN_ERR "dmi_save_ipmi_device: out of memory.\n");
276 memcpy(data, dm, dm->length);
278 dev = dmi_alloc(sizeof(*dev));
280 printk(KERN_ERR "dmi_save_ipmi_device: out of memory.\n");
284 dev->type = DMI_DEV_TYPE_IPMI;
285 dev->name = "IPMI controller";
286 dev->device_data = data;
288 list_add_tail(&dev->list, &dmi_devices);
291 static void __init dmi_save_dev_onboard(int instance, int segment, int bus,
292 int devfn, const char *name)
294 struct dmi_dev_onboard *onboard_dev;
296 onboard_dev = dmi_alloc(sizeof(*onboard_dev) + strlen(name) + 1);
298 printk(KERN_ERR "dmi_save_dev_onboard: out of memory.\n");
301 onboard_dev->instance = instance;
302 onboard_dev->segment = segment;
303 onboard_dev->bus = bus;
304 onboard_dev->devfn = devfn;
306 strcpy((char *)&onboard_dev[1], name);
307 onboard_dev->dev.type = DMI_DEV_TYPE_DEV_ONBOARD;
308 onboard_dev->dev.name = (char *)&onboard_dev[1];
309 onboard_dev->dev.device_data = onboard_dev;
311 list_add(&onboard_dev->dev.list, &dmi_devices);
314 static void __init dmi_save_extended_devices(const struct dmi_header *dm)
316 const u8 *d = (u8*) dm + 5;
318 /* Skip disabled device */
319 if ((*d & 0x80) == 0)
322 dmi_save_dev_onboard(*(d+1), *(u16 *)(d+2), *(d+4), *(d+5),
323 dmi_string_nosave(dm, *(d-1)));
324 dmi_save_one_device(*d & 0x7f, dmi_string_nosave(dm, *(d - 1)));
328 * Process a DMI table entry. Right now all we care about are the BIOS
329 * and machine entries. For 2.5 we should pull the smbus controller info
332 static void __init dmi_decode(const struct dmi_header *dm, void *dummy)
335 case 0: /* BIOS Information */
336 dmi_save_ident(dm, DMI_BIOS_VENDOR, 4);
337 dmi_save_ident(dm, DMI_BIOS_VERSION, 5);
338 dmi_save_ident(dm, DMI_BIOS_DATE, 8);
340 case 1: /* System Information */
341 dmi_save_ident(dm, DMI_SYS_VENDOR, 4);
342 dmi_save_ident(dm, DMI_PRODUCT_NAME, 5);
343 dmi_save_ident(dm, DMI_PRODUCT_VERSION, 6);
344 dmi_save_ident(dm, DMI_PRODUCT_SERIAL, 7);
345 dmi_save_uuid(dm, DMI_PRODUCT_UUID, 8);
347 case 2: /* Base Board Information */
348 dmi_save_ident(dm, DMI_BOARD_VENDOR, 4);
349 dmi_save_ident(dm, DMI_BOARD_NAME, 5);
350 dmi_save_ident(dm, DMI_BOARD_VERSION, 6);
351 dmi_save_ident(dm, DMI_BOARD_SERIAL, 7);
352 dmi_save_ident(dm, DMI_BOARD_ASSET_TAG, 8);
354 case 3: /* Chassis Information */
355 dmi_save_ident(dm, DMI_CHASSIS_VENDOR, 4);
356 dmi_save_type(dm, DMI_CHASSIS_TYPE, 5);
357 dmi_save_ident(dm, DMI_CHASSIS_VERSION, 6);
358 dmi_save_ident(dm, DMI_CHASSIS_SERIAL, 7);
359 dmi_save_ident(dm, DMI_CHASSIS_ASSET_TAG, 8);
361 case 10: /* Onboard Devices Information */
362 dmi_save_devices(dm);
364 case 11: /* OEM Strings */
365 dmi_save_oem_strings_devices(dm);
367 case 38: /* IPMI Device Information */
368 dmi_save_ipmi_device(dm);
370 case 41: /* Onboard Devices Extended Information */
371 dmi_save_extended_devices(dm);
375 static void __init print_filtered(const char *info)
382 for (p = info; *p; p++)
384 printk(KERN_CONT "%c", *p);
386 printk(KERN_CONT "\\x%02x", *p & 0xff);
389 static void __init dmi_dump_ids(void)
391 const char *board; /* Board Name is optional */
393 printk(KERN_DEBUG "DMI: ");
394 print_filtered(dmi_get_system_info(DMI_SYS_VENDOR));
395 printk(KERN_CONT " ");
396 print_filtered(dmi_get_system_info(DMI_PRODUCT_NAME));
397 board = dmi_get_system_info(DMI_BOARD_NAME);
399 printk(KERN_CONT "/");
400 print_filtered(board);
402 printk(KERN_CONT ", BIOS ");
403 print_filtered(dmi_get_system_info(DMI_BIOS_VERSION));
404 printk(KERN_CONT " ");
405 print_filtered(dmi_get_system_info(DMI_BIOS_DATE));
406 printk(KERN_CONT "\n");
409 static int __init dmi_present(const u8 *buf)
413 if (memcmp(buf, "_SM_", 4) == 0 &&
414 buf[5] < 32 && dmi_checksum(buf, buf[5])) {
415 smbios_ver = (buf[6] << 8) + buf[7];
417 /* Some BIOS report weird SMBIOS version, fix that up */
418 switch (smbios_ver) {
421 pr_debug("SMBIOS version fixup(2.%d->2.%d)\n",
422 smbios_ver & 0xFF, 3);
426 pr_debug("SMBIOS version fixup(2.%d->2.%d)\n", 51, 6);
436 if (memcmp(buf, "_DMI_", 5) == 0 && dmi_checksum(buf, 15)) {
438 dmi_ver = smbios_ver;
440 dmi_ver = (buf[14] & 0xF0) << 4 | (buf[14] & 0x0F);
441 dmi_num = (buf[13] << 8) | buf[12];
442 dmi_len = (buf[7] << 8) | buf[6];
443 dmi_base = (buf[11] << 24) | (buf[10] << 16) |
444 (buf[9] << 8) | buf[8];
446 if (dmi_walk_early(dmi_decode) == 0) {
448 pr_info("SMBIOS %d.%d present.\n",
449 dmi_ver >> 8, dmi_ver & 0xFF);
451 pr_info("Legacy DMI %d.%d present.\n",
452 dmi_ver >> 8, dmi_ver & 0xFF);
462 void __init dmi_scan_machine(void)
467 if (efi_enabled(EFI_CONFIG_TABLES)) {
468 if (efi.smbios == EFI_INVALID_TABLE_ADDR)
471 /* This is called as a core_initcall() because it isn't
472 * needed during early boot. This also means we can
473 * iounmap the space when we're done with it.
475 p = dmi_ioremap(efi.smbios, 32);
478 memcpy_fromio(buf, p, 32);
481 if (!dmi_present(buf)) {
488 * no iounmap() for that ioremap(); it would be a no-op, but
489 * it's so early in setup that sucker gets confused into doing
490 * what it shouldn't if we actually call it.
492 p = dmi_ioremap(0xF0000, 0x10000);
497 for (q = p; q < p + 0x10000; q += 16) {
498 memcpy_fromio(buf + 16, q, 16);
499 if (!dmi_present(buf)) {
501 dmi_iounmap(p, 0x10000);
504 memcpy(buf, buf + 16, 16);
506 dmi_iounmap(p, 0x10000);
509 printk(KERN_INFO "DMI not present or invalid.\n");
515 * dmi_matches - check if dmi_system_id structure matches system DMI data
516 * @dmi: pointer to the dmi_system_id structure to check
518 static bool dmi_matches(const struct dmi_system_id *dmi)
522 WARN(!dmi_initialized, KERN_ERR "dmi check: not initialized yet.\n");
524 for (i = 0; i < ARRAY_SIZE(dmi->matches); i++) {
525 int s = dmi->matches[i].slot;
529 if (!dmi->matches[i].exact_match &&
530 strstr(dmi_ident[s], dmi->matches[i].substr))
532 else if (dmi->matches[i].exact_match &&
533 !strcmp(dmi_ident[s], dmi->matches[i].substr))
544 * dmi_is_end_of_table - check for end-of-table marker
545 * @dmi: pointer to the dmi_system_id structure to check
547 static bool dmi_is_end_of_table(const struct dmi_system_id *dmi)
549 return dmi->matches[0].slot == DMI_NONE;
553 * dmi_check_system - check system DMI data
554 * @list: array of dmi_system_id structures to match against
555 * All non-null elements of the list must match
556 * their slot's (field index's) data (i.e., each
557 * list string must be a substring of the specified
558 * DMI slot's string data) to be considered a
561 * Walk the blacklist table running matching functions until someone
562 * returns non zero or we hit the end. Callback function is called for
563 * each successful match. Returns the number of matches.
565 int dmi_check_system(const struct dmi_system_id *list)
568 const struct dmi_system_id *d;
570 for (d = list; !dmi_is_end_of_table(d); d++)
571 if (dmi_matches(d)) {
573 if (d->callback && d->callback(d))
579 EXPORT_SYMBOL(dmi_check_system);
582 * dmi_first_match - find dmi_system_id structure matching system DMI data
583 * @list: array of dmi_system_id structures to match against
584 * All non-null elements of the list must match
585 * their slot's (field index's) data (i.e., each
586 * list string must be a substring of the specified
587 * DMI slot's string data) to be considered a
590 * Walk the blacklist table until the first match is found. Return the
591 * pointer to the matching entry or NULL if there's no match.
593 const struct dmi_system_id *dmi_first_match(const struct dmi_system_id *list)
595 const struct dmi_system_id *d;
597 for (d = list; !dmi_is_end_of_table(d); d++)
603 EXPORT_SYMBOL(dmi_first_match);
606 * dmi_get_system_info - return DMI data value
607 * @field: data index (see enum dmi_field)
609 * Returns one DMI data value, can be used to perform
610 * complex DMI data checks.
612 const char *dmi_get_system_info(int field)
614 return dmi_ident[field];
616 EXPORT_SYMBOL(dmi_get_system_info);
619 * dmi_name_in_serial - Check if string is in the DMI product serial information
620 * @str: string to check for
622 int dmi_name_in_serial(const char *str)
624 int f = DMI_PRODUCT_SERIAL;
625 if (dmi_ident[f] && strstr(dmi_ident[f], str))
631 * dmi_name_in_vendors - Check if string is in the DMI system or board vendor name
632 * @str: Case sensitive Name
634 int dmi_name_in_vendors(const char *str)
636 static int fields[] = { DMI_SYS_VENDOR, DMI_BOARD_VENDOR, DMI_NONE };
638 for (i = 0; fields[i] != DMI_NONE; i++) {
640 if (dmi_ident[f] && strstr(dmi_ident[f], str))
645 EXPORT_SYMBOL(dmi_name_in_vendors);
648 * dmi_find_device - find onboard device by type/name
649 * @type: device type or %DMI_DEV_TYPE_ANY to match all device types
650 * @name: device name string or %NULL to match all
651 * @from: previous device found in search, or %NULL for new search.
653 * Iterates through the list of known onboard devices. If a device is
654 * found with a matching @vendor and @device, a pointer to its device
655 * structure is returned. Otherwise, %NULL is returned.
656 * A new search is initiated by passing %NULL as the @from argument.
657 * If @from is not %NULL, searches continue from next device.
659 const struct dmi_device * dmi_find_device(int type, const char *name,
660 const struct dmi_device *from)
662 const struct list_head *head = from ? &from->list : &dmi_devices;
665 for(d = head->next; d != &dmi_devices; d = d->next) {
666 const struct dmi_device *dev =
667 list_entry(d, struct dmi_device, list);
669 if (((type == DMI_DEV_TYPE_ANY) || (dev->type == type)) &&
670 ((name == NULL) || (strcmp(dev->name, name) == 0)))
676 EXPORT_SYMBOL(dmi_find_device);
679 * dmi_get_date - parse a DMI date
680 * @field: data index (see enum dmi_field)
681 * @yearp: optional out parameter for the year
682 * @monthp: optional out parameter for the month
683 * @dayp: optional out parameter for the day
685 * The date field is assumed to be in the form resembling
686 * [mm[/dd]]/yy[yy] and the result is stored in the out
687 * parameters any or all of which can be omitted.
689 * If the field doesn't exist, all out parameters are set to zero
690 * and false is returned. Otherwise, true is returned with any
691 * invalid part of date set to zero.
693 * On return, year, month and day are guaranteed to be in the
694 * range of [0,9999], [0,12] and [0,31] respectively.
696 bool dmi_get_date(int field, int *yearp, int *monthp, int *dayp)
698 int year = 0, month = 0, day = 0;
703 s = dmi_get_system_info(field);
709 * Determine year first. We assume the date string resembles
710 * mm/dd/yy[yy] but the original code extracted only the year
711 * from the end. Keep the behavior in the spirit of no
719 year = simple_strtoul(y, &e, 10);
720 if (y != e && year < 100) { /* 2-digit year */
722 if (year < 1996) /* no dates < spec 1.0 */
725 if (year > 9999) /* year should fit in %04d */
728 /* parse the mm and dd */
729 month = simple_strtoul(s, &e, 10);
730 if (s == e || *e != '/' || !month || month > 12) {
736 day = simple_strtoul(s, &e, 10);
737 if (s == y || s == e || *e != '/' || day > 31)
748 EXPORT_SYMBOL(dmi_get_date);
751 * dmi_walk - Walk the DMI table and get called back for every record
752 * @decode: Callback function
753 * @private_data: Private data to be passed to the callback function
755 * Returns -1 when the DMI table can't be reached, 0 on success.
757 int dmi_walk(void (*decode)(const struct dmi_header *, void *),
765 buf = ioremap(dmi_base, dmi_len);
769 dmi_table(buf, dmi_len, dmi_num, decode, private_data);
774 EXPORT_SYMBOL_GPL(dmi_walk);
777 * dmi_match - compare a string to the dmi field (if exists)
778 * @f: DMI field identifier
779 * @str: string to compare the DMI field to
781 * Returns true if the requested field equals to the str (including NULL).
783 bool dmi_match(enum dmi_field f, const char *str)
785 const char *info = dmi_get_system_info(f);
787 if (info == NULL || str == NULL)
790 return !strcmp(info, str);
792 EXPORT_SYMBOL_GPL(dmi_match);