]> git.proxmox.com Git - libtpms.git/blobdiff - src/tpm_library.c
Move common debug, memory & nvfile units to src/
[libtpms.git] / src / tpm_library.c
index 5953a4f1410731e0a8601bcd333e3b572974ff00..460cd59c0fec08e4c5eb124a6678e119702828ad 100644 (file)
@@ -41,6 +41,9 @@
 
 #include <assert.h>
 #include <string.h>
+#if defined __FreeBSD__
+# define _WITH_DPRINTF
+#endif
 #include <stdio.h>
 #include <stdlib.h>
 #include <stdarg.h>
 # include <openssl/evp.h>
 #endif
 
-#include "tpm12/tpm_debug.h"
+#include "tpm_debug.h"
 #include "tpm_error.h"
 #include "tpm_library.h"
 #include "tpm_library_intern.h"
-#include "tpm_memory.h"
 #include "tpm_nvfilename.h"
+#include "tpm_tis.h"
 
 static const struct tags_and_indices {
     const char    *starttag;
@@ -107,10 +110,16 @@ TPM_RESULT TPMLIB_ChooseTPMVersion(TPMLIB_TPMVersion ver)
 
     switch (ver) {
     case TPMLIB_TPM_VERSION_1_2:
+        if (tpmvers_choice != 0)
+            ClearAllCachedState();
+
         tpmvers_choice = 0; // entry 0 in tpm_iface
         break;
     case TPMLIB_TPM_VERSION_2:
 #if WITH_TPM2
+        if (tpmvers_choice != 1)
+            ClearAllCachedState();
+
         tpmvers_choice = 1; // entry 1 in tpm_iface
         break;
 #endif
@@ -201,13 +210,13 @@ char *TPMLIB_GetInfo(enum TPMLIB_InfoFlags flags)
 TPM_RESULT TPMLIB_SetState(enum TPMLIB_StateType st,
                            const unsigned char *buffer, uint32_t buflen)
 {
-    return tpm_iface[0]->SetState(st, buffer, buflen);
+    return tpm_iface[tpmvers_choice]->SetState(st, buffer, buflen);
 }
 
 TPM_RESULT TPMLIB_GetState(enum TPMLIB_StateType st,
                            unsigned char **buffer, uint32_t *buflen)
 {
-    return tpm_iface[0]->GetState(st, buffer, buflen);
+    return tpm_iface[tpmvers_choice]->GetState(st, buffer, buflen);
 }
 
 TPM_RESULT TPM_IO_Hash_Start(void)
@@ -538,6 +547,33 @@ void TPMLIB_LogPrintfA(unsigned int indent, const char *format, ...)
     va_end(args);
 }
 
+/*
+ * TPMLIB_LogArray: Display an array of data
+ *
+ * @indent: how many spaces to indent; indent of ~0 forces logging
+ *          with indent 0 even if not debug_level is set
+ * @data: the data to print
+ * @datalen: length of the data
+ */
+void TPMLIB_LogArray(unsigned int indent, const unsigned char *data,
+                     size_t datalen)
+{
+    char line[80];
+    size_t i, o = 0;
+
+    for (i = 0; i < datalen; i++) {
+        snprintf(&line[o], sizeof(line) - o, "%02x ", data[i]);
+        o += 3;
+        if (o >= 16 * 3) {
+            TPMLIB_LogPrintfA(indent, "%s\n", line);
+            o = 0;
+        }
+    }
+    if (o > 0) {
+        TPMLIB_LogPrintfA(indent, "%s\n", line);
+    }
+}
+
 void ClearCachedState(enum TPMLIB_StateType st)
 {
     free(cached_blobs[st].buffer);