dmi_scan.c 29 KB

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