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