Skip to contentSkip to conversation

tpm: Add TPM command library and utility commands

PATCH New Che-Liang Chiou 3 participants 6 messages Feb 9, 2013 – Feb 28, 2013 Library code

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tpm: Add TPM command library and utility commands

TPM command library implements a subset of TPM commands defined in TCG Main Specification 1.2 that are useful for implementing secure boot. More TPM commands could be added out of necessity.

You may exercise these commands through the 'tpm' command. However, the 'tpm' command is too primitive for writing secure boot in command interpreter scripts; so the utility commands 'tpmutil' is provided to make this task easier.

For example, to define a counter in TPM non-volatile storage and initialize it to zero:

$ tpm init
$ tpm startup TPM_ST_CLEAR
$ tpmutil nv_define d 0x1001 0x1
$ tpmutil nv_write d 0x1001 0

And then increment the counter by one:

$ tpmutil nv_read d 0x1001 i
$ setexpr.l i $i + 1
$ tpmutil nv_write d 0x1001 $i
---
 common/cmd_tpm.c         |  732 +++++++++++++++++++++++++++++++++++++++-------
 include/{tpm.h => tis.h} |    8 +-
 include/tpm.h            |  197 +++++++++++--
 lib/Makefile             |    1 +
 lib/tpm.c                |  569 +++++++++++++++++++++++++++++++++++
 5 files changed, 1362 insertions(+), 145 deletions(-)
 copy include/{tpm.h => tis.h} (95%)
 create mode 100644 lib/tpm.c
common/cmd_tpm.c +622 -110
index 0970a6f..cdf74e6 100644
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2011 The Chromium OS Authors.
+ * Copyright (c) 2013 The Chromium OS Authors.
  *
  * See file CREDITS for list of people who contributed to this
  * project.
@@ -22,163 +22,675 @@
 
 #include <common.h>
 #include <command.h>
+#include <malloc.h>
 #include <tpm.h>
+#include <asm/unaligned.h>
+#include <linux/string.h>
 
-#define MAX_TRANSACTION_SIZE 30
+/**
+ * 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(uint8_t *data, size_t count)
+{
+	int i, print_newline = 0;
 
-/*
- * tpm_write() expects a variable number of parameters: the internal address
- * followed by data to write, byte by byte.
+	for (i = 0; i < count; i++) {
+		printf(" %02x", data[i]);
+		print_newline = (i % 16 == 15);
+		if (print_newline)
+			putc('\n');
+	}
+	/* Avoid duplicated newline@the end */
+	if (!print_newline)
+		putc('\n');
+}
+
+/**
+ * Convert a text string of hexdecimal values into a byte string.
  *
- * Returns 0 on success or -1 on errors (wrong arguments or TPM failure).
+ * @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 int tpm_process(int argc, char * const argv[], cmd_tbl_t *cmdtp)
-{
-	u8 tpm_buffer[MAX_TRANSACTION_SIZE];
-	u32 write_size, read_size;
-	char *p;
-	int rv = -1;
-
-	for (write_size = 0; write_size < argc; write_size++) {
-		u32 datum = simple_strtoul(argv[write_size], &p, 0);
-		if (*p || (datum > 0xff)) {
-			printf("\n%s: bad data value\n\n", argv[write_size]);
-			cmd_usage(cmdtp);
-			return rv;
-		}
-		tpm_buffer[write_size] = (u8)datum;
+static void *parse_byte_string(char *bytes, uint8_t *data, size_t *count_ptr)
+{
+	char byte[3];
+	size_t count, length;
+	int i;
+
+	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] = (uint8_t)simple_strtoul(byte, NULL, 16);
 	}
 
-	read_size = sizeof(tpm_buffer);
-	if (!tis_sendrecv(tpm_buffer, write_size, tpm_buffer, &read_size)) {
-		int i;
-		puts("Got TPM response:\n");
-		for (i = 0; i < read_size; i++)
-			printf(" %2.2x", tpm_buffer[i]);
-		puts("\n");
-		rv = 0;
-	} else {
-		puts("tpm command failed\n");
+	if (count_ptr)
+		*count_ptr = count;
+
+	return data;
+}
+
+/**
+ * Convert TPM command return code to U-Boot command error codes.
+ *
+ * @param return_code	TPM command return code
+ * @return value of enum command_ret_t
+ */
+static int convert_return_code(uint32_t return_code)
+{
+	if (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 rv;
+
+	return size;
 }
 
-#define CHECK(exp) do {							\
-		int _rv = exp;						\
-		if (_rv) {						\
-			printf("CHECK: %s %d %x\n", #exp, __LINE__, _rv);\
-		}							\
-	} while (0)
+/**
+ * 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, uint32_t *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;
+}
 
-static int tpm_process_stress(int repeat_count)
+/**
+ * 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[],
+		uint8_t *data)
 {
-	int i;
-	int rv = 0;
-	u8 request[] = {0x0, 0xc1,
-			0x0, 0x0, 0x0, 0x16,
-			0x0, 0x0, 0x0, 0x65,
-			0x0, 0x0, 0x0, 0x4,
-			0x0, 0x0, 0x0, 0x4,
-			0x0, 0x0, 0x1, 0x9};
-	u8 response[MAX_TRANSACTION_SIZE];
-	u32 rlength = MAX_TRANSACTION_SIZE;
-
-	CHECK(tis_init());
-
-	for (i = 0; i < repeat_count; i++) {
-		CHECK(tis_open());
-		rv = tis_sendrecv(request, sizeof(request), response, &rlength);
-		if (rv) {
-			printf("tpm test failed at step %d with 0x%x\n", i, rv);
-			CHECK(tis_close());
+	size_t offset;
+	uint32_t 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;
 		}
-		CHECK(tis_close());
-		if ((response[6] || response[7] || response[8] || response[9])
-		    && response[9] != 0x26) {
-			/* Ignore postinit errors */
-			printf("tpm command failed at step %d\n"
-			       "tpm error code: %02x%02x%02x%02x\n", i,
-			       response[6], response[7],
-			       response[8], response[9]);
-			rv = -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, uint8_t *data,
+		char * const vars[])
+{
+	size_t offset;
+	uint32_t 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 (setenv_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 rv;
+
+	return convert_return_code(tpm_startup(mode));
 }
 
+static int do_tpm_nv_define_space(cmd_tbl_t *cmdtp, int flag,
+		int argc, char * const argv[])
+{
+	uint32_t index, perm, size;
 
-static int do_tpm_many(cmd_tbl_t *cmdtp, int flag,
-		       int argc, char * const argv[], int repeat_count)
+	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 convert_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[])
 {
-	int rv = 0;
+	uint32_t index, count, rc;
+	void *data;
 
-	if (argc < 7 && repeat_count == 0) {
-		puts("command should be@least six bytes in size\n");
-		return -1;
+	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);
 	}
 
-	if (repeat_count > 0) {
-		rv = tpm_process_stress(repeat_count);
-		return rv;
+	return convert_return_code(rc);
+}
+
+static int do_tpm_nv_write_value(cmd_tbl_t *cmdtp, int flag,
+		int argc, char * const argv[])
+{
+	uint32_t 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;
 	}
 
-	if (tis_init()) {
-		puts("tis_init() failed!\n");
-		return -1;
+	rc = tpm_nv_write_value(index, data, count);
+	free(data);
+
+	return convert_return_code(rc);
+}
+
+static int do_tpm_extend(cmd_tbl_t *cmdtp, int flag,
+		int argc, char * const argv[])
+{
+	uint32_t index, rc;
+	uint8_t 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;
 	}
 
-	if (tis_open()) {
-		puts("tis_open() failed!\n");
-		return -1;
+	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));
 	}
 
