firmware: dmi_scan: Drop dmi_initialized
[linux-2.6-microblaze.git] / drivers / firmware / dmi_scan.c
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 #include <asm/unaligned.h>
12
13 struct kobject *dmi_kobj;
14 EXPORT_SYMBOL_GPL(dmi_kobj);
15
16 /*
17  * DMI stands for "Desktop Management Interface".  It is part
18  * of and an antecedent to, SMBIOS, which stands for System
19  * Management BIOS.  See further: http://www.dmtf.org/standards
20  */
21 static const char dmi_empty_string[] = "        ";
22
23 static u32 dmi_ver __initdata;
24 static u32 dmi_len;
25 static u16 dmi_num;
26 static u8 smbios_entry_point[32];
27 static int smbios_entry_point_size;
28
29 /* DMI system identification string used during boot */
30 static char dmi_ids_string[128] __initdata;
31
32 static struct dmi_memdev_info {
33         const char *device;
34         const char *bank;
35         u16 handle;
36 } *dmi_memdev;
37 static int dmi_memdev_nr;
38
39 static const char * __init dmi_string_nosave(const struct dmi_header *dm, u8 s)
40 {
41         const u8 *bp = ((u8 *) dm) + dm->length;
42
43         if (s) {
44                 s--;
45                 while (s > 0 && *bp) {
46                         bp += strlen(bp) + 1;
47                         s--;
48                 }
49
50                 if (*bp != 0) {
51                         size_t len = strlen(bp)+1;
52                         size_t cmp_len = len > 8 ? 8 : len;
53
54                         if (!memcmp(bp, dmi_empty_string, cmp_len))
55                                 return dmi_empty_string;
56                         return bp;
57                 }
58         }
59
60         return "";
61 }
62
63 static const char * __init dmi_string(const struct dmi_header *dm, u8 s)
64 {
65         const char *bp = dmi_string_nosave(dm, s);
66         char *str;
67         size_t len;
68
69         if (bp == dmi_empty_string)
70                 return dmi_empty_string;
71
72         len = strlen(bp) + 1;
73         str = dmi_alloc(len);
74         if (str != NULL)
75                 strcpy(str, bp);
76
77         return str;
78 }
79
80 /*
81  *      We have to be cautious here. We have seen BIOSes with DMI pointers
82  *      pointing to completely the wrong place for example
83  */
84 static void dmi_decode_table(u8 *buf,
85                              void (*decode)(const struct dmi_header *, void *),
86                              void *private_data)
87 {
88         u8 *data = buf;
89         int i = 0;
90
91         /*
92          * Stop when we have seen all the items the table claimed to have
93          * (SMBIOS < 3.0 only) OR we reach an end-of-table marker (SMBIOS
94          * >= 3.0 only) OR we run off the end of the table (should never
95          * happen but sometimes does on bogus implementations.)
96          */
97         while ((!dmi_num || i < dmi_num) &&
98                (data - buf + sizeof(struct dmi_header)) <= dmi_len) {
99                 const struct dmi_header *dm = (const struct dmi_header *)data;
100
101                 /*
102                  *  We want to know the total length (formatted area and
103                  *  strings) before decoding to make sure we won't run off the
104                  *  table in dmi_decode or dmi_string
105                  */
106                 data += dm->length;
107                 while ((data - buf < dmi_len - 1) && (data[0] || data[1]))
108                         data++;
109                 if (data - buf < dmi_len - 1)
110                         decode(dm, private_data);
111
112                 data += 2;
113                 i++;
114
115                 /*
116                  * 7.45 End-of-Table (Type 127) [SMBIOS reference spec v3.0.0]
117                  * For tables behind a 64-bit entry point, we have no item
118                  * count and no exact table length, so stop on end-of-table
119                  * marker. For tables behind a 32-bit entry point, we have
120                  * seen OEM structures behind the end-of-table marker on
121                  * some systems, so don't trust it.
122                  */
123                 if (!dmi_num && dm->type == DMI_ENTRY_END_OF_TABLE)
124                         break;
125         }
126
127         /* Trim DMI table length if needed */
128         if (dmi_len > data - buf)
129                 dmi_len = data - buf;
130 }
131
132 static phys_addr_t dmi_base;
133
134 static int __init dmi_walk_early(void (*decode)(const struct dmi_header *,
135                 void *))
136 {
137         u8 *buf;
138         u32 orig_dmi_len = dmi_len;
139
140         buf = dmi_early_remap(dmi_base, orig_dmi_len);
141         if (buf == NULL)
142                 return -ENOMEM;
143
144         dmi_decode_table(buf, decode, NULL);
145
146         add_device_randomness(buf, dmi_len);
147
148         dmi_early_unmap(buf, orig_dmi_len);
149         return 0;
150 }
151
152 static int __init dmi_checksum(const u8 *buf, u8 len)
153 {
154         u8 sum = 0;
155         int a;
156
157         for (a = 0; a < len; a++)
158                 sum += buf[a];
159
160         return sum == 0;
161 }
162
163 static const char *dmi_ident[DMI_STRING_MAX];
164 static LIST_HEAD(dmi_devices);
165 int dmi_available;
166
167 /*
168  *      Save a DMI string
169  */
170 static void __init dmi_save_ident(const struct dmi_header *dm, int slot,
171                 int string)
172 {
173         const char *d = (const char *) dm;
174         const char *p;
175
176         if (dmi_ident[slot] || dm->length <= string)
177                 return;
178
179         p = dmi_string(dm, d[string]);
180         if (p == NULL)
181                 return;
182
183         dmi_ident[slot] = p;
184 }
185
186 static void __init dmi_save_uuid(const struct dmi_header *dm, int slot,
187                 int index)
188 {
189         const u8 *d;
190         char *s;
191         int is_ff = 1, is_00 = 1, i;
192
193         if (dmi_ident[slot] || dm->length <= index + 16)
194                 return;
195
196         d = (u8 *) dm + index;
197         for (i = 0; i < 16 && (is_ff || is_00); i++) {
198                 if (d[i] != 0x00)
199                         is_00 = 0;
200                 if (d[i] != 0xFF)
201                         is_ff = 0;
202         }
203
204         if (is_ff || is_00)
205                 return;
206
207         s = dmi_alloc(16*2+4+1);
208         if (!s)
209                 return;
210
211         /*
212          * As of version 2.6 of the SMBIOS specification, the first 3 fields of
213          * the UUID are supposed to be little-endian encoded.  The specification
214          * says that this is the defacto standard.
215          */
216         if (dmi_ver >= 0x020600)
217                 sprintf(s, "%pUL", d);
218         else
219                 sprintf(s, "%pUB", d);
220
221         dmi_ident[slot] = s;
222 }
223
224 static void __init dmi_save_type(const struct dmi_header *dm, int slot,
225                 int index)
226 {
227         const u8 *d;
228         char *s;
229
230         if (dmi_ident[slot] || dm->length <= index)
231                 return;
232
233         s = dmi_alloc(4);
234         if (!s)
235                 return;
236
237         d = (u8 *) dm + index;
238         sprintf(s, "%u", *d & 0x7F);
239         dmi_ident[slot] = s;
240 }
241
242 static void __init dmi_save_one_device(int type, const char *name)
243 {
244         struct dmi_device *dev;
245
246         /* No duplicate device */
247         if (dmi_find_device(type, name, NULL))
248                 return;
249
250         dev = dmi_alloc(sizeof(*dev) + strlen(name) + 1);
251         if (!dev)
252                 return;
253
254         dev->type = type;
255         strcpy((char *)(dev + 1), name);
256         dev->name = (char *)(dev + 1);
257         dev->device_data = NULL;
258         list_add(&dev->list, &dmi_devices);
259 }
260
261 static void __init dmi_save_devices(const struct dmi_header *dm)
262 {
263         int i, count = (dm->length - sizeof(struct dmi_header)) / 2;
264
265         for (i = 0; i < count; i++) {
266                 const char *d = (char *)(dm + 1) + (i * 2);
267
268                 /* Skip disabled device */
269                 if ((*d & 0x80) == 0)
270                         continue;
271
272                 dmi_save_one_device(*d & 0x7f, dmi_string_nosave(dm, *(d + 1)));
273         }
274 }
275
276 static void __init dmi_save_oem_strings_devices(const struct dmi_header *dm)
277 {
278         int i, count;
279         struct dmi_device *dev;
280
281         if (dm->length < 0x05)
282                 return;
283
284         count = *(u8 *)(dm + 1);
285         for (i = 1; i <= count; i++) {
286                 const char *devname = dmi_string(dm, i);
287
288                 if (devname == dmi_empty_string)
289                         continue;
290
291                 dev = dmi_alloc(sizeof(*dev));
292                 if (!dev)
293                         break;
294
295                 dev->type = DMI_DEV_TYPE_OEM_STRING;
296                 dev->name = devname;
297                 dev->device_data = NULL;
298
299                 list_add(&dev->list, &dmi_devices);
300         }
301 }
302
303 static void __init dmi_save_ipmi_device(const struct dmi_header *dm)
304 {
305         struct dmi_device *dev;
306         void *data;
307
308         data = dmi_alloc(dm->length);
309         if (data == NULL)
310                 return;
311
312         memcpy(data, dm, dm->length);
313
314         dev = dmi_alloc(sizeof(*dev));
315         if (!dev)
316                 return;
317
318         dev->type = DMI_DEV_TYPE_IPMI;
319         dev->name = "IPMI controller";
320         dev->device_data = data;
321
322         list_add_tail(&dev->list, &dmi_devices);
323 }
324
325 static void __init dmi_save_dev_pciaddr(int instance, int segment, int bus,
326                                         int devfn, const char *name, int type)
327 {
328         struct dmi_dev_onboard *dev;
329
330         /* Ignore invalid values */
331         if (type == DMI_DEV_TYPE_DEV_SLOT &&
332             segment == 0xFFFF && bus == 0xFF && devfn == 0xFF)
333                 return;
334
335         dev = dmi_alloc(sizeof(*dev) + strlen(name) + 1);
336         if (!dev)
337                 return;
338
339         dev->instance = instance;
340         dev->segment = segment;
341         dev->bus = bus;
342         dev->devfn = devfn;
343
344         strcpy((char *)&dev[1], name);
345         dev->dev.type = type;
346         dev->dev.name = (char *)&dev[1];
347         dev->dev.device_data = dev;
348
349         list_add(&dev->dev.list, &dmi_devices);
350 }
351
352 static void __init dmi_save_extended_devices(const struct dmi_header *dm)
353 {
354         const char *name;
355         const u8 *d = (u8 *)dm;
356
357         if (dm->length < 0x0B)
358                 return;
359
360         /* Skip disabled device */
361         if ((d[0x5] & 0x80) == 0)
362                 return;
363
364         name = dmi_string_nosave(dm, d[0x4]);
365         dmi_save_dev_pciaddr(d[0x6], *(u16 *)(d + 0x7), d[0x9], d[0xA], name,
366                              DMI_DEV_TYPE_DEV_ONBOARD);
367         dmi_save_one_device(d[0x5] & 0x7f, name);
368 }
369
370 static void __init dmi_save_system_slot(const struct dmi_header *dm)
371 {
372         const u8 *d = (u8 *)dm;
373
374         /* Need SMBIOS 2.6+ structure */
375         if (dm->length < 0x11)
376                 return;
377         dmi_save_dev_pciaddr(*(u16 *)(d + 0x9), *(u16 *)(d + 0xD), d[0xF],
378                              d[0x10], dmi_string_nosave(dm, d[0x4]),
379                              DMI_DEV_TYPE_DEV_SLOT);
380 }
381
382 static void __init count_mem_devices(const struct dmi_header *dm, void *v)
383 {
384         if (dm->type != DMI_ENTRY_MEM_DEVICE)
385                 return;
386         dmi_memdev_nr++;
387 }
388
389 static void __init save_mem_devices(const struct dmi_header *dm, void *v)
390 {
391         const char *d = (const char *)dm;
392         static int nr;
393
394         if (dm->type != DMI_ENTRY_MEM_DEVICE || dm->length < 0x12)
395                 return;
396         if (nr >= dmi_memdev_nr) {
397                 pr_warn(FW_BUG "Too many DIMM entries in SMBIOS table\n");
398                 return;
399         }
400         dmi_memdev[nr].handle = get_unaligned(&dm->handle);
401         dmi_memdev[nr].device = dmi_string(dm, d[0x10]);
402         dmi_memdev[nr].bank = dmi_string(dm, d[0x11]);
403         nr++;
404 }
405
406 void __init dmi_memdev_walk(void)
407 {
408         if (!dmi_available)
409                 return;
410
411         if (dmi_walk_early(count_mem_devices) == 0 && dmi_memdev_nr) {
412                 dmi_memdev = dmi_alloc(sizeof(*dmi_memdev) * dmi_memdev_nr);
413                 if (dmi_memdev)
414                         dmi_walk_early(save_mem_devices);
415         }
416 }
417
418 /*
419  *      Process a DMI table entry. Right now all we care about are the BIOS
420  *      and machine entries. For 2.5 we should pull the smbus controller info
421  *      out of here.
422  */
423 static void __init dmi_decode(const struct dmi_header *dm, void *dummy)
424 {
425         switch (dm->type) {
426         case 0:         /* BIOS Information */
427                 dmi_save_ident(dm, DMI_BIOS_VENDOR, 4);
428                 dmi_save_ident(dm, DMI_BIOS_VERSION, 5);
429                 dmi_save_ident(dm, DMI_BIOS_DATE, 8);
430                 break;
431         case 1:         /* System Information */
432                 dmi_save_ident(dm, DMI_SYS_VENDOR, 4);
433                 dmi_save_ident(dm, DMI_PRODUCT_NAME, 5);
434                 dmi_save_ident(dm, DMI_PRODUCT_VERSION, 6);
435                 dmi_save_ident(dm, DMI_PRODUCT_SERIAL, 7);
436                 dmi_save_uuid(dm, DMI_PRODUCT_UUID, 8);
437                 dmi_save_ident(dm, DMI_PRODUCT_FAMILY, 26);
438                 break;
439         case 2:         /* Base Board Information */
440                 dmi_save_ident(dm, DMI_BOARD_VENDOR, 4);
441                 dmi_save_ident(dm, DMI_BOARD_NAME, 5);
442                 dmi_save_ident(dm, DMI_BOARD_VERSION, 6);
443                 dmi_save_ident(dm, DMI_BOARD_SERIAL, 7);
444                 dmi_save_ident(dm, DMI_BOARD_ASSET_TAG, 8);
445                 break;
446         case 3:         /* Chassis Information */
447                 dmi_save_ident(dm, DMI_CHASSIS_VENDOR, 4);
448                 dmi_save_type(dm, DMI_CHASSIS_TYPE, 5);
449                 dmi_save_ident(dm, DMI_CHASSIS_VERSION, 6);
450                 dmi_save_ident(dm, DMI_CHASSIS_SERIAL, 7);
451                 dmi_save_ident(dm, DMI_CHASSIS_ASSET_TAG, 8);
452                 break;
453         case 9:         /* System Slots */
454                 dmi_save_system_slot(dm);
455                 break;
456         case 10:        /* Onboard Devices Information */
457                 dmi_save_devices(dm);
458                 break;
459         case 11:        /* OEM Strings */
460                 dmi_save_oem_strings_devices(dm);
461                 break;
462         case 38:        /* IPMI Device Information */
463                 dmi_save_ipmi_device(dm);
464                 break;
465         case 41:        /* Onboard Devices Extended Information */
466                 dmi_save_extended_devices(dm);
467         }
468 }
469
470 static int __init print_filtered(char *buf, size_t len, const char *info)
471 {
472         int c = 0;
473         const char *p;
474
475         if (!info)
476                 return c;
477
478         for (p = info; *p; p++)
479                 if (isprint(*p))
480                         c += scnprintf(buf + c, len - c, "%c", *p);
481                 else
482                         c += scnprintf(buf + c, len - c, "\\x%02x", *p & 0xff);
483         return c;
484 }
485
486 static void __init dmi_format_ids(char *buf, size_t len)
487 {
488         int c = 0;
489         const char *board;      /* Board Name is optional */
490
491         c += print_filtered(buf + c, len - c,
492                             dmi_get_system_info(DMI_SYS_VENDOR));
493         c += scnprintf(buf + c, len - c, " ");
494         c += print_filtered(buf + c, len - c,
495                             dmi_get_system_info(DMI_PRODUCT_NAME));
496
497         board = dmi_get_system_info(DMI_BOARD_NAME);
498         if (board) {
499                 c += scnprintf(buf + c, len - c, "/");
500                 c += print_filtered(buf + c, len - c, board);
501         }
502         c += scnprintf(buf + c, len - c, ", BIOS ");
503         c += print_filtered(buf + c, len - c,
504                             dmi_get_system_info(DMI_BIOS_VERSION));
505         c += scnprintf(buf + c, len - c, " ");
506         c += print_filtered(buf + c, len - c,
507                             dmi_get_system_info(DMI_BIOS_DATE));
508 }
509
510 /*
511  * Check for DMI/SMBIOS headers in the system firmware image.  Any
512  * SMBIOS header must start 16 bytes before the DMI header, so take a
513  * 32 byte buffer and check for DMI at offset 16 and SMBIOS at offset
514  * 0.  If the DMI header is present, set dmi_ver accordingly (SMBIOS
515  * takes precedence) and return 0.  Otherwise return 1.
516  */
517 static int __init dmi_present(const u8 *buf)
518 {
519         u32 smbios_ver;
520
521         if (memcmp(buf, "_SM_", 4) == 0 &&
522             buf[5] < 32 && dmi_checksum(buf, buf[5])) {
523                 smbios_ver = get_unaligned_be16(buf + 6);
524                 smbios_entry_point_size = buf[5];
525                 memcpy(smbios_entry_point, buf, smbios_entry_point_size);
526
527                 /* Some BIOS report weird SMBIOS version, fix that up */
528                 switch (smbios_ver) {
529                 case 0x021F:
530                 case 0x0221:
531                         pr_debug("SMBIOS version fixup (2.%d->2.%d)\n",
532                                  smbios_ver & 0xFF, 3);
533                         smbios_ver = 0x0203;
534                         break;
535                 case 0x0233:
536                         pr_debug("SMBIOS version fixup (2.%d->2.%d)\n", 51, 6);
537                         smbios_ver = 0x0206;
538                         break;
539                 }
540         } else {
541                 smbios_ver = 0;
542         }
543
544         buf += 16;
545
546         if (memcmp(buf, "_DMI_", 5) == 0 && dmi_checksum(buf, 15)) {
547                 if (smbios_ver)
548                         dmi_ver = smbios_ver;
549                 else
550                         dmi_ver = (buf[14] & 0xF0) << 4 | (buf[14] & 0x0F);
551                 dmi_ver <<= 8;
552                 dmi_num = get_unaligned_le16(buf + 12);
553                 dmi_len = get_unaligned_le16(buf + 6);
554                 dmi_base = get_unaligned_le32(buf + 8);
555
556                 if (dmi_walk_early(dmi_decode) == 0) {
557                         if (smbios_ver) {
558                                 pr_info("SMBIOS %d.%d present.\n",
559                                         dmi_ver >> 16, (dmi_ver >> 8) & 0xFF);
560                         } else {
561                                 smbios_entry_point_size = 15;
562                                 memcpy(smbios_entry_point, buf,
563                                        smbios_entry_point_size);
564                                 pr_info("Legacy DMI %d.%d present.\n",
565                                         dmi_ver >> 16, (dmi_ver >> 8) & 0xFF);
566                         }
567                         dmi_format_ids(dmi_ids_string, sizeof(dmi_ids_string));
568                         pr_info("DMI: %s\n", dmi_ids_string);
569                         return 0;
570                 }
571         }
572
573         return 1;
574 }
575
576 /*
577  * Check for the SMBIOS 3.0 64-bit entry point signature. Unlike the legacy
578  * 32-bit entry point, there is no embedded DMI header (_DMI_) in here.
579  */
580 static int __init dmi_smbios3_present(const u8 *buf)
581 {
582         if (memcmp(buf, "_SM3_", 5) == 0 &&
583             buf[6] < 32 && dmi_checksum(buf, buf[6])) {
584                 dmi_ver = get_unaligned_be32(buf + 6) & 0xFFFFFF;
585                 dmi_num = 0;                    /* No longer specified */
586                 dmi_len = get_unaligned_le32(buf + 12);
587                 dmi_base = get_unaligned_le64(buf + 16);
588                 smbios_entry_point_size = buf[6];
589                 memcpy(smbios_entry_point, buf, smbios_entry_point_size);
590
591                 if (dmi_walk_early(dmi_decode) == 0) {
592                         pr_info("SMBIOS %d.%d.%d present.\n",
593                                 dmi_ver >> 16, (dmi_ver >> 8) & 0xFF,
594                                 dmi_ver & 0xFF);
595                         dmi_format_ids(dmi_ids_string, sizeof(dmi_ids_string));
596                         pr_info("DMI: %s\n", dmi_ids_string);
597                         return 0;
598                 }
599         }
600         return 1;
601 }
602
603 void __init dmi_scan_machine(void)
604 {
605         char __iomem *p, *q;
606         char buf[32];
607
608         if (efi_enabled(EFI_CONFIG_TABLES)) {
609                 /*
610                  * According to the DMTF SMBIOS reference spec v3.0.0, it is
611                  * allowed to define both the 64-bit entry point (smbios3) and
612                  * the 32-bit entry point (smbios), in which case they should
613                  * either both point to the same SMBIOS structure table, or the
614                  * table pointed to by the 64-bit entry point should contain a
615                  * superset of the table contents pointed to by the 32-bit entry
616                  * point (section 5.2)
617                  * This implies that the 64-bit entry point should have
618                  * precedence if it is defined and supported by the OS. If we
619                  * have the 64-bit entry point, but fail to decode it, fall
620                  * back to the legacy one (if available)
621                  */
622                 if (efi.smbios3 != EFI_INVALID_TABLE_ADDR) {
623                         p = dmi_early_remap(efi.smbios3, 32);
624                         if (p == NULL)
625                                 goto error;
626                         memcpy_fromio(buf, p, 32);
627                         dmi_early_unmap(p, 32);
628
629                         if (!dmi_smbios3_present(buf)) {
630                                 dmi_available = 1;
631                                 return;
632                         }
633                 }
634                 if (efi.smbios == EFI_INVALID_TABLE_ADDR)
635                         goto error;
636
637                 /* This is called as a core_initcall() because it isn't
638                  * needed during early boot.  This also means we can
639                  * iounmap the space when we're done with it.
640                  */
641                 p = dmi_early_remap(efi.smbios, 32);
642                 if (p == NULL)
643                         goto error;
644                 memcpy_fromio(buf, p, 32);
645                 dmi_early_unmap(p, 32);
646
647                 if (!dmi_present(buf)) {
648                         dmi_available = 1;
649                         return;
650                 }
651         } else if (IS_ENABLED(CONFIG_DMI_SCAN_MACHINE_NON_EFI_FALLBACK)) {
652                 p = dmi_early_remap(0xF0000, 0x10000);
653                 if (p == NULL)
654                         goto error;
655
656                 /*
657                  * Same logic as above, look for a 64-bit entry point
658                  * first, and if not found, fall back to 32-bit entry point.
659                  */
660                 memcpy_fromio(buf, p, 16);
661                 for (q = p + 16; q < p + 0x10000; q += 16) {
662                         memcpy_fromio(buf + 16, q, 16);
663                         if (!dmi_smbios3_present(buf)) {
664                                 dmi_available = 1;
665                                 dmi_early_unmap(p, 0x10000);
666                                 return;
667                         }
668                         memcpy(buf, buf + 16, 16);
669                 }
670
671                 /*
672                  * Iterate over all possible DMI header addresses q.
673                  * Maintain the 32 bytes around q in buf.  On the
674                  * first iteration, substitute zero for the
675                  * out-of-range bytes so there is no chance of falsely
676                  * detecting an SMBIOS header.
677                  */
678                 memset(buf, 0, 16);
679                 for (q = p; q < p + 0x10000; q += 16) {
680                         memcpy_fromio(buf + 16, q, 16);
681                         if (!dmi_present(buf)) {
682                                 dmi_available = 1;
683                                 dmi_early_unmap(p, 0x10000);
684                                 return;
685                         }
686                         memcpy(buf, buf + 16, 16);
687                 }
688                 dmi_early_unmap(p, 0x10000);
689         }
690  error:
691         pr_info("DMI not present or invalid.\n");
692 }
693
694 static ssize_t raw_table_read(struct file *file, struct kobject *kobj,
695                               struct bin_attribute *attr, char *buf,
696                               loff_t pos, size_t count)
697 {
698         memcpy(buf, attr->private + pos, count);
699         return count;
700 }
701
702 static BIN_ATTR(smbios_entry_point, S_IRUSR, raw_table_read, NULL, 0);
703 static BIN_ATTR(DMI, S_IRUSR, raw_table_read, NULL, 0);
704
705 static int __init dmi_init(void)
706 {
707         struct kobject *tables_kobj;
708         u8 *dmi_table;
709         int ret = -ENOMEM;
710
711         if (!dmi_available) {
712                 ret = -ENODATA;
713                 goto err;
714         }
715
716         /*
717          * Set up dmi directory at /sys/firmware/dmi. This entry should stay
718          * even after farther error, as it can be used by other modules like
719          * dmi-sysfs.
720          */
721         dmi_kobj = kobject_create_and_add("dmi", firmware_kobj);
722         if (!dmi_kobj)
723                 goto err;
724
725         tables_kobj = kobject_create_and_add("tables", dmi_kobj);
726         if (!tables_kobj)
727                 goto err;
728
729         dmi_table = dmi_remap(dmi_base, dmi_len);
730         if (!dmi_table)
731                 goto err_tables;
732
733         bin_attr_smbios_entry_point.size = smbios_entry_point_size;
734         bin_attr_smbios_entry_point.private = smbios_entry_point;
735         ret = sysfs_create_bin_file(tables_kobj, &bin_attr_smbios_entry_point);
736         if (ret)
737                 goto err_unmap;
738
739         bin_attr_DMI.size = dmi_len;
740         bin_attr_DMI.private = dmi_table;
741         ret = sysfs_create_bin_file(tables_kobj, &bin_attr_DMI);
742         if (!ret)
743                 return 0;
744
745         sysfs_remove_bin_file(tables_kobj,
746                               &bin_attr_smbios_entry_point);
747  err_unmap:
748         dmi_unmap(dmi_table);
749  err_tables:
750         kobject_del(tables_kobj);
751         kobject_put(tables_kobj);
752  err:
753         pr_err("dmi: Firmware registration failed.\n");
754
755         return ret;
756 }
757 subsys_initcall(dmi_init);
758
759 /**
760  * dmi_set_dump_stack_arch_desc - set arch description for dump_stack()
761  *
762  * Invoke dump_stack_set_arch_desc() with DMI system information so that
763  * DMI identifiers are printed out on task dumps.  Arch boot code should
764  * call this function after dmi_scan_machine() if it wants to print out DMI
765  * identifiers on task dumps.
766  */
767 void __init dmi_set_dump_stack_arch_desc(void)
768 {
769         dump_stack_set_arch_desc("%s", dmi_ids_string);
770 }
771
772 /**
773  *      dmi_matches - check if dmi_system_id structure matches system DMI data
774  *      @dmi: pointer to the dmi_system_id structure to check
775  */
776 static bool dmi_matches(const struct dmi_system_id *dmi)
777 {
778         int i;
779
780         for (i = 0; i < ARRAY_SIZE(dmi->matches); i++) {
781                 int s = dmi->matches[i].slot;
782                 if (s == DMI_NONE)
783                         break;
784                 if (dmi_ident[s]) {
785                         if (dmi->matches[i].exact_match) {
786                                 if (!strcmp(dmi_ident[s],
787                                             dmi->matches[i].substr))
788                                         continue;
789                         } else {
790                                 if (strstr(dmi_ident[s],
791                                            dmi->matches[i].substr))
792                                         continue;
793                         }
794                 }
795
796                 /* No match */
797                 return false;
798         }
799         return true;
800 }
801
802 /**
803  *      dmi_is_end_of_table - check for end-of-table marker
804  *      @dmi: pointer to the dmi_system_id structure to check
805  */
806 static bool dmi_is_end_of_table(const struct dmi_system_id *dmi)
807 {
808         return dmi->matches[0].slot == DMI_NONE;
809 }
810
811 /**
812  *      dmi_check_system - check system DMI data
813  *      @list: array of dmi_system_id structures to match against
814  *              All non-null elements of the list must match
815  *              their slot's (field index's) data (i.e., each
816  *              list string must be a substring of the specified
817  *              DMI slot's string data) to be considered a
818  *              successful match.
819  *
820  *      Walk the blacklist table running matching functions until someone
821  *      returns non zero or we hit the end. Callback function is called for
822  *      each successful match. Returns the number of matches.
823  *
824  *      dmi_scan_machine must be called before this function is called.
825  */
826 int dmi_check_system(const struct dmi_system_id *list)
827 {
828         int count = 0;
829         const struct dmi_system_id *d;
830
831         for (d = list; !dmi_is_end_of_table(d); d++)
832                 if (dmi_matches(d)) {
833                         count++;
834                         if (d->callback && d->callback(d))
835                                 break;
836                 }
837
838         return count;
839 }
840 EXPORT_SYMBOL(dmi_check_system);
841
842 /**
843  *      dmi_first_match - find dmi_system_id structure matching system DMI data
844  *      @list: array of dmi_system_id structures to match against
845  *              All non-null elements of the list must match
846  *              their slot's (field index's) data (i.e., each
847  *              list string must be a substring of the specified
848  *              DMI slot's string data) to be considered a
849  *              successful match.
850  *
851  *      Walk the blacklist table until the first match is found.  Return the
852  *      pointer to the matching entry or NULL if there's no match.
853  *
854  *      dmi_scan_machine must be called before this function is called.
855  */
856 const struct dmi_system_id *dmi_first_match(const struct dmi_system_id *list)
857 {
858         const struct dmi_system_id *d;
859
860         for (d = list; !dmi_is_end_of_table(d); d++)
861                 if (dmi_matches(d))
862                         return d;
863
864         return NULL;
865 }
866 EXPORT_SYMBOL(dmi_first_match);
867
868 /**
869  *      dmi_get_system_info - return DMI data value
870  *      @field: data index (see enum dmi_field)
871  *
872  *      Returns one DMI data value, can be used to perform
873  *      complex DMI data checks.
874  */
875 const char *dmi_get_system_info(int field)
876 {
877         return dmi_ident[field];
878 }
879 EXPORT_SYMBOL(dmi_get_system_info);
880
881 /**
882  * dmi_name_in_serial - Check if string is in the DMI product serial information
883  * @str: string to check for
884  */
885 int dmi_name_in_serial(const char *str)
886 {
887         int f = DMI_PRODUCT_SERIAL;
888         if (dmi_ident[f] && strstr(dmi_ident[f], str))
889                 return 1;
890         return 0;
891 }
892
893 /**
894  *      dmi_name_in_vendors - Check if string is in the DMI system or board vendor name
895  *      @str: Case sensitive Name
896  */
897 int dmi_name_in_vendors(const char *str)
898 {
899         static int fields[] = { DMI_SYS_VENDOR, DMI_BOARD_VENDOR, DMI_NONE };
900         int i;
901         for (i = 0; fields[i] != DMI_NONE; i++) {
902                 int f = fields[i];
903                 if (dmi_ident[f] && strstr(dmi_ident[f], str))
904                         return 1;
905         }
906         return 0;
907 }
908 EXPORT_SYMBOL(dmi_name_in_vendors);
909
910 /**
911  *      dmi_find_device - find onboard device by type/name
912  *      @type: device type or %DMI_DEV_TYPE_ANY to match all device types
913  *      @name: device name string or %NULL to match all
914  *      @from: previous device found in search, or %NULL for new search.
915  *
916  *      Iterates through the list of known onboard devices. If a device is
917  *      found with a matching @type and @name, a pointer to its device
918  *      structure is returned.  Otherwise, %NULL is returned.
919  *      A new search is initiated by passing %NULL as the @from argument.
920  *      If @from is not %NULL, searches continue from next device.
921  */
922 const struct dmi_device *dmi_find_device(int type, const char *name,
923                                     const struct dmi_device *from)
924 {
925         const struct list_head *head = from ? &from->list : &dmi_devices;
926         struct list_head *d;
927
928         for (d = head->next; d != &dmi_devices; d = d->next) {
929                 const struct dmi_device *dev =
930                         list_entry(d, struct dmi_device, list);
931
932                 if (((type == DMI_DEV_TYPE_ANY) || (dev->type == type)) &&
933                     ((name == NULL) || (strcmp(dev->name, name) == 0)))
934                         return dev;
935         }
936
937         return NULL;
938 }
939 EXPORT_SYMBOL(dmi_find_device);
940
941 /**
942  *      dmi_get_date - parse a DMI date
943  *      @field: data index (see enum dmi_field)
944  *      @yearp: optional out parameter for the year
945  *      @monthp: optional out parameter for the month
946  *      @dayp: optional out parameter for the day
947  *
948  *      The date field is assumed to be in the form resembling
949  *      [mm[/dd]]/yy[yy] and the result is stored in the out
950  *      parameters any or all of which can be omitted.
951  *
952  *      If the field doesn't exist, all out parameters are set to zero
953  *      and false is returned.  Otherwise, true is returned with any
954  *      invalid part of date set to zero.
955  *
956  *      On return, year, month and day are guaranteed to be in the
957  *      range of [0,9999], [0,12] and [0,31] respectively.
958  */
959 bool dmi_get_date(int field, int *yearp, int *monthp, int *dayp)
960 {
961         int year = 0, month = 0, day = 0;
962         bool exists;
963         const char *s, *y;
964         char *e;
965
966         s = dmi_get_system_info(field);
967         exists = s;
968         if (!exists)
969                 goto out;
970
971         /*
972          * Determine year first.  We assume the date string resembles
973          * mm/dd/yy[yy] but the original code extracted only the year
974          * from the end.  Keep the behavior in the spirit of no
975          * surprises.
976          */
977         y = strrchr(s, '/');
978         if (!y)
979                 goto out;
980
981         y++;
982         year = simple_strtoul(y, &e, 10);
983         if (y != e && year < 100) {     /* 2-digit year */
984                 year += 1900;
985                 if (year < 1996)        /* no dates < spec 1.0 */
986                         year += 100;
987         }
988         if (year > 9999)                /* year should fit in %04d */
989                 year = 0;
990
991         /* parse the mm and dd */
992         month = simple_strtoul(s, &e, 10);
993         if (s == e || *e != '/' || !month || month > 12) {
994                 month = 0;
995                 goto out;
996         }
997
998         s = e + 1;
999         day = simple_strtoul(s, &e, 10);
1000         if (s == y || s == e || *e != '/' || day > 31)
1001                 day = 0;
1002 out:
1003         if (yearp)
1004                 *yearp = year;
1005         if (monthp)
1006                 *monthp = month;
1007         if (dayp)
1008                 *dayp = day;
1009         return exists;
1010 }
1011 EXPORT_SYMBOL(dmi_get_date);
1012
1013 /**
1014  *      dmi_walk - Walk the DMI table and get called back for every record
1015  *      @decode: Callback function
1016  *      @private_data: Private data to be passed to the callback function
1017  *
1018  *      Returns 0 on success, -ENXIO if DMI is not selected or not present,
1019  *      or a different negative error code if DMI walking fails.
1020  */
1021 int dmi_walk(void (*decode)(const struct dmi_header *, void *),
1022              void *private_data)
1023 {
1024         u8 *buf;
1025
1026         if (!dmi_available)
1027                 return -ENXIO;
1028
1029         buf = dmi_remap(dmi_base, dmi_len);
1030         if (buf == NULL)
1031                 return -ENOMEM;
1032
1033         dmi_decode_table(buf, decode, private_data);
1034
1035         dmi_unmap(buf);
1036         return 0;
1037 }
1038 EXPORT_SYMBOL_GPL(dmi_walk);
1039
1040 /**
1041  * dmi_match - compare a string to the dmi field (if exists)
1042  * @f: DMI field identifier
1043  * @str: string to compare the DMI field to
1044  *
1045  * Returns true if the requested field equals to the str (including NULL).
1046  */
1047 bool dmi_match(enum dmi_field f, const char *str)
1048 {
1049         const char *info = dmi_get_system_info(f);
1050
1051         if (info == NULL || str == NULL)
1052                 return info == str;
1053
1054         return !strcmp(info, str);
1055 }
1056 EXPORT_SYMBOL_GPL(dmi_match);
1057
1058 void dmi_memdev_name(u16 handle, const char **bank, const char **device)
1059 {
1060         int n;
1061
1062         if (dmi_memdev == NULL)
1063                 return;
1064
1065         for (n = 0; n < dmi_memdev_nr; n++) {
1066                 if (handle == dmi_memdev[n].handle) {
1067                         *bank = dmi_memdev[n].bank;
1068                         *device = dmi_memdev[n].device;
1069                         break;
1070                 }
1071         }
1072 }
1073 EXPORT_SYMBOL_GPL(dmi_memdev_name);