2 * Copyright (c) 2013 The Chromium OS Authors.
4 * SPDX-License-Identifier: GPL-2.0+
12 #include <asm/unaligned.h>
13 #include <linux/string.h>
15 /* Useful constants */
18 /* max lengths, valid for RSA keys <= 2048 bits */
19 TPM_PUBKEY_MAX_LENGTH = 288,
23 * Print a byte string in hexdecimal format, 16-bytes per line.
25 * @param data byte string to be printed
26 * @param count number of bytes to be printed
28 static void print_byte_string(uint8_t *data, size_t count)
30 int i, print_newline = 0;
32 for (i = 0; i < count; i++) {
33 printf(" %02x", data[i]);
34 print_newline = (i % 16 == 15);
38 /* Avoid duplicated newline at the end */
44 * Convert a text string of hexdecimal values into a byte string.
46 * @param bytes text string of hexdecimal values with no space
48 * @param data output buffer for byte string. The caller has to make
49 * sure it is large enough for storing the output. If
50 * NULL is passed, a large enough buffer will be allocated,
51 * and the caller must free it.
52 * @param count_ptr output variable for the length of byte string
53 * @return pointer to output buffer
55 static void *parse_byte_string(char *bytes, uint8_t *data, size_t *count_ptr)
61 length = strlen(bytes);
70 for (i = 0; i < length; i += 2) {
72 byte[1] = bytes[i + 1];
73 data[i / 2] = (uint8_t)simple_strtoul(byte, NULL, 16);
83 * report_return_code() - Report any error and return failure or success
85 * @param return_code TPM command return code
86 * @return value of enum command_ret_t
88 static int report_return_code(int return_code)
91 printf("Error: %d\n", return_code);
92 return CMD_RET_FAILURE;
94 return CMD_RET_SUCCESS;
99 * Return number of values defined by a type string.
101 * @param type_str type string
102 * @return number of values of type string
104 static int type_string_get_num_values(const char *type_str)
106 return strlen(type_str);
110 * Return total size of values defined by a type string.
112 * @param type_str type string
113 * @return total size of values of type string, or 0 if type string
114 * contains illegal type character.
116 static size_t type_string_get_space_size(const char *type_str)
120 for (size = 0; *type_str; type_str++) {
140 * Allocate a buffer large enough to hold values defined by a type
141 * string. The caller has to free the buffer.
143 * @param type_str type string
144 * @param count pointer for storing size of buffer
145 * @return pointer to buffer or NULL on error
147 static void *type_string_alloc(const char *type_str, uint32_t *count)
152 size = type_string_get_space_size(type_str);
163 * Pack values defined by a type string into a buffer. The buffer must have
164 * large enough space.
166 * @param type_str type string
167 * @param values text strings of values to be packed
168 * @param data output buffer of values
169 * @return 0 on success, non-0 on error
171 static int type_string_pack(const char *type_str, char * const values[],
177 for (offset = 0; *type_str; type_str++, values++) {
178 value = simple_strtoul(values[0], NULL, 0);
181 data[offset] = value;
185 put_unaligned_be16(value, data + offset);
189 put_unaligned_be32(value, data + offset);
201 * Read values defined by a type string from a buffer, and write these values
202 * to environment variables.
204 * @param type_str type string
205 * @param data input buffer of values
206 * @param vars names of environment variables
207 * @return 0 on success, non-0 on error
209 static int type_string_write_vars(const char *type_str, uint8_t *data,
215 for (offset = 0; *type_str; type_str++, vars++) {
218 value = data[offset];
222 value = get_unaligned_be16(data + offset);
226 value = get_unaligned_be32(data + offset);
232 if (setenv_ulong(*vars, value))
239 static int do_tpm_startup(cmd_tbl_t *cmdtp, int flag,
240 int argc, char * const argv[])
242 enum tpm_startup_type mode;
245 return CMD_RET_USAGE;
246 if (!strcasecmp("TPM_ST_CLEAR", argv[1])) {
248 } else if (!strcasecmp("TPM_ST_STATE", argv[1])) {
250 } else if (!strcasecmp("TPM_ST_DEACTIVATED", argv[1])) {
251 mode = TPM_ST_DEACTIVATED;
253 printf("Couldn't recognize mode string: %s\n", argv[1]);
254 return CMD_RET_FAILURE;
257 return report_return_code(tpm_startup(mode));
260 static int do_tpm_nv_define_space(cmd_tbl_t *cmdtp, int flag,
261 int argc, char * const argv[])
263 uint32_t index, perm, size;
266 return CMD_RET_USAGE;
267 index = simple_strtoul(argv[1], NULL, 0);
268 perm = simple_strtoul(argv[2], NULL, 0);
269 size = simple_strtoul(argv[3], NULL, 0);
271 return report_return_code(tpm_nv_define_space(index, perm, size));
274 static int do_tpm_nv_read_value(cmd_tbl_t *cmdtp, int flag,
275 int argc, char * const argv[])
277 uint32_t index, count, rc;
281 return CMD_RET_USAGE;
282 index = simple_strtoul(argv[1], NULL, 0);
283 data = (void *)simple_strtoul(argv[2], NULL, 0);
284 count = simple_strtoul(argv[3], NULL, 0);
286 rc = tpm_nv_read_value(index, data, count);
288 puts("area content:\n");
289 print_byte_string(data, count);
292 return report_return_code(rc);
295 static int do_tpm_nv_write_value(cmd_tbl_t *cmdtp, int flag,
296 int argc, char * const argv[])
303 return CMD_RET_USAGE;
304 index = simple_strtoul(argv[1], NULL, 0);
305 data = parse_byte_string(argv[2], NULL, &count);
307 printf("Couldn't parse byte string %s\n", argv[2]);
308 return CMD_RET_FAILURE;
311 rc = tpm_nv_write_value(index, data, count);
314 return report_return_code(rc);
317 static int do_tpm_extend(cmd_tbl_t *cmdtp, int flag,
318 int argc, char * const argv[])
321 uint8_t in_digest[20], out_digest[20];
324 return CMD_RET_USAGE;
325 index = simple_strtoul(argv[1], NULL, 0);
326 if (!parse_byte_string(argv[2], in_digest, NULL)) {
327 printf("Couldn't parse byte string %s\n", argv[2]);
328 return CMD_RET_FAILURE;
331 rc = tpm_extend(index, in_digest, out_digest);
333 puts("PCR value after execution of the command:\n");
334 print_byte_string(out_digest, sizeof(out_digest));
337 return report_return_code(rc);
340 static int do_tpm_pcr_read(cmd_tbl_t *cmdtp, int flag,
341 int argc, char * const argv[])
343 uint32_t index, count, rc;
347 return CMD_RET_USAGE;
348 index = simple_strtoul(argv[1], NULL, 0);
349 data = (void *)simple_strtoul(argv[2], NULL, 0);
350 count = simple_strtoul(argv[3], NULL, 0);
352 rc = tpm_pcr_read(index, data, count);
354 puts("Named PCR content:\n");
355 print_byte_string(data, count);
358 return report_return_code(rc);
361 static int do_tpm_tsc_physical_presence(cmd_tbl_t *cmdtp, int flag,
362 int argc, char * const argv[])
367 return CMD_RET_USAGE;
368 presence = (uint16_t)simple_strtoul(argv[1], NULL, 0);
370 return report_return_code(tpm_tsc_physical_presence(presence));
373 static int do_tpm_read_pubek(cmd_tbl_t *cmdtp, int flag,
374 int argc, char * const argv[])
380 return CMD_RET_USAGE;
381 data = (void *)simple_strtoul(argv[1], NULL, 0);
382 count = simple_strtoul(argv[2], NULL, 0);
384 rc = tpm_read_pubek(data, count);
386 puts("pubek value:\n");
387 print_byte_string(data, count);
390 return report_return_code(rc);
393 static int do_tpm_physical_set_deactivated(cmd_tbl_t *cmdtp, int flag,
394 int argc, char * const argv[])
399 return CMD_RET_USAGE;
400 state = (uint8_t)simple_strtoul(argv[1], NULL, 0);
402 return report_return_code(tpm_physical_set_deactivated(state));
405 static int do_tpm_get_capability(cmd_tbl_t *cmdtp, int flag,
406 int argc, char * const argv[])
408 uint32_t cap_area, sub_cap, rc;
413 return CMD_RET_USAGE;
414 cap_area = simple_strtoul(argv[1], NULL, 0);
415 sub_cap = simple_strtoul(argv[2], NULL, 0);
416 cap = (void *)simple_strtoul(argv[3], NULL, 0);
417 count = simple_strtoul(argv[4], NULL, 0);
419 rc = tpm_get_capability(cap_area, sub_cap, cap, count);
421 puts("capability information:\n");
422 print_byte_string(cap, count);
425 return report_return_code(rc);
428 #define TPM_COMMAND_NO_ARG(cmd) \
429 static int do_##cmd(cmd_tbl_t *cmdtp, int flag, \
430 int argc, char * const argv[]) \
433 return CMD_RET_USAGE; \
434 return report_return_code(cmd()); \
437 TPM_COMMAND_NO_ARG(tpm_init)
438 TPM_COMMAND_NO_ARG(tpm_self_test_full)
439 TPM_COMMAND_NO_ARG(tpm_continue_self_test)
440 TPM_COMMAND_NO_ARG(tpm_force_clear)
441 TPM_COMMAND_NO_ARG(tpm_physical_enable)
442 TPM_COMMAND_NO_ARG(tpm_physical_disable)
445 static int get_tpm(struct udevice **devp)
449 rc = uclass_first_device(UCLASS_TPM, devp);
451 printf("Could not find TPM (ret=%d)\n", rc);
452 return CMD_RET_FAILURE;
459 static int do_tpm_raw_transfer(cmd_tbl_t *cmdtp, int flag,
460 int argc, char * const argv[])
463 uint8_t response[1024];
464 size_t count, response_length = sizeof(response);
467 command = parse_byte_string(argv[1], NULL, &count);
469 printf("Couldn't parse byte string %s\n", argv[1]);
470 return CMD_RET_FAILURE;
480 rc = tpm_xfer(dev, command, count, response, &response_length);
482 rc = tis_sendrecv(command, count, response, &response_length);
486 puts("tpm response:\n");
487 print_byte_string(response, response_length);
490 return report_return_code(rc);
493 static int do_tpm_nv_define(cmd_tbl_t *cmdtp, int flag,
494 int argc, char * const argv[])
496 uint32_t index, perm, size;
499 return CMD_RET_USAGE;
500 size = type_string_get_space_size(argv[1]);
502 printf("Couldn't parse arguments\n");
503 return CMD_RET_USAGE;
505 index = simple_strtoul(argv[2], NULL, 0);
506 perm = simple_strtoul(argv[3], NULL, 0);
508 return report_return_code(tpm_nv_define_space(index, perm, size));
511 static int do_tpm_nv_read(cmd_tbl_t *cmdtp, int flag,
512 int argc, char * const argv[])
514 uint32_t index, count, err;
518 return CMD_RET_USAGE;
519 if (argc != 3 + type_string_get_num_values(argv[1]))
520 return CMD_RET_USAGE;
521 index = simple_strtoul(argv[2], NULL, 0);
522 data = type_string_alloc(argv[1], &count);
524 printf("Couldn't parse arguments\n");
525 return CMD_RET_USAGE;
528 err = tpm_nv_read_value(index, data, count);
530 if (type_string_write_vars(argv[1], data, argv + 3)) {
531 printf("Couldn't write to variables\n");
537 return report_return_code(err);
540 static int do_tpm_nv_write(cmd_tbl_t *cmdtp, int flag,
541 int argc, char * const argv[])
543 uint32_t index, count, err;
547 return CMD_RET_USAGE;
548 if (argc != 3 + type_string_get_num_values(argv[1]))
549 return CMD_RET_USAGE;
550 index = simple_strtoul(argv[2], NULL, 0);
551 data = type_string_alloc(argv[1], &count);
553 printf("Couldn't parse arguments\n");
554 return CMD_RET_USAGE;
556 if (type_string_pack(argv[1], argv + 3, data)) {
557 printf("Couldn't parse arguments\n");
559 return CMD_RET_USAGE;
562 err = tpm_nv_write_value(index, data, count);
565 return report_return_code(err);
568 #ifdef CONFIG_TPM_AUTH_SESSIONS
570 static int do_tpm_oiap(cmd_tbl_t *cmdtp, int flag,
571 int argc, char * const argv[])
573 uint32_t auth_handle, err;
575 err = tpm_oiap(&auth_handle);
577 return report_return_code(err);
580 static int do_tpm_load_key2_oiap(cmd_tbl_t *cmdtp, int flag,
581 int argc, char * const argv[])
583 uint32_t parent_handle, key_len, key_handle, err;
584 uint8_t usage_auth[DIGEST_LENGTH];
588 return CMD_RET_USAGE;
590 parent_handle = simple_strtoul(argv[1], NULL, 0);
591 key = (void *)simple_strtoul(argv[2], NULL, 0);
592 key_len = simple_strtoul(argv[3], NULL, 0);
593 if (strlen(argv[4]) != 2 * DIGEST_LENGTH)
594 return CMD_RET_FAILURE;
595 parse_byte_string(argv[4], usage_auth, NULL);
597 err = tpm_load_key2_oiap(parent_handle, key, key_len, usage_auth,
600 printf("Key handle is 0x%x\n", key_handle);
602 return report_return_code(err);
605 static int do_tpm_get_pub_key_oiap(cmd_tbl_t *cmdtp, int flag,
606 int argc, char * const argv[])
608 uint32_t key_handle, err;
609 uint8_t usage_auth[DIGEST_LENGTH];
610 uint8_t pub_key_buffer[TPM_PUBKEY_MAX_LENGTH];
611 size_t pub_key_len = sizeof(pub_key_buffer);
614 return CMD_RET_USAGE;
616 key_handle = simple_strtoul(argv[1], NULL, 0);
617 if (strlen(argv[2]) != 2 * DIGEST_LENGTH)
618 return CMD_RET_FAILURE;
619 parse_byte_string(argv[2], usage_auth, NULL);
621 err = tpm_get_pub_key_oiap(key_handle, usage_auth,
622 pub_key_buffer, &pub_key_len);
624 printf("dump of received pub key structure:\n");
625 print_byte_string(pub_key_buffer, pub_key_len);
627 return report_return_code(err);
630 TPM_COMMAND_NO_ARG(tpm_end_oiap)
632 #endif /* CONFIG_TPM_AUTH_SESSIONS */
634 #define MAKE_TPM_CMD_ENTRY(cmd) \
635 U_BOOT_CMD_MKENT(cmd, 0, 1, do_tpm_ ## cmd, "", "")
637 static cmd_tbl_t tpm_commands[] = {
638 U_BOOT_CMD_MKENT(init, 0, 1,
639 do_tpm_init, "", ""),
640 U_BOOT_CMD_MKENT(startup, 0, 1,
641 do_tpm_startup, "", ""),
642 U_BOOT_CMD_MKENT(self_test_full, 0, 1,
643 do_tpm_self_test_full, "", ""),
644 U_BOOT_CMD_MKENT(continue_self_test, 0, 1,
645 do_tpm_continue_self_test, "", ""),
646 U_BOOT_CMD_MKENT(force_clear, 0, 1,
647 do_tpm_force_clear, "", ""),
648 U_BOOT_CMD_MKENT(physical_enable, 0, 1,
649 do_tpm_physical_enable, "", ""),
650 U_BOOT_CMD_MKENT(physical_disable, 0, 1,
651 do_tpm_physical_disable, "", ""),
652 U_BOOT_CMD_MKENT(nv_define_space, 0, 1,
653 do_tpm_nv_define_space, "", ""),
654 U_BOOT_CMD_MKENT(nv_read_value, 0, 1,
655 do_tpm_nv_read_value, "", ""),
656 U_BOOT_CMD_MKENT(nv_write_value, 0, 1,
657 do_tpm_nv_write_value, "", ""),
658 U_BOOT_CMD_MKENT(extend, 0, 1,
659 do_tpm_extend, "", ""),
660 U_BOOT_CMD_MKENT(pcr_read, 0, 1,
661 do_tpm_pcr_read, "", ""),
662 U_BOOT_CMD_MKENT(tsc_physical_presence, 0, 1,
663 do_tpm_tsc_physical_presence, "", ""),
664 U_BOOT_CMD_MKENT(read_pubek, 0, 1,
665 do_tpm_read_pubek, "", ""),
666 U_BOOT_CMD_MKENT(physical_set_deactivated, 0, 1,
667 do_tpm_physical_set_deactivated, "", ""),
668 U_BOOT_CMD_MKENT(get_capability, 0, 1,
669 do_tpm_get_capability, "", ""),
670 U_BOOT_CMD_MKENT(raw_transfer, 0, 1,
671 do_tpm_raw_transfer, "", ""),
672 U_BOOT_CMD_MKENT(nv_define, 0, 1,
673 do_tpm_nv_define, "", ""),
674 U_BOOT_CMD_MKENT(nv_read, 0, 1,
675 do_tpm_nv_read, "", ""),
676 U_BOOT_CMD_MKENT(nv_write, 0, 1,
677 do_tpm_nv_write, "", ""),
678 #ifdef CONFIG_TPM_AUTH_SESSIONS
679 U_BOOT_CMD_MKENT(oiap, 0, 1,
680 do_tpm_oiap, "", ""),
681 U_BOOT_CMD_MKENT(end_oiap, 0, 1,
682 do_tpm_end_oiap, "", ""),
683 U_BOOT_CMD_MKENT(load_key2_oiap, 0, 1,
684 do_tpm_load_key2_oiap, "", ""),
685 U_BOOT_CMD_MKENT(get_pub_key_oiap, 0, 1,
686 do_tpm_get_pub_key_oiap, "", ""),
687 #endif /* CONFIG_TPM_AUTH_SESSIONS */
690 static int do_tpm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
695 return CMD_RET_USAGE;
696 tpm_cmd = find_cmd_tbl(argv[1], tpm_commands, ARRAY_SIZE(tpm_commands));
698 return CMD_RET_USAGE;
700 return tpm_cmd->cmd(cmdtp, flag, argc - 1, argv + 1);
703 U_BOOT_CMD(tpm, CONFIG_SYS_MAXARGS, 1, do_tpm,
704 "Issue a TPM command",
706 " - Issue TPM command <cmd> with arguments <args...>.\n"
707 "Admin Startup and State Commands:\n"
709 " - Put TPM into a state where it waits for 'startup' command.\n"
711 " - Issue TPM_Starup command. <mode> is one of TPM_ST_CLEAR,\n"
712 " TPM_ST_STATE, and TPM_ST_DEACTIVATED.\n"
713 "Admin Testing Commands:\n"
715 " - Test all of the TPM capabilities.\n"
716 " continue_self_test\n"
717 " - Inform TPM that it should complete the self-test.\n"
718 "Admin Opt-in Commands:\n"
720 " - Set the PERMANENT disable flag to FALSE using physical presence as\n"
722 " physical_disable\n"
723 " - Set the PERMANENT disable flag to TRUE using physical presence as\n"
725 " physical_set_deactivated 0|1\n"
726 " - Set deactivated flag.\n"
727 "Admin Ownership Commands:\n"
729 " - Issue TPM_ForceClear command.\n"
730 " tsc_physical_presence flags\n"
731 " - Set TPM device's Physical Presence flags to <flags>.\n"
732 "The Capability Commands:\n"
733 " get_capability cap_area sub_cap addr count\n"
734 " - Read <count> bytes of TPM capability indexed by <cap_area> and\n"
735 " <sub_cap> to memory address <addr>.\n"
736 #ifdef CONFIG_TPM_AUTH_SESSIONS
737 "Storage functions\n"
738 " loadkey2_oiap parent_handle key_addr key_len usage_auth\n"
739 " - loads a key data from memory address <key_addr>, <key_len> bytes\n"
740 " into TPM using the parent key <parent_handle> with authorization\n"
741 " <usage_auth> (20 bytes hex string).\n"
742 " get_pub_key_oiap key_handle usage_auth\n"
743 " - get the public key portion of a loaded key <key_handle> using\n"
744 " authorization <usage auth> (20 bytes hex string)\n"
745 #endif /* CONFIG_TPM_AUTH_SESSIONS */
746 "Endorsement Key Handling Commands:\n"
747 " read_pubek addr count\n"
748 " - Read <count> bytes of the public endorsement key to memory\n"
750 "Integrity Collection and Reporting Commands:\n"
751 " extend index digest_hex_string\n"
752 " - Add a new measurement to a PCR. Update PCR <index> with the 20-bytes\n"
753 " <digest_hex_string>\n"
754 " pcr_read index addr count\n"
755 " - Read <count> bytes from PCR <index> to memory address <addr>.\n"
756 #ifdef CONFIG_TPM_AUTH_SESSIONS
757 "Authorization Sessions\n"
759 " - setup an OIAP session\n"
761 " - terminates an active OIAP session\n"
762 #endif /* CONFIG_TPM_AUTH_SESSIONS */
763 "Non-volatile Storage Commands:\n"
764 " nv_define_space index permission size\n"
765 " - Establish a space at index <index> with <permission> of <size> bytes.\n"
766 " nv_read_value index addr count\n"
767 " - Read <count> bytes from space <index> to memory address <addr>.\n"
768 " nv_write_value index addr count\n"
769 " - Write <count> bytes from memory address <addr> to space <index>.\n"
770 "Miscellaneous helper functions:\n"
771 " raw_transfer byte_string\n"
772 " - Send a byte string <byte_string> to TPM and print the response.\n"
773 " Non-volatile storage helper functions:\n"
774 " These helper functions treat a non-volatile space as a non-padded\n"
775 " sequence of integer values. These integer values are defined by a type\n"
776 " string, which is a text string of 'bwd' characters: 'b' means a 8-bit\n"
777 " value, 'w' 16-bit value, 'd' 32-bit value. All helper functions take\n"
778 " a type string as their first argument.\n"
779 " nv_define type_string index perm\n"
780 " - Define a space <index> with permission <perm>.\n"
781 " nv_read types_string index vars...\n"
782 " - Read from space <index> to environment variables <vars...>.\n"
783 " nv_write types_string index values...\n"
784 " - Write to space <index> from values <values...>.\n"