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 <grp.h> |
| 32 | #include <iostream> |
| 33 | #include <libgen.h> |
| 34 | #include <pwd.h> |
| 35 | #include <zlib.h> |
| 36 | #include <sstream> |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 37 | |
Ethan Yonker | 007de98 | 2018-08-31 14:23:16 -0500 | [diff] [blame] | 38 | #include "cutils/properties.h" |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 39 | #include "libblkid/include/blkid.h" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 40 | #include "variables.h" |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 41 | #include "twcommon.h" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 42 | #include "partitions.hpp" |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 43 | #include "data.hpp" |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 44 | #include "twrp-functions.hpp" |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 45 | #include "twrpTar.hpp" |
Ethan Yonker | 3fdcda4 | 2016-11-30 12:29:37 -0600 | [diff] [blame] | 46 | #include "exclude.hpp" |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 47 | #include "infomanager.hpp" |
Ethan Yonker | 4b94cfd | 2014-12-11 10:00:45 -0600 | [diff] [blame] | 48 | #include "set_metadata.h" |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 49 | #include "gui/gui.hpp" |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 50 | #include "adbbu/libtwadbbu.hpp" |
Ethan Yonker | 98661c1 | 2018-10-17 08:39:28 -0500 | [diff] [blame] | 51 | #ifdef TW_INCLUDE_CRYPTO |
| 52 | #include "crypto/fde/cryptfs.h" |
| 53 | #ifdef TW_INCLUDE_FBE |
| 54 | #include "crypto/ext4crypt/Decrypt.h" |
| 55 | #endif |
| 56 | #else |
| 57 | #define CRYPT_FOOTER_OFFSET 0x4000 |
| 58 | #endif |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 59 | extern "C" { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 60 | #include "mtdutils/mtdutils.h" |
| 61 | #include "mtdutils/mounts.h" |
Dees_Troy | a95f55c | 2013-08-17 13:14:43 +0000 | [diff] [blame] | 62 | #ifdef USE_EXT4 |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 63 | // #include "make_ext4fs.h" TODO need ifdef for android8 |
| 64 | #include <ext4_utils/make_ext4fs.h> |
Dees_Troy | a95f55c | 2013-08-17 13:14:43 +0000 | [diff] [blame] | 65 | #endif |
Ethan Yonker | 71413f4 | 2014-02-26 13:36:08 -0600 | [diff] [blame] | 66 | #ifdef TW_INCLUDE_CRYPTO |
Ethan Yonker | 66a1949 | 2015-12-10 10:19:45 -0600 | [diff] [blame] | 67 | #include "gpt/gpt.h" |
Ethan Yonker | 71413f4 | 2014-02-26 13:36:08 -0600 | [diff] [blame] | 68 | #endif |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 69 | } |
Matt Mower | 8741364 | 2017-01-17 21:14:46 -0600 | [diff] [blame] | 70 | #include <selinux/selinux.h> |
Ethan Yonker | f27497f | 2014-02-09 11:48:33 -0600 | [diff] [blame] | 71 | #include <selinux/label.h> |
Dees Troy | 4159aed | 2014-02-28 17:24:43 +0000 | [diff] [blame] | 72 | #ifdef HAVE_CAPABILITIES |
| 73 | #include <sys/capability.h> |
| 74 | #include <sys/xattr.h> |
| 75 | #include <linux/xattr.h> |
| 76 | #endif |
HashBang | ed974bb | 2016-01-30 14:20:09 -0500 | [diff] [blame] | 77 | #include <sparse_format.h> |
Ethan Yonker | 472f506 | 2016-02-25 13:47:30 -0600 | [diff] [blame] | 78 | #include "progresstracking.hpp" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 79 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 80 | using namespace std; |
| 81 | |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 82 | 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 |
| 83 | |
Dees_Troy | a95f55c | 2013-08-17 13:14:43 +0000 | [diff] [blame] | 84 | extern struct selabel_handle *selinux_handle; |
Ethan Yonker | 6277c79 | 2014-09-15 14:54:30 -0500 | [diff] [blame] | 85 | extern bool datamedia; |
Dees_Troy | a95f55c | 2013-08-17 13:14:43 +0000 | [diff] [blame] | 86 | |
Hashcode | 62bd9e0 | 2013-11-19 21:59:42 -0800 | [diff] [blame] | 87 | struct flag_list { |
| 88 | const char *name; |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 89 | unsigned long flag; |
Hashcode | 62bd9e0 | 2013-11-19 21:59:42 -0800 | [diff] [blame] | 90 | }; |
| 91 | |
Matt Mower | 4ab42b1 | 2016-04-21 13:52:18 -0500 | [diff] [blame] | 92 | const struct flag_list mount_flags[] = { |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 93 | { "noatime", MS_NOATIME }, |
| 94 | { "noexec", MS_NOEXEC }, |
| 95 | { "nosuid", MS_NOSUID }, |
| 96 | { "nodev", MS_NODEV }, |
| 97 | { "nodiratime", MS_NODIRATIME }, |
| 98 | { "ro", MS_RDONLY }, |
| 99 | { "rw", 0 }, |
| 100 | { "remount", MS_REMOUNT }, |
| 101 | { "bind", MS_BIND }, |
| 102 | { "rec", MS_REC }, |
Dees Troy | c4bc30e | 2014-02-03 15:04:19 +0000 | [diff] [blame] | 103 | #ifdef MS_UNBINDABLE |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 104 | { "unbindable", MS_UNBINDABLE }, |
Dees Troy | c4bc30e | 2014-02-03 15:04:19 +0000 | [diff] [blame] | 105 | #endif |
| 106 | #ifdef MS_PRIVATE |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 107 | { "private", MS_PRIVATE }, |
Dees Troy | c4bc30e | 2014-02-03 15:04:19 +0000 | [diff] [blame] | 108 | #endif |
| 109 | #ifdef MS_SLAVE |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 110 | { "slave", MS_SLAVE }, |
Dees Troy | c4bc30e | 2014-02-03 15:04:19 +0000 | [diff] [blame] | 111 | #endif |
| 112 | #ifdef MS_SHARED |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 113 | { "shared", MS_SHARED }, |
Dees Troy | c4bc30e | 2014-02-03 15:04:19 +0000 | [diff] [blame] | 114 | #endif |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 115 | { "sync", MS_SYNCHRONOUS }, |
| 116 | { 0, 0 }, |
| 117 | }; |
| 118 | |
| 119 | const char *ignored_mount_items[] = { |
| 120 | "defaults=", |
| 121 | "errors=", |
Ethan Yonker | 9338282 | 2018-11-01 15:25:31 -0500 | [diff] [blame] | 122 | "latemount", |
| 123 | "sysfs_path=", |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 124 | NULL |
Hashcode | 62bd9e0 | 2013-11-19 21:59:42 -0800 | [diff] [blame] | 125 | }; |
| 126 | |
Matt Mower | 2416a50 | 2016-04-12 19:54:46 -0500 | [diff] [blame] | 127 | enum TW_FSTAB_FLAGS { |
| 128 | TWFLAG_DEFAULTS, // Retain position |
| 129 | TWFLAG_ANDSEC, |
| 130 | TWFLAG_BACKUP, |
| 131 | TWFLAG_BACKUPNAME, |
| 132 | TWFLAG_BLOCKSIZE, |
| 133 | TWFLAG_CANBEWIPED, |
| 134 | TWFLAG_CANENCRYPTBACKUP, |
| 135 | TWFLAG_DISPLAY, |
| 136 | TWFLAG_ENCRYPTABLE, |
Ethan Yonker | e9afc3d | 2018-08-30 15:16:27 -0500 | [diff] [blame] | 137 | TWFLAG_FILEENCRYPTION, |
Matt Mower | 2416a50 | 2016-04-12 19:54:46 -0500 | [diff] [blame] | 138 | TWFLAG_FLASHIMG, |
| 139 | TWFLAG_FORCEENCRYPT, |
| 140 | TWFLAG_FSFLAGS, |
| 141 | TWFLAG_IGNOREBLKID, |
| 142 | TWFLAG_LENGTH, |
| 143 | TWFLAG_MOUNTTODECRYPT, |
| 144 | TWFLAG_REMOVABLE, |
Matt Mower | 2416a50 | 2016-04-12 19:54:46 -0500 | [diff] [blame] | 145 | TWFLAG_SETTINGSSTORAGE, |
| 146 | TWFLAG_STORAGE, |
| 147 | TWFLAG_STORAGENAME, |
| 148 | TWFLAG_SUBPARTITIONOF, |
| 149 | TWFLAG_SYMLINK, |
| 150 | TWFLAG_USERDATAENCRYPTBACKUP, |
| 151 | TWFLAG_USERMRF, |
| 152 | TWFLAG_WIPEDURINGFACTORYRESET, |
| 153 | TWFLAG_WIPEINGUI, |
Ethan Yonker | 1b19016 | 2016-12-05 15:25:19 -0600 | [diff] [blame] | 154 | TWFLAG_SLOTSELECT, |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 155 | TWFLAG_WAIT, |
| 156 | TWFLAG_VERIFY, |
| 157 | TWFLAG_CHECK, |
| 158 | TWFLAG_ALTDEVICE, |
| 159 | TWFLAG_NOTRIM, |
| 160 | TWFLAG_VOLDMANAGED, |
| 161 | TWFLAG_FORMATTABLE, |
| 162 | TWFLAG_RESIZE, |
Ethan Yonker | 9338282 | 2018-11-01 15:25:31 -0500 | [diff] [blame] | 163 | TWFLAG_KEYDIRECTORY, |
Peter Cai | 28ea7b0 | 2019-05-24 11:38:54 +0800 | [diff] [blame] | 164 | TWFLAG_WRAPPEDKEY, |
Matt Mower | 2416a50 | 2016-04-12 19:54:46 -0500 | [diff] [blame] | 165 | }; |
| 166 | |
| 167 | /* Flags without a trailing '=' are considered dual format flags and can be |
| 168 | * written as either 'flagname' or 'flagname=', where the character following |
| 169 | * the '=' is Y,y,1 for true and false otherwise. |
| 170 | */ |
| 171 | const struct flag_list tw_flags[] = { |
| 172 | { "andsec", TWFLAG_ANDSEC }, |
| 173 | { "backup", TWFLAG_BACKUP }, |
| 174 | { "backupname=", TWFLAG_BACKUPNAME }, |
| 175 | { "blocksize=", TWFLAG_BLOCKSIZE }, |
| 176 | { "canbewiped", TWFLAG_CANBEWIPED }, |
| 177 | { "canencryptbackup", TWFLAG_CANENCRYPTBACKUP }, |
| 178 | { "defaults", TWFLAG_DEFAULTS }, |
| 179 | { "display=", TWFLAG_DISPLAY }, |
| 180 | { "encryptable=", TWFLAG_ENCRYPTABLE }, |
Ethan Yonker | e9afc3d | 2018-08-30 15:16:27 -0500 | [diff] [blame] | 181 | { "fileencryption=", TWFLAG_FILEENCRYPTION }, |
Matt Mower | 2416a50 | 2016-04-12 19:54:46 -0500 | [diff] [blame] | 182 | { "flashimg", TWFLAG_FLASHIMG }, |
| 183 | { "forceencrypt=", TWFLAG_FORCEENCRYPT }, |
| 184 | { "fsflags=", TWFLAG_FSFLAGS }, |
| 185 | { "ignoreblkid", TWFLAG_IGNOREBLKID }, |
| 186 | { "length=", TWFLAG_LENGTH }, |
| 187 | { "mounttodecrypt", TWFLAG_MOUNTTODECRYPT }, |
| 188 | { "removable", TWFLAG_REMOVABLE }, |
Matt Mower | 2416a50 | 2016-04-12 19:54:46 -0500 | [diff] [blame] | 189 | { "settingsstorage", TWFLAG_SETTINGSSTORAGE }, |
| 190 | { "storage", TWFLAG_STORAGE }, |
| 191 | { "storagename=", TWFLAG_STORAGENAME }, |
| 192 | { "subpartitionof=", TWFLAG_SUBPARTITIONOF }, |
| 193 | { "symlink=", TWFLAG_SYMLINK }, |
| 194 | { "userdataencryptbackup", TWFLAG_USERDATAENCRYPTBACKUP }, |
| 195 | { "usermrf", TWFLAG_USERMRF }, |
| 196 | { "wipeduringfactoryreset", TWFLAG_WIPEDURINGFACTORYRESET }, |
| 197 | { "wipeingui", TWFLAG_WIPEINGUI }, |
Ethan Yonker | 1b19016 | 2016-12-05 15:25:19 -0600 | [diff] [blame] | 198 | { "slotselect", TWFLAG_SLOTSELECT }, |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 199 | { "wait", TWFLAG_WAIT }, |
| 200 | { "verify", TWFLAG_VERIFY }, |
| 201 | { "check", TWFLAG_CHECK }, |
| 202 | { "altdevice", TWFLAG_ALTDEVICE }, |
| 203 | { "notrim", TWFLAG_NOTRIM }, |
| 204 | { "voldmanaged=", TWFLAG_VOLDMANAGED }, |
| 205 | { "formattable", TWFLAG_FORMATTABLE }, |
| 206 | { "resize", TWFLAG_RESIZE }, |
Ethan Yonker | 9338282 | 2018-11-01 15:25:31 -0500 | [diff] [blame] | 207 | { "keydirectory=", TWFLAG_KEYDIRECTORY }, |
Peter Cai | 28ea7b0 | 2019-05-24 11:38:54 +0800 | [diff] [blame] | 208 | { "wrappedkey", TWFLAG_WRAPPEDKEY }, |
Matt Mower | 2416a50 | 2016-04-12 19:54:46 -0500 | [diff] [blame] | 209 | { 0, 0 }, |
| 210 | }; |
| 211 | |
that | 9e0593e | 2014-10-08 00:01:24 +0200 | [diff] [blame] | 212 | TWPartition::TWPartition() { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 213 | Can_Be_Mounted = false; |
| 214 | Can_Be_Wiped = false; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 215 | Can_Be_Backed_Up = false; |
Vojtech Bocek | 1dc3098 | 2013-08-30 21:49:30 +0200 | [diff] [blame] | 216 | Use_Rm_Rf = false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 217 | Wipe_During_Factory_Reset = false; |
| 218 | Wipe_Available_in_GUI = false; |
| 219 | Is_SubPartition = false; |
Dees_Troy | 2691f9d | 2012-09-24 11:15:49 -0400 | [diff] [blame] | 220 | Has_SubPartition = false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 221 | SubPartition_Of = ""; |
| 222 | Symlink_Path = ""; |
| 223 | Symlink_Mount_Point = ""; |
| 224 | Mount_Point = ""; |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 225 | Backup_Path = ""; |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 226 | Wildcard_Block_Device = false; |
| 227 | Sysfs_Entry = ""; |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 228 | Actual_Block_Device = ""; |
| 229 | Primary_Block_Device = ""; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 230 | Alternate_Block_Device = ""; |
| 231 | Removable = false; |
| 232 | Is_Present = false; |
| 233 | Length = 0; |
| 234 | Size = 0; |
| 235 | Used = 0; |
| 236 | Free = 0; |
| 237 | Backup_Size = 0; |
| 238 | Can_Be_Encrypted = false; |
| 239 | Is_Encrypted = false; |
| 240 | Is_Decrypted = false; |
Ethan Yonker | bd7492d | 2016-12-07 13:55:01 -0600 | [diff] [blame] | 241 | Is_FBE = false; |
Ethan Yonker | 253368a | 2014-11-25 15:00:52 -0600 | [diff] [blame] | 242 | Mount_To_Decrypt = false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 243 | Decrypted_Block_Device = ""; |
| 244 | Display_Name = ""; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 245 | Backup_Display_Name = ""; |
| 246 | Storage_Name = ""; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 247 | Backup_Name = ""; |
Dees_Troy | 63c8df7 | 2012-09-10 14:02:05 -0400 | [diff] [blame] | 248 | Backup_FileName = ""; |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 249 | MTD_Name = ""; |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 250 | Backup_Method = BM_NONE; |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 251 | Can_Encrypt_Backup = false; |
| 252 | Use_Userdata_Encryption = false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 253 | Has_Data_Media = false; |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 254 | Has_Android_Secure = false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 255 | Is_Storage = false; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 256 | Is_Settings_Storage = false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 257 | Storage_Path = ""; |
| 258 | Current_File_System = ""; |
| 259 | Fstab_File_System = ""; |
Hashcode | 62bd9e0 | 2013-11-19 21:59:42 -0800 | [diff] [blame] | 260 | Mount_Flags = 0; |
| 261 | Mount_Options = ""; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 262 | Format_Block_Size = 0; |
Dees_Troy | 68cab49 | 2012-12-12 19:29:35 +0000 | [diff] [blame] | 263 | Ignore_Blkid = false; |
dianlujitao | 4879b37 | 2018-12-03 18:45:47 +0800 | [diff] [blame] | 264 | Crypto_Key_Location = ""; |
Ethan Yonker | 726a020 | 2014-12-16 20:01:38 -0600 | [diff] [blame] | 265 | MTP_Storage_ID = 0; |
Ethan Yonker | 96af84a | 2015-01-05 14:58:36 -0600 | [diff] [blame] | 266 | Can_Flash_Img = false; |
Ethan Yonker | eb32b1f | 2015-05-18 10:23:03 -0500 | [diff] [blame] | 267 | Mount_Read_Only = false; |
Ethan Yonker | 66a1949 | 2015-12-10 10:19:45 -0600 | [diff] [blame] | 268 | Is_Adopted_Storage = false; |
| 269 | Adopted_GUID = ""; |
Ethan Yonker | 1b19016 | 2016-12-05 15:25:19 -0600 | [diff] [blame] | 270 | SlotSelect = false; |
Ethan Yonker | 9338282 | 2018-11-01 15:25:31 -0500 | [diff] [blame] | 271 | Key_Directory = ""; |
bigbiff | ee7b7ff | 2020-03-23 15:08:27 -0400 | [diff] [blame] | 272 | Is_Super = false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | TWPartition::~TWPartition(void) { |
| 276 | // Do nothing |
| 277 | } |
| 278 | |
Chaosmaster | f6e42ce | 2020-01-27 00:17:04 +0100 | [diff] [blame] | 279 | bool TWPartition::Process_Fstab_Line(const char *fstab_line, bool Display_Error, std::map<string, Flags_Map> *twrp_flags, bool Sar_Detect) { |
Matt Mower | 2b2dd15 | 2016-04-26 11:24:08 -0500 | [diff] [blame] | 280 | char full_line[MAX_FSTAB_LINE_LENGTH]; |
Matt Mower | 2416a50 | 2016-04-12 19:54:46 -0500 | [diff] [blame] | 281 | char twflags[MAX_FSTAB_LINE_LENGTH] = ""; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 282 | char* ptr; |
Matt Mower | 2b2dd15 | 2016-04-26 11:24:08 -0500 | [diff] [blame] | 283 | int line_len = strlen(fstab_line), index = 0, item_index = 0; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 284 | bool skip = false; |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 285 | int fstab_version = 1, mount_point_index = 0, fs_index = 1, block_device_index = 2; |
| 286 | TWPartition *additional_entry = NULL; |
| 287 | std::map<string, Flags_Map>::iterator it; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 288 | |
Matt Mower | 2b2dd15 | 2016-04-26 11:24:08 -0500 | [diff] [blame] | 289 | strlcpy(full_line, fstab_line, sizeof(full_line)); |
bigbiff | ee7b7ff | 2020-03-23 15:08:27 -0400 | [diff] [blame] | 290 | |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 291 | for (index = 0; index < line_len; index++) { |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 292 | if (full_line[index] == 34) |
| 293 | skip = !skip; |
| 294 | if (!skip && full_line[index] <= 32) |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 295 | full_line[index] = '\0'; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 296 | } |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 297 | if (line_len < 10) |
| 298 | return false; // There can't possibly be a valid fstab line that is less than 10 chars |
| 299 | if (strncmp(fstab_line, "/dev/", strlen("/dev/")) == 0 || strncmp(fstab_line, "/devices/", strlen("/devices/")) == 0) { |
| 300 | fstab_version = 2; |
| 301 | block_device_index = 0; |
| 302 | mount_point_index = 1; |
| 303 | fs_index = 2; |
| 304 | } |
| 305 | |
bigbiff | ee7b7ff | 2020-03-23 15:08:27 -0400 | [diff] [blame] | 306 | Is_Super = PartitionManager.Is_Super_Partition(fstab_line); |
| 307 | if (Is_Super) { |
| 308 | block_device_index = 0; |
| 309 | fstab_version = 2; |
| 310 | mount_point_index = 1; |
| 311 | fs_index = 2; |
| 312 | } |
| 313 | |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 314 | index = 0; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 315 | while (index < line_len) { |
| 316 | while (index < line_len && full_line[index] == '\0') |
| 317 | index++; |
| 318 | if (index >= line_len) |
| 319 | continue; |
| 320 | ptr = full_line + index; |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 321 | if (item_index == mount_point_index) { |
| 322 | Mount_Point = ptr; |
bigbiff | ee7b7ff | 2020-03-23 15:08:27 -0400 | [diff] [blame] | 323 | if (fstab_version == 2 && Is_Super == false) { |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 324 | additional_entry = PartitionManager.Find_Partition_By_Path(Mount_Point); |
Chaosmaster | f6e42ce | 2020-01-27 00:17:04 +0100 | [diff] [blame] | 325 | if (!Sar_Detect && additional_entry) { |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 326 | LOGINFO("Found an additional entry for '%s'\n", Mount_Point.c_str()); |
| 327 | } |
| 328 | } |
Chaosmaster | f6e42ce | 2020-01-27 00:17:04 +0100 | [diff] [blame] | 329 | if(!Sar_Detect) |
| 330 | LOGINFO("Processing '%s'\n", Mount_Point.c_str()); |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 331 | Backup_Path = Mount_Point; |
| 332 | Storage_Path = Mount_Point; |
| 333 | Display_Name = ptr + 1; |
| 334 | Backup_Display_Name = Display_Name; |
| 335 | Storage_Name = Display_Name; |
| 336 | item_index++; |
| 337 | } else if (item_index == fs_index) { |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 338 | // File System |
| 339 | Fstab_File_System = ptr; |
| 340 | Current_File_System = ptr; |
| 341 | item_index++; |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 342 | } else if (item_index == block_device_index) { |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 343 | // Primary Block Device |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 344 | if (Fstab_File_System == "mtd" || Fstab_File_System == "yaffs2") { |
Dees_Troy | 094207a | 2012-09-26 12:00:39 -0400 | [diff] [blame] | 345 | MTD_Name = ptr; |
| 346 | Find_MTD_Block_Device(MTD_Name); |
Dees_Troy | 5fcd8f9 | 2012-10-16 12:22:05 -0400 | [diff] [blame] | 347 | } else if (Fstab_File_System == "bml") { |
| 348 | if (Mount_Point == "/boot") |
| 349 | MTD_Name = "boot"; |
| 350 | else if (Mount_Point == "/recovery") |
| 351 | MTD_Name = "recovery"; |
| 352 | Primary_Block_Device = ptr; |
| 353 | if (*ptr != '/') |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 354 | 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] | 355 | } else if (*ptr != '/') { |
bigbiff | ee7b7ff | 2020-03-23 15:08:27 -0400 | [diff] [blame] | 356 | if (!Is_Super) { |
| 357 | if (Display_Error) |
| 358 | LOGERR("Invalid block device '%s' in fstab line '%s'", ptr, fstab_line); |
| 359 | else |
| 360 | LOGINFO("Invalid block device '%s' in fstab line '%s'", ptr, fstab_line); |
| 361 | return false; |
| 362 | } |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 363 | } else { |
| 364 | Primary_Block_Device = ptr; |
| 365 | Find_Real_Block_Device(Primary_Block_Device, Display_Error); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 366 | } |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 367 | item_index++; |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 368 | } else if (item_index > 2) { |
| 369 | if (fstab_version == 2) { |
| 370 | if (item_index == 3) { |
| 371 | Process_FS_Flags(ptr); |
| 372 | if (additional_entry) { |
| 373 | additional_entry->Save_FS_Flags(Fstab_File_System, Mount_Flags, Mount_Options); |
| 374 | return false; // We save the extra fs flags in the other partition entry and by returning false, this entry will be deleted |
| 375 | } |
| 376 | } else { |
| 377 | strlcpy(twflags, ptr, sizeof(twflags)); |
| 378 | } |
| 379 | item_index++; |
| 380 | } else if (*ptr == '/') { // v2 fstab does not allow alternate block devices |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 381 | // Alternate Block Device |
| 382 | Alternate_Block_Device = ptr; |
| 383 | Find_Real_Block_Device(Alternate_Block_Device, Display_Error); |
| 384 | } else if (strlen(ptr) > 7 && strncmp(ptr, "length=", 7) == 0) { |
| 385 | // Partition length |
| 386 | ptr += 7; |
| 387 | Length = atoi(ptr); |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 388 | } else if (strlen(ptr) > 6 && strncmp(ptr, "flags=", 6) == 0) { |
| 389 | // Custom flags, save for later so that new values aren't overwritten by defaults |
| 390 | ptr += 6; |
Matt Mower | 2416a50 | 2016-04-12 19:54:46 -0500 | [diff] [blame] | 391 | strlcpy(twflags, ptr, sizeof(twflags)); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 392 | } else if (strlen(ptr) == 4 && (strncmp(ptr, "NULL", 4) == 0 || strncmp(ptr, "null", 4) == 0 || strncmp(ptr, "null", 4) == 0)) { |
| 393 | // Do nothing |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 394 | } else { |
| 395 | // Unhandled data |
Matt Mower | 2b2dd15 | 2016-04-26 11:24:08 -0500 | [diff] [blame] | 396 | 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] | 397 | } |
| 398 | } |
| 399 | while (index < line_len && full_line[index] != '\0') |
| 400 | index++; |
| 401 | } |
| 402 | |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 403 | // override block devices from the v2 fstab with the ones we read from the twrp.flags file in case they are different |
| 404 | if (fstab_version == 2 && twrp_flags && twrp_flags->size() > 0) { |
| 405 | it = twrp_flags->find(Mount_Point); |
| 406 | if (it != twrp_flags->end()) { |
| 407 | if (!it->second.Primary_Block_Device.empty()) { |
| 408 | Primary_Block_Device = it->second.Primary_Block_Device; |
| 409 | Find_Real_Block_Device(Primary_Block_Device, Display_Error); |
| 410 | } |
| 411 | if (!it->second.Alternate_Block_Device.empty()) { |
| 412 | Alternate_Block_Device = it->second.Alternate_Block_Device; |
| 413 | Find_Real_Block_Device(Alternate_Block_Device, Display_Error); |
| 414 | } |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | if (strncmp(fstab_line, "/devices/", strlen("/devices/")) == 0) { |
| 419 | Sysfs_Entry = Primary_Block_Device; |
| 420 | Primary_Block_Device = ""; |
| 421 | Is_Storage = true; |
| 422 | Removable = true; |
| 423 | Wipe_Available_in_GUI = true; |
| 424 | Wildcard_Block_Device = true; |
| 425 | } |
| 426 | if (Primary_Block_Device.find("*") != string::npos) |
| 427 | Wildcard_Block_Device = true; |
| 428 | |
Chaosmaster | f6e42ce | 2020-01-27 00:17:04 +0100 | [diff] [blame] | 429 | if (Sar_Detect) { |
| 430 | if(Is_File_System(Fstab_File_System) && (Mount_Point == "/" || Mount_Point == "/system" || Mount_Point == "/system_root")) |
| 431 | Find_Actual_Block_Device(); |
| 432 | else |
| 433 | return true; |
| 434 | } else if (Mount_Point == "auto") { |
dianlujitao | b76a73a | 2020-04-30 20:33:20 +0800 | [diff] [blame] | 435 | Mount_Point = "/auto" + to_string(auto_index); |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 436 | Backup_Path = Mount_Point; |
| 437 | Storage_Path = Mount_Point; |
dianlujitao | b76a73a | 2020-04-30 20:33:20 +0800 | [diff] [blame] | 438 | Backup_Name = Mount_Point.substr(1); |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 439 | auto_index++; |
| 440 | Setup_File_System(Display_Error); |
| 441 | Display_Name = "Storage"; |
| 442 | Backup_Display_Name = Display_Name; |
| 443 | Storage_Name = Display_Name; |
| 444 | Can_Be_Backed_Up = false; |
| 445 | Wipe_Available_in_GUI = true; |
| 446 | Is_Storage = true; |
| 447 | Removable = true; |
| 448 | Wipe_Available_in_GUI = true; |
| 449 | } 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] | 450 | if (Display_Error) |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 451 | LOGERR("Unknown File System: '%s'\n", Fstab_File_System.c_str()); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 452 | else |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 453 | LOGINFO("Unknown File System: '%s'\n", Fstab_File_System.c_str()); |
Matt Mower | 72c87ce | 2016-04-26 14:34:56 -0500 | [diff] [blame] | 454 | return false; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 455 | } else if (Is_File_System(Fstab_File_System)) { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 456 | Find_Actual_Block_Device(); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 457 | Setup_File_System(Display_Error); |
dianlujitao | b76a73a | 2020-04-30 20:33:20 +0800 | [diff] [blame] | 458 | Backup_Name = Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1); |
Chaosmaster | 3b68273 | 2020-01-25 21:09:28 +0100 | [diff] [blame] | 459 | if (Mount_Point == "/" || Mount_Point == "/system" || Mount_Point == "/system_root") { |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 460 | Display_Name = "System"; |
dianlujitao | b76a73a | 2020-04-30 20:33:20 +0800 | [diff] [blame] | 461 | Backup_Name = "system"; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 462 | Backup_Display_Name = Display_Name; |
| 463 | Storage_Name = Display_Name; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 464 | Wipe_Available_in_GUI = true; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 465 | Can_Be_Backed_Up = true; |
Ethan Yonker | eb32b1f | 2015-05-18 10:23:03 -0500 | [diff] [blame] | 466 | Mount_Read_Only = true; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 467 | } else if (Mount_Point == "/data") { |
| 468 | Display_Name = "Data"; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 469 | Backup_Display_Name = Display_Name; |
| 470 | Storage_Name = Display_Name; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 471 | Wipe_Available_in_GUI = true; |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 472 | Wipe_During_Factory_Reset = true; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 473 | Can_Be_Backed_Up = true; |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 474 | Can_Encrypt_Backup = true; |
| 475 | Use_Userdata_Encryption = true; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 476 | } else if (Mount_Point == "/cache") { |
| 477 | Display_Name = "Cache"; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 478 | Backup_Display_Name = Display_Name; |
| 479 | Storage_Name = Display_Name; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 480 | Wipe_Available_in_GUI = true; |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 481 | Wipe_During_Factory_Reset = true; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 482 | Can_Be_Backed_Up = true; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 483 | } else if (Mount_Point == "/datadata") { |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 484 | Wipe_During_Factory_Reset = true; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 485 | Display_Name = "DataData"; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 486 | Backup_Display_Name = Display_Name; |
| 487 | Storage_Name = Display_Name; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 488 | Is_SubPartition = true; |
| 489 | SubPartition_Of = "/data"; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 490 | DataManager::SetValue(TW_HAS_DATADATA, 1); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 491 | Can_Be_Backed_Up = true; |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 492 | Can_Encrypt_Backup = true; |
| 493 | Use_Userdata_Encryption = false; // This whole partition should be encrypted |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 494 | } else if (Mount_Point == "/sd-ext") { |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 495 | Wipe_During_Factory_Reset = true; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 496 | Display_Name = "SD-Ext"; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 497 | Backup_Display_Name = Display_Name; |
| 498 | Storage_Name = Display_Name; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 499 | Wipe_Available_in_GUI = true; |
Dees_Troy | c51f1f9 | 2012-09-20 15:32:13 -0400 | [diff] [blame] | 500 | Removable = true; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 501 | Can_Be_Backed_Up = true; |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 502 | Can_Encrypt_Backup = true; |
| 503 | Use_Userdata_Encryption = true; |
Dees_Troy | 2c50e18 | 2012-09-26 20:05:28 -0400 | [diff] [blame] | 504 | } else if (Mount_Point == "/boot") { |
| 505 | Display_Name = "Boot"; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 506 | Backup_Display_Name = Display_Name; |
Dees_Troy | 2c50e18 | 2012-09-26 20:05:28 -0400 | [diff] [blame] | 507 | DataManager::SetValue("tw_boot_is_mountable", 1); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 508 | Can_Be_Backed_Up = true; |
Ethan Yonker | 1673e3d | 2015-10-26 21:51:58 -0500 | [diff] [blame] | 509 | } else if (Mount_Point == "/vendor") { |
| 510 | Display_Name = "Vendor"; |
| 511 | Backup_Display_Name = Display_Name; |
| 512 | Storage_Name = Display_Name; |
| 513 | Mount_Read_Only = true; |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 514 | } |
| 515 | #ifdef TW_EXTERNAL_STORAGE_PATH |
| 516 | if (Mount_Point == EXPAND(TW_EXTERNAL_STORAGE_PATH)) { |
| 517 | Is_Storage = true; |
| 518 | Storage_Path = EXPAND(TW_EXTERNAL_STORAGE_PATH); |
Dees_Troy | c51f1f9 | 2012-09-20 15:32:13 -0400 | [diff] [blame] | 519 | Removable = true; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 520 | Wipe_Available_in_GUI = true; |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 521 | #else |
Dees_Troy | 70737fa | 2013-04-08 13:19:20 +0000 | [diff] [blame] | 522 | if (Mount_Point == "/sdcard" || Mount_Point == "/external_sd" || Mount_Point == "/external_sdcard") { |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 523 | Is_Storage = true; |
Dees_Troy | c51f1f9 | 2012-09-20 15:32:13 -0400 | [diff] [blame] | 524 | Removable = true; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 525 | Wipe_Available_in_GUI = true; |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 526 | #endif |
Dees_Troy | b05ddee | 2013-01-28 20:24:50 +0000 | [diff] [blame] | 527 | } |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 528 | #ifdef TW_INTERNAL_STORAGE_PATH |
| 529 | if (Mount_Point == EXPAND(TW_INTERNAL_STORAGE_PATH)) { |
| 530 | Is_Storage = true; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 531 | Is_Settings_Storage = true; |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 532 | Storage_Path = EXPAND(TW_INTERNAL_STORAGE_PATH); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 533 | Wipe_Available_in_GUI = true; |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 534 | } |
| 535 | #else |
Dees_Troy | 70737fa | 2013-04-08 13:19:20 +0000 | [diff] [blame] | 536 | if (Mount_Point == "/emmc" || Mount_Point == "/internal_sd" || Mount_Point == "/internal_sdcard") { |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 537 | Is_Storage = true; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 538 | Is_Settings_Storage = 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 | } |
| 541 | #endif |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 542 | } else if (Is_Image(Fstab_File_System)) { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 543 | Find_Actual_Block_Device(); |
Ethan Yonker | 1b19016 | 2016-12-05 15:25:19 -0600 | [diff] [blame] | 544 | Setup_Image(); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 545 | if (Mount_Point == "/boot") { |
| 546 | Display_Name = "Boot"; |
| 547 | Backup_Display_Name = Display_Name; |
| 548 | Can_Be_Backed_Up = true; |
Ethan Yonker | 96af84a | 2015-01-05 14:58:36 -0600 | [diff] [blame] | 549 | Can_Flash_Img = true; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 550 | } else if (Mount_Point == "/recovery") { |
| 551 | Display_Name = "Recovery"; |
| 552 | Backup_Display_Name = Display_Name; |
Ethan Yonker | 96af84a | 2015-01-05 14:58:36 -0600 | [diff] [blame] | 553 | Can_Flash_Img = true; |
Ethan Yonker | eb32b1f | 2015-05-18 10:23:03 -0500 | [diff] [blame] | 554 | } else if (Mount_Point == "/system_image") { |
| 555 | Display_Name = "System Image"; |
| 556 | Backup_Display_Name = Display_Name; |
HashBang | ed974bb | 2016-01-30 14:20:09 -0500 | [diff] [blame] | 557 | Can_Flash_Img = true; |
Ethan Yonker | eb32b1f | 2015-05-18 10:23:03 -0500 | [diff] [blame] | 558 | Can_Be_Backed_Up = true; |
Ethan Yonker | 1673e3d | 2015-10-26 21:51:58 -0500 | [diff] [blame] | 559 | } else if (Mount_Point == "/vendor_image") { |
| 560 | Display_Name = "Vendor Image"; |
| 561 | Backup_Display_Name = Display_Name; |
HashBang | ed974bb | 2016-01-30 14:20:09 -0500 | [diff] [blame] | 562 | Can_Flash_Img = true; |
Ethan Yonker | 1673e3d | 2015-10-26 21:51:58 -0500 | [diff] [blame] | 563 | Can_Be_Backed_Up = true; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 564 | } |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 565 | } |
| 566 | |
Matt Mower | 2416a50 | 2016-04-12 19:54:46 -0500 | [diff] [blame] | 567 | // Process TWRP fstab flags |
| 568 | if (strlen(twflags) > 0) { |
| 569 | string Prev_Display_Name = Display_Name; |
| 570 | string Prev_Storage_Name = Storage_Name; |
| 571 | string Prev_Backup_Display_Name = Backup_Display_Name; |
| 572 | Display_Name = ""; |
| 573 | Storage_Name = ""; |
| 574 | Backup_Display_Name = ""; |
| 575 | |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 576 | Process_TW_Flags(twflags, (fstab_version == 1), fstab_version); |
| 577 | Save_FS_Flags(Fstab_File_System, Mount_Flags, Mount_Options); |
Matt Mower | 2416a50 | 2016-04-12 19:54:46 -0500 | [diff] [blame] | 578 | |
| 579 | bool has_display_name = !Display_Name.empty(); |
| 580 | bool has_storage_name = !Storage_Name.empty(); |
| 581 | bool has_backup_name = !Backup_Display_Name.empty(); |
| 582 | if (!has_display_name) Display_Name = Prev_Display_Name; |
| 583 | if (!has_storage_name) Storage_Name = Prev_Storage_Name; |
| 584 | if (!has_backup_name) Backup_Display_Name = Prev_Backup_Display_Name; |
| 585 | |
| 586 | if (has_display_name && !has_storage_name) |
| 587 | Storage_Name = Display_Name; |
| 588 | if (!has_display_name && has_storage_name) |
| 589 | Display_Name = Storage_Name; |
| 590 | if (has_display_name && !has_backup_name && Backup_Display_Name != "Android Secure") |
| 591 | Backup_Display_Name = Display_Name; |
| 592 | if (!has_display_name && has_backup_name) |
| 593 | Display_Name = Backup_Display_Name; |
| 594 | } |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 595 | |
| 596 | if (fstab_version == 2 && twrp_flags && twrp_flags->size() > 0) { |
| 597 | it = twrp_flags->find(Mount_Point); |
| 598 | if (it != twrp_flags->end()) { |
| 599 | char twrpflags[MAX_FSTAB_LINE_LENGTH] = ""; |
| 600 | int skip = 0; |
| 601 | string Flags = it->second.Flags; |
| 602 | strcpy(twrpflags, Flags.c_str()); |
| 603 | if (strlen(twrpflags) > strlen("flags=") && strncmp(twrpflags, "flags=", strlen("flags=")) == 0) |
| 604 | skip += strlen("flags="); |
| 605 | char* flagptr = twrpflags; |
| 606 | flagptr += skip; |
| 607 | 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 |
| 608 | } |
| 609 | } |
nkk71 | 98fc399 | 2017-12-16 16:26:42 +0200 | [diff] [blame] | 610 | |
| 611 | if (Mount_Point == "/persist" && Can_Be_Mounted) { |
| 612 | bool mounted = Is_Mounted(); |
| 613 | if (mounted || Mount(false)) { |
| 614 | // Read the backup settings file |
| 615 | DataManager::LoadPersistValues(); |
| 616 | TWFunc::Fixup_Time_On_Boot("/persist/time/"); |
| 617 | if (!mounted) |
| 618 | UnMount(false); |
| 619 | } |
| 620 | } |
| 621 | |
Chaosmaster | f6e42ce | 2020-01-27 00:17:04 +0100 | [diff] [blame] | 622 | if (Is_File_System(Fstab_File_System) && (Mount_Point == "/" || Mount_Point == "/system" || Mount_Point == "/system_root")) { |
| 623 | if (Sar_Detect) { |
| 624 | Mount_Point = "/s"; |
| 625 | Mount_Read_Only = true; |
| 626 | Can_Be_Mounted = true; |
| 627 | } else { |
| 628 | Mount_Point = PartitionManager.Get_Android_Root_Path(); |
| 629 | Backup_Path = Mount_Point; |
| 630 | Storage_Path = Mount_Point; |
| 631 | Make_Dir(Mount_Point, Display_Error); |
| 632 | } |
bigbiff | ee7b7ff | 2020-03-23 15:08:27 -0400 | [diff] [blame] | 633 | if (Is_Super) { |
| 634 | Can_Be_Backed_Up = false; |
| 635 | Can_Be_Wiped = false; |
| 636 | Wipe_Available_in_GUI = false; |
| 637 | } |
Chaosmaster | f6e42ce | 2020-01-27 00:17:04 +0100 | [diff] [blame] | 638 | } |
| 639 | |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 640 | return true; |
| 641 | } |
| 642 | |
Matt Mower | 72c87ce | 2016-04-26 14:34:56 -0500 | [diff] [blame] | 643 | void TWPartition::Partition_Post_Processing(bool Display_Error) { |
| 644 | if (Mount_Point == "/data") |
| 645 | Setup_Data_Partition(Display_Error); |
| 646 | else if (Mount_Point == "/cache") |
| 647 | Setup_Cache_Partition(Display_Error); |
| 648 | } |
| 649 | |
Ethan Yonker | bd7492d | 2016-12-07 13:55:01 -0600 | [diff] [blame] | 650 | void TWPartition::ExcludeAll(const string& path) { |
| 651 | backup_exclusions.add_absolute_dir(path); |
| 652 | wipe_exclusions.add_absolute_dir(path); |
| 653 | } |
| 654 | |
Matt Mower | 72c87ce | 2016-04-26 14:34:56 -0500 | [diff] [blame] | 655 | void TWPartition::Setup_Data_Partition(bool Display_Error) { |
| 656 | if (Mount_Point != "/data") |
| 657 | return; |
| 658 | |
| 659 | // Ensure /data is not mounted as tmpfs for qcom hardware decrypt |
| 660 | UnMount(false); |
| 661 | |
| 662 | #ifdef TW_INCLUDE_CRYPTO |
| 663 | if (datamedia) |
| 664 | Setup_Data_Media(); |
| 665 | Can_Be_Encrypted = true; |
| 666 | char crypto_blkdev[255]; |
| 667 | property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error"); |
| 668 | if (strcmp(crypto_blkdev, "error") != 0) { |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame^] | 669 | Set_FBE_Status(); |
Matt Mower | 72c87ce | 2016-04-26 14:34:56 -0500 | [diff] [blame] | 670 | Decrypted_Block_Device = crypto_blkdev; |
| 671 | LOGINFO("Data already decrypted, new block device: '%s'\n", crypto_blkdev); |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame^] | 672 | DataManager::SetValue(TW_IS_ENCRYPTED, 0); |
Matt Mower | 72c87ce | 2016-04-26 14:34:56 -0500 | [diff] [blame] | 673 | } else if (!Mount(false)) { |
| 674 | if (Is_Present) { |
Ethan Yonker | 9338282 | 2018-11-01 15:25:31 -0500 | [diff] [blame] | 675 | if (Key_Directory.empty()) { |
bigbiff | adc599e | 2020-05-28 19:36:30 +0000 | [diff] [blame] | 676 | 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] | 677 | if (cryptfs_check_footer() == 0) { |
| 678 | Is_Encrypted = true; |
| 679 | Is_Decrypted = false; |
| 680 | Can_Be_Mounted = false; |
| 681 | Current_File_System = "emmc"; |
| 682 | Setup_Image(); |
Ethan Yonker | 9338282 | 2018-11-01 15:25:31 -0500 | [diff] [blame] | 683 | DataManager::SetValue(TW_CRYPTO_PWTYPE, cryptfs_get_password_type()); |
| 684 | DataManager::SetValue(TW_CRYPTO_PASSWORD, ""); |
| 685 | DataManager::SetValue("tw_crypto_display", ""); |
| 686 | } else { |
| 687 | gui_err("mount_data_footer=Could not mount /data and unable to find crypto footer."); |
| 688 | } |
| 689 | } else { |
Matt Mower | 72c87ce | 2016-04-26 14:34:56 -0500 | [diff] [blame] | 690 | Is_Encrypted = true; |
| 691 | Is_Decrypted = false; |
Matt Mower | 72c87ce | 2016-04-26 14:34:56 -0500 | [diff] [blame] | 692 | } |
Ethan Yonker | 9338282 | 2018-11-01 15:25:31 -0500 | [diff] [blame] | 693 | } else if (Key_Directory.empty()) { |
bigbiff | adc599e | 2020-05-28 19:36:30 +0000 | [diff] [blame] | 694 | 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] | 695 | } |
| 696 | } else { |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame^] | 697 | Set_FBE_Status(); |
mauronofrio | 9bf7335 | 2019-11-23 22:27:34 +0100 | [diff] [blame] | 698 | if (!Decrypt_FBE_DE()) { |
mauronofrio | fc79aa6 | 2019-11-23 23:13:04 +0100 | [diff] [blame] | 699 | char wrappedvalue[PROPERTY_VALUE_MAX]; |
| 700 | property_get("fbe.data.wrappedkey", wrappedvalue, ""); |
| 701 | std::string wrappedkeyvalue(wrappedvalue); |
| 702 | if (wrappedkeyvalue == "true") { |
| 703 | LOGERR("Unable to decrypt FBE device\n"); |
| 704 | } else { |
| 705 | LOGINFO("Trying wrapped key.\n"); |
| 706 | property_set("fbe.data.wrappedkey", "true"); |
mauronofrio | 9bf7335 | 2019-11-23 22:27:34 +0100 | [diff] [blame] | 707 | if (!Decrypt_FBE_DE()) { |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 708 | LOGERR("Unable to decrypt FBE device\n"); |
mauronofrio | 9bf7335 | 2019-11-23 22:27:34 +0100 | [diff] [blame] | 709 | } |
mauronofrio | fc79aa6 | 2019-11-23 23:13:04 +0100 | [diff] [blame] | 710 | } |
mauronofrio | 9bf7335 | 2019-11-23 22:27:34 +0100 | [diff] [blame] | 711 | } |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame^] | 712 | DataManager::SetValue(TW_IS_ENCRYPTED, 0); |
Matt Mower | 72c87ce | 2016-04-26 14:34:56 -0500 | [diff] [blame] | 713 | } |
| 714 | if (datamedia && (!Is_Encrypted || (Is_Encrypted && Is_Decrypted))) { |
| 715 | Setup_Data_Media(); |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame^] | 716 | if (!TWFunc::Is_Mount_Wiped("/data")) |
| 717 | Recreate_Media_Folder(); |
Matt Mower | 72c87ce | 2016-04-26 14:34:56 -0500 | [diff] [blame] | 718 | } |
| 719 | #else |
| 720 | if (datamedia) { |
| 721 | Setup_Data_Media(); |
| 722 | Recreate_Media_Folder(); |
| 723 | } |
| 724 | #endif |
| 725 | } |
| 726 | |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame^] | 727 | void TWPartition::Set_FBE_Status() { |
| 728 | DataManager::SetValue(TW_IS_DECRYPTED, 1); |
| 729 | Is_Encrypted = true; |
| 730 | Is_Decrypted = true; |
| 731 | LOGINFO("Setup_Data_Partition::Key_Directory::%s\n", Key_Directory.c_str()); |
| 732 | if (Key_Directory.empty()) { |
| 733 | Is_FBE = false; |
| 734 | DataManager::SetValue(TW_IS_FBE, 0); |
| 735 | } else { |
| 736 | Is_FBE = true; |
| 737 | DataManager::SetValue(TW_IS_FBE, 1); |
| 738 | } |
| 739 | } |
| 740 | |
Ethan Yonker | 9338282 | 2018-11-01 15:25:31 -0500 | [diff] [blame] | 741 | bool TWPartition::Decrypt_FBE_DE() { |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame^] | 742 | if (TWFunc::Path_Exists("/data/unencrypted/key/version")) { |
Captain Throwback | 87d73d6 | 2020-02-17 16:44:50 -0500 | [diff] [blame] | 743 | DataManager::SetValue(TW_IS_FBE, 1); |
Ethan Yonker | 9338282 | 2018-11-01 15:25:31 -0500 | [diff] [blame] | 744 | LOGINFO("File Based Encryption is present\n"); |
| 745 | #ifdef TW_INCLUDE_FBE |
bigbiff | adc599e | 2020-05-28 19:36:30 +0000 | [diff] [blame] | 746 | ExcludeAll(Mount_Point + "/convert_fbe"); |
| 747 | ExcludeAll(Mount_Point + "/unencrypted"); |
| 748 | //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 |
| 749 | ExcludeAll(Mount_Point + "/misc/vold/user_keys"); |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame^] | 750 | ExcludeAll(Mount_Point + "/misc/vold/volume_keys"); |
bigbiff | adc599e | 2020-05-28 19:36:30 +0000 | [diff] [blame] | 751 | //ExcludeAll(Mount_Point + "/system_ce"); |
| 752 | //ExcludeAll(Mount_Point + "/system_de"); |
| 753 | //ExcludeAll(Mount_Point + "/misc_ce"); |
| 754 | //ExcludeAll(Mount_Point + "/misc_de"); |
| 755 | ExcludeAll(Mount_Point + "/system/gatekeeper.password.key"); |
| 756 | ExcludeAll(Mount_Point + "/system/gatekeeper.pattern.key"); |
| 757 | ExcludeAll(Mount_Point + "/system/locksettings.db"); |
| 758 | //ExcludeAll(Mount_Point + "/system/locksettings.db-shm"); // don't seem to need this one, but the other 2 are needed |
| 759 | ExcludeAll(Mount_Point + "/system/locksettings.db-wal"); |
| 760 | //ExcludeAll(Mount_Point + "/user_de"); |
| 761 | //ExcludeAll(Mount_Point + "/misc/profiles/cur/0"); // might be important later |
| 762 | ExcludeAll(Mount_Point + "/misc/gatekeeper"); |
| 763 | ExcludeAll(Mount_Point + "/misc/keystore"); |
| 764 | ExcludeAll(Mount_Point + "/drm/kek.dat"); |
| 765 | 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^] | 766 | ExcludeAll(Mount_Point + "/per_boot"); // removed each boot by init |
| 767 | |
bigbiff | adc599e | 2020-05-28 19:36:30 +0000 | [diff] [blame] | 768 | int retry_count = 3; |
| 769 | while (!Decrypt_DE() && --retry_count) |
| 770 | usleep(2000); |
| 771 | if (retry_count > 0) { |
| 772 | property_set("ro.crypto.state", "encrypted"); |
| 773 | Is_Encrypted = true; |
| 774 | Is_Decrypted = false; |
| 775 | Is_FBE = true; |
| 776 | DataManager::SetValue(TW_IS_FBE, 1); |
bigbiff | adc599e | 2020-05-28 19:36:30 +0000 | [diff] [blame] | 777 | string filename; |
| 778 | int pwd_type = Get_Password_Type(0, filename); |
| 779 | if (pwd_type < 0) { |
| 780 | LOGERR("This TWRP does not have synthetic password decrypt support\n"); |
| 781 | pwd_type = 0; // default password |
| 782 | } |
| 783 | DataManager::SetValue(TW_CRYPTO_PWTYPE, pwd_type); |
| 784 | DataManager::SetValue(TW_CRYPTO_PASSWORD, ""); |
| 785 | DataManager::SetValue("tw_crypto_display", ""); |
| 786 | return true; |
Ethan Yonker | 9338282 | 2018-11-01 15:25:31 -0500 | [diff] [blame] | 787 | } |
| 788 | #else |
| 789 | LOGERR("FBE found but FBE support not present in TWRP\n"); |
| 790 | #endif |
| 791 | } |
Captain Throwback | 87d73d6 | 2020-02-17 16:44:50 -0500 | [diff] [blame] | 792 | DataManager::SetValue(TW_IS_FBE, 0); |
Ethan Yonker | 9338282 | 2018-11-01 15:25:31 -0500 | [diff] [blame] | 793 | return false; |
| 794 | } |
| 795 | |
Matt Mower | 72c87ce | 2016-04-26 14:34:56 -0500 | [diff] [blame] | 796 | void TWPartition::Setup_Cache_Partition(bool Display_Error __unused) { |
| 797 | if (Mount_Point != "/cache") |
| 798 | return; |
| 799 | |
| 800 | if (!Mount(true)) |
| 801 | return; |
| 802 | |
| 803 | if (!TWFunc::Path_Exists("/cache/recovery/.")) { |
| 804 | LOGINFO("Recreating /cache/recovery folder\n"); |
| 805 | if (mkdir("/cache/recovery", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0) |
| 806 | LOGERR("Could not create /cache/recovery\n"); |
| 807 | } |
| 808 | } |
| 809 | |
Matt Mower | 4ab42b1 | 2016-04-21 13:52:18 -0500 | [diff] [blame] | 810 | void TWPartition::Process_FS_Flags(const char *str) { |
| 811 | char *options = strdup(str); |
| 812 | char *ptr, *savep; |
Hashcode | 62bd9e0 | 2013-11-19 21:59:42 -0800 | [diff] [blame] | 813 | |
Matt Mower | 4ab42b1 | 2016-04-21 13:52:18 -0500 | [diff] [blame] | 814 | Mount_Options = ""; |
Hashcode | 62bd9e0 | 2013-11-19 21:59:42 -0800 | [diff] [blame] | 815 | |
Matt Mower | 4ab42b1 | 2016-04-21 13:52:18 -0500 | [diff] [blame] | 816 | // Avoid issues with potentially nested strtok by using strtok_r |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 817 | for (ptr = strtok_r(options, ",", &savep); ptr; ptr = strtok_r(NULL, ",", &savep)) { |
| 818 | char *equals = strstr(ptr, "="); |
| 819 | size_t name_len; |
Matt Mower | 4ab42b1 | 2016-04-21 13:52:18 -0500 | [diff] [blame] | 820 | |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 821 | if (!equals) |
| 822 | name_len = strlen(ptr); |
| 823 | else |
| 824 | name_len = equals - ptr; |
| 825 | |
| 826 | // There are some flags that we want to ignore in TWRP |
| 827 | bool found_match = false; |
| 828 | for (const char** ignored_mount_item = ignored_mount_items; *ignored_mount_item; ignored_mount_item++) { |
| 829 | if (strncmp(ptr, *ignored_mount_item, name_len) == 0) { |
| 830 | found_match = true; |
Hashcode | 62bd9e0 | 2013-11-19 21:59:42 -0800 | [diff] [blame] | 831 | break; |
| 832 | } |
| 833 | } |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 834 | if (found_match) |
| 835 | continue; |
Hashcode | 62bd9e0 | 2013-11-19 21:59:42 -0800 | [diff] [blame] | 836 | |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 837 | // mount_flags are never postfixed by '=' |
| 838 | if (!equals) { |
| 839 | const struct flag_list* mount_flag = mount_flags; |
| 840 | for (; mount_flag->name; mount_flag++) { |
| 841 | if (strcmp(ptr, mount_flag->name) == 0) { |
| 842 | if (mount_flag->flag == MS_RDONLY) |
| 843 | Mount_Read_Only = true; |
| 844 | else |
| 845 | Mount_Flags |= (unsigned)mount_flag->flag; |
| 846 | found_match = true; |
| 847 | break; |
| 848 | } |
| 849 | } |
| 850 | if (found_match) |
| 851 | continue; |
Matt Mower | 4ab42b1 | 2016-04-21 13:52:18 -0500 | [diff] [blame] | 852 | } |
| 853 | |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 854 | // If we aren't ignoring this flag and it's not a mount flag, then it must be a mount option |
| 855 | if (!Mount_Options.empty()) |
| 856 | Mount_Options += ","; |
| 857 | Mount_Options += ptr; |
Matt Mower | 4ab42b1 | 2016-04-21 13:52:18 -0500 | [diff] [blame] | 858 | } |
| 859 | free(options); |
Hashcode | 62bd9e0 | 2013-11-19 21:59:42 -0800 | [diff] [blame] | 860 | } |
| 861 | |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 862 | void TWPartition::Save_FS_Flags(const string& local_File_System, int local_Mount_Flags, const string& local_Mount_Options) { |
| 863 | partition_fs_flags_struct flags; |
| 864 | flags.File_System = local_File_System; |
| 865 | flags.Mount_Flags = local_Mount_Flags; |
| 866 | flags.Mount_Options = local_Mount_Options; |
| 867 | fs_flags.push_back(flags); |
| 868 | } |
| 869 | |
Matt Mower | 2416a50 | 2016-04-12 19:54:46 -0500 | [diff] [blame] | 870 | void TWPartition::Apply_TW_Flag(const unsigned flag, const char* str, const bool val) { |
| 871 | switch (flag) { |
| 872 | case TWFLAG_ANDSEC: |
| 873 | Has_Android_Secure = val; |
| 874 | break; |
| 875 | case TWFLAG_BACKUP: |
| 876 | Can_Be_Backed_Up = val; |
| 877 | break; |
| 878 | case TWFLAG_BACKUPNAME: |
| 879 | Backup_Display_Name = str; |
| 880 | break; |
| 881 | case TWFLAG_BLOCKSIZE: |
| 882 | Format_Block_Size = (unsigned long)(atol(str)); |
| 883 | break; |
| 884 | case TWFLAG_CANBEWIPED: |
| 885 | Can_Be_Wiped = val; |
| 886 | break; |
| 887 | case TWFLAG_CANENCRYPTBACKUP: |
| 888 | Can_Encrypt_Backup = val; |
| 889 | break; |
| 890 | case TWFLAG_DEFAULTS: |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 891 | case TWFLAG_WAIT: |
| 892 | case TWFLAG_VERIFY: |
| 893 | case TWFLAG_CHECK: |
| 894 | case TWFLAG_NOTRIM: |
| 895 | case TWFLAG_VOLDMANAGED: |
| 896 | case TWFLAG_RESIZE: |
Matt Mower | 2416a50 | 2016-04-12 19:54:46 -0500 | [diff] [blame] | 897 | // Do nothing |
| 898 | break; |
| 899 | case TWFLAG_DISPLAY: |
| 900 | Display_Name = str; |
| 901 | break; |
| 902 | case TWFLAG_ENCRYPTABLE: |
| 903 | case TWFLAG_FORCEENCRYPT: |
| 904 | Crypto_Key_Location = str; |
| 905 | break; |
Ethan Yonker | e9afc3d | 2018-08-30 15:16:27 -0500 | [diff] [blame] | 906 | case TWFLAG_FILEENCRYPTION: |
| 907 | // This flag isn't used by TWRP but is needed in 9.0 FBE decrypt |
| 908 | // fileencryption=ice:aes-256-heh |
| 909 | { |
| 910 | std::string FBE = str; |
Ethan Yonker | e9afc3d | 2018-08-30 15:16:27 -0500 | [diff] [blame] | 911 | size_t colon_loc = FBE.find(":"); |
| 912 | if (colon_loc == std::string::npos) { |
Ethan Yonker | 9338282 | 2018-11-01 15:25:31 -0500 | [diff] [blame] | 913 | property_set("fbe.contents", FBE.c_str()); |
| 914 | property_set("fbe.filenames", ""); |
| 915 | LOGINFO("FBE contents '%s', filenames ''\n", FBE.c_str()); |
Ethan Yonker | e9afc3d | 2018-08-30 15:16:27 -0500 | [diff] [blame] | 916 | break; |
| 917 | } |
Ethan Yonker | 9338282 | 2018-11-01 15:25:31 -0500 | [diff] [blame] | 918 | std::string FBE_contents, FBE_filenames; |
Ethan Yonker | e9afc3d | 2018-08-30 15:16:27 -0500 | [diff] [blame] | 919 | FBE_contents = FBE.substr(0, colon_loc); |
| 920 | FBE_filenames = FBE.substr(colon_loc + 1); |
| 921 | property_set("fbe.contents", FBE_contents.c_str()); |
| 922 | property_set("fbe.filenames", FBE_filenames.c_str()); |
| 923 | LOGINFO("FBE contents '%s', filenames '%s'\n", FBE_contents.c_str(), FBE_filenames.c_str()); |
| 924 | } |
| 925 | break; |
Peter Cai | 28ea7b0 | 2019-05-24 11:38:54 +0800 | [diff] [blame] | 926 | case TWFLAG_WRAPPEDKEY: |
| 927 | // Set fbe.data.wrappedkey to true |
| 928 | { |
| 929 | property_set("fbe.data.wrappedkey", "true"); |
| 930 | LOGINFO("FBE wrapped key enabled\n"); |
| 931 | } |
| 932 | break; |
Matt Mower | 2416a50 | 2016-04-12 19:54:46 -0500 | [diff] [blame] | 933 | case TWFLAG_FLASHIMG: |
| 934 | Can_Flash_Img = val; |
| 935 | break; |
| 936 | case TWFLAG_FSFLAGS: |
Matt Mower | 4ab42b1 | 2016-04-21 13:52:18 -0500 | [diff] [blame] | 937 | Process_FS_Flags(str); |
Matt Mower | 2416a50 | 2016-04-12 19:54:46 -0500 | [diff] [blame] | 938 | break; |
| 939 | case TWFLAG_IGNOREBLKID: |
| 940 | Ignore_Blkid = val; |
| 941 | break; |
| 942 | case TWFLAG_LENGTH: |
| 943 | Length = atoi(str); |
| 944 | break; |
| 945 | case TWFLAG_MOUNTTODECRYPT: |
| 946 | Mount_To_Decrypt = val; |
| 947 | break; |
| 948 | case TWFLAG_REMOVABLE: |
| 949 | Removable = val; |
| 950 | break; |
Matt Mower | 2416a50 | 2016-04-12 19:54:46 -0500 | [diff] [blame] | 951 | case TWFLAG_SETTINGSSTORAGE: |
| 952 | Is_Settings_Storage = val; |
| 953 | if (Is_Settings_Storage) |
| 954 | Is_Storage = true; |
| 955 | break; |
| 956 | case TWFLAG_STORAGE: |
| 957 | Is_Storage = val; |
| 958 | break; |
| 959 | case TWFLAG_STORAGENAME: |
| 960 | Storage_Name = str; |
| 961 | break; |
| 962 | case TWFLAG_SUBPARTITIONOF: |
| 963 | Is_SubPartition = true; |
| 964 | SubPartition_Of = str; |
| 965 | break; |
| 966 | case TWFLAG_SYMLINK: |
| 967 | Symlink_Path = str; |
| 968 | break; |
| 969 | case TWFLAG_USERDATAENCRYPTBACKUP: |
| 970 | Use_Userdata_Encryption = val; |
| 971 | if (Use_Userdata_Encryption) |
| 972 | Can_Encrypt_Backup = true; |
| 973 | break; |
| 974 | case TWFLAG_USERMRF: |
| 975 | Use_Rm_Rf = val; |
| 976 | break; |
| 977 | case TWFLAG_WIPEDURINGFACTORYRESET: |
| 978 | Wipe_During_Factory_Reset = val; |
| 979 | if (Wipe_During_Factory_Reset) { |
| 980 | Can_Be_Wiped = true; |
| 981 | Wipe_Available_in_GUI = true; |
| 982 | } |
| 983 | break; |
| 984 | case TWFLAG_WIPEINGUI: |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 985 | case TWFLAG_FORMATTABLE: |
Matt Mower | 2416a50 | 2016-04-12 19:54:46 -0500 | [diff] [blame] | 986 | Wipe_Available_in_GUI = val; |
| 987 | if (Wipe_Available_in_GUI) |
| 988 | Can_Be_Wiped = true; |
| 989 | break; |
Ethan Yonker | 1b19016 | 2016-12-05 15:25:19 -0600 | [diff] [blame] | 990 | case TWFLAG_SLOTSELECT: |
| 991 | SlotSelect = true; |
| 992 | break; |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 993 | case TWFLAG_ALTDEVICE: |
| 994 | Alternate_Block_Device = str; |
| 995 | break; |
Ethan Yonker | 9338282 | 2018-11-01 15:25:31 -0500 | [diff] [blame] | 996 | case TWFLAG_KEYDIRECTORY: |
| 997 | Key_Directory = str; |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 998 | break; |
Matt Mower | 2416a50 | 2016-04-12 19:54:46 -0500 | [diff] [blame] | 999 | default: |
| 1000 | // Should not get here |
| 1001 | LOGINFO("Flag identified for processing, but later unmatched: %i\n", flag); |
| 1002 | break; |
| 1003 | } |
| 1004 | } |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 1005 | |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 1006 | 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] | 1007 | char separator[2] = {'\n', 0}; |
| 1008 | char *ptr, *savep; |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 1009 | char source_separator = ';'; |
| 1010 | |
| 1011 | if (fstab_ver == 2) |
| 1012 | source_separator = ','; |
Matt Mower | 2416a50 | 2016-04-12 19:54:46 -0500 | [diff] [blame] | 1013 | |
| 1014 | // Semicolons within double-quotes are not forbidden, so replace |
| 1015 | // only the semicolons intended as separators with '\n' for strtok |
| 1016 | for (unsigned i = 0, skip = 0; i < strlen(flags); i++) { |
| 1017 | if (flags[i] == '\"') |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 1018 | skip = !skip; |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 1019 | if (!skip && flags[i] == source_separator) |
Matt Mower | 2416a50 | 2016-04-12 19:54:46 -0500 | [diff] [blame] | 1020 | flags[i] = separator[0]; |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 1021 | } |
| 1022 | |
Matt Mower | 2416a50 | 2016-04-12 19:54:46 -0500 | [diff] [blame] | 1023 | // Avoid issues with potentially nested strtok by using strtok_r |
| 1024 | ptr = strtok_r(flags, separator, &savep); |
| 1025 | while (ptr) { |
| 1026 | int ptr_len = strlen(ptr); |
| 1027 | const struct flag_list* tw_flag = tw_flags; |
Hashcode | 62bd9e0 | 2013-11-19 21:59:42 -0800 | [diff] [blame] | 1028 | |
Matt Mower | 2416a50 | 2016-04-12 19:54:46 -0500 | [diff] [blame] | 1029 | for (; tw_flag->name; tw_flag++) { |
| 1030 | int flag_len = strlen(tw_flag->name); |
| 1031 | |
| 1032 | if (strncmp(ptr, tw_flag->name, flag_len) == 0) { |
| 1033 | bool flag_val = false; |
| 1034 | |
| 1035 | if (ptr_len > flag_len && (tw_flag->name)[flag_len-1] != '=' |
| 1036 | && ptr[flag_len] != '=') { |
| 1037 | // Handle flags with same starting string |
| 1038 | // (e.g. backup and backupname) |
| 1039 | continue; |
| 1040 | } else if (ptr_len > flag_len && ptr[flag_len] == '=') { |
| 1041 | // Handle flags with dual format: Part 1 |
| 1042 | // (e.g. backup and backup=y. backup=y handled here) |
| 1043 | ptr += flag_len + 1; |
| 1044 | TWFunc::Strip_Quotes(ptr); |
| 1045 | // Skip flags with empty argument |
| 1046 | // (e.g. backup=) |
| 1047 | if (strlen(ptr) == 0) { |
| 1048 | LOGINFO("Flag missing argument or should not include '=': %s=\n", tw_flag->name); |
| 1049 | break; |
| 1050 | } |
| 1051 | flag_val = strchr("yY1", *ptr) != NULL; |
| 1052 | } else if (ptr_len == flag_len |
| 1053 | && (tw_flag->name)[flag_len-1] == '=') { |
| 1054 | // Skip flags missing argument after = |
| 1055 | // (e.g. backupname=) |
| 1056 | LOGINFO("Flag missing argument: %s\n", tw_flag->name); |
| 1057 | break; |
| 1058 | } else if (ptr_len > flag_len |
| 1059 | && (tw_flag->name)[flag_len-1] == '=') { |
| 1060 | // Handle arguments to flags |
| 1061 | // (e.g. backupname="My Stuff") |
| 1062 | ptr += flag_len; |
| 1063 | TWFunc::Strip_Quotes(ptr); |
| 1064 | // Skip flags with empty argument |
| 1065 | // (e.g. backupname="") |
| 1066 | if (strlen(ptr) == 0) { |
| 1067 | LOGINFO("Flag missing argument: %s\n", tw_flag->name); |
| 1068 | break; |
| 1069 | } |
| 1070 | } else if (ptr_len == flag_len) { |
| 1071 | // Handle flags with dual format: Part 2 |
| 1072 | // (e.g. backup and backup=y. backup handled here) |
| 1073 | flag_val = true; |
Ethan Yonker | 96af84a | 2015-01-05 14:58:36 -0600 | [diff] [blame] | 1074 | } else { |
Matt Mower | 2416a50 | 2016-04-12 19:54:46 -0500 | [diff] [blame] | 1075 | LOGINFO("Flag matched, but could not be processed: %s\n", ptr); |
| 1076 | break; |
Ethan Yonker | 96af84a | 2015-01-05 14:58:36 -0600 | [diff] [blame] | 1077 | } |
Matt Mower | 2416a50 | 2016-04-12 19:54:46 -0500 | [diff] [blame] | 1078 | |
| 1079 | Apply_TW_Flag(tw_flag->flag, ptr, flag_val); |
| 1080 | break; |
Ethan Yonker | 96af84a | 2015-01-05 14:58:36 -0600 | [diff] [blame] | 1081 | } |
Matt Mower | 2416a50 | 2016-04-12 19:54:46 -0500 | [diff] [blame] | 1082 | } |
| 1083 | if (tw_flag->name == 0) { |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 1084 | if (Display_Error) |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1085 | LOGERR("Unhandled flag: '%s'\n", ptr); |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 1086 | else |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1087 | LOGINFO("Unhandled flag: '%s'\n", ptr); |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 1088 | } |
Matt Mower | 2416a50 | 2016-04-12 19:54:46 -0500 | [diff] [blame] | 1089 | ptr = strtok_r(NULL, separator, &savep); |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 1090 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1091 | } |
| 1092 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1093 | bool TWPartition::Is_File_System(string File_System) { |
| 1094 | if (File_System == "ext2" || |
Dees_Troy | 63c8df7 | 2012-09-10 14:02:05 -0400 | [diff] [blame] | 1095 | File_System == "ext3" || |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1096 | File_System == "ext4" || |
| 1097 | File_System == "vfat" || |
| 1098 | File_System == "ntfs" || |
| 1099 | File_System == "yaffs2" || |
bigbiff bigbiff | 3e14652 | 2012-11-14 14:32:59 -0500 | [diff] [blame] | 1100 | File_System == "exfat" || |
Dees_Troy | e501704 | 2013-08-29 16:38:55 +0000 | [diff] [blame] | 1101 | File_System == "f2fs" || |
Erfan Abdi | b859e91 | 2019-02-05 18:18:46 +0200 | [diff] [blame] | 1102 | File_System == "squashfs" || |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1103 | File_System == "auto") |
| 1104 | return true; |
| 1105 | else |
| 1106 | return false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1107 | } |
| 1108 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1109 | bool TWPartition::Is_Image(string File_System) { |
Dees_Troy | 5fcd8f9 | 2012-10-16 12:22:05 -0400 | [diff] [blame] | 1110 | if (File_System == "emmc" || File_System == "mtd" || File_System == "bml") |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1111 | return true; |
| 1112 | else |
| 1113 | return false; |
| 1114 | } |
| 1115 | |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 1116 | bool TWPartition::Make_Dir(string Path, bool Display_Error) { |
Ethan Yonker | 93ac7a0 | 2016-11-07 22:05:58 -0600 | [diff] [blame] | 1117 | if (TWFunc::Get_D_Type_From_Stat(Path) != S_IFDIR) |
| 1118 | unlink(Path.c_str()); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 1119 | if (!TWFunc::Path_Exists(Path)) { |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 1120 | if (mkdir(Path.c_str(), 0777) == -1) { |
| 1121 | if (Display_Error) |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1122 | 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] | 1123 | else |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1124 | LOGINFO("Can not create '%s' folder.\n", Path.c_str()); |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 1125 | return false; |
| 1126 | } else { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1127 | LOGINFO("Created '%s' folder.\n", Path.c_str()); |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 1128 | return true; |
| 1129 | } |
| 1130 | } |
| 1131 | return true; |
| 1132 | } |
| 1133 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1134 | void TWPartition::Setup_File_System(bool Display_Error) { |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1135 | Can_Be_Mounted = true; |
| 1136 | Can_Be_Wiped = true; |
| 1137 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1138 | // Make the mount point folder if it doesn't exist |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 1139 | Make_Dir(Mount_Point, Display_Error); |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 1140 | Backup_Method = BM_FILES; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1141 | } |
| 1142 | |
Ethan Yonker | 1b19016 | 2016-12-05 15:25:19 -0600 | [diff] [blame] | 1143 | void TWPartition::Setup_Image() { |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1144 | Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1); |
| 1145 | Backup_Name = Display_Name; |
Gary Peck | 82599a8 | 2012-11-21 16:23:12 -0800 | [diff] [blame] | 1146 | if (Current_File_System == "emmc") |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 1147 | Backup_Method = BM_DD; |
Gary Peck | 82599a8 | 2012-11-21 16:23:12 -0800 | [diff] [blame] | 1148 | else if (Current_File_System == "mtd" || Current_File_System == "bml") |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 1149 | Backup_Method = BM_FLASH_UTILS; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1150 | else |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1151 | 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] | 1152 | } |
| 1153 | |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 1154 | void TWPartition::Setup_AndSec(void) { |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 1155 | Backup_Display_Name = "Android Secure"; |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 1156 | Backup_Name = "and-sec"; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 1157 | Can_Be_Backed_Up = true; |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 1158 | Has_Android_Secure = true; |
| 1159 | Symlink_Path = Mount_Point + "/.android_secure"; |
| 1160 | Symlink_Mount_Point = "/and-sec"; |
| 1161 | Backup_Path = Symlink_Mount_Point; |
| 1162 | Make_Dir("/and-sec", true); |
| 1163 | Recreate_AndSec_Folder(); |
Kjell Braden | 3126a11 | 2016-06-19 16:58:15 +0000 | [diff] [blame] | 1164 | Mount_Storage_Retry(true); |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 1165 | } |
| 1166 | |
that | 9e0593e | 2014-10-08 00:01:24 +0200 | [diff] [blame] | 1167 | void TWPartition::Setup_Data_Media() { |
Ethan Yonker | 6277c79 | 2014-09-15 14:54:30 -0500 | [diff] [blame] | 1168 | 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] | 1169 | if (Storage_Name.empty() || Storage_Name == "Data") |
| 1170 | Storage_Name = "Internal Storage"; |
Ethan Yonker | 6277c79 | 2014-09-15 14:54:30 -0500 | [diff] [blame] | 1171 | Has_Data_Media = true; |
| 1172 | Is_Storage = true; |
Ethan Yonker | 66a1949 | 2015-12-10 10:19:45 -0600 | [diff] [blame] | 1173 | Storage_Path = Mount_Point + "/media"; |
Ethan Yonker | 6277c79 | 2014-09-15 14:54:30 -0500 | [diff] [blame] | 1174 | Symlink_Path = Storage_Path; |
Ethan Yonker | 66a1949 | 2015-12-10 10:19:45 -0600 | [diff] [blame] | 1175 | if (Mount_Point == "/data") { |
| 1176 | Is_Settings_Storage = true; |
| 1177 | if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) { |
| 1178 | Make_Dir("/emmc", false); |
| 1179 | Symlink_Mount_Point = "/emmc"; |
| 1180 | } else { |
| 1181 | Make_Dir("/sdcard", false); |
| 1182 | Symlink_Mount_Point = "/sdcard"; |
| 1183 | } |
| 1184 | if (Mount(false) && TWFunc::Path_Exists(Mount_Point + "/media/0")) { |
| 1185 | Storage_Path = Mount_Point + "/media/0"; |
| 1186 | Symlink_Path = Storage_Path; |
| 1187 | DataManager::SetValue(TW_INTERNAL_PATH, Mount_Point + "/media/0"); |
| 1188 | UnMount(true); |
| 1189 | } |
| 1190 | DataManager::SetValue("tw_has_internal", 1); |
| 1191 | DataManager::SetValue("tw_has_data_media", 1); |
Ethan Yonker | 3fdcda4 | 2016-11-30 12:29:37 -0600 | [diff] [blame] | 1192 | backup_exclusions.add_absolute_dir("/data/data/com.google.android.music/files"); |
Ethan Yonker | 6355b56 | 2017-05-01 09:42:17 -0500 | [diff] [blame] | 1193 | wipe_exclusions.add_absolute_dir(Mount_Point + "/misc/vold"); // adopted storage keys |
Ethan Yonker | 79f88bd | 2016-12-09 14:52:12 -0600 | [diff] [blame] | 1194 | ExcludeAll(Mount_Point + "/system/storage.xml"); |
Ethan Yonker | 6277c79 | 2014-09-15 14:54:30 -0500 | [diff] [blame] | 1195 | } else { |
Ethan Yonker | 66a1949 | 2015-12-10 10:19:45 -0600 | [diff] [blame] | 1196 | if (Mount(true) && TWFunc::Path_Exists(Mount_Point + "/media/0")) { |
| 1197 | Storage_Path = Mount_Point + "/media/0"; |
| 1198 | Symlink_Path = Storage_Path; |
| 1199 | UnMount(true); |
| 1200 | } |
Ethan Yonker | 6277c79 | 2014-09-15 14:54:30 -0500 | [diff] [blame] | 1201 | } |
Ethan Yonker | 79f88bd | 2016-12-09 14:52:12 -0600 | [diff] [blame] | 1202 | ExcludeAll(Mount_Point + "/media"); |
Ethan Yonker | 6277c79 | 2014-09-15 14:54:30 -0500 | [diff] [blame] | 1203 | } |
| 1204 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1205 | void TWPartition::Find_Real_Block_Device(string& Block, bool Display_Error) { |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 1206 | char device[PATH_MAX], realDevice[PATH_MAX]; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1207 | |
| 1208 | strcpy(device, Block.c_str()); |
| 1209 | memset(realDevice, 0, sizeof(realDevice)); |
| 1210 | while (readlink(device, realDevice, sizeof(realDevice)) > 0) |
| 1211 | { |
| 1212 | strcpy(device, realDevice); |
| 1213 | memset(realDevice, 0, sizeof(realDevice)); |
| 1214 | } |
| 1215 | |
| 1216 | if (device[0] != '/') { |
| 1217 | if (Display_Error) |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1218 | 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] | 1219 | else |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1220 | 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] | 1221 | return; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1222 | } else { |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1223 | Block = device; |
| 1224 | return; |
| 1225 | } |
| 1226 | } |
| 1227 | |
Kjell Braden | 3126a11 | 2016-06-19 16:58:15 +0000 | [diff] [blame] | 1228 | bool TWPartition::Mount_Storage_Retry(bool Display_Error) { |
Dees_Troy | 8e337f3 | 2012-10-13 22:07:49 -0400 | [diff] [blame] | 1229 | // 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] | 1230 | if (!Mount(Display_Error)) { |
Dees_Troy | 8e337f3 | 2012-10-13 22:07:49 -0400 | [diff] [blame] | 1231 | int retry_count = 5; |
| 1232 | while (retry_count > 0 && !Mount(false)) { |
| 1233 | usleep(500000); |
| 1234 | retry_count--; |
| 1235 | } |
Kjell Braden | 3126a11 | 2016-06-19 16:58:15 +0000 | [diff] [blame] | 1236 | return Mount(Display_Error); |
Dees_Troy | 8e337f3 | 2012-10-13 22:07:49 -0400 | [diff] [blame] | 1237 | } |
Kjell Braden | 3126a11 | 2016-06-19 16:58:15 +0000 | [diff] [blame] | 1238 | return true; |
Dees_Troy | 8e337f3 | 2012-10-13 22:07:49 -0400 | [diff] [blame] | 1239 | } |
| 1240 | |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1241 | bool TWPartition::Find_MTD_Block_Device(string MTD_Name) { |
| 1242 | FILE *fp = NULL; |
| 1243 | char line[255]; |
| 1244 | |
| 1245 | fp = fopen("/proc/mtd", "rt"); |
| 1246 | if (fp == NULL) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1247 | LOGERR("Device does not support /proc/mtd\n"); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1248 | return false; |
| 1249 | } |
| 1250 | |
| 1251 | while (fgets(line, sizeof(line), fp) != NULL) |
| 1252 | { |
| 1253 | char device[32], label[32]; |
| 1254 | unsigned long size = 0; |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1255 | int deviceId; |
| 1256 | |
| 1257 | sscanf(line, "%s %lx %*s %*c%s", device, &size, label); |
| 1258 | |
| 1259 | // Skip header and blank lines |
| 1260 | if ((strcmp(device, "dev:") == 0) || (strlen(line) < 8)) |
| 1261 | continue; |
| 1262 | |
| 1263 | // Strip off the trailing " from the label |
| 1264 | label[strlen(label)-1] = '\0'; |
| 1265 | |
| 1266 | if (strcmp(label, MTD_Name.c_str()) == 0) { |
| 1267 | // We found our device |
| 1268 | // Strip off the trailing : from the device |
| 1269 | device[strlen(device)-1] = '\0'; |
| 1270 | if (sscanf(device,"mtd%d", &deviceId) == 1) { |
| 1271 | sprintf(device, "/dev/block/mtdblock%d", deviceId); |
| 1272 | Primary_Block_Device = device; |
Dees_Troy | 76543db | 2013-06-19 16:24:30 +0000 | [diff] [blame] | 1273 | fclose(fp); |
| 1274 | return true; |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1275 | } |
| 1276 | } |
| 1277 | } |
| 1278 | fclose(fp); |
| 1279 | |
| 1280 | return false; |
| 1281 | } |
| 1282 | |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 1283 | bool TWPartition::Get_Size_Via_statfs(bool Display_Error) { |
| 1284 | struct statfs st; |
| 1285 | string Local_Path = Mount_Point + "/."; |
| 1286 | |
| 1287 | if (!Mount(Display_Error)) |
| 1288 | return false; |
| 1289 | |
| 1290 | if (statfs(Local_Path.c_str(), &st) != 0) { |
| 1291 | if (!Removable) { |
| 1292 | if (Display_Error) |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1293 | LOGERR("Unable to statfs '%s'\n", Local_Path.c_str()); |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 1294 | else |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1295 | LOGINFO("Unable to statfs '%s'\n", Local_Path.c_str()); |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 1296 | } |
| 1297 | return false; |
| 1298 | } |
| 1299 | Size = (st.f_blocks * st.f_bsize); |
| 1300 | Used = ((st.f_blocks - st.f_bfree) * st.f_bsize); |
| 1301 | Free = (st.f_bfree * st.f_bsize); |
| 1302 | Backup_Size = Used; |
| 1303 | return true; |
| 1304 | } |
| 1305 | |
| 1306 | bool TWPartition::Get_Size_Via_df(bool Display_Error) { |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1307 | FILE* fp; |
| 1308 | char command[255], line[512]; |
| 1309 | int include_block = 1; |
| 1310 | unsigned int min_len; |
| 1311 | |
| 1312 | if (!Mount(Display_Error)) |
| 1313 | return false; |
| 1314 | |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1315 | min_len = Actual_Block_Device.size() + 2; |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 1316 | sprintf(command, "df %s > /tmp/dfoutput.txt", Mount_Point.c_str()); |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 1317 | TWFunc::Exec_Cmd(command); |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 1318 | fp = fopen("/tmp/dfoutput.txt", "rt"); |
| 1319 | if (fp == NULL) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1320 | LOGINFO("Unable to open /tmp/dfoutput.txt.\n"); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1321 | return false; |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 1322 | } |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1323 | |
| 1324 | while (fgets(line, sizeof(line), fp) != NULL) |
| 1325 | { |
| 1326 | unsigned long blocks, used, available; |
| 1327 | char device[64]; |
| 1328 | char tmpString[64]; |
| 1329 | |
| 1330 | if (strncmp(line, "Filesystem", 10) == 0) |
| 1331 | continue; |
| 1332 | if (strlen(line) < min_len) { |
| 1333 | include_block = 0; |
| 1334 | continue; |
| 1335 | } |
| 1336 | if (include_block) { |
| 1337 | sscanf(line, "%s %lu %lu %lu", device, &blocks, &used, &available); |
| 1338 | } else { |
| 1339 | // The device block string is so long that the df information is on the next line |
| 1340 | int space_count = 0; |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 1341 | sprintf(tmpString, "/dev/block/%s", Actual_Block_Device.c_str()); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1342 | while (tmpString[space_count] == 32) |
| 1343 | space_count++; |
| 1344 | sscanf(line + space_count, "%lu %lu %lu", &blocks, &used, &available); |
| 1345 | } |
| 1346 | |
| 1347 | // Adjust block size to byte size |
| 1348 | Size = blocks * 1024ULL; |
| 1349 | Used = used * 1024ULL; |
| 1350 | Free = available * 1024ULL; |
| 1351 | Backup_Size = Used; |
| 1352 | } |
| 1353 | fclose(fp); |
| 1354 | return true; |
| 1355 | } |
| 1356 | |
Ethan Yonker | d18a821 | 2015-12-14 10:17:00 -0600 | [diff] [blame] | 1357 | unsigned long long TWPartition::IOCTL_Get_Block_Size() { |
Ethan Yonker | d18a821 | 2015-12-14 10:17:00 -0600 | [diff] [blame] | 1358 | Find_Actual_Block_Device(); |
Ethan Yonker | 483e9f4 | 2016-01-11 22:21:18 -0600 | [diff] [blame] | 1359 | |
| 1360 | return TWFunc::IOCTL_Get_Block_Size(Actual_Block_Device.c_str()); |
Ethan Yonker | d18a821 | 2015-12-14 10:17:00 -0600 | [diff] [blame] | 1361 | } |
| 1362 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1363 | bool TWPartition::Find_Partition_Size(void) { |
| 1364 | FILE* fp; |
| 1365 | char line[512]; |
| 1366 | string tmpdevice; |
| 1367 | |
igoriok | 87e3d93 | 2013-01-31 21:03:53 +0200 | [diff] [blame] | 1368 | fp = fopen("/proc/dumchar_info", "rt"); |
| 1369 | if (fp != NULL) { |
| 1370 | while (fgets(line, sizeof(line), fp) != NULL) |
| 1371 | { |
| 1372 | char label[32], device[32]; |
| 1373 | unsigned long size = 0; |
| 1374 | |
that | d43bf2d | 2014-09-21 23:13:02 +0200 | [diff] [blame] | 1375 | sscanf(line, "%s %lx %*x %*u %s", label, &size, device); |
igoriok | 87e3d93 | 2013-01-31 21:03:53 +0200 | [diff] [blame] | 1376 | |
bigbiff bigbiff | 34684ff | 2013-12-01 21:03:45 -0500 | [diff] [blame] | 1377 | // Skip header, annotation and blank lines |
igoriok | 87e3d93 | 2013-01-31 21:03:53 +0200 | [diff] [blame] | 1378 | if ((strncmp(device, "/dev/", 5) != 0) || (strlen(line) < 8)) |
| 1379 | continue; |
| 1380 | |
| 1381 | tmpdevice = "/dev/"; |
| 1382 | tmpdevice += label; |
| 1383 | if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) { |
| 1384 | Size = size; |
| 1385 | fclose(fp); |
| 1386 | return true; |
| 1387 | } |
| 1388 | } |
| 1389 | } |
| 1390 | |
Ethan Yonker | d18a821 | 2015-12-14 10:17:00 -0600 | [diff] [blame] | 1391 | unsigned long long ioctl_size = IOCTL_Get_Block_Size(); |
| 1392 | if (ioctl_size) { |
| 1393 | Size = ioctl_size; |
| 1394 | return true; |
| 1395 | } |
| 1396 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1397 | // In this case, we'll first get the partitions we care about (with labels) |
| 1398 | fp = fopen("/proc/partitions", "rt"); |
| 1399 | if (fp == NULL) |
| 1400 | return false; |
| 1401 | |
| 1402 | while (fgets(line, sizeof(line), fp) != NULL) |
| 1403 | { |
| 1404 | unsigned long major, minor, blocks; |
| 1405 | char device[512]; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1406 | |
Dees_Troy | 63c8df7 | 2012-09-10 14:02:05 -0400 | [diff] [blame] | 1407 | if (strlen(line) < 7 || line[0] == 'm') continue; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1408 | sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device); |
| 1409 | |
| 1410 | tmpdevice = "/dev/block/"; |
| 1411 | tmpdevice += device; |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1412 | if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) { |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1413 | // Adjust block size to byte size |
| 1414 | Size = blocks * 1024ULL; |
| 1415 | fclose(fp); |
| 1416 | return true; |
| 1417 | } |
| 1418 | } |
| 1419 | fclose(fp); |
| 1420 | return false; |
| 1421 | } |
| 1422 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1423 | bool TWPartition::Is_Mounted(void) { |
| 1424 | if (!Can_Be_Mounted) |
| 1425 | return false; |
| 1426 | |
| 1427 | struct stat st1, st2; |
| 1428 | string test_path; |
| 1429 | |
| 1430 | // Check to see if the mount point directory exists |
| 1431 | test_path = Mount_Point + "/."; |
| 1432 | if (stat(test_path.c_str(), &st1) != 0) return false; |
| 1433 | |
| 1434 | // Check to see if the directory above the mount point exists |
| 1435 | test_path = Mount_Point + "/../."; |
| 1436 | if (stat(test_path.c_str(), &st2) != 0) return false; |
| 1437 | |
| 1438 | // Compare the device IDs -- if they match then we're (probably) using tmpfs instead of an actual device |
| 1439 | int ret = (st1.st_dev != st2.st_dev) ? true : false; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1440 | return ret; |
| 1441 | } |
| 1442 | |
James Christopher Adduono | d6f94ac | 2016-02-29 04:26:04 -0500 | [diff] [blame] | 1443 | bool TWPartition::Is_File_System_Writable(void) { |
| 1444 | if (!Is_File_System(Current_File_System) || !Is_Mounted()) |
| 1445 | return false; |
| 1446 | |
| 1447 | string test_path = Mount_Point + "/."; |
| 1448 | return (access(test_path.c_str(), W_OK) == 0); |
| 1449 | } |
| 1450 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1451 | bool TWPartition::Mount(bool Display_Error) { |
Dees_Troy | 3f5c4e8 | 2013-02-01 15:16:59 +0000 | [diff] [blame] | 1452 | int exfat_mounted = 0; |
Martin Dünkelmann | 6d84378 | 2018-12-27 23:40:28 +0100 | [diff] [blame] | 1453 | unsigned int flags = Mount_Flags; |
Dees_Troy | 3f5c4e8 | 2013-02-01 15:16:59 +0000 | [diff] [blame] | 1454 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1455 | if (Is_Mounted()) { |
| 1456 | return true; |
| 1457 | } else if (!Can_Be_Mounted) { |
| 1458 | return false; |
| 1459 | } |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1460 | |
| 1461 | Find_Actual_Block_Device(); |
| 1462 | |
| 1463 | // Check the current file system before mounting |
| 1464 | Check_FS_Type(); |
Dees_Troy | 3f5c4e8 | 2013-02-01 15:16:59 +0000 | [diff] [blame] | 1465 | if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/exfat-fuse")) { |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 1466 | string cmd = "/sbin/exfat-fuse -o big_writes,max_read=131072,max_write=131072 " + Actual_Block_Device + " " + Mount_Point; |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1467 | LOGINFO("cmd: %s\n", cmd.c_str()); |
Dees_Troy | 3f5c4e8 | 2013-02-01 15:16:59 +0000 | [diff] [blame] | 1468 | string result; |
| 1469 | if (TWFunc::Exec_Cmd(cmd, result) != 0) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1470 | 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] | 1471 | Current_File_System = "vfat"; |
| 1472 | } else { |
| 1473 | #ifdef TW_NO_EXFAT_FUSE |
| 1474 | UnMount(false); |
| 1475 | // We'll let the kernel handle it but using exfat-fuse to detect if the file system is actually exfat |
| 1476 | // Some kernels let us mount vfat as exfat which doesn't work out too well |
| 1477 | #else |
| 1478 | exfat_mounted = 1; |
| 1479 | #endif |
| 1480 | } |
| 1481 | } |
Ethan Yonker | eb32b1f | 2015-05-18 10:23:03 -0500 | [diff] [blame] | 1482 | |
Greg Wallace | b39e6c6 | 2015-12-29 00:55:26 -0500 | [diff] [blame] | 1483 | if (Current_File_System == "ntfs" && (TWFunc::Path_Exists("/sbin/ntfs-3g") || TWFunc::Path_Exists("/sbin/mount.ntfs"))) { |
Ethan Yonker | b81d905 | 2015-07-09 13:20:53 -0500 | [diff] [blame] | 1484 | string cmd; |
Greg Wallace | b39e6c6 | 2015-12-29 00:55:26 -0500 | [diff] [blame] | 1485 | string Ntfsmount_Binary = ""; |
| 1486 | |
| 1487 | if (TWFunc::Path_Exists("/sbin/ntfs-3g")) |
| 1488 | Ntfsmount_Binary = "ntfs-3g"; |
| 1489 | else if (TWFunc::Path_Exists("/sbin/mount.ntfs")) |
| 1490 | Ntfsmount_Binary = "mount.ntfs"; |
| 1491 | |
Ethan Yonker | b81d905 | 2015-07-09 13:20:53 -0500 | [diff] [blame] | 1492 | if (Mount_Read_Only) |
Greg Wallace | b39e6c6 | 2015-12-29 00:55:26 -0500 | [diff] [blame] | 1493 | cmd = "/sbin/" + Ntfsmount_Binary + " -o ro " + Actual_Block_Device + " " + Mount_Point; |
Ethan Yonker | b81d905 | 2015-07-09 13:20:53 -0500 | [diff] [blame] | 1494 | else |
Greg Wallace | b39e6c6 | 2015-12-29 00:55:26 -0500 | [diff] [blame] | 1495 | cmd = "/sbin/" + Ntfsmount_Binary + " " + Actual_Block_Device + " " + Mount_Point; |
Ethan Yonker | b81d905 | 2015-07-09 13:20:53 -0500 | [diff] [blame] | 1496 | LOGINFO("cmd: '%s'\n", cmd.c_str()); |
Greg Wallace | b39e6c6 | 2015-12-29 00:55:26 -0500 | [diff] [blame] | 1497 | |
Ethan Yonker | b81d905 | 2015-07-09 13:20:53 -0500 | [diff] [blame] | 1498 | if (TWFunc::Exec_Cmd(cmd) == 0) { |
| 1499 | return true; |
| 1500 | } else { |
| 1501 | LOGINFO("ntfs-3g failed to mount, trying regular mount method.\n"); |
| 1502 | } |
| 1503 | } |
| 1504 | |
Ethan Yonker | eb32b1f | 2015-05-18 10:23:03 -0500 | [diff] [blame] | 1505 | if (Mount_Read_Only) |
| 1506 | flags |= MS_RDONLY; |
| 1507 | |
Dees_Troy | 2204203 | 2012-12-18 21:23:08 +0000 | [diff] [blame] | 1508 | if (Fstab_File_System == "yaffs2") { |
| 1509 | // mount an MTD partition as a YAFFS2 filesystem. |
Ethan Yonker | eb32b1f | 2015-05-18 10:23:03 -0500 | [diff] [blame] | 1510 | flags = MS_NOATIME | MS_NODEV | MS_NODIRATIME; |
| 1511 | if (Mount_Read_Only) |
| 1512 | flags |= MS_RDONLY; |
Dees_Troy | 76543db | 2013-06-19 16:24:30 +0000 | [diff] [blame] | 1513 | if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags, NULL) < 0) { |
| 1514 | if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags | MS_RDONLY, NULL) < 0) { |
| 1515 | if (Display_Error) |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1516 | 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] | 1517 | else |
| 1518 | LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str()); |
| 1519 | return false; |
| 1520 | } else { |
| 1521 | LOGINFO("Mounted '%s' (MTD) as RO\n", Mount_Point.c_str()); |
| 1522 | return true; |
| 1523 | } |
| 1524 | } else { |
| 1525 | struct stat st; |
| 1526 | string test_path = Mount_Point; |
| 1527 | if (stat(test_path.c_str(), &st) < 0) { |
| 1528 | if (Display_Error) |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1529 | 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] | 1530 | else |
| 1531 | LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str()); |
| 1532 | return false; |
| 1533 | } |
| 1534 | mode_t new_mode = st.st_mode | S_IXUSR | S_IXGRP | S_IXOTH; |
| 1535 | if (new_mode != st.st_mode) { |
| 1536 | LOGINFO("Fixing execute permissions for %s\n", Mount_Point.c_str()); |
| 1537 | if (chmod(Mount_Point.c_str(), new_mode) < 0) { |
| 1538 | if (Display_Error) |
| 1539 | LOGERR("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno)); |
| 1540 | else |
| 1541 | LOGINFO("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno)); |
| 1542 | return false; |
| 1543 | } |
| 1544 | } |
Dees_Troy | 2204203 | 2012-12-18 21:23:08 +0000 | [diff] [blame] | 1545 | return true; |
Dees_Troy | 76543db | 2013-06-19 16:24:30 +0000 | [diff] [blame] | 1546 | } |
that | c7572eb | 2015-03-31 18:55:30 +0200 | [diff] [blame] | 1547 | } |
| 1548 | |
| 1549 | string mount_fs = Current_File_System; |
| 1550 | if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sys/module/texfat")) |
| 1551 | mount_fs = "texfat"; |
| 1552 | |
| 1553 | if (!exfat_mounted && |
Ethan Yonker | eb32b1f | 2015-05-18 10:23:03 -0500 | [diff] [blame] | 1554 | mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), mount_fs.c_str(), flags, Mount_Options.c_str()) != 0 && |
| 1555 | 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] | 1556 | #ifdef TW_NO_EXFAT_FUSE |
| 1557 | if (Current_File_System == "exfat") { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1558 | LOGINFO("Mounting exfat failed, trying vfat...\n"); |
Dees_Troy | 3f5c4e8 | 2013-02-01 15:16:59 +0000 | [diff] [blame] | 1559 | 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] | 1560 | if (Display_Error) |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1561 | 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] | 1562 | else |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1563 | LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str()); |
Ethan Yonker | eb32b1f | 2015-05-18 10:23:03 -0500 | [diff] [blame] | 1564 | 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] | 1565 | return false; |
Dees_Troy | 85f44ed | 2013-01-09 18:42:36 +0000 | [diff] [blame] | 1566 | } |
Dees_Troy | b05ddee | 2013-01-28 20:24:50 +0000 | [diff] [blame] | 1567 | } else { |
Dees_Troy | 3f5c4e8 | 2013-02-01 15:16:59 +0000 | [diff] [blame] | 1568 | #endif |
bigbiff bigbiff | 26774a0 | 2014-03-29 18:22:00 -0400 | [diff] [blame] | 1569 | if (!Removable && Display_Error) |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1570 | 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] | 1571 | else |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1572 | LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str()); |
| 1573 | 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] | 1574 | return false; |
| 1575 | #ifdef TW_NO_EXFAT_FUSE |
Dees_Troy | 85f44ed | 2013-01-09 18:42:36 +0000 | [diff] [blame] | 1576 | } |
| 1577 | #endif |
Dees_Troy | 3f5c4e8 | 2013-02-01 15:16:59 +0000 | [diff] [blame] | 1578 | } |
Ethan Yonker | 253368a | 2014-11-25 15:00:52 -0600 | [diff] [blame] | 1579 | |
Dees_Troy | 3f5c4e8 | 2013-02-01 15:16:59 +0000 | [diff] [blame] | 1580 | if (Removable) |
| 1581 | Update_Size(Display_Error); |
| 1582 | |
xiaolu | 9416f4f | 2015-06-04 08:22:23 +0800 | [diff] [blame] | 1583 | if (!Symlink_Mount_Point.empty() && TWFunc::Path_Exists(Symlink_Path)) { |
codelover | 2a3d4ce | 2015-03-14 20:26:49 +0800 | [diff] [blame] | 1584 | string Command = "mount -o bind '" + Symlink_Path + "' '" + Symlink_Mount_Point + "'"; |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 1585 | TWFunc::Exec_Cmd(Command); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1586 | } |
Chaosmaster | da97480 | 2020-01-26 21:09:28 +0100 | [diff] [blame] | 1587 | |
bigbiff | 32cbabe | 2020-04-12 19:16:19 -0400 | [diff] [blame] | 1588 | #ifndef TW_NO_BIND_SYSTEM |
Chaosmaster | da97480 | 2020-01-26 21:09:28 +0100 | [diff] [blame] | 1589 | if (Mount_Point == "/system_root") { |
| 1590 | unlink("/system"); |
| 1591 | mkdir("/system", 0755); |
| 1592 | mount("/system_root/system", "/system", "auto", MS_BIND, NULL); |
| 1593 | } |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame^] | 1594 | #endif |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1595 | return true; |
| 1596 | } |
| 1597 | |
| 1598 | bool TWPartition::UnMount(bool Display_Error) { |
Chaosmaster | da97480 | 2020-01-26 21:09:28 +0100 | [diff] [blame] | 1599 | if (Mount_Point == "/system_root") { |
| 1600 | if (umount("/system") == -1) |
| 1601 | umount2("/system", MNT_DETACH); |
| 1602 | rmdir("/system"); |
| 1603 | symlink("/system_root/system", "/system"); |
| 1604 | } |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1605 | if (Is_Mounted()) { |
| 1606 | int never_unmount_system; |
| 1607 | |
| 1608 | DataManager::GetValue(TW_DONT_UNMOUNT_SYSTEM, never_unmount_system); |
Captain Throwback | 9d6feb5 | 2018-07-27 10:05:24 -0400 | [diff] [blame] | 1609 | if (never_unmount_system == 1 && Mount_Point == PartitionManager.Get_Android_Root_Path()) |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1610 | return true; // Never unmount system if you're not supposed to unmount it |
| 1611 | |
Matt Mower | a8e6d83 | 2017-02-14 20:20:59 -0600 | [diff] [blame] | 1612 | if (Is_Storage && MTP_Storage_ID > 0) |
Ethan Yonker | 726a020 | 2014-12-16 20:01:38 -0600 | [diff] [blame] | 1613 | PartitionManager.Remove_MTP_Storage(MTP_Storage_ID); |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 1614 | |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1615 | if (!Symlink_Mount_Point.empty()) |
| 1616 | umount(Symlink_Mount_Point.c_str()); |
| 1617 | |
Dees_Troy | b05ddee | 2013-01-28 20:24:50 +0000 | [diff] [blame] | 1618 | umount(Mount_Point.c_str()); |
| 1619 | if (Is_Mounted()) { |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1620 | if (Display_Error) |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1621 | 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] | 1622 | else |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1623 | LOGINFO("Unable to unmount '%s'\n", Mount_Point.c_str()); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1624 | return false; |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 1625 | } else { |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1626 | return true; |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 1627 | } |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1628 | } else { |
| 1629 | return true; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1630 | } |
| 1631 | } |
| 1632 | |
James Christopher Adduono | d6f94ac | 2016-02-29 04:26:04 -0500 | [diff] [blame] | 1633 | bool TWPartition::ReMount(bool Display_Error) { |
| 1634 | if (UnMount(Display_Error)) |
| 1635 | return Mount(Display_Error); |
| 1636 | return false; |
| 1637 | } |
| 1638 | |
| 1639 | bool TWPartition::ReMount_RW(bool Display_Error) { |
| 1640 | // No need to remount if already mounted rw |
| 1641 | if (Is_File_System_Writable()) |
| 1642 | return true; |
| 1643 | |
| 1644 | bool ro = Mount_Read_Only; |
| 1645 | int flags = Mount_Flags; |
| 1646 | |
| 1647 | Mount_Read_Only = false; |
| 1648 | Mount_Flags &= ~MS_RDONLY; |
| 1649 | |
| 1650 | bool ret = ReMount(Display_Error); |
| 1651 | |
| 1652 | Mount_Read_Only = ro; |
| 1653 | Mount_Flags = flags; |
| 1654 | |
| 1655 | return ret; |
| 1656 | } |
| 1657 | |
Gary Peck | 43acadf | 2012-11-21 21:19:01 -0800 | [diff] [blame] | 1658 | bool TWPartition::Wipe(string New_File_System) { |
Ethan Yonker | 726a020 | 2014-12-16 20:01:38 -0600 | [diff] [blame] | 1659 | bool wiped = false, update_crypt = false, recreate_media = true; |
Dees_Troy | 16c2b31 | 2013-01-15 16:51:18 +0000 | [diff] [blame] | 1660 | int check; |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame^] | 1661 | fscrypt_encryption_policy policy; |
Dees_Troy | 16c2b31 | 2013-01-15 16:51:18 +0000 | [diff] [blame] | 1662 | |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1663 | if (!Can_Be_Wiped) { |
Matt Mower | 3c36697 | 2015-12-25 19:28:31 -0600 | [diff] [blame] | 1664 | 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] | 1665 | return false; |
| 1666 | } |
| 1667 | |
Dees_Troy | c51f1f9 | 2012-09-20 15:32:13 -0400 | [diff] [blame] | 1668 | if (Mount_Point == "/cache") |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1669 | Log_Offset = 0; |
Dees_Troy | c51f1f9 | 2012-09-20 15:32:13 -0400 | [diff] [blame] | 1670 | |
Ethan Yonker | 87c7bac | 2014-05-25 21:41:08 -0500 | [diff] [blame] | 1671 | if (Has_Data_Media && Current_File_System == New_File_System) { |
Dees_Troy | 16c2b31 | 2013-01-15 16:51:18 +0000 | [diff] [blame] | 1672 | wiped = Wipe_Data_Without_Wiping_Media(); |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame^] | 1673 | if (Mount_Point == "/data" && TWFunc::get_cache_dir() == AB_CACHE_DIR && !TWFunc::Is_Mount_Wiped("/data")) { |
| 1674 | bool created = Recreate_AB_Cache_Dir(policy); |
| 1675 | if (created) |
| 1676 | gui_msg(Msg(msg::kWarning, "fbe_wipe_msg=WARNING: {1} wiped. FBE device should be booted into Android and not Recovery to set initial FBE policy after wipe.")(TWFunc::get_cache_dir())); |
| 1677 | } |
Ethan Yonker | 5eac222 | 2014-06-11 12:22:55 -0500 | [diff] [blame] | 1678 | recreate_media = false; |
Dees_Troy | 16c2b31 | 2013-01-15 16:51:18 +0000 | [diff] [blame] | 1679 | } else { |
Dees_Troy | 16c2b31 | 2013-01-15 16:51:18 +0000 | [diff] [blame] | 1680 | DataManager::GetValue(TW_RM_RF_VAR, check); |
| 1681 | |
Hashcode | dabfd49 | 2013-08-29 22:45:30 -0700 | [diff] [blame] | 1682 | if (check || Use_Rm_Rf) |
Dees_Troy | 16c2b31 | 2013-01-15 16:51:18 +0000 | [diff] [blame] | 1683 | wiped = Wipe_RMRF(); |
| 1684 | else if (New_File_System == "ext4") |
| 1685 | wiped = Wipe_EXT4(); |
| 1686 | else if (New_File_System == "ext2" || New_File_System == "ext3") |
dianlujitao | 4879b37 | 2018-12-03 18:45:47 +0800 | [diff] [blame] | 1687 | wiped = Wipe_EXTFS(New_File_System); |
Dees_Troy | 16c2b31 | 2013-01-15 16:51:18 +0000 | [diff] [blame] | 1688 | else if (New_File_System == "vfat") |
| 1689 | wiped = Wipe_FAT(); |
| 1690 | else if (New_File_System == "exfat") |
| 1691 | wiped = Wipe_EXFAT(); |
| 1692 | else if (New_File_System == "yaffs2") |
| 1693 | wiped = Wipe_MTD(); |
Dees_Troy | e501704 | 2013-08-29 16:38:55 +0000 | [diff] [blame] | 1694 | else if (New_File_System == "f2fs") |
| 1695 | wiped = Wipe_F2FS(); |
Ethan Yonker | b81d905 | 2015-07-09 13:20:53 -0500 | [diff] [blame] | 1696 | else if (New_File_System == "ntfs") |
| 1697 | wiped = Wipe_NTFS(); |
Dees_Troy | 16c2b31 | 2013-01-15 16:51:18 +0000 | [diff] [blame] | 1698 | else { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1699 | 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] | 1700 | return false; |
| 1701 | } |
| 1702 | update_crypt = wiped; |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame^] | 1703 | update_crypt = false; |
Gary Peck | e8bc5d7 | 2012-12-21 06:45:25 -0800 | [diff] [blame] | 1704 | } |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1705 | |
Gary Peck | e8bc5d7 | 2012-12-21 06:45:25 -0800 | [diff] [blame] | 1706 | if (wiped) { |
Dees_Troy | 16c2b31 | 2013-01-15 16:51:18 +0000 | [diff] [blame] | 1707 | if (update_crypt) { |
| 1708 | Setup_File_System(false); |
| 1709 | if (Is_Encrypted && !Is_Decrypted) { |
| 1710 | // just wiped an encrypted partition back to its unencrypted state |
| 1711 | Is_Encrypted = false; |
| 1712 | Is_Decrypted = false; |
| 1713 | Decrypted_Block_Device = ""; |
| 1714 | if (Mount_Point == "/data") { |
| 1715 | DataManager::SetValue(TW_IS_ENCRYPTED, 0); |
| 1716 | DataManager::SetValue(TW_IS_DECRYPTED, 0); |
| 1717 | } |
Gary Peck | e8bc5d7 | 2012-12-21 06:45:25 -0800 | [diff] [blame] | 1718 | } |
| 1719 | } |
Ethan Yonker | 5eac222 | 2014-06-11 12:22:55 -0500 | [diff] [blame] | 1720 | |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame^] | 1721 | // if (Is_Storage && Mount(false)) |
| 1722 | // PartitionManager.Add_MTP_Storage(MTP_Storage_ID); |
Gary Peck | e8bc5d7 | 2012-12-21 06:45:25 -0800 | [diff] [blame] | 1723 | } |
Matt Mower | 209c963 | 2016-01-20 12:08:35 -0600 | [diff] [blame] | 1724 | |
Gary Peck | e8bc5d7 | 2012-12-21 06:45:25 -0800 | [diff] [blame] | 1725 | return wiped; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1726 | } |
| 1727 | |
Gary Peck | 43acadf | 2012-11-21 21:19:01 -0800 | [diff] [blame] | 1728 | bool TWPartition::Wipe() { |
Gary Peck | 82599a8 | 2012-11-21 16:23:12 -0800 | [diff] [blame] | 1729 | if (Is_File_System(Current_File_System)) |
| 1730 | return Wipe(Current_File_System); |
| 1731 | else |
| 1732 | return Wipe(Fstab_File_System); |
Gary Peck | 43acadf | 2012-11-21 21:19:01 -0800 | [diff] [blame] | 1733 | } |
| 1734 | |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 1735 | bool TWPartition::Wipe_AndSec(void) { |
| 1736 | if (!Has_Android_Secure) |
| 1737 | return false; |
| 1738 | |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 1739 | if (!Mount(true)) |
| 1740 | return false; |
| 1741 | |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1742 | gui_msg(Msg("wiping=Wiping {1}")(Backup_Display_Name)); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1743 | TWFunc::removeDir(Mount_Point + "/.android_secure/", true); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1744 | return true; |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 1745 | } |
| 1746 | |
Ethan Yonker | 87c7bac | 2014-05-25 21:41:08 -0500 | [diff] [blame] | 1747 | bool TWPartition::Can_Repair() { |
Ethan Yonker | a271915 | 2015-05-28 09:44:41 -0500 | [diff] [blame] | 1748 | if (Mount_Read_Only) |
| 1749 | return false; |
Matt Mower | |