dmi_scan.c 5.6 KB

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