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