tpm-common.h 7.9 KB

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