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