tpm-sysfs.c 7.9 KB

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