dmi_scan.c 26 KB

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