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 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
) {
37 size_t len
= strlen(bp
)+1;
38 size_t cmp_len
= len
> 8 ? 8 : len
;
40 if (!memcmp(bp
, dmi_empty_string
, cmp_len
))
41 return dmi_empty_string
;
49 static char * __init
dmi_string(const struct dmi_header
*dm
, u8 s
)
51 const char *bp
= dmi_string_nosave(dm
, s
);
55 if (bp
== dmi_empty_string
)
56 return dmi_empty_string
;
63 printk(KERN_ERR
"dmi_string: cannot allocate %Zu bytes.\n", len
);
69 * We have to be cautious here. We have seen BIOSes with DMI pointers
70 * pointing to completely the wrong place for example
72 static void dmi_table(u8
*buf
, int len
, int num
,
73 void (*decode
)(const struct dmi_header
*, void *),
80 * Stop when we see all the items the table claimed to have
81 * OR we run off the end of the table (also happens)
83 while ((i
< num
) && (data
- buf
+ sizeof(struct dmi_header
)) <= len
) {
84 const struct dmi_header
*dm
= (const struct dmi_header
*)data
;
87 * We want to know the total length (formatted area and
88 * strings) before decoding to make sure we won't run off the
89 * table in dmi_decode or dmi_string
92 while ((data
- buf
< len
- 1) && (data
[0] || data
[1]))
94 if (data
- buf
< len
- 1)
95 decode(dm
, private_data
);
105 static int __init
dmi_walk_early(void (*decode
)(const struct dmi_header
*,
110 buf
= dmi_ioremap(dmi_base
, dmi_len
);
114 dmi_table(buf
, dmi_len
, dmi_num
, decode
, NULL
);
116 add_device_randomness(buf
, dmi_len
);
118 dmi_iounmap(buf
, dmi_len
);
122 static int __init
dmi_checksum(const u8
*buf
, u8 len
)
127 for (a
= 0; a
< len
; a
++)
133 static char *dmi_ident
[DMI_STRING_MAX
];
134 static LIST_HEAD(dmi_devices
);
140 static void __init
dmi_save_ident(const struct dmi_header
*dm
, int slot
, int string
)
142 const char *d
= (const char*) dm
;
148 p
= dmi_string(dm
, d
[string
]);
155 static void __init
dmi_save_uuid(const struct dmi_header
*dm
, int slot
, int index
)
157 const u8
*d
= (u8
*) dm
+ index
;
159 int is_ff
= 1, is_00
= 1, i
;
164 for (i
= 0; i
< 16 && (is_ff
|| is_00
); i
++) {
174 s
= dmi_alloc(16*2+4+1);
179 * As of version 2.6 of the SMBIOS specification, the first 3 fields of
180 * the UUID are supposed to be little-endian encoded. The specification
181 * says that this is the defacto standard.
183 if (dmi_ver
>= 0x0206)
184 sprintf(s
, "%pUL", d
);
186 sprintf(s
, "%pUB", d
);
191 static void __init
dmi_save_type(const struct dmi_header
*dm
, int slot
, int index
)
193 const u8
*d
= (u8
*) dm
+ index
;
203 sprintf(s
, "%u", *d
& 0x7F);
207 static void __init
dmi_save_one_device(int type
, const char *name
)
209 struct dmi_device
*dev
;
211 /* No duplicate device */
212 if (dmi_find_device(type
, name
, NULL
))
215 dev
= dmi_alloc(sizeof(*dev
) + strlen(name
) + 1);
217 printk(KERN_ERR
"dmi_save_one_device: out of memory.\n");
222 strcpy((char *)(dev
+ 1), name
);
223 dev
->name
= (char *)(dev
+ 1);
224 dev
->device_data
= NULL
;
225 list_add(&dev
->list
, &dmi_devices
);
228 static void __init
dmi_save_devices(const struct dmi_header
*dm
)
230 int i
, count
= (dm
->length
- sizeof(struct dmi_header
)) / 2;
232 for (i
= 0; i
< count
; i
++) {
233 const char *d
= (char *)(dm
+ 1) + (i
* 2);
235 /* Skip disabled device */
236 if ((*d
& 0x80) == 0)
239 dmi_save_one_device(*d
& 0x7f, dmi_string_nosave(dm
, *(d
+ 1)));
243 static void __init
dmi_save_oem_strings_devices(const struct dmi_header
*dm
)
245 int i
, count
= *(u8
*)(dm
+ 1);
246 struct dmi_device
*dev
;
248 for (i
= 1; i
<= count
; i
++) {
249 char *devname
= dmi_string(dm
, i
);
251 if (devname
== dmi_empty_string
)
254 dev
= dmi_alloc(sizeof(*dev
));
257 "dmi_save_oem_strings_devices: out of memory.\n");
261 dev
->type
= DMI_DEV_TYPE_OEM_STRING
;
263 dev
->device_data
= NULL
;
265 list_add(&dev
->list
, &dmi_devices
);
269 static void __init
dmi_save_ipmi_device(const struct dmi_header
*dm
)
271 struct dmi_device
*dev
;
274 data
= dmi_alloc(dm
->length
);
276 printk(KERN_ERR
"dmi_save_ipmi_device: out of memory.\n");
280 memcpy(data
, dm
, dm
->length
);
282 dev
= dmi_alloc(sizeof(*dev
));
284 printk(KERN_ERR
"dmi_save_ipmi_device: out of memory.\n");
288 dev
->type
= DMI_DEV_TYPE_IPMI
;
289 dev
->name
= "IPMI controller";
290 dev
->device_data
= data
;
292 list_add_tail(&dev
->list
, &dmi_devices
);
295 static void __init
dmi_save_dev_onboard(int instance
, int segment
, int bus
,
296 int devfn
, const char *name
)
298 struct dmi_dev_onboard
*onboard_dev
;
300 onboard_dev
= dmi_alloc(sizeof(*onboard_dev
) + strlen(name
) + 1);
302 printk(KERN_ERR
"dmi_save_dev_onboard: out of memory.\n");
305 onboard_dev
->instance
= instance
;
306 onboard_dev
->segment
= segment
;
307 onboard_dev
->bus
= bus
;
308 onboard_dev
->devfn
= devfn
;
310 strcpy((char *)&onboard_dev
[1], name
);
311 onboard_dev
->dev
.type
= DMI_DEV_TYPE_DEV_ONBOARD
;
312 onboard_dev
->dev
.name
= (char *)&onboard_dev
[1];
313 onboard_dev
->dev
.device_data
= onboard_dev
;
315 list_add(&onboard_dev
->dev
.list
, &dmi_devices
);
318 static void __init
dmi_save_extended_devices(const struct dmi_header
*dm
)
320 const u8
*d
= (u8
*) dm
+ 5;
322 /* Skip disabled device */
323 if ((*d
& 0x80) == 0)
326 dmi_save_dev_onboard(*(d
+1), *(u16
*)(d
+2), *(d
+4), *(d
+5),
327 dmi_string_nosave(dm
, *(d
-1)));
328 dmi_save_one_device(*d
& 0x7f, dmi_string_nosave(dm
, *(d
- 1)));
332 * Process a DMI table entry. Right now all we care about are the BIOS
333 * and machine entries. For 2.5 we should pull the smbus controller info
336 static void __init
dmi_decode(const struct dmi_header
*dm
, void *dummy
)
339 case 0: /* BIOS Information */
340 dmi_save_ident(dm
, DMI_BIOS_VENDOR
, 4);
341 dmi_save_ident(dm
, DMI_BIOS_VERSION
, 5);
342 dmi_save_ident(dm
, DMI_BIOS_DATE
, 8);
344 case 1: /* System Information */
345 dmi_save_ident(dm
, DMI_SYS_VENDOR
, 4);
346 dmi_save_ident(dm
, DMI_PRODUCT_NAME
, 5);
347 dmi_save_ident(dm
, DMI_PRODUCT_VERSION
, 6);
348 dmi_save_ident(dm
, DMI_PRODUCT_SERIAL
, 7);
349 dmi_save_uuid(dm
, DMI_PRODUCT_UUID
, 8);
351 case 2: /* Base Board Information */
352 dmi_save_ident(dm
, DMI_BOARD_VENDOR
, 4);
353 dmi_save_ident(dm
, DMI_BOARD_NAME
, 5);
354 dmi_save_ident(dm
, DMI_BOARD_VERSION
, 6);
355 dmi_save_ident(dm
, DMI_BOARD_SERIAL
, 7);
356 dmi_save_ident(dm
, DMI_BOARD_ASSET_TAG
, 8);
358 case 3: /* Chassis Information */
359 dmi_save_ident(dm
, DMI_CHASSIS_VENDOR
, 4);
360 dmi_save_type(dm
, DMI_CHASSIS_TYPE
, 5);
361 dmi_save_ident(dm
, DMI_CHASSIS_VERSION
, 6);
362 dmi_save_ident(dm
, DMI_CHASSIS_SERIAL
, 7);
363 dmi_save_ident(dm
, DMI_CHASSIS_ASSET_TAG
, 8);
365 case 10: /* Onboard Devices Information */
366 dmi_save_devices(dm
);
368 case 11: /* OEM Strings */
369 dmi_save_oem_strings_devices(dm
);
371 case 38: /* IPMI Device Information */
372 dmi_save_ipmi_device(dm
);
374 case 41: /* Onboard Devices Extended Information */
375 dmi_save_extended_devices(dm
);
379 static void __init
print_filtered(const char *info
)
386 for (p
= info
; *p
; p
++)
388 printk(KERN_CONT
"%c", *p
);
390 printk(KERN_CONT
"\\x%02x", *p
& 0xff);
393 static void __init
dmi_dump_ids(void)
395 const char *board
; /* Board Name is optional */
397 printk(KERN_DEBUG
"DMI: ");
398 print_filtered(dmi_get_system_info(DMI_SYS_VENDOR
));
399 printk(KERN_CONT
" ");
400 print_filtered(dmi_get_system_info(DMI_PRODUCT_NAME
));
401 board
= dmi_get_system_info(DMI_BOARD_NAME
);
403 printk(KERN_CONT
"/");
404 print_filtered(board
);
406 printk(KERN_CONT
", BIOS ");
407 print_filtered(dmi_get_system_info(DMI_BIOS_VERSION
));
408 printk(KERN_CONT
" ");
409 print_filtered(dmi_get_system_info(DMI_BIOS_DATE
));
410 printk(KERN_CONT
"\n");
413 static int __init
dmi_present(const char __iomem
*p
)
417 memcpy_fromio(buf
, p
, 15);
418 if (dmi_checksum(buf
, 15)) {
419 dmi_num
= (buf
[13] << 8) | buf
[12];
420 dmi_len
= (buf
[7] << 8) | buf
[6];
421 dmi_base
= (buf
[11] << 24) | (buf
[10] << 16) |
422 (buf
[9] << 8) | buf
[8];
424 if (dmi_walk_early(dmi_decode
) == 0) {
426 pr_info("SMBIOS %d.%d present.\n",
427 dmi_ver
>> 8, dmi_ver
& 0xFF);
429 dmi_ver
= (buf
[14] & 0xF0) << 4 |
431 pr_info("Legacy DMI %d.%d present.\n",
432 dmi_ver
>> 8, dmi_ver
& 0xFF);
442 static int __init
smbios_present(const char __iomem
*p
)
446 memcpy_fromio(buf
, p
, 32);
447 if ((buf
[5] < 32) && dmi_checksum(buf
, buf
[5])) {
448 dmi_ver
= (buf
[6] << 8) + buf
[7];
450 /* Some BIOS report weird SMBIOS version, fix that up */
454 pr_debug("SMBIOS version fixup(2.%d->2.%d)\n",
459 pr_debug("SMBIOS version fixup(2.%d->2.%d)\n", 51, 6);
463 return memcmp(p
+ 16, "_DMI_", 5) || dmi_present(p
+ 16);
468 void __init
dmi_scan_machine(void)
473 if (efi_enabled(EFI_CONFIG_TABLES
)) {
474 if (efi
.smbios
== EFI_INVALID_TABLE_ADDR
)
477 /* This is called as a core_initcall() because it isn't
478 * needed during early boot. This also means we can
479 * iounmap the space when we're done with it.
481 p
= dmi_ioremap(efi
.smbios
, 32);
485 rc
= smbios_present(p
);
494 * no iounmap() for that ioremap(); it would be a no-op, but
495 * it's so early in setup that sucker gets confused into doing
496 * what it shouldn't if we actually call it.
498 p
= dmi_ioremap(0xF0000, 0x10000);
502 for (q
= p
; q
< p
+ 0x10000; q
+= 16) {
503 if (memcmp(q
, "_SM_", 4) == 0 && q
- p
<= 0xFFE0)
504 rc
= smbios_present(q
);
505 else if (memcmp(q
, "_DMI_", 5) == 0)
511 dmi_iounmap(p
, 0x10000);
515 dmi_iounmap(p
, 0x10000);
518 printk(KERN_INFO
"DMI not present or invalid.\n");
524 * dmi_matches - check if dmi_system_id structure matches system DMI data
525 * @dmi: pointer to the dmi_system_id structure to check
527 static bool dmi_matches(const struct dmi_system_id
*dmi
)
531 WARN(!dmi_initialized
, KERN_ERR
"dmi check: not initialized yet.\n");
533 for (i
= 0; i
< ARRAY_SIZE(dmi
->matches
); i
++) {
534 int s
= dmi
->matches
[i
].slot
;
538 && strstr(dmi_ident
[s
], dmi
->matches
[i
].substr
))
547 * dmi_is_end_of_table - check for end-of-table marker
548 * @dmi: pointer to the dmi_system_id structure to check
550 static bool dmi_is_end_of_table(const struct dmi_system_id
*dmi
)
552 return dmi
->matches
[0].slot
== DMI_NONE
;
556 * dmi_check_system - check system DMI data
557 * @list: array of dmi_system_id structures to match against
558 * All non-null elements of the list must match
559 * their slot's (field index's) data (i.e., each
560 * list string must be a substring of the specified
561 * DMI slot's string data) to be considered a
564 * Walk the blacklist table running matching functions until someone
565 * returns non zero or we hit the end. Callback function is called for
566 * each successful match. Returns the number of matches.
568 int dmi_check_system(const struct dmi_system_id
*list
)
571 const struct dmi_system_id
*d
;
573 for (d
= list
; !dmi_is_end_of_table(d
); d
++)
574 if (dmi_matches(d
)) {
576 if (d
->callback
&& d
->callback(d
))
582 EXPORT_SYMBOL(dmi_check_system
);
585 * dmi_first_match - find dmi_system_id structure matching system DMI data
586 * @list: array of dmi_system_id structures to match against
587 * All non-null elements of the list must match
588 * their slot's (field index's) data (i.e., each
589 * list string must be a substring of the specified
590 * DMI slot's string data) to be considered a
593 * Walk the blacklist table until the first match is found. Return the
594 * pointer to the matching entry or NULL if there's no match.
596 const struct dmi_system_id
*dmi_first_match(const struct dmi_system_id
*list
)
598 const struct dmi_system_id
*d
;
600 for (d
= list
; !dmi_is_end_of_table(d
); d
++)
606 EXPORT_SYMBOL(dmi_first_match
);
609 * dmi_get_system_info - return DMI data value
610 * @field: data index (see enum dmi_field)
612 * Returns one DMI data value, can be used to perform
613 * complex DMI data checks.
615 const char *dmi_get_system_info(int field
)
617 return dmi_ident
[field
];
619 EXPORT_SYMBOL(dmi_get_system_info
);
622 * dmi_name_in_serial - Check if string is in the DMI product serial information
623 * @str: string to check for
625 int dmi_name_in_serial(const char *str
)
627 int f
= DMI_PRODUCT_SERIAL
;
628 if (dmi_ident
[f
] && strstr(dmi_ident
[f
], str
))
634 * dmi_name_in_vendors - Check if string is in the DMI system or board vendor name
635 * @str: Case sensitive Name
637 int dmi_name_in_vendors(const char *str
)
639 static int fields
[] = { DMI_SYS_VENDOR
, DMI_BOARD_VENDOR
, DMI_NONE
};
641 for (i
= 0; fields
[i
] != DMI_NONE
; i
++) {
643 if (dmi_ident
[f
] && strstr(dmi_ident
[f
], str
))
648 EXPORT_SYMBOL(dmi_name_in_vendors
);
651 * dmi_find_device - find onboard device by type/name
652 * @type: device type or %DMI_DEV_TYPE_ANY to match all device types
653 * @name: device name string or %NULL to match all
654 * @from: previous device found in search, or %NULL for new search.
656 * Iterates through the list of known onboard devices. If a device is
657 * found with a matching @vendor and @device, a pointer to its device
658 * structure is returned. Otherwise, %NULL is returned.
659 * A new search is initiated by passing %NULL as the @from argument.
660 * If @from is not %NULL, searches continue from next device.
662 const struct dmi_device
* dmi_find_device(int type
, const char *name
,
663 const struct dmi_device
*from
)
665 const struct list_head
*head
= from
? &from
->list
: &dmi_devices
;
668 for(d
= head
->next
; d
!= &dmi_devices
; d
= d
->next
) {
669 const struct dmi_device
*dev
=
670 list_entry(d
, struct dmi_device
, list
);
672 if (((type
== DMI_DEV_TYPE_ANY
) || (dev
->type
== type
)) &&
673 ((name
== NULL
) || (strcmp(dev
->name
, name
) == 0)))
679 EXPORT_SYMBOL(dmi_find_device
);
682 * dmi_get_date - parse a DMI date
683 * @field: data index (see enum dmi_field)
684 * @yearp: optional out parameter for the year
685 * @monthp: optional out parameter for the month
686 * @dayp: optional out parameter for the day
688 * The date field is assumed to be in the form resembling
689 * [mm[/dd]]/yy[yy] and the result is stored in the out
690 * parameters any or all of which can be omitted.
692 * If the field doesn't exist, all out parameters are set to zero
693 * and false is returned. Otherwise, true is returned with any
694 * invalid part of date set to zero.
696 * On return, year, month and day are guaranteed to be in the
697 * range of [0,9999], [0,12] and [0,31] respectively.
699 bool dmi_get_date(int field
, int *yearp
, int *monthp
, int *dayp
)
701 int year
= 0, month
= 0, day
= 0;
706 s
= dmi_get_system_info(field
);
712 * Determine year first. We assume the date string resembles
713 * mm/dd/yy[yy] but the original code extracted only the year
714 * from the end. Keep the behavior in the spirit of no
722 year
= simple_strtoul(y
, &e
, 10);
723 if (y
!= e
&& year
< 100) { /* 2-digit year */
725 if (year
< 1996) /* no dates < spec 1.0 */
728 if (year
> 9999) /* year should fit in %04d */
731 /* parse the mm and dd */
732 month
= simple_strtoul(s
, &e
, 10);
733 if (s
== e
|| *e
!= '/' || !month
|| month
> 12) {
739 day
= simple_strtoul(s
, &e
, 10);
740 if (s
== y
|| s
== e
|| *e
!= '/' || day
> 31)
751 EXPORT_SYMBOL(dmi_get_date
);
754 * dmi_walk - Walk the DMI table and get called back for every record
755 * @decode: Callback function
756 * @private_data: Private data to be passed to the callback function
758 * Returns -1 when the DMI table can't be reached, 0 on success.
760 int dmi_walk(void (*decode
)(const struct dmi_header
*, void *),
768 buf
= ioremap(dmi_base
, dmi_len
);
772 dmi_table(buf
, dmi_len
, dmi_num
, decode
, private_data
);
777 EXPORT_SYMBOL_GPL(dmi_walk
);
780 * dmi_match - compare a string to the dmi field (if exists)
781 * @f: DMI field identifier
782 * @str: string to compare the DMI field to
784 * Returns true if the requested field equals to the str (including NULL).
786 bool dmi_match(enum dmi_field f
, const char *str
)
788 const char *info
= dmi_get_system_info(f
);
790 if (info
== NULL
|| str
== NULL
)
793 return !strcmp(info
, str
);
795 EXPORT_SYMBOL_GPL(dmi_match
);