blob: 35ed56b9c34f141a3cdd33c42853bdfc35f13931 [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002#include <linux/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07003#include <linux/string.h>
4#include <linux/init.h>
5#include <linux/module.h>
Bjorn Helgaas8881cdc2010-10-27 15:32:59 -07006#include <linux/ctype.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include <linux/dmi.h>
Matt Domsch3ed3bce2006-03-26 01:37:03 -08008#include <linux/efi.h>
Mike Rapoport57c8a662018-10-30 15:09:49 -07009#include <linux/memblock.h>
Tony Luckd114a332012-07-20 13:15:20 -070010#include <linux/random.h>
Andi Kleenf2d3efe2006-03-25 16:30:22 +010011#include <asm/dmi.h>
Luck, Tony0841c042013-11-01 13:59:52 -070012#include <asm/unaligned.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013
Ivan Khoronzhukd7f96f972015-06-25 09:06:56 +020014struct kobject *dmi_kobj;
15EXPORT_SYMBOL_GPL(dmi_kobj);
16
Paul Jacksoncb5dd7c2008-05-14 08:15:16 -070017/*
18 * DMI stands for "Desktop Management Interface". It is part
19 * of and an antecedent to, SMBIOS, which stands for System
20 * Management BIOS. See further: http://www.dmtf.org/standards
21 */
Jean Delvarea7770ae2018-02-03 11:25:20 +010022static const char dmi_empty_string[] = "";
Parag Warudkar79da4722008-01-30 13:31:59 +010023
Ivan Khoronzhuk95be58d2015-02-18 13:33:20 +020024static u32 dmi_ver __initdata;
Ivan Khoronzhuk552e19d82015-02-18 13:33:21 +020025static u32 dmi_len;
26static u16 dmi_num;
Ivan Khoronzhukd7f96f972015-06-25 09:06:56 +020027static u8 smbios_entry_point[32];
28static int smbios_entry_point_size;
29
Tejun Heoc90fe6b2013-04-30 15:27:14 -070030/* DMI system identification string used during boot */
31static char dmi_ids_string[128] __initdata;
32
Chen, Gongdd6dad42013-10-18 14:29:25 -070033static struct dmi_memdev_info {
34 const char *device;
35 const char *bank;
Tony Luck6deae962018-03-12 11:24:29 -070036 u64 size; /* bytes */
Chen, Gongdd6dad42013-10-18 14:29:25 -070037 u16 handle;
38} *dmi_memdev;
39static int dmi_memdev_nr;
40
Jean Delvaref3069ae2008-02-23 15:23:46 -080041static const char * __init dmi_string_nosave(const struct dmi_header *dm, u8 s)
Linus Torvalds1da177e2005-04-16 15:20:36 -070042{
Jeff Garzik18552562007-10-03 15:15:40 -040043 const u8 *bp = ((u8 *) dm) + dm->length;
Jean Delvarea7770ae2018-02-03 11:25:20 +010044 const u8 *nsp;
Andrey Panin1249c512005-06-25 14:54:47 -070045
Andrey Paninc3c71202005-09-06 15:18:28 -070046 if (s) {
Jean Delvarea7770ae2018-02-03 11:25:20 +010047 while (--s > 0 && *bp)
Andrey Paninc3c71202005-09-06 15:18:28 -070048 bp += strlen(bp) + 1;
Andrey Paninc3c71202005-09-06 15:18:28 -070049
Jean Delvarea7770ae2018-02-03 11:25:20 +010050 /* Strings containing only spaces are considered empty */
51 nsp = bp;
52 while (*nsp == ' ')
53 nsp++;
54 if (*nsp != '\0')
Jean Delvaref3069ae2008-02-23 15:23:46 -080055 return bp;
Bjorn Helgaas4f705ae2006-04-03 17:09:22 -070056 }
Andrey Paninc3c71202005-09-06 15:18:28 -070057
Jean Delvarea7770ae2018-02-03 11:25:20 +010058 return dmi_empty_string;
Jean Delvaref3069ae2008-02-23 15:23:46 -080059}
60
Jean Delvareffbbb962013-09-11 14:24:09 -070061static const char * __init dmi_string(const struct dmi_header *dm, u8 s)
Jean Delvaref3069ae2008-02-23 15:23:46 -080062{
63 const char *bp = dmi_string_nosave(dm, s);
64 char *str;
65 size_t len;
66
67 if (bp == dmi_empty_string)
68 return dmi_empty_string;
69
70 len = strlen(bp) + 1;
71 str = dmi_alloc(len);
72 if (str != NULL)
73 strcpy(str, bp);
Jean Delvaref3069ae2008-02-23 15:23:46 -080074
Andrey Paninc3c71202005-09-06 15:18:28 -070075 return str;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076}
77
78/*
79 * We have to be cautious here. We have seen BIOSes with DMI pointers
80 * pointing to completely the wrong place for example
81 */
Ivan Khoronzhukeb4c5ea2015-06-25 09:06:56 +020082static void dmi_decode_table(u8 *buf,
83 void (*decode)(const struct dmi_header *, void *),
84 void *private_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085{
Jean Delvare7fce0842007-11-03 17:29:20 +010086 u8 *data = buf;
Andrey Panin1249c512005-06-25 14:54:47 -070087 int i = 0;
Bjorn Helgaas4f705ae2006-04-03 17:09:22 -070088
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 /*
Jean Delvarebfbaafa2015-03-20 09:59:47 +010090 * Stop when we have seen all the items the table claimed to have
Jean Delvare17cd5bd2015-06-25 09:06:55 +020091 * (SMBIOS < 3.0 only) OR we reach an end-of-table marker (SMBIOS
92 * >= 3.0 only) OR we run off the end of the table (should never
93 * happen but sometimes does on bogus implementations.)
Bjorn Helgaas4f705ae2006-04-03 17:09:22 -070094 */
Linus Torvalds9c65e122015-04-13 10:22:30 -070095 while ((!dmi_num || i < dmi_num) &&
96 (data - buf + sizeof(struct dmi_header)) <= dmi_len) {
Jeff Garzik18552562007-10-03 15:15:40 -040097 const struct dmi_header *dm = (const struct dmi_header *)data;
98
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 /*
Alan Cox86385452008-11-07 16:03:46 +0000100 * We want to know the total length (formatted area and
101 * strings) before decoding to make sure we won't run off the
102 * table in dmi_decode or dmi_string
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 */
Andrey Panin1249c512005-06-25 14:54:47 -0700104 data += dm->length;
Ivan Khoronzhuk552e19d82015-02-18 13:33:21 +0200105 while ((data - buf < dmi_len - 1) && (data[0] || data[1]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 data++;
Ivan Khoronzhuk552e19d82015-02-18 13:33:21 +0200107 if (data - buf < dmi_len - 1)
Jean Delvaree7a19c562009-03-30 21:46:44 +0200108 decode(dm, private_data);
Ivan Khoronzhukce204e92015-02-18 15:51:41 +0200109
Jean Delvare6e0ad592015-06-25 09:06:56 +0200110 data += 2;
111 i++;
112
Ivan Khoronzhukce204e92015-02-18 15:51:41 +0200113 /*
114 * 7.45 End-of-Table (Type 127) [SMBIOS reference spec v3.0.0]
Jean Delvare17cd5bd2015-06-25 09:06:55 +0200115 * For tables behind a 64-bit entry point, we have no item
116 * count and no exact table length, so stop on end-of-table
117 * marker. For tables behind a 32-bit entry point, we have
118 * seen OEM structures behind the end-of-table marker on
119 * some systems, so don't trust it.
Ivan Khoronzhukce204e92015-02-18 15:51:41 +0200120 */
Jean Delvare17cd5bd2015-06-25 09:06:55 +0200121 if (!dmi_num && dm->type == DMI_ENTRY_END_OF_TABLE)
Ivan Khoronzhukce204e92015-02-18 15:51:41 +0200122 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 }
Jean Delvare6e0ad592015-06-25 09:06:56 +0200124
125 /* Trim DMI table length if needed */
126 if (dmi_len > data - buf)
127 dmi_len = data - buf;
Jean Delvare7fce0842007-11-03 17:29:20 +0100128}
129
Ard Biesheuvelfc430262014-10-14 16:41:27 +0200130static phys_addr_t dmi_base;
Jean Delvare7fce0842007-11-03 17:29:20 +0100131
Jean Delvaree7a19c562009-03-30 21:46:44 +0200132static int __init dmi_walk_early(void (*decode)(const struct dmi_header *,
133 void *))
Jean Delvare7fce0842007-11-03 17:29:20 +0100134{
135 u8 *buf;
Jean Delvare6e0ad592015-06-25 09:06:56 +0200136 u32 orig_dmi_len = dmi_len;
Jean Delvare7fce0842007-11-03 17:29:20 +0100137
Jean Delvare6e0ad592015-06-25 09:06:56 +0200138 buf = dmi_early_remap(dmi_base, orig_dmi_len);
Jean Delvare7fce0842007-11-03 17:29:20 +0100139 if (buf == NULL)
Andy Lutomirskic9268202017-06-15 13:46:00 +0200140 return -ENOMEM;
Jean Delvare7fce0842007-11-03 17:29:20 +0100141
Ivan Khoronzhukeb4c5ea2015-06-25 09:06:56 +0200142 dmi_decode_table(buf, decode, NULL);
Jean Delvare7fce0842007-11-03 17:29:20 +0100143
Tony Luckd114a332012-07-20 13:15:20 -0700144 add_device_randomness(buf, dmi_len);
145
Jean Delvare6e0ad592015-06-25 09:06:56 +0200146 dmi_early_unmap(buf, orig_dmi_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 return 0;
148}
149
Zhenzhong Duan9f9c9cbb2012-12-20 15:05:14 -0800150static int __init dmi_checksum(const u8 *buf, u8 len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151{
Andrey Panin1249c512005-06-25 14:54:47 -0700152 u8 sum = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 int a;
Bjorn Helgaas4f705ae2006-04-03 17:09:22 -0700154
Zhenzhong Duan9f9c9cbb2012-12-20 15:05:14 -0800155 for (a = 0; a < len; a++)
Andrey Panin1249c512005-06-25 14:54:47 -0700156 sum += buf[a];
157
158 return sum == 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159}
160
Jean Delvareffbbb962013-09-11 14:24:09 -0700161static const char *dmi_ident[DMI_STRING_MAX];
Andrey Paninebad6a42005-09-06 15:18:29 -0700162static LIST_HEAD(dmi_devices);
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200163int dmi_available;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
165/*
166 * Save a DMI string
167 */
Jean Delvare02d9c472013-09-11 14:24:08 -0700168static void __init dmi_save_ident(const struct dmi_header *dm, int slot,
169 int string)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170{
Jean Delvare02d9c472013-09-11 14:24:08 -0700171 const char *d = (const char *) dm;
Jean Delvareffbbb962013-09-11 14:24:09 -0700172 const char *p;
Andrey Panin1249c512005-06-25 14:54:47 -0700173
Jean Delvarea814c352017-06-15 13:46:01 +0200174 if (dmi_ident[slot] || dm->length <= string)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 return;
Andrey Panin1249c512005-06-25 14:54:47 -0700176
Andrey Paninc3c71202005-09-06 15:18:28 -0700177 p = dmi_string(dm, d[string]);
178 if (p == NULL)
179 return;
180
181 dmi_ident[slot] = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182}
183
Jean Delvare02d9c472013-09-11 14:24:08 -0700184static void __init dmi_save_uuid(const struct dmi_header *dm, int slot,
185 int index)
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200186{
Jean Delvarea814c352017-06-15 13:46:01 +0200187 const u8 *d;
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200188 char *s;
189 int is_ff = 1, is_00 = 1, i;
190
Jean Delvare90fe6f82018-04-13 15:37:59 +0200191 if (dmi_ident[slot] || dm->length < index + 16)
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200192 return;
193
Jean Delvarea814c352017-06-15 13:46:01 +0200194 d = (u8 *) dm + index;
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200195 for (i = 0; i < 16 && (is_ff || is_00); i++) {
Zhenzhong Duanf1d8e612012-12-20 15:05:13 -0800196 if (d[i] != 0x00)
197 is_00 = 0;
198 if (d[i] != 0xFF)
199 is_ff = 0;
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200200 }
201
202 if (is_ff || is_00)
203 return;
204
205 s = dmi_alloc(16*2+4+1);
206 if (!s)
207 return;
208
Zhenzhong Duanf1d8e612012-12-20 15:05:13 -0800209 /*
210 * As of version 2.6 of the SMBIOS specification, the first 3 fields of
211 * the UUID are supposed to be little-endian encoded. The specification
212 * says that this is the defacto standard.
213 */
Ivan Khoronzhuk95be58d2015-02-18 13:33:20 +0200214 if (dmi_ver >= 0x020600)
Jean Delvare712ff252018-04-13 15:37:59 +0200215 sprintf(s, "%pUl", d);
Zhenzhong Duanf1d8e612012-12-20 15:05:13 -0800216 else
Jean Delvare712ff252018-04-13 15:37:59 +0200217 sprintf(s, "%pUb", d);
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200218
Jean Delvare02d9c472013-09-11 14:24:08 -0700219 dmi_ident[slot] = s;
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200220}
221
Jean Delvare02d9c472013-09-11 14:24:08 -0700222static void __init dmi_save_type(const struct dmi_header *dm, int slot,
223 int index)
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200224{
Jean Delvarea814c352017-06-15 13:46:01 +0200225 const u8 *d;
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200226 char *s;
227
Jean Delvarea814c352017-06-15 13:46:01 +0200228 if (dmi_ident[slot] || dm->length <= index)
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200229 return;
230
231 s = dmi_alloc(4);
232 if (!s)
233 return;
234
Jean Delvarea814c352017-06-15 13:46:01 +0200235 d = (u8 *) dm + index;
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200236 sprintf(s, "%u", *d & 0x7F);
237 dmi_ident[slot] = s;
238}
239
Jean Delvaref3069ae2008-02-23 15:23:46 -0800240static void __init dmi_save_one_device(int type, const char *name)
241{
242 struct dmi_device *dev;
243
244 /* No duplicate device */
245 if (dmi_find_device(type, name, NULL))
246 return;
247
248 dev = dmi_alloc(sizeof(*dev) + strlen(name) + 1);
Jean Delvareae797442013-09-11 14:24:10 -0700249 if (!dev)
Jean Delvaref3069ae2008-02-23 15:23:46 -0800250 return;
Jean Delvaref3069ae2008-02-23 15:23:46 -0800251
252 dev->type = type;
253 strcpy((char *)(dev + 1), name);
254 dev->name = (char *)(dev + 1);
255 dev->device_data = NULL;
256 list_add(&dev->list, &dmi_devices);
257}
258
Jeff Garzik18552562007-10-03 15:15:40 -0400259static void __init dmi_save_devices(const struct dmi_header *dm)
Andrey Paninebad6a42005-09-06 15:18:29 -0700260{
261 int i, count = (dm->length - sizeof(struct dmi_header)) / 2;
Andrey Paninebad6a42005-09-06 15:18:29 -0700262
263 for (i = 0; i < count; i++) {
Jeff Garzik18552562007-10-03 15:15:40 -0400264 const char *d = (char *)(dm + 1) + (i * 2);
Andrey Paninebad6a42005-09-06 15:18:29 -0700265
266 /* Skip disabled device */
267 if ((*d & 0x80) == 0)
268 continue;
269
Jean Delvaref3069ae2008-02-23 15:23:46 -0800270 dmi_save_one_device(*d & 0x7f, dmi_string_nosave(dm, *(d + 1)));
Shem Multinymous2e0c1f62006-09-29 01:59:37 -0700271 }
272}
273
Jeff Garzik18552562007-10-03 15:15:40 -0400274static void __init dmi_save_oem_strings_devices(const struct dmi_header *dm)
Shem Multinymous2e0c1f62006-09-29 01:59:37 -0700275{
Jean Delvarea814c352017-06-15 13:46:01 +0200276 int i, count;
Shem Multinymous2e0c1f62006-09-29 01:59:37 -0700277 struct dmi_device *dev;
278
Jean Delvarea814c352017-06-15 13:46:01 +0200279 if (dm->length < 0x05)
280 return;
281
282 count = *(u8 *)(dm + 1);
Shem Multinymous2e0c1f62006-09-29 01:59:37 -0700283 for (i = 1; i <= count; i++) {
Jean Delvareffbbb962013-09-11 14:24:09 -0700284 const char *devname = dmi_string(dm, i);
Parag Warudkar79da4722008-01-30 13:31:59 +0100285
Jean Delvare43fe1052008-02-23 15:23:55 -0800286 if (devname == dmi_empty_string)
Parag Warudkar79da4722008-01-30 13:31:59 +0100287 continue;
Parag Warudkar79da4722008-01-30 13:31:59 +0100288
Shem Multinymous2e0c1f62006-09-29 01:59:37 -0700289 dev = dmi_alloc(sizeof(*dev));
Jean Delvareae797442013-09-11 14:24:10 -0700290 if (!dev)
Shem Multinymous2e0c1f62006-09-29 01:59:37 -0700291 break;
Shem Multinymous2e0c1f62006-09-29 01:59:37 -0700292
293 dev->type = DMI_DEV_TYPE_OEM_STRING;
Parag Warudkar79da4722008-01-30 13:31:59 +0100294 dev->name = devname;
Shem Multinymous2e0c1f62006-09-29 01:59:37 -0700295 dev->device_data = NULL;
Andrey Paninebad6a42005-09-06 15:18:29 -0700296
297 list_add(&dev->list, &dmi_devices);
298 }
299}
300
Jeff Garzik18552562007-10-03 15:15:40 -0400301static void __init dmi_save_ipmi_device(const struct dmi_header *dm)
Andrey Paninebad6a42005-09-06 15:18:29 -0700302{
303 struct dmi_device *dev;
Jean Delvare02d9c472013-09-11 14:24:08 -0700304 void *data;
Andrey Paninebad6a42005-09-06 15:18:29 -0700305
Andi Kleene9928672006-01-11 22:43:33 +0100306 data = dmi_alloc(dm->length);
Jean Delvareae797442013-09-11 14:24:10 -0700307 if (data == NULL)
Andrey Paninebad6a42005-09-06 15:18:29 -0700308 return;
Andrey Paninebad6a42005-09-06 15:18:29 -0700309
310 memcpy(data, dm, dm->length);
311
Andi Kleene9928672006-01-11 22:43:33 +0100312 dev = dmi_alloc(sizeof(*dev));
Jean Delvareae797442013-09-11 14:24:10 -0700313 if (!dev)
Andrey Paninebad6a42005-09-06 15:18:29 -0700314 return;
Andrey Paninebad6a42005-09-06 15:18:29 -0700315
316 dev->type = DMI_DEV_TYPE_IPMI;
317 dev->name = "IPMI controller";
318 dev->device_data = data;
319
Carol Hebertabd24df2008-04-04 14:30:03 -0700320 list_add_tail(&dev->list, &dmi_devices);
Andrey Paninebad6a42005-09-06 15:18:29 -0700321}
322
Jordan Hargravee5b6c152016-01-15 22:08:45 +0100323static void __init dmi_save_dev_pciaddr(int instance, int segment, int bus,
324 int devfn, const char *name, int type)
Narendra K911e1c92010-07-26 05:56:50 -0500325{
Jordan Hargravee5b6c152016-01-15 22:08:45 +0100326 struct dmi_dev_onboard *dev;
Narendra K911e1c92010-07-26 05:56:50 -0500327
Jordan Hargravee5b6c152016-01-15 22:08:45 +0100328 /* Ignore invalid values */
329 if (type == DMI_DEV_TYPE_DEV_SLOT &&
330 segment == 0xFFFF && bus == 0xFF && devfn == 0xFF)
Narendra K911e1c92010-07-26 05:56:50 -0500331 return;
Jean Delvareae797442013-09-11 14:24:10 -0700332
Jordan Hargravee5b6c152016-01-15 22:08:45 +0100333 dev = dmi_alloc(sizeof(*dev) + strlen(name) + 1);
334 if (!dev)
335 return;
Narendra K911e1c92010-07-26 05:56:50 -0500336
Jordan Hargravee5b6c152016-01-15 22:08:45 +0100337 dev->instance = instance;
338 dev->segment = segment;
339 dev->bus = bus;
340 dev->devfn = devfn;
Narendra K911e1c92010-07-26 05:56:50 -0500341
Jordan Hargravee5b6c152016-01-15 22:08:45 +0100342 strcpy((char *)&dev[1], name);
343 dev->dev.type = type;
344 dev->dev.name = (char *)&dev[1];
345 dev->dev.device_data = dev;
346
347 list_add(&dev->dev.list, &dmi_devices);
Narendra K911e1c92010-07-26 05:56:50 -0500348}
349
Wim Van Sebroeckb4bd7d52008-02-08 04:20:58 -0800350static void __init dmi_save_extended_devices(const struct dmi_header *dm)
351{
Jean Delvare96e23942016-01-15 22:08:44 +0100352 const char *name;
Jean Delvare45b98252016-01-15 22:08:44 +0100353 const u8 *d = (u8 *)dm;
Wim Van Sebroeckb4bd7d52008-02-08 04:20:58 -0800354
Jean Delvarea814c352017-06-15 13:46:01 +0200355 if (dm->length < 0x0B)
356 return;
357
Wim Van Sebroeckb4bd7d52008-02-08 04:20:58 -0800358 /* Skip disabled device */
Jean Delvare45b98252016-01-15 22:08:44 +0100359 if ((d[0x5] & 0x80) == 0)
Wim Van Sebroeckb4bd7d52008-02-08 04:20:58 -0800360 return;
361
Jean Delvare45b98252016-01-15 22:08:44 +0100362 name = dmi_string_nosave(dm, d[0x4]);
Jordan Hargravee5b6c152016-01-15 22:08:45 +0100363 dmi_save_dev_pciaddr(d[0x6], *(u16 *)(d + 0x7), d[0x9], d[0xA], name,
364 DMI_DEV_TYPE_DEV_ONBOARD);
Jean Delvare45b98252016-01-15 22:08:44 +0100365 dmi_save_one_device(d[0x5] & 0x7f, name);
Wim Van Sebroeckb4bd7d52008-02-08 04:20:58 -0800366}
367
Jordan Hargravee5b6c152016-01-15 22:08:45 +0100368static void __init dmi_save_system_slot(const struct dmi_header *dm)
369{
370 const u8 *d = (u8 *)dm;
371
372 /* Need SMBIOS 2.6+ structure */
373 if (dm->length < 0x11)
374 return;
375 dmi_save_dev_pciaddr(*(u16 *)(d + 0x9), *(u16 *)(d + 0xD), d[0xF],
376 d[0x10], dmi_string_nosave(dm, d[0x4]),
377 DMI_DEV_TYPE_DEV_SLOT);
378}
379
Chen, Gongdd6dad42013-10-18 14:29:25 -0700380static void __init count_mem_devices(const struct dmi_header *dm, void *v)
381{
382 if (dm->type != DMI_ENTRY_MEM_DEVICE)
383 return;
384 dmi_memdev_nr++;
385}
386
387static void __init save_mem_devices(const struct dmi_header *dm, void *v)
388{
389 const char *d = (const char *)dm;
390 static int nr;
Tony Luck6deae962018-03-12 11:24:29 -0700391 u64 bytes;
392 u16 size;
Chen, Gongdd6dad42013-10-18 14:29:25 -0700393
Jean Delvarea814c352017-06-15 13:46:01 +0200394 if (dm->type != DMI_ENTRY_MEM_DEVICE || dm->length < 0x12)
Chen, Gongdd6dad42013-10-18 14:29:25 -0700395 return;
396 if (nr >= dmi_memdev_nr) {
397 pr_warn(FW_BUG "Too many DIMM entries in SMBIOS table\n");
398 return;
399 }
Luck, Tony0841c042013-11-01 13:59:52 -0700400 dmi_memdev[nr].handle = get_unaligned(&dm->handle);
Chen, Gongdd6dad42013-10-18 14:29:25 -0700401 dmi_memdev[nr].device = dmi_string(dm, d[0x10]);
402 dmi_memdev[nr].bank = dmi_string(dm, d[0x11]);
Tony Luck6deae962018-03-12 11:24:29 -0700403
404 size = get_unaligned((u16 *)&d[0xC]);
405 if (size == 0)
406 bytes = 0;
407 else if (size == 0xffff)
408 bytes = ~0ull;
409 else if (size & 0x8000)
410 bytes = (u64)(size & 0x7fff) << 10;
411 else if (size != 0x7fff)
412 bytes = (u64)size << 20;
413 else
414 bytes = (u64)get_unaligned((u32 *)&d[0x1C]) << 20;
415
416 dmi_memdev[nr].size = bytes;
Chen, Gongdd6dad42013-10-18 14:29:25 -0700417 nr++;
418}
419
Robert Richter0fca0812019-03-28 20:34:28 +0100420static void __init dmi_memdev_walk(void)
Chen, Gongdd6dad42013-10-18 14:29:25 -0700421{
Chen, Gongdd6dad42013-10-18 14:29:25 -0700422 if (dmi_walk_early(count_mem_devices) == 0 && dmi_memdev_nr) {
423 dmi_memdev = dmi_alloc(sizeof(*dmi_memdev) * dmi_memdev_nr);
424 if (dmi_memdev)
425 dmi_walk_early(save_mem_devices);
426 }
427}
428
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 * Process a DMI table entry. Right now all we care about are the BIOS
431 * and machine entries. For 2.5 we should pull the smbus controller info
432 * out of here.
433 */
Jean Delvaree7a19c562009-03-30 21:46:44 +0200434static void __init dmi_decode(const struct dmi_header *dm, void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435{
Jean Delvare02d9c472013-09-11 14:24:08 -0700436 switch (dm->type) {
Andrey Paninebad6a42005-09-06 15:18:29 -0700437 case 0: /* BIOS Information */
Andrey Panin1249c512005-06-25 14:54:47 -0700438 dmi_save_ident(dm, DMI_BIOS_VENDOR, 4);
Andrey Panin1249c512005-06-25 14:54:47 -0700439 dmi_save_ident(dm, DMI_BIOS_VERSION, 5);
Andrey Panin1249c512005-06-25 14:54:47 -0700440 dmi_save_ident(dm, DMI_BIOS_DATE, 8);
441 break;
Andrey Paninebad6a42005-09-06 15:18:29 -0700442 case 1: /* System Information */
Andrey Panin1249c512005-06-25 14:54:47 -0700443 dmi_save_ident(dm, DMI_SYS_VENDOR, 4);
Andrey Panin1249c512005-06-25 14:54:47 -0700444 dmi_save_ident(dm, DMI_PRODUCT_NAME, 5);
Andrey Panin1249c512005-06-25 14:54:47 -0700445 dmi_save_ident(dm, DMI_PRODUCT_VERSION, 6);
Andrey Panin1249c512005-06-25 14:54:47 -0700446 dmi_save_ident(dm, DMI_PRODUCT_SERIAL, 7);
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200447 dmi_save_uuid(dm, DMI_PRODUCT_UUID, 8);
Simon Glassb23908d2018-06-17 14:09:42 +0200448 dmi_save_ident(dm, DMI_PRODUCT_SKU, 25);
Mika Westerbergc61872c2017-05-17 13:25:12 +0300449 dmi_save_ident(dm, DMI_PRODUCT_FAMILY, 26);
Andrey Panin1249c512005-06-25 14:54:47 -0700450 break;
Andrey Paninebad6a42005-09-06 15:18:29 -0700451 case 2: /* Base Board Information */
Andrey Panin1249c512005-06-25 14:54:47 -0700452 dmi_save_ident(dm, DMI_BOARD_VENDOR, 4);
Andrey Panin1249c512005-06-25 14:54:47 -0700453 dmi_save_ident(dm, DMI_BOARD_NAME, 5);
Andrey Panin1249c512005-06-25 14:54:47 -0700454 dmi_save_ident(dm, DMI_BOARD_VERSION, 6);
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200455 dmi_save_ident(dm, DMI_BOARD_SERIAL, 7);
456 dmi_save_ident(dm, DMI_BOARD_ASSET_TAG, 8);
457 break;
458 case 3: /* Chassis Information */
459 dmi_save_ident(dm, DMI_CHASSIS_VENDOR, 4);
460 dmi_save_type(dm, DMI_CHASSIS_TYPE, 5);
461 dmi_save_ident(dm, DMI_CHASSIS_VERSION, 6);
462 dmi_save_ident(dm, DMI_CHASSIS_SERIAL, 7);
463 dmi_save_ident(dm, DMI_CHASSIS_ASSET_TAG, 8);
Andrey Panin1249c512005-06-25 14:54:47 -0700464 break;
Jordan Hargravee5b6c152016-01-15 22:08:45 +0100465 case 9: /* System Slots */
466 dmi_save_system_slot(dm);
467 break;
Andrey Paninebad6a42005-09-06 15:18:29 -0700468 case 10: /* Onboard Devices Information */
469 dmi_save_devices(dm);
470 break;
Shem Multinymous2e0c1f62006-09-29 01:59:37 -0700471 case 11: /* OEM Strings */
472 dmi_save_oem_strings_devices(dm);
473 break;
Andrey Paninebad6a42005-09-06 15:18:29 -0700474 case 38: /* IPMI Device Information */
475 dmi_save_ipmi_device(dm);
Wim Van Sebroeckb4bd7d52008-02-08 04:20:58 -0800476 break;
477 case 41: /* Onboard Devices Extended Information */
478 dmi_save_extended_devices(dm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 }
480}
481
Tejun Heoc90fe6b2013-04-30 15:27:14 -0700482static int __init print_filtered(char *buf, size_t len, const char *info)
Bjorn Helgaas8881cdc2010-10-27 15:32:59 -0700483{
Tejun Heoc90fe6b2013-04-30 15:27:14 -0700484 int c = 0;
Bjorn Helgaas8881cdc2010-10-27 15:32:59 -0700485 const char *p;
486
487 if (!info)
Tejun Heoc90fe6b2013-04-30 15:27:14 -0700488 return c;
Bjorn Helgaas8881cdc2010-10-27 15:32:59 -0700489
490 for (p = info; *p; p++)
491 if (isprint(*p))
Tejun Heoc90fe6b2013-04-30 15:27:14 -0700492 c += scnprintf(buf + c, len - c, "%c", *p);
Bjorn Helgaas8881cdc2010-10-27 15:32:59 -0700493 else
Tejun Heoc90fe6b2013-04-30 15:27:14 -0700494 c += scnprintf(buf + c, len - c, "\\x%02x", *p & 0xff);
495 return c;
Bjorn Helgaas8881cdc2010-10-27 15:32:59 -0700496}
497
Tejun Heoc90fe6b2013-04-30 15:27:14 -0700498static void __init dmi_format_ids(char *buf, size_t len)
Bjorn Helgaas8881cdc2010-10-27 15:32:59 -0700499{
Tejun Heoc90fe6b2013-04-30 15:27:14 -0700500 int c = 0;
Naga Chumbalkar84e383b2011-02-14 22:47:17 +0000501 const char *board; /* Board Name is optional */
502
Tejun Heoc90fe6b2013-04-30 15:27:14 -0700503 c += print_filtered(buf + c, len - c,
504 dmi_get_system_info(DMI_SYS_VENDOR));
505 c += scnprintf(buf + c, len - c, " ");
506 c += print_filtered(buf + c, len - c,
507 dmi_get_system_info(DMI_PRODUCT_NAME));
508
Naga Chumbalkar84e383b2011-02-14 22:47:17 +0000509 board = dmi_get_system_info(DMI_BOARD_NAME);
510 if (board) {
Tejun Heoc90fe6b2013-04-30 15:27:14 -0700511 c += scnprintf(buf + c, len - c, "/");
512 c += print_filtered(buf + c, len - c, board);
Naga Chumbalkar84e383b2011-02-14 22:47:17 +0000513 }
Tejun Heoc90fe6b2013-04-30 15:27:14 -0700514 c += scnprintf(buf + c, len - c, ", BIOS ");
515 c += print_filtered(buf + c, len - c,
516 dmi_get_system_info(DMI_BIOS_VERSION));
517 c += scnprintf(buf + c, len - c, " ");
518 c += print_filtered(buf + c, len - c,
519 dmi_get_system_info(DMI_BIOS_DATE));
Bjorn Helgaas8881cdc2010-10-27 15:32:59 -0700520}
521
Ben Hutchingsd39de282013-07-31 13:53:30 -0700522/*
523 * Check for DMI/SMBIOS headers in the system firmware image. Any
524 * SMBIOS header must start 16 bytes before the DMI header, so take a
525 * 32 byte buffer and check for DMI at offset 16 and SMBIOS at offset
526 * 0. If the DMI header is present, set dmi_ver accordingly (SMBIOS
527 * takes precedence) and return 0. Otherwise return 1.
528 */
Ben Hutchings79bae422013-04-30 15:27:46 -0700529static int __init dmi_present(const u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530{
Ivan Khoronzhuk95be58d2015-02-18 13:33:20 +0200531 u32 smbios_ver;
Jeff Garzik18552562007-10-03 15:15:40 -0400532
Ben Hutchings79bae422013-04-30 15:27:46 -0700533 if (memcmp(buf, "_SM_", 4) == 0 &&
534 buf[5] < 32 && dmi_checksum(buf, buf[5])) {
Ard Biesheuvelfc430262014-10-14 16:41:27 +0200535 smbios_ver = get_unaligned_be16(buf + 6);
Ivan Khoronzhukd7f96f972015-06-25 09:06:56 +0200536 smbios_entry_point_size = buf[5];
537 memcpy(smbios_entry_point, buf, smbios_entry_point_size);
Ben Hutchings79bae422013-04-30 15:27:46 -0700538
539 /* Some BIOS report weird SMBIOS version, fix that up */
540 switch (smbios_ver) {
541 case 0x021F:
542 case 0x0221:
Jean Delvared1d87042015-06-25 09:06:57 +0200543 pr_debug("SMBIOS version fixup (2.%d->2.%d)\n",
Ben Hutchings79bae422013-04-30 15:27:46 -0700544 smbios_ver & 0xFF, 3);
545 smbios_ver = 0x0203;
546 break;
547 case 0x0233:
Jean Delvared1d87042015-06-25 09:06:57 +0200548 pr_debug("SMBIOS version fixup (2.%d->2.%d)\n", 51, 6);
Ben Hutchings79bae422013-04-30 15:27:46 -0700549 smbios_ver = 0x0206;
550 break;
551 }
552 } else {
553 smbios_ver = 0;
554 }
555
556 buf += 16;
557
558 if (memcmp(buf, "_DMI_", 5) == 0 && dmi_checksum(buf, 15)) {
Jean Delvare5c1ac562015-05-14 14:40:50 +0200559 if (smbios_ver)
560 dmi_ver = smbios_ver;
561 else
562 dmi_ver = (buf[14] & 0xF0) << 4 | (buf[14] & 0x0F);
Andrea Arcangeliff4319d2016-01-08 09:00:54 +0100563 dmi_ver <<= 8;
Ard Biesheuvelfc430262014-10-14 16:41:27 +0200564 dmi_num = get_unaligned_le16(buf + 12);
565 dmi_len = get_unaligned_le16(buf + 6);
566 dmi_base = get_unaligned_le32(buf + 8);
Matt Domsch3ed3bce2006-03-26 01:37:03 -0800567
Bjorn Helgaas8881cdc2010-10-27 15:32:59 -0700568 if (dmi_walk_early(dmi_decode) == 0) {
Ben Hutchings79bae422013-04-30 15:27:46 -0700569 if (smbios_ver) {
Jean Delvarec2493042015-05-14 14:40:50 +0200570 pr_info("SMBIOS %d.%d present.\n",
Andrea Arcangeliff4319d2016-01-08 09:00:54 +0100571 dmi_ver >> 16, (dmi_ver >> 8) & 0xFF);
Ben Hutchings79bae422013-04-30 15:27:46 -0700572 } else {
Ivan Khoronzhukd7f96f972015-06-25 09:06:56 +0200573 smbios_entry_point_size = 15;
574 memcpy(smbios_entry_point, buf,
575 smbios_entry_point_size);
Zhenzhong Duan9f9c9cbb2012-12-20 15:05:14 -0800576 pr_info("Legacy DMI %d.%d present.\n",
Andrea Arcangeliff4319d2016-01-08 09:00:54 +0100577 dmi_ver >> 16, (dmi_ver >> 8) & 0xFF);
Zhenzhong Duan9f9c9cbb2012-12-20 15:05:14 -0800578 }
Tejun Heoc90fe6b2013-04-30 15:27:14 -0700579 dmi_format_ids(dmi_ids_string, sizeof(dmi_ids_string));
Kefeng Wangd4af49f2016-12-19 10:01:47 +0100580 pr_info("DMI: %s\n", dmi_ids_string);
Matt Domsch3ed3bce2006-03-26 01:37:03 -0800581 return 0;
Bjorn Helgaas8881cdc2010-10-27 15:32:59 -0700582 }
Matt Domsch3ed3bce2006-03-26 01:37:03 -0800583 }
Matt Domsch3ed3bce2006-03-26 01:37:03 -0800584
Ben Hutchingsa40e7cf2013-03-08 12:43:32 -0800585 return 1;
Zhenzhong Duan9f9c9cbb2012-12-20 15:05:14 -0800586}
587
Ard Biesheuvelfc430262014-10-14 16:41:27 +0200588/*
589 * Check for the SMBIOS 3.0 64-bit entry point signature. Unlike the legacy
590 * 32-bit entry point, there is no embedded DMI header (_DMI_) in here.
591 */
592static int __init dmi_smbios3_present(const u8 *buf)
593{
594 if (memcmp(buf, "_SM3_", 5) == 0 &&
595 buf[6] < 32 && dmi_checksum(buf, buf[6])) {
Jean Delvared1d87042015-06-25 09:06:57 +0200596 dmi_ver = get_unaligned_be32(buf + 6) & 0xFFFFFF;
Jean Delvarebfbaafa2015-03-20 09:59:47 +0100597 dmi_num = 0; /* No longer specified */
Ard Biesheuvelfc430262014-10-14 16:41:27 +0200598 dmi_len = get_unaligned_le32(buf + 12);
599 dmi_base = get_unaligned_le64(buf + 16);
Ivan Khoronzhukd7f96f972015-06-25 09:06:56 +0200600 smbios_entry_point_size = buf[6];
601 memcpy(smbios_entry_point, buf, smbios_entry_point_size);
Ard Biesheuvelfc430262014-10-14 16:41:27 +0200602
Ard Biesheuvelfc430262014-10-14 16:41:27 +0200603 if (dmi_walk_early(dmi_decode) == 0) {
Ivan Khoronzhuk95be58d2015-02-18 13:33:20 +0200604 pr_info("SMBIOS %d.%d.%d present.\n",
605 dmi_ver >> 16, (dmi_ver >> 8) & 0xFF,
606 dmi_ver & 0xFF);
Ard Biesheuvelfc430262014-10-14 16:41:27 +0200607 dmi_format_ids(dmi_ids_string, sizeof(dmi_ids_string));
Kefeng Wangd4af49f2016-12-19 10:01:47 +0100608 pr_info("DMI: %s\n", dmi_ids_string);
Ard Biesheuvelfc430262014-10-14 16:41:27 +0200609 return 0;
610 }
611 }
612 return 1;
613}
614
Robert Richter0fca0812019-03-28 20:34:28 +0100615static void __init dmi_scan_machine(void)
Matt Domsch3ed3bce2006-03-26 01:37:03 -0800616{
Andrey Panin61e032f2005-09-06 15:18:26 -0700617 char __iomem *p, *q;
Ben Hutchings79bae422013-04-30 15:27:46 -0700618 char buf[32];
Andrey Panin61e032f2005-09-06 15:18:26 -0700619
Matt Fleming83e68182012-11-14 09:42:35 +0000620 if (efi_enabled(EFI_CONFIG_TABLES)) {
Ard Biesheuvelfc430262014-10-14 16:41:27 +0200621 /*
622 * According to the DMTF SMBIOS reference spec v3.0.0, it is
623 * allowed to define both the 64-bit entry point (smbios3) and
624 * the 32-bit entry point (smbios), in which case they should
625 * either both point to the same SMBIOS structure table, or the
626 * table pointed to by the 64-bit entry point should contain a
627 * superset of the table contents pointed to by the 32-bit entry
628 * point (section 5.2)
629 * This implies that the 64-bit entry point should have
630 * precedence if it is defined and supported by the OS. If we
631 * have the 64-bit entry point, but fail to decode it, fall
632 * back to the legacy one (if available)
633 */
634 if (efi.smbios3 != EFI_INVALID_TABLE_ADDR) {
635 p = dmi_early_remap(efi.smbios3, 32);
636 if (p == NULL)
637 goto error;
638 memcpy_fromio(buf, p, 32);
639 dmi_early_unmap(p, 32);
640
641 if (!dmi_smbios3_present(buf)) {
642 dmi_available = 1;
Jean Delvare71177942018-02-03 11:25:20 +0100643 return;
Ard Biesheuvelfc430262014-10-14 16:41:27 +0200644 }
645 }
Bjorn Helgaasb2c99e32006-03-26 01:37:08 -0800646 if (efi.smbios == EFI_INVALID_TABLE_ADDR)
Ingo Molnar9a22b6e2008-09-18 12:50:18 +0200647 goto error;
Andrey Panin61e032f2005-09-06 15:18:26 -0700648
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200649 /* This is called as a core_initcall() because it isn't
650 * needed during early boot. This also means we can
651 * iounmap the space when we're done with it.
652 */
Ard Biesheuvelcf074402014-01-23 15:54:39 -0800653 p = dmi_early_remap(efi.smbios, 32);
Matt Domsch3ed3bce2006-03-26 01:37:03 -0800654 if (p == NULL)
Ingo Molnar9a22b6e2008-09-18 12:50:18 +0200655 goto error;
Ben Hutchings79bae422013-04-30 15:27:46 -0700656 memcpy_fromio(buf, p, 32);
Ard Biesheuvelcf074402014-01-23 15:54:39 -0800657 dmi_early_unmap(p, 32);
Ben Hutchings79bae422013-04-30 15:27:46 -0700658
659 if (!dmi_present(buf)) {
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200660 dmi_available = 1;
Jean Delvare71177942018-02-03 11:25:20 +0100661 return;
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200662 }
Ard Biesheuvelcf074402014-01-23 15:54:39 -0800663 } else if (IS_ENABLED(CONFIG_DMI_SCAN_MACHINE_NON_EFI_FALLBACK)) {
664 p = dmi_early_remap(0xF0000, 0x10000);
Matt Domsch3ed3bce2006-03-26 01:37:03 -0800665 if (p == NULL)
Ingo Molnar9a22b6e2008-09-18 12:50:18 +0200666 goto error;
Andrey Panin61e032f2005-09-06 15:18:26 -0700667
Ben Hutchingsd39de282013-07-31 13:53:30 -0700668 /*
Jean Delvarec9aba142017-06-15 13:46:00 +0200669 * Same logic as above, look for a 64-bit entry point
670 * first, and if not found, fall back to 32-bit entry point.
671 */
672 memcpy_fromio(buf, p, 16);
673 for (q = p + 16; q < p + 0x10000; q += 16) {
674 memcpy_fromio(buf + 16, q, 16);
675 if (!dmi_smbios3_present(buf)) {
676 dmi_available = 1;
677 dmi_early_unmap(p, 0x10000);
Jean Delvare71177942018-02-03 11:25:20 +0100678 return;
Jean Delvarec9aba142017-06-15 13:46:00 +0200679 }
680 memcpy(buf, buf + 16, 16);
681 }
682
683 /*
Ben Hutchingsd39de282013-07-31 13:53:30 -0700684 * Iterate over all possible DMI header addresses q.
685 * Maintain the 32 bytes around q in buf. On the
686 * first iteration, substitute zero for the
687 * out-of-range bytes so there is no chance of falsely
688 * detecting an SMBIOS header.
689 */
Ben Hutchings79bae422013-04-30 15:27:46 -0700690 memset(buf, 0, 16);
Matt Domsch3ed3bce2006-03-26 01:37:03 -0800691 for (q = p; q < p + 0x10000; q += 16) {
Ben Hutchings79bae422013-04-30 15:27:46 -0700692 memcpy_fromio(buf + 16, q, 16);
Jean Delvarec9aba142017-06-15 13:46:00 +0200693 if (!dmi_present(buf)) {
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200694 dmi_available = 1;
Ard Biesheuvelcf074402014-01-23 15:54:39 -0800695 dmi_early_unmap(p, 0x10000);
Jean Delvare71177942018-02-03 11:25:20 +0100696 return;
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200697 }
Ben Hutchings79bae422013-04-30 15:27:46 -0700698 memcpy(buf, buf + 16, 16);
Andrey Panin61e032f2005-09-06 15:18:26 -0700699 }
Ard Biesheuvelcf074402014-01-23 15:54:39 -0800700 dmi_early_unmap(p, 0x10000);
Andrey Panin61e032f2005-09-06 15:18:26 -0700701 }
Ingo Molnar9a22b6e2008-09-18 12:50:18 +0200702 error:
Jean Delvare02d9c472013-09-11 14:24:08 -0700703 pr_info("DMI not present or invalid.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704}
705
Ivan Khoronzhukd7f96f972015-06-25 09:06:56 +0200706static ssize_t raw_table_read(struct file *file, struct kobject *kobj,
707 struct bin_attribute *attr, char *buf,
708 loff_t pos, size_t count)
709{
710 memcpy(buf, attr->private + pos, count);
711 return count;
712}
713
714static BIN_ATTR(smbios_entry_point, S_IRUSR, raw_table_read, NULL, 0);
715static BIN_ATTR(DMI, S_IRUSR, raw_table_read, NULL, 0);
716
717static int __init dmi_init(void)
718{
719 struct kobject *tables_kobj;
720 u8 *dmi_table;
721 int ret = -ENOMEM;
722
Ard Biesheuvela81114d2018-02-03 11:25:20 +0100723 if (!dmi_available)
724 return 0;
Ivan Khoronzhukd7f96f972015-06-25 09:06:56 +0200725
726 /*
727 * Set up dmi directory at /sys/firmware/dmi. This entry should stay
728 * even after farther error, as it can be used by other modules like
729 * dmi-sysfs.
730 */
731 dmi_kobj = kobject_create_and_add("dmi", firmware_kobj);
732 if (!dmi_kobj)
733 goto err;
734
735 tables_kobj = kobject_create_and_add("tables", dmi_kobj);
736 if (!tables_kobj)
737 goto err;
738
739 dmi_table = dmi_remap(dmi_base, dmi_len);
740 if (!dmi_table)
741 goto err_tables;
742
743 bin_attr_smbios_entry_point.size = smbios_entry_point_size;
744 bin_attr_smbios_entry_point.private = smbios_entry_point;
745 ret = sysfs_create_bin_file(tables_kobj, &bin_attr_smbios_entry_point);
746 if (ret)
747 goto err_unmap;
748
749 bin_attr_DMI.size = dmi_len;
750 bin_attr_DMI.private = dmi_table;
751 ret = sysfs_create_bin_file(tables_kobj, &bin_attr_DMI);
752 if (!ret)
753 return 0;
754
755 sysfs_remove_bin_file(tables_kobj,
756 &bin_attr_smbios_entry_point);
757 err_unmap:
758 dmi_unmap(dmi_table);
759 err_tables:
760 kobject_del(tables_kobj);
761 kobject_put(tables_kobj);
762 err:
763 pr_err("dmi: Firmware registration failed.\n");
764
765 return ret;
766}
767subsys_initcall(dmi_init);
768
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769/**
Robert Richter0fca0812019-03-28 20:34:28 +0100770 * dmi_setup - scan and setup DMI system information
Tejun Heo98e5e1b2013-04-30 15:27:15 -0700771 *
Robert Richter0fca0812019-03-28 20:34:28 +0100772 * Scan the DMI system information. This setups DMI identifiers
773 * (dmi_system_id) for printing it out on task dumps and prepares
774 * DIMM entry information (dmi_memdev_info) from the SMBIOS table
775 * for using this when reporting memory errors.
Tejun Heo98e5e1b2013-04-30 15:27:15 -0700776 */
Robert Richter0fca0812019-03-28 20:34:28 +0100777void __init dmi_setup(void)
Tejun Heo98e5e1b2013-04-30 15:27:15 -0700778{
Robert Richter0fca0812019-03-28 20:34:28 +0100779 dmi_scan_machine();
780 if (!dmi_available)
781 return;
782
783 dmi_memdev_walk();
Tejun Heo98e5e1b2013-04-30 15:27:15 -0700784 dump_stack_set_arch_desc("%s", dmi_ids_string);
785}
786
787/**
Rafael J. Wysockid7b19562009-01-19 20:55:50 +0100788 * dmi_matches - check if dmi_system_id structure matches system DMI data
789 * @dmi: pointer to the dmi_system_id structure to check
790 */
791static bool dmi_matches(const struct dmi_system_id *dmi)
792{
793 int i;
794
Rafael J. Wysockid7b19562009-01-19 20:55:50 +0100795 for (i = 0; i < ARRAY_SIZE(dmi->matches); i++) {
796 int s = dmi->matches[i].slot;
797 if (s == DMI_NONE)
Dmitry Torokhov75757502009-12-04 10:24:19 -0800798 break;
Alex Hungde40614d2018-04-13 15:37:59 +0200799 if (s == DMI_OEM_STRING) {
800 /* DMI_OEM_STRING must be exact match */
801 const struct dmi_device *valid;
802
803 valid = dmi_find_device(DMI_DEV_TYPE_OEM_STRING,
804 dmi->matches[i].substr, NULL);
805 if (valid)
806 continue;
807 } else if (dmi_ident[s]) {
Jean Delvare8cf4e6a2018-02-03 11:25:20 +0100808 if (dmi->matches[i].exact_match) {
809 if (!strcmp(dmi_ident[s],
810 dmi->matches[i].substr))
811 continue;
812 } else {
813 if (strstr(dmi_ident[s],
814 dmi->matches[i].substr))
815 continue;
816 }
Jani Nikula5017b282013-07-03 15:05:02 -0700817 }
818
Rafael J. Wysockid7b19562009-01-19 20:55:50 +0100819 /* No match */
820 return false;
821 }
822 return true;
823}
824
825/**
Dmitry Torokhov75757502009-12-04 10:24:19 -0800826 * dmi_is_end_of_table - check for end-of-table marker
827 * @dmi: pointer to the dmi_system_id structure to check
828 */
829static bool dmi_is_end_of_table(const struct dmi_system_id *dmi)
830{
831 return dmi->matches[0].slot == DMI_NONE;
832}
833
834/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 * dmi_check_system - check system DMI data
836 * @list: array of dmi_system_id structures to match against
Randy Dunlapb0ef3712006-06-25 05:49:18 -0700837 * All non-null elements of the list must match
838 * their slot's (field index's) data (i.e., each
839 * list string must be a substring of the specified
840 * DMI slot's string data) to be considered a
841 * successful match.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 *
843 * Walk the blacklist table running matching functions until someone
844 * returns non zero or we hit the end. Callback function is called for
Randy Dunlapb0ef3712006-06-25 05:49:18 -0700845 * each successful match. Returns the number of matches.
Jean Delvare71177942018-02-03 11:25:20 +0100846 *
Robert Richter0fca0812019-03-28 20:34:28 +0100847 * dmi_setup must be called before this function is called.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 */
Jeff Garzik18552562007-10-03 15:15:40 -0400849int dmi_check_system(const struct dmi_system_id *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850{
Rafael J. Wysockid7b19562009-01-19 20:55:50 +0100851 int count = 0;
852 const struct dmi_system_id *d;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
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 count++;
857 if (d->callback && d->callback(d))
858 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
861 return count;
862}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863EXPORT_SYMBOL(dmi_check_system);
864
865/**
Rafael J. Wysockid7b19562009-01-19 20:55:50 +0100866 * dmi_first_match - find dmi_system_id structure matching system DMI data
867 * @list: array of dmi_system_id structures to match against
868 * All non-null elements of the list must match
869 * their slot's (field index's) data (i.e., each
870 * list string must be a substring of the specified
871 * DMI slot's string data) to be considered a
872 * successful match.
873 *
874 * Walk the blacklist table until the first match is found. Return the
875 * pointer to the matching entry or NULL if there's no match.
Jean Delvare71177942018-02-03 11:25:20 +0100876 *
Robert Richter0fca0812019-03-28 20:34:28 +0100877 * dmi_setup must be called before this function is called.
Rafael J. Wysockid7b19562009-01-19 20:55:50 +0100878 */
879const struct dmi_system_id *dmi_first_match(const struct dmi_system_id *list)
880{
881 const struct dmi_system_id *d;
882
Dmitry Torokhov75757502009-12-04 10:24:19 -0800883 for (d = list; !dmi_is_end_of_table(d); d++)
Rafael J. Wysockid7b19562009-01-19 20:55:50 +0100884 if (dmi_matches(d))
885 return d;
886
887 return NULL;
888}
889EXPORT_SYMBOL(dmi_first_match);
890
891/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 * dmi_get_system_info - return DMI data value
Randy Dunlapb0ef3712006-06-25 05:49:18 -0700893 * @field: data index (see enum dmi_field)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 *
895 * Returns one DMI data value, can be used to perform
896 * complex DMI data checks.
897 */
Jeff Garzik18552562007-10-03 15:15:40 -0400898const char *dmi_get_system_info(int field)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899{
900 return dmi_ident[field];
901}
Dmitry Torokhove70c9d52005-06-25 14:54:25 -0700902EXPORT_SYMBOL(dmi_get_system_info);
Andrey Paninebad6a42005-09-06 15:18:29 -0700903
Alok Katariafd8cd7e2008-11-03 15:50:38 -0800904/**
Randy Dunlapc2bacfc2009-01-06 14:41:40 -0800905 * dmi_name_in_serial - Check if string is in the DMI product serial information
906 * @str: string to check for
Alok Katariafd8cd7e2008-11-03 15:50:38 -0800907 */
908int dmi_name_in_serial(const char *str)
909{
910 int f = DMI_PRODUCT_SERIAL;
911 if (dmi_ident[f] && strstr(dmi_ident[f], str))
912 return 1;
913 return 0;
914}
Andi Kleena1bae672006-10-21 18:37:02 +0200915
916/**
Jean Delvare66e13e62011-11-15 14:36:09 -0800917 * dmi_name_in_vendors - Check if string is in the DMI system or board vendor name
Jean Delvare02d9c472013-09-11 14:24:08 -0700918 * @str: Case sensitive Name
Andi Kleena1bae672006-10-21 18:37:02 +0200919 */
Jeff Garzik18552562007-10-03 15:15:40 -0400920int dmi_name_in_vendors(const char *str)
Andi Kleena1bae672006-10-21 18:37:02 +0200921{
Jean Delvare66e13e62011-11-15 14:36:09 -0800922 static int fields[] = { DMI_SYS_VENDOR, DMI_BOARD_VENDOR, DMI_NONE };
Andi Kleena1bae672006-10-21 18:37:02 +0200923 int i;
924 for (i = 0; fields[i] != DMI_NONE; i++) {
925 int f = fields[i];
926 if (dmi_ident[f] && strstr(dmi_ident[f], str))
927 return 1;
928 }
929 return 0;
930}
931EXPORT_SYMBOL(dmi_name_in_vendors);
932
Andrey Paninebad6a42005-09-06 15:18:29 -0700933/**
934 * dmi_find_device - find onboard device by type/name
935 * @type: device type or %DMI_DEV_TYPE_ANY to match all device types
Randy Dunlapb0ef3712006-06-25 05:49:18 -0700936 * @name: device name string or %NULL to match all
Andrey Paninebad6a42005-09-06 15:18:29 -0700937 * @from: previous device found in search, or %NULL for new search.
938 *
939 * Iterates through the list of known onboard devices. If a device is
Jean Delvarebfab8b482016-01-15 22:08:44 +0100940 * found with a matching @type and @name, a pointer to its device
Andrey Paninebad6a42005-09-06 15:18:29 -0700941 * structure is returned. Otherwise, %NULL is returned.
Randy Dunlapb0ef3712006-06-25 05:49:18 -0700942 * A new search is initiated by passing %NULL as the @from argument.
Andrey Paninebad6a42005-09-06 15:18:29 -0700943 * If @from is not %NULL, searches continue from next device.
944 */
Jean Delvare02d9c472013-09-11 14:24:08 -0700945const struct dmi_device *dmi_find_device(int type, const char *name,
Jeff Garzik18552562007-10-03 15:15:40 -0400946 const struct dmi_device *from)
Andrey Paninebad6a42005-09-06 15:18:29 -0700947{
Jeff Garzik18552562007-10-03 15:15:40 -0400948 const struct list_head *head = from ? &from->list : &dmi_devices;
949 struct list_head *d;
Andrey Paninebad6a42005-09-06 15:18:29 -0700950
Jean Delvare02d9c472013-09-11 14:24:08 -0700951 for (d = head->next; d != &dmi_devices; d = d->next) {
Jeff Garzik18552562007-10-03 15:15:40 -0400952 const struct dmi_device *dev =
953 list_entry(d, struct dmi_device, list);
Andrey Paninebad6a42005-09-06 15:18:29 -0700954
955 if (((type == DMI_DEV_TYPE_ANY) || (dev->type == type)) &&
956 ((name == NULL) || (strcmp(dev->name, name) == 0)))
957 return dev;
958 }
959
960 return NULL;
961}
962EXPORT_SYMBOL(dmi_find_device);
Andi Kleenf083a322006-03-25 16:30:19 +0100963
964/**
Tejun Heo3e5cd1f2009-08-16 21:02:36 +0900965 * dmi_get_date - parse a DMI date
966 * @field: data index (see enum dmi_field)
967 * @yearp: optional out parameter for the year
968 * @monthp: optional out parameter for the month
969 * @dayp: optional out parameter for the day
Andi Kleenf083a322006-03-25 16:30:19 +0100970 *
Tejun Heo3e5cd1f2009-08-16 21:02:36 +0900971 * The date field is assumed to be in the form resembling
972 * [mm[/dd]]/yy[yy] and the result is stored in the out
973 * parameters any or all of which can be omitted.
974 *
975 * If the field doesn't exist, all out parameters are set to zero
976 * and false is returned. Otherwise, true is returned with any
977 * invalid part of date set to zero.
978 *
979 * On return, year, month and day are guaranteed to be in the
980 * range of [0,9999], [0,12] and [0,31] respectively.
Andi Kleenf083a322006-03-25 16:30:19 +0100981 */
Tejun Heo3e5cd1f2009-08-16 21:02:36 +0900982bool dmi_get_date(int field, int *yearp, int *monthp, int *dayp)
Andi Kleenf083a322006-03-25 16:30:19 +0100983{
Tejun Heo3e5cd1f2009-08-16 21:02:36 +0900984 int year = 0, month = 0, day = 0;
985 bool exists;
986 const char *s, *y;
Tejun Heo02c24fa2009-08-16 21:01:22 +0900987 char *e;
Andi Kleenf083a322006-03-25 16:30:19 +0100988
Tejun Heo3e5cd1f2009-08-16 21:02:36 +0900989 s = dmi_get_system_info(field);
990 exists = s;
991 if (!exists)
992 goto out;
Andi Kleenf083a322006-03-25 16:30:19 +0100993
Tejun Heo3e5cd1f2009-08-16 21:02:36 +0900994 /*
995 * Determine year first. We assume the date string resembles
996 * mm/dd/yy[yy] but the original code extracted only the year
997 * from the end. Keep the behavior in the spirit of no
998 * surprises.
999 */
1000 y = strrchr(s, '/');
1001 if (!y)
1002 goto out;
1003
1004 y++;
1005 year = simple_strtoul(y, &e, 10);
1006 if (y != e && year < 100) { /* 2-digit year */
Andi Kleenf083a322006-03-25 16:30:19 +01001007 year += 1900;
1008 if (year < 1996) /* no dates < spec 1.0 */
1009 year += 100;
1010 }
Tejun Heo3e5cd1f2009-08-16 21:02:36 +09001011 if (year > 9999) /* year should fit in %04d */
1012 year = 0;
Andi Kleenf083a322006-03-25 16:30:19 +01001013
Tejun Heo3e5cd1f2009-08-16 21:02:36 +09001014 /* parse the mm and dd */
1015 month = simple_strtoul(s, &e, 10);
1016 if (s == e || *e != '/' || !month || month > 12) {
1017 month = 0;
1018 goto out;
1019 }
1020
1021 s = e + 1;
1022 day = simple_strtoul(s, &e, 10);
1023 if (s == y || s == e || *e != '/' || day > 31)
1024 day = 0;
1025out:
1026 if (yearp)
1027 *yearp = year;
1028 if (monthp)
1029 *monthp = month;
1030 if (dayp)
1031 *dayp = day;
1032 return exists;
Andi Kleenf083a322006-03-25 16:30:19 +01001033}
Tejun Heo3e5cd1f2009-08-16 21:02:36 +09001034EXPORT_SYMBOL(dmi_get_date);
Jean Delvare7fce0842007-11-03 17:29:20 +01001035
1036/**
Andy Shevchenko3af34522018-03-20 20:07:59 +02001037 * dmi_get_bios_year - get a year out of DMI_BIOS_DATE field
1038 *
1039 * Returns year on success, -ENXIO if DMI is not selected,
1040 * or a different negative error code if DMI field is not present
1041 * or not parseable.
1042 */
1043int dmi_get_bios_year(void)
1044{
1045 bool exists;
1046 int year;
1047
1048 exists = dmi_get_date(DMI_BIOS_DATE, &year, NULL, NULL);
1049 if (!exists)
1050 return -ENODATA;
1051
1052 return year ? year : -ERANGE;
1053}
1054EXPORT_SYMBOL(dmi_get_bios_year);
1055
1056/**
Jean Delvare7fce0842007-11-03 17:29:20 +01001057 * dmi_walk - Walk the DMI table and get called back for every record
1058 * @decode: Callback function
Jean Delvaree7a19c562009-03-30 21:46:44 +02001059 * @private_data: Private data to be passed to the callback function
Jean Delvare7fce0842007-11-03 17:29:20 +01001060 *
Andy Lutomirskic9268202017-06-15 13:46:00 +02001061 * Returns 0 on success, -ENXIO if DMI is not selected or not present,
1062 * or a different negative error code if DMI walking fails.
Jean Delvare7fce0842007-11-03 17:29:20 +01001063 */
Jean Delvaree7a19c562009-03-30 21:46:44 +02001064int dmi_walk(void (*decode)(const struct dmi_header *, void *),
1065 void *private_data)
Jean Delvare7fce0842007-11-03 17:29:20 +01001066{
1067 u8 *buf;
1068
1069 if (!dmi_available)
Andy Lutomirskic9268202017-06-15 13:46:00 +02001070 return -ENXIO;
Jean Delvare7fce0842007-11-03 17:29:20 +01001071
Ard Biesheuvelcf074402014-01-23 15:54:39 -08001072 buf = dmi_remap(dmi_base, dmi_len);
Jean Delvare7fce0842007-11-03 17:29:20 +01001073 if (buf == NULL)
Andy Lutomirskic9268202017-06-15 13:46:00 +02001074 return -ENOMEM;
Jean Delvare7fce0842007-11-03 17:29:20 +01001075
Ivan Khoronzhukeb4c5ea2015-06-25 09:06:56 +02001076 dmi_decode_table(buf, decode, private_data);
Jean Delvare7fce0842007-11-03 17:29:20 +01001077
Ard Biesheuvelcf074402014-01-23 15:54:39 -08001078 dmi_unmap(buf);
Jean Delvare7fce0842007-11-03 17:29:20 +01001079 return 0;
1080}
1081EXPORT_SYMBOL_GPL(dmi_walk);
Jiri Slabyd61c72e2008-12-10 14:07:21 +01001082
1083/**
1084 * dmi_match - compare a string to the dmi field (if exists)
Randy Dunlapc2bacfc2009-01-06 14:41:40 -08001085 * @f: DMI field identifier
1086 * @str: string to compare the DMI field to
Jiri Slabyd61c72e2008-12-10 14:07:21 +01001087 *
1088 * Returns true if the requested field equals to the str (including NULL).
1089 */
1090bool dmi_match(enum dmi_field f, const char *str)
1091{
1092 const char *info = dmi_get_system_info(f);
1093
1094 if (info == NULL || str == NULL)
1095 return info == str;
1096
1097 return !strcmp(info, str);
1098}
1099EXPORT_SYMBOL_GPL(dmi_match);
Chen, Gongdd6dad42013-10-18 14:29:25 -07001100
1101void dmi_memdev_name(u16 handle, const char **bank, const char **device)
1102{
1103 int n;
1104
1105 if (dmi_memdev == NULL)
1106 return;
1107
1108 for (n = 0; n < dmi_memdev_nr; n++) {
1109 if (handle == dmi_memdev[n].handle) {
1110 *bank = dmi_memdev[n].bank;
1111 *device = dmi_memdev[n].device;
1112 break;
1113 }
1114 }
1115}
1116EXPORT_SYMBOL_GPL(dmi_memdev_name);
Tony Luck6deae962018-03-12 11:24:29 -07001117
1118u64 dmi_memdev_size(u16 handle)
1119{
1120 int n;
1121
1122 if (dmi_memdev) {
1123 for (n = 0; n < dmi_memdev_nr; n++) {
1124 if (handle == dmi_memdev[n].handle)
1125 return dmi_memdev[n].size;
1126 }
1127 }
1128 return ~0ull;
1129}
1130EXPORT_SYMBOL_GPL(dmi_memdev_size);