blob: 2eebd28b4c40af2789c32e0008f2b60006fc03ac [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#include <linux/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07002#include <linux/string.h>
3#include <linux/init.h>
4#include <linux/module.h>
Bjorn Helgaas8881cdc2010-10-27 15:32:59 -07005#include <linux/ctype.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006#include <linux/dmi.h>
Matt Domsch3ed3bce2006-03-26 01:37:03 -08007#include <linux/efi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <linux/bootmem.h>
Tony Luckd114a332012-07-20 13:15:20 -07009#include <linux/random.h>
Andi Kleenf2d3efe2006-03-25 16:30:22 +010010#include <asm/dmi.h>
Luck, Tony0841c042013-11-01 13:59:52 -070011#include <asm/unaligned.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012
Paul Jacksoncb5dd7c2008-05-14 08:15:16 -070013/*
14 * DMI stands for "Desktop Management Interface". It is part
15 * of and an antecedent to, SMBIOS, which stands for System
16 * Management BIOS. See further: http://www.dmtf.org/standards
17 */
Jean Delvareffbbb962013-09-11 14:24:09 -070018static const char dmi_empty_string[] = " ";
Parag Warudkar79da4722008-01-30 13:31:59 +010019
Zhenzhong Duanf1d8e612012-12-20 15:05:13 -080020static u16 __initdata dmi_ver;
Ingo Molnar9a22b6e2008-09-18 12:50:18 +020021/*
22 * Catch too early calls to dmi_check_system():
23 */
24static int dmi_initialized;
25
Tejun Heoc90fe6b2013-04-30 15:27:14 -070026/* DMI system identification string used during boot */
27static char dmi_ids_string[128] __initdata;
28
Chen, Gongdd6dad42013-10-18 14:29:25 -070029static struct dmi_memdev_info {
30 const char *device;
31 const char *bank;
32 u16 handle;
33} *dmi_memdev;
34static int dmi_memdev_nr;
35
Jean Delvaref3069ae2008-02-23 15:23:46 -080036static const char * __init dmi_string_nosave(const struct dmi_header *dm, u8 s)
Linus Torvalds1da177e2005-04-16 15:20:36 -070037{
Jeff Garzik18552562007-10-03 15:15:40 -040038 const u8 *bp = ((u8 *) dm) + dm->length;
Andrey Panin1249c512005-06-25 14:54:47 -070039
Andrey Paninc3c71202005-09-06 15:18:28 -070040 if (s) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 s--;
Andrey Paninc3c71202005-09-06 15:18:28 -070042 while (s > 0 && *bp) {
43 bp += strlen(bp) + 1;
44 s--;
45 }
46
47 if (*bp != 0) {
Parag Warudkar79da4722008-01-30 13:31:59 +010048 size_t len = strlen(bp)+1;
49 size_t cmp_len = len > 8 ? 8 : len;
50
51 if (!memcmp(bp, dmi_empty_string, cmp_len))
52 return dmi_empty_string;
Jean Delvaref3069ae2008-02-23 15:23:46 -080053 return bp;
Andrey Paninc3c71202005-09-06 15:18:28 -070054 }
Bjorn Helgaas4f705ae2006-04-03 17:09:22 -070055 }
Andrey Paninc3c71202005-09-06 15:18:28 -070056
Jean Delvaref3069ae2008-02-23 15:23:46 -080057 return "";
58}
59
Jean Delvareffbbb962013-09-11 14:24:09 -070060static const char * __init dmi_string(const struct dmi_header *dm, u8 s)
Jean Delvaref3069ae2008-02-23 15:23:46 -080061{
62 const char *bp = dmi_string_nosave(dm, s);
63 char *str;
64 size_t len;
65
66 if (bp == dmi_empty_string)
67 return dmi_empty_string;
68
69 len = strlen(bp) + 1;
70 str = dmi_alloc(len);
71 if (str != NULL)
72 strcpy(str, bp);
Jean Delvaref3069ae2008-02-23 15:23:46 -080073
Andrey Paninc3c71202005-09-06 15:18:28 -070074 return str;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075}
76
77/*
78 * We have to be cautious here. We have seen BIOSes with DMI pointers
79 * pointing to completely the wrong place for example
80 */
Ivan Khoronzhuk6d9ff472015-02-18 13:33:19 +020081static void dmi_table(u8 *buf, u32 len, int num,
Jean Delvaree7a19c562009-03-30 21:46:44 +020082 void (*decode)(const struct dmi_header *, void *),
83 void *private_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070084{
Jean Delvare7fce0842007-11-03 17:29:20 +010085 u8 *data = buf;
Andrey Panin1249c512005-06-25 14:54:47 -070086 int i = 0;
Bjorn Helgaas4f705ae2006-04-03 17:09:22 -070087
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 /*
Jean Delvarebfbaafa2015-03-20 09:59:47 +010089 * Stop when we have seen all the items the table claimed to have
90 * (SMBIOS < 3.0 only) OR we reach an end-of-table marker OR we run
91 * off the end of the table (should never happen but sometimes does
92 * on bogus implementations.)
Bjorn Helgaas4f705ae2006-04-03 17:09:22 -070093 */
Jean Delvarebfbaafa2015-03-20 09:59:47 +010094 while ((!num || i < num) &&
95 (data - buf + sizeof(struct dmi_header)) <= len) {
Jeff Garzik18552562007-10-03 15:15:40 -040096 const struct dmi_header *dm = (const struct dmi_header *)data;
97
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 /*
Alan Cox86385452008-11-07 16:03:46 +000099 * We want to know the total length (formatted area and
100 * strings) before decoding to make sure we won't run off the
101 * table in dmi_decode or dmi_string
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 */
Andrey Panin1249c512005-06-25 14:54:47 -0700103 data += dm->length;
104 while ((data - buf < len - 1) && (data[0] || data[1]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 data++;
Andrey Panin1249c512005-06-25 14:54:47 -0700106 if (data - buf < len - 1)
Jean Delvaree7a19c562009-03-30 21:46:44 +0200107 decode(dm, private_data);
Ivan Khoronzhukce204e92015-02-18 15:51:41 +0200108
109 /*
110 * 7.45 End-of-Table (Type 127) [SMBIOS reference spec v3.0.0]
111 */
112 if (dm->type == DMI_ENTRY_END_OF_TABLE)
113 break;
114
Andrey Panin1249c512005-06-25 14:54:47 -0700115 data += 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 i++;
117 }
Jean Delvare7fce0842007-11-03 17:29:20 +0100118}
119
Ard Biesheuvelfc430262014-10-14 16:41:27 +0200120static phys_addr_t dmi_base;
Ivan Khoronzhuk6d9ff472015-02-18 13:33:19 +0200121static u32 dmi_len;
Jean Delvare7fce0842007-11-03 17:29:20 +0100122static u16 dmi_num;
123
Jean Delvaree7a19c562009-03-30 21:46:44 +0200124static int __init dmi_walk_early(void (*decode)(const struct dmi_header *,
125 void *))
Jean Delvare7fce0842007-11-03 17:29:20 +0100126{
127 u8 *buf;
128
Ard Biesheuvelcf074402014-01-23 15:54:39 -0800129 buf = dmi_early_remap(dmi_base, dmi_len);
Jean Delvare7fce0842007-11-03 17:29:20 +0100130 if (buf == NULL)
131 return -1;
132
Jean Delvaree7a19c562009-03-30 21:46:44 +0200133 dmi_table(buf, dmi_len, dmi_num, decode, NULL);
Jean Delvare7fce0842007-11-03 17:29:20 +0100134
Tony Luckd114a332012-07-20 13:15:20 -0700135 add_device_randomness(buf, dmi_len);
136
Ard Biesheuvelcf074402014-01-23 15:54:39 -0800137 dmi_early_unmap(buf, dmi_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 return 0;
139}
140
Zhenzhong Duan9f9c9cbb2012-12-20 15:05:14 -0800141static int __init dmi_checksum(const u8 *buf, u8 len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142{
Andrey Panin1249c512005-06-25 14:54:47 -0700143 u8 sum = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 int a;
Bjorn Helgaas4f705ae2006-04-03 17:09:22 -0700145
Zhenzhong Duan9f9c9cbb2012-12-20 15:05:14 -0800146 for (a = 0; a < len; a++)
Andrey Panin1249c512005-06-25 14:54:47 -0700147 sum += buf[a];
148
149 return sum == 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150}
151
Jean Delvareffbbb962013-09-11 14:24:09 -0700152static const char *dmi_ident[DMI_STRING_MAX];
Andrey Paninebad6a42005-09-06 15:18:29 -0700153static LIST_HEAD(dmi_devices);
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200154int dmi_available;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
156/*
157 * Save a DMI string
158 */
Jean Delvare02d9c472013-09-11 14:24:08 -0700159static void __init dmi_save_ident(const struct dmi_header *dm, int slot,
160 int string)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161{
Jean Delvare02d9c472013-09-11 14:24:08 -0700162 const char *d = (const char *) dm;
Jean Delvareffbbb962013-09-11 14:24:09 -0700163 const char *p;
Andrey Panin1249c512005-06-25 14:54:47 -0700164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 if (dmi_ident[slot])
166 return;
Andrey Panin1249c512005-06-25 14:54:47 -0700167
Andrey Paninc3c71202005-09-06 15:18:28 -0700168 p = dmi_string(dm, d[string]);
169 if (p == NULL)
170 return;
171
172 dmi_ident[slot] = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173}
174
Jean Delvare02d9c472013-09-11 14:24:08 -0700175static void __init dmi_save_uuid(const struct dmi_header *dm, int slot,
176 int index)
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200177{
Jean Delvare02d9c472013-09-11 14:24:08 -0700178 const u8 *d = (u8 *) dm + index;
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200179 char *s;
180 int is_ff = 1, is_00 = 1, i;
181
182 if (dmi_ident[slot])
183 return;
184
185 for (i = 0; i < 16 && (is_ff || is_00); i++) {
Zhenzhong Duanf1d8e612012-12-20 15:05:13 -0800186 if (d[i] != 0x00)
187 is_00 = 0;
188 if (d[i] != 0xFF)
189 is_ff = 0;
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200190 }
191
192 if (is_ff || is_00)
193 return;
194
195 s = dmi_alloc(16*2+4+1);
196 if (!s)
197 return;
198
Zhenzhong Duanf1d8e612012-12-20 15:05:13 -0800199 /*
200 * As of version 2.6 of the SMBIOS specification, the first 3 fields of
201 * the UUID are supposed to be little-endian encoded. The specification
202 * says that this is the defacto standard.
203 */
204 if (dmi_ver >= 0x0206)
205 sprintf(s, "%pUL", d);
206 else
207 sprintf(s, "%pUB", d);
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200208
Jean Delvare02d9c472013-09-11 14:24:08 -0700209 dmi_ident[slot] = s;
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200210}
211
Jean Delvare02d9c472013-09-11 14:24:08 -0700212static void __init dmi_save_type(const struct dmi_header *dm, int slot,
213 int index)
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200214{
Jean Delvare02d9c472013-09-11 14:24:08 -0700215 const u8 *d = (u8 *) dm + index;
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200216 char *s;
217
218 if (dmi_ident[slot])
219 return;
220
221 s = dmi_alloc(4);
222 if (!s)
223 return;
224
225 sprintf(s, "%u", *d & 0x7F);
226 dmi_ident[slot] = s;
227}
228
Jean Delvaref3069ae2008-02-23 15:23:46 -0800229static void __init dmi_save_one_device(int type, const char *name)
230{
231 struct dmi_device *dev;
232
233 /* No duplicate device */
234 if (dmi_find_device(type, name, NULL))
235 return;
236
237 dev = dmi_alloc(sizeof(*dev) + strlen(name) + 1);
Jean Delvareae797442013-09-11 14:24:10 -0700238 if (!dev)
Jean Delvaref3069ae2008-02-23 15:23:46 -0800239 return;
Jean Delvaref3069ae2008-02-23 15:23:46 -0800240
241 dev->type = type;
242 strcpy((char *)(dev + 1), name);
243 dev->name = (char *)(dev + 1);
244 dev->device_data = NULL;
245 list_add(&dev->list, &dmi_devices);
246}
247
Jeff Garzik18552562007-10-03 15:15:40 -0400248static void __init dmi_save_devices(const struct dmi_header *dm)
Andrey Paninebad6a42005-09-06 15:18:29 -0700249{
250 int i, count = (dm->length - sizeof(struct dmi_header)) / 2;
Andrey Paninebad6a42005-09-06 15:18:29 -0700251
252 for (i = 0; i < count; i++) {
Jeff Garzik18552562007-10-03 15:15:40 -0400253 const char *d = (char *)(dm + 1) + (i * 2);
Andrey Paninebad6a42005-09-06 15:18:29 -0700254
255 /* Skip disabled device */
256 if ((*d & 0x80) == 0)
257 continue;
258
Jean Delvaref3069ae2008-02-23 15:23:46 -0800259 dmi_save_one_device(*d & 0x7f, dmi_string_nosave(dm, *(d + 1)));
Shem Multinymous2e0c1f62006-09-29 01:59:37 -0700260 }
261}
262
Jeff Garzik18552562007-10-03 15:15:40 -0400263static void __init dmi_save_oem_strings_devices(const struct dmi_header *dm)
Shem Multinymous2e0c1f62006-09-29 01:59:37 -0700264{
265 int i, count = *(u8 *)(dm + 1);
266 struct dmi_device *dev;
267
268 for (i = 1; i <= count; i++) {
Jean Delvareffbbb962013-09-11 14:24:09 -0700269 const char *devname = dmi_string(dm, i);
Parag Warudkar79da4722008-01-30 13:31:59 +0100270
Jean Delvare43fe1052008-02-23 15:23:55 -0800271 if (devname == dmi_empty_string)
Parag Warudkar79da4722008-01-30 13:31:59 +0100272 continue;
Parag Warudkar79da4722008-01-30 13:31:59 +0100273
Shem Multinymous2e0c1f62006-09-29 01:59:37 -0700274 dev = dmi_alloc(sizeof(*dev));
Jean Delvareae797442013-09-11 14:24:10 -0700275 if (!dev)
Shem Multinymous2e0c1f62006-09-29 01:59:37 -0700276 break;
Shem Multinymous2e0c1f62006-09-29 01:59:37 -0700277
278 dev->type = DMI_DEV_TYPE_OEM_STRING;
Parag Warudkar79da4722008-01-30 13:31:59 +0100279 dev->name = devname;
Shem Multinymous2e0c1f62006-09-29 01:59:37 -0700280 dev->device_data = NULL;
Andrey Paninebad6a42005-09-06 15:18:29 -0700281
282 list_add(&dev->list, &dmi_devices);
283 }
284}
285
Jeff Garzik18552562007-10-03 15:15:40 -0400286static void __init dmi_save_ipmi_device(const struct dmi_header *dm)
Andrey Paninebad6a42005-09-06 15:18:29 -0700287{
288 struct dmi_device *dev;
Jean Delvare02d9c472013-09-11 14:24:08 -0700289 void *data;
Andrey Paninebad6a42005-09-06 15:18:29 -0700290
Andi Kleene9928672006-01-11 22:43:33 +0100291 data = dmi_alloc(dm->length);
Jean Delvareae797442013-09-11 14:24:10 -0700292 if (data == NULL)
Andrey Paninebad6a42005-09-06 15:18:29 -0700293 return;
Andrey Paninebad6a42005-09-06 15:18:29 -0700294
295 memcpy(data, dm, dm->length);
296
Andi Kleene9928672006-01-11 22:43:33 +0100297 dev = dmi_alloc(sizeof(*dev));
Jean Delvareae797442013-09-11 14:24:10 -0700298 if (!dev)
Andrey Paninebad6a42005-09-06 15:18:29 -0700299 return;
Andrey Paninebad6a42005-09-06 15:18:29 -0700300
301 dev->type = DMI_DEV_TYPE_IPMI;
302 dev->name = "IPMI controller";
303 dev->device_data = data;
304
Carol Hebertabd24df2008-04-04 14:30:03 -0700305 list_add_tail(&dev->list, &dmi_devices);
Andrey Paninebad6a42005-09-06 15:18:29 -0700306}
307
Narendra K911e1c92010-07-26 05:56:50 -0500308static void __init dmi_save_dev_onboard(int instance, int segment, int bus,
309 int devfn, const char *name)
310{
311 struct dmi_dev_onboard *onboard_dev;
312
313 onboard_dev = dmi_alloc(sizeof(*onboard_dev) + strlen(name) + 1);
Jean Delvareae797442013-09-11 14:24:10 -0700314 if (!onboard_dev)
Narendra K911e1c92010-07-26 05:56:50 -0500315 return;
Jean Delvareae797442013-09-11 14:24:10 -0700316
Narendra K911e1c92010-07-26 05:56:50 -0500317 onboard_dev->instance = instance;
318 onboard_dev->segment = segment;
319 onboard_dev->bus = bus;
320 onboard_dev->devfn = devfn;
321
322 strcpy((char *)&onboard_dev[1], name);
323 onboard_dev->dev.type = DMI_DEV_TYPE_DEV_ONBOARD;
324 onboard_dev->dev.name = (char *)&onboard_dev[1];
325 onboard_dev->dev.device_data = onboard_dev;
326
327 list_add(&onboard_dev->dev.list, &dmi_devices);
328}
329
Wim Van Sebroeckb4bd7d52008-02-08 04:20:58 -0800330static void __init dmi_save_extended_devices(const struct dmi_header *dm)
331{
Jean Delvare02d9c472013-09-11 14:24:08 -0700332 const u8 *d = (u8 *) dm + 5;
Wim Van Sebroeckb4bd7d52008-02-08 04:20:58 -0800333
334 /* Skip disabled device */
335 if ((*d & 0x80) == 0)
336 return;
337
Narendra K911e1c92010-07-26 05:56:50 -0500338 dmi_save_dev_onboard(*(d+1), *(u16 *)(d+2), *(d+4), *(d+5),
339 dmi_string_nosave(dm, *(d-1)));
Jean Delvaref3069ae2008-02-23 15:23:46 -0800340 dmi_save_one_device(*d & 0x7f, dmi_string_nosave(dm, *(d - 1)));
Wim Van Sebroeckb4bd7d52008-02-08 04:20:58 -0800341}
342
Chen, Gongdd6dad42013-10-18 14:29:25 -0700343static void __init count_mem_devices(const struct dmi_header *dm, void *v)
344{
345 if (dm->type != DMI_ENTRY_MEM_DEVICE)
346 return;
347 dmi_memdev_nr++;
348}
349
350static void __init save_mem_devices(const struct dmi_header *dm, void *v)
351{
352 const char *d = (const char *)dm;
353 static int nr;
354
355 if (dm->type != DMI_ENTRY_MEM_DEVICE)
356 return;
357 if (nr >= dmi_memdev_nr) {
358 pr_warn(FW_BUG "Too many DIMM entries in SMBIOS table\n");
359 return;
360 }
Luck, Tony0841c042013-11-01 13:59:52 -0700361 dmi_memdev[nr].handle = get_unaligned(&dm->handle);
Chen, Gongdd6dad42013-10-18 14:29:25 -0700362 dmi_memdev[nr].device = dmi_string(dm, d[0x10]);
363 dmi_memdev[nr].bank = dmi_string(dm, d[0x11]);
364 nr++;
365}
366
367void __init dmi_memdev_walk(void)
368{
369 if (!dmi_available)
370 return;
371
372 if (dmi_walk_early(count_mem_devices) == 0 && dmi_memdev_nr) {
373 dmi_memdev = dmi_alloc(sizeof(*dmi_memdev) * dmi_memdev_nr);
374 if (dmi_memdev)
375 dmi_walk_early(save_mem_devices);
376 }
377}
378
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 * Process a DMI table entry. Right now all we care about are the BIOS
381 * and machine entries. For 2.5 we should pull the smbus controller info
382 * out of here.
383 */
Jean Delvaree7a19c562009-03-30 21:46:44 +0200384static void __init dmi_decode(const struct dmi_header *dm, void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385{
Jean Delvare02d9c472013-09-11 14:24:08 -0700386 switch (dm->type) {
Andrey Paninebad6a42005-09-06 15:18:29 -0700387 case 0: /* BIOS Information */
Andrey Panin1249c512005-06-25 14:54:47 -0700388 dmi_save_ident(dm, DMI_BIOS_VENDOR, 4);
Andrey Panin1249c512005-06-25 14:54:47 -0700389 dmi_save_ident(dm, DMI_BIOS_VERSION, 5);
Andrey Panin1249c512005-06-25 14:54:47 -0700390 dmi_save_ident(dm, DMI_BIOS_DATE, 8);
391 break;
Andrey Paninebad6a42005-09-06 15:18:29 -0700392 case 1: /* System Information */
Andrey Panin1249c512005-06-25 14:54:47 -0700393 dmi_save_ident(dm, DMI_SYS_VENDOR, 4);
Andrey Panin1249c512005-06-25 14:54:47 -0700394 dmi_save_ident(dm, DMI_PRODUCT_NAME, 5);
Andrey Panin1249c512005-06-25 14:54:47 -0700395 dmi_save_ident(dm, DMI_PRODUCT_VERSION, 6);
Andrey Panin1249c512005-06-25 14:54:47 -0700396 dmi_save_ident(dm, DMI_PRODUCT_SERIAL, 7);
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200397 dmi_save_uuid(dm, DMI_PRODUCT_UUID, 8);
Andrey Panin1249c512005-06-25 14:54:47 -0700398 break;
Andrey Paninebad6a42005-09-06 15:18:29 -0700399 case 2: /* Base Board Information */
Andrey Panin1249c512005-06-25 14:54:47 -0700400 dmi_save_ident(dm, DMI_BOARD_VENDOR, 4);
Andrey Panin1249c512005-06-25 14:54:47 -0700401 dmi_save_ident(dm, DMI_BOARD_NAME, 5);
Andrey Panin1249c512005-06-25 14:54:47 -0700402 dmi_save_ident(dm, DMI_BOARD_VERSION, 6);
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200403 dmi_save_ident(dm, DMI_BOARD_SERIAL, 7);
404 dmi_save_ident(dm, DMI_BOARD_ASSET_TAG, 8);
405 break;
406 case 3: /* Chassis Information */
407 dmi_save_ident(dm, DMI_CHASSIS_VENDOR, 4);
408 dmi_save_type(dm, DMI_CHASSIS_TYPE, 5);
409 dmi_save_ident(dm, DMI_CHASSIS_VERSION, 6);
410 dmi_save_ident(dm, DMI_CHASSIS_SERIAL, 7);
411 dmi_save_ident(dm, DMI_CHASSIS_ASSET_TAG, 8);
Andrey Panin1249c512005-06-25 14:54:47 -0700412 break;
Andrey Paninebad6a42005-09-06 15:18:29 -0700413 case 10: /* Onboard Devices Information */
414 dmi_save_devices(dm);
415 break;
Shem Multinymous2e0c1f62006-09-29 01:59:37 -0700416 case 11: /* OEM Strings */
417 dmi_save_oem_strings_devices(dm);
418 break;
Andrey Paninebad6a42005-09-06 15:18:29 -0700419 case 38: /* IPMI Device Information */
420 dmi_save_ipmi_device(dm);
Wim Van Sebroeckb4bd7d52008-02-08 04:20:58 -0800421 break;
422 case 41: /* Onboard Devices Extended Information */
423 dmi_save_extended_devices(dm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 }
425}
426
Tejun Heoc90fe6b2013-04-30 15:27:14 -0700427static int __init print_filtered(char *buf, size_t len, const char *info)
Bjorn Helgaas8881cdc2010-10-27 15:32:59 -0700428{
Tejun Heoc90fe6b2013-04-30 15:27:14 -0700429 int c = 0;
Bjorn Helgaas8881cdc2010-10-27 15:32:59 -0700430 const char *p;
431
432 if (!info)
Tejun Heoc90fe6b2013-04-30 15:27:14 -0700433 return c;
Bjorn Helgaas8881cdc2010-10-27 15:32:59 -0700434
435 for (p = info; *p; p++)
436 if (isprint(*p))
Tejun Heoc90fe6b2013-04-30 15:27:14 -0700437 c += scnprintf(buf + c, len - c, "%c", *p);
Bjorn Helgaas8881cdc2010-10-27 15:32:59 -0700438 else
Tejun Heoc90fe6b2013-04-30 15:27:14 -0700439 c += scnprintf(buf + c, len - c, "\\x%02x", *p & 0xff);
440 return c;
Bjorn Helgaas8881cdc2010-10-27 15:32:59 -0700441}
442
Tejun Heoc90fe6b2013-04-30 15:27:14 -0700443static void __init dmi_format_ids(char *buf, size_t len)
Bjorn Helgaas8881cdc2010-10-27 15:32:59 -0700444{
Tejun Heoc90fe6b2013-04-30 15:27:14 -0700445 int c = 0;
Naga Chumbalkar84e383b2011-02-14 22:47:17 +0000446 const char *board; /* Board Name is optional */
447
Tejun Heoc90fe6b2013-04-30 15:27:14 -0700448 c += print_filtered(buf + c, len - c,
449 dmi_get_system_info(DMI_SYS_VENDOR));
450 c += scnprintf(buf + c, len - c, " ");
451 c += print_filtered(buf + c, len - c,
452 dmi_get_system_info(DMI_PRODUCT_NAME));
453
Naga Chumbalkar84e383b2011-02-14 22:47:17 +0000454 board = dmi_get_system_info(DMI_BOARD_NAME);
455 if (board) {
Tejun Heoc90fe6b2013-04-30 15:27:14 -0700456 c += scnprintf(buf + c, len - c, "/");
457 c += print_filtered(buf + c, len - c, board);
Naga Chumbalkar84e383b2011-02-14 22:47:17 +0000458 }
Tejun Heoc90fe6b2013-04-30 15:27:14 -0700459 c += scnprintf(buf + c, len - c, ", BIOS ");
460 c += print_filtered(buf + c, len - c,
461 dmi_get_system_info(DMI_BIOS_VERSION));
462 c += scnprintf(buf + c, len - c, " ");
463 c += print_filtered(buf + c, len - c,
464 dmi_get_system_info(DMI_BIOS_DATE));
Bjorn Helgaas8881cdc2010-10-27 15:32:59 -0700465}
466
Ben Hutchingsd39de282013-07-31 13:53:30 -0700467/*
468 * Check for DMI/SMBIOS headers in the system firmware image. Any
469 * SMBIOS header must start 16 bytes before the DMI header, so take a
470 * 32 byte buffer and check for DMI at offset 16 and SMBIOS at offset
471 * 0. If the DMI header is present, set dmi_ver accordingly (SMBIOS
472 * takes precedence) and return 0. Otherwise return 1.
473 */
Ben Hutchings79bae422013-04-30 15:27:46 -0700474static int __init dmi_present(const u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475{
Ben Hutchings79bae422013-04-30 15:27:46 -0700476 int smbios_ver;
Jeff Garzik18552562007-10-03 15:15:40 -0400477
Ben Hutchings79bae422013-04-30 15:27:46 -0700478 if (memcmp(buf, "_SM_", 4) == 0 &&
479 buf[5] < 32 && dmi_checksum(buf, buf[5])) {
Ard Biesheuvelfc430262014-10-14 16:41:27 +0200480 smbios_ver = get_unaligned_be16(buf + 6);
Ben Hutchings79bae422013-04-30 15:27:46 -0700481
482 /* Some BIOS report weird SMBIOS version, fix that up */
483 switch (smbios_ver) {
484 case 0x021F:
485 case 0x0221:
486 pr_debug("SMBIOS version fixup(2.%d->2.%d)\n",
487 smbios_ver & 0xFF, 3);
488 smbios_ver = 0x0203;
489 break;
490 case 0x0233:
491 pr_debug("SMBIOS version fixup(2.%d->2.%d)\n", 51, 6);
492 smbios_ver = 0x0206;
493 break;
494 }
495 } else {
496 smbios_ver = 0;
497 }
498
499 buf += 16;
500
501 if (memcmp(buf, "_DMI_", 5) == 0 && dmi_checksum(buf, 15)) {
Ard Biesheuvelfc430262014-10-14 16:41:27 +0200502 dmi_num = get_unaligned_le16(buf + 12);
503 dmi_len = get_unaligned_le16(buf + 6);
504 dmi_base = get_unaligned_le32(buf + 8);
Matt Domsch3ed3bce2006-03-26 01:37:03 -0800505
Bjorn Helgaas8881cdc2010-10-27 15:32:59 -0700506 if (dmi_walk_early(dmi_decode) == 0) {
Ben Hutchings79bae422013-04-30 15:27:46 -0700507 if (smbios_ver) {
508 dmi_ver = smbios_ver;
Zhenzhong Duan9f9c9cbb2012-12-20 15:05:14 -0800509 pr_info("SMBIOS %d.%d present.\n",
510 dmi_ver >> 8, dmi_ver & 0xFF);
Ben Hutchings79bae422013-04-30 15:27:46 -0700511 } else {
Zhenzhong Duan9f9c9cbb2012-12-20 15:05:14 -0800512 dmi_ver = (buf[14] & 0xF0) << 4 |
513 (buf[14] & 0x0F);
514 pr_info("Legacy DMI %d.%d present.\n",
515 dmi_ver >> 8, dmi_ver & 0xFF);
516 }
Tejun Heoc90fe6b2013-04-30 15:27:14 -0700517 dmi_format_ids(dmi_ids_string, sizeof(dmi_ids_string));
518 printk(KERN_DEBUG "DMI: %s\n", dmi_ids_string);
Matt Domsch3ed3bce2006-03-26 01:37:03 -0800519 return 0;
Bjorn Helgaas8881cdc2010-10-27 15:32:59 -0700520 }
Matt Domsch3ed3bce2006-03-26 01:37:03 -0800521 }
Matt Domsch3ed3bce2006-03-26 01:37:03 -0800522
Ben Hutchingsa40e7cf2013-03-08 12:43:32 -0800523 return 1;
Zhenzhong Duan9f9c9cbb2012-12-20 15:05:14 -0800524}
525
Ard Biesheuvelfc430262014-10-14 16:41:27 +0200526/*
527 * Check for the SMBIOS 3.0 64-bit entry point signature. Unlike the legacy
528 * 32-bit entry point, there is no embedded DMI header (_DMI_) in here.
529 */
530static int __init dmi_smbios3_present(const u8 *buf)
531{
532 if (memcmp(buf, "_SM3_", 5) == 0 &&
533 buf[6] < 32 && dmi_checksum(buf, buf[6])) {
534 dmi_ver = get_unaligned_be16(buf + 7);
Jean Delvarebfbaafa2015-03-20 09:59:47 +0100535 dmi_num = 0; /* No longer specified */
Ard Biesheuvelfc430262014-10-14 16:41:27 +0200536 dmi_len = get_unaligned_le32(buf + 12);
537 dmi_base = get_unaligned_le64(buf + 16);
538
Ard Biesheuvelfc430262014-10-14 16:41:27 +0200539 if (dmi_walk_early(dmi_decode) == 0) {
540 pr_info("SMBIOS %d.%d present.\n",
541 dmi_ver >> 8, dmi_ver & 0xFF);
542 dmi_format_ids(dmi_ids_string, sizeof(dmi_ids_string));
543 pr_debug("DMI: %s\n", dmi_ids_string);
544 return 0;
545 }
546 }
547 return 1;
548}
549
Matt Domsch3ed3bce2006-03-26 01:37:03 -0800550void __init dmi_scan_machine(void)
551{
Andrey Panin61e032fa2005-09-06 15:18:26 -0700552 char __iomem *p, *q;
Ben Hutchings79bae422013-04-30 15:27:46 -0700553 char buf[32];
Andrey Panin61e032fa2005-09-06 15:18:26 -0700554
Matt Fleming83e68182012-11-14 09:42:35 +0000555 if (efi_enabled(EFI_CONFIG_TABLES)) {
Ard Biesheuvelfc430262014-10-14 16:41:27 +0200556 /*
557 * According to the DMTF SMBIOS reference spec v3.0.0, it is
558 * allowed to define both the 64-bit entry point (smbios3) and
559 * the 32-bit entry point (smbios), in which case they should
560 * either both point to the same SMBIOS structure table, or the
561 * table pointed to by the 64-bit entry point should contain a
562 * superset of the table contents pointed to by the 32-bit entry
563 * point (section 5.2)
564 * This implies that the 64-bit entry point should have
565 * precedence if it is defined and supported by the OS. If we
566 * have the 64-bit entry point, but fail to decode it, fall
567 * back to the legacy one (if available)
568 */
569 if (efi.smbios3 != EFI_INVALID_TABLE_ADDR) {
570 p = dmi_early_remap(efi.smbios3, 32);
571 if (p == NULL)
572 goto error;
573 memcpy_fromio(buf, p, 32);
574 dmi_early_unmap(p, 32);
575
576 if (!dmi_smbios3_present(buf)) {
577 dmi_available = 1;
578 goto out;
579 }
580 }
Bjorn Helgaasb2c99e32006-03-26 01:37:08 -0800581 if (efi.smbios == EFI_INVALID_TABLE_ADDR)
Ingo Molnar9a22b6e2008-09-18 12:50:18 +0200582 goto error;
Andrey Panin61e032fa2005-09-06 15:18:26 -0700583
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200584 /* This is called as a core_initcall() because it isn't
585 * needed during early boot. This also means we can
586 * iounmap the space when we're done with it.
587 */
Ard Biesheuvelcf074402014-01-23 15:54:39 -0800588 p = dmi_early_remap(efi.smbios, 32);
Matt Domsch3ed3bce2006-03-26 01:37:03 -0800589 if (p == NULL)
Ingo Molnar9a22b6e2008-09-18 12:50:18 +0200590 goto error;
Ben Hutchings79bae422013-04-30 15:27:46 -0700591 memcpy_fromio(buf, p, 32);
Ard Biesheuvelcf074402014-01-23 15:54:39 -0800592 dmi_early_unmap(p, 32);
Ben Hutchings79bae422013-04-30 15:27:46 -0700593
594 if (!dmi_present(buf)) {
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200595 dmi_available = 1;
Ingo Molnar9a22b6e2008-09-18 12:50:18 +0200596 goto out;
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200597 }
Ard Biesheuvelcf074402014-01-23 15:54:39 -0800598 } else if (IS_ENABLED(CONFIG_DMI_SCAN_MACHINE_NON_EFI_FALLBACK)) {
599 p = dmi_early_remap(0xF0000, 0x10000);
Matt Domsch3ed3bce2006-03-26 01:37:03 -0800600 if (p == NULL)
Ingo Molnar9a22b6e2008-09-18 12:50:18 +0200601 goto error;
Andrey Panin61e032fa2005-09-06 15:18:26 -0700602
Ben Hutchingsd39de282013-07-31 13:53:30 -0700603 /*
604 * Iterate over all possible DMI header addresses q.
605 * Maintain the 32 bytes around q in buf. On the
606 * first iteration, substitute zero for the
607 * out-of-range bytes so there is no chance of falsely
608 * detecting an SMBIOS header.
609 */
Ben Hutchings79bae422013-04-30 15:27:46 -0700610 memset(buf, 0, 16);
Matt Domsch3ed3bce2006-03-26 01:37:03 -0800611 for (q = p; q < p + 0x10000; q += 16) {
Ben Hutchings79bae422013-04-30 15:27:46 -0700612 memcpy_fromio(buf + 16, q, 16);
Ard Biesheuvelfc430262014-10-14 16:41:27 +0200613 if (!dmi_smbios3_present(buf) || !dmi_present(buf)) {
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200614 dmi_available = 1;
Ard Biesheuvelcf074402014-01-23 15:54:39 -0800615 dmi_early_unmap(p, 0x10000);
Ingo Molnar9a22b6e2008-09-18 12:50:18 +0200616 goto out;
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200617 }
Ben Hutchings79bae422013-04-30 15:27:46 -0700618 memcpy(buf, buf + 16, 16);
Andrey Panin61e032fa2005-09-06 15:18:26 -0700619 }
Ard Biesheuvelcf074402014-01-23 15:54:39 -0800620 dmi_early_unmap(p, 0x10000);
Andrey Panin61e032fa2005-09-06 15:18:26 -0700621 }
Ingo Molnar9a22b6e2008-09-18 12:50:18 +0200622 error:
Jean Delvare02d9c472013-09-11 14:24:08 -0700623 pr_info("DMI not present or invalid.\n");
Ingo Molnar9a22b6e2008-09-18 12:50:18 +0200624 out:
625 dmi_initialized = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626}
627
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628/**
Tejun Heo98e5e1b2013-04-30 15:27:15 -0700629 * dmi_set_dump_stack_arch_desc - set arch description for dump_stack()
630 *
631 * Invoke dump_stack_set_arch_desc() with DMI system information so that
632 * DMI identifiers are printed out on task dumps. Arch boot code should
633 * call this function after dmi_scan_machine() if it wants to print out DMI
634 * identifiers on task dumps.
635 */
636void __init dmi_set_dump_stack_arch_desc(void)
637{
638 dump_stack_set_arch_desc("%s", dmi_ids_string);
639}
640
641/**
Rafael J. Wysockid7b19562009-01-19 20:55:50 +0100642 * dmi_matches - check if dmi_system_id structure matches system DMI data
643 * @dmi: pointer to the dmi_system_id structure to check
644 */
645static bool dmi_matches(const struct dmi_system_id *dmi)
646{
647 int i;
648
649 WARN(!dmi_initialized, KERN_ERR "dmi check: not initialized yet.\n");
650
651 for (i = 0; i < ARRAY_SIZE(dmi->matches); i++) {
652 int s = dmi->matches[i].slot;
653 if (s == DMI_NONE)
Dmitry Torokhov75757502009-12-04 10:24:19 -0800654 break;
Jani Nikula5017b282013-07-03 15:05:02 -0700655 if (dmi_ident[s]) {
656 if (!dmi->matches[i].exact_match &&
657 strstr(dmi_ident[s], dmi->matches[i].substr))
658 continue;
659 else if (dmi->matches[i].exact_match &&
660 !strcmp(dmi_ident[s], dmi->matches[i].substr))
661 continue;
662 }
663
Rafael J. Wysockid7b19562009-01-19 20:55:50 +0100664 /* No match */
665 return false;
666 }
667 return true;
668}
669
670/**
Dmitry Torokhov75757502009-12-04 10:24:19 -0800671 * dmi_is_end_of_table - check for end-of-table marker
672 * @dmi: pointer to the dmi_system_id structure to check
673 */
674static bool dmi_is_end_of_table(const struct dmi_system_id *dmi)
675{
676 return dmi->matches[0].slot == DMI_NONE;
677}
678
679/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 * dmi_check_system - check system DMI data
681 * @list: array of dmi_system_id structures to match against
Randy Dunlapb0ef3712006-06-25 05:49:18 -0700682 * All non-null elements of the list must match
683 * their slot's (field index's) data (i.e., each
684 * list string must be a substring of the specified
685 * DMI slot's string data) to be considered a
686 * successful match.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 *
688 * Walk the blacklist table running matching functions until someone
689 * returns non zero or we hit the end. Callback function is called for
Randy Dunlapb0ef3712006-06-25 05:49:18 -0700690 * each successful match. Returns the number of matches.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 */
Jeff Garzik18552562007-10-03 15:15:40 -0400692int dmi_check_system(const struct dmi_system_id *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693{
Rafael J. Wysockid7b19562009-01-19 20:55:50 +0100694 int count = 0;
695 const struct dmi_system_id *d;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
Dmitry Torokhov75757502009-12-04 10:24:19 -0800697 for (d = list; !dmi_is_end_of_table(d); d++)
Rafael J. Wysockid7b19562009-01-19 20:55:50 +0100698 if (dmi_matches(d)) {
699 count++;
700 if (d->callback && d->callback(d))
701 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
704 return count;
705}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706EXPORT_SYMBOL(dmi_check_system);
707
708/**
Rafael J. Wysockid7b19562009-01-19 20:55:50 +0100709 * dmi_first_match - find dmi_system_id structure matching system DMI data
710 * @list: array of dmi_system_id structures to match against
711 * All non-null elements of the list must match
712 * their slot's (field index's) data (i.e., each
713 * list string must be a substring of the specified
714 * DMI slot's string data) to be considered a
715 * successful match.
716 *
717 * Walk the blacklist table until the first match is found. Return the
718 * pointer to the matching entry or NULL if there's no match.
719 */
720const struct dmi_system_id *dmi_first_match(const struct dmi_system_id *list)
721{
722 const struct dmi_system_id *d;
723
Dmitry Torokhov75757502009-12-04 10:24:19 -0800724 for (d = list; !dmi_is_end_of_table(d); d++)
Rafael J. Wysockid7b19562009-01-19 20:55:50 +0100725 if (dmi_matches(d))
726 return d;
727
728 return NULL;
729}
730EXPORT_SYMBOL(dmi_first_match);
731
732/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 * dmi_get_system_info - return DMI data value
Randy Dunlapb0ef3712006-06-25 05:49:18 -0700734 * @field: data index (see enum dmi_field)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 *
736 * Returns one DMI data value, can be used to perform
737 * complex DMI data checks.
738 */
Jeff Garzik18552562007-10-03 15:15:40 -0400739const char *dmi_get_system_info(int field)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740{
741 return dmi_ident[field];
742}
Dmitry Torokhove70c9d52005-06-25 14:54:25 -0700743EXPORT_SYMBOL(dmi_get_system_info);
Andrey Paninebad6a42005-09-06 15:18:29 -0700744
Alok Katariafd8cd7e2008-11-03 15:50:38 -0800745/**
Randy Dunlapc2bacfc2009-01-06 14:41:40 -0800746 * dmi_name_in_serial - Check if string is in the DMI product serial information
747 * @str: string to check for
Alok Katariafd8cd7e2008-11-03 15:50:38 -0800748 */
749int dmi_name_in_serial(const char *str)
750{
751 int f = DMI_PRODUCT_SERIAL;
752 if (dmi_ident[f] && strstr(dmi_ident[f], str))
753 return 1;
754 return 0;
755}
Andi Kleena1bae672006-10-21 18:37:02 +0200756
757/**
Jean Delvare66e13e62011-11-15 14:36:09 -0800758 * dmi_name_in_vendors - Check if string is in the DMI system or board vendor name
Jean Delvare02d9c472013-09-11 14:24:08 -0700759 * @str: Case sensitive Name
Andi Kleena1bae672006-10-21 18:37:02 +0200760 */
Jeff Garzik18552562007-10-03 15:15:40 -0400761int dmi_name_in_vendors(const char *str)
Andi Kleena1bae672006-10-21 18:37:02 +0200762{
Jean Delvare66e13e62011-11-15 14:36:09 -0800763 static int fields[] = { DMI_SYS_VENDOR, DMI_BOARD_VENDOR, DMI_NONE };
Andi Kleena1bae672006-10-21 18:37:02 +0200764 int i;
765 for (i = 0; fields[i] != DMI_NONE; i++) {
766 int f = fields[i];
767 if (dmi_ident[f] && strstr(dmi_ident[f], str))
768 return 1;
769 }
770 return 0;
771}
772EXPORT_SYMBOL(dmi_name_in_vendors);
773
Andrey Paninebad6a42005-09-06 15:18:29 -0700774/**
775 * dmi_find_device - find onboard device by type/name
776 * @type: device type or %DMI_DEV_TYPE_ANY to match all device types
Randy Dunlapb0ef3712006-06-25 05:49:18 -0700777 * @name: device name string or %NULL to match all
Andrey Paninebad6a42005-09-06 15:18:29 -0700778 * @from: previous device found in search, or %NULL for new search.
779 *
780 * Iterates through the list of known onboard devices. If a device is
781 * found with a matching @vendor and @device, a pointer to its device
782 * structure is returned. Otherwise, %NULL is returned.
Randy Dunlapb0ef3712006-06-25 05:49:18 -0700783 * A new search is initiated by passing %NULL as the @from argument.
Andrey Paninebad6a42005-09-06 15:18:29 -0700784 * If @from is not %NULL, searches continue from next device.
785 */
Jean Delvare02d9c472013-09-11 14:24:08 -0700786const struct dmi_device *dmi_find_device(int type, const char *name,
Jeff Garzik18552562007-10-03 15:15:40 -0400787 const struct dmi_device *from)
Andrey Paninebad6a42005-09-06 15:18:29 -0700788{
Jeff Garzik18552562007-10-03 15:15:40 -0400789 const struct list_head *head = from ? &from->list : &dmi_devices;
790 struct list_head *d;
Andrey Paninebad6a42005-09-06 15:18:29 -0700791
Jean Delvare02d9c472013-09-11 14:24:08 -0700792 for (d = head->next; d != &dmi_devices; d = d->next) {
Jeff Garzik18552562007-10-03 15:15:40 -0400793 const struct dmi_device *dev =
794 list_entry(d, struct dmi_device, list);
Andrey Paninebad6a42005-09-06 15:18:29 -0700795
796 if (((type == DMI_DEV_TYPE_ANY) || (dev->type == type)) &&
797 ((name == NULL) || (strcmp(dev->name, name) == 0)))
798 return dev;
799 }
800
801 return NULL;
802}
803EXPORT_SYMBOL(dmi_find_device);
Andi Kleenf083a322006-03-25 16:30:19 +0100804
805/**
Tejun Heo3e5cd1f2009-08-16 21:02:36 +0900806 * dmi_get_date - parse a DMI date
807 * @field: data index (see enum dmi_field)
808 * @yearp: optional out parameter for the year
809 * @monthp: optional out parameter for the month
810 * @dayp: optional out parameter for the day
Andi Kleenf083a322006-03-25 16:30:19 +0100811 *
Tejun Heo3e5cd1f2009-08-16 21:02:36 +0900812 * The date field is assumed to be in the form resembling
813 * [mm[/dd]]/yy[yy] and the result is stored in the out
814 * parameters any or all of which can be omitted.
815 *
816 * If the field doesn't exist, all out parameters are set to zero
817 * and false is returned. Otherwise, true is returned with any
818 * invalid part of date set to zero.
819 *
820 * On return, year, month and day are guaranteed to be in the
821 * range of [0,9999], [0,12] and [0,31] respectively.
Andi Kleenf083a322006-03-25 16:30:19 +0100822 */
Tejun Heo3e5cd1f2009-08-16 21:02:36 +0900823bool dmi_get_date(int field, int *yearp, int *monthp, int *dayp)
Andi Kleenf083a322006-03-25 16:30:19 +0100824{
Tejun Heo3e5cd1f2009-08-16 21:02:36 +0900825 int year = 0, month = 0, day = 0;
826 bool exists;
827 const char *s, *y;
Tejun Heo02c24fa2009-08-16 21:01:22 +0900828 char *e;
Andi Kleenf083a322006-03-25 16:30:19 +0100829
Tejun Heo3e5cd1f2009-08-16 21:02:36 +0900830 s = dmi_get_system_info(field);
831 exists = s;
832 if (!exists)
833 goto out;
Andi Kleenf083a322006-03-25 16:30:19 +0100834
Tejun Heo3e5cd1f2009-08-16 21:02:36 +0900835 /*
836 * Determine year first. We assume the date string resembles
837 * mm/dd/yy[yy] but the original code extracted only the year
838 * from the end. Keep the behavior in the spirit of no
839 * surprises.
840 */
841 y = strrchr(s, '/');
842 if (!y)
843 goto out;
844
845 y++;
846 year = simple_strtoul(y, &e, 10);
847 if (y != e && year < 100) { /* 2-digit year */
Andi Kleenf083a322006-03-25 16:30:19 +0100848 year += 1900;
849 if (year < 1996) /* no dates < spec 1.0 */
850 year += 100;
851 }
Tejun Heo3e5cd1f2009-08-16 21:02:36 +0900852 if (year > 9999) /* year should fit in %04d */
853 year = 0;
Andi Kleenf083a322006-03-25 16:30:19 +0100854
Tejun Heo3e5cd1f2009-08-16 21:02:36 +0900855 /* parse the mm and dd */
856 month = simple_strtoul(s, &e, 10);
857 if (s == e || *e != '/' || !month || month > 12) {
858 month = 0;
859 goto out;
860 }
861
862 s = e + 1;
863 day = simple_strtoul(s, &e, 10);
864 if (s == y || s == e || *e != '/' || day > 31)
865 day = 0;
866out:
867 if (yearp)
868 *yearp = year;
869 if (monthp)
870 *monthp = month;
871 if (dayp)
872 *dayp = day;
873 return exists;
Andi Kleenf083a322006-03-25 16:30:19 +0100874}
Tejun Heo3e5cd1f2009-08-16 21:02:36 +0900875EXPORT_SYMBOL(dmi_get_date);
Jean Delvare7fce0842007-11-03 17:29:20 +0100876
877/**
878 * dmi_walk - Walk the DMI table and get called back for every record
879 * @decode: Callback function
Jean Delvaree7a19c562009-03-30 21:46:44 +0200880 * @private_data: Private data to be passed to the callback function
Jean Delvare7fce0842007-11-03 17:29:20 +0100881 *
882 * Returns -1 when the DMI table can't be reached, 0 on success.
883 */
Jean Delvaree7a19c562009-03-30 21:46:44 +0200884int dmi_walk(void (*decode)(const struct dmi_header *, void *),
885 void *private_data)
Jean Delvare7fce0842007-11-03 17:29:20 +0100886{
887 u8 *buf;
888
889 if (!dmi_available)
890 return -1;
891
Ard Biesheuvelcf074402014-01-23 15:54:39 -0800892 buf = dmi_remap(dmi_base, dmi_len);
Jean Delvare7fce0842007-11-03 17:29:20 +0100893 if (buf == NULL)
894 return -1;
895
Jean Delvaree7a19c562009-03-30 21:46:44 +0200896 dmi_table(buf, dmi_len, dmi_num, decode, private_data);
Jean Delvare7fce0842007-11-03 17:29:20 +0100897
Ard Biesheuvelcf074402014-01-23 15:54:39 -0800898 dmi_unmap(buf);
Jean Delvare7fce0842007-11-03 17:29:20 +0100899 return 0;
900}
901EXPORT_SYMBOL_GPL(dmi_walk);
Jiri Slabyd61c72e2008-12-10 14:07:21 +0100902
903/**
904 * dmi_match - compare a string to the dmi field (if exists)
Randy Dunlapc2bacfc2009-01-06 14:41:40 -0800905 * @f: DMI field identifier
906 * @str: string to compare the DMI field to
Jiri Slabyd61c72e2008-12-10 14:07:21 +0100907 *
908 * Returns true if the requested field equals to the str (including NULL).
909 */
910bool dmi_match(enum dmi_field f, const char *str)
911{
912 const char *info = dmi_get_system_info(f);
913
914 if (info == NULL || str == NULL)
915 return info == str;
916
917 return !strcmp(info, str);
918}
919EXPORT_SYMBOL_GPL(dmi_match);
Chen, Gongdd6dad42013-10-18 14:29:25 -0700920
921void dmi_memdev_name(u16 handle, const char **bank, const char **device)
922{
923 int n;
924
925 if (dmi_memdev == NULL)
926 return;
927
928 for (n = 0; n < dmi_memdev_nr; n++) {
929 if (handle == dmi_memdev[n].handle) {
930 *bank = dmi_memdev[n].bank;
931 *device = dmi_memdev[n].device;
932 break;
933 }
934 }
935}
936EXPORT_SYMBOL_GPL(dmi_memdev_name);