dmi_scan.c 4.7 KB

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