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