cryptfs: only include keymaster 2 if tree supports it

This fixes missing <hardware/keymaster2.h> header error
in the 6.0 build tree

Change-Id: I9cad67f0d36f215a033bf131c410620e7cdfdb2d
diff --git a/crypto/fde/cryptfs.cpp b/crypto/fde/cryptfs.cpp
index eeefc29..544d3ec 100644
--- a/crypto/fde/cryptfs.cpp
+++ b/crypto/fde/cryptfs.cpp
@@ -60,6 +60,9 @@
 //#include "f2fs_sparseblock.h"
 //#include "EncryptInplace.h"
 //#include "Process.h"
+#if TW_KEYMASTER_MAX_API > 1
+#include <hardware/keymaster2.h>
+#endif
 #if TW_KEYMASTER_MAX_API == 3
 #include "../ext4crypt/Keymaster3.h"
 #endif
@@ -74,7 +77,6 @@
 #include <openssl/sha.h>
 #include <hardware/keymaster0.h>
 #include <hardware/keymaster1.h>
-#include <hardware/keymaster2.h>
 #endif
 //#include "android-base/properties.h"
 //#include <bootloader_message/bootloader_message.h>
@@ -150,7 +152,7 @@
   }
 }
 
-#if TW_KEYMASTER_MAX_API == 0
+#if TW_KEYMASTER_MAX_API == 0 //TW_KEYMASTER_MAX_API
 static int keymaster_init(keymaster_device_t **keymaster_dev)
 {
     int rc;
@@ -175,7 +177,7 @@
     *keymaster_dev = NULL;
     return rc;
 }
-#else //TW_KEYMASTER_MAX_API == 0
+#elif TW_KEYMASTER_MAX_API > 1
 static int keymaster_init(keymaster0_device_t **keymaster0_dev,
                           keymaster1_device_t **keymaster1_dev,
 			  keymaster2_device_t **keymaster2_dev)
@@ -220,7 +222,46 @@
     *keymaster2_dev = NULL;
     return rc;
 }
-#endif //TW_KEYMASTER_MAX_API == 0
+#else
+static int keymaster_init(keymaster0_device_t **keymaster0_dev,
+                          keymaster1_device_t **keymaster1_dev)
+{
+    int rc;
+
+    const hw_module_t* mod;
+    rc = hw_get_module_by_class(KEYSTORE_HARDWARE_MODULE_ID, NULL, &mod);
+    if (rc) {
+        printf("could not find any keystore module\n");
+        goto err;
+    }
+
+    printf("keymaster module name is %s\n", mod->name);
+    printf("keymaster version is %d\n", mod->module_api_version);
+
+    *keymaster0_dev = NULL;
+    *keymaster1_dev = NULL;
+    if (mod->module_api_version == KEYMASTER_MODULE_API_VERSION_1_0) {
+        printf("Found keymaster1 module, using keymaster1 API.\n");
+        rc = keymaster1_open(mod, keymaster1_dev);
+    } else {
+        printf("Found keymaster0 module, using keymaster0 API.\n");
+        rc = keymaster0_open(mod, keymaster0_dev);
+    }
+
+    if (rc) {
+        printf("could not open keymaster device in %s (%s)\n",
+              KEYSTORE_HARDWARE_MODULE_ID, strerror(-rc));
+        goto err;
+    }
+
+    return 0;
+
+err:
+    *keymaster0_dev = NULL;
+    *keymaster1_dev = NULL;
+    return rc;
+}
+#endif //TW_KEYMASTER_MAX_API
 
 #ifdef CONFIG_HW_DISK_ENCRYPTION
 static int scrypt_keymaster(const char *passwd, const unsigned char *salt,
@@ -431,9 +472,13 @@
 #if TW_KEYMASTER_MAX_API >= 1
     keymaster0_device_t *keymaster0_dev = 0;
     keymaster1_device_t *keymaster1_dev = 0;
+#if TW_KEYMASTER_MAX_API > 1
     keymaster2_device_t *keymaster2_dev = 0;
     if (keymaster_init(&keymaster0_dev, &keymaster1_dev, &keymaster2_dev)) {
 #else
+    if (keymaster_init(&keymaster0_dev, &keymaster1_dev)) {
+#endif
+#else
     keymaster_device_t *keymaster0_dev = 0;
     if (keymaster_init(&keymaster0_dev)) {
 #endif
@@ -512,6 +557,7 @@
         *signature_size = tmp_sig.data_length;
         rc = 0;
     }
+#if TW_KEYMASTER_MAX_API > 1
     else if (keymaster2_dev) {
         keymaster_key_blob_t key = { ftr->keymaster_blob, ftr->keymaster_blob_size };
         keymaster_key_param_t params[] = {
@@ -588,6 +634,7 @@
         *signature_size = tmp_sig.data_length;
         rc = 0;
     }
+#endif // TW_KEYMASTER_API > 1
 #endif // TW_KEYMASTER_API >= 1
 
     out: