dmi_scan.c 26 KB

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