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