blob: eb760a218da4292f7c208ddc145deffb833df7ab [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 */
Parag Warudkar79da4722008-01-30 13:31:59 +010017static char dmi_empty_string[] = " ";
18
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
52static char * __init dmi_string(const struct dmi_header *dm, u8 s)
53{
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
66 printk(KERN_ERR "dmi_string: cannot allocate %Zu bytes.\n", len);
67
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136static 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 */
Jeff Garzik18552562007-10-03 15:15:40 -0400143static void __init dmi_save_ident(const struct dmi_header *dm, int slot, int string)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
Jeff Garzik18552562007-10-03 15:15:40 -0400145 const char *d = (const char*) dm;
146 char *p;
Andrey Panin1249c512005-06-25 14:54:47 -0700147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 if (dmi_ident[slot])
149 return;
Andrey Panin1249c512005-06-25 14:54:47 -0700150
Andrey Paninc3c71202005-09-06 15:18:28 -0700151 p = dmi_string(dm, d[string]);
152 if (p == NULL)
153 return;
154
155 dmi_ident[slot] = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156}
157
Jeff Garzik18552562007-10-03 15:15:40 -0400158static void __init dmi_save_uuid(const struct dmi_header *dm, int slot, int index)
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200159{
Jeff Garzik18552562007-10-03 15:15:40 -0400160 const u8 *d = (u8*) dm + index;
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200161 char *s;
162 int is_ff = 1, is_00 = 1, i;
163
164 if (dmi_ident[slot])
165 return;
166
167 for (i = 0; i < 16 && (is_ff || is_00); i++) {
Zhenzhong Duanf1d8e612012-12-20 15:05:13 -0800168 if (d[i] != 0x00)
169 is_00 = 0;
170 if (d[i] != 0xFF)
171 is_ff = 0;
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200172 }
173
174 if (is_ff || is_00)
175 return;
176
177 s = dmi_alloc(16*2+4+1);
178 if (!s)
179 return;
180
Zhenzhong Duanf1d8e612012-12-20 15:05:13 -0800181 /*
182 * As of version 2.6 of the SMBIOS specification, the first 3 fields of
183 * the UUID are supposed to be little-endian encoded. The specification
184 * says that this is the defacto standard.
185 */
186 if (dmi_ver >= 0x0206)
187 sprintf(s, "%pUL", d);
188 else
189 sprintf(s, "%pUB", d);
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200190
191 dmi_ident[slot] = s;
192}
193
Jeff Garzik18552562007-10-03 15:15:40 -0400194static void __init dmi_save_type(const struct dmi_header *dm, int slot, int index)
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200195{
Jeff Garzik18552562007-10-03 15:15:40 -0400196 const u8 *d = (u8*) dm + index;
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200197 char *s;
198
199 if (dmi_ident[slot])
200 return;
201
202 s = dmi_alloc(4);
203 if (!s)
204 return;
205
206 sprintf(s, "%u", *d & 0x7F);
207 dmi_ident[slot] = s;
208}
209
Jean Delvaref3069ae2008-02-23 15:23:46 -0800210static void __init dmi_save_one_device(int type, const char *name)
211{
212 struct dmi_device *dev;
213
214 /* No duplicate device */
215 if (dmi_find_device(type, name, NULL))
216 return;
217
218 dev = dmi_alloc(sizeof(*dev) + strlen(name) + 1);
219 if (!dev) {
220 printk(KERN_ERR "dmi_save_one_device: out of memory.\n");
221 return;
222 }
223
224 dev->type = type;
225 strcpy((char *)(dev + 1), name);
226 dev->name = (char *)(dev + 1);
227 dev->device_data = NULL;
228 list_add(&dev->list, &dmi_devices);
229}
230
Jeff Garzik18552562007-10-03 15:15:40 -0400231static void __init dmi_save_devices(const struct dmi_header *dm)
Andrey Paninebad6a42005-09-06 15:18:29 -0700232{
233 int i, count = (dm->length - sizeof(struct dmi_header)) / 2;
Andrey Paninebad6a42005-09-06 15:18:29 -0700234
235 for (i = 0; i < count; i++) {
Jeff Garzik18552562007-10-03 15:15:40 -0400236 const char *d = (char *)(dm + 1) + (i * 2);
Andrey Paninebad6a42005-09-06 15:18:29 -0700237
238 /* Skip disabled device */
239 if ((*d & 0x80) == 0)
240 continue;
241
Jean Delvaref3069ae2008-02-23 15:23:46 -0800242 dmi_save_one_device(*d & 0x7f, dmi_string_nosave(dm, *(d + 1)));
Shem Multinymous2e0c1f62006-09-29 01:59:37 -0700243 }
244}
245
Jeff Garzik18552562007-10-03 15:15:40 -0400246static void __init dmi_save_oem_strings_devices(const struct dmi_header *dm)
Shem Multinymous2e0c1f62006-09-29 01:59:37 -0700247{
248 int i, count = *(u8 *)(dm + 1);
249 struct dmi_device *dev;
250
251 for (i = 1; i <= count; i++) {
Parag Warudkar79da4722008-01-30 13:31:59 +0100252 char *devname = dmi_string(dm, i);
253
Jean Delvare43fe1052008-02-23 15:23:55 -0800254 if (devname == dmi_empty_string)
Parag Warudkar79da4722008-01-30 13:31:59 +0100255 continue;
Parag Warudkar79da4722008-01-30 13:31:59 +0100256
Shem Multinymous2e0c1f62006-09-29 01:59:37 -0700257 dev = dmi_alloc(sizeof(*dev));
258 if (!dev) {
259 printk(KERN_ERR
260 "dmi_save_oem_strings_devices: out of memory.\n");
261 break;
262 }
263
264 dev->type = DMI_DEV_TYPE_OEM_STRING;
Parag Warudkar79da4722008-01-30 13:31:59 +0100265 dev->name = devname;
Shem Multinymous2e0c1f62006-09-29 01:59:37 -0700266 dev->device_data = NULL;
Andrey Paninebad6a42005-09-06 15:18:29 -0700267
268 list_add(&dev->list, &dmi_devices);
269 }
270}
271
Jeff Garzik18552562007-10-03 15:15:40 -0400272static void __init dmi_save_ipmi_device(const struct dmi_header *dm)
Andrey Paninebad6a42005-09-06 15:18:29 -0700273{
274 struct dmi_device *dev;
275 void * data;
276
Andi Kleene9928672006-01-11 22:43:33 +0100277 data = dmi_alloc(dm->length);
Andrey Paninebad6a42005-09-06 15:18:29 -0700278 if (data == NULL) {
279 printk(KERN_ERR "dmi_save_ipmi_device: out of memory.\n");
280 return;
281 }
282
283 memcpy(data, dm, dm->length);
284
Andi Kleene9928672006-01-11 22:43:33 +0100285 dev = dmi_alloc(sizeof(*dev));
Andrey Paninebad6a42005-09-06 15:18:29 -0700286 if (!dev) {
287 printk(KERN_ERR "dmi_save_ipmi_device: out of memory.\n");
288 return;
289 }
290
291 dev->type = DMI_DEV_TYPE_IPMI;
292 dev->name = "IPMI controller";
293 dev->device_data = data;
294
Carol Hebertabd24df2008-04-04 14:30:03 -0700295 list_add_tail(&dev->list, &dmi_devices);
Andrey Paninebad6a42005-09-06 15:18:29 -0700296}
297
Narendra K911e1c92010-07-26 05:56:50 -0500298static void __init dmi_save_dev_onboard(int instance, int segment, int bus,
299 int devfn, const char *name)
300{
301 struct dmi_dev_onboard *onboard_dev;
302
303 onboard_dev = dmi_alloc(sizeof(*onboard_dev) + strlen(name) + 1);
304 if (!onboard_dev) {
305 printk(KERN_ERR "dmi_save_dev_onboard: out of memory.\n");
306 return;
307 }
308 onboard_dev->instance = instance;
309 onboard_dev->segment = segment;
310 onboard_dev->bus = bus;
311 onboard_dev->devfn = devfn;
312
313 strcpy((char *)&onboard_dev[1], name);
314 onboard_dev->dev.type = DMI_DEV_TYPE_DEV_ONBOARD;
315 onboard_dev->dev.name = (char *)&onboard_dev[1];
316 onboard_dev->dev.device_data = onboard_dev;
317
318 list_add(&onboard_dev->dev.list, &dmi_devices);
319}
320
Wim Van Sebroeckb4bd7d52008-02-08 04:20:58 -0800321static void __init dmi_save_extended_devices(const struct dmi_header *dm)
322{
323 const u8 *d = (u8*) dm + 5;
Wim Van Sebroeckb4bd7d52008-02-08 04:20:58 -0800324
325 /* Skip disabled device */
326 if ((*d & 0x80) == 0)
327 return;
328
Narendra K911e1c92010-07-26 05:56:50 -0500329 dmi_save_dev_onboard(*(d+1), *(u16 *)(d+2), *(d+4), *(d+5),
330 dmi_string_nosave(dm, *(d-1)));
Jean Delvaref3069ae2008-02-23 15:23:46 -0800331 dmi_save_one_device(*d & 0x7f, dmi_string_nosave(dm, *(d - 1)));
Wim Van Sebroeckb4bd7d52008-02-08 04:20:58 -0800332}
333
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 * Process a DMI table entry. Right now all we care about are the BIOS
336 * and machine entries. For 2.5 we should pull the smbus controller info
337 * out of here.
338 */
Jean Delvaree7a19c562009-03-30 21:46:44 +0200339static void __init dmi_decode(const struct dmi_header *dm, void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340{
Andrey Panin1249c512005-06-25 14:54:47 -0700341 switch(dm->type) {
Andrey Paninebad6a42005-09-06 15:18:29 -0700342 case 0: /* BIOS Information */
Andrey Panin1249c512005-06-25 14:54:47 -0700343 dmi_save_ident(dm, DMI_BIOS_VENDOR, 4);
Andrey Panin1249c512005-06-25 14:54:47 -0700344 dmi_save_ident(dm, DMI_BIOS_VERSION, 5);
Andrey Panin1249c512005-06-25 14:54:47 -0700345 dmi_save_ident(dm, DMI_BIOS_DATE, 8);
346 break;
Andrey Paninebad6a42005-09-06 15:18:29 -0700347 case 1: /* System Information */
Andrey Panin1249c512005-06-25 14:54:47 -0700348 dmi_save_ident(dm, DMI_SYS_VENDOR, 4);
Andrey Panin1249c512005-06-25 14:54:47 -0700349 dmi_save_ident(dm, DMI_PRODUCT_NAME, 5);
Andrey Panin1249c512005-06-25 14:54:47 -0700350 dmi_save_ident(dm, DMI_PRODUCT_VERSION, 6);
Andrey Panin1249c512005-06-25 14:54:47 -0700351 dmi_save_ident(dm, DMI_PRODUCT_SERIAL, 7);
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200352 dmi_save_uuid(dm, DMI_PRODUCT_UUID, 8);
Andrey Panin1249c512005-06-25 14:54:47 -0700353 break;
Andrey Paninebad6a42005-09-06 15:18:29 -0700354 case 2: /* Base Board Information */
Andrey Panin1249c512005-06-25 14:54:47 -0700355 dmi_save_ident(dm, DMI_BOARD_VENDOR, 4);
Andrey Panin1249c512005-06-25 14:54:47 -0700356 dmi_save_ident(dm, DMI_BOARD_NAME, 5);
Andrey Panin1249c512005-06-25 14:54:47 -0700357 dmi_save_ident(dm, DMI_BOARD_VERSION, 6);
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200358 dmi_save_ident(dm, DMI_BOARD_SERIAL, 7);
359 dmi_save_ident(dm, DMI_BOARD_ASSET_TAG, 8);
360 break;
361 case 3: /* Chassis Information */
362 dmi_save_ident(dm, DMI_CHASSIS_VENDOR, 4);
363 dmi_save_type(dm, DMI_CHASSIS_TYPE, 5);
364 dmi_save_ident(dm, DMI_CHASSIS_VERSION, 6);
365 dmi_save_ident(dm, DMI_CHASSIS_SERIAL, 7);
366 dmi_save_ident(dm, DMI_CHASSIS_ASSET_TAG, 8);
Andrey Panin1249c512005-06-25 14:54:47 -0700367 break;
Andrey Paninebad6a42005-09-06 15:18:29 -0700368 case 10: /* Onboard Devices Information */
369 dmi_save_devices(dm);
370 break;
Shem Multinymous2e0c1f62006-09-29 01:59:37 -0700371 case 11: /* OEM Strings */
372 dmi_save_oem_strings_devices(dm);
373 break;
Andrey Paninebad6a42005-09-06 15:18:29 -0700374 case 38: /* IPMI Device Information */
375 dmi_save_ipmi_device(dm);
Wim Van Sebroeckb4bd7d52008-02-08 04:20:58 -0800376 break;
377 case 41: /* Onboard Devices Extended Information */
378 dmi_save_extended_devices(dm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 }
380}
381
Tejun Heoc90fe6b2013-04-30 15:27:14 -0700382static int __init print_filtered(char *buf, size_t len, const char *info)
Bjorn Helgaas8881cdc2010-10-27 15:32:59 -0700383{
Tejun Heoc90fe6b2013-04-30 15:27:14 -0700384 int c = 0;
Bjorn Helgaas8881cdc2010-10-27 15:32:59 -0700385 const char *p;
386
387 if (!info)
Tejun Heoc90fe6b2013-04-30 15:27:14 -0700388 return c;
Bjorn Helgaas8881cdc2010-10-27 15:32:59 -0700389
390 for (p = info; *p; p++)
391 if (isprint(*p))
Tejun Heoc90fe6b2013-04-30 15:27:14 -0700392 c += scnprintf(buf + c, len - c, "%c", *p);
Bjorn Helgaas8881cdc2010-10-27 15:32:59 -0700393 else
Tejun Heoc90fe6b2013-04-30 15:27:14 -0700394 c += scnprintf(buf + c, len - c, "\\x%02x", *p & 0xff);
395 return c;
Bjorn Helgaas8881cdc2010-10-27 15:32:59 -0700396}
397
Tejun Heoc90fe6b2013-04-30 15:27:14 -0700398static void __init dmi_format_ids(char *buf, size_t len)
Bjorn Helgaas8881cdc2010-10-27 15:32:59 -0700399{
Tejun Heoc90fe6b2013-04-30 15:27:14 -0700400 int c = 0;
Naga Chumbalkar84e383b2011-02-14 22:47:17 +0000401 const char *board; /* Board Name is optional */
402
Tejun Heoc90fe6b2013-04-30 15:27:14 -0700403 c += print_filtered(buf + c, len - c,
404 dmi_get_system_info(DMI_SYS_VENDOR));
405 c += scnprintf(buf + c, len - c, " ");
406 c += print_filtered(buf + c, len - c,
407 dmi_get_system_info(DMI_PRODUCT_NAME));
408
Naga Chumbalkar84e383b2011-02-14 22:47:17 +0000409 board = dmi_get_system_info(DMI_BOARD_NAME);
410 if (board) {
Tejun Heoc90fe6b2013-04-30 15:27:14 -0700411 c += scnprintf(buf + c, len - c, "/");
412 c += print_filtered(buf + c, len - c, board);
Naga Chumbalkar84e383b2011-02-14 22:47:17 +0000413 }
Tejun Heoc90fe6b2013-04-30 15:27:14 -0700414 c += scnprintf(buf + c, len - c, ", BIOS ");
415 c += print_filtered(buf + c, len - c,
416 dmi_get_system_info(DMI_BIOS_VERSION));
417 c += scnprintf(buf + c, len - c, " ");
418 c += print_filtered(buf + c, len - c,
419 dmi_get_system_info(DMI_BIOS_DATE));
Bjorn Helgaas8881cdc2010-10-27 15:32:59 -0700420}
421
Ben Hutchings79bae422013-04-30 15:27:46 -0700422static int __init dmi_present(const u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423{
Ben Hutchings79bae422013-04-30 15:27:46 -0700424 int smbios_ver;
Jeff Garzik18552562007-10-03 15:15:40 -0400425
Ben Hutchings79bae422013-04-30 15:27:46 -0700426 if (memcmp(buf, "_SM_", 4) == 0 &&
427 buf[5] < 32 && dmi_checksum(buf, buf[5])) {
428 smbios_ver = (buf[6] << 8) + buf[7];
429
430 /* Some BIOS report weird SMBIOS version, fix that up */
431 switch (smbios_ver) {
432 case 0x021F:
433 case 0x0221:
434 pr_debug("SMBIOS version fixup(2.%d->2.%d)\n",
435 smbios_ver & 0xFF, 3);
436 smbios_ver = 0x0203;
437 break;
438 case 0x0233:
439 pr_debug("SMBIOS version fixup(2.%d->2.%d)\n", 51, 6);
440 smbios_ver = 0x0206;
441 break;
442 }
443 } else {
444 smbios_ver = 0;
445 }
446
447 buf += 16;
448
449 if (memcmp(buf, "_DMI_", 5) == 0 && dmi_checksum(buf, 15)) {
Jean Delvare7fce0842007-11-03 17:29:20 +0100450 dmi_num = (buf[13] << 8) | buf[12];
451 dmi_len = (buf[7] << 8) | buf[6];
452 dmi_base = (buf[11] << 24) | (buf[10] << 16) |
Matt Domsch3ed3bce2006-03-26 01:37:03 -0800453 (buf[9] << 8) | buf[8];
454
Bjorn Helgaas8881cdc2010-10-27 15:32:59 -0700455 if (dmi_walk_early(dmi_decode) == 0) {
Ben Hutchings79bae422013-04-30 15:27:46 -0700456 if (smbios_ver) {
457 dmi_ver = smbios_ver;
Zhenzhong Duan9f9c9cbb2012-12-20 15:05:14 -0800458 pr_info("SMBIOS %d.%d present.\n",
459 dmi_ver >> 8, dmi_ver & 0xFF);
Ben Hutchings79bae422013-04-30 15:27:46 -0700460 } else {
Zhenzhong Duan9f9c9cbb2012-12-20 15:05:14 -0800461 dmi_ver = (buf[14] & 0xF0) << 4 |
462 (buf[14] & 0x0F);
463 pr_info("Legacy DMI %d.%d present.\n",
464 dmi_ver >> 8, dmi_ver & 0xFF);
465 }
Tejun Heoc90fe6b2013-04-30 15:27:14 -0700466 dmi_format_ids(dmi_ids_string, sizeof(dmi_ids_string));
467 printk(KERN_DEBUG "DMI: %s\n", dmi_ids_string);
Matt Domsch3ed3bce2006-03-26 01:37:03 -0800468 return 0;
Bjorn Helgaas8881cdc2010-10-27 15:32:59 -0700469 }
Matt Domsch3ed3bce2006-03-26 01:37:03 -0800470 }
Matt Domsch3ed3bce2006-03-26 01:37:03 -0800471
Ben Hutchingsa40e7cf2013-03-08 12:43:32 -0800472 return 1;
Zhenzhong Duan9f9c9cbb2012-12-20 15:05:14 -0800473}
474
Matt Domsch3ed3bce2006-03-26 01:37:03 -0800475void __init dmi_scan_machine(void)
476{
Andrey Panin61e032fa2005-09-06 15:18:26 -0700477 char __iomem *p, *q;
Ben Hutchings79bae422013-04-30 15:27:46 -0700478 char buf[32];
Andrey Panin61e032fa2005-09-06 15:18:26 -0700479
Matt Fleming83e68182012-11-14 09:42:35 +0000480 if (efi_enabled(EFI_CONFIG_TABLES)) {
Bjorn Helgaasb2c99e32006-03-26 01:37:08 -0800481 if (efi.smbios == EFI_INVALID_TABLE_ADDR)
Ingo Molnar9a22b6e2008-09-18 12:50:18 +0200482 goto error;
Andrey Panin61e032fa2005-09-06 15:18:26 -0700483
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200484 /* This is called as a core_initcall() because it isn't
485 * needed during early boot. This also means we can
486 * iounmap the space when we're done with it.
487 */
Bjorn Helgaasb2c99e32006-03-26 01:37:08 -0800488 p = dmi_ioremap(efi.smbios, 32);
Matt Domsch3ed3bce2006-03-26 01:37:03 -0800489 if (p == NULL)
Ingo Molnar9a22b6e2008-09-18 12:50:18 +0200490 goto error;
Ben Hutchings79bae422013-04-30 15:27:46 -0700491 memcpy_fromio(buf, p, 32);
Tolentino, Matthew E23dd8422006-03-26 01:37:09 -0800492 dmi_iounmap(p, 32);
Ben Hutchings79bae422013-04-30 15:27:46 -0700493
494 if (!dmi_present(buf)) {
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200495 dmi_available = 1;
Ingo Molnar9a22b6e2008-09-18 12:50:18 +0200496 goto out;
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200497 }
Matt Domsch3ed3bce2006-03-26 01:37:03 -0800498 }
499 else {
500 /*
501 * no iounmap() for that ioremap(); it would be a no-op, but
502 * it's so early in setup that sucker gets confused into doing
503 * what it shouldn't if we actually call it.
504 */
505 p = dmi_ioremap(0xF0000, 0x10000);
506 if (p == NULL)
Ingo Molnar9a22b6e2008-09-18 12:50:18 +0200507 goto error;
Andrey Panin61e032fa2005-09-06 15:18:26 -0700508
Ben Hutchings79bae422013-04-30 15:27:46 -0700509 memset(buf, 0, 16);
Matt Domsch3ed3bce2006-03-26 01:37:03 -0800510 for (q = p; q < p + 0x10000; q += 16) {
Ben Hutchings79bae422013-04-30 15:27:46 -0700511 memcpy_fromio(buf + 16, q, 16);
512 if (!dmi_present(buf)) {
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200513 dmi_available = 1;
Ingo Molnar0d644842008-01-30 13:33:09 +0100514 dmi_iounmap(p, 0x10000);
Ingo Molnar9a22b6e2008-09-18 12:50:18 +0200515 goto out;
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200516 }
Ben Hutchings79bae422013-04-30 15:27:46 -0700517 memcpy(buf, buf + 16, 16);
Andrey Panin61e032fa2005-09-06 15:18:26 -0700518 }
Yinghai Lu3212bff2008-01-30 13:33:32 +0100519 dmi_iounmap(p, 0x10000);
Andrey Panin61e032fa2005-09-06 15:18:26 -0700520 }
Ingo Molnar9a22b6e2008-09-18 12:50:18 +0200521 error:
522 printk(KERN_INFO "DMI not present or invalid.\n");
523 out:
524 dmi_initialized = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525}
526
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527/**
Tejun Heo98e5e1b2013-04-30 15:27:15 -0700528 * dmi_set_dump_stack_arch_desc - set arch description for dump_stack()
529 *
530 * Invoke dump_stack_set_arch_desc() with DMI system information so that
531 * DMI identifiers are printed out on task dumps. Arch boot code should
532 * call this function after dmi_scan_machine() if it wants to print out DMI
533 * identifiers on task dumps.
534 */
535void __init dmi_set_dump_stack_arch_desc(void)
536{
537 dump_stack_set_arch_desc("%s", dmi_ids_string);
538}
539
540/**
Rafael J. Wysockid7b19562009-01-19 20:55:50 +0100541 * dmi_matches - check if dmi_system_id structure matches system DMI data
542 * @dmi: pointer to the dmi_system_id structure to check
543 */
544static bool dmi_matches(const struct dmi_system_id *dmi)
545{
546 int i;
547
548 WARN(!dmi_initialized, KERN_ERR "dmi check: not initialized yet.\n");
549
550 for (i = 0; i < ARRAY_SIZE(dmi->matches); i++) {
551 int s = dmi->matches[i].slot;
552 if (s == DMI_NONE)
Dmitry Torokhov75757502009-12-04 10:24:19 -0800553 break;
Jani Nikula5017b282013-07-03 15:05:02 -0700554 if (dmi_ident[s]) {
555 if (!dmi->matches[i].exact_match &&
556 strstr(dmi_ident[s], dmi->matches[i].substr))
557 continue;
558 else if (dmi->matches[i].exact_match &&
559 !strcmp(dmi_ident[s], dmi->matches[i].substr))
560 continue;
561 }
562
Rafael J. Wysockid7b19562009-01-19 20:55:50 +0100563 /* No match */
564 return false;
565 }
566 return true;
567}
568
569/**
Dmitry Torokhov75757502009-12-04 10:24:19 -0800570 * dmi_is_end_of_table - check for end-of-table marker
571 * @dmi: pointer to the dmi_system_id structure to check
572 */
573static bool dmi_is_end_of_table(const struct dmi_system_id *dmi)
574{
575 return dmi->matches[0].slot == DMI_NONE;
576}
577
578/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 * dmi_check_system - check system DMI data
580 * @list: array of dmi_system_id structures to match against
Randy Dunlapb0ef3712006-06-25 05:49:18 -0700581 * All non-null elements of the list must match
582 * their slot's (field index's) data (i.e., each
583 * list string must be a substring of the specified
584 * DMI slot's string data) to be considered a
585 * successful match.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 *
587 * Walk the blacklist table running matching functions until someone
588 * returns non zero or we hit the end. Callback function is called for
Randy Dunlapb0ef3712006-06-25 05:49:18 -0700589 * each successful match. Returns the number of matches.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 */
Jeff Garzik18552562007-10-03 15:15:40 -0400591int dmi_check_system(const struct dmi_system_id *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592{
Rafael J. Wysockid7b19562009-01-19 20:55:50 +0100593 int count = 0;
594 const struct dmi_system_id *d;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
Dmitry Torokhov75757502009-12-04 10:24:19 -0800596 for (d = list; !dmi_is_end_of_table(d); d++)
Rafael J. Wysockid7b19562009-01-19 20:55:50 +0100597 if (dmi_matches(d)) {
598 count++;
599 if (d->callback && d->callback(d))
600 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
603 return count;
604}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605EXPORT_SYMBOL(dmi_check_system);
606
607/**
Rafael J. Wysockid7b19562009-01-19 20:55:50 +0100608 * dmi_first_match - find dmi_system_id structure matching system DMI data
609 * @list: array of dmi_system_id structures to match against
610 * All non-null elements of the list must match
611 * their slot's (field index's) data (i.e., each
612 * list string must be a substring of the specified
613 * DMI slot's string data) to be considered a
614 * successful match.
615 *
616 * Walk the blacklist table until the first match is found. Return the
617 * pointer to the matching entry or NULL if there's no match.
618 */
619const struct dmi_system_id *dmi_first_match(const struct dmi_system_id *list)
620{
621 const struct dmi_system_id *d;
622
Dmitry Torokhov75757502009-12-04 10:24:19 -0800623 for (d = list; !dmi_is_end_of_table(d); d++)
Rafael J. Wysockid7b19562009-01-19 20:55:50 +0100624 if (dmi_matches(d))
625 return d;
626
627 return NULL;
628}
629EXPORT_SYMBOL(dmi_first_match);
630
631/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 * dmi_get_system_info - return DMI data value
Randy Dunlapb0ef3712006-06-25 05:49:18 -0700633 * @field: data index (see enum dmi_field)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 *
635 * Returns one DMI data value, can be used to perform
636 * complex DMI data checks.
637 */
Jeff Garzik18552562007-10-03 15:15:40 -0400638const char *dmi_get_system_info(int field)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639{
640 return dmi_ident[field];
641}
Dmitry Torokhove70c9d52005-06-25 14:54:25 -0700642EXPORT_SYMBOL(dmi_get_system_info);
Andrey Paninebad6a42005-09-06 15:18:29 -0700643
Alok Katariafd8cd7e2008-11-03 15:50:38 -0800644/**
Randy Dunlapc2bacfc2009-01-06 14:41:40 -0800645 * dmi_name_in_serial - Check if string is in the DMI product serial information
646 * @str: string to check for
Alok Katariafd8cd7e2008-11-03 15:50:38 -0800647 */
648int dmi_name_in_serial(const char *str)
649{
650 int f = DMI_PRODUCT_SERIAL;
651 if (dmi_ident[f] && strstr(dmi_ident[f], str))
652 return 1;
653 return 0;
654}
Andi Kleena1bae672006-10-21 18:37:02 +0200655
656/**
Jean Delvare66e13e62011-11-15 14:36:09 -0800657 * dmi_name_in_vendors - Check if string is in the DMI system or board vendor name
Andi Kleena1bae672006-10-21 18:37:02 +0200658 * @str: Case sensitive Name
659 */
Jeff Garzik18552562007-10-03 15:15:40 -0400660int dmi_name_in_vendors(const char *str)
Andi Kleena1bae672006-10-21 18:37:02 +0200661{
Jean Delvare66e13e62011-11-15 14:36:09 -0800662 static int fields[] = { DMI_SYS_VENDOR, DMI_BOARD_VENDOR, DMI_NONE };
Andi Kleena1bae672006-10-21 18:37:02 +0200663 int i;
664 for (i = 0; fields[i] != DMI_NONE; i++) {
665 int f = fields[i];
666 if (dmi_ident[f] && strstr(dmi_ident[f], str))
667 return 1;
668 }
669 return 0;
670}
671EXPORT_SYMBOL(dmi_name_in_vendors);
672
Andrey Paninebad6a42005-09-06 15:18:29 -0700673/**
674 * dmi_find_device - find onboard device by type/name
675 * @type: device type or %DMI_DEV_TYPE_ANY to match all device types
Randy Dunlapb0ef3712006-06-25 05:49:18 -0700676 * @name: device name string or %NULL to match all
Andrey Paninebad6a42005-09-06 15:18:29 -0700677 * @from: previous device found in search, or %NULL for new search.
678 *
679 * Iterates through the list of known onboard devices. If a device is
680 * found with a matching @vendor and @device, a pointer to its device
681 * structure is returned. Otherwise, %NULL is returned.
Randy Dunlapb0ef3712006-06-25 05:49:18 -0700682 * A new search is initiated by passing %NULL as the @from argument.
Andrey Paninebad6a42005-09-06 15:18:29 -0700683 * If @from is not %NULL, searches continue from next device.
684 */
Jeff Garzik18552562007-10-03 15:15:40 -0400685const struct dmi_device * dmi_find_device(int type, const char *name,
686 const struct dmi_device *from)
Andrey Paninebad6a42005-09-06 15:18:29 -0700687{
Jeff Garzik18552562007-10-03 15:15:40 -0400688 const struct list_head *head = from ? &from->list : &dmi_devices;
689 struct list_head *d;
Andrey Paninebad6a42005-09-06 15:18:29 -0700690
691 for(d = head->next; d != &dmi_devices; d = d->next) {
Jeff Garzik18552562007-10-03 15:15:40 -0400692 const struct dmi_device *dev =
693 list_entry(d, struct dmi_device, list);
Andrey Paninebad6a42005-09-06 15:18:29 -0700694
695 if (((type == DMI_DEV_TYPE_ANY) || (dev->type == type)) &&
696 ((name == NULL) || (strcmp(dev->name, name) == 0)))
697 return dev;
698 }
699
700 return NULL;
701}
702EXPORT_SYMBOL(dmi_find_device);
Andi Kleenf083a322006-03-25 16:30:19 +0100703
704/**
Tejun Heo3e5cd1f2009-08-16 21:02:36 +0900705 * dmi_get_date - parse a DMI date
706 * @field: data index (see enum dmi_field)
707 * @yearp: optional out parameter for the year
708 * @monthp: optional out parameter for the month
709 * @dayp: optional out parameter for the day
Andi Kleenf083a322006-03-25 16:30:19 +0100710 *
Tejun Heo3e5cd1f2009-08-16 21:02:36 +0900711 * The date field is assumed to be in the form resembling
712 * [mm[/dd]]/yy[yy] and the result is stored in the out
713 * parameters any or all of which can be omitted.
714 *
715 * If the field doesn't exist, all out parameters are set to zero
716 * and false is returned. Otherwise, true is returned with any
717 * invalid part of date set to zero.
718 *
719 * On return, year, month and day are guaranteed to be in the
720 * range of [0,9999], [0,12] and [0,31] respectively.
Andi Kleenf083a322006-03-25 16:30:19 +0100721 */
Tejun Heo3e5cd1f2009-08-16 21:02:36 +0900722bool dmi_get_date(int field, int *yearp, int *monthp, int *dayp)
Andi Kleenf083a322006-03-25 16:30:19 +0100723{
Tejun Heo3e5cd1f2009-08-16 21:02:36 +0900724 int year = 0, month = 0, day = 0;
725 bool exists;
726 const char *s, *y;
Tejun Heo02c24fa2009-08-16 21:01:22 +0900727 char *e;
Andi Kleenf083a322006-03-25 16:30:19 +0100728
Tejun Heo3e5cd1f2009-08-16 21:02:36 +0900729 s = dmi_get_system_info(field);
730 exists = s;
731 if (!exists)
732 goto out;
Andi Kleenf083a322006-03-25 16:30:19 +0100733
Tejun Heo3e5cd1f2009-08-16 21:02:36 +0900734 /*
735 * Determine year first. We assume the date string resembles
736 * mm/dd/yy[yy] but the original code extracted only the year
737 * from the end. Keep the behavior in the spirit of no
738 * surprises.
739 */
740 y = strrchr(s, '/');
741 if (!y)
742 goto out;
743
744 y++;
745 year = simple_strtoul(y, &e, 10);
746 if (y != e && year < 100) { /* 2-digit year */
Andi Kleenf083a322006-03-25 16:30:19 +0100747 year += 1900;
748 if (year < 1996) /* no dates < spec 1.0 */
749 year += 100;
750 }
Tejun Heo3e5cd1f2009-08-16 21:02:36 +0900751 if (year > 9999) /* year should fit in %04d */
752 year = 0;
Andi Kleenf083a322006-03-25 16:30:19 +0100753
Tejun Heo3e5cd1f2009-08-16 21:02:36 +0900754 /* parse the mm and dd */
755 month = simple_strtoul(s, &e, 10);
756 if (s == e || *e != '/' || !month || month > 12) {
757 month = 0;
758 goto out;
759 }
760
761 s = e + 1;
762 day = simple_strtoul(s, &e, 10);
763 if (s == y || s == e || *e != '/' || day > 31)
764 day = 0;
765out:
766 if (yearp)
767 *yearp = year;
768 if (monthp)
769 *monthp = month;
770 if (dayp)
771 *dayp = day;
772 return exists;
Andi Kleenf083a322006-03-25 16:30:19 +0100773}
Tejun Heo3e5cd1f2009-08-16 21:02:36 +0900774EXPORT_SYMBOL(dmi_get_date);
Jean Delvare7fce0842007-11-03 17:29:20 +0100775
776/**
777 * dmi_walk - Walk the DMI table and get called back for every record
778 * @decode: Callback function
Jean Delvaree7a19c562009-03-30 21:46:44 +0200779 * @private_data: Private data to be passed to the callback function
Jean Delvare7fce0842007-11-03 17:29:20 +0100780 *
781 * Returns -1 when the DMI table can't be reached, 0 on success.
782 */
Jean Delvaree7a19c562009-03-30 21:46:44 +0200783int dmi_walk(void (*decode)(const struct dmi_header *, void *),
784 void *private_data)
Jean Delvare7fce0842007-11-03 17:29:20 +0100785{
786 u8 *buf;
787
788 if (!dmi_available)
789 return -1;
790
791 buf = ioremap(dmi_base, dmi_len);
792 if (buf == NULL)
793 return -1;
794
Jean Delvaree7a19c562009-03-30 21:46:44 +0200795 dmi_table(buf, dmi_len, dmi_num, decode, private_data);
Jean Delvare7fce0842007-11-03 17:29:20 +0100796
797 iounmap(buf);
798 return 0;
799}
800EXPORT_SYMBOL_GPL(dmi_walk);
Jiri Slabyd61c72e2008-12-10 14:07:21 +0100801
802/**
803 * dmi_match - compare a string to the dmi field (if exists)
Randy Dunlapc2bacfc2009-01-06 14:41:40 -0800804 * @f: DMI field identifier
805 * @str: string to compare the DMI field to
Jiri Slabyd61c72e2008-12-10 14:07:21 +0100806 *
807 * Returns true if the requested field equals to the str (including NULL).
808 */
809bool dmi_match(enum dmi_field f, const char *str)
810{
811 const char *info = dmi_get_system_info(f);
812
813 if (info == NULL || str == NULL)
814 return info == str;
815
816 return !strcmp(info, str);
817}
818EXPORT_SYMBOL_GPL(dmi_match);