blob: 9ed0ebcbdc8ab40240c2b8d4716c649db83fdbb9 [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,
Matt Mower2416a502016-04-12 19:54:46 -0500161};
162
163/* Flags without a trailing '=' are considered dual format flags and can be
164 * written as either 'flagname' or 'flagname=', where the character following
165 * the '=' is Y,y,1 for true and false otherwise.
166 */
167const struct flag_list tw_flags[] = {
168 { "andsec", TWFLAG_ANDSEC },
169 { "backup", TWFLAG_BACKUP },
170 { "backupname=", TWFLAG_BACKUPNAME },
171 { "blocksize=", TWFLAG_BLOCKSIZE },
172 { "canbewiped", TWFLAG_CANBEWIPED },
173 { "canencryptbackup", TWFLAG_CANENCRYPTBACKUP },
174 { "defaults", TWFLAG_DEFAULTS },
175 { "display=", TWFLAG_DISPLAY },
176 { "encryptable=", TWFLAG_ENCRYPTABLE },
Ethan Yonkere9afc3d2018-08-30 15:16:27 -0500177 { "fileencryption=", TWFLAG_FILEENCRYPTION },
Matt Mower2416a502016-04-12 19:54:46 -0500178 { "flashimg", TWFLAG_FLASHIMG },
179 { "forceencrypt=", TWFLAG_FORCEENCRYPT },
180 { "fsflags=", TWFLAG_FSFLAGS },
181 { "ignoreblkid", TWFLAG_IGNOREBLKID },
182 { "length=", TWFLAG_LENGTH },
183 { "mounttodecrypt", TWFLAG_MOUNTTODECRYPT },
184 { "removable", TWFLAG_REMOVABLE },
185 { "retainlayoutversion", TWFLAG_RETAINLAYOUTVERSION },
186 { "settingsstorage", TWFLAG_SETTINGSSTORAGE },
187 { "storage", TWFLAG_STORAGE },
188 { "storagename=", TWFLAG_STORAGENAME },
189 { "subpartitionof=", TWFLAG_SUBPARTITIONOF },
190 { "symlink=", TWFLAG_SYMLINK },
191 { "userdataencryptbackup", TWFLAG_USERDATAENCRYPTBACKUP },
192 { "usermrf", TWFLAG_USERMRF },
193 { "wipeduringfactoryreset", TWFLAG_WIPEDURINGFACTORYRESET },
194 { "wipeingui", TWFLAG_WIPEINGUI },
Ethan Yonker1b190162016-12-05 15:25:19 -0600195 { "slotselect", TWFLAG_SLOTSELECT },
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600196 { "wait", TWFLAG_WAIT },
197 { "verify", TWFLAG_VERIFY },
198 { "check", TWFLAG_CHECK },
199 { "altdevice", TWFLAG_ALTDEVICE },
200 { "notrim", TWFLAG_NOTRIM },
201 { "voldmanaged=", TWFLAG_VOLDMANAGED },
202 { "formattable", TWFLAG_FORMATTABLE },
203 { "resize", TWFLAG_RESIZE },
Ethan Yonker93382822018-11-01 15:25:31 -0500204 { "keydirectory=", TWFLAG_KEYDIRECTORY },
Matt Mower2416a502016-04-12 19:54:46 -0500205 { 0, 0 },
206};
207
that9e0593e2014-10-08 00:01:24 +0200208TWPartition::TWPartition() {
Dees_Troy51a0e822012-09-05 15:24:24 -0400209 Can_Be_Mounted = false;
210 Can_Be_Wiped = false;
Dees_Troya13d74f2013-03-24 08:54:55 -0500211 Can_Be_Backed_Up = false;
Vojtech Bocek1dc30982013-08-30 21:49:30 +0200212 Use_Rm_Rf = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400213 Wipe_During_Factory_Reset = false;
214 Wipe_Available_in_GUI = false;
215 Is_SubPartition = false;
Dees_Troy2691f9d2012-09-24 11:15:49 -0400216 Has_SubPartition = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400217 SubPartition_Of = "";
218 Symlink_Path = "";
219 Symlink_Mount_Point = "";
220 Mount_Point = "";
Dees_Troye58d5262012-09-21 12:27:57 -0400221 Backup_Path = "";
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600222 Wildcard_Block_Device = false;
223 Sysfs_Entry = "";
Dees_Troy38bd7602012-09-14 13:33:53 -0400224 Actual_Block_Device = "";
225 Primary_Block_Device = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400226 Alternate_Block_Device = "";
227 Removable = false;
228 Is_Present = false;
229 Length = 0;
230 Size = 0;
231 Used = 0;
232 Free = 0;
233 Backup_Size = 0;
234 Can_Be_Encrypted = false;
235 Is_Encrypted = false;
236 Is_Decrypted = false;
Ethan Yonkerbd7492d2016-12-07 13:55:01 -0600237 Is_FBE = false;
Ethan Yonker253368a2014-11-25 15:00:52 -0600238 Mount_To_Decrypt = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400239 Decrypted_Block_Device = "";
240 Display_Name = "";
Dees_Troya13d74f2013-03-24 08:54:55 -0500241 Backup_Display_Name = "";
242 Storage_Name = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400243 Backup_Name = "";
Dees_Troy63c8df72012-09-10 14:02:05 -0400244 Backup_FileName = "";
Dees_Troy38bd7602012-09-14 13:33:53 -0400245 MTD_Name = "";
bigbiffce8f83c2015-12-12 18:30:21 -0500246 Backup_Method = BM_NONE;
Dees_Troy83bd4832013-05-04 12:39:56 +0000247 Can_Encrypt_Backup = false;
248 Use_Userdata_Encryption = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400249 Has_Data_Media = false;
Dees_Troye58d5262012-09-21 12:27:57 -0400250 Has_Android_Secure = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400251 Is_Storage = false;
Dees_Troya13d74f2013-03-24 08:54:55 -0500252 Is_Settings_Storage = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400253 Storage_Path = "";
254 Current_File_System = "";
255 Fstab_File_System = "";
Hashcode62bd9e02013-11-19 21:59:42 -0800256 Mount_Flags = 0;
257 Mount_Options = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400258 Format_Block_Size = 0;
Dees_Troy68cab492012-12-12 19:29:35 +0000259 Ignore_Blkid = false;
Dees_Troy16c2b312013-01-15 16:51:18 +0000260 Retain_Layout_Version = false;
dianlujitao4879b372018-12-03 18:45:47 +0800261 Crypto_Key_Location = "";
Ethan Yonker726a0202014-12-16 20:01:38 -0600262 MTP_Storage_ID = 0;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600263 Can_Flash_Img = false;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500264 Mount_Read_Only = false;
Ethan Yonker66a19492015-12-10 10:19:45 -0600265 Is_Adopted_Storage = false;
266 Adopted_GUID = "";
Ethan Yonker1b190162016-12-05 15:25:19 -0600267 SlotSelect = false;
Ethan Yonker93382822018-11-01 15:25:31 -0500268 Key_Directory = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400269}
270
271TWPartition::~TWPartition(void) {
272 // Do nothing
273}
274
Chaosmasterf6e42ce2020-01-27 00:17:04 +0100275bool 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 -0500276 char full_line[MAX_FSTAB_LINE_LENGTH];
Matt Mower2416a502016-04-12 19:54:46 -0500277 char twflags[MAX_FSTAB_LINE_LENGTH] = "";
Dees_Troy5bf43922012-09-07 16:07:55 -0400278 char* ptr;
Matt Mower2b2dd152016-04-26 11:24:08 -0500279 int line_len = strlen(fstab_line), index = 0, item_index = 0;
Dees_Troya13d74f2013-03-24 08:54:55 -0500280 bool skip = false;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600281 int fstab_version = 1, mount_point_index = 0, fs_index = 1, block_device_index = 2;
282 TWPartition *additional_entry = NULL;
283 std::map<string, Flags_Map>::iterator it;
Dees_Troy5bf43922012-09-07 16:07:55 -0400284
Matt Mower2b2dd152016-04-26 11:24:08 -0500285 strlcpy(full_line, fstab_line, sizeof(full_line));
Dees_Troy51127312012-09-08 13:08:49 -0400286 for (index = 0; index < line_len; index++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500287 if (full_line[index] == 34)
288 skip = !skip;
289 if (!skip && full_line[index] <= 32)
Dees_Troy5bf43922012-09-07 16:07:55 -0400290 full_line[index] = '\0';
Dees_Troy5bf43922012-09-07 16:07:55 -0400291 }
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600292 if (line_len < 10)
293 return false; // There can't possibly be a valid fstab line that is less than 10 chars
294 if (strncmp(fstab_line, "/dev/", strlen("/dev/")) == 0 || strncmp(fstab_line, "/devices/", strlen("/devices/")) == 0) {
295 fstab_version = 2;
296 block_device_index = 0;
297 mount_point_index = 1;
298 fs_index = 2;
299 }
300
301 index = 0;
Dees_Troy5bf43922012-09-07 16:07:55 -0400302 while (index < line_len) {
303 while (index < line_len && full_line[index] == '\0')
304 index++;
305 if (index >= line_len)
306 continue;
307 ptr = full_line + index;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600308 if (item_index == mount_point_index) {
309 Mount_Point = ptr;
310 if (fstab_version == 2) {
311 additional_entry = PartitionManager.Find_Partition_By_Path(Mount_Point);
Chaosmasterf6e42ce2020-01-27 00:17:04 +0100312 if (!Sar_Detect && additional_entry) {
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600313 LOGINFO("Found an additional entry for '%s'\n", Mount_Point.c_str());
314 }
315 }
Chaosmasterf6e42ce2020-01-27 00:17:04 +0100316 if(!Sar_Detect)
317 LOGINFO("Processing '%s'\n", Mount_Point.c_str());
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600318 Backup_Path = Mount_Point;
319 Storage_Path = Mount_Point;
320 Display_Name = ptr + 1;
321 Backup_Display_Name = Display_Name;
322 Storage_Name = Display_Name;
323 item_index++;
324 } else if (item_index == fs_index) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400325 // File System
326 Fstab_File_System = ptr;
327 Current_File_System = ptr;
328 item_index++;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600329 } else if (item_index == block_device_index) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400330 // Primary Block Device
Dees_Troy38bd7602012-09-14 13:33:53 -0400331 if (Fstab_File_System == "mtd" || Fstab_File_System == "yaffs2") {
Dees_Troy094207a2012-09-26 12:00:39 -0400332 MTD_Name = ptr;
333 Find_MTD_Block_Device(MTD_Name);
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400334 } else if (Fstab_File_System == "bml") {
335 if (Mount_Point == "/boot")
336 MTD_Name = "boot";
337 else if (Mount_Point == "/recovery")
338 MTD_Name = "recovery";
339 Primary_Block_Device = ptr;
340 if (*ptr != '/')
Dees_Troy2673cec2013-04-02 20:22:16 +0000341 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 -0400342 } else if (*ptr != '/') {
Dees_Troy5bf43922012-09-07 16:07:55 -0400343 if (Display_Error)
Matt Mower2b2dd152016-04-26 11:24:08 -0500344 LOGERR("Invalid block device '%s' in fstab line '%s'", ptr, fstab_line);
Dees_Troy5bf43922012-09-07 16:07:55 -0400345 else
Matt Mower2b2dd152016-04-26 11:24:08 -0500346 LOGINFO("Invalid block device '%s' in fstab line '%s'", ptr, fstab_line);
Matt Mower72c87ce2016-04-26 14:34:56 -0500347 return false;
Dees_Troy38bd7602012-09-14 13:33:53 -0400348 } else {
349 Primary_Block_Device = ptr;
350 Find_Real_Block_Device(Primary_Block_Device, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400351 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400352 item_index++;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600353 } else if (item_index > 2) {
354 if (fstab_version == 2) {
355 if (item_index == 3) {
356 Process_FS_Flags(ptr);
357 if (additional_entry) {
358 additional_entry->Save_FS_Flags(Fstab_File_System, Mount_Flags, Mount_Options);
359 return false; // We save the extra fs flags in the other partition entry and by returning false, this entry will be deleted
360 }
361 } else {
362 strlcpy(twflags, ptr, sizeof(twflags));
363 }
364 item_index++;
365 } else if (*ptr == '/') { // v2 fstab does not allow alternate block devices
Dees_Troy5bf43922012-09-07 16:07:55 -0400366 // Alternate Block Device
367 Alternate_Block_Device = ptr;
368 Find_Real_Block_Device(Alternate_Block_Device, Display_Error);
369 } else if (strlen(ptr) > 7 && strncmp(ptr, "length=", 7) == 0) {
370 // Partition length
371 ptr += 7;
372 Length = atoi(ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400373 } else if (strlen(ptr) > 6 && strncmp(ptr, "flags=", 6) == 0) {
374 // Custom flags, save for later so that new values aren't overwritten by defaults
375 ptr += 6;
Matt Mower2416a502016-04-12 19:54:46 -0500376 strlcpy(twflags, ptr, sizeof(twflags));
Dees_Troy38bd7602012-09-14 13:33:53 -0400377 } else if (strlen(ptr) == 4 && (strncmp(ptr, "NULL", 4) == 0 || strncmp(ptr, "null", 4) == 0 || strncmp(ptr, "null", 4) == 0)) {
378 // Do nothing
Dees_Troy5bf43922012-09-07 16:07:55 -0400379 } else {
380 // Unhandled data
Matt Mower2b2dd152016-04-26 11:24:08 -0500381 LOGINFO("Unhandled fstab information '%s' in fstab line '%s'\n", ptr, fstab_line);
Dees_Troy5bf43922012-09-07 16:07:55 -0400382 }
383 }
384 while (index < line_len && full_line[index] != '\0')
385 index++;
386 }
387
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600388 // override block devices from the v2 fstab with the ones we read from the twrp.flags file in case they are different
389 if (fstab_version == 2 && twrp_flags && twrp_flags->size() > 0) {
390 it = twrp_flags->find(Mount_Point);
391 if (it != twrp_flags->end()) {
392 if (!it->second.Primary_Block_Device.empty()) {
393 Primary_Block_Device = it->second.Primary_Block_Device;
394 Find_Real_Block_Device(Primary_Block_Device, Display_Error);
395 }
396 if (!it->second.Alternate_Block_Device.empty()) {
397 Alternate_Block_Device = it->second.Alternate_Block_Device;
398 Find_Real_Block_Device(Alternate_Block_Device, Display_Error);
399 }
400 }
401 }
402
403 if (strncmp(fstab_line, "/devices/", strlen("/devices/")) == 0) {
404 Sysfs_Entry = Primary_Block_Device;
405 Primary_Block_Device = "";
406 Is_Storage = true;
407 Removable = true;
408 Wipe_Available_in_GUI = true;
409 Wildcard_Block_Device = true;
410 }
411 if (Primary_Block_Device.find("*") != string::npos)
412 Wildcard_Block_Device = true;
413
Chaosmasterf6e42ce2020-01-27 00:17:04 +0100414 if (Sar_Detect) {
415 if(Is_File_System(Fstab_File_System) && (Mount_Point == "/" || Mount_Point == "/system" || Mount_Point == "/system_root"))
416 Find_Actual_Block_Device();
417 else
418 return true;
419 } else if (Mount_Point == "auto") {
dianlujitaob76a73a2020-04-30 20:33:20 +0800420 Mount_Point = "/auto" + to_string(auto_index);
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600421 Backup_Path = Mount_Point;
422 Storage_Path = Mount_Point;
dianlujitaob76a73a2020-04-30 20:33:20 +0800423 Backup_Name = Mount_Point.substr(1);
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600424 auto_index++;
425 Setup_File_System(Display_Error);
426 Display_Name = "Storage";
427 Backup_Display_Name = Display_Name;
428 Storage_Name = Display_Name;
429 Can_Be_Backed_Up = false;
430 Wipe_Available_in_GUI = true;
431 Is_Storage = true;
432 Removable = true;
433 Wipe_Available_in_GUI = true;
434 } else if (!Is_File_System(Fstab_File_System) && !Is_Image(Fstab_File_System)) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400435 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000436 LOGERR("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400437 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000438 LOGINFO("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Matt Mower72c87ce2016-04-26 14:34:56 -0500439 return false;
Dees_Troy5bf43922012-09-07 16:07:55 -0400440 } else if (Is_File_System(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400441 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400442 Setup_File_System(Display_Error);
dianlujitaob76a73a2020-04-30 20:33:20 +0800443 Backup_Name = Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
Chaosmaster3b682732020-01-25 21:09:28 +0100444 if (Mount_Point == "/" || Mount_Point == "/system" || Mount_Point == "/system_root") {
Dees_Troy5bf43922012-09-07 16:07:55 -0400445 Display_Name = "System";
dianlujitaob76a73a2020-04-30 20:33:20 +0800446 Backup_Name = "system";
Dees_Troya13d74f2013-03-24 08:54:55 -0500447 Backup_Display_Name = Display_Name;
448 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400449 Wipe_Available_in_GUI = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500450 Can_Be_Backed_Up = true;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500451 Mount_Read_Only = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400452 } else if (Mount_Point == "/data") {
453 Display_Name = "Data";
Dees_Troya13d74f2013-03-24 08:54:55 -0500454 Backup_Display_Name = Display_Name;
455 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400456 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400457 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500458 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000459 Can_Encrypt_Backup = true;
460 Use_Userdata_Encryption = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400461 } else if (Mount_Point == "/cache") {
462 Display_Name = "Cache";
Dees_Troya13d74f2013-03-24 08:54:55 -0500463 Backup_Display_Name = Display_Name;
464 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400465 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400466 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500467 Can_Be_Backed_Up = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400468 } else if (Mount_Point == "/datadata") {
Dees_Troy51127312012-09-08 13:08:49 -0400469 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400470 Display_Name = "DataData";
Dees_Troya13d74f2013-03-24 08:54:55 -0500471 Backup_Display_Name = Display_Name;
472 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400473 Is_SubPartition = true;
474 SubPartition_Of = "/data";
Dees_Troy5bf43922012-09-07 16:07:55 -0400475 DataManager::SetValue(TW_HAS_DATADATA, 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500476 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000477 Can_Encrypt_Backup = true;
478 Use_Userdata_Encryption = false; // This whole partition should be encrypted
Dees_Troy5bf43922012-09-07 16:07:55 -0400479 } else if (Mount_Point == "/sd-ext") {
Dees_Troy51127312012-09-08 13:08:49 -0400480 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400481 Display_Name = "SD-Ext";
Dees_Troya13d74f2013-03-24 08:54:55 -0500482 Backup_Display_Name = Display_Name;
483 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400484 Wipe_Available_in_GUI = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400485 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500486 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000487 Can_Encrypt_Backup = true;
488 Use_Userdata_Encryption = true;
Dees_Troy2c50e182012-09-26 20:05:28 -0400489 } else if (Mount_Point == "/boot") {
490 Display_Name = "Boot";
Dees_Troya13d74f2013-03-24 08:54:55 -0500491 Backup_Display_Name = Display_Name;
Dees_Troy2c50e182012-09-26 20:05:28 -0400492 DataManager::SetValue("tw_boot_is_mountable", 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500493 Can_Be_Backed_Up = true;
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500494 } else if (Mount_Point == "/vendor") {
495 Display_Name = "Vendor";
496 Backup_Display_Name = Display_Name;
497 Storage_Name = Display_Name;
498 Mount_Read_Only = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400499 }
500#ifdef TW_EXTERNAL_STORAGE_PATH
501 if (Mount_Point == EXPAND(TW_EXTERNAL_STORAGE_PATH)) {
502 Is_Storage = true;
503 Storage_Path = EXPAND(TW_EXTERNAL_STORAGE_PATH);
Dees_Troyc51f1f92012-09-20 15:32:13 -0400504 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500505 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400506#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000507 if (Mount_Point == "/sdcard" || Mount_Point == "/external_sd" || Mount_Point == "/external_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400508 Is_Storage = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400509 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500510 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400511#endif
Dees_Troyb05ddee2013-01-28 20:24:50 +0000512 }
Dees_Troy8170a922012-09-18 15:40:25 -0400513#ifdef TW_INTERNAL_STORAGE_PATH
514 if (Mount_Point == EXPAND(TW_INTERNAL_STORAGE_PATH)) {
515 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500516 Is_Settings_Storage = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400517 Storage_Path = EXPAND(TW_INTERNAL_STORAGE_PATH);
Dees_Troya13d74f2013-03-24 08:54:55 -0500518 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400519 }
520#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000521 if (Mount_Point == "/emmc" || Mount_Point == "/internal_sd" || Mount_Point == "/internal_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400522 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500523 Is_Settings_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500524 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400525 }
526#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400527 } else if (Is_Image(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400528 Find_Actual_Block_Device();
Ethan Yonker1b190162016-12-05 15:25:19 -0600529 Setup_Image();
Dees_Troya13d74f2013-03-24 08:54:55 -0500530 if (Mount_Point == "/boot") {
531 Display_Name = "Boot";
532 Backup_Display_Name = Display_Name;
533 Can_Be_Backed_Up = true;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600534 Can_Flash_Img = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500535 } else if (Mount_Point == "/recovery") {
536 Display_Name = "Recovery";
537 Backup_Display_Name = Display_Name;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600538 Can_Flash_Img = true;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500539 } else if (Mount_Point == "/system_image") {
540 Display_Name = "System Image";
541 Backup_Display_Name = Display_Name;
HashBanged974bb2016-01-30 14:20:09 -0500542 Can_Flash_Img = true;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500543 Can_Be_Backed_Up = true;
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500544 } else if (Mount_Point == "/vendor_image") {
545 Display_Name = "Vendor Image";
546 Backup_Display_Name = Display_Name;
HashBanged974bb2016-01-30 14:20:09 -0500547 Can_Flash_Img = true;
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500548 Can_Be_Backed_Up = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500549 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400550 }
551
Matt Mower2416a502016-04-12 19:54:46 -0500552 // Process TWRP fstab flags
553 if (strlen(twflags) > 0) {
554 string Prev_Display_Name = Display_Name;
555 string Prev_Storage_Name = Storage_Name;
556 string Prev_Backup_Display_Name = Backup_Display_Name;
557 Display_Name = "";
558 Storage_Name = "";
559 Backup_Display_Name = "";
560
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600561 Process_TW_Flags(twflags, (fstab_version == 1), fstab_version);
562 Save_FS_Flags(Fstab_File_System, Mount_Flags, Mount_Options);
Matt Mower2416a502016-04-12 19:54:46 -0500563
564 bool has_display_name = !Display_Name.empty();
565 bool has_storage_name = !Storage_Name.empty();
566 bool has_backup_name = !Backup_Display_Name.empty();
567 if (!has_display_name) Display_Name = Prev_Display_Name;
568 if (!has_storage_name) Storage_Name = Prev_Storage_Name;
569 if (!has_backup_name) Backup_Display_Name = Prev_Backup_Display_Name;
570
571 if (has_display_name && !has_storage_name)
572 Storage_Name = Display_Name;
573 if (!has_display_name && has_storage_name)
574 Display_Name = Storage_Name;
575 if (has_display_name && !has_backup_name && Backup_Display_Name != "Android Secure")
576 Backup_Display_Name = Display_Name;
577 if (!has_display_name && has_backup_name)
578 Display_Name = Backup_Display_Name;
579 }
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600580
581 if (fstab_version == 2 && twrp_flags && twrp_flags->size() > 0) {
582 it = twrp_flags->find(Mount_Point);
583 if (it != twrp_flags->end()) {
584 char twrpflags[MAX_FSTAB_LINE_LENGTH] = "";
585 int skip = 0;
586 string Flags = it->second.Flags;
587 strcpy(twrpflags, Flags.c_str());
588 if (strlen(twrpflags) > strlen("flags=") && strncmp(twrpflags, "flags=", strlen("flags=")) == 0)
589 skip += strlen("flags=");
590 char* flagptr = twrpflags;
591 flagptr += skip;
592 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
593 }
594 }
nkk7198fc3992017-12-16 16:26:42 +0200595
596 if (Mount_Point == "/persist" && Can_Be_Mounted) {
597 bool mounted = Is_Mounted();
598 if (mounted || Mount(false)) {
599 // Read the backup settings file
600 DataManager::LoadPersistValues();
601 TWFunc::Fixup_Time_On_Boot("/persist/time/");
602 if (!mounted)
603 UnMount(false);
604 }
605 }
606
Chaosmasterf6e42ce2020-01-27 00:17:04 +0100607 if (Is_File_System(Fstab_File_System) && (Mount_Point == "/" || Mount_Point == "/system" || Mount_Point == "/system_root")) {
608 if (Sar_Detect) {
609 Mount_Point = "/s";
610 Mount_Read_Only = true;
611 Can_Be_Mounted = true;
612 } else {
613 Mount_Point = PartitionManager.Get_Android_Root_Path();
614 Backup_Path = Mount_Point;
615 Storage_Path = Mount_Point;
616 Make_Dir(Mount_Point, Display_Error);
617 }
618 }
619
Dees_Troy51127312012-09-08 13:08:49 -0400620 return true;
621}
622
Matt Mower72c87ce2016-04-26 14:34:56 -0500623void TWPartition::Partition_Post_Processing(bool Display_Error) {
624 if (Mount_Point == "/data")
625 Setup_Data_Partition(Display_Error);
626 else if (Mount_Point == "/cache")
627 Setup_Cache_Partition(Display_Error);
628}
629
Ethan Yonkerbd7492d2016-12-07 13:55:01 -0600630void TWPartition::ExcludeAll(const string& path) {
631 backup_exclusions.add_absolute_dir(path);
632 wipe_exclusions.add_absolute_dir(path);
633}
634
Matt Mower72c87ce2016-04-26 14:34:56 -0500635void TWPartition::Setup_Data_Partition(bool Display_Error) {
636 if (Mount_Point != "/data")
637 return;
638
639 // Ensure /data is not mounted as tmpfs for qcom hardware decrypt
640 UnMount(false);
641
642#ifdef TW_INCLUDE_CRYPTO
643 if (datamedia)
644 Setup_Data_Media();
645 Can_Be_Encrypted = true;
646 char crypto_blkdev[255];
647 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
648 if (strcmp(crypto_blkdev, "error") != 0) {
649 DataManager::SetValue(TW_IS_DECRYPTED, 1);
650 Is_Encrypted = true;
651 Is_Decrypted = true;
Ethan Yonker93382822018-11-01 15:25:31 -0500652 if (Key_Directory.empty())
653 Is_FBE = false;
654 else
655 Is_FBE = true;
Ethan Yonkerbd7492d2016-12-07 13:55:01 -0600656 DataManager::SetValue(TW_IS_FBE, 0);
Matt Mower72c87ce2016-04-26 14:34:56 -0500657 Decrypted_Block_Device = crypto_blkdev;
658 LOGINFO("Data already decrypted, new block device: '%s'\n", crypto_blkdev);
659 } else if (!Mount(false)) {
660 if (Is_Present) {
Ethan Yonker93382822018-11-01 15:25:31 -0500661 if (Key_Directory.empty()) {
662 set_partition_data(Actual_Block_Device.c_str(), Crypto_Key_Location.c_str(), Fstab_File_System.c_str());
663 if (cryptfs_check_footer() == 0) {
664 Is_Encrypted = true;
665 Is_Decrypted = false;
666 Can_Be_Mounted = false;
667 Current_File_System = "emmc";
668 Setup_Image();
669 DataManager::SetValue(TW_IS_ENCRYPTED, 1);
670 DataManager::SetValue(TW_CRYPTO_PWTYPE, cryptfs_get_password_type());
671 DataManager::SetValue(TW_CRYPTO_PASSWORD, "");
672 DataManager::SetValue("tw_crypto_display", "");
673 } else {
674 gui_err("mount_data_footer=Could not mount /data and unable to find crypto footer.");
675 }
676 } else {
Matt Mower72c87ce2016-04-26 14:34:56 -0500677 Is_Encrypted = true;
678 Is_Decrypted = false;
Matt Mower72c87ce2016-04-26 14:34:56 -0500679 }
Ethan Yonker93382822018-11-01 15:25:31 -0500680 } else if (Key_Directory.empty()) {
Matt Mower72c87ce2016-04-26 14:34:56 -0500681 LOGERR("Primary block device '%s' for mount point '%s' is not present!\n", Primary_Block_Device.c_str(), Mount_Point.c_str());
682 }
683 } else {
mauronofrio9bf73352019-11-23 22:27:34 +0100684
685 if (!Decrypt_FBE_DE()) {
686 LOGINFO("Trying wrapped key.\n");
687 property_set("fbe.data.wrappedkey", "true");
688 if (!Decrypt_FBE_DE()) {
Mauronofrio Matarrese6d5d08a2019-12-03 14:25:25 +0100689 LOGINFO("Unable to decrypt device\n");
mauronofrio9bf73352019-11-23 22:27:34 +0100690 }
691 }
Matt Mower72c87ce2016-04-26 14:34:56 -0500692 }
693 if (datamedia && (!Is_Encrypted || (Is_Encrypted && Is_Decrypted))) {
694 Setup_Data_Media();
695 Recreate_Media_Folder();
696 }
697#else
698 if (datamedia) {
699 Setup_Data_Media();
700 Recreate_Media_Folder();
701 }
702#endif
703}
704
Ethan Yonker93382822018-11-01 15:25:31 -0500705bool TWPartition::Decrypt_FBE_DE() {
706if (TWFunc::Path_Exists("/data/unencrypted/key/version")) {
707 LOGINFO("File Based Encryption is present\n");
708#ifdef TW_INCLUDE_FBE
709 ExcludeAll(Mount_Point + "/convert_fbe");
710 ExcludeAll(Mount_Point + "/unencrypted");
711 //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
712 ExcludeAll(Mount_Point + "/misc/vold/user_keys");
713 //ExcludeAll(Mount_Point + "/system_ce");
714 //ExcludeAll(Mount_Point + "/system_de");
715 //ExcludeAll(Mount_Point + "/misc_ce");
716 //ExcludeAll(Mount_Point + "/misc_de");
717 ExcludeAll(Mount_Point + "/system/gatekeeper.password.key");
718 ExcludeAll(Mount_Point + "/system/gatekeeper.pattern.key");
719 ExcludeAll(Mount_Point + "/system/locksettings.db");
720 //ExcludeAll(Mount_Point + "/system/locksettings.db-shm"); // don't seem to need this one, but the other 2 are needed
721 ExcludeAll(Mount_Point + "/system/locksettings.db-wal");
722 //ExcludeAll(Mount_Point + "/user_de");
723 //ExcludeAll(Mount_Point + "/misc/profiles/cur/0"); // might be important later
724 ExcludeAll(Mount_Point + "/misc/gatekeeper");
725 ExcludeAll(Mount_Point + "/misc/keystore");
726 ExcludeAll(Mount_Point + "/drm/kek.dat");
727 ExcludeAll(Mount_Point + "/system_de/0/spblob"); // contains data needed to decrypt pixel 2
728 int retry_count = 3;
729 while (!Decrypt_DE() && --retry_count)
730 usleep(2000);
731 if (retry_count > 0) {
732 property_set("ro.crypto.state", "encrypted");
733 Is_Encrypted = true;
734 Is_Decrypted = false;
735 Is_FBE = true;
736 DataManager::SetValue(TW_IS_FBE, 1);
737 DataManager::SetValue(TW_IS_ENCRYPTED, 1);
738 string filename;
739 int pwd_type = Get_Password_Type(0, filename);
740 if (pwd_type < 0) {
741 LOGERR("This TWRP does not have synthetic password decrypt support\n");
742 pwd_type = 0; // default password
743 }
744 DataManager::SetValue(TW_CRYPTO_PWTYPE, pwd_type);
745 DataManager::SetValue(TW_CRYPTO_PASSWORD, "");
746 DataManager::SetValue("tw_crypto_display", "");
747 return true;
748 }
749#else
750 LOGERR("FBE found but FBE support not present in TWRP\n");
751#endif
752 }
753 return false;
754}
755
Matt Mower72c87ce2016-04-26 14:34:56 -0500756void TWPartition::Setup_Cache_Partition(bool Display_Error __unused) {
757 if (Mount_Point != "/cache")
758 return;
759
760 if (!Mount(true))
761 return;
762
763 if (!TWFunc::Path_Exists("/cache/recovery/.")) {
764 LOGINFO("Recreating /cache/recovery folder\n");
765 if (mkdir("/cache/recovery", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0)
766 LOGERR("Could not create /cache/recovery\n");
767 }
768}
769
Matt Mower4ab42b12016-04-21 13:52:18 -0500770void TWPartition::Process_FS_Flags(const char *str) {
771 char *options = strdup(str);
772 char *ptr, *savep;
Hashcode62bd9e02013-11-19 21:59:42 -0800773
Matt Mower4ab42b12016-04-21 13:52:18 -0500774 Mount_Options = "";
Hashcode62bd9e02013-11-19 21:59:42 -0800775
Matt Mower4ab42b12016-04-21 13:52:18 -0500776 // Avoid issues with potentially nested strtok by using strtok_r
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600777 for (ptr = strtok_r(options, ",", &savep); ptr; ptr = strtok_r(NULL, ",", &savep)) {
778 char *equals = strstr(ptr, "=");
779 size_t name_len;
Matt Mower4ab42b12016-04-21 13:52:18 -0500780
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600781 if (!equals)
782 name_len = strlen(ptr);
783 else
784 name_len = equals - ptr;
785
786 // There are some flags that we want to ignore in TWRP
787 bool found_match = false;
788 for (const char** ignored_mount_item = ignored_mount_items; *ignored_mount_item; ignored_mount_item++) {
789 if (strncmp(ptr, *ignored_mount_item, name_len) == 0) {
790 found_match = true;
Hashcode62bd9e02013-11-19 21:59:42 -0800791 break;
792 }
793 }
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600794 if (found_match)
795 continue;
Hashcode62bd9e02013-11-19 21:59:42 -0800796
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600797 // mount_flags are never postfixed by '='
798 if (!equals) {
799 const struct flag_list* mount_flag = mount_flags;
800 for (; mount_flag->name; mount_flag++) {
801 if (strcmp(ptr, mount_flag->name) == 0) {
802 if (mount_flag->flag == MS_RDONLY)
803 Mount_Read_Only = true;
804 else
805 Mount_Flags |= (unsigned)mount_flag->flag;
806 found_match = true;
807 break;
808 }
809 }
810 if (found_match)
811 continue;
Matt Mower4ab42b12016-04-21 13:52:18 -0500812 }
813
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600814 // If we aren't ignoring this flag and it's not a mount flag, then it must be a mount option
815 if (!Mount_Options.empty())
816 Mount_Options += ",";
817 Mount_Options += ptr;
Matt Mower4ab42b12016-04-21 13:52:18 -0500818 }
819 free(options);
Hashcode62bd9e02013-11-19 21:59:42 -0800820}
821
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600822void TWPartition::Save_FS_Flags(const string& local_File_System, int local_Mount_Flags, const string& local_Mount_Options) {
823 partition_fs_flags_struct flags;
824 flags.File_System = local_File_System;
825 flags.Mount_Flags = local_Mount_Flags;
826 flags.Mount_Options = local_Mount_Options;
827 fs_flags.push_back(flags);
828}
829
Matt Mower2416a502016-04-12 19:54:46 -0500830void TWPartition::Apply_TW_Flag(const unsigned flag, const char* str, const bool val) {
831 switch (flag) {
832 case TWFLAG_ANDSEC:
833 Has_Android_Secure = val;
834 break;
835 case TWFLAG_BACKUP:
836 Can_Be_Backed_Up = val;
837 break;
838 case TWFLAG_BACKUPNAME:
839 Backup_Display_Name = str;
840 break;
841 case TWFLAG_BLOCKSIZE:
842 Format_Block_Size = (unsigned long)(atol(str));
843 break;
844 case TWFLAG_CANBEWIPED:
845 Can_Be_Wiped = val;
846 break;
847 case TWFLAG_CANENCRYPTBACKUP:
848 Can_Encrypt_Backup = val;
849 break;
850 case TWFLAG_DEFAULTS:
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600851 case TWFLAG_WAIT:
852 case TWFLAG_VERIFY:
853 case TWFLAG_CHECK:
854 case TWFLAG_NOTRIM:
855 case TWFLAG_VOLDMANAGED:
856 case TWFLAG_RESIZE:
Matt Mower2416a502016-04-12 19:54:46 -0500857 // Do nothing
858 break;
859 case TWFLAG_DISPLAY:
860 Display_Name = str;
861 break;
862 case TWFLAG_ENCRYPTABLE:
863 case TWFLAG_FORCEENCRYPT:
864 Crypto_Key_Location = str;
865 break;
Ethan Yonkere9afc3d2018-08-30 15:16:27 -0500866 case TWFLAG_FILEENCRYPTION:
867 // This flag isn't used by TWRP but is needed in 9.0 FBE decrypt
868 // fileencryption=ice:aes-256-heh
869 {
870 std::string FBE = str;
Ethan Yonkere9afc3d2018-08-30 15:16:27 -0500871 size_t colon_loc = FBE.find(":");
872 if (colon_loc == std::string::npos) {
Ethan Yonker93382822018-11-01 15:25:31 -0500873 property_set("fbe.contents", FBE.c_str());
874 property_set("fbe.filenames", "");
875 LOGINFO("FBE contents '%s', filenames ''\n", FBE.c_str());
Ethan Yonkere9afc3d2018-08-30 15:16:27 -0500876 break;
877 }
Ethan Yonker93382822018-11-01 15:25:31 -0500878 std::string FBE_contents, FBE_filenames;
Ethan Yonkere9afc3d2018-08-30 15:16:27 -0500879 FBE_contents = FBE.substr(0, colon_loc);
880 FBE_filenames = FBE.substr(colon_loc + 1);
881 property_set("fbe.contents", FBE_contents.c_str());
882 property_set("fbe.filenames", FBE_filenames.c_str());
883 LOGINFO("FBE contents '%s', filenames '%s'\n", FBE_contents.c_str(), FBE_filenames.c_str());
884 }
885 break;
Matt Mower2416a502016-04-12 19:54:46 -0500886 case TWFLAG_FLASHIMG:
887 Can_Flash_Img = val;
888 break;
889 case TWFLAG_FSFLAGS:
Matt Mower4ab42b12016-04-21 13:52:18 -0500890 Process_FS_Flags(str);
Matt Mower2416a502016-04-12 19:54:46 -0500891 break;
892 case TWFLAG_IGNOREBLKID:
893 Ignore_Blkid = val;
894 break;
895 case TWFLAG_LENGTH:
896 Length = atoi(str);
897 break;
898 case TWFLAG_MOUNTTODECRYPT:
899 Mount_To_Decrypt = val;
900 break;
901 case TWFLAG_REMOVABLE:
902 Removable = val;
903 break;
904 case TWFLAG_RETAINLAYOUTVERSION:
905 Retain_Layout_Version = val;
906 break;
907 case TWFLAG_SETTINGSSTORAGE:
908 Is_Settings_Storage = val;
909 if (Is_Settings_Storage)
910 Is_Storage = true;
911 break;
912 case TWFLAG_STORAGE:
913 Is_Storage = val;
914 break;
915 case TWFLAG_STORAGENAME:
916 Storage_Name = str;
917 break;
918 case TWFLAG_SUBPARTITIONOF:
919 Is_SubPartition = true;
920 SubPartition_Of = str;
921 break;
922 case TWFLAG_SYMLINK:
923 Symlink_Path = str;
924 break;
925 case TWFLAG_USERDATAENCRYPTBACKUP:
926 Use_Userdata_Encryption = val;
927 if (Use_Userdata_Encryption)
928 Can_Encrypt_Backup = true;
929 break;
930 case TWFLAG_USERMRF:
931 Use_Rm_Rf = val;
932 break;
933 case TWFLAG_WIPEDURINGFACTORYRESET:
934 Wipe_During_Factory_Reset = val;
935 if (Wipe_During_Factory_Reset) {
936 Can_Be_Wiped = true;
937 Wipe_Available_in_GUI = true;
938 }
939 break;
940 case TWFLAG_WIPEINGUI:
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600941 case TWFLAG_FORMATTABLE:
Matt Mower2416a502016-04-12 19:54:46 -0500942 Wipe_Available_in_GUI = val;
943 if (Wipe_Available_in_GUI)
944 Can_Be_Wiped = true;
945 break;
Ethan Yonker1b190162016-12-05 15:25:19 -0600946 case TWFLAG_SLOTSELECT:
947 SlotSelect = true;
948 break;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600949 case TWFLAG_ALTDEVICE:
950 Alternate_Block_Device = str;
951 break;
Ethan Yonker93382822018-11-01 15:25:31 -0500952 case TWFLAG_KEYDIRECTORY:
953 Key_Directory = str;
Matt Mower2416a502016-04-12 19:54:46 -0500954 default:
955 // Should not get here
956 LOGINFO("Flag identified for processing, but later unmatched: %i\n", flag);
957 break;
958 }
959}
Dees_Troy51127312012-09-08 13:08:49 -0400960
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600961void TWPartition::Process_TW_Flags(char *flags, bool Display_Error, int fstab_ver) {
Matt Mower2416a502016-04-12 19:54:46 -0500962 char separator[2] = {'\n', 0};
963 char *ptr, *savep;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600964 char source_separator = ';';
965
966 if (fstab_ver == 2)
967 source_separator = ',';
Matt Mower2416a502016-04-12 19:54:46 -0500968
969 // Semicolons within double-quotes are not forbidden, so replace
970 // only the semicolons intended as separators with '\n' for strtok
971 for (unsigned i = 0, skip = 0; i < strlen(flags); i++) {
972 if (flags[i] == '\"')
Dees_Troya13d74f2013-03-24 08:54:55 -0500973 skip = !skip;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600974 if (!skip && flags[i] == source_separator)
Matt Mower2416a502016-04-12 19:54:46 -0500975 flags[i] = separator[0];
Dees_Troy51127312012-09-08 13:08:49 -0400976 }
977
Matt Mower2416a502016-04-12 19:54:46 -0500978 // Avoid issues with potentially nested strtok by using strtok_r
979 ptr = strtok_r(flags, separator, &savep);
980 while (ptr) {
981 int ptr_len = strlen(ptr);
982 const struct flag_list* tw_flag = tw_flags;
Hashcode62bd9e02013-11-19 21:59:42 -0800983
Matt Mower2416a502016-04-12 19:54:46 -0500984 for (; tw_flag->name; tw_flag++) {
985 int flag_len = strlen(tw_flag->name);
986
987 if (strncmp(ptr, tw_flag->name, flag_len) == 0) {
988 bool flag_val = false;
989
990 if (ptr_len > flag_len && (tw_flag->name)[flag_len-1] != '='
991 && ptr[flag_len] != '=') {
992 // Handle flags with same starting string
993 // (e.g. backup and backupname)
994 continue;
995 } else if (ptr_len > flag_len && ptr[flag_len] == '=') {
996 // Handle flags with dual format: Part 1
997 // (e.g. backup and backup=y. backup=y handled here)
998 ptr += flag_len + 1;
999 TWFunc::Strip_Quotes(ptr);
1000 // Skip flags with empty argument
1001 // (e.g. backup=)
1002 if (strlen(ptr) == 0) {
1003 LOGINFO("Flag missing argument or should not include '=': %s=\n", tw_flag->name);
1004 break;
1005 }
1006 flag_val = strchr("yY1", *ptr) != NULL;
1007 } else if (ptr_len == flag_len
1008 && (tw_flag->name)[flag_len-1] == '=') {
1009 // Skip flags missing argument after =
1010 // (e.g. backupname=)
1011 LOGINFO("Flag missing argument: %s\n", tw_flag->name);
1012 break;
1013 } else if (ptr_len > flag_len
1014 && (tw_flag->name)[flag_len-1] == '=') {
1015 // Handle arguments to flags
1016 // (e.g. backupname="My Stuff")
1017 ptr += flag_len;
1018 TWFunc::Strip_Quotes(ptr);
1019 // Skip flags with empty argument
1020 // (e.g. backupname="")
1021 if (strlen(ptr) == 0) {
1022 LOGINFO("Flag missing argument: %s\n", tw_flag->name);
1023 break;
1024 }
1025 } else if (ptr_len == flag_len) {
1026 // Handle flags with dual format: Part 2
1027 // (e.g. backup and backup=y. backup handled here)
1028 flag_val = true;
Ethan Yonker96af84a2015-01-05 14:58:36 -06001029 } else {
Matt Mower2416a502016-04-12 19:54:46 -05001030 LOGINFO("Flag matched, but could not be processed: %s\n", ptr);
1031 break;
Ethan Yonker96af84a2015-01-05 14:58:36 -06001032 }
Matt Mower2416a502016-04-12 19:54:46 -05001033
1034 Apply_TW_Flag(tw_flag->flag, ptr, flag_val);
1035 break;
Ethan Yonker96af84a2015-01-05 14:58:36 -06001036 }
Matt Mower2416a502016-04-12 19:54:46 -05001037 }
1038 if (tw_flag->name == 0) {
Dees_Troy51127312012-09-08 13:08:49 -04001039 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001040 LOGERR("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -04001041 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001042 LOGINFO("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -04001043 }
Matt Mower2416a502016-04-12 19:54:46 -05001044 ptr = strtok_r(NULL, separator, &savep);
Dees_Troy51127312012-09-08 13:08:49 -04001045 }
Dees_Troy51a0e822012-09-05 15:24:24 -04001046}
1047
Dees_Troy5bf43922012-09-07 16:07:55 -04001048bool TWPartition::Is_File_System(string File_System) {
1049 if (File_System == "ext2" ||
Dees_Troy63c8df72012-09-10 14:02:05 -04001050 File_System == "ext3" ||
Dees_Troy5bf43922012-09-07 16:07:55 -04001051 File_System == "ext4" ||
1052 File_System == "vfat" ||
1053 File_System == "ntfs" ||
1054 File_System == "yaffs2" ||
bigbiff bigbiff3e146522012-11-14 14:32:59 -05001055 File_System == "exfat" ||
Dees_Troye5017042013-08-29 16:38:55 +00001056 File_System == "f2fs" ||
Erfan Abdib859e912019-02-05 18:18:46 +02001057 File_System == "squashfs" ||
Dees_Troy5bf43922012-09-07 16:07:55 -04001058 File_System == "auto")
1059 return true;
1060 else
1061 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001062}
1063
Dees_Troy5bf43922012-09-07 16:07:55 -04001064bool TWPartition::Is_Image(string File_System) {
Dees_Troy5fcd8f92012-10-16 12:22:05 -04001065 if (File_System == "emmc" || File_System == "mtd" || File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -04001066 return true;
1067 else
1068 return false;
1069}
1070
Dees_Troy51127312012-09-08 13:08:49 -04001071bool TWPartition::Make_Dir(string Path, bool Display_Error) {
Ethan Yonker93ac7a02016-11-07 22:05:58 -06001072 if (TWFunc::Get_D_Type_From_Stat(Path) != S_IFDIR)
1073 unlink(Path.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001074 if (!TWFunc::Path_Exists(Path)) {
Dees_Troy51127312012-09-08 13:08:49 -04001075 if (mkdir(Path.c_str(), 0777) == -1) {
1076 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001077 gui_msg(Msg(msg::kError, "create_folder_strerr=Can not create '{1}' folder ({2}).")(Path)(strerror(errno)));
Dees_Troy51127312012-09-08 13:08:49 -04001078 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001079 LOGINFO("Can not create '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -04001080 return false;
1081 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001082 LOGINFO("Created '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -04001083 return true;
1084 }
1085 }
1086 return true;
1087}
1088
Dees_Troy5bf43922012-09-07 16:07:55 -04001089void TWPartition::Setup_File_System(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001090 Can_Be_Mounted = true;
1091 Can_Be_Wiped = true;
1092
Dees_Troy5bf43922012-09-07 16:07:55 -04001093 // Make the mount point folder if it doesn't exist
Dees_Troy51127312012-09-08 13:08:49 -04001094 Make_Dir(Mount_Point, Display_Error);
bigbiffce8f83c2015-12-12 18:30:21 -05001095 Backup_Method = BM_FILES;
Dees_Troy5bf43922012-09-07 16:07:55 -04001096}
1097
Ethan Yonker1b190162016-12-05 15:25:19 -06001098void TWPartition::Setup_Image() {
Dees_Troy5bf43922012-09-07 16:07:55 -04001099 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
1100 Backup_Name = Display_Name;
Gary Peck82599a82012-11-21 16:23:12 -08001101 if (Current_File_System == "emmc")
bigbiffce8f83c2015-12-12 18:30:21 -05001102 Backup_Method = BM_DD;
Gary Peck82599a82012-11-21 16:23:12 -08001103 else if (Current_File_System == "mtd" || Current_File_System == "bml")
bigbiffce8f83c2015-12-12 18:30:21 -05001104 Backup_Method = BM_FLASH_UTILS;
Dees_Troy5bf43922012-09-07 16:07:55 -04001105 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001106 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 -04001107}
1108
Dees_Troye58d5262012-09-21 12:27:57 -04001109void TWPartition::Setup_AndSec(void) {
Dees_Troya13d74f2013-03-24 08:54:55 -05001110 Backup_Display_Name = "Android Secure";
Dees_Troye58d5262012-09-21 12:27:57 -04001111 Backup_Name = "and-sec";
Dees_Troya13d74f2013-03-24 08:54:55 -05001112 Can_Be_Backed_Up = true;
Dees_Troye58d5262012-09-21 12:27:57 -04001113 Has_Android_Secure = true;
1114 Symlink_Path = Mount_Point + "/.android_secure";
1115 Symlink_Mount_Point = "/and-sec";
1116 Backup_Path = Symlink_Mount_Point;
1117 Make_Dir("/and-sec", true);
1118 Recreate_AndSec_Folder();
Kjell Braden3126a112016-06-19 16:58:15 +00001119 Mount_Storage_Retry(true);
Dees_Troye58d5262012-09-21 12:27:57 -04001120}
1121
that9e0593e2014-10-08 00:01:24 +02001122void TWPartition::Setup_Data_Media() {
Ethan Yonker6277c792014-09-15 14:54:30 -05001123 LOGINFO("Setting up '%s' as data/media emulated storage.\n", Mount_Point.c_str());
Ethan Yonker66a19492015-12-10 10:19:45 -06001124 if (Storage_Name.empty() || Storage_Name == "Data")
1125 Storage_Name = "Internal Storage";
Ethan Yonker6277c792014-09-15 14:54:30 -05001126 Has_Data_Media = true;
1127 Is_Storage = true;
Ethan Yonker66a19492015-12-10 10:19:45 -06001128 Storage_Path = Mount_Point + "/media";
Ethan Yonker6277c792014-09-15 14:54:30 -05001129 Symlink_Path = Storage_Path;
Ethan Yonker66a19492015-12-10 10:19:45 -06001130 if (Mount_Point == "/data") {
1131 Is_Settings_Storage = true;
1132 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
1133 Make_Dir("/emmc", false);
1134 Symlink_Mount_Point = "/emmc";
1135 } else {
1136 Make_Dir("/sdcard", false);
1137 Symlink_Mount_Point = "/sdcard";
1138 }
1139 if (Mount(false) && TWFunc::Path_Exists(Mount_Point + "/media/0")) {
1140 Storage_Path = Mount_Point + "/media/0";
1141 Symlink_Path = Storage_Path;
1142 DataManager::SetValue(TW_INTERNAL_PATH, Mount_Point + "/media/0");
1143 UnMount(true);
1144 }
1145 DataManager::SetValue("tw_has_internal", 1);
1146 DataManager::SetValue("tw_has_data_media", 1);
Ethan Yonker3fdcda42016-11-30 12:29:37 -06001147 backup_exclusions.add_absolute_dir("/data/data/com.google.android.music/files");
Darth90864aa52020-01-14 20:58:16 +00001148 backup_exclusions.add_absolute_dir("/data/per_boot"); // DJ9,14Jan2020 - exclude this dir to prevent "error 255" on AOSP ROMs that create and lock it
Ethan Yonker6355b562017-05-01 09:42:17 -05001149 wipe_exclusions.add_absolute_dir(Mount_Point + "/misc/vold"); // adopted storage keys
Ethan Yonker79f88bd2016-12-09 14:52:12 -06001150 ExcludeAll(Mount_Point + "/.layout_version");
1151 ExcludeAll(Mount_Point + "/system/storage.xml");
Ethan Yonker6277c792014-09-15 14:54:30 -05001152 } else {
Ethan Yonker66a19492015-12-10 10:19:45 -06001153 if (Mount(true) && TWFunc::Path_Exists(Mount_Point + "/media/0")) {
1154 Storage_Path = Mount_Point + "/media/0";
1155 Symlink_Path = Storage_Path;
1156 UnMount(true);
1157 }
Ethan Yonker6277c792014-09-15 14:54:30 -05001158 }
Ethan Yonker79f88bd2016-12-09 14:52:12 -06001159 ExcludeAll(Mount_Point + "/media");
Ethan Yonker6277c792014-09-15 14:54:30 -05001160}
1161
Dees_Troy5bf43922012-09-07 16:07:55 -04001162void TWPartition::Find_Real_Block_Device(string& Block, bool Display_Error) {
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06001163 char device[PATH_MAX], realDevice[PATH_MAX];
Dees_Troy5bf43922012-09-07 16:07:55 -04001164
1165 strcpy(device, Block.c_str());
1166 memset(realDevice, 0, sizeof(realDevice));
1167 while (readlink(device, realDevice, sizeof(realDevice)) > 0)
1168 {
1169 strcpy(device, realDevice);
1170 memset(realDevice, 0, sizeof(realDevice));
1171 }
1172
1173 if (device[0] != '/') {
1174 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001175 LOGERR("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001176 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001177 LOGINFO("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001178 return;
Dees_Troy51a0e822012-09-05 15:24:24 -04001179 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -04001180 Block = device;
1181 return;
1182 }
1183}
1184
Kjell Braden3126a112016-06-19 16:58:15 +00001185bool TWPartition::Mount_Storage_Retry(bool Display_Error) {
Dees_Troy8e337f32012-10-13 22:07:49 -04001186 // On some devices, storage doesn't want to mount right away, retry and sleep
Kjell Braden3126a112016-06-19 16:58:15 +00001187 if (!Mount(Display_Error)) {
Dees_Troy8e337f32012-10-13 22:07:49 -04001188 int retry_count = 5;
1189 while (retry_count > 0 && !Mount(false)) {
1190 usleep(500000);
1191 retry_count--;
1192 }
Kjell Braden3126a112016-06-19 16:58:15 +00001193 return Mount(Display_Error);
Dees_Troy8e337f32012-10-13 22:07:49 -04001194 }
Kjell Braden3126a112016-06-19 16:58:15 +00001195 return true;
Dees_Troy8e337f32012-10-13 22:07:49 -04001196}
1197
Dees_Troy38bd7602012-09-14 13:33:53 -04001198bool TWPartition::Find_MTD_Block_Device(string MTD_Name) {
1199 FILE *fp = NULL;
1200 char line[255];
1201
1202 fp = fopen("/proc/mtd", "rt");
1203 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001204 LOGERR("Device does not support /proc/mtd\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001205 return false;
1206 }
1207
1208 while (fgets(line, sizeof(line), fp) != NULL)
1209 {
1210 char device[32], label[32];
1211 unsigned long size = 0;
Dees_Troy38bd7602012-09-14 13:33:53 -04001212 int deviceId;
1213
1214 sscanf(line, "%s %lx %*s %*c%s", device, &size, label);
1215
1216 // Skip header and blank lines
1217 if ((strcmp(device, "dev:") == 0) || (strlen(line) < 8))
1218 continue;
1219
1220 // Strip off the trailing " from the label
1221 label[strlen(label)-1] = '\0';
1222
1223 if (strcmp(label, MTD_Name.c_str()) == 0) {
1224 // We found our device
1225 // Strip off the trailing : from the device
1226 device[strlen(device)-1] = '\0';
1227 if (sscanf(device,"mtd%d", &deviceId) == 1) {
1228 sprintf(device, "/dev/block/mtdblock%d", deviceId);
1229 Primary_Block_Device = device;
Dees_Troy76543db2013-06-19 16:24:30 +00001230 fclose(fp);
1231 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -04001232 }
1233 }
1234 }
1235 fclose(fp);
1236
1237 return false;
1238}
1239
Dees_Troy51127312012-09-08 13:08:49 -04001240bool TWPartition::Get_Size_Via_statfs(bool Display_Error) {
1241 struct statfs st;
1242 string Local_Path = Mount_Point + "/.";
1243
1244 if (!Mount(Display_Error))
1245 return false;
1246
1247 if (statfs(Local_Path.c_str(), &st) != 0) {
1248 if (!Removable) {
1249 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001250 LOGERR("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -04001251 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001252 LOGINFO("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -04001253 }
1254 return false;
1255 }
1256 Size = (st.f_blocks * st.f_bsize);
1257 Used = ((st.f_blocks - st.f_bfree) * st.f_bsize);
1258 Free = (st.f_bfree * st.f_bsize);
1259 Backup_Size = Used;
1260 return true;
1261}
1262
1263bool TWPartition::Get_Size_Via_df(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001264 FILE* fp;
1265 char command[255], line[512];
1266 int include_block = 1;
1267 unsigned int min_len;
1268
1269 if (!Mount(Display_Error))
1270 return false;
1271
Dees_Troy38bd7602012-09-14 13:33:53 -04001272 min_len = Actual_Block_Device.size() + 2;
Dees_Troy51127312012-09-08 13:08:49 -04001273 sprintf(command, "df %s > /tmp/dfoutput.txt", Mount_Point.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001274 TWFunc::Exec_Cmd(command);
Dees_Troy51127312012-09-08 13:08:49 -04001275 fp = fopen("/tmp/dfoutput.txt", "rt");
1276 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001277 LOGINFO("Unable to open /tmp/dfoutput.txt.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001278 return false;
Dees_Troy51127312012-09-08 13:08:49 -04001279 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001280
1281 while (fgets(line, sizeof(line), fp) != NULL)
1282 {
1283 unsigned long blocks, used, available;
1284 char device[64];
1285 char tmpString[64];
1286
1287 if (strncmp(line, "Filesystem", 10) == 0)
1288 continue;
1289 if (strlen(line) < min_len) {
1290 include_block = 0;
1291 continue;
1292 }
1293 if (include_block) {
1294 sscanf(line, "%s %lu %lu %lu", device, &blocks, &used, &available);
1295 } else {
1296 // The device block string is so long that the df information is on the next line
1297 int space_count = 0;
Dees_Troye58d5262012-09-21 12:27:57 -04001298 sprintf(tmpString, "/dev/block/%s", Actual_Block_Device.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001299 while (tmpString[space_count] == 32)
1300 space_count++;
1301 sscanf(line + space_count, "%lu %lu %lu", &blocks, &used, &available);
1302 }
1303
1304 // Adjust block size to byte size
1305 Size = blocks * 1024ULL;
1306 Used = used * 1024ULL;
1307 Free = available * 1024ULL;
1308 Backup_Size = Used;
1309 }
1310 fclose(fp);
1311 return true;
1312}
1313
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001314unsigned long long TWPartition::IOCTL_Get_Block_Size() {
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001315 Find_Actual_Block_Device();
Ethan Yonker483e9f42016-01-11 22:21:18 -06001316
1317 return TWFunc::IOCTL_Get_Block_Size(Actual_Block_Device.c_str());
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001318}
1319
Dees_Troy5bf43922012-09-07 16:07:55 -04001320bool TWPartition::Find_Partition_Size(void) {
1321 FILE* fp;
1322 char line[512];
1323 string tmpdevice;
1324
igoriok87e3d932013-01-31 21:03:53 +02001325 fp = fopen("/proc/dumchar_info", "rt");
1326 if (fp != NULL) {
1327 while (fgets(line, sizeof(line), fp) != NULL)
1328 {
1329 char label[32], device[32];
1330 unsigned long size = 0;
1331
thatd43bf2d2014-09-21 23:13:02 +02001332 sscanf(line, "%s %lx %*x %*u %s", label, &size, device);
igoriok87e3d932013-01-31 21:03:53 +02001333
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05001334 // Skip header, annotation and blank lines
igoriok87e3d932013-01-31 21:03:53 +02001335 if ((strncmp(device, "/dev/", 5) != 0) || (strlen(line) < 8))
1336 continue;
1337
1338 tmpdevice = "/dev/";
1339 tmpdevice += label;
1340 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
1341 Size = size;
1342 fclose(fp);
1343 return true;
1344 }
1345 }
1346 }
1347
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001348 unsigned long long ioctl_size = IOCTL_Get_Block_Size();
1349 if (ioctl_size) {
1350 Size = ioctl_size;
1351 return true;
1352 }
1353
Dees_Troy5bf43922012-09-07 16:07:55 -04001354 // In this case, we'll first get the partitions we care about (with labels)
1355 fp = fopen("/proc/partitions", "rt");
1356 if (fp == NULL)
1357 return false;
1358
1359 while (fgets(line, sizeof(line), fp) != NULL)
1360 {
1361 unsigned long major, minor, blocks;
1362 char device[512];
Dees_Troy5bf43922012-09-07 16:07:55 -04001363
Dees_Troy63c8df72012-09-10 14:02:05 -04001364 if (strlen(line) < 7 || line[0] == 'm') continue;
Dees_Troy5bf43922012-09-07 16:07:55 -04001365 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
1366
1367 tmpdevice = "/dev/block/";
1368 tmpdevice += device;
Dees_Troy38bd7602012-09-14 13:33:53 -04001369 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001370 // Adjust block size to byte size
1371 Size = blocks * 1024ULL;
1372 fclose(fp);
1373 return true;
1374 }
1375 }
1376 fclose(fp);
1377 return false;
1378}
1379
Dees_Troy5bf43922012-09-07 16:07:55 -04001380bool TWPartition::Is_Mounted(void) {
1381 if (!Can_Be_Mounted)
1382 return false;
1383
1384 struct stat st1, st2;
1385 string test_path;
1386
1387 // Check to see if the mount point directory exists
1388 test_path = Mount_Point + "/.";
1389 if (stat(test_path.c_str(), &st1) != 0) return false;
1390
1391 // Check to see if the directory above the mount point exists
1392 test_path = Mount_Point + "/../.";
1393 if (stat(test_path.c_str(), &st2) != 0) return false;
1394
1395 // Compare the device IDs -- if they match then we're (probably) using tmpfs instead of an actual device
1396 int ret = (st1.st_dev != st2.st_dev) ? true : false;
1397
1398 return ret;
1399}
1400
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05001401bool TWPartition::Is_File_System_Writable(void) {
1402 if (!Is_File_System(Current_File_System) || !Is_Mounted())
1403 return false;
1404
1405 string test_path = Mount_Point + "/.";
1406 return (access(test_path.c_str(), W_OK) == 0);
1407}
1408
Dees_Troy5bf43922012-09-07 16:07:55 -04001409bool TWPartition::Mount(bool Display_Error) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001410 int exfat_mounted = 0;
Martin Dünkelmann6d843782018-12-27 23:40:28 +01001411 unsigned int flags = Mount_Flags;
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001412
Dees_Troy5bf43922012-09-07 16:07:55 -04001413 if (Is_Mounted()) {
1414 return true;
1415 } else if (!Can_Be_Mounted) {
1416 return false;
1417 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001418
1419 Find_Actual_Block_Device();
1420
1421 // Check the current file system before mounting
1422 Check_FS_Type();
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001423 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/exfat-fuse")) {
Dees_Troye34c1332013-02-06 19:13:00 +00001424 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 +00001425 LOGINFO("cmd: %s\n", cmd.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001426 string result;
1427 if (TWFunc::Exec_Cmd(cmd, result) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001428 LOGINFO("exfat-fuse failed to mount with result '%s', trying vfat\n", result.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001429 Current_File_System = "vfat";
1430 } else {
1431#ifdef TW_NO_EXFAT_FUSE
1432 UnMount(false);
1433 // We'll let the kernel handle it but using exfat-fuse to detect if the file system is actually exfat
1434 // Some kernels let us mount vfat as exfat which doesn't work out too well
1435#else
1436 exfat_mounted = 1;
1437#endif
1438 }
1439 }
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001440
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001441 if (Current_File_System == "ntfs" && (TWFunc::Path_Exists("/sbin/ntfs-3g") || TWFunc::Path_Exists("/sbin/mount.ntfs"))) {
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001442 string cmd;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001443 string Ntfsmount_Binary = "";
1444
1445 if (TWFunc::Path_Exists("/sbin/ntfs-3g"))
1446 Ntfsmount_Binary = "ntfs-3g";
1447 else if (TWFunc::Path_Exists("/sbin/mount.ntfs"))
1448 Ntfsmount_Binary = "mount.ntfs";
1449
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001450 if (Mount_Read_Only)
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001451 cmd = "/sbin/" + Ntfsmount_Binary + " -o ro " + Actual_Block_Device + " " + Mount_Point;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001452 else
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001453 cmd = "/sbin/" + Ntfsmount_Binary + " " + Actual_Block_Device + " " + Mount_Point;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001454 LOGINFO("cmd: '%s'\n", cmd.c_str());
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001455
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001456 if (TWFunc::Exec_Cmd(cmd) == 0) {
1457 return true;
1458 } else {
1459 LOGINFO("ntfs-3g failed to mount, trying regular mount method.\n");
1460 }
1461 }
1462
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001463 if (Mount_Read_Only)
1464 flags |= MS_RDONLY;
1465
Dees_Troy22042032012-12-18 21:23:08 +00001466 if (Fstab_File_System == "yaffs2") {
1467 // mount an MTD partition as a YAFFS2 filesystem.
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001468 flags = MS_NOATIME | MS_NODEV | MS_NODIRATIME;
1469 if (Mount_Read_Only)
1470 flags |= MS_RDONLY;
Dees_Troy76543db2013-06-19 16:24:30 +00001471 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags, NULL) < 0) {
1472 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags | MS_RDONLY, NULL) < 0) {
1473 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001474 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy76543db2013-06-19 16:24:30 +00001475 else
1476 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
1477 return false;
1478 } else {
1479 LOGINFO("Mounted '%s' (MTD) as RO\n", Mount_Point.c_str());
1480 return true;
1481 }
1482 } else {
1483 struct stat st;
1484 string test_path = Mount_Point;
1485 if (stat(test_path.c_str(), &st) < 0) {
1486 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001487 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy76543db2013-06-19 16:24:30 +00001488 else
1489 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
1490 return false;
1491 }
1492 mode_t new_mode = st.st_mode | S_IXUSR | S_IXGRP | S_IXOTH;
1493 if (new_mode != st.st_mode) {
1494 LOGINFO("Fixing execute permissions for %s\n", Mount_Point.c_str());
1495 if (chmod(Mount_Point.c_str(), new_mode) < 0) {
1496 if (Display_Error)
1497 LOGERR("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
1498 else
1499 LOGINFO("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
1500 return false;
1501 }
1502 }
Dees_Troy22042032012-12-18 21:23:08 +00001503 return true;
Dees_Troy76543db2013-06-19 16:24:30 +00001504 }
thatc7572eb2015-03-31 18:55:30 +02001505 }
1506
1507 string mount_fs = Current_File_System;
1508 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sys/module/texfat"))
1509 mount_fs = "texfat";
1510
1511 if (!exfat_mounted &&
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001512 mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), mount_fs.c_str(), flags, Mount_Options.c_str()) != 0 &&
1513 mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), mount_fs.c_str(), flags, NULL) != 0) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001514#ifdef TW_NO_EXFAT_FUSE
1515 if (Current_File_System == "exfat") {
Dees_Troy2673cec2013-04-02 20:22:16 +00001516 LOGINFO("Mounting exfat failed, trying vfat...\n");
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001517 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), "vfat", 0, NULL) != 0) {
Dees_Troy85f44ed2013-01-09 18:42:36 +00001518 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001519 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy85f44ed2013-01-09 18:42:36 +00001520 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001521 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001522 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 +00001523 return false;
Dees_Troy85f44ed2013-01-09 18:42:36 +00001524 }
Dees_Troyb05ddee2013-01-28 20:24:50 +00001525 } else {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001526#endif
bigbiff bigbiff26774a02014-03-29 18:22:00 -04001527 if (!Removable && 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_Troy3f5c4e82013-02-01 15:16:59 +00001529 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001530 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
1531 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 +00001532 return false;
1533#ifdef TW_NO_EXFAT_FUSE
Dees_Troy85f44ed2013-01-09 18:42:36 +00001534 }
1535#endif
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001536 }
Ethan Yonker253368a2014-11-25 15:00:52 -06001537
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001538 if (Removable)
1539 Update_Size(Display_Error);
1540
xiaolu9416f4f2015-06-04 08:22:23 +08001541 if (!Symlink_Mount_Point.empty() && TWFunc::Path_Exists(Symlink_Path)) {
codelover2a3d4ce2015-03-14 20:26:49 +08001542 string Command = "mount -o bind '" + Symlink_Path + "' '" + Symlink_Mount_Point + "'";
Vojtech Bocek05534202013-09-11 08:11:56 +02001543 TWFunc::Exec_Cmd(Command);
Dees_Troy5bf43922012-09-07 16:07:55 -04001544 }
Chaosmasterda974802020-01-26 21:09:28 +01001545
1546 if (Mount_Point == "/system_root") {
1547 unlink("/system");
1548 mkdir("/system", 0755);
1549 mount("/system_root/system", "/system", "auto", MS_BIND, NULL);
1550 }
1551
Dees_Troy5bf43922012-09-07 16:07:55 -04001552 return true;
1553}
1554
1555bool TWPartition::UnMount(bool Display_Error) {
Chaosmasterda974802020-01-26 21:09:28 +01001556 if (Mount_Point == "/system_root") {
1557 if (umount("/system") == -1)
1558 umount2("/system", MNT_DETACH);
1559 rmdir("/system");
1560 symlink("/system_root/system", "/system");
1561 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001562 if (Is_Mounted()) {
1563 int never_unmount_system;
1564
1565 DataManager::GetValue(TW_DONT_UNMOUNT_SYSTEM, never_unmount_system);
Captain Throwback9d6feb52018-07-27 10:05:24 -04001566 if (never_unmount_system == 1 && Mount_Point == PartitionManager.Get_Android_Root_Path())
Dees_Troy5bf43922012-09-07 16:07:55 -04001567 return true; // Never unmount system if you're not supposed to unmount it
1568
Matt Mowera8e6d832017-02-14 20:20:59 -06001569 if (Is_Storage && MTP_Storage_ID > 0)
Ethan Yonker726a0202014-12-16 20:01:38 -06001570 PartitionManager.Remove_MTP_Storage(MTP_Storage_ID);
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001571
Dees_Troy38bd7602012-09-14 13:33:53 -04001572 if (!Symlink_Mount_Point.empty())
1573 umount(Symlink_Mount_Point.c_str());
1574
Dees_Troyb05ddee2013-01-28 20:24:50 +00001575 umount(Mount_Point.c_str());
1576 if (Is_Mounted()) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001577 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001578 gui_msg(Msg(msg::kError, "fail_unmount=Failed to unmount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy5bf43922012-09-07 16:07:55 -04001579 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001580 LOGINFO("Unable to unmount '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001581 return false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001582 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -04001583 return true;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001584 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001585 } else {
1586 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001587 }
1588}
1589
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05001590bool TWPartition::ReMount(bool Display_Error) {
1591 if (UnMount(Display_Error))
1592 return Mount(Display_Error);
1593 return false;
1594}
1595
1596bool TWPartition::ReMount_RW(bool Display_Error) {
1597 // No need to remount if already mounted rw
1598 if (Is_File_System_Writable())
1599 return true;
1600
1601 bool ro = Mount_Read_Only;
1602 int flags = Mount_Flags;
1603
1604 Mount_Read_Only = false;
1605 Mount_Flags &= ~MS_RDONLY;
1606
1607 bool ret = ReMount(Display_Error);
1608
1609 Mount_Read_Only = ro;
1610 Mount_Flags = flags;
1611
1612 return ret;
1613}
1614
Gary Peck43acadf2012-11-21 21:19:01 -08001615bool TWPartition::Wipe(string New_File_System) {
Ethan Yonker726a0202014-12-16 20:01:38 -06001616 bool wiped = false, update_crypt = false, recreate_media = true;
Dees_Troy16c2b312013-01-15 16:51:18 +00001617 int check;
1618 string Layout_Filename = Mount_Point + "/.layout_version";
1619
Dees_Troy38bd7602012-09-14 13:33:53 -04001620 if (!Can_Be_Wiped) {
Matt Mower3c366972015-12-25 19:28:31 -06001621 gui_msg(Msg(msg::kError, "cannot_wipe=Partition {1} cannot be wiped.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04001622 return false;
1623 }
1624
Dees_Troyc51f1f92012-09-20 15:32:13 -04001625 if (Mount_Point == "/cache")
Dees_Troy2673cec2013-04-02 20:22:16 +00001626 Log_Offset = 0;
Dees_Troyc51f1f92012-09-20 15:32:13 -04001627
Dees_Troy16c2b312013-01-15 16:51:18 +00001628 if (Retain_Layout_Version && Mount(false) && TWFunc::Path_Exists(Layout_Filename))
1629 TWFunc::copy_file(Layout_Filename, "/.layout_version", 0600);
1630 else
1631 unlink("/.layout_version");
Dees_Troy38bd7602012-09-14 13:33:53 -04001632
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001633 if (Has_Data_Media && Current_File_System == New_File_System) {
Dees_Troy16c2b312013-01-15 16:51:18 +00001634 wiped = Wipe_Data_Without_Wiping_Media();
Ethan Yonker5eac2222014-06-11 12:22:55 -05001635 recreate_media = false;
Dees_Troy16c2b312013-01-15 16:51:18 +00001636 } else {
Dees_Troy16c2b312013-01-15 16:51:18 +00001637 DataManager::GetValue(TW_RM_RF_VAR, check);
1638
Hashcodedabfd492013-08-29 22:45:30 -07001639 if (check || Use_Rm_Rf)
Dees_Troy16c2b312013-01-15 16:51:18 +00001640 wiped = Wipe_RMRF();
1641 else if (New_File_System == "ext4")
1642 wiped = Wipe_EXT4();
1643 else if (New_File_System == "ext2" || New_File_System == "ext3")
dianlujitao4879b372018-12-03 18:45:47 +08001644 wiped = Wipe_EXTFS(New_File_System);
Dees_Troy16c2b312013-01-15 16:51:18 +00001645 else if (New_File_System == "vfat")
1646 wiped = Wipe_FAT();
1647 else if (New_File_System == "exfat")
1648 wiped = Wipe_EXFAT();
1649 else if (New_File_System == "yaffs2")
1650 wiped = Wipe_MTD();
Dees_Troye5017042013-08-29 16:38:55 +00001651 else if (New_File_System == "f2fs")
1652 wiped = Wipe_F2FS();
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001653 else if (New_File_System == "ntfs")
1654 wiped = Wipe_NTFS();
Dees_Troy16c2b312013-01-15 16:51:18 +00001655 else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001656 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 +00001657 unlink("/.layout_version");
1658 return false;
1659 }
1660 update_crypt = wiped;
Gary Pecke8bc5d72012-12-21 06:45:25 -08001661 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001662
Gary Pecke8bc5d72012-12-21 06:45:25 -08001663 if (wiped) {
Dees_Troy1c1ac442013-01-17 21:42:14 +00001664 if (Mount_Point == "/cache")
1665 DataManager::Output_Version();
1666
Dees_Troy16c2b312013-01-15 16:51:18 +00001667 if (TWFunc::Path_Exists("/.layout_version") && Mount(false))
1668 TWFunc::copy_file("/.layout_version", Layout_Filename, 0600);
1669
1670 if (update_crypt) {
1671 Setup_File_System(false);
1672 if (Is_Encrypted && !Is_Decrypted) {
1673 // just wiped an encrypted partition back to its unencrypted state
1674 Is_Encrypted = false;
1675 Is_Decrypted = false;
1676 Decrypted_Block_Device = "";
1677 if (Mount_Point == "/data") {
1678 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1679 DataManager::SetValue(TW_IS_DECRYPTED, 0);
1680 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001681 }
1682 }
Ethan Yonker5eac2222014-06-11 12:22:55 -05001683
Ethan Yonker66a19492015-12-10 10:19:45 -06001684 if (Has_Data_Media && recreate_media) {
Ethan Yonker5eac2222014-06-11 12:22:55 -05001685 Recreate_Media_Folder();
1686 }
Matt Mower209c9632016-01-20 12:08:35 -06001687 if (Is_Storage && Mount(false))
1688 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Gary Pecke8bc5d72012-12-21 06:45:25 -08001689 }
Matt Mower209c9632016-01-20 12:08:35 -06001690
Gary Pecke8bc5d72012-12-21 06:45:25 -08001691 return wiped;
Dees_Troy51a0e822012-09-05 15:24:24 -04001692}
1693
Gary Peck43acadf2012-11-21 21:19:01 -08001694bool TWPartition::Wipe() {
Gary Peck82599a82012-11-21 16:23:12 -08001695 if (Is_File_System(Current_File_System))
1696 return Wipe(Current_File_System);
1697 else
1698 return Wipe(Fstab_File_System);
Gary Peck43acadf2012-11-21 21:19:01 -08001699}
1700
Dees_Troye58d5262012-09-21 12:27:57 -04001701bool TWPartition::Wipe_AndSec(void) {
1702 if (!Has_Android_Secure)
1703 return false;
1704
Dees_Troye58d5262012-09-21 12:27:57 -04001705 if (!Mount(true))
1706 return false;
1707
Ethan Yonker74db1572015-10-28 12:44:49 -05001708 gui_msg(Msg("wiping=Wiping {1}")(Backup_Display_Name));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001709 TWFunc::removeDir(Mount_Point + "/.android_secure/", true);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001710 return true;
Dees_Troye58d5262012-09-21 12:27:57 -04001711}
1712
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001713bool TWPartition::Can_Repair() {
Ethan Yonkera2719152015-05-28 09:44:41 -05001714 if (Mount_Read_Only)
1715 return false;
Matt Mower18794c82015-11-11 16:22:45 -06001716 if (Current_File_System == "vfat" && TWFunc::Path_Exists("/sbin/fsck.fat"))
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001717 return true;
1718 else if ((Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") && TWFunc::Path_Exists("/sbin/e2fsck"))
1719 return true;
1720 else if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/fsck.exfat"))
1721 return true;
1722 else if (Current_File_System == "f2fs" && TWFunc::Path_Exists("/sbin/fsck.f2fs"))
1723 return true;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001724 else if (Current_File_System == "ntfs" && (TWFunc::Path_Exists("/sbin/ntfsfix") || TWFunc::Path_Exists("/sbin/fsck.ntfs")))
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001725 return true;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001726 return false;
1727}
1728
1729bool TWPartition::Repair() {
1730 string command;
1731
1732 if (Current_File_System == "vfat") {
Matt Mower18794c82015-11-11 16:22:45 -06001733 if (!TWFunc::Path_Exists("/sbin/fsck.fat")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001734 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.fat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001735 return false;
1736 }
1737 if (!UnMount(true))
1738 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001739 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.fat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001740 Find_Actual_Block_Device();
Matt Mower18794c82015-11-11 16:22:45 -06001741 command = "/sbin/fsck.fat -y " + Actual_Block_Device;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001742 LOGINFO("Repair command: %s\n", command.c_str());
1743 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001744 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001745 return true;
1746 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001747 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001748 return false;
1749 }
1750 }
1751 if (Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") {
1752 if (!TWFunc::Path_Exists("/sbin/e2fsck")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001753 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("e2fsck"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001754 return false;
1755 }
1756 if (!UnMount(true))
1757 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001758 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("e2fsck"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001759 Find_Actual_Block_Device();
Ethan Yonkera2719152015-05-28 09:44:41 -0500