dmi_scan.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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(struct dmi_header *dm, u8 s)
  11. {
  12. 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)(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. struct dmi_header *dm = (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(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. /*
  76. * Save a DMI string
  77. */
  78. static void __init dmi_save_ident(struct dmi_header *dm, int slot, int string)
  79. {
  80. char *p, *d = (char*) dm;
  81. if (dmi_ident[slot])
  82. return;
  83. p = dmi_string(dm, d[string]);
  84. if (p == NULL)
  85. return;
  86. dmi_ident[slot] = p;
  87. }
  88. static void __init dmi_save_devices(struct dmi_header *dm)
  89. {
  90. int i, count = (dm->length - sizeof(struct dmi_header)) / 2;
  91. struct dmi_device *dev;
  92. for (i = 0; i < count; i++) {
  93. char *d = (char *)(dm + 1) + (i * 2);
  94. /* Skip disabled device */
  95. if ((*d & 0x80) == 0)
  96. continue;
  97. dev = dmi_alloc(sizeof(*dev));
  98. if (!dev) {
  99. printk(KERN_ERR "dmi_save_devices: out of memory.\n");
  100. break;
  101. }
  102. dev->type = *d++ & 0x7f;
  103. dev->name = dmi_string(dm, *d);
  104. dev->device_data = NULL;
  105. list_add(&dev->list, &dmi_devices);
  106. }
  107. }
  108. static void __init dmi_save_ipmi_device(struct dmi_header *dm)
  109. {
  110. struct dmi_device *dev;
  111. void * data;
  112. data = dmi_alloc(dm->length);
  113. if (data == NULL) {
  114. printk(KERN_ERR "dmi_save_ipmi_device: out of memory.\n");
  115. return;
  116. }
  117. memcpy(data, dm, dm->length);
  118. dev = dmi_alloc(sizeof(*dev));
  119. if (!dev) {
  120. printk(KERN_ERR "dmi_save_ipmi_device: out of memory.\n");
  121. return;
  122. }
  123. dev->type = DMI_DEV_TYPE_IPMI;
  124. dev->name = "IPMI controller";
  125. dev->device_data = data;
  126. list_add(&dev->list, &dmi_devices);
  127. }
  128. /*
  129. * Process a DMI table entry. Right now all we care about are the BIOS
  130. * and machine entries. For 2.5 we should pull the smbus controller info
  131. * out of here.
  132. */
  133. static void __init dmi_decode(struct dmi_header *dm)
  134. {
  135. switch(dm->type) {
  136. case 0: /* BIOS Information */
  137. dmi_save_ident(dm, DMI_BIOS_VENDOR, 4);
  138. dmi_save_ident(dm, DMI_BIOS_VERSION, 5);
  139. dmi_save_ident(dm, DMI_BIOS_DATE, 8);
  140. break;
  141. case 1: /* System Information */
  142. dmi_save_ident(dm, DMI_SYS_VENDOR, 4);
  143. dmi_save_ident(dm, DMI_PRODUCT_NAME, 5);
  144. dmi_save_ident(dm, DMI_PRODUCT_VERSION, 6);
  145. dmi_save_ident(dm, DMI_PRODUCT_SERIAL, 7);
  146. break;
  147. case 2: /* Base Board Information */
  148. dmi_save_ident(dm, DMI_BOARD_VENDOR, 4);
  149. dmi_save_ident(dm, DMI_BOARD_NAME, 5);
  150. dmi_save_ident(dm, DMI_BOARD_VERSION, 6);
  151. break;
  152. case 10: /* Onboard Devices Information */
  153. dmi_save_devices(dm);
  154. break;
  155. case 38: /* IPMI Device Information */
  156. dmi_save_ipmi_device(dm);
  157. }
  158. }
  159. static int __init dmi_present(char __iomem *p)
  160. {
  161. u8 buf[15];
  162. memcpy_fromio(buf, p, 15);
  163. if ((memcmp(buf, "_DMI_", 5) == 0) && dmi_checksum(buf)) {
  164. u16 num = (buf[13] << 8) | buf[12];
  165. u16 len = (buf[7] << 8) | buf[6];
  166. u32 base = (buf[11] << 24) | (buf[10] << 16) |
  167. (buf[9] << 8) | buf[8];
  168. /*
  169. * DMI version 0.0 means that the real version is taken from
  170. * the SMBIOS version, which we don't know at this point.
  171. */
  172. if (buf[14] != 0)
  173. printk(KERN_INFO "DMI %d.%d present.\n",
  174. buf[14] >> 4, buf[14] & 0xF);
  175. else
  176. printk(KERN_INFO "DMI present.\n");
  177. if (dmi_table(base,len, num, dmi_decode) == 0)
  178. return 0;
  179. }
  180. return 1;
  181. }
  182. void __init dmi_scan_machine(void)
  183. {
  184. char __iomem *p, *q;
  185. int rc;
  186. if (efi_enabled) {
  187. if (efi.smbios == EFI_INVALID_TABLE_ADDR)
  188. goto out;
  189. /* This is called as a core_initcall() because it isn't
  190. * needed during early boot. This also means we can
  191. * iounmap the space when we're done with it.
  192. */
  193. p = dmi_ioremap(efi.smbios, 32);
  194. if (p == NULL)
  195. goto out;
  196. rc = dmi_present(p + 0x10); /* offset of _DMI_ string */
  197. dmi_iounmap(p, 32);
  198. if (!rc)
  199. return;
  200. }
  201. else {
  202. /*
  203. * no iounmap() for that ioremap(); it would be a no-op, but
  204. * it's so early in setup that sucker gets confused into doing
  205. * what it shouldn't if we actually call it.
  206. */
  207. p = dmi_ioremap(0xF0000, 0x10000);
  208. if (p == NULL)
  209. goto out;
  210. for (q = p; q < p + 0x10000; q += 16) {
  211. rc = dmi_present(q);
  212. if (!rc)
  213. return;
  214. }
  215. }
  216. out: printk(KERN_INFO "DMI not present or invalid.\n");
  217. }
  218. /**
  219. * dmi_check_system - check system DMI data
  220. * @list: array of dmi_system_id structures to match against
  221. * All non-null elements of the list must match
  222. * their slot's (field index's) data (i.e., each
  223. * list string must be a substring of the specified
  224. * DMI slot's string data) to be considered a
  225. * successful match.
  226. *
  227. * Walk the blacklist table running matching functions until someone
  228. * returns non zero or we hit the end. Callback function is called for
  229. * each successful match. Returns the number of matches.
  230. */
  231. int dmi_check_system(struct dmi_system_id *list)
  232. {
  233. int i, count = 0;
  234. struct dmi_system_id *d = list;
  235. while (d->ident) {
  236. for (i = 0; i < ARRAY_SIZE(d->matches); i++) {
  237. int s = d->matches[i].slot;
  238. if (s == DMI_NONE)
  239. continue;
  240. if (dmi_ident[s] && strstr(dmi_ident[s], d->matches[i].substr))
  241. continue;
  242. /* No match */
  243. goto fail;
  244. }
  245. count++;
  246. if (d->callback && d->callback(d))
  247. break;
  248. fail: d++;
  249. }
  250. return count;
  251. }
  252. EXPORT_SYMBOL(dmi_check_system);
  253. /**
  254. * dmi_get_system_info - return DMI data value
  255. * @field: data index (see enum dmi_field)
  256. *
  257. * Returns one DMI data value, can be used to perform
  258. * complex DMI data checks.
  259. */
  260. char *dmi_get_system_info(int field)
  261. {
  262. return dmi_ident[field];
  263. }
  264. EXPORT_SYMBOL(dmi_get_system_info);
  265. /**
  266. * dmi_find_device - find onboard device by type/name
  267. * @type: device type or %DMI_DEV_TYPE_ANY to match all device types
  268. * @name: device name string or %NULL to match all
  269. * @from: previous device found in search, or %NULL for new search.
  270. *
  271. * Iterates through the list of known onboard devices. If a device is
  272. * found with a matching @vendor and @device, a pointer to its device
  273. * structure is returned. Otherwise, %NULL is returned.
  274. * A new search is initiated by passing %NULL as the @from argument.
  275. * If @from is not %NULL, searches continue from next device.
  276. */
  277. struct dmi_device * dmi_find_device(int type, const char *name,
  278. struct dmi_device *from)
  279. {
  280. struct list_head *d, *head = from ? &from->list : &dmi_devices;
  281. for(d = head->next; d != &dmi_devices; d = d->next) {
  282. struct dmi_device *dev = list_entry(d, struct dmi_device, list);
  283. if (((type == DMI_DEV_TYPE_ANY) || (dev->type == type)) &&
  284. ((name == NULL) || (strcmp(dev->name, name) == 0)))
  285. return dev;
  286. }
  287. return NULL;
  288. }
  289. EXPORT_SYMBOL(dmi_find_device);
  290. /**
  291. * dmi_get_year - Return year of a DMI date
  292. * @field: data index (like dmi_get_system_info)
  293. *
  294. * Returns -1 when the field doesn't exist. 0 when it is broken.
  295. */
  296. int dmi_get_year(int field)
  297. {
  298. int year;
  299. char *s = dmi_get_system_info(field);
  300. if (!s)
  301. return -1;
  302. if (*s == '\0')
  303. return 0;
  304. s = strrchr(s, '/');
  305. if (!s)
  306. return 0;
  307. s += 1;
  308. year = simple_strtoul(s, NULL, 0);
  309. if (year && year < 100) { /* 2-digit year */
  310. year += 1900;
  311. if (year < 1996) /* no dates < spec 1.0 */
  312. year += 100;
  313. }
  314. return year;
  315. }