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