]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/firmware/dmi_scan.c
UBUNTU: Ubuntu-4.15.0-96.97
[mirror_ubuntu-bionic-kernel.git] / drivers / firmware / dmi_scan.c
CommitLineData
1da177e4 1#include <linux/types.h>
1da177e4
LT
2#include <linux/string.h>
3#include <linux/init.h>
4#include <linux/module.h>
8881cdce 5#include <linux/ctype.h>
1da177e4 6#include <linux/dmi.h>
3ed3bce8 7#include <linux/efi.h>
1da177e4 8#include <linux/bootmem.h>
d114a333 9#include <linux/random.h>
f2d3efed 10#include <asm/dmi.h>
0841c04d 11#include <asm/unaligned.h>
1da177e4 12
d7f96f97
IK
13struct kobject *dmi_kobj;
14EXPORT_SYMBOL_GPL(dmi_kobj);
15
cb5dd7c1
PJ
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 */
77476ed9 21static const char dmi_empty_string[] = "";
79da4721 22
95be58df 23static u32 dmi_ver __initdata;
552e19d8
IK
24static u32 dmi_len;
25static u16 dmi_num;
d7f96f97
IK
26static u8 smbios_entry_point[32];
27static int smbios_entry_point_size;
28
9a22b6e7
IM
29/*
30 * Catch too early calls to dmi_check_system():
31 */
32static int dmi_initialized;
33
c90fe6bc
TH
34/* DMI system identification string used during boot */
35static char dmi_ids_string[128] __initdata;
36
dd6dad42
CG
37static struct dmi_memdev_info {
38 const char *device;
39 const char *bank;
40 u16 handle;
41} *dmi_memdev;
42static int dmi_memdev_nr;
43
f3069ae9 44static const char * __init dmi_string_nosave(const struct dmi_header *dm, u8 s)
1da177e4 45{
1855256c 46 const u8 *bp = ((u8 *) dm) + dm->length;
77476ed9 47 const u8 *nsp;
1249c513 48
c3c7120d 49 if (s) {
77476ed9 50 while (--s > 0 && *bp)
c3c7120d 51 bp += strlen(bp) + 1;
c3c7120d 52
77476ed9
JD
53 /* Strings containing only spaces are considered empty */
54 nsp = bp;
55 while (*nsp == ' ')
56 nsp++;
57 if (*nsp != '\0')
f3069ae9 58 return bp;
4f705ae3 59 }
c3c7120d 60
77476ed9 61 return dmi_empty_string;
f3069ae9
JD
62}
63
ffbbb96d 64static const char * __init dmi_string(const struct dmi_header *dm, u8 s)
f3069ae9
JD
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);
f3069ae9 77
c3c7120d 78 return str;
1da177e4
LT
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 */
eb4c5ea5
IK
85static void dmi_decode_table(u8 *buf,
86 void (*decode)(const struct dmi_header *, void *),
87 void *private_data)
1da177e4 88{
7fce084a 89 u8 *data = buf;
1249c513 90 int i = 0;
4f705ae3 91
1da177e4 92 /*
bfbaafae 93 * Stop when we have seen all the items the table claimed to have
17cd5bd5
JD
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.)
4f705ae3 97 */
9c65e12a
LT
98 while ((!dmi_num || i < dmi_num) &&
99 (data - buf + sizeof(struct dmi_header)) <= dmi_len) {
1855256c
JG
100 const struct dmi_header *dm = (const struct dmi_header *)data;
101
1da177e4 102 /*
8638545c
AC
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
1da177e4 106 */
1249c513 107 data += dm->length;
552e19d8 108 while ((data - buf < dmi_len - 1) && (data[0] || data[1]))
1da177e4 109 data++;
552e19d8 110 if (data - buf < dmi_len - 1)
e7a19c56 111 decode(dm, private_data);
ce204e9a 112
6e0ad59e
JD
113 data += 2;
114 i++;
115
ce204e9a
IK
116 /*
117 * 7.45 End-of-Table (Type 127) [SMBIOS reference spec v3.0.0]
17cd5bd5
JD
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.
ce204e9a 123 */
17cd5bd5 124 if (!dmi_num && dm->type == DMI_ENTRY_END_OF_TABLE)
ce204e9a 125 break;
1da177e4 126 }
6e0ad59e
JD
127
128 /* Trim DMI table length if needed */
129 if (dmi_len > data - buf)
130 dmi_len = data - buf;
7fce084a
JD
131}
132
fc430262 133static phys_addr_t dmi_base;
7fce084a 134
e7a19c56
JD
135static int __init dmi_walk_early(void (*decode)(const struct dmi_header *,
136 void *))
7fce084a
JD
137{
138 u8 *buf;
6e0ad59e 139 u32 orig_dmi_len = dmi_len;
7fce084a 140
6e0ad59e 141 buf = dmi_early_remap(dmi_base, orig_dmi_len);
7fce084a 142 if (buf == NULL)
c9268200 143 return -ENOMEM;
7fce084a 144
eb4c5ea5 145 dmi_decode_table(buf, decode, NULL);
7fce084a 146
d114a333
TL
147 add_device_randomness(buf, dmi_len);
148
6e0ad59e 149 dmi_early_unmap(buf, orig_dmi_len);
1da177e4
LT
150 return 0;
151}
152
9f9c9cbb 153static int __init dmi_checksum(const u8 *buf, u8 len)
1da177e4 154{
1249c513 155 u8 sum = 0;
1da177e4 156 int a;
4f705ae3 157
9f9c9cbb 158 for (a = 0; a < len; a++)
1249c513
AP
159 sum += buf[a];
160
161 return sum == 0;
1da177e4
LT
162}
163
ffbbb96d 164static const char *dmi_ident[DMI_STRING_MAX];
ebad6a42 165static LIST_HEAD(dmi_devices);
4f5c791a 166int dmi_available;
1da177e4
LT
167
168/*
169 * Save a DMI string
170 */
02d9c47f
JD
171static void __init dmi_save_ident(const struct dmi_header *dm, int slot,
172 int string)
1da177e4 173{
02d9c47f 174 const char *d = (const char *) dm;
ffbbb96d 175 const char *p;
1249c513 176
a814c359 177 if (dmi_ident[slot] || dm->length <= string)
1da177e4 178 return;
1249c513 179
c3c7120d
AP
180 p = dmi_string(dm, d[string]);
181 if (p == NULL)
182 return;
183
184 dmi_ident[slot] = p;
1da177e4
LT
185}
186
02d9c47f
JD
187static void __init dmi_save_uuid(const struct dmi_header *dm, int slot,
188 int index)
4f5c791a 189{
a814c359 190 const u8 *d;
4f5c791a
LP
191 char *s;
192 int is_ff = 1, is_00 = 1, i;
193
2858e4e8 194 if (dmi_ident[slot] || dm->length < index + 16)
4f5c791a
LP
195 return;
196
a814c359 197 d = (u8 *) dm + index;
4f5c791a 198 for (i = 0; i < 16 && (is_ff || is_00); i++) {
f1d8e614
ZD
199 if (d[i] != 0x00)
200 is_00 = 0;
201 if (d[i] != 0xFF)
202 is_ff = 0;
4f5c791a
LP
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
f1d8e614
ZD
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 */
95be58df 217 if (dmi_ver >= 0x020600)
f1d8e614
ZD
218 sprintf(s, "%pUL", d);
219 else
220 sprintf(s, "%pUB", d);
4f5c791a 221
02d9c47f 222 dmi_ident[slot] = s;
4f5c791a
LP
223}
224
02d9c47f
JD
225static void __init dmi_save_type(const struct dmi_header *dm, int slot,
226 int index)
4f5c791a 227{
a814c359 228 const u8 *d;
4f5c791a
LP
229 char *s;
230
a814c359 231 if (dmi_ident[slot] || dm->length <= index)
4f5c791a
LP
232 return;
233
234 s = dmi_alloc(4);
235 if (!s)
236 return;
237
a814c359 238 d = (u8 *) dm + index;
4f5c791a
LP
239 sprintf(s, "%u", *d & 0x7F);
240 dmi_ident[slot] = s;
241}
242
f3069ae9
JD
243static 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);
ae797449 252 if (!dev)
f3069ae9 253 return;
f3069ae9
JD
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
1855256c 262static void __init dmi_save_devices(const struct dmi_header *dm)
ebad6a42
AP
263{
264 int i, count = (dm->length - sizeof(struct dmi_header)) / 2;
ebad6a42
AP
265
266 for (i = 0; i < count; i++) {
1855256c 267 const char *d = (char *)(dm + 1) + (i * 2);
ebad6a42
AP
268
269 /* Skip disabled device */
270 if ((*d & 0x80) == 0)
271 continue;
272
f3069ae9 273 dmi_save_one_device(*d & 0x7f, dmi_string_nosave(dm, *(d + 1)));
2e0c1f6c
SM
274 }
275}
276
1855256c 277static void __init dmi_save_oem_strings_devices(const struct dmi_header *dm)
2e0c1f6c 278{
a814c359 279 int i, count;
2e0c1f6c
SM
280 struct dmi_device *dev;
281
a814c359
JD
282 if (dm->length < 0x05)
283 return;
284
285 count = *(u8 *)(dm + 1);
2e0c1f6c 286 for (i = 1; i <= count; i++) {
ffbbb96d 287 const char *devname = dmi_string(dm, i);
79da4721 288
43fe105a 289 if (devname == dmi_empty_string)
79da4721 290 continue;
79da4721 291
2e0c1f6c 292 dev = dmi_alloc(sizeof(*dev));
ae797449 293 if (!dev)
2e0c1f6c 294 break;
2e0c1f6c
SM
295
296 dev->type = DMI_DEV_TYPE_OEM_STRING;
79da4721 297 dev->name = devname;
2e0c1f6c 298 dev->device_data = NULL;
ebad6a42
AP
299
300 list_add(&dev->list, &dmi_devices);
301 }
302}
303
1855256c 304static void __init dmi_save_ipmi_device(const struct dmi_header *dm)
ebad6a42
AP
305{
306 struct dmi_device *dev;
02d9c47f 307 void *data;
ebad6a42 308
e9928674 309 data = dmi_alloc(dm->length);
ae797449 310 if (data == NULL)
ebad6a42 311 return;
ebad6a42
AP
312
313 memcpy(data, dm, dm->length);
314
e9928674 315 dev = dmi_alloc(sizeof(*dev));
ae797449 316 if (!dev)
ebad6a42 317 return;
ebad6a42
AP
318
319 dev->type = DMI_DEV_TYPE_IPMI;
320 dev->name = "IPMI controller";
321 dev->device_data = data;
322
abd24df8 323 list_add_tail(&dev->list, &dmi_devices);
ebad6a42
AP
324}
325
e5b6c151
JH
326static void __init dmi_save_dev_pciaddr(int instance, int segment, int bus,
327 int devfn, const char *name, int type)
911e1c9b 328{
e5b6c151 329 struct dmi_dev_onboard *dev;
911e1c9b 330
e5b6c151
JH
331 /* Ignore invalid values */
332 if (type == DMI_DEV_TYPE_DEV_SLOT &&
333 segment == 0xFFFF && bus == 0xFF && devfn == 0xFF)
911e1c9b 334 return;
ae797449 335
e5b6c151
JH
336 dev = dmi_alloc(sizeof(*dev) + strlen(name) + 1);
337 if (!dev)
338 return;
911e1c9b 339
e5b6c151
JH
340 dev->instance = instance;
341 dev->segment = segment;
342 dev->bus = bus;
343 dev->devfn = devfn;
911e1c9b 344
e5b6c151
JH
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);
911e1c9b
N
351}
352
b4bd7d59
WVS
353static void __init dmi_save_extended_devices(const struct dmi_header *dm)
354{
96e23943 355 const char *name;
45b98257 356 const u8 *d = (u8 *)dm;
b4bd7d59 357
a814c359
JD
358 if (dm->length < 0x0B)
359 return;
360
b4bd7d59 361 /* Skip disabled device */
45b98257 362 if ((d[0x5] & 0x80) == 0)
b4bd7d59
WVS
363 return;
364
45b98257 365 name = dmi_string_nosave(dm, d[0x4]);
e5b6c151
JH
366 dmi_save_dev_pciaddr(d[0x6], *(u16 *)(d + 0x7), d[0x9], d[0xA], name,
367 DMI_DEV_TYPE_DEV_ONBOARD);
45b98257 368 dmi_save_one_device(d[0x5] & 0x7f, name);
b4bd7d59
WVS
369}
370
e5b6c151
JH
371static 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
dd6dad42
CG
383static 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
390static 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
a814c359 395 if (dm->type != DMI_ENTRY_MEM_DEVICE || dm->length < 0x12)
dd6dad42
CG
396 return;
397 if (nr >= dmi_memdev_nr) {
398 pr_warn(FW_BUG "Too many DIMM entries in SMBIOS table\n");
399 return;
400 }
0841c04d 401 dmi_memdev[nr].handle = get_unaligned(&dm->handle);
dd6dad42
CG
402 dmi_memdev[nr].device = dmi_string(dm, d[0x10]);
403 dmi_memdev[nr].bank = dmi_string(dm, d[0x11]);
404 nr++;
405}
406
407void __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
1da177e4
LT
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 */
e7a19c56 424static void __init dmi_decode(const struct dmi_header *dm, void *dummy)
1da177e4 425{
02d9c47f 426 switch (dm->type) {
ebad6a42 427 case 0: /* BIOS Information */
1249c513 428 dmi_save_ident(dm, DMI_BIOS_VENDOR, 4);
1249c513 429 dmi_save_ident(dm, DMI_BIOS_VERSION, 5);
1249c513
AP
430 dmi_save_ident(dm, DMI_BIOS_DATE, 8);
431 break;
ebad6a42 432 case 1: /* System Information */
1249c513 433 dmi_save_ident(dm, DMI_SYS_VENDOR, 4);
1249c513 434 dmi_save_ident(dm, DMI_PRODUCT_NAME, 5);
1249c513 435 dmi_save_ident(dm, DMI_PRODUCT_VERSION, 6);
1249c513 436 dmi_save_ident(dm, DMI_PRODUCT_SERIAL, 7);
4f5c791a 437 dmi_save_uuid(dm, DMI_PRODUCT_UUID, 8);
c61872c9 438 dmi_save_ident(dm, DMI_PRODUCT_FAMILY, 26);
1249c513 439 break;
ebad6a42 440 case 2: /* Base Board Information */
1249c513 441 dmi_save_ident(dm, DMI_BOARD_VENDOR, 4);
1249c513 442 dmi_save_ident(dm, DMI_BOARD_NAME, 5);
1249c513 443 dmi_save_ident(dm, DMI_BOARD_VERSION, 6);
4f5c791a
LP
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);
1249c513 453 break;
e5b6c151
JH
454 case 9: /* System Slots */
455 dmi_save_system_slot(dm);
456 break;
ebad6a42
AP
457 case 10: /* Onboard Devices Information */
458 dmi_save_devices(dm);
459 break;
2e0c1f6c
SM
460 case 11: /* OEM Strings */
461 dmi_save_oem_strings_devices(dm);
462 break;
ebad6a42
AP
463 case 38: /* IPMI Device Information */
464 dmi_save_ipmi_device(dm);
b4bd7d59
WVS
465 break;
466 case 41: /* Onboard Devices Extended Information */
467 dmi_save_extended_devices(dm);
1da177e4
LT
468 }
469}
470
c90fe6bc 471static int __init print_filtered(char *buf, size_t len, const char *info)
8881cdce 472{
c90fe6bc 473 int c = 0;
8881cdce
BH
474 const char *p;
475
476 if (!info)
c90fe6bc 477 return c;
8881cdce
BH
478
479 for (p = info; *p; p++)
480 if (isprint(*p))
c90fe6bc 481 c += scnprintf(buf + c, len - c, "%c", *p);
8881cdce 482 else
c90fe6bc
TH
483 c += scnprintf(buf + c, len - c, "\\x%02x", *p & 0xff);
484 return c;
8881cdce
BH
485}
486
c90fe6bc 487static void __init dmi_format_ids(char *buf, size_t len)
8881cdce 488{
c90fe6bc 489 int c = 0;
84e383b3
NC
490 const char *board; /* Board Name is optional */
491
c90fe6bc
TH
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
84e383b3
NC
498 board = dmi_get_system_info(DMI_BOARD_NAME);
499 if (board) {
c90fe6bc
TH
500 c += scnprintf(buf + c, len - c, "/");
501 c += print_filtered(buf + c, len - c, board);
84e383b3 502 }
c90fe6bc
TH
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));
8881cdce
BH
509}
510
d39de28c
BH
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 */
79bae42d 518static int __init dmi_present(const u8 *buf)
1da177e4 519{
95be58df 520 u32 smbios_ver;
1855256c 521
79bae42d
BH
522 if (memcmp(buf, "_SM_", 4) == 0 &&
523 buf[5] < 32 && dmi_checksum(buf, buf[5])) {
fc430262 524 smbios_ver = get_unaligned_be16(buf + 6);
d7f96f97
IK
525 smbios_entry_point_size = buf[5];
526 memcpy(smbios_entry_point, buf, smbios_entry_point_size);
79bae42d
BH
527
528 /* Some BIOS report weird SMBIOS version, fix that up */
529 switch (smbios_ver) {
530 case 0x021F:
531 case 0x0221:
d1d8704c 532 pr_debug("SMBIOS version fixup (2.%d->2.%d)\n",
79bae42d
BH
533 smbios_ver & 0xFF, 3);
534 smbios_ver = 0x0203;
535 break;
536 case 0x0233:
d1d8704c 537 pr_debug("SMBIOS version fixup (2.%d->2.%d)\n", 51, 6);
79bae42d
BH
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)) {
5c1ac56b
JD
548 if (smbios_ver)
549 dmi_ver = smbios_ver;
550 else
551 dmi_ver = (buf[14] & 0xF0) << 4 | (buf[14] & 0x0F);
ff4319dc 552 dmi_ver <<= 8;
fc430262
AB
553 dmi_num = get_unaligned_le16(buf + 12);
554 dmi_len = get_unaligned_le16(buf + 6);
555 dmi_base = get_unaligned_le32(buf + 8);
61e032fa 556
8881cdce 557 if (dmi_walk_early(dmi_decode) == 0) {
79bae42d 558 if (smbios_ver) {
c2493045 559 pr_info("SMBIOS %d.%d present.\n",
ff4319dc 560 dmi_ver >> 16, (dmi_ver >> 8) & 0xFF);
79bae42d 561 } else {
d7f96f97
IK
562 smbios_entry_point_size = 15;
563 memcpy(smbios_entry_point, buf,
564 smbios_entry_point_size);
9f9c9cbb 565 pr_info("Legacy DMI %d.%d present.\n",
ff4319dc 566 dmi_ver >> 16, (dmi_ver >> 8) & 0xFF);
9f9c9cbb 567 }
c90fe6bc 568 dmi_format_ids(dmi_ids_string, sizeof(dmi_ids_string));
d4af49f8 569 pr_info("DMI: %s\n", dmi_ids_string);
3ed3bce8 570 return 0;
8881cdce 571 }
3ed3bce8 572 }
61e032fa 573
a40e7cf8 574 return 1;
9f9c9cbb
ZD
575}
576
fc430262
AB
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 */
581static 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])) {
d1d8704c 585 dmi_ver = get_unaligned_be32(buf + 6) & 0xFFFFFF;
bfbaafae 586 dmi_num = 0; /* No longer specified */
fc430262
AB
587 dmi_len = get_unaligned_le32(buf + 12);
588 dmi_base = get_unaligned_le64(buf + 16);
d7f96f97
IK
589 smbios_entry_point_size = buf[6];
590 memcpy(smbios_entry_point, buf, smbios_entry_point_size);
fc430262 591
fc430262 592 if (dmi_walk_early(dmi_decode) == 0) {
95be58df
IK
593 pr_info("SMBIOS %d.%d.%d present.\n",
594 dmi_ver >> 16, (dmi_ver >> 8) & 0xFF,
595 dmi_ver & 0xFF);
fc430262 596 dmi_format_ids(dmi_ids_string, sizeof(dmi_ids_string));
d4af49f8 597 pr_info("DMI: %s\n", dmi_ids_string);
fc430262
AB
598 return 0;
599 }
600 }
601 return 1;
602}
603
3ed3bce8
MD
604void __init dmi_scan_machine(void)
605{
606 char __iomem *p, *q;
79bae42d 607 char buf[32];
3ed3bce8 608
83e68189 609 if (efi_enabled(EFI_CONFIG_TABLES)) {
fc430262
AB
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 }
b2c99e3c 635 if (efi.smbios == EFI_INVALID_TABLE_ADDR)
9a22b6e7 636 goto error;
3ed3bce8 637
4f5c791a
LP
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 */
cf074402 642 p = dmi_early_remap(efi.smbios, 32);
3ed3bce8 643 if (p == NULL)
9a22b6e7 644 goto error;
79bae42d 645 memcpy_fromio(buf, p, 32);
cf074402 646 dmi_early_unmap(p, 32);
79bae42d
BH
647
648 if (!dmi_present(buf)) {
4f5c791a 649 dmi_available = 1;
9a22b6e7 650 goto out;
4f5c791a 651 }
cf074402
AB
652 } else if (IS_ENABLED(CONFIG_DMI_SCAN_MACHINE_NON_EFI_FALLBACK)) {
653 p = dmi_early_remap(0xF0000, 0x10000);
3ed3bce8 654 if (p == NULL)
9a22b6e7 655 goto error;
3ed3bce8 656
c9aba143
JD
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
d39de28c
BH
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 */
79bae42d 679 memset(buf, 0, 16);
3ed3bce8 680 for (q = p; q < p + 0x10000; q += 16) {
79bae42d 681 memcpy_fromio(buf + 16, q, 16);
c9aba143 682 if (!dmi_present(buf)) {
4f5c791a 683 dmi_available = 1;
cf074402 684 dmi_early_unmap(p, 0x10000);
9a22b6e7 685 goto out;
4f5c791a 686 }
79bae42d 687 memcpy(buf, buf + 16, 16);
61e032fa 688 }
cf074402 689 dmi_early_unmap(p, 0x10000);
61e032fa 690 }
9a22b6e7 691 error:
02d9c47f 692 pr_info("DMI not present or invalid.\n");
9a22b6e7
IM
693 out:
694 dmi_initialized = 1;
1da177e4
LT
695}
696
d7f96f97
IK
697static 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
705static BIN_ATTR(smbios_entry_point, S_IRUSR, raw_table_read, NULL, 0);
706static BIN_ATTR(DMI, S_IRUSR, raw_table_read, NULL, 0);
707
708static 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}
760subsys_initcall(dmi_init);
761
98e5e1bf
TH
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 */
770void __init dmi_set_dump_stack_arch_desc(void)
771{
772 dump_stack_set_arch_desc("%s", dmi_ids_string);
773}
774
d7b1956f
RW
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 */
779static 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)
75757507 788 break;
5017b285
JN
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
d7b1956f
RW
798 /* No match */
799 return false;
800 }
801 return true;
802}
803
75757507
DT
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 */
808static bool dmi_is_end_of_table(const struct dmi_system_id *dmi)
809{
810 return dmi->matches[0].slot == DMI_NONE;
811}
812
1da177e4
LT
813/**
814 * dmi_check_system - check system DMI data
815 * @list: array of dmi_system_id structures to match against
b0ef371e
RD
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.
1da177e4
LT
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
b0ef371e 824 * each successful match. Returns the number of matches.
1da177e4 825 */
1855256c 826int dmi_check_system(const struct dmi_system_id *list)
1da177e4 827{
d7b1956f
RW
828 int count = 0;
829 const struct dmi_system_id *d;
830
75757507 831 for (d = list; !dmi_is_end_of_table(d); d++)
d7b1956f
RW
832 if (dmi_matches(d)) {
833 count++;
834 if (d->callback && d->callback(d))
835 break;
1da177e4 836 }
1da177e4
LT
837
838 return count;
839}
1da177e4
LT
840EXPORT_SYMBOL(dmi_check_system);
841
d7b1956f
RW
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 */
854const struct dmi_system_id *dmi_first_match(const struct dmi_system_id *list)
855{
856 const struct dmi_system_id *d;
857
75757507 858 for (d = list; !dmi_is_end_of_table(d); d++)
d7b1956f
RW
859 if (dmi_matches(d))
860 return d;
861
862 return NULL;
863}
864EXPORT_SYMBOL(dmi_first_match);
865
1da177e4
LT
866/**
867 * dmi_get_system_info - return DMI data value
b0ef371e 868 * @field: data index (see enum dmi_field)
1da177e4
LT
869 *
870 * Returns one DMI data value, can be used to perform
871 * complex DMI data checks.
872 */
1855256c 873const char *dmi_get_system_info(int field)
1da177e4
LT
874{
875 return dmi_ident[field];
876}
e70c9d5e 877EXPORT_SYMBOL(dmi_get_system_info);
ebad6a42 878
fd8cd7e1 879/**
c2bacfc4
RD
880 * dmi_name_in_serial - Check if string is in the DMI product serial information
881 * @str: string to check for
fd8cd7e1
AK
882 */
883int 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}
a1bae672
AK
890
891/**
66e13e66 892 * dmi_name_in_vendors - Check if string is in the DMI system or board vendor name
02d9c47f 893 * @str: Case sensitive Name
a1bae672 894 */
1855256c 895int dmi_name_in_vendors(const char *str)
a1bae672 896{
66e13e66 897 static int fields[] = { DMI_SYS_VENDOR, DMI_BOARD_VENDOR, DMI_NONE };
a1bae672
AK
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}
906EXPORT_SYMBOL(dmi_name_in_vendors);
907
ebad6a42
AP
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
b0ef371e 911 * @name: device name string or %NULL to match all
ebad6a42
AP
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
bfab8b48 915 * found with a matching @type and @name, a pointer to its device
ebad6a42 916 * structure is returned. Otherwise, %NULL is returned.
b0ef371e 917 * A new search is initiated by passing %NULL as the @from argument.
ebad6a42
AP
918 * If @from is not %NULL, searches continue from next device.
919 */
02d9c47f 920const struct dmi_device *dmi_find_device(int type, const char *name,
1855256c 921 const struct dmi_device *from)
ebad6a42 922{
1855256c
JG
923 const struct list_head *head = from ? &from->list : &dmi_devices;
924 struct list_head *d;
ebad6a42 925
02d9c47f 926 for (d = head->next; d != &dmi_devices; d = d->next) {
1855256c
JG
927 const struct dmi_device *dev =
928 list_entry(d, struct dmi_device, list);
ebad6a42
AP
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}
937EXPORT_SYMBOL(dmi_find_device);
f083a329
AK
938
939/**
3e5cd1f2
TH
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
f083a329 945 *
3e5cd1f2
TH
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.
f083a329 956 */
3e5cd1f2 957bool dmi_get_date(int field, int *yearp, int *monthp, int *dayp)
f083a329 958{
3e5cd1f2
TH
959 int year = 0, month = 0, day = 0;
960 bool exists;
961 const char *s, *y;
02c24fa8 962 char *e;
f083a329 963
3e5cd1f2
TH
964 s = dmi_get_system_info(field);
965 exists = s;
966 if (!exists)
967 goto out;
f083a329 968
3e5cd1f2
TH
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 */
f083a329
AK
982 year += 1900;
983 if (year < 1996) /* no dates < spec 1.0 */
984 year += 100;
985 }
3e5cd1f2
TH
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 }
f083a329 995
3e5cd1f2
TH
996 s = e + 1;
997 day = simple_strtoul(s, &e, 10);
998 if (s == y || s == e || *e != '/' || day > 31)
999 day = 0;
1000out:
1001 if (yearp)
1002 *yearp = year;
1003 if (monthp)
1004 *monthp = month;
1005 if (dayp)
1006 *dayp = day;
1007 return exists;
f083a329 1008}
3e5cd1f2 1009EXPORT_SYMBOL(dmi_get_date);
7fce084a
JD
1010
1011/**
1012 * dmi_walk - Walk the DMI table and get called back for every record
1013 * @decode: Callback function
e7a19c56 1014 * @private_data: Private data to be passed to the callback function
7fce084a 1015 *
c9268200
AL
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.
7fce084a 1018 */
e7a19c56
JD
1019int dmi_walk(void (*decode)(const struct dmi_header *, void *),
1020 void *private_data)
7fce084a
JD
1021{
1022 u8 *buf;
1023
1024 if (!dmi_available)
c9268200 1025 return -ENXIO;
7fce084a 1026
cf074402 1027 buf = dmi_remap(dmi_base, dmi_len);
7fce084a 1028 if (buf == NULL)
c9268200 1029 return -ENOMEM;
7fce084a 1030
eb4c5ea5 1031 dmi_decode_table(buf, decode, private_data);
7fce084a 1032
cf074402 1033 dmi_unmap(buf);
7fce084a
JD
1034 return 0;
1035}
1036EXPORT_SYMBOL_GPL(dmi_walk);
d61c72e5
JS
1037
1038/**
1039 * dmi_match - compare a string to the dmi field (if exists)
c2bacfc4
RD
1040 * @f: DMI field identifier
1041 * @str: string to compare the DMI field to
d61c72e5
JS
1042 *
1043 * Returns true if the requested field equals to the str (including NULL).
1044 */
1045bool 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}
1054EXPORT_SYMBOL_GPL(dmi_match);
dd6dad42
CG
1055
1056void 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}
1071EXPORT_SYMBOL_GPL(dmi_memdev_name);