blob: c3be8ef9243f60c81f0a03cb2a6d18524b4de291 [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
Ivan Khoronzhukd7f96f972015-06-25 09:06:56 +020013struct kobject *dmi_kobj;
14EXPORT_SYMBOL_GPL(dmi_kobj);
15
Paul Jacksoncb5dd7c2008-05-14 08:15:16 -070016/*
17 * DMI stands for "Desktop Management Interface". It is part
18 * of and an antecedent to, SMBIOS, which stands for System
19 * Management BIOS. See further: http://www.dmtf.org/standards
20 */
Jean Delvarea7770ae2018-02-03 11:25:20 +010021static const char dmi_empty_string[] = "";
Parag Warudkar79da4722008-01-30 13:31:59 +010022
Ivan Khoronzhuk95be58d2015-02-18 13:33:20 +020023static u32 dmi_ver __initdata;
Ivan Khoronzhuk552e19d82015-02-18 13:33:21 +020024static u32 dmi_len;
25static u16 dmi_num;
Ivan Khoronzhukd7f96f972015-06-25 09:06:56 +020026static u8 smbios_entry_point[32];
27static int smbios_entry_point_size;
28
Tejun Heoc90fe6b2013-04-30 15:27:14 -070029/* DMI system identification string used during boot */
30static char dmi_ids_string[128] __initdata;
31
Chen, Gongdd6dad42013-10-18 14:29:25 -070032static struct dmi_memdev_info {
33 const char *device;
34 const char *bank;
35 u16 handle;
36} *dmi_memdev;
37static int dmi_memdev_nr;
38
Jean Delvaref3069ae2008-02-23 15:23:46 -080039static const char * __init dmi_string_nosave(const struct dmi_header *dm, u8 s)
Linus Torvalds1da177e2005-04-16 15:20:36 -070040{
Jeff Garzik18552562007-10-03 15:15:40 -040041 const u8 *bp = ((u8 *) dm) + dm->length;
Jean Delvarea7770ae2018-02-03 11:25:20 +010042 const u8 *nsp;
Andrey Panin1249c512005-06-25 14:54:47 -070043
Andrey Paninc3c71202005-09-06 15:18:28 -070044 if (s) {
Jean Delvarea7770ae2018-02-03 11:25:20 +010045 while (--s > 0 && *bp)
Andrey Paninc3c71202005-09-06 15:18:28 -070046 bp += strlen(bp) + 1;
Andrey Paninc3c71202005-09-06 15:18:28 -070047
Jean Delvarea7770ae2018-02-03 11:25:20 +010048 /* Strings containing only spaces are considered empty */
49 nsp = bp;
50 while (*nsp == ' ')
51 nsp++;
52 if (*nsp != '\0')
Jean Delvaref3069ae2008-02-23 15:23:46 -080053 return bp;
Bjorn Helgaas4f705ae2006-04-03 17:09:22 -070054 }
Andrey Paninc3c71202005-09-06 15:18:28 -070055
Jean Delvarea7770ae2018-02-03 11:25:20 +010056 return dmi_empty_string;
Jean Delvaref3069ae2008-02-23 15:23:46 -080057}
58
Jean Delvareffbbb962013-09-11 14:24:09 -070059static const char * __init dmi_string(const struct dmi_header *dm, u8 s)
Jean Delvaref3069ae2008-02-23 15:23:46 -080060{
61 const char *bp = dmi_string_nosave(dm, s);
62 char *str;
63 size_t len;
64
65 if (bp == dmi_empty_string)
66 return dmi_empty_string;
67
68 len = strlen(bp) + 1;
69 str = dmi_alloc(len);
70 if (str != NULL)
71 strcpy(str, bp);
Jean Delvaref3069ae2008-02-23 15:23:46 -080072
Andrey Paninc3c71202005-09-06 15:18:28 -070073 return str;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074}
75
76/*
77 * We have to be cautious here. We have seen BIOSes with DMI pointers
78 * pointing to completely the wrong place for example
79 */
Ivan Khoronzhukeb4c5ea2015-06-25 09:06:56 +020080static void dmi_decode_table(u8 *buf,
81 void (*decode)(const struct dmi_header *, void *),
82 void *private_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070083{
Jean Delvare7fce0842007-11-03 17:29:20 +010084 u8 *data = buf;
Andrey Panin1249c512005-06-25 14:54:47 -070085 int i = 0;
Bjorn Helgaas4f705ae2006-04-03 17:09:22 -070086
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 /*
Jean Delvarebfbaafa2015-03-20 09:59:47 +010088 * Stop when we have seen all the items the table claimed to have
Jean Delvare17cd5bd2015-06-25 09:06:55 +020089 * (SMBIOS < 3.0 only) OR we reach an end-of-table marker (SMBIOS
90 * >= 3.0 only) OR we run off the end of the table (should never
91 * happen but sometimes does on bogus implementations.)
Bjorn Helgaas4f705ae2006-04-03 17:09:22 -070092 */
Linus Torvalds9c65e122015-04-13 10:22:30 -070093 while ((!dmi_num || i < dmi_num) &&
94 (data - buf + sizeof(struct dmi_header)) <= dmi_len) {
Jeff Garzik18552562007-10-03 15:15:40 -040095 const struct dmi_header *dm = (const struct dmi_header *)data;
96
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 /*
Alan Cox86385452008-11-07 16:03:46 +000098 * We want to know the total length (formatted area and
99 * strings) before decoding to make sure we won't run off the
100 * table in dmi_decode or dmi_string
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 */
Andrey Panin1249c512005-06-25 14:54:47 -0700102 data += dm->length;
Ivan Khoronzhuk552e19d82015-02-18 13:33:21 +0200103 while ((data - buf < dmi_len - 1) && (data[0] || data[1]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 data++;
Ivan Khoronzhuk552e19d82015-02-18 13:33:21 +0200105 if (data - buf < dmi_len - 1)
Jean Delvaree7a19c562009-03-30 21:46:44 +0200106 decode(dm, private_data);
Ivan Khoronzhukce204e92015-02-18 15:51:41 +0200107
Jean Delvare6e0ad592015-06-25 09:06:56 +0200108 data += 2;
109 i++;
110
Ivan Khoronzhukce204e92015-02-18 15:51:41 +0200111 /*
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 }
Jean Delvare6e0ad592015-06-25 09:06:56 +0200122
123 /* Trim DMI table length if needed */
124 if (dmi_len > data - buf)
125 dmi_len = data - buf;
Jean Delvare7fce0842007-11-03 17:29:20 +0100126}
127
Ard Biesheuvelfc430262014-10-14 16:41:27 +0200128static phys_addr_t dmi_base;
Jean Delvare7fce0842007-11-03 17:29:20 +0100129
Jean Delvaree7a19c562009-03-30 21:46:44 +0200130static int __init dmi_walk_early(void (*decode)(const struct dmi_header *,
131 void *))
Jean Delvare7fce0842007-11-03 17:29:20 +0100132{
133 u8 *buf;
Jean Delvare6e0ad592015-06-25 09:06:56 +0200134 u32 orig_dmi_len = dmi_len;
Jean Delvare7fce0842007-11-03 17:29:20 +0100135
Jean Delvare6e0ad592015-06-25 09:06:56 +0200136 buf = dmi_early_remap(dmi_base, orig_dmi_len);
Jean Delvare7fce0842007-11-03 17:29:20 +0100137 if (buf == NULL)
Andy Lutomirskic9268202017-06-15 13:46:00 +0200138 return -ENOMEM;
Jean Delvare7fce0842007-11-03 17:29:20 +0100139
Ivan Khoronzhukeb4c5ea2015-06-25 09:06:56 +0200140 dmi_decode_table(buf, decode, NULL);
Jean Delvare7fce0842007-11-03 17:29:20 +0100141
Tony Luckd114a332012-07-20 13:15:20 -0700142 add_device_randomness(buf, dmi_len);
143
Jean Delvare6e0ad592015-06-25 09:06:56 +0200144 dmi_early_unmap(buf, orig_dmi_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 return 0;
146}
147
Zhenzhong Duan9f9c9cbb2012-12-20 15:05:14 -0800148static int __init dmi_checksum(const u8 *buf, u8 len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149{
Andrey Panin1249c512005-06-25 14:54:47 -0700150 u8 sum = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 int a;
Bjorn Helgaas4f705ae2006-04-03 17:09:22 -0700152
Zhenzhong Duan9f9c9cbb2012-12-20 15:05:14 -0800153 for (a = 0; a < len; a++)
Andrey Panin1249c512005-06-25 14:54:47 -0700154 sum += buf[a];
155
156 return sum == 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157}
158
Jean Delvareffbbb962013-09-11 14:24:09 -0700159static const char *dmi_ident[DMI_STRING_MAX];
Andrey Paninebad6a42005-09-06 15:18:29 -0700160static LIST_HEAD(dmi_devices);
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200161int dmi_available;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
163/*
164 * Save a DMI string
165 */
Jean Delvare02d9c472013-09-11 14:24:08 -0700166static void __init dmi_save_ident(const struct dmi_header *dm, int slot,
167 int string)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168{
Jean Delvare02d9c472013-09-11 14:24:08 -0700169 const char *d = (const char *) dm;
Jean Delvareffbbb962013-09-11 14:24:09 -0700170 const char *p;
Andrey Panin1249c512005-06-25 14:54:47 -0700171
Jean Delvarea814c352017-06-15 13:46:01 +0200172 if (dmi_ident[slot] || dm->length <= string)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 return;
Andrey Panin1249c512005-06-25 14:54:47 -0700174
Andrey Paninc3c71202005-09-06 15:18:28 -0700175 p = dmi_string(dm, d[string]);
176 if (p == NULL)
177 return;
178
179 dmi_ident[slot] = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180}
181
Jean Delvare02d9c472013-09-11 14:24:08 -0700182static void __init dmi_save_uuid(const struct dmi_header *dm, int slot,
183 int index)
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200184{
Jean Delvarea814c352017-06-15 13:46:01 +0200185 const u8 *d;
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200186 char *s;
187 int is_ff = 1, is_00 = 1, i;
188
Jean Delvare90fe6f82018-04-13 15:37:59 +0200189 if (dmi_ident[slot] || dm->length < index + 16)
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200190 return;
191
Jean Delvarea814c352017-06-15 13:46:01 +0200192 d = (u8 *) dm + index;
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200193 for (i = 0; i < 16 && (is_ff || is_00); i++) {
Zhenzhong Duanf1d8e612012-12-20 15:05:13 -0800194 if (d[i] != 0x00)
195 is_00 = 0;
196 if (d[i] != 0xFF)
197 is_ff = 0;
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200198 }
199
200 if (is_ff || is_00)
201 return;
202
203 s = dmi_alloc(16*2+4+1);
204 if (!s)
205 return;
206
Zhenzhong Duanf1d8e612012-12-20 15:05:13 -0800207 /*
208 * As of version 2.6 of the SMBIOS specification, the first 3 fields of
209 * the UUID are supposed to be little-endian encoded. The specification
210 * says that this is the defacto standard.
211 */
Ivan Khoronzhuk95be58d2015-02-18 13:33:20 +0200212 if (dmi_ver >= 0x020600)
Zhenzhong Duanf1d8e612012-12-20 15:05:13 -0800213 sprintf(s, "%pUL", d);
214 else
215 sprintf(s, "%pUB", d);
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200216
Jean Delvare02d9c472013-09-11 14:24:08 -0700217 dmi_ident[slot] = s;
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200218}
219
Jean Delvare02d9c472013-09-11 14:24:08 -0700220static void __init dmi_save_type(const struct dmi_header *dm, int slot,
221 int index)
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200222{
Jean Delvarea814c352017-06-15 13:46:01 +0200223 const u8 *d;
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200224 char *s;
225
Jean Delvarea814c352017-06-15 13:46:01 +0200226 if (dmi_ident[slot] || dm->length <= index)
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200227 return;
228
229 s = dmi_alloc(4);
230 if (!s)
231 return;
232
Jean Delvarea814c352017-06-15 13:46:01 +0200233 d = (u8 *) dm + index;
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200234 sprintf(s, "%u", *d & 0x7F);
235 dmi_ident[slot] = s;
236}
237
Jean Delvaref3069ae2008-02-23 15:23:46 -0800238static void __init dmi_save_one_device(int type, const char *name)
239{
240 struct dmi_device *dev;
241
242 /* No duplicate device */
243 if (dmi_find_device(type, name, NULL))
244 return;
245
246 dev = dmi_alloc(sizeof(*dev) + strlen(name) + 1);
Jean Delvareae797442013-09-11 14:24:10 -0700247 if (!dev)
Jean Delvaref3069ae2008-02-23 15:23:46 -0800248 return;
Jean Delvaref3069ae2008-02-23 15:23:46 -0800249
250 dev->type = type;
251 strcpy((char *)(dev + 1), name);
252 dev->name = (char *)(dev + 1);
253 dev->device_data = NULL;
254 list_add(&dev->list, &dmi_devices);
255}
256
Jeff Garzik18552562007-10-03 15:15:40 -0400257static void __init dmi_save_devices(const struct dmi_header *dm)
Andrey Paninebad6a42005-09-06 15:18:29 -0700258{
259 int i, count = (dm->length - sizeof(struct dmi_header)) / 2;
Andrey Paninebad6a42005-09-06 15:18:29 -0700260
261 for (i = 0; i < count; i++) {
Jeff Garzik18552562007-10-03 15:15:40 -0400262 const char *d = (char *)(dm + 1) + (i * 2);
Andrey Paninebad6a42005-09-06 15:18:29 -0700263
264 /* Skip disabled device */
265 if ((*d & 0x80) == 0)
266 continue;
267
Jean Delvaref3069ae2008-02-23 15:23:46 -0800268 dmi_save_one_device(*d & 0x7f, dmi_string_nosave(dm, *(d + 1)));
Shem Multinymous2e0c1f62006-09-29 01:59:37 -0700269 }
270}
271
Jeff Garzik18552562007-10-03 15:15:40 -0400272static void __init dmi_save_oem_strings_devices(const struct dmi_header *dm)
Shem Multinymous2e0c1f62006-09-29 01:59:37 -0700273{
Jean Delvarea814c352017-06-15 13:46:01 +0200274 int i, count;
Shem Multinymous2e0c1f62006-09-29 01:59:37 -0700275 struct dmi_device *dev;
276
Jean Delvarea814c352017-06-15 13:46:01 +0200277 if (dm->length < 0x05)
278 return;
279
280 count = *(u8 *)(dm + 1);
Shem Multinymous2e0c1f62006-09-29 01:59:37 -0700281 for (i = 1; i <= count; i++) {
Jean Delvareffbbb962013-09-11 14:24:09 -0700282 const char *devname = dmi_string(dm, i);
Parag Warudkar79da4722008-01-30 13:31:59 +0100283
Jean Delvare43fe1052008-02-23 15:23:55 -0800284 if (devname == dmi_empty_string)
Parag Warudkar79da4722008-01-30 13:31:59 +0100285 continue;
Parag Warudkar79da4722008-01-30 13:31:59 +0100286
Shem Multinymous2e0c1f62006-09-29 01:59:37 -0700287 dev = dmi_alloc(sizeof(*dev));
Jean Delvareae797442013-09-11 14:24:10 -0700288 if (!dev)
Shem Multinymous2e0c1f62006-09-29 01:59:37 -0700289 break;
Shem Multinymous2e0c1f62006-09-29 01:59:37 -0700290
291 dev->type = DMI_DEV_TYPE_OEM_STRING;
Parag Warudkar79da4722008-01-30 13:31:59 +0100292 dev->name = devname;
Shem Multinymous2e0c1f62006-09-29 01:59:37 -0700293 dev->device_data = NULL;
Andrey Paninebad6a42005-09-06 15:18:29 -0700294
295 list_add(&dev->list, &dmi_devices);
296 }
297}
298
Jeff Garzik18552562007-10-03 15:15:40 -0400299static void __init dmi_save_ipmi_device(const struct dmi_header *dm)
Andrey Paninebad6a42005-09-06 15:18:29 -0700300{
301 struct dmi_device *dev;
Jean Delvare02d9c472013-09-11 14:24:08 -0700302 void *data;
Andrey Paninebad6a42005-09-06 15:18:29 -0700303
Andi Kleene9928672006-01-11 22:43:33 +0100304 data = dmi_alloc(dm->length);
Jean Delvareae797442013-09-11 14:24:10 -0700305 if (data == NULL)
Andrey Paninebad6a42005-09-06 15:18:29 -0700306 return;
Andrey Paninebad6a42005-09-06 15:18:29 -0700307
308 memcpy(data, dm, dm->length);
309
Andi Kleene9928672006-01-11 22:43:33 +0100310 dev = dmi_alloc(sizeof(*dev));
Jean Delvareae797442013-09-11 14:24:10 -0700311 if (!dev)
Andrey Paninebad6a42005-09-06 15:18:29 -0700312 return;
Andrey Paninebad6a42005-09-06 15:18:29 -0700313
314 dev->type = DMI_DEV_TYPE_IPMI;
315 dev->name = "IPMI controller";
316 dev->device_data = data;
317
Carol Hebertabd24df2008-04-04 14:30:03 -0700318 list_add_tail(&dev->list, &dmi_devices);
Andrey Paninebad6a42005-09-06 15:18:29 -0700319}
320
Jordan Hargravee5b6c152016-01-15 22:08:45 +0100321static void __init dmi_save_dev_pciaddr(int instance, int segment, int bus,
322 int devfn, const char *name, int type)
Narendra K911e1c92010-07-26 05:56:50 -0500323{
Jordan Hargravee5b6c152016-01-15 22:08:45 +0100324 struct dmi_dev_onboard *dev;
Narendra K911e1c92010-07-26 05:56:50 -0500325
Jordan Hargravee5b6c152016-01-15 22:08:45 +0100326 /* Ignore invalid values */
327 if (type == DMI_DEV_TYPE_DEV_SLOT &&
328 segment == 0xFFFF && bus == 0xFF && devfn == 0xFF)
Narendra K911e1c92010-07-26 05:56:50 -0500329 return;
Jean Delvareae797442013-09-11 14:24:10 -0700330
Jordan Hargravee5b6c152016-01-15 22:08:45 +0100331 dev = dmi_alloc(sizeof(*dev) + strlen(name) + 1);
332 if (!dev)
333 return;
Narendra K911e1c92010-07-26 05:56:50 -0500334
Jordan Hargravee5b6c152016-01-15 22:08:45 +0100335 dev->instance = instance;
336 dev->segment = segment;
337 dev->bus = bus;
338 dev->devfn = devfn;
Narendra K911e1c92010-07-26 05:56:50 -0500339
Jordan Hargravee5b6c152016-01-15 22:08:45 +0100340 strcpy((char *)&dev[1], name);
341 dev->dev.type = type;
342 dev->dev.name = (char *)&dev[1];
343 dev->dev.device_data = dev;
344
345 list_add(&dev->dev.list, &dmi_devices);
Narendra K911e1c92010-07-26 05:56:50 -0500346}
347
Wim Van Sebroeckb4bd7d52008-02-08 04:20:58 -0800348static void __init dmi_save_extended_devices(const struct dmi_header *dm)
349{
Jean Delvare96e23942016-01-15 22:08:44 +0100350 const char *name;
Jean Delvare45b98252016-01-15 22:08:44 +0100351 const u8 *d = (u8 *)dm;
Wim Van Sebroeckb4bd7d52008-02-08 04:20:58 -0800352
Jean Delvarea814c352017-06-15 13:46:01 +0200353 if (dm->length < 0x0B)
354 return;
355
Wim Van Sebroeckb4bd7d52008-02-08 04:20:58 -0800356 /* Skip disabled device */
Jean Delvare45b98252016-01-15 22:08:44 +0100357 if ((d[0x5] & 0x80) == 0)
Wim Van Sebroeckb4bd7d52008-02-08 04:20:58 -0800358 return;
359
Jean Delvare45b98252016-01-15 22:08:44 +0100360 name = dmi_string_nosave(dm, d[0x4]);
Jordan Hargravee5b6c152016-01-15 22:08:45 +0100361 dmi_save_dev_pciaddr(d[0x6], *(u16 *)(d + 0x7), d[0x9], d[0xA], name,
362 DMI_DEV_TYPE_DEV_ONBOARD);
Jean Delvare45b98252016-01-15 22:08:44 +0100363 dmi_save_one_device(d[0x5] & 0x7f, name);
Wim Van Sebroeckb4bd7d52008-02-08 04:20:58 -0800364}
365
Jordan Hargravee5b6c152016-01-15 22:08:45 +0100366static void __init dmi_save_system_slot(const struct dmi_header *dm)
367{
368 const u8 *d = (u8 *)dm;
369
370 /* Need SMBIOS 2.6+ structure */
371 if (dm->length < 0x11)
372 return;
373 dmi_save_dev_pciaddr(*(u16 *)(d + 0x9), *(u16 *)(d + 0xD), d[0xF],
374 d[0x10], dmi_string_nosave(dm, d[0x4]),
375 DMI_DEV_TYPE_DEV_SLOT);
376}
377
Chen, Gongdd6dad42013-10-18 14:29:25 -0700378static void __init count_mem_devices(const struct dmi_header *dm, void *v)
379{
380 if (dm->type != DMI_ENTRY_MEM_DEVICE)
381 return;
382 dmi_memdev_nr++;
383}
384
385static void __init save_mem_devices(const struct dmi_header *dm, void *v)
386{
387 const char *d = (const char *)dm;
388 static int nr;
389
Jean Delvarea814c352017-06-15 13:46:01 +0200390 if (dm->type != DMI_ENTRY_MEM_DEVICE || dm->length < 0x12)
Chen, Gongdd6dad42013-10-18 14:29:25 -0700391 return;
392 if (nr >= dmi_memdev_nr) {
393 pr_warn(FW_BUG "Too many DIMM entries in SMBIOS table\n");
394 return;
395 }
Luck, Tony0841c042013-11-01 13:59:52 -0700396 dmi_memdev[nr].handle = get_unaligned(&dm->handle);
Chen, Gongdd6dad42013-10-18 14:29:25 -0700397 dmi_memdev[nr].device = dmi_string(dm, d[0x10]);
398 dmi_memdev[nr].bank = dmi_string(dm, d[0x11]);
399 nr++;
400}
401
402void __init dmi_memdev_walk(void)
403{
404 if (!dmi_available)
405 return;
406
407 if (dmi_walk_early(count_mem_devices) == 0 && dmi_memdev_nr) {
408 dmi_memdev = dmi_alloc(sizeof(*dmi_memdev) * dmi_memdev_nr);
409 if (dmi_memdev)
410 dmi_walk_early(save_mem_devices);
411 }
412}
413
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 * Process a DMI table entry. Right now all we care about are the BIOS
416 * and machine entries. For 2.5 we should pull the smbus controller info
417 * out of here.
418 */
Jean Delvaree7a19c562009-03-30 21:46:44 +0200419static void __init dmi_decode(const struct dmi_header *dm, void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420{
Jean Delvare02d9c472013-09-11 14:24:08 -0700421 switch (dm->type) {
Andrey Paninebad6a42005-09-06 15:18:29 -0700422 case 0: /* BIOS Information */
Andrey Panin1249c512005-06-25 14:54:47 -0700423 dmi_save_ident(dm, DMI_BIOS_VENDOR, 4);
Andrey Panin1249c512005-06-25 14:54:47 -0700424 dmi_save_ident(dm, DMI_BIOS_VERSION, 5);
Andrey Panin1249c512005-06-25 14:54:47 -0700425 dmi_save_ident(dm, DMI_BIOS_DATE, 8);
426 break;
Andrey Paninebad6a42005-09-06 15:18:29 -0700427 case 1: /* System Information */
Andrey Panin1249c512005-06-25 14:54:47 -0700428 dmi_save_ident(dm, DMI_SYS_VENDOR, 4);
Andrey Panin1249c512005-06-25 14:54:47 -0700429 dmi_save_ident(dm, DMI_PRODUCT_NAME, 5);
Andrey Panin1249c512005-06-25 14:54:47 -0700430 dmi_save_ident(dm, DMI_PRODUCT_VERSION, 6);
Andrey Panin1249c512005-06-25 14:54:47 -0700431 dmi_save_ident(dm, DMI_PRODUCT_SERIAL, 7);
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200432 dmi_save_uuid(dm, DMI_PRODUCT_UUID, 8);
Mika Westerbergc61872c2017-05-17 13:25:12 +0300433 dmi_save_ident(dm, DMI_PRODUCT_FAMILY, 26);
Andrey Panin1249c512005-06-25 14:54:47 -0700434 break;
Andrey Paninebad6a42005-09-06 15:18:29 -0700435 case 2: /* Base Board Information */
Andrey Panin1249c512005-06-25 14:54:47 -0700436 dmi_save_ident(dm, DMI_BOARD_VENDOR, 4);
Andrey Panin1249c512005-06-25 14:54:47 -0700437 dmi_save_ident(dm, DMI_BOARD_NAME, 5);
Andrey Panin1249c512005-06-25 14:54:47 -0700438 dmi_save_ident(dm, DMI_BOARD_VERSION, 6);
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200439 dmi_save_ident(dm, DMI_BOARD_SERIAL, 7);
440 dmi_save_ident(dm, DMI_BOARD_ASSET_TAG, 8);
441 break;
442 case 3: /* Chassis Information */
443 dmi_save_ident(dm, DMI_CHASSIS_VENDOR, 4);
444 dmi_save_type(dm, DMI_CHASSIS_TYPE, 5);
445 dmi_save_ident(dm, DMI_CHASSIS_VERSION, 6);
446 dmi_save_ident(dm, DMI_CHASSIS_SERIAL, 7);
447 dmi_save_ident(dm, DMI_CHASSIS_ASSET_TAG, 8);
Andrey Panin1249c512005-06-25 14:54:47 -0700448 break;
Jordan Hargravee5b6c152016-01-15 22:08:45 +0100449 case 9: /* System Slots */
450 dmi_save_system_slot(dm);
451 break;
Andrey Paninebad6a42005-09-06 15:18:29 -0700452 case 10: /* Onboard Devices Information */
453 dmi_save_devices(dm);
454 break;
Shem Multinymous2e0c1f62006-09-29 01:59:37 -0700455 case 11: /* OEM Strings */
456 dmi_save_oem_strings_devices(dm);
457 break;
Andrey Paninebad6a42005-09-06 15:18:29 -0700458 case 38: /* IPMI Device Information */
459 dmi_save_ipmi_device(dm);
Wim Van Sebroeckb4bd7d52008-02-08 04:20:58 -0800460 break;
461 case 41: /* Onboard Devices Extended Information */
462 dmi_save_extended_devices(dm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 }
464}
465
Tejun Heoc90fe6b2013-04-30 15:27:14 -0700466static int __init print_filtered(char *buf, size_t len, const char *info)
Bjorn Helgaas8881cdc2010-10-27 15:32:59 -0700467{
Tejun Heoc90fe6b2013-04-30 15:27:14 -0700468 int c = 0;
Bjorn Helgaas8881cdc2010-10-27 15:32:59 -0700469 const char *p;
470
471 if (!info)
Tejun Heoc90fe6b2013-04-30 15:27:14 -0700472 return c;
Bjorn Helgaas8881cdc2010-10-27 15:32:59 -0700473
474 for (p = info; *p; p++)
475 if (isprint(*p))
Tejun Heoc90fe6b2013-04-30 15:27:14 -0700476 c += scnprintf(buf + c, len - c, "%c", *p);
Bjorn Helgaas8881cdc2010-10-27 15:32:59 -0700477 else
Tejun Heoc90fe6b2013-04-30 15:27:14 -0700478 c += scnprintf(buf + c, len - c, "\\x%02x", *p & 0xff);
479 return c;
Bjorn Helgaas8881cdc2010-10-27 15:32:59 -0700480}
481
Tejun Heoc90fe6b2013-04-30 15:27:14 -0700482static void __init dmi_format_ids(char *buf, size_t len)
Bjorn Helgaas8881cdc2010-10-27 15:32:59 -0700483{
Tejun Heoc90fe6b2013-04-30 15:27:14 -0700484 int c = 0;
Naga Chumbalkar84e383b2011-02-14 22:47:17 +0000485 const char *board; /* Board Name is optional */
486
Tejun Heoc90fe6b2013-04-30 15:27:14 -0700487 c += print_filtered(buf + c, len - c,
488 dmi_get_system_info(DMI_SYS_VENDOR));
489 c += scnprintf(buf + c, len - c, " ");
490 c += print_filtered(buf + c, len - c,
491 dmi_get_system_info(DMI_PRODUCT_NAME));
492
Naga Chumbalkar84e383b2011-02-14 22:47:17 +0000493 board = dmi_get_system_info(DMI_BOARD_NAME);
494 if (board) {
Tejun Heoc90fe6b2013-04-30 15:27:14 -0700495 c += scnprintf(buf + c, len - c, "/");
496 c += print_filtered(buf + c, len - c, board);
Naga Chumbalkar84e383b2011-02-14 22:47:17 +0000497 }
Tejun Heoc90fe6b2013-04-30 15:27:14 -0700498 c += scnprintf(buf + c, len - c, ", BIOS ");
499 c += print_filtered(buf + c, len - c,
500 dmi_get_system_info(DMI_BIOS_VERSION));
501 c += scnprintf(buf + c, len - c, " ");
502 c += print_filtered(buf + c, len - c,
503 dmi_get_system_info(DMI_BIOS_DATE));
Bjorn Helgaas8881cdc2010-10-27 15:32:59 -0700504}
505
Ben Hutchingsd39de282013-07-31 13:53:30 -0700506/*
507 * Check for DMI/SMBIOS headers in the system firmware image. Any
508 * SMBIOS header must start 16 bytes before the DMI header, so take a
509 * 32 byte buffer and check for DMI at offset 16 and SMBIOS at offset
510 * 0. If the DMI header is present, set dmi_ver accordingly (SMBIOS
511 * takes precedence) and return 0. Otherwise return 1.
512 */
Ben Hutchings79bae422013-04-30 15:27:46 -0700513static int __init dmi_present(const u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514{
Ivan Khoronzhuk95be58d2015-02-18 13:33:20 +0200515 u32 smbios_ver;
Jeff Garzik18552562007-10-03 15:15:40 -0400516
Ben Hutchings79bae422013-04-30 15:27:46 -0700517 if (memcmp(buf, "_SM_", 4) == 0 &&
518 buf[5] < 32 && dmi_checksum(buf, buf[5])) {
Ard Biesheuvelfc430262014-10-14 16:41:27 +0200519 smbios_ver = get_unaligned_be16(buf + 6);
Ivan Khoronzhukd7f96f972015-06-25 09:06:56 +0200520 smbios_entry_point_size = buf[5];
521 memcpy(smbios_entry_point, buf, smbios_entry_point_size);
Ben Hutchings79bae422013-04-30 15:27:46 -0700522
523 /* Some BIOS report weird SMBIOS version, fix that up */
524 switch (smbios_ver) {
525 case 0x021F:
526 case 0x0221:
Jean Delvared1d87042015-06-25 09:06:57 +0200527 pr_debug("SMBIOS version fixup (2.%d->2.%d)\n",
Ben Hutchings79bae422013-04-30 15:27:46 -0700528 smbios_ver & 0xFF, 3);
529 smbios_ver = 0x0203;
530 break;
531 case 0x0233:
Jean Delvared1d87042015-06-25 09:06:57 +0200532 pr_debug("SMBIOS version fixup (2.%d->2.%d)\n", 51, 6);
Ben Hutchings79bae422013-04-30 15:27:46 -0700533 smbios_ver = 0x0206;
534 break;
535 }
536 } else {
537 smbios_ver = 0;
538 }
539
540 buf += 16;
541
542 if (memcmp(buf, "_DMI_", 5) == 0 && dmi_checksum(buf, 15)) {
Jean Delvare5c1ac562015-05-14 14:40:50 +0200543 if (smbios_ver)
544 dmi_ver = smbios_ver;
545 else
546 dmi_ver = (buf[14] & 0xF0) << 4 | (buf[14] & 0x0F);
Andrea Arcangeliff4319d2016-01-08 09:00:54 +0100547 dmi_ver <<= 8;
Ard Biesheuvelfc430262014-10-14 16:41:27 +0200548 dmi_num = get_unaligned_le16(buf + 12);
549 dmi_len = get_unaligned_le16(buf + 6);
550 dmi_base = get_unaligned_le32(buf + 8);
Matt Domsch3ed3bce2006-03-26 01:37:03 -0800551
Bjorn Helgaas8881cdc2010-10-27 15:32:59 -0700552 if (dmi_walk_early(dmi_decode) == 0) {
Ben Hutchings79bae422013-04-30 15:27:46 -0700553 if (smbios_ver) {
Jean Delvarec2493042015-05-14 14:40:50 +0200554 pr_info("SMBIOS %d.%d present.\n",
Andrea Arcangeliff4319d2016-01-08 09:00:54 +0100555 dmi_ver >> 16, (dmi_ver >> 8) & 0xFF);
Ben Hutchings79bae422013-04-30 15:27:46 -0700556 } else {
Ivan Khoronzhukd7f96f972015-06-25 09:06:56 +0200557 smbios_entry_point_size = 15;
558 memcpy(smbios_entry_point, buf,
559 smbios_entry_point_size);
Zhenzhong Duan9f9c9cbb2012-12-20 15:05:14 -0800560 pr_info("Legacy DMI %d.%d present.\n",
Andrea Arcangeliff4319d2016-01-08 09:00:54 +0100561 dmi_ver >> 16, (dmi_ver >> 8) & 0xFF);
Zhenzhong Duan9f9c9cbb2012-12-20 15:05:14 -0800562 }
Tejun Heoc90fe6b2013-04-30 15:27:14 -0700563 dmi_format_ids(dmi_ids_string, sizeof(dmi_ids_string));
Kefeng Wangd4af49f2016-12-19 10:01:47 +0100564 pr_info("DMI: %s\n", dmi_ids_string);
Matt Domsch3ed3bce2006-03-26 01:37:03 -0800565 return 0;
Bjorn Helgaas8881cdc2010-10-27 15:32:59 -0700566 }
Matt Domsch3ed3bce2006-03-26 01:37:03 -0800567 }
Matt Domsch3ed3bce2006-03-26 01:37:03 -0800568
Ben Hutchingsa40e7cf2013-03-08 12:43:32 -0800569 return 1;
Zhenzhong Duan9f9c9cbb2012-12-20 15:05:14 -0800570}
571
Ard Biesheuvelfc430262014-10-14 16:41:27 +0200572/*
573 * Check for the SMBIOS 3.0 64-bit entry point signature. Unlike the legacy
574 * 32-bit entry point, there is no embedded DMI header (_DMI_) in here.
575 */
576static int __init dmi_smbios3_present(const u8 *buf)
577{
578 if (memcmp(buf, "_SM3_", 5) == 0 &&
579 buf[6] < 32 && dmi_checksum(buf, buf[6])) {
Jean Delvared1d87042015-06-25 09:06:57 +0200580 dmi_ver = get_unaligned_be32(buf + 6) & 0xFFFFFF;
Jean Delvarebfbaafa2015-03-20 09:59:47 +0100581 dmi_num = 0; /* No longer specified */
Ard Biesheuvelfc430262014-10-14 16:41:27 +0200582 dmi_len = get_unaligned_le32(buf + 12);
583 dmi_base = get_unaligned_le64(buf + 16);
Ivan Khoronzhukd7f96f972015-06-25 09:06:56 +0200584 smbios_entry_point_size = buf[6];
585 memcpy(smbios_entry_point, buf, smbios_entry_point_size);
Ard Biesheuvelfc430262014-10-14 16:41:27 +0200586
Ard Biesheuvelfc430262014-10-14 16:41:27 +0200587 if (dmi_walk_early(dmi_decode) == 0) {
Ivan Khoronzhuk95be58d2015-02-18 13:33:20 +0200588 pr_info("SMBIOS %d.%d.%d present.\n",
589 dmi_ver >> 16, (dmi_ver >> 8) & 0xFF,
590 dmi_ver & 0xFF);
Ard Biesheuvelfc430262014-10-14 16:41:27 +0200591 dmi_format_ids(dmi_ids_string, sizeof(dmi_ids_string));
Kefeng Wangd4af49f2016-12-19 10:01:47 +0100592 pr_info("DMI: %s\n", dmi_ids_string);
Ard Biesheuvelfc430262014-10-14 16:41:27 +0200593 return 0;
594 }
595 }
596 return 1;
597}
598
Matt Domsch3ed3bce2006-03-26 01:37:03 -0800599void __init dmi_scan_machine(void)
600{
Andrey Panin61e032f2005-09-06 15:18:26 -0700601 char __iomem *p, *q;
Ben Hutchings79bae422013-04-30 15:27:46 -0700602 char buf[32];
Andrey Panin61e032f2005-09-06 15:18:26 -0700603
Matt Fleming83e68182012-11-14 09:42:35 +0000604 if (efi_enabled(EFI_CONFIG_TABLES)) {
Ard Biesheuvelfc430262014-10-14 16:41:27 +0200605 /*
606 * According to the DMTF SMBIOS reference spec v3.0.0, it is
607 * allowed to define both the 64-bit entry point (smbios3) and
608 * the 32-bit entry point (smbios), in which case they should
609 * either both point to the same SMBIOS structure table, or the
610 * table pointed to by the 64-bit entry point should contain a
611 * superset of the table contents pointed to by the 32-bit entry
612 * point (section 5.2)
613 * This implies that the 64-bit entry point should have
614 * precedence if it is defined and supported by the OS. If we
615 * have the 64-bit entry point, but fail to decode it, fall
616 * back to the legacy one (if available)
617 */
618 if (efi.smbios3 != EFI_INVALID_TABLE_ADDR) {
619 p = dmi_early_remap(efi.smbios3, 32);
620 if (p == NULL)
621 goto error;
622 memcpy_fromio(buf, p, 32);
623 dmi_early_unmap(p, 32);
624
625 if (!dmi_smbios3_present(buf)) {
626 dmi_available = 1;
Jean Delvare71177942018-02-03 11:25:20 +0100627 return;
Ard Biesheuvelfc430262014-10-14 16:41:27 +0200628 }
629 }
Bjorn Helgaasb2c99e32006-03-26 01:37:08 -0800630 if (efi.smbios == EFI_INVALID_TABLE_ADDR)
Ingo Molnar9a22b6e2008-09-18 12:50:18 +0200631 goto error;
Andrey Panin61e032f2005-09-06 15:18:26 -0700632
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200633 /* This is called as a core_initcall() because it isn't
634 * needed during early boot. This also means we can
635 * iounmap the space when we're done with it.
636 */
Ard Biesheuvelcf074402014-01-23 15:54:39 -0800637 p = dmi_early_remap(efi.smbios, 32);
Matt Domsch3ed3bce2006-03-26 01:37:03 -0800638 if (p == NULL)
Ingo Molnar9a22b6e2008-09-18 12:50:18 +0200639 goto error;
Ben Hutchings79bae422013-04-30 15:27:46 -0700640 memcpy_fromio(buf, p, 32);
Ard Biesheuvelcf074402014-01-23 15:54:39 -0800641 dmi_early_unmap(p, 32);
Ben Hutchings79bae422013-04-30 15:27:46 -0700642
643 if (!dmi_present(buf)) {
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200644 dmi_available = 1;
Jean Delvare71177942018-02-03 11:25:20 +0100645 return;
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200646 }
Ard Biesheuvelcf074402014-01-23 15:54:39 -0800647 } else if (IS_ENABLED(CONFIG_DMI_SCAN_MACHINE_NON_EFI_FALLBACK)) {
648 p = dmi_early_remap(0xF0000, 0x10000);
Matt Domsch3ed3bce2006-03-26 01:37:03 -0800649 if (p == NULL)
Ingo Molnar9a22b6e2008-09-18 12:50:18 +0200650 goto error;
Andrey Panin61e032f2005-09-06 15:18:26 -0700651
Ben Hutchingsd39de282013-07-31 13:53:30 -0700652 /*
Jean Delvarec9aba142017-06-15 13:46:00 +0200653 * Same logic as above, look for a 64-bit entry point
654 * first, and if not found, fall back to 32-bit entry point.
655 */
656 memcpy_fromio(buf, p, 16);
657 for (q = p + 16; q < p + 0x10000; q += 16) {
658 memcpy_fromio(buf + 16, q, 16);
659 if (!dmi_smbios3_present(buf)) {
660 dmi_available = 1;
661 dmi_early_unmap(p, 0x10000);
Jean Delvare71177942018-02-03 11:25:20 +0100662 return;
Jean Delvarec9aba142017-06-15 13:46:00 +0200663 }
664 memcpy(buf, buf + 16, 16);
665 }
666
667 /*
Ben Hutchingsd39de282013-07-31 13:53:30 -0700668 * Iterate over all possible DMI header addresses q.
669 * Maintain the 32 bytes around q in buf. On the
670 * first iteration, substitute zero for the
671 * out-of-range bytes so there is no chance of falsely
672 * detecting an SMBIOS header.
673 */
Ben Hutchings79bae422013-04-30 15:27:46 -0700674 memset(buf, 0, 16);
Matt Domsch3ed3bce2006-03-26 01:37:03 -0800675 for (q = p; q < p + 0x10000; q += 16) {
Ben Hutchings79bae422013-04-30 15:27:46 -0700676 memcpy_fromio(buf + 16, q, 16);
Jean Delvarec9aba142017-06-15 13:46:00 +0200677 if (!dmi_present(buf)) {
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200678 dmi_available = 1;
Ard Biesheuvelcf074402014-01-23 15:54:39 -0800679 dmi_early_unmap(p, 0x10000);
Jean Delvare71177942018-02-03 11:25:20 +0100680 return;
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200681 }
Ben Hutchings79bae422013-04-30 15:27:46 -0700682 memcpy(buf, buf + 16, 16);
Andrey Panin61e032f2005-09-06 15:18:26 -0700683 }
Ard Biesheuvelcf074402014-01-23 15:54:39 -0800684 dmi_early_unmap(p, 0x10000);
Andrey Panin61e032f2005-09-06 15:18:26 -0700685 }
Ingo Molnar9a22b6e2008-09-18 12:50:18 +0200686 error:
Jean Delvare02d9c472013-09-11 14:24:08 -0700687 pr_info("DMI not present or invalid.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688}
689
Ivan Khoronzhukd7f96f972015-06-25 09:06:56 +0200690static ssize_t raw_table_read(struct file *file, struct kobject *kobj,
691 struct bin_attribute *attr, char *buf,
692 loff_t pos, size_t count)
693{
694 memcpy(buf, attr->private + pos, count);
695 return count;
696}
697
698static BIN_ATTR(smbios_entry_point, S_IRUSR, raw_table_read, NULL, 0);
699static BIN_ATTR(DMI, S_IRUSR, raw_table_read, NULL, 0);
700
701static int __init dmi_init(void)
702{
703 struct kobject *tables_kobj;
704 u8 *dmi_table;
705 int ret = -ENOMEM;
706
Ard Biesheuvela81114d2018-02-03 11:25:20 +0100707 if (!dmi_available)
708 return 0;
Ivan Khoronzhukd7f96f972015-06-25 09:06:56 +0200709
710 /*
711 * Set up dmi directory at /sys/firmware/dmi. This entry should stay
712 * even after farther error, as it can be used by other modules like
713 * dmi-sysfs.
714 */
715 dmi_kobj = kobject_create_and_add("dmi", firmware_kobj);
716 if (!dmi_kobj)
717 goto err;
718
719 tables_kobj = kobject_create_and_add("tables", dmi_kobj);
720 if (!tables_kobj)
721 goto err;
722
723 dmi_table = dmi_remap(dmi_base, dmi_len);
724 if (!dmi_table)
725 goto err_tables;
726
727 bin_attr_smbios_entry_point.size = smbios_entry_point_size;
728 bin_attr_smbios_entry_point.private = smbios_entry_point;
729 ret = sysfs_create_bin_file(tables_kobj, &bin_attr_smbios_entry_point);
730 if (ret)
731 goto err_unmap;
732
733 bin_attr_DMI.size = dmi_len;
734 bin_attr_DMI.private = dmi_table;
735 ret = sysfs_create_bin_file(tables_kobj, &bin_attr_DMI);
736 if (!ret)
737 return 0;
738
739 sysfs_remove_bin_file(tables_kobj,
740 &bin_attr_smbios_entry_point);
741 err_unmap:
742 dmi_unmap(dmi_table);
743 err_tables:
744 kobject_del(tables_kobj);
745 kobject_put(tables_kobj);
746 err:
747 pr_err("dmi: Firmware registration failed.\n");
748
749 return ret;
750}
751subsys_initcall(dmi_init);
752
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753/**
Tejun Heo98e5e1b2013-04-30 15:27:15 -0700754 * dmi_set_dump_stack_arch_desc - set arch description for dump_stack()
755 *
756 * Invoke dump_stack_set_arch_desc() with DMI system information so that
757 * DMI identifiers are printed out on task dumps. Arch boot code should
758 * call this function after dmi_scan_machine() if it wants to print out DMI
759 * identifiers on task dumps.
760 */
761void __init dmi_set_dump_stack_arch_desc(void)
762{
763 dump_stack_set_arch_desc("%s", dmi_ids_string);
764}
765
766/**
Rafael J. Wysockid7b19562009-01-19 20:55:50 +0100767 * dmi_matches - check if dmi_system_id structure matches system DMI data
768 * @dmi: pointer to the dmi_system_id structure to check
769 */
770static bool dmi_matches(const struct dmi_system_id *dmi)
771{
772 int i;
773
Rafael J. Wysockid7b19562009-01-19 20:55:50 +0100774 for (i = 0; i < ARRAY_SIZE(dmi->matches); i++) {
775 int s = dmi->matches[i].slot;
776 if (s == DMI_NONE)
Dmitry Torokhov75757502009-12-04 10:24:19 -0800777 break;
Jani Nikula5017b282013-07-03 15:05:02 -0700778 if (dmi_ident[s]) {
Jean Delvare8cf4e6a2018-02-03 11:25:20 +0100779 if (dmi->matches[i].exact_match) {
780 if (!strcmp(dmi_ident[s],
781 dmi->matches[i].substr))
782 continue;
783 } else {
784 if (strstr(dmi_ident[s],
785 dmi->matches[i].substr))
786 continue;
787 }
Jani Nikula5017b282013-07-03 15:05:02 -0700788 }
789
Rafael J. Wysockid7b19562009-01-19 20:55:50 +0100790 /* No match */
791 return false;
792 }
793 return true;
794}
795
796/**
Dmitry Torokhov75757502009-12-04 10:24:19 -0800797 * dmi_is_end_of_table - check for end-of-table marker
798 * @dmi: pointer to the dmi_system_id structure to check
799 */
800static bool dmi_is_end_of_table(const struct dmi_system_id *dmi)
801{
802 return dmi->matches[0].slot == DMI_NONE;
803}
804
805/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 * dmi_check_system - check system DMI data
807 * @list: array of dmi_system_id structures to match against
Randy Dunlapb0ef3712006-06-25 05:49:18 -0700808 * All non-null elements of the list must match
809 * their slot's (field index's) data (i.e., each
810 * list string must be a substring of the specified
811 * DMI slot's string data) to be considered a
812 * successful match.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 *
814 * Walk the blacklist table running matching functions until someone
815 * returns non zero or we hit the end. Callback function is called for
Randy Dunlapb0ef3712006-06-25 05:49:18 -0700816 * each successful match. Returns the number of matches.
Jean Delvare71177942018-02-03 11:25:20 +0100817 *
818 * dmi_scan_machine must be called before this function is called.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 */
Jeff Garzik18552562007-10-03 15:15:40 -0400820int dmi_check_system(const struct dmi_system_id *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821{
Rafael J. Wysockid7b19562009-01-19 20:55:50 +0100822 int count = 0;
823 const struct dmi_system_id *d;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
Dmitry Torokhov75757502009-12-04 10:24:19 -0800825 for (d = list; !dmi_is_end_of_table(d); d++)
Rafael J. Wysockid7b19562009-01-19 20:55:50 +0100826 if (dmi_matches(d)) {
827 count++;
828 if (d->callback && d->callback(d))
829 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
832 return count;
833}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834EXPORT_SYMBOL(dmi_check_system);
835
836/**
Rafael J. Wysockid7b19562009-01-19 20:55:50 +0100837 * dmi_first_match - find dmi_system_id structure matching system DMI data
838 * @list: array of dmi_system_id structures to match against
839 * All non-null elements of the list must match
840 * their slot's (field index's) data (i.e., each
841 * list string must be a substring of the specified
842 * DMI slot's string data) to be considered a
843 * successful match.
844 *
845 * Walk the blacklist table until the first match is found. Return the
846 * pointer to the matching entry or NULL if there's no match.
Jean Delvare71177942018-02-03 11:25:20 +0100847 *
848 * dmi_scan_machine must be called before this function is called.
Rafael J. Wysockid7b19562009-01-19 20:55:50 +0100849 */
850const struct dmi_system_id *dmi_first_match(const struct dmi_system_id *list)
851{
852 const struct dmi_system_id *d;
853
Dmitry Torokhov75757502009-12-04 10:24:19 -0800854 for (d = list; !dmi_is_end_of_table(d); d++)
Rafael J. Wysockid7b19562009-01-19 20:55:50 +0100855 if (dmi_matches(d))
856 return d;
857
858 return NULL;
859}
860EXPORT_SYMBOL(dmi_first_match);
861
862/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 * dmi_get_system_info - return DMI data value
Randy Dunlapb0ef3712006-06-25 05:49:18 -0700864 * @field: data index (see enum dmi_field)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 *
866 * Returns one DMI data value, can be used to perform
867 * complex DMI data checks.
868 */
Jeff Garzik18552562007-10-03 15:15:40 -0400869const char *dmi_get_system_info(int field)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870{
871 return dmi_ident[field];
872}
Dmitry Torokhove70c9d52005-06-25 14:54:25 -0700873EXPORT_SYMBOL(dmi_get_system_info);
Andrey Paninebad6a42005-09-06 15:18:29 -0700874
Alok Katariafd8cd7e2008-11-03 15:50:38 -0800875/**
Randy Dunlapc2bacfc2009-01-06 14:41:40 -0800876 * dmi_name_in_serial - Check if string is in the DMI product serial information
877 * @str: string to check for
Alok Katariafd8cd7e2008-11-03 15:50:38 -0800878 */
879int dmi_name_in_serial(const char *str)
880{
881 int f = DMI_PRODUCT_SERIAL;
882 if (dmi_ident[f] && strstr(dmi_ident[f], str))
883 return 1;
884 return 0;
885}
Andi Kleena1bae672006-10-21 18:37:02 +0200886
887/**
Jean Delvare66e13e62011-11-15 14:36:09 -0800888 * dmi_name_in_vendors - Check if string is in the DMI system or board vendor name
Jean Delvare02d9c472013-09-11 14:24:08 -0700889 * @str: Case sensitive Name
Andi Kleena1bae672006-10-21 18:37:02 +0200890 */
Jeff Garzik18552562007-10-03 15:15:40 -0400891int dmi_name_in_vendors(const char *str)
Andi Kleena1bae672006-10-21 18:37:02 +0200892{
Jean Delvare66e13e62011-11-15 14:36:09 -0800893 static int fields[] = { DMI_SYS_VENDOR, DMI_BOARD_VENDOR, DMI_NONE };
Andi Kleena1bae672006-10-21 18:37:02 +0200894 int i;
895 for (i = 0; fields[i] != DMI_NONE; i++) {
896 int f = fields[i];
897 if (dmi_ident[f] && strstr(dmi_ident[f], str))
898 return 1;
899 }
900 return 0;
901}
902EXPORT_SYMBOL(dmi_name_in_vendors);
903
Andrey Paninebad6a42005-09-06 15:18:29 -0700904/**
905 * dmi_find_device - find onboard device by type/name
906 * @type: device type or %DMI_DEV_TYPE_ANY to match all device types
Randy Dunlapb0ef3712006-06-25 05:49:18 -0700907 * @name: device name string or %NULL to match all
Andrey Paninebad6a42005-09-06 15:18:29 -0700908 * @from: previous device found in search, or %NULL for new search.
909 *
910 * Iterates through the list of known onboard devices. If a device is
Jean Delvarebfab8b482016-01-15 22:08:44 +0100911 * found with a matching @type and @name, a pointer to its device
Andrey Paninebad6a42005-09-06 15:18:29 -0700912 * structure is returned. Otherwise, %NULL is returned.
Randy Dunlapb0ef3712006-06-25 05:49:18 -0700913 * A new search is initiated by passing %NULL as the @from argument.
Andrey Paninebad6a42005-09-06 15:18:29 -0700914 * If @from is not %NULL, searches continue from next device.
915 */
Jean Delvare02d9c472013-09-11 14:24:08 -0700916const struct dmi_device *dmi_find_device(int type, const char *name,
Jeff Garzik18552562007-10-03 15:15:40 -0400917 const struct dmi_device *from)
Andrey Paninebad6a42005-09-06 15:18:29 -0700918{
Jeff Garzik18552562007-10-03 15:15:40 -0400919 const struct list_head *head = from ? &from->list : &dmi_devices;
920 struct list_head *d;
Andrey Paninebad6a42005-09-06 15:18:29 -0700921
Jean Delvare02d9c472013-09-11 14:24:08 -0700922 for (d = head->next; d != &dmi_devices; d = d->next) {
Jeff Garzik18552562007-10-03 15:15:40 -0400923 const struct dmi_device *dev =
924 list_entry(d, struct dmi_device, list);
Andrey Paninebad6a42005-09-06 15:18:29 -0700925
926 if (((type == DMI_DEV_TYPE_ANY) || (dev->type == type)) &&
927 ((name == NULL) || (strcmp(dev->name, name) == 0)))
928 return dev;
929 }
930
931 return NULL;
932}
933EXPORT_SYMBOL(dmi_find_device);
Andi Kleenf083a322006-03-25 16:30:19 +0100934
935/**
Tejun Heo3e5cd1f2009-08-16 21:02:36 +0900936 * dmi_get_date - parse a DMI date
937 * @field: data index (see enum dmi_field)
938 * @yearp: optional out parameter for the year
939 * @monthp: optional out parameter for the month
940 * @dayp: optional out parameter for the day
Andi Kleenf083a322006-03-25 16:30:19 +0100941 *
Tejun Heo3e5cd1f2009-08-16 21:02:36 +0900942 * The date field is assumed to be in the form resembling
943 * [mm[/dd]]/yy[yy] and the result is stored in the out
944 * parameters any or all of which can be omitted.
945 *
946 * If the field doesn't exist, all out parameters are set to zero
947 * and false is returned. Otherwise, true is returned with any
948 * invalid part of date set to zero.
949 *
950 * On return, year, month and day are guaranteed to be in the
951 * range of [0,9999], [0,12] and [0,31] respectively.
Andi Kleenf083a322006-03-25 16:30:19 +0100952 */
Tejun Heo3e5cd1f2009-08-16 21:02:36 +0900953bool dmi_get_date(int field, int *yearp, int *monthp, int *dayp)
Andi Kleenf083a322006-03-25 16:30:19 +0100954{
Tejun Heo3e5cd1f2009-08-16 21:02:36 +0900955 int year = 0, month = 0, day = 0;
956 bool exists;
957 const char *s, *y;
Tejun Heo02c24fa2009-08-16 21:01:22 +0900958 char *e;
Andi Kleenf083a322006-03-25 16:30:19 +0100959
Tejun Heo3e5cd1f2009-08-16 21:02:36 +0900960 s = dmi_get_system_info(field);
961 exists = s;
962 if (!exists)
963 goto out;
Andi Kleenf083a322006-03-25 16:30:19 +0100964
Tejun Heo3e5cd1f2009-08-16 21:02:36 +0900965 /*
966 * Determine year first. We assume the date string resembles
967 * mm/dd/yy[yy] but the original code extracted only the year
968 * from the end. Keep the behavior in the spirit of no
969 * surprises.
970 */
971 y = strrchr(s, '/');
972 if (!y)
973 goto out;
974
975 y++;
976 year = simple_strtoul(y, &e, 10);
977 if (y != e && year < 100) { /* 2-digit year */
Andi Kleenf083a322006-03-25 16:30:19 +0100978 year += 1900;
979 if (year < 1996) /* no dates < spec 1.0 */
980 year += 100;
981 }
Tejun Heo3e5cd1f2009-08-16 21:02:36 +0900982 if (year > 9999) /* year should fit in %04d */
983 year = 0;
Andi Kleenf083a322006-03-25 16:30:19 +0100984
Tejun Heo3e5cd1f2009-08-16 21:02:36 +0900985 /* parse the mm and dd */
986 month = simple_strtoul(s, &e, 10);
987 if (s == e || *e != '/' || !month || month > 12) {
988 month = 0;
989 goto out;
990 }
991
992 s = e + 1;
993 day = simple_strtoul(s, &e, 10);
994 if (s == y || s == e || *e != '/' || day > 31)
995 day = 0;
996out:
997 if (yearp)
998 *yearp = year;
999 if (monthp)
1000 *monthp = month;
1001 if (dayp)
1002 *dayp = day;
1003 return exists;
Andi Kleenf083a322006-03-25 16:30:19 +01001004}
Tejun Heo3e5cd1f2009-08-16 21:02:36 +09001005EXPORT_SYMBOL(dmi_get_date);
Jean Delvare7fce0842007-11-03 17:29:20 +01001006
1007/**
1008 * dmi_walk - Walk the DMI table and get called back for every record
1009 * @decode: Callback function
Jean Delvaree7a19c562009-03-30 21:46:44 +02001010 * @private_data: Private data to be passed to the callback function
Jean Delvare7fce0842007-11-03 17:29:20 +01001011 *
Andy Lutomirskic9268202017-06-15 13:46:00 +02001012 * Returns 0 on success, -ENXIO if DMI is not selected or not present,
1013 * or a different negative error code if DMI walking fails.
Jean Delvare7fce0842007-11-03 17:29:20 +01001014 */
Jean Delvaree7a19c562009-03-30 21:46:44 +02001015int dmi_walk(void (*decode)(const struct dmi_header *, void *),
1016 void *private_data)
Jean Delvare7fce0842007-11-03 17:29:20 +01001017{
1018 u8 *buf;
1019
1020 if (!dmi_available)
Andy Lutomirskic9268202017-06-15 13:46:00 +02001021 return -ENXIO;
Jean Delvare7fce0842007-11-03 17:29:20 +01001022
Ard Biesheuvelcf074402014-01-23 15:54:39 -08001023 buf = dmi_remap(dmi_base, dmi_len);
Jean Delvare7fce0842007-11-03 17:29:20 +01001024 if (buf == NULL)
Andy Lutomirskic9268202017-06-15 13:46:00 +02001025 return -ENOMEM;
Jean Delvare7fce0842007-11-03 17:29:20 +01001026
Ivan Khoronzhukeb4c5ea2015-06-25 09:06:56 +02001027 dmi_decode_table(buf, decode, private_data);
Jean Delvare7fce0842007-11-03 17:29:20 +01001028
Ard Biesheuvelcf074402014-01-23 15:54:39 -08001029 dmi_unmap(buf);
Jean Delvare7fce0842007-11-03 17:29:20 +01001030 return 0;
1031}
1032EXPORT_SYMBOL_GPL(dmi_walk);
Jiri Slabyd61c72e2008-12-10 14:07:21 +01001033
1034/**
1035 * dmi_match - compare a string to the dmi field (if exists)
Randy Dunlapc2bacfc2009-01-06 14:41:40 -08001036 * @f: DMI field identifier
1037 * @str: string to compare the DMI field to
Jiri Slabyd61c72e2008-12-10 14:07:21 +01001038 *
1039 * Returns true if the requested field equals to the str (including NULL).
1040 */
1041bool dmi_match(enum dmi_field f, const char *str)
1042{
1043 const char *info = dmi_get_system_info(f);
1044
1045 if (info == NULL || str == NULL)
1046 return info == str;
1047
1048 return !strcmp(info, str);
1049}
1050EXPORT_SYMBOL_GPL(dmi_match);
Chen, Gongdd6dad42013-10-18 14:29:25 -07001051
1052void dmi_memdev_name(u16 handle, const char **bank, const char **device)
1053{
1054 int n;
1055
1056 if (dmi_memdev == NULL)
1057 return;
1058
1059 for (n = 0; n < dmi_memdev_nr; n++) {
1060 if (handle == dmi_memdev[n].handle) {
1061 *bank = dmi_memdev[n].bank;
1062 *device = dmi_memdev[n].device;
1063 break;
1064 }
1065 }
1066}
1067EXPORT_SYMBOL_GPL(dmi_memdev_name);