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