PageRenderTime 52ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/firmware/dmi_scan.c

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