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>
6#include <linux/dmi.h>
7#include <linux/efi.h>
8#include <linux/bootmem.h>
9#include <linux/random.h>
10#include <asm/dmi.h>
11
12
13
14
15
16
17static const char dmi_empty_string[] = " ";
18
19static u16 __initdata dmi_ver;
20
21
22
23static int dmi_initialized;
24
25
26static char dmi_ids_string[128] __initdata;
27
28static const char * __init dmi_string_nosave(const struct dmi_header *dm, u8 s)
29{
30 const u8 *bp = ((u8 *) dm) + dm->length;
31
32 if (s) {
33 s--;
34 while (s > 0 && *bp) {
35 bp += strlen(bp) + 1;
36 s--;
37 }
38
39 if (*bp != 0) {
40 size_t len = strlen(bp)+1;
41 size_t cmp_len = len > 8 ? 8 : len;
42
43 if (!memcmp(bp, dmi_empty_string, cmp_len))
44 return dmi_empty_string;
45 return bp;
46 }
47 }
48
49 return "";
50}
51
52static const char * __init dmi_string(const struct dmi_header *dm, u8 s)
53{
54 const char *bp = dmi_string_nosave(dm, s);
55 char *str;
56 size_t len;
57
58 if (bp == dmi_empty_string)
59 return dmi_empty_string;
60
61 len = strlen(bp) + 1;
62 str = dmi_alloc(len);
63 if (str != NULL)
64 strcpy(str, bp);
65
66 return str;
67}
68
69
70
71
72
73static void dmi_table(u8 *buf, int len, int num,
74 void (*decode)(const struct dmi_header *, void *),
75 void *private_data)
76{
77 u8 *data = buf;
78 int i = 0;
79
80
81
82
83
84 while ((i < num) && (data - buf + sizeof(struct dmi_header)) <= len) {
85 const struct dmi_header *dm = (const struct dmi_header *)data;
86
87
88
89
90
91
92 data += dm->length;
93 while ((data - buf < len - 1) && (data[0] || data[1]))
94 data++;
95 if (data - buf < len - 1)
96 decode(dm, private_data);
97 data += 2;
98 i++;
99 }
100}
101
102static u32 dmi_base;
103static u16 dmi_len;
104static u16 dmi_num;
105
106static int __init dmi_walk_early(void (*decode)(const struct dmi_header *,
107 void *))
108{
109 u8 *buf;
110
111 buf = dmi_ioremap(dmi_base, dmi_len);
112 if (buf == NULL)
113 return -1;
114
115 dmi_table(buf, dmi_len, dmi_num, decode, NULL);
116
117 add_device_randomness(buf, dmi_len);
118
119 dmi_iounmap(buf, dmi_len);
120 return 0;
121}
122
123static int __init dmi_checksum(const u8 *buf, u8 len)
124{
125 u8 sum = 0;
126 int a;
127
128 for (a = 0; a < len; a++)
129 sum += buf[a];
130
131 return sum == 0;
132}
133
134static const char *dmi_ident[DMI_STRING_MAX];
135static LIST_HEAD(dmi_devices);
136int dmi_available;
137
138
139
140
141static void __init dmi_save_ident(const struct dmi_header *dm, int slot,
142 int string)
143{
144 const char *d = (const char *) dm;
145 const char *p;
146
147 if (dmi_ident[slot])
148 return;
149
150 p = dmi_string(dm, d[string]);
151 if (p == NULL)
152 return;
153
154 dmi_ident[slot] = p;
155}
156
157static void __init dmi_save_uuid(const struct dmi_header *dm, int slot,
158 int index)
159{
160 const u8 *d = (u8 *) dm + index;
161 char *s;
162 int is_ff = 1, is_00 = 1, i;
163
164 if (dmi_ident[slot])
165 return;
166
167 for (i = 0; i < 16 && (is_ff || is_00); i++) {
168 if (d[i] != 0x00)
169 is_00 = 0;
170 if (d[i] != 0xFF)
171 is_ff = 0;
172 }
173
174 if (is_ff || is_00)
175 return;
176
177 s = dmi_alloc(16*2+4+1);
178 if (!s)
179 return;
180
181
182
183
184
185
186 if (dmi_ver >= 0x0206)
187 sprintf(s, "%pUL", d);
188 else
189 sprintf(s, "%pUB", d);
190
191 dmi_ident[slot] = s;
192}
193
194static void __init dmi_save_type(const struct dmi_header *dm, int slot,
195 int index)
196{
197 const u8 *d = (u8 *) dm + index;
198 char *s;
199
200 if (dmi_ident[slot])
201 return;
202
203 s = dmi_alloc(4);
204 if (!s)
205 return;
206
207 sprintf(s, "%u", *d & 0x7F);
208 dmi_ident[slot] = s;
209}
210
211static void __init dmi_save_one_device(int type, const char *name)
212{
213 struct dmi_device *dev;
214
215
216 if (dmi_find_device(type, name, NULL))
217 return;
218
219 dev = dmi_alloc(sizeof(*dev) + strlen(name) + 1);
220 if (!dev)
221 return;
222
223 dev->type = type;
224 strcpy((char *)(dev + 1), name);
225 dev->name = (char *)(dev + 1);
226 dev->device_data = NULL;
227 list_add(&dev->list, &dmi_devices);
228}
229
230static void __init dmi_save_devices(const struct dmi_header *dm)
231{
232 int i, count = (dm->length - sizeof(struct dmi_header)) / 2;
233
234 for (i = 0; i < count; i++) {
235 const char *d = (char *)(dm + 1) + (i * 2);
236
237
238 if ((*d & 0x80) == 0)
239 continue;
240
241 dmi_save_one_device(*d & 0x7f, dmi_string_nosave(dm, *(d + 1)));
242 }
243}
244
245static void __init dmi_save_oem_strings_devices(const struct dmi_header *dm)
246{
247 int i, count = *(u8 *)(dm + 1);
248 struct dmi_device *dev;
249
250 for (i = 1; i <= count; i++) {
251 const char *devname = dmi_string(dm, i);
252
253 if (devname == dmi_empty_string)
254 continue;
255
256 dev = dmi_alloc(sizeof(*dev));
257 if (!dev)
258 break;
259
260 dev->type = DMI_DEV_TYPE_OEM_STRING;
261 dev->name = devname;
262 dev->device_data = NULL;
263
264 list_add(&dev->list, &dmi_devices);
265 }
266}
267
268static void __init dmi_save_ipmi_device(const struct dmi_header *dm)
269{
270 struct dmi_device *dev;
271 void *data;
272
273 data = dmi_alloc(dm->length);
274 if (data == NULL)
275 return;
276
277 memcpy(data, dm, dm->length);
278
279 dev = dmi_alloc(sizeof(*dev));
280 if (!dev)
281 return;
282
283 dev->type = DMI_DEV_TYPE_IPMI;
284 dev->name = "IPMI controller";
285 dev->device_data = data;
286
287 list_add_tail(&dev->list, &dmi_devices);
288}
289
290static void __init dmi_save_dev_onboard(int instance, int segment, int bus,
291 int devfn, const char *name)
292{
293 struct dmi_dev_onboard *onboard_dev;
294
295 onboard_dev = dmi_alloc(sizeof(*onboard_dev) + strlen(name) + 1);
296 if (!onboard_dev)
297 return;
298
299 onboard_dev->instance = instance;
300 onboard_dev->segment = segment;
301 onboard_dev->bus = bus;
302 onboard_dev->devfn = devfn;
303
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;
308
309 list_add(&onboard_dev->dev.list, &dmi_devices);
310}
311
312static void __init dmi_save_extended_devices(const struct dmi_header *dm)
313{
314 const u8 *d = (u8 *) dm + 5;
315
316
317 if ((*d & 0x80) == 0)
318 return;
319
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)));
323}
324
325
326
327
328
329
330static void __init dmi_decode(const struct dmi_header *dm, void *dummy)
331{
332 switch (dm->type) {
333 case 0:
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);
337 break;
338 case 1:
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);
344 break;
345 case 2:
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);
351 break;
352 case 3:
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);
358 break;
359 case 10:
360 dmi_save_devices(dm);
361 break;
362 case 11:
363 dmi_save_oem_strings_devices(dm);
364 break;
365 case 38:
366 dmi_save_ipmi_device(dm);
367 break;
368 case 41:
369 dmi_save_extended_devices(dm);
370 }
371}
372
373static int __init print_filtered(char *buf, size_t len, const char *info)
374{
375 int c = 0;
376 const char *p;
377
378 if (!info)
379 return c;
380
381 for (p = info; *p; p++)
382 if (isprint(*p))
383 c += scnprintf(buf + c, len - c, "%c", *p);
384 else
385 c += scnprintf(buf + c, len - c, "\\x%02x", *p & 0xff);
386 return c;
387}
388
389static void __init dmi_format_ids(char *buf, size_t len)
390{
391 int c = 0;
392 const char *board;
393
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));
399
400 board = dmi_get_system_info(DMI_BOARD_NAME);
401 if (board) {
402 c += scnprintf(buf + c, len - c, "/");
403 c += print_filtered(buf + c, len - c, board);
404 }
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));
411}
412
413
414
415
416
417
418
419
420static int __init dmi_present(const u8 *buf)
421{
422 int smbios_ver;
423
424 if (memcmp(buf, "_SM_", 4) == 0 &&
425 buf[5] < 32 && dmi_checksum(buf, buf[5])) {
426 smbios_ver = (buf[6] << 8) + buf[7];
427
428
429 switch (smbios_ver) {
430 case 0x021F:
431 case 0x0221:
432 pr_debug("SMBIOS version fixup(2.%d->2.%d)\n",
433 smbios_ver & 0xFF, 3);
434 smbios_ver = 0x0203;
435 break;
436 case 0x0233:
437 pr_debug("SMBIOS version fixup(2.%d->2.%d)\n", 51, 6);
438 smbios_ver = 0x0206;
439 break;
440 }
441 } else {
442 smbios_ver = 0;
443 }
444
445 buf += 16;
446
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];
452
453 if (dmi_walk_early(dmi_decode) == 0) {
454 if (smbios_ver) {
455 dmi_ver = smbios_ver;
456 pr_info("SMBIOS %d.%d present.\n",
457 dmi_ver >> 8, dmi_ver & 0xFF);
458 } else {
459 dmi_ver = (buf[14] & 0xF0) << 4 |
460 (buf[14] & 0x0F);
461 pr_info("Legacy DMI %d.%d present.\n",
462 dmi_ver >> 8, dmi_ver & 0xFF);
463 }
464 dmi_format_ids(dmi_ids_string, sizeof(dmi_ids_string));
465 printk(KERN_DEBUG "DMI: %s\n", dmi_ids_string);
466 return 0;
467 }
468 }
469
470 return 1;
471}
472
473void __init dmi_scan_machine(void)
474{
475 char __iomem *p, *q;
476 char buf[32];
477
478 if (efi_enabled(EFI_CONFIG_TABLES)) {
479 if (efi.smbios == EFI_INVALID_TABLE_ADDR)
480 goto error;
481
482
483
484
485
486 p = dmi_ioremap(efi.smbios, 32);
487 if (p == NULL)
488 goto error;
489 memcpy_fromio(buf, p, 32);
490 dmi_iounmap(p, 32);
491
492 if (!dmi_present(buf)) {
493 dmi_available = 1;
494 goto out;
495 }
496 } else {
497 p = dmi_ioremap(0xF0000, 0x10000);
498 if (p == NULL)
499 goto error;
500
501
502
503
504
505
506
507
508 memset(buf, 0, 16);
509 for (q = p; q < p + 0x10000; q += 16) {
510 memcpy_fromio(buf + 16, q, 16);
511 if (!dmi_present(buf)) {
512 dmi_available = 1;
513 dmi_iounmap(p, 0x10000);
514 goto out;
515 }
516 memcpy(buf, buf + 16, 16);
517 }
518 dmi_iounmap(p, 0x10000);
519 }
520 error:
521 pr_info("DMI not present or invalid.\n");
522 out:
523 dmi_initialized = 1;
524}
525
526
527
528
529
530
531
532
533
534void __init dmi_set_dump_stack_arch_desc(void)
535{
536 dump_stack_set_arch_desc("%s", dmi_ids_string);
537}
538
539
540
541
542
543static bool dmi_matches(const struct dmi_system_id *dmi)
544{
545 int i;
546
547 WARN(!dmi_initialized, KERN_ERR "dmi check: not initialized yet.\n");
548
549 for (i = 0; i < ARRAY_SIZE(dmi->matches); i++) {
550 int s = dmi->matches[i].slot;
551 if (s == DMI_NONE)
552 break;
553 if (dmi_ident[s]) {
554 if (!dmi->matches[i].exact_match &&
555 strstr(dmi_ident[s], dmi->matches[i].substr))
556 continue;
557 else if (dmi->matches[i].exact_match &&
558 !strcmp(dmi_ident[s], dmi->matches[i].substr))
559 continue;
560 }
561
562
563 return false;
564 }
565 return true;
566}
567
568
569
570
571
572static bool dmi_is_end_of_table(const struct dmi_system_id *dmi)
573{
574 return dmi->matches[0].slot == DMI_NONE;
575}
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590int dmi_check_system(const struct dmi_system_id *list)
591{
592 int count = 0;
593 const struct dmi_system_id *d;
594
595 for (d = list; !dmi_is_end_of_table(d); d++)
596 if (dmi_matches(d)) {
597 count++;
598 if (d->callback && d->callback(d))
599 break;
600 }
601
602 return count;
603}
604EXPORT_SYMBOL(dmi_check_system);
605
606
607
608
609
610
611
612
613
614
615
616
617
618const struct dmi_system_id *dmi_first_match(const struct dmi_system_id *list)
619{
620 const struct dmi_system_id *d;
621
622 for (d = list; !dmi_is_end_of_table(d); d++)
623 if (dmi_matches(d))
624 return d;
625
626 return NULL;
627}
628EXPORT_SYMBOL(dmi_first_match);
629
630
631
632
633
634
635
636
637const char *dmi_get_system_info(int field)
638{
639 return dmi_ident[field];
640}
641EXPORT_SYMBOL(dmi_get_system_info);
642
643
644
645
646
647int dmi_name_in_serial(const char *str)
648{
649 int f = DMI_PRODUCT_SERIAL;
650 if (dmi_ident[f] && strstr(dmi_ident[f], str))
651 return 1;
652 return 0;
653}
654
655
656
657
658
659int dmi_name_in_vendors(const char *str)
660{
661 static int fields[] = { DMI_SYS_VENDOR, DMI_BOARD_VENDOR, DMI_NONE };
662 int i;
663 for (i = 0; fields[i] != DMI_NONE; i++) {
664 int f = fields[i];
665 if (dmi_ident[f] && strstr(dmi_ident[f], str))
666 return 1;
667 }
668 return 0;
669}
670EXPORT_SYMBOL(dmi_name_in_vendors);
671
672
673
674
675
676
677
678
679
680
681
682
683
684const struct dmi_device *dmi_find_device(int type, const char *name,
685 const struct dmi_device *from)
686{
687 const struct list_head *head = from ? &from->list : &dmi_devices;
688 struct list_head *d;
689
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);
693
694 if (((type == DMI_DEV_TYPE_ANY) || (dev->type == type)) &&
695 ((name == NULL) || (strcmp(dev->name, name) == 0)))
696 return dev;
697 }
698
699 return NULL;
700}
701EXPORT_SYMBOL(dmi_find_device);
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721bool dmi_get_date(int field, int *yearp, int *monthp, int *dayp)
722{
723 int year = 0, month = 0, day = 0;
724 bool exists;
725 const char *s, *y;
726 char *e;
727
728 s = dmi_get_system_info(field);
729 exists = s;
730 if (!exists)
731 goto out;
732
733
734
735
736
737
738
739 y = strrchr(s, '/');
740 if (!y)
741 goto out;
742
743 y++;
744 year = simple_strtoul(y, &e, 10);
745 if (y != e && year < 100) {
746 year += 1900;
747 if (year < 1996)
748 year += 100;
749 }
750 if (year > 9999)
751 year = 0;
752
753
754 month = simple_strtoul(s, &e, 10);
755 if (s == e || *e != '/' || !month || month > 12) {
756 month = 0;
757 goto out;
758 }
759
760 s = e + 1;
761 day = simple_strtoul(s, &e, 10);
762 if (s == y || s == e || *e != '/' || day > 31)
763 day = 0;
764out:
765 if (yearp)
766 *yearp = year;
767 if (monthp)
768 *monthp = month;
769 if (dayp)
770 *dayp = day;
771 return exists;
772}
773EXPORT_SYMBOL(dmi_get_date);
774
775
776
777
778
779
780
781
782int dmi_walk(void (*decode)(const struct dmi_header *, void *),
783 void *private_data)
784{
785 u8 *buf;
786
787 if (!dmi_available)
788 return -1;
789
790 buf = ioremap(dmi_base, dmi_len);
791 if (buf == NULL)
792 return -1;
793
794 dmi_table(buf, dmi_len, dmi_num, decode, private_data);
795
796 iounmap(buf);
797 return 0;
798}
799EXPORT_SYMBOL_GPL(dmi_walk);
800
801
802
803
804
805
806
807
808bool dmi_match(enum dmi_field f, const char *str)
809{
810 const char *info = dmi_get_system_info(f);
811
812 if (info == NULL || str == NULL)
813 return info == str;
814
815 return !strcmp(info, str);
816}
817EXPORT_SYMBOL_GPL(dmi_match);
818