dmi_scan.c 26 KB

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