tpm-sysfs.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /*
  2. * Copyright (C) 2004 IBM Corporation
  3. * Authors:
  4. * Leendert van Doorn <leendert@watson.ibm.com>
  5. * Dave Safford <safford@watson.ibm.com>
  6. * Reiner Sailer <sailer@watson.ibm.com>
  7. * Kylene Hall <kjhall@us.ibm.com>
  8. *
  9. * Copyright (C) 2013 Obsidian Research Corp
  10. * Jason Gunthorpe <jgunthorpe@obsidianresearch.com>
  11. *
  12. * sysfs filesystem inspection interface to the TPM
  13. *
  14. * This program is free software; you can redistribute it and/or
  15. * modify it under the terms of the GNU General Public License as
  16. * published by the Free Software Foundation, version 2 of the
  17. * License.
  18. *
  19. */
  20. #include <linux/device.h>
  21. #include "tpm.h"
  22. struct tpm_readpubek_out {
  23. u8 algorithm[4];
  24. u8 encscheme[2];
  25. u8 sigscheme[2];
  26. __be32 paramsize;
  27. u8 parameters[12];
  28. __be32 keysize;
  29. u8 modulus[256];
  30. u8 checksum[20];
  31. } __packed;
  32. #define READ_PUBEK_RESULT_MIN_BODY_SIZE (28 + 256)
  33. #define TPM_ORD_READPUBEK 124
  34. static ssize_t pubek_show(struct device *dev, struct device_attribute *attr,
  35. char *buf)
  36. {
  37. struct tpm_buf tpm_buf;
  38. struct tpm_readpubek_out *out;
  39. int i;
  40. char *str = buf;
  41. struct tpm_chip *chip = to_tpm_chip(dev);
  42. char anti_replay[20];
  43. memset(&anti_replay, 0, sizeof(anti_replay));
  44. if (tpm_try_get_ops(chip))
  45. return 0;
  46. if (tpm_buf_init(&tpm_buf, TPM_TAG_RQU_COMMAND, TPM_ORD_READPUBEK))
  47. goto out_ops;
  48. tpm_buf_append(&tpm_buf, anti_replay, sizeof(anti_replay));
  49. if (tpm_transmit_cmd(chip, NULL, tpm_buf.data, PAGE_SIZE,
  50. READ_PUBEK_RESULT_MIN_BODY_SIZE, 0,
  51. "attempting to read the PUBEK"))
  52. goto out_buf;
  53. out = (struct tpm_readpubek_out *)&tpm_buf.data[10];
  54. str +=
  55. sprintf(str,
  56. "Algorithm: %02X %02X %02X %02X\n"
  57. "Encscheme: %02X %02X\n"
  58. "Sigscheme: %02X %02X\n"
  59. "Parameters: %02X %02X %02X %02X "
  60. "%02X %02X %02X %02X "
  61. "%02X %02X %02X %02X\n"
  62. "Modulus length: %d\n"
  63. "Modulus:\n",
  64. out->algorithm[0], out->algorithm[1], out->algorithm[2],
  65. out->algorithm[3],
  66. out->encscheme[0], out->encscheme[1],
  67. out->sigscheme[0], out->sigscheme[1],
  68. out->parameters[0], out->parameters[1],
  69. out->parameters[2], out->parameters[3],
  70. out->parameters[4], out->parameters[5],
  71. out->parameters[6], out->parameters[7],
  72. out->parameters[8], out->parameters[9],
  73. out->parameters[10], out->parameters[11],
  74. be32_to_cpu(out->keysize));
  75. for (i = 0; i < 256; i++) {
  76. str += sprintf(str, "%02X ", out->modulus[i]);
  77. if ((i + 1) % 16 == 0)
  78. str += sprintf(str, "\n");
  79. }
  80. out_buf:
  81. tpm_buf_destroy(&tpm_buf);
  82. out_ops:
  83. tpm_put_ops(chip);
  84. return str - buf;
  85. }
  86. static DEVICE_ATTR_RO(pubek);
  87. static ssize_t pcrs_show(struct device *dev, struct device_attribute *attr,
  88. char *buf)
  89. {
  90. cap_t cap;
  91. u8 digest[TPM_DIGEST_SIZE];
  92. ssize_t rc;
  93. int i, j, num_pcrs;
  94. char *str = buf;
  95. struct tpm_chip *chip = to_tpm_chip(dev);
  96. if (tpm_try_get_ops(chip))
  97. return 0;
  98. if (tpm_getcap(chip, TPM_CAP_PROP_PCR, &cap,
  99. "attempting to determine the number of PCRS",
  100. sizeof(cap.num_pcrs))) {
  101. tpm_put_ops(chip);
  102. return 0;
  103. }
  104. num_pcrs = be32_to_cpu(cap.num_pcrs);
  105. for (i = 0; i < num_pcrs; i++) {
  106. rc = tpm_pcr_read_dev(chip, i, digest);
  107. if (rc)
  108. break;
  109. str += sprintf(str, "PCR-%02d: ", i);
  110. for (j = 0; j < TPM_DIGEST_SIZE; j++)
  111. str += sprintf(str, "%02X ", digest[j]);
  112. str += sprintf(str, "\n");
  113. }
  114. tpm_put_ops(chip);
  115. return str - buf;
  116. }
  117. static DEVICE_ATTR_RO(pcrs);
  118. static ssize_t enabled_show(struct device *dev, struct device_attribute *attr,
  119. char *buf)
  120. {
  121. struct tpm_chip *chip = to_tpm_chip(dev);
  122. ssize_t rc = 0;
  123. cap_t cap;
  124. if (tpm_try_get_ops(chip))
  125. return 0;
  126. if (tpm_getcap(chip, TPM_CAP_FLAG_PERM, &cap,
  127. "attempting to determine the permanent enabled state",
  128. sizeof(cap.perm_flags)))
  129. goto out_ops;
  130. rc = sprintf(buf, "%d\n", !cap.perm_flags.disable);
  131. out_ops:
  132. tpm_put_ops(chip);
  133. return rc;
  134. }
  135. static DEVICE_ATTR_RO(enabled);
  136. static ssize_t active_show(struct device *dev, struct device_attribute *attr,
  137. char *buf)
  138. {
  139. struct tpm_chip *chip = to_tpm_chip(dev);
  140. ssize_t rc = 0;
  141. cap_t cap;
  142. if (tpm_try_get_ops(chip))
  143. return 0;
  144. if (tpm_getcap(chip, TPM_CAP_FLAG_PERM, &cap,
  145. "attempting to determine the permanent active state",
  146. sizeof(cap.perm_flags)))
  147. goto out_ops;
  148. rc = sprintf(buf, "%d\n", !cap.perm_flags.deactivated);
  149. out_ops:
  150. tpm_put_ops(chip);
  151. return rc;
  152. }
  153. static DEVICE_ATTR_RO(active);
  154. static ssize_t owned_show(struct device *dev, struct device_attribute *attr,
  155. char *buf)
  156. {
  157. struct tpm_chip *chip = to_tpm_chip(dev);
  158. ssize_t rc = 0;
  159. cap_t cap;
  160. if (tpm_try_get_ops(chip))
  161. return 0;
  162. if (tpm_getcap(to_tpm_chip(dev), TPM_CAP_PROP_OWNER, &cap,
  163. "attempting to determine the owner state",
  164. sizeof(cap.owned)))
  165. goto out_ops;
  166. rc = sprintf(buf, "%d\n", cap.owned);
  167. out_ops:
  168. tpm_put_ops(chip);
  169. return rc;
  170. }
  171. static DEVICE_ATTR_RO(owned);
  172. static ssize_t temp_deactivated_show(struct device *dev,
  173. struct device_attribute *attr, char *buf)
  174. {
  175. struct tpm_chip *chip = to_tpm_chip(dev);
  176. ssize_t rc = 0;
  177. cap_t cap;
  178. if (tpm_try_get_ops(chip))
  179. return 0;
  180. if (tpm_getcap(to_tpm_chip(dev), TPM_CAP_FLAG_VOL, &cap,
  181. "attempting to determine the temporary state",
  182. sizeof(cap.stclear_flags)))
  183. goto out_ops;
  184. rc = sprintf(buf, "%d\n", cap.stclear_flags.deactivated);
  185. out_ops:
  186. tpm_put_ops(chip);
  187. return rc;
  188. }
  189. static DEVICE_ATTR_RO(temp_deactivated);
  190. static ssize_t caps_show(struct device *dev, struct device_attribute *attr,
  191. char *buf)
  192. {
  193. struct tpm_chip *chip = to_tpm_chip(dev);
  194. ssize_t rc = 0;
  195. char *str = buf;
  196. cap_t cap;
  197. if (tpm_try_get_ops(chip))
  198. return 0;
  199. if (tpm_getcap(chip, TPM_CAP_PROP_MANUFACTURER, &cap,
  200. "attempting to determine the manufacturer",
  201. sizeof(cap.manufacturer_id)))
  202. goto out_ops;
  203. str += sprintf(str, "Manufacturer: 0x%x\n",
  204. be32_to_cpu(cap.manufacturer_id));
  205. /* Try to get a TPM version 1.2 TPM_CAP_VERSION_INFO */
  206. rc = tpm_getcap(chip, TPM_CAP_VERSION_1_2, &cap,
  207. "attempting to determine the 1.2 version",
  208. sizeof(cap.tpm_version_1_2));
  209. if (!rc) {
  210. str += sprintf(str,
  211. "TCG version: %d.%d\nFirmware version: %d.%d\n",
  212. cap.tpm_version_1_2.Major,
  213. cap.tpm_version_1_2.Minor,
  214. cap.tpm_version_1_2.revMajor,
  215. cap.tpm_version_1_2.revMinor);
  216. } else {
  217. /* Otherwise just use TPM_STRUCT_VER */
  218. if (tpm_getcap(chip, TPM_CAP_VERSION_1_1, &cap,
  219. "attempting to determine the 1.1 version",
  220. sizeof(cap.tpm_version)))
  221. goto out_ops;
  222. str += sprintf(str,
  223. "TCG version: %d.%d\nFirmware version: %d.%d\n",
  224. cap.tpm_version.Major,
  225. cap.tpm_version.Minor,
  226. cap.tpm_version.revMajor,
  227. cap.tpm_version.revMinor);
  228. }
  229. rc = str - buf;
  230. out_ops:
  231. tpm_put_ops(chip);
  232. return rc;
  233. }
  234. static DEVICE_ATTR_RO(caps);
  235. static ssize_t cancel_store(struct device *dev, struct device_attribute *attr,
  236. const char *buf, size_t count)
  237. {
  238. struct tpm_chip *chip = to_tpm_chip(dev);
  239. if (tpm_try_get_ops(chip))
  240. return 0;
  241. chip->ops->cancel(chip);
  242. tpm_put_ops(chip);
  243. return count;
  244. }
  245. static DEVICE_ATTR_WO(cancel);
  246. static ssize_t durations_show(struct device *dev, struct device_attribute *attr,
  247. char *buf)
  248. {
  249. struct tpm_chip *chip = to_tpm_chip(dev);
  250. if (chip->duration[TPM_LONG] == 0)
  251. return 0;
  252. return sprintf(buf, "%d %d %d [%s]\n",
  253. jiffies_to_usecs(chip->duration[TPM_SHORT]),
  254. jiffies_to_usecs(chip->duration[TPM_MEDIUM]),
  255. jiffies_to_usecs(chip->duration[TPM_LONG]),
  256. chip->duration_adjusted
  257. ? "adjusted" : "original");
  258. }
  259. static DEVICE_ATTR_RO(durations);
  260. static ssize_t timeouts_show(struct device *dev, struct device_attribute *attr,
  261. char *buf)
  262. {
  263. struct tpm_chip *chip = to_tpm_chip(dev);
  264. return sprintf(buf, "%d %d %d %d [%s]\n",
  265. jiffies_to_usecs(chip->timeout_a),
  266. jiffies_to_usecs(chip->timeout_b),
  267. jiffies_to_usecs(chip->timeout_c),
  268. jiffies_to_usecs(chip->timeout_d),
  269. chip->timeout_adjusted
  270. ? "adjusted" : "original");
  271. }
  272. static DEVICE_ATTR_RO(timeouts);
  273. static struct attribute *tpm_dev_attrs[] = {
  274. &dev_attr_pubek.attr,
  275. &dev_attr_pcrs.attr,
  276. &dev_attr_enabled.attr,
  277. &dev_attr_active.attr,
  278. &dev_attr_owned.attr,
  279. &dev_attr_temp_deactivated.attr,
  280. &dev_attr_caps.attr,
  281. &dev_attr_cancel.attr,
  282. &dev_attr_durations.attr,
  283. &dev_attr_timeouts.attr,
  284. NULL,
  285. };
  286. static const struct attribute_group tpm_dev_group = {
  287. .attrs = tpm_dev_attrs,
  288. };
  289. void tpm_sysfs_add_device(struct tpm_chip *chip)
  290. {
  291. /* XXX: If you wish to remove this restriction, you must first update
  292. * tpm_sysfs to explicitly lock chip->ops.
  293. */
  294. if (chip->flags & TPM_CHIP_FLAG_TPM2)
  295. return;
  296. /* The sysfs routines rely on an implicit tpm_try_get_ops, device_del
  297. * is called before ops is null'd and the sysfs core synchronizes this
  298. * removal so that no callbacks are running or can run again
  299. */
  300. WARN_ON(chip->groups_cnt != 0);
  301. chip->groups[chip->groups_cnt++] = &tpm_dev_group;
  302. }