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