dmi_scan.c 30 KB

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