diff options
author | Tom Rini <trini@ti.com> | 2013-04-15 07:45:07 -0400 |
---|---|---|
committer | Tom Rini <trini@ti.com> | 2013-04-15 07:45:07 -0400 |
commit | 277f037074fbb73be10a7bff27079b6eb0a3bfbb (patch) | |
tree | fda7a8df71f7ac5b1a4082dd612d3f4cfd17bcf1 | |
parent | e303be2d2819699a443b5eece5a04ecc380a9b61 (diff) | |
parent | 8732b0700d21588b4954d0f1b182f4c60725d228 (diff) |
Merge branch 'tpm' of git://git.denx.de/u-boot-x86
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | common/cmd_tpm.c | 709 | ||||
-rw-r--r-- | drivers/tpm/Makefile | 4 | ||||
-rw-r--r-- | drivers/tpm/generic_lpc_tpm.c | 10 | ||||
-rw-r--r-- | drivers/tpm/slb9635_i2c/compatibility.h | 51 | ||||
-rw-r--r-- | drivers/tpm/slb9635_i2c/tpm.c | 453 | ||||
-rw-r--r-- | drivers/tpm/slb9635_i2c/tpm.h | 161 | ||||
-rw-r--r-- | drivers/tpm/slb9635_i2c/tpm_tis_i2c.c | 561 | ||||
-rw-r--r-- | drivers/tpm/tis_i2c.c | 181 | ||||
-rw-r--r-- | include/configs/exynos5250-dt.h | 7 | ||||
-rw-r--r-- | include/fdtdec.h | 1 | ||||
-rw-r--r-- | include/tis.h | 73 | ||||
-rw-r--r-- | include/tpm.h | 197 | ||||
-rw-r--r-- | lib/Makefile | 1 | ||||
-rw-r--r-- | lib/fdtdec.c | 1 | ||||
-rw-r--r-- | lib/tpm.c | 581 |
16 files changed, 2845 insertions, 148 deletions
@@ -314,7 +314,7 @@ endif LIBS-y += drivers/rtc/librtc.o LIBS-y += drivers/serial/libserial.o LIBS-y += drivers/sound/libsound.o -LIBS-$(CONFIG_GENERIC_LPC_TPM) += drivers/tpm/libtpm.o +LIBS-y += drivers/tpm/libtpm.o LIBS-y += drivers/twserial/libtws.o LIBS-y += drivers/usb/eth/libusb_eth.o LIBS-y += drivers/usb/gadget/libusb_gadget.o diff --git a/common/cmd_tpm.c b/common/cmd_tpm.c index 0970a6fc19..46fae18775 100644 --- a/common/cmd_tpm.c +++ b/common/cmd_tpm.c @@ -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 at 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 at 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" ); diff --git a/drivers/tpm/Makefile b/drivers/tpm/Makefile index be11c8b595..e8c159c0f3 100644 --- a/drivers/tpm/Makefile +++ b/drivers/tpm/Makefile @@ -23,7 +23,11 @@ include $(TOPDIR)/config.mk LIB := $(obj)libtpm.o +$(shell mkdir -p $(obj)slb9635_i2c) + COBJS-$(CONFIG_GENERIC_LPC_TPM) = generic_lpc_tpm.o +COBJS-$(CONFIG_INFINEON_TPM_I2C) += tis_i2c.o slb9635_i2c/tpm.o +COBJS-$(CONFIG_INFINEON_TPM_I2C) += slb9635_i2c/tpm_tis_i2c.o COBJS := $(COBJS-y) SRCS := $(COBJS:.o=.c) diff --git a/drivers/tpm/generic_lpc_tpm.c b/drivers/tpm/generic_lpc_tpm.c index 6c494eb98a..04ad418973 100644 --- a/drivers/tpm/generic_lpc_tpm.c +++ b/drivers/tpm/generic_lpc_tpm.c @@ -135,7 +135,7 @@ static u8 tpm_read_byte(const u8 *ptr) { u8 ret = readb(ptr); debug(PREFIX "Read reg 0x%4.4x returns 0x%2.2x\n", - (u32)ptr - (u32)lpc_tpm_dev, ret); + (u32)(uintptr_t)ptr - (u32)(uintptr_t)lpc_tpm_dev, ret); return ret; } @@ -143,21 +143,21 @@ static u32 tpm_read_word(const u32 *ptr) { u32 ret = readl(ptr); debug(PREFIX "Read reg 0x%4.4x returns 0x%8.8x\n", - (u32)ptr - (u32)lpc_tpm_dev, ret); + (u32)(uintptr_t)ptr - (u32)(uintptr_t)lpc_tpm_dev, ret); return ret; } static void tpm_write_byte(u8 value, u8 *ptr) { debug(PREFIX "Write reg 0x%4.4x with 0x%2.2x\n", - (u32)ptr - (u32)lpc_tpm_dev, value); + (u32)(uintptr_t)ptr - (u32)(uintptr_t)lpc_tpm_dev, value); writeb(value, ptr); } static void tpm_write_word(u32 value, u32 *ptr) { debug(PREFIX "Write reg 0x%4.4x with 0x%8.8x\n", - (u32)ptr - (u32)lpc_tpm_dev, value); + (u32)(uintptr_t)ptr - (u32)(uintptr_t)lpc_tpm_dev, value); writel(value, ptr); } @@ -491,5 +491,5 @@ int tis_sendrecv(const u8 *sendbuf, size_t send_size, return TPM_DRIVER_ERR; } - return tis_readresponse(recvbuf, recv_len); + return tis_readresponse(recvbuf, (u32 *)recv_len); } diff --git a/drivers/tpm/slb9635_i2c/compatibility.h b/drivers/tpm/slb9635_i2c/compatibility.h new file mode 100644 index 0000000000..62dc9fa964 --- /dev/null +++ b/drivers/tpm/slb9635_i2c/compatibility.h @@ -0,0 +1,51 @@ +/* + * Copyright (C) 2011 Infineon Technologies + * + * Authors: + * Peter Huewe <huewe.external@infineon.com> + * + * Version: 2.1.1 + * + * See file CREDITS for list of people who contributed to this + * project. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of + * the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, + * MA 02111-1307 USA + */ + +#ifndef _COMPATIBILITY_H_ +#define _COMPATIBILITY_H_ + +/* all includes from U-Boot */ +#include <linux/types.h> +#include <linux/unaligned/be_byteshift.h> +#include <asm-generic/errno.h> +#include <compiler.h> +#include <common.h> + +/* extended error numbers from linux (see errno.h) */ +#define ECANCELED 125 /* Operation Canceled */ + +#define msleep(t) udelay((t)*1000) + +/* Timer frequency. Corresponds to msec timer resolution*/ +#define HZ 1000 + +#define dev_dbg(dev, format, arg...) debug(format, ##arg) +#define dev_err(dev, format, arg...) printf(format, ##arg) +#define dev_info(dev, format, arg...) debug(format, ##arg) +#define dbg_printf debug + +#endif diff --git a/drivers/tpm/slb9635_i2c/tpm.c b/drivers/tpm/slb9635_i2c/tpm.c new file mode 100644 index 0000000000..496c48e8cf --- /dev/null +++ b/drivers/tpm/slb9635_i2c/tpm.c @@ -0,0 +1,453 @@ +/* + * Copyright (C) 2011 Infineon Technologies + * + * Authors: + * Peter Huewe <huewe.external@infineon.com> + * + * Description: + * Device driver for TCG/TCPA TPM (trusted platform module). + * Specifications at www.trustedcomputinggroup.org + * + * It is based on the Linux kernel driver tpm.c from Leendert van + * Dorn, Dave Safford, Reiner Sailer, and Kyleen Hall. + * + * Version: 2.1.1 + * + * See file CREDITS for list of people who contributed to this + * project. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation, version 2 of the + * License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, + * MA 02111-1307 USA + */ + +#include <malloc.h> +#include "tpm.h" + +/* global structure for tpm chip data */ +struct tpm_chip g_chip; + +enum tpm_duration { + TPM_SHORT = 0, + TPM_MEDIUM = 1, + TPM_LONG = 2, + TPM_UNDEFINED, +}; + +#define TPM_MAX_ORDINAL 243 +#define TPM_MAX_PROTECTED_ORDINAL 12 +#define TPM_PROTECTED_ORDINAL_MASK 0xFF + +/* + * Array with one entry per ordinal defining the maximum amount + * of time the chip could take to return the result. The ordinal + * designation of short, medium or long is defined in a table in + * TCG Specification TPM Main Part 2 TPM Structures Section 17. The + * values of the SHORT, MEDIUM, and LONG durations are retrieved + * from the chip during initialization with a call to tpm_get_timeouts. + */ +static const u8 tpm_protected_ordinal_duration[TPM_MAX_PROTECTED_ORDINAL] = { + TPM_UNDEFINED, /* 0 */ + TPM_UNDEFINED, + TPM_UNDEFINED, + TPM_UNDEFINED, + TPM_UNDEFINED, + TPM_UNDEFINED, /* 5 */ + TPM_UNDEFINED, + TPM_UNDEFINED, + TPM_UNDEFINED, + TPM_UNDEFINED, + TPM_SHORT, /* 10 */ + TPM_SHORT, +}; + +static const u8 tpm_ordinal_duration[TPM_MAX_ORDINAL] = { + TPM_UNDEFINED, /* 0 */ + TPM_UNDEFINED, + TPM_UNDEFINED, + TPM_UNDEFINED, + TPM_UNDEFINED, + TPM_UNDEFINED, /* 5 */ + TPM_UNDEFINED, + TPM_UNDEFINED, + TPM_UNDEFINED, + TPM_UNDEFINED, + TPM_SHORT, /* 10 */ + TPM_SHORT, + TPM_MEDIUM, + TPM_LONG, + TPM_LONG, + TPM_MEDIUM, /* 15 */ + TPM_SHORT, + TPM_SHORT, + TPM_MEDIUM, + TPM_LONG, + TPM_SHORT, /* 20 */ + TPM_SHORT, + TPM_MEDIUM, + TPM_MEDIUM, + TPM_MEDIUM, + TPM_SHORT, /* 25 */ + TPM_SHORT, + TPM_MEDIUM, + TPM_SHORT, + TPM_SHORT, + TPM_MEDIUM, /* 30 */ + TPM_LONG, + TPM_MEDIUM, + TPM_SHORT, + TPM_SHORT, + TPM_SHORT, /* 35 */ + TPM_MEDIUM, + TPM_MEDIUM, + TPM_UNDEFINED, + TPM_UNDEFINED, + TPM_MEDIUM, /* 40 */ + TPM_LONG, + TPM_MEDIUM, + TPM_SHORT, + TPM_SHORT, + TPM_SHORT, /* 45 */ + TPM_SHORT, + TPM_SHORT, + TPM_SHORT, + TPM_LONG, + TPM_MEDIUM, /* 50 */ + TPM_MEDIUM, + TPM_UNDEFINED, + TPM_UNDEFINED, + TPM_UNDEFINED, + TPM_UNDEFINED, /* 55 */ + TPM_UNDEFINED, + TPM_UNDEFINED, + TPM_UNDEFINED, + TPM_UNDEFINED, + TPM_MEDIUM, /* 60 */ + TPM_MEDIUM, + TPM_MEDIUM, + TPM_SHORT, + TPM_SHORT, + TPM_MEDIUM, /* 65 */ + TPM_UNDEFINED, + TPM_UNDEFINED, + TPM_UNDEFINED, + TPM_UNDEFINED, + TPM_SHORT, /* 70 */ + TPM_SHORT, + TPM_UNDEFINED, + TPM_UNDEFINED, + TPM_UNDEFINED, + TPM_UNDEFINED, /* 75 */ + TPM_UNDEFINED, + TPM_UNDEFINED, + TPM_UNDEFINED, + TPM_UNDEFINED, + TPM_LONG, /* 80 */ + TPM_UNDEFINED, + TPM_MEDIUM, + TPM_LONG, + TPM_SHORT, + TPM_UNDEFINED, /* 85 */ + TPM_UNDEFINED, + TPM_UNDEFINED, + TPM_UNDEFINED, + TPM_UNDEFINED, + TPM_SHORT, /* 90 */ + TPM_SHORT, + TPM_SHORT, + TPM_SHORT, + TPM_SHORT, + TPM_UNDEFINED, /* 95 */ + TPM_UNDEFINED, + TPM_UNDEFINED, + TPM_UNDEFINED, + TPM_UNDEFINED, + TPM_MEDIUM, /* 100 */ + TPM_SHORT, + TPM_SHORT, + TPM_UNDEFINED, + TPM_UNDEFINED, + TPM_UNDEFINED, /* 105 */ + TPM_UNDEFINED, + TPM_UNDEFINED, + TPM_UNDEFINED, + TPM_UNDEFINED, + TPM_SHORT, /* 110 */ + TPM_SHORT, + TPM_SHORT, + TPM_SHORT, + TPM_SHORT, + TPM_SHORT, /* 115 */ + TPM_SHORT, + TPM_SHORT, + TPM_UNDEFINED, + TPM_UNDEFINED, + TPM_LONG, /* 120 */ + TPM_LONG, + TPM_MEDIUM, + TPM_UNDEFINED, + TPM_SHORT, + TPM_SHORT, /* 125 */ + TPM_SHORT, + TPM_LONG, + TPM_SHORT, + TPM_SHORT, + TPM_SHORT, /* 130 */ + TPM_MEDIUM, + TPM_UNDEFINED, + TPM_SHORT, + TPM_MEDIUM, + TPM_UNDEFINED, /* 135 */ + TPM_UNDEFINED, + TPM_UNDEFINED, + TPM_UNDEFINED, + TPM_UNDEFINED, + TPM_SHORT, /* 140 */ + TPM_SHORT, + TPM_UNDEFINED, + TPM_UNDEFINED, + TPM_UNDEFINED, + TPM_UNDEFINED, /* 145 */ + TPM_UNDEFINED, + TPM_UNDEFINED, + TPM_UNDEFINED, + TPM_UNDEFINED, + TPM_SHORT, /* 150 */ + TPM_MEDIUM, + TPM_MEDIUM, + TPM_SHORT, + TPM_SHORT, + TPM_UNDEFINED, /* 155 */ + TPM_UNDEFINED, + TPM_UNDEFINED, + TPM_UNDEFINED, + TPM_UNDEFINED, + TPM_SHORT, /* 160 */ + TPM_SHORT, + TPM_SHORT, + TPM_SHORT, + TPM_UNDEFINED, + TPM_UNDEFINED, /* 165 */ + TPM_UNDEFINED, + TPM_UNDEFINED, + TPM_UNDEFINED, + TPM_UNDEFINED, + TPM_LONG, /* 170 */ + TPM_UNDEFINED, + TPM_UNDEFINED, + TPM_UNDEFINED, + TPM_UNDEFINED, + TPM_UNDEFINED, /* 175 */ + TPM_UNDEFINED, + TPM_UNDEFINED, + TPM_UNDEFINED, + TPM_UNDEFINED, + TPM_MEDIUM, /* 180 */ + TPM_SHORT, + TPM_MEDIUM, + TPM_MEDIUM, + TPM_MEDIUM, + TPM_MEDIUM, /* 185 */ + TPM_SHORT, + TPM_UNDEFINED, + TPM_UNDEFINED, + TPM_UNDEFINED, + TPM_UNDEFINED, /* 190 */ + TPM_UNDEFINED, + TPM_UNDEFINED, + TPM_UNDEFINED, + TPM_UNDEFINED, + TPM_UNDEFINED, /* 195 */ + TPM_UNDEFINED, + TPM_UNDEFINED, + TPM_UNDEFINED, + TPM_UNDEFINED, + TPM_SHORT, /* 200 */ + TPM_UNDEFINED, + TPM_UNDEFINED, + TPM_UNDEFINED, + TPM_SHORT, + TPM_SHORT, /* 205 */ + TPM_SHORT, + TPM_SHORT, + TPM_SHORT, + TPM_SHORT, + TPM_MEDIUM, /* 210 */ + TPM_UNDEFINED, + TPM_MEDIUM, + TPM_MEDIUM, + TPM_MEDIUM, + TPM_UNDEFINED, /* 215 */ + TPM_MEDIUM, + TPM_UNDEFINED, + TPM_UNDEFINED, + TPM_SHORT, + TPM_SHORT, /* 220 */ + TPM_SHORT, + TPM_SHORT, + TPM_SHORT, + TPM_SHORT, + TPM_UNDEFINED, /* 225 */ + TPM_UNDEFINED, + TPM_UNDEFINED, + TPM_UNDEFINED, + TPM_UNDEFINED, + TPM_SHORT, /* 230 */ + TPM_LONG, + TPM_MEDIUM, + TPM_UNDEFINED, + TPM_UNDEFINED, + TPM_UNDEFINED, /* 235 */ + TPM_UNDEFINED, + TPM_UNDEFINED, + TPM_UNDEFINED, + TPM_UNDEFINED, + TPM_SHORT, /* 240 */ + TPM_UNDEFINED, + TPM_MEDIUM, +}; + +/* + * Returns max number of milliseconds to wait + */ +unsigned long tpm_calc_ordinal_duration(struct tpm_chip *chip, u32 ordinal) +{ + int duration_idx = TPM_UNDEFINED; + int duration = 0; + + if (ordinal < TPM_MAX_ORDINAL) + duration_idx = tpm_ordinal_duration[ordinal]; + else if ((ordinal & TPM_PROTECTED_ORDINAL_MASK) < + TPM_MAX_PROTECTED_ORDINAL) + duration_idx = + tpm_protected_ordinal_duration[ordinal & + TPM_PROTECTED_ORDINAL_MASK]; + + if (duration_idx != TPM_UNDEFINED) + duration = chip->vendor.duration[duration_idx]; + if (duration <= 0) + return 2 * 60 * HZ; /*two minutes timeout*/ + else + return duration; +} + +#define TPM_CMD_COUNT_BYTE 2 +#define TPM_CMD_ORDINAL_BYTE 6 + +ssize_t tpm_transmit(const unsigned char *buf, size_t bufsiz) +{ + ssize_t rc; + u32 count, ordinal; + unsigned long start, stop; + + struct tpm_chip *chip = &g_chip; + + /* switch endianess: big->little */ + count = get_unaligned_be32(buf + TPM_CMD_COUNT_BYTE); + ordinal = get_unaligned_be32(buf + TPM_CMD_ORDINAL_BYTE); + + if (count == 0) { + dev_err(chip->dev, "no data\n"); + return -ENODATA; + } + if (count > bufsiz) { + dev_err(chip->dev, + "invalid count value %x %zx\n", count, bufsiz); + return -E2BIG; + } + + rc = chip->vendor.send(chip, (u8 *)buf, count); + if (rc < 0) { + dev_err(chip->dev, "tpm_transmit: tpm_send: error %zd\n", rc); + goto out; + } + + if (chip->vendor.irq) + goto out_recv; + + start = get_timer(0); + stop = tpm_calc_ordinal_duration(chip, ordinal); + do { + dbg_printf("waiting for status...\n"); + u8 status = chip->vendor.status(chip); + if ((status & chip->vendor.req_complete_mask) == + chip->vendor.req_complete_val) { + dbg_printf("...got it;\n"); + goto out_recv; + } + + if ((status == chip->vendor.req_canceled)) { + dev_err(chip->dev, "Operation Canceled\n"); + rc = -ECANCELED; + goto out; + } + msleep(TPM_TIMEOUT); + } while (get_timer(start) < stop); + + chip->vendor.cancel(chip); + dev_err(chip->dev, "Operation Timed out\n"); + rc = -ETIME; + goto out; + +out_recv: + + dbg_printf("out_recv: reading response...\n"); + rc = chip->vendor.recv(chip, (u8 *)buf, TPM_BUFSIZE); + if (rc < 0) + dev_err(chip->dev, "tpm_transmit: tpm_recv: error %zd\n", rc); +out: + return rc; +} + +#define TPM_ERROR_SIZE 10 + +enum tpm_capabilities { + TPM_CAP_PROP = cpu_to_be32(5), +}; + +enum tpm_sub_capabilities { + TPM_CAP_PROP_TIS_TIMEOUT = cpu_to_be32(0x115), + TPM_CAP_PROP_TIS_DURATION = cpu_to_be32(0x120), +}; + +struct tpm_chip *tpm_register_hardware(const struct tpm_vendor_specific *entry) +{ + struct tpm_chip *chip; + + /* Driver specific per-device data */ + chip = &g_chip; + memcpy(&chip->vendor, entry, sizeof(struct tpm_vendor_specific)); + chip->is_open = 1; + + return chip; +} + +int tpm_open(uint32_t dev_addr) +{ + int rc; + if (g_chip.is_open) + return -EBUSY; + rc = tpm_vendor_init(dev_addr); + if (rc < 0) + g_chip.is_open = 0; + return rc; +} + +void tpm_close(void) +{ + if (g_chip.is_open) { + tpm_vendor_cleanup(&g_chip); + g_chip.is_open = 0; + } +} diff --git a/drivers/tpm/slb9635_i2c/tpm.h b/drivers/tpm/slb9635_i2c/tpm.h new file mode 100644 index 0000000000..9ddee865df --- /dev/null +++ b/drivers/tpm/slb9635_i2c/tpm.h @@ -0,0 +1,161 @@ +/* + * Copyright (C) 2011 Infineon Technologies + * + * Authors: + * Peter Huewe <huewe.external@infineon.com> + * + * Version: 2.1.1 + * + * Description: + * Device driver for TCG/TCPA TPM (trusted platform module). + * Specifications at www.trustedcomputinggroup.org + * + * It is based on the Linux kernel driver tpm.c from Leendert van + * Dorn, Dave Safford, Reiner Sailer, and Kyleen Hall. + * + * + * See file CREDITS for list of people who contributed to this + * project. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation, version 2 of the + * License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, + * MA 02111-1307 USA + */ + +#ifndef _TPM_H_ +#define _TPM_H_ + +#include <linux/compiler.h> + +#include "compatibility.h" + +enum tpm_timeout { + TPM_TIMEOUT = 5, /* msecs */ +}; + +/* Size of external transmit buffer (used in tpm_transmit)*/ +#define TPM_BUFSIZE 4096 + +/* Index of fields in TPM command buffer */ +#define TPM_CMD_SIZE_BYTE 2 +#define TPM_CMD_ORDINAL_BYTE 6 + +/* Index of Count field in TPM response buffer */ +#define TPM_RSP_SIZE_BYTE 2 +#define TPM_RSP_RC_BYTE 6 + +struct tpm_chip; + +struct tpm_vendor_specific { + const u8 req_complete_mask; + const u8 req_complete_val; + const u8 req_canceled; + int irq; + int (*recv) (struct tpm_chip *, u8 *, size_t); + int (*send) (struct tpm_chip *, u8 *, size_t); + void (*cancel) (struct tpm_chip *); + u8(*status) (struct tpm_chip *); + int locality; + unsigned long timeout_a, timeout_b, timeout_c, timeout_d; /* msec */ + unsigned long duration[3]; /* msec */ +}; + +struct tpm_chip { + int is_open; + struct tpm_vendor_specific vendor; +}; + +struct tpm_input_header { + __be16 tag; + __be32 length; + __be32 ordinal; +} __packed; + +struct tpm_output_header { + __be16 tag; + __be32 length; + __be32 return_code; +} __packed; + +struct timeout_t { + __be32 a; + __be32 b; + __be32 c; + __be32 d; +} __packed; + +struct duration_t { + __be32 tpm_short; + __be32 tpm_medium; + __be32 tpm_long; +} __packed; + +union cap_t { + struct timeout_t timeout; + struct duration_t duration; +}; + +struct tpm_getcap_params_in { + __be32 cap; + __be32 subcap_size; + __be32 subcap; +} __packed; + +struct tpm_getcap_params_out { + __be32 cap_size; + union cap_t cap; +} __packed; + +union tpm_cmd_header { + struct tpm_input_header in; + struct tpm_output_header out; +}; + +union tpm_cmd_params { + struct tpm_getcap_params_out getcap_out; + struct tpm_getcap_params_in getcap_in; +}; + +struct tpm_cmd_t { + union tpm_cmd_header header; + union tpm_cmd_params params; +} __packed; + + +/* ---------- Interface for TPM vendor ------------ */ + +extern struct tpm_chip *tpm_register_hardware( + const struct tpm_vendor_specific *); + +extern int tpm_vendor_init(uint32_t dev_addr); + +extern void tpm_vendor_cleanup(struct tpm_chip *chip); + +/* ---------- Interface for TDDL ------------------- */ + +/* + * if dev_addr != 0 - redefines TPM device address + * Returns < 0 on error, 0 on success. + */ +extern int tpm_open(uint32_t dev_addr); + +extern void tpm_close(void); + +/* + * Transmit bufsiz bytes out of buf to TPM and get results back in buf, too. + * Returns < 0 on error, 0 on success. + */ +extern ssize_t tpm_transmit(const unsigned char *buf, size_t bufsiz); + +#endif diff --git a/drivers/tpm/slb9635_i2c/tpm_tis_i2c.c b/drivers/tpm/slb9635_i2c/tpm_tis_i2c.c new file mode 100644 index 0000000000..82a41bf5b2 --- /dev/null +++ b/drivers/tpm/slb9635_i2c/tpm_tis_i2c.c @@ -0,0 +1,561 @@ +/* + * Copyright (C) 2011 Infineon Technologies + * + * Authors: + * Peter Huewe <huewe.external@infineon.com> + * + * Description: + * Device driver for TCG/TCPA TPM (trusted platform module). + * Specifications at www.trustedcomputinggroup.org + * + * This device driver implements the TPM interface as defined in + * the TCG TPM Interface Spec version 1.2, revision 1.0 and the + * Infineon I2C Protocol Stack Specification v0.20. + * + * It is based on the Linux kernel driver tpm.c from Leendert van + * Dorn, Dave Safford, Reiner Sailer, and Kyleen Hall. + * + * Version: 2.1.1 + * + * See file CREDITS for list of people who contributed to this + * project. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation, version 2 of the + * License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, + * MA 02111-1307 USA + */ + +#include <common.h> +#include <i2c.h> +#include <linux/types.h> + +#include "compatibility.h" +#include "tpm.h" + +/* max. buffer size supported by our tpm */ +#ifdef TPM_BUFSIZE +#undef TPM_BUFSIZE +#endif +#define TPM_BUFSIZE 1260 +/* Address of the TPM on the I2C bus */ +#define TPM_I2C_ADDR 0x20 +/* max. number of iterations after i2c NAK */ +#define MAX_COUNT 3 + +#define SLEEP_DURATION 60 /*in usec*/ + +/* max. number of iterations after i2c NAK for 'long' commands + * we need this especially for sending TPM_READY, since the cleanup after the + * transtion to the ready state may take some time, but it is unpredictable + * how long it will take. + */ +#define MAX_COUNT_LONG 50 + +#define SLEEP_DURATION_LONG 210 /* in usec */ + +/* expected value for DIDVID register */ +#define TPM_TIS_I2C_DID_VID 0x000b15d1L + +/* Structure to store I2C TPM specific stuff */ +struct tpm_inf_dev { + uint addr; + u8 buf[TPM_BUFSIZE + sizeof(u8)]; /* max. buffer size + addr */ +}; + +static struct tpm_inf_dev tpm_dev = { + .addr = TPM_I2C_ADDR +}; + +/* + * iic_tpm_read() - read from TPM register + * @addr: register address to read from + * @buffer: provided by caller + * @len: number of bytes to read + * + * Read len bytes from TPM register and put them into + * buffer (little-endian format, i.e. first byte is put into buffer[0]). + * + * NOTE: TPM is big-endian for multi-byte values. Multi-byte + * values have to be swapped. + * + * Return -EIO on error, 0 on success. + */ +int iic_tpm_read(u8 addr, u8 *buffer, size_t len) +{ + int rc; + int count; + uint myaddr = addr; + /* we have to use uint here, uchar hangs the board */ + + for (count = 0; count < MAX_COUNT; count++) { + rc = i2c_write(tpm_dev.addr, 0, 0, (uchar *)&myaddr, 1); + if (rc == 0) + break; /*success, break to skip sleep*/ + + udelay(SLEEP_DURATION); + } + + if (rc) + return -rc; + + /* After the TPM has successfully received the register address it needs + * some time, thus we're sleeping here again, before retrieving the data + */ + for (count = 0; count < MAX_COUNT; count++) { + udelay(SLEEP_DURATION); + rc = i2c_read(tpm_dev.addr, 0, 0, buffer, len); + if (rc == 0) + break; /*success, break to skip sleep*/ + } + + if (rc) + return -rc; + + return 0; +} + +static int iic_tpm_write_generic(u8 addr, u8 *buffer, size_t len, + unsigned int sleep_time, + u8 max_count) +{ + int rc = 0; + int count; + + /* prepare send buffer */ + tpm_dev.buf[0] = addr; + memcpy(&(tpm_dev.buf[1]), buffer, len); + + for (count = 0; count < max_count; count++) { + rc = i2c_write(tpm_dev.addr, 0, 0, tpm_dev.buf, len + 1); + if (rc == 0) + break; /*success, break to skip sleep*/ + + udelay(sleep_time); + } + + if (rc) + return -rc; + + return 0; +} + +/* + * iic_tpm_write() - write to TPM register + * @addr: register address to write to + * @buffer: containing data to be written + * @len: number of bytes to write + * + * Write len bytes from provided buffer to TPM register (little + * endian format, i.e. buffer[0] is written as first byte). + * + * NOTE: TPM is big-endian for multi-byte values. Multi-byte + * values have to be swapped. + * + * NOTE: use this function instead of the iic_tpm_write_generic function. + * + * Return -EIO on error, 0 on success + */ +static int iic_tpm_write(u8 addr, u8 *buffer, size_t len) +{ + return iic_tpm_write_generic(addr, buffer, len, SLEEP_DURATION, + MAX_COUNT); +} + +/* + * This function is needed especially for the cleanup situation after + * sending TPM_READY + * */ +static int iic_tpm_write_long(u8 addr, u8 *buffer, size_t len) +{ + return iic_tpm_write_generic(addr, buffer, len, SLEEP_DURATION_LONG, + MAX_COUNT_LONG); +} + +#define TPM_HEADER_SIZE 10 + +enum tis_access { + TPM_ACCESS_VALID = 0x80, + TPM_ACCESS_ACTIVE_LOCALITY = 0x20, + TPM_ACCESS_REQUEST_PENDING = 0x04, + TPM_ACCESS_REQUEST_USE = 0x02, +}; + +enum tis_status { + TPM_STS_VALID = 0x80, + TPM_STS_COMMAND_READY = 0x40, + TPM_STS_GO = 0x20, + TPM_STS_DATA_AVAIL = 0x10, + TPM_STS_DATA_EXPECT = 0x08, +}; + +enum tis_defaults { + TIS_SHORT_TIMEOUT = 750, /* ms */ + TIS_LONG_TIMEOUT = 2000, /* 2 sec */ +}; + +#define TPM_ACCESS(l) (0x0000 | ((l) << 4)) +#define TPM_STS(l) (0x0001 | ((l) << 4)) +#define TPM_DATA_FIFO(l) (0x0005 | ((l) << 4)) +#define TPM_DID_VID(l) (0x0006 | ((l) << 4)) + +static int check_locality(struct tpm_chip *chip, int loc) +{ + u8 buf; + int rc; + + rc = iic_tpm_read(TPM_ACCESS(loc), &buf, 1); + if (rc < 0) + return rc; + + if ((buf & (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID)) == + (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID)) { + chip->vendor.locality = loc; + return loc; + } + + return -1; +} + +static void release_locality(struct tpm_chip *chip, int loc, int force) +{ + u8 buf; + if (iic_tpm_read(TPM_ACCESS(loc), &buf, 1) < 0) + return; + + if (force || (buf & (TPM_ACCESS_REQUEST_PENDING | TPM_ACCESS_VALID)) == + (TPM_ACCESS_REQUEST_PENDING | TPM_ACCESS_VALID)) { + buf = TPM_ACCESS_ACTIVE_LOCALITY; + iic_tpm_write(TPM_ACCESS(loc), &buf, 1); + } +} + +static int request_locality(struct tpm_chip *chip, int loc) +{ + unsigned long start, stop; + u8 buf = TPM_ACCESS_REQUEST_USE; + + if (check_locality(chip, loc) >= 0) + return loc; /* we already have the locality */ + + iic_tpm_write(TPM_ACCESS(loc), &buf, 1); + + /* wait for burstcount */ + start = get_timer(0); + stop = chip->vendor.timeout_a; + do { + if (check_locality(chip, loc) >= 0) + return loc; + msleep(TPM_TIMEOUT); + } while (get_timer(start) < stop); + + return -1; +} + +static u8 tpm_tis_i2c_status(struct tpm_chip *chip) +{ + /* NOTE: since i2c read may fail, return 0 in this case --> time-out */ + u8 buf; + if (iic_tpm_read(TPM_STS(chip->vendor.locality), &buf, 1) < 0) + return 0; + else + return buf; +} + +static void tpm_tis_i2c_ready(struct tpm_chip *chip) +{ + /* this causes the current command to be aborted */ + u8 buf = TPM_STS_COMMAND_READY; + iic_tpm_write_long(TPM_STS(chip->vendor.locality), &buf, 1); +} + +static ssize_t get_burstcount(struct tpm_chip *chip) +{ + unsigned long start, stop; + ssize_t burstcnt; + u8 buf[3]; + + /* wait for burstcount */ + /* which timeout value, spec has 2 answers (c & d) */ + start = get_timer(0); + stop = chip->vendor.timeout_d; + do { + /* Note: STS is little endian */ + if (iic_tpm_read(TPM_STS(chip->vendor.locality) + 1, buf, 3) + < 0) + burstcnt = 0; + else + burstcnt = (buf[2] << 16) + (buf[1] << 8) + buf[0]; + + if (burstcnt) + return burstcnt; + msleep(TPM_TIMEOUT); + } while (get_timer(start) < stop); + + return -EBUSY; +} + +static int wait_for_stat(struct tpm_chip *chip, u8 mask, unsigned long timeout, + int *status) +{ + unsigned long start, stop; + + /* check current status */ + *status = tpm_tis_i2c_status(chip); + if ((*status & mask) == mask) + return 0; + + start = get_timer(0); + stop = timeout; + do { + msleep(TPM_TIMEOUT); + *status = tpm_tis_i2c_status(chip); + if ((*status & mask) == mask) + return 0; + + } while (get_timer(start) < stop); + + return -ETIME; +} + +static int recv_data(struct tpm_chip *chip, u8 *buf, size_t count) +{ + size_t size = 0; + ssize_t burstcnt; + int rc; + + while (size < count) { + burstcnt = get_burstcount(chip); + + /* burstcount < 0 = tpm is busy */ + if (burstcnt < 0) + return burstcnt; + + /* limit received data to max. left */ + if (burstcnt > (count - size)) + burstcnt = count - size; + + rc = iic_tpm_read(TPM_DATA_FIFO(chip->vendor.locality), + &(buf[size]), + burstcnt); + if (rc == 0) + size += burstcnt; + } + + return size; +} + +static int tpm_tis_i2c_recv(struct tpm_chip *chip, u8 *buf, size_t count) +{ + int size = 0; + int expected, status; + + if (count < TPM_HEADER_SIZE) { + size = -EIO; + goto out; + } + + /* read first 10 bytes, including tag, paramsize, and result */ + size = recv_data(chip, buf, TPM_HEADER_SIZE); + if (size < TPM_HEADER_SIZE) { + dev_err(chip->dev, "Unable to read header\n"); + goto out; + } + + expected = get_unaligned_be32(buf + TPM_RSP_SIZE_BYTE); + if ((size_t)expected > count) { + size = -EIO; + goto out; + } + + size += recv_data(chip, &buf[TPM_HEADER_SIZE], + expected - TPM_HEADER_SIZE); + if (size < expected) { + dev_err(chip->dev, "Unable to read remainder of result\n"); + size = -ETIME; + goto out; + } + + wait_for_stat(chip, TPM_STS_VALID, chip->vendor.timeout_c, &status); + if (status & TPM_STS_DATA_AVAIL) { /* retry? */ + dev_err(chip->dev, "Error left over data\n"); + size = -EIO; + goto out; + } + +out: + tpm_tis_i2c_ready(chip); + /* The TPM needs some time to clean up here, + * so we sleep rather than keeping the bus busy + */ + udelay(2000); + release_locality(chip, chip->vendor.locality, 0); + + return size; +} + +static int tpm_tis_i2c_send(struct tpm_chip *chip, u8 *buf, size_t len) +{ + int rc, status; + ssize_t burstcnt; + size_t count = 0; + u8 sts = TPM_STS_GO; + + if (len > TPM_BUFSIZE) + return -E2BIG; /* command is too long for our tpm, sorry */ + + if (request_locality(chip, 0) < 0) + return -EBUSY; + + status = tpm_tis_i2c_status(chip); + if ((status & TPM_STS_COMMAND_READY) == 0) { + tpm_tis_i2c_ready(chip); + if (wait_for_stat + (chip, TPM_STS_COMMAND_READY, + chip->vendor.timeout_b, &status) < 0) { + rc = -ETIME; + goto out_err; + } + } + + while (count < len - 1) { + burstcnt = get_burstcount(chip); + + /* burstcount < 0 = tpm is busy */ + if (burstcnt < 0) + return burstcnt; + + if (burstcnt > (len-1-count)) + burstcnt = len-1-count; + +#ifdef CONFIG_TPM_I2C_BURST_LIMITATION + if (burstcnt > CONFIG_TPM_I2C_BURST_LIMITATION) + burstcnt = CONFIG_TPM_I2C_BURST_LIMITATION; +#endif /* CONFIG_TPM_I2C_BURST_LIMITATION */ + + rc = iic_tpm_write(TPM_DATA_FIFO(chip->vendor.locality), + &(buf[count]), burstcnt); + if (rc == 0) + count += burstcnt; + + wait_for_stat(chip, TPM_STS_VALID, + chip->vendor.timeout_c, &status); + + if ((status & TPM_STS_DATA_EXPECT) == 0) { + rc = -EIO; + goto out_err; + } + } + + /* write last byte */ + iic_tpm_write(TPM_DATA_FIFO(chip->vendor.locality), &(buf[count]), 1); + wait_for_stat(chip, TPM_STS_VALID, chip->vendor.timeout_c, &status); + if ((status & TPM_STS_DATA_EXPECT) != 0) { + rc = -EIO; + goto out_err; + } + + /* go and do it */ + iic_tpm_write(TPM_STS(chip->vendor.locality), &sts, 1); + + return len; +out_err: + tpm_tis_i2c_ready(chip); + /* The TPM needs some time to clean up here, + * so we sleep rather than keeping the bus busy + */ + udelay(2000); + release_locality(chip, chip->vendor.locality, 0); + + return rc; +} + +static struct tpm_vendor_specific tpm_tis_i2c = { + .status = tpm_tis_i2c_status, + .recv = tpm_tis_i2c_recv, + .send = tpm_tis_i2c_send, + .cancel = tpm_tis_i2c_ready, + .req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID, + .req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID, + .req_canceled = TPM_STS_COMMAND_READY, +}; + +/* initialisation of i2c tpm */ + + +int tpm_vendor_init(uint32_t dev_addr) +{ + u32 vendor; + uint old_addr; + int rc = 0; + struct tpm_chip *chip; + + old_addr = tpm_dev.addr; + if (dev_addr != 0) + tpm_dev.addr = dev_addr; + + chip = tpm_register_hardware(&tpm_tis_i2c); + if (chip < 0) { + rc = -ENODEV; + goto out_err; + } + + /* Disable interrupts (not supported) */ + chip->vendor.irq = 0; + + /* Default timeouts */ + chip->vendor.timeout_a = TIS_SHORT_TIMEOUT; + chip->vendor.timeout_b = TIS_LONG_TIMEOUT; + chip->vendor.timeout_c = TIS_SHORT_TIMEOUT; + chip->vendor.timeout_d = TIS_SHORT_TIMEOUT; + + if (request_locality(chip, 0) != 0) { + rc = -ENODEV; + goto out_err; + } + + /* read four bytes from DID_VID register */ + if (iic_tpm_read(TPM_DID_VID(0), (uchar *)&vendor, 4) < 0) { + rc = -EIO; + goto out_release; + } + + /* create DID_VID register value, after swapping to little-endian */ + vendor = be32_to_cpu(vendor); + + if (vendor != TPM_TIS_I2C_DID_VID) { + rc = -ENODEV; + goto out_release; + } + + dev_info(dev, "1.2 TPM (device-id 0x%X)\n", vendor >> 16); + + /* + * A timeout query to TPM can be placed here. + * Standard timeout values are used so far + */ + + return 0; + +out_release: + release_locality(chip, 0, 1); + +out_err: + tpm_dev.addr = old_addr; + return rc; +} + +void tpm_vendor_cleanup(struct tpm_chip *chip) +{ + release_locality(chip, chip->vendor.locality, 1); +} diff --git a/drivers/tpm/tis_i2c.c b/drivers/tpm/tis_i2c.c new file mode 100644 index 0000000000..e818fbaf54 --- /dev/null +++ b/drivers/tpm/tis_i2c.c @@ -0,0 +1,181 @@ +/* + * Copyright (c) 2012 The Chromium OS Authors. All rights reserved. + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include <config.h> +#include <common.h> +#include <fdtdec.h> +#include <i2c.h> +#include "slb9635_i2c/tpm.h" + +DECLARE_GLOBAL_DATA_PTR; + +/* TPM configuration */ +struct tpm { + int i2c_bus; + int slave_addr; + char inited; + int old_bus; +} tpm; + + +static int tpm_select(void) +{ + int ret; + + tpm.old_bus = i2c_get_bus_num(); + if (tpm.old_bus != tpm.i2c_bus) { + ret = i2c_set_bus_num(tpm.i2c_bus); + if (ret) { + debug("%s: Fail to set i2c bus %d\n", __func__, + tpm.i2c_bus); + return -1; + } + } + return 0; +} + +static int tpm_deselect(void) +{ + int ret; + + if (tpm.old_bus != i2c_get_bus_num()) { + ret = i2c_set_bus_num(tpm.old_bus); + if (ret) { + debug("%s: Fail to restore i2c bus %d\n", + __func__, tpm.old_bus); + return -1; + } + } + tpm.old_bus = -1; + return 0; +} + +/** + * Decode TPM configuration. + * + * @param dev Returns a configuration of TPM device + * @return 0 if ok, -1 on error + */ +static int tpm_decode_config(struct tpm *dev) +{ +#ifdef CONFIG_OF_CONTROL + const void *blob = gd->fdt_blob; + int node, parent; + int i2c_bus; + + node = fdtdec_next_compatible(blob, 0, COMPAT_INFINEON_SLB9635_TPM); + if (node < 0) { + debug("%s: Node not found\n", __func__); + return -1; + } + parent = fdt_parent_offset(blob, node); + if (parent < 0) { + debug("%s: Cannot find node parent\n", __func__); + return -1; + } + i2c_bus = i2c_get_bus_num_fdt(parent); + if (i2c_bus < 0) + return -1; + dev->i2c_bus = i2c_bus; + dev->slave_addr = fdtdec_get_addr(blob, node, "reg"); +#else + dev->i2c_bus = CONFIG_INFINEON_TPM_I2C_BUS; + dev->slave_addr = CONFIG_INFINEON_TPM_I2C_ADDR; +#endif + return 0; +} + +int tis_init(void) +{ + if (tpm.inited) + return 0; + + if (tpm_decode_config(&tpm)) + return -1; + + if (tpm_select()) + return -1; + + /* + * Probe TPM twice; the first probing might fail because TPM is asleep, + * and the probing can wake up TPM. + */ + if (i2c_probe(tpm.slave_addr) && i2c_probe(tpm.slave_addr)) { + debug("%s: fail to probe i2c addr 0x%x\n", __func__, + tpm.slave_addr); + return -1; + } + + tpm_deselect(); + + tpm.inited = 1; + + return 0; +} + +int tis_open(void) +{ + int rc; + + if (!tpm.inited) + return -1; + + if (tpm_select()) + return -1; + + rc = tpm_open(tpm.slave_addr); + + tpm_deselect(); + + return rc; +} + +int tis_close(void) +{ + if (!tpm.inited) + return -1; + + if (tpm_select()) + return -1; + + tpm_close(); + + tpm_deselect(); + + return 0; +} + +int tis_sendrecv(const uint8_t *sendbuf, size_t sbuf_size, + uint8_t *recvbuf, size_t *rbuf_len) +{ + int len; + uint8_t buf[4096]; + + if (!tpm.inited) + return -1; + + if (sizeof(buf) < sbuf_size) + return -1; + + memcpy(buf, sendbuf, sbuf_size); + + if (tpm_select()) + return -1; + + len = tpm_transmit(buf, sbuf_size); + + tpm_deselect(); + + if (len < 10) { + *rbuf_len = 0; + return -1; + } + + memcpy(recvbuf, buf, len); + *rbuf_len = len; + + return 0; +} diff --git a/include/configs/exynos5250-dt.h b/include/configs/exynos5250-dt.h index 2b9d6ac061..7378487b3f 100644 --- a/include/configs/exynos5250-dt.h +++ b/include/configs/exynos5250-dt.h @@ -129,6 +129,13 @@ #define CONFIG_USB_EHCI_EXYNOS #define CONFIG_USB_STORAGE +/* TPM */ +#define CONFIG_TPM +#define CONFIG_CMD_TPM +#define CONFIG_INFINEON_TPM_I2C +#define CONFIG_INFINEON_TPM_I2C_BUS 3 +#define CONFIG_INFINEON_TPM_I2C_ADDR 0x20 + /* MMC SPL */ #define CONFIG_SPL #define COPY_BL2_FNPTR_ADDR 0x02020030 diff --git a/include/fdtdec.h b/include/fdtdec.h index e7e3ff9e03..39fbdf144b 100644 --- a/include/fdtdec.h +++ b/include/fdtdec.h @@ -90,6 +90,7 @@ enum fdt_compat_id { COMPAT_MAXIM_MAX77686_PMIC, /* MAX77686 PMIC */ COMPAT_GENERIC_SPI_FLASH, /* Generic SPI Flash chip */ COMPAT_MAXIM_98095_CODEC, /* MAX98095 Codec */ + COMPAT_INFINEON_SLB9635_TPM, /* Infineon SLB9635 TPM */ COMPAT_COUNT, }; diff --git a/include/tis.h b/include/tis.h new file mode 100644 index 0000000000..89e57300b0 --- /dev/null +++ b/include/tis.h @@ -0,0 +1,73 @@ +/* + * Copyright (c) 2011 The Chromium OS Authors. + * + * See file CREDITS for list of people who contributed to this + * project. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of + * the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, + * MA 02111-1307 USA + */ + +#ifndef __TIS_H +#define __TIS_H + +#include <common.h> + +/* Low-level interface to access TPM */ + +/* + * tis_init() + * + * Initialize the TPM device. Returns 0 on success or -1 on + * failure (in case device probing did not succeed). + */ +int tis_init(void); + +/* + * tis_open() + * + * Requests access to locality 0 for the caller. After all commands have been + * completed the caller is supposed to call tis_close(). + * + * Returns 0 on success, -1 on failure. + */ +int tis_open(void); + +/* + * tis_close() + * + * terminate the currect session with the TPM by releasing the locked + * locality. Returns 0 on success of -1 on failure (in case lock + * removal did not succeed). + */ +int tis_close(void); + +/* + * tis_sendrecv() + * + * Send the requested data to the TPM and then try to get its response + * + * @sendbuf - buffer of the data to send + * @send_size size of the data to send + * @recvbuf - memory to save the response to + * @recv_len - pointer to the size of the response buffer + * + * Returns 0 on success (and places the number of response bytes at recv_len) + * or -1 on failure. + */ +int tis_sendrecv(const uint8_t *sendbuf, size_t send_size, uint8_t *recvbuf, + size_t *recv_len); + +#endif /* __TIS_H */ diff --git a/include/tpm.h b/include/tpm.h index 6b21e9c734..7219b7319c 100644 --- a/include/tpm.h +++ b/include/tpm.h @@ -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. @@ -20,52 +20,185 @@ * MA 02111-1307 USA */ -#ifndef _INCLUDE_TPM_H_ -#define _INCLUDE_TPM_H_ +#ifndef __TPM_H +#define __TPM_H -#include <common.h> +#include <tis.h> /* - * tis_init() + * Here is a partial implementation of TPM commands. Please consult TCG Main + * Specification for definitions of TPM commands. + */ + +enum tpm_startup_type { + TPM_ST_CLEAR = 0x0001, + TPM_ST_STATE = 0x0002, + TPM_ST_DEACTIVATED = 0x0003, +}; + +enum tpm_physical_presence { + TPM_PHYSICAL_PRESENCE_HW_DISABLE = 0x0200, + TPM_PHYSICAL_PRESENCE_CMD_DISABLE = 0x0100, + TPM_PHYSICAL_PRESENCE_LIFETIME_LOCK = 0x0080, + TPM_PHYSICAL_PRESENCE_HW_ENABLE = 0x0040, + TPM_PHYSICAL_PRESENCE_CMD_ENABLE = 0x0020, + TPM_PHYSICAL_PRESENCE_NOTPRESENT = 0x0010, + TPM_PHYSICAL_PRESENCE_PRESENT = 0x0008, + TPM_PHYSICAL_PRESENCE_LOCK = 0x0004, +}; + +enum tpm_nv_index { + TPM_NV_INDEX_LOCK = 0xffffffff, + TPM_NV_INDEX_0 = 0x00000000, + TPM_NV_INDEX_DIR = 0x10000001, +}; + +/** + * Initialize TPM device. It must be called before any TPM commands. * - * Initialize the TPM device. Returns 0 on success or -1 on - * failure (in case device probing did not succeed). + * @return 0 on success, non-0 on error. */ -int tis_init(void); +uint32_t tpm_init(void); -/* - * tis_open() +/** + * Issue a TPM_Startup command. * - * Requests access to locality 0 for the caller. After all commands have been - * completed the caller is supposed to call tis_close(). + * @param mode TPM startup mode + * @return return code of the operation + */ +uint32_t tpm_startup(enum tpm_startup_type mode); + +/** + * Issue a TPM_SelfTestFull command. * - * Returns 0 on success, -1 on failure. + * @return return code of the operation */ -int tis_open(void); +uint32_t tpm_self_test_full(void); -/* - * tis_close() +/** + * Issue a TPM_ContinueSelfTest command. * - * terminate the currect session with the TPM by releasing the locked - * locality. Returns 0 on success of -1 on failure (in case lock - * removal did not succeed). + * @return return code of the operation */ -int tis_close(void); +uint32_t tpm_continue_self_test(void); -/* - * tis_sendrecv() +/** + * Issue a TPM_NV_DefineSpace command. The implementation is limited + * to specify TPM_NV_ATTRIBUTES and size of the area. The area index + * could be one of the special value listed in enum tpm_nv_index. * - * Send the requested data to the TPM and then try to get its response + * @param index index of the area + * @param perm TPM_NV_ATTRIBUTES of the area + * @param size size of the area + * @return return code of the operation + */ +uint32_t tpm_nv_define_space(uint32_t index, uint32_t perm, uint32_t size); + +/** + * Issue a TPM_NV_ReadValue command. This implementation is limited + * to read the area from offset 0. The area index could be one of + * the special value listed in enum tpm_nv_index. + * + * @param index index of the area + * @param data output buffer of the area contents + * @param count size of output buffer + * @return return code of the operation + */ +uint32_t tpm_nv_read_value(uint32_t index, void *data, uint32_t count); + +/** + * Issue a TPM_NV_WriteValue command. This implementation is limited + * to write the area from offset 0. The area index could be one of + * the special value listed in enum tpm_nv_index. + * + * @param index index of the area + * @param data input buffer to be wrote to the area + * @param length length of data bytes of input buffer + * @return return code of the operation + */ +uint32_t tpm_nv_write_value(uint32_t index, const void *data, uint32_t length); + +/** + * Issue a TPM_Extend command. + * + * @param index index of the PCR + * @param in_digest 160-bit value representing the event to be + * recorded + * @param out_digest 160-bit PCR value after execution of the + * command + * @return return code of the operation + */ +uint32_t tpm_extend(uint32_t index, const void *in_digest, void *out_digest); + +/** + * Issue a TPM_PCRRead command. * - * @sendbuf - buffer of the data to send - * @send_size size of the data to send - * @recvbuf - memory to save the response to - * @recv_len - pointer to the size of the response buffer + * @param index index of the PCR + * @param data output buffer for contents of the named PCR + * @param count size of output buffer + * @return return code of the operation + */ +uint32_t tpm_pcr_read(uint32_t index, void *data, size_t count); + +/** + * Issue a TSC_PhysicalPresence command. TPM physical presence flag + * is bit-wise OR'ed of flags listed in enum tpm_physical_presence. + * + * @param presence TPM physical presence flag + * @return return code of the operation + */ +uint32_t tpm_tsc_physical_presence(uint16_t presence); + +/** + * Issue a TPM_ReadPubek command. + * + * @param data output buffer for the public endorsement key + * @param count size of ouput buffer + * @return return code of the operation + */ +uint32_t tpm_read_pubek(void *data, size_t count); + +/** + * Issue a TPM_ForceClear command. + * + * @return return code of the operation + */ +uint32_t tpm_force_clear(void); + +/** + * Issue a TPM_PhysicalEnable command. + * + * @return return code of the operation + */ +uint32_t tpm_physical_enable(void); + +/** + * Issue a TPM_PhysicalDisable command. + * + * @return return code of the operation + */ +uint32_t tpm_physical_disable(void); + +/** + * Issue a TPM_PhysicalSetDeactivated command. + * + * @param state boolean state of the deactivated flag + * @return return code of the operation + */ +uint32_t tpm_physical_set_deactivated(uint8_t state); + +/** + * Issue a TPM_GetCapability command. This implementation is limited + * to query sub_cap index that is 4-byte wide. * - * Returns 0 on success (and places the number of response bytes at recv_len) - * or -1 on failure. + * @param cap_area partition of capabilities + * @param sub_cap further definition of capability, which is + * limited to be 4-byte wide + * @param cap output buffer for capability information + * @param count size of ouput buffer + * @return return code of the operation */ -int tis_sendrecv(const uint8_t *sendbuf, size_t send_size, uint8_t *recvbuf, - size_t *recv_len); +uint32_t tpm_get_capability(uint32_t cap_area, uint32_t sub_cap, + void *cap, size_t count); -#endif /* _INCLUDE_TPM_H_ */ +#endif /* __TPM_H */ diff --git a/lib/Makefile b/lib/Makefile index 1bfd3ee124..d57775d7c4 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -54,6 +54,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 diff --git a/lib/fdtdec.c b/lib/fdtdec.c index e17dd001ca..e93743c87a 100644 --- a/lib/fdtdec.c +++ b/lib/fdtdec.c @@ -63,6 +63,7 @@ static const char * const compat_names[COMPAT_COUNT] = { COMPAT(MAXIM_MAX77686_PMIC, "maxim,max77686_pmic"), COMPAT(GENERIC_SPI_FLASH, "spi-flash"), COMPAT(MAXIM_98095_CODEC, "maxim,max98095-codec"), + COMPAT(INFINEON_SLB9635_TPM, "infineon,slb9635-tpm"), }; const char *fdtdec_get_compatible(enum fdt_compat_id id) diff --git a/lib/tpm.c b/lib/tpm.c new file mode 100644 index 0000000000..42c9bea0f9 --- /dev/null +++ b/lib/tpm.c @@ -0,0 +1,581 @@ +/* + * Copyright (c) 2013 The Chromium OS Authors. + * + * See file CREDITS for list of people who contributed to this + * project. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of + * the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, + * MA 02111-1307 USA + */ + +#include <common.h> +#include <stdarg.h> +#include <tpm.h> +#include <asm/unaligned.h> + +/* Internal error of TPM command library */ +#define TPM_LIB_ERROR ((uint32_t)~0u) + +/* Useful constants */ +enum { + COMMAND_BUFFER_SIZE = 256, + TPM_PUBEK_SIZE = 256, + TPM_REQUEST_HEADER_LENGTH = 10, + TPM_RESPONSE_HEADER_LENGTH = 10, + PCR_DIGEST_LENGTH = 20, +}; + +/** + * Pack data into a byte string. The data types are specified in + * the format string: 'b' means unsigned byte, 'w' unsigned word, + * 'd' unsigned double word, and 's' byte string. The data are a + * series of offsets and values (for type byte string there are also + * lengths). The data values are packed into the byte string + * sequentially, and so a latter value could over-write a former + * value. + * + * @param str output string + * @param size size of output string + * @param format format string + * @param ... data points + * @return 0 on success, non-0 on error + */ +int pack_byte_string(uint8_t *str, size_t size, const char *format, ...) +{ + va_list args; + size_t offset = 0, length = 0; + uint8_t *data = NULL; + uint32_t value = 0; + + va_start(args, format); + for (; *format; format++) { + switch (*format) { + case 'b': + offset = va_arg(args, size_t); + value = va_arg(args, int); + length = 1; + break; + case 'w': + offset = va_arg(args, size_t); + value = va_arg(args, int); + length = 2; + break; + case 'd': + offset = va_arg(args, size_t); + value = va_arg(args, uint32_t); + length = 4; + break; + case 's': + offset = va_arg(args, size_t); + data = va_arg(args, uint8_t *); + length = va_arg(args, uint32_t); + break; + default: + debug("Couldn't recognize format string\n"); + return -1; + } + + if (offset + length > size) + return -1; + + switch (*format) { + case 'b': + str[offset] = value; + break; + case 'w': + put_unaligned_be16(value, str + offset); + break; + case 'd': + put_unaligned_be32(value, str + offset); + break; + case 's': + memcpy(str + offset, data, length); + break; + } + } + va_end(args); + + return 0; +} + +/** + * Unpack data from a byte string. The data types are specified in + * the format string: 'b' means unsigned byte, 'w' unsigned word, + * 'd' unsigned double word, and 's' byte string. The data are a + * series of offsets and pointers (for type byte string there are also + * lengths). + * + * @param str output string + * @param size size of output string + * @param format format string + * @param ... data points + * @return 0 on success, non-0 on error + */ +int unpack_byte_string(const uint8_t *str, size_t size, const char *format, ...) +{ + va_list args; + size_t offset = 0, length = 0; + uint8_t *ptr8 = NULL; + uint16_t *ptr16 = NULL; + uint32_t *ptr32 = NULL; + + va_start(args, format); + for (; *format; format++) { + switch (*format) { + case 'b': + offset = va_arg(args, size_t); + ptr8 = va_arg(args, uint8_t *); + length = 1; + break; + case 'w': + offset = va_arg(args, size_t); + ptr16 = va_arg(args, uint16_t *); + length = 2; + break; + case 'd': + offset = va_arg(args, size_t); + ptr32 = va_arg(args, uint32_t *); + length = 4; + break; + case 's': + offset = va_arg(args, size_t); + ptr8 = va_arg(args, uint8_t *); + length = va_arg(args, uint32_t); + break; + default: + debug("Couldn't recognize format string\n"); + return -1; + } + + if (offset + length > size) + return -1; + + switch (*format) { + case 'b': + *ptr8 = str[offset]; + break; + case 'w': + *ptr16 = get_unaligned_be16(str + offset); + break; + case 'd': + *ptr32 = get_unaligned_be32(str + offset); + break; + case 's': + memcpy(ptr8, str + offset, length); + break; + } + } + va_end(args); + + return 0; +} + +/** + * Get TPM command size. + * + * @param command byte string of TPM command + * @return command size of the TPM command + */ +static uint32_t tpm_command_size(const void *command) +{ + const size_t command_size_offset = 2; + return get_unaligned_be32(command + command_size_offset); +} + +/** + * Get TPM response return code, which is one of TPM_RESULT values. + * + * @param response byte string of TPM response + * @return return code of the TPM response + */ +static uint32_t tpm_return_code(const void *response) +{ + const size_t return_code_offset = 6; + return get_unaligned_be32(response + return_code_offset); +} + +/** + * Send a TPM command and return response's return code, and optionally + * return response to caller. + * + * @param command byte string of TPM command + * @param response output buffer for TPM response, or NULL if the + * caller does not care about it + * @param size_ptr output buffer size (input parameter) and TPM + * response length (output parameter); this parameter + * is a bidirectional + * @return return code of the TPM response + */ +static uint32_t tpm_sendrecv_command(const void *command, + void *response, size_t *size_ptr) +{ + uint8_t response_buffer[COMMAND_BUFFER_SIZE]; + size_t response_length; + uint32_t err; + + if (response) { + response_length = *size_ptr; + } else { + response = response_buffer; + response_length = sizeof(response_buffer); + } + err = tis_sendrecv(command, tpm_command_size(command), + response, &response_length); + if (err) + return TPM_LIB_ERROR; + if (response) + *size_ptr = response_length; + + return tpm_return_code(response); +} + +uint32_t tpm_init(void) +{ + uint32_t err; + + err = tis_init(); + if (err) + return err; + + return tis_open(); +} + +uint32_t tpm_startup(enum tpm_startup_type mode) +{ + const uint8_t command[12] = { + 0x0, 0xc1, 0x0, 0x0, 0x0, 0xc, 0x0, 0x0, 0x0, 0x99, 0x0, 0x0, + }; + const size_t mode_offset = 10; + uint8_t buf[COMMAND_BUFFER_SIZE]; + + if (pack_byte_string(buf, sizeof(buf), "sw", + 0, command, sizeof(command), + mode_offset, mode)) + return TPM_LIB_ERROR; + + return tpm_sendrecv_command(buf, NULL, NULL); +} + +uint32_t tpm_self_test_full(void) +{ + const uint8_t command[10] = { + 0x0, 0xc1, 0x0, 0x0, 0x0, 0xa, 0x0, 0x0, 0x0, 0x50, + }; + return tpm_sendrecv_command(command, NULL, NULL); +} + +uint32_t tpm_continue_self_test(void) +{ + const uint8_t command[10] = { + 0x0, 0xc1, 0x0, 0x0, 0x0, 0xa, 0x0, 0x0, 0x0, 0x53, + }; + return tpm_sendrecv_command(command, NULL, NULL); +} + +uint32_t tpm_nv_define_space(uint32_t index, uint32_t perm, uint32_t size) +{ + const uint8_t command[101] = { + 0x0, 0xc1, /* TPM_TAG */ + 0x0, 0x0, 0x0, 0x65, /* parameter size */ + 0x0, 0x0, 0x0, 0xcc, /* TPM_COMMAND_CODE */ + /* TPM_NV_DATA_PUBLIC->... */ + 0x0, 0x18, /* ...->TPM_STRUCTURE_TAG */ + 0, 0, 0, 0, /* ...->TPM_NV_INDEX */ + /* TPM_NV_DATA_PUBLIC->TPM_PCR_INFO_SHORT */ + 0x0, 0x3, + 0, 0, 0, + 0x1f, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + /* TPM_NV_DATA_PUBLIC->TPM_PCR_INFO_SHORT */ + 0x0, 0x3, + 0, 0, 0, + 0x1f, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + /* TPM_NV_ATTRIBUTES->... */ + 0x0, 0x17, /* ...->TPM_STRUCTURE_TAG */ + 0, 0, 0, 0, /* ...->attributes */ + /* End of TPM_NV_ATTRIBUTES */ + 0, /* bReadSTClear */ + 0, /* bWriteSTClear */ + 0, /* bWriteDefine */ + 0, 0, 0, 0, /* size */ + }; + const size_t index_offset = 12; + const size_t perm_offset = 70; + const size_t size_offset = 77; + uint8_t buf[COMMAND_BUFFER_SIZE]; + + if (pack_byte_string(buf, sizeof(buf), "sddd", + 0, command, sizeof(command), + index_offset, index, + perm_offset, perm, + size_offset, size)) + return TPM_LIB_ERROR; + + return tpm_sendrecv_command(buf, NULL, NULL); +} + +uint32_t tpm_nv_read_value(uint32_t index, void *data, uint32_t count) +{ + 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]; + size_t response_length = sizeof(response); + uint32_t data_size; + uint32_t err; + + if (pack_byte_string(buf, sizeof(buf), "sdd", + 0, command, sizeof(command), + index_offset, index, + length_offset, count)) + return TPM_LIB_ERROR; + err = tpm_sendrecv_command(buf, response, &response_length); + if (err) + return err; + 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) +{ + 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]; + size_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 = tpm_sendrecv_command(buf, response, &response_length); + if (err) + return err; + + return 0; +} + +uint32_t tpm_extend(uint32_t index, const void *in_digest, void *out_digest) +{ + const uint8_t command[34] = { + 0x0, 0xc1, 0x0, 0x0, 0x0, 0x22, 0x0, 0x0, 0x0, 0x14, + }; + const size_t index_offset = 10; + const size_t in_digest_offset = 14; + const size_t out_digest_offset = 10; + uint8_t buf[COMMAND_BUFFER_SIZE]; + uint8_t response[TPM_RESPONSE_HEADER_LENGTH + PCR_DIGEST_LENGTH]; + size_t response_length = sizeof(response); + uint32_t err; + + if (pack_byte_string(buf, sizeof(buf), "sds", + 0, command, sizeof(command), + index_offset, index, + in_digest_offset, in_digest, + PCR_DIGEST_LENGTH)) + return TPM_LIB_ERROR; + err = tpm_sendrecv_command(buf, response, &response_length); + if (err) + return err; + + if (unpack_byte_string(response, response_length, "s", + out_digest_offset, out_digest, + PCR_DIGEST_LENGTH)) + return TPM_LIB_ERROR; + + return 0; +} + +uint32_t tpm_pcr_read(uint32_t index, void *data, size_t count) +{ + const uint8_t command[14] = { + 0x0, 0xc1, 0x0, 0x0, 0x0, 0xe, 0x0, 0x0, 0x0, 0x15, + }; + const size_t index_offset = 10; + const size_t out_digest_offset = 10; + uint8_t buf[COMMAND_BUFFER_SIZE], response[COMMAND_BUFFER_SIZE]; + size_t response_length = sizeof(response); + uint32_t err; + + if (count < PCR_DIGEST_LENGTH) + return TPM_LIB_ERROR; + + if (pack_byte_string(buf, sizeof(buf), "sd", + 0, command, sizeof(command), + index_offset, index)) + return TPM_LIB_ERROR; + err = tpm_sendrecv_command(buf, response, &response_length); + if (err) + return err; + if (unpack_byte_string(response, response_length, "s", + out_digest_offset, data, PCR_DIGEST_LENGTH)) + return TPM_LIB_ERROR; + + return 0; +} + +uint32_t tpm_tsc_physical_presence(uint16_t presence) +{ + const uint8_t command[12] = { + 0x0, 0xc1, 0x0, 0x0, 0x0, 0xc, 0x40, 0x0, 0x0, 0xa, 0x0, 0x0, + }; + const size_t presence_offset = 10; + uint8_t buf[COMMAND_BUFFER_SIZE]; + + if (pack_byte_string(buf, sizeof(buf), "sw", + 0, command, sizeof(command), + presence_offset, presence)) + return TPM_LIB_ERROR; + + return tpm_sendrecv_command(buf, NULL, NULL); +} + +uint32_t tpm_read_pubek(void *data, size_t count) +{ + const uint8_t command[30] = { + 0x0, 0xc1, 0x0, 0x0, 0x0, 0x1e, 0x0, 0x0, 0x0, 0x7c, + }; + const size_t response_size_offset = 2; + const size_t data_offset = 10; + const size_t header_and_checksum_size = TPM_RESPONSE_HEADER_LENGTH + 20; + uint8_t response[COMMAND_BUFFER_SIZE + TPM_PUBEK_SIZE]; + size_t response_length = sizeof(response); + uint32_t data_size; + uint32_t err; + + err = tpm_sendrecv_command(command, response, &response_length); + if (err) + return err; + if (unpack_byte_string(response, response_length, "d", + response_size_offset, &data_size)) + return TPM_LIB_ERROR; + if (data_size < header_and_checksum_size) + return TPM_LIB_ERROR; + data_size -= header_and_checksum_size; + 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_force_clear(void) +{ + const uint8_t command[10] = { + 0x0, 0xc1, 0x0, 0x0, 0x0, 0xa, 0x0, 0x0, 0x0, 0x5d, + }; + + return tpm_sendrecv_command(command, NULL, NULL); +} + +uint32_t tpm_physical_enable(void) +{ + const uint8_t command[10] = { + 0x0, 0xc1, 0x0, 0x0, 0x0, 0xa, 0x0, 0x0, 0x0, 0x6f, + }; + + return tpm_sendrecv_command(command, NULL, NULL); +} + +uint32_t tpm_physical_disable(void) +{ + const uint8_t command[10] = { + 0x0, 0xc1, 0x0, 0x0, 0x0, 0xa, 0x0, 0x0, 0x0, 0x70, + }; + + return tpm_sendrecv_command(command, NULL, NULL); +} + +uint32_t tpm_physical_set_deactivated(uint8_t state) +{ + const uint8_t command[11] = { + 0x0, 0xc1, 0x0, 0x0, 0x0, 0xb, 0x0, 0x0, 0x0, 0x72, + }; + const size_t state_offset = 10; + uint8_t buf[COMMAND_BUFFER_SIZE]; + + if (pack_byte_string(buf, sizeof(buf), "sb", + 0, command, sizeof(command), + state_offset, state)) + return TPM_LIB_ERROR; + + return tpm_sendrecv_command(buf, NULL, NULL); +} + +uint32_t tpm_get_capability(uint32_t cap_area, uint32_t sub_cap, + void *cap, size_t count) +{ + const uint8_t command[22] = { + 0x0, 0xc1, /* TPM_TAG */ + 0x0, 0x0, 0x0, 0x16, /* parameter size */ + 0x0, 0x0, 0x0, 0x65, /* TPM_COMMAND_CODE */ + 0x0, 0x0, 0x0, 0x0, /* TPM_CAPABILITY_AREA */ + 0x0, 0x0, 0x0, 0x4, /* subcap size */ + 0x0, 0x0, 0x0, 0x0, /* subcap value */ + }; + const size_t cap_area_offset = 10; + const size_t sub_cap_offset = 18; + const size_t cap_offset = 14; + const size_t cap_size_offset = 10; + uint8_t buf[COMMAND_BUFFER_SIZE], response[COMMAND_BUFFER_SIZE]; + size_t response_length = sizeof(response); + uint32_t cap_size; + uint32_t err; + + if (pack_byte_string(buf, sizeof(buf), "sdd", + 0, command, sizeof(command), + cap_area_offset, cap_area, + sub_cap_offset, sub_cap)) + return TPM_LIB_ERROR; + err = tpm_sendrecv_command(buf, response, &response_length); + if (err) + return err; + if (unpack_byte_string(response, response_length, "d", + cap_size_offset, &cap_size)) + return TPM_LIB_ERROR; + if (cap_size > response_length || cap_size > count) + return TPM_LIB_ERROR; + if (unpack_byte_string(response, response_length, "s", + cap_offset, cap, cap_size)) + return TPM_LIB_ERROR; + + return 0; +} |