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