-	rv = tpm_process(argc - 1, argv + 1, cmdtp);
+	return convert_return_code(rc);
+}
+
+static int do_tpm_pcr_read(cmd_tbl_t *cmdtp, int flag,
+		int argc, char * const argv[])
+{
+	uint32_t 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);
 
-	if (tis_close()) {
-		puts("tis_close() failed!\n");
-		rv = -1;
+	rc = tpm_pcr_read(index, data, count);
+	if (!rc) {
+		puts("Named PCR content:\n");
+		print_byte_string(data, count);
 	}
 
-	return rv;
+	return convert_return_code(rc);
 }
 
+static int do_tpm_tsc_physical_presence(cmd_tbl_t *cmdtp, int flag,
+		int argc, char * const argv[])
+{
+	uint16_t presence;
 
-static int do_tpm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
+	if (argc != 2)
+		return CMD_RET_USAGE;
+	presence = (uint16_t)simple_strtoul(argv[1], NULL, 0);
+
+	return convert_return_code(tpm_tsc_physical_presence(presence));
+}
+
+static int do_tpm_read_pubek(cmd_tbl_t *cmdtp, int flag,
+		int argc, char * const argv[])
+{
+	uint32_t 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 convert_return_code(rc);
+}
+
+static int do_tpm_physical_set_deactivated(cmd_tbl_t *cmdtp, int flag,
+		int argc, char * const argv[])
 {
-	return do_tpm_many(cmdtp, flag, argc, argv, 0);
+	uint8_t state;
+
+	if (argc != 2)
+		return CMD_RET_USAGE;
+	state = (uint8_t)simple_strtoul(argv[1], NULL, 0);
+
+	return convert_return_code(tpm_physical_set_deactivated(state));
 }
 
+static int do_tpm_get_capability(cmd_tbl_t *cmdtp, int flag,
+		int argc, char * const argv[])
+{
+	uint32_t 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);
 
-U_BOOT_CMD(tpm, MAX_TRANSACTION_SIZE, 1, do_tpm,
-	   "<byte> [<byte> ...]   - write data and read response",
-	   "send arbitrary data (at least 6 bytes) to the TPM "
-	   "device and read the response"
-);
+	rc = tpm_get_capability(cap_area, sub_cap, cap, count);
+	if (!rc) {
+		puts("capability information:\n");
+		print_byte_string(cap, count);
+	}
+
+	return convert_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 convert_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 do_tpmutil_raw_transfer(cmd_tbl_t *cmdtp, int flag,
+		int argc, char * const argv[])
+{
+	void *command;
+	uint8_t response[1024];
+	size_t count, response_length = sizeof(response);
+	uint32_t 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 = tis_sendrecv(command, count, response, &response_length);
+	free(command);
+	if (!rc) {
+		puts("tpm response:\n");
+		print_byte_string(response, response_length);
+	}
+
+	return convert_return_code(rc);
+}
+
+static int do_tpmutil_nv_define(cmd_tbl_t *cmdtp, int flag,
+		int argc, char * const argv[])
+{
+	uint32_t 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);
 
-static int do_tpm_stress(cmd_tbl_t *cmdtp, int flag,
-			 int argc, char * const argv[])
+	return convert_return_code(tpm_nv_define_space(index, perm, size));
+}
+
+static int do_tpmutil_nv_read(cmd_tbl_t *cmdtp, int flag,
+		int argc, char * const argv[])
 {
-	long unsigned int n;
-	int rv;
+	uint32_t index, count, err;
+	void *data;
 
-	if (argc != 2) {
-		puts("usage: tpm_stress <count>\n");
-		return -1;
+	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 convert_return_code(err);
+}
+
+static int do_tpmutil_nv_write(cmd_tbl_t *cmdtp, int flag,
+		int argc, char * const argv[])
+{
+	uint32_t index, count, err;
+	void *data;
 
-	rv = strict_strtoul(argv[1], 10, &n);
-	if (rv) {
-		puts("tpm_stress: bad count");
-		return -1;
+	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 convert_return_code(err);
+}
+
+#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(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, "", ""),
+};
+
+static cmd_tbl_t tpmutil_commands[] = {
+	U_BOOT_CMD_MKENT(raw_transfer, 0, 1,
+			do_tpmutil_raw_transfer, "", ""),
+	U_BOOT_CMD_MKENT(nv_define, 0, 1,
+			do_tpmutil_nv_define, "", ""),
+	U_BOOT_CMD_MKENT(nv_read, 0, 1,
+			do_tpmutil_nv_read, "", ""),
+	U_BOOT_CMD_MKENT(nv_write, 0, 1,
+			do_tpmutil_nv_write, "", ""),
+};
+
+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 do_tpm_many(cmdtp, flag, argc, argv, n);
+	return tpm_cmd->cmd(cmdtp, flag, argc - 1, argv + 1);
 }
 
