1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) Microsoft Corporation
9 * Device Driver for a firmware TPM as described here:
10 * https://www.microsoft.com/en-us/research/publication/ftpm-software-implementation-tpm-chip/
12 * A reference implementation is available here:
13 * https://github.com/microsoft/ms-tpm-20-ref/tree/master/Samples/ARM32-FirmwareTPM/optee_ta/fTPM
20 #include <tee/optee_service.h>
23 #include "tpm2_ftpm_tee.h"
25 OPTEE_SERVICE_DRIVER(optee_ftpm, TA_FTPM_UUID, "ftpm_tee");
28 * ftpm_tee_transceive() - send fTPM commands and retrieve fTPM response.
29 * @sendbuf - address of the data to send, byte by byte
30 * @send_size - length of the data to send
31 * @recvbuf - address where to read the response, byte by byte.
32 * @recv_len - pointer to the size of buffer
35 * In case of success, returns 0.
38 static int ftpm_tee_transceive(struct udevice *dev, const u8 *sendbuf,
39 size_t send_size, u8 *recvbuf,
42 struct ftpm_tee_private *context = dev_get_priv(dev);
46 struct tpm_output_header *resp_header;
47 struct tee_invoke_arg transceive_args;
48 struct tee_param command_params[4];
51 if (send_size > MAX_COMMAND_SIZE) {
52 debug("%s:send_size=%zd exceeds MAX_COMMAND_SIZE\n",
58 memset(&transceive_args, 0, sizeof(transceive_args));
59 memset(command_params, 0, sizeof(command_params));
61 /* Invoke FTPM_OPTEE_TA_SUBMIT_COMMAND function of fTPM TA */
62 transceive_args = (struct tee_invoke_arg) {
63 .func = FTPM_OPTEE_TA_SUBMIT_COMMAND,
64 .session = context->session,
67 /* Fill FTPM_OPTEE_TA_SUBMIT_COMMAND parameters */
69 command_params[0] = (struct tee_param) {
70 .attr = TEE_PARAM_ATTR_TYPE_MEMREF_INPUT,
77 memset(command_params[0].u.memref.shm->addr, 0,
78 (MAX_COMMAND_SIZE + MAX_RESPONSE_SIZE));
79 memcpy(command_params[0].u.memref.shm->addr, sendbuf, send_size);
82 command_params[1] = (struct tee_param) {
83 .attr = TEE_PARAM_ATTR_TYPE_MEMREF_INOUT,
86 .size = MAX_RESPONSE_SIZE,
87 .shm_offs = MAX_COMMAND_SIZE,
91 rc = tee_invoke_func(context->tee_dev, &transceive_args, 4,
93 if ((rc < 0) || (transceive_args.ret != 0)) {
94 debug("%s:SUBMIT_COMMAND invoke error: 0x%x\n",
95 __func__, transceive_args.ret);
96 return (rc < 0) ? rc : transceive_args.ret;
99 resp_buf = command_params[1].u.memref.shm->addr +
100 command_params[1].u.memref.shm_offs;
101 resp_header = (struct tpm_output_header *)resp_buf;
102 resp_len = be32_to_cpu(resp_header->length);
104 /* sanity check resp_len*/
105 if (resp_len < TPM_HEADER_SIZE) {
106 debug("%s:tpm response header too small\n", __func__);
109 if (resp_len > MAX_RESPONSE_SIZE) {
110 debug("%s:resp_len=%zd exceeds MAX_RESPONSE_SIZE\n",
114 if (resp_len > *recv_len) {
115 debug("%s:response length is bigger than receive buffer\n",
120 /* sanity checks look good, copy the response */
121 memcpy(recvbuf, resp_buf, resp_len);
122 *recv_len = resp_len;
127 static int ftpm_tee_open(struct udevice *dev)
129 struct ftpm_tee_private *context = dev_get_priv(dev);
131 if (context->is_open)
134 context->is_open = 1;
139 static int ftpm_tee_close(struct udevice *dev)
141 struct ftpm_tee_private *context = dev_get_priv(dev);
143 if (context->is_open)
144 context->is_open = 0;
149 static int ftpm_tee_desc(struct udevice *dev, char *buf, int size)
154 return snprintf(buf, size, "Microsoft OP-TEE fTPM");
157 static int ftpm_tee_match(struct tee_version_data *vers, const void *data)
159 debug("%s:vers->gen_caps =0x%x\n", __func__, vers->gen_caps);
162 * Currently this driver only support GP Complaint OPTEE based fTPM TA
164 return vers->gen_caps & TEE_GEN_CAP_GP;
167 static int ftpm_tee_probe(struct udevice *dev)
170 struct tpm_chip_priv *priv = dev_get_uclass_priv(dev);
171 struct ftpm_tee_private *context = dev_get_priv(dev);
172 struct tee_open_session_arg sess_arg;
173 const struct tee_optee_ta_uuid uuid = TA_FTPM_UUID;
175 memset(context, 0, sizeof(*context));
177 /* Use the TPM v2 stack */
178 priv->version = TPM_V2;
179 priv->pcr_count = 24;
180 priv->pcr_select_min = 3;
182 /* Find TEE device */
183 context->tee_dev = tee_find_device(NULL, ftpm_tee_match, NULL, NULL);
184 if (!context->tee_dev) {
185 debug("%s:tee_find_device failed\n", __func__);
189 /* Open a session with the fTPM TA */
190 memset(&sess_arg, 0, sizeof(sess_arg));
191 sess_arg.clnt_login = TEE_LOGIN_REE_KERNEL;
192 tee_optee_ta_uuid_to_octets(sess_arg.uuid, &uuid);
194 rc = tee_open_session(context->tee_dev, &sess_arg, 0, NULL);
195 if ((rc < 0) || (sess_arg.ret != 0)) {
196 debug("%s:tee_open_session failed, err=%x\n",
197 __func__, sess_arg.ret);
200 context->session = sess_arg.session;
202 /* Allocate dynamic shared memory with fTPM TA */
203 rc = tee_shm_alloc(context->tee_dev,
204 MAX_COMMAND_SIZE + MAX_RESPONSE_SIZE,
207 debug("%s:tee_shm_alloc failed with rc = %d\n", __func__, rc);
214 tee_close_session(context->tee_dev, context->session);
219 static int ftpm_tee_remove(struct udevice *dev)
221 struct ftpm_tee_private *context = dev_get_priv(dev);
224 /* tee_pre_remove frees any leftover TEE shared memory */
226 /* close the existing session with fTPM TA*/
227 rc = tee_close_session(context->tee_dev, context->session);
228 debug("%s: tee_close_session - rc =%d\n", __func__, rc);
233 static const struct tpm_ops ftpm_tee_ops = {
234 .open = ftpm_tee_open,
235 .close = ftpm_tee_close,
236 .get_desc = ftpm_tee_desc,
237 .xfer = ftpm_tee_transceive,
240 static const struct udevice_id ftpm_tee_ids[] = {
241 { .compatible = "microsoft,ftpm" },
245 U_BOOT_DRIVER(ftpm_tee) = {
248 .of_match = ftpm_tee_ids,
249 .ops = &ftpm_tee_ops,
250 .probe = ftpm_tee_probe,
251 .remove = ftpm_tee_remove,
252 .flags = DM_FLAG_OS_PREPARE,
253 .priv_auto = sizeof(struct ftpm_tee_private),