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