From 422cb0c5e227b4f944af5e31d8c34c18dd0c5b11 Mon Sep 17 00:00:00 2001 From: Vadim Bendebury Date: Thu, 12 Nov 2015 13:30:00 -0800 Subject: cr50: make use of byteorder routines Use the previously introduced endian conversion routines in the driver and the TPM2 library. Use packed TPM message header structure to make it easy to access unaligned header fields. BRANCH=none BUG=chrome-os-partner:43025 TEST=the tpm startup command still succeeds. Change-Id: I03078481664858a19617e248f98cb20013c27445 Signed-off-by: Vadim Bendebury Reviewed-on: https://chromium-review.googlesource.com/312585 --- board/cr50/build.mk | 4 ++++ board/cr50/tpm2/endian.h | 39 +-------------------------------------- common/tpm_registers.c | 24 ++++++++++-------------- include/tpm_registers.h | 15 +++++++++++++++ 4 files changed, 30 insertions(+), 52 deletions(-) diff --git a/board/cr50/build.mk b/board/cr50/build.mk index 7cdc079bde..d2fe6309bd 100644 --- a/board/cr50/build.mk +++ b/board/cr50/build.mk @@ -40,6 +40,10 @@ EXTLIB := $(realpath ../../third_party/tpm2) CFLAGS += -I$(EXTLIB) LDFLAGS_EXTRA += -L$(out)/tpm2 -ltpm2 +# For the benefit of the tpm2 library. +INCLUDE_ROOT := $(abspath ./include) +CFLAGS += -I$(INCLUDE_ROOT) + # Add dependencies on that library $(out)/RO/ec.RO.elf: $(out)/tpm2/libtpm2.a $(out)/RW/ec.RW.elf: $(out)/tpm2/libtpm2.a diff --git a/board/cr50/tpm2/endian.h b/board/cr50/tpm2/endian.h index 893a794c16..b5b64d3bea 100644 --- a/board/cr50/tpm2/endian.h +++ b/board/cr50/tpm2/endian.h @@ -6,43 +6,6 @@ #ifndef __EC_BOARD_CR50_TPM2_ENDIAN_H #define __EC_BOARD_CR50_TPM2_ENDIAN_H -#include -#include - -static inline void swap_n(void *in, void *out, size_t size) -{ - int i; - - for (i = 0; i < size; i++) - ((uint8_t *)out)[size - i - 1] = ((uint8_t *)in)[i]; -} - -static inline uint16_t be16toh(uint16_t in) -{ - uint16_t out; - - swap_n(&in, &out, sizeof(out)); - return out; -} - -static inline uint32_t be32toh(uint32_t in) -{ - uint32_t out; - - swap_n(&in, &out, sizeof(out)); - return out; -} - -static inline uint64_t be64toh(uint64_t in) -{ - uint64_t out; - - swap_n(&in, &out, sizeof(out)); - return out; -} - -#define htobe16 be16toh -#define htobe32 be32toh -#define htobe64 be64toh +#include "byteorder.h" #endif /* __EC_BOARD_CR50_TPM2_ENDIAN_H */ diff --git a/common/tpm_registers.c b/common/tpm_registers.c index 5aba839764..52de4aa6f6 100644 --- a/common/tpm_registers.c +++ b/common/tpm_registers.c @@ -9,6 +9,7 @@ * 24-bit address. There is no provision for error reporting at this level. */ +#include "byteorder.h" #include "common.h" #include "console.h" #include "hooks.h" @@ -273,22 +274,12 @@ static void sts_reg_write(const uint8_t *data, uint32_t data_size) } } -/* We presume to be running on a little endian CPU. */ -static uint32_t be32_to_cpu(const uint8_t *data) -{ - int i; - uint32_t value = 0; - - for (i = 0; i < sizeof(value); i++) - value |= ((uint32_t)data[i]) << (8 * (sizeof(value) - i - 1)); - - return value; -} - /* Collect received data in the local buffer and change state accordingly. */ static void fifo_reg_write(const uint8_t *data, uint32_t data_size) { uint32_t packet_size; + struct tpm_cmd_header *tpmh; + /* * Make sure we are in the approriate sate, otherwise ignore this * access. @@ -322,7 +313,8 @@ static void fifo_reg_write(const uint8_t *data, uint32_t data_size) return; } - packet_size = be32_to_cpu(tpm_.regs.data_fifo + 2); + tpmh = (struct tpm_cmd_header *)tpm_.regs.data_fifo; + packet_size = be32toh(tpmh->size); if (tpm_.fifo_write_index < packet_size) { tpm_.regs.sts |= expect; /* More data is needed. */ return; @@ -427,11 +419,15 @@ void tpm_task(void) while (1) { uint8_t *response; unsigned response_size; + uint32_t command_code; + struct tpm_cmd_header *tpmh; /* Wait for the next command event */ task_wait_event(-1); + tpmh = (struct tpm_cmd_header *)tpm_.regs.data_fifo; + command_code = be32toh(tpmh->command_code); CPRINTF("%s: received fifo command 0x%04x\n", - __func__, be32_to_cpu(tpm_.regs.data_fifo + 6)); + __func__, command_code); ExecuteCommand(tpm_.fifo_write_index, tpm_.regs.data_fifo, diff --git a/include/tpm_registers.h b/include/tpm_registers.h index 2bdd87bc25..ea8011bfed 100644 --- a/include/tpm_registers.h +++ b/include/tpm_registers.h @@ -14,6 +14,8 @@ #include +#include "common.h" + /* The SPI master is writing data into a TPM register. */ void tpm_register_put(uint32_t regaddr, const uint8_t *data, uint32_t data_size); @@ -24,4 +26,17 @@ void tpm_register_get(uint32_t regaddr, uint8_t *dest, uint32_t data_size); /* Enable SPS TPM driver. */ void sps_tpm_enable(void); +/* + * This structure describes the header of all commands and responses sent and + * received over TPM FIFO. + * + * Note that all fields are stored in the network (big endian) byte order. + */ + +struct tpm_cmd_header { + uint16_t tag; + uint32_t size; + uint32_t command_code; +} __packed; + #endif /* __CROS_EC_TPM_REGISTERS_H */ -- cgit v1.2.1