tpm-common.h 8.0 KB

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