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