blob: f2ced50c4eb7e3ca22da8daf6552595157750412 [file] [log] [blame]
Thirupathaiah Annapureddy8d73be72020-01-12 23:34:22 -08001// 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
Thirupathaiah Annapureddy8d73be72020-01-12 23:34:22 -080016#include <dm.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060017#include <log.h>
Thirupathaiah Annapureddy8d73be72020-01-12 23:34:22 -080018#include <tpm-v2.h>
19#include <tee.h>
Ilias Apalodimas9d28f672023-01-19 16:36:12 +020020#include <tee/optee_service.h>
Thirupathaiah Annapureddy8d73be72020-01-12 23:34:22 -080021
22#include "tpm_tis.h"
23#include "tpm2_ftpm_tee.h"
24
Ilias Apalodimas9d28f672023-01-19 16:36:12 +020025OPTEE_SERVICE_DRIVER(optee_ftpm, TA_FTPM_UUID, "ftpm_tee");
26
Thirupathaiah Annapureddy8d73be72020-01-12 23:34:22 -080027/**
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
33 *
34 * Return:
35 * In case of success, returns 0.
36 * On failure, -errno
37 */
38static int ftpm_tee_transceive(struct udevice *dev, const u8 *sendbuf,
39 size_t send_size, u8 *recvbuf,
40 size_t *recv_len)
41{
42 struct ftpm_tee_private *context = dev_get_priv(dev);
43 int rc = 0;
44 size_t resp_len;
45 u8 *resp_buf;
46 struct tpm_output_header *resp_header;
47 struct tee_invoke_arg transceive_args;
48 struct tee_param command_params[4];
49 struct tee_shm *shm;
50
51 if (send_size > MAX_COMMAND_SIZE) {
52 debug("%s:send_size=%zd exceeds MAX_COMMAND_SIZE\n",
53 __func__, send_size);
54 return -EIO;
55 }
56
57 shm = context->shm;
58 memset(&transceive_args, 0, sizeof(transceive_args));
59 memset(command_params, 0, sizeof(command_params));
60
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,
65 };
66
67 /* Fill FTPM_OPTEE_TA_SUBMIT_COMMAND parameters */
68 /* request */
69 command_params[0] = (struct tee_param) {
70 .attr = TEE_PARAM_ATTR_TYPE_MEMREF_INPUT,
71 .u.memref = {
72 .shm = shm,
73 .size = send_size,
74 .shm_offs = 0,
75 },
76 };
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);
80
81 /* response */
82 command_params[1] = (struct tee_param) {
83 .attr = TEE_PARAM_ATTR_TYPE_MEMREF_INOUT,
84 .u.memref = {
85 .shm = shm,
86 .size = MAX_RESPONSE_SIZE,
87 .shm_offs = MAX_COMMAND_SIZE,
88 },
89 };
90
91 rc = tee_invoke_func(context->tee_dev, &transceive_args, 4,
92 command_params);
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;
97 }
98
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);
103
104 /* sanity check resp_len*/
105 if (resp_len < TPM_HEADER_SIZE) {
106 debug("%s:tpm response header too small\n", __func__);
107 return -EIO;
108 }
109 if (resp_len > MAX_RESPONSE_SIZE) {
110 debug("%s:resp_len=%zd exceeds MAX_RESPONSE_SIZE\n",
111 __func__, resp_len);
112 return -EIO;
113 }
114 if (resp_len > *recv_len) {
115 debug("%s:response length is bigger than receive buffer\n",
116 __func__);
117 return -EIO;
118 }
119
120 /* sanity checks look good, copy the response */
121 memcpy(recvbuf, resp_buf, resp_len);
122 *recv_len = resp_len;
123
124 return 0;
125}
126
127static int ftpm_tee_open(struct udevice *dev)
128{
129 struct ftpm_tee_private *context = dev_get_priv(dev);
130
131 if (context->is_open)
132 return -EBUSY;
133
134 context->is_open = 1;
135
136 return 0;
137}
138
139static int ftpm_tee_close(struct udevice *dev)
140{
141 struct ftpm_tee_private *context = dev_get_priv(dev);
142
143 if (context->is_open)
144 context->is_open = 0;
145
146 return 0;
147}
148
149static int ftpm_tee_desc(struct udevice *dev, char *buf, int size)
150{
151 if (size < 32)
152 return -ENOSPC;
153
154 return snprintf(buf, size, "Microsoft OP-TEE fTPM");
155}
156
157static int ftpm_tee_match(struct tee_version_data *vers, const void *data)
158{
159 debug("%s:vers->gen_caps =0x%x\n", __func__, vers->gen_caps);
160
161 /*
162 * Currently this driver only support GP Complaint OPTEE based fTPM TA
163 */
164 return vers->gen_caps & TEE_GEN_CAP_GP;
165}
166
167static int ftpm_tee_probe(struct udevice *dev)
168{
169 int rc;
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;
174
175 memset(context, 0, sizeof(*context));
176
177 /* Use the TPM v2 stack */
178 priv->version = TPM_V2;
179 priv->pcr_count = 24;
180 priv->pcr_select_min = 3;
181
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__);
186 return -ENODEV;
187 }
188
189 /* Open a session with the fTPM TA */
190 memset(&sess_arg, 0, sizeof(sess_arg));
Etienne Carriere33ba8032022-12-07 16:25:33 +0100191 sess_arg.clnt_login = TEE_LOGIN_REE_KERNEL;
Thirupathaiah Annapureddy8d73be72020-01-12 23:34:22 -0800192 tee_optee_ta_uuid_to_octets(sess_arg.uuid, &uuid);
193
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);
198 return -EIO;
199 }
200 context->session = sess_arg.session;
201
202 /* Allocate dynamic shared memory with fTPM TA */
203 rc = tee_shm_alloc(context->tee_dev,
204 MAX_COMMAND_SIZE + MAX_RESPONSE_SIZE,
205 0, &context->shm);
206 if (rc) {
207 debug("%s:tee_shm_alloc failed with rc = %d\n", __func__, rc);
208 goto out_shm_alloc;
209 }
210
211 return 0;
212
213out_shm_alloc:
214 tee_close_session(context->tee_dev, context->session);
215
216 return rc;
217}
218
219static int ftpm_tee_remove(struct udevice *dev)
220{
221 struct ftpm_tee_private *context = dev_get_priv(dev);
222 int rc;
223
224 /* tee_pre_remove frees any leftover TEE shared memory */
225
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);
229
230 return 0;
231}
232
233static 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,
238};
239
240static const struct udevice_id ftpm_tee_ids[] = {
241 { .compatible = "microsoft,ftpm" },
242 { }
243};
244
245U_BOOT_DRIVER(ftpm_tee) = {
246 .name = "ftpm_tee",
247 .id = UCLASS_TPM,
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,
Simon Glass41575d82020-12-03 16:55:17 -0700253 .priv_auto = sizeof(struct ftpm_tee_private),
Thirupathaiah Annapureddy8d73be72020-01-12 23:34:22 -0800254};