tpm2_ftpm_tee.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) Microsoft Corporation
  4. *
  5. * Authors:
  6. * Thirupathaiah Annapureddy <thiruan@microsoft.com>
  7. *
  8. * Description:
  9. * Device Driver for a firmware TPM as described here:
  10. * https://www.microsoft.com/en-us/research/publication/ftpm-software-implementation-tpm-chip/
  11. *
  12. * A reference implementation is available here:
  13. * https://github.com/microsoft/ms-tpm-20-ref/tree/master/Samples/ARM32-FirmwareTPM/optee_ta/fTPM
  14. */
  15. #include <common.h>
  16. #include <dm.h>
  17. #include <log.h>
  18. #include <tpm-v2.h>
  19. #include <tee.h>
  20. #include "tpm_tis.h"
  21. #include "tpm2_ftpm_tee.h"
  22. /**
  23. * ftpm_tee_transceive() - send fTPM commands and retrieve fTPM response.
  24. * @sendbuf - address of the data to send, byte by byte
  25. * @send_size - length of the data to send
  26. * @recvbuf - address where to read the response, byte by byte.
  27. * @recv_len - pointer to the size of buffer
  28. *
  29. * Return:
  30. * In case of success, returns 0.
  31. * On failure, -errno
  32. */
  33. static int ftpm_tee_transceive(struct udevice *dev, const u8 *sendbuf,
  34. size_t send_size, u8 *recvbuf,
  35. size_t *recv_len)
  36. {
  37. struct ftpm_tee_private *context = dev_get_priv(dev);
  38. int rc = 0;
  39. size_t resp_len;
  40. u8 *resp_buf;
  41. struct tpm_output_header *resp_header;
  42. struct tee_invoke_arg transceive_args;
  43. struct tee_param command_params[4];
  44. struct tee_shm *shm;
  45. if (send_size > MAX_COMMAND_SIZE) {
  46. debug("%s:send_size=%zd exceeds MAX_COMMAND_SIZE\n",
  47. __func__, send_size);
  48. return -EIO;
  49. }
  50. shm = context->shm;
  51. memset(&transceive_args, 0, sizeof(transceive_args));
  52. memset(command_params, 0, sizeof(command_params));
  53. /* Invoke FTPM_OPTEE_TA_SUBMIT_COMMAND function of fTPM TA */
  54. transceive_args = (struct tee_invoke_arg) {
  55. .func = FTPM_OPTEE_TA_SUBMIT_COMMAND,
  56. .session = context->session,
  57. };
  58. /* Fill FTPM_OPTEE_TA_SUBMIT_COMMAND parameters */
  59. /* request */
  60. command_params[0] = (struct tee_param) {
  61. .attr = TEE_PARAM_ATTR_TYPE_MEMREF_INPUT,
  62. .u.memref = {
  63. .shm = shm,
  64. .size = send_size,
  65. .shm_offs = 0,
  66. },
  67. };
  68. memset(command_params[0].u.memref.shm->addr, 0,
  69. (MAX_COMMAND_SIZE + MAX_RESPONSE_SIZE));
  70. memcpy(command_params[0].u.memref.shm->addr, sendbuf, send_size);
  71. /* response */
  72. command_params[1] = (struct tee_param) {
  73. .attr = TEE_PARAM_ATTR_TYPE_MEMREF_INOUT,
  74. .u.memref = {
  75. .shm = shm,
  76. .size = MAX_RESPONSE_SIZE,
  77. .shm_offs = MAX_COMMAND_SIZE,
  78. },
  79. };
  80. rc = tee_invoke_func(context->tee_dev, &transceive_args, 4,
  81. command_params);
  82. if ((rc < 0) || (transceive_args.ret != 0)) {
  83. debug("%s:SUBMIT_COMMAND invoke error: 0x%x\n",
  84. __func__, transceive_args.ret);
  85. return (rc < 0) ? rc : transceive_args.ret;
  86. }
  87. resp_buf = command_params[1].u.memref.shm->addr +
  88. command_params[1].u.memref.shm_offs;
  89. resp_header = (struct tpm_output_header *)resp_buf;
  90. resp_len = be32_to_cpu(resp_header->length);
  91. /* sanity check resp_len*/
  92. if (resp_len < TPM_HEADER_SIZE) {
  93. debug("%s:tpm response header too small\n", __func__);
  94. return -EIO;
  95. }
  96. if (resp_len > MAX_RESPONSE_SIZE) {
  97. debug("%s:resp_len=%zd exceeds MAX_RESPONSE_SIZE\n",
  98. __func__, resp_len);
  99. return -EIO;
  100. }
  101. if (resp_len > *recv_len) {
  102. debug("%s:response length is bigger than receive buffer\n",
  103. __func__);
  104. return -EIO;
  105. }
  106. /* sanity checks look good, copy the response */
  107. memcpy(recvbuf, resp_buf, resp_len);
  108. *recv_len = resp_len;
  109. return 0;
  110. }
  111. static int ftpm_tee_open(struct udevice *dev)
  112. {
  113. struct ftpm_tee_private *context = dev_get_priv(dev);
  114. if (context->is_open)
  115. return -EBUSY;
  116. context->is_open = 1;
  117. return 0;
  118. }
  119. static int ftpm_tee_close(struct udevice *dev)
  120. {
  121. struct ftpm_tee_private *context = dev_get_priv(dev);
  122. if (context->is_open)
  123. context->is_open = 0;
  124. return 0;
  125. }
  126. static int ftpm_tee_desc(struct udevice *dev, char *buf, int size)
  127. {
  128. if (size < 32)
  129. return -ENOSPC;
  130. return snprintf(buf, size, "Microsoft OP-TEE fTPM");
  131. }
  132. static int ftpm_tee_match(struct tee_version_data *vers, const void *data)
  133. {
  134. debug("%s:vers->gen_caps =0x%x\n", __func__, vers->gen_caps);
  135. /*
  136. * Currently this driver only support GP Complaint OPTEE based fTPM TA
  137. */
  138. return vers->gen_caps & TEE_GEN_CAP_GP;
  139. }
  140. static int ftpm_tee_probe(struct udevice *dev)
  141. {
  142. int rc;
  143. struct tpm_chip_priv *priv = dev_get_uclass_priv(dev);
  144. struct ftpm_tee_private *context = dev_get_priv(dev);
  145. struct tee_open_session_arg sess_arg;
  146. const struct tee_optee_ta_uuid uuid = TA_FTPM_UUID;
  147. memset(context, 0, sizeof(*context));
  148. /* Use the TPM v2 stack */
  149. priv->version = TPM_V2;
  150. priv->pcr_count = 24;
  151. priv->pcr_select_min = 3;
  152. /* Find TEE device */
  153. context->tee_dev = tee_find_device(NULL, ftpm_tee_match, NULL, NULL);
  154. if (!context->tee_dev) {
  155. debug("%s:tee_find_device failed\n", __func__);
  156. return -ENODEV;
  157. }
  158. /* Open a session with the fTPM TA */
  159. memset(&sess_arg, 0, sizeof(sess_arg));
  160. tee_optee_ta_uuid_to_octets(sess_arg.uuid, &uuid);
  161. rc = tee_open_session(context->tee_dev, &sess_arg, 0, NULL);
  162. if ((rc < 0) || (sess_arg.ret != 0)) {
  163. debug("%s:tee_open_session failed, err=%x\n",
  164. __func__, sess_arg.ret);
  165. return -EIO;
  166. }
  167. context->session = sess_arg.session;
  168. /* Allocate dynamic shared memory with fTPM TA */
  169. rc = tee_shm_alloc(context->tee_dev,
  170. MAX_COMMAND_SIZE + MAX_RESPONSE_SIZE,
  171. 0, &context->shm);
  172. if (rc) {
  173. debug("%s:tee_shm_alloc failed with rc = %d\n", __func__, rc);
  174. goto out_shm_alloc;
  175. }
  176. return 0;
  177. out_shm_alloc:
  178. tee_close_session(context->tee_dev, context->session);
  179. return rc;
  180. }
  181. static int ftpm_tee_remove(struct udevice *dev)
  182. {
  183. struct ftpm_tee_private *context = dev_get_priv(dev);
  184. int rc;
  185. /* tee_pre_remove frees any leftover TEE shared memory */
  186. /* close the existing session with fTPM TA*/
  187. rc = tee_close_session(context->tee_dev, context->session);
  188. debug("%s: tee_close_session - rc =%d\n", __func__, rc);
  189. return 0;
  190. }
  191. static const struct tpm_ops ftpm_tee_ops = {
  192. .open = ftpm_tee_open,
  193. .close = ftpm_tee_close,
  194. .get_desc = ftpm_tee_desc,
  195. .xfer = ftpm_tee_transceive,
  196. };
  197. static const struct udevice_id ftpm_tee_ids[] = {
  198. { .compatible = "microsoft,ftpm" },
  199. { }
  200. };
  201. U_BOOT_DRIVER(ftpm_tee) = {
  202. .name = "ftpm_tee",
  203. .id = UCLASS_TPM,
  204. .of_match = ftpm_tee_ids,
  205. .ops = &ftpm_tee_ops,
  206. .probe = ftpm_tee_probe,
  207. .remove = ftpm_tee_remove,
  208. .flags = DM_FLAG_OS_PREPARE,
  209. .priv_auto_alloc_size = sizeof(struct ftpm_tee_private),
  210. };