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