dmi_scan.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. struct dmi_header {
  8. u8 type;
  9. u8 length;
  10. u16 handle;
  11. };
  12. #undef DMI_DEBUG
  13. #ifdef DMI_DEBUG
  14. #define dmi_printk(x) printk x
  15. #else
  16. #define dmi_printk(x)
  17. #endif
  18. static char * __init dmi_string(struct dmi_header *dm, u8 s)
  19. {
  20. u8 *bp = ((u8 *) dm) + dm->length;
  21. if (!s)
  22. return "";
  23. s--;
  24. while (s > 0 && *bp) {
  25. bp += strlen(bp) + 1;
  26. s--;
  27. }
  28. return bp;
  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 = bt_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. bt_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 int __init dmi_iterate(void (*decode)(struct dmi_header *))
  74. {
  75. u8 buf[15];
  76. char __iomem *p, *q;
  77. /*
  78. * no iounmap() for that ioremap(); it would be a no-op, but it's
  79. * so early in setup that sucker gets confused into doing what
  80. * it shouldn't if we actually call it.
  81. */
  82. p = ioremap(0xF0000, 0x10000);
  83. if (p == NULL)
  84. return -1;
  85. for (q = p; q < p + 0x10000; q += 16) {
  86. memcpy_fromio(buf, q, 15);
  87. if ((memcmp(buf, "_DMI_", 5) == 0) && dmi_checksum(buf)) {
  88. u16 num = (buf[13] << 8) | buf[12];
  89. u16 len = (buf[7] << 8) | buf[6];
  90. u32 base = (buf[11] << 24) | (buf[10] << 16) |
  91. (buf[9] << 8) | buf[8];
  92. /*
  93. * DMI version 0.0 means that the real version is taken from
  94. * the SMBIOS version, which we don't know at this point.
  95. */
  96. if (buf[14] != 0)
  97. printk(KERN_INFO "DMI %d.%d present.\n",
  98. buf[14] >> 4, buf[14] & 0xF);
  99. else
  100. printk(KERN_INFO "DMI present.\n");
  101. dmi_printk((KERN_INFO "%d structures occupying %d bytes.\n",
  102. num, len));
  103. dmi_printk((KERN_INFO "DMI table at 0x%08X.\n", base));
  104. if (dmi_table(base,len, num, decode) == 0)
  105. return 0;
  106. }
  107. }
  108. return -1;
  109. }
  110. static char *dmi_ident[DMI_STRING_MAX];
  111. /*
  112. * Save a DMI string
  113. */
  114. static void __init dmi_save_ident(struct dmi_header *dm, int slot, int string)
  115. {
  116. char *d = (char*)dm;
  117. char *p = dmi_string(dm, d[string]);
  118. if (p == NULL || *p == 0)
  119. return;
  120. if (dmi_ident[slot])
  121. return;
  122. dmi_ident[slot] = alloc_bootmem(strlen(p) + 1);
  123. if(dmi_ident[slot])
  124. strcpy(dmi_ident[slot], p);
  125. else
  126. printk(KERN_ERR "dmi_save_ident: out of memory.\n");
  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. u8 *data __attribute__((__unused__)) = (u8 *)dm;
  136. switch(dm->type) {
  137. case 0:
  138. dmi_printk(("BIOS Vendor: %s\n", dmi_string(dm, data[4])));
  139. dmi_save_ident(dm, DMI_BIOS_VENDOR, 4);
  140. dmi_printk(("BIOS Version: %s\n", dmi_string(dm, data[5])));
  141. dmi_save_ident(dm, DMI_BIOS_VERSION, 5);
  142. dmi_printk(("BIOS Release: %s\n", dmi_string(dm, data[8])));
  143. dmi_save_ident(dm, DMI_BIOS_DATE, 8);
  144. break;
  145. case 1:
  146. dmi_printk(("System Vendor: %s\n", dmi_string(dm, data[4])));
  147. dmi_save_ident(dm, DMI_SYS_VENDOR, 4);
  148. dmi_printk(("Product Name: %s\n", dmi_string(dm, data[5])));
  149. dmi_save_ident(dm, DMI_PRODUCT_NAME, 5);
  150. dmi_printk(("Version: %s\n", dmi_string(dm, data[6])));
  151. dmi_save_ident(dm, DMI_PRODUCT_VERSION, 6);
  152. dmi_printk(("Serial Number: %s\n", dmi_string(dm, data[7])));
  153. dmi_save_ident(dm, DMI_PRODUCT_SERIAL, 7);
  154. break;
  155. case 2:
  156. dmi_printk(("Board Vendor: %s\n", dmi_string(dm, data[4])));
  157. dmi_save_ident(dm, DMI_BOARD_VENDOR, 4);
  158. dmi_printk(("Board Name: %s\n", dmi_string(dm, data[5])));
  159. dmi_save_ident(dm, DMI_BOARD_NAME, 5);
  160. dmi_printk(("Board Version: %s\n", dmi_string(dm, data[6])));
  161. dmi_save_ident(dm, DMI_BOARD_VERSION, 6);
  162. break;
  163. }
  164. }
  165. void __init dmi_scan_machine(void)
  166. {
  167. if (dmi_iterate(dmi_decode))
  168. printk(KERN_INFO "DMI not present.\n");
  169. }
  170. /**
  171. * dmi_check_system - check system DMI data
  172. * @list: array of dmi_system_id structures to match against
  173. *
  174. * Walk the blacklist table running matching functions until someone
  175. * returns non zero or we hit the end. Callback function is called for
  176. * each successfull match. Returns the number of matches.
  177. */
  178. int dmi_check_system(struct dmi_system_id *list)
  179. {
  180. int i, count = 0;
  181. struct dmi_system_id *d = list;
  182. while (d->ident) {
  183. for (i = 0; i < ARRAY_SIZE(d->matches); i++) {
  184. int s = d->matches[i].slot;
  185. if (s == DMI_NONE)
  186. continue;
  187. if (dmi_ident[s] && strstr(dmi_ident[s], d->matches[i].substr))
  188. continue;
  189. /* No match */
  190. goto fail;
  191. }
  192. if (d->callback && d->callback(d))
  193. break;
  194. count++;
  195. fail: d++;
  196. }
  197. return count;
  198. }
  199. EXPORT_SYMBOL(dmi_check_system);
  200. /**
  201. * dmi_get_system_info - return DMI data value
  202. * @field: data index (see enum dmi_filed)
  203. *
  204. * Returns one DMI data value, can be used to perform
  205. * complex DMI data checks.
  206. */
  207. char *dmi_get_system_info(int field)
  208. {
  209. return dmi_ident[field];
  210. }
  211. EXPORT_SYMBOL(dmi_get_system_info);