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