blob: c287ca2278fc4ecc5ff05ae3703f9402ebcc11bf [file] [log] [blame]
Miquel Raynal2bae7122018-05-15 11:57:25 +02001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (c) 2018, Bootlin
4 * Author: Miquel Raynal <miquel.raynal@bootlin.com>
5 */
6
7#include <common.h>
8#include <dm.h>
9#include <tpm-v2.h>
10#include <asm/state.h>
11#include <asm/unaligned.h>
Simon Glasscd93d622020-05-10 11:40:13 -060012#include <linux/bitops.h>
Simon Glassc3a4d1c2019-11-14 12:57:14 -070013#include <u-boot/crc.h>
Miquel Raynal2bae7122018-05-15 11:57:25 +020014
15/* Hierarchies */
16enum tpm2_hierarchy {
17 TPM2_HIERARCHY_LOCKOUT = 0,
18 TPM2_HIERARCHY_ENDORSEMENT,
19 TPM2_HIERARCHY_PLATFORM,
20 TPM2_HIERARCHY_NB,
21};
22
23/* Subset of supported capabilities */
24enum tpm2_capability {
25 TPM_CAP_TPM_PROPERTIES = 0x6,
26};
27
28/* Subset of supported properties */
29#define TPM2_PROPERTIES_OFFSET 0x0000020E
30
31enum tpm2_cap_tpm_property {
32 TPM2_FAIL_COUNTER = 0,
33 TPM2_PROP_MAX_TRIES,
34 TPM2_RECOVERY_TIME,
35 TPM2_LOCKOUT_RECOVERY,
36 TPM2_PROPERTY_NB,
37};
38
39#define SANDBOX_TPM_PCR_NB 1
40
41static const u8 sandbox_extended_once_pcr[] = {
42 0xf5, 0xa5, 0xfd, 0x42, 0xd1, 0x6a, 0x20, 0x30,
43 0x27, 0x98, 0xef, 0x6e, 0xd3, 0x09, 0x97, 0x9b,
44 0x43, 0x00, 0x3d, 0x23, 0x20, 0xd9, 0xf0, 0xe8,
45 0xea, 0x98, 0x31, 0xa9, 0x27, 0x59, 0xfb, 0x4b,
46};
47
Simon Glass46aed062021-07-18 14:18:01 -060048/*
49 * Information about our TPM emulation. This is preserved in the sandbox
50 * state file if enabled.
51 *
Simon Glass0c0ddad2021-07-18 14:18:02 -060052 * @valid: true if this is valid (only used in s_state)
Simon Glass46aed062021-07-18 14:18:01 -060053 * @init_done: true if open() has been called
54 * @startup_done: true if TPM2_CC_STARTUP has been processed
55 * @tests_done: true if TPM2_CC_SELF_TEST has be processed
56 * @pw: TPM password per hierarchy
57 * @pw_sz: Size of each password in bytes
58 * @properties: TPM properties
59 * @pcr: TPM Platform Configuration Registers. Each of these holds a hash and
60 * can be 'extended' a number of times, meaning another hash is added into
61 * its value (initial value all zeroes)
62 * @pcr_extensions: Number of times each PCR has been extended (starts at 0)
63 * @nvdata: non-volatile data, used to store important things for the platform
64 */
Miquel Raynal2bae7122018-05-15 11:57:25 +020065struct sandbox_tpm2 {
Simon Glass0c0ddad2021-07-18 14:18:02 -060066 bool valid;
Miquel Raynal2bae7122018-05-15 11:57:25 +020067 /* TPM internal states */
68 bool init_done;
69 bool startup_done;
70 bool tests_done;
Miquel Raynal2bae7122018-05-15 11:57:25 +020071 char pw[TPM2_HIERARCHY_NB][TPM2_DIGEST_LEN + 1];
72 int pw_sz[TPM2_HIERARCHY_NB];
Miquel Raynal2bae7122018-05-15 11:57:25 +020073 u32 properties[TPM2_PROPERTY_NB];
Miquel Raynal2bae7122018-05-15 11:57:25 +020074 u8 pcr[SANDBOX_TPM_PCR_NB][TPM2_DIGEST_LEN];
Miquel Raynal2bae7122018-05-15 11:57:25 +020075 u32 pcr_extensions[SANDBOX_TPM_PCR_NB];
76};
77
Simon Glass0c0ddad2021-07-18 14:18:02 -060078static struct sandbox_tpm2 s_state, *g_state;
79
Miquel Raynal2bae7122018-05-15 11:57:25 +020080/*
81 * Check the tag validity depending on the command (authentication required or
82 * not). If authentication is required, check it is valid. Update the auth
83 * pointer to point to the next chunk of data to process if needed.
84 */
85static int sandbox_tpm2_check_session(struct udevice *dev, u32 command, u16 tag,
86 const u8 **auth,
87 enum tpm2_hierarchy *hierarchy)
88{
89 struct sandbox_tpm2 *tpm = dev_get_priv(dev);
90 u32 handle, auth_sz, session_handle;
91 u16 nonce_sz, pw_sz;
92 const char *pw;
93
94 switch (command) {
95 case TPM2_CC_STARTUP:
96 case TPM2_CC_SELF_TEST:
97 case TPM2_CC_GET_CAPABILITY:
98 case TPM2_CC_PCR_READ:
99 if (tag != TPM2_ST_NO_SESSIONS) {
100 printf("No session required for command 0x%x\n",
101 command);
102 return TPM2_RC_BAD_TAG;
103 }
104
105 return 0;
106
107 case TPM2_CC_CLEAR:
108 case TPM2_CC_HIERCHANGEAUTH:
109 case TPM2_CC_DAM_RESET:
110 case TPM2_CC_DAM_PARAMETERS:
111 case TPM2_CC_PCR_EXTEND:
112 if (tag != TPM2_ST_SESSIONS) {
113 printf("Session required for command 0x%x\n", command);
114 return TPM2_RC_AUTH_CONTEXT;
115 }
116
117 handle = get_unaligned_be32(*auth);
118 *auth += sizeof(handle);
119
120 /*
121 * PCR_Extend had a different protection mechanism and does not
122 * use the same standards as other commands.
123 */
124 if (command == TPM2_CC_PCR_EXTEND)
125 break;
126
127 switch (handle) {
128 case TPM2_RH_LOCKOUT:
129 *hierarchy = TPM2_HIERARCHY_LOCKOUT;
130 break;
131 case TPM2_RH_ENDORSEMENT:
132 if (command == TPM2_CC_CLEAR) {
133 printf("Endorsement hierarchy unsupported\n");
134 return TPM2_RC_AUTH_MISSING;
135 }
136 *hierarchy = TPM2_HIERARCHY_ENDORSEMENT;
137 break;
138 case TPM2_RH_PLATFORM:
139 *hierarchy = TPM2_HIERARCHY_PLATFORM;
140 break;
141 default:
142 printf("Wrong handle 0x%x\n", handle);
143 return TPM2_RC_VALUE;
144 }
145
146 break;
147
148 default:
149 printf("Command code not recognized: 0x%x\n", command);
150 return TPM2_RC_COMMAND_CODE;
151 }
152
153 auth_sz = get_unaligned_be32(*auth);
154 *auth += sizeof(auth_sz);
155
156 session_handle = get_unaligned_be32(*auth);
157 *auth += sizeof(session_handle);
158 if (session_handle != TPM2_RS_PW) {
159 printf("Wrong session handle 0x%x\n", session_handle);
160 return TPM2_RC_VALUE;
161 }
162
163 nonce_sz = get_unaligned_be16(*auth);
164 *auth += sizeof(nonce_sz);
165 if (nonce_sz) {
166 printf("Nonces not supported in Sandbox, aborting\n");
167 return TPM2_RC_HANDLE;
168 }
169
170 /* Ignore attributes */
171 *auth += sizeof(u8);
172
173 pw_sz = get_unaligned_be16(*auth);
174 *auth += sizeof(pw_sz);
175 if (auth_sz != (9 + nonce_sz + pw_sz)) {
176 printf("Authentication size (%d) do not match %d\n",
177 auth_sz, 9 + nonce_sz + pw_sz);
178 return TPM2_RC_SIZE;
179 }
180
181 /* No passwork is acceptable */
182 if (!pw_sz && !tpm->pw_sz[*hierarchy])
183 return TPM2_RC_SUCCESS;
184
185 /* Password is too long */
186 if (pw_sz > TPM2_DIGEST_LEN) {
187 printf("Password should not be more than %dB\n",
188 TPM2_DIGEST_LEN);
189 return TPM2_RC_AUTHSIZE;
190 }
191
192 pw = (const char *)*auth;
193 *auth += pw_sz;
194
195 /* Password is wrong */
196 if (pw_sz != tpm->pw_sz[*hierarchy] ||
197 strncmp(pw, tpm->pw[*hierarchy], tpm->pw_sz[*hierarchy])) {
198 printf("Authentication failed: wrong password.\n");
199 return TPM2_RC_BAD_AUTH;
200 }
201
202 return TPM2_RC_SUCCESS;
203}
204
205static int sandbox_tpm2_check_readyness(struct udevice *dev, int command)
206{
207 struct sandbox_tpm2 *tpm = dev_get_priv(dev);
208
209 switch (command) {
210 case TPM2_CC_STARTUP:
211 if (!tpm->init_done || tpm->startup_done)
212 return TPM2_RC_INITIALIZE;
213
214 break;
215 case TPM2_CC_GET_CAPABILITY:
216 if (!tpm->init_done || !tpm->startup_done)
217 return TPM2_RC_INITIALIZE;
218
219 break;
220 case TPM2_CC_SELF_TEST:
221 if (!tpm->startup_done)
222 return TPM2_RC_INITIALIZE;
223
224 break;
225 default:
226 if (!tpm->tests_done)
227 return TPM2_RC_NEEDS_TEST;
228
229 break;
230 }
231
232 return 0;
233}
234
Miquel Raynal46703cd2018-08-05 18:53:07 +0200235static int sandbox_tpm2_fill_buf(u8 *recv, size_t *recv_len, u16 tag, u32 rc)
Miquel Raynal2bae7122018-05-15 11:57:25 +0200236{
237 *recv_len = sizeof(tag) + sizeof(u32) + sizeof(rc);
238
239 /* Write tag */
Miquel Raynal46703cd2018-08-05 18:53:07 +0200240 put_unaligned_be16(tag, recv);
241 recv += sizeof(tag);
Miquel Raynal2bae7122018-05-15 11:57:25 +0200242
243 /* Write length */
Miquel Raynal46703cd2018-08-05 18:53:07 +0200244 put_unaligned_be32(*recv_len, recv);
245 recv += sizeof(u32);
Miquel Raynal2bae7122018-05-15 11:57:25 +0200246
247 /* Write return code */
Miquel Raynal46703cd2018-08-05 18:53:07 +0200248 put_unaligned_be32(rc, recv);
249 recv += sizeof(rc);
Miquel Raynal2bae7122018-05-15 11:57:25 +0200250
251 /* Add trailing \0 */
Miquel Raynal46703cd2018-08-05 18:53:07 +0200252 *recv = '\0';
Miquel Raynal2bae7122018-05-15 11:57:25 +0200253
254 return 0;
255}
256
257static int sandbox_tpm2_extend(struct udevice *dev, int pcr_index,
258 const u8 *extension)
259{
260 struct sandbox_tpm2 *tpm = dev_get_priv(dev);
261 int i;
262
263 /* Only simulate the first extensions from all '0' with only '0' */
264 for (i = 0; i < TPM2_DIGEST_LEN; i++)
265 if (tpm->pcr[pcr_index][i] || extension[i])
266 return TPM2_RC_FAILURE;
267
268 memcpy(tpm->pcr[pcr_index], sandbox_extended_once_pcr,
269 TPM2_DIGEST_LEN);
270 tpm->pcr_extensions[pcr_index]++;
271
272 return 0;
273};
274
275static int sandbox_tpm2_xfer(struct udevice *dev, const u8 *sendbuf,
276 size_t send_size, u8 *recvbuf,
277 size_t *recv_len)
278{
279 struct sandbox_tpm2 *tpm = dev_get_priv(dev);
280 enum tpm2_hierarchy hierarchy = 0;
281 const u8 *sent = sendbuf;
282 u8 *recv = recvbuf;
283 u32 length, command, rc = 0;
284 u16 tag, mode, new_pw_sz;
285 u8 yes_no;
286 int i, j;
287
288 /* TPM2_GetProperty */
289 u32 capability, property, property_count;
290
291 /* TPM2_PCR_Read/Extend variables */
Miquel Raynalfd973ca2018-08-05 18:53:06 +0200292 int pcr_index = 0;
Miquel Raynal2bae7122018-05-15 11:57:25 +0200293 u64 pcr_map = 0;
294 u32 selections, pcr_nb;
295 u16 alg;
296 u8 pcr_array_sz;
297
298 tag = get_unaligned_be16(sent);
299 sent += sizeof(tag);
300
301 length = get_unaligned_be32(sent);
302 sent += sizeof(length);
303 if (length != send_size) {
Simon Glasse7c920a2021-02-07 14:27:04 -0700304 printf("TPM2: Unmatching length, received: %zd, expected: %d\n",
Miquel Raynal2bae7122018-05-15 11:57:25 +0200305 send_size, length);
306 rc = TPM2_RC_SIZE;
Miquel Raynal46703cd2018-08-05 18:53:07 +0200307 sandbox_tpm2_fill_buf(recv, recv_len, tag, rc);
Miquel Raynal2bae7122018-05-15 11:57:25 +0200308 return 0;
309 }
310
311 command = get_unaligned_be32(sent);
312 sent += sizeof(command);
313 rc = sandbox_tpm2_check_readyness(dev, command);
314 if (rc) {
Miquel Raynal46703cd2018-08-05 18:53:07 +0200315 sandbox_tpm2_fill_buf(recv, recv_len, tag, rc);
Miquel Raynal2bae7122018-05-15 11:57:25 +0200316 return 0;
317 }
318
319 rc = sandbox_tpm2_check_session(dev, command, tag, &sent, &hierarchy);
320 if (rc) {
Miquel Raynal46703cd2018-08-05 18:53:07 +0200321 sandbox_tpm2_fill_buf(recv, recv_len, tag, rc);
Miquel Raynal2bae7122018-05-15 11:57:25 +0200322 return 0;
323 }
324
325 switch (command) {
326 case TPM2_CC_STARTUP:
327 mode = get_unaligned_be16(sent);
328 sent += sizeof(mode);
329 switch (mode) {
330 case TPM2_SU_CLEAR:
331 case TPM2_SU_STATE:
332 break;
333 default:
334 rc = TPM2_RC_VALUE;
335 }
336
337 tpm->startup_done = true;
338
Miquel Raynal46703cd2018-08-05 18:53:07 +0200339 sandbox_tpm2_fill_buf(recv, recv_len, tag, rc);
Miquel Raynal2bae7122018-05-15 11:57:25 +0200340 break;
341
342 case TPM2_CC_SELF_TEST:
343 yes_no = *sent;
344 sent += sizeof(yes_no);
345 switch (yes_no) {
346 case TPMI_YES:
347 case TPMI_NO:
348 break;
349 default:
350 rc = TPM2_RC_VALUE;
351 }
352
353 tpm->tests_done = true;
354
Miquel Raynal46703cd2018-08-05 18:53:07 +0200355 sandbox_tpm2_fill_buf(recv, recv_len, tag, rc);
Miquel Raynal2bae7122018-05-15 11:57:25 +0200356 break;
357
358 case TPM2_CC_CLEAR:
359 /* Reset this hierarchy password */
360 tpm->pw_sz[hierarchy] = 0;
361
362 /* Reset all password if thisis the PLATFORM hierarchy */
363 if (hierarchy == TPM2_HIERARCHY_PLATFORM)
364 for (i = 0; i < TPM2_HIERARCHY_NB; i++)
365 tpm->pw_sz[i] = 0;
366
367 /* Reset the properties */
368 for (i = 0; i < TPM2_PROPERTY_NB; i++)
369 tpm->properties[i] = 0;
370
371 /* Reset the PCRs and their number of extensions */
372 for (i = 0; i < SANDBOX_TPM_PCR_NB; i++) {
373 tpm->pcr_extensions[i] = 0;
374 for (j = 0; j < TPM2_DIGEST_LEN; j++)
375 tpm->pcr[i][j] = 0;
376 }
377
Miquel Raynal46703cd2018-08-05 18:53:07 +0200378 sandbox_tpm2_fill_buf(recv, recv_len, tag, rc);
Miquel Raynal2bae7122018-05-15 11:57:25 +0200379 break;
380
381 case TPM2_CC_HIERCHANGEAUTH:
382 new_pw_sz = get_unaligned_be16(sent);
383 sent += sizeof(new_pw_sz);
384 if (new_pw_sz > TPM2_DIGEST_LEN) {
385 rc = TPM2_RC_SIZE;
386 } else if (new_pw_sz) {
387 tpm->pw_sz[hierarchy] = new_pw_sz;
388 memcpy(tpm->pw[hierarchy], sent, new_pw_sz);
389 sent += new_pw_sz;
390 }
391
Miquel Raynal46703cd2018-08-05 18:53:07 +0200392 sandbox_tpm2_fill_buf(recv, recv_len, tag, rc);
Miquel Raynal2bae7122018-05-15 11:57:25 +0200393 break;
394
395 case TPM2_CC_GET_CAPABILITY:
396 capability = get_unaligned_be32(sent);
397 sent += sizeof(capability);
398 if (capability != TPM_CAP_TPM_PROPERTIES) {
399 printf("Sandbox TPM only support TPM_CAPABILITIES\n");
400 return TPM2_RC_HANDLE;
401 }
402
403 property = get_unaligned_be32(sent);
404 sent += sizeof(property);
405 property -= TPM2_PROPERTIES_OFFSET;
406
407 property_count = get_unaligned_be32(sent);
408 sent += sizeof(property_count);
409 if (!property_count ||
410 property + property_count > TPM2_PROPERTY_NB) {
411 rc = TPM2_RC_HANDLE;
Miquel Raynal46703cd2018-08-05 18:53:07 +0200412 return sandbox_tpm2_fill_buf(recv, recv_len, tag, rc);
Miquel Raynal2bae7122018-05-15 11:57:25 +0200413 }
414
415 /* Write tag */
416 put_unaligned_be16(tag, recv);
417 recv += sizeof(tag);
418
419 /* Ignore length for now */
420 recv += sizeof(u32);
421
422 /* Write return code */
423 put_unaligned_be32(rc, recv);
424 recv += sizeof(rc);
425
426 /* Tell there is more data to read */
427 *recv = TPMI_YES;
428 recv += sizeof(yes_no);
429
430 /* Repeat the capability */
431 put_unaligned_be32(capability, recv);
432 recv += sizeof(capability);
433
434 /* Give the number of properties that follow */
435 put_unaligned_be32(property_count, recv);
436 recv += sizeof(property_count);
437
438 /* Fill with the properties */
439 for (i = 0; i < property_count; i++) {
440 put_unaligned_be32(TPM2_PROPERTIES_OFFSET + property +
441 i, recv);
442 recv += sizeof(property);
443 put_unaligned_be32(tpm->properties[property + i],
444 recv);
445 recv += sizeof(property);
446 }
447
448 /* Add trailing \0 */
449 *recv = '\0';
450
451 /* Write response length */
452 *recv_len = recv - recvbuf;
453 put_unaligned_be32(*recv_len, recvbuf + sizeof(tag));
454
455 break;
456
457 case TPM2_CC_DAM_PARAMETERS:
458 tpm->properties[TPM2_PROP_MAX_TRIES] = get_unaligned_be32(sent);
459 sent += sizeof(*tpm->properties);
460 tpm->properties[TPM2_RECOVERY_TIME] = get_unaligned_be32(sent);
461 sent += sizeof(*tpm->properties);
462 tpm->properties[TPM2_LOCKOUT_RECOVERY] = get_unaligned_be32(sent);
463 sent += sizeof(*tpm->properties);
464
Miquel Raynal46703cd2018-08-05 18:53:07 +0200465 sandbox_tpm2_fill_buf(recv, recv_len, tag, rc);
Miquel Raynal2bae7122018-05-15 11:57:25 +0200466 break;
467
468 case TPM2_CC_PCR_READ:
469 selections = get_unaligned_be32(sent);
470 sent += sizeof(selections);
471 if (selections != 1) {
472 printf("Sandbox cannot handle more than one PCR\n");
473 rc = TPM2_RC_VALUE;
Miquel Raynal46703cd2018-08-05 18:53:07 +0200474 return sandbox_tpm2_fill_buf(recv, recv_len, tag, rc);
Miquel Raynal2bae7122018-05-15 11:57:25 +0200475 }
476
477 alg = get_unaligned_be16(sent);
478 sent += sizeof(alg);
479 if (alg != TPM2_ALG_SHA256) {
480 printf("Sandbox TPM only handle SHA256 algorithm\n");
481 rc = TPM2_RC_VALUE;
Miquel Raynal46703cd2018-08-05 18:53:07 +0200482 return sandbox_tpm2_fill_buf(recv, recv_len, tag, rc);
Miquel Raynal2bae7122018-05-15 11:57:25 +0200483 }
484
485 pcr_array_sz = *sent;
486 sent += sizeof(pcr_array_sz);
487 if (!pcr_array_sz || pcr_array_sz > 8) {
488 printf("Sandbox TPM cannot handle so much PCRs\n");
489 rc = TPM2_RC_VALUE;
Miquel Raynal46703cd2018-08-05 18:53:07 +0200490 return sandbox_tpm2_fill_buf(recv, recv_len, tag, rc);
Miquel Raynal2bae7122018-05-15 11:57:25 +0200491 }
492
493 for (i = 0; i < pcr_array_sz; i++)
494 pcr_map += (u64)sent[i] << (i * 8);
495
496 if (pcr_map >> SANDBOX_TPM_PCR_NB) {
497 printf("Sandbox TPM handles up to %d PCR(s)\n",
498 SANDBOX_TPM_PCR_NB);
499 rc = TPM2_RC_VALUE;
Miquel Raynal46703cd2018-08-05 18:53:07 +0200500 return sandbox_tpm2_fill_buf(recv, recv_len, tag, rc);
Miquel Raynal2bae7122018-05-15 11:57:25 +0200501 }
502
Miquel Raynalfd973ca2018-08-05 18:53:06 +0200503 if (!pcr_map) {
504 printf("Empty PCR map.\n");
Miquel Raynal2bae7122018-05-15 11:57:25 +0200505 rc = TPM2_RC_VALUE;
Miquel Raynal46703cd2018-08-05 18:53:07 +0200506 return sandbox_tpm2_fill_buf(recv, recv_len, tag, rc);
Miquel Raynal2bae7122018-05-15 11:57:25 +0200507 }
508
509 for (i = 0; i < SANDBOX_TPM_PCR_NB; i++)
510 if (pcr_map & BIT(i))
511 pcr_index = i;
512
513 /* Write tag */
514 put_unaligned_be16(tag, recv);
515 recv += sizeof(tag);
516
517 /* Ignore length for now */
518 recv += sizeof(u32);
519
520 /* Write return code */
521 put_unaligned_be32(rc, recv);
522 recv += sizeof(rc);
523
524 /* Number of extensions */
525 put_unaligned_be32(tpm->pcr_extensions[pcr_index], recv);
526 recv += sizeof(u32);
527
528 /* Copy the PCR */
529 memcpy(recv, tpm->pcr[pcr_index], TPM2_DIGEST_LEN);
530 recv += TPM2_DIGEST_LEN;
531
532 /* Add trailing \0 */
533 *recv = '\0';
534
535 /* Write response length */
536 *recv_len = recv - recvbuf;
537 put_unaligned_be32(*recv_len, recvbuf + sizeof(tag));
538
539 break;
540
541 case TPM2_CC_PCR_EXTEND:
542 /* Get the PCR index */
543 pcr_index = get_unaligned_be32(sendbuf + sizeof(tag) +
544 sizeof(length) +
545 sizeof(command));
546 if (pcr_index > SANDBOX_TPM_PCR_NB) {
547 printf("Sandbox TPM handles up to %d PCR(s)\n",
548 SANDBOX_TPM_PCR_NB);
549 rc = TPM2_RC_VALUE;
550 }
551
552 /* Check the number of hashes */
553 pcr_nb = get_unaligned_be32(sent);
554 sent += sizeof(pcr_nb);
555 if (pcr_nb != 1) {
556 printf("Sandbox cannot handle more than one PCR\n");
557 rc = TPM2_RC_VALUE;
Miquel Raynal46703cd2018-08-05 18:53:07 +0200558 return sandbox_tpm2_fill_buf(recv, recv_len, tag, rc);
Miquel Raynal2bae7122018-05-15 11:57:25 +0200559 }
560
561 /* Check the hash algorithm */
562 alg = get_unaligned_be16(sent);
563 sent += sizeof(alg);
564 if (alg != TPM2_ALG_SHA256) {
565 printf("Sandbox TPM only handle SHA256 algorithm\n");
566 rc = TPM2_RC_VALUE;
Miquel Raynal46703cd2018-08-05 18:53:07 +0200567 return sandbox_tpm2_fill_buf(recv, recv_len, tag, rc);
Miquel Raynal2bae7122018-05-15 11:57:25 +0200568 }
569
570 /* Extend the PCR */
571 rc = sandbox_tpm2_extend(dev, pcr_index, sent);
572
Miquel Raynal46703cd2018-08-05 18:53:07 +0200573 sandbox_tpm2_fill_buf(recv, recv_len, tag, rc);
Miquel Raynal2bae7122018-05-15 11:57:25 +0200574 break;
575
576 default:
577 printf("TPM2 command %02x unknown in Sandbox\n", command);
578 rc = TPM2_RC_COMMAND_CODE;
Miquel Raynal46703cd2018-08-05 18:53:07 +0200579 sandbox_tpm2_fill_buf(recv, recv_len, tag, rc);
Miquel Raynal2bae7122018-05-15 11:57:25 +0200580 }
581
582 return 0;
583}
584
585static int sandbox_tpm2_get_desc(struct udevice *dev, char *buf, int size)
586{
587 if (size < 15)
588 return -ENOSPC;
589
590 return snprintf(buf, size, "Sandbox TPM2.x");
591}
592
593static int sandbox_tpm2_open(struct udevice *dev)
594{
595 struct sandbox_tpm2 *tpm = dev_get_priv(dev);
596
597 if (tpm->init_done)
598 return -EIO;
599
600 tpm->init_done = true;
601
602 return 0;
603}
604
605static int sandbox_tpm2_probe(struct udevice *dev)
606{
607 struct sandbox_tpm2 *tpm = dev_get_priv(dev);
608 struct tpm_chip_priv *priv = dev_get_uclass_priv(dev);
609
Miquel Raynal2a2096e2018-07-19 22:35:09 +0200610 /* Use the TPM v2 stack */
611 priv->version = TPM_V2;
612
Miquel Raynal2bae7122018-05-15 11:57:25 +0200613 priv->pcr_count = 32;
614 priv->pcr_select_min = 2;
615
Simon Glass0c0ddad2021-07-18 14:18:02 -0600616 if (s_state.valid)
617 memcpy(tpm, &s_state, sizeof(*tpm));
618 g_state = tpm;
619
Miquel Raynal2bae7122018-05-15 11:57:25 +0200620 return 0;
621}
622
623static int sandbox_tpm2_close(struct udevice *dev)
624{
625 return 0;
626}
627
628static const struct tpm_ops sandbox_tpm2_ops = {
629 .open = sandbox_tpm2_open,
630 .close = sandbox_tpm2_close,
631 .get_desc = sandbox_tpm2_get_desc,
632 .xfer = sandbox_tpm2_xfer,
633};
634
635static const struct udevice_id sandbox_tpm2_ids[] = {
636 { .compatible = "sandbox,tpm2" },
637 { }
638};
639
640U_BOOT_DRIVER(sandbox_tpm2) = {
641 .name = "sandbox_tpm2",
642 .id = UCLASS_TPM,
643 .of_match = sandbox_tpm2_ids,
644 .ops = &sandbox_tpm2_ops,
645 .probe = sandbox_tpm2_probe,
Simon Glass41575d82020-12-03 16:55:17 -0700646 .priv_auto = sizeof(struct sandbox_tpm2),
Miquel Raynal2bae7122018-05-15 11:57:25 +0200647};