Minor fixes for the TPM Linux device interface and debug improvements

Signed-off-by: Dimitar Tomov <dimi@designfirst.ee>
pull/91/head
Dimitar Tomov 2020-04-28 22:07:02 +03:00 committed by David Garske
parent d6abe06a5d
commit 9416f1ef09
5 changed files with 36 additions and 11 deletions

View File

@ -242,6 +242,7 @@ AM_CONDITIONAL([BUILD_ADVIO], [test "x$ENABLED_ADVIO" = "xyes"])
AM_CONDITIONAL([BUILD_ST33], [test "x$ENABLED_ST33" = "xyes"]) AM_CONDITIONAL([BUILD_ST33], [test "x$ENABLED_ST33" = "xyes"])
AM_CONDITIONAL([BUILD_MCHP], [test "x$ENABLED_MCHP" = "xyes"]) AM_CONDITIONAL([BUILD_MCHP], [test "x$ENABLED_MCHP" = "xyes"])
AM_CONDITIONAL([BUILD_INFINEON], [test "x$ENABLED_INFINEON" = "xyes"]) AM_CONDITIONAL([BUILD_INFINEON], [test "x$ENABLED_INFINEON" = "xyes"])
AM_CONDITIONAL([BUILD_DEVTPM], [test "x$ENABLED_DEVTPM" = "xyes"])
@ -359,3 +360,4 @@ echo " * Infineon SLB9670 $ENABLED_INFINEON"
echo " * STM ST33: $ENABLED_ST33" echo " * STM ST33: $ENABLED_ST33"
echo " * Microchip ATTPM20: $ENABLED_MCHP" echo " * Microchip ATTPM20: $ENABLED_MCHP"
echo " * I2C: $ENABLED_I2C" echo " * I2C: $ENABLED_I2C"
echo " * Linux kernel TPM device: $ENABLED_DEVTPM"

View File

@ -167,7 +167,11 @@ static TPM_RC TPM2_SendCommandAuth(TPM2_CTX* ctx, TPM2_Packet* packet,
} }
/* submit command and wait for response */ /* submit command and wait for response */
#ifdef WOLFTPM_LINUX_DEV
rc = (TPM_RC)TPM2_LINUX_SendCommand(ctx, cmd, cmdSz);
#else
rc = (TPM_RC)TPM2_TIS_SendCommand(ctx, cmd, cmdSz); rc = (TPM_RC)TPM2_TIS_SendCommand(ctx, cmd, cmdSz);
#endif
/* parse response */ /* parse response */
rc = TPM2_Packet_Parse(rc, packet); rc = TPM2_Packet_Parse(rc, packet);

View File

@ -20,6 +20,7 @@
*/ */
#ifdef WOLFTPM_LINUX_DEV
#include <wolftpm/tpm2_linux.h> #include <wolftpm/tpm2_linux.h>
#include <wolftpm/tpm2_packet.h> #include <wolftpm/tpm2_packet.h>
#include <wolftpm/tpm2_wrap.h> /* Needed only for WOLFTPM2_MAX_BUFFER */ #include <wolftpm/tpm2_wrap.h> /* Needed only for WOLFTPM2_MAX_BUFFER */
@ -32,7 +33,10 @@
#include <string.h> #include <string.h>
#ifndef TPM2_LINUX_DEV
#define TPM2_LINUX_DEV "/dev/tpm0" #define TPM2_LINUX_DEV "/dev/tpm0"
#endif
#define TPM2_LINUX_DEV_POLL_TIMEOUT -1 /* Infinite time for poll events */ #define TPM2_LINUX_DEV_POLL_TIMEOUT -1 /* Infinite time for poll events */
#define TPM2_LINUX_DEV_RSP_SIZE WOLFTPM2_MAX_BUFFER #define TPM2_LINUX_DEV_RSP_SIZE WOLFTPM2_MAX_BUFFER
/* Linux kernels older than v4.20 (before December 2018) do not support /* Linux kernels older than v4.20 (before December 2018) do not support
@ -49,11 +53,12 @@
int TPM2_LINUX_SendCommand(TPM2_CTX* ctx, byte* cmd, word16 cmdSz) int TPM2_LINUX_SendCommand(TPM2_CTX* ctx, byte* cmd, word16 cmdSz)
{ {
int rc = TPM_RC_FAILURE; int rc = TPM_RC_FAILURE;
int fd, rspSz; int fd;
int rc_poll, nfds= 1; /* Polling single TPM dev file */ int rc_poll, nfds = 1; /* Polling single TPM dev file */
struct pollfd fds; struct pollfd fds;
size_t rspSz = 0;
#ifdef DEBUG_WOLFTPM /* TODO: Change to WOLFTPM_DEBUG_VERBOSE */ #ifdef WOLFTPM_DEBUG_VERBOSE
printf("Command size: %d\n", cmdSz); printf("Command size: %d\n", cmdSz);
TPM2_PrintBin(cmd, cmdSz); TPM2_PrintBin(cmd, cmdSz);
#endif #endif
@ -79,7 +84,7 @@ int TPM2_LINUX_SendCommand(TPM2_CTX* ctx, byte* cmd, word16 cmdSz)
#ifdef DEBUG_WOLFTPM #ifdef DEBUG_WOLFTPM
else else
{ {
printf("Response size is %d bytes, not enough to " printf("Response size is %ld bytes, not enough to "
"hold TPM response.\n", rspSz); "hold TPM response.\n", rspSz);
} }
} }
@ -92,20 +97,32 @@ int TPM2_LINUX_SendCommand(TPM2_CTX* ctx, byte* cmd, word16 cmdSz)
#endif #endif
} }
} }
#ifdef DEBUG_WOLFTPM /* TODO: Change to WOLFTPM_DEBUG_TIMEOUT */ #ifdef WOLFTPM_DEBUG_VERBOSE
else { else {
printf("Failed to get a response from fd %d, got errno %d =" printf("Failed to get a response from fd %d, got errno %d ="
"%s\n", fd, errno, strerror(errno)); "%s\n", fd, errno, strerror(errno));
} }
}
else {
printf("Failed to send the TPM command to fd %d, got errno %d ="
"%s\n", fd, errno, strerror(errno));
#endif #endif
} }
close(fd); close(fd);
} }
#ifdef DEBUG_WOLFTPM
else if (fd == -1 && errno == EACCES) {
printf("Permission denied. Use sudo or change the user group.\n");
}
else {
perror("Failed to open device");
}
#endif
#ifdef DEBUG_WOLFTPM /* TODO: Change to WOLFTPM_DEBUG_VERBOSE */ #ifdef WOLFTPM_DEBUG_VERBOSE
if (rspSz > 0) { if (rspSz > 0) {
printf("Response size: %d\n", rspSz); printf("Response size: %ld\n", rspSz);
TPM2_PrintBin(cmd, rspSz); TPM2_PrintBin(cmd, rspSz);
} }
#endif #endif
@ -114,3 +131,4 @@ int TPM2_LINUX_SendCommand(TPM2_CTX* ctx, byte* cmd, word16 cmdSz)
return rc; return rc;
} }
#endif

View File

@ -8,6 +8,7 @@ nobase_include_HEADERS+= \
wolftpm/tpm2_tis.h \ wolftpm/tpm2_tis.h \
wolftpm/tpm2_types.h \ wolftpm/tpm2_types.h \
wolftpm/tpm2_wrap.h \ wolftpm/tpm2_wrap.h \
wolftpm/tpm2_linux.h \
wolftpm/version.h \ wolftpm/version.h \
wolftpm/visibility.h \ wolftpm/visibility.h \
wolftpm/options.h wolftpm/options.h