dmi_scan.c 25 KB

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