From b328bb108cc926274ca20cc722d8d8ad91f19d60 Mon Sep 17 00:00:00 2001
From: Lans Zhang <jia.zhang@windriver.com>
Date: Thu, 20 Jul 2017 15:19:51 +0800
Subject: [PATCH] tpm2-tools: use dynamic linkage with tpm2-abrmd

tpm2-abrmd has huge dependencies and they are not necessary to be involved
in initramfs.

Signed-off-by: Lans Zhang <jia.zhang@windriver.com>
---
 lib/context-util.c | 21 +++++++++++++++++++--
 1 file changed, 19 insertions(+), 2 deletions(-)

diff --git a/lib/context-util.c b/lib/context-util.c
index 7de22ac..9814548 100644
--- a/lib/context-util.c
+++ b/lib/context-util.c
@@ -38,6 +38,7 @@
 #include <tcti/tcti_socket.h>
 #endif
 #ifdef HAVE_TCTI_TABRMD
+#include <dlfcn.h>
 #include <tcti/tcti-tabrmd.h>
 #endif
 
@@ -134,10 +135,26 @@ TSS2_TCTI_CONTEXT*
 tcti_tabrmd_init (void)
 {
     TSS2_TCTI_CONTEXT *tcti_ctx;
+    TSS2_RC (*init)(TSS2_TCTI_CONTEXT *, size_t *);
+    /*
+     * Intend to "forget" the handle in order to make sure libtcti-tabrmd
+     * is unloaded along with the deconstructed functions.
+     */
+    void *tabrmd_handle;
     TSS2_RC rc;
     size_t size;
 
-    rc = tss2_tcti_tabrmd_init(NULL, &size);
+    tabrmd_handle = dlopen("libtcti-tabrmd.so.0", RTLD_LAZY);
+    if (!tabrmd_handle) {
+       LOG_ERR ("Unable to find out the tabrmd tcti library");
+       return NULL;
+    }
+
+    init = dlsym(tabrmd_handle, "tss2_tcti_tabrmd_init");
+    if (!init)
+        return NULL;
+
+    rc = init(NULL, &size);
     if (rc != TSS2_RC_SUCCESS) {
         LOG_ERR ("Failed to get size for TABRMD TCTI context: 0x%" PRIx32, rc);
         return NULL;
@@ -148,7 +165,7 @@ tcti_tabrmd_init (void)
                  strerror (errno));
         return NULL;
     }
-    rc = tss2_tcti_tabrmd_init (tcti_ctx, &size);
+    rc = init (tcti_ctx, &size);
     if (rc != TSS2_RC_SUCCESS) {
         LOG_ERR ("Failed to initialize TABRMD TCTI context: 0x%" PRIx32, rc);
         free (tcti_ctx);
-- 
2.7.5

