Dees Troy | 3be70a8 | 2013-10-22 14:25:12 +0000 | [diff] [blame] | 1 | /* |
Ethan Yonker | 472f506 | 2016-02-25 13:47:30 -0600 | [diff] [blame] | 2 | Copyright 2013 to 2016 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 | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 44 | #include "twrpDigest.hpp" |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 45 | #include "twrpTar.hpp" |
Ethan Yonker | 3fdcda4 | 2016-11-30 12:29:37 -0600 | [diff] [blame] | 46 | #include "exclude.hpp" |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 47 | #include "infomanager.hpp" |
Ethan Yonker | 4b94cfd | 2014-12-11 10:00:45 -0600 | [diff] [blame] | 48 | #include "set_metadata.h" |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 49 | #include "gui/gui.hpp" |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 50 | #include "adbbu/libtwadbbu.hpp" |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 51 | extern "C" { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 52 | #include "mtdutils/mtdutils.h" |
| 53 | #include "mtdutils/mounts.h" |
Dees_Troy | a95f55c | 2013-08-17 13:14:43 +0000 | [diff] [blame] | 54 | #ifdef USE_EXT4 |
| 55 | #include "make_ext4fs.h" |
| 56 | #endif |
Ethan Yonker | 71413f4 | 2014-02-26 13:36:08 -0600 | [diff] [blame] | 57 | |
| 58 | #ifdef TW_INCLUDE_CRYPTO |
Ethan Yonker | 253368a | 2014-11-25 15:00:52 -0600 | [diff] [blame] | 59 | #include "crypto/lollipop/cryptfs.h" |
Ethan Yonker | 66a1949 | 2015-12-10 10:19:45 -0600 | [diff] [blame] | 60 | #include "gpt/gpt.h" |
Ethan Yonker | bd7492d | 2016-12-07 13:55:01 -0600 | [diff] [blame] | 61 | #ifdef TW_INCLUDE_FBE |
| 62 | #include "crypto/ext4crypt/Decrypt.h" |
Ethan Yonker | bd7492d | 2016-12-07 13:55:01 -0600 | [diff] [blame] | 63 | #endif |
Ethan Yonker | bc85b63 | 2015-08-09 12:48:14 -0500 | [diff] [blame] | 64 | #else |
| 65 | #define CRYPT_FOOTER_OFFSET 0x4000 |
Ethan Yonker | 71413f4 | 2014-02-26 13:36:08 -0600 | [diff] [blame] | 66 | #endif |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 67 | } |
bigbiff bigbiff | c49d706 | 2013-10-11 20:28:00 -0400 | [diff] [blame] | 68 | #ifdef HAVE_SELINUX |
| 69 | #include "selinux/selinux.h" |
Ethan Yonker | f27497f | 2014-02-09 11:48:33 -0600 | [diff] [blame] | 70 | #include <selinux/label.h> |
bigbiff bigbiff | c49d706 | 2013-10-11 20:28:00 -0400 | [diff] [blame] | 71 | #endif |
Dees Troy | 4159aed | 2014-02-28 17:24:43 +0000 | [diff] [blame] | 72 | #ifdef HAVE_CAPABILITIES |
| 73 | #include <sys/capability.h> |
| 74 | #include <sys/xattr.h> |
| 75 | #include <linux/xattr.h> |
| 76 | #endif |
HashBang | ed974bb | 2016-01-30 14:20:09 -0500 | [diff] [blame] | 77 | #include <sparse_format.h> |
Ethan Yonker | 472f506 | 2016-02-25 13:47:30 -0600 | [diff] [blame] | 78 | #include "progresstracking.hpp" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 79 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 80 | using namespace std; |
| 81 | |
Dees_Troy | a95f55c | 2013-08-17 13:14:43 +0000 | [diff] [blame] | 82 | extern struct selabel_handle *selinux_handle; |
Ethan Yonker | 6277c79 | 2014-09-15 14:54:30 -0500 | [diff] [blame] | 83 | extern bool datamedia; |
Dees_Troy | a95f55c | 2013-08-17 13:14:43 +0000 | [diff] [blame] | 84 | |
Hashcode | 62bd9e0 | 2013-11-19 21:59:42 -0800 | [diff] [blame] | 85 | struct flag_list { |
| 86 | const char *name; |
| 87 | unsigned flag; |
| 88 | }; |
| 89 | |
Matt Mower | 4ab42b1 | 2016-04-21 13:52:18 -0500 | [diff] [blame] | 90 | const struct flag_list mount_flags[] = { |
Hashcode | 62bd9e0 | 2013-11-19 21:59:42 -0800 | [diff] [blame] | 91 | { "noatime", MS_NOATIME }, |
| 92 | { "noexec", MS_NOEXEC }, |
| 93 | { "nosuid", MS_NOSUID }, |
| 94 | { "nodev", MS_NODEV }, |
| 95 | { "nodiratime", MS_NODIRATIME }, |
| 96 | { "ro", MS_RDONLY }, |
| 97 | { "rw", 0 }, |
| 98 | { "remount", MS_REMOUNT }, |
| 99 | { "bind", MS_BIND }, |
| 100 | { "rec", MS_REC }, |
Dees Troy | c4bc30e | 2014-02-03 15:04:19 +0000 | [diff] [blame] | 101 | #ifdef MS_UNBINDABLE |
Hashcode | 62bd9e0 | 2013-11-19 21:59:42 -0800 | [diff] [blame] | 102 | { "unbindable", MS_UNBINDABLE }, |
Dees Troy | c4bc30e | 2014-02-03 15:04:19 +0000 | [diff] [blame] | 103 | #endif |
| 104 | #ifdef MS_PRIVATE |
Hashcode | 62bd9e0 | 2013-11-19 21:59:42 -0800 | [diff] [blame] | 105 | { "private", MS_PRIVATE }, |
Dees Troy | c4bc30e | 2014-02-03 15:04:19 +0000 | [diff] [blame] | 106 | #endif |
| 107 | #ifdef MS_SLAVE |
Hashcode | 62bd9e0 | 2013-11-19 21:59:42 -0800 | [diff] [blame] | 108 | { "slave", MS_SLAVE }, |
Dees Troy | c4bc30e | 2014-02-03 15:04:19 +0000 | [diff] [blame] | 109 | #endif |
| 110 | #ifdef MS_SHARED |
Hashcode | 62bd9e0 | 2013-11-19 21:59:42 -0800 | [diff] [blame] | 111 | { "shared", MS_SHARED }, |
Dees Troy | c4bc30e | 2014-02-03 15:04:19 +0000 | [diff] [blame] | 112 | #endif |
Hashcode | 62bd9e0 | 2013-11-19 21:59:42 -0800 | [diff] [blame] | 113 | { "sync", MS_SYNCHRONOUS }, |
| 114 | { "defaults", 0 }, |
| 115 | { 0, 0 }, |
| 116 | }; |
| 117 | |
Matt Mower | 2416a50 | 2016-04-12 19:54:46 -0500 | [diff] [blame] | 118 | enum TW_FSTAB_FLAGS { |
| 119 | TWFLAG_DEFAULTS, // Retain position |
| 120 | TWFLAG_ANDSEC, |
| 121 | TWFLAG_BACKUP, |
| 122 | TWFLAG_BACKUPNAME, |
| 123 | TWFLAG_BLOCKSIZE, |
| 124 | TWFLAG_CANBEWIPED, |
| 125 | TWFLAG_CANENCRYPTBACKUP, |
| 126 | TWFLAG_DISPLAY, |
| 127 | TWFLAG_ENCRYPTABLE, |
| 128 | TWFLAG_FLASHIMG, |
| 129 | TWFLAG_FORCEENCRYPT, |
| 130 | TWFLAG_FSFLAGS, |
| 131 | TWFLAG_IGNOREBLKID, |
| 132 | TWFLAG_LENGTH, |
| 133 | TWFLAG_MOUNTTODECRYPT, |
| 134 | TWFLAG_REMOVABLE, |
| 135 | TWFLAG_RETAINLAYOUTVERSION, |
| 136 | TWFLAG_SETTINGSSTORAGE, |
| 137 | TWFLAG_STORAGE, |
| 138 | TWFLAG_STORAGENAME, |
| 139 | TWFLAG_SUBPARTITIONOF, |
| 140 | TWFLAG_SYMLINK, |
| 141 | TWFLAG_USERDATAENCRYPTBACKUP, |
| 142 | TWFLAG_USERMRF, |
| 143 | TWFLAG_WIPEDURINGFACTORYRESET, |
| 144 | TWFLAG_WIPEINGUI, |
Ethan Yonker | 1b19016 | 2016-12-05 15:25:19 -0600 | [diff] [blame] | 145 | TWFLAG_SLOTSELECT, |
Matt Mower | 2416a50 | 2016-04-12 19:54:46 -0500 | [diff] [blame] | 146 | }; |
| 147 | |
| 148 | /* Flags without a trailing '=' are considered dual format flags and can be |
| 149 | * written as either 'flagname' or 'flagname=', where the character following |
| 150 | * the '=' is Y,y,1 for true and false otherwise. |
| 151 | */ |
| 152 | const struct flag_list tw_flags[] = { |
| 153 | { "andsec", TWFLAG_ANDSEC }, |
| 154 | { "backup", TWFLAG_BACKUP }, |
| 155 | { "backupname=", TWFLAG_BACKUPNAME }, |
| 156 | { "blocksize=", TWFLAG_BLOCKSIZE }, |
| 157 | { "canbewiped", TWFLAG_CANBEWIPED }, |
| 158 | { "canencryptbackup", TWFLAG_CANENCRYPTBACKUP }, |
| 159 | { "defaults", TWFLAG_DEFAULTS }, |
| 160 | { "display=", TWFLAG_DISPLAY }, |
| 161 | { "encryptable=", TWFLAG_ENCRYPTABLE }, |
| 162 | { "flashimg", TWFLAG_FLASHIMG }, |
| 163 | { "forceencrypt=", TWFLAG_FORCEENCRYPT }, |
| 164 | { "fsflags=", TWFLAG_FSFLAGS }, |
| 165 | { "ignoreblkid", TWFLAG_IGNOREBLKID }, |
| 166 | { "length=", TWFLAG_LENGTH }, |
| 167 | { "mounttodecrypt", TWFLAG_MOUNTTODECRYPT }, |
| 168 | { "removable", TWFLAG_REMOVABLE }, |
| 169 | { "retainlayoutversion", TWFLAG_RETAINLAYOUTVERSION }, |
| 170 | { "settingsstorage", TWFLAG_SETTINGSSTORAGE }, |
| 171 | { "storage", TWFLAG_STORAGE }, |
| 172 | { "storagename=", TWFLAG_STORAGENAME }, |
| 173 | { "subpartitionof=", TWFLAG_SUBPARTITIONOF }, |
| 174 | { "symlink=", TWFLAG_SYMLINK }, |
| 175 | { "userdataencryptbackup", TWFLAG_USERDATAENCRYPTBACKUP }, |
| 176 | { "usermrf", TWFLAG_USERMRF }, |
| 177 | { "wipeduringfactoryreset", TWFLAG_WIPEDURINGFACTORYRESET }, |
| 178 | { "wipeingui", TWFLAG_WIPEINGUI }, |
Ethan Yonker | 1b19016 | 2016-12-05 15:25:19 -0600 | [diff] [blame] | 179 | { "slotselect", TWFLAG_SLOTSELECT }, |
Matt Mower | 2416a50 | 2016-04-12 19:54:46 -0500 | [diff] [blame] | 180 | { 0, 0 }, |
| 181 | }; |
| 182 | |
that | 9e0593e | 2014-10-08 00:01:24 +0200 | [diff] [blame] | 183 | TWPartition::TWPartition() { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 184 | Can_Be_Mounted = false; |
| 185 | Can_Be_Wiped = false; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 186 | Can_Be_Backed_Up = false; |
Vojtech Bocek | 1dc3098 | 2013-08-30 21:49:30 +0200 | [diff] [blame] | 187 | Use_Rm_Rf = false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 188 | Wipe_During_Factory_Reset = false; |
| 189 | Wipe_Available_in_GUI = false; |
| 190 | Is_SubPartition = false; |
Dees_Troy | 2691f9d | 2012-09-24 11:15:49 -0400 | [diff] [blame] | 191 | Has_SubPartition = false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 192 | SubPartition_Of = ""; |
| 193 | Symlink_Path = ""; |
| 194 | Symlink_Mount_Point = ""; |
| 195 | Mount_Point = ""; |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 196 | Backup_Path = ""; |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 197 | Actual_Block_Device = ""; |
| 198 | Primary_Block_Device = ""; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 199 | Alternate_Block_Device = ""; |
| 200 | Removable = false; |
| 201 | Is_Present = false; |
| 202 | Length = 0; |
| 203 | Size = 0; |
| 204 | Used = 0; |
| 205 | Free = 0; |
| 206 | Backup_Size = 0; |
| 207 | Can_Be_Encrypted = false; |
| 208 | Is_Encrypted = false; |
| 209 | Is_Decrypted = false; |
Ethan Yonker | bd7492d | 2016-12-07 13:55:01 -0600 | [diff] [blame] | 210 | Is_FBE = false; |
Ethan Yonker | 253368a | 2014-11-25 15:00:52 -0600 | [diff] [blame] | 211 | Mount_To_Decrypt = false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 212 | Decrypted_Block_Device = ""; |
| 213 | Display_Name = ""; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 214 | Backup_Display_Name = ""; |
| 215 | Storage_Name = ""; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 216 | Backup_Name = ""; |
Dees_Troy | 63c8df7 | 2012-09-10 14:02:05 -0400 | [diff] [blame] | 217 | Backup_FileName = ""; |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 218 | MTD_Name = ""; |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 219 | Backup_Method = BM_NONE; |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 220 | Can_Encrypt_Backup = false; |
| 221 | Use_Userdata_Encryption = false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 222 | Has_Data_Media = false; |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 223 | Has_Android_Secure = false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 224 | Is_Storage = false; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 225 | Is_Settings_Storage = false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 226 | Storage_Path = ""; |
| 227 | Current_File_System = ""; |
| 228 | Fstab_File_System = ""; |
Hashcode | 62bd9e0 | 2013-11-19 21:59:42 -0800 | [diff] [blame] | 229 | Mount_Flags = 0; |
| 230 | Mount_Options = ""; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 231 | Format_Block_Size = 0; |
Dees_Troy | 68cab49 | 2012-12-12 19:29:35 +0000 | [diff] [blame] | 232 | Ignore_Blkid = false; |
Dees_Troy | 16c2b31 | 2013-01-15 16:51:18 +0000 | [diff] [blame] | 233 | Retain_Layout_Version = false; |
Ethan Yonker | 253368a | 2014-11-25 15:00:52 -0600 | [diff] [blame] | 234 | Crypto_Key_Location = "footer"; |
Ethan Yonker | 726a020 | 2014-12-16 20:01:38 -0600 | [diff] [blame] | 235 | MTP_Storage_ID = 0; |
Ethan Yonker | 96af84a | 2015-01-05 14:58:36 -0600 | [diff] [blame] | 236 | Can_Flash_Img = false; |
Ethan Yonker | eb32b1f | 2015-05-18 10:23:03 -0500 | [diff] [blame] | 237 | Mount_Read_Only = false; |
Ethan Yonker | 66a1949 | 2015-12-10 10:19:45 -0600 | [diff] [blame] | 238 | Is_Adopted_Storage = false; |
| 239 | Adopted_GUID = ""; |
Ethan Yonker | 1b19016 | 2016-12-05 15:25:19 -0600 | [diff] [blame] | 240 | SlotSelect = false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | TWPartition::~TWPartition(void) { |
| 244 | // Do nothing |
| 245 | } |
| 246 | |
Matt Mower | 2b2dd15 | 2016-04-26 11:24:08 -0500 | [diff] [blame] | 247 | bool TWPartition::Process_Fstab_Line(const char *fstab_line, bool Display_Error) { |
| 248 | char full_line[MAX_FSTAB_LINE_LENGTH]; |
Matt Mower | 2416a50 | 2016-04-12 19:54:46 -0500 | [diff] [blame] | 249 | char twflags[MAX_FSTAB_LINE_LENGTH] = ""; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 250 | char* ptr; |
Matt Mower | 2b2dd15 | 2016-04-26 11:24:08 -0500 | [diff] [blame] | 251 | int line_len = strlen(fstab_line), index = 0, item_index = 0; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 252 | bool skip = false; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 253 | |
Matt Mower | 2b2dd15 | 2016-04-26 11:24:08 -0500 | [diff] [blame] | 254 | strlcpy(full_line, fstab_line, sizeof(full_line)); |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 255 | for (index = 0; index < line_len; index++) { |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 256 | if (full_line[index] == 34) |
| 257 | skip = !skip; |
| 258 | if (!skip && full_line[index] <= 32) |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 259 | full_line[index] = '\0'; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 260 | } |
Dees_Troy | 7c2dec8 | 2012-09-26 09:49:14 -0400 | [diff] [blame] | 261 | Mount_Point = full_line; |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 262 | LOGINFO("Processing '%s'\n", Mount_Point.c_str()); |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 263 | Backup_Path = Mount_Point; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 264 | Storage_Path = Mount_Point; |
Dees_Troy | 70737fa | 2013-04-08 13:19:20 +0000 | [diff] [blame] | 265 | Display_Name = full_line + 1; |
| 266 | Backup_Display_Name = Display_Name; |
| 267 | Storage_Name = Display_Name; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 268 | index = Mount_Point.size(); |
| 269 | while (index < line_len) { |
| 270 | while (index < line_len && full_line[index] == '\0') |
| 271 | index++; |
| 272 | if (index >= line_len) |
| 273 | continue; |
| 274 | ptr = full_line + index; |
| 275 | if (item_index == 0) { |
| 276 | // File System |
| 277 | Fstab_File_System = ptr; |
| 278 | Current_File_System = ptr; |
| 279 | item_index++; |
| 280 | } else if (item_index == 1) { |
| 281 | // Primary Block Device |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 282 | if (Fstab_File_System == "mtd" || Fstab_File_System == "yaffs2") { |
Dees_Troy | 094207a | 2012-09-26 12:00:39 -0400 | [diff] [blame] | 283 | MTD_Name = ptr; |
| 284 | Find_MTD_Block_Device(MTD_Name); |
Dees_Troy | 5fcd8f9 | 2012-10-16 12:22:05 -0400 | [diff] [blame] | 285 | } else if (Fstab_File_System == "bml") { |
| 286 | if (Mount_Point == "/boot") |
| 287 | MTD_Name = "boot"; |
| 288 | else if (Mount_Point == "/recovery") |
| 289 | MTD_Name = "recovery"; |
| 290 | Primary_Block_Device = ptr; |
| 291 | if (*ptr != '/') |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 292 | 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] | 293 | } else if (*ptr != '/') { |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 294 | if (Display_Error) |
Matt Mower | 2b2dd15 | 2016-04-26 11:24:08 -0500 | [diff] [blame] | 295 | LOGERR("Invalid block device '%s' in fstab line '%s'", ptr, fstab_line); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 296 | else |
Matt Mower | 2b2dd15 | 2016-04-26 11:24:08 -0500 | [diff] [blame] | 297 | LOGINFO("Invalid block device '%s' in fstab line '%s'", ptr, fstab_line); |
Matt Mower | 72c87ce | 2016-04-26 14:34:56 -0500 | [diff] [blame] | 298 | return false; |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 299 | } else { |
| 300 | Primary_Block_Device = ptr; |
| 301 | Find_Real_Block_Device(Primary_Block_Device, Display_Error); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 302 | } |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 303 | item_index++; |
| 304 | } else if (item_index > 1) { |
| 305 | if (*ptr == '/') { |
| 306 | // Alternate Block Device |
| 307 | Alternate_Block_Device = ptr; |
| 308 | Find_Real_Block_Device(Alternate_Block_Device, Display_Error); |
| 309 | } else if (strlen(ptr) > 7 && strncmp(ptr, "length=", 7) == 0) { |
| 310 | // Partition length |
| 311 | ptr += 7; |
| 312 | Length = atoi(ptr); |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 313 | } else if (strlen(ptr) > 6 && strncmp(ptr, "flags=", 6) == 0) { |
| 314 | // Custom flags, save for later so that new values aren't overwritten by defaults |
| 315 | ptr += 6; |
Matt Mower | 2416a50 | 2016-04-12 19:54:46 -0500 | [diff] [blame] | 316 | strlcpy(twflags, ptr, sizeof(twflags)); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 317 | } else if (strlen(ptr) == 4 && (strncmp(ptr, "NULL", 4) == 0 || strncmp(ptr, "null", 4) == 0 || strncmp(ptr, "null", 4) == 0)) { |
| 318 | // Do nothing |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 319 | } else { |
| 320 | // Unhandled data |
Matt Mower | 2b2dd15 | 2016-04-26 11:24:08 -0500 | [diff] [blame] | 321 | 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] | 322 | } |
| 323 | } |
| 324 | while (index < line_len && full_line[index] != '\0') |
| 325 | index++; |
| 326 | } |
| 327 | |
| 328 | if (!Is_File_System(Fstab_File_System) && !Is_Image(Fstab_File_System)) { |
| 329 | if (Display_Error) |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 330 | LOGERR("Unknown File System: '%s'\n", Fstab_File_System.c_str()); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 331 | else |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 332 | LOGINFO("Unknown File System: '%s'\n", Fstab_File_System.c_str()); |
Matt Mower | 72c87ce | 2016-04-26 14:34:56 -0500 | [diff] [blame] | 333 | return false; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 334 | } else if (Is_File_System(Fstab_File_System)) { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 335 | Find_Actual_Block_Device(); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 336 | Setup_File_System(Display_Error); |
| 337 | if (Mount_Point == "/system") { |
| 338 | Display_Name = "System"; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 339 | Backup_Display_Name = Display_Name; |
| 340 | Storage_Name = Display_Name; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 341 | Wipe_Available_in_GUI = true; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 342 | Can_Be_Backed_Up = true; |
Ethan Yonker | eb32b1f | 2015-05-18 10:23:03 -0500 | [diff] [blame] | 343 | Mount_Read_Only = true; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 344 | } else if (Mount_Point == "/data") { |
| 345 | Display_Name = "Data"; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 346 | Backup_Display_Name = Display_Name; |
| 347 | Storage_Name = Display_Name; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 348 | Wipe_Available_in_GUI = true; |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 349 | Wipe_During_Factory_Reset = true; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 350 | Can_Be_Backed_Up = true; |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 351 | Can_Encrypt_Backup = true; |
| 352 | Use_Userdata_Encryption = true; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 353 | } else if (Mount_Point == "/cache") { |
| 354 | Display_Name = "Cache"; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 355 | Backup_Display_Name = Display_Name; |
| 356 | Storage_Name = Display_Name; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 357 | Wipe_Available_in_GUI = true; |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 358 | Wipe_During_Factory_Reset = true; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 359 | Can_Be_Backed_Up = true; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 360 | } else if (Mount_Point == "/datadata") { |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 361 | Wipe_During_Factory_Reset = true; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 362 | Display_Name = "DataData"; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 363 | Backup_Display_Name = Display_Name; |
| 364 | Storage_Name = Display_Name; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 365 | Is_SubPartition = true; |
| 366 | SubPartition_Of = "/data"; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 367 | DataManager::SetValue(TW_HAS_DATADATA, 1); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 368 | Can_Be_Backed_Up = true; |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 369 | Can_Encrypt_Backup = true; |
| 370 | Use_Userdata_Encryption = false; // This whole partition should be encrypted |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 371 | } else if (Mount_Point == "/sd-ext") { |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 372 | Wipe_During_Factory_Reset = true; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 373 | Display_Name = "SD-Ext"; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 374 | Backup_Display_Name = Display_Name; |
| 375 | Storage_Name = Display_Name; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 376 | Wipe_Available_in_GUI = true; |
Dees_Troy | c51f1f9 | 2012-09-20 15:32:13 -0400 | [diff] [blame] | 377 | Removable = true; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 378 | Can_Be_Backed_Up = true; |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 379 | Can_Encrypt_Backup = true; |
| 380 | Use_Userdata_Encryption = true; |
Dees_Troy | 2c50e18 | 2012-09-26 20:05:28 -0400 | [diff] [blame] | 381 | } else if (Mount_Point == "/boot") { |
| 382 | Display_Name = "Boot"; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 383 | Backup_Display_Name = Display_Name; |
Dees_Troy | 2c50e18 | 2012-09-26 20:05:28 -0400 | [diff] [blame] | 384 | DataManager::SetValue("tw_boot_is_mountable", 1); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 385 | Can_Be_Backed_Up = true; |
Ethan Yonker | 1673e3d | 2015-10-26 21:51:58 -0500 | [diff] [blame] | 386 | } else if (Mount_Point == "/vendor") { |
| 387 | Display_Name = "Vendor"; |
| 388 | Backup_Display_Name = Display_Name; |
| 389 | Storage_Name = Display_Name; |
| 390 | Mount_Read_Only = true; |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 391 | } |
| 392 | #ifdef TW_EXTERNAL_STORAGE_PATH |
| 393 | if (Mount_Point == EXPAND(TW_EXTERNAL_STORAGE_PATH)) { |
| 394 | Is_Storage = true; |
| 395 | Storage_Path = EXPAND(TW_EXTERNAL_STORAGE_PATH); |
Dees_Troy | c51f1f9 | 2012-09-20 15:32:13 -0400 | [diff] [blame] | 396 | Removable = true; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 397 | Wipe_Available_in_GUI = true; |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 398 | #else |
Dees_Troy | 70737fa | 2013-04-08 13:19:20 +0000 | [diff] [blame] | 399 | if (Mount_Point == "/sdcard" || Mount_Point == "/external_sd" || Mount_Point == "/external_sdcard") { |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 400 | Is_Storage = true; |
Dees_Troy | c51f1f9 | 2012-09-20 15:32:13 -0400 | [diff] [blame] | 401 | Removable = true; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 402 | Wipe_Available_in_GUI = true; |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 403 | #endif |
Dees_Troy | b05ddee | 2013-01-28 20:24:50 +0000 | [diff] [blame] | 404 | } |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 405 | #ifdef TW_INTERNAL_STORAGE_PATH |
| 406 | if (Mount_Point == EXPAND(TW_INTERNAL_STORAGE_PATH)) { |
| 407 | Is_Storage = true; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 408 | Is_Settings_Storage = true; |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 409 | Storage_Path = EXPAND(TW_INTERNAL_STORAGE_PATH); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 410 | Wipe_Available_in_GUI = true; |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 411 | } |
| 412 | #else |
Dees_Troy | 70737fa | 2013-04-08 13:19:20 +0000 | [diff] [blame] | 413 | if (Mount_Point == "/emmc" || Mount_Point == "/internal_sd" || Mount_Point == "/internal_sdcard") { |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 414 | Is_Storage = true; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 415 | Is_Settings_Storage = true; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 416 | Wipe_Available_in_GUI = true; |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 417 | } |
| 418 | #endif |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 419 | } else if (Is_Image(Fstab_File_System)) { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 420 | Find_Actual_Block_Device(); |
Ethan Yonker | 1b19016 | 2016-12-05 15:25:19 -0600 | [diff] [blame] | 421 | Setup_Image(); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 422 | if (Mount_Point == "/boot") { |
| 423 | Display_Name = "Boot"; |
| 424 | Backup_Display_Name = Display_Name; |
| 425 | Can_Be_Backed_Up = true; |
Ethan Yonker | 96af84a | 2015-01-05 14:58:36 -0600 | [diff] [blame] | 426 | Can_Flash_Img = true; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 427 | } else if (Mount_Point == "/recovery") { |
| 428 | Display_Name = "Recovery"; |
| 429 | Backup_Display_Name = Display_Name; |
Ethan Yonker | 96af84a | 2015-01-05 14:58:36 -0600 | [diff] [blame] | 430 | Can_Flash_Img = true; |
Ethan Yonker | eb32b1f | 2015-05-18 10:23:03 -0500 | [diff] [blame] | 431 | } else if (Mount_Point == "/system_image") { |
| 432 | Display_Name = "System Image"; |
| 433 | Backup_Display_Name = Display_Name; |
HashBang | ed974bb | 2016-01-30 14:20:09 -0500 | [diff] [blame] | 434 | Can_Flash_Img = true; |
Ethan Yonker | eb32b1f | 2015-05-18 10:23:03 -0500 | [diff] [blame] | 435 | Can_Be_Backed_Up = true; |
Ethan Yonker | 1673e3d | 2015-10-26 21:51:58 -0500 | [diff] [blame] | 436 | } else if (Mount_Point == "/vendor_image") { |
| 437 | Display_Name = "Vendor Image"; |
| 438 | Backup_Display_Name = Display_Name; |
HashBang | ed974bb | 2016-01-30 14:20:09 -0500 | [diff] [blame] | 439 | Can_Flash_Img = true; |
Ethan Yonker | 1673e3d | 2015-10-26 21:51:58 -0500 | [diff] [blame] | 440 | Can_Be_Backed_Up = true; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 441 | } |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 442 | } |
| 443 | |
Matt Mower | 2416a50 | 2016-04-12 19:54:46 -0500 | [diff] [blame] | 444 | // Process TWRP fstab flags |
| 445 | if (strlen(twflags) > 0) { |
| 446 | string Prev_Display_Name = Display_Name; |
| 447 | string Prev_Storage_Name = Storage_Name; |
| 448 | string Prev_Backup_Display_Name = Backup_Display_Name; |
| 449 | Display_Name = ""; |
| 450 | Storage_Name = ""; |
| 451 | Backup_Display_Name = ""; |
| 452 | |
| 453 | Process_TW_Flags(twflags, Display_Error); |
| 454 | |
| 455 | bool has_display_name = !Display_Name.empty(); |
| 456 | bool has_storage_name = !Storage_Name.empty(); |
| 457 | bool has_backup_name = !Backup_Display_Name.empty(); |
| 458 | if (!has_display_name) Display_Name = Prev_Display_Name; |
| 459 | if (!has_storage_name) Storage_Name = Prev_Storage_Name; |
| 460 | if (!has_backup_name) Backup_Display_Name = Prev_Backup_Display_Name; |
| 461 | |
| 462 | if (has_display_name && !has_storage_name) |
| 463 | Storage_Name = Display_Name; |
| 464 | if (!has_display_name && has_storage_name) |
| 465 | Display_Name = Storage_Name; |
| 466 | if (has_display_name && !has_backup_name && Backup_Display_Name != "Android Secure") |
| 467 | Backup_Display_Name = Display_Name; |
| 468 | if (!has_display_name && has_backup_name) |
| 469 | Display_Name = Backup_Display_Name; |
| 470 | } |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 471 | return true; |
| 472 | } |
| 473 | |
Matt Mower | 72c87ce | 2016-04-26 14:34:56 -0500 | [diff] [blame] | 474 | void TWPartition::Partition_Post_Processing(bool Display_Error) { |
| 475 | if (Mount_Point == "/data") |
| 476 | Setup_Data_Partition(Display_Error); |
| 477 | else if (Mount_Point == "/cache") |
| 478 | Setup_Cache_Partition(Display_Error); |
| 479 | } |
| 480 | |
Ethan Yonker | bd7492d | 2016-12-07 13:55:01 -0600 | [diff] [blame] | 481 | void TWPartition::ExcludeAll(const string& path) { |
| 482 | backup_exclusions.add_absolute_dir(path); |
| 483 | wipe_exclusions.add_absolute_dir(path); |
| 484 | } |
| 485 | |
Matt Mower | 72c87ce | 2016-04-26 14:34:56 -0500 | [diff] [blame] | 486 | void TWPartition::Setup_Data_Partition(bool Display_Error) { |
| 487 | if (Mount_Point != "/data") |
| 488 | return; |
| 489 | |
| 490 | // Ensure /data is not mounted as tmpfs for qcom hardware decrypt |
| 491 | UnMount(false); |
| 492 | |
| 493 | #ifdef TW_INCLUDE_CRYPTO |
| 494 | if (datamedia) |
| 495 | Setup_Data_Media(); |
| 496 | Can_Be_Encrypted = true; |
| 497 | char crypto_blkdev[255]; |
| 498 | property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error"); |
| 499 | if (strcmp(crypto_blkdev, "error") != 0) { |
| 500 | DataManager::SetValue(TW_IS_DECRYPTED, 1); |
| 501 | Is_Encrypted = true; |
| 502 | Is_Decrypted = true; |
Ethan Yonker | bd7492d | 2016-12-07 13:55:01 -0600 | [diff] [blame] | 503 | Is_FBE = false; |
| 504 | DataManager::SetValue(TW_IS_FBE, 0); |
Matt Mower | 72c87ce | 2016-04-26 14:34:56 -0500 | [diff] [blame] | 505 | Decrypted_Block_Device = crypto_blkdev; |
| 506 | LOGINFO("Data already decrypted, new block device: '%s'\n", crypto_blkdev); |
| 507 | } else if (!Mount(false)) { |
| 508 | if (Is_Present) { |
| 509 | set_partition_data(Actual_Block_Device.c_str(), Crypto_Key_Location.c_str(), Fstab_File_System.c_str()); |
| 510 | if (cryptfs_check_footer() == 0) { |
| 511 | Is_Encrypted = true; |
| 512 | Is_Decrypted = false; |
| 513 | Can_Be_Mounted = false; |
| 514 | Current_File_System = "emmc"; |
Ethan Yonker | 1b19016 | 2016-12-05 15:25:19 -0600 | [diff] [blame] | 515 | Setup_Image(); |
Matt Mower | 72c87ce | 2016-04-26 14:34:56 -0500 | [diff] [blame] | 516 | DataManager::SetValue(TW_IS_ENCRYPTED, 1); |
| 517 | DataManager::SetValue(TW_CRYPTO_PWTYPE, cryptfs_get_password_type()); |
| 518 | DataManager::SetValue(TW_CRYPTO_PASSWORD, ""); |
| 519 | DataManager::SetValue("tw_crypto_display", ""); |
| 520 | } else { |
| 521 | gui_err("mount_data_footer=Could not mount /data and unable to find crypto footer."); |
| 522 | } |
| 523 | } else { |
| 524 | LOGERR("Primary block device '%s' for mount point '%s' is not present!\n", Primary_Block_Device.c_str(), Mount_Point.c_str()); |
| 525 | } |
| 526 | } else { |
Ethan Yonker | bd7492d | 2016-12-07 13:55:01 -0600 | [diff] [blame] | 527 | if (TWFunc::Path_Exists("/data/unencrypted/key/version")) { |
| 528 | LOGINFO("File Based Encryption is present\n"); |
| 529 | #ifdef TW_INCLUDE_FBE |
| 530 | ExcludeAll(Mount_Point + "/convert_fbe"); |
| 531 | ExcludeAll(Mount_Point + "/unencrypted"); |
| 532 | //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 |
| 533 | ExcludeAll(Mount_Point + "/misc/vold/user_keys"); |
Ethan Yonker | 79f88bd | 2016-12-09 14:52:12 -0600 | [diff] [blame^] | 534 | //ExcludeAll(Mount_Point + "/system_ce"); |
| 535 | //ExcludeAll(Mount_Point + "/system_de"); |
| 536 | //ExcludeAll(Mount_Point + "/misc_ce"); |
| 537 | //ExcludeAll(Mount_Point + "/misc_de"); |
Ethan Yonker | bd7492d | 2016-12-07 13:55:01 -0600 | [diff] [blame] | 538 | ExcludeAll(Mount_Point + "/system/gatekeeper.password.key"); |
| 539 | ExcludeAll(Mount_Point + "/system/gatekeeper.pattern.key"); |
| 540 | ExcludeAll(Mount_Point + "/system/locksettings.db"); |
| 541 | //ExcludeAll(Mount_Point + "/system/locksettings.db-shm"); // don't seem to need this one, but the other 2 are needed |
| 542 | ExcludeAll(Mount_Point + "/system/locksettings.db-wal"); |
Ethan Yonker | 79f88bd | 2016-12-09 14:52:12 -0600 | [diff] [blame^] | 543 | //ExcludeAll(Mount_Point + "/user_de"); |
Ethan Yonker | bd7492d | 2016-12-07 13:55:01 -0600 | [diff] [blame] | 544 | //ExcludeAll(Mount_Point + "/misc/profiles/cur/0"); // might be important later |
| 545 | ExcludeAll(Mount_Point + "/misc/gatekeeper"); |
| 546 | ExcludeAll(Mount_Point + "/drm/kek.dat"); |
| 547 | int retry_count = 3; |
| 548 | while (!Decrypt_DE() && --retry_count) |
| 549 | usleep(2000); |
| 550 | if (retry_count > 0) { |
| 551 | property_set("ro.crypto.state", "encrypted"); |
| 552 | Is_Encrypted = true; |
| 553 | Is_Decrypted = false; |
| 554 | Is_FBE = true; |
| 555 | DataManager::SetValue(TW_IS_FBE, 1); |
| 556 | DataManager::SetValue(TW_IS_ENCRYPTED, 1); |
| 557 | string filename; |
| 558 | DataManager::SetValue(TW_CRYPTO_PWTYPE, Get_Password_Type(0, filename)); |
| 559 | DataManager::SetValue(TW_CRYPTO_PASSWORD, ""); |
| 560 | DataManager::SetValue("tw_crypto_display", ""); |
| 561 | } |
| 562 | #else |
| 563 | LOGERR("FBE found but FBE support not present in TWRP\n"); |
| 564 | #endif |
| 565 | } else { |
| 566 | // Filesystem is not encrypted and the mount succeeded, so return to |
| 567 | // the original unmounted state |
| 568 | UnMount(false); |
| 569 | } |
Matt Mower | 72c87ce | 2016-04-26 14:34:56 -0500 | [diff] [blame] | 570 | } |
| 571 | if (datamedia && (!Is_Encrypted || (Is_Encrypted && Is_Decrypted))) { |
| 572 | Setup_Data_Media(); |
| 573 | Recreate_Media_Folder(); |
| 574 | } |
| 575 | #else |
| 576 | if (datamedia) { |
| 577 | Setup_Data_Media(); |
| 578 | Recreate_Media_Folder(); |
| 579 | } |
| 580 | #endif |
| 581 | } |
| 582 | |
| 583 | void TWPartition::Setup_Cache_Partition(bool Display_Error __unused) { |
| 584 | if (Mount_Point != "/cache") |
| 585 | return; |
| 586 | |
| 587 | if (!Mount(true)) |
| 588 | return; |
| 589 | |
| 590 | if (!TWFunc::Path_Exists("/cache/recovery/.")) { |
| 591 | LOGINFO("Recreating /cache/recovery folder\n"); |
| 592 | if (mkdir("/cache/recovery", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0) |
| 593 | LOGERR("Could not create /cache/recovery\n"); |
| 594 | } |
| 595 | } |
| 596 | |
Matt Mower | 4ab42b1 | 2016-04-21 13:52:18 -0500 | [diff] [blame] | 597 | void TWPartition::Process_FS_Flags(const char *str) { |
| 598 | char *options = strdup(str); |
| 599 | char *ptr, *savep; |
Hashcode | 62bd9e0 | 2013-11-19 21:59:42 -0800 | [diff] [blame] | 600 | |
Matt Mower | 4ab42b1 | 2016-04-21 13:52:18 -0500 | [diff] [blame] | 601 | Mount_Options = ""; |
Hashcode | 62bd9e0 | 2013-11-19 21:59:42 -0800 | [diff] [blame] | 602 | |
Matt Mower | 4ab42b1 | 2016-04-21 13:52:18 -0500 | [diff] [blame] | 603 | // Avoid issues with potentially nested strtok by using strtok_r |
| 604 | ptr = strtok_r(options, ",", &savep); |
| 605 | while (ptr) { |
| 606 | const struct flag_list* mount_flag = mount_flags; |
| 607 | |
| 608 | for (; mount_flag->name; mount_flag++) { |
| 609 | // mount_flags are never postfixed by '=', |
| 610 | // so only match identical strings (including length) |
| 611 | if (strcmp(ptr, mount_flag->name) == 0) { |
| 612 | Mount_Flags |= mount_flag->flag; |
Hashcode | 62bd9e0 | 2013-11-19 21:59:42 -0800 | [diff] [blame] | 613 | break; |
| 614 | } |
| 615 | } |
| 616 | |
Matt Mower | 4ab42b1 | 2016-04-21 13:52:18 -0500 | [diff] [blame] | 617 | if (mount_flag->flag == MS_RDONLY) |
| 618 | Mount_Read_Only = true; |
Hashcode | 62bd9e0 | 2013-11-19 21:59:42 -0800 | [diff] [blame] | 619 | |
Matt Mower | 4ab42b1 | 2016-04-21 13:52:18 -0500 | [diff] [blame] | 620 | if (mount_flag->name != 0) { |
| 621 | if (!Mount_Options.empty()) |
| 622 | Mount_Options += ","; |
| 623 | Mount_Options += mount_flag->name; |
| 624 | } else { |
| 625 | LOGINFO("Unhandled mount flag: '%s'\n", ptr); |
| 626 | } |
| 627 | |
| 628 | ptr = strtok_r(NULL, ",", &savep); |
| 629 | } |
| 630 | free(options); |
Hashcode | 62bd9e0 | 2013-11-19 21:59:42 -0800 | [diff] [blame] | 631 | } |
| 632 | |
Matt Mower | 2416a50 | 2016-04-12 19:54:46 -0500 | [diff] [blame] | 633 | void TWPartition::Apply_TW_Flag(const unsigned flag, const char* str, const bool val) { |
| 634 | switch (flag) { |
| 635 | case TWFLAG_ANDSEC: |
| 636 | Has_Android_Secure = val; |
| 637 | break; |
| 638 | case TWFLAG_BACKUP: |
| 639 | Can_Be_Backed_Up = val; |
| 640 | break; |
| 641 | case TWFLAG_BACKUPNAME: |
| 642 | Backup_Display_Name = str; |
| 643 | break; |
| 644 | case TWFLAG_BLOCKSIZE: |
| 645 | Format_Block_Size = (unsigned long)(atol(str)); |
| 646 | break; |
| 647 | case TWFLAG_CANBEWIPED: |
| 648 | Can_Be_Wiped = val; |
| 649 | break; |
| 650 | case TWFLAG_CANENCRYPTBACKUP: |
| 651 | Can_Encrypt_Backup = val; |
| 652 | break; |
| 653 | case TWFLAG_DEFAULTS: |
| 654 | // Do nothing |
| 655 | break; |
| 656 | case TWFLAG_DISPLAY: |
| 657 | Display_Name = str; |
| 658 | break; |
| 659 | case TWFLAG_ENCRYPTABLE: |
| 660 | case TWFLAG_FORCEENCRYPT: |
| 661 | Crypto_Key_Location = str; |
| 662 | break; |
| 663 | case TWFLAG_FLASHIMG: |
| 664 | Can_Flash_Img = val; |
| 665 | break; |
| 666 | case TWFLAG_FSFLAGS: |
Matt Mower | 4ab42b1 | 2016-04-21 13:52:18 -0500 | [diff] [blame] | 667 | Process_FS_Flags(str); |
Matt Mower | 2416a50 | 2016-04-12 19:54:46 -0500 | [diff] [blame] | 668 | break; |
| 669 | case TWFLAG_IGNOREBLKID: |
| 670 | Ignore_Blkid = val; |
| 671 | break; |
| 672 | case TWFLAG_LENGTH: |
| 673 | Length = atoi(str); |
| 674 | break; |
| 675 | case TWFLAG_MOUNTTODECRYPT: |
| 676 | Mount_To_Decrypt = val; |
| 677 | break; |
| 678 | case TWFLAG_REMOVABLE: |
| 679 | Removable = val; |
| 680 | break; |
| 681 | case TWFLAG_RETAINLAYOUTVERSION: |
| 682 | Retain_Layout_Version = val; |
| 683 | break; |
| 684 | case TWFLAG_SETTINGSSTORAGE: |
| 685 | Is_Settings_Storage = val; |
| 686 | if (Is_Settings_Storage) |
| 687 | Is_Storage = true; |
| 688 | break; |
| 689 | case TWFLAG_STORAGE: |
| 690 | Is_Storage = val; |
| 691 | break; |
| 692 | case TWFLAG_STORAGENAME: |
| 693 | Storage_Name = str; |
| 694 | break; |
| 695 | case TWFLAG_SUBPARTITIONOF: |
| 696 | Is_SubPartition = true; |
| 697 | SubPartition_Of = str; |
| 698 | break; |
| 699 | case TWFLAG_SYMLINK: |
| 700 | Symlink_Path = str; |
| 701 | break; |
| 702 | case TWFLAG_USERDATAENCRYPTBACKUP: |
| 703 | Use_Userdata_Encryption = val; |
| 704 | if (Use_Userdata_Encryption) |
| 705 | Can_Encrypt_Backup = true; |
| 706 | break; |
| 707 | case TWFLAG_USERMRF: |
| 708 | Use_Rm_Rf = val; |
| 709 | break; |
| 710 | case TWFLAG_WIPEDURINGFACTORYRESET: |
| 711 | Wipe_During_Factory_Reset = val; |
| 712 | if (Wipe_During_Factory_Reset) { |
| 713 | Can_Be_Wiped = true; |
| 714 | Wipe_Available_in_GUI = true; |
| 715 | } |
| 716 | break; |
| 717 | case TWFLAG_WIPEINGUI: |
| 718 | Wipe_Available_in_GUI = val; |
| 719 | if (Wipe_Available_in_GUI) |
| 720 | Can_Be_Wiped = true; |
| 721 | break; |
Ethan Yonker | 1b19016 | 2016-12-05 15:25:19 -0600 | [diff] [blame] | 722 | case TWFLAG_SLOTSELECT: |
| 723 | SlotSelect = true; |
| 724 | break; |
Matt Mower | 2416a50 | 2016-04-12 19:54:46 -0500 | [diff] [blame] | 725 | default: |
| 726 | // Should not get here |
| 727 | LOGINFO("Flag identified for processing, but later unmatched: %i\n", flag); |
| 728 | break; |
| 729 | } |
| 730 | } |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 731 | |
Matt Mower | 2416a50 | 2016-04-12 19:54:46 -0500 | [diff] [blame] | 732 | void TWPartition::Process_TW_Flags(char *flags, bool Display_Error) { |
| 733 | char separator[2] = {'\n', 0}; |
| 734 | char *ptr, *savep; |
| 735 | |
| 736 | // Semicolons within double-quotes are not forbidden, so replace |
| 737 | // only the semicolons intended as separators with '\n' for strtok |
| 738 | for (unsigned i = 0, skip = 0; i < strlen(flags); i++) { |
| 739 | if (flags[i] == '\"') |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 740 | skip = !skip; |
Matt Mower | 2416a50 | 2016-04-12 19:54:46 -0500 | [diff] [blame] | 741 | if (!skip && flags[i] == ';') |
| 742 | flags[i] = separator[0]; |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 743 | } |
| 744 | |
Matt Mower | 2416a50 | 2016-04-12 19:54:46 -0500 | [diff] [blame] | 745 | // Avoid issues with potentially nested strtok by using strtok_r |
| 746 | ptr = strtok_r(flags, separator, &savep); |
| 747 | while (ptr) { |
| 748 | int ptr_len = strlen(ptr); |
| 749 | const struct flag_list* tw_flag = tw_flags; |
Hashcode | 62bd9e0 | 2013-11-19 21:59:42 -0800 | [diff] [blame] | 750 | |
Matt Mower | 2416a50 | 2016-04-12 19:54:46 -0500 | [diff] [blame] | 751 | for (; tw_flag->name; tw_flag++) { |
| 752 | int flag_len = strlen(tw_flag->name); |
| 753 | |
| 754 | if (strncmp(ptr, tw_flag->name, flag_len) == 0) { |
| 755 | bool flag_val = false; |
| 756 | |
| 757 | if (ptr_len > flag_len && (tw_flag->name)[flag_len-1] != '=' |
| 758 | && ptr[flag_len] != '=') { |
| 759 | // Handle flags with same starting string |
| 760 | // (e.g. backup and backupname) |
| 761 | continue; |
| 762 | } else if (ptr_len > flag_len && ptr[flag_len] == '=') { |
| 763 | // Handle flags with dual format: Part 1 |
| 764 | // (e.g. backup and backup=y. backup=y handled here) |
| 765 | ptr += flag_len + 1; |
| 766 | TWFunc::Strip_Quotes(ptr); |
| 767 | // Skip flags with empty argument |
| 768 | // (e.g. backup=) |
| 769 | if (strlen(ptr) == 0) { |
| 770 | LOGINFO("Flag missing argument or should not include '=': %s=\n", tw_flag->name); |
| 771 | break; |
| 772 | } |
| 773 | flag_val = strchr("yY1", *ptr) != NULL; |
| 774 | } else if (ptr_len == flag_len |
| 775 | && (tw_flag->name)[flag_len-1] == '=') { |
| 776 | // Skip flags missing argument after = |
| 777 | // (e.g. backupname=) |
| 778 | LOGINFO("Flag missing argument: %s\n", tw_flag->name); |
| 779 | break; |
| 780 | } else if (ptr_len > flag_len |
| 781 | && (tw_flag->name)[flag_len-1] == '=') { |
| 782 | // Handle arguments to flags |
| 783 | // (e.g. backupname="My Stuff") |
| 784 | ptr += flag_len; |
| 785 | TWFunc::Strip_Quotes(ptr); |
| 786 | // Skip flags with empty argument |
| 787 | // (e.g. backupname="") |
| 788 | if (strlen(ptr) == 0) { |
| 789 | LOGINFO("Flag missing argument: %s\n", tw_flag->name); |
| 790 | break; |
| 791 | } |
| 792 | } else if (ptr_len == flag_len) { |
| 793 | // Handle flags with dual format: Part 2 |
| 794 | // (e.g. backup and backup=y. backup handled here) |
| 795 | flag_val = true; |
Ethan Yonker | 96af84a | 2015-01-05 14:58:36 -0600 | [diff] [blame] | 796 | } else { |
Matt Mower | 2416a50 | 2016-04-12 19:54:46 -0500 | [diff] [blame] | 797 | LOGINFO("Flag matched, but could not be processed: %s\n", ptr); |
| 798 | break; |
Ethan Yonker | 96af84a | 2015-01-05 14:58:36 -0600 | [diff] [blame] | 799 | } |
Matt Mower | 2416a50 | 2016-04-12 19:54:46 -0500 | [diff] [blame] | 800 | |
| 801 | Apply_TW_Flag(tw_flag->flag, ptr, flag_val); |
| 802 | break; |
Ethan Yonker | 96af84a | 2015-01-05 14:58:36 -0600 | [diff] [blame] | 803 | } |
Matt Mower | 2416a50 | 2016-04-12 19:54:46 -0500 | [diff] [blame] | 804 | } |
| 805 | if (tw_flag->name == 0) { |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 806 | if (Display_Error) |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 807 | LOGERR("Unhandled flag: '%s'\n", ptr); |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 808 | else |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 809 | LOGINFO("Unhandled flag: '%s'\n", ptr); |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 810 | } |
Matt Mower | 2416a50 | 2016-04-12 19:54:46 -0500 | [diff] [blame] | 811 | ptr = strtok_r(NULL, separator, &savep); |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 812 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 813 | } |
| 814 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 815 | bool TWPartition::Is_File_System(string File_System) { |
| 816 | if (File_System == "ext2" || |
Dees_Troy | 63c8df7 | 2012-09-10 14:02:05 -0400 | [diff] [blame] | 817 | File_System == "ext3" || |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 818 | File_System == "ext4" || |
| 819 | File_System == "vfat" || |
| 820 | File_System == "ntfs" || |
| 821 | File_System == "yaffs2" || |
bigbiff bigbiff | 3e14652 | 2012-11-14 14:32:59 -0500 | [diff] [blame] | 822 | File_System == "exfat" || |
Dees_Troy | e501704 | 2013-08-29 16:38:55 +0000 | [diff] [blame] | 823 | File_System == "f2fs" || |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 824 | File_System == "auto") |
| 825 | return true; |
| 826 | else |
| 827 | return false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 828 | } |
| 829 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 830 | bool TWPartition::Is_Image(string File_System) { |
Dees_Troy | 5fcd8f9 | 2012-10-16 12:22:05 -0400 | [diff] [blame] | 831 | if (File_System == "emmc" || File_System == "mtd" || File_System == "bml") |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 832 | return true; |
| 833 | else |
| 834 | return false; |
| 835 | } |
| 836 | |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 837 | bool TWPartition::Make_Dir(string Path, bool Display_Error) { |
Ethan Yonker | 93ac7a0 | 2016-11-07 22:05:58 -0600 | [diff] [blame] | 838 | if (TWFunc::Get_D_Type_From_Stat(Path) != S_IFDIR) |
| 839 | unlink(Path.c_str()); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 840 | if (!TWFunc::Path_Exists(Path)) { |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 841 | if (mkdir(Path.c_str(), 0777) == -1) { |
| 842 | if (Display_Error) |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 843 | 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] | 844 | else |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 845 | LOGINFO("Can not create '%s' folder.\n", Path.c_str()); |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 846 | return false; |
| 847 | } else { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 848 | LOGINFO("Created '%s' folder.\n", Path.c_str()); |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 849 | return true; |
| 850 | } |
| 851 | } |
| 852 | return true; |
| 853 | } |
| 854 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 855 | void TWPartition::Setup_File_System(bool Display_Error) { |
| 856 | struct statfs st; |
| 857 | |
| 858 | Can_Be_Mounted = true; |
| 859 | Can_Be_Wiped = true; |
| 860 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 861 | // Make the mount point folder if it doesn't exist |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 862 | Make_Dir(Mount_Point, Display_Error); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 863 | Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1); |
| 864 | Backup_Name = Display_Name; |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 865 | Backup_Method = BM_FILES; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 866 | } |
| 867 | |
Ethan Yonker | 1b19016 | 2016-12-05 15:25:19 -0600 | [diff] [blame] | 868 | void TWPartition::Setup_Image() { |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 869 | Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1); |
| 870 | Backup_Name = Display_Name; |
Gary Peck | 82599a8 | 2012-11-21 16:23:12 -0800 | [diff] [blame] | 871 | if (Current_File_System == "emmc") |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 872 | Backup_Method = BM_DD; |
Gary Peck | 82599a8 | 2012-11-21 16:23:12 -0800 | [diff] [blame] | 873 | else if (Current_File_System == "mtd" || Current_File_System == "bml") |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 874 | Backup_Method = BM_FLASH_UTILS; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 875 | else |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 876 | 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] | 877 | } |
| 878 | |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 879 | void TWPartition::Setup_AndSec(void) { |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 880 | Backup_Display_Name = "Android Secure"; |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 881 | Backup_Name = "and-sec"; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 882 | Can_Be_Backed_Up = true; |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 883 | Has_Android_Secure = true; |
| 884 | Symlink_Path = Mount_Point + "/.android_secure"; |
| 885 | Symlink_Mount_Point = "/and-sec"; |
| 886 | Backup_Path = Symlink_Mount_Point; |
| 887 | Make_Dir("/and-sec", true); |
| 888 | Recreate_AndSec_Folder(); |
Kjell Braden | 3126a11 | 2016-06-19 16:58:15 +0000 | [diff] [blame] | 889 | Mount_Storage_Retry(true); |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 890 | } |
| 891 | |
that | 9e0593e | 2014-10-08 00:01:24 +0200 | [diff] [blame] | 892 | void TWPartition::Setup_Data_Media() { |
Ethan Yonker | 6277c79 | 2014-09-15 14:54:30 -0500 | [diff] [blame] | 893 | 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] | 894 | if (Storage_Name.empty() || Storage_Name == "Data") |
| 895 | Storage_Name = "Internal Storage"; |
Ethan Yonker | 6277c79 | 2014-09-15 14:54:30 -0500 | [diff] [blame] | 896 | Has_Data_Media = true; |
| 897 | Is_Storage = true; |
Ethan Yonker | 66a1949 | 2015-12-10 10:19:45 -0600 | [diff] [blame] | 898 | Storage_Path = Mount_Point + "/media"; |
Ethan Yonker | 6277c79 | 2014-09-15 14:54:30 -0500 | [diff] [blame] | 899 | Symlink_Path = Storage_Path; |
Ethan Yonker | 66a1949 | 2015-12-10 10:19:45 -0600 | [diff] [blame] | 900 | if (Mount_Point == "/data") { |
| 901 | Is_Settings_Storage = true; |
| 902 | if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) { |
| 903 | Make_Dir("/emmc", false); |
| 904 | Symlink_Mount_Point = "/emmc"; |
| 905 | } else { |
| 906 | Make_Dir("/sdcard", false); |
| 907 | Symlink_Mount_Point = "/sdcard"; |
| 908 | } |
| 909 | if (Mount(false) && TWFunc::Path_Exists(Mount_Point + "/media/0")) { |
| 910 | Storage_Path = Mount_Point + "/media/0"; |
| 911 | Symlink_Path = Storage_Path; |
| 912 | DataManager::SetValue(TW_INTERNAL_PATH, Mount_Point + "/media/0"); |
| 913 | UnMount(true); |
| 914 | } |
| 915 | DataManager::SetValue("tw_has_internal", 1); |
| 916 | DataManager::SetValue("tw_has_data_media", 1); |
Ethan Yonker | 3fdcda4 | 2016-11-30 12:29:37 -0600 | [diff] [blame] | 917 | backup_exclusions.add_absolute_dir("/data/data/com.google.android.music/files"); |
Ethan Yonker | 79f88bd | 2016-12-09 14:52:12 -0600 | [diff] [blame^] | 918 | ExcludeAll(Mount_Point + "/misc/vold"); |
| 919 | ExcludeAll(Mount_Point + "/.layout_version"); |
| 920 | ExcludeAll(Mount_Point + "/system/storage.xml"); |
Ethan Yonker | 6277c79 | 2014-09-15 14:54:30 -0500 | [diff] [blame] | 921 | } else { |
Ethan Yonker | 66a1949 | 2015-12-10 10:19:45 -0600 | [diff] [blame] | 922 | if (Mount(true) && TWFunc::Path_Exists(Mount_Point + "/media/0")) { |
| 923 | Storage_Path = Mount_Point + "/media/0"; |
| 924 | Symlink_Path = Storage_Path; |
| 925 | UnMount(true); |
| 926 | } |
Ethan Yonker | 6277c79 | 2014-09-15 14:54:30 -0500 | [diff] [blame] | 927 | } |
Ethan Yonker | 79f88bd | 2016-12-09 14:52:12 -0600 | [diff] [blame^] | 928 | ExcludeAll(Mount_Point + "/media"); |
Ethan Yonker | 6277c79 | 2014-09-15 14:54:30 -0500 | [diff] [blame] | 929 | } |
| 930 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 931 | void TWPartition::Find_Real_Block_Device(string& Block, bool Display_Error) { |
| 932 | char device[512], realDevice[512]; |
| 933 | |
| 934 | strcpy(device, Block.c_str()); |
| 935 | memset(realDevice, 0, sizeof(realDevice)); |
| 936 | while (readlink(device, realDevice, sizeof(realDevice)) > 0) |
| 937 | { |
| 938 | strcpy(device, realDevice); |
| 939 | memset(realDevice, 0, sizeof(realDevice)); |
| 940 | } |
| 941 | |
| 942 | if (device[0] != '/') { |
| 943 | if (Display_Error) |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 944 | 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] | 945 | else |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 946 | 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] | 947 | return; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 948 | } else { |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 949 | Block = device; |
| 950 | return; |
| 951 | } |
| 952 | } |
| 953 | |
Kjell Braden | 3126a11 | 2016-06-19 16:58:15 +0000 | [diff] [blame] | 954 | bool TWPartition::Mount_Storage_Retry(bool Display_Error) { |
Dees_Troy | 8e337f3 | 2012-10-13 22:07:49 -0400 | [diff] [blame] | 955 | // 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] | 956 | if (!Mount(Display_Error)) { |
Dees_Troy | 8e337f3 | 2012-10-13 22:07:49 -0400 | [diff] [blame] | 957 | int retry_count = 5; |
| 958 | while (retry_count > 0 && !Mount(false)) { |
| 959 | usleep(500000); |
| 960 | retry_count--; |
| 961 | } |
Kjell Braden | 3126a11 | 2016-06-19 16:58:15 +0000 | [diff] [blame] | 962 | return Mount(Display_Error); |
Dees_Troy | 8e337f3 | 2012-10-13 22:07:49 -0400 | [diff] [blame] | 963 | } |
Kjell Braden | 3126a11 | 2016-06-19 16:58:15 +0000 | [diff] [blame] | 964 | return true; |
Dees_Troy | 8e337f3 | 2012-10-13 22:07:49 -0400 | [diff] [blame] | 965 | } |
| 966 | |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 967 | bool TWPartition::Find_MTD_Block_Device(string MTD_Name) { |
| 968 | FILE *fp = NULL; |
| 969 | char line[255]; |
| 970 | |
| 971 | fp = fopen("/proc/mtd", "rt"); |
| 972 | if (fp == NULL) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 973 | LOGERR("Device does not support /proc/mtd\n"); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 974 | return false; |
| 975 | } |
| 976 | |
| 977 | while (fgets(line, sizeof(line), fp) != NULL) |
| 978 | { |
| 979 | char device[32], label[32]; |
| 980 | unsigned long size = 0; |
| 981 | char* fstype = NULL; |
| 982 | int deviceId; |
| 983 | |
| 984 | sscanf(line, "%s %lx %*s %*c%s", device, &size, label); |
| 985 | |
| 986 | // Skip header and blank lines |
| 987 | if ((strcmp(device, "dev:") == 0) || (strlen(line) < 8)) |
| 988 | continue; |
| 989 | |
| 990 | // Strip off the trailing " from the label |
| 991 | label[strlen(label)-1] = '\0'; |
| 992 | |
| 993 | if (strcmp(label, MTD_Name.c_str()) == 0) { |
| 994 | // We found our device |
| 995 | // Strip off the trailing : from the device |
| 996 | device[strlen(device)-1] = '\0'; |
| 997 | if (sscanf(device,"mtd%d", &deviceId) == 1) { |
| 998 | sprintf(device, "/dev/block/mtdblock%d", deviceId); |
| 999 | Primary_Block_Device = device; |
Dees_Troy | 76543db | 2013-06-19 16:24:30 +0000 | [diff] [blame] | 1000 | fclose(fp); |
| 1001 | return true; |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1002 | } |
| 1003 | } |
| 1004 | } |
| 1005 | fclose(fp); |
| 1006 | |
| 1007 | return false; |
| 1008 | } |
| 1009 | |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 1010 | bool TWPartition::Get_Size_Via_statfs(bool Display_Error) { |
| 1011 | struct statfs st; |
| 1012 | string Local_Path = Mount_Point + "/."; |
| 1013 | |
| 1014 | if (!Mount(Display_Error)) |
| 1015 | return false; |
| 1016 | |
| 1017 | if (statfs(Local_Path.c_str(), &st) != 0) { |
| 1018 | if (!Removable) { |
| 1019 | if (Display_Error) |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1020 | LOGERR("Unable to statfs '%s'\n", Local_Path.c_str()); |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 1021 | else |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1022 | LOGINFO("Unable to statfs '%s'\n", Local_Path.c_str()); |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 1023 | } |
| 1024 | return false; |
| 1025 | } |
| 1026 | Size = (st.f_blocks * st.f_bsize); |
| 1027 | Used = ((st.f_blocks - st.f_bfree) * st.f_bsize); |
| 1028 | Free = (st.f_bfree * st.f_bsize); |
| 1029 | Backup_Size = Used; |
| 1030 | return true; |
| 1031 | } |
| 1032 | |
| 1033 | bool TWPartition::Get_Size_Via_df(bool Display_Error) { |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1034 | FILE* fp; |
| 1035 | char command[255], line[512]; |
| 1036 | int include_block = 1; |
| 1037 | unsigned int min_len; |
| 1038 | |
| 1039 | if (!Mount(Display_Error)) |
| 1040 | return false; |
| 1041 | |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1042 | min_len = Actual_Block_Device.size() + 2; |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 1043 | sprintf(command, "df %s > /tmp/dfoutput.txt", Mount_Point.c_str()); |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 1044 | TWFunc::Exec_Cmd(command); |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 1045 | fp = fopen("/tmp/dfoutput.txt", "rt"); |
| 1046 | if (fp == NULL) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1047 | LOGINFO("Unable to open /tmp/dfoutput.txt.\n"); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1048 | return false; |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 1049 | } |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1050 | |
| 1051 | while (fgets(line, sizeof(line), fp) != NULL) |
| 1052 | { |
| 1053 | unsigned long blocks, used, available; |
| 1054 | char device[64]; |
| 1055 | char tmpString[64]; |
| 1056 | |
| 1057 | if (strncmp(line, "Filesystem", 10) == 0) |
| 1058 | continue; |
| 1059 | if (strlen(line) < min_len) { |
| 1060 | include_block = 0; |
| 1061 | continue; |
| 1062 | } |
| 1063 | if (include_block) { |
| 1064 | sscanf(line, "%s %lu %lu %lu", device, &blocks, &used, &available); |
| 1065 | } else { |
| 1066 | // The device block string is so long that the df information is on the next line |
| 1067 | int space_count = 0; |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 1068 | sprintf(tmpString, "/dev/block/%s", Actual_Block_Device.c_str()); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1069 | while (tmpString[space_count] == 32) |
| 1070 | space_count++; |
| 1071 | sscanf(line + space_count, "%lu %lu %lu", &blocks, &used, &available); |
| 1072 | } |
| 1073 | |
| 1074 | // Adjust block size to byte size |
| 1075 | Size = blocks * 1024ULL; |
| 1076 | Used = used * 1024ULL; |
| 1077 | Free = available * 1024ULL; |
| 1078 | Backup_Size = Used; |
| 1079 | } |
| 1080 | fclose(fp); |
| 1081 | return true; |
| 1082 | } |
| 1083 | |
Ethan Yonker | d18a821 | 2015-12-14 10:17:00 -0600 | [diff] [blame] | 1084 | unsigned long long TWPartition::IOCTL_Get_Block_Size() { |
Ethan Yonker | d18a821 | 2015-12-14 10:17:00 -0600 | [diff] [blame] | 1085 | Find_Actual_Block_Device(); |
Ethan Yonker | 483e9f4 | 2016-01-11 22:21:18 -0600 | [diff] [blame] | 1086 | |
| 1087 | return TWFunc::IOCTL_Get_Block_Size(Actual_Block_Device.c_str()); |
Ethan Yonker | d18a821 | 2015-12-14 10:17:00 -0600 | [diff] [blame] | 1088 | } |
| 1089 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1090 | bool TWPartition::Find_Partition_Size(void) { |
| 1091 | FILE* fp; |
| 1092 | char line[512]; |
| 1093 | string tmpdevice; |
| 1094 | |
igoriok | 87e3d93 | 2013-01-31 21:03:53 +0200 | [diff] [blame] | 1095 | fp = fopen("/proc/dumchar_info", "rt"); |
| 1096 | if (fp != NULL) { |
| 1097 | while (fgets(line, sizeof(line), fp) != NULL) |
| 1098 | { |
| 1099 | char label[32], device[32]; |
| 1100 | unsigned long size = 0; |
| 1101 | |
that | d43bf2d | 2014-09-21 23:13:02 +0200 | [diff] [blame] | 1102 | sscanf(line, "%s %lx %*x %*u %s", label, &size, device); |
igoriok | 87e3d93 | 2013-01-31 21:03:53 +0200 | [diff] [blame] | 1103 | |
bigbiff bigbiff | 34684ff | 2013-12-01 21:03:45 -0500 | [diff] [blame] | 1104 | // Skip header, annotation and blank lines |
igoriok | 87e3d93 | 2013-01-31 21:03:53 +0200 | [diff] [blame] | 1105 | if ((strncmp(device, "/dev/", 5) != 0) || (strlen(line) < 8)) |
| 1106 | continue; |
| 1107 | |
| 1108 | tmpdevice = "/dev/"; |
| 1109 | tmpdevice += label; |
| 1110 | if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) { |
| 1111 | Size = size; |
| 1112 | fclose(fp); |
| 1113 | return true; |
| 1114 | } |
| 1115 | } |
| 1116 | } |
| 1117 | |
Ethan Yonker | d18a821 | 2015-12-14 10:17:00 -0600 | [diff] [blame] | 1118 | unsigned long long ioctl_size = IOCTL_Get_Block_Size(); |
| 1119 | if (ioctl_size) { |
| 1120 | Size = ioctl_size; |
| 1121 | return true; |
| 1122 | } |
| 1123 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1124 | // In this case, we'll first get the partitions we care about (with labels) |
| 1125 | fp = fopen("/proc/partitions", "rt"); |
| 1126 | if (fp == NULL) |
| 1127 | return false; |
| 1128 | |
| 1129 | while (fgets(line, sizeof(line), fp) != NULL) |
| 1130 | { |
| 1131 | unsigned long major, minor, blocks; |
| 1132 | char device[512]; |
| 1133 | char tmpString[64]; |
| 1134 | |
Dees_Troy | 63c8df7 | 2012-09-10 14:02:05 -0400 | [diff] [blame] | 1135 | if (strlen(line) < 7 || line[0] == 'm') continue; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1136 | sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device); |
| 1137 | |
| 1138 | tmpdevice = "/dev/block/"; |
| 1139 | tmpdevice += device; |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1140 | if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) { |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1141 | // Adjust block size to byte size |
| 1142 | Size = blocks * 1024ULL; |
| 1143 | fclose(fp); |
| 1144 | return true; |
| 1145 | } |
| 1146 | } |
| 1147 | fclose(fp); |
| 1148 | return false; |
| 1149 | } |
| 1150 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1151 | bool TWPartition::Is_Mounted(void) { |
| 1152 | if (!Can_Be_Mounted) |
| 1153 | return false; |
| 1154 | |
| 1155 | struct stat st1, st2; |
| 1156 | string test_path; |
| 1157 | |
| 1158 | // Check to see if the mount point directory exists |
| 1159 | test_path = Mount_Point + "/."; |
| 1160 | if (stat(test_path.c_str(), &st1) != 0) return false; |
| 1161 | |
| 1162 | // Check to see if the directory above the mount point exists |
| 1163 | test_path = Mount_Point + "/../."; |
| 1164 | if (stat(test_path.c_str(), &st2) != 0) return false; |
| 1165 | |
| 1166 | // Compare the device IDs -- if they match then we're (probably) using tmpfs instead of an actual device |
| 1167 | int ret = (st1.st_dev != st2.st_dev) ? true : false; |
| 1168 | |
| 1169 | return ret; |
| 1170 | } |
| 1171 | |
James Christopher Adduono | d6f94ac | 2016-02-29 04:26:04 -0500 | [diff] [blame] | 1172 | bool TWPartition::Is_File_System_Writable(void) { |
| 1173 | if (!Is_File_System(Current_File_System) || !Is_Mounted()) |
| 1174 | return false; |
| 1175 | |
| 1176 | string test_path = Mount_Point + "/."; |
| 1177 | return (access(test_path.c_str(), W_OK) == 0); |
| 1178 | } |
| 1179 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1180 | bool TWPartition::Mount(bool Display_Error) { |
Dees_Troy | 3f5c4e8 | 2013-02-01 15:16:59 +0000 | [diff] [blame] | 1181 | int exfat_mounted = 0; |
Ethan Yonker | eb32b1f | 2015-05-18 10:23:03 -0500 | [diff] [blame] | 1182 | unsigned long flags = Mount_Flags; |
Dees_Troy | 3f5c4e8 | 2013-02-01 15:16:59 +0000 | [diff] [blame] | 1183 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1184 | if (Is_Mounted()) { |
| 1185 | return true; |
| 1186 | } else if (!Can_Be_Mounted) { |
| 1187 | return false; |
| 1188 | } |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1189 | |
| 1190 | Find_Actual_Block_Device(); |
| 1191 | |
| 1192 | // Check the current file system before mounting |
| 1193 | Check_FS_Type(); |
Dees_Troy | 3f5c4e8 | 2013-02-01 15:16:59 +0000 | [diff] [blame] | 1194 | if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/exfat-fuse")) { |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 1195 | 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] | 1196 | LOGINFO("cmd: %s\n", cmd.c_str()); |
Dees_Troy | 3f5c4e8 | 2013-02-01 15:16:59 +0000 | [diff] [blame] | 1197 | string result; |
| 1198 | if (TWFunc::Exec_Cmd(cmd, result) != 0) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1199 | 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] | 1200 | Current_File_System = "vfat"; |
| 1201 | } else { |
| 1202 | #ifdef TW_NO_EXFAT_FUSE |
| 1203 | UnMount(false); |
| 1204 | // We'll let the kernel handle it but using exfat-fuse to detect if the file system is actually exfat |
| 1205 | // Some kernels let us mount vfat as exfat which doesn't work out too well |
| 1206 | #else |
| 1207 | exfat_mounted = 1; |
| 1208 | #endif |
| 1209 | } |
| 1210 | } |
Ethan Yonker | eb32b1f | 2015-05-18 10:23:03 -0500 | [diff] [blame] | 1211 | |
Greg Wallace | b39e6c6 | 2015-12-29 00:55:26 -0500 | [diff] [blame] | 1212 | 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] | 1213 | string cmd; |
Greg Wallace | b39e6c6 | 2015-12-29 00:55:26 -0500 | [diff] [blame] | 1214 | string Ntfsmount_Binary = ""; |
| 1215 | |
| 1216 | if (TWFunc::Path_Exists("/sbin/ntfs-3g")) |
| 1217 | Ntfsmount_Binary = "ntfs-3g"; |
| 1218 | else if (TWFunc::Path_Exists("/sbin/mount.ntfs")) |
| 1219 | Ntfsmount_Binary = "mount.ntfs"; |
| 1220 | |
Ethan Yonker | b81d905 | 2015-07-09 13:20:53 -0500 | [diff] [blame] | 1221 | if (Mount_Read_Only) |
Greg Wallace | b39e6c6 | 2015-12-29 00:55:26 -0500 | [diff] [blame] | 1222 | cmd = "/sbin/" + Ntfsmount_Binary + " -o ro " + Actual_Block_Device + " " + Mount_Point; |
Ethan Yonker | b81d905 | 2015-07-09 13:20:53 -0500 | [diff] [blame] | 1223 | else |
Greg Wallace | b39e6c6 | 2015-12-29 00:55:26 -0500 | [diff] [blame] | 1224 | cmd = "/sbin/" + Ntfsmount_Binary + " " + Actual_Block_Device + " " + Mount_Point; |
Ethan Yonker | b81d905 | 2015-07-09 13:20:53 -0500 | [diff] [blame] | 1225 | LOGINFO("cmd: '%s'\n", cmd.c_str()); |
Greg Wallace | b39e6c6 | 2015-12-29 00:55:26 -0500 | [diff] [blame] | 1226 | |
Ethan Yonker | b81d905 | 2015-07-09 13:20:53 -0500 | [diff] [blame] | 1227 | if (TWFunc::Exec_Cmd(cmd) == 0) { |
| 1228 | return true; |
| 1229 | } else { |
| 1230 | LOGINFO("ntfs-3g failed to mount, trying regular mount method.\n"); |
| 1231 | } |
| 1232 | } |
| 1233 | |
Ethan Yonker | eb32b1f | 2015-05-18 10:23:03 -0500 | [diff] [blame] | 1234 | if (Mount_Read_Only) |
| 1235 | flags |= MS_RDONLY; |
| 1236 | |
Dees_Troy | 2204203 | 2012-12-18 21:23:08 +0000 | [diff] [blame] | 1237 | if (Fstab_File_System == "yaffs2") { |
| 1238 | // mount an MTD partition as a YAFFS2 filesystem. |
Ethan Yonker | eb32b1f | 2015-05-18 10:23:03 -0500 | [diff] [blame] | 1239 | flags = MS_NOATIME | MS_NODEV | MS_NODIRATIME; |
| 1240 | if (Mount_Read_Only) |
| 1241 | flags |= MS_RDONLY; |
Dees_Troy | 76543db | 2013-06-19 16:24:30 +0000 | [diff] [blame] | 1242 | if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags, NULL) < 0) { |
| 1243 | if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags | MS_RDONLY, NULL) < 0) { |
| 1244 | if (Display_Error) |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1245 | 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] | 1246 | else |
| 1247 | LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str()); |
| 1248 | return false; |
| 1249 | } else { |
| 1250 | LOGINFO("Mounted '%s' (MTD) as RO\n", Mount_Point.c_str()); |
| 1251 | return true; |
| 1252 | } |
| 1253 | } else { |
| 1254 | struct stat st; |
| 1255 | string test_path = Mount_Point; |
| 1256 | if (stat(test_path.c_str(), &st) < 0) { |
| 1257 | if (Display_Error) |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1258 | 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] | 1259 | else |
| 1260 | LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str()); |
| 1261 | return false; |
| 1262 | } |
| 1263 | mode_t new_mode = st.st_mode | S_IXUSR | S_IXGRP | S_IXOTH; |
| 1264 | if (new_mode != st.st_mode) { |
| 1265 | LOGINFO("Fixing execute permissions for %s\n", Mount_Point.c_str()); |
| 1266 | if (chmod(Mount_Point.c_str(), new_mode) < 0) { |
| 1267 | if (Display_Error) |
| 1268 | LOGERR("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno)); |
| 1269 | else |
| 1270 | LOGINFO("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno)); |
| 1271 | return false; |
| 1272 | } |
| 1273 | } |
Dees_Troy | 2204203 | 2012-12-18 21:23:08 +0000 | [diff] [blame] | 1274 | return true; |
Dees_Troy | 76543db | 2013-06-19 16:24:30 +0000 | [diff] [blame] | 1275 | } |
that | c7572eb | 2015-03-31 18:55:30 +0200 | [diff] [blame] | 1276 | } |
| 1277 | |
| 1278 | string mount_fs = Current_File_System; |
| 1279 | if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sys/module/texfat")) |
| 1280 | mount_fs = "texfat"; |
| 1281 | |
| 1282 | if (!exfat_mounted && |
Ethan Yonker | eb32b1f | 2015-05-18 10:23:03 -0500 | [diff] [blame] | 1283 | mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), mount_fs.c_str(), flags, Mount_Options.c_str()) != 0 && |
| 1284 | 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] | 1285 | #ifdef TW_NO_EXFAT_FUSE |
| 1286 | if (Current_File_System == "exfat") { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1287 | LOGINFO("Mounting exfat failed, trying vfat...\n"); |
Dees_Troy | 3f5c4e8 | 2013-02-01 15:16:59 +0000 | [diff] [blame] | 1288 | 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] | 1289 | if (Display_Error) |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1290 | 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] | 1291 | else |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1292 | LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str()); |
Ethan Yonker | eb32b1f | 2015-05-18 10:23:03 -0500 | [diff] [blame] | 1293 | 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] | 1294 | return false; |
Dees_Troy | 85f44ed | 2013-01-09 18:42:36 +0000 | [diff] [blame] | 1295 | } |
Dees_Troy | b05ddee | 2013-01-28 20:24:50 +0000 | [diff] [blame] | 1296 | } else { |
Dees_Troy | 3f5c4e8 | 2013-02-01 15:16:59 +0000 | [diff] [blame] | 1297 | #endif |
bigbiff bigbiff | 26774a0 | 2014-03-29 18:22:00 -0400 | [diff] [blame] | 1298 | if (!Removable && Display_Error) |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1299 | 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] | 1300 | else |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1301 | LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str()); |
| 1302 | 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] | 1303 | return false; |
| 1304 | #ifdef TW_NO_EXFAT_FUSE |
Dees_Troy | 85f44ed | 2013-01-09 18:42:36 +0000 | [diff] [blame] | 1305 | } |
| 1306 | #endif |
Dees_Troy | 3f5c4e8 | 2013-02-01 15:16:59 +0000 | [diff] [blame] | 1307 | } |
Ethan Yonker | 253368a | 2014-11-25 15:00:52 -0600 | [diff] [blame] | 1308 | |
Dees_Troy | 3f5c4e8 | 2013-02-01 15:16:59 +0000 | [diff] [blame] | 1309 | if (Removable) |
| 1310 | Update_Size(Display_Error); |
| 1311 | |
xiaolu | 9416f4f | 2015-06-04 08:22:23 +0800 | [diff] [blame] | 1312 | if (!Symlink_Mount_Point.empty() && TWFunc::Path_Exists(Symlink_Path)) { |
codelover | 2a3d4ce | 2015-03-14 20:26:49 +0800 | [diff] [blame] | 1313 | string Command = "mount -o bind '" + Symlink_Path + "' '" + Symlink_Mount_Point + "'"; |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 1314 | TWFunc::Exec_Cmd(Command); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1315 | } |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1316 | return true; |
| 1317 | } |
| 1318 | |
| 1319 | bool TWPartition::UnMount(bool Display_Error) { |
| 1320 | if (Is_Mounted()) { |
| 1321 | int never_unmount_system; |
| 1322 | |
| 1323 | DataManager::GetValue(TW_DONT_UNMOUNT_SYSTEM, never_unmount_system); |
| 1324 | if (never_unmount_system == 1 && Mount_Point == "/system") |
| 1325 | return true; // Never unmount system if you're not supposed to unmount it |
| 1326 | |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 1327 | if (Is_Storage) |
Ethan Yonker | 726a020 | 2014-12-16 20:01:38 -0600 | [diff] [blame] | 1328 | PartitionManager.Remove_MTP_Storage(MTP_Storage_ID); |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 1329 | |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1330 | if (!Symlink_Mount_Point.empty()) |
| 1331 | umount(Symlink_Mount_Point.c_str()); |
| 1332 | |
Dees_Troy | b05ddee | 2013-01-28 20:24:50 +0000 | [diff] [blame] | 1333 | umount(Mount_Point.c_str()); |
| 1334 | if (Is_Mounted()) { |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1335 | if (Display_Error) |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1336 | 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] | 1337 | else |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1338 | LOGINFO("Unable to unmount '%s'\n", Mount_Point.c_str()); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1339 | return false; |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 1340 | } else { |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1341 | return true; |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 1342 | } |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1343 | } else { |
| 1344 | return true; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1345 | } |
| 1346 | } |
| 1347 | |
James Christopher Adduono | d6f94ac | 2016-02-29 04:26:04 -0500 | [diff] [blame] | 1348 | bool TWPartition::ReMount(bool Display_Error) { |
| 1349 | if (UnMount(Display_Error)) |
| 1350 | return Mount(Display_Error); |
| 1351 | return false; |
| 1352 | } |
| 1353 | |
| 1354 | bool TWPartition::ReMount_RW(bool Display_Error) { |
| 1355 | // No need to remount if already mounted rw |
| 1356 | if (Is_File_System_Writable()) |
| 1357 | return true; |
| 1358 | |
| 1359 | bool ro = Mount_Read_Only; |
| 1360 | int flags = Mount_Flags; |
| 1361 | |
| 1362 | Mount_Read_Only = false; |
| 1363 | Mount_Flags &= ~MS_RDONLY; |
| 1364 | |
| 1365 | bool ret = ReMount(Display_Error); |
| 1366 | |
| 1367 | Mount_Read_Only = ro; |
| 1368 | Mount_Flags = flags; |
| 1369 | |
| 1370 | return ret; |
| 1371 | } |
| 1372 | |
Gary Peck | 43acadf | 2012-11-21 21:19:01 -0800 | [diff] [blame] | 1373 | bool TWPartition::Wipe(string New_File_System) { |
Ethan Yonker | 726a020 | 2014-12-16 20:01:38 -0600 | [diff] [blame] | 1374 | bool wiped = false, update_crypt = false, recreate_media = true; |
Dees_Troy | 16c2b31 | 2013-01-15 16:51:18 +0000 | [diff] [blame] | 1375 | int check; |
| 1376 | string Layout_Filename = Mount_Point + "/.layout_version"; |
| 1377 | |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1378 | if (!Can_Be_Wiped) { |
Matt Mower | 3c36697 | 2015-12-25 19:28:31 -0600 | [diff] [blame] | 1379 | 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] | 1380 | return false; |
| 1381 | } |
| 1382 | |
Dees_Troy | c51f1f9 | 2012-09-20 15:32:13 -0400 | [diff] [blame] | 1383 | if (Mount_Point == "/cache") |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1384 | Log_Offset = 0; |
Dees_Troy | c51f1f9 | 2012-09-20 15:32:13 -0400 | [diff] [blame] | 1385 | |
Dees_Troy | 16c2b31 | 2013-01-15 16:51:18 +0000 | [diff] [blame] | 1386 | if (Retain_Layout_Version && Mount(false) && TWFunc::Path_Exists(Layout_Filename)) |
| 1387 | TWFunc::copy_file(Layout_Filename, "/.layout_version", 0600); |
| 1388 | else |
| 1389 | unlink("/.layout_version"); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1390 | |
Ethan Yonker | 87c7bac | 2014-05-25 21:41:08 -0500 | [diff] [blame] | 1391 | if (Has_Data_Media && Current_File_System == New_File_System) { |
Dees_Troy | 16c2b31 | 2013-01-15 16:51:18 +0000 | [diff] [blame] | 1392 | wiped = Wipe_Data_Without_Wiping_Media(); |
Ethan Yonker | 5eac222 | 2014-06-11 12:22:55 -0500 | [diff] [blame] | 1393 | recreate_media = false; |
Dees_Troy | 16c2b31 | 2013-01-15 16:51:18 +0000 | [diff] [blame] | 1394 | } else { |
Dees_Troy | 16c2b31 | 2013-01-15 16:51:18 +0000 | [diff] [blame] | 1395 | DataManager::GetValue(TW_RM_RF_VAR, check); |
| 1396 | |
Hashcode | dabfd49 | 2013-08-29 22:45:30 -0700 | [diff] [blame] | 1397 | if (check || Use_Rm_Rf) |
Dees_Troy | 16c2b31 | 2013-01-15 16:51:18 +0000 | [diff] [blame] | 1398 | wiped = Wipe_RMRF(); |
| 1399 | else if (New_File_System == "ext4") |
| 1400 | wiped = Wipe_EXT4(); |
| 1401 | else if (New_File_System == "ext2" || New_File_System == "ext3") |
| 1402 | wiped = Wipe_EXT23(New_File_System); |
| 1403 | else if (New_File_System == "vfat") |
| 1404 | wiped = Wipe_FAT(); |
| 1405 | else if (New_File_System == "exfat") |
| 1406 | wiped = Wipe_EXFAT(); |
| 1407 | else if (New_File_System == "yaffs2") |
| 1408 | wiped = Wipe_MTD(); |
Dees_Troy | e501704 | 2013-08-29 16:38:55 +0000 | [diff] [blame] | 1409 | else if (New_File_System == "f2fs") |
| 1410 | wiped = Wipe_F2FS(); |
Ethan Yonker | b81d905 | 2015-07-09 13:20:53 -0500 | [diff] [blame] | 1411 | else if (New_File_System == "ntfs") |
| 1412 | wiped = Wipe_NTFS(); |
Dees_Troy | 16c2b31 | 2013-01-15 16:51:18 +0000 | [diff] [blame] | 1413 | else { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1414 | 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] | 1415 | unlink("/.layout_version"); |
| 1416 | return false; |
| 1417 | } |
| 1418 | update_crypt = wiped; |
Gary Peck | e8bc5d7 | 2012-12-21 06:45:25 -0800 | [diff] [blame] | 1419 | } |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1420 | |
Gary Peck | e8bc5d7 | 2012-12-21 06:45:25 -0800 | [diff] [blame] | 1421 | if (wiped) { |
Dees_Troy | 1c1ac44 | 2013-01-17 21:42:14 +0000 | [diff] [blame] | 1422 | if (Mount_Point == "/cache") |
| 1423 | DataManager::Output_Version(); |
| 1424 | |
Dees_Troy | 16c2b31 | 2013-01-15 16:51:18 +0000 | [diff] [blame] | 1425 | if (TWFunc::Path_Exists("/.layout_version") && Mount(false)) |
| 1426 | TWFunc::copy_file("/.layout_version", Layout_Filename, 0600); |
| 1427 | |
| 1428 | if (update_crypt) { |
| 1429 | Setup_File_System(false); |
| 1430 | if (Is_Encrypted && !Is_Decrypted) { |
| 1431 | // just wiped an encrypted partition back to its unencrypted state |
| 1432 | Is_Encrypted = false; |
| 1433 | Is_Decrypted = false; |
| 1434 | Decrypted_Block_Device = ""; |
| 1435 | if (Mount_Point == "/data") { |
| 1436 | DataManager::SetValue(TW_IS_ENCRYPTED, 0); |
| 1437 | DataManager::SetValue(TW_IS_DECRYPTED, 0); |
| 1438 | } |
Gary Peck | e8bc5d7 | 2012-12-21 06:45:25 -0800 | [diff] [blame] | 1439 | } |
| 1440 | } |
Ethan Yonker | 5eac222 | 2014-06-11 12:22:55 -0500 | [diff] [blame] | 1441 | |
Ethan Yonker | 66a1949 | 2015-12-10 10:19:45 -0600 | [diff] [blame] | 1442 | if (Has_Data_Media && recreate_media) { |
Ethan Yonker | 5eac222 | 2014-06-11 12:22:55 -0500 | [diff] [blame] | 1443 | Recreate_Media_Folder(); |
| 1444 | } |
Matt Mower | 209c963 | 2016-01-20 12:08:35 -0600 | [diff] [blame] | 1445 | if (Is_Storage && Mount(false)) |
| 1446 | PartitionManager.Add_MTP_Storage(MTP_Storage_ID); |
Gary Peck | e8bc5d7 | 2012-12-21 06:45:25 -0800 | [diff] [blame] | 1447 | } |
Matt Mower | 209c963 | 2016-01-20 12:08:35 -0600 | [diff] [blame] | 1448 | |
Gary Peck | e8bc5d7 | 2012-12-21 06:45:25 -0800 | [diff] [blame] | 1449 | return wiped; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1450 | } |
| 1451 | |
Gary Peck | 43acadf | 2012-11-21 21:19:01 -0800 | [diff] [blame] | 1452 | bool TWPartition::Wipe() { |
Gary Peck | 82599a8 | 2012-11-21 16:23:12 -0800 | [diff] [blame] | 1453 | if (Is_File_System(Current_File_System)) |
| 1454 | return Wipe(Current_File_System); |
| 1455 | else |
| 1456 | return Wipe(Fstab_File_System); |
Gary Peck | 43acadf | 2012-11-21 21:19:01 -0800 | [diff] [blame] | 1457 | } |
| 1458 | |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 1459 | bool TWPartition::Wipe_AndSec(void) { |
| 1460 | if (!Has_Android_Secure) |
| 1461 | return false; |
| 1462 | |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 1463 | if (!Mount(true)) |
| 1464 | return false; |
| 1465 | |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1466 | gui_msg(Msg("wiping=Wiping {1}")(Backup_Display_Name)); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1467 | TWFunc::removeDir(Mount_Point + "/.android_secure/", true); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1468 | return true; |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 1469 | } |
| 1470 | |
Ethan Yonker | 87c7bac | 2014-05-25 21:41:08 -0500 | [diff] [blame] | 1471 | bool TWPartition::Can_Repair() { |
Ethan Yonker | a271915 | 2015-05-28 09:44:41 -0500 | [diff] [blame] | 1472 | if (Mount_Read_Only) |
| 1473 | return false; |
Matt Mower | 18794c8 | 2015-11-11 16:22:45 -0600 | [diff] [blame] | 1474 | if (Current_File_System == "vfat" && TWFunc::Path_Exists("/sbin/fsck.fat")) |
Ethan Yonker | 87c7bac | 2014-05-25 21:41:08 -0500 | [diff] [blame] | 1475 | return true; |
| 1476 | else if ((Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") && TWFunc::Path_Exists("/sbin/e2fsck")) |
| 1477 | return true; |
| 1478 | else if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/fsck.exfat")) |
| 1479 | return true; |
| 1480 | else if (Current_File_System == "f2fs" && TWFunc::Path_Exists("/sbin/fsck.f2fs")) |
| 1481 | return true; |
Greg Wallace | b39e6c6 | 2015-12-29 00:55:26 -0500 | [diff] [blame] | 1482 | 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] | 1483 | return true; |
Ethan Yonker | 87c7bac | 2014-05-25 21:41:08 -0500 | [diff] [blame] | 1484 | return false; |
| 1485 | } |
| 1486 | |
| 1487 | bool TWPartition::Repair() { |
| 1488 | string command; |
| 1489 | |
| 1490 | if (Current_File_System == "vfat") { |
Matt Mower | 18794c8 | 2015-11-11 16:22:45 -0600 | [diff] [blame] | 1491 | if (!TWFunc::Path_Exists("/sbin/fsck.fat")) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1492 | 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] | 1493 | return false; |
| 1494 | } |
| 1495 | if (!UnMount(true)) |
| 1496 | return false; |
Greg Wallace | 2d65340 | 2015-12-29 14:19:21 -0500 | [diff] [blame] | 1497 | 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] | 1498 | Find_Actual_Block_Device(); |
Matt Mower | 18794c8 | 2015-11-11 16:22:45 -0600 | [diff] [blame] | 1499 | command = "/sbin/fsck.fat -y " + Actual_Block_Device; |
Ethan Yonker | 87c7bac | 2014-05-25 21:41:08 -0500 | [diff] [blame] | 1500 | LOGINFO("Repair command: %s\n", command.c_str()); |
| 1501 | if (TWFunc::Exec_Cmd(command) == 0) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1502 | gui_msg("done=Done."); |
Ethan Yonker | 87c7bac | 2014-05-25 21:41:08 -0500 | [diff] [blame] | 1503 | return true; |
| 1504 | } else { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1505 | 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] | 1506 | return false; |
| 1507 | } |
| 1508 | } |
| 1509 | if (Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") { |
| 1510 | if (!TWFunc::Path_Exists("/sbin/e2fsck")) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1511 | 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] | 1512 | return false; |
| 1513 | } |
| 1514 | if (!UnMount(true)) |
| 1515 | return false; |
Greg Wallace | 2d65340 | 2015-12-29 14:19:21 -0500 | [diff] [blame] | 1516 | gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("e2fsck")); |
Ethan Yonker | 87c7bac | 2014-05-25 21:41:08 -0500 | [diff] [blame] | 1517 | Find_Actual_Block_Device(); |
Ethan Yonker | a271915 | 2015-05-28 09:44:41 -0500 | [diff] [blame] | 1518 | command = "/sbin/e2fsck -fp " + Actual_Block_Device; |
Ethan Yonker | 87c7bac | 2014-05-25 21:41:08 -0500 | [diff] [blame] | 1519 | LOGINFO("Repair command: %s\n", command.c_str()); |
| 1520 | if (TWFunc::Exec_Cmd(command) == 0) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1521 | gui_msg("done=Done."); |
Ethan Yonker | 87c7bac | 2014-05-25 21:41:08 -0500 | [diff] [blame] | 1522 | return true; |
| 1523 | } else { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1524 | 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] | 1525 | return false; |
| 1526 | } |
| 1527 | } |
| 1528 | if (Current_File_System == "exfat") { |
| 1529 | if (!TWFunc::Path_Exists("/sbin/fsck.exfat")) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1530 | 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] | 1531 | return false; |
| 1532 | } |
| 1533 | if (!UnMount(true)) |
| 1534 | return false; |
Greg Wallace | 2d65340 | 2015-12-29 14:19:21 -0500 | [diff] [blame] | 1535 | 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] | 1536 | Find_Actual_Block_Device(); |
| 1537 | command = "/sbin/fsck.exfat " + Actual_Block_Device; |
| 1538 | LOGINFO("Repair command: %s\n", command.c_str()); |
| 1539 | if (TWFunc::Exec_Cmd(command) == 0) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1540 | gui_msg("done=Done."); |
Ethan Yonker | 87c7bac | 2014-05-25 21:41:08 -0500 | [diff] [blame] | 1541 | return true; |
| 1542 | } else { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1543 | 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] | 1544 | return false; |
| 1545 | } |
| 1546 | } |
| 1547 | if (Current_File_System == "f2fs") { |
| 1548 | if (!TWFunc::Path_Exists("/sbin/fsck.f2fs")) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1549 | 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] | 1550 | return false; |
| 1551 | } |
| 1552 | if (!UnMount(true)) |
| 1553 | return false; |
Greg Wallace | 2d65340 | 2015-12-29 14:19:21 -0500 | [diff] [blame] | 1554 | 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] | 1555 | Find_Actual_Block_Device(); |
| 1556 | command = "/sbin/fsck.f2fs " + Actual_Block_Device; |
| 1557 | LOGINFO("Repair command: %s\n", command.c_str()); |
| 1558 | if (TWFunc::Exec_Cmd(command) == 0) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1559 | gui_msg("done=Done."); |
Ethan Yonker | 87c7bac | 2014-05-25 21:41:08 -0500 | [diff] [blame] | 1560 | return true; |
| 1561 | } else { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1562 | 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] | 1563 | return false; |
| 1564 | } |
| 1565 | } |
Ethan Yonker | b81d905 | 2015-07-09 13:20:53 -0500 | [diff] [blame] | 1566 | if (Current_File_System == "ntfs") { |
Greg Wallace | b39e6c6 | 2015-12-29 00:55:26 -0500 | [diff] [blame] | 1567 | string Ntfsfix_Binary; |
| 1568 | if (TWFunc::Path_Exists("/sbin/ntfsfix")) |
| 1569 | Ntfsfix_Binary = "ntfsfix"; |
| 1570 | else if (TWFunc::Path_Exists("/sbin/fsck.ntfs")) |
| 1571 | Ntfsfix_Binary = "fsck.ntfs"; |
| 1572 | else { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1573 | 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] | 1574 | return false; |
| 1575 | } |
| 1576 | if (!UnMount(true)) |
| 1577 | return false; |
Greg Wallace | 2d65340 | 2015-12-29 14:19:21 -0500 | [diff] [blame] | 1578 | 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] | 1579 | Find_Actual_Block_Device(); |
Greg Wallace | b39e6c6 | 2015-12-29 00:55:26 -0500 | [diff] [blame] | 1580 | command = "/sbin/" + Ntfsfix_Binary + " " + Actual_Block_Device; |
Ethan Yonker | b81d905 | 2015-07-09 13:20:53 -0500 | [diff] [blame] | 1581 | LOGINFO("Repair command: %s\n", command.c_str()); |
| 1582 | if (TWFunc::Exec_Cmd(command) == 0) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1583 | gui_msg("done=Done."); |
Ethan Yonker | b81d905 | 2015-07-09 13:20:53 -0500 | [diff] [blame] | 1584 | return true; |
| 1585 | } else { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1586 | 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] | 1587 | return false; |
| 1588 | } |
| 1589 | } |
Ethan Yonker | 87c7bac | 2014-05-25 21:41:08 -0500 | [diff] [blame] | 1590 | return false; |
| 1591 | } |
| 1592 | |
Ethan Yonker | a271915 | 2015-05-28 09:44:41 -0500 | [diff] [blame] | 1593 | bool TWPartition::Can_Resize() { |
| 1594 | if (Mount_Read_Only) |
| 1595 | return false; |
| 1596 | if ((Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") && TWFunc::Path_Exists("/sbin/resize2fs")) |
| 1597 | return true; |
| 1598 | return false; |
| 1599 | } |
| 1600 | |
| 1601 | bool TWPartition::Resize() { |
| 1602 | string command; |
| 1603 | |
| 1604 | if (Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") { |
| 1605 | if (!Can_Repair()) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1606 | LOGINFO("Cannot resize %s because %s cannot be repaired before resizing.\n", Display_Name.c_str(), Display_Name.c_str()); |
| 1607 | gui_msg(Msg(msg::kError, "cannot_resize=Cannot resize {1}.")(Display_Name)); |
Ethan Yonker | a271915 | 2015-05-28 09:44:41 -0500 | [diff] [blame] | 1608 | return false; |
| 1609 | } |
| 1610 | if (!TWFunc::Path_Exists("/sbin/resize2fs")) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1611 | LOGINFO("resize2fs does not exist! Cannot resize!\n"); |
| 1612 | gui_msg(Msg(msg::kError, "cannot_resize=Cannot resize {1}.")(Display_Name)); |
Ethan Yonker | a271915 | 2015-05-28 09:44:41 -0500 | [diff] [blame] | 1613 | return false; |
| 1614 | } |
| 1615 | // Repair will unmount so no need to do it twice |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1616 | gui_msg(Msg("repair_resize=Repairing {1} before resizing.")( Display_Name)); |
Ethan Yonker | a271915 | 2015-05-28 09:44:41 -0500 | [diff] [blame] | 1617 | if (!Repair()) |
| 1618 | return false; |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1619 | gui_msg(Msg("resizing=Resizing {1} using {2}...")(Display_Name)("resize2fs")); |
Ethan Yonker | a271915 | 2015-05-28 09:44:41 -0500 | [diff] [blame] | 1620 | Find_Actual_Block_Device(); |
| 1621 | command = "/sbin/resize2fs " + Actual_Block_Device; |
| 1622 | if (Length != 0) { |
Ethan Yonker | d18a821 | 2015-12-14 10:17:00 -0600 | [diff] [blame] | 1623 | unsigned long long Actual_Size = IOCTL_Get_Block_Size(); |
| 1624 | if (Actual_Size == 0) |
| 1625 | return false; |
Ethan Yonker | a271915 | 2015-05-28 09:44:41 -0500 | [diff] [blame] | 1626 | |
Ethan Yonker | a271915 | 2015-05-28 09:44:41 -0500 | [diff] [blame] | 1627 | unsigned long long Block_Count; |
| 1628 | if (Length < 0) { |
| 1629 | // Reduce overall size by this length |
| 1630 | Block_Count = (Actual_Size / 1024LLU) - ((unsigned long long)(Length * -1) / 1024LLU); |
| 1631 | } else { |
| 1632 | // This is the size, not a size reduction |
| 1633 | Block_Count = ((unsigned long long)(Length) / 1024LLU); |
| 1634 | } |
| 1635 | char temp[256]; |
| 1636 | sprintf(temp, "%llu", Block_Count); |
| 1637 | command += " "; |
| 1638 | command += temp; |
| 1639 | command += "K"; |
| 1640 | } |
| 1641 | LOGINFO("Resize command: %s\n", command.c_str()); |
| 1642 | if (TWFunc::Exec_Cmd(command) == 0) { |
| 1643 | Update_Size(true); |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1644 | gui_msg("done=Done."); |
Ethan Yonker | a271915 | 2015-05-28 09:44:41 -0500 | [diff] [blame] | 1645 | return true; |
| 1646 | } else { |
| 1647 | Update_Size(true); |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1648 | 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] | 1649 | return false; |
| 1650 | } |
| 1651 | } |
| 1652 | return false; |
| 1653 | } |
| 1654 | |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 1655 | bool TWPartition::Backup(PartitionSettings *part_settings, pid_t *tar_fork_pid) { |
| 1656 | if (Backup_Method == BM_FILES) |
| 1657 | return Backup_Tar(part_settings, tar_fork_pid); |
| 1658 | else if (Backup_Method == BM_DD) |
| 1659 | return Backup_Image(part_settings); |
| 1660 | else if (Backup_Method == BM_FLASH_UTILS) |
| 1661 | return Backup_Dump_Image(part_settings); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1662 | LOGERR("Unknown backup method for '%s'\n", Mount_Point.c_str()); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1663 | return false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1664 | } |
| 1665 | |
James Christopher Adduono | 79ae093 | 2016-10-25 02:18:32 -0400 | [diff] [blame] | 1666 | bool TWPartition::Check_Restore_File_MD5(const string& Filename) { |
| 1667 | twrpDigest md5sum; |
| 1668 | |
| 1669 | md5sum.setfn(Filename); |
| 1670 | switch (md5sum.verify_md5digest()) { |
| 1671 | case MD5_OK: |
| 1672 | gui_msg(Msg("md5_matched=MD5 matched for '{1}'.")(Filename)); |
| 1673 | return true; |
| 1674 | case MD5_FILE_UNREADABLE: |
| 1675 | case MD5_NOT_FOUND: |
| 1676 | gui_msg(Msg(msg::kError, "no_md5_found=No md5 file found for '{1}'. Please unselect Enable MD5 verification to restore.")(Filename)); |
| 1677 | break; |
| 1678 | case MD5_MATCH_FAIL: |
| 1679 | gui_msg(Msg(msg::kError, "md5_fail_match=MD5 failed to match on '{1}'.")(Filename)); |
| 1680 | break; |
| 1681 | } |
| 1682 | return false; |
| 1683 | } |
| 1684 | |
bigbiff bigbiff | 8fd4b09 | 2016-08-30 20:48:53 -0400 | [diff] [blame] | 1685 | bool TWPartition::Check_MD5(PartitionSettings *part_settings) { |
James Christopher Adduono | 79ae093 | 2016-10-25 02:18:32 -0400 | [diff] [blame] | 1686 | string Full_Filename; |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 1687 | char split_filename[512]; |
| 1688 | int index = 0; |
| 1689 | |
Captain Throwback | ff5935f | 2014-09-23 16:08:34 -0400 | [diff] [blame] | 1690 | sync(); |
| 1691 | |
Ethan Yonker | dcf2b67 | 2016-09-13 14:41:53 -0500 | [diff] [blame] | 1692 | Full_Filename = part_settings->Backup_Folder + "/" + Backup_FileName; |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 1693 | if (!TWFunc::Path_Exists(Full_Filename)) { |
| 1694 | // This is a split archive, we presume |
James Christopher Adduono | 79ae093 | 2016-10-25 02:18:32 -0400 | [diff] [blame] | 1695 | memset(split_filename, 0, sizeof(split_filename)); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 1696 | while (index < 1000) { |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 1697 | sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index); |
James Christopher Adduono | 79ae093 | 2016-10-25 02:18:32 -0400 | [diff] [blame] | 1698 | if (!TWFunc::Path_Exists(split_filename)) |
| 1699 | break; |
| 1700 | LOGINFO("split_filename: %s\n", split_filename); |
| 1701 | if (!Check_Restore_File_MD5(split_filename)) |
| 1702 | return false; |
| 1703 | index++; |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 1704 | } |
Dees_Troy | 4a2a126 | 2012-09-18 09:33:47 -0400 | [diff] [blame] | 1705 | return true; |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 1706 | } |
James Christopher Adduono | 79ae093 | 2016-10-25 02:18:32 -0400 | [diff] [blame] | 1707 | return Check_Restore_File_MD5(Full_Filename); // Single file archive |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 1708 | } |
| 1709 | |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 1710 | bool TWPartition::Restore(PartitionSettings *part_settings) { |
Matt Mower | 3c36697 | 2015-12-25 19:28:31 -0600 | [diff] [blame] | 1711 | 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] | 1712 | 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] | 1713 | |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 1714 | string Restore_File_System = Get_Restore_File_System(part_settings); |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 1715 | |
| 1716 | if (Is_File_System(Restore_File_System)) |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 1717 | return Restore_Tar(part_settings); |
| 1718 | else if (Is_Image(Restore_File_System)) |
| 1719 | return Restore_Image(part_settings); |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 1720 | |
| 1721 | LOGERR("Unknown restore method for '%s'\n", Mount_Point.c_str()); |
| 1722 | return false; |
| 1723 | } |
| 1724 | |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 1725 | string TWPartition::Get_Restore_File_System(PartitionSettings *part_settings) { |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 1726 | size_t first_period, second_period; |
| 1727 | string Restore_File_System; |
| 1728 | |
Gary Peck | 43acadf | 2012-11-21 21:19:01 -0800 | [diff] [blame] | 1729 | // Parse backup filename to extract the file system before wiping |
Ethan Yonker | dcf2b67 | 2016-09-13 14:41:53 -0500 | [diff] [blame] | 1730 | first_period = Backup_FileName.find("."); |
Gary Peck | 43acadf | 2012-11-21 21:19:01 -0800 | [diff] [blame] | 1731 | if (first_period == string::npos) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1732 | LOGERR("Unable to find file system (first period).\n"); |
that | d43bf2d | 2014-09-21 23:13:02 +0200 | [diff] [blame] | 1733 | return string(); |
Gary Peck | 43acadf | 2012-11-21 21:19:01 -0800 | [diff] [blame] | 1734 | } |
Ethan Yonker | dcf2b67 | 2016-09-13 14:41:53 -0500 | [diff] [blame] | 1735 | 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] | 1736 | second_period = Restore_File_System.find("."); |
| 1737 | if (second_period == string::npos) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1738 | LOGERR("Unable to find file system (second period).\n"); |
that | d43bf2d | 2014-09-21 23:13:02 +0200 | [diff] [blame] | 1739 | return string(); |
Gary Peck | 43acadf | 2012-11-21 21:19:01 -0800 | [diff] [blame] | 1740 | } |
| 1741 | Restore_File_System.resize(second_period); |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 1742 | return Restore_File_System; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1743 | } |
| 1744 | |
| 1745 | string TWPartition::Backup_Method_By_Name() { |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 1746 | if (Backup_Method == BM_NONE) |
Dees_Troy | |