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