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 /* DMI system identification string used during boot */
26 static char dmi_ids_string
[128] __initdata
;
28 static const char * __init
dmi_string_nosave(const struct dmi_header
*dm
, u8 s
)
30 const u8
*bp
= ((u8
*) dm
) + dm
->length
;
34 while (s
> 0 && *bp
) {
40 size_t len
= strlen(bp
)+1;
41 size_t cmp_len
= len
> 8 ? 8 : len
;
43 if (!memcmp(bp
, dmi_empty_string
, cmp_len
))
44 return dmi_empty_string
;
52 static const char * __init
dmi_string(const struct dmi_header
*dm
, u8 s
)
54 const char *bp
= dmi_string_nosave(dm
, s
);
58 if (bp
== dmi_empty_string
)
59 return dmi_empty_string
;
70 * We have to be cautious here. We have seen BIOSes with DMI pointers
71 * pointing to completely the wrong place for example
73 static void dmi_table(u8
*buf
, int len
, int num
,
74 void (*decode
)(const struct dmi_header
*, void *),
81 * Stop when we see all the items the table claimed to have
82 * OR we run off the end of the table (also happens)
84 while ((i
< num
) && (data
- buf
+ sizeof(struct dmi_header
)) <= len
) {
85 const struct dmi_header
*dm
= (const struct dmi_header
*)data
;
88 * We want to know the total length (formatted area and
89 * strings) before decoding to make sure we won't run off the
90 * table in dmi_decode or dmi_string
93 while ((data
- buf
< len
- 1) && (data
[0] || data
[1]))
95 if (data
- buf
< len
- 1)
96 decode(dm
, private_data
);
106 static int __init
dmi_walk_early(void (*decode
)(const struct dmi_header
*,
111 buf
= dmi_ioremap(dmi_base
, dmi_len
);
115 dmi_table(buf
, dmi_len
, dmi_num
, decode
, NULL
);
117 add_device_randomness(buf
, dmi_len
);
119 dmi_iounmap(buf
, dmi_len
);
123 static int __init
dmi_checksum(const u8
*buf
, u8 len
)
128 for (a
= 0; a
< len
; a
++)
134 static const char *dmi_ident
[DMI_STRING_MAX
];
135 static LIST_HEAD(dmi_devices
);
141 static void __init
dmi_save_ident(const struct dmi_header
*dm
, int slot
,
144 const char *d
= (const char *) dm
;
150 p
= dmi_string(dm
, d
[string
]);
157 static void __init
dmi_save_uuid(const struct dmi_header
*dm
, int slot
,
160 const u8
*d
= (u8
*) dm
+ index
;
162 int is_ff
= 1, is_00
= 1, i
;
167 for (i
= 0; i
< 16 && (is_ff
|| is_00
); i
++) {
177 s
= dmi_alloc(16*2+4+1);
182 * As of version 2.6 of the SMBIOS specification, the first 3 fields of
183 * the UUID are supposed to be little-endian encoded. The specification
184 * says that this is the defacto standard.
186 if (dmi_ver
>= 0x0206)
187 sprintf(s
, "%pUL", d
);
189 sprintf(s
, "%pUB", d
);
194 static void __init
dmi_save_type(const struct dmi_header
*dm
, int slot
,
197 const u8
*d
= (u8
*) dm
+ index
;
207 sprintf(s
, "%u", *d
& 0x7F);
211 static void __init
dmi_save_one_device(int type
, const char *name
)
213 struct dmi_device
*dev
;
215 /* No duplicate device */
216 if (dmi_find_device(type
, name
, NULL
))
219 dev
= dmi_alloc(sizeof(*dev
) + strlen(name
) + 1);
224 strcpy((char *)(dev
+ 1), name
);
225 dev
->name
= (char *)(dev
+ 1);
226 dev
->device_data
= NULL
;
227 list_add(&dev
->list
, &dmi_devices
);
230 static void __init
dmi_save_devices(const struct dmi_header
*dm
)
232 int i
, count
= (dm
->length
- sizeof(struct dmi_header
)) / 2;
234 for (i
= 0; i
< count
; i
++) {
235 const char *d
= (char *)(dm
+ 1) + (i
* 2);
237 /* Skip disabled device */
238 if ((*d
& 0x80) == 0)
241 dmi_save_one_device(*d
& 0x7f, dmi_string_nosave(dm
, *(d
+ 1)));
245 static void __init
dmi_save_oem_strings_devices(const struct dmi_header
*dm
)
247 int i
, count
= *(u8
*)(dm
+ 1);
248 struct dmi_device
*dev
;
250 for (i
= 1; i
<= count
; i
++) {
251 const char *devname
= dmi_string(dm
, i
);
253 if (devname
== dmi_empty_string
)
256 dev
= dmi_alloc(sizeof(*dev
));
260 dev
->type
= DMI_DEV_TYPE_OEM_STRING
;
262 dev
->device_data
= NULL
;
264 list_add(&dev
->list
, &dmi_devices
);
268 static void __init
dmi_save_ipmi_device(const struct dmi_header
*dm
)
270 struct dmi_device
*dev
;
273 data
= dmi_alloc(dm
->length
);
277 memcpy(data
, dm
, dm
->length
);
279 dev
= dmi_alloc(sizeof(*dev
));
283 dev
->type
= DMI_DEV_TYPE_IPMI
;
284 dev
->name
= "IPMI controller";
285 dev
->device_data
= data
;
287 list_add_tail(&dev
->list
, &dmi_devices
);
290 static void __init
dmi_save_dev_onboard(int instance
, int segment
, int bus
,
291 int devfn
, const char *name
)
293 struct dmi_dev_onboard
*onboard_dev
;
295 onboard_dev
= dmi_alloc(sizeof(*onboard_dev
) + strlen(name
) + 1);
299 onboard_dev
->instance
= instance
;
300 onboard_dev
->segment
= segment
;
301 onboard_dev
->bus
= bus
;
302 onboard_dev
->devfn
= devfn
;
304 strcpy((char *)&onboard_dev
[1], name
);
305 onboard_dev
->dev
.type
= DMI_DEV_TYPE_DEV_ONBOARD
;
306 onboard_dev
->dev
.name
= (char *)&onboard_dev
[1];
307 onboard_dev
->dev
.device_data
= onboard_dev
;
309 list_add(&onboard_dev
->dev
.list
, &dmi_devices
);
312 static void __init
dmi_save_extended_devices(const struct dmi_header
*dm
)
314 const u8
*d
= (u8
*) dm
+ 5;
316 /* Skip disabled device */
317 if ((*d
& 0x80) == 0)
320 dmi_save_dev_onboard(*(d
+1), *(u16
*)(d
+2), *(d
+4), *(d
+5),
321 dmi_string_nosave(dm
, *(d
-1)));
322 dmi_save_one_device(*d
& 0x7f, dmi_string_nosave(dm
, *(d
- 1)));
326 * Process a DMI table entry. Right now all we care about are the BIOS
327 * and machine entries. For 2.5 we should pull the smbus controller info
330 static void __init
dmi_decode(const struct dmi_header
*dm
, void *dummy
)
333 case 0: /* BIOS Information */
334 dmi_save_ident(dm
, DMI_BIOS_VENDOR
, 4);
335 dmi_save_ident(dm
, DMI_BIOS_VERSION
, 5);
336 dmi_save_ident(dm
, DMI_BIOS_DATE
, 8);
338 case 1: /* System Information */
339 dmi_save_ident(dm
, DMI_SYS_VENDOR
, 4);
340 dmi_save_ident(dm
, DMI_PRODUCT_NAME
, 5);
341 dmi_save_ident(dm
, DMI_PRODUCT_VERSION
, 6);
342 dmi_save_ident(dm
, DMI_PRODUCT_SERIAL
, 7);
343 dmi_save_uuid(dm
, DMI_PRODUCT_UUID
, 8);
345 case 2: /* Base Board Information */
346 dmi_save_ident(dm
, DMI_BOARD_VENDOR
, 4);
347 dmi_save_ident(dm
, DMI_BOARD_NAME
, 5);
348 dmi_save_ident(dm
, DMI_BOARD_VERSION
, 6);
349 dmi_save_ident(dm
, DMI_BOARD_SERIAL
, 7);
350 dmi_save_ident(dm
, DMI_BOARD_ASSET_TAG
, 8);
352 case 3: /* Chassis Information */
353 dmi_save_ident(dm
, DMI_CHASSIS_VENDOR
, 4);
354 dmi_save_type(dm
, DMI_CHASSIS_TYPE
, 5);
355 dmi_save_ident(dm
, DMI_CHASSIS_VERSION
, 6);
356 dmi_save_ident(dm
, DMI_CHASSIS_SERIAL
, 7);
357 dmi_save_ident(dm
, DMI_CHASSIS_ASSET_TAG
, 8);
359 case 10: /* Onboard Devices Information */
360 dmi_save_devices(dm
);
362 case 11: /* OEM Strings */
363 dmi_save_oem_strings_devices(dm
);
365 case 38: /* IPMI Device Information */
366 dmi_save_ipmi_device(dm
);
368 case 41: /* Onboard Devices Extended Information */
369 dmi_save_extended_devices(dm
);
373 static int __init
print_filtered(char *buf
, size_t len
, const char *info
)
381 for (p
= info
; *p
; p
++)
383 c
+= scnprintf(buf
+ c
, len
- c
, "%c", *p
);
385 c
+= scnprintf(buf
+ c
, len
- c
, "\\x%02x", *p
& 0xff);
389 static void __init
dmi_format_ids(char *buf
, size_t len
)
392 const char *board
; /* Board Name is optional */
394 c
+= print_filtered(buf
+ c
, len
- c
,
395 dmi_get_system_info(DMI_SYS_VENDOR
));
396 c
+= scnprintf(buf
+ c
, len
- c
, " ");
397 c
+= print_filtered(buf
+ c
, len
- c
,
398 dmi_get_system_info(DMI_PRODUCT_NAME
));
400 board
= dmi_get_system_info(DMI_BOARD_NAME
);
402 c
+= scnprintf(buf
+ c
, len
- c
, "/");
403 c
+= print_filtered(buf
+ c
, len
- c
, board
);
405 c
+= scnprintf(buf
+ c
, len
- c
, ", BIOS ");
406 c
+= print_filtered(buf
+ c
, len
- c
,
407 dmi_get_system_info(DMI_BIOS_VERSION
));
408 c
+= scnprintf(buf
+ c
, len
- c
, " ");
409 c
+= print_filtered(buf
+ c
, len
- c
,
410 dmi_get_system_info(DMI_BIOS_DATE
));
414 * Check for DMI/SMBIOS headers in the system firmware image. Any
415 * SMBIOS header must start 16 bytes before the DMI header, so take a
416 * 32 byte buffer and check for DMI at offset 16 and SMBIOS at offset
417 * 0. If the DMI header is present, set dmi_ver accordingly (SMBIOS
418 * takes precedence) and return 0. Otherwise return 1.
420 static int __init
dmi_present(const u8
*buf
)
424 if (memcmp(buf
, "_SM_", 4) == 0 &&
425 buf
[5] < 32 && dmi_checksum(buf
, buf
[5])) {
426 smbios_ver
= (buf
[6] << 8) + buf
[7];
428 /* Some BIOS report weird SMBIOS version, fix that up */
429 switch (smbios_ver
) {
432 pr_debug("SMBIOS version fixup(2.%d->2.%d)\n",
433 smbios_ver
& 0xFF, 3);
437 pr_debug("SMBIOS version fixup(2.%d->2.%d)\n", 51, 6);
447 if (memcmp(buf
, "_DMI_", 5) == 0 && dmi_checksum(buf
, 15)) {
448 dmi_num
= (buf
[13] << 8) | buf
[12];
449 dmi_len
= (buf
[7] << 8) | buf
[6];
450 dmi_base
= (buf
[11] << 24) | (buf
[10] << 16) |
451 (buf
[9] << 8) | buf
[8];
453 if (dmi_walk_early(dmi_decode
) == 0) {
455 dmi_ver
= smbios_ver
;
456 pr_info("SMBIOS %d.%d present.\n",
457 dmi_ver
>> 8, dmi_ver
& 0xFF);
459 dmi_ver
= (buf
[14] & 0xF0) << 4 |
461 pr_info("Legacy DMI %d.%d present.\n",
462 dmi_ver
>> 8, dmi_ver
& 0xFF);
464 dmi_format_ids(dmi_ids_string
, sizeof(dmi_ids_string
));
465 printk(KERN_DEBUG
"DMI: %s\n", dmi_ids_string
);
473 void __init
dmi_scan_machine(void)
478 if (efi_enabled(EFI_CONFIG_TABLES
)) {
479 if (efi
.smbios
== EFI_INVALID_TABLE_ADDR
)
482 /* This is called as a core_initcall() because it isn't
483 * needed during early boot. This also means we can
484 * iounmap the space when we're done with it.
486 p
= dmi_ioremap(efi
.smbios
, 32);
489 memcpy_fromio(buf
, p
, 32);
492 if (!dmi_present(buf
)) {
497 p
= dmi_ioremap(0xF0000, 0x10000);
502 * Iterate over all possible DMI header addresses q.
503 * Maintain the 32 bytes around q in buf. On the
504 * first iteration, substitute zero for the
505 * out-of-range bytes so there is no chance of falsely
506 * detecting an SMBIOS header.
509 for (q
= p
; q
< p
+ 0x10000; q
+= 16) {
510 memcpy_fromio(buf
+ 16, q
, 16);
511 if (!dmi_present(buf
)) {
513 dmi_iounmap(p
, 0x10000);
516 memcpy(buf
, buf
+ 16, 16);
518 dmi_iounmap(p
, 0x10000);
521 pr_info("DMI not present or invalid.\n");
527 * dmi_set_dump_stack_arch_desc - set arch description for dump_stack()
529 * Invoke dump_stack_set_arch_desc() with DMI system information so that
530 * DMI identifiers are printed out on task dumps. Arch boot code should
531 * call this function after dmi_scan_machine() if it wants to print out DMI
532 * identifiers on task dumps.
534 void __init
dmi_set_dump_stack_arch_desc(void)
536 dump_stack_set_arch_desc("%s", dmi_ids_string
);
540 * dmi_matches - check if dmi_system_id structure matches system DMI data
541 * @dmi: pointer to the dmi_system_id structure to check
543 static bool dmi_matches(const struct dmi_system_id
*dmi
)
547 WARN(!dmi_initialized
, KERN_ERR
"dmi check: not initialized yet.\n");
549 for (i
= 0; i
< ARRAY_SIZE(dmi
->matches
); i
++) {
550 int s
= dmi
->matches
[i
].slot
;
554 if (!dmi
->matches
[i
].exact_match
&&
555 strstr(dmi_ident
[s
], dmi
->matches
[i
].substr
))
557 else if (dmi
->matches
[i
].exact_match
&&
558 !strcmp(dmi_ident
[s
], dmi
->matches
[i
].substr
))
569 * dmi_is_end_of_table - check for end-of-table marker
570 * @dmi: pointer to the dmi_system_id structure to check
572 static bool dmi_is_end_of_table(const struct dmi_system_id
*dmi
)
574 return dmi
->matches
[0].slot
== DMI_NONE
;
578 * dmi_check_system - check system DMI data
579 * @list: array of dmi_system_id structures to match against
580 * All non-null elements of the list must match
581 * their slot's (field index's) data (i.e., each
582 * list string must be a substring of the specified
583 * DMI slot's string data) to be considered a
586 * Walk the blacklist table running matching functions until someone
587 * returns non zero or we hit the end. Callback function is called for
588 * each successful match. Returns the number of matches.
590 int dmi_check_system(const struct dmi_system_id
*list
)
593 const struct dmi_system_id
*d
;
595 for (d
= list
; !dmi_is_end_of_table(d
); d
++)
596 if (dmi_matches(d
)) {
598 if (d
->callback
&& d
->callback(d
))
604 EXPORT_SYMBOL(dmi_check_system
);
607 * dmi_first_match - find dmi_system_id structure matching system DMI data
608 * @list: array of dmi_system_id structures to match against
609 * All non-null elements of the list must match
610 * their slot's (field index's) data (i.e., each
611 * list string must be a substring of the specified
612 * DMI slot's string data) to be considered a
615 * Walk the blacklist table until the first match is found. Return the
616 * pointer to the matching entry or NULL if there's no match.
618 const struct dmi_system_id
*dmi_first_match(const struct dmi_system_id
*list
)
620 const struct dmi_system_id
*d
;
622 for (d
= list
; !dmi_is_end_of_table(d
); d
++)
628 EXPORT_SYMBOL(dmi_first_match
);
631 * dmi_get_system_info - return DMI data value
632 * @field: data index (see enum dmi_field)
634 * Returns one DMI data value, can be used to perform
635 * complex DMI data checks.
637 const char *dmi_get_system_info(int field
)
639 return dmi_ident
[field
];
641 EXPORT_SYMBOL(dmi_get_system_info
);
644 * dmi_name_in_serial - Check if string is in the DMI product serial information
645 * @str: string to check for
647 int dmi_name_in_serial(const char *str
)
649 int f
= DMI_PRODUCT_SERIAL
;
650 if (dmi_ident
[f
] && strstr(dmi_ident
[f
], str
))
656 * dmi_name_in_vendors - Check if string is in the DMI system or board vendor name
657 * @str: Case sensitive Name
659 int dmi_name_in_vendors(const char *str
)
661 static int fields
[] = { DMI_SYS_VENDOR
, DMI_BOARD_VENDOR
, DMI_NONE
};
663 for (i
= 0; fields
[i
] != DMI_NONE
; i
++) {
665 if (dmi_ident
[f
] && strstr(dmi_ident
[f
], str
))
670 EXPORT_SYMBOL(dmi_name_in_vendors
);
673 * dmi_find_device - find onboard device by type/name
674 * @type: device type or %DMI_DEV_TYPE_ANY to match all device types
675 * @name: device name string or %NULL to match all
676 * @from: previous device found in search, or %NULL for new search.
678 * Iterates through the list of known onboard devices. If a device is
679 * found with a matching @vendor and @device, a pointer to its device
680 * structure is returned. Otherwise, %NULL is returned.
681 * A new search is initiated by passing %NULL as the @from argument.
682 * If @from is not %NULL, searches continue from next device.
684 const struct dmi_device
*dmi_find_device(int type
, const char *name
,
685 const struct dmi_device
*from
)
687 const struct list_head
*head
= from
? &from
->list
: &dmi_devices
;
690 for (d
= head
->next
; d
!= &dmi_devices
; d
= d
->next
) {
691 const struct dmi_device
*dev
=
692 list_entry(d
, struct dmi_device
, list
);
694 if (((type
== DMI_DEV_TYPE_ANY
) || (dev
->type
== type
)) &&
695 ((name
== NULL
) || (strcmp(dev
->name
, name
) == 0)))
701 EXPORT_SYMBOL(dmi_find_device
);
704 * dmi_get_date - parse a DMI date
705 * @field: data index (see enum dmi_field)
706 * @yearp: optional out parameter for the year
707 * @monthp: optional out parameter for the month
708 * @dayp: optional out parameter for the day
710 * The date field is assumed to be in the form resembling
711 * [mm[/dd]]/yy[yy] and the result is stored in the out
712 * parameters any or all of which can be omitted.
714 * If the field doesn't exist, all out parameters are set to zero
715 * and false is returned. Otherwise, true is returned with any
716 * invalid part of date set to zero.
718 * On return, year, month and day are guaranteed to be in the
719 * range of [0,9999], [0,12] and [0,31] respectively.
721 bool dmi_get_date(int field
, int *yearp
, int *monthp
, int *dayp
)
723 int year
= 0, month
= 0, day
= 0;
728 s
= dmi_get_system_info(field
);
734 * Determine year first. We assume the date string resembles
735 * mm/dd/yy[yy] but the original code extracted only the year
736 * from the end. Keep the behavior in the spirit of no
744 year
= simple_strtoul(y
, &e
, 10);
745 if (y
!= e
&& year
< 100) { /* 2-digit year */
747 if (year
< 1996) /* no dates < spec 1.0 */
750 if (year
> 9999) /* year should fit in %04d */
753 /* parse the mm and dd */
754 month
= simple_strtoul(s
, &e
, 10);
755 if (s
== e
|| *e
!= '/' || !month
|| month
> 12) {
761 day
= simple_strtoul(s
, &e
, 10);
762 if (s
== y
|| s
== e
|| *e
!= '/' || day
> 31)
773 EXPORT_SYMBOL(dmi_get_date
);
776 * dmi_walk - Walk the DMI table and get called back for every record
777 * @decode: Callback function
778 * @private_data: Private data to be passed to the callback function
780 * Returns -1 when the DMI table can't be reached, 0 on success.
782 int dmi_walk(void (*decode
)(const struct dmi_header
*, void *),
790 buf
= ioremap(dmi_base
, dmi_len
);
794 dmi_table(buf
, dmi_len
, dmi_num
, decode
, private_data
);
799 EXPORT_SYMBOL_GPL(dmi_walk
);
802 * dmi_match - compare a string to the dmi field (if exists)
803 * @f: DMI field identifier
804 * @str: string to compare the DMI field to
806 * Returns true if the requested field equals to the str (including NULL).
808 bool dmi_match(enum dmi_field f
, const char *str
)
810 const char *info
= dmi_get_system_info(f
);
812 if (info
== NULL
|| str
== NULL
)
815 return !strcmp(info
, str
);
817 EXPORT_SYMBOL_GPL(dmi_match
);