]> git.ipfire.org Git - thirdparty/u-boot.git/blob - cmd/tpm.c
a8d6607f33bc23eedd7e6e81b11c6b25752f573d
[thirdparty/u-boot.git] / cmd / tpm.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Copyright (c) 2013 The Chromium OS Authors.
4 */
5
6 #include <common.h>
7 #include <command.h>
8 #include <dm.h>
9 #include <malloc.h>
10 #include <tpm.h>
11 #include <asm/unaligned.h>
12 #include <linux/string.h>
13
14 /* Useful constants */
15 enum {
16 DIGEST_LENGTH = 20,
17 /* max lengths, valid for RSA keys <= 2048 bits */
18 TPM_PUBKEY_MAX_LENGTH = 288,
19 };
20
21 /**
22 * Print a byte string in hexdecimal format, 16-bytes per line.
23 *
24 * @param data byte string to be printed
25 * @param count number of bytes to be printed
26 */
27 static void print_byte_string(u8 *data, size_t count)
28 {
29 int i, print_newline = 0;
30
31 for (i = 0; i < count; i++) {
32 printf(" %02x", data[i]);
33 print_newline = (i % 16 == 15);
34 if (print_newline)
35 putc('\n');
36 }
37 /* Avoid duplicated newline at the end */
38 if (!print_newline)
39 putc('\n');
40 }
41
42 /**
43 * Convert a text string of hexdecimal values into a byte string.
44 *
45 * @param bytes text string of hexdecimal values with no space
46 * between them
47 * @param data output buffer for byte string. The caller has to make
48 * sure it is large enough for storing the output. If
49 * NULL is passed, a large enough buffer will be allocated,
50 * and the caller must free it.
51 * @param count_ptr output variable for the length of byte string
52 * @return pointer to output buffer
53 */
54 static void *parse_byte_string(char *bytes, u8 *data, size_t *count_ptr)
55 {
56 char byte[3];
57 size_t count, length;
58 int i;
59
60 if (!bytes)
61 return NULL;
62 length = strlen(bytes);
63 count = length / 2;
64
65 if (!data)
66 data = malloc(count);
67 if (!data)
68 return NULL;
69
70 byte[2] = '\0';
71 for (i = 0; i < length; i += 2) {
72 byte[0] = bytes[i];
73 byte[1] = bytes[i + 1];
74 data[i / 2] = (u8)simple_strtoul(byte, NULL, 16);
75 }
76
77 if (count_ptr)
78 *count_ptr = count;
79
80 return data;
81 }
82
83 /**
84 * report_return_code() - Report any error and return failure or success
85 *
86 * @param return_code TPM command return code
87 * @return value of enum command_ret_t
88 */
89 static int report_return_code(int return_code)
90 {
91 if (return_code) {
92 printf("Error: %d\n", return_code);
93 return CMD_RET_FAILURE;
94 } else {
95 return CMD_RET_SUCCESS;
96 }
97 }
98
99 /**
100 * Return number of values defined by a type string.
101 *
102 * @param type_str type string
103 * @return number of values of type string
104 */
105 static int type_string_get_num_values(const char *type_str)
106 {
107 return strlen(type_str);
108 }
109
110 /**
111 * Return total size of values defined by a type string.
112 *
113 * @param type_str type string
114 * @return total size of values of type string, or 0 if type string
115 * contains illegal type character.
116 */
117 static size_t type_string_get_space_size(const char *type_str)
118 {
119 size_t size;
120
121 for (size = 0; *type_str; type_str++) {
122 switch (*type_str) {
123 case 'b':
124 size += 1;
125 break;
126 case 'w':
127 size += 2;
128 break;
129 case 'd':
130 size += 4;
131 break;
132 default:
133 return 0;
134 }
135 }
136
137 return size;
138 }
139
140 /**
141 * Allocate a buffer large enough to hold values defined by a type
142 * string. The caller has to free the buffer.
143 *
144 * @param type_str type string
145 * @param count pointer for storing size of buffer
146 * @return pointer to buffer or NULL on error
147 */
148 static void *type_string_alloc(const char *type_str, u32 *count)
149 {
150 void *data;
151 size_t size;
152
153 size = type_string_get_space_size(type_str);
154 if (!size)
155 return NULL;
156 data = malloc(size);
157 if (data)
158 *count = size;
159
160 return data;
161 }
162
163 /**
164 * Pack values defined by a type string into a buffer. The buffer must have
165 * large enough space.
166 *
167 * @param type_str type string
168 * @param values text strings of values to be packed
169 * @param data output buffer of values
170 * @return 0 on success, non-0 on error
171 */
172 static int type_string_pack(const char *type_str, char * const values[],
173 u8 *data)
174 {
175 size_t offset;
176 u32 value;
177
178 for (offset = 0; *type_str; type_str++, values++) {
179 value = simple_strtoul(values[0], NULL, 0);
180 switch (*type_str) {
181 case 'b':
182 data[offset] = value;
183 offset += 1;
184 break;
185 case 'w':
186 put_unaligned_be16(value, data + offset);
187 offset += 2;
188 break;
189 case 'd':
190 put_unaligned_be32(value, data + offset);
191 offset += 4;
192 break;
193 default:
194 return -1;
195 }
196 }
197
198 return 0;
199 }
200
201 /**
202 * Read values defined by a type string from a buffer, and write these values
203 * to environment variables.
204 *
205 * @param type_str type string
206 * @param data input buffer of values
207 * @param vars names of environment variables
208 * @return 0 on success, non-0 on error
209 */
210 static int type_string_write_vars(const char *type_str, u8 *data,
211 char * const vars[])
212 {
213 size_t offset;
214 u32 value;
215
216 for (offset = 0; *type_str; type_str++, vars++) {
217 switch (*type_str) {
218 case 'b':
219 value = data[offset];
220 offset += 1;
221 break;
222 case 'w':
223 value = get_unaligned_be16(data + offset);
224 offset += 2;
225 break;
226 case 'd':
227 value = get_unaligned_be32(data + offset);
228 offset += 4;
229 break;
230 default:
231 return -1;
232 }
233 if (env_set_ulong(*vars, value))
234 return -1;
235 }
236
237 return 0;
238 }
239
240 static int do_tpm_startup(cmd_tbl_t *cmdtp, int flag,
241 int argc, char * const argv[])
242 {
243 enum tpm_startup_type mode;
244
245 if (argc != 2)
246 return CMD_RET_USAGE;
247 if (!strcasecmp("TPM_ST_CLEAR", argv[1])) {
248 mode = TPM_ST_CLEAR;
249 } else if (!strcasecmp("TPM_ST_STATE", argv[1])) {
250 mode = TPM_ST_STATE;
251 } else if (!strcasecmp("TPM_ST_DEACTIVATED", argv[1])) {
252 mode = TPM_ST_DEACTIVATED;
253 } else {
254 printf("Couldn't recognize mode string: %s\n", argv[1]);
255 return CMD_RET_FAILURE;
256 }
257
258 return report_return_code(tpm_startup(mode));
259 }
260
261 static int do_tpm_nv_define_space(cmd_tbl_t *cmdtp, int flag,
262 int argc, char * const argv[])
263 {
264 u32 index, perm, size;
265
266 if (argc != 4)
267 return CMD_RET_USAGE;
268 index = simple_strtoul(argv[1], NULL, 0);
269 perm = simple_strtoul(argv[2], NULL, 0);
270 size = simple_strtoul(argv[3], NULL, 0);
271
272 return report_return_code(tpm_nv_define_space(index, perm, size));
273 }
274
275 static int do_tpm_nv_read_value(cmd_tbl_t *cmdtp, int flag,
276 int argc, char * const argv[])
277 {
278 u32 index, count, rc;
279 void *data;
280
281 if (argc != 4)
282 return CMD_RET_USAGE;
283 index = simple_strtoul(argv[1], NULL, 0);
284 data = (void *)simple_strtoul(argv[2], NULL, 0);
285 count = simple_strtoul(argv[3], NULL, 0);
286
287 rc = tpm_nv_read_value(index, data, count);
288 if (!rc) {
289 puts("area content:\n");
290 print_byte_string(data, count);
291 }
292
293 return report_return_code(rc);
294 }
295
296 static int do_tpm_nv_write_value(cmd_tbl_t *cmdtp, int flag,
297 int argc, char * const argv[])
298 {
299 u32 index, rc;
300 size_t count;
301 void *data;
302
303 if (argc != 3)
304 return CMD_RET_USAGE;
305 index = simple_strtoul(argv[1], NULL, 0);
306 data = parse_byte_string(argv[2], NULL, &count);
307 if (!data) {
308 printf("Couldn't parse byte string %s\n", argv[2]);
309 return CMD_RET_FAILURE;
310 }
311
312 rc = tpm_nv_write_value(index, data, count);
313 free(data);
314
315 return report_return_code(rc);
316 }
317
318 static int do_tpm_extend(cmd_tbl_t *cmdtp, int flag,
319 int argc, char * const argv[])
320 {
321 u32 index, rc;
322 u8 in_digest[20], out_digest[20];
323
324 if (argc != 3)
325 return CMD_RET_USAGE;
326 index = simple_strtoul(argv[1], NULL, 0);
327 if (!parse_byte_string(argv[2], in_digest, NULL)) {
328 printf("Couldn't parse byte string %s\n", argv[2]);
329 return CMD_RET_FAILURE;
330 }
331
332 rc = tpm_extend(index, in_digest, out_digest);
333 if (!rc) {
334 puts("PCR value after execution of the command:\n");
335 print_byte_string(out_digest, sizeof(out_digest));
336 }
337
338 return report_return_code(rc);
339 }
340
341 static int do_tpm_pcr_read(cmd_tbl_t *cmdtp, int flag,
342 int argc, char * const argv[])
343 {
344 u32 index, count, rc;
345 void *data;
346
347 if (argc != 4)
348 return CMD_RET_USAGE;
349 index = simple_strtoul(argv[1], NULL, 0);
350 data = (void *)simple_strtoul(argv[2], NULL, 0);
351 count = simple_strtoul(argv[3], NULL, 0);
352
353 rc = tpm_pcr_read(index, data, count);
354 if (!rc) {
355 puts("Named PCR content:\n");
356 print_byte_string(data, count);
357 }
358
359 return report_return_code(rc);
360 }
361
362 static int do_tpm_tsc_physical_presence(cmd_tbl_t *cmdtp, int flag,
363 int argc, char * const argv[])
364 {
365 u16 presence;
366
367 if (argc != 2)
368 return CMD_RET_USAGE;
369 presence = (u16)simple_strtoul(argv[1], NULL, 0);
370
371 return report_return_code(tpm_tsc_physical_presence(presence));
372 }
373
374 static int do_tpm_read_pubek(cmd_tbl_t *cmdtp, int flag,
375 int argc, char * const argv[])
376 {
377 u32 count, rc;
378 void *data;
379
380 if (argc != 3)
381 return CMD_RET_USAGE;
382 data = (void *)simple_strtoul(argv[1], NULL, 0);
383 count = simple_strtoul(argv[2], NULL, 0);
384
385 rc = tpm_read_pubek(data, count);
386 if (!rc) {
387 puts("pubek value:\n");
388 print_byte_string(data, count);
389 }
390
391 return report_return_code(rc);
392 }
393
394 static int do_tpm_physical_set_deactivated(cmd_tbl_t *cmdtp, int flag,
395 int argc, char * const argv[])
396 {
397 u8 state;
398
399 if (argc != 2)
400 return CMD_RET_USAGE;
401 state = (u8)simple_strtoul(argv[1], NULL, 0);
402
403 return report_return_code(tpm_physical_set_deactivated(state));
404 }
405
406 static int do_tpm_get_capability(cmd_tbl_t *cmdtp, int flag,
407 int argc, char * const argv[])
408 {
409 u32 cap_area, sub_cap, rc;
410 void *cap;
411 size_t count;
412
413 if (argc != 5)
414 return CMD_RET_USAGE;
415 cap_area = simple_strtoul(argv[1], NULL, 0);
416 sub_cap = simple_strtoul(argv[2], NULL, 0);
417 cap = (void *)simple_strtoul(argv[3], NULL, 0);
418 count = simple_strtoul(argv[4], NULL, 0);
419
420 rc = tpm_get_capability(cap_area, sub_cap, cap, count);
421 if (!rc) {
422 puts("capability information:\n");
423 print_byte_string(cap, count);
424 }
425
426 return report_return_code(rc);
427 }
428
429 #define TPM_COMMAND_NO_ARG(cmd) \
430 static int do_##cmd(cmd_tbl_t *cmdtp, int flag, \
431 int argc, char * const argv[]) \
432 { \
433 if (argc != 1) \
434 return CMD_RET_USAGE; \
435 return report_return_code(cmd()); \
436 }
437
438 TPM_COMMAND_NO_ARG(tpm_init)
439 TPM_COMMAND_NO_ARG(tpm_self_test_full)
440 TPM_COMMAND_NO_ARG(tpm_continue_self_test)
441 TPM_COMMAND_NO_ARG(tpm_force_clear)
442 TPM_COMMAND_NO_ARG(tpm_physical_enable)
443 TPM_COMMAND_NO_ARG(tpm_physical_disable)
444
445 static int get_tpm(struct udevice **devp)
446 {
447 int rc;
448
449 rc = uclass_first_device_err(UCLASS_TPM, devp);
450 if (rc) {
451 printf("Could not find TPM (ret=%d)\n", rc);
452 return CMD_RET_FAILURE;
453 }
454
455 return 0;
456 }
457
458 static int do_tpm_info(cmd_tbl_t *cmdtp, int flag, int argc,
459 char *const argv[])
460 {
461 struct udevice *dev;
462 char buf[80];
463 int rc;
464
465 rc = get_tpm(&dev);
466 if (rc)
467 return rc;
468 rc = tpm_get_desc(dev, buf, sizeof(buf));
469 if (rc < 0) {
470 printf("Couldn't get TPM info (%d)\n", rc);
471 return CMD_RET_FAILURE;
472 }
473 printf("%s\n", buf);
474
475 return 0;
476 }
477
478 static int do_tpm_raw_transfer(cmd_tbl_t *cmdtp, int flag,
479 int argc, char * const argv[])
480 {
481 struct udevice *dev;
482 void *command;
483 u8 response[1024];
484 size_t count, response_length = sizeof(response);
485 u32 rc;
486
487 command = parse_byte_string(argv[1], NULL, &count);
488 if (!command) {
489 printf("Couldn't parse byte string %s\n", argv[1]);
490 return CMD_RET_FAILURE;
491 }
492
493 rc = get_tpm(&dev);
494 if (rc)
495 return rc;
496
497 rc = tpm_xfer(dev, command, count, response, &response_length);
498 free(command);
499 if (!rc) {
500 puts("tpm response:\n");
501 print_byte_string(response, response_length);
502 }
503
504 return report_return_code(rc);
505 }
506
507 static int do_tpm_nv_define(cmd_tbl_t *cmdtp, int flag,
508 int argc, char * const argv[])
509 {
510 u32 index, perm, size;
511
512 if (argc != 4)
513 return CMD_RET_USAGE;
514 size = type_string_get_space_size(argv[1]);
515 if (!size) {
516 printf("Couldn't parse arguments\n");
517 return CMD_RET_USAGE;
518 }
519 index = simple_strtoul(argv[2], NULL, 0);
520 perm = simple_strtoul(argv[3], NULL, 0);
521
522 return report_return_code(tpm_nv_define_space(index, perm, size));
523 }
524
525 static int do_tpm_nv_read(cmd_tbl_t *cmdtp, int flag,
526 int argc, char * const argv[])
527 {
528 u32 index, count, err;
529 void *data;
530
531 if (argc < 3)
532 return CMD_RET_USAGE;
533 if (argc != 3 + type_string_get_num_values(argv[1]))
534 return CMD_RET_USAGE;
535 index = simple_strtoul(argv[2], NULL, 0);
536 data = type_string_alloc(argv[1], &count);
537 if (!data) {
538 printf("Couldn't parse arguments\n");
539 return CMD_RET_USAGE;
540 }
541
542 err = tpm_nv_read_value(index, data, count);
543 if (!err) {
544 if (type_string_write_vars(argv[1], data, argv + 3)) {
545 printf("Couldn't write to variables\n");
546 err = ~0;
547 }
548 }
549 free(data);
550
551 return report_return_code(err);
552 }
553
554 static int do_tpm_nv_write(cmd_tbl_t *cmdtp, int flag,
555 int argc, char * const argv[])
556 {
557 u32 index, count, err;
558 void *data;
559
560 if (argc < 3)
561 return CMD_RET_USAGE;
562 if (argc != 3 + type_string_get_num_values(argv[1]))
563 return CMD_RET_USAGE;
564 index = simple_strtoul(argv[2], NULL, 0);
565 data = type_string_alloc(argv[1], &count);
566 if (!data) {
567 printf("Couldn't parse arguments\n");
568 return CMD_RET_USAGE;
569 }
570 if (type_string_pack(argv[1], argv + 3, data)) {
571 printf("Couldn't parse arguments\n");
572 free(data);
573 return CMD_RET_USAGE;
574 }
575
576 err = tpm_nv_write_value(index, data, count);
577 free(data);
578
579 return report_return_code(err);
580 }
581
582 #ifdef CONFIG_TPM_AUTH_SESSIONS
583
584 static int do_tpm_oiap(cmd_tbl_t *cmdtp, int flag,
585 int argc, char * const argv[])
586 {
587 u32 auth_handle, err;
588
589 err = tpm_oiap(&auth_handle);
590
591 return report_return_code(err);
592 }
593
594 #ifdef CONFIG_TPM_LOAD_KEY_BY_SHA1
595 static int do_tpm_load_key_by_sha1(cmd_tbl_t *cmdtp, int flag, int argc, char *
596 const argv[])
597 {
598 u32 parent_handle = 0;
599 u32 key_len, key_handle, err;
600 u8 usage_auth[DIGEST_LENGTH];
601 u8 parent_hash[DIGEST_LENGTH];
602 void *key;
603
604 if (argc < 5)
605 return CMD_RET_USAGE;
606
607 parse_byte_string(argv[1], parent_hash, NULL);
608 key = (void *)simple_strtoul(argv[2], NULL, 0);
609 key_len = simple_strtoul(argv[3], NULL, 0);
610 if (strlen(argv[4]) != 2 * DIGEST_LENGTH)
611 return CMD_RET_FAILURE;
612 parse_byte_string(argv[4], usage_auth, NULL);
613
614 err = tpm_find_key_sha1(usage_auth, parent_hash, &parent_handle);
615 if (err) {
616 printf("Could not find matching parent key (err = %d)\n", err);
617 return CMD_RET_FAILURE;
618 }
619
620 printf("Found parent key %08x\n", parent_handle);
621
622 err = tpm_load_key2_oiap(parent_handle, key, key_len, usage_auth,
623 &key_handle);
624 if (!err) {
625 printf("Key handle is 0x%x\n", key_handle);
626 env_set_hex("key_handle", key_handle);
627 }
628
629 return report_return_code(err);
630 }
631 #endif /* CONFIG_TPM_LOAD_KEY_BY_SHA1 */
632
633 static int do_tpm_load_key2_oiap(cmd_tbl_t *cmdtp, int flag,
634 int argc, char * const argv[])
635 {
636 u32 parent_handle, key_len, key_handle, err;
637 u8 usage_auth[DIGEST_LENGTH];
638 void *key;
639
640 if (argc < 5)
641 return CMD_RET_USAGE;
642
643 parent_handle = simple_strtoul(argv[1], NULL, 0);
644 key = (void *)simple_strtoul(argv[2], NULL, 0);
645 key_len = simple_strtoul(argv[3], NULL, 0);
646 if (strlen(argv[4]) != 2 * DIGEST_LENGTH)
647 return CMD_RET_FAILURE;
648 parse_byte_string(argv[4], usage_auth, NULL);
649
650 err = tpm_load_key2_oiap(parent_handle, key, key_len, usage_auth,
651 &key_handle);
652 if (!err)
653 printf("Key handle is 0x%x\n", key_handle);
654
655 return report_return_code(err);
656 }
657
658 static int do_tpm_get_pub_key_oiap(cmd_tbl_t *cmdtp, int flag,
659 int argc, char * const argv[])
660 {
661 u32 key_handle, err;
662 u8 usage_auth[DIGEST_LENGTH];
663 u8 pub_key_buffer[TPM_PUBKEY_MAX_LENGTH];
664 size_t pub_key_len = sizeof(pub_key_buffer);
665
666 if (argc < 3)
667 return CMD_RET_USAGE;
668
669 key_handle = simple_strtoul(argv[1], NULL, 0);
670 if (strlen(argv[2]) != 2 * DIGEST_LENGTH)
671 return CMD_RET_FAILURE;
672 parse_byte_string(argv[2], usage_auth, NULL);
673
674 err = tpm_get_pub_key_oiap(key_handle, usage_auth,
675 pub_key_buffer, &pub_key_len);
676 if (!err) {
677 printf("dump of received pub key structure:\n");
678 print_byte_string(pub_key_buffer, pub_key_len);
679 }
680 return report_return_code(err);
681 }
682
683 TPM_COMMAND_NO_ARG(tpm_end_oiap)
684
685 #endif /* CONFIG_TPM_AUTH_SESSIONS */
686
687 #ifdef CONFIG_TPM_FLUSH_RESOURCES
688 static int do_tpm_flush(cmd_tbl_t *cmdtp, int flag, int argc,
689 char * const argv[])
690 {
691 int type = 0;
692
693 if (argc != 3)
694 return CMD_RET_USAGE;
695
696 if (!strcasecmp(argv[1], "key"))
697 type = TPM_RT_KEY;
698 else if (!strcasecmp(argv[1], "auth"))
699 type = TPM_RT_AUTH;
700 else if (!strcasecmp(argv[1], "hash"))
701 type = TPM_RT_HASH;
702 else if (!strcasecmp(argv[1], "trans"))
703 type = TPM_RT_TRANS;
704 else if (!strcasecmp(argv[1], "context"))
705 type = TPM_RT_CONTEXT;
706 else if (!strcasecmp(argv[1], "counter"))
707 type = TPM_RT_COUNTER;
708 else if (!strcasecmp(argv[1], "delegate"))
709 type = TPM_RT_DELEGATE;
710 else if (!strcasecmp(argv[1], "daa_tpm"))
711 type = TPM_RT_DAA_TPM;
712 else if (!strcasecmp(argv[1], "daa_v0"))
713 type = TPM_RT_DAA_V0;
714 else if (!strcasecmp(argv[1], "daa_v1"))
715 type = TPM_RT_DAA_V1;
716
717 if (!type) {
718 printf("Resource type %s unknown.\n", argv[1]);
719 return -1;
720 }
721
722 if (!strcasecmp(argv[2], "all")) {
723 u16 res_count;
724 u8 buf[288];
725 u8 *ptr;
726 int err;
727 uint i;
728
729 /* fetch list of already loaded resources in the TPM */
730 err = tpm_get_capability(TPM_CAP_HANDLE, type, buf,
731 sizeof(buf));
732 if (err) {
733 printf("tpm_get_capability returned error %d.\n", err);
734 return -1;
735 }
736 res_count = get_unaligned_be16(buf);
737 ptr = buf + 2;
738 for (i = 0; i < res_count; ++i, ptr += 4)
739 tpm_flush_specific(get_unaligned_be32(ptr), type);
740 } else {
741 u32 handle = simple_strtoul(argv[2], NULL, 0);
742
743 if (!handle) {
744 printf("Illegal resource handle %s\n", argv[2]);
745 return -1;
746 }
747 tpm_flush_specific(cpu_to_be32(handle), type);
748 }
749
750 return 0;
751 }
752 #endif /* CONFIG_TPM_FLUSH_RESOURCES */
753
754 #ifdef CONFIG_TPM_LIST_RESOURCES
755 static int do_tpm_list(cmd_tbl_t *cmdtp, int flag, int argc,
756 char * const argv[])
757 {
758 int type = 0;
759 u16 res_count;
760 u8 buf[288];
761 u8 *ptr;
762 int err;
763 uint i;
764
765 if (argc != 2)
766 return CMD_RET_USAGE;
767
768 if (!strcasecmp(argv[1], "key"))
769 type = TPM_RT_KEY;
770 else if (!strcasecmp(argv[1], "auth"))
771 type = TPM_RT_AUTH;
772 else if (!strcasecmp(argv[1], "hash"))
773 type = TPM_RT_HASH;
774 else if (!strcasecmp(argv[1], "trans"))
775 type = TPM_RT_TRANS;
776 else if (!strcasecmp(argv[1], "context"))
777 type = TPM_RT_CONTEXT;
778 else if (!strcasecmp(argv[1], "counter"))
779 type = TPM_RT_COUNTER;
780 else if (!strcasecmp(argv[1], "delegate"))
781 type = TPM_RT_DELEGATE;
782 else if (!strcasecmp(argv[1], "daa_tpm"))
783 type = TPM_RT_DAA_TPM;
784 else if (!strcasecmp(argv[1], "daa_v0"))
785 type = TPM_RT_DAA_V0;
786 else if (!strcasecmp(argv[1], "daa_v1"))
787 type = TPM_RT_DAA_V1;
788
789 if (!type) {
790 printf("Resource type %s unknown.\n", argv[1]);
791 return -1;
792 }
793
794 /* fetch list of already loaded resources in the TPM */
795 err = tpm_get_capability(TPM_CAP_HANDLE, type, buf,
796 sizeof(buf));
797 if (err) {
798 printf("tpm_get_capability returned error %d.\n", err);
799 return -1;
800 }
801 res_count = get_unaligned_be16(buf);
802 ptr = buf + 2;
803
804 printf("Resources of type %s (%02x):\n", argv[1], type);
805 if (!res_count) {
806 puts("None\n");
807 } else {
808 for (i = 0; i < res_count; ++i, ptr += 4)
809 printf("Index %d: %08x\n", i, get_unaligned_be32(ptr));
810 }
811
812 return 0;
813 }
814 #endif /* CONFIG_TPM_LIST_RESOURCES */
815
816 #define MAKE_TPM_CMD_ENTRY(cmd) \
817 U_BOOT_CMD_MKENT(cmd, 0, 1, do_tpm_ ## cmd, "", "")
818
819 static cmd_tbl_t tpm_commands[] = {
820 U_BOOT_CMD_MKENT(info, 0, 1, do_tpm_info, "", ""),
821 U_BOOT_CMD_MKENT(init, 0, 1,
822 do_tpm_init, "", ""),
823 U_BOOT_CMD_MKENT(startup, 0, 1,
824 do_tpm_startup, "", ""),
825 U_BOOT_CMD_MKENT(self_test_full, 0, 1,
826 do_tpm_self_test_full, "", ""),
827 U_BOOT_CMD_MKENT(continue_self_test, 0, 1,
828 do_tpm_continue_self_test, "", ""),
829 U_BOOT_CMD_MKENT(force_clear, 0, 1,
830 do_tpm_force_clear, "", ""),
831 U_BOOT_CMD_MKENT(physical_enable, 0, 1,
832 do_tpm_physical_enable, "", ""),
833 U_BOOT_CMD_MKENT(physical_disable, 0, 1,
834 do_tpm_physical_disable, "", ""),
835 U_BOOT_CMD_MKENT(nv_define_space, 0, 1,
836 do_tpm_nv_define_space, "", ""),
837 U_BOOT_CMD_MKENT(nv_read_value, 0, 1,
838 do_tpm_nv_read_value, "", ""),
839 U_BOOT_CMD_MKENT(nv_write_value, 0, 1,
840 do_tpm_nv_write_value, "", ""),
841 U_BOOT_CMD_MKENT(extend, 0, 1,
842 do_tpm_extend, "", ""),
843 U_BOOT_CMD_MKENT(pcr_read, 0, 1,
844 do_tpm_pcr_read, "", ""),
845 U_BOOT_CMD_MKENT(tsc_physical_presence, 0, 1,
846 do_tpm_tsc_physical_presence, "", ""),
847 U_BOOT_CMD_MKENT(read_pubek, 0, 1,
848 do_tpm_read_pubek, "", ""),
849 U_BOOT_CMD_MKENT(physical_set_deactivated, 0, 1,
850 do_tpm_physical_set_deactivated, "", ""),
851 U_BOOT_CMD_MKENT(get_capability, 0, 1,
852 do_tpm_get_capability, "", ""),
853 U_BOOT_CMD_MKENT(raw_transfer, 0, 1,
854 do_tpm_raw_transfer, "", ""),
855 U_BOOT_CMD_MKENT(nv_define, 0, 1,
856 do_tpm_nv_define, "", ""),
857 U_BOOT_CMD_MKENT(nv_read, 0, 1,
858 do_tpm_nv_read, "", ""),
859 U_BOOT_CMD_MKENT(nv_write, 0, 1,
860 do_tpm_nv_write, "", ""),
861 #ifdef CONFIG_TPM_AUTH_SESSIONS
862 U_BOOT_CMD_MKENT(oiap, 0, 1,
863 do_tpm_oiap, "", ""),
864 U_BOOT_CMD_MKENT(end_oiap, 0, 1,
865 do_tpm_end_oiap, "", ""),
866 U_BOOT_CMD_MKENT(load_key2_oiap, 0, 1,
867 do_tpm_load_key2_oiap, "", ""),
868 #ifdef CONFIG_TPM_LOAD_KEY_BY_SHA1
869 U_BOOT_CMD_MKENT(load_key_by_sha1, 0, 1,
870 do_tpm_load_key_by_sha1, "", ""),
871 #endif /* CONFIG_TPM_LOAD_KEY_BY_SHA1 */
872 U_BOOT_CMD_MKENT(get_pub_key_oiap, 0, 1,
873 do_tpm_get_pub_key_oiap, "", ""),
874 #endif /* CONFIG_TPM_AUTH_SESSIONS */
875 #ifdef CONFIG_TPM_FLUSH_RESOURCES
876 U_BOOT_CMD_MKENT(flush, 0, 1,
877 do_tpm_flush, "", ""),
878 #endif /* CONFIG_TPM_FLUSH_RESOURCES */
879 #ifdef CONFIG_TPM_LIST_RESOURCES
880 U_BOOT_CMD_MKENT(list, 0, 1,
881 do_tpm_list, "", ""),
882 #endif /* CONFIG_TPM_LIST_RESOURCES */
883 };
884
885 static int do_tpm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
886 {
887 cmd_tbl_t *tpm_cmd;
888
889 if (argc < 2)
890 return CMD_RET_USAGE;
891 tpm_cmd = find_cmd_tbl(argv[1], tpm_commands, ARRAY_SIZE(tpm_commands));
892 if (!tpm_cmd)
893 return CMD_RET_USAGE;
894
895 return tpm_cmd->cmd(cmdtp, flag, argc - 1, argv + 1);
896 }
897
898 U_BOOT_CMD(tpm, CONFIG_SYS_MAXARGS, 1, do_tpm,
899 "Issue a TPM command",
900 "cmd args...\n"
901 " - Issue TPM command <cmd> with arguments <args...>.\n"
902 "Admin Startup and State Commands:\n"
903 " info - Show information about the TPM\n"
904 " init\n"
905 " - Put TPM into a state where it waits for 'startup' command.\n"
906 " startup mode\n"
907 " - Issue TPM_Starup command. <mode> is one of TPM_ST_CLEAR,\n"
908 " TPM_ST_STATE, and TPM_ST_DEACTIVATED.\n"
909 "Admin Testing Commands:\n"
910 " self_test_full\n"
911 " - Test all of the TPM capabilities.\n"
912 " continue_self_test\n"
913 " - Inform TPM that it should complete the self-test.\n"
914 "Admin Opt-in Commands:\n"
915 " physical_enable\n"
916 " - Set the PERMANENT disable flag to FALSE using physical presence as\n"
917 " authorization.\n"
918 " physical_disable\n"
919 " - Set the PERMANENT disable flag to TRUE using physical presence as\n"
920 " authorization.\n"
921 " physical_set_deactivated 0|1\n"
922 " - Set deactivated flag.\n"
923 "Admin Ownership Commands:\n"
924 " force_clear\n"
925 " - Issue TPM_ForceClear command.\n"
926 " tsc_physical_presence flags\n"
927 " - Set TPM device's Physical Presence flags to <flags>.\n"
928 "The Capability Commands:\n"
929 " get_capability cap_area sub_cap addr count\n"
930 " - Read <count> bytes of TPM capability indexed by <cap_area> and\n"
931 " <sub_cap> to memory address <addr>.\n"
932 #if defined(CONFIG_TPM_FLUSH_RESOURCES) || defined(CONFIG_TPM_LIST_RESOURCES)
933 "Resource management functions\n"
934 #endif
935 #ifdef CONFIG_TPM_FLUSH_RESOURCES
936 " flush resource_type id\n"
937 " - flushes a resource of type <resource_type> (may be one of key, auth,\n"
938 " hash, trans, context, counter, delegate, daa_tpm, daa_v0, daa_v1),\n"
939 " and id <id> from the TPM. Use an <id> of \"all\" to flush all\n"
940 " resources of that type.\n"
941 #endif /* CONFIG_TPM_FLUSH_RESOURCES */
942 #ifdef CONFIG_TPM_LIST_RESOURCES
943 " list resource_type\n"
944 " - lists resources of type <resource_type> (may be one of key, auth,\n"
945 " hash, trans, context, counter, delegate, daa_tpm, daa_v0, daa_v1),\n"
946 " contained in the TPM.\n"
947 #endif /* CONFIG_TPM_LIST_RESOURCES */
948 #ifdef CONFIG_TPM_AUTH_SESSIONS
949 "Storage functions\n"
950 " loadkey2_oiap parent_handle key_addr key_len usage_auth\n"
951 " - loads a key data from memory address <key_addr>, <key_len> bytes\n"
952 " into TPM using the parent key <parent_handle> with authorization\n"
953 " <usage_auth> (20 bytes hex string).\n"
954 #ifdef CONFIG_TPM_LOAD_KEY_BY_SHA1
955 " load_key_by_sha1 parent_hash key_addr key_len usage_auth\n"
956 " - loads a key data from memory address <key_addr>, <key_len> bytes\n"
957 " into TPM using the parent hash <parent_hash> (20 bytes hex string)\n"
958 " with authorization <usage_auth> (20 bytes hex string).\n"
959 #endif /* CONFIG_TPM_LOAD_KEY_BY_SHA1 */
960 " get_pub_key_oiap key_handle usage_auth\n"
961 " - get the public key portion of a loaded key <key_handle> using\n"
962 " authorization <usage auth> (20 bytes hex string)\n"
963 #endif /* CONFIG_TPM_AUTH_SESSIONS */
964 "Endorsement Key Handling Commands:\n"
965 " read_pubek addr count\n"
966 " - Read <count> bytes of the public endorsement key to memory\n"
967 " address <addr>\n"
968 "Integrity Collection and Reporting Commands:\n"
969 " extend index digest_hex_string\n"
970 " - Add a new measurement to a PCR. Update PCR <index> with the 20-bytes\n"
971 " <digest_hex_string>\n"
972 " pcr_read index addr count\n"
973 " - Read <count> bytes from PCR <index> to memory address <addr>.\n"
974 #ifdef CONFIG_TPM_AUTH_SESSIONS
975 "Authorization Sessions\n"
976 " oiap\n"
977 " - setup an OIAP session\n"
978 " end_oiap\n"
979 " - terminates an active OIAP session\n"
980 #endif /* CONFIG_TPM_AUTH_SESSIONS */
981 "Non-volatile Storage Commands:\n"
982 " nv_define_space index permission size\n"
983 " - Establish a space at index <index> with <permission> of <size> bytes.\n"
984 " nv_read_value index addr count\n"
985 " - Read <count> bytes from space <index> to memory address <addr>.\n"
986 " nv_write_value index addr count\n"
987 " - Write <count> bytes from memory address <addr> to space <index>.\n"
988 "Miscellaneous helper functions:\n"
989 " raw_transfer byte_string\n"
990 " - Send a byte string <byte_string> to TPM and print the response.\n"
991 " Non-volatile storage helper functions:\n"
992 " These helper functions treat a non-volatile space as a non-padded\n"
993 " sequence of integer values. These integer values are defined by a type\n"
994 " string, which is a text string of 'bwd' characters: 'b' means a 8-bit\n"
995 " value, 'w' 16-bit value, 'd' 32-bit value. All helper functions take\n"
996 " a type string as their first argument.\n"
997 " nv_define type_string index perm\n"
998 " - Define a space <index> with permission <perm>.\n"
999 " nv_read types_string index vars...\n"
1000 " - Read from space <index> to environment variables <vars...>.\n"
1001 " nv_write types_string index values...\n"
1002 " - Write to space <index> from values <values...>.\n"
1003 );