tpm-sysfs.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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 const 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 = to_tpm_chip(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 = to_tpm_chip(dev);
  91. rc = tpm_getcap(chip, 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(to_tpm_chip(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(to_tpm_chip(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(to_tpm_chip(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(to_tpm_chip(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. struct tpm_chip *chip = to_tpm_chip(dev);
  164. cap_t cap;
  165. ssize_t rc;
  166. char *str = buf;
  167. rc = tpm_getcap(chip, TPM_CAP_PROP_MANUFACTURER, &cap,
  168. "attempting to determine the manufacturer");
  169. if (rc)
  170. return 0;
  171. str += sprintf(str, "Manufacturer: 0x%x\n",
  172. be32_to_cpu(cap.manufacturer_id));
  173. /* Try to get a TPM version 1.2 TPM_CAP_VERSION_INFO */
  174. rc = tpm_getcap(chip, CAP_VERSION_1_2, &cap,
  175. "attempting to determine the 1.2 version");
  176. if (!rc) {
  177. str += sprintf(str,
  178. "TCG version: %d.%d\nFirmware version: %d.%d\n",
  179. cap.tpm_version_1_2.Major,
  180. cap.tpm_version_1_2.Minor,
  181. cap.tpm_version_1_2.revMajor,
  182. cap.tpm_version_1_2.revMinor);
  183. } else {
  184. /* Otherwise just use TPM_STRUCT_VER */
  185. rc = tpm_getcap(chip, CAP_VERSION_1_1, &cap,
  186. "attempting to determine the 1.1 version");
  187. if (rc)
  188. return 0;
  189. str += sprintf(str,
  190. "TCG version: %d.%d\nFirmware version: %d.%d\n",
  191. cap.tpm_version.Major,
  192. cap.tpm_version.Minor,
  193. cap.tpm_version.revMajor,
  194. cap.tpm_version.revMinor);
  195. }
  196. return str - buf;
  197. }
  198. static DEVICE_ATTR_RO(caps);
  199. static ssize_t cancel_store(struct device *dev, struct device_attribute *attr,
  200. const char *buf, size_t count)
  201. {
  202. struct tpm_chip *chip = to_tpm_chip(dev);
  203. if (chip == NULL)
  204. return 0;
  205. chip->ops->cancel(chip);
  206. return count;
  207. }
  208. static DEVICE_ATTR_WO(cancel);
  209. static ssize_t durations_show(struct device *dev, struct device_attribute *attr,
  210. char *buf)
  211. {
  212. struct tpm_chip *chip = to_tpm_chip(dev);
  213. if (chip->duration[TPM_LONG] == 0)
  214. return 0;
  215. return sprintf(buf, "%d %d %d [%s]\n",
  216. jiffies_to_usecs(chip->duration[TPM_SHORT]),
  217. jiffies_to_usecs(chip->duration[TPM_MEDIUM]),
  218. jiffies_to_usecs(chip->duration[TPM_LONG]),
  219. chip->duration_adjusted
  220. ? "adjusted" : "original");
  221. }
  222. static DEVICE_ATTR_RO(durations);
  223. static ssize_t timeouts_show(struct device *dev, struct device_attribute *attr,
  224. char *buf)
  225. {
  226. struct tpm_chip *chip = to_tpm_chip(dev);
  227. return sprintf(buf, "%d %d %d %d [%s]\n",
  228. jiffies_to_usecs(chip->timeout_a),
  229. jiffies_to_usecs(chip->timeout_b),
  230. jiffies_to_usecs(chip->timeout_c),
  231. jiffies_to_usecs(chip->timeout_d),
  232. chip->timeout_adjusted
  233. ? "adjusted" : "original");
  234. }
  235. static DEVICE_ATTR_RO(timeouts);
  236. static struct attribute *tpm_dev_attrs[] = {
  237. &dev_attr_pubek.attr,
  238. &dev_attr_pcrs.attr,
  239. &dev_attr_enabled.attr,
  240. &dev_attr_active.attr,
  241. &dev_attr_owned.attr,
  242. &dev_attr_temp_deactivated.attr,
  243. &dev_attr_caps.attr,
  244. &dev_attr_cancel.attr,
  245. &dev_attr_durations.attr,
  246. &dev_attr_timeouts.attr,
  247. NULL,
  248. };
  249. static const struct attribute_group tpm_dev_group = {
  250. .attrs = tpm_dev_attrs,
  251. };
  252. void tpm_sysfs_add_device(struct tpm_chip *chip)
  253. {
  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;
  259. /* The sysfs routines rely on an implicit tpm_try_get_ops, device_del
  260. * is called before ops is null'd and the sysfs core synchronizes this
  261. * removal so that no callbacks are running or can run again
  262. */
  263. WARN_ON(chip->groups_cnt != 0);
  264. chip->groups[chip->groups_cnt++] = &tpm_dev_group;
  265. }