1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2013 The Chromium OS Authors.
9 #include <asm/unaligned.h>
10 #include <tpm-common.h>
12 #include "tpm-user-utils.h"
14 static int do_tpm_startup(cmd_tbl_t *cmdtp, int flag, int argc,
17 enum tpm_startup_type mode;
26 if (!strcasecmp("TPM_ST_CLEAR", argv[1])) {
28 } else if (!strcasecmp("TPM_ST_STATE", argv[1])) {
30 } else if (!strcasecmp("TPM_ST_DEACTIVATED", argv[1])) {
31 mode = TPM_ST_DEACTIVATED;
33 printf("Couldn't recognize mode string: %s\n", argv[1]);
34 return CMD_RET_FAILURE;
37 return report_return_code(tpm_startup(dev, mode));
40 static int do_tpm_nv_define_space(cmd_tbl_t *cmdtp, int flag, int argc,
43 u32 index, perm, size;
53 index = simple_strtoul(argv[1], NULL, 0);
54 perm = simple_strtoul(argv[2], NULL, 0);
55 size = simple_strtoul(argv[3], NULL, 0);
57 return report_return_code(tpm_nv_define_space(dev, index, perm, size));
60 static int do_tpm_nv_read_value(cmd_tbl_t *cmdtp, int flag, int argc,
73 index = simple_strtoul(argv[1], NULL, 0);
74 data = (void *)simple_strtoul(argv[2], NULL, 0);
75 count = simple_strtoul(argv[3], NULL, 0);
77 rc = tpm_nv_read_value(dev, index, data, count);
79 puts("area content:\n");
80 print_byte_string(data, count);
83 return report_return_code(rc);
86 static int do_tpm_nv_write_value(cmd_tbl_t *cmdtp, int flag, int argc,
100 index = simple_strtoul(argv[1], NULL, 0);
101 data = parse_byte_string(argv[2], NULL, &count);
103 printf("Couldn't parse byte string %s\n", argv[2]);
104 return CMD_RET_FAILURE;
107 rc = tpm_nv_write_value(dev, index, data, count);
110 return report_return_code(rc);
113 static int do_tpm_extend(cmd_tbl_t *cmdtp, int flag, int argc,
116 u8 in_digest[20], out_digest[20];
125 return CMD_RET_USAGE;
126 index = simple_strtoul(argv[1], NULL, 0);
127 if (!parse_byte_string(argv[2], in_digest, NULL)) {
128 printf("Couldn't parse byte string %s\n", argv[2]);
129 return CMD_RET_FAILURE;
132 rc = tpm_extend(dev, index, in_digest, out_digest);
134 puts("PCR value after execution of the command:\n");
135 print_byte_string(out_digest, sizeof(out_digest));
138 return report_return_code(rc);
141 static int do_tpm_pcr_read(cmd_tbl_t *cmdtp, int flag, int argc,
144 u32 index, count, rc;
153 return CMD_RET_USAGE;
154 index = simple_strtoul(argv[1], NULL, 0);
155 data = (void *)simple_strtoul(argv[2], NULL, 0);
156 count = simple_strtoul(argv[3], NULL, 0);
158 rc = tpm_pcr_read(dev, index, data, count);
160 puts("Named PCR content:\n");
161 print_byte_string(data, count);
164 return report_return_code(rc);
167 static int do_tpm_tsc_physical_presence(cmd_tbl_t *cmdtp, int flag, int argc,
179 return CMD_RET_USAGE;
180 presence = (u16)simple_strtoul(argv[1], NULL, 0);
182 return report_return_code(tpm_tsc_physical_presence(dev, presence));
185 static int do_tpm_read_pubek(cmd_tbl_t *cmdtp, int flag, int argc,
197 return CMD_RET_USAGE;
198 data = (void *)simple_strtoul(argv[1], NULL, 0);
199 count = simple_strtoul(argv[2], NULL, 0);
201 rc = tpm_read_pubek(dev, data, count);
203 puts("pubek value:\n");
204 print_byte_string(data, count);
207 return report_return_code(rc);
210 static int do_tpm_physical_set_deactivated(cmd_tbl_t *cmdtp, int flag, int argc,
222 return CMD_RET_USAGE;
223 state = (u8)simple_strtoul(argv[1], NULL, 0);
225 return report_return_code(tpm_physical_set_deactivated(dev, state));
228 static int do_tpm_get_capability(cmd_tbl_t *cmdtp, int flag, int argc,
231 u32 cap_area, sub_cap, rc;
241 return CMD_RET_USAGE;
242 cap_area = simple_strtoul(argv[1], NULL, 0);
243 sub_cap = simple_strtoul(argv[2], NULL, 0);
244 cap = (void *)simple_strtoul(argv[3], NULL, 0);
245 count = simple_strtoul(argv[4], NULL, 0);
247 rc = tpm_get_capability(dev, cap_area, sub_cap, cap, count);
249 puts("capability information:\n");
250 print_byte_string(cap, count);
253 return report_return_code(rc);
256 static int do_tpm_raw_transfer(cmd_tbl_t *cmdtp, int flag, int argc,
262 size_t count, response_length = sizeof(response);
265 command = parse_byte_string(argv[1], NULL, &count);
267 printf("Couldn't parse byte string %s\n", argv[1]);
268 return CMD_RET_FAILURE;
275 rc = tpm_xfer(dev, command, count, response, &response_length);
278 puts("tpm response:\n");
279 print_byte_string(response, response_length);
282 return report_return_code(rc);
285 static int do_tpm_nv_define(cmd_tbl_t *cmdtp, int flag, int argc,
288 u32 index, perm, size;
297 return CMD_RET_USAGE;
298 size = type_string_get_space_size(argv[1]);
300 printf("Couldn't parse arguments\n");
301 return CMD_RET_USAGE;
303 index = simple_strtoul(argv[2], NULL, 0);
304 perm = simple_strtoul(argv[3], NULL, 0);
306 return report_return_code(tpm_nv_define_space(dev, index, perm, size));
309 static int do_tpm_nv_read(cmd_tbl_t *cmdtp, int flag, int argc,
312 u32 index, count, err;
322 return CMD_RET_USAGE;
323 if (argc != 3 + type_string_get_num_values(argv[1]))
324 return CMD_RET_USAGE;
325 index = simple_strtoul(argv[2], NULL, 0);
326 data = type_string_alloc(argv[1], &count);
328 printf("Couldn't parse arguments\n");
329 return CMD_RET_USAGE;
332 err = tpm_nv_read_value(dev, index, data, count);
334 if (type_string_write_vars(argv[1], data, argv + 3)) {
335 printf("Couldn't write to variables\n");
341 return report_return_code(err);
344 static int do_tpm_nv_write(cmd_tbl_t *cmdtp, int flag, int argc,
347 u32 index, count, err;
357 return CMD_RET_USAGE;
358 if (argc != 3 + type_string_get_num_values(argv[1]))
359 return CMD_RET_USAGE;
360 index = simple_strtoul(argv[2], NULL, 0);
361 data = type_string_alloc(argv[1], &count);
363 printf("Couldn't parse arguments\n");
364 return CMD_RET_USAGE;
366 if (type_string_pack(argv[1], argv + 3, data)) {
367 printf("Couldn't parse arguments\n");
369 return CMD_RET_USAGE;
372 err = tpm_nv_write_value(dev, index, data, count);
375 return report_return_code(err);
378 #ifdef CONFIG_TPM_AUTH_SESSIONS
380 static int do_tpm_oiap(cmd_tbl_t *cmdtp, int flag, int argc,
383 u32 auth_handle, err;
391 err = tpm_oiap(dev, &auth_handle);
393 return report_return_code(err);
396 #ifdef CONFIG_TPM_LOAD_KEY_BY_SHA1
397 static int do_tpm_load_key_by_sha1(cmd_tbl_t *cmdtp, int flag, int argc, char *
400 u32 parent_handle = 0;
401 u32 key_len, key_handle, err;
402 u8 usage_auth[DIGEST_LENGTH];
403 u8 parent_hash[DIGEST_LENGTH];
412 return CMD_RET_USAGE;
414 parse_byte_string(argv[1], parent_hash, NULL);
415 key = (void *)simple_strtoul(argv[2], NULL, 0);
416 key_len = simple_strtoul(argv[3], NULL, 0);
417 if (strlen(argv[4]) != 2 * DIGEST_LENGTH)
418 return CMD_RET_FAILURE;
419 parse_byte_string(argv[4], usage_auth, NULL);
421 err = tpm_find_key_sha1(usage_auth, parent_hash, &parent_handle);
423 printf("Could not find matching parent key (err = %d)\n", err);
424 return CMD_RET_FAILURE;
427 printf("Found parent key %08x\n", parent_handle);
429 err = tpm_load_key2_oiap(parent_handle, key, key_len, usage_auth,
432 printf("Key handle is 0x%x\n", key_handle);
433 env_set_hex("key_handle", key_handle);
436 return report_return_code(err);
438 #endif /* CONFIG_TPM_LOAD_KEY_BY_SHA1 */
440 static int do_tpm_load_key2_oiap(cmd_tbl_t *cmdtp, int flag, int argc,
443 u32 parent_handle, key_len, key_handle, err;
444 u8 usage_auth[DIGEST_LENGTH];
454 return CMD_RET_USAGE;
456 parent_handle = simple_strtoul(argv[1], NULL, 0);
457 key = (void *)simple_strtoul(argv[2], NULL, 0);
458 key_len = simple_strtoul(argv[3], NULL, 0);
459 if (strlen(argv[4]) != 2 * DIGEST_LENGTH)
460 return CMD_RET_FAILURE;
461 parse_byte_string(argv[4], usage_auth, NULL);
463 err = tpm_load_key2_oiap(dev, parent_handle, key, key_len, usage_auth,
466 printf("Key handle is 0x%x\n", key_handle);
468 return report_return_code(err);
471 static int do_tpm_get_pub_key_oiap(cmd_tbl_t *cmdtp, int flag, int argc,
475 u8 usage_auth[DIGEST_LENGTH];
476 u8 pub_key_buffer[TPM_PUBKEY_MAX_LENGTH];
477 size_t pub_key_len = sizeof(pub_key_buffer);
486 return CMD_RET_USAGE;
488 key_handle = simple_strtoul(argv[1], NULL, 0);
489 if (strlen(argv[2]) != 2 * DIGEST_LENGTH)
490 return CMD_RET_FAILURE;
491 parse_byte_string(argv[2], usage_auth, NULL);
493 err = tpm_get_pub_key_oiap(dev, key_handle, usage_auth, pub_key_buffer,
496 printf("dump of received pub key structure:\n");
497 print_byte_string(pub_key_buffer, pub_key_len);
499 return report_return_code(err);
502 TPM_COMMAND_NO_ARG(tpm_end_oiap)
504 #endif /* CONFIG_TPM_AUTH_SESSIONS */
506 #ifdef CONFIG_TPM_FLUSH_RESOURCES
507 static int do_tpm_flush(cmd_tbl_t *cmdtp, int flag, int argc,
519 return CMD_RET_USAGE;
521 if (!strcasecmp(argv[1], "key"))
523 else if (!strcasecmp(argv[1], "auth"))
525 else if (!strcasecmp(argv[1], "hash"))
527 else if (!strcasecmp(argv[1], "trans"))
529 else if (!strcasecmp(argv[1], "context"))
530 type = TPM_RT_CONTEXT;
531 else if (!strcasecmp(argv[1], "counter"))
532 type = TPM_RT_COUNTER;
533 else if (!strcasecmp(argv[1], "delegate"))
534 type = TPM_RT_DELEGATE;
535 else if (!strcasecmp(argv[1], "daa_tpm"))
536 type = TPM_RT_DAA_TPM;
537 else if (!strcasecmp(argv[1], "daa_v0"))
538 type = TPM_RT_DAA_V0;
539 else if (!strcasecmp(argv[1], "daa_v1"))
540 type = TPM_RT_DAA_V1;
543 printf("Resource type %s unknown.\n", argv[1]);
547 if (!strcasecmp(argv[2], "all")) {
554 /* fetch list of already loaded resources in the TPM */
555 err = tpm_get_capability(dev, TPM_CAP_HANDLE, type, buf,
558 printf("tpm_get_capability returned error %d.\n", err);
561 res_count = get_unaligned_be16(buf);
563 for (i = 0; i < res_count; ++i, ptr += 4)
564 tpm_flush_specific(dev, get_unaligned_be32(ptr), type);
566 u32 handle = simple_strtoul(argv[2], NULL, 0);
569 printf("Illegal resource handle %s\n", argv[2]);
572 tpm_flush_specific(dev, cpu_to_be32(handle), type);
577 #endif /* CONFIG_TPM_FLUSH_RESOURCES */
579 #ifdef CONFIG_TPM_LIST_RESOURCES
580 static int do_tpm_list(cmd_tbl_t *cmdtp, int flag, int argc,
591 return CMD_RET_USAGE;
593 if (!strcasecmp(argv[1], "key"))
595 else if (!strcasecmp(argv[1], "auth"))
597 else if (!strcasecmp(argv[1], "hash"))
599 else if (!strcasecmp(argv[1], "trans"))
601 else if (!strcasecmp(argv[1], "context"))
602 type = TPM_RT_CONTEXT;
603 else if (!strcasecmp(argv[1], "counter"))
604 type = TPM_RT_COUNTER;
605 else if (!strcasecmp(argv[1], "delegate"))
606 type = TPM_RT_DELEGATE;
607 else if (!strcasecmp(argv[1], "daa_tpm"))
608 type = TPM_RT_DAA_TPM;
609 else if (!strcasecmp(argv[1], "daa_v0"))
610 type = TPM_RT_DAA_V0;
611 else if (!strcasecmp(argv[1], "daa_v1"))
612 type = TPM_RT_DAA_V1;
615 printf("Resource type %s unknown.\n", argv[1]);
619 /* fetch list of already loaded resources in the TPM */
620 err = tpm_get_capability(TPM_CAP_HANDLE, type, buf,
623 printf("tpm_get_capability returned error %d.\n", err);
626 res_count = get_unaligned_be16(buf);
629 printf("Resources of type %s (%02x):\n", argv[1], type);
633 for (i = 0; i < res_count; ++i, ptr += 4)
634 printf("Index %d: %08x\n", i, get_unaligned_be32(ptr));
639 #endif /* CONFIG_TPM_LIST_RESOURCES */
641 TPM_COMMAND_NO_ARG(tpm_self_test_full)
642 TPM_COMMAND_NO_ARG(tpm_continue_self_test)
643 TPM_COMMAND_NO_ARG(tpm_force_clear)
644 TPM_COMMAND_NO_ARG(tpm_physical_enable)
645 TPM_COMMAND_NO_ARG(tpm_physical_disable)
647 static cmd_tbl_t tpm1_commands[] = {
648 U_BOOT_CMD_MKENT(device, 0, 1, do_tpm_device, "", ""),
649 U_BOOT_CMD_MKENT(info, 0, 1, do_tpm_info, "", ""),
650 U_BOOT_CMD_MKENT(init, 0, 1, do_tpm_init, "", ""),
651 U_BOOT_CMD_MKENT(startup, 0, 1,
652 do_tpm_startup, "", ""),
653 U_BOOT_CMD_MKENT(self_test_full, 0, 1,
654 do_tpm_self_test_full, "", ""),
655 U_BOOT_CMD_MKENT(continue_self_test, 0, 1,
656 do_tpm_continue_self_test, "", ""),
657 U_BOOT_CMD_MKENT(force_clear, 0, 1,
658 do_tpm_force_clear, "", ""),
659 U_BOOT_CMD_MKENT(physical_enable, 0, 1,
660 do_tpm_physical_enable, "", ""),
661 U_BOOT_CMD_MKENT(physical_disable, 0, 1,
662 do_tpm_physical_disable, "", ""),
663 U_BOOT_CMD_MKENT(nv_define_space, 0, 1,
664 do_tpm_nv_define_space, "", ""),
665 U_BOOT_CMD_MKENT(nv_read_value, 0, 1,
666 do_tpm_nv_read_value, "", ""),
667 U_BOOT_CMD_MKENT(nv_write_value, 0, 1,
668 do_tpm_nv_write_value, "", ""),
669 U_BOOT_CMD_MKENT(extend, 0, 1,
670 do_tpm_extend, "", ""),
671 U_BOOT_CMD_MKENT(pcr_read, 0, 1,
672 do_tpm_pcr_read, "", ""),
673 U_BOOT_CMD_MKENT(tsc_physical_presence, 0, 1,
674 do_tpm_tsc_physical_presence, "", ""),
675 U_BOOT_CMD_MKENT(read_pubek, 0, 1,
676 do_tpm_read_pubek, "", ""),
677 U_BOOT_CMD_MKENT(physical_set_deactivated, 0, 1,
678 do_tpm_physical_set_deactivated, "", ""),
679 U_BOOT_CMD_MKENT(get_capability, 0, 1,
680 do_tpm_get_capability, "", ""),
681 U_BOOT_CMD_MKENT(raw_transfer, 0, 1,
682 do_tpm_raw_transfer, "", ""),
683 U_BOOT_CMD_MKENT(nv_define, 0, 1,
684 do_tpm_nv_define, "", ""),
685 U_BOOT_CMD_MKENT(nv_read, 0, 1,
686 do_tpm_nv_read, "", ""),
687 U_BOOT_CMD_MKENT(nv_write, 0, 1,
688 do_tpm_nv_write, "", ""),
689 #ifdef CONFIG_TPM_AUTH_SESSIONS
690 U_BOOT_CMD_MKENT(oiap, 0, 1,
691 do_tpm_oiap, "", ""),
692 U_BOOT_CMD_MKENT(end_oiap, 0, 1,
693 do_tpm_end_oiap, "", ""),
694 U_BOOT_CMD_MKENT(load_key2_oiap, 0, 1,
695 do_tpm_load_key2_oiap, "", ""),
696 #ifdef CONFIG_TPM_LOAD_KEY_BY_SHA1
697 U_BOOT_CMD_MKENT(load_key_by_sha1, 0, 1,
698 do_tpm_load_key_by_sha1, "", ""),
699 #endif /* CONFIG_TPM_LOAD_KEY_BY_SHA1 */
700 U_BOOT_CMD_MKENT(get_pub_key_oiap, 0, 1,
701 do_tpm_get_pub_key_oiap, "", ""),
702 #endif /* CONFIG_TPM_AUTH_SESSIONS */
703 #ifdef CONFIG_TPM_FLUSH_RESOURCES
704 U_BOOT_CMD_MKENT(flush, 0, 1,
705 do_tpm_flush, "", ""),
706 #endif /* CONFIG_TPM_FLUSH_RESOURCES */
707 #ifdef CONFIG_TPM_LIST_RESOURCES
708 U_BOOT_CMD_MKENT(list, 0, 1,
709 do_tpm_list, "", ""),
710 #endif /* CONFIG_TPM_LIST_RESOURCES */
713 cmd_tbl_t *get_tpm1_commands(unsigned int *size)
715 *size = ARRAY_SIZE(tpm1_commands);
717 return tpm1_commands;
720 U_BOOT_CMD(tpm, CONFIG_SYS_MAXARGS, 1, do_tpm,
721 "Issue a TPMv1.x command",
723 " - Issue TPM command <cmd> with arguments <args...>.\n"
724 "Admin Startup and State Commands:\n"
725 " device [num device]\n"
726 " - Show all devices or set the specified device\n"
727 " info - Show information about the TPM\n"
729 " - Put TPM into a state where it waits for 'startup' command.\n"
731 " - Issue TPM_Starup command. <mode> is one of TPM_ST_CLEAR,\n"
732 " TPM_ST_STATE, and TPM_ST_DEACTIVATED.\n"
733 "Admin Testing Commands:\n"
735 " - Test all of the TPM capabilities.\n"
736 " continue_self_test\n"
737 " - Inform TPM that it should complete the self-test.\n"
738 "Admin Opt-in Commands:\n"
740 " - Set the PERMANENT disable flag to FALSE using physical presence as\n"
742 " physical_disable\n"
743 " - Set the PERMANENT disable flag to TRUE using physical presence as\n"
745 " physical_set_deactivated 0|1\n"
746 " - Set deactivated flag.\n"
747 "Admin Ownership Commands:\n"
749 " - Issue TPM_ForceClear command.\n"
750 " tsc_physical_presence flags\n"
751 " - Set TPM device's Physical Presence flags to <flags>.\n"
752 "The Capability Commands:\n"
753 " get_capability cap_area sub_cap addr count\n"
754 " - Read <count> bytes of TPM capability indexed by <cap_area> and\n"
755 " <sub_cap> to memory address <addr>.\n"
756 #if defined(CONFIG_TPM_FLUSH_RESOURCES) || defined(CONFIG_TPM_LIST_RESOURCES)
757 "Resource management functions\n"
759 #ifdef CONFIG_TPM_FLUSH_RESOURCES
760 " flush resource_type id\n"
761 " - flushes a resource of type <resource_type> (may be one of key, auth,\n"
762 " hash, trans, context, counter, delegate, daa_tpm, daa_v0, daa_v1),\n"
763 " and id <id> from the TPM. Use an <id> of \"all\" to flush all\n"
764 " resources of that type.\n"
765 #endif /* CONFIG_TPM_FLUSH_RESOURCES */
766 #ifdef CONFIG_TPM_LIST_RESOURCES
767 " list resource_type\n"
768 " - lists resources of type <resource_type> (may be one of key, auth,\n"
769 " hash, trans, context, counter, delegate, daa_tpm, daa_v0, daa_v1),\n"
770 " contained in the TPM.\n"
771 #endif /* CONFIG_TPM_LIST_RESOURCES */
772 #ifdef CONFIG_TPM_AUTH_SESSIONS
773 "Storage functions\n"
774 " loadkey2_oiap parent_handle key_addr key_len usage_auth\n"
775 " - loads a key data from memory address <key_addr>, <key_len> bytes\n"
776 " into TPM using the parent key <parent_handle> with authorization\n"
777 " <usage_auth> (20 bytes hex string).\n"
778 #ifdef CONFIG_TPM_LOAD_KEY_BY_SHA1
779 " load_key_by_sha1 parent_hash key_addr key_len usage_auth\n"
780 " - loads a key data from memory address <key_addr>, <key_len> bytes\n"
781 " into TPM using the parent hash <parent_hash> (20 bytes hex string)\n"
782 " with authorization <usage_auth> (20 bytes hex string).\n"
783 #endif /* CONFIG_TPM_LOAD_KEY_BY_SHA1 */
784 " get_pub_key_oiap key_handle usage_auth\n"
785 " - get the public key portion of a loaded key <key_handle> using\n"
786 " authorization <usage auth> (20 bytes hex string)\n"
787 #endif /* CONFIG_TPM_AUTH_SESSIONS */
788 "Endorsement Key Handling Commands:\n"
789 " read_pubek addr count\n"
790 " - Read <count> bytes of the public endorsement key to memory\n"
792 "Integrity Collection and Reporting Commands:\n"
793 " extend index digest_hex_string\n"
794 " - Add a new measurement to a PCR. Update PCR <index> with the 20-bytes\n"
795 " <digest_hex_string>\n"
796 " pcr_read index addr count\n"
797 " - Read <count> bytes from PCR <index> to memory address <addr>.\n"
798 #ifdef CONFIG_TPM_AUTH_SESSIONS
799 "Authorization Sessions\n"
801 " - setup an OIAP session\n"
803 " - terminates an active OIAP session\n"
804 #endif /* CONFIG_TPM_AUTH_SESSIONS */
805 "Non-volatile Storage Commands:\n"
806 " nv_define_space index permission size\n"
807 " - Establish a space at index <index> with <permission> of <size> bytes.\n"
808 " nv_read_value index addr count\n"
809 " - Read <count> bytes from space <index> to memory address <addr>.\n"
810 " nv_write_value index addr count\n"
811 " - Write <count> bytes from memory address <addr> to space <index>.\n"
812 "Miscellaneous helper functions:\n"
813 " raw_transfer byte_string\n"
814 " - Send a byte string <byte_string> to TPM and print the response.\n"
815 " Non-volatile storage helper functions:\n"
816 " These helper functions treat a non-volatile space as a non-padded\n"
817 " sequence of integer values. These integer values are defined by a type\n"
818 " string, which is a text string of 'bwd' characters: 'b' means a 8-bit\n"
819 " value, 'w' 16-bit value, 'd' 32-bit value. All helper functions take\n"
820 " a type string as their first argument.\n"
821 " nv_define type_string index perm\n"
822 " - Define a space <index> with permission <perm>.\n"
823 " nv_read types_string index vars...\n"
824 " - Read from space <index> to environment variables <vars...>.\n"
825 " nv_write types_string index values...\n"
826 " - Write to space <index> from values <values...>.\n"