dmi_scan.c 27 KB

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