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