dmi_scan.c 20 KB

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