Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1 | /* Partition class for TWRP |
| 2 | * |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License version 2 and |
| 5 | * only version 2 as published by the Free Software Foundation. |
| 6 | * |
| 7 | * This program is distributed in the hope that it will be useful, |
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | * GNU General Public License for more details. |
| 11 | * |
| 12 | * You should have received a copy of the GNU General Public License |
| 13 | * along with this program; if not, write to the Free Software |
| 14 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 15 | * 02110-1301, USA. |
| 16 | * |
| 17 | * The code was written from scratch by Dees_Troy dees_troy at |
| 18 | * yahoo |
| 19 | * |
| 20 | * Copyright (c) 2012 |
| 21 | */ |
| 22 | |
| 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
| 26 | #include <sys/stat.h> |
| 27 | #include <sys/vfs.h> |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 28 | #include <sys/mount.h> |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 29 | #include <unistd.h> |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 30 | #include <dirent.h> |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 31 | #include <iostream> |
| 32 | #include <sstream> |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 33 | |
Dees_Troy | 657c309 | 2012-09-10 20:32:10 -0400 | [diff] [blame] | 34 | #ifdef TW_INCLUDE_CRYPTO |
| 35 | #include "cutils/properties.h" |
| 36 | #endif |
| 37 | |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 38 | #include "libblkid/blkid.h" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 39 | #include "variables.h" |
| 40 | #include "common.h" |
| 41 | #include "partitions.hpp" |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 42 | #include "data.hpp" |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 43 | #include "twrp-functions.hpp" |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 44 | #include "twrpDigest.hpp" |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 45 | #include "twrpTar.hpp" |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 46 | extern "C" { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 47 | #include "mtdutils/mtdutils.h" |
| 48 | #include "mtdutils/mounts.h" |
Dees_Troy | 85f44ed | 2013-01-09 18:42:36 +0000 | [diff] [blame] | 49 | #ifdef TW_INCLUDE_CRYPTO_SAMSUNG |
| 50 | #include "crypto/libcrypt_samsung/include/libcrypt_samsung.h" |
| 51 | #endif |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 52 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 53 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 54 | using namespace std; |
| 55 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 56 | TWPartition::TWPartition(void) { |
| 57 | Can_Be_Mounted = false; |
| 58 | Can_Be_Wiped = false; |
| 59 | Wipe_During_Factory_Reset = false; |
| 60 | Wipe_Available_in_GUI = false; |
| 61 | Is_SubPartition = false; |
Dees_Troy | 2691f9d | 2012-09-24 11:15:49 -0400 | [diff] [blame] | 62 | Has_SubPartition = false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 63 | SubPartition_Of = ""; |
| 64 | Symlink_Path = ""; |
| 65 | Symlink_Mount_Point = ""; |
| 66 | Mount_Point = ""; |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 67 | Backup_Path = ""; |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 68 | Actual_Block_Device = ""; |
| 69 | Primary_Block_Device = ""; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 70 | Alternate_Block_Device = ""; |
| 71 | Removable = false; |
| 72 | Is_Present = false; |
| 73 | Length = 0; |
| 74 | Size = 0; |
| 75 | Used = 0; |
| 76 | Free = 0; |
| 77 | Backup_Size = 0; |
| 78 | Can_Be_Encrypted = false; |
| 79 | Is_Encrypted = false; |
| 80 | Is_Decrypted = false; |
| 81 | Decrypted_Block_Device = ""; |
| 82 | Display_Name = ""; |
| 83 | Backup_Name = ""; |
Dees_Troy | 63c8df7 | 2012-09-10 14:02:05 -0400 | [diff] [blame] | 84 | Backup_FileName = ""; |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 85 | MTD_Name = ""; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 86 | Backup_Method = NONE; |
| 87 | Has_Data_Media = false; |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 88 | Has_Android_Secure = false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 89 | Is_Storage = false; |
| 90 | Storage_Path = ""; |
| 91 | Current_File_System = ""; |
| 92 | Fstab_File_System = ""; |
| 93 | Format_Block_Size = 0; |
Dees_Troy | 68cab49 | 2012-12-12 19:29:35 +0000 | [diff] [blame] | 94 | Ignore_Blkid = false; |
Dees_Troy | 16c2b31 | 2013-01-15 16:51:18 +0000 | [diff] [blame] | 95 | Retain_Layout_Version = false; |
Dees_Troy | 85f44ed | 2013-01-09 18:42:36 +0000 | [diff] [blame] | 96 | #ifdef TW_INCLUDE_CRYPTO_SAMSUNG |
| 97 | EcryptFS_Password = ""; |
| 98 | #endif |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | TWPartition::~TWPartition(void) { |
| 102 | // Do nothing |
| 103 | } |
| 104 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 105 | bool TWPartition::Process_Fstab_Line(string Line, bool Display_Error) { |
| 106 | char full_line[MAX_FSTAB_LINE_LENGTH], item[MAX_FSTAB_LINE_LENGTH]; |
| 107 | int line_len = Line.size(), index = 0, item_index = 0; |
| 108 | char* ptr; |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 109 | string Flags; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 110 | strncpy(full_line, Line.c_str(), line_len); |
| 111 | |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 112 | for (index = 0; index < line_len; index++) { |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 113 | if (full_line[index] <= 32) |
| 114 | full_line[index] = '\0'; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 115 | } |
Dees_Troy | 7c2dec8 | 2012-09-26 09:49:14 -0400 | [diff] [blame] | 116 | Mount_Point = full_line; |
| 117 | LOGI("Processing '%s'\n", Mount_Point.c_str()); |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 118 | Backup_Path = Mount_Point; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 119 | index = Mount_Point.size(); |
| 120 | while (index < line_len) { |
| 121 | while (index < line_len && full_line[index] == '\0') |
| 122 | index++; |
| 123 | if (index >= line_len) |
| 124 | continue; |
| 125 | ptr = full_line + index; |
| 126 | if (item_index == 0) { |
| 127 | // File System |
| 128 | Fstab_File_System = ptr; |
| 129 | Current_File_System = ptr; |
| 130 | item_index++; |
| 131 | } else if (item_index == 1) { |
| 132 | // Primary Block Device |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 133 | if (Fstab_File_System == "mtd" || Fstab_File_System == "yaffs2") { |
Dees_Troy | 094207a | 2012-09-26 12:00:39 -0400 | [diff] [blame] | 134 | MTD_Name = ptr; |
| 135 | Find_MTD_Block_Device(MTD_Name); |
Dees_Troy | 5fcd8f9 | 2012-10-16 12:22:05 -0400 | [diff] [blame] | 136 | } else if (Fstab_File_System == "bml") { |
| 137 | if (Mount_Point == "/boot") |
| 138 | MTD_Name = "boot"; |
| 139 | else if (Mount_Point == "/recovery") |
| 140 | MTD_Name = "recovery"; |
| 141 | Primary_Block_Device = ptr; |
| 142 | if (*ptr != '/') |
| 143 | LOGE("Until we get better BML support, you will have to find and provide the full block device path to the BML devices e.g. /dev/block/bml9 instead of the partition name\n"); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 144 | } else if (*ptr != '/') { |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 145 | if (Display_Error) |
| 146 | LOGE("Invalid block device on '%s', '%s', %i\n", Line.c_str(), ptr, index); |
| 147 | else |
| 148 | LOGI("Invalid block device on '%s', '%s', %i\n", Line.c_str(), ptr, index); |
| 149 | return 0; |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 150 | } else { |
| 151 | Primary_Block_Device = ptr; |
| 152 | Find_Real_Block_Device(Primary_Block_Device, Display_Error); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 153 | } |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 154 | item_index++; |
| 155 | } else if (item_index > 1) { |
| 156 | if (*ptr == '/') { |
| 157 | // Alternate Block Device |
| 158 | Alternate_Block_Device = ptr; |
| 159 | Find_Real_Block_Device(Alternate_Block_Device, Display_Error); |
| 160 | } else if (strlen(ptr) > 7 && strncmp(ptr, "length=", 7) == 0) { |
| 161 | // Partition length |
| 162 | ptr += 7; |
| 163 | Length = atoi(ptr); |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 164 | } else if (strlen(ptr) > 6 && strncmp(ptr, "flags=", 6) == 0) { |
| 165 | // Custom flags, save for later so that new values aren't overwritten by defaults |
| 166 | ptr += 6; |
| 167 | Flags = ptr; |
Dees_Troy | 68cab49 | 2012-12-12 19:29:35 +0000 | [diff] [blame] | 168 | Process_Flags(Flags, Display_Error); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 169 | } else if (strlen(ptr) == 4 && (strncmp(ptr, "NULL", 4) == 0 || strncmp(ptr, "null", 4) == 0 || strncmp(ptr, "null", 4) == 0)) { |
| 170 | // Do nothing |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 171 | } else { |
| 172 | // Unhandled data |
Dees_Troy | ab10ee2 | 2012-09-21 14:27:30 -0400 | [diff] [blame] | 173 | LOGI("Unhandled fstab information: '%s', %i, line: '%s'\n", ptr, index, Line.c_str()); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 174 | } |
| 175 | } |
| 176 | while (index < line_len && full_line[index] != '\0') |
| 177 | index++; |
| 178 | } |
| 179 | |
| 180 | if (!Is_File_System(Fstab_File_System) && !Is_Image(Fstab_File_System)) { |
| 181 | if (Display_Error) |
| 182 | LOGE("Unknown File System: '%s'\n", Fstab_File_System.c_str()); |
| 183 | else |
| 184 | LOGI("Unknown File System: '%s'\n", Fstab_File_System.c_str()); |
| 185 | return 0; |
| 186 | } else if (Is_File_System(Fstab_File_System)) { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 187 | Find_Actual_Block_Device(); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 188 | Setup_File_System(Display_Error); |
| 189 | if (Mount_Point == "/system") { |
| 190 | Display_Name = "System"; |
| 191 | Wipe_Available_in_GUI = true; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 192 | } else if (Mount_Point == "/data") { |
| 193 | Display_Name = "Data"; |
| 194 | Wipe_Available_in_GUI = true; |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 195 | Wipe_During_Factory_Reset = true; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 196 | #ifdef RECOVERY_SDCARD_ON_DATA |
| 197 | Has_Data_Media = true; |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 198 | Is_Storage = true; |
| 199 | Storage_Path = "/data/media"; |
Dees_Troy | 16b7435 | 2012-11-14 22:27:31 +0000 | [diff] [blame] | 200 | Symlink_Path = Storage_Path; |
Dees_Troy | 657c309 | 2012-09-10 20:32:10 -0400 | [diff] [blame] | 201 | if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) { |
| 202 | Make_Dir("/emmc", Display_Error); |
Dees_Troy | 657c309 | 2012-09-10 20:32:10 -0400 | [diff] [blame] | 203 | Symlink_Mount_Point = "/emmc"; |
| 204 | } else { |
| 205 | Make_Dir("/sdcard", Display_Error); |
Dees_Troy | 657c309 | 2012-09-10 20:32:10 -0400 | [diff] [blame] | 206 | Symlink_Mount_Point = "/sdcard"; |
| 207 | } |
Dees_Troy | 16b7435 | 2012-11-14 22:27:31 +0000 | [diff] [blame] | 208 | if (Mount(false) && TWFunc::Path_Exists("/data/media/0")) { |
| 209 | Storage_Path = "/data/media/0"; |
| 210 | Symlink_Path = Storage_Path; |
| 211 | DataManager::SetValue(TW_INTERNAL_PATH, "/data/media/0"); |
| 212 | UnMount(true); |
| 213 | } |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 214 | #endif |
| 215 | #ifdef TW_INCLUDE_CRYPTO |
| 216 | Can_Be_Encrypted = true; |
Dees_Troy | 657c309 | 2012-09-10 20:32:10 -0400 | [diff] [blame] | 217 | char crypto_blkdev[255]; |
| 218 | property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error"); |
| 219 | if (strcmp(crypto_blkdev, "error") != 0) { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 220 | DataManager::SetValue(TW_DATA_BLK_DEVICE, Primary_Block_Device); |
Dees_Troy | 657c309 | 2012-09-10 20:32:10 -0400 | [diff] [blame] | 221 | DataManager::SetValue(TW_IS_DECRYPTED, 1); |
| 222 | Is_Encrypted = true; |
| 223 | Is_Decrypted = true; |
| 224 | Decrypted_Block_Device = crypto_blkdev; |
| 225 | LOGI("Data already decrypted, new block device: '%s'\n", crypto_blkdev); |
| 226 | } else if (!Mount(false)) { |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 227 | Is_Encrypted = true; |
| 228 | Is_Decrypted = false; |
Gary Peck | 82599a8 | 2012-11-21 16:23:12 -0800 | [diff] [blame] | 229 | Can_Be_Mounted = false; |
| 230 | Current_File_System = "emmc"; |
| 231 | Setup_Image(Display_Error); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 232 | DataManager::SetValue(TW_IS_ENCRYPTED, 1); |
| 233 | DataManager::SetValue(TW_CRYPTO_PASSWORD, ""); |
| 234 | DataManager::SetValue("tw_crypto_display", ""); |
Gary Peck | 82599a8 | 2012-11-21 16:23:12 -0800 | [diff] [blame] | 235 | } else { |
| 236 | // Filesystem is not encrypted and the mount |
| 237 | // succeeded, so get it back to the original |
| 238 | // unmounted state |
| 239 | UnMount(false); |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 240 | } |
Dees_Troy | 9b21af7 | 2012-10-01 15:51:46 -0400 | [diff] [blame] | 241 | #ifdef RECOVERY_SDCARD_ON_DATA |
| 242 | if (!Is_Encrypted || (Is_Encrypted && Is_Decrypted)) |
| 243 | Recreate_Media_Folder(); |
| 244 | #endif |
| 245 | #else |
| 246 | #ifdef RECOVERY_SDCARD_ON_DATA |
| 247 | Recreate_Media_Folder(); |
| 248 | #endif |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 249 | #endif |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 250 | } else if (Mount_Point == "/cache") { |
| 251 | Display_Name = "Cache"; |
| 252 | Wipe_Available_in_GUI = true; |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 253 | Wipe_During_Factory_Reset = true; |
Dees_Troy | ce2fe77 | 2012-09-28 12:34:33 -0400 | [diff] [blame] | 254 | if (Mount(false) && !TWFunc::Path_Exists("/cache/recovery/.")) { |
Dees_Troy | b46a684 | 2012-09-25 11:06:46 -0400 | [diff] [blame] | 255 | LOGI("Recreating /cache/recovery folder.\n"); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 256 | if (mkdir("/cache/recovery", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0) |
| 257 | return -1; |
Dees_Troy | b46a684 | 2012-09-25 11:06:46 -0400 | [diff] [blame] | 258 | } |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 259 | } else if (Mount_Point == "/datadata") { |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 260 | Wipe_During_Factory_Reset = true; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 261 | Display_Name = "DataData"; |
| 262 | Is_SubPartition = true; |
| 263 | SubPartition_Of = "/data"; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 264 | DataManager::SetValue(TW_HAS_DATADATA, 1); |
| 265 | } else if (Mount_Point == "/sd-ext") { |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 266 | Wipe_During_Factory_Reset = true; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 267 | Display_Name = "SD-Ext"; |
| 268 | Wipe_Available_in_GUI = true; |
Dees_Troy | c51f1f9 | 2012-09-20 15:32:13 -0400 | [diff] [blame] | 269 | Removable = true; |
Dees_Troy | 2c50e18 | 2012-09-26 20:05:28 -0400 | [diff] [blame] | 270 | } else if (Mount_Point == "/boot") { |
| 271 | Display_Name = "Boot"; |
| 272 | DataManager::SetValue("tw_boot_is_mountable", 1); |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 273 | } |
| 274 | #ifdef TW_EXTERNAL_STORAGE_PATH |
| 275 | if (Mount_Point == EXPAND(TW_EXTERNAL_STORAGE_PATH)) { |
| 276 | Is_Storage = true; |
| 277 | Storage_Path = EXPAND(TW_EXTERNAL_STORAGE_PATH); |
Dees_Troy | c51f1f9 | 2012-09-20 15:32:13 -0400 | [diff] [blame] | 278 | Removable = true; |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 279 | #else |
| 280 | if (Mount_Point == "/sdcard") { |
| 281 | Is_Storage = true; |
| 282 | Storage_Path = "/sdcard"; |
Dees_Troy | c51f1f9 | 2012-09-20 15:32:13 -0400 | [diff] [blame] | 283 | Removable = true; |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 284 | #ifndef RECOVERY_SDCARD_ON_DATA |
| 285 | Setup_AndSec(); |
Dees_Troy | 8e337f3 | 2012-10-13 22:07:49 -0400 | [diff] [blame] | 286 | Mount_Storage_Retry(); |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 287 | #endif |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 288 | #endif |
Dees_Troy | b05ddee | 2013-01-28 20:24:50 +0000 | [diff] [blame] | 289 | } |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 290 | #ifdef TW_INTERNAL_STORAGE_PATH |
| 291 | if (Mount_Point == EXPAND(TW_INTERNAL_STORAGE_PATH)) { |
| 292 | Is_Storage = true; |
| 293 | Storage_Path = EXPAND(TW_INTERNAL_STORAGE_PATH); |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 294 | #ifndef RECOVERY_SDCARD_ON_DATA |
| 295 | Setup_AndSec(); |
Dees_Troy | 8e337f3 | 2012-10-13 22:07:49 -0400 | [diff] [blame] | 296 | Mount_Storage_Retry(); |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 297 | #endif |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 298 | } |
| 299 | #else |
| 300 | if (Mount_Point == "/emmc") { |
| 301 | Is_Storage = true; |
| 302 | Storage_Path = "/emmc"; |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 303 | #ifndef RECOVERY_SDCARD_ON_DATA |
| 304 | Setup_AndSec(); |
Dees_Troy | 8e337f3 | 2012-10-13 22:07:49 -0400 | [diff] [blame] | 305 | Mount_Storage_Retry(); |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 306 | #endif |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 307 | } |
| 308 | #endif |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 309 | } else if (Is_Image(Fstab_File_System)) { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 310 | Find_Actual_Block_Device(); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 311 | Setup_Image(Display_Error); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 312 | } |
| 313 | |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 314 | // Process any custom flags |
| 315 | if (Flags.size() > 0) |
| 316 | Process_Flags(Flags, Display_Error); |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 317 | return true; |
| 318 | } |
| 319 | |
| 320 | bool TWPartition::Process_Flags(string Flags, bool Display_Error) { |
| 321 | char flags[MAX_FSTAB_LINE_LENGTH]; |
| 322 | int flags_len, index = 0; |
| 323 | char* ptr; |
| 324 | |
| 325 | strcpy(flags, Flags.c_str()); |
| 326 | flags_len = Flags.size(); |
| 327 | for (index = 0; index < flags_len; index++) { |
| 328 | if (flags[index] == ';') |
| 329 | flags[index] = '\0'; |
| 330 | } |
| 331 | |
| 332 | index = 0; |
| 333 | while (index < flags_len) { |
| 334 | while (index < flags_len && flags[index] == '\0') |
| 335 | index++; |
| 336 | if (index >= flags_len) |
| 337 | continue; |
| 338 | ptr = flags + index; |
| 339 | if (strcmp(ptr, "removable") == 0) { |
| 340 | Removable = true; |
| 341 | } else if (strcmp(ptr, "storage") == 0) { |
| 342 | Is_Storage = true; |
Dees_Troy | 63c8df7 | 2012-09-10 14:02:05 -0400 | [diff] [blame] | 343 | } else if (strcmp(ptr, "canbewiped") == 0) { |
| 344 | Can_Be_Wiped = true; |
| 345 | } else if (strcmp(ptr, "wipeingui") == 0) { |
| 346 | Can_Be_Wiped = true; |
| 347 | Wipe_Available_in_GUI = true; |
| 348 | } else if (strcmp(ptr, "wipeduringfactoryreset") == 0) { |
| 349 | Can_Be_Wiped = true; |
| 350 | Wipe_Available_in_GUI = true; |
| 351 | Wipe_During_Factory_Reset = true; |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 352 | } else if (strlen(ptr) > 15 && strncmp(ptr, "subpartitionof=", 15) == 0) { |
Dees_Troy | 2c4c26f | 2013-01-28 15:26:43 +0000 | [diff] [blame] | 353 | ptr += 15; |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 354 | Is_SubPartition = true; |
| 355 | SubPartition_Of = ptr; |
Dees_Troy | 68cab49 | 2012-12-12 19:29:35 +0000 | [diff] [blame] | 356 | } else if (strcmp(ptr, "ignoreblkid") == 0) { |
| 357 | Ignore_Blkid = true; |
Dees_Troy | 16c2b31 | 2013-01-15 16:51:18 +0000 | [diff] [blame] | 358 | } else if (strcmp(ptr, "retainlayoutversion") == 0) { |
| 359 | Retain_Layout_Version = true; |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 360 | } else if (strlen(ptr) > 8 && strncmp(ptr, "symlink=", 8) == 0) { |
| 361 | ptr += 8; |
| 362 | Symlink_Path = ptr; |
| 363 | } else if (strlen(ptr) > 8 && strncmp(ptr, "display=", 8) == 0) { |
| 364 | ptr += 8; |
| 365 | Display_Name = ptr; |
| 366 | } else if (strlen(ptr) > 10 && strncmp(ptr, "blocksize=", 10) == 0) { |
| 367 | ptr += 10; |
| 368 | Format_Block_Size = atoi(ptr); |
| 369 | } else if (strlen(ptr) > 7 && strncmp(ptr, "length=", 7) == 0) { |
| 370 | ptr += 7; |
| 371 | Length = atoi(ptr); |
| 372 | } else { |
| 373 | if (Display_Error) |
| 374 | LOGE("Unhandled flag: '%s'\n", ptr); |
| 375 | else |
| 376 | LOGI("Unhandled flag: '%s'\n", ptr); |
| 377 | } |
| 378 | while (index < flags_len && flags[index] != '\0') |
| 379 | index++; |
| 380 | } |
| 381 | return true; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 382 | } |
| 383 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 384 | bool TWPartition::Is_File_System(string File_System) { |
| 385 | if (File_System == "ext2" || |
Dees_Troy | 63c8df7 | 2012-09-10 14:02:05 -0400 | [diff] [blame] | 386 | File_System == "ext3" || |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 387 | File_System == "ext4" || |
| 388 | File_System == "vfat" || |
| 389 | File_System == "ntfs" || |
| 390 | File_System == "yaffs2" || |
bigbiff bigbiff | 3e14652 | 2012-11-14 14:32:59 -0500 | [diff] [blame] | 391 | File_System == "exfat" || |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 392 | File_System == "auto") |
| 393 | return true; |
| 394 | else |
| 395 | return false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 396 | } |
| 397 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 398 | bool TWPartition::Is_Image(string File_System) { |
Dees_Troy | 5fcd8f9 | 2012-10-16 12:22:05 -0400 | [diff] [blame] | 399 | if (File_System == "emmc" || File_System == "mtd" || File_System == "bml") |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 400 | return true; |
| 401 | else |
| 402 | return false; |
| 403 | } |
| 404 | |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 405 | bool TWPartition::Make_Dir(string Path, bool Display_Error) { |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 406 | if (!TWFunc::Path_Exists(Path)) { |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 407 | if (mkdir(Path.c_str(), 0777) == -1) { |
| 408 | if (Display_Error) |
| 409 | LOGE("Can not create '%s' folder.\n", Path.c_str()); |
| 410 | else |
| 411 | LOGI("Can not create '%s' folder.\n", Path.c_str()); |
| 412 | return false; |
| 413 | } else { |
| 414 | LOGI("Created '%s' folder.\n", Path.c_str()); |
| 415 | return true; |
| 416 | } |
| 417 | } |
| 418 | return true; |
| 419 | } |
| 420 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 421 | void TWPartition::Setup_File_System(bool Display_Error) { |
| 422 | struct statfs st; |
| 423 | |
| 424 | Can_Be_Mounted = true; |
| 425 | Can_Be_Wiped = true; |
| 426 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 427 | // Make the mount point folder if it doesn't exist |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 428 | Make_Dir(Mount_Point, Display_Error); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 429 | Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1); |
| 430 | Backup_Name = Display_Name; |
| 431 | Backup_Method = FILES; |
| 432 | } |
| 433 | |
| 434 | void TWPartition::Setup_Image(bool Display_Error) { |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 435 | Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1); |
| 436 | Backup_Name = Display_Name; |
Gary Peck | 82599a8 | 2012-11-21 16:23:12 -0800 | [diff] [blame] | 437 | if (Current_File_System == "emmc") |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 438 | Backup_Method = DD; |
Gary Peck | 82599a8 | 2012-11-21 16:23:12 -0800 | [diff] [blame] | 439 | else if (Current_File_System == "mtd" || Current_File_System == "bml") |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 440 | Backup_Method = FLASH_UTILS; |
| 441 | else |
Gary Peck | 82599a8 | 2012-11-21 16:23:12 -0800 | [diff] [blame] | 442 | LOGI("Unhandled file system '%s' on image '%s'\n", Current_File_System.c_str(), Display_Name.c_str()); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 443 | if (Find_Partition_Size()) { |
| 444 | Used = Size; |
| 445 | Backup_Size = Size; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 446 | } else { |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 447 | if (Display_Error) |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 448 | LOGE("Unable to find parition size for '%s'\n", Mount_Point.c_str()); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 449 | else |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 450 | LOGI("Unable to find parition size for '%s'\n", Mount_Point.c_str()); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 451 | } |
| 452 | } |
| 453 | |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 454 | void TWPartition::Setup_AndSec(void) { |
| 455 | Backup_Name = "and-sec"; |
| 456 | Has_Android_Secure = true; |
| 457 | Symlink_Path = Mount_Point + "/.android_secure"; |
| 458 | Symlink_Mount_Point = "/and-sec"; |
| 459 | Backup_Path = Symlink_Mount_Point; |
| 460 | Make_Dir("/and-sec", true); |
| 461 | Recreate_AndSec_Folder(); |
| 462 | } |
| 463 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 464 | void TWPartition::Find_Real_Block_Device(string& Block, bool Display_Error) { |
| 465 | char device[512], realDevice[512]; |
| 466 | |
| 467 | strcpy(device, Block.c_str()); |
| 468 | memset(realDevice, 0, sizeof(realDevice)); |
| 469 | while (readlink(device, realDevice, sizeof(realDevice)) > 0) |
| 470 | { |
| 471 | strcpy(device, realDevice); |
| 472 | memset(realDevice, 0, sizeof(realDevice)); |
| 473 | } |
| 474 | |
| 475 | if (device[0] != '/') { |
| 476 | if (Display_Error) |
| 477 | LOGE("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str()); |
| 478 | else |
| 479 | LOGI("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str()); |
| 480 | return; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 481 | } else { |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 482 | Block = device; |
| 483 | return; |
| 484 | } |
| 485 | } |
| 486 | |
Dees_Troy | 8e337f3 | 2012-10-13 22:07:49 -0400 | [diff] [blame] | 487 | void TWPartition::Mount_Storage_Retry(void) { |
| 488 | // On some devices, storage doesn't want to mount right away, retry and sleep |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 489 | if (!Mount(true)) { |
Dees_Troy | 8e337f3 | 2012-10-13 22:07:49 -0400 | [diff] [blame] | 490 | int retry_count = 5; |
| 491 | while (retry_count > 0 && !Mount(false)) { |
| 492 | usleep(500000); |
| 493 | retry_count--; |
| 494 | } |
| 495 | Mount(true); |
| 496 | } |
| 497 | } |
| 498 | |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 499 | bool TWPartition::Find_MTD_Block_Device(string MTD_Name) { |
| 500 | FILE *fp = NULL; |
| 501 | char line[255]; |
| 502 | |
| 503 | fp = fopen("/proc/mtd", "rt"); |
| 504 | if (fp == NULL) { |
| 505 | LOGE("Device does not support /proc/mtd\n"); |
| 506 | return false; |
| 507 | } |
| 508 | |
| 509 | while (fgets(line, sizeof(line), fp) != NULL) |
| 510 | { |
| 511 | char device[32], label[32]; |
| 512 | unsigned long size = 0; |
| 513 | char* fstype = NULL; |
| 514 | int deviceId; |
| 515 | |
| 516 | sscanf(line, "%s %lx %*s %*c%s", device, &size, label); |
| 517 | |
| 518 | // Skip header and blank lines |
| 519 | if ((strcmp(device, "dev:") == 0) || (strlen(line) < 8)) |
| 520 | continue; |
| 521 | |
| 522 | // Strip off the trailing " from the label |
| 523 | label[strlen(label)-1] = '\0'; |
| 524 | |
| 525 | if (strcmp(label, MTD_Name.c_str()) == 0) { |
| 526 | // We found our device |
| 527 | // Strip off the trailing : from the device |
| 528 | device[strlen(device)-1] = '\0'; |
| 529 | if (sscanf(device,"mtd%d", &deviceId) == 1) { |
| 530 | sprintf(device, "/dev/block/mtdblock%d", deviceId); |
| 531 | Primary_Block_Device = device; |
| 532 | } |
| 533 | } |
| 534 | } |
| 535 | fclose(fp); |
| 536 | |
| 537 | return false; |
| 538 | } |
| 539 | |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 540 | bool TWPartition::Get_Size_Via_statfs(bool Display_Error) { |
| 541 | struct statfs st; |
| 542 | string Local_Path = Mount_Point + "/."; |
| 543 | |
| 544 | if (!Mount(Display_Error)) |
| 545 | return false; |
| 546 | |
| 547 | if (statfs(Local_Path.c_str(), &st) != 0) { |
| 548 | if (!Removable) { |
| 549 | if (Display_Error) |
| 550 | LOGE("Unable to statfs '%s'\n", Local_Path.c_str()); |
| 551 | else |
| 552 | LOGI("Unable to statfs '%s'\n", Local_Path.c_str()); |
| 553 | } |
| 554 | return false; |
| 555 | } |
| 556 | Size = (st.f_blocks * st.f_bsize); |
| 557 | Used = ((st.f_blocks - st.f_bfree) * st.f_bsize); |
| 558 | Free = (st.f_bfree * st.f_bsize); |
| 559 | Backup_Size = Used; |
| 560 | return true; |
| 561 | } |
| 562 | |
| 563 | bool TWPartition::Get_Size_Via_df(bool Display_Error) { |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 564 | FILE* fp; |
| 565 | char command[255], line[512]; |
| 566 | int include_block = 1; |
| 567 | unsigned int min_len; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 568 | string result; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 569 | |
| 570 | if (!Mount(Display_Error)) |
| 571 | return false; |
| 572 | |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 573 | min_len = Actual_Block_Device.size() + 2; |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 574 | sprintf(command, "df %s > /tmp/dfoutput.txt", Mount_Point.c_str()); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 575 | TWFunc::Exec_Cmd(command, result); |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 576 | fp = fopen("/tmp/dfoutput.txt", "rt"); |
| 577 | if (fp == NULL) { |
| 578 | LOGI("Unable to open /tmp/dfoutput.txt.\n"); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 579 | return false; |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 580 | } |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 581 | |
| 582 | while (fgets(line, sizeof(line), fp) != NULL) |
| 583 | { |
| 584 | unsigned long blocks, used, available; |
| 585 | char device[64]; |
| 586 | char tmpString[64]; |
| 587 | |
| 588 | if (strncmp(line, "Filesystem", 10) == 0) |
| 589 | continue; |
| 590 | if (strlen(line) < min_len) { |
| 591 | include_block = 0; |
| 592 | continue; |
| 593 | } |
| 594 | if (include_block) { |
| 595 | sscanf(line, "%s %lu %lu %lu", device, &blocks, &used, &available); |
| 596 | } else { |
| 597 | // The device block string is so long that the df information is on the next line |
| 598 | int space_count = 0; |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 599 | sprintf(tmpString, "/dev/block/%s", Actual_Block_Device.c_str()); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 600 | while (tmpString[space_count] == 32) |
| 601 | space_count++; |
| 602 | sscanf(line + space_count, "%lu %lu %lu", &blocks, &used, &available); |
| 603 | } |
| 604 | |
| 605 | // Adjust block size to byte size |
| 606 | Size = blocks * 1024ULL; |
| 607 | Used = used * 1024ULL; |
| 608 | Free = available * 1024ULL; |
| 609 | Backup_Size = Used; |
| 610 | } |
| 611 | fclose(fp); |
| 612 | return true; |
| 613 | } |
| 614 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 615 | bool TWPartition::Find_Partition_Size(void) { |
| 616 | FILE* fp; |
| 617 | char line[512]; |
| 618 | string tmpdevice; |
| 619 | |
igoriok | 87e3d93 | 2013-01-31 21:03:53 +0200 | [diff] [blame] | 620 | fp = fopen("/proc/dumchar_info", "rt"); |
| 621 | if (fp != NULL) { |
| 622 | while (fgets(line, sizeof(line), fp) != NULL) |
| 623 | { |
| 624 | char label[32], device[32]; |
| 625 | unsigned long size = 0; |
| 626 | |
| 627 | sscanf(line, "%s %lx %*lx %*lu %s", label, &size, device); |
| 628 | |
| 629 | // Skip header, annotation and blank lines |
| 630 | if ((strncmp(device, "/dev/", 5) != 0) || (strlen(line) < 8)) |
| 631 | continue; |
| 632 | |
| 633 | tmpdevice = "/dev/"; |
| 634 | tmpdevice += label; |
| 635 | if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) { |
| 636 | Size = size; |
| 637 | fclose(fp); |
| 638 | return true; |
| 639 | } |
| 640 | } |
| 641 | } |
| 642 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 643 | // In this case, we'll first get the partitions we care about (with labels) |
| 644 | fp = fopen("/proc/partitions", "rt"); |
| 645 | if (fp == NULL) |
| 646 | return false; |
| 647 | |
| 648 | while (fgets(line, sizeof(line), fp) != NULL) |
| 649 | { |
| 650 | unsigned long major, minor, blocks; |
| 651 | char device[512]; |
| 652 | char tmpString[64]; |
| 653 | |
Dees_Troy | 63c8df7 | 2012-09-10 14:02:05 -0400 | [diff] [blame] | 654 | if (strlen(line) < 7 || line[0] == 'm') continue; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 655 | sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device); |
| 656 | |
| 657 | tmpdevice = "/dev/block/"; |
| 658 | tmpdevice += device; |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 659 | if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) { |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 660 | // Adjust block size to byte size |
| 661 | Size = blocks * 1024ULL; |
| 662 | fclose(fp); |
| 663 | return true; |
| 664 | } |
| 665 | } |
| 666 | fclose(fp); |
| 667 | return false; |
| 668 | } |
| 669 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 670 | bool TWPartition::Is_Mounted(void) { |
| 671 | if (!Can_Be_Mounted) |
| 672 | return false; |
| 673 | |
| 674 | struct stat st1, st2; |
| 675 | string test_path; |
| 676 | |
| 677 | // Check to see if the mount point directory exists |
| 678 | test_path = Mount_Point + "/."; |
| 679 | if (stat(test_path.c_str(), &st1) != 0) return false; |
| 680 | |
| 681 | // Check to see if the directory above the mount point exists |
| 682 | test_path = Mount_Point + "/../."; |
| 683 | if (stat(test_path.c_str(), &st2) != 0) return false; |
| 684 | |
| 685 | // Compare the device IDs -- if they match then we're (probably) using tmpfs instead of an actual device |
| 686 | int ret = (st1.st_dev != st2.st_dev) ? true : false; |
| 687 | |
| 688 | return ret; |
| 689 | } |
| 690 | |
| 691 | bool TWPartition::Mount(bool Display_Error) { |
Dees_Troy | 3f5c4e8 | 2013-02-01 15:16:59 +0000 | [diff] [blame] | 692 | int exfat_mounted = 0; |
| 693 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 694 | if (Is_Mounted()) { |
| 695 | return true; |
| 696 | } else if (!Can_Be_Mounted) { |
| 697 | return false; |
| 698 | } |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 699 | |
| 700 | Find_Actual_Block_Device(); |
| 701 | |
| 702 | // Check the current file system before mounting |
| 703 | Check_FS_Type(); |
Dees_Troy | 3f5c4e8 | 2013-02-01 15:16:59 +0000 | [diff] [blame] | 704 | if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/exfat-fuse")) { |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 705 | string cmd = "/sbin/exfat-fuse -o big_writes,max_read=131072,max_write=131072 " + Actual_Block_Device + " " + Mount_Point; |
Dees_Troy | 3f5c4e8 | 2013-02-01 15:16:59 +0000 | [diff] [blame] | 706 | LOGI("cmd: %s\n", cmd.c_str()); |
| 707 | string result; |
| 708 | if (TWFunc::Exec_Cmd(cmd, result) != 0) { |
| 709 | LOGI("exfat-fuse failed to mount with result '%s', trying vfat\n", result.c_str()); |
| 710 | Current_File_System = "vfat"; |
| 711 | } else { |
| 712 | #ifdef TW_NO_EXFAT_FUSE |
| 713 | UnMount(false); |
| 714 | // We'll let the kernel handle it but using exfat-fuse to detect if the file system is actually exfat |
| 715 | // Some kernels let us mount vfat as exfat which doesn't work out too well |
| 716 | #else |
| 717 | exfat_mounted = 1; |
| 718 | #endif |
| 719 | } |
| 720 | } |
Dees_Troy | 2204203 | 2012-12-18 21:23:08 +0000 | [diff] [blame] | 721 | if (Fstab_File_System == "yaffs2") { |
| 722 | // mount an MTD partition as a YAFFS2 filesystem. |
| 723 | mtd_scan_partitions(); |
| 724 | const MtdPartition* partition; |
| 725 | partition = mtd_find_partition_by_name(MTD_Name.c_str()); |
| 726 | if (partition == NULL) { |
| 727 | LOGE("Failed to find '%s' partition to mount at '%s'\n", |
| 728 | MTD_Name.c_str(), Mount_Point.c_str()); |
| 729 | return false; |
| 730 | } |
| 731 | if (mtd_mount_partition(partition, Mount_Point.c_str(), Fstab_File_System.c_str(), 0)) { |
| 732 | if (Display_Error) |
| 733 | LOGE("Failed to mount '%s' (MTD)\n", Mount_Point.c_str()); |
| 734 | else |
| 735 | LOGI("Failed to mount '%s' (MTD)\n", Mount_Point.c_str()); |
| 736 | return false; |
| 737 | } else |
| 738 | return true; |
Dees_Troy | 3f5c4e8 | 2013-02-01 15:16:59 +0000 | [diff] [blame] | 739 | } else if (!exfat_mounted && mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Current_File_System.c_str(), 0, NULL) != 0) { |
| 740 | #ifdef TW_NO_EXFAT_FUSE |
| 741 | if (Current_File_System == "exfat") { |
| 742 | LOGI("Mounting exfat failed, trying vfat...\n"); |
| 743 | if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), "vfat", 0, NULL) != 0) { |
Dees_Troy | 85f44ed | 2013-01-09 18:42:36 +0000 | [diff] [blame] | 744 | if (Display_Error) |
Dees_Troy | 3f5c4e8 | 2013-02-01 15:16:59 +0000 | [diff] [blame] | 745 | LOGE("Unable to mount '%s'\n", Mount_Point.c_str()); |
Dees_Troy | 85f44ed | 2013-01-09 18:42:36 +0000 | [diff] [blame] | 746 | else |
Dees_Troy | 3f5c4e8 | 2013-02-01 15:16:59 +0000 | [diff] [blame] | 747 | LOGI("Unable to mount '%s'\n", Mount_Point.c_str()); |
| 748 | LOGI("Actual block device: '%s', current file system: '%s'\n", Actual_Block_Device.c_str(), Current_File_System.c_str()); |
| 749 | return false; |
Dees_Troy | 85f44ed | 2013-01-09 18:42:36 +0000 | [diff] [blame] | 750 | } |
Dees_Troy | b05ddee | 2013-01-28 20:24:50 +0000 | [diff] [blame] | 751 | } else { |
Dees_Troy | 3f5c4e8 | 2013-02-01 15:16:59 +0000 | [diff] [blame] | 752 | #endif |
| 753 | if (Display_Error) |
| 754 | LOGE("Unable to mount '%s'\n", Mount_Point.c_str()); |
| 755 | else |
| 756 | LOGI("Unable to mount '%s'\n", Mount_Point.c_str()); |
| 757 | LOGI("Actual block device: '%s', current file system: '%s'\n", Actual_Block_Device.c_str(), Current_File_System.c_str()); |
| 758 | return false; |
| 759 | #ifdef TW_NO_EXFAT_FUSE |
Dees_Troy | 85f44ed | 2013-01-09 18:42:36 +0000 | [diff] [blame] | 760 | } |
| 761 | #endif |
Dees_Troy | 3f5c4e8 | 2013-02-01 15:16:59 +0000 | [diff] [blame] | 762 | } |
| 763 | #ifdef TW_INCLUDE_CRYPTO_SAMSUNG |
| 764 | string MetaEcfsFile = EXPAND(TW_EXTERNAL_STORAGE_PATH); |
| 765 | MetaEcfsFile += "/.MetaEcfsFile"; |
| 766 | if (EcryptFS_Password.size() > 0 && PartitionManager.Mount_By_Path("/data", false) && TWFunc::Path_Exists(MetaEcfsFile)) { |
| 767 | if (mount_ecryptfs_drive(EcryptFS_Password.c_str(), Mount_Point.c_str(), Mount_Point.c_str(), 0) != 0) { |
| 768 | if (Display_Error) |
| 769 | LOGE("Unable to mount ecryptfs for '%s'\n", Mount_Point.c_str()); |
| 770 | else |
| 771 | LOGI("Unable to mount ecryptfs for '%s'\n", Mount_Point.c_str()); |
| 772 | } else { |
| 773 | LOGI("Successfully mounted ecryptfs for '%s'\n", Mount_Point.c_str()); |
| 774 | Is_Decrypted = true; |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 775 | } |
Dees_Troy | 3f5c4e8 | 2013-02-01 15:16:59 +0000 | [diff] [blame] | 776 | } else { |
| 777 | Is_Decrypted = false; |
| 778 | } |
| 779 | #endif |
| 780 | if (Removable) |
| 781 | Update_Size(Display_Error); |
| 782 | |
| 783 | if (!Symlink_Mount_Point.empty()) { |
| 784 | string Command, Result; |
| 785 | Command = "mount '" + Symlink_Path + "' '" + Symlink_Mount_Point + "'"; |
| 786 | TWFunc::Exec_Cmd(Command, Result); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 787 | } |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 788 | return true; |
| 789 | } |
| 790 | |
| 791 | bool TWPartition::UnMount(bool Display_Error) { |
| 792 | if (Is_Mounted()) { |
| 793 | int never_unmount_system; |
| 794 | |
| 795 | DataManager::GetValue(TW_DONT_UNMOUNT_SYSTEM, never_unmount_system); |
| 796 | if (never_unmount_system == 1 && Mount_Point == "/system") |
| 797 | return true; // Never unmount system if you're not supposed to unmount it |
| 798 | |
Dees_Troy | c8bafa1 | 2013-01-10 15:43:00 +0000 | [diff] [blame] | 799 | #ifdef TW_INCLUDE_CRYPTO_SAMSUNG |
| 800 | if (EcryptFS_Password.size() > 0) { |
| 801 | if (unmount_ecryptfs_drive(Mount_Point.c_str()) != 0) { |
| 802 | if (Display_Error) |
| 803 | LOGE("Unable to unmount ecryptfs for '%s'\n", Mount_Point.c_str()); |
| 804 | else |
| 805 | LOGI("Unable to unmount ecryptfs for '%s'\n", Mount_Point.c_str()); |
| 806 | } else { |
| 807 | LOGI("Successfully unmounted ecryptfs for '%s'\n", Mount_Point.c_str()); |
| 808 | } |
| 809 | } |
| 810 | #endif |
| 811 | |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 812 | if (!Symlink_Mount_Point.empty()) |
| 813 | umount(Symlink_Mount_Point.c_str()); |
| 814 | |
Dees_Troy | b05ddee | 2013-01-28 20:24:50 +0000 | [diff] [blame] | 815 | umount(Mount_Point.c_str()); |
| 816 | if (Is_Mounted()) { |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 817 | if (Display_Error) |
| 818 | LOGE("Unable to unmount '%s'\n", Mount_Point.c_str()); |
| 819 | else |
| 820 | LOGI("Unable to unmount '%s'\n", Mount_Point.c_str()); |
| 821 | return false; |
| 822 | } else |
| 823 | return true; |
| 824 | } else { |
| 825 | return true; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 826 | } |
| 827 | } |
| 828 | |
Gary Peck | 43acadf | 2012-11-21 21:19:01 -0800 | [diff] [blame] | 829 | bool TWPartition::Wipe(string New_File_System) { |
Dees_Troy | 16c2b31 | 2013-01-15 16:51:18 +0000 | [diff] [blame] | 830 | bool wiped = false, update_crypt = false; |
| 831 | int check; |
| 832 | string Layout_Filename = Mount_Point + "/.layout_version"; |
| 833 | |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 834 | if (!Can_Be_Wiped) { |
| 835 | LOGE("Partition '%s' cannot be wiped.\n", Mount_Point.c_str()); |
| 836 | return false; |
| 837 | } |
| 838 | |
Dees_Troy | c51f1f9 | 2012-09-20 15:32:13 -0400 | [diff] [blame] | 839 | if (Mount_Point == "/cache") |
| 840 | tmplog_offset = 0; |
| 841 | |
Dees_Troy | ce67546 | 2013-01-09 19:48:21 +0000 | [diff] [blame] | 842 | #ifdef TW_INCLUDE_CRYPTO_SAMSUNG |
| 843 | if (Mount_Point == "/data" && Mount(false)) { |
| 844 | if (TWFunc::Path_Exists("/data/system/edk_p_sd")) |
| 845 | TWFunc::copy_file("/data/system/edk_p_sd", "/tmp/edk_p_sd", 0600); |
| 846 | } |
| 847 | #endif |
| 848 | |
Dees_Troy | 16c2b31 | 2013-01-15 16:51:18 +0000 | [diff] [blame] | 849 | if (Retain_Layout_Version && Mount(false) && TWFunc::Path_Exists(Layout_Filename)) |
| 850 | TWFunc::copy_file(Layout_Filename, "/.layout_version", 0600); |
| 851 | else |
| 852 | unlink("/.layout_version"); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 853 | |
Dees_Troy | 16c2b31 | 2013-01-15 16:51:18 +0000 | [diff] [blame] | 854 | if (Has_Data_Media) { |
| 855 | wiped = Wipe_Data_Without_Wiping_Media(); |
| 856 | } else { |
Gary Peck | e8bc5d7 | 2012-12-21 06:45:25 -0800 | [diff] [blame] | 857 | |
Dees_Troy | 16c2b31 | 2013-01-15 16:51:18 +0000 | [diff] [blame] | 858 | DataManager::GetValue(TW_RM_RF_VAR, check); |
| 859 | |
| 860 | if (check) |
| 861 | wiped = Wipe_RMRF(); |
| 862 | else if (New_File_System == "ext4") |
| 863 | wiped = Wipe_EXT4(); |
| 864 | else if (New_File_System == "ext2" || New_File_System == "ext3") |
| 865 | wiped = Wipe_EXT23(New_File_System); |
| 866 | else if (New_File_System == "vfat") |
| 867 | wiped = Wipe_FAT(); |
| 868 | else if (New_File_System == "exfat") |
| 869 | wiped = Wipe_EXFAT(); |
| 870 | else if (New_File_System == "yaffs2") |
| 871 | wiped = Wipe_MTD(); |
| 872 | else { |
| 873 | LOGE("Unable to wipe '%s' -- unknown file system '%s'\n", Mount_Point.c_str(), New_File_System.c_str()); |
| 874 | unlink("/.layout_version"); |
| 875 | return false; |
| 876 | } |
| 877 | update_crypt = wiped; |
Gary Peck | e8bc5d7 | 2012-12-21 06:45:25 -0800 | [diff] [blame] | 878 | } |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 879 | |
Gary Peck | e8bc5d7 | 2012-12-21 06:45:25 -0800 | [diff] [blame] | 880 | if (wiped) { |
Dees_Troy | ce67546 | 2013-01-09 19:48:21 +0000 | [diff] [blame] | 881 | #ifdef TW_INCLUDE_CRYPTO_SAMSUNG |
| 882 | if (Mount_Point == "/data" && Mount(false)) { |
| 883 | if (TWFunc::Path_Exists("/tmp/edk_p_sd")) { |
| 884 | Make_Dir("/data/system", true); |
| 885 | TWFunc::copy_file("/tmp/edk_p_sd", "/data/system/edk_p_sd", 0600); |
| 886 | } |
| 887 | } |
| 888 | #endif |
Dees_Troy | 16c2b31 | 2013-01-15 16:51:18 +0000 | [diff] [blame] | 889 | |
Dees_Troy | 1c1ac44 | 2013-01-17 21:42:14 +0000 | [diff] [blame] | 890 | if (Mount_Point == "/cache") |
| 891 | DataManager::Output_Version(); |
| 892 | |
Dees_Troy | 16c2b31 | 2013-01-15 16:51:18 +0000 | [diff] [blame] | 893 | if (TWFunc::Path_Exists("/.layout_version") && Mount(false)) |
| 894 | TWFunc::copy_file("/.layout_version", Layout_Filename, 0600); |
| 895 | |
| 896 | if (update_crypt) { |
| 897 | Setup_File_System(false); |
| 898 | if (Is_Encrypted && !Is_Decrypted) { |
| 899 | // just wiped an encrypted partition back to its unencrypted state |
| 900 | Is_Encrypted = false; |
| 901 | Is_Decrypted = false; |
| 902 | Decrypted_Block_Device = ""; |
| 903 | if (Mount_Point == "/data") { |
| 904 | DataManager::SetValue(TW_IS_ENCRYPTED, 0); |
| 905 | DataManager::SetValue(TW_IS_DECRYPTED, 0); |
| 906 | } |
Gary Peck | e8bc5d7 | 2012-12-21 06:45:25 -0800 | [diff] [blame] | 907 | } |
| 908 | } |
| 909 | } |
| 910 | return wiped; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 911 | } |
| 912 | |
Gary Peck | 43acadf | 2012-11-21 21:19:01 -0800 | [diff] [blame] | 913 | bool TWPartition::Wipe() { |
Gary Peck | 82599a8 | 2012-11-21 16:23:12 -0800 | [diff] [blame] | 914 | if (Is_File_System(Current_File_System)) |
| 915 | return Wipe(Current_File_System); |
| 916 | else |
| 917 | return Wipe(Fstab_File_System); |
Gary Peck | 43acadf | 2012-11-21 21:19:01 -0800 | [diff] [blame] | 918 | } |
| 919 | |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 920 | bool TWPartition::Wipe_AndSec(void) { |
| 921 | if (!Has_Android_Secure) |
| 922 | return false; |
| 923 | |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 924 | if (!Mount(true)) |
| 925 | return false; |
| 926 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 927 | ui_print("Wiping .android_secure\n"); |
| 928 | TWFunc::removeDir(Mount_Point + "/.android_secure/", true); |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 929 | return true; |
| 930 | } |
| 931 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 932 | bool TWPartition::Backup(string backup_folder) { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 933 | if (Backup_Method == FILES) |
| 934 | return Backup_Tar(backup_folder); |
| 935 | else if (Backup_Method == DD) |
| 936 | return Backup_DD(backup_folder); |
| 937 | else if (Backup_Method == FLASH_UTILS) |
| 938 | return Backup_Dump_Image(backup_folder); |
| 939 | LOGE("Unknown backup method for '%s'\n", Mount_Point.c_str()); |
| 940 | return false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 941 | } |
| 942 | |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 943 | bool TWPartition::Check_MD5(string restore_folder) { |
bigbiff bigbiff | 65a4c73 | 2013-03-15 15:17:50 -0400 | [diff] [blame] | 944 | string Full_Filename, md5file; |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 945 | char split_filename[512]; |
| 946 | int index = 0; |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 947 | twrpDigest md5sum; |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 948 | |
bigbiff bigbiff | 65a4c73 | 2013-03-15 15:17:50 -0400 | [diff] [blame] | 949 | memset(split_filename, 0, sizeof(split_filename)); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 950 | Full_Filename = restore_folder + "/" + Backup_FileName; |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 951 | if (!TWFunc::Path_Exists(Full_Filename)) { |
| 952 | // This is a split archive, we presume |
| 953 | sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index); |
bigbiff bigbiff | 65a4c73 | 2013-03-15 15:17:50 -0400 | [diff] [blame] | 954 | LOGI("split_filename: %s\n", split_filename); |
| 955 | md5file = split_filename; |
| 956 | md5file += ".md5"; |
| 957 | if (!TWFunc::Path_Exists(md5file)) { |
| 958 | LOGE("No md5 file found for '%s'.\n", split_filename); |
| 959 | LOGE("Please unselect Enable MD5 verification to restore.\n"); |
| 960 | return false; |
| 961 | } |
| 962 | md5sum.setfn(split_filename); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 963 | while (index < 1000 && TWFunc::Path_Exists(split_filename)) { |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 964 | if (md5sum.verify_md5digest() != 0) { |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 965 | LOGE("MD5 failed to match on '%s'.\n", split_filename); |
| 966 | return false; |
| 967 | } |
| 968 | index++; |
| 969 | sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index); |
bigbiff bigbiff | 65a4c73 | 2013-03-15 15:17:50 -0400 | [diff] [blame] | 970 | md5sum.setfn(split_filename); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 971 | } |
Dees_Troy | 4a2a126 | 2012-09-18 09:33:47 -0400 | [diff] [blame] | 972 | return true; |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 973 | } else { |
| 974 | // Single file archive |
bigbiff bigbiff | 65a4c73 | 2013-03-15 15:17:50 -0400 | [diff] [blame] | 975 | md5file = Full_Filename + ".md5"; |
| 976 | if (!TWFunc::Path_Exists(md5file)) { |
| 977 | LOGE("No md5 file found for '%s'.\n", Full_Filename.c_str()); |
| 978 | LOGE("Please unselect Enable MD5 verification to restore.\n"); |
| 979 | return false; |
| 980 | } |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 981 | md5sum.setfn(Full_Filename); |
| 982 | if (md5sum.verify_md5digest() != 0) { |
bigbiff bigbiff | 65a4c73 | 2013-03-15 15:17:50 -0400 | [diff] [blame] | 983 | LOGE("MD5 failed to match on '%s'.\n", Full_Filename.c_str()); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 984 | return false; |
| 985 | } else |
| 986 | return true; |
| 987 | } |
| 988 | return false; |
| 989 | } |
| 990 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 991 | bool TWPartition::Restore(string restore_folder) { |
Gary Peck | 43acadf | 2012-11-21 21:19:01 -0800 | [diff] [blame] | 992 | size_t first_period, second_period; |
| 993 | string Restore_File_System; |
| 994 | |
| 995 | TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring"); |
| 996 | LOGI("Restore filename is: %s\n", Backup_FileName.c_str()); |
| 997 | |
| 998 | // Parse backup filename to extract the file system before wiping |
| 999 | first_period = Backup_FileName.find("."); |
| 1000 | if (first_period == string::npos) { |
| 1001 | LOGE("Unable to find file system (first period).\n"); |
| 1002 | return false; |
| 1003 | } |
| 1004 | Restore_File_System = Backup_FileName.substr(first_period + 1, Backup_FileName.size() - first_period - 1); |
| 1005 | second_period = Restore_File_System.find("."); |
| 1006 | if (second_period == string::npos) { |
| 1007 | LOGE("Unable to find file system (second period).\n"); |
| 1008 | return false; |
| 1009 | } |
| 1010 | Restore_File_System.resize(second_period); |
| 1011 | LOGI("Restore file system is: '%s'.\n", Restore_File_System.c_str()); |
| 1012 | |
| 1013 | if (Is_File_System(Restore_File_System)) |
| 1014 | return Restore_Tar(restore_folder, Restore_File_System); |
| 1015 | else if (Is_Image(Restore_File_System)) { |
| 1016 | if (Restore_File_System == "emmc") |
| 1017 | return Restore_DD(restore_folder); |
| 1018 | else if (Restore_File_System == "mtd" || Restore_File_System == "bml") |
| 1019 | return Restore_Flash_Image(restore_folder); |
| 1020 | } |
| 1021 | |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1022 | LOGE("Unknown restore method for '%s'\n", Mount_Point.c_str()); |
| 1023 | return false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1024 | } |
| 1025 | |
| 1026 | string TWPartition::Backup_Method_By_Name() { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1027 | if (Backup_Method == NONE) |
| 1028 | return "none"; |
| 1029 | else if (Backup_Method == FILES) |
| 1030 | return "files"; |
| 1031 | else if (Backup_Method == DD) |
| 1032 | return "dd"; |
| 1033 | else if (Backup_Method == FLASH_UTILS) |
| 1034 | return "flash_utils"; |
| 1035 | else |
| 1036 | return "undefined"; |
| 1037 | return "ERROR!"; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1038 | } |
| 1039 | |
| 1040 | bool TWPartition::Decrypt(string Password) { |
| 1041 | LOGI("STUB TWPartition::Decrypt, password: '%s'\n", Password.c_str()); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1042 | // Is this needed? |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1043 | return 1; |
| 1044 | } |
| 1045 | |
| 1046 | bool TWPartition::Wipe_Encryption() { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1047 | bool Save_Data_Media = Has_Data_Media; |
| 1048 | |
| 1049 | if (!UnMount(true)) |
| 1050 | return false; |
| 1051 | |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1052 | Has_Data_Media = false; |
Gary Peck | e8bc5d7 | 2012-12-21 06:45:25 -0800 | [diff] [blame] | 1053 | if (Wipe(Fstab_File_System)) { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1054 | Has_Data_Media = Save_Data_Media; |
| 1055 | if (Has_Data_Media && !Symlink_Mount_Point.empty()) { |
| 1056 | Recreate_Media_Folder(); |
| 1057 | } |
Dees_Troy | b46a684 | 2012-09-25 11:06:46 -0400 | [diff] [blame] | 1058 | ui_print("You may need to reboot recovery to be able to use /data again.\n"); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1059 | return true; |
| 1060 | } else { |
| 1061 | Has_Data_Media = Save_Data_Media; |
| 1062 | LOGE("Unable to format to remove encryption.\n"); |
| 1063 | return false; |
| 1064 | } |
| 1065 | return false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1066 | } |
| 1067 | |
| 1068 | void TWPartition::Check_FS_Type() { |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 1069 | const char* type; |
| 1070 | blkid_probe pr; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1071 | |
Dees_Troy | 68cab49 | 2012-12-12 19:29:35 +0000 | [diff] [blame] | 1072 | if (Fstab_File_System == "yaffs2" || Fstab_File_System == "mtd" || Fstab_File_System == "bml" || Ignore_Blkid) |
| 1073 | return; // Running blkid on some mtd devices causes a massive crash or needs to be skipped |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1074 | |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1075 | Find_Actual_Block_Device(); |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 1076 | if (!Is_Present) |
| 1077 | return; |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 1078 | |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 1079 | pr = blkid_new_probe_from_filename(Actual_Block_Device.c_str()); |
| 1080 | if (blkid_do_fullprobe(pr)) { |
| 1081 | blkid_free_probe(pr); |
| 1082 | LOGI("Can't probe device %s\n", Actual_Block_Device.c_str()); |
| 1083 | return; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1084 | } |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 1085 | if (blkid_probe_lookup_value(pr, "TYPE", &type, NULL) < 0) { |
| 1086 | blkid_free_probe(pr); |
| 1087 | LOGI("can't find filesystem on device %s\n", Actual_Block_Device.c_str()); |
| 1088 | return; |
| 1089 | } |
| 1090 | Current_File_System = type; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1091 | return; |
| 1092 | } |
| 1093 | |
Gary Peck | 43acadf | 2012-11-21 21:19:01 -0800 | [diff] [blame] | 1094 | bool TWPartition::Wipe_EXT23(string File_System) { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1095 | if (!UnMount(true)) |
| 1096 | return false; |
| 1097 | |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 1098 | if (TWFunc::Path_Exists("/sbin/mke2fs")) { |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1099 | string command, result; |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1100 | |
| 1101 | ui_print("Formatting %s using mke2fs...\n", Display_Name.c_str()); |
| 1102 | Find_Actual_Block_Device(); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1103 | command = "mke2fs -t " + File_System + " -m 0 " + Actual_Block_Device; |
| 1104 | LOGI("mke2fs command: %s\n", command.c_str()); |
| 1105 | if (TWFunc::Exec_Cmd(command, result) == 0) { |
Gary Peck | e8bc5d7 | 2012-12-21 06:45:25 -0800 | [diff] [blame] | 1106 | Current_File_System = File_System; |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 1107 | Recreate_AndSec_Folder(); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1108 | ui_print("Done.\n"); |
| 1109 | return true; |
| 1110 | } else { |
| 1111 | LOGE("Unable to wipe '%s'.\n", Mount_Point.c_str()); |
| 1112 | return false; |
| 1113 | } |
| 1114 | } else |
| 1115 | return Wipe_RMRF(); |
| 1116 | |
| 1117 | return false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1118 | } |
| 1119 | |
| 1120 | bool TWPartition::Wipe_EXT4() { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1121 | if (!UnMount(true)) |
| 1122 | return false; |
| 1123 | |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 1124 | if (TWFunc::Path_Exists("/sbin/make_ext4fs")) { |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1125 | string Command, result; |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1126 | |
| 1127 | ui_print("Formatting %s using make_ext4fs...\n", Display_Name.c_str()); |
| 1128 | Find_Actual_Block_Device(); |
| 1129 | Command = "make_ext4fs"; |
| 1130 | if (!Is_Decrypted && Length != 0) { |
| 1131 | // Only use length if we're not decrypted |
| 1132 | char len[32]; |
| 1133 | sprintf(len, "%i", Length); |
| 1134 | Command += " -l "; |
| 1135 | Command += len; |
| 1136 | } |
| 1137 | Command += " " + Actual_Block_Device; |
| 1138 | LOGI("make_ext4fs command: %s\n", Command.c_str()); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1139 | if (TWFunc::Exec_Cmd(Command, result) == 0) { |
Gary Peck | e8bc5d7 | 2012-12-21 06:45:25 -0800 | [diff] [blame] | 1140 | Current_File_System = "ext4"; |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 1141 | Recreate_AndSec_Folder(); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1142 | ui_print("Done.\n"); |
| 1143 | return true; |
| 1144 | } else { |
| 1145 | LOGE("Unable to wipe '%s'.\n", Mount_Point.c_str()); |
| 1146 | return false; |
| 1147 | } |
| 1148 | } else |
Gary Peck | 43acadf | 2012-11-21 21:19:01 -0800 | [diff] [blame] | 1149 | return Wipe_EXT23("ext4"); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1150 | |
| 1151 | return false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1152 | } |
| 1153 | |
| 1154 | bool TWPartition::Wipe_FAT() { |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1155 | string command, result; |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1156 | |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 1157 | if (TWFunc::Path_Exists("/sbin/mkdosfs")) { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1158 | if (!UnMount(true)) |
| 1159 | return false; |
| 1160 | |
| 1161 | ui_print("Formatting %s using mkdosfs...\n", Display_Name.c_str()); |
| 1162 | Find_Actual_Block_Device(); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1163 | command = "mkdosfs " + Actual_Block_Device; |
| 1164 | if (TWFunc::Exec_Cmd(command, result) == 0) { |
Gary Peck | e8bc5d7 | 2012-12-21 06:45:25 -0800 | [diff] [blame] | 1165 | Current_File_System = "vfat"; |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 1166 | Recreate_AndSec_Folder(); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1167 | ui_print("Done.\n"); |
| 1168 | return true; |
| 1169 | } else { |
| 1170 | LOGE("Unable to wipe '%s'.\n", Mount_Point.c_str()); |
| 1171 | return false; |
| 1172 | } |
| 1173 | return true; |
| 1174 | } |
| 1175 | else |
| 1176 | return Wipe_RMRF(); |
| 1177 | |
| 1178 | return false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1179 | } |
| 1180 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1181 | bool TWPartition::Wipe_EXFAT() { |
| 1182 | string command, result; |
| 1183 | |
| 1184 | if (TWFunc::Path_Exists("/sbin/mkexfatfs")) { |
| 1185 | if (!UnMount(true)) |
| 1186 | return false; |
| 1187 | |
| 1188 | ui_print("Formatting %s using mkexfatfs...\n", Display_Name.c_str()); |
| 1189 | Find_Actual_Block_Device(); |
| 1190 | command = "mkexfatfs " + Actual_Block_Device; |
| 1191 | if (TWFunc::Exec_Cmd(command, result) == 0) { |
| 1192 | Recreate_AndSec_Folder(); |
| 1193 | ui_print("Done.\n"); |
| 1194 | return true; |
| 1195 | } else { |
| 1196 | LOGE("Unable to wipe '%s'.\n", Mount_Point.c_str()); |
| 1197 | return false; |
| 1198 | } |
| 1199 | return true; |
| 1200 | } |
| 1201 | return false; |
| 1202 | } |
| 1203 | |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1204 | bool TWPartition::Wipe_MTD() { |
| 1205 | if (!UnMount(true)) |
| 1206 | return false; |
| 1207 | |
| 1208 | ui_print("MTD Formatting \"%s\"\n", MTD_Name.c_str()); |
| 1209 | |
| 1210 | mtd_scan_partitions(); |
| 1211 | const MtdPartition* mtd = mtd_find_partition_by_name(MTD_Name.c_str()); |
| 1212 | if (mtd == NULL) { |
| 1213 | LOGE("No mtd partition named '%s'", MTD_Name.c_str()); |
| 1214 | return false; |
| 1215 | } |
| 1216 | |
| 1217 | MtdWriteContext* ctx = mtd_write_partition(mtd); |
| 1218 | if (ctx == NULL) { |
| 1219 | LOGE("Can't write '%s', failed to format.", MTD_Name.c_str()); |
| 1220 | return false; |
| 1221 | } |
| 1222 | if (mtd_erase_blocks(ctx, -1) == -1) { |
| 1223 | mtd_write_close(ctx); |
| 1224 | LOGE("Failed to format '%s'", MTD_Name.c_str()); |
| 1225 | return false; |
| 1226 | } |
| 1227 | if (mtd_write_close(ctx) != 0) { |
| 1228 | LOGE("Failed to close '%s'", MTD_Name.c_str()); |
| 1229 | return false; |
| 1230 | } |
Gary Peck | e8bc5d7 | 2012-12-21 06:45:25 -0800 | [diff] [blame] | 1231 | Current_File_System = "yaffs2"; |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 1232 | Recreate_AndSec_Folder(); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1233 | ui_print("Done.\n"); |
| 1234 | return true; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1235 | } |
| 1236 | |
| 1237 | bool TWPartition::Wipe_RMRF() { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1238 | if (!Mount(true)) |
| 1239 | return false; |
| 1240 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1241 | ui_print("Removing all files under '%s'\n", Mount_Point.c_str()); |
| 1242 | TWFunc::removeDir(Mount_Point, true); |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 1243 | Recreate_AndSec_Folder(); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1244 | return true; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1245 | } |
| 1246 | |
| 1247 | bool TWPartition::Wipe_Data_Without_Wiping_Media() { |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1248 | string dir; |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1249 | |
| 1250 | // This handles wiping data on devices with "sdcard" in /data/media |
| 1251 | if (!Mount(true)) |
| 1252 | return false; |
| 1253 | |
| 1254 | ui_print("Wiping data without wiping /data/media ...\n"); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1255 | |
| 1256 | DIR* d; |
| 1257 | d = opendir("/data"); |
Dees_Troy | 16b7435 | 2012-11-14 22:27:31 +0000 | [diff] [blame] | 1258 | if (d != NULL) { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1259 | struct dirent* de; |
| 1260 | while ((de = readdir(d)) != NULL) { |
Dees_Troy | 16b7435 | 2012-11-14 22:27:31 +0000 | [diff] [blame] | 1261 | if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) continue; |
| 1262 | // The media folder is the "internal sdcard" |
| 1263 | // The .layout_version file is responsible for determining whether 4.2 decides up upgrade |
| 1264 | // the media folder for multi-user. |
| 1265 | if (strcmp(de->d_name, "media") == 0 || strcmp(de->d_name, ".layout_version") == 0) continue; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1266 | |
| 1267 | dir = "/data/"; |
| 1268 | dir.append(de->d_name); |
Dees_Troy | ce67546 | 2013-01-09 19:48:21 +0000 | [diff] [blame] | 1269 | if (de->d_type == DT_DIR) { |
| 1270 | TWFunc::removeDir(dir, false); |
bigbiff bigbiff | 98f1f90 | 2013-01-19 18:46:13 -0500 | [diff] [blame] | 1271 | } else if (de->d_type == DT_REG || de->d_type == DT_LNK || de->d_type == DT_FIFO || de->d_type == DT_SOCK) { |
Dees_Troy | ce67546 | 2013-01-09 19:48:21 +0000 | [diff] [blame] | 1272 | if (!unlink(dir.c_str())) |
| 1273 | LOGI("Unable to unlink '%s'\n", dir.c_str()); |
| 1274 | } |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1275 | } |
| 1276 | closedir(d); |
Dees_Troy | 16b7435 | 2012-11-14 22:27:31 +0000 | [diff] [blame] | 1277 | ui_print("Done.\n"); |
| 1278 | return true; |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1279 | } |
Dees_Troy | 16b7435 | 2012-11-14 22:27:31 +0000 | [diff] [blame] | 1280 | ui_print("Dirent failed to open /data, error!\n"); |
| 1281 | return false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1282 | } |
| 1283 | |
| 1284 | bool TWPartition::Backup_Tar(string backup_folder) { |
Dees_Troy | 4a2a126 | 2012-09-18 09:33:47 -0400 | [diff] [blame] | 1285 | char back_name[255], split_index[5]; |
| 1286 | string Full_FileName, Split_FileName, Tar_Args, Command; |
| 1287 | int use_compression, index, backup_count; |
| 1288 | struct stat st; |
Dees_Troy | 7c2dec8 | 2012-09-26 09:49:14 -0400 | [diff] [blame] | 1289 | unsigned long long total_bsize = 0, file_size; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1290 | twrpTar tar; |
| 1291 | vector <string> files; |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 1292 | |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 1293 | if (!Mount(true)) |
| 1294 | return false; |
| 1295 | |
Dees_Troy | 2c50e18 | 2012-09-26 20:05:28 -0400 | [diff] [blame] | 1296 | if (Backup_Path == "/and-sec") { |
| 1297 | TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, "Android Secure", "Backing Up"); |
| 1298 | ui_print("Backing up %s...\n", "Android Secure"); |
| 1299 | } else { |
| 1300 | TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up"); |
| 1301 | ui_print("Backing up %s...\n", Display_Name.c_str()); |
| 1302 | } |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 1303 | |
| 1304 | DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 1305 | |
| 1306 | sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str()); |
| 1307 | Backup_FileName = back_name; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1308 | Full_FileName = backup_folder + "/" + Backup_FileName; |
| 1309 | if (Backup_Size > MAX_ARCHIVE_SIZE) { |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 1310 | // This backup needs to be split into multiple archives |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1311 | ui_print("Breaking backup file into multiple archives...\n"); |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 1312 | sprintf(back_name, "%s", Backup_Path.c_str()); |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 1313 | tar.setdir(back_name); |
| 1314 | tar.setfn(Full_FileName); |
bigbiff bigbiff | e6594ab | 2013-02-17 20:18:31 -0500 | [diff] [blame] | 1315 | backup_count = tar.splitArchiveFork(); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1316 | if (backup_count == -1) { |
| 1317 | LOGE("Error tarring split files!\n"); |
Dees_Troy | 4a2a126 | 2012-09-18 09:33:47 -0400 | [diff] [blame] | 1318 | return false; |
| 1319 | } |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1320 | return true; |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 1321 | } else { |
Dees_Troy | 4a2a126 | 2012-09-18 09:33:47 -0400 | [diff] [blame] | 1322 | Full_FileName = backup_folder + "/" + Backup_FileName; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1323 | if (use_compression) { |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 1324 | tar.setdir(Backup_Path); |
| 1325 | tar.setfn(Full_FileName); |
bigbiff bigbiff | e6594ab | 2013-02-17 20:18:31 -0500 | [diff] [blame] | 1326 | if (tar.createTarGZFork() != 0) |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 1327 | return -1; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1328 | string gzname = Full_FileName + ".gz"; |
| 1329 | rename(gzname.c_str(), Full_FileName.c_str()); |
| 1330 | } |
| 1331 | else { |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 1332 | tar.setdir(Backup_Path); |
| 1333 | tar.setfn(Full_FileName); |
bigbiff bigbiff | e6594ab | 2013-02-17 20:18:31 -0500 | [diff] [blame] | 1334 | if (tar.createTarFork() != 0) |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 1335 | return -1; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1336 | } |
Dees_Troy | 7c2dec8 | 2012-09-26 09:49:14 -0400 | [diff] [blame] | 1337 | if (TWFunc::Get_File_Size(Full_FileName) == 0) { |
| 1338 | LOGE("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str()); |
| 1339 | return false; |
| 1340 | } |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 1341 | } |
| 1342 | return true; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1343 | } |
| 1344 | |
| 1345 | bool TWPartition::Backup_DD(string backup_folder) { |
igoriok | 87e3d93 | 2013-01-31 21:03:53 +0200 | [diff] [blame] | 1346 | char back_name[255], backup_size[32]; |
| 1347 | string Full_FileName, Command, result, DD_BS; |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 1348 | int use_compression; |
| 1349 | |
igoriok | 87e3d93 | 2013-01-31 21:03:53 +0200 | [diff] [blame] | 1350 | sprintf(backup_size, "%llu", Backup_Size); |
| 1351 | DD_BS = backup_size; |
| 1352 | |
Dees_Troy | b46a684 | 2012-09-25 11:06:46 -0400 | [diff] [blame] | 1353 | TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up"); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 1354 | ui_print("Backing up %s...\n", Display_Name.c_str()); |
| 1355 | |
| 1356 | sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str()); |
| 1357 | Backup_FileName = back_name; |
| 1358 | |
| 1359 | Full_FileName = backup_folder + "/" + Backup_FileName; |
| 1360 | |
igoriok | 87e3d93 | 2013-01-31 21:03:53 +0200 | [diff] [blame] | 1361 | Command = "dd if=" + Actual_Block_Device + " of='" + Full_FileName + "'" + " bs=" + DD_BS + "c count=1"; |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 1362 | LOGI("Backup command: '%s'\n", Command.c_str()); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1363 | TWFunc::Exec_Cmd(Command, result); |
Dees_Troy | c154ac2 | 2012-10-12 15:36:47 -0400 | [diff] [blame] | 1364 | if (TWFunc::Get_File_Size(Full_FileName) == 0) { |
| 1365 | LOGE("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str()); |
Dees_Troy | 7c2dec8 | 2012-09-26 09:49:14 -0400 | [diff] [blame] | 1366 | return false; |
| 1367 | } |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 1368 | return true; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1369 | } |
| 1370 | |
| 1371 | bool TWPartition::Backup_Dump_Image(string backup_folder) { |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 1372 | char back_name[255]; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1373 | string Full_FileName, Command, result; |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 1374 | int use_compression; |
| 1375 | |
Dees_Troy | b46a684 | 2012-09-25 11:06:46 -0400 | [diff] [blame] | 1376 | TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up"); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 1377 | ui_print("Backing up %s...\n", Display_Name.c_str()); |
| 1378 | |
| 1379 | sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str()); |
| 1380 | Backup_FileName = back_name; |
| 1381 | |
| 1382 | Full_FileName = backup_folder + "/" + Backup_FileName; |
| 1383 | |
| 1384 | Command = "dump_image " + MTD_Name + " '" + Full_FileName + "'"; |
| 1385 | LOGI("Backup command: '%s'\n", Command.c_str()); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1386 | TWFunc::Exec_Cmd(Command, result); |
Dees_Troy | 7c2dec8 | 2012-09-26 09:49:14 -0400 | [diff] [blame] | 1387 | if (TWFunc::Get_File_Size(Full_FileName) == 0) { |
| 1388 | // Actual size may not match backup size due to bad blocks on MTD devices so just check for 0 bytes |
| 1389 | LOGE("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str()); |
| 1390 | return false; |
| 1391 | } |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 1392 | return true; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1393 | } |
| 1394 | |
Gary Peck | 43acadf | 2012-11-21 21:19:01 -0800 | [diff] [blame] | 1395 | bool TWPartition::Restore_Tar(string restore_folder, string Restore_File_System) { |
| 1396 | string Full_FileName, Command; |
Dees_Troy | 4a2a126 | 2012-09-18 09:33:47 -0400 | [diff] [blame] | 1397 | int index = 0; |
| 1398 | char split_index[5]; |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 1399 | |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 1400 | if (Has_Android_Secure) { |
| 1401 | ui_print("Wiping android secure...\n"); |
| 1402 | if (!Wipe_AndSec()) |
| 1403 | return false; |
Gary Peck | 43acadf | 2012-11-21 21:19:01 -0800 | [diff] [blame] | 1404 | } else { |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 1405 | ui_print("Wiping %s...\n", Display_Name.c_str()); |
Gary Peck | 43acadf | 2012-11-21 21:19:01 -0800 | [diff] [blame] | 1406 | if (!Wipe(Restore_File_System)) |
| 1407 | return false; |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 1408 | } |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 1409 | |
| 1410 | if (!Mount(true)) |
| 1411 | return false; |
| 1412 | |
Dees_Troy | da8b55a | 2012-12-12 19:18:30 +0000 | [diff] [blame] | 1413 | TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring"); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 1414 | ui_print("Restoring %s...\n", Display_Name.c_str()); |
Dees_Troy | 4a2a126 | 2012-09-18 09:33:47 -0400 | [diff] [blame] | 1415 | Full_FileName = restore_folder + "/" + Backup_FileName; |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 1416 | if (!TWFunc::Path_Exists(Full_FileName)) { |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1417 | if (!TWFunc::Path_Exists(Full_FileName)) { |
| 1418 | // Backup is multiple archives |
| 1419 | LOGI("Backup is multiple archives.\n"); |
Dees_Troy | 4a2a126 | 2012-09-18 09:33:47 -0400 | [diff] [blame] | 1420 | sprintf(split_index, "%03i", index); |
| 1421 | Full_FileName = restore_folder + "/" + Backup_FileName + split_index; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1422 | while (TWFunc::Path_Exists(Full_FileName)) { |
| 1423 | index++; |
| 1424 | ui_print("Restoring archive %i...\n", index); |
| 1425 | LOGI("Restoring '%s'...\n", Full_FileName.c_str()); |
| 1426 | twrpTar tar; |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 1427 | tar.setdir("/"); |
| 1428 | tar.setfn(Full_FileName); |
bigbiff bigbiff | e6594ab | 2013-02-17 20:18:31 -0500 | [diff] [blame] | 1429 | if (tar.extractTarFork() != 0) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1430 | return false; |
| 1431 | sprintf(split_index, "%03i", index); |
| 1432 | Full_FileName = restore_folder + "/" + Backup_FileName + split_index; |
| 1433 | } |
| 1434 | if (index == 0) { |
| 1435 | LOGE("Error locating restore file: '%s'\n", Full_FileName.c_str()); |
| 1436 | return false; |
| 1437 | } |
Dees_Troy | 4a2a126 | 2012-09-18 09:33:47 -0400 | [diff] [blame] | 1438 | } |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 1439 | } else { |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1440 | twrpTar tar; |
bigbiff bigbiff | 3bf2b0e | 2013-01-21 21:26:43 -0500 | [diff] [blame] | 1441 | tar.setdir(Backup_Path); |
| 1442 | tar.setfn(Full_FileName); |
bigbiff bigbiff | e6594ab | 2013-02-17 20:18:31 -0500 | [diff] [blame] | 1443 | if (tar.extractTarFork() != 0) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1444 | return false; |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 1445 | } |
| 1446 | return true; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1447 | } |
| 1448 | |
| 1449 | bool TWPartition::Restore_DD(string restore_folder) { |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1450 | string Full_FileName, Command, result; |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 1451 | |
Dees_Troy | da8b55a | 2012-12-12 19:18:30 +0000 | [diff] [blame] | 1452 | TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring"); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 1453 | Full_FileName = restore_folder + "/" + Backup_FileName; |
Gary Peck | 15e623d | 2012-11-21 21:07:58 -0800 | [diff] [blame] | 1454 | |
| 1455 | if (!Find_Partition_Size()) { |
| 1456 | LOGE("Unable to find partition size for '%s'\n", Mount_Point.c_str()); |
| 1457 | return false; |
| 1458 | } |
| 1459 | unsigned long long backup_size = TWFunc::Get_File_Size(Full_FileName); |
| 1460 | if (backup_size > Size) { |
| 1461 | LOGE("Size (%iMB) of backup '%s' is larger than target device '%s' (%iMB)\n", |
| 1462 | (int)(backup_size / 1048576LLU), Full_FileName.c_str(), |
| 1463 | Actual_Block_Device.c_str(), (int)(Size / 1048576LLU)); |
| 1464 | return false; |
| 1465 | } |
| 1466 | |
| 1467 | ui_print("Restoring %s...\n", Display_Name.c_str()); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 1468 | Command = "dd bs=4096 if='" + Full_FileName + "' of=" + Actual_Block_Device; |
| 1469 | LOGI("Restore command: '%s'\n", Command.c_str()); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1470 | TWFunc::Exec_Cmd(Command, result); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 1471 | return true; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1472 | } |
| 1473 | |
| 1474 | bool TWPartition::Restore_Flash_Image(string restore_folder) { |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1475 | string Full_FileName, Command, result; |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 1476 | |
| 1477 | ui_print("Restoring %s...\n", Display_Name.c_str()); |
| 1478 | Full_FileName = restore_folder + "/" + Backup_FileName; |
| 1479 | // Sometimes flash image doesn't like to flash due to the first 2KB matching, so we erase first to ensure that it flashes |
| 1480 | Command = "erase_image " + MTD_Name; |
| 1481 | LOGI("Erase command: '%s'\n", Command.c_str()); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1482 | TWFunc::Exec_Cmd(Command, result); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 1483 | Command = "flash_image " + MTD_Name + " '" + Full_FileName + "'"; |
| 1484 | LOGI("Restore command: '%s'\n", Command.c_str()); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1485 | TWFunc::Exec_Cmd(Command, result); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 1486 | return true; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1487 | } |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1488 | |
| 1489 | bool TWPartition::Update_Size(bool Display_Error) { |
Dees_Troy | 0550cfb | 2012-10-13 11:56:13 -0400 | [diff] [blame] | 1490 | bool ret = false, Was_Already_Mounted = false; |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 1491 | |
Dees_Troy | ab10ee2 | 2012-09-21 14:27:30 -0400 | [diff] [blame] | 1492 | if (!Can_Be_Mounted && !Is_Encrypted) |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1493 | return false; |
| 1494 | |
Dees_Troy | 0550cfb | 2012-10-13 11:56:13 -0400 | [diff] [blame] | 1495 | Was_Already_Mounted = Is_Mounted(); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1496 | if (Removable || Is_Encrypted) { |
| 1497 | if (!Mount(false)) |
| 1498 | return true; |
| 1499 | } else if (!Mount(Display_Error)) |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1500 | return false; |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 1501 | |
| 1502 | ret = Get_Size_Via_statfs(Display_Error); |
Dees_Troy | 0550cfb | 2012-10-13 11:56:13 -0400 | [diff] [blame] | 1503 | if (!ret || Size == 0) { |
| 1504 | if (!Get_Size_Via_df(Display_Error)) { |
| 1505 | if (!Was_Already_Mounted) |
| 1506 | UnMount(false); |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 1507 | return false; |
Dees_Troy | 0550cfb | 2012-10-13 11:56:13 -0400 | [diff] [blame] | 1508 | } |
| 1509 | } |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 1510 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1511 | if (Has_Data_Media) { |
| 1512 | if (Mount(Display_Error)) { |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 1513 | unsigned long long data_media_used, actual_data; |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 1514 | Used = TWFunc::Get_Folder_Size("/data", Display_Error); |
| 1515 | data_media_used = TWFunc::Get_Folder_Size("/data/media", Display_Error); |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 1516 | actual_data = Used - data_media_used; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1517 | Backup_Size = actual_data; |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 1518 | int bak = (int)(Backup_Size / 1048576LLU); |
| 1519 | int total = (int)(Size / 1048576LLU); |
| 1520 | int us = (int)(Used / 1048576LLU); |
| 1521 | int fre = (int)(Free / 1048576LLU); |
| 1522 | int datmed = (int)(data_media_used / 1048576LLU); |
| 1523 | LOGI("Data backup size is %iMB, size: %iMB, used: %iMB, free: %iMB, in data/media: %iMB.\n", bak, total, us, fre, datmed); |
Dees_Troy | 0550cfb | 2012-10-13 11:56:13 -0400 | [diff] [blame] | 1524 | } else { |
| 1525 | if (!Was_Already_Mounted) |
| 1526 | UnMount(false); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1527 | return false; |
Dees_Troy | 0550cfb | 2012-10-13 11:56:13 -0400 | [diff] [blame] | 1528 | } |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 1529 | } else if (Has_Android_Secure) { |
| 1530 | if (Mount(Display_Error)) |
| 1531 | Backup_Size = TWFunc::Get_Folder_Size(Backup_Path, Display_Error); |
Dees_Troy | 0550cfb | 2012-10-13 11:56:13 -0400 | [diff] [blame] | 1532 | else { |
| 1533 | if (!Was_Already_Mounted) |
| 1534 | UnMount(false); |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 1535 | return false; |
Dees_Troy | 0550cfb | 2012-10-13 11:56:13 -0400 | [diff] [blame] | 1536 | } |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1537 | } |
Dees_Troy | 0550cfb | 2012-10-13 11:56:13 -0400 | [diff] [blame] | 1538 | if (!Was_Already_Mounted) |
| 1539 | UnMount(false); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1540 | return true; |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 1541 | } |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1542 | |
| 1543 | void TWPartition::Find_Actual_Block_Device(void) { |
| 1544 | if (Is_Decrypted) { |
| 1545 | Actual_Block_Device = Decrypted_Block_Device; |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 1546 | if (TWFunc::Path_Exists(Primary_Block_Device)) |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1547 | Is_Present = true; |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 1548 | } else if (TWFunc::Path_Exists(Primary_Block_Device)) { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1549 | Is_Present = true; |
| 1550 | Actual_Block_Device = Primary_Block_Device; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1551 | return; |
| 1552 | } |
| 1553 | if (Is_Decrypted) { |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 1554 | } else if (!Alternate_Block_Device.empty() && TWFunc::Path_Exists(Alternate_Block_Device)) { |
Dees_Troy | 3f04d03 | 2012-10-07 18:20:09 -0400 | [diff] [blame] | 1555 | Actual_Block_Device = Alternate_Block_Device; |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1556 | Is_Present = true; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1557 | } else { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1558 | Is_Present = false; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1559 | } |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1560 | } |
| 1561 | |
| 1562 | void TWPartition::Recreate_Media_Folder(void) { |
| 1563 | string Command; |
| 1564 | |
| 1565 | if (!Mount(true)) { |
| 1566 | LOGE("Unable to recreate /data/media folder.\n"); |
Dees_Troy | b46a684 | 2012-09-25 11:06:46 -0400 | [diff] [blame] | 1567 | } else if (!TWFunc::Path_Exists("/data/media")) { |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1568 | PartitionManager.Mount_By_Path(Symlink_Mount_Point, true); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1569 | LOGI("Recreating /data/media folder.\n"); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1570 | mkdir("/data/media", S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); |
| 1571 | PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1572 | } |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 1573 | } |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 1574 | |
| 1575 | void TWPartition::Recreate_AndSec_Folder(void) { |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 1576 | if (!Has_Android_Secure) |
| 1577 | return; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1578 | LOGI("Creating .android_secure: %s\n", Symlink_Path.c_str()); |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 1579 | if (!Mount(true)) { |
| 1580 | LOGE("Unable to recreate android secure folder.\n"); |
| 1581 | } else if (!TWFunc::Path_Exists(Symlink_Path)) { |
| 1582 | LOGI("Recreating android secure folder.\n"); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1583 | PartitionManager.Mount_By_Path(Symlink_Mount_Point, true); |
| 1584 | mkdir(Symlink_Path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); |
| 1585 | PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true); |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 1586 | } |
| 1587 | } |