Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tcti-helper: Properly handle state transition from init to ready #2689

Merged
merged 1 commit into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions src/tss2-tcti/tcti-i2c-helper.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
#define LOGMODULE tcti
#include "util/log.h"

#define TIMEOUT_B 2000 /* The default timeout value as specified in the TCG spec. */

/*
* CRC-CCITT KERMIT with following parameters:
*
Expand Down Expand Up @@ -560,7 +562,7 @@ TSS2_RC tcti_i2c_helper_transmit (TSS2_TCTI_CONTEXT *tcti_ctx, size_t size, cons

/* Wait until ready bit is set by TPM device */
uint32_t expected_status_bits = TCTI_I2C_HELPER_TPM_STS_COMMAND_READY;
rc = i2c_tpm_helper_wait_for_status (ctx, expected_status_bits, expected_status_bits, 200);
rc = i2c_tpm_helper_wait_for_status (ctx, expected_status_bits, expected_status_bits, TIMEOUT_B);
if (rc != TSS2_RC_SUCCESS) {
LOG_ERROR("Failed waiting for TPM to become ready");
return rc;
Expand Down Expand Up @@ -817,14 +819,23 @@ TSS2_RC Tss2_Tcti_I2c_Helper_Init (TSS2_TCTI_CONTEXT* tcti_context, size_t* size
return TSS2_TCTI_RC_IO_ERROR;
}

/* Wait up to 200ms for TPM to become ready */
/* Wait up to TIMEOUT_B for TPM to become ready */
LOG_DEBUG ("Waiting for TPM to become ready...");
uint32_t expected_status_bits = TCTI_I2C_HELPER_TPM_STS_COMMAND_READY;
rc = i2c_tpm_helper_wait_for_status (ctx, expected_status_bits, expected_status_bits, 200);
rc = i2c_tpm_helper_wait_for_status (ctx, expected_status_bits, expected_status_bits, TIMEOUT_B);
if (rc == TSS2_TCTI_RC_TRY_AGAIN) {
/*
* TPM did not auto transition into ready state,
* write 1 to commandReady to start the transition.
*/
i2c_tpm_helper_write_sts_reg (ctx, TCTI_I2C_HELPER_TPM_STS_COMMAND_READY);
rc = i2c_tpm_helper_wait_for_status (ctx, expected_status_bits, expected_status_bits, TIMEOUT_B);
}
if (rc != TSS2_RC_SUCCESS) {
LOG_ERROR ("Failed waiting for TPM to become ready");
return rc;
}

LOG_DEBUG ("TPM is ready");

/* Get rid */
Expand Down
17 changes: 14 additions & 3 deletions src/tss2-tcti/tcti-spi-helper.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
#define LOGMODULE tcti
#include "util/log.h"

#define TIMEOUT_B 2000 // The default timeout value as specified in the TCG spec

static inline TSS2_RC spi_tpm_helper_delay_ms(TSS2_TCTI_SPI_HELPER_CONTEXT* ctx, int milliseconds)
{
// Sleep a specified amount of milliseconds
Expand Down Expand Up @@ -621,7 +623,7 @@ TSS2_RC tcti_spi_helper_transmit (TSS2_TCTI_CONTEXT *tcti_ctx, size_t size, cons

// Wait until ready bit is set by TPM device
uint32_t expected_status_bits = TCTI_SPI_HELPER_TPM_STS_COMMAND_READY;
rc = spi_tpm_helper_wait_for_status(ctx, expected_status_bits, expected_status_bits, 200);
rc = spi_tpm_helper_wait_for_status(ctx, expected_status_bits, expected_status_bits, TIMEOUT_B);
if (rc != TSS2_RC_SUCCESS) {
LOG_ERROR("Failed waiting for TPM to become ready");
return rc;
Expand Down Expand Up @@ -736,14 +738,23 @@ TSS2_RC Tss2_Tcti_Spi_Helper_Init (TSS2_TCTI_CONTEXT* tcti_context, size_t* size
return TSS2_TCTI_RC_IO_ERROR;
}

// Wait up to 200ms for TPM to become ready
// Wait up to TIMEOUT_B for TPM to become ready
LOG_DEBUG("Waiting for TPM to become ready...");
uint32_t expected_status_bits = TCTI_SPI_HELPER_TPM_STS_COMMAND_READY;
rc = spi_tpm_helper_wait_for_status(ctx, expected_status_bits, expected_status_bits, 200);
rc = spi_tpm_helper_wait_for_status(ctx, expected_status_bits, expected_status_bits, TIMEOUT_B);
if (rc == TSS2_TCTI_RC_TRY_AGAIN) {
/*
* TPM did not auto transition into ready state,
* write 1 to commandReady to start the transition.
*/
spi_tpm_helper_write_sts_reg(ctx, TCTI_SPI_HELPER_TPM_STS_COMMAND_READY);
rc = spi_tpm_helper_wait_for_status(ctx, expected_status_bits, expected_status_bits, TIMEOUT_B);
}
if (rc != TSS2_RC_SUCCESS) {
LOG_ERROR("Failed waiting for TPM to become ready");
return rc;
}

LOG_DEBUG("TPM is ready");

// Get rid
Expand Down