dmi_scan.c 11 KB

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