-U_BOOT_CMD(tpm_stress, 2, 1, do_tpm_stress,
-	   "<n>   - stress-test communication with TPM",
-	   "Repeat a TPM transaction (request-response) N times"
+static int do_tpmutil(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
+{
+	cmd_tbl_t *tpmutil_cmd;
+
+	if (argc < 2)
+		return CMD_RET_USAGE;
+	tpmutil_cmd = find_cmd_tbl(argv[1],
+			tpmutil_commands, ARRAY_SIZE(tpmutil_commands));
+	if (!tpmutil_cmd)
+		return CMD_RET_USAGE;
+
+	return tpmutil_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 <cmd> with arguments <args...>.\n"
+"Admin Startup and State Commands:\n"
+"  init\n"
+"    - Put TPM into a state where it waits for 'startup' command.\n"
+"  startup mode\n"
+"    - Issue TPM_Starup command.  <mode> 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 <flags>.\n"
+"The Capability Commands:\n"
+"  get_capability cap_area sub_cap addr count\n"
+"    - Read <count> bytes of TPM capability indexed by <cap_area> and\n"
+"      <sub_cap> to memory address <addr>.\n"
+"Endorsement Key Handling Commands:\n"
+"  read_pubek addr count\n"
+"    - Read <count> bytes of the public endorsement key to memory\n"
+"      address <addr>\n"
+"Integrity Collection and Reporting Commands:\n"
+"  extend index digest_hex_string\n"
+"    - Add a new measurement to a PCR.  Update PCR <index> with the 20-bytes\n"
+"      <digest_hex_string>\n"
+"  pcr_read index addr count\n"
+"    - Read <count> bytes from PCR <index> to memory address <addr>.\n"
+"Non-volatile Storage Commands:\n"
+"  nv_define_space index permission size\n"
+"    - Establish a space at index <index> with <permission> of <size> bytes.\n"
+"  nv_read_value index addr count\n"
+"    - Read <count> bytes from space <index> to memory address <addr>.\n"
+"  nv_write_value index addr count\n"
+"    - Write <count> bytes from memory address <addr> to space <index>.\n"
+);
+
+U_BOOT_CMD(tpmutil, CONFIG_SYS_MAXARGS, 1, do_tpmutil,
+"Miscellaneous TPM helper functions",
+"func args...\n"
+"    - Run helper function <func> with arguments <args...>.\n"
+"List of helper functions:"
+"  raw_transfer byte_string\n"
+"    - Send a byte string <byte_string> to TPM and print the response.\n"
+"Helper functions for accessing TPM non-volatile storage:\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 <index> with permission <perm>.\n"
+"  nv_read types_string index vars...\n"
+"    - Read from space <index> to environment variables <vars...>.\n"
+"  nv_write types_string index values...\n"
+"    - Write to space <index> from values <values...>.\n"
 );
include/tis.h renamed from include/tpm.h +5 -3
similarity index 95%
copy from include/tpm.h
copy to include/tis.h
index 6b21e9c..89e5730 100644
@@ -20,11 +20,13 @@
  * MA 02111-1307 USA
  */
 
-#ifndef _INCLUDE_TPM_H_
-#define _INCLUDE_TPM_H_
+#ifndef __TIS_H
+#define __TIS_H
 
 #include <common.h>
 
+/* Low-level interface to access TPM */
+
 /*
  * tis_init()
  *
@@ -68,4 +70,4 @@ int tis_close(void);
 int tis_sendrecv(const uint8_t *sendbuf, size_t send_size, uint8_t *recvbuf,
 			size_t *recv_len);
 
-#endif /* _INCLUDE_TPM_H_ */
+#endif /* __TIS_H */
include/tpm.h +165 -32

222 lines not on this page · raw message

lib/Makefile +1 -0
index 86ca1a6..d07d40b 100644
@@ -53,6 +53,7 @@ COBJS-y += qsort.o
 COBJS-$(CONFIG_SHA1) += sha1.o
 COBJS-$(CONFIG_SHA256) += sha256.o
 COBJS-y	+= strmhz.o
+COBJS-$(CONFIG_TPM) += tpm.o
 COBJS-$(CONFIG_RBTREE)	+= rbtree.o
 endif
 
lib/tpm.c new file +569 -0

569 lines not on this page · raw message

Signature · 1 line
1.7.7.3
#1 lore raw

PATCH 1/1 tpm: Add TPM command library and utility commands

Dear Che-Liang Chiou,

Quoted 9 lines · In message <[email protected]> you wrote:

TPM command library implements a subset of TPM commands defined in TCG Main Specification 1.2 that are useful for implementing secure boot. More TPM commands could be added out of necessity.

You may exercise these commands through the 'tpm' command. However, the 'tpm' command is too primitive for writing secure boot in command interpreter scripts; so the utility commands 'tpmutil' is provided to make this task easier.

Quoted 7 lines

For example, to define a counter in TPM non-volatile storage and initialize it to zero:

$ tpm init
$ tpm startup TPM_ST_CLEAR
$ tpmutil nv_define d 0x1001 0x1
$ tpmutil nv_write d 0x1001 0

This is extremely ugly? Why can we not implement both parts of the functionality in a single command (tpm) ?

Please fix!

Best regards,

Wolfgang Denk

Signature · 5 lines
DENX Software Engineering GmbH,     MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de
Philosophy:  A route of many roads leading from nowhere to nothing.
- Ambrose Bierce
#2 lore raw

PATCH v2 1/1 tpm: Add TPM command library

In reply to Che-Liang Chiou

TPM command library implements a subset of TPM commands defined in TCG Main Specification 1.2 that are useful for implementing secure boot. More TPM commands could be added out of necessity.

You may exercise these commands through the 'tpm' command. However, the raw TPM commands are too primitive for writing secure boot in command interpreter scripts; so the 'tpm' command also provides helper functions to make scripting easier.

For example, to define a counter in TPM non-volatile storage and initialize it to zero:

$ tpm init
$ tpm startup TPM_ST_CLEAR
$ tpm nv_define d 0x1001 0x1
$ tpm nv_write d 0x1001 0

And then increment the counter by one:

$ tpm nv_read d 0x1001 i
$ setexpr.l i $i + 1
$ tpm nv_write d 0x1001 $i
---
 common/cmd_tpm.c         | 709 +++++++++++++++++++++++++++++++++++++++--------
 include/{tpm.h => tis.h} |   8 +-
 include/tpm.h            | 197 ++++++++++---
 lib/Makefile             |   1 +
 lib/tpm.c                | 569 +++++++++++++++++++++++++++++++++++++
 5 files changed, 1339 insertions(+), 145 deletions(-)
 copy include/{tpm.h => tis.h} (95%)
 create mode 100644 lib/tpm.c
common/cmd_tpm.c +599 -110
index 0970a6f..46fae18 100644
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2011 The Chromium OS Authors.
+ * Copyright (c) 2013 The Chromium OS Authors.
  *
  * See file CREDITS for list of people who contributed to this
  * project.
@@ -22,163 +22,652 @@
 
 #include <common.h>
 #include <command.h>
+#include <malloc.h>
 #include <tpm.h>
+#include <asm/unaligned.h>
+#include <linux/string.h>
 
-#define MAX_TRANSACTION_SIZE 30
+/**
+ * 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(uint8_t *data, size_t count)
+{
+	int i, print_newline = 0;
 
-/*
- * tpm_write() expects a variable number of parameters: the internal address
- * followed by data to write, byte by byte.
+	for (i = 0; i < count; i++) {
+		printf(" %02x", data[i]);
+		print_newline = (i % 16 == 15);
+		if (print_newline)
+			putc('\n');
+	}
+	/* Avoid duplicated newline@the end */
+	if (!print_newline)
+		putc('\n');
+}
+
+/**
+ * Convert a text string of hexdecimal values into a byte string.
  *
- * Returns 0 on success or -1 on errors (wrong arguments or TPM failure).
+ * @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 int tpm_process(int argc, char * const argv[], cmd_tbl_t *cmdtp)
-{
-	u8 tpm_buffer[MAX_TRANSACTION_SIZE];
-	u32 write_size, read_size;
-	char *p;
-	int rv = -1;
-
-	for (write_size = 0; write_size < argc; write_size++) {
-		u32 datum = simple_strtoul(argv[write_size], &p, 0);
-		if (*p || (datum > 0xff)) {
-			printf("\n%s: bad data value\n\n", argv[write_size]);
-			cmd_usage(cmdtp);
-			return rv;
-		}
-		tpm_buffer[write_size] = (u8)datum;
+static void *parse_byte_string(char *bytes, uint8_t *data, size_t *count_ptr)
+{
+	char byte[3];
+	size_t count, length;
+	int i;
+
+	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] = (uint8_t)simple_strtoul(byte, NULL, 16);
 	}
 
-	read_size = sizeof(tpm_buffer);
-	if (!tis_sendrecv(tpm_buffer, write_size, tpm_buffer, &read_size)) {
-		int i;
-		puts("Got TPM response:\n");
-		for (i = 0; i < read_size; i++)
-			printf(" %2.2x", tpm_buffer[i]);
-		puts("\n");
-		rv = 0;
-	} else {
-		puts("tpm command failed\n");
+	if (count_ptr)
+		*count_ptr = count;
+
+	return data;
+}
+
+/**
+ * Convert TPM command return code to U-Boot command error codes.
+ *
+ * @param return_code	TPM command return code
+ * @return value of enum command_ret_t
+ */
+static int convert_return_code(uint32_t return_code)
+{
+	if (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 rv;
+
+	return size;
 }
 
-#define CHECK(exp) do {							\
-		int _rv = exp;						\
-		if (_rv) {						\
-			printf("CHECK: %s %d %x\n", #exp, __LINE__, _rv);\
-		}							\
-	} while (0)
+/**
+ * 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, uint32_t *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;
 
-static int tpm_process_stress(int repeat_count)
+	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[],
+		uint8_t *data)
 {
-	int i;
-	int rv = 0;
-	u8 request[] = {0x0, 0xc1,
-			0x0, 0x0, 0x0, 0x16,
-			0x0, 0x0, 0x0, 0x65,
-			0x0, 0x0, 0x0, 0x4,
-			0x0, 0x0, 0x0, 0x4,
-			0x0, 0x0, 0x1, 0x9};
-	u8 response[MAX_TRANSACTION_SIZE];
-	u32 rlength = MAX_TRANSACTION_SIZE;
-
-	CHECK(tis_init());
-
-	for (i = 0; i < repeat_count; i++) {
-		CHECK(tis_open());
-		rv = tis_sendrecv(request, sizeof(request), response, &rlength);
-		if (rv) {
-			printf("tpm test failed at step %d with 0x%x\n", i, rv);
-			CHECK(tis_close());
+	size_t offset;
+	uint32_t 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;
 		}
-		CHECK(tis_close());
-		if ((response[6] || response[7] || response[8] || response[9])
-		    && response[9] != 0x26) {
-			/* Ignore postinit errors */
-			printf("tpm command failed at step %d\n"
-			       "tpm error code: %02x%02x%02x%02x\n", i,
-			       response[6], response[7],
-			       response[8], response[9]);
-			rv = -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, uint8_t *data,
+		char * const vars[])
+{
+	size_t offset;
+	uint32_t 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 (setenv_ulong(*vars, value))
+			return -1;
 	}
-	return rv;
+
+	return 0;
 }
 
