Skip to content

Commit

Permalink
warning fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
JacobBarthelmeh committed Dec 12, 2023
1 parent 33b6ede commit 2a1d768
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions src/tpm2_swtpm.c
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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;
Expand All @@ -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) {
Expand All @@ -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 */
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 2a1d768

Please sign in to comment.