Dees Troy | 3be70a8 | 2013-10-22 14:25:12 +0000 | [diff] [blame] | 1 | /* |
Ethan Yonker | 472f506 | 2016-02-25 13:47:30 -0600 | [diff] [blame] | 2 | Copyright 2013 to 2016 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> |
that | f54e539 | 2016-01-29 22:04:43 +0100 | [diff] [blame] | 27 | #include <libgen.h> |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 28 | #include <zlib.h> |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 29 | #include <iostream> |
| 30 | #include <sstream> |
Ethan Yonker | 66a1949 | 2015-12-10 10:19:45 -0600 | [diff] [blame] | 31 | #include <sys/param.h> |
Ethan Yonker | 472f506 | 2016-02-25 13:47:30 -0600 | [diff] [blame] | 32 | #include <fcntl.h> |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 33 | |
Dees_Troy | 657c309 | 2012-09-10 20:32:10 -0400 | [diff] [blame] | 34 | #ifdef TW_INCLUDE_CRYPTO |
| 35 | #include "cutils/properties.h" |
| 36 | #endif |
| 37 | |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 38 | #include "libblkid/include/blkid.h" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 39 | #include "variables.h" |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 40 | #include "twcommon.h" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 41 | #include "partitions.hpp" |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 42 | #include "data.hpp" |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 43 | #include "twrp-functions.hpp" |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 44 | #include "twrpDigest.hpp" |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 45 | #include "twrpTar.hpp" |
Ethan Yonker | 3fdcda4 | 2016-11-30 12:29:37 -0600 | [diff] [blame] | 46 | #include "exclude.hpp" |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 47 | #include "infomanager.hpp" |
Ethan Yonker | 4b94cfd | 2014-12-11 10:00:45 -0600 | [diff] [blame] | 48 | #include "set_metadata.h" |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 49 | #include "gui/gui.hpp" |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 50 | #include "adbbu/libtwadbbu.hpp" |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 51 | extern "C" { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 52 | #include "mtdutils/mtdutils.h" |
| 53 | #include "mtdutils/mounts.h" |
Dees_Troy | a95f55c | 2013-08-17 13:14:43 +0000 | [diff] [blame] | 54 | #ifdef USE_EXT4 |
| 55 | #include "make_ext4fs.h" |
| 56 | #endif |
Ethan Yonker | 71413f4 | 2014-02-26 13:36:08 -0600 | [diff] [blame] | 57 | |
| 58 | #ifdef TW_INCLUDE_CRYPTO |
Ethan Yonker | 253368a | 2014-11-25 15:00:52 -0600 | [diff] [blame] | 59 | #include "crypto/lollipop/cryptfs.h" |
Ethan Yonker | 66a1949 | 2015-12-10 10:19:45 -0600 | [diff] [blame] | 60 | #include "gpt/gpt.h" |
Ethan Yonker | bd7492d | 2016-12-07 13:55:01 -0600 | [diff] [blame] | 61 | #ifdef TW_INCLUDE_FBE |
| 62 | #include "crypto/ext4crypt/Decrypt.h" |
Ethan Yonker | bd7492d | 2016-12-07 13:55:01 -0600 | [diff] [blame] | 63 | #endif |
Ethan Yonker | bc85b63 | 2015-08-09 12:48:14 -0500 | [diff] [blame] | 64 | #else |
| 65 | #define CRYPT_FOOTER_OFFSET 0x4000 |
Ethan Yonker | 71413f4 | 2014-02-26 13:36:08 -0600 | [diff] [blame] | 66 | #endif |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 67 | } |
bigbiff bigbiff | c49d706 | 2013-10-11 20:28:00 -0400 | [diff] [blame] | 68 | #ifdef HAVE_SELINUX |
| 69 | #include "selinux/selinux.h" |
Ethan Yonker | f27497f | 2014-02-09 11:48:33 -0600 | [diff] [blame] | 70 | #include <selinux/label.h> |
bigbiff bigbiff | c49d706 | 2013-10-11 20:28:00 -0400 | [diff] [blame] | 71 | #endif |
Dees Troy | 4159aed | 2014-02-28 17:24:43 +0000 | [diff] [blame] | 72 | #ifdef HAVE_CAPABILITIES |
| 73 | #include <sys/capability.h> |
| 74 | #include <sys/xattr.h> |
| 75 | #include <linux/xattr.h> |
| 76 | #endif |
HashBang | ed974bb | 2016-01-30 14:20:09 -0500 | [diff] [blame] | 77 | #include <sparse_format.h> |
Ethan Yonker | 472f506 | 2016-02-25 13:47:30 -0600 | [diff] [blame] | 78 | #include "progresstracking.hpp" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 79 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 80 | using namespace std; |
| 81 | |
Dees_Troy | a95f55c | 2013-08-17 13:14:43 +0000 | [diff] [blame] | 82 | extern struct selabel_handle *selinux_handle; |
Ethan Yonker | 6277c79 | 2014-09-15 14:54:30 -0500 | [diff] [blame] | 83 | extern bool datamedia; |
Dees_Troy | a95f55c | 2013-08-17 13:14:43 +0000 | [diff] [blame] | 84 | |
Hashcode | 62bd9e0 | 2013-11-19 21:59:42 -0800 | [diff] [blame] | 85 | struct flag_list { |
| 86 | const char *name; |
| 87 | unsigned flag; |
| 88 | }; |
| 89 | |
Matt Mower | 4ab42b1 | 2016-04-21 13:52:18 -0500 | [diff] [blame] | 90 | const struct flag_list mount_flags[] = { |
Hashcode | 62bd9e0 | 2013-11-19 21:59:42 -0800 | [diff] [blame] | 91 | { "noatime", MS_NOATIME }, |
| 92 | { "noexec", MS_NOEXEC }, |
| 93 | { "nosuid", MS_NOSUID }, |
| 94 | { "nodev", MS_NODEV }, |
| 95 | { "nodiratime", MS_NODIRATIME }, |
| 96 | { "ro", MS_RDONLY }, |
| 97 | { "rw", 0 }, |
| 98 | { "remount", MS_REMOUNT }, |
| 99 | { "bind", MS_BIND }, |
| 100 | { "rec", MS_REC }, |
Dees Troy | c4bc30e | 2014-02-03 15:04:19 +0000 | [diff] [blame] | 101 | #ifdef MS_UNBINDABLE |
Hashcode | 62bd9e0 | 2013-11-19 21:59:42 -0800 | [diff] [blame] | 102 | { "unbindable", MS_UNBINDABLE }, |
Dees Troy | c4bc30e | 2014-02-03 15:04:19 +0000 | [diff] [blame] | 103 | #endif |
| 104 | #ifdef MS_PRIVATE |
Hashcode | 62bd9e0 | 2013-11-19 21:59:42 -0800 | [diff] [blame] | 105 | { "private", MS_PRIVATE }, |
Dees Troy | c4bc30e | 2014-02-03 15:04:19 +0000 | [diff] [blame] | 106 | #endif |
| 107 | #ifdef MS_SLAVE |
Hashcode | 62bd9e0 | 2013-11-19 21:59:42 -0800 | [diff] [blame] | 108 | { "slave", MS_SLAVE }, |
Dees Troy | c4bc30e | 2014-02-03 15:04:19 +0000 | [diff] [blame] | 109 | #endif |
| 110 | #ifdef MS_SHARED |
Hashcode | 62bd9e0 | 2013-11-19 21:59:42 -0800 | [diff] [blame] | 111 | { "shared", MS_SHARED }, |
Dees Troy | c4bc30e | 2014-02-03 15:04:19 +0000 | [diff] [blame] | 112 | #endif |
Hashcode | 62bd9e0 | 2013-11-19 21:59:42 -0800 | [diff] [blame] | 113 | { "sync", MS_SYNCHRONOUS }, |
| 114 | { "defaults", 0 }, |
| 115 | { 0, 0 }, |
| 116 | }; |
| 117 | |
Matt Mower | 2416a50 | 2016-04-12 19:54:46 -0500 | [diff] [blame] | 118 | enum TW_FSTAB_FLAGS { |
| 119 | TWFLAG_DEFAULTS, // Retain position |
| 120 | TWFLAG_ANDSEC, |
| 121 | TWFLAG_BACKUP, |
| 122 | TWFLAG_BACKUPNAME, |
| 123 | TWFLAG_BLOCKSIZE, |
| 124 | TWFLAG_CANBEWIPED, |
| 125 | TWFLAG_CANENCRYPTBACKUP, |
| 126 | TWFLAG_DISPLAY, |
| 127 | TWFLAG_ENCRYPTABLE, |
| 128 | TWFLAG_FLASHIMG, |
| 129 | TWFLAG_FORCEENCRYPT, |
| 130 | TWFLAG_FSFLAGS, |
| 131 | TWFLAG_IGNOREBLKID, |
| 132 | TWFLAG_LENGTH, |
| 133 | TWFLAG_MOUNTTODECRYPT, |
| 134 | TWFLAG_REMOVABLE, |
| 135 | TWFLAG_RETAINLAYOUTVERSION, |
| 136 | TWFLAG_SETTINGSSTORAGE, |
| 137 | TWFLAG_STORAGE, |
| 138 | TWFLAG_STORAGENAME, |
| 139 | TWFLAG_SUBPARTITIONOF, |
| 140 | TWFLAG_SYMLINK, |
| 141 | TWFLAG_USERDATAENCRYPTBACKUP, |
| 142 | TWFLAG_USERMRF, |
| 143 | TWFLAG_WIPEDURINGFACTORYRESET, |
| 144 | TWFLAG_WIPEINGUI, |
Ethan Yonker | 1b19016 | 2016-12-05 15:25:19 -0600 | [diff] [blame] | 145 | TWFLAG_SLOTSELECT, |
Matt Mower | 2416a50 | 2016-04-12 19:54:46 -0500 | [diff] [blame] | 146 | }; |
| 147 | |
| 148 | /* Flags without a trailing '=' are considered dual format flags and can be |
| 149 | * written as either 'flagname' or 'flagname=', where the character following |
| 150 | * the '=' is Y,y,1 for true and false otherwise. |
| 151 | */ |
| 152 | const struct flag_list tw_flags[] = { |
| 153 | { "andsec", TWFLAG_ANDSEC }, |
| 154 | { "backup", TWFLAG_BACKUP }, |
| 155 | { "backupname=", TWFLAG_BACKUPNAME }, |
| 156 | { "blocksize=", TWFLAG_BLOCKSIZE }, |
| 157 | { "canbewiped", TWFLAG_CANBEWIPED }, |
| 158 | { "canencryptbackup", TWFLAG_CANENCRYPTBACKUP }, |
| 159 | { "defaults", TWFLAG_DEFAULTS }, |
| 160 | { "display=", TWFLAG_DISPLAY }, |
| 161 | { "encryptable=", TWFLAG_ENCRYPTABLE }, |
| 162 | { "flashimg", TWFLAG_FLASHIMG }, |
| 163 | { "forceencrypt=", TWFLAG_FORCEENCRYPT }, |
| 164 | { "fsflags=", TWFLAG_FSFLAGS }, |
| 165 | { "ignoreblkid", TWFLAG_IGNOREBLKID }, |
| 166 | { "length=", TWFLAG_LENGTH }, |
| 167 | { "mounttodecrypt", TWFLAG_MOUNTTODECRYPT }, |
| 168 | { "removable", TWFLAG_REMOVABLE }, |
| 169 | { "retainlayoutversion", TWFLAG_RETAINLAYOUTVERSION }, |
| 170 | { "settingsstorage", TWFLAG_SETTINGSSTORAGE }, |
| 171 | { "storage", TWFLAG_STORAGE }, |
| 172 | { "storagename=", TWFLAG_STORAGENAME }, |
| 173 | { "subpartitionof=", TWFLAG_SUBPARTITIONOF }, |
| 174 | { "symlink=", TWFLAG_SYMLINK }, |
| 175 | { "userdataencryptbackup", TWFLAG_USERDATAENCRYPTBACKUP }, |
| 176 | { "usermrf", TWFLAG_USERMRF }, |
| 177 | { "wipeduringfactoryreset", TWFLAG_WIPEDURINGFACTORYRESET }, |
| 178 | { "wipeingui", TWFLAG_WIPEINGUI }, |
Ethan Yonker | 1b19016 | 2016-12-05 15:25:19 -0600 | [diff] [blame] | 179 | { "slotselect", TWFLAG_SLOTSELECT }, |
Matt Mower | 2416a50 | 2016-04-12 19:54:46 -0500 | [diff] [blame] | 180 | { 0, 0 }, |
| 181 | }; |
| 182 | |
that | 9e0593e | 2014-10-08 00:01:24 +0200 | [diff] [blame] | 183 | TWPartition::TWPartition() { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 184 | Can_Be_Mounted = false; |
| 185 | Can_Be_Wiped = false; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 186 | Can_Be_Backed_Up = false; |
Vojtech Bocek | 1dc3098 | 2013-08-30 21:49:30 +0200 | [diff] [blame] | 187 | Use_Rm_Rf = false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 188 | Wipe_During_Factory_Reset = false; |
| 189 | Wipe_Available_in_GUI = false; |
| 190 | Is_SubPartition = false; |
Dees_Troy | 2691f9d | 2012-09-24 11:15:49 -0400 | [diff] [blame] | 191 | Has_SubPartition = false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 192 | SubPartition_Of = ""; |
| 193 | Symlink_Path = ""; |
| 194 | Symlink_Mount_Point = ""; |
| 195 | Mount_Point = ""; |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 196 | Backup_Path = ""; |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 197 | Actual_Block_Device = ""; |
| 198 | Primary_Block_Device = ""; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 199 | Alternate_Block_Device = ""; |
| 200 | Removable = false; |
| 201 | Is_Present = false; |
| 202 | Length = 0; |
| 203 | Size = 0; |
| 204 | Used = 0; |
| 205 | Free = 0; |
| 206 | Backup_Size = 0; |
| 207 | Can_Be_Encrypted = false; |
| 208 | Is_Encrypted = false; |
| 209 | Is_Decrypted = false; |
Ethan Yonker | bd7492d | 2016-12-07 13:55:01 -0600 | [diff] [blame] | 210 | Is_FBE = false; |
Ethan Yonker | 253368a | 2014-11-25 15:00:52 -0600 | [diff] [blame] | 211 | Mount_To_Decrypt = false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 212 | Decrypted_Block_Device = ""; |
| 213 | Display_Name = ""; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 214 | Backup_Display_Name = ""; |
| 215 | Storage_Name = ""; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 216 | Backup_Name = ""; |
Dees_Troy | 63c8df7 | 2012-09-10 14:02:05 -0400 | [diff] [blame] | 217 | Backup_FileName = ""; |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 218 | MTD_Name = ""; |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 219 | Backup_Method = BM_NONE; |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 220 | Can_Encrypt_Backup = false; |
| 221 | Use_Userdata_Encryption = false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 222 | Has_Data_Media = false; |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 223 | Has_Android_Secure = false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 224 | Is_Storage = false; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 225 | Is_Settings_Storage = false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 226 | Storage_Path = ""; |
| 227 | Current_File_System = ""; |
| 228 | Fstab_File_System = ""; |
Hashcode | 62bd9e0 | 2013-11-19 21:59:42 -0800 | [diff] [blame] | 229 | Mount_Flags = 0; |
| 230 | Mount_Options = ""; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 231 | Format_Block_Size = 0; |
Dees_Troy | 68cab49 | 2012-12-12 19:29:35 +0000 | [diff] [blame] | 232 | Ignore_Blkid = false; |
Dees_Troy | 16c2b31 | 2013-01-15 16:51:18 +0000 | [diff] [blame] | 233 | Retain_Layout_Version = false; |
Ethan Yonker | 253368a | 2014-11-25 15:00:52 -0600 | [diff] [blame] | 234 | Crypto_Key_Location = "footer"; |
Ethan Yonker | 726a020 | 2014-12-16 20:01:38 -0600 | [diff] [blame] | 235 | MTP_Storage_ID = 0; |
Ethan Yonker | 96af84a | 2015-01-05 14:58:36 -0600 | [diff] [blame] | 236 | Can_Flash_Img = false; |
Ethan Yonker | eb32b1f | 2015-05-18 10:23:03 -0500 | [diff] [blame] | 237 | Mount_Read_Only = false; |
Ethan Yonker | 66a1949 | 2015-12-10 10:19:45 -0600 | [diff] [blame] | 238 | Is_Adopted_Storage = false; |
| 239 | Adopted_GUID = ""; |
Ethan Yonker | 1b19016 | 2016-12-05 15:25:19 -0600 | [diff] [blame] | 240 | SlotSelect = false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | TWPartition::~TWPartition(void) { |
| 244 | // Do nothing |
| 245 | } |
| 246 | |
Matt Mower | 2b2dd15 | 2016-04-26 11:24:08 -0500 | [diff] [blame] | 247 | bool TWPartition::Process_Fstab_Line(const char *fstab_line, bool Display_Error) { |
| 248 | char full_line[MAX_FSTAB_LINE_LENGTH]; |
Matt Mower | 2416a50 | 2016-04-12 19:54:46 -0500 | [diff] [blame] | 249 | char twflags[MAX_FSTAB_LINE_LENGTH] = ""; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 250 | char* ptr; |
Matt Mower | 2b2dd15 | 2016-04-26 11:24:08 -0500 | [diff] [blame] | 251 | int line_len = strlen(fstab_line), index = 0, item_index = 0; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 252 | bool skip = false; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 253 | |
Matt Mower | 2b2dd15 | 2016-04-26 11:24:08 -0500 | [diff] [blame] | 254 | strlcpy(full_line, fstab_line, sizeof(full_line)); |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 255 | for (index = 0; index < line_len; index++) { |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 256 | if (full_line[index] == 34) |
| 257 | skip = !skip; |
| 258 | if (!skip && full_line[index] <= 32) |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 259 | full_line[index] = '\0'; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 260 | } |
Dees_Troy | 7c2dec8 | 2012-09-26 09:49:14 -0400 | [diff] [blame] | 261 | Mount_Point = full_line; |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 262 | LOGINFO("Processing '%s'\n", Mount_Point.c_str()); |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 263 | Backup_Path = Mount_Point; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 264 | Storage_Path = Mount_Point; |
Dees_Troy | 70737fa | 2013-04-08 13:19:20 +0000 | [diff] [blame] | 265 | Display_Name = full_line + 1; |
| 266 | Backup_Display_Name = Display_Name; |
| 267 | Storage_Name = Display_Name; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 268 | index = Mount_Point.size(); |
| 269 | while (index < line_len) { |
| 270 | while (index < line_len && full_line[index] == '\0') |
| 271 | index++; |
| 272 | if (index >= line_len) |
| 273 | continue; |
| 274 | ptr = full_line + index; |
| 275 | if (item_index == 0) { |
| 276 | // File System |
| 277 | Fstab_File_System = ptr; |
| 278 | Current_File_System = ptr; |
| 279 | item_index++; |
| 280 | } else if (item_index == 1) { |
| 281 | // Primary Block Device |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 282 | if (Fstab_File_System == "mtd" || Fstab_File_System == "yaffs2") { |
Dees_Troy | 094207a | 2012-09-26 12:00:39 -0400 | [diff] [blame] | 283 | MTD_Name = ptr; |
| 284 | Find_MTD_Block_Device(MTD_Name); |
Dees_Troy | 5fcd8f9 | 2012-10-16 12:22:05 -0400 | [diff] [blame] | 285 | } else if (Fstab_File_System == "bml") { |
| 286 | if (Mount_Point == "/boot") |
| 287 | MTD_Name = "boot"; |
| 288 | else if (Mount_Point == "/recovery") |
| 289 | MTD_Name = "recovery"; |
| 290 | Primary_Block_Device = ptr; |
| 291 | if (*ptr != '/') |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 292 | 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] | 293 | } else if (*ptr != '/') { |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 294 | if (Display_Error) |
Matt Mower | 2b2dd15 | 2016-04-26 11:24:08 -0500 | [diff] [blame] | 295 | LOGERR("Invalid block device '%s' in fstab line '%s'", ptr, fstab_line); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 296 | else |
Matt Mower | 2b2dd15 | 2016-04-26 11:24:08 -0500 | [diff] [blame] | 297 | LOGINFO("Invalid block device '%s' in fstab line '%s'", ptr, fstab_line); |
Matt Mower | 72c87ce | 2016-04-26 14:34:56 -0500 | [diff] [blame] | 298 | return false; |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 299 | } else { |
| 300 | Primary_Block_Device = ptr; |
| 301 | Find_Real_Block_Device(Primary_Block_Device, Display_Error); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 302 | } |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 303 | item_index++; |
| 304 | } else if (item_index > 1) { |
| 305 | if (*ptr == '/') { |
| 306 | // Alternate Block Device |
| 307 | Alternate_Block_Device = ptr; |
| 308 | Find_Real_Block_Device(Alternate_Block_Device, Display_Error); |
| 309 | } else if (strlen(ptr) > 7 && strncmp(ptr, "length=", 7) == 0) { |
| 310 | // Partition length |
| 311 | ptr += 7; |
| 312 | Length = atoi(ptr); |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 313 | } else if (strlen(ptr) > 6 && strncmp(ptr, "flags=", 6) == 0) { |
| 314 | // Custom flags, save for later so that new values aren't overwritten by defaults |
| 315 | ptr += 6; |
Matt Mower | 2416a50 | 2016-04-12 19:54:46 -0500 | [diff] [blame] | 316 | strlcpy(twflags, ptr, sizeof(twflags)); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 317 | } else if (strlen(ptr) == 4 && (strncmp(ptr, "NULL", 4) == 0 || strncmp(ptr, "null", 4) == 0 || strncmp(ptr, "null", 4) == 0)) { |
| 318 | // Do nothing |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 319 | } else { |
| 320 | // Unhandled data |
Matt Mower | 2b2dd15 | 2016-04-26 11:24:08 -0500 | [diff] [blame] | 321 | LOGINFO("Unhandled fstab information '%s' in fstab line '%s'\n", ptr, fstab_line); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 322 | } |
| 323 | } |
| 324 | while (index < line_len && full_line[index] != '\0') |
| 325 | index++; |
| 326 | } |
| 327 | |
| 328 | if (!Is_File_System(Fstab_File_System) && !Is_Image(Fstab_File_System)) { |
| 329 | if (Display_Error) |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 330 | LOGERR("Unknown File System: '%s'\n", Fstab_File_System.c_str()); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 331 | else |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 332 | LOGINFO("Unknown File System: '%s'\n", Fstab_File_System.c_str()); |
Matt Mower | 72c87ce | 2016-04-26 14:34:56 -0500 | [diff] [blame] | 333 | return false; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 334 | } else if (Is_File_System(Fstab_File_System)) { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 335 | Find_Actual_Block_Device(); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 336 | Setup_File_System(Display_Error); |
| 337 | if (Mount_Point == "/system") { |
| 338 | Display_Name = "System"; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 339 | Backup_Display_Name = Display_Name; |
| 340 | Storage_Name = Display_Name; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 341 | Wipe_Available_in_GUI = true; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 342 | Can_Be_Backed_Up = true; |
Ethan Yonker | eb32b1f | 2015-05-18 10:23:03 -0500 | [diff] [blame] | 343 | Mount_Read_Only = true; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 344 | } else if (Mount_Point == "/data") { |
| 345 | Display_Name = "Data"; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 346 | Backup_Display_Name = Display_Name; |
| 347 | Storage_Name = Display_Name; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 348 | Wipe_Available_in_GUI = true; |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 349 | Wipe_During_Factory_Reset = true; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 350 | Can_Be_Backed_Up = true; |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 351 | Can_Encrypt_Backup = true; |
| 352 | Use_Userdata_Encryption = true; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 353 | } else if (Mount_Point == "/cache") { |
| 354 | Display_Name = "Cache"; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 355 | Backup_Display_Name = Display_Name; |
| 356 | Storage_Name = Display_Name; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 357 | Wipe_Available_in_GUI = true; |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 358 | Wipe_During_Factory_Reset = true; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 359 | Can_Be_Backed_Up = true; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 360 | } else if (Mount_Point == "/datadata") { |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 361 | Wipe_During_Factory_Reset = true; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 362 | Display_Name = "DataData"; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 363 | Backup_Display_Name = Display_Name; |
| 364 | Storage_Name = Display_Name; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 365 | Is_SubPartition = true; |
| 366 | SubPartition_Of = "/data"; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 367 | DataManager::SetValue(TW_HAS_DATADATA, 1); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 368 | Can_Be_Backed_Up = true; |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 369 | Can_Encrypt_Backup = true; |
| 370 | Use_Userdata_Encryption = false; // This whole partition should be encrypted |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 371 | } else if (Mount_Point == "/sd-ext") { |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 372 | Wipe_During_Factory_Reset = true; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 373 | Display_Name = "SD-Ext"; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 374 | Backup_Display_Name = Display_Name; |
| 375 | Storage_Name = Display_Name; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 376 | Wipe_Available_in_GUI = true; |
Dees_Troy | c51f1f9 | 2012-09-20 15:32:13 -0400 | [diff] [blame] | 377 | Removable = true; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 378 | Can_Be_Backed_Up = true; |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 379 | Can_Encrypt_Backup = true; |
| 380 | Use_Userdata_Encryption = true; |
Dees_Troy | 2c50e18 | 2012-09-26 20:05:28 -0400 | [diff] [blame] | 381 | } else if (Mount_Point == "/boot") { |
| 382 | Display_Name = "Boot"; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 383 | Backup_Display_Name = Display_Name; |
Dees_Troy | 2c50e18 | 2012-09-26 20:05:28 -0400 | [diff] [blame] | 384 | DataManager::SetValue("tw_boot_is_mountable", 1); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 385 | Can_Be_Backed_Up = true; |
Ethan Yonker | 1673e3d | 2015-10-26 21:51:58 -0500 | [diff] [blame] | 386 | } else if (Mount_Point == "/vendor") { |
| 387 | Display_Name = "Vendor"; |
| 388 | Backup_Display_Name = Display_Name; |
| 389 | Storage_Name = Display_Name; |
| 390 | Mount_Read_Only = true; |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 391 | } |
| 392 | #ifdef TW_EXTERNAL_STORAGE_PATH |
| 393 | if (Mount_Point == EXPAND(TW_EXTERNAL_STORAGE_PATH)) { |
| 394 | Is_Storage = true; |
| 395 | Storage_Path = EXPAND(TW_EXTERNAL_STORAGE_PATH); |
Dees_Troy | c51f1f9 | 2012-09-20 15:32:13 -0400 | [diff] [blame] | 396 | Removable = true; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 397 | Wipe_Available_in_GUI = true; |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 398 | #else |
Dees_Troy | 70737fa | 2013-04-08 13:19:20 +0000 | [diff] [blame] | 399 | if (Mount_Point == "/sdcard" || Mount_Point == "/external_sd" || Mount_Point == "/external_sdcard") { |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 400 | Is_Storage = true; |
Dees_Troy | c51f1f9 | 2012-09-20 15:32:13 -0400 | [diff] [blame] | 401 | Removable = true; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 402 | Wipe_Available_in_GUI = true; |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 403 | #endif |
Dees_Troy | b05ddee | 2013-01-28 20:24:50 +0000 | [diff] [blame] | 404 | } |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 405 | #ifdef TW_INTERNAL_STORAGE_PATH |
| 406 | if (Mount_Point == EXPAND(TW_INTERNAL_STORAGE_PATH)) { |
| 407 | Is_Storage = true; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 408 | Is_Settings_Storage = true; |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 409 | Storage_Path = EXPAND(TW_INTERNAL_STORAGE_PATH); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 410 | Wipe_Available_in_GUI = true; |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 411 | } |
| 412 | #else |
Dees_Troy | 70737fa | 2013-04-08 13:19:20 +0000 | [diff] [blame] | 413 | if (Mount_Point == "/emmc" || Mount_Point == "/internal_sd" || Mount_Point == "/internal_sdcard") { |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 414 | Is_Storage = true; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 415 | Is_Settings_Storage = true; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 416 | Wipe_Available_in_GUI = true; |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 417 | } |
| 418 | #endif |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 419 | } else if (Is_Image(Fstab_File_System)) { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 420 | Find_Actual_Block_Device(); |
Ethan Yonker | 1b19016 | 2016-12-05 15:25:19 -0600 | [diff] [blame] | 421 | Setup_Image(); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 422 | if (Mount_Point == "/boot") { |
| 423 | Display_Name = "Boot"; |
| 424 | Backup_Display_Name = Display_Name; |
| 425 | Can_Be_Backed_Up = true; |
Ethan Yonker | 96af84a | 2015-01-05 14:58:36 -0600 | [diff] [blame] | 426 | Can_Flash_Img = true; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 427 | } else if (Mount_Point == "/recovery") { |
| 428 | Display_Name = "Recovery"; |
| 429 | Backup_Display_Name = Display_Name; |
Ethan Yonker | 96af84a | 2015-01-05 14:58:36 -0600 | [diff] [blame] | 430 | Can_Flash_Img = true; |
Ethan Yonker | eb32b1f | 2015-05-18 10:23:03 -0500 | [diff] [blame] | 431 | } else if (Mount_Point == "/system_image") { |
| 432 | Display_Name = "System Image"; |
| 433 | Backup_Display_Name = Display_Name; |
HashBang | ed974bb | 2016-01-30 14:20:09 -0500 | [diff] [blame] | 434 | Can_Flash_Img = true; |
Ethan Yonker | eb32b1f | 2015-05-18 10:23:03 -0500 | [diff] [blame] | 435 | Can_Be_Backed_Up = true; |
Ethan Yonker | 1673e3d | 2015-10-26 21:51:58 -0500 | [diff] [blame] | 436 | } else if (Mount_Point == "/vendor_image") { |
| 437 | Display_Name = "Vendor Image"; |
| 438 | Backup_Display_Name = Display_Name; |
HashBang | ed974bb | 2016-01-30 14:20:09 -0500 | [diff] [blame] | 439 | Can_Flash_Img = true; |
Ethan Yonker | 1673e3d | 2015-10-26 21:51:58 -0500 | [diff] [blame] | 440 | Can_Be_Backed_Up = true; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 441 | } |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 442 | } |
| 443 | |
Matt Mower | 2416a50 | 2016-04-12 19:54:46 -0500 | [diff] [blame] | 444 | // Process TWRP fstab flags |
| 445 | if (strlen(twflags) > 0) { |
| 446 | string Prev_Display_Name = Display_Name; |
| 447 | string Prev_Storage_Name = Storage_Name; |
| 448 | string Prev_Backup_Display_Name = Backup_Display_Name; |
| 449 | Display_Name = ""; |
| 450 | Storage_Name = ""; |
| 451 | Backup_Display_Name = ""; |
| 452 | |
| 453 | Process_TW_Flags(twflags, Display_Error); |
| 454 | |
| 455 | bool has_display_name = !Display_Name.empty(); |
| 456 | bool has_storage_name = !Storage_Name.empty(); |
| 457 | bool has_backup_name = !Backup_Display_Name.empty(); |
| 458 | if (!has_display_name) Display_Name = Prev_Display_Name; |
| 459 | if (!has_storage_name) Storage_Name = Prev_Storage_Name; |
| 460 | if (!has_backup_name) Backup_Display_Name = Prev_Backup_Display_Name; |
| 461 | |
| 462 | if (has_display_name && !has_storage_name) |
| 463 | Storage_Name = Display_Name; |
| 464 | if (!has_display_name && has_storage_name) |
| 465 | Display_Name = Storage_Name; |
| 466 | if (has_display_name && !has_backup_name && Backup_Display_Name != "Android Secure") |
| 467 | Backup_Display_Name = Display_Name; |
| 468 | if (!has_display_name && has_backup_name) |
| 469 | Display_Name = Backup_Display_Name; |
| 470 | } |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 471 | return true; |
| 472 | } |
| 473 | |
Matt Mower | 72c87ce | 2016-04-26 14:34:56 -0500 | [diff] [blame] | 474 | void TWPartition::Partition_Post_Processing(bool Display_Error) { |
| 475 | if (Mount_Point == "/data") |
| 476 | Setup_Data_Partition(Display_Error); |
| 477 | else if (Mount_Point == "/cache") |
| 478 | Setup_Cache_Partition(Display_Error); |
| 479 | } |
| 480 | |
Ethan Yonker | bd7492d | 2016-12-07 13:55:01 -0600 | [diff] [blame] | 481 | void TWPartition::ExcludeAll(const string& path) { |
| 482 | backup_exclusions.add_absolute_dir(path); |
| 483 | wipe_exclusions.add_absolute_dir(path); |
| 484 | } |
| 485 | |
Matt Mower | 72c87ce | 2016-04-26 14:34:56 -0500 | [diff] [blame] | 486 | void TWPartition::Setup_Data_Partition(bool Display_Error) { |
| 487 | if (Mount_Point != "/data") |
| 488 | return; |
| 489 | |
| 490 | // Ensure /data is not mounted as tmpfs for qcom hardware decrypt |
| 491 | UnMount(false); |
| 492 | |
| 493 | #ifdef TW_INCLUDE_CRYPTO |
| 494 | if (datamedia) |
| 495 | Setup_Data_Media(); |
| 496 | Can_Be_Encrypted = true; |
| 497 | char crypto_blkdev[255]; |
| 498 | property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error"); |
| 499 | if (strcmp(crypto_blkdev, "error") != 0) { |
| 500 | DataManager::SetValue(TW_IS_DECRYPTED, 1); |
| 501 | Is_Encrypted = true; |
| 502 | Is_Decrypted = true; |
Ethan Yonker | bd7492d | 2016-12-07 13:55:01 -0600 | [diff] [blame] | 503 | Is_FBE = false; |
| 504 | DataManager::SetValue(TW_IS_FBE, 0); |
Matt Mower | 72c87ce | 2016-04-26 14:34:56 -0500 | [diff] [blame] | 505 | Decrypted_Block_Device = crypto_blkdev; |
| 506 | LOGINFO("Data already decrypted, new block device: '%s'\n", crypto_blkdev); |
| 507 | } else if (!Mount(false)) { |
| 508 | if (Is_Present) { |
| 509 | set_partition_data(Actual_Block_Device.c_str(), Crypto_Key_Location.c_str(), Fstab_File_System.c_str()); |
| 510 | if (cryptfs_check_footer() == 0) { |
| 511 | Is_Encrypted = true; |
| 512 | Is_Decrypted = false; |
| 513 | Can_Be_Mounted = false; |
| 514 | Current_File_System = "emmc"; |
Ethan Yonker | 1b19016 | 2016-12-05 15:25:19 -0600 | [diff] [blame] | 515 | Setup_Image(); |
Matt Mower | 72c87ce | 2016-04-26 14:34:56 -0500 | [diff] [blame] | 516 | DataManager::SetValue(TW_IS_ENCRYPTED, 1); |
| 517 | DataManager::SetValue(TW_CRYPTO_PWTYPE, cryptfs_get_password_type()); |
| 518 | DataManager::SetValue(TW_CRYPTO_PASSWORD, ""); |
| 519 | DataManager::SetValue("tw_crypto_display", ""); |
| 520 | } else { |
| 521 | gui_err("mount_data_footer=Could not mount /data and unable to find crypto footer."); |
| 522 | } |
| 523 | } else { |
| 524 | LOGERR("Primary block device '%s' for mount point '%s' is not present!\n", Primary_Block_Device.c_str(), Mount_Point.c_str()); |
| 525 | } |
| 526 | } else { |
Ethan Yonker | bd7492d | 2016-12-07 13:55:01 -0600 | [diff] [blame] | 527 | if (TWFunc::Path_Exists("/data/unencrypted/key/version")) { |
| 528 | LOGINFO("File Based Encryption is present\n"); |
| 529 | #ifdef TW_INCLUDE_FBE |
| 530 | ExcludeAll(Mount_Point + "/convert_fbe"); |
| 531 | ExcludeAll(Mount_Point + "/unencrypted"); |
| 532 | //ExcludeAll(Mount_Point + "/system/users/0"); // we WILL need to retain some of this if multiple users are present or we just need to delete more folders for the extra users somewhere else |
| 533 | ExcludeAll(Mount_Point + "/misc/vold/user_keys"); |
Ethan Yonker | 79f88bd | 2016-12-09 14:52:12 -0600 | [diff] [blame] | 534 | //ExcludeAll(Mount_Point + "/system_ce"); |
| 535 | //ExcludeAll(Mount_Point + "/system_de"); |
| 536 | //ExcludeAll(Mount_Point + "/misc_ce"); |
| 537 | //ExcludeAll(Mount_Point + "/misc_de"); |
Ethan Yonker | bd7492d | 2016-12-07 13:55:01 -0600 | [diff] [blame] | 538 | ExcludeAll(Mount_Point + "/system/gatekeeper.password.key"); |
| 539 | ExcludeAll(Mount_Point + "/system/gatekeeper.pattern.key"); |
| 540 | ExcludeAll(Mount_Point + "/system/locksettings.db"); |
| 541 | //ExcludeAll(Mount_Point + "/system/locksettings.db-shm"); // don't seem to need this one, but the other 2 are needed |
| 542 | ExcludeAll(Mount_Point + "/system/locksettings.db-wal"); |
Ethan Yonker | 79f88bd | 2016-12-09 14:52:12 -0600 | [diff] [blame] | 543 | //ExcludeAll(Mount_Point + "/user_de"); |
Ethan Yonker | bd7492d | 2016-12-07 13:55:01 -0600 | [diff] [blame] | 544 | //ExcludeAll(Mount_Point + "/misc/profiles/cur/0"); // might be important later |
| 545 | ExcludeAll(Mount_Point + "/misc/gatekeeper"); |
| 546 | ExcludeAll(Mount_Point + "/drm/kek.dat"); |
| 547 | int retry_count = 3; |
| 548 | while (!Decrypt_DE() && --retry_count) |
| 549 | usleep(2000); |
| 550 | if (retry_count > 0) { |
| 551 | property_set("ro.crypto.state", "encrypted"); |
| 552 | Is_Encrypted = true; |
| 553 | Is_Decrypted = false; |
| 554 | Is_FBE = true; |
| 555 | DataManager::SetValue(TW_IS_FBE, 1); |
| 556 | DataManager::SetValue(TW_IS_ENCRYPTED, 1); |
| 557 | string filename; |
| 558 | DataManager::SetValue(TW_CRYPTO_PWTYPE, Get_Password_Type(0, filename)); |
| 559 | DataManager::SetValue(TW_CRYPTO_PASSWORD, ""); |
| 560 | DataManager::SetValue("tw_crypto_display", ""); |
| 561 | } |
| 562 | #else |
| 563 | LOGERR("FBE found but FBE support not present in TWRP\n"); |
| 564 | #endif |
| 565 | } else { |
| 566 | // Filesystem is not encrypted and the mount succeeded, so return to |
| 567 | // the original unmounted state |
| 568 | UnMount(false); |
| 569 | } |
Matt Mower | 72c87ce | 2016-04-26 14:34:56 -0500 | [diff] [blame] | 570 | } |
| 571 | if (datamedia && (!Is_Encrypted || (Is_Encrypted && Is_Decrypted))) { |
| 572 | Setup_Data_Media(); |
| 573 | Recreate_Media_Folder(); |
| 574 | } |
| 575 | #else |
| 576 | if (datamedia) { |
| 577 | Setup_Data_Media(); |
| 578 | Recreate_Media_Folder(); |
| 579 | } |
| 580 | #endif |
| 581 | } |
| 582 | |
| 583 | void TWPartition::Setup_Cache_Partition(bool Display_Error __unused) { |
| 584 | if (Mount_Point != "/cache") |
| 585 | return; |
| 586 | |
| 587 | if (!Mount(true)) |
| 588 | return; |
| 589 | |
| 590 | if (!TWFunc::Path_Exists("/cache/recovery/.")) { |
| 591 | LOGINFO("Recreating /cache/recovery folder\n"); |
| 592 | if (mkdir("/cache/recovery", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0) |
| 593 | LOGERR("Could not create /cache/recovery\n"); |
| 594 | } |
| 595 | } |
| 596 | |
Matt Mower | 4ab42b1 | 2016-04-21 13:52:18 -0500 | [diff] [blame] | 597 | void TWPartition::Process_FS_Flags(const char *str) { |
| 598 | char *options = strdup(str); |
| 599 | char *ptr, *savep; |
Hashcode | 62bd9e0 | 2013-11-19 21:59:42 -0800 | [diff] [blame] | 600 | |
Matt Mower | 4ab42b1 | 2016-04-21 13:52:18 -0500 | [diff] [blame] | 601 | Mount_Options = ""; |
Hashcode | 62bd9e0 | 2013-11-19 21:59:42 -0800 | [diff] [blame] | 602 | |
Matt Mower | 4ab42b1 | 2016-04-21 13:52:18 -0500 | [diff] [blame] | 603 | // Avoid issues with potentially nested strtok by using strtok_r |
| 604 | ptr = strtok_r(options, ",", &savep); |
| 605 | while (ptr) { |
| 606 | const struct flag_list* mount_flag = mount_flags; |
| 607 | |
| 608 | for (; mount_flag->name; mount_flag++) { |
| 609 | // mount_flags are never postfixed by '=', |
| 610 | // so only match identical strings (including length) |
| 611 | if (strcmp(ptr, mount_flag->name) == 0) { |
| 612 | Mount_Flags |= mount_flag->flag; |
Hashcode | 62bd9e0 | 2013-11-19 21:59:42 -0800 | [diff] [blame] | 613 | break; |
| 614 | } |
| 615 | } |
| 616 | |
Matt Mower | 4ab42b1 | 2016-04-21 13:52:18 -0500 | [diff] [blame] | 617 | if (mount_flag->flag == MS_RDONLY) |
| 618 | Mount_Read_Only = true; |
Hashcode | 62bd9e0 | 2013-11-19 21:59:42 -0800 | [diff] [blame] | 619 | |
Matt Mower | 4ab42b1 | 2016-04-21 13:52:18 -0500 | [diff] [blame] | 620 | if (mount_flag->name != 0) { |
| 621 | if (!Mount_Options.empty()) |
| 622 | Mount_Options += ","; |
| 623 | Mount_Options += mount_flag->name; |
| 624 | } else { |
| 625 | LOGINFO("Unhandled mount flag: '%s'\n", ptr); |
| 626 | } |
| 627 | |
| 628 | ptr = strtok_r(NULL, ",", &savep); |
| 629 | } |
| 630 | free(options); |
Hashcode | 62bd9e0 | 2013-11-19 21:59:42 -0800 | [diff] [blame] | 631 | } |
| 632 | |
Matt Mower | 2416a50 | 2016-04-12 19:54:46 -0500 | [diff] [blame] | 633 | void TWPartition::Apply_TW_Flag(const unsigned flag, const char* str, const bool val) { |
| 634 | switch (flag) { |
| 635 | case TWFLAG_ANDSEC: |
| 636 | Has_Android_Secure = val; |
| 637 | break; |
| 638 | case TWFLAG_BACKUP: |
| 639 | Can_Be_Backed_Up = val; |
| 640 | break; |
| 641 | case TWFLAG_BACKUPNAME: |
| 642 | Backup_Display_Name = str; |
| 643 | break; |
| 644 | case TWFLAG_BLOCKSIZE: |
| 645 | Format_Block_Size = (unsigned long)(atol(str)); |
| 646 | break; |
| 647 | case TWFLAG_CANBEWIPED: |
| 648 | Can_Be_Wiped = val; |
| 649 | break; |
| 650 | case TWFLAG_CANENCRYPTBACKUP: |
| 651 | Can_Encrypt_Backup = val; |
| 652 | break; |
| 653 | case TWFLAG_DEFAULTS: |
| 654 | // Do nothing |
| 655 | break; |
| 656 | case TWFLAG_DISPLAY: |
| 657 | Display_Name = str; |
| 658 | break; |
| 659 | case TWFLAG_ENCRYPTABLE: |
| 660 | case TWFLAG_FORCEENCRYPT: |
| 661 | Crypto_Key_Location = str; |
| 662 | break; |
| 663 | case TWFLAG_FLASHIMG: |
| 664 | Can_Flash_Img = val; |
| 665 | break; |
| 666 | case TWFLAG_FSFLAGS: |
Matt Mower | 4ab42b1 | 2016-04-21 13:52:18 -0500 | [diff] [blame] | 667 | Process_FS_Flags(str); |
Matt Mower | 2416a50 | 2016-04-12 19:54:46 -0500 | [diff] [blame] | 668 | break; |
| 669 | case TWFLAG_IGNOREBLKID: |
| 670 | Ignore_Blkid = val; |
| 671 | break; |
| 672 | case TWFLAG_LENGTH: |
| 673 | Length = atoi(str); |
| 674 | break; |
| 675 | case TWFLAG_MOUNTTODECRYPT: |
| 676 | Mount_To_Decrypt = val; |
| 677 | break; |
| 678 | case TWFLAG_REMOVABLE: |
| 679 | Removable = val; |
| 680 | break; |
| 681 | case TWFLAG_RETAINLAYOUTVERSION: |
| 682 | Retain_Layout_Version = val; |
| 683 | break; |
| 684 | case TWFLAG_SETTINGSSTORAGE: |
| 685 | Is_Settings_Storage = val; |
| 686 | if (Is_Settings_Storage) |
| 687 | Is_Storage = true; |
| 688 | break; |
| 689 | case TWFLAG_STORAGE: |
| 690 | Is_Storage = val; |
| 691 | break; |
| 692 | case TWFLAG_STORAGENAME: |
| 693 | Storage_Name = str; |
| 694 | break; |
| 695 | case TWFLAG_SUBPARTITIONOF: |
| 696 | Is_SubPartition = true; |
| 697 | SubPartition_Of = str; |
| 698 | break; |
| 699 | case TWFLAG_SYMLINK: |
| 700 | Symlink_Path = str; |
| 701 | break; |
| 702 | case TWFLAG_USERDATAENCRYPTBACKUP: |
| 703 | Use_Userdata_Encryption = val; |
| 704 | if (Use_Userdata_Encryption) |
| 705 | Can_Encrypt_Backup = true; |
| 706 | break; |
| 707 | case TWFLAG_USERMRF: |
| 708 | Use_Rm_Rf = val; |
| 709 | break; |
| 710 | case TWFLAG_WIPEDURINGFACTORYRESET: |
| 711 | Wipe_During_Factory_Reset = val; |
| 712 | if (Wipe_During_Factory_Reset) { |
| 713 | Can_Be_Wiped = true; |
| 714 | Wipe_Available_in_GUI = true; |
| 715 | } |
| 716 | break; |
| 717 | case TWFLAG_WIPEINGUI: |
| 718 | Wipe_Available_in_GUI = val; |
| 719 | if (Wipe_Available_in_GUI) |
| 720 | Can_Be_Wiped = true; |
| 721 | break; |
Ethan Yonker | 1b19016 | 2016-12-05 15:25:19 -0600 | [diff] [blame] | 722 | case TWFLAG_SLOTSELECT: |
| 723 | SlotSelect = true; |
| 724 | break; |
Matt Mower | 2416a50 | 2016-04-12 19:54:46 -0500 | [diff] [blame] | 725 | default: |
| 726 | // Should not get here |
| 727 | LOGINFO("Flag identified for processing, but later unmatched: %i\n", flag); |
| 728 | break; |
| 729 | } |
| 730 | } |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 731 | |
Matt Mower | 2416a50 | 2016-04-12 19:54:46 -0500 | [diff] [blame] | 732 | void TWPartition::Process_TW_Flags(char *flags, bool Display_Error) { |
| 733 | char separator[2] = {'\n', 0}; |
| 734 | char *ptr, *savep; |
| 735 | |
| 736 | // Semicolons within double-quotes are not forbidden, so replace |
| 737 | // only the semicolons intended as separators with '\n' for strtok |
| 738 | for (unsigned i = 0, skip = 0; i < strlen(flags); i++) { |
| 739 | if (flags[i] == '\"') |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 740 | skip = !skip; |
Matt Mower | 2416a50 | 2016-04-12 19:54:46 -0500 | [diff] [blame] | 741 | if (!skip && flags[i] == ';') |
| 742 | flags[i] = separator[0]; |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 743 | } |
| 744 | |
Matt Mower | 2416a50 | 2016-04-12 19:54:46 -0500 | [diff] [blame] | 745 | // Avoid issues with potentially nested strtok by using strtok_r |
| 746 | ptr = strtok_r(flags, separator, &savep); |
| 747 | while (ptr) { |
| 748 | int ptr_len = strlen(ptr); |
| 749 | const struct flag_list* tw_flag = tw_flags; |
Hashcode | 62bd9e0 | 2013-11-19 21:59:42 -0800 | [diff] [blame] | 750 | |
Matt Mower | 2416a50 | 2016-04-12 19:54:46 -0500 | [diff] [blame] | 751 | for (; tw_flag->name; tw_flag++) { |
| 752 | int flag_len = strlen(tw_flag->name); |
| 753 | |
| 754 | if (strncmp(ptr, tw_flag->name, flag_len) == 0) { |
| 755 | bool flag_val = false; |
| 756 | |
| 757 | if (ptr_len > flag_len && (tw_flag->name)[flag_len-1] != '=' |
| 758 | && ptr[flag_len] != '=') { |
| 759 | // Handle flags with same starting string |
| 760 | // (e.g. backup and backupname) |
| 761 | continue; |
| 762 | } else if (ptr_len > flag_len && ptr[flag_len] == '=') { |
| 763 | // Handle flags with dual format: Part 1 |
| 764 | // (e.g. backup and backup=y. backup=y handled here) |
| 765 | ptr += flag_len + 1; |
| 766 | TWFunc::Strip_Quotes(ptr); |
| 767 | // Skip flags with empty argument |
| 768 | // (e.g. backup=) |
| 769 | if (strlen(ptr) == 0) { |
| 770 | LOGINFO("Flag missing argument or should not include '=': %s=\n", tw_flag->name); |
| 771 | break; |
| 772 | } |
| 773 | flag_val = strchr("yY1", *ptr) != NULL; |
| 774 | } else if (ptr_len == flag_len |
| 775 | && (tw_flag->name)[flag_len-1] == '=') { |
| 776 | // Skip flags missing argument after = |
| 777 | // (e.g. backupname=) |
| 778 | LOGINFO("Flag missing argument: %s\n", tw_flag->name); |
| 779 | break; |
| 780 | } else if (ptr_len > flag_len |
| 781 | && (tw_flag->name)[flag_len-1] == '=') { |
| 782 | // Handle arguments to flags |
| 783 | // (e.g. backupname="My Stuff") |
| 784 | ptr += flag_len; |
| 785 | TWFunc::Strip_Quotes(ptr); |
| 786 | // Skip flags with empty argument |
| 787 | // (e.g. backupname="") |
| 788 | if (strlen(ptr) == 0) { |
| 789 | LOGINFO("Flag missing argument: %s\n", tw_flag->name); |
| 790 | break; |
| 791 | } |
| 792 | } else if (ptr_len == flag_len) { |
| 793 | // Handle flags with dual format: Part 2 |
| 794 | // (e.g. backup and backup=y. backup handled here) |
| 795 | flag_val = true; |
Ethan Yonker | 96af84a | 2015-01-05 14:58:36 -0600 | [diff] [blame] | 796 | } else { |
Matt Mower | 2416a50 | 2016-04-12 19:54:46 -0500 | [diff] [blame] | 797 | LOGINFO("Flag matched, but could not be processed: %s\n", ptr); |
| 798 | break; |
Ethan Yonker | 96af84a | 2015-01-05 14:58:36 -0600 | [diff] [blame] | 799 | } |
Matt Mower | 2416a50 | 2016-04-12 19:54:46 -0500 | [diff] [blame] | 800 | |
| 801 | Apply_TW_Flag(tw_flag->flag, ptr, flag_val); |
| 802 | break; |
Ethan Yonker | 96af84a | 2015-01-05 14:58:36 -0600 | [diff] [blame] | 803 | } |
Matt Mower | 2416a50 | 2016-04-12 19:54:46 -0500 | [diff] [blame] | 804 | } |
| 805 | if (tw_flag->name == 0) { |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 806 | if (Display_Error) |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 807 | LOGERR("Unhandled flag: '%s'\n", ptr); |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 808 | else |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 809 | LOGINFO("Unhandled flag: '%s'\n", ptr); |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 810 | } |
Matt Mower | 2416a50 | 2016-04-12 19:54:46 -0500 | [diff] [blame] | 811 | ptr = strtok_r(NULL, separator, &savep); |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 812 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 813 | } |
| 814 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 815 | bool TWPartition::Is_File_System(string File_System) { |
| 816 | if (File_System == "ext2" || |
Dees_Troy | 63c8df7 | 2012-09-10 14:02:05 -0400 | [diff] [blame] | 817 | File_System == "ext3" || |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 818 | File_System == "ext4" || |
| 819 | File_System == "vfat" || |
| 820 | File_System == "ntfs" || |
| 821 | File_System == "yaffs2" || |
bigbiff bigbiff | 3e14652 | 2012-11-14 14:32:59 -0500 | [diff] [blame] | 822 | File_System == "exfat" || |
Dees_Troy | e501704 | 2013-08-29 16:38:55 +0000 | [diff] [blame] | 823 | File_System == "f2fs" || |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 824 | File_System == "auto") |
| 825 | return true; |
| 826 | else |
| 827 | return false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 828 | } |
| 829 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 830 | bool TWPartition::Is_Image(string File_System) { |
Dees_Troy | 5fcd8f9 | 2012-10-16 12:22:05 -0400 | [diff] [blame] | 831 | if (File_System == "emmc" || File_System == "mtd" || File_System == "bml") |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 832 | return true; |
| 833 | else |
| 834 | return false; |
| 835 | } |
| 836 | |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 837 | bool TWPartition::Make_Dir(string Path, bool Display_Error) { |
Ethan Yonker | 93ac7a0 | 2016-11-07 22:05:58 -0600 | [diff] [blame] | 838 | if (TWFunc::Get_D_Type_From_Stat(Path) != S_IFDIR) |
| 839 | unlink(Path.c_str()); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 840 | if (!TWFunc::Path_Exists(Path)) { |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 841 | if (mkdir(Path.c_str(), 0777) == -1) { |
| 842 | if (Display_Error) |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 843 | gui_msg(Msg(msg::kError, "create_folder_strerr=Can not create '{1}' folder ({2}).")(Path)(strerror(errno))); |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 844 | else |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 845 | LOGINFO("Can not create '%s' folder.\n", Path.c_str()); |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 846 | return false; |
| 847 | } else { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 848 | LOGINFO("Created '%s' folder.\n", Path.c_str()); |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 849 | return true; |
| 850 | } |
| 851 | } |
| 852 | return true; |
| 853 | } |
| 854 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 855 | void TWPartition::Setup_File_System(bool Display_Error) { |
| 856 | struct statfs st; |
| 857 | |
| 858 | Can_Be_Mounted = true; |
| 859 | Can_Be_Wiped = true; |
| 860 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 861 | // Make the mount point folder if it doesn't exist |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 862 | Make_Dir(Mount_Point, Display_Error); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 863 | Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1); |
| 864 | Backup_Name = Display_Name; |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 865 | Backup_Method = BM_FILES; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 866 | } |
| 867 | |
Ethan Yonker | 1b19016 | 2016-12-05 15:25:19 -0600 | [diff] [blame] | 868 | void TWPartition::Setup_Image() { |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 869 | Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1); |
| 870 | Backup_Name = Display_Name; |
Gary Peck | 82599a8 | 2012-11-21 16:23:12 -0800 | [diff] [blame] | 871 | if (Current_File_System == "emmc") |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 872 | Backup_Method = BM_DD; |
Gary Peck | 82599a8 | 2012-11-21 16:23:12 -0800 | [diff] [blame] | 873 | else if (Current_File_System == "mtd" || Current_File_System == "bml") |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 874 | Backup_Method = BM_FLASH_UTILS; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 875 | else |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 876 | LOGINFO("Unhandled file system '%s' on image '%s'\n", Current_File_System.c_str(), Display_Name.c_str()); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 877 | } |
| 878 | |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 879 | void TWPartition::Setup_AndSec(void) { |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 880 | Backup_Display_Name = "Android Secure"; |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 881 | Backup_Name = "and-sec"; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 882 | Can_Be_Backed_Up = true; |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 883 | Has_Android_Secure = true; |
| 884 | Symlink_Path = Mount_Point + "/.android_secure"; |
| 885 | Symlink_Mount_Point = "/and-sec"; |
| 886 | Backup_Path = Symlink_Mount_Point; |
| 887 | Make_Dir("/and-sec", true); |
| 888 | Recreate_AndSec_Folder(); |
Kjell Braden | 3126a11 | 2016-06-19 16:58:15 +0000 | [diff] [blame] | 889 | Mount_Storage_Retry(true); |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 890 | } |
| 891 | |
that | 9e0593e | 2014-10-08 00:01:24 +0200 | [diff] [blame] | 892 | void TWPartition::Setup_Data_Media() { |
Ethan Yonker | 6277c79 | 2014-09-15 14:54:30 -0500 | [diff] [blame] | 893 | LOGINFO("Setting up '%s' as data/media emulated storage.\n", Mount_Point.c_str()); |
Ethan Yonker | 66a1949 | 2015-12-10 10:19:45 -0600 | [diff] [blame] | 894 | if (Storage_Name.empty() || Storage_Name == "Data") |
| 895 | Storage_Name = "Internal Storage"; |
Ethan Yonker | 6277c79 | 2014-09-15 14:54:30 -0500 | [diff] [blame] | 896 | Has_Data_Media = true; |
| 897 | Is_Storage = true; |
Ethan Yonker | 66a1949 | 2015-12-10 10:19:45 -0600 | [diff] [blame] | 898 | Storage_Path = Mount_Point + "/media"; |
Ethan Yonker | 6277c79 | 2014-09-15 14:54:30 -0500 | [diff] [blame] | 899 | Symlink_Path = Storage_Path; |
Ethan Yonker | 66a1949 | 2015-12-10 10:19:45 -0600 | [diff] [blame] | 900 | if (Mount_Point == "/data") { |
| 901 | Is_Settings_Storage = true; |
| 902 | if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) { |
| 903 | Make_Dir("/emmc", false); |
| 904 | Symlink_Mount_Point = "/emmc"; |
| 905 | } else { |
| 906 | Make_Dir("/sdcard", false); |
| 907 | Symlink_Mount_Point = "/sdcard"; |
| 908 | } |
| 909 | if (Mount(false) && TWFunc::Path_Exists(Mount_Point + "/media/0")) { |
| 910 | Storage_Path = Mount_Point + "/media/0"; |
| 911 | Symlink_Path = Storage_Path; |
| 912 | DataManager::SetValue(TW_INTERNAL_PATH, Mount_Point + "/media/0"); |
| 913 | UnMount(true); |
| 914 | } |
| 915 | DataManager::SetValue("tw_has_internal", 1); |
| 916 | DataManager::SetValue("tw_has_data_media", 1); |
Ethan Yonker | 3fdcda4 | 2016-11-30 12:29:37 -0600 | [diff] [blame] | 917 | backup_exclusions.add_absolute_dir("/data/data/com.google.android.music/files"); |
Ethan Yonker | 79f88bd | 2016-12-09 14:52:12 -0600 | [diff] [blame] | 918 | ExcludeAll(Mount_Point + "/misc/vold"); |
| 919 | ExcludeAll(Mount_Point + "/.layout_version"); |
| 920 | ExcludeAll(Mount_Point + "/system/storage.xml"); |
Ethan Yonker | 6277c79 | 2014-09-15 14:54:30 -0500 | [diff] [blame] | 921 | } else { |
Ethan Yonker | 66a1949 | 2015-12-10 10:19:45 -0600 | [diff] [blame] | 922 | if (Mount(true) && TWFunc::Path_Exists(Mount_Point + "/media/0")) { |
| 923 | Storage_Path = Mount_Point + "/media/0"; |
| 924 | Symlink_Path = Storage_Path; |
| 925 | UnMount(true); |
| 926 | } |
Ethan Yonker | 6277c79 | 2014-09-15 14:54:30 -0500 | [diff] [blame] | 927 | } |
Ethan Yonker | 79f88bd | 2016-12-09 14:52:12 -0600 | [diff] [blame] | 928 | ExcludeAll(Mount_Point + "/media"); |
Ethan Yonker | 6277c79 | 2014-09-15 14:54:30 -0500 | [diff] [blame] | 929 | } |
| 930 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 931 | void TWPartition::Find_Real_Block_Device(string& Block, bool Display_Error) { |
| 932 | char device[512], realDevice[512]; |
| 933 | |
| 934 | strcpy(device, Block.c_str()); |
| 935 | memset(realDevice, 0, sizeof(realDevice)); |
| 936 | while (readlink(device, realDevice, sizeof(realDevice)) > 0) |
| 937 | { |
| 938 | strcpy(device, realDevice); |
| 939 | memset(realDevice, 0, sizeof(realDevice)); |
| 940 | } |
| 941 | |
| 942 | if (device[0] != '/') { |
| 943 | if (Display_Error) |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 944 | 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] | 945 | else |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 946 | 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] | 947 | return; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 948 | } else { |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 949 | Block = device; |
| 950 | return; |
| 951 | } |
| 952 | } |
| 953 | |
Kjell Braden | 3126a11 | 2016-06-19 16:58:15 +0000 | [diff] [blame] | 954 | bool TWPartition::Mount_Storage_Retry(bool Display_Error) { |
Dees_Troy | 8e337f3 | 2012-10-13 22:07:49 -0400 | [diff] [blame] | 955 | // On some devices, storage doesn't want to mount right away, retry and sleep |
Kjell Braden | 3126a11 | 2016-06-19 16:58:15 +0000 | [diff] [blame] | 956 | if (!Mount(Display_Error)) { |
Dees_Troy | 8e337f3 | 2012-10-13 22:07:49 -0400 | [diff] [blame] | 957 | int retry_count = 5; |
| 958 | while (retry_count > 0 && !Mount(false)) { |
| 959 | usleep(500000); |
| 960 | retry_count--; |
| 961 | } |
Kjell Braden | 3126a11 | 2016-06-19 16:58:15 +0000 | [diff] [blame] | 962 | return Mount(Display_Error); |
Dees_Troy | 8e337f3 | 2012-10-13 22:07:49 -0400 | [diff] [blame] | 963 | } |
Kjell Braden | 3126a11 | 2016-06-19 16:58:15 +0000 | [diff] [blame] | 964 | return true; |
Dees_Troy | 8e337f3 | 2012-10-13 22:07:49 -0400 | [diff] [blame] | 965 | } |
| 966 | |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 967 | bool TWPartition::Find_MTD_Block_Device(string MTD_Name) { |
| 968 | FILE *fp = NULL; |
| 969 | char line[255]; |
| 970 | |
| 971 | fp = fopen("/proc/mtd", "rt"); |
| 972 | if (fp == NULL) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 973 | LOGERR("Device does not support /proc/mtd\n"); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 974 | return false; |
| 975 | } |
| 976 | |
| 977 | while (fgets(line, sizeof(line), fp) != NULL) |
| 978 | { |
| 979 | char device[32], label[32]; |
| 980 | unsigned long size = 0; |
| 981 | char* fstype = NULL; |
| 982 | int deviceId; |
| 983 | |
| 984 | sscanf(line, "%s %lx %*s %*c%s", device, &size, label); |
| 985 | |
| 986 | // Skip header and blank lines |
| 987 | if ((strcmp(device, "dev:") == 0) || (strlen(line) < 8)) |
| 988 | continue; |
| 989 | |
| 990 | // Strip off the trailing " from the label |
| 991 | label[strlen(label)-1] = '\0'; |
| 992 | |
| 993 | if (strcmp(label, MTD_Name.c_str()) == 0) { |
| 994 | // We found our device |
| 995 | // Strip off the trailing : from the device |
| 996 | device[strlen(device)-1] = '\0'; |
| 997 | if (sscanf(device,"mtd%d", &deviceId) == 1) { |
| 998 | sprintf(device, "/dev/block/mtdblock%d", deviceId); |
| 999 | Primary_Block_Device = device; |
Dees_Troy | 76543db | 2013-06-19 16:24:30 +0000 | [diff] [blame] | 1000 | fclose(fp); |
| 1001 | return true; |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1002 | } |
| 1003 | } |
| 1004 | } |
| 1005 | fclose(fp); |
| 1006 | |
| 1007 | return false; |
| 1008 | } |
| 1009 | |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 1010 | bool TWPartition::Get_Size_Via_statfs(bool Display_Error) { |
| 1011 | struct statfs st; |
| 1012 | string Local_Path = Mount_Point + "/."; |
| 1013 | |
| 1014 | if (!Mount(Display_Error)) |
| 1015 | return false; |
| 1016 | |
| 1017 | if (statfs(Local_Path.c_str(), &st) != 0) { |
| 1018 | if (!Removable) { |
| 1019 | if (Display_Error) |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1020 | LOGERR("Unable to statfs '%s'\n", Local_Path.c_str()); |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 1021 | else |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1022 | LOGINFO("Unable to statfs '%s'\n", Local_Path.c_str()); |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 1023 | } |
| 1024 | return false; |
| 1025 | } |
| 1026 | Size = (st.f_blocks * st.f_bsize); |
| 1027 | Used = ((st.f_blocks - st.f_bfree) * st.f_bsize); |
| 1028 | Free = (st.f_bfree * st.f_bsize); |
| 1029 | Backup_Size = Used; |
| 1030 | return true; |
| 1031 | } |
| 1032 | |
| 1033 | bool TWPartition::Get_Size_Via_df(bool Display_Error) { |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1034 | FILE* fp; |
| 1035 | char command[255], line[512]; |
| 1036 | int include_block = 1; |
| 1037 | unsigned int min_len; |
| 1038 | |
| 1039 | if (!Mount(Display_Error)) |
| 1040 | return false; |
| 1041 | |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1042 | min_len = Actual_Block_Device.size() + 2; |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 1043 | sprintf(command, "df %s > /tmp/dfoutput.txt", Mount_Point.c_str()); |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 1044 | TWFunc::Exec_Cmd(command); |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 1045 | fp = fopen("/tmp/dfoutput.txt", "rt"); |
| 1046 | if (fp == NULL) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1047 | LOGINFO("Unable to open /tmp/dfoutput.txt.\n"); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1048 | return false; |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 1049 | } |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1050 | |
| 1051 | while (fgets(line, sizeof(line), fp) != NULL) |
| 1052 | { |
| 1053 | unsigned long blocks, used, available; |
| 1054 | char device[64]; |
| 1055 | char tmpString[64]; |
| 1056 | |
| 1057 | if (strncmp(line, "Filesystem", 10) == 0) |
| 1058 | continue; |
| 1059 | if (strlen(line) < min_len) { |
| 1060 | include_block = 0; |
| 1061 | continue; |
| 1062 | } |
| 1063 | if (include_block) { |
| 1064 | sscanf(line, "%s %lu %lu %lu", device, &blocks, &used, &available); |
| 1065 | } else { |
| 1066 | // The device block string is so long that the df information is on the next line |
| 1067 | int space_count = 0; |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 1068 | sprintf(tmpString, "/dev/block/%s", Actual_Block_Device.c_str()); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1069 | while (tmpString[space_count] == 32) |
| 1070 | space_count++; |
| 1071 | sscanf(line + space_count, "%lu %lu %lu", &blocks, &used, &available); |
| 1072 | } |
| 1073 | |
| 1074 | // Adjust block size to byte size |
| 1075 | Size = blocks * 1024ULL; |
| 1076 | Used = used * 1024ULL; |
| 1077 | Free = available * 1024ULL; |
| 1078 | Backup_Size = Used; |
| 1079 | } |
| 1080 | fclose(fp); |
| 1081 | return true; |
| 1082 | } |
| 1083 | |
Ethan Yonker | d18a821 | 2015-12-14 10:17:00 -0600 | [diff] [blame] | 1084 | unsigned long long TWPartition::IOCTL_Get_Block_Size() { |
Ethan Yonker | d18a821 | 2015-12-14 10:17:00 -0600 | [diff] [blame] | 1085 | Find_Actual_Block_Device(); |
Ethan Yonker | 483e9f4 | 2016-01-11 22:21:18 -0600 | [diff] [blame] | 1086 | |
| 1087 | return TWFunc::IOCTL_Get_Block_Size(Actual_Block_Device.c_str()); |
Ethan Yonker | d18a821 | 2015-12-14 10:17:00 -0600 | [diff] [blame] | 1088 | } |
| 1089 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1090 | bool TWPartition::Find_Partition_Size(void) { |
| 1091 | FILE* fp; |
| 1092 | char line[512]; |
| 1093 | string tmpdevice; |
| 1094 | |
igoriok | 87e3d93 | 2013-01-31 21:03:53 +0200 | [diff] [blame] | 1095 | fp = fopen("/proc/dumchar_info", "rt"); |
| 1096 | if (fp != NULL) { |
| 1097 | while (fgets(line, sizeof(line), fp) != NULL) |
| 1098 | { |
| 1099 | char label[32], device[32]; |
| 1100 | unsigned long size = 0; |
| 1101 | |
that | d43bf2d | 2014-09-21 23:13:02 +0200 | [diff] [blame] | 1102 | sscanf(line, "%s %lx %*x %*u %s", label, &size, device); |
igoriok | 87e3d93 | 2013-01-31 21:03:53 +0200 | [diff] [blame] | 1103 | |
bigbiff bigbiff | 34684ff | 2013-12-01 21:03:45 -0500 | [diff] [blame] | 1104 | // Skip header, annotation and blank lines |
igoriok | 87e3d93 | 2013-01-31 21:03:53 +0200 | [diff] [blame] | 1105 | if ((strncmp(device, "/dev/", 5) != 0) || (strlen(line) < 8)) |
| 1106 | continue; |
| 1107 | |
| 1108 | tmpdevice = "/dev/"; |
| 1109 | tmpdevice += label; |
| 1110 | if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) { |
| 1111 | Size = size; |
| 1112 | fclose(fp); |
| 1113 | return true; |
| 1114 | } |
| 1115 | } |
| 1116 | } |
| 1117 | |
Ethan Yonker | d18a821 | 2015-12-14 10:17:00 -0600 | [diff] [blame] | 1118 | unsigned long long ioctl_size = IOCTL_Get_Block_Size(); |
| 1119 | if (ioctl_size) { |
| 1120 | Size = ioctl_size; |
| 1121 | return true; |
| 1122 | } |
| 1123 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1124 | // In this case, we'll first get the partitions we care about (with labels) |
| 1125 | fp = fopen("/proc/partitions", "rt"); |
| 1126 | if (fp == NULL) |
| 1127 | return false; |
| 1128 | |
| 1129 | while (fgets(line, sizeof(line), fp) != NULL) |
| 1130 | { |
| 1131 | unsigned long major, minor, blocks; |
| 1132 | char device[512]; |
| 1133 | char tmpString[64]; |
| 1134 | |
Dees_Troy | 63c8df7 | 2012-09-10 14:02:05 -0400 | [diff] [blame] | 1135 | if (strlen(line) < 7 || line[0] == 'm') continue; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1136 | sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device); |
| 1137 | |
| 1138 | tmpdevice = "/dev/block/"; |
| 1139 | tmpdevice += device; |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1140 | if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) { |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1141 | // Adjust block size to byte size |
| 1142 | Size = blocks * 1024ULL; |
| 1143 | fclose(fp); |
| 1144 | return true; |
| 1145 | } |
| 1146 | } |
| 1147 | fclose(fp); |
| 1148 | return false; |
| 1149 | } |
| 1150 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1151 | bool TWPartition::Is_Mounted(void) { |
| 1152 | if (!Can_Be_Mounted) |
| 1153 | return false; |
| 1154 | |
| 1155 | struct stat st1, st2; |
| 1156 | string test_path; |
| 1157 | |
| 1158 | // Check to see if the mount point directory exists |
| 1159 | test_path = Mount_Point + "/."; |
| 1160 | if (stat(test_path.c_str(), &st1) != 0) return false; |
| 1161 | |
| 1162 | // Check to see if the directory above the mount point exists |
| 1163 | test_path = Mount_Point + "/../."; |
| 1164 | if (stat(test_path.c_str(), &st2) != 0) return false; |
| 1165 | |
| 1166 | // Compare the device IDs -- if they match then we're (probably) using tmpfs instead of an actual device |
| 1167 | int ret = (st1.st_dev != st2.st_dev) ? true : false; |
| 1168 | |
| 1169 | return ret; |
| 1170 | } |
| 1171 | |
James Christopher Adduono | d6f94ac | 2016-02-29 04:26:04 -0500 | [diff] [blame] | 1172 | bool TWPartition::Is_File_System_Writable(void) { |
| 1173 | if (!Is_File_System(Current_File_System) || !Is_Mounted()) |
| 1174 | return false; |
| 1175 | |
| 1176 | string test_path = Mount_Point + "/."; |
| 1177 | return (access(test_path.c_str(), W_OK) == 0); |
| 1178 | } |
| 1179 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1180 | bool TWPartition::Mount(bool Display_Error) { |
Dees_Troy | 3f5c4e8 | 2013-02-01 15:16:59 +0000 | [diff] [blame] | 1181 | int exfat_mounted = 0; |
Ethan Yonker | eb32b1f | 2015-05-18 10:23:03 -0500 | [diff] [blame] | 1182 | unsigned long flags = Mount_Flags; |
Dees_Troy | 3f5c4e8 | 2013-02-01 15:16:59 +0000 | [diff] [blame] | 1183 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1184 | if (Is_Mounted()) { |
| 1185 | return true; |
| 1186 | } else if (!Can_Be_Mounted) { |
| 1187 | return false; |
| 1188 | } |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1189 | |
| 1190 | Find_Actual_Block_Device(); |
| 1191 | |
| 1192 | // Check the current file system before mounting |
| 1193 | Check_FS_Type(); |
Dees_Troy | 3f5c4e8 | 2013-02-01 15:16:59 +0000 | [diff] [blame] | 1194 | if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/exfat-fuse")) { |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 1195 | 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] | 1196 | LOGINFO("cmd: %s\n", cmd.c_str()); |
Dees_Troy | 3f5c4e8 | 2013-02-01 15:16:59 +0000 | [diff] [blame] | 1197 | string result; |
| 1198 | if (TWFunc::Exec_Cmd(cmd, result) != 0) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1199 | 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] | 1200 | Current_File_System = "vfat"; |
| 1201 | } else { |
| 1202 | #ifdef TW_NO_EXFAT_FUSE |
| 1203 | UnMount(false); |
| 1204 | // We'll let the kernel handle it but using exfat-fuse to detect if the file system is actually exfat |
| 1205 | // Some kernels let us mount vfat as exfat which doesn't work out too well |
| 1206 | #else |
| 1207 | exfat_mounted = 1; |
| 1208 | #endif |
| 1209 | } |
| 1210 | } |
Ethan Yonker | eb32b1f | 2015-05-18 10:23:03 -0500 | [diff] [blame] | 1211 | |
Greg Wallace | b39e6c6 | 2015-12-29 00:55:26 -0500 | [diff] [blame] | 1212 | if (Current_File_System == "ntfs" && (TWFunc::Path_Exists("/sbin/ntfs-3g") || TWFunc::Path_Exists("/sbin/mount.ntfs"))) { |
Ethan Yonker | b81d905 | 2015-07-09 13:20:53 -0500 | [diff] [blame] | 1213 | string cmd; |
Greg Wallace | b39e6c6 | 2015-12-29 00:55:26 -0500 | [diff] [blame] | 1214 | string Ntfsmount_Binary = ""; |
| 1215 | |
| 1216 | if (TWFunc::Path_Exists("/sbin/ntfs-3g")) |
| 1217 | Ntfsmount_Binary = "ntfs-3g"; |
| 1218 | else if (TWFunc::Path_Exists("/sbin/mount.ntfs")) |
| 1219 | Ntfsmount_Binary = "mount.ntfs"; |
| 1220 | |
Ethan Yonker | b81d905 | 2015-07-09 13:20:53 -0500 | [diff] [blame] | 1221 | if (Mount_Read_Only) |
Greg Wallace | b39e6c6 | 2015-12-29 00:55:26 -0500 | [diff] [blame] | 1222 | cmd = "/sbin/" + Ntfsmount_Binary + " -o ro " + Actual_Block_Device + " " + Mount_Point; |
Ethan Yonker | b81d905 | 2015-07-09 13:20:53 -0500 | [diff] [blame] | 1223 | else |
Greg Wallace | b39e6c6 | 2015-12-29 00:55:26 -0500 | [diff] [blame] | 1224 | cmd = "/sbin/" + Ntfsmount_Binary + " " + Actual_Block_Device + " " + Mount_Point; |
Ethan Yonker | b81d905 | 2015-07-09 13:20:53 -0500 | [diff] [blame] | 1225 | LOGINFO("cmd: '%s'\n", cmd.c_str()); |
Greg Wallace | b39e6c6 | 2015-12-29 00:55:26 -0500 | [diff] [blame] | 1226 | |
Ethan Yonker | b81d905 | 2015-07-09 13:20:53 -0500 | [diff] [blame] | 1227 | if (TWFunc::Exec_Cmd(cmd) == 0) { |
| 1228 | return true; |
| 1229 | } else { |
| 1230 | LOGINFO("ntfs-3g failed to mount, trying regular mount method.\n"); |
| 1231 | } |
| 1232 | } |
| 1233 | |
Ethan Yonker | eb32b1f | 2015-05-18 10:23:03 -0500 | [diff] [blame] | 1234 | if (Mount_Read_Only) |
| 1235 | flags |= MS_RDONLY; |
| 1236 | |
Dees_Troy | 2204203 | 2012-12-18 21:23:08 +0000 | [diff] [blame] | 1237 | if (Fstab_File_System == "yaffs2") { |
| 1238 | // mount an MTD partition as a YAFFS2 filesystem. |
Ethan Yonker | eb32b1f | 2015-05-18 10:23:03 -0500 | [diff] [blame] | 1239 | flags = MS_NOATIME | MS_NODEV | MS_NODIRATIME; |
| 1240 | if (Mount_Read_Only) |
| 1241 | flags |= MS_RDONLY; |
Dees_Troy | 76543db | 2013-06-19 16:24:30 +0000 | [diff] [blame] | 1242 | if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags, NULL) < 0) { |
| 1243 | if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags | MS_RDONLY, NULL) < 0) { |
| 1244 | if (Display_Error) |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1245 | gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno))); |
Dees_Troy | 76543db | 2013-06-19 16:24:30 +0000 | [diff] [blame] | 1246 | else |
| 1247 | LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str()); |
| 1248 | return false; |
| 1249 | } else { |
| 1250 | LOGINFO("Mounted '%s' (MTD) as RO\n", Mount_Point.c_str()); |
| 1251 | return true; |
| 1252 | } |
| 1253 | } else { |
| 1254 | struct stat st; |
| 1255 | string test_path = Mount_Point; |
| 1256 | if (stat(test_path.c_str(), &st) < 0) { |
| 1257 | if (Display_Error) |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1258 | gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno))); |
Dees_Troy | 76543db | 2013-06-19 16:24:30 +0000 | [diff] [blame] | 1259 | else |
| 1260 | LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str()); |
| 1261 | return false; |
| 1262 | } |
| 1263 | mode_t new_mode = st.st_mode | S_IXUSR | S_IXGRP | S_IXOTH; |
| 1264 | if (new_mode != st.st_mode) { |
| 1265 | LOGINFO("Fixing execute permissions for %s\n", Mount_Point.c_str()); |
| 1266 | if (chmod(Mount_Point.c_str(), new_mode) < 0) { |
| 1267 | if (Display_Error) |
| 1268 | LOGERR("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno)); |
| 1269 | else |
| 1270 | LOGINFO("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno)); |
| 1271 | return false; |
| 1272 | } |
| 1273 | } |
Dees_Troy | 2204203 | 2012-12-18 21:23:08 +0000 | [diff] [blame] | 1274 | return true; |
Dees_Troy | 76543db | 2013-06-19 16:24:30 +0000 | [diff] [blame] | 1275 | } |
that | c7572eb | 2015-03-31 18:55:30 +0200 | [diff] [blame] | 1276 | } |
| 1277 | |
| 1278 | string mount_fs = Current_File_System; |
| 1279 | if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sys/module/texfat")) |
| 1280 | mount_fs = "texfat"; |
| 1281 | |
| 1282 | if (!exfat_mounted && |
Ethan Yonker | eb32b1f | 2015-05-18 10:23:03 -0500 | [diff] [blame] | 1283 | mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), mount_fs.c_str(), flags, Mount_Options.c_str()) != 0 && |
| 1284 | mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), mount_fs.c_str(), flags, NULL) != 0) { |
Dees_Troy | 3f5c4e8 | 2013-02-01 15:16:59 +0000 | [diff] [blame] | 1285 | #ifdef TW_NO_EXFAT_FUSE |
| 1286 | if (Current_File_System == "exfat") { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1287 | LOGINFO("Mounting exfat failed, trying vfat...\n"); |
Dees_Troy | 3f5c4e8 | 2013-02-01 15:16:59 +0000 | [diff] [blame] | 1288 | 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] | 1289 | if (Display_Error) |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1290 | gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno))); |
Dees_Troy | 85f44ed | 2013-01-09 18:42:36 +0000 | [diff] [blame] | 1291 | else |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1292 | LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str()); |
Ethan Yonker | eb32b1f | 2015-05-18 10:23:03 -0500 | [diff] [blame] | 1293 | 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(), flags, Mount_Options.c_str()); |
Dees_Troy | 3f5c4e8 | 2013-02-01 15:16:59 +0000 | [diff] [blame] | 1294 | return false; |
Dees_Troy | 85f44ed | 2013-01-09 18:42:36 +0000 | [diff] [blame] | 1295 | } |
Dees_Troy | b05ddee | 2013-01-28 20:24:50 +0000 | [diff] [blame] | 1296 | } else { |
Dees_Troy | 3f5c4e8 | 2013-02-01 15:16:59 +0000 | [diff] [blame] | 1297 | #endif |
bigbiff bigbiff | 26774a0 | 2014-03-29 18:22:00 -0400 | [diff] [blame] | 1298 | if (!Removable && Display_Error) |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1299 | gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno))); |
Dees_Troy | 3f5c4e8 | 2013-02-01 15:16:59 +0000 | [diff] [blame] | 1300 | else |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1301 | LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str()); |
| 1302 | 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] | 1303 | return false; |
| 1304 | #ifdef TW_NO_EXFAT_FUSE |
Dees_Troy | 85f44ed | 2013-01-09 18:42:36 +0000 | [diff] [blame] | 1305 | } |
| 1306 | #endif |
Dees_Troy | 3f5c4e8 | 2013-02-01 15:16:59 +0000 | [diff] [blame] | 1307 | } |
Ethan Yonker | 253368a | 2014-11-25 15:00:52 -0600 | [diff] [blame] | 1308 | |
Dees_Troy | 3f5c4e8 | 2013-02-01 15:16:59 +0000 | [diff] [blame] | 1309 | if (Removable) |
| 1310 | Update_Size(Display_Error); |
| 1311 | |
xiaolu | 9416f4f | 2015-06-04 08:22:23 +0800 | [diff] [blame] | 1312 | if (!Symlink_Mount_Point.empty() && TWFunc::Path_Exists(Symlink_Path)) { |
codelover | 2a3d4ce | 2015-03-14 20:26:49 +0800 | [diff] [blame] | 1313 | string Command = "mount -o bind '" + Symlink_Path + "' '" + Symlink_Mount_Point + "'"; |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 1314 | TWFunc::Exec_Cmd(Command); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1315 | } |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1316 | return true; |
| 1317 | } |
| 1318 | |
| 1319 | bool TWPartition::UnMount(bool Display_Error) { |
| 1320 | if (Is_Mounted()) { |
| 1321 | int never_unmount_system; |
| 1322 | |
| 1323 | DataManager::GetValue(TW_DONT_UNMOUNT_SYSTEM, never_unmount_system); |
| 1324 | if (never_unmount_system == 1 && Mount_Point == "/system") |
| 1325 | return true; // Never unmount system if you're not supposed to unmount it |
| 1326 | |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 1327 | if (Is_Storage) |
Ethan Yonker | 726a020 | 2014-12-16 20:01:38 -0600 | [diff] [blame] | 1328 | PartitionManager.Remove_MTP_Storage(MTP_Storage_ID); |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 1329 | |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1330 | if (!Symlink_Mount_Point.empty()) |
| 1331 | umount(Symlink_Mount_Point.c_str()); |
| 1332 | |
Dees_Troy | b05ddee | 2013-01-28 20:24:50 +0000 | [diff] [blame] | 1333 | umount(Mount_Point.c_str()); |
| 1334 | if (Is_Mounted()) { |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1335 | if (Display_Error) |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1336 | gui_msg(Msg(msg::kError, "fail_unmount=Failed to unmount '{1}' ({2})")(Mount_Point)(strerror(errno))); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1337 | else |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1338 | LOGINFO("Unable to unmount '%s'\n", Mount_Point.c_str()); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1339 | return false; |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 1340 | } else { |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1341 | return true; |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 1342 | } |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1343 | } else { |
| 1344 | return true; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1345 | } |
| 1346 | } |
| 1347 | |
James Christopher Adduono | d6f94ac | 2016-02-29 04:26:04 -0500 | [diff] [blame] | 1348 | bool TWPartition::ReMount(bool Display_Error) { |
| 1349 | if (UnMount(Display_Error)) |
| 1350 | return Mount(Display_Error); |
| 1351 | return false; |
| 1352 | } |
| 1353 | |
| 1354 | bool TWPartition::ReMount_RW(bool Display_Error) { |
| 1355 | // No need to remount if already mounted rw |
| 1356 | if (Is_File_System_Writable()) |
| 1357 | return true; |
| 1358 | |
| 1359 | bool ro = Mount_Read_Only; |
| 1360 | int flags = Mount_Flags; |
| 1361 | |
| 1362 | Mount_Read_Only = false; |
| 1363 | Mount_Flags &= ~MS_RDONLY; |
| 1364 | |
| 1365 | bool ret = ReMount(Display_Error); |
| 1366 | |
| 1367 | Mount_Read_Only = ro; |
| 1368 | Mount_Flags = flags; |
| 1369 | |
| 1370 | return ret; |
| 1371 | } |
| 1372 | |
Gary Peck | 43acadf | 2012-11-21 21:19:01 -0800 | [diff] [blame] | 1373 | bool TWPartition::Wipe(string New_File_System) { |
Ethan Yonker | 726a020 | 2014-12-16 20:01:38 -0600 | [diff] [blame] | 1374 | bool wiped = false, update_crypt = false, recreate_media = true; |
Dees_Troy | 16c2b31 | 2013-01-15 16:51:18 +0000 | [diff] [blame] | 1375 | int check; |
| 1376 | string Layout_Filename = Mount_Point + "/.layout_version"; |
| 1377 | |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1378 | if (!Can_Be_Wiped) { |
Matt Mower | 3c36697 | 2015-12-25 19:28:31 -0600 | [diff] [blame] | 1379 | gui_msg(Msg(msg::kError, "cannot_wipe=Partition {1} cannot be wiped.")(Display_Name)); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1380 | return false; |
| 1381 | } |
| 1382 | |
Dees_Troy | c51f1f9 | 2012-09-20 15:32:13 -0400 | [diff] [blame] | 1383 | if (Mount_Point == "/cache") |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1384 | Log_Offset = 0; |
Dees_Troy | c51f1f9 | 2012-09-20 15:32:13 -0400 | [diff] [blame] | 1385 | |
Dees_Troy | 16c2b31 | 2013-01-15 16:51:18 +0000 | [diff] [blame] | 1386 | if (Retain_Layout_Version && Mount(false) && TWFunc::Path_Exists(Layout_Filename)) |
| 1387 | TWFunc::copy_file(Layout_Filename, "/.layout_version", 0600); |
| 1388 | else |
| 1389 | unlink("/.layout_version"); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1390 | |
Ethan Yonker | 87c7bac | 2014-05-25 21:41:08 -0500 | [diff] [blame] | 1391 | if (Has_Data_Media && Current_File_System == New_File_System) { |
Dees_Troy | 16c2b31 | 2013-01-15 16:51:18 +0000 | [diff] [blame] | 1392 | wiped = Wipe_Data_Without_Wiping_Media(); |
Ethan Yonker | 5eac222 | 2014-06-11 12:22:55 -0500 | [diff] [blame] | 1393 | recreate_media = false; |
Dees_Troy | 16c2b31 | 2013-01-15 16:51:18 +0000 | [diff] [blame] | 1394 | } else { |
Dees_Troy | 16c2b31 | 2013-01-15 16:51:18 +0000 | [diff] [blame] | 1395 | DataManager::GetValue(TW_RM_RF_VAR, check); |
| 1396 | |
Hashcode | dabfd49 | 2013-08-29 22:45:30 -0700 | [diff] [blame] | 1397 | if (check || Use_Rm_Rf) |
Dees_Troy | 16c2b31 | 2013-01-15 16:51:18 +0000 | [diff] [blame] | 1398 | wiped = Wipe_RMRF(); |
| 1399 | else if (New_File_System == "ext4") |
| 1400 | wiped = Wipe_EXT4(); |
| 1401 | else if (New_File_System == "ext2" || New_File_System == "ext3") |
| 1402 | wiped = Wipe_EXT23(New_File_System); |
| 1403 | else if (New_File_System == "vfat") |
| 1404 | wiped = Wipe_FAT(); |
| 1405 | else if (New_File_System == "exfat") |
| 1406 | wiped = Wipe_EXFAT(); |
| 1407 | else if (New_File_System == "yaffs2") |
| 1408 | wiped = Wipe_MTD(); |
Dees_Troy | e501704 | 2013-08-29 16:38:55 +0000 | [diff] [blame] | 1409 | else if (New_File_System == "f2fs") |
| 1410 | wiped = Wipe_F2FS(); |
Ethan Yonker | b81d905 | 2015-07-09 13:20:53 -0500 | [diff] [blame] | 1411 | else if (New_File_System == "ntfs") |
| 1412 | wiped = Wipe_NTFS(); |
Dees_Troy | 16c2b31 | 2013-01-15 16:51:18 +0000 | [diff] [blame] | 1413 | else { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1414 | 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] | 1415 | unlink("/.layout_version"); |
| 1416 | return false; |
| 1417 | } |
| 1418 | update_crypt = wiped; |
Gary Peck | e8bc5d7 | 2012-12-21 06:45:25 -0800 | [diff] [blame] | 1419 | } |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1420 | |
Gary Peck | e8bc5d7 | 2012-12-21 06:45:25 -0800 | [diff] [blame] | 1421 | if (wiped) { |
Dees_Troy | 1c1ac44 | 2013-01-17 21:42:14 +0000 | [diff] [blame] | 1422 | if (Mount_Point == "/cache") |
| 1423 | DataManager::Output_Version(); |
| 1424 | |
Dees_Troy | 16c2b31 | 2013-01-15 16:51:18 +0000 | [diff] [blame] | 1425 | if (TWFunc::Path_Exists("/.layout_version") && Mount(false)) |
| 1426 | TWFunc::copy_file("/.layout_version", Layout_Filename, 0600); |
| 1427 | |
| 1428 | if (update_crypt) { |
| 1429 | Setup_File_System(false); |
| 1430 | if (Is_Encrypted && !Is_Decrypted) { |
| 1431 | // just wiped an encrypted partition back to its unencrypted state |
| 1432 | Is_Encrypted = false; |
| 1433 | Is_Decrypted = false; |
| 1434 | Decrypted_Block_Device = ""; |
| 1435 | if (Mount_Point == "/data") { |
| 1436 | DataManager::SetValue(TW_IS_ENCRYPTED, 0); |
| 1437 | DataManager::SetValue(TW_IS_DECRYPTED, 0); |
| 1438 | } |
Gary Peck | e8bc5d7 | 2012-12-21 06:45:25 -0800 | [diff] [blame] | 1439 | } |
| 1440 | } |
Ethan Yonker | 5eac222 | 2014-06-11 12:22:55 -0500 | [diff] [blame] | 1441 | |
Ethan Yonker | 66a1949 | 2015-12-10 10:19:45 -0600 | [diff] [blame] | 1442 | if (Has_Data_Media && recreate_media) { |
Ethan Yonker | 5eac222 | 2014-06-11 12:22:55 -0500 | [diff] [blame] | 1443 | Recreate_Media_Folder(); |
| 1444 | } |
Matt Mower | 209c963 | 2016-01-20 12:08:35 -0600 | [diff] [blame] | 1445 | if (Is_Storage && Mount(false)) |
| 1446 | PartitionManager.Add_MTP_Storage(MTP_Storage_ID); |
Gary Peck | e8bc5d7 | 2012-12-21 06:45:25 -0800 | [diff] [blame] | 1447 | } |
Matt Mower | 209c963 | 2016-01-20 12:08:35 -0600 | [diff] [blame] | 1448 | |
Gary Peck | e8bc5d7 | 2012-12-21 06:45:25 -0800 | [diff] [blame] | 1449 | return wiped; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1450 | } |
| 1451 | |
Gary Peck | 43acadf | 2012-11-21 21:19:01 -0800 | [diff] [blame] | 1452 | bool TWPartition::Wipe() { |
Gary Peck | 82599a8 | 2012-11-21 16:23:12 -0800 | [diff] [blame] | 1453 | if (Is_File_System(Current_File_System)) |
| 1454 | return Wipe(Current_File_System); |
| 1455 | else |
| 1456 | return Wipe(Fstab_File_System); |
Gary Peck | 43acadf | 2012-11-21 21:19:01 -0800 | [diff] [blame] | 1457 | } |
| 1458 | |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 1459 | bool TWPartition::Wipe_AndSec(void) { |
| 1460 | if (!Has_Android_Secure) |
| 1461 | return false; |
| 1462 | |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 1463 | if (!Mount(true)) |
| 1464 | return false; |
| 1465 | |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1466 | gui_msg(Msg("wiping=Wiping {1}")(Backup_Display_Name)); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1467 | TWFunc::removeDir(Mount_Point + "/.android_secure/", true); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1468 | return true; |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 1469 | } |
| 1470 | |
Ethan Yonker | 87c7bac | 2014-05-25 21:41:08 -0500 | [diff] [blame] | 1471 | bool TWPartition::Can_Repair() { |
Ethan Yonker | a271915 | 2015-05-28 09:44:41 -0500 | [diff] [blame] | 1472 | if (Mount_Read_Only) |
| 1473 | return false; |
Matt Mower | 18794c8 | 2015-11-11 16:22:45 -0600 | [diff] [blame] | 1474 | if (Current_File_System == "vfat" && TWFunc::Path_Exists("/sbin/fsck.fat")) |
Ethan Yonker | 87c7bac | 2014-05-25 21:41:08 -0500 | [diff] [blame] | 1475 | return true; |
| 1476 | else if ((Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") && TWFunc::Path_Exists("/sbin/e2fsck")) |
| 1477 | return true; |
| 1478 | else if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/fsck.exfat")) |
| 1479 | return true; |
| 1480 | else if (Current_File_System == "f2fs" && TWFunc::Path_Exists("/sbin/fsck.f2fs")) |
| 1481 | return true; |
Greg Wallace | b39e6c6 | 2015-12-29 00:55:26 -0500 | [diff] [blame] | 1482 | else if (Current_File_System == "ntfs" && (TWFunc::Path_Exists("/sbin/ntfsfix") || TWFunc::Path_Exists("/sbin/fsck.ntfs"))) |
Ethan Yonker | b81d905 | 2015-07-09 13:20:53 -0500 | [diff] [blame] | 1483 | return true; |
Ethan Yonker | 87c7bac | 2014-05-25 21:41:08 -0500 | [diff] [blame] | 1484 | return false; |
| 1485 | } |
| 1486 | |
| 1487 | bool TWPartition::Repair() { |
| 1488 | string command; |
| 1489 | |
| 1490 | if (Current_File_System == "vfat") { |
Matt Mower | 18794c8 | 2015-11-11 16:22:45 -0600 | [diff] [blame] | 1491 | if (!TWFunc::Path_Exists("/sbin/fsck.fat")) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1492 | gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.fat")); |
Ethan Yonker | 87c7bac | 2014-05-25 21:41:08 -0500 | [diff] [blame] | 1493 | return false; |
| 1494 | } |
| 1495 | if (!UnMount(true)) |
| 1496 | return false; |
Greg Wallace | 2d65340 | 2015-12-29 14:19:21 -0500 | [diff] [blame] | 1497 | gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.fat")); |
Ethan Yonker | 87c7bac | 2014-05-25 21:41:08 -0500 | [diff] [blame] | 1498 | Find_Actual_Block_Device(); |
Matt Mower | 18794c8 | 2015-11-11 16:22:45 -0600 | [diff] [blame] | 1499 | command = "/sbin/fsck.fat -y " + Actual_Block_Device; |
Ethan Yonker | 87c7bac | 2014-05-25 21:41:08 -0500 | [diff] [blame] | 1500 | LOGINFO("Repair command: %s\n", command.c_str()); |
| 1501 | if (TWFunc::Exec_Cmd(command) == 0) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1502 | gui_msg("done=Done."); |
Ethan Yonker | 87c7bac | 2014-05-25 21:41:08 -0500 | [diff] [blame] | 1503 | return true; |
| 1504 | } else { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1505 | gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name)); |
Ethan Yonker | 87c7bac | 2014-05-25 21:41:08 -0500 | [diff] [blame] | 1506 | return false; |
| 1507 | } |
| 1508 | } |
| 1509 | if (Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") { |
| 1510 | if (!TWFunc::Path_Exists("/sbin/e2fsck")) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1511 | gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("e2fsck")); |
Ethan Yonker | 87c7bac | 2014-05-25 21:41:08 -0500 | [diff] [blame] | 1512 | return false; |
| 1513 | } |
| 1514 | if (!UnMount(true)) |
| 1515 | return false; |
Greg Wallace | 2d65340 | 2015-12-29 14:19:21 -0500 | [diff] [blame] | 1516 | gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("e2fsck")); |
Ethan Yonker | 87c7bac | 2014-05-25 21:41:08 -0500 | [diff] [blame] | 1517 | Find_Actual_Block_Device(); |
Ethan Yonker | a271915 | 2015-05-28 09:44:41 -0500 | [diff] [blame] | 1518 | command = "/sbin/e2fsck -fp " + Actual_Block_Device; |
Ethan Yonker | 87c7bac | 2014-05-25 21:41:08 -0500 | [diff] [blame] | 1519 | LOGINFO("Repair command: %s\n", command.c_str()); |
| 1520 | if (TWFunc::Exec_Cmd(command) == 0) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1521 | gui_msg("done=Done."); |
Ethan Yonker | 87c7bac | 2014-05-25 21:41:08 -0500 | [diff] [blame] | 1522 | return true; |
| 1523 | } else { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1524 | gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name)); |
Ethan Yonker | 87c7bac | 2014-05-25 21:41:08 -0500 | [diff] [blame] | 1525 | return false; |
| 1526 | } |
| 1527 | } |
| 1528 | if (Current_File_System == "exfat") { |
| 1529 | if (!TWFunc::Path_Exists("/sbin/fsck.exfat")) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1530 | gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.exfat")); |
Ethan Yonker | 87c7bac | 2014-05-25 21:41:08 -0500 | [diff] [blame] | 1531 | return false; |
| 1532 | } |
| 1533 | if (!UnMount(true)) |
| 1534 | return false; |
Greg Wallace | 2d65340 | 2015-12-29 14:19:21 -0500 | [diff] [blame] | 1535 | gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.exfat")); |
Ethan Yonker | 87c7bac | 2014-05-25 21:41:08 -0500 | [diff] [blame] | 1536 | Find_Actual_Block_Device(); |
| 1537 | command = "/sbin/fsck.exfat " + Actual_Block_Device; |
| 1538 | LOGINFO("Repair command: %s\n", command.c_str()); |
| 1539 | if (TWFunc::Exec_Cmd(command) == 0) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1540 | gui_msg("done=Done."); |
Ethan Yonker | 87c7bac | 2014-05-25 21:41:08 -0500 | [diff] [blame] | 1541 | return true; |
| 1542 | } else { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1543 | gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name)); |
Ethan Yonker | 87c7bac | 2014-05-25 21:41:08 -0500 | [diff] [blame] | 1544 | return false; |
| 1545 | } |
| 1546 | } |
| 1547 | if (Current_File_System == "f2fs") { |
| 1548 | if (!TWFunc::Path_Exists("/sbin/fsck.f2fs")) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1549 | gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.f2fs")); |
Ethan Yonker | 87c7bac | 2014-05-25 21:41:08 -0500 | [diff] [blame] | 1550 | return false; |
| 1551 | } |
| 1552 | if (!UnMount(true)) |
| 1553 | return false; |
Greg Wallace | 2d65340 | 2015-12-29 14:19:21 -0500 | [diff] [blame] | 1554 | gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.f2fs")); |
Ethan Yonker | 87c7bac | 2014-05-25 21:41:08 -0500 | [diff] [blame] | 1555 | Find_Actual_Block_Device(); |
| 1556 | command = "/sbin/fsck.f2fs " + Actual_Block_Device; |
| 1557 | LOGINFO("Repair command: %s\n", command.c_str()); |
| 1558 | if (TWFunc::Exec_Cmd(command) == 0) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1559 | gui_msg("done=Done."); |
Ethan Yonker | 87c7bac | 2014-05-25 21:41:08 -0500 | [diff] [blame] | 1560 | return true; |
| 1561 | } else { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1562 | gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name)); |
Ethan Yonker | 87c7bac | 2014-05-25 21:41:08 -0500 | [diff] [blame] | 1563 | return false; |
| 1564 | } |
| 1565 | } |
Ethan Yonker | b81d905 | 2015-07-09 13:20:53 -0500 | [diff] [blame] | 1566 | if (Current_File_System == "ntfs") { |
Greg Wallace | b39e6c6 | 2015-12-29 00:55:26 -0500 | [diff] [blame] | 1567 | string Ntfsfix_Binary; |
| 1568 | if (TWFunc::Path_Exists("/sbin/ntfsfix")) |
| 1569 | Ntfsfix_Binary = "ntfsfix"; |
| 1570 | else if (TWFunc::Path_Exists("/sbin/fsck.ntfs")) |
| 1571 | Ntfsfix_Binary = "fsck.ntfs"; |
| 1572 | else { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1573 | gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("ntfsfix")); |
Ethan Yonker | b81d905 | 2015-07-09 13:20:53 -0500 | [diff] [blame] | 1574 | return false; |
| 1575 | } |
| 1576 | if (!UnMount(true)) |
| 1577 | return false; |
Greg Wallace | 2d65340 | 2015-12-29 14:19:21 -0500 | [diff] [blame] | 1578 | gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)(Ntfsfix_Binary)); |
Ethan Yonker | b81d905 | 2015-07-09 13:20:53 -0500 | [diff] [blame] | 1579 | Find_Actual_Block_Device(); |
Greg Wallace | b39e6c6 | 2015-12-29 00:55:26 -0500 | [diff] [blame] | 1580 | command = "/sbin/" + Ntfsfix_Binary + " " + Actual_Block_Device; |
Ethan Yonker | b81d905 | 2015-07-09 13:20:53 -0500 | [diff] [blame] | 1581 | LOGINFO("Repair command: %s\n", command.c_str()); |
| 1582 | if (TWFunc::Exec_Cmd(command) == 0) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1583 | gui_msg("done=Done."); |
Ethan Yonker | b81d905 | 2015-07-09 13:20:53 -0500 | [diff] [blame] | 1584 | return true; |
| 1585 | } else { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1586 | gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name)); |
Ethan Yonker | b81d905 | 2015-07-09 13:20:53 -0500 | [diff] [blame] | 1587 | return false; |
| 1588 | } |
| 1589 | } |
Ethan Yonker | 87c7bac | 2014-05-25 21:41:08 -0500 | [diff] [blame] | 1590 | return false; |
| 1591 | } |
| 1592 | |
Ethan Yonker | a271915 | 2015-05-28 09:44:41 -0500 | [diff] [blame] | 1593 | bool TWPartition::Can_Resize() { |
| 1594 | if (Mount_Read_Only) |
| 1595 | return false; |
| 1596 | if ((Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") && TWFunc::Path_Exists("/sbin/resize2fs")) |
| 1597 | return true; |
| 1598 | return false; |
| 1599 | } |
| 1600 | |
| 1601 | bool TWPartition::Resize() { |
| 1602 | string command; |
| 1603 | |
| 1604 | if (Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") { |
| 1605 | if (!Can_Repair()) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1606 | LOGINFO("Cannot resize %s because %s cannot be repaired before resizing.\n", Display_Name.c_str(), Display_Name.c_str()); |
| 1607 | gui_msg(Msg(msg::kError, "cannot_resize=Cannot resize {1}.")(Display_Name)); |
Ethan Yonker | a271915 | 2015-05-28 09:44:41 -0500 | [diff] [blame] | 1608 | return false; |
| 1609 | } |
| 1610 | if (!TWFunc::Path_Exists("/sbin/resize2fs")) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1611 | LOGINFO("resize2fs does not exist! Cannot resize!\n"); |
| 1612 | gui_msg(Msg(msg::kError, "cannot_resize=Cannot resize {1}.")(Display_Name)); |
Ethan Yonker | a271915 | 2015-05-28 09:44:41 -0500 | [diff] [blame] | 1613 | return false; |
| 1614 | } |
| 1615 | // Repair will unmount so no need to do it twice |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1616 | gui_msg(Msg("repair_resize=Repairing {1} before resizing.")( Display_Name)); |
Ethan Yonker | a271915 | 2015-05-28 09:44:41 -0500 | [diff] [blame] | 1617 | if (!Repair()) |
| 1618 | return false; |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1619 | gui_msg(Msg("resizing=Resizing {1} using {2}...")(Display_Name)("resize2fs")); |
Ethan Yonker | a271915 | 2015-05-28 09:44:41 -0500 | [diff] [blame] | 1620 | Find_Actual_Block_Device(); |
| 1621 | command = "/sbin/resize2fs " + Actual_Block_Device; |
| 1622 | if (Length != 0) { |
Ethan Yonker | d18a821 | 2015-12-14 10:17:00 -0600 | [diff] [blame] | 1623 | unsigned long long Actual_Size = IOCTL_Get_Block_Size(); |
| 1624 | if (Actual_Size == 0) |
| 1625 | return false; |
Ethan Yonker | a271915 | 2015-05-28 09:44:41 -0500 | [diff] [blame] | 1626 | |
Ethan Yonker | a271915 | 2015-05-28 09:44:41 -0500 | [diff] [blame] | 1627 | unsigned long long Block_Count; |
| 1628 | if (Length < 0) { |
| 1629 | // Reduce overall size by this length |
| 1630 | Block_Count = (Actual_Size / 1024LLU) - ((unsigned long long)(Length * -1) / 1024LLU); |
| 1631 | } else { |
| 1632 | // This is the size, not a size reduction |
| 1633 | Block_Count = ((unsigned long long)(Length) / 1024LLU); |
| 1634 | } |
| 1635 | char temp[256]; |
| 1636 | sprintf(temp, "%llu", Block_Count); |
| 1637 | command += " "; |
| 1638 | command += temp; |
| 1639 | command += "K"; |
| 1640 | } |
| 1641 | LOGINFO("Resize command: %s\n", command.c_str()); |
| 1642 | if (TWFunc::Exec_Cmd(command) == 0) { |
| 1643 | Update_Size(true); |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1644 | gui_msg("done=Done."); |
Ethan Yonker | a271915 | 2015-05-28 09:44:41 -0500 | [diff] [blame] | 1645 | return true; |
| 1646 | } else { |
| 1647 | Update_Size(true); |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1648 | gui_msg(Msg(msg::kError, "unable_resize=Unable to resize {1}.")(Display_Name)); |
Ethan Yonker | a271915 | 2015-05-28 09:44:41 -0500 | [diff] [blame] | 1649 | return false; |
| 1650 | } |
| 1651 | } |
| 1652 | return false; |
| 1653 | } |
| 1654 | |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 1655 | bool TWPartition::Backup(PartitionSettings *part_settings, pid_t *tar_fork_pid) { |
| 1656 | if (Backup_Method == BM_FILES) |
| 1657 | return Backup_Tar(part_settings, tar_fork_pid); |
| 1658 | else if (Backup_Method == BM_DD) |
| 1659 | return Backup_Image(part_settings); |
| 1660 | else if (Backup_Method == BM_FLASH_UTILS) |
| 1661 | return Backup_Dump_Image(part_settings); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1662 | LOGERR("Unknown backup method for '%s'\n", Mount_Point.c_str()); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1663 | return false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1664 | } |
| 1665 | |
James Christopher Adduono | 79ae093 | 2016-10-25 02:18:32 -0400 | [diff] [blame] | 1666 | bool TWPartition::Check_Restore_File_MD5(const string& Filename) { |
| 1667 | twrpDigest md5sum; |
| 1668 | |
| 1669 | md5sum.setfn(Filename); |
| 1670 | switch (md5sum.verify_md5digest()) { |
| 1671 | case MD5_OK: |
| 1672 | gui_msg(Msg("md5_matched=MD5 matched for '{1}'.")(Filename)); |
| 1673 | return true; |
| 1674 | case MD5_FILE_UNREADABLE: |
| 1675 | case MD5_NOT_FOUND: |
| 1676 | gui_msg(Msg(msg::kError, "no_md5_found=No md5 file found for '{1}'. Please unselect Enable MD5 verification to restore.")(Filename)); |
| 1677 | break; |
| 1678 | case MD5_MATCH_FAIL: |
| 1679 | gui_msg(Msg(msg::kError, "md5_fail_match=MD5 failed to match on '{1}'.")(Filename)); |
| 1680 | break; |
| 1681 | } |
| 1682 | return false; |
| 1683 | } |
| 1684 | |
bigbiff bigbiff | 8fd4b09 | 2016-08-30 20:48:53 -0400 | [diff] [blame] | 1685 | bool TWPartition::Check_MD5(PartitionSettings *part_settings) { |
James Christopher Adduono | 79ae093 | 2016-10-25 02:18:32 -0400 | [diff] [blame] | 1686 | string Full_Filename; |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 1687 | char split_filename[512]; |
| 1688 | int index = 0; |
| 1689 | |
Captain Throwback | ff5935f | 2014-09-23 16:08:34 -0400 | [diff] [blame] | 1690 | sync(); |
| 1691 | |
Ethan Yonker | dcf2b67 | 2016-09-13 14:41:53 -0500 | [diff] [blame] | 1692 | Full_Filename = part_settings->Backup_Folder + "/" + Backup_FileName; |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 1693 | if (!TWFunc::Path_Exists(Full_Filename)) { |
| 1694 | // This is a split archive, we presume |
James Christopher Adduono | 79ae093 | 2016-10-25 02:18:32 -0400 | [diff] [blame] | 1695 | memset(split_filename, 0, sizeof(split_filename)); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 1696 | while (index < 1000) { |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 1697 | sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index); |
James Christopher Adduono | 79ae093 | 2016-10-25 02:18:32 -0400 | [diff] [blame] | 1698 | if (!TWFunc::Path_Exists(split_filename)) |
| 1699 | break; |
| 1700 | LOGINFO("split_filename: %s\n", split_filename); |
| 1701 | if (!Check_Restore_File_MD5(split_filename)) |
| 1702 | return false; |
| 1703 | index++; |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 1704 | } |
Dees_Troy | 4a2a126 | 2012-09-18 09:33:47 -0400 | [diff] [blame] | 1705 | return true; |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 1706 | } |
James Christopher Adduono | 79ae093 | 2016-10-25 02:18:32 -0400 | [diff] [blame] | 1707 | return Check_Restore_File_MD5(Full_Filename); // Single file archive |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 1708 | } |
| 1709 | |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 1710 | bool TWPartition::Restore(PartitionSettings *part_settings) { |
Matt Mower | 3c36697 | 2015-12-25 19:28:31 -0600 | [diff] [blame] | 1711 | TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, gui_parse_text("{@restoring_hdr}")); |
Ethan Yonker | e080c1f | 2016-09-19 13:50:25 -0500 | [diff] [blame] | 1712 | LOGINFO("Restore filename is: %s/%s\n", part_settings->Backup_Folder.c_str(), Backup_FileName.c_str()); |
Gary Peck | 43acadf | 2012-11-21 21:19:01 -0800 | [diff] [blame] | 1713 | |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 1714 | string Restore_File_System = Get_Restore_File_System(part_settings); |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 1715 | |
| 1716 | if (Is_File_System(Restore_File_System)) |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 1717 | return Restore_Tar(part_settings); |
| 1718 | else if (Is_Image(Restore_File_System)) |
| 1719 | return Restore_Image(part_settings); |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 1720 | |
| 1721 | LOGERR("Unknown restore method for '%s'\n", Mount_Point.c_str()); |
| 1722 | return false; |
| 1723 | } |
| 1724 | |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 1725 | string TWPartition::Get_Restore_File_System(PartitionSettings *part_settings) { |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 1726 | size_t first_period, second_period; |
| 1727 | string Restore_File_System; |
| 1728 | |
Gary Peck | 43acadf | 2012-11-21 21:19:01 -0800 | [diff] [blame] | 1729 | // Parse backup filename to extract the file system before wiping |
Ethan Yonker | dcf2b67 | 2016-09-13 14:41:53 -0500 | [diff] [blame] | 1730 | first_period = Backup_FileName.find("."); |
Gary Peck | 43acadf | 2012-11-21 21:19:01 -0800 | [diff] [blame] | 1731 | if (first_period == string::npos) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1732 | LOGERR("Unable to find file system (first period).\n"); |
that | d43bf2d | 2014-09-21 23:13:02 +0200 | [diff] [blame] | 1733 | return string(); |
Gary Peck | 43acadf | 2012-11-21 21:19:01 -0800 | [diff] [blame] | 1734 | } |
Ethan Yonker | dcf2b67 | 2016-09-13 14:41:53 -0500 | [diff] [blame] | 1735 | Restore_File_System = Backup_FileName.substr(first_period + 1, Backup_FileName.size() - first_period - 1); |
Gary Peck | 43acadf | 2012-11-21 21:19:01 -0800 | [diff] [blame] | 1736 | second_period = Restore_File_System.find("."); |
| 1737 | if (second_period == string::npos) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1738 | LOGERR("Unable to find file system (second period).\n"); |
that | d43bf2d | 2014-09-21 23:13:02 +0200 | [diff] [blame] | 1739 | return string(); |
Gary Peck | 43acadf | 2012-11-21 21:19:01 -0800 | [diff] [blame] | 1740 | } |
| 1741 | Restore_File_System.resize(second_period); |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 1742 | return Restore_File_System; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1743 | } |
| 1744 | |
| 1745 | string TWPartition::Backup_Method_By_Name() { |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 1746 | if (Backup_Method == BM_NONE) |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1747 | return "none"; |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 1748 | else if (Backup_Method == BM_FILES) |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1749 | return "files"; |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 1750 | else if (Backup_Method == BM_DD) |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1751 | return "dd"; |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 1752 | else if (Backup_Method == BM_FLASH_UTILS) |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1753 | return "flash_utils"; |
| 1754 | else |
| 1755 | return "undefined"; |
| 1756 | return "ERROR!"; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1757 | } |
| 1758 | |
| 1759 | bool TWPartition::Decrypt(string Password) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1760 | LOGINFO("STUB TWPartition::Decrypt, password: '%s'\n", Password.c_str()); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1761 | // Is this needed? |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1762 | return 1; |
| 1763 | } |
| 1764 | |
| 1765 | bool TWPartition::Wipe_Encryption() { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1766 | bool Save_Data_Media = Has_Data_Media; |
| 1767 | |
| 1768 | if (!UnMount(true)) |
| 1769 | return false; |
| 1770 | |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1771 | Has_Data_Media = false; |
Dees_Troy | 74fb2e9 | 2013-04-15 14:35:47 +0000 | [diff] [blame] | 1772 | Decrypted_Block_Device = ""; |
Ethan Yonker | d79d9bc | 2014-12-20 15:38:29 -0600 | [diff] [blame] | 1773 | #ifdef TW_INCLUDE_CRYPTO |
Ethan Yonker | 1b19016 | 2016-12-05 15:25:19 -0600 | [diff] [blame] | 1774 | if (Is_Decrypted && !Decrypted_Block_Device.empty()) { |
Ethan Yonker | d79d9bc | 2014-12-20 15:38:29 -0600 | [diff] [blame] | 1775 | if (!UnMount(true)) |
| 1776 | return false; |
Matt Mower | 2b18a53 | 2015-02-20 16:58:05 -0600 | [diff] [blame] | 1777 | if (delete_crypto_blk_dev((char*)("userdata")) != 0) { |
Ethan Yonker | d79d9bc | 2014-12-20 15:38:29 -0600 | [diff] [blame] | 1778 | LOGERR("Error deleting crypto block device, continuing anyway.\n"); |
| 1779 | } |
| 1780 | } |
| 1781 | #endif |
Dees_Troy | 74fb2e9 | 2013-04-15 14:35:47 +0000 | [diff] [blame] | 1782 | Is_Decrypted = false; |
| 1783 | Is_Encrypted = false; |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 1784 | Find_Actual_Block_Device(); |
Ethan Yonker | bc85b63 | 2015-08-09 12:48:14 -0500 | [diff] [blame] | 1785 | if (Crypto_Key_Location == "footer") { |
| 1786 | int newlen, fd; |
| 1787 | if (Length != 0) { |
| 1788 | newlen = Length; |
| 1789 | if (newlen < 0) |
| 1790 | newlen = newlen * -1; |
| 1791 | } else { |
| 1792 | newlen = CRYPT_FOOTER_OFFSET; |
| 1793 | } |
| 1794 | if ((fd = open(Actual_Block_Device.c_str(), O_RDWR)) < 0) { |
| 1795 | gui_print_color("warning", "Unable to open '%s' to wipe crypto key\n", Actual_Block_Device.c_str()); |
| 1796 | } else { |
| 1797 | unsigned int block_count; |
| 1798 | if ((ioctl(fd, BLKGETSIZE, &block_count)) == -1) { |
| 1799 | gui_print_color("warning", "Unable to get block size for wiping crypto footer.\n"); |
| 1800 | } else { |
| 1801 | off64_t offset = ((off64_t)block_count * 512) - newlen; |
| 1802 | if (lseek64(fd, offset, SEEK_SET) == -1) { |
| 1803 | gui_print_color("warning", "Unable to lseek64 for wiping crypto footer.\n"); |
| 1804 | } else { |
| 1805 | void* buffer = malloc(newlen); |
| 1806 | if (!buffer) { |
| 1807 | gui_print_color("warning", "Failed to malloc for wiping crypto footer.\n"); |
| 1808 | } else { |
| 1809 | memset(buffer, 0, newlen); |
| 1810 | int ret = write(fd, buffer, newlen); |
| 1811 | if (ret != newlen) { |
| 1812 | gui_print_color("warning", "Failed to wipe crypto footer.\n"); |
| 1813 | } else { |
| 1814 | LOGINFO("Successfully wiped crypto footer.\n"); |
| 1815 | } |
Matt Mower | 13a8f0b | 2015-09-26 15:40:03 -0500 | [diff] [blame] | 1816 | free(buffer); |
Ethan Yonker | bc85b63 | 2015-08-09 12:48:14 -0500 | [diff] [blame] | 1817 | } |
| 1818 | } |
| 1819 | } |
| 1820 | close(fd); |
| 1821 | } |
| 1822 | } else { |
Ethan Yonker | c2dafbb | 2016-03-15 22:20:59 -0500 | [diff] [blame] | 1823 | if (TWFunc::IOCTL_Get_Block_Size(Crypto_Key_Location.c_str()) >= 16384LLU) { |
| 1824 | string Command = "dd of='" + Crypto_Key_Location + "' if=/dev/zero bs=16384 count=1"; |
| 1825 | TWFunc::Exec_Cmd(Command); |
| 1826 | } else { |
| 1827 | LOGINFO("Crypto key location reports size < 16K so not wiping crypto footer.\n"); |
| 1828 | } |
Ethan Yonker | bc85b63 | 2015-08-09 12:48:14 -0500 | [diff] [blame] | 1829 | } |
Gary Peck | e8bc5d7 | 2012-12-21 06:45:25 -0800 | [diff] [blame] | 1830 | if (Wipe(Fstab_File_System)) { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1831 | Has_Data_Media = Save_Data_Media; |
| 1832 | if (Has_Data_Media && !Symlink_Mount_Point.empty()) { |
| 1833 | Recreate_Media_Folder(); |
Ethan Yonker | 726a020 | 2014-12-16 20:01:38 -0600 | [diff] [blame] | 1834 | if (Mount(false)) |
| 1835 | PartitionManager.Add_MTP_Storage(MTP_Storage_ID); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1836 | } |
Ethan Yonker | d79d9bc | 2014-12-20 15:38:29 -0600 | [diff] [blame] | 1837 | DataManager::SetValue(TW_IS_ENCRYPTED, 0); |
Ethan Yonker | 83e8257 | 2014-04-04 10:59:28 -0500 | [diff] [blame] | 1838 | #ifndef TW_OEM_BUILD |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1839 | gui_msg("format_data_msg=You may need to reboot recovery to be able to use /data again."); |
Ethan Yonker | 83e8257 | 2014-04-04 10:59:28 -0500 | [diff] [blame] | 1840 | #endif |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1841 | return true; |
| 1842 | } else { |
| 1843 | Has_Data_Media = Save_Data_Media; |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1844 | gui_err("format_data_err=Unable to format to remove encryption."); |
Ethan Yonker | 726a020 | 2014-12-16 20:01:38 -0600 | [diff] [blame] | 1845 | if (Has_Data_Media && Mount(false)) |
| 1846 | PartitionManager.Add_MTP_Storage(MTP_Storage_ID); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1847 | return false; |
| 1848 | } |
| 1849 | return false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1850 | } |
| 1851 | |
| 1852 | void TWPartition::Check_FS_Type() { |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 1853 | const char* type; |
| 1854 | blkid_probe pr; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1855 | |
Dees_Troy | 68cab49 | 2012-12-12 19:29:35 +0000 | [diff] [blame] | 1856 | if (Fstab_File_System == "yaffs2" || Fstab_File_System == "mtd" || Fstab_File_System == "bml" || Ignore_Blkid) |
| 1857 | 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] | 1858 | |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1859 | Find_Actual_Block_Device(); |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 1860 | if (!Is_Present) |
| 1861 | return; |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 1862 | |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 1863 | pr = blkid_new_probe_from_filename(Actual_Block_Device.c_str()); |
| 1864 | if (blkid_do_fullprobe(pr)) { |
| 1865 | blkid_free_probe(pr); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1866 | LOGINFO("Can't probe device %s\n", Actual_Block_Device.c_str()); |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 1867 | return; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1868 | } |
Vojtech Bocek | 4d4b336 | 2013-06-24 22:46:13 +0200 | [diff] [blame] | 1869 | |
Matt Mower | fb1c4ff | 2014-04-16 13:43:36 -0500 | [diff] [blame] | 1870 | if (blkid_probe_lookup_value(pr, "TYPE", &type, NULL) < 0) { |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 1871 | blkid_free_probe(pr); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1872 | 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] | 1873 | return; |
| 1874 | } |
Vojtech Bocek | 4d4b336 | 2013-06-24 22:46:13 +0200 | [diff] [blame] | 1875 | |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 1876 | Current_File_System = type; |
Vojtech Bocek | 4d4b336 | 2013-06-24 22:46:13 +0200 | [diff] [blame] | 1877 | blkid_free_probe(pr); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1878 | } |
| 1879 | |
Gary Peck | 43acadf | 2012-11-21 21:19:01 -0800 | [diff] [blame] | 1880 | bool TWPartition::Wipe_EXT23(string File_System) { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1881 | if (!UnMount(true)) |
| 1882 | return false; |
| 1883 | |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 1884 | if (TWFunc::Path_Exists("/sbin/mke2fs")) { |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 1885 | string command; |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1886 | |
Greg Wallace | 4b44fef | 2015-12-29 15:33:24 -0500 | [diff] [blame] | 1887 | gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mke2fs")); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1888 | Find_Actual_Block_Device(); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1889 | command = "mke2fs -t " + File_System + " -m 0 " + Actual_Block_Device; |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1890 | LOGINFO("mke2fs command: %s\n", command.c_str()); |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 1891 | if (TWFunc::Exec_Cmd(command) == 0) { |
Gary Peck | e8bc5d7 | 2012-12-21 06:45:25 -0800 | [diff] [blame] | 1892 | Current_File_System = File_System; |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 1893 | Recreate_AndSec_Folder(); |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1894 | gui_msg("done=Done."); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1895 | return true; |
| 1896 | } else { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1897 | gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name)); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1898 | return false; |
| 1899 | } |
| 1900 | } else |
| 1901 | return Wipe_RMRF(); |
| 1902 | |
| 1903 | return false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1904 | } |
| 1905 | |
| 1906 | bool TWPartition::Wipe_EXT4() { |
Ethan Yonker | 25f20c1 | 2014-10-14 09:04:43 -0500 | [diff] [blame] | 1907 | Find_Actual_Block_Device(); |
| 1908 | if (!Is_Present) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1909 | LOGINFO("Block device not present, cannot wipe %s.\n", Display_Name.c_str()); |
| 1910 | gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name)); |
Ethan Yonker | 25f20c1 | 2014-10-14 09:04:43 -0500 | [diff] [blame] | 1911 | return false; |
| 1912 | } |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1913 | if (!UnMount(true)) |
| 1914 | return false; |
| 1915 | |
Dees_Troy | b3265ab | 2013-08-30 02:59:14 +0000 | [diff] [blame] | 1916 | #if defined(HAVE_SELINUX) && defined(USE_EXT4) |
Ethan Yonker | f27497f | 2014-02-09 11:48:33 -0600 | [diff] [blame] | 1917 | int ret; |
| 1918 | char *secontext = NULL; |
| 1919 | |
Greg Wallace | 4b44fef | 2015-12-29 15:33:24 -0500 | [diff] [blame] | 1920 | gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("make_ext4fs")); |
Ethan Yonker | f27497f | 2014-02-09 11:48:33 -0600 | [diff] [blame] | 1921 | |
Dees Troy | 99c8dbf | 2014-03-10 16:53:58 +0000 | [diff] [blame] | 1922 | if (!selinux_handle || selabel_lookup(selinux_handle, &secontext, Mount_Point.c_str(), S_IFDIR) < 0) { |
Ethan Yonker | f27497f | 2014-02-09 11:48:33 -0600 | [diff] [blame] | 1923 | LOGINFO("Cannot lookup security context for '%s'\n", Mount_Point.c_str()); |
| 1924 | ret = make_ext4fs(Actual_Block_Device.c_str(), Length, Mount_Point.c_str(), NULL); |
| 1925 | } else { |
| 1926 | ret = make_ext4fs(Actual_Block_Device.c_str(), Length, Mount_Point.c_str(), selinux_handle); |
| 1927 | } |
| 1928 | if (ret != 0) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1929 | gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name)); |
Dees_Troy | a95f55c | 2013-08-17 13:14:43 +0000 | [diff] [blame] | 1930 | return false; |
| 1931 | } else { |
bigbiff bigbiff | c49d706 | 2013-10-11 20:28:00 -0400 | [diff] [blame] | 1932 | string sedir = Mount_Point + "/lost+found"; |
| 1933 | PartitionManager.Mount_By_Path(sedir.c_str(), true); |
| 1934 | rmdir(sedir.c_str()); |
| 1935 | mkdir(sedir.c_str(), S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP); |
Dees_Troy | a95f55c | 2013-08-17 13:14:43 +0000 | [diff] [blame] | 1936 | return true; |
| 1937 | } |
| 1938 | #else |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 1939 | if (TWFunc::Path_Exists("/sbin/make_ext4fs")) { |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 1940 | string Command; |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1941 | |
Greg Wallace | 4b44fef | 2015-12-29 15:33:24 -0500 | [diff] [blame] | 1942 | gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("make_ext4fs")); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1943 | Find_Actual_Block_Device(); |
| 1944 | Command = "make_ext4fs"; |
| 1945 | if (!Is_Decrypted && Length != 0) { |
| 1946 | // Only use length if we're not decrypted |
| 1947 | char len[32]; |
| 1948 | sprintf(len, "%i", Length); |
| 1949 | Command += " -l "; |
| 1950 | Command += len; |
| 1951 | } |
Dees_Troy | 5295d58 | 2013-09-06 15:51:08 +0000 | [diff] [blame] | 1952 | if (TWFunc::Path_Exists("/file_contexts")) { |
| 1953 | Command += " -S /file_contexts"; |
| 1954 | } |
| 1955 | Command += " -a " + Mount_Point + " " + Actual_Block_Device; |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1956 | LOGINFO("make_ext4fs command: %s\n", Command.c_str()); |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 1957 | if (TWFunc::Exec_Cmd(Command) == 0) { |
Gary Peck | e8bc5d7 | 2012-12-21 06:45:25 -0800 | [diff] [blame] | 1958 | Current_File_System = "ext4"; |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 1959 | Recreate_AndSec_Folder(); |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1960 | gui_msg("done=Done."); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1961 | return true; |
| 1962 | } else { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1963 | gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name)); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1964 | return false; |
| 1965 | } |
| 1966 | } else |
Gary Peck | 43acadf | 2012-11-21 21:19:01 -0800 | [diff] [blame] | 1967 | return Wipe_EXT23("ext4"); |
Dees_Troy | a95f55c | 2013-08-17 13:14:43 +0000 | [diff] [blame] | 1968 | #endif |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1969 | return false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1970 | } |
| 1971 | |
| 1972 | bool TWPartition::Wipe_FAT() { |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 1973 | string command; |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1974 | |
Matt Mower | 18794c8 | 2015-11-11 16:22:45 -0600 | [diff] [blame] | 1975 | if (TWFunc::Path_Exists("/sbin/mkfs.fat")) { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1976 | if (!UnMount(true)) |
| 1977 | return false; |
| 1978 | |
Greg Wallace | 4b44fef | 2015-12-29 15:33:24 -0500 | [diff] [blame] | 1979 | gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mkfs.fat")); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1980 | Find_Actual_Block_Device(); |
Matt Mower | 18794c8 | 2015-11-11 16:22:45 -0600 | [diff] [blame] | 1981 | command = "mkfs.fat " + Actual_Block_Device; |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 1982 | if (TWFunc::Exec_Cmd(command) == 0) { |
Gary Peck | e8bc5d7 | 2012-12-21 06:45:25 -0800 | [diff] [blame] | 1983 | Current_File_System = "vfat"; |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 1984 | Recreate_AndSec_Folder(); |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1985 | gui_msg("done=Done."); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1986 | return true; |
| 1987 | } else { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1988 | gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name)); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1989 | return false; |
| 1990 | } |
| 1991 | return true; |
| 1992 | } |
| 1993 | else |
| 1994 | return Wipe_RMRF(); |
| 1995 | |
| 1996 | return false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1997 | } |
| 1998 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1999 | bool TWPartition::Wipe_EXFAT() { |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 2000 | string command; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 2001 | |
| 2002 | if (TWFunc::Path_Exists("/sbin/mkexfatfs")) { |
| 2003 | if (!UnMount(true)) |
| 2004 | return false; |
| 2005 | |
Greg Wallace | 4b44fef | 2015-12-29 15:33:24 -0500 | [diff] [blame] | 2006 | gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mkexfatfs")); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 2007 | Find_Actual_Block_Device(); |
| 2008 | command = "mkexfatfs " + Actual_Block_Device; |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 2009 | if (TWFunc::Exec_Cmd(command) == 0) { |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 2010 | Recreate_AndSec_Folder(); |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 2011 | gui_msg("done=Done."); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 2012 | return true; |
| 2013 | } else { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 2014 | gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name)); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 2015 | return false; |
| 2016 | } |
| 2017 | return true; |
| 2018 | } |
| 2019 | return false; |
| 2020 | } |
| 2021 | |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 2022 | bool TWPartition::Wipe_MTD() { |
| 2023 | if (!UnMount(true)) |
| 2024 | return false; |
| 2025 | |
Greg Wallace | 4b44fef | 2015-12-29 15:33:24 -0500 | [diff] [blame] | 2026 | gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("MTD")); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 2027 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 2028 | mtd_scan_partitions(); |
| 2029 | const MtdPartition* mtd = mtd_find_partition_by_name(MTD_Name.c_str()); |
| 2030 | if (mtd == NULL) { |
| 2031 | LOGERR("No mtd partition named '%s'", MTD_Name.c_str()); |
| 2032 | return false; |
| 2033 | } |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 2034 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 2035 | MtdWriteContext* ctx = mtd_write_partition(mtd); |
| 2036 | if (ctx == NULL) { |
| 2037 | LOGERR("Can't write '%s', failed to format.", MTD_Name.c_str()); |
| 2038 | return false; |
| 2039 | } |
| 2040 | if (mtd_erase_blocks(ctx, -1) == -1) { |
| 2041 | mtd_write_close(ctx); |
| 2042 | LOGERR("Failed to format '%s'", MTD_Name.c_str()); |
| 2043 | return false; |
| 2044 | } |
| 2045 | if (mtd_write_close(ctx) != 0) { |
| 2046 | LOGERR("Failed to close '%s'", MTD_Name.c_str()); |
| 2047 | return false; |
| 2048 | } |
Gary Peck | e8bc5d7 | 2012-12-21 06:45:25 -0800 | [diff] [blame] | 2049 | Current_File_System = "yaffs2"; |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 2050 | Recreate_AndSec_Folder(); |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 2051 | gui_msg("done=Done."); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 2052 | return true; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 2053 | } |
| 2054 | |
| 2055 | bool TWPartition::Wipe_RMRF() { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 2056 | if (!Mount(true)) |
| 2057 | return false; |
Ethan Yonker | 726a020 | 2014-12-16 20:01:38 -0600 | [diff] [blame] | 2058 | // This is the only wipe that leaves the partition mounted, so we |
| 2059 | // must manually remove the partition from MTP if it is a storage |
| 2060 | // partition. |
| 2061 | if (Is_Storage) |
| 2062 | PartitionManager.Remove_MTP_Storage(MTP_Storage_ID); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 2063 | |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 2064 | gui_msg(Msg("remove_all=Removing all files under '{1}'")(Mount_Point)); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 2065 | TWFunc::removeDir(Mount_Point, true); |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 2066 | Recreate_AndSec_Folder(); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 2067 | return true; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 2068 | } |
| 2069 | |
Dees_Troy | e501704 | 2013-08-29 16:38:55 +0000 | [diff] [blame] | 2070 | bool TWPartition::Wipe_F2FS() { |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 2071 | string command; |
Dees_Troy | e501704 | 2013-08-29 16:38:55 +0000 | [diff] [blame] | 2072 | |
| 2073 | if (TWFunc::Path_Exists("/sbin/mkfs.f2fs")) { |
| 2074 | if (!UnMount(true)) |
| 2075 | return false; |
| 2076 | |
Greg Wallace | 4b44fef | 2015-12-29 15:33:24 -0500 | [diff] [blame] | 2077 | gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mkfs.f2fs")); |
Dees_Troy | e501704 | 2013-08-29 16:38:55 +0000 | [diff] [blame] | 2078 | Find_Actual_Block_Device(); |
Greg Wallace | 558aef7 | 2016-01-19 21:14:30 -0500 | [diff] [blame] | 2079 | command = "mkfs.f2fs -t 0"; |
dhacker29 | a3fa75f | 2015-01-17 19:42:33 -0500 | [diff] [blame] | 2080 | if (!Is_Decrypted && Length != 0) { |
| 2081 | // Only use length if we're not decrypted |
| 2082 | char len[32]; |
| 2083 | int mod_length = Length; |
| 2084 | if (Length < 0) |
| 2085 | mod_length *= -1; |
| 2086 | sprintf(len, "%i", mod_length); |
| 2087 | command += " -r "; |
| 2088 | command += len; |
| 2089 | } |
| 2090 | command += " " + Actual_Block_Device; |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 2091 | if (TWFunc::Exec_Cmd(command) == 0) { |
Dees_Troy | e501704 | 2013-08-29 16:38:55 +0000 | [diff] [blame] | 2092 | Recreate_AndSec_Folder(); |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 2093 | gui_msg("done=Done."); |
Dees_Troy | e501704 | 2013-08-29 16:38:55 +0000 | [diff] [blame] | 2094 | return true; |
| 2095 | } else { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 2096 | gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name)); |
Dees_Troy | e501704 | 2013-08-29 16:38:55 +0000 | [diff] [blame] | 2097 | return false; |
| 2098 | } |
| 2099 | return true; |
| 2100 | } else { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 2101 | LOGINFO("mkfs.f2fs binary not found, using rm -rf to wipe.\n"); |
Dees_Troy | e501704 | 2013-08-29 16:38:55 +0000 | [diff] [blame] | 2102 | return Wipe_RMRF(); |
| 2103 | } |
| 2104 | return false; |
| 2105 | } |
| 2106 | |
Ethan Yonker | b81d905 | 2015-07-09 13:20:53 -0500 | [diff] [blame] | 2107 | bool TWPartition::Wipe_NTFS() { |
| 2108 | string command; |
Greg Wallace | b39e6c6 | 2015-12-29 00:55:26 -0500 | [diff] [blame] | 2109 | string Ntfsmake_Binary; |
Ethan Yonker | b81d905 | 2015-07-09 13:20:53 -0500 | [diff] [blame] | 2110 | |
Greg Wallace | b39e6c6 | 2015-12-29 00:55:26 -0500 | [diff] [blame] | 2111 | if (TWFunc::Path_Exists("/sbin/mkntfs")) |
| 2112 | Ntfsmake_Binary = "mkntfs"; |
| 2113 | else if (TWFunc::Path_Exists("/sbin/mkfs.ntfs")) |
| 2114 | Ntfsmake_Binary = "mkfs.ntfs"; |
| 2115 | else |
| 2116 | return false; |
Ethan Yonker | b81d905 | 2015-07-09 13:20:53 -0500 | [diff] [blame] | 2117 | |
Greg Wallace | b39e6c6 | 2015-12-29 00:55:26 -0500 | [diff] [blame] | 2118 | if (!UnMount(true)) |
| 2119 | return false; |
| 2120 | |
Greg Wallace | 4b44fef | 2015-12-29 15:33:24 -0500 | [diff] [blame] | 2121 | gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)(Ntfsmake_Binary)); |
Greg Wallace | b39e6c6 | 2015-12-29 00:55:26 -0500 | [diff] [blame] | 2122 | Find_Actual_Block_Device(); |
| 2123 | command = "/sbin/" + Ntfsmake_Binary + " " + Actual_Block_Device; |
| 2124 | if (TWFunc::Exec_Cmd(command) == 0) { |
| 2125 | Recreate_AndSec_Folder(); |
| 2126 | gui_msg("done=Done."); |
Ethan Yonker | b81d905 | 2015-07-09 13:20:53 -0500 | [diff] [blame] | 2127 | return true; |
Greg Wallace | b39e6c6 | 2015-12-29 00:55:26 -0500 | [diff] [blame] | 2128 | } else { |
| 2129 | gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name)); |
| 2130 | return false; |
Ethan Yonker | b81d905 | 2015-07-09 13:20:53 -0500 | [diff] [blame] | 2131 | } |
| 2132 | return false; |
| 2133 | } |
| 2134 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 2135 | bool TWPartition::Wipe_Data_Without_Wiping_Media() { |
Ethan Yonker | 83e8257 | 2014-04-04 10:59:28 -0500 | [diff] [blame] | 2136 | #ifdef TW_OEM_BUILD |
| 2137 | // In an OEM Build we want to do a full format |
| 2138 | return Wipe_Encryption(); |
| 2139 | #else |
Ethan Yonker | 66a1949 | 2015-12-10 10:19:45 -0600 | [diff] [blame] | 2140 | bool ret = false; |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 2141 | |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 2142 | if (!Mount(true)) |
| 2143 | return false; |
| 2144 | |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 2145 | gui_msg("wiping_data=Wiping data without wiping /data/media ..."); |
Ethan Yonker | 66a1949 | 2015-12-10 10:19:45 -0600 | [diff] [blame] | 2146 | ret = Wipe_Data_Without_Wiping_Media_Func(Mount_Point + "/"); |
| 2147 | if (ret) |
| 2148 | gui_msg("done=Done."); |
| 2149 | return ret; |
| 2150 | #endif // ifdef TW_OEM_BUILD |
| 2151 | } |
| 2152 | |
| 2153 | bool TWPartition::Wipe_Data_Without_Wiping_Media_Func(const string& parent __unused) { |
| 2154 | string dir; |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 2155 | |
| 2156 | DIR* d; |
Ethan Yonker | 66a1949 | 2015-12-10 10:19:45 -0600 | [diff] [blame] | 2157 | d = opendir(parent.c_str()); |
Dees_Troy | 16b7435 | 2012-11-14 22:27:31 +0000 | [diff] [blame] | 2158 | if (d != NULL) { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 2159 | struct dirent* de; |
| 2160 | while ((de = readdir(d)) != NULL) { |
bigbiff bigbiff | 34684ff | 2013-12-01 21:03:45 -0500 | [diff] [blame] | 2161 | if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) continue; |
bigbiff bigbiff | c7360dd | 2014-01-25 15:02:57 -0500 | [diff] [blame] | 2162 | |
Ethan Yonker | 66a1949 | 2015-12-10 10:19:45 -0600 | [diff] [blame] | 2163 | dir = parent; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 2164 | dir.append(de->d_name); |
Ethan Yonker | 3fdcda4 | 2016-11-30 12:29:37 -0600 | [diff] [blame] | 2165 | if (wipe_exclusions.check_skip_dirs(dir)) { |
Ethan Yonker | 66a1949 | 2015-12-10 10:19:45 -0600 | [diff] [blame] | 2166 | LOGINFO("skipped '%s'\n", dir.c_str()); |
| 2167 | continue; |
| 2168 | } |
Dees_Troy | ce67546 | 2013-01-09 19:48:21 +0000 | [diff] [blame] | 2169 | if (de->d_type == DT_DIR) { |
Ethan Yonker | 66a1949 | 2015-12-10 10:19:45 -0600 | [diff] [blame] | 2170 | dir.append("/"); |
| 2171 | if (!Wipe_Data_Without_Wiping_Media_Func(dir)) { |
| 2172 | closedir(d); |
| 2173 | return false; |
| 2174 | } |
| 2175 | rmdir(dir.c_str()); |
bigbiff bigbiff | 98f1f90 | 2013-01-19 18:46:13 -0500 | [diff] [blame] | 2176 | } 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] | 2177 | if (!unlink(dir.c_str())) |
Ethan Yonker | 79f88bd | 2016-12-09 14:52:12 -0600 | [diff] [blame] | 2178 | LOGINFO("Unable to unlink '%s': %s\n", dir.c_str(), strerror(errno)); |
Dees_Troy | ce67546 | 2013-01-09 19:48:21 +0000 | [diff] [blame] | 2179 | } |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 2180 | } |
| 2181 | closedir(d); |
bigbiff bigbiff | c7360dd | 2014-01-25 15:02:57 -0500 | [diff] [blame] | 2182 | |
Dees_Troy | 16b7435 | 2012-11-14 22:27:31 +0000 | [diff] [blame] | 2183 | return true; |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 2184 | } |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 2185 | gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Mount_Point)(strerror(errno))); |
Dees_Troy | 16b7435 | 2012-11-14 22:27:31 +0000 | [diff] [blame] | 2186 | return false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 2187 | } |
| 2188 | |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 2189 | bool TWPartition::Backup_Tar(PartitionSettings *part_settings, pid_t *tar_fork_pid) { |
Ethan Yonker | 472f506 | 2016-02-25 13:47:30 -0600 | [diff] [blame] | 2190 | string Full_FileName; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 2191 | twrpTar tar; |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 2192 | |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 2193 | if (!Mount(true)) |
| 2194 | return false; |
| 2195 | |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 2196 | TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Backup_Display_Name, "Backing Up"); |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 2197 | gui_msg(Msg("backing_up=Backing up {1}...")(Backup_Display_Name)); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 2198 | |
Ethan Yonker | 472f506 | 2016-02-25 13:47:30 -0600 | [diff] [blame] | 2199 | DataManager::GetValue(TW_USE_COMPRESSION_VAR, tar.use_compression); |
Matt Mower | bb81e5d | 2014-03-20 18:05:41 -0500 | [diff] [blame] | 2200 | |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 2201 | #ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS |
Ethan Yonker | 472f506 | 2016-02-25 13:47:30 -0600 | [diff] [blame] | 2202 | if (Can_Encrypt_Backup) { |
| 2203 | DataManager::GetValue("tw_encrypt_backup", tar.use_encryption); |
| 2204 | if (tar.use_encryption) { |
| 2205 | if (Use_Userdata_Encryption) |
| 2206 | tar.userdata_encryption = tar.use_encryption; |
| 2207 | string Password; |
| 2208 | DataManager::GetValue("tw_backup_password", Password); |
| 2209 | tar.setpassword(Password); |
| 2210 | } else { |
| 2211 | tar.use_encryption = 0; |
| 2212 | } |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 2213 | } |
| 2214 | #endif |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 2215 | |
Ethan Yonker | 472f506 | 2016-02-25 13:47:30 -0600 | [diff] [blame] | 2216 | Backup_FileName = Backup_Name + "." + Current_File_System + ".win"; |
Ethan Yonker | e080c1f | 2016-09-19 13:50:25 -0500 | [diff] [blame] | 2217 | Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName; |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 2218 | tar.has_data_media = Has_Data_Media; |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 2219 | tar.part_settings = part_settings; |
Ethan Yonker | 3fdcda4 | 2016-11-30 12:29:37 -0600 | [diff] [blame] | 2220 | tar.backup_exclusions = &backup_exclusions; |
Dees Troy | e0a433a | 2013-12-02 04:10:37 +0000 | [diff] [blame] | 2221 | tar.setdir(Backup_Path); |
| 2222 | tar.setfn(Full_FileName); |
| 2223 | tar.setsize(Backup_Size); |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 2224 | tar.partition_name = Backup_Name; |
Ethan Yonker | dcf2b67 | 2016-09-13 14:41:53 -0500 | [diff] [blame] | 2225 | tar.backup_folder = part_settings->Backup_Folder; |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 2226 | if (tar.createTarFork(tar_fork_pid) != 0) |
Dees Troy | e0a433a | 2013-12-02 04:10:37 +0000 | [diff] [blame] | 2227 | return false; |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 2228 | return true; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 2229 | } |
| 2230 | |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 2231 | bool TWPartition::Backup_Image(PartitionSettings *part_settings) { |
| 2232 | string Full_FileName, adb_file_name; |
| 2233 | int adb_control_bu_fd, compressed; |
igoriok | 87e3d93 | 2013-01-31 21:03:53 +0200 | [diff] [blame] | 2234 | |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 2235 | TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, gui_parse_text("{@backing}")); |
| 2236 | gui_msg(Msg("backing_up=Backing up {1}...")(Backup_Display_Name)); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 2237 | |
Ethan Yonker | 472f506 | 2016-02-25 13:47:30 -0600 | [diff] [blame] | 2238 | Backup_FileName = Backup_Name + "." + Current_File_System + ".win"; |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 2239 | |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 2240 | if (part_settings->adbbackup) { |
| 2241 | Full_FileName = TW_ADB_BACKUP; |
Ethan Yonker | dcf2b67 | 2016-09-13 14:41:53 -0500 | [diff] [blame] | 2242 | adb_file_name = part_settings->Backup_Folder + "/" + Backup_FileName; |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 2243 | } |
| 2244 | else |
Ethan Yonker | dcf2b67 | 2016-09-13 14:41:53 -0500 | [diff] [blame] | 2245 | Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName; |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 2246 | |
| 2247 | part_settings->total_restore_size = Backup_Size; |
| 2248 | |
| 2249 | if (part_settings->adbbackup) { |
| 2250 | if (!twadbbu::Write_TWIMG(adb_file_name, Backup_Size)) |
| 2251 | return false; |
| 2252 | } |
| 2253 | |
| 2254 | if (!Raw_Read_Write(part_settings)) |
Ethan Yonker | 472f506 | 2016-02-25 13:47:30 -0600 | [diff] [blame] | 2255 | return false; |
| 2256 | |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 2257 | if (part_settings->adbbackup) { |
| 2258 | if (!twadbbu::Write_TWEOF()) |
| 2259 | return false; |
Dees_Troy | 7c2dec8 | 2012-09-26 09:49:14 -0400 | [diff] [blame] | 2260 | } |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 2261 | return true; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 2262 | } |
| 2263 | |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 2264 | bool TWPartition::Raw_Read_Write(PartitionSettings *part_settings) { |
| 2265 | unsigned long long RW_Block_Size, Remain = Backup_Size; |
| 2266 | int src_fd = -1, dest_fd = -1; |
| 2267 | ssize_t bs; |
Ethan Yonker | 472f506 | 2016-02-25 13:47:30 -0600 | [diff] [blame] | 2268 | bool ret = false; |
| 2269 | void* buffer = NULL; |
| 2270 | unsigned long long backedup_size = 0; |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 2271 | string srcfn, destfn; |
Ethan Yonker | 472f506 | 2016-02-25 13:47:30 -0600 | [diff] [blame] | 2272 | |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 2273 | if (part_settings->PM_Method == PM_BACKUP) { |
| 2274 | srcfn = Actual_Block_Device; |
| 2275 | if (part_settings->adbbackup) |
| 2276 | destfn = TW_ADB_BACKUP; |
| 2277 | else |
Ethan Yonker | e080c1f | 2016-09-19 13:50:25 -0500 | [diff] [blame] | 2278 | destfn = part_settings->Backup_Folder + "/" + Backup_FileName; |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 2279 | } |
| 2280 | else { |
| 2281 | destfn = Actual_Block_Device; |
| 2282 | if (part_settings->adbbackup) { |
| 2283 | srcfn = TW_ADB_RESTORE; |
| 2284 | } else { |
Ethan Yonker | dcf2b67 | 2016-09-13 14:41:53 -0500 | [diff] [blame] | 2285 | srcfn = part_settings->Backup_Folder + "/" + Backup_FileName; |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 2286 | Remain = TWFunc::Get_File_Size(srcfn); |
| 2287 | } |
| 2288 | } |
Ethan Yonker | 472f506 | 2016-02-25 13:47:30 -0600 | [diff] [blame] | 2289 | |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 2290 | src_fd = open(srcfn.c_str(), O_RDONLY | O_LARGEFILE); |
Ethan Yonker | 472f506 | 2016-02-25 13:47:30 -0600 | [diff] [blame] | 2291 | if (src_fd < 0) { |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 2292 | gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(srcfn.c_str())(strerror(errno))); |
Ethan Yonker | 472f506 | 2016-02-25 13:47:30 -0600 | [diff] [blame] | 2293 | return false; |
| 2294 | } |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 2295 | |
| 2296 | dest_fd = open(destfn.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_LARGEFILE, S_IRUSR | S_IWUSR); |
Ethan Yonker | 472f506 | 2016-02-25 13:47:30 -0600 | [diff] [blame] | 2297 | if (dest_fd < 0) { |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 2298 | gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(destfn.c_str())(strerror(errno))); |
Ethan Yonker | 472f506 | 2016-02-25 13:47:30 -0600 | [diff] [blame] | 2299 | goto exit; |
| 2300 | } |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 2301 | |
| 2302 | LOGINFO("Reading '%s', writing '%s'\n", srcfn.c_str(), destfn.c_str()); |
| 2303 | |
| 2304 | if (part_settings->adbbackup) { |
| 2305 | RW_Block_Size = MAX_ADB_READ; |
| 2306 | bs = MAX_ADB_READ; |
| 2307 | } |
| 2308 | else { |
| 2309 | RW_Block_Size = 1048576LLU; // 1MB |
| 2310 | bs = (ssize_t)(RW_Block_Size); |
| 2311 | } |
| 2312 | |
Ethan Yonker | 472f506 | 2016-02-25 13:47:30 -0600 | [diff] [blame] | 2313 | buffer = malloc((size_t)bs); |
| 2314 | if (!buffer) { |
| 2315 | LOGINFO("Raw_Read_Write failed to malloc\n"); |
| 2316 | goto exit; |
| 2317 | } |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 2318 | |
| 2319 | if (part_settings->progress) |
| 2320 | part_settings->progress->SetPartitionSize(part_settings->total_restore_size); |
| 2321 | |
Ethan Yonker | 472f506 | 2016-02-25 13:47:30 -0600 | [diff] [blame] | 2322 | while (Remain > 0) { |
| 2323 | if (Remain < RW_Block_Size) |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 2324 | bs = (ssize_t)(Remain); |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame^] | 2325 | if (read(src_fd, buffer, bs) != bs) { |
Ethan Yonker | 472f506 | 2016-02-25 13:47:30 -0600 | [diff] [blame] | 2326 | LOGINFO("Error reading source fd (%s)\n", strerror(errno)); |
| 2327 | goto exit; |
| 2328 | } |
| 2329 | if (write(dest_fd, buffer, bs) != bs) { |
| 2330 | LOGINFO("Error writing destination fd (%s)\n", strerror(errno)); |
| 2331 | goto exit; |
| 2332 | } |
| 2333 | backedup_size += (unsigned long long)(bs); |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 2334 | Remain = Remain - (unsigned long long)(bs); |
| 2335 | if (part_settings->progress) |
| 2336 | part_settings->progress->UpdateSize(backedup_size); |
Ethan Yonker | 472f506 | 2016-02-25 13:47:30 -0600 | [diff] [blame] | 2337 | if (PartitionManager.Check_Backup_Cancel() != 0) |
| 2338 | goto exit; |
| 2339 | } |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 2340 | if (part_settings->progress) |
| 2341 | part_settings->progress->UpdateDisplayDetails(true); |
Ethan Yonker | 472f506 | 2016-02-25 13:47:30 -0600 | [diff] [blame] | 2342 | fsync(dest_fd); |
nailyk | a083dc6 | 2016-11-11 21:41:28 +0100 | [diff] [blame] | 2343 | |
| 2344 | if (!part_settings->adbbackup && part_settings->PM_Method == PM_BACKUP) { |
| 2345 | tw_set_default_metadata(destfn.c_str()); |
| 2346 | LOGINFO("Restored default metadata for %s\n", destfn.c_str()); |
| 2347 | } |
| 2348 | |
Ethan Yonker | 472f506 | 2016-02-25 13:47:30 -0600 | [diff] [blame] | 2349 | ret = true; |
| 2350 | exit: |
| 2351 | if (src_fd >= 0) |
| 2352 | close(src_fd); |
| 2353 | if (dest_fd >= 0) |
| 2354 | close(dest_fd); |
| 2355 | if (buffer) |
| 2356 | free(buffer); |
| 2357 | return ret; |
| 2358 | } |
| 2359 | |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 2360 | bool TWPartition::Backup_Dump_Image(PartitionSettings *part_settings) { |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 2361 | string Full_FileName, Command; |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 2362 | int use_compression, adb_control_bu_fd; |
| 2363 | unsigned long long compressed; |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 2364 | |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 2365 | TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, gui_parse_text("{@backing}")); |
| 2366 | gui_msg(Msg("backing_up=Backing up {1}...")(Backup_Display_Name)); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 2367 | |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 2368 | if (part_settings->progress) |
| 2369 | part_settings->progress->SetPartitionSize(Backup_Size); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 2370 | |
Ethan Yonker | 472f506 | 2016-02-25 13:47:30 -0600 | [diff] [blame] | 2371 | Backup_FileName = Backup_Name + "." + Current_File_System + ".win"; |
Ethan Yonker | dcf2b67 | 2016-09-13 14:41:53 -0500 | [diff] [blame] | 2372 | Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName; |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 2373 | |
| 2374 | Command = "dump_image " + MTD_Name + " '" + Full_FileName + "'"; |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 2375 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 2376 | LOGINFO("Backup command: '%s'\n", Command.c_str()); |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 2377 | TWFunc::Exec_Cmd(Command); |
Ethan Yonker | 4b94cfd | 2014-12-11 10:00:45 -0600 | [diff] [blame] | 2378 | tw_set_default_metadata(Full_FileName.c_str()); |
Dees_Troy | 7c2dec8 | 2012-09-26 09:49:14 -0400 | [diff] [blame] | 2379 | if (TWFunc::Get_File_Size(Full_FileName) == 0) { |
| 2380 | // Actual size may not match backup size due to bad blocks on MTD devices so just check for 0 bytes |
Matt Mower | 3c36697 | 2015-12-25 19:28:31 -0600 | [diff] [blame] | 2381 | gui_msg(Msg(msg::kError, "backup_size=Backup file size for '{1}' is 0 bytes.")(Full_FileName)); |
Dees_Troy | 7c2dec8 | 2012-09-26 09:49:14 -0400 | [diff] [blame] | 2382 | return false; |
| 2383 | } |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 2384 | if (part_settings->progress) |
| 2385 | part_settings->progress->UpdateSize(Backup_Size); |
| 2386 | |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 2387 | return true; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 2388 | } |
| 2389 | |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 2390 | unsigned long long TWPartition::Get_Restore_Size(PartitionSettings *part_settings) { |
| 2391 | if (!part_settings->adbbackup) { |
Ethan Yonker | dcf2b67 | 2016-09-13 14:41:53 -0500 | [diff] [blame] | 2392 | InfoManager restore_info(part_settings->Backup_Folder + "/" + Backup_Name + ".info"); |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 2393 | if (restore_info.LoadValues() == 0) { |
| 2394 | if (restore_info.GetValue("backup_size", Restore_Size) == 0) { |
| 2395 | LOGINFO("Read info file, restore size is %llu\n", Restore_Size); |
| 2396 | return Restore_Size; |
| 2397 | } |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 2398 | } |
| 2399 | } |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 2400 | |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 2401 | string Full_FileName, Restore_File_System = Get_Restore_File_System(part_settings); |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 2402 | |
Ethan Yonker | dcf2b67 | 2016-09-13 14:41:53 -0500 | [diff] [blame] | 2403 | Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName; |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 2404 | if (Is_Image(Restore_File_System)) { |
| 2405 | Restore_Size = TWFunc::Get_File_Size(Full_FileName); |
| 2406 | return Restore_Size; |
| 2407 | } |
| 2408 | |
| 2409 | twrpTar tar; |
| 2410 | tar.setdir(Backup_Path); |
| 2411 | tar.setfn(Full_FileName); |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 2412 | tar.backup_name = Full_FileName; |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 2413 | #ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS |
| 2414 | string Password; |
| 2415 | DataManager::GetValue("tw_restore_password", Password); |
| 2416 | if (!Password.empty()) |
| 2417 | tar.setpassword(Password); |
| 2418 | #endif |
| 2419 | tar.partition_name = Backup_Name; |
Ethan Yonker | dcf2b67 | 2016-09-13 14:41:53 -0500 | [diff] [blame] | 2420 | tar.backup_folder = part_settings->Backup_Folder; |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 2421 | tar.part_settings = part_settings; |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 2422 | Restore_Size = tar.get_size(); |
| 2423 | return Restore_Size; |
| 2424 | } |
| 2425 | |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 2426 | bool TWPartition::Restore_Tar(PartitionSettings *part_settings) { |
Ethan Yonker | 472f506 | 2016-02-25 13:47:30 -0600 | [diff] [blame] | 2427 | string Full_FileName; |
Dees Troy | 4159aed | 2014-02-28 17:24:43 +0000 | [diff] [blame] | 2428 | bool ret = false; |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 2429 | string Restore_File_System = Get_Restore_File_System(part_settings); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 2430 | |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 2431 | if (Has_Android_Secure) { |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 2432 | if (!Wipe_AndSec()) |
| 2433 | return false; |
Gary Peck | 43acadf | 2012-11-21 21:19:01 -0800 | [diff] [blame] | 2434 | } else { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 2435 | gui_msg(Msg("wiping=Wiping {1}")(Backup_Display_Name)); |
Ethan Yonker | 5eac222 | 2014-06-11 12:22:55 -0500 | [diff] [blame] | 2436 | if (Has_Data_Media && Mount_Point == "/data" && Restore_File_System != Current_File_System) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 2437 | gui_msg(Msg(msg::kWarning, "datamedia_fs_restore=WARNING: This /data backup was made with {1} file system! The backup may not boot unless you change back to {1}.")(Restore_File_System)); |
Ethan Yonker | 5eac222 | 2014-06-11 12:22:55 -0500 | [diff] [blame] | 2438 | if (!Wipe_Data_Without_Wiping_Media()) |
| 2439 | return false; |
| 2440 | } else { |
| 2441 | if (!Wipe(Restore_File_System)) |
| 2442 | return false; |
| 2443 | } |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 2444 | } |
Matt Mower | 3c36697 | 2015-12-25 19:28:31 -0600 | [diff] [blame] | 2445 | TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Backup_Display_Name, gui_parse_text("{@restoring_hdr}")); |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 2446 | gui_msg(Msg("restoring=Restoring {1}...")(Backup_Display_Name)); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 2447 | |
James Christopher Adduono | d6f94ac | 2016-02-29 04:26:04 -0500 | [diff] [blame] | 2448 | // Remount as read/write as needed so we can restore the backup |
| 2449 | if (!ReMount_RW(true)) |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 2450 | return false; |
| 2451 | |
Ethan Yonker | dcf2b67 | 2016-09-13 14:41:53 -0500 | [diff] [blame] | 2452 | Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName; |
Ethan Yonker | 87af563 | 2014-02-10 11:56:35 -0600 | [diff] [blame] | 2453 | twrpTar tar; |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 2454 | tar.part_settings = part_settings; |
Ethan Yonker | 87af563 | 2014-02-10 11:56:35 -0600 | [diff] [blame] | 2455 | tar.setdir(Backup_Path); |
| 2456 | tar.setfn(Full_FileName); |
| 2457 | tar.backup_name = Backup_Name; |
| 2458 | #ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS |
| 2459 | string Password; |
| 2460 | DataManager::GetValue("tw_restore_password", Password); |
| 2461 | if (!Password.empty()) |
| 2462 | tar.setpassword(Password); |
| 2463 | #endif |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 2464 | part_settings->progress->SetPartitionSize(Get_Restore_Size(part_settings)); |
| 2465 | if (tar.extractTarFork() != 0) |
Dees Troy | 4159aed | 2014-02-28 17:24:43 +0000 | [diff] [blame] | 2466 | ret = false; |
| 2467 | else |
| 2468 | ret = true; |
| 2469 | #ifdef HAVE_CAPABILITIES |
| 2470 | // Restore capabilities to the run-as binary |
| 2471 | if (Mount_Point == "/system" && Mount(true) && TWFunc::Path_Exists("/system/bin/run-as")) { |
| 2472 | struct vfs_cap_data cap_data; |
| 2473 | uint64_t capabilities = (1 << CAP_SETUID) | (1 << CAP_SETGID); |
| 2474 | |
| 2475 | memset(&cap_data, 0, sizeof(cap_data)); |
| 2476 | cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE; |
| 2477 | cap_data.data[0].permitted = (uint32_t) (capabilities & 0xffffffff); |
| 2478 | cap_data.data[0].inheritable = 0; |
| 2479 | cap_data.data[1].permitted = (uint32_t) (capabilities >> 32); |
| 2480 | cap_data.data[1].inheritable = 0; |
| 2481 | if (setxattr("/system/bin/run-as", XATTR_NAME_CAPS, &cap_data, sizeof(cap_data), 0) < 0) { |
| 2482 | LOGINFO("Failed to reset capabilities of /system/bin/run-as binary.\n"); |
| 2483 | } else { |
| 2484 | LOGINFO("Reset capabilities of /system/bin/run-as binary successful.\n"); |
| 2485 | } |
| 2486 | } |
| 2487 | #endif |
James Christopher Adduono | d6f94ac | 2016-02-29 04:26:04 -0500 | [diff] [blame] | 2488 | if (Mount_Read_Only || Mount_Flags & MS_RDONLY) |
| 2489 | // Remount as read only when restoration is complete |
| 2490 | ReMount(true); |
| 2491 | |
Dees Troy | 4159aed | 2014-02-28 17:24:43 +0000 | [diff] [blame] | 2492 | return ret; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 2493 | } |
| 2494 | |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 2495 | bool TWPartition::Restore_Image(PartitionSettings *part_settings) { |
Ethan Yonker | 96af84a | 2015-01-05 14:58:36 -0600 | [diff] [blame] | 2496 | string Full_FileName; |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 2497 | string Restore_File_System = Get_Restore_File_System(part_settings); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 2498 | |
Matt Mower | 3c36697 | 2015-12-25 19:28:31 -0600 | [diff] [blame] | 2499 | TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Backup_Display_Name, gui_parse_text("{@restoring_hdr}")); |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 2500 | gui_msg(Msg("restoring=Restoring {1}...")(Backup_Display_Name)); |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 2501 | |
| 2502 | if (part_settings->adbbackup) |
| 2503 | Full_FileName = TW_ADB_RESTORE; |
| 2504 | else |
Ethan Yonker | e080c1f | 2016-09-19 13:50:25 -0500 | [diff] [blame] | 2505 | Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName; |
Gary Peck | 15e623d | 2012-11-21 21:07:58 -0800 | [diff] [blame] | 2506 | |
Ethan Yonker | 96af84a | 2015-01-05 14:58:36 -0600 | [diff] [blame] | 2507 | if (Restore_File_System == "emmc") { |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 2508 | if (!part_settings->adbbackup) |
| 2509 | part_settings->total_restore_size = (uint64_t)(TWFunc::Get_File_Size(Full_FileName)); |
| 2510 | if (!Raw_Read_Write(part_settings)) |
Ethan Yonker | 96af84a | 2015-01-05 14:58:36 -0600 | [diff] [blame] | 2511 | return false; |
| 2512 | } else if (Restore_File_System == "mtd" || Restore_File_System == "bml") { |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 2513 | if (!Flash_Image_FI(Full_FileName, part_settings->progress)) |
| 2514 | return false; |
| 2515 | } |
| 2516 | |
| 2517 | if (part_settings->adbbackup) { |
| 2518 | if (!twadbbu::Write_TWEOF()) |
Ethan Yonker | 96af84a | 2015-01-05 14:58:36 -0600 | [diff] [blame] | 2519 | return false; |
Gary Peck | 15e623d | 2012-11-21 21:07:58 -0800 | [diff] [blame] | 2520 | } |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 2521 | return true; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 2522 | } |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 2523 | |
| 2524 | bool TWPartition::Update_Size(bool Display_Error) { |
Dees_Troy | 0550cfb | 2012-10-13 11:56:13 -0400 | [diff] [blame] | 2525 | bool ret = false, Was_Already_Mounted = false; |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 2526 | |
Ethan Yonker | 1b19016 | 2016-12-05 15:25:19 -0600 | [diff] [blame] | 2527 | Find_Actual_Block_Device(); |
| 2528 | |
| 2529 | if (!Can_Be_Mounted && !Is_Encrypted) { |
| 2530 | if (TWFunc::Path_Exists(Actual_Block_Device) && Find_Partition_Size()) { |
| 2531 | Used = Size; |
| 2532 | Backup_Size = Size; |
| 2533 | return true; |
| 2534 | } |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 2535 | return false; |
Ethan Yonker | 1b19016 | 2016-12-05 15:25:19 -0600 | [diff] [blame] | 2536 | } |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 2537 | |
Dees_Troy | 0550cfb | 2012-10-13 11:56:13 -0400 | [diff] [blame] | 2538 | Was_Already_Mounted = Is_Mounted(); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 2539 | if (Removable || Is_Encrypted) { |
| 2540 | if (!Mount(false)) |
| 2541 | return true; |
| 2542 | } else if (!Mount(Display_Error)) |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 2543 | return false; |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 2544 | |
| 2545 | ret = Get_Size_Via_statfs(Display_Error); |
Dees_Troy | 0550cfb | 2012-10-13 11:56:13 -0400 | [diff] [blame] | 2546 | if (!ret || Size == 0) { |
| 2547 | if (!Get_Size_Via_df(Display_Error)) { |
| 2548 | if (!Was_Already_Mounted) |
| 2549 | UnMount(false); |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 2550 | return false; |
Dees_Troy | 0550cfb | 2012-10-13 11:56:13 -0400 | [diff] [blame] | 2551 | } |
| 2552 | } |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 2553 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 2554 | if (Has_Data_Media) { |
| 2555 | if (Mount(Display_Error)) { |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 2556 | unsigned long long data_media_used, actual_data; |
Ethan Yonker | 3fdcda4 | 2016-11-30 12:29:37 -0600 | [diff] [blame] | 2557 | Used = backup_exclusions.Get_Folder_Size(Mount_Point); |
bigbiff bigbiff | 34684ff | 2013-12-01 21:03:45 -0500 | [diff] [blame] | 2558 | Backup_Size = Used; |
| 2559 | int bak = (int)(Used / 1048576LLU); |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 2560 | int fre = (int)(Free / 1048576LLU); |
bigbiff bigbiff | 34684ff | 2013-12-01 21:03:45 -0500 | [diff] [blame] | 2561 | LOGINFO("Data backup size is %iMB, free: %iMB.\n", bak, fre); |
Dees_Troy | 0550cfb | 2012-10-13 11:56:13 -0400 | [diff] [blame] | 2562 | } else { |
| 2563 | if (!Was_Already_Mounted) |
| 2564 | UnMount(false); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 2565 | return false; |
Dees_Troy | 0550cfb | 2012-10-13 11:56:13 -0400 | [diff] [blame] | 2566 | } |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 2567 | } else if (Has_Android_Secure) { |
| 2568 | if (Mount(Display_Error)) |
Ethan Yonker | 3fdcda4 | 2016-11-30 12:29:37 -0600 | [diff] [blame] | 2569 | Backup_Size = backup_exclusions.Get_Folder_Size(Backup_Path); |
Dees_Troy | 0550cfb | 2012-10-13 11:56:13 -0400 | [diff] [blame] | 2570 | else { |
| 2571 | if (!Was_Already_Mounted) |
| 2572 | UnMount(false); |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 2573 | return false; |
Dees_Troy | 0550cfb | 2012-10-13 11:56:13 -0400 | [diff] [blame] | 2574 | } |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 2575 | } |
Dees_Troy | 0550cfb | 2012-10-13 11:56:13 -0400 | [diff] [blame] | 2576 | if (!Was_Already_Mounted) |
| 2577 | UnMount(false); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 2578 | return true; |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 2579 | } |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 2580 | |
| 2581 | void TWPartition::Find_Actual_Block_Device(void) { |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 2582 | if (Is_Decrypted && !Decrypted_Block_Device.empty()) { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 2583 | Actual_Block_Device = Decrypted_Block_Device; |
Ethan Yonker | 1b19016 | 2016-12-05 15:25:19 -0600 | [diff] [blame] | 2584 | if (TWFunc::Path_Exists(Decrypted_Block_Device)) { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 2585 | Is_Present = true; |
Ethan Yonker | 1b19016 | 2016-12-05 15:25:19 -0600 | [diff] [blame] | 2586 | return; |
| 2587 | } |
| 2588 | } else if (SlotSelect && TWFunc::Path_Exists(Primary_Block_Device + PartitionManager.Get_Active_Slot_Suffix())) { |
| 2589 | Actual_Block_Device = Primary_Block_Device + PartitionManager.Get_Active_Slot_Suffix(); |
| 2590 | unlink(Primary_Block_Device.c_str()); |
| 2591 | symlink(Actual_Block_Device.c_str(), Primary_Block_Device.c_str()); // we create a non-slot symlink pointing to the currently selected slot which may assist zips with installing |
| 2592 | Is_Present = true; |
| 2593 | return; |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 2594 | } else if (TWFunc::Path_Exists(Primary_Block_Device)) { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 2595 | Is_Present = true; |
| 2596 | Actual_Block_Device = Primary_Block_Device; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 2597 | return; |
| 2598 | } |
Ethan Yonker | 1b19016 | 2016-12-05 15:25:19 -0600 | [diff] [blame] | 2599 | if (!Alternate_Block_Device.empty() && TWFunc::Path_Exists(Alternate_Block_Device)) { |
Dees_Troy | 3f04d03 | 2012-10-07 18:20:09 -0400 | [diff] [blame] | 2600 | Actual_Block_Device = Alternate_Block_Device; |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 2601 | Is_Present = true; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 2602 | } else { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 2603 | Is_Present = false; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 2604 | } |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 2605 | } |
| 2606 | |
| 2607 | void TWPartition::Recreate_Media_Folder(void) { |
| 2608 | string Command; |
Ethan Yonker | 66a1949 | 2015-12-10 10:19:45 -0600 | [diff] [blame] | 2609 | string Media_Path = Mount_Point + "/media"; |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 2610 | |
Ethan Yonker | bd7492d | 2016-12-07 13:55:01 -0600 | [diff] [blame] | 2611 | if (Is_FBE) { |
| 2612 | LOGINFO("Not recreating media folder on FBE\n"); |
| 2613 | return; |
| 2614 | } |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 2615 | if (!Mount(true)) { |
Ethan Yonker | 66a1949 | 2015-12-10 10:19:45 -0600 | [diff] [blame] | 2616 | gui_msg(Msg(msg::kError, "recreate_folder_err=Unable to recreate {1} folder.")(Media_Path)); |
| 2617 | } else if (!TWFunc::Path_Exists(Media_Path)) { |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 2618 | PartitionManager.Mount_By_Path(Symlink_Mount_Point, true); |
Ethan Yonker | 66a1949 | 2015-12-10 10:19:45 -0600 | [diff] [blame] | 2619 | LOGINFO("Recreating %s folder.\n", Media_Path.c_str()); |
| 2620 | mkdir(Media_Path.c_str(), 0770); |
xiaolu | 9416f4f | 2015-06-04 08:22:23 +0800 | [diff] [blame] | 2621 | string Internal_path = DataManager::GetStrValue("tw_internal_path"); |
| 2622 | if (!Internal_path.empty()) { |
| 2623 | LOGINFO("Recreating %s folder.\n", Internal_path.c_str()); |
| 2624 | mkdir(Internal_path.c_str(), 0770); |
| 2625 | } |
| 2626 | #ifdef TW_INTERNAL_STORAGE_PATH |
| 2627 | mkdir(EXPAND(TW_INTERNAL_STORAGE_PATH), 0770); |
| 2628 | #endif |
Ethan Yonker | 5ef301e | 2014-10-14 10:04:44 -0500 | [diff] [blame] | 2629 | #ifdef HAVE_SELINUX |
that | a3d31fb | 2014-12-21 22:27:40 +0100 | [diff] [blame] | 2630 | // Afterwards, we will try to set the |
| 2631 | // default metadata that we were hopefully able to get during |
| 2632 | // early boot. |
Ethan Yonker | 66a1949 | 2015-12-10 10:19:45 -0600 | [diff] [blame] | 2633 | tw_set_default_metadata(Media_Path.c_str()); |
xiaolu | 9416f4f | 2015-06-04 08:22:23 +0800 | [diff] [blame] | 2634 | if (!Internal_path.empty()) |
| 2635 | tw_set_default_metadata(Internal_path.c_str()); |
Ethan Yonker | 5ef301e | 2014-10-14 10:04:44 -0500 | [diff] [blame] | 2636 | #endif |
Ethan Yonker | 5eac222 | 2014-06-11 12:22:55 -0500 | [diff] [blame] | 2637 | // Toggle mount to ensure that "internal sdcard" gets mounted |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 2638 | PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true); |
Ethan Yonker | 5eac222 | 2014-06-11 12:22:55 -0500 | [diff] [blame] | 2639 | PartitionManager.Mount_By_Path(Symlink_Mount_Point, true); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 2640 | } |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 2641 | } |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 2642 | |
| 2643 | void TWPartition::Recreate_AndSec_Folder(void) { |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 2644 | if (!Has_Android_Secure) |
| 2645 | return; |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 2646 | 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] | 2647 | if (!Mount(true)) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 2648 | gui_msg(Msg(msg::kError, "recreate_folder_err=Unable to recreate {1} folder.")(Backup_Name)); |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 2649 | } else if (!TWFunc::Path_Exists(Symlink_Path)) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 2650 | LOGINFO("Recreating %s folder.\n", Backup_Name.c_str()); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 2651 | PartitionManager.Mount_By_Path(Symlink_Mount_Point, true); |
Matt Mower | fb1c4ff | 2014-04-16 13:43:36 -0500 | [diff] [blame] | 2652 | mkdir(Symlink_Path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 2653 | PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true); |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 2654 | } |
| 2655 | } |
bigbiff | 7cb4c33 | 2014-11-26 20:36:07 -0500 | [diff] [blame] | 2656 | |
| 2657 | uint64_t TWPartition::Get_Max_FileSize() { |
| 2658 | uint64_t maxFileSize = 0; |
| 2659 | const uint64_t constGB = (uint64_t) 1024 * 1024 * 1024; |
| 2660 | const uint64_t constTB = (uint64_t) constGB * 1024; |
| 2661 | const uint64_t constPB = (uint64_t) constTB * 1024; |
| 2662 | const uint64_t constEB = (uint64_t) constPB * 1024; |
bigbiff | 0c53203 | 2014-12-21 13:41:26 -0500 | [diff] [blame] | 2663 | if (Current_File_System == "ext4") |
| 2664 | maxFileSize = 16 * constTB; //16 TB |
| 2665 | else if (Current_File_System == "vfat") |
| 2666 | maxFileSize = 4 * constGB; //4 GB |
| 2667 | else if (Current_File_System == "ntfs") |
| 2668 | maxFileSize = 256 * constTB; //256 TB |
| 2669 | else if (Current_File_System == "exfat") |
| 2670 | maxFileSize = 16 * constPB; //16 PB |
| 2671 | else if (Current_File_System == "ext3") |
| 2672 | maxFileSize = 2 * constTB; //2 TB |
| 2673 | else if (Current_File_System == "f2fs") |
| 2674 | maxFileSize = 3.94 * constTB; //3.94 TB |
bigbiff | 7cb4c33 | 2014-11-26 20:36:07 -0500 | [diff] [blame] | 2675 | else |
| 2676 | maxFileSize = 100000000L; |
| 2677 | return maxFileSize - 1; |
| 2678 | } |
| 2679 | |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 2680 | bool TWPartition::Flash_Image(PartitionSettings *part_settings) { |
| 2681 | string Restore_File_System, full_filename; |
Ethan Yonker | 96af84a | 2015-01-05 14:58:36 -0600 | [diff] [blame] | 2682 | |
Ethan Yonker | e080c1f | 2016-09-19 13:50:25 -0500 | [diff] [blame] | 2683 | full_filename = part_settings->Backup_Folder + "/" + Backup_FileName; |
Ethan Yonker | 96af84a | 2015-01-05 14:58:36 -0600 | [diff] [blame] | 2684 | |
Ethan Yonker | dcf2b67 | 2016-09-13 14:41:53 -0500 | [diff] [blame] | 2685 | LOGINFO("Image filename is: %s\n", Backup_FileName.c_str()); |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 2686 | |
| 2687 | if (Backup_Method == BM_FILES) { |
Ethan Yonker | 96af84a | 2015-01-05 14:58:36 -0600 | [diff] [blame] | 2688 | LOGERR("Cannot flash images to file systems\n"); |
| 2689 | return false; |
| 2690 | } else if (!Can_Flash_Img) { |
| 2691 | LOGERR("Cannot flash images to partitions %s\n", Display_Name.c_str()); |
| 2692 | return false; |
| 2693 | } else { |
| 2694 | if (!Find_Partition_Size()) { |
| 2695 | LOGERR("Unable to find partition size for '%s'\n", Mount_Point.c_str()); |
| 2696 | return false; |
| 2697 | } |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 2698 | unsigned long long image_size = TWFunc::Get_File_Size(full_filename); |
Ethan Yonker | 96af84a | 2015-01-05 14:58:36 -0600 | [diff] [blame] | 2699 | if (image_size > Size) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 2700 | LOGINFO("Size (%llu bytes) of image '%s' is larger than target device '%s' (%llu bytes)\n", |
Ethan Yonker | dcf2b67 | 2016-09-13 14:41:53 -0500 | [diff] [blame] | 2701 | image_size, Backup_FileName.c_str(), Actual_Block_Device.c_str(), Size); |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 2702 | gui_err("img_size_err=Size of image is larger than target device"); |
Ethan Yonker | 96af84a | 2015-01-05 14:58:36 -0600 | [diff] [blame] | 2703 | return false; |
| 2704 | } |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 2705 | if (Backup_Method == BM_DD) { |
| 2706 | if (!part_settings->adbbackup) { |
| 2707 | if (Is_Sparse_Image(full_filename)) { |
| 2708 | return Flash_Sparse_Image(full_filename); |
| 2709 | } |
Ethan Yonker | 472f506 | 2016-02-25 13:47:30 -0600 | [diff] [blame] | 2710 | } |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 2711 | unsigned long long file_size = (unsigned long long)(TWFunc::Get_File_Size(full_filename)); |
| 2712 | return Raw_Read_Write(part_settings); |
| 2713 | } else if (Backup_Method == BM_FLASH_UTILS) { |
| 2714 | return Flash_Image_FI(full_filename, NULL); |
Ethan Yonker | 472f506 | 2016-02-25 13:47:30 -0600 | [diff] [blame] | 2715 | } |
Ethan Yonker | 96af84a | 2015-01-05 14:58:36 -0600 | [diff] [blame] | 2716 | } |
| 2717 | |
| 2718 | LOGERR("Unknown flash method for '%s'\n", Mount_Point.c_str()); |
| 2719 | return false; |
| 2720 | } |
| 2721 | |
Ethan Yonker | 472f506 | 2016-02-25 13:47:30 -0600 | [diff] [blame] | 2722 | bool TWPartition::Is_Sparse_Image(const string& Filename) { |
HashBang | ed974bb | 2016-01-30 14:20:09 -0500 | [diff] [blame] | 2723 | uint32_t magic = 0; |
| 2724 | int fd = open(Filename.c_str(), O_RDONLY); |
| 2725 | if (fd < 0) { |
| 2726 | gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Filename)(strerror(errno))); |
| 2727 | return false; |
| 2728 | } |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 2729 | |
HashBang | ed974bb | 2016-01-30 14:20:09 -0500 | [diff] [blame] | 2730 | if (read(fd, &magic, sizeof(magic)) != sizeof(magic)) { |
| 2731 | gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Filename)(strerror(errno))); |
| 2732 | close(fd); |
| 2733 | return false; |
| 2734 | } |
| 2735 | close(fd); |
Ethan Yonker | 472f506 | 2016-02-25 13:47:30 -0600 | [diff] [blame] | 2736 | if (magic == SPARSE_HEADER_MAGIC) |
| 2737 | return true; |
| 2738 | return false; |
| 2739 | } |
| 2740 | |
| 2741 | bool TWPartition::Flash_Sparse_Image(const string& Filename) { |
| 2742 | string Command; |
| 2743 | |
| 2744 | gui_msg(Msg("flashing=Flashing {1}...")(Display_Name)); |
| 2745 | |
| 2746 | Command = "simg2img '" + Filename + "' '" + Actual_Block_Device + "'"; |
Ethan Yonker | 96af84a | 2015-01-05 14:58:36 -0600 | [diff] [blame] | 2747 | LOGINFO("Flash command: '%s'\n", Command.c_str()); |
| 2748 | TWFunc::Exec_Cmd(Command); |
| 2749 | return true; |
| 2750 | } |
| 2751 | |
Ethan Yonker | 472f506 | 2016-02-25 13:47:30 -0600 | [diff] [blame] | 2752 | bool TWPartition::Flash_Image_FI(const string& Filename, ProgressTracking *progress) { |
Ethan Yonker | 96af84a | 2015-01-05 14:58:36 -0600 | [diff] [blame] | 2753 | string Command; |
Ethan Yonker | 472f506 | 2016-02-25 13:47:30 -0600 | [diff] [blame] | 2754 | unsigned long long file_size; |
Ethan Yonker | 96af84a | 2015-01-05 14:58:36 -0600 | [diff] [blame] | 2755 | |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 2756 | gui_msg(Msg("flashing=Flashing {1}...")(Display_Name)); |
Ethan Yonker | 472f506 | 2016-02-25 13:47:30 -0600 | [diff] [blame] | 2757 | if (progress) { |
| 2758 | file_size = (unsigned long long)(TWFunc::Get_File_Size(Filename)); |
| 2759 | progress->SetPartitionSize(file_size); |
| 2760 | } |
Ethan Yonker | 96af84a | 2015-01-05 14:58:36 -0600 | [diff] [blame] | 2761 | // Sometimes flash image doesn't like to flash due to the first 2KB matching, so we erase first to ensure that it flashes |
| 2762 | Command = "erase_image " + MTD_Name; |
| 2763 | LOGINFO("Erase command: '%s'\n", Command.c_str()); |
| 2764 | TWFunc::Exec_Cmd(Command); |
| 2765 | Command = "flash_image " + MTD_Name + " '" + Filename + "'"; |
| 2766 | LOGINFO("Flash command: '%s'\n", Command.c_str()); |
| 2767 | TWFunc::Exec_Cmd(Command); |
Ethan Yonker | 472f506 | 2016-02-25 13:47:30 -0600 | [diff] [blame] | 2768 | if (progress) |
| 2769 | progress->UpdateSize(file_size); |
Ethan Yonker | 96af84a | 2015-01-05 14:58:36 -0600 | [diff] [blame] | 2770 | return true; |
| 2771 | } |
Ethan Yonker | eb32b1f | 2015-05-18 10:23:03 -0500 | [diff] [blame] | 2772 | |
| 2773 | void TWPartition::Change_Mount_Read_Only(bool new_value) { |
| 2774 | Mount_Read_Only = new_value; |
| 2775 | } |
| 2776 | |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 2777 | bool TWPartition::Is_Read_Only() { |
| 2778 | return Mount_Read_Only; |
| 2779 | } |
| 2780 | |
Ethan Yonker | eb32b1f | 2015-05-18 10:23:03 -0500 | [diff] [blame] | 2781 | int TWPartition::Check_Lifetime_Writes() { |
| 2782 | bool original_read_only = Mount_Read_Only; |
| 2783 | int ret = 1; |
| 2784 | |
| 2785 | Mount_Read_Only = true; |
| 2786 | if (Mount(false)) { |
| 2787 | Find_Actual_Block_Device(); |
| 2788 | string block = basename(Actual_Block_Device.c_str()); |
| 2789 | string file = "/sys/fs/" + Current_File_System + "/" + block + "/lifetime_write_kbytes"; |
| 2790 | string result; |
| 2791 | if (TWFunc::Path_Exists(file)) { |
| 2792 | if (TWFunc::read_file(file, result) != 0) { |
| 2793 | LOGINFO("Check_Lifetime_Writes of '%s' failed to read_file\n", file.c_str()); |
| 2794 | } else { |
| 2795 | LOGINFO("Check_Lifetime_Writes result: '%s'\n", result.c_str()); |
| 2796 | if (result == "0") { |
| 2797 | ret = 0; |
| 2798 | } |
| 2799 | } |
| 2800 | } else { |
| 2801 | LOGINFO("Check_Lifetime_Writes file does not exist '%s'\n", file.c_str()); |
| 2802 | } |
| 2803 | UnMount(true); |
| 2804 | } else { |
| 2805 | LOGINFO("Check_Lifetime_Writes failed to mount '%s'\n", Mount_Point.c_str()); |
| 2806 | } |
| 2807 | Mount_Read_Only = original_read_only; |
| 2808 | return ret; |
| 2809 | } |
Ethan Yonker | 66a1949 | 2015-12-10 10:19:45 -0600 | [diff] [blame] | 2810 | |
| 2811 | int TWPartition::Decrypt_Adopted() { |
| 2812 | #ifdef TW_INCLUDE_CRYPTO |
| 2813 | int ret = 1; |
| 2814 | Is_Adopted_Storage = false; |
| 2815 | string Adopted_Key_File = ""; |
| 2816 | |
| 2817 | if (!Removable) |
| 2818 | return ret; |
| 2819 | |
| 2820 | int fd = open(Alternate_Block_Device.c_str(), O_RDONLY); |
| 2821 | if (fd < 0) { |
| 2822 | LOGINFO("failed to open '%s'\n", Alternate_Block_Device.c_str()); |
| 2823 | return ret; |
| 2824 | } |
| 2825 | char type_guid[80]; |
| 2826 | char part_guid[80]; |
| 2827 | |
| 2828 | if (gpt_disk_get_partition_info(fd, 2, type_guid, part_guid) == 0) { |
| 2829 | LOGINFO("type: '%s'\n", type_guid); |
| 2830 | LOGINFO("part: '%s'\n", part_guid); |
| 2831 | Adopted_GUID = part_guid; |
| 2832 | LOGINFO("Adopted_GUID '%s'\n", Adopted_GUID.c_str()); |
| 2833 | if (strcmp(type_guid, TWGptAndroidExpand) == 0) { |
| 2834 | LOGINFO("android_expand found\n"); |
| 2835 | Adopted_Key_File = "/data/misc/vold/expand_"; |
| 2836 | Adopted_Key_File += part_guid; |
| 2837 | Adopted_Key_File += ".key"; |
| 2838 | if (TWFunc::Path_Exists(Adopted_Key_File)) { |
| 2839 | Is_Adopted_Storage = true; |
| 2840 | /* Until we find a use case for this, I think it is safe |
| 2841 | * to disable USB Mass Storage whenever adopted storage |
| 2842 | * is present. |
| 2843 | */ |
| 2844 | LOGINFO("Detected adopted storage, disabling USB mass storage mode\n"); |
| 2845 | DataManager::SetValue("tw_has_usb_storage", 0); |
| 2846 | } |
| 2847 | } |
| 2848 | } |
| 2849 | |
| 2850 | if (Is_Adopted_Storage) { |
| 2851 | string Adopted_Block_Device = Alternate_Block_Device + "p2"; |
| 2852 | if (!TWFunc::Path_Exists(Adopted_Block_Device)) { |
| 2853 | Adopted_Block_Device = Alternate_Block_Device + "2"; |
| 2854 | if (!TWFunc::Path_Exists(Adopted_Block_Device)) { |
| 2855 | LOGINFO("Adopted block device does not exist\n"); |
| 2856 | goto exit; |
| 2857 | } |
| 2858 | } |
| 2859 | LOGINFO("key file is '%s', block device '%s'\n", Adopted_Key_File.c_str(), Adopted_Block_Device.c_str()); |
| 2860 | char crypto_blkdev[MAXPATHLEN]; |
| 2861 | std::string thekey; |
| 2862 | int fdkey = open(Adopted_Key_File.c_str(), O_RDONLY); |
| 2863 | if (fdkey < 0) { |
| 2864 | LOGINFO("failed to open key file\n"); |
| 2865 | goto exit; |
| 2866 | } |
| 2867 | char buf[512]; |
| 2868 | ssize_t n; |
| 2869 | while ((n = read(fdkey, &buf[0], sizeof(buf))) > 0) { |
| 2870 | thekey.append(buf, n); |
| 2871 | } |
| 2872 | close(fdkey); |
| 2873 | unsigned char* key = (unsigned char*) thekey.data(); |
| 2874 | cryptfs_revert_ext_volume(part_guid); |
| 2875 | |
| 2876 | ret = cryptfs_setup_ext_volume(part_guid, Adopted_Block_Device.c_str(), key, thekey.size(), crypto_blkdev); |
| 2877 | if (ret == 0) { |
| 2878 | LOGINFO("adopted storage new block device: '%s'\n", crypto_blkdev); |
| 2879 | Decrypted_Block_Device = crypto_blkdev; |
| 2880 | Is_Decrypted = true; |
| 2881 | Is_Encrypted = true; |
| 2882 | Find_Actual_Block_Device(); |
Kjell Braden | 3126a11 | 2016-06-19 16:58:15 +0000 | [diff] [blame] | 2883 | if (!Mount_Storage_Retry(false)) { |
Ethan Yonker | 66a1949 | 2015-12-10 10:19:45 -0600 | [diff] [blame] | 2884 | LOGERR("Failed to mount decrypted adopted storage device\n"); |
| 2885 | Is_Decrypted = false; |
| 2886 | Is_Encrypted = false; |
| 2887 | cryptfs_revert_ext_volume(part_guid); |
| 2888 | ret = 1; |
| 2889 | } else { |
Ethan Yonker | fcf3f24 | 2016-02-16 12:30:26 -0600 | [diff] [blame] | 2890 | UnMount(false); |
| 2891 | Has_Android_Secure = false; |
| 2892 | Symlink_Path = ""; |
| 2893 | Symlink_Mount_Point = ""; |
| 2894 | Backup_Name = Mount_Point.substr(1); |
| 2895 | Backup_Path = Mount_Point; |
| 2896 | TWPartition* sdext = PartitionManager.Find_Partition_By_Path("/sd-ext"); |
| 2897 | if (sdext && sdext->Actual_Block_Device == Adopted_Block_Device) { |
| 2898 | LOGINFO("Removing /sd-ext from partition list due to adopted storage\n"); |
| 2899 | PartitionManager.Remove_Partition_By_Path("/sd-ext"); |
| 2900 | } |
Ethan Yonker | 66a1949 | 2015-12-10 10:19:45 -0600 | [diff] [blame] | 2901 | Setup_Data_Media(); |
| 2902 | Recreate_Media_Folder(); |
| 2903 | Wipe_Available_in_GUI = true; |
| 2904 | Wipe_During_Factory_Reset = true; |
| 2905 | Can_Be_Backed_Up = true; |
| 2906 | Can_Encrypt_Backup = true; |
| 2907 | Use_Userdata_Encryption = true; |
| 2908 | Is_Storage = true; |
| 2909 | Storage_Name = "Adopted Storage"; |
| 2910 | Is_SubPartition = true; |
| 2911 | SubPartition_Of = "/data"; |
| 2912 | PartitionManager.Add_MTP_Storage(MTP_Storage_ID); |
| 2913 | DataManager::SetValue("tw_has_adopted_storage", 1); |
| 2914 | } |
| 2915 | } else { |
| 2916 | LOGERR("Failed to setup adopted storage decryption\n"); |
| 2917 | } |
| 2918 | } |
| 2919 | exit: |
| 2920 | return ret; |
| 2921 | #else |
| 2922 | LOGINFO("Decrypt_Adopted: no crypto support\n"); |
| 2923 | return 1; |
| 2924 | #endif |
| 2925 | } |
| 2926 | |
| 2927 | void TWPartition::Revert_Adopted() { |
| 2928 | #ifdef TW_INCLUDE_CRYPTO |
| 2929 | if (!Adopted_GUID.empty()) { |
| 2930 | PartitionManager.Remove_MTP_Storage(Mount_Point); |
| 2931 | UnMount(false); |
| 2932 | cryptfs_revert_ext_volume(Adopted_GUID.c_str()); |
| 2933 | Is_Adopted_Storage = false; |
| 2934 | Is_Encrypted = false; |
| 2935 | Is_Decrypted = false; |
| 2936 | Decrypted_Block_Device = ""; |
| 2937 | Find_Actual_Block_Device(); |
| 2938 | Wipe_During_Factory_Reset = false; |
| 2939 | Can_Be_Backed_Up = false; |
| 2940 | Can_Encrypt_Backup = false; |
| 2941 | Use_Userdata_Encryption = false; |
| 2942 | Is_SubPartition = false; |
| 2943 | SubPartition_Of = ""; |
| 2944 | Has_Data_Media = false; |
| 2945 | Storage_Path = Mount_Point; |
| 2946 | if (!Symlink_Mount_Point.empty()) { |
| 2947 | TWPartition* Dat = PartitionManager.Find_Partition_By_Path("/data"); |
| 2948 | if (Dat) { |
| 2949 | Dat->UnMount(false); |
| 2950 | Dat->Symlink_Mount_Point = Symlink_Mount_Point; |
| 2951 | } |
| 2952 | Symlink_Mount_Point = ""; |
| 2953 | } |
| 2954 | } |
| 2955 | #else |
| 2956 | LOGINFO("Revert_Adopted: no crypto support\n"); |
| 2957 | #endif |
| 2958 | } |