Improve error logging when wolfTPM2_Init fails

Resolves #457. When the SPI device could not be opened (e.g., kernel TPM
  driver owns it), wolfTPM failed silently with no error output. Users had
  no indication of what went wrong or how to fix it.

  Changes:
  - Make TPM2_Init failed message in wolfTPM2_Init_ex always print
    (previously required --enable-debug)
  - Add SPI/I2C open failure logging in hal/tpm_io_linux.c: permission
    denied prints unconditionally, other errors print with DEBUG_WOLFTPM
  - Add autodetect scan exhaustion message (DEBUG_WOLFTPM)
  - Fix examples (caps, wrap_test, bench, pkcs7) that silently returned
    on init failure to print error code and RC string
  - Update 13 other examples to include error code in init failure message
pull/460/head
Aidan Garske 2026-02-20 20:48:25 +00:00
parent 438c63e113
commit 978b72fb12
19 changed files with 70 additions and 22 deletions

View File

@ -262,7 +262,10 @@ int TPM2_Wrapper_BenchArgs(void* userCtx, int argc, char *argv[])
/* Init the TPM2 device */
rc = wolfTPM2_Init(&dev, TPM2_IoCb, userCtx);
if (rc != 0) return rc;
if (rc != 0) {
printf("wolfTPM2_Init failed 0x%x: %s\n", rc, TPM2_GetRCString(rc));
return rc;
}
/* See if primary storage key already exists */
rc = getPrimaryStoragekey(&dev, &storageKey, TPM_ALG_RSA);

View File

@ -175,7 +175,7 @@ int TPM2_Boot_SecureROT_Example(void* userCtx, int argc, char *argv[])
rc = wolfTPM2_Init(&dev, TPM2_IoCb, userCtx);
if (rc != TPM_RC_SUCCESS) {
printf("\nwolfTPM2_Init failed\n");
printf("\nwolfTPM2_Init failed 0x%x: %s\n", rc, TPM2_GetRCString(rc));
goto exit;
}

View File

@ -78,7 +78,7 @@ int TPM2_GPIO_Read_Example(void* userCtx, int argc, char *argv[])
rc = wolfTPM2_Init(&dev, TPM2_IoCb, userCtx);
if (rc != TPM_RC_SUCCESS) {
printf("\nwolfTPM2_Init failed\n");
printf("\nwolfTPM2_Init failed 0x%x: %s\n", rc, TPM2_GetRCString(rc));
goto exit;
}

View File

@ -91,7 +91,7 @@ int TPM2_GPIO_Set_Example(void* userCtx, int argc, char *argv[])
rc = wolfTPM2_Init(&dev, TPM2_IoCb, userCtx);
if (rc != TPM_RC_SUCCESS) {
printf("\nwolfTPM2_Init failed\n");
printf("\nwolfTPM2_Init failed 0x%x: %s\n", rc, TPM2_GetRCString(rc));
goto exit;
}

View File

@ -180,7 +180,7 @@ int TPM2_CreatePrimaryKey_Example(void* userCtx, int argc, char *argv[])
rc = wolfTPM2_Init(&dev, TPM2_IoCb, userCtx);
if (rc != TPM_RC_SUCCESS) {
printf("\nwolfTPM2_Init failed\n");
printf("\nwolfTPM2_Init failed 0x%x: %s\n", rc, TPM2_GetRCString(rc));
goto exit;
}

View File

@ -244,7 +244,7 @@ int TPM2_Keygen_Example(void* userCtx, int argc, char *argv[])
rc = wolfTPM2_Init(&dev, TPM2_IoCb, userCtx);
if (rc != TPM_RC_SUCCESS) {
printf("\nwolfTPM2_Init failed\n");
printf("\nwolfTPM2_Init failed 0x%x: %s\n", rc, TPM2_GetRCString(rc));
goto exit;
}

View File

@ -155,7 +155,7 @@ int TPM2_Keyimport_Example(void* userCtx, int argc, char *argv[])
rc = wolfTPM2_Init(&dev, TPM2_IoCb, userCtx);
if (rc != TPM_RC_SUCCESS) {
printf("\nwolfTPM2_Init failed\n");
printf("\nwolfTPM2_Init failed 0x%x: %s\n", rc, TPM2_GetRCString(rc));
goto exit;
}

