dmi_scan.c 21 KB

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