+static int do_tpm_startup(cmd_tbl_t *cmdtp, int flag,
+		int argc, char * const argv[])
+{
+	enum tpm_startup_type mode;
 
-static int do_tpm_many(cmd_tbl_t *cmdtp, int flag,
-		       int argc, char * const argv[], int repeat_count)
+	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 convert_return_code(tpm_startup(mode));
+}
+
+static int do_tpm_nv_define_space(cmd_tbl_t *cmdtp, int flag,
+		int argc, char * const argv[])
+{
+	uint32_t 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 convert_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[])
 {
-	int rv = 0;
+	uint32_t index, count, rc;
+	void *data;
 
-	if (argc < 7 && repeat_count == 0) {
-		puts("command should be@least six bytes in size\n");
-		return -1;
+	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);
 	}
 
-	if (repeat_count > 0) {
-		rv = tpm_process_stress(repeat_count);
-		return rv;
+	return convert_return_code(rc);
+}
+
+static int do_tpm_nv_write_value(cmd_tbl_t *cmdtp, int flag,
+		int argc, char * const argv[])
+{
+	uint32_t 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;
 	}
 
-	if (tis_init()) {
-		puts("tis_init() failed!\n");
-		return -1;
+	rc = tpm_nv_write_value(index, data, count);
+	free(data);
+
+	return convert_return_code(rc);
+}
+
+static int do_tpm_extend(cmd_tbl_t *cmdtp, int flag,
+		int argc, char * const argv[])
+{
+	uint32_t index, rc;
+	uint8_t 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;
 	}
 
-	if (tis_open()) {
-		puts("tis_open() failed!\n");
-		return -1;
+	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));
 	}
 
-	rv = tpm_process(argc - 1, argv + 1, cmdtp);
+	return convert_return_code(rc);
+}
+
+static int do_tpm_pcr_read(cmd_tbl_t *cmdtp, int flag,
+		int argc, char * const argv[])
+{
+	uint32_t 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);
 
-	if (tis_close()) {
-		puts("tis_close() failed!\n");
-		rv = -1;
+	rc = tpm_pcr_read(index, data, count);
+	if (!rc) {
+		puts("Named PCR content:\n");
+		print_byte_string(data, count);
 	}
 
-	return rv;
+	return convert_return_code(rc);
 }
 
+static int do_tpm_tsc_physical_presence(cmd_tbl_t *cmdtp, int flag,
+		int argc, char * const argv[])
+{
+	uint16_t presence;
+
+	if (argc != 2)
+		return CMD_RET_USAGE;
+	presence = (uint16_t)simple_strtoul(argv[1], NULL, 0);
 
-static int do_tpm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
+	return convert_return_code(tpm_tsc_physical_presence(presence));
+}
+
+static int do_tpm_read_pubek(cmd_tbl_t *cmdtp, int flag,
+		int argc, char * const argv[])
 {
-	return do_tpm_many(cmdtp, flag, argc, argv, 0);
+	uint32_t 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 convert_return_code(rc);
 }
 
+static int do_tpm_physical_set_deactivated(cmd_tbl_t *cmdtp, int flag,
+		int argc, char * const argv[])
+{
+	uint8_t state;
+
+	if (argc != 2)
+		return CMD_RET_USAGE;
+	state = (uint8_t)simple_strtoul(argv[1], NULL, 0);
 
-U_BOOT_CMD(tpm, MAX_TRANSACTION_SIZE, 1, do_tpm,
-	   "<byte> [<byte> ...]   - write data and read response",
-	   "send arbitrary data (at least 6 bytes) to the TPM "
-	   "device and read the response"
-);
+	return convert_return_code(tpm_physical_set_deactivated(state));
+}
+
+static int do_tpm_get_capability(cmd_tbl_t *cmdtp, int flag,
+		int argc, char * const argv[])
+{
+	uint32_t 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 convert_return_code(rc);
+}
 
-static int do_tpm_stress(cmd_tbl_t *cmdtp, int flag,
-			 int argc, char * const argv[])
+#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 convert_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 do_tpm_raw_transfer(cmd_tbl_t *cmdtp, int flag,
+		int argc, char * const argv[])
+{
+	void *command;
+	uint8_t response[1024];
+	size_t count, response_length = sizeof(response);
+	uint32_t 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 = tis_sendrecv(command, count, response, &response_length);
+	free(command);
+	if (!rc) {
+		puts("tpm response:\n");
+		print_byte_string(response, response_length);
+	}
+
+	return convert_return_code(rc);
+}
+
+static int do_tpm_nv_define(cmd_tbl_t *cmdtp, int flag,
+		int argc, char * const argv[])
+{
+	uint32_t 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 convert_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[])
 {
-	long unsigned int n;
-	int rv;
+	uint32_t index, count, err;
+	void *data;
 
-	if (argc != 2) {
-		puts("usage: tpm_stress <count>\n");
-		return -1;
+	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;
 	}
 
-	rv = strict_strtoul(argv[1], 10, &n);
-	if (rv) {
-		puts("tpm_stress: bad count");
-		return -1;
+	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 convert_return_code(err);
+}
+
+static int do_tpm_nv_write(cmd_tbl_t *cmdtp, int flag,
+		int argc, char * const argv[])
+{
+	uint32_t 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 convert_return_code(err);
+}
+
+#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(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, "", ""),
+};
+
+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 do_tpm_many(cmdtp, flag, argc, argv, n);
+	return tpm_cmd->cmd(cmdtp, flag, argc - 1, argv + 1);
 }
 
