dmi_scan.c 11 KB

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