PageRenderTime 25ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/drivers/firmware/dmi_scan.c

https://bitbucket.org/abioy/linux
C | 689 lines | 443 code | 101 blank | 145 comment | 126 complexity | 2fa6c0a542ef3e0d668389856f81f1fa MD5 | raw file
Possible License(s): CC-BY-SA-3.0, GPL-2.0, LGPL-2.0, AGPL-1.0
  1. #include <linux/types.h>
  2. #include <linux/string.h>
  3. #include <linux/init.h>
  4. #include <linux/module.h>
  5. #include <linux/dmi.h>
  6. #include <linux/efi.h>
  7. #include <linux/bootmem.h>
  8. #include <asm/dmi.h>
  9. /*
  10. * DMI stands for "Desktop Management Interface". It is part
  11. * of and an antecedent to, SMBIOS, which stands for System
  12. * Management BIOS. See further: http://www.dmtf.org/standards
  13. */
  14. static char dmi_empty_string[] = " ";
  15. /*
  16. * Catch too early calls to dmi_check_system():
  17. */
  18. static int dmi_initialized;
  19. static const char * __init dmi_string_nosave(const struct dmi_header *dm, u8 s)
  20. {
  21. const u8 *bp = ((u8 *) dm) + dm->length;
  22. if (s) {
  23. s--;
  24. while (s > 0 && *bp) {
  25. bp += strlen(bp) + 1;
  26. s--;
  27. }
  28. if (*bp != 0) {
  29. size_t len = strlen(bp)+1;
  30. size_t cmp_len = len > 8 ? 8 : len;
  31. if (!memcmp(bp, dmi_empty_string, cmp_len))
  32. return dmi_empty_string;
  33. return bp;
  34. }
  35. }
  36. return "";
  37. }
  38. static char * __init dmi_string(const struct dmi_header *dm, u8 s)
  39. {
  40. const char *bp = dmi_string_nosave(dm, s);
  41. char *str;
  42. size_t len;
  43. if (bp == dmi_empty_string)
  44. return dmi_empty_string;
  45. len = strlen(bp) + 1;
  46. str = dmi_alloc(len);
  47. if (str != NULL)
  48. strcpy(str, bp);
  49. else
  50. printk(KERN_ERR "dmi_string: cannot allocate %Zu bytes.\n", len);
  51. return str;
  52. }
  53. /*
  54. * We have to be cautious here. We have seen BIOSes with DMI pointers
  55. * pointing to completely the wrong place for example
  56. */
  57. static void dmi_table(u8 *buf, int len, int num,
  58. void (*decode)(const struct dmi_header *, void *),
  59. void *private_data)
  60. {
  61. u8 *data = buf;
  62. int i = 0;
  63. /*
  64. * Stop when we see all the items the table claimed to have
  65. * OR we run off the end of the table (also happens)
  66. */
  67. while ((i < num) && (data - buf + sizeof(struct dmi_header)) <= len) {
  68. const struct dmi_header *dm = (const struct dmi_header *)data;
  69. /*
  70. * We want to know the total length (formatted area and
  71. * strings) before decoding to make sure we won't run off the
  72. * table in dmi_decode or dmi_string
  73. */
  74. data += dm->length;
  75. while ((data - buf < len - 1) && (data[0] || data[1]))
  76. data++;
  77. if (data - buf < len - 1)
  78. decode(dm, private_data);
  79. data += 2;
  80. i++;
  81. }
  82. }
  83. static u32 dmi_base;
  84. static u16 dmi_len;
  85. static u16 dmi_num;
  86. static int __init dmi_walk_early(void (*decode)(const struct dmi_header *,
  87. void *))
  88. {
  89. u8 *buf;
  90. buf = dmi_ioremap(dmi_base, dmi_len);
  91. if (buf == NULL)
  92. return -1;
  93. dmi_table(buf, dmi_len, dmi_num, decode, NULL);
  94. dmi_iounmap(buf, dmi_len);
  95. return 0;
  96. }
  97. static int __init dmi_checksum(const u8 *buf)
  98. {
  99. u8 sum = 0;
  100. int a;
  101. for (a = 0; a < 15; a++)
  102. sum += buf[a];
  103. return sum == 0;
  104. }
  105. static char *dmi_ident[DMI_STRING_MAX];
  106. static LIST_HEAD(dmi_devices);
  107. int dmi_available;
  108. /*
  109. * Save a DMI string
  110. */
  111. static void __init dmi_save_ident(const struct dmi_header *dm, int slot, int string)
  112. {
  113. const char *d = (const char*) dm;
  114. char *p;
  115. if (dmi_ident[slot])
  116. return;
  117. p = dmi_string(dm, d[string]);
  118. if (p == NULL)
  119. return;
  120. dmi_ident[slot] = p;
  121. }
  122. static void __init dmi_save_uuid(const struct dmi_header *dm, int slot, int index)
  123. {
  124. const u8 *d = (u8*) dm + index;
  125. char *s;
  126. int is_ff = 1, is_00 = 1, i;
  127. if (dmi_ident[slot])
  128. return;
  129. for (i = 0; i < 16 && (is_ff || is_00); i++) {
  130. if(d[i] != 0x00) is_ff = 0;
  131. if(d[i] != 0xFF) is_00 = 0;
  132. }
  133. if (is_ff || is_00)
  134. return;
  135. s = dmi_alloc(16*2+4+1);
  136. if (!s)
  137. return;
  138. sprintf(s, "%pUB", d);
  139. dmi_ident[slot] = s;
  140. }
  141. static void __init dmi_save_type(const struct dmi_header *dm, int slot, int index)
  142. {
  143. const u8 *d = (u8*) dm + index;
  144. char *s;
  145. if (dmi_ident[slot])
  146. return;
  147. s = dmi_alloc(4);
  148. if (!s)
  149. return;
  150. sprintf(s, "%u", *d & 0x7F);
  151. dmi_ident[slot] = s;
  152. }
  153. static void __init dmi_save_one_device(int type, const char *name)
  154. {
  155. struct dmi_device *dev;
  156. /* No duplicate device */
  157. if (dmi_find_device(type, name, NULL))
  158. return;
  159. dev = dmi_alloc(sizeof(*dev) + strlen(name) + 1);
  160. if (!dev) {
  161. printk(KERN_ERR "dmi_save_one_device: out of memory.\n");
  162. return;
  163. }
  164. dev->type = type;
  165. strcpy((char *)(dev + 1), name);
  166. dev->name = (char *)(dev + 1);
  167. dev->device_data = NULL;
  168. list_add(&dev->list, &dmi_devices);
  169. }
  170. static void __init dmi_save_devices(const struct dmi_header *dm)
  171. {
  172. int i, count = (dm->length - sizeof(struct dmi_header)) / 2;
  173. for (i = 0; i < count; i++) {
  174. const char *d = (char *)(dm + 1) + (i * 2);
  175. /* Skip disabled device */
  176. if ((*d & 0x80) == 0)
  177. continue;
  178. dmi_save_one_device(*d & 0x7f, dmi_string_nosave(dm, *(d + 1)));
  179. }
  180. }
  181. static void __init dmi_save_oem_strings_devices(const struct dmi_header *dm)
  182. {
  183. int i, count = *(u8 *)(dm + 1);
  184. struct dmi_device *dev;
  185. for (i = 1; i <= count; i++) {
  186. char *devname = dmi_string(dm, i);
  187. if (devname == dmi_empty_string)
  188. continue;
  189. dev = dmi_alloc(sizeof(*dev));
  190. if (!dev) {
  191. printk(KERN_ERR
  192. "dmi_save_oem_strings_devices: out of memory.\n");
  193. break;
  194. }
  195. dev->type = DMI_DEV_TYPE_OEM_STRING;
  196. dev->name = devname;
  197. dev->device_data = NULL;
  198. list_add(&dev->list, &dmi_devices);
  199. }
  200. }
  201. static void __init dmi_save_ipmi_device(const struct dmi_header *dm)
  202. {
  203. struct dmi_device *dev;
  204. void * data;
  205. data = dmi_alloc(dm->length);
  206. if (data == NULL) {
  207. printk(KERN_ERR "dmi_save_ipmi_device: out of memory.\n");
  208. return;
  209. }
  210. memcpy(data, dm, dm->length);
  211. dev = dmi_alloc(sizeof(*dev));
  212. if (!dev) {
  213. printk(KERN_ERR "dmi_save_ipmi_device: out of memory.\n");
  214. return;
  215. }
  216. dev->type = DMI_DEV_TYPE_IPMI;
  217. dev->name = "IPMI controller";
  218. dev->device_data = data;
  219. list_add_tail(&dev->list, &dmi_devices);
  220. }
  221. static void __init dmi_save_extended_devices(const struct dmi_header *dm)
  222. {
  223. const u8 *d = (u8*) dm + 5;
  224. /* Skip disabled device */
  225. if ((*d & 0x80) == 0)
  226. return;
  227. dmi_save_one_device(*d & 0x7f, dmi_string_nosave(dm, *(d - 1)));
  228. }
  229. /*
  230. * Process a DMI table entry. Right now all we care about are the BIOS
  231. * and machine entries. For 2.5 we should pull the smbus controller info
  232. * out of here.
  233. */
  234. static void __init dmi_decode(const struct dmi_header *dm, void *dummy)
  235. {
  236. switch(dm->type) {
  237. case 0: /* BIOS Information */
  238. dmi_save_ident(dm, DMI_BIOS_VENDOR, 4);
  239. dmi_save_ident(dm, DMI_BIOS_VERSION, 5);
  240. dmi_save_ident(dm, DMI_BIOS_DATE, 8);
  241. break;
  242. case 1: /* System Information */
  243. dmi_save_ident(dm, DMI_SYS_VENDOR, 4);
  244. dmi_save_ident(dm, DMI_PRODUCT_NAME, 5);
  245. dmi_save_ident(dm, DMI_PRODUCT_VERSION, 6);
  246. dmi_save_ident(dm, DMI_PRODUCT_SERIAL, 7);
  247. dmi_save_uuid(dm, DMI_PRODUCT_UUID, 8);
  248. break;
  249. case 2: /* Base Board Information */
  250. dmi_save_ident(dm, DMI_BOARD_VENDOR, 4);
  251. dmi_save_ident(dm, DMI_BOARD_NAME, 5);
  252. dmi_save_ident(dm, DMI_BOARD_VERSION, 6);
  253. dmi_save_ident(dm, DMI_BOARD_SERIAL, 7);
  254. dmi_save_ident(dm, DMI_BOARD_ASSET_TAG, 8);
  255. break;
  256. case 3: /* Chassis Information */
  257. dmi_save_ident(dm, DMI_CHASSIS_VENDOR, 4);
  258. dmi_save_type(dm, DMI_CHASSIS_TYPE, 5);
  259. dmi_save_ident(dm, DMI_CHASSIS_VERSION, 6);
  260. dmi_save_ident(dm, DMI_CHASSIS_SERIAL, 7);
  261. dmi_save_ident(dm, DMI_CHASSIS_ASSET_TAG, 8);
  262. break;
  263. case 10: /* Onboard Devices Information */
  264. dmi_save_devices(dm);
  265. break;
  266. case 11: /* OEM Strings */
  267. dmi_save_oem_strings_devices(dm);
  268. break;
  269. case 38: /* IPMI Device Information */
  270. dmi_save_ipmi_device(dm);
  271. break;
  272. case 41: /* Onboard Devices Extended Information */
  273. dmi_save_extended_devices(dm);
  274. }
  275. }
  276. static int __init dmi_present(const char __iomem *p)
  277. {
  278. u8 buf[15];
  279. memcpy_fromio(buf, p, 15);
  280. if ((memcmp(buf, "_DMI_", 5) == 0) && dmi_checksum(buf)) {
  281. dmi_num = (buf[13] << 8) | buf[12];
  282. dmi_len = (buf[7] << 8) | buf[6];
  283. dmi_base = (buf[11] << 24) | (buf[10] << 16) |
  284. (buf[9] << 8) | buf[8];
  285. /*
  286. * DMI version 0.0 means that the real version is taken from
  287. * the SMBIOS version, which we don't know at this point.
  288. */
  289. if (buf[14] != 0)
  290. printk(KERN_INFO "DMI %d.%d present.\n",
  291. buf[14] >> 4, buf[14] & 0xF);
  292. else
  293. printk(KERN_INFO "DMI present.\n");
  294. if (dmi_walk_early(dmi_decode) == 0)
  295. return 0;
  296. }
  297. return 1;
  298. }
  299. void __init dmi_scan_machine(void)
  300. {
  301. char __iomem *p, *q;
  302. int rc;
  303. if (efi_enabled) {
  304. if (efi.smbios == EFI_INVALID_TABLE_ADDR)
  305. goto error;
  306. /* This is called as a core_initcall() because it isn't
  307. * needed during early boot. This also means we can
  308. * iounmap the space when we're done with it.
  309. */
  310. p = dmi_ioremap(efi.smbios, 32);
  311. if (p == NULL)
  312. goto error;
  313. rc = dmi_present(p + 0x10); /* offset of _DMI_ string */
  314. dmi_iounmap(p, 32);
  315. if (!rc) {
  316. dmi_available = 1;
  317. goto out;
  318. }
  319. }
  320. else {
  321. /*
  322. * no iounmap() for that ioremap(); it would be a no-op, but
  323. * it's so early in setup that sucker gets confused into doing
  324. * what it shouldn't if we actually call it.
  325. */
  326. p = dmi_ioremap(0xF0000, 0x10000);
  327. if (p == NULL)
  328. goto error;
  329. for (q = p; q < p + 0x10000; q += 16) {
  330. rc = dmi_present(q);
  331. if (!rc) {
  332. dmi_available = 1;
  333. dmi_iounmap(p, 0x10000);
  334. goto out;
  335. }
  336. }
  337. dmi_iounmap(p, 0x10000);
  338. }
  339. error:
  340. printk(KERN_INFO "DMI not present or invalid.\n");
  341. out:
  342. dmi_initialized = 1;
  343. }
  344. /**
  345. * dmi_matches - check if dmi_system_id structure matches system DMI data
  346. * @dmi: pointer to the dmi_system_id structure to check
  347. */
  348. static bool dmi_matches(const struct dmi_system_id *dmi)
  349. {
  350. int i;
  351. WARN(!dmi_initialized, KERN_ERR "dmi check: not initialized yet.\n");
  352. for (i = 0; i < ARRAY_SIZE(dmi->matches); i++) {
  353. int s = dmi->matches[i].slot;
  354. if (s == DMI_NONE)
  355. break;
  356. if (dmi_ident[s]
  357. && strstr(dmi_ident[s], dmi->matches[i].substr))
  358. continue;
  359. /* No match */
  360. return false;
  361. }
  362. return true;
  363. }
  364. /**
  365. * dmi_is_end_of_table - check for end-of-table marker
  366. * @dmi: pointer to the dmi_system_id structure to check
  367. */
  368. static bool dmi_is_end_of_table(const struct dmi_system_id *dmi)
  369. {
  370. return dmi->matches[0].slot == DMI_NONE;
  371. }
  372. /**
  373. * dmi_check_system - check system DMI data
  374. * @list: array of dmi_system_id structures to match against
  375. * All non-null elements of the list must match
  376. * their slot's (field index's) data (i.e., each
  377. * list string must be a substring of the specified
  378. * DMI slot's string data) to be considered a
  379. * successful match.
  380. *
  381. * Walk the blacklist table running matching functions until someone
  382. * returns non zero or we hit the end. Callback function is called for
  383. * each successful match. Returns the number of matches.
  384. */
  385. int dmi_check_system(const struct dmi_system_id *list)
  386. {
  387. int count = 0;
  388. const struct dmi_system_id *d;
  389. for (d = list; !dmi_is_end_of_table(d); d++)
  390. if (dmi_matches(d)) {
  391. count++;
  392. if (d->callback && d->callback(d))
  393. break;
  394. }
  395. return count;
  396. }
  397. EXPORT_SYMBOL(dmi_check_system);
  398. /**
  399. * dmi_first_match - find dmi_system_id structure matching system DMI data
  400. * @list: array of dmi_system_id structures to match against
  401. * All non-null elements of the list must match
  402. * their slot's (field index's) data (i.e., each
  403. * list string must be a substring of the specified
  404. * DMI slot's string data) to be considered a
  405. * successful match.
  406. *
  407. * Walk the blacklist table until the first match is found. Return the
  408. * pointer to the matching entry or NULL if there's no match.
  409. */
  410. const struct dmi_system_id *dmi_first_match(const struct dmi_system_id *list)
  411. {
  412. const struct dmi_system_id *d;
  413. for (d = list; !dmi_is_end_of_table(d); d++)
  414. if (dmi_matches(d))
  415. return d;
  416. return NULL;
  417. }
  418. EXPORT_SYMBOL(dmi_first_match);
  419. /**
  420. * dmi_get_system_info - return DMI data value
  421. * @field: data index (see enum dmi_field)
  422. *
  423. * Returns one DMI data value, can be used to perform
  424. * complex DMI data checks.
  425. */
  426. const char *dmi_get_system_info(int field)
  427. {
  428. return dmi_ident[field];
  429. }
  430. EXPORT_SYMBOL(dmi_get_system_info);
  431. /**
  432. * dmi_name_in_serial - Check if string is in the DMI product serial information
  433. * @str: string to check for
  434. */
  435. int dmi_name_in_serial(const char *str)
  436. {
  437. int f = DMI_PRODUCT_SERIAL;
  438. if (dmi_ident[f] && strstr(dmi_ident[f], str))
  439. return 1;
  440. return 0;
  441. }
  442. /**
  443. * dmi_name_in_vendors - Check if string is anywhere in the DMI vendor information.
  444. * @str: Case sensitive Name
  445. */
  446. int dmi_name_in_vendors(const char *str)
  447. {
  448. static int fields[] = { DMI_BIOS_VENDOR, DMI_BIOS_VERSION, DMI_SYS_VENDOR,
  449. DMI_PRODUCT_NAME, DMI_PRODUCT_VERSION, DMI_BOARD_VENDOR,
  450. DMI_BOARD_NAME, DMI_BOARD_VERSION, DMI_NONE };
  451. int i;
  452. for (i = 0; fields[i] != DMI_NONE; i++) {
  453. int f = fields[i];
  454. if (dmi_ident[f] && strstr(dmi_ident[f], str))
  455. return 1;
  456. }
  457. return 0;
  458. }
  459. EXPORT_SYMBOL(dmi_name_in_vendors);
  460. /**
  461. * dmi_find_device - find onboard device by type/name
  462. * @type: device type or %DMI_DEV_TYPE_ANY to match all device types
  463. * @name: device name string or %NULL to match all
  464. * @from: previous device found in search, or %NULL for new search.
  465. *
  466. * Iterates through the list of known onboard devices. If a device is
  467. * found with a matching @vendor and @device, a pointer to its device
  468. * structure is returned. Otherwise, %NULL is returned.
  469. * A new search is initiated by passing %NULL as the @from argument.
  470. * If @from is not %NULL, searches continue from next device.
  471. */
  472. const struct dmi_device * dmi_find_device(int type, const char *name,
  473. const struct dmi_device *from)
  474. {
  475. const struct list_head *head = from ? &from->list : &dmi_devices;
  476. struct list_head *d;
  477. for(d = head->next; d != &dmi_devices; d = d->next) {
  478. const struct dmi_device *dev =
  479. list_entry(d, struct dmi_device, list);
  480. if (((type == DMI_DEV_TYPE_ANY) || (dev->type == type)) &&
  481. ((name == NULL) || (strcmp(dev->name, name) == 0)))
  482. return dev;
  483. }
  484. return NULL;
  485. }
  486. EXPORT_SYMBOL(dmi_find_device);
  487. /**
  488. * dmi_get_date - parse a DMI date
  489. * @field: data index (see enum dmi_field)
  490. * @yearp: optional out parameter for the year
  491. * @monthp: optional out parameter for the month
  492. * @dayp: optional out parameter for the day
  493. *
  494. * The date field is assumed to be in the form resembling
  495. * [mm[/dd]]/yy[yy] and the result is stored in the out
  496. * parameters any or all of which can be omitted.
  497. *
  498. * If the field doesn't exist, all out parameters are set to zero
  499. * and false is returned. Otherwise, true is returned with any
  500. * invalid part of date set to zero.
  501. *
  502. * On return, year, month and day are guaranteed to be in the
  503. * range of [0,9999], [0,12] and [0,31] respectively.
  504. */
  505. bool dmi_get_date(int field, int *yearp, int *monthp, int *dayp)
  506. {
  507. int year = 0, month = 0, day = 0;
  508. bool exists;
  509. const char *s, *y;
  510. char *e;
  511. s = dmi_get_system_info(field);
  512. exists = s;
  513. if (!exists)
  514. goto out;
  515. /*
  516. * Determine year first. We assume the date string resembles
  517. * mm/dd/yy[yy] but the original code extracted only the year
  518. * from the end. Keep the behavior in the spirit of no
  519. * surprises.
  520. */
  521. y = strrchr(s, '/');
  522. if (!y)
  523. goto out;
  524. y++;
  525. year = simple_strtoul(y, &e, 10);
  526. if (y != e && year < 100) { /* 2-digit year */
  527. year += 1900;
  528. if (year < 1996) /* no dates < spec 1.0 */
  529. year += 100;
  530. }
  531. if (year > 9999) /* year should fit in %04d */
  532. year = 0;
  533. /* parse the mm and dd */
  534. month = simple_strtoul(s, &e, 10);
  535. if (s == e || *e != '/' || !month || month > 12) {
  536. month = 0;
  537. goto out;
  538. }
  539. s = e + 1;
  540. day = simple_strtoul(s, &e, 10);
  541. if (s == y || s == e || *e != '/' || day > 31)
  542. day = 0;
  543. out:
  544. if (yearp)
  545. *yearp = year;
  546. if (monthp)
  547. *monthp = month;
  548. if (dayp)
  549. *dayp = day;
  550. return exists;
  551. }
  552. EXPORT_SYMBOL(dmi_get_date);
  553. /**
  554. * dmi_walk - Walk the DMI table and get called back for every record
  555. * @decode: Callback function
  556. * @private_data: Private data to be passed to the callback function
  557. *
  558. * Returns -1 when the DMI table can't be reached, 0 on success.
  559. */
  560. int dmi_walk(void (*decode)(const struct dmi_header *, void *),
  561. void *private_data)
  562. {
  563. u8 *buf;
  564. if (!dmi_available)
  565. return -1;
  566. buf = ioremap(dmi_base, dmi_len);
  567. if (buf == NULL)
  568. return -1;
  569. dmi_table(buf, dmi_len, dmi_num, decode, private_data);
  570. iounmap(buf);
  571. return 0;
  572. }
  573. EXPORT_SYMBOL_GPL(dmi_walk);
  574. /**
  575. * dmi_match - compare a string to the dmi field (if exists)
  576. * @f: DMI field identifier
  577. * @str: string to compare the DMI field to
  578. *
  579. * Returns true if the requested field equals to the str (including NULL).
  580. */
  581. bool dmi_match(enum dmi_field f, const char *str)
  582. {
  583. const char *info = dmi_get_system_info(f);
  584. if (info == NULL || str == NULL)
  585. return info == str;
  586. return !strcmp(info, str);
  587. }
  588. EXPORT_SYMBOL_GPL(dmi_match);