dmi_scan.c 14 KB

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