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