blob: 69fac068669fde566f41013cefbdf48db023466c [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 /*
Bjorn Helgaas4f705ae2006-04-03 17:09:22 -070089 * Stop when we see all the items the table claimed to have
90 * OR we run off the end of the table (also happens)
91 */
Andrey Panin1249c512005-06-25 14:54:47 -070092 while ((i < num) && (data - buf + sizeof(struct dmi_header)) <= len) {
Jeff Garzik18552562007-10-03 15:15:40 -040093 const struct dmi_header *dm = (const struct dmi_header *)data;
94
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 /*
Alan Cox86385452008-11-07 16:03:46 +000096 * We want to know the total length (formatted area and
97 * strings) before decoding to make sure we won't run off the
98 * table in dmi_decode or dmi_string
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 */
Andrey Panin1249c512005-06-25 14:54:47 -0700100 data += dm->length;
101 while ((data - buf < len - 1) && (data[0] || data[1]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 data++;
Andrey Panin1249c512005-06-25 14:54:47 -0700103 if (data - buf < len - 1)
Jean Delvaree7a19c562009-03-30 21:46:44 +0200104 decode(dm, private_data);
Ivan Khoronzhukce204e92015-02-18 15:51:41 +0200105
106 /*
107 * 7.45 End-of-Table (Type 127) [SMBIOS reference spec v3.0.0]
108 */
109 if (dm->type == DMI_ENTRY_END_OF_TABLE)
110 break;
111
Andrey Panin1249c512005-06-25 14:54:47 -0700112 data += 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 i++;
114 }
Jean Delvare7fce0842007-11-03 17:29:20 +0100115}
116
Ard Biesheuvelfc430262014-10-14 16:41:27 +0200117static phys_addr_t dmi_base;
Ivan Khoronzhuk6d9ff472015-02-18 13:33:19 +0200118static u32 dmi_len;
Jean Delvare7fce0842007-11-03 17:29:20 +0100119static u16 dmi_num;
120
Jean Delvaree7a19c562009-03-30 21:46:44 +0200121static int __init dmi_walk_early(void (*decode)(const struct dmi_header *,
122 void *))
Jean Delvare7fce0842007-11-03 17:29:20 +0100123{
124 u8 *buf;
125
Ard Biesheuvelcf074402014-01-23 15:54:39 -0800126 buf = dmi_early_remap(dmi_base, dmi_len);
Jean Delvare7fce0842007-11-03 17:29:20 +0100127 if (buf == NULL)
128 return -1;
129
Jean Delvaree7a19c562009-03-30 21:46:44 +0200130 dmi_table(buf, dmi_len, dmi_num, decode, NULL);
Jean Delvare7fce0842007-11-03 17:29:20 +0100131
Tony Luckd114a332012-07-20 13:15:20 -0700132 add_device_randomness(buf, dmi_len);
133
Ard Biesheuvelcf074402014-01-23 15:54:39 -0800134 dmi_early_unmap(buf, dmi_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 return 0;
136}
137
Zhenzhong Duan9f9c9cbb2012-12-20 15:05:14 -0800138static int __init dmi_checksum(const u8 *buf, u8 len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139{
Andrey Panin1249c512005-06-25 14:54:47 -0700140 u8 sum = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 int a;
Bjorn Helgaas4f705ae2006-04-03 17:09:22 -0700142
Zhenzhong Duan9f9c9cbb2012-12-20 15:05:14 -0800143 for (a = 0; a < len; a++)
Andrey Panin1249c512005-06-25 14:54:47 -0700144 sum += buf[a];
145
146 return sum == 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147}
148
Jean Delvareffbbb962013-09-11 14:24:09 -0700149static const char *dmi_ident[DMI_STRING_MAX];
Andrey Paninebad6a42005-09-06 15:18:29 -0700150static LIST_HEAD(dmi_devices);
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200151int dmi_available;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
153/*
154 * Save a DMI string
155 */
Jean Delvare02d9c472013-09-11 14:24:08 -0700156static void __init dmi_save_ident(const struct dmi_header *dm, int slot,
157 int string)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158{
Jean Delvare02d9c472013-09-11 14:24:08 -0700159 const char *d = (const char *) dm;
Jean Delvareffbbb962013-09-11 14:24:09 -0700160 const char *p;
Andrey Panin1249c512005-06-25 14:54:47 -0700161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 if (dmi_ident[slot])
163 return;
Andrey Panin1249c512005-06-25 14:54:47 -0700164
Andrey Paninc3c71202005-09-06 15:18:28 -0700165 p = dmi_string(dm, d[string]);
166 if (p == NULL)
167 return;
168
169 dmi_ident[slot] = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170}
171
Jean Delvare02d9c472013-09-11 14:24:08 -0700172static void __init dmi_save_uuid(const struct dmi_header *dm, int slot,
173 int index)
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200174{
Jean Delvare02d9c472013-09-11 14:24:08 -0700175 const u8 *d = (u8 *) dm + index;
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200176 char *s;
177 int is_ff = 1, is_00 = 1, i;
178
179 if (dmi_ident[slot])
180 return;
181
182 for (i = 0; i < 16 && (is_ff || is_00); i++) {
Zhenzhong Duanf1d8e612012-12-20 15:05:13 -0800183 if (d[i] != 0x00)
184 is_00 = 0;
185 if (d[i] != 0xFF)
186 is_ff = 0;
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200187 }
188
189 if (is_ff || is_00)
190 return;
191
192 s = dmi_alloc(16*2+4+1);
193 if (!s)
194 return;
195
Zhenzhong Duanf1d8e612012-12-20 15:05:13 -0800196 /*
197 * As of version 2.6 of the SMBIOS specification, the first 3 fields of
198 * the UUID are supposed to be little-endian encoded. The specification
199 * says that this is the defacto standard.
200 */
201 if (dmi_ver >= 0x0206)
202 sprintf(s, "%pUL", d);
203 else
204 sprintf(s, "%pUB", d);
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200205
Jean Delvare02d9c472013-09-11 14:24:08 -0700206 dmi_ident[slot] = s;
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200207}
208
Jean Delvare02d9c472013-09-11 14:24:08 -0700209static void __init dmi_save_type(const struct dmi_header *dm, int slot,
210 int index)
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200211{
Jean Delvare02d9c472013-09-11 14:24:08 -0700212 const u8 *d = (u8 *) dm + index;
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200213 char *s;
214
215 if (dmi_ident[slot])
216 return;
217
218 s = dmi_alloc(4);
219 if (!s)
220 return;
221
222 sprintf(s, "%u", *d & 0x7F);
223 dmi_ident[slot] = s;
224}
225
Jean Delvaref3069ae2008-02-23 15:23:46 -0800226static void __init dmi_save_one_device(int type, const char *name)
227{
228 struct dmi_device *dev;
229
230 /* No duplicate device */
231 if (dmi_find_device(type, name, NULL))
232 return;
233
234 dev = dmi_alloc(sizeof(*dev) + strlen(name) + 1);
Jean Delvareae797442013-09-11 14:24:10 -0700235 if (!dev)
Jean Delvaref3069ae2008-02-23 15:23:46 -0800236 return;
Jean Delvaref3069ae2008-02-23 15:23:46 -0800237
238 dev->type = type;
239 strcpy((char *)(dev + 1), name);
240 dev->name = (char *)(dev + 1);
241 dev->device_data = NULL;
242 list_add(&dev->list, &dmi_devices);
243}
244
Jeff Garzik18552562007-10-03 15:15:40 -0400245static void __init dmi_save_devices(const struct dmi_header *dm)
Andrey Paninebad6a42005-09-06 15:18:29 -0700246{
247 int i, count = (dm->length - sizeof(struct dmi_header)) / 2;
Andrey Paninebad6a42005-09-06 15:18:29 -0700248
249 for (i = 0; i < count; i++) {
Jeff Garzik18552562007-10-03 15:15:40 -0400250 const char *d = (char *)(dm + 1) + (i * 2);
Andrey Paninebad6a42005-09-06 15:18:29 -0700251
252 /* Skip disabled device */
253 if ((*d & 0x80) == 0)
254 continue;
255
Jean Delvaref3069ae2008-02-23 15:23:46 -0800256 dmi_save_one_device(*d & 0x7f, dmi_string_nosave(dm, *(d + 1)));
Shem Multinymous2e0c1f62006-09-29 01:59:37 -0700257 }
258}
259
Jeff Garzik18552562007-10-03 15:15:40 -0400260static void __init dmi_save_oem_strings_devices(const struct dmi_header *dm)
Shem Multinymous2e0c1f62006-09-29 01:59:37 -0700261{
262 int i, count = *(u8 *)(dm + 1);
263 struct dmi_device *dev;
264
265 for (i = 1; i <= count; i++) {
Jean Delvareffbbb962013-09-11 14:24:09 -0700266 const char *devname = dmi_string(dm, i);
Parag Warudkar79da4722008-01-30 13:31:59 +0100267
Jean Delvare43fe1052008-02-23 15:23:55 -0800268 if (devname == dmi_empty_string)
Parag Warudkar79da4722008-01-30 13:31:59 +0100269 continue;
Parag Warudkar79da4722008-01-30 13:31:59 +0100270
Shem Multinymous2e0c1f62006-09-29 01:59:37 -0700271 dev = dmi_alloc(sizeof(*dev));
Jean Delvareae797442013-09-11 14:24:10 -0700272 if (!dev)
Shem Multinymous2e0c1f62006-09-29 01:59:37 -0700273 break;
Shem Multinymous2e0c1f62006-09-29 01:59:37 -0700274
275 dev->type = DMI_DEV_TYPE_OEM_STRING;
Parag Warudkar79da4722008-01-30 13:31:59 +0100276 dev->name = devname;
Shem Multinymous2e0c1f62006-09-29 01:59:37 -0700277 dev->device_data = NULL;
Andrey Paninebad6a42005-09-06 15:18:29 -0700278
279 list_add(&dev->list, &dmi_devices);
280 }
281}
282
Jeff Garzik18552562007-10-03 15:15:40 -0400283static void __init dmi_save_ipmi_device(const struct dmi_header *dm)
Andrey Paninebad6a42005-09-06 15:18:29 -0700284{
285 struct dmi_device *dev;
Jean Delvare02d9c472013-09-11 14:24:08 -0700286 void *data;
Andrey Paninebad6a42005-09-06 15:18:29 -0700287
Andi Kleene9928672006-01-11 22:43:33 +0100288 data = dmi_alloc(dm->length);
Jean Delvareae797442013-09-11 14:24:10 -0700289 if (data == NULL)
Andrey Paninebad6a42005-09-06 15:18:29 -0700290 return;
Andrey Paninebad6a42005-09-06 15:18:29 -0700291
292 memcpy(data, dm, dm->length);
293
Andi Kleene9928672006-01-11 22:43:33 +0100294 dev = dmi_alloc(sizeof(*dev));
Jean Delvareae797442013-09-11 14:24:10 -0700295 if (!dev)
Andrey Paninebad6a42005-09-06 15:18:29 -0700296 return;
Andrey Paninebad6a42005-09-06 15:18:29 -0700297
298 dev->type = DMI_DEV_TYPE_IPMI;
299 dev->name = "IPMI controller";
300 dev->device_data = data;
301
Carol Hebertabd24df2008-04-04 14:30:03 -0700302 list_add_tail(&dev->list, &dmi_devices);
Andrey Paninebad6a42005-09-06 15:18:29 -0700303}
304
Narendra K911e1c92010-07-26 05:56:50 -0500305static void __init dmi_save_dev_onboard(int instance, int segment, int bus,
306 int devfn, const char *name)
307{
308 struct dmi_dev_onboard *onboard_dev;
309
310 onboard_dev = dmi_alloc(sizeof(*onboard_dev) + strlen(name) + 1);
Jean Delvareae797442013-09-11 14:24:10 -0700311 if (!onboard_dev)
Narendra K911e1c92010-07-26 05:56:50 -0500312 return;
Jean Delvareae797442013-09-11 14:24:10 -0700313
Narendra K911e1c92010-07-26 05:56:50 -0500314 onboard_dev->instance = instance;
315 onboard_dev->segment = segment;
316 onboard_dev->bus = bus;
317 onboard_dev->devfn = devfn;
318
319 strcpy((char *)&onboard_dev[1], name);
320 onboard_dev->dev.type = DMI_DEV_TYPE_DEV_ONBOARD;
321 onboard_dev->dev.name = (char *)&onboard_dev[1];
322 onboard_dev->dev.device_data = onboard_dev;
323
324 list_add(&onboard_dev->dev.list, &dmi_devices);
325}
326
Wim Van Sebroeckb4bd7d52008-02-08 04:20:58 -0800327static void __init dmi_save_extended_devices(const struct dmi_header *dm)
328{
Jean Delvare02d9c472013-09-11 14:24:08 -0700329 const u8 *d = (u8 *) dm + 5;
Wim Van Sebroeckb4bd7d52008-02-08 04:20:58 -0800330
331 /* Skip disabled device */
332 if ((*d & 0x80) == 0)
333 return;
334
Narendra K911e1c92010-07-26 05:56:50 -0500335 dmi_save_dev_onboard(*(d+1), *(u16 *)(d+2), *(d+4), *(d+5),
336 dmi_string_nosave(dm, *(d-1)));
Jean Delvaref3069ae2008-02-23 15:23:46 -0800337 dmi_save_one_device(*d & 0x7f, dmi_string_nosave(dm, *(d - 1)));
Wim Van Sebroeckb4bd7d52008-02-08 04:20:58 -0800338}
339
Chen, Gongdd6dad42013-10-18 14:29:25 -0700340static void __init count_mem_devices(const struct dmi_header *dm, void *v)
341{
342 if (dm->type != DMI_ENTRY_MEM_DEVICE)
343 return;
344 dmi_memdev_nr++;
345}
346
347static void __init save_mem_devices(const struct dmi_header *dm, void *v)
348{
349 const char *d = (const char *)dm;
350 static int nr;
351
352 if (dm->type != DMI_ENTRY_MEM_DEVICE)
353 return;
354 if (nr >= dmi_memdev_nr) {
355 pr_warn(FW_BUG "Too many DIMM entries in SMBIOS table\n");
356 return;
357 }
Luck, Tony0841c042013-11-01 13:59:52 -0700358 dmi_memdev[nr].handle = get_unaligned(&dm->handle);
Chen, Gongdd6dad42013-10-18 14:29:25 -0700359 dmi_memdev[nr].device = dmi_string(dm, d[0x10]);
360 dmi_memdev[nr].bank = dmi_string(dm, d[0x11]);
361 nr++;
362}
363
364void __init dmi_memdev_walk(void)
365{
366 if (!dmi_available)
367 return;
368
369 if (dmi_walk_early(count_mem_devices) == 0 && dmi_memdev_nr) {
370 dmi_memdev = dmi_alloc(sizeof(*dmi_memdev) * dmi_memdev_nr);
371 if (dmi_memdev)
372 dmi_walk_early(save_mem_devices);
373 }
374}
375
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 * Process a DMI table entry. Right now all we care about are the BIOS
378 * and machine entries. For 2.5 we should pull the smbus controller info
379 * out of here.
380 */
Jean Delvaree7a19c562009-03-30 21:46:44 +0200381static void __init dmi_decode(const struct dmi_header *dm, void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382{
Jean Delvare02d9c472013-09-11 14:24:08 -0700383 switch (dm->type) {
Andrey Paninebad6a42005-09-06 15:18:29 -0700384 case 0: /* BIOS Information */
Andrey Panin1249c512005-06-25 14:54:47 -0700385 dmi_save_ident(dm, DMI_BIOS_VENDOR, 4);
Andrey Panin1249c512005-06-25 14:54:47 -0700386 dmi_save_ident(dm, DMI_BIOS_VERSION, 5);
Andrey Panin1249c512005-06-25 14:54:47 -0700387 dmi_save_ident(dm, DMI_BIOS_DATE, 8);
388 break;
Andrey Paninebad6a42005-09-06 15:18:29 -0700389 case 1: /* System Information */
Andrey Panin1249c512005-06-25 14:54:47 -0700390 dmi_save_ident(dm, DMI_SYS_VENDOR, 4);
Andrey Panin1249c512005-06-25 14:54:47 -0700391 dmi_save_ident(dm, DMI_PRODUCT_NAME, 5);
Andrey Panin1249c512005-06-25 14:54:47 -0700392 dmi_save_ident(dm, DMI_PRODUCT_VERSION, 6);
Andrey Panin1249c512005-06-25 14:54:47 -0700393 dmi_save_ident(dm, DMI_PRODUCT_SERIAL, 7);
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200394 dmi_save_uuid(dm, DMI_PRODUCT_UUID, 8);
Andrey Panin1249c512005-06-25 14:54:47 -0700395 break;
Andrey Paninebad6a42005-09-06 15:18:29 -0700396 case 2: /* Base Board Information */
Andrey Panin1249c512005-06-25 14:54:47 -0700397 dmi_save_ident(dm, DMI_BOARD_VENDOR, 4);
Andrey Panin1249c512005-06-25 14:54:47 -0700398 dmi_save_ident(dm, DMI_BOARD_NAME, 5);
Andrey Panin1249c512005-06-25 14:54:47 -0700399 dmi_save_ident(dm, DMI_BOARD_VERSION, 6);
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200400 dmi_save_ident(dm, DMI_BOARD_SERIAL, 7);
401 dmi_save_ident(dm, DMI_BOARD_ASSET_TAG, 8);
402 break;
403 case 3: /* Chassis Information */
404 dmi_save_ident(dm, DMI_CHASSIS_VENDOR, 4);
405 dmi_save_type(dm, DMI_CHASSIS_TYPE, 5);
406 dmi_save_ident(dm, DMI_CHASSIS_VERSION, 6);
407 dmi_save_ident(dm, DMI_CHASSIS_SERIAL, 7);
408 dmi_save_ident(dm, DMI_CHASSIS_ASSET_TAG, 8);
Andrey Panin1249c512005-06-25 14:54:47 -0700409 break;
Andrey Paninebad6a42005-09-06 15:18:29 -0700410 case 10: /* Onboard Devices Information */
411 dmi_save_devices(dm);
412 break;
Shem Multinymous2e0c1f62006-09-29 01:59:37 -0700413 case 11: /* OEM Strings */
414 dmi_save_oem_strings_devices(dm);
415 break;
Andrey Paninebad6a42005-09-06 15:18:29 -0700416 case 38: /* IPMI Device Information */
417 dmi_save_ipmi_device(dm);
Wim Van Sebroeckb4bd7d52008-02-08 04:20:58 -0800418 break;
419 case 41: /* Onboard Devices Extended Information */
420 dmi_save_extended_devices(dm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 }
422}
423
Tejun Heoc90fe6b2013-04-30 15:27:14 -0700424static int __init print_filtered(char *buf, size_t len, const char *info)
Bjorn Helgaas8881cdc2010-10-27 15:32:59 -0700425{
Tejun Heoc90fe6b2013-04-30 15:27:14 -0700426 int c = 0;
Bjorn Helgaas8881cdc2010-10-27 15:32:59 -0700427 const char *p;
428
429 if (!info)
Tejun Heoc90fe6b2013-04-30 15:27:14 -0700430 return c;
Bjorn Helgaas8881cdc2010-10-27 15:32:59 -0700431
432 for (p = info; *p; p++)
433 if (isprint(*p))
Tejun Heoc90fe6b2013-04-30 15:27:14 -0700434 c += scnprintf(buf + c, len - c, "%c", *p);
Bjorn Helgaas8881cdc2010-10-27 15:32:59 -0700435 else
Tejun Heoc90fe6b2013-04-30 15:27:14 -0700436 c += scnprintf(buf + c, len - c, "\\x%02x", *p & 0xff);
437 return c;
Bjorn Helgaas8881cdc2010-10-27 15:32:59 -0700438}
439
Tejun Heoc90fe6b2013-04-30 15:27:14 -0700440static void __init dmi_format_ids(char *buf, size_t len)
Bjorn Helgaas8881cdc2010-10-27 15:32:59 -0700441{
Tejun Heoc90fe6b2013-04-30 15:27:14 -0700442 int c = 0;
Naga Chumbalkar84e383b2011-02-14 22:47:17 +0000443 const char *board; /* Board Name is optional */
444
Tejun Heoc90fe6b2013-04-30 15:27:14 -0700445 c += print_filtered(buf + c, len - c,
446 dmi_get_system_info(DMI_SYS_VENDOR));
447 c += scnprintf(buf + c, len - c, " ");
448 c += print_filtered(buf + c, len - c,
449 dmi_get_system_info(DMI_PRODUCT_NAME));
450
Naga Chumbalkar84e383b2011-02-14 22:47:17 +0000451 board = dmi_get_system_info(DMI_BOARD_NAME);
452 if (board) {
Tejun Heoc90fe6b2013-04-30 15:27:14 -0700453 c += scnprintf(buf + c, len - c, "/");
454 c += print_filtered(buf + c, len - c, board);
Naga Chumbalkar84e383b2011-02-14 22:47:17 +0000455 }
Tejun Heoc90fe6b2013-04-30 15:27:14 -0700456 c += scnprintf(buf + c, len - c, ", BIOS ");
457 c += print_filtered(buf + c, len - c,
458 dmi_get_system_info(DMI_BIOS_VERSION));
459 c += scnprintf(buf + c, len - c, " ");
460 c += print_filtered(buf + c, len - c,
461 dmi_get_system_info(DMI_BIOS_DATE));
Bjorn Helgaas8881cdc2010-10-27 15:32:59 -0700462}
463
Ben Hutchingsd39de282013-07-31 13:53:30 -0700464/*
465 * Check for DMI/SMBIOS headers in the system firmware image. Any
466 * SMBIOS header must start 16 bytes before the DMI header, so take a
467 * 32 byte buffer and check for DMI at offset 16 and SMBIOS at offset
468 * 0. If the DMI header is present, set dmi_ver accordingly (SMBIOS
469 * takes precedence) and return 0. Otherwise return 1.
470 */
Ben Hutchings79bae422013-04-30 15:27:46 -0700471static int __init dmi_present(const u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472{
Ben Hutchings79bae422013-04-30 15:27:46 -0700473 int smbios_ver;
Jeff Garzik18552562007-10-03 15:15:40 -0400474
Ben Hutchings79bae422013-04-30 15:27:46 -0700475 if (memcmp(buf, "_SM_", 4) == 0 &&
476 buf[5] < 32 && dmi_checksum(buf, buf[5])) {
Ard Biesheuvelfc430262014-10-14 16:41:27 +0200477 smbios_ver = get_unaligned_be16(buf + 6);
Ben Hutchings79bae422013-04-30 15:27:46 -0700478
479 /* Some BIOS report weird SMBIOS version, fix that up */
480 switch (smbios_ver) {
481 case 0x021F:
482 case 0x0221:
483 pr_debug("SMBIOS version fixup(2.%d->2.%d)\n",
484 smbios_ver & 0xFF, 3);
485 smbios_ver = 0x0203;
486 break;
487 case 0x0233:
488 pr_debug("SMBIOS version fixup(2.%d->2.%d)\n", 51, 6);
489 smbios_ver = 0x0206;
490 break;
491 }
492 } else {
493 smbios_ver = 0;
494 }
495
496 buf += 16;
497
498 if (memcmp(buf, "_DMI_", 5) == 0 && dmi_checksum(buf, 15)) {
Ard Biesheuvelfc430262014-10-14 16:41:27 +0200499 dmi_num = get_unaligned_le16(buf + 12);
500 dmi_len = get_unaligned_le16(buf + 6);
501 dmi_base = get_unaligned_le32(buf + 8);
Matt Domsch3ed3bce2006-03-26 01:37:03 -0800502
Bjorn Helgaas8881cdc2010-10-27 15:32:59 -0700503 if (dmi_walk_early(dmi_decode) == 0) {
Ben Hutchings79bae422013-04-30 15:27:46 -0700504 if (smbios_ver) {
505 dmi_ver = smbios_ver;
Zhenzhong Duan9f9c9cbb2012-12-20 15:05:14 -0800506 pr_info("SMBIOS %d.%d present.\n",
507 dmi_ver >> 8, dmi_ver & 0xFF);
Ben Hutchings79bae422013-04-30 15:27:46 -0700508 } else {
Zhenzhong Duan9f9c9cbb2012-12-20 15:05:14 -0800509 dmi_ver = (buf[14] & 0xF0) << 4 |
510 (buf[14] & 0x0F);
511 pr_info("Legacy DMI %d.%d present.\n",
512 dmi_ver >> 8, dmi_ver & 0xFF);
513 }
Tejun Heoc90fe6b2013-04-30 15:27:14 -0700514 dmi_format_ids(dmi_ids_string, sizeof(dmi_ids_string));
515 printk(KERN_DEBUG "DMI: %s\n", dmi_ids_string);
Matt Domsch3ed3bce2006-03-26 01:37:03 -0800516 return 0;
Bjorn Helgaas8881cdc2010-10-27 15:32:59 -0700517 }
Matt Domsch3ed3bce2006-03-26 01:37:03 -0800518 }
Matt Domsch3ed3bce2006-03-26 01:37:03 -0800519
Ben Hutchingsa40e7cf2013-03-08 12:43:32 -0800520 return 1;
Zhenzhong Duan9f9c9cbb2012-12-20 15:05:14 -0800521}
522
Ard Biesheuvelfc430262014-10-14 16:41:27 +0200523/*
524 * Check for the SMBIOS 3.0 64-bit entry point signature. Unlike the legacy
525 * 32-bit entry point, there is no embedded DMI header (_DMI_) in here.
526 */
527static int __init dmi_smbios3_present(const u8 *buf)
528{
529 if (memcmp(buf, "_SM3_", 5) == 0 &&
530 buf[6] < 32 && dmi_checksum(buf, buf[6])) {
531 dmi_ver = get_unaligned_be16(buf + 7);
532 dmi_len = get_unaligned_le32(buf + 12);
533 dmi_base = get_unaligned_le64(buf + 16);
534
535 /*
536 * The 64-bit SMBIOS 3.0 entry point no longer has a field
537 * containing the number of structures present in the table.
538 * Instead, it defines the table size as a maximum size, and
539 * relies on the end-of-table structure type (#127) to be used
540 * to signal the end of the table.
541 * So let's define dmi_num as an upper bound as well: each
542 * structure has a 4 byte header, so dmi_len / 4 is an upper
543 * bound for the number of structures in the table.
544 */
545 dmi_num = dmi_len / 4;
546
547 if (dmi_walk_early(dmi_decode) == 0) {
548 pr_info("SMBIOS %d.%d present.\n",
549 dmi_ver >> 8, dmi_ver & 0xFF);
550 dmi_format_ids(dmi_ids_string, sizeof(dmi_ids_string));
551 pr_debug("DMI: %s\n", dmi_ids_string);
552 return 0;
553 }
554 }
555 return 1;
556}
557
Matt Domsch3ed3bce2006-03-26 01:37:03 -0800558void __init dmi_scan_machine(void)
559{
Andrey Panin61e032fa2005-09-06 15:18:26 -0700560 char __iomem *p, *q;
Ben Hutchings79bae422013-04-30 15:27:46 -0700561 char buf[32];
Andrey Panin61e032fa2005-09-06 15:18:26 -0700562
Matt Fleming83e68182012-11-14 09:42:35 +0000563 if (efi_enabled(EFI_CONFIG_TABLES)) {
Ard Biesheuvelfc430262014-10-14 16:41:27 +0200564 /*
565 * According to the DMTF SMBIOS reference spec v3.0.0, it is
566 * allowed to define both the 64-bit entry point (smbios3) and
567 * the 32-bit entry point (smbios), in which case they should
568 * either both point to the same SMBIOS structure table, or the
569 * table pointed to by the 64-bit entry point should contain a
570 * superset of the table contents pointed to by the 32-bit entry
571 * point (section 5.2)
572 * This implies that the 64-bit entry point should have
573 * precedence if it is defined and supported by the OS. If we
574 * have the 64-bit entry point, but fail to decode it, fall
575 * back to the legacy one (if available)
576 */
577 if (efi.smbios3 != EFI_INVALID_TABLE_ADDR) {
578 p = dmi_early_remap(efi.smbios3, 32);
579 if (p == NULL)
580 goto error;
581 memcpy_fromio(buf, p, 32);
582 dmi_early_unmap(p, 32);
583
584 if (!dmi_smbios3_present(buf)) {
585 dmi_available = 1;
586 goto out;
587 }
588 }
Bjorn Helgaasb2c99e32006-03-26 01:37:08 -0800589 if (efi.smbios == EFI_INVALID_TABLE_ADDR)
Ingo Molnar9a22b6e2008-09-18 12:50:18 +0200590 goto error;
Andrey Panin61e032fa2005-09-06 15:18:26 -0700591
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200592 /* This is called as a core_initcall() because it isn't
593 * needed during early boot. This also means we can
594 * iounmap the space when we're done with it.
595 */
Ard Biesheuvelcf074402014-01-23 15:54:39 -0800596 p = dmi_early_remap(efi.smbios, 32);
Matt Domsch3ed3bce2006-03-26 01:37:03 -0800597 if (p == NULL)
Ingo Molnar9a22b6e2008-09-18 12:50:18 +0200598 goto error;
Ben Hutchings79bae422013-04-30 15:27:46 -0700599 memcpy_fromio(buf, p, 32);
Ard Biesheuvelcf074402014-01-23 15:54:39 -0800600 dmi_early_unmap(p, 32);
Ben Hutchings79bae422013-04-30 15:27:46 -0700601
602 if (!dmi_present(buf)) {
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200603 dmi_available = 1;
Ingo Molnar9a22b6e2008-09-18 12:50:18 +0200604 goto out;
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200605 }
Ard Biesheuvelcf074402014-01-23 15:54:39 -0800606 } else if (IS_ENABLED(CONFIG_DMI_SCAN_MACHINE_NON_EFI_FALLBACK)) {
607 p = dmi_early_remap(0xF0000, 0x10000);
Matt Domsch3ed3bce2006-03-26 01:37:03 -0800608 if (p == NULL)
Ingo Molnar9a22b6e2008-09-18 12:50:18 +0200609 goto error;
Andrey Panin61e032fa2005-09-06 15:18:26 -0700610
Ben Hutchingsd39de282013-07-31 13:53:30 -0700611 /*
612 * Iterate over all possible DMI header addresses q.
613 * Maintain the 32 bytes around q in buf. On the
614 * first iteration, substitute zero for the
615 * out-of-range bytes so there is no chance of falsely
616 * detecting an SMBIOS header.
617 */
Ben Hutchings79bae422013-04-30 15:27:46 -0700618 memset(buf, 0, 16);
Matt Domsch3ed3bce2006-03-26 01:37:03 -0800619 for (q = p; q < p + 0x10000; q += 16) {
Ben Hutchings79bae422013-04-30 15:27:46 -0700620 memcpy_fromio(buf + 16, q, 16);
Ard Biesheuvelfc430262014-10-14 16:41:27 +0200621 if (!dmi_smbios3_present(buf) || !dmi_present(buf)) {
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200622 dmi_available = 1;
Ard Biesheuvelcf074402014-01-23 15:54:39 -0800623 dmi_early_unmap(p, 0x10000);
Ingo Molnar9a22b6e2008-09-18 12:50:18 +0200624 goto out;
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200625 }
Ben Hutchings79bae422013-04-30 15:27:46 -0700626 memcpy(buf, buf + 16, 16);
Andrey Panin61e032fa2005-09-06 15:18:26 -0700627 }
Ard Biesheuvelcf074402014-01-23 15:54:39 -0800628 dmi_early_unmap(p, 0x10000);
Andrey Panin61e032fa2005-09-06 15:18:26 -0700629 }
Ingo Molnar9a22b6e2008-09-18 12:50:18 +0200630 error:
Jean Delvare02d9c472013-09-11 14:24:08 -0700631 pr_info("DMI not present or invalid.\n");
Ingo Molnar9a22b6e2008-09-18 12:50:18 +0200632 out:
633 dmi_initialized = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634}
635
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636/**
Tejun Heo98e5e1b2013-04-30 15:27:15 -0700637 * dmi_set_dump_stack_arch_desc - set arch description for dump_stack()
638 *
639 * Invoke dump_stack_set_arch_desc() with DMI system information so that
640 * DMI identifiers are printed out on task dumps. Arch boot code should
641 * call this function after dmi_scan_machine() if it wants to print out DMI
642 * identifiers on task dumps.
643 */
644void __init dmi_set_dump_stack_arch_desc(void)
645{
646 dump_stack_set_arch_desc("%s", dmi_ids_string);
647}
648
649/**
Rafael J. Wysockid7b19562009-01-19 20:55:50 +0100650 * dmi_matches - check if dmi_system_id structure matches system DMI data
651 * @dmi: pointer to the dmi_system_id structure to check
652 */
653static bool dmi_matches(const struct dmi_system_id *dmi)
654{
655 int i;
656
657 WARN(!dmi_initialized, KERN_ERR "dmi check: not initialized yet.\n");
658
659 for (i = 0; i < ARRAY_SIZE(dmi->matches); i++) {
660 int s = dmi->matches[i].slot;
661 if (s == DMI_NONE)
Dmitry Torokhov75757502009-12-04 10:24:19 -0800662 break;
Jani Nikula5017b282013-07-03 15:05:02 -0700663 if (dmi_ident[s]) {
664 if (!dmi->matches[i].exact_match &&
665 strstr(dmi_ident[s], dmi->matches[i].substr))
666 continue;
667 else if (dmi->matches[i].exact_match &&
668 !strcmp(dmi_ident[s], dmi->matches[i].substr))
669 continue;
670 }
671
Rafael J. Wysockid7b19562009-01-19 20:55:50 +0100672 /* No match */
673 return false;
674 }
675 return true;
676}
677
678/**
Dmitry Torokhov75757502009-12-04 10:24:19 -0800679 * dmi_is_end_of_table - check for end-of-table marker
680 * @dmi: pointer to the dmi_system_id structure to check
681 */
682static bool dmi_is_end_of_table(const struct dmi_system_id *dmi)
683{
684 return dmi->matches[0].slot == DMI_NONE;
685}
686
687/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 * dmi_check_system - check system DMI data
689 * @list: array of dmi_system_id structures to match against
Randy Dunlapb0ef3712006-06-25 05:49:18 -0700690 * All non-null elements of the list must match
691 * their slot's (field index's) data (i.e., each
692 * list string must be a substring of the specified
693 * DMI slot's string data) to be considered a
694 * successful match.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 *
696 * Walk the blacklist table running matching functions until someone
697 * returns non zero or we hit the end. Callback function is called for
Randy Dunlapb0ef3712006-06-25 05:49:18 -0700698 * each successful match. Returns the number of matches.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 */
Jeff Garzik18552562007-10-03 15:15:40 -0400700int dmi_check_system(const struct dmi_system_id *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701{
Rafael J. Wysockid7b19562009-01-19 20:55:50 +0100702 int count = 0;
703 const struct dmi_system_id *d;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
Dmitry Torokhov75757502009-12-04 10:24:19 -0800705 for (d = list; !dmi_is_end_of_table(d); d++)
Rafael J. Wysockid7b19562009-01-19 20:55:50 +0100706 if (dmi_matches(d)) {
707 count++;
708 if (d->callback && d->callback(d))
709 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
712 return count;
713}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714EXPORT_SYMBOL(dmi_check_system);
715
716/**
Rafael J. Wysockid7b19562009-01-19 20:55:50 +0100717 * dmi_first_match - find dmi_system_id structure matching system DMI data
718 * @list: array of dmi_system_id structures to match against
719 * All non-null elements of the list must match
720 * their slot's (field index's) data (i.e., each
721 * list string must be a substring of the specified
722 * DMI slot's string data) to be considered a
723 * successful match.
724 *
725 * Walk the blacklist table until the first match is found. Return the
726 * pointer to the matching entry or NULL if there's no match.
727 */
728const struct dmi_system_id *dmi_first_match(const struct dmi_system_id *list)
729{
730 const struct dmi_system_id *d;
731
Dmitry Torokhov75757502009-12-04 10:24:19 -0800732 for (d = list; !dmi_is_end_of_table(d); d++)
Rafael J. Wysockid7b19562009-01-19 20:55:50 +0100733 if (dmi_matches(d))
734 return d;
735
736 return NULL;
737}
738EXPORT_SYMBOL(dmi_first_match);
739
740/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 * dmi_get_system_info - return DMI data value
Randy Dunlapb0ef3712006-06-25 05:49:18 -0700742 * @field: data index (see enum dmi_field)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 *
744 * Returns one DMI data value, can be used to perform
745 * complex DMI data checks.
746 */
Jeff Garzik18552562007-10-03 15:15:40 -0400747const char *dmi_get_system_info(int field)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748{
749 return dmi_ident[field];
750}
Dmitry Torokhove70c9d52005-06-25 14:54:25 -0700751EXPORT_SYMBOL(dmi_get_system_info);
Andrey Paninebad6a42005-09-06 15:18:29 -0700752
Alok Katariafd8cd7e2008-11-03 15:50:38 -0800753/**
Randy Dunlapc2bacfc2009-01-06 14:41:40 -0800754 * dmi_name_in_serial - Check if string is in the DMI product serial information
755 * @str: string to check for
Alok Katariafd8cd7e2008-11-03 15:50:38 -0800756 */
757int dmi_name_in_serial(const char *str)
758{
759 int f = DMI_PRODUCT_SERIAL;
760 if (dmi_ident[f] && strstr(dmi_ident[f], str))
761 return 1;
762 return 0;
763}
Andi Kleena1bae672006-10-21 18:37:02 +0200764
765/**
Jean Delvare66e13e62011-11-15 14:36:09 -0800766 * dmi_name_in_vendors - Check if string is in the DMI system or board vendor name
Jean Delvare02d9c472013-09-11 14:24:08 -0700767 * @str: Case sensitive Name
Andi Kleena1bae672006-10-21 18:37:02 +0200768 */
Jeff Garzik18552562007-10-03 15:15:40 -0400769int dmi_name_in_vendors(const char *str)
Andi Kleena1bae672006-10-21 18:37:02 +0200770{
Jean Delvare66e13e62011-11-15 14:36:09 -0800771 static int fields[] = { DMI_SYS_VENDOR, DMI_BOARD_VENDOR, DMI_NONE };
Andi Kleena1bae672006-10-21 18:37:02 +0200772 int i;
773 for (i = 0; fields[i] != DMI_NONE; i++) {
774 int f = fields[i];
775 if (dmi_ident[f] && strstr(dmi_ident[f], str))
776 return 1;
777 }
778 return 0;
779}
780EXPORT_SYMBOL(dmi_name_in_vendors);
781
Andrey Paninebad6a42005-09-06 15:18:29 -0700782/**
783 * dmi_find_device - find onboard device by type/name
784 * @type: device type or %DMI_DEV_TYPE_ANY to match all device types
Randy Dunlapb0ef3712006-06-25 05:49:18 -0700785 * @name: device name string or %NULL to match all
Andrey Paninebad6a42005-09-06 15:18:29 -0700786 * @from: previous device found in search, or %NULL for new search.
787 *
788 * Iterates through the list of known onboard devices. If a device is
789 * found with a matching @vendor and @device, a pointer to its device
790 * structure is returned. Otherwise, %NULL is returned.
Randy Dunlapb0ef3712006-06-25 05:49:18 -0700791 * A new search is initiated by passing %NULL as the @from argument.
Andrey Paninebad6a42005-09-06 15:18:29 -0700792 * If @from is not %NULL, searches continue from next device.
793 */
Jean Delvare02d9c472013-09-11 14:24:08 -0700794const struct dmi_device *dmi_find_device(int type, const char *name,
Jeff Garzik18552562007-10-03 15:15:40 -0400795 const struct dmi_device *from)
Andrey Paninebad6a42005-09-06 15:18:29 -0700796{
Jeff Garzik18552562007-10-03 15:15:40 -0400797 const struct list_head *head = from ? &from->list : &dmi_devices;
798 struct list_head *d;
Andrey Paninebad6a42005-09-06 15:18:29 -0700799
Jean Delvare02d9c472013-09-11 14:24:08 -0700800 for (d = head->next; d != &dmi_devices; d = d->next) {
Jeff Garzik18552562007-10-03 15:15:40 -0400801 const struct dmi_device *dev =
802 list_entry(d, struct dmi_device, list);
Andrey Paninebad6a42005-09-06 15:18:29 -0700803
804 if (((type == DMI_DEV_TYPE_ANY) || (dev->type == type)) &&
805 ((name == NULL) || (strcmp(dev->name, name) == 0)))
806 return dev;
807 }
808
809 return NULL;
810}
811EXPORT_SYMBOL(dmi_find_device);
Andi Kleenf083a322006-03-25 16:30:19 +0100812
813/**
Tejun Heo3e5cd1f2009-08-16 21:02:36 +0900814 * dmi_get_date - parse a DMI date
815 * @field: data index (see enum dmi_field)
816 * @yearp: optional out parameter for the year
817 * @monthp: optional out parameter for the month
818 * @dayp: optional out parameter for the day
Andi Kleenf083a322006-03-25 16:30:19 +0100819 *
Tejun Heo3e5cd1f2009-08-16 21:02:36 +0900820 * The date field is assumed to be in the form resembling
821 * [mm[/dd]]/yy[yy] and the result is stored in the out
822 * parameters any or all of which can be omitted.
823 *
824 * If the field doesn't exist, all out parameters are set to zero
825 * and false is returned. Otherwise, true is returned with any
826 * invalid part of date set to zero.
827 *
828 * On return, year, month and day are guaranteed to be in the
829 * range of [0,9999], [0,12] and [0,31] respectively.
Andi Kleenf083a322006-03-25 16:30:19 +0100830 */
Tejun Heo3e5cd1f2009-08-16 21:02:36 +0900831bool dmi_get_date(int field, int *yearp, int *monthp, int *dayp)
Andi Kleenf083a322006-03-25 16:30:19 +0100832{
Tejun Heo3e5cd1f2009-08-16 21:02:36 +0900833 int year = 0, month = 0, day = 0;
834 bool exists;
835 const char *s, *y;
Tejun Heo02c24fa2009-08-16 21:01:22 +0900836 char *e;
Andi Kleenf083a322006-03-25 16:30:19 +0100837
Tejun Heo3e5cd1f2009-08-16 21:02:36 +0900838 s = dmi_get_system_info(field);
839 exists = s;
840 if (!exists)
841 goto out;
Andi Kleenf083a322006-03-25 16:30:19 +0100842
Tejun Heo3e5cd1f2009-08-16 21:02:36 +0900843 /*
844 * Determine year first. We assume the date string resembles
845 * mm/dd/yy[yy] but the original code extracted only the year
846 * from the end. Keep the behavior in the spirit of no
847 * surprises.
848 */
849 y = strrchr(s, '/');
850 if (!y)
851 goto out;
852
853 y++;
854 year = simple_strtoul(y, &e, 10);
855 if (y != e && year < 100) { /* 2-digit year */
Andi Kleenf083a322006-03-25 16:30:19 +0100856 year += 1900;
857 if (year < 1996) /* no dates < spec 1.0 */
858 year += 100;
859 }
Tejun Heo3e5cd1f2009-08-16 21:02:36 +0900860 if (year > 9999) /* year should fit in %04d */
861 year = 0;
Andi Kleenf083a322006-03-25 16:30:19 +0100862
Tejun Heo3e5cd1f2009-08-16 21:02:36 +0900863 /* parse the mm and dd */
864 month = simple_strtoul(s, &e, 10);
865 if (s == e || *e != '/' || !month || month > 12) {
866 month = 0;
867 goto out;
868 }
869
870 s = e + 1;
871 day = simple_strtoul(s, &e, 10);
872 if (s == y || s == e || *e != '/' || day > 31)
873 day = 0;
874out:
875 if (yearp)
876 *yearp = year;
877 if (monthp)
878 *monthp = month;
879 if (dayp)
880 *dayp = day;
881 return exists;
Andi Kleenf083a322006-03-25 16:30:19 +0100882}
Tejun Heo3e5cd1f2009-08-16 21:02:36 +0900883EXPORT_SYMBOL(dmi_get_date);
Jean Delvare7fce0842007-11-03 17:29:20 +0100884
885/**
886 * dmi_walk - Walk the DMI table and get called back for every record
887 * @decode: Callback function
Jean Delvaree7a19c562009-03-30 21:46:44 +0200888 * @private_data: Private data to be passed to the callback function
Jean Delvare7fce0842007-11-03 17:29:20 +0100889 *
890 * Returns -1 when the DMI table can't be reached, 0 on success.
891 */
Jean Delvaree7a19c562009-03-30 21:46:44 +0200892int dmi_walk(void (*decode)(const struct dmi_header *, void *),
893 void *private_data)
Jean Delvare7fce0842007-11-03 17:29:20 +0100894{
895 u8 *buf;
896
897 if (!dmi_available)
898 return -1;
899
Ard Biesheuvelcf074402014-01-23 15:54:39 -0800900 buf = dmi_remap(dmi_base, dmi_len);
Jean Delvare7fce0842007-11-03 17:29:20 +0100901 if (buf == NULL)
902 return -1;
903
Jean Delvaree7a19c562009-03-30 21:46:44 +0200904 dmi_table(buf, dmi_len, dmi_num, decode, private_data);
Jean Delvare7fce0842007-11-03 17:29:20 +0100905
Ard Biesheuvelcf074402014-01-23 15:54:39 -0800906 dmi_unmap(buf);
Jean Delvare7fce0842007-11-03 17:29:20 +0100907 return 0;
908}
909EXPORT_SYMBOL_GPL(dmi_walk);
Jiri Slabyd61c72e2008-12-10 14:07:21 +0100910
911/**
912 * dmi_match - compare a string to the dmi field (if exists)
Randy Dunlapc2bacfc2009-01-06 14:41:40 -0800913 * @f: DMI field identifier
914 * @str: string to compare the DMI field to
Jiri Slabyd61c72e2008-12-10 14:07:21 +0100915 *
916 * Returns true if the requested field equals to the str (including NULL).
917 */
918bool dmi_match(enum dmi_field f, const char *str)
919{
920 const char *info = dmi_get_system_info(f);
921
922 if (info == NULL || str == NULL)
923 return info == str;
924
925 return !strcmp(info, str);
926}
927EXPORT_SYMBOL_GPL(dmi_match);
Chen, Gongdd6dad42013-10-18 14:29:25 -0700928
929void dmi_memdev_name(u16 handle, const char **bank, const char **device)
930{
931 int n;
932
933 if (dmi_memdev == NULL)
934 return;
935
936 for (n = 0; n < dmi_memdev_nr; n++) {
937 if (handle == dmi_memdev[n].handle) {
938 *bank = dmi_memdev[n].bank;
939 *device = dmi_memdev[n].device;
940 break;
941 }
942 }
943}
944EXPORT_SYMBOL_GPL(dmi_memdev_name);