dmi_scan.c 11 KB

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