diff --git a/firmware/include/tlcl_stub.h b/firmware/include/tlcl_stub.h index 72582af056..b9a1f9abdb 100644 --- a/firmware/include/tlcl_stub.h +++ b/firmware/include/tlcl_stub.h @@ -17,15 +17,15 @@ /*****************************************************************************/ /* Functions to be implemented by the stub library */ -/* Initialize the stub library */ -void TlclStubInit(void); +/* Initialize the stub library. Returns 0 if success, nonzero if error. */ +uint32_t TlclStubInit(void); /* Close and open the device. This is needed for running more complex commands * at user level, such as TPM_TakeOwnership, since the TPM device can be opened - * only by one process at a time. + * only by one process at a time. Returns 0 if success, nonzero if error. */ -void TlclCloseDevice(void); -void TlclOpenDevice(void); +uint32_t TlclCloseDevice(void); +uint32_t TlclOpenDevice(void); /* Send data to the TPM and receive a response. Returns 0 if success, * nonzero if error. */ diff --git a/firmware/include/tss_constants.h b/firmware/include/tss_constants.h index 00a7c033a7..b35f7613bf 100644 --- a/firmware/include/tss_constants.h +++ b/firmware/include/tss_constants.h @@ -22,6 +22,7 @@ #define TPM_E_AREA_LOCKED ((uint32_t)0x0000003c) #define TPM_E_BADINDEX ((uint32_t)0x00000002) #define TPM_E_BAD_PRESENCE ((uint32_t)0x0000002d) +#define TPM_E_IOERROR ((uint32_t)0x0000001f) #define TPM_E_INVALID_POSTINIT ((uint32_t)0x00000026) #define TPM_E_MAXNVWRITES ((uint32_t)0x00000048) #define TPM_E_OWNER_SET ((uint32_t)0x00000014) diff --git a/firmware/lib/rollback_index.c b/firmware/lib/rollback_index.c index 766b62e263..efbc87be88 100644 --- a/firmware/lib/rollback_index.c +++ b/firmware/lib/rollback_index.c @@ -197,8 +197,7 @@ uint32_t SetupTPM(int recovery_mode, int developer_mode, g_rollback_recovery_mode = 1; /* Global variables are usable in * recovery mode */ - /* TODO: TlclLibInit() should be able to return failure */ - TlclLibInit(); + RETURN_ON_FAILURE(TlclLibInit()); RETURN_ON_FAILURE(TlclStartup()); /* Use ContinueSelfTest rather than SelfTestFull(). It enables @@ -336,7 +335,7 @@ uint32_t RollbackKernelLock(void) { uint32_t RollbackS3Resume(void) { uint32_t result; - TlclLibInit(); + RETURN_ON_FAILURE(TlclLibInit()); result = TlclResume(); if (result == TPM_E_INVALID_POSTINIT) { /* We're on a platform where the TPM maintains power in S3, so diff --git a/firmware/lib/tpm_lite/include/tlcl.h b/firmware/lib/tpm_lite/include/tlcl.h index 401a3ae31c..9531054141 100644 --- a/firmware/lib/tpm_lite/include/tlcl.h +++ b/firmware/lib/tpm_lite/include/tlcl.h @@ -18,9 +18,9 @@ /*****************************************************************************/ /* Functions implemented in tlcl.c */ -/* Call this first. +/* Call this first. Returns 0 if success, nonzero if error. */ -void TlclLibInit(void); +uint32_t TlclLibInit(void); /* Logs to stdout. Arguments like printf. */ diff --git a/firmware/lib/tpm_lite/tlcl.c b/firmware/lib/tpm_lite/tlcl.c index cc0c373af6..99cc165afd 100644 --- a/firmware/lib/tpm_lite/tlcl.c +++ b/firmware/lib/tpm_lite/tlcl.c @@ -91,8 +91,8 @@ static uint32_t Send(const uint8_t* command) { /* Exported functions. */ -void TlclLibInit(void) { - TlclStubInit(); +uint32_t TlclLibInit(void) { + return TlclStubInit(); } uint32_t TlclStartup(void) { diff --git a/firmware/stub/tpm_lite_stub.c b/firmware/stub/tpm_lite_stub.c index 2695fae35d..2351ee47e7 100644 --- a/firmware/stub/tpm_lite_stub.c +++ b/firmware/stub/tpm_lite_stub.c @@ -99,22 +99,25 @@ POSSIBLY_UNUSED static INLINE int TpmResponseSize(const uint8_t* buffer) { } -void TlclStubInit(void) { - TlclOpenDevice(); +uint32_t TlclStubInit(void) { + return TlclOpenDevice(); } -void TlclCloseDevice(void) { - close(tpm_fd); - tpm_fd = -1; +uint32_t TlclCloseDevice(void) { + if (tpm_fd != -1) { + close(tpm_fd); + tpm_fd = -1; + } + return 0; } -void TlclOpenDevice(void) { +uint32_t TlclOpenDevice(void) { char* device_path; if (tpm_fd >= 0) - return; /* Already open */ + return 0; /* Already open */ device_path = getenv("TPM_DEVICE_PATH"); if (device_path == NULL) { @@ -123,8 +126,12 @@ void TlclOpenDevice(void) { tpm_fd = open(device_path, O_RDWR); if (tpm_fd < 0) { - error("cannot open TPM device %s: %s\n", device_path, strerror(errno)); + VBDEBUG(("TPM: Cannot open TPM device %s: %s\n", device_path, + strerror(errno))); + return TPM_E_IOERROR; } + + return 0; }