DMI: Parse memory device (type 17) in SMBIOS
[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>
1da177e4 11
cb5dd7c1
PJ
12/*
13 * DMI stands for "Desktop Management Interface". It is part
14 * of and an antecedent to, SMBIOS, which stands for System
15 * Management BIOS. See further: http://www.dmtf.org/standards
16 */
ffbbb96d 17static const char dmi_empty_string[] = " ";
79da4721 18
f1d8e614 19static u16 __initdata dmi_ver;
9a22b6e7
IM
20/*
21 * Catch too early calls to dmi_check_system():
22 */
23static int dmi_initialized;
24
c90fe6bc
TH
25/* DMI system identification string used during boot */
26static char dmi_ids_string[128] __initdata;
27
dd6dad42
CG
28static struct dmi_memdev_info {
29 const char *device;
30 const char *bank;
31 u16 handle;
32} *dmi_memdev;
33static int dmi_memdev_nr;
34
f3069ae9 35static const char * __init dmi_string_nosave(const struct dmi_header *dm, u8 s)
1da177e4 36{
1855256c 37 const u8 *bp = ((u8 *) dm) + dm->length;
1249c513 38
c3c7120d 39 if (s) {
1da177e4 40 s--;
c3c7120d
AP
41 while (s > 0 && *bp) {
42 bp += strlen(bp) + 1;
43 s--;
44 }
45
46 if (*bp != 0) {
79da4721
PW
47 size_t len = strlen(bp)+1;
48 size_t cmp_len = len > 8 ? 8 : len;
49
50 if (!memcmp(bp, dmi_empty_string, cmp_len))
51 return dmi_empty_string;
f3069ae9 52 return bp;
c3c7120d 53 }
4f705ae3 54 }
c3c7120d 55
f3069ae9
JD
56 return "";
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 */
7fce084a 80static void dmi_table(u8 *buf, int len, int num,
e7a19c56
JD
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 /*
4f705ae3
BH
88 * Stop when we see all the items the table claimed to have
89 * OR we run off the end of the table (also happens)
90 */
1249c513 91 while ((i < num) && (data - buf + sizeof(struct dmi_header)) <= len) {
1855256c
JG
92 const struct dmi_header *dm = (const struct dmi_header *)data;
93
1da177e4 94 /*
8638545c
AC
95 * We want to know the total length (formatted area and
96 * strings) before decoding to make sure we won't run off the
97 * table in dmi_decode or dmi_string
1da177e4 98 */
1249c513
AP
99 data += dm->length;
100 while ((data - buf < len - 1) && (data[0] || data[1]))
1da177e4 101 data++;
1249c513 102 if (data - buf < len - 1)
e7a19c56 103 decode(dm, private_data);
1249c513 104 data += 2;
1da177e4
LT
105 i++;
106 }
7fce084a
JD
107}
108
109static u32 dmi_base;
110static u16 dmi_len;
111static u16 dmi_num;
112
e7a19c56
JD
113static int __init dmi_walk_early(void (*decode)(const struct dmi_header *,
114 void *))
7fce084a
JD
115{
116 u8 *buf;
117
118 buf = dmi_ioremap(dmi_base, dmi_len);
119 if (buf == NULL)
120 return -1;
121
e7a19c56 122 dmi_table(buf, dmi_len, dmi_num, decode, NULL);
7fce084a 123
d114a333
TL
124 add_device_randomness(buf, dmi_len);
125
7fce084a 126 dmi_iounmap(buf, dmi_len);
1da177e4
LT
127 return 0;
128}
129
9f9c9cbb 130static int __init dmi_checksum(const u8 *buf, u8 len)
1da177e4 131{
1249c513 132 u8 sum = 0;
1da177e4 133 int a;
4f705ae3 134
9f9c9cbb 135 for (a = 0; a < len; a++)
1249c513
AP
136 sum += buf[a];
137
138 return sum == 0;
1da177e4
LT
139}
140
ffbbb96d 141static const char *dmi_ident[DMI_STRING_MAX];
ebad6a42 142static LIST_HEAD(dmi_devices);
4f5c791a 143int dmi_available;
1da177e4
LT
144
145/*
146 * Save a DMI string
147 */
02d9c47f
JD
148static void __init dmi_save_ident(const struct dmi_header *dm, int slot,
149 int string)
1da177e4 150{
02d9c47f 151 const char *d = (const char *) dm;
ffbbb96d 152 const char *p;
1249c513 153
1da177e4
LT
154 if (dmi_ident[slot])
155 return;
1249c513 156
c3c7120d
AP
157 p = dmi_string(dm, d[string]);
158 if (p == NULL)
159 return;
160
161 dmi_ident[slot] = p;
1da177e4
LT
162}
163
02d9c47f
JD
164static void __init dmi_save_uuid(const struct dmi_header *dm, int slot,
165 int index)
4f5c791a 166{
02d9c47f 167 const u8 *d = (u8 *) dm + index;
4f5c791a
LP
168 char *s;
169 int is_ff = 1, is_00 = 1, i;
170
171 if (dmi_ident[slot])
172 return;
173
174 for (i = 0; i < 16 && (is_ff || is_00); i++) {
f1d8e614
ZD
175 if (d[i] != 0x00)
176 is_00 = 0;
177 if (d[i] != 0xFF)
178 is_ff = 0;
4f5c791a
LP
179 }
180
181 if (is_ff || is_00)
182 return;
183
184 s = dmi_alloc(16*2+4+1);
185 if (!s)
186 return;
187
f1d8e614
ZD
188 /*
189 * As of version 2.6 of the SMBIOS specification, the first 3 fields of
190 * the UUID are supposed to be little-endian encoded. The specification
191 * says that this is the defacto standard.
192 */
193 if (dmi_ver >= 0x0206)
194 sprintf(s, "%pUL", d);
195 else
196 sprintf(s, "%pUB", d);
4f5c791a 197
02d9c47f 198 dmi_ident[slot] = s;
4f5c791a
LP
199}
200
02d9c47f
JD
201static void __init dmi_save_type(const struct dmi_header *dm, int slot,
202 int index)
4f5c791a 203{
02d9c47f 204 const u8 *d = (u8 *) dm + index;
4f5c791a
LP
205 char *s;
206
207 if (dmi_ident[slot])
208 return;
209
210 s = dmi_alloc(4);
211 if (!s)
212 return;
213
214 sprintf(s, "%u", *d & 0x7F);
215 dmi_ident[slot] = s;
216}
217
f3069ae9
JD
218static void __init dmi_save_one_device(int type, const char *name)
219{
220 struct dmi_device *dev;
221
222 /* No duplicate device */
223 if (dmi_find_device(type, name, NULL))
224 return;
225
226 dev = dmi_alloc(sizeof(*dev) + strlen(name) + 1);
ae797449 227 if (!dev)
f3069ae9 228 return;
f3069ae9
JD
229
230 dev->type = type;
231 strcpy((char *)(dev + 1), name);
232 dev->name = (char *)(dev + 1);
233 dev->device_data = NULL;
234 list_add(&dev->list, &dmi_devices);
235}
236
1855256c 237static void __init dmi_save_devices(const struct dmi_header *dm)
ebad6a42
AP
238{
239 int i, count = (dm->length - sizeof(struct dmi_header)) / 2;
ebad6a42
AP
240
241 for (i = 0; i < count; i++) {
1855256c 242 const char *d = (char *)(dm + 1) + (i * 2);
ebad6a42
AP
243
244 /* Skip disabled device */
245 if ((*d & 0x80) == 0)
246 continue;
247
f3069ae9 248 dmi_save_one_device(*d & 0x7f, dmi_string_nosave(dm, *(d + 1)));
2e0c1f6c
SM
249 }
250}
251
1855256c 252static void __init dmi_save_oem_strings_devices(const struct dmi_header *dm)
2e0c1f6c
SM
253{
254 int i, count = *(u8 *)(dm + 1);
255 struct dmi_device *dev;
256
257 for (i = 1; i <= count; i++) {
ffbbb96d 258 const char *devname = dmi_string(dm, i);
79da4721 259
43fe105a 260 if (devname == dmi_empty_string)
79da4721 261 continue;
79da4721 262
2e0c1f6c 263 dev = dmi_alloc(sizeof(*dev));
ae797449 264 if (!dev)
2e0c1f6c 265 break;
2e0c1f6c
SM
266
267 dev->type = DMI_DEV_TYPE_OEM_STRING;
79da4721 268 dev->name = devname;
2e0c1f6c 269 dev->device_data = NULL;
ebad6a42
AP
270
271 list_add(&dev->list, &dmi_devices);
272 }
273}
274
1855256c 275static void __init dmi_save_ipmi_device(const struct dmi_header *dm)
ebad6a42
AP
276{
277 struct dmi_device *dev;
02d9c47f 278 void *data;
ebad6a42 279
e9928674 280 data = dmi_alloc(dm->length);
ae797449 281 if (data == NULL)
ebad6a42 282 return;
ebad6a42
AP
283
284 memcpy(data, dm, dm->length);
285
e9928674 286 dev = dmi_alloc(sizeof(*dev));
ae797449 287 if (!dev)
ebad6a42 288 return;
ebad6a42
AP
289
290 dev->type = DMI_DEV_TYPE_IPMI;
291 dev->name = "IPMI controller";
292 dev->device_data = data;
293
abd24df8 294 list_add_tail(&dev->list, &dmi_devices);
ebad6a42
AP
295}
296
911e1c9b
N
297static void __init dmi_save_dev_onboard(int instance, int segment, int bus,
298 int devfn, const char *name)
299{
300 struct dmi_dev_onboard *onboard_dev;
301
302 onboard_dev = dmi_alloc(sizeof(*onboard_dev) + strlen(name) + 1);
ae797449 303 if (!onboard_dev)
911e1c9b 304 return;
ae797449 305
911e1c9b
N
306 onboard_dev->instance = instance;
307 onboard_dev->segment = segment;
308 onboard_dev->bus = bus;
309 onboard_dev->devfn = devfn;
310
311 strcpy((char *)&onboard_dev[1], name);
312 onboard_dev->dev.type = DMI_DEV_TYPE_DEV_ONBOARD;
313 onboard_dev->dev.name = (char *)&onboard_dev[1];
314 onboard_dev->dev.device_data = onboard_dev;
315
316 list_add(&onboard_dev->dev.list, &dmi_devices);
317}
318
b4bd7d59
WVS
319static void __init dmi_save_extended_devices(const struct dmi_header *dm)
320{
02d9c47f 321 const u8 *d = (u8 *) dm + 5;
b4bd7d59
WVS
322
323 /* Skip disabled device */
324 if ((*d & 0x80) == 0)
325 return;
326
911e1c9b
N
327 dmi_save_dev_onboard(*(d+1), *(u16 *)(d+2), *(d+4), *(d+5),
328 dmi_string_nosave(dm, *(d-1)));
f3069ae9 329 dmi_save_one_device(*d & 0x7f, dmi_string_nosave(dm, *(d - 1)));
b4bd7d59
WVS
330}
331
dd6dad42
CG
332static void __init count_mem_devices(const struct dmi_header *dm, void *v)
333{
334 if (dm->type != DMI_ENTRY_MEM_DEVICE)
335 return;
336 dmi_memdev_nr++;
337}
338
339static void __init save_mem_devices(const struct dmi_header *dm, void *v)
340{
341 const char *d = (const char *)dm;
342 static int nr;
343
344 if (dm->type != DMI_ENTRY_MEM_DEVICE)
345 return;
346 if (nr >= dmi_memdev_nr) {
347 pr_warn(FW_BUG "Too many DIMM entries in SMBIOS table\n");
348 return;
349 }
350 dmi_memdev[nr].handle = dm->handle;
351 dmi_memdev[nr].device = dmi_string(dm, d[0x10]);
352 dmi_memdev[nr].bank = dmi_string(dm, d[0x11]);
353 nr++;
354}
355
356void __init dmi_memdev_walk(void)
357{
358 if (!dmi_available)
359 return;
360
361 if (dmi_walk_early(count_mem_devices) == 0 && dmi_memdev_nr) {
362 dmi_memdev = dmi_alloc(sizeof(*dmi_memdev) * dmi_memdev_nr);
363 if (dmi_memdev)
364 dmi_walk_early(save_mem_devices);
365 }
366}
367
1da177e4
LT
368/*
369 * Process a DMI table entry. Right now all we care about are the BIOS
370 * and machine entries. For 2.5 we should pull the smbus controller info
371 * out of here.
372 */
e7a19c56 373static void __init dmi_decode(const struct dmi_header *dm, void *dummy)
1da177e4 374{
02d9c47f 375 switch (dm->type) {
ebad6a42 376 case 0: /* BIOS Information */
1249c513 377 dmi_save_ident(dm, DMI_BIOS_VENDOR, 4);
1249c513 378 dmi_save_ident(dm, DMI_BIOS_VERSION, 5);
1249c513
AP
379 dmi_save_ident(dm, DMI_BIOS_DATE, 8);
380 break;
ebad6a42 381 case 1: /* System Information */
1249c513 382 dmi_save_ident(dm, DMI_SYS_VENDOR, 4);
1249c513 383 dmi_save_ident(dm, DMI_PRODUCT_NAME, 5);
1249c513 384 dmi_save_ident(dm, DMI_PRODUCT_VERSION, 6);
1249c513 385 dmi_save_ident(dm, DMI_PRODUCT_SERIAL, 7);
4f5c791a 386 dmi_save_uuid(dm, DMI_PRODUCT_UUID, 8);
1249c513 387 break;
ebad6a42 388 case 2: /* Base Board Information */
1249c513 389 dmi_save_ident(dm, DMI_BOARD_VENDOR, 4);
1249c513 390 dmi_save_ident(dm, DMI_BOARD_NAME, 5);
1249c513 391 dmi_save_ident(dm, DMI_BOARD_VERSION, 6);
4f5c791a
LP
392 dmi_save_ident(dm, DMI_BOARD_SERIAL, 7);
393 dmi_save_ident(dm, DMI_BOARD_ASSET_TAG, 8);
394 break;
395 case 3: /* Chassis Information */
396 dmi_save_ident(dm, DMI_CHASSIS_VENDOR, 4);
397 dmi_save_type(dm, DMI_CHASSIS_TYPE, 5);
398 dmi_save_ident(dm, DMI_CHASSIS_VERSION, 6);
399 dmi_save_ident(dm, DMI_CHASSIS_SERIAL, 7);
400 dmi_save_ident(dm, DMI_CHASSIS_ASSET_TAG, 8);
1249c513 401 break;
ebad6a42
AP
402 case 10: /* Onboard Devices Information */
403 dmi_save_devices(dm);
404 break;
2e0c1f6c
SM
405 case 11: /* OEM Strings */
406 dmi_save_oem_strings_devices(dm);
407 break;
ebad6a42
AP
408 case 38: /* IPMI Device Information */
409 dmi_save_ipmi_device(dm);
b4bd7d59
WVS
410 break;
411 case 41: /* Onboard Devices Extended Information */
412 dmi_save_extended_devices(dm);
1da177e4
LT
413 }
414}
415
c90fe6bc 416static int __init print_filtered(char *buf, size_t len, const char *info)
8881cdce 417{
c90fe6bc 418 int c = 0;
8881cdce
BH
419 const char *p;
420
421 if (!info)
c90fe6bc 422 return c;
8881cdce
BH
423
424 for (p = info; *p; p++)
425 if (isprint(*p))
c90fe6bc 426 c += scnprintf(buf + c, len - c, "%c", *p);
8881cdce 427 else
c90fe6bc
TH
428 c += scnprintf(buf + c, len - c, "\\x%02x", *p & 0xff);
429 return c;
8881cdce
BH
430}
431
c90fe6bc 432static void __init dmi_format_ids(char *buf, size_t len)
8881cdce 433{
c90fe6bc 434 int c = 0;
84e383b3
NC
435 const char *board; /* Board Name is optional */
436
c90fe6bc
TH
437 c += print_filtered(buf + c, len - c,
438 dmi_get_system_info(DMI_SYS_VENDOR));
439 c += scnprintf(buf + c, len - c, " ");
440 c += print_filtered(buf + c, len - c,
441 dmi_get_system_info(DMI_PRODUCT_NAME));
442
84e383b3
NC
443 board = dmi_get_system_info(DMI_BOARD_NAME);
444 if (board) {
c90fe6bc
TH
445 c += scnprintf(buf + c, len - c, "/");
446 c += print_filtered(buf + c, len - c, board);
84e383b3 447 }
c90fe6bc
TH
448 c += scnprintf(buf + c, len - c, ", BIOS ");
449 c += print_filtered(buf + c, len - c,
450 dmi_get_system_info(DMI_BIOS_VERSION));
451 c += scnprintf(buf + c, len - c, " ");
452 c += print_filtered(buf + c, len - c,
453 dmi_get_system_info(DMI_BIOS_DATE));
8881cdce
BH
454}
455
d39de28c
BH
456/*
457 * Check for DMI/SMBIOS headers in the system firmware image. Any
458 * SMBIOS header must start 16 bytes before the DMI header, so take a
459 * 32 byte buffer and check for DMI at offset 16 and SMBIOS at offset
460 * 0. If the DMI header is present, set dmi_ver accordingly (SMBIOS
461 * takes precedence) and return 0. Otherwise return 1.
462 */
79bae42d 463static int __init dmi_present(const u8 *buf)
1da177e4 464{
79bae42d 465 int smbios_ver;
1855256c 466
79bae42d
BH
467 if (memcmp(buf, "_SM_", 4) == 0 &&
468 buf[5] < 32 && dmi_checksum(buf, buf[5])) {
469 smbios_ver = (buf[6] << 8) + buf[7];
470
471 /* Some BIOS report weird SMBIOS version, fix that up */
472 switch (smbios_ver) {
473 case 0x021F:
474 case 0x0221:
475 pr_debug("SMBIOS version fixup(2.%d->2.%d)\n",
476 smbios_ver & 0xFF, 3);
477 smbios_ver = 0x0203;
478 break;
479 case 0x0233:
480 pr_debug("SMBIOS version fixup(2.%d->2.%d)\n", 51, 6);
481 smbios_ver = 0x0206;
482 break;
483 }
484 } else {
485 smbios_ver = 0;
486 }
487
488 buf += 16;
489
490 if (memcmp(buf, "_DMI_", 5) == 0 && dmi_checksum(buf, 15)) {
7fce084a
JD
491 dmi_num = (buf[13] << 8) | buf[12];
492 dmi_len = (buf[7] << 8) | buf[6];
493 dmi_base = (buf[11] << 24) | (buf[10] << 16) |
3ed3bce8 494 (buf[9] << 8) | buf[8];
61e032fa 495
8881cdce 496 if (dmi_walk_early(dmi_decode) == 0) {
79bae42d
BH
497 if (smbios_ver) {
498 dmi_ver = smbios_ver;
9f9c9cbb
ZD
499 pr_info("SMBIOS %d.%d present.\n",
500 dmi_ver >> 8, dmi_ver & 0xFF);
79bae42d 501 } else {
9f9c9cbb
ZD
502 dmi_ver = (buf[14] & 0xF0) << 4 |
503 (buf[14] & 0x0F);
504 pr_info("Legacy DMI %d.%d present.\n",
505 dmi_ver >> 8, dmi_ver & 0xFF);
506 }
c90fe6bc
TH
507 dmi_format_ids(dmi_ids_string, sizeof(dmi_ids_string));
508 printk(KERN_DEBUG "DMI: %s\n", dmi_ids_string);
3ed3bce8 509 return 0;
8881cdce 510 }
3ed3bce8 511 }
61e032fa 512
a40e7cf8 513 return 1;
9f9c9cbb
ZD
514}
515
3ed3bce8
MD
516void __init dmi_scan_machine(void)
517{
518 char __iomem *p, *q;
79bae42d 519 char buf[32];
3ed3bce8 520
83e68189 521 if (efi_enabled(EFI_CONFIG_TABLES)) {
b2c99e3c 522 if (efi.smbios == EFI_INVALID_TABLE_ADDR)
9a22b6e7 523 goto error;
3ed3bce8 524
4f5c791a
LP
525 /* This is called as a core_initcall() because it isn't
526 * needed during early boot. This also means we can
527 * iounmap the space when we're done with it.
528 */
b2c99e3c 529 p = dmi_ioremap(efi.smbios, 32);
3ed3bce8 530 if (p == NULL)
9a22b6e7 531 goto error;
79bae42d 532 memcpy_fromio(buf, p, 32);
23dd842c 533 dmi_iounmap(p, 32);
79bae42d
BH
534
535 if (!dmi_present(buf)) {
4f5c791a 536 dmi_available = 1;
9a22b6e7 537 goto out;
4f5c791a 538 }
02d9c47f 539 } else {
3ed3bce8
MD
540 p = dmi_ioremap(0xF0000, 0x10000);
541 if (p == NULL)
9a22b6e7 542 goto error;
3ed3bce8 543
d39de28c
BH
544 /*
545 * Iterate over all possible DMI header addresses q.
546 * Maintain the 32 bytes around q in buf. On the
547 * first iteration, substitute zero for the
548 * out-of-range bytes so there is no chance of falsely
549 * detecting an SMBIOS header.
550 */
79bae42d 551 memset(buf, 0, 16);
3ed3bce8 552 for (q = p; q < p + 0x10000; q += 16) {
79bae42d
BH
553 memcpy_fromio(buf + 16, q, 16);
554 if (!dmi_present(buf)) {
4f5c791a 555 dmi_available = 1;
0d64484f 556 dmi_iounmap(p, 0x10000);
9a22b6e7 557 goto out;
4f5c791a 558 }
79bae42d 559 memcpy(buf, buf + 16, 16);
61e032fa 560 }
3212bff3 561 dmi_iounmap(p, 0x10000);
61e032fa 562 }
9a22b6e7 563 error:
02d9c47f 564 pr_info("DMI not present or invalid.\n");
9a22b6e7
IM
565 out:
566 dmi_initialized = 1;
1da177e4
LT
567}
568
98e5e1bf
TH
569/**
570 * dmi_set_dump_stack_arch_desc - set arch description for dump_stack()
571 *
572 * Invoke dump_stack_set_arch_desc() with DMI system information so that
573 * DMI identifiers are printed out on task dumps. Arch boot code should
574 * call this function after dmi_scan_machine() if it wants to print out DMI
575 * identifiers on task dumps.
576 */
577void __init dmi_set_dump_stack_arch_desc(void)
578{
579 dump_stack_set_arch_desc("%s", dmi_ids_string);
580}
581
d7b1956f
RW
582/**
583 * dmi_matches - check if dmi_system_id structure matches system DMI data
584 * @dmi: pointer to the dmi_system_id structure to check
585 */
586static bool dmi_matches(const struct dmi_system_id *dmi)
587{
588 int i;
589
590 WARN(!dmi_initialized, KERN_ERR "dmi check: not initialized yet.\n");
591
592 for (i = 0; i < ARRAY_SIZE(dmi->matches); i++) {
593 int s = dmi->matches[i].slot;
594 if (s == DMI_NONE)
75757507 595 break;
5017b285
JN
596 if (dmi_ident[s]) {
597 if (!dmi->matches[i].exact_match &&
598 strstr(dmi_ident[s], dmi->matches[i].substr))
599 continue;
600 else if (dmi->matches[i].exact_match &&
601 !strcmp(dmi_ident[s], dmi->matches[i].substr))
602 continue;
603 }
604
d7b1956f
RW
605 /* No match */
606 return false;
607 }
608 return true;
609}
610
75757507
DT
611/**
612 * dmi_is_end_of_table - check for end-of-table marker
613 * @dmi: pointer to the dmi_system_id structure to check
614 */
615static bool dmi_is_end_of_table(const struct dmi_system_id *dmi)
616{
617 return dmi->matches[0].slot == DMI_NONE;
618}
619
1da177e4
LT
620/**
621 * dmi_check_system - check system DMI data
622 * @list: array of dmi_system_id structures to match against
b0ef371e
RD
623 * All non-null elements of the list must match
624 * their slot's (field index's) data (i.e., each
625 * list string must be a substring of the specified
626 * DMI slot's string data) to be considered a
627 * successful match.
1da177e4
LT
628 *
629 * Walk the blacklist table running matching functions until someone
630 * returns non zero or we hit the end. Callback function is called for
b0ef371e 631 * each successful match. Returns the number of matches.
1da177e4 632 */
1855256c 633int dmi_check_system(const struct dmi_system_id *list)
1da177e4 634{
d7b1956f
RW
635 int count = 0;
636 const struct dmi_system_id *d;
637
75757507 638 for (d = list; !dmi_is_end_of_table(d); d++)
d7b1956f
RW
639 if (dmi_matches(d)) {
640 count++;
641 if (d->callback && d->callback(d))
642 break;
1da177e4 643 }
1da177e4
LT
644
645 return count;
646}
1da177e4
LT
647EXPORT_SYMBOL(dmi_check_system);
648
d7b1956f
RW
649/**
650 * dmi_first_match - find dmi_system_id structure matching system DMI data
651 * @list: array of dmi_system_id structures to match against
652 * All non-null elements of the list must match
653 * their slot's (field index's) data (i.e., each
654 * list string must be a substring of the specified
655 * DMI slot's string data) to be considered a
656 * successful match.
657 *
658 * Walk the blacklist table until the first match is found. Return the
659 * pointer to the matching entry or NULL if there's no match.
660 */
661const struct dmi_system_id *dmi_first_match(const struct dmi_system_id *list)
662{
663 const struct dmi_system_id *d;
664
75757507 665 for (d = list; !dmi_is_end_of_table(d); d++)
d7b1956f
RW
666 if (dmi_matches(d))
667 return d;
668
669 return NULL;
670}
671EXPORT_SYMBOL(dmi_first_match);
672
1da177e4
LT
673/**
674 * dmi_get_system_info - return DMI data value
b0ef371e 675 * @field: data index (see enum dmi_field)
1da177e4
LT
676 *
677 * Returns one DMI data value, can be used to perform
678 * complex DMI data checks.
679 */
1855256c 680const char *dmi_get_system_info(int field)
1da177e4
LT
681{
682 return dmi_ident[field];
683}
e70c9d5e 684EXPORT_SYMBOL(dmi_get_system_info);
ebad6a42 685
fd8cd7e1 686/**
c2bacfc4
RD
687 * dmi_name_in_serial - Check if string is in the DMI product serial information
688 * @str: string to check for
fd8cd7e1
AK
689 */
690int dmi_name_in_serial(const char *str)
691{
692 int f = DMI_PRODUCT_SERIAL;
693 if (dmi_ident[f] && strstr(dmi_ident[f], str))
694 return 1;
695 return 0;
696}
a1bae672
AK
697
698/**
66e13e66 699 * dmi_name_in_vendors - Check if string is in the DMI system or board vendor name
02d9c47f 700 * @str: Case sensitive Name
a1bae672 701 */
1855256c 702int dmi_name_in_vendors(const char *str)
a1bae672 703{
66e13e66 704 static int fields[] = { DMI_SYS_VENDOR, DMI_BOARD_VENDOR, DMI_NONE };
a1bae672
AK
705 int i;
706 for (i = 0; fields[i] != DMI_NONE; i++) {
707 int f = fields[i];
708 if (dmi_ident[f] && strstr(dmi_ident[f], str))
709 return 1;
710 }
711 return 0;
712}
713EXPORT_SYMBOL(dmi_name_in_vendors);
714
ebad6a42
AP
715/**
716 * dmi_find_device - find onboard device by type/name
717 * @type: device type or %DMI_DEV_TYPE_ANY to match all device types
b0ef371e 718 * @name: device name string or %NULL to match all
ebad6a42
AP
719 * @from: previous device found in search, or %NULL for new search.
720 *
721 * Iterates through the list of known onboard devices. If a device is
722 * found with a matching @vendor and @device, a pointer to its device
723 * structure is returned. Otherwise, %NULL is returned.
b0ef371e 724 * A new search is initiated by passing %NULL as the @from argument.
ebad6a42
AP
725 * If @from is not %NULL, searches continue from next device.
726 */
02d9c47f 727const struct dmi_device *dmi_find_device(int type, const char *name,
1855256c 728 const struct dmi_device *from)
ebad6a42 729{
1855256c
JG
730 const struct list_head *head = from ? &from->list : &dmi_devices;
731 struct list_head *d;
ebad6a42 732
02d9c47f 733 for (d = head->next; d != &dmi_devices; d = d->next) {
1855256c
JG
734 const struct dmi_device *dev =
735 list_entry(d, struct dmi_device, list);
ebad6a42
AP
736
737 if (((type == DMI_DEV_TYPE_ANY) || (dev->type == type)) &&
738 ((name == NULL) || (strcmp(dev->name, name) == 0)))
739 return dev;
740 }
741
742 return NULL;
743}
744EXPORT_SYMBOL(dmi_find_device);
f083a329
AK
745
746/**
3e5cd1f2
TH
747 * dmi_get_date - parse a DMI date
748 * @field: data index (see enum dmi_field)
749 * @yearp: optional out parameter for the year
750 * @monthp: optional out parameter for the month
751 * @dayp: optional out parameter for the day
f083a329 752 *
3e5cd1f2
TH
753 * The date field is assumed to be in the form resembling
754 * [mm[/dd]]/yy[yy] and the result is stored in the out
755 * parameters any or all of which can be omitted.
756 *
757 * If the field doesn't exist, all out parameters are set to zero
758 * and false is returned. Otherwise, true is returned with any
759 * invalid part of date set to zero.
760 *
761 * On return, year, month and day are guaranteed to be in the
762 * range of [0,9999], [0,12] and [0,31] respectively.
f083a329 763 */
3e5cd1f2 764bool dmi_get_date(int field, int *yearp, int *monthp, int *dayp)
f083a329 765{
3e5cd1f2
TH
766 int year = 0, month = 0, day = 0;
767 bool exists;
768 const char *s, *y;
02c24fa8 769 char *e;
f083a329 770
3e5cd1f2
TH
771 s = dmi_get_system_info(field);
772 exists = s;
773 if (!exists)
774 goto out;
f083a329 775
3e5cd1f2
TH
776 /*
777 * Determine year first. We assume the date string resembles
778 * mm/dd/yy[yy] but the original code extracted only the year
779 * from the end. Keep the behavior in the spirit of no
780 * surprises.
781 */
782 y = strrchr(s, '/');
783 if (!y)
784 goto out;
785
786 y++;
787 year = simple_strtoul(y, &e, 10);
788 if (y != e && year < 100) { /* 2-digit year */
f083a329
AK
789 year += 1900;
790 if (year < 1996) /* no dates < spec 1.0 */
791 year += 100;
792 }
3e5cd1f2
TH
793 if (year > 9999) /* year should fit in %04d */
794 year = 0;
795
796 /* parse the mm and dd */
797 month = simple_strtoul(s, &e, 10);
798 if (s == e || *e != '/' || !month || month > 12) {
799 month = 0;
800 goto out;
801 }
f083a329 802
3e5cd1f2
TH
803 s = e + 1;
804 day = simple_strtoul(s, &e, 10);
805 if (s == y || s == e || *e != '/' || day > 31)
806 day = 0;
807out:
808 if (yearp)
809 *yearp = year;
810 if (monthp)
811 *monthp = month;
812 if (dayp)
813 *dayp = day;
814 return exists;
f083a329 815}
3e5cd1f2 816EXPORT_SYMBOL(dmi_get_date);
7fce084a
JD
817
818/**
819 * dmi_walk - Walk the DMI table and get called back for every record
820 * @decode: Callback function
e7a19c56 821 * @private_data: Private data to be passed to the callback function
7fce084a
JD
822 *
823 * Returns -1 when the DMI table can't be reached, 0 on success.
824 */
e7a19c56
JD
825int dmi_walk(void (*decode)(const struct dmi_header *, void *),
826 void *private_data)
7fce084a
JD
827{
828 u8 *buf;
829
830 if (!dmi_available)
831 return -1;
832
833 buf = ioremap(dmi_base, dmi_len);
834 if (buf == NULL)
835 return -1;
836
e7a19c56 837 dmi_table(buf, dmi_len, dmi_num, decode, private_data);
7fce084a
JD
838
839 iounmap(buf);
840 return 0;
841}
842EXPORT_SYMBOL_GPL(dmi_walk);
d61c72e5
JS
843
844/**
845 * dmi_match - compare a string to the dmi field (if exists)
c2bacfc4
RD
846 * @f: DMI field identifier
847 * @str: string to compare the DMI field to
d61c72e5
JS
848 *
849 * Returns true if the requested field equals to the str (including NULL).
850 */
851bool dmi_match(enum dmi_field f, const char *str)
852{
853 const char *info = dmi_get_system_info(f);
854
855 if (info == NULL || str == NULL)
856 return info == str;
857
858 return !strcmp(info, str);
859}
860EXPORT_SYMBOL_GPL(dmi_match);
dd6dad42
CG
861
862void dmi_memdev_name(u16 handle, const char **bank, const char **device)
863{
864 int n;
865
866 if (dmi_memdev == NULL)
867 return;
868
869 for (n = 0; n < dmi_memdev_nr; n++) {
870 if (handle == dmi_memdev[n].handle) {
871 *bank = dmi_memdev[n].bank;
872 *device = dmi_memdev[n].device;
873 break;
874 }
875 }
876}
877EXPORT_SYMBOL_GPL(dmi_memdev_name);