]> git.ipfire.org Git - thirdparty/u-boot.git/blame - cmd/tpm-v1.c
sandbox: move sandbox specifics to booti_setup()
[thirdparty/u-boot.git] / cmd / tpm-v1.c
CommitLineData
83d290c5 1// SPDX-License-Identifier: GPL-2.0+
576fb1ed 2/*
8732b070 3 * Copyright (c) 2013 The Chromium OS Authors.
576fb1ed
VB
4 */
5
6#include <common.h>
09140113 7#include <command.h>
c7694dd4 8#include <env.h>
8732b070 9#include <malloc.h>
8732b070 10#include <asm/unaligned.h>
d677bfe2
MR
11#include <tpm-common.h>
12#include <tpm-v1.h>
13#include "tpm-user-utils.h"
d6a885f0 14#include <tpm_api.h>
576fb1ed 15
09140113
SG
16static int do_tpm_startup(struct cmd_tbl *cmdtp, int flag, int argc,
17 char *const argv[])
8732b070
CC
18{
19 enum tpm_startup_type mode;
abdc7b8a
SG
20 struct udevice *dev;
21 int rc;
eea3f4d3 22
abdc7b8a
SG
23 rc = get_tpm(&dev);
24 if (rc)
25 return rc;
8732b070
CC
26 if (argc != 2)
27 return CMD_RET_USAGE;
28 if (!strcasecmp("TPM_ST_CLEAR", argv[1])) {
29 mode = TPM_ST_CLEAR;
30 } else if (!strcasecmp("TPM_ST_STATE", argv[1])) {
31 mode = TPM_ST_STATE;
32 } else if (!strcasecmp("TPM_ST_DEACTIVATED", argv[1])) {
33 mode = TPM_ST_DEACTIVATED;
34 } else {
35 printf("Couldn't recognize mode string: %s\n", argv[1]);
36 return CMD_RET_FAILURE;
37 }
38
abdc7b8a 39 return report_return_code(tpm_startup(dev, mode));
8732b070
CC
40}
41
09140113
SG
42static int do_tpm_nv_define_space(struct cmd_tbl *cmdtp, int flag, int argc,
43 char *const argv[])
8732b070 44{
b9804e5b 45 u32 index, perm, size;
abdc7b8a
SG
46 struct udevice *dev;
47 int rc;
48
49 rc = get_tpm(&dev);
50 if (rc)
51 return rc;
8732b070
CC
52
53 if (argc != 4)
54 return CMD_RET_USAGE;
55 index = simple_strtoul(argv[1], NULL, 0);
56 perm = simple_strtoul(argv[2], NULL, 0);
57 size = simple_strtoul(argv[3], NULL, 0);
58
d6a885f0 59 return report_return_code(tpm1_nv_define_space(dev, index, perm, size));
8732b070 60}
eea3f4d3 61
09140113
SG
62static int do_tpm_nv_read_value(struct cmd_tbl *cmdtp, int flag, int argc,
63 char *const argv[])
eea3f4d3 64{
b9804e5b 65 u32 index, count, rc;
abdc7b8a 66 struct udevice *dev;
8732b070 67 void *data;
eea3f4d3 68
abdc7b8a
SG
69 rc = get_tpm(&dev);
70 if (rc)
71 return rc;
72
8732b070
CC
73 if (argc != 4)
74 return CMD_RET_USAGE;
75 index = simple_strtoul(argv[1], NULL, 0);
76 data = (void *)simple_strtoul(argv[2], NULL, 0);
77 count = simple_strtoul(argv[3], NULL, 0);
78
abdc7b8a 79 rc = tpm_nv_read_value(dev, index, data, count);
8732b070
CC
80 if (!rc) {
81 puts("area content:\n");
82 print_byte_string(data, count);
576fb1ed
VB
83 }
84
f8f1fe1d 85 return report_return_code(rc);
8732b070
CC
86}
87
09140113
SG
88static int do_tpm_nv_write_value(struct cmd_tbl *cmdtp, int flag, int argc,
89 char *const argv[])
8732b070 90{
abdc7b8a 91 struct udevice *dev;
b9804e5b 92 u32 index, rc;
8732b070
CC
93 size_t count;
94 void *data;
95
abdc7b8a
SG
96 rc = get_tpm(&dev);
97 if (rc)
98 return rc;
99
8732b070
CC
100 if (argc != 3)
101 return CMD_RET_USAGE;
102 index = simple_strtoul(argv[1], NULL, 0);
103 data = parse_byte_string(argv[2], NULL, &count);
104 if (!data) {
105 printf("Couldn't parse byte string %s\n", argv[2]);
106 return CMD_RET_FAILURE;
eea3f4d3
LS
107 }
108
abdc7b8a 109 rc = tpm_nv_write_value(dev, index, data, count);
8732b070
CC
110 free(data);
111
f8f1fe1d 112 return report_return_code(rc);
8732b070
CC
113}
114
09140113
SG
115static int do_tpm_extend(struct cmd_tbl *cmdtp, int flag, int argc,
116 char *const argv[])
8732b070 117{
b9804e5b 118 u8 in_digest[20], out_digest[20];
abdc7b8a
SG
119 struct udevice *dev;
120 u32 index, rc;
121
122 rc = get_tpm(&dev);
123 if (rc)
124 return rc;
8732b070
CC
125
126 if (argc != 3)
127 return CMD_RET_USAGE;
128 index = simple_strtoul(argv[1], NULL, 0);
129 if (!parse_byte_string(argv[2], in_digest, NULL)) {
130 printf("Couldn't parse byte string %s\n", argv[2]);
131 return CMD_RET_FAILURE;
576fb1ed
VB
132 }
133
a557d258
SG
134 rc = tpm_pcr_extend(dev, index, in_digest, sizeof(in_digest),
135 out_digest, "cmd");
8732b070
CC
136 if (!rc) {
137 puts("PCR value after execution of the command:\n");
138 print_byte_string(out_digest, sizeof(out_digest));
576fb1ed
VB
139 }
140
f8f1fe1d 141 return report_return_code(rc);
8732b070
CC
142}
143
09140113
SG
144static int do_tpm_pcr_read(struct cmd_tbl *cmdtp, int flag, int argc,
145 char *const argv[])
8732b070 146{
b9804e5b 147 u32 index, count, rc;
abdc7b8a 148 struct udevice *dev;
8732b070
CC
149 void *data;
150
abdc7b8a
SG
151 rc = get_tpm(&dev);
152 if (rc)
153 return rc;
154
8732b070
CC
155 if (argc != 4)
156 return CMD_RET_USAGE;
157 index = simple_strtoul(argv[1], NULL, 0);
158 data = (void *)simple_strtoul(argv[2], NULL, 0);
159 count = simple_strtoul(argv[3], NULL, 0);
576fb1ed 160
abdc7b8a 161 rc = tpm_pcr_read(dev, index, data, count);
8732b070
CC
162 if (!rc) {
163 puts("Named PCR content:\n");
164 print_byte_string(data, count);
576fb1ed
VB
165 }
166
f8f1fe1d 167 return report_return_code(rc);
576fb1ed
VB
168}
169
09140113
SG
170static int do_tpm_tsc_physical_presence(struct cmd_tbl *cmdtp, int flag,
171 int argc, char *const argv[])
8732b070 172{
abdc7b8a 173 struct udevice *dev;
b9804e5b 174 u16 presence;
abdc7b8a
SG
175 int rc;
176
177 rc = get_tpm(&dev);
178 if (rc)
179 return rc;
8732b070
CC
180
181 if (argc != 2)
182 return CMD_RET_USAGE;
b9804e5b 183 presence = (u16)simple_strtoul(argv[1], NULL, 0);
eea3f4d3 184
abdc7b8a 185 return report_return_code(tpm_tsc_physical_presence(dev, presence));
8732b070
CC
186}
187
09140113
SG
188static int do_tpm_read_pubek(struct cmd_tbl *cmdtp, int flag, int argc,
189 char *const argv[])
eea3f4d3 190{
abdc7b8a 191 struct udevice *dev;
b9804e5b 192 u32 count, rc;
8732b070
CC
193 void *data;
194
abdc7b8a
SG
195 rc = get_tpm(&dev);
196 if (rc)
197 return rc;
198
8732b070
CC
199 if (argc != 3)
200 return CMD_RET_USAGE;
201 data = (void *)simple_strtoul(argv[1], NULL, 0);
202 count = simple_strtoul(argv[2], NULL, 0);
203
abdc7b8a 204 rc = tpm_read_pubek(dev, data, count);
8732b070
CC
205 if (!rc) {
206 puts("pubek value:\n");
207 print_byte_string(data, count);
208 }
209
f8f1fe1d 210 return report_return_code(rc);
eea3f4d3
LS
211}
212
09140113
SG
213static int do_tpm_physical_set_deactivated(struct cmd_tbl *cmdtp, int flag,
214 int argc, char *const argv[])
8732b070 215{
abdc7b8a 216 struct udevice *dev;
b9804e5b 217 u8 state;
abdc7b8a
SG
218 int rc;
219
220 rc = get_tpm(&dev);
221 if (rc)
222 return rc;
8732b070
CC
223
224 if (argc != 2)
225 return CMD_RET_USAGE;
b9804e5b 226 state = (u8)simple_strtoul(argv[1], NULL, 0);
eea3f4d3 227
abdc7b8a 228 return report_return_code(tpm_physical_set_deactivated(dev, state));
8732b070
CC
229}
230
09140113
SG
231static int do_tpm_get_capability(struct cmd_tbl *cmdtp, int flag, int argc,
232 char *const argv[])
8732b070 233{
b9804e5b 234 u32 cap_area, sub_cap, rc;
8732b070
CC
235 void *cap;
236 size_t count;
abdc7b8a
SG
237 struct udevice *dev;
238
239 rc = get_tpm(&dev);
240 if (rc)
241 return rc;
8732b070
CC
242
243 if (argc != 5)
244 return CMD_RET_USAGE;
245 cap_area = simple_strtoul(argv[1], NULL, 0);
246 sub_cap = simple_strtoul(argv[2], NULL, 0);
247 cap = (void *)simple_strtoul(argv[3], NULL, 0);
248 count = simple_strtoul(argv[4], NULL, 0);
249
abdc7b8a 250 rc = tpm_get_capability(dev, cap_area, sub_cap, cap, count);
8732b070
CC
251 if (!rc) {
252 puts("capability information:\n");
253 print_byte_string(cap, count);
254 }
255
f8f1fe1d 256 return report_return_code(rc);
8732b070 257}
eea3f4d3 258
09140113
SG
259static int do_tpm_raw_transfer(struct cmd_tbl *cmdtp, int flag, int argc,
260 char *const argv[])
8732b070 261{
c2b0f600 262 struct udevice *dev;
8732b070 263 void *command;
b9804e5b 264 u8 response[1024];
8732b070 265 size_t count, response_length = sizeof(response);
b9804e5b 266 u32 rc;
8732b070
CC
267
268 command = parse_byte_string(argv[1], NULL, &count);
269 if (!command) {
270 printf("Couldn't parse byte string %s\n", argv[1]);
271 return CMD_RET_FAILURE;
272 }
273
c8a8c510
SG
274 rc = get_tpm(&dev);
275 if (rc)
276 return rc;
277
278 rc = tpm_xfer(dev, command, count, response, &response_length);
8732b070
CC
279 free(command);
280 if (!rc) {
281 puts("tpm response:\n");
282 print_byte_string(response, response_length);
283 }
284
f8f1fe1d 285 return report_return_code(rc);
8732b070
CC
286}
287
09140113
SG
288static int do_tpm_nv_define(struct cmd_tbl *cmdtp, int flag, int argc,
289 char *const argv[])
8732b070 290{
b9804e5b 291 u32 index, perm, size;
abdc7b8a
SG
292 struct udevice *dev;
293 int rc;
294
295 rc = get_tpm(&dev);
296 if (rc)
297 return rc;
8732b070
CC
298
299 if (argc != 4)
300 return CMD_RET_USAGE;
301 size = type_string_get_space_size(argv[1]);
302 if (!size) {
303 printf("Couldn't parse arguments\n");
304 return CMD_RET_USAGE;
305 }
306 index = simple_strtoul(argv[2], NULL, 0);
307 perm = simple_strtoul(argv[3], NULL, 0);
308
d6a885f0 309 return report_return_code(tpm1_nv_define_space(dev, index, perm, size));
8732b070
CC
310}
311
09140113
SG
312static int do_tpm_nv_read(struct cmd_tbl *cmdtp, int flag, int argc,
313 char *const argv[])
eea3f4d3 314{
b9804e5b 315 u32 index, count, err;
abdc7b8a 316 struct udevice *dev;
8732b070 317 void *data;
abdc7b8a
SG
318 int rc;
319
320 rc = get_tpm(&dev);
321 if (rc)
322 return rc;
eea3f4d3 323
8732b070
CC
324 if (argc < 3)
325 return CMD_RET_USAGE;
326 if (argc != 3 + type_string_get_num_values(argv[1]))
327 return CMD_RET_USAGE;
328 index = simple_strtoul(argv[2], NULL, 0);
329 data = type_string_alloc(argv[1], &count);
330 if (!data) {
331 printf("Couldn't parse arguments\n");
332 return CMD_RET_USAGE;
eea3f4d3
LS
333 }
334
abdc7b8a 335 err = tpm_nv_read_value(dev, index, data, count);
8732b070
CC
336 if (!err) {
337 if (type_string_write_vars(argv[1], data, argv + 3)) {
338 printf("Couldn't write to variables\n");
339 err = ~0;
340 }
eea3f4d3 341 }
8732b070
CC
342 free(data);
343
f8f1fe1d 344 return report_return_code(err);
8732b070
CC
345}
346
09140113
SG
347static int do_tpm_nv_write(struct cmd_tbl *cmdtp, int flag, int argc,
348 char *const argv[])
8732b070 349{
b9804e5b 350 u32 index, count, err;
abdc7b8a 351 struct udevice *dev;
8732b070 352 void *data;
abdc7b8a
SG
353 int rc;
354
355 rc = get_tpm(&dev);
356 if (rc)
357 return rc;
8732b070
CC
358
359 if (argc < 3)
360 return CMD_RET_USAGE;
361 if (argc != 3 + type_string_get_num_values(argv[1]))
362 return CMD_RET_USAGE;
363 index = simple_strtoul(argv[2], NULL, 0);
364 data = type_string_alloc(argv[1], &count);
365 if (!data) {
366 printf("Couldn't parse arguments\n");
367 return CMD_RET_USAGE;
368 }
369 if (type_string_pack(argv[1], argv + 3, data)) {
370 printf("Couldn't parse arguments\n");
371 free(data);
372 return CMD_RET_USAGE;
373 }
374
abdc7b8a 375 err = tpm_nv_write_value(dev, index, data, count);
8732b070
CC
376 free(data);
377
f8f1fe1d 378 return report_return_code(err);
8732b070
CC
379}
380
be6c1529
RP
381#ifdef CONFIG_TPM_AUTH_SESSIONS
382
09140113
SG
383static int do_tpm_oiap(struct cmd_tbl *cmdtp, int flag, int argc,
384 char *const argv[])
be6c1529 385{
b9804e5b 386 u32 auth_handle, err;
abdc7b8a
SG
387 struct udevice *dev;
388 int rc;
389
390 rc = get_tpm(&dev);
391 if (rc)
392 return rc;
be6c1529 393
d6a885f0 394 err = tpm1_oiap(dev, &auth_handle);
be6c1529 395
f8f1fe1d 396 return report_return_code(err);
be6c1529
RP
397}
398
0f4b2ba1 399#ifdef CONFIG_TPM_LOAD_KEY_BY_SHA1
09140113
SG
400static int do_tpm_load_key_by_sha1(struct cmd_tbl *cmdtp, int flag, int argc,
401 char *const argv[])
0f4b2ba1 402{
b9804e5b
MR
403 u32 parent_handle = 0;
404 u32 key_len, key_handle, err;
405 u8 usage_auth[DIGEST_LENGTH];
406 u8 parent_hash[DIGEST_LENGTH];
0f4b2ba1 407 void *key;
abdc7b8a
SG
408 struct udevice *dev;
409
e845dd7c
MM
410 err = get_tpm(&dev);
411 if (err)
412 return err;
0f4b2ba1 413
414 if (argc < 5)
415 return CMD_RET_USAGE;
416
417 parse_byte_string(argv[1], parent_hash, NULL);
418 key = (void *)simple_strtoul(argv[2], NULL, 0);
419 key_len = simple_strtoul(argv[3], NULL, 0);
420 if (strlen(argv[4]) != 2 * DIGEST_LENGTH)
421 return CMD_RET_FAILURE;
422 parse_byte_string(argv[4], usage_auth, NULL);
423
e845dd7c 424 err = tpm1_find_key_sha1(dev, usage_auth, parent_hash, &parent_handle);
0f4b2ba1 425 if (err) {
426 printf("Could not find matching parent key (err = %d)\n", err);
427 return CMD_RET_FAILURE;
428 }
429
430 printf("Found parent key %08x\n", parent_handle);
431
e845dd7c 432 err = tpm1_load_key2_oiap(dev, parent_handle, key, key_len, usage_auth,
0f4b2ba1 433 &key_handle);
434 if (!err) {
435 printf("Key handle is 0x%x\n", key_handle);
018f5303 436 env_set_hex("key_handle", key_handle);
0f4b2ba1 437 }
438
439 return report_return_code(err);
440}
441#endif /* CONFIG_TPM_LOAD_KEY_BY_SHA1 */
442
09140113
SG
443static int do_tpm_load_key2_oiap(struct cmd_tbl *cmdtp, int flag, int argc,
444 char *const argv[])
be6c1529 445{
b9804e5b
MR
446 u32 parent_handle, key_len, key_handle, err;
447 u8 usage_auth[DIGEST_LENGTH];
be6c1529 448 void *key;
abdc7b8a
SG
449 struct udevice *dev;
450 int rc;
451
452 rc = get_tpm(&dev);
453 if (rc)
454 return rc;
be6c1529
RP
455
456 if (argc < 5)
457 return CMD_RET_USAGE;
458
459 parent_handle = simple_strtoul(argv[1], NULL, 0);
460 key = (void *)simple_strtoul(argv[2], NULL, 0);
461 key_len = simple_strtoul(argv[3], NULL, 0);
462 if (strlen(argv[4]) != 2 * DIGEST_LENGTH)
463 return CMD_RET_FAILURE;
464 parse_byte_string(argv[4], usage_auth, NULL);
465
d6a885f0
SG
466 err = tpm1_load_key2_oiap(dev, parent_handle, key, key_len, usage_auth,
467 &key_handle);
be6c1529
RP
468 if (!err)
469 printf("Key handle is 0x%x\n", key_handle);
470
f8f1fe1d 471 return report_return_code(err);
be6c1529
RP
472}
473
09140113
SG
474static int do_tpm_get_pub_key_oiap(struct cmd_tbl *cmdtp, int flag, int argc,
475 char *const argv[])
be6c1529 476{
b9804e5b
MR
477 u32 key_handle, err;
478 u8 usage_auth[DIGEST_LENGTH];
479 u8 pub_key_buffer[TPM_PUBKEY_MAX_LENGTH];
be6c1529 480 size_t pub_key_len = sizeof(pub_key_buffer);
abdc7b8a
SG
481 struct udevice *dev;
482 int rc;
483
484 rc = get_tpm(&dev);
485 if (rc)
486 return rc;
be6c1529
RP
487
488 if (argc < 3)
489 return CMD_RET_USAGE;
490
491 key_handle = simple_strtoul(argv[1], NULL, 0);
492 if (strlen(argv[2]) != 2 * DIGEST_LENGTH)
493 return CMD_RET_FAILURE;
494 parse_byte_string(argv[2], usage_auth, NULL);
495
d6a885f0
SG
496 err = tpm1_get_pub_key_oiap(dev, key_handle, usage_auth, pub_key_buffer,
497 &pub_key_len);
be6c1529
RP
498 if (!err) {
499 printf("dump of received pub key structure:\n");
500 print_byte_string(pub_key_buffer, pub_key_len);
501 }
f8f1fe1d 502 return report_return_code(err);
be6c1529
RP
503}
504
d6a885f0 505TPM_COMMAND_NO_ARG(tpm1_end_oiap)
be6c1529
RP
506
507#endif /* CONFIG_TPM_AUTH_SESSIONS */
508
7690be35 509#ifdef CONFIG_TPM_FLUSH_RESOURCES
09140113
SG
510static int do_tpm_flush(struct cmd_tbl *cmdtp, int flag, int argc,
511 char *const argv[])
7690be35 512{
abdc7b8a 513 struct udevice *dev;
7690be35 514 int type = 0;
abdc7b8a
SG
515 int rc;
516
517 rc = get_tpm(&dev);
518 if (rc)
519 return rc;
7690be35 520
1c08b210 521 if (argc != 3)
7690be35
MS
522 return CMD_RET_USAGE;
523
1c08b210 524 if (!strcasecmp(argv[1], "key"))
7690be35 525 type = TPM_RT_KEY;
1c08b210 526 else if (!strcasecmp(argv[1], "auth"))
7690be35 527 type = TPM_RT_AUTH;
1c08b210 528 else if (!strcasecmp(argv[1], "hash"))
7690be35 529 type = TPM_RT_HASH;
1c08b210 530 else if (!strcasecmp(argv[1], "trans"))
7690be35 531 type = TPM_RT_TRANS;
1c08b210 532 else if (!strcasecmp(argv[1], "context"))
7690be35 533 type = TPM_RT_CONTEXT;
1c08b210 534 else if (!strcasecmp(argv[1], "counter"))
7690be35 535 type = TPM_RT_COUNTER;
1c08b210 536 else if (!strcasecmp(argv[1], "delegate"))
7690be35 537 type = TPM_RT_DELEGATE;
1c08b210 538 else if (!strcasecmp(argv[1], "daa_tpm"))
7690be35 539 type = TPM_RT_DAA_TPM;
1c08b210 540 else if (!strcasecmp(argv[1], "daa_v0"))
7690be35 541 type = TPM_RT_DAA_V0;
1c08b210 542 else if (!strcasecmp(argv[1], "daa_v1"))
7690be35
MS
543 type = TPM_RT_DAA_V1;
544
1c08b210 545 if (!type) {
546 printf("Resource type %s unknown.\n", argv[1]);
547 return -1;
548 }
549
550 if (!strcasecmp(argv[2], "all")) {
b9804e5b
MR
551 u16 res_count;
552 u8 buf[288];
553 u8 *ptr;
7690be35
MS
554 int err;
555 uint i;
556
557 /* fetch list of already loaded resources in the TPM */
abdc7b8a 558 err = tpm_get_capability(dev, TPM_CAP_HANDLE, type, buf,
7690be35 559 sizeof(buf));
1c08b210 560 if (err) {
561 printf("tpm_get_capability returned error %d.\n", err);
7690be35 562 return -1;
1c08b210 563 }
7690be35
MS
564 res_count = get_unaligned_be16(buf);
565 ptr = buf + 2;
566 for (i = 0; i < res_count; ++i, ptr += 4)
d6a885f0 567 tpm1_flush_specific(dev, get_unaligned_be32(ptr), type);
7690be35 568 } else {
b9804e5b 569 u32 handle = simple_strtoul(argv[2], NULL, 0);
7690be35 570
1c08b210 571 if (!handle) {
572 printf("Illegal resource handle %s\n", argv[2]);
7690be35 573 return -1;
1c08b210 574 }
d6a885f0 575 tpm1_flush_specific(dev, cpu_to_be32(handle), type);
7690be35
MS
576 }
577
578 return 0;
579}
580#endif /* CONFIG_TPM_FLUSH_RESOURCES */
581
3d1df0e3 582#ifdef CONFIG_TPM_LIST_RESOURCES
09140113
SG
583static int do_tpm_list(struct cmd_tbl *cmdtp, int flag, int argc,
584 char *const argv[])
3d1df0e3 585{
ebb6d74d 586 struct udevice *dev;
3d1df0e3 587 int type = 0;
b9804e5b
MR
588 u16 res_count;
589 u8 buf[288];
590 u8 *ptr;
3d1df0e3 591 int err;
592 uint i;
593
ebb6d74d
MM
594 err = get_tpm(&dev);
595 if (err)
596 return err;
597
3d1df0e3 598 if (argc != 2)
599 return CMD_RET_USAGE;
600
601 if (!strcasecmp(argv[1], "key"))
602 type = TPM_RT_KEY;
603 else if (!strcasecmp(argv[1], "auth"))
604 type = TPM_RT_AUTH;
605 else if (!strcasecmp(argv[1], "hash"))
606 type = TPM_RT_HASH;
607 else if (!strcasecmp(argv[1], "trans"))
608 type = TPM_RT_TRANS;
609 else if (!strcasecmp(argv[1], "context"))
610 type = TPM_RT_CONTEXT;
611 else if (!strcasecmp(argv[1], "counter"))
612 type = TPM_RT_COUNTER;
613 else if (!strcasecmp(argv[1], "delegate"))
614 type = TPM_RT_DELEGATE;
615 else if (!strcasecmp(argv[1], "daa_tpm"))
616 type = TPM_RT_DAA_TPM;
617 else if (!strcasecmp(argv[1], "daa_v0"))
618 type = TPM_RT_DAA_V0;
619 else if (!strcasecmp(argv[1], "daa_v1"))
620 type = TPM_RT_DAA_V1;
621
622 if (!type) {
623 printf("Resource type %s unknown.\n", argv[1]);
624 return -1;
625 }
626
627 /* fetch list of already loaded resources in the TPM */
ebb6d74d 628 err = tpm_get_capability(dev, TPM_CAP_HANDLE, type, buf,
3d1df0e3 629 sizeof(buf));
630 if (err) {
631 printf("tpm_get_capability returned error %d.\n", err);
632 return -1;
633 }
634 res_count = get_unaligned_be16(buf);
635 ptr = buf + 2;
636
637 printf("Resources of type %s (%02x):\n", argv[1], type);
638 if (!res_count) {
639 puts("None\n");
640 } else {
641 for (i = 0; i < res_count; ++i, ptr += 4)
642 printf("Index %d: %08x\n", i, get_unaligned_be32(ptr));
643 }
644
645 return 0;
646}
647#endif /* CONFIG_TPM_LIST_RESOURCES */
648
d677bfe2
MR
649TPM_COMMAND_NO_ARG(tpm_self_test_full)
650TPM_COMMAND_NO_ARG(tpm_continue_self_test)
651TPM_COMMAND_NO_ARG(tpm_force_clear)
652TPM_COMMAND_NO_ARG(tpm_physical_enable)
653TPM_COMMAND_NO_ARG(tpm_physical_disable)
8732b070 654
09140113 655static struct cmd_tbl tpm1_commands[] = {
3780e2d0 656 U_BOOT_CMD_MKENT(device, 0, 1, do_tpm_device, "", ""),
ad77694e 657 U_BOOT_CMD_MKENT(info, 0, 1, do_tpm_info, "", ""),
edce82c8 658 U_BOOT_CMD_MKENT(autostart, 0, 1, do_tpm_autostart, "", ""),
c6179187 659 U_BOOT_CMD_MKENT(init, 0, 1, do_tpm_init, "", ""),
8732b070 660 U_BOOT_CMD_MKENT(startup, 0, 1,
c6179187 661 do_tpm_startup, "", ""),
8732b070 662 U_BOOT_CMD_MKENT(self_test_full, 0, 1,
c6179187 663 do_tpm_self_test_full, "", ""),
8732b070 664 U_BOOT_CMD_MKENT(continue_self_test, 0, 1,
c6179187 665 do_tpm_continue_self_test, "", ""),
8732b070 666 U_BOOT_CMD_MKENT(force_clear, 0, 1,
c6179187 667 do_tpm_force_clear, "", ""),
8732b070 668 U_BOOT_CMD_MKENT(physical_enable, 0, 1,
c6179187 669 do_tpm_physical_enable, "", ""),
8732b070 670 U_BOOT_CMD_MKENT(physical_disable, 0, 1,
c6179187 671 do_tpm_physical_disable, "", ""),
8732b070 672 U_BOOT_CMD_MKENT(nv_define_space, 0, 1,
c6179187 673 do_tpm_nv_define_space, "", ""),
8732b070 674 U_BOOT_CMD_MKENT(nv_read_value, 0, 1,
c6179187 675 do_tpm_nv_read_value, "", ""),
8732b070 676 U_BOOT_CMD_MKENT(nv_write_value, 0, 1,
c6179187 677 do_tpm_nv_write_value, "", ""),
8732b070 678 U_BOOT_CMD_MKENT(extend, 0, 1,
c6179187 679 do_tpm_extend, "", ""),
8732b070 680 U_BOOT_CMD_MKENT(pcr_read, 0, 1,
c6179187 681 do_tpm_pcr_read, "", ""),
8732b070 682 U_BOOT_CMD_MKENT(tsc_physical_presence, 0, 1,
c6179187 683 do_tpm_tsc_physical_presence, "", ""),
8732b070 684 U_BOOT_CMD_MKENT(read_pubek, 0, 1,
c6179187 685 do_tpm_read_pubek, "", ""),
8732b070 686 U_BOOT_CMD_MKENT(physical_set_deactivated, 0, 1,
c6179187 687 do_tpm_physical_set_deactivated, "", ""),
8732b070 688 U_BOOT_CMD_MKENT(get_capability, 0, 1,
c6179187 689 do_tpm_get_capability, "", ""),
8732b070 690 U_BOOT_CMD_MKENT(raw_transfer, 0, 1,
c6179187 691 do_tpm_raw_transfer, "", ""),
8732b070 692 U_BOOT_CMD_MKENT(nv_define, 0, 1,
c6179187 693 do_tpm_nv_define, "", ""),
8732b070 694 U_BOOT_CMD_MKENT(nv_read, 0, 1,
c6179187 695 do_tpm_nv_read, "", ""),
8732b070 696 U_BOOT_CMD_MKENT(nv_write, 0, 1,
c6179187 697 do_tpm_nv_write, "", ""),
be6c1529
RP
698#ifdef CONFIG_TPM_AUTH_SESSIONS
699 U_BOOT_CMD_MKENT(oiap, 0, 1,
700 do_tpm_oiap, "", ""),
701 U_BOOT_CMD_MKENT(end_oiap, 0, 1,
d6a885f0 702 do_tpm1_end_oiap, "", ""),
be6c1529
RP
703 U_BOOT_CMD_MKENT(load_key2_oiap, 0, 1,
704 do_tpm_load_key2_oiap, "", ""),
0f4b2ba1 705#ifdef CONFIG_TPM_LOAD_KEY_BY_SHA1
706 U_BOOT_CMD_MKENT(load_key_by_sha1, 0, 1,
707 do_tpm_load_key_by_sha1, "", ""),
708#endif /* CONFIG_TPM_LOAD_KEY_BY_SHA1 */
be6c1529
RP
709 U_BOOT_CMD_MKENT(get_pub_key_oiap, 0, 1,
710 do_tpm_get_pub_key_oiap, "", ""),
711#endif /* CONFIG_TPM_AUTH_SESSIONS */
7690be35
MS
712#ifdef CONFIG_TPM_FLUSH_RESOURCES
713 U_BOOT_CMD_MKENT(flush, 0, 1,
714 do_tpm_flush, "", ""),
715#endif /* CONFIG_TPM_FLUSH_RESOURCES */
3d1df0e3 716#ifdef CONFIG_TPM_LIST_RESOURCES
717 U_BOOT_CMD_MKENT(list, 0, 1,
718 do_tpm_list, "", ""),
719#endif /* CONFIG_TPM_LIST_RESOURCES */
8732b070
CC
720};
721
09140113 722struct cmd_tbl *get_tpm1_commands(unsigned int *size)
8732b070 723{
d677bfe2 724 *size = ARRAY_SIZE(tpm1_commands);
eea3f4d3 725
d677bfe2 726 return tpm1_commands;
eea3f4d3
LS
727}
728
8732b070 729U_BOOT_CMD(tpm, CONFIG_SYS_MAXARGS, 1, do_tpm,
d677bfe2 730"Issue a TPMv1.x command",
8732b070
CC
731"cmd args...\n"
732" - Issue TPM command <cmd> with arguments <args...>.\n"
733"Admin Startup and State Commands:\n"
3780e2d0
PR
734" device [num device]\n"
735" - Show all devices or set the specified device\n"
ad77694e 736" info - Show information about the TPM\n"
e663b2ff
IA
737" autostart\n"
738" - Initalize the tpm, perform a Startup(clear) and run a full selftest\n"
739" sequence\n"
8732b070
CC
740" init\n"
741" - Put TPM into a state where it waits for 'startup' command.\n"
e663b2ff 742" startup mode\n"
8732b070
CC
743" - Issue TPM_Starup command. <mode> is one of TPM_ST_CLEAR,\n"
744" TPM_ST_STATE, and TPM_ST_DEACTIVATED.\n"
745"Admin Testing Commands:\n"
746" self_test_full\n"
747" - Test all of the TPM capabilities.\n"
748" continue_self_test\n"
749" - Inform TPM that it should complete the self-test.\n"
750"Admin Opt-in Commands:\n"
751" physical_enable\n"
752" - Set the PERMANENT disable flag to FALSE using physical presence as\n"
753" authorization.\n"
754" physical_disable\n"
755" - Set the PERMANENT disable flag to TRUE using physical presence as\n"
756" authorization.\n"
757" physical_set_deactivated 0|1\n"
758" - Set deactivated flag.\n"
759"Admin Ownership Commands:\n"
760" force_clear\n"
761" - Issue TPM_ForceClear command.\n"
762" tsc_physical_presence flags\n"
763" - Set TPM device's Physical Presence flags to <flags>.\n"
764"The Capability Commands:\n"
765" get_capability cap_area sub_cap addr count\n"
766" - Read <count> bytes of TPM capability indexed by <cap_area> and\n"
767" <sub_cap> to memory address <addr>.\n"
3d1df0e3 768#if defined(CONFIG_TPM_FLUSH_RESOURCES) || defined(CONFIG_TPM_LIST_RESOURCES)
7690be35 769"Resource management functions\n"
3d1df0e3 770#endif
771#ifdef CONFIG_TPM_FLUSH_RESOURCES
7690be35
MS
772" flush resource_type id\n"
773" - flushes a resource of type <resource_type> (may be one of key, auth,\n"
774" hash, trans, context, counter, delegate, daa_tpm, daa_v0, daa_v1),\n"
775" and id <id> from the TPM. Use an <id> of \"all\" to flush all\n"
776" resources of that type.\n"
777#endif /* CONFIG_TPM_FLUSH_RESOURCES */
3d1df0e3 778#ifdef CONFIG_TPM_LIST_RESOURCES
779" list resource_type\n"
780" - lists resources of type <resource_type> (may be one of key, auth,\n"
781" hash, trans, context, counter, delegate, daa_tpm, daa_v0, daa_v1),\n"
782" contained in the TPM.\n"
783#endif /* CONFIG_TPM_LIST_RESOURCES */
be6c1529
RP
784#ifdef CONFIG_TPM_AUTH_SESSIONS
785"Storage functions\n"
786" loadkey2_oiap parent_handle key_addr key_len usage_auth\n"
787" - loads a key data from memory address <key_addr>, <key_len> bytes\n"
788" into TPM using the parent key <parent_handle> with authorization\n"
789" <usage_auth> (20 bytes hex string).\n"
0f4b2ba1 790#ifdef CONFIG_TPM_LOAD_KEY_BY_SHA1
791" load_key_by_sha1 parent_hash key_addr key_len usage_auth\n"
792" - loads a key data from memory address <key_addr>, <key_len> bytes\n"
793" into TPM using the parent hash <parent_hash> (20 bytes hex string)\n"
794" with authorization <usage_auth> (20 bytes hex string).\n"
795#endif /* CONFIG_TPM_LOAD_KEY_BY_SHA1 */
be6c1529
RP
796" get_pub_key_oiap key_handle usage_auth\n"
797" - get the public key portion of a loaded key <key_handle> using\n"
798" authorization <usage auth> (20 bytes hex string)\n"
799#endif /* CONFIG_TPM_AUTH_SESSIONS */
8732b070
CC
800"Endorsement Key Handling Commands:\n"
801" read_pubek addr count\n"
802" - Read <count> bytes of the public endorsement key to memory\n"
803" address <addr>\n"
804"Integrity Collection and Reporting Commands:\n"
805" extend index digest_hex_string\n"
806" - Add a new measurement to a PCR. Update PCR <index> with the 20-bytes\n"
807" <digest_hex_string>\n"
808" pcr_read index addr count\n"
809" - Read <count> bytes from PCR <index> to memory address <addr>.\n"
be6c1529
RP
810#ifdef CONFIG_TPM_AUTH_SESSIONS
811"Authorization Sessions\n"
812" oiap\n"
813" - setup an OIAP session\n"
814" end_oiap\n"
815" - terminates an active OIAP session\n"
816#endif /* CONFIG_TPM_AUTH_SESSIONS */
8732b070
CC
817"Non-volatile Storage Commands:\n"
818" nv_define_space index permission size\n"
819" - Establish a space at index <index> with <permission> of <size> bytes.\n"
820" nv_read_value index addr count\n"
821" - Read <count> bytes from space <index> to memory address <addr>.\n"
822" nv_write_value index addr count\n"
823" - Write <count> bytes from memory address <addr> to space <index>.\n"
824"Miscellaneous helper functions:\n"
825" raw_transfer byte_string\n"
826" - Send a byte string <byte_string> to TPM and print the response.\n"
827" Non-volatile storage helper functions:\n"
828" These helper functions treat a non-volatile space as a non-padded\n"
829" sequence of integer values. These integer values are defined by a type\n"
830" string, which is a text string of 'bwd' characters: 'b' means a 8-bit\n"
831" value, 'w' 16-bit value, 'd' 32-bit value. All helper functions take\n"
832" a type string as their first argument.\n"
833" nv_define type_string index perm\n"
834" - Define a space <index> with permission <perm>.\n"
835" nv_read types_string index vars...\n"
836" - Read from space <index> to environment variables <vars...>.\n"
837" nv_write types_string index values...\n"
838" - Write to space <index> from values <values...>.\n"
eea3f4d3 839);