blob: 183283dce3bebaa88b0d08e18154d00ded39e535 [file] [log] [blame]
Dees Troy3be70a82013-10-22 14:25:12 +00001/*
bigbiff7ba75002020-04-11 20:47:09 -04002 Copyright 2013 to 2020 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>
bigbiff7ba75002020-04-11 20:47:09 -040022
Dees_Troy5bf43922012-09-07 16:07:55 -040023#include <sys/mount.h>
bigbiff7ba75002020-04-11 20:47:09 -040024#include <sys/param.h>
25#include <sys/stat.h>
26#include <sys/types.h>
27#include <sys/vfs.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040028#include <unistd.h>
Dees_Troy51127312012-09-08 13:08:49 -040029#include <dirent.h>
Ethan Yonker472f5062016-02-25 13:47:30 -060030#include <fcntl.h>
bigbiff7ba75002020-04-11 20:47:09 -040031#include <grp.h>
32#include <iostream>
33#include <libgen.h>
34#include <pwd.h>
35#include <zlib.h>
36#include <sstream>
Dees_Troy51a0e822012-09-05 15:24:24 -040037
Ethan Yonker007de982018-08-31 14:23:16 -050038#include "cutils/properties.h"
bigbiff7b4c7a62015-01-01 19:44:14 -050039#include "libblkid/include/blkid.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040040#include "variables.h"
Dees_Troy2673cec2013-04-02 20:22:16 +000041#include "twcommon.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040042#include "partitions.hpp"
Dees_Troy5bf43922012-09-07 16:07:55 -040043#include "data.hpp"
Dees_Troy43d8b002012-09-17 16:00:01 -040044#include "twrp-functions.hpp"
bigbiff bigbiff9c754052013-01-09 09:09:08 -050045#include "twrpTar.hpp"
Ethan Yonker3fdcda42016-11-30 12:29:37 -060046#include "exclude.hpp"
Ethan Yonker1b7a31b2014-07-03 15:09:22 -050047#include "infomanager.hpp"
Ethan Yonker4b94cfd2014-12-11 10:00:45 -060048#include "set_metadata.h"
Ethan Yonker74db1572015-10-28 12:44:49 -050049#include "gui/gui.hpp"
bigbiffce8f83c2015-12-12 18:30:21 -050050#include "adbbu/libtwadbbu.hpp"
Ethan Yonker98661c12018-10-17 08:39:28 -050051#ifdef TW_INCLUDE_CRYPTO
52 #include "crypto/fde/cryptfs.h"
53 #ifdef TW_INCLUDE_FBE
54 #include "crypto/ext4crypt/Decrypt.h"
55 #endif
56#else
57 #define CRYPT_FOOTER_OFFSET 0x4000
58#endif
Dees_Troy5bf43922012-09-07 16:07:55 -040059extern "C" {
Dees_Troy38bd7602012-09-14 13:33:53 -040060 #include "mtdutils/mtdutils.h"
61 #include "mtdutils/mounts.h"
Dees_Troya95f55c2013-08-17 13:14:43 +000062#ifdef USE_EXT4
Ethan Yonker8373cfe2017-09-08 06:50:54 -050063 // #include "make_ext4fs.h" TODO need ifdef for android8
64 #include <ext4_utils/make_ext4fs.h>
Dees_Troya95f55c2013-08-17 13:14:43 +000065#endif
Ethan Yonker71413f42014-02-26 13:36:08 -060066#ifdef TW_INCLUDE_CRYPTO
Ethan Yonker66a19492015-12-10 10:19:45 -060067 #include "gpt/gpt.h"
Ethan Yonker71413f42014-02-26 13:36:08 -060068#endif
Dees_Troy5bf43922012-09-07 16:07:55 -040069}
Matt Mower87413642017-01-17 21:14:46 -060070#include <selinux/selinux.h>
Ethan Yonkerf27497f2014-02-09 11:48:33 -060071#include <selinux/label.h>
Dees Troy4159aed2014-02-28 17:24:43 +000072#ifdef HAVE_CAPABILITIES
73#include <sys/capability.h>
74#include <sys/xattr.h>
75#include <linux/xattr.h>
76#endif
HashBanged974bb2016-01-30 14:20:09 -050077#include <sparse_format.h>
Ethan Yonker472f5062016-02-25 13:47:30 -060078#include "progresstracking.hpp"
Dees_Troy51a0e822012-09-05 15:24:24 -040079
bigbiff bigbiff9c754052013-01-09 09:09:08 -050080using namespace std;
81
Ethan Yonker6e8c27a2016-12-22 17:55:57 -060082static 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
83
Dees_Troya95f55c2013-08-17 13:14:43 +000084extern struct selabel_handle *selinux_handle;
Ethan Yonker6277c792014-09-15 14:54:30 -050085extern bool datamedia;
Dees_Troya95f55c2013-08-17 13:14:43 +000086
Hashcode62bd9e02013-11-19 21:59:42 -080087struct flag_list {
88 const char *name;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -060089 unsigned long flag;
Hashcode62bd9e02013-11-19 21:59:42 -080090};
91
Matt Mower4ab42b12016-04-21 13:52:18 -050092const struct flag_list mount_flags[] = {
Ethan Yonker6e8c27a2016-12-22 17:55:57 -060093 { "noatime", MS_NOATIME },
94 { "noexec", MS_NOEXEC },
95 { "nosuid", MS_NOSUID },
96 { "nodev", MS_NODEV },
97 { "nodiratime", MS_NODIRATIME },
98 { "ro", MS_RDONLY },
99 { "rw", 0 },
100 { "remount", MS_REMOUNT },
101 { "bind", MS_BIND },
102 { "rec", MS_REC },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000103#ifdef MS_UNBINDABLE
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600104 { "unbindable", MS_UNBINDABLE },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000105#endif
106#ifdef MS_PRIVATE
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600107 { "private", MS_PRIVATE },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000108#endif
109#ifdef MS_SLAVE
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600110 { "slave", MS_SLAVE },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000111#endif
112#ifdef MS_SHARED
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600113 { "shared", MS_SHARED },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000114#endif
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600115 { "sync", MS_SYNCHRONOUS },
116 { 0, 0 },
117};
118
119const char *ignored_mount_items[] = {
120 "defaults=",
121 "errors=",
Ethan Yonker93382822018-11-01 15:25:31 -0500122 "latemount",
123 "sysfs_path=",
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600124 NULL
Hashcode62bd9e02013-11-19 21:59:42 -0800125};
126
Matt Mower2416a502016-04-12 19:54:46 -0500127enum TW_FSTAB_FLAGS {
128 TWFLAG_DEFAULTS, // Retain position
129 TWFLAG_ANDSEC,
130 TWFLAG_BACKUP,
131 TWFLAG_BACKUPNAME,
132 TWFLAG_BLOCKSIZE,
133 TWFLAG_CANBEWIPED,
134 TWFLAG_CANENCRYPTBACKUP,
135 TWFLAG_DISPLAY,
136 TWFLAG_ENCRYPTABLE,
Ethan Yonkere9afc3d2018-08-30 15:16:27 -0500137 TWFLAG_FILEENCRYPTION,
Matt Mower2416a502016-04-12 19:54:46 -0500138 TWFLAG_FLASHIMG,
139 TWFLAG_FORCEENCRYPT,
140 TWFLAG_FSFLAGS,
141 TWFLAG_IGNOREBLKID,
142 TWFLAG_LENGTH,
143 TWFLAG_MOUNTTODECRYPT,
144 TWFLAG_REMOVABLE,
Matt Mower2416a502016-04-12 19:54:46 -0500145 TWFLAG_SETTINGSSTORAGE,
146 TWFLAG_STORAGE,
147 TWFLAG_STORAGENAME,
148 TWFLAG_SUBPARTITIONOF,
149 TWFLAG_SYMLINK,
150 TWFLAG_USERDATAENCRYPTBACKUP,
151 TWFLAG_USERMRF,
152 TWFLAG_WIPEDURINGFACTORYRESET,
153 TWFLAG_WIPEINGUI,
Ethan Yonker1b190162016-12-05 15:25:19 -0600154 TWFLAG_SLOTSELECT,
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600155 TWFLAG_WAIT,
156 TWFLAG_VERIFY,
157 TWFLAG_CHECK,
158 TWFLAG_ALTDEVICE,
159 TWFLAG_NOTRIM,
160 TWFLAG_VOLDMANAGED,
161 TWFLAG_FORMATTABLE,
162 TWFLAG_RESIZE,
Ethan Yonker93382822018-11-01 15:25:31 -0500163 TWFLAG_KEYDIRECTORY,
Peter Cai28ea7b02019-05-24 11:38:54 +0800164 TWFLAG_WRAPPEDKEY,
Matt Mower2416a502016-04-12 19:54:46 -0500165};
166
167/* Flags without a trailing '=' are considered dual format flags and can be
168 * written as either 'flagname' or 'flagname=', where the character following
169 * the '=' is Y,y,1 for true and false otherwise.
170 */
171const struct flag_list tw_flags[] = {
172 { "andsec", TWFLAG_ANDSEC },
173 { "backup", TWFLAG_BACKUP },
174 { "backupname=", TWFLAG_BACKUPNAME },
175 { "blocksize=", TWFLAG_BLOCKSIZE },
176 { "canbewiped", TWFLAG_CANBEWIPED },
177 { "canencryptbackup", TWFLAG_CANENCRYPTBACKUP },
178 { "defaults", TWFLAG_DEFAULTS },
179 { "display=", TWFLAG_DISPLAY },
180 { "encryptable=", TWFLAG_ENCRYPTABLE },
Ethan Yonkere9afc3d2018-08-30 15:16:27 -0500181 { "fileencryption=", TWFLAG_FILEENCRYPTION },
Matt Mower2416a502016-04-12 19:54:46 -0500182 { "flashimg", TWFLAG_FLASHIMG },
183 { "forceencrypt=", TWFLAG_FORCEENCRYPT },
184 { "fsflags=", TWFLAG_FSFLAGS },
185 { "ignoreblkid", TWFLAG_IGNOREBLKID },
186 { "length=", TWFLAG_LENGTH },
187 { "mounttodecrypt", TWFLAG_MOUNTTODECRYPT },
188 { "removable", TWFLAG_REMOVABLE },
Matt Mower2416a502016-04-12 19:54:46 -0500189 { "settingsstorage", TWFLAG_SETTINGSSTORAGE },
190 { "storage", TWFLAG_STORAGE },
191 { "storagename=", TWFLAG_STORAGENAME },
192 { "subpartitionof=", TWFLAG_SUBPARTITIONOF },
193 { "symlink=", TWFLAG_SYMLINK },
194 { "userdataencryptbackup", TWFLAG_USERDATAENCRYPTBACKUP },
195 { "usermrf", TWFLAG_USERMRF },
196 { "wipeduringfactoryreset", TWFLAG_WIPEDURINGFACTORYRESET },
197 { "wipeingui", TWFLAG_WIPEINGUI },
Ethan Yonker1b190162016-12-05 15:25:19 -0600198 { "slotselect", TWFLAG_SLOTSELECT },
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600199 { "wait", TWFLAG_WAIT },
200 { "verify", TWFLAG_VERIFY },
201 { "check", TWFLAG_CHECK },
202 { "altdevice", TWFLAG_ALTDEVICE },
203 { "notrim", TWFLAG_NOTRIM },
204 { "voldmanaged=", TWFLAG_VOLDMANAGED },
205 { "formattable", TWFLAG_FORMATTABLE },
206 { "resize", TWFLAG_RESIZE },
Ethan Yonker93382822018-11-01 15:25:31 -0500207 { "keydirectory=", TWFLAG_KEYDIRECTORY },
Peter Cai28ea7b02019-05-24 11:38:54 +0800208 { "wrappedkey", TWFLAG_WRAPPEDKEY },
Matt Mower2416a502016-04-12 19:54:46 -0500209 { 0, 0 },
210};
211
that9e0593e2014-10-08 00:01:24 +0200212TWPartition::TWPartition() {
Dees_Troy51a0e822012-09-05 15:24:24 -0400213 Can_Be_Mounted = false;
214 Can_Be_Wiped = false;
Dees_Troya13d74f2013-03-24 08:54:55 -0500215 Can_Be_Backed_Up = false;
Vojtech Bocek1dc30982013-08-30 21:49:30 +0200216 Use_Rm_Rf = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400217 Wipe_During_Factory_Reset = false;
218 Wipe_Available_in_GUI = false;
219 Is_SubPartition = false;
Dees_Troy2691f9d2012-09-24 11:15:49 -0400220 Has_SubPartition = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400221 SubPartition_Of = "";
222 Symlink_Path = "";
223 Symlink_Mount_Point = "";
224 Mount_Point = "";
Dees_Troye58d5262012-09-21 12:27:57 -0400225 Backup_Path = "";
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600226 Wildcard_Block_Device = false;
227 Sysfs_Entry = "";
Dees_Troy38bd7602012-09-14 13:33:53 -0400228 Actual_Block_Device = "";
229 Primary_Block_Device = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400230 Alternate_Block_Device = "";
231 Removable = false;
232 Is_Present = false;
233 Length = 0;
234 Size = 0;
235 Used = 0;
236 Free = 0;
237 Backup_Size = 0;
238 Can_Be_Encrypted = false;
239 Is_Encrypted = false;
240 Is_Decrypted = false;
Ethan Yonkerbd7492d2016-12-07 13:55:01 -0600241 Is_FBE = false;
Ethan Yonker253368a2014-11-25 15:00:52 -0600242 Mount_To_Decrypt = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400243 Decrypted_Block_Device = "";
244 Display_Name = "";
Dees_Troya13d74f2013-03-24 08:54:55 -0500245 Backup_Display_Name = "";
246 Storage_Name = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400247 Backup_Name = "";
Dees_Troy63c8df72012-09-10 14:02:05 -0400248 Backup_FileName = "";
Dees_Troy38bd7602012-09-14 13:33:53 -0400249 MTD_Name = "";
bigbiffce8f83c2015-12-12 18:30:21 -0500250 Backup_Method = BM_NONE;
Dees_Troy83bd4832013-05-04 12:39:56 +0000251 Can_Encrypt_Backup = false;
252 Use_Userdata_Encryption = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400253 Has_Data_Media = false;
Dees_Troye58d5262012-09-21 12:27:57 -0400254 Has_Android_Secure = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400255 Is_Storage = false;
Dees_Troya13d74f2013-03-24 08:54:55 -0500256 Is_Settings_Storage = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400257 Storage_Path = "";
258 Current_File_System = "";
259 Fstab_File_System = "";
Hashcode62bd9e02013-11-19 21:59:42 -0800260 Mount_Flags = 0;
261 Mount_Options = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400262 Format_Block_Size = 0;
Dees_Troy68cab492012-12-12 19:29:35 +0000263 Ignore_Blkid = false;
dianlujitao4879b372018-12-03 18:45:47 +0800264 Crypto_Key_Location = "";
Ethan Yonker726a0202014-12-16 20:01:38 -0600265 MTP_Storage_ID = 0;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600266 Can_Flash_Img = false;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500267 Mount_Read_Only = false;
Ethan Yonker66a19492015-12-10 10:19:45 -0600268 Is_Adopted_Storage = false;
269 Adopted_GUID = "";
Ethan Yonker1b190162016-12-05 15:25:19 -0600270 SlotSelect = false;
Ethan Yonker93382822018-11-01 15:25:31 -0500271 Key_Directory = "";
bigbiffee7b7ff2020-03-23 15:08:27 -0400272 Is_Super = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400273}
274
275TWPartition::~TWPartition(void) {
276 // Do nothing
277}
278
Chaosmasterf6e42ce2020-01-27 00:17:04 +0100279bool 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 -0500280 char full_line[MAX_FSTAB_LINE_LENGTH];
Matt Mower2416a502016-04-12 19:54:46 -0500281 char twflags[MAX_FSTAB_LINE_LENGTH] = "";
Dees_Troy5bf43922012-09-07 16:07:55 -0400282 char* ptr;
Matt Mower2b2dd152016-04-26 11:24:08 -0500283 int line_len = strlen(fstab_line), index = 0, item_index = 0;
Dees_Troya13d74f2013-03-24 08:54:55 -0500284 bool skip = false;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600285 int fstab_version = 1, mount_point_index = 0, fs_index = 1, block_device_index = 2;
286 TWPartition *additional_entry = NULL;
287 std::map<string, Flags_Map>::iterator it;
Dees_Troy5bf43922012-09-07 16:07:55 -0400288
Matt Mower2b2dd152016-04-26 11:24:08 -0500289 strlcpy(full_line, fstab_line, sizeof(full_line));
bigbiffee7b7ff2020-03-23 15:08:27 -0400290
Dees_Troy51127312012-09-08 13:08:49 -0400291 for (index = 0; index < line_len; index++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500292 if (full_line[index] == 34)
293 skip = !skip;
294 if (!skip && full_line[index] <= 32)
Dees_Troy5bf43922012-09-07 16:07:55 -0400295 full_line[index] = '\0';
Dees_Troy5bf43922012-09-07 16:07:55 -0400296 }
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600297 if (line_len < 10)
298 return false; // There can't possibly be a valid fstab line that is less than 10 chars
299 if (strncmp(fstab_line, "/dev/", strlen("/dev/")) == 0 || strncmp(fstab_line, "/devices/", strlen("/devices/")) == 0) {
300 fstab_version = 2;
301 block_device_index = 0;
302 mount_point_index = 1;
303 fs_index = 2;
304 }
305
bigbiffee7b7ff2020-03-23 15:08:27 -0400306 Is_Super = PartitionManager.Is_Super_Partition(fstab_line);
307 if (Is_Super) {
308 block_device_index = 0;
309 fstab_version = 2;
310 mount_point_index = 1;
311 fs_index = 2;
312 }
313
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600314 index = 0;
Dees_Troy5bf43922012-09-07 16:07:55 -0400315 while (index < line_len) {
316 while (index < line_len && full_line[index] == '\0')
317 index++;
318 if (index >= line_len)
319 continue;
320 ptr = full_line + index;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600321 if (item_index == mount_point_index) {
322 Mount_Point = ptr;
bigbiffee7b7ff2020-03-23 15:08:27 -0400323 if (fstab_version == 2 && Is_Super == false) {
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600324 additional_entry = PartitionManager.Find_Partition_By_Path(Mount_Point);
Chaosmasterf6e42ce2020-01-27 00:17:04 +0100325 if (!Sar_Detect && additional_entry) {
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600326 LOGINFO("Found an additional entry for '%s'\n", Mount_Point.c_str());
327 }
328 }
Chaosmasterf6e42ce2020-01-27 00:17:04 +0100329 if(!Sar_Detect)
330 LOGINFO("Processing '%s'\n", Mount_Point.c_str());
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600331 Backup_Path = Mount_Point;
332 Storage_Path = Mount_Point;
333 Display_Name = ptr + 1;
334 Backup_Display_Name = Display_Name;
335 Storage_Name = Display_Name;
336 item_index++;
337 } else if (item_index == fs_index) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400338 // File System
339 Fstab_File_System = ptr;
340 Current_File_System = ptr;
341 item_index++;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600342 } else if (item_index == block_device_index) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400343 // Primary Block Device
Dees_Troy38bd7602012-09-14 13:33:53 -0400344 if (Fstab_File_System == "mtd" || Fstab_File_System == "yaffs2") {
Dees_Troy094207a2012-09-26 12:00:39 -0400345 MTD_Name = ptr;
346 Find_MTD_Block_Device(MTD_Name);
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400347 } else if (Fstab_File_System == "bml") {
348 if (Mount_Point == "/boot")
349 MTD_Name = "boot";
350 else if (Mount_Point == "/recovery")
351 MTD_Name = "recovery";
352 Primary_Block_Device = ptr;
353 if (*ptr != '/')
Dees_Troy2673cec2013-04-02 20:22:16 +0000354 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 -0400355 } else if (*ptr != '/') {
bigbiffee7b7ff2020-03-23 15:08:27 -0400356 if (!Is_Super) {
357 if (Display_Error)
358 LOGERR("Invalid block device '%s' in fstab line '%s'", ptr, fstab_line);
359 else
360 LOGINFO("Invalid block device '%s' in fstab line '%s'", ptr, fstab_line);
361 return false;
362 }
Dees_Troy38bd7602012-09-14 13:33:53 -0400363 } else {
364 Primary_Block_Device = ptr;
365 Find_Real_Block_Device(Primary_Block_Device, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400366 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400367 item_index++;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600368 } else if (item_index > 2) {
369 if (fstab_version == 2) {
370 if (item_index == 3) {
371 Process_FS_Flags(ptr);
372 if (additional_entry) {
373 additional_entry->Save_FS_Flags(Fstab_File_System, Mount_Flags, Mount_Options);
374 return false; // We save the extra fs flags in the other partition entry and by returning false, this entry will be deleted
375 }
376 } else {
377 strlcpy(twflags, ptr, sizeof(twflags));
378 }
379 item_index++;
380 } else if (*ptr == '/') { // v2 fstab does not allow alternate block devices
Dees_Troy5bf43922012-09-07 16:07:55 -0400381 // Alternate Block Device
382 Alternate_Block_Device = ptr;
383 Find_Real_Block_Device(Alternate_Block_Device, Display_Error);
384 } else if (strlen(ptr) > 7 && strncmp(ptr, "length=", 7) == 0) {
385 // Partition length
386 ptr += 7;
387 Length = atoi(ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400388 } else if (strlen(ptr) > 6 && strncmp(ptr, "flags=", 6) == 0) {
389 // Custom flags, save for later so that new values aren't overwritten by defaults
390 ptr += 6;
Matt Mower2416a502016-04-12 19:54:46 -0500391 strlcpy(twflags, ptr, sizeof(twflags));
Dees_Troy38bd7602012-09-14 13:33:53 -0400392 } else if (strlen(ptr) == 4 && (strncmp(ptr, "NULL", 4) == 0 || strncmp(ptr, "null", 4) == 0 || strncmp(ptr, "null", 4) == 0)) {
393 // Do nothing
Dees_Troy5bf43922012-09-07 16:07:55 -0400394 } else {
395 // Unhandled data
Matt Mower2b2dd152016-04-26 11:24:08 -0500396 LOGINFO("Unhandled fstab information '%s' in fstab line '%s'\n", ptr, fstab_line);
Dees_Troy5bf43922012-09-07 16:07:55 -0400397 }
398 }
399 while (index < line_len && full_line[index] != '\0')
400 index++;
401 }
402
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600403 // override block devices from the v2 fstab with the ones we read from the twrp.flags file in case they are different
404 if (fstab_version == 2 && twrp_flags && twrp_flags->size() > 0) {
405 it = twrp_flags->find(Mount_Point);
406 if (it != twrp_flags->end()) {
407 if (!it->second.Primary_Block_Device.empty()) {
408 Primary_Block_Device = it->second.Primary_Block_Device;
409 Find_Real_Block_Device(Primary_Block_Device, Display_Error);
410 }
411 if (!it->second.Alternate_Block_Device.empty()) {
412 Alternate_Block_Device = it->second.Alternate_Block_Device;
413 Find_Real_Block_Device(Alternate_Block_Device, Display_Error);
414 }
415 }
416 }
417
418 if (strncmp(fstab_line, "/devices/", strlen("/devices/")) == 0) {
419 Sysfs_Entry = Primary_Block_Device;
420 Primary_Block_Device = "";
421 Is_Storage = true;
422 Removable = true;
423 Wipe_Available_in_GUI = true;
424 Wildcard_Block_Device = true;
425 }
426 if (Primary_Block_Device.find("*") != string::npos)
427 Wildcard_Block_Device = true;
428
Chaosmasterf6e42ce2020-01-27 00:17:04 +0100429 if (Sar_Detect) {
430 if(Is_File_System(Fstab_File_System) && (Mount_Point == "/" || Mount_Point == "/system" || Mount_Point == "/system_root"))
431 Find_Actual_Block_Device();
432 else
433 return true;
434 } else if (Mount_Point == "auto") {
dianlujitaob76a73a2020-04-30 20:33:20 +0800435 Mount_Point = "/auto" + to_string(auto_index);
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600436 Backup_Path = Mount_Point;
437 Storage_Path = Mount_Point;
dianlujitaob76a73a2020-04-30 20:33:20 +0800438 Backup_Name = Mount_Point.substr(1);
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600439 auto_index++;
440 Setup_File_System(Display_Error);
441 Display_Name = "Storage";
442 Backup_Display_Name = Display_Name;
443 Storage_Name = Display_Name;
444 Can_Be_Backed_Up = false;
445 Wipe_Available_in_GUI = true;
446 Is_Storage = true;
447 Removable = true;
448 Wipe_Available_in_GUI = true;
449 } else if (!Is_File_System(Fstab_File_System) && !Is_Image(Fstab_File_System)) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400450 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000451 LOGERR("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400452 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000453 LOGINFO("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Matt Mower72c87ce2016-04-26 14:34:56 -0500454 return false;
Dees_Troy5bf43922012-09-07 16:07:55 -0400455 } else if (Is_File_System(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400456 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400457 Setup_File_System(Display_Error);
dianlujitaob76a73a2020-04-30 20:33:20 +0800458 Backup_Name = Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
Chaosmaster3b682732020-01-25 21:09:28 +0100459 if (Mount_Point == "/" || Mount_Point == "/system" || Mount_Point == "/system_root") {
Dees_Troy5bf43922012-09-07 16:07:55 -0400460 Display_Name = "System";
dianlujitaob76a73a2020-04-30 20:33:20 +0800461 Backup_Name = "system";
Dees_Troya13d74f2013-03-24 08:54:55 -0500462 Backup_Display_Name = Display_Name;
463 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400464 Wipe_Available_in_GUI = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500465 Can_Be_Backed_Up = true;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500466 Mount_Read_Only = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400467 } else if (Mount_Point == "/data") {
468 Display_Name = "Data";
Dees_Troya13d74f2013-03-24 08:54:55 -0500469 Backup_Display_Name = Display_Name;
470 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400471 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400472 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500473 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000474 Can_Encrypt_Backup = true;
475 Use_Userdata_Encryption = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400476 } else if (Mount_Point == "/cache") {
477 Display_Name = "Cache";
Dees_Troya13d74f2013-03-24 08:54:55 -0500478 Backup_Display_Name = Display_Name;
479 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400480 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400481 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500482 Can_Be_Backed_Up = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400483 } else if (Mount_Point == "/datadata") {
Dees_Troy51127312012-09-08 13:08:49 -0400484 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400485 Display_Name = "DataData";
Dees_Troya13d74f2013-03-24 08:54:55 -0500486 Backup_Display_Name = Display_Name;
487 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400488 Is_SubPartition = true;
489 SubPartition_Of = "/data";
Dees_Troy5bf43922012-09-07 16:07:55 -0400490 DataManager::SetValue(TW_HAS_DATADATA, 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500491 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000492 Can_Encrypt_Backup = true;
493 Use_Userdata_Encryption = false; // This whole partition should be encrypted
Dees_Troy5bf43922012-09-07 16:07:55 -0400494 } else if (Mount_Point == "/sd-ext") {
Dees_Troy51127312012-09-08 13:08:49 -0400495 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400496 Display_Name = "SD-Ext";
Dees_Troya13d74f2013-03-24 08:54:55 -0500497 Backup_Display_Name = Display_Name;
498 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400499 Wipe_Available_in_GUI = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400500 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500501 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000502 Can_Encrypt_Backup = true;
503 Use_Userdata_Encryption = true;
Dees_Troy2c50e182012-09-26 20:05:28 -0400504 } else if (Mount_Point == "/boot") {
505 Display_Name = "Boot";
Dees_Troya13d74f2013-03-24 08:54:55 -0500506 Backup_Display_Name = Display_Name;
Dees_Troy2c50e182012-09-26 20:05:28 -0400507 DataManager::SetValue("tw_boot_is_mountable", 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500508 Can_Be_Backed_Up = true;
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500509 } else if (Mount_Point == "/vendor") {
510 Display_Name = "Vendor";
511 Backup_Display_Name = Display_Name;
512 Storage_Name = Display_Name;
513 Mount_Read_Only = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400514 }
515#ifdef TW_EXTERNAL_STORAGE_PATH
516 if (Mount_Point == EXPAND(TW_EXTERNAL_STORAGE_PATH)) {
517 Is_Storage = true;
518 Storage_Path = EXPAND(TW_EXTERNAL_STORAGE_PATH);
Dees_Troyc51f1f92012-09-20 15:32:13 -0400519 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500520 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400521#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000522 if (Mount_Point == "/sdcard" || Mount_Point == "/external_sd" || Mount_Point == "/external_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400523 Is_Storage = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400524 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500525 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400526#endif
Dees_Troyb05ddee2013-01-28 20:24:50 +0000527 }
Dees_Troy8170a922012-09-18 15:40:25 -0400528#ifdef TW_INTERNAL_STORAGE_PATH
529 if (Mount_Point == EXPAND(TW_INTERNAL_STORAGE_PATH)) {
530 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500531 Is_Settings_Storage = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400532 Storage_Path = EXPAND(TW_INTERNAL_STORAGE_PATH);
Dees_Troya13d74f2013-03-24 08:54:55 -0500533 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400534 }
535#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000536 if (Mount_Point == "/emmc" || Mount_Point == "/internal_sd" || Mount_Point == "/internal_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400537 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500538 Is_Settings_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500539 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400540 }
541#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400542 } else if (Is_Image(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400543 Find_Actual_Block_Device();
Ethan Yonker1b190162016-12-05 15:25:19 -0600544 Setup_Image();
Dees_Troya13d74f2013-03-24 08:54:55 -0500545 if (Mount_Point == "/boot") {
546 Display_Name = "Boot";
547 Backup_Display_Name = Display_Name;
548 Can_Be_Backed_Up = true;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600549 Can_Flash_Img = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500550 } else if (Mount_Point == "/recovery") {
551 Display_Name = "Recovery";
552 Backup_Display_Name = Display_Name;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600553 Can_Flash_Img = true;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500554 } else if (Mount_Point == "/system_image") {
555 Display_Name = "System Image";
556 Backup_Display_Name = Display_Name;
HashBanged974bb2016-01-30 14:20:09 -0500557 Can_Flash_Img = true;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500558 Can_Be_Backed_Up = true;
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500559 } else if (Mount_Point == "/vendor_image") {
560 Display_Name = "Vendor Image";
561 Backup_Display_Name = Display_Name;
HashBanged974bb2016-01-30 14:20:09 -0500562 Can_Flash_Img = true;
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500563 Can_Be_Backed_Up = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500564 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400565 }
566
Matt Mower2416a502016-04-12 19:54:46 -0500567 // Process TWRP fstab flags
568 if (strlen(twflags) > 0) {
569 string Prev_Display_Name = Display_Name;
570 string Prev_Storage_Name = Storage_Name;
571 string Prev_Backup_Display_Name = Backup_Display_Name;
572 Display_Name = "";
573 Storage_Name = "";
574 Backup_Display_Name = "";
575
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600576 Process_TW_Flags(twflags, (fstab_version == 1), fstab_version);
577 Save_FS_Flags(Fstab_File_System, Mount_Flags, Mount_Options);
Matt Mower2416a502016-04-12 19:54:46 -0500578
579 bool has_display_name = !Display_Name.empty();
580 bool has_storage_name = !Storage_Name.empty();
581 bool has_backup_name = !Backup_Display_Name.empty();
582 if (!has_display_name) Display_Name = Prev_Display_Name;
583 if (!has_storage_name) Storage_Name = Prev_Storage_Name;
584 if (!has_backup_name) Backup_Display_Name = Prev_Backup_Display_Name;
585
586 if (has_display_name && !has_storage_name)
587 Storage_Name = Display_Name;
588 if (!has_display_name && has_storage_name)
589 Display_Name = Storage_Name;
590 if (has_display_name && !has_backup_name && Backup_Display_Name != "Android Secure")
591 Backup_Display_Name = Display_Name;
592 if (!has_display_name && has_backup_name)
593 Display_Name = Backup_Display_Name;
594 }
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600595
596 if (fstab_version == 2 && twrp_flags && twrp_flags->size() > 0) {
597 it = twrp_flags->find(Mount_Point);
598 if (it != twrp_flags->end()) {
599 char twrpflags[MAX_FSTAB_LINE_LENGTH] = "";
600 int skip = 0;
601 string Flags = it->second.Flags;
602 strcpy(twrpflags, Flags.c_str());
603 if (strlen(twrpflags) > strlen("flags=") && strncmp(twrpflags, "flags=", strlen("flags=")) == 0)
604 skip += strlen("flags=");
605 char* flagptr = twrpflags;
606 flagptr += skip;
607 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
608 }
609 }
nkk7198fc3992017-12-16 16:26:42 +0200610
611 if (Mount_Point == "/persist" && Can_Be_Mounted) {
612 bool mounted = Is_Mounted();
613 if (mounted || Mount(false)) {
614 // Read the backup settings file
615 DataManager::LoadPersistValues();
616 TWFunc::Fixup_Time_On_Boot("/persist/time/");
617 if (!mounted)
618 UnMount(false);
619 }
620 }
621
Chaosmasterf6e42ce2020-01-27 00:17:04 +0100622 if (Is_File_System(Fstab_File_System) && (Mount_Point == "/" || Mount_Point == "/system" || Mount_Point == "/system_root")) {
623 if (Sar_Detect) {
624 Mount_Point = "/s";
625 Mount_Read_Only = true;
626 Can_Be_Mounted = true;
627 } else {
628 Mount_Point = PartitionManager.Get_Android_Root_Path();
629 Backup_Path = Mount_Point;
630 Storage_Path = Mount_Point;
631 Make_Dir(Mount_Point, Display_Error);
632 }
bigbiffee7b7ff2020-03-23 15:08:27 -0400633 if (Is_Super) {
634 Can_Be_Backed_Up = false;
635 Can_Be_Wiped = false;
636 Wipe_Available_in_GUI = false;
637 }
Chaosmasterf6e42ce2020-01-27 00:17:04 +0100638 }
639
Dees_Troy51127312012-09-08 13:08:49 -0400640 return true;
641}
642
Matt Mower72c87ce2016-04-26 14:34:56 -0500643void TWPartition::Partition_Post_Processing(bool Display_Error) {
644 if (Mount_Point == "/data")
645 Setup_Data_Partition(Display_Error);
646 else if (Mount_Point == "/cache")
647 Setup_Cache_Partition(Display_Error);
648}
649
Ethan Yonkerbd7492d2016-12-07 13:55:01 -0600650void TWPartition::ExcludeAll(const string& path) {
651 backup_exclusions.add_absolute_dir(path);
652 wipe_exclusions.add_absolute_dir(path);
653}
654
Matt Mower72c87ce2016-04-26 14:34:56 -0500655void TWPartition::Setup_Data_Partition(bool Display_Error) {
656 if (Mount_Point != "/data")
657 return;
658
659 // Ensure /data is not mounted as tmpfs for qcom hardware decrypt
660 UnMount(false);
661
662#ifdef TW_INCLUDE_CRYPTO
663 if (datamedia)
664 Setup_Data_Media();
665 Can_Be_Encrypted = true;
666 char crypto_blkdev[255];
667 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
668 if (strcmp(crypto_blkdev, "error") != 0) {
bigbiff7ba75002020-04-11 20:47:09 -0400669 Set_FBE_Status();
Matt Mower72c87ce2016-04-26 14:34:56 -0500670 Decrypted_Block_Device = crypto_blkdev;
671 LOGINFO("Data already decrypted, new block device: '%s'\n", crypto_blkdev);
bigbiff7ba75002020-04-11 20:47:09 -0400672 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
Matt Mower72c87ce2016-04-26 14:34:56 -0500673 } else if (!Mount(false)) {
674 if (Is_Present) {
Ethan Yonker93382822018-11-01 15:25:31 -0500675 if (Key_Directory.empty()) {
bigbiffadc599e2020-05-28 19:36:30 +0000676 set_partition_data(Actual_Block_Device.c_str(), Crypto_Key_Location.c_str(), Fstab_File_System.c_str());
Ethan Yonker93382822018-11-01 15:25:31 -0500677 if (cryptfs_check_footer() == 0) {
678 Is_Encrypted = true;
679 Is_Decrypted = false;
680 Can_Be_Mounted = false;
681 Current_File_System = "emmc";
682 Setup_Image();
Ethan Yonker93382822018-11-01 15:25:31 -0500683 DataManager::SetValue(TW_CRYPTO_PWTYPE, cryptfs_get_password_type());
684 DataManager::SetValue(TW_CRYPTO_PASSWORD, "");
685 DataManager::SetValue("tw_crypto_display", "");
686 } else {
687 gui_err("mount_data_footer=Could not mount /data and unable to find crypto footer.");
688 }
689 } else {
Matt Mower72c87ce2016-04-26 14:34:56 -0500690 Is_Encrypted = true;
691 Is_Decrypted = false;
Matt Mower72c87ce2016-04-26 14:34:56 -0500692 }
Ethan Yonker93382822018-11-01 15:25:31 -0500693 } else if (Key_Directory.empty()) {
bigbiffadc599e2020-05-28 19:36:30 +0000694 LOGERR("Primary block device '%s' for mount point '%s' is not present!\n", Primary_Block_Device.c_str(), Mount_Point.c_str());
Matt Mower72c87ce2016-04-26 14:34:56 -0500695 }
696 } else {
bigbiff7ba75002020-04-11 20:47:09 -0400697 Set_FBE_Status();
mauronofrio9bf73352019-11-23 22:27:34 +0100698 if (!Decrypt_FBE_DE()) {
mauronofriofc79aa62019-11-23 23:13:04 +0100699 char wrappedvalue[PROPERTY_VALUE_MAX];
700 property_get("fbe.data.wrappedkey", wrappedvalue, "");
701 std::string wrappedkeyvalue(wrappedvalue);
702 if (wrappedkeyvalue == "true") {
703 LOGERR("Unable to decrypt FBE device\n");
704 } else {
705 LOGINFO("Trying wrapped key.\n");
706 property_set("fbe.data.wrappedkey", "true");
mauronofrio9bf73352019-11-23 22:27:34 +0100707 if (!Decrypt_FBE_DE()) {
bigbiffd58ba182020-03-23 10:02:29 -0400708 LOGERR("Unable to decrypt FBE device\n");
mauronofrio9bf73352019-11-23 22:27:34 +0100709 }
mauronofriofc79aa62019-11-23 23:13:04 +0100710 }
mauronofrio9bf73352019-11-23 22:27:34 +0100711 }
bigbiff7ba75002020-04-11 20:47:09 -0400712 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
Matt Mower72c87ce2016-04-26 14:34:56 -0500713 }
714 if (datamedia && (!Is_Encrypted || (Is_Encrypted && Is_Decrypted))) {
715 Setup_Data_Media();
bigbiff7ba75002020-04-11 20:47:09 -0400716 if (!TWFunc::Is_Mount_Wiped("/data"))
717 Recreate_Media_Folder();
Matt Mower72c87ce2016-04-26 14:34:56 -0500718 }
719#else
720 if (datamedia) {
721 Setup_Data_Media();
722 Recreate_Media_Folder();
723 }
724#endif
725}
726
bigbiff7ba75002020-04-11 20:47:09 -0400727void TWPartition::Set_FBE_Status() {
728 DataManager::SetValue(TW_IS_DECRYPTED, 1);
729 Is_Encrypted = true;
730 Is_Decrypted = true;
731 LOGINFO("Setup_Data_Partition::Key_Directory::%s\n", Key_Directory.c_str());
732 if (Key_Directory.empty()) {
733 Is_FBE = false;
734 DataManager::SetValue(TW_IS_FBE, 0);
735 } else {
736 Is_FBE = true;
737 DataManager::SetValue(TW_IS_FBE, 1);
738 }
739}
740
Ethan Yonker93382822018-11-01 15:25:31 -0500741bool TWPartition::Decrypt_FBE_DE() {
bigbiff7ba75002020-04-11 20:47:09 -0400742 if (TWFunc::Path_Exists("/data/unencrypted/key/version")) {
Captain Throwback87d73d62020-02-17 16:44:50 -0500743 DataManager::SetValue(TW_IS_FBE, 1);
Ethan Yonker93382822018-11-01 15:25:31 -0500744 LOGINFO("File Based Encryption is present\n");
745#ifdef TW_INCLUDE_FBE
bigbiffadc599e2020-05-28 19:36:30 +0000746 ExcludeAll(Mount_Point + "/convert_fbe");
747 ExcludeAll(Mount_Point + "/unencrypted");
748 //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
749 ExcludeAll(Mount_Point + "/misc/vold/user_keys");
bigbiff7ba75002020-04-11 20:47:09 -0400750 ExcludeAll(Mount_Point + "/misc/vold/volume_keys");
bigbiffadc599e2020-05-28 19:36:30 +0000751 //ExcludeAll(Mount_Point + "/system_ce");
752 //ExcludeAll(Mount_Point + "/system_de");
753 //ExcludeAll(Mount_Point + "/misc_ce");
754 //ExcludeAll(Mount_Point + "/misc_de");
755 ExcludeAll(Mount_Point + "/system/gatekeeper.password.key");
756 ExcludeAll(Mount_Point + "/system/gatekeeper.pattern.key");
757 ExcludeAll(Mount_Point + "/system/locksettings.db");
758 //ExcludeAll(Mount_Point + "/system/locksettings.db-shm"); // don't seem to need this one, but the other 2 are needed
759 ExcludeAll(Mount_Point + "/system/locksettings.db-wal");
760 //ExcludeAll(Mount_Point + "/user_de");
761 //ExcludeAll(Mount_Point + "/misc/profiles/cur/0"); // might be important later
762 ExcludeAll(Mount_Point + "/misc/gatekeeper");
763 ExcludeAll(Mount_Point + "/misc/keystore");
764 ExcludeAll(Mount_Point + "/drm/kek.dat");
765 ExcludeAll(Mount_Point + "/system_de/0/spblob"); // contains data needed to decrypt pixel 2
bigbiff7ba75002020-04-11 20:47:09 -0400766 ExcludeAll(Mount_Point + "/per_boot"); // removed each boot by init
767
bigbiffadc599e2020-05-28 19:36:30 +0000768 int retry_count = 3;
769 while (!Decrypt_DE() && --retry_count)
770 usleep(2000);
771 if (retry_count > 0) {
772 property_set("ro.crypto.state", "encrypted");
773 Is_Encrypted = true;
774 Is_Decrypted = false;
775 Is_FBE = true;
776 DataManager::SetValue(TW_IS_FBE, 1);
bigbiffadc599e2020-05-28 19:36:30 +0000777 string filename;
778 int pwd_type = Get_Password_Type(0, filename);
779 if (pwd_type < 0) {
780 LOGERR("This TWRP does not have synthetic password decrypt support\n");
781 pwd_type = 0; // default password
782 }
783 DataManager::SetValue(TW_CRYPTO_PWTYPE, pwd_type);
784 DataManager::SetValue(TW_CRYPTO_PASSWORD, "");
785 DataManager::SetValue("tw_crypto_display", "");
786 return true;
Ethan Yonker93382822018-11-01 15:25:31 -0500787 }
788#else
789 LOGERR("FBE found but FBE support not present in TWRP\n");
790#endif
791 }
Captain Throwback87d73d62020-02-17 16:44:50 -0500792 DataManager::SetValue(TW_IS_FBE, 0);
Ethan Yonker93382822018-11-01 15:25:31 -0500793 return false;
794}
795
Matt Mower72c87ce2016-04-26 14:34:56 -0500796void TWPartition::Setup_Cache_Partition(bool Display_Error __unused) {
797 if (Mount_Point != "/cache")
798 return;
799
800 if (!Mount(true))
801 return;
802
803 if (!TWFunc::Path_Exists("/cache/recovery/.")) {
804 LOGINFO("Recreating /cache/recovery folder\n");
805 if (mkdir("/cache/recovery", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0)
806 LOGERR("Could not create /cache/recovery\n");
807 }
808}
809
Matt Mower4ab42b12016-04-21 13:52:18 -0500810void TWPartition::Process_FS_Flags(const char *str) {
811 char *options = strdup(str);
812 char *ptr, *savep;
Hashcode62bd9e02013-11-19 21:59:42 -0800813
Matt Mower4ab42b12016-04-21 13:52:18 -0500814 Mount_Options = "";
Hashcode62bd9e02013-11-19 21:59:42 -0800815
Matt Mower4ab42b12016-04-21 13:52:18 -0500816 // Avoid issues with potentially nested strtok by using strtok_r
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600817 for (ptr = strtok_r(options, ",", &savep); ptr; ptr = strtok_r(NULL, ",", &savep)) {
818 char *equals = strstr(ptr, "=");
819 size_t name_len;
Matt Mower4ab42b12016-04-21 13:52:18 -0500820
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600821 if (!equals)
822 name_len = strlen(ptr);
823 else
824 name_len = equals - ptr;
825
826 // There are some flags that we want to ignore in TWRP
827 bool found_match = false;
828 for (const char** ignored_mount_item = ignored_mount_items; *ignored_mount_item; ignored_mount_item++) {
829 if (strncmp(ptr, *ignored_mount_item, name_len) == 0) {
830 found_match = true;
Hashcode62bd9e02013-11-19 21:59:42 -0800831 break;
832 }
833 }
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600834 if (found_match)
835 continue;
Hashcode62bd9e02013-11-19 21:59:42 -0800836
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600837 // mount_flags are never postfixed by '='
838 if (!equals) {
839 const struct flag_list* mount_flag = mount_flags;
840 for (; mount_flag->name; mount_flag++) {
841 if (strcmp(ptr, mount_flag->name) == 0) {
842 if (mount_flag->flag == MS_RDONLY)
843 Mount_Read_Only = true;
844 else
845 Mount_Flags |= (unsigned)mount_flag->flag;
846 found_match = true;
847 break;
848 }
849 }
850 if (found_match)
851 continue;
Matt Mower4ab42b12016-04-21 13:52:18 -0500852 }
853
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600854 // If we aren't ignoring this flag and it's not a mount flag, then it must be a mount option
855 if (!Mount_Options.empty())
856 Mount_Options += ",";
857 Mount_Options += ptr;
Matt Mower4ab42b12016-04-21 13:52:18 -0500858 }
859 free(options);
Hashcode62bd9e02013-11-19 21:59:42 -0800860}
861
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600862void TWPartition::Save_FS_Flags(const string& local_File_System, int local_Mount_Flags, const string& local_Mount_Options) {
863 partition_fs_flags_struct flags;
864 flags.File_System = local_File_System;
865 flags.Mount_Flags = local_Mount_Flags;
866 flags.Mount_Options = local_Mount_Options;
867 fs_flags.push_back(flags);
868}
869
Matt Mower2416a502016-04-12 19:54:46 -0500870void TWPartition::Apply_TW_Flag(const unsigned flag, const char* str, const bool val) {
871 switch (flag) {
872 case TWFLAG_ANDSEC:
873 Has_Android_Secure = val;
874 break;
875 case TWFLAG_BACKUP:
876 Can_Be_Backed_Up = val;
877 break;
878 case TWFLAG_BACKUPNAME:
879 Backup_Display_Name = str;
880 break;
881 case TWFLAG_BLOCKSIZE:
882 Format_Block_Size = (unsigned long)(atol(str));
883 break;
884 case TWFLAG_CANBEWIPED:
885 Can_Be_Wiped = val;
886 break;
887 case TWFLAG_CANENCRYPTBACKUP:
888 Can_Encrypt_Backup = val;
889 break;
890 case TWFLAG_DEFAULTS:
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600891 case TWFLAG_WAIT:
892 case TWFLAG_VERIFY:
893 case TWFLAG_CHECK:
894 case TWFLAG_NOTRIM:
895 case TWFLAG_VOLDMANAGED:
896 case TWFLAG_RESIZE:
Matt Mower2416a502016-04-12 19:54:46 -0500897 // Do nothing
898 break;
899 case TWFLAG_DISPLAY:
900 Display_Name = str;
901 break;
902 case TWFLAG_ENCRYPTABLE:
903 case TWFLAG_FORCEENCRYPT:
904 Crypto_Key_Location = str;
905 break;
Ethan Yonkere9afc3d2018-08-30 15:16:27 -0500906 case TWFLAG_FILEENCRYPTION:
907 // This flag isn't used by TWRP but is needed in 9.0 FBE decrypt
908 // fileencryption=ice:aes-256-heh
909 {
910 std::string FBE = str;
Ethan Yonkere9afc3d2018-08-30 15:16:27 -0500911 size_t colon_loc = FBE.find(":");
912 if (colon_loc == std::string::npos) {
Ethan Yonker93382822018-11-01 15:25:31 -0500913 property_set("fbe.contents", FBE.c_str());
914 property_set("fbe.filenames", "");
915 LOGINFO("FBE contents '%s', filenames ''\n", FBE.c_str());
Ethan Yonkere9afc3d2018-08-30 15:16:27 -0500916 break;
917 }
Ethan Yonker93382822018-11-01 15:25:31 -0500918 std::string FBE_contents, FBE_filenames;
Ethan Yonkere9afc3d2018-08-30 15:16:27 -0500919 FBE_contents = FBE.substr(0, colon_loc);
920 FBE_filenames = FBE.substr(colon_loc + 1);
921 property_set("fbe.contents", FBE_contents.c_str());
922 property_set("fbe.filenames", FBE_filenames.c_str());
923 LOGINFO("FBE contents '%s', filenames '%s'\n", FBE_contents.c_str(), FBE_filenames.c_str());
924 }
925 break;
Peter Cai28ea7b02019-05-24 11:38:54 +0800926 case TWFLAG_WRAPPEDKEY:
927 // Set fbe.data.wrappedkey to true
928 {
929 property_set("fbe.data.wrappedkey", "true");
930 LOGINFO("FBE wrapped key enabled\n");
931 }
932 break;
Matt Mower2416a502016-04-12 19:54:46 -0500933 case TWFLAG_FLASHIMG:
934 Can_Flash_Img = val;
935 break;
936 case TWFLAG_FSFLAGS:
Matt Mower4ab42b12016-04-21 13:52:18 -0500937 Process_FS_Flags(str);
Matt Mower2416a502016-04-12 19:54:46 -0500938 break;
939 case TWFLAG_IGNOREBLKID:
940 Ignore_Blkid = val;
941 break;
942 case TWFLAG_LENGTH:
943 Length = atoi(str);
944 break;
945 case TWFLAG_MOUNTTODECRYPT:
946 Mount_To_Decrypt = val;
947 break;
948 case TWFLAG_REMOVABLE:
949 Removable = val;
950 break;
Matt Mower2416a502016-04-12 19:54:46 -0500951 case TWFLAG_SETTINGSSTORAGE:
952 Is_Settings_Storage = val;
953 if (Is_Settings_Storage)
954 Is_Storage = true;
955 break;
956 case TWFLAG_STORAGE:
957 Is_Storage = val;
958 break;
959 case TWFLAG_STORAGENAME:
960 Storage_Name = str;
961 break;
962 case TWFLAG_SUBPARTITIONOF:
963 Is_SubPartition = true;
964 SubPartition_Of = str;
965 break;
966 case TWFLAG_SYMLINK:
967 Symlink_Path = str;
968 break;
969 case TWFLAG_USERDATAENCRYPTBACKUP:
970 Use_Userdata_Encryption = val;
971 if (Use_Userdata_Encryption)
972 Can_Encrypt_Backup = true;
973 break;
974 case TWFLAG_USERMRF:
975 Use_Rm_Rf = val;
976 break;
977 case TWFLAG_WIPEDURINGFACTORYRESET:
978 Wipe_During_Factory_Reset = val;
979 if (Wipe_During_Factory_Reset) {
980 Can_Be_Wiped = true;
981 Wipe_Available_in_GUI = true;
982 }
983 break;
984 case TWFLAG_WIPEINGUI:
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600985 case TWFLAG_FORMATTABLE:
Matt Mower2416a502016-04-12 19:54:46 -0500986 Wipe_Available_in_GUI = val;
987 if (Wipe_Available_in_GUI)
988 Can_Be_Wiped = true;
989 break;
Ethan Yonker1b190162016-12-05 15:25:19 -0600990 case TWFLAG_SLOTSELECT:
991 SlotSelect = true;
992 break;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600993 case TWFLAG_ALTDEVICE:
994 Alternate_Block_Device = str;
995 break;
Ethan Yonker93382822018-11-01 15:25:31 -0500996 case TWFLAG_KEYDIRECTORY:
997 Key_Directory = str;
bigbiffd58ba182020-03-23 10:02:29 -0400998 break;
Matt Mower2416a502016-04-12 19:54:46 -0500999 default:
1000 // Should not get here
1001 LOGINFO("Flag identified for processing, but later unmatched: %i\n", flag);
1002 break;
1003 }
1004}
Dees_Troy51127312012-09-08 13:08:49 -04001005
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06001006void TWPartition::Process_TW_Flags(char *flags, bool Display_Error, int fstab_ver) {
Matt Mower2416a502016-04-12 19:54:46 -05001007 char separator[2] = {'\n', 0};
1008 char *ptr, *savep;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06001009 char source_separator = ';';
1010
1011 if (fstab_ver == 2)
1012 source_separator = ',';
Matt Mower2416a502016-04-12 19:54:46 -05001013
1014 // Semicolons within double-quotes are not forbidden, so replace
1015 // only the semicolons intended as separators with '\n' for strtok
1016 for (unsigned i = 0, skip = 0; i < strlen(flags); i++) {
1017 if (flags[i] == '\"')
Dees_Troya13d74f2013-03-24 08:54:55 -05001018 skip = !skip;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06001019 if (!skip && flags[i] == source_separator)
Matt Mower2416a502016-04-12 19:54:46 -05001020 flags[i] = separator[0];
Dees_Troy51127312012-09-08 13:08:49 -04001021 }
1022
Matt Mower2416a502016-04-12 19:54:46 -05001023 // Avoid issues with potentially nested strtok by using strtok_r
1024 ptr = strtok_r(flags, separator, &savep);
1025 while (ptr) {
1026 int ptr_len = strlen(ptr);
1027 const struct flag_list* tw_flag = tw_flags;
Hashcode62bd9e02013-11-19 21:59:42 -08001028
Matt Mower2416a502016-04-12 19:54:46 -05001029 for (; tw_flag->name; tw_flag++) {
1030 int flag_len = strlen(tw_flag->name);
1031
1032 if (strncmp(ptr, tw_flag->name, flag_len) == 0) {
1033 bool flag_val = false;
1034
1035 if (ptr_len > flag_len && (tw_flag->name)[flag_len-1] != '='
1036 && ptr[flag_len] != '=') {
1037 // Handle flags with same starting string
1038 // (e.g. backup and backupname)
1039 continue;
1040 } else if (ptr_len > flag_len && ptr[flag_len] == '=') {
1041 // Handle flags with dual format: Part 1
1042 // (e.g. backup and backup=y. backup=y handled here)
1043 ptr += flag_len + 1;
1044 TWFunc::Strip_Quotes(ptr);
1045 // Skip flags with empty argument
1046 // (e.g. backup=)
1047 if (strlen(ptr) == 0) {
1048 LOGINFO("Flag missing argument or should not include '=': %s=\n", tw_flag->name);
1049 break;
1050 }
1051 flag_val = strchr("yY1", *ptr) != NULL;
1052 } else if (ptr_len == flag_len
1053 && (tw_flag->name)[flag_len-1] == '=') {
1054 // Skip flags missing argument after =
1055 // (e.g. backupname=)
1056 LOGINFO("Flag missing argument: %s\n", tw_flag->name);
1057 break;
1058 } else if (ptr_len > flag_len
1059 && (tw_flag->name)[flag_len-1] == '=') {
1060 // Handle arguments to flags
1061 // (e.g. backupname="My Stuff")
1062 ptr += flag_len;
1063 TWFunc::Strip_Quotes(ptr);
1064 // Skip flags with empty argument
1065 // (e.g. backupname="")
1066 if (strlen(ptr) == 0) {
1067 LOGINFO("Flag missing argument: %s\n", tw_flag->name);
1068 break;
1069 }
1070 } else if (ptr_len == flag_len) {
1071 // Handle flags with dual format: Part 2
1072 // (e.g. backup and backup=y. backup handled here)
1073 flag_val = true;
Ethan Yonker96af84a2015-01-05 14:58:36 -06001074 } else {
Matt Mower2416a502016-04-12 19:54:46 -05001075 LOGINFO("Flag matched, but could not be processed: %s\n", ptr);
1076 break;
Ethan Yonker96af84a2015-01-05 14:58:36 -06001077 }
Matt Mower2416a502016-04-12 19:54:46 -05001078
1079 Apply_TW_Flag(tw_flag->flag, ptr, flag_val);
1080 break;
Ethan Yonker96af84a2015-01-05 14:58:36 -06001081 }
Matt Mower2416a502016-04-12 19:54:46 -05001082 }
1083 if (tw_flag->name == 0) {
Dees_Troy51127312012-09-08 13:08:49 -04001084 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001085 LOGERR("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -04001086 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001087 LOGINFO("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -04001088 }
Matt Mower2416a502016-04-12 19:54:46 -05001089 ptr = strtok_r(NULL, separator, &savep);
Dees_Troy51127312012-09-08 13:08:49 -04001090 }
Dees_Troy51a0e822012-09-05 15:24:24 -04001091}
1092
Dees_Troy5bf43922012-09-07 16:07:55 -04001093bool TWPartition::Is_File_System(string File_System) {
1094 if (File_System == "ext2" ||
Dees_Troy63c8df72012-09-10 14:02:05 -04001095 File_System == "ext3" ||
Dees_Troy5bf43922012-09-07 16:07:55 -04001096 File_System == "ext4" ||
1097 File_System == "vfat" ||
1098 File_System == "ntfs" ||
1099 File_System == "yaffs2" ||
bigbiff bigbiff3e146522012-11-14 14:32:59 -05001100 File_System == "exfat" ||
Dees_Troye5017042013-08-29 16:38:55 +00001101 File_System == "f2fs" ||
Erfan Abdib859e912019-02-05 18:18:46 +02001102 File_System == "squashfs" ||
Dees_Troy5bf43922012-09-07 16:07:55 -04001103 File_System == "auto")
1104 return true;
1105 else
1106 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001107}
1108
Dees_Troy5bf43922012-09-07 16:07:55 -04001109bool TWPartition::Is_Image(string File_System) {
Dees_Troy5fcd8f92012-10-16 12:22:05 -04001110 if (File_System == "emmc" || File_System == "mtd" || File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -04001111 return true;
1112 else
1113 return false;
1114}
1115
Dees_Troy51127312012-09-08 13:08:49 -04001116bool TWPartition::Make_Dir(string Path, bool Display_Error) {
Ethan Yonker93ac7a02016-11-07 22:05:58 -06001117 if (TWFunc::Get_D_Type_From_Stat(Path) != S_IFDIR)
1118 unlink(Path.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001119 if (!TWFunc::Path_Exists(Path)) {
Dees_Troy51127312012-09-08 13:08:49 -04001120 if (mkdir(Path.c_str(), 0777) == -1) {
1121 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001122 gui_msg(Msg(msg::kError, "create_folder_strerr=Can not create '{1}' folder ({2}).")(Path)(strerror(errno)));
Dees_Troy51127312012-09-08 13:08:49 -04001123 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001124 LOGINFO("Can not create '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -04001125 return false;
1126 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001127 LOGINFO("Created '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -04001128 return true;
1129 }
1130 }
1131 return true;
1132}
1133
Dees_Troy5bf43922012-09-07 16:07:55 -04001134void TWPartition::Setup_File_System(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001135 Can_Be_Mounted = true;
1136 Can_Be_Wiped = true;
1137
Dees_Troy5bf43922012-09-07 16:07:55 -04001138 // Make the mount point folder if it doesn't exist
Dees_Troy51127312012-09-08 13:08:49 -04001139 Make_Dir(Mount_Point, Display_Error);
bigbiffce8f83c2015-12-12 18:30:21 -05001140 Backup_Method = BM_FILES;
Dees_Troy5bf43922012-09-07 16:07:55 -04001141}
1142
Ethan Yonker1b190162016-12-05 15:25:19 -06001143void TWPartition::Setup_Image() {
Dees_Troy5bf43922012-09-07 16:07:55 -04001144 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
1145 Backup_Name = Display_Name;
Gary Peck82599a82012-11-21 16:23:12 -08001146 if (Current_File_System == "emmc")
bigbiffce8f83c2015-12-12 18:30:21 -05001147 Backup_Method = BM_DD;
Gary Peck82599a82012-11-21 16:23:12 -08001148 else if (Current_File_System == "mtd" || Current_File_System == "bml")
bigbiffce8f83c2015-12-12 18:30:21 -05001149 Backup_Method = BM_FLASH_UTILS;
Dees_Troy5bf43922012-09-07 16:07:55 -04001150 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001151 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 -04001152}
1153
Dees_Troye58d5262012-09-21 12:27:57 -04001154void TWPartition::Setup_AndSec(void) {
Dees_Troya13d74f2013-03-24 08:54:55 -05001155 Backup_Display_Name = "Android Secure";
Dees_Troye58d5262012-09-21 12:27:57 -04001156 Backup_Name = "and-sec";
Dees_Troya13d74f2013-03-24 08:54:55 -05001157 Can_Be_Backed_Up = true;
Dees_Troye58d5262012-09-21 12:27:57 -04001158 Has_Android_Secure = true;
1159 Symlink_Path = Mount_Point + "/.android_secure";
1160 Symlink_Mount_Point = "/and-sec";
1161 Backup_Path = Symlink_Mount_Point;
1162 Make_Dir("/and-sec", true);
1163 Recreate_AndSec_Folder();
Kjell Braden3126a112016-06-19 16:58:15 +00001164 Mount_Storage_Retry(true);
Dees_Troye58d5262012-09-21 12:27:57 -04001165}
1166
that9e0593e2014-10-08 00:01:24 +02001167void TWPartition::Setup_Data_Media() {
Ethan Yonker6277c792014-09-15 14:54:30 -05001168 LOGINFO("Setting up '%s' as data/media emulated storage.\n", Mount_Point.c_str());
Ethan Yonker66a19492015-12-10 10:19:45 -06001169 if (Storage_Name.empty() || Storage_Name == "Data")
1170 Storage_Name = "Internal Storage";
Ethan Yonker6277c792014-09-15 14:54:30 -05001171 Has_Data_Media = true;
1172 Is_Storage = true;
Ethan Yonker66a19492015-12-10 10:19:45 -06001173 Storage_Path = Mount_Point + "/media";
Ethan Yonker6277c792014-09-15 14:54:30 -05001174 Symlink_Path = Storage_Path;
Ethan Yonker66a19492015-12-10 10:19:45 -06001175 if (Mount_Point == "/data") {
1176 Is_Settings_Storage = true;
1177 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
1178 Make_Dir("/emmc", false);
1179 Symlink_Mount_Point = "/emmc";
1180 } else {
1181 Make_Dir("/sdcard", false);
1182 Symlink_Mount_Point = "/sdcard";
1183 }
1184 if (Mount(false) && TWFunc::Path_Exists(Mount_Point + "/media/0")) {
1185 Storage_Path = Mount_Point + "/media/0";
1186 Symlink_Path = Storage_Path;
1187 DataManager::SetValue(TW_INTERNAL_PATH, Mount_Point + "/media/0");
1188 UnMount(true);
1189 }
1190 DataManager::SetValue("tw_has_internal", 1);
1191 DataManager::SetValue("tw_has_data_media", 1);
Ethan Yonker3fdcda42016-11-30 12:29:37 -06001192 backup_exclusions.add_absolute_dir("/data/data/com.google.android.music/files");
Ethan Yonker6355b562017-05-01 09:42:17 -05001193 wipe_exclusions.add_absolute_dir(Mount_Point + "/misc/vold"); // adopted storage keys
Ethan Yonker79f88bd2016-12-09 14:52:12 -06001194 ExcludeAll(Mount_Point + "/system/storage.xml");
Ethan Yonker6277c792014-09-15 14:54:30 -05001195 } else {
Ethan Yonker66a19492015-12-10 10:19:45 -06001196 if (Mount(true) && TWFunc::Path_Exists(Mount_Point + "/media/0")) {
1197 Storage_Path = Mount_Point + "/media/0";
1198 Symlink_Path = Storage_Path;
1199 UnMount(true);
1200 }
Ethan Yonker6277c792014-09-15 14:54:30 -05001201 }
Ethan Yonker79f88bd2016-12-09 14:52:12 -06001202 ExcludeAll(Mount_Point + "/media");
Ethan Yonker6277c792014-09-15 14:54:30 -05001203}
1204
Dees_Troy5bf43922012-09-07 16:07:55 -04001205void TWPartition::Find_Real_Block_Device(string& Block, bool Display_Error) {
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06001206 char device[PATH_MAX], realDevice[PATH_MAX];
Dees_Troy5bf43922012-09-07 16:07:55 -04001207
1208 strcpy(device, Block.c_str());
1209 memset(realDevice, 0, sizeof(realDevice));
1210 while (readlink(device, realDevice, sizeof(realDevice)) > 0)
1211 {
1212 strcpy(device, realDevice);
1213 memset(realDevice, 0, sizeof(realDevice));
1214 }
1215
1216 if (device[0] != '/') {
1217 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001218 LOGERR("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001219 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001220 LOGINFO("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001221 return;
Dees_Troy51a0e822012-09-05 15:24:24 -04001222 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -04001223 Block = device;
1224 return;
1225 }
1226}
1227
Kjell Braden3126a112016-06-19 16:58:15 +00001228bool TWPartition::Mount_Storage_Retry(bool Display_Error) {
Dees_Troy8e337f32012-10-13 22:07:49 -04001229 // On some devices, storage doesn't want to mount right away, retry and sleep
Kjell Braden3126a112016-06-19 16:58:15 +00001230 if (!Mount(Display_Error)) {
Dees_Troy8e337f32012-10-13 22:07:49 -04001231 int retry_count = 5;
1232 while (retry_count > 0 && !Mount(false)) {
1233 usleep(500000);
1234 retry_count--;
1235 }
Kjell Braden3126a112016-06-19 16:58:15 +00001236 return Mount(Display_Error);
Dees_Troy8e337f32012-10-13 22:07:49 -04001237 }
Kjell Braden3126a112016-06-19 16:58:15 +00001238 return true;
Dees_Troy8e337f32012-10-13 22:07:49 -04001239}
1240
Dees_Troy38bd7602012-09-14 13:33:53 -04001241bool TWPartition::Find_MTD_Block_Device(string MTD_Name) {
1242 FILE *fp = NULL;
1243 char line[255];
1244
1245 fp = fopen("/proc/mtd", "rt");
1246 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001247 LOGERR("Device does not support /proc/mtd\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001248 return false;
1249 }
1250
1251 while (fgets(line, sizeof(line), fp) != NULL)
1252 {
1253 char device[32], label[32];
1254 unsigned long size = 0;
Dees_Troy38bd7602012-09-14 13:33:53 -04001255 int deviceId;
1256
1257 sscanf(line, "%s %lx %*s %*c%s", device, &size, label);
1258
1259 // Skip header and blank lines
1260 if ((strcmp(device, "dev:") == 0) || (strlen(line) < 8))
1261 continue;
1262
1263 // Strip off the trailing " from the label
1264 label[strlen(label)-1] = '\0';
1265
1266 if (strcmp(label, MTD_Name.c_str()) == 0) {
1267 // We found our device
1268 // Strip off the trailing : from the device
1269 device[strlen(device)-1] = '\0';
1270 if (sscanf(device,"mtd%d", &deviceId) == 1) {
1271 sprintf(device, "/dev/block/mtdblock%d", deviceId);
1272 Primary_Block_Device = device;
Dees_Troy76543db2013-06-19 16:24:30 +00001273 fclose(fp);
1274 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -04001275 }
1276 }
1277 }
1278 fclose(fp);
1279
1280 return false;
1281}
1282
Dees_Troy51127312012-09-08 13:08:49 -04001283bool TWPartition::Get_Size_Via_statfs(bool Display_Error) {
1284 struct statfs st;
1285 string Local_Path = Mount_Point + "/.";
1286
1287 if (!Mount(Display_Error))
1288 return false;
1289
1290 if (statfs(Local_Path.c_str(), &st) != 0) {
1291 if (!Removable) {
1292 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001293 LOGERR("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -04001294 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001295 LOGINFO("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -04001296 }
1297 return false;
1298 }
1299 Size = (st.f_blocks * st.f_bsize);
1300 Used = ((st.f_blocks - st.f_bfree) * st.f_bsize);
1301 Free = (st.f_bfree * st.f_bsize);
1302 Backup_Size = Used;
1303 return true;
1304}
1305
1306bool TWPartition::Get_Size_Via_df(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001307 FILE* fp;
1308 char command[255], line[512];
1309 int include_block = 1;
1310 unsigned int min_len;
1311
1312 if (!Mount(Display_Error))
1313 return false;
1314
Dees_Troy38bd7602012-09-14 13:33:53 -04001315 min_len = Actual_Block_Device.size() + 2;
Dees_Troy51127312012-09-08 13:08:49 -04001316 sprintf(command, "df %s > /tmp/dfoutput.txt", Mount_Point.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001317 TWFunc::Exec_Cmd(command);
Dees_Troy51127312012-09-08 13:08:49 -04001318 fp = fopen("/tmp/dfoutput.txt", "rt");
1319 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001320 LOGINFO("Unable to open /tmp/dfoutput.txt.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001321 return false;
Dees_Troy51127312012-09-08 13:08:49 -04001322 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001323
1324 while (fgets(line, sizeof(line), fp) != NULL)
1325 {
1326 unsigned long blocks, used, available;
1327 char device[64];
1328 char tmpString[64];
1329
1330 if (strncmp(line, "Filesystem", 10) == 0)
1331 continue;
1332 if (strlen(line) < min_len) {
1333 include_block = 0;
1334 continue;
1335 }
1336 if (include_block) {
1337 sscanf(line, "%s %lu %lu %lu", device, &blocks, &used, &available);
1338 } else {
1339 // The device block string is so long that the df information is on the next line
1340 int space_count = 0;
Dees_Troye58d5262012-09-21 12:27:57 -04001341 sprintf(tmpString, "/dev/block/%s", Actual_Block_Device.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001342 while (tmpString[space_count] == 32)
1343 space_count++;
1344 sscanf(line + space_count, "%lu %lu %lu", &blocks, &used, &available);
1345 }
1346
1347 // Adjust block size to byte size
1348 Size = blocks * 1024ULL;
1349 Used = used * 1024ULL;
1350 Free = available * 1024ULL;
1351 Backup_Size = Used;
1352 }
1353 fclose(fp);
1354 return true;
1355}
1356
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001357unsigned long long TWPartition::IOCTL_Get_Block_Size() {
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001358 Find_Actual_Block_Device();
Ethan Yonker483e9f42016-01-11 22:21:18 -06001359
1360 return TWFunc::IOCTL_Get_Block_Size(Actual_Block_Device.c_str());
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001361}
1362
Dees_Troy5bf43922012-09-07 16:07:55 -04001363bool TWPartition::Find_Partition_Size(void) {
1364 FILE* fp;
1365 char line[512];
1366 string tmpdevice;
1367
igoriok87e3d932013-01-31 21:03:53 +02001368 fp = fopen("/proc/dumchar_info", "rt");
1369 if (fp != NULL) {
1370 while (fgets(line, sizeof(line), fp) != NULL)
1371 {
1372 char label[32], device[32];
1373 unsigned long size = 0;
1374
thatd43bf2d2014-09-21 23:13:02 +02001375 sscanf(line, "%s %lx %*x %*u %s", label, &size, device);
igoriok87e3d932013-01-31 21:03:53 +02001376
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05001377 // Skip header, annotation and blank lines
igoriok87e3d932013-01-31 21:03:53 +02001378 if ((strncmp(device, "/dev/", 5) != 0) || (strlen(line) < 8))
1379 continue;
1380
1381 tmpdevice = "/dev/";
1382 tmpdevice += label;
1383 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
1384 Size = size;
1385 fclose(fp);
1386 return true;
1387 }
1388 }
1389 }
1390
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001391 unsigned long long ioctl_size = IOCTL_Get_Block_Size();
1392 if (ioctl_size) {
1393 Size = ioctl_size;
1394 return true;
1395 }
1396
Dees_Troy5bf43922012-09-07 16:07:55 -04001397 // In this case, we'll first get the partitions we care about (with labels)
1398 fp = fopen("/proc/partitions", "rt");
1399 if (fp == NULL)
1400 return false;
1401
1402 while (fgets(line, sizeof(line), fp) != NULL)
1403 {
1404 unsigned long major, minor, blocks;
1405 char device[512];
Dees_Troy5bf43922012-09-07 16:07:55 -04001406
Dees_Troy63c8df72012-09-10 14:02:05 -04001407 if (strlen(line) < 7 || line[0] == 'm') continue;
Dees_Troy5bf43922012-09-07 16:07:55 -04001408 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
1409
1410 tmpdevice = "/dev/block/";
1411 tmpdevice += device;
Dees_Troy38bd7602012-09-14 13:33:53 -04001412 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001413 // Adjust block size to byte size
1414 Size = blocks * 1024ULL;
1415 fclose(fp);
1416 return true;
1417 }
1418 }
1419 fclose(fp);
1420 return false;
1421}
1422
Dees_Troy5bf43922012-09-07 16:07:55 -04001423bool TWPartition::Is_Mounted(void) {
1424 if (!Can_Be_Mounted)
1425 return false;
1426
1427 struct stat st1, st2;
1428 string test_path;
1429
1430 // Check to see if the mount point directory exists
1431 test_path = Mount_Point + "/.";
1432 if (stat(test_path.c_str(), &st1) != 0) return false;
1433
1434 // Check to see if the directory above the mount point exists
1435 test_path = Mount_Point + "/../.";
1436 if (stat(test_path.c_str(), &st2) != 0) return false;
1437
1438 // Compare the device IDs -- if they match then we're (probably) using tmpfs instead of an actual device
1439 int ret = (st1.st_dev != st2.st_dev) ? true : false;
Dees_Troy5bf43922012-09-07 16:07:55 -04001440 return ret;
1441}
1442
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05001443bool TWPartition::Is_File_System_Writable(void) {
1444 if (!Is_File_System(Current_File_System) || !Is_Mounted())
1445 return false;
1446
1447 string test_path = Mount_Point + "/.";
1448 return (access(test_path.c_str(), W_OK) == 0);
1449}
1450
Dees_Troy5bf43922012-09-07 16:07:55 -04001451bool TWPartition::Mount(bool Display_Error) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001452 int exfat_mounted = 0;
Martin Dünkelmann6d843782018-12-27 23:40:28 +01001453 unsigned int flags = Mount_Flags;
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001454
Dees_Troy5bf43922012-09-07 16:07:55 -04001455 if (Is_Mounted()) {
1456 return true;
1457 } else if (!Can_Be_Mounted) {
1458 return false;
1459 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001460
1461 Find_Actual_Block_Device();
1462
1463 // Check the current file system before mounting
1464 Check_FS_Type();
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001465 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/exfat-fuse")) {
Dees_Troye34c1332013-02-06 19:13:00 +00001466 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 +00001467 LOGINFO("cmd: %s\n", cmd.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001468 string result;
1469 if (TWFunc::Exec_Cmd(cmd, result) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001470 LOGINFO("exfat-fuse failed to mount with result '%s', trying vfat\n", result.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001471 Current_File_System = "vfat";
1472 } else {
1473#ifdef TW_NO_EXFAT_FUSE
1474 UnMount(false);
1475 // We'll let the kernel handle it but using exfat-fuse to detect if the file system is actually exfat
1476 // Some kernels let us mount vfat as exfat which doesn't work out too well
1477#else
1478 exfat_mounted = 1;
1479#endif
1480 }
1481 }
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001482
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001483 if (Current_File_System == "ntfs" && (TWFunc::Path_Exists("/sbin/ntfs-3g") || TWFunc::Path_Exists("/sbin/mount.ntfs"))) {
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001484 string cmd;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001485 string Ntfsmount_Binary = "";
1486
1487 if (TWFunc::Path_Exists("/sbin/ntfs-3g"))
1488 Ntfsmount_Binary = "ntfs-3g";
1489 else if (TWFunc::Path_Exists("/sbin/mount.ntfs"))
1490 Ntfsmount_Binary = "mount.ntfs";
1491
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001492 if (Mount_Read_Only)
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001493 cmd = "/sbin/" + Ntfsmount_Binary + " -o ro " + Actual_Block_Device + " " + Mount_Point;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001494 else
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001495 cmd = "/sbin/" + Ntfsmount_Binary + " " + Actual_Block_Device + " " + Mount_Point;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001496 LOGINFO("cmd: '%s'\n", cmd.c_str());
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001497
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001498 if (TWFunc::Exec_Cmd(cmd) == 0) {
1499 return true;
1500 } else {
1501 LOGINFO("ntfs-3g failed to mount, trying regular mount method.\n");
1502 }
1503 }
1504
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001505 if (Mount_Read_Only)
1506 flags |= MS_RDONLY;
1507
Dees_Troy22042032012-12-18 21:23:08 +00001508 if (Fstab_File_System == "yaffs2") {
1509 // mount an MTD partition as a YAFFS2 filesystem.
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001510 flags = MS_NOATIME | MS_NODEV | MS_NODIRATIME;
1511 if (Mount_Read_Only)
1512 flags |= MS_RDONLY;
Dees_Troy76543db2013-06-19 16:24:30 +00001513 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags, NULL) < 0) {
1514 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags | MS_RDONLY, NULL) < 0) {
1515 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001516 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy76543db2013-06-19 16:24:30 +00001517 else
1518 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
1519 return false;
1520 } else {
1521 LOGINFO("Mounted '%s' (MTD) as RO\n", Mount_Point.c_str());
1522 return true;
1523 }
1524 } else {
1525 struct stat st;
1526 string test_path = Mount_Point;
1527 if (stat(test_path.c_str(), &st) < 0) {
1528 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001529 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy76543db2013-06-19 16:24:30 +00001530 else
1531 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
1532 return false;
1533 }
1534 mode_t new_mode = st.st_mode | S_IXUSR | S_IXGRP | S_IXOTH;
1535 if (new_mode != st.st_mode) {
1536 LOGINFO("Fixing execute permissions for %s\n", Mount_Point.c_str());
1537 if (chmod(Mount_Point.c_str(), new_mode) < 0) {
1538 if (Display_Error)
1539 LOGERR("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
1540 else
1541 LOGINFO("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
1542 return false;
1543 }
1544 }
Dees_Troy22042032012-12-18 21:23:08 +00001545 return true;
Dees_Troy76543db2013-06-19 16:24:30 +00001546 }
thatc7572eb2015-03-31 18:55:30 +02001547 }
1548
1549 string mount_fs = Current_File_System;
1550 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sys/module/texfat"))
1551 mount_fs = "texfat";
1552
1553 if (!exfat_mounted &&
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001554 mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), mount_fs.c_str(), flags, Mount_Options.c_str()) != 0 &&
1555 mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), mount_fs.c_str(), flags, NULL) != 0) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001556#ifdef TW_NO_EXFAT_FUSE
1557 if (Current_File_System == "exfat") {
Dees_Troy2673cec2013-04-02 20:22:16 +00001558 LOGINFO("Mounting exfat failed, trying vfat...\n");
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001559 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), "vfat", 0, NULL) != 0) {
Dees_Troy85f44ed2013-01-09 18:42:36 +00001560 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001561 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy85f44ed2013-01-09 18:42:36 +00001562 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001563 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001564 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 +00001565 return false;
Dees_Troy85f44ed2013-01-09 18:42:36 +00001566 }
Dees_Troyb05ddee2013-01-28 20:24:50 +00001567 } else {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001568#endif
bigbiff bigbiff26774a02014-03-29 18:22:00 -04001569 if (!Removable && Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001570 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001571 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001572 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
1573 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 +00001574 return false;
1575#ifdef TW_NO_EXFAT_FUSE
Dees_Troy85f44ed2013-01-09 18:42:36 +00001576 }
1577#endif
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001578 }
Ethan Yonker253368a2014-11-25 15:00:52 -06001579
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001580 if (Removable)
1581 Update_Size(Display_Error);
1582
xiaolu9416f4f2015-06-04 08:22:23 +08001583 if (!Symlink_Mount_Point.empty() && TWFunc::Path_Exists(Symlink_Path)) {
codelover2a3d4ce2015-03-14 20:26:49 +08001584 string Command = "mount -o bind '" + Symlink_Path + "' '" + Symlink_Mount_Point + "'";
Vojtech Bocek05534202013-09-11 08:11:56 +02001585 TWFunc::Exec_Cmd(Command);
Dees_Troy5bf43922012-09-07 16:07:55 -04001586 }
Chaosmasterda974802020-01-26 21:09:28 +01001587
bigbiff32cbabe2020-04-12 19:16:19 -04001588 #ifndef TW_NO_BIND_SYSTEM
Chaosmasterda974802020-01-26 21:09:28 +01001589 if (Mount_Point == "/system_root") {
1590 unlink("/system");
1591 mkdir("/system", 0755);
1592 mount("/system_root/system", "/system", "auto", MS_BIND, NULL);
1593 }
bigbiff7ba75002020-04-11 20:47:09 -04001594#endif
Dees_Troy5bf43922012-09-07 16:07:55 -04001595 return true;
1596}
1597
1598bool TWPartition::UnMount(bool Display_Error) {
Chaosmasterda974802020-01-26 21:09:28 +01001599 if (Mount_Point == "/system_root") {
1600 if (umount("/system") == -1)
1601 umount2("/system", MNT_DETACH);
1602 rmdir("/system");
1603 symlink("/system_root/system", "/system");
1604 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001605 if (Is_Mounted()) {
1606 int never_unmount_system;
1607
1608 DataManager::GetValue(TW_DONT_UNMOUNT_SYSTEM, never_unmount_system);
Captain Throwback9d6feb52018-07-27 10:05:24 -04001609 if (never_unmount_system == 1 && Mount_Point == PartitionManager.Get_Android_Root_Path())
Dees_Troy5bf43922012-09-07 16:07:55 -04001610 return true; // Never unmount system if you're not supposed to unmount it
1611
Matt Mowera8e6d832017-02-14 20:20:59 -06001612 if (Is_Storage && MTP_Storage_ID > 0)
Ethan Yonker726a0202014-12-16 20:01:38 -06001613 PartitionManager.Remove_MTP_Storage(MTP_Storage_ID);
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001614
Dees_Troy38bd7602012-09-14 13:33:53 -04001615 if (!Symlink_Mount_Point.empty())
1616 umount(Symlink_Mount_Point.c_str());
1617
Dees_Troyb05ddee2013-01-28 20:24:50 +00001618 umount(Mount_Point.c_str());
1619 if (Is_Mounted()) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001620 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001621 gui_msg(Msg(msg::kError, "fail_unmount=Failed to unmount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy5bf43922012-09-07 16:07:55 -04001622 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001623 LOGINFO("Unable to unmount '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001624 return false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001625 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -04001626 return true;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001627 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001628 } else {
1629 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001630 }
1631}
1632
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05001633bool TWPartition::ReMount(bool Display_Error) {
1634 if (UnMount(Display_Error))
1635 return Mount(Display_Error);
1636 return false;
1637}
1638
1639bool TWPartition::ReMount_RW(bool Display_Error) {
1640 // No need to remount if already mounted rw
1641 if (Is_File_System_Writable())
1642 return true;
1643
1644 bool ro = Mount_Read_Only;
1645 int flags = Mount_Flags;
1646
1647 Mount_Read_Only = false;
1648 Mount_Flags &= ~MS_RDONLY;
1649
1650 bool ret = ReMount(Display_Error);
1651
1652 Mount_Read_Only = ro;
1653 Mount_Flags = flags;
1654
1655 return ret;
1656}
1657
Gary Peck43acadf2012-11-21 21:19:01 -08001658bool TWPartition::Wipe(string New_File_System) {
Ethan Yonker726a0202014-12-16 20:01:38 -06001659 bool wiped = false, update_crypt = false, recreate_media = true;
Dees_Troy16c2b312013-01-15 16:51:18 +00001660 int check;
bigbiff7ba75002020-04-11 20:47:09 -04001661 fscrypt_encryption_policy policy;
Dees_Troy16c2b312013-01-15 16:51:18 +00001662
Dees_Troy38bd7602012-09-14 13:33:53 -04001663 if (!Can_Be_Wiped) {
Matt Mower3c366972015-12-25 19:28:31 -06001664 gui_msg(Msg(msg::kError, "cannot_wipe=Partition {1} cannot be wiped.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04001665 return false;
1666 }
1667
Dees_Troyc51f1f92012-09-20 15:32:13 -04001668 if (Mount_Point == "/cache")
Dees_Troy2673cec2013-04-02 20:22:16 +00001669 Log_Offset = 0;
Dees_Troyc51f1f92012-09-20 15:32:13 -04001670
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001671 if (Has_Data_Media && Current_File_System == New_File_System) {
Dees_Troy16c2b312013-01-15 16:51:18 +00001672 wiped = Wipe_Data_Without_Wiping_Media();
bigbiff7ba75002020-04-11 20:47:09 -04001673 if (Mount_Point == "/data" && TWFunc::get_cache_dir() == AB_CACHE_DIR && !TWFunc::Is_Mount_Wiped("/data")) {
1674 bool created = Recreate_AB_Cache_Dir(policy);
1675 if (created)
1676 gui_msg(Msg(msg::kWarning, "fbe_wipe_msg=WARNING: {1} wiped. FBE device should be booted into Android and not Recovery to set initial FBE policy after wipe.")(TWFunc::get_cache_dir()));
1677 }
Ethan Yonker5eac2222014-06-11 12:22:55 -05001678 recreate_media = false;
Dees_Troy16c2b312013-01-15 16:51:18 +00001679 } else {
Dees_Troy16c2b312013-01-15 16:51:18 +00001680 DataManager::GetValue(TW_RM_RF_VAR, check);
1681
Hashcodedabfd492013-08-29 22:45:30 -07001682 if (check || Use_Rm_Rf)
Dees_Troy16c2b312013-01-15 16:51:18 +00001683 wiped = Wipe_RMRF();
1684 else if (New_File_System == "ext4")
1685 wiped = Wipe_EXT4();
1686 else if (New_File_System == "ext2" || New_File_System == "ext3")
dianlujitao4879b372018-12-03 18:45:47 +08001687 wiped = Wipe_EXTFS(New_File_System);
Dees_Troy16c2b312013-01-15 16:51:18 +00001688 else if (New_File_System == "vfat")
1689 wiped = Wipe_FAT();
1690 else if (New_File_System == "exfat")
1691 wiped = Wipe_EXFAT();
1692 else if (New_File_System == "yaffs2")
1693 wiped = Wipe_MTD();
Dees_Troye5017042013-08-29 16:38:55 +00001694 else if (New_File_System == "f2fs")
1695 wiped = Wipe_F2FS();
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001696 else if (New_File_System == "ntfs")
1697 wiped = Wipe_NTFS();
Dees_Troy16c2b312013-01-15 16:51:18 +00001698 else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001699 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 +00001700 return false;
1701 }
1702 update_crypt = wiped;
bigbiff7ba75002020-04-11 20:47:09 -04001703 update_crypt = false;
Gary Pecke8bc5d72012-12-21 06:45:25 -08001704 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001705
Gary Pecke8bc5d72012-12-21 06:45:25 -08001706 if (wiped) {
Dees_Troy16c2b312013-01-15 16:51:18 +00001707 if (update_crypt) {
1708 Setup_File_System(false);
1709 if (Is_Encrypted && !Is_Decrypted) {
1710 // just wiped an encrypted partition back to its unencrypted state
1711 Is_Encrypted = false;
1712 Is_Decrypted = false;
1713 Decrypted_Block_Device = "";
1714 if (Mount_Point == "/data") {
1715 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1716 DataManager::SetValue(TW_IS_DECRYPTED, 0);
1717 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001718 }
1719 }
Ethan Yonker5eac2222014-06-11 12:22:55 -05001720
bigbiff7ba75002020-04-11 20:47:09 -04001721 // if (Is_Storage && Mount(false))
1722 // PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Gary Pecke8bc5d72012-12-21 06:45:25 -08001723 }
Matt Mower209c9632016-01-20 12:08:35 -06001724
Gary Pecke8bc5d72012-12-21 06:45:25 -08001725 return wiped;
Dees_Troy51a0e822012-09-05 15:24:24 -04001726}
1727
Gary Peck43acadf2012-11-21 21:19:01 -08001728bool TWPartition::Wipe() {
Gary Peck82599a82012-11-21 16:23:12 -08001729 if (Is_File_System(Current_File_System))
1730 return Wipe(Current_File_System);
1731 else
1732 return Wipe(Fstab_File_System);
Gary Peck43acadf2012-11-21 21:19:01 -08001733}
1734
Dees_Troye58d5262012-09-21 12:27:57 -04001735bool TWPartition::Wipe_AndSec(void) {
1736 if (!Has_Android_Secure)
1737 return false;
1738
Dees_Troye58d5262012-09-21 12:27:57 -04001739 if (!Mount(true))
1740 return false;
1741
Ethan Yonker74db1572015-10-28 12:44:49 -05001742 gui_msg(Msg("wiping=Wiping {1}")(Backup_Display_Name));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001743 TWFunc::removeDir(Mount_Point + "/.android_secure/", true);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001744 return true;
Dees_Troye58d5262012-09-21 12:27:57 -04001745}
1746
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001747bool TWPartition::Can_Repair() {
Ethan Yonkera2719152015-05-28 09:44:41 -05001748 if (Mount_Read_Only)
1749 return false;
Matt Mower