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