tpm-sysfs.c 7.9 KB

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