8484 #define TPM2_SWTPM_PORT 115200
8585 #endif
8686 #ifndef TPM2_TIMEOUT_SECONDS
87- #define TPM2_TIMEOUT_SECONDS 7200
87+ #define TPM2_TIMEOUT_SECONDS 21600
8888 #endif
8989 #define WOLFTPM_WRITE (u , b , sz ) XUartNs550_Send(&(u), (b), (sz))
9090 #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)
282282 int rc ;
283283 size_t remain , rxRemain ;
284284 int sendAck = 0 ;
285+ int timeOut = TPM2_TIMEOUT_SECONDS ;
285286
286287 if (ctx == NULL || buffer == NULL ) {
287288 return BAD_FUNC_ARG ;
@@ -292,7 +293,7 @@ static int SwTpmReceive(TPM2_CTX* ctx, void* buffer, size_t rxSz)
292293
293294 /* use up any leftovers before trying to pull more */
294295 if (rxBuffIdx > 0 ) {
295- int minSz = (rxBuffIdx < remain )? rxBuffIdx : remain ;
296+ int minSz = (rxBuffIdx < ( int ) remain )? rxBuffIdx : ( int ) remain ;
296297
297298 memcpy (buffer , rxBuff , minSz );
298299 if (rxBuffIdx > minSz ) {
@@ -314,19 +315,29 @@ static int SwTpmReceive(TPM2_CTX* ctx, void* buffer, size_t rxSz)
314315 if (rc > 0 ) {
315316 usleep (500 );
316317 sendAck = 1 ;
318+ timeOut = TPM2_TIMEOUT_SECONDS ; /* reset timeout */
317319 }
318320
319321 if (rc == 0 ) {
320322 if (sendAck ) {
321- char buffer [1 ] = {0x01 };
323+ unsigned char tmpBuf [1 ] = {0x01 };
322324
323325 sendAck = 0 ;
324- WOLFTPM_WRITE (ctx -> tcpCtx .fd , buffer , 1 );
326+ WOLFTPM_WRITE (ctx -> tcpCtx .fd , tmpBuf , 1 );
325327 }
326328
327329 if (rxBuffIdx >= rxRemain || rxRemain == 0 ) {
328330 break ;
329331 }
332+
333+ if ((timeOut -- ) <= 0 ) {
334+ rxBuffIdx = 0 ; /* reset read state */
335+ rc = SOCKET_ERROR_E ; /* timeout */
336+ #if DEBUG_WOLFTPM
337+ DEBUG_PRINTF ("Connection timed out\r\n" );
338+ #endif
339+ break ;
340+ }
330341 continue ; /* keep trying */
331342 }
332343
@@ -364,7 +375,7 @@ static int SwTpmReceive(TPM2_CTX* ctx, void* buffer, size_t rxSz)
364375}
365376
366377
367- static int SwTpmConnect (TPM2_CTX * ctx , const char * uartDev , uint32_t baud )
378+ static int SwTpmConnect (TPM2_CTX * ctx , uint32_t baud )
368379{
369380 int ret = TPM_RC_SUCCESS ;
370381 XUartNs550_Config * config ;
@@ -510,12 +521,14 @@ int TPM2_SWTPM_SendCommand(TPM2_CTX* ctx, TPM2_Packet* packet)
510521
511522#if !defined(WOLFTPM_SWTPM_UARTNS550 )
512523 if (ctx -> tcpCtx .fd < 0 ) {
524+ rc = SwTpmConnect (ctx , TPM2_SWTPM_HOST , TPM2_SWTPM_PORT );
525+ }
513526#else
514527 if (ctx -> tcpCtx .setup == 0 ) {
515528 ctx -> tcpCtx .setup = 1 ;
516- #endif
517- rc = SwTpmConnect (ctx , TPM2_SWTPM_HOST , TPM2_SWTPM_PORT );
529+ rc = SwTpmConnect (ctx , TPM2_SWTPM_PORT );
518530 }
531+ #endif
519532
520533#ifdef WOLFTPM_DEBUG_VERBOSE
521534 DEBUG_PRINTF ("Command size: %d\n\r" , packet -> pos );
0 commit comments