blob: bba843c2b0acea0c8c8ebec49c1a56bb3ea9ddc6 [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
Ivan Khoronzhuk95be58d2015-02-18 13:33:20 +020020static u32 dmi_ver __initdata;
Ivan Khoronzhuk552e19d82015-02-18 13:33:21 +020021static u32 dmi_len;
22static u16 dmi_num;
Ingo Molnar9a22b6e2008-09-18 12:50:18 +020023/*
24 * Catch too early calls to dmi_check_system():
25 */
26static int dmi_initialized;
27
Tejun Heoc90fe6b2013-04-30 15:27:14 -070028/* DMI system identification string used during boot */
29static char dmi_ids_string[128] __initdata;
30
Chen, Gongdd6dad42013-10-18 14:29:25 -070031static struct dmi_memdev_info {
32 const char *device;
33 const char *bank;
34 u16 handle;
35} *dmi_memdev;
36static int dmi_memdev_nr;
37
Jean Delvaref3069ae2008-02-23 15:23:46 -080038static const char * __init dmi_string_nosave(const struct dmi_header *dm, u8 s)
Linus Torvalds1da177e2005-04-16 15:20:36 -070039{
Jeff Garzik18552562007-10-03 15:15:40 -040040 const u8 *bp = ((u8 *) dm) + dm->length;
Andrey Panin1249c512005-06-25 14:54:47 -070041
Andrey Paninc3c71202005-09-06 15:18:28 -070042 if (s) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 s--;
Andrey Paninc3c71202005-09-06 15:18:28 -070044 while (s > 0 && *bp) {
45 bp += strlen(bp) + 1;
46 s--;
47 }
48
49 if (*bp != 0) {
Parag Warudkar79da4722008-01-30 13:31:59 +010050 size_t len = strlen(bp)+1;
51 size_t cmp_len = len > 8 ? 8 : len;
52
53 if (!memcmp(bp, dmi_empty_string, cmp_len))
54 return dmi_empty_string;
Jean Delvaref3069ae2008-02-23 15:23:46 -080055 return bp;
Andrey Paninc3c71202005-09-06 15:18:28 -070056 }
Bjorn Helgaas4f705ae2006-04-03 17:09:22 -070057 }
Andrey Paninc3c71202005-09-06 15:18:28 -070058
Jean Delvaref3069ae2008-02-23 15:23:46 -080059 return "";
60}
61
Jean Delvareffbbb962013-09-11 14:24:09 -070062static const char * __init dmi_string(const struct dmi_header *dm, u8 s)
Jean Delvaref3069ae2008-02-23 15:23:46 -080063{
64 const char *bp = dmi_string_nosave(dm, s);
65 char *str;
66 size_t len;
67
68 if (bp == dmi_empty_string)
69 return dmi_empty_string;
70
71 len = strlen(bp) + 1;
72 str = dmi_alloc(len);
73 if (str != NULL)
74 strcpy(str, bp);
Jean Delvaref3069ae2008-02-23 15:23:46 -080075
Andrey Paninc3c71202005-09-06 15:18:28 -070076 return str;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077}
78
79/*
80 * We have to be cautious here. We have seen BIOSes with DMI pointers
81 * pointing to completely the wrong place for example
82 */
Ivan Khoronzhuk552e19d82015-02-18 13:33:21 +020083static void dmi_table(u8 *buf,
Jean Delvaree7a19c562009-03-30 21:46:44 +020084 void (*decode)(const struct dmi_header *, void *),
85 void *private_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086{
Jean Delvare7fce0842007-11-03 17:29:20 +010087 u8 *data = buf;
Andrey Panin1249c512005-06-25 14:54:47 -070088 int i = 0;
Bjorn Helgaas4f705ae2006-04-03 17:09:22 -070089
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 /*
Jean Delvarebfbaafa2015-03-20 09:59:47 +010091 * Stop when we have seen all the items the table claimed to have
Jean Delvare17cd5bd2015-06-25 09:06:55 +020092 * (SMBIOS < 3.0 only) OR we reach an end-of-table marker (SMBIOS
93 * >= 3.0 only) OR we run off the end of the table (should never
94 * happen but sometimes does on bogus implementations.)
Bjorn Helgaas4f705ae2006-04-03 17:09:22 -070095 */
Linus Torvalds9c65e122015-04-13 10:22:30 -070096 while ((!dmi_num || i < dmi_num) &&
97 (data - buf + sizeof(struct dmi_header)) <= dmi_len) {
Jeff Garzik18552562007-10-03 15:15:40 -040098 const struct dmi_header *dm = (const struct dmi_header *)data;
99
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 /*
Alan Cox86385452008-11-07 16:03:46 +0000101 * We want to know the total length (formatted area and
102 * strings) before decoding to make sure we won't run off the
103 * table in dmi_decode or dmi_string
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 */
Andrey Panin1249c512005-06-25 14:54:47 -0700105 data += dm->length;
Ivan Khoronzhuk552e19d82015-02-18 13:33:21 +0200106 while ((data - buf < dmi_len - 1) && (data[0] || data[1]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 data++;
Ivan Khoronzhuk552e19d82015-02-18 13:33:21 +0200108 if (data - buf < dmi_len - 1)
Jean Delvaree7a19c562009-03-30 21:46:44 +0200109 decode(dm, private_data);
Ivan Khoronzhukce204e92015-02-18 15:51:41 +0200110
111 /*
112 * 7.45 End-of-Table (Type 127) [SMBIOS reference spec v3.0.0]
Jean Delvare17cd5bd2015-06-25 09:06:55 +0200113 * For tables behind a 64-bit entry point, we have no item
114 * count and no exact table length, so stop on end-of-table
115 * marker. For tables behind a 32-bit entry point, we have
116 * seen OEM structures behind the end-of-table marker on
117 * some systems, so don't trust it.
Ivan Khoronzhukce204e92015-02-18 15:51:41 +0200118 */
Jean Delvare17cd5bd2015-06-25 09:06:55 +0200119 if (!dmi_num && dm->type == DMI_ENTRY_END_OF_TABLE)
Ivan Khoronzhukce204e92015-02-18 15:51:41 +0200120 break;
121
Andrey Panin1249c512005-06-25 14:54:47 -0700122 data += 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 i++;
124 }
Jean Delvare7fce0842007-11-03 17:29:20 +0100125}
126
Ard Biesheuvelfc430262014-10-14 16:41:27 +0200127static phys_addr_t dmi_base;
Jean Delvare7fce0842007-11-03 17:29:20 +0100128
Jean Delvaree7a19c562009-03-30 21:46:44 +0200129static int __init dmi_walk_early(void (*decode)(const struct dmi_header *,
130 void *))
Jean Delvare7fce0842007-11-03 17:29:20 +0100131{
132 u8 *buf;
133
Ard Biesheuvelcf074402014-01-23 15:54:39 -0800134 buf = dmi_early_remap(dmi_base, dmi_len);
Jean Delvare7fce0842007-11-03 17:29:20 +0100135 if (buf == NULL)
136 return -1;
137
Ivan Khoronzhuk552e19d82015-02-18 13:33:21 +0200138 dmi_table(buf, decode, NULL);
Jean Delvare7fce0842007-11-03 17:29:20 +0100139
Tony Luckd114a332012-07-20 13:15:20 -0700140 add_device_randomness(buf, dmi_len);
141
Ard Biesheuvelcf074402014-01-23 15:54:39 -0800142 dmi_early_unmap(buf, dmi_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 return 0;
144}
145
Zhenzhong Duan9f9c9cbb2012-12-20 15:05:14 -0800146static int __init dmi_checksum(const u8 *buf, u8 len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147{
Andrey Panin1249c512005-06-25 14:54:47 -0700148 u8 sum = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 int a;
Bjorn Helgaas4f705ae2006-04-03 17:09:22 -0700150
Zhenzhong Duan9f9c9cbb2012-12-20 15:05:14 -0800151 for (a = 0; a < len; a++)
Andrey Panin1249c512005-06-25 14:54:47 -0700152 sum += buf[a];
153
154 return sum == 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155}
156
Jean Delvareffbbb962013-09-11 14:24:09 -0700157static const char *dmi_ident[DMI_STRING_MAX];
Andrey Paninebad6a42005-09-06 15:18:29 -0700158static LIST_HEAD(dmi_devices);
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200159int dmi_available;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
161/*
162 * Save a DMI string
163 */
Jean Delvare02d9c472013-09-11 14:24:08 -0700164static void __init dmi_save_ident(const struct dmi_header *dm, int slot,
165 int string)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
Jean Delvare02d9c472013-09-11 14:24:08 -0700167 const char *d = (const char *) dm;
Jean Delvareffbbb962013-09-11 14:24:09 -0700168 const char *p;
Andrey Panin1249c512005-06-25 14:54:47 -0700169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 if (dmi_ident[slot])
171 return;
Andrey Panin1249c512005-06-25 14:54:47 -0700172
Andrey Paninc3c71202005-09-06 15:18:28 -0700173 p = dmi_string(dm, d[string]);
174 if (p == NULL)
175 return;
176
177 dmi_ident[slot] = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178}
179
Jean Delvare02d9c472013-09-11 14:24:08 -0700180static void __init dmi_save_uuid(const struct dmi_header *dm, int slot,
181 int index)
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200182{
Jean Delvare02d9c472013-09-11 14:24:08 -0700183 const u8 *d = (u8 *) dm + index;
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200184 char *s;
185 int is_ff = 1, is_00 = 1, i;
186
187 if (dmi_ident[slot])
188 return;
189
190 for (i = 0; i < 16 && (is_ff || is_00); i++) {
Zhenzhong Duanf1d8e612012-12-20 15:05:13 -0800191 if (d[i] != 0x00)
192 is_00 = 0;
193 if (d[i] != 0xFF)
194 is_ff = 0;
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200195 }
196
197 if (is_ff || is_00)
198 return;
199
200 s = dmi_alloc(16*2+4+1);
201 if (!s)
202 return;
203
Zhenzhong Duanf1d8e612012-12-20 15:05:13 -0800204 /*
205 * As of version 2.6 of the SMBIOS specification, the first 3 fields of
206 * the UUID are supposed to be little-endian encoded. The specification
207 * says that this is the defacto standard.
208 */
Ivan Khoronzhuk95be58d2015-02-18 13:33:20 +0200209 if (dmi_ver >= 0x020600)
Zhenzhong Duanf1d8e612012-12-20 15:05:13 -0800210 sprintf(s, "%pUL", d);
211 else
212 sprintf(s, "%pUB", d);
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200213
Jean Delvare02d9c472013-09-11 14:24:08 -0700214 dmi_ident[slot] = s;
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200215}
216
Jean Delvare02d9c472013-09-11 14:24:08 -0700217static void __init dmi_save_type(const struct dmi_header *dm, int slot,
218 int index)
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200219{
Jean Delvare02d9c472013-09-11 14:24:08 -0700220 const u8 *d = (u8 *) dm + index;
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200221 char *s;
222
223 if (dmi_ident[slot])
224 return;
225
226 s = dmi_alloc(4);
227 if (!s)
228 return;
229
230 sprintf(s, "%u", *d & 0x7F);
231 dmi_ident[slot] = s;
232}
233
Jean Delvaref3069ae2008-02-23 15:23:46 -0800234static void __init dmi_save_one_device(int type, const char *name)
235{
236 struct dmi_device *dev;
237
238 /* No duplicate device */
239 if (dmi_find_device(type, name, NULL))
240 return;
241
242 dev = dmi_alloc(sizeof(*dev) + strlen(name) + 1);
Jean Delvareae797442013-09-11 14:24:10 -0700243 if (!dev)
Jean Delvaref3069ae2008-02-23 15:23:46 -0800244 return;
Jean Delvaref3069ae2008-02-23 15:23:46 -0800245
246 dev->type = type;
247 strcpy((char *)(dev + 1), name);
248 dev->name = (char *)(dev + 1);
249 dev->device_data = NULL;
250 list_add(&dev->list, &dmi_devices);
251}
252
Jeff Garzik18552562007-10-03 15:15:40 -0400253static void __init dmi_save_devices(const struct dmi_header *dm)
Andrey Paninebad6a42005-09-06 15:18:29 -0700254{
255 int i, count = (dm->length - sizeof(struct dmi_header)) / 2;
Andrey Paninebad6a42005-09-06 15:18:29 -0700256
257 for (i = 0; i < count; i++) {
Jeff Garzik18552562007-10-03 15:15:40 -0400258 const char *d = (char *)(dm + 1) + (i * 2);
Andrey Paninebad6a42005-09-06 15:18:29 -0700259
260 /* Skip disabled device */
261 if ((*d & 0x80) == 0)
262 continue;
263
Jean Delvaref3069ae2008-02-23 15:23:46 -0800264 dmi_save_one_device(*d & 0x7f, dmi_string_nosave(dm, *(d + 1)));
Shem Multinymous2e0c1f62006-09-29 01:59:37 -0700265 }
266}
267
Jeff Garzik18552562007-10-03 15:15:40 -0400268static void __init dmi_save_oem_strings_devices(const struct dmi_header *dm)
Shem Multinymous2e0c1f62006-09-29 01:59:37 -0700269{
270 int i, count = *(u8 *)(dm + 1);
271 struct dmi_device *dev;
272
273 for (i = 1; i <= count; i++) {
Jean Delvareffbbb962013-09-11 14:24:09 -0700274 const char *devname = dmi_string(dm, i);
Parag Warudkar79da4722008-01-30 13:31:59 +0100275
Jean Delvare43fe1052008-02-23 15:23:55 -0800276 if (devname == dmi_empty_string)
Parag Warudkar79da4722008-01-30 13:31:59 +0100277 continue;
Parag Warudkar79da4722008-01-30 13:31:59 +0100278
Shem Multinymous2e0c1f62006-09-29 01:59:37 -0700279 dev = dmi_alloc(sizeof(*dev));
Jean Delvareae797442013-09-11 14:24:10 -0700280 if (!dev)
Shem Multinymous2e0c1f62006-09-29 01:59:37 -0700281 break;
Shem Multinymous2e0c1f62006-09-29 01:59:37 -0700282
283 dev->type = DMI_DEV_TYPE_OEM_STRING;
Parag Warudkar79da4722008-01-30 13:31:59 +0100284 dev->name = devname;
Shem Multinymous2e0c1f62006-09-29 01:59:37 -0700285 dev->device_data = NULL;
Andrey Paninebad6a42005-09-06 15:18:29 -0700286
287 list_add(&dev->list, &dmi_devices);
288 }
289}
290
Jeff Garzik18552562007-10-03 15:15:40 -0400291static void __init dmi_save_ipmi_device(const struct dmi_header *dm)
Andrey Paninebad6a42005-09-06 15:18:29 -0700292{
293 struct dmi_device *dev;
Jean Delvare02d9c472013-09-11 14:24:08 -0700294 void *data;
Andrey Paninebad6a42005-09-06 15:18:29 -0700295
Andi Kleene9928672006-01-11 22:43:33 +0100296 data = dmi_alloc(dm->length);
Jean Delvareae797442013-09-11 14:24:10 -0700297 if (data == NULL)
Andrey Paninebad6a42005-09-06 15:18:29 -0700298 return;
Andrey Paninebad6a42005-09-06 15:18:29 -0700299
300 memcpy(data, dm, dm->length);
301
Andi Kleene9928672006-01-11 22:43:33 +0100302 dev = dmi_alloc(sizeof(*dev));
Jean Delvareae797442013-09-11 14:24:10 -0700303 if (!dev)
Andrey Paninebad6a42005-09-06 15:18:29 -0700304 return;
Andrey Paninebad6a42005-09-06 15:18:29 -0700305
306 dev->type = DMI_DEV_TYPE_IPMI;
307 dev->name = "IPMI controller";
308 dev->device_data = data;
309
Carol Hebertabd24df2008-04-04 14:30:03 -0700310 list_add_tail(&dev->list, &dmi_devices);
Andrey Paninebad6a42005-09-06 15:18:29 -0700311}
312
Narendra K911e1c92010-07-26 05:56:50 -0500313static void __init dmi_save_dev_onboard(int instance, int segment, int bus,
314 int devfn, const char *name)
315{
316 struct dmi_dev_onboard *onboard_dev;
317
318 onboard_dev = dmi_alloc(sizeof(*onboard_dev) + strlen(name) + 1);
Jean Delvareae797442013-09-11 14:24:10 -0700319 if (!onboard_dev)
Narendra K911e1c92010-07-26 05:56:50 -0500320 return;
Jean Delvareae797442013-09-11 14:24:10 -0700321
Narendra K911e1c92010-07-26 05:56:50 -0500322 onboard_dev->instance = instance;
323 onboard_dev->segment = segment;
324 onboard_dev->bus = bus;
325 onboard_dev->devfn = devfn;
326
327 strcpy((char *)&onboard_dev[1], name);
328 onboard_dev->dev.type = DMI_DEV_TYPE_DEV_ONBOARD;
329 onboard_dev->dev.name = (char *)&onboard_dev[1];
330 onboard_dev->dev.device_data = onboard_dev;
331
332 list_add(&onboard_dev->dev.list, &dmi_devices);
333}
334
Wim Van Sebroeckb4bd7d52008-02-08 04:20:58 -0800335static void __init dmi_save_extended_devices(const struct dmi_header *dm)
336{
Jean Delvare02d9c472013-09-11 14:24:08 -0700337 const u8 *d = (u8 *) dm + 5;
Wim Van Sebroeckb4bd7d52008-02-08 04:20:58 -0800338
339 /* Skip disabled device */
340 if ((*d & 0x80) == 0)
341 return;
342
Narendra K911e1c92010-07-26 05:56:50 -0500343 dmi_save_dev_onboard(*(d+1), *(u16 *)(d+2), *(d+4), *(d+5),
344 dmi_string_nosave(dm, *(d-1)));
Jean Delvaref3069ae2008-02-23 15:23:46 -0800345 dmi_save_one_device(*d & 0x7f, dmi_string_nosave(dm, *(d - 1)));
Wim Van Sebroeckb4bd7d52008-02-08 04:20:58 -0800346}
347
Chen, Gongdd6dad42013-10-18 14:29:25 -0700348static void __init count_mem_devices(const struct dmi_header *dm, void *v)
349{
350 if (dm->type != DMI_ENTRY_MEM_DEVICE)
351 return;
352 dmi_memdev_nr++;
353}
354
355static void __init save_mem_devices(const struct dmi_header *dm, void *v)
356{
357 const char *d = (const char *)dm;
358 static int nr;
359
360 if (dm->type != DMI_ENTRY_MEM_DEVICE)
361 return;
362 if (nr >= dmi_memdev_nr) {
363 pr_warn(FW_BUG "Too many DIMM entries in SMBIOS table\n");
364 return;
365 }
Luck, Tony0841c042013-11-01 13:59:52 -0700366 dmi_memdev[nr].handle = get_unaligned(&dm->handle);
Chen, Gongdd6dad42013-10-18 14:29:25 -0700367 dmi_memdev[nr].device = dmi_string(dm, d[0x10]);
368 dmi_memdev[nr].bank = dmi_string(dm, d[0x11]);
369 nr++;
370}
371
372void __init dmi_memdev_walk(void)
373{
374 if (!dmi_available)
375 return;
376
377 if (dmi_walk_early(count_mem_devices) == 0 && dmi_memdev_nr) {
378 dmi_memdev = dmi_alloc(sizeof(*dmi_memdev) * dmi_memdev_nr);
379 if (dmi_memdev)
380 dmi_walk_early(save_mem_devices);
381 }
382}
383
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 * Process a DMI table entry. Right now all we care about are the BIOS
386 * and machine entries. For 2.5 we should pull the smbus controller info
387 * out of here.
388 */
Jean Delvaree7a19c562009-03-30 21:46:44 +0200389static void __init dmi_decode(const struct dmi_header *dm, void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390{
Jean Delvare02d9c472013-09-11 14:24:08 -0700391 switch (dm->type) {
Andrey Paninebad6a42005-09-06 15:18:29 -0700392 case 0: /* BIOS Information */
Andrey Panin1249c512005-06-25 14:54:47 -0700393 dmi_save_ident(dm, DMI_BIOS_VENDOR, 4);
Andrey Panin1249c512005-06-25 14:54:47 -0700394 dmi_save_ident(dm, DMI_BIOS_VERSION, 5);
Andrey Panin1249c512005-06-25 14:54:47 -0700395 dmi_save_ident(dm, DMI_BIOS_DATE, 8);
396 break;
Andrey Paninebad6a42005-09-06 15:18:29 -0700397 case 1: /* System Information */
Andrey Panin1249c512005-06-25 14:54:47 -0700398 dmi_save_ident(dm, DMI_SYS_VENDOR, 4);
Andrey Panin1249c512005-06-25 14:54:47 -0700399 dmi_save_ident(dm, DMI_PRODUCT_NAME, 5);
Andrey Panin1249c512005-06-25 14:54:47 -0700400 dmi_save_ident(dm, DMI_PRODUCT_VERSION, 6);
Andrey Panin1249c512005-06-25 14:54:47 -0700401 dmi_save_ident(dm, DMI_PRODUCT_SERIAL, 7);
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200402 dmi_save_uuid(dm, DMI_PRODUCT_UUID, 8);
Andrey Panin1249c512005-06-25 14:54:47 -0700403 break;
Andrey Paninebad6a42005-09-06 15:18:29 -0700404 case 2: /* Base Board Information */
Andrey Panin1249c512005-06-25 14:54:47 -0700405 dmi_save_ident(dm, DMI_BOARD_VENDOR, 4);
Andrey Panin1249c512005-06-25 14:54:47 -0700406 dmi_save_ident(dm, DMI_BOARD_NAME, 5);
Andrey Panin1249c512005-06-25 14:54:47 -0700407 dmi_save_ident(dm, DMI_BOARD_VERSION, 6);
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200408 dmi_save_ident(dm, DMI_BOARD_SERIAL, 7);
409 dmi_save_ident(dm, DMI_BOARD_ASSET_TAG, 8);
410 break;
411 case 3: /* Chassis Information */
412 dmi_save_ident(dm, DMI_CHASSIS_VENDOR, 4);
413 dmi_save_type(dm, DMI_CHASSIS_TYPE, 5);
414 dmi_save_ident(dm, DMI_CHASSIS_VERSION, 6);
415 dmi_save_ident(dm, DMI_CHASSIS_SERIAL, 7);
416 dmi_save_ident(dm, DMI_CHASSIS_ASSET_TAG, 8);
Andrey Panin1249c512005-06-25 14:54:47 -0700417 break;
Andrey Paninebad6a42005-09-06 15:18:29 -0700418 case 10: /* Onboard Devices Information */
419 dmi_save_devices(dm);
420 break;
Shem Multinymous2e0c1f62006-09-29 01:59:37 -0700421 case 11: /* OEM Strings */
422 dmi_save_oem_strings_devices(dm);
423 break;
Andrey Paninebad6a42005-09-06 15:18:29 -0700424 case 38: /* IPMI Device Information */
425 dmi_save_ipmi_device(dm);
Wim Van Sebroeckb4bd7d52008-02-08 04:20:58 -0800426 break;
427 case 41: /* Onboard Devices Extended Information */
428 dmi_save_extended_devices(dm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 }
430}
431
Tejun Heoc90fe6b2013-04-30 15:27:14 -0700432static int __init print_filtered(char *buf, size_t len, const char *info)
Bjorn Helgaas8881cdc2010-10-27 15:32:59 -0700433{
Tejun Heoc90fe6b2013-04-30 15:27:14 -0700434 int c = 0;
Bjorn Helgaas8881cdc2010-10-27 15:32:59 -0700435 const char *p;
436
437 if (!info)
Tejun Heoc90fe6b2013-04-30 15:27:14 -0700438 return c;
Bjorn Helgaas8881cdc2010-10-27 15:32:59 -0700439
440 for (p = info; *p; p++)
441 if (isprint(*p))
Tejun Heoc90fe6b2013-04-30 15:27:14 -0700442 c += scnprintf(buf + c, len - c, "%c", *p);
Bjorn Helgaas8881cdc2010-10-27 15:32:59 -0700443 else
Tejun Heoc90fe6b2013-04-30 15:27:14 -0700444 c += scnprintf(buf + c, len - c, "\\x%02x", *p & 0xff);
445 return c;
Bjorn Helgaas8881cdc2010-10-27 15:32:59 -0700446}
447
Tejun Heoc90fe6b2013-04-30 15:27:14 -0700448static void __init dmi_format_ids(char *buf, size_t len)
Bjorn Helgaas8881cdc2010-10-27 15:32:59 -0700449{
Tejun Heoc90fe6b2013-04-30 15:27:14 -0700450 int c = 0;
Naga Chumbalkar84e383b2011-02-14 22:47:17 +0000451 const char *board; /* Board Name is optional */
452
Tejun Heoc90fe6b2013-04-30 15:27:14 -0700453 c += print_filtered(buf + c, len - c,
454 dmi_get_system_info(DMI_SYS_VENDOR));
455 c += scnprintf(buf + c, len - c, " ");
456 c += print_filtered(buf + c, len - c,
457 dmi_get_system_info(DMI_PRODUCT_NAME));
458
Naga Chumbalkar84e383b2011-02-14 22:47:17 +0000459 board = dmi_get_system_info(DMI_BOARD_NAME);
460 if (board) {
Tejun Heoc90fe6b2013-04-30 15:27:14 -0700461 c += scnprintf(buf + c, len - c, "/");
462 c += print_filtered(buf + c, len - c, board);
Naga Chumbalkar84e383b2011-02-14 22:47:17 +0000463 }
Tejun Heoc90fe6b2013-04-30 15:27:14 -0700464 c += scnprintf(buf + c, len - c, ", BIOS ");
465 c += print_filtered(buf + c, len - c,
466 dmi_get_system_info(DMI_BIOS_VERSION));
467 c += scnprintf(buf + c, len - c, " ");
468 c += print_filtered(buf + c, len - c,
469 dmi_get_system_info(DMI_BIOS_DATE));
Bjorn Helgaas8881cdc2010-10-27 15:32:59 -0700470}
471
Ben Hutchingsd39de282013-07-31 13:53:30 -0700472/*
473 * Check for DMI/SMBIOS headers in the system firmware image. Any
474 * SMBIOS header must start 16 bytes before the DMI header, so take a
475 * 32 byte buffer and check for DMI at offset 16 and SMBIOS at offset
476 * 0. If the DMI header is present, set dmi_ver accordingly (SMBIOS
477 * takes precedence) and return 0. Otherwise return 1.
478 */
Ben Hutchings79bae422013-04-30 15:27:46 -0700479static int __init dmi_present(const u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480{
Ivan Khoronzhuk95be58d2015-02-18 13:33:20 +0200481 u32 smbios_ver;
Jeff Garzik18552562007-10-03 15:15:40 -0400482
Ben Hutchings79bae422013-04-30 15:27:46 -0700483 if (memcmp(buf, "_SM_", 4) == 0 &&
484 buf[5] < 32 && dmi_checksum(buf, buf[5])) {
Ard Biesheuvelfc430262014-10-14 16:41:27 +0200485 smbios_ver = get_unaligned_be16(buf + 6);
Ben Hutchings79bae422013-04-30 15:27:46 -0700486
487 /* Some BIOS report weird SMBIOS version, fix that up */
488 switch (smbios_ver) {
489 case 0x021F:
490 case 0x0221:
491 pr_debug("SMBIOS version fixup(2.%d->2.%d)\n",
492 smbios_ver & 0xFF, 3);
493 smbios_ver = 0x0203;
494 break;
495 case 0x0233:
496 pr_debug("SMBIOS version fixup(2.%d->2.%d)\n", 51, 6);
497 smbios_ver = 0x0206;
498 break;
499 }
500 } else {
501 smbios_ver = 0;
502 }
503
504 buf += 16;
505
506 if (memcmp(buf, "_DMI_", 5) == 0 && dmi_checksum(buf, 15)) {
Jean Delvare5c1ac562015-05-14 14:40:50 +0200507 if (smbios_ver)
508 dmi_ver = smbios_ver;
509 else
510 dmi_ver = (buf[14] & 0xF0) << 4 | (buf[14] & 0x0F);
Ard Biesheuvelfc430262014-10-14 16:41:27 +0200511 dmi_num = get_unaligned_le16(buf + 12);
512 dmi_len = get_unaligned_le16(buf + 6);
513 dmi_base = get_unaligned_le32(buf + 8);
Matt Domsch3ed3bce2006-03-26 01:37:03 -0800514
Bjorn Helgaas8881cdc2010-10-27 15:32:59 -0700515 if (dmi_walk_early(dmi_decode) == 0) {
Ben Hutchings79bae422013-04-30 15:27:46 -0700516 if (smbios_ver) {
Jean Delvarec2493042015-05-14 14:40:50 +0200517 pr_info("SMBIOS %d.%d present.\n",
518 dmi_ver >> 8, dmi_ver & 0xFF);
Ben Hutchings79bae422013-04-30 15:27:46 -0700519 } else {
Zhenzhong Duan9f9c9cbb2012-12-20 15:05:14 -0800520 pr_info("Legacy DMI %d.%d present.\n",
521 dmi_ver >> 8, dmi_ver & 0xFF);
522 }
Ivan Khoronzhuk95be58d2015-02-18 13:33:20 +0200523 dmi_ver <<= 8;
Tejun Heoc90fe6b2013-04-30 15:27:14 -0700524 dmi_format_ids(dmi_ids_string, sizeof(dmi_ids_string));
525 printk(KERN_DEBUG "DMI: %s\n", dmi_ids_string);
Matt Domsch3ed3bce2006-03-26 01:37:03 -0800526 return 0;
Bjorn Helgaas8881cdc2010-10-27 15:32:59 -0700527 }
Matt Domsch3ed3bce2006-03-26 01:37:03 -0800528 }
Matt Domsch3ed3bce2006-03-26 01:37:03 -0800529
Ben Hutchingsa40e7cf2013-03-08 12:43:32 -0800530 return 1;
Zhenzhong Duan9f9c9cbb2012-12-20 15:05:14 -0800531}
532
Ard Biesheuvelfc430262014-10-14 16:41:27 +0200533/*
534 * Check for the SMBIOS 3.0 64-bit entry point signature. Unlike the legacy
535 * 32-bit entry point, there is no embedded DMI header (_DMI_) in here.
536 */
537static int __init dmi_smbios3_present(const u8 *buf)
538{
539 if (memcmp(buf, "_SM3_", 5) == 0 &&
540 buf[6] < 32 && dmi_checksum(buf, buf[6])) {
Ivan Khoronzhuk95be58d2015-02-18 13:33:20 +0200541 dmi_ver = get_unaligned_be32(buf + 6);
542 dmi_ver &= 0xFFFFFF;
Jean Delvarebfbaafa2015-03-20 09:59:47 +0100543 dmi_num = 0; /* No longer specified */
Ard Biesheuvelfc430262014-10-14 16:41:27 +0200544 dmi_len = get_unaligned_le32(buf + 12);
545 dmi_base = get_unaligned_le64(buf + 16);
546
Ard Biesheuvelfc430262014-10-14 16:41:27 +0200547 if (dmi_walk_early(dmi_decode) == 0) {
Ivan Khoronzhuk95be58d2015-02-18 13:33:20 +0200548 pr_info("SMBIOS %d.%d.%d present.\n",
549 dmi_ver >> 16, (dmi_ver >> 8) & 0xFF,
550 dmi_ver & 0xFF);
Ard Biesheuvelfc430262014-10-14 16:41:27 +0200551 dmi_format_ids(dmi_ids_string, sizeof(dmi_ids_string));
552 pr_debug("DMI: %s\n", dmi_ids_string);
553 return 0;
554 }
555 }
556 return 1;
557}
558
Matt Domsch3ed3bce2006-03-26 01:37:03 -0800559void __init dmi_scan_machine(void)
560{
Andrey Panin61e032fa2005-09-06 15:18:26 -0700561 char __iomem *p, *q;
Ben Hutchings79bae422013-04-30 15:27:46 -0700562 char buf[32];
Andrey Panin61e032fa2005-09-06 15:18:26 -0700563
Matt Fleming83e68182012-11-14 09:42:35 +0000564 if (efi_enabled(EFI_CONFIG_TABLES)) {
Ard Biesheuvelfc430262014-10-14 16:41:27 +0200565 /*
566 * According to the DMTF SMBIOS reference spec v3.0.0, it is
567 * allowed to define both the 64-bit entry point (smbios3) and
568 * the 32-bit entry point (smbios), in which case they should
569 * either both point to the same SMBIOS structure table, or the
570 * table pointed to by the 64-bit entry point should contain a
571 * superset of the table contents pointed to by the 32-bit entry
572 * point (section 5.2)
573 * This implies that the 64-bit entry point should have
574 * precedence if it is defined and supported by the OS. If we
575 * have the 64-bit entry point, but fail to decode it, fall
576 * back to the legacy one (if available)
577 */
578 if (efi.smbios3 != EFI_INVALID_TABLE_ADDR) {
579 p = dmi_early_remap(efi.smbios3, 32);
580 if (p == NULL)
581 goto error;
582 memcpy_fromio(buf, p, 32);
583 dmi_early_unmap(p, 32);
584
585 if (!dmi_smbios3_present(buf)) {
586 dmi_available = 1;
587 goto out;
588 }
589 }
Bjorn Helgaasb2c99e32006-03-26 01:37:08 -0800590 if (efi.smbios == EFI_INVALID_TABLE_ADDR)
Ingo Molnar9a22b6e2008-09-18 12:50:18 +0200591 goto error;
Andrey Panin61e032fa2005-09-06 15:18:26 -0700592
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200593 /* This is called as a core_initcall() because it isn't
594 * needed during early boot. This also means we can
595 * iounmap the space when we're done with it.
596 */
Ard Biesheuvelcf074402014-01-23 15:54:39 -0800597 p = dmi_early_remap(efi.smbios, 32);
Matt Domsch3ed3bce2006-03-26 01:37:03 -0800598 if (p == NULL)
Ingo Molnar9a22b6e2008-09-18 12:50:18 +0200599 goto error;
Ben Hutchings79bae422013-04-30 15:27:46 -0700600 memcpy_fromio(buf, p, 32);
Ard Biesheuvelcf074402014-01-23 15:54:39 -0800601 dmi_early_unmap(p, 32);
Ben Hutchings79bae422013-04-30 15:27:46 -0700602
603 if (!dmi_present(buf)) {
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200604 dmi_available = 1;
Ingo Molnar9a22b6e2008-09-18 12:50:18 +0200605 goto out;
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200606 }
Ard Biesheuvelcf074402014-01-23 15:54:39 -0800607 } else if (IS_ENABLED(CONFIG_DMI_SCAN_MACHINE_NON_EFI_FALLBACK)) {
608 p = dmi_early_remap(0xF0000, 0x10000);
Matt Domsch3ed3bce2006-03-26 01:37:03 -0800609 if (p == NULL)
Ingo Molnar9a22b6e2008-09-18 12:50:18 +0200610 goto error;
Andrey Panin61e032fa2005-09-06 15:18:26 -0700611
Ben Hutchingsd39de282013-07-31 13:53:30 -0700612 /*
613 * Iterate over all possible DMI header addresses q.
614 * Maintain the 32 bytes around q in buf. On the
615 * first iteration, substitute zero for the
616 * out-of-range bytes so there is no chance of falsely
617 * detecting an SMBIOS header.
618 */
Ben Hutchings79bae422013-04-30 15:27:46 -0700619 memset(buf, 0, 16);
Matt Domsch3ed3bce2006-03-26 01:37:03 -0800620 for (q = p; q < p + 0x10000; q += 16) {
Ben Hutchings79bae422013-04-30 15:27:46 -0700621 memcpy_fromio(buf + 16, q, 16);
Ard Biesheuvelfc430262014-10-14 16:41:27 +0200622 if (!dmi_smbios3_present(buf) || !dmi_present(buf)) {
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200623 dmi_available = 1;
Ard Biesheuvelcf074402014-01-23 15:54:39 -0800624 dmi_early_unmap(p, 0x10000);
Ingo Molnar9a22b6e2008-09-18 12:50:18 +0200625 goto out;
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200626 }
Ben Hutchings79bae422013-04-30 15:27:46 -0700627 memcpy(buf, buf + 16, 16);
Andrey Panin61e032fa2005-09-06 15:18:26 -0700628 }
Ard Biesheuvelcf074402014-01-23 15:54:39 -0800629 dmi_early_unmap(p, 0x10000);
Andrey Panin61e032fa2005-09-06 15:18:26 -0700630 }
Ingo Molnar9a22b6e2008-09-18 12:50:18 +0200631 error:
Jean Delvare02d9c472013-09-11 14:24:08 -0700632 pr_info("DMI not present or invalid.\n");
Ingo Molnar9a22b6e2008-09-18 12:50:18 +0200633 out:
634 dmi_initialized = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635}
636
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637/**
Tejun Heo98e5e1b2013-04-30 15:27:15 -0700638 * dmi_set_dump_stack_arch_desc - set arch description for dump_stack()
639 *
640 * Invoke dump_stack_set_arch_desc() with DMI system information so that
641 * DMI identifiers are printed out on task dumps. Arch boot code should
642 * call this function after dmi_scan_machine() if it wants to print out DMI
643 * identifiers on task dumps.
644 */
645void __init dmi_set_dump_stack_arch_desc(void)
646{
647 dump_stack_set_arch_desc("%s", dmi_ids_string);
648}
649
650/**
Rafael J. Wysockid7b19562009-01-19 20:55:50 +0100651 * dmi_matches - check if dmi_system_id structure matches system DMI data
652 * @dmi: pointer to the dmi_system_id structure to check
653 */
654static bool dmi_matches(const struct dmi_system_id *dmi)
655{
656 int i;
657
658 WARN(!dmi_initialized, KERN_ERR "dmi check: not initialized yet.\n");
659
660 for (i = 0; i < ARRAY_SIZE(dmi->matches); i++) {
661 int s = dmi->matches[i].slot;
662 if (s == DMI_NONE)
Dmitry Torokhov75757502009-12-04 10:24:19 -0800663 break;
Jani Nikula5017b282013-07-03 15:05:02 -0700664 if (dmi_ident[s]) {
665 if (!dmi->matches[i].exact_match &&
666 strstr(dmi_ident[s], dmi->matches[i].substr))
667 continue;
668 else if (dmi->matches[i].exact_match &&
669 !strcmp(dmi_ident[s], dmi->matches[i].substr))
670 continue;
671 }
672
Rafael J. Wysockid7b19562009-01-19 20:55:50 +0100673 /* No match */
674 return false;
675 }
676 return true;
677}
678
679/**
Dmitry Torokhov75757502009-12-04 10:24:19 -0800680 * dmi_is_end_of_table - check for end-of-table marker
681 * @dmi: pointer to the dmi_system_id structure to check
682 */
683static bool dmi_is_end_of_table(const struct dmi_system_id *dmi)
684{
685 return dmi->matches[0].slot == DMI_NONE;
686}
687
688/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 * dmi_check_system - check system DMI data
690 * @list: array of dmi_system_id structures to match against
Randy Dunlapb0ef3712006-06-25 05:49:18 -0700691 * All non-null elements of the list must match
692 * their slot's (field index's) data (i.e., each
693 * list string must be a substring of the specified
694 * DMI slot's string data) to be considered a
695 * successful match.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 *
697 * Walk the blacklist table running matching functions until someone
698 * returns non zero or we hit the end. Callback function is called for
Randy Dunlapb0ef3712006-06-25 05:49:18 -0700699 * each successful match. Returns the number of matches.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 */
Jeff Garzik18552562007-10-03 15:15:40 -0400701int dmi_check_system(const struct dmi_system_id *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702{
Rafael J. Wysockid7b19562009-01-19 20:55:50 +0100703 int count = 0;
704 const struct dmi_system_id *d;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
Dmitry Torokhov75757502009-12-04 10:24:19 -0800706 for (d = list; !dmi_is_end_of_table(d); d++)
Rafael J. Wysockid7b19562009-01-19 20:55:50 +0100707 if (dmi_matches(d)) {
708 count++;
709 if (d->callback && d->callback(d))
710 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
713 return count;
714}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715EXPORT_SYMBOL(dmi_check_system);
716
717/**
Rafael J. Wysockid7b19562009-01-19 20:55:50 +0100718 * dmi_first_match - find dmi_system_id structure matching system DMI data
719 * @list: array of dmi_system_id structures to match against
720 * All non-null elements of the list must match
721 * their slot's (field index's) data (i.e., each
722 * list string must be a substring of the specified
723 * DMI slot's string data) to be considered a
724 * successful match.
725 *
726 * Walk the blacklist table until the first match is found. Return the
727 * pointer to the matching entry or NULL if there's no match.
728 */
729const struct dmi_system_id *dmi_first_match(const struct dmi_system_id *list)
730{
731 const struct dmi_system_id *d;
732
Dmitry Torokhov75757502009-12-04 10:24:19 -0800733 for (d = list; !dmi_is_end_of_table(d); d++)
Rafael J. Wysockid7b19562009-01-19 20:55:50 +0100734 if (dmi_matches(d))
735 return d;
736
737 return NULL;
738}
739EXPORT_SYMBOL(dmi_first_match);
740
741/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 * dmi_get_system_info - return DMI data value
Randy Dunlapb0ef3712006-06-25 05:49:18 -0700743 * @field: data index (see enum dmi_field)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 *
745 * Returns one DMI data value, can be used to perform
746 * complex DMI data checks.
747 */
Jeff Garzik18552562007-10-03 15:15:40 -0400748const char *dmi_get_system_info(int field)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749{
750 return dmi_ident[field];
751}
Dmitry Torokhove70c9d52005-06-25 14:54:25 -0700752EXPORT_SYMBOL(dmi_get_system_info);
Andrey Paninebad6a42005-09-06 15:18:29 -0700753
Alok Katariafd8cd7e2008-11-03 15:50:38 -0800754/**
Randy Dunlapc2bacfc2009-01-06 14:41:40 -0800755 * dmi_name_in_serial - Check if string is in the DMI product serial information
756 * @str: string to check for
Alok Katariafd8cd7e2008-11-03 15:50:38 -0800757 */
758int dmi_name_in_serial(const char *str)
759{
760 int f = DMI_PRODUCT_SERIAL;
761 if (dmi_ident[f] && strstr(dmi_ident[f], str))
762 return 1;
763 return 0;
764}
Andi Kleena1bae672006-10-21 18:37:02 +0200765
766/**
Jean Delvare66e13e62011-11-15 14:36:09 -0800767 * dmi_name_in_vendors - Check if string is in the DMI system or board vendor name
Jean Delvare02d9c472013-09-11 14:24:08 -0700768 * @str: Case sensitive Name
Andi Kleena1bae672006-10-21 18:37:02 +0200769 */
Jeff Garzik18552562007-10-03 15:15:40 -0400770int dmi_name_in_vendors(const char *str)
Andi Kleena1bae672006-10-21 18:37:02 +0200771{
Jean Delvare66e13e62011-11-15 14:36:09 -0800772 static int fields[] = { DMI_SYS_VENDOR, DMI_BOARD_VENDOR, DMI_NONE };
Andi Kleena1bae672006-10-21 18:37:02 +0200773 int i;
774 for (i = 0; fields[i] != DMI_NONE; i++) {
775 int f = fields[i];
776 if (dmi_ident[f] && strstr(dmi_ident[f], str))
777 return 1;
778 }
779 return 0;
780}
781EXPORT_SYMBOL(dmi_name_in_vendors);
782
Andrey Paninebad6a42005-09-06 15:18:29 -0700783/**
784 * dmi_find_device - find onboard device by type/name
785 * @type: device type or %DMI_DEV_TYPE_ANY to match all device types
Randy Dunlapb0ef3712006-06-25 05:49:18 -0700786 * @name: device name string or %NULL to match all
Andrey Paninebad6a42005-09-06 15:18:29 -0700787 * @from: previous device found in search, or %NULL for new search.
788 *
789 * Iterates through the list of known onboard devices. If a device is
790 * found with a matching @vendor and @device, a pointer to its device
791 * structure is returned. Otherwise, %NULL is returned.
Randy Dunlapb0ef3712006-06-25 05:49:18 -0700792 * A new search is initiated by passing %NULL as the @from argument.
Andrey Paninebad6a42005-09-06 15:18:29 -0700793 * If @from is not %NULL, searches continue from next device.
794 */
Jean Delvare02d9c472013-09-11 14:24:08 -0700795const struct dmi_device *dmi_find_device(int type, const char *name,
Jeff Garzik18552562007-10-03 15:15:40 -0400796 const struct dmi_device *from)
Andrey Paninebad6a42005-09-06 15:18:29 -0700797{
Jeff Garzik18552562007-10-03 15:15:40 -0400798 const struct list_head *head = from ? &from->list : &dmi_devices;
799 struct list_head *d;
Andrey Paninebad6a42005-09-06 15:18:29 -0700800
Jean Delvare02d9c472013-09-11 14:24:08 -0700801 for (d = head->next; d != &dmi_devices; d = d->next) {
Jeff Garzik18552562007-10-03 15:15:40 -0400802 const struct dmi_device *dev =
803 list_entry(d, struct dmi_device, list);
Andrey Paninebad6a42005-09-06 15:18:29 -0700804
805 if (((type == DMI_DEV_TYPE_ANY) || (dev->type == type)) &&
806 ((name == NULL) || (strcmp(dev->name, name) == 0)))
807 return dev;
808 }
809
810 return NULL;
811}
812EXPORT_SYMBOL(dmi_find_device);
Andi Kleenf083a322006-03-25 16:30:19 +0100813
814/**
Tejun Heo3e5cd1f2009-08-16 21:02:36 +0900815 * dmi_get_date - parse a DMI date
816 * @field: data index (see enum dmi_field)
817 * @yearp: optional out parameter for the year
818 * @monthp: optional out parameter for the month
819 * @dayp: optional out parameter for the day
Andi Kleenf083a322006-03-25 16:30:19 +0100820 *
Tejun Heo3e5cd1f2009-08-16 21:02:36 +0900821 * The date field is assumed to be in the form resembling
822 * [mm[/dd]]/yy[yy] and the result is stored in the out
823 * parameters any or all of which can be omitted.
824 *
825 * If the field doesn't exist, all out parameters are set to zero
826 * and false is returned. Otherwise, true is returned with any
827 * invalid part of date set to zero.
828 *
829 * On return, year, month and day are guaranteed to be in the
830 * range of [0,9999], [0,12] and [0,31] respectively.
Andi Kleenf083a322006-03-25 16:30:19 +0100831 */
Tejun Heo3e5cd1f2009-08-16 21:02:36 +0900832bool dmi_get_date(int field, int *yearp, int *monthp, int *dayp)
Andi Kleenf083a322006-03-25 16:30:19 +0100833{
Tejun Heo3e5cd1f2009-08-16 21:02:36 +0900834 int year = 0, month = 0, day = 0;
835 bool exists;
836 const char *s, *y;
Tejun Heo02c24fa2009-08-16 21:01:22 +0900837 char *e;
Andi Kleenf083a322006-03-25 16:30:19 +0100838
Tejun Heo3e5cd1f2009-08-16 21:02:36 +0900839 s = dmi_get_system_info(field);
840 exists = s;
841 if (!exists)
842 goto out;
Andi Kleenf083a322006-03-25 16:30:19 +0100843
Tejun Heo3e5cd1f2009-08-16 21:02:36 +0900844 /*
845 * Determine year first. We assume the date string resembles
846 * mm/dd/yy[yy] but the original code extracted only the year
847 * from the end. Keep the behavior in the spirit of no
848 * surprises.
849 */
850 y = strrchr(s, '/');
851 if (!y)
852 goto out;
853
854 y++;
855 year = simple_strtoul(y, &e, 10);
856 if (y != e && year < 100) { /* 2-digit year */
Andi Kleenf083a322006-03-25 16:30:19 +0100857 year += 1900;
858 if (year < 1996) /* no dates < spec 1.0 */
859 year += 100;
860 }
Tejun Heo3e5cd1f2009-08-16 21:02:36 +0900861 if (year > 9999) /* year should fit in %04d */
862 year = 0;
Andi Kleenf083a322006-03-25 16:30:19 +0100863
Tejun Heo3e5cd1f2009-08-16 21:02:36 +0900864 /* parse the mm and dd */
865 month = simple_strtoul(s, &e, 10);
866 if (s == e || *e != '/' || !month || month > 12) {
867 month = 0;
868 goto out;
869 }
870
871 s = e + 1;
872 day = simple_strtoul(s, &e, 10);
873 if (s == y || s == e || *e != '/' || day > 31)
874 day = 0;
875out:
876 if (yearp)
877 *yearp = year;
878 if (monthp)
879 *monthp = month;
880 if (dayp)
881 *dayp = day;
882 return exists;
Andi Kleenf083a322006-03-25 16:30:19 +0100883}
Tejun Heo3e5cd1f2009-08-16 21:02:36 +0900884EXPORT_SYMBOL(dmi_get_date);
Jean Delvare7fce0842007-11-03 17:29:20 +0100885
886/**
887 * dmi_walk - Walk the DMI table and get called back for every record
888 * @decode: Callback function
Jean Delvaree7a19c562009-03-30 21:46:44 +0200889 * @private_data: Private data to be passed to the callback function
Jean Delvare7fce0842007-11-03 17:29:20 +0100890 *
891 * Returns -1 when the DMI table can't be reached, 0 on success.
892 */
Jean Delvaree7a19c562009-03-30 21:46:44 +0200893int dmi_walk(void (*decode)(const struct dmi_header *, void *),
894 void *private_data)
Jean Delvare7fce0842007-11-03 17:29:20 +0100895{
896 u8 *buf;
897
898 if (!dmi_available)
899 return -1;
900
Ard Biesheuvelcf074402014-01-23 15:54:39 -0800901 buf = dmi_remap(dmi_base, dmi_len);
Jean Delvare7fce0842007-11-03 17:29:20 +0100902 if (buf == NULL)
903 return -1;
904
Ivan Khoronzhuk552e19d82015-02-18 13:33:21 +0200905 dmi_table(buf, decode, private_data);
Jean Delvare7fce0842007-11-03 17:29:20 +0100906
Ard Biesheuvelcf074402014-01-23 15:54:39 -0800907 dmi_unmap(buf);
Jean Delvare7fce0842007-11-03 17:29:20 +0100908 return 0;
909}
910EXPORT_SYMBOL_GPL(dmi_walk);
Jiri Slabyd61c72e2008-12-10 14:07:21 +0100911
912/**
913 * dmi_match - compare a string to the dmi field (if exists)
Randy Dunlapc2bacfc2009-01-06 14:41:40 -0800914 * @f: DMI field identifier
915 * @str: string to compare the DMI field to
Jiri Slabyd61c72e2008-12-10 14:07:21 +0100916 *
917 * Returns true if the requested field equals to the str (including NULL).
918 */
919bool dmi_match(enum dmi_field f, const char *str)
920{
921 const char *info = dmi_get_system_info(f);
922
923 if (info == NULL || str == NULL)
924 return info == str;
925
926 return !strcmp(info, str);
927}
928EXPORT_SYMBOL_GPL(dmi_match);
Chen, Gongdd6dad42013-10-18 14:29:25 -0700929
930void dmi_memdev_name(u16 handle, const char **bank, const char **device)
931{
932 int n;
933
934 if (dmi_memdev == NULL)
935 return;
936
937 for (n = 0; n < dmi_memdev_nr; n++) {
938 if (handle == dmi_memdev[n].handle) {
939 *bank = dmi_memdev[n].bank;
940 *device = dmi_memdev[n].device;
941 break;
942 }
943 }
944}
945EXPORT_SYMBOL_GPL(dmi_memdev_name);