Improve handling of undecrypted encrypted /data

- Until the encrypted partition is decrypted, treat it as a disk image
  instead of a filesystem so that it is properly backed up using dd.
- Make Partition::Wipe() use the filesystem from fstab for disk images
  so that wiping an undecrypted /data makes it a plain unencrypted
  partition again.

Change-Id: I78db977d6348cfa66b0cfbc492fb5776f84db48d
diff --git a/partition.cpp b/partition.cpp
index 145fb14..f5173b1 100644
--- a/partition.cpp
+++ b/partition.cpp
@@ -214,9 +214,17 @@
 			} else if (!Mount(false)) {
 				Is_Encrypted = true;
 				Is_Decrypted = false;
+				Can_Be_Mounted = false;
+				Current_File_System = "emmc";
+				Setup_Image(Display_Error);
 				DataManager::SetValue(TW_IS_ENCRYPTED, 1);
 				DataManager::SetValue(TW_CRYPTO_PASSWORD, "");
 				DataManager::SetValue("tw_crypto_display", "");
+			} else {
+				// Filesystem is not encrypted and the mount
+				// succeeded, so get it back to the original
+				// unmounted state
+				UnMount(false);
 			}
 	#ifdef RECOVERY_SDCARD_ON_DATA
 			if (!Is_Encrypted || (Is_Encrypted && Is_Decrypted))
@@ -414,12 +422,12 @@
 void TWPartition::Setup_Image(bool Display_Error) {
 	Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
 	Backup_Name = Display_Name;
-	if (Fstab_File_System == "emmc")
+	if (Current_File_System == "emmc")
 		Backup_Method = DD;
-	else if (Fstab_File_System == "mtd" || Fstab_File_System == "bml")
+	else if (Current_File_System == "mtd" || Current_File_System == "bml")
 		Backup_Method = FLASH_UTILS;
 	else
-		LOGI("Unhandled file system '%s' on image '%s'\n", Fstab_File_System.c_str(), Display_Name.c_str());
+		LOGI("Unhandled file system '%s' on image '%s'\n", Current_File_System.c_str(), Display_Name.c_str());
 	if (Find_Partition_Size()) {
 		Used = Size;
 		Backup_Size = Size;
@@ -768,7 +776,10 @@
 }
 
 bool TWPartition::Wipe() {
-	return Wipe(Current_File_System);
+	if (Is_File_System(Current_File_System))
+		return Wipe(Current_File_System);
+	else
+		return Wipe(Fstab_File_System);
 }
 
 bool TWPartition::Wipe_AndSec(void) {
diff --git a/partitionmanager.cpp b/partitionmanager.cpp
index 536e867..95f0100 100644
--- a/partitionmanager.cpp
+++ b/partitionmanager.cpp
@@ -1483,6 +1483,8 @@
 					DataManager::SetValue(TW_BACKUP_RECOVERY_VAR, 0);
 				} else
 					DataManager::SetValue(TW_HAS_RECOVERY_PARTITION, 1);
+			} else if ((*iter)->Mount_Point == "/data") {
+				data_size += (int)((*iter)->Backup_Size / 1048576LLU);
 			}
 #ifdef SP1_NAME
 			if ((*iter)->Backup_Name == EXPAND(SP1_NAME)) {
@@ -1581,6 +1583,7 @@
 			DataManager::SetValue(TW_IS_DECRYPTED, 1);
 			dat->Is_Decrypted = true;
 			dat->Decrypted_Block_Device = crypto_blkdev;
+			dat->Setup_File_System(false);
 			ui_print("Data successfully decrypted, new block device: '%s'\n", crypto_blkdev);
 			// Sleep for a bit so that the device will be ready
 			sleep(1);