drivers/char/Kconfig: don't mess it up for everyone else
[linux-2.6.32.60-moxart.git] / drivers / firmware / dmi_scan.c
blob10a4246c7a6a0fd4b8af3f233c9398b395f47048
1 #include <linux/types.h>
2 #include <linux/string.h>
3 #include <linux/init.h>
4 #include <linux/module.h>
5 #include <linux/dmi.h>
6 #include <linux/efi.h>
7 #include <linux/bootmem.h>
8 #include <linux/slab.h>
9 #include <linux/random.h>
10 #include <asm/dmi.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;
28 if (s) {
29 s--;
30 while (s > 0 && *bp) {
31 bp += strlen(bp) + 1;
32 s--;
35 if (*bp != 0) {
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;
41 return bp;
45 return "";
48 static char * __init dmi_string(const struct dmi_header *dm, u8 s)
50 const char *bp = dmi_string_nosave(dm, s);
51 char *str;
52 size_t len;
54 if (bp == dmi_empty_string)
55 return dmi_empty_string;
57 len = strlen(bp) + 1;
58 str = dmi_alloc(len);
59 if (str != NULL)
60 strcpy(str, bp);
61 else
62 printk(KERN_ERR "dmi_string: cannot allocate %Zu bytes.\n", len);
64 return str;
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 *),
73 void *private_data)
75 u8 *data = buf;
76 int i = 0;
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
90 data += dm->length;
91 while ((data - buf < len - 1) && (data[0] || data[1]))
92 data++;
93 if (data - buf < len - 1)
94 decode(dm, private_data);
95 data += 2;
96 i++;
100 static u32 dmi_base;
101 static u16 dmi_len;
102 static u16 dmi_num;
104 static int __init dmi_walk_early(void (*decode)(const struct dmi_header *,
105 void *))
107 u8 *buf;
109 buf = dmi_ioremap(dmi_base, dmi_len);
110 if (buf == NULL)
111 return -1;
113 dmi_table(buf, dmi_len, dmi_num, decode, NULL);
115 add_device_randomness(buf, dmi_len);
117 dmi_iounmap(buf, dmi_len);
118 return 0;
121 static int __init dmi_checksum(const u8 *buf)
123 u8 sum = 0;
124 int a;
126 for (a = 0; a < 15; a++)
127 sum += buf[a];
129 return sum == 0;
132 static char *dmi_ident[DMI_STRING_MAX];
133 static LIST_HEAD(dmi_devices);
134 int dmi_available;
137 * Save a DMI string
139 static void __init dmi_save_ident(const struct dmi_header *dm, int slot, int string)
141 const char *d = (const char*) dm;
142 char *p;
144 if (dmi_ident[slot])
145 return;
147 p = dmi_string(dm, d[string]);
148 if (p == NULL)
149 return;
151 dmi_ident[slot] = p;
154 static void __init dmi_save_uuid(const struct dmi_header *dm, int slot, int index)
156 const u8 *d = (u8*) dm + index;
157 char *s;
158 int is_ff = 1, is_00 = 1, i;
160 if (dmi_ident[slot])
161 return;
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;
168 if (is_ff || is_00)
169 return;
171 s = dmi_alloc(16*2+4+1);
172 if (!s)
173 return;
175 sprintf(s,
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]);
180 dmi_ident[slot] = s;
183 static void __init dmi_save_type(const struct dmi_header *dm, int slot, int index)
185 const u8 *d = (u8*) dm + index;
186 char *s;
188 if (dmi_ident[slot])
189 return;
191 s = dmi_alloc(4);
192 if (!s)
193 return;
195 sprintf(s, "%u", *d & 0x7F);
196 dmi_ident[slot] = s;
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))
205 return;
207 dev = dmi_alloc(sizeof(*dev) + strlen(name) + 1);
208 if (!dev) {
209 printk(KERN_ERR "dmi_save_one_device: out of memory.\n");
210 return;
213 dev->type = type;
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)
229 continue;
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)
244 continue;
246 dev = dmi_alloc(sizeof(*dev));
247 if (!dev) {
248 printk(KERN_ERR
249 "dmi_save_oem_strings_devices: out of memory.\n");
250 break;
253 dev->type = DMI_DEV_TYPE_OEM_STRING;
254 dev->name = devname;
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;
264 void * data;
266 data = dmi_alloc(dm->length);
267 if (data == NULL) {
268 printk(KERN_ERR "dmi_save_ipmi_device: out of memory.\n");
269 return;
272 memcpy(data, dm, dm->length);
274 dev = dmi_alloc(sizeof(*dev));
275 if (!dev) {
276 printk(KERN_ERR "dmi_save_ipmi_device: out of memory.\n");
277 return;
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)
293 return;
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
301 * out of here.
303 static void __init dmi_decode(const struct dmi_header *dm, void *dummy)
305 switch(dm->type) {
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);
310 break;
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);
317 break;
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);
324 break;
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);
331 break;
332 case 10: /* Onboard Devices Information */
333 dmi_save_devices(dm);
334 break;
335 case 11: /* OEM Strings */
336 dmi_save_oem_strings_devices(dm);
337 break;
338 case 38: /* IPMI Device Information */
339 dmi_save_ipmi_device(dm);
340 break;
341 case 41: /* Onboard Devices Extended Information */
342 dmi_save_extended_devices(dm);
346 static int __init dmi_present(const char __iomem *p)
348 u8 buf[15];
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.
361 if (buf[14] != 0)
362 printk(KERN_INFO "DMI %d.%d present.\n",
363 buf[14] >> 4, buf[14] & 0xF);
364 else
365 printk(KERN_INFO "DMI present.\n");
366 if (dmi_walk_early(dmi_decode) == 0)
367 return 0;
369 return 1;
372 void __init dmi_scan_machine(void)
374 char __iomem *p, *q;
375 int rc;
377 if (efi_enabled) {
378 if (efi.smbios == EFI_INVALID_TABLE_ADDR)
379 goto error;
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);
386 if (p == NULL)
387 goto error;
389 rc = dmi_present(p + 0x10); /* offset of _DMI_ string */
390 dmi_iounmap(p, 32);
391 if (!rc) {
392 dmi_available = 1;
393 goto out;
396 else {
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);
403 if (p == NULL)
404 goto error;
406 for (q = p; q < p + 0x10000; q += 16) {
407 rc = dmi_present(q);
408 if (!rc) {
409 dmi_available = 1;
410 dmi_iounmap(p, 0x10000);
411 goto out;
414 dmi_iounmap(p, 0x10000);
416 error:
417 printk(KERN_INFO "DMI not present or invalid.\n");
418 out:
419 dmi_initialized = 1;
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)
428 int i;
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;
434 if (s == DMI_NONE)
435 break;
436 if (dmi_ident[s]
437 && strstr(dmi_ident[s], dmi->matches[i].substr))
438 continue;
439 /* No match */
440 return false;
442 return true;
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
461 * successful match.
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)
469 int count = 0;
470 const struct dmi_system_id *d;
472 for (d = list; !dmi_is_end_of_table(d); d++)
473 if (dmi_matches(d)) {
474 count++;
475 if (d->callback && d->callback(d))
476 break;
479 return count;
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
490 * successful match.
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++)
500 if (dmi_matches(d))
501 return d;
503 return NULL;
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))
528 return 1;
529 return 0;
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 };
541 int i;
542 for (i = 0; fields[i] != DMI_NONE; i++) {
543 int f = fields[i];
544 if (dmi_ident[f] && strstr(dmi_ident[f], str))
545 return 1;
547 return 0;
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;
567 struct list_head *d;
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)))
575 return dev;
578 return NULL;
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;
603 bool exists;
604 const char *s, *y;
605 char *e;
607 s = dmi_get_system_info(field);
608 exists = s;
609 if (!exists)
610 goto out;
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
616 * surprises.
618 y = strrchr(s, '/');
619 if (!y)
620 goto out;
622 y++;
623 year = simple_strtoul(y, &e, 10);
624 if (y != e && year < 100) { /* 2-digit year */
625 year += 1900;
626 if (year < 1996) /* no dates < spec 1.0 */
627 year += 100;
629 if (year > 9999) /* year should fit in %04d */
630 year = 0;
632 /* parse the mm and dd */
633 month = simple_strtoul(s, &e, 10);
634 if (s == e || *e != '/' || !month || month > 12) {
635 month = 0;
636 goto out;
639 s = e + 1;
640 day = simple_strtoul(s, &e, 10);
641 if (s == y || s == e || *e != '/' || day > 31)
642 day = 0;
643 out:
644 if (yearp)
645 *yearp = year;
646 if (monthp)
647 *monthp = month;
648 if (dayp)
649 *dayp = day;
650 return exists;
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 *),
662 void *private_data)
664 u8 *buf;
666 if (!dmi_available)
667 return -1;
669 buf = ioremap(dmi_base, dmi_len);
670 if (buf == NULL)
671 return -1;
673 dmi_table(buf, dmi_len, dmi_num, decode, private_data);
675 iounmap(buf);
676 return 0;
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)
692 return info == str;
694 return !strcmp(info, str);
696 EXPORT_SYMBOL_GPL(dmi_match);