dmi_scan.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. static char * __init dmi_string(struct dmi_header *dm, u8 s)
  13. {
  14. u8 *bp = ((u8 *) dm) + dm->length;
  15. char *str = "";
  16. if (s) {
  17. s--;
  18. while (s > 0 && *bp) {
  19. bp += strlen(bp) + 1;
  20. s--;
  21. }
  22. if (*bp != 0) {
  23. str = alloc_bootmem(strlen(bp) + 1);
  24. if (str != NULL)
  25. strcpy(str, bp);
  26. else
  27. printk(KERN_ERR "dmi_string: out of memory.\n");
  28. }
  29. }
  30. return str;
  31. }
  32. /*
  33. * We have to be cautious here. We have seen BIOSes with DMI pointers
  34. * pointing to completely the wrong place for example
  35. */
  36. static int __init dmi_table(u32 base, int len, int num,
  37. void (*decode)(struct dmi_header *))
  38. {
  39. u8 *buf, *data;
  40. int i = 0;
  41. buf = bt_ioremap(base, len);
  42. if (buf == NULL)
  43. return -1;
  44. data = buf;
  45. /*
  46. * Stop when we see all the items the table claimed to have
  47. * OR we run off the end of the table (also happens)
  48. */
  49. while ((i < num) && (data - buf + sizeof(struct dmi_header)) <= len) {
  50. struct dmi_header *dm = (struct dmi_header *)data;
  51. /*
  52. * We want to know the total length (formated area and strings)
  53. * before decoding to make sure we won't run off the table in
  54. * dmi_decode or dmi_string
  55. */
  56. data += dm->length;
  57. while ((data - buf < len - 1) && (data[0] || data[1]))
  58. data++;
  59. if (data - buf < len - 1)
  60. decode(dm);
  61. data += 2;
  62. i++;
  63. }
  64. bt_iounmap(buf, len);
  65. return 0;
  66. }
  67. static int __init dmi_checksum(u8 *buf)
  68. {
  69. u8 sum = 0;
  70. int a;
  71. for (a = 0; a < 15; a++)
  72. sum += buf[a];
  73. return sum == 0;
  74. }
  75. static char *dmi_ident[DMI_STRING_MAX];
  76. /*
  77. * Save a DMI string
  78. */
  79. static void __init dmi_save_ident(struct dmi_header *dm, int slot, int string)
  80. {
  81. char *p, *d = (char*) dm;
  82. if (dmi_ident[slot])
  83. return;
  84. p = dmi_string(dm, d[string]);
  85. if (p == NULL)
  86. return;
  87. dmi_ident[slot] = p;
  88. }
  89. /*
  90. * Process a DMI table entry. Right now all we care about are the BIOS
  91. * and machine entries. For 2.5 we should pull the smbus controller info
  92. * out of here.
  93. */
  94. static void __init dmi_decode(struct dmi_header *dm)
  95. {
  96. u8 *data __attribute__((__unused__)) = (u8 *)dm;
  97. switch(dm->type) {
  98. case 0:
  99. dmi_save_ident(dm, DMI_BIOS_VENDOR, 4);
  100. dmi_save_ident(dm, DMI_BIOS_VERSION, 5);
  101. dmi_save_ident(dm, DMI_BIOS_DATE, 8);
  102. break;
  103. case 1:
  104. dmi_save_ident(dm, DMI_SYS_VENDOR, 4);
  105. dmi_save_ident(dm, DMI_PRODUCT_NAME, 5);
  106. dmi_save_ident(dm, DMI_PRODUCT_VERSION, 6);
  107. dmi_save_ident(dm, DMI_PRODUCT_SERIAL, 7);
  108. break;
  109. case 2:
  110. dmi_save_ident(dm, DMI_BOARD_VENDOR, 4);
  111. dmi_save_ident(dm, DMI_BOARD_NAME, 5);
  112. dmi_save_ident(dm, DMI_BOARD_VERSION, 6);
  113. break;
  114. }
  115. }
  116. void __init dmi_scan_machine(void)
  117. {
  118. u8 buf[15];
  119. char __iomem *p, *q;
  120. /*
  121. * no iounmap() for that ioremap(); it would be a no-op, but it's
  122. * so early in setup that sucker gets confused into doing what
  123. * it shouldn't if we actually call it.
  124. */
  125. p = ioremap(0xF0000, 0x10000);
  126. if (p == NULL)
  127. goto out;
  128. for (q = p; q < p + 0x10000; q += 16) {
  129. memcpy_fromio(buf, q, 15);
  130. if ((memcmp(buf, "_DMI_", 5) == 0) && dmi_checksum(buf)) {
  131. u16 num = (buf[13] << 8) | buf[12];
  132. u16 len = (buf[7] << 8) | buf[6];
  133. u32 base = (buf[11] << 24) | (buf[10] << 16) |
  134. (buf[9] << 8) | buf[8];
  135. /*
  136. * DMI version 0.0 means that the real version is taken from
  137. * the SMBIOS version, which we don't know at this point.
  138. */
  139. if (buf[14] != 0)
  140. printk(KERN_INFO "DMI %d.%d present.\n",
  141. buf[14] >> 4, buf[14] & 0xF);
  142. else
  143. printk(KERN_INFO "DMI present.\n");
  144. if (dmi_table(base,len, num, dmi_decode) == 0)
  145. return;
  146. }
  147. }
  148. out: printk(KERN_INFO "DMI not present.\n");
  149. }
  150. /**
  151. * dmi_check_system - check system DMI data
  152. * @list: array of dmi_system_id structures to match against
  153. *
  154. * Walk the blacklist table running matching functions until someone
  155. * returns non zero or we hit the end. Callback function is called for
  156. * each successfull match. Returns the number of matches.
  157. */
  158. int dmi_check_system(struct dmi_system_id *list)
  159. {
  160. int i, count = 0;
  161. struct dmi_system_id *d = list;
  162. while (d->ident) {
  163. for (i = 0; i < ARRAY_SIZE(d->matches); i++) {
  164. int s = d->matches[i].slot;
  165. if (s == DMI_NONE)
  166. continue;
  167. if (dmi_ident[s] && strstr(dmi_ident[s], d->matches[i].substr))
  168. continue;
  169. /* No match */
  170. goto fail;
  171. }
  172. if (d->callback && d->callback(d))
  173. break;
  174. count++;
  175. fail: d++;
  176. }
  177. return count;
  178. }
  179. EXPORT_SYMBOL(dmi_check_system);
  180. /**
  181. * dmi_get_system_info - return DMI data value
  182. * @field: data index (see enum dmi_filed)
  183. *
  184. * Returns one DMI data value, can be used to perform
  185. * complex DMI data checks.
  186. */
  187. char *dmi_get_system_info(int field)
  188. {
  189. return dmi_ident[field];
  190. }
  191. EXPORT_SYMBOL(dmi_get_system_info);