-U_BOOT_CMD(tpm_stress, 2, 1, do_tpm_stress,
-	   "<n>   - stress-test communication with TPM",
-	   "Repeat a TPM transaction (request-response) N times"
+U_BOOT_CMD(tpm, CONFIG_SYS_MAXARGS, 1, do_tpm,
+"Issue a TPM command",
+"cmd args...\n"
+"    - Issue TPM command <cmd> with arguments <args...>.\n"
+"Admin Startup and State Commands:\n"
+"  init\n"
+"    - Put TPM into a state where it waits for 'startup' command.\n"
+"  startup mode\n"
+"    - Issue TPM_Starup command.  <mode> 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 <flags>.\n"
+"The Capability Commands:\n"
+"  get_capability cap_area sub_cap addr count\n"
+"    - Read <count> bytes of TPM capability indexed by <cap_area> and\n"
+"      <sub_cap> to memory address <addr>.\n"
+"Endorsement Key Handling Commands:\n"
+"  read_pubek addr count\n"
+"    - Read <count> bytes of the public endorsement key to memory\n"
+"      address <addr>\n"
+"Integrity Collection and Reporting Commands:\n"
+"  extend index digest_hex_string\n"
+"    - Add a new measurement to a PCR.  Update PCR <index> with the 20-bytes\n"
+"      <digest_hex_string>\n"
+"  pcr_read index addr count\n"
+"    - Read <count> bytes from PCR <index> to memory address <addr>.\n"
+"Non-volatile Storage Commands:\n"
+"  nv_define_space index permission size\n"
+"    - Establish a space at index <index> with <permission> of <size> bytes.\n"
+"  nv_read_value index addr count\n"
+"    - Read <count> bytes from space <index> to memory address <addr>.\n"
+"  nv_write_value index addr count\n"
+"    - Write <count> bytes from memory address <addr> to space <index>.\n"
+"Miscellaneous helper functions:\n"
+"  raw_transfer byte_string\n"
+"    - Send a byte string <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 <index> with permission <perm>.\n"
+"  nv_read types_string index vars...\n"
+"    - Read from space <index> to environment variables <vars...>.\n"
+"  nv_write types_string index values...\n"
+"    - Write to space <index> from values <values...>.\n"
 );
include/tis.h renamed from include/tpm.h +5 -3
similarity index 95%
copy from include/tpm.h
copy to include/tis.h
index 6b21e9c..89e5730 100644
@@ -20,11 +20,13 @@
  * MA 02111-1307 USA
  */
 
-#ifndef _INCLUDE_TPM_H_
-#define _INCLUDE_TPM_H_
+#ifndef __TIS_H
+#define __TIS_H
 
 #include <common.h>
 
+/* Low-level interface to access TPM */
+
 /*
  * tis_init()
  *
@@ -68,4 +70,4 @@ int tis_close(void);
 int tis_sendrecv(const uint8_t *sendbuf, size_t send_size, uint8_t *recvbuf,
 			size_t *recv_len);
 
-#endif /* _INCLUDE_TPM_H_ */
+#endif /* __TIS_H */
include/tpm.h +165 -32

222 lines not on this page · raw message

lib/Makefile +1 -0
index 86ca1a6..d07d40b 100644
@@ -53,6 +53,7 @@ COBJS-y += qsort.o
 COBJS-$(CONFIG_SHA1) += sha1.o
 COBJS-$(CONFIG_SHA256) += sha256.o
 COBJS-y	+= strmhz.o
+COBJS-$(CONFIG_TPM) += tpm.o
 COBJS-$(CONFIG_RBTREE)	+= rbtree.o
 endif
 
lib/tpm.c new file +569 -0

569 lines not on this page · raw message

Signature · 1 line
1.8.1
#3 lore raw

PATCH v2 1/1 tpm: Add TPM command library

Hi,

While digging through the code, some question arises. So let me drop some notes about the patch:

( I apologize for some weird word wraps in the quoted text, but I have to use
a well known UMA which seems to be too stupid to keep lines of text without additional
word wraps...)

Quoted 44 lines

-----Original Message-----
From: u-boot-bounces at lists.denx.de [mailto:u-boot-bounces at lists.denx.de] On Behalf Of Che-Liang Chiou
Sent: Friday, February 15, 2013 12:01 AM
To: u-boot at lists.denx.de
Subject: [U-Boot] [PATCH v2] tpm: Add TPM command library

TPM command library implements a subset of TPM commands defined in TCG Main Specification 1.2 that are useful for implementing secure boot. More TPM commands could be added out of necessity.

You may exercise these commands through the 'tpm' command. However, the
raw TPM commands are too primitive for writing secure boot in command interpreter scripts; so the 'tpm' command also provides helper functions
to make scripting easier.

For example, to define a counter in TPM non-volatile storage and initialize it to zero:

$ tpm init
$ tpm startup TPM_ST_CLEAR
$ tpm nv_define d 0x1001 0x1
$ tpm nv_write d 0x1001 0

And then increment the counter by one:

$ tpm nv_read d 0x1001 i
$ setexpr.l i $i + 1
$ tpm nv_write d 0x1001 $i
---
 common/cmd_tpm.c         | 709 
+++++++++++++++++++++++++++++++++++++++--------
 include/{tpm.h => tis.h} |   8 +-
 include/tpm.h            | 197 ++++++++++---
 lib/Makefile             |   1 +
 lib/tpm.c                | 569 +++++++++++++++++++++++++++++++++++++
 5 files changed, 1339 insertions(+), 145 deletions(-)
 copy include/{tpm.h => tis.h} (95%)
 create mode 100644 lib/tpm.c

[snip]

Quoted 5 lines
lib/tpm.c new file +0 -0
new file mode 100644
index 0000000..7d13951

[snip]

Quoted 9 lines
lib/tpm.c new file +9 -0
+/**
+ * Send a TPM command and return response's return code.
+ *
+ * @param command	byte string of TPM command
+ * @return return code of the TPM response
+ */
+static uint32_t tpm_send_command(const void *command)
+{
+	uint8_t response[COMMAND_BUFFER_SIZE];

lib/tpm.c line 227

Quoted 10 lines
lib/tpm.c new file +10 -0
+	size_t response_length = sizeof(response);
+	uint32_t err;
+
+	err = tis_sendrecv(command, tpm_command_size(command),
+			response, &response_length);
+	if (err)
+		return err;
+
+	return tpm_return_code(response);
+}

Using the result of tis_sendrecv would be OK with the I2C-TPM patch posted on this ML about a year ago.
The implementation of tis_sendrecv in generic_lpc_tpm.c returns the value (1) on error instead of -1
(as documented in the header file). Since 1 is a well defined TPM result code (TPM_AUTHFAIL) this could
result in some problems.
(But I think, this is a bug in the current generic_lpc_tpm driver...)

[snip]

Quoted 14 lines
lib/tpm.c new file +2 -0
+
+uint32_t tpm_nv_read_value(uint32_t index, void *data, 

uint32_t count)

lib/tpm.c new file +11 -0
+{
+	const uint8_t command[22] = {
+		0x0, 0xc1, 0x0, 0x0, 0x0, 0x16, 0x0, 0x0, 0x0, 0xcf,
+	};
+	const size_t index_offset = 10;
+	const size_t length_offset = 18;
+	const size_t data_size_offset = 10;
+	const size_t data_offset = 14;
+	uint8_t buf[COMMAND_BUFFER_SIZE], response[COMMAND_BUFFER_SIZE];
+	uint32_t response_length = sizeof(response), data_size;
+	uint32_t err;

lib/tpm.c line 336

Quoted 10 lines
lib/tpm.c new file +10 -0
+
+	if (pack_byte_string(buf, sizeof(buf), "sdd",
+				0, command, sizeof(command),
+				index_offset, index,
+				length_offset, count))
+		return TPM_LIB_ERROR;
+	err = tis_sendrecv(buf, tpm_command_size(buf),
+			response, &response_length);
+	if (err)
+		return err;

At this point we should add something like:

	err = tpm_return_code(response);
	if (err)
		return err;

to return the "real" result code from the TPM in case of an error. Else the following code will map all TPM errors to TPM_LIB_ERROR (since unpack_byte_string will fail).

Same in the other funcs which interpret the result of the command (tpm_extend, tpm_pcr_read, rtpm_read_pubek, tpm_get_capability).

Quoted 32 lines
lib/tpm.c new file +13 -0
+	if (unpack_byte_string(response, response_length, "d",
+				data_size_offset, &data_size))
+		return TPM_LIB_ERROR;
+	if (data_size > count)
+		return TPM_LIB_ERROR;
+	if (unpack_byte_string(response, response_length, "s",
+				data_offset, data, data_size))
+		return TPM_LIB_ERROR;
+
+	return 0;
+}
+
+uint32_t tpm_nv_write_value(uint32_t index, const void 

*data, uint32_t length)

lib/tpm.c new file +18 -0
+{
+	const uint8_t command[256] = {
+		0x0, 0xc1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xcd,
+	};
+	const size_t command_size_offset = 2;
+	const size_t index_offset = 10;
+	const size_t length_offset = 18;
+	const size_t data_offset = 22;
+	const size_t write_info_size = 12;
+	const uint32_t total_length =
+		TPM_REQUEST_HEADER_LENGTH + write_info_size + length;
+	uint8_t buf[COMMAND_BUFFER_SIZE], response[COMMAND_BUFFER_SIZE];
+	uint32_t response_length = sizeof(response);
+	uint32_t err;
+
+	if (pack_byte_string(buf, sizeof(buf), "sddds",
+				0, command, sizeof(command),
+				command_size_offset, total_length,

lib/tpm.c line 377

Quoted 10 lines
lib/tpm.c new file +10 -0
+				index_offset, index,
+				length_offset, length,
+				data_offset, data, length))
+		return TPM_LIB_ERROR;
+	err = tis_sendrecv(buf, tpm_command_size(buf),
+			response, &response_length);
+	if (err)
+		return err;
+
+	return 0;

Why not just call tpm_send_command(buf) here like in the other funcs where the
Result does not need further interpretation (beside the return code)?

Beside theese remarks: I like Your patch, since one of our upcoming boards also uses
a TPM to check system integrity at boot time :-)

Greetings,
Reinhard Pfau.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/ms-tnef
Size: 5088 bytes
Desc: not available
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20130228/fb2e6c5b/attachment.bin>

#4 lore raw

PATCH v2 1/1 tpm: Add TPM command library

Hi Reinhard,

Replied below.

Quoted 92 lines · Reinhard

Hi,

While digging through the code, some question arises. So let me drop some notes about the patch:

( I apologize for some weird word wraps in the quoted text, but I have to use
a well known UMA which seems to be too stupid to keep lines of text without additional
word wraps...)

Quoted 44 lines

-----Original Message-----
From: u-boot-bounces at lists.denx.de [mailto:u-boot-bounces at lists.denx.de] On Behalf Of Che-Liang Chiou
Sent: Friday, February 15, 2013 12:01 AM
To: u-boot at lists.denx.de
Subject: [U-Boot] [PATCH v2] tpm: Add TPM command library

TPM command library implements a subset of TPM commands defined in TCG Main Specification 1.2 that are useful for implementing secure boot. More TPM commands could be added out of necessity.

You may exercise these commands through the 'tpm' command. However, the
raw TPM commands are too primitive for writing secure boot in command interpreter scripts; so the 'tpm' command also provides helper functions
to make scripting easier.

For example, to define a counter in TPM non-volatile storage and initialize it to zero:

$ tpm init
$ tpm startup TPM_ST_CLEAR
$ tpm nv_define d 0x1001 0x1
$ tpm nv_write d 0x1001 0

And then increment the counter by one:

$ tpm nv_read d 0x1001 i
$ setexpr.l i $i + 1
$ tpm nv_write d 0x1001 $i
---
 common/cmd_tpm.c         | 709
+++++++++++++++++++++++++++++++++++++++--------
 include/{tpm.h => tis.h} |   8 +-
 include/tpm.h            | 197 ++++++++++---
 lib/Makefile             |   1 +
 lib/tpm.c                | 569 +++++++++++++++++++++++++++++++++++++
 5 files changed, 1339 insertions(+), 145 deletions(-)
 copy include/{tpm.h => tis.h} (95%)
 create mode 100644 lib/tpm.c

[snip]

Quoted 5 lines
lib/tpm.c new file +0 -0
new file mode 100644
index 0000000..7d13951

[snip]

Quoted 9 lines
lib/tpm.c new file +9 -0
+/**
+ * Send a TPM command and return response's return code.
+ *
+ * @param command    byte string of TPM command
+ * @return return code of the TPM response
+ */
+static uint32_t tpm_send_command(const void *command)
+{
+     uint8_t response[COMMAND_BUFFER_SIZE];

lib/tpm.c line 227

Quoted 10 lines
lib/tpm.c new file +10 -0
+     size_t response_length = sizeof(response);
+     uint32_t err;
+
+     err = tis_sendrecv(command, tpm_command_size(command),
+                     response, &response_length);
+     if (err)
+             return err;
+
+     return tpm_return_code(response);
+}

Using the result of tis_sendrecv would be OK with the I2C-TPM patch posted on this ML about a year ago.
The implementation of tis_sendrecv in generic_lpc_tpm.c returns the value (1) on error instead of -1
(as documented in the header file). Since 1 is a well defined TPM result code (TPM_AUTHFAIL) this could
result in some problems.
(But I think, this is a bug in the current generic_lpc_tpm driver...)

Quoted 2 lines

[snip]

I agree it is a bug in the current generic_lpc_tpm driver. And here tpm_send_command() should return TPM_LIB_ERROR (which is a distinguishable value from well-defined TPM result codes) in case of tis_sendrecv() returning non-zero value.

Quoted 31 lines
Quoted 14 lines
lib/tpm.c new file +2 -0
+
+uint32_t tpm_nv_read_value(uint32_t index, void *data,

uint32_t count)

lib/tpm.c new file +11 -0
+{
+     const uint8_t command[22] = {
+             0x0, 0xc1, 0x0, 0x0, 0x0, 0x16, 0x0, 0x0, 0x0, 0xcf,
+     };
+     const size_t index_offset = 10;
+     const size_t length_offset = 18;
+     const size_t data_size_offset = 10;
+     const size_t data_offset = 14;
+     uint8_t buf[COMMAND_BUFFER_SIZE], response[COMMAND_BUFFER_SIZE];
+     uint32_t response_length = sizeof(response), data_size;
+     uint32_t err;

lib/tpm.c line 336

Quoted 10 lines
lib/tpm.c new file +10 -0
+
+     if (pack_byte_string(buf, sizeof(buf), "sdd",
+                             0, command, sizeof(command),
+                             index_offset, index,
+                             length_offset, count))
+             return TPM_LIB_ERROR;
+     err = tis_sendrecv(buf, tpm_command_size(buf),
+                     response, &response_length);
+     if (err)
+             return err;

At this point we should add something like:

        err = tpm_return_code(response);
        if (err)
                return err;
Quoted 7 lines

to return the "real" result code from the TPM in case of an error. Else the following code will map all TPM errors to TPM_LIB_ERROR (since unpack_byte_string will fail).

Same in the other funcs which interpret the result of the command (tpm_extend, tpm_pcr_read, rtpm_read_pubek, tpm_get_capability).

Done.

Quoted 38 lines
Quoted 38 lines
lib/tpm.c new file +13 -0
+     if (unpack_byte_string(response, response_length, "d",
+                             data_size_offset, &data_size))
+             return TPM_LIB_ERROR;
+     if (data_size > count)
+             return TPM_LIB_ERROR;
+     if (unpack_byte_string(response, response_length, "s",
+                             data_offset, data, data_size))
+             return TPM_LIB_ERROR;
+
+     return 0;
+}
+
+uint32_t tpm_nv_write_value(uint32_t index, const void

*data, uint32_t length)

lib/tpm.c new file +24 -0
+{
+     const uint8_t command[256] = {
+             0x0, 0xc1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xcd,
+     };
+     const size_t command_size_offset = 2;
+     const size_t index_offset = 10;
+     const size_t length_offset = 18;
+     const size_t data_offset = 22;
+     const size_t write_info_size = 12;
+     const uint32_t total_length =
+             TPM_REQUEST_HEADER_LENGTH + write_info_size + length;
+     uint8_t buf[COMMAND_BUFFER_SIZE], response[COMMAND_BUFFER_SIZE];
+     uint32_t response_length = sizeof(response);
+     uint32_t err;
+
+     if (pack_byte_string(buf, sizeof(buf), "sddds",
+                             0, command, sizeof(command),
+                             command_size_offset, total_length,
+                             index_offset, index,
+                             length_offset, length,
+                             data_offset, data, length))
+             return TPM_LIB_ERROR;
+     err = tis_sendrecv(buf, tpm_command_size(buf),
+                     response, &response_length);
Quoted 9 lines

lib/tpm.c line 346

Quoted 4 lines
lib/tpm.c new file +4 -0
+     if (err)
+             return err;
+
+     return 0;

Why not just call tpm_send_command(buf) here like in the other funcs where the
Result does not need further interpretation (beside the return code)?

Done.

Quoted 11 lines

Beside theese remarks: I like Your patch, since one of our upcoming boards also uses
a TPM to check system integrity at boot time :-)

Greetings,
Reinhard Pfau.

Regards,
Che-Liang

#5 lore raw

PATCH v3 1/1 tpm: Add TPM command library

In reply to Che-Liang Chiou

TPM command library implements a subset of TPM commands defined in TCG Main Specification 1.2 that are useful for implementing secure boot. More TPM commands could be added out of necessity.

You may exercise these commands through the 'tpm' command. However, the raw TPM commands are too primitive for writing secure boot in command interpreter scripts; so the 'tpm' command also provides helper functions to make scripting easier.

For example, to define a counter in TPM non-volatile storage and initialize it to zero:

$ tpm init
$ tpm startup TPM_ST_CLEAR
$ tpm nv_define d 0x1001 0x1
$ tpm nv_write d 0x1001 0

And then increment the counter by one:

$ tpm nv_read d 0x1001 i
$ setexpr.l i $i + 1
$ tpm nv_write d 0x1001 $i

---
Changes in v3:
- Add check of TPM response code

Changes in v2:
- Merge tpm_util into tpm

 common/cmd_tpm.c         | 709 +++++++++++++++++++++++++++++++++++++++--------
 include/{tpm.h => tis.h} |   8 +-
 include/tpm.h            | 197 ++++++++++---
 lib/Makefile             |   1 +
 lib/tpm.c                | 581 ++++++++++++++++++++++++++++++++++++++
 5 files changed, 1351 insertions(+), 145 deletions(-)
 copy include/{tpm.h => tis.h} (95%)
 create mode 100644 lib/tpm.c
common/cmd_tpm.c +599 -110
index 0970a6f..46fae18 100644
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2011 The Chromium OS Authors.
+ * Copyright (c) 2013 The Chromium OS Authors.
  *
  * See file CREDITS for list of people who contributed to this
  * project.
@@ -22,163 +22,652 @@
 
 #include <common.h>
 #include <command.h>
+#include <malloc.h>
 #include <tpm.h>
+#include <asm/unaligned.h>
+#include <linux/string.h>
 
-#define MAX_TRANSACTION_SIZE 30
+/**
+ * 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(uint8_t *data, size_t count)
+{
+	int i, print_newline = 0;
 
-/*
- * tpm_write() expects a variable number of parameters: the internal address
- * followed by data to write, byte by byte.
+	for (i = 0; i < count; i++) {
+		printf(" %02x", data[i]);
+		print_newline = (i % 16 == 15);
+		if (print_newline)
+			putc('\n');
+	}
+	/* Avoid duplicated newline@the end */
+	if (!print_newline)
+		putc('\n');
+}
+
+/**
+ * Convert a text string of hexdecimal values into a byte string.
  *
- * Returns 0 on success or -1 on errors (wrong arguments or TPM failure).
+ * @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 int tpm_process(int argc, char * const argv[], cmd_tbl_t *cmdtp)
-{
-	u8 tpm_buffer[MAX_TRANSACTION_SIZE];
-	u32 write_size, read_size;
-	char *p;
-	int rv = -1;
-
-	for (write_size = 0; write_size < argc; write_size++) {
-		u32 datum = simple_strtoul(argv[write_size], &p, 0);
-		if (*p || (datum > 0xff)) {
-			printf("\n%s: bad data value\n\n", argv[write_size]);
-			cmd_usage(cmdtp);
-			return rv;
-		}
-		tpm_buffer[write_size] = (u8)datum;
+static void *parse_byte_string(char *bytes, uint8_t *data, size_t *count_ptr)
+{
+	char byte[3];
+	size_t count, length;
+	int i;
+
+	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] = (uint8_t)simple_strtoul(byte, NULL, 16);
 	}
 
-	read_size = sizeof(tpm_buffer);
-	if (!tis_sendrecv(tpm_buffer, write_size, tpm_buffer, &read_size)) {
-		int i;
-		puts("Got TPM response:\n");
-		for (i = 0; i < read_size; i++)
-			printf(" %2.2x", tpm_buffer[i]);
-		puts("\n");
-		rv = 0;
-	} else {
-		puts("tpm command failed\n");
+	if (count_ptr)
+		*count_ptr = count;
+
+	return data;
+}
+
+/**
+ * Convert TPM command return code to U-Boot command error codes.
+ *
+ * @param return_code	TPM command return code
+ * @return value of enum command_ret_t
+ */
+static int convert_return_code(uint32_t return_code)
+{
+	if (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 rv;
+
+	return size;
 }
 
-#define CHECK(exp) do {							\
-		int _rv = exp;						\
-		if (_rv) {						\
-			printf("CHECK: %s %d %x\n", #exp, __LINE__, _rv);\
-		}							\
-	} while (0)
+/**
+ * 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, uint32_t *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;
 
-static int tpm_process_stress(int repeat_count)
+	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[],
+		uint8_t *data)
 {
-	int i;
-	int rv = 0;
-	u8 request[] = {0x0, 0xc1,
-			0x0, 0x0, 0x0, 0x16,
-			0x0, 0x0, 0x0, 0x65,
-			0x0, 0x0, 0x0, 0x4,
-			0x0, 0x0, 0x0, 0x4,
-			0x0, 0x0, 0x1, 0x9};
-	u8 response[MAX_TRANSACTION_SIZE];
-	u32 rlength = MAX_TRANSACTION_SIZE;
-
-	CHECK(tis_init());
-
-	for (i = 0; i < repeat_count; i++) {
-		CHECK(tis_open());
-		rv = tis_sendrecv(request, sizeof(request), response, &rlength);
-		if (rv) {
-			printf("tpm test failed at step %d with 0x%x\n", i, rv);
-			CHECK(tis_close());
+	size_t offset;
+	uint32_t 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;
 		}
-		CHECK(tis_close());
-		if ((response[6] || response[7] || response[8] || response[9])
-		    && response[9] != 0x26) {
-			/* Ignore postinit errors */
-			printf("tpm command failed at step %d\n"
-			       "tpm error code: %02x%02x%02x%02x\n", i,
-			       response[6], response[7],
-			       response[8], response[9]);
-			rv = -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, uint8_t *data,
+		char * const vars[])
+{
+	size_t offset;
+	uint32_t 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 (setenv_ulong(*vars, value))
+			return -1;
 	}
