tpm-common.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. /*
  3. * Copyright (c) 2013 The Chromium OS Authors.
  4. * Coypright (c) 2013 Guntermann & Drunck GmbH
  5. */
  6. #ifndef __TPM_COMMON_H
  7. #define __TPM_COMMON_H
  8. #include <command.h>
  9. struct udevice;
  10. enum tpm_duration {
  11. TPM_SHORT = 0,
  12. TPM_MEDIUM = 1,
  13. TPM_LONG = 2,
  14. TPM_UNDEFINED,
  15. TPM_DURATION_COUNT,
  16. };
  17. /*
  18. * Here is a partial implementation of TPM commands. Please consult TCG Main
  19. * Specification for definitions of TPM commands.
  20. */
  21. #define TPM_HEADER_SIZE 10
  22. /* Max buffer size supported by our tpm */
  23. #define TPM_DEV_BUFSIZE 1260
  24. #define TPM_PCR_MINIMUM_DIGEST_SIZE 20
  25. /**
  26. * enum tpm_version - The version of the TPM stack to be used
  27. * @TPM_V1: Use TPM v1.x stack
  28. * @TPM_V2: Use TPM v2.x stack
  29. */
  30. enum tpm_version {
  31. TPM_V1 = 0,
  32. TPM_V2,
  33. };
  34. /**
  35. * struct tpm_chip_priv - Information about a TPM, stored by the uclass
  36. *
  37. * These values must be set up by the device's probe() method before
  38. * communcation is attempted. If the device has an xfer() method, this is
  39. * not needed. There is no need to set up @buf.
  40. *
  41. * @version: TPM stack to be used
  42. * @duration_ms: Length of each duration type in milliseconds
  43. * @retry_time_ms: Time to wait before retrying receive
  44. * @buf: Buffer used during the exchanges with the chip
  45. * @pcr_count: Number of PCR per bank
  46. * @pcr_select_min: Minimum size in bytes of the pcrSelect array
  47. * @plat_hier_disabled: Platform hierarchy has been disabled (TPM is locked
  48. * down until next reboot)
  49. */
  50. struct tpm_chip_priv {
  51. enum tpm_version version;
  52. uint duration_ms[TPM_DURATION_COUNT];
  53. uint retry_time_ms;
  54. u8 buf[TPM_DEV_BUFSIZE + sizeof(u8)]; /* Max buffer size + addr */
  55. /* TPM v2 specific data */
  56. uint pcr_count;
  57. uint pcr_select_min;
  58. bool plat_hier_disabled;
  59. };
  60. /**
  61. * struct tpm_ops - low-level TPM operations
  62. *
  63. * These are designed to avoid loops and delays in the driver itself. These
  64. * should be handled in the uclass.
  65. *
  66. * In gneral you should implement everything except xfer(). Where you need
  67. * complete control of the transfer, then xfer() can be provided and will
  68. * override the other methods.
  69. *
  70. * This interface is for low-level TPM access. It does not understand the
  71. * concept of localities or the various TPM messages. That interface is
  72. * defined in the functions later on in this file, but they all translate
  73. * to bytes which are sent and received.
  74. */
  75. struct tpm_ops {
  76. /**
  77. * open() - Request access to locality 0 for the caller
  78. *
  79. * After all commands have been completed the caller should call
  80. * close().
  81. *
  82. * @dev: Device to open
  83. * @return 0 ok OK, -ve on error
  84. */
  85. int (*open)(struct udevice *dev);
  86. /**
  87. * close() - Close the current session
  88. *
  89. * Releasing the locked locality. Returns 0 on success, -ve 1 on
  90. * failure (in case lock removal did not succeed).
  91. *
  92. * @dev: Device to close
  93. * @return 0 ok OK, -ve on error
  94. */
  95. int (*close)(struct udevice *dev);
  96. /**
  97. * get_desc() - Get a text description of the TPM
  98. *
  99. * @dev: Device to check
  100. * @buf: Buffer to put the string
  101. * @size: Maximum size of buffer
  102. * @return length of string, or -ENOSPC it no space
  103. */
  104. int (*get_desc)(struct udevice *dev, char *buf, int size);
  105. /**
  106. * send() - send data to the TPM
  107. *
  108. * @dev: Device to talk to
  109. * @sendbuf: Buffer of the data to send
  110. * @send_size: Size of the data to send
  111. *
  112. * Returns 0 on success or -ve on failure.
  113. */
  114. int (*send)(struct udevice *dev, const u8 *sendbuf, size_t send_size);
  115. /**
  116. * recv() - receive a response from the TPM
  117. *
  118. * @dev: Device to talk to
  119. * @recvbuf: Buffer to save the response to
  120. * @max_size: Maximum number of bytes to receive
  121. *
  122. * Returns number of bytes received on success, -EAGAIN if the TPM
  123. * response is not ready, -EINTR if cancelled, or other -ve value on
  124. * failure.
  125. */
  126. int (*recv)(struct udevice *dev, u8 *recvbuf, size_t max_size);
  127. /**
  128. * cleanup() - clean up after an operation in progress
  129. *
  130. * This is called if receiving times out. The TPM may need to abort
  131. * the current transaction if it did not complete, and make itself
  132. * ready for another.
  133. *
  134. * @dev: Device to talk to
  135. */
  136. int (*cleanup)(struct udevice *dev);
  137. /**
  138. * xfer() - send data to the TPM and get response
  139. *
  140. * This method is optional. If it exists it is used in preference
  141. * to send(), recv() and cleanup(). It should handle all aspects of
  142. * TPM communication for a single transfer.
  143. *
  144. * @dev: Device to talk to
  145. * @sendbuf: Buffer of the data to send
  146. * @send_size: Size of the data to send
  147. * @recvbuf: Buffer to save the response to
  148. * @recv_size: Pointer to the size of the response buffer
  149. *
  150. * Returns 0 on success (and places the number of response bytes at
  151. * recv_size) or -ve on failure.
  152. */
  153. int (*xfer)(struct udevice *dev, const u8 *sendbuf, size_t send_size,
  154. u8 *recvbuf, size_t *recv_size);
  155. };
  156. #define tpm_get_ops(dev) ((struct tpm_ops *)device_get_ops(dev))
  157. #define MAKE_TPM_CMD_ENTRY(cmd) \
  158. U_BOOT_CMD_MKENT(cmd, 0, 1, do_tpm_ ## cmd, "", "")
  159. #define TPM_COMMAND_NO_ARG(cmd) \
  160. int do_##cmd(struct cmd_tbl *cmdtp, int flag, \
  161. int argc, char *const argv[]) \
  162. { \
  163. struct udevice *dev; \
  164. int rc; \
  165. \
  166. rc = get_tpm(&dev); \
  167. if (rc) \
  168. return rc; \
  169. if (argc != 1) \
  170. return CMD_RET_USAGE; \
  171. return report_return_code(cmd(dev)); \
  172. }
  173. /**
  174. * tpm_open() - Request access to locality 0 for the caller
  175. *
  176. * After all commands have been completed the caller is supposed to
  177. * call tpm_close().
  178. *
  179. * @dev - TPM device
  180. * Returns 0 on success, -ve on failure.
  181. */
  182. int tpm_open(struct udevice *dev);
  183. /**
  184. * tpm_close() - Close the current session
  185. *
  186. * Releasing the locked locality. Returns 0 on success, -ve 1 on
  187. * failure (in case lock removal did not succeed).
  188. *
  189. * @dev - TPM device
  190. * Returns 0 on success, -ve on failure.
  191. */
  192. int tpm_close(struct udevice *dev);
  193. /**
  194. * tpm_clear_and_reenable() - Force clear the TPM and reenable it
  195. *
  196. * @dev: TPM device
  197. * @return 0 on success, -ve on failure
  198. */
  199. u32 tpm_clear_and_reenable(struct udevice *dev);
  200. /**
  201. * tpm_get_desc() - Get a text description of the TPM
  202. *
  203. * @dev: Device to check
  204. * @buf: Buffer to put the string
  205. * @size: Maximum size of buffer
  206. * @return length of string, or -ENOSPC it no space
  207. */
  208. int tpm_get_desc(struct udevice *dev, char *buf, int size);
  209. /**
  210. * tpm_xfer() - send data to the TPM and get response
  211. *
  212. * This first uses the device's send() method to send the bytes. Then it calls
  213. * recv() to get the reply. If recv() returns -EAGAIN then it will delay a
  214. * short time and then call recv() again.
  215. *
  216. * Regardless of whether recv() completes successfully, it will then call
  217. * cleanup() to finish the transaction.
  218. *
  219. * Note that the outgoing data is inspected to determine command type
  220. * (ordinal) and a timeout is used for that command type.
  221. *
  222. * @dev - TPM device
  223. * @sendbuf - buffer of the data to send
  224. * @send_size size of the data to send
  225. * @recvbuf - memory to save the response to
  226. * @recv_len - pointer to the size of the response buffer
  227. *
  228. * Returns 0 on success (and places the number of response bytes at
  229. * recv_len) or -ve on failure.
  230. */
  231. int tpm_xfer(struct udevice *dev, const u8 *sendbuf, size_t send_size,
  232. u8 *recvbuf, size_t *recv_size);
  233. /**
  234. * Initialize TPM device. It must be called before any TPM commands.
  235. *
  236. * @dev - TPM device
  237. * @return 0 on success, non-0 on error.
  238. */
  239. int tpm_init(struct udevice *dev);
  240. /**
  241. * Retrieve the array containing all the v1 (resp. v2) commands.
  242. *
  243. * @return a struct cmd_tbl array.
  244. */
  245. #if defined(CONFIG_TPM_V1)
  246. struct cmd_tbl *get_tpm1_commands(unsigned int *size);
  247. #else
  248. static inline struct cmd_tbl *get_tpm1_commands(unsigned int *size)
  249. {
  250. return NULL;
  251. }
  252. #endif
  253. #if defined(CONFIG_TPM_V2)
  254. struct cmd_tbl *get_tpm2_commands(unsigned int *size);
  255. #else
  256. static inline struct cmd_tbl *get_tpm2_commands(unsigned int *size)
  257. {
  258. return NULL;
  259. }
  260. #endif
  261. /**
  262. * tpm_get_version() - Find the version of a TPM
  263. *
  264. * This checks the uclass data for a TPM device and returns the version number
  265. * it supports.
  266. *
  267. * @dev: TPM device
  268. * @return version number (TPM_V1 or TPMV2)
  269. */
  270. enum tpm_version tpm_get_version(struct udevice *dev);
  271. /* Iterate on all TPM devices */
  272. #define for_each_tpm_device(dev) uclass_foreach_dev_probe(UCLASS_TPM, (dev))
  273. #endif /* __TPM_COMMON_H */