Dees Troy | 3be70a8 | 2013-10-22 14:25:12 +0000 | [diff] [blame] | 1 | /* |
bigbiff bigbiff | 56cf564 | 2016-08-19 17:43:45 -0400 | [diff] [blame] | 2 | Copyright 2013 to 2017 TeamWin |
Dees Troy | 3be70a8 | 2013-10-22 14:25:12 +0000 | [diff] [blame] | 3 | This file is part of TWRP/TeamWin Recovery Project. |
| 4 | |
| 5 | TWRP is free software: you can redistribute it and/or modify |
| 6 | it under the terms of the GNU General Public License as published by |
| 7 | the Free Software Foundation, either version 3 of the License, or |
| 8 | (at your option) any later version. |
| 9 | |
| 10 | TWRP is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | GNU General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU General Public License |
| 16 | along with TWRP. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 18 | |
| 19 | #include <stdio.h> |
| 20 | #include <stdlib.h> |
| 21 | #include <string.h> |
| 22 | #include <sys/stat.h> |
| 23 | #include <sys/vfs.h> |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 24 | #include <sys/mount.h> |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 25 | #include <unistd.h> |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 26 | #include <dirent.h> |
that | f54e539 | 2016-01-29 22:04:43 +0100 | [diff] [blame] | 27 | #include <libgen.h> |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 28 | #include <zlib.h> |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 29 | #include <iostream> |
| 30 | #include <sstream> |
Ethan Yonker | 66a1949 | 2015-12-10 10:19:45 -0600 | [diff] [blame] | 31 | #include <sys/param.h> |
Ethan Yonker | 472f506 | 2016-02-25 13:47:30 -0600 | [diff] [blame] | 32 | #include <fcntl.h> |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 33 | |
Dees_Troy | 657c309 | 2012-09-10 20:32:10 -0400 | [diff] [blame] | 34 | #ifdef TW_INCLUDE_CRYPTO |
| 35 | #include "cutils/properties.h" |
| 36 | #endif |
| 37 | |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 38 | #include "libblkid/include/blkid.h" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 39 | #include "variables.h" |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 40 | #include "twcommon.h" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 41 | #include "partitions.hpp" |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 42 | #include "data.hpp" |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 43 | #include "twrp-functions.hpp" |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 44 | #include "twrpTar.hpp" |
Ethan Yonker | 3fdcda4 | 2016-11-30 12:29:37 -0600 | [diff] [blame] | 45 | #include "exclude.hpp" |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 46 | #include "infomanager.hpp" |
Ethan Yonker | 4b94cfd | 2014-12-11 10:00:45 -0600 | [diff] [blame] | 47 | #include "set_metadata.h" |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 48 | #include "gui/gui.hpp" |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 49 | #include "adbbu/libtwadbbu.hpp" |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 50 | extern "C" { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 51 | #include "mtdutils/mtdutils.h" |
| 52 | #include "mtdutils/mounts.h" |
Dees_Troy | a95f55c | 2013-08-17 13:14:43 +0000 | [diff] [blame] | 53 | #ifdef USE_EXT4 |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 54 | // #include "make_ext4fs.h" TODO need ifdef for android8 |
| 55 | #include <ext4_utils/make_ext4fs.h> |
Dees_Troy | a95f55c | 2013-08-17 13:14:43 +0000 | [diff] [blame] | 56 | #endif |
Ethan Yonker | 71413f4 | 2014-02-26 13:36:08 -0600 | [diff] [blame] | 57 | |
| 58 | #ifdef TW_INCLUDE_CRYPTO |
Ethan Yonker | 253368a | 2014-11-25 15:00:52 -0600 | [diff] [blame] | 59 | #include "crypto/lollipop/cryptfs.h" |
Ethan Yonker | 66a1949 | 2015-12-10 10:19:45 -0600 | [diff] [blame] | 60 | #include "gpt/gpt.h" |
Ethan Yonker | bd7492d | 2016-12-07 13:55:01 -0600 | [diff] [blame] | 61 | #ifdef TW_INCLUDE_FBE |
| 62 | #include "crypto/ext4crypt/Decrypt.h" |
Ethan Yonker | bd7492d | 2016-12-07 13:55:01 -0600 | [diff] [blame] | 63 | #endif |
Ethan Yonker | bc85b63 | 2015-08-09 12:48:14 -0500 | [diff] [blame] | 64 | #else |
| 65 | #define CRYPT_FOOTER_OFFSET 0x4000 |
Ethan Yonker | 71413f4 | 2014-02-26 13:36:08 -0600 | [diff] [blame] | 66 | #endif |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 67 | } |
Matt Mower | 8741364 | 2017-01-17 21:14:46 -0600 | [diff] [blame] | 68 | #include <selinux/selinux.h> |
Ethan Yonker | f27497f | 2014-02-09 11:48:33 -0600 | [diff] [blame] | 69 | #include <selinux/label.h> |
Dees Troy | 4159aed | 2014-02-28 17:24:43 +0000 | [diff] [blame] | 70 | #ifdef HAVE_CAPABILITIES |
| 71 | #include <sys/capability.h> |
| 72 | #include <sys/xattr.h> |
| 73 | #include <linux/xattr.h> |
| 74 | #endif |
HashBang | ed974bb | 2016-01-30 14:20:09 -0500 | [diff] [blame] | 75 | #include <sparse_format.h> |
Ethan Yonker | 472f506 | 2016-02-25 13:47:30 -0600 | [diff] [blame] | 76 | #include "progresstracking.hpp" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 77 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 78 | using namespace std; |
| 79 | |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 80 | static int auto_index = 0; // v2 fstab allows you to specify a mount point of "auto" with no /. These items are given a mount point of /auto* where * == auto_index |
| 81 | |
Dees_Troy | a95f55c | 2013-08-17 13:14:43 +0000 | [diff] [blame] | 82 | extern struct selabel_handle *selinux_handle; |
Ethan Yonker | 6277c79 | 2014-09-15 14:54:30 -0500 | [diff] [blame] | 83 | extern bool datamedia; |
Dees_Troy | a95f55c | 2013-08-17 13:14:43 +0000 | [diff] [blame] | 84 | |
Hashcode | 62bd9e0 | 2013-11-19 21:59:42 -0800 | [diff] [blame] | 85 | struct flag_list { |
| 86 | const char *name; |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 87 | unsigned long flag; |
Hashcode | 62bd9e0 | 2013-11-19 21:59:42 -0800 | [diff] [blame] | 88 | }; |
| 89 | |
Matt Mower | 4ab42b1 | 2016-04-21 13:52:18 -0500 | [diff] [blame] | 90 | const struct flag_list mount_flags[] = { |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 91 | { "noatime", MS_NOATIME }, |
| 92 | { "noexec", MS_NOEXEC }, |
| 93 | { "nosuid", MS_NOSUID }, |
| 94 | { "nodev", MS_NODEV }, |
| 95 | { "nodiratime", MS_NODIRATIME }, |
| 96 | { "ro", MS_RDONLY }, |
| 97 | { "rw", 0 }, |
| 98 | { "remount", MS_REMOUNT }, |
| 99 | { "bind", MS_BIND }, |
| 100 | { "rec", MS_REC }, |
Dees Troy | c4bc30e | 2014-02-03 15:04:19 +0000 | [diff] [blame] | 101 | #ifdef MS_UNBINDABLE |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 102 | { "unbindable", MS_UNBINDABLE }, |
Dees Troy | c4bc30e | 2014-02-03 15:04:19 +0000 | [diff] [blame] | 103 | #endif |
| 104 | #ifdef MS_PRIVATE |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 105 | { "private", MS_PRIVATE }, |
Dees Troy | c4bc30e | 2014-02-03 15:04:19 +0000 | [diff] [blame] | 106 | #endif |
| 107 | #ifdef MS_SLAVE |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 108 | { "slave", MS_SLAVE }, |
Dees Troy | c4bc30e | 2014-02-03 15:04:19 +0000 | [diff] [blame] | 109 | #endif |
| 110 | #ifdef MS_SHARED |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 111 | { "shared", MS_SHARED }, |
Dees Troy | c4bc30e | 2014-02-03 15:04:19 +0000 | [diff] [blame] | 112 | #endif |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 113 | { "sync", MS_SYNCHRONOUS }, |
| 114 | { 0, 0 }, |
| 115 | }; |
| 116 | |
| 117 | const char *ignored_mount_items[] = { |
| 118 | "defaults=", |
| 119 | "errors=", |
| 120 | NULL |
Hashcode | 62bd9e0 | 2013-11-19 21:59:42 -0800 | [diff] [blame] | 121 | }; |
| 122 | |
Matt Mower | 2416a50 | 2016-04-12 19:54:46 -0500 | [diff] [blame] | 123 | enum TW_FSTAB_FLAGS { |
| 124 | TWFLAG_DEFAULTS, // Retain position |
| 125 | TWFLAG_ANDSEC, |
| 126 | TWFLAG_BACKUP, |
| 127 | TWFLAG_BACKUPNAME, |
| 128 | TWFLAG_BLOCKSIZE, |
| 129 | TWFLAG_CANBEWIPED, |
| 130 | TWFLAG_CANENCRYPTBACKUP, |
| 131 | TWFLAG_DISPLAY, |
| 132 | TWFLAG_ENCRYPTABLE, |
Ethan Yonker | e9afc3d | 2018-08-30 15:16:27 -0500 | [diff] [blame^] | 133 | TWFLAG_FILEENCRYPTION, |
Matt Mower | 2416a50 | 2016-04-12 19:54:46 -0500 | [diff] [blame] | 134 | TWFLAG_FLASHIMG, |
| 135 | TWFLAG_FORCEENCRYPT, |
| 136 | TWFLAG_FSFLAGS, |
| 137 | TWFLAG_IGNOREBLKID, |
| 138 | TWFLAG_LENGTH, |
| 139 | TWFLAG_MOUNTTODECRYPT, |
| 140 | TWFLAG_REMOVABLE, |
| 141 | TWFLAG_RETAINLAYOUTVERSION, |
| 142 | TWFLAG_SETTINGSSTORAGE, |
| 143 | TWFLAG_STORAGE, |
| 144 | TWFLAG_STORAGENAME, |
| 145 | TWFLAG_SUBPARTITIONOF, |
| 146 | TWFLAG_SYMLINK, |
| 147 | TWFLAG_USERDATAENCRYPTBACKUP, |
| 148 | TWFLAG_USERMRF, |
| 149 | TWFLAG_WIPEDURINGFACTORYRESET, |
| 150 | TWFLAG_WIPEINGUI, |
Ethan Yonker | 1b19016 | 2016-12-05 15:25:19 -0600 | [diff] [blame] | 151 | TWFLAG_SLOTSELECT, |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 152 | TWFLAG_WAIT, |
| 153 | TWFLAG_VERIFY, |
| 154 | TWFLAG_CHECK, |
| 155 | TWFLAG_ALTDEVICE, |
| 156 | TWFLAG_NOTRIM, |
| 157 | TWFLAG_VOLDMANAGED, |
| 158 | TWFLAG_FORMATTABLE, |
| 159 | TWFLAG_RESIZE, |
Matt Mower | 2416a50 | 2016-04-12 19:54:46 -0500 | [diff] [blame] | 160 | }; |
| 161 | |
| 162 | /* Flags without a trailing '=' are considered dual format flags and can be |
| 163 | * written as either 'flagname' or 'flagname=', where the character following |
| 164 | * the '=' is Y,y,1 for true and false otherwise. |
| 165 | */ |
| 166 | const struct flag_list tw_flags[] = { |
| 167 | { "andsec", TWFLAG_ANDSEC }, |
| 168 | { "backup", TWFLAG_BACKUP }, |
| 169 | { "backupname=", TWFLAG_BACKUPNAME }, |
| 170 | { "blocksize=", TWFLAG_BLOCKSIZE }, |
| 171 | { "canbewiped", TWFLAG_CANBEWIPED }, |
| 172 | { "canencryptbackup", TWFLAG_CANENCRYPTBACKUP }, |
| 173 | { "defaults", TWFLAG_DEFAULTS }, |
| 174 | { "display=", TWFLAG_DISPLAY }, |
| 175 | { "encryptable=", TWFLAG_ENCRYPTABLE }, |
Ethan Yonker | e9afc3d | 2018-08-30 15:16:27 -0500 | [diff] [blame^] | 176 | { "fileencryption=", TWFLAG_FILEENCRYPTION }, |
Matt Mower | 2416a50 | 2016-04-12 19:54:46 -0500 | [diff] [blame] | 177 | { "flashimg", TWFLAG_FLASHIMG }, |
| 178 | { "forceencrypt=", TWFLAG_FORCEENCRYPT }, |
| 179 | { "fsflags=", TWFLAG_FSFLAGS }, |
| 180 | { "ignoreblkid", TWFLAG_IGNOREBLKID }, |
| 181 | { "length=", TWFLAG_LENGTH }, |
| 182 | { "mounttodecrypt", TWFLAG_MOUNTTODECRYPT }, |
| 183 | { "removable", TWFLAG_REMOVABLE }, |
| 184 | { "retainlayoutversion", TWFLAG_RETAINLAYOUTVERSION }, |
| 185 | { "settingsstorage", TWFLAG_SETTINGSSTORAGE }, |
| 186 | { "storage", TWFLAG_STORAGE }, |
| 187 | { "storagename=", TWFLAG_STORAGENAME }, |
| 188 | { "subpartitionof=", TWFLAG_SUBPARTITIONOF }, |
| 189 | { "symlink=", TWFLAG_SYMLINK }, |
| 190 | { "userdataencryptbackup", TWFLAG_USERDATAENCRYPTBACKUP }, |
| 191 | { "usermrf", TWFLAG_USERMRF }, |
| 192 | { "wipeduringfactoryreset", TWFLAG_WIPEDURINGFACTORYRESET }, |
| 193 | { "wipeingui", TWFLAG_WIPEINGUI }, |
Ethan Yonker | 1b19016 | 2016-12-05 15:25:19 -0600 | [diff] [blame] | 194 | { "slotselect", TWFLAG_SLOTSELECT }, |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 195 | { "wait", TWFLAG_WAIT }, |
| 196 | { "verify", TWFLAG_VERIFY }, |
| 197 | { "check", TWFLAG_CHECK }, |
| 198 | { "altdevice", TWFLAG_ALTDEVICE }, |
| 199 | { "notrim", TWFLAG_NOTRIM }, |
| 200 | { "voldmanaged=", TWFLAG_VOLDMANAGED }, |
| 201 | { "formattable", TWFLAG_FORMATTABLE }, |
| 202 | { "resize", TWFLAG_RESIZE }, |
Matt Mower | 2416a50 | 2016-04-12 19:54:46 -0500 | [diff] [blame] | 203 | { 0, 0 }, |
| 204 | }; |
| 205 | |
that | 9e0593e | 2014-10-08 00:01:24 +0200 | [diff] [blame] | 206 | TWPartition::TWPartition() { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 207 | Can_Be_Mounted = false; |
| 208 | Can_Be_Wiped = false; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 209 | Can_Be_Backed_Up = false; |
Vojtech Bocek | 1dc3098 | 2013-08-30 21:49:30 +0200 | [diff] [blame] | 210 | Use_Rm_Rf = false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 211 | Wipe_During_Factory_Reset = false; |
| 212 | Wipe_Available_in_GUI = false; |
| 213 | Is_SubPartition = false; |
Dees_Troy | 2691f9d | 2012-09-24 11:15:49 -0400 | [diff] [blame] | 214 | Has_SubPartition = false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 215 | SubPartition_Of = ""; |
| 216 | Symlink_Path = ""; |
| 217 | Symlink_Mount_Point = ""; |
| 218 | Mount_Point = ""; |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 219 | Backup_Path = ""; |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 220 | Wildcard_Block_Device = false; |
| 221 | Sysfs_Entry = ""; |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 222 | Actual_Block_Device = ""; |
| 223 | Primary_Block_Device = ""; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 224 | Alternate_Block_Device = ""; |
| 225 | Removable = false; |
| 226 | Is_Present = false; |
| 227 | Length = 0; |
| 228 | Size = 0; |
| 229 | Used = 0; |
| 230 | Free = 0; |
| 231 | Backup_Size = 0; |
| 232 | Can_Be_Encrypted = false; |
| 233 | Is_Encrypted = false; |
| 234 | Is_Decrypted = false; |
Ethan Yonker | bd7492d | 2016-12-07 13:55:01 -0600 | [diff] [blame] | 235 | Is_FBE = false; |
Ethan Yonker | 253368a | 2014-11-25 15:00:52 -0600 | [diff] [blame] | 236 | Mount_To_Decrypt = false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 237 | Decrypted_Block_Device = ""; |
| 238 | Display_Name = ""; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 239 | Backup_Display_Name = ""; |
| 240 | Storage_Name = ""; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 241 | Backup_Name = ""; |
Dees_Troy | 63c8df7 | 2012-09-10 14:02:05 -0400 | [diff] [blame] | 242 | Backup_FileName = ""; |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 243 | MTD_Name = ""; |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 244 | Backup_Method = BM_NONE; |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 245 | Can_Encrypt_Backup = false; |
| 246 | Use_Userdata_Encryption = false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 247 | Has_Data_Media = false; |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 248 | Has_Android_Secure = false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 249 | Is_Storage = false; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 250 | Is_Settings_Storage = false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 251 | Storage_Path = ""; |
| 252 | Current_File_System = ""; |
| 253 | Fstab_File_System = ""; |
Hashcode | 62bd9e0 | 2013-11-19 21:59:42 -0800 | [diff] [blame] | 254 | Mount_Flags = 0; |
| 255 | Mount_Options = ""; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 256 | Format_Block_Size = 0; |
Dees_Troy | 68cab49 | 2012-12-12 19:29:35 +0000 | [diff] [blame] | 257 | Ignore_Blkid = false; |
Dees_Troy | 16c2b31 | 2013-01-15 16:51:18 +0000 | [diff] [blame] | 258 | Retain_Layout_Version = false; |
Ethan Yonker | 253368a | 2014-11-25 15:00:52 -0600 | [diff] [blame] | 259 | Crypto_Key_Location = "footer"; |
Ethan Yonker | 726a020 | 2014-12-16 20:01:38 -0600 | [diff] [blame] | 260 | MTP_Storage_ID = 0; |
Ethan Yonker | 96af84a | 2015-01-05 14:58:36 -0600 | [diff] [blame] | 261 | Can_Flash_Img = false; |
Ethan Yonker | eb32b1f | 2015-05-18 10:23:03 -0500 | [diff] [blame] | 262 | Mount_Read_Only = false; |
Ethan Yonker | 66a1949 | 2015-12-10 10:19:45 -0600 | [diff] [blame] | 263 | Is_Adopted_Storage = false; |
| 264 | Adopted_GUID = ""; |
Ethan Yonker | 1b19016 | 2016-12-05 15:25:19 -0600 | [diff] [blame] | 265 | SlotSelect = false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | TWPartition::~TWPartition(void) { |
| 269 | // Do nothing |
| 270 | } |
| 271 | |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 272 | bool TWPartition::Process_Fstab_Line(const char *fstab_line, bool Display_Error, std::map<string, Flags_Map> *twrp_flags) { |
Matt Mower | 2b2dd15 | 2016-04-26 11:24:08 -0500 | [diff] [blame] | 273 | char full_line[MAX_FSTAB_LINE_LENGTH]; |
Matt Mower | 2416a50 | 2016-04-12 19:54:46 -0500 | [diff] [blame] | 274 | char twflags[MAX_FSTAB_LINE_LENGTH] = ""; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 275 | char* ptr; |
Matt Mower | 2b2dd15 | 2016-04-26 11:24:08 -0500 | [diff] [blame] | 276 | int line_len = strlen(fstab_line), index = 0, item_index = 0; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 277 | bool skip = false; |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 278 | int fstab_version = 1, mount_point_index = 0, fs_index = 1, block_device_index = 2; |
| 279 | TWPartition *additional_entry = NULL; |
| 280 | std::map<string, Flags_Map>::iterator it; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 281 | |
Matt Mower | 2b2dd15 | 2016-04-26 11:24:08 -0500 | [diff] [blame] | 282 | strlcpy(full_line, fstab_line, sizeof(full_line)); |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 283 | for (index = 0; index < line_len; index++) { |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 284 | if (full_line[index] == 34) |
| 285 | skip = !skip; |
| 286 | if (!skip && full_line[index] <= 32) |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 287 | full_line[index] = '\0'; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 288 | } |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 289 | if (line_len < 10) |
| 290 | return false; // There can't possibly be a valid fstab line that is less than 10 chars |
| 291 | if (strncmp(fstab_line, "/dev/", strlen("/dev/")) == 0 || strncmp(fstab_line, "/devices/", strlen("/devices/")) == 0) { |
| 292 | fstab_version = 2; |
| 293 | block_device_index = 0; |
| 294 | mount_point_index = 1; |
| 295 | fs_index = 2; |
| 296 | } |
| 297 | |
| 298 | index = 0; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 299 | while (index < line_len) { |
| 300 | while (index < line_len && full_line[index] == '\0') |
| 301 | index++; |
| 302 | if (index >= line_len) |
| 303 | continue; |
| 304 | ptr = full_line + index; |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 305 | if (item_index == mount_point_index) { |
| 306 | Mount_Point = ptr; |
| 307 | if (fstab_version == 2) { |
| 308 | additional_entry = PartitionManager.Find_Partition_By_Path(Mount_Point); |
| 309 | if (additional_entry) { |
| 310 | LOGINFO("Found an additional entry for '%s'\n", Mount_Point.c_str()); |
| 311 | } |
| 312 | } |
| 313 | LOGINFO("Processing '%s'\n", Mount_Point.c_str()); |
| 314 | Backup_Path = Mount_Point; |
| 315 | Storage_Path = Mount_Point; |
| 316 | Display_Name = ptr + 1; |
| 317 | Backup_Display_Name = Display_Name; |
| 318 | Storage_Name = Display_Name; |
| 319 | item_index++; |
| 320 | } else if (item_index == fs_index) { |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 321 | // File System |
| 322 | Fstab_File_System = ptr; |
| 323 | Current_File_System = ptr; |
| 324 | item_index++; |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 325 | } else if (item_index == block_device_index) { |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 326 | // Primary Block Device |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 327 | if (Fstab_File_System == "mtd" || Fstab_File_System == "yaffs2") { |
Dees_Troy | 094207a | 2012-09-26 12:00:39 -0400 | [diff] [blame] | 328 | MTD_Name = ptr; |
| 329 | Find_MTD_Block_Device(MTD_Name); |
Dees_Troy | 5fcd8f9 | 2012-10-16 12:22:05 -0400 | [diff] [blame] | 330 | } else if (Fstab_File_System == "bml") { |
| 331 | if (Mount_Point == "/boot") |
| 332 | MTD_Name = "boot"; |
| 333 | else if (Mount_Point == "/recovery") |
| 334 | MTD_Name = "recovery"; |
| 335 | Primary_Block_Device = ptr; |
| 336 | if (*ptr != '/') |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 337 | 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] | 338 | } else if (*ptr != '/') { |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 339 | if (Display_Error) |
Matt Mower | 2b2dd15 | 2016-04-26 11:24:08 -0500 | [diff] [blame] | 340 | LOGERR("Invalid block device '%s' in fstab line '%s'", ptr, fstab_line); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 341 | else |
Matt Mower | 2b2dd15 | 2016-04-26 11:24:08 -0500 | [diff] [blame] | 342 | LOGINFO("Invalid block device '%s' in fstab line '%s'", ptr, fstab_line); |
Matt Mower | 72c87ce | 2016-04-26 14:34:56 -0500 | [diff] [blame] | 343 | return false; |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 344 | } else { |
| 345 | Primary_Block_Device = ptr; |
| 346 | Find_Real_Block_Device(Primary_Block_Device, Display_Error); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 347 | } |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 348 | item_index++; |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 349 | } else if (item_index > 2) { |
| 350 | if (fstab_version == 2) { |
| 351 | if (item_index == 3) { |
| 352 | Process_FS_Flags(ptr); |
| 353 | if (additional_entry) { |
| 354 | additional_entry->Save_FS_Flags(Fstab_File_System, Mount_Flags, Mount_Options); |
| 355 | return false; // We save the extra fs flags in the other partition entry and by returning false, this entry will be deleted |
| 356 | } |
| 357 | } else { |
| 358 | strlcpy(twflags, ptr, sizeof(twflags)); |
| 359 | } |
| 360 | item_index++; |
| 361 | } else if (*ptr == '/') { // v2 fstab does not allow alternate block devices |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 362 | // Alternate Block Device |
| 363 | Alternate_Block_Device = ptr; |
| 364 | Find_Real_Block_Device(Alternate_Block_Device, Display_Error); |
| 365 | } else if (strlen(ptr) > 7 && strncmp(ptr, "length=", 7) == 0) { |
| 366 | // Partition length |
| 367 | ptr += 7; |
| 368 | Length = atoi(ptr); |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 369 | } else if (strlen(ptr) > 6 && strncmp(ptr, "flags=", 6) == 0) { |
| 370 | // Custom flags, save for later so that new values aren't overwritten by defaults |
| 371 | ptr += 6; |
Matt Mower | 2416a50 | 2016-04-12 19:54:46 -0500 | [diff] [blame] | 372 | strlcpy(twflags, ptr, sizeof(twflags)); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 373 | } else if (strlen(ptr) == 4 && (strncmp(ptr, "NULL", 4) == 0 || strncmp(ptr, "null", 4) == 0 || strncmp(ptr, "null", 4) == 0)) { |
| 374 | // Do nothing |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 375 | } else { |
| 376 | // Unhandled data |
Matt Mower | 2b2dd15 | 2016-04-26 11:24:08 -0500 | [diff] [blame] | 377 | 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] | 378 | } |
| 379 | } |
| 380 | while (index < line_len && full_line[index] != '\0') |
| 381 | index++; |
| 382 | } |
| 383 | |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 384 | // override block devices from the v2 fstab with the ones we read from the twrp.flags file in case they are different |
| 385 | if (fstab_version == 2 && twrp_flags && twrp_flags->size() > 0) { |
| 386 | it = twrp_flags->find(Mount_Point); |
| 387 | if (it != twrp_flags->end()) { |
| 388 | if (!it->second.Primary_Block_Device.empty()) { |
| 389 | Primary_Block_Device = it->second.Primary_Block_Device; |
| 390 | Find_Real_Block_Device(Primary_Block_Device, Display_Error); |
| 391 | } |
| 392 | if (!it->second.Alternate_Block_Device.empty()) { |
| 393 | Alternate_Block_Device = it->second.Alternate_Block_Device; |
| 394 | Find_Real_Block_Device(Alternate_Block_Device, Display_Error); |
| 395 | } |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | if (strncmp(fstab_line, "/devices/", strlen("/devices/")) == 0) { |
| 400 | Sysfs_Entry = Primary_Block_Device; |
| 401 | Primary_Block_Device = ""; |
| 402 | Is_Storage = true; |
| 403 | Removable = true; |
| 404 | Wipe_Available_in_GUI = true; |
| 405 | Wildcard_Block_Device = true; |
| 406 | } |
| 407 | if (Primary_Block_Device.find("*") != string::npos) |
| 408 | Wildcard_Block_Device = true; |
| 409 | |
| 410 | if (Mount_Point == "auto") { |
| 411 | Mount_Point = "/auto"; |
| 412 | char autoi[5]; |
| 413 | sprintf(autoi, "%i", auto_index); |
| 414 | Mount_Point += autoi; |
| 415 | Backup_Path = Mount_Point; |
| 416 | Storage_Path = Mount_Point; |
| 417 | auto_index++; |
| 418 | Setup_File_System(Display_Error); |
| 419 | Display_Name = "Storage"; |
| 420 | Backup_Display_Name = Display_Name; |
| 421 | Storage_Name = Display_Name; |
| 422 | Can_Be_Backed_Up = false; |
| 423 | Wipe_Available_in_GUI = true; |
| 424 | Is_Storage = true; |
| 425 | Removable = true; |
| 426 | Wipe_Available_in_GUI = true; |
| 427 | } else if (!Is_File_System(Fstab_File_System) && !Is_Image(Fstab_File_System)) { |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 428 | if (Display_Error) |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 429 | LOGERR("Unknown File System: '%s'\n", Fstab_File_System.c_str()); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 430 | else |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 431 | LOGINFO("Unknown File System: '%s'\n", Fstab_File_System.c_str()); |
Matt Mower | 72c87ce | 2016-04-26 14:34:56 -0500 | [diff] [blame] | 432 | return false; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 433 | } else if (Is_File_System(Fstab_File_System)) { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 434 | Find_Actual_Block_Device(); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 435 | Setup_File_System(Display_Error); |
| 436 | if (Mount_Point == "/system") { |
| 437 | Display_Name = "System"; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 438 | Backup_Display_Name = Display_Name; |
| 439 | Storage_Name = Display_Name; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 440 | Wipe_Available_in_GUI = true; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 441 | Can_Be_Backed_Up = true; |
Ethan Yonker | eb32b1f | 2015-05-18 10:23:03 -0500 | [diff] [blame] | 442 | Mount_Read_Only = true; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 443 | } else if (Mount_Point == "/data") { |
| 444 | Display_Name = "Data"; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 445 | Backup_Display_Name = Display_Name; |
| 446 | Storage_Name = Display_Name; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 447 | Wipe_Available_in_GUI = true; |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 448 | Wipe_During_Factory_Reset = true; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 449 | Can_Be_Backed_Up = true; |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 450 | Can_Encrypt_Backup = true; |
| 451 | Use_Userdata_Encryption = true; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 452 | } else if (Mount_Point == "/cache") { |
| 453 | Display_Name = "Cache"; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 454 | Backup_Display_Name = Display_Name; |
| 455 | Storage_Name = Display_Name; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 456 | Wipe_Available_in_GUI = true; |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 457 | Wipe_During_Factory_Reset = true; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 458 | Can_Be_Backed_Up = true; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 459 | } else if (Mount_Point == "/datadata") { |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 460 | Wipe_During_Factory_Reset = true; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 461 | Display_Name = "DataData"; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 462 | Backup_Display_Name = Display_Name; |
| 463 | Storage_Name = Display_Name; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 464 | Is_SubPartition = true; |
| 465 | SubPartition_Of = "/data"; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 466 | DataManager::SetValue(TW_HAS_DATADATA, 1); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 467 | Can_Be_Backed_Up = true; |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 468 | Can_Encrypt_Backup = true; |
| 469 | Use_Userdata_Encryption = false; // This whole partition should be encrypted |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 470 | } else if (Mount_Point == "/sd-ext") { |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 471 | Wipe_During_Factory_Reset = true; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 472 | Display_Name = "SD-Ext"; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 473 | Backup_Display_Name = Display_Name; |
| 474 | Storage_Name = Display_Name; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 475 | Wipe_Available_in_GUI = true; |
Dees_Troy | c51f1f9 | 2012-09-20 15:32:13 -0400 | [diff] [blame] | 476 | Removable = true; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 477 | Can_Be_Backed_Up = true; |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 478 | Can_Encrypt_Backup = true; |
| 479 | Use_Userdata_Encryption = true; |
Dees_Troy | 2c50e18 | 2012-09-26 20:05:28 -0400 | [diff] [blame] | 480 | } else if (Mount_Point == "/boot") { |
| 481 | Display_Name = "Boot"; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 482 | Backup_Display_Name = Display_Name; |
Dees_Troy | 2c50e18 | 2012-09-26 20:05:28 -0400 | [diff] [blame] | 483 | DataManager::SetValue("tw_boot_is_mountable", 1); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 484 | Can_Be_Backed_Up = true; |
Ethan Yonker | 1673e3d | 2015-10-26 21:51:58 -0500 | [diff] [blame] | 485 | } else if (Mount_Point == "/vendor") { |
| 486 | Display_Name = "Vendor"; |
| 487 | Backup_Display_Name = Display_Name; |
| 488 | Storage_Name = Display_Name; |
| 489 | Mount_Read_Only = true; |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 490 | } |
| 491 | #ifdef TW_EXTERNAL_STORAGE_PATH |
| 492 | if (Mount_Point == EXPAND(TW_EXTERNAL_STORAGE_PATH)) { |
| 493 | Is_Storage = true; |
| 494 | Storage_Path = EXPAND(TW_EXTERNAL_STORAGE_PATH); |
Dees_Troy | c51f1f9 | 2012-09-20 15:32:13 -0400 | [diff] [blame] | 495 | Removable = true; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 496 | Wipe_Available_in_GUI = true; |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 497 | #else |
Dees_Troy | 70737fa | 2013-04-08 13:19:20 +0000 | [diff] [blame] | 498 | if (Mount_Point == "/sdcard" || Mount_Point == "/external_sd" || Mount_Point == "/external_sdcard") { |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 499 | Is_Storage = true; |
Dees_Troy | c51f1f9 | 2012-09-20 15:32:13 -0400 | [diff] [blame] | 500 | Removable = true; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 501 | Wipe_Available_in_GUI = true; |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 502 | #endif |
Dees_Troy | b05ddee | 2013-01-28 20:24:50 +0000 | [diff] [blame] | 503 | } |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 504 | #ifdef TW_INTERNAL_STORAGE_PATH |
| 505 | if (Mount_Point == EXPAND(TW_INTERNAL_STORAGE_PATH)) { |
| 506 | Is_Storage = true; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 507 | Is_Settings_Storage = true; |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 508 | Storage_Path = EXPAND(TW_INTERNAL_STORAGE_PATH); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 509 | Wipe_Available_in_GUI = true; |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 510 | } |
| 511 | #else |
Dees_Troy | 70737fa | 2013-04-08 13:19:20 +0000 | [diff] [blame] | 512 | if (Mount_Point == "/emmc" || Mount_Point == "/internal_sd" || Mount_Point == "/internal_sdcard") { |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 513 | Is_Storage = true; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 514 | Is_Settings_Storage = true; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 515 | Wipe_Available_in_GUI = true; |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 516 | } |
| 517 | #endif |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 518 | } else if (Is_Image(Fstab_File_System)) { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 519 | Find_Actual_Block_Device(); |
Ethan Yonker | 1b19016 | 2016-12-05 15:25:19 -0600 | [diff] [blame] | 520 | Setup_Image(); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 521 | if (Mount_Point == "/boot") { |
| 522 | Display_Name = "Boot"; |
| 523 | Backup_Display_Name = Display_Name; |
| 524 | Can_Be_Backed_Up = true; |
Ethan Yonker | 96af84a | 2015-01-05 14:58:36 -0600 | [diff] [blame] | 525 | Can_Flash_Img = true; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 526 | } else if (Mount_Point == "/recovery") { |
| 527 | Display_Name = "Recovery"; |
| 528 | Backup_Display_Name = Display_Name; |
Ethan Yonker | 96af84a | 2015-01-05 14:58:36 -0600 | [diff] [blame] | 529 | Can_Flash_Img = true; |
Ethan Yonker | eb32b1f | 2015-05-18 10:23:03 -0500 | [diff] [blame] | 530 | } else if (Mount_Point == "/system_image") { |
| 531 | Display_Name = "System Image"; |
| 532 | Backup_Display_Name = Display_Name; |
HashBang | ed974bb | 2016-01-30 14:20:09 -0500 | [diff] [blame] | 533 | Can_Flash_Img = true; |
Ethan Yonker | eb32b1f | 2015-05-18 10:23:03 -0500 | [diff] [blame] | 534 | Can_Be_Backed_Up = true; |
Ethan Yonker | 1673e3d | 2015-10-26 21:51:58 -0500 | [diff] [blame] | 535 | } else if (Mount_Point == "/vendor_image") { |
| 536 | Display_Name = "Vendor Image"; |
| 537 | Backup_Display_Name = Display_Name; |
HashBang | ed974bb | 2016-01-30 14:20:09 -0500 | [diff] [blame] | 538 | Can_Flash_Img = true; |
Ethan Yonker | 1673e3d | 2015-10-26 21:51:58 -0500 | [diff] [blame] | 539 | Can_Be_Backed_Up = true; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 540 | } |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 541 | } |
| 542 | |
Matt Mower | 2416a50 | 2016-04-12 19:54:46 -0500 | [diff] [blame] | 543 | // Process TWRP fstab flags |
| 544 | if (strlen(twflags) > 0) { |
| 545 | string Prev_Display_Name = Display_Name; |
| 546 | string Prev_Storage_Name = Storage_Name; |
| 547 | string Prev_Backup_Display_Name = Backup_Display_Name; |
| 548 | Display_Name = ""; |
| 549 | Storage_Name = ""; |
| 550 | Backup_Display_Name = ""; |
| 551 | |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 552 | Process_TW_Flags(twflags, (fstab_version == 1), fstab_version); |
| 553 | Save_FS_Flags(Fstab_File_System, Mount_Flags, Mount_Options); |
Matt Mower | 2416a50 | 2016-04-12 19:54:46 -0500 | [diff] [blame] | 554 | |
| 555 | bool has_display_name = !Display_Name.empty(); |
| 556 | bool has_storage_name = !Storage_Name.empty(); |
| 557 | bool has_backup_name = !Backup_Display_Name.empty(); |
| 558 | if (!has_display_name) Display_Name = Prev_Display_Name; |
| 559 | if (!has_storage_name) Storage_Name = Prev_Storage_Name; |
| 560 | if (!has_backup_name) Backup_Display_Name = Prev_Backup_Display_Name; |
| 561 | |
| 562 | if (has_display_name && !has_storage_name) |
| 563 | Storage_Name = Display_Name; |
| 564 | if (!has_display_name && has_storage_name) |
| 565 | Display_Name = Storage_Name; |
| 566 | if (has_display_name && !has_backup_name && Backup_Display_Name != "Android Secure") |
| 567 | Backup_Display_Name = Display_Name; |
| 568 | if (!has_display_name && has_backup_name) |
| 569 | Display_Name = Backup_Display_Name; |
| 570 | } |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 571 | |
| 572 | if (fstab_version == 2 && twrp_flags && twrp_flags->size() > 0) { |
| 573 | it = twrp_flags->find(Mount_Point); |
| 574 | if (it != twrp_flags->end()) { |
| 575 | char twrpflags[MAX_FSTAB_LINE_LENGTH] = ""; |
| 576 | int skip = 0; |
| 577 | string Flags = it->second.Flags; |
| 578 | strcpy(twrpflags, Flags.c_str()); |
| 579 | if (strlen(twrpflags) > strlen("flags=") && strncmp(twrpflags, "flags=", strlen("flags=")) == 0) |
| 580 | skip += strlen("flags="); |
| 581 | char* flagptr = twrpflags; |
| 582 | flagptr += skip; |
| 583 | Process_TW_Flags(flagptr, Display_Error, 1); // Forcing the fstab to ver 1 because this data is coming from the /etc/twrp.flags which should be using the TWRP v1 flags format |
| 584 | } |
| 585 | } |
nkk71 | 98fc399 | 2017-12-16 16:26:42 +0200 | [diff] [blame] | 586 | |
| 587 | if (Mount_Point == "/persist" && Can_Be_Mounted) { |
| 588 | bool mounted = Is_Mounted(); |
| 589 | if (mounted || Mount(false)) { |
| 590 | // Read the backup settings file |
| 591 | DataManager::LoadPersistValues(); |
| 592 | TWFunc::Fixup_Time_On_Boot("/persist/time/"); |
| 593 | if (!mounted) |
| 594 | UnMount(false); |
| 595 | } |
| 596 | } |
| 597 | |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 598 | return true; |
| 599 | } |
| 600 | |
Matt Mower | 72c87ce | 2016-04-26 14:34:56 -0500 | [diff] [blame] | 601 | void TWPartition::Partition_Post_Processing(bool Display_Error) { |
| 602 | if (Mount_Point == "/data") |
| 603 | Setup_Data_Partition(Display_Error); |
| 604 | else if (Mount_Point == "/cache") |
| 605 | Setup_Cache_Partition(Display_Error); |
| 606 | } |
| 607 | |
Ethan Yonker | bd7492d | 2016-12-07 13:55:01 -0600 | [diff] [blame] | 608 | void TWPartition::ExcludeAll(const string& path) { |
| 609 | backup_exclusions.add_absolute_dir(path); |
| 610 | wipe_exclusions.add_absolute_dir(path); |
| 611 | } |
| 612 | |
Matt Mower | 72c87ce | 2016-04-26 14:34:56 -0500 | [diff] [blame] | 613 | void TWPartition::Setup_Data_Partition(bool Display_Error) { |
| 614 | if (Mount_Point != "/data") |
| 615 | return; |
| 616 | |
| 617 | // Ensure /data is not mounted as tmpfs for qcom hardware decrypt |
| 618 | UnMount(false); |
| 619 | |
| 620 | #ifdef TW_INCLUDE_CRYPTO |
| 621 | if (datamedia) |
| 622 | Setup_Data_Media(); |
| 623 | Can_Be_Encrypted = true; |
| 624 | char crypto_blkdev[255]; |
| 625 | property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error"); |
| 626 | if (strcmp(crypto_blkdev, "error") != 0) { |
| 627 | DataManager::SetValue(TW_IS_DECRYPTED, 1); |
| 628 | Is_Encrypted = true; |
| 629 | Is_Decrypted = true; |
Ethan Yonker | bd7492d | 2016-12-07 13:55:01 -0600 | [diff] [blame] | 630 | Is_FBE = false; |
| 631 | DataManager::SetValue(TW_IS_FBE, 0); |
Matt Mower | 72c87ce | 2016-04-26 14:34:56 -0500 | [diff] [blame] | 632 | Decrypted_Block_Device = crypto_blkdev; |
| 633 | LOGINFO("Data already decrypted, new block device: '%s'\n", crypto_blkdev); |
| 634 | } else if (!Mount(false)) { |
| 635 | if (Is_Present) { |
| 636 | set_partition_data(Actual_Block_Device.c_str(), Crypto_Key_Location.c_str(), Fstab_File_System.c_str()); |
| 637 | if (cryptfs_check_footer() == 0) { |
| 638 | Is_Encrypted = true; |
| 639 | Is_Decrypted = false; |
| 640 | Can_Be_Mounted = false; |
| 641 | Current_File_System = "emmc"; |
Ethan Yonker | 1b19016 | 2016-12-05 15:25:19 -0600 | [diff] [blame] | 642 | Setup_Image(); |
Matt Mower | 72c87ce | 2016-04-26 14:34:56 -0500 | [diff] [blame] | 643 | DataManager::SetValue(TW_IS_ENCRYPTED, 1); |
| 644 | DataManager::SetValue(TW_CRYPTO_PWTYPE, cryptfs_get_password_type()); |
| 645 | DataManager::SetValue(TW_CRYPTO_PASSWORD, ""); |
| 646 | DataManager::SetValue("tw_crypto_display", ""); |
| 647 | } else { |
| 648 | gui_err("mount_data_footer=Could not mount /data and unable to find crypto footer."); |
| 649 | } |
| 650 | } else { |
| 651 | LOGERR("Primary block device '%s' for mount point '%s' is not present!\n", Primary_Block_Device.c_str(), Mount_Point.c_str()); |
| 652 | } |
| 653 | } else { |
Ethan Yonker | bd7492d | 2016-12-07 13:55:01 -0600 | [diff] [blame] | 654 | if (TWFunc::Path_Exists("/data/unencrypted/key/version")) { |
| 655 | LOGINFO("File Based Encryption is present\n"); |
| 656 | #ifdef TW_INCLUDE_FBE |
| 657 | ExcludeAll(Mount_Point + "/convert_fbe"); |
| 658 | ExcludeAll(Mount_Point + "/unencrypted"); |
| 659 | //ExcludeAll(Mount_Point + "/system/users/0"); // we WILL need to retain some of this if multiple users are present or we just need to delete more folders for the extra users somewhere else |
| 660 | ExcludeAll(Mount_Point + "/misc/vold/user_keys"); |
Ethan Yonker | 79f88bd | 2016-12-09 14:52:12 -0600 | [diff] [blame] | 661 | //ExcludeAll(Mount_Point + "/system_ce"); |
| 662 | //ExcludeAll(Mount_Point + "/system_de"); |
| 663 | //ExcludeAll(Mount_Point + "/misc_ce"); |
| 664 | //ExcludeAll(Mount_Point + "/misc_de"); |
Ethan Yonker | bd7492d | 2016-12-07 13:55:01 -0600 | [diff] [blame] | 665 | ExcludeAll(Mount_Point + "/system/gatekeeper.password.key"); |
| 666 | ExcludeAll(Mount_Point + "/system/gatekeeper.pattern.key"); |
| 667 | ExcludeAll(Mount_Point + "/system/locksettings.db"); |
| 668 | //ExcludeAll(Mount_Point + "/system/locksettings.db-shm"); // don't seem to need this one, but the other 2 are needed |
| 669 | ExcludeAll(Mount_Point + "/system/locksettings.db-wal"); |
Ethan Yonker | 79f88bd | 2016-12-09 14:52:12 -0600 | [diff] [blame] | 670 | //ExcludeAll(Mount_Point + "/user_de"); |
Ethan Yonker | bd7492d | 2016-12-07 13:55:01 -0600 | [diff] [blame] | 671 | //ExcludeAll(Mount_Point + "/misc/profiles/cur/0"); // might be important later |
| 672 | ExcludeAll(Mount_Point + "/misc/gatekeeper"); |
Ethan Yonker | fefe591 | 2017-09-30 22:22:13 -0500 | [diff] [blame] | 673 | ExcludeAll(Mount_Point + "/misc/keystore"); |
Ethan Yonker | bd7492d | 2016-12-07 13:55:01 -0600 | [diff] [blame] | 674 | ExcludeAll(Mount_Point + "/drm/kek.dat"); |
Ethan Yonker | fefe591 | 2017-09-30 22:22:13 -0500 | [diff] [blame] | 675 | ExcludeAll(Mount_Point + "/system_de/0/spblob"); // contains data needed to decrypt pixel 2 |
Ethan Yonker | bd7492d | 2016-12-07 13:55:01 -0600 | [diff] [blame] | 676 | int retry_count = 3; |
| 677 | while (!Decrypt_DE() && --retry_count) |
| 678 | usleep(2000); |
| 679 | if (retry_count > 0) { |
| 680 | property_set("ro.crypto.state", "encrypted"); |
| 681 | Is_Encrypted = true; |
| 682 | Is_Decrypted = false; |
| 683 | Is_FBE = true; |
| 684 | DataManager::SetValue(TW_IS_FBE, 1); |
| 685 | DataManager::SetValue(TW_IS_ENCRYPTED, 1); |
| 686 | string filename; |
Ethan Yonker | fefe591 | 2017-09-30 22:22:13 -0500 | [diff] [blame] | 687 | int pwd_type = Get_Password_Type(0, filename); |
| 688 | if (pwd_type < 0) { |
| 689 | LOGERR("This TWRP does not have synthetic password decrypt support\n"); |
| 690 | pwd_type = 0; // default password |
| 691 | } |
| 692 | DataManager::SetValue(TW_CRYPTO_PWTYPE, pwd_type); |
Ethan Yonker | bd7492d | 2016-12-07 13:55:01 -0600 | [diff] [blame] | 693 | DataManager::SetValue(TW_CRYPTO_PASSWORD, ""); |
| 694 | DataManager::SetValue("tw_crypto_display", ""); |
| 695 | } |
| 696 | #else |
| 697 | LOGERR("FBE found but FBE support not present in TWRP\n"); |
| 698 | #endif |
| 699 | } else { |
| 700 | // Filesystem is not encrypted and the mount succeeded, so return to |
| 701 | // the original unmounted state |
| 702 | UnMount(false); |
| 703 | } |
Matt Mower | 72c87ce | 2016-04-26 14:34:56 -0500 | [diff] [blame] | 704 | } |
| 705 | if (datamedia && (!Is_Encrypted || (Is_Encrypted && Is_Decrypted))) { |
| 706 | Setup_Data_Media(); |
| 707 | Recreate_Media_Folder(); |
| 708 | } |
| 709 | #else |
| 710 | if (datamedia) { |
| 711 | Setup_Data_Media(); |
| 712 | Recreate_Media_Folder(); |
| 713 | } |
| 714 | #endif |
| 715 | } |
| 716 | |
| 717 | void TWPartition::Setup_Cache_Partition(bool Display_Error __unused) { |
| 718 | if (Mount_Point != "/cache") |
| 719 | return; |
| 720 | |
| 721 | if (!Mount(true)) |
| 722 | return; |
| 723 | |
| 724 | if (!TWFunc::Path_Exists("/cache/recovery/.")) { |
| 725 | LOGINFO("Recreating /cache/recovery folder\n"); |
| 726 | if (mkdir("/cache/recovery", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0) |
| 727 | LOGERR("Could not create /cache/recovery\n"); |
| 728 | } |
| 729 | } |
| 730 | |
Matt Mower | 4ab42b1 | 2016-04-21 13:52:18 -0500 | [diff] [blame] | 731 | void TWPartition::Process_FS_Flags(const char *str) { |
| 732 | char *options = strdup(str); |
| 733 | char *ptr, *savep; |
Hashcode | 62bd9e0 | 2013-11-19 21:59:42 -0800 | [diff] [blame] | 734 | |
Matt Mower | 4ab42b1 | 2016-04-21 13:52:18 -0500 | [diff] [blame] | 735 | Mount_Options = ""; |
Hashcode | 62bd9e0 | 2013-11-19 21:59:42 -0800 | [diff] [blame] | 736 | |
Matt Mower | 4ab42b1 | 2016-04-21 13:52:18 -0500 | [diff] [blame] | 737 | // Avoid issues with potentially nested strtok by using strtok_r |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 738 | for (ptr = strtok_r(options, ",", &savep); ptr; ptr = strtok_r(NULL, ",", &savep)) { |
| 739 | char *equals = strstr(ptr, "="); |
| 740 | size_t name_len; |
Matt Mower | 4ab42b1 | 2016-04-21 13:52:18 -0500 | [diff] [blame] | 741 | |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 742 | if (!equals) |
| 743 | name_len = strlen(ptr); |
| 744 | else |
| 745 | name_len = equals - ptr; |
| 746 | |
| 747 | // There are some flags that we want to ignore in TWRP |
| 748 | bool found_match = false; |
| 749 | for (const char** ignored_mount_item = ignored_mount_items; *ignored_mount_item; ignored_mount_item++) { |
| 750 | if (strncmp(ptr, *ignored_mount_item, name_len) == 0) { |
| 751 | found_match = true; |
Hashcode | 62bd9e0 | 2013-11-19 21:59:42 -0800 | [diff] [blame] | 752 | break; |
| 753 | } |
| 754 | } |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 755 | if (found_match) |
| 756 | continue; |
Hashcode | 62bd9e0 | 2013-11-19 21:59:42 -0800 | [diff] [blame] | 757 | |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 758 | // mount_flags are never postfixed by '=' |
| 759 | if (!equals) { |
| 760 | const struct flag_list* mount_flag = mount_flags; |
| 761 | for (; mount_flag->name; mount_flag++) { |
| 762 | if (strcmp(ptr, mount_flag->name) == 0) { |
| 763 | if (mount_flag->flag == MS_RDONLY) |
| 764 | Mount_Read_Only = true; |
| 765 | else |
| 766 | Mount_Flags |= (unsigned)mount_flag->flag; |
| 767 | found_match = true; |
| 768 | break; |
| 769 | } |
| 770 | } |
| 771 | if (found_match) |
| 772 | continue; |
Matt Mower | 4ab42b1 | 2016-04-21 13:52:18 -0500 | [diff] [blame] | 773 | } |
| 774 | |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 775 | // If we aren't ignoring this flag and it's not a mount flag, then it must be a mount option |
| 776 | if (!Mount_Options.empty()) |
| 777 | Mount_Options += ","; |
| 778 | Mount_Options += ptr; |
Matt Mower | 4ab42b1 | 2016-04-21 13:52:18 -0500 | [diff] [blame] | 779 | } |
| 780 | free(options); |
Hashcode | 62bd9e0 | 2013-11-19 21:59:42 -0800 | [diff] [blame] | 781 | } |
| 782 | |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 783 | void TWPartition::Save_FS_Flags(const string& local_File_System, int local_Mount_Flags, const string& local_Mount_Options) { |
| 784 | partition_fs_flags_struct flags; |
| 785 | flags.File_System = local_File_System; |
| 786 | flags.Mount_Flags = local_Mount_Flags; |
| 787 | flags.Mount_Options = local_Mount_Options; |
| 788 | fs_flags.push_back(flags); |
| 789 | } |
| 790 | |
Matt Mower | 2416a50 | 2016-04-12 19:54:46 -0500 | [diff] [blame] | 791 | void TWPartition::Apply_TW_Flag(const unsigned flag, const char* str, const bool val) { |
| 792 | switch (flag) { |
| 793 | case TWFLAG_ANDSEC: |
| 794 | Has_Android_Secure = val; |
| 795 | break; |
| 796 | case TWFLAG_BACKUP: |
| 797 | Can_Be_Backed_Up = val; |
| 798 | break; |
| 799 | case TWFLAG_BACKUPNAME: |
| 800 | Backup_Display_Name = str; |
| 801 | break; |
| 802 | case TWFLAG_BLOCKSIZE: |
| 803 | Format_Block_Size = (unsigned long)(atol(str)); |
| 804 | break; |
| 805 | case TWFLAG_CANBEWIPED: |
| 806 | Can_Be_Wiped = val; |
| 807 | break; |
| 808 | case TWFLAG_CANENCRYPTBACKUP: |
| 809 | Can_Encrypt_Backup = val; |
| 810 | break; |
| 811 | case TWFLAG_DEFAULTS: |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 812 | case TWFLAG_WAIT: |
| 813 | case TWFLAG_VERIFY: |
| 814 | case TWFLAG_CHECK: |
| 815 | case TWFLAG_NOTRIM: |
| 816 | case TWFLAG_VOLDMANAGED: |
| 817 | case TWFLAG_RESIZE: |
Matt Mower | 2416a50 | 2016-04-12 19:54:46 -0500 | [diff] [blame] | 818 | // Do nothing |
| 819 | break; |
| 820 | case TWFLAG_DISPLAY: |
| 821 | Display_Name = str; |
| 822 | break; |
| 823 | case TWFLAG_ENCRYPTABLE: |
| 824 | case TWFLAG_FORCEENCRYPT: |
| 825 | Crypto_Key_Location = str; |
| 826 | break; |
Ethan Yonker | e9afc3d | 2018-08-30 15:16:27 -0500 | [diff] [blame^] | 827 | case TWFLAG_FILEENCRYPTION: |
| 828 | // This flag isn't used by TWRP but is needed in 9.0 FBE decrypt |
| 829 | // fileencryption=ice:aes-256-heh |
| 830 | { |
| 831 | std::string FBE = str; |
| 832 | std::string FBE_contents, FBE_filenames; |
| 833 | size_t colon_loc = FBE.find(":"); |
| 834 | if (colon_loc == std::string::npos) { |
| 835 | LOGINFO("Invalid fileencryption fstab flag: '%s'\n", str); |
| 836 | break; |
| 837 | } |
| 838 | FBE_contents = FBE.substr(0, colon_loc); |
| 839 | FBE_filenames = FBE.substr(colon_loc + 1); |
| 840 | property_set("fbe.contents", FBE_contents.c_str()); |
| 841 | property_set("fbe.filenames", FBE_filenames.c_str()); |
| 842 | LOGINFO("FBE contents '%s', filenames '%s'\n", FBE_contents.c_str(), FBE_filenames.c_str()); |
| 843 | } |
| 844 | break; |
Matt Mower | 2416a50 | 2016-04-12 19:54:46 -0500 | [diff] [blame] | 845 | case TWFLAG_FLASHIMG: |
| 846 | Can_Flash_Img = val; |
| 847 | break; |
| 848 | case TWFLAG_FSFLAGS: |
Matt Mower | 4ab42b1 | 2016-04-21 13:52:18 -0500 | [diff] [blame] | 849 | Process_FS_Flags(str); |
Matt Mower | 2416a50 | 2016-04-12 19:54:46 -0500 | [diff] [blame] | 850 | break; |
| 851 | case TWFLAG_IGNOREBLKID: |
| 852 | Ignore_Blkid = val; |
| 853 | break; |
| 854 | case TWFLAG_LENGTH: |
| 855 | Length = atoi(str); |
| 856 | break; |
| 857 | case TWFLAG_MOUNTTODECRYPT: |
| 858 | Mount_To_Decrypt = val; |
| 859 | break; |
| 860 | case TWFLAG_REMOVABLE: |
| 861 | Removable = val; |
| 862 | break; |
| 863 | case TWFLAG_RETAINLAYOUTVERSION: |
| 864 | Retain_Layout_Version = val; |
| 865 | break; |
| 866 | case TWFLAG_SETTINGSSTORAGE: |
| 867 | Is_Settings_Storage = val; |
| 868 | if (Is_Settings_Storage) |
| 869 | Is_Storage = true; |
| 870 | break; |
| 871 | case TWFLAG_STORAGE: |
| 872 | Is_Storage = val; |
| 873 | break; |
| 874 | case TWFLAG_STORAGENAME: |
| 875 | Storage_Name = str; |
| 876 | break; |
| 877 | case TWFLAG_SUBPARTITIONOF: |
| 878 | Is_SubPartition = true; |
| 879 | SubPartition_Of = str; |
| 880 | break; |
| 881 | case TWFLAG_SYMLINK: |
| 882 | Symlink_Path = str; |
| 883 | break; |
| 884 | case TWFLAG_USERDATAENCRYPTBACKUP: |
| 885 | Use_Userdata_Encryption = val; |
| 886 | if (Use_Userdata_Encryption) |
| 887 | Can_Encrypt_Backup = true; |
| 888 | break; |
| 889 | case TWFLAG_USERMRF: |
| 890 | Use_Rm_Rf = val; |
| 891 | break; |
| 892 | case TWFLAG_WIPEDURINGFACTORYRESET: |
| 893 | Wipe_During_Factory_Reset = val; |
| 894 | if (Wipe_During_Factory_Reset) { |
| 895 | Can_Be_Wiped = true; |
| 896 | Wipe_Available_in_GUI = true; |
| 897 | } |
| 898 | break; |
| 899 | case TWFLAG_WIPEINGUI: |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 900 | case TWFLAG_FORMATTABLE: |
Matt Mower | 2416a50 | 2016-04-12 19:54:46 -0500 | [diff] [blame] | 901 | Wipe_Available_in_GUI = val; |
| 902 | if (Wipe_Available_in_GUI) |
| 903 | Can_Be_Wiped = true; |
| 904 | break; |
Ethan Yonker | 1b19016 | 2016-12-05 15:25:19 -0600 | [diff] [blame] | 905 | case TWFLAG_SLOTSELECT: |
| 906 | SlotSelect = true; |
| 907 | break; |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 908 | case TWFLAG_ALTDEVICE: |
| 909 | Alternate_Block_Device = str; |
| 910 | break; |
Matt Mower | 2416a50 | 2016-04-12 19:54:46 -0500 | [diff] [blame] | 911 | default: |
| 912 | // Should not get here |
| 913 | LOGINFO("Flag identified for processing, but later unmatched: %i\n", flag); |
| 914 | break; |
| 915 | } |
| 916 | } |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 917 | |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 918 | void TWPartition::Process_TW_Flags(char *flags, bool Display_Error, int fstab_ver) { |
Matt Mower | 2416a50 | 2016-04-12 19:54:46 -0500 | [diff] [blame] | 919 | char separator[2] = {'\n', 0}; |
| 920 | char *ptr, *savep; |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 921 | char source_separator = ';'; |
| 922 | |
| 923 | if (fstab_ver == 2) |
| 924 | source_separator = ','; |
Matt Mower | 2416a50 | 2016-04-12 19:54:46 -0500 | [diff] [blame] | 925 | |
| 926 | // Semicolons within double-quotes are not forbidden, so replace |
| 927 | // only the semicolons intended as separators with '\n' for strtok |
| 928 | for (unsigned i = 0, skip = 0; i < strlen(flags); i++) { |
| 929 | if (flags[i] == '\"') |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 930 | skip = !skip; |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 931 | if (!skip && flags[i] == source_separator) |
Matt Mower | 2416a50 | 2016-04-12 19:54:46 -0500 | [diff] [blame] | 932 | flags[i] = separator[0]; |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 933 | } |
| 934 | |
Matt Mower | 2416a50 | 2016-04-12 19:54:46 -0500 | [diff] [blame] | 935 | // Avoid issues with potentially nested strtok by using strtok_r |
| 936 | ptr = strtok_r(flags, separator, &savep); |
| 937 | while (ptr) { |
| 938 | int ptr_len = strlen(ptr); |
| 939 | const struct flag_list* tw_flag = tw_flags; |
Hashcode | 62bd9e0 | 2013-11-19 21:59:42 -0800 | [diff] [blame] | 940 | |
Matt Mower | 2416a50 | 2016-04-12 19:54:46 -0500 | [diff] [blame] | 941 | for (; tw_flag->name; tw_flag++) { |
| 942 | int flag_len = strlen(tw_flag->name); |
| 943 | |
| 944 | if (strncmp(ptr, tw_flag->name, flag_len) == 0) { |
| 945 | bool flag_val = false; |
| 946 | |
| 947 | if (ptr_len > flag_len && (tw_flag->name)[flag_len-1] != '=' |
| 948 | && ptr[flag_len] != '=') { |
| 949 | // Handle flags with same starting string |
| 950 | // (e.g. backup and backupname) |
| 951 | continue; |
| 952 | } else if (ptr_len > flag_len && ptr[flag_len] == '=') { |
| 953 | // Handle flags with dual format: Part 1 |
| 954 | // (e.g. backup and backup=y. backup=y handled here) |
| 955 | ptr += flag_len + 1; |
| 956 | TWFunc::Strip_Quotes(ptr); |
| 957 | // Skip flags with empty argument |
| 958 | // (e.g. backup=) |
| 959 | if (strlen(ptr) == 0) { |
| 960 | LOGINFO("Flag missing argument or should not include '=': %s=\n", tw_flag->name); |
| 961 | break; |
| 962 | } |
| 963 | flag_val = strchr("yY1", *ptr) != NULL; |
| 964 | } else if (ptr_len == flag_len |
| 965 | && (tw_flag->name)[flag_len-1] == '=') { |
| 966 | // Skip flags missing argument after = |
| 967 | // (e.g. backupname=) |
| 968 | LOGINFO("Flag missing argument: %s\n", tw_flag->name); |
| 969 | break; |
| 970 | } else if (ptr_len > flag_len |
| 971 | && (tw_flag->name)[flag_len-1] == '=') { |
| 972 | // Handle arguments to flags |
| 973 | // (e.g. backupname="My Stuff") |
| 974 | ptr += flag_len; |
| 975 | TWFunc::Strip_Quotes(ptr); |
| 976 | // Skip flags with empty argument |
| 977 | // (e.g. backupname="") |
| 978 | if (strlen(ptr) == 0) { |
| 979 | LOGINFO("Flag missing argument: %s\n", tw_flag->name); |
| 980 | break; |
| 981 | } |
| 982 | } else if (ptr_len == flag_len) { |
| 983 | // Handle flags with dual format: Part 2 |
| 984 | // (e.g. backup and backup=y. backup handled here) |
| 985 | flag_val = true; |
Ethan Yonker | 96af84a | 2015-01-05 14:58:36 -0600 | [diff] [blame] | 986 | } else { |
Matt Mower | 2416a50 | 2016-04-12 19:54:46 -0500 | [diff] [blame] | 987 | LOGINFO("Flag matched, but could not be processed: %s\n", ptr); |
| 988 | break; |
Ethan Yonker | 96af84a | 2015-01-05 14:58:36 -0600 | [diff] [blame] | 989 | } |
Matt Mower | 2416a50 | 2016-04-12 19:54:46 -0500 | [diff] [blame] | 990 | |
| 991 | Apply_TW_Flag(tw_flag->flag, ptr, flag_val); |
| 992 | break; |
Ethan Yonker | 96af84a | 2015-01-05 14:58:36 -0600 | [diff] [blame] | 993 | } |
Matt Mower | 2416a50 | 2016-04-12 19:54:46 -0500 | [diff] [blame] | 994 | } |
| 995 | if (tw_flag->name == 0) { |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 996 | if (Display_Error) |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 997 | LOGERR("Unhandled flag: '%s'\n", ptr); |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 998 | else |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 999 | LOGINFO("Unhandled flag: '%s'\n", ptr); |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 1000 | } |
Matt Mower | 2416a50 | 2016-04-12 19:54:46 -0500 | [diff] [blame] | 1001 | ptr = strtok_r(NULL, separator, &savep); |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 1002 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1003 | } |
| 1004 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1005 | bool TWPartition::Is_File_System(string File_System) { |
| 1006 | if (File_System == "ext2" || |
Dees_Troy | 63c8df7 | 2012-09-10 14:02:05 -0400 | [diff] [blame] | 1007 | File_System == "ext3" || |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1008 | File_System == "ext4" || |
| 1009 | File_System == "vfat" || |
| 1010 | File_System == "ntfs" || |
| 1011 | File_System == "yaffs2" || |
bigbiff bigbiff | 3e14652 | 2012-11-14 14:32:59 -0500 | [diff] [blame] | 1012 | File_System == "exfat" || |
Dees_Troy | e501704 | 2013-08-29 16:38:55 +0000 | [diff] [blame] | 1013 | File_System == "f2fs" || |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1014 | File_System == "auto") |
| 1015 | return true; |
| 1016 | else |
| 1017 | return false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1018 | } |
| 1019 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1020 | bool TWPartition::Is_Image(string File_System) { |
Dees_Troy | 5fcd8f9 | 2012-10-16 12:22:05 -0400 | [diff] [blame] | 1021 | if (File_System == "emmc" || File_System == "mtd" || File_System == "bml") |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1022 | return true; |
| 1023 | else |
| 1024 | return false; |
| 1025 | } |
| 1026 | |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 1027 | bool TWPartition::Make_Dir(string Path, bool Display_Error) { |
Ethan Yonker | 93ac7a0 | 2016-11-07 22:05:58 -0600 | [diff] [blame] | 1028 | if (TWFunc::Get_D_Type_From_Stat(Path) != S_IFDIR) |
| 1029 | unlink(Path.c_str()); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 1030 | if (!TWFunc::Path_Exists(Path)) { |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 1031 | if (mkdir(Path.c_str(), 0777) == -1) { |
| 1032 | if (Display_Error) |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1033 | 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] | 1034 | else |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1035 | LOGINFO("Can not create '%s' folder.\n", Path.c_str()); |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 1036 | return false; |
| 1037 | } else { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1038 | LOGINFO("Created '%s' folder.\n", Path.c_str()); |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 1039 | return true; |
| 1040 | } |
| 1041 | } |
| 1042 | return true; |
| 1043 | } |
| 1044 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1045 | void TWPartition::Setup_File_System(bool Display_Error) { |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1046 | Can_Be_Mounted = true; |
| 1047 | Can_Be_Wiped = true; |
| 1048 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1049 | // Make the mount point folder if it doesn't exist |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 1050 | Make_Dir(Mount_Point, Display_Error); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1051 | Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1); |
| 1052 | Backup_Name = Display_Name; |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 1053 | Backup_Method = BM_FILES; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1054 | } |
| 1055 | |
Ethan Yonker | 1b19016 | 2016-12-05 15:25:19 -0600 | [diff] [blame] | 1056 | void TWPartition::Setup_Image() { |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1057 | Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1); |
| 1058 | Backup_Name = Display_Name; |
Gary Peck | 82599a8 | 2012-11-21 16:23:12 -0800 | [diff] [blame] | 1059 | if (Current_File_System == "emmc") |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 1060 | Backup_Method = BM_DD; |
Gary Peck | 82599a8 | 2012-11-21 16:23:12 -0800 | [diff] [blame] | 1061 | else if (Current_File_System == "mtd" || Current_File_System == "bml") |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 1062 | Backup_Method = BM_FLASH_UTILS; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1063 | else |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1064 | LOGINFO("Unhandled file system '%s' on image '%s'\n", Current_File_System.c_str(), Display_Name.c_str()); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1065 | } |
| 1066 | |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 1067 | void TWPartition::Setup_AndSec(void) { |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 1068 | Backup_Display_Name = "Android Secure"; |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 1069 | Backup_Name = "and-sec"; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 1070 | Can_Be_Backed_Up = true; |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 1071 | Has_Android_Secure = true; |
| 1072 | Symlink_Path = Mount_Point + "/.android_secure"; |
| 1073 | Symlink_Mount_Point = "/and-sec"; |
| 1074 | Backup_Path = Symlink_Mount_Point; |
| 1075 | Make_Dir("/and-sec", true); |
| 1076 | Recreate_AndSec_Folder(); |
Kjell Braden | 3126a11 | 2016-06-19 16:58:15 +0000 | [diff] [blame] | 1077 | Mount_Storage_Retry(true); |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 1078 | } |
| 1079 | |
that | 9e0593e | 2014-10-08 00:01:24 +0200 | [diff] [blame] | 1080 | void TWPartition::Setup_Data_Media() { |
Ethan Yonker | 6277c79 | 2014-09-15 14:54:30 -0500 | [diff] [blame] | 1081 | 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] | 1082 | if (Storage_Name.empty() || Storage_Name == "Data") |
| 1083 | Storage_Name = "Internal Storage"; |
Ethan Yonker | 6277c79 | 2014-09-15 14:54:30 -0500 | [diff] [blame] | 1084 | Has_Data_Media = true; |
| 1085 | Is_Storage = true; |
Ethan Yonker | 66a1949 | 2015-12-10 10:19:45 -0600 | [diff] [blame] | 1086 | Storage_Path = Mount_Point + "/media"; |
Ethan Yonker | 6277c79 | 2014-09-15 14:54:30 -0500 | [diff] [blame] | 1087 | Symlink_Path = Storage_Path; |
Ethan Yonker | 66a1949 | 2015-12-10 10:19:45 -0600 | [diff] [blame] | 1088 | if (Mount_Point == "/data") { |
| 1089 | Is_Settings_Storage = true; |
| 1090 | if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) { |
| 1091 | Make_Dir("/emmc", false); |
| 1092 | Symlink_Mount_Point = "/emmc"; |
| 1093 | } else { |
| 1094 | Make_Dir("/sdcard", false); |
| 1095 | Symlink_Mount_Point = "/sdcard"; |
| 1096 | } |
| 1097 | if (Mount(false) && TWFunc::Path_Exists(Mount_Point + "/media/0")) { |
| 1098 | Storage_Path = Mount_Point + "/media/0"; |
| 1099 | Symlink_Path = Storage_Path; |
| 1100 | DataManager::SetValue(TW_INTERNAL_PATH, Mount_Point + "/media/0"); |
| 1101 | UnMount(true); |
| 1102 | } |
| 1103 | DataManager::SetValue("tw_has_internal", 1); |
| 1104 | DataManager::SetValue("tw_has_data_media", 1); |
Ethan Yonker | 3fdcda4 | 2016-11-30 12:29:37 -0600 | [diff] [blame] | 1105 | backup_exclusions.add_absolute_dir("/data/data/com.google.android.music/files"); |
Ethan Yonker | 6355b56 | 2017-05-01 09:42:17 -0500 | [diff] [blame] | 1106 | wipe_exclusions.add_absolute_dir(Mount_Point + "/misc/vold"); // adopted storage keys |
Ethan Yonker | 79f88bd | 2016-12-09 14:52:12 -0600 | [diff] [blame] | 1107 | ExcludeAll(Mount_Point + "/.layout_version"); |
| 1108 | ExcludeAll(Mount_Point + "/system/storage.xml"); |
Ethan Yonker | 6277c79 | 2014-09-15 14:54:30 -0500 | [diff] [blame] | 1109 | } else { |
Ethan Yonker | 66a1949 | 2015-12-10 10:19:45 -0600 | [diff] [blame] | 1110 | if (Mount(true) && TWFunc::Path_Exists(Mount_Point + "/media/0")) { |
| 1111 | Storage_Path = Mount_Point + "/media/0"; |
| 1112 | Symlink_Path = Storage_Path; |
| 1113 | UnMount(true); |
| 1114 | } |
Ethan Yonker | 6277c79 | 2014-09-15 14:54:30 -0500 | [diff] [blame] | 1115 | } |
Ethan Yonker | 79f88bd | 2016-12-09 14:52:12 -0600 | [diff] [blame] | 1116 | ExcludeAll(Mount_Point + "/media"); |
Ethan Yonker | 6277c79 | 2014-09-15 14:54:30 -0500 | [diff] [blame] | 1117 | } |
| 1118 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1119 | void TWPartition::Find_Real_Block_Device(string& Block, bool Display_Error) { |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 1120 | char device[PATH_MAX], realDevice[PATH_MAX]; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1121 | |
| 1122 | strcpy(device, Block.c_str()); |
| 1123 | memset(realDevice, 0, sizeof(realDevice)); |
| 1124 | while (readlink(device, realDevice, sizeof(realDevice)) > 0) |
| 1125 | { |
| 1126 | strcpy(device, realDevice); |
| 1127 | memset(realDevice, 0, sizeof(realDevice)); |
| 1128 | } |
| 1129 | |
| 1130 | if (device[0] != '/') { |
| 1131 | if (Display_Error) |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1132 | 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] | 1133 | else |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1134 | 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] | 1135 | return; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1136 | } else { |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1137 | Block = device; |
| 1138 | return; |
| 1139 | } |
| 1140 | } |
| 1141 | |
Kjell Braden | 3126a11 | 2016-06-19 16:58:15 +0000 | [diff] [blame] | 1142 | bool TWPartition::Mount_Storage_Retry(bool Display_Error) { |
Dees_Troy | 8e337f3 | 2012-10-13 22:07:49 -0400 | [diff] [blame] | 1143 | // 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] | 1144 | if (!Mount(Display_Error)) { |
Dees_Troy | 8e337f3 | 2012-10-13 22:07:49 -0400 | [diff] [blame] | 1145 | int retry_count = 5; |
| 1146 | while (retry_count > 0 && !Mount(false)) { |
| 1147 | usleep(500000); |
| 1148 | retry_count--; |
| 1149 | } |
Kjell Braden | 3126a11 | 2016-06-19 16:58:15 +0000 | [diff] [blame] | 1150 | return Mount(Display_Error); |
Dees_Troy | 8e337f3 | 2012-10-13 22:07:49 -0400 | [diff] [blame] | 1151 | } |
Kjell Braden | 3126a11 | 2016-06-19 16:58:15 +0000 | [diff] [blame] | 1152 | return true; |
Dees_Troy | 8e337f3 | 2012-10-13 22:07:49 -0400 | [diff] [blame] | 1153 | } |
| 1154 | |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1155 | bool TWPartition::Find_MTD_Block_Device(string MTD_Name) { |
| 1156 | FILE *fp = NULL; |
| 1157 | char line[255]; |
| 1158 | |
| 1159 | fp = fopen("/proc/mtd", "rt"); |
| 1160 | if (fp == NULL) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1161 | LOGERR("Device does not support /proc/mtd\n"); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1162 | return false; |
| 1163 | } |
| 1164 | |
| 1165 | while (fgets(line, sizeof(line), fp) != NULL) |
| 1166 | { |
| 1167 | char device[32], label[32]; |
| 1168 | unsigned long size = 0; |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1169 | int deviceId; |
| 1170 | |
| 1171 | sscanf(line, "%s %lx %*s %*c%s", device, &size, label); |
| 1172 | |
| 1173 | // Skip header and blank lines |
| 1174 | if ((strcmp(device, "dev:") == 0) || (strlen(line) < 8)) |
| 1175 | continue; |
| 1176 | |
| 1177 | // Strip off the trailing " from the label |
| 1178 | label[strlen(label)-1] = '\0'; |
| 1179 | |
| 1180 | if (strcmp(label, MTD_Name.c_str()) == 0) { |
| 1181 | // We found our device |
| 1182 | // Strip off the trailing : from the device |
| 1183 | device[strlen(device)-1] = '\0'; |
| 1184 | if (sscanf(device,"mtd%d", &deviceId) == 1) { |
| 1185 | sprintf(device, "/dev/block/mtdblock%d", deviceId); |
| 1186 | Primary_Block_Device = device; |
Dees_Troy | 76543db | 2013-06-19 16:24:30 +0000 | [diff] [blame] | 1187 | fclose(fp); |
| 1188 | return true; |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1189 | } |
| 1190 | } |
| 1191 | } |
| 1192 | fclose(fp); |
| 1193 | |
| 1194 | return false; |
| 1195 | } |
| 1196 | |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 1197 | bool TWPartition::Get_Size_Via_statfs(bool Display_Error) { |
| 1198 | struct statfs st; |
| 1199 | string Local_Path = Mount_Point + "/."; |
| 1200 | |
| 1201 | if (!Mount(Display_Error)) |
| 1202 | return false; |
| 1203 | |
| 1204 | if (statfs(Local_Path.c_str(), &st) != 0) { |
| 1205 | if (!Removable) { |
| 1206 | if (Display_Error) |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1207 | LOGERR("Unable to statfs '%s'\n", Local_Path.c_str()); |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 1208 | else |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1209 | LOGINFO("Unable to statfs '%s'\n", Local_Path.c_str()); |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 1210 | } |
| 1211 | return false; |
| 1212 | } |
| 1213 | Size = (st.f_blocks * st.f_bsize); |
| 1214 | Used = ((st.f_blocks - st.f_bfree) * st.f_bsize); |
| 1215 | Free = (st.f_bfree * st.f_bsize); |
| 1216 | Backup_Size = Used; |
| 1217 | return true; |
| 1218 | } |
| 1219 | |
| 1220 | bool TWPartition::Get_Size_Via_df(bool Display_Error) { |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1221 | FILE* fp; |
| 1222 | char command[255], line[512]; |
| 1223 | int include_block = 1; |
| 1224 | unsigned int min_len; |
| 1225 | |
| 1226 | if (!Mount(Display_Error)) |
| 1227 | return false; |
| 1228 | |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1229 | min_len = Actual_Block_Device.size() + 2; |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 1230 | sprintf(command, "df %s > /tmp/dfoutput.txt", Mount_Point.c_str()); |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 1231 | TWFunc::Exec_Cmd(command); |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 1232 | fp = fopen("/tmp/dfoutput.txt", "rt"); |
| 1233 | if (fp == NULL) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1234 | LOGINFO("Unable to open /tmp/dfoutput.txt.\n"); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1235 | return false; |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 1236 | } |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1237 | |
| 1238 | while (fgets(line, sizeof(line), fp) != NULL) |
| 1239 | { |
| 1240 | unsigned long blocks, used, available; |
| 1241 | char device[64]; |
| 1242 | char tmpString[64]; |
| 1243 | |
| 1244 | if (strncmp(line, "Filesystem", 10) == 0) |
| 1245 | continue; |
| 1246 | if (strlen(line) < min_len) { |
| 1247 | include_block = 0; |
| 1248 | continue; |
| 1249 | } |
| 1250 | if (include_block) { |
| 1251 | sscanf(line, "%s %lu %lu %lu", device, &blocks, &used, &available); |
| 1252 | } else { |
| 1253 | // The device block string is so long that the df information is on the next line |
| 1254 | int space_count = 0; |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 1255 | sprintf(tmpString, "/dev/block/%s", Actual_Block_Device.c_str()); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1256 | while (tmpString[space_count] == 32) |
| 1257 | space_count++; |
| 1258 | sscanf(line + space_count, "%lu %lu %lu", &blocks, &used, &available); |
| 1259 | } |
| 1260 | |
| 1261 | // Adjust block size to byte size |
| 1262 | Size = blocks * 1024ULL; |
| 1263 | Used = used * 1024ULL; |
| 1264 | Free = available * 1024ULL; |
| 1265 | Backup_Size = Used; |
| 1266 | } |
| 1267 | fclose(fp); |
| 1268 | return true; |
| 1269 | } |
| 1270 | |
Ethan Yonker | d18a821 | 2015-12-14 10:17:00 -0600 | [diff] [blame] | 1271 | unsigned long long TWPartition::IOCTL_Get_Block_Size() { |
Ethan Yonker | d18a821 | 2015-12-14 10:17:00 -0600 | [diff] [blame] | 1272 | Find_Actual_Block_Device(); |
Ethan Yonker | 483e9f4 | 2016-01-11 22:21:18 -0600 | [diff] [blame] | 1273 | |
| 1274 | return TWFunc::IOCTL_Get_Block_Size(Actual_Block_Device.c_str()); |
Ethan Yonker | d18a821 | 2015-12-14 10:17:00 -0600 | [diff] [blame] | 1275 | } |
| 1276 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1277 | bool TWPartition::Find_Partition_Size(void) { |
| 1278 | FILE* fp; |
| 1279 | char line[512]; |
| 1280 | string tmpdevice; |
| 1281 | |
igoriok | 87e3d93 | 2013-01-31 21:03:53 +0200 | [diff] [blame] | 1282 | fp = fopen("/proc/dumchar_info", "rt"); |
| 1283 | if (fp != NULL) { |
| 1284 | while (fgets(line, sizeof(line), fp) != NULL) |
| 1285 | { |
| 1286 | char label[32], device[32]; |
| 1287 | unsigned long size = 0; |
| 1288 | |
that | d43bf2d | 2014-09-21 23:13:02 +0200 | [diff] [blame] | 1289 | sscanf(line, "%s %lx %*x %*u %s", label, &size, device); |
igoriok | 87e3d93 | 2013-01-31 21:03:53 +0200 | [diff] [blame] | 1290 | |
bigbiff bigbiff | 34684ff | 2013-12-01 21:03:45 -0500 | [diff] [blame] | 1291 | // Skip header, annotation and blank lines |
igoriok | 87e3d93 | 2013-01-31 21:03:53 +0200 | [diff] [blame] | 1292 | if ((strncmp(device, "/dev/", 5) != 0) || (strlen(line) < 8)) |
| 1293 | continue; |
| 1294 | |
| 1295 | tmpdevice = "/dev/"; |
| 1296 | tmpdevice += label; |
| 1297 | if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) { |
| 1298 | Size = size; |
| 1299 | fclose(fp); |
| 1300 | return true; |
| 1301 | } |
| 1302 | } |
| 1303 | } |
| 1304 | |
Ethan Yonker | d18a821 | 2015-12-14 10:17:00 -0600 | [diff] [blame] | 1305 | unsigned long long ioctl_size = IOCTL_Get_Block_Size(); |
| 1306 | if (ioctl_size) { |
| 1307 | Size = ioctl_size; |
| 1308 | return true; |
| 1309 | } |
| 1310 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1311 | // In this case, we'll first get the partitions we care about (with labels) |
| 1312 | fp = fopen("/proc/partitions", "rt"); |
| 1313 | if (fp == NULL) |
| 1314 | return false; |
| 1315 | |
| 1316 | while (fgets(line, sizeof(line), fp) != NULL) |
| 1317 | { |
| 1318 | unsigned long major, minor, blocks; |
| 1319 | char device[512]; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1320 | |
Dees_Troy | 63c8df7 | 2012-09-10 14:02:05 -0400 | [diff] [blame] | 1321 | if (strlen(line) < 7 || line[0] == 'm') continue; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1322 | sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device); |
| 1323 | |
| 1324 | tmpdevice = "/dev/block/"; |
| 1325 | tmpdevice += device; |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1326 | if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) { |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1327 | // Adjust block size to byte size |
| 1328 | Size = blocks * 1024ULL; |
| 1329 | fclose(fp); |
| 1330 | return true; |
| 1331 | } |
| 1332 | } |
| 1333 | fclose(fp); |
| 1334 | return false; |
| 1335 | } |
| 1336 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1337 | bool TWPartition::Is_Mounted(void) { |
| 1338 | if (!Can_Be_Mounted) |
| 1339 | return false; |
| 1340 | |
| 1341 | struct stat st1, st2; |
| 1342 | string test_path; |
| 1343 | |
| 1344 | // Check to see if the mount point directory exists |
| 1345 | test_path = Mount_Point + "/."; |
| 1346 | if (stat(test_path.c_str(), &st1) != 0) return false; |
| 1347 | |
| 1348 | // Check to see if the directory above the mount point exists |
| 1349 | test_path = Mount_Point + "/../."; |
| 1350 | if (stat(test_path.c_str(), &st2) != 0) return false; |
| 1351 | |
| 1352 | // Compare the device IDs -- if they match then we're (probably) using tmpfs instead of an actual device |
| 1353 | int ret = (st1.st_dev != st2.st_dev) ? true : false; |
| 1354 | |
| 1355 | return ret; |
| 1356 | } |
| 1357 | |
James Christopher Adduono | d6f94ac | 2016-02-29 04:26:04 -0500 | [diff] [blame] | 1358 | bool TWPartition::Is_File_System_Writable(void) { |
| 1359 | if (!Is_File_System(Current_File_System) || !Is_Mounted()) |
| 1360 | return false; |
| 1361 | |
| 1362 | string test_path = Mount_Point + "/."; |
| 1363 | return (access(test_path.c_str(), W_OK) == 0); |
| 1364 | } |
| 1365 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1366 | bool TWPartition::Mount(bool Display_Error) { |
Dees_Troy | 3f5c4e8 | 2013-02-01 15:16:59 +0000 | [diff] [blame] | 1367 | int exfat_mounted = 0; |
Ethan Yonker | eb32b1f | 2015-05-18 10:23:03 -0500 | [diff] [blame] | 1368 | unsigned long flags = Mount_Flags; |
Dees_Troy | 3f5c4e8 | 2013-02-01 15:16:59 +0000 | [diff] [blame] | 1369 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1370 | if (Is_Mounted()) { |
| 1371 | return true; |
| 1372 | } else if (!Can_Be_Mounted) { |
| 1373 | return false; |
| 1374 | } |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1375 | |
| 1376 | Find_Actual_Block_Device(); |
| 1377 | |
| 1378 | // Check the current file system before mounting |
| 1379 | Check_FS_Type(); |
Dees_Troy | 3f5c4e8 | 2013-02-01 15:16:59 +0000 | [diff] [blame] | 1380 | if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/exfat-fuse")) { |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 1381 | 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] | 1382 | LOGINFO("cmd: %s\n", cmd.c_str()); |
Dees_Troy | 3f5c4e8 | 2013-02-01 15:16:59 +0000 | [diff] [blame] | 1383 | string result; |
| 1384 | if (TWFunc::Exec_Cmd(cmd, result) != 0) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1385 | 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] | 1386 | Current_File_System = "vfat"; |
| 1387 | } else { |
| 1388 | #ifdef TW_NO_EXFAT_FUSE |
| 1389 | UnMount(false); |
| 1390 | // We'll let the kernel handle it but using exfat-fuse to detect if the file system is actually exfat |
| 1391 | // Some kernels let us mount vfat as exfat which doesn't work out too well |
| 1392 | #else |
| 1393 | exfat_mounted = 1; |
| 1394 | #endif |
| 1395 | } |
| 1396 | } |
Ethan Yonker | eb32b1f | 2015-05-18 10:23:03 -0500 | [diff] [blame] | 1397 | |
Greg Wallace | b39e6c6 | 2015-12-29 00:55:26 -0500 | [diff] [blame] | 1398 | 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] | 1399 | string cmd; |
Greg Wallace | b39e6c6 | 2015-12-29 00:55:26 -0500 | [diff] [blame] | 1400 | string Ntfsmount_Binary = ""; |
| 1401 | |
| 1402 | if (TWFunc::Path_Exists("/sbin/ntfs-3g")) |
| 1403 | Ntfsmount_Binary = "ntfs-3g"; |
| 1404 | else if (TWFunc::Path_Exists("/sbin/mount.ntfs")) |
| 1405 | Ntfsmount_Binary = "mount.ntfs"; |
| 1406 | |
Ethan Yonker | b81d905 | 2015-07-09 13:20:53 -0500 | [diff] [blame] | 1407 | if (Mount_Read_Only) |
Greg Wallace | b39e6c6 | 2015-12-29 00:55:26 -0500 | [diff] [blame] | 1408 | cmd = "/sbin/" + Ntfsmount_Binary + " -o ro " + Actual_Block_Device + " " + Mount_Point; |
Ethan Yonker | b81d905 | 2015-07-09 13:20:53 -0500 | [diff] [blame] | 1409 | else |
Greg Wallace | b39e6c6 | 2015-12-29 00:55:26 -0500 | [diff] [blame] | 1410 | cmd = "/sbin/" + Ntfsmount_Binary + " " + Actual_Block_Device + " " + Mount_Point; |
Ethan Yonker | b81d905 | 2015-07-09 13:20:53 -0500 | [diff] [blame] | 1411 | LOGINFO("cmd: '%s'\n", cmd.c_str()); |
Greg Wallace | b39e6c6 | 2015-12-29 00:55:26 -0500 | [diff] [blame] | 1412 | |
Ethan Yonker | b81d905 | 2015-07-09 13:20:53 -0500 | [diff] [blame] | 1413 | if (TWFunc::Exec_Cmd(cmd) == 0) { |
| 1414 | return true; |
| 1415 | } else { |
| 1416 | LOGINFO("ntfs-3g failed to mount, trying regular mount method.\n"); |
| 1417 | } |
| 1418 | } |
| 1419 | |
Ethan Yonker | eb32b1f | 2015-05-18 10:23:03 -0500 | [diff] [blame] | 1420 | if (Mount_Read_Only) |
| 1421 | flags |= MS_RDONLY; |
| 1422 | |
Dees_Troy | 2204203 | 2012-12-18 21:23:08 +0000 | [diff] [blame] | 1423 | if (Fstab_File_System == "yaffs2") { |
| 1424 | // mount an MTD partition as a YAFFS2 filesystem. |
Ethan Yonker | eb32b1f | 2015-05-18 10:23:03 -0500 | [diff] [blame] | 1425 | flags = MS_NOATIME | MS_NODEV | MS_NODIRATIME; |
| 1426 | if (Mount_Read_Only) |
| 1427 | flags |= MS_RDONLY; |
Dees_Troy | 76543db | 2013-06-19 16:24:30 +0000 | [diff] [blame] | 1428 | if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags, NULL) < 0) { |
| 1429 | if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags | MS_RDONLY, NULL) < 0) { |
| 1430 | if (Display_Error) |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1431 | 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] | 1432 | else |
| 1433 | LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str()); |
| 1434 | return false; |
| 1435 | } else { |
| 1436 | LOGINFO("Mounted '%s' (MTD) as RO\n", Mount_Point.c_str()); |
| 1437 | return true; |
| 1438 | } |
| 1439 | } else { |
| 1440 | struct stat st; |
| 1441 | string test_path = Mount_Point; |
| 1442 | if (stat(test_path.c_str(), &st) < 0) { |
| 1443 | if (Display_Error) |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1444 | gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno))); |
Dees_Troy | 76543db | 2013-06-19 16:24:30 +0000 | [diff] [blame] | 1445 | else |
| 1446 | LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str()); |
| 1447 | return false; |
| 1448 | } |
| 1449 | mode_t new_mode = st.st_mode | S_IXUSR | S_IXGRP | S_IXOTH; |
| 1450 | if (new_mode != st.st_mode) { |
| 1451 | LOGINFO("Fixing execute permissions for %s\n", Mount_Point.c_str()); |
| 1452 | if (chmod(Mount_Point.c_str(), new_mode) < 0) { |
| 1453 | if (Display_Error) |
| 1454 | LOGERR("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno)); |
| 1455 | else |
| 1456 | LOGINFO("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno)); |
| 1457 | return false; |
| 1458 | } |
| 1459 | } |
Dees_Troy | 2204203 | 2012-12-18 21:23:08 +0000 | [diff] [blame] | 1460 | return true; |
Dees_Troy | 76543db | 2013-06-19 16:24:30 +0000 | [diff] [blame] | 1461 | } |
that | c7572eb | 2015-03-31 18:55:30 +0200 | [diff] [blame] | 1462 | } |
| 1463 | |
| 1464 | string mount_fs = Current_File_System; |
| 1465 | if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sys/module/texfat")) |
| 1466 | mount_fs = "texfat"; |
| 1467 | |
| 1468 | if (!exfat_mounted && |
Ethan Yonker | eb32b1f | 2015-05-18 10:23:03 -0500 | [diff] [blame] | 1469 | mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), mount_fs.c_str(), flags, Mount_Options.c_str()) != 0 && |
| 1470 | 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] | 1471 | #ifdef TW_NO_EXFAT_FUSE |
| 1472 | if (Current_File_System == "exfat") { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1473 | LOGINFO("Mounting exfat failed, trying vfat...\n"); |
Dees_Troy | 3f5c4e8 | 2013-02-01 15:16:59 +0000 | [diff] [blame] | 1474 | 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] | 1475 | if (Display_Error) |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1476 | 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] | 1477 | else |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1478 | LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str()); |
Ethan Yonker | eb32b1f | 2015-05-18 10:23:03 -0500 | [diff] [blame] | 1479 | 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] | 1480 | return false; |
Dees_Troy | 85f44ed | 2013-01-09 18:42:36 +0000 | [diff] [blame] | 1481 | } |
Dees_Troy | b05ddee | 2013-01-28 20:24:50 +0000 | [diff] [blame] | 1482 | } else { |
Dees_Troy | 3f5c4e8 | 2013-02-01 15:16:59 +0000 | [diff] [blame] | 1483 | #endif |
bigbiff bigbiff | 26774a0 | 2014-03-29 18:22:00 -0400 | [diff] [blame] | 1484 | if (!Removable && Display_Error) |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1485 | 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] | 1486 | else |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1487 | LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str()); |
| 1488 | 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] | 1489 | return false; |
| 1490 | #ifdef TW_NO_EXFAT_FUSE |
Dees_Troy | 85f44ed | 2013-01-09 18:42:36 +0000 | [diff] [blame] | 1491 | } |
| 1492 | #endif |
Dees_Troy | 3f5c4e8 | 2013-02-01 15:16:59 +0000 | [diff] [blame] | 1493 | } |
Ethan Yonker | 253368a | 2014-11-25 15:00:52 -0600 | [diff] [blame] | 1494 | |
Dees_Troy | 3f5c4e8 | 2013-02-01 15:16:59 +0000 | [diff] [blame] | 1495 | if (Removable) |
| 1496 | Update_Size(Display_Error); |
| 1497 | |
xiaolu | 9416f4f | 2015-06-04 08:22:23 +0800 | [diff] [blame] | 1498 | if (!Symlink_Mount_Point.empty() && TWFunc::Path_Exists(Symlink_Path)) { |
codelover | 2a3d4ce | 2015-03-14 20:26:49 +0800 | [diff] [blame] | 1499 | string Command = "mount -o bind '" + Symlink_Path + "' '" + Symlink_Mount_Point + "'"; |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 1500 | TWFunc::Exec_Cmd(Command); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1501 | } |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1502 | return true; |
| 1503 | } |
| 1504 | |
| 1505 | bool TWPartition::UnMount(bool Display_Error) { |
| 1506 | if (Is_Mounted()) { |
| 1507 | int never_unmount_system; |
| 1508 | |
| 1509 | DataManager::GetValue(TW_DONT_UNMOUNT_SYSTEM, never_unmount_system); |
| 1510 | if (never_unmount_system == 1 && Mount_Point == "/system") |
| 1511 | return true; // Never unmount system if you're not supposed to unmount it |
| 1512 | |
Matt Mower | a8e6d83 | 2017-02-14 20:20:59 -0600 | [diff] [blame] | 1513 | if (Is_Storage && MTP_Storage_ID > 0) |
Ethan Yonker | 726a020 | 2014-12-16 20:01:38 -0600 | [diff] [blame] | 1514 | PartitionManager.Remove_MTP_Storage(MTP_Storage_ID); |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 1515 | |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1516 | if (!Symlink_Mount_Point.empty()) |
| 1517 | umount(Symlink_Mount_Point.c_str()); |
| 1518 | |
Dees_Troy | b05ddee | 2013-01-28 20:24:50 +0000 | [diff] [blame] | 1519 | umount(Mount_Point.c_str()); |
| 1520 | if (Is_Mounted()) { |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1521 | if (Display_Error) |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1522 | 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] | 1523 | else |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1524 | LOGINFO("Unable to unmount '%s'\n", Mount_Point.c_str()); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1525 | return false; |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 1526 | } else { |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1527 | return true; |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 1528 | } |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1529 | } else { |
| 1530 | return true; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1531 | } |
| 1532 | } |
| 1533 | |
James Christopher Adduono | d6f94ac | 2016-02-29 04:26:04 -0500 | [diff] [blame] | 1534 | bool TWPartition::ReMount(bool Display_Error) { |
| 1535 | if (UnMount(Display_Error)) |
| 1536 | return Mount(Display_Error); |
| 1537 | return false; |
| 1538 | } |
| 1539 | |
| 1540 | bool TWPartition::ReMount_RW(bool Display_Error) { |
| 1541 | // No need to remount if already mounted rw |
| 1542 | if (Is_File_System_Writable()) |
| 1543 | return true; |
| 1544 | |
| 1545 | bool ro = Mount_Read_Only; |
| 1546 | int flags = Mount_Flags; |
| 1547 | |
| 1548 | Mount_Read_Only = false; |
| 1549 | Mount_Flags &= ~MS_RDONLY; |
| 1550 | |
| 1551 | bool ret = ReMount(Display_Error); |
| 1552 | |
| 1553 | Mount_Read_Only = ro; |
| 1554 | Mount_Flags = flags; |
| 1555 | |
| 1556 | return ret; |
| 1557 | } |
| 1558 | |
Gary Peck | 43acadf | 2012-11-21 21:19:01 -0800 | [diff] [blame] | 1559 | bool TWPartition::Wipe(string New_File_System) { |
Ethan Yonker | 726a020 | 2014-12-16 20:01:38 -0600 | [diff] [blame] | 1560 | bool wiped = false, update_crypt = false, recreate_media = true; |
Dees_Troy | 16c2b31 | 2013-01-15 16:51:18 +0000 | [diff] [blame] | 1561 | int check; |
| 1562 | string Layout_Filename = Mount_Point + "/.layout_version"; |
| 1563 | |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1564 | if (!Can_Be_Wiped) { |
Matt Mower | 3c36697 | 2015-12-25 19:28:31 -0600 | [diff] [blame] | 1565 | 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] | 1566 | return false; |
| 1567 | } |
| 1568 | |
Dees_Troy | c51f1f9 | 2012-09-20 15:32:13 -0400 | [diff] [blame] | 1569 | if (Mount_Point == "/cache") |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1570 | Log_Offset = 0; |
Dees_Troy | c51f1f9 | 2012-09-20 15:32:13 -0400 | [diff] [blame] | 1571 | |
Dees_Troy | 16c2b31 | 2013-01-15 16:51:18 +0000 | [diff] [blame] | 1572 | if (Retain_Layout_Version && Mount(false) && TWFunc::Path_Exists(Layout_Filename)) |
| 1573 | TWFunc::copy_file(Layout_Filename, "/.layout_version", 0600); |
| 1574 | else |
| 1575 | unlink("/.layout_version"); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1576 | |
Ethan Yonker | 87c7bac | 2014-05-25 21:41:08 -0500 | [diff] [blame] | 1577 | if (Has_Data_Media && Current_File_System == New_File_System) { |
Dees_Troy | 16c2b31 | 2013-01-15 16:51:18 +0000 | [diff] [blame] | 1578 | wiped = Wipe_Data_Without_Wiping_Media(); |
Ethan Yonker | 5eac222 | 2014-06-11 12:22:55 -0500 | [diff] [blame] | 1579 | recreate_media = false; |
Dees_Troy | 16c2b31 | 2013-01-15 16:51:18 +0000 | [diff] [blame] | 1580 | } else { |
Dees_Troy | 16c2b31 | 2013-01-15 16:51:18 +0000 | [diff] [blame] | 1581 | DataManager::GetValue(TW_RM_RF_VAR, check); |
| 1582 | |
Hashcode | dabfd49 | 2013-08-29 22:45:30 -0700 | [diff] [blame] | 1583 | if (check || Use_Rm_Rf) |
Dees_Troy | 16c2b31 | 2013-01-15 16:51:18 +0000 | [diff] [blame] | 1584 | wiped = Wipe_RMRF(); |
| 1585 | else if (New_File_System == "ext4") |
| 1586 | wiped = Wipe_EXT4(); |
| 1587 | else if (New_File_System == "ext2" || New_File_System == "ext3") |
| 1588 | wiped = Wipe_EXT23(New_File_System); |
| 1589 | else if (New_File_System == "vfat") |
| 1590 | wiped = Wipe_FAT(); |
| 1591 | else if (New_File_System == "exfat") |
| 1592 | wiped = Wipe_EXFAT(); |
| 1593 | else if (New_File_System == "yaffs2") |
| 1594 | wiped = Wipe_MTD(); |
Dees_Troy | e501704 | 2013-08-29 16:38:55 +0000 | [diff] [blame] | 1595 | else if (New_File_System == "f2fs") |
| 1596 | wiped = Wipe_F2FS(); |
Ethan Yonker | b81d905 | 2015-07-09 13:20:53 -0500 | [diff] [blame] | 1597 | else if (New_File_System == "ntfs") |
| 1598 | wiped = Wipe_NTFS(); |
Dees_Troy | 16c2b31 | 2013-01-15 16:51:18 +0000 | [diff] [blame] | 1599 | else { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1600 | 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] | 1601 | unlink("/.layout_version"); |
| 1602 | return false; |
| 1603 | } |
| 1604 | update_crypt = wiped; |
Gary Peck | e8bc5d7 | 2012-12-21 06:45:25 -0800 | [diff] [blame] | 1605 | } |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1606 | |
Gary Peck | e8bc5d7 | 2012-12-21 06:45:25 -0800 | [diff] [blame] | 1607 | if (wiped) { |
Dees_Troy | 1c1ac44 | 2013-01-17 21:42:14 +0000 | [diff] [blame] | 1608 | if (Mount_Point == "/cache") |
| 1609 | DataManager::Output_Version(); |
| 1610 | |
Dees_Troy | 16c2b31 | 2013-01-15 16:51:18 +0000 | [diff] [blame] | 1611 | if (TWFunc::Path_Exists("/.layout_version") && Mount(false)) |
| 1612 | TWFunc::copy_file("/.layout_version", Layout_Filename, 0600); |
| 1613 | |
| 1614 | if (update_crypt) { |
| 1615 | Setup_File_System(false); |
| 1616 | if (Is_Encrypted && !Is_Decrypted) { |
| 1617 | // just wiped an encrypted partition back to its unencrypted state |
| 1618 | Is_Encrypted = false; |
| 1619 | Is_Decrypted = false; |
| 1620 | Decrypted_Block_Device = ""; |
| 1621 | if (Mount_Point == "/data") { |
| 1622 | DataManager::SetValue(TW_IS_ENCRYPTED, 0); |
| 1623 | DataManager::SetValue(TW_IS_DECRYPTED, 0); |
| 1624 | } |
Gary Peck | e8bc5d7 | 2012-12-21 06:45:25 -0800 | [diff] [blame] | 1625 | } |
| 1626 | } |
Ethan Yonker | 5eac222 | 2014-06-11 12:22:55 -0500 | [diff] [blame] | 1627 | |
Ethan Yonker | 66a1949 | 2015-12-10 10:19:45 -0600 | [diff] [blame] | 1628 | if (Has_Data_Media && recreate_media) { |
Ethan Yonker | 5eac222 | 2014-06-11 12:22:55 -0500 | [diff] [blame] | 1629 | Recreate_Media_Folder(); |
| 1630 | } |
Matt Mower | 209c963 | 2016-01-20 12:08:35 -0600 | [diff] [blame] | 1631 | if (Is_Storage && Mount(false)) |
| 1632 | PartitionManager.Add_MTP_Storage(MTP_Storage_ID); |
Gary Peck | e8bc5d7 | 2012-12-21 06:45:25 -0800 | [diff] [blame] | 1633 | } |
Matt Mower | 209c963 | 2016-01-20 12:08:35 -0600 | [diff] [blame] | 1634 | |
Gary Peck | e8bc5d7 | 2012-12-21 06:45:25 -0800 | [diff] [blame] | 1635 | return wiped; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1636 | } |
| 1637 | |
Gary Peck | 43acadf | 2012-11-21 21:19:01 -0800 | [diff] [blame] | 1638 | bool TWPartition::Wipe() { |
Gary Peck | 82599a8 | 2012-11-21 16:23:12 -0800 | [diff] [blame] | 1639 | if (Is_File_System(Current_File_System)) |
| 1640 | return Wipe(Current_File_System); |
| 1641 | else |
| 1642 | return Wipe(Fstab_File_System); |
Gary Peck | 43acadf | 2012-11-21 21:19:01 -0800 | [diff] [blame] | 1643 | } |
| 1644 | |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 1645 | bool TWPartition::Wipe_AndSec(void) { |
| 1646 | if (!Has_Android_Secure) |
| 1647 | return false; |
| 1648 | |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 1649 | if (!Mount(true)) |
| 1650 | return false; |
| 1651 | |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1652 | gui_msg(Msg("wiping=Wiping {1}")(Backup_Display_Name)); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1653 | TWFunc::removeDir(Mount_Point + "/.android_secure/", true); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1654 | return true; |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 1655 | } |
| 1656 | |
Ethan Yonker | 87c7bac | 2014-05-25 21:41:08 -0500 | [diff] [blame] | 1657 | bool TWPartition::Can_Repair() { |
Ethan Yonker | a271915 | 2015-05-28 09:44:41 -0500 | [diff] [blame] | 1658 | if (Mount_Read_Only) |
| 1659 | return false; |
Matt Mower | 18794c8 | 2015-11-11 16:22:45 -0600 | [diff] [blame] | 1660 | if (Current_File_System == "vfat" && TWFunc::Path_Exists("/sbin/fsck.fat")) |
Ethan Yonker | 87c7bac | 2014-05-25 21:41:08 -0500 | [diff] [blame] | 1661 | return true; |
| 1662 | else if ((Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") && TWFunc::Path_Exists("/sbin/e2fsck")) |
| 1663 | return true; |
| 1664 | else if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/fsck.exfat")) |
| 1665 | return true; |
| 1666 | else if (Current_File_System == "f2fs" && TWFunc::Path_Exists("/sbin/fsck.f2fs")) |
| 1667 | return true; |
Greg Wallace | b39e6c6 | 2015-12-29 00:55:26 -0500 | [diff] [blame] | 1668 | 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] | 1669 | return true; |
Ethan Yonker | 87c7bac | 2014-05-25 21:41:08 -0500 | [diff] [blame] | 1670 | return false; |
| 1671 | } |
| 1672 | |
| 1673 | bool TWPartition::Repair() { |
| 1674 | string command; |
| 1675 | |
| 1676 | if (Current_File_System == "vfat") { |
Matt Mower | 18794c8 | 2015-11-11 16:22:45 -0600 | [diff] [blame] | 1677 | if (!TWFunc::Path_Exists("/sbin/fsck.fat")) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1678 | 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] | 1679 | return false; |
| 1680 | } |
| 1681 | if (!UnMount(true)) |
| 1682 | return false; |
Greg Wallace | 2d65340 | 2015-12-29 14:19:21 -0500 | [diff] [blame] | 1683 | 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] | 1684 | Find_Actual_Block_Device(); |
Matt Mower | 18794c8 | 2015-11-11 16:22:45 -0600 | [diff] [blame] | 1685 | command = "/sbin/fsck.fat -y " + Actual_Block_Device; |
Ethan Yonker | 87c7bac | 2014-05-25 21:41:08 -0500 | [diff] [blame] | 1686 | LOGINFO("Repair command: %s\n", command.c_str()); |
| 1687 | if (TWFunc::Exec_Cmd(command) == 0) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1688 | gui_msg("done=Done."); |
Ethan Yonker | 87c7bac | 2014-05-25 21:41:08 -0500 | [diff] [blame] | 1689 | return true; |
| 1690 | } else { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1691 | 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] | 1692 | return false; |
| 1693 | } |
| 1694 | } |
| 1695 | if (Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") { |
| 1696 | if (!TWFunc::Path_Exists("/sbin/e2fsck")) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1697 | 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] | 1698 | return false; |
| 1699 | } |
| 1700 | if (!UnMount(true)) |
| 1701 | return false; |
Greg Wallace | 2d65340 | 2015-12-29 14:19:21 -0500 | [diff] [blame] | 1702 | gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("e2fsck")); |
Ethan Yonker | 87c7bac | 2014-05-25 21:41:08 -0500 | [diff] [blame] | 1703 | Find_Actual_Block_Device(); |
Ethan Yonker | a271915 | 2015-05-28 09:44:41 -0500 | [diff] [blame] | 1704 | command = "/sbin/e2fsck -fp " + Actual_Block_Device; |
Ethan Yonker | 87c7bac | 2014-05-25 21:41:08 -0500 | [diff] [blame] | 1705 | LOGINFO("Repair command: %s\n", command.c_str()); |
| 1706 | if (TWFunc::Exec_Cmd(command) == 0) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1707 | gui_msg("done=Done."); |
Ethan Yonker | 87c7bac | 2014-05-25 21:41:08 -0500 | [diff] [blame] | 1708 | return true; |
| 1709 | } else { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1710 | 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] | 1711 | return false; |
| 1712 | } |
| 1713 | } |
| 1714 | if (Current_File_System == "exfat") { |
| 1715 | if (!TWFunc::Path_Exists("/sbin/fsck.exfat")) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1716 | 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] | 1717 | return false; |
| 1718 | } |
| 1719 | if (!UnMount(true)) |
| 1720 | return false; |
Greg Wallace | 2d65340 | 2015-12-29 14:19:21 -0500 | [diff] [blame] | 1721 | 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] | 1722 | Find_Actual_Block_Device(); |
| 1723 | command = "/sbin/fsck.exfat " + Actual_Block_Device; |
| 1724 | LOGINFO("Repair command: %s\n", command.c_str()); |
| 1725 | if (TWFunc::Exec_Cmd(command) == 0) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1726 | gui_msg("done=Done."); |
Ethan Yonker | 87c7bac | 2014-05-25 21:41:08 -0500 | [diff] [blame] | 1727 | return true; |
| 1728 | } else { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1729 | 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] | 1730 | return false; |
| 1731 | } |
| 1732 | } |
| 1733 | if (Current_File_System == "f2fs") { |
| 1734 | if (!TWFunc::Path_Exists("/sbin/fsck.f2fs")) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1735 | 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] | 1736 | return false; |
| 1737 | } |
| 1738 | if (!UnMount(true)) |
| 1739 | return false; |
Greg Wallace | 2d65340 | 2015-12-29 14:19:21 -0500 | [diff] [blame] | 1740 | 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] | 1741 | Find_Actual_Block_Device(); |
| 1742 | command = "/sbin/fsck.f2fs " + Actual_Block_Device; |
| 1743 | LOGINFO("Repair command: %s\n", command.c_str()); |
| 1744 | if (TWFunc::Exec_Cmd(command) == 0) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1745 | gui_msg("done=Done."); |
Ethan Yonker | 87c7bac | 2014-05-25 21:41:08 -0500 | [diff] [blame] | 1746 | return true; |
| 1747 | } else { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1748 | 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] | 1749 | return false; |
| 1750 | } |
| 1751 | } |
Ethan Yonker | b81d905 | 2015-07-09 13:20:53 -0500 | [diff] [blame] | 1752 | if (Current_File_System == "ntfs") { |
Greg Wallace | b39e6c6 | 2015-12-29 00:55:26 -0500 | [diff] [blame] | 1753 | string Ntfsfix_Binary; |
| 1754 | if (TWFunc::Path_Exists("/sbin/ntfsfix")) |
| 1755 | Ntfsfix_Binary = "ntfsfix"; |
| 1756 | else if (TWFunc::Path_Exists("/sbin/fsck.ntfs")) |
| 1757 | Ntfsfix_Binary = "fsck.ntfs"; |
| 1758 | else { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1759 | 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] | 1760 | return false; |
| 1761 | } |
| 1762 | if (!UnMount(true)) |
| 1763 | return false; |
Greg Wallace | |