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