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