1 #include <linux/types.h>
2 #include <linux/string.h>
3 #include <linux/init.h>
4 #include <linux/module.h>
7 #include <linux/bootmem.h>
8 #include <linux/slab.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
[] = " ";
20 * Catch too early calls to dmi_check_system():
22 static int dmi_initialized
;
24 static const char * __init
dmi_string_nosave(const struct dmi_header
*dm
, u8 s
)
26 const u8
*bp
= ((u8
*) dm
) + dm
->length
;
30 while (s
> 0 && *bp
) {
36 size_t len
= strlen(bp
)+1;
37 size_t cmp_len
= len
> 8 ? 8 : len
;
39 if (!memcmp(bp
, dmi_empty_string
, cmp_len
))
40 return dmi_empty_string
;
48 static char * __init
dmi_string(const struct dmi_header
*dm
, u8 s
)
50 const char *bp
= dmi_string_nosave(dm
, s
);
54 if (bp
== dmi_empty_string
)
55 return dmi_empty_string
;
62 printk(KERN_ERR
"dmi_string: cannot allocate %Zu bytes.\n", len
);
68 * We have to be cautious here. We have seen BIOSes with DMI pointers
69 * pointing to completely the wrong place for example
71 static void dmi_table(u8
*buf
, int len
, int num
,
72 void (*decode
)(const struct dmi_header
*, void *),
79 * Stop when we see all the items the table claimed to have
80 * OR we run off the end of the table (also happens)
82 while ((i
< num
) && (data
- buf
+ sizeof(struct dmi_header
)) <= len
) {
83 const struct dmi_header
*dm
= (const struct dmi_header
*)data
;
86 * We want to know the total length (formatted area and
87 * strings) before decoding to make sure we won't run off the
88 * table in dmi_decode or dmi_string
91 while ((data
- buf
< len
- 1) && (data
[0] || data
[1]))
93 if (data
- buf
< len
- 1)
94 decode(dm
, private_data
);
104 static int __init
dmi_walk_early(void (*decode
)(const struct dmi_header
*,
109 buf
= dmi_ioremap(dmi_base
, dmi_len
);
113 dmi_table(buf
, dmi_len
, dmi_num
, decode
, NULL
);
115 add_device_randomness(buf
, dmi_len
);
117 dmi_iounmap(buf
, dmi_len
);
121 static int __init
dmi_checksum(const u8
*buf
)
126 for (a
= 0; a
< 15; a
++)
132 static char *dmi_ident
[DMI_STRING_MAX
];
133 static LIST_HEAD(dmi_devices
);
139 static void __init
dmi_save_ident(const struct dmi_header
*dm
, int slot
, int string
)
141 const char *d
= (const char*) dm
;
147 p
= dmi_string(dm
, d
[string
]);
154 static void __init
dmi_save_uuid(const struct dmi_header
*dm
, int slot
, int index
)
156 const u8
*d
= (u8
*) dm
+ index
;
158 int is_ff
= 1, is_00
= 1, i
;
163 for (i
= 0; i
< 16 && (is_ff
|| is_00
); i
++) {
164 if(d
[i
] != 0x00) is_ff
= 0;
165 if(d
[i
] != 0xFF) is_00
= 0;
171 s
= dmi_alloc(16*2+4+1);
176 "%02X%02X%02X%02X-%02X%02X-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X",
177 d
[0], d
[1], d
[2], d
[3], d
[4], d
[5], d
[6], d
[7],
178 d
[8], d
[9], d
[10], d
[11], d
[12], d
[13], d
[14], d
[15]);
183 static void __init
dmi_save_type(const struct dmi_header
*dm
, int slot
, int index
)
185 const u8
*d
= (u8
*) dm
+ index
;
195 sprintf(s
, "%u", *d
& 0x7F);
199 static void __init
dmi_save_one_device(int type
, const char *name
)
201 struct dmi_device
*dev
;
203 /* No duplicate device */
204 if (dmi_find_device(type
, name
, NULL
))
207 dev
= dmi_alloc(sizeof(*dev
) + strlen(name
) + 1);
209 printk(KERN_ERR
"dmi_save_one_device: out of memory.\n");
214 strcpy((char *)(dev
+ 1), name
);
215 dev
->name
= (char *)(dev
+ 1);
216 dev
->device_data
= NULL
;
217 list_add(&dev
->list
, &dmi_devices
);
220 static void __init
dmi_save_devices(const struct dmi_header
*dm
)
222 int i
, count
= (dm
->length
- sizeof(struct dmi_header
)) / 2;
224 for (i
= 0; i
< count
; i
++) {
225 const char *d
= (char *)(dm
+ 1) + (i
* 2);
227 /* Skip disabled device */
228 if ((*d
& 0x80) == 0)
231 dmi_save_one_device(*d
& 0x7f, dmi_string_nosave(dm
, *(d
+ 1)));
235 static void __init
dmi_save_oem_strings_devices(const struct dmi_header
*dm
)
237 int i
, count
= *(u8
*)(dm
+ 1);
238 struct dmi_device
*dev
;
240 for (i
= 1; i
<= count
; i
++) {
241 char *devname
= dmi_string(dm
, i
);
243 if (devname
== dmi_empty_string
)
246 dev
= dmi_alloc(sizeof(*dev
));
249 "dmi_save_oem_strings_devices: out of memory.\n");
253 dev
->type
= DMI_DEV_TYPE_OEM_STRING
;
255 dev
->device_data
= NULL
;
257 list_add(&dev
->list
, &dmi_devices
);
261 static void __init
dmi_save_ipmi_device(const struct dmi_header
*dm
)
263 struct dmi_device
*dev
;
266 data
= dmi_alloc(dm
->length
);
268 printk(KERN_ERR
"dmi_save_ipmi_device: out of memory.\n");
272 memcpy(data
, dm
, dm
->length
);
274 dev
= dmi_alloc(sizeof(*dev
));
276 printk(KERN_ERR
"dmi_save_ipmi_device: out of memory.\n");
280 dev
->type
= DMI_DEV_TYPE_IPMI
;
281 dev
->name
= "IPMI controller";
282 dev
->device_data
= data
;
284 list_add_tail(&dev
->list
, &dmi_devices
);
287 static void __init
dmi_save_extended_devices(const struct dmi_header
*dm
)
289 const u8
*d
= (u8
*) dm
+ 5;
291 /* Skip disabled device */
292 if ((*d
& 0x80) == 0)
295 dmi_save_one_device(*d
& 0x7f, dmi_string_nosave(dm
, *(d
- 1)));
299 * Process a DMI table entry. Right now all we care about are the BIOS
300 * and machine entries. For 2.5 we should pull the smbus controller info
303 static void __init
dmi_decode(const struct dmi_header
*dm
, void *dummy
)
306 case 0: /* BIOS Information */
307 dmi_save_ident(dm
, DMI_BIOS_VENDOR
, 4);
308 dmi_save_ident(dm
, DMI_BIOS_VERSION
, 5);
309 dmi_save_ident(dm
, DMI_BIOS_DATE
, 8);
311 case 1: /* System Information */
312 dmi_save_ident(dm
, DMI_SYS_VENDOR
, 4);
313 dmi_save_ident(dm
, DMI_PRODUCT_NAME
, 5);
314 dmi_save_ident(dm
, DMI_PRODUCT_VERSION
, 6);
315 dmi_save_ident(dm
, DMI_PRODUCT_SERIAL
, 7);
316 dmi_save_uuid(dm
, DMI_PRODUCT_UUID
, 8);
318 case 2: /* Base Board Information */
319 dmi_save_ident(dm
, DMI_BOARD_VENDOR
, 4);
320 dmi_save_ident(dm
, DMI_BOARD_NAME
, 5);
321 dmi_save_ident(dm
, DMI_BOARD_VERSION
, 6);
322 dmi_save_ident(dm
, DMI_BOARD_SERIAL
, 7);
323 dmi_save_ident(dm
, DMI_BOARD_ASSET_TAG
, 8);
325 case 3: /* Chassis Information */
326 dmi_save_ident(dm
, DMI_CHASSIS_VENDOR
, 4);
327 dmi_save_type(dm
, DMI_CHASSIS_TYPE
, 5);
328 dmi_save_ident(dm
, DMI_CHASSIS_VERSION
, 6);
329 dmi_save_ident(dm
, DMI_CHASSIS_SERIAL
, 7);
330 dmi_save_ident(dm
, DMI_CHASSIS_ASSET_TAG
, 8);
332 case 10: /* Onboard Devices Information */
333 dmi_save_devices(dm
);
335 case 11: /* OEM Strings */
336 dmi_save_oem_strings_devices(dm
);
338 case 38: /* IPMI Device Information */
339 dmi_save_ipmi_device(dm
);
341 case 41: /* Onboard Devices Extended Information */
342 dmi_save_extended_devices(dm
);
346 static int __init
dmi_present(const char __iomem
*p
)
350 memcpy_fromio(buf
, p
, 15);
351 if ((memcmp(buf
, "_DMI_", 5) == 0) && dmi_checksum(buf
)) {
352 dmi_num
= (buf
[13] << 8) | buf
[12];
353 dmi_len
= (buf
[7] << 8) | buf
[6];
354 dmi_base
= (buf
[11] << 24) | (buf
[10] << 16) |
355 (buf
[9] << 8) | buf
[8];
358 * DMI version 0.0 means that the real version is taken from
359 * the SMBIOS version, which we don't know at this point.
362 printk(KERN_INFO
"DMI %d.%d present.\n",
363 buf
[14] >> 4, buf
[14] & 0xF);
365 printk(KERN_INFO
"DMI present.\n");
366 if (dmi_walk_early(dmi_decode
) == 0)
372 void __init
dmi_scan_machine(void)
378 if (efi
.smbios
== EFI_INVALID_TABLE_ADDR
)
381 /* This is called as a core_initcall() because it isn't
382 * needed during early boot. This also means we can
383 * iounmap the space when we're done with it.
385 p
= dmi_ioremap(efi
.smbios
, 32);
389 rc
= dmi_present(p
+ 0x10); /* offset of _DMI_ string */
398 * no iounmap() for that ioremap(); it would be a no-op, but
399 * it's so early in setup that sucker gets confused into doing
400 * what it shouldn't if we actually call it.
402 p
= dmi_ioremap(0xF0000, 0x10000);
406 for (q
= p
; q
< p
+ 0x10000; q
+= 16) {
410 dmi_iounmap(p
, 0x10000);
414 dmi_iounmap(p
, 0x10000);
417 printk(KERN_INFO
"DMI not present or invalid.\n");
423 * dmi_matches - check if dmi_system_id structure matches system DMI data
424 * @dmi: pointer to the dmi_system_id structure to check
426 static bool dmi_matches(const struct dmi_system_id
*dmi
)
430 WARN(!dmi_initialized
, KERN_ERR
"dmi check: not initialized yet.\n");
432 for (i
= 0; i
< ARRAY_SIZE(dmi
->matches
); i
++) {
433 int s
= dmi
->matches
[i
].slot
;
437 && strstr(dmi_ident
[s
], dmi
->matches
[i
].substr
))
446 * dmi_is_end_of_table - check for end-of-table marker
447 * @dmi: pointer to the dmi_system_id structure to check
449 static bool dmi_is_end_of_table(const struct dmi_system_id
*dmi
)
451 return dmi
->matches
[0].slot
== DMI_NONE
;
455 * dmi_check_system - check system DMI data
456 * @list: array of dmi_system_id structures to match against
457 * All non-null elements of the list must match
458 * their slot's (field index's) data (i.e., each
459 * list string must be a substring of the specified
460 * DMI slot's string data) to be considered a
463 * Walk the blacklist table running matching functions until someone
464 * returns non zero or we hit the end. Callback function is called for
465 * each successful match. Returns the number of matches.
467 int dmi_check_system(const struct dmi_system_id
*list
)
470 const struct dmi_system_id
*d
;
472 for (d
= list
; !dmi_is_end_of_table(d
); d
++)
473 if (dmi_matches(d
)) {
475 if (d
->callback
&& d
->callback(d
))
481 EXPORT_SYMBOL(dmi_check_system
);
484 * dmi_first_match - find dmi_system_id structure matching system DMI data
485 * @list: array of dmi_system_id structures to match against
486 * All non-null elements of the list must match
487 * their slot's (field index's) data (i.e., each
488 * list string must be a substring of the specified
489 * DMI slot's string data) to be considered a
492 * Walk the blacklist table until the first match is found. Return the
493 * pointer to the matching entry or NULL if there's no match.
495 const struct dmi_system_id
*dmi_first_match(const struct dmi_system_id
*list
)
497 const struct dmi_system_id
*d
;
499 for (d
= list
; !dmi_is_end_of_table(d
); d
++)
505 EXPORT_SYMBOL(dmi_first_match
);
508 * dmi_get_system_info - return DMI data value
509 * @field: data index (see enum dmi_field)
511 * Returns one DMI data value, can be used to perform
512 * complex DMI data checks.
514 const char *dmi_get_system_info(int field
)
516 return dmi_ident
[field
];
518 EXPORT_SYMBOL(dmi_get_system_info
);
521 * dmi_name_in_serial - Check if string is in the DMI product serial information
522 * @str: string to check for
524 int dmi_name_in_serial(const char *str
)
526 int f
= DMI_PRODUCT_SERIAL
;
527 if (dmi_ident
[f
] && strstr(dmi_ident
[f
], str
))
533 * dmi_name_in_vendors - Check if string is anywhere in the DMI vendor information.
534 * @str: Case sensitive Name
536 int dmi_name_in_vendors(const char *str
)
538 static int fields
[] = { DMI_BIOS_VENDOR
, DMI_BIOS_VERSION
, DMI_SYS_VENDOR
,
539 DMI_PRODUCT_NAME
, DMI_PRODUCT_VERSION
, DMI_BOARD_VENDOR
,
540 DMI_BOARD_NAME
, DMI_BOARD_VERSION
, DMI_NONE
};
542 for (i
= 0; fields
[i
] != DMI_NONE
; i
++) {
544 if (dmi_ident
[f
] && strstr(dmi_ident
[f
], str
))
549 EXPORT_SYMBOL(dmi_name_in_vendors
);
552 * dmi_find_device - find onboard device by type/name
553 * @type: device type or %DMI_DEV_TYPE_ANY to match all device types
554 * @name: device name string or %NULL to match all
555 * @from: previous device found in search, or %NULL for new search.
557 * Iterates through the list of known onboard devices. If a device is
558 * found with a matching @vendor and @device, a pointer to its device
559 * structure is returned. Otherwise, %NULL is returned.
560 * A new search is initiated by passing %NULL as the @from argument.
561 * If @from is not %NULL, searches continue from next device.
563 const struct dmi_device
* dmi_find_device(int type
, const char *name
,
564 const struct dmi_device
*from
)
566 const struct list_head
*head
= from
? &from
->list
: &dmi_devices
;
569 for(d
= head
->next
; d
!= &dmi_devices
; d
= d
->next
) {
570 const struct dmi_device
*dev
=
571 list_entry(d
, struct dmi_device
, list
);
573 if (((type
== DMI_DEV_TYPE_ANY
) || (dev
->type
== type
)) &&
574 ((name
== NULL
) || (strcmp(dev
->name
, name
) == 0)))
580 EXPORT_SYMBOL(dmi_find_device
);
583 * dmi_get_date - parse a DMI date
584 * @field: data index (see enum dmi_field)
585 * @yearp: optional out parameter for the year
586 * @monthp: optional out parameter for the month
587 * @dayp: optional out parameter for the day
589 * The date field is assumed to be in the form resembling
590 * [mm[/dd]]/yy[yy] and the result is stored in the out
591 * parameters any or all of which can be omitted.
593 * If the field doesn't exist, all out parameters are set to zero
594 * and false is returned. Otherwise, true is returned with any
595 * invalid part of date set to zero.
597 * On return, year, month and day are guaranteed to be in the
598 * range of [0,9999], [0,12] and [0,31] respectively.
600 bool dmi_get_date(int field
, int *yearp
, int *monthp
, int *dayp
)
602 int year
= 0, month
= 0, day
= 0;
607 s
= dmi_get_system_info(field
);
613 * Determine year first. We assume the date string resembles
614 * mm/dd/yy[yy] but the original code extracted only the year
615 * from the end. Keep the behavior in the spirit of no
623 year
= simple_strtoul(y
, &e
, 10);
624 if (y
!= e
&& year
< 100) { /* 2-digit year */
626 if (year
< 1996) /* no dates < spec 1.0 */
629 if (year
> 9999) /* year should fit in %04d */
632 /* parse the mm and dd */
633 month
= simple_strtoul(s
, &e
, 10);
634 if (s
== e
|| *e
!= '/' || !month
|| month
> 12) {
640 day
= simple_strtoul(s
, &e
, 10);
641 if (s
== y
|| s
== e
|| *e
!= '/' || day
> 31)
652 EXPORT_SYMBOL(dmi_get_date
);
655 * dmi_walk - Walk the DMI table and get called back for every record
656 * @decode: Callback function
657 * @private_data: Private data to be passed to the callback function
659 * Returns -1 when the DMI table can't be reached, 0 on success.
661 int dmi_walk(void (*decode
)(const struct dmi_header
*, void *),
669 buf
= ioremap(dmi_base
, dmi_len
);
673 dmi_table(buf
, dmi_len
, dmi_num
, decode
, private_data
);
678 EXPORT_SYMBOL_GPL(dmi_walk
);
681 * dmi_match - compare a string to the dmi field (if exists)
682 * @f: DMI field identifier
683 * @str: string to compare the DMI field to
685 * Returns true if the requested field equals to the str (including NULL).
687 bool dmi_match(enum dmi_field f
, const char *str
)
689 const char *info
= dmi_get_system_info(f
);
691 if (info
== NULL
|| str
== NULL
)
694 return !strcmp(info
, str
);
696 EXPORT_SYMBOL_GPL(dmi_match
);