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