-	return rv;
+
+	return 0;
 }
 
+static int do_tpm_startup(cmd_tbl_t *cmdtp, int flag,
+		int argc, char * const argv[])
+{
+	enum tpm_startup_type mode;
 
-static int do_tpm_many(cmd_tbl_t *cmdtp, int flag,
-		       int argc, char * const argv[], int repeat_count)
+	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 convert_return_code(tpm_startup(mode));
+}
+
+static int do_tpm_nv_define_space(cmd_tbl_t *cmdtp, int flag,
+		int argc, char * const argv[])
+{
+	uint32_t 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 convert_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[])
 {
-	int rv = 0;
+	uint32_t index, count, rc;
+	void *data;
 
-	if (argc < 7 && repeat_count == 0) {
-		puts("command should be@least six bytes in size\n");
-		return -1;
+	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);
 	}
 
-	if (repeat_count > 0) {
-		rv = tpm_process_stress(repeat_count);
-		return rv;
+	return convert_return_code(rc);
+}
+
+static int do_tpm_nv_write_value(cmd_tbl_t *cmdtp, int flag,
+		int argc, char * const argv[])
+{
+	uint32_t 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;
 	}
 
-	if (tis_init()) {
-		puts("tis_init() failed!\n");
-		return -1;
+	rc = tpm_nv_write_value(index, data, count);
+	free(data);
+
+	return convert_return_code(rc);
+}
+
+static int do_tpm_extend(cmd_tbl_t *cmdtp, int flag,
+		int argc, char * const argv[])
+{
+	uint32_t index, rc;
+	uint8_t 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;
 	}
 
