Add device open/close.
Review URL: http://codereview.chromium.org/2822042
diff --git a/src/tlcl/tlcl.c b/src/tlcl/tlcl.c
index 631ecb0..1b22b43 100644
--- a/src/tlcl/tlcl.c
+++ b/src/tlcl/tlcl.c
@@ -39,6 +39,8 @@
#include "tpmemu.h"
#endif
+#define TPM_DEVICE_PATH "/dev/tpm0"
+
/* The file descriptor for the TPM device.
*/
int tpm_fd = -1;
@@ -231,13 +233,24 @@
#if USE_TPM_EMULATOR
tpmemu_init();
#else
+ TlclOpenDevice();
+#endif /* USE_TPM_EMULATOR */
+}
+
+void TlclCloseDevice(void) {
#if !FIRMWARE
- tpm_fd = open("/dev/tpm0", O_RDWR);
+ close(tpm_fd);
+ tpm_fd = -1;
+#endif /* !FIRMWARE */
+}
+
+void TlclOpenDevice(void) {
+#if !FIRMWARE
+ tpm_fd = open(TPM_DEVICE_PATH, O_RDWR);
if (tpm_fd < 0) {
- error("cannot open TPM device: %s\n", strerror(errno));
+ error("cannot open TPM device %s: %s\n", TPM_DEVICE_PATH, strerror(errno));
}
#endif /* !FIRMWARE */
-#endif /* USE_TPM_EMULATOR */
}
uint32_t TlclStartup(void) {
diff --git a/src/tlcl/tlcl.h b/src/tlcl/tlcl.h
index 570d884..4dce9b1 100644
--- a/src/tlcl/tlcl.h
+++ b/src/tlcl/tlcl.h
@@ -55,6 +55,13 @@
*/
void TlclLibInit(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.
+ */
+void TlclCloseDevice(void);
+void TlclOpenDevice(void);
+
/* Logs to stdout. Arguments like printf.
*/
void TlclLog(char* format, ...);
diff --git a/src/tlcl/version.h b/src/tlcl/version.h
index d5aa7c6..f145ed1 100644
--- a/src/tlcl/version.h
+++ b/src/tlcl/version.h
@@ -1 +1 @@
-char* TlclVersion = "TLCLv=d2cb8e8d";
+char* TlclVersion = "TLCLv=6ff70681";