blob: 5626e5c4b3750b8c8e97da03318efc36a6ac6103 [file] [log] [blame]
Dees Troy3be70a82013-10-22 14:25:12 +00001/*
bigbiff bigbiff56cf5642016-08-19 17:43:45 -04002 Copyright 2013 to 2017 TeamWin
Dees Troy3be70a82013-10-22 14:25:12 +00003 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_Troy51a0e822012-09-05 15:24:24 -040018
19#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
22#include <sys/stat.h>
23#include <sys/vfs.h>
Dees_Troy5bf43922012-09-07 16:07:55 -040024#include <sys/mount.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040025#include <unistd.h>
Dees_Troy51127312012-09-08 13:08:49 -040026#include <dirent.h>
thatf54e5392016-01-29 22:04:43 +010027#include <libgen.h>
bigbiffce8f83c2015-12-12 18:30:21 -050028#include <zlib.h>
bigbiff bigbiff9c754052013-01-09 09:09:08 -050029#include <iostream>
30#include <sstream>
Ethan Yonker66a19492015-12-10 10:19:45 -060031#include <sys/param.h>
Ethan Yonker472f5062016-02-25 13:47:30 -060032#include <fcntl.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040033
Ethan Yonker007de982018-08-31 14:23:16 -050034#include "cutils/properties.h"
bigbiff7b4c7a62015-01-01 19:44:14 -050035#include "libblkid/include/blkid.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040036#include "variables.h"
Dees_Troy2673cec2013-04-02 20:22:16 +000037#include "twcommon.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040038#include "partitions.hpp"
Dees_Troy5bf43922012-09-07 16:07:55 -040039#include "data.hpp"
Dees_Troy43d8b002012-09-17 16:00:01 -040040#include "twrp-functions.hpp"
bigbiff bigbiff9c754052013-01-09 09:09:08 -050041#include "twrpTar.hpp"
Ethan Yonker3fdcda42016-11-30 12:29:37 -060042#include "exclude.hpp"
Ethan Yonker1b7a31b2014-07-03 15:09:22 -050043#include "infomanager.hpp"
Ethan Yonker4b94cfd2014-12-11 10:00:45 -060044#include "set_metadata.h"
Ethan Yonker74db1572015-10-28 12:44:49 -050045#include "gui/gui.hpp"
bigbiffce8f83c2015-12-12 18:30:21 -050046#include "adbbu/libtwadbbu.hpp"
Ethan Yonker98661c12018-10-17 08:39:28 -050047#ifdef TW_INCLUDE_CRYPTO
48 #include "crypto/fde/cryptfs.h"
49 #ifdef TW_INCLUDE_FBE
50 #include "crypto/ext4crypt/Decrypt.h"
51 #endif
52#else
53 #define CRYPT_FOOTER_OFFSET 0x4000
54#endif
Dees_Troy5bf43922012-09-07 16:07:55 -040055extern "C" {
Dees_Troy38bd7602012-09-14 13:33:53 -040056 #include "mtdutils/mtdutils.h"
57 #include "mtdutils/mounts.h"
Dees_Troya95f55c2013-08-17 13:14:43 +000058#ifdef USE_EXT4
Ethan Yonker8373cfe2017-09-08 06:50:54 -050059 // #include "make_ext4fs.h" TODO need ifdef for android8
60 #include <ext4_utils/make_ext4fs.h>
Dees_Troya95f55c2013-08-17 13:14:43 +000061#endif
Ethan Yonker71413f42014-02-26 13:36:08 -060062#ifdef TW_INCLUDE_CRYPTO
Ethan Yonker66a19492015-12-10 10:19:45 -060063 #include "gpt/gpt.h"
Ethan Yonker71413f42014-02-26 13:36:08 -060064#endif
Dees_Troy5bf43922012-09-07 16:07:55 -040065}
Matt Mower87413642017-01-17 21:14:46 -060066#include <selinux/selinux.h>
Ethan Yonkerf27497f2014-02-09 11:48:33 -060067#include <selinux/label.h>
Dees Troy4159aed2014-02-28 17:24:43 +000068#ifdef HAVE_CAPABILITIES
69#include <sys/capability.h>
70#include <sys/xattr.h>
71#include <linux/xattr.h>
72#endif
HashBanged974bb2016-01-30 14:20:09 -050073#include <sparse_format.h>
Ethan Yonker472f5062016-02-25 13:47:30 -060074#include "progresstracking.hpp"
Dees_Troy51a0e822012-09-05 15:24:24 -040075
bigbiff bigbiff9c754052013-01-09 09:09:08 -050076using namespace std;
77
Ethan Yonker6e8c27a2016-12-22 17:55:57 -060078static 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
79
Dees_Troya95f55c2013-08-17 13:14:43 +000080extern struct selabel_handle *selinux_handle;
Ethan Yonker6277c792014-09-15 14:54:30 -050081extern bool datamedia;
Dees_Troya95f55c2013-08-17 13:14:43 +000082
Hashcode62bd9e02013-11-19 21:59:42 -080083struct flag_list {
84 const char *name;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -060085 unsigned long flag;
Hashcode62bd9e02013-11-19 21:59:42 -080086};
87
Matt Mower4ab42b12016-04-21 13:52:18 -050088const struct flag_list mount_flags[] = {
Ethan Yonker6e8c27a2016-12-22 17:55:57 -060089 { "noatime", MS_NOATIME },
90 { "noexec", MS_NOEXEC },
91 { "nosuid", MS_NOSUID },
92 { "nodev", MS_NODEV },
93 { "nodiratime", MS_NODIRATIME },
94 { "ro", MS_RDONLY },
95 { "rw", 0 },
96 { "remount", MS_REMOUNT },
97 { "bind", MS_BIND },
98 { "rec", MS_REC },
Dees Troyc4bc30e2014-02-03 15:04:19 +000099#ifdef MS_UNBINDABLE
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600100 { "unbindable", MS_UNBINDABLE },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000101#endif
102#ifdef MS_PRIVATE
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600103 { "private", MS_PRIVATE },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000104#endif
105#ifdef MS_SLAVE
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600106 { "slave", MS_SLAVE },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000107#endif
108#ifdef MS_SHARED
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600109 { "shared", MS_SHARED },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000110#endif
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600111 { "sync", MS_SYNCHRONOUS },
112 { 0, 0 },
113};
114
115const char *ignored_mount_items[] = {
116 "defaults=",
117 "errors=",
Ethan Yonker93382822018-11-01 15:25:31 -0500118 "latemount",
119 "sysfs_path=",
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600120 NULL
Hashcode62bd9e02013-11-19 21:59:42 -0800121};
122
Matt Mower2416a502016-04-12 19:54:46 -0500123enum TW_FSTAB_FLAGS {
124 TWFLAG_DEFAULTS, // Retain position
125 TWFLAG_ANDSEC,
126 TWFLAG_BACKUP,
127 TWFLAG_BACKUPNAME,
128 TWFLAG_BLOCKSIZE,
129 TWFLAG_CANBEWIPED,
130 TWFLAG_CANENCRYPTBACKUP,
131 TWFLAG_DISPLAY,
132 TWFLAG_ENCRYPTABLE,
Ethan Yonkere9afc3d2018-08-30 15:16:27 -0500133 TWFLAG_FILEENCRYPTION,
Matt Mower2416a502016-04-12 19:54:46 -0500134 TWFLAG_FLASHIMG,
135 TWFLAG_FORCEENCRYPT,
136 TWFLAG_FSFLAGS,
137 TWFLAG_IGNOREBLKID,
138 TWFLAG_LENGTH,
139 TWFLAG_MOUNTTODECRYPT,
140 TWFLAG_REMOVABLE,
141 TWFLAG_RETAINLAYOUTVERSION,
142 TWFLAG_SETTINGSSTORAGE,
143 TWFLAG_STORAGE,
144 TWFLAG_STORAGENAME,
145 TWFLAG_SUBPARTITIONOF,
146 TWFLAG_SYMLINK,
147 TWFLAG_USERDATAENCRYPTBACKUP,
148 TWFLAG_USERMRF,
149 TWFLAG_WIPEDURINGFACTORYRESET,
150 TWFLAG_WIPEINGUI,
Ethan Yonker1b190162016-12-05 15:25:19 -0600151 TWFLAG_SLOTSELECT,
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600152 TWFLAG_WAIT,
153 TWFLAG_VERIFY,
154 TWFLAG_CHECK,
155 TWFLAG_ALTDEVICE,
156 TWFLAG_NOTRIM,
157 TWFLAG_VOLDMANAGED,
158 TWFLAG_FORMATTABLE,
159 TWFLAG_RESIZE,
Ethan Yonker93382822018-11-01 15:25:31 -0500160 TWFLAG_KEYDIRECTORY,
Peter Cai28ea7b02019-05-24 11:38:54 +0800161 TWFLAG_WRAPPEDKEY,
Matt Mower2416a502016-04-12 19:54:46 -0500162};
163
164/* Flags without a trailing '=' are considered dual format flags and can be
165 * written as either 'flagname' or 'flagname=', where the character following
166 * the '=' is Y,y,1 for true and false otherwise.
167 */
168const struct flag_list tw_flags[] = {
169 { "andsec", TWFLAG_ANDSEC },
170 { "backup", TWFLAG_BACKUP },
171 { "backupname=", TWFLAG_BACKUPNAME },
172 { "blocksize=", TWFLAG_BLOCKSIZE },
173 { "canbewiped", TWFLAG_CANBEWIPED },
174 { "canencryptbackup", TWFLAG_CANENCRYPTBACKUP },
175 { "defaults", TWFLAG_DEFAULTS },
176 { "display=", TWFLAG_DISPLAY },
177 { "encryptable=", TWFLAG_ENCRYPTABLE },
Ethan Yonkere9afc3d2018-08-30 15:16:27 -0500178 { "fileencryption=", TWFLAG_FILEENCRYPTION },
Matt Mower2416a502016-04-12 19:54:46 -0500179 { "flashimg", TWFLAG_FLASHIMG },
180 { "forceencrypt=", TWFLAG_FORCEENCRYPT },
181 { "fsflags=", TWFLAG_FSFLAGS },
182 { "ignoreblkid", TWFLAG_IGNOREBLKID },
183 { "length=", TWFLAG_LENGTH },
184 { "mounttodecrypt", TWFLAG_MOUNTTODECRYPT },
185 { "removable", TWFLAG_REMOVABLE },
186 { "retainlayoutversion", TWFLAG_RETAINLAYOUTVERSION },
187 { "settingsstorage", TWFLAG_SETTINGSSTORAGE },
188 { "storage", TWFLAG_STORAGE },
189 { "storagename=", TWFLAG_STORAGENAME },
190 { "subpartitionof=", TWFLAG_SUBPARTITIONOF },
191 { "symlink=", TWFLAG_SYMLINK },
192 { "userdataencryptbackup", TWFLAG_USERDATAENCRYPTBACKUP },
193 { "usermrf", TWFLAG_USERMRF },
194 { "wipeduringfactoryreset", TWFLAG_WIPEDURINGFACTORYRESET },
195 { "wipeingui", TWFLAG_WIPEINGUI },
Ethan Yonker1b190162016-12-05 15:25:19 -0600196 { "slotselect", TWFLAG_SLOTSELECT },
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600197 { "wait", TWFLAG_WAIT },
198 { "verify", TWFLAG_VERIFY },
199 { "check", TWFLAG_CHECK },
200 { "altdevice", TWFLAG_ALTDEVICE },
201 { "notrim", TWFLAG_NOTRIM },
202 { "voldmanaged=", TWFLAG_VOLDMANAGED },
203 { "formattable", TWFLAG_FORMATTABLE },
204 { "resize", TWFLAG_RESIZE },
Ethan Yonker93382822018-11-01 15:25:31 -0500205 { "keydirectory=", TWFLAG_KEYDIRECTORY },
Peter Cai28ea7b02019-05-24 11:38:54 +0800206 { "wrappedkey", TWFLAG_WRAPPEDKEY },
Matt Mower2416a502016-04-12 19:54:46 -0500207 { 0, 0 },
208};
209
that9e0593e2014-10-08 00:01:24 +0200210TWPartition::TWPartition() {
Dees_Troy51a0e822012-09-05 15:24:24 -0400211 Can_Be_Mounted = false;
212 Can_Be_Wiped = false;
Dees_Troya13d74f2013-03-24 08:54:55 -0500213 Can_Be_Backed_Up = false;
Vojtech Bocek1dc30982013-08-30 21:49:30 +0200214 Use_Rm_Rf = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400215 Wipe_During_Factory_Reset = false;
216 Wipe_Available_in_GUI = false;
217 Is_SubPartition = false;
Dees_Troy2691f9d2012-09-24 11:15:49 -0400218 Has_SubPartition = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400219 SubPartition_Of = "";
220 Symlink_Path = "";
221 Symlink_Mount_Point = "";
222 Mount_Point = "";
Dees_Troye58d5262012-09-21 12:27:57 -0400223 Backup_Path = "";
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600224 Wildcard_Block_Device = false;
225 Sysfs_Entry = "";
Dees_Troy38bd7602012-09-14 13:33:53 -0400226 Actual_Block_Device = "";
227 Primary_Block_Device = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400228 Alternate_Block_Device = "";
229 Removable = false;
230 Is_Present = false;
231 Length = 0;
232 Size = 0;
233 Used = 0;
234 Free = 0;
235 Backup_Size = 0;
236 Can_Be_Encrypted = false;
237 Is_Encrypted = false;
238 Is_Decrypted = false;
Ethan Yonkerbd7492d2016-12-07 13:55:01 -0600239 Is_FBE = false;
Ethan Yonker253368a2014-11-25 15:00:52 -0600240 Mount_To_Decrypt = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400241 Decrypted_Block_Device = "";
242 Display_Name = "";
Dees_Troya13d74f2013-03-24 08:54:55 -0500243 Backup_Display_Name = "";
244 Storage_Name = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400245 Backup_Name = "";
Dees_Troy63c8df72012-09-10 14:02:05 -0400246 Backup_FileName = "";
Dees_Troy38bd7602012-09-14 13:33:53 -0400247 MTD_Name = "";
bigbiffce8f83c2015-12-12 18:30:21 -0500248 Backup_Method = BM_NONE;
Dees_Troy83bd4832013-05-04 12:39:56 +0000249 Can_Encrypt_Backup = false;
250 Use_Userdata_Encryption = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400251 Has_Data_Media = false;
Dees_Troye58d5262012-09-21 12:27:57 -0400252 Has_Android_Secure = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400253 Is_Storage = false;
Dees_Troya13d74f2013-03-24 08:54:55 -0500254 Is_Settings_Storage = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400255 Storage_Path = "";
256 Current_File_System = "";
257 Fstab_File_System = "";
Hashcode62bd9e02013-11-19 21:59:42 -0800258 Mount_Flags = 0;
259 Mount_Options = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400260 Format_Block_Size = 0;
Dees_Troy68cab492012-12-12 19:29:35 +0000261 Ignore_Blkid = false;
Dees_Troy16c2b312013-01-15 16:51:18 +0000262 Retain_Layout_Version = false;
dianlujitao4879b372018-12-03 18:45:47 +0800263 Crypto_Key_Location = "";
Ethan Yonker726a0202014-12-16 20:01:38 -0600264 MTP_Storage_ID = 0;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600265 Can_Flash_Img = false;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500266 Mount_Read_Only = false;
Ethan Yonker66a19492015-12-10 10:19:45 -0600267 Is_Adopted_Storage = false;
268 Adopted_GUID = "";
Ethan Yonker1b190162016-12-05 15:25:19 -0600269 SlotSelect = false;
Ethan Yonker93382822018-11-01 15:25:31 -0500270 Key_Directory = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400271}
272
273TWPartition::~TWPartition(void) {
274 // Do nothing
275}
276
Chaosmasterf6e42ce2020-01-27 00:17:04 +0100277bool TWPartition::Process_Fstab_Line(const char *fstab_line, bool Display_Error, std::map<string, Flags_Map> *twrp_flags, bool Sar_Detect) {
Matt Mower2b2dd152016-04-26 11:24:08 -0500278 char full_line[MAX_FSTAB_LINE_LENGTH];
Matt Mower2416a502016-04-12 19:54:46 -0500279 char twflags[MAX_FSTAB_LINE_LENGTH] = "";
Dees_Troy5bf43922012-09-07 16:07:55 -0400280 char* ptr;
Matt Mower2b2dd152016-04-26 11:24:08 -0500281 int line_len = strlen(fstab_line), index = 0, item_index = 0;
Dees_Troya13d74f2013-03-24 08:54:55 -0500282 bool skip = false;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600283 int fstab_version = 1, mount_point_index = 0, fs_index = 1, block_device_index = 2;
284 TWPartition *additional_entry = NULL;
285 std::map<string, Flags_Map>::iterator it;
Dees_Troy5bf43922012-09-07 16:07:55 -0400286
Matt Mower2b2dd152016-04-26 11:24:08 -0500287 strlcpy(full_line, fstab_line, sizeof(full_line));
Dees_Troy51127312012-09-08 13:08:49 -0400288 for (index = 0; index < line_len; index++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500289 if (full_line[index] == 34)
290 skip = !skip;
291 if (!skip && full_line[index] <= 32)
Dees_Troy5bf43922012-09-07 16:07:55 -0400292 full_line[index] = '\0';
Dees_Troy5bf43922012-09-07 16:07:55 -0400293 }
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600294 if (line_len < 10)
295 return false; // There can't possibly be a valid fstab line that is less than 10 chars
296 if (strncmp(fstab_line, "/dev/", strlen("/dev/")) == 0 || strncmp(fstab_line, "/devices/", strlen("/devices/")) == 0) {
297 fstab_version = 2;
298 block_device_index = 0;
299 mount_point_index = 1;
300 fs_index = 2;
301 }
302
303 index = 0;
Dees_Troy5bf43922012-09-07 16:07:55 -0400304 while (index < line_len) {
305 while (index < line_len && full_line[index] == '\0')
306 index++;
307 if (index >= line_len)
308 continue;
309 ptr = full_line + index;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600310 if (item_index == mount_point_index) {
311 Mount_Point = ptr;
312 if (fstab_version == 2) {
313 additional_entry = PartitionManager.Find_Partition_By_Path(Mount_Point);
Chaosmasterf6e42ce2020-01-27 00:17:04 +0100314 if (!Sar_Detect && additional_entry) {
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600315 LOGINFO("Found an additional entry for '%s'\n", Mount_Point.c_str());
316 }
317 }
Chaosmasterf6e42ce2020-01-27 00:17:04 +0100318 if(!Sar_Detect)
319 LOGINFO("Processing '%s'\n", Mount_Point.c_str());
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600320 Backup_Path = Mount_Point;
321 Storage_Path = Mount_Point;
322 Display_Name = ptr + 1;
323 Backup_Display_Name = Display_Name;
324 Storage_Name = Display_Name;
325 item_index++;
326 } else if (item_index == fs_index) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400327 // File System
328 Fstab_File_System = ptr;
329 Current_File_System = ptr;
330 item_index++;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600331 } else if (item_index == block_device_index) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400332 // Primary Block Device
Dees_Troy38bd7602012-09-14 13:33:53 -0400333 if (Fstab_File_System == "mtd" || Fstab_File_System == "yaffs2") {
Dees_Troy094207a2012-09-26 12:00:39 -0400334 MTD_Name = ptr;
335 Find_MTD_Block_Device(MTD_Name);
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400336 } else if (Fstab_File_System == "bml") {
337 if (Mount_Point == "/boot")
338 MTD_Name = "boot";
339 else if (Mount_Point == "/recovery")
340 MTD_Name = "recovery";
341 Primary_Block_Device = ptr;
342 if (*ptr != '/')
Dees_Troy2673cec2013-04-02 20:22:16 +0000343 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_Troy38bd7602012-09-14 13:33:53 -0400344 } else if (*ptr != '/') {
Dees_Troy5bf43922012-09-07 16:07:55 -0400345 if (Display_Error)
Matt Mower2b2dd152016-04-26 11:24:08 -0500346 LOGERR("Invalid block device '%s' in fstab line '%s'", ptr, fstab_line);
Dees_Troy5bf43922012-09-07 16:07:55 -0400347 else
Matt Mower2b2dd152016-04-26 11:24:08 -0500348 LOGINFO("Invalid block device '%s' in fstab line '%s'", ptr, fstab_line);
Matt Mower72c87ce2016-04-26 14:34:56 -0500349 return false;
Dees_Troy38bd7602012-09-14 13:33:53 -0400350 } else {
351 Primary_Block_Device = ptr;
352 Find_Real_Block_Device(Primary_Block_Device, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400353 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400354 item_index++;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600355 } else if (item_index > 2) {
356 if (fstab_version == 2) {
357 if (item_index == 3) {
358 Process_FS_Flags(ptr);
359 if (additional_entry) {
360 additional_entry->Save_FS_Flags(Fstab_File_System, Mount_Flags, Mount_Options);
361 return false; // We save the extra fs flags in the other partition entry and by returning false, this entry will be deleted
362 }
363 } else {
364 strlcpy(twflags, ptr, sizeof(twflags));
365 }
366 item_index++;
367 } else if (*ptr == '/') { // v2 fstab does not allow alternate block devices
Dees_Troy5bf43922012-09-07 16:07:55 -0400368 // Alternate Block Device
369 Alternate_Block_Device = ptr;
370 Find_Real_Block_Device(Alternate_Block_Device, Display_Error);
371 } else if (strlen(ptr) > 7 && strncmp(ptr, "length=", 7) == 0) {
372 // Partition length
373 ptr += 7;
374 Length = atoi(ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400375 } else if (strlen(ptr) > 6 && strncmp(ptr, "flags=", 6) == 0) {
376 // Custom flags, save for later so that new values aren't overwritten by defaults
377 ptr += 6;
Matt Mower2416a502016-04-12 19:54:46 -0500378 strlcpy(twflags, ptr, sizeof(twflags));
Dees_Troy38bd7602012-09-14 13:33:53 -0400379 } else if (strlen(ptr) == 4 && (strncmp(ptr, "NULL", 4) == 0 || strncmp(ptr, "null", 4) == 0 || strncmp(ptr, "null", 4) == 0)) {
380 // Do nothing
Dees_Troy5bf43922012-09-07 16:07:55 -0400381 } else {
382 // Unhandled data
Matt Mower2b2dd152016-04-26 11:24:08 -0500383 LOGINFO("Unhandled fstab information '%s' in fstab line '%s'\n", ptr, fstab_line);
Dees_Troy5bf43922012-09-07 16:07:55 -0400384 }
385 }
386 while (index < line_len && full_line[index] != '\0')
387 index++;
388 }
389
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600390 // override block devices from the v2 fstab with the ones we read from the twrp.flags file in case they are different
391 if (fstab_version == 2 && twrp_flags && twrp_flags->size() > 0) {
392 it = twrp_flags->find(Mount_Point);
393 if (it != twrp_flags->end()) {
394 if (!it->second.Primary_Block_Device.empty()) {
395 Primary_Block_Device = it->second.Primary_Block_Device;
396 Find_Real_Block_Device(Primary_Block_Device, Display_Error);
397 }
398 if (!it->second.Alternate_Block_Device.empty()) {
399 Alternate_Block_Device = it->second.Alternate_Block_Device;
400 Find_Real_Block_Device(Alternate_Block_Device, Display_Error);
401 }
402 }
403 }
404
405 if (strncmp(fstab_line, "/devices/", strlen("/devices/")) == 0) {
406 Sysfs_Entry = Primary_Block_Device;
407 Primary_Block_Device = "";
408 Is_Storage = true;
409 Removable = true;
410 Wipe_Available_in_GUI = true;
411 Wildcard_Block_Device = true;
412 }
413 if (Primary_Block_Device.find("*") != string::npos)
414 Wildcard_Block_Device = true;
415
Chaosmasterf6e42ce2020-01-27 00:17:04 +0100416 if (Sar_Detect) {
417 if(Is_File_System(Fstab_File_System) && (Mount_Point == "/" || Mount_Point == "/system" || Mount_Point == "/system_root"))
418 Find_Actual_Block_Device();
419 else
420 return true;
421 } else if (Mount_Point == "auto") {
dianlujitaob76a73a2020-04-30 20:33:20 +0800422 Mount_Point = "/auto" + to_string(auto_index);
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600423 Backup_Path = Mount_Point;
424 Storage_Path = Mount_Point;
dianlujitaob76a73a2020-04-30 20:33:20 +0800425 Backup_Name = Mount_Point.substr(1);
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600426 auto_index++;
427 Setup_File_System(Display_Error);
428 Display_Name = "Storage";
429 Backup_Display_Name = Display_Name;
430 Storage_Name = Display_Name;
431 Can_Be_Backed_Up = false;
432 Wipe_Available_in_GUI = true;
433 Is_Storage = true;
434 Removable = true;
435 Wipe_Available_in_GUI = true;
436 } else if (!Is_File_System(Fstab_File_System) && !Is_Image(Fstab_File_System)) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400437 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000438 LOGERR("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400439 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000440 LOGINFO("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Matt Mower72c87ce2016-04-26 14:34:56 -0500441 return false;
Dees_Troy5bf43922012-09-07 16:07:55 -0400442 } else if (Is_File_System(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400443 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400444 Setup_File_System(Display_Error);
dianlujitaob76a73a2020-04-30 20:33:20 +0800445 Backup_Name = Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
Chaosmaster3b682732020-01-25 21:09:28 +0100446 if (Mount_Point == "/" || Mount_Point == "/system" || Mount_Point == "/system_root") {
Dees_Troy5bf43922012-09-07 16:07:55 -0400447 Display_Name = "System";
dianlujitaob76a73a2020-04-30 20:33:20 +0800448 Backup_Name = "system";
Dees_Troya13d74f2013-03-24 08:54:55 -0500449 Backup_Display_Name = Display_Name;
450 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400451 Wipe_Available_in_GUI = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500452 Can_Be_Backed_Up = true;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500453 Mount_Read_Only = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400454 } else if (Mount_Point == "/data") {
455 Display_Name = "Data";
Dees_Troya13d74f2013-03-24 08:54:55 -0500456 Backup_Display_Name = Display_Name;
457 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400458 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400459 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500460 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000461 Can_Encrypt_Backup = true;
462 Use_Userdata_Encryption = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400463 } else if (Mount_Point == "/cache") {
464 Display_Name = "Cache";
Dees_Troya13d74f2013-03-24 08:54:55 -0500465 Backup_Display_Name = Display_Name;
466 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400467 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400468 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500469 Can_Be_Backed_Up = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400470 } else if (Mount_Point == "/datadata") {
Dees_Troy51127312012-09-08 13:08:49 -0400471 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400472 Display_Name = "DataData";
Dees_Troya13d74f2013-03-24 08:54:55 -0500473 Backup_Display_Name = Display_Name;
474 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400475 Is_SubPartition = true;
476 SubPartition_Of = "/data";
Dees_Troy5bf43922012-09-07 16:07:55 -0400477 DataManager::SetValue(TW_HAS_DATADATA, 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500478 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000479 Can_Encrypt_Backup = true;
480 Use_Userdata_Encryption = false; // This whole partition should be encrypted
Dees_Troy5bf43922012-09-07 16:07:55 -0400481 } else if (Mount_Point == "/sd-ext") {
Dees_Troy51127312012-09-08 13:08:49 -0400482 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400483 Display_Name = "SD-Ext";
Dees_Troya13d74f2013-03-24 08:54:55 -0500484 Backup_Display_Name = Display_Name;
485 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400486 Wipe_Available_in_GUI = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400487 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500488 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000489 Can_Encrypt_Backup = true;
490 Use_Userdata_Encryption = true;
Dees_Troy2c50e182012-09-26 20:05:28 -0400491 } else if (Mount_Point == "/boot") {
492 Display_Name = "Boot";
Dees_Troya13d74f2013-03-24 08:54:55 -0500493 Backup_Display_Name = Display_Name;
Dees_Troy2c50e182012-09-26 20:05:28 -0400494 DataManager::SetValue("tw_boot_is_mountable", 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500495 Can_Be_Backed_Up = true;
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500496 } else if (Mount_Point == "/vendor") {
497 Display_Name = "Vendor";
498 Backup_Display_Name = Display_Name;
499 Storage_Name = Display_Name;
500 Mount_Read_Only = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400501 }
502#ifdef TW_EXTERNAL_STORAGE_PATH
503 if (Mount_Point == EXPAND(TW_EXTERNAL_STORAGE_PATH)) {
504 Is_Storage = true;
505 Storage_Path = EXPAND(TW_EXTERNAL_STORAGE_PATH);
Dees_Troyc51f1f92012-09-20 15:32:13 -0400506 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500507 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400508#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000509 if (Mount_Point == "/sdcard" || Mount_Point == "/external_sd" || Mount_Point == "/external_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400510 Is_Storage = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400511 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500512 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400513#endif
Dees_Troyb05ddee2013-01-28 20:24:50 +0000514 }
Dees_Troy8170a922012-09-18 15:40:25 -0400515#ifdef TW_INTERNAL_STORAGE_PATH
516 if (Mount_Point == EXPAND(TW_INTERNAL_STORAGE_PATH)) {
517 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500518 Is_Settings_Storage = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400519 Storage_Path = EXPAND(TW_INTERNAL_STORAGE_PATH);
Dees_Troya13d74f2013-03-24 08:54:55 -0500520 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400521 }
522#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000523 if (Mount_Point == "/emmc" || Mount_Point == "/internal_sd" || Mount_Point == "/internal_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400524 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500525 Is_Settings_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500526 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400527 }
528#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400529 } else if (Is_Image(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400530 Find_Actual_Block_Device();
Ethan Yonker1b190162016-12-05 15:25:19 -0600531 Setup_Image();
Dees_Troya13d74f2013-03-24 08:54:55 -0500532 if (Mount_Point == "/boot") {
533 Display_Name = "Boot";
534 Backup_Display_Name = Display_Name;
535 Can_Be_Backed_Up = true;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600536 Can_Flash_Img = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500537 } else if (Mount_Point == "/recovery") {
538 Display_Name = "Recovery";
539 Backup_Display_Name = Display_Name;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600540 Can_Flash_Img = true;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500541 } else if (Mount_Point == "/system_image") {
542 Display_Name = "System Image";
543 Backup_Display_Name = Display_Name;
HashBanged974bb2016-01-30 14:20:09 -0500544 Can_Flash_Img = true;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500545 Can_Be_Backed_Up = true;
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500546 } else if (Mount_Point == "/vendor_image") {
547 Display_Name = "Vendor Image";
548 Backup_Display_Name = Display_Name;
HashBanged974bb2016-01-30 14:20:09 -0500549 Can_Flash_Img = true;
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500550 Can_Be_Backed_Up = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500551 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400552 }
553
Matt Mower2416a502016-04-12 19:54:46 -0500554 // Process TWRP fstab flags
555 if (strlen(twflags) > 0) {
556 string Prev_Display_Name = Display_Name;
557 string Prev_Storage_Name = Storage_Name;
558 string Prev_Backup_Display_Name = Backup_Display_Name;
559 Display_Name = "";
560 Storage_Name = "";
561 Backup_Display_Name = "";
562
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600563 Process_TW_Flags(twflags, (fstab_version == 1), fstab_version);
564 Save_FS_Flags(Fstab_File_System, Mount_Flags, Mount_Options);
Matt Mower2416a502016-04-12 19:54:46 -0500565
566 bool has_display_name = !Display_Name.empty();
567 bool has_storage_name = !Storage_Name.empty();
568 bool has_backup_name = !Backup_Display_Name.empty();
569 if (!has_display_name) Display_Name = Prev_Display_Name;
570 if (!has_storage_name) Storage_Name = Prev_Storage_Name;
571 if (!has_backup_name) Backup_Display_Name = Prev_Backup_Display_Name;
572
573 if (has_display_name && !has_storage_name)
574 Storage_Name = Display_Name;
575 if (!has_display_name && has_storage_name)
576 Display_Name = Storage_Name;
577 if (has_display_name && !has_backup_name && Backup_Display_Name != "Android Secure")
578 Backup_Display_Name = Display_Name;
579 if (!has_display_name && has_backup_name)
580 Display_Name = Backup_Display_Name;
581 }
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600582
583 if (fstab_version == 2 && twrp_flags && twrp_flags->size() > 0) {
584 it = twrp_flags->find(Mount_Point);
585 if (it != twrp_flags->end()) {
586 char twrpflags[MAX_FSTAB_LINE_LENGTH] = "";
587 int skip = 0;
588 string Flags = it->second.Flags;
589 strcpy(twrpflags, Flags.c_str());
590 if (strlen(twrpflags) > strlen("flags=") && strncmp(twrpflags, "flags=", strlen("flags=")) == 0)
591 skip += strlen("flags=");
592 char* flagptr = twrpflags;
593 flagptr += skip;
594 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
595 }
596 }
nkk7198fc3992017-12-16 16:26:42 +0200597
598 if (Mount_Point == "/persist" && Can_Be_Mounted) {
599 bool mounted = Is_Mounted();
600 if (mounted || Mount(false)) {
601 // Read the backup settings file
602 DataManager::LoadPersistValues();
603 TWFunc::Fixup_Time_On_Boot("/persist/time/");
604 if (!mounted)
605 UnMount(false);
606 }
607 }
608
Chaosmasterf6e42ce2020-01-27 00:17:04 +0100609 if (Is_File_System(Fstab_File_System) && (Mount_Point == "/" || Mount_Point == "/system" || Mount_Point == "/system_root")) {
610 if (Sar_Detect) {
611 Mount_Point = "/s";
612 Mount_Read_Only = true;
613 Can_Be_Mounted = true;
614 } else {
615 Mount_Point = PartitionManager.Get_Android_Root_Path();
616 Backup_Path = Mount_Point;
617 Storage_Path = Mount_Point;
618 Make_Dir(Mount_Point, Display_Error);
619 }
620 }
621
Dees_Troy51127312012-09-08 13:08:49 -0400622 return true;
623}
624
Matt Mower72c87ce2016-04-26 14:34:56 -0500625void TWPartition::Partition_Post_Processing(bool Display_Error) {
626 if (Mount_Point == "/data")
627 Setup_Data_Partition(Display_Error);
628 else if (Mount_Point == "/cache")
629 Setup_Cache_Partition(Display_Error);
630}
631
Ethan Yonkerbd7492d2016-12-07 13:55:01 -0600632void TWPartition::ExcludeAll(const string& path) {
633 backup_exclusions.add_absolute_dir(path);
634 wipe_exclusions.add_absolute_dir(path);
635}
636
Matt Mower72c87ce2016-04-26 14:34:56 -0500637void TWPartition::Setup_Data_Partition(bool Display_Error) {
638 if (Mount_Point != "/data")
639 return;
640
641 // Ensure /data is not mounted as tmpfs for qcom hardware decrypt
642 UnMount(false);
643
644#ifdef TW_INCLUDE_CRYPTO
645 if (datamedia)
646 Setup_Data_Media();
647 Can_Be_Encrypted = true;
648 char crypto_blkdev[255];
649 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
650 if (strcmp(crypto_blkdev, "error") != 0) {
651 DataManager::SetValue(TW_IS_DECRYPTED, 1);
652 Is_Encrypted = true;
653 Is_Decrypted = true;
Ethan Yonker93382822018-11-01 15:25:31 -0500654 if (Key_Directory.empty())
655 Is_FBE = false;
656 else
657 Is_FBE = true;
Ethan Yonkerbd7492d2016-12-07 13:55:01 -0600658 DataManager::SetValue(TW_IS_FBE, 0);
Matt Mower72c87ce2016-04-26 14:34:56 -0500659 Decrypted_Block_Device = crypto_blkdev;
660 LOGINFO("Data already decrypted, new block device: '%s'\n", crypto_blkdev);
661 } else if (!Mount(false)) {
662 if (Is_Present) {
Ethan Yonker93382822018-11-01 15:25:31 -0500663 if (Key_Directory.empty()) {
664 set_partition_data(Actual_Block_Device.c_str(), Crypto_Key_Location.c_str(), Fstab_File_System.c_str());
665 if (cryptfs_check_footer() == 0) {
666 Is_Encrypted = true;
667 Is_Decrypted = false;
668 Can_Be_Mounted = false;
669 Current_File_System = "emmc";
670 Setup_Image();
671 DataManager::SetValue(TW_IS_ENCRYPTED, 1);
672 DataManager::SetValue(TW_CRYPTO_PWTYPE, cryptfs_get_password_type());
673 DataManager::SetValue(TW_CRYPTO_PASSWORD, "");
674 DataManager::SetValue("tw_crypto_display", "");
675 } else {
676 gui_err("mount_data_footer=Could not mount /data and unable to find crypto footer.");
677 }
678 } else {
Matt Mower72c87ce2016-04-26 14:34:56 -0500679 Is_Encrypted = true;
680 Is_Decrypted = false;
Matt Mower72c87ce2016-04-26 14:34:56 -0500681 }
Ethan Yonker93382822018-11-01 15:25:31 -0500682 } else if (Key_Directory.empty()) {
Matt Mower72c87ce2016-04-26 14:34:56 -0500683 LOGERR("Primary block device '%s' for mount point '%s' is not present!\n", Primary_Block_Device.c_str(), Mount_Point.c_str());
684 }
685 } else {
mauronofrio9bf73352019-11-23 22:27:34 +0100686
687 if (!Decrypt_FBE_DE()) {
688 LOGINFO("Trying wrapped key.\n");
689 property_set("fbe.data.wrappedkey", "true");
690 if (!Decrypt_FBE_DE()) {
bigbiffd58ba182020-03-23 10:02:29 -0400691 LOGERR("Unable to decrypt FBE device\n");
mauronofrio9bf73352019-11-23 22:27:34 +0100692 }
693 }
Matt Mower72c87ce2016-04-26 14:34:56 -0500694 }
695 if (datamedia && (!Is_Encrypted || (Is_Encrypted && Is_Decrypted))) {
696 Setup_Data_Media();
697 Recreate_Media_Folder();
698 }
699#else
700 if (datamedia) {
701 Setup_Data_Media();
702 Recreate_Media_Folder();
703 }
704#endif
705}
706
Ethan Yonker93382822018-11-01 15:25:31 -0500707bool TWPartition::Decrypt_FBE_DE() {
708if (TWFunc::Path_Exists("/data/unencrypted/key/version")) {
709 LOGINFO("File Based Encryption is present\n");
710#ifdef TW_INCLUDE_FBE
711 ExcludeAll(Mount_Point + "/convert_fbe");
712 ExcludeAll(Mount_Point + "/unencrypted");
713 //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
714 ExcludeAll(Mount_Point + "/misc/vold/user_keys");
715 //ExcludeAll(Mount_Point + "/system_ce");
716 //ExcludeAll(Mount_Point + "/system_de");
717 //ExcludeAll(Mount_Point + "/misc_ce");
718 //ExcludeAll(Mount_Point + "/misc_de");
719 ExcludeAll(Mount_Point + "/system/gatekeeper.password.key");
720 ExcludeAll(Mount_Point + "/system/gatekeeper.pattern.key");
721 ExcludeAll(Mount_Point + "/system/locksettings.db");
722 //ExcludeAll(Mount_Point + "/system/locksettings.db-shm"); // don't seem to need this one, but the other 2 are needed
723 ExcludeAll(Mount_Point + "/system/locksettings.db-wal");
724 //ExcludeAll(Mount_Point + "/user_de");
725 //ExcludeAll(Mount_Point + "/misc/profiles/cur/0"); // might be important later
726 ExcludeAll(Mount_Point + "/misc/gatekeeper");
727 ExcludeAll(Mount_Point + "/misc/keystore");
728 ExcludeAll(Mount_Point + "/drm/kek.dat");
729 ExcludeAll(Mount_Point + "/system_de/0/spblob"); // contains data needed to decrypt pixel 2
730 int retry_count = 3;
731 while (!Decrypt_DE() && --retry_count)
732 usleep(2000);
733 if (retry_count > 0) {
734 property_set("ro.crypto.state", "encrypted");
735 Is_Encrypted = true;
736 Is_Decrypted = false;
737 Is_FBE = true;
738 DataManager::SetValue(TW_IS_FBE, 1);
739 DataManager::SetValue(TW_IS_ENCRYPTED, 1);
740 string filename;
741 int pwd_type = Get_Password_Type(0, filename);
742 if (pwd_type < 0) {
743 LOGERR("This TWRP does not have synthetic password decrypt support\n");
744 pwd_type = 0; // default password
745 }
746 DataManager::SetValue(TW_CRYPTO_PWTYPE, pwd_type);
747 DataManager::SetValue(TW_CRYPTO_PASSWORD, "");
748 DataManager::SetValue("tw_crypto_display", "");
749 return true;
750 }
751#else
752 LOGERR("FBE found but FBE support not present in TWRP\n");
753#endif
754 }
755 return false;
756}
757
Matt Mower72c87ce2016-04-26 14:34:56 -0500758void TWPartition::Setup_Cache_Partition(bool Display_Error __unused) {
759 if (Mount_Point != "/cache")
760 return;
761
762 if (!Mount(true))
763 return;
764
765 if (!TWFunc::Path_Exists("/cache/recovery/.")) {
766 LOGINFO("Recreating /cache/recovery folder\n");
767 if (mkdir("/cache/recovery", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0)
768 LOGERR("Could not create /cache/recovery\n");
769 }
770}
771
Matt Mower4ab42b12016-04-21 13:52:18 -0500772void TWPartition::Process_FS_Flags(const char *str) {
773 char *options = strdup(str);
774 char *ptr, *savep;
Hashcode62bd9e02013-11-19 21:59:42 -0800775
Matt Mower4ab42b12016-04-21 13:52:18 -0500776 Mount_Options = "";
Hashcode62bd9e02013-11-19 21:59:42 -0800777
Matt Mower4ab42b12016-04-21 13:52:18 -0500778 // Avoid issues with potentially nested strtok by using strtok_r
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600779 for (ptr = strtok_r(options, ",", &savep); ptr; ptr = strtok_r(NULL, ",", &savep)) {
780 char *equals = strstr(ptr, "=");
781 size_t name_len;
Matt Mower4ab42b12016-04-21 13:52:18 -0500782
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600783 if (!equals)
784 name_len = strlen(ptr);
785 else
786 name_len = equals - ptr;
787
788 // There are some flags that we want to ignore in TWRP
789 bool found_match = false;
790 for (const char** ignored_mount_item = ignored_mount_items; *ignored_mount_item; ignored_mount_item++) {
791 if (strncmp(ptr, *ignored_mount_item, name_len) == 0) {
792 found_match = true;
Hashcode62bd9e02013-11-19 21:59:42 -0800793 break;
794 }
795 }
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600796 if (found_match)
797 continue;
Hashcode62bd9e02013-11-19 21:59:42 -0800798
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600799 // mount_flags are never postfixed by '='
800 if (!equals) {
801 const struct flag_list* mount_flag = mount_flags;
802 for (; mount_flag->name; mount_flag++) {
803 if (strcmp(ptr, mount_flag->name) == 0) {
804 if (mount_flag->flag == MS_RDONLY)
805 Mount_Read_Only = true;
806 else
807 Mount_Flags |= (unsigned)mount_flag->flag;
808 found_match = true;
809 break;
810 }
811 }
812 if (found_match)
813 continue;
Matt Mower4ab42b12016-04-21 13:52:18 -0500814 }
815
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600816 // If we aren't ignoring this flag and it's not a mount flag, then it must be a mount option
817 if (!Mount_Options.empty())
818 Mount_Options += ",";
819 Mount_Options += ptr;
Matt Mower4ab42b12016-04-21 13:52:18 -0500820 }
821 free(options);
Hashcode62bd9e02013-11-19 21:59:42 -0800822}
823
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600824void TWPartition::Save_FS_Flags(const string& local_File_System, int local_Mount_Flags, const string& local_Mount_Options) {
825 partition_fs_flags_struct flags;
826 flags.File_System = local_File_System;
827 flags.Mount_Flags = local_Mount_Flags;
828 flags.Mount_Options = local_Mount_Options;
829 fs_flags.push_back(flags);
830}
831
Matt Mower2416a502016-04-12 19:54:46 -0500832void TWPartition::Apply_TW_Flag(const unsigned flag, const char* str, const bool val) {
833 switch (flag) {
834 case TWFLAG_ANDSEC:
835 Has_Android_Secure = val;
836 break;
837 case TWFLAG_BACKUP:
838 Can_Be_Backed_Up = val;
839 break;
840 case TWFLAG_BACKUPNAME:
841 Backup_Display_Name = str;
842 break;
843 case TWFLAG_BLOCKSIZE:
844 Format_Block_Size = (unsigned long)(atol(str));
845 break;
846 case TWFLAG_CANBEWIPED:
847 Can_Be_Wiped = val;
848 break;
849 case TWFLAG_CANENCRYPTBACKUP:
850 Can_Encrypt_Backup = val;
851 break;
852 case TWFLAG_DEFAULTS:
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600853 case TWFLAG_WAIT:
854 case TWFLAG_VERIFY:
855 case TWFLAG_CHECK:
856 case TWFLAG_NOTRIM:
857 case TWFLAG_VOLDMANAGED:
858 case TWFLAG_RESIZE:
Matt Mower2416a502016-04-12 19:54:46 -0500859 // Do nothing
860 break;
861 case TWFLAG_DISPLAY:
862 Display_Name = str;
863 break;
864 case TWFLAG_ENCRYPTABLE:
865 case TWFLAG_FORCEENCRYPT:
866 Crypto_Key_Location = str;
867 break;
Ethan Yonkere9afc3d2018-08-30 15:16:27 -0500868 case TWFLAG_FILEENCRYPTION:
869 // This flag isn't used by TWRP but is needed in 9.0 FBE decrypt
870 // fileencryption=ice:aes-256-heh
871 {
872 std::string FBE = str;
Ethan Yonkere9afc3d2018-08-30 15:16:27 -0500873 size_t colon_loc = FBE.find(":");
874 if (colon_loc == std::string::npos) {
Ethan Yonker93382822018-11-01 15:25:31 -0500875 property_set("fbe.contents", FBE.c_str());
876 property_set("fbe.filenames", "");
877 LOGINFO("FBE contents '%s', filenames ''\n", FBE.c_str());
Ethan Yonkere9afc3d2018-08-30 15:16:27 -0500878 break;
879 }
Ethan Yonker93382822018-11-01 15:25:31 -0500880 std::string FBE_contents, FBE_filenames;
Ethan Yonkere9afc3d2018-08-30 15:16:27 -0500881 FBE_contents = FBE.substr(0, colon_loc);
882 FBE_filenames = FBE.substr(colon_loc + 1);
883 property_set("fbe.contents", FBE_contents.c_str());
884 property_set("fbe.filenames", FBE_filenames.c_str());
885 LOGINFO("FBE contents '%s', filenames '%s'\n", FBE_contents.c_str(), FBE_filenames.c_str());
886 }
887 break;
Peter Cai28ea7b02019-05-24 11:38:54 +0800888 case TWFLAG_WRAPPEDKEY:
889 // Set fbe.data.wrappedkey to true
890 {
891 property_set("fbe.data.wrappedkey", "true");
892 LOGINFO("FBE wrapped key enabled\n");
893 }
894 break;
Matt Mower2416a502016-04-12 19:54:46 -0500895 case TWFLAG_FLASHIMG:
896 Can_Flash_Img = val;
897 break;
898 case TWFLAG_FSFLAGS:
Matt Mower4ab42b12016-04-21 13:52:18 -0500899 Process_FS_Flags(str);
Matt Mower2416a502016-04-12 19:54:46 -0500900 break;
901 case TWFLAG_IGNOREBLKID:
902 Ignore_Blkid = val;
903 break;
904 case TWFLAG_LENGTH:
905 Length = atoi(str);
906 break;
907 case TWFLAG_MOUNTTODECRYPT:
908 Mount_To_Decrypt = val;
909 break;
910 case TWFLAG_REMOVABLE:
911 Removable = val;
912 break;
913 case TWFLAG_RETAINLAYOUTVERSION:
914 Retain_Layout_Version = val;
915 break;
916 case TWFLAG_SETTINGSSTORAGE:
917 Is_Settings_Storage = val;
918 if (Is_Settings_Storage)
919 Is_Storage = true;
920 break;
921 case TWFLAG_STORAGE:
922 Is_Storage = val;
923 break;
924 case TWFLAG_STORAGENAME:
925 Storage_Name = str;
926 break;
927 case TWFLAG_SUBPARTITIONOF:
928 Is_SubPartition = true;
929 SubPartition_Of = str;
930 break;
931 case TWFLAG_SYMLINK:
932 Symlink_Path = str;
933 break;
934 case TWFLAG_USERDATAENCRYPTBACKUP:
935 Use_Userdata_Encryption = val;
936 if (Use_Userdata_Encryption)
937 Can_Encrypt_Backup = true;
938 break;
939 case TWFLAG_USERMRF:
940 Use_Rm_Rf = val;
941 break;
942 case TWFLAG_WIPEDURINGFACTORYRESET:
943 Wipe_During_Factory_Reset = val;
944 if (Wipe_During_Factory_Reset) {
945 Can_Be_Wiped = true;
946 Wipe_Available_in_GUI = true;
947 }
948 break;
949 case TWFLAG_WIPEINGUI:
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600950 case TWFLAG_FORMATTABLE:
Matt Mower2416a502016-04-12 19:54:46 -0500951 Wipe_Available_in_GUI = val;
952 if (Wipe_Available_in_GUI)
953 Can_Be_Wiped = true;
954 break;
Ethan Yonker1b190162016-12-05 15:25:19 -0600955 case TWFLAG_SLOTSELECT:
956 SlotSelect = true;
957 break;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600958 case TWFLAG_ALTDEVICE:
959 Alternate_Block_Device = str;
960 break;
Ethan Yonker93382822018-11-01 15:25:31 -0500961 case TWFLAG_KEYDIRECTORY:
962 Key_Directory = str;
bigbiffd58ba182020-03-23 10:02:29 -0400963 break;
Matt Mower2416a502016-04-12 19:54:46 -0500964 default:
965 // Should not get here
966 LOGINFO("Flag identified for processing, but later unmatched: %i\n", flag);
967 break;
968 }
969}
Dees_Troy51127312012-09-08 13:08:49 -0400970
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600971void TWPartition::Process_TW_Flags(char *flags, bool Display_Error, int fstab_ver) {
Matt Mower2416a502016-04-12 19:54:46 -0500972 char separator[2] = {'\n', 0};
973 char *ptr, *savep;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600974 char source_separator = ';';
975
976 if (fstab_ver == 2)
977 source_separator = ',';
Matt Mower2416a502016-04-12 19:54:46 -0500978
979 // Semicolons within double-quotes are not forbidden, so replace
980 // only the semicolons intended as separators with '\n' for strtok
981 for (unsigned i = 0, skip = 0; i < strlen(flags); i++) {
982 if (flags[i] == '\"')
Dees_Troya13d74f2013-03-24 08:54:55 -0500983 skip = !skip;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600984 if (!skip && flags[i] == source_separator)
Matt Mower2416a502016-04-12 19:54:46 -0500985 flags[i] = separator[0];
Dees_Troy51127312012-09-08 13:08:49 -0400986 }
987
Matt Mower2416a502016-04-12 19:54:46 -0500988 // Avoid issues with potentially nested strtok by using strtok_r
989 ptr = strtok_r(flags, separator, &savep);
990 while (ptr) {
991 int ptr_len = strlen(ptr);
992 const struct flag_list* tw_flag = tw_flags;
Hashcode62bd9e02013-11-19 21:59:42 -0800993
Matt Mower2416a502016-04-12 19:54:46 -0500994 for (; tw_flag->name; tw_flag++) {
995 int flag_len = strlen(tw_flag->name);
996
997 if (strncmp(ptr, tw_flag->name, flag_len) == 0) {
998 bool flag_val = false;
999
1000 if (ptr_len > flag_len && (tw_flag->name)[flag_len-1] != '='
1001 && ptr[flag_len] != '=') {
1002 // Handle flags with same starting string
1003 // (e.g. backup and backupname)
1004 continue;
1005 } else if (ptr_len > flag_len && ptr[flag_len] == '=') {
1006 // Handle flags with dual format: Part 1
1007 // (e.g. backup and backup=y. backup=y handled here)
1008 ptr += flag_len + 1;
1009 TWFunc::Strip_Quotes(ptr);
1010 // Skip flags with empty argument
1011 // (e.g. backup=)
1012 if (strlen(ptr) == 0) {
1013 LOGINFO("Flag missing argument or should not include '=': %s=\n", tw_flag->name);
1014 break;
1015 }
1016 flag_val = strchr("yY1", *ptr) != NULL;
1017 } else if (ptr_len == flag_len
1018 && (tw_flag->name)[flag_len-1] == '=') {
1019 // Skip flags missing argument after =
1020 // (e.g. backupname=)
1021 LOGINFO("Flag missing argument: %s\n", tw_flag->name);
1022 break;
1023 } else if (ptr_len > flag_len
1024 && (tw_flag->name)[flag_len-1] == '=') {
1025 // Handle arguments to flags
1026 // (e.g. backupname="My Stuff")
1027 ptr += flag_len;
1028 TWFunc::Strip_Quotes(ptr);
1029 // Skip flags with empty argument
1030 // (e.g. backupname="")
1031 if (strlen(ptr) == 0) {
1032 LOGINFO("Flag missing argument: %s\n", tw_flag->name);
1033 break;
1034 }
1035 } else if (ptr_len == flag_len) {
1036 // Handle flags with dual format: Part 2
1037 // (e.g. backup and backup=y. backup handled here)
1038 flag_val = true;
Ethan Yonker96af84a2015-01-05 14:58:36 -06001039 } else {
Matt Mower2416a502016-04-12 19:54:46 -05001040 LOGINFO("Flag matched, but could not be processed: %s\n", ptr);
1041 break;
Ethan Yonker96af84a2015-01-05 14:58:36 -06001042 }
Matt Mower2416a502016-04-12 19:54:46 -05001043
1044 Apply_TW_Flag(tw_flag->flag, ptr, flag_val);
1045 break;
Ethan Yonker96af84a2015-01-05 14:58:36 -06001046 }
Matt Mower2416a502016-04-12 19:54:46 -05001047 }
1048 if (tw_flag->name == 0) {
Dees_Troy51127312012-09-08 13:08:49 -04001049 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001050 LOGERR("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -04001051 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001052 LOGINFO("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -04001053 }
Matt Mower2416a502016-04-12 19:54:46 -05001054 ptr = strtok_r(NULL, separator, &savep);
Dees_Troy51127312012-09-08 13:08:49 -04001055 }
Dees_Troy51a0e822012-09-05 15:24:24 -04001056}
1057
Dees_Troy5bf43922012-09-07 16:07:55 -04001058bool TWPartition::Is_File_System(string File_System) {
1059 if (File_System == "ext2" ||
Dees_Troy63c8df72012-09-10 14:02:05 -04001060 File_System == "ext3" ||
Dees_Troy5bf43922012-09-07 16:07:55 -04001061 File_System == "ext4" ||
1062 File_System == "vfat" ||
1063 File_System == "ntfs" ||
1064 File_System == "yaffs2" ||
bigbiff bigbiff3e146522012-11-14 14:32:59 -05001065 File_System == "exfat" ||
Dees_Troye5017042013-08-29 16:38:55 +00001066 File_System == "f2fs" ||
Erfan Abdib859e912019-02-05 18:18:46 +02001067 File_System == "squashfs" ||
Dees_Troy5bf43922012-09-07 16:07:55 -04001068 File_System == "auto")
1069 return true;
1070 else
1071 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001072}
1073
Dees_Troy5bf43922012-09-07 16:07:55 -04001074bool TWPartition::Is_Image(string File_System) {
Dees_Troy5fcd8f92012-10-16 12:22:05 -04001075 if (File_System == "emmc" || File_System == "mtd" || File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -04001076 return true;
1077 else
1078 return false;
1079}
1080
Dees_Troy51127312012-09-08 13:08:49 -04001081bool TWPartition::Make_Dir(string Path, bool Display_Error) {
Ethan Yonker93ac7a02016-11-07 22:05:58 -06001082 if (TWFunc::Get_D_Type_From_Stat(Path) != S_IFDIR)
1083 unlink(Path.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001084 if (!TWFunc::Path_Exists(Path)) {
Dees_Troy51127312012-09-08 13:08:49 -04001085 if (mkdir(Path.c_str(), 0777) == -1) {
1086 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001087 gui_msg(Msg(msg::kError, "create_folder_strerr=Can not create '{1}' folder ({2}).")(Path)(strerror(errno)));
Dees_Troy51127312012-09-08 13:08:49 -04001088 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001089 LOGINFO("Can not create '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -04001090 return false;
1091 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001092 LOGINFO("Created '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -04001093 return true;
1094 }
1095 }
1096 return true;
1097}
1098
Dees_Troy5bf43922012-09-07 16:07:55 -04001099void TWPartition::Setup_File_System(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001100 Can_Be_Mounted = true;
1101 Can_Be_Wiped = true;
1102
Dees_Troy5bf43922012-09-07 16:07:55 -04001103 // Make the mount point folder if it doesn't exist
Dees_Troy51127312012-09-08 13:08:49 -04001104 Make_Dir(Mount_Point, Display_Error);
bigbiffce8f83c2015-12-12 18:30:21 -05001105 Backup_Method = BM_FILES;
Dees_Troy5bf43922012-09-07 16:07:55 -04001106}
1107
Ethan Yonker1b190162016-12-05 15:25:19 -06001108void TWPartition::Setup_Image() {
Dees_Troy5bf43922012-09-07 16:07:55 -04001109 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
1110 Backup_Name = Display_Name;
Gary Peck82599a82012-11-21 16:23:12 -08001111 if (Current_File_System == "emmc")
bigbiffce8f83c2015-12-12 18:30:21 -05001112 Backup_Method = BM_DD;
Gary Peck82599a82012-11-21 16:23:12 -08001113 else if (Current_File_System == "mtd" || Current_File_System == "bml")
bigbiffce8f83c2015-12-12 18:30:21 -05001114 Backup_Method = BM_FLASH_UTILS;
Dees_Troy5bf43922012-09-07 16:07:55 -04001115 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001116 LOGINFO("Unhandled file system '%s' on image '%s'\n", Current_File_System.c_str(), Display_Name.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -04001117}
1118
Dees_Troye58d5262012-09-21 12:27:57 -04001119void TWPartition::Setup_AndSec(void) {
Dees_Troya13d74f2013-03-24 08:54:55 -05001120 Backup_Display_Name = "Android Secure";
Dees_Troye58d5262012-09-21 12:27:57 -04001121 Backup_Name = "and-sec";
Dees_Troya13d74f2013-03-24 08:54:55 -05001122 Can_Be_Backed_Up = true;
Dees_Troye58d5262012-09-21 12:27:57 -04001123 Has_Android_Secure = true;
1124 Symlink_Path = Mount_Point + "/.android_secure";
1125 Symlink_Mount_Point = "/and-sec";
1126 Backup_Path = Symlink_Mount_Point;
1127 Make_Dir("/and-sec", true);
1128 Recreate_AndSec_Folder();
Kjell Braden3126a112016-06-19 16:58:15 +00001129 Mount_Storage_Retry(true);
Dees_Troye58d5262012-09-21 12:27:57 -04001130}
1131
that9e0593e2014-10-08 00:01:24 +02001132void TWPartition::Setup_Data_Media() {
Ethan Yonker6277c792014-09-15 14:54:30 -05001133 LOGINFO("Setting up '%s' as data/media emulated storage.\n", Mount_Point.c_str());
Ethan Yonker66a19492015-12-10 10:19:45 -06001134 if (Storage_Name.empty() || Storage_Name == "Data")
1135 Storage_Name = "Internal Storage";
Ethan Yonker6277c792014-09-15 14:54:30 -05001136 Has_Data_Media = true;
1137 Is_Storage = true;
Ethan Yonker66a19492015-12-10 10:19:45 -06001138 Storage_Path = Mount_Point + "/media";
Ethan Yonker6277c792014-09-15 14:54:30 -05001139 Symlink_Path = Storage_Path;
Ethan Yonker66a19492015-12-10 10:19:45 -06001140 if (Mount_Point == "/data") {
1141 Is_Settings_Storage = true;
1142 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
1143 Make_Dir("/emmc", false);
1144 Symlink_Mount_Point = "/emmc";
1145 } else {
1146 Make_Dir("/sdcard", false);
1147 Symlink_Mount_Point = "/sdcard";
1148 }
1149 if (Mount(false) && TWFunc::Path_Exists(Mount_Point + "/media/0")) {
1150 Storage_Path = Mount_Point + "/media/0";
1151 Symlink_Path = Storage_Path;
1152 DataManager::SetValue(TW_INTERNAL_PATH, Mount_Point + "/media/0");
1153 UnMount(true);
1154 }
1155 DataManager::SetValue("tw_has_internal", 1);
1156 DataManager::SetValue("tw_has_data_media", 1);
Ethan Yonker3fdcda42016-11-30 12:29:37 -06001157 backup_exclusions.add_absolute_dir("/data/data/com.google.android.music/files");
Ethan Yonker6355b562017-05-01 09:42:17 -05001158 wipe_exclusions.add_absolute_dir(Mount_Point + "/misc/vold"); // adopted storage keys
Ethan Yonker79f88bd2016-12-09 14:52:12 -06001159 ExcludeAll(Mount_Point + "/.layout_version");
1160 ExcludeAll(Mount_Point + "/system/storage.xml");
Ethan Yonker6277c792014-09-15 14:54:30 -05001161 } else {
Ethan Yonker66a19492015-12-10 10:19:45 -06001162 if (Mount(true) && TWFunc::Path_Exists(Mount_Point + "/media/0")) {
1163 Storage_Path = Mount_Point + "/media/0";
1164 Symlink_Path = Storage_Path;
1165 UnMount(true);
1166 }
Ethan Yonker6277c792014-09-15 14:54:30 -05001167 }
Ethan Yonker79f88bd2016-12-09 14:52:12 -06001168 ExcludeAll(Mount_Point + "/media");
Ethan Yonker6277c792014-09-15 14:54:30 -05001169}
1170
Dees_Troy5bf43922012-09-07 16:07:55 -04001171void TWPartition::Find_Real_Block_Device(string& Block, bool Display_Error) {
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06001172 char device[PATH_MAX], realDevice[PATH_MAX];
Dees_Troy5bf43922012-09-07 16:07:55 -04001173
1174 strcpy(device, Block.c_str());
1175 memset(realDevice, 0, sizeof(realDevice));
1176 while (readlink(device, realDevice, sizeof(realDevice)) > 0)
1177 {
1178 strcpy(device, realDevice);
1179 memset(realDevice, 0, sizeof(realDevice));
1180 }
1181
1182 if (device[0] != '/') {
1183 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001184 LOGERR("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001185 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001186 LOGINFO("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001187 return;
Dees_Troy51a0e822012-09-05 15:24:24 -04001188 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -04001189 Block = device;
1190 return;
1191 }
1192}
1193
Kjell Braden3126a112016-06-19 16:58:15 +00001194bool TWPartition::Mount_Storage_Retry(bool Display_Error) {
Dees_Troy8e337f32012-10-13 22:07:49 -04001195 // On some devices, storage doesn't want to mount right away, retry and sleep
Kjell Braden3126a112016-06-19 16:58:15 +00001196 if (!Mount(Display_Error)) {
Dees_Troy8e337f32012-10-13 22:07:49 -04001197 int retry_count = 5;
1198 while (retry_count > 0 && !Mount(false)) {
1199 usleep(500000);
1200 retry_count--;
1201 }
Kjell Braden3126a112016-06-19 16:58:15 +00001202 return Mount(Display_Error);
Dees_Troy8e337f32012-10-13 22:07:49 -04001203 }
Kjell Braden3126a112016-06-19 16:58:15 +00001204 return true;
Dees_Troy8e337f32012-10-13 22:07:49 -04001205}
1206
Dees_Troy38bd7602012-09-14 13:33:53 -04001207bool TWPartition::Find_MTD_Block_Device(string MTD_Name) {
1208 FILE *fp = NULL;
1209 char line[255];
1210
1211 fp = fopen("/proc/mtd", "rt");
1212 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001213 LOGERR("Device does not support /proc/mtd\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001214 return false;
1215 }
1216
1217 while (fgets(line, sizeof(line), fp) != NULL)
1218 {
1219 char device[32], label[32];
1220 unsigned long size = 0;
Dees_Troy38bd7602012-09-14 13:33:53 -04001221 int deviceId;
1222
1223 sscanf(line, "%s %lx %*s %*c%s", device, &size, label);
1224
1225 // Skip header and blank lines
1226 if ((strcmp(device, "dev:") == 0) || (strlen(line) < 8))
1227 continue;
1228
1229 // Strip off the trailing " from the label
1230 label[strlen(label)-1] = '\0';
1231
1232 if (strcmp(label, MTD_Name.c_str()) == 0) {
1233 // We found our device
1234 // Strip off the trailing : from the device
1235 device[strlen(device)-1] = '\0';
1236 if (sscanf(device,"mtd%d", &deviceId) == 1) {
1237 sprintf(device, "/dev/block/mtdblock%d", deviceId);
1238 Primary_Block_Device = device;
Dees_Troy76543db2013-06-19 16:24:30 +00001239 fclose(fp);
1240 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -04001241 }
1242 }
1243 }
1244 fclose(fp);
1245
1246 return false;
1247}
1248
Dees_Troy51127312012-09-08 13:08:49 -04001249bool TWPartition::Get_Size_Via_statfs(bool Display_Error) {
1250 struct statfs st;
1251 string Local_Path = Mount_Point + "/.";
1252
1253 if (!Mount(Display_Error))
1254 return false;
1255
1256 if (statfs(Local_Path.c_str(), &st) != 0) {
1257 if (!Removable) {
1258 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001259 LOGERR("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -04001260 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001261 LOGINFO("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -04001262 }
1263 return false;
1264 }
1265 Size = (st.f_blocks * st.f_bsize);
1266 Used = ((st.f_blocks - st.f_bfree) * st.f_bsize);
1267 Free = (st.f_bfree * st.f_bsize);
1268 Backup_Size = Used;
1269 return true;
1270}
1271
1272bool TWPartition::Get_Size_Via_df(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001273 FILE* fp;
1274 char command[255], line[512];
1275 int include_block = 1;
1276 unsigned int min_len;
1277
1278 if (!Mount(Display_Error))
1279 return false;
1280
Dees_Troy38bd7602012-09-14 13:33:53 -04001281 min_len = Actual_Block_Device.size() + 2;
Dees_Troy51127312012-09-08 13:08:49 -04001282 sprintf(command, "df %s > /tmp/dfoutput.txt", Mount_Point.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001283 TWFunc::Exec_Cmd(command);
Dees_Troy51127312012-09-08 13:08:49 -04001284 fp = fopen("/tmp/dfoutput.txt", "rt");
1285 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001286 LOGINFO("Unable to open /tmp/dfoutput.txt.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001287 return false;
Dees_Troy51127312012-09-08 13:08:49 -04001288 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001289
1290 while (fgets(line, sizeof(line), fp) != NULL)
1291 {
1292 unsigned long blocks, used, available;
1293 char device[64];
1294 char tmpString[64];
1295
1296 if (strncmp(line, "Filesystem", 10) == 0)
1297 continue;
1298 if (strlen(line) < min_len) {
1299 include_block = 0;
1300 continue;
1301 }
1302 if (include_block) {
1303 sscanf(line, "%s %lu %lu %lu", device, &blocks, &used, &available);
1304 } else {
1305 // The device block string is so long that the df information is on the next line
1306 int space_count = 0;
Dees_Troye58d5262012-09-21 12:27:57 -04001307 sprintf(tmpString, "/dev/block/%s", Actual_Block_Device.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001308 while (tmpString[space_count] == 32)
1309 space_count++;
1310 sscanf(line + space_count, "%lu %lu %lu", &blocks, &used, &available);
1311 }
1312
1313 // Adjust block size to byte size
1314 Size = blocks * 1024ULL;
1315 Used = used * 1024ULL;
1316 Free = available * 1024ULL;
1317 Backup_Size = Used;
1318 }
1319 fclose(fp);
1320 return true;
1321}
1322
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001323unsigned long long TWPartition::IOCTL_Get_Block_Size() {
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001324 Find_Actual_Block_Device();
Ethan Yonker483e9f42016-01-11 22:21:18 -06001325
1326 return TWFunc::IOCTL_Get_Block_Size(Actual_Block_Device.c_str());
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001327}
1328
Dees_Troy5bf43922012-09-07 16:07:55 -04001329bool TWPartition::Find_Partition_Size(void) {
1330 FILE* fp;
1331 char line[512];
1332 string tmpdevice;
1333
igoriok87e3d932013-01-31 21:03:53 +02001334 fp = fopen("/proc/dumchar_info", "rt");
1335 if (fp != NULL) {
1336 while (fgets(line, sizeof(line), fp) != NULL)
1337 {
1338 char label[32], device[32];
1339 unsigned long size = 0;
1340
thatd43bf2d2014-09-21 23:13:02 +02001341 sscanf(line, "%s %lx %*x %*u %s", label, &size, device);
igoriok87e3d932013-01-31 21:03:53 +02001342
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05001343 // Skip header, annotation and blank lines
igoriok87e3d932013-01-31 21:03:53 +02001344 if ((strncmp(device, "/dev/", 5) != 0) || (strlen(line) < 8))
1345 continue;
1346
1347 tmpdevice = "/dev/";
1348 tmpdevice += label;
1349 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
1350 Size = size;
1351 fclose(fp);
1352 return true;
1353 }
1354 }
1355 }
1356
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001357 unsigned long long ioctl_size = IOCTL_Get_Block_Size();
1358 if (ioctl_size) {
1359 Size = ioctl_size;
1360 return true;
1361 }
1362
Dees_Troy5bf43922012-09-07 16:07:55 -04001363 // In this case, we'll first get the partitions we care about (with labels)
1364 fp = fopen("/proc/partitions", "rt");
1365 if (fp == NULL)
1366 return false;
1367
1368 while (fgets(line, sizeof(line), fp) != NULL)
1369 {
1370 unsigned long major, minor, blocks;
1371 char device[512];
Dees_Troy5bf43922012-09-07 16:07:55 -04001372
Dees_Troy63c8df72012-09-10 14:02:05 -04001373 if (strlen(line) < 7 || line[0] == 'm') continue;
Dees_Troy5bf43922012-09-07 16:07:55 -04001374 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
1375
1376 tmpdevice = "/dev/block/";
1377 tmpdevice += device;
Dees_Troy38bd7602012-09-14 13:33:53 -04001378 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001379 // Adjust block size to byte size
1380 Size = blocks * 1024ULL;
1381 fclose(fp);
1382 return true;
1383 }
1384 }
1385 fclose(fp);
1386 return false;
1387}
1388
Dees_Troy5bf43922012-09-07 16:07:55 -04001389bool TWPartition::Is_Mounted(void) {
1390 if (!Can_Be_Mounted)
1391 return false;
1392
1393 struct stat st1, st2;
1394 string test_path;
1395
1396 // Check to see if the mount point directory exists
1397 test_path = Mount_Point + "/.";
1398 if (stat(test_path.c_str(), &st1) != 0) return false;
1399
1400 // Check to see if the directory above the mount point exists
1401 test_path = Mount_Point + "/../.";
1402 if (stat(test_path.c_str(), &st2) != 0) return false;
1403
1404 // Compare the device IDs -- if they match then we're (probably) using tmpfs instead of an actual device
1405 int ret = (st1.st_dev != st2.st_dev) ? true : false;
1406
1407 return ret;
1408}
1409
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05001410bool TWPartition::Is_File_System_Writable(void) {
1411 if (!Is_File_System(Current_File_System) || !Is_Mounted())
1412 return false;
1413
1414 string test_path = Mount_Point + "/.";
1415 return (access(test_path.c_str(), W_OK) == 0);
1416}
1417
Dees_Troy5bf43922012-09-07 16:07:55 -04001418bool TWPartition::Mount(bool Display_Error) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001419 int exfat_mounted = 0;
Martin Dünkelmann6d843782018-12-27 23:40:28 +01001420 unsigned int flags = Mount_Flags;
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001421
Dees_Troy5bf43922012-09-07 16:07:55 -04001422 if (Is_Mounted()) {
1423 return true;
1424 } else if (!Can_Be_Mounted) {
1425 return false;
1426 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001427
1428 Find_Actual_Block_Device();
1429
1430 // Check the current file system before mounting
1431 Check_FS_Type();
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001432 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/exfat-fuse")) {
Dees_Troye34c1332013-02-06 19:13:00 +00001433 string cmd = "/sbin/exfat-fuse -o big_writes,max_read=131072,max_write=131072 " + Actual_Block_Device + " " + Mount_Point;
Dees_Troy2673cec2013-04-02 20:22:16 +00001434 LOGINFO("cmd: %s\n", cmd.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001435 string result;
1436 if (TWFunc::Exec_Cmd(cmd, result) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001437 LOGINFO("exfat-fuse failed to mount with result '%s', trying vfat\n", result.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001438 Current_File_System = "vfat";
1439 } else {
1440#ifdef TW_NO_EXFAT_FUSE
1441 UnMount(false);
1442 // We'll let the kernel handle it but using exfat-fuse to detect if the file system is actually exfat
1443 // Some kernels let us mount vfat as exfat which doesn't work out too well
1444#else
1445 exfat_mounted = 1;
1446#endif
1447 }
1448 }
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001449
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001450 if (Current_File_System == "ntfs" && (TWFunc::Path_Exists("/sbin/ntfs-3g") || TWFunc::Path_Exists("/sbin/mount.ntfs"))) {
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001451 string cmd;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001452 string Ntfsmount_Binary = "";
1453
1454 if (TWFunc::Path_Exists("/sbin/ntfs-3g"))
1455 Ntfsmount_Binary = "ntfs-3g";
1456 else if (TWFunc::Path_Exists("/sbin/mount.ntfs"))
1457 Ntfsmount_Binary = "mount.ntfs";
1458
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001459 if (Mount_Read_Only)
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001460 cmd = "/sbin/" + Ntfsmount_Binary + " -o ro " + Actual_Block_Device + " " + Mount_Point;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001461 else
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001462 cmd = "/sbin/" + Ntfsmount_Binary + " " + Actual_Block_Device + " " + Mount_Point;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001463 LOGINFO("cmd: '%s'\n", cmd.c_str());
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001464
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001465 if (TWFunc::Exec_Cmd(cmd) == 0) {
1466 return true;
1467 } else {
1468 LOGINFO("ntfs-3g failed to mount, trying regular mount method.\n");
1469 }
1470 }
1471
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001472 if (Mount_Read_Only)
1473 flags |= MS_RDONLY;
1474
Dees_Troy22042032012-12-18 21:23:08 +00001475 if (Fstab_File_System == "yaffs2") {
1476 // mount an MTD partition as a YAFFS2 filesystem.
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001477 flags = MS_NOATIME | MS_NODEV | MS_NODIRATIME;
1478 if (Mount_Read_Only)
1479 flags |= MS_RDONLY;
Dees_Troy76543db2013-06-19 16:24:30 +00001480 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags, NULL) < 0) {
1481 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags | MS_RDONLY, NULL) < 0) {
1482 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001483 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy76543db2013-06-19 16:24:30 +00001484 else
1485 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
1486 return false;
1487 } else {
1488 LOGINFO("Mounted '%s' (MTD) as RO\n", Mount_Point.c_str());
1489 return true;
1490 }
1491 } else {
1492 struct stat st;
1493 string test_path = Mount_Point;
1494 if (stat(test_path.c_str(), &st) < 0) {
1495 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001496 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy76543db2013-06-19 16:24:30 +00001497 else
1498 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
1499 return false;
1500 }
1501 mode_t new_mode = st.st_mode | S_IXUSR | S_IXGRP | S_IXOTH;
1502 if (new_mode != st.st_mode) {
1503 LOGINFO("Fixing execute permissions for %s\n", Mount_Point.c_str());
1504 if (chmod(Mount_Point.c_str(), new_mode) < 0) {
1505 if (Display_Error)
1506 LOGERR("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
1507 else
1508 LOGINFO("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
1509 return false;
1510 }
1511 }
Dees_Troy22042032012-12-18 21:23:08 +00001512 return true;
Dees_Troy76543db2013-06-19 16:24:30 +00001513 }
thatc7572eb2015-03-31 18:55:30 +02001514 }
1515
1516 string mount_fs = Current_File_System;
1517 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sys/module/texfat"))
1518 mount_fs = "texfat";
1519
1520 if (!exfat_mounted &&
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001521 mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), mount_fs.c_str(), flags, Mount_Options.c_str()) != 0 &&
1522 mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), mount_fs.c_str(), flags, NULL) != 0) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001523#ifdef TW_NO_EXFAT_FUSE
1524 if (Current_File_System == "exfat") {
Dees_Troy2673cec2013-04-02 20:22:16 +00001525 LOGINFO("Mounting exfat failed, trying vfat...\n");
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001526 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), "vfat", 0, NULL) != 0) {
Dees_Troy85f44ed2013-01-09 18:42:36 +00001527 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001528 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy85f44ed2013-01-09 18:42:36 +00001529 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001530 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001531 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_Troy3f5c4e82013-02-01 15:16:59 +00001532 return false;
Dees_Troy85f44ed2013-01-09 18:42:36 +00001533 }
Dees_Troyb05ddee2013-01-28 20:24:50 +00001534 } else {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001535#endif
bigbiff bigbiff26774a02014-03-29 18:22:00 -04001536 if (!Removable && Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001537 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001538 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001539 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
1540 LOGINFO("Actual block device: '%s', current file system: '%s'\n", Actual_Block_Device.c_str(), Current_File_System.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001541 return false;
1542#ifdef TW_NO_EXFAT_FUSE
Dees_Troy85f44ed2013-01-09 18:42:36 +00001543 }
1544#endif
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001545 }
Ethan Yonker253368a2014-11-25 15:00:52 -06001546
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001547 if (Removable)
1548 Update_Size(Display_Error);
1549
xiaolu9416f4f2015-06-04 08:22:23 +08001550 if (!Symlink_Mount_Point.empty() && TWFunc::Path_Exists(Symlink_Path)) {
codelover2a3d4ce2015-03-14 20:26:49 +08001551 string Command = "mount -o bind '" + Symlink_Path + "' '" + Symlink_Mount_Point + "'";
Vojtech Bocek05534202013-09-11 08:11:56 +02001552 TWFunc::Exec_Cmd(Command);
Dees_Troy5bf43922012-09-07 16:07:55 -04001553 }
Chaosmasterda974802020-01-26 21:09:28 +01001554
1555 if (Mount_Point == "/system_root") {
1556 unlink("/system");
1557 mkdir("/system", 0755);
1558 mount("/system_root/system", "/system", "auto", MS_BIND, NULL);
1559 }
1560
Dees_Troy5bf43922012-09-07 16:07:55 -04001561 return true;
1562}
1563
1564bool TWPartition::UnMount(bool Display_Error) {
Chaosmasterda974802020-01-26 21:09:28 +01001565 if (Mount_Point == "/system_root") {
1566 if (umount("/system") == -1)
1567 umount2("/system", MNT_DETACH);
1568 rmdir("/system");
1569 symlink("/system_root/system", "/system");
1570 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001571 if (Is_Mounted()) {
1572 int never_unmount_system;
1573
1574 DataManager::GetValue(TW_DONT_UNMOUNT_SYSTEM, never_unmount_system);
Captain Throwback9d6feb52018-07-27 10:05:24 -04001575 if (never_unmount_system == 1 && Mount_Point == PartitionManager.Get_Android_Root_Path())
Dees_Troy5bf43922012-09-07 16:07:55 -04001576 return true; // Never unmount system if you're not supposed to unmount it
1577
Matt Mowera8e6d832017-02-14 20:20:59 -06001578 if (Is_Storage && MTP_Storage_ID > 0)
Ethan Yonker726a0202014-12-16 20:01:38 -06001579 PartitionManager.Remove_MTP_Storage(MTP_Storage_ID);
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001580
Dees_Troy38bd7602012-09-14 13:33:53 -04001581 if (!Symlink_Mount_Point.empty())
1582 umount(Symlink_Mount_Point.c_str());
1583
Dees_Troyb05ddee2013-01-28 20:24:50 +00001584 umount(Mount_Point.c_str());
1585 if (Is_Mounted()) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001586 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001587 gui_msg(Msg(msg::kError, "fail_unmount=Failed to unmount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy5bf43922012-09-07 16:07:55 -04001588 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001589 LOGINFO("Unable to unmount '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001590 return false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001591 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -04001592 return true;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001593 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001594 } else {
1595 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001596 }
1597}
1598
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05001599bool TWPartition::ReMount(bool Display_Error) {
1600 if (UnMount(Display_Error))
1601 return Mount(Display_Error);
1602 return false;
1603}
1604
1605bool TWPartition::ReMount_RW(bool Display_Error) {
1606 // No need to remount if already mounted rw
1607 if (Is_File_System_Writable())
1608 return true;
1609
1610 bool ro = Mount_Read_Only;
1611 int flags = Mount_Flags;
1612
1613 Mount_Read_Only = false;
1614 Mount_Flags &= ~MS_RDONLY;
1615
1616 bool ret = ReMount(Display_Error);
1617
1618 Mount_Read_Only = ro;
1619 Mount_Flags = flags;
1620
1621 return ret;
1622}
1623
Gary Peck43acadf2012-11-21 21:19:01 -08001624bool TWPartition::Wipe(string New_File_System) {
Ethan Yonker726a0202014-12-16 20:01:38 -06001625 bool wiped = false, update_crypt = false, recreate_media = true;
Dees_Troy16c2b312013-01-15 16:51:18 +00001626 int check;
1627 string Layout_Filename = Mount_Point + "/.layout_version";
1628
Dees_Troy38bd7602012-09-14 13:33:53 -04001629 if (!Can_Be_Wiped) {
Matt Mower3c366972015-12-25 19:28:31 -06001630 gui_msg(Msg(msg::kError, "cannot_wipe=Partition {1} cannot be wiped.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04001631 return false;
1632 }
1633
Dees_Troyc51f1f92012-09-20 15:32:13 -04001634 if (Mount_Point == "/cache")
Dees_Troy2673cec2013-04-02 20:22:16 +00001635 Log_Offset = 0;
Dees_Troyc51f1f92012-09-20 15:32:13 -04001636
Dees_Troy16c2b312013-01-15 16:51:18 +00001637 if (Retain_Layout_Version && Mount(false) && TWFunc::Path_Exists(Layout_Filename))
1638 TWFunc::copy_file(Layout_Filename, "/.layout_version", 0600);
1639 else
1640 unlink("/.layout_version");
Dees_Troy38bd7602012-09-14 13:33:53 -04001641
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001642 if (Has_Data_Media && Current_File_System == New_File_System) {
Dees_Troy16c2b312013-01-15 16:51:18 +00001643 wiped = Wipe_Data_Without_Wiping_Media();
Ethan Yonker5eac2222014-06-11 12:22:55 -05001644 recreate_media = false;
Dees_Troy16c2b312013-01-15 16:51:18 +00001645 } else {
Dees_Troy16c2b312013-01-15 16:51:18 +00001646 DataManager::GetValue(TW_RM_RF_VAR, check);
1647
Hashcodedabfd492013-08-29 22:45:30 -07001648 if (check || Use_Rm_Rf)
Dees_Troy16c2b312013-01-15 16:51:18 +00001649 wiped = Wipe_RMRF();
1650 else if (New_File_System == "ext4")
1651 wiped = Wipe_EXT4();
1652 else if (New_File_System == "ext2" || New_File_System == "ext3")
dianlujitao4879b372018-12-03 18:45:47 +08001653 wiped = Wipe_EXTFS(New_File_System);
Dees_Troy16c2b312013-01-15 16:51:18 +00001654 else if (New_File_System == "vfat")
1655 wiped = Wipe_FAT();
1656 else if (New_File_System == "exfat")
1657 wiped = Wipe_EXFAT();
1658 else if (New_File_System == "yaffs2")
1659 wiped = Wipe_MTD();
Dees_Troye5017042013-08-29 16:38:55 +00001660 else if (New_File_System == "f2fs")
1661 wiped = Wipe_F2FS();
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001662 else if (New_File_System == "ntfs")
1663 wiped = Wipe_NTFS();
Dees_Troy16c2b312013-01-15 16:51:18 +00001664 else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001665 LOGERR("Unable to wipe '%s' -- unknown file system '%s'\n", Mount_Point.c_str(), New_File_System.c_str());
Dees_Troy16c2b312013-01-15 16:51:18 +00001666 unlink("/.layout_version");
1667 return false;
1668 }
1669 update_crypt = wiped;
Gary Pecke8bc5d72012-12-21 06:45:25 -08001670 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001671
Gary Pecke8bc5d72012-12-21 06:45:25 -08001672 if (wiped) {
Dees_Troy1c1ac442013-01-17 21:42:14 +00001673 if (Mount_Point == "/cache")
1674 DataManager::Output_Version();
1675
Dees_Troy16c2b312013-01-15 16:51:18 +00001676 if (TWFunc::Path_Exists("/.layout_version") && Mount(false))
1677 TWFunc::copy_file("/.layout_version", Layout_Filename, 0600);
1678
1679 if (update_crypt) {
1680 Setup_File_System(false);
1681 if (Is_Encrypted && !Is_Decrypted) {
1682 // just wiped an encrypted partition back to its unencrypted state
1683 Is_Encrypted = false;
1684 Is_Decrypted = false;
1685 Decrypted_Block_Device = "";
1686 if (Mount_Point == "/data") {
1687 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1688 DataManager::SetValue(TW_IS_DECRYPTED, 0);
1689 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001690 }
1691 }
Ethan Yonker5eac2222014-06-11 12:22:55 -05001692
Ethan Yonker66a19492015-12-10 10:19:45 -06001693 if (Has_Data_Media && recreate_media) {
Ethan Yonker5eac2222014-06-11 12:22:55 -05001694 Recreate_Media_Folder();
1695 }
Matt Mower209c9632016-01-20 12:08:35 -06001696 if (Is_Storage && Mount(false))
1697 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Gary Pecke8bc5d72012-12-21 06:45:25 -08001698 }
Matt Mower209c9632016-01-20 12:08:35 -06001699
Gary Pecke8bc5d72012-12-21 06:45:25 -08001700 return wiped;
Dees_Troy51a0e822012-09-05 15:24:24 -04001701}
1702
Gary Peck43acadf2012-11-21 21:19:01 -08001703bool TWPartition::Wipe() {
Gary Peck82599a82012-11-21 16:23:12 -08001704 if (Is_File_System(Current_File_System))
1705 return Wipe(Current_File_System);
1706 else
1707 return Wipe(Fstab_File_System);
Gary Peck43acadf2012-11-21 21:19:01 -08001708}
1709
Dees_Troye58d5262012-09-21 12:27:57 -04001710bool TWPartition::Wipe_AndSec(void) {
1711 if (!Has_Android_Secure)
1712 return false;
1713
Dees_Troye58d5262012-09-21 12:27:57 -04001714 if (!Mount(true))
1715 return false;
1716
Ethan Yonker74db1572015-10-28 12:44:49 -05001717 gui_msg(Msg("wiping=Wiping {1}")(Backup_Display_Name));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001718 TWFunc::removeDir(Mount_Point + "/.android_secure/", true);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001719 return true;
Dees_Troye58d5262012-09-21 12:27:57 -04001720}
1721
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001722bool TWPartition::Can_Repair() {
Ethan Yonkera2719152015-05-28 09:44:41 -05001723 if (Mount_Read_Only)
1724 return false;
Matt Mower18794c82015-11-11 16:22:45 -06001725 if (Current_File_System == "vfat" && TWFunc::Path_Exists("/sbin/fsck.fat"))
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001726 return true;
1727 else if ((Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") && TWFunc::Path_Exists("/sbin/e2fsck"))
1728 return true;
1729 else if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/fsck.exfat"))
1730 return true;
1731 else if (Current_File_System == "f2fs" && TWFunc::Path_Exists("/sbin/fsck.f2fs"))
1732 return true;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001733 else if (Current_File_System == "ntfs" && (TWFunc::Path_Exists("/sbin/ntfsfix") || TWFunc::Path_Exists("/sbin/fsck.ntfs")))
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001734 return true;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001735 return false;
1736}
1737
1738bool TWPartition::Repair() {
1739 string command;
1740
1741 if (Current_File_System == "vfat") {
Matt Mower18794c82015-11-11 16:22:45 -06001742 if (!TWFunc::Path_Exists("/sbin/fsck.fat")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001743 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.fat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001744 return false;
1745 }
1746 if (!UnMount(true))
1747 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001748 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.fat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001749 Find_Actual_Block_Device();
Matt Mower18794c82015-11-11 16:22:45 -06001750 command = "/sbin/fsck.fat -y " + Actual_Block_Device;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001751 LOGINFO("Repair command: %s\n", command.c_str());
1752 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001753 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001754 return true;
1755 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001756 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001757 return false;
1758 }
1759 }
1760 if (Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") {
1761 if (!TWFunc::Path_Exists("/sbin/e2fsck")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001762 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("e2fsck"<