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