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