dmi_scan.c 25 KB

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