dmi_scan.c 27 KB

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