blob: 8318c9bcbac5963317cc43fd4f7a6926bae42524 [file] [log] [blame]
Dees Troy3be70a82013-10-22 14:25:12 +00001/*
bigbiffa2bd7b72020-05-07 21:45:34 -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>
Dees_Troy5bf43922012-09-07 16:07:55 -040022#include <sys/mount.h>
bigbiffa2bd7b72020-05-07 21:45:34 -040023#include <sys/param.h>
24#include <sys/stat.h>
25#include <sys/types.h>
26#include <sys/vfs.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040027#include <unistd.h>
Dees_Troy51127312012-09-08 13:08:49 -040028#include <dirent.h>
Ethan Yonker472f5062016-02-25 13:47:30 -060029#include <fcntl.h>
bigbiffa2bd7b72020-05-07 21:45:34 -040030#include <grp.h>
31#include <iostream>
32#include <libgen.h>
33#include <pwd.h>
34#include <zlib.h>
35#include <sstream>
Dees_Troy51a0e822012-09-05 15:24:24 -040036
Ethan Yonker007de982018-08-31 14:23:16 -050037#include "cutils/properties.h"
bigbiff7b4c7a62015-01-01 19:44:14 -050038#include "libblkid/include/blkid.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040039#include "variables.h"
Dees_Troy2673cec2013-04-02 20:22:16 +000040#include "twcommon.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040041#include "partitions.hpp"
Dees_Troy5bf43922012-09-07 16:07:55 -040042#include "data.hpp"
Dees_Troy43d8b002012-09-17 16:00:01 -040043#include "twrp-functions.hpp"
bigbiff bigbiff9c754052013-01-09 09:09:08 -050044#include "twrpTar.hpp"
Ethan Yonker3fdcda42016-11-30 12:29:37 -060045#include "exclude.hpp"
Ethan Yonker1b7a31b2014-07-03 15:09:22 -050046#include "infomanager.hpp"
Ethan Yonker4b94cfd2014-12-11 10:00:45 -060047#include "set_metadata.h"
Ethan Yonker74db1572015-10-28 12:44:49 -050048#include "gui/gui.hpp"
bigbiffce8f83c2015-12-12 18:30:21 -050049#include "adbbu/libtwadbbu.hpp"
Ethan Yonker98661c12018-10-17 08:39:28 -050050#ifdef TW_INCLUDE_CRYPTO
51 #include "crypto/fde/cryptfs.h"
52 #ifdef TW_INCLUDE_FBE
53 #include "crypto/ext4crypt/Decrypt.h"
54 #endif
55#else
56 #define CRYPT_FOOTER_OFFSET 0x4000
57#endif
Dees_Troy5bf43922012-09-07 16:07:55 -040058extern "C" {
Dees_Troy38bd7602012-09-14 13:33:53 -040059 #include "mtdutils/mtdutils.h"
60 #include "mtdutils/mounts.h"
Dees_Troya95f55c2013-08-17 13:14:43 +000061#ifdef USE_EXT4
Ethan Yonker8373cfe2017-09-08 06:50:54 -050062 // #include "make_ext4fs.h" TODO need ifdef for android8
63 #include <ext4_utils/make_ext4fs.h>
Dees_Troya95f55c2013-08-17 13:14:43 +000064#endif
Ethan Yonker71413f42014-02-26 13:36:08 -060065#ifdef TW_INCLUDE_CRYPTO
Ethan Yonker66a19492015-12-10 10:19:45 -060066 #include "gpt/gpt.h"
Ethan Yonker71413f42014-02-26 13:36:08 -060067#endif
Dees_Troy5bf43922012-09-07 16:07:55 -040068}
Matt Mower87413642017-01-17 21:14:46 -060069#include <selinux/selinux.h>
Ethan Yonkerf27497f2014-02-09 11:48:33 -060070#include <selinux/label.h>
Dees Troy4159aed2014-02-28 17:24:43 +000071#ifdef HAVE_CAPABILITIES
72#include <sys/capability.h>
73#include <sys/xattr.h>
74#include <linux/xattr.h>
75#endif
HashBanged974bb2016-01-30 14:20:09 -050076#include <sparse_format.h>
Ethan Yonker472f5062016-02-25 13:47:30 -060077#include "progresstracking.hpp"
Dees_Troy51a0e822012-09-05 15:24:24 -040078
bigbiff bigbiff9c754052013-01-09 09:09:08 -050079using namespace std;
80
Ethan Yonker6e8c27a2016-12-22 17:55:57 -060081static 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
82
Dees_Troya95f55c2013-08-17 13:14:43 +000083extern struct selabel_handle *selinux_handle;
Ethan Yonker6277c792014-09-15 14:54:30 -050084extern bool datamedia;
Dees_Troya95f55c2013-08-17 13:14:43 +000085
Hashcode62bd9e02013-11-19 21:59:42 -080086struct flag_list {
87 const char *name;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -060088 unsigned long flag;
Hashcode62bd9e02013-11-19 21:59:42 -080089};
90
Matt Mower4ab42b12016-04-21 13:52:18 -050091const struct flag_list mount_flags[] = {
Ethan Yonker6e8c27a2016-12-22 17:55:57 -060092 { "noatime", MS_NOATIME },
93 { "noexec", MS_NOEXEC },
94 { "nosuid", MS_NOSUID },
95 { "nodev", MS_NODEV },
96 { "nodiratime", MS_NODIRATIME },
97 { "ro", MS_RDONLY },
98 { "rw", 0 },
99 { "remount", MS_REMOUNT },
100 { "bind", MS_BIND },
101 { "rec", MS_REC },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000102#ifdef MS_UNBINDABLE
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600103 { "unbindable", MS_UNBINDABLE },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000104#endif
105#ifdef MS_PRIVATE
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600106 { "private", MS_PRIVATE },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000107#endif
108#ifdef MS_SLAVE
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600109 { "slave", MS_SLAVE },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000110#endif
111#ifdef MS_SHARED
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600112 { "shared", MS_SHARED },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000113#endif
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600114 { "sync", MS_SYNCHRONOUS },
115 { 0, 0 },
116};
117
118const char *ignored_mount_items[] = {
119 "defaults=",
120 "errors=",
Ethan Yonker93382822018-11-01 15:25:31 -0500121 "latemount",
122 "sysfs_path=",
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600123 NULL
Hashcode62bd9e02013-11-19 21:59:42 -0800124};
125
Matt Mower2416a502016-04-12 19:54:46 -0500126enum TW_FSTAB_FLAGS {
127 TWFLAG_DEFAULTS, // Retain position
128 TWFLAG_ANDSEC,
129 TWFLAG_BACKUP,
130 TWFLAG_BACKUPNAME,
131 TWFLAG_BLOCKSIZE,
132 TWFLAG_CANBEWIPED,
133 TWFLAG_CANENCRYPTBACKUP,
134 TWFLAG_DISPLAY,
135 TWFLAG_ENCRYPTABLE,
Ethan Yonkere9afc3d2018-08-30 15:16:27 -0500136 TWFLAG_FILEENCRYPTION,
Matt Mower2416a502016-04-12 19:54:46 -0500137 TWFLAG_FLASHIMG,
138 TWFLAG_FORCEENCRYPT,
139 TWFLAG_FSFLAGS,
140 TWFLAG_IGNOREBLKID,
141 TWFLAG_LENGTH,
142 TWFLAG_MOUNTTODECRYPT,
143 TWFLAG_REMOVABLE,
144 TWFLAG_RETAINLAYOUTVERSION,
145 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 Cai40ed06e2019-05-24 11:38:54 +0800164 TWFLAG_WRAPPEDKEY,
Peter Caic50044b2019-10-17 08:49:10 +0800165 TWFLAG_ADOPTED_MOUNT_DELAY,
Peter Cai39881ab2019-11-02 19:22:38 +0800166 TWFLAG_DM_USE_ORIGINAL_PATH,
Matt Mower2416a502016-04-12 19:54:46 -0500167};
168
169/* Flags without a trailing '=' are considered dual format flags and can be
170 * written as either 'flagname' or 'flagname=', where the character following
171 * the '=' is Y,y,1 for true and false otherwise.
172 */
173const struct flag_list tw_flags[] = {
174 { "andsec", TWFLAG_ANDSEC },
175 { "backup", TWFLAG_BACKUP },
176 { "backupname=", TWFLAG_BACKUPNAME },
177 { "blocksize=", TWFLAG_BLOCKSIZE },
178 { "canbewiped", TWFLAG_CANBEWIPED },
179 { "canencryptbackup", TWFLAG_CANENCRYPTBACKUP },
180 { "defaults", TWFLAG_DEFAULTS },
181 { "display=", TWFLAG_DISPLAY },
182 { "encryptable=", TWFLAG_ENCRYPTABLE },
Ethan Yonkere9afc3d2018-08-30 15:16:27 -0500183 { "fileencryption=", TWFLAG_FILEENCRYPTION },
Matt Mower2416a502016-04-12 19:54:46 -0500184 { "flashimg", TWFLAG_FLASHIMG },
185 { "forceencrypt=", TWFLAG_FORCEENCRYPT },
186 { "fsflags=", TWFLAG_FSFLAGS },
187 { "ignoreblkid", TWFLAG_IGNOREBLKID },
188 { "length=", TWFLAG_LENGTH },
189 { "mounttodecrypt", TWFLAG_MOUNTTODECRYPT },
190 { "removable", TWFLAG_REMOVABLE },
191 { "retainlayoutversion", TWFLAG_RETAINLAYOUTVERSION },
192 { "settingsstorage", TWFLAG_SETTINGSSTORAGE },
193 { "storage", TWFLAG_STORAGE },
194 { "storagename=", TWFLAG_STORAGENAME },
195 { "subpartitionof=", TWFLAG_SUBPARTITIONOF },
196 { "symlink=", TWFLAG_SYMLINK },
197 { "userdataencryptbackup", TWFLAG_USERDATAENCRYPTBACKUP },
198 { "usermrf", TWFLAG_USERMRF },
199 { "wipeduringfactoryreset", TWFLAG_WIPEDURINGFACTORYRESET },
200 { "wipeingui", TWFLAG_WIPEINGUI },
Ethan Yonker1b190162016-12-05 15:25:19 -0600201 { "slotselect", TWFLAG_SLOTSELECT },
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600202 { "wait", TWFLAG_WAIT },
203 { "verify", TWFLAG_VERIFY },
204 { "check", TWFLAG_CHECK },
205 { "altdevice", TWFLAG_ALTDEVICE },
206 { "notrim", TWFLAG_NOTRIM },
207 { "voldmanaged=", TWFLAG_VOLDMANAGED },
208 { "formattable", TWFLAG_FORMATTABLE },
209 { "resize", TWFLAG_RESIZE },
Ethan Yonker93382822018-11-01 15:25:31 -0500210 { "keydirectory=", TWFLAG_KEYDIRECTORY },
Peter Cai40ed06e2019-05-24 11:38:54 +0800211 { "wrappedkey", TWFLAG_WRAPPEDKEY },
Peter Caic50044b2019-10-17 08:49:10 +0800212 { "adopted_mount_delay=", TWFLAG_ADOPTED_MOUNT_DELAY },
Peter Cai39881ab2019-11-02 19:22:38 +0800213 { "dm_use_original_path", TWFLAG_DM_USE_ORIGINAL_PATH },
Matt Mower2416a502016-04-12 19:54:46 -0500214 { 0, 0 },
215};
216
that9e0593e2014-10-08 00:01:24 +0200217TWPartition::TWPartition() {
Dees_Troy51a0e822012-09-05 15:24:24 -0400218 Can_Be_Mounted = false;
219 Can_Be_Wiped = false;
Dees_Troya13d74f2013-03-24 08:54:55 -0500220 Can_Be_Backed_Up = false;
Vojtech Bocek1dc30982013-08-30 21:49:30 +0200221 Use_Rm_Rf = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400222 Wipe_During_Factory_Reset = false;
223 Wipe_Available_in_GUI = false;
224 Is_SubPartition = false;
Dees_Troy2691f9d2012-09-24 11:15:49 -0400225 Has_SubPartition = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400226 SubPartition_Of = "";
227 Symlink_Path = "";
228 Symlink_Mount_Point = "";
229 Mount_Point = "";
Dees_Troye58d5262012-09-21 12:27:57 -0400230 Backup_Path = "";
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600231 Wildcard_Block_Device = false;
232 Sysfs_Entry = "";
Dees_Troy38bd7602012-09-14 13:33:53 -0400233 Actual_Block_Device = "";
234 Primary_Block_Device = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400235 Alternate_Block_Device = "";
236 Removable = false;
237 Is_Present = false;
238 Length = 0;
239 Size = 0;
240 Used = 0;
241 Free = 0;
242 Backup_Size = 0;
243 Can_Be_Encrypted = false;
244 Is_Encrypted = false;
245 Is_Decrypted = false;
Ethan Yonkerbd7492d2016-12-07 13:55:01 -0600246 Is_FBE = false;
Ethan Yonker253368a2014-11-25 15:00:52 -0600247 Mount_To_Decrypt = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400248 Decrypted_Block_Device = "";
249 Display_Name = "";
Dees_Troya13d74f2013-03-24 08:54:55 -0500250 Backup_Display_Name = "";
251 Storage_Name = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400252 Backup_Name = "";
Dees_Troy63c8df72012-09-10 14:02:05 -0400253 Backup_FileName = "";
Dees_Troy38bd7602012-09-14 13:33:53 -0400254 MTD_Name = "";
bigbiffce8f83c2015-12-12 18:30:21 -0500255 Backup_Method = BM_NONE;
Dees_Troy83bd4832013-05-04 12:39:56 +0000256 Can_Encrypt_Backup = false;
257 Use_Userdata_Encryption = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400258 Has_Data_Media = false;
Dees_Troye58d5262012-09-21 12:27:57 -0400259 Has_Android_Secure = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400260 Is_Storage = false;
Dees_Troya13d74f2013-03-24 08:54:55 -0500261 Is_Settings_Storage = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400262 Storage_Path = "";
263 Current_File_System = "";
264 Fstab_File_System = "";
Hashcode62bd9e02013-11-19 21:59:42 -0800265 Mount_Flags = 0;
266 Mount_Options = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400267 Format_Block_Size = 0;
Dees_Troy68cab492012-12-12 19:29:35 +0000268 Ignore_Blkid = false;
Dees_Troy16c2b312013-01-15 16:51:18 +0000269 Retain_Layout_Version = false;
dianlujitao4879b372018-12-03 18:45:47 +0800270 Crypto_Key_Location = "";
Ethan Yonker726a0202014-12-16 20:01:38 -0600271 MTP_Storage_ID = 0;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600272 Can_Flash_Img = false;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500273 Mount_Read_Only = false;
Ethan Yonker66a19492015-12-10 10:19:45 -0600274 Is_Adopted_Storage = false;
275 Adopted_GUID = "";
Ethan Yonker1b190162016-12-05 15:25:19 -0600276 SlotSelect = false;
Ethan Yonker93382822018-11-01 15:25:31 -0500277 Key_Directory = "";
Peter Caic50044b2019-10-17 08:49:10 +0800278 Adopted_Mount_Delay = 0;
Peter Cai39881ab2019-11-02 19:22:38 +0800279 Original_Path = "";
280 Use_Original_Path = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400281}
282
283TWPartition::~TWPartition(void) {
284 // Do nothing
285}
286
Chaosmasterf6e42ce2020-01-27 00:17:04 +0100287bool 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 -0500288 char full_line[MAX_FSTAB_LINE_LENGTH];
Matt Mower2416a502016-04-12 19:54:46 -0500289 char twflags[MAX_FSTAB_LINE_LENGTH] = "";
Dees_Troy5bf43922012-09-07 16:07:55 -0400290 char* ptr;
Matt Mower2b2dd152016-04-26 11:24:08 -0500291 int line_len = strlen(fstab_line), index = 0, item_index = 0;
Dees_Troya13d74f2013-03-24 08:54:55 -0500292 bool skip = false;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600293 int fstab_version = 1, mount_point_index = 0, fs_index = 1, block_device_index = 2;
294 TWPartition *additional_entry = NULL;
295 std::map<string, Flags_Map>::iterator it;
Dees_Troy5bf43922012-09-07 16:07:55 -0400296
Matt Mower2b2dd152016-04-26 11:24:08 -0500297 strlcpy(full_line, fstab_line, sizeof(full_line));
Dees_Troy51127312012-09-08 13:08:49 -0400298 for (index = 0; index < line_len; index++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500299 if (full_line[index] == 34)
300 skip = !skip;
301 if (!skip && full_line[index] <= 32)
Dees_Troy5bf43922012-09-07 16:07:55 -0400302 full_line[index] = '\0';
Dees_Troy5bf43922012-09-07 16:07:55 -0400303 }
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600304 if (line_len < 10)
305 return false; // There can't possibly be a valid fstab line that is less than 10 chars
306 if (strncmp(fstab_line, "/dev/", strlen("/dev/")) == 0 || strncmp(fstab_line, "/devices/", strlen("/devices/")) == 0) {
307 fstab_version = 2;
308 block_device_index = 0;
309 mount_point_index = 1;
310 fs_index = 2;
311 }
312
313 index = 0;
Dees_Troy5bf43922012-09-07 16:07:55 -0400314 while (index < line_len) {
315 while (index < line_len && full_line[index] == '\0')
316 index++;
317 if (index >= line_len)
318 continue;
319 ptr = full_line + index;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600320 if (item_index == mount_point_index) {
321 Mount_Point = ptr;
322 if (fstab_version == 2) {
323 additional_entry = PartitionManager.Find_Partition_By_Path(Mount_Point);
Chaosmasterf6e42ce2020-01-27 00:17:04 +0100324 if (!Sar_Detect && additional_entry) {
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600325 LOGINFO("Found an additional entry for '%s'\n", Mount_Point.c_str());
326 }
327 }
Chaosmasterf6e42ce2020-01-27 00:17:04 +0100328 if(!Sar_Detect)
329 LOGINFO("Processing '%s'\n", Mount_Point.c_str());
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600330 Backup_Path = Mount_Point;
331 Storage_Path = Mount_Point;
332 Display_Name = ptr + 1;
333 Backup_Display_Name = Display_Name;
334 Storage_Name = Display_Name;
335 item_index++;
336 } else if (item_index == fs_index) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400337 // File System
338 Fstab_File_System = ptr;
339 Current_File_System = ptr;
340 item_index++;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600341 } else if (item_index == block_device_index) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400342 // Primary Block Device
Dees_Troy38bd7602012-09-14 13:33:53 -0400343 if (Fstab_File_System == "mtd" || Fstab_File_System == "yaffs2") {
Dees_Troy094207a2012-09-26 12:00:39 -0400344 MTD_Name = ptr;
345 Find_MTD_Block_Device(MTD_Name);
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400346 } else if (Fstab_File_System == "bml") {
347 if (Mount_Point == "/boot")
348 MTD_Name = "boot";
349 else if (Mount_Point == "/recovery")
350 MTD_Name = "recovery";
351 Primary_Block_Device = ptr;
352 if (*ptr != '/')
Dees_Troy2673cec2013-04-02 20:22:16 +0000353 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 -0400354 } else if (*ptr != '/') {
Dees_Troy5bf43922012-09-07 16:07:55 -0400355 if (Display_Error)
Matt Mower2b2dd152016-04-26 11:24:08 -0500356 LOGERR("Invalid block device '%s' in fstab line '%s'", ptr, fstab_line);
Dees_Troy5bf43922012-09-07 16:07:55 -0400357 else
Matt Mower2b2dd152016-04-26 11:24:08 -0500358 LOGINFO("Invalid block device '%s' in fstab line '%s'", ptr, fstab_line);
Matt Mower72c87ce2016-04-26 14:34:56 -0500359 return false;
Dees_Troy38bd7602012-09-14 13:33:53 -0400360 } else {
361 Primary_Block_Device = ptr;
bigbiffdf224ce2021-02-26 19:36:07 -0500362 if (PartitionManager.Get_Android_Root_Path() != "/system_root")
363 Find_Real_Block_Device(Primary_Block_Device, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400364 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400365 item_index++;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600366 } else if (item_index > 2) {
367 if (fstab_version == 2) {
368 if (item_index == 3) {
369 Process_FS_Flags(ptr);
370 if (additional_entry) {
371 additional_entry->Save_FS_Flags(Fstab_File_System, Mount_Flags, Mount_Options);
372 return false; // We save the extra fs flags in the other partition entry and by returning false, this entry will be deleted
373 }
374 } else {
375 strlcpy(twflags, ptr, sizeof(twflags));
376 }
377 item_index++;
378 } else if (*ptr == '/') { // v2 fstab does not allow alternate block devices
Dees_Troy5bf43922012-09-07 16:07:55 -0400379 // Alternate Block Device
380 Alternate_Block_Device = ptr;
381 Find_Real_Block_Device(Alternate_Block_Device, Display_Error);
382 } else if (strlen(ptr) > 7 && strncmp(ptr, "length=", 7) == 0) {
383 // Partition length
384 ptr += 7;
385 Length = atoi(ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400386 } else if (strlen(ptr) > 6 && strncmp(ptr, "flags=", 6) == 0) {
387 // Custom flags, save for later so that new values aren't overwritten by defaults
388 ptr += 6;
Matt Mower2416a502016-04-12 19:54:46 -0500389 strlcpy(twflags, ptr, sizeof(twflags));
Dees_Troy38bd7602012-09-14 13:33:53 -0400390 } else if (strlen(ptr) == 4 && (strncmp(ptr, "NULL", 4) == 0 || strncmp(ptr, "null", 4) == 0 || strncmp(ptr, "null", 4) == 0)) {
391 // Do nothing
Dees_Troy5bf43922012-09-07 16:07:55 -0400392 } else {
393 // Unhandled data
Matt Mower2b2dd152016-04-26 11:24:08 -0500394 LOGINFO("Unhandled fstab information '%s' in fstab line '%s'\n", ptr, fstab_line);
Dees_Troy5bf43922012-09-07 16:07:55 -0400395 }
396 }
397 while (index < line_len && full_line[index] != '\0')
398 index++;
399 }
400
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600401 // override block devices from the v2 fstab with the ones we read from the twrp.flags file in case they are different
402 if (fstab_version == 2 && twrp_flags && twrp_flags->size() > 0) {
403 it = twrp_flags->find(Mount_Point);
404 if (it != twrp_flags->end()) {
405 if (!it->second.Primary_Block_Device.empty()) {
406 Primary_Block_Device = it->second.Primary_Block_Device;
407 Find_Real_Block_Device(Primary_Block_Device, Display_Error);
408 }
409 if (!it->second.Alternate_Block_Device.empty()) {
410 Alternate_Block_Device = it->second.Alternate_Block_Device;
411 Find_Real_Block_Device(Alternate_Block_Device, Display_Error);
412 }
413 }
414 }
415
416 if (strncmp(fstab_line, "/devices/", strlen("/devices/")) == 0) {
417 Sysfs_Entry = Primary_Block_Device;
418 Primary_Block_Device = "";
419 Is_Storage = true;
420 Removable = true;
421 Wipe_Available_in_GUI = true;
422 Wildcard_Block_Device = true;
423 }
424 if (Primary_Block_Device.find("*") != string::npos)
425 Wildcard_Block_Device = true;
426
Chaosmasterf6e42ce2020-01-27 00:17:04 +0100427 if (Sar_Detect) {
428 if(Is_File_System(Fstab_File_System) && (Mount_Point == "/" || Mount_Point == "/system" || Mount_Point == "/system_root"))
429 Find_Actual_Block_Device();
430 else
431 return true;
432 } else if (Mount_Point == "auto") {
Captain Throwback4e379532020-06-05 20:42:16 -0400433 Mount_Point = "/auto";
434 char autoi[5];
435 sprintf(autoi, "%i", auto_index);
436 Mount_Point += autoi;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600437 Backup_Path = Mount_Point;
438 Storage_Path = Mount_Point;
dianlujitaob76a73a2020-04-30 20:33:20 +0800439 Backup_Name = Mount_Point.substr(1);
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600440 auto_index++;
441 Setup_File_System(Display_Error);
442 Display_Name = "Storage";
443 Backup_Display_Name = Display_Name;
444 Storage_Name = Display_Name;
445 Can_Be_Backed_Up = false;
446 Wipe_Available_in_GUI = true;
447 Is_Storage = true;
448 Removable = true;
449 Wipe_Available_in_GUI = true;
450 } else if (!Is_File_System(Fstab_File_System) && !Is_Image(Fstab_File_System)) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400451 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000452 LOGERR("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400453 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000454 LOGINFO("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Matt Mower72c87ce2016-04-26 14:34:56 -0500455 return false;
Dees_Troy5bf43922012-09-07 16:07:55 -0400456 } else if (Is_File_System(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400457 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400458 Setup_File_System(Display_Error);
dianlujitaob76a73a2020-04-30 20:33:20 +0800459 Backup_Name = Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
Chaosmaster3b682732020-01-25 21:09:28 +0100460 if (Mount_Point == "/" || Mount_Point == "/system" || Mount_Point == "/system_root") {
Dees_Troy5bf43922012-09-07 16:07:55 -0400461 Display_Name = "System";
dianlujitaob76a73a2020-04-30 20:33:20 +0800462 Backup_Name = "system";
Dees_Troya13d74f2013-03-24 08:54:55 -0500463 Backup_Display_Name = Display_Name;
464 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400465 Wipe_Available_in_GUI = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500466 Can_Be_Backed_Up = true;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500467 Mount_Read_Only = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400468 } else if (Mount_Point == "/data") {
469 Display_Name = "Data";
Dees_Troya13d74f2013-03-24 08:54:55 -0500470 Backup_Display_Name = Display_Name;
471 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400472 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400473 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500474 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000475 Can_Encrypt_Backup = true;
476 Use_Userdata_Encryption = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400477 } else if (Mount_Point == "/cache") {
478 Display_Name = "Cache";
Dees_Troya13d74f2013-03-24 08:54:55 -0500479 Backup_Display_Name = Display_Name;
480 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400481 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400482 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500483 Can_Be_Backed_Up = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400484 } else if (Mount_Point == "/datadata") {
Dees_Troy51127312012-09-08 13:08:49 -0400485 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400486 Display_Name = "DataData";
Dees_Troya13d74f2013-03-24 08:54:55 -0500487 Backup_Display_Name = Display_Name;
488 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400489 Is_SubPartition = true;
490 SubPartition_Of = "/data";
Dees_Troy5bf43922012-09-07 16:07:55 -0400491 DataManager::SetValue(TW_HAS_DATADATA, 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500492 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000493 Can_Encrypt_Backup = true;
494 Use_Userdata_Encryption = false; // This whole partition should be encrypted
Dees_Troy5bf43922012-09-07 16:07:55 -0400495 } else if (Mount_Point == "/sd-ext") {
Dees_Troy51127312012-09-08 13:08:49 -0400496 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400497 Display_Name = "SD-Ext";
Dees_Troya13d74f2013-03-24 08:54:55 -0500498 Backup_Display_Name = Display_Name;
499 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400500 Wipe_Available_in_GUI = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400501 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500502 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000503 Can_Encrypt_Backup = true;
504 Use_Userdata_Encryption = true;
Dees_Troy2c50e182012-09-26 20:05:28 -0400505 } else if (Mount_Point == "/boot") {
506 Display_Name = "Boot";
Dees_Troya13d74f2013-03-24 08:54:55 -0500507 Backup_Display_Name = Display_Name;
Dees_Troy2c50e182012-09-26 20:05:28 -0400508 DataManager::SetValue("tw_boot_is_mountable", 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500509 Can_Be_Backed_Up = true;
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500510 } else if (Mount_Point == "/vendor") {
511 Display_Name = "Vendor";
512 Backup_Display_Name = Display_Name;
513 Storage_Name = Display_Name;
514 Mount_Read_Only = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400515 }
516#ifdef TW_EXTERNAL_STORAGE_PATH
517 if (Mount_Point == EXPAND(TW_EXTERNAL_STORAGE_PATH)) {
518 Is_Storage = true;
519 Storage_Path = EXPAND(TW_EXTERNAL_STORAGE_PATH);
Dees_Troyc51f1f92012-09-20 15:32:13 -0400520 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500521 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400522#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000523 if (Mount_Point == "/sdcard" || Mount_Point == "/external_sd" || Mount_Point == "/external_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400524 Is_Storage = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400525 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500526 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400527#endif
Dees_Troyb05ddee2013-01-28 20:24:50 +0000528 }
Dees_Troy8170a922012-09-18 15:40:25 -0400529#ifdef TW_INTERNAL_STORAGE_PATH
530 if (Mount_Point == EXPAND(TW_INTERNAL_STORAGE_PATH)) {
531 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500532 Is_Settings_Storage = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400533 Storage_Path = EXPAND(TW_INTERNAL_STORAGE_PATH);
Dees_Troya13d74f2013-03-24 08:54:55 -0500534 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400535 }
536#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000537 if (Mount_Point == "/emmc" || Mount_Point == "/internal_sd" || Mount_Point == "/internal_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400538 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500539 Is_Settings_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500540 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400541 }
542#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400543 } else if (Is_Image(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400544 Find_Actual_Block_Device();
Ethan Yonker1b190162016-12-05 15:25:19 -0600545 Setup_Image();
Dees_Troya13d74f2013-03-24 08:54:55 -0500546 if (Mount_Point == "/boot") {
547 Display_Name = "Boot";
548 Backup_Display_Name = Display_Name;
549 Can_Be_Backed_Up = true;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600550 Can_Flash_Img = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500551 } else if (Mount_Point == "/recovery") {
552 Display_Name = "Recovery";
553 Backup_Display_Name = Display_Name;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600554 Can_Flash_Img = true;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500555 } else if (Mount_Point == "/system_image") {
556 Display_Name = "System Image";
557 Backup_Display_Name = Display_Name;
HashBanged974bb2016-01-30 14:20:09 -0500558 Can_Flash_Img = true;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500559 Can_Be_Backed_Up = true;
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500560 } else if (Mount_Point == "/vendor_image") {
561 Display_Name = "Vendor Image";
562 Backup_Display_Name = Display_Name;
HashBanged974bb2016-01-30 14:20:09 -0500563 Can_Flash_Img = true;
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500564 Can_Be_Backed_Up = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500565 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400566 }
567
Matt Mower2416a502016-04-12 19:54:46 -0500568 // Process TWRP fstab flags
569 if (strlen(twflags) > 0) {
570 string Prev_Display_Name = Display_Name;
571 string Prev_Storage_Name = Storage_Name;
572 string Prev_Backup_Display_Name = Backup_Display_Name;
573 Display_Name = "";
574 Storage_Name = "";
575 Backup_Display_Name = "";
576
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600577 Process_TW_Flags(twflags, (fstab_version == 1), fstab_version);
578 Save_FS_Flags(Fstab_File_System, Mount_Flags, Mount_Options);
Matt Mower2416a502016-04-12 19:54:46 -0500579
580 bool has_display_name = !Display_Name.empty();
581 bool has_storage_name = !Storage_Name.empty();
582 bool has_backup_name = !Backup_Display_Name.empty();
583 if (!has_display_name) Display_Name = Prev_Display_Name;
584 if (!has_storage_name) Storage_Name = Prev_Storage_Name;
585 if (!has_backup_name) Backup_Display_Name = Prev_Backup_Display_Name;
586
587 if (has_display_name && !has_storage_name)
588 Storage_Name = Display_Name;
589 if (!has_display_name && has_storage_name)
590 Display_Name = Storage_Name;
591 if (has_display_name && !has_backup_name && Backup_Display_Name != "Android Secure")
592 Backup_Display_Name = Display_Name;
593 if (!has_display_name && has_backup_name)
594 Display_Name = Backup_Display_Name;
595 }
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600596
597 if (fstab_version == 2 && twrp_flags && twrp_flags->size() > 0) {
598 it = twrp_flags->find(Mount_Point);
599 if (it != twrp_flags->end()) {
600 char twrpflags[MAX_FSTAB_LINE_LENGTH] = "";
601 int skip = 0;
602 string Flags = it->second.Flags;
603 strcpy(twrpflags, Flags.c_str());
604 if (strlen(twrpflags) > strlen("flags=") && strncmp(twrpflags, "flags=", strlen("flags=")) == 0)
605 skip += strlen("flags=");
606 char* flagptr = twrpflags;
607 flagptr += skip;
608 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
609 }
610 }
nkk7198fc3992017-12-16 16:26:42 +0200611
612 if (Mount_Point == "/persist" && Can_Be_Mounted) {
613 bool mounted = Is_Mounted();
614 if (mounted || Mount(false)) {
615 // Read the backup settings file
nkk7198fc3992017-12-16 16:26:42 +0200616 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 }
633 }
634
Dees_Troy51127312012-09-08 13:08:49 -0400635 return true;
636}
637
Matt Mower72c87ce2016-04-26 14:34:56 -0500638void TWPartition::Partition_Post_Processing(bool Display_Error) {
639 if (Mount_Point == "/data")
640 Setup_Data_Partition(Display_Error);
641 else if (Mount_Point == "/cache")
642 Setup_Cache_Partition(Display_Error);
643}
644
Ethan Yonkerbd7492d2016-12-07 13:55:01 -0600645void TWPartition::ExcludeAll(const string& path) {
646 backup_exclusions.add_absolute_dir(path);
647 wipe_exclusions.add_absolute_dir(path);
648}
649
Matt Mower72c87ce2016-04-26 14:34:56 -0500650void TWPartition::Setup_Data_Partition(bool Display_Error) {
651 if (Mount_Point != "/data")
652 return;
653
654 // Ensure /data is not mounted as tmpfs for qcom hardware decrypt
655 UnMount(false);
656
657#ifdef TW_INCLUDE_CRYPTO
658 if (datamedia)
659 Setup_Data_Media();
660 Can_Be_Encrypted = true;
661 char crypto_blkdev[255];
662 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
663 if (strcmp(crypto_blkdev, "error") != 0) {
664 DataManager::SetValue(TW_IS_DECRYPTED, 1);
665 Is_Encrypted = true;
666 Is_Decrypted = true;
Noah Jacobson5a79f672019-04-28 00:10:07 -0400667 if (Key_Directory.empty()) {
Ethan Yonker93382822018-11-01 15:25:31 -0500668 Is_FBE = false;
Noah Jacobson5a79f672019-04-28 00:10:07 -0400669 DataManager::SetValue(TW_IS_FBE, 0);
670 } else {
Ethan Yonker93382822018-11-01 15:25:31 -0500671 Is_FBE = true;
Noah Jacobson5a79f672019-04-28 00:10:07 -0400672 DataManager::SetValue(TW_IS_FBE, 1);
673 }
Matt Mower72c87ce2016-04-26 14:34:56 -0500674 Decrypted_Block_Device = crypto_blkdev;
675 LOGINFO("Data already decrypted, new block device: '%s'\n", crypto_blkdev);
676 } else if (!Mount(false)) {
677 if (Is_Present) {
Ethan Yonker93382822018-11-01 15:25:31 -0500678 if (Key_Directory.empty()) {
Peter Cai39881ab2019-11-02 19:22:38 +0800679 set_partition_data(Use_Original_Path ? Original_Path.c_str() : Actual_Block_Device.c_str(), Crypto_Key_Location.c_str(),
Noah Jacobson5a79f672019-04-28 00:10:07 -0400680 Fstab_File_System.c_str());
Ethan Yonker93382822018-11-01 15:25:31 -0500681 if (cryptfs_check_footer() == 0) {
682 Is_Encrypted = true;
683 Is_Decrypted = false;
684 Can_Be_Mounted = false;
685 Current_File_System = "emmc";
686 Setup_Image();
687 DataManager::SetValue(TW_IS_ENCRYPTED, 1);
688 DataManager::SetValue(TW_CRYPTO_PWTYPE, cryptfs_get_password_type());
Noah Jacobson5a79f672019-04-28 00:10:07 -0400689 DataManager::SetValue("tw_crypto_pwtype_0", cryptfs_get_password_type());
Ethan Yonker93382822018-11-01 15:25:31 -0500690 DataManager::SetValue(TW_CRYPTO_PASSWORD, "");
691 DataManager::SetValue("tw_crypto_display", "");
692 } else {
693 gui_err("mount_data_footer=Could not mount /data and unable to find crypto footer.");
694 }
695 } else {
Matt Mower72c87ce2016-04-26 14:34:56 -0500696 Is_Encrypted = true;
697 Is_Decrypted = false;
Matt Mower72c87ce2016-04-26 14:34:56 -0500698 }
Ethan Yonker93382822018-11-01 15:25:31 -0500699 } else if (Key_Directory.empty()) {
Noah Jacobson5a79f672019-04-28 00:10:07 -0400700 LOGERR("Primary block device '%s' for mount point '%s' is not present!\n",
701 Primary_Block_Device.c_str(), Mount_Point.c_str());
Matt Mower72c87ce2016-04-26 14:34:56 -0500702 }
703 } else {
Captain Throwback83c22342020-12-29 15:54:33 -0500704 int is_device_fbe;
705 DataManager::GetValue(TW_IS_FBE, is_device_fbe);
706 if (!Decrypt_FBE_DE() && is_device_fbe == 1) {
mauronofrio1db94322019-11-23 23:13:04 +0100707 char wrappedvalue[PROPERTY_VALUE_MAX];
708 property_get("fbe.data.wrappedkey", wrappedvalue, "");
709 std::string wrappedkeyvalue(wrappedvalue);
710 if (wrappedkeyvalue == "true") {
711 LOGERR("Unable to decrypt FBE device\n");
712 } else {
713 LOGINFO("Trying wrapped key.\n");
714 property_set("fbe.data.wrappedkey", "true");
mauronofrio9bf73352019-11-23 22:27:34 +0100715 if (!Decrypt_FBE_DE()) {
Mauronofrio Matarrese6d5d08a2019-12-03 14:25:25 +0100716 LOGINFO("Unable to decrypt device\n");
mauronofrio9bf73352019-11-23 22:27:34 +0100717 }
mauronofrio1db94322019-11-23 23:13:04 +0100718 }
mauronofrio9bf73352019-11-23 22:27:34 +0100719 }
Matt Mower72c87ce2016-04-26 14:34:56 -0500720 }
721 if (datamedia && (!Is_Encrypted || (Is_Encrypted && Is_Decrypted))) {
722 Setup_Data_Media();
723 Recreate_Media_Folder();
724 }
725#else
726 if (datamedia) {
727 Setup_Data_Media();
728 Recreate_Media_Folder();
729 }
730#endif
731}
732
Ethan Yonker93382822018-11-01 15:25:31 -0500733bool TWPartition::Decrypt_FBE_DE() {
734if (TWFunc::Path_Exists("/data/unencrypted/key/version")) {
Captain Throwbackcf0dffc2020-02-17 16:44:50 -0500735 DataManager::SetValue(TW_IS_FBE, 1);
Captain Throwbacka1ad7eb2021-10-06 11:38:18 -0400736 PartitionManager.Set_Crypto_State();
737 PartitionManager.Set_Crypto_Type("file");
Ethan Yonker93382822018-11-01 15:25:31 -0500738 LOGINFO("File Based Encryption is present\n");
739#ifdef TW_INCLUDE_FBE
Noah Jacobson5a79f672019-04-28 00:10:07 -0400740 Is_FBE = true;
Noah Jacobson5a79f672019-04-28 00:10:07 -0400741 ExcludeAll(Mount_Point + "/convert_fbe");
742 ExcludeAll(Mount_Point + "/unencrypted");
743 ExcludeAll(Mount_Point + "/misc/vold/user_keys");
744 ExcludeAll(Mount_Point + "/system/gatekeeper.password.key");
745 ExcludeAll(Mount_Point + "/system/gatekeeper.pattern.key");
746 ExcludeAll(Mount_Point + "/system/locksettings.db");
747 ExcludeAll(Mount_Point + "/system/locksettings.db-wal");
748 ExcludeAll(Mount_Point + "/misc/gatekeeper");
749 ExcludeAll(Mount_Point + "/misc/keystore");
750 ExcludeAll(Mount_Point + "/drm/kek.dat");
751 ExcludeAll(Mount_Point + "/system_de/0/spblob"); // contains data needed to decrypt pixel 2
752 ExcludeAll(Mount_Point + "/system/users/0/gatekeeper.password.key");
753 ExcludeAll(Mount_Point + "/system/users/0/gatekeeper.pattern.key");
Noah Jacobson5a79f672019-04-28 00:10:07 -0400754 int retry_count = 3;
755 while (!Decrypt_DE() && --retry_count)
756 usleep(2000);
Noah Jacobson5a79f672019-04-28 00:10:07 -0400757 if (retry_count > 0) {
Captain Throwbacka1ad7eb2021-10-06 11:38:18 -0400758 PartitionManager.Set_Crypto_State();
Noah Jacobson5a79f672019-04-28 00:10:07 -0400759 Is_Encrypted = true;
760 Is_Decrypted = false;
761 DataManager::SetValue(TW_IS_ENCRYPTED, 1);
762 string filename;
763 int pwd_type = Get_Password_Type(0, filename);
764 if (pwd_type < 0) {
765 LOGERR("This TWRP does not have synthetic password decrypt support\n");
766 pwd_type = 0; // default password
767 }
Noah Jacobsonbf0b0fb2020-06-05 12:47:20 -0400768 PartitionManager.Parse_Users(); // after load_all_de_keys() to parse_users
769 std::vector<users_struct>::iterator iter;
770 std::vector<users_struct>* userList = PartitionManager.Get_Users_List();
771 for (iter = userList->begin(); iter != userList->end(); iter++) {
772 if (atoi((*iter).userId.c_str()) != 0) {
773 ExcludeAll(Mount_Point + "/system_de/" + (*iter).userId + "/spblob");
774 ExcludeAll(Mount_Point + "/system/users/" + (*iter).userId + "/gatekeeper.password.key");
775 ExcludeAll(Mount_Point + "/system/users/" + (*iter).userId + "/gatekeeper.pattern.key");
776 ExcludeAll(Mount_Point + "/system/users/" + (*iter).userId + "/locksettings.db");
777 ExcludeAll(Mount_Point + "/system/users/" + (*iter).userId + "/locksettings.db-wal");
778 }
779 }
Noah Jacobson5a79f672019-04-28 00:10:07 -0400780 DataManager::SetValue(TW_CRYPTO_PWTYPE, pwd_type);
781 DataManager::SetValue("tw_crypto_pwtype_0", pwd_type);
782 DataManager::SetValue(TW_CRYPTO_PASSWORD, "");
783 DataManager::SetValue("tw_crypto_display", "");
784 return true;
785 }
Ethan Yonker93382822018-11-01 15:25:31 -0500786#else
787 LOGERR("FBE found but FBE support not present in TWRP\n");
788#endif
789 }
Captain Throwbackcf0dffc2020-02-17 16:44:50 -0500790 DataManager::SetValue(TW_IS_FBE, 0);
Ethan Yonker93382822018-11-01 15:25:31 -0500791 return false;
792}
793
Matt Mower72c87ce2016-04-26 14:34:56 -0500794void TWPartition::Setup_Cache_Partition(bool Display_Error __unused) {
795 if (Mount_Point != "/cache")
796 return;
797
798 if (!Mount(true))
799 return;
800
801 if (!TWFunc::Path_Exists("/cache/recovery/.")) {
802 LOGINFO("Recreating /cache/recovery folder\n");
803 if (mkdir("/cache/recovery", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0)
804 LOGERR("Could not create /cache/recovery\n");
805 }
806}
807
Matt Mower4ab42b12016-04-21 13:52:18 -0500808void TWPartition::Process_FS_Flags(const char *str) {
809 char *options = strdup(str);
810 char *ptr, *savep;
Hashcode62bd9e02013-11-19 21:59:42 -0800811
Matt Mower4ab42b12016-04-21 13:52:18 -0500812 Mount_Options = "";
Hashcode62bd9e02013-11-19 21:59:42 -0800813
Matt Mower4ab42b12016-04-21 13:52:18 -0500814 // Avoid issues with potentially nested strtok by using strtok_r
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600815 for (ptr = strtok_r(options, ",", &savep); ptr; ptr = strtok_r(NULL, ",", &savep)) {
816 char *equals = strstr(ptr, "=");
817 size_t name_len;
Matt Mower4ab42b12016-04-21 13:52:18 -0500818
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600819 if (!equals)
820 name_len = strlen(ptr);
821 else
822 name_len = equals - ptr;
823
824 // There are some flags that we want to ignore in TWRP
825 bool found_match = false;
826 for (const char** ignored_mount_item = ignored_mount_items; *ignored_mount_item; ignored_mount_item++) {
827 if (strncmp(ptr, *ignored_mount_item, name_len) == 0) {
828 found_match = true;
Hashcode62bd9e02013-11-19 21:59:42 -0800829 break;
830 }
831 }
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600832 if (found_match)
833 continue;
Hashcode62bd9e02013-11-19 21:59:42 -0800834
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600835 // mount_flags are never postfixed by '='
836 if (!equals) {
837 const struct flag_list* mount_flag = mount_flags;
838 for (; mount_flag->name; mount_flag++) {
839 if (strcmp(ptr, mount_flag->name) == 0) {
840 if (mount_flag->flag == MS_RDONLY)
841 Mount_Read_Only = true;
842 else
843 Mount_Flags |= (unsigned)mount_flag->flag;
844 found_match = true;
845 break;
846 }
847 }
848 if (found_match)
849 continue;
Matt Mower4ab42b12016-04-21 13:52:18 -0500850 }
851
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600852 // If we aren't ignoring this flag and it's not a mount flag, then it must be a mount option
853 if (!Mount_Options.empty())
854 Mount_Options += ",";
855 Mount_Options += ptr;
Matt Mower4ab42b12016-04-21 13:52:18 -0500856 }
857 free(options);
Hashcode62bd9e02013-11-19 21:59:42 -0800858}
859
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600860void TWPartition::Save_FS_Flags(const string& local_File_System, int local_Mount_Flags, const string& local_Mount_Options) {
861 partition_fs_flags_struct flags;
862 flags.File_System = local_File_System;
863 flags.Mount_Flags = local_Mount_Flags;
864 flags.Mount_Options = local_Mount_Options;
865 fs_flags.push_back(flags);
866}
867
Matt Mower2416a502016-04-12 19:54:46 -0500868void TWPartition::Apply_TW_Flag(const unsigned flag, const char* str, const bool val) {
869 switch (flag) {
870 case TWFLAG_ANDSEC:
871 Has_Android_Secure = val;
872 break;
873 case TWFLAG_BACKUP:
874 Can_Be_Backed_Up = val;
875 break;
876 case TWFLAG_BACKUPNAME:
877 Backup_Display_Name = str;
878 break;
879 case TWFLAG_BLOCKSIZE:
880 Format_Block_Size = (unsigned long)(atol(str));
881 break;
882 case TWFLAG_CANBEWIPED:
883 Can_Be_Wiped = val;
884 break;
885 case TWFLAG_CANENCRYPTBACKUP:
886 Can_Encrypt_Backup = val;
887 break;
888 case TWFLAG_DEFAULTS:
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600889 case TWFLAG_WAIT:
890 case TWFLAG_VERIFY:
891 case TWFLAG_CHECK:
892 case TWFLAG_NOTRIM:
893 case TWFLAG_VOLDMANAGED:
894 case TWFLAG_RESIZE:
Matt Mower2416a502016-04-12 19:54:46 -0500895 // Do nothing
896 break;
897 case TWFLAG_DISPLAY:
898 Display_Name = str;
899 break;
900 case TWFLAG_ENCRYPTABLE:
901 case TWFLAG_FORCEENCRYPT:
902 Crypto_Key_Location = str;
903 break;
Ethan Yonkere9afc3d2018-08-30 15:16:27 -0500904 case TWFLAG_FILEENCRYPTION:
905 // This flag isn't used by TWRP but is needed in 9.0 FBE decrypt
906 // fileencryption=ice:aes-256-heh
907 {
908 std::string FBE = str;
Ethan Yonkere9afc3d2018-08-30 15:16:27 -0500909 size_t colon_loc = FBE.find(":");
910 if (colon_loc == std::string::npos) {
Ethan Yonker93382822018-11-01 15:25:31 -0500911 property_set("fbe.contents", FBE.c_str());
912 property_set("fbe.filenames", "");
913 LOGINFO("FBE contents '%s', filenames ''\n", FBE.c_str());
Ethan Yonkere9afc3d2018-08-30 15:16:27 -0500914 break;
915 }
Ethan Yonker93382822018-11-01 15:25:31 -0500916 std::string FBE_contents, FBE_filenames;
Ethan Yonkere9afc3d2018-08-30 15:16:27 -0500917 FBE_contents = FBE.substr(0, colon_loc);
918 FBE_filenames = FBE.substr(colon_loc + 1);
919 property_set("fbe.contents", FBE_contents.c_str());
920 property_set("fbe.filenames", FBE_filenames.c_str());
921 LOGINFO("FBE contents '%s', filenames '%s'\n", FBE_contents.c_str(), FBE_filenames.c_str());
922 }
923 break;
Peter Cai40ed06e2019-05-24 11:38:54 +0800924 case TWFLAG_WRAPPEDKEY:
925 // Set fbe.data.wrappedkey to true
926 {
927 property_set("fbe.data.wrappedkey", "true");
928 LOGINFO("FBE wrapped key enabled\n");
929 }
930 break;
Matt Mower2416a502016-04-12 19:54:46 -0500931 case TWFLAG_FLASHIMG:
932 Can_Flash_Img = val;
933 break;
934 case TWFLAG_FSFLAGS:
Matt Mower4ab42b12016-04-21 13:52:18 -0500935 Process_FS_Flags(str);
Matt Mower2416a502016-04-12 19:54:46 -0500936 break;
937 case TWFLAG_IGNOREBLKID:
938 Ignore_Blkid = val;
939 break;
940 case TWFLAG_LENGTH:
941 Length = atoi(str);
942 break;
943 case TWFLAG_MOUNTTODECRYPT:
944 Mount_To_Decrypt = val;
945 break;
946 case TWFLAG_REMOVABLE:
947 Removable = val;
948 break;
949 case TWFLAG_RETAINLAYOUTVERSION:
950 Retain_Layout_Version = val;
951 break;
952 case TWFLAG_SETTINGSSTORAGE:
953 Is_Settings_Storage = val;
954 if (Is_Settings_Storage)
955 Is_Storage = true;
956 break;
957 case TWFLAG_STORAGE:
958 Is_Storage = val;
959 break;
960 case TWFLAG_STORAGENAME:
961 Storage_Name = str;
962 break;
963 case TWFLAG_SUBPARTITIONOF:
964 Is_SubPartition = true;
965 SubPartition_Of = str;
966 break;
967 case TWFLAG_SYMLINK:
968 Symlink_Path = str;
969 break;
970 case TWFLAG_USERDATAENCRYPTBACKUP:
971 Use_Userdata_Encryption = val;
972 if (Use_Userdata_Encryption)
973 Can_Encrypt_Backup = true;
974 break;
975 case TWFLAG_USERMRF:
976 Use_Rm_Rf = val;
977 break;
978 case TWFLAG_WIPEDURINGFACTORYRESET:
979 Wipe_During_Factory_Reset = val;
980 if (Wipe_During_Factory_Reset) {
981 Can_Be_Wiped = true;
982 Wipe_Available_in_GUI = true;
983 }
984 break;
985 case TWFLAG_WIPEINGUI:
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600986 case TWFLAG_FORMATTABLE:
Matt Mower2416a502016-04-12 19:54:46 -0500987 Wipe_Available_in_GUI = val;
988 if (Wipe_Available_in_GUI)
989 Can_Be_Wiped = true;
990 break;
Ethan Yonker1b190162016-12-05 15:25:19 -0600991 case TWFLAG_SLOTSELECT:
992 SlotSelect = true;
993 break;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600994 case TWFLAG_ALTDEVICE:
995 Alternate_Block_Device = str;
996 break;
Peter Caic50044b2019-10-17 08:49:10 +0800997 case TWFLAG_ADOPTED_MOUNT_DELAY:
998 Adopted_Mount_Delay = atoi(str);
999 break;
Ethan Yonker93382822018-11-01 15:25:31 -05001000 case TWFLAG_KEYDIRECTORY:
1001 Key_Directory = str;
Peter Cai39881ab2019-11-02 19:22:38 +08001002 case TWFLAG_DM_USE_ORIGINAL_PATH:
1003 Use_Original_Path = true;
Matt Mower2416a502016-04-12 19:54:46 -05001004 default:
1005 // Should not get here
1006 LOGINFO("Flag identified for processing, but later unmatched: %i\n", flag);
1007 break;
1008 }
1009}
Dees_Troy51127312012-09-08 13:08:49 -04001010
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06001011void TWPartition::Process_TW_Flags(char *flags, bool Display_Error, int fstab_ver) {
Matt Mower2416a502016-04-12 19:54:46 -05001012 char separator[2] = {'\n', 0};
1013 char *ptr, *savep;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06001014 char source_separator = ';';
1015
1016 if (fstab_ver == 2)
1017 source_separator = ',';
Matt Mower2416a502016-04-12 19:54:46 -05001018
1019 // Semicolons within double-quotes are not forbidden, so replace
1020 // only the semicolons intended as separators with '\n' for strtok
1021 for (unsigned i = 0, skip = 0; i < strlen(flags); i++) {
1022 if (flags[i] == '\"')
Dees_Troya13d74f2013-03-24 08:54:55 -05001023 skip = !skip;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06001024 if (!skip && flags[i] == source_separator)
Matt Mower2416a502016-04-12 19:54:46 -05001025 flags[i] = separator[0];
Dees_Troy51127312012-09-08 13:08:49 -04001026 }
1027
Matt Mower2416a502016-04-12 19:54:46 -05001028 // Avoid issues with potentially nested strtok by using strtok_r
1029 ptr = strtok_r(flags, separator, &savep);
1030 while (ptr) {
1031 int ptr_len = strlen(ptr);
1032 const struct flag_list* tw_flag = tw_flags;
Hashcode62bd9e02013-11-19 21:59:42 -08001033
Matt Mower2416a502016-04-12 19:54:46 -05001034 for (; tw_flag->name; tw_flag++) {
1035 int flag_len = strlen(tw_flag->name);
1036
1037 if (strncmp(ptr, tw_flag->name, flag_len) == 0) {
1038 bool flag_val = false;
1039
1040 if (ptr_len > flag_len && (tw_flag->name)[flag_len-1] != '='
1041 && ptr[flag_len] != '=') {
1042 // Handle flags with same starting string
1043 // (e.g. backup and backupname)
1044 continue;
1045 } else if (ptr_len > flag_len && ptr[flag_len] == '=') {
1046 // Handle flags with dual format: Part 1
1047 // (e.g. backup and backup=y. backup=y handled here)
1048 ptr += flag_len + 1;
1049 TWFunc::Strip_Quotes(ptr);
1050 // Skip flags with empty argument
1051 // (e.g. backup=)
1052 if (strlen(ptr) == 0) {
1053 LOGINFO("Flag missing argument or should not include '=': %s=\n", tw_flag->name);
1054 break;
1055 }
1056 flag_val = strchr("yY1", *ptr) != NULL;
1057 } else if (ptr_len == flag_len
1058 && (tw_flag->name)[flag_len-1] == '=') {
1059 // Skip flags missing argument after =
1060 // (e.g. backupname=)
1061 LOGINFO("Flag missing argument: %s\n", tw_flag->name);
1062 break;
1063 } else if (ptr_len > flag_len
1064 && (tw_flag->name)[flag_len-1] == '=') {
1065 // Handle arguments to flags
1066 // (e.g. backupname="My Stuff")
1067 ptr += flag_len;
1068 TWFunc::Strip_Quotes(ptr);
1069 // Skip flags with empty argument
1070 // (e.g. backupname="")
1071 if (strlen(ptr) == 0) {
1072 LOGINFO("Flag missing argument: %s\n", tw_flag->name);
1073 break;
1074 }
1075 } else if (ptr_len == flag_len) {
1076 // Handle flags with dual format: Part 2
1077 // (e.g. backup and backup=y. backup handled here)
1078 flag_val = true;
Ethan Yonker96af84a2015-01-05 14:58:36 -06001079 } else {
Matt Mower2416a502016-04-12 19:54:46 -05001080 LOGINFO("Flag matched, but could not be processed: %s\n", ptr);
1081 break;
Ethan Yonker96af84a2015-01-05 14:58:36 -06001082 }
Matt Mower2416a502016-04-12 19:54:46 -05001083
1084 Apply_TW_Flag(tw_flag->flag, ptr, flag_val);
1085 break;
Ethan Yonker96af84a2015-01-05 14:58:36 -06001086 }
Matt Mower2416a502016-04-12 19:54:46 -05001087 }
1088 if (tw_flag->name == 0) {
Dees_Troy51127312012-09-08 13:08:49 -04001089 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001090 LOGERR("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -04001091 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001092 LOGINFO("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -04001093 }
Matt Mower2416a502016-04-12 19:54:46 -05001094 ptr = strtok_r(NULL, separator, &savep);
Dees_Troy51127312012-09-08 13:08:49 -04001095 }
Dees_Troy51a0e822012-09-05 15:24:24 -04001096}
1097
Dees_Troy5bf43922012-09-07 16:07:55 -04001098bool TWPartition::Is_File_System(string File_System) {
1099 if (File_System == "ext2" ||
Dees_Troy63c8df72012-09-10 14:02:05 -04001100 File_System == "ext3" ||
Dees_Troy5bf43922012-09-07 16:07:55 -04001101 File_System == "ext4" ||
1102 File_System == "vfat" ||
1103 File_System == "ntfs" ||
1104 File_System == "yaffs2" ||
bigbiff bigbiff3e146522012-11-14 14:32:59 -05001105 File_System == "exfat" ||
Dees_Troye5017042013-08-29 16:38:55 +00001106 File_System == "f2fs" ||
Erfan Abdib859e912019-02-05 18:18:46 +02001107 File_System == "squashfs" ||
Dees_Troy5bf43922012-09-07 16:07:55 -04001108 File_System == "auto")
1109 return true;
1110 else
1111 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001112}
1113
Dees_Troy5bf43922012-09-07 16:07:55 -04001114bool TWPartition::Is_Image(string File_System) {
Dees_Troy5fcd8f92012-10-16 12:22:05 -04001115 if (File_System == "emmc" || File_System == "mtd" || File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -04001116 return true;
1117 else
1118 return false;
1119}
1120
Dees_Troy51127312012-09-08 13:08:49 -04001121bool TWPartition::Make_Dir(string Path, bool Display_Error) {
Ethan Yonker93ac7a02016-11-07 22:05:58 -06001122 if (TWFunc::Get_D_Type_From_Stat(Path) != S_IFDIR)
1123 unlink(Path.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001124 if (!TWFunc::Path_Exists(Path)) {
Dees_Troy51127312012-09-08 13:08:49 -04001125 if (mkdir(Path.c_str(), 0777) == -1) {
1126 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001127 gui_msg(Msg(msg::kError, "create_folder_strerr=Can not create '{1}' folder ({2}).")(Path)(strerror(errno)));
Dees_Troy51127312012-09-08 13:08:49 -04001128 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001129 LOGINFO("Can not create '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -04001130 return false;
1131 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001132 LOGINFO("Created '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -04001133 return true;
1134 }
1135 }
1136 return true;
1137}
1138
Dees_Troy5bf43922012-09-07 16:07:55 -04001139void TWPartition::Setup_File_System(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001140 Can_Be_Mounted = true;
1141 Can_Be_Wiped = true;
1142
Dees_Troy5bf43922012-09-07 16:07:55 -04001143 // Make the mount point folder if it doesn't exist
Dees_Troy51127312012-09-08 13:08:49 -04001144 Make_Dir(Mount_Point, Display_Error);
bigbiffce8f83c2015-12-12 18:30:21 -05001145 Backup_Method = BM_FILES;
Dees_Troy5bf43922012-09-07 16:07:55 -04001146}
1147
Ethan Yonker1b190162016-12-05 15:25:19 -06001148void TWPartition::Setup_Image() {
Dees_Troy5bf43922012-09-07 16:07:55 -04001149 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
1150 Backup_Name = Display_Name;
Gary Peck82599a82012-11-21 16:23:12 -08001151 if (Current_File_System == "emmc")
bigbiffce8f83c2015-12-12 18:30:21 -05001152 Backup_Method = BM_DD;
Gary Peck82599a82012-11-21 16:23:12 -08001153 else if (Current_File_System == "mtd" || Current_File_System == "bml")
bigbiffce8f83c2015-12-12 18:30:21 -05001154 Backup_Method = BM_FLASH_UTILS;
Dees_Troy5bf43922012-09-07 16:07:55 -04001155 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001156 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 -04001157}
1158
Dees_Troye58d5262012-09-21 12:27:57 -04001159void TWPartition::Setup_AndSec(void) {
Dees_Troya13d74f2013-03-24 08:54:55 -05001160 Backup_Display_Name = "Android Secure";
Dees_Troye58d5262012-09-21 12:27:57 -04001161 Backup_Name = "and-sec";
Dees_Troya13d74f2013-03-24 08:54:55 -05001162 Can_Be_Backed_Up = true;
Dees_Troye58d5262012-09-21 12:27:57 -04001163 Has_Android_Secure = true;
1164 Symlink_Path = Mount_Point + "/.android_secure";
1165 Symlink_Mount_Point = "/and-sec";
1166 Backup_Path = Symlink_Mount_Point;
1167 Make_Dir("/and-sec", true);
1168 Recreate_AndSec_Folder();
Kjell Braden3126a112016-06-19 16:58:15 +00001169 Mount_Storage_Retry(true);
Dees_Troye58d5262012-09-21 12:27:57 -04001170}
1171
that9e0593e2014-10-08 00:01:24 +02001172void TWPartition::Setup_Data_Media() {
Ethan Yonker6277c792014-09-15 14:54:30 -05001173 LOGINFO("Setting up '%s' as data/media emulated storage.\n", Mount_Point.c_str());
Ethan Yonker66a19492015-12-10 10:19:45 -06001174 if (Storage_Name.empty() || Storage_Name == "Data")
1175 Storage_Name = "Internal Storage";
Ethan Yonker6277c792014-09-15 14:54:30 -05001176 Has_Data_Media = true;
1177 Is_Storage = true;
Ethan Yonker66a19492015-12-10 10:19:45 -06001178 Storage_Path = Mount_Point + "/media";
Ethan Yonker6277c792014-09-15 14:54:30 -05001179 Symlink_Path = Storage_Path;
Ethan Yonker66a19492015-12-10 10:19:45 -06001180 if (Mount_Point == "/data") {
1181 Is_Settings_Storage = true;
1182 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
1183 Make_Dir("/emmc", false);
1184 Symlink_Mount_Point = "/emmc";
1185 } else {
1186 Make_Dir("/sdcard", false);
1187 Symlink_Mount_Point = "/sdcard";
1188 }
1189 if (Mount(false) && TWFunc::Path_Exists(Mount_Point + "/media/0")) {
1190 Storage_Path = Mount_Point + "/media/0";
1191 Symlink_Path = Storage_Path;
1192 DataManager::SetValue(TW_INTERNAL_PATH, Mount_Point + "/media/0");
1193 UnMount(true);
1194 }
1195 DataManager::SetValue("tw_has_internal", 1);
1196 DataManager::SetValue("tw_has_data_media", 1);
Ethan Yonker3fdcda42016-11-30 12:29:37 -06001197 backup_exclusions.add_absolute_dir("/data/data/com.google.android.music/files");
Darth90864aa52020-01-14 20:58:16 +00001198 backup_exclusions.add_absolute_dir("/data/per_boot"); // DJ9,14Jan2020 - exclude this dir to prevent "error 255" on AOSP ROMs that create and lock it
DarthJabba913b07f82020-08-04 10:31:27 +01001199 backup_exclusions.add_absolute_dir("/data/vendor/dumpsys");
bigbiff349ea552020-06-19 16:07:38 -04001200 backup_exclusions.add_absolute_dir("/data/cache");
Ethan Yonker6355b562017-05-01 09:42:17 -05001201 wipe_exclusions.add_absolute_dir(Mount_Point + "/misc/vold"); // adopted storage keys
Ethan Yonker79f88bd2016-12-09 14:52:12 -06001202 ExcludeAll(Mount_Point + "/.layout_version");
1203 ExcludeAll(Mount_Point + "/system/storage.xml");
Ethan Yonker6277c792014-09-15 14:54:30 -05001204 } else {
nkk71f940ba22017-03-24 18:28:03 +02001205 int i;
1206 string path;
1207 for (i = 2; i <= 9; i++) {
1208 path = "/sdcard" + TWFunc::to_string(i);
1209 if (!TWFunc::Path_Exists(path)) {
1210 Make_Dir(path, false);
1211 Symlink_Mount_Point = path;
1212 LOGINFO("'%s' data/media emulated storage symlinked to %s.\n", Mount_Point.c_str(), Symlink_Mount_Point.c_str());
1213 break;
1214 }
1215 }
Ethan Yonker66a19492015-12-10 10:19:45 -06001216 if (Mount(true) && TWFunc::Path_Exists(Mount_Point + "/media/0")) {
1217 Storage_Path = Mount_Point + "/media/0";
1218 Symlink_Path = Storage_Path;
1219 UnMount(true);
1220 }
Ethan Yonker6277c792014-09-15 14:54:30 -05001221 }
Ethan Yonker79f88bd2016-12-09 14:52:12 -06001222 ExcludeAll(Mount_Point + "/media");
Ethan Yonker6277c792014-09-15 14:54:30 -05001223}
1224
Dees_Troy5bf43922012-09-07 16:07:55 -04001225void TWPartition::Find_Real_Block_Device(string& Block, bool Display_Error) {
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06001226 char device[PATH_MAX], realDevice[PATH_MAX];
Dees_Troy5bf43922012-09-07 16:07:55 -04001227
Peter Cai39881ab2019-11-02 19:22:38 +08001228 Original_Path = Block;
1229
Dees_Troy5bf43922012-09-07 16:07:55 -04001230 strcpy(device, Block.c_str());
1231 memset(realDevice, 0, sizeof(realDevice));
1232 while (readlink(device, realDevice, sizeof(realDevice)) > 0)
1233 {
1234 strcpy(device, realDevice);
1235 memset(realDevice, 0, sizeof(realDevice));
1236 }
1237
1238 if (device[0] != '/') {
1239 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001240 LOGERR("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001241 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001242 LOGINFO("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001243 return;
Dees_Troy51a0e822012-09-05 15:24:24 -04001244 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -04001245 Block = device;
1246 return;
1247 }
1248}
1249
Kjell Braden3126a112016-06-19 16:58:15 +00001250bool TWPartition::Mount_Storage_Retry(bool Display_Error) {
Dees_Troy8e337f32012-10-13 22:07:49 -04001251 // On some devices, storage doesn't want to mount right away, retry and sleep
Kjell Braden3126a112016-06-19 16:58:15 +00001252 if (!Mount(Display_Error)) {
Dees_Troy8e337f32012-10-13 22:07:49 -04001253 int retry_count = 5;
1254 while (retry_count > 0 && !Mount(false)) {
1255 usleep(500000);
1256 retry_count--;
1257 }
Kjell Braden3126a112016-06-19 16:58:15 +00001258 return Mount(Display_Error);
Dees_Troy8e337f32012-10-13 22:07:49 -04001259 }
Kjell Braden3126a112016-06-19 16:58:15 +00001260 return true;
Dees_Troy8e337f32012-10-13 22:07:49 -04001261}
1262
Dees_Troy38bd7602012-09-14 13:33:53 -04001263bool TWPartition::Find_MTD_Block_Device(string MTD_Name) {
1264 FILE *fp = NULL;
1265 char line[255];
1266
1267 fp = fopen("/proc/mtd", "rt");
1268 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001269 LOGERR("Device does not support /proc/mtd\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001270 return false;
1271 }
1272
1273 while (fgets(line, sizeof(line), fp) != NULL)
1274 {
1275 char device[32], label[32];
1276 unsigned long size = 0;
Dees_Troy38bd7602012-09-14 13:33:53 -04001277 int deviceId;
1278
1279 sscanf(line, "%s %lx %*s %*c%s", device, &size, label);
1280
1281 // Skip header and blank lines
1282 if ((strcmp(device, "dev:") == 0) || (strlen(line) < 8))
1283 continue;
1284
1285 // Strip off the trailing " from the label
1286 label[strlen(label)-1] = '\0';
1287
1288 if (strcmp(label, MTD_Name.c_str()) == 0) {
1289 // We found our device
1290 // Strip off the trailing : from the device
1291 device[strlen(device)-1] = '\0';
1292 if (sscanf(device,"mtd%d", &deviceId) == 1) {
1293 sprintf(device, "/dev/block/mtdblock%d", deviceId);
1294 Primary_Block_Device = device;
Dees_Troy76543db2013-06-19 16:24:30 +00001295 fclose(fp);
1296 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -04001297 }
1298 }
1299 }
1300 fclose(fp);
1301
1302 return false;
1303}
1304
Dees_Troy51127312012-09-08 13:08:49 -04001305bool TWPartition::Get_Size_Via_statfs(bool Display_Error) {
1306 struct statfs st;
1307 string Local_Path = Mount_Point + "/.";
1308
1309 if (!Mount(Display_Error))
1310 return false;
1311
1312 if (statfs(Local_Path.c_str(), &st) != 0) {
1313 if (!Removable) {
1314 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001315 LOGERR("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -04001316 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001317 LOGINFO("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -04001318 }
1319 return false;
1320 }
1321 Size = (st.f_blocks * st.f_bsize);
1322 Used = ((st.f_blocks - st.f_bfree) * st.f_bsize);
1323 Free = (st.f_bfree * st.f_bsize);
1324 Backup_Size = Used;
1325 return true;
1326}
1327
1328bool TWPartition::Get_Size_Via_df(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001329 FILE* fp;
1330 char command[255], line[512];
1331 int include_block = 1;
1332 unsigned int min_len;
1333
1334 if (!Mount(Display_Error))
1335 return false;
1336
Dees_Troy38bd7602012-09-14 13:33:53 -04001337 min_len = Actual_Block_Device.size() + 2;
Dees_Troy51127312012-09-08 13:08:49 -04001338 sprintf(command, "df %s > /tmp/dfoutput.txt", Mount_Point.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001339 TWFunc::Exec_Cmd(command);
Dees_Troy51127312012-09-08 13:08:49 -04001340 fp = fopen("/tmp/dfoutput.txt", "rt");
1341 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001342 LOGINFO("Unable to open /tmp/dfoutput.txt.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001343 return false;
Dees_Troy51127312012-09-08 13:08:49 -04001344 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001345
1346 while (fgets(line, sizeof(line), fp) != NULL)
1347 {
1348 unsigned long blocks, used, available;
1349 char device[64];
1350 char tmpString[64];
1351
1352 if (strncmp(line, "Filesystem", 10) == 0)
1353 continue;
1354 if (strlen(line) < min_len) {
1355 include_block = 0;
1356 continue;
1357 }
1358 if (include_block) {
1359 sscanf(line, "%s %lu %lu %lu", device, &blocks, &used, &available);
1360 } else {
1361 // The device block string is so long that the df information is on the next line
1362 int space_count = 0;
Dees_Troye58d5262012-09-21 12:27:57 -04001363 sprintf(tmpString, "/dev/block/%s", Actual_Block_Device.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001364 while (tmpString[space_count] == 32)
1365 space_count++;
1366 sscanf(line + space_count, "%lu %lu %lu", &blocks, &used, &available);
1367 }
1368
1369 // Adjust block size to byte size
1370 Size = blocks * 1024ULL;
1371 Used = used * 1024ULL;
1372 Free = available * 1024ULL;
1373 Backup_Size = Used;
1374 }
1375 fclose(fp);
1376 return true;
1377}
1378
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001379unsigned long long TWPartition::IOCTL_Get_Block_Size() {
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001380 Find_Actual_Block_Device();
Ethan Yonker483e9f42016-01-11 22:21:18 -06001381
1382 return TWFunc::IOCTL_Get_Block_Size(Actual_Block_Device.c_str());
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001383}
1384
Dees_Troy5bf43922012-09-07 16:07:55 -04001385bool TWPartition::Find_Partition_Size(void) {
1386 FILE* fp;
1387 char line[512];
1388 string tmpdevice;
1389
igoriok87e3d932013-01-31 21:03:53 +02001390 fp = fopen("/proc/dumchar_info", "rt");
1391 if (fp != NULL) {
1392 while (fgets(line, sizeof(line), fp) != NULL)
1393 {
1394 char label[32], device[32];
1395 unsigned long size = 0;
1396
thatd43bf2d2014-09-21 23:13:02 +02001397 sscanf(line, "%s %lx %*x %*u %s", label, &size, device);
igoriok87e3d932013-01-31 21:03:53 +02001398
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05001399 // Skip header, annotation and blank lines
igoriok87e3d932013-01-31 21:03:53 +02001400 if ((strncmp(device, "/dev/", 5) != 0) || (strlen(line) < 8))
1401 continue;
1402
1403 tmpdevice = "/dev/";
1404 tmpdevice += label;
1405 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
1406 Size = size;
1407 fclose(fp);
1408 return true;
1409 }
1410 }
1411 }
1412
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001413 unsigned long long ioctl_size = IOCTL_Get_Block_Size();
1414 if (ioctl_size) {
1415 Size = ioctl_size;
1416 return true;
1417 }
1418
Dees_Troy5bf43922012-09-07 16:07:55 -04001419 // In this case, we'll first get the partitions we care about (with labels)
1420 fp = fopen("/proc/partitions", "rt");
1421 if (fp == NULL)
1422 return false;
1423
1424 while (fgets(line, sizeof(line), fp) != NULL)
1425 {
1426 unsigned long major, minor, blocks;
1427 char device[512];
Dees_Troy5bf43922012-09-07 16:07:55 -04001428
Dees_Troy63c8df72012-09-10 14:02:05 -04001429 if (strlen(line) < 7 || line[0] == 'm') continue;
Dees_Troy5bf43922012-09-07 16:07:55 -04001430 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
1431
1432 tmpdevice = "/dev/block/";
1433 tmpdevice += device;
Dees_Troy38bd7602012-09-14 13:33:53 -04001434 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001435 // Adjust block size to byte size
1436 Size = blocks * 1024ULL;
1437 fclose(fp);
1438 return true;
1439 }
1440 }
1441 fclose(fp);
1442 return false;
1443}
1444
Dees_Troy5bf43922012-09-07 16:07:55 -04001445bool TWPartition::Is_Mounted(void) {
1446 if (!Can_Be_Mounted)
1447 return false;
1448
1449 struct stat st1, st2;
1450 string test_path;
1451
1452 // Check to see if the mount point directory exists
1453 test_path = Mount_Point + "/.";
1454 if (stat(test_path.c_str(), &st1) != 0) return false;
1455
1456 // Check to see if the directory above the mount point exists
1457 test_path = Mount_Point + "/../.";
1458 if (stat(test_path.c_str(), &st2) != 0) return false;
1459
1460 // Compare the device IDs -- if they match then we're (probably) using tmpfs instead of an actual device
1461 int ret = (st1.st_dev != st2.st_dev) ? true : false;
1462
1463 return ret;
1464}
1465
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05001466bool TWPartition::Is_File_System_Writable(void) {
1467 if (!Is_File_System(Current_File_System) || !Is_Mounted())
1468 return false;
1469
1470 string test_path = Mount_Point + "/.";
1471 return (access(test_path.c_str(), W_OK) == 0);
1472}
1473
Dees_Troy5bf43922012-09-07 16:07:55 -04001474bool TWPartition::Mount(bool Display_Error) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001475 int exfat_mounted = 0;
Martin Dünkelmann6d843782018-12-27 23:40:28 +01001476 unsigned int flags = Mount_Flags;
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001477
Dees_Troy5bf43922012-09-07 16:07:55 -04001478 if (Is_Mounted()) {
1479 return true;
1480 } else if (!Can_Be_Mounted) {
1481 return false;
1482 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001483
1484 Find_Actual_Block_Device();
1485
1486 // Check the current file system before mounting
1487 Check_FS_Type();
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001488 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/exfat-fuse")) {
Dees_Troye34c1332013-02-06 19:13:00 +00001489 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 +00001490 LOGINFO("cmd: %s\n", cmd.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001491 string result;
bigbiff56b02eb2020-07-06 20:24:34 -04001492 if (TWFunc::Exec_Cmd(cmd, result, false) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001493 LOGINFO("exfat-fuse failed to mount with result '%s', trying vfat\n", result.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001494 Current_File_System = "vfat";
1495 } else {
1496#ifdef TW_NO_EXFAT_FUSE
1497 UnMount(false);
1498 // We'll let the kernel handle it but using exfat-fuse to detect if the file system is actually exfat
1499 // Some kernels let us mount vfat as exfat which doesn't work out too well
1500#else
1501 exfat_mounted = 1;
1502#endif
1503 }
1504 }
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001505
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001506 if (Current_File_System == "ntfs" && (TWFunc::Path_Exists("/sbin/ntfs-3g") || TWFunc::Path_Exists("/sbin/mount.ntfs"))) {
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001507 string cmd;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001508 string Ntfsmount_Binary = "";
1509
1510 if (TWFunc::Path_Exists("/sbin/ntfs-3g"))
1511 Ntfsmount_Binary = "ntfs-3g";
1512 else if (TWFunc::Path_Exists("/sbin/mount.ntfs"))
1513 Ntfsmount_Binary = "mount.ntfs";
1514
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001515 if (Mount_Read_Only)
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001516 cmd = "/sbin/" + Ntfsmount_Binary + " -o ro " + Actual_Block_Device + " " + Mount_Point;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001517 else
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001518 cmd = "/sbin/" + Ntfsmount_Binary + " " + Actual_Block_Device + " " + Mount_Point;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001519 LOGINFO("cmd: '%s'\n", cmd.c_str());
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001520
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001521 if (TWFunc::Exec_Cmd(cmd) == 0) {
1522 return true;
1523 } else {
1524 LOGINFO("ntfs-3g failed to mount, trying regular mount method.\n");
1525 }
1526 }
1527
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001528 if (Mount_Read_Only)
1529 flags |= MS_RDONLY;
1530
Dees_Troy22042032012-12-18 21:23:08 +00001531 if (Fstab_File_System == "yaffs2") {
1532 // mount an MTD partition as a YAFFS2 filesystem.
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001533 flags = MS_NOATIME | MS_NODEV | MS_NODIRATIME;
1534 if (Mount_Read_Only)
1535 flags |= MS_RDONLY;
Dees_Troy76543db2013-06-19 16:24:30 +00001536 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags, NULL) < 0) {
1537 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags | MS_RDONLY, NULL) < 0) {
1538 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001539 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy76543db2013-06-19 16:24:30 +00001540 else
1541 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
1542 return false;
1543 } else {
1544 LOGINFO("Mounted '%s' (MTD) as RO\n", Mount_Point.c_str());
1545 return true;
1546 }
1547 } else {
1548 struct stat st;
1549 string test_path = Mount_Point;
1550 if (stat(test_path.c_str(), &st) < 0) {
1551 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001552 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy76543db2013-06-19 16:24:30 +00001553 else
1554 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
1555 return false;
1556 }
1557 mode_t new_mode = st.st_mode | S_IXUSR | S_IXGRP | S_IXOTH;
1558 if (new_mode != st.st_mode) {
1559 LOGINFO("Fixing execute permissions for %s\n", Mount_Point.c_str());
1560 if (chmod(Mount_Point.c_str(), new_mode) < 0) {
1561 if (Display_Error)
1562 LOGERR("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
1563 else
1564 LOGINFO("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
1565 return false;
1566 }
1567 }
Dees_Troy22042032012-12-18 21:23:08 +00001568 return true;
Dees_Troy76543db2013-06-19 16:24:30 +00001569 }
thatc7572eb2015-03-31 18:55:30 +02001570 }
1571
1572 string mount_fs = Current_File_System;
1573 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sys/module/texfat"))
1574 mount_fs = "texfat";
1575
1576 if (!exfat_mounted &&
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001577 mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), mount_fs.c_str(), flags, Mount_Options.c_str()) != 0 &&
1578 mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), mount_fs.c_str(), flags, NULL) != 0) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001579#ifdef TW_NO_EXFAT_FUSE
1580 if (Current_File_System == "exfat") {
Dees_Troy2673cec2013-04-02 20:22:16 +00001581 LOGINFO("Mounting exfat failed, trying vfat...\n");
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001582 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), "vfat", 0, NULL) != 0) {
Dees_Troy85f44ed2013-01-09 18:42:36 +00001583 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001584 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy85f44ed2013-01-09 18:42:36 +00001585 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001586 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001587 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 +00001588 return false;
Dees_Troy85f44ed2013-01-09 18:42:36 +00001589 }
Dees_Troyb05ddee2013-01-28 20:24:50 +00001590 } else {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001591#endif
bigbiff bigbiff26774a02014-03-29 18:22:00 -04001592 if (!Removable && Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001593 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001594 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001595 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
1596 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 +00001597 return false;
1598#ifdef TW_NO_EXFAT_FUSE
Dees_Troy85f44ed2013-01-09 18:42:36 +00001599 }
1600#endif
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001601 }
Ethan Yonker253368a2014-11-25 15:00:52 -06001602
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001603 if (Removable)
1604 Update_Size(Display_Error);
1605
xiaolu9416f4f2015-06-04 08:22:23 +08001606 if (!Symlink_Mount_Point.empty() && TWFunc::Path_Exists(Symlink_Path)) {
codelover2a3d4ce2015-03-14 20:26:49 +08001607 string Command = "mount -o bind '" + Symlink_Path + "' '" + Symlink_Mount_Point + "'";
Vojtech Bocek05534202013-09-11 08:11:56 +02001608 TWFunc::Exec_Cmd(Command);
Dees_Troy5bf43922012-09-07 16:07:55 -04001609 }
Chaosmasterda974802020-01-26 21:09:28 +01001610
1611 if (Mount_Point == "/system_root") {
1612 unlink("/system");
1613 mkdir("/system", 0755);
1614 mount("/system_root/system", "/system", "auto", MS_BIND, NULL);
1615 }
1616
Dees_Troy5bf43922012-09-07 16:07:55 -04001617 return true;
1618}
1619
1620bool TWPartition::UnMount(bool Display_Error) {
Chaosmasterda974802020-01-26 21:09:28 +01001621 if (Mount_Point == "/system_root") {
1622 if (umount("/system") == -1)
1623 umount2("/system", MNT_DETACH);
1624 rmdir("/system");
1625 symlink("/system_root/system", "/system");
1626 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001627 if (Is_Mounted()) {
1628 int never_unmount_system;
1629
1630 DataManager::GetValue(TW_DONT_UNMOUNT_SYSTEM, never_unmount_system);
Captain Throwback9d6feb52018-07-27 10:05:24 -04001631 if (never_unmount_system == 1 && Mount_Point == PartitionManager.Get_Android_Root_Path())
Dees_Troy5bf43922012-09-07 16:07:55 -04001632 return true; // Never unmount system if you're not supposed to unmount it
1633
Matt Mowera8e6d832017-02-14 20:20:59 -06001634 if (Is_Storage && MTP_Storage_ID > 0)
Ethan Yonker726a0202014-12-16 20:01:38 -06001635 PartitionManager.Remove_MTP_Storage(MTP_Storage_ID);
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001636
Dees_Troy38bd7602012-09-14 13:33:53 -04001637 if (!Symlink_Mount_Point.empty())
1638 umount(Symlink_Mount_Point.c_str());
1639
Dees_Troyb05ddee2013-01-28 20:24:50 +00001640 umount(Mount_Point.c_str());
1641 if (Is_Mounted()) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001642 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001643 gui_msg(Msg(msg::kError, "fail_unmount=Failed to unmount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy5bf43922012-09-07 16:07:55 -04001644 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001645 LOGINFO("Unable to unmount '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001646 return false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001647 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -04001648 return true;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001649 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001650 } else {
1651 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001652 }
1653}
1654
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05001655bool TWPartition::ReMount(bool Display_Error) {
1656 if (UnMount(Display_Error))
1657 return Mount(Display_Error);
1658 return false;
1659}
1660
1661bool TWPartition::ReMount_RW(bool Display_Error) {
1662 // No need to remount if already mounted rw
1663 if (Is_File_System_Writable())
1664 return true;
1665
1666 bool ro = Mount_Read_Only;
1667 int flags = Mount_Flags;
1668
1669 Mount_Read_Only = false;
1670 Mount_Flags &= ~MS_RDONLY;
1671
1672 bool ret = ReMount(Display_Error);
1673
1674 Mount_Read_Only = ro;
1675 Mount_Flags = flags;
1676
1677 return ret;
1678}
1679
Gary Peck43acadf2012-11-21 21:19:01 -08001680bool TWPartition::Wipe(string New_File_System) {
Ethan Yonker726a0202014-12-16 20:01:38 -06001681 bool wiped = false, update_crypt = false, recreate_media = true;
Dees_Troy16c2b312013-01-15 16:51:18 +00001682 int check;
1683 string Layout_Filename = Mount_Point + "/.layout_version";
1684
Dees_Troy38bd7602012-09-14 13:33:53 -04001685 if (!Can_Be_Wiped) {
Matt Mower3c366972015-12-25 19:28:31 -06001686 gui_msg(Msg(msg::kError, "cannot_wipe=Partition {1} cannot be wiped.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04001687 return false;
1688 }
1689
Dees_Troyc51f1f92012-09-20 15:32:13 -04001690 if (Mount_Point == "/cache")
Dees_Troy2673cec2013-04-02 20:22:16 +00001691 Log_Offset = 0;
Dees_Troyc51f1f92012-09-20 15:32:13 -04001692
bigbiff35d2bfd2021-01-23 14:08:03 -05001693 if (Mount_Point == PartitionManager.Get_Android_Root_Path()) {
1694 if (tw_get_default_metadata(PartitionManager.Get_Android_Root_Path().c_str()) != 0) {
1695 gui_msg(Msg(msg::kWarning, "restore_system_context=Unable to get default context for {1} -- Android may not boot.")(PartitionManager.Get_Android_Root_Path()));
1696 }
1697 }
1698
Dees_Troy16c2b312013-01-15 16:51:18 +00001699 if (Retain_Layout_Version && Mount(false) && TWFunc::Path_Exists(Layout_Filename))
1700 TWFunc::copy_file(Layout_Filename, "/.layout_version", 0600);
1701 else
1702 unlink("/.layout_version");
Dees_Troy38bd7602012-09-14 13:33:53 -04001703
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001704 if (Has_Data_Media && Current_File_System == New_File_System) {
Dees_Troy16c2b312013-01-15 16:51:18 +00001705 wiped = Wipe_Data_Without_Wiping_Media();
bigbiff349ea552020-06-19 16:07:38 -04001706 if (Mount_Point == "/data" && TWFunc::get_log_dir() == DATA_LOGS_DIR) {
1707 bool created = Recreate_Logs_Dir();
1708 if (!created)
1709 LOGERR("Unable to create log directory for TWRP\n");
bigbiffa2bd7b72020-05-07 21:45:34 -04001710 }
Ethan Yonker5eac2222014-06-11 12:22:55 -05001711 recreate_media = false;
Dees_Troy16c2b312013-01-15 16:51:18 +00001712 } else {
Dees_Troy16c2b312013-01-15 16:51:18 +00001713 DataManager::GetValue(TW_RM_RF_VAR, check);
1714
Hashcodedabfd492013-08-29 22:45:30 -07001715 if (check || Use_Rm_Rf)
Dees_Troy16c2b312013-01-15 16:51:18 +00001716 wiped = Wipe_RMRF();
1717 else if (New_File_System == "ext4")
1718 wiped = Wipe_EXT4();
1719 else if (New_File_System == "ext2" || New_File_System == "ext3")
dianlujitao4879b372018-12-03 18:45:47 +08001720 wiped = Wipe_EXTFS(New_File_System);
Dees_Troy16c2b312013-01-15 16:51:18 +00001721 else if (New_File_System == "vfat")
1722 wiped = Wipe_FAT();
1723 else if (New_File_System == "exfat")
1724 wiped = Wipe_EXFAT();
1725 else if (New_File_System == "yaffs2")
1726 wiped = Wipe_MTD();
Dees_Troye5017042013-08-29 16:38:55 +00001727 else if (New_File_System == "f2fs")
1728 wiped = Wipe_F2FS();
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001729 else if (New_File_System == "ntfs")
1730 wiped = Wipe_NTFS();
Dees_Troy16c2b312013-01-15 16:51:18 +00001731 else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001732 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 +00001733 unlink("/.layout_version");
1734 return false;
1735 }
1736 update_crypt = wiped;
Gary Pecke8bc5d72012-12-21 06:45:25 -08001737 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001738
Gary Pecke8bc5d72012-12-21 06:45:25 -08001739 if (wiped) {
Mohd Faraz25371a72020-06-15 00:28:32 +05301740 if (Mount_Point == "/cache" && TWFunc::get_log_dir() != DATA_LOGS_DIR)
1741 DataManager::Output_Version();
1742
bigbiff35d2bfd2021-01-23 14:08:03 -05001743 if (Mount_Point == PartitionManager.Get_Android_Root_Path()) {
1744 tw_set_default_metadata(PartitionManager.Get_Android_Root_Path().c_str());
1745 }
Dees_Troy16c2b312013-01-15 16:51:18 +00001746 if (TWFunc::Path_Exists("/.layout_version") && Mount(false))
1747 TWFunc::copy_file("/.layout_version", Layout_Filename, 0600);
1748
1749 if (update_crypt) {
1750 Setup_File_System(false);
1751 if (Is_Encrypted && !Is_Decrypted) {
1752 // just wiped an encrypted partition back to its unencrypted state
1753 Is_Encrypted = false;
1754 Is_Decrypted = false;
1755 Decrypted_Block_Device = "";
1756 if (Mount_Point == "/data") {
1757 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1758 DataManager::SetValue(TW_IS_DECRYPTED, 0);
1759 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001760 }
1761 }
Ethan Yonker5eac2222014-06-11 12:22:55 -05001762
Captain Throwbackdfeab0d2021-10-02 20:47:33 -04001763 if (Is_Storage && Mount(false) && !Is_FBE)
Matt Mower209c9632016-01-20 12:08:35 -06001764 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Gary Pecke8bc5d72012-12-21 06:45:25 -08001765 }
Matt Mower209c9632016-01-20 12:08:35 -06001766
Gary Pecke8bc5d72012-12-21 06:45:25 -08001767 return wiped;
Dees_Troy51a0e822012-09-05 15:24:24 -04001768}
1769
Gary Peck43acadf2012-11-21 21:19:01 -08001770bool TWPartition::Wipe() {
Gary Peck82599a82012-11-21 16:23:12 -08001771 if (Is_File_System(Current_File_System))
1772 return Wipe(Current_File_System);
1773 else
1774 return Wipe(Fstab_File_System);
Gary Peck43acadf2012-11-21 21:19:01 -08001775}
1776
Dees_Troye58d5262012-09-21 12:27:57 -04001777bool TWPartition::Wipe_AndSec(void) {
1778 if (!Has_Android_Secure)
1779 return false;
1780
Dees_Troye58d5262012-09-21 12:27:57 -04001781 if (!Mount(true))
1782 return false;
1783
Ethan Yonker74db1572015-10-28 12:44:49 -05001784 gui_msg(Msg("wiping=Wiping {1}")(Backup_Display_Name));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001785 TWFunc::removeDir(Mount_Point + "/.android_secure/", true);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001786 return true;
Dees_Troye58d5262012-09-21 12:27:57 -04001787}
1788
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001789bool TWPartition::Can_Repair() {
Ethan Yonkera2719152015-05-28 09:44:41 -05001790 if (Mount_Read_Only)
1791 return false;
Matt Mower18794c82015-11-11 16:22:45 -06001792 if (Current_File_System == "vfat" && TWFunc::Path_Exists("/sbin/fsck.fat"))
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001793 return true;
1794 else if ((Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") && TWFunc::Path_Exists("/sbin/e2fsck"))
1795 return true;
1796 else if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/fsck.exfat"))
1797 return true;
1798 else if (Current_File_System == "f2fs" && TWFunc::Path_Exists("/sbin/fsck.f2fs"))
1799 return true;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001800 else if (Current_File_System == "ntfs" && (TWFunc::Path_Exists("/sbin/ntfsfix") || TWFunc::Path_Exists("/sbin/fsck.ntfs")))
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001801 return true;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001802 return false;
1803}
1804
1805bool TWPartition::Repair() {
1806 string command;
1807
1808 if (Current_File_System == "vfat") {
Matt Mower18794c82015-11-11 16:22:45 -06001809 if (!TWFunc::Path_Exists("/sbin/fsck.fat")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001810 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.fat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001811 return false;
1812 }
1813 if (!UnMount(true))
1814 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001815 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.fat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001816 Find_Actual_Block_Device();
Matt Mower18794c82015-11-11 16:22:45 -06001817 command = "/sbin/fsck.fat -y " + Actual_Block_Device;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001818 LOGINFO("Repair command: %s\n", command.c_str());
1819 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001820 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001821 return true;
1822 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001823 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001824 return false;
1825 }
1826 }
1827 if (Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") {
1828 if (!TWFunc::Path_Exists("/sbin/e2fsck")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001829 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("e2fsck"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001830 return false;
1831 }
1832 if (!UnMount(true))
1833 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001834 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("e2fsck"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001835 Find_Actual_Block_Device();
Ethan Yonkera2719152015-05-28 09:44:41 -05001836 command = "/sbin/e2fsck -fp " + Actual_Block_Device;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001837 LOGINFO("Repair command: %s\n", command.c_str());
1838 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001839 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001840 return true;
1841 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001842 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001843 return false;
1844 }
1845 }
1846 if (Current_File_System == "exfat") {
1847 if (!TWFunc::Path_Exists("/sbin/fsck.exfat")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001848 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.exfat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001849 return false;
1850 }
1851 if (!UnMount(true))
1852 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001853 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.exfat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001854 Find_Actual_Block_Device();
1855 command = "/sbin/fsck.exfat " + Actual_Block_Device;
1856 LOGINFO("Repair command: %s\n", command.c_str());
1857 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001858 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001859 return true;
1860 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001861 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001862 return false;
1863 }
1864 }
1865 if (Current_File_System == "f2fs") {
1866 if (!TWFunc::Path_Exists("/sbin/fsck.f2fs")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001867 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.f2fs"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001868 return false;
1869 }
1870 if (!UnMount(true))
1871 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001872 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.f2fs"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001873 Find_Actual_Block_Device();
1874 command = "/sbin/fsck.f2fs " + Actual_Block_Device;
1875 LOGINFO("Repair command: %s\n", command.c_str());
1876 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001877 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001878 return true;
1879 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001880 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001881 return false;
1882 }
1883 }
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001884 if (Current_File_System == "ntfs") {
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001885 string Ntfsfix_Binary;
1886 if (TWFunc::Path_Exists("/sbin/ntfsfix"))
1887 Ntfsfix_Binary = "ntfsfix";
1888 else if (TWFunc::Path_Exists("/sbin/fsck.ntfs"))
1889 Ntfsfix_Binary = "fsck.ntfs";
1890 else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001891 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("ntfsfix"));
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001892 return false;
1893 }
1894 if (!UnMount(true))
1895 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001896 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)(Ntfsfix_Binary));
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001897 Find_Actual_Block_Device();
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001898 command = "/sbin/" + Ntfsfix_Binary + " " + Actual_Block_Device;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001899 LOGINFO("Repair command: %s\n", command.c_str());
1900 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001901 gui_msg("done=Done.");
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001902 return true;
1903 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001904 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001905 return false;
1906 }
1907 }
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001908 return false;
1909}
1910
Ethan Yonkera2719152015-05-28 09:44:41 -05001911bool TWPartition::Can_Resize() {
1912 if (Mount_Read_Only)
1913 return false;
1914 if ((Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") && TWFunc::Path_Exists("/sbin/resize2fs"))
1915 return true;
1916 return false;
1917}
1918
1919bool TWPartition::Resize() {
1920 string command;
1921
1922 if (Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") {
1923 if (!Can_Repair()) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001924 LOGINFO("Cannot resize %s because %s cannot be repaired before resizing.\n", Display_Name.c_str(), Display_Name.c_str());
1925 gui_msg(Msg(msg::kError, "cannot_resize=Cannot resize {1}.")(Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001926 return false;
1927 }
1928 if (!TWFunc::Path_Exists("/sbin/resize2fs")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001929 LOGINFO("resize2fs does not exist! Cannot resize!\n");
1930 gui_msg(Msg(msg::kError, "cannot_resize=Cannot resize {1}.")(Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001931 return false;
1932 }
1933 // Repair will unmount so no need to do it twice
Ethan Yonker74db1572015-10-28 12:44:49 -05001934 gui_msg(Msg("repair_resize=Repairing {1} before resizing.")( Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001935 if (!Repair())
1936 return false;
Ethan Yonker74db1572015-10-28 12:44:49 -05001937 gui_msg(Msg("resizing=Resizing {1} using {2}...")(Display_Name)("resize2fs"));
Ethan Yonkera2719152015-05-28 09:44:41 -05001938 Find_Actual_Block_Device();
1939 command = "/sbin/resize2fs " + Actual_Block_Device;
1940 if (Length != 0) {
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001941 unsigned long long Actual_Size = IOCTL_Get_Block_Size();
1942 if (Actual_Size == 0)
1943 return false;
Ethan Yonkera2719152015-05-28 09:44:41 -05001944
Ethan Yonkera2719152015-05-28 09:44:41 -05001945 unsigned long long Block_Count;
1946 if (Length < 0) {
1947 // Reduce overall size by this length
1948 Block_Count = (Actual_Size / 1024LLU) - ((unsigned long long)(Length * -1) / 1024LLU);
1949 } else {
1950 // This is the size, not a size reduction
1951 Block_Count = ((unsigned long long)(Length) / 1024LLU);
1952 }
1953 char temp[256];
1954 sprintf(temp, "%llu", Block_Count);
1955 command += " ";
1956 command += temp;
1957 command += "K";
1958 }
1959 LOGINFO("Resize command: %s\n", command.c_str());
1960 if (TWFunc::Exec_Cmd(command) == 0) {
1961 Update_Size(true);
Ethan Yonker74db1572015-10-28 12:44:49 -05001962 gui_msg("done=Done.");
Ethan Yonkera2719152015-05-28 09:44:41 -05001963 return true;
1964 } else {
1965 Update_Size(true);
Ethan Yonker74db1572015-10-28 12:44:49 -05001966 gui_msg(Msg(msg::kError, "unable_resize=Unable to resize {1}.")(Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001967 return false;
1968 }
1969 }
1970 return false;
1971}
1972
bigbiffce8f83c2015-12-12 18:30:21 -05001973bool TWPartition::Backup(PartitionSettings *part_settings, pid_t *tar_fork_pid) {
1974 if (Backup_Method == BM_FILES)
1975 return Backup_Tar(part_settings, tar_fork_pid);
1976 else if (Backup_Method == BM_DD)
1977 return Backup_Image(part_settings);
1978 else if (Backup_Method == BM_FLASH_UTILS)
1979 return Backup_Dump_Image(part_settings);
Dees_Troy2673cec2013-04-02 20:22:16 +00001980 LOGERR("Unknown backup method for '%s'\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001981 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001982}
1983
bigbiffce8f83c2015-12-12 18:30:21 -05001984bool TWPartition::Restore(PartitionSettings *part_settings) {
Matt Mower3c366972015-12-25 19:28:31 -06001985 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, gui_parse_text("{@restoring_hdr}"));
Ethan Yonkere080c1f2016-09-19 13:50:25 -05001986 LOGINFO("Restore filename is: %s/%s\n", part_settings->Backup_Folder.c_str(), Backup_FileName.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001987
bigbiffce8f83c2015-12-12 18:30:21 -05001988 string Restore_File_System = Get_Restore_File_System(part_settings);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001989
1990 if (Is_File_System(Restore_File_System))
bigbiffce8f83c2015-12-12 18:30:21 -05001991 return Restore_Tar(part_settings);
1992 else if (Is_Image(Restore_File_System))
1993 return Restore_Image(part_settings);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001994
1995 LOGERR("Unknown restore method for '%s'\n", Mount_Point.c_str());
1996 return false;
1997}
1998
bigbiffce8f83c2015-12-12 18:30:21 -05001999string TWPartition::Get_Restore_File_System(PartitionSettings *part_settings) {
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002000 size_t first_period, second_period;
2001 string Restore_File_System;
2002
Gary Peck43acadf2012-11-21 21:19:01 -08002003 // Parse backup filename to extract the file system before wiping
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002004 first_period = Backup_FileName.find(".");
Gary Peck43acadf2012-11-21 21:19:01 -08002005 if (first_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00002006 LOGERR("Unable to find file system (first period).\n");
thatd43bf2d2014-09-21 23:13:02 +02002007 return string();
Gary Peck43acadf2012-11-21 21:19:01 -08002008 }
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002009 Restore_File_System = Backup_FileName.substr(first_period + 1, Backup_FileName.size() - first_period - 1);
Gary Peck43acadf2012-11-21 21:19:01 -08002010 second_period = Restore_File_System.find(".");
2011 if (second_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00002012 LOGERR("Unable to find file system (second period).\n");
thatd43bf2d2014-09-21 23:13:02 +02002013 return string();
Gary Peck43acadf2012-11-21 21:19:01 -08002014 }
2015 Restore_File_System.resize(second_period);
Matt Mowera0cd91d2016-12-30 18:21:42 -06002016 LOGINFO("Restore file system is: '%s'.\n", Restore_File_System.c_str());
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002017 return Restore_File_System;
Dees_Troy51a0e822012-09-05 15:24:24 -04002018}
2019
2020string TWPartition::Backup_Method_By_Name() {
bigbiffce8f83c2015-12-12 18:30:21 -05002021 if (Backup_Method == BM_NONE)
Dees_Troy38bd7602012-09-14 13:33:53 -04002022 return "none";
bigbiffce8f83c2015-12-12 18:30:21 -05002023 else if (Backup_Method == BM_FILES)
Dees_Troy38bd7602012-09-14 13:33:53 -04002024 return "files";
bigbiffce8f83c2015-12-12 18:30:21 -05002025 else if (Backup_Method == BM_DD)
Dees_Troy38bd7602012-09-14 13:33:53 -04002026 return "dd";
bigbiffce8f83c2015-12-12 18:30:21 -05002027 else if (Backup_Method == BM_FLASH_UTILS)
Dees_Troy38bd7602012-09-14 13:33:53 -04002028 return "flash_utils";
2029 else
2030 return "undefined";
2031 return "ERROR!";
Dees_Troy51a0e822012-09-05 15:24:24 -04002032}
2033
2034bool TWPartition::Decrypt(string Password) {
Dees_Troy2673cec2013-04-02 20:22:16 +00002035 LOGINFO("STUB TWPartition::Decrypt, password: '%s'\n", Password.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04002036 // Is this needed?
Dees_Troy51a0e822012-09-05 15:24:24 -04002037 return 1;
2038}
2039
2040bool TWPartition::Wipe_Encryption() {
Dees_Troy38bd7602012-09-14 13:33:53 -04002041 bool Save_Data_Media = Has_Data_Media;
Ethan Yonker93382822018-11-01 15:25:31 -05002042 bool ret = false;
2043 BasePartition* base_partition = make_partition();
2044
2045 if (!base_partition->PreWipeEncryption())
2046 goto exit;
Dees_Troy38bd7602012-09-14 13:33:53 -04002047
dianlujitao4879b372018-12-03 18:45:47 +08002048 Find_Actual_Block_Device();
2049 if (!Is_Present) {
2050 LOGINFO("Block device not present, cannot format %s.\n", Display_Name.c_str());
2051 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
2052 return false;
2053 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002054
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06002055#ifdef TW_INCLUDE_CRYPTO
bigbiffa2bd7b72020-05-07 21:45:34 -04002056 if (!UnMount(true))
2057 return false;
Ethan Yonker1b190162016-12-05 15:25:19 -06002058 if (Is_Decrypted && !Decrypted_Block_Device.empty()) {
Matt Mower2b18a532015-02-20 16:58:05 -06002059 if (delete_crypto_blk_dev((char*)("userdata")) != 0) {
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06002060 LOGERR("Error deleting crypto block device, continuing anyway.\n");
2061 }
2062 }
2063#endif
dianlujitao4879b372018-12-03 18:45:47 +08002064 Has_Data_Media = false;
Ethan Yonker93382822018-11-01 15:25:31 -05002065 Decrypted_Block_Device = "";
Dees_Troy74fb2e92013-04-15 14:35:47 +00002066 Is_Decrypted = false;
2067 Is_Encrypted = false;
Gary Pecke8bc5d72012-12-21 06:45:25 -08002068 if (Wipe(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002069 Has_Data_Media = Save_Data_Media;
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06002070 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
Ethan Yonker83e82572014-04-04 10:59:28 -05002071#ifndef TW_OEM_BUILD
bigbiff349ea552020-06-19 16:07:38 -04002072 gui_msg("format_data_msg=You may need to reboot recovery to be able to use /data again.");
Ethan Yonker83e82572014-04-04 10:59:28 -05002073#endif
bigbiff0f7f9a12021-07-09 20:08:44 -04002074 if (Is_FBE) {
2075 gui_msg(Msg(msg::kWarning, "data_media_fbe_msg=TWRP will not recreate /data/media on an FBE device. Please reboot into your rom to create /data/media."));
Captain Throwbackdfeab0d2021-10-02 20:47:33 -04002076 } else {
2077 if (Has_Data_Media && !Symlink_Mount_Point.empty()) {
2078 Recreate_Media_Folder();
2079 if (Mount(false))
2080 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
2081 }
bigbiff0f7f9a12021-07-09 20:08:44 -04002082 }
2083
Ethan Yonker93382822018-11-01 15:25:31 -05002084 ret = true;
2085 if (!Key_Directory.empty())
2086 ret = PartitionManager.Wipe_By_Path(Key_Directory);
2087 if (ret)
2088 ret = base_partition->PostWipeEncryption();
2089 goto exit;
Dees_Troy38bd7602012-09-14 13:33:53 -04002090 } else {
2091 Has_Data_Media = Save_Data_Media;
Ethan Yonker74db1572015-10-28 12:44:49 -05002092 gui_err("format_data_err=Unable to format to remove encryption.");
Ethan Yonker726a0202014-12-16 20:01:38 -06002093 if (Has_Data_Media && Mount(false))
2094 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Ethan Yonker93382822018-11-01 15:25:31 -05002095 goto exit;
Dees_Troy38bd7602012-09-14 13:33:53 -04002096 }
Ethan Yonker93382822018-11-01 15:25:31 -05002097exit:
2098 delete base_partition;
2099 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04002100}
2101
2102void TWPartition::Check_FS_Type() {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05002103 const char* type;
2104 blkid_probe pr;
Dees_Troy5bf43922012-09-07 16:07:55 -04002105
Dees_Troy68cab492012-12-12 19:29:35 +00002106 if (Fstab_File_System == "yaffs2" || Fstab_File_System == "mtd" || Fstab_File_System == "bml" || Ignore_Blkid)
2107 return; // Running blkid on some mtd devices causes a massive crash or needs to be skipped
Dees_Troy5bf43922012-09-07 16:07:55 -04002108
Dees_Troy38bd7602012-09-14 13:33:53 -04002109 Find_Actual_Block_Device();
Dees_Troy8170a922012-09-18 15:40:25 -04002110 if (!Is_Present)
2111 return;
Dees_Troy51127312012-09-08 13:08:49 -04002112
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05002113 pr = blkid_new_probe_from_filename(Actual_Block_Device.c_str());
2114 if (blkid_do_fullprobe(pr)) {
2115 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00002116 LOGINFO("Can't probe device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05002117 return;
Dees_Troy5bf43922012-09-07 16:07:55 -04002118 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02002119
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05002120 if (blkid_probe_lookup_value(pr, "TYPE", &type, NULL) < 0) {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05002121 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00002122 LOGINFO("can't find filesystem on device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05002123 return;
2124 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02002125
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05002126 Current_File_System = type;
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02002127 blkid_free_probe(pr);
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06002128 if (fs_flags.size() > 1) {
2129 std::vector<partition_fs_flags_struct>::iterator iter;
2130 std::vector<partition_fs_flags_struct>::iterator found = fs_flags.begin();
2131
2132 for (iter = fs_flags.begin(); iter != fs_flags.end(); iter++) {
2133 if (iter->File_System == Current_File_System) {
2134 found = iter;
2135 break;
2136 }
2137 }
2138 // If we don't find a match, we default the flags to the first set of flags that we received from the fstab
2139 if (Mount_Flags != found->Mount_Flags || Mount_Options != found->Mount_Options) {
2140 Mount_Flags = found->Mount_Flags;
2141 Mount_Options = found->Mount_Options;
2142 LOGINFO("Mount_Flags: %i, Mount_Options: %s\n", Mount_Flags, Mount_Options.c_str());
2143 }
2144 }
Dees_Troy51a0e822012-09-05 15:24:24 -04002145}
2146
dianlujitao4879b372018-12-03 18:45:47 +08002147bool TWPartition::Wipe_EXTFS(string File_System) {
bigbiffa2bd7b72020-05-07 21:45:34 -04002148 if (!UnMount(true))
2149 return false;
2150
dianlujitao4879b372018-12-03 18:45:47 +08002151#if PLATFORM_SDK_VERSION < 28
2152 if (!TWFunc::Path_Exists("/sbin/mke2fs"))
2153#else
2154 if (!TWFunc::Path_Exists("/sbin/mke2fs") || !TWFunc::Path_Exists("/sbin/e2fsdroid"))
2155#endif
Dees_Troy38bd7602012-09-14 13:33:53 -04002156 return Wipe_RMRF();
2157
dianlujitao4879b372018-12-03 18:45:47 +08002158 int ret;
2159 bool NeedPreserveFooter = true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002160
Ethan Yonker25f20c12014-10-14 09:04:43 -05002161 Find_Actual_Block_Device();
2162 if (!Is_Present) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002163 LOGINFO("Block device not present, cannot wipe %s.\n", Display_Name.c_str());
2164 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Ethan Yonker25f20c12014-10-14 09:04:43 -05002165 return false;
2166 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002167 if (!UnMount(true))
2168 return false;
2169
dianlujitao4879b372018-12-03 18:45:47 +08002170 /**
2171 * On decrypted devices, IOCTL_Get_Block_Size calculates size on device mapper,
2172 * so there's no need to preserve footer.
2173 */
2174 if ((Is_Decrypted && !Decrypted_Block_Device.empty()) ||
2175 Crypto_Key_Location != "footer") {
2176 NeedPreserveFooter = false;
2177 }
2178
2179 unsigned long long dev_sz = TWFunc::IOCTL_Get_Block_Size(Actual_Block_Device.c_str());
2180 if (!dev_sz)
2181 return false;
2182
2183 if (NeedPreserveFooter)
2184 Length < 0 ? dev_sz += Length : dev_sz -= CRYPT_FOOTER_OFFSET;
2185
Dees Troy9a4d7402019-03-21 14:17:28 -04002186 char dout[16];
2187 sprintf(dout, "%llu", dev_sz / 4096);
2188
2189 //string size_str =to_string(dev_sz / 4096);
2190 string size_str = dout;
dianlujitao4879b372018-12-03 18:45:47 +08002191 string Command;
2192
2193 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mke2fs"));
2194
2195 // Execute mke2fs to create empty ext4 filesystem
2196 Command = "mke2fs -t " + File_System + " -b 4096 " + Actual_Block_Device + " " + size_str;
2197 LOGINFO("mke2fs command: %s\n", Command.c_str());
2198 ret = TWFunc::Exec_Cmd(Command);
2199 if (ret) {
2200 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
2201 return false;
2202 }
2203
2204 if (TWFunc::Path_Exists("/sbin/e2fsdroid")) {
dianlujitao3cf05a52019-05-15 15:33:27 +08002205 const string& File_Contexts_Entry = (Mount_Point == "/system_root" ? "/" : Mount_Point);
2206 char *secontext = NULL;
2207 if (!selinux_handle || selabel_lookup(selinux_handle, &secontext, File_Contexts_Entry.c_str(), S_IFDIR) < 0) {
2208 LOGINFO("Cannot lookup security context for '%s'\n", Mount_Point.c_str());
2209 } else {
2210 // Execute e2fsdroid to initialize selinux context
2211 Command = "e2fsdroid -e -S /file_contexts -a " + File_Contexts_Entry + " " + Actual_Block_Device;
2212 LOGINFO("e2fsdroid command: %s\n", Command.c_str());
2213 ret = TWFunc::Exec_Cmd(Command);
2214 if (ret) {
2215 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
2216 return false;
2217 }
dianlujitao4879b372018-12-03 18:45:47 +08002218 }
2219 } else {
2220 LOGINFO("e2fsdroid not present\n");
2221 }
2222
2223 if (NeedPreserveFooter)
2224 Wipe_Crypto_Key();
2225 Current_File_System = File_System;
2226 Recreate_AndSec_Folder();
2227 gui_msg("done=Done.");
2228 return true;
2229}
2230
2231bool TWPartition::Wipe_EXT4() {
2232#ifdef USE_EXT4
Ethan Yonkerf27497f2014-02-09 11:48:33 -06002233 int ret;
dianlujitao4879b372018-12-03 18:45:47 +08002234 bool NeedPreserveFooter = true;
2235
2236 Find_Actual_Block_Device();
2237 if (!Is_Present) {
2238 LOGINFO("Block device not present, cannot wipe %s.\n", Display_Name.c_str());
2239 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
2240 return false;
2241 }
2242 if (!UnMount(true))
2243 return false;
2244
2245 /**
2246 * On decrypted devices, IOCTL_Get_Block_Size calculates size on device mapper,
2247 * so there's no need to preserve footer.
2248 */
2249 if ((Is_Decrypted && !Decrypted_Block_Device.empty()) ||
2250 Crypto_Key_Location != "footer") {
2251 NeedPreserveFooter = false;
2252 }
2253
2254 unsigned long long dev_sz = TWFunc::IOCTL_Get_Block_Size(Actual_Block_Device.c_str());
2255 if (!dev_sz)
2256 return false;
2257
2258 if (NeedPreserveFooter)
2259 Length < 0 ? dev_sz += Length : dev_sz -= CRYPT_FOOTER_OFFSET;
2260
Ethan Yonkerf27497f2014-02-09 11:48:33 -06002261 char *secontext = NULL;
2262
Greg Wallace4b44fef2015-12-29 15:33:24 -05002263 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("make_ext4fs"));
Ethan Yonkerf27497f2014-02-09 11:48:33 -06002264
Dees Troy99c8dbf2014-03-10 16:53:58 +00002265 if (!selinux_handle || selabel_lookup(selinux_handle, &secontext, Mount_Point.c_str(), S_IFDIR) < 0) {
Ethan Yonkerf27497f2014-02-09 11:48:33 -06002266 LOGINFO("Cannot lookup security context for '%s'\n", Mount_Point.c_str());
dianlujitao4879b372018-12-03 18:45:47 +08002267 ret = make_ext4fs(Actual_Block_Device.c_str(), dev_sz, Mount_Point.c_str(), NULL);
Ethan Yonkerf27497f2014-02-09 11:48:33 -06002268 } else {
dianlujitao4879b372018-12-03 18:45:47 +08002269 ret = make_ext4fs(Actual_Block_Device.c_str(), dev_sz, Mount_Point.c_str(), selinux_handle);
Ethan Yonkerf27497f2014-02-09 11:48:33 -06002270 }
2271 if (ret != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002272 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troya95f55c2013-08-17 13:14:43 +00002273 return false;
2274 } else {
dianlujitao4879b372018-12-03 18:45:47 +08002275 if (NeedPreserveFooter)
2276 Wipe_Crypto_Key();
bigbiff bigbiffc49d7062013-10-11 20:28:00 -04002277 string sedir = Mount_Point + "/lost+found";
2278 PartitionManager.Mount_By_Path(sedir.c_str(), true);
2279 rmdir(sedir.c_str());
2280 mkdir(sedir.c_str(), S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP);
Dees_Troya95f55c2013-08-17 13:14:43 +00002281 return true;
2282 }
2283#else
dianlujitao4879b372018-12-03 18:45:47 +08002284 return Wipe_EXTFS("ext4");
Dees_Troya95f55c2013-08-17 13:14:43 +00002285#endif
Dees_Troy51a0e822012-09-05 15:24:24 -04002286}
2287
2288bool TWPartition::Wipe_FAT() {
Vojtech Bocek05534202013-09-11 08:11:56 +02002289 string command;
bigbiffa2bd7b72020-05-07 21:45:34 -04002290 if (!UnMount(true))
2291 return false;
Dees_Troy38bd7602012-09-14 13:33:53 -04002292
Matt Mower18794c82015-11-11 16:22:45 -06002293 if (TWFunc::Path_Exists("/sbin/mkfs.fat")) {
Greg Wallace4b44fef2015-12-29 15:33:24 -05002294 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mkfs.fat"));
Dees_Troy38bd7602012-09-14 13:33:53 -04002295 Find_Actual_Block_Device();
Matt Mower18794c82015-11-11 16:22:45 -06002296 command = "mkfs.fat " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02002297 if (TWFunc::Exec_Cmd(command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08002298 Current_File_System = "vfat";
Dees_Troye58d5262012-09-21 12:27:57 -04002299 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05002300 gui_msg("done=Done.");
Dees_Troy38bd7602012-09-14 13:33:53 -04002301 return true;
2302 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002303 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04002304 return false;
2305 }
2306 return true;
2307 }
2308 else
2309 return Wipe_RMRF();
2310
2311 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04002312}
2313
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002314bool TWPartition::Wipe_EXFAT() {
Vojtech Bocek05534202013-09-11 08:11:56 +02002315 string command;
bigbiffa2bd7b72020-05-07 21:45:34 -04002316 if (!UnMount(true))
2317 return false;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002318
2319 if (TWFunc::Path_Exists("/sbin/mkexfatfs")) {
Greg Wallace4b44fef2015-12-29 15:33:24 -05002320 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mkexfatfs"));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002321 Find_Actual_Block_Device();
2322 command = "mkexfatfs " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02002323 if (TWFunc::Exec_Cmd(command) == 0) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002324 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05002325 gui_msg("done=Done.");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002326 return true;
2327 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002328 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002329 return false;
2330 }
2331 return true;
2332 }
2333 return false;
2334}
2335
Dees_Troy38bd7602012-09-14 13:33:53 -04002336bool TWPartition::Wipe_MTD() {
2337 if (!UnMount(true))
2338 return false;
2339
Greg Wallace4b44fef2015-12-29 15:33:24 -05002340 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("MTD"));
Dees_Troy38bd7602012-09-14 13:33:53 -04002341
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02002342 mtd_scan_partitions();
2343 const MtdPartition* mtd = mtd_find_partition_by_name(MTD_Name.c_str());
2344 if (mtd == NULL) {
2345 LOGERR("No mtd partition named '%s'", MTD_Name.c_str());
2346 return false;
2347 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002348
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02002349 MtdWriteContext* ctx = mtd_write_partition(mtd);
2350 if (ctx == NULL) {
2351 LOGERR("Can't write '%s', failed to format.", MTD_Name.c_str());
2352 return false;
2353 }
2354 if (mtd_erase_blocks(ctx, -1) == -1) {
2355 mtd_write_close(ctx);
2356 LOGERR("Failed to format '%s'", MTD_Name.c_str());
2357 return false;
2358 }
2359 if (mtd_write_close(ctx) != 0) {
2360 LOGERR("Failed to close '%s'", MTD_Name.c_str());
2361 return false;
2362 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08002363 Current_File_System = "yaffs2";
Dees_Troye58d5262012-09-21 12:27:57 -04002364 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05002365 gui_msg("done=Done.");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02002366 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002367}
2368
2369bool TWPartition::Wipe_RMRF() {
Dees_Troy38bd7602012-09-14 13:33:53 -04002370 if (!Mount(true))
2371 return false;
Ethan Yonker726a0202014-12-16 20:01:38 -06002372 // This is the only wipe that leaves the partition mounted, so we
2373 // must manually remove the partition from MTP if it is a storage
2374 // partition.
2375 if (Is_Storage)
2376 PartitionManager.Remove_MTP_Storage(MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04002377
Ethan Yonker74db1572015-10-28 12:44:49 -05002378 gui_msg(Msg("remove_all=Removing all files under '{1}'")(Mount_Point));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002379 TWFunc::removeDir(Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04002380 Recreate_AndSec_Folder();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002381 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002382}
2383
Dees_Troye5017042013-08-29 16:38:55 +00002384bool TWPartition::Wipe_F2FS() {
Vojtech Bocek05534202013-09-11 08:11:56 +02002385 string command;
bigbiffa2bd7b72020-05-07 21:45:34 -04002386 if (!UnMount(true))
2387 return false;
Dees_Troye5017042013-08-29 16:38:55 +00002388
2389 if (TWFunc::Path_Exists("/sbin/mkfs.f2fs")) {
dianlujitao4879b372018-12-03 18:45:47 +08002390 bool NeedPreserveFooter = true;
2391
2392 Find_Actual_Block_Device();
2393 if (!Is_Present) {
2394 LOGINFO("Block device not present, cannot wipe %s.\n", Display_Name.c_str());
2395 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
2396 return false;
2397 }
Dees_Troye5017042013-08-29 16:38:55 +00002398
dianlujitao4879b372018-12-03 18:45:47 +08002399 /**
2400 * On decrypted devices, IOCTL_Get_Block_Size calculates size on device mapper,
2401 * so there's no need to preserve footer.
2402 */
2403 if ((Is_Decrypted && !Decrypted_Block_Device.empty()) ||
2404 Crypto_Key_Location != "footer") {
2405 NeedPreserveFooter = false;
dhacker29a3fa75f2015-01-17 19:42:33 -05002406 }
dianlujitao4879b372018-12-03 18:45:47 +08002407
dianlujitao4879b372018-12-03 18:45:47 +08002408 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mkfs.f2fs"));
Ethan Yonker7e941582019-03-22 08:18:21 -05002409 // First determine if we have the old mkfs.f2fs that uses "-r reserved_bytes"
2410 // or the new mkfs.f2fs that expects the number of sectors as the optional last argument
2411 // Note: some 7.1 trees have the old and some have the new.
2412 command = "mkfs.f2fs | grep \"reserved\" > /tmp/f2fsversiontest";
2413 TWFunc::Exec_Cmd(command, false); // no help argument so printing usage exits with an error code
2414 if (!TWFunc::Path_Exists("/tmp/f2fsversiontest")) {
2415 LOGINFO("Error determining mkfs.f2fs version\n");
2416 return false;
2417 }
2418 if (TWFunc::Get_File_Size("/tmp/f2fsversiontest") <= 0) {
2419 LOGINFO("Using newer mkfs.f2fs\n");
2420 unsigned long long dev_sz = TWFunc::IOCTL_Get_Block_Size(Actual_Block_Device.c_str());
2421 if (!dev_sz)
2422 return false;
2423
2424 if (NeedPreserveFooter)
2425 Length < 0 ? dev_sz += Length : dev_sz -= CRYPT_FOOTER_OFFSET;
2426
2427 char dev_sz_str[48];
2428 sprintf(dev_sz_str, "%llu", (dev_sz / 4096));
2429 command = "mkfs.f2fs -d1 -f -O encrypt -O quota -O verity -w 4096 " + Actual_Block_Device + " " + dev_sz_str;
2430 if (TWFunc::Path_Exists("/sbin/sload.f2fs")) {
2431 command += " && sload.f2fs -t /data " + Actual_Block_Device;
2432 }
2433 } else {
2434 LOGINFO("Using older mkfs.f2fs\n");
2435 command = "mkfs.f2fs -t 0";
2436 if (NeedPreserveFooter) {
2437 // Only use length if we're not decrypted
2438 char len[32];
2439 int mod_length = Length;
2440 if (Length < 0)
2441 mod_length *= -1;
2442 sprintf(len, "%i", mod_length);
2443 command += " -r ";
2444 command += len;
2445 }
2446 command += " " + Actual_Block_Device;
dianlujitao4879b372018-12-03 18:45:47 +08002447 }
2448 LOGINFO("mkfs.f2fs command: %s\n", command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02002449 if (TWFunc::Exec_Cmd(command) == 0) {
dianlujitao4879b372018-12-03 18:45:47 +08002450 if (NeedPreserveFooter)
2451 Wipe_Crypto_Key();
Dees_Troye5017042013-08-29 16:38:55 +00002452 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05002453 gui_msg("done=Done.");
Dees_Troye5017042013-08-29 16:38:55 +00002454 return true;
2455 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002456 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troye5017042013-08-29 16:38:55 +00002457 return false;
2458 }
2459 return true;
2460 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002461 LOGINFO("mkfs.f2fs binary not found, using rm -rf to wipe.\n");
Dees_Troye5017042013-08-29 16:38:55 +00002462 return Wipe_RMRF();
2463 }
2464 return false;
2465}
2466
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002467bool TWPartition::Wipe_NTFS() {
2468 string command;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002469 string Ntfsmake_Binary;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002470
bigbiffa2bd7b72020-05-07 21:45:34 -04002471 if (!UnMount(true))
2472 return false;
2473
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002474 if (TWFunc::Path_Exists("/sbin/mkntfs"))
2475 Ntfsmake_Binary = "mkntfs";
2476 else if (TWFunc::Path_Exists("/sbin/mkfs.ntfs"))
2477 Ntfsmake_Binary = "mkfs.ntfs";
2478 else
2479 return false;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002480
Greg Wallace4b44fef2015-12-29 15:33:24 -05002481 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)(Ntfsmake_Binary));
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002482 Find_Actual_Block_Device();
2483 command = "/sbin/" + Ntfsmake_Binary + " " + Actual_Block_Device;
2484 if (TWFunc::Exec_Cmd(command) == 0) {
2485 Recreate_AndSec_Folder();
2486 gui_msg("done=Done.");
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002487 return true;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002488 } else {
2489 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
2490 return false;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002491 }
2492 return false;
2493}
2494
Dees_Troy51a0e822012-09-05 15:24:24 -04002495bool TWPartition::Wipe_Data_Without_Wiping_Media() {
Ethan Yonker83e82572014-04-04 10:59:28 -05002496#ifdef TW_OEM_BUILD
2497 // In an OEM Build we want to do a full format
2498 return Wipe_Encryption();
2499#else
Ethan Yonker66a19492015-12-10 10:19:45 -06002500 bool ret = false;
Dees_Troy38bd7602012-09-14 13:33:53 -04002501
Dees_Troy38bd7602012-09-14 13:33:53 -04002502 if (!Mount(true))
2503 return false;
Ethan Yonker74db1572015-10-28 12:44:49 -05002504 gui_msg("wiping_data=Wiping data without wiping /data/media ...");
Ethan Yonker66a19492015-12-10 10:19:45 -06002505 ret = Wipe_Data_Without_Wiping_Media_Func(Mount_Point + "/");
2506 if (ret)
2507 gui_msg("done=Done.");
2508 return ret;
2509#endif // ifdef TW_OEM_BUILD
2510}
2511
bigbiff349ea552020-06-19 16:07:38 -04002512bool TWPartition::Recreate_Logs_Dir() {
bigbifff62d2492020-05-20 10:15:34 -04002513#ifdef TW_INCLUDE_FBE
bigbiffa2bd7b72020-05-07 21:45:34 -04002514 struct passwd pd;
2515 struct passwd *pwdptr = &pd;
2516 struct passwd *tempPd;
2517 char pwdBuf[512];
2518 int uid = 0, gid = 0;
2519
2520 if ((getpwnam_r("system", pwdptr, pwdBuf, sizeof(pwdBuf), &tempPd)) != 0) {
2521 LOGERR("unable to get system user id\n");
2522 return false;
2523 } else {
2524 struct group grp;
2525 struct group *grpptr = &grp;
2526 struct group *tempGrp;
2527 char grpBuf[512];
2528
2529 if ((getgrnam_r("cache", grpptr, grpBuf, sizeof(grpBuf), &tempGrp)) != 0) {
2530 LOGERR("unable to get cache group id\n");
2531 return false;
2532 } else {
2533 uid = pd.pw_uid;
2534 gid = grp.gr_gid;
bigbiff349ea552020-06-19 16:07:38 -04002535 std::string abLogsRecoveryDir(DATA_LOGS_DIR);
2536 abLogsRecoveryDir += "/recovery/";
bigbiffa2bd7b72020-05-07 21:45:34 -04002537
bigbiff349ea552020-06-19 16:07:38 -04002538 if (!TWFunc::Create_Dir_Recursive(abLogsRecoveryDir, S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP, uid, gid)) {
2539 LOGERR("Unable to recreate %s\n", abLogsRecoveryDir.c_str());
bigbiffa2bd7b72020-05-07 21:45:34 -04002540 return false;
2541 }
bigbiff349ea552020-06-19 16:07:38 -04002542 if (setfilecon(abLogsRecoveryDir.c_str(), "u:object_r:cache_file:s0") != 0) {
2543 LOGERR("Unable to set contexts for %s\n", abLogsRecoveryDir.c_str());
bigbiffa2bd7b72020-05-07 21:45:34 -04002544 return false;
2545 }
2546 }
2547 }
bigbifff62d2492020-05-20 10:15:34 -04002548#endif
bigbiffa2bd7b72020-05-07 21:45:34 -04002549 return true;
2550}
2551
Ethan Yonker66a19492015-12-10 10:19:45 -06002552bool TWPartition::Wipe_Data_Without_Wiping_Media_Func(const string& parent __unused) {
2553 string dir;
Dees_Troy38bd7602012-09-14 13:33:53 -04002554
2555 DIR* d;
Ethan Yonker66a19492015-12-10 10:19:45 -06002556 d = opendir(parent.c_str());
Dees_Troy16b74352012-11-14 22:27:31 +00002557 if (d != NULL) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002558 struct dirent* de;
2559 while ((de = readdir(d)) != NULL) {
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002560 if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) continue;
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05002561
Ethan Yonker66a19492015-12-10 10:19:45 -06002562 dir = parent;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002563 dir.append(de->d_name);
Ethan Yonker3fdcda42016-11-30 12:29:37 -06002564 if (wipe_exclusions.check_skip_dirs(dir)) {
Ethan Yonker66a19492015-12-10 10:19:45 -06002565 LOGINFO("skipped '%s'\n", dir.c_str());
2566 continue;
2567 }
Dees_Troyce675462013-01-09 19:48:21 +00002568 if (de->d_type == DT_DIR) {
Ethan Yonker66a19492015-12-10 10:19:45 -06002569 dir.append("/");
2570 if (!Wipe_Data_Without_Wiping_Media_Func(dir)) {
2571 closedir(d);
2572 return false;
2573 }
2574 rmdir(dir.c_str());
bigbiff bigbiff98f1f902013-01-19 18:46:13 -05002575 } else if (de->d_type == DT_REG || de->d_type == DT_LNK || de->d_type == DT_FIFO || de->d_type == DT_SOCK) {
nkk719efbe762017-06-13 19:49:05 +03002576 if (unlink(dir.c_str()) != 0)
Ethan Yonker79f88bd2016-12-09 14:52:12 -06002577 LOGINFO("Unable to unlink '%s': %s\n", dir.c_str(), strerror(errno));
Dees_Troyce675462013-01-09 19:48:21 +00002578 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002579 }
2580 closedir(d);
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05002581
Dees_Troy16b74352012-11-14 22:27:31 +00002582 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -04002583 }
Ethan Yonker74db1572015-10-28 12:44:49 -05002584 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy16b74352012-11-14 22:27:31 +00002585 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04002586}
2587
dianlujitao4879b372018-12-03 18:45:47 +08002588void TWPartition::Wipe_Crypto_Key() {
2589 Find_Actual_Block_Device();
2590 if (Crypto_Key_Location.empty())
2591 return;
2592 else if (Crypto_Key_Location == "footer") {
2593 int fd = open(Actual_Block_Device.c_str(), O_RDWR);
2594 if (fd < 0) {
2595 gui_print_color("warning", "Unable to open '%s' to wipe crypto key\n", Actual_Block_Device.c_str());
2596 return;
2597 }
2598
2599 unsigned int block_count;
2600 if ((ioctl(fd, BLKGETSIZE, &block_count)) == -1) {
2601 gui_print_color("warning", "Unable to get block size for wiping crypto footer.\n");
2602 } else {
2603 int newlen = Length < 0 ? -Length : CRYPT_FOOTER_OFFSET;
2604 off64_t offset = ((off64_t)block_count * 512) - newlen;
2605 if (lseek64(fd, offset, SEEK_SET) == -1) {
2606 gui_print_color("warning", "Unable to lseek64 for wiping crypto footer.\n");
2607 } else {
2608 void* buffer = malloc(newlen);
2609 if (!buffer) {
2610 gui_print_color("warning", "Failed to malloc for wiping crypto footer.\n");
2611 } else {
2612 memset(buffer, 0, newlen);
2613 int ret = write(fd, buffer, newlen);
2614 if (ret != newlen) {
2615 gui_print_color("warning", "Failed to wipe crypto footer.\n");
2616 } else {
2617 LOGINFO("Successfully wiped crypto footer.\n");
2618 }
2619 free(buffer);
2620 }
2621 }
2622 }
2623 close(fd);
2624 } else {
2625 if (TWFunc::IOCTL_Get_Block_Size(Crypto_Key_Location.c_str()) >= 16384LLU) {
2626 string Command = "dd of='" + Crypto_Key_Location + "' if=/dev/zero bs=16384 count=1";
2627 TWFunc::Exec_Cmd(Command);
2628 } else {
2629 LOGINFO("Crypto key location reports size < 16K so not wiping crypto footer.\n");
2630 }
2631 }
2632}
2633
bigbiffce8f83c2015-12-12 18:30:21 -05002634bool TWPartition::Backup_Tar(PartitionSettings *part_settings, pid_t *tar_fork_pid) {
Ethan Yonker472f5062016-02-25 13:47:30 -06002635 string Full_FileName;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002636 twrpTar tar;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05002637
Dees_Troy43d8b002012-09-17 16:00:01 -04002638 if (!Mount(true))
2639 return false;
2640
Ian Macdonalda3b25a32019-06-16 22:39:14 +02002641 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Backup_Display_Name, gui_parse_text("{@backing}"));
Ethan Yonker74db1572015-10-28 12:44:49 -05002642 gui_msg(Msg("backing_up=Backing up {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002643
Ethan Yonker472f5062016-02-25 13:47:30 -06002644 DataManager::GetValue(TW_USE_COMPRESSION_VAR, tar.use_compression);
Matt Mowerbb81e5d2014-03-20 18:05:41 -05002645
Dees_Troy83bd4832013-05-04 12:39:56 +00002646#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
Ethan Yonker472f5062016-02-25 13:47:30 -06002647 if (Can_Encrypt_Backup) {
2648 DataManager::GetValue("tw_encrypt_backup", tar.use_encryption);
2649 if (tar.use_encryption) {
2650 if (Use_Userdata_Encryption)
2651 tar.userdata_encryption = tar.use_encryption;
2652 string Password;
2653 DataManager::GetValue("tw_backup_password", Password);
2654 tar.setpassword(Password);
2655 } else {
2656 tar.use_encryption = 0;
2657 }
Dees_Troy83bd4832013-05-04 12:39:56 +00002658 }
2659#endif
Dees_Troy43d8b002012-09-17 16:00:01 -04002660
Ethan Yonker472f5062016-02-25 13:47:30 -06002661 Backup_FileName = Backup_Name + "." + Current_File_System + ".win";
Ethan Yonkere080c1f2016-09-19 13:50:25 -05002662 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
Ethan Yonker01f4e032017-02-03 15:30:52 -06002663 if (Has_Data_Media)
2664 gui_msg(Msg(msg::kWarning, "backup_storage_warning=Backups of {1} do not include any files in internal storage such as pictures or downloads.")(Display_Name));
Noah Jacobson5a79f672019-04-28 00:10:07 -04002665 if (Mount_Point == "/data" && DataManager::GetIntValue(TW_IS_FBE)) {
2666 std::vector<users_struct>::iterator iter;
2667 std::vector<users_struct>* userList = PartitionManager.Get_Users_List();
2668 for (iter = userList->begin(); iter != userList->end(); iter++) {
2669 if (!(*iter).isDecrypted && (*iter).userId != "0") {
2670 gui_msg(Msg(msg::kWarning,
2671 "backup_storage_undecrypt_warning=Backup will not include some files from user {1} "
2672 "because the user is not decrypted.")((*iter).userId));
2673 backup_exclusions.add_absolute_dir("/data/system_ce/" + (*iter).userId);
2674 backup_exclusions.add_absolute_dir("/data/misc_ce/" + (*iter).userId);
2675 backup_exclusions.add_absolute_dir("/data/vendor_ce/" + (*iter).userId);
2676 backup_exclusions.add_absolute_dir("/data/media/" + (*iter).userId);
2677 backup_exclusions.add_absolute_dir("/data/user/" + (*iter).userId);
2678 }
2679 }
2680 }
bigbiffce8f83c2015-12-12 18:30:21 -05002681 tar.part_settings = part_settings;
Ethan Yonker3fdcda42016-11-30 12:29:37 -06002682 tar.backup_exclusions = &backup_exclusions;
Dees Troye0a433a2013-12-02 04:10:37 +00002683 tar.setdir(Backup_Path);
2684 tar.setfn(Full_FileName);
2685 tar.setsize(Backup_Size);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002686 tar.partition_name = Backup_Name;
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002687 tar.backup_folder = part_settings->Backup_Folder;
bigbiffce8f83c2015-12-12 18:30:21 -05002688 if (tar.createTarFork(tar_fork_pid) != 0)
Dees Troye0a433a2013-12-02 04:10:37 +00002689 return false;
Dees_Troy43d8b002012-09-17 16:00:01 -04002690 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002691}
2692
bigbiffce8f83c2015-12-12 18:30:21 -05002693bool TWPartition::Backup_Image(PartitionSettings *part_settings) {
2694 string Full_FileName, adb_file_name;
igoriok87e3d932013-01-31 21:03:53 +02002695
Ethan Yonker74db1572015-10-28 12:44:49 -05002696 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, gui_parse_text("{@backing}"));
2697 gui_msg(Msg("backing_up=Backing up {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002698
Ethan Yonker472f5062016-02-25 13:47:30 -06002699 Backup_FileName = Backup_Name + "." + Current_File_System + ".win";
Dees_Troy43d8b002012-09-17 16:00:01 -04002700
bigbiffce8f83c2015-12-12 18:30:21 -05002701 if (part_settings->adbbackup) {
2702 Full_FileName = TW_ADB_BACKUP;
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002703 adb_file_name = part_settings->Backup_Folder + "/" + Backup_FileName;
bigbiffce8f83c2015-12-12 18:30:21 -05002704 }
2705 else
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002706 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
bigbiffce8f83c2015-12-12 18:30:21 -05002707
2708 part_settings->total_restore_size = Backup_Size;
2709
2710 if (part_settings->adbbackup) {
2711 if (!twadbbu::Write_TWIMG(adb_file_name, Backup_Size))
2712 return false;
2713 }
2714
2715 if (!Raw_Read_Write(part_settings))
Ethan Yonker472f5062016-02-25 13:47:30 -06002716 return false;
2717
bigbiffce8f83c2015-12-12 18:30:21 -05002718 if (part_settings->adbbackup) {
2719 if (!twadbbu::Write_TWEOF())
2720 return false;
Dees_Troy7c2dec82012-09-26 09:49:14 -04002721 }
Dees_Troy43d8b002012-09-17 16:00:01 -04002722 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002723}
2724
bigbiffce8f83c2015-12-12 18:30:21 -05002725bool TWPartition::Raw_Read_Write(PartitionSettings *part_settings) {
2726 unsigned long long RW_Block_Size, Remain = Backup_Size;
2727 int src_fd = -1, dest_fd = -1;
2728 ssize_t bs;
Ethan Yonker472f5062016-02-25 13:47:30 -06002729 bool ret = false;
2730 void* buffer = NULL;
2731 unsigned long long backedup_size = 0;
bigbiffce8f83c2015-12-12 18:30:21 -05002732 string srcfn, destfn;
Ethan Yonker472f5062016-02-25 13:47:30 -06002733
bigbiffce8f83c2015-12-12 18:30:21 -05002734 if (part_settings->PM_Method == PM_BACKUP) {
2735 srcfn = Actual_Block_Device;
2736 if (part_settings->adbbackup)
2737 destfn = TW_ADB_BACKUP;
bigbiff bigbiff38b83c12017-12-28 19:58:52 -05002738 else {
Ethan Yonkere080c1f2016-09-19 13:50:25 -05002739 destfn = part_settings->Backup_Folder + "/" + Backup_FileName;
bigbiff bigbiff38b83c12017-12-28 19:58:52 -05002740 }
bigbiffce8f83c2015-12-12 18:30:21 -05002741 }
2742 else {
2743 destfn = Actual_Block_Device;
2744 if (part_settings->adbbackup) {
2745 srcfn = TW_ADB_RESTORE;
2746 } else {
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002747 srcfn = part_settings->Backup_Folder + "/" + Backup_FileName;
bigbiffce8f83c2015-12-12 18:30:21 -05002748 Remain = TWFunc::Get_File_Size(srcfn);
2749 }
2750 }
Ethan Yonker472f5062016-02-25 13:47:30 -06002751
bigbiffce8f83c2015-12-12 18:30:21 -05002752 src_fd = open(srcfn.c_str(), O_RDONLY | O_LARGEFILE);
Ethan Yonker472f5062016-02-25 13:47:30 -06002753 if (src_fd < 0) {
bigbiffce8f83c2015-12-12 18:30:21 -05002754 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(srcfn.c_str())(strerror(errno)));
Ethan Yonker472f5062016-02-25 13:47:30 -06002755 return false;
2756 }
bigbiffce8f83c2015-12-12 18:30:21 -05002757
2758 dest_fd = open(destfn.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_LARGEFILE, S_IRUSR | S_IWUSR);
Ethan Yonker472f5062016-02-25 13:47:30 -06002759 if (dest_fd < 0) {
bigbiffce8f83c2015-12-12 18:30:21 -05002760 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(destfn.c_str())(strerror(errno)));
Ethan Yonker472f5062016-02-25 13:47:30 -06002761 goto exit;
2762 }
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -04002763
bigbiffce8f83c2015-12-12 18:30:21 -05002764 LOGINFO("Reading '%s', writing '%s'\n", srcfn.c_str(), destfn.c_str());
2765
2766 if (part_settings->adbbackup) {
2767 RW_Block_Size = MAX_ADB_READ;
2768 bs = MAX_ADB_READ;
2769 }
2770 else {
2771 RW_Block_Size = 1048576LLU; // 1MB
2772 bs = (ssize_t)(RW_Block_Size);
2773 }
2774
Ethan Yonker472f5062016-02-25 13:47:30 -06002775 buffer = malloc((size_t)bs);
2776 if (!buffer) {
2777 LOGINFO("Raw_Read_Write failed to malloc\n");
2778 goto exit;
2779 }
bigbiffce8f83c2015-12-12 18:30:21 -05002780
2781 if (part_settings->progress)
2782 part_settings->progress->SetPartitionSize(part_settings->total_restore_size);
2783
Ethan Yonker472f5062016-02-25 13:47:30 -06002784 while (Remain > 0) {
2785 if (Remain < RW_Block_Size)
bigbiffce8f83c2015-12-12 18:30:21 -05002786 bs = (ssize_t)(Remain);
Matt Mowera8a89d12016-12-30 18:10:37 -06002787 if (read(src_fd, buffer, bs) != bs) {
Ethan Yonker472f5062016-02-25 13:47:30 -06002788 LOGINFO("Error reading source fd (%s)\n", strerror(errno));
2789 goto exit;
2790 }
2791 if (write(dest_fd, buffer, bs) != bs) {
2792 LOGINFO("Error writing destination fd (%s)\n", strerror(errno));
2793 goto exit;
2794 }
2795 backedup_size += (unsigned long long)(bs);
Matt Mower029a82d2017-01-08 13:12:38 -06002796 Remain -= (unsigned long long)(bs);
bigbiffce8f83c2015-12-12 18:30:21 -05002797 if (part_settings->progress)
2798 part_settings->progress->UpdateSize(backedup_size);
Ethan Yonker472f5062016-02-25 13:47:30 -06002799 if (PartitionManager.Check_Backup_Cancel() != 0)
2800 goto exit;
2801 }
bigbiffce8f83c2015-12-12 18:30:21 -05002802 if (part_settings->progress)
2803 part_settings->progress->UpdateDisplayDetails(true);
Ethan Yonker472f5062016-02-25 13:47:30 -06002804 fsync(dest_fd);
nailyka083dc62016-11-11 21:41:28 +01002805
2806 if (!part_settings->adbbackup && part_settings->PM_Method == PM_BACKUP) {
2807 tw_set_default_metadata(destfn.c_str());
2808 LOGINFO("Restored default metadata for %s\n", destfn.c_str());
2809 }
2810
Ethan Yonker472f5062016-02-25 13:47:30 -06002811 ret = true;
2812exit:
2813 if (src_fd >= 0)
2814 close(src_fd);
2815 if (dest_fd >= 0)
2816 close(dest_fd);
2817 if (buffer)
2818 free(buffer);
2819 return ret;
2820}
2821
bigbiffce8f83c2015-12-12 18:30:21 -05002822bool TWPartition::Backup_Dump_Image(PartitionSettings *part_settings) {
Vojtech Bocek05534202013-09-11 08:11:56 +02002823 string Full_FileName, Command;
Dees_Troy43d8b002012-09-17 16:00:01 -04002824
Ethan Yonker74db1572015-10-28 12:44:49 -05002825 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, gui_parse_text("{@backing}"));
2826 gui_msg(Msg("backing_up=Backing up {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002827
bigbiffce8f83c2015-12-12 18:30:21 -05002828 if (part_settings->progress)
2829 part_settings->progress->SetPartitionSize(Backup_Size);
Dees_Troy43d8b002012-09-17 16:00:01 -04002830
Ethan Yonker472f5062016-02-25 13:47:30 -06002831 Backup_FileName = Backup_Name + "." + Current_File_System + ".win";
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002832 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04002833
2834 Command = "dump_image " + MTD_Name + " '" + Full_FileName + "'";
bigbiffce8f83c2015-12-12 18:30:21 -05002835
Dees_Troy2673cec2013-04-02 20:22:16 +00002836 LOGINFO("Backup command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02002837 TWFunc::Exec_Cmd(Command);
Ethan Yonker4b94cfd2014-12-11 10:00:45 -06002838 tw_set_default_metadata(Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04002839 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
2840 // Actual size may not match backup size due to bad blocks on MTD devices so just check for 0 bytes
Matt Mower3c366972015-12-25 19:28:31 -06002841 gui_msg(Msg(msg::kError, "backup_size=Backup file size for '{1}' is 0 bytes.")(Full_FileName));
Dees_Troy7c2dec82012-09-26 09:49:14 -04002842 return false;
2843 }
bigbiffce8f83c2015-12-12 18:30:21 -05002844 if (part_settings->progress)
2845 part_settings->progress->UpdateSize(Backup_Size);
2846
Dees_Troy43d8b002012-09-17 16:00:01 -04002847 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002848}
2849
bigbiffce8f83c2015-12-12 18:30:21 -05002850unsigned long long TWPartition::Get_Restore_Size(PartitionSettings *part_settings) {
2851 if (!part_settings->adbbackup) {
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002852 InfoManager restore_info(part_settings->Backup_Folder + "/" + Backup_Name + ".info");
bigbiffce8f83c2015-12-12 18:30:21 -05002853 if (restore_info.LoadValues() == 0) {
2854 if (restore_info.GetValue("backup_size", Restore_Size) == 0) {
2855 LOGINFO("Read info file, restore size is %llu\n", Restore_Size);
2856 return Restore_Size;
2857 }
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002858 }
2859 }
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002860
Matt Mower029a82d2017-01-08 13:12:38 -06002861 string Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
2862 string Restore_File_System = Get_Restore_File_System(part_settings);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002863
2864 if (Is_Image(Restore_File_System)) {
2865 Restore_Size = TWFunc::Get_File_Size(Full_FileName);
2866 return Restore_Size;
2867 }
2868
2869 twrpTar tar;
2870 tar.setdir(Backup_Path);
2871 tar.setfn(Full_FileName);
bigbiffce8f83c2015-12-12 18:30:21 -05002872 tar.backup_name = Full_FileName;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002873#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
2874 string Password;
2875 DataManager::GetValue("tw_restore_password", Password);
2876 if (!Password.empty())
2877 tar.setpassword(Password);
2878#endif
2879 tar.partition_name = Backup_Name;
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002880 tar.backup_folder = part_settings->Backup_Folder;
bigbiffce8f83c2015-12-12 18:30:21 -05002881 tar.part_settings = part_settings;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002882 Restore_Size = tar.get_size();
2883 return Restore_Size;
2884}
2885
bigbiffce8f83c2015-12-12 18:30:21 -05002886bool TWPartition::Restore_Tar(PartitionSettings *part_settings) {
Ethan Yonker472f5062016-02-25 13:47:30 -06002887 string Full_FileName;
Dees Troy4159aed2014-02-28 17:24:43 +00002888 bool ret = false;
bigbiffce8f83c2015-12-12 18:30:21 -05002889 string Restore_File_System = Get_Restore_File_System(part_settings);
Dees_Troy43d8b002012-09-17 16:00:01 -04002890
Dees_Troye58d5262012-09-21 12:27:57 -04002891 if (Has_Android_Secure) {
Dees_Troye58d5262012-09-21 12:27:57 -04002892 if (!Wipe_AndSec())
2893 return false;
Gary Peck43acadf2012-11-21 21:19:01 -08002894 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002895 gui_msg(Msg("wiping=Wiping {1}")(Backup_Display_Name));
Ethan Yonker5eac2222014-06-11 12:22:55 -05002896 if (Has_Data_Media && Mount_Point == "/data" && Restore_File_System != Current_File_System) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002897 gui_msg(Msg(msg::kWarning, "datamedia_fs_restore=WARNING: This /data backup was made with {1} file system! The backup may not boot unless you change back to {1}.")(Restore_File_System));
Ethan Yonker5eac2222014-06-11 12:22:55 -05002898 if (!Wipe_Data_Without_Wiping_Media())
2899 return false;
2900 } else {
2901 if (!Wipe(Restore_File_System))
2902 return false;
2903 }
Dees_Troye58d5262012-09-21 12:27:57 -04002904 }
Matt Mower3c366972015-12-25 19:28:31 -06002905 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Backup_Display_Name, gui_parse_text("{@restoring_hdr}"));
Ethan Yonker74db1572015-10-28 12:44:49 -05002906 gui_msg(Msg("restoring=Restoring {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002907
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05002908 // Remount as read/write as needed so we can restore the backup
2909 if (!ReMount_RW(true))
Dees_Troy43d8b002012-09-17 16:00:01 -04002910 return false;
2911
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002912 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
Ethan Yonker87af5632014-02-10 11:56:35 -06002913 twrpTar tar;
bigbiffce8f83c2015-12-12 18:30:21 -05002914 tar.part_settings = part_settings;
Ethan Yonker87af5632014-02-10 11:56:35 -06002915 tar.setdir(Backup_Path);
2916 tar.setfn(Full_FileName);
2917 tar.backup_name = Backup_Name;
2918#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
2919 string Password;
2920 DataManager::GetValue("tw_restore_password", Password);
2921 if (!Password.empty())
2922 tar.setpassword(Password);
2923#endif
bigbiffce8f83c2015-12-12 18:30:21 -05002924 part_settings->progress->SetPartitionSize(Get_Restore_Size(part_settings));
2925 if (tar.extractTarFork() != 0)
Dees Troy4159aed2014-02-28 17:24:43 +00002926 ret = false;
2927 else
2928 ret = true;
2929#ifdef HAVE_CAPABILITIES
2930 // Restore capabilities to the run-as binary
Captain Throwback9d6feb52018-07-27 10:05:24 -04002931 if (Mount_Point == PartitionManager.Get_Android_Root_Path() && Mount(true) && TWFunc::Path_Exists("/system/bin/run-as")) {
Dees Troy4159aed2014-02-28 17:24:43 +00002932 struct vfs_cap_data cap_data;
2933 uint64_t capabilities = (1 << CAP_SETUID) | (1 << CAP_SETGID);
2934
2935 memset(&cap_data, 0, sizeof(cap_data));
2936 cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE;
2937 cap_data.data[0].permitted = (uint32_t) (capabilities & 0xffffffff);
2938 cap_data.data[0].inheritable = 0;
2939 cap_data.data[1].permitted = (uint32_t) (capabilities >> 32);
2940 cap_data.data[1].inheritable = 0;
2941 if (setxattr("/system/bin/run-as", XATTR_NAME_CAPS, &cap_data, sizeof(cap_data), 0) < 0) {
2942 LOGINFO("Failed to reset capabilities of /system/bin/run-as binary.\n");
2943 } else {
2944 LOGINFO("Reset capabilities of /system/bin/run-as binary successful.\n");
2945 }
2946 }
2947#endif
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05002948 if (Mount_Read_Only || Mount_Flags & MS_RDONLY)
2949 // Remount as read only when restoration is complete
2950 ReMount(true);
2951
Dees Troy4159aed2014-02-28 17:24:43 +00002952 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04002953}
2954
bigbiffce8f83c2015-12-12 18:30:21 -05002955bool TWPartition::Restore_Image(PartitionSettings *part_settings) {
Ethan Yonker96af84a2015-01-05 14:58:36 -06002956 string Full_FileName;
bigbiffce8f83c2015-12-12 18:30:21 -05002957 string Restore_File_System = Get_Restore_File_System(part_settings);
Dees_Troy43d8b002012-09-17 16:00:01 -04002958
Matt Mower3c366972015-12-25 19:28:31 -06002959 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Backup_Display_Name, gui_parse_text("{@restoring_hdr}"));
Ethan Yonker74db1572015-10-28 12:44:49 -05002960 gui_msg(Msg("restoring=Restoring {1}...")(Backup_Display_Name));
bigbiffce8f83c2015-12-12 18:30:21 -05002961
2962 if (part_settings->adbbackup)
2963 Full_FileName = TW_ADB_RESTORE;
2964 else
Ethan Yonkere080c1f2016-09-19 13:50:25 -05002965 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
Gary Peck15e623d2012-11-21 21:07:58 -08002966
Ethan Yonker96af84a2015-01-05 14:58:36 -06002967 if (Restore_File_System == "emmc") {
bigbiffce8f83c2015-12-12 18:30:21 -05002968 if (!part_settings->adbbackup)
2969 part_settings->total_restore_size = (uint64_t)(TWFunc::Get_File_Size(Full_FileName));
2970 if (!Raw_Read_Write(part_settings))
Ethan Yonker96af84a2015-01-05 14:58:36 -06002971 return false;
2972 } else if (Restore_File_System == "mtd" || Restore_File_System == "bml") {
bigbiffce8f83c2015-12-12 18:30:21 -05002973 if (!Flash_Image_FI(Full_FileName, part_settings->progress))
2974 return false;
2975 }
2976
2977 if (part_settings->adbbackup) {
2978 if (!twadbbu::Write_TWEOF())
Ethan Yonker96af84a2015-01-05 14:58:36 -06002979 return false;
Gary Peck15e623d2012-11-21 21:07:58 -08002980 }
Dees_Troy43d8b002012-09-17 16:00:01 -04002981 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002982}
Dees_Troy5bf43922012-09-07 16:07:55 -04002983
2984bool TWPartition::Update_Size(bool Display_Error) {
Dees_Troy0550cfb2012-10-13 11:56:13 -04002985 bool ret = false, Was_Already_Mounted = false;
Dees_Troy51127312012-09-08 13:08:49 -04002986
Ethan Yonker1b190162016-12-05 15:25:19 -06002987 Find_Actual_Block_Device();
2988
2989 if (!Can_Be_Mounted && !Is_Encrypted) {
2990 if (TWFunc::Path_Exists(Actual_Block_Device) && Find_Partition_Size()) {
2991 Used = Size;
2992 Backup_Size = Size;
2993 return true;
2994 }
Dees_Troy5bf43922012-09-07 16:07:55 -04002995 return false;
Ethan Yonker1b190162016-12-05 15:25:19 -06002996 }
Dees_Troy5bf43922012-09-07 16:07:55 -04002997
Dees_Troy0550cfb2012-10-13 11:56:13 -04002998 Was_Already_Mounted = Is_Mounted();
Dees_Troy38bd7602012-09-14 13:33:53 -04002999 if (Removable || Is_Encrypted) {
3000 if (!Mount(false))
3001 return true;
3002 } else if (!Mount(Display_Error))
Dees_Troy5bf43922012-09-07 16:07:55 -04003003 return false;
Dees_Troy51127312012-09-08 13:08:49 -04003004
3005 ret = Get_Size_Via_statfs(Display_Error);
Dees_Troy0550cfb2012-10-13 11:56:13 -04003006 if (!ret || Size == 0) {
3007 if (!Get_Size_Via_df(Display_Error)) {
3008 if (!Was_Already_Mounted)
3009 UnMount(false);
Dees_Troy51127312012-09-08 13:08:49 -04003010 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04003011 }
3012 }
Dees_Troy51127312012-09-08 13:08:49 -04003013
Dees_Troy5bf43922012-09-07 16:07:55 -04003014 if (Has_Data_Media) {
3015 if (Mount(Display_Error)) {
Ethan Yonker3fdcda42016-11-30 12:29:37 -06003016 Used = backup_exclusions.Get_Folder_Size(Mount_Point);
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05003017 Backup_Size = Used;
3018 int bak = (int)(Used / 1048576LLU);
Dees_Troy51127312012-09-08 13:08:49 -04003019 int fre = (int)(Free / 1048576LLU);
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05003020 LOGINFO("Data backup size is %iMB, free: %iMB.\n", bak, fre);
Dees_Troy0550cfb2012-10-13 11:56:13 -04003021 } else {
3022 if (!Was_Already_Mounted)
3023 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04003024 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04003025 }
Dees_Troye58d5262012-09-21 12:27:57 -04003026 } else if (Has_Android_Secure) {
3027 if (Mount(Display_Error))
Ethan Yonker3fdcda42016-11-30 12:29:37 -06003028 Backup_Size = backup_exclusions.Get_Folder_Size(Backup_Path);
Dees_Troy0550cfb2012-10-13 11:56:13 -04003029 else {
3030 if (!Was_Already_Mounted)
3031 UnMount(false);
Dees_Troye58d5262012-09-21 12:27:57 -04003032 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04003033 }
Dees_Troy5bf43922012-09-07 16:07:55 -04003034 }
Dees_Troy0550cfb2012-10-13 11:56:13 -04003035 if (!Was_Already_Mounted)
3036 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04003037 return true;
Dees_Troy51127312012-09-08 13:08:49 -04003038}
Dees_Troy38bd7602012-09-14 13:33:53 -04003039
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06003040bool TWPartition::Find_Wildcard_Block_Devices(const string& Device) {
3041 int mount_point_index = 0; // we will need to create separate mount points for each partition found and we use this index to name each one
3042 string Path = TWFunc::Get_Path(Device);
3043 string Dev = TWFunc::Get_Filename(Device);
3044 size_t wildcard_index = Dev.find("*");
3045 if (wildcard_index != string::npos)
3046 Dev = Dev.substr(0, wildcard_index);
3047 wildcard_index = Dev.size();
3048 DIR* d = opendir(Path.c_str());
3049 if (d == NULL) {
3050 LOGINFO("Error opening '%s': %s\n", Path.c_str(), strerror(errno));
3051 return false;
3052 }
3053 struct dirent* de;
3054 while ((de = readdir(d)) != NULL) {
3055 if (de->d_type != DT_BLK || strlen(de->d_name) <= wildcard_index || strncmp(de->d_name, Dev.c_str(), wildcard_index) != 0)
3056 continue;
3057
3058 string item = Path + "/";
3059 item.append(de->d_name);
3060 if (PartitionManager.Find_Partition_By_Block_Device(item))
3061 continue;
3062 TWPartition *part = new TWPartition;
3063 char buffer[MAX_FSTAB_LINE_LENGTH];
3064 sprintf(buffer, "%s %s-%i auto defaults defaults", item.c_str(), Mount_Point.c_str(), ++mount_point_index);
Chaosmasterf6e42ce2020-01-27 00:17:04 +01003065 part->Process_Fstab_Line(buffer, false, NULL, false);
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06003066 char display[MAX_FSTAB_LINE_LENGTH];
3067 sprintf(display, "%s %i", Storage_Name.c_str(), mount_point_index);
3068 part->Storage_Name = display;
3069 part->Display_Name = display;
3070 part->Primary_Block_Device = item;
3071 part->Wildcard_Block_Device = false;
3072 part->Is_SubPartition = true;
3073 part->SubPartition_Of = Mount_Point;
3074 part->Is_Storage = Is_Storage;
3075 part->Can_Be_Mounted = true;
3076 part->Removable = true;
3077 part->Can_Be_Wiped = Can_Be_Wiped;
3078 part->Wipe_Available_in_GUI = Wipe_Available_in_GUI;
3079 part->Find_Actual_Block_Device();
3080 part->Update_Size(false);
3081 Has_SubPartition = true;
3082 PartitionManager.Output_Partition(part);
3083 PartitionManager.Add_Partition(part);
3084 }
3085 closedir(d);
3086 return (mount_point_index > 0);
3087}
3088
Dees_Troy38bd7602012-09-14 13:33:53 -04003089void TWPartition::Find_Actual_Block_Device(void) {
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06003090 if (!Sysfs_Entry.empty() && Primary_Block_Device.empty() && Decrypted_Block_Device.empty()) {
3091 /* Sysfs_Entry.empty() indicates if this is a sysfs entry that begins with /device/
3092 * If we have a syfs entry then we are looking for this device from a uevent add.
3093 * The uevent add will set the primary block device based on the data we receive from
3094 * after checking for adopted storage. If the device ends up being adopted, then the
3095 * decrypted block device will be set instead of the primary block device. */
3096 Is_Present = false;
3097 return;
3098 }
3099 if (Wildcard_Block_Device && !Is_Adopted_Storage) {
3100 Is_Present = false;
3101 Actual_Block_Device = "";
3102 Can_Be_Mounted = false;
3103 if (!Find_Wildcard_Block_Devices(Primary_Block_Device)) {
3104 string Dev = Primary_Block_Device.substr(0, Primary_Block_Device.find("*"));
3105 if (TWFunc::Path_Exists(Dev)) {
3106 Is_Present = true;
3107 Can_Be_Mounted = true;
3108 Actual_Block_Device = Dev;
3109 }
3110 }
3111 return;
3112 } else if (Is_Decrypted && !Decrypted_Block_Device.empty()) {
Dees_Troy38bd7602012-09-14 13:33:53 -04003113 Actual_Block_Device = Decrypted_Block_Device;
Ethan Yonker1b190162016-12-05 15:25:19 -06003114 if (TWFunc::Path_Exists(Decrypted_Block_Device)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04003115 Is_Present = true;
Ethan Yonker1b190162016-12-05 15:25:19 -06003116 return;
3117 }
3118 } else if (SlotSelect && TWFunc::Path_Exists(Primary_Block_Device + PartitionManager.Get_Active_Slot_Suffix())) {
3119 Actual_Block_Device = Primary_Block_Device + PartitionManager.Get_Active_Slot_Suffix();
3120 unlink(Primary_Block_Device.c_str());
3121 symlink(Actual_Block_Device.c_str(), Primary_Block_Device.c_str()); // we create a non-slot symlink pointing to the currently selected slot which may assist zips with installing
3122 Is_Present = true;
3123 return;
Dees_Troy43d8b002012-09-17 16:00:01 -04003124 } else if (TWFunc::Path_Exists(Primary_Block_Device)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04003125 Is_Present = true;
3126 Actual_Block_Device = Primary_Block_Device;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05003127 return;
3128 }
Ethan Yonker1b190162016-12-05 15:25:19 -06003129 if (!Alternate_Block_Device.empty() && TWFunc::Path_Exists(Alternate_Block_Device)) {
Dees_Troy3f04d032012-10-07 18:20:09 -04003130 Actual_Block_Device = Alternate_Block_Device;
Dees_Troy38bd7602012-09-14 13:33:53 -04003131 Is_Present = true;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05003132 } else {
Dees_Troy38bd7602012-09-14 13:33:53 -04003133 Is_Present = false;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05003134 }
Dees_Troy38bd7602012-09-14 13:33:53 -04003135}
3136
3137void TWPartition::Recreate_Media_Folder(void) {
3138 string Command;
Ethan Yonker66a19492015-12-10 10:19:45 -06003139 string Media_Path = Mount_Point + "/media";
Dees_Troy38bd7602012-09-14 13:33:53 -04003140
Ethan Yonkerbd7492d2016-12-07 13:55:01 -06003141 if (Is_FBE) {
3142 LOGINFO("Not recreating media folder on FBE\n");
3143 return;
3144 }
Dees_Troy38bd7602012-09-14 13:33:53 -04003145 if (!Mount(true)) {
Ethan Yonker66a19492015-12-10 10:19:45 -06003146 gui_msg(Msg(msg::kError, "recreate_folder_err=Unable to recreate {1} folder.")(Media_Path));
3147 } else if (!TWFunc::Path_Exists(Media_Path)) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05003148 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Ethan Yonker66a19492015-12-10 10:19:45 -06003149 LOGINFO("Recreating %s folder.\n", Media_Path.c_str());
3150 mkdir(Media_Path.c_str(), 0770);
xiaolu9416f4f2015-06-04 08:22:23 +08003151 string Internal_path = DataManager::GetStrValue("tw_internal_path");
3152 if (!Internal_path.empty()) {
3153 LOGINFO("Recreating %s folder.\n", Internal_path.c_str());
3154 mkdir(Internal_path.c_str(), 0770);
3155 }
3156#ifdef TW_INTERNAL_STORAGE_PATH
3157 mkdir(EXPAND(TW_INTERNAL_STORAGE_PATH), 0770);
3158#endif
Matt Mower87413642017-01-17 21:14:46 -06003159
thata3d31fb2014-12-21 22:27:40 +01003160 // Afterwards, we will try to set the
3161 // default metadata that we were hopefully able to get during
3162 // early boot.
Ethan Yonker66a19492015-12-10 10:19:45 -06003163 tw_set_default_metadata(Media_Path.c_str());
xiaolu9416f4f2015-06-04 08:22:23 +08003164 if (!Internal_path.empty())
3165 tw_set_default_metadata(Internal_path.c_str());
Matt Mower87413642017-01-17 21:14:46 -06003166
Ethan Yonker5eac2222014-06-11 12:22:55 -05003167 // Toggle mount to ensure that "internal sdcard" gets mounted
bigbiff bigbiff9c754052013-01-09 09:09:08 -05003168 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Ethan Yonker5eac2222014-06-11 12:22:55 -05003169 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Dees_Troy38bd7602012-09-14 13:33:53 -04003170 }
Dees_Troy43d8b002012-09-17 16:00:01 -04003171}
Dees_Troye58d5262012-09-21 12:27:57 -04003172
3173void TWPartition::Recreate_AndSec_Folder(void) {
Dees_Troye58d5262012-09-21 12:27:57 -04003174 if (!Has_Android_Secure)
3175 return;
Dees_Troy2673cec2013-04-02 20:22:16 +00003176 LOGINFO("Creating %s: %s\n", Backup_Display_Name.c_str(), Symlink_Path.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -04003177 if (!Mount(true)) {
Ethan Yonker74db1572015-10-28 12:44:49 -05003178 gui_msg(Msg(msg::kError, "recreate_folder_err=Unable to recreate {1} folder.")(Backup_Name));
Dees_Troye58d5262012-09-21 12:27:57 -04003179 } else if (!TWFunc::Path_Exists(Symlink_Path)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00003180 LOGINFO("Recreating %s folder.\n", Backup_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05003181 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05003182 mkdir(Symlink_Path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
bigbiff bigbiff9c754052013-01-09 09:09:08 -05003183 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04003184 }
3185}
bigbiff7cb4c332014-11-26 20:36:07 -05003186
3187uint64_t TWPartition::Get_Max_FileSize() {
3188 uint64_t maxFileSize = 0;
3189 const uint64_t constGB = (uint64_t) 1024 * 1024 * 1024;
3190 const uint64_t constTB = (uint64_t) constGB * 1024;
3191 const uint64_t constPB = (uint64_t) constTB * 1024;
bigbiff0c532032014-12-21 13:41:26 -05003192 if (Current_File_System == "ext4")
3193 maxFileSize = 16 * constTB; //16 TB
3194 else if (Current_File_System == "vfat")
3195 maxFileSize = 4 * constGB; //4 GB
3196 else if (Current_File_System == "ntfs")
3197 maxFileSize = 256 * constTB; //256 TB
3198 else if (Current_File_System == "exfat")
3199 maxFileSize = 16 * constPB; //16 PB
3200 else if (Current_File_System == "ext3")
3201 maxFileSize = 2 * constTB; //2 TB
3202 else if (Current_File_System == "f2fs")
3203 maxFileSize = 3.94 * constTB; //3.94 TB
bigbiff7cb4c332014-11-26 20:36:07 -05003204 else
3205 maxFileSize = 100000000L;
3206 return maxFileSize - 1;
3207}
3208
bigbiffce8f83c2015-12-12 18:30:21 -05003209bool TWPartition::Flash_Image(PartitionSettings *part_settings) {
3210 string Restore_File_System, full_filename;
Ethan Yonker96af84a2015-01-05 14:58:36 -06003211
Ethan Yonkere080c1f2016-09-19 13:50:25 -05003212 full_filename = part_settings->Backup_Folder + "/" + Backup_FileName;
Ethan Yonker96af84a2015-01-05 14:58:36 -06003213
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05003214 LOGINFO("Image filename is: %s\n", Backup_FileName.c_str());
bigbiffce8f83c2015-12-12 18:30:21 -05003215
3216 if (Backup_Method == BM_FILES) {
Ethan Yonker96af84a2015-01-05 14:58:36 -06003217 LOGERR("Cannot flash images to file systems\n");
3218 return false;
3219 } else if (!Can_Flash_Img) {
3220 LOGERR("Cannot flash images to partitions %s\n", Display_Name.c_str());
3221 return false;
3222 } else {
3223 if (!Find_Partition_Size()) {
3224 LOGERR("Unable to find partition size for '%s'\n", Mount_Point.c_str());
3225 return false;
3226 }
bigbiffce8f83c2015-12-12 18:30:21 -05003227 unsigned long long image_size = TWFunc::Get_File_Size(full_filename);
Ethan Yonker96af84a2015-01-05 14:58:36 -06003228 if (image_size > Size) {
Ethan Yonker74db1572015-10-28 12:44:49 -05003229 LOGINFO("Size (%llu bytes) of image '%s' is larger than target device '%s' (%llu bytes)\n",
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05003230 image_size, Backup_FileName.c_str(), Actual_Block_Device.c_str(), Size);
Ethan Yonker74db1572015-10-28 12:44:49 -05003231 gui_err("img_size_err=Size of image is larger than target device");
Ethan Yonker96af84a2015-01-05 14:58:36 -06003232 return false;
3233 }
bigbiffce8f83c2015-12-12 18:30:21 -05003234 if (Backup_Method == BM_DD) {
3235 if (!part_settings->adbbackup) {
3236 if (Is_Sparse_Image(full_filename)) {
3237 return Flash_Sparse_Image(full_filename);
3238 }
Ethan Yonker472f5062016-02-25 13:47:30 -06003239 }
bigbiffce8f83c2015-12-12 18:30:21 -05003240 return Raw_Read_Write(part_settings);
3241 } else if (Backup_Method == BM_FLASH_UTILS) {
3242 return Flash_Image_FI(full_filename, NULL);
Ethan Yonker472f5062016-02-25 13:47:30 -06003243 }
Ethan Yonker96af84a2015-01-05 14:58:36 -06003244 }
3245
3246 LOGERR("Unknown flash method for '%s'\n", Mount_Point.c_str());
3247 return false;
3248}
3249
Ethan Yonker472f5062016-02-25 13:47:30 -06003250bool TWPartition::Is_Sparse_Image(const string& Filename) {
HashBanged974bb2016-01-30 14:20:09 -05003251 uint32_t magic = 0;
3252 int fd = open(Filename.c_str(), O_RDONLY);
3253 if (fd < 0) {
3254 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Filename)(strerror(errno)));
3255 return false;
3256 }
bigbiffce8f83c2015-12-12 18:30:21 -05003257
HashBanged974bb2016-01-30 14:20:09 -05003258 if (read(fd, &magic, sizeof(magic)) != sizeof(magic)) {
3259 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Filename)(strerror(errno)));
3260 close(fd);
3261 return false;
3262 }
3263 close(fd);
Ethan Yonker472f5062016-02-25 13:47:30 -06003264 if (magic == SPARSE_HEADER_MAGIC)
3265 return true;
3266 return false;
3267}
3268
3269bool TWPartition::Flash_Sparse_Image(const string& Filename) {
3270 string Command;
3271
3272 gui_msg(Msg("flashing=Flashing {1}...")(Display_Name));
3273
3274 Command = "simg2img '" + Filename + "' '" + Actual_Block_Device + "'";
Ethan Yonker96af84a2015-01-05 14:58:36 -06003275 LOGINFO("Flash command: '%s'\n", Command.c_str());
3276 TWFunc::Exec_Cmd(Command);
3277 return true;
3278}
3279
Ethan Yonker472f5062016-02-25 13:47:30 -06003280bool TWPartition::Flash_Image_FI(const string& Filename, ProgressTracking *progress) {
Ethan Yonker96af84a2015-01-05 14:58:36 -06003281 string Command;
Ethan Yonker472f5062016-02-25 13:47:30 -06003282 unsigned long long file_size;
Ethan Yonker96af84a2015-01-05 14:58:36 -06003283
Ethan Yonker74db1572015-10-28 12:44:49 -05003284 gui_msg(Msg("flashing=Flashing {1}...")(Display_Name));
Ethan Yonker472f5062016-02-25 13:47:30 -06003285 if (progress) {
3286 file_size = (unsigned long long)(TWFunc::Get_File_Size(Filename));
3287 progress->SetPartitionSize(file_size);
3288 }
Ethan Yonker96af84a2015-01-05 14:58:36 -06003289 // Sometimes flash image doesn't like to flash due to the first 2KB matching, so we erase first to ensure that it flashes
3290 Command = "erase_image " + MTD_Name;
3291 LOGINFO("Erase command: '%s'\n", Command.c_str());
3292 TWFunc::Exec_Cmd(Command);
3293 Command = "flash_image " + MTD_Name + " '" + Filename + "'";
3294 LOGINFO("Flash command: '%s'\n", Command.c_str());
3295 TWFunc::Exec_Cmd(Command);
Ethan Yonker472f5062016-02-25 13:47:30 -06003296 if (progress)
3297 progress->UpdateSize(file_size);
Ethan Yonker96af84a2015-01-05 14:58:36 -06003298 return true;
3299}
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05003300
3301void TWPartition::Change_Mount_Read_Only(bool new_value) {
3302 Mount_Read_Only = new_value;
3303}
3304
bigbiffce8f83c2015-12-12 18:30:21 -05003305bool TWPartition::Is_Read_Only() {
3306 return Mount_Read_Only;
3307}
3308
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05003309int TWPartition::Check_Lifetime_Writes() {
3310 bool original_read_only = Mount_Read_Only;
3311 int ret = 1;
3312
3313 Mount_Read_Only = true;
3314 if (Mount(false)) {
3315 Find_Actual_Block_Device();
Ethan Yonker54970bb2019-01-17 16:19:24 -06003316 string temp = Actual_Block_Device;
3317 Find_Real_Block_Device(temp, false);
3318 string block = basename(temp.c_str());
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05003319 string file = "/sys/fs/" + Current_File_System + "/" + block + "/lifetime_write_kbytes";
3320 string result;
3321 if (TWFunc::Path_Exists(file)) {
3322 if (TWFunc::read_file(file, result) != 0) {
3323 LOGINFO("Check_Lifetime_Writes of '%s' failed to read_file\n", file.c_str());
3324 } else {
3325 LOGINFO("Check_Lifetime_Writes result: '%s'\n", result.c_str());
3326 if (result == "0") {
3327 ret = 0;
3328 }
3329 }
3330 } else {
3331 LOGINFO("Check_Lifetime_Writes file does not exist '%s'\n", file.c_str());
3332 }
3333 UnMount(true);
3334 } else {
3335 LOGINFO("Check_Lifetime_Writes failed to mount '%s'\n", Mount_Point.c_str());
3336 }
3337 Mount_Read_Only = original_read_only;
3338 return ret;
3339}
Ethan Yonker66a19492015-12-10 10:19:45 -06003340
3341int TWPartition::Decrypt_Adopted() {
3342#ifdef TW_INCLUDE_CRYPTO
3343 int ret = 1;
3344 Is_Adopted_Storage = false;
3345 string Adopted_Key_File = "";
3346
3347 if (!Removable)
3348 return ret;
3349
3350 int fd = open(Alternate_Block_Device.c_str(), O_RDONLY);
3351 if (fd < 0) {
3352 LOGINFO("failed to open '%s'\n", Alternate_Block_Device.c_str());
3353 return ret;
3354 }
3355 char type_guid[80];
3356 char part_guid[80];
3357
nkk71f940ba22017-03-24 18:28:03 +02003358 uint32_t p_num;
3359 size_t last_digit = Primary_Block_Device.find_last_not_of("0123456789");
3360 if ((last_digit != string::npos) && (last_digit != Primary_Block_Device.length()-1))
3361 p_num = atoi(Primary_Block_Device.substr(last_digit + 1).c_str()) + 1;
3362 else
3363 p_num = 2;
3364
3365 if (gpt_disk_get_partition_info(fd, p_num, type_guid, part_guid) == 0) {
Ethan Yonker66a19492015-12-10 10:19:45 -06003366 LOGINFO("type: '%s'\n", type_guid);
3367 LOGINFO("part: '%s'\n", part_guid);
3368 Adopted_GUID = part_guid;
3369 LOGINFO("Adopted_GUID '%s'\n", Adopted_GUID.c_str());
3370 if (strcmp(type_guid, TWGptAndroidExpand) == 0) {
3371 LOGINFO("android_expand found\n");
3372 Adopted_Key_File = "/data/misc/vold/expand_";
3373 Adopted_Key_File += part_guid;
3374 Adopted_Key_File += ".key";
3375 if (TWFunc::Path_Exists(Adopted_Key_File)) {
3376 Is_Adopted_Storage = true;
3377 /* Until we find a use case for this, I think it is safe
3378 * to disable USB Mass Storage whenever adopted storage
3379 * is present.
3380 */
nkk71f940ba22017-03-24 18:28:03 +02003381 if (p_num == 2) {
3382 // TODO: Properly detect mixed vs fully adopted storage. Maybe this
3383 // should be moved to partitionmanager instead, and disable after
3384 // checking all partitions. Also the presence of adopted storage does
3385 // not necessarily mean it's being used as Internal Storage
3386 LOGINFO("Detected adopted storage, disabling USB mass storage mode\n");
3387 DataManager::SetValue("tw_has_usb_storage", 0);
3388 }
Ethan Yonker66a19492015-12-10 10:19:45 -06003389 }
3390 }
3391 }
3392
3393 if (Is_Adopted_Storage) {
nkk71f940ba22017-03-24 18:28:03 +02003394 string Adopted_Block_Device = Alternate_Block_Device + "p" + TWFunc::to_string(p_num);
Ethan Yonker66a19492015-12-10 10:19:45 -06003395 if (!TWFunc::Path_Exists(Adopted_Block_Device)) {
nkk71f940ba22017-03-24 18:28:03 +02003396 Adopted_Block_Device = Alternate_Block_Device + TWFunc::to_string(p_num);
Ethan Yonker66a19492015-12-10 10:19:45 -06003397 if (!TWFunc::Path_Exists(Adopted_Block_Device)) {
3398 LOGINFO("Adopted block device does not exist\n");
3399 goto exit;
3400 }
3401 }
3402 LOGINFO("key file is '%s', block device '%s'\n", Adopted_Key_File.c_str(), Adopted_Block_Device.c_str());
3403 char crypto_blkdev[MAXPATHLEN];
3404 std::string thekey;
3405 int fdkey = open(Adopted_Key_File.c_str(), O_RDONLY);
3406 if (fdkey < 0) {
3407 LOGINFO("failed to open key file\n");
3408 goto exit;
3409 }
3410 char buf[512];
3411 ssize_t n;
3412 while ((n = read(fdkey, &buf[0], sizeof(buf))) > 0) {
3413 thekey.append(buf, n);
3414 }
3415 close(fdkey);
3416 unsigned char* key = (unsigned char*) thekey.data();
3417 cryptfs_revert_ext_volume(part_guid);
3418
3419 ret = cryptfs_setup_ext_volume(part_guid, Adopted_Block_Device.c_str(), key, thekey.size(), crypto_blkdev);
3420 if (ret == 0) {
3421 LOGINFO("adopted storage new block device: '%s'\n", crypto_blkdev);
3422 Decrypted_Block_Device = crypto_blkdev;
3423 Is_Decrypted = true;
3424 Is_Encrypted = true;
3425 Find_Actual_Block_Device();
Kjell Braden3126a112016-06-19 16:58:15 +00003426 if (!Mount_Storage_Retry(false)) {
Ethan Yonker66a19492015-12-10 10:19:45 -06003427 LOGERR("Failed to mount decrypted adopted storage device\n");
3428 Is_Decrypted = false;
3429 Is_Encrypted = false;
3430 cryptfs_revert_ext_volume(part_guid);
3431 ret = 1;
3432 } else {
Ethan Yonkerfcf3f242016-02-16 12:30:26 -06003433 UnMount(false);
3434 Has_Android_Secure = false;
3435 Symlink_Path = "";
3436 Symlink_Mount_Point = "";
3437 Backup_Name = Mount_Point.substr(1);
3438 Backup_Path = Mount_Point;
3439 TWPartition* sdext = PartitionManager.Find_Partition_By_Path("/sd-ext");
3440 if (sdext && sdext->Actual_Block_Device == Adopted_Block_Device) {
3441 LOGINFO("Removing /sd-ext from partition list due to adopted storage\n");
3442 PartitionManager.Remove_Partition_By_Path("/sd-ext");
3443 }
Ethan Yonker66a19492015-12-10 10:19:45 -06003444 Setup_Data_Media();
3445 Recreate_Media_Folder();
3446 Wipe_Available_in_GUI = true;
3447 Wipe_During_Factory_Reset = true;
3448 Can_Be_Backed_Up = true;
3449 Can_Encrypt_Backup = true;
3450 Use_Userdata_Encryption = true;
3451 Is_Storage = true;
3452 Storage_Name = "Adopted Storage";
3453 Is_SubPartition = true;
3454 SubPartition_Of = "/data";
3455 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
3456 DataManager::SetValue("tw_has_adopted_storage", 1);
3457 }
3458 } else {
3459 LOGERR("Failed to setup adopted storage decryption\n");
3460 }
3461 }
3462exit:
Matt Mower06543e32017-01-06 15:25:26 -06003463 close(fd);
Ethan Yonker66a19492015-12-10 10:19:45 -06003464 return ret;
3465#else
3466 LOGINFO("Decrypt_Adopted: no crypto support\n");
3467 return 1;
3468#endif
3469}
3470
3471void TWPartition::Revert_Adopted() {
3472#ifdef TW_INCLUDE_CRYPTO
3473 if (!Adopted_GUID.empty()) {
3474 PartitionManager.Remove_MTP_Storage(Mount_Point);
3475 UnMount(false);
3476 cryptfs_revert_ext_volume(Adopted_GUID.c_str());
3477 Is_Adopted_Storage = false;
3478 Is_Encrypted = false;
3479 Is_Decrypted = false;
3480 Decrypted_Block_Device = "";
3481 Find_Actual_Block_Device();
3482 Wipe_During_Factory_Reset = false;
3483 Can_Be_Backed_Up = false;
3484 Can_Encrypt_Backup = false;
3485 Use_Userdata_Encryption = false;
3486 Is_SubPartition = false;
3487 SubPartition_Of = "";
3488 Has_Data_Media = false;
3489 Storage_Path = Mount_Point;
3490 if (!Symlink_Mount_Point.empty()) {
3491 TWPartition* Dat = PartitionManager.Find_Partition_By_Path("/data");
3492 if (Dat) {
3493 Dat->UnMount(false);
3494 Dat->Symlink_Mount_Point = Symlink_Mount_Point;
3495 }
3496 Symlink_Mount_Point = "";
3497 }
3498 }
3499#else
3500 LOGINFO("Revert_Adopted: no crypto support\n");
3501#endif
3502}
bigbiff bigbiffb5ecaad2017-03-20 18:53:53 -04003503
3504void TWPartition::Set_Backup_FileName(string fname) {
3505 Backup_FileName = fname;
3506}
3507
3508string TWPartition::Get_Backup_Name() {
3509 return Backup_Name;
3510}
bigbiff56b02eb2020-07-06 20:24:34 -04003511
3512std::string TWPartition::Get_Backup_FileName() {
3513 return Backup_FileName;
3514}
3515
3516std::string TWPartition::Get_Display_Name() {
3517 return Display_Name;
3518}
3519
3520bool TWPartition::Is_SlotSelect() {
3521 return SlotSelect;
3522
3523}