tpm-common.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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, -EBUSY if already opened, other -ve on other 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. * report_state() - Collect information about the current TPM state
  107. *
  108. * @dev: Device to check
  109. * @buf: Buffer to put the string
  110. * @size: Maximum size of buffer
  111. * Return: return code of the operation (0 = success)
  112. */
  113. int (*report_state)(struct udevice *dev, char *buf, int size);
  114. /**
  115. * send() - send data to the TPM
  116. *
  117. * @dev: Device to talk to
  118. * @sendbuf: Buffer of the data to send
  119. * @send_size: Size of the data to send
  120. *
  121. * Returns 0 on success or -ve on failure.
  122. */
  123. int (*send)(struct udevice *dev, const u8 *sendbuf, size_t send_size);
  124. /**
  125. * recv() - receive a response from the TPM
  126. *
  127. * @dev: Device to talk to
  128. * @recvbuf: Buffer to save the response to
  129. * @max_size: Maximum number of bytes to receive
  130. *
  131. * Returns number of bytes received on success, -EAGAIN if the TPM
  132. * response is not ready, -EINTR if cancelled, or other -ve value on
  133. * failure.
  134. */
  135. int (*recv)(struct udevice *dev, u8 *recvbuf, size_t max_size);
  136. /**
  137. * cleanup() - clean up after an operation in progress
  138. *
  139. * This is called if receiving times out. The TPM may need to abort
  140. * the current transaction if it did not complete, and make itself
  141. * ready for another.
  142. *
  143. * @dev: Device to talk to
  144. */
  145. int (*cleanup)(struct udevice *dev);
  146. /**
  147. * xfer() - send data to the TPM and get response
  148. *
  149. * This method is optional. If it exists it is used in preference
  150. * to send(), recv() and cleanup(). It should handle all aspects of
  151. * TPM communication for a single transfer.
  152. *
  153. * @dev: Device to talk to
  154. * @sendbuf: Buffer of the data to send
  155. * @send_size: Size of the data to send
  156. * @recvbuf: Buffer to save the response to
  157. * @recv_size: Pointer to the size of the response buffer
  158. *
  159. * Returns 0 on success (and places the number of response bytes at
  160. * recv_size) or -ve on failure.
  161. */
  162. int (*xfer)(struct udevice *dev, const u8 *sendbuf, size_t send_size,
  163. u8 *recvbuf, size_t *recv_size);
  164. };
  165. #define tpm_get_ops(dev) ((struct tpm_ops *)device_get_ops(dev))
  166. #define MAKE_TPM_CMD_ENTRY(cmd) \
  167. U_BOOT_CMD_MKENT(cmd, 0, 1, do_tpm_ ## cmd, "", "")
  168. #define TPM_COMMAND_NO_ARG(cmd) \
  169. int do_##cmd(struct cmd_tbl *cmdtp, int flag, \
  170. int argc, char *const argv[]) \
  171. { \
  172. struct udevice *dev; \
  173. int rc; \
  174. \
  175. rc = get_tpm(&dev); \
  176. if (rc) \
  177. return rc; \
  178. if (argc != 1) \
  179. return CMD_RET_USAGE; \
  180. return report_return_code(cmd(dev)); \
  181. }
  182. /**
  183. * tpm_open() - Request access to locality 0 for the caller
  184. *
  185. * After all commands have been completed the caller is supposed to
  186. * call tpm_close().
  187. *
  188. * @dev - TPM device
  189. * Returns 0 on success, -ve on failure.
  190. */
  191. int tpm_open(struct udevice *dev);
  192. /**
  193. * tpm_close() - Close the current session
  194. *
  195. * Releasing the locked locality. Returns 0 on success, -ve 1 on
  196. * failure (in case lock removal did not succeed).
  197. *
  198. * @dev - TPM device
  199. * Returns 0 on success, -ve on failure.
  200. */
  201. int tpm_close(struct udevice *dev);
  202. /**
  203. * tpm_clear_and_reenable() - Force clear the TPM and reenable it
  204. *
  205. * @dev: TPM device
  206. * Return: 0 on success, -ve on failure
  207. */
  208. u32 tpm_clear_and_reenable(struct udevice *dev);
  209. /**
  210. * tpm_get_desc() - Get a text description of the TPM
  211. *
  212. * @dev: Device to check
  213. * @buf: Buffer to put the string
  214. * @size: Maximum size of buffer
  215. * Return: length of string, or -ENOSPC it no space
  216. */
  217. int tpm_get_desc(struct udevice *dev, char *buf, int size);
  218. /**
  219. * tpm_report_state() - Collect information about the current TPM state
  220. *
  221. * @dev: Device to check
  222. * @buf: Buffer to put the string
  223. * @size: Maximum size of buffer
  224. * Return: return code of the operation (0 = success)
  225. */
  226. int tpm_report_state(struct udevice *dev, char *buf, int size);
  227. /**
  228. * tpm_xfer() - send data to the TPM and get response
  229. *
  230. * This first uses the device's send() method to send the bytes. Then it calls
  231. * recv() to get the reply. If recv() returns -EAGAIN then it will delay a
  232. * short time and then call recv() again.
  233. *
  234. * Regardless of whether recv() completes successfully, it will then call
  235. * cleanup() to finish the transaction.
  236. *
  237. * Note that the outgoing data is inspected to determine command type
  238. * (ordinal) and a timeout is used for that command type.
  239. *
  240. * @dev - TPM device
  241. * @sendbuf - buffer of the data to send
  242. * @send_size size of the data to send
  243. * @recvbuf - memory to save the response to
  244. * @recv_len - pointer to the size of the response buffer
  245. *
  246. * Returns 0 on success (and places the number of response bytes at
  247. * recv_len) or -ve on failure.
  248. */
  249. int tpm_xfer(struct udevice *dev, const u8 *sendbuf, size_t send_size,
  250. u8 *recvbuf, size_t *recv_size);
  251. /**
  252. * Initialize TPM device. It must be called before any TPM commands.
  253. *
  254. * @dev - TPM device
  255. * Return: 0 on success, non-0 on error.
  256. */
  257. int tpm_init(struct udevice *dev);
  258. /**
  259. * Retrieve the array containing all the v1 (resp. v2) commands.
  260. *
  261. * Return: a struct cmd_tbl array.
  262. */
  263. #if defined(CONFIG_TPM_V1)
  264. struct cmd_tbl *get_tpm1_commands(unsigned int *size);
  265. #else
  266. static inline struct cmd_tbl *get_tpm1_commands(unsigned int *size)
  267. {
  268. return NULL;
  269. }
  270. #endif
  271. #if defined(CONFIG_TPM_V2)
  272. struct cmd_tbl *get_tpm2_commands(unsigned int *size);
  273. #else
  274. static inline struct cmd_tbl *get_tpm2_commands(unsigned int *size)
  275. {
  276. return NULL;
  277. }
  278. #endif
  279. /**
  280. * tpm_get_version() - Find the version of a TPM
  281. *
  282. * This checks the uclass data for a TPM device and returns the version number
  283. * it supports.
  284. *
  285. * @dev: TPM device
  286. * Return: version number (TPM_V1 or TPMV2)
  287. */
  288. enum tpm_version tpm_get_version(struct udevice *dev);
  289. /* Iterate on all TPM devices */
  290. #define for_each_tpm_device(dev) uclass_foreach_dev_probe(UCLASS_TPM, (dev))
  291. #endif /* __TPM_COMMON_H */