diff --git a/src/tpm2_swtpm.c b/src/tpm2_swtpm.c index 9b5a5960..0770774c 100644 --- a/src/tpm2_swtpm.c +++ b/src/tpm2_swtpm.c @@ -84,7 +84,7 @@ #define TPM2_SWTPM_PORT 115200 #endif #ifndef TPM2_TIMEOUT_SECONDS - #define TPM2_TIMEOUT_SECONDS 7200 + #define TPM2_TIMEOUT_SECONDS 21600 #endif #define WOLFTPM_WRITE(u, b, sz) XUartNs550_Send(&(u), (b), (sz)) #define WOLFTPM_READ(u, b, sz) XUartNs550_Recv(&(u), (b), (sz)) @@ -282,6 +282,7 @@ static int SwTpmReceive(TPM2_CTX* ctx, void* buffer, size_t rxSz) int rc; size_t remain, rxRemain; int sendAck = 0; + int timeOut = TPM2_TIMEOUT_SECONDS; if (ctx == NULL || buffer == NULL) { return BAD_FUNC_ARG; @@ -292,7 +293,7 @@ static int SwTpmReceive(TPM2_CTX* ctx, void* buffer, size_t rxSz) /* use up any leftovers before trying to pull more */ if (rxBuffIdx > 0) { - int minSz = (rxBuffIdx < remain)? rxBuffIdx : remain; + int minSz = (rxBuffIdx < (int)remain)? rxBuffIdx : (int)remain; memcpy(buffer, rxBuff, minSz); if (rxBuffIdx > minSz) { @@ -314,19 +315,29 @@ static int SwTpmReceive(TPM2_CTX* ctx, void* buffer, size_t rxSz) if (rc > 0 ) { usleep(500); sendAck = 1; + timeOut = TPM2_TIMEOUT_SECONDS; /* reset timeout */ } if (rc == 0) { if (sendAck) { - char buffer[1] = {0x01}; + unsigned char tmpBuf[1] = {0x01}; sendAck = 0; - WOLFTPM_WRITE(ctx->tcpCtx.fd, buffer, 1); + WOLFTPM_WRITE(ctx->tcpCtx.fd, tmpBuf, 1); } if (rxBuffIdx >= rxRemain || rxRemain == 0) { break; } + + if ((timeOut--) <= 0) { + rxBuffIdx = 0; /* reset read state */ + rc = SOCKET_ERROR_E; /* timeout */ + #if DEBUG_WOLFTPM + DEBUG_PRINTF("Connection timed out\r\n"); + #endif + break; + } continue; /* keep trying */ } @@ -364,7 +375,7 @@ static int SwTpmReceive(TPM2_CTX* ctx, void* buffer, size_t rxSz) } -static int SwTpmConnect(TPM2_CTX* ctx, const char* uartDev, uint32_t baud) +static int SwTpmConnect(TPM2_CTX* ctx, uint32_t baud) { int ret = TPM_RC_SUCCESS; XUartNs550_Config *config; @@ -510,12 +521,14 @@ int TPM2_SWTPM_SendCommand(TPM2_CTX* ctx, TPM2_Packet* packet) #if !defined(WOLFTPM_SWTPM_UARTNS550) if (ctx->tcpCtx.fd < 0) { + rc = SwTpmConnect(ctx, TPM2_SWTPM_HOST, TPM2_SWTPM_PORT); + } #else if (ctx->tcpCtx.setup == 0) { ctx->tcpCtx.setup = 1; -#endif - rc = SwTpmConnect(ctx, TPM2_SWTPM_HOST, TPM2_SWTPM_PORT); + rc = SwTpmConnect(ctx, TPM2_SWTPM_PORT); } +#endif #ifdef WOLFTPM_DEBUG_VERBOSE DEBUG_PRINTF("Command size: %d\n\r", packet->pos);