tpm-common.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. /**
  23. * struct tpm_chip_priv - Information about a TPM, stored by the uclass
  24. *
  25. * These values must be set up by the device's probe() method before
  26. * communcation is attempted. If the device has an xfer() method, this is
  27. * not needed. There is no need to set up @buf.
  28. *
  29. * @duration_ms: Length of each duration type in milliseconds
  30. * @retry_time_ms: Time to wait before retrying receive
  31. */
  32. struct tpm_chip_priv {
  33. uint duration_ms[TPM_DURATION_COUNT];
  34. uint retry_time_ms;
  35. u8 buf[TPM_DEV_BUFSIZE + sizeof(u8)]; /* Max buffer size + addr */
  36. };
  37. /**
  38. * struct tpm_ops - low-level TPM operations
  39. *
  40. * These are designed to avoid loops and delays in the driver itself. These
  41. * should be handled in the uclass.
  42. *
  43. * In gneral you should implement everything except xfer(). Where you need
  44. * complete control of the transfer, then xfer() can be provided and will
  45. * override the other methods.
  46. *
  47. * This interface is for low-level TPM access. It does not understand the
  48. * concept of localities or the various TPM messages. That interface is
  49. * defined in the functions later on in this file, but they all translate
  50. * to bytes which are sent and received.
  51. */
  52. struct tpm_ops {
  53. /**
  54. * open() - Request access to locality 0 for the caller
  55. *
  56. * After all commands have been completed the caller should call
  57. * close().
  58. *
  59. * @dev: Device to close
  60. * @return 0 ok OK, -ve on error
  61. */
  62. int (*open)(struct udevice *dev);
  63. /**
  64. * close() - Close the current session
  65. *
  66. * Releasing the locked locality. Returns 0 on success, -ve 1 on
  67. * failure (in case lock removal did not succeed).
  68. *
  69. * @dev: Device to close
  70. * @return 0 ok OK, -ve on error
  71. */
  72. int (*close)(struct udevice *dev);
  73. /**
  74. * get_desc() - Get a text description of the TPM
  75. *
  76. * @dev: Device to check
  77. * @buf: Buffer to put the string
  78. * @size: Maximum size of buffer
  79. * @return length of string, or -ENOSPC it no space
  80. */
  81. int (*get_desc)(struct udevice *dev, char *buf, int size);
  82. /**
  83. * send() - send data to the TPM
  84. *
  85. * @dev: Device to talk to
  86. * @sendbuf: Buffer of the data to send
  87. * @send_size: Size of the data to send
  88. *
  89. * Returns 0 on success or -ve on failure.
  90. */
  91. int (*send)(struct udevice *dev, const u8 *sendbuf, size_t send_size);
  92. /**
  93. * recv() - receive a response from the TPM
  94. *
  95. * @dev: Device to talk to
  96. * @recvbuf: Buffer to save the response to
  97. * @max_size: Maximum number of bytes to receive
  98. *
  99. * Returns number of bytes received on success, -EAGAIN if the TPM
  100. * response is not ready, -EINTR if cancelled, or other -ve value on
  101. * failure.
  102. */
  103. int (*recv)(struct udevice *dev, u8 *recvbuf, size_t max_size);
  104. /**
  105. * cleanup() - clean up after an operation in progress
  106. *
  107. * This is called if receiving times out. The TPM may need to abort
  108. * the current transaction if it did not complete, and make itself
  109. * ready for another.
  110. *
  111. * @dev: Device to talk to
  112. */
  113. int (*cleanup)(struct udevice *dev);
  114. /**
  115. * xfer() - send data to the TPM and get response
  116. *
  117. * This method is optional. If it exists it is used in preference
  118. * to send(), recv() and cleanup(). It should handle all aspects of
  119. * TPM communication for a single transfer.
  120. *
  121. * @dev: Device to talk to
  122. * @sendbuf: Buffer of the data to send
  123. * @send_size: Size of the data to send
  124. * @recvbuf: Buffer to save the response to
  125. * @recv_size: Pointer to the size of the response buffer
  126. *
  127. * Returns 0 on success (and places the number of response bytes at
  128. * recv_size) or -ve on failure.
  129. */
  130. int (*xfer)(struct udevice *dev, const u8 *sendbuf, size_t send_size,
  131. u8 *recvbuf, size_t *recv_size);
  132. };
  133. #define tpm_get_ops(dev) ((struct tpm_ops *)device_get_ops(dev))
  134. #define MAKE_TPM_CMD_ENTRY(cmd) \
  135. U_BOOT_CMD_MKENT(cmd, 0, 1, do_tpm_ ## cmd, "", "")
  136. #define TPM_COMMAND_NO_ARG(cmd) \
  137. int do_##cmd(cmd_tbl_t *cmdtp, int flag, \
  138. int argc, char * const argv[]) \
  139. { \
  140. if (argc != 1) \
  141. return CMD_RET_USAGE; \
  142. return report_return_code(cmd()); \
  143. }
  144. /**
  145. * tpm_get_desc() - Get a text description of the TPM
  146. *
  147. * @dev: Device to check
  148. * @buf: Buffer to put the string
  149. * @size: Maximum size of buffer
  150. * @return length of string, or -ENOSPC it no space
  151. */
  152. int tpm_get_desc(struct udevice *dev, char *buf, int size);
  153. /**
  154. * tpm_xfer() - send data to the TPM and get response
  155. *
  156. * This first uses the device's send() method to send the bytes. Then it calls
  157. * recv() to get the reply. If recv() returns -EAGAIN then it will delay a
  158. * short time and then call recv() again.
  159. *
  160. * Regardless of whether recv() completes successfully, it will then call
  161. * cleanup() to finish the transaction.
  162. *
  163. * Note that the outgoing data is inspected to determine command type
  164. * (ordinal) and a timeout is used for that command type.
  165. *
  166. * @sendbuf - buffer of the data to send
  167. * @send_size size of the data to send
  168. * @recvbuf - memory to save the response to
  169. * @recv_len - pointer to the size of the response buffer
  170. *
  171. * Returns 0 on success (and places the number of response bytes at
  172. * recv_len) or -ve on failure.
  173. */
  174. int tpm_xfer(struct udevice *dev, const u8 *sendbuf, size_t send_size,
  175. u8 *recvbuf, size_t *recv_size);
  176. /**
  177. * Initialize TPM device. It must be called before any TPM commands.
  178. *
  179. * @return 0 on success, non-0 on error.
  180. */
  181. int tpm_init(void);
  182. /**
  183. * Retrieve the array containing all the commands.
  184. *
  185. * @return a cmd_tbl_t array.
  186. */
  187. cmd_tbl_t *get_tpm_commands(unsigned int *size);
  188. #endif /* __TPM_COMMON_H */