dmi_scan.c 21 KB

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