dmi_scan.c 18 KB

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