From d677bfe2f7914367d1caa6146b34e86d0df1c75d Mon Sep 17 00:00:00 2001 From: Miquel Raynal Date: Tue, 15 May 2018 11:57:06 +0200 Subject: tpm: disociate TPMv1.x specific and generic code There are no changes in this commit but a new organization of the code as follow. * cmd/ directory: > move existing code from cmd/tpm.c in cmd/tpm-common.c > move specific code in cmd/tpm-v1.c > create a specific header file with generic definitions for commands only called cmd/tpm-user-utils.h * lib/ directory: > move existing code from lib/tpm.c in lib/tpm-common.c > move specific code in lib/tpm-v1.c > create a specific header file with generic definitions for the library itself called lib/tpm-utils.h * include/ directory: > move existing code from include/tpm.h in include/tpm-common.h > move specific code in include/tpm-v1.h Code designated as 'common' is compiled if TPM are used. Code designated as 'specific' is compiled only if the right specification has been selected. All files include tpm-common.h. Files in cmd/ include tpm-user-utils.h. Files in lib/ include tpm-utils.h. Depending on the specification, files may include either (not both) tpm-v1.h or tpm-v2.h. Signed-off-by: Miquel Raynal Reviewed-by: Tom Rini [trini: Fix a few more cases of tpm.h -> tpm-v1.h, some Kconfig logic] Signed-off-by: Tom Rini --- cmd/Makefile | 3 +- cmd/tpm-common.c | 288 +++++++++++++++ cmd/tpm-user-utils.h | 24 ++ cmd/tpm-v1.c | 723 ++++++++++++++++++++++++++++++++++++ cmd/tpm.c | 1001 -------------------------------------------------- cmd/tpm_test.c | 2 +- 6 files changed, 1038 insertions(+), 1003 deletions(-) create mode 100644 cmd/tpm-common.c create mode 100644 cmd/tpm-user-utils.h create mode 100644 cmd/tpm-v1.c delete mode 100644 cmd/tpm.c (limited to 'cmd') diff --git a/cmd/Makefile b/cmd/Makefile index ca6ead8e8e8..6164f6e611f 100644 --- a/cmd/Makefile +++ b/cmd/Makefile @@ -120,7 +120,8 @@ obj-$(CONFIG_CMD_TERMINAL) += terminal.o obj-$(CONFIG_CMD_TIME) += time.o obj-$(CONFIG_CMD_TRACE) += trace.o obj-$(CONFIG_HUSH_PARSER) += test.o -obj-$(CONFIG_CMD_TPM_V1) += tpm.o +obj-$(CONFIG_CMD_TPM) += tpm-common.o +obj-$(CONFIG_CMD_TPM_V1) += tpm-v1.o obj-$(CONFIG_CMD_TPM_TEST) += tpm_test.o obj-$(CONFIG_CMD_CROS_EC) += cros_ec.o obj-$(CONFIG_CMD_TSI148) += tsi148.o diff --git a/cmd/tpm-common.c b/cmd/tpm-common.c new file mode 100644 index 00000000000..6cf9fcc9ac8 --- /dev/null +++ b/cmd/tpm-common.c @@ -0,0 +1,288 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * Copyright (c) 2013 The Chromium OS Authors. + */ + +#include +#include +#include +#include +#include +#include +#include "tpm-user-utils.h" + +/** + * Print a byte string in hexdecimal format, 16-bytes per line. + * + * @param data byte string to be printed + * @param count number of bytes to be printed + */ +void print_byte_string(u8 *data, size_t count) +{ + int i, print_newline = 0; + + for (i = 0; i < count; i++) { + printf(" %02x", data[i]); + print_newline = (i % 16 == 15); + if (print_newline) + putc('\n'); + } + /* Avoid duplicated newline at the end */ + if (!print_newline) + putc('\n'); +} + +/** + * Convert a text string of hexdecimal values into a byte string. + * + * @param bytes text string of hexdecimal values with no space + * between them + * @param data output buffer for byte string. The caller has to make + * sure it is large enough for storing the output. If + * NULL is passed, a large enough buffer will be allocated, + * and the caller must free it. + * @param count_ptr output variable for the length of byte string + * @return pointer to output buffer + */ +void *parse_byte_string(char *bytes, u8 *data, size_t *count_ptr) +{ + char byte[3]; + size_t count, length; + int i; + + if (!bytes) + return NULL; + length = strlen(bytes); + count = length / 2; + + if (!data) + data = malloc(count); + if (!data) + return NULL; + + byte[2] = '\0'; + for (i = 0; i < length; i += 2) { + byte[0] = bytes[i]; + byte[1] = bytes[i + 1]; + data[i / 2] = (u8)simple_strtoul(byte, NULL, 16); + } + + if (count_ptr) + *count_ptr = count; + + return data; +} + +/** + * report_return_code() - Report any error and return failure or success + * + * @param return_code TPM command return code + * @return value of enum command_ret_t + */ +int report_return_code(int return_code) +{ + if (return_code) { + printf("Error: %d\n", return_code); + return CMD_RET_FAILURE; + } else { + return CMD_RET_SUCCESS; + } +} + +/** + * Return number of values defined by a type string. + * + * @param type_str type string + * @return number of values of type string + */ +int type_string_get_num_values(const char *type_str) +{ + return strlen(type_str); +} + +/** + * Return total size of values defined by a type string. + * + * @param type_str type string + * @return total size of values of type string, or 0 if type string + * contains illegal type character. + */ +size_t type_string_get_space_size(const char *type_str) +{ + size_t size; + + for (size = 0; *type_str; type_str++) { + switch (*type_str) { + case 'b': + size += 1; + break; + case 'w': + size += 2; + break; + case 'd': + size += 4; + break; + default: + return 0; + } + } + + return size; +} + +/** + * Allocate a buffer large enough to hold values defined by a type + * string. The caller has to free the buffer. + * + * @param type_str type string + * @param count pointer for storing size of buffer + * @return pointer to buffer or NULL on error + */ +void *type_string_alloc(const char *type_str, u32 *count) +{ + void *data; + size_t size; + + size = type_string_get_space_size(type_str); + if (!size) + return NULL; + data = malloc(size); + if (data) + *count = size; + + return data; +} + +/** + * Pack values defined by a type string into a buffer. The buffer must have + * large enough space. + * + * @param type_str type string + * @param values text strings of values to be packed + * @param data output buffer of values + * @return 0 on success, non-0 on error + */ +int type_string_pack(const char *type_str, char * const values[], + u8 *data) +{ + size_t offset; + u32 value; + + for (offset = 0; *type_str; type_str++, values++) { + value = simple_strtoul(values[0], NULL, 0); + switch (*type_str) { + case 'b': + data[offset] = value; + offset += 1; + break; + case 'w': + put_unaligned_be16(value, data + offset); + offset += 2; + break; + case 'd': + put_unaligned_be32(value, data + offset); + offset += 4; + break; + default: + return -1; + } + } + + return 0; +} + +/** + * Read values defined by a type string from a buffer, and write these values + * to environment variables. + * + * @param type_str type string + * @param data input buffer of values + * @param vars names of environment variables + * @return 0 on success, non-0 on error + */ +int type_string_write_vars(const char *type_str, u8 *data, + char * const vars[]) +{ + size_t offset; + u32 value; + + for (offset = 0; *type_str; type_str++, vars++) { + switch (*type_str) { + case 'b': + value = data[offset]; + offset += 1; + break; + case 'w': + value = get_unaligned_be16(data + offset); + offset += 2; + break; + case 'd': + value = get_unaligned_be32(data + offset); + offset += 4; + break; + default: + return -1; + } + if (env_set_ulong(*vars, value)) + return -1; + } + + return 0; +} + +int get_tpm(struct udevice **devp) +{ + int rc; + + rc = uclass_first_device_err(UCLASS_TPM, devp); + if (rc) { + printf("Could not find TPM (ret=%d)\n", rc); + return CMD_RET_FAILURE; + } + + return 0; +} + +int do_tpm_info(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[]) +{ + struct udevice *dev; + char buf[80]; + int rc; + + rc = get_tpm(&dev); + if (rc) + return rc; + rc = tpm_get_desc(dev, buf, sizeof(buf)); + if (rc < 0) { + printf("Couldn't get TPM info (%d)\n", rc); + return CMD_RET_FAILURE; + } + printf("%s\n", buf); + + return 0; +} + +int do_tpm_init(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) +{ + if (argc != 1) + return CMD_RET_USAGE; + + return report_return_code(tpm_init()); +} + +int do_tpm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) +{ + cmd_tbl_t *tpm_commands, *cmd; + unsigned int size; + + if (argc < 2) + return CMD_RET_USAGE; + + tpm_commands = get_tpm_commands(&size); + + cmd = find_cmd_tbl(argv[1], tpm_commands, size); + if (!cmd) + return CMD_RET_USAGE; + + return cmd->cmd(cmdtp, flag, argc - 1, argv + 1); +} diff --git a/cmd/tpm-user-utils.h b/cmd/tpm-user-utils.h new file mode 100644 index 00000000000..8ce98617843 --- /dev/null +++ b/cmd/tpm-user-utils.h @@ -0,0 +1,24 @@ +/* SPDX-License-Identifier: GPL-2.0+ */ +/* + * Copyright (c) 2013 The Chromium OS Authors. + * Coypright (c) 2013 Guntermann & Drunck GmbH + */ + +#ifndef __TPM_USER_UTILS_H +#define __TPM_USER_UTILS_H + +void print_byte_string(u8 *data, size_t count); +void *parse_byte_string(char *bytes, u8 *data, size_t *count_ptr); +int report_return_code(int return_code); +int type_string_get_num_values(const char *type_str); +size_t type_string_get_space_size(const char *type_str); +void *type_string_alloc(const char *type_str, u32 *count); +int type_string_pack(const char *type_str, char * const values[], u8 *data); +int type_string_write_vars(const char *type_str, u8 *data, char * const vars[]); +int get_tpm(struct udevice **devp); + +int do_tpm_init(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[]); +int do_tpm_info(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[]); +int do_tpm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]); + +#endif /* __TPM_USER_UTILS_H */ diff --git a/cmd/tpm-v1.c b/cmd/tpm-v1.c new file mode 100644 index 00000000000..0874c4d7baf --- /dev/null +++ b/cmd/tpm-v1.c @@ -0,0 +1,723 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * Copyright (c) 2013 The Chromium OS Authors. + */ + +#include +#include +#include +#include +#include +#include "tpm-user-utils.h" + +static int do_tpm_startup(cmd_tbl_t *cmdtp, int flag, int argc, + char * const argv[]) +{ + enum tpm_startup_type mode; + + if (argc != 2) + return CMD_RET_USAGE; + if (!strcasecmp("TPM_ST_CLEAR", argv[1])) { + mode = TPM_ST_CLEAR; + } else if (!strcasecmp("TPM_ST_STATE", argv[1])) { + mode = TPM_ST_STATE; + } else if (!strcasecmp("TPM_ST_DEACTIVATED", argv[1])) { + mode = TPM_ST_DEACTIVATED; + } else { + printf("Couldn't recognize mode string: %s\n", argv[1]); + return CMD_RET_FAILURE; + } + + return report_return_code(tpm_startup(mode)); +} + +static int do_tpm_nv_define_space(cmd_tbl_t *cmdtp, int flag, int argc, + char * const argv[]) +{ + u32 index, perm, size; + + if (argc != 4) + return CMD_RET_USAGE; + index = simple_strtoul(argv[1], NULL, 0); + perm = simple_strtoul(argv[2], NULL, 0); + size = simple_strtoul(argv[3], NULL, 0); + + return report_return_code(tpm_nv_define_space(index, perm, size)); +} + +static int do_tpm_nv_read_value(cmd_tbl_t *cmdtp, int flag, int argc, + char * const argv[]) +{ + u32 index, count, rc; + void *data; + + if (argc != 4) + return CMD_RET_USAGE; + index = simple_strtoul(argv[1], NULL, 0); + data = (void *)simple_strtoul(argv[2], NULL, 0); + count = simple_strtoul(argv[3], NULL, 0); + + rc = tpm_nv_read_value(index, data, count); + if (!rc) { + puts("area content:\n"); + print_byte_string(data, count); + } + + return report_return_code(rc); +} + +static int do_tpm_nv_write_value(cmd_tbl_t *cmdtp, int flag, int argc, + char * const argv[]) +{ + u32 index, rc; + size_t count; + void *data; + + if (argc != 3) + return CMD_RET_USAGE; + index = simple_strtoul(argv[1], NULL, 0); + data = parse_byte_string(argv[2], NULL, &count); + if (!data) { + printf("Couldn't parse byte string %s\n", argv[2]); + return CMD_RET_FAILURE; + } + + rc = tpm_nv_write_value(index, data, count); + free(data); + + return report_return_code(rc); +} + +static int do_tpm_extend(cmd_tbl_t *cmdtp, int flag, int argc, + char * const argv[]) +{ + u32 index, rc; + u8 in_digest[20], out_digest[20]; + + if (argc != 3) + return CMD_RET_USAGE; + index = simple_strtoul(argv[1], NULL, 0); + if (!parse_byte_string(argv[2], in_digest, NULL)) { + printf("Couldn't parse byte string %s\n", argv[2]); + return CMD_RET_FAILURE; + } + + rc = tpm_extend(index, in_digest, out_digest); + if (!rc) { + puts("PCR value after execution of the command:\n"); + print_byte_string(out_digest, sizeof(out_digest)); + } + + return report_return_code(rc); +} + +static int do_tpm_pcr_read(cmd_tbl_t *cmdtp, int flag, int argc, + char * const argv[]) +{ + u32 index, count, rc; + void *data; + + if (argc != 4) + return CMD_RET_USAGE; + index = simple_strtoul(argv[1], NULL, 0); + data = (void *)simple_strtoul(argv[2], NULL, 0); + count = simple_strtoul(argv[3], NULL, 0); + + rc = tpm_pcr_read(index, data, count); + if (!rc) { + puts("Named PCR content:\n"); + print_byte_string(data, count); + } + + return report_return_code(rc); +} + +static int do_tpm_tsc_physical_presence(cmd_tbl_t *cmdtp, int flag, int argc, + char * const argv[]) +{ + u16 presence; + + if (argc != 2) + return CMD_RET_USAGE; + presence = (u16)simple_strtoul(argv[1], NULL, 0); + + return report_return_code(tpm_tsc_physical_presence(presence)); +} + +static int do_tpm_read_pubek(cmd_tbl_t *cmdtp, int flag, int argc, + char * const argv[]) +{ + u32 count, rc; + void *data; + + if (argc != 3) + return CMD_RET_USAGE; + data = (void *)simple_strtoul(argv[1], NULL, 0); + count = simple_strtoul(argv[2], NULL, 0); + + rc = tpm_read_pubek(data, count); + if (!rc) { + puts("pubek value:\n"); + print_byte_string(data, count); + } + + return report_return_code(rc); +} + +static int do_tpm_physical_set_deactivated(cmd_tbl_t *cmdtp, int flag, int argc, + char * const argv[]) +{ + u8 state; + + if (argc != 2) + return CMD_RET_USAGE; + state = (u8)simple_strtoul(argv[1], NULL, 0); + + return report_return_code(tpm_physical_set_deactivated(state)); +} + +static int do_tpm_get_capability(cmd_tbl_t *cmdtp, int flag, int argc, + char * const argv[]) +{ + u32 cap_area, sub_cap, rc; + void *cap; + size_t count; + + if (argc != 5) + return CMD_RET_USAGE; + cap_area = simple_strtoul(argv[1], NULL, 0); + sub_cap = simple_strtoul(argv[2], NULL, 0); + cap = (void *)simple_strtoul(argv[3], NULL, 0); + count = simple_strtoul(argv[4], NULL, 0); + + rc = tpm_get_capability(cap_area, sub_cap, cap, count); + if (!rc) { + puts("capability information:\n"); + print_byte_string(cap, count); + } + + return report_return_code(rc); +} + +static int do_tpm_raw_transfer(cmd_tbl_t *cmdtp, int flag, int argc, + char * const argv[]) +{ + struct udevice *dev; + void *command; + u8 response[1024]; + size_t count, response_length = sizeof(response); + u32 rc; + + command = parse_byte_string(argv[1], NULL, &count); + if (!command) { + printf("Couldn't parse byte string %s\n", argv[1]); + return CMD_RET_FAILURE; + } + + rc = get_tpm(&dev); + if (rc) + return rc; + + rc = tpm_xfer(dev, command, count, response, &response_length); + free(command); + if (!rc) { + puts("tpm response:\n"); + print_byte_string(response, response_length); + } + + return report_return_code(rc); +} + +static int do_tpm_nv_define(cmd_tbl_t *cmdtp, int flag, int argc, + char * const argv[]) +{ + u32 index, perm, size; + + if (argc != 4) + return CMD_RET_USAGE; + size = type_string_get_space_size(argv[1]); + if (!size) { + printf("Couldn't parse arguments\n"); + return CMD_RET_USAGE; + } + index = simple_strtoul(argv[2], NULL, 0); + perm = simple_strtoul(argv[3], NULL, 0); + + return report_return_code(tpm_nv_define_space(index, perm, size)); +} + +static int do_tpm_nv_read(cmd_tbl_t *cmdtp, int flag, int argc, + char * const argv[]) +{ + u32 index, count, err; + void *data; + + if (argc < 3) + return CMD_RET_USAGE; + if (argc != 3 + type_string_get_num_values(argv[1])) + return CMD_RET_USAGE; + index = simple_strtoul(argv[2], NULL, 0); + data = type_string_alloc(argv[1], &count); + if (!data) { + printf("Couldn't parse arguments\n"); + return CMD_RET_USAGE; + } + + err = tpm_nv_read_value(index, data, count); + if (!err) { + if (type_string_write_vars(argv[1], data, argv + 3)) { + printf("Couldn't write to variables\n"); + err = ~0; + } + } + free(data); + + return report_return_code(err); +} + +static int do_tpm_nv_write(cmd_tbl_t *cmdtp, int flag, int argc, + char * const argv[]) +{ + u32 index, count, err; + void *data; + + if (argc < 3) + return CMD_RET_USAGE; + if (argc != 3 + type_string_get_num_values(argv[1])) + return CMD_RET_USAGE; + index = simple_strtoul(argv[2], NULL, 0); + data = type_string_alloc(argv[1], &count); + if (!data) { + printf("Couldn't parse arguments\n"); + return CMD_RET_USAGE; + } + if (type_string_pack(argv[1], argv + 3, data)) { + printf("Couldn't parse arguments\n"); + free(data); + return CMD_RET_USAGE; + } + + err = tpm_nv_write_value(index, data, count); + free(data); + + return report_return_code(err); +} + +#ifdef CONFIG_TPM_AUTH_SESSIONS + +static int do_tpm_oiap(cmd_tbl_t *cmdtp, int flag, int argc, + char * const argv[]) +{ + u32 auth_handle, err; + + err = tpm_oiap(&auth_handle); + + return report_return_code(err); +} + +#ifdef CONFIG_TPM_LOAD_KEY_BY_SHA1 +static int do_tpm_load_key_by_sha1(cmd_tbl_t *cmdtp, int flag, int argc, char * + const argv[]) +{ + u32 parent_handle = 0; + u32 key_len, key_handle, err; + u8 usage_auth[DIGEST_LENGTH]; + u8 parent_hash[DIGEST_LENGTH]; + void *key; + + if (argc < 5) + return CMD_RET_USAGE; + + parse_byte_string(argv[1], parent_hash, NULL); + key = (void *)simple_strtoul(argv[2], NULL, 0); + key_len = simple_strtoul(argv[3], NULL, 0); + if (strlen(argv[4]) != 2 * DIGEST_LENGTH) + return CMD_RET_FAILURE; + parse_byte_string(argv[4], usage_auth, NULL); + + err = tpm_find_key_sha1(usage_auth, parent_hash, &parent_handle); + if (err) { + printf("Could not find matching parent key (err = %d)\n", err); + return CMD_RET_FAILURE; + } + + printf("Found parent key %08x\n", parent_handle); + + err = tpm_load_key2_oiap(parent_handle, key, key_len, usage_auth, + &key_handle); + if (!err) { + printf("Key handle is 0x%x\n", key_handle); + env_set_hex("key_handle", key_handle); + } + + return report_return_code(err); +} +#endif /* CONFIG_TPM_LOAD_KEY_BY_SHA1 */ + +static int do_tpm_load_key2_oiap(cmd_tbl_t *cmdtp, int flag, int argc, + char * const argv[]) +{ + u32 parent_handle, key_len, key_handle, err; + u8 usage_auth[DIGEST_LENGTH]; + void *key; + + if (argc < 5) + return CMD_RET_USAGE; + + parent_handle = simple_strtoul(argv[1], NULL, 0); + key = (void *)simple_strtoul(argv[2], NULL, 0); + key_len = simple_strtoul(argv[3], NULL, 0); + if (strlen(argv[4]) != 2 * DIGEST_LENGTH) + return CMD_RET_FAILURE; + parse_byte_string(argv[4], usage_auth, NULL); + + err = tpm_load_key2_oiap(parent_handle, key, key_len, usage_auth, + &key_handle); + if (!err) + printf("Key handle is 0x%x\n", key_handle); + + return report_return_code(err); +} + +static int do_tpm_get_pub_key_oiap(cmd_tbl_t *cmdtp, int flag, int argc, + char * const argv[]) +{ + u32 key_handle, err; + u8 usage_auth[DIGEST_LENGTH]; + u8 pub_key_buffer[TPM_PUBKEY_MAX_LENGTH]; + size_t pub_key_len = sizeof(pub_key_buffer); + + if (argc < 3) + return CMD_RET_USAGE; + + key_handle = simple_strtoul(argv[1], NULL, 0); + if (strlen(argv[2]) != 2 * DIGEST_LENGTH) + return CMD_RET_FAILURE; + parse_byte_string(argv[2], usage_auth, NULL); + + err = tpm_get_pub_key_oiap(key_handle, usage_auth, pub_key_buffer, + &pub_key_len); + if (!err) { + printf("dump of received pub key structure:\n"); + print_byte_string(pub_key_buffer, pub_key_len); + } + return report_return_code(err); +} + +TPM_COMMAND_NO_ARG(tpm_end_oiap) + +#endif /* CONFIG_TPM_AUTH_SESSIONS */ + +#ifdef CONFIG_TPM_FLUSH_RESOURCES +static int do_tpm_flush(cmd_tbl_t *cmdtp, int flag, int argc, + char * const argv[]) +{ + int type = 0; + + if (argc != 3) + return CMD_RET_USAGE; + + if (!strcasecmp(argv[1], "key")) + type = TPM_RT_KEY; + else if (!strcasecmp(argv[1], "auth")) + type = TPM_RT_AUTH; + else if (!strcasecmp(argv[1], "hash")) + type = TPM_RT_HASH; + else if (!strcasecmp(argv[1], "trans")) + type = TPM_RT_TRANS; + else if (!strcasecmp(argv[1], "context")) + type = TPM_RT_CONTEXT; + else if (!strcasecmp(argv[1], "counter")) + type = TPM_RT_COUNTER; + else if (!strcasecmp(argv[1], "delegate")) + type = TPM_RT_DELEGATE; + else if (!strcasecmp(argv[1], "daa_tpm")) + type = TPM_RT_DAA_TPM; + else if (!strcasecmp(argv[1], "daa_v0")) + type = TPM_RT_DAA_V0; + else if (!strcasecmp(argv[1], "daa_v1")) + type = TPM_RT_DAA_V1; + + if (!type) { + printf("Resource type %s unknown.\n", argv[1]); + return -1; + } + + if (!strcasecmp(argv[2], "all")) { + u16 res_count; + u8 buf[288]; + u8 *ptr; + int err; + uint i; + + /* fetch list of already loaded resources in the TPM */ + err = tpm_get_capability(TPM_CAP_HANDLE, type, buf, + sizeof(buf)); + if (err) { + printf("tpm_get_capability returned error %d.\n", err); + return -1; + } + res_count = get_unaligned_be16(buf); + ptr = buf + 2; + for (i = 0; i < res_count; ++i, ptr += 4) + tpm_flush_specific(get_unaligned_be32(ptr), type); + } else { + u32 handle = simple_strtoul(argv[2], NULL, 0); + + if (!handle) { + printf("Illegal resource handle %s\n", argv[2]); + return -1; + } + tpm_flush_specific(cpu_to_be32(handle), type); + } + + return 0; +} +#endif /* CONFIG_TPM_FLUSH_RESOURCES */ + +#ifdef CONFIG_TPM_LIST_RESOURCES +static int do_tpm_list(cmd_tbl_t *cmdtp, int flag, int argc, + char * const argv[]) +{ + int type = 0; + u16 res_count; + u8 buf[288]; + u8 *ptr; + int err; + uint i; + + if (argc != 2) + return CMD_RET_USAGE; + + if (!strcasecmp(argv[1], "key")) + type = TPM_RT_KEY; + else if (!strcasecmp(argv[1], "auth")) + type = TPM_RT_AUTH; + else if (!strcasecmp(argv[1], "hash")) + type = TPM_RT_HASH; + else if (!strcasecmp(argv[1], "trans")) + type = TPM_RT_TRANS; + else if (!strcasecmp(argv[1], "context")) + type = TPM_RT_CONTEXT; + else if (!strcasecmp(argv[1], "counter")) + type = TPM_RT_COUNTER; + else if (!strcasecmp(argv[1], "delegate")) + type = TPM_RT_DELEGATE; + else if (!strcasecmp(argv[1], "daa_tpm")) + type = TPM_RT_DAA_TPM; + else if (!strcasecmp(argv[1], "daa_v0")) + type = TPM_RT_DAA_V0; + else if (!strcasecmp(argv[1], "daa_v1")) + type = TPM_RT_DAA_V1; + + if (!type) { + printf("Resource type %s unknown.\n", argv[1]); + return -1; + } + + /* fetch list of already loaded resources in the TPM */ + err = tpm_get_capability(TPM_CAP_HANDLE, type, buf, + sizeof(buf)); + if (err) { + printf("tpm_get_capability returned error %d.\n", err); + return -1; + } + res_count = get_unaligned_be16(buf); + ptr = buf + 2; + + printf("Resources of type %s (%02x):\n", argv[1], type); + if (!res_count) { + puts("None\n"); + } else { + for (i = 0; i < res_count; ++i, ptr += 4) + printf("Index %d: %08x\n", i, get_unaligned_be32(ptr)); + } + + return 0; +} +#endif /* CONFIG_TPM_LIST_RESOURCES */ + +TPM_COMMAND_NO_ARG(tpm_self_test_full) +TPM_COMMAND_NO_ARG(tpm_continue_self_test) +TPM_COMMAND_NO_ARG(tpm_force_clear) +TPM_COMMAND_NO_ARG(tpm_physical_enable) +TPM_COMMAND_NO_ARG(tpm_physical_disable) + +static cmd_tbl_t tpm1_commands[] = { + U_BOOT_CMD_MKENT(info, 0, 1, do_tpm_info, "", ""), + U_BOOT_CMD_MKENT(init, 0, 1, do_tpm_init, "", ""), + U_BOOT_CMD_MKENT(startup, 0, 1, + do_tpm_startup, "", ""), + U_BOOT_CMD_MKENT(self_test_full, 0, 1, + do_tpm_self_test_full, "", ""), + U_BOOT_CMD_MKENT(continue_self_test, 0, 1, + do_tpm_continue_self_test, "", ""), + U_BOOT_CMD_MKENT(force_clear, 0, 1, + do_tpm_force_clear, "", ""), + U_BOOT_CMD_MKENT(physical_enable, 0, 1, + do_tpm_physical_enable, "", ""), + U_BOOT_CMD_MKENT(physical_disable, 0, 1, + do_tpm_physical_disable, "", ""), + U_BOOT_CMD_MKENT(nv_define_space, 0, 1, + do_tpm_nv_define_space, "", ""), + U_BOOT_CMD_MKENT(nv_read_value, 0, 1, + do_tpm_nv_read_value, "", ""), + U_BOOT_CMD_MKENT(nv_write_value, 0, 1, + do_tpm_nv_write_value, "", ""), + U_BOOT_CMD_MKENT(extend, 0, 1, + do_tpm_extend, "", ""), + U_BOOT_CMD_MKENT(pcr_read, 0, 1, + do_tpm_pcr_read, "", ""), + U_BOOT_CMD_MKENT(tsc_physical_presence, 0, 1, + do_tpm_tsc_physical_presence, "", ""), + U_BOOT_CMD_MKENT(read_pubek, 0, 1, + do_tpm_read_pubek, "", ""), + U_BOOT_CMD_MKENT(physical_set_deactivated, 0, 1, + do_tpm_physical_set_deactivated, "", ""), + U_BOOT_CMD_MKENT(get_capability, 0, 1, + do_tpm_get_capability, "", ""), + U_BOOT_CMD_MKENT(raw_transfer, 0, 1, + do_tpm_raw_transfer, "", ""), + U_BOOT_CMD_MKENT(nv_define, 0, 1, + do_tpm_nv_define, "", ""), + U_BOOT_CMD_MKENT(nv_read, 0, 1, + do_tpm_nv_read, "", ""), + U_BOOT_CMD_MKENT(nv_write, 0, 1, + do_tpm_nv_write, "", ""), +#ifdef CONFIG_TPM_AUTH_SESSIONS + U_BOOT_CMD_MKENT(oiap, 0, 1, + do_tpm_oiap, "", ""), + U_BOOT_CMD_MKENT(end_oiap, 0, 1, + do_tpm_end_oiap, "", ""), + U_BOOT_CMD_MKENT(load_key2_oiap, 0, 1, + do_tpm_load_key2_oiap, "", ""), +#ifdef CONFIG_TPM_LOAD_KEY_BY_SHA1 + U_BOOT_CMD_MKENT(load_key_by_sha1, 0, 1, + do_tpm_load_key_by_sha1, "", ""), +#endif /* CONFIG_TPM_LOAD_KEY_BY_SHA1 */ + U_BOOT_CMD_MKENT(get_pub_key_oiap, 0, 1, + do_tpm_get_pub_key_oiap, "", ""), +#endif /* CONFIG_TPM_AUTH_SESSIONS */ +#ifdef CONFIG_TPM_FLUSH_RESOURCES + U_BOOT_CMD_MKENT(flush, 0, 1, + do_tpm_flush, "", ""), +#endif /* CONFIG_TPM_FLUSH_RESOURCES */ +#ifdef CONFIG_TPM_LIST_RESOURCES + U_BOOT_CMD_MKENT(list, 0, 1, + do_tpm_list, "", ""), +#endif /* CONFIG_TPM_LIST_RESOURCES */ +}; + +cmd_tbl_t *get_tpm_commands(unsigned int *size) +{ + *size = ARRAY_SIZE(tpm1_commands); + + return tpm1_commands; +} + +U_BOOT_CMD(tpm, CONFIG_SYS_MAXARGS, 1, do_tpm, +"Issue a TPMv1.x command", +"cmd args...\n" +" - Issue TPM command with arguments .\n" +"Admin Startup and State Commands:\n" +" info - Show information about the TPM\n" +" init\n" +" - Put TPM into a state where it waits for 'startup' command.\n" +" startup mode\n" +" - Issue TPM_Starup command. is one of TPM_ST_CLEAR,\n" +" TPM_ST_STATE, and TPM_ST_DEACTIVATED.\n" +"Admin Testing Commands:\n" +" self_test_full\n" +" - Test all of the TPM capabilities.\n" +" continue_self_test\n" +" - Inform TPM that it should complete the self-test.\n" +"Admin Opt-in Commands:\n" +" physical_enable\n" +" - Set the PERMANENT disable flag to FALSE using physical presence as\n" +" authorization.\n" +" physical_disable\n" +" - Set the PERMANENT disable flag to TRUE using physical presence as\n" +" authorization.\n" +" physical_set_deactivated 0|1\n" +" - Set deactivated flag.\n" +"Admin Ownership Commands:\n" +" force_clear\n" +" - Issue TPM_ForceClear command.\n" +" tsc_physical_presence flags\n" +" - Set TPM device's Physical Presence flags to .\n" +"The Capability Commands:\n" +" get_capability cap_area sub_cap addr count\n" +" - Read bytes of TPM capability indexed by and\n" +" to memory address .\n" +#if defined(CONFIG_TPM_FLUSH_RESOURCES) || defined(CONFIG_TPM_LIST_RESOURCES) +"Resource management functions\n" +#endif +#ifdef CONFIG_TPM_FLUSH_RESOURCES +" flush resource_type id\n" +" - flushes a resource of type (may be one of key, auth,\n" +" hash, trans, context, counter, delegate, daa_tpm, daa_v0, daa_v1),\n" +" and id from the TPM. Use an of \"all\" to flush all\n" +" resources of that type.\n" +#endif /* CONFIG_TPM_FLUSH_RESOURCES */ +#ifdef CONFIG_TPM_LIST_RESOURCES +" list resource_type\n" +" - lists resources of type (may be one of key, auth,\n" +" hash, trans, context, counter, delegate, daa_tpm, daa_v0, daa_v1),\n" +" contained in the TPM.\n" +#endif /* CONFIG_TPM_LIST_RESOURCES */ +#ifdef CONFIG_TPM_AUTH_SESSIONS +"Storage functions\n" +" loadkey2_oiap parent_handle key_addr key_len usage_auth\n" +" - loads a key data from memory address , bytes\n" +" into TPM using the parent key with authorization\n" +" (20 bytes hex string).\n" +#ifdef CONFIG_TPM_LOAD_KEY_BY_SHA1 +" load_key_by_sha1 parent_hash key_addr key_len usage_auth\n" +" - loads a key data from memory address , bytes\n" +" into TPM using the parent hash (20 bytes hex string)\n" +" with authorization (20 bytes hex string).\n" +#endif /* CONFIG_TPM_LOAD_KEY_BY_SHA1 */ +" get_pub_key_oiap key_handle usage_auth\n" +" - get the public key portion of a loaded key using\n" +" authorization (20 bytes hex string)\n" +#endif /* CONFIG_TPM_AUTH_SESSIONS */ +"Endorsement Key Handling Commands:\n" +" read_pubek addr count\n" +" - Read bytes of the public endorsement key to memory\n" +" address \n" +"Integrity Collection and Reporting Commands:\n" +" extend index digest_hex_string\n" +" - Add a new measurement to a PCR. Update PCR with the 20-bytes\n" +" \n" +" pcr_read index addr count\n" +" - Read bytes from PCR to memory address .\n" +#ifdef CONFIG_TPM_AUTH_SESSIONS +"Authorization Sessions\n" +" oiap\n" +" - setup an OIAP session\n" +" end_oiap\n" +" - terminates an active OIAP session\n" +#endif /* CONFIG_TPM_AUTH_SESSIONS */ +"Non-volatile Storage Commands:\n" +" nv_define_space index permission size\n" +" - Establish a space at index with of bytes.\n" +" nv_read_value index addr count\n" +" - Read bytes from space to memory address .\n" +" nv_write_value index addr count\n" +" - Write bytes from memory address to space .\n" +"Miscellaneous helper functions:\n" +" raw_transfer byte_string\n" +" - Send a byte string to TPM and print the response.\n" +" Non-volatile storage helper functions:\n" +" These helper functions treat a non-volatile space as a non-padded\n" +" sequence of integer values. These integer values are defined by a type\n" +" string, which is a text string of 'bwd' characters: 'b' means a 8-bit\n" +" value, 'w' 16-bit value, 'd' 32-bit value. All helper functions take\n" +" a type string as their first argument.\n" +" nv_define type_string index perm\n" +" - Define a space with permission .\n" +" nv_read types_string index vars...\n" +" - Read from space to environment variables .\n" +" nv_write types_string index values...\n" +" - Write to space from values .\n" +); diff --git a/cmd/tpm.c b/cmd/tpm.c deleted file mode 100644 index e0cd8410f53..00000000000 --- a/cmd/tpm.c +++ /dev/null @@ -1,1001 +0,0 @@ -// SPDX-License-Identifier: GPL-2.0+ -/* - * Copyright (c) 2013 The Chromium OS Authors. - */ - -#include -#include -#include -#include -#include -#include -#include - -/* Useful constants */ -enum { - DIGEST_LENGTH = 20, - /* max lengths, valid for RSA keys <= 2048 bits */ - TPM_PUBKEY_MAX_LENGTH = 288, -}; - -/** - * Print a byte string in hexdecimal format, 16-bytes per line. - * - * @param data byte string to be printed - * @param count number of bytes to be printed - */ -static void print_byte_string(u8 *data, size_t count) -{ - int i, print_newline = 0; - - for (i = 0; i < count; i++) { - printf(" %02x", data[i]); - print_newline = (i % 16 == 15); - if (print_newline) - putc('\n'); - } - /* Avoid duplicated newline at the end */ - if (!print_newline) - putc('\n'); -} - -/** - * Convert a text string of hexdecimal values into a byte string. - * - * @param bytes text string of hexdecimal values with no space - * between them - * @param data output buffer for byte string. The caller has to make - * sure it is large enough for storing the output. If - * NULL is passed, a large enough buffer will be allocated, - * and the caller must free it. - * @param count_ptr output variable for the length of byte string - * @return pointer to output buffer - */ -static void *parse_byte_string(char *bytes, u8 *data, size_t *count_ptr) -{ - char byte[3]; - size_t count, length; - int i; - - if (!bytes) - return NULL; - length = strlen(bytes); - count = length / 2; - - if (!data) - data = malloc(count); - if (!data) - return NULL; - - byte[2] = '\0'; - for (i = 0; i < length; i += 2) { - byte[0] = bytes[i]; - byte[1] = bytes[i + 1]; - data[i / 2] = (u8)simple_strtoul(byte, NULL, 16); - } - - if (count_ptr) - *count_ptr = count; - - return data; -} - -/** - * report_return_code() - Report any error and return failure or success - * - * @param return_code TPM command return code - * @return value of enum command_ret_t - */ -static int report_return_code(int return_code) -{ - if (return_code) { - printf("Error: %d\n", return_code); - return CMD_RET_FAILURE; - } else { - return CMD_RET_SUCCESS; - } -} - -/** - * Return number of values defined by a type string. - * - * @param type_str type string - * @return number of values of type string - */ -static int type_string_get_num_values(const char *type_str) -{ - return strlen(type_str); -} - -/** - * Return total size of values defined by a type string. - * - * @param type_str type string - * @return total size of values of type string, or 0 if type string - * contains illegal type character. - */ -static size_t type_string_get_space_size(const char *type_str) -{ - size_t size; - - for (size = 0; *type_str; type_str++) { - switch (*type_str) { - case 'b': - size += 1; - break; - case 'w': - size += 2; - break; - case 'd': - size += 4; - break; - default: - return 0; - } - } - - return size; -} - -/** - * Allocate a buffer large enough to hold values defined by a type - * string. The caller has to free the buffer. - * - * @param type_str type string - * @param count pointer for storing size of buffer - * @return pointer to buffer or NULL on error - */ -static void *type_string_alloc(const char *type_str, u32 *count) -{ - void *data; - size_t size; - - size = type_string_get_space_size(type_str); - if (!size) - return NULL; - data = malloc(size); - if (data) - *count = size; - - return data; -} - -/** - * Pack values defined by a type string into a buffer. The buffer must have - * large enough space. - * - * @param type_str type string - * @param values text strings of values to be packed - * @param data output buffer of values - * @return 0 on success, non-0 on error - */ -static int type_string_pack(const char *type_str, char * const values[], - u8 *data) -{ - size_t offset; - u32 value; - - for (offset = 0; *type_str; type_str++, values++) { - value = simple_strtoul(values[0], NULL, 0); - switch (*type_str) { - case 'b': - data[offset] = value; - offset += 1; - break; - case 'w': - put_unaligned_be16(value, data + offset); - offset += 2; - break; - case 'd': - put_unaligned_be32(value, data + offset); - offset += 4; - break; - default: - return -1; - } - } - - return 0; -} - -/** - * Read values defined by a type string from a buffer, and write these values - * to environment variables. - * - * @param type_str type string - * @param data input buffer of values - * @param vars names of environment variables - * @return 0 on success, non-0 on error - */ -static int type_string_write_vars(const char *type_str, u8 *data, - char * const vars[]) -{ - size_t offset; - u32 value; - - for (offset = 0; *type_str; type_str++, vars++) { - switch (*type_str) { - case 'b': - value = data[offset]; - offset += 1; - break; - case 'w': - value = get_unaligned_be16(data + offset); - offset += 2; - break; - case 'd': - value = get_unaligned_be32(data + offset); - offset += 4; - break; - default: - return -1; - } - if (env_set_ulong(*vars, value)) - return -1; - } - - return 0; -} - -static int do_tpm_startup(cmd_tbl_t *cmdtp, int flag, int argc, - char * const argv[]) -{ - enum tpm_startup_type mode; - - if (argc != 2) - return CMD_RET_USAGE; - if (!strcasecmp("TPM_ST_CLEAR", argv[1])) { - mode = TPM_ST_CLEAR; - } else if (!strcasecmp("TPM_ST_STATE", argv[1])) { - mode = TPM_ST_STATE; - } else if (!strcasecmp("TPM_ST_DEACTIVATED", argv[1])) { - mode = TPM_ST_DEACTIVATED; - } else { - printf("Couldn't recognize mode string: %s\n", argv[1]); - return CMD_RET_FAILURE; - } - - return report_return_code(tpm_startup(mode)); -} - -static int do_tpm_nv_define_space(cmd_tbl_t *cmdtp, int flag, int argc, - char * const argv[]) -{ - u32 index, perm, size; - - if (argc != 4) - return CMD_RET_USAGE; - index = simple_strtoul(argv[1], NULL, 0); - perm = simple_strtoul(argv[2], NULL, 0); - size = simple_strtoul(argv[3], NULL, 0); - - return report_return_code(tpm_nv_define_space(index, perm, size)); -} - -static int do_tpm_nv_read_value(cmd_tbl_t *cmdtp, int flag, int argc, - char * const argv[]) -{ - u32 index, count, rc; - void *data; - - if (argc != 4) - return CMD_RET_USAGE; - index = simple_strtoul(argv[1], NULL, 0); - data = (void *)simple_strtoul(argv[2], NULL, 0); - count = simple_strtoul(argv[3], NULL, 0); - - rc = tpm_nv_read_value(index, data, count); - if (!rc) { - puts("area content:\n"); - print_byte_string(data, count); - } - - return report_return_code(rc); -} - -static int do_tpm_nv_write_value(cmd_tbl_t *cmdtp, int flag, int argc, - char * const argv[]) -{ - u32 index, rc; - size_t count; - void *data; - - if (argc != 3) - return CMD_RET_USAGE; - index = simple_strtoul(argv[1], NULL, 0); - data = parse_byte_string(argv[2], NULL, &count); - if (!data) { - printf("Couldn't parse byte string %s\n", argv[2]); - return CMD_RET_FAILURE; - } - - rc = tpm_nv_write_value(index, data, count); - free(data); - - return report_return_code(rc); -} - -static int do_tpm_extend(cmd_tbl_t *cmdtp, int flag, int argc, - char * const argv[]) -{ - u32 index, rc; - u8 in_digest[20], out_digest[20]; - - if (argc != 3) - return CMD_RET_USAGE; - index = simple_strtoul(argv[1], NULL, 0); - if (!parse_byte_string(argv[2], in_digest, NULL)) { - printf("Couldn't parse byte string %s\n", argv[2]); - return CMD_RET_FAILURE; - } - - rc = tpm_extend(index, in_digest, out_digest); - if (!rc) { - puts("PCR value after execution of the command:\n"); - print_byte_string(out_digest, sizeof(out_digest)); - } - - return report_return_code(rc); -} - -static int do_tpm_pcr_read(cmd_tbl_t *cmdtp, int flag, int argc, - char * const argv[]) -{ - u32 index, count, rc; - void *data; - - if (argc != 4) - return CMD_RET_USAGE; - index = simple_strtoul(argv[1], NULL, 0); - data = (void *)simple_strtoul(argv[2], NULL, 0); - count = simple_strtoul(argv[3], NULL, 0); - - rc = tpm_pcr_read(index, data, count); - if (!rc) { - puts("Named PCR content:\n"); - print_byte_string(data, count); - } - - return report_return_code(rc); -} - -static int do_tpm_tsc_physical_presence(cmd_tbl_t *cmdtp, int flag, int argc, - char * const argv[]) -{ - u16 presence; - - if (argc != 2) - return CMD_RET_USAGE; - presence = (u16)simple_strtoul(argv[1], NULL, 0); - - return report_return_code(tpm_tsc_physical_presence(presence)); -} - -static int do_tpm_read_pubek(cmd_tbl_t *cmdtp, int flag, int argc, - char * const argv[]) -{ - u32 count, rc; - void *data; - - if (argc != 3) - return CMD_RET_USAGE; - data = (void *)simple_strtoul(argv[1], NULL, 0); - count = simple_strtoul(argv[2], NULL, 0); - - rc = tpm_read_pubek(data, count); - if (!rc) { - puts("pubek value:\n"); - print_byte_string(data, count); - } - - return report_return_code(rc); -} - -static int do_tpm_physical_set_deactivated(cmd_tbl_t *cmdtp, int flag, int argc, - char * const argv[]) -{ - u8 state; - - if (argc != 2) - return CMD_RET_USAGE; - state = (u8)simple_strtoul(argv[1], NULL, 0); - - return report_return_code(tpm_physical_set_deactivated(state)); -} - -static int do_tpm_get_capability(cmd_tbl_t *cmdtp, int flag, int argc, - char * const argv[]) -{ - u32 cap_area, sub_cap, rc; - void *cap; - size_t count; - - if (argc != 5) - return CMD_RET_USAGE; - cap_area = simple_strtoul(argv[1], NULL, 0); - sub_cap = simple_strtoul(argv[2], NULL, 0); - cap = (void *)simple_strtoul(argv[3], NULL, 0); - count = simple_strtoul(argv[4], NULL, 0); - - rc = tpm_get_capability(cap_area, sub_cap, cap, count); - if (!rc) { - puts("capability information:\n"); - print_byte_string(cap, count); - } - - return report_return_code(rc); -} - -#define TPM_COMMAND_NO_ARG(cmd) \ -static int do_##cmd(cmd_tbl_t *cmdtp, int flag, \ - int argc, char * const argv[]) \ -{ \ - if (argc != 1) \ - return CMD_RET_USAGE; \ - return report_return_code(cmd()); \ -} - -TPM_COMMAND_NO_ARG(tpm_init) -TPM_COMMAND_NO_ARG(tpm_self_test_full) -TPM_COMMAND_NO_ARG(tpm_continue_self_test) -TPM_COMMAND_NO_ARG(tpm_force_clear) -TPM_COMMAND_NO_ARG(tpm_physical_enable) -TPM_COMMAND_NO_ARG(tpm_physical_disable) - -static int get_tpm(struct udevice **devp) -{ - int rc; - - rc = uclass_first_device_err(UCLASS_TPM, devp); - if (rc) { - printf("Could not find TPM (ret=%d)\n", rc); - return CMD_RET_FAILURE; - } - - return 0; -} - -static int do_tpm_info(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[]) -{ - struct udevice *dev; - char buf[80]; - int rc; - - rc = get_tpm(&dev); - if (rc) - return rc; - rc = tpm_get_desc(dev, buf, sizeof(buf)); - if (rc < 0) { - printf("Couldn't get TPM info (%d)\n", rc); - return CMD_RET_FAILURE; - } - printf("%s\n", buf); - - return 0; -} - -static int do_tpm_raw_transfer(cmd_tbl_t *cmdtp, int flag, int argc, - char * const argv[]) -{ - struct udevice *dev; - void *command; - u8 response[1024]; - size_t count, response_length = sizeof(response); - u32 rc; - - command = parse_byte_string(argv[1], NULL, &count); - if (!command) { - printf("Couldn't parse byte string %s\n", argv[1]); - return CMD_RET_FAILURE; - } - - rc = get_tpm(&dev); - if (rc) - return rc; - - rc = tpm_xfer(dev, command, count, response, &response_length); - free(command); - if (!rc) { - puts("tpm response:\n"); - print_byte_string(response, response_length); - } - - return report_return_code(rc); -} - -static int do_tpm_nv_define(cmd_tbl_t *cmdtp, int flag, int argc, - char * const argv[]) -{ - u32 index, perm, size; - - if (argc != 4) - return CMD_RET_USAGE; - size = type_string_get_space_size(argv[1]); - if (!size) { - printf("Couldn't parse arguments\n"); - return CMD_RET_USAGE; - } - index = simple_strtoul(argv[2], NULL, 0); - perm = simple_strtoul(argv[3], NULL, 0); - - return report_return_code(tpm_nv_define_space(index, perm, size)); -} - -static int do_tpm_nv_read(cmd_tbl_t *cmdtp, int flag, int argc, - char * const argv[]) -{ - u32 index, count, err; - void *data; - - if (argc < 3) - return CMD_RET_USAGE; - if (argc != 3 + type_string_get_num_values(argv[1])) - return CMD_RET_USAGE; - index = simple_strtoul(argv[2], NULL, 0); - data = type_string_alloc(argv[1], &count); - if (!data) { - printf("Couldn't parse arguments\n"); - return CMD_RET_USAGE; - } - - err = tpm_nv_read_value(index, data, count); - if (!err) { - if (type_string_write_vars(argv[1], data, argv + 3)) { - printf("Couldn't write to variables\n"); - err = ~0; - } - } - free(data); - - return report_return_code(err); -} - -static int do_tpm_nv_write(cmd_tbl_t *cmdtp, int flag, int argc, - char * const argv[]) -{ - u32 index, count, err; - void *data; - - if (argc < 3) - return CMD_RET_USAGE; - if (argc != 3 + type_string_get_num_values(argv[1])) - return CMD_RET_USAGE; - index = simple_strtoul(argv[2], NULL, 0); - data = type_string_alloc(argv[1], &count); - if (!data) { - printf("Couldn't parse arguments\n"); - return CMD_RET_USAGE; - } - if (type_string_pack(argv[1], argv + 3, data)) { - printf("Couldn't parse arguments\n"); - free(data); - return CMD_RET_USAGE; - } - - err = tpm_nv_write_value(index, data, count); - free(data); - - return report_return_code(err); -} - -#ifdef CONFIG_TPM_AUTH_SESSIONS - -static int do_tpm_oiap(cmd_tbl_t *cmdtp, int flag, int argc, - char * const argv[]) -{ - u32 auth_handle, err; - - err = tpm_oiap(&auth_handle); - - return report_return_code(err); -} - -#ifdef CONFIG_TPM_LOAD_KEY_BY_SHA1 -static int do_tpm_load_key_by_sha1(cmd_tbl_t *cmdtp, int flag, int argc, char * - const argv[]) -{ - u32 parent_handle = 0; - u32 key_len, key_handle, err; - u8 usage_auth[DIGEST_LENGTH]; - u8 parent_hash[DIGEST_LENGTH]; - void *key; - - if (argc < 5) - return CMD_RET_USAGE; - - parse_byte_string(argv[1], parent_hash, NULL); - key = (void *)simple_strtoul(argv[2], NULL, 0); - key_len = simple_strtoul(argv[3], NULL, 0); - if (strlen(argv[4]) != 2 * DIGEST_LENGTH) - return CMD_RET_FAILURE; - parse_byte_string(argv[4], usage_auth, NULL); - - err = tpm_find_key_sha1(usage_auth, parent_hash, &parent_handle); - if (err) { - printf("Could not find matching parent key (err = %d)\n", err); - return CMD_RET_FAILURE; - } - - printf("Found parent key %08x\n", parent_handle); - - err = tpm_load_key2_oiap(parent_handle, key, key_len, usage_auth, - &key_handle); - if (!err) { - printf("Key handle is 0x%x\n", key_handle); - env_set_hex("key_handle", key_handle); - } - - return report_return_code(err); -} -#endif /* CONFIG_TPM_LOAD_KEY_BY_SHA1 */ - -static int do_tpm_load_key2_oiap(cmd_tbl_t *cmdtp, int flag, int argc, - char * const argv[]) -{ - u32 parent_handle, key_len, key_handle, err; - u8 usage_auth[DIGEST_LENGTH]; - void *key; - - if (argc < 5) - return CMD_RET_USAGE; - - parent_handle = simple_strtoul(argv[1], NULL, 0); - key = (void *)simple_strtoul(argv[2], NULL, 0); - key_len = simple_strtoul(argv[3], NULL, 0); - if (strlen(argv[4]) != 2 * DIGEST_LENGTH) - return CMD_RET_FAILURE; - parse_byte_string(argv[4], usage_auth, NULL); - - err = tpm_load_key2_oiap(parent_handle, key, key_len, usage_auth, - &key_handle); - if (!err) - printf("Key handle is 0x%x\n", key_handle); - - return report_return_code(err); -} - -static int do_tpm_get_pub_key_oiap(cmd_tbl_t *cmdtp, int flag, int argc, - char * const argv[]) -{ - u32 key_handle, err; - u8 usage_auth[DIGEST_LENGTH]; - u8 pub_key_buffer[TPM_PUBKEY_MAX_LENGTH]; - size_t pub_key_len = sizeof(pub_key_buffer); - - if (argc < 3) - return CMD_RET_USAGE; - - key_handle = simple_strtoul(argv[1], NULL, 0); - if (strlen(argv[2]) != 2 * DIGEST_LENGTH) - return CMD_RET_FAILURE; - parse_byte_string(argv[2], usage_auth, NULL); - - err = tpm_get_pub_key_oiap(key_handle, usage_auth, pub_key_buffer, - &pub_key_len); - if (!err) { - printf("dump of received pub key structure:\n"); - print_byte_string(pub_key_buffer, pub_key_len); - } - return report_return_code(err); -} - -TPM_COMMAND_NO_ARG(tpm_end_oiap) - -#endif /* CONFIG_TPM_AUTH_SESSIONS */ - -#ifdef CONFIG_TPM_FLUSH_RESOURCES -static int do_tpm_flush(cmd_tbl_t *cmdtp, int flag, int argc, - char * const argv[]) -{ - int type = 0; - - if (argc != 3) - return CMD_RET_USAGE; - - if (!strcasecmp(argv[1], "key")) - type = TPM_RT_KEY; - else if (!strcasecmp(argv[1], "auth")) - type = TPM_RT_AUTH; - else if (!strcasecmp(argv[1], "hash")) - type = TPM_RT_HASH; - else if (!strcasecmp(argv[1], "trans")) - type = TPM_RT_TRANS; - else if (!strcasecmp(argv[1], "context")) - type = TPM_RT_CONTEXT; - else if (!strcasecmp(argv[1], "counter")) - type = TPM_RT_COUNTER; - else if (!strcasecmp(argv[1], "delegate")) - type = TPM_RT_DELEGATE; - else if (!strcasecmp(argv[1], "daa_tpm")) - type = TPM_RT_DAA_TPM; - else if (!strcasecmp(argv[1], "daa_v0")) - type = TPM_RT_DAA_V0; - else if (!strcasecmp(argv[1], "daa_v1")) - type = TPM_RT_DAA_V1; - - if (!type) { - printf("Resource type %s unknown.\n", argv[1]); - return -1; - } - - if (!strcasecmp(argv[2], "all")) { - u16 res_count; - u8 buf[288]; - u8 *ptr; - int err; - uint i; - - /* fetch list of already loaded resources in the TPM */ - err = tpm_get_capability(TPM_CAP_HANDLE, type, buf, - sizeof(buf)); - if (err) { - printf("tpm_get_capability returned error %d.\n", err); - return -1; - } - res_count = get_unaligned_be16(buf); - ptr = buf + 2; - for (i = 0; i < res_count; ++i, ptr += 4) - tpm_flush_specific(get_unaligned_be32(ptr), type); - } else { - u32 handle = simple_strtoul(argv[2], NULL, 0); - - if (!handle) { - printf("Illegal resource handle %s\n", argv[2]); - return -1; - } - tpm_flush_specific(cpu_to_be32(handle), type); - } - - return 0; -} -#endif /* CONFIG_TPM_FLUSH_RESOURCES */ - -#ifdef CONFIG_TPM_LIST_RESOURCES -static int do_tpm_list(cmd_tbl_t *cmdtp, int flag, int argc, - char * const argv[]) -{ - int type = 0; - u16 res_count; - u8 buf[288]; - u8 *ptr; - int err; - uint i; - - if (argc != 2) - return CMD_RET_USAGE; - - if (!strcasecmp(argv[1], "key")) - type = TPM_RT_KEY; - else if (!strcasecmp(argv[1], "auth")) - type = TPM_RT_AUTH; - else if (!strcasecmp(argv[1], "hash")) - type = TPM_RT_HASH; - else if (!strcasecmp(argv[1], "trans")) - type = TPM_RT_TRANS; - else if (!strcasecmp(argv[1], "context")) - type = TPM_RT_CONTEXT; - else if (!strcasecmp(argv[1], "counter")) - type = TPM_RT_COUNTER; - else if (!strcasecmp(argv[1], "delegate")) - type = TPM_RT_DELEGATE; - else if (!strcasecmp(argv[1], "daa_tpm")) - type = TPM_RT_DAA_TPM; - else if (!strcasecmp(argv[1], "daa_v0")) - type = TPM_RT_DAA_V0; - else if (!strcasecmp(argv[1], "daa_v1")) - type = TPM_RT_DAA_V1; - - if (!type) { - printf("Resource type %s unknown.\n", argv[1]); - return -1; - } - - /* fetch list of already loaded resources in the TPM */ - err = tpm_get_capability(TPM_CAP_HANDLE, type, buf, - sizeof(buf)); - if (err) { - printf("tpm_get_capability returned error %d.\n", err); - return -1; - } - res_count = get_unaligned_be16(buf); - ptr = buf + 2; - - printf("Resources of type %s (%02x):\n", argv[1], type); - if (!res_count) { - puts("None\n"); - } else { - for (i = 0; i < res_count; ++i, ptr += 4) - printf("Index %d: %08x\n", i, get_unaligned_be32(ptr)); - } - - return 0; -} -#endif /* CONFIG_TPM_LIST_RESOURCES */ - -#define MAKE_TPM_CMD_ENTRY(cmd) \ - U_BOOT_CMD_MKENT(cmd, 0, 1, do_tpm_ ## cmd, "", "") - -static cmd_tbl_t tpm_commands[] = { - U_BOOT_CMD_MKENT(info, 0, 1, do_tpm_info, "", ""), - U_BOOT_CMD_MKENT(init, 0, 1, do_tpm_init, "", ""), - U_BOOT_CMD_MKENT(startup, 0, 1, - do_tpm_startup, "", ""), - U_BOOT_CMD_MKENT(self_test_full, 0, 1, - do_tpm_self_test_full, "", ""), - U_BOOT_CMD_MKENT(continue_self_test, 0, 1, - do_tpm_continue_self_test, "", ""), - U_BOOT_CMD_MKENT(force_clear, 0, 1, - do_tpm_force_clear, "", ""), - U_BOOT_CMD_MKENT(physical_enable, 0, 1, - do_tpm_physical_enable, "", ""), - U_BOOT_CMD_MKENT(physical_disable, 0, 1, - do_tpm_physical_disable, "", ""), - U_BOOT_CMD_MKENT(nv_define_space, 0, 1, - do_tpm_nv_define_space, "", ""), - U_BOOT_CMD_MKENT(nv_read_value, 0, 1, - do_tpm_nv_read_value, "", ""), - U_BOOT_CMD_MKENT(nv_write_value, 0, 1, - do_tpm_nv_write_value, "", ""), - U_BOOT_CMD_MKENT(extend, 0, 1, - do_tpm_extend, "", ""), - U_BOOT_CMD_MKENT(pcr_read, 0, 1, - do_tpm_pcr_read, "", ""), - U_BOOT_CMD_MKENT(tsc_physical_presence, 0, 1, - do_tpm_tsc_physical_presence, "", ""), - U_BOOT_CMD_MKENT(read_pubek, 0, 1, - do_tpm_read_pubek, "", ""), - U_BOOT_CMD_MKENT(physical_set_deactivated, 0, 1, - do_tpm_physical_set_deactivated, "", ""), - U_BOOT_CMD_MKENT(get_capability, 0, 1, - do_tpm_get_capability, "", ""), - U_BOOT_CMD_MKENT(raw_transfer, 0, 1, - do_tpm_raw_transfer, "", ""), - U_BOOT_CMD_MKENT(nv_define, 0, 1, - do_tpm_nv_define, "", ""), - U_BOOT_CMD_MKENT(nv_read, 0, 1, - do_tpm_nv_read, "", ""), - U_BOOT_CMD_MKENT(nv_write, 0, 1, - do_tpm_nv_write, "", ""), -#ifdef CONFIG_TPM_AUTH_SESSIONS - U_BOOT_CMD_MKENT(oiap, 0, 1, - do_tpm_oiap, "", ""), - U_BOOT_CMD_MKENT(end_oiap, 0, 1, - do_tpm_end_oiap, "", ""), - U_BOOT_CMD_MKENT(load_key2_oiap, 0, 1, - do_tpm_load_key2_oiap, "", ""), -#ifdef CONFIG_TPM_LOAD_KEY_BY_SHA1 - U_BOOT_CMD_MKENT(load_key_by_sha1, 0, 1, - do_tpm_load_key_by_sha1, "", ""), -#endif /* CONFIG_TPM_LOAD_KEY_BY_SHA1 */ - U_BOOT_CMD_MKENT(get_pub_key_oiap, 0, 1, - do_tpm_get_pub_key_oiap, "", ""), -#endif /* CONFIG_TPM_AUTH_SESSIONS */ -#ifdef CONFIG_TPM_FLUSH_RESOURCES - U_BOOT_CMD_MKENT(flush, 0, 1, - do_tpm_flush, "", ""), -#endif /* CONFIG_TPM_FLUSH_RESOURCES */ -#ifdef CONFIG_TPM_LIST_RESOURCES - U_BOOT_CMD_MKENT(list, 0, 1, - do_tpm_list, "", ""), -#endif /* CONFIG_TPM_LIST_RESOURCES */ -}; - -static int do_tpm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) -{ - cmd_tbl_t *tpm_cmd; - - if (argc < 2) - return CMD_RET_USAGE; - tpm_cmd = find_cmd_tbl(argv[1], tpm_commands, ARRAY_SIZE(tpm_commands)); - if (!tpm_cmd) - return CMD_RET_USAGE; - - return tpm_cmd->cmd(cmdtp, flag, argc - 1, argv + 1); -} - -U_BOOT_CMD(tpm, CONFIG_SYS_MAXARGS, 1, do_tpm, -"Issue a TPM command", -"cmd args...\n" -" - Issue TPM command with arguments .\n" -"Admin Startup and State Commands:\n" -" info - Show information about the TPM\n" -" init\n" -" - Put TPM into a state where it waits for 'startup' command.\n" -" startup mode\n" -" - Issue TPM_Starup command. is one of TPM_ST_CLEAR,\n" -" TPM_ST_STATE, and TPM_ST_DEACTIVATED.\n" -"Admin Testing Commands:\n" -" self_test_full\n" -" - Test all of the TPM capabilities.\n" -" continue_self_test\n" -" - Inform TPM that it should complete the self-test.\n" -"Admin Opt-in Commands:\n" -" physical_enable\n" -" - Set the PERMANENT disable flag to FALSE using physical presence as\n" -" authorization.\n" -" physical_disable\n" -" - Set the PERMANENT disable flag to TRUE using physical presence as\n" -" authorization.\n" -" physical_set_deactivated 0|1\n" -" - Set deactivated flag.\n" -"Admin Ownership Commands:\n" -" force_clear\n" -" - Issue TPM_ForceClear command.\n" -" tsc_physical_presence flags\n" -" - Set TPM device's Physical Presence flags to .\n" -"The Capability Commands:\n" -" get_capability cap_area sub_cap addr count\n" -" - Read bytes of TPM capability indexed by and\n" -" to memory address .\n" -#if defined(CONFIG_TPM_FLUSH_RESOURCES) || defined(CONFIG_TPM_LIST_RESOURCES) -"Resource management functions\n" -#endif -#ifdef CONFIG_TPM_FLUSH_RESOURCES -" flush resource_type id\n" -" - flushes a resource of type (may be one of key, auth,\n" -" hash, trans, context, counter, delegate, daa_tpm, daa_v0, daa_v1),\n" -" and id from the TPM. Use an of \"all\" to flush all\n" -" resources of that type.\n" -#endif /* CONFIG_TPM_FLUSH_RESOURCES */ -#ifdef CONFIG_TPM_LIST_RESOURCES -" list resource_type\n" -" - lists resources of type (may be one of key, auth,\n" -" hash, trans, context, counter, delegate, daa_tpm, daa_v0, daa_v1),\n" -" contained in the TPM.\n" -#endif /* CONFIG_TPM_LIST_RESOURCES */ -#ifdef CONFIG_TPM_AUTH_SESSIONS -"Storage functions\n" -" loadkey2_oiap parent_handle key_addr key_len usage_auth\n" -" - loads a key data from memory address , bytes\n" -" into TPM using the parent key with authorization\n" -" (20 bytes hex string).\n" -#ifdef CONFIG_TPM_LOAD_KEY_BY_SHA1 -" load_key_by_sha1 parent_hash key_addr key_len usage_auth\n" -" - loads a key data from memory address , bytes\n" -" into TPM using the parent hash (20 bytes hex string)\n" -" with authorization (20 bytes hex string).\n" -#endif /* CONFIG_TPM_LOAD_KEY_BY_SHA1 */ -" get_pub_key_oiap key_handle usage_auth\n" -" - get the public key portion of a loaded key using\n" -" authorization (20 bytes hex string)\n" -#endif /* CONFIG_TPM_AUTH_SESSIONS */ -"Endorsement Key Handling Commands:\n" -" read_pubek addr count\n" -" - Read bytes of the public endorsement key to memory\n" -" address \n" -"Integrity Collection and Reporting Commands:\n" -" extend index digest_hex_string\n" -" - Add a new measurement to a PCR. Update PCR with the 20-bytes\n" -" \n" -" pcr_read index addr count\n" -" - Read bytes from PCR to memory address .\n" -#ifdef CONFIG_TPM_AUTH_SESSIONS -"Authorization Sessions\n" -" oiap\n" -" - setup an OIAP session\n" -" end_oiap\n" -" - terminates an active OIAP session\n" -#endif /* CONFIG_TPM_AUTH_SESSIONS */ -"Non-volatile Storage Commands:\n" -" nv_define_space index permission size\n" -" - Establish a space at index with of bytes.\n" -" nv_read_value index addr count\n" -" - Read bytes from space to memory address .\n" -" nv_write_value index addr count\n" -" - Write bytes from memory address to space .\n" -"Miscellaneous helper functions:\n" -" raw_transfer byte_string\n" -" - Send a byte string to TPM and print the response.\n" -" Non-volatile storage helper functions:\n" -" These helper functions treat a non-volatile space as a non-padded\n" -" sequence of integer values. These integer values are defined by a type\n" -" string, which is a text string of 'bwd' characters: 'b' means a 8-bit\n" -" value, 'w' 16-bit value, 'd' 32-bit value. All helper functions take\n" -" a type string as their first argument.\n" -" nv_define type_string index perm\n" -" - Define a space with permission .\n" -" nv_read types_string index vars...\n" -" - Read from space to environment variables .\n" -" nv_write types_string index values...\n" -" - Write to space from values .\n" -); diff --git a/cmd/tpm_test.c b/cmd/tpm_test.c index 2e7d133a47e..35f3c96e3de 100644 --- a/cmd/tpm_test.c +++ b/cmd/tpm_test.c @@ -6,7 +6,7 @@ #include #include #include -#include +#include /* Prints error and returns on failure */ #define TPM_CHECK(tpm_command) do { \ -- cgit v1.2.3