dmi_scan.c 29 KB

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