-	if (tis_open()) {
-		puts("tis_open() failed!\n");
-		return -1;
+	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));
 	}
 
-	rv = tpm_process(argc - 1, argv + 1, cmdtp);
+	return convert_return_code(rc);
+}
+
+static int do_tpm_pcr_read(cmd_tbl_t *cmdtp, int flag,
+		int argc, char * const argv[])
+{
+	uint32_t 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);
 
-	if (tis_close()) {
-		puts("tis_close() failed!\n");
-		rv = -1;
+	rc = tpm_pcr_read(index, data, count);
+	if (!rc) {
+		puts("Named PCR content:\n");
+		print_byte_string(data, count);
 	}
 
-	return rv;
+	return convert_return_code(rc);
 }
 
+static int do_tpm_tsc_physical_presence(cmd_tbl_t *cmdtp, int flag,
+		int argc, char * const argv[])
+{
+	uint16_t presence;
+
+	if (argc != 2)
+		return CMD_RET_USAGE;
+	presence = (uint16_t)simple_strtoul(argv[1], NULL, 0);
 
-static int do_tpm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
+	return convert_return_code(tpm_tsc_physical_presence(presence));
+}
+
+static int do_tpm_read_pubek(cmd_tbl_t *cmdtp, int flag,
+		int argc, char * const argv[])
 {
-	return do_tpm_many(cmdtp, flag, argc, argv, 0);
+	uint32_t 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 convert_return_code(rc);
 }
 