View File

@ -118,7 +118,7 @@ int TPM2_Keyload_Example(void* userCtx, int argc, char *argv[])
rc = wolfTPM2_Init(&dev, TPM2_IoCb, userCtx);
if (rc != TPM_RC_SUCCESS) {
printf("\nwolfTPM2_Init failed\n");
printf("\nwolfTPM2_Init failed 0x%x: %s\n", rc, TPM2_GetRCString(rc));
goto exit;
}

View File

@ -125,7 +125,7 @@ int TPM2_NVRAM_Counter_Example(void* userCtx, int argc, char *argv[])
rc = wolfTPM2_Init(&dev, TPM2_IoCb, userCtx);
if (rc != TPM_RC_SUCCESS) {
printf("wolfTPM2_Init failed\n");
printf("wolfTPM2_Init failed 0x%x: %s\n", rc, TPM2_GetRCString(rc));
goto exit;
}

View File

@ -153,7 +153,7 @@ int TPM2_NVRAM_Extend_Example(void* userCtx, int argc, char *argv[])
rc = wolfTPM2_Init(&dev, TPM2_IoCb, userCtx);
if (rc != TPM_RC_SUCCESS) {
printf("wolfTPM2_Init failed\n");
printf("wolfTPM2_Init failed 0x%x: %s\n", rc, TPM2_GetRCString(rc));
goto exit;
}

View File

@ -147,7 +147,7 @@ int TPM2_NVRAM_PolicyNV_Example(void* userCtx, int argc, char *argv[])
rc = wolfTPM2_Init(&dev, TPM2_IoCb, userCtx);
if (rc != TPM_RC_SUCCESS) {
printf("\nwolfTPM2_Init failed\n");
printf("\nwolfTPM2_Init failed 0x%x: %s\n", rc, TPM2_GetRCString(rc));
goto exit;
}

View File

@ -156,7 +156,7 @@ int TPM2_NVRAM_Read_Example(void* userCtx, int argc, char *argv[])
rc = wolfTPM2_Init(&dev, TPM2_IoCb, userCtx);
if (rc != TPM_RC_SUCCESS) {
printf("\nwolfTPM2_Init failed\n");
printf("\nwolfTPM2_Init failed 0x%x: %s\n", rc, TPM2_GetRCString(rc));
goto exit;
}

View File

@ -147,7 +147,7 @@ int TPM2_NVRAM_Store_Example(void* userCtx, int argc, char *argv[])
rc = wolfTPM2_Init(&dev, TPM2_IoCb, userCtx);
if (rc != TPM_RC_SUCCESS) {
printf("\nwolfTPM2_Init failed\n");
printf("\nwolfTPM2_Init failed 0x%x: %s\n", rc, TPM2_GetRCString(rc));
goto exit;
}

View File

@ -417,7 +417,10 @@ int TPM2_PKCS7_ExampleArgs(void* userCtx, int argc, char *argv[])
/* Init the TPM2 device */
rc = wolfTPM2_Init(&dev, TPM2_IoCb, userCtx);
if (rc != 0) return rc;
if (rc != 0) {
printf("wolfTPM2_Init failed 0x%x: %s\n", rc, TPM2_GetRCString(rc));
return rc;
}
/* Setup the wolf crypto device callback */
XMEMSET(&tpmCtx, 0, sizeof(tpmCtx));

View File

@ -104,7 +104,7 @@ int TPM2_Seal_Example(void* userCtx, int argc, char *argv[])
rc = wolfTPM2_Init(&dev, TPM2_IoCb, userCtx);
if (rc != TPM_RC_SUCCESS) {
printf("\nwolfTPM2_Init failed\n");
printf("\nwolfTPM2_Init failed 0x%x: %s\n", rc, TPM2_GetRCString(rc));
goto exit;
}

View File

@ -106,7 +106,10 @@ int TPM2_Wrapper_CapsArgs(void* userCtx, int argc, char *argv[])
/* Init the TPM2 device */
rc = wolfTPM2_Init(&dev, TPM2_IoCb, userCtx);
if (rc != 0) return rc;
if (rc != 0) {
printf("wolfTPM2_Init failed 0x%x: %s\n", rc, TPM2_GetRCString(rc));
return rc;
}
rc = wolfTPM2_GetCapabilities(&dev, &caps);
if (rc != 0) goto exit;

