1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2013 The Chromium OS Authors.
8 #include <asm/unaligned.h>
9 #include <tpm-common.h>
11 #include "tpm-user-utils.h"
13 static int do_tpm_startup(cmd_tbl_t *cmdtp, int flag, int argc,
16 enum tpm_startup_type mode;
20 if (!strcasecmp("TPM_ST_CLEAR", argv[1])) {
22 } else if (!strcasecmp("TPM_ST_STATE", argv[1])) {
24 } else if (!strcasecmp("TPM_ST_DEACTIVATED", argv[1])) {
25 mode = TPM_ST_DEACTIVATED;
27 printf("Couldn't recognize mode string: %s\n", argv[1]);
28 return CMD_RET_FAILURE;
31 return report_return_code(tpm_startup(mode));
34 static int do_tpm_nv_define_space(cmd_tbl_t *cmdtp, int flag, int argc,
37 u32 index, perm, size;
41 index = simple_strtoul(argv[1], NULL, 0);
42 perm = simple_strtoul(argv[2], NULL, 0);
43 size = simple_strtoul(argv[3], NULL, 0);
45 return report_return_code(tpm_nv_define_space(index, perm, size));
48 static int do_tpm_nv_read_value(cmd_tbl_t *cmdtp, int flag, int argc,
56 index = simple_strtoul(argv[1], NULL, 0);
57 data = (void *)simple_strtoul(argv[2], NULL, 0);
58 count = simple_strtoul(argv[3], NULL, 0);
60 rc = tpm_nv_read_value(index, data, count);
62 puts("area content:\n");
63 print_byte_string(data, count);
66 return report_return_code(rc);
69 static int do_tpm_nv_write_value(cmd_tbl_t *cmdtp, int flag, int argc,
78 index = simple_strtoul(argv[1], NULL, 0);
79 data = parse_byte_string(argv[2], NULL, &count);
81 printf("Couldn't parse byte string %s\n", argv[2]);
82 return CMD_RET_FAILURE;
85 rc = tpm_nv_write_value(index, data, count);
88 return report_return_code(rc);
91 static int do_tpm_extend(cmd_tbl_t *cmdtp, int flag, int argc,
95 u8 in_digest[20], out_digest[20];
99 index = simple_strtoul(argv[1], NULL, 0);
100 if (!parse_byte_string(argv[2], in_digest, NULL)) {
101 printf("Couldn't parse byte string %s\n", argv[2]);
102 return CMD_RET_FAILURE;
105 rc = tpm_extend(index, in_digest, out_digest);
107 puts("PCR value after execution of the command:\n");
108 print_byte_string(out_digest, sizeof(out_digest));
111 return report_return_code(rc);
114 static int do_tpm_pcr_read(cmd_tbl_t *cmdtp, int flag, int argc,
117 u32 index, count, rc;
121 return CMD_RET_USAGE;
122 index = simple_strtoul(argv[1], NULL, 0);
123 data = (void *)simple_strtoul(argv[2], NULL, 0);
124 count = simple_strtoul(argv[3], NULL, 0);
126 rc = tpm_pcr_read(index, data, count);
128 puts("Named PCR content:\n");
129 print_byte_string(data, count);
132 return report_return_code(rc);
135 static int do_tpm_tsc_physical_presence(cmd_tbl_t *cmdtp, int flag, int argc,
141 return CMD_RET_USAGE;
142 presence = (u16)simple_strtoul(argv[1], NULL, 0);
144 return report_return_code(tpm_tsc_physical_presence(presence));
147 static int do_tpm_read_pubek(cmd_tbl_t *cmdtp, int flag, int argc,
154 return CMD_RET_USAGE;
155 data = (void *)simple_strtoul(argv[1], NULL, 0);
156 count = simple_strtoul(argv[2], NULL, 0);
158 rc = tpm_read_pubek(data, count);
160 puts("pubek value:\n");
161 print_byte_string(data, count);
164 return report_return_code(rc);
167 static int do_tpm_physical_set_deactivated(cmd_tbl_t *cmdtp, int flag, int argc,
173 return CMD_RET_USAGE;
174 state = (u8)simple_strtoul(argv[1], NULL, 0);
176 return report_return_code(tpm_physical_set_deactivated(state));
179 static int do_tpm_get_capability(cmd_tbl_t *cmdtp, int flag, int argc,
182 u32 cap_area, sub_cap, rc;
187 return CMD_RET_USAGE;
188 cap_area = simple_strtoul(argv[1], NULL, 0);
189 sub_cap = simple_strtoul(argv[2], NULL, 0);
190 cap = (void *)simple_strtoul(argv[3], NULL, 0);
191 count = simple_strtoul(argv[4], NULL, 0);
193 rc = tpm_get_capability(cap_area, sub_cap, cap, count);
195 puts("capability information:\n");
196 print_byte_string(cap, count);
199 return report_return_code(rc);
202 static int do_tpm_raw_transfer(cmd_tbl_t *cmdtp, int flag, int argc,
208 size_t count, response_length = sizeof(response);
211 command = parse_byte_string(argv[1], NULL, &count);
213 printf("Couldn't parse byte string %s\n", argv[1]);
214 return CMD_RET_FAILURE;
221 rc = tpm_xfer(dev, command, count, response, &response_length);
224 puts("tpm response:\n");
225 print_byte_string(response, response_length);
228 return report_return_code(rc);
231 static int do_tpm_nv_define(cmd_tbl_t *cmdtp, int flag, int argc,
234 u32 index, perm, size;
237 return CMD_RET_USAGE;
238 size = type_string_get_space_size(argv[1]);
240 printf("Couldn't parse arguments\n");
241 return CMD_RET_USAGE;
243 index = simple_strtoul(argv[2], NULL, 0);
244 perm = simple_strtoul(argv[3], NULL, 0);
246 return report_return_code(tpm_nv_define_space(index, perm, size));
249 static int do_tpm_nv_read(cmd_tbl_t *cmdtp, int flag, int argc,
252 u32 index, count, err;
256 return CMD_RET_USAGE;
257 if (argc != 3 + type_string_get_num_values(argv[1]))
258 return CMD_RET_USAGE;
259 index = simple_strtoul(argv[2], NULL, 0);
260 data = type_string_alloc(argv[1], &count);
262 printf("Couldn't parse arguments\n");
263 return CMD_RET_USAGE;
266 err = tpm_nv_read_value(index, data, count);
268 if (type_string_write_vars(argv[1], data, argv + 3)) {
269 printf("Couldn't write to variables\n");
275 return report_return_code(err);
278 static int do_tpm_nv_write(cmd_tbl_t *cmdtp, int flag, int argc,
281 u32 index, count, err;
285 return CMD_RET_USAGE;
286 if (argc != 3 + type_string_get_num_values(argv[1]))
287 return CMD_RET_USAGE;
288 index = simple_strtoul(argv[2], NULL, 0);
289 data = type_string_alloc(argv[1], &count);
291 printf("Couldn't parse arguments\n");
292 return CMD_RET_USAGE;
294 if (type_string_pack(argv[1], argv + 3, data)) {
295 printf("Couldn't parse arguments\n");
297 return CMD_RET_USAGE;
300 err = tpm_nv_write_value(index, data, count);
303 return report_return_code(err);
306 #ifdef CONFIG_TPM_AUTH_SESSIONS
308 static int do_tpm_oiap(cmd_tbl_t *cmdtp, int flag, int argc,
311 u32 auth_handle, err;
313 err = tpm_oiap(&auth_handle);
315 return report_return_code(err);
318 #ifdef CONFIG_TPM_LOAD_KEY_BY_SHA1
319 static int do_tpm_load_key_by_sha1(cmd_tbl_t *cmdtp, int flag, int argc, char *
322 u32 parent_handle = 0;
323 u32 key_len, key_handle, err;
324 u8 usage_auth[DIGEST_LENGTH];
325 u8 parent_hash[DIGEST_LENGTH];
329 return CMD_RET_USAGE;
331 parse_byte_string(argv[1], parent_hash, NULL);
332 key = (void *)simple_strtoul(argv[2], NULL, 0);
333 key_len = simple_strtoul(argv[3], NULL, 0);
334 if (strlen(argv[4]) != 2 * DIGEST_LENGTH)
335 return CMD_RET_FAILURE;
336 parse_byte_string(argv[4], usage_auth, NULL);
338 err = tpm_find_key_sha1(usage_auth, parent_hash, &parent_handle);
340 printf("Could not find matching parent key (err = %d)\n", err);
341 return CMD_RET_FAILURE;
344 printf("Found parent key %08x\n", parent_handle);
346 err = tpm_load_key2_oiap(parent_handle, key, key_len, usage_auth,
349 printf("Key handle is 0x%x\n", key_handle);
350 env_set_hex("key_handle", key_handle);
353 return report_return_code(err);
355 #endif /* CONFIG_TPM_LOAD_KEY_BY_SHA1 */
357 static int do_tpm_load_key2_oiap(cmd_tbl_t *cmdtp, int flag, int argc,
360 u32 parent_handle, key_len, key_handle, err;
361 u8 usage_auth[DIGEST_LENGTH];
365 return CMD_RET_USAGE;
367 parent_handle = simple_strtoul(argv[1], NULL, 0);
368 key = (void *)simple_strtoul(argv[2], NULL, 0);
369 key_len = simple_strtoul(argv[3], NULL, 0);
370 if (strlen(argv[4]) != 2 * DIGEST_LENGTH)
371 return CMD_RET_FAILURE;
372 parse_byte_string(argv[4], usage_auth, NULL);
374 err = tpm_load_key2_oiap(parent_handle, key, key_len, usage_auth,
377 printf("Key handle is 0x%x\n", key_handle);
379 return report_return_code(err);
382 static int do_tpm_get_pub_key_oiap(cmd_tbl_t *cmdtp, int flag, int argc,
386 u8 usage_auth[DIGEST_LENGTH];
387 u8 pub_key_buffer[TPM_PUBKEY_MAX_LENGTH];
388 size_t pub_key_len = sizeof(pub_key_buffer);
391 return CMD_RET_USAGE;
393 key_handle = simple_strtoul(argv[1], NULL, 0);
394 if (strlen(argv[2]) != 2 * DIGEST_LENGTH)
395 return CMD_RET_FAILURE;
396 parse_byte_string(argv[2], usage_auth, NULL);
398 err = tpm_get_pub_key_oiap(key_handle, usage_auth, pub_key_buffer,
401 printf("dump of received pub key structure:\n");
402 print_byte_string(pub_key_buffer, pub_key_len);
404 return report_return_code(err);
407 TPM_COMMAND_NO_ARG(tpm_end_oiap)
409 #endif /* CONFIG_TPM_AUTH_SESSIONS */
411 #ifdef CONFIG_TPM_FLUSH_RESOURCES
412 static int do_tpm_flush(cmd_tbl_t *cmdtp, int flag, int argc,
418 return CMD_RET_USAGE;
420 if (!strcasecmp(argv[1], "key"))
422 else if (!strcasecmp(argv[1], "auth"))
424 else if (!strcasecmp(argv[1], "hash"))
426 else if (!strcasecmp(argv[1], "trans"))
428 else if (!strcasecmp(argv[1], "context"))
429 type = TPM_RT_CONTEXT;
430 else if (!strcasecmp(argv[1], "counter"))
431 type = TPM_RT_COUNTER;
432 else if (!strcasecmp(argv[1], "delegate"))
433 type = TPM_RT_DELEGATE;
434 else if (!strcasecmp(argv[1], "daa_tpm"))
435 type = TPM_RT_DAA_TPM;
436 else if (!strcasecmp(argv[1], "daa_v0"))
437 type = TPM_RT_DAA_V0;
438 else if (!strcasecmp(argv[1], "daa_v1"))
439 type = TPM_RT_DAA_V1;
442 printf("Resource type %s unknown.\n", argv[1]);
446 if (!strcasecmp(argv[2], "all")) {
453 /* fetch list of already loaded resources in the TPM */
454 err = tpm_get_capability(TPM_CAP_HANDLE, type, buf,
457 printf("tpm_get_capability returned error %d.\n", err);
460 res_count = get_unaligned_be16(buf);
462 for (i = 0; i < res_count; ++i, ptr += 4)
463 tpm_flush_specific(get_unaligned_be32(ptr), type);
465 u32 handle = simple_strtoul(argv[2], NULL, 0);
468 printf("Illegal resource handle %s\n", argv[2]);
471 tpm_flush_specific(cpu_to_be32(handle), type);
476 #endif /* CONFIG_TPM_FLUSH_RESOURCES */
478 #ifdef CONFIG_TPM_LIST_RESOURCES
479 static int do_tpm_list(cmd_tbl_t *cmdtp, int flag, int argc,
490 return CMD_RET_USAGE;
492 if (!strcasecmp(argv[1], "key"))
494 else if (!strcasecmp(argv[1], "auth"))
496 else if (!strcasecmp(argv[1], "hash"))
498 else if (!strcasecmp(argv[1], "trans"))
500 else if (!strcasecmp(argv[1], "context"))
501 type = TPM_RT_CONTEXT;
502 else if (!strcasecmp(argv[1], "counter"))
503 type = TPM_RT_COUNTER;
504 else if (!strcasecmp(argv[1], "delegate"))
505 type = TPM_RT_DELEGATE;
506 else if (!strcasecmp(argv[1], "daa_tpm"))
507 type = TPM_RT_DAA_TPM;
508 else if (!strcasecmp(argv[1], "daa_v0"))
509 type = TPM_RT_DAA_V0;
510 else if (!strcasecmp(argv[1], "daa_v1"))
511 type = TPM_RT_DAA_V1;
514 printf("Resource type %s unknown.\n", argv[1]);
518 /* fetch list of already loaded resources in the TPM */
519 err = tpm_get_capability(TPM_CAP_HANDLE, type, buf,
522 printf("tpm_get_capability returned error %d.\n", err);
525 res_count = get_unaligned_be16(buf);
528 printf("Resources of type %s (%02x):\n", argv[1], type);
532 for (i = 0; i < res_count; ++i, ptr += 4)
533 printf("Index %d: %08x\n", i, get_unaligned_be32(ptr));
538 #endif /* CONFIG_TPM_LIST_RESOURCES */
540 TPM_COMMAND_NO_ARG(tpm_self_test_full)
541 TPM_COMMAND_NO_ARG(tpm_continue_self_test)
542 TPM_COMMAND_NO_ARG(tpm_force_clear)
543 TPM_COMMAND_NO_ARG(tpm_physical_enable)
544 TPM_COMMAND_NO_ARG(tpm_physical_disable)
546 static cmd_tbl_t tpm1_commands[] = {
547 U_BOOT_CMD_MKENT(info, 0, 1, do_tpm_info, "", ""),
548 U_BOOT_CMD_MKENT(init, 0, 1, do_tpm_init, "", ""),
549 U_BOOT_CMD_MKENT(startup, 0, 1,
550 do_tpm_startup, "", ""),
551 U_BOOT_CMD_MKENT(self_test_full, 0, 1,
552 do_tpm_self_test_full, "", ""),
553 U_BOOT_CMD_MKENT(continue_self_test, 0, 1,
554 do_tpm_continue_self_test, "", ""),
555 U_BOOT_CMD_MKENT(force_clear, 0, 1,
556 do_tpm_force_clear, "", ""),
557 U_BOOT_CMD_MKENT(physical_enable, 0, 1,
558 do_tpm_physical_enable, "", ""),
559 U_BOOT_CMD_MKENT(physical_disable, 0, 1,
560 do_tpm_physical_disable, "", ""),
561 U_BOOT_CMD_MKENT(nv_define_space, 0, 1,
562 do_tpm_nv_define_space, "", ""),
563 U_BOOT_CMD_MKENT(nv_read_value, 0, 1,
564 do_tpm_nv_read_value, "", ""),
565 U_BOOT_CMD_MKENT(nv_write_value, 0, 1,
566 do_tpm_nv_write_value, "", ""),
567 U_BOOT_CMD_MKENT(extend, 0, 1,
568 do_tpm_extend, "", ""),
569 U_BOOT_CMD_MKENT(pcr_read, 0, 1,
570 do_tpm_pcr_read, "", ""),
571 U_BOOT_CMD_MKENT(tsc_physical_presence, 0, 1,
572 do_tpm_tsc_physical_presence, "", ""),
573 U_BOOT_CMD_MKENT(read_pubek, 0, 1,
574 do_tpm_read_pubek, "", ""),
575 U_BOOT_CMD_MKENT(physical_set_deactivated, 0, 1,
576 do_tpm_physical_set_deactivated, "", ""),
577 U_BOOT_CMD_MKENT(get_capability, 0, 1,
578 do_tpm_get_capability, "", ""),
579 U_BOOT_CMD_MKENT(raw_transfer, 0, 1,
580 do_tpm_raw_transfer, "", ""),
581 U_BOOT_CMD_MKENT(nv_define, 0, 1,
582 do_tpm_nv_define, "", ""),
583 U_BOOT_CMD_MKENT(nv_read, 0, 1,
584 do_tpm_nv_read, "", ""),
585 U_BOOT_CMD_MKENT(nv_write, 0, 1,
586 do_tpm_nv_write, "", ""),
587 #ifdef CONFIG_TPM_AUTH_SESSIONS
588 U_BOOT_CMD_MKENT(oiap, 0, 1,
589 do_tpm_oiap, "", ""),
590 U_BOOT_CMD_MKENT(end_oiap, 0, 1,
591 do_tpm_end_oiap, "", ""),
592 U_BOOT_CMD_MKENT(load_key2_oiap, 0, 1,
593 do_tpm_load_key2_oiap, "", ""),
594 #ifdef CONFIG_TPM_LOAD_KEY_BY_SHA1
595 U_BOOT_CMD_MKENT(load_key_by_sha1, 0, 1,
596 do_tpm_load_key_by_sha1, "", ""),
597 #endif /* CONFIG_TPM_LOAD_KEY_BY_SHA1 */
598 U_BOOT_CMD_MKENT(get_pub_key_oiap, 0, 1,
599 do_tpm_get_pub_key_oiap, "", ""),
600 #endif /* CONFIG_TPM_AUTH_SESSIONS */
601 #ifdef CONFIG_TPM_FLUSH_RESOURCES
602 U_BOOT_CMD_MKENT(flush, 0, 1,
603 do_tpm_flush, "", ""),
604 #endif /* CONFIG_TPM_FLUSH_RESOURCES */
605 #ifdef CONFIG_TPM_LIST_RESOURCES
606 U_BOOT_CMD_MKENT(list, 0, 1,
607 do_tpm_list, "", ""),
608 #endif /* CONFIG_TPM_LIST_RESOURCES */
611 cmd_tbl_t *get_tpm1_commands(unsigned int *size)
613 *size = ARRAY_SIZE(tpm1_commands);
615 return tpm1_commands;
618 U_BOOT_CMD(tpm, CONFIG_SYS_MAXARGS, 1, do_tpm,
619 "Issue a TPMv1.x command",
621 " - Issue TPM command <cmd> with arguments <args...>.\n"
622 "Admin Startup and State Commands:\n"
623 " info - Show information about the TPM\n"
625 " - Put TPM into a state where it waits for 'startup' command.\n"
627 " - Issue TPM_Starup command. <mode> is one of TPM_ST_CLEAR,\n"
628 " TPM_ST_STATE, and TPM_ST_DEACTIVATED.\n"
629 "Admin Testing Commands:\n"
631 " - Test all of the TPM capabilities.\n"
632 " continue_self_test\n"
633 " - Inform TPM that it should complete the self-test.\n"
634 "Admin Opt-in Commands:\n"
636 " - Set the PERMANENT disable flag to FALSE using physical presence as\n"
638 " physical_disable\n"
639 " - Set the PERMANENT disable flag to TRUE using physical presence as\n"
641 " physical_set_deactivated 0|1\n"
642 " - Set deactivated flag.\n"
643 "Admin Ownership Commands:\n"
645 " - Issue TPM_ForceClear command.\n"
646 " tsc_physical_presence flags\n"
647 " - Set TPM device's Physical Presence flags to <flags>.\n"
648 "The Capability Commands:\n"
649 " get_capability cap_area sub_cap addr count\n"
650 " - Read <count> bytes of TPM capability indexed by <cap_area> and\n"
651 " <sub_cap> to memory address <addr>.\n"
652 #if defined(CONFIG_TPM_FLUSH_RESOURCES) || defined(CONFIG_TPM_LIST_RESOURCES)
653 "Resource management functions\n"
655 #ifdef CONFIG_TPM_FLUSH_RESOURCES
656 " flush resource_type id\n"
657 " - flushes a resource of type <resource_type> (may be one of key, auth,\n"
658 " hash, trans, context, counter, delegate, daa_tpm, daa_v0, daa_v1),\n"
659 " and id <id> from the TPM. Use an <id> of \"all\" to flush all\n"
660 " resources of that type.\n"
661 #endif /* CONFIG_TPM_FLUSH_RESOURCES */
662 #ifdef CONFIG_TPM_LIST_RESOURCES
663 " list resource_type\n"
664 " - lists resources of type <resource_type> (may be one of key, auth,\n"
665 " hash, trans, context, counter, delegate, daa_tpm, daa_v0, daa_v1),\n"
666 " contained in the TPM.\n"
667 #endif /* CONFIG_TPM_LIST_RESOURCES */
668 #ifdef CONFIG_TPM_AUTH_SESSIONS
669 "Storage functions\n"
670 " loadkey2_oiap parent_handle key_addr key_len usage_auth\n"
671 " - loads a key data from memory address <key_addr>, <key_len> bytes\n"
672 " into TPM using the parent key <parent_handle> with authorization\n"
673 " <usage_auth> (20 bytes hex string).\n"
674 #ifdef CONFIG_TPM_LOAD_KEY_BY_SHA1
675 " load_key_by_sha1 parent_hash key_addr key_len usage_auth\n"
676 " - loads a key data from memory address <key_addr>, <key_len> bytes\n"
677 " into TPM using the parent hash <parent_hash> (20 bytes hex string)\n"
678 " with authorization <usage_auth> (20 bytes hex string).\n"
679 #endif /* CONFIG_TPM_LOAD_KEY_BY_SHA1 */
680 " get_pub_key_oiap key_handle usage_auth\n"
681 " - get the public key portion of a loaded key <key_handle> using\n"
682 " authorization <usage auth> (20 bytes hex string)\n"
683 #endif /* CONFIG_TPM_AUTH_SESSIONS */
684 "Endorsement Key Handling Commands:\n"
685 " read_pubek addr count\n"
686 " - Read <count> bytes of the public endorsement key to memory\n"
688 "Integrity Collection and Reporting Commands:\n"
689 " extend index digest_hex_string\n"
690 " - Add a new measurement to a PCR. Update PCR <index> with the 20-bytes\n"
691 " <digest_hex_string>\n"
692 " pcr_read index addr count\n"
693 " - Read <count> bytes from PCR <index> to memory address <addr>.\n"
694 #ifdef CONFIG_TPM_AUTH_SESSIONS
695 "Authorization Sessions\n"
697 " - setup an OIAP session\n"
699 " - terminates an active OIAP session\n"
700 #endif /* CONFIG_TPM_AUTH_SESSIONS */
701 "Non-volatile Storage Commands:\n"
702 " nv_define_space index permission size\n"
703 " - Establish a space at index <index> with <permission> of <size> bytes.\n"
704 " nv_read_value index addr count\n"
705 " - Read <count> bytes from space <index> to memory address <addr>.\n"
706 " nv_write_value index addr count\n"
707 " - Write <count> bytes from memory address <addr> to space <index>.\n"
708 "Miscellaneous helper functions:\n"
709 " raw_transfer byte_string\n"
710 " - Send a byte string <byte_string> to TPM and print the response.\n"
711 " Non-volatile storage helper functions:\n"
712 " These helper functions treat a non-volatile space as a non-padded\n"
713 " sequence of integer values. These integer values are defined by a type\n"
714 " string, which is a text string of 'bwd' characters: 'b' means a 8-bit\n"
715 " value, 'w' 16-bit value, 'd' 32-bit value. All helper functions take\n"
716 " a type string as their first argument.\n"
717 " nv_define type_string index perm\n"
718 " - Define a space <index> with permission <perm>.\n"
719 " nv_read types_string index vars...\n"
720 " - Read from space <index> to environment variables <vars...>.\n"
721 " nv_write types_string index values...\n"
722 " - Write to space <index> from values <values...>.\n"