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