Prepare v2023.10
[platform/kernel/u-boot.git] / drivers / tpm / tpm_tis_sandbox.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) 2013 Google, Inc
4  */
5
6 #include <common.h>
7 #include <display_options.h>
8 #include <dm.h>
9 #include <tpm-v1.h>
10 #include <asm/state.h>
11 #include <asm/unaligned.h>
12 #include <u-boot/crc.h>
13 #include "sandbox_common.h"
14
15 #define NV_DATA_PUBLIC_PERMISSIONS_OFFSET       60
16
17 /*
18  * Information about our TPM emulation. This is preserved in the sandbox
19  * state file if enabled.
20  */
21 static struct tpm_state {
22         bool valid;
23         struct nvdata_state nvdata[NV_SEQ_COUNT];
24 } s_state, *g_state;
25
26 /**
27  * sandbox_tpm_read_state() - read the sandbox EC state from the state file
28  *
29  * If data is available, then blob and node will provide access to it. If
30  * not this function sets up an empty TPM.
31  *
32  * @blob: Pointer to device tree blob, or NULL if no data to read
33  * @node: Node offset to read from
34  */
35 static int sandbox_tpm_read_state(const void *blob, int node)
36 {
37         struct tpm_state *state = &s_state;
38         const char *prop;
39         int len;
40         int i;
41
42         if (!blob)
43                 return 0;
44
45         for (i = 0; i < NV_SEQ_COUNT; i++) {
46                 struct nvdata_state *nvd = &state->nvdata[i];
47                 char prop_name[20];
48
49                 sprintf(prop_name, "nvdata%d", i);
50                 prop = fdt_getprop(blob, node, prop_name, &len);
51                 if (len >= NV_DATA_SIZE)
52                         return log_msg_ret("nvd", -E2BIG);
53                 if (prop) {
54                         memcpy(nvd->data, prop, len);
55                         nvd->length = len;
56                         nvd->present = true;
57                 }
58         }
59
60         s_state.valid = true;
61
62         return 0;
63 }
64
65 /**
66  * sandbox_tpm_write_state() - Write out our state to the state file
67  *
68  * The caller will ensure that there is a node ready for the state. The node
69  * may already contain the old state, in which case it is overridden.
70  *
71  * @blob: Device tree blob holding state
72  * @node: Node to write our state into
73  */
74 static int sandbox_tpm_write_state(void *blob, int node)
75 {
76         const struct tpm_state *state = g_state;
77         int i;
78
79         if (!state)
80                 return 0;
81
82         /*
83          * We are guaranteed enough space to write basic properties.
84          * We could use fdt_add_subnode() to put each set of data in its
85          * own node - perhaps useful if we add access informaiton to each.
86          */
87         for (i = 0; i < NV_SEQ_COUNT; i++) {
88                 const struct nvdata_state *nvd = &state->nvdata[i];
89                 char prop_name[20];
90
91                 if (nvd->present) {
92                         snprintf(prop_name, sizeof(prop_name), "nvdata%d", i);
93                         fdt_setprop(blob, node, prop_name, nvd->data,
94                                     nvd->length);
95                 }
96         }
97
98         return 0;
99 }
100
101 SANDBOX_STATE_IO(sandbox_tpm, "google,sandbox-tpm", sandbox_tpm_read_state,
102                  sandbox_tpm_write_state);
103
104 static void handle_cap_flag_space(u8 **datap, uint index)
105 {
106         struct tpm_nv_data_public pub;
107
108         /* TPM_NV_PER_PPWRITE */
109         memset(&pub, '\0', sizeof(pub));
110         pub.nv_index = __cpu_to_be32(index);
111         pub.pcr_info_read.pcr_selection.size_of_select = __cpu_to_be16(
112                 sizeof(pub.pcr_info_read.pcr_selection.pcr_select));
113         pub.permission.attributes = __cpu_to_be32(1);
114         pub.pcr_info_write = pub.pcr_info_read;
115         memcpy(*datap, &pub, sizeof(pub));
116         *datap += sizeof(pub);
117 }
118
119 static int sandbox_tpm_xfer(struct udevice *dev, const uint8_t *sendbuf,
120                             size_t send_size, uint8_t *recvbuf,
121                             size_t *recv_len)
122 {
123         struct tpm_state *tpm = dev_get_priv(dev);
124         uint32_t code, index, length, type;
125         uint8_t *data;
126         int seq;
127
128         code = get_unaligned_be32(sendbuf + sizeof(uint16_t) +
129                                   sizeof(uint32_t));
130 #ifdef DEBUG
131         printf("tpm: %zd bytes, recv_len %zd, cmd = %x\n", send_size,
132                *recv_len, code);
133         print_buffer(0, sendbuf, 1, send_size, 0);
134 #endif
135         switch (code) {
136         case TPM_CMD_GET_CAPABILITY:
137                 type = get_unaligned_be32(sendbuf + 14);
138                 switch (type) {
139                 case TPM_CAP_FLAG:
140                         index = get_unaligned_be32(sendbuf + 18);
141                         printf("Get flags index %#02x\n", index);
142                         *recv_len = 22;
143                         memset(recvbuf, '\0', *recv_len);
144                         data = recvbuf + TPM_HDR_LEN + sizeof(uint32_t);
145                         switch (index) {
146                         case FIRMWARE_NV_INDEX:
147                                 break;
148                         case KERNEL_NV_INDEX:
149                                 handle_cap_flag_space(&data, index);
150                                 *recv_len = data - recvbuf;
151                                 break;
152                         case TPM_CAP_FLAG_PERMANENT: {
153                                 struct tpm_permanent_flags *pflags;
154
155                                 pflags = (struct tpm_permanent_flags *)data;
156                                 memset(pflags, '\0', sizeof(*pflags));
157                                 put_unaligned_be32(TPM_TAG_PERMANENT_FLAGS,
158                                                    &pflags->tag);
159                                 *recv_len = TPM_HEADER_SIZE + 4 +
160                                                 sizeof(*pflags);
161                                 break;
162                         }
163                         default:
164                                 printf("   ** Unknown flags index %x\n", index);
165                                 return -ENOSYS;
166                         }
167                         put_unaligned_be32(*recv_len, recvbuf + TPM_HDR_LEN);
168                         break;
169                 case TPM_CAP_NV_INDEX:
170                         index = get_unaligned_be32(sendbuf + 18);
171                         printf("Get cap nv index %#02x\n", index);
172                         put_unaligned_be32(22, recvbuf + TPM_HDR_LEN);
173                         break;
174                 default:
175                         printf("   ** Unknown 0x65 command type %#02x\n",
176                                type);
177                         return -ENOSYS;
178                 }
179                 break;
180         case TPM_CMD_NV_WRITE_VALUE:
181                 index = get_unaligned_be32(sendbuf + 10);
182                 length = get_unaligned_be32(sendbuf + 18);
183                 seq = sb_tpm_index_to_seq(index);
184                 if (seq < 0)
185                         return -EINVAL;
186                 printf("tpm: nvwrite index=%#02x, len=%#02x\n", index, length);
187                 sb_tpm_write_data(tpm->nvdata, seq, sendbuf, 22, length);
188                 break;
189         case TPM_CMD_NV_READ_VALUE: /* nvread */
190                 index = get_unaligned_be32(sendbuf + 10);
191                 length = get_unaligned_be32(sendbuf + 18);
192                 seq = sb_tpm_index_to_seq(index);
193                 if (seq < 0)
194                         return -EINVAL;
195                 printf("tpm: nvread index=%#02x, len=%#02x, seq=%#02x\n", index,
196                        length, seq);
197                 *recv_len = TPM_HDR_LEN + sizeof(uint32_t) + length;
198                 memset(recvbuf, '\0', *recv_len);
199                 put_unaligned_be32(length, recvbuf + TPM_HDR_LEN);
200                 sb_tpm_read_data(tpm->nvdata, seq, recvbuf, TPM_HDR_LEN + 4,
201                                  length);
202                 break;
203         case TPM_CMD_EXTEND:
204                 *recv_len = 30;
205                 memset(recvbuf, '\0', *recv_len);
206                 break;
207         case TPM_CMD_NV_DEFINE_SPACE:
208                 index = get_unaligned_be32(sendbuf + 12);
209                 length = get_unaligned_be32(sendbuf + 77);
210                 seq = sb_tpm_index_to_seq(index);
211                 if (seq < 0)
212                         return -EINVAL;
213                 printf("tpm: define_space index=%#02x, len=%#02x, seq=%#02x\n",
214                        index, length, seq);
215                 sb_tpm_define_data(tpm->nvdata, seq, length);
216                 *recv_len = 12;
217                 memset(recvbuf, '\0', *recv_len);
218                 break;
219         case 0x15: /* pcr read */
220         case 0x5d: /* force clear */
221         case 0x6f: /* physical enable */
222         case 0x72: /* physical set deactivated */
223         case 0x99: /* startup */
224         case 0x50: /* self test full */
225         case 0x4000000a:  /* assert physical presence */
226                 *recv_len = 12;
227                 memset(recvbuf, '\0', *recv_len);
228                 break;
229         default:
230                 printf("Unknown tpm command %02x\n", code);
231                 return -ENOSYS;
232         }
233 #ifdef DEBUG
234         printf("tpm: rx recv_len %zd\n", *recv_len);
235         print_buffer(0, recvbuf, 1, *recv_len, 0);
236 #endif
237
238         return 0;
239 }
240
241 static int sandbox_tpm_get_desc(struct udevice *dev, char *buf, int size)
242 {
243         if (size < 15)
244                 return -ENOSPC;
245
246         return snprintf(buf, size, "sandbox TPM");
247 }
248
249 static int sandbox_tpm_probe(struct udevice *dev)
250 {
251         struct tpm_state *tpm = dev_get_priv(dev);
252
253         if (s_state.valid)
254                 memcpy(tpm, &s_state, sizeof(*tpm));
255         g_state = tpm;
256
257         return 0;
258 }
259
260 static int sandbox_tpm_open(struct udevice *dev)
261 {
262         return 0;
263 }
264
265 static int sandbox_tpm_close(struct udevice *dev)
266 {
267         return 0;
268 }
269
270 static const struct tpm_ops sandbox_tpm_ops = {
271         .open           = sandbox_tpm_open,
272         .close          = sandbox_tpm_close,
273         .get_desc       = sandbox_tpm_get_desc,
274         .xfer           = sandbox_tpm_xfer,
275 };
276
277 static const struct udevice_id sandbox_tpm_ids[] = {
278         { .compatible = "google,sandbox-tpm" },
279         { }
280 };
281
282 U_BOOT_DRIVER(google_sandbox_tpm) = {
283         .name   = "google_sandbox_tpm",
284         .id     = UCLASS_TPM,
285         .of_match = sandbox_tpm_ids,
286         .ops    = &sandbox_tpm_ops,
287         .probe  = sandbox_tpm_probe,
288         .priv_auto      = sizeof(struct tpm_state),
289 };