dmi_scan.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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/bootmem.h>
  7. #include <linux/slab.h>
  8. static char * __init dmi_string(struct dmi_header *dm, u8 s)
  9. {
  10. u8 *bp = ((u8 *) dm) + dm->length;
  11. char *str = "";
  12. if (s) {
  13. s--;
  14. while (s > 0 && *bp) {
  15. bp += strlen(bp) + 1;
  16. s--;
  17. }
  18. if (*bp != 0) {
  19. str = dmi_alloc(strlen(bp) + 1);
  20. if (str != NULL)
  21. strcpy(str, bp);
  22. else
  23. printk(KERN_ERR "dmi_string: out of memory.\n");
  24. }
  25. }
  26. return str;
  27. }
  28. /*
  29. * We have to be cautious here. We have seen BIOSes with DMI pointers
  30. * pointing to completely the wrong place for example
  31. */
  32. static int __init dmi_table(u32 base, int len, int num,
  33. void (*decode)(struct dmi_header *))
  34. {
  35. u8 *buf, *data;
  36. int i = 0;
  37. buf = dmi_ioremap(base, len);
  38. if (buf == NULL)
  39. return -1;
  40. data = buf;
  41. /*
  42. * Stop when we see all the items the table claimed to have
  43. * OR we run off the end of the table (also happens)
  44. */
  45. while ((i < num) && (data - buf + sizeof(struct dmi_header)) <= len) {
  46. struct dmi_header *dm = (struct dmi_header *)data;
  47. /*
  48. * We want to know the total length (formated area and strings)
  49. * before decoding to make sure we won't run off the table in
  50. * dmi_decode or dmi_string
  51. */
  52. data += dm->length;
  53. while ((data - buf < len - 1) && (data[0] || data[1]))
  54. data++;
  55. if (data - buf < len - 1)
  56. decode(dm);
  57. data += 2;
  58. i++;
  59. }
  60. dmi_iounmap(buf, len);
  61. return 0;
  62. }
  63. static int __init dmi_checksum(u8 *buf)
  64. {
  65. u8 sum = 0;
  66. int a;
  67. for (a = 0; a < 15; a++)
  68. sum += buf[a];
  69. return sum == 0;
  70. }
  71. static char *dmi_ident[DMI_STRING_MAX];
  72. static LIST_HEAD(dmi_devices);
  73. /*
  74. * Save a DMI string
  75. */
  76. static void __init dmi_save_ident(struct dmi_header *dm, int slot, int string)
  77. {
  78. char *p, *d = (char*) dm;
  79. if (dmi_ident[slot])
  80. return;
  81. p = dmi_string(dm, d[string]);
  82. if (p == NULL)
  83. return;
  84. dmi_ident[slot] = p;
  85. }
  86. static void __init dmi_save_devices(struct dmi_header *dm)
  87. {
  88. int i, count = (dm->length - sizeof(struct dmi_header)) / 2;
  89. struct dmi_device *dev;
  90. for (i = 0; i < count; i++) {
  91. char *d = ((char *) dm) + (i * 2);
  92. /* Skip disabled device */
  93. if ((*d & 0x80) == 0)
  94. continue;
  95. dev = dmi_alloc(sizeof(*dev));
  96. if (!dev) {
  97. printk(KERN_ERR "dmi_save_devices: out of memory.\n");
  98. break;
  99. }
  100. dev->type = *d++ & 0x7f;
  101. dev->name = dmi_string(dm, *d);
  102. dev->device_data = NULL;
  103. list_add(&dev->list, &dmi_devices);
  104. }
  105. }
  106. static void __init dmi_save_ipmi_device(struct dmi_header *dm)
  107. {
  108. struct dmi_device *dev;
  109. void * data;
  110. data = dmi_alloc(dm->length);
  111. if (data == NULL) {
  112. printk(KERN_ERR "dmi_save_ipmi_device: out of memory.\n");
  113. return;
  114. }
  115. memcpy(data, dm, dm->length);
  116. dev = dmi_alloc(sizeof(*dev));
  117. if (!dev) {
  118. printk(KERN_ERR "dmi_save_ipmi_device: out of memory.\n");
  119. return;
  120. }
  121. dev->type = DMI_DEV_TYPE_IPMI;
  122. dev->name = "IPMI controller";
  123. dev->device_data = data;
  124. list_add(&dev->list, &dmi_devices);
  125. }
  126. /*
  127. * Process a DMI table entry. Right now all we care about are the BIOS
  128. * and machine entries. For 2.5 we should pull the smbus controller info
  129. * out of here.
  130. */
  131. static void __init dmi_decode(struct dmi_header *dm)
  132. {
  133. switch(dm->type) {
  134. case 0: /* BIOS Information */
  135. dmi_save_ident(dm, DMI_BIOS_VENDOR, 4);
  136. dmi_save_ident(dm, DMI_BIOS_VERSION, 5);
  137. dmi_save_ident(dm, DMI_BIOS_DATE, 8);
  138. break;
  139. case 1: /* System Information */
  140. dmi_save_ident(dm, DMI_SYS_VENDOR, 4);
  141. dmi_save_ident(dm, DMI_PRODUCT_NAME, 5);
  142. dmi_save_ident(dm, DMI_PRODUCT_VERSION, 6);
  143. dmi_save_ident(dm, DMI_PRODUCT_SERIAL, 7);
  144. break;
  145. case 2: /* Base Board Information */
  146. dmi_save_ident(dm, DMI_BOARD_VENDOR, 4);
  147. dmi_save_ident(dm, DMI_BOARD_NAME, 5);
  148. dmi_save_ident(dm, DMI_BOARD_VERSION, 6);
  149. break;
  150. case 10: /* Onboard Devices Information */
  151. dmi_save_devices(dm);
  152. break;
  153. case 38: /* IPMI Device Information */
  154. dmi_save_ipmi_device(dm);
  155. }
  156. }
  157. void __init dmi_scan_machine(void)
  158. {
  159. u8 buf[15];
  160. char __iomem *p, *q;
  161. /*
  162. * no iounmap() for that ioremap(); it would be a no-op, but it's
  163. * so early in setup that sucker gets confused into doing what
  164. * it shouldn't if we actually call it.
  165. */
  166. p = ioremap(0xF0000, 0x10000);
  167. if (p == NULL)
  168. goto out;
  169. for (q = p; q < p + 0x10000; q += 16) {
  170. memcpy_fromio(buf, q, 15);
  171. if ((memcmp(buf, "_DMI_", 5) == 0) && dmi_checksum(buf)) {
  172. u16 num = (buf[13] << 8) | buf[12];
  173. u16 len = (buf[7] << 8) | buf[6];
  174. u32 base = (buf[11] << 24) | (buf[10] << 16) |
  175. (buf[9] << 8) | buf[8];
  176. /*
  177. * DMI version 0.0 means that the real version is taken from
  178. * the SMBIOS version, which we don't know at this point.
  179. */
  180. if (buf[14] != 0)
  181. printk(KERN_INFO "DMI %d.%d present.\n",
  182. buf[14] >> 4, buf[14] & 0xF);
  183. else
  184. printk(KERN_INFO "DMI present.\n");
  185. if (dmi_table(base,len, num, dmi_decode) == 0)
  186. return;
  187. }
  188. }
  189. out: printk(KERN_INFO "DMI not present or invalid.\n");
  190. }
  191. /**
  192. * dmi_check_system - check system DMI data
  193. * @list: array of dmi_system_id structures to match against
  194. *
  195. * Walk the blacklist table running matching functions until someone
  196. * returns non zero or we hit the end. Callback function is called for
  197. * each successfull match. Returns the number of matches.
  198. */
  199. int dmi_check_system(struct dmi_system_id *list)
  200. {
  201. int i, count = 0;
  202. struct dmi_system_id *d = list;
  203. while (d->ident) {
  204. for (i = 0; i < ARRAY_SIZE(d->matches); i++) {
  205. int s = d->matches[i].slot;
  206. if (s == DMI_NONE)
  207. continue;
  208. if (dmi_ident[s] && strstr(dmi_ident[s], d->matches[i].substr))
  209. continue;
  210. /* No match */
  211. goto fail;
  212. }
  213. count++;
  214. if (d->callback && d->callback(d))
  215. break;
  216. fail: d++;
  217. }
  218. return count;
  219. }
  220. EXPORT_SYMBOL(dmi_check_system);
  221. /**
  222. * dmi_get_system_info - return DMI data value
  223. * @field: data index (see enum dmi_filed)
  224. *
  225. * Returns one DMI data value, can be used to perform
  226. * complex DMI data checks.
  227. */
  228. char *dmi_get_system_info(int field)
  229. {
  230. return dmi_ident[field];
  231. }
  232. EXPORT_SYMBOL(dmi_get_system_info);
  233. /**
  234. * dmi_find_device - find onboard device by type/name
  235. * @type: device type or %DMI_DEV_TYPE_ANY to match all device types
  236. * @desc: device name string or %NULL to match all
  237. * @from: previous device found in search, or %NULL for new search.
  238. *
  239. * Iterates through the list of known onboard devices. If a device is
  240. * found with a matching @vendor and @device, a pointer to its device
  241. * structure is returned. Otherwise, %NULL is returned.
  242. * A new search is initiated by passing %NULL to the @from argument.
  243. * If @from is not %NULL, searches continue from next device.
  244. */
  245. struct dmi_device * dmi_find_device(int type, const char *name,
  246. struct dmi_device *from)
  247. {
  248. struct list_head *d, *head = from ? &from->list : &dmi_devices;
  249. for(d = head->next; d != &dmi_devices; d = d->next) {
  250. struct dmi_device *dev = list_entry(d, struct dmi_device, list);
  251. if (((type == DMI_DEV_TYPE_ANY) || (dev->type == type)) &&
  252. ((name == NULL) || (strcmp(dev->name, name) == 0)))
  253. return dev;
  254. }
  255. return NULL;
  256. }
  257. EXPORT_SYMBOL(dmi_find_device);