Skip to content

Commit

Permalink
tcti-spi/i2c-helper: Use builtin endianess conversion
Browse files Browse the repository at this point in the history
Instead of relying on the endian.h functions (which are not
available on all platforms) use the tss2_engian.h file which
includes also a customer implementation.

Signed-off-by: Andreas Fuchs <andreas.fuchs@infineon.com>
  • Loading branch information
AndreasFuchsTPM committed Aug 8, 2023
1 parent 3d0bfb1 commit 1668bb6
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 20 deletions.
15 changes: 7 additions & 8 deletions src/tss2-tcti/tcti-i2c-helper.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
#include "tss2_mu.h"
#include "tcti-common.h"
#include "tcti-i2c-helper.h"
#include "util/io.h"
#include "util/tss2_endian.h"
#define LOGMODULE tcti
#include "util/log.h"
Expand Down Expand Up @@ -257,10 +256,10 @@ static TSS2_RC i2c_tpm_sanity_check_read (uint8_t reg, uint8_t *buffer, size_t c
value = buffer[0];
break;
case sizeof (uint16_t):
value = le16toh (*((uint16_t *)buffer));
value = LE_TO_HOST_16 (*((uint16_t *)buffer));
break;
case sizeof (uint32_t):
value = le32toh (*((uint32_t *)buffer));
value = LE_TO_HOST_32 (*((uint32_t *)buffer));
break;
default:
return TSS2_RC_SUCCESS;
Expand Down Expand Up @@ -349,12 +348,12 @@ static uint32_t i2c_tpm_helper_read_sts_reg (TSS2_TCTI_I2C_HELPER_CONTEXT* ctx)
{
uint32_t status = 0;
i2c_tpm_helper_read_reg (ctx, TCTI_I2C_HELPER_TPM_STS_REG, &status, sizeof(status));
return le32toh (status);
return LE_TO_HOST_32 (status);
}

static void i2c_tpm_helper_write_sts_reg (TSS2_TCTI_I2C_HELPER_CONTEXT* ctx, uint32_t status)
{
status = htole32 (status);
status = HOST_TO_LE_32 (status);
i2c_tpm_helper_write_reg (ctx, TCTI_I2C_HELPER_TPM_STS_REG, &status, sizeof (status));
}

Expand Down Expand Up @@ -415,7 +414,7 @@ static TSS2_RC i2c_tpm_helper_init_guard_time (TSS2_TCTI_I2C_HELPER_CONTEXT* ctx
return rc;
}

i2c_caps = le32toh (i2c_caps);
i2c_caps = LE_TO_HOST_32 (i2c_caps);

ctx->guard_time_read = (i2c_caps & TCTI_I2C_HELPER_TPM_GUARD_TIME_RR_MASK) ||
(i2c_caps & TCTI_I2C_HELPER_TPM_GUARD_TIME_RW_MASK);
Expand Down Expand Up @@ -488,7 +487,7 @@ static TSS2_RC i2c_tpm_helper_verify_crc (TSS2_TCTI_I2C_HELPER_CONTEXT* ctx, con
return rc;
}

crc_tpm = le16toh (crc_tpm);
crc_tpm = LE_TO_HOST_16 (crc_tpm);
/* Reflect crc result, regardless of host endianness */
crc_tpm = ((crc_tpm >> 8) & 0xFFu) | ((crc_tpm << 8) & 0xFF00u);
crc_host = crc_ccitt (buffer, size);
Expand Down Expand Up @@ -783,7 +782,7 @@ TSS2_RC Tss2_Tcti_I2c_Helper_Init (TSS2_TCTI_CONTEXT* tcti_context, size_t* size
/* In case of failed read div_vid is set to zero */
i2c_tpm_helper_read_reg (ctx, TCTI_I2C_HELPER_TPM_DID_VID_REG, &did_vid, sizeof(did_vid));
if (did_vid != 0) {
did_vid = le32toh (did_vid);
did_vid = LE_TO_HOST_32 (did_vid);
break;
}
/* TPM might be resetting, let's retry in a bit */
Expand Down
1 change: 0 additions & 1 deletion src/tss2-tcti/tcti-spi-helper.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
#include "tss2_mu.h"
#include "tcti-common.h"
#include "tcti-spi-helper.h"
#include "util/io.h"
#define LOGMODULE tcti
#include "util/log.h"

Expand Down
44 changes: 33 additions & 11 deletions src/util/tss2_endian.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,15 @@
#define BE_TO_HOST_32(value) be32toh(value)
#define BE_TO_HOST_64(value) be64toh(value)

#else /* linux || unix */

#if defined(WORDS_BIGENDIAN)
#define HOST_TO_LE_16(value) htole16(value)
#define HOST_TO_LE_32(value) htole32(value)
#define HOST_TO_LE_64(value) htole64(value)
#define LE_TO_HOST_16(value) le16toh(value)
#define LE_TO_HOST_32(value) le32toh(value)
#define LE_TO_HOST_64(value) le64toh(value)

#define HOST_TO_BE_16(value) (value)
#define HOST_TO_BE_32(value) (value)
#define HOST_TO_BE_64(value) (value)
#define BE_TO_HOST_16(value) (value)
#define BE_TO_HOST_32(value) (value)
#define BE_TO_HOST_64(value) (value)
#else /* linux || unix */

#else
#include <stdint.h>

static inline uint16_t endian_conv_16(uint16_t value)
Expand Down Expand Up @@ -62,13 +59,38 @@ static inline uint64_t endian_conv_64(uint64_t value)
((value & (0xffULL << 56)) >> 56);
}

#if defined(WORDS_BIGENDIAN)

#define HOST_TO_BE_16(value) (value)
#define HOST_TO_BE_32(value) (value)
#define HOST_TO_BE_64(value) (value)
#define BE_TO_HOST_16(value) (value)
#define BE_TO_HOST_32(value) (value)
#define BE_TO_HOST_64(value) (value)

#define HOST_TO_LE_16(value) endian_conv_16(value)
#define HOST_TO_LE_32(value) endian_conv_32(value)
#define HOST_TO_LE_64(value) endian_conv_64(value)
#define LE_TO_HOST_16(value) endian_conv_16(value)
#define LE_TO_HOST_32(value) endian_conv_32(value)
#define LE_TO_HOST_64(value) endian_conv_64(value)

#else /* WORDS_BIGENDIAN */

#define HOST_TO_BE_16(value) endian_conv_16(value)
#define HOST_TO_BE_32(value) endian_conv_32(value)
#define HOST_TO_BE_64(value) endian_conv_64(value)
#define BE_TO_HOST_16(value) endian_conv_16(value)
#define BE_TO_HOST_32(value) endian_conv_32(value)
#define BE_TO_HOST_64(value) endian_conv_64(value)

#endif /* WORDS_BIGENDIAN */
#define HOST_TO_LE_16(value) (value)
#define HOST_TO_LE_32(value) (value)
#define HOST_TO_LE_64(value) (value)
#define LE_TO_HOST_16(value) (value)
#define LE_TO_HOST_32(value) (value)
#define LE_TO_HOST_64(value) (value)

#endif /* WORDS_BIGENDIAN else */
#endif /* linux || unix */
#endif /* TSS2_ENDIAN_H */

0 comments on commit 1668bb6

Please sign in to comment.