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