Commit | Line | Data |
---|---|---|
1da177e4 | 1 | #include <linux/types.h> |
1da177e4 LT |
2 | #include <linux/string.h> |
3 | #include <linux/init.h> | |
4 | #include <linux/module.h> | |
8881cdce | 5 | #include <linux/ctype.h> |
1da177e4 | 6 | #include <linux/dmi.h> |
3ed3bce8 | 7 | #include <linux/efi.h> |
1da177e4 | 8 | #include <linux/bootmem.h> |
d114a333 | 9 | #include <linux/random.h> |
f2d3efed | 10 | #include <asm/dmi.h> |
1da177e4 | 11 | |
cb5dd7c1 PJ |
12 | /* |
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 | */ | |
79da4721 PW |
17 | static char dmi_empty_string[] = " "; |
18 | ||
f1d8e614 | 19 | static u16 __initdata dmi_ver; |
9a22b6e7 IM |
20 | /* |
21 | * Catch too early calls to dmi_check_system(): | |
22 | */ | |
23 | static int dmi_initialized; | |
24 | ||
f3069ae9 | 25 | static const char * __init dmi_string_nosave(const struct dmi_header *dm, u8 s) |
1da177e4 | 26 | { |
1855256c | 27 | const u8 *bp = ((u8 *) dm) + dm->length; |
1249c513 | 28 | |
c3c7120d | 29 | if (s) { |
1da177e4 | 30 | s--; |
c3c7120d AP |
31 | while (s > 0 && *bp) { |
32 | bp += strlen(bp) + 1; | |
33 | s--; | |
34 | } | |
35 | ||
36 | if (*bp != 0) { | |
79da4721 PW |
37 | size_t len = strlen(bp)+1; |
38 | size_t cmp_len = len > 8 ? 8 : len; | |
39 | ||
40 | if (!memcmp(bp, dmi_empty_string, cmp_len)) | |
41 | return dmi_empty_string; | |
f3069ae9 | 42 | return bp; |
c3c7120d | 43 | } |
4f705ae3 | 44 | } |
c3c7120d | 45 | |
f3069ae9 JD |
46 | return ""; |
47 | } | |
48 | ||
49 | static char * __init dmi_string(const struct dmi_header *dm, u8 s) | |
50 | { | |
51 | const char *bp = dmi_string_nosave(dm, s); | |
52 | char *str; | |
53 | size_t len; | |
54 | ||
55 | if (bp == dmi_empty_string) | |
56 | return dmi_empty_string; | |
57 | ||
58 | len = strlen(bp) + 1; | |
59 | str = dmi_alloc(len); | |
60 | if (str != NULL) | |
61 | strcpy(str, bp); | |
62 | else | |
63 | printk(KERN_ERR "dmi_string: cannot allocate %Zu bytes.\n", len); | |
64 | ||
c3c7120d | 65 | return str; |
1da177e4 LT |
66 | } |
67 | ||
68 | /* | |
69 | * We have to be cautious here. We have seen BIOSes with DMI pointers | |
70 | * pointing to completely the wrong place for example | |
71 | */ | |
7fce084a | 72 | static void dmi_table(u8 *buf, int len, int num, |
e7a19c56 JD |
73 | void (*decode)(const struct dmi_header *, void *), |
74 | void *private_data) | |
1da177e4 | 75 | { |
7fce084a | 76 | u8 *data = buf; |
1249c513 | 77 | int i = 0; |
4f705ae3 | 78 | |
1da177e4 | 79 | /* |
4f705ae3 BH |
80 | * Stop when we see all the items the table claimed to have |
81 | * OR we run off the end of the table (also happens) | |
82 | */ | |
1249c513 | 83 | while ((i < num) && (data - buf + sizeof(struct dmi_header)) <= len) { |
1855256c JG |
84 | const struct dmi_header *dm = (const struct dmi_header *)data; |
85 | ||
1da177e4 | 86 | /* |
8638545c AC |
87 | * We want to know the total length (formatted area and |
88 | * strings) before decoding to make sure we won't run off the | |
89 | * table in dmi_decode or dmi_string | |
1da177e4 | 90 | */ |
1249c513 AP |
91 | data += dm->length; |
92 | while ((data - buf < len - 1) && (data[0] || data[1])) | |
1da177e4 | 93 | data++; |
1249c513 | 94 | if (data - buf < len - 1) |
e7a19c56 | 95 | decode(dm, private_data); |
1249c513 | 96 | data += 2; |
1da177e4 LT |
97 | i++; |
98 | } | |
7fce084a JD |
99 | } |
100 | ||
101 | static u32 dmi_base; | |
102 | static u16 dmi_len; | |
103 | static u16 dmi_num; | |
104 | ||
e7a19c56 JD |
105 | static int __init dmi_walk_early(void (*decode)(const struct dmi_header *, |
106 | void *)) | |
7fce084a JD |
107 | { |
108 | u8 *buf; | |
109 | ||
110 | buf = dmi_ioremap(dmi_base, dmi_len); | |
111 | if (buf == NULL) | |
112 | return -1; | |
113 | ||
e7a19c56 | 114 | dmi_table(buf, dmi_len, dmi_num, decode, NULL); |
7fce084a | 115 | |
d114a333 TL |
116 | add_device_randomness(buf, dmi_len); |
117 | ||
7fce084a | 118 | dmi_iounmap(buf, dmi_len); |
1da177e4 LT |
119 | return 0; |
120 | } | |
121 | ||
9f9c9cbb | 122 | static int __init dmi_checksum(const u8 *buf, u8 len) |
1da177e4 | 123 | { |
1249c513 | 124 | u8 sum = 0; |
1da177e4 | 125 | int a; |
4f705ae3 | 126 | |
9f9c9cbb | 127 | for (a = 0; a < len; a++) |
1249c513 AP |
128 | sum += buf[a]; |
129 | ||
130 | return sum == 0; | |
1da177e4 LT |
131 | } |
132 | ||
1da177e4 | 133 | static char *dmi_ident[DMI_STRING_MAX]; |
ebad6a42 | 134 | static LIST_HEAD(dmi_devices); |
4f5c791a | 135 | int dmi_available; |
1da177e4 LT |
136 | |
137 | /* | |
138 | * Save a DMI string | |
139 | */ | |
1855256c | 140 | static void __init dmi_save_ident(const struct dmi_header *dm, int slot, int string) |
1da177e4 | 141 | { |
1855256c JG |
142 | const char *d = (const char*) dm; |
143 | char *p; | |
1249c513 | 144 | |
1da177e4 LT |
145 | if (dmi_ident[slot]) |
146 | return; | |
1249c513 | 147 | |
c3c7120d AP |
148 | p = dmi_string(dm, d[string]); |
149 | if (p == NULL) | |
150 | return; | |
151 | ||
152 | dmi_ident[slot] = p; | |
1da177e4 LT |
153 | } |
154 | ||
1855256c | 155 | static void __init dmi_save_uuid(const struct dmi_header *dm, int slot, int index) |
4f5c791a | 156 | { |
1855256c | 157 | const u8 *d = (u8*) dm + index; |
4f5c791a LP |
158 | char *s; |
159 | int is_ff = 1, is_00 = 1, i; | |
160 | ||
161 | if (dmi_ident[slot]) | |
162 | return; | |
163 | ||
164 | for (i = 0; i < 16 && (is_ff || is_00); i++) { | |
f1d8e614 ZD |
165 | if (d[i] != 0x00) |
166 | is_00 = 0; | |
167 | if (d[i] != 0xFF) | |
168 | is_ff = 0; | |
4f5c791a LP |
169 | } |
170 | ||
171 | if (is_ff || is_00) | |
172 | return; | |
173 | ||
174 | s = dmi_alloc(16*2+4+1); | |
175 | if (!s) | |
176 | return; | |
177 | ||
f1d8e614 ZD |
178 | /* |
179 | * As of version 2.6 of the SMBIOS specification, the first 3 fields of | |
180 | * the UUID are supposed to be little-endian encoded. The specification | |
181 | * says that this is the defacto standard. | |
182 | */ | |
183 | if (dmi_ver >= 0x0206) | |
184 | sprintf(s, "%pUL", d); | |
185 | else | |
186 | sprintf(s, "%pUB", d); | |
4f5c791a LP |
187 | |
188 | dmi_ident[slot] = s; | |
189 | } | |
190 | ||
1855256c | 191 | static void __init dmi_save_type(const struct dmi_header *dm, int slot, int index) |
4f5c791a | 192 | { |
1855256c | 193 | const u8 *d = (u8*) dm + index; |
4f5c791a LP |
194 | char *s; |
195 | ||
196 | if (dmi_ident[slot]) | |
197 | return; | |
198 | ||
199 | s = dmi_alloc(4); | |
200 | if (!s) | |
201 | return; | |
202 | ||
203 | sprintf(s, "%u", *d & 0x7F); | |
204 | dmi_ident[slot] = s; | |
205 | } | |
206 | ||
f3069ae9 JD |
207 | static void __init dmi_save_one_device(int type, const char *name) |
208 | { | |
209 | struct dmi_device *dev; | |
210 | ||
211 | /* No duplicate device */ | |
212 | if (dmi_find_device(type, name, NULL)) | |
213 | return; | |
214 | ||
215 | dev = dmi_alloc(sizeof(*dev) + strlen(name) + 1); | |
216 | if (!dev) { | |
217 | printk(KERN_ERR "dmi_save_one_device: out of memory.\n"); | |
218 | return; | |
219 | } | |
220 | ||
221 | dev->type = type; | |
222 | strcpy((char *)(dev + 1), name); | |
223 | dev->name = (char *)(dev + 1); | |
224 | dev->device_data = NULL; | |
225 | list_add(&dev->list, &dmi_devices); | |
226 | } | |
227 | ||
1855256c | 228 | static void __init dmi_save_devices(const struct dmi_header *dm) |
ebad6a42 AP |
229 | { |
230 | int i, count = (dm->length - sizeof(struct dmi_header)) / 2; | |
ebad6a42 AP |
231 | |
232 | for (i = 0; i < count; i++) { | |
1855256c | 233 | const char *d = (char *)(dm + 1) + (i * 2); |
ebad6a42 AP |
234 | |
235 | /* Skip disabled device */ | |
236 | if ((*d & 0x80) == 0) | |
237 | continue; | |
238 | ||
f3069ae9 | 239 | dmi_save_one_device(*d & 0x7f, dmi_string_nosave(dm, *(d + 1))); |
2e0c1f6c SM |
240 | } |
241 | } | |
242 | ||
1855256c | 243 | static void __init dmi_save_oem_strings_devices(const struct dmi_header *dm) |
2e0c1f6c SM |
244 | { |
245 | int i, count = *(u8 *)(dm + 1); | |
246 | struct dmi_device *dev; | |
247 | ||
248 | for (i = 1; i <= count; i++) { | |
79da4721 PW |
249 | char *devname = dmi_string(dm, i); |
250 | ||
43fe105a | 251 | if (devname == dmi_empty_string) |
79da4721 | 252 | continue; |
79da4721 | 253 | |
2e0c1f6c SM |
254 | dev = dmi_alloc(sizeof(*dev)); |
255 | if (!dev) { | |
256 | printk(KERN_ERR | |
257 | "dmi_save_oem_strings_devices: out of memory.\n"); | |
258 | break; | |
259 | } | |
260 | ||
261 | dev->type = DMI_DEV_TYPE_OEM_STRING; | |
79da4721 | 262 | dev->name = devname; |
2e0c1f6c | 263 | dev->device_data = NULL; |
ebad6a42 AP |
264 | |
265 | list_add(&dev->list, &dmi_devices); | |
266 | } | |
267 | } | |
268 | ||
1855256c | 269 | static void __init dmi_save_ipmi_device(const struct dmi_header *dm) |
ebad6a42 AP |
270 | { |
271 | struct dmi_device *dev; | |
272 | void * data; | |
273 | ||
e9928674 | 274 | data = dmi_alloc(dm->length); |
ebad6a42 AP |
275 | if (data == NULL) { |
276 | printk(KERN_ERR "dmi_save_ipmi_device: out of memory.\n"); | |
277 | return; | |
278 | } | |
279 | ||
280 | memcpy(data, dm, dm->length); | |
281 | ||
e9928674 | 282 | dev = dmi_alloc(sizeof(*dev)); |
ebad6a42 AP |
283 | if (!dev) { |
284 | printk(KERN_ERR "dmi_save_ipmi_device: out of memory.\n"); | |
285 | return; | |
286 | } | |
287 | ||
288 | dev->type = DMI_DEV_TYPE_IPMI; | |
289 | dev->name = "IPMI controller"; | |
290 | dev->device_data = data; | |
291 | ||
abd24df8 | 292 | list_add_tail(&dev->list, &dmi_devices); |
ebad6a42 AP |
293 | } |
294 | ||
911e1c9b N |
295 | static void __init dmi_save_dev_onboard(int instance, int segment, int bus, |
296 | int devfn, const char *name) | |
297 | { | |
298 | struct dmi_dev_onboard *onboard_dev; | |
299 | ||
300 | onboard_dev = dmi_alloc(sizeof(*onboard_dev) + strlen(name) + 1); | |
301 | if (!onboard_dev) { | |
302 | printk(KERN_ERR "dmi_save_dev_onboard: out of memory.\n"); | |
303 | return; | |
304 | } | |
305 | onboard_dev->instance = instance; | |
306 | onboard_dev->segment = segment; | |
307 | onboard_dev->bus = bus; | |
308 | onboard_dev->devfn = devfn; | |
309 | ||
310 | strcpy((char *)&onboard_dev[1], name); | |
311 | onboard_dev->dev.type = DMI_DEV_TYPE_DEV_ONBOARD; | |
312 | onboard_dev->dev.name = (char *)&onboard_dev[1]; | |
313 | onboard_dev->dev.device_data = onboard_dev; | |
314 | ||
315 | list_add(&onboard_dev->dev.list, &dmi_devices); | |
316 | } | |
317 | ||
b4bd7d59 WVS |
318 | static void __init dmi_save_extended_devices(const struct dmi_header *dm) |
319 | { | |
320 | const u8 *d = (u8*) dm + 5; | |
b4bd7d59 WVS |
321 | |
322 | /* Skip disabled device */ | |
323 | if ((*d & 0x80) == 0) | |
324 | return; | |
325 | ||
911e1c9b N |
326 | dmi_save_dev_onboard(*(d+1), *(u16 *)(d+2), *(d+4), *(d+5), |
327 | dmi_string_nosave(dm, *(d-1))); | |
f3069ae9 | 328 | dmi_save_one_device(*d & 0x7f, dmi_string_nosave(dm, *(d - 1))); |
b4bd7d59 WVS |
329 | } |
330 | ||
1da177e4 LT |
331 | /* |
332 | * Process a DMI table entry. Right now all we care about are the BIOS | |
333 | * and machine entries. For 2.5 we should pull the smbus controller info | |
334 | * out of here. | |
335 | */ | |
e7a19c56 | 336 | static void __init dmi_decode(const struct dmi_header *dm, void *dummy) |
1da177e4 | 337 | { |
1249c513 | 338 | switch(dm->type) { |
ebad6a42 | 339 | case 0: /* BIOS Information */ |
1249c513 | 340 | dmi_save_ident(dm, DMI_BIOS_VENDOR, 4); |
1249c513 | 341 | dmi_save_ident(dm, DMI_BIOS_VERSION, 5); |
1249c513 AP |
342 | dmi_save_ident(dm, DMI_BIOS_DATE, 8); |
343 | break; | |
ebad6a42 | 344 | case 1: /* System Information */ |
1249c513 | 345 | dmi_save_ident(dm, DMI_SYS_VENDOR, 4); |
1249c513 | 346 | dmi_save_ident(dm, DMI_PRODUCT_NAME, 5); |
1249c513 | 347 | dmi_save_ident(dm, DMI_PRODUCT_VERSION, 6); |
1249c513 | 348 | dmi_save_ident(dm, DMI_PRODUCT_SERIAL, 7); |
4f5c791a | 349 | dmi_save_uuid(dm, DMI_PRODUCT_UUID, 8); |
1249c513 | 350 | break; |
ebad6a42 | 351 | case 2: /* Base Board Information */ |
1249c513 | 352 | dmi_save_ident(dm, DMI_BOARD_VENDOR, 4); |
1249c513 | 353 | dmi_save_ident(dm, DMI_BOARD_NAME, 5); |
1249c513 | 354 | dmi_save_ident(dm, DMI_BOARD_VERSION, 6); |
4f5c791a LP |
355 | dmi_save_ident(dm, DMI_BOARD_SERIAL, 7); |
356 | dmi_save_ident(dm, DMI_BOARD_ASSET_TAG, 8); | |
357 | break; | |
358 | case 3: /* Chassis Information */ | |
359 | dmi_save_ident(dm, DMI_CHASSIS_VENDOR, 4); | |
360 | dmi_save_type(dm, DMI_CHASSIS_TYPE, 5); | |
361 | dmi_save_ident(dm, DMI_CHASSIS_VERSION, 6); | |
362 | dmi_save_ident(dm, DMI_CHASSIS_SERIAL, 7); | |
363 | dmi_save_ident(dm, DMI_CHASSIS_ASSET_TAG, 8); | |
1249c513 | 364 | break; |
ebad6a42 AP |
365 | case 10: /* Onboard Devices Information */ |
366 | dmi_save_devices(dm); | |
367 | break; | |
2e0c1f6c SM |
368 | case 11: /* OEM Strings */ |
369 | dmi_save_oem_strings_devices(dm); | |
370 | break; | |
ebad6a42 AP |
371 | case 38: /* IPMI Device Information */ |
372 | dmi_save_ipmi_device(dm); | |
b4bd7d59 WVS |
373 | break; |
374 | case 41: /* Onboard Devices Extended Information */ | |
375 | dmi_save_extended_devices(dm); | |
1da177e4 LT |
376 | } |
377 | } | |
378 | ||
8881cdce BH |
379 | static void __init print_filtered(const char *info) |
380 | { | |
381 | const char *p; | |
382 | ||
383 | if (!info) | |
384 | return; | |
385 | ||
386 | for (p = info; *p; p++) | |
387 | if (isprint(*p)) | |
388 | printk(KERN_CONT "%c", *p); | |
389 | else | |
390 | printk(KERN_CONT "\\x%02x", *p & 0xff); | |
391 | } | |
392 | ||
393 | static void __init dmi_dump_ids(void) | |
394 | { | |
84e383b3 NC |
395 | const char *board; /* Board Name is optional */ |
396 | ||
8881cdce | 397 | printk(KERN_DEBUG "DMI: "); |
84e383b3 NC |
398 | print_filtered(dmi_get_system_info(DMI_SYS_VENDOR)); |
399 | printk(KERN_CONT " "); | |
8881cdce | 400 | print_filtered(dmi_get_system_info(DMI_PRODUCT_NAME)); |
84e383b3 NC |
401 | board = dmi_get_system_info(DMI_BOARD_NAME); |
402 | if (board) { | |
403 | printk(KERN_CONT "/"); | |
404 | print_filtered(board); | |
405 | } | |
8881cdce BH |
406 | printk(KERN_CONT ", BIOS "); |
407 | print_filtered(dmi_get_system_info(DMI_BIOS_VERSION)); | |
408 | printk(KERN_CONT " "); | |
409 | print_filtered(dmi_get_system_info(DMI_BIOS_DATE)); | |
410 | printk(KERN_CONT "\n"); | |
411 | } | |
412 | ||
1855256c | 413 | static int __init dmi_present(const char __iomem *p) |
1da177e4 | 414 | { |
61e032fa | 415 | u8 buf[15]; |
1855256c | 416 | |
3ed3bce8 | 417 | memcpy_fromio(buf, p, 15); |
9f9c9cbb | 418 | if (dmi_checksum(buf, 15)) { |
7fce084a JD |
419 | dmi_num = (buf[13] << 8) | buf[12]; |
420 | dmi_len = (buf[7] << 8) | buf[6]; | |
421 | dmi_base = (buf[11] << 24) | (buf[10] << 16) | | |
3ed3bce8 | 422 | (buf[9] << 8) | buf[8]; |
61e032fa | 423 | |
8881cdce | 424 | if (dmi_walk_early(dmi_decode) == 0) { |
9f9c9cbb ZD |
425 | if (dmi_ver) |
426 | pr_info("SMBIOS %d.%d present.\n", | |
427 | dmi_ver >> 8, dmi_ver & 0xFF); | |
428 | else { | |
429 | dmi_ver = (buf[14] & 0xF0) << 4 | | |
430 | (buf[14] & 0x0F); | |
431 | pr_info("Legacy DMI %d.%d present.\n", | |
432 | dmi_ver >> 8, dmi_ver & 0xFF); | |
433 | } | |
8881cdce | 434 | dmi_dump_ids(); |
3ed3bce8 | 435 | return 0; |
8881cdce | 436 | } |
3ed3bce8 | 437 | } |
9f9c9cbb | 438 | dmi_ver = 0; |
3ed3bce8 MD |
439 | return 1; |
440 | } | |
61e032fa | 441 | |
9f9c9cbb ZD |
442 | static int __init smbios_present(const char __iomem *p) |
443 | { | |
444 | u8 buf[32]; | |
445 | int offset = 0; | |
446 | ||
447 | memcpy_fromio(buf, p, 32); | |
448 | if ((buf[5] < 32) && dmi_checksum(buf, buf[5])) { | |
449 | dmi_ver = (buf[6] << 8) + buf[7]; | |
450 | ||
451 | /* Some BIOS report weird SMBIOS version, fix that up */ | |
452 | switch (dmi_ver) { | |
453 | case 0x021F: | |
454 | case 0x0221: | |
455 | pr_debug("SMBIOS version fixup(2.%d->2.%d)\n", | |
456 | dmi_ver & 0xFF, 3); | |
457 | dmi_ver = 0x0203; | |
458 | break; | |
459 | case 0x0233: | |
460 | pr_debug("SMBIOS version fixup(2.%d->2.%d)\n", 51, 6); | |
461 | dmi_ver = 0x0206; | |
462 | break; | |
463 | } | |
464 | offset = 16; | |
465 | } | |
466 | return dmi_present(buf + offset); | |
467 | } | |
468 | ||
3ed3bce8 MD |
469 | void __init dmi_scan_machine(void) |
470 | { | |
471 | char __iomem *p, *q; | |
472 | int rc; | |
473 | ||
474 | if (efi_enabled) { | |
b2c99e3c | 475 | if (efi.smbios == EFI_INVALID_TABLE_ADDR) |
9a22b6e7 | 476 | goto error; |
3ed3bce8 | 477 | |
4f5c791a LP |
478 | /* This is called as a core_initcall() because it isn't |
479 | * needed during early boot. This also means we can | |
480 | * iounmap the space when we're done with it. | |
481 | */ | |
b2c99e3c | 482 | p = dmi_ioremap(efi.smbios, 32); |
3ed3bce8 | 483 | if (p == NULL) |
9a22b6e7 | 484 | goto error; |
3ed3bce8 | 485 | |
9f9c9cbb | 486 | rc = smbios_present(p); |
23dd842c | 487 | dmi_iounmap(p, 32); |
4f5c791a LP |
488 | if (!rc) { |
489 | dmi_available = 1; | |
9a22b6e7 | 490 | goto out; |
4f5c791a | 491 | } |
3ed3bce8 MD |
492 | } |
493 | else { | |
494 | /* | |
495 | * no iounmap() for that ioremap(); it would be a no-op, but | |
496 | * it's so early in setup that sucker gets confused into doing | |
497 | * what it shouldn't if we actually call it. | |
498 | */ | |
499 | p = dmi_ioremap(0xF0000, 0x10000); | |
500 | if (p == NULL) | |
9a22b6e7 | 501 | goto error; |
3ed3bce8 MD |
502 | |
503 | for (q = p; q < p + 0x10000; q += 16) { | |
9f9c9cbb ZD |
504 | if (memcmp(q, "_SM_", 4) == 0 && q - p <= 0xFFE0) |
505 | rc = smbios_present(q); | |
506 | else if (memcmp(q, "_DMI_", 5) == 0) | |
507 | rc = dmi_present(q); | |
508 | else | |
509 | continue; | |
4f5c791a LP |
510 | if (!rc) { |
511 | dmi_available = 1; | |
0d64484f | 512 | dmi_iounmap(p, 0x10000); |
9a22b6e7 | 513 | goto out; |
4f5c791a | 514 | } |
61e032fa | 515 | } |
3212bff3 | 516 | dmi_iounmap(p, 0x10000); |
61e032fa | 517 | } |
9a22b6e7 IM |
518 | error: |
519 | printk(KERN_INFO "DMI not present or invalid.\n"); | |
520 | out: | |
521 | dmi_initialized = 1; | |
1da177e4 LT |
522 | } |
523 | ||
d7b1956f RW |
524 | /** |
525 | * dmi_matches - check if dmi_system_id structure matches system DMI data | |
526 | * @dmi: pointer to the dmi_system_id structure to check | |
527 | */ | |
528 | static bool dmi_matches(const struct dmi_system_id *dmi) | |
529 | { | |
530 | int i; | |
531 | ||
532 | WARN(!dmi_initialized, KERN_ERR "dmi check: not initialized yet.\n"); | |
533 | ||
534 | for (i = 0; i < ARRAY_SIZE(dmi->matches); i++) { | |
535 | int s = dmi->matches[i].slot; | |
536 | if (s == DMI_NONE) | |
75757507 | 537 | break; |
d7b1956f RW |
538 | if (dmi_ident[s] |
539 | && strstr(dmi_ident[s], dmi->matches[i].substr)) | |
540 | continue; | |
541 | /* No match */ | |
542 | return false; | |
543 | } | |
544 | return true; | |
545 | } | |
546 | ||
75757507 DT |
547 | /** |
548 | * dmi_is_end_of_table - check for end-of-table marker | |
549 | * @dmi: pointer to the dmi_system_id structure to check | |
550 | */ | |
551 | static bool dmi_is_end_of_table(const struct dmi_system_id *dmi) | |
552 | { | |
553 | return dmi->matches[0].slot == DMI_NONE; | |
554 | } | |
555 | ||
1da177e4 LT |
556 | /** |
557 | * dmi_check_system - check system DMI data | |
558 | * @list: array of dmi_system_id structures to match against | |
b0ef371e RD |
559 | * All non-null elements of the list must match |
560 | * their slot's (field index's) data (i.e., each | |
561 | * list string must be a substring of the specified | |
562 | * DMI slot's string data) to be considered a | |
563 | * successful match. | |
1da177e4 LT |
564 | * |
565 | * Walk the blacklist table running matching functions until someone | |
566 | * returns non zero or we hit the end. Callback function is called for | |
b0ef371e | 567 | * each successful match. Returns the number of matches. |
1da177e4 | 568 | */ |
1855256c | 569 | int dmi_check_system(const struct dmi_system_id *list) |
1da177e4 | 570 | { |
d7b1956f RW |
571 | int count = 0; |
572 | const struct dmi_system_id *d; | |
573 | ||
75757507 | 574 | for (d = list; !dmi_is_end_of_table(d); d++) |
d7b1956f RW |
575 | if (dmi_matches(d)) { |
576 | count++; | |
577 | if (d->callback && d->callback(d)) | |
578 | break; | |
1da177e4 | 579 | } |
1da177e4 LT |
580 | |
581 | return count; | |
582 | } | |
1da177e4 LT |
583 | EXPORT_SYMBOL(dmi_check_system); |
584 | ||
d7b1956f RW |
585 | /** |
586 | * dmi_first_match - find dmi_system_id structure matching system DMI data | |
587 | * @list: array of dmi_system_id structures to match against | |
588 | * All non-null elements of the list must match | |
589 | * their slot's (field index's) data (i.e., each | |
590 | * list string must be a substring of the specified | |
591 | * DMI slot's string data) to be considered a | |
592 | * successful match. | |
593 | * | |
594 | * Walk the blacklist table until the first match is found. Return the | |
595 | * pointer to the matching entry or NULL if there's no match. | |
596 | */ | |
597 | const struct dmi_system_id *dmi_first_match(const struct dmi_system_id *list) | |
598 | { | |
599 | const struct dmi_system_id *d; | |
600 | ||
75757507 | 601 | for (d = list; !dmi_is_end_of_table(d); d++) |
d7b1956f RW |
602 | if (dmi_matches(d)) |
603 | return d; | |
604 | ||
605 | return NULL; | |
606 | } | |
607 | EXPORT_SYMBOL(dmi_first_match); | |
608 | ||
1da177e4 LT |
609 | /** |
610 | * dmi_get_system_info - return DMI data value | |
b0ef371e | 611 | * @field: data index (see enum dmi_field) |
1da177e4 LT |
612 | * |
613 | * Returns one DMI data value, can be used to perform | |
614 | * complex DMI data checks. | |
615 | */ | |
1855256c | 616 | const char *dmi_get_system_info(int field) |
1da177e4 LT |
617 | { |
618 | return dmi_ident[field]; | |
619 | } | |
e70c9d5e | 620 | EXPORT_SYMBOL(dmi_get_system_info); |
ebad6a42 | 621 | |
fd8cd7e1 | 622 | /** |
c2bacfc4 RD |
623 | * dmi_name_in_serial - Check if string is in the DMI product serial information |
624 | * @str: string to check for | |
fd8cd7e1 AK |
625 | */ |
626 | int dmi_name_in_serial(const char *str) | |
627 | { | |
628 | int f = DMI_PRODUCT_SERIAL; | |
629 | if (dmi_ident[f] && strstr(dmi_ident[f], str)) | |
630 | return 1; | |
631 | return 0; | |
632 | } | |
a1bae672 AK |
633 | |
634 | /** | |
66e13e66 | 635 | * dmi_name_in_vendors - Check if string is in the DMI system or board vendor name |
a1bae672 AK |
636 | * @str: Case sensitive Name |
637 | */ | |
1855256c | 638 | int dmi_name_in_vendors(const char *str) |
a1bae672 | 639 | { |
66e13e66 | 640 | static int fields[] = { DMI_SYS_VENDOR, DMI_BOARD_VENDOR, DMI_NONE }; |
a1bae672 AK |
641 | int i; |
642 | for (i = 0; fields[i] != DMI_NONE; i++) { | |
643 | int f = fields[i]; | |
644 | if (dmi_ident[f] && strstr(dmi_ident[f], str)) | |
645 | return 1; | |
646 | } | |
647 | return 0; | |
648 | } | |
649 | EXPORT_SYMBOL(dmi_name_in_vendors); | |
650 | ||
ebad6a42 AP |
651 | /** |
652 | * dmi_find_device - find onboard device by type/name | |
653 | * @type: device type or %DMI_DEV_TYPE_ANY to match all device types | |
b0ef371e | 654 | * @name: device name string or %NULL to match all |
ebad6a42 AP |
655 | * @from: previous device found in search, or %NULL for new search. |
656 | * | |
657 | * Iterates through the list of known onboard devices. If a device is | |
658 | * found with a matching @vendor and @device, a pointer to its device | |
659 | * structure is returned. Otherwise, %NULL is returned. | |
b0ef371e | 660 | * A new search is initiated by passing %NULL as the @from argument. |
ebad6a42 AP |
661 | * If @from is not %NULL, searches continue from next device. |
662 | */ | |
1855256c JG |
663 | const struct dmi_device * dmi_find_device(int type, const char *name, |
664 | const struct dmi_device *from) | |
ebad6a42 | 665 | { |
1855256c JG |
666 | const struct list_head *head = from ? &from->list : &dmi_devices; |
667 | struct list_head *d; | |
ebad6a42 AP |
668 | |
669 | for(d = head->next; d != &dmi_devices; d = d->next) { | |
1855256c JG |
670 | const struct dmi_device *dev = |
671 | list_entry(d, struct dmi_device, list); | |
ebad6a42 AP |
672 | |
673 | if (((type == DMI_DEV_TYPE_ANY) || (dev->type == type)) && | |
674 | ((name == NULL) || (strcmp(dev->name, name) == 0))) | |
675 | return dev; | |
676 | } | |
677 | ||
678 | return NULL; | |
679 | } | |
680 | EXPORT_SYMBOL(dmi_find_device); | |
f083a329 AK |
681 | |
682 | /** | |
3e5cd1f2 TH |
683 | * dmi_get_date - parse a DMI date |
684 | * @field: data index (see enum dmi_field) | |
685 | * @yearp: optional out parameter for the year | |
686 | * @monthp: optional out parameter for the month | |
687 | * @dayp: optional out parameter for the day | |
f083a329 | 688 | * |
3e5cd1f2 TH |
689 | * The date field is assumed to be in the form resembling |
690 | * [mm[/dd]]/yy[yy] and the result is stored in the out | |
691 | * parameters any or all of which can be omitted. | |
692 | * | |
693 | * If the field doesn't exist, all out parameters are set to zero | |
694 | * and false is returned. Otherwise, true is returned with any | |
695 | * invalid part of date set to zero. | |
696 | * | |
697 | * On return, year, month and day are guaranteed to be in the | |
698 | * range of [0,9999], [0,12] and [0,31] respectively. | |
f083a329 | 699 | */ |
3e5cd1f2 | 700 | bool dmi_get_date(int field, int *yearp, int *monthp, int *dayp) |
f083a329 | 701 | { |
3e5cd1f2 TH |
702 | int year = 0, month = 0, day = 0; |
703 | bool exists; | |
704 | const char *s, *y; | |
02c24fa8 | 705 | char *e; |
f083a329 | 706 | |
3e5cd1f2 TH |
707 | s = dmi_get_system_info(field); |
708 | exists = s; | |
709 | if (!exists) | |
710 | goto out; | |
f083a329 | 711 | |
3e5cd1f2 TH |
712 | /* |
713 | * Determine year first. We assume the date string resembles | |
714 | * mm/dd/yy[yy] but the original code extracted only the year | |
715 | * from the end. Keep the behavior in the spirit of no | |
716 | * surprises. | |
717 | */ | |
718 | y = strrchr(s, '/'); | |
719 | if (!y) | |
720 | goto out; | |
721 | ||
722 | y++; | |
723 | year = simple_strtoul(y, &e, 10); | |
724 | if (y != e && year < 100) { /* 2-digit year */ | |
f083a329 AK |
725 | year += 1900; |
726 | if (year < 1996) /* no dates < spec 1.0 */ | |
727 | year += 100; | |
728 | } | |
3e5cd1f2 TH |
729 | if (year > 9999) /* year should fit in %04d */ |
730 | year = 0; | |
731 | ||
732 | /* parse the mm and dd */ | |
733 | month = simple_strtoul(s, &e, 10); | |
734 | if (s == e || *e != '/' || !month || month > 12) { | |
735 | month = 0; | |
736 | goto out; | |
737 | } | |
f083a329 | 738 | |
3e5cd1f2 TH |
739 | s = e + 1; |
740 | day = simple_strtoul(s, &e, 10); | |
741 | if (s == y || s == e || *e != '/' || day > 31) | |
742 | day = 0; | |
743 | out: | |
744 | if (yearp) | |
745 | *yearp = year; | |
746 | if (monthp) | |
747 | *monthp = month; | |
748 | if (dayp) | |
749 | *dayp = day; | |
750 | return exists; | |
f083a329 | 751 | } |
3e5cd1f2 | 752 | EXPORT_SYMBOL(dmi_get_date); |
7fce084a JD |
753 | |
754 | /** | |
755 | * dmi_walk - Walk the DMI table and get called back for every record | |
756 | * @decode: Callback function | |
e7a19c56 | 757 | * @private_data: Private data to be passed to the callback function |
7fce084a JD |
758 | * |
759 | * Returns -1 when the DMI table can't be reached, 0 on success. | |
760 | */ | |
e7a19c56 JD |
761 | int dmi_walk(void (*decode)(const struct dmi_header *, void *), |
762 | void *private_data) | |
7fce084a JD |
763 | { |
764 | u8 *buf; | |
765 | ||
766 | if (!dmi_available) | |
767 | return -1; | |
768 | ||
769 | buf = ioremap(dmi_base, dmi_len); | |
770 | if (buf == NULL) | |
771 | return -1; | |
772 | ||
e7a19c56 | 773 | dmi_table(buf, dmi_len, dmi_num, decode, private_data); |
7fce084a JD |
774 | |
775 | iounmap(buf); | |
776 | return 0; | |
777 | } | |
778 | EXPORT_SYMBOL_GPL(dmi_walk); | |
d61c72e5 JS |
779 | |
780 | /** | |
781 | * dmi_match - compare a string to the dmi field (if exists) | |
c2bacfc4 RD |
782 | * @f: DMI field identifier |
783 | * @str: string to compare the DMI field to | |
d61c72e5 JS |
784 | * |
785 | * Returns true if the requested field equals to the str (including NULL). | |
786 | */ | |
787 | bool dmi_match(enum dmi_field f, const char *str) | |
788 | { | |
789 | const char *info = dmi_get_system_info(f); | |
790 | ||
791 | if (info == NULL || str == NULL) | |
792 | return info == str; | |
793 | ||
794 | return !strcmp(info, str); | |
795 | } | |
796 | EXPORT_SYMBOL_GPL(dmi_match); |