dmi_scan.c 12 KB

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