tpm-common.h 8.0 KB

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