Skip to content

Commit de4f085

Browse files
authored
Merge pull request #435 from danielinux/linuxdev-persistent-fd
Persistent access to /dev/tpmrmX
2 parents 7e7046d + 9dd6c1f commit de4f085

File tree

3 files changed

+19
-12
lines changed

3 files changed

+19
-12
lines changed

src/tpm2.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -645,6 +645,10 @@ TPM_RC TPM2_Init_ex(TPM2_CTX* ctx, TPM2HalIoCb ioCb, void* userCtx,
645645
return rc;
646646
#endif
647647

648+
#ifdef WOLFTPM_LINUX_DEV
649+
ctx->fd = -1;
650+
#endif
651+
648652
/* Set the active TPM global */
649653
TPM2_SetActiveCtx(ctx);
650654

@@ -712,6 +716,11 @@ TPM_RC TPM2_Cleanup(TPM2_CTX* ctx)
712716
}
713717
#endif /* !WOLFTPM2_NO_WOLFCRYPT */
714718

719+
#ifdef WOLFTPM_LINUX_DEV
720+
if (ctx->fd >= 0)
721+
close(ctx->fd);
722+
#endif
723+
715724
return TPM_RC_SUCCESS;
716725
}
717726

src/tpm2_linux.c

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,6 @@ int TPM2_LINUX_SendCommand(TPM2_CTX* ctx, TPM2_Packet* packet)
118118
int TPM2_LINUX_SendCommand(TPM2_CTX* ctx, TPM2_Packet* packet)
119119
{
120120
int rc = TPM_RC_FAILURE;
121-
int fd;
122121
int rc_poll, nfds = 1; /* Polling single TPM dev file */
123122
struct pollfd fds;
124123
int rspSz = 0;
@@ -128,16 +127,17 @@ int TPM2_LINUX_SendCommand(TPM2_CTX* ctx, TPM2_Packet* packet)
128127
TPM2_PrintBin(packet->buf, packet->pos);
129128
#endif
130129

131-
fd = open(TPM2_LINUX_DEV, O_RDWR | O_NONBLOCK);
132-
if (fd >= 0) {
130+
if (ctx->fd < 0)
131+
ctx->fd = open(TPM2_LINUX_DEV, O_RDWR | O_NONBLOCK);
132+
if (ctx->fd >= 0) {
133133
/* Send the TPM command */
134-
if (write(fd, packet->buf, packet->pos) == packet->pos) {
135-
fds.fd = fd;
134+
if (write(ctx->fd, packet->buf, packet->pos) == packet->pos) {
135+
fds.fd = ctx->fd;
136136
fds.events = POLLIN;
137137
/* Wait for response to be available */
138138
rc_poll = poll(&fds, nfds, TPM2_LINUX_DEV_POLL_TIMEOUT);
139139
if (rc_poll > 0 && fds.revents == POLLIN) {
140-
ssize_t ret = read(fd, packet->buf, packet->size);
140+
ssize_t ret = read(ctx->fd, packet->buf, packet->size);
141141
/* The caller parses the TPM_Packet for correctness */
142142
if (ret >= TPM2_HEADER_SIZE) {
143143
/* Enough bytes for a TPM response */
@@ -173,10 +173,8 @@ int TPM2_LINUX_SendCommand(TPM2_CTX* ctx, TPM2_Packet* packet)
173173
#endif
174174
rc = TPM_RC_FAILURE;
175175
}
176-
177-
close(fd);
178176
}
179-
else if (fd == -1 && errno == EACCES) {
177+
else if (ctx->fd == -1 && errno == EACCES) {
180178
printf("Permission denied on %s\n"
181179
"Use sudo or add tss group to user.\n", TPM2_LINUX_DEV);
182180
}
@@ -193,9 +191,6 @@ int TPM2_LINUX_SendCommand(TPM2_CTX* ctx, TPM2_Packet* packet)
193191
TPM2_PrintBin(packet->buf, rspSz);
194192
}
195193
#endif
196-
197-
(void)ctx;
198-
199194
return rc;
200195
}
201196
#endif /* __UBOOT__ __linux__ */

wolftpm/tpm2.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1883,6 +1883,9 @@ typedef struct TPM2_CTX {
18831883
unsigned int rngInit:1;
18841884
#endif
18851885
#endif
1886+
#ifdef WOLFTPM_LINUX_DEV
1887+
int fd;
1888+
#endif
18861889
} TPM2_CTX;
18871890

18881891

0 commit comments

Comments
 (0)