@@ -118,7 +118,6 @@ int TPM2_LINUX_SendCommand(TPM2_CTX* ctx, TPM2_Packet* packet)
118118int 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__ */
0 commit comments