dmi_scan.c 23 KB

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