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