decryption: solve specific decryption problems on some devices

Change-Id: I832c16da3d45888299fd9f96888ed8a85e5d48c9
diff --git a/Android.mk b/Android.mk
index fad1878..eb7f2ee 100755
--- a/Android.mk
+++ b/Android.mk
@@ -183,6 +183,10 @@
     LOCAL_CFLAGS += -DTW_NO_FLASH_CURRENT_TWRP
 endif
 
+ifeq ($(TW_PREPARE_DATA_MEDIA_EARLY),true)
+    LOCAL_CFLAGS += -DTW_PREPARE_DATA_MEDIA_EARLY
+endif
+
 LOCAL_MODULE_PATH := $(TARGET_RECOVERY_ROOT_OUT)/system/bin
 
 ifeq ($(TARGET_RECOVERY_TWRP_LIB),)
diff --git a/partition.cpp b/partition.cpp
index 3bb1bd1..6814a4b 100755
--- a/partition.cpp
+++ b/partition.cpp
@@ -666,6 +666,10 @@
 	UnMount(false);
 
 #ifdef TW_INCLUDE_CRYPTO
+	#ifdef TW_PREPARE_DATA_MEDIA_EARLY
+	if (datamedia)
+		Setup_Data_Media();
+	#endif
 	Can_Be_Encrypted = true;
 	char crypto_blkdev[255];
 	property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
@@ -673,8 +677,10 @@
 		Set_FBE_Status();
 		Decrypted_Block_Device = crypto_blkdev;
 		LOGINFO("Data already decrypted, new block device: '%s'\n", crypto_blkdev);
+		#ifndef TW_PREPARE_DATA_MEDIA_EARLY
 		if (datamedia)
 			Setup_Data_Media();
+		#endif
 		DataManager::SetValue(TW_IS_ENCRYPTED, 0);
 	} else if (!Mount(false)) {
 		if (Is_Present) {
@@ -716,9 +722,10 @@
 				LOGERR("Unable to decrypt FBE device\n");
 		} else {
 			DataManager::SetValue(TW_IS_ENCRYPTED, 0);
+			#ifndef TW_PREPARE_DATA_MEDIA_EARLY
 			if (datamedia)
 				Setup_Data_Media();
-
+			#endif
 		}
 	}
 	if (datamedia && (!Is_Encrypted || (Is_Encrypted && Is_Decrypted))) {
@@ -1231,8 +1238,12 @@
 			Make_Dir("/sdcard", false);
 			Symlink_Mount_Point = "/sdcard";
 		}
+		#ifdef TW_PREPARE_DATA_MEDIA_EARLY
+		if (Mount(false) && TWFunc::Path_Exists(Mount_Point + "/media/0")) {
+		#else
 		Mount(false);
 		if (TWFunc::Path_Exists(Mount_Point + "/media/0")) {
+		#endif
 			Storage_Path = Mount_Point + "/media/0";
 			Symlink_Path = Storage_Path;
 			DataManager::SetValue(TW_INTERNAL_PATH, Mount_Point + "/media/0");