1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) Microsoft Corporation
6 * Thirupathaiah Annapureddy <thiruan@microsoft.com>
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
21 #include <tee/optee_service.h>
24 #include "tpm2_ftpm_tee.h"
26 OPTEE_SERVICE_DRIVER(optee_ftpm, TA_FTPM_UUID, "ftpm_tee");
29 * ftpm_tee_transceive() - send fTPM commands and retrieve fTPM response.
30 * @sendbuf - address of the data to send, byte by byte
31 * @send_size - length of the data to send
32 * @recvbuf - address where to read the response, byte by byte.
33 * @recv_len - pointer to the size of buffer
36 * In case of success, returns 0.
39 static int ftpm_tee_transceive(struct udevice *dev, const u8 *sendbuf,
40 size_t send_size, u8 *recvbuf,
43 struct ftpm_tee_private *context = dev_get_priv(dev);
47 struct tpm_output_header *resp_header;
48 struct tee_invoke_arg transceive_args;
49 struct tee_param command_params[4];
52 if (send_size > MAX_COMMAND_SIZE) {
53 debug("%s:send_size=%zd exceeds MAX_COMMAND_SIZE\n",
59 memset(&transceive_args, 0, sizeof(transceive_args));
60 memset(command_params, 0, sizeof(command_params));
62 /* Invoke FTPM_OPTEE_TA_SUBMIT_COMMAND function of fTPM TA */
63 transceive_args = (struct tee_invoke_arg) {
64 .func = FTPM_OPTEE_TA_SUBMIT_COMMAND,
65 .session = context->session,
68 /* Fill FTPM_OPTEE_TA_SUBMIT_COMMAND parameters */
70 command_params[0] = (struct tee_param) {
71 .attr = TEE_PARAM_ATTR_TYPE_MEMREF_INPUT,
78 memset(command_params[0].u.memref.shm->addr, 0,
79 (MAX_COMMAND_SIZE + MAX_RESPONSE_SIZE));
80 memcpy(command_params[0].u.memref.shm->addr, sendbuf, send_size);
83 command_params[1] = (struct tee_param) {
84 .attr = TEE_PARAM_ATTR_TYPE_MEMREF_INOUT,
87 .size = MAX_RESPONSE_SIZE,
88 .shm_offs = MAX_COMMAND_SIZE,
92 rc = tee_invoke_func(context->tee_dev, &transceive_args, 4,
94 if ((rc < 0) || (transceive_args.ret != 0)) {
95 debug("%s:SUBMIT_COMMAND invoke error: 0x%x\n",
96 __func__, transceive_args.ret);
97 return (rc < 0) ? rc : transceive_args.ret;
100 resp_buf = command_params[1].u.memref.shm->addr +
101 command_params[1].u.memref.shm_offs;
102 resp_header = (struct tpm_output_header *)resp_buf;
103 resp_len = be32_to_cpu(resp_header->length);
105 /* sanity check resp_len*/
106 if (resp_len < TPM_HEADER_SIZE) {
107 debug("%s:tpm response header too small\n", __func__);
110 if (resp_len > MAX_RESPONSE_SIZE) {
111 debug("%s:resp_len=%zd exceeds MAX_RESPONSE_SIZE\n",
115 if (resp_len > *recv_len) {
116 debug("%s:response length is bigger than receive buffer\n",
121 /* sanity checks look good, copy the response */
122 memcpy(recvbuf, resp_buf, resp_len);
123 *recv_len = resp_len;
128 static int ftpm_tee_open(struct udevice *dev)
130 struct ftpm_tee_private *context = dev_get_priv(dev);
132 if (context->is_open)
135 context->is_open = 1;
140 static int ftpm_tee_close(struct udevice *dev)
142 struct ftpm_tee_private *context = dev_get_priv(dev);
144 if (context->is_open)
145 context->is_open = 0;
150 static int ftpm_tee_desc(struct udevice *dev, char *buf, int size)
155 return snprintf(buf, size, "Microsoft OP-TEE fTPM");
158 static int ftpm_tee_match(struct tee_version_data *vers, const void *data)
160 debug("%s:vers->gen_caps =0x%x\n", __func__, vers->gen_caps);
163 * Currently this driver only support GP Complaint OPTEE based fTPM TA
165 return vers->gen_caps & TEE_GEN_CAP_GP;
168 static int ftpm_tee_probe(struct udevice *dev)
171 struct tpm_chip_priv *priv = dev_get_uclass_priv(dev);
172 struct ftpm_tee_private *context = dev_get_priv(dev);
173 struct tee_open_session_arg sess_arg;
174 const struct tee_optee_ta_uuid uuid = TA_FTPM_UUID;
176 memset(context, 0, sizeof(*context));
178 /* Use the TPM v2 stack */
179 priv->version = TPM_V2;
180 priv->pcr_count = 24;
181 priv->pcr_select_min = 3;
183 /* Find TEE device */
184 context->tee_dev = tee_find_device(NULL, ftpm_tee_match, NULL, NULL);
185 if (!context->tee_dev) {
186 debug("%s:tee_find_device failed\n", __func__);
190 /* Open a session with the fTPM TA */
191 memset(&sess_arg, 0, sizeof(sess_arg));
192 sess_arg.clnt_login = TEE_LOGIN_REE_KERNEL;
193 tee_optee_ta_uuid_to_octets(sess_arg.uuid, &uuid);
195 rc = tee_open_session(context->tee_dev, &sess_arg, 0, NULL);
196 if ((rc < 0) || (sess_arg.ret != 0)) {
197 debug("%s:tee_open_session failed, err=%x\n",
198 __func__, sess_arg.ret);
201 context->session = sess_arg.session;
203 /* Allocate dynamic shared memory with fTPM TA */
204 rc = tee_shm_alloc(context->tee_dev,
205 MAX_COMMAND_SIZE + MAX_RESPONSE_SIZE,
208 debug("%s:tee_shm_alloc failed with rc = %d\n", __func__, rc);
215 tee_close_session(context->tee_dev, context->session);
220 static int ftpm_tee_remove(struct udevice *dev)
222 struct ftpm_tee_private *context = dev_get_priv(dev);
225 /* tee_pre_remove frees any leftover TEE shared memory */
227 /* close the existing session with fTPM TA*/
228 rc = tee_close_session(context->tee_dev, context->session);
229 debug("%s: tee_close_session - rc =%d\n", __func__, rc);
234 static const struct tpm_ops ftpm_tee_ops = {
235 .open = ftpm_tee_open,
236 .close = ftpm_tee_close,
237 .get_desc = ftpm_tee_desc,
238 .xfer = ftpm_tee_transceive,
241 static const struct udevice_id ftpm_tee_ids[] = {
242 { .compatible = "microsoft,ftpm" },
246 U_BOOT_DRIVER(ftpm_tee) = {
249 .of_match = ftpm_tee_ids,
250 .ops = &ftpm_tee_ops,
251 .probe = ftpm_tee_probe,
252 .remove = ftpm_tee_remove,
253 .flags = DM_FLAG_OS_PREPARE,
254 .priv_auto = sizeof(struct ftpm_tee_private),