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 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 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
;
66 printk(KERN_ERR
"dmi_string: cannot allocate %Zu bytes.\n", len
);
72 * We have to be cautious here. We have seen BIOSes with DMI pointers
73 * pointing to completely the wrong place for example
75 static void dmi_table(u8
*buf
, int len
, int num
,
76 void (*decode
)(const struct dmi_header
*, void *),
83 * Stop when we see all the items the table claimed to have
84 * OR we run off the end of the table (also happens)
86 while ((i
< num
) && (data
- buf
+ sizeof(struct dmi_header
)) <= len
) {
87 const struct dmi_header
*dm
= (const struct dmi_header
*)data
;
90 * We want to know the total length (formatted area and
91 * strings) before decoding to make sure we won't run off the
92 * table in dmi_decode or dmi_string
95 while ((data
- buf
< len
- 1) && (data
[0] || data
[1]))
97 if (data
- buf
< len
- 1)
98 decode(dm
, private_data
);
108 static int __init
dmi_walk_early(void (*decode
)(const struct dmi_header
*,
113 buf
= dmi_ioremap(dmi_base
, dmi_len
);
117 dmi_table(buf
, dmi_len
, dmi_num
, decode
, NULL
);
119 add_device_randomness(buf
, dmi_len
);
121 dmi_iounmap(buf
, dmi_len
);
125 static int __init
dmi_checksum(const u8
*buf
, u8 len
)
130 for (a
= 0; a
< len
; a
++)
136 static char *dmi_ident
[DMI_STRING_MAX
];
137 static LIST_HEAD(dmi_devices
);
143 static void __init
dmi_save_ident(const struct dmi_header
*dm
, int slot
, int string
)
145 const char *d
= (const char*) dm
;
151 p
= dmi_string(dm
, d
[string
]);
158 static void __init
dmi_save_uuid(const struct dmi_header
*dm
, int slot
, int index
)
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
, int index
)
196 const u8
*d
= (u8
*) dm
+ index
;
206 sprintf(s
, "%u", *d
& 0x7F);
210 static void __init
dmi_save_one_device(int type
, const char *name
)
212 struct dmi_device
*dev
;
214 /* No duplicate device */
215 if (dmi_find_device(type
, name
, NULL
))
218 dev
= dmi_alloc(sizeof(*dev
) + strlen(name
) + 1);
220 printk(KERN_ERR
"dmi_save_one_device: out of memory.\n");
225 strcpy((char *)(dev
+ 1), name
);
226 dev
->name
= (char *)(dev
+ 1);
227 dev
->device_data
= NULL
;
228 list_add(&dev
->list
, &dmi_devices
);
231 static void __init
dmi_save_devices(const struct dmi_header
*dm
)
233 int i
, count
= (dm
->length
- sizeof(struct dmi_header
)) / 2;
235 for (i
= 0; i
< count
; i
++) {
236 const char *d
= (char *)(dm
+ 1) + (i
* 2);
238 /* Skip disabled device */
239 if ((*d
& 0x80) == 0)
242 dmi_save_one_device(*d
& 0x7f, dmi_string_nosave(dm
, *(d
+ 1)));
246 static void __init
dmi_save_oem_strings_devices(const struct dmi_header
*dm
)
248 int i
, count
= *(u8
*)(dm
+ 1);
249 struct dmi_device
*dev
;
251 for (i
= 1; i
<= count
; i
++) {
252 char *devname
= dmi_string(dm
, i
);
254 if (devname
== dmi_empty_string
)
257 dev
= dmi_alloc(sizeof(*dev
));
260 "dmi_save_oem_strings_devices: out of memory.\n");
264 dev
->type
= DMI_DEV_TYPE_OEM_STRING
;
266 dev
->device_data
= NULL
;
268 list_add(&dev
->list
, &dmi_devices
);
272 static void __init
dmi_save_ipmi_device(const struct dmi_header
*dm
)
274 struct dmi_device
*dev
;
277 data
= dmi_alloc(dm
->length
);
279 printk(KERN_ERR
"dmi_save_ipmi_device: out of memory.\n");
283 memcpy(data
, dm
, dm
->length
);
285 dev
= dmi_alloc(sizeof(*dev
));
287 printk(KERN_ERR
"dmi_save_ipmi_device: out of memory.\n");
291 dev
->type
= DMI_DEV_TYPE_IPMI
;
292 dev
->name
= "IPMI controller";
293 dev
->device_data
= data
;
295 list_add_tail(&dev
->list
, &dmi_devices
);
298 static void __init
dmi_save_dev_onboard(int instance
, int segment
, int bus
,
299 int devfn
, const char *name
)
301 struct dmi_dev_onboard
*onboard_dev
;
303 onboard_dev
= dmi_alloc(sizeof(*onboard_dev
) + strlen(name
) + 1);
305 printk(KERN_ERR
"dmi_save_dev_onboard: out of memory.\n");
308 onboard_dev
->instance
= instance
;
309 onboard_dev
->segment
= segment
;
310 onboard_dev
->bus
= bus
;
311 onboard_dev
->devfn
= devfn
;
313 strcpy((char *)&onboard_dev
[1], name
);
314 onboard_dev
->dev
.type
= DMI_DEV_TYPE_DEV_ONBOARD
;
315 onboard_dev
->dev
.name
= (char *)&onboard_dev
[1];
316 onboard_dev
->dev
.device_data
= onboard_dev
;
318 list_add(&onboard_dev
->dev
.list
, &dmi_devices
);
321 static void __init
dmi_save_extended_devices(const struct dmi_header
*dm
)
323 const u8
*d
= (u8
*) dm
+ 5;
325 /* Skip disabled device */
326 if ((*d
& 0x80) == 0)
329 dmi_save_dev_onboard(*(d
+1), *(u16
*)(d
+2), *(d
+4), *(d
+5),
330 dmi_string_nosave(dm
, *(d
-1)));
331 dmi_save_one_device(*d
& 0x7f, dmi_string_nosave(dm
, *(d
- 1)));
335 * Process a DMI table entry. Right now all we care about are the BIOS
336 * and machine entries. For 2.5 we should pull the smbus controller info
339 static void __init
dmi_decode(const struct dmi_header
*dm
, void *dummy
)
342 case 0: /* BIOS Information */
343 dmi_save_ident(dm
, DMI_BIOS_VENDOR
, 4);
344 dmi_save_ident(dm
, DMI_BIOS_VERSION
, 5);
345 dmi_save_ident(dm
, DMI_BIOS_DATE
, 8);
347 case 1: /* System Information */
348 dmi_save_ident(dm
, DMI_SYS_VENDOR
, 4);
349 dmi_save_ident(dm
, DMI_PRODUCT_NAME
, 5);
350 dmi_save_ident(dm
, DMI_PRODUCT_VERSION
, 6);
351 dmi_save_ident(dm
, DMI_PRODUCT_SERIAL
, 7);
352 dmi_save_uuid(dm
, DMI_PRODUCT_UUID
, 8);
354 case 2: /* Base Board Information */
355 dmi_save_ident(dm
, DMI_BOARD_VENDOR
, 4);
356 dmi_save_ident(dm
, DMI_BOARD_NAME
, 5);
357 dmi_save_ident(dm
, DMI_BOARD_VERSION
, 6);
358 dmi_save_ident(dm
, DMI_BOARD_SERIAL
, 7);
359 dmi_save_ident(dm
, DMI_BOARD_ASSET_TAG
, 8);
361 case 3: /* Chassis Information */
362 dmi_save_ident(dm
, DMI_CHASSIS_VENDOR
, 4);
363 dmi_save_type(dm
, DMI_CHASSIS_TYPE
, 5);
364 dmi_save_ident(dm
, DMI_CHASSIS_VERSION
, 6);
365 dmi_save_ident(dm
, DMI_CHASSIS_SERIAL
, 7);
366 dmi_save_ident(dm
, DMI_CHASSIS_ASSET_TAG
, 8);
368 case 10: /* Onboard Devices Information */
369 dmi_save_devices(dm
);
371 case 11: /* OEM Strings */
372 dmi_save_oem_strings_devices(dm
);
374 case 38: /* IPMI Device Information */
375 dmi_save_ipmi_device(dm
);
377 case 41: /* Onboard Devices Extended Information */
378 dmi_save_extended_devices(dm
);
382 static int __init
print_filtered(char *buf
, size_t len
, const char *info
)
390 for (p
= info
; *p
; p
++)
392 c
+= scnprintf(buf
+ c
, len
- c
, "%c", *p
);
394 c
+= scnprintf(buf
+ c
, len
- c
, "\\x%02x", *p
& 0xff);
398 static void __init
dmi_format_ids(char *buf
, size_t len
)
401 const char *board
; /* Board Name is optional */
403 c
+= print_filtered(buf
+ c
, len
- c
,
404 dmi_get_system_info(DMI_SYS_VENDOR
));
405 c
+= scnprintf(buf
+ c
, len
- c
, " ");
406 c
+= print_filtered(buf
+ c
, len
- c
,
407 dmi_get_system_info(DMI_PRODUCT_NAME
));
409 board
= dmi_get_system_info(DMI_BOARD_NAME
);
411 c
+= scnprintf(buf
+ c
, len
- c
, "/");
412 c
+= print_filtered(buf
+ c
, len
- c
, board
);
414 c
+= scnprintf(buf
+ c
, len
- c
, ", BIOS ");
415 c
+= print_filtered(buf
+ c
, len
- c
,
416 dmi_get_system_info(DMI_BIOS_VERSION
));
417 c
+= scnprintf(buf
+ c
, len
- c
, " ");
418 c
+= print_filtered(buf
+ c
, len
- c
,
419 dmi_get_system_info(DMI_BIOS_DATE
));
423 * Check for DMI/SMBIOS headers in the system firmware image. Any
424 * SMBIOS header must start 16 bytes before the DMI header, so take a
425 * 32 byte buffer and check for DMI at offset 16 and SMBIOS at offset
426 * 0. If the DMI header is present, set dmi_ver accordingly (SMBIOS
427 * takes precedence) and return 0. Otherwise return 1.
429 static int __init
dmi_present(const u8
*buf
)
433 if (memcmp(buf
, "_SM_", 4) == 0 &&
434 buf
[5] < 32 && dmi_checksum(buf
, buf
[5])) {
435 smbios_ver
= (buf
[6] << 8) + buf
[7];
437 /* Some BIOS report weird SMBIOS version, fix that up */
438 switch (smbios_ver
) {
441 pr_debug("SMBIOS version fixup(2.%d->2.%d)\n",
442 smbios_ver
& 0xFF, 3);
446 pr_debug("SMBIOS version fixup(2.%d->2.%d)\n", 51, 6);
456 if (memcmp(buf
, "_DMI_", 5) == 0 && dmi_checksum(buf
, 15)) {
457 dmi_num
= (buf
[13] << 8) | buf
[12];
458 dmi_len
= (buf
[7] << 8) | buf
[6];
459 dmi_base
= (buf
[11] << 24) | (buf
[10] << 16) |
460 (buf
[9] << 8) | buf
[8];
462 if (dmi_walk_early(dmi_decode
) == 0) {
464 dmi_ver
= smbios_ver
;
465 pr_info("SMBIOS %d.%d present.\n",
466 dmi_ver
>> 8, dmi_ver
& 0xFF);
468 dmi_ver
= (buf
[14] & 0xF0) << 4 |
470 pr_info("Legacy DMI %d.%d present.\n",
471 dmi_ver
>> 8, dmi_ver
& 0xFF);
473 dmi_format_ids(dmi_ids_string
, sizeof(dmi_ids_string
));
474 printk(KERN_DEBUG
"DMI: %s\n", dmi_ids_string
);
482 void __init
dmi_scan_machine(void)
487 if (efi_enabled(EFI_CONFIG_TABLES
)) {
488 if (efi
.smbios
== EFI_INVALID_TABLE_ADDR
)
491 /* This is called as a core_initcall() because it isn't
492 * needed during early boot. This also means we can
493 * iounmap the space when we're done with it.
495 p
= dmi_ioremap(efi
.smbios
, 32);
498 memcpy_fromio(buf
, p
, 32);
501 if (!dmi_present(buf
)) {
508 * no iounmap() for that ioremap(); it would be a no-op, but
509 * it's so early in setup that sucker gets confused into doing
510 * what it shouldn't if we actually call it.
512 p
= dmi_ioremap(0xF0000, 0x10000);
517 * Iterate over all possible DMI header addresses q.
518 * Maintain the 32 bytes around q in buf. On the
519 * first iteration, substitute zero for the
520 * out-of-range bytes so there is no chance of falsely
521 * detecting an SMBIOS header.
524 for (q
= p
; q
< p
+ 0x10000; q
+= 16) {
525 memcpy_fromio(buf
+ 16, q
, 16);
526 if (!dmi_present(buf
)) {
528 dmi_iounmap(p
, 0x10000);
531 memcpy(buf
, buf
+ 16, 16);
533 dmi_iounmap(p
, 0x10000);
536 printk(KERN_INFO
"DMI not present or invalid.\n");
542 * dmi_set_dump_stack_arch_desc - set arch description for dump_stack()
544 * Invoke dump_stack_set_arch_desc() with DMI system information so that
545 * DMI identifiers are printed out on task dumps. Arch boot code should
546 * call this function after dmi_scan_machine() if it wants to print out DMI
547 * identifiers on task dumps.
549 void __init
dmi_set_dump_stack_arch_desc(void)
551 dump_stack_set_arch_desc("%s", dmi_ids_string
);
555 * dmi_matches - check if dmi_system_id structure matches system DMI data
556 * @dmi: pointer to the dmi_system_id structure to check
558 static bool dmi_matches(const struct dmi_system_id
*dmi
)
562 WARN(!dmi_initialized
, KERN_ERR
"dmi check: not initialized yet.\n");
564 for (i
= 0; i
< ARRAY_SIZE(dmi
->matches
); i
++) {
565 int s
= dmi
->matches
[i
].slot
;
569 if (!dmi
->matches
[i
].exact_match
&&
570 strstr(dmi_ident
[s
], dmi
->matches
[i
].substr
))
572 else if (dmi
->matches
[i
].exact_match
&&
573 !strcmp(dmi_ident
[s
], dmi
->matches
[i
].substr
))
584 * dmi_is_end_of_table - check for end-of-table marker
585 * @dmi: pointer to the dmi_system_id structure to check
587 static bool dmi_is_end_of_table(const struct dmi_system_id
*dmi
)
589 return dmi
->matches
[0].slot
== DMI_NONE
;
593 * dmi_check_system - check system DMI data
594 * @list: array of dmi_system_id structures to match against
595 * All non-null elements of the list must match
596 * their slot's (field index's) data (i.e., each
597 * list string must be a substring of the specified
598 * DMI slot's string data) to be considered a
601 * Walk the blacklist table running matching functions until someone
602 * returns non zero or we hit the end. Callback function is called for
603 * each successful match. Returns the number of matches.
605 int dmi_check_system(const struct dmi_system_id
*list
)
608 const struct dmi_system_id
*d
;
610 for (d
= list
; !dmi_is_end_of_table(d
); d
++)
611 if (dmi_matches(d
)) {
613 if (d
->callback
&& d
->callback(d
))
619 EXPORT_SYMBOL(dmi_check_system
);
622 * dmi_first_match - find dmi_system_id structure matching system DMI data
623 * @list: array of dmi_system_id structures to match against
624 * All non-null elements of the list must match
625 * their slot's (field index's) data (i.e., each
626 * list string must be a substring of the specified
627 * DMI slot's string data) to be considered a
630 * Walk the blacklist table until the first match is found. Return the
631 * pointer to the matching entry or NULL if there's no match.
633 const struct dmi_system_id
*dmi_first_match(const struct dmi_system_id
*list
)
635 const struct dmi_system_id
*d
;
637 for (d
= list
; !dmi_is_end_of_table(d
); d
++)
643 EXPORT_SYMBOL(dmi_first_match
);
646 * dmi_get_system_info - return DMI data value
647 * @field: data index (see enum dmi_field)
649 * Returns one DMI data value, can be used to perform
650 * complex DMI data checks.
652 const char *dmi_get_system_info(int field
)
654 return dmi_ident
[field
];
656 EXPORT_SYMBOL(dmi_get_system_info
);
659 * dmi_name_in_serial - Check if string is in the DMI product serial information
660 * @str: string to check for
662 int dmi_name_in_serial(const char *str
)
664 int f
= DMI_PRODUCT_SERIAL
;
665 if (dmi_ident
[f
] && strstr(dmi_ident
[f
], str
))
671 * dmi_name_in_vendors - Check if string is in the DMI system or board vendor name
672 * @str: Case sensitive Name
674 int dmi_name_in_vendors(const char *str
)
676 static int fields
[] = { DMI_SYS_VENDOR
, DMI_BOARD_VENDOR
, DMI_NONE
};
678 for (i
= 0; fields
[i
] != DMI_NONE
; i
++) {
680 if (dmi_ident
[f
] && strstr(dmi_ident
[f
], str
))
685 EXPORT_SYMBOL(dmi_name_in_vendors
);
688 * dmi_find_device - find onboard device by type/name
689 * @type: device type or %DMI_DEV_TYPE_ANY to match all device types
690 * @name: device name string or %NULL to match all
691 * @from: previous device found in search, or %NULL for new search.
693 * Iterates through the list of known onboard devices. If a device is
694 * found with a matching @vendor and @device, a pointer to its device
695 * structure is returned. Otherwise, %NULL is returned.
696 * A new search is initiated by passing %NULL as the @from argument.
697 * If @from is not %NULL, searches continue from next device.
699 const struct dmi_device
* dmi_find_device(int type
, const char *name
,
700 const struct dmi_device
*from
)
702 const struct list_head
*head
= from
? &from
->list
: &dmi_devices
;
705 for(d
= head
->next
; d
!= &dmi_devices
; d
= d
->next
) {
706 const struct dmi_device
*dev
=
707 list_entry(d
, struct dmi_device
, list
);
709 if (((type
== DMI_DEV_TYPE_ANY
) || (dev
->type
== type
)) &&
710 ((name
== NULL
) || (strcmp(dev
->name
, name
) == 0)))
716 EXPORT_SYMBOL(dmi_find_device
);
719 * dmi_get_date - parse a DMI date
720 * @field: data index (see enum dmi_field)
721 * @yearp: optional out parameter for the year
722 * @monthp: optional out parameter for the month
723 * @dayp: optional out parameter for the day
725 * The date field is assumed to be in the form resembling
726 * [mm[/dd]]/yy[yy] and the result is stored in the out
727 * parameters any or all of which can be omitted.
729 * If the field doesn't exist, all out parameters are set to zero
730 * and false is returned. Otherwise, true is returned with any
731 * invalid part of date set to zero.
733 * On return, year, month and day are guaranteed to be in the
734 * range of [0,9999], [0,12] and [0,31] respectively.
736 bool dmi_get_date(int field
, int *yearp
, int *monthp
, int *dayp
)
738 int year
= 0, month
= 0, day
= 0;
743 s
= dmi_get_system_info(field
);
749 * Determine year first. We assume the date string resembles
750 * mm/dd/yy[yy] but the original code extracted only the year
751 * from the end. Keep the behavior in the spirit of no
759 year
= simple_strtoul(y
, &e
, 10);
760 if (y
!= e
&& year
< 100) { /* 2-digit year */
762 if (year
< 1996) /* no dates < spec 1.0 */
765 if (year
> 9999) /* year should fit in %04d */
768 /* parse the mm and dd */
769 month
= simple_strtoul(s
, &e
, 10);
770 if (s
== e
|| *e
!= '/' || !month
|| month
> 12) {
776 day
= simple_strtoul(s
, &e
, 10);
777 if (s
== y
|| s
== e
|| *e
!= '/' || day
> 31)
788 EXPORT_SYMBOL(dmi_get_date
);
791 * dmi_walk - Walk the DMI table and get called back for every record
792 * @decode: Callback function
793 * @private_data: Private data to be passed to the callback function
795 * Returns -1 when the DMI table can't be reached, 0 on success.
797 int dmi_walk(void (*decode
)(const struct dmi_header
*, void *),
805 buf
= ioremap(dmi_base
, dmi_len
);
809 dmi_table(buf
, dmi_len
, dmi_num
, decode
, private_data
);
814 EXPORT_SYMBOL_GPL(dmi_walk
);
817 * dmi_match - compare a string to the dmi field (if exists)
818 * @f: DMI field identifier
819 * @str: string to compare the DMI field to
821 * Returns true if the requested field equals to the str (including NULL).
823 bool dmi_match(enum dmi_field f
, const char *str
)
825 const char *info
= dmi_get_system_info(f
);
827 if (info
== NULL
|| str
== NULL
)
830 return !strcmp(info
, str
);
832 EXPORT_SYMBOL_GPL(dmi_match
);
This page took 0.178046 seconds and 6 git commands to generate.