blob: b31b228c45ac4852b1be43a372052f5da59e69ee [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;
362 Find_Real_Block_Device(Primary_Block_Device, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400363 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400364 item_index++;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600365 } else if (item_index > 2) {
366 if (fstab_version == 2) {
367 if (item_index == 3) {
368 Process_FS_Flags(ptr);
369 if (additional_entry) {
370 additional_entry->Save_FS_Flags(Fstab_File_System, Mount_Flags, Mount_Options);
371 return false; // We save the extra fs flags in the other partition entry and by returning false, this entry will be deleted
372 }
373 } else {
374 strlcpy(twflags, ptr, sizeof(twflags));
375 }
376 item_index++;
377 } else if (*ptr == '/') { // v2 fstab does not allow alternate block devices
Dees_Troy5bf43922012-09-07 16:07:55 -0400378 // Alternate Block Device
379 Alternate_Block_Device = ptr;
380 Find_Real_Block_Device(Alternate_Block_Device, Display_Error);
381 } else if (strlen(ptr) > 7 && strncmp(ptr, "length=", 7) == 0) {
382 // Partition length
383 ptr += 7;
384 Length = atoi(ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400385 } else if (strlen(ptr) > 6 && strncmp(ptr, "flags=", 6) == 0) {
386 // Custom flags, save for later so that new values aren't overwritten by defaults
387 ptr += 6;
Matt Mower2416a502016-04-12 19:54:46 -0500388 strlcpy(twflags, ptr, sizeof(twflags));
Dees_Troy38bd7602012-09-14 13:33:53 -0400389 } else if (strlen(ptr) == 4 && (strncmp(ptr, "NULL", 4) == 0 || strncmp(ptr, "null", 4) == 0 || strncmp(ptr, "null", 4) == 0)) {
390 // Do nothing
Dees_Troy5bf43922012-09-07 16:07:55 -0400391 } else {
392 // Unhandled data
Matt Mower2b2dd152016-04-26 11:24:08 -0500393 LOGINFO("Unhandled fstab information '%s' in fstab line '%s'\n", ptr, fstab_line);
Dees_Troy5bf43922012-09-07 16:07:55 -0400394 }
395 }
396 while (index < line_len && full_line[index] != '\0')
397 index++;
398 }
399
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600400 // override block devices from the v2 fstab with the ones we read from the twrp.flags file in case they are different
401 if (fstab_version == 2 && twrp_flags && twrp_flags->size() > 0) {
402 it = twrp_flags->find(Mount_Point);
403 if (it != twrp_flags->end()) {
404 if (!it->second.Primary_Block_Device.empty()) {
405 Primary_Block_Device = it->second.Primary_Block_Device;
406 Find_Real_Block_Device(Primary_Block_Device, Display_Error);
407 }
408 if (!it->second.Alternate_Block_Device.empty()) {
409 Alternate_Block_Device = it->second.Alternate_Block_Device;
410 Find_Real_Block_Device(Alternate_Block_Device, Display_Error);
411 }
412 }
413 }
414
415 if (strncmp(fstab_line, "/devices/", strlen("/devices/")) == 0) {
416 Sysfs_Entry = Primary_Block_Device;
417 Primary_Block_Device = "";
418 Is_Storage = true;
419 Removable = true;
420 Wipe_Available_in_GUI = true;
421 Wildcard_Block_Device = true;
422 }
423 if (Primary_Block_Device.find("*") != string::npos)
424 Wildcard_Block_Device = true;
425
Chaosmasterf6e42ce2020-01-27 00:17:04 +0100426 if (Sar_Detect) {
427 if(Is_File_System(Fstab_File_System) && (Mount_Point == "/" || Mount_Point == "/system" || Mount_Point == "/system_root"))
428 Find_Actual_Block_Device();
429 else
430 return true;
431 } else if (Mount_Point == "auto") {
Captain Throwback4e379532020-06-05 20:42:16 -0400432 Mount_Point = "/auto";
433 char autoi[5];
434 sprintf(autoi, "%i", auto_index);
435 Mount_Point += autoi;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600436 Backup_Path = Mount_Point;
437 Storage_Path = Mount_Point;
dianlujitaob76a73a2020-04-30 20:33:20 +0800438 Backup_Name = Mount_Point.substr(1);
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600439 auto_index++;
440 Setup_File_System(Display_Error);
441 Display_Name = "Storage";
442 Backup_Display_Name = Display_Name;
443 Storage_Name = Display_Name;
444 Can_Be_Backed_Up = false;
445 Wipe_Available_in_GUI = true;
446 Is_Storage = true;
447 Removable = true;
448 Wipe_Available_in_GUI = true;
449 } else if (!Is_File_System(Fstab_File_System) && !Is_Image(Fstab_File_System)) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400450 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000451 LOGERR("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400452 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000453 LOGINFO("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Matt Mower72c87ce2016-04-26 14:34:56 -0500454 return false;
Dees_Troy5bf43922012-09-07 16:07:55 -0400455 } else if (Is_File_System(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400456 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400457 Setup_File_System(Display_Error);
dianlujitaob76a73a2020-04-30 20:33:20 +0800458 Backup_Name = Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
Chaosmaster3b682732020-01-25 21:09:28 +0100459 if (Mount_Point == "/" || Mount_Point == "/system" || Mount_Point == "/system_root") {
Dees_Troy5bf43922012-09-07 16:07:55 -0400460 Display_Name = "System";
dianlujitaob76a73a2020-04-30 20:33:20 +0800461 Backup_Name = "system";
Dees_Troya13d74f2013-03-24 08:54:55 -0500462 Backup_Display_Name = Display_Name;
463 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400464 Wipe_Available_in_GUI = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500465 Can_Be_Backed_Up = true;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500466 Mount_Read_Only = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400467 } else if (Mount_Point == "/data") {
468 Display_Name = "Data";
Dees_Troya13d74f2013-03-24 08:54:55 -0500469 Backup_Display_Name = Display_Name;
470 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400471 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400472 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500473 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000474 Can_Encrypt_Backup = true;
475 Use_Userdata_Encryption = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400476 } else if (Mount_Point == "/cache") {
477 Display_Name = "Cache";
Dees_Troya13d74f2013-03-24 08:54:55 -0500478 Backup_Display_Name = Display_Name;
479 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400480 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400481 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500482 Can_Be_Backed_Up = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400483 } else if (Mount_Point == "/datadata") {
Dees_Troy51127312012-09-08 13:08:49 -0400484 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400485 Display_Name = "DataData";
Dees_Troya13d74f2013-03-24 08:54:55 -0500486 Backup_Display_Name = Display_Name;
487 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400488 Is_SubPartition = true;
489 SubPartition_Of = "/data";
Dees_Troy5bf43922012-09-07 16:07:55 -0400490 DataManager::SetValue(TW_HAS_DATADATA, 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500491 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000492 Can_Encrypt_Backup = true;
493 Use_Userdata_Encryption = false; // This whole partition should be encrypted
Dees_Troy5bf43922012-09-07 16:07:55 -0400494 } else if (Mount_Point == "/sd-ext") {
Dees_Troy51127312012-09-08 13:08:49 -0400495 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400496 Display_Name = "SD-Ext";
Dees_Troya13d74f2013-03-24 08:54:55 -0500497 Backup_Display_Name = Display_Name;
498 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400499 Wipe_Available_in_GUI = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400500 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500501 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000502 Can_Encrypt_Backup = true;
503 Use_Userdata_Encryption = true;
Dees_Troy2c50e182012-09-26 20:05:28 -0400504 } else if (Mount_Point == "/boot") {
505 Display_Name = "Boot";
Dees_Troya13d74f2013-03-24 08:54:55 -0500506 Backup_Display_Name = Display_Name;
Dees_Troy2c50e182012-09-26 20:05:28 -0400507 DataManager::SetValue("tw_boot_is_mountable", 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500508 Can_Be_Backed_Up = true;
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500509 } else if (Mount_Point == "/vendor") {
510 Display_Name = "Vendor";
511 Backup_Display_Name = Display_Name;
512 Storage_Name = Display_Name;
513 Mount_Read_Only = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400514 }
515#ifdef TW_EXTERNAL_STORAGE_PATH
516 if (Mount_Point == EXPAND(TW_EXTERNAL_STORAGE_PATH)) {
517 Is_Storage = true;
518 Storage_Path = EXPAND(TW_EXTERNAL_STORAGE_PATH);
Dees_Troyc51f1f92012-09-20 15:32:13 -0400519 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500520 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400521#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000522 if (Mount_Point == "/sdcard" || Mount_Point == "/external_sd" || Mount_Point == "/external_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400523 Is_Storage = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400524 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500525 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400526#endif
Dees_Troyb05ddee2013-01-28 20:24:50 +0000527 }
Dees_Troy8170a922012-09-18 15:40:25 -0400528#ifdef TW_INTERNAL_STORAGE_PATH
529 if (Mount_Point == EXPAND(TW_INTERNAL_STORAGE_PATH)) {
530 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500531 Is_Settings_Storage = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400532 Storage_Path = EXPAND(TW_INTERNAL_STORAGE_PATH);
Dees_Troya13d74f2013-03-24 08:54:55 -0500533 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400534 }
535#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000536 if (Mount_Point == "/emmc" || Mount_Point == "/internal_sd" || Mount_Point == "/internal_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400537 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500538 Is_Settings_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500539 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400540 }
541#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400542 } else if (Is_Image(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400543 Find_Actual_Block_Device();
Ethan Yonker1b190162016-12-05 15:25:19 -0600544 Setup_Image();
Dees_Troya13d74f2013-03-24 08:54:55 -0500545 if (Mount_Point == "/boot") {
546 Display_Name = "Boot";
547 Backup_Display_Name = Display_Name;
548 Can_Be_Backed_Up = true;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600549 Can_Flash_Img = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500550 } else if (Mount_Point == "/recovery") {
551 Display_Name = "Recovery";
552 Backup_Display_Name = Display_Name;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600553 Can_Flash_Img = true;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500554 } else if (Mount_Point == "/system_image") {
555 Display_Name = "System Image";
556 Backup_Display_Name = Display_Name;
HashBanged974bb2016-01-30 14:20:09 -0500557 Can_Flash_Img = true;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500558 Can_Be_Backed_Up = true;
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500559 } else if (Mount_Point == "/vendor_image") {
560 Display_Name = "Vendor Image";
561 Backup_Display_Name = Display_Name;
HashBanged974bb2016-01-30 14:20:09 -0500562 Can_Flash_Img = true;
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500563 Can_Be_Backed_Up = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500564 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400565 }
566
Matt Mower2416a502016-04-12 19:54:46 -0500567 // Process TWRP fstab flags
568 if (strlen(twflags) > 0) {
569 string Prev_Display_Name = Display_Name;
570 string Prev_Storage_Name = Storage_Name;
571 string Prev_Backup_Display_Name = Backup_Display_Name;
572 Display_Name = "";
573 Storage_Name = "";
574 Backup_Display_Name = "";
575
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600576 Process_TW_Flags(twflags, (fstab_version == 1), fstab_version);
577 Save_FS_Flags(Fstab_File_System, Mount_Flags, Mount_Options);
Matt Mower2416a502016-04-12 19:54:46 -0500578
579 bool has_display_name = !Display_Name.empty();
580 bool has_storage_name = !Storage_Name.empty();
581 bool has_backup_name = !Backup_Display_Name.empty();
582 if (!has_display_name) Display_Name = Prev_Display_Name;
583 if (!has_storage_name) Storage_Name = Prev_Storage_Name;
584 if (!has_backup_name) Backup_Display_Name = Prev_Backup_Display_Name;
585
586 if (has_display_name && !has_storage_name)
587 Storage_Name = Display_Name;
588 if (!has_display_name && has_storage_name)
589 Display_Name = Storage_Name;
590 if (has_display_name && !has_backup_name && Backup_Display_Name != "Android Secure")
591 Backup_Display_Name = Display_Name;
592 if (!has_display_name && has_backup_name)
593 Display_Name = Backup_Display_Name;
594 }
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600595
596 if (fstab_version == 2 && twrp_flags && twrp_flags->size() > 0) {
597 it = twrp_flags->find(Mount_Point);
598 if (it != twrp_flags->end()) {
599 char twrpflags[MAX_FSTAB_LINE_LENGTH] = "";
600 int skip = 0;
601 string Flags = it->second.Flags;
602 strcpy(twrpflags, Flags.c_str());
603 if (strlen(twrpflags) > strlen("flags=") && strncmp(twrpflags, "flags=", strlen("flags=")) == 0)
604 skip += strlen("flags=");
605 char* flagptr = twrpflags;
606 flagptr += skip;
607 Process_TW_Flags(flagptr, Display_Error, 1); // Forcing the fstab to ver 1 because this data is coming from the /etc/twrp.flags which should be using the TWRP v1 flags format
608 }
609 }
nkk7198fc3992017-12-16 16:26:42 +0200610
611 if (Mount_Point == "/persist" && Can_Be_Mounted) {
612 bool mounted = Is_Mounted();
613 if (mounted || Mount(false)) {
614 // Read the backup settings file
nkk7198fc3992017-12-16 16:26:42 +0200615 TWFunc::Fixup_Time_On_Boot("/persist/time/");
616 if (!mounted)
617 UnMount(false);
618 }
619 }
620
Chaosmasterf6e42ce2020-01-27 00:17:04 +0100621 if (Is_File_System(Fstab_File_System) && (Mount_Point == "/" || Mount_Point == "/system" || Mount_Point == "/system_root")) {
622 if (Sar_Detect) {
623 Mount_Point = "/s";
624 Mount_Read_Only = true;
625 Can_Be_Mounted = true;
626 } else {
627 Mount_Point = PartitionManager.Get_Android_Root_Path();
628 Backup_Path = Mount_Point;
629 Storage_Path = Mount_Point;
630 Make_Dir(Mount_Point, Display_Error);
631 }
632 }
633
Dees_Troy51127312012-09-08 13:08:49 -0400634 return true;
635}
636
Matt Mower72c87ce2016-04-26 14:34:56 -0500637void TWPartition::Partition_Post_Processing(bool Display_Error) {
638 if (Mount_Point == "/data")
639 Setup_Data_Partition(Display_Error);
640 else if (Mount_Point == "/cache")
641 Setup_Cache_Partition(Display_Error);
642}
643
Ethan Yonkerbd7492d2016-12-07 13:55:01 -0600644void TWPartition::ExcludeAll(const string& path) {
645 backup_exclusions.add_absolute_dir(path);
646 wipe_exclusions.add_absolute_dir(path);
647}
648
Matt Mower72c87ce2016-04-26 14:34:56 -0500649void TWPartition::Setup_Data_Partition(bool Display_Error) {
650 if (Mount_Point != "/data")
651 return;
652
653 // Ensure /data is not mounted as tmpfs for qcom hardware decrypt
654 UnMount(false);
655
656#ifdef TW_INCLUDE_CRYPTO
657 if (datamedia)
658 Setup_Data_Media();
659 Can_Be_Encrypted = true;
660 char crypto_blkdev[255];
661 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
662 if (strcmp(crypto_blkdev, "error") != 0) {
663 DataManager::SetValue(TW_IS_DECRYPTED, 1);
664 Is_Encrypted = true;
665 Is_Decrypted = true;
Noah Jacobson5a79f672019-04-28 00:10:07 -0400666 if (Key_Directory.empty()) {
Ethan Yonker93382822018-11-01 15:25:31 -0500667 Is_FBE = false;
Noah Jacobson5a79f672019-04-28 00:10:07 -0400668 DataManager::SetValue(TW_IS_FBE, 0);
669 } else {
Ethan Yonker93382822018-11-01 15:25:31 -0500670 Is_FBE = true;
Noah Jacobson5a79f672019-04-28 00:10:07 -0400671 DataManager::SetValue(TW_IS_FBE, 1);
672 }
Matt Mower72c87ce2016-04-26 14:34:56 -0500673 Decrypted_Block_Device = crypto_blkdev;
674 LOGINFO("Data already decrypted, new block device: '%s'\n", crypto_blkdev);
675 } else if (!Mount(false)) {
676 if (Is_Present) {
Ethan Yonker93382822018-11-01 15:25:31 -0500677 if (Key_Directory.empty()) {
Peter Cai39881ab2019-11-02 19:22:38 +0800678 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 -0400679 Fstab_File_System.c_str());
Ethan Yonker93382822018-11-01 15:25:31 -0500680 if (cryptfs_check_footer() == 0) {
681 Is_Encrypted = true;
682 Is_Decrypted = false;
683 Can_Be_Mounted = false;
684 Current_File_System = "emmc";
685 Setup_Image();
686 DataManager::SetValue(TW_IS_ENCRYPTED, 1);
687 DataManager::SetValue(TW_CRYPTO_PWTYPE, cryptfs_get_password_type());
Noah Jacobson5a79f672019-04-28 00:10:07 -0400688 DataManager::SetValue("tw_crypto_pwtype_0", cryptfs_get_password_type());
Ethan Yonker93382822018-11-01 15:25:31 -0500689 DataManager::SetValue(TW_CRYPTO_PASSWORD, "");
690 DataManager::SetValue("tw_crypto_display", "");
691 } else {
692 gui_err("mount_data_footer=Could not mount /data and unable to find crypto footer.");
693 }
694 } else {
Matt Mower72c87ce2016-04-26 14:34:56 -0500695 Is_Encrypted = true;
696 Is_Decrypted = false;
Matt Mower72c87ce2016-04-26 14:34:56 -0500697 }
Ethan Yonker93382822018-11-01 15:25:31 -0500698 } else if (Key_Directory.empty()) {
Noah Jacobson5a79f672019-04-28 00:10:07 -0400699 LOGERR("Primary block device '%s' for mount point '%s' is not present!\n",
700 Primary_Block_Device.c_str(), Mount_Point.c_str());
Matt Mower72c87ce2016-04-26 14:34:56 -0500701 }
702 } else {
Captain Throwback83c22342020-12-29 15:54:33 -0500703 int is_device_fbe;
704 DataManager::GetValue(TW_IS_FBE, is_device_fbe);
705 if (!Decrypt_FBE_DE() && is_device_fbe == 1) {
mauronofrio1db94322019-11-23 23:13:04 +0100706 char wrappedvalue[PROPERTY_VALUE_MAX];
707 property_get("fbe.data.wrappedkey", wrappedvalue, "");
708 std::string wrappedkeyvalue(wrappedvalue);
709 if (wrappedkeyvalue == "true") {
710 LOGERR("Unable to decrypt FBE device\n");
711 } else {
712 LOGINFO("Trying wrapped key.\n");
713 property_set("fbe.data.wrappedkey", "true");
mauronofrio9bf73352019-11-23 22:27:34 +0100714 if (!Decrypt_FBE_DE()) {
Mauronofrio Matarrese6d5d08a2019-12-03 14:25:25 +0100715 LOGINFO("Unable to decrypt device\n");
mauronofrio9bf73352019-11-23 22:27:34 +0100716 }
mauronofrio1db94322019-11-23 23:13:04 +0100717 }
mauronofrio9bf73352019-11-23 22:27:34 +0100718 }
Matt Mower72c87ce2016-04-26 14:34:56 -0500719 }
720 if (datamedia && (!Is_Encrypted || (Is_Encrypted && Is_Decrypted))) {
721 Setup_Data_Media();
722 Recreate_Media_Folder();
723 }
724#else
725 if (datamedia) {
726 Setup_Data_Media();
727 Recreate_Media_Folder();
728 }
729#endif
730}
731
Ethan Yonker93382822018-11-01 15:25:31 -0500732bool TWPartition::Decrypt_FBE_DE() {
733if (TWFunc::Path_Exists("/data/unencrypted/key/version")) {
Captain Throwbackcf0dffc2020-02-17 16:44:50 -0500734 DataManager::SetValue(TW_IS_FBE, 1);
Captain Throwback83c22342020-12-29 15:54:33 -0500735 property_set("ro.crypto.state", "encrypted");
nebrassy674bb0c2020-10-24 21:08:20 +0200736 property_set("ro.crypto.type", "file");
Ethan Yonker93382822018-11-01 15:25:31 -0500737 LOGINFO("File Based Encryption is present\n");
738#ifdef TW_INCLUDE_FBE
Noah Jacobson5a79f672019-04-28 00:10:07 -0400739 Is_FBE = true;
Noah Jacobson5a79f672019-04-28 00:10:07 -0400740 ExcludeAll(Mount_Point + "/convert_fbe");
741 ExcludeAll(Mount_Point + "/unencrypted");
742 ExcludeAll(Mount_Point + "/misc/vold/user_keys");
743 ExcludeAll(Mount_Point + "/system/gatekeeper.password.key");
744 ExcludeAll(Mount_Point + "/system/gatekeeper.pattern.key");
745 ExcludeAll(Mount_Point + "/system/locksettings.db");
746 ExcludeAll(Mount_Point + "/system/locksettings.db-wal");
747 ExcludeAll(Mount_Point + "/misc/gatekeeper");
748 ExcludeAll(Mount_Point + "/misc/keystore");
749 ExcludeAll(Mount_Point + "/drm/kek.dat");
750 ExcludeAll(Mount_Point + "/system_de/0/spblob"); // contains data needed to decrypt pixel 2
751 ExcludeAll(Mount_Point + "/system/users/0/gatekeeper.password.key");
752 ExcludeAll(Mount_Point + "/system/users/0/gatekeeper.pattern.key");
Noah Jacobson5a79f672019-04-28 00:10:07 -0400753 int retry_count = 3;
754 while (!Decrypt_DE() && --retry_count)
755 usleep(2000);
Noah Jacobson5a79f672019-04-28 00:10:07 -0400756 if (retry_count > 0) {
757 property_set("ro.crypto.state", "encrypted");
758 Is_Encrypted = true;
759 Is_Decrypted = false;
760 DataManager::SetValue(TW_IS_ENCRYPTED, 1);
761 string filename;
762 int pwd_type = Get_Password_Type(0, filename);
763 if (pwd_type < 0) {
764 LOGERR("This TWRP does not have synthetic password decrypt support\n");
765 pwd_type = 0; // default password
766 }
Noah Jacobsonbf0b0fb2020-06-05 12:47:20 -0400767 PartitionManager.Parse_Users(); // after load_all_de_keys() to parse_users
768 std::vector<users_struct>::iterator iter;
769 std::vector<users_struct>* userList = PartitionManager.Get_Users_List();
770 for (iter = userList->begin(); iter != userList->end(); iter++) {
771 if (atoi((*iter).userId.c_str()) != 0) {
772 ExcludeAll(Mount_Point + "/system_de/" + (*iter).userId + "/spblob");
773 ExcludeAll(Mount_Point + "/system/users/" + (*iter).userId + "/gatekeeper.password.key");
774 ExcludeAll(Mount_Point + "/system/users/" + (*iter).userId + "/gatekeeper.pattern.key");
775 ExcludeAll(Mount_Point + "/system/users/" + (*iter).userId + "/locksettings.db");
776 ExcludeAll(Mount_Point + "/system/users/" + (*iter).userId + "/locksettings.db-wal");
777 }
778 }
Noah Jacobson5a79f672019-04-28 00:10:07 -0400779 DataManager::SetValue(TW_CRYPTO_PWTYPE, pwd_type);
780 DataManager::SetValue("tw_crypto_pwtype_0", pwd_type);
781 DataManager::SetValue(TW_CRYPTO_PASSWORD, "");
782 DataManager::SetValue("tw_crypto_display", "");
783 return true;
784 }
Ethan Yonker93382822018-11-01 15:25:31 -0500785#else
786 LOGERR("FBE found but FBE support not present in TWRP\n");
787#endif
788 }
Captain Throwbackcf0dffc2020-02-17 16:44:50 -0500789 DataManager::SetValue(TW_IS_FBE, 0);
Ethan Yonker93382822018-11-01 15:25:31 -0500790 return false;
791}
792
Matt Mower72c87ce2016-04-26 14:34:56 -0500793void TWPartition::Setup_Cache_Partition(bool Display_Error __unused) {
794 if (Mount_Point != "/cache")
795 return;
796
797 if (!Mount(true))
798 return;
799
800 if (!TWFunc::Path_Exists("/cache/recovery/.")) {
801 LOGINFO("Recreating /cache/recovery folder\n");
802 if (mkdir("/cache/recovery", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0)
803 LOGERR("Could not create /cache/recovery\n");
804 }
805}
806
Matt Mower4ab42b12016-04-21 13:52:18 -0500807void TWPartition::Process_FS_Flags(const char *str) {
808 char *options = strdup(str);
809 char *ptr, *savep;
Hashcode62bd9e02013-11-19 21:59:42 -0800810
Matt Mower4ab42b12016-04-21 13:52:18 -0500811 Mount_Options = "";
Hashcode62bd9e02013-11-19 21:59:42 -0800812
Matt Mower4ab42b12016-04-21 13:52:18 -0500813 // Avoid issues with potentially nested strtok by using strtok_r
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600814 for (ptr = strtok_r(options, ",", &savep); ptr; ptr = strtok_r(NULL, ",", &savep)) {
815 char *equals = strstr(ptr, "=");
816 size_t name_len;
Matt Mower4ab42b12016-04-21 13:52:18 -0500817
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600818 if (!equals)
819 name_len = strlen(ptr);
820 else
821 name_len = equals - ptr;
822
823 // There are some flags that we want to ignore in TWRP
824 bool found_match = false;
825 for (const char** ignored_mount_item = ignored_mount_items; *ignored_mount_item; ignored_mount_item++) {
826 if (strncmp(ptr, *ignored_mount_item, name_len) == 0) {
827 found_match = true;
Hashcode62bd9e02013-11-19 21:59:42 -0800828 break;
829 }
830 }
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600831 if (found_match)
832 continue;
Hashcode62bd9e02013-11-19 21:59:42 -0800833
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600834 // mount_flags are never postfixed by '='
835 if (!equals) {
836 const struct flag_list* mount_flag = mount_flags;
837 for (; mount_flag->name; mount_flag++) {
838 if (strcmp(ptr, mount_flag->name) == 0) {
839 if (mount_flag->flag == MS_RDONLY)
840 Mount_Read_Only = true;
841 else
842 Mount_Flags |= (unsigned)mount_flag->flag;
843 found_match = true;
844 break;
845 }
846 }
847 if (found_match)
848 continue;
Matt Mower4ab42b12016-04-21 13:52:18 -0500849 }
850
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600851 // If we aren't ignoring this flag and it's not a mount flag, then it must be a mount option
852 if (!Mount_Options.empty())
853 Mount_Options += ",";
854 Mount_Options += ptr;
Matt Mower4ab42b12016-04-21 13:52:18 -0500855 }
856 free(options);
Hashcode62bd9e02013-11-19 21:59:42 -0800857}
858
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600859void TWPartition::Save_FS_Flags(const string& local_File_System, int local_Mount_Flags, const string& local_Mount_Options) {
860 partition_fs_flags_struct flags;
861 flags.File_System = local_File_System;
862 flags.Mount_Flags = local_Mount_Flags;
863 flags.Mount_Options = local_Mount_Options;
864 fs_flags.push_back(flags);
865}
866
Matt Mower2416a502016-04-12 19:54:46 -0500867void TWPartition::Apply_TW_Flag(const unsigned flag, const char* str, const bool val) {
868 switch (flag) {
869 case TWFLAG_ANDSEC:
870 Has_Android_Secure = val;
871 break;
872 case TWFLAG_BACKUP:
873 Can_Be_Backed_Up = val;
874 break;
875 case TWFLAG_BACKUPNAME:
876 Backup_Display_Name = str;
877 break;
878 case TWFLAG_BLOCKSIZE:
879 Format_Block_Size = (unsigned long)(atol(str));
880 break;
881 case TWFLAG_CANBEWIPED:
882 Can_Be_Wiped = val;
883 break;
884 case TWFLAG_CANENCRYPTBACKUP:
885 Can_Encrypt_Backup = val;
886 break;
887 case TWFLAG_DEFAULTS:
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600888 case TWFLAG_WAIT:
889 case TWFLAG_VERIFY:
890 case TWFLAG_CHECK:
891 case TWFLAG_NOTRIM:
892 case TWFLAG_VOLDMANAGED:
893 case TWFLAG_RESIZE:
Matt Mower2416a502016-04-12 19:54:46 -0500894 // Do nothing
895 break;
896 case TWFLAG_DISPLAY:
897 Display_Name = str;
898 break;
899 case TWFLAG_ENCRYPTABLE:
900 case TWFLAG_FORCEENCRYPT:
901 Crypto_Key_Location = str;
902 break;
Ethan Yonkere9afc3d2018-08-30 15:16:27 -0500903 case TWFLAG_FILEENCRYPTION:
904 // This flag isn't used by TWRP but is needed in 9.0 FBE decrypt
905 // fileencryption=ice:aes-256-heh
906 {
907 std::string FBE = str;
Ethan Yonkere9afc3d2018-08-30 15:16:27 -0500908 size_t colon_loc = FBE.find(":");
909 if (colon_loc == std::string::npos) {
Ethan Yonker93382822018-11-01 15:25:31 -0500910 property_set("fbe.contents", FBE.c_str());
911 property_set("fbe.filenames", "");
912 LOGINFO("FBE contents '%s', filenames ''\n", FBE.c_str());
Ethan Yonkere9afc3d2018-08-30 15:16:27 -0500913 break;
914 }
Ethan Yonker93382822018-11-01 15:25:31 -0500915 std::string FBE_contents, FBE_filenames;
Ethan Yonkere9afc3d2018-08-30 15:16:27 -0500916 FBE_contents = FBE.substr(0, colon_loc);
917 FBE_filenames = FBE.substr(colon_loc + 1);
918 property_set("fbe.contents", FBE_contents.c_str());
919 property_set("fbe.filenames", FBE_filenames.c_str());
920 LOGINFO("FBE contents '%s', filenames '%s'\n", FBE_contents.c_str(), FBE_filenames.c_str());
921 }
922 break;
Peter Cai40ed06e2019-05-24 11:38:54 +0800923 case TWFLAG_WRAPPEDKEY:
924 // Set fbe.data.wrappedkey to true
925 {
926 property_set("fbe.data.wrappedkey", "true");
927 LOGINFO("FBE wrapped key enabled\n");
928 }
929 break;
Matt Mower2416a502016-04-12 19:54:46 -0500930 case TWFLAG_FLASHIMG:
931 Can_Flash_Img = val;
932 break;
933 case TWFLAG_FSFLAGS:
Matt Mower4ab42b12016-04-21 13:52:18 -0500934 Process_FS_Flags(str);
Matt Mower2416a502016-04-12 19:54:46 -0500935 break;
936 case TWFLAG_IGNOREBLKID:
937 Ignore_Blkid = val;
938 break;
939 case TWFLAG_LENGTH:
940 Length = atoi(str);
941 break;
942 case TWFLAG_MOUNTTODECRYPT:
943 Mount_To_Decrypt = val;
944 break;
945 case TWFLAG_REMOVABLE:
946 Removable = val;
947 break;
948 case TWFLAG_RETAINLAYOUTVERSION:
949 Retain_Layout_Version = val;
950 break;
951 case TWFLAG_SETTINGSSTORAGE:
952 Is_Settings_Storage = val;
953 if (Is_Settings_Storage)
954 Is_Storage = true;
955 break;
956 case TWFLAG_STORAGE:
957 Is_Storage = val;
958 break;
959 case TWFLAG_STORAGENAME:
960 Storage_Name = str;
961 break;
962 case TWFLAG_SUBPARTITIONOF:
963 Is_SubPartition = true;
964 SubPartition_Of = str;
965 break;
966 case TWFLAG_SYMLINK:
967 Symlink_Path = str;
968 break;
969 case TWFLAG_USERDATAENCRYPTBACKUP:
970 Use_Userdata_Encryption = val;
971 if (Use_Userdata_Encryption)
972 Can_Encrypt_Backup = true;
973 break;
974 case TWFLAG_USERMRF:
975 Use_Rm_Rf = val;
976 break;
977 case TWFLAG_WIPEDURINGFACTORYRESET:
978 Wipe_During_Factory_Reset = val;
979 if (Wipe_During_Factory_Reset) {
980 Can_Be_Wiped = true;
981 Wipe_Available_in_GUI = true;
982 }
983 break;
984 case TWFLAG_WIPEINGUI:
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600985 case TWFLAG_FORMATTABLE:
Matt Mower2416a502016-04-12 19:54:46 -0500986 Wipe_Available_in_GUI = val;
987 if (Wipe_Available_in_GUI)
988 Can_Be_Wiped = true;
989 break;
Ethan Yonker1b190162016-12-05 15:25:19 -0600990 case TWFLAG_SLOTSELECT:
991 SlotSelect = true;
992 break;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600993 case TWFLAG_ALTDEVICE:
994 Alternate_Block_Device = str;
995 break;
Peter Caic50044b2019-10-17 08:49:10 +0800996 case TWFLAG_ADOPTED_MOUNT_DELAY:
997 Adopted_Mount_Delay = atoi(str);
998 break;
Ethan Yonker93382822018-11-01 15:25:31 -0500999 case TWFLAG_KEYDIRECTORY:
1000 Key_Directory = str;
Peter Cai39881ab2019-11-02 19:22:38 +08001001 case TWFLAG_DM_USE_ORIGINAL_PATH:
1002 Use_Original_Path = true;
Matt Mower2416a502016-04-12 19:54:46 -05001003 default:
1004 // Should not get here
1005 LOGINFO("Flag identified for processing, but later unmatched: %i\n", flag);
1006 break;
1007 }
1008}
Dees_Troy51127312012-09-08 13:08:49 -04001009
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06001010void TWPartition::Process_TW_Flags(char *flags, bool Display_Error, int fstab_ver) {
Matt Mower2416a502016-04-12 19:54:46 -05001011 char separator[2] = {'\n', 0};
1012 char *ptr, *savep;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06001013 char source_separator = ';';
1014
1015 if (fstab_ver == 2)
1016 source_separator = ',';
Matt Mower2416a502016-04-12 19:54:46 -05001017
1018 // Semicolons within double-quotes are not forbidden, so replace
1019 // only the semicolons intended as separators with '\n' for strtok
1020 for (unsigned i = 0, skip = 0; i < strlen(flags); i++) {
1021 if (flags[i] == '\"')
Dees_Troya13d74f2013-03-24 08:54:55 -05001022 skip = !skip;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06001023 if (!skip && flags[i] == source_separator)
Matt Mower2416a502016-04-12 19:54:46 -05001024 flags[i] = separator[0];
Dees_Troy51127312012-09-08 13:08:49 -04001025 }
1026
Matt Mower2416a502016-04-12 19:54:46 -05001027 // Avoid issues with potentially nested strtok by using strtok_r
1028 ptr = strtok_r(flags, separator, &savep);
1029 while (ptr) {
1030 int ptr_len = strlen(ptr);
1031 const struct flag_list* tw_flag = tw_flags;
Hashcode62bd9e02013-11-19 21:59:42 -08001032
Matt Mower2416a502016-04-12 19:54:46 -05001033 for (; tw_flag->name; tw_flag++) {
1034 int flag_len = strlen(tw_flag->name);
1035
1036 if (strncmp(ptr, tw_flag->name, flag_len) == 0) {
1037 bool flag_val = false;
1038
1039 if (ptr_len > flag_len && (tw_flag->name)[flag_len-1] != '='
1040 && ptr[flag_len] != '=') {
1041 // Handle flags with same starting string
1042 // (e.g. backup and backupname)
1043 continue;
1044 } else if (ptr_len > flag_len && ptr[flag_len] == '=') {
1045 // Handle flags with dual format: Part 1
1046 // (e.g. backup and backup=y. backup=y handled here)
1047 ptr += flag_len + 1;
1048 TWFunc::Strip_Quotes(ptr);
1049 // Skip flags with empty argument
1050 // (e.g. backup=)
1051 if (strlen(ptr) == 0) {
1052 LOGINFO("Flag missing argument or should not include '=': %s=\n", tw_flag->name);
1053 break;
1054 }
1055 flag_val = strchr("yY1", *ptr) != NULL;
1056 } else if (ptr_len == flag_len
1057 && (tw_flag->name)[flag_len-1] == '=') {
1058 // Skip flags missing argument after =
1059 // (e.g. backupname=)
1060 LOGINFO("Flag missing argument: %s\n", tw_flag->name);
1061 break;
1062 } else if (ptr_len > flag_len
1063 && (tw_flag->name)[flag_len-1] == '=') {
1064 // Handle arguments to flags
1065 // (e.g. backupname="My Stuff")
1066 ptr += flag_len;
1067 TWFunc::Strip_Quotes(ptr);
1068 // Skip flags with empty argument
1069 // (e.g. backupname="")
1070 if (strlen(ptr) == 0) {
1071 LOGINFO("Flag missing argument: %s\n", tw_flag->name);
1072 break;
1073 }
1074 } else if (ptr_len == flag_len) {
1075 // Handle flags with dual format: Part 2
1076 // (e.g. backup and backup=y. backup handled here)
1077 flag_val = true;
Ethan Yonker96af84a2015-01-05 14:58:36 -06001078 } else {
Matt Mower2416a502016-04-12 19:54:46 -05001079 LOGINFO("Flag matched, but could not be processed: %s\n", ptr);
1080 break;
Ethan Yonker96af84a2015-01-05 14:58:36 -06001081 }
Matt Mower2416a502016-04-12 19:54:46 -05001082
1083 Apply_TW_Flag(tw_flag->flag, ptr, flag_val);
1084 break;
Ethan Yonker96af84a2015-01-05 14:58:36 -06001085 }
Matt Mower2416a502016-04-12 19:54:46 -05001086 }
1087 if (tw_flag->name == 0) {
Dees_Troy51127312012-09-08 13:08:49 -04001088 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001089 LOGERR("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -04001090 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001091 LOGINFO("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -04001092 }
Matt Mower2416a502016-04-12 19:54:46 -05001093 ptr = strtok_r(NULL, separator, &savep);
Dees_Troy51127312012-09-08 13:08:49 -04001094 }
Dees_Troy51a0e822012-09-05 15:24:24 -04001095}
1096
Dees_Troy5bf43922012-09-07 16:07:55 -04001097bool TWPartition::Is_File_System(string File_System) {
1098 if (File_System == "ext2" ||
Dees_Troy63c8df72012-09-10 14:02:05 -04001099 File_System == "ext3" ||
Dees_Troy5bf43922012-09-07 16:07:55 -04001100 File_System == "ext4" ||
1101 File_System == "vfat" ||
1102 File_System == "ntfs" ||
1103 File_System == "yaffs2" ||
bigbiff bigbiff3e146522012-11-14 14:32:59 -05001104 File_System == "exfat" ||
Dees_Troye5017042013-08-29 16:38:55 +00001105 File_System == "f2fs" ||
Erfan Abdib859e912019-02-05 18:18:46 +02001106 File_System == "squashfs" ||
Dees_Troy5bf43922012-09-07 16:07:55 -04001107 File_System == "auto")
1108 return true;
1109 else
1110 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001111}
1112
Dees_Troy5bf43922012-09-07 16:07:55 -04001113bool TWPartition::Is_Image(string File_System) {
Dees_Troy5fcd8f92012-10-16 12:22:05 -04001114 if (File_System == "emmc" || File_System == "mtd" || File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -04001115 return true;
1116 else
1117 return false;
1118}
1119
Dees_Troy51127312012-09-08 13:08:49 -04001120bool TWPartition::Make_Dir(string Path, bool Display_Error) {
Ethan Yonker93ac7a02016-11-07 22:05:58 -06001121 if (TWFunc::Get_D_Type_From_Stat(Path) != S_IFDIR)
1122 unlink(Path.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001123 if (!TWFunc::Path_Exists(Path)) {
Dees_Troy51127312012-09-08 13:08:49 -04001124 if (mkdir(Path.c_str(), 0777) == -1) {
1125 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001126 gui_msg(Msg(msg::kError, "create_folder_strerr=Can not create '{1}' folder ({2}).")(Path)(strerror(errno)));
Dees_Troy51127312012-09-08 13:08:49 -04001127 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001128 LOGINFO("Can not create '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -04001129 return false;
1130 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001131 LOGINFO("Created '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -04001132 return true;
1133 }
1134 }
1135 return true;
1136}
1137
Dees_Troy5bf43922012-09-07 16:07:55 -04001138void TWPartition::Setup_File_System(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001139 Can_Be_Mounted = true;
1140 Can_Be_Wiped = true;
1141
Dees_Troy5bf43922012-09-07 16:07:55 -04001142 // Make the mount point folder if it doesn't exist
Dees_Troy51127312012-09-08 13:08:49 -04001143 Make_Dir(Mount_Point, Display_Error);
bigbiffce8f83c2015-12-12 18:30:21 -05001144 Backup_Method = BM_FILES;
Dees_Troy5bf43922012-09-07 16:07:55 -04001145}
1146
Ethan Yonker1b190162016-12-05 15:25:19 -06001147void TWPartition::Setup_Image() {
Dees_Troy5bf43922012-09-07 16:07:55 -04001148 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
1149 Backup_Name = Display_Name;
Gary Peck82599a82012-11-21 16:23:12 -08001150 if (Current_File_System == "emmc")
bigbiffce8f83c2015-12-12 18:30:21 -05001151 Backup_Method = BM_DD;
Gary Peck82599a82012-11-21 16:23:12 -08001152 else if (Current_File_System == "mtd" || Current_File_System == "bml")
bigbiffce8f83c2015-12-12 18:30:21 -05001153 Backup_Method = BM_FLASH_UTILS;
Dees_Troy5bf43922012-09-07 16:07:55 -04001154 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001155 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 -04001156}
1157
Dees_Troye58d5262012-09-21 12:27:57 -04001158void TWPartition::Setup_AndSec(void) {
Dees_Troya13d74f2013-03-24 08:54:55 -05001159 Backup_Display_Name = "Android Secure";
Dees_Troye58d5262012-09-21 12:27:57 -04001160 Backup_Name = "and-sec";
Dees_Troya13d74f2013-03-24 08:54:55 -05001161 Can_Be_Backed_Up = true;
Dees_Troye58d5262012-09-21 12:27:57 -04001162 Has_Android_Secure = true;
1163 Symlink_Path = Mount_Point + "/.android_secure";
1164 Symlink_Mount_Point = "/and-sec";
1165 Backup_Path = Symlink_Mount_Point;
1166 Make_Dir("/and-sec", true);
1167 Recreate_AndSec_Folder();
Kjell Braden3126a112016-06-19 16:58:15 +00001168 Mount_Storage_Retry(true);
Dees_Troye58d5262012-09-21 12:27:57 -04001169}
1170
that9e0593e2014-10-08 00:01:24 +02001171void TWPartition::Setup_Data_Media() {
Ethan Yonker6277c792014-09-15 14:54:30 -05001172 LOGINFO("Setting up '%s' as data/media emulated storage.\n", Mount_Point.c_str());
Ethan Yonker66a19492015-12-10 10:19:45 -06001173 if (Storage_Name.empty() || Storage_Name == "Data")
1174 Storage_Name = "Internal Storage";
Ethan Yonker6277c792014-09-15 14:54:30 -05001175 Has_Data_Media = true;
1176 Is_Storage = true;
Ethan Yonker66a19492015-12-10 10:19:45 -06001177 Storage_Path = Mount_Point + "/media";
Ethan Yonker6277c792014-09-15 14:54:30 -05001178 Symlink_Path = Storage_Path;
Ethan Yonker66a19492015-12-10 10:19:45 -06001179 if (Mount_Point == "/data") {
1180 Is_Settings_Storage = true;
1181 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
1182 Make_Dir("/emmc", false);
1183 Symlink_Mount_Point = "/emmc";
1184 } else {
1185 Make_Dir("/sdcard", false);
1186 Symlink_Mount_Point = "/sdcard";
1187 }
1188 if (Mount(false) && TWFunc::Path_Exists(Mount_Point + "/media/0")) {
1189 Storage_Path = Mount_Point + "/media/0";
1190 Symlink_Path = Storage_Path;
1191 DataManager::SetValue(TW_INTERNAL_PATH, Mount_Point + "/media/0");
1192 UnMount(true);
1193 }
1194 DataManager::SetValue("tw_has_internal", 1);
1195 DataManager::SetValue("tw_has_data_media", 1);
Ethan Yonker3fdcda42016-11-30 12:29:37 -06001196 backup_exclusions.add_absolute_dir("/data/data/com.google.android.music/files");
Darth90864aa52020-01-14 20:58:16 +00001197 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 +01001198 backup_exclusions.add_absolute_dir("/data/vendor/dumpsys");
bigbiff349ea552020-06-19 16:07:38 -04001199 backup_exclusions.add_absolute_dir("/data/cache");
Ethan Yonker6355b562017-05-01 09:42:17 -05001200 wipe_exclusions.add_absolute_dir(Mount_Point + "/misc/vold"); // adopted storage keys
Ethan Yonker79f88bd2016-12-09 14:52:12 -06001201 ExcludeAll(Mount_Point + "/.layout_version");
1202 ExcludeAll(Mount_Point + "/system/storage.xml");
Ethan Yonker6277c792014-09-15 14:54:30 -05001203 } else {
Ethan Yonker66a19492015-12-10 10:19:45 -06001204 if (Mount(true) && TWFunc::Path_Exists(Mount_Point + "/media/0")) {
1205 Storage_Path = Mount_Point + "/media/0";
1206 Symlink_Path = Storage_Path;
1207 UnMount(true);
1208 }
Ethan Yonker6277c792014-09-15 14:54:30 -05001209 }
Ethan Yonker79f88bd2016-12-09 14:52:12 -06001210 ExcludeAll(Mount_Point + "/media");
Ethan Yonker6277c792014-09-15 14:54:30 -05001211}
1212
Dees_Troy5bf43922012-09-07 16:07:55 -04001213void TWPartition::Find_Real_Block_Device(string& Block, bool Display_Error) {
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06001214 char device[PATH_MAX], realDevice[PATH_MAX];
Dees_Troy5bf43922012-09-07 16:07:55 -04001215
Peter Cai39881ab2019-11-02 19:22:38 +08001216 Original_Path = Block;
1217
Dees_Troy5bf43922012-09-07 16:07:55 -04001218 strcpy(device, Block.c_str());
1219 memset(realDevice, 0, sizeof(realDevice));
1220 while (readlink(device, realDevice, sizeof(realDevice)) > 0)
1221 {
1222 strcpy(device, realDevice);
1223 memset(realDevice, 0, sizeof(realDevice));
1224 }
1225
1226 if (device[0] != '/') {
1227 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001228 LOGERR("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001229 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001230 LOGINFO("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001231 return;
Dees_Troy51a0e822012-09-05 15:24:24 -04001232 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -04001233 Block = device;
1234 return;
1235 }
1236}
1237
Kjell Braden3126a112016-06-19 16:58:15 +00001238bool TWPartition::Mount_Storage_Retry(bool Display_Error) {
Dees_Troy8e337f32012-10-13 22:07:49 -04001239 // On some devices, storage doesn't want to mount right away, retry and sleep
Kjell Braden3126a112016-06-19 16:58:15 +00001240 if (!Mount(Display_Error)) {
Dees_Troy8e337f32012-10-13 22:07:49 -04001241 int retry_count = 5;
1242 while (retry_count > 0 && !Mount(false)) {
1243 usleep(500000);
1244 retry_count--;
1245 }
Kjell Braden3126a112016-06-19 16:58:15 +00001246 return Mount(Display_Error);
Dees_Troy8e337f32012-10-13 22:07:49 -04001247 }
Kjell Braden3126a112016-06-19 16:58:15 +00001248 return true;
Dees_Troy8e337f32012-10-13 22:07:49 -04001249}
1250
Dees_Troy38bd7602012-09-14 13:33:53 -04001251bool TWPartition::Find_MTD_Block_Device(string MTD_Name) {
1252 FILE *fp = NULL;
1253 char line[255];
1254
1255 fp = fopen("/proc/mtd", "rt");
1256 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001257 LOGERR("Device does not support /proc/mtd\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001258 return false;
1259 }
1260
1261 while (fgets(line, sizeof(line), fp) != NULL)
1262 {
1263 char device[32], label[32];
1264 unsigned long size = 0;
Dees_Troy38bd7602012-09-14 13:33:53 -04001265 int deviceId;
1266
1267 sscanf(line, "%s %lx %*s %*c%s", device, &size, label);
1268
1269 // Skip header and blank lines
1270 if ((strcmp(device, "dev:") == 0) || (strlen(line) < 8))
1271 continue;
1272
1273 // Strip off the trailing " from the label
1274 label[strlen(label)-1] = '\0';
1275
1276 if (strcmp(label, MTD_Name.c_str()) == 0) {
1277 // We found our device
1278 // Strip off the trailing : from the device
1279 device[strlen(device)-1] = '\0';
1280 if (sscanf(device,"mtd%d", &deviceId) == 1) {
1281 sprintf(device, "/dev/block/mtdblock%d", deviceId);
1282 Primary_Block_Device = device;
Dees_Troy76543db2013-06-19 16:24:30 +00001283 fclose(fp);
1284 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -04001285 }
1286 }
1287 }
1288 fclose(fp);
1289
1290 return false;
1291}
1292
Dees_Troy51127312012-09-08 13:08:49 -04001293bool TWPartition::Get_Size_Via_statfs(bool Display_Error) {
1294 struct statfs st;
1295 string Local_Path = Mount_Point + "/.";
1296
1297 if (!Mount(Display_Error))
1298 return false;
1299
1300 if (statfs(Local_Path.c_str(), &st) != 0) {
1301 if (!Removable) {
1302 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001303 LOGERR("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -04001304 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001305 LOGINFO("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -04001306 }
1307 return false;
1308 }
1309 Size = (st.f_blocks * st.f_bsize);
1310 Used = ((st.f_blocks - st.f_bfree) * st.f_bsize);
1311 Free = (st.f_bfree * st.f_bsize);
1312 Backup_Size = Used;
1313 return true;
1314}
1315
1316bool TWPartition::Get_Size_Via_df(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001317 FILE* fp;
1318 char command[255], line[512];
1319 int include_block = 1;
1320 unsigned int min_len;
1321
1322 if (!Mount(Display_Error))
1323 return false;
1324
Dees_Troy38bd7602012-09-14 13:33:53 -04001325 min_len = Actual_Block_Device.size() + 2;
Dees_Troy51127312012-09-08 13:08:49 -04001326 sprintf(command, "df %s > /tmp/dfoutput.txt", Mount_Point.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001327 TWFunc::Exec_Cmd(command);
Dees_Troy51127312012-09-08 13:08:49 -04001328 fp = fopen("/tmp/dfoutput.txt", "rt");
1329 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001330 LOGINFO("Unable to open /tmp/dfoutput.txt.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001331 return false;
Dees_Troy51127312012-09-08 13:08:49 -04001332 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001333
1334 while (fgets(line, sizeof(line), fp) != NULL)
1335 {
1336 unsigned long blocks, used, available;
1337 char device[64];
1338 char tmpString[64];
1339
1340 if (strncmp(line, "Filesystem", 10) == 0)
1341 continue;
1342 if (strlen(line) < min_len) {
1343 include_block = 0;
1344 continue;
1345 }
1346 if (include_block) {
1347 sscanf(line, "%s %lu %lu %lu", device, &blocks, &used, &available);
1348 } else {
1349 // The device block string is so long that the df information is on the next line
1350 int space_count = 0;
Dees_Troye58d5262012-09-21 12:27:57 -04001351 sprintf(tmpString, "/dev/block/%s", Actual_Block_Device.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001352 while (tmpString[space_count] == 32)
1353 space_count++;
1354 sscanf(line + space_count, "%lu %lu %lu", &blocks, &used, &available);
1355 }
1356
1357 // Adjust block size to byte size
1358 Size = blocks * 1024ULL;
1359 Used = used * 1024ULL;
1360 Free = available * 1024ULL;
1361 Backup_Size = Used;
1362 }
1363 fclose(fp);
1364 return true;
1365}
1366
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001367unsigned long long TWPartition::IOCTL_Get_Block_Size() {
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001368 Find_Actual_Block_Device();
Ethan Yonker483e9f42016-01-11 22:21:18 -06001369
1370 return TWFunc::IOCTL_Get_Block_Size(Actual_Block_Device.c_str());
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001371}
1372
Dees_Troy5bf43922012-09-07 16:07:55 -04001373bool TWPartition::Find_Partition_Size(void) {
1374 FILE* fp;
1375 char line[512];
1376 string tmpdevice;
1377
igoriok87e3d932013-01-31 21:03:53 +02001378 fp = fopen("/proc/dumchar_info", "rt");
1379 if (fp != NULL) {
1380 while (fgets(line, sizeof(line), fp) != NULL)
1381 {
1382 char label[32], device[32];
1383 unsigned long size = 0;
1384
thatd43bf2d2014-09-21 23:13:02 +02001385 sscanf(line, "%s %lx %*x %*u %s", label, &size, device);
igoriok87e3d932013-01-31 21:03:53 +02001386
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05001387 // Skip header, annotation and blank lines
igoriok87e3d932013-01-31 21:03:53 +02001388 if ((strncmp(device, "/dev/", 5) != 0) || (strlen(line) < 8))
1389 continue;
1390
1391 tmpdevice = "/dev/";
1392 tmpdevice += label;
1393 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
1394 Size = size;
1395 fclose(fp);
1396 return true;
1397 }
1398 }
1399 }
1400
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001401 unsigned long long ioctl_size = IOCTL_Get_Block_Size();
1402 if (ioctl_size) {
1403 Size = ioctl_size;
1404 return true;
1405 }
1406
Dees_Troy5bf43922012-09-07 16:07:55 -04001407 // In this case, we'll first get the partitions we care about (with labels)
1408 fp = fopen("/proc/partitions", "rt");
1409 if (fp == NULL)
1410 return false;
1411
1412 while (fgets(line, sizeof(line), fp) != NULL)
1413 {
1414 unsigned long major, minor, blocks;
1415 char device[512];
Dees_Troy5bf43922012-09-07 16:07:55 -04001416
Dees_Troy63c8df72012-09-10 14:02:05 -04001417 if (strlen(line) < 7 || line[0] == 'm') continue;
Dees_Troy5bf43922012-09-07 16:07:55 -04001418 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
1419
1420 tmpdevice = "/dev/block/";
1421 tmpdevice += device;
Dees_Troy38bd7602012-09-14 13:33:53 -04001422 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001423 // Adjust block size to byte size
1424 Size = blocks * 1024ULL;
1425 fclose(fp);
1426 return true;
1427 }
1428 }
1429 fclose(fp);
1430 return false;
1431}
1432
Dees_Troy5bf43922012-09-07 16:07:55 -04001433bool TWPartition::Is_Mounted(void) {
1434 if (!Can_Be_Mounted)
1435 return false;
1436
1437 struct stat st1, st2;
1438 string test_path;
1439
1440 // Check to see if the mount point directory exists
1441 test_path = Mount_Point + "/.";
1442 if (stat(test_path.c_str(), &st1) != 0) return false;
1443
1444 // Check to see if the directory above the mount point exists
1445 test_path = Mount_Point + "/../.";
1446 if (stat(test_path.c_str(), &st2) != 0) return false;
1447
1448 // Compare the device IDs -- if they match then we're (probably) using tmpfs instead of an actual device
1449 int ret = (st1.st_dev != st2.st_dev) ? true : false;
1450
1451 return ret;
1452}
1453
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05001454bool TWPartition::Is_File_System_Writable(void) {
1455 if (!Is_File_System(Current_File_System) || !Is_Mounted())
1456 return false;
1457
1458 string test_path = Mount_Point + "/.";
1459 return (access(test_path.c_str(), W_OK) == 0);
1460}
1461
Dees_Troy5bf43922012-09-07 16:07:55 -04001462bool TWPartition::Mount(bool Display_Error) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001463 int exfat_mounted = 0;
Martin Dünkelmann6d843782018-12-27 23:40:28 +01001464 unsigned int flags = Mount_Flags;
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001465
Dees_Troy5bf43922012-09-07 16:07:55 -04001466 if (Is_Mounted()) {
1467 return true;
1468 } else if (!Can_Be_Mounted) {
1469 return false;
1470 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001471
1472 Find_Actual_Block_Device();
1473
1474 // Check the current file system before mounting
1475 Check_FS_Type();
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001476 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/exfat-fuse")) {
Dees_Troye34c1332013-02-06 19:13:00 +00001477 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 +00001478 LOGINFO("cmd: %s\n", cmd.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001479 string result;
1480 if (TWFunc::Exec_Cmd(cmd, result) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001481 LOGINFO("exfat-fuse failed to mount with result '%s', trying vfat\n", result.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001482 Current_File_System = "vfat";
1483 } else {
1484#ifdef TW_NO_EXFAT_FUSE
1485 UnMount(false);
1486 // We'll let the kernel handle it but using exfat-fuse to detect if the file system is actually exfat
1487 // Some kernels let us mount vfat as exfat which doesn't work out too well
1488#else
1489 exfat_mounted = 1;
1490#endif
1491 }
1492 }
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001493
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001494 if (Current_File_System == "ntfs" && (TWFunc::Path_Exists("/sbin/ntfs-3g") || TWFunc::Path_Exists("/sbin/mount.ntfs"))) {
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001495 string cmd;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001496 string Ntfsmount_Binary = "";
1497
1498 if (TWFunc::Path_Exists("/sbin/ntfs-3g"))
1499 Ntfsmount_Binary = "ntfs-3g";
1500 else if (TWFunc::Path_Exists("/sbin/mount.ntfs"))
1501 Ntfsmount_Binary = "mount.ntfs";
1502
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001503 if (Mount_Read_Only)
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001504 cmd = "/sbin/" + Ntfsmount_Binary + " -o ro " + Actual_Block_Device + " " + Mount_Point;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001505 else
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001506 cmd = "/sbin/" + Ntfsmount_Binary + " " + Actual_Block_Device + " " + Mount_Point;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001507 LOGINFO("cmd: '%s'\n", cmd.c_str());
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001508
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001509 if (TWFunc::Exec_Cmd(cmd) == 0) {
1510 return true;
1511 } else {
1512 LOGINFO("ntfs-3g failed to mount, trying regular mount method.\n");
1513 }
1514 }
1515
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001516 if (Mount_Read_Only)
1517 flags |= MS_RDONLY;
1518
Dees_Troy22042032012-12-18 21:23:08 +00001519 if (Fstab_File_System == "yaffs2") {
1520 // mount an MTD partition as a YAFFS2 filesystem.
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001521 flags = MS_NOATIME | MS_NODEV | MS_NODIRATIME;
1522 if (Mount_Read_Only)
1523 flags |= MS_RDONLY;
Dees_Troy76543db2013-06-19 16:24:30 +00001524 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags, NULL) < 0) {
1525 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags | MS_RDONLY, NULL) < 0) {
1526 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001527 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy76543db2013-06-19 16:24:30 +00001528 else
1529 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
1530 return false;
1531 } else {
1532 LOGINFO("Mounted '%s' (MTD) as RO\n", Mount_Point.c_str());
1533 return true;
1534 }
1535 } else {
1536 struct stat st;
1537 string test_path = Mount_Point;
1538 if (stat(test_path.c_str(), &st) < 0) {
1539 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001540 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy76543db2013-06-19 16:24:30 +00001541 else
1542 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
1543 return false;
1544 }
1545 mode_t new_mode = st.st_mode | S_IXUSR | S_IXGRP | S_IXOTH;
1546 if (new_mode != st.st_mode) {
1547 LOGINFO("Fixing execute permissions for %s\n", Mount_Point.c_str());
1548 if (chmod(Mount_Point.c_str(), new_mode) < 0) {
1549 if (Display_Error)
1550 LOGERR("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
1551 else
1552 LOGINFO("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
1553 return false;
1554 }
1555 }
Dees_Troy22042032012-12-18 21:23:08 +00001556 return true;
Dees_Troy76543db2013-06-19 16:24:30 +00001557 }
thatc7572eb2015-03-31 18:55:30 +02001558 }
1559
1560 string mount_fs = Current_File_System;
1561 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sys/module/texfat"))
1562 mount_fs = "texfat";
1563
1564 if (!exfat_mounted &&
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001565 mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), mount_fs.c_str(), flags, Mount_Options.c_str()) != 0 &&
1566 mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), mount_fs.c_str(), flags, NULL) != 0) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001567#ifdef TW_NO_EXFAT_FUSE
1568 if (Current_File_System == "exfat") {
Dees_Troy2673cec2013-04-02 20:22:16 +00001569 LOGINFO("Mounting exfat failed, trying vfat...\n");
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001570 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), "vfat", 0, NULL) != 0) {
Dees_Troy85f44ed2013-01-09 18:42:36 +00001571 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001572 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy85f44ed2013-01-09 18:42:36 +00001573 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001574 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001575 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 +00001576 return false;
Dees_Troy85f44ed2013-01-09 18:42:36 +00001577 }
Dees_Troyb05ddee2013-01-28 20:24:50 +00001578 } else {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001579#endif
bigbiff bigbiff26774a02014-03-29 18:22:00 -04001580 if (!Removable && Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001581 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001582 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001583 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
1584 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 +00001585 return false;
1586#ifdef TW_NO_EXFAT_FUSE
Dees_Troy85f44ed2013-01-09 18:42:36 +00001587 }
1588#endif
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001589 }
Ethan Yonker253368a2014-11-25 15:00:52 -06001590
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001591 if (Removable)
1592 Update_Size(Display_Error);
1593
xiaolu9416f4f2015-06-04 08:22:23 +08001594 if (!Symlink_Mount_Point.empty() && TWFunc::Path_Exists(Symlink_Path)) {
codelover2a3d4ce2015-03-14 20:26:49 +08001595 string Command = "mount -o bind '" + Symlink_Path + "' '" + Symlink_Mount_Point + "'";
Vojtech Bocek05534202013-09-11 08:11:56 +02001596 TWFunc::Exec_Cmd(Command);
Dees_Troy5bf43922012-09-07 16:07:55 -04001597 }
Chaosmasterda974802020-01-26 21:09:28 +01001598
1599 if (Mount_Point == "/system_root") {
1600 unlink("/system");
1601 mkdir("/system", 0755);
1602 mount("/system_root/system", "/system", "auto", MS_BIND, NULL);
1603 }
1604
Dees_Troy5bf43922012-09-07 16:07:55 -04001605 return true;
1606}
1607
1608bool TWPartition::UnMount(bool Display_Error) {
Chaosmasterda974802020-01-26 21:09:28 +01001609 if (Mount_Point == "/system_root") {
1610 if (umount("/system") == -1)
1611 umount2("/system", MNT_DETACH);
1612 rmdir("/system");
1613 symlink("/system_root/system", "/system");
1614 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001615 if (Is_Mounted()) {
1616 int never_unmount_system;
1617
1618 DataManager::GetValue(TW_DONT_UNMOUNT_SYSTEM, never_unmount_system);
Captain Throwback9d6feb52018-07-27 10:05:24 -04001619 if (never_unmount_system == 1 && Mount_Point == PartitionManager.Get_Android_Root_Path())
Dees_Troy5bf43922012-09-07 16:07:55 -04001620 return true; // Never unmount system if you're not supposed to unmount it
1621
Matt Mowera8e6d832017-02-14 20:20:59 -06001622 if (Is_Storage && MTP_Storage_ID > 0)
Ethan Yonker726a0202014-12-16 20:01:38 -06001623 PartitionManager.Remove_MTP_Storage(MTP_Storage_ID);
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001624
Dees_Troy38bd7602012-09-14 13:33:53 -04001625 if (!Symlink_Mount_Point.empty())
1626 umount(Symlink_Mount_Point.c_str());
1627
Dees_Troyb05ddee2013-01-28 20:24:50 +00001628 umount(Mount_Point.c_str());
1629 if (Is_Mounted()) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001630 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001631 gui_msg(Msg(msg::kError, "fail_unmount=Failed to unmount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy5bf43922012-09-07 16:07:55 -04001632 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001633 LOGINFO("Unable to unmount '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001634 return false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001635 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -04001636 return true;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001637 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001638 } else {
1639 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001640 }
1641}
1642
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05001643bool TWPartition::ReMount(bool Display_Error) {
1644 if (UnMount(Display_Error))
1645 return Mount(Display_Error);
1646 return false;
1647}
1648
1649bool TWPartition::ReMount_RW(bool Display_Error) {
1650 // No need to remount if already mounted rw
1651 if (Is_File_System_Writable())
1652 return true;
1653
1654 bool ro = Mount_Read_Only;
1655 int flags = Mount_Flags;
1656
1657 Mount_Read_Only = false;
1658 Mount_Flags &= ~MS_RDONLY;
1659
1660 bool ret = ReMount(Display_Error);
1661
1662 Mount_Read_Only = ro;
1663 Mount_Flags = flags;
1664
1665 return ret;
1666}
1667
Gary Peck43acadf2012-11-21 21:19:01 -08001668bool TWPartition::Wipe(string New_File_System) {
Ethan Yonker726a0202014-12-16 20:01:38 -06001669 bool wiped = false, update_crypt = false, recreate_media = true;
Dees_Troy16c2b312013-01-15 16:51:18 +00001670 int check;
1671 string Layout_Filename = Mount_Point + "/.layout_version";
1672
Dees_Troy38bd7602012-09-14 13:33:53 -04001673 if (!Can_Be_Wiped) {
Matt Mower3c366972015-12-25 19:28:31 -06001674 gui_msg(Msg(msg::kError, "cannot_wipe=Partition {1} cannot be wiped.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04001675 return false;
1676 }
1677
Dees_Troyc51f1f92012-09-20 15:32:13 -04001678 if (Mount_Point == "/cache")
Dees_Troy2673cec2013-04-02 20:22:16 +00001679 Log_Offset = 0;
Dees_Troyc51f1f92012-09-20 15:32:13 -04001680
bigbiff35d2bfd2021-01-23 14:08:03 -05001681 if (Mount_Point == PartitionManager.Get_Android_Root_Path()) {
1682 if (tw_get_default_metadata(PartitionManager.Get_Android_Root_Path().c_str()) != 0) {
1683 gui_msg(Msg(msg::kWarning, "restore_system_context=Unable to get default context for {1} -- Android may not boot.")(PartitionManager.Get_Android_Root_Path()));
1684 }
1685 }
1686
Dees_Troy16c2b312013-01-15 16:51:18 +00001687 if (Retain_Layout_Version && Mount(false) && TWFunc::Path_Exists(Layout_Filename))
1688 TWFunc::copy_file(Layout_Filename, "/.layout_version", 0600);
1689 else
1690 unlink("/.layout_version");
Dees_Troy38bd7602012-09-14 13:33:53 -04001691
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001692 if (Has_Data_Media && Current_File_System == New_File_System) {
Dees_Troy16c2b312013-01-15 16:51:18 +00001693 wiped = Wipe_Data_Without_Wiping_Media();
bigbiff349ea552020-06-19 16:07:38 -04001694 if (Mount_Point == "/data" && TWFunc::get_log_dir() == DATA_LOGS_DIR) {
1695 bool created = Recreate_Logs_Dir();
1696 if (!created)
1697 LOGERR("Unable to create log directory for TWRP\n");
bigbiffa2bd7b72020-05-07 21:45:34 -04001698 }
Ethan Yonker5eac2222014-06-11 12:22:55 -05001699 recreate_media = false;
Dees_Troy16c2b312013-01-15 16:51:18 +00001700 } else {
Dees_Troy16c2b312013-01-15 16:51:18 +00001701 DataManager::GetValue(TW_RM_RF_VAR, check);
1702
Hashcodedabfd492013-08-29 22:45:30 -07001703 if (check || Use_Rm_Rf)
Dees_Troy16c2b312013-01-15 16:51:18 +00001704 wiped = Wipe_RMRF();
1705 else if (New_File_System == "ext4")
1706 wiped = Wipe_EXT4();
1707 else if (New_File_System == "ext2" || New_File_System == "ext3")
dianlujitao4879b372018-12-03 18:45:47 +08001708 wiped = Wipe_EXTFS(New_File_System);
Dees_Troy16c2b312013-01-15 16:51:18 +00001709 else if (New_File_System == "vfat")
1710 wiped = Wipe_FAT();
1711 else if (New_File_System == "exfat")
1712 wiped = Wipe_EXFAT();
1713 else if (New_File_System == "yaffs2")
1714 wiped = Wipe_MTD();
Dees_Troye5017042013-08-29 16:38:55 +00001715 else if (New_File_System == "f2fs")
1716 wiped = Wipe_F2FS();
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001717 else if (New_File_System == "ntfs")
1718 wiped = Wipe_NTFS();
Dees_Troy16c2b312013-01-15 16:51:18 +00001719 else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001720 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 +00001721 unlink("/.layout_version");
1722 return false;
1723 }
1724 update_crypt = wiped;
Gary Pecke8bc5d72012-12-21 06:45:25 -08001725 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001726
Gary Pecke8bc5d72012-12-21 06:45:25 -08001727 if (wiped) {
Mohd Faraz25371a72020-06-15 00:28:32 +05301728 if (Mount_Point == "/cache" && TWFunc::get_log_dir() != DATA_LOGS_DIR)
1729 DataManager::Output_Version();
1730
bigbiff35d2bfd2021-01-23 14:08:03 -05001731 if (Mount_Point == PartitionManager.Get_Android_Root_Path()) {
1732 tw_set_default_metadata(PartitionManager.Get_Android_Root_Path().c_str());
1733 }
Dees_Troy16c2b312013-01-15 16:51:18 +00001734 if (TWFunc::Path_Exists("/.layout_version") && Mount(false))
1735 TWFunc::copy_file("/.layout_version", Layout_Filename, 0600);
1736
1737 if (update_crypt) {
1738 Setup_File_System(false);
1739 if (Is_Encrypted && !Is_Decrypted) {
1740 // just wiped an encrypted partition back to its unencrypted state
1741 Is_Encrypted = false;
1742 Is_Decrypted = false;
1743 Decrypted_Block_Device = "";
1744 if (Mount_Point == "/data") {
1745 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1746 DataManager::SetValue(TW_IS_DECRYPTED, 0);
1747 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001748 }
1749 }
Ethan Yonker5eac2222014-06-11 12:22:55 -05001750
Ethan Yonker66a19492015-12-10 10:19:45 -06001751 if (Has_Data_Media && recreate_media) {
Ethan Yonker5eac2222014-06-11 12:22:55 -05001752 Recreate_Media_Folder();
1753 }
Matt Mower209c9632016-01-20 12:08:35 -06001754 if (Is_Storage && Mount(false))
1755 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Gary Pecke8bc5d72012-12-21 06:45:25 -08001756 }
Matt Mower209c9632016-01-20 12:08:35 -06001757
Gary Pecke8bc5d72012-12-21 06:45:25 -08001758 return wiped;
Dees_Troy51a0e822012-09-05 15:24:24 -04001759}
1760
Gary Peck43acadf2012-11-21 21:19:01 -08001761bool TWPartition::Wipe() {
Gary Peck82599a82012-11-21 16:23:12 -08001762 if (Is_File_System(Current_File_System))
1763 return Wipe(Current_File_System);
1764 else
1765 return Wipe(Fstab_File_System);
Gary Peck43acadf2012-11-21 21:19:01 -08001766}
1767
Dees_Troye58d5262012-09-21 12:27:57 -04001768bool TWPartition::Wipe_AndSec(void) {
1769 if (!Has_Android_Secure)
1770 return false;
1771
Dees_Troye58d5262012-09-21 12:27:57 -04001772 if (!Mount(true))
1773 return false;
1774
Ethan Yonker74db1572015-10-28 12:44:49 -05001775 gui_msg(Msg("wiping=Wiping {1}")(Backup_Display_Name));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001776 TWFunc::removeDir(Mount_Point + "/.android_secure/", true);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001777 return true;
Dees_Troye58d5262012-09-21 12:27:57 -04001778}
1779
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001780bool TWPartition::Can_Repair() {
Ethan Yonkera2719152015-05-28 09:44:41 -05001781 if (Mount_Read_Only)
1782 return false;
Matt Mower18794c82015-11-11 16:22:45 -06001783 if (Current_File_System == "vfat" && TWFunc::Path_Exists("/sbin/fsck.fat"))
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001784 return true;
1785 else if ((Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") && TWFunc::Path_Exists("/sbin/e2fsck"))
1786 return true;
1787 else if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/fsck.exfat"))
1788 return true;
1789 else if (Current_File_System == "f2fs" && TWFunc::Path_Exists("/sbin/fsck.f2fs"))
1790 return true;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001791 else if (Current_File_System == "ntfs" && (TWFunc::Path_Exists("/sbin/ntfsfix") || TWFunc::Path_Exists("/sbin/fsck.ntfs")))
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001792 return true;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001793 return false;
1794}
1795
1796bool TWPartition::Repair() {
1797 string command;
1798
1799 if (Current_File_System == "vfat") {
Matt Mower18794c82015-11-11 16:22:45 -06001800 if (!TWFunc::Path_Exists("/sbin/fsck.fat")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001801 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.fat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001802 return false;
1803 }
1804 if (!UnMount(true))
1805 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001806 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.fat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001807 Find_Actual_Block_Device();
Matt Mower18794c82015-11-11 16:22:45 -06001808 command = "/sbin/fsck.fat -y " + Actual_Block_Device;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001809 LOGINFO("Repair command: %s\n", command.c_str());
1810 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001811 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001812 return true;
1813 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001814 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001815 return false;
1816 }
1817 }
1818 if (Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") {
1819 if (!TWFunc::Path_Exists("/sbin/e2fsck")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001820 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("e2fsck"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001821 return false;
1822 }
1823 if (!UnMount(true))
1824 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001825 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("e2fsck"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001826 Find_Actual_Block_Device();
Ethan Yonkera2719152015-05-28 09:44:41 -05001827 command = "/sbin/e2fsck -fp " + Actual_Block_Device;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001828 LOGINFO("Repair command: %s\n", command.c_str());
1829 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001830 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001831 return true;
1832 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001833 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001834 return false;
1835 }
1836 }
1837 if (Current_File_System == "exfat") {
1838 if (!TWFunc::Path_Exists("/sbin/fsck.exfat")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001839 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.exfat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001840 return false;
1841 }
1842 if (!UnMount(true))
1843 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001844 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.exfat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001845 Find_Actual_Block_Device();
1846 command = "/sbin/fsck.exfat " + Actual_Block_Device;
1847 LOGINFO("Repair command: %s\n", command.c_str());
1848 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001849 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001850 return true;
1851 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001852 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001853 return false;
1854 }
1855 }
1856 if (Current_File_System == "f2fs") {
1857 if (!TWFunc::Path_Exists("/sbin/fsck.f2fs")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001858 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.f2fs"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001859 return false;
1860 }
1861 if (!UnMount(true))
1862 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001863 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.f2fs"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001864 Find_Actual_Block_Device();
1865 command = "/sbin/fsck.f2fs " + Actual_Block_Device;
1866 LOGINFO("Repair command: %s\n", command.c_str());
1867 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001868 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001869 return true;
1870 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001871 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001872 return false;
1873 }
1874 }
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001875 if (Current_File_System == "ntfs") {
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001876 string Ntfsfix_Binary;
1877 if (TWFunc::Path_Exists("/sbin/ntfsfix"))
1878 Ntfsfix_Binary = "ntfsfix";
1879 else if (TWFunc::Path_Exists("/sbin/fsck.ntfs"))
1880 Ntfsfix_Binary = "fsck.ntfs";
1881 else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001882 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("ntfsfix"));
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001883 return false;
1884 }
1885 if (!UnMount(true))
1886 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001887 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)(Ntfsfix_Binary));
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001888 Find_Actual_Block_Device();
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001889 command = "/sbin/" + Ntfsfix_Binary + " " + Actual_Block_Device;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001890 LOGINFO("Repair command: %s\n", command.c_str());
1891 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001892 gui_msg("done=Done.");
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001893 return true;
1894 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001895 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001896 return false;
1897 }
1898 }
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001899 return false;
1900}
1901
Ethan Yonkera2719152015-05-28 09:44:41 -05001902bool TWPartition::Can_Resize() {
1903 if (Mount_Read_Only)
1904 return false;
1905 if ((Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") && TWFunc::Path_Exists("/sbin/resize2fs"))
1906 return true;
1907 return false;
1908}
1909
1910bool TWPartition::Resize() {
1911 string command;
1912
1913 if (Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") {
1914 if (!Can_Repair()) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001915 LOGINFO("Cannot resize %s because %s cannot be repaired before resizing.\n", Display_Name.c_str(), Display_Name.c_str());
1916 gui_msg(Msg(msg::kError, "cannot_resize=Cannot resize {1}.")(Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001917 return false;
1918 }
1919 if (!TWFunc::Path_Exists("/sbin/resize2fs")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001920 LOGINFO("resize2fs does not exist! Cannot resize!\n");
1921 gui_msg(Msg(msg::kError, "cannot_resize=Cannot resize {1}.")(Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001922 return false;
1923 }
1924 // Repair will unmount so no need to do it twice
Ethan Yonker74db1572015-10-28 12:44:49 -05001925 gui_msg(Msg("repair_resize=Repairing {1} before resizing.")( Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001926 if (!Repair())
1927 return false;
Ethan Yonker74db1572015-10-28 12:44:49 -05001928 gui_msg(Msg("resizing=Resizing {1} using {2}...")(Display_Name)("resize2fs"));
Ethan Yonkera2719152015-05-28 09:44:41 -05001929 Find_Actual_Block_Device();
1930 command = "/sbin/resize2fs " + Actual_Block_Device;
1931 if (Length != 0) {
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001932 unsigned long long Actual_Size = IOCTL_Get_Block_Size();
1933 if (Actual_Size == 0)
1934 return false;
Ethan Yonkera2719152015-05-28 09:44:41 -05001935
Ethan Yonkera2719152015-05-28 09:44:41 -05001936 unsigned long long Block_Count;
1937 if (Length < 0) {
1938 // Reduce overall size by this length
1939 Block_Count = (Actual_Size / 1024LLU) - ((unsigned long long)(Length * -1) / 1024LLU);
1940 } else {
1941 // This is the size, not a size reduction
1942 Block_Count = ((unsigned long long)(Length) / 1024LLU);
1943 }
1944 char temp[256];
1945 sprintf(temp, "%llu", Block_Count);
1946 command += " ";
1947 command += temp;
1948 command += "K";
1949 }
1950 LOGINFO("Resize command: %s\n", command.c_str());
1951 if (TWFunc::Exec_Cmd(command) == 0) {
1952 Update_Size(true);
Ethan Yonker74db1572015-10-28 12:44:49 -05001953 gui_msg("done=Done.");
Ethan Yonkera2719152015-05-28 09:44:41 -05001954 return true;
1955 } else {
1956 Update_Size(true);
Ethan Yonker74db1572015-10-28 12:44:49 -05001957 gui_msg(Msg(msg::kError, "unable_resize=Unable to resize {1}.")(Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001958 return false;
1959 }
1960 }
1961 return false;
1962}
1963
bigbiffce8f83c2015-12-12 18:30:21 -05001964bool TWPartition::Backup(PartitionSettings *part_settings, pid_t *tar_fork_pid) {
1965 if (Backup_Method == BM_FILES)
1966 return Backup_Tar(part_settings, tar_fork_pid);
1967 else if (Backup_Method == BM_DD)
1968 return Backup_Image(part_settings);
1969 else if (Backup_Method == BM_FLASH_UTILS)
1970 return Backup_Dump_Image(part_settings);
Dees_Troy2673cec2013-04-02 20:22:16 +00001971 LOGERR("Unknown backup method for '%s'\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001972 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001973}
1974
bigbiffce8f83c2015-12-12 18:30:21 -05001975bool TWPartition::Restore(PartitionSettings *part_settings) {
Matt Mower3c366972015-12-25 19:28:31 -06001976 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, gui_parse_text("{@restoring_hdr}"));
Ethan Yonkere080c1f2016-09-19 13:50:25 -05001977 LOGINFO("Restore filename is: %s/%s\n", part_settings->Backup_Folder.c_str(), Backup_FileName.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001978
bigbiffce8f83c2015-12-12 18:30:21 -05001979 string Restore_File_System = Get_Restore_File_System(part_settings);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001980
1981 if (Is_File_System(Restore_File_System))
bigbiffce8f83c2015-12-12 18:30:21 -05001982 return Restore_Tar(part_settings);
1983 else if (Is_Image(Restore_File_System))
1984 return Restore_Image(part_settings);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001985
1986 LOGERR("Unknown restore method for '%s'\n", Mount_Point.c_str());
1987 return false;
1988}
1989
bigbiffce8f83c2015-12-12 18:30:21 -05001990string TWPartition::Get_Restore_File_System(PartitionSettings *part_settings) {
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001991 size_t first_period, second_period;
1992 string Restore_File_System;
1993
Gary Peck43acadf2012-11-21 21:19:01 -08001994 // Parse backup filename to extract the file system before wiping
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05001995 first_period = Backup_FileName.find(".");
Gary Peck43acadf2012-11-21 21:19:01 -08001996 if (first_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001997 LOGERR("Unable to find file system (first period).\n");
thatd43bf2d2014-09-21 23:13:02 +02001998 return string();
Gary Peck43acadf2012-11-21 21:19:01 -08001999 }
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002000 Restore_File_System = Backup_FileName.substr(first_period + 1, Backup_FileName.size() - first_period - 1);
Gary Peck43acadf2012-11-21 21:19:01 -08002001 second_period = Restore_File_System.find(".");
2002 if (second_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00002003 LOGERR("Unable to find file system (second period).\n");
thatd43bf2d2014-09-21 23:13:02 +02002004 return string();
Gary Peck43acadf2012-11-21 21:19:01 -08002005 }
2006 Restore_File_System.resize(second_period);
Matt Mowera0cd91d2016-12-30 18:21:42 -06002007 LOGINFO("Restore file system is: '%s'.\n", Restore_File_System.c_str());
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002008 return Restore_File_System;
Dees_Troy51a0e822012-09-05 15:24:24 -04002009}
2010
2011string TWPartition::Backup_Method_By_Name() {
bigbiffce8f83c2015-12-12 18:30:21 -05002012 if (Backup_Method == BM_NONE)
Dees_Troy38bd7602012-09-14 13:33:53 -04002013 return "none";
bigbiffce8f83c2015-12-12 18:30:21 -05002014 else if (Backup_Method == BM_FILES)
Dees_Troy38bd7602012-09-14 13:33:53 -04002015 return "files";
bigbiffce8f83c2015-12-12 18:30:21 -05002016 else if (Backup_Method == BM_DD)
Dees_Troy38bd7602012-09-14 13:33:53 -04002017 return "dd";
bigbiffce8f83c2015-12-12 18:30:21 -05002018 else if (Backup_Method == BM_FLASH_UTILS)
Dees_Troy38bd7602012-09-14 13:33:53 -04002019 return "flash_utils";
2020 else
2021 return "undefined";
2022 return "ERROR!";
Dees_Troy51a0e822012-09-05 15:24:24 -04002023}
2024
2025bool TWPartition::Decrypt(string Password) {
Dees_Troy2673cec2013-04-02 20:22:16 +00002026 LOGINFO("STUB TWPartition::Decrypt, password: '%s'\n", Password.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04002027 // Is this needed?
Dees_Troy51a0e822012-09-05 15:24:24 -04002028 return 1;
2029}
2030
2031bool TWPartition::Wipe_Encryption() {
Dees_Troy38bd7602012-09-14 13:33:53 -04002032 bool Save_Data_Media = Has_Data_Media;
Ethan Yonker93382822018-11-01 15:25:31 -05002033 bool ret = false;
2034 BasePartition* base_partition = make_partition();
2035
2036 if (!base_partition->PreWipeEncryption())
2037 goto exit;
Dees_Troy38bd7602012-09-14 13:33:53 -04002038
dianlujitao4879b372018-12-03 18:45:47 +08002039 Find_Actual_Block_Device();
2040 if (!Is_Present) {
2041 LOGINFO("Block device not present, cannot format %s.\n", Display_Name.c_str());
2042 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
2043 return false;
2044 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002045
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06002046#ifdef TW_INCLUDE_CRYPTO
bigbiffa2bd7b72020-05-07 21:45:34 -04002047 if (!UnMount(true))
2048 return false;
Ethan Yonker1b190162016-12-05 15:25:19 -06002049 if (Is_Decrypted && !Decrypted_Block_Device.empty()) {
Matt Mower2b18a532015-02-20 16:58:05 -06002050 if (delete_crypto_blk_dev((char*)("userdata")) != 0) {
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06002051 LOGERR("Error deleting crypto block device, continuing anyway.\n");
2052 }
2053 }
2054#endif
dianlujitao4879b372018-12-03 18:45:47 +08002055 Has_Data_Media = false;
Ethan Yonker93382822018-11-01 15:25:31 -05002056 Decrypted_Block_Device = "";
Dees_Troy74fb2e92013-04-15 14:35:47 +00002057 Is_Decrypted = false;
2058 Is_Encrypted = false;
Gary Pecke8bc5d72012-12-21 06:45:25 -08002059 if (Wipe(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002060 Has_Data_Media = Save_Data_Media;
2061 if (Has_Data_Media && !Symlink_Mount_Point.empty()) {
2062 Recreate_Media_Folder();
Ethan Yonker726a0202014-12-16 20:01:38 -06002063 if (Mount(false))
2064 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04002065 }
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06002066 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
Ethan Yonker83e82572014-04-04 10:59:28 -05002067#ifndef TW_OEM_BUILD
bigbiff349ea552020-06-19 16:07:38 -04002068 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 -05002069#endif
Ethan Yonker93382822018-11-01 15:25:31 -05002070 ret = true;
2071 if (!Key_Directory.empty())
2072 ret = PartitionManager.Wipe_By_Path(Key_Directory);
2073 if (ret)
2074 ret = base_partition->PostWipeEncryption();
2075 goto exit;
Dees_Troy38bd7602012-09-14 13:33:53 -04002076 } else {
2077 Has_Data_Media = Save_Data_Media;
Ethan Yonker74db1572015-10-28 12:44:49 -05002078 gui_err("format_data_err=Unable to format to remove encryption.");
Ethan Yonker726a0202014-12-16 20:01:38 -06002079 if (Has_Data_Media && Mount(false))
2080 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Ethan Yonker93382822018-11-01 15:25:31 -05002081 goto exit;
Dees_Troy38bd7602012-09-14 13:33:53 -04002082 }
Ethan Yonker93382822018-11-01 15:25:31 -05002083exit:
2084 delete base_partition;
2085 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04002086}
2087
2088void TWPartition::Check_FS_Type() {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05002089 const char* type;
2090 blkid_probe pr;
Dees_Troy5bf43922012-09-07 16:07:55 -04002091
Dees_Troy68cab492012-12-12 19:29:35 +00002092 if (Fstab_File_System == "yaffs2" || Fstab_File_System == "mtd" || Fstab_File_System == "bml" || Ignore_Blkid)
2093 return; // Running blkid on some mtd devices causes a massive crash or needs to be skipped
Dees_Troy5bf43922012-09-07 16:07:55 -04002094
Dees_Troy38bd7602012-09-14 13:33:53 -04002095 Find_Actual_Block_Device();
Dees_Troy8170a922012-09-18 15:40:25 -04002096 if (!Is_Present)
2097 return;
Dees_Troy51127312012-09-08 13:08:49 -04002098
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05002099 pr = blkid_new_probe_from_filename(Actual_Block_Device.c_str());
2100 if (blkid_do_fullprobe(pr)) {
2101 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00002102 LOGINFO("Can't probe device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05002103 return;
Dees_Troy5bf43922012-09-07 16:07:55 -04002104 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02002105
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05002106 if (blkid_probe_lookup_value(pr, "TYPE", &type, NULL) < 0) {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05002107 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00002108 LOGINFO("can't find filesystem on device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05002109 return;
2110 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02002111
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05002112 Current_File_System = type;
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02002113 blkid_free_probe(pr);
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06002114 if (fs_flags.size() > 1) {
2115 std::vector<partition_fs_flags_struct>::iterator iter;
2116 std::vector<partition_fs_flags_struct>::iterator found = fs_flags.begin();
2117
2118 for (iter = fs_flags.begin(); iter != fs_flags.end(); iter++) {
2119 if (iter->File_System == Current_File_System) {
2120 found = iter;
2121 break;
2122 }
2123 }
2124 // If we don't find a match, we default the flags to the first set of flags that we received from the fstab
2125 if (Mount_Flags != found->Mount_Flags || Mount_Options != found->Mount_Options) {
2126 Mount_Flags = found->Mount_Flags;
2127 Mount_Options = found->Mount_Options;
2128 LOGINFO("Mount_Flags: %i, Mount_Options: %s\n", Mount_Flags, Mount_Options.c_str());
2129 }
2130 }
Dees_Troy51a0e822012-09-05 15:24:24 -04002131}
2132
dianlujitao4879b372018-12-03 18:45:47 +08002133bool TWPartition::Wipe_EXTFS(string File_System) {
bigbiffa2bd7b72020-05-07 21:45:34 -04002134 if (!UnMount(true))
2135 return false;
2136
dianlujitao4879b372018-12-03 18:45:47 +08002137#if PLATFORM_SDK_VERSION < 28
2138 if (!TWFunc::Path_Exists("/sbin/mke2fs"))
2139#else
2140 if (!TWFunc::Path_Exists("/sbin/mke2fs") || !TWFunc::Path_Exists("/sbin/e2fsdroid"))
2141#endif
Dees_Troy38bd7602012-09-14 13:33:53 -04002142 return Wipe_RMRF();
2143
dianlujitao4879b372018-12-03 18:45:47 +08002144 int ret;
2145 bool NeedPreserveFooter = true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002146
Ethan Yonker25f20c12014-10-14 09:04:43 -05002147 Find_Actual_Block_Device();
2148 if (!Is_Present) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002149 LOGINFO("Block device not present, cannot wipe %s.\n", Display_Name.c_str());
2150 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Ethan Yonker25f20c12014-10-14 09:04:43 -05002151 return false;
2152 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002153 if (!UnMount(true))
2154 return false;
2155
dianlujitao4879b372018-12-03 18:45:47 +08002156 /**
2157 * On decrypted devices, IOCTL_Get_Block_Size calculates size on device mapper,
2158 * so there's no need to preserve footer.
2159 */
2160 if ((Is_Decrypted && !Decrypted_Block_Device.empty()) ||
2161 Crypto_Key_Location != "footer") {
2162 NeedPreserveFooter = false;
2163 }
2164
2165 unsigned long long dev_sz = TWFunc::IOCTL_Get_Block_Size(Actual_Block_Device.c_str());
2166 if (!dev_sz)
2167 return false;
2168
2169 if (NeedPreserveFooter)
2170 Length < 0 ? dev_sz += Length : dev_sz -= CRYPT_FOOTER_OFFSET;
2171
Dees Troy9a4d7402019-03-21 14:17:28 -04002172 char dout[16];
2173 sprintf(dout, "%llu", dev_sz / 4096);
2174
2175 //string size_str =to_string(dev_sz / 4096);
2176 string size_str = dout;
dianlujitao4879b372018-12-03 18:45:47 +08002177 string Command;
2178
2179 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mke2fs"));
2180
2181 // Execute mke2fs to create empty ext4 filesystem
2182 Command = "mke2fs -t " + File_System + " -b 4096 " + Actual_Block_Device + " " + size_str;
2183 LOGINFO("mke2fs command: %s\n", Command.c_str());
2184 ret = TWFunc::Exec_Cmd(Command);
2185 if (ret) {
2186 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
2187 return false;
2188 }
2189
2190 if (TWFunc::Path_Exists("/sbin/e2fsdroid")) {
dianlujitao3cf05a52019-05-15 15:33:27 +08002191 const string& File_Contexts_Entry = (Mount_Point == "/system_root" ? "/" : Mount_Point);
2192 char *secontext = NULL;
2193 if (!selinux_handle || selabel_lookup(selinux_handle, &secontext, File_Contexts_Entry.c_str(), S_IFDIR) < 0) {
2194 LOGINFO("Cannot lookup security context for '%s'\n", Mount_Point.c_str());
2195 } else {
2196 // Execute e2fsdroid to initialize selinux context
2197 Command = "e2fsdroid -e -S /file_contexts -a " + File_Contexts_Entry + " " + Actual_Block_Device;
2198 LOGINFO("e2fsdroid command: %s\n", Command.c_str());
2199 ret = TWFunc::Exec_Cmd(Command);
2200 if (ret) {
2201 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
2202 return false;
2203 }
dianlujitao4879b372018-12-03 18:45:47 +08002204 }
2205 } else {
2206 LOGINFO("e2fsdroid not present\n");
2207 }
2208
2209 if (NeedPreserveFooter)
2210 Wipe_Crypto_Key();
2211 Current_File_System = File_System;
2212 Recreate_AndSec_Folder();
2213 gui_msg("done=Done.");
2214 return true;
2215}
2216
2217bool TWPartition::Wipe_EXT4() {
2218#ifdef USE_EXT4
Ethan Yonkerf27497f2014-02-09 11:48:33 -06002219 int ret;
dianlujitao4879b372018-12-03 18:45:47 +08002220 bool NeedPreserveFooter = true;
2221
2222 Find_Actual_Block_Device();
2223 if (!Is_Present) {
2224 LOGINFO("Block device not present, cannot wipe %s.\n", Display_Name.c_str());
2225 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
2226 return false;
2227 }
2228 if (!UnMount(true))
2229 return false;
2230
2231 /**
2232 * On decrypted devices, IOCTL_Get_Block_Size calculates size on device mapper,
2233 * so there's no need to preserve footer.
2234 */
2235 if ((Is_Decrypted && !Decrypted_Block_Device.empty()) ||
2236 Crypto_Key_Location != "footer") {
2237 NeedPreserveFooter = false;
2238 }
2239
2240 unsigned long long dev_sz = TWFunc::IOCTL_Get_Block_Size(Actual_Block_Device.c_str());
2241 if (!dev_sz)
2242 return false;
2243
2244 if (NeedPreserveFooter)
2245 Length < 0 ? dev_sz += Length : dev_sz -= CRYPT_FOOTER_OFFSET;
2246
Ethan Yonkerf27497f2014-02-09 11:48:33 -06002247 char *secontext = NULL;
2248
Greg Wallace4b44fef2015-12-29 15:33:24 -05002249 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("make_ext4fs"));
Ethan Yonkerf27497f2014-02-09 11:48:33 -06002250
Dees Troy99c8dbf2014-03-10 16:53:58 +00002251 if (!selinux_handle || selabel_lookup(selinux_handle, &secontext, Mount_Point.c_str(), S_IFDIR) < 0) {
Ethan Yonkerf27497f2014-02-09 11:48:33 -06002252 LOGINFO("Cannot lookup security context for '%s'\n", Mount_Point.c_str());
dianlujitao4879b372018-12-03 18:45:47 +08002253 ret = make_ext4fs(Actual_Block_Device.c_str(), dev_sz, Mount_Point.c_str(), NULL);
Ethan Yonkerf27497f2014-02-09 11:48:33 -06002254 } else {
dianlujitao4879b372018-12-03 18:45:47 +08002255 ret = make_ext4fs(Actual_Block_Device.c_str(), dev_sz, Mount_Point.c_str(), selinux_handle);
Ethan Yonkerf27497f2014-02-09 11:48:33 -06002256 }
2257 if (ret != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002258 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troya95f55c2013-08-17 13:14:43 +00002259 return false;
2260 } else {
dianlujitao4879b372018-12-03 18:45:47 +08002261 if (NeedPreserveFooter)
2262 Wipe_Crypto_Key();
bigbiff bigbiffc49d7062013-10-11 20:28:00 -04002263 string sedir = Mount_Point + "/lost+found";
2264 PartitionManager.Mount_By_Path(sedir.c_str(), true);
2265 rmdir(sedir.c_str());
2266 mkdir(sedir.c_str(), S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP);
Dees_Troya95f55c2013-08-17 13:14:43 +00002267 return true;
2268 }
2269#else
dianlujitao4879b372018-12-03 18:45:47 +08002270 return Wipe_EXTFS("ext4");
Dees_Troya95f55c2013-08-17 13:14:43 +00002271#endif
Dees_Troy51a0e822012-09-05 15:24:24 -04002272}
2273
2274bool TWPartition::Wipe_FAT() {
Vojtech Bocek05534202013-09-11 08:11:56 +02002275 string command;
bigbiffa2bd7b72020-05-07 21:45:34 -04002276 if (!UnMount(true))
2277 return false;
Dees_Troy38bd7602012-09-14 13:33:53 -04002278
Matt Mower18794c82015-11-11 16:22:45 -06002279 if (TWFunc::Path_Exists("/sbin/mkfs.fat")) {
Greg Wallace4b44fef2015-12-29 15:33:24 -05002280 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mkfs.fat"));
Dees_Troy38bd7602012-09-14 13:33:53 -04002281 Find_Actual_Block_Device();
Matt Mower18794c82015-11-11 16:22:45 -06002282 command = "mkfs.fat " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02002283 if (TWFunc::Exec_Cmd(command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08002284 Current_File_System = "vfat";
Dees_Troye58d5262012-09-21 12:27:57 -04002285 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05002286 gui_msg("done=Done.");
Dees_Troy38bd7602012-09-14 13:33:53 -04002287 return true;
2288 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002289 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04002290 return false;
2291 }
2292 return true;
2293 }
2294 else
2295 return Wipe_RMRF();
2296
2297 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04002298}
2299
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002300bool TWPartition::Wipe_EXFAT() {
Vojtech Bocek05534202013-09-11 08:11:56 +02002301 string command;
bigbiffa2bd7b72020-05-07 21:45:34 -04002302 if (!UnMount(true))
2303 return false;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002304
2305 if (TWFunc::Path_Exists("/sbin/mkexfatfs")) {
Greg Wallace4b44fef2015-12-29 15:33:24 -05002306 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mkexfatfs"));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002307 Find_Actual_Block_Device();
2308 command = "mkexfatfs " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02002309 if (TWFunc::Exec_Cmd(command) == 0) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002310 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05002311 gui_msg("done=Done.");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002312 return true;
2313 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002314 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002315 return false;
2316 }
2317 return true;
2318 }
2319 return false;
2320}
2321
Dees_Troy38bd7602012-09-14 13:33:53 -04002322bool TWPartition::Wipe_MTD() {
2323 if (!UnMount(true))
2324 return false;
2325
Greg Wallace4b44fef2015-12-29 15:33:24 -05002326 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("MTD"));
Dees_Troy38bd7602012-09-14 13:33:53 -04002327
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02002328 mtd_scan_partitions();
2329 const MtdPartition* mtd = mtd_find_partition_by_name(MTD_Name.c_str());
2330 if (mtd == NULL) {
2331 LOGERR("No mtd partition named '%s'", MTD_Name.c_str());
2332 return false;
2333 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002334
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02002335 MtdWriteContext* ctx = mtd_write_partition(mtd);
2336 if (ctx == NULL) {
2337 LOGERR("Can't write '%s', failed to format.", MTD_Name.c_str());
2338 return false;
2339 }
2340 if (mtd_erase_blocks(ctx, -1) == -1) {
2341 mtd_write_close(ctx);
2342 LOGERR("Failed to format '%s'", MTD_Name.c_str());
2343 return false;
2344 }
2345 if (mtd_write_close(ctx) != 0) {
2346 LOGERR("Failed to close '%s'", MTD_Name.c_str());
2347 return false;
2348 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08002349 Current_File_System = "yaffs2";
Dees_Troye58d5262012-09-21 12:27:57 -04002350 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05002351 gui_msg("done=Done.");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02002352 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002353}
2354
2355bool TWPartition::Wipe_RMRF() {
Dees_Troy38bd7602012-09-14 13:33:53 -04002356 if (!Mount(true))
2357 return false;
Ethan Yonker726a0202014-12-16 20:01:38 -06002358 // This is the only wipe that leaves the partition mounted, so we
2359 // must manually remove the partition from MTP if it is a storage
2360 // partition.
2361 if (Is_Storage)
2362 PartitionManager.Remove_MTP_Storage(MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04002363
Ethan Yonker74db1572015-10-28 12:44:49 -05002364 gui_msg(Msg("remove_all=Removing all files under '{1}'")(Mount_Point));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002365 TWFunc::removeDir(Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04002366 Recreate_AndSec_Folder();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002367 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002368}
2369
Dees_Troye5017042013-08-29 16:38:55 +00002370bool TWPartition::Wipe_F2FS() {
Vojtech Bocek05534202013-09-11 08:11:56 +02002371 string command;
bigbiffa2bd7b72020-05-07 21:45:34 -04002372 if (!UnMount(true))
2373 return false;
Dees_Troye5017042013-08-29 16:38:55 +00002374
2375 if (TWFunc::Path_Exists("/sbin/mkfs.f2fs")) {
dianlujitao4879b372018-12-03 18:45:47 +08002376 bool NeedPreserveFooter = true;
2377
2378 Find_Actual_Block_Device();
2379 if (!Is_Present) {
2380 LOGINFO("Block device not present, cannot wipe %s.\n", Display_Name.c_str());
2381 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
2382 return false;
2383 }
Dees_Troye5017042013-08-29 16:38:55 +00002384
dianlujitao4879b372018-12-03 18:45:47 +08002385 /**
2386 * On decrypted devices, IOCTL_Get_Block_Size calculates size on device mapper,
2387 * so there's no need to preserve footer.
2388 */
2389 if ((Is_Decrypted && !Decrypted_Block_Device.empty()) ||
2390 Crypto_Key_Location != "footer") {
2391 NeedPreserveFooter = false;
dhacker29a3fa75f2015-01-17 19:42:33 -05002392 }
dianlujitao4879b372018-12-03 18:45:47 +08002393
dianlujitao4879b372018-12-03 18:45:47 +08002394 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mkfs.f2fs"));
Ethan Yonker7e941582019-03-22 08:18:21 -05002395 // First determine if we have the old mkfs.f2fs that uses "-r reserved_bytes"
2396 // or the new mkfs.f2fs that expects the number of sectors as the optional last argument
2397 // Note: some 7.1 trees have the old and some have the new.
2398 command = "mkfs.f2fs | grep \"reserved\" > /tmp/f2fsversiontest";
2399 TWFunc::Exec_Cmd(command, false); // no help argument so printing usage exits with an error code
2400 if (!TWFunc::Path_Exists("/tmp/f2fsversiontest")) {
2401 LOGINFO("Error determining mkfs.f2fs version\n");
2402 return false;
2403 }
2404 if (TWFunc::Get_File_Size("/tmp/f2fsversiontest") <= 0) {
2405 LOGINFO("Using newer mkfs.f2fs\n");
2406 unsigned long long dev_sz = TWFunc::IOCTL_Get_Block_Size(Actual_Block_Device.c_str());
2407 if (!dev_sz)
2408 return false;
2409
2410 if (NeedPreserveFooter)
2411 Length < 0 ? dev_sz += Length : dev_sz -= CRYPT_FOOTER_OFFSET;
2412
2413 char dev_sz_str[48];
2414 sprintf(dev_sz_str, "%llu", (dev_sz / 4096));
2415 command = "mkfs.f2fs -d1 -f -O encrypt -O quota -O verity -w 4096 " + Actual_Block_Device + " " + dev_sz_str;
2416 if (TWFunc::Path_Exists("/sbin/sload.f2fs")) {
2417 command += " && sload.f2fs -t /data " + Actual_Block_Device;
2418 }
2419 } else {
2420 LOGINFO("Using older mkfs.f2fs\n");
2421 command = "mkfs.f2fs -t 0";
2422 if (NeedPreserveFooter) {
2423 // Only use length if we're not decrypted
2424 char len[32];
2425 int mod_length = Length;
2426 if (Length < 0)
2427 mod_length *= -1;
2428 sprintf(len, "%i", mod_length);
2429 command += " -r ";
2430 command += len;
2431 }
2432 command += " " + Actual_Block_Device;
dianlujitao4879b372018-12-03 18:45:47 +08002433 }
2434 LOGINFO("mkfs.f2fs command: %s\n", command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02002435 if (TWFunc::Exec_Cmd(command) == 0) {
dianlujitao4879b372018-12-03 18:45:47 +08002436 if (NeedPreserveFooter)
2437 Wipe_Crypto_Key();
Dees_Troye5017042013-08-29 16:38:55 +00002438 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05002439 gui_msg("done=Done.");
Dees_Troye5017042013-08-29 16:38:55 +00002440 return true;
2441 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002442 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troye5017042013-08-29 16:38:55 +00002443 return false;
2444 }
2445 return true;
2446 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002447 LOGINFO("mkfs.f2fs binary not found, using rm -rf to wipe.\n");
Dees_Troye5017042013-08-29 16:38:55 +00002448 return Wipe_RMRF();
2449 }
2450 return false;
2451}
2452
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002453bool TWPartition::Wipe_NTFS() {
2454 string command;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002455 string Ntfsmake_Binary;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002456
bigbiffa2bd7b72020-05-07 21:45:34 -04002457 if (!UnMount(true))
2458 return false;
2459
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002460 if (TWFunc::Path_Exists("/sbin/mkntfs"))
2461 Ntfsmake_Binary = "mkntfs";
2462 else if (TWFunc::Path_Exists("/sbin/mkfs.ntfs"))
2463 Ntfsmake_Binary = "mkfs.ntfs";
2464 else
2465 return false;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002466
Greg Wallace4b44fef2015-12-29 15:33:24 -05002467 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)(Ntfsmake_Binary));
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002468 Find_Actual_Block_Device();
2469 command = "/sbin/" + Ntfsmake_Binary + " " + Actual_Block_Device;
2470 if (TWFunc::Exec_Cmd(command) == 0) {
2471 Recreate_AndSec_Folder();
2472 gui_msg("done=Done.");
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002473 return true;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002474 } else {
2475 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
2476 return false;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002477 }
2478 return false;
2479}
2480
Dees_Troy51a0e822012-09-05 15:24:24 -04002481bool TWPartition::Wipe_Data_Without_Wiping_Media() {
Ethan Yonker83e82572014-04-04 10:59:28 -05002482#ifdef TW_OEM_BUILD
2483 // In an OEM Build we want to do a full format
2484 return Wipe_Encryption();
2485#else
Ethan Yonker66a19492015-12-10 10:19:45 -06002486 bool ret = false;
Dees_Troy38bd7602012-09-14 13:33:53 -04002487
Dees_Troy38bd7602012-09-14 13:33:53 -04002488 if (!Mount(true))
2489 return false;
Ethan Yonker74db1572015-10-28 12:44:49 -05002490 gui_msg("wiping_data=Wiping data without wiping /data/media ...");
Ethan Yonker66a19492015-12-10 10:19:45 -06002491 ret = Wipe_Data_Without_Wiping_Media_Func(Mount_Point + "/");
2492 if (ret)
2493 gui_msg("done=Done.");
2494 return ret;
2495#endif // ifdef TW_OEM_BUILD
2496}
2497
bigbiff349ea552020-06-19 16:07:38 -04002498bool TWPartition::Recreate_Logs_Dir() {
bigbifff62d2492020-05-20 10:15:34 -04002499#ifdef TW_INCLUDE_FBE
bigbiffa2bd7b72020-05-07 21:45:34 -04002500 struct passwd pd;
2501 struct passwd *pwdptr = &pd;
2502 struct passwd *tempPd;
2503 char pwdBuf[512];
2504 int uid = 0, gid = 0;
2505
2506 if ((getpwnam_r("system", pwdptr, pwdBuf, sizeof(pwdBuf), &tempPd)) != 0) {
2507 LOGERR("unable to get system user id\n");
2508 return false;
2509 } else {
2510 struct group grp;
2511 struct group *grpptr = &grp;
2512 struct group *tempGrp;
2513 char grpBuf[512];
2514
2515 if ((getgrnam_r("cache", grpptr, grpBuf, sizeof(grpBuf), &tempGrp)) != 0) {
2516 LOGERR("unable to get cache group id\n");
2517 return false;
2518 } else {
2519 uid = pd.pw_uid;
2520 gid = grp.gr_gid;
bigbiff349ea552020-06-19 16:07:38 -04002521 std::string abLogsRecoveryDir(DATA_LOGS_DIR);
2522 abLogsRecoveryDir += "/recovery/";
bigbiffa2bd7b72020-05-07 21:45:34 -04002523
bigbiff349ea552020-06-19 16:07:38 -04002524 if (!TWFunc::Create_Dir_Recursive(abLogsRecoveryDir, S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP, uid, gid)) {
2525 LOGERR("Unable to recreate %s\n", abLogsRecoveryDir.c_str());
bigbiffa2bd7b72020-05-07 21:45:34 -04002526 return false;
2527 }
bigbiff349ea552020-06-19 16:07:38 -04002528 if (setfilecon(abLogsRecoveryDir.c_str(), "u:object_r:cache_file:s0") != 0) {
2529 LOGERR("Unable to set contexts for %s\n", abLogsRecoveryDir.c_str());
bigbiffa2bd7b72020-05-07 21:45:34 -04002530 return false;
2531 }
2532 }
2533 }
bigbifff62d2492020-05-20 10:15:34 -04002534#endif
bigbiffa2bd7b72020-05-07 21:45:34 -04002535 return true;
2536}
2537
Ethan Yonker66a19492015-12-10 10:19:45 -06002538bool TWPartition::Wipe_Data_Without_Wiping_Media_Func(const string& parent __unused) {
2539 string dir;
Dees_Troy38bd7602012-09-14 13:33:53 -04002540
2541 DIR* d;
Ethan Yonker66a19492015-12-10 10:19:45 -06002542 d = opendir(parent.c_str());
Dees_Troy16b74352012-11-14 22:27:31 +00002543 if (d != NULL) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002544 struct dirent* de;
2545 while ((de = readdir(d)) != NULL) {
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002546 if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) continue;
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05002547
Ethan Yonker66a19492015-12-10 10:19:45 -06002548 dir = parent;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002549 dir.append(de->d_name);
Ethan Yonker3fdcda42016-11-30 12:29:37 -06002550 if (wipe_exclusions.check_skip_dirs(dir)) {
Ethan Yonker66a19492015-12-10 10:19:45 -06002551 LOGINFO("skipped '%s'\n", dir.c_str());
2552 continue;
2553 }
Dees_Troyce675462013-01-09 19:48:21 +00002554 if (de->d_type == DT_DIR) {
Ethan Yonker66a19492015-12-10 10:19:45 -06002555 dir.append("/");
2556 if (!Wipe_Data_Without_Wiping_Media_Func(dir)) {
2557 closedir(d);
2558 return false;
2559 }
2560 rmdir(dir.c_str());
bigbiff bigbiff98f1f902013-01-19 18:46:13 -05002561 } 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 +03002562 if (unlink(dir.c_str()) != 0)
Ethan Yonker79f88bd2016-12-09 14:52:12 -06002563 LOGINFO("Unable to unlink '%s': %s\n", dir.c_str(), strerror(errno));
Dees_Troyce675462013-01-09 19:48:21 +00002564 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002565 }
2566 closedir(d);
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05002567
Dees_Troy16b74352012-11-14 22:27:31 +00002568 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -04002569 }
Ethan Yonker74db1572015-10-28 12:44:49 -05002570 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy16b74352012-11-14 22:27:31 +00002571 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04002572}
2573
dianlujitao4879b372018-12-03 18:45:47 +08002574void TWPartition::Wipe_Crypto_Key() {
2575 Find_Actual_Block_Device();
2576 if (Crypto_Key_Location.empty())
2577 return;
2578 else if (Crypto_Key_Location == "footer") {
2579 int fd = open(Actual_Block_Device.c_str(), O_RDWR);
2580 if (fd < 0) {
2581 gui_print_color("warning", "Unable to open '%s' to wipe crypto key\n", Actual_Block_Device.c_str());
2582 return;
2583 }
2584
2585 unsigned int block_count;
2586 if ((ioctl(fd, BLKGETSIZE, &block_count)) == -1) {
2587 gui_print_color("warning", "Unable to get block size for wiping crypto footer.\n");
2588 } else {
2589 int newlen = Length < 0 ? -Length : CRYPT_FOOTER_OFFSET;
2590 off64_t offset = ((off64_t)block_count * 512) - newlen;
2591 if (lseek64(fd, offset, SEEK_SET) == -1) {
2592 gui_print_color("warning", "Unable to lseek64 for wiping crypto footer.\n");
2593 } else {
2594 void* buffer = malloc(newlen);
2595 if (!buffer) {
2596 gui_print_color("warning", "Failed to malloc for wiping crypto footer.\n");
2597 } else {
2598 memset(buffer, 0, newlen);
2599 int ret = write(fd, buffer, newlen);
2600 if (ret != newlen) {
2601 gui_print_color("warning", "Failed to wipe crypto footer.\n");
2602 } else {
2603 LOGINFO("Successfully wiped crypto footer.\n");
2604 }
2605 free(buffer);
2606 }
2607 }
2608 }
2609 close(fd);
2610 } else {
2611 if (TWFunc::IOCTL_Get_Block_Size(Crypto_Key_Location.c_str()) >= 16384LLU) {
2612 string Command = "dd of='" + Crypto_Key_Location + "' if=/dev/zero bs=16384 count=1";
2613 TWFunc::Exec_Cmd(Command);
2614 } else {
2615 LOGINFO("Crypto key location reports size < 16K so not wiping crypto footer.\n");
2616 }
2617 }
2618}
2619
bigbiffce8f83c2015-12-12 18:30:21 -05002620bool TWPartition::Backup_Tar(PartitionSettings *part_settings, pid_t *tar_fork_pid) {
Ethan Yonker472f5062016-02-25 13:47:30 -06002621 string Full_FileName;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002622 twrpTar tar;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05002623
Dees_Troy43d8b002012-09-17 16:00:01 -04002624 if (!Mount(true))
2625 return false;
2626
Ian Macdonalda3b25a32019-06-16 22:39:14 +02002627 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Backup_Display_Name, gui_parse_text("{@backing}"));
Ethan Yonker74db1572015-10-28 12:44:49 -05002628 gui_msg(Msg("backing_up=Backing up {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002629
Ethan Yonker472f5062016-02-25 13:47:30 -06002630 DataManager::GetValue(TW_USE_COMPRESSION_VAR, tar.use_compression);
Matt Mowerbb81e5d2014-03-20 18:05:41 -05002631
Dees_Troy83bd4832013-05-04 12:39:56 +00002632#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
Ethan Yonker472f5062016-02-25 13:47:30 -06002633 if (Can_Encrypt_Backup) {
2634 DataManager::GetValue("tw_encrypt_backup", tar.use_encryption);
2635 if (tar.use_encryption) {
2636 if (Use_Userdata_Encryption)
2637 tar.userdata_encryption = tar.use_encryption;
2638 string Password;
2639 DataManager::GetValue("tw_backup_password", Password);
2640 tar.setpassword(Password);
2641 } else {
2642 tar.use_encryption = 0;
2643 }
Dees_Troy83bd4832013-05-04 12:39:56 +00002644 }
2645#endif
Dees_Troy43d8b002012-09-17 16:00:01 -04002646
Ethan Yonker472f5062016-02-25 13:47:30 -06002647 Backup_FileName = Backup_Name + "." + Current_File_System + ".win";
Ethan Yonkere080c1f2016-09-19 13:50:25 -05002648 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
Ethan Yonker01f4e032017-02-03 15:30:52 -06002649 if (Has_Data_Media)
2650 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 -04002651 if (Mount_Point == "/data" && DataManager::GetIntValue(TW_IS_FBE)) {
2652 std::vector<users_struct>::iterator iter;
2653 std::vector<users_struct>* userList = PartitionManager.Get_Users_List();
2654 for (iter = userList->begin(); iter != userList->end(); iter++) {
2655 if (!(*iter).isDecrypted && (*iter).userId != "0") {
2656 gui_msg(Msg(msg::kWarning,
2657 "backup_storage_undecrypt_warning=Backup will not include some files from user {1} "
2658 "because the user is not decrypted.")((*iter).userId));
2659 backup_exclusions.add_absolute_dir("/data/system_ce/" + (*iter).userId);
2660 backup_exclusions.add_absolute_dir("/data/misc_ce/" + (*iter).userId);
2661 backup_exclusions.add_absolute_dir("/data/vendor_ce/" + (*iter).userId);
2662 backup_exclusions.add_absolute_dir("/data/media/" + (*iter).userId);
2663 backup_exclusions.add_absolute_dir("/data/user/" + (*iter).userId);
2664 }
2665 }
2666 }
bigbiffce8f83c2015-12-12 18:30:21 -05002667 tar.part_settings = part_settings;
Ethan Yonker3fdcda42016-11-30 12:29:37 -06002668 tar.backup_exclusions = &backup_exclusions;
Dees Troye0a433a2013-12-02 04:10:37 +00002669 tar.setdir(Backup_Path);
2670 tar.setfn(Full_FileName);
2671 tar.setsize(Backup_Size);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002672 tar.partition_name = Backup_Name;
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002673 tar.backup_folder = part_settings->Backup_Folder;
bigbiffce8f83c2015-12-12 18:30:21 -05002674 if (tar.createTarFork(tar_fork_pid) != 0)
Dees Troye0a433a2013-12-02 04:10:37 +00002675 return false;
Dees_Troy43d8b002012-09-17 16:00:01 -04002676 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002677}
2678
bigbiffce8f83c2015-12-12 18:30:21 -05002679bool TWPartition::Backup_Image(PartitionSettings *part_settings) {
2680 string Full_FileName, adb_file_name;
igoriok87e3d932013-01-31 21:03:53 +02002681
Ethan Yonker74db1572015-10-28 12:44:49 -05002682 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, gui_parse_text("{@backing}"));
2683 gui_msg(Msg("backing_up=Backing up {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002684
Ethan Yonker472f5062016-02-25 13:47:30 -06002685 Backup_FileName = Backup_Name + "." + Current_File_System + ".win";
Dees_Troy43d8b002012-09-17 16:00:01 -04002686
bigbiffce8f83c2015-12-12 18:30:21 -05002687 if (part_settings->adbbackup) {
2688 Full_FileName = TW_ADB_BACKUP;
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002689 adb_file_name = part_settings->Backup_Folder + "/" + Backup_FileName;
bigbiffce8f83c2015-12-12 18:30:21 -05002690 }
2691 else
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002692 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
bigbiffce8f83c2015-12-12 18:30:21 -05002693
2694 part_settings->total_restore_size = Backup_Size;
2695
2696 if (part_settings->adbbackup) {
2697 if (!twadbbu::Write_TWIMG(adb_file_name, Backup_Size))
2698 return false;
2699 }
2700
2701 if (!Raw_Read_Write(part_settings))
Ethan Yonker472f5062016-02-25 13:47:30 -06002702 return false;
2703
bigbiffce8f83c2015-12-12 18:30:21 -05002704 if (part_settings->adbbackup) {
2705 if (!twadbbu::Write_TWEOF())
2706 return false;
Dees_Troy7c2dec82012-09-26 09:49:14 -04002707 }
Dees_Troy43d8b002012-09-17 16:00:01 -04002708 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002709}
2710
bigbiffce8f83c2015-12-12 18:30:21 -05002711bool TWPartition::Raw_Read_Write(PartitionSettings *part_settings) {
2712 unsigned long long RW_Block_Size, Remain = Backup_Size;
2713 int src_fd = -1, dest_fd = -1;
2714 ssize_t bs;
Ethan Yonker472f5062016-02-25 13:47:30 -06002715 bool ret = false;
2716 void* buffer = NULL;
2717 unsigned long long backedup_size = 0;
bigbiffce8f83c2015-12-12 18:30:21 -05002718 string srcfn, destfn;
Ethan Yonker472f5062016-02-25 13:47:30 -06002719
bigbiffce8f83c2015-12-12 18:30:21 -05002720 if (part_settings->PM_Method == PM_BACKUP) {
2721 srcfn = Actual_Block_Device;
2722 if (part_settings->adbbackup)
2723 destfn = TW_ADB_BACKUP;
bigbiff bigbiff38b83c12017-12-28 19:58:52 -05002724 else {
Ethan Yonkere080c1f2016-09-19 13:50:25 -05002725 destfn = part_settings->Backup_Folder + "/" + Backup_FileName;
bigbiff bigbiff38b83c12017-12-28 19:58:52 -05002726 }
bigbiffce8f83c2015-12-12 18:30:21 -05002727 }
2728 else {
2729 destfn = Actual_Block_Device;
2730 if (part_settings->adbbackup) {
2731 srcfn = TW_ADB_RESTORE;
2732 } else {
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002733 srcfn = part_settings->Backup_Folder + "/" + Backup_FileName;
bigbiffce8f83c2015-12-12 18:30:21 -05002734 Remain = TWFunc::Get_File_Size(srcfn);
2735 }
2736 }
Ethan Yonker472f5062016-02-25 13:47:30 -06002737
bigbiffce8f83c2015-12-12 18:30:21 -05002738 src_fd = open(srcfn.c_str(), O_RDONLY | O_LARGEFILE);
Ethan Yonker472f5062016-02-25 13:47:30 -06002739 if (src_fd < 0) {
bigbiffce8f83c2015-12-12 18:30:21 -05002740 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(srcfn.c_str())(strerror(errno)));
Ethan Yonker472f5062016-02-25 13:47:30 -06002741 return false;
2742 }
bigbiffce8f83c2015-12-12 18:30:21 -05002743
2744 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 -06002745 if (dest_fd < 0) {
bigbiffce8f83c2015-12-12 18:30:21 -05002746 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(destfn.c_str())(strerror(errno)));
Ethan Yonker472f5062016-02-25 13:47:30 -06002747 goto exit;
2748 }
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -04002749
bigbiffce8f83c2015-12-12 18:30:21 -05002750 LOGINFO("Reading '%s', writing '%s'\n", srcfn.c_str(), destfn.c_str());
2751
2752 if (part_settings->adbbackup) {
2753 RW_Block_Size = MAX_ADB_READ;
2754 bs = MAX_ADB_READ;
2755 }
2756 else {
2757 RW_Block_Size = 1048576LLU; // 1MB
2758 bs = (ssize_t)(RW_Block_Size);
2759 }
2760
Ethan Yonker472f5062016-02-25 13:47:30 -06002761 buffer = malloc((size_t)bs);
2762 if (!buffer) {
2763 LOGINFO("Raw_Read_Write failed to malloc\n");
2764 goto exit;
2765 }
bigbiffce8f83c2015-12-12 18:30:21 -05002766
2767 if (part_settings->progress)
2768 part_settings->progress->SetPartitionSize(part_settings->total_restore_size);
2769
Ethan Yonker472f5062016-02-25 13:47:30 -06002770 while (Remain > 0) {
2771 if (Remain < RW_Block_Size)
bigbiffce8f83c2015-12-12 18:30:21 -05002772 bs = (ssize_t)(Remain);
Matt Mowera8a89d12016-12-30 18:10:37 -06002773 if (read(src_fd, buffer, bs) != bs) {
Ethan Yonker472f5062016-02-25 13:47:30 -06002774 LOGINFO("Error reading source fd (%s)\n", strerror(errno));
2775 goto exit;
2776 }
2777 if (write(dest_fd, buffer, bs) != bs) {
2778 LOGINFO("Error writing destination fd (%s)\n", strerror(errno));
2779 goto exit;
2780 }
2781 backedup_size += (unsigned long long)(bs);
Matt Mower029a82d2017-01-08 13:12:38 -06002782 Remain -= (unsigned long long)(bs);
bigbiffce8f83c2015-12-12 18:30:21 -05002783 if (part_settings->progress)
2784 part_settings->progress->UpdateSize(backedup_size);
Ethan Yonker472f5062016-02-25 13:47:30 -06002785 if (PartitionManager.Check_Backup_Cancel() != 0)
2786 goto exit;
2787 }
bigbiffce8f83c2015-12-12 18:30:21 -05002788 if (part_settings->progress)
2789 part_settings->progress->UpdateDisplayDetails(true);
Ethan Yonker472f5062016-02-25 13:47:30 -06002790 fsync(dest_fd);
nailyka083dc62016-11-11 21:41:28 +01002791
2792 if (!part_settings->adbbackup && part_settings->PM_Method == PM_BACKUP) {
2793 tw_set_default_metadata(destfn.c_str());
2794 LOGINFO("Restored default metadata for %s\n", destfn.c_str());
2795 }
2796
Ethan Yonker472f5062016-02-25 13:47:30 -06002797 ret = true;
2798exit:
2799 if (src_fd >= 0)
2800 close(src_fd);
2801 if (dest_fd >= 0)
2802 close(dest_fd);
2803 if (buffer)
2804 free(buffer);
2805 return ret;
2806}
2807
bigbiffce8f83c2015-12-12 18:30:21 -05002808bool TWPartition::Backup_Dump_Image(PartitionSettings *part_settings) {
Vojtech Bocek05534202013-09-11 08:11:56 +02002809 string Full_FileName, Command;
Dees_Troy43d8b002012-09-17 16:00:01 -04002810
Ethan Yonker74db1572015-10-28 12:44:49 -05002811 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, gui_parse_text("{@backing}"));
2812 gui_msg(Msg("backing_up=Backing up {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002813
bigbiffce8f83c2015-12-12 18:30:21 -05002814 if (part_settings->progress)
2815 part_settings->progress->SetPartitionSize(Backup_Size);
Dees_Troy43d8b002012-09-17 16:00:01 -04002816
Ethan Yonker472f5062016-02-25 13:47:30 -06002817 Backup_FileName = Backup_Name + "." + Current_File_System + ".win";
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002818 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04002819
2820 Command = "dump_image " + MTD_Name + " '" + Full_FileName + "'";
bigbiffce8f83c2015-12-12 18:30:21 -05002821
Dees_Troy2673cec2013-04-02 20:22:16 +00002822 LOGINFO("Backup command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02002823 TWFunc::Exec_Cmd(Command);
Ethan Yonker4b94cfd2014-12-11 10:00:45 -06002824 tw_set_default_metadata(Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04002825 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
2826 // 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 -06002827 gui_msg(Msg(msg::kError, "backup_size=Backup file size for '{1}' is 0 bytes.")(Full_FileName));
Dees_Troy7c2dec82012-09-26 09:49:14 -04002828 return false;
2829 }
bigbiffce8f83c2015-12-12 18:30:21 -05002830 if (part_settings->progress)
2831 part_settings->progress->UpdateSize(Backup_Size);
2832
Dees_Troy43d8b002012-09-17 16:00:01 -04002833 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002834}
2835
bigbiffce8f83c2015-12-12 18:30:21 -05002836unsigned long long TWPartition::Get_Restore_Size(PartitionSettings *part_settings) {
2837 if (!part_settings->adbbackup) {
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002838 InfoManager restore_info(part_settings->Backup_Folder + "/" + Backup_Name + ".info");
bigbiffce8f83c2015-12-12 18:30:21 -05002839 if (restore_info.LoadValues() == 0) {
2840 if (restore_info.GetValue("backup_size", Restore_Size) == 0) {
2841 LOGINFO("Read info file, restore size is %llu\n", Restore_Size);
2842 return Restore_Size;
2843 }
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002844 }
2845 }
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002846
Matt Mower029a82d2017-01-08 13:12:38 -06002847 string Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
2848 string Restore_File_System = Get_Restore_File_System(part_settings);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002849
2850 if (Is_Image(Restore_File_System)) {
2851 Restore_Size = TWFunc::Get_File_Size(Full_FileName);
2852 return Restore_Size;
2853 }
2854
2855 twrpTar tar;
2856 tar.setdir(Backup_Path);
2857 tar.setfn(Full_FileName);
bigbiffce8f83c2015-12-12 18:30:21 -05002858 tar.backup_name = Full_FileName;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002859#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
2860 string Password;
2861 DataManager::GetValue("tw_restore_password", Password);
2862 if (!Password.empty())
2863 tar.setpassword(Password);
2864#endif
2865 tar.partition_name = Backup_Name;
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002866 tar.backup_folder = part_settings->Backup_Folder;
bigbiffce8f83c2015-12-12 18:30:21 -05002867 tar.part_settings = part_settings;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002868 Restore_Size = tar.get_size();
2869 return Restore_Size;
2870}
2871
bigbiffce8f83c2015-12-12 18:30:21 -05002872bool TWPartition::Restore_Tar(PartitionSettings *part_settings) {
Ethan Yonker472f5062016-02-25 13:47:30 -06002873 string Full_FileName;
Dees Troy4159aed2014-02-28 17:24:43 +00002874 bool ret = false;
bigbiffce8f83c2015-12-12 18:30:21 -05002875 string Restore_File_System = Get_Restore_File_System(part_settings);
Dees_Troy43d8b002012-09-17 16:00:01 -04002876
Dees_Troye58d5262012-09-21 12:27:57 -04002877 if (Has_Android_Secure) {
Dees_Troye58d5262012-09-21 12:27:57 -04002878 if (!Wipe_AndSec())
2879 return false;
Gary Peck43acadf2012-11-21 21:19:01 -08002880 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002881 gui_msg(Msg("wiping=Wiping {1}")(Backup_Display_Name));
Ethan Yonker5eac2222014-06-11 12:22:55 -05002882 if (Has_Data_Media && Mount_Point == "/data" && Restore_File_System != Current_File_System) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002883 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 -05002884 if (!Wipe_Data_Without_Wiping_Media())
2885 return false;
2886 } else {
2887 if (!Wipe(Restore_File_System))
2888 return false;
2889 }
Dees_Troye58d5262012-09-21 12:27:57 -04002890 }
Matt Mower3c366972015-12-25 19:28:31 -06002891 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Backup_Display_Name, gui_parse_text("{@restoring_hdr}"));
Ethan Yonker74db1572015-10-28 12:44:49 -05002892 gui_msg(Msg("restoring=Restoring {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002893
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05002894 // Remount as read/write as needed so we can restore the backup
2895 if (!ReMount_RW(true))
Dees_Troy43d8b002012-09-17 16:00:01 -04002896 return false;
2897
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002898 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
Ethan Yonker87af5632014-02-10 11:56:35 -06002899 twrpTar tar;
bigbiffce8f83c2015-12-12 18:30:21 -05002900 tar.part_settings = part_settings;
Ethan Yonker87af5632014-02-10 11:56:35 -06002901 tar.setdir(Backup_Path);
2902 tar.setfn(Full_FileName);
2903 tar.backup_name = Backup_Name;
2904#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
2905 string Password;
2906 DataManager::GetValue("tw_restore_password", Password);
2907 if (!Password.empty())
2908 tar.setpassword(Password);
2909#endif
bigbiffce8f83c2015-12-12 18:30:21 -05002910 part_settings->progress->SetPartitionSize(Get_Restore_Size(part_settings));
2911 if (tar.extractTarFork() != 0)
Dees Troy4159aed2014-02-28 17:24:43 +00002912 ret = false;
2913 else
2914 ret = true;
2915#ifdef HAVE_CAPABILITIES
2916 // Restore capabilities to the run-as binary
Captain Throwback9d6feb52018-07-27 10:05:24 -04002917 if (Mount_Point == PartitionManager.Get_Android_Root_Path() && Mount(true) && TWFunc::Path_Exists("/system/bin/run-as")) {
Dees Troy4159aed2014-02-28 17:24:43 +00002918 struct vfs_cap_data cap_data;
2919 uint64_t capabilities = (1 << CAP_SETUID) | (1 << CAP_SETGID);
2920
2921 memset(&cap_data, 0, sizeof(cap_data));
2922 cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE;
2923 cap_data.data[0].permitted = (uint32_t) (capabilities & 0xffffffff);
2924 cap_data.data[0].inheritable = 0;
2925 cap_data.data[1].permitted = (uint32_t) (capabilities >> 32);
2926 cap_data.data[1].inheritable = 0;
2927 if (setxattr("/system/bin/run-as", XATTR_NAME_CAPS, &cap_data, sizeof(cap_data), 0) < 0) {
2928 LOGINFO("Failed to reset capabilities of /system/bin/run-as binary.\n");
2929 } else {
2930 LOGINFO("Reset capabilities of /system/bin/run-as binary successful.\n");
2931 }
2932 }
2933#endif
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05002934 if (Mount_Read_Only || Mount_Flags & MS_RDONLY)
2935 // Remount as read only when restoration is complete
2936 ReMount(true);
2937
Dees Troy4159aed2014-02-28 17:24:43 +00002938 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04002939}
2940
bigbiffce8f83c2015-12-12 18:30:21 -05002941bool TWPartition::Restore_Image(PartitionSettings *part_settings) {
Ethan Yonker96af84a2015-01-05 14:58:36 -06002942 string Full_FileName;
bigbiffce8f83c2015-12-12 18:30:21 -05002943 string Restore_File_System = Get_Restore_File_System(part_settings);
Dees_Troy43d8b002012-09-17 16:00:01 -04002944
Matt Mower3c366972015-12-25 19:28:31 -06002945 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Backup_Display_Name, gui_parse_text("{@restoring_hdr}"));
Ethan Yonker74db1572015-10-28 12:44:49 -05002946 gui_msg(Msg("restoring=Restoring {1}...")(Backup_Display_Name));
bigbiffce8f83c2015-12-12 18:30:21 -05002947
2948 if (part_settings->adbbackup)
2949 Full_FileName = TW_ADB_RESTORE;
2950 else
Ethan Yonkere080c1f2016-09-19 13:50:25 -05002951 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
Gary Peck15e623d2012-11-21 21:07:58 -08002952
Ethan Yonker96af84a2015-01-05 14:58:36 -06002953 if (Restore_File_System == "emmc") {
bigbiffce8f83c2015-12-12 18:30:21 -05002954 if (!part_settings->adbbackup)
2955 part_settings->total_restore_size = (uint64_t)(TWFunc::Get_File_Size(Full_FileName));
2956 if (!Raw_Read_Write(part_settings))
Ethan Yonker96af84a2015-01-05 14:58:36 -06002957 return false;
2958 } else if (Restore_File_System == "mtd" || Restore_File_System == "bml") {
bigbiffce8f83c2015-12-12 18:30:21 -05002959 if (!Flash_Image_FI(Full_FileName, part_settings->progress))
2960 return false;
2961 }
2962
2963 if (part_settings->adbbackup) {
2964 if (!twadbbu::Write_TWEOF())
Ethan Yonker96af84a2015-01-05 14:58:36 -06002965 return false;
Gary Peck15e623d2012-11-21 21:07:58 -08002966 }
Dees_Troy43d8b002012-09-17 16:00:01 -04002967 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002968}
Dees_Troy5bf43922012-09-07 16:07:55 -04002969
2970bool TWPartition::Update_Size(bool Display_Error) {
Dees_Troy0550cfb2012-10-13 11:56:13 -04002971 bool ret = false, Was_Already_Mounted = false;
Dees_Troy51127312012-09-08 13:08:49 -04002972
Ethan Yonker1b190162016-12-05 15:25:19 -06002973 Find_Actual_Block_Device();
2974
2975 if (!Can_Be_Mounted && !Is_Encrypted) {
2976 if (TWFunc::Path_Exists(Actual_Block_Device) && Find_Partition_Size()) {
2977 Used = Size;
2978 Backup_Size = Size;
2979 return true;
2980 }
Dees_Troy5bf43922012-09-07 16:07:55 -04002981 return false;
Ethan Yonker1b190162016-12-05 15:25:19 -06002982 }
Dees_Troy5bf43922012-09-07 16:07:55 -04002983
Dees_Troy0550cfb2012-10-13 11:56:13 -04002984 Was_Already_Mounted = Is_Mounted();
Dees_Troy38bd7602012-09-14 13:33:53 -04002985 if (Removable || Is_Encrypted) {
2986 if (!Mount(false))
2987 return true;
2988 } else if (!Mount(Display_Error))
Dees_Troy5bf43922012-09-07 16:07:55 -04002989 return false;
Dees_Troy51127312012-09-08 13:08:49 -04002990
2991 ret = Get_Size_Via_statfs(Display_Error);
Dees_Troy0550cfb2012-10-13 11:56:13 -04002992 if (!ret || Size == 0) {
2993 if (!Get_Size_Via_df(Display_Error)) {
2994 if (!Was_Already_Mounted)
2995 UnMount(false);
Dees_Troy51127312012-09-08 13:08:49 -04002996 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04002997 }
2998 }
Dees_Troy51127312012-09-08 13:08:49 -04002999
Dees_Troy5bf43922012-09-07 16:07:55 -04003000 if (Has_Data_Media) {
3001 if (Mount(Display_Error)) {
Ethan Yonker3fdcda42016-11-30 12:29:37 -06003002 Used = backup_exclusions.Get_Folder_Size(Mount_Point);
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05003003 Backup_Size = Used;
3004 int bak = (int)(Used / 1048576LLU);
Dees_Troy51127312012-09-08 13:08:49 -04003005 int fre = (int)(Free / 1048576LLU);
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05003006 LOGINFO("Data backup size is %iMB, free: %iMB.\n", bak, fre);
Dees_Troy0550cfb2012-10-13 11:56:13 -04003007 } else {
3008 if (!Was_Already_Mounted)
3009 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04003010 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04003011 }
Dees_Troye58d5262012-09-21 12:27:57 -04003012 } else if (Has_Android_Secure) {
3013 if (Mount(Display_Error))
Ethan Yonker3fdcda42016-11-30 12:29:37 -06003014 Backup_Size = backup_exclusions.Get_Folder_Size(Backup_Path);
Dees_Troy0550cfb2012-10-13 11:56:13 -04003015 else {
3016 if (!Was_Already_Mounted)
3017 UnMount(false);
Dees_Troye58d5262012-09-21 12:27:57 -04003018 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04003019 }
Dees_Troy5bf43922012-09-07 16:07:55 -04003020 }
Dees_Troy0550cfb2012-10-13 11:56:13 -04003021 if (!Was_Already_Mounted)
3022 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04003023 return true;
Dees_Troy51127312012-09-08 13:08:49 -04003024}
Dees_Troy38bd7602012-09-14 13:33:53 -04003025
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06003026bool TWPartition::Find_Wildcard_Block_Devices(const string& Device) {
3027 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
3028 string Path = TWFunc::Get_Path(Device);
3029 string Dev = TWFunc::Get_Filename(Device);
3030 size_t wildcard_index = Dev.find("*");
3031 if (wildcard_index != string::npos)
3032 Dev = Dev.substr(0, wildcard_index);
3033 wildcard_index = Dev.size();
3034 DIR* d = opendir(Path.c_str());
3035 if (d == NULL) {
3036 LOGINFO("Error opening '%s': %s\n", Path.c_str(), strerror(errno));
3037 return false;
3038 }
3039 struct dirent* de;
3040 while ((de = readdir(d)) != NULL) {
3041 if (de->d_type != DT_BLK || strlen(de->d_name) <= wildcard_index || strncmp(de->d_name, Dev.c_str(), wildcard_index) != 0)
3042 continue;
3043
3044 string item = Path + "/";
3045 item.append(de->d_name);
3046 if (PartitionManager.Find_Partition_By_Block_Device(item))
3047 continue;
3048 TWPartition *part = new TWPartition;
3049 char buffer[MAX_FSTAB_LINE_LENGTH];
3050 sprintf(buffer, "%s %s-%i auto defaults defaults", item.c_str(), Mount_Point.c_str(), ++mount_point_index);
Chaosmasterf6e42ce2020-01-27 00:17:04 +01003051 part->Process_Fstab_Line(buffer, false, NULL, false);
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06003052 char display[MAX_FSTAB_LINE_LENGTH];
3053 sprintf(display, "%s %i", Storage_Name.c_str(), mount_point_index);
3054 part->Storage_Name = display;
3055 part->Display_Name = display;
3056 part->Primary_Block_Device = item;
3057 part->Wildcard_Block_Device = false;
3058 part->Is_SubPartition = true;
3059 part->SubPartition_Of = Mount_Point;
3060 part->Is_Storage = Is_Storage;
3061 part->Can_Be_Mounted = true;
3062 part->Removable = true;
3063 part->Can_Be_Wiped = Can_Be_Wiped;
3064 part->Wipe_Available_in_GUI = Wipe_Available_in_GUI;
3065 part->Find_Actual_Block_Device();
3066 part->Update_Size(false);
3067 Has_SubPartition = true;
3068 PartitionManager.Output_Partition(part);
3069 PartitionManager.Add_Partition(part);
3070 }
3071 closedir(d);
3072 return (mount_point_index > 0);
3073}
3074
Dees_Troy38bd7602012-09-14 13:33:53 -04003075void TWPartition::Find_Actual_Block_Device(void) {
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06003076 if (!Sysfs_Entry.empty() && Primary_Block_Device.empty() && Decrypted_Block_Device.empty()) {
3077 /* Sysfs_Entry.empty() indicates if this is a sysfs entry that begins with /device/
3078 * If we have a syfs entry then we are looking for this device from a uevent add.
3079 * The uevent add will set the primary block device based on the data we receive from
3080 * after checking for adopted storage. If the device ends up being adopted, then the
3081 * decrypted block device will be set instead of the primary block device. */
3082 Is_Present = false;
3083 return;
3084 }
3085 if (Wildcard_Block_Device && !Is_Adopted_Storage) {
3086 Is_Present = false;
3087 Actual_Block_Device = "";
3088 Can_Be_Mounted = false;
3089 if (!Find_Wildcard_Block_Devices(Primary_Block_Device)) {
3090 string Dev = Primary_Block_Device.substr(0, Primary_Block_Device.find("*"));
3091 if (TWFunc::Path_Exists(Dev)) {
3092 Is_Present = true;
3093 Can_Be_Mounted = true;
3094 Actual_Block_Device = Dev;
3095 }
3096 }
3097 return;
3098 } else if (Is_Decrypted && !Decrypted_Block_Device.empty()) {
Dees_Troy38bd7602012-09-14 13:33:53 -04003099 Actual_Block_Device = Decrypted_Block_Device;
Ethan Yonker1b190162016-12-05 15:25:19 -06003100 if (TWFunc::Path_Exists(Decrypted_Block_Device)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04003101 Is_Present = true;
Ethan Yonker1b190162016-12-05 15:25:19 -06003102 return;
3103 }
3104 } else if (SlotSelect && TWFunc::Path_Exists(Primary_Block_Device + PartitionManager.Get_Active_Slot_Suffix())) {
3105 Actual_Block_Device = Primary_Block_Device + PartitionManager.Get_Active_Slot_Suffix();
3106 unlink(Primary_Block_Device.c_str());
3107 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
3108 Is_Present = true;
3109 return;
Dees_Troy43d8b002012-09-17 16:00:01 -04003110 } else if (TWFunc::Path_Exists(Primary_Block_Device)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04003111 Is_Present = true;
3112 Actual_Block_Device = Primary_Block_Device;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05003113 return;
3114 }
Ethan Yonker1b190162016-12-05 15:25:19 -06003115 if (!Alternate_Block_Device.empty() && TWFunc::Path_Exists(Alternate_Block_Device)) {
Dees_Troy3f04d032012-10-07 18:20:09 -04003116 Actual_Block_Device = Alternate_Block_Device;
Dees_Troy38bd7602012-09-14 13:33:53 -04003117 Is_Present = true;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05003118 } else {
Dees_Troy38bd7602012-09-14 13:33:53 -04003119 Is_Present = false;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05003120 }
Dees_Troy38bd7602012-09-14 13:33:53 -04003121}
3122
3123void TWPartition::Recreate_Media_Folder(void) {
3124 string Command;
Ethan Yonker66a19492015-12-10 10:19:45 -06003125 string Media_Path = Mount_Point + "/media";
Dees_Troy38bd7602012-09-14 13:33:53 -04003126
Ethan Yonkerbd7492d2016-12-07 13:55:01 -06003127 if (Is_FBE) {
3128 LOGINFO("Not recreating media folder on FBE\n");
3129 return;
3130 }
Dees_Troy38bd7602012-09-14 13:33:53 -04003131 if (!Mount(true)) {
Ethan Yonker66a19492015-12-10 10:19:45 -06003132 gui_msg(Msg(msg::kError, "recreate_folder_err=Unable to recreate {1} folder.")(Media_Path));
3133 } else if (!TWFunc::Path_Exists(Media_Path)) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05003134 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Ethan Yonker66a19492015-12-10 10:19:45 -06003135 LOGINFO("Recreating %s folder.\n", Media_Path.c_str());
3136 mkdir(Media_Path.c_str(), 0770);
xiaolu9416f4f2015-06-04 08:22:23 +08003137 string Internal_path = DataManager::GetStrValue("tw_internal_path");
3138 if (!Internal_path.empty()) {
3139 LOGINFO("Recreating %s folder.\n", Internal_path.c_str());
3140 mkdir(Internal_path.c_str(), 0770);
3141 }
3142#ifdef TW_INTERNAL_STORAGE_PATH
3143 mkdir(EXPAND(TW_INTERNAL_STORAGE_PATH), 0770);
3144#endif
Matt Mower87413642017-01-17 21:14:46 -06003145
thata3d31fb2014-12-21 22:27:40 +01003146 // Afterwards, we will try to set the
3147 // default metadata that we were hopefully able to get during
3148 // early boot.
Ethan Yonker66a19492015-12-10 10:19:45 -06003149 tw_set_default_metadata(Media_Path.c_str());
xiaolu9416f4f2015-06-04 08:22:23 +08003150 if (!Internal_path.empty())
3151 tw_set_default_metadata(Internal_path.c_str());
Matt Mower87413642017-01-17 21:14:46 -06003152
Ethan Yonker5eac2222014-06-11 12:22:55 -05003153 // Toggle mount to ensure that "internal sdcard" gets mounted
bigbiff bigbiff9c754052013-01-09 09:09:08 -05003154 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Ethan Yonker5eac2222014-06-11 12:22:55 -05003155 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Dees_Troy38bd7602012-09-14 13:33:53 -04003156 }
Dees_Troy43d8b002012-09-17 16:00:01 -04003157}
Dees_Troye58d5262012-09-21 12:27:57 -04003158
3159void TWPartition::Recreate_AndSec_Folder(void) {
Dees_Troye58d5262012-09-21 12:27:57 -04003160 if (!Has_Android_Secure)
3161 return;
Dees_Troy2673cec2013-04-02 20:22:16 +00003162 LOGINFO("Creating %s: %s\n", Backup_Display_Name.c_str(), Symlink_Path.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -04003163 if (!Mount(true)) {
Ethan Yonker74db1572015-10-28 12:44:49 -05003164 gui_msg(Msg(msg::kError, "recreate_folder_err=Unable to recreate {1} folder.")(Backup_Name));
Dees_Troye58d5262012-09-21 12:27:57 -04003165 } else if (!TWFunc::Path_Exists(Symlink_Path)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00003166 LOGINFO("Recreating %s folder.\n", Backup_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05003167 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05003168 mkdir(Symlink_Path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
bigbiff bigbiff9c754052013-01-09 09:09:08 -05003169 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04003170 }
3171}
bigbiff7cb4c332014-11-26 20:36:07 -05003172
3173uint64_t TWPartition::Get_Max_FileSize() {
3174 uint64_t maxFileSize = 0;
3175 const uint64_t constGB = (uint64_t) 1024 * 1024 * 1024;
3176 const uint64_t constTB = (uint64_t) constGB * 1024;
3177 const uint64_t constPB = (uint64_t) constTB * 1024;
bigbiff0c532032014-12-21 13:41:26 -05003178 if (Current_File_System == "ext4")
3179 maxFileSize = 16 * constTB; //16 TB
3180 else if (Current_File_System == "vfat")
3181 maxFileSize = 4 * constGB; //4 GB
3182 else if (Current_File_System == "ntfs")
3183 maxFileSize = 256 * constTB; //256 TB
3184 else if (Current_File_System == "exfat")
3185 maxFileSize = 16 * constPB; //16 PB
3186 else if (Current_File_System == "ext3")
3187 maxFileSize = 2 * constTB; //2 TB
3188 else if (Current_File_System == "f2fs")
3189 maxFileSize = 3.94 * constTB; //3.94 TB
bigbiff7cb4c332014-11-26 20:36:07 -05003190 else
3191 maxFileSize = 100000000L;
3192 return maxFileSize - 1;
3193}
3194
bigbiffce8f83c2015-12-12 18:30:21 -05003195bool TWPartition::Flash_Image(PartitionSettings *part_settings) {
3196 string Restore_File_System, full_filename;
Ethan Yonker96af84a2015-01-05 14:58:36 -06003197
Ethan Yonkere080c1f2016-09-19 13:50:25 -05003198 full_filename = part_settings->Backup_Folder + "/" + Backup_FileName;
Ethan Yonker96af84a2015-01-05 14:58:36 -06003199
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05003200 LOGINFO("Image filename is: %s\n", Backup_FileName.c_str());
bigbiffce8f83c2015-12-12 18:30:21 -05003201
3202 if (Backup_Method == BM_FILES) {
Ethan Yonker96af84a2015-01-05 14:58:36 -06003203 LOGERR("Cannot flash images to file systems\n");
3204 return false;
3205 } else if (!Can_Flash_Img) {
3206 LOGERR("Cannot flash images to partitions %s\n", Display_Name.c_str());
3207 return false;
3208 } else {
3209 if (!Find_Partition_Size()) {
3210 LOGERR("Unable to find partition size for '%s'\n", Mount_Point.c_str());
3211 return false;
3212 }
bigbiffce8f83c2015-12-12 18:30:21 -05003213 unsigned long long image_size = TWFunc::Get_File_Size(full_filename);
Ethan Yonker96af84a2015-01-05 14:58:36 -06003214 if (image_size > Size) {
Ethan Yonker74db1572015-10-28 12:44:49 -05003215 LOGINFO("Size (%llu bytes) of image '%s' is larger than target device '%s' (%llu bytes)\n",
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05003216 image_size, Backup_FileName.c_str(), Actual_Block_Device.c_str(), Size);
Ethan Yonker74db1572015-10-28 12:44:49 -05003217 gui_err("img_size_err=Size of image is larger than target device");
Ethan Yonker96af84a2015-01-05 14:58:36 -06003218 return false;
3219 }
bigbiffce8f83c2015-12-12 18:30:21 -05003220 if (Backup_Method == BM_DD) {
3221 if (!part_settings->adbbackup) {
3222 if (Is_Sparse_Image(full_filename)) {
3223 return Flash_Sparse_Image(full_filename);
3224 }
Ethan Yonker472f5062016-02-25 13:47:30 -06003225 }
bigbiffce8f83c2015-12-12 18:30:21 -05003226 return Raw_Read_Write(part_settings);
3227 } else if (Backup_Method == BM_FLASH_UTILS) {
3228 return Flash_Image_FI(full_filename, NULL);
Ethan Yonker472f5062016-02-25 13:47:30 -06003229 }
Ethan Yonker96af84a2015-01-05 14:58:36 -06003230 }
3231
3232 LOGERR("Unknown flash method for '%s'\n", Mount_Point.c_str());
3233 return false;
3234}
3235
Ethan Yonker472f5062016-02-25 13:47:30 -06003236bool TWPartition::Is_Sparse_Image(const string& Filename) {
HashBanged974bb2016-01-30 14:20:09 -05003237 uint32_t magic = 0;
3238 int fd = open(Filename.c_str(), O_RDONLY);
3239 if (fd < 0) {
3240 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Filename)(strerror(errno)));
3241 return false;
3242 }
bigbiffce8f83c2015-12-12 18:30:21 -05003243
HashBanged974bb2016-01-30 14:20:09 -05003244 if (read(fd, &magic, sizeof(magic)) != sizeof(magic)) {
3245 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Filename)(strerror(errno)));
3246 close(fd);
3247 return false;
3248 }
3249 close(fd);
Ethan Yonker472f5062016-02-25 13:47:30 -06003250 if (magic == SPARSE_HEADER_MAGIC)
3251 return true;
3252 return false;
3253}
3254
3255bool TWPartition::Flash_Sparse_Image(const string& Filename) {
3256 string Command;
3257
3258 gui_msg(Msg("flashing=Flashing {1}...")(Display_Name));
3259
3260 Command = "simg2img '" + Filename + "' '" + Actual_Block_Device + "'";
Ethan Yonker96af84a2015-01-05 14:58:36 -06003261 LOGINFO("Flash command: '%s'\n", Command.c_str());
3262 TWFunc::Exec_Cmd(Command);
3263 return true;
3264}
3265
Ethan Yonker472f5062016-02-25 13:47:30 -06003266bool TWPartition::Flash_Image_FI(const string& Filename, ProgressTracking *progress) {
Ethan Yonker96af84a2015-01-05 14:58:36 -06003267 string Command;
Ethan Yonker472f5062016-02-25 13:47:30 -06003268 unsigned long long file_size;
Ethan Yonker96af84a2015-01-05 14:58:36 -06003269
Ethan Yonker74db1572015-10-28 12:44:49 -05003270 gui_msg(Msg("flashing=Flashing {1}...")(Display_Name));
Ethan Yonker472f5062016-02-25 13:47:30 -06003271 if (progress) {
3272 file_size = (unsigned long long)(TWFunc::Get_File_Size(Filename));
3273 progress->SetPartitionSize(file_size);
3274 }
Ethan Yonker96af84a2015-01-05 14:58:36 -06003275 // Sometimes flash image doesn't like to flash due to the first 2KB matching, so we erase first to ensure that it flashes
3276 Command = "erase_image " + MTD_Name;
3277 LOGINFO("Erase command: '%s'\n", Command.c_str());
3278 TWFunc::Exec_Cmd(Command);
3279 Command = "flash_image " + MTD_Name + " '" + Filename + "'";
3280 LOGINFO("Flash command: '%s'\n", Command.c_str());
3281 TWFunc::Exec_Cmd(Command);
Ethan Yonker472f5062016-02-25 13:47:30 -06003282 if (progress)
3283 progress->UpdateSize(file_size);
Ethan Yonker96af84a2015-01-05 14:58:36 -06003284 return true;
3285}
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05003286
3287void TWPartition::Change_Mount_Read_Only(bool new_value) {
3288 Mount_Read_Only = new_value;
3289}
3290
bigbiffce8f83c2015-12-12 18:30:21 -05003291bool TWPartition::Is_Read_Only() {
3292 return Mount_Read_Only;
3293}
3294
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05003295int TWPartition::Check_Lifetime_Writes() {
3296 bool original_read_only = Mount_Read_Only;
3297 int ret = 1;
3298
3299 Mount_Read_Only = true;
3300 if (Mount(false)) {
3301 Find_Actual_Block_Device();
Ethan Yonker54970bb2019-01-17 16:19:24 -06003302 string temp = Actual_Block_Device;
3303 Find_Real_Block_Device(temp, false);
3304 string block = basename(temp.c_str());
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05003305 string file = "/sys/fs/" + Current_File_System + "/" + block + "/lifetime_write_kbytes";
3306 string result;
3307 if (TWFunc::Path_Exists(file)) {
3308 if (TWFunc::read_file(file, result) != 0) {
3309 LOGINFO("Check_Lifetime_Writes of '%s' failed to read_file\n", file.c_str());
3310 } else {
3311 LOGINFO("Check_Lifetime_Writes result: '%s'\n", result.c_str());
3312 if (result == "0") {
3313 ret = 0;
3314 }
3315 }
3316 } else {
3317 LOGINFO("Check_Lifetime_Writes file does not exist '%s'\n", file.c_str());
3318 }
3319 UnMount(true);
3320 } else {
3321 LOGINFO("Check_Lifetime_Writes failed to mount '%s'\n", Mount_Point.c_str());
3322 }
3323 Mount_Read_Only = original_read_only;
3324 return ret;
3325}
Ethan Yonker66a19492015-12-10 10:19:45 -06003326
3327int TWPartition::Decrypt_Adopted() {
3328#ifdef TW_INCLUDE_CRYPTO
3329 int ret = 1;
3330 Is_Adopted_Storage = false;
3331 string Adopted_Key_File = "";
3332
3333 if (!Removable)
3334 return ret;
3335
3336 int fd = open(Alternate_Block_Device.c_str(), O_RDONLY);
3337 if (fd < 0) {
3338 LOGINFO("failed to open '%s'\n", Alternate_Block_Device.c_str());
3339 return ret;
3340 }
3341 char type_guid[80];
3342 char part_guid[80];
3343
3344 if (gpt_disk_get_partition_info(fd, 2, type_guid, part_guid) == 0) {
3345 LOGINFO("type: '%s'\n", type_guid);
3346 LOGINFO("part: '%s'\n", part_guid);
3347 Adopted_GUID = part_guid;
3348 LOGINFO("Adopted_GUID '%s'\n", Adopted_GUID.c_str());
3349 if (strcmp(type_guid, TWGptAndroidExpand) == 0) {
3350 LOGINFO("android_expand found\n");
3351 Adopted_Key_File = "/data/misc/vold/expand_";
3352 Adopted_Key_File += part_guid;
3353 Adopted_Key_File += ".key";
3354 if (TWFunc::Path_Exists(Adopted_Key_File)) {
3355 Is_Adopted_Storage = true;
3356 /* Until we find a use case for this, I think it is safe
3357 * to disable USB Mass Storage whenever adopted storage
3358 * is present.
3359 */
3360 LOGINFO("Detected adopted storage, disabling USB mass storage mode\n");
3361 DataManager::SetValue("tw_has_usb_storage", 0);
3362 }
3363 }
3364 }
3365
3366 if (Is_Adopted_Storage) {
3367 string Adopted_Block_Device = Alternate_Block_Device + "p2";
3368 if (!TWFunc::Path_Exists(Adopted_Block_Device)) {
3369 Adopted_Block_Device = Alternate_Block_Device + "2";
3370 if (!TWFunc::Path_Exists(Adopted_Block_Device)) {
3371 LOGINFO("Adopted block device does not exist\n");
3372 goto exit;
3373 }
3374 }
3375 LOGINFO("key file is '%s', block device '%s'\n", Adopted_Key_File.c_str(), Adopted_Block_Device.c_str());
3376 char crypto_blkdev[MAXPATHLEN];
3377 std::string thekey;
3378 int fdkey = open(Adopted_Key_File.c_str(), O_RDONLY);
3379 if (fdkey < 0) {
3380 LOGINFO("failed to open key file\n");
3381 goto exit;
3382 }
3383 char buf[512];
3384 ssize_t n;
3385 while ((n = read(fdkey, &buf[0], sizeof(buf))) > 0) {
3386 thekey.append(buf, n);
3387 }
3388 close(fdkey);
3389 unsigned char* key = (unsigned char*) thekey.data();
3390 cryptfs_revert_ext_volume(part_guid);
3391
3392 ret = cryptfs_setup_ext_volume(part_guid, Adopted_Block_Device.c_str(), key, thekey.size(), crypto_blkdev);
3393 if (ret == 0) {
3394 LOGINFO("adopted storage new block device: '%s'\n", crypto_blkdev);
3395 Decrypted_Block_Device = crypto_blkdev;
3396 Is_Decrypted = true;
3397 Is_Encrypted = true;
3398 Find_Actual_Block_Device();
Kjell Braden3126a112016-06-19 16:58:15 +00003399 if (!Mount_Storage_Retry(false)) {
Ethan Yonker66a19492015-12-10 10:19:45 -06003400 LOGERR("Failed to mount decrypted adopted storage device\n");
3401 Is_Decrypted = false;
3402 Is_Encrypted = false;
3403 cryptfs_revert_ext_volume(part_guid);
3404 ret = 1;
3405 } else {
Ethan Yonkerfcf3f242016-02-16 12:30:26 -06003406 UnMount(false);
3407 Has_Android_Secure = false;
3408 Symlink_Path = "";
3409 Symlink_Mount_Point = "";
3410 Backup_Name = Mount_Point.substr(1);
3411 Backup_Path = Mount_Point;
3412 TWPartition* sdext = PartitionManager.Find_Partition_By_Path("/sd-ext");
3413 if (sdext && sdext->Actual_Block_Device == Adopted_Block_Device) {
3414 LOGINFO("Removing /sd-ext from partition list due to adopted storage\n");
3415 PartitionManager.Remove_Partition_By_Path("/sd-ext");
3416 }
Ethan Yonker66a19492015-12-10 10:19:45 -06003417 Setup_Data_Media();
3418 Recreate_Media_Folder();
3419 Wipe_Available_in_GUI = true;
3420 Wipe_During_Factory_Reset = true;
3421 Can_Be_Backed_Up = true;
3422 Can_Encrypt_Backup = true;
3423 Use_Userdata_Encryption = true;
3424 Is_Storage = true;
3425 Storage_Name = "Adopted Storage";
3426 Is_SubPartition = true;
3427 SubPartition_Of = "/data";
3428 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
3429 DataManager::SetValue("tw_has_adopted_storage", 1);
3430 }
3431 } else {
3432 LOGERR("Failed to setup adopted storage decryption\n");
3433 }
3434 }
3435exit:
Matt Mower06543e32017-01-06 15:25:26 -06003436 close(fd);
Ethan Yonker66a19492015-12-10 10:19:45 -06003437 return ret;
3438#else
3439 LOGINFO("Decrypt_Adopted: no crypto support\n");
3440 return 1;
3441#endif
3442}
3443
3444void TWPartition::Revert_Adopted() {
3445#ifdef TW_INCLUDE_CRYPTO
3446 if (!Adopted_GUID.empty()) {
3447 PartitionManager.Remove_MTP_Storage(Mount_Point);
3448 UnMount(false);
3449 cryptfs_revert_ext_volume(Adopted_GUID.c_str());
3450 Is_Adopted_Storage = false;
3451 Is_Encrypted = false;
3452 Is_Decrypted = false;
3453 Decrypted_Block_Device = "";
3454 Find_Actual_Block_Device();
3455 Wipe_During_Factory_Reset = false;
3456 Can_Be_Backed_Up = false;
3457 Can_Encrypt_Backup = false;
3458 Use_Userdata_Encryption = false;
3459 Is_SubPartition = false;
3460 SubPartition_Of = "";
3461 Has_Data_Media = false;
3462 Storage_Path = Mount_Point;
3463 if (!Symlink_Mount_Point.empty()) {
3464 TWPartition* Dat = PartitionManager.Find_Partition_By_Path("/data");
3465 if (Dat) {
3466 Dat->UnMount(false);
3467 Dat->Symlink_Mount_Point = Symlink_Mount_Point;
3468 }
3469 Symlink_Mount_Point = "";
3470 }
3471 }
3472#else
3473 LOGINFO("Revert_Adopted: no crypto support\n");
3474#endif
3475}
bigbiff bigbiffb5ecaad2017-03-20 18:53:53 -04003476
3477void TWPartition::Set_Backup_FileName(string fname) {
3478 Backup_FileName = fname;
3479}
3480
3481string TWPartition::Get_Backup_Name() {
3482 return Backup_Name;
3483}