dmi_scan.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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/efi.h>
  7. #include <linux/bootmem.h>
  8. #include <linux/slab.h>
  9. #include <asm/dmi.h>
  10. static char * __init dmi_string(struct dmi_header *dm, u8 s)
  11. {
  12. u8 *bp = ((u8 *) dm) + dm->length;
  13. char *str = "";
  14. if (s) {
  15. s--;
  16. while (s > 0 && *bp) {
  17. bp += strlen(bp) + 1;
  18. s--;
  19. }
  20. if (*bp != 0) {
  21. str = dmi_alloc(strlen(bp) + 1);
  22. if (str != NULL)
  23. strcpy(str, bp);
  24. else
  25. printk(KERN_ERR "dmi_string: out of memory.\n");
  26. }
  27. }
  28. return str;
  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 = dmi_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. dmi_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. static LIST_HEAD(dmi_devices);
  75. /*
  76. * Save a DMI string
  77. */
  78. static void __init dmi_save_ident(struct dmi_header *dm, int slot, int string)
  79. {
  80. char *p, *d = (char*) dm;
  81. if (dmi_ident[slot])
  82. return;
  83. p = dmi_string(dm, d[string]);
  84. if (p == NULL)
  85. return;
  86. dmi_ident[slot] = p;
  87. }
  88. static void __init dmi_save_devices(struct dmi_header *dm)
  89. {
  90. int i, count = (dm->length - sizeof(struct dmi_header)) / 2;
  91. struct dmi_device *dev;
  92. for (i = 0; i < count; i++) {
  93. char *d = (char *)(dm + 1) + (i * 2);
  94. /* Skip disabled device */
  95. if ((*d & 0x80) == 0)
  96. continue;
  97. dev = dmi_alloc(sizeof(*dev));
  98. if (!dev) {
  99. printk(KERN_ERR "dmi_save_devices: out of memory.\n");
  100. break;
  101. }
  102. dev->type = *d++ & 0x7f;
  103. dev->name = dmi_string(dm, *d);
  104. dev->device_data = NULL;
  105. list_add(&dev->list, &dmi_devices);
  106. }
  107. }
  108. static void __init dmi_save_oem_strings_devices(struct dmi_header *dm)
  109. {
  110. int i, count = *(u8 *)(dm + 1);
  111. struct dmi_device *dev;
  112. for (i = 1; i <= count; i++) {
  113. dev = dmi_alloc(sizeof(*dev));
  114. if (!dev) {
  115. printk(KERN_ERR
  116. "dmi_save_oem_strings_devices: out of memory.\n");
  117. break;
  118. }
  119. dev->type = DMI_DEV_TYPE_OEM_STRING;
  120. dev->name = dmi_string(dm, i);
  121. dev->device_data = NULL;
  122. list_add(&dev->list, &dmi_devices);
  123. }
  124. }
  125. static void __init dmi_save_ipmi_device(struct dmi_header *dm)
  126. {
  127. struct dmi_device *dev;
  128. void * data;
  129. data = dmi_alloc(dm->length);
  130. if (data == NULL) {
  131. printk(KERN_ERR "dmi_save_ipmi_device: out of memory.\n");
  132. return;
  133. }
  134. memcpy(data, dm, dm->length);
  135. dev = dmi_alloc(sizeof(*dev));
  136. if (!dev) {
  137. printk(KERN_ERR "dmi_save_ipmi_device: out of memory.\n");
  138. return;
  139. }
  140. dev->type = DMI_DEV_TYPE_IPMI;
  141. dev->name = "IPMI controller";
  142. dev->device_data = data;
  143. list_add(&dev->list, &dmi_devices);
  144. }
  145. /*
  146. * Process a DMI table entry. Right now all we care about are the BIOS
  147. * and machine entries. For 2.5 we should pull the smbus controller info
  148. * out of here.
  149. */
  150. static void __init dmi_decode(struct dmi_header *dm)
  151. {
  152. switch(dm->type) {
  153. case 0: /* BIOS Information */
  154. dmi_save_ident(dm, DMI_BIOS_VENDOR, 4);
  155. dmi_save_ident(dm, DMI_BIOS_VERSION, 5);
  156. dmi_save_ident(dm, DMI_BIOS_DATE, 8);
  157. break;
  158. case 1: /* System Information */
  159. dmi_save_ident(dm, DMI_SYS_VENDOR, 4);
  160. dmi_save_ident(dm, DMI_PRODUCT_NAME, 5);
  161. dmi_save_ident(dm, DMI_PRODUCT_VERSION, 6);
  162. dmi_save_ident(dm, DMI_PRODUCT_SERIAL, 7);
  163. break;
  164. case 2: /* Base Board Information */
  165. dmi_save_ident(dm, DMI_BOARD_VENDOR, 4);
  166. dmi_save_ident(dm, DMI_BOARD_NAME, 5);
  167. dmi_save_ident(dm, DMI_BOARD_VERSION, 6);
  168. break;
  169. case 10: /* Onboard Devices Information */
  170. dmi_save_devices(dm);
  171. break;
  172. case 11: /* OEM Strings */
  173. dmi_save_oem_strings_devices(dm);
  174. break;
  175. case 38: /* IPMI Device Information */
  176. dmi_save_ipmi_device(dm);
  177. }
  178. }
  179. static int __init dmi_present(char __iomem *p)
  180. {
  181. u8 buf[15];
  182. memcpy_fromio(buf, p, 15);
  183. if ((memcmp(buf, "_DMI_", 5) == 0) && dmi_checksum(buf)) {
  184. u16 num = (buf[13] << 8) | buf[12];
  185. u16 len = (buf[7] << 8) | buf[6];
  186. u32 base = (buf[11] << 24) | (buf[10] << 16) |
  187. (buf[9] << 8) | buf[8];
  188. /*
  189. * DMI version 0.0 means that the real version is taken from
  190. * the SMBIOS version, which we don't know at this point.
  191. */
  192. if (buf[14] != 0)
  193. printk(KERN_INFO "DMI %d.%d present.\n",
  194. buf[14] >> 4, buf[14] & 0xF);
  195. else
  196. printk(KERN_INFO "DMI present.\n");
  197. if (dmi_table(base,len, num, dmi_decode) == 0)
  198. return 0;
  199. }
  200. return 1;
  201. }
  202. void __init dmi_scan_machine(void)
  203. {
  204. char __iomem *p, *q;
  205. int rc;
  206. if (efi_enabled) {
  207. if (efi.smbios == EFI_INVALID_TABLE_ADDR)
  208. goto out;
  209. /* This is called as a core_initcall() because it isn't
  210. * needed during early boot. This also means we can
  211. * iounmap the space when we're done with it.
  212. */
  213. p = dmi_ioremap(efi.smbios, 32);
  214. if (p == NULL)
  215. goto out;
  216. rc = dmi_present(p + 0x10); /* offset of _DMI_ string */
  217. dmi_iounmap(p, 32);
  218. if (!rc)
  219. return;
  220. }
  221. else {
  222. /*
  223. * no iounmap() for that ioremap(); it would be a no-op, but
  224. * it's so early in setup that sucker gets confused into doing
  225. * what it shouldn't if we actually call it.
  226. */
  227. p = dmi_ioremap(0xF0000, 0x10000);
  228. if (p == NULL)
  229. goto out;
  230. for (q = p; q < p + 0x10000; q += 16) {
  231. rc = dmi_present(q);
  232. if (!rc)
  233. return;
  234. }
  235. }
  236. out: printk(KERN_INFO "DMI not present or invalid.\n");
  237. }
  238. /**
  239. * dmi_check_system - check system DMI data
  240. * @list: array of dmi_system_id structures to match against
  241. * All non-null elements of the list must match
  242. * their slot's (field index's) data (i.e., each
  243. * list string must be a substring of the specified
  244. * DMI slot's string data) to be considered a
  245. * successful match.
  246. *
  247. * Walk the blacklist table running matching functions until someone
  248. * returns non zero or we hit the end. Callback function is called for
  249. * each successful match. Returns the number of matches.
  250. */
  251. int dmi_check_system(struct dmi_system_id *list)
  252. {
  253. int i, count = 0;
  254. struct dmi_system_id *d = list;
  255. while (d->ident) {
  256. for (i = 0; i < ARRAY_SIZE(d->matches); i++) {
  257. int s = d->matches[i].slot;
  258. if (s == DMI_NONE)
  259. continue;
  260. if (dmi_ident[s] && strstr(dmi_ident[s], d->matches[i].substr))
  261. continue;
  262. /* No match */
  263. goto fail;
  264. }
  265. count++;
  266. if (d->callback && d->callback(d))
  267. break;
  268. fail: d++;
  269. }
  270. return count;
  271. }
  272. EXPORT_SYMBOL(dmi_check_system);
  273. /**
  274. * dmi_get_system_info - return DMI data value
  275. * @field: data index (see enum dmi_field)
  276. *
  277. * Returns one DMI data value, can be used to perform
  278. * complex DMI data checks.
  279. */
  280. char *dmi_get_system_info(int field)
  281. {
  282. return dmi_ident[field];
  283. }
  284. EXPORT_SYMBOL(dmi_get_system_info);
  285. /**
  286. * dmi_name_in_vendors - Check if string is anywhere in the DMI vendor information.
  287. * @str: Case sensitive Name
  288. */
  289. int dmi_name_in_vendors(char *str)
  290. {
  291. static int fields[] = { DMI_BIOS_VENDOR, DMI_BIOS_VERSION, DMI_SYS_VENDOR,
  292. DMI_PRODUCT_NAME, DMI_PRODUCT_VERSION, DMI_BOARD_VENDOR,
  293. DMI_BOARD_NAME, DMI_BOARD_VERSION, DMI_NONE };
  294. int i;
  295. for (i = 0; fields[i] != DMI_NONE; i++) {
  296. int f = fields[i];
  297. if (dmi_ident[f] && strstr(dmi_ident[f], str))
  298. return 1;
  299. }
  300. return 0;
  301. }
  302. EXPORT_SYMBOL(dmi_name_in_vendors);
  303. /**
  304. * dmi_find_device - find onboard device by type/name
  305. * @type: device type or %DMI_DEV_TYPE_ANY to match all device types
  306. * @name: device name string or %NULL to match all
  307. * @from: previous device found in search, or %NULL for new search.
  308. *
  309. * Iterates through the list of known onboard devices. If a device is
  310. * found with a matching @vendor and @device, a pointer to its device
  311. * structure is returned. Otherwise, %NULL is returned.
  312. * A new search is initiated by passing %NULL as the @from argument.
  313. * If @from is not %NULL, searches continue from next device.
  314. */
  315. struct dmi_device * dmi_find_device(int type, const char *name,
  316. struct dmi_device *from)
  317. {
  318. struct list_head *d, *head = from ? &from->list : &dmi_devices;
  319. for(d = head->next; d != &dmi_devices; d = d->next) {
  320. struct dmi_device *dev = list_entry(d, struct dmi_device, list);
  321. if (((type == DMI_DEV_TYPE_ANY) || (dev->type == type)) &&
  322. ((name == NULL) || (strcmp(dev->name, name) == 0)))
  323. return dev;
  324. }
  325. return NULL;
  326. }
  327. EXPORT_SYMBOL(dmi_find_device);
  328. /**
  329. * dmi_get_year - Return year of a DMI date
  330. * @field: data index (like dmi_get_system_info)
  331. *
  332. * Returns -1 when the field doesn't exist. 0 when it is broken.
  333. */
  334. int dmi_get_year(int field)
  335. {
  336. int year;
  337. char *s = dmi_get_system_info(field);
  338. if (!s)
  339. return -1;
  340. if (*s == '\0')
  341. return 0;
  342. s = strrchr(s, '/');
  343. if (!s)
  344. return 0;
  345. s += 1;
  346. year = simple_strtoul(s, NULL, 0);
  347. if (year && year < 100) { /* 2-digit year */
  348. year += 1900;
  349. if (year < 1996) /* no dates < spec 1.0 */
  350. year += 100;
  351. }
  352. return year;
  353. }