View File

@ -168,7 +168,10 @@ int TPM2_Wrapper_TestArgs(void* userCtx, int argc, char *argv[])
/* Init the TPM2 device */
rc = wolfTPM2_Init(&dev, TPM2_IoCb, userCtx);
if (rc != 0) return rc;
if (rc != 0) {
printf("wolfTPM2_Init failed 0x%x: %s\n", rc, TPM2_GetRCString(rc));
return rc;
}
#ifdef WOLFTPM_CRYPTOCB
/* Setup the wolf crypto device callback */

View File

@ -63,12 +63,14 @@
#endif
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#ifdef WOLFTPM_I2C
/* I2C - (Only tested with SLB9673 and ST33 I2C) */
#define TPM2_I2C_ADDR 0x2e
#define TPM2_I2C_DEV "/dev/i2c-1"
#define TPM2_I2C_HZ 400000 /* 400kHz */
static int i2cOpenFailed = 0;
#else
/* SPI */
#ifndef TPM2_SPI_DEV_CS
@ -97,8 +99,10 @@
static char TPM2_SPI_DEV[] = TPM2_SPI_DEV_PATH "0";
#define MAX_SPI_DEV_CS '4'
static int foundSpiDev = 0;
static int spiDevNotFound = 0;
#else
#define TPM2_SPI_DEV TPM2_SPI_DEV_PATH TPM2_SPI_DEV_CS
static int spiOpenFailed = 0;
#endif
#endif
#endif
@ -190,6 +194,20 @@
close(i2cDev);
}
else if (!i2cOpenFailed) {
i2cOpenFailed = 1;
if (errno == EACCES) {
printf("Permission denied on %s\n"
"Use sudo or add appropriate group to user.\n",
TPM2_I2C_DEV);
}
#ifdef DEBUG_WOLFTPM
else {
printf("Failed to open I2C device %s (errno %d)\n",
TPM2_I2C_DEV, errno);
}
#endif
}
(void)ctx;
(void)userCtx;
@ -308,6 +326,22 @@
else {
/* Failed to open device */
ret = TPM_RC_FAILURE;
#ifndef WOLFTPM_AUTODETECT
if (!spiOpenFailed) {
spiOpenFailed = 1;
if (errno == EACCES) {
printf("Permission denied on %s\n"
"Use sudo or check device permissions.\n",
TPM2_SPI_DEV);
}
#ifdef DEBUG_WOLFTPM
else {
printf("Failed to open SPI device %s (errno %d)\n",
TPM2_SPI_DEV, errno);
}
#endif
}
#endif
}
#ifdef WOLFTPM_AUTODETECT
@ -326,6 +360,13 @@
TPM2_SPI_DEV[devLen-1]++;
goto tryagain;
}
#ifdef DEBUG_WOLFTPM
if (!spiDevNotFound) {
spiDevNotFound = 1;
printf("TPM not found on SPI bus %s[0-%c]\n",
TPM2_SPI_DEV_PATH, MAX_SPI_DEV_CS);
}
#endif
}
}
#endif

View File

@ -74,9 +74,7 @@ static int wolfTPM2_Init_ex(TPM2_CTX* ctx, TPM2HalIoCb ioCb, void* userCtx,
rc = TPM2_Init_ex(ctx, ioCb, userCtx, timeoutTries);
#endif
if (rc != TPM_RC_SUCCESS) {
#ifdef DEBUG_WOLFTPM
printf("TPM2_Init failed %d: %s\n", rc, wolfTPM2_GetRCString(rc));
#endif
return rc;
}
#ifdef DEBUG_WOLFTPM
@ -561,9 +559,6 @@ int wolfTPM2_OpenExisting(WOLFTPM2_DEV* dev, TPM2HalIoCb ioCb, void* userCtx)
/* The 0 startup indicates use existing locality */
rc = wolfTPM2_Init_ex(&dev->ctx, ioCb, userCtx, 0);
if (rc != TPM_RC_SUCCESS) {
#ifdef DEBUG_WOLFTPM
printf("TPM2_Init failed %d: %s\n", rc, wolfTPM2_GetRCString(rc));
#endif
return rc;
}