tpm-sysfs.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. ssize_t rc;
  40. int i;
  41. char *str = buf;
  42. struct tpm_chip *chip = to_tpm_chip(dev);
  43. char anti_replay[20];
  44. memset(&anti_replay, 0, sizeof(anti_replay));
  45. rc = tpm_buf_init(&tpm_buf, TPM_TAG_RQU_COMMAND, TPM_ORD_READPUBEK);
  46. if (rc)
  47. return rc;
  48. tpm_buf_append(&tpm_buf, anti_replay, sizeof(anti_replay));
  49. rc = 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. if (rc) {
  53. tpm_buf_destroy(&tpm_buf);
  54. return 0;
  55. }
  56. out = (struct tpm_readpubek_out *)&tpm_buf.data[10];
  57. str +=
  58. sprintf(str,
  59. "Algorithm: %02X %02X %02X %02X\n"
  60. "Encscheme: %02X %02X\n"
  61. "Sigscheme: %02X %02X\n"
  62. "Parameters: %02X %02X %02X %02X "
  63. "%02X %02X %02X %02X "
  64. "%02X %02X %02X %02X\n"
  65. "Modulus length: %d\n"
  66. "Modulus:\n",
  67. out->algorithm[0], out->algorithm[1], out->algorithm[2],
  68. out->algorithm[3],
  69. out->encscheme[0], out->encscheme[1],
  70. out->sigscheme[0], out->sigscheme[1],
  71. out->parameters[0], out->parameters[1],
  72. out->parameters[2], out->parameters[3],
  73. out->parameters[4], out->parameters[5],
  74. out->parameters[6], out->parameters[7],
  75. out->parameters[8], out->parameters[9],
  76. out->parameters[10], out->parameters[11],
  77. be32_to_cpu(out->keysize));
  78. for (i = 0; i < 256; i++) {
  79. str += sprintf(str, "%02X ", out->modulus[i]);
  80. if ((i + 1) % 16 == 0)
  81. str += sprintf(str, "\n");
  82. }
  83. rc = str - buf;
  84. tpm_buf_destroy(&tpm_buf);
  85. return rc;
  86. }
  87. static DEVICE_ATTR_RO(pubek);
  88. static ssize_t pcrs_show(struct device *dev, struct device_attribute *attr,
  89. char *buf)
  90. {
  91. cap_t cap;
  92. u8 digest[TPM_DIGEST_SIZE];
  93. ssize_t rc;
  94. int i, j, num_pcrs;
  95. char *str = buf;
  96. struct tpm_chip *chip = to_tpm_chip(dev);
  97. rc = tpm_getcap(chip, TPM_CAP_PROP_PCR, &cap,
  98. "attempting to determine the number of PCRS",
  99. sizeof(cap.num_pcrs));
  100. if (rc)
  101. return 0;
  102. num_pcrs = be32_to_cpu(cap.num_pcrs);
  103. for (i = 0; i < num_pcrs; i++) {
  104. rc = tpm_pcr_read_dev(chip, i, digest);
  105. if (rc)
  106. break;
  107. str += sprintf(str, "PCR-%02d: ", i);
  108. for (j = 0; j < TPM_DIGEST_SIZE; j++)
  109. str += sprintf(str, "%02X ", digest[j]);
  110. str += sprintf(str, "\n");
  111. }
  112. return str - buf;
  113. }
  114. static DEVICE_ATTR_RO(pcrs);
  115. static ssize_t enabled_show(struct device *dev, struct device_attribute *attr,
  116. char *buf)
  117. {
  118. cap_t cap;
  119. ssize_t rc;
  120. rc = tpm_getcap(to_tpm_chip(dev), TPM_CAP_FLAG_PERM, &cap,
  121. "attempting to determine the permanent enabled state",
  122. sizeof(cap.perm_flags));
  123. if (rc)
  124. return 0;
  125. rc = sprintf(buf, "%d\n", !cap.perm_flags.disable);
  126. return rc;
  127. }
  128. static DEVICE_ATTR_RO(enabled);
  129. static ssize_t active_show(struct device *dev, struct device_attribute *attr,
  130. char *buf)
  131. {
  132. cap_t cap;
  133. ssize_t rc;
  134. rc = tpm_getcap(to_tpm_chip(dev), TPM_CAP_FLAG_PERM, &cap,
  135. "attempting to determine the permanent active state",
  136. sizeof(cap.perm_flags));
  137. if (rc)
  138. return 0;
  139. rc = sprintf(buf, "%d\n", !cap.perm_flags.deactivated);
  140. return rc;
  141. }
  142. static DEVICE_ATTR_RO(active);
  143. static ssize_t owned_show(struct device *dev, struct device_attribute *attr,
  144. char *buf)
  145. {
  146. cap_t cap;
  147. ssize_t rc;
  148. rc = tpm_getcap(to_tpm_chip(dev), TPM_CAP_PROP_OWNER, &cap,
  149. "attempting to determine the owner state",
  150. sizeof(cap.owned));
  151. if (rc)
  152. return 0;
  153. rc = sprintf(buf, "%d\n", cap.owned);
  154. return rc;
  155. }
  156. static DEVICE_ATTR_RO(owned);
  157. static ssize_t temp_deactivated_show(struct device *dev,
  158. struct device_attribute *attr, char *buf)
  159. {
  160. cap_t cap;
  161. ssize_t rc;
  162. rc = tpm_getcap(to_tpm_chip(dev), TPM_CAP_FLAG_VOL, &cap,
  163. "attempting to determine the temporary state",
  164. sizeof(cap.stclear_flags));
  165. if (rc)
  166. return 0;
  167. rc = sprintf(buf, "%d\n", cap.stclear_flags.deactivated);
  168. return rc;
  169. }
  170. static DEVICE_ATTR_RO(temp_deactivated);
  171. static ssize_t caps_show(struct device *dev, struct device_attribute *attr,
  172. char *buf)
  173. {
  174. struct tpm_chip *chip = to_tpm_chip(dev);
  175. cap_t cap;
  176. ssize_t rc;
  177. char *str = buf;
  178. rc = tpm_getcap(chip, TPM_CAP_PROP_MANUFACTURER, &cap,
  179. "attempting to determine the manufacturer",
  180. sizeof(cap.manufacturer_id));
  181. if (rc)
  182. return 0;
  183. str += sprintf(str, "Manufacturer: 0x%x\n",
  184. be32_to_cpu(cap.manufacturer_id));
  185. /* Try to get a TPM version 1.2 TPM_CAP_VERSION_INFO */
  186. rc = tpm_getcap(chip, TPM_CAP_VERSION_1_2, &cap,
  187. "attempting to determine the 1.2 version",
  188. sizeof(cap.tpm_version_1_2));
  189. if (!rc) {
  190. str += sprintf(str,
  191. "TCG version: %d.%d\nFirmware version: %d.%d\n",
  192. cap.tpm_version_1_2.Major,
  193. cap.tpm_version_1_2.Minor,
  194. cap.tpm_version_1_2.revMajor,
  195. cap.tpm_version_1_2.revMinor);
  196. } else {
  197. /* Otherwise just use TPM_STRUCT_VER */
  198. rc = tpm_getcap(chip, TPM_CAP_VERSION_1_1, &cap,
  199. "attempting to determine the 1.1 version",
  200. sizeof(cap.tpm_version));
  201. if (rc)
  202. return 0;
  203. str += sprintf(str,
  204. "TCG version: %d.%d\nFirmware version: %d.%d\n",
  205. cap.tpm_version.Major,
  206. cap.tpm_version.Minor,
  207. cap.tpm_version.revMajor,
  208. cap.tpm_version.revMinor);
  209. }
  210. return str - buf;
  211. }
  212. static DEVICE_ATTR_RO(caps);
  213. static ssize_t cancel_store(struct device *dev, struct device_attribute *attr,
  214. const char *buf, size_t count)
  215. {
  216. struct tpm_chip *chip = to_tpm_chip(dev);
  217. if (chip == NULL)
  218. return 0;
  219. chip->ops->cancel(chip);
  220. return count;
  221. }
  222. static DEVICE_ATTR_WO(cancel);
  223. static ssize_t durations_show(struct device *dev, struct device_attribute *attr,
  224. char *buf)
  225. {
  226. struct tpm_chip *chip = to_tpm_chip(dev);
  227. if (chip->duration[TPM_LONG] == 0)
  228. return 0;
  229. return sprintf(buf, "%d %d %d [%s]\n",
  230. jiffies_to_usecs(chip->duration[TPM_SHORT]),
  231. jiffies_to_usecs(chip->duration[TPM_MEDIUM]),
  232. jiffies_to_usecs(chip->duration[TPM_LONG]),
  233. chip->duration_adjusted
  234. ? "adjusted" : "original");
  235. }
  236. static DEVICE_ATTR_RO(durations);
  237. static ssize_t timeouts_show(struct device *dev, struct device_attribute *attr,
  238. char *buf)
  239. {
  240. struct tpm_chip *chip = to_tpm_chip(dev);
  241. return sprintf(buf, "%d %d %d %d [%s]\n",
  242. jiffies_to_usecs(chip->timeout_a),
  243. jiffies_to_usecs(chip->timeout_b),
  244. jiffies_to_usecs(chip->timeout_c),
  245. jiffies_to_usecs(chip->timeout_d),
  246. chip->timeout_adjusted
  247. ? "adjusted" : "original");
  248. }
  249. static DEVICE_ATTR_RO(timeouts);
  250. static struct attribute *tpm_dev_attrs[] = {
  251. &dev_attr_pubek.attr,
  252. &dev_attr_pcrs.attr,
  253. &dev_attr_enabled.attr,
  254. &dev_attr_active.attr,
  255. &dev_attr_owned.attr,
  256. &dev_attr_temp_deactivated.attr,
  257. &dev_attr_caps.attr,
  258. &dev_attr_cancel.attr,
  259. &dev_attr_durations.attr,
  260. &dev_attr_timeouts.attr,
  261. NULL,
  262. };
  263. static const struct attribute_group tpm_dev_group = {
  264. .attrs = tpm_dev_attrs,
  265. };
  266. void tpm_sysfs_add_device(struct tpm_chip *chip)
  267. {
  268. /* XXX: If you wish to remove this restriction, you must first update
  269. * tpm_sysfs to explicitly lock chip->ops.
  270. */
  271. if (chip->flags & TPM_CHIP_FLAG_TPM2)
  272. return;
  273. /* The sysfs routines rely on an implicit tpm_try_get_ops, device_del
  274. * is called before ops is null'd and the sysfs core synchronizes this
  275. * removal so that no callbacks are running or can run again
  276. */
  277. WARN_ON(chip->groups_cnt != 0);
  278. chip->groups[chip->groups_cnt++] = &tpm_dev_group;
  279. }