dmi_scan.c 25 KB

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