blob: 9a094bb44e3d2d2eaaf74f011088f8fe6e8e7942 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011
Paul Jacksoncb5dd7c2008-05-14 08:15:16 -070012/*
13 * DMI stands for "Desktop Management Interface". It is part
14 * of and an antecedent to, SMBIOS, which stands for System
15 * Management BIOS. See further: http://www.dmtf.org/standards
16 */
Jean Delvareffbbb962013-09-11 14:24:09 -070017static const char dmi_empty_string[] = " ";
Parag Warudkar79da4722008-01-30 13:31:59 +010018
Zhenzhong Duanf1d8e612012-12-20 15:05:13 -080019static u16 __initdata dmi_ver;
Ingo Molnar9a22b6e2008-09-18 12:50:18 +020020/*
21 * Catch too early calls to dmi_check_system():
22 */
23static int dmi_initialized;
24
Tejun Heoc90fe6b2013-04-30 15:27:14 -070025/* DMI system identification string used during boot */
26static char dmi_ids_string[128] __initdata;
27
Jean Delvaref3069ae2008-02-23 15:23:46 -080028static const char * __init dmi_string_nosave(const struct dmi_header *dm, u8 s)
Linus Torvalds1da177e2005-04-16 15:20:36 -070029{
Jeff Garzik18552562007-10-03 15:15:40 -040030 const u8 *bp = ((u8 *) dm) + dm->length;
Andrey Panin1249c512005-06-25 14:54:47 -070031
Andrey Paninc3c71202005-09-06 15:18:28 -070032 if (s) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 s--;
Andrey Paninc3c71202005-09-06 15:18:28 -070034 while (s > 0 && *bp) {
35 bp += strlen(bp) + 1;
36 s--;
37 }
38
39 if (*bp != 0) {
Parag Warudkar79da4722008-01-30 13:31:59 +010040 size_t len = strlen(bp)+1;
41 size_t cmp_len = len > 8 ? 8 : len;
42
43 if (!memcmp(bp, dmi_empty_string, cmp_len))
44 return dmi_empty_string;
Jean Delvaref3069ae2008-02-23 15:23:46 -080045 return bp;
Andrey Paninc3c71202005-09-06 15:18:28 -070046 }
Bjorn Helgaas4f705ae2006-04-03 17:09:22 -070047 }
Andrey Paninc3c71202005-09-06 15:18:28 -070048
Jean Delvaref3069ae2008-02-23 15:23:46 -080049 return "";
50}
51
Jean Delvareffbbb962013-09-11 14:24:09 -070052static const char * __init dmi_string(const struct dmi_header *dm, u8 s)
Jean Delvaref3069ae2008-02-23 15:23:46 -080053{
54 const char *bp = dmi_string_nosave(dm, s);
55 char *str;
56 size_t len;
57
58 if (bp == dmi_empty_string)
59 return dmi_empty_string;
60
61 len = strlen(bp) + 1;
62 str = dmi_alloc(len);
63 if (str != NULL)
64 strcpy(str, bp);
65 else
Jean Delvare02d9c472013-09-11 14:24:08 -070066 pr_err("dmi_string: cannot allocate %Zu bytes.\n", len);
Jean Delvaref3069ae2008-02-23 15:23:46 -080067
Andrey Paninc3c71202005-09-06 15:18:28 -070068 return str;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069}
70
71/*
72 * We have to be cautious here. We have seen BIOSes with DMI pointers
73 * pointing to completely the wrong place for example
74 */
Jean Delvare7fce0842007-11-03 17:29:20 +010075static void dmi_table(u8 *buf, int len, int num,
Jean Delvaree7a19c562009-03-30 21:46:44 +020076 void (*decode)(const struct dmi_header *, void *),
77 void *private_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070078{
Jean Delvare7fce0842007-11-03 17:29:20 +010079 u8 *data = buf;
Andrey Panin1249c512005-06-25 14:54:47 -070080 int i = 0;
Bjorn Helgaas4f705ae2006-04-03 17:09:22 -070081
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 /*
Bjorn Helgaas4f705ae2006-04-03 17:09:22 -070083 * Stop when we see all the items the table claimed to have
84 * OR we run off the end of the table (also happens)
85 */
Andrey Panin1249c512005-06-25 14:54:47 -070086 while ((i < num) && (data - buf + sizeof(struct dmi_header)) <= len) {
Jeff Garzik18552562007-10-03 15:15:40 -040087 const struct dmi_header *dm = (const struct dmi_header *)data;
88
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 /*
Alan Cox86385452008-11-07 16:03:46 +000090 * We want to know the total length (formatted area and
91 * strings) before decoding to make sure we won't run off the
92 * table in dmi_decode or dmi_string
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 */
Andrey Panin1249c512005-06-25 14:54:47 -070094 data += dm->length;
95 while ((data - buf < len - 1) && (data[0] || data[1]))
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 data++;
Andrey Panin1249c512005-06-25 14:54:47 -070097 if (data - buf < len - 1)
Jean Delvaree7a19c562009-03-30 21:46:44 +020098 decode(dm, private_data);
Andrey Panin1249c512005-06-25 14:54:47 -070099 data += 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 i++;
101 }
Jean Delvare7fce0842007-11-03 17:29:20 +0100102}
103
104static u32 dmi_base;
105static u16 dmi_len;
106static u16 dmi_num;
107
Jean Delvaree7a19c562009-03-30 21:46:44 +0200108static int __init dmi_walk_early(void (*decode)(const struct dmi_header *,
109 void *))
Jean Delvare7fce0842007-11-03 17:29:20 +0100110{
111 u8 *buf;
112
113 buf = dmi_ioremap(dmi_base, dmi_len);
114 if (buf == NULL)
115 return -1;
116
Jean Delvaree7a19c562009-03-30 21:46:44 +0200117 dmi_table(buf, dmi_len, dmi_num, decode, NULL);
Jean Delvare7fce0842007-11-03 17:29:20 +0100118
Tony Luckd114a332012-07-20 13:15:20 -0700119 add_device_randomness(buf, dmi_len);
120
Jean Delvare7fce0842007-11-03 17:29:20 +0100121 dmi_iounmap(buf, dmi_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 return 0;
123}
124
Zhenzhong Duan9f9c9cbb2012-12-20 15:05:14 -0800125static int __init dmi_checksum(const u8 *buf, u8 len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126{
Andrey Panin1249c512005-06-25 14:54:47 -0700127 u8 sum = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 int a;
Bjorn Helgaas4f705ae2006-04-03 17:09:22 -0700129
Zhenzhong Duan9f9c9cbb2012-12-20 15:05:14 -0800130 for (a = 0; a < len; a++)
Andrey Panin1249c512005-06-25 14:54:47 -0700131 sum += buf[a];
132
133 return sum == 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134}
135
Jean Delvareffbbb962013-09-11 14:24:09 -0700136static const char *dmi_ident[DMI_STRING_MAX];
Andrey Paninebad6a42005-09-06 15:18:29 -0700137static LIST_HEAD(dmi_devices);
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200138int dmi_available;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
140/*
141 * Save a DMI string
142 */
Jean Delvare02d9c472013-09-11 14:24:08 -0700143static void __init dmi_save_ident(const struct dmi_header *dm, int slot,
144 int string)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145{
Jean Delvare02d9c472013-09-11 14:24:08 -0700146 const char *d = (const char *) dm;
Jean Delvareffbbb962013-09-11 14:24:09 -0700147 const char *p;
Andrey Panin1249c512005-06-25 14:54:47 -0700148
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 if (dmi_ident[slot])
150 return;
Andrey Panin1249c512005-06-25 14:54:47 -0700151
Andrey Paninc3c71202005-09-06 15:18:28 -0700152 p = dmi_string(dm, d[string]);
153 if (p == NULL)
154 return;
155
156 dmi_ident[slot] = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157}
158
Jean Delvare02d9c472013-09-11 14:24:08 -0700159static void __init dmi_save_uuid(const struct dmi_header *dm, int slot,
160 int index)
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200161{
Jean Delvare02d9c472013-09-11 14:24:08 -0700162 const u8 *d = (u8 *) dm + index;
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200163 char *s;
164 int is_ff = 1, is_00 = 1, i;
165
166 if (dmi_ident[slot])
167 return;
168
169 for (i = 0; i < 16 && (is_ff || is_00); i++) {
Zhenzhong Duanf1d8e612012-12-20 15:05:13 -0800170 if (d[i] != 0x00)
171 is_00 = 0;
172 if (d[i] != 0xFF)
173 is_ff = 0;
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200174 }
175
176 if (is_ff || is_00)
177 return;
178
179 s = dmi_alloc(16*2+4+1);
180 if (!s)
181 return;
182
Zhenzhong Duanf1d8e612012-12-20 15:05:13 -0800183 /*
184 * As of version 2.6 of the SMBIOS specification, the first 3 fields of
185 * the UUID are supposed to be little-endian encoded. The specification
186 * says that this is the defacto standard.
187 */
188 if (dmi_ver >= 0x0206)
189 sprintf(s, "%pUL", d);
190 else
191 sprintf(s, "%pUB", d);
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200192
Jean Delvare02d9c472013-09-11 14:24:08 -0700193 dmi_ident[slot] = s;
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200194}
195
Jean Delvare02d9c472013-09-11 14:24:08 -0700196static void __init dmi_save_type(const struct dmi_header *dm, int slot,
197 int index)
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200198{
Jean Delvare02d9c472013-09-11 14:24:08 -0700199 const u8 *d = (u8 *) dm + index;
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200200 char *s;
201
202 if (dmi_ident[slot])
203 return;
204
205 s = dmi_alloc(4);
206 if (!s)
207 return;
208
209 sprintf(s, "%u", *d & 0x7F);
210 dmi_ident[slot] = s;
211}
212
Jean Delvaref3069ae2008-02-23 15:23:46 -0800213static void __init dmi_save_one_device(int type, const char *name)
214{
215 struct dmi_device *dev;
216
217 /* No duplicate device */
218 if (dmi_find_device(type, name, NULL))
219 return;
220
221 dev = dmi_alloc(sizeof(*dev) + strlen(name) + 1);
222 if (!dev) {
Jean Delvare02d9c472013-09-11 14:24:08 -0700223 pr_err("dmi_save_one_device: out of memory.\n");
Jean Delvaref3069ae2008-02-23 15:23:46 -0800224 return;
225 }
226
227 dev->type = type;
228 strcpy((char *)(dev + 1), name);
229 dev->name = (char *)(dev + 1);
230 dev->device_data = NULL;
231 list_add(&dev->list, &dmi_devices);
232}
233
Jeff Garzik18552562007-10-03 15:15:40 -0400234static void __init dmi_save_devices(const struct dmi_header *dm)
Andrey Paninebad6a42005-09-06 15:18:29 -0700235{
236 int i, count = (dm->length - sizeof(struct dmi_header)) / 2;
Andrey Paninebad6a42005-09-06 15:18:29 -0700237
238 for (i = 0; i < count; i++) {
Jeff Garzik18552562007-10-03 15:15:40 -0400239 const char *d = (char *)(dm + 1) + (i * 2);
Andrey Paninebad6a42005-09-06 15:18:29 -0700240
241 /* Skip disabled device */
242 if ((*d & 0x80) == 0)
243 continue;
244
Jean Delvaref3069ae2008-02-23 15:23:46 -0800245 dmi_save_one_device(*d & 0x7f, dmi_string_nosave(dm, *(d + 1)));
Shem Multinymous2e0c1f62006-09-29 01:59:37 -0700246 }
247}
248
Jeff Garzik18552562007-10-03 15:15:40 -0400249static void __init dmi_save_oem_strings_devices(const struct dmi_header *dm)
Shem Multinymous2e0c1f62006-09-29 01:59:37 -0700250{
251 int i, count = *(u8 *)(dm + 1);
252 struct dmi_device *dev;
253
254 for (i = 1; i <= count; i++) {
Jean Delvareffbbb962013-09-11 14:24:09 -0700255 const char *devname = dmi_string(dm, i);
Parag Warudkar79da4722008-01-30 13:31:59 +0100256
Jean Delvare43fe1052008-02-23 15:23:55 -0800257 if (devname == dmi_empty_string)
Parag Warudkar79da4722008-01-30 13:31:59 +0100258 continue;
Parag Warudkar79da4722008-01-30 13:31:59 +0100259
Shem Multinymous2e0c1f62006-09-29 01:59:37 -0700260 dev = dmi_alloc(sizeof(*dev));
261 if (!dev) {
Jean Delvare02d9c472013-09-11 14:24:08 -0700262 pr_err("dmi_save_oem_strings_devices: out of memory.\n");
Shem Multinymous2e0c1f62006-09-29 01:59:37 -0700263 break;
264 }
265
266 dev->type = DMI_DEV_TYPE_OEM_STRING;
Parag Warudkar79da4722008-01-30 13:31:59 +0100267 dev->name = devname;
Shem Multinymous2e0c1f62006-09-29 01:59:37 -0700268 dev->device_data = NULL;
Andrey Paninebad6a42005-09-06 15:18:29 -0700269
270 list_add(&dev->list, &dmi_devices);
271 }
272}
273
Jeff Garzik18552562007-10-03 15:15:40 -0400274static void __init dmi_save_ipmi_device(const struct dmi_header *dm)
Andrey Paninebad6a42005-09-06 15:18:29 -0700275{
276 struct dmi_device *dev;
Jean Delvare02d9c472013-09-11 14:24:08 -0700277 void *data;
Andrey Paninebad6a42005-09-06 15:18:29 -0700278
Andi Kleene9928672006-01-11 22:43:33 +0100279 data = dmi_alloc(dm->length);
Andrey Paninebad6a42005-09-06 15:18:29 -0700280 if (data == NULL) {
Jean Delvare02d9c472013-09-11 14:24:08 -0700281 pr_err("dmi_save_ipmi_device: out of memory.\n");
Andrey Paninebad6a42005-09-06 15:18:29 -0700282 return;
283 }
284
285 memcpy(data, dm, dm->length);
286
Andi Kleene9928672006-01-11 22:43:33 +0100287 dev = dmi_alloc(sizeof(*dev));
Andrey Paninebad6a42005-09-06 15:18:29 -0700288 if (!dev) {
Jean Delvare02d9c472013-09-11 14:24:08 -0700289 pr_err("dmi_save_ipmi_device: out of memory.\n");
Andrey Paninebad6a42005-09-06 15:18:29 -0700290 return;
291 }
292
293 dev->type = DMI_DEV_TYPE_IPMI;
294 dev->name = "IPMI controller";
295 dev->device_data = data;
296
Carol Hebertabd24df2008-04-04 14:30:03 -0700297 list_add_tail(&dev->list, &dmi_devices);
Andrey Paninebad6a42005-09-06 15:18:29 -0700298}
299
Narendra K911e1c92010-07-26 05:56:50 -0500300static void __init dmi_save_dev_onboard(int instance, int segment, int bus,
301 int devfn, const char *name)
302{
303 struct dmi_dev_onboard *onboard_dev;
304
305 onboard_dev = dmi_alloc(sizeof(*onboard_dev) + strlen(name) + 1);
306 if (!onboard_dev) {
Jean Delvare02d9c472013-09-11 14:24:08 -0700307 pr_err("dmi_save_dev_onboard: out of memory.\n");
Narendra K911e1c92010-07-26 05:56:50 -0500308 return;
309 }
310 onboard_dev->instance = instance;
311 onboard_dev->segment = segment;
312 onboard_dev->bus = bus;
313 onboard_dev->devfn = devfn;
314
315 strcpy((char *)&onboard_dev[1], name);
316 onboard_dev->dev.type = DMI_DEV_TYPE_DEV_ONBOARD;
317 onboard_dev->dev.name = (char *)&onboard_dev[1];
318 onboard_dev->dev.device_data = onboard_dev;
319
320 list_add(&onboard_dev->dev.list, &dmi_devices);
321}
322
Wim Van Sebroeckb4bd7d52008-02-08 04:20:58 -0800323static void __init dmi_save_extended_devices(const struct dmi_header *dm)
324{
Jean Delvare02d9c472013-09-11 14:24:08 -0700325 const u8 *d = (u8 *) dm + 5;
Wim Van Sebroeckb4bd7d52008-02-08 04:20:58 -0800326
327 /* Skip disabled device */
328 if ((*d & 0x80) == 0)
329 return;
330
Narendra K911e1c92010-07-26 05:56:50 -0500331 dmi_save_dev_onboard(*(d+1), *(u16 *)(d+2), *(d+4), *(d+5),
332 dmi_string_nosave(dm, *(d-1)));
Jean Delvaref3069ae2008-02-23 15:23:46 -0800333 dmi_save_one_device(*d & 0x7f, dmi_string_nosave(dm, *(d - 1)));
Wim Van Sebroeckb4bd7d52008-02-08 04:20:58 -0800334}
335
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 * Process a DMI table entry. Right now all we care about are the BIOS
338 * and machine entries. For 2.5 we should pull the smbus controller info
339 * out of here.
340 */
Jean Delvaree7a19c562009-03-30 21:46:44 +0200341static void __init dmi_decode(const struct dmi_header *dm, void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342{
Jean Delvare02d9c472013-09-11 14:24:08 -0700343 switch (dm->type) {
Andrey Paninebad6a42005-09-06 15:18:29 -0700344 case 0: /* BIOS Information */
Andrey Panin1249c512005-06-25 14:54:47 -0700345 dmi_save_ident(dm, DMI_BIOS_VENDOR, 4);
Andrey Panin1249c512005-06-25 14:54:47 -0700346 dmi_save_ident(dm, DMI_BIOS_VERSION, 5);
Andrey Panin1249c512005-06-25 14:54:47 -0700347 dmi_save_ident(dm, DMI_BIOS_DATE, 8);
348 break;
Andrey Paninebad6a42005-09-06 15:18:29 -0700349 case 1: /* System Information */
Andrey Panin1249c512005-06-25 14:54:47 -0700350 dmi_save_ident(dm, DMI_SYS_VENDOR, 4);
Andrey Panin1249c512005-06-25 14:54:47 -0700351 dmi_save_ident(dm, DMI_PRODUCT_NAME, 5);
Andrey Panin1249c512005-06-25 14:54:47 -0700352 dmi_save_ident(dm, DMI_PRODUCT_VERSION, 6);
Andrey Panin1249c512005-06-25 14:54:47 -0700353 dmi_save_ident(dm, DMI_PRODUCT_SERIAL, 7);
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200354 dmi_save_uuid(dm, DMI_PRODUCT_UUID, 8);
Andrey Panin1249c512005-06-25 14:54:47 -0700355 break;
Andrey Paninebad6a42005-09-06 15:18:29 -0700356 case 2: /* Base Board Information */
Andrey Panin1249c512005-06-25 14:54:47 -0700357 dmi_save_ident(dm, DMI_BOARD_VENDOR, 4);
Andrey Panin1249c512005-06-25 14:54:47 -0700358 dmi_save_ident(dm, DMI_BOARD_NAME, 5);
Andrey Panin1249c512005-06-25 14:54:47 -0700359 dmi_save_ident(dm, DMI_BOARD_VERSION, 6);
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200360 dmi_save_ident(dm, DMI_BOARD_SERIAL, 7);
361 dmi_save_ident(dm, DMI_BOARD_ASSET_TAG, 8);
362 break;
363 case 3: /* Chassis Information */
364 dmi_save_ident(dm, DMI_CHASSIS_VENDOR, 4);
365 dmi_save_type(dm, DMI_CHASSIS_TYPE, 5);
366 dmi_save_ident(dm, DMI_CHASSIS_VERSION, 6);
367 dmi_save_ident(dm, DMI_CHASSIS_SERIAL, 7);
368 dmi_save_ident(dm, DMI_CHASSIS_ASSET_TAG, 8);
Andrey Panin1249c512005-06-25 14:54:47 -0700369 break;
Andrey Paninebad6a42005-09-06 15:18:29 -0700370 case 10: /* Onboard Devices Information */
371 dmi_save_devices(dm);
372 break;
Shem Multinymous2e0c1f62006-09-29 01:59:37 -0700373 case 11: /* OEM Strings */
374 dmi_save_oem_strings_devices(dm);
375 break;
Andrey Paninebad6a42005-09-06 15:18:29 -0700376 case 38: /* IPMI Device Information */
377 dmi_save_ipmi_device(dm);
Wim Van Sebroeckb4bd7d52008-02-08 04:20:58 -0800378 break;
379 case 41: /* Onboard Devices Extended Information */
380 dmi_save_extended_devices(dm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 }
382}
383
Tejun Heoc90fe6b2013-04-30 15:27:14 -0700384static int __init print_filtered(char *buf, size_t len, const char *info)
Bjorn Helgaas8881cdc2010-10-27 15:32:59 -0700385{
Tejun Heoc90fe6b2013-04-30 15:27:14 -0700386 int c = 0;
Bjorn Helgaas8881cdc2010-10-27 15:32:59 -0700387 const char *p;
388
389 if (!info)
Tejun Heoc90fe6b2013-04-30 15:27:14 -0700390 return c;
Bjorn Helgaas8881cdc2010-10-27 15:32:59 -0700391
392 for (p = info; *p; p++)
393 if (isprint(*p))
Tejun Heoc90fe6b2013-04-30 15:27:14 -0700394 c += scnprintf(buf + c, len - c, "%c", *p);
Bjorn Helgaas8881cdc2010-10-27 15:32:59 -0700395 else
Tejun Heoc90fe6b2013-04-30 15:27:14 -0700396 c += scnprintf(buf + c, len - c, "\\x%02x", *p & 0xff);
397 return c;
Bjorn Helgaas8881cdc2010-10-27 15:32:59 -0700398}
399
Tejun Heoc90fe6b2013-04-30 15:27:14 -0700400static void __init dmi_format_ids(char *buf, size_t len)
Bjorn Helgaas8881cdc2010-10-27 15:32:59 -0700401{
Tejun Heoc90fe6b2013-04-30 15:27:14 -0700402 int c = 0;
Naga Chumbalkar84e383b2011-02-14 22:47:17 +0000403 const char *board; /* Board Name is optional */
404
Tejun Heoc90fe6b2013-04-30 15:27:14 -0700405 c += print_filtered(buf + c, len - c,
406 dmi_get_system_info(DMI_SYS_VENDOR));
407 c += scnprintf(buf + c, len - c, " ");
408 c += print_filtered(buf + c, len - c,
409 dmi_get_system_info(DMI_PRODUCT_NAME));
410
Naga Chumbalkar84e383b2011-02-14 22:47:17 +0000411 board = dmi_get_system_info(DMI_BOARD_NAME);
412 if (board) {
Tejun Heoc90fe6b2013-04-30 15:27:14 -0700413 c += scnprintf(buf + c, len - c, "/");
414 c += print_filtered(buf + c, len - c, board);
Naga Chumbalkar84e383b2011-02-14 22:47:17 +0000415 }
Tejun Heoc90fe6b2013-04-30 15:27:14 -0700416 c += scnprintf(buf + c, len - c, ", BIOS ");
417 c += print_filtered(buf + c, len - c,
418 dmi_get_system_info(DMI_BIOS_VERSION));
419 c += scnprintf(buf + c, len - c, " ");
420 c += print_filtered(buf + c, len - c,
421 dmi_get_system_info(DMI_BIOS_DATE));
Bjorn Helgaas8881cdc2010-10-27 15:32:59 -0700422}
423
Ben Hutchingsd39de282013-07-31 13:53:30 -0700424/*
425 * Check for DMI/SMBIOS headers in the system firmware image. Any
426 * SMBIOS header must start 16 bytes before the DMI header, so take a
427 * 32 byte buffer and check for DMI at offset 16 and SMBIOS at offset
428 * 0. If the DMI header is present, set dmi_ver accordingly (SMBIOS
429 * takes precedence) and return 0. Otherwise return 1.
430 */
Ben Hutchings79bae422013-04-30 15:27:46 -0700431static int __init dmi_present(const u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432{
Ben Hutchings79bae422013-04-30 15:27:46 -0700433 int smbios_ver;
Jeff Garzik18552562007-10-03 15:15:40 -0400434
Ben Hutchings79bae422013-04-30 15:27:46 -0700435 if (memcmp(buf, "_SM_", 4) == 0 &&
436 buf[5] < 32 && dmi_checksum(buf, buf[5])) {
437 smbios_ver = (buf[6] << 8) + buf[7];
438
439 /* Some BIOS report weird SMBIOS version, fix that up */
440 switch (smbios_ver) {
441 case 0x021F:
442 case 0x0221:
443 pr_debug("SMBIOS version fixup(2.%d->2.%d)\n",
444 smbios_ver & 0xFF, 3);
445 smbios_ver = 0x0203;
446 break;
447 case 0x0233:
448 pr_debug("SMBIOS version fixup(2.%d->2.%d)\n", 51, 6);
449 smbios_ver = 0x0206;
450 break;
451 }
452 } else {
453 smbios_ver = 0;
454 }
455
456 buf += 16;
457
458 if (memcmp(buf, "_DMI_", 5) == 0 && dmi_checksum(buf, 15)) {
Jean Delvare7fce0842007-11-03 17:29:20 +0100459 dmi_num = (buf[13] << 8) | buf[12];
460 dmi_len = (buf[7] << 8) | buf[6];
461 dmi_base = (buf[11] << 24) | (buf[10] << 16) |
Matt Domsch3ed3bce2006-03-26 01:37:03 -0800462 (buf[9] << 8) | buf[8];
463
Bjorn Helgaas8881cdc2010-10-27 15:32:59 -0700464 if (dmi_walk_early(dmi_decode) == 0) {
Ben Hutchings79bae422013-04-30 15:27:46 -0700465 if (smbios_ver) {
466 dmi_ver = smbios_ver;
Zhenzhong Duan9f9c9cbb2012-12-20 15:05:14 -0800467 pr_info("SMBIOS %d.%d present.\n",
468 dmi_ver >> 8, dmi_ver & 0xFF);
Ben Hutchings79bae422013-04-30 15:27:46 -0700469 } else {
Zhenzhong Duan9f9c9cbb2012-12-20 15:05:14 -0800470 dmi_ver = (buf[14] & 0xF0) << 4 |
471 (buf[14] & 0x0F);
472 pr_info("Legacy DMI %d.%d present.\n",
473 dmi_ver >> 8, dmi_ver & 0xFF);
474 }
Tejun Heoc90fe6b2013-04-30 15:27:14 -0700475 dmi_format_ids(dmi_ids_string, sizeof(dmi_ids_string));
476 printk(KERN_DEBUG "DMI: %s\n", dmi_ids_string);
Matt Domsch3ed3bce2006-03-26 01:37:03 -0800477 return 0;
Bjorn Helgaas8881cdc2010-10-27 15:32:59 -0700478 }
Matt Domsch3ed3bce2006-03-26 01:37:03 -0800479 }
Matt Domsch3ed3bce2006-03-26 01:37:03 -0800480
Ben Hutchingsa40e7cf2013-03-08 12:43:32 -0800481 return 1;
Zhenzhong Duan9f9c9cbb2012-12-20 15:05:14 -0800482}
483
Matt Domsch3ed3bce2006-03-26 01:37:03 -0800484void __init dmi_scan_machine(void)
485{
Andrey Panin61e032fa2005-09-06 15:18:26 -0700486 char __iomem *p, *q;
Ben Hutchings79bae422013-04-30 15:27:46 -0700487 char buf[32];
Andrey Panin61e032fa2005-09-06 15:18:26 -0700488
Matt Fleming83e68182012-11-14 09:42:35 +0000489 if (efi_enabled(EFI_CONFIG_TABLES)) {
Bjorn Helgaasb2c99e32006-03-26 01:37:08 -0800490 if (efi.smbios == EFI_INVALID_TABLE_ADDR)
Ingo Molnar9a22b6e2008-09-18 12:50:18 +0200491 goto error;
Andrey Panin61e032fa2005-09-06 15:18:26 -0700492
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200493 /* This is called as a core_initcall() because it isn't
494 * needed during early boot. This also means we can
495 * iounmap the space when we're done with it.
496 */
Bjorn Helgaasb2c99e32006-03-26 01:37:08 -0800497 p = dmi_ioremap(efi.smbios, 32);
Matt Domsch3ed3bce2006-03-26 01:37:03 -0800498 if (p == NULL)
Ingo Molnar9a22b6e2008-09-18 12:50:18 +0200499 goto error;
Ben Hutchings79bae422013-04-30 15:27:46 -0700500 memcpy_fromio(buf, p, 32);
Tolentino, Matthew E23dd8422006-03-26 01:37:09 -0800501 dmi_iounmap(p, 32);
Ben Hutchings79bae422013-04-30 15:27:46 -0700502
503 if (!dmi_present(buf)) {
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200504 dmi_available = 1;
Ingo Molnar9a22b6e2008-09-18 12:50:18 +0200505 goto out;
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200506 }
Jean Delvare02d9c472013-09-11 14:24:08 -0700507 } else {
Matt Domsch3ed3bce2006-03-26 01:37:03 -0800508 p = dmi_ioremap(0xF0000, 0x10000);
509 if (p == NULL)
Ingo Molnar9a22b6e2008-09-18 12:50:18 +0200510 goto error;
Andrey Panin61e032fa2005-09-06 15:18:26 -0700511
Ben Hutchingsd39de282013-07-31 13:53:30 -0700512 /*
513 * Iterate over all possible DMI header addresses q.
514 * Maintain the 32 bytes around q in buf. On the
515 * first iteration, substitute zero for the
516 * out-of-range bytes so there is no chance of falsely
517 * detecting an SMBIOS header.
518 */
Ben Hutchings79bae422013-04-30 15:27:46 -0700519 memset(buf, 0, 16);
Matt Domsch3ed3bce2006-03-26 01:37:03 -0800520 for (q = p; q < p + 0x10000; q += 16) {
Ben Hutchings79bae422013-04-30 15:27:46 -0700521 memcpy_fromio(buf + 16, q, 16);
522 if (!dmi_present(buf)) {
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200523 dmi_available = 1;
Ingo Molnar0d644842008-01-30 13:33:09 +0100524 dmi_iounmap(p, 0x10000);
Ingo Molnar9a22b6e2008-09-18 12:50:18 +0200525 goto out;
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200526 }
Ben Hutchings79bae422013-04-30 15:27:46 -0700527 memcpy(buf, buf + 16, 16);
Andrey Panin61e032fa2005-09-06 15:18:26 -0700528 }
Yinghai Lu3212bff2008-01-30 13:33:32 +0100529 dmi_iounmap(p, 0x10000);
Andrey Panin61e032fa2005-09-06 15:18:26 -0700530 }
Ingo Molnar9a22b6e2008-09-18 12:50:18 +0200531 error:
Jean Delvare02d9c472013-09-11 14:24:08 -0700532 pr_info("DMI not present or invalid.\n");
Ingo Molnar9a22b6e2008-09-18 12:50:18 +0200533 out:
534 dmi_initialized = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535}
536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537/**
Tejun Heo98e5e1b2013-04-30 15:27:15 -0700538 * dmi_set_dump_stack_arch_desc - set arch description for dump_stack()
539 *
540 * Invoke dump_stack_set_arch_desc() with DMI system information so that
541 * DMI identifiers are printed out on task dumps. Arch boot code should
542 * call this function after dmi_scan_machine() if it wants to print out DMI
543 * identifiers on task dumps.
544 */
545void __init dmi_set_dump_stack_arch_desc(void)
546{
547 dump_stack_set_arch_desc("%s", dmi_ids_string);
548}
549
550/**
Rafael J. Wysockid7b19562009-01-19 20:55:50 +0100551 * dmi_matches - check if dmi_system_id structure matches system DMI data
552 * @dmi: pointer to the dmi_system_id structure to check
553 */
554static bool dmi_matches(const struct dmi_system_id *dmi)
555{
556 int i;
557
558 WARN(!dmi_initialized, KERN_ERR "dmi check: not initialized yet.\n");
559
560 for (i = 0; i < ARRAY_SIZE(dmi->matches); i++) {
561 int s = dmi->matches[i].slot;
562 if (s == DMI_NONE)
Dmitry Torokhov75757502009-12-04 10:24:19 -0800563 break;
Jani Nikula5017b282013-07-03 15:05:02 -0700564 if (dmi_ident[s]) {
565 if (!dmi->matches[i].exact_match &&
566 strstr(dmi_ident[s], dmi->matches[i].substr))
567 continue;
568 else if (dmi->matches[i].exact_match &&
569 !strcmp(dmi_ident[s], dmi->matches[i].substr))
570 continue;
571 }
572
Rafael J. Wysockid7b19562009-01-19 20:55:50 +0100573 /* No match */
574 return false;
575 }
576 return true;
577}
578
579/**
Dmitry Torokhov75757502009-12-04 10:24:19 -0800580 * dmi_is_end_of_table - check for end-of-table marker
581 * @dmi: pointer to the dmi_system_id structure to check
582 */
583static bool dmi_is_end_of_table(const struct dmi_system_id *dmi)
584{
585 return dmi->matches[0].slot == DMI_NONE;
586}
587
588/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 * dmi_check_system - check system DMI data
590 * @list: array of dmi_system_id structures to match against
Randy Dunlapb0ef3712006-06-25 05:49:18 -0700591 * All non-null elements of the list must match
592 * their slot's (field index's) data (i.e., each
593 * list string must be a substring of the specified
594 * DMI slot's string data) to be considered a
595 * successful match.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 *
597 * Walk the blacklist table running matching functions until someone
598 * returns non zero or we hit the end. Callback function is called for
Randy Dunlapb0ef3712006-06-25 05:49:18 -0700599 * each successful match. Returns the number of matches.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 */
Jeff Garzik18552562007-10-03 15:15:40 -0400601int dmi_check_system(const struct dmi_system_id *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602{
Rafael J. Wysockid7b19562009-01-19 20:55:50 +0100603 int count = 0;
604 const struct dmi_system_id *d;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
Dmitry Torokhov75757502009-12-04 10:24:19 -0800606 for (d = list; !dmi_is_end_of_table(d); d++)
Rafael J. Wysockid7b19562009-01-19 20:55:50 +0100607 if (dmi_matches(d)) {
608 count++;
609 if (d->callback && d->callback(d))
610 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
613 return count;
614}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615EXPORT_SYMBOL(dmi_check_system);
616
617/**
Rafael J. Wysockid7b19562009-01-19 20:55:50 +0100618 * dmi_first_match - find dmi_system_id structure matching system DMI data
619 * @list: array of dmi_system_id structures to match against
620 * All non-null elements of the list must match
621 * their slot's (field index's) data (i.e., each
622 * list string must be a substring of the specified
623 * DMI slot's string data) to be considered a
624 * successful match.
625 *
626 * Walk the blacklist table until the first match is found. Return the
627 * pointer to the matching entry or NULL if there's no match.
628 */
629const struct dmi_system_id *dmi_first_match(const struct dmi_system_id *list)
630{
631 const struct dmi_system_id *d;
632
Dmitry Torokhov75757502009-12-04 10:24:19 -0800633 for (d = list; !dmi_is_end_of_table(d); d++)
Rafael J. Wysockid7b19562009-01-19 20:55:50 +0100634 if (dmi_matches(d))
635 return d;
636
637 return NULL;
638}
639EXPORT_SYMBOL(dmi_first_match);
640
641/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 * dmi_get_system_info - return DMI data value
Randy Dunlapb0ef3712006-06-25 05:49:18 -0700643 * @field: data index (see enum dmi_field)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 *
645 * Returns one DMI data value, can be used to perform
646 * complex DMI data checks.
647 */
Jeff Garzik18552562007-10-03 15:15:40 -0400648const char *dmi_get_system_info(int field)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649{
650 return dmi_ident[field];
651}
Dmitry Torokhove70c9d52005-06-25 14:54:25 -0700652EXPORT_SYMBOL(dmi_get_system_info);
Andrey Paninebad6a42005-09-06 15:18:29 -0700653
Alok Katariafd8cd7e2008-11-03 15:50:38 -0800654/**
Randy Dunlapc2bacfc2009-01-06 14:41:40 -0800655 * dmi_name_in_serial - Check if string is in the DMI product serial information
656 * @str: string to check for
Alok Katariafd8cd7e2008-11-03 15:50:38 -0800657 */
658int dmi_name_in_serial(const char *str)
659{
660 int f = DMI_PRODUCT_SERIAL;
661 if (dmi_ident[f] && strstr(dmi_ident[f], str))
662 return 1;
663 return 0;
664}
Andi Kleena1bae672006-10-21 18:37:02 +0200665
666/**
Jean Delvare66e13e62011-11-15 14:36:09 -0800667 * dmi_name_in_vendors - Check if string is in the DMI system or board vendor name
Jean Delvare02d9c472013-09-11 14:24:08 -0700668 * @str: Case sensitive Name
Andi Kleena1bae672006-10-21 18:37:02 +0200669 */
Jeff Garzik18552562007-10-03 15:15:40 -0400670int dmi_name_in_vendors(const char *str)
Andi Kleena1bae672006-10-21 18:37:02 +0200671{
Jean Delvare66e13e62011-11-15 14:36:09 -0800672 static int fields[] = { DMI_SYS_VENDOR, DMI_BOARD_VENDOR, DMI_NONE };
Andi Kleena1bae672006-10-21 18:37:02 +0200673 int i;
674 for (i = 0; fields[i] != DMI_NONE; i++) {
675 int f = fields[i];
676 if (dmi_ident[f] && strstr(dmi_ident[f], str))
677 return 1;
678 }
679 return 0;
680}
681EXPORT_SYMBOL(dmi_name_in_vendors);
682
Andrey Paninebad6a42005-09-06 15:18:29 -0700683/**
684 * dmi_find_device - find onboard device by type/name
685 * @type: device type or %DMI_DEV_TYPE_ANY to match all device types
Randy Dunlapb0ef3712006-06-25 05:49:18 -0700686 * @name: device name string or %NULL to match all
Andrey Paninebad6a42005-09-06 15:18:29 -0700687 * @from: previous device found in search, or %NULL for new search.
688 *
689 * Iterates through the list of known onboard devices. If a device is
690 * found with a matching @vendor and @device, a pointer to its device
691 * structure is returned. Otherwise, %NULL is returned.
Randy Dunlapb0ef3712006-06-25 05:49:18 -0700692 * A new search is initiated by passing %NULL as the @from argument.
Andrey Paninebad6a42005-09-06 15:18:29 -0700693 * If @from is not %NULL, searches continue from next device.
694 */
Jean Delvare02d9c472013-09-11 14:24:08 -0700695const struct dmi_device *dmi_find_device(int type, const char *name,
Jeff Garzik18552562007-10-03 15:15:40 -0400696 const struct dmi_device *from)
Andrey Paninebad6a42005-09-06 15:18:29 -0700697{
Jeff Garzik18552562007-10-03 15:15:40 -0400698 const struct list_head *head = from ? &from->list : &dmi_devices;
699 struct list_head *d;
Andrey Paninebad6a42005-09-06 15:18:29 -0700700
Jean Delvare02d9c472013-09-11 14:24:08 -0700701 for (d = head->next; d != &dmi_devices; d = d->next) {
Jeff Garzik18552562007-10-03 15:15:40 -0400702 const struct dmi_device *dev =
703 list_entry(d, struct dmi_device, list);
Andrey Paninebad6a42005-09-06 15:18:29 -0700704
705 if (((type == DMI_DEV_TYPE_ANY) || (dev->type == type)) &&
706 ((name == NULL) || (strcmp(dev->name, name) == 0)))
707 return dev;
708 }
709
710 return NULL;
711}
712EXPORT_SYMBOL(dmi_find_device);
Andi Kleenf083a322006-03-25 16:30:19 +0100713
714/**
Tejun Heo3e5cd1f2009-08-16 21:02:36 +0900715 * dmi_get_date - parse a DMI date
716 * @field: data index (see enum dmi_field)
717 * @yearp: optional out parameter for the year
718 * @monthp: optional out parameter for the month
719 * @dayp: optional out parameter for the day
Andi Kleenf083a322006-03-25 16:30:19 +0100720 *
Tejun Heo3e5cd1f2009-08-16 21:02:36 +0900721 * The date field is assumed to be in the form resembling
722 * [mm[/dd]]/yy[yy] and the result is stored in the out
723 * parameters any or all of which can be omitted.
724 *
725 * If the field doesn't exist, all out parameters are set to zero
726 * and false is returned. Otherwise, true is returned with any
727 * invalid part of date set to zero.
728 *
729 * On return, year, month and day are guaranteed to be in the
730 * range of [0,9999], [0,12] and [0,31] respectively.
Andi Kleenf083a322006-03-25 16:30:19 +0100731 */
Tejun Heo3e5cd1f2009-08-16 21:02:36 +0900732bool dmi_get_date(int field, int *yearp, int *monthp, int *dayp)
Andi Kleenf083a322006-03-25 16:30:19 +0100733{
Tejun Heo3e5cd1f2009-08-16 21:02:36 +0900734 int year = 0, month = 0, day = 0;
735 bool exists;
736 const char *s, *y;
Tejun Heo02c24fa2009-08-16 21:01:22 +0900737 char *e;
Andi Kleenf083a322006-03-25 16:30:19 +0100738
Tejun Heo3e5cd1f2009-08-16 21:02:36 +0900739 s = dmi_get_system_info(field);
740 exists = s;
741 if (!exists)
742 goto out;
Andi Kleenf083a322006-03-25 16:30:19 +0100743
Tejun Heo3e5cd1f2009-08-16 21:02:36 +0900744 /*
745 * Determine year first. We assume the date string resembles
746 * mm/dd/yy[yy] but the original code extracted only the year
747 * from the end. Keep the behavior in the spirit of no
748 * surprises.
749 */
750 y = strrchr(s, '/');
751 if (!y)
752 goto out;
753
754 y++;
755 year = simple_strtoul(y, &e, 10);
756 if (y != e && year < 100) { /* 2-digit year */
Andi Kleenf083a322006-03-25 16:30:19 +0100757 year += 1900;
758 if (year < 1996) /* no dates < spec 1.0 */
759 year += 100;
760 }
Tejun Heo3e5cd1f2009-08-16 21:02:36 +0900761 if (year > 9999) /* year should fit in %04d */
762 year = 0;
Andi Kleenf083a322006-03-25 16:30:19 +0100763
Tejun Heo3e5cd1f2009-08-16 21:02:36 +0900764 /* parse the mm and dd */
765 month = simple_strtoul(s, &e, 10);
766 if (s == e || *e != '/' || !month || month > 12) {
767 month = 0;
768 goto out;
769 }
770
771 s = e + 1;
772 day = simple_strtoul(s, &e, 10);
773 if (s == y || s == e || *e != '/' || day > 31)
774 day = 0;
775out:
776 if (yearp)
777 *yearp = year;
778 if (monthp)
779 *monthp = month;
780 if (dayp)
781 *dayp = day;
782 return exists;
Andi Kleenf083a322006-03-25 16:30:19 +0100783}
Tejun Heo3e5cd1f2009-08-16 21:02:36 +0900784EXPORT_SYMBOL(dmi_get_date);
Jean Delvare7fce0842007-11-03 17:29:20 +0100785
786/**
787 * dmi_walk - Walk the DMI table and get called back for every record
788 * @decode: Callback function
Jean Delvaree7a19c562009-03-30 21:46:44 +0200789 * @private_data: Private data to be passed to the callback function
Jean Delvare7fce0842007-11-03 17:29:20 +0100790 *
791 * Returns -1 when the DMI table can't be reached, 0 on success.
792 */
Jean Delvaree7a19c562009-03-30 21:46:44 +0200793int dmi_walk(void (*decode)(const struct dmi_header *, void *),
794 void *private_data)
Jean Delvare7fce0842007-11-03 17:29:20 +0100795{
796 u8 *buf;
797
798 if (!dmi_available)
799 return -1;
800
801 buf = ioremap(dmi_base, dmi_len);
802 if (buf == NULL)
803 return -1;
804
Jean Delvaree7a19c562009-03-30 21:46:44 +0200805 dmi_table(buf, dmi_len, dmi_num, decode, private_data);
Jean Delvare7fce0842007-11-03 17:29:20 +0100806
807 iounmap(buf);
808 return 0;
809}
810EXPORT_SYMBOL_GPL(dmi_walk);
Jiri Slabyd61c72e2008-12-10 14:07:21 +0100811
812/**
813 * dmi_match - compare a string to the dmi field (if exists)
Randy Dunlapc2bacfc2009-01-06 14:41:40 -0800814 * @f: DMI field identifier
815 * @str: string to compare the DMI field to
Jiri Slabyd61c72e2008-12-10 14:07:21 +0100816 *
817 * Returns true if the requested field equals to the str (including NULL).
818 */
819bool dmi_match(enum dmi_field f, const char *str)
820{
821 const char *info = dmi_get_system_info(f);
822
823 if (info == NULL || str == NULL)
824 return info == str;
825
826 return !strcmp(info, str);
827}
828EXPORT_SYMBOL_GPL(dmi_match);