blob: 7f4189a638dd077739bf270f48678fec8c69440b [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()) {
mauronofriofc79aa62019-11-23 23:13:04 +0100688 char wrappedvalue[PROPERTY_VALUE_MAX];
689 property_get("fbe.data.wrappedkey", wrappedvalue, "");
690 std::string wrappedkeyvalue(wrappedvalue);
691 if (wrappedkeyvalue == "true") {
692 LOGERR("Unable to decrypt FBE device\n");
693 } else {
694 LOGINFO("Trying wrapped key.\n");
695 property_set("fbe.data.wrappedkey", "true");
mauronofrio9bf73352019-11-23 22:27:34 +0100696 if (!Decrypt_FBE_DE()) {
bigbiffd58ba182020-03-23 10:02:29 -0400697 LOGERR("Unable to decrypt FBE device\n");
mauronofrio9bf73352019-11-23 22:27:34 +0100698 }
mauronofriofc79aa62019-11-23 23:13:04 +0100699 }
mauronofrio9bf73352019-11-23 22:27:34 +0100700 }
Matt Mower72c87ce2016-04-26 14:34:56 -0500701 }
702 if (datamedia && (!Is_Encrypted || (Is_Encrypted && Is_Decrypted))) {
703 Setup_Data_Media();
704 Recreate_Media_Folder();
705 }
706#else
707 if (datamedia) {
708 Setup_Data_Media();
709 Recreate_Media_Folder();
710 }
711#endif
712}
713
Ethan Yonker93382822018-11-01 15:25:31 -0500714bool TWPartition::Decrypt_FBE_DE() {
715if (TWFunc::Path_Exists("/data/unencrypted/key/version")) {
716 LOGINFO("File Based Encryption is present\n");
717#ifdef TW_INCLUDE_FBE
718 ExcludeAll(Mount_Point + "/convert_fbe");
719 ExcludeAll(Mount_Point + "/unencrypted");
720 //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
721 ExcludeAll(Mount_Point + "/misc/vold/user_keys");
722 //ExcludeAll(Mount_Point + "/system_ce");
723 //ExcludeAll(Mount_Point + "/system_de");
724 //ExcludeAll(Mount_Point + "/misc_ce");
725 //ExcludeAll(Mount_Point + "/misc_de");
726 ExcludeAll(Mount_Point + "/system/gatekeeper.password.key");
727 ExcludeAll(Mount_Point + "/system/gatekeeper.pattern.key");
728 ExcludeAll(Mount_Point + "/system/locksettings.db");
729 //ExcludeAll(Mount_Point + "/system/locksettings.db-shm"); // don't seem to need this one, but the other 2 are needed
730 ExcludeAll(Mount_Point + "/system/locksettings.db-wal");
731 //ExcludeAll(Mount_Point + "/user_de");
732 //ExcludeAll(Mount_Point + "/misc/profiles/cur/0"); // might be important later
733 ExcludeAll(Mount_Point + "/misc/gatekeeper");
734 ExcludeAll(Mount_Point + "/misc/keystore");
735 ExcludeAll(Mount_Point + "/drm/kek.dat");
736 ExcludeAll(Mount_Point + "/system_de/0/spblob"); // contains data needed to decrypt pixel 2
737 int retry_count = 3;
738 while (!Decrypt_DE() && --retry_count)
739 usleep(2000);
740 if (retry_count > 0) {
741 property_set("ro.crypto.state", "encrypted");
742 Is_Encrypted = true;
743 Is_Decrypted = false;
744 Is_FBE = true;
745 DataManager::SetValue(TW_IS_FBE, 1);
746 DataManager::SetValue(TW_IS_ENCRYPTED, 1);
747 string filename;
748 int pwd_type = Get_Password_Type(0, filename);
749 if (pwd_type < 0) {
750 LOGERR("This TWRP does not have synthetic password decrypt support\n");
751 pwd_type = 0; // default password
752 }
753 DataManager::SetValue(TW_CRYPTO_PWTYPE, pwd_type);
754 DataManager::SetValue(TW_CRYPTO_PASSWORD, "");
755 DataManager::SetValue("tw_crypto_display", "");
756 return true;
757 }
758#else
759 LOGERR("FBE found but FBE support not present in TWRP\n");
760#endif
761 }
762 return false;
763}
764
Matt Mower72c87ce2016-04-26 14:34:56 -0500765void TWPartition::Setup_Cache_Partition(bool Display_Error __unused) {
766 if (Mount_Point != "/cache")
767 return;
768
769 if (!Mount(true))
770 return;
771
772 if (!TWFunc::Path_Exists("/cache/recovery/.")) {
773 LOGINFO("Recreating /cache/recovery folder\n");
774 if (mkdir("/cache/recovery", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0)
775 LOGERR("Could not create /cache/recovery\n");
776 }
777}
778
Matt Mower4ab42b12016-04-21 13:52:18 -0500779void TWPartition::Process_FS_Flags(const char *str) {
780 char *options = strdup(str);
781 char *ptr, *savep;
Hashcode62bd9e02013-11-19 21:59:42 -0800782
Matt Mower4ab42b12016-04-21 13:52:18 -0500783 Mount_Options = "";
Hashcode62bd9e02013-11-19 21:59:42 -0800784
Matt Mower4ab42b12016-04-21 13:52:18 -0500785 // Avoid issues with potentially nested strtok by using strtok_r
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600786 for (ptr = strtok_r(options, ",", &savep); ptr; ptr = strtok_r(NULL, ",", &savep)) {
787 char *equals = strstr(ptr, "=");
788 size_t name_len;
Matt Mower4ab42b12016-04-21 13:52:18 -0500789
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600790 if (!equals)
791 name_len = strlen(ptr);
792 else
793 name_len = equals - ptr;
794
795 // There are some flags that we want to ignore in TWRP
796 bool found_match = false;
797 for (const char** ignored_mount_item = ignored_mount_items; *ignored_mount_item; ignored_mount_item++) {
798 if (strncmp(ptr, *ignored_mount_item, name_len) == 0) {
799 found_match = true;
Hashcode62bd9e02013-11-19 21:59:42 -0800800 break;
801 }
802 }
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600803 if (found_match)
804 continue;
Hashcode62bd9e02013-11-19 21:59:42 -0800805
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600806 // mount_flags are never postfixed by '='
807 if (!equals) {
808 const struct flag_list* mount_flag = mount_flags;
809 for (; mount_flag->name; mount_flag++) {
810 if (strcmp(ptr, mount_flag->name) == 0) {
811 if (mount_flag->flag == MS_RDONLY)
812 Mount_Read_Only = true;
813 else
814 Mount_Flags |= (unsigned)mount_flag->flag;
815 found_match = true;
816 break;
817 }
818 }
819 if (found_match)
820 continue;
Matt Mower4ab42b12016-04-21 13:52:18 -0500821 }
822
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600823 // If we aren't ignoring this flag and it's not a mount flag, then it must be a mount option
824 if (!Mount_Options.empty())
825 Mount_Options += ",";
826 Mount_Options += ptr;
Matt Mower4ab42b12016-04-21 13:52:18 -0500827 }
828 free(options);
Hashcode62bd9e02013-11-19 21:59:42 -0800829}
830
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600831void TWPartition::Save_FS_Flags(const string& local_File_System, int local_Mount_Flags, const string& local_Mount_Options) {
832 partition_fs_flags_struct flags;
833 flags.File_System = local_File_System;
834 flags.Mount_Flags = local_Mount_Flags;
835 flags.Mount_Options = local_Mount_Options;
836 fs_flags.push_back(flags);
837}
838
Matt Mower2416a502016-04-12 19:54:46 -0500839void TWPartition::Apply_TW_Flag(const unsigned flag, const char* str, const bool val) {
840 switch (flag) {
841 case TWFLAG_ANDSEC:
842 Has_Android_Secure = val;
843 break;
844 case TWFLAG_BACKUP:
845 Can_Be_Backed_Up = val;
846 break;
847 case TWFLAG_BACKUPNAME:
848 Backup_Display_Name = str;
849 break;
850 case TWFLAG_BLOCKSIZE:
851 Format_Block_Size = (unsigned long)(atol(str));
852 break;
853 case TWFLAG_CANBEWIPED:
854 Can_Be_Wiped = val;
855 break;
856 case TWFLAG_CANENCRYPTBACKUP:
857 Can_Encrypt_Backup = val;
858 break;
859 case TWFLAG_DEFAULTS:
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600860 case TWFLAG_WAIT:
861 case TWFLAG_VERIFY:
862 case TWFLAG_CHECK:
863 case TWFLAG_NOTRIM:
864 case TWFLAG_VOLDMANAGED:
865 case TWFLAG_RESIZE:
Matt Mower2416a502016-04-12 19:54:46 -0500866 // Do nothing
867 break;
868 case TWFLAG_DISPLAY:
869 Display_Name = str;
870 break;
871 case TWFLAG_ENCRYPTABLE:
872 case TWFLAG_FORCEENCRYPT:
873 Crypto_Key_Location = str;
874 break;
Ethan Yonkere9afc3d2018-08-30 15:16:27 -0500875 case TWFLAG_FILEENCRYPTION:
876 // This flag isn't used by TWRP but is needed in 9.0 FBE decrypt
877 // fileencryption=ice:aes-256-heh
878 {
879 std::string FBE = str;
Ethan Yonkere9afc3d2018-08-30 15:16:27 -0500880 size_t colon_loc = FBE.find(":");
881 if (colon_loc == std::string::npos) {
Ethan Yonker93382822018-11-01 15:25:31 -0500882 property_set("fbe.contents", FBE.c_str());
883 property_set("fbe.filenames", "");
884 LOGINFO("FBE contents '%s', filenames ''\n", FBE.c_str());
Ethan Yonkere9afc3d2018-08-30 15:16:27 -0500885 break;
886 }
Ethan Yonker93382822018-11-01 15:25:31 -0500887 std::string FBE_contents, FBE_filenames;
Ethan Yonkere9afc3d2018-08-30 15:16:27 -0500888 FBE_contents = FBE.substr(0, colon_loc);
889 FBE_filenames = FBE.substr(colon_loc + 1);
890 property_set("fbe.contents", FBE_contents.c_str());
891 property_set("fbe.filenames", FBE_filenames.c_str());
892 LOGINFO("FBE contents '%s', filenames '%s'\n", FBE_contents.c_str(), FBE_filenames.c_str());
893 }
894 break;
Peter Cai28ea7b02019-05-24 11:38:54 +0800895 case TWFLAG_WRAPPEDKEY:
896 // Set fbe.data.wrappedkey to true
897 {
898 property_set("fbe.data.wrappedkey", "true");
899 LOGINFO("FBE wrapped key enabled\n");
900 }
901 break;
Matt Mower2416a502016-04-12 19:54:46 -0500902 case TWFLAG_FLASHIMG:
903 Can_Flash_Img = val;
904 break;
905 case TWFLAG_FSFLAGS:
Matt Mower4ab42b12016-04-21 13:52:18 -0500906 Process_FS_Flags(str);
Matt Mower2416a502016-04-12 19:54:46 -0500907 break;
908 case TWFLAG_IGNOREBLKID:
909 Ignore_Blkid = val;
910 break;
911 case TWFLAG_LENGTH:
912 Length = atoi(str);
913 break;
914 case TWFLAG_MOUNTTODECRYPT:
915 Mount_To_Decrypt = val;
916 break;
917 case TWFLAG_REMOVABLE:
918 Removable = val;
919 break;
920 case TWFLAG_RETAINLAYOUTVERSION:
921 Retain_Layout_Version = val;
922 break;
923 case TWFLAG_SETTINGSSTORAGE:
924 Is_Settings_Storage = val;
925 if (Is_Settings_Storage)
926 Is_Storage = true;
927 break;
928 case TWFLAG_STORAGE:
929 Is_Storage = val;
930 break;
931 case TWFLAG_STORAGENAME:
932 Storage_Name = str;
933 break;
934 case TWFLAG_SUBPARTITIONOF:
935 Is_SubPartition = true;
936 SubPartition_Of = str;
937 break;
938 case TWFLAG_SYMLINK:
939 Symlink_Path = str;
940 break;
941 case TWFLAG_USERDATAENCRYPTBACKUP:
942 Use_Userdata_Encryption = val;
943 if (Use_Userdata_Encryption)
944 Can_Encrypt_Backup = true;
945 break;
946 case TWFLAG_USERMRF:
947 Use_Rm_Rf = val;
948 break;
949 case TWFLAG_WIPEDURINGFACTORYRESET:
950 Wipe_During_Factory_Reset = val;
951 if (Wipe_During_Factory_Reset) {
952 Can_Be_Wiped = true;
953 Wipe_Available_in_GUI = true;
954 }
955 break;
956 case TWFLAG_WIPEINGUI:
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600957 case TWFLAG_FORMATTABLE:
Matt Mower2416a502016-04-12 19:54:46 -0500958 Wipe_Available_in_GUI = val;
959 if (Wipe_Available_in_GUI)
960 Can_Be_Wiped = true;
961 break;
Ethan Yonker1b190162016-12-05 15:25:19 -0600962 case TWFLAG_SLOTSELECT:
963 SlotSelect = true;
964 break;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600965 case TWFLAG_ALTDEVICE:
966 Alternate_Block_Device = str;
967 break;
Ethan Yonker93382822018-11-01 15:25:31 -0500968 case TWFLAG_KEYDIRECTORY:
969 Key_Directory = str;
bigbiffd58ba182020-03-23 10:02:29 -0400970 break;
Matt Mower2416a502016-04-12 19:54:46 -0500971 default:
972 // Should not get here
973 LOGINFO("Flag identified for processing, but later unmatched: %i\n", flag);
974 break;
975 }
976}
Dees_Troy51127312012-09-08 13:08:49 -0400977
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600978void TWPartition::Process_TW_Flags(char *flags, bool Display_Error, int fstab_ver) {
Matt Mower2416a502016-04-12 19:54:46 -0500979 char separator[2] = {'\n', 0};
980 char *ptr, *savep;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600981 char source_separator = ';';
982
983 if (fstab_ver == 2)
984 source_separator = ',';
Matt Mower2416a502016-04-12 19:54:46 -0500985
986 // Semicolons within double-quotes are not forbidden, so replace
987 // only the semicolons intended as separators with '\n' for strtok
988 for (unsigned i = 0, skip = 0; i < strlen(flags); i++) {
989 if (flags[i] == '\"')
Dees_Troya13d74f2013-03-24 08:54:55 -0500990 skip = !skip;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600991 if (!skip && flags[i] == source_separator)
Matt Mower2416a502016-04-12 19:54:46 -0500992 flags[i] = separator[0];
Dees_Troy51127312012-09-08 13:08:49 -0400993 }
994
Matt Mower2416a502016-04-12 19:54:46 -0500995 // Avoid issues with potentially nested strtok by using strtok_r
996 ptr = strtok_r(flags, separator, &savep);
997 while (ptr) {
998 int ptr_len = strlen(ptr);
999 const struct flag_list* tw_flag = tw_flags;
Hashcode62bd9e02013-11-19 21:59:42 -08001000
Matt Mower2416a502016-04-12 19:54:46 -05001001 for (; tw_flag->name; tw_flag++) {
1002 int flag_len = strlen(tw_flag->name);
1003
1004 if (strncmp(ptr, tw_flag->name, flag_len) == 0) {
1005 bool flag_val = false;
1006
1007 if (ptr_len > flag_len && (tw_flag->name)[flag_len-1] != '='
1008 && ptr[flag_len] != '=') {
1009 // Handle flags with same starting string
1010 // (e.g. backup and backupname)
1011 continue;
1012 } else if (ptr_len > flag_len && ptr[flag_len] == '=') {
1013 // Handle flags with dual format: Part 1
1014 // (e.g. backup and backup=y. backup=y handled here)
1015 ptr += flag_len + 1;
1016 TWFunc::Strip_Quotes(ptr);
1017 // Skip flags with empty argument
1018 // (e.g. backup=)
1019 if (strlen(ptr) == 0) {
1020 LOGINFO("Flag missing argument or should not include '=': %s=\n", tw_flag->name);
1021 break;
1022 }
1023 flag_val = strchr("yY1", *ptr) != NULL;
1024 } else if (ptr_len == flag_len
1025 && (tw_flag->name)[flag_len-1] == '=') {
1026 // Skip flags missing argument after =
1027 // (e.g. backupname=)
1028 LOGINFO("Flag missing argument: %s\n", tw_flag->name);
1029 break;
1030 } else if (ptr_len > flag_len
1031 && (tw_flag->name)[flag_len-1] == '=') {
1032 // Handle arguments to flags
1033 // (e.g. backupname="My Stuff")
1034 ptr += flag_len;
1035 TWFunc::Strip_Quotes(ptr);
1036 // Skip flags with empty argument
1037 // (e.g. backupname="")
1038 if (strlen(ptr) == 0) {
1039 LOGINFO("Flag missing argument: %s\n", tw_flag->name);
1040 break;
1041 }
1042 } else if (ptr_len == flag_len) {
1043 // Handle flags with dual format: Part 2
1044 // (e.g. backup and backup=y. backup handled here)
1045 flag_val = true;
Ethan Yonker96af84a2015-01-05 14:58:36 -06001046 } else {
Matt Mower2416a502016-04-12 19:54:46 -05001047 LOGINFO("Flag matched, but could not be processed: %s\n", ptr);
1048 break;
Ethan Yonker96af84a2015-01-05 14:58:36 -06001049 }
Matt Mower2416a502016-04-12 19:54:46 -05001050
1051 Apply_TW_Flag(tw_flag->flag, ptr, flag_val);
1052 break;
Ethan Yonker96af84a2015-01-05 14:58:36 -06001053 }
Matt Mower2416a502016-04-12 19:54:46 -05001054 }
1055 if (tw_flag->name == 0) {
Dees_Troy51127312012-09-08 13:08:49 -04001056 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001057 LOGERR("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -04001058 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001059 LOGINFO("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -04001060 }
Matt Mower2416a502016-04-12 19:54:46 -05001061 ptr = strtok_r(NULL, separator, &savep);
Dees_Troy51127312012-09-08 13:08:49 -04001062 }
Dees_Troy51a0e822012-09-05 15:24:24 -04001063}
1064
Dees_Troy5bf43922012-09-07 16:07:55 -04001065bool TWPartition::Is_File_System(string File_System) {
1066 if (File_System == "ext2" ||
Dees_Troy63c8df72012-09-10 14:02:05 -04001067 File_System == "ext3" ||
Dees_Troy5bf43922012-09-07 16:07:55 -04001068 File_System == "ext4" ||
1069 File_System == "vfat" ||
1070 File_System == "ntfs" ||
1071 File_System == "yaffs2" ||
bigbiff bigbiff3e146522012-11-14 14:32:59 -05001072 File_System == "exfat" ||
Dees_Troye5017042013-08-29 16:38:55 +00001073 File_System == "f2fs" ||
Erfan Abdib859e912019-02-05 18:18:46 +02001074 File_System == "squashfs" ||
Dees_Troy5bf43922012-09-07 16:07:55 -04001075 File_System == "auto")
1076 return true;
1077 else
1078 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001079}
1080
Dees_Troy5bf43922012-09-07 16:07:55 -04001081bool TWPartition::Is_Image(string File_System) {
Dees_Troy5fcd8f92012-10-16 12:22:05 -04001082 if (File_System == "emmc" || File_System == "mtd" || File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -04001083 return true;
1084 else
1085 return false;
1086}
1087
Dees_Troy51127312012-09-08 13:08:49 -04001088bool TWPartition::Make_Dir(string Path, bool Display_Error) {
Ethan Yonker93ac7a02016-11-07 22:05:58 -06001089 if (TWFunc::Get_D_Type_From_Stat(Path) != S_IFDIR)
1090 unlink(Path.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001091 if (!TWFunc::Path_Exists(Path)) {
Dees_Troy51127312012-09-08 13:08:49 -04001092 if (mkdir(Path.c_str(), 0777) == -1) {
1093 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001094 gui_msg(Msg(msg::kError, "create_folder_strerr=Can not create '{1}' folder ({2}).")(Path)(strerror(errno)));
Dees_Troy51127312012-09-08 13:08:49 -04001095 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001096 LOGINFO("Can not create '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -04001097 return false;
1098 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001099 LOGINFO("Created '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -04001100 return true;
1101 }
1102 }
1103 return true;
1104}
1105
Dees_Troy5bf43922012-09-07 16:07:55 -04001106void TWPartition::Setup_File_System(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001107 Can_Be_Mounted = true;
1108 Can_Be_Wiped = true;
1109
Dees_Troy5bf43922012-09-07 16:07:55 -04001110 // Make the mount point folder if it doesn't exist
Dees_Troy51127312012-09-08 13:08:49 -04001111 Make_Dir(Mount_Point, Display_Error);
bigbiffce8f83c2015-12-12 18:30:21 -05001112 Backup_Method = BM_FILES;
Dees_Troy5bf43922012-09-07 16:07:55 -04001113}
1114
Ethan Yonker1b190162016-12-05 15:25:19 -06001115void TWPartition::Setup_Image() {
Dees_Troy5bf43922012-09-07 16:07:55 -04001116 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
1117 Backup_Name = Display_Name;
Gary Peck82599a82012-11-21 16:23:12 -08001118 if (Current_File_System == "emmc")
bigbiffce8f83c2015-12-12 18:30:21 -05001119 Backup_Method = BM_DD;
Gary Peck82599a82012-11-21 16:23:12 -08001120 else if (Current_File_System == "mtd" || Current_File_System == "bml")
bigbiffce8f83c2015-12-12 18:30:21 -05001121 Backup_Method = BM_FLASH_UTILS;
Dees_Troy5bf43922012-09-07 16:07:55 -04001122 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001123 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 -04001124}
1125
Dees_Troye58d5262012-09-21 12:27:57 -04001126void TWPartition::Setup_AndSec(void) {
Dees_Troya13d74f2013-03-24 08:54:55 -05001127 Backup_Display_Name = "Android Secure";
Dees_Troye58d5262012-09-21 12:27:57 -04001128 Backup_Name = "and-sec";
Dees_Troya13d74f2013-03-24 08:54:55 -05001129 Can_Be_Backed_Up = true;
Dees_Troye58d5262012-09-21 12:27:57 -04001130 Has_Android_Secure = true;
1131 Symlink_Path = Mount_Point + "/.android_secure";
1132 Symlink_Mount_Point = "/and-sec";
1133 Backup_Path = Symlink_Mount_Point;
1134 Make_Dir("/and-sec", true);
1135 Recreate_AndSec_Folder();
Kjell Braden3126a112016-06-19 16:58:15 +00001136 Mount_Storage_Retry(true);
Dees_Troye58d5262012-09-21 12:27:57 -04001137}
1138
that9e0593e2014-10-08 00:01:24 +02001139void TWPartition::Setup_Data_Media() {
Ethan Yonker6277c792014-09-15 14:54:30 -05001140 LOGINFO("Setting up '%s' as data/media emulated storage.\n", Mount_Point.c_str());
Ethan Yonker66a19492015-12-10 10:19:45 -06001141 if (Storage_Name.empty() || Storage_Name == "Data")
1142 Storage_Name = "Internal Storage";
Ethan Yonker6277c792014-09-15 14:54:30 -05001143 Has_Data_Media = true;
1144 Is_Storage = true;
Ethan Yonker66a19492015-12-10 10:19:45 -06001145 Storage_Path = Mount_Point + "/media";
Ethan Yonker6277c792014-09-15 14:54:30 -05001146 Symlink_Path = Storage_Path;
Ethan Yonker66a19492015-12-10 10:19:45 -06001147 if (Mount_Point == "/data") {
1148 Is_Settings_Storage = true;
1149 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
1150 Make_Dir("/emmc", false);
1151 Symlink_Mount_Point = "/emmc";
1152 } else {
1153 Make_Dir("/sdcard", false);
1154 Symlink_Mount_Point = "/sdcard";
1155 }
1156 if (Mount(false) && TWFunc::Path_Exists(Mount_Point + "/media/0")) {
1157 Storage_Path = Mount_Point + "/media/0";
1158 Symlink_Path = Storage_Path;
1159 DataManager::SetValue(TW_INTERNAL_PATH, Mount_Point + "/media/0");
1160 UnMount(true);
1161 }
1162 DataManager::SetValue("tw_has_internal", 1);
1163 DataManager::SetValue("tw_has_data_media", 1);
Ethan Yonker3fdcda42016-11-30 12:29:37 -06001164 backup_exclusions.add_absolute_dir("/data/data/com.google.android.music/files");
Ethan Yonker6355b562017-05-01 09:42:17 -05001165 wipe_exclusions.add_absolute_dir(Mount_Point + "/misc/vold"); // adopted storage keys
Ethan Yonker79f88bd2016-12-09 14:52:12 -06001166 ExcludeAll(Mount_Point + "/.layout_version");
1167 ExcludeAll(Mount_Point + "/system/storage.xml");
Ethan Yonker6277c792014-09-15 14:54:30 -05001168 } else {
Ethan Yonker66a19492015-12-10 10:19:45 -06001169 if (Mount(true) && TWFunc::Path_Exists(Mount_Point + "/media/0")) {
1170 Storage_Path = Mount_Point + "/media/0";
1171 Symlink_Path = Storage_Path;
1172 UnMount(true);
1173 }
Ethan Yonker6277c792014-09-15 14:54:30 -05001174 }
Ethan Yonker79f88bd2016-12-09 14:52:12 -06001175 ExcludeAll(Mount_Point + "/media");
Ethan Yonker6277c792014-09-15 14:54:30 -05001176}
1177
Dees_Troy5bf43922012-09-07 16:07:55 -04001178void TWPartition::Find_Real_Block_Device(string& Block, bool Display_Error) {
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06001179 char device[PATH_MAX], realDevice[PATH_MAX];
Dees_Troy5bf43922012-09-07 16:07:55 -04001180
1181 strcpy(device, Block.c_str());
1182 memset(realDevice, 0, sizeof(realDevice));
1183 while (readlink(device, realDevice, sizeof(realDevice)) > 0)
1184 {
1185 strcpy(device, realDevice);
1186 memset(realDevice, 0, sizeof(realDevice));
1187 }
1188
1189 if (device[0] != '/') {
1190 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001191 LOGERR("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001192 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001193 LOGINFO("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001194 return;
Dees_Troy51a0e822012-09-05 15:24:24 -04001195 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -04001196 Block = device;
1197 return;
1198 }
1199}
1200
Kjell Braden3126a112016-06-19 16:58:15 +00001201bool TWPartition::Mount_Storage_Retry(bool Display_Error) {
Dees_Troy8e337f32012-10-13 22:07:49 -04001202 // On some devices, storage doesn't want to mount right away, retry and sleep
Kjell Braden3126a112016-06-19 16:58:15 +00001203 if (!Mount(Display_Error)) {
Dees_Troy8e337f32012-10-13 22:07:49 -04001204 int retry_count = 5;
1205 while (retry_count > 0 && !Mount(false)) {
1206 usleep(500000);
1207 retry_count--;
1208 }
Kjell Braden3126a112016-06-19 16:58:15 +00001209 return Mount(Display_Error);
Dees_Troy8e337f32012-10-13 22:07:49 -04001210 }
Kjell Braden3126a112016-06-19 16:58:15 +00001211 return true;
Dees_Troy8e337f32012-10-13 22:07:49 -04001212}
1213
Dees_Troy38bd7602012-09-14 13:33:53 -04001214bool TWPartition::Find_MTD_Block_Device(string MTD_Name) {
1215 FILE *fp = NULL;
1216 char line[255];
1217
1218 fp = fopen("/proc/mtd", "rt");
1219 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001220 LOGERR("Device does not support /proc/mtd\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001221 return false;
1222 }
1223
1224 while (fgets(line, sizeof(line), fp) != NULL)
1225 {
1226 char device[32], label[32];
1227 unsigned long size = 0;
Dees_Troy38bd7602012-09-14 13:33:53 -04001228 int deviceId;
1229
1230 sscanf(line, "%s %lx %*s %*c%s", device, &size, label);
1231
1232 // Skip header and blank lines
1233 if ((strcmp(device, "dev:") == 0) || (strlen(line) < 8))
1234 continue;
1235
1236 // Strip off the trailing " from the label
1237 label[strlen(label)-1] = '\0';
1238
1239 if (strcmp(label, MTD_Name.c_str()) == 0) {
1240 // We found our device
1241 // Strip off the trailing : from the device
1242 device[strlen(device)-1] = '\0';
1243 if (sscanf(device,"mtd%d", &deviceId) == 1) {
1244 sprintf(device, "/dev/block/mtdblock%d", deviceId);
1245 Primary_Block_Device = device;
Dees_Troy76543db2013-06-19 16:24:30 +00001246 fclose(fp);
1247 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -04001248 }
1249 }
1250 }
1251 fclose(fp);
1252
1253 return false;
1254}
1255
Dees_Troy51127312012-09-08 13:08:49 -04001256bool TWPartition::Get_Size_Via_statfs(bool Display_Error) {
1257 struct statfs st;
1258 string Local_Path = Mount_Point + "/.";
1259
1260 if (!Mount(Display_Error))
1261 return false;
1262
1263 if (statfs(Local_Path.c_str(), &st) != 0) {
1264 if (!Removable) {
1265 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001266 LOGERR("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -04001267 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001268 LOGINFO("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -04001269 }
1270 return false;
1271 }
1272 Size = (st.f_blocks * st.f_bsize);
1273 Used = ((st.f_blocks - st.f_bfree) * st.f_bsize);
1274 Free = (st.f_bfree * st.f_bsize);
1275 Backup_Size = Used;
1276 return true;
1277}
1278
1279bool TWPartition::Get_Size_Via_df(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001280 FILE* fp;
1281 char command[255], line[512];
1282 int include_block = 1;
1283 unsigned int min_len;
1284
1285 if (!Mount(Display_Error))
1286 return false;
1287
Dees_Troy38bd7602012-09-14 13:33:53 -04001288 min_len = Actual_Block_Device.size() + 2;
Dees_Troy51127312012-09-08 13:08:49 -04001289 sprintf(command, "df %s > /tmp/dfoutput.txt", Mount_Point.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001290 TWFunc::Exec_Cmd(command);
Dees_Troy51127312012-09-08 13:08:49 -04001291 fp = fopen("/tmp/dfoutput.txt", "rt");
1292 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001293 LOGINFO("Unable to open /tmp/dfoutput.txt.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001294 return false;
Dees_Troy51127312012-09-08 13:08:49 -04001295 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001296
1297 while (fgets(line, sizeof(line), fp) != NULL)
1298 {
1299 unsigned long blocks, used, available;
1300 char device[64];
1301 char tmpString[64];
1302
1303 if (strncmp(line, "Filesystem", 10) == 0)
1304 continue;
1305 if (strlen(line) < min_len) {
1306 include_block = 0;
1307 continue;
1308 }
1309 if (include_block) {
1310 sscanf(line, "%s %lu %lu %lu", device, &blocks, &used, &available);
1311 } else {
1312 // The device block string is so long that the df information is on the next line
1313 int space_count = 0;
Dees_Troye58d5262012-09-21 12:27:57 -04001314 sprintf(tmpString, "/dev/block/%s", Actual_Block_Device.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001315 while (tmpString[space_count] == 32)
1316 space_count++;
1317 sscanf(line + space_count, "%lu %lu %lu", &blocks, &used, &available);
1318 }
1319
1320 // Adjust block size to byte size
1321 Size = blocks * 1024ULL;
1322 Used = used * 1024ULL;
1323 Free = available * 1024ULL;
1324 Backup_Size = Used;
1325 }
1326 fclose(fp);
1327 return true;
1328}
1329
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001330unsigned long long TWPartition::IOCTL_Get_Block_Size() {
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001331 Find_Actual_Block_Device();
Ethan Yonker483e9f42016-01-11 22:21:18 -06001332
1333 return TWFunc::IOCTL_Get_Block_Size(Actual_Block_Device.c_str());
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001334}
1335
Dees_Troy5bf43922012-09-07 16:07:55 -04001336bool TWPartition::Find_Partition_Size(void) {
1337 FILE* fp;
1338 char line[512];
1339 string tmpdevice;
1340
igoriok87e3d932013-01-31 21:03:53 +02001341 fp = fopen("/proc/dumchar_info", "rt");
1342 if (fp != NULL) {
1343 while (fgets(line, sizeof(line), fp) != NULL)
1344 {
1345 char label[32], device[32];
1346 unsigned long size = 0;
1347
thatd43bf2d2014-09-21 23:13:02 +02001348 sscanf(line, "%s %lx %*x %*u %s", label, &size, device);
igoriok87e3d932013-01-31 21:03:53 +02001349
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05001350 // Skip header, annotation and blank lines
igoriok87e3d932013-01-31 21:03:53 +02001351 if ((strncmp(device, "/dev/", 5) != 0) || (strlen(line) < 8))
1352 continue;
1353
1354 tmpdevice = "/dev/";
1355 tmpdevice += label;
1356 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
1357 Size = size;
1358 fclose(fp);
1359 return true;
1360 }
1361 }
1362 }
1363
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001364 unsigned long long ioctl_size = IOCTL_Get_Block_Size();
1365 if (ioctl_size) {
1366 Size = ioctl_size;
1367 return true;
1368 }
1369
Dees_Troy5bf43922012-09-07 16:07:55 -04001370 // In this case, we'll first get the partitions we care about (with labels)
1371 fp = fopen("/proc/partitions", "rt");
1372 if (fp == NULL)
1373 return false;
1374
1375 while (fgets(line, sizeof(line), fp) != NULL)
1376 {
1377 unsigned long major, minor, blocks;
1378 char device[512];
Dees_Troy5bf43922012-09-07 16:07:55 -04001379
Dees_Troy63c8df72012-09-10 14:02:05 -04001380 if (strlen(line) < 7 || line[0] == 'm') continue;
Dees_Troy5bf43922012-09-07 16:07:55 -04001381 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
1382
1383 tmpdevice = "/dev/block/";
1384 tmpdevice += device;
Dees_Troy38bd7602012-09-14 13:33:53 -04001385 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001386 // Adjust block size to byte size
1387 Size = blocks * 1024ULL;
1388 fclose(fp);
1389 return true;
1390 }
1391 }
1392 fclose(fp);
1393 return false;
1394}
1395
Dees_Troy5bf43922012-09-07 16:07:55 -04001396bool TWPartition::Is_Mounted(void) {
1397 if (!Can_Be_Mounted)
1398 return false;
1399
1400 struct stat st1, st2;
1401 string test_path;
1402
1403 // Check to see if the mount point directory exists
1404 test_path = Mount_Point + "/.";
1405 if (stat(test_path.c_str(), &st1) != 0) return false;
1406
1407 // Check to see if the directory above the mount point exists
1408 test_path = Mount_Point + "/../.";
1409 if (stat(test_path.c_str(), &st2) != 0) return false;
1410
1411 // Compare the device IDs -- if they match then we're (probably) using tmpfs instead of an actual device
1412 int ret = (st1.st_dev != st2.st_dev) ? true : false;
1413
1414 return ret;
1415}
1416
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05001417bool TWPartition::Is_File_System_Writable(void) {
1418 if (!Is_File_System(Current_File_System) || !Is_Mounted())
1419 return false;
1420
1421 string test_path = Mount_Point + "/.";
1422 return (access(test_path.c_str(), W_OK) == 0);
1423}
1424
Dees_Troy5bf43922012-09-07 16:07:55 -04001425bool TWPartition::Mount(bool Display_Error) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001426 int exfat_mounted = 0;
Martin Dünkelmann6d843782018-12-27 23:40:28 +01001427 unsigned int flags = Mount_Flags;
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001428
Dees_Troy5bf43922012-09-07 16:07:55 -04001429 if (Is_Mounted()) {
1430 return true;
1431 } else if (!Can_Be_Mounted) {
1432 return false;
1433 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001434
1435 Find_Actual_Block_Device();
1436
1437 // Check the current file system before mounting
1438 Check_FS_Type();
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001439 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/exfat-fuse")) {
Dees_Troye34c1332013-02-06 19:13:00 +00001440 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 +00001441 LOGINFO("cmd: %s\n", cmd.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001442 string result;
1443 if (TWFunc::Exec_Cmd(cmd, result) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001444 LOGINFO("exfat-fuse failed to mount with result '%s', trying vfat\n", result.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001445 Current_File_System = "vfat";
1446 } else {
1447#ifdef TW_NO_EXFAT_FUSE
1448 UnMount(false);
1449 // We'll let the kernel handle it but using exfat-fuse to detect if the file system is actually exfat
1450 // Some kernels let us mount vfat as exfat which doesn't work out too well
1451#else
1452 exfat_mounted = 1;
1453#endif
1454 }
1455 }
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001456
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001457 if (Current_File_System == "ntfs" && (TWFunc::Path_Exists("/sbin/ntfs-3g") || TWFunc::Path_Exists("/sbin/mount.ntfs"))) {
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001458 string cmd;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001459 string Ntfsmount_Binary = "";
1460
1461 if (TWFunc::Path_Exists("/sbin/ntfs-3g"))
1462 Ntfsmount_Binary = "ntfs-3g";
1463 else if (TWFunc::Path_Exists("/sbin/mount.ntfs"))
1464 Ntfsmount_Binary = "mount.ntfs";
1465
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001466 if (Mount_Read_Only)
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001467 cmd = "/sbin/" + Ntfsmount_Binary + " -o ro " + Actual_Block_Device + " " + Mount_Point;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001468 else
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001469 cmd = "/sbin/" + Ntfsmount_Binary + " " + Actual_Block_Device + " " + Mount_Point;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001470 LOGINFO("cmd: '%s'\n", cmd.c_str());
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001471
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001472 if (TWFunc::Exec_Cmd(cmd) == 0) {
1473 return true;
1474 } else {
1475 LOGINFO("ntfs-3g failed to mount, trying regular mount method.\n");
1476 }
1477 }
1478
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001479 if (Mount_Read_Only)
1480 flags |= MS_RDONLY;
1481
Dees_Troy22042032012-12-18 21:23:08 +00001482 if (Fstab_File_System == "yaffs2") {
1483 // mount an MTD partition as a YAFFS2 filesystem.
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001484 flags = MS_NOATIME | MS_NODEV | MS_NODIRATIME;
1485 if (Mount_Read_Only)
1486 flags |= MS_RDONLY;
Dees_Troy76543db2013-06-19 16:24:30 +00001487 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags, NULL) < 0) {
1488 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags | MS_RDONLY, NULL) < 0) {
1489 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001490 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy76543db2013-06-19 16:24:30 +00001491 else
1492 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
1493 return false;
1494 } else {
1495 LOGINFO("Mounted '%s' (MTD) as RO\n", Mount_Point.c_str());
1496 return true;
1497 }
1498 } else {
1499 struct stat st;
1500 string test_path = Mount_Point;
1501 if (stat(test_path.c_str(), &st) < 0) {
1502 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001503 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy76543db2013-06-19 16:24:30 +00001504 else
1505 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
1506 return false;
1507 }
1508 mode_t new_mode = st.st_mode | S_IXUSR | S_IXGRP | S_IXOTH;
1509 if (new_mode != st.st_mode) {
1510 LOGINFO("Fixing execute permissions for %s\n", Mount_Point.c_str());
1511 if (chmod(Mount_Point.c_str(), new_mode) < 0) {
1512 if (Display_Error)
1513 LOGERR("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
1514 else
1515 LOGINFO("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
1516 return false;
1517 }
1518 }
Dees_Troy22042032012-12-18 21:23:08 +00001519 return true;
Dees_Troy76543db2013-06-19 16:24:30 +00001520 }
thatc7572eb2015-03-31 18:55:30 +02001521 }
1522
1523 string mount_fs = Current_File_System;
1524 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sys/module/texfat"))
1525 mount_fs = "texfat";
1526
1527 if (!exfat_mounted &&
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001528 mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), mount_fs.c_str(), flags, Mount_Options.c_str()) != 0 &&
1529 mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), mount_fs.c_str(), flags, NULL) != 0) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001530#ifdef TW_NO_EXFAT_FUSE
1531 if (Current_File_System == "exfat") {
Dees_Troy2673cec2013-04-02 20:22:16 +00001532 LOGINFO("Mounting exfat failed, trying vfat...\n");
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001533 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), "vfat", 0, NULL) != 0) {
Dees_Troy85f44ed2013-01-09 18:42:36 +00001534 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001535 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy85f44ed2013-01-09 18:42:36 +00001536 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001537 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001538 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 +00001539 return false;
Dees_Troy85f44ed2013-01-09 18:42:36 +00001540 }
Dees_Troyb05ddee2013-01-28 20:24:50 +00001541 } else {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001542#endif
bigbiff bigbiff26774a02014-03-29 18:22:00 -04001543 if (!Removable && Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001544 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001545 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001546 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
1547 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 +00001548 return false;
1549#ifdef TW_NO_EXFAT_FUSE
Dees_Troy85f44ed2013-01-09 18:42:36 +00001550 }
1551#endif
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001552 }
Ethan Yonker253368a2014-11-25 15:00:52 -06001553
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001554 if (Removable)
1555 Update_Size(Display_Error);
1556
xiaolu9416f4f2015-06-04 08:22:23 +08001557 if (!Symlink_Mount_Point.empty() && TWFunc::Path_Exists(Symlink_Path)) {
codelover2a3d4ce2015-03-14 20:26:49 +08001558 string Command = "mount -o bind '" + Symlink_Path + "' '" + Symlink_Mount_Point + "'";
Vojtech Bocek05534202013-09-11 08:11:56 +02001559 TWFunc::Exec_Cmd(Command);
Dees_Troy5bf43922012-09-07 16:07:55 -04001560 }
Chaosmasterda974802020-01-26 21:09:28 +01001561
1562 if (Mount_Point == "/system_root") {
1563 unlink("/system");
1564 mkdir("/system", 0755);
1565 mount("/system_root/system", "/system", "auto", MS_BIND, NULL);
1566 }
1567
Dees_Troy5bf43922012-09-07 16:07:55 -04001568 return true;
1569}
1570
1571bool TWPartition::UnMount(bool Display_Error) {
Chaosmasterda974802020-01-26 21:09:28 +01001572 if (Mount_Point == "/system_root") {
1573 if (umount("/system") == -1)
1574 umount2("/system", MNT_DETACH);
1575 rmdir("/system");
1576 symlink("/system_root/system", "/system");
1577 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001578 if (Is_Mounted()) {
1579 int never_unmount_system;
1580
1581 DataManager::GetValue(TW_DONT_UNMOUNT_SYSTEM, never_unmount_system);
Captain Throwback9d6feb52018-07-27 10:05:24 -04001582 if (never_unmount_system == 1 && Mount_Point == PartitionManager.Get_Android_Root_Path())
Dees_Troy5bf43922012-09-07 16:07:55 -04001583 return true; // Never unmount system if you're not supposed to unmount it
1584
Matt Mowera8e6d832017-02-14 20:20:59 -06001585 if (Is_Storage && MTP_Storage_ID > 0)
Ethan Yonker726a0202014-12-16 20:01:38 -06001586 PartitionManager.Remove_MTP_Storage(MTP_Storage_ID);
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001587
Dees_Troy38bd7602012-09-14 13:33:53 -04001588 if (!Symlink_Mount_Point.empty())
1589 umount(Symlink_Mount_Point.c_str());
1590
Dees_Troyb05ddee2013-01-28 20:24:50 +00001591 umount(Mount_Point.c_str());
1592 if (Is_Mounted()) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001593 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001594 gui_msg(Msg(msg::kError, "fail_unmount=Failed to unmount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy5bf43922012-09-07 16:07:55 -04001595 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001596 LOGINFO("Unable to unmount '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001597 return false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001598 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -04001599 return true;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001600 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001601 } else {
1602 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001603 }
1604}
1605
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05001606bool TWPartition::ReMount(bool Display_Error) {
1607 if (UnMount(Display_Error))
1608 return Mount(Display_Error);
1609 return false;
1610}
1611
1612bool TWPartition::ReMount_RW(bool Display_Error) {
1613 // No need to remount if already mounted rw
1614 if (Is_File_System_Writable())
1615 return true;
1616
1617 bool ro = Mount_Read_Only;
1618 int flags = Mount_Flags;
1619
1620 Mount_Read_Only = false;
1621 Mount_Flags &= ~MS_RDONLY;
1622
1623 bool ret = ReMount(Display_Error);
1624
1625 Mount_Read_Only = ro;
1626 Mount_Flags = flags;
1627
1628 return ret;
1629}
1630
Gary Peck43acadf2012-11-21 21:19:01 -08001631bool TWPartition::Wipe(string New_File_System) {
Ethan Yonker726a0202014-12-16 20:01:38 -06001632 bool wiped = false, update_crypt = false, recreate_media = true;
Dees_Troy16c2b312013-01-15 16:51:18 +00001633 int check;
1634 string Layout_Filename = Mount_Point + "/.layout_version";
1635
Dees_Troy38bd7602012-09-14 13:33:53 -04001636 if (!Can_Be_Wiped) {
Matt Mower3c366972015-12-25 19:28:31 -06001637 gui_msg(Msg(msg::kError, "cannot_wipe=Partition {1} cannot be wiped.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04001638 return false;
1639 }
1640
Dees_Troyc51f1f92012-09-20 15:32:13 -04001641 if (Mount_Point == "/cache")
Dees_Troy2673cec2013-04-02 20:22:16 +00001642 Log_Offset = 0;
Dees_Troyc51f1f92012-09-20 15:32:13 -04001643
Dees_Troy16c2b312013-01-15 16:51:18 +00001644 if (Retain_Layout_Version && Mount(false) && TWFunc::Path_Exists(Layout_Filename))
1645 TWFunc::copy_file(Layout_Filename, "/.layout_version", 0600);
1646 else
1647 unlink("/.layout_version");
Dees_Troy38bd7602012-09-14 13:33:53 -04001648
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001649 if (Has_Data_Media && Current_File_System == New_File_System) {
Dees_Troy16c2b312013-01-15 16:51:18 +00001650 wiped = Wipe_Data_Without_Wiping_Media();
Ethan Yonker5eac2222014-06-11 12:22:55 -05001651 recreate_media = false;
Dees_Troy16c2b312013-01-15 16:51:18 +00001652 } else {
Dees_Troy16c2b312013-01-15 16:51:18 +00001653 DataManager::GetValue(TW_RM_RF_VAR, check);
1654
Hashcodedabfd492013-08-29 22:45:30 -07001655 if (check || Use_Rm_Rf)
Dees_Troy16c2b312013-01-15 16:51:18 +00001656 wiped = Wipe_RMRF();
1657 else if (New_File_System == "ext4")
1658 wiped = Wipe_EXT4();
1659 else if (New_File_System == "ext2" || New_File_System == "ext3")
dianlujitao4879b372018-12-03 18:45:47 +08001660 wiped = Wipe_EXTFS(New_File_System);
Dees_Troy16c2b312013-01-15 16:51:18 +00001661 else if (New_File_System == "vfat")
1662 wiped = Wipe_FAT();
1663 else if (New_File_System == "exfat")
1664 wiped = Wipe_EXFAT();
1665 else if (New_File_System == "yaffs2")
1666 wiped = Wipe_MTD();
Dees_Troye5017042013-08-29 16:38:55 +00001667 else if (New_File_System == "f2fs")
1668 wiped = Wipe_F2FS();
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001669 else if (New_File_System == "ntfs")
1670 wiped = Wipe_NTFS();
Dees_Troy16c2b312013-01-15 16:51:18 +00001671 else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001672 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 +00001673 unlink("/.layout_version");
1674 return false;
1675 }
1676 update_crypt = wiped;
Gary Pecke8bc5d72012-12-21 06:45:25 -08001677 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001678
Gary Pecke8bc5d72012-12-21 06:45:25 -08001679 if (wiped) {
Dees_Troy1c1ac442013-01-17 21:42:14 +00001680 if (Mount_Point == "/cache")
1681 DataManager::Output_Version();
1682
Dees_Troy16c2b312013-01-15 16:51:18 +00001683 if (TWFunc::Path_Exists("/.layout_version") && Mount(false))
1684 TWFunc::copy_file("/.layout_version", Layout_Filename, 0600);
1685
1686 if (update_crypt) {
1687 Setup_File_System(false);
1688 if (Is_Encrypted && !Is_Decrypted) {
1689 // just wiped an encrypted partition back to its unencrypted state
1690 Is_Encrypted = false;
1691 Is_Decrypted = false;
1692 Decrypted_Block_Device = "";
1693 if (Mount_Point == "/data") {
1694 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1695 DataManager::SetValue(TW_IS_DECRYPTED, 0);
1696 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001697 }
1698 }
Ethan Yonker5eac2222014-06-11 12:22:55 -05001699
Ethan Yonker66a19492015-12-10 10:19:45 -06001700 if (Has_Data_Media && recreate_media) {
Ethan Yonker5eac2222014-06-11 12:22:55 -05001701 Recreate_Media_Folder();
1702 }
Matt Mower209c9632016-01-20 12:08:35 -06001703 if (Is_Storage && Mount(false))
1704 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Gary Pecke8bc5d72012-12-21 06:45:25 -08001705 }
Matt Mower209c9632016-01-20 12:08:35 -06001706
Gary Pecke8bc5d72012-12-21 06:45:25 -08001707 return wiped;
Dees_Troy51a0e822012-09-05 15:24:24 -04001708}
1709
Gary Peck43acadf2012-11-21 21:19:01 -08001710bool TWPartition::Wipe() {
Gary Peck82599a82012-11-21 16:23:12 -08001711 if (Is_File_System(Current_File_System))
1712 return Wipe(Current_File_System);
1713 else
1714 return Wipe(Fstab_File_System);
Gary Peck43acadf2012-11-21 21:19:01 -08001715}
1716
Dees_Troye58d5262012-09-21 12:27:57 -04001717bool TWPartition::Wipe_AndSec(void) {
1718 if (!Has_Android_Secure)
1719 return false;
1720
Dees_Troye58d5262012-09-21 12:27:57 -04001721 if (!Mount(true))
1722 return false;
1723
Ethan Yonker74db1572015-10-28 12:44:49 -05001724 gui_msg(Msg("wiping=Wiping {1}")(Backup_Display_Name));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001725 TWFunc::removeDir(Mount_Point + "/.android_secure/", true);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001726 return true;
Dees_Troye58d5262012-09-21 12:27:57 -04001727}
1728
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001729bool TWPartition::Can_Repair() {
Ethan Yonkera2719152015-05-28 09:44:41 -05001730 if (Mount_Read_Only)
1731 return false;
Matt Mower18794c82015-11-11 16:22:45 -06001732 if (Current_File_System == "vfat" && TWFunc::Path_Exists("/sbin/fsck.fat"))
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001733 return true;
1734 else if ((Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") && TWFunc::Path_Exists("/sbin/e2fsck"))
1735 return true;
1736 else if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/fsck.exfat"))
1737 return true;
1738 else if (Current_File_System == "f2fs" && TWFunc::Path_Exists("/sbin/fsck.f2fs"))
1739 return true;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001740 else if (Current_File_System == "ntfs" && (TWFunc::Path_Exists("/sbin/ntfsfix") || TWFunc::Path_Exists("/sbin/fsck.ntfs")))
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001741 return true;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001742 return false;
1743}
1744
1745bool TWPartition::Repair() {
1746 string command;
1747
1748 if (Current_File_System == "vfat") {
Matt Mower18794c82015-11-11 16:22:45 -06001749 if (!TWFunc::Path_Exists("/sbin/fsck.fat")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001750 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.fat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001751 return false;
1752 }
1753 if (!UnMount(true))
1754 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001755 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.fat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001756 Find_Actual_Block_Device();
Matt Mower18794c82015-11-11 16:22:45 -06001757 command = "/sbin/fsck.fat -y " + Actual_Block_Device;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001758 LOGINFO("Repair command: %s\n", command.c_str());
1759 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001760 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001761 return true;
1762 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500