+static int do_tpm_physical_set_deactivated(cmd_tbl_t *cmdtp, int flag,
+		int argc, char * const argv[])
+{
+	uint8_t state;
+
+	if (argc != 2)
+		return CMD_RET_USAGE;
+	state = (uint8_t)simple_strtoul(argv[1], NULL, 0);
 
-U_BOOT_CMD(tpm, MAX_TRANSACTION_SIZE, 1, do_tpm,
-	   "<byte> [<byte> ...]   - write data and read response",
-	   "send arbitrary data (at least 6 bytes) to the TPM "
-	   "device and read the response"
-);
+	return convert_return_code(tpm_physical_set_deactivated(state));
+}
+
+static int do_tpm_get_capability(cmd_tbl_t *cmdtp, int flag,
+		int argc, char * const argv[])
+{
+	uint32_t 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 convert_return_code(rc);
+}
 
-static int do_tpm_stress(cmd_tbl_t *cmdtp, int flag,
-			 int argc, char * const argv[])
+#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 convert_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 do_tpm_raw_transfer(cmd_tbl_t *cmdtp, int flag,
+		int argc, char * const argv[])
+{
+	void *command;
+	uint8_t response[1024];
+	size_t count, response_length = sizeof(response);
+	uint32_t 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 = tis_sendrecv(command, count, response, &response_length);
+	free(command);
+	if (!rc) {
+		puts("tpm response:\n");
+		print_byte_string(response, response_length);
+	}
+
+	return convert_return_code(rc);
+}
+
+static int do_tpm_nv_define(cmd_tbl_t *cmdtp, int flag,
+		int argc, char * const argv[])
+{
+	uint32_t 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 convert_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[])
 {
-	long unsigned int n;
-	int rv;
+	uint32_t index, count, err;
+	void *data;
 
-	if (argc != 2) {
-		puts("usage: tpm_stress <count>\n");
-		return -1;
+	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;
 	}
 
-	rv = strict_strtoul(argv[1], 10, &n);
-	if (rv) {
-		puts("tpm_stress: bad count");
-		return -1;
+	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 convert_return_code(err);
+}
+
+static int do_tpm_nv_write(cmd_tbl_t *cmdtp, int flag,
+		int argc, char * const argv[])
+{
+	uint32_t 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 convert_return_code(err);
+}
+
+#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(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, "", ""),
+};
+
+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 do_tpm_many(cmdtp, flag, argc, argv, n);
+	return tpm_cmd->cmd(cmdtp, flag, argc - 1, argv + 1);
 }
 
-U_BOOT_CMD(tpm_stress, 2, 1, do_tpm_stress,
-	   "<n>   - stress-test communication with TPM",
-	   "Repeat a TPM transaction (request-response) N times"
+U_BOOT_CMD(tpm, CONFIG_SYS_MAXARGS, 1, do_tpm,
+"Issue a TPM command",
+"cmd args...\n"
+"    - Issue TPM command <cmd> with arguments <args...>.\n"
+"Admin Startup and State Commands:\n"
+"  init\n"
+"    - Put TPM into a state where it waits for 'startup' command.\n"
+"  startup mode\n"
+"    - Issue TPM_Starup command.  <mode> 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 <flags>.\n"
+"The Capability Commands:\n"
+"  get_capability cap_area sub_cap addr count\n"
+"    - Read <count> bytes of TPM capability indexed by <cap_area> and\n"
+"      <sub_cap> to memory address <addr>.\n"
+"Endorsement Key Handling Commands:\n"
+"  read_pubek addr count\n"
+"    - Read <count> bytes of the public endorsement key to memory\n"
+"      address <addr>\n"
+"Integrity Collection and Reporting Commands:\n"
+"  extend index digest_hex_string\n"
+"    - Add a new measurement to a PCR.  Update PCR <index> with the 20-bytes\n"
+"      <digest_hex_string>\n"
+"  pcr_read index addr count\n"
+"    - Read <count> bytes from PCR <index> to memory address <addr>.\n"
+"Non-volatile Storage Commands:\n"
+"  nv_define_space index permission size\n"
+"    - Establish a space at index <index> with <permission> of <size> bytes.\n"
+"  nv_read_value index addr count\n"
+"    - Read <count> bytes from space <index> to memory address <addr>.\n"
+"  nv_write_value index addr count\n"
+"    - Write <count> bytes from memory address <addr> to space <index>.\n"
+"Miscellaneous helper functions:\n"
+"  raw_transfer byte_string\n"
+"    - Send a byte string <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 <index> with permission <perm>.\n"
+"  nv_read types_string index vars...\n"
+"    - Read from space <index> to environment variables <vars...>.\n"
+"  nv_write types_string index values...\n"
+"    - Write to space <index> from values <values...>.\n"
 );
include/tis.h renamed from include/tpm.h +5 -3
similarity index 95%
copy from include/tpm.h
copy to include/tis.h
index 6b21e9c..89e5730 100644
@@ -20,11 +20,13 @@
  * MA 02111-1307 USA
  */
 
-#ifndef _INCLUDE_TPM_H_
-#define _INCLUDE_TPM_H_
+#ifndef __TIS_H
+#define __TIS_H
 
 #include <common.h>
 
+/* Low-level interface to access TPM */
+
 /*
  * tis_init()
  *
@@ -68,4 +70,4 @@ int tis_close(void);
 int tis_sendrecv(const uint8_t *sendbuf, size_t send_size, uint8_t *recvbuf,
 			size_t *recv_len);
 
-#endif /* _INCLUDE_TPM_H_ */
+#endif /* __TIS_H */
include/tpm.h +165 -32

222 lines not on this page · raw message

lib/Makefile +1 -0
index 86ca1a6..d07d40b 100644
@@ -53,6 +53,7 @@ COBJS-y += qsort.o
 COBJS-$(CONFIG_SHA1) += sha1.o
 COBJS-$(CONFIG_SHA256) += sha256.o
 COBJS-y	+= strmhz.o
+COBJS-$(CONFIG_TPM) += tpm.o
 COBJS-$(CONFIG_RBTREE)	+= rbtree.o
 endif
 
lib/tpm.c new file +581 -0

581 lines not on this page · raw message

Signature · 1 line
1.8.1