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