dmi_scan.c 23 KB

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