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