dmi_scan.c 13 KB

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