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