blob: b47c2947f73b1a90697bd7ece4e88c7f06522a66 [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,
Matt Mower2416a502016-04-12 19:54:46 -0500165};
166
167/* Flags without a trailing '=' are considered dual format flags and can be
168 * written as either 'flagname' or 'flagname=', where the character following
169 * the '=' is Y,y,1 for true and false otherwise.
170 */
171const struct flag_list tw_flags[] = {
172 { "andsec", TWFLAG_ANDSEC },
173 { "backup", TWFLAG_BACKUP },
174 { "backupname=", TWFLAG_BACKUPNAME },
175 { "blocksize=", TWFLAG_BLOCKSIZE },
176 { "canbewiped", TWFLAG_CANBEWIPED },
177 { "canencryptbackup", TWFLAG_CANENCRYPTBACKUP },
178 { "defaults", TWFLAG_DEFAULTS },
179 { "display=", TWFLAG_DISPLAY },
180 { "encryptable=", TWFLAG_ENCRYPTABLE },
Ethan Yonkere9afc3d2018-08-30 15:16:27 -0500181 { "fileencryption=", TWFLAG_FILEENCRYPTION },
Matt Mower2416a502016-04-12 19:54:46 -0500182 { "flashimg", TWFLAG_FLASHIMG },
183 { "forceencrypt=", TWFLAG_FORCEENCRYPT },
184 { "fsflags=", TWFLAG_FSFLAGS },
185 { "ignoreblkid", TWFLAG_IGNOREBLKID },
186 { "length=", TWFLAG_LENGTH },
187 { "mounttodecrypt", TWFLAG_MOUNTTODECRYPT },
188 { "removable", TWFLAG_REMOVABLE },
189 { "retainlayoutversion", TWFLAG_RETAINLAYOUTVERSION },
190 { "settingsstorage", TWFLAG_SETTINGSSTORAGE },
191 { "storage", TWFLAG_STORAGE },
192 { "storagename=", TWFLAG_STORAGENAME },
193 { "subpartitionof=", TWFLAG_SUBPARTITIONOF },
194 { "symlink=", TWFLAG_SYMLINK },
195 { "userdataencryptbackup", TWFLAG_USERDATAENCRYPTBACKUP },
196 { "usermrf", TWFLAG_USERMRF },
197 { "wipeduringfactoryreset", TWFLAG_WIPEDURINGFACTORYRESET },
198 { "wipeingui", TWFLAG_WIPEINGUI },
Ethan Yonker1b190162016-12-05 15:25:19 -0600199 { "slotselect", TWFLAG_SLOTSELECT },
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600200 { "wait", TWFLAG_WAIT },
201 { "verify", TWFLAG_VERIFY },
202 { "check", TWFLAG_CHECK },
203 { "altdevice", TWFLAG_ALTDEVICE },
204 { "notrim", TWFLAG_NOTRIM },
205 { "voldmanaged=", TWFLAG_VOLDMANAGED },
206 { "formattable", TWFLAG_FORMATTABLE },
207 { "resize", TWFLAG_RESIZE },
Ethan Yonker93382822018-11-01 15:25:31 -0500208 { "keydirectory=", TWFLAG_KEYDIRECTORY },
Peter Cai40ed06e2019-05-24 11:38:54 +0800209 { "wrappedkey", TWFLAG_WRAPPEDKEY },
Matt Mower2416a502016-04-12 19:54:46 -0500210 { 0, 0 },
211};
212
that9e0593e2014-10-08 00:01:24 +0200213TWPartition::TWPartition() {
Dees_Troy51a0e822012-09-05 15:24:24 -0400214 Can_Be_Mounted = false;
215 Can_Be_Wiped = false;
Dees_Troya13d74f2013-03-24 08:54:55 -0500216 Can_Be_Backed_Up = false;
Vojtech Bocek1dc30982013-08-30 21:49:30 +0200217 Use_Rm_Rf = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400218 Wipe_During_Factory_Reset = false;
219 Wipe_Available_in_GUI = false;
220 Is_SubPartition = false;
Dees_Troy2691f9d2012-09-24 11:15:49 -0400221 Has_SubPartition = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400222 SubPartition_Of = "";
223 Symlink_Path = "";
224 Symlink_Mount_Point = "";
225 Mount_Point = "";
Dees_Troye58d5262012-09-21 12:27:57 -0400226 Backup_Path = "";
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600227 Wildcard_Block_Device = false;
228 Sysfs_Entry = "";
Dees_Troy38bd7602012-09-14 13:33:53 -0400229 Actual_Block_Device = "";
230 Primary_Block_Device = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400231 Alternate_Block_Device = "";
232 Removable = false;
233 Is_Present = false;
234 Length = 0;
235 Size = 0;
236 Used = 0;
237 Free = 0;
238 Backup_Size = 0;
239 Can_Be_Encrypted = false;
240 Is_Encrypted = false;
241 Is_Decrypted = false;
Ethan Yonkerbd7492d2016-12-07 13:55:01 -0600242 Is_FBE = false;
Ethan Yonker253368a2014-11-25 15:00:52 -0600243 Mount_To_Decrypt = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400244 Decrypted_Block_Device = "";
245 Display_Name = "";
Dees_Troya13d74f2013-03-24 08:54:55 -0500246 Backup_Display_Name = "";
247 Storage_Name = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400248 Backup_Name = "";
Dees_Troy63c8df72012-09-10 14:02:05 -0400249 Backup_FileName = "";
Dees_Troy38bd7602012-09-14 13:33:53 -0400250 MTD_Name = "";
bigbiffce8f83c2015-12-12 18:30:21 -0500251 Backup_Method = BM_NONE;
Dees_Troy83bd4832013-05-04 12:39:56 +0000252 Can_Encrypt_Backup = false;
253 Use_Userdata_Encryption = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400254 Has_Data_Media = false;
Dees_Troye58d5262012-09-21 12:27:57 -0400255 Has_Android_Secure = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400256 Is_Storage = false;
Dees_Troya13d74f2013-03-24 08:54:55 -0500257 Is_Settings_Storage = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400258 Storage_Path = "";
259 Current_File_System = "";
260 Fstab_File_System = "";
Hashcode62bd9e02013-11-19 21:59:42 -0800261 Mount_Flags = 0;
262 Mount_Options = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400263 Format_Block_Size = 0;
Dees_Troy68cab492012-12-12 19:29:35 +0000264 Ignore_Blkid = false;
Dees_Troy16c2b312013-01-15 16:51:18 +0000265 Retain_Layout_Version = false;
dianlujitao4879b372018-12-03 18:45:47 +0800266 Crypto_Key_Location = "";
Ethan Yonker726a0202014-12-16 20:01:38 -0600267 MTP_Storage_ID = 0;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600268 Can_Flash_Img = false;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500269 Mount_Read_Only = false;
Ethan Yonker66a19492015-12-10 10:19:45 -0600270 Is_Adopted_Storage = false;
271 Adopted_GUID = "";
Ethan Yonker1b190162016-12-05 15:25:19 -0600272 SlotSelect = false;
Ethan Yonker93382822018-11-01 15:25:31 -0500273 Key_Directory = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400274}
275
276TWPartition::~TWPartition(void) {
277 // Do nothing
278}
279
Chaosmasterf6e42ce2020-01-27 00:17:04 +0100280bool 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 -0500281 char full_line[MAX_FSTAB_LINE_LENGTH];
Matt Mower2416a502016-04-12 19:54:46 -0500282 char twflags[MAX_FSTAB_LINE_LENGTH] = "";
Dees_Troy5bf43922012-09-07 16:07:55 -0400283 char* ptr;
Matt Mower2b2dd152016-04-26 11:24:08 -0500284 int line_len = strlen(fstab_line), index = 0, item_index = 0;
Dees_Troya13d74f2013-03-24 08:54:55 -0500285 bool skip = false;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600286 int fstab_version = 1, mount_point_index = 0, fs_index = 1, block_device_index = 2;
287 TWPartition *additional_entry = NULL;
288 std::map<string, Flags_Map>::iterator it;
Dees_Troy5bf43922012-09-07 16:07:55 -0400289
Matt Mower2b2dd152016-04-26 11:24:08 -0500290 strlcpy(full_line, fstab_line, sizeof(full_line));
Dees_Troy51127312012-09-08 13:08:49 -0400291 for (index = 0; index < line_len; index++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500292 if (full_line[index] == 34)
293 skip = !skip;
294 if (!skip && full_line[index] <= 32)
Dees_Troy5bf43922012-09-07 16:07:55 -0400295 full_line[index] = '\0';
Dees_Troy5bf43922012-09-07 16:07:55 -0400296 }
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600297 if (line_len < 10)
298 return false; // There can't possibly be a valid fstab line that is less than 10 chars
299 if (strncmp(fstab_line, "/dev/", strlen("/dev/")) == 0 || strncmp(fstab_line, "/devices/", strlen("/devices/")) == 0) {
300 fstab_version = 2;
301 block_device_index = 0;
302 mount_point_index = 1;
303 fs_index = 2;
304 }
305
306 index = 0;
Dees_Troy5bf43922012-09-07 16:07:55 -0400307 while (index < line_len) {
308 while (index < line_len && full_line[index] == '\0')
309 index++;
310 if (index >= line_len)
311 continue;
312 ptr = full_line + index;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600313 if (item_index == mount_point_index) {
314 Mount_Point = ptr;
315 if (fstab_version == 2) {
316 additional_entry = PartitionManager.Find_Partition_By_Path(Mount_Point);
Chaosmasterf6e42ce2020-01-27 00:17:04 +0100317 if (!Sar_Detect && additional_entry) {
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600318 LOGINFO("Found an additional entry for '%s'\n", Mount_Point.c_str());
319 }
320 }
Chaosmasterf6e42ce2020-01-27 00:17:04 +0100321 if(!Sar_Detect)
322 LOGINFO("Processing '%s'\n", Mount_Point.c_str());
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600323 Backup_Path = Mount_Point;
324 Storage_Path = Mount_Point;
325 Display_Name = ptr + 1;
326 Backup_Display_Name = Display_Name;
327 Storage_Name = Display_Name;
328 item_index++;
329 } else if (item_index == fs_index) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400330 // File System
331 Fstab_File_System = ptr;
332 Current_File_System = ptr;
333 item_index++;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600334 } else if (item_index == block_device_index) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400335 // Primary Block Device
Dees_Troy38bd7602012-09-14 13:33:53 -0400336 if (Fstab_File_System == "mtd" || Fstab_File_System == "yaffs2") {
Dees_Troy094207a2012-09-26 12:00:39 -0400337 MTD_Name = ptr;
338 Find_MTD_Block_Device(MTD_Name);
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400339 } else if (Fstab_File_System == "bml") {
340 if (Mount_Point == "/boot")
341 MTD_Name = "boot";
342 else if (Mount_Point == "/recovery")
343 MTD_Name = "recovery";
344 Primary_Block_Device = ptr;
345 if (*ptr != '/')
Dees_Troy2673cec2013-04-02 20:22:16 +0000346 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 -0400347 } else if (*ptr != '/') {
Dees_Troy5bf43922012-09-07 16:07:55 -0400348 if (Display_Error)
Matt Mower2b2dd152016-04-26 11:24:08 -0500349 LOGERR("Invalid block device '%s' in fstab line '%s'", ptr, fstab_line);
Dees_Troy5bf43922012-09-07 16:07:55 -0400350 else
Matt Mower2b2dd152016-04-26 11:24:08 -0500351 LOGINFO("Invalid block device '%s' in fstab line '%s'", ptr, fstab_line);
Matt Mower72c87ce2016-04-26 14:34:56 -0500352 return false;
Dees_Troy38bd7602012-09-14 13:33:53 -0400353 } else {
354 Primary_Block_Device = ptr;
355 Find_Real_Block_Device(Primary_Block_Device, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400356 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400357 item_index++;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600358 } else if (item_index > 2) {
359 if (fstab_version == 2) {
360 if (item_index == 3) {
361 Process_FS_Flags(ptr);
362 if (additional_entry) {
363 additional_entry->Save_FS_Flags(Fstab_File_System, Mount_Flags, Mount_Options);
364 return false; // We save the extra fs flags in the other partition entry and by returning false, this entry will be deleted
365 }
366 } else {
367 strlcpy(twflags, ptr, sizeof(twflags));
368 }
369 item_index++;
370 } else if (*ptr == '/') { // v2 fstab does not allow alternate block devices
Dees_Troy5bf43922012-09-07 16:07:55 -0400371 // Alternate Block Device
372 Alternate_Block_Device = ptr;
373 Find_Real_Block_Device(Alternate_Block_Device, Display_Error);
374 } else if (strlen(ptr) > 7 && strncmp(ptr, "length=", 7) == 0) {
375 // Partition length
376 ptr += 7;
377 Length = atoi(ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400378 } else if (strlen(ptr) > 6 && strncmp(ptr, "flags=", 6) == 0) {
379 // Custom flags, save for later so that new values aren't overwritten by defaults
380 ptr += 6;
Matt Mower2416a502016-04-12 19:54:46 -0500381 strlcpy(twflags, ptr, sizeof(twflags));
Dees_Troy38bd7602012-09-14 13:33:53 -0400382 } else if (strlen(ptr) == 4 && (strncmp(ptr, "NULL", 4) == 0 || strncmp(ptr, "null", 4) == 0 || strncmp(ptr, "null", 4) == 0)) {
383 // Do nothing
Dees_Troy5bf43922012-09-07 16:07:55 -0400384 } else {
385 // Unhandled data
Matt Mower2b2dd152016-04-26 11:24:08 -0500386 LOGINFO("Unhandled fstab information '%s' in fstab line '%s'\n", ptr, fstab_line);
Dees_Troy5bf43922012-09-07 16:07:55 -0400387 }
388 }
389 while (index < line_len && full_line[index] != '\0')
390 index++;
391 }
392
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600393 // override block devices from the v2 fstab with the ones we read from the twrp.flags file in case they are different
394 if (fstab_version == 2 && twrp_flags && twrp_flags->size() > 0) {
395 it = twrp_flags->find(Mount_Point);
396 if (it != twrp_flags->end()) {
397 if (!it->second.Primary_Block_Device.empty()) {
398 Primary_Block_Device = it->second.Primary_Block_Device;
399 Find_Real_Block_Device(Primary_Block_Device, Display_Error);
400 }
401 if (!it->second.Alternate_Block_Device.empty()) {
402 Alternate_Block_Device = it->second.Alternate_Block_Device;
403 Find_Real_Block_Device(Alternate_Block_Device, Display_Error);
404 }
405 }
406 }
407
408 if (strncmp(fstab_line, "/devices/", strlen("/devices/")) == 0) {
409 Sysfs_Entry = Primary_Block_Device;
410 Primary_Block_Device = "";
411 Is_Storage = true;
412 Removable = true;
413 Wipe_Available_in_GUI = true;
414 Wildcard_Block_Device = true;
415 }
416 if (Primary_Block_Device.find("*") != string::npos)
417 Wildcard_Block_Device = true;
418
Chaosmasterf6e42ce2020-01-27 00:17:04 +0100419 if (Sar_Detect) {
420 if(Is_File_System(Fstab_File_System) && (Mount_Point == "/" || Mount_Point == "/system" || Mount_Point == "/system_root"))
421 Find_Actual_Block_Device();
422 else
423 return true;
424 } else if (Mount_Point == "auto") {
dianlujitaob76a73a2020-04-30 20:33:20 +0800425 Mount_Point = "/auto" + to_string(auto_index);
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600426 Backup_Path = Mount_Point;
427 Storage_Path = Mount_Point;
dianlujitaob76a73a2020-04-30 20:33:20 +0800428 Backup_Name = Mount_Point.substr(1);
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600429 auto_index++;
430 Setup_File_System(Display_Error);
431 Display_Name = "Storage";
432 Backup_Display_Name = Display_Name;
433 Storage_Name = Display_Name;
434 Can_Be_Backed_Up = false;
435 Wipe_Available_in_GUI = true;
436 Is_Storage = true;
437 Removable = true;
438 Wipe_Available_in_GUI = true;
439 } else if (!Is_File_System(Fstab_File_System) && !Is_Image(Fstab_File_System)) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400440 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000441 LOGERR("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400442 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000443 LOGINFO("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Matt Mower72c87ce2016-04-26 14:34:56 -0500444 return false;
Dees_Troy5bf43922012-09-07 16:07:55 -0400445 } else if (Is_File_System(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400446 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400447 Setup_File_System(Display_Error);
dianlujitaob76a73a2020-04-30 20:33:20 +0800448 Backup_Name = Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
Chaosmaster3b682732020-01-25 21:09:28 +0100449 if (Mount_Point == "/" || Mount_Point == "/system" || Mount_Point == "/system_root") {
Dees_Troy5bf43922012-09-07 16:07:55 -0400450 Display_Name = "System";
dianlujitaob76a73a2020-04-30 20:33:20 +0800451 Backup_Name = "system";
Dees_Troya13d74f2013-03-24 08:54:55 -0500452 Backup_Display_Name = Display_Name;
453 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400454 Wipe_Available_in_GUI = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500455 Can_Be_Backed_Up = true;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500456 Mount_Read_Only = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400457 } else if (Mount_Point == "/data") {
458 Display_Name = "Data";
Dees_Troya13d74f2013-03-24 08:54:55 -0500459 Backup_Display_Name = Display_Name;
460 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400461 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400462 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500463 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000464 Can_Encrypt_Backup = true;
465 Use_Userdata_Encryption = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400466 } else if (Mount_Point == "/cache") {
467 Display_Name = "Cache";
Dees_Troya13d74f2013-03-24 08:54:55 -0500468 Backup_Display_Name = Display_Name;
469 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400470 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400471 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500472 Can_Be_Backed_Up = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400473 } else if (Mount_Point == "/datadata") {
Dees_Troy51127312012-09-08 13:08:49 -0400474 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400475 Display_Name = "DataData";
Dees_Troya13d74f2013-03-24 08:54:55 -0500476 Backup_Display_Name = Display_Name;
477 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400478 Is_SubPartition = true;
479 SubPartition_Of = "/data";
Dees_Troy5bf43922012-09-07 16:07:55 -0400480 DataManager::SetValue(TW_HAS_DATADATA, 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500481 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000482 Can_Encrypt_Backup = true;
483 Use_Userdata_Encryption = false; // This whole partition should be encrypted
Dees_Troy5bf43922012-09-07 16:07:55 -0400484 } else if (Mount_Point == "/sd-ext") {
Dees_Troy51127312012-09-08 13:08:49 -0400485 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400486 Display_Name = "SD-Ext";
Dees_Troya13d74f2013-03-24 08:54:55 -0500487 Backup_Display_Name = Display_Name;
488 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400489 Wipe_Available_in_GUI = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400490 Removable = true;
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 = true;
Dees_Troy2c50e182012-09-26 20:05:28 -0400494 } else if (Mount_Point == "/boot") {
495 Display_Name = "Boot";
Dees_Troya13d74f2013-03-24 08:54:55 -0500496 Backup_Display_Name = Display_Name;
Dees_Troy2c50e182012-09-26 20:05:28 -0400497 DataManager::SetValue("tw_boot_is_mountable", 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500498 Can_Be_Backed_Up = true;
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500499 } else if (Mount_Point == "/vendor") {
500 Display_Name = "Vendor";
501 Backup_Display_Name = Display_Name;
502 Storage_Name = Display_Name;
503 Mount_Read_Only = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400504 }
505#ifdef TW_EXTERNAL_STORAGE_PATH
506 if (Mount_Point == EXPAND(TW_EXTERNAL_STORAGE_PATH)) {
507 Is_Storage = true;
508 Storage_Path = EXPAND(TW_EXTERNAL_STORAGE_PATH);
Dees_Troyc51f1f92012-09-20 15:32:13 -0400509 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500510 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400511#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000512 if (Mount_Point == "/sdcard" || Mount_Point == "/external_sd" || Mount_Point == "/external_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400513 Is_Storage = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400514 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500515 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400516#endif
Dees_Troyb05ddee2013-01-28 20:24:50 +0000517 }
Dees_Troy8170a922012-09-18 15:40:25 -0400518#ifdef TW_INTERNAL_STORAGE_PATH
519 if (Mount_Point == EXPAND(TW_INTERNAL_STORAGE_PATH)) {
520 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500521 Is_Settings_Storage = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400522 Storage_Path = EXPAND(TW_INTERNAL_STORAGE_PATH);
Dees_Troya13d74f2013-03-24 08:54:55 -0500523 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400524 }
525#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000526 if (Mount_Point == "/emmc" || Mount_Point == "/internal_sd" || Mount_Point == "/internal_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400527 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500528 Is_Settings_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500529 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400530 }
531#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400532 } else if (Is_Image(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400533 Find_Actual_Block_Device();
Ethan Yonker1b190162016-12-05 15:25:19 -0600534 Setup_Image();
Dees_Troya13d74f2013-03-24 08:54:55 -0500535 if (Mount_Point == "/boot") {
536 Display_Name = "Boot";
537 Backup_Display_Name = Display_Name;
538 Can_Be_Backed_Up = true;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600539 Can_Flash_Img = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500540 } else if (Mount_Point == "/recovery") {
541 Display_Name = "Recovery";
542 Backup_Display_Name = Display_Name;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600543 Can_Flash_Img = true;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500544 } else if (Mount_Point == "/system_image") {
545 Display_Name = "System Image";
546 Backup_Display_Name = Display_Name;
HashBanged974bb2016-01-30 14:20:09 -0500547 Can_Flash_Img = true;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500548 Can_Be_Backed_Up = true;
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500549 } else if (Mount_Point == "/vendor_image") {
550 Display_Name = "Vendor Image";
551 Backup_Display_Name = Display_Name;
HashBanged974bb2016-01-30 14:20:09 -0500552 Can_Flash_Img = true;
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500553 Can_Be_Backed_Up = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500554 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400555 }
556
Matt Mower2416a502016-04-12 19:54:46 -0500557 // Process TWRP fstab flags
558 if (strlen(twflags) > 0) {
559 string Prev_Display_Name = Display_Name;
560 string Prev_Storage_Name = Storage_Name;
561 string Prev_Backup_Display_Name = Backup_Display_Name;
562 Display_Name = "";
563 Storage_Name = "";
564 Backup_Display_Name = "";
565
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600566 Process_TW_Flags(twflags, (fstab_version == 1), fstab_version);
567 Save_FS_Flags(Fstab_File_System, Mount_Flags, Mount_Options);
Matt Mower2416a502016-04-12 19:54:46 -0500568
569 bool has_display_name = !Display_Name.empty();
570 bool has_storage_name = !Storage_Name.empty();
571 bool has_backup_name = !Backup_Display_Name.empty();
572 if (!has_display_name) Display_Name = Prev_Display_Name;
573 if (!has_storage_name) Storage_Name = Prev_Storage_Name;
574 if (!has_backup_name) Backup_Display_Name = Prev_Backup_Display_Name;
575
576 if (has_display_name && !has_storage_name)
577 Storage_Name = Display_Name;
578 if (!has_display_name && has_storage_name)
579 Display_Name = Storage_Name;
580 if (has_display_name && !has_backup_name && Backup_Display_Name != "Android Secure")
581 Backup_Display_Name = Display_Name;
582 if (!has_display_name && has_backup_name)
583 Display_Name = Backup_Display_Name;
584 }
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600585
586 if (fstab_version == 2 && twrp_flags && twrp_flags->size() > 0) {
587 it = twrp_flags->find(Mount_Point);
588 if (it != twrp_flags->end()) {
589 char twrpflags[MAX_FSTAB_LINE_LENGTH] = "";
590 int skip = 0;
591 string Flags = it->second.Flags;
592 strcpy(twrpflags, Flags.c_str());
593 if (strlen(twrpflags) > strlen("flags=") && strncmp(twrpflags, "flags=", strlen("flags=")) == 0)
594 skip += strlen("flags=");
595 char* flagptr = twrpflags;
596 flagptr += skip;
597 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
598 }
599 }
nkk7198fc3992017-12-16 16:26:42 +0200600
601 if (Mount_Point == "/persist" && Can_Be_Mounted) {
602 bool mounted = Is_Mounted();
603 if (mounted || Mount(false)) {
604 // Read the backup settings file
605 DataManager::LoadPersistValues();
606 TWFunc::Fixup_Time_On_Boot("/persist/time/");
607 if (!mounted)
608 UnMount(false);
609 }
610 }
611
Chaosmasterf6e42ce2020-01-27 00:17:04 +0100612 if (Is_File_System(Fstab_File_System) && (Mount_Point == "/" || Mount_Point == "/system" || Mount_Point == "/system_root")) {
613 if (Sar_Detect) {
614 Mount_Point = "/s";
615 Mount_Read_Only = true;
616 Can_Be_Mounted = true;
617 } else {
618 Mount_Point = PartitionManager.Get_Android_Root_Path();
619 Backup_Path = Mount_Point;
620 Storage_Path = Mount_Point;
621 Make_Dir(Mount_Point, Display_Error);
622 }
623 }
624
Dees_Troy51127312012-09-08 13:08:49 -0400625 return true;
626}
627
Matt Mower72c87ce2016-04-26 14:34:56 -0500628void TWPartition::Partition_Post_Processing(bool Display_Error) {
629 if (Mount_Point == "/data")
630 Setup_Data_Partition(Display_Error);
631 else if (Mount_Point == "/cache")
632 Setup_Cache_Partition(Display_Error);
633}
634
Ethan Yonkerbd7492d2016-12-07 13:55:01 -0600635void TWPartition::ExcludeAll(const string& path) {
636 backup_exclusions.add_absolute_dir(path);
637 wipe_exclusions.add_absolute_dir(path);
638}
639
Matt Mower72c87ce2016-04-26 14:34:56 -0500640void TWPartition::Setup_Data_Partition(bool Display_Error) {
641 if (Mount_Point != "/data")
642 return;
643
644 // Ensure /data is not mounted as tmpfs for qcom hardware decrypt
645 UnMount(false);
646
647#ifdef TW_INCLUDE_CRYPTO
648 if (datamedia)
649 Setup_Data_Media();
650 Can_Be_Encrypted = true;
651 char crypto_blkdev[255];
652 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
653 if (strcmp(crypto_blkdev, "error") != 0) {
654 DataManager::SetValue(TW_IS_DECRYPTED, 1);
655 Is_Encrypted = true;
656 Is_Decrypted = true;
Ethan Yonker93382822018-11-01 15:25:31 -0500657 if (Key_Directory.empty())
658 Is_FBE = false;
659 else
660 Is_FBE = true;
Ethan Yonkerbd7492d2016-12-07 13:55:01 -0600661 DataManager::SetValue(TW_IS_FBE, 0);
Matt Mower72c87ce2016-04-26 14:34:56 -0500662 Decrypted_Block_Device = crypto_blkdev;
663 LOGINFO("Data already decrypted, new block device: '%s'\n", crypto_blkdev);
664 } else if (!Mount(false)) {
665 if (Is_Present) {
Ethan Yonker93382822018-11-01 15:25:31 -0500666 if (Key_Directory.empty()) {
667 set_partition_data(Actual_Block_Device.c_str(), Crypto_Key_Location.c_str(), Fstab_File_System.c_str());
668 if (cryptfs_check_footer() == 0) {
669 Is_Encrypted = true;
670 Is_Decrypted = false;
671 Can_Be_Mounted = false;
672 Current_File_System = "emmc";
673 Setup_Image();
674 DataManager::SetValue(TW_IS_ENCRYPTED, 1);
675 DataManager::SetValue(TW_CRYPTO_PWTYPE, cryptfs_get_password_type());
676 DataManager::SetValue(TW_CRYPTO_PASSWORD, "");
677 DataManager::SetValue("tw_crypto_display", "");
678 } else {
679 gui_err("mount_data_footer=Could not mount /data and unable to find crypto footer.");
680 }
681 } else {
Matt Mower72c87ce2016-04-26 14:34:56 -0500682 Is_Encrypted = true;
683 Is_Decrypted = false;
Matt Mower72c87ce2016-04-26 14:34:56 -0500684 }
Ethan Yonker93382822018-11-01 15:25:31 -0500685 } else if (Key_Directory.empty()) {
Matt Mower72c87ce2016-04-26 14:34:56 -0500686 LOGERR("Primary block device '%s' for mount point '%s' is not present!\n", Primary_Block_Device.c_str(), Mount_Point.c_str());
687 }
688 } else {
mauronofrio9bf73352019-11-23 22:27:34 +0100689
690 if (!Decrypt_FBE_DE()) {
mauronofrio1db94322019-11-23 23:13:04 +0100691 char wrappedvalue[PROPERTY_VALUE_MAX];
692 property_get("fbe.data.wrappedkey", wrappedvalue, "");
693 std::string wrappedkeyvalue(wrappedvalue);
694 if (wrappedkeyvalue == "true") {
695 LOGERR("Unable to decrypt FBE device\n");
696 } else {
697 LOGINFO("Trying wrapped key.\n");
698 property_set("fbe.data.wrappedkey", "true");
mauronofrio9bf73352019-11-23 22:27:34 +0100699 if (!Decrypt_FBE_DE()) {
Mauronofrio Matarrese6d5d08a2019-12-03 14:25:25 +0100700 LOGINFO("Unable to decrypt device\n");
mauronofrio9bf73352019-11-23 22:27:34 +0100701 }
mauronofrio1db94322019-11-23 23:13:04 +0100702 }
mauronofrio9bf73352019-11-23 22:27:34 +0100703 }
Matt Mower72c87ce2016-04-26 14:34:56 -0500704 }
705 if (datamedia && (!Is_Encrypted || (Is_Encrypted && Is_Decrypted))) {
706 Setup_Data_Media();
707 Recreate_Media_Folder();
708 }
709#else
710 if (datamedia) {
711 Setup_Data_Media();
712 Recreate_Media_Folder();
713 }
714#endif
715}
716
Ethan Yonker93382822018-11-01 15:25:31 -0500717bool TWPartition::Decrypt_FBE_DE() {
718if (TWFunc::Path_Exists("/data/unencrypted/key/version")) {
719 LOGINFO("File Based Encryption is present\n");
720#ifdef TW_INCLUDE_FBE
721 ExcludeAll(Mount_Point + "/convert_fbe");
722 ExcludeAll(Mount_Point + "/unencrypted");
723 //ExcludeAll(Mount_Point + "/system/users/0"); // we WILL need to retain some of this if multiple users are present or we just need to delete more folders for the extra users somewhere else
724 ExcludeAll(Mount_Point + "/misc/vold/user_keys");
725 //ExcludeAll(Mount_Point + "/system_ce");
726 //ExcludeAll(Mount_Point + "/system_de");
727 //ExcludeAll(Mount_Point + "/misc_ce");
728 //ExcludeAll(Mount_Point + "/misc_de");
729 ExcludeAll(Mount_Point + "/system/gatekeeper.password.key");
730 ExcludeAll(Mount_Point + "/system/gatekeeper.pattern.key");
731 ExcludeAll(Mount_Point + "/system/locksettings.db");
732 //ExcludeAll(Mount_Point + "/system/locksettings.db-shm"); // don't seem to need this one, but the other 2 are needed
733 ExcludeAll(Mount_Point + "/system/locksettings.db-wal");
734 //ExcludeAll(Mount_Point + "/user_de");
735 //ExcludeAll(Mount_Point + "/misc/profiles/cur/0"); // might be important later
736 ExcludeAll(Mount_Point + "/misc/gatekeeper");
737 ExcludeAll(Mount_Point + "/misc/keystore");
738 ExcludeAll(Mount_Point + "/drm/kek.dat");
739 ExcludeAll(Mount_Point + "/system_de/0/spblob"); // contains data needed to decrypt pixel 2
740 int retry_count = 3;
741 while (!Decrypt_DE() && --retry_count)
742 usleep(2000);
743 if (retry_count > 0) {
744 property_set("ro.crypto.state", "encrypted");
745 Is_Encrypted = true;
746 Is_Decrypted = false;
747 Is_FBE = true;
748 DataManager::SetValue(TW_IS_FBE, 1);
749 DataManager::SetValue(TW_IS_ENCRYPTED, 1);
750 string filename;
751 int pwd_type = Get_Password_Type(0, filename);
752 if (pwd_type < 0) {
753 LOGERR("This TWRP does not have synthetic password decrypt support\n");
754 pwd_type = 0; // default password
755 }
756 DataManager::SetValue(TW_CRYPTO_PWTYPE, pwd_type);
757 DataManager::SetValue(TW_CRYPTO_PASSWORD, "");
758 DataManager::SetValue("tw_crypto_display", "");
759 return true;
760 }
761#else
762 LOGERR("FBE found but FBE support not present in TWRP\n");
763#endif
764 }
765 return false;
766}
767
Matt Mower72c87ce2016-04-26 14:34:56 -0500768void TWPartition::Setup_Cache_Partition(bool Display_Error __unused) {
769 if (Mount_Point != "/cache")
770 return;
771
772 if (!Mount(true))
773 return;
774
775 if (!TWFunc::Path_Exists("/cache/recovery/.")) {
776 LOGINFO("Recreating /cache/recovery folder\n");
777 if (mkdir("/cache/recovery", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0)
778 LOGERR("Could not create /cache/recovery\n");
779 }
780}
781
Matt Mower4ab42b12016-04-21 13:52:18 -0500782void TWPartition::Process_FS_Flags(const char *str) {
783 char *options = strdup(str);
784 char *ptr, *savep;
Hashcode62bd9e02013-11-19 21:59:42 -0800785
Matt Mower4ab42b12016-04-21 13:52:18 -0500786 Mount_Options = "";
Hashcode62bd9e02013-11-19 21:59:42 -0800787
Matt Mower4ab42b12016-04-21 13:52:18 -0500788 // Avoid issues with potentially nested strtok by using strtok_r
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600789 for (ptr = strtok_r(options, ",", &savep); ptr; ptr = strtok_r(NULL, ",", &savep)) {
790 char *equals = strstr(ptr, "=");
791 size_t name_len;
Matt Mower4ab42b12016-04-21 13:52:18 -0500792
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600793 if (!equals)
794 name_len = strlen(ptr);
795 else
796 name_len = equals - ptr;
797
798 // There are some flags that we want to ignore in TWRP
799 bool found_match = false;
800 for (const char** ignored_mount_item = ignored_mount_items; *ignored_mount_item; ignored_mount_item++) {
801 if (strncmp(ptr, *ignored_mount_item, name_len) == 0) {
802 found_match = true;
Hashcode62bd9e02013-11-19 21:59:42 -0800803 break;
804 }
805 }
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600806 if (found_match)
807 continue;
Hashcode62bd9e02013-11-19 21:59:42 -0800808
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600809 // mount_flags are never postfixed by '='
810 if (!equals) {
811 const struct flag_list* mount_flag = mount_flags;
812 for (; mount_flag->name; mount_flag++) {
813 if (strcmp(ptr, mount_flag->name) == 0) {
814 if (mount_flag->flag == MS_RDONLY)
815 Mount_Read_Only = true;
816 else
817 Mount_Flags |= (unsigned)mount_flag->flag;
818 found_match = true;
819 break;
820 }
821 }
822 if (found_match)
823 continue;
Matt Mower4ab42b12016-04-21 13:52:18 -0500824 }
825
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600826 // If we aren't ignoring this flag and it's not a mount flag, then it must be a mount option
827 if (!Mount_Options.empty())
828 Mount_Options += ",";
829 Mount_Options += ptr;
Matt Mower4ab42b12016-04-21 13:52:18 -0500830 }
831 free(options);
Hashcode62bd9e02013-11-19 21:59:42 -0800832}
833
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600834void TWPartition::Save_FS_Flags(const string& local_File_System, int local_Mount_Flags, const string& local_Mount_Options) {
835 partition_fs_flags_struct flags;
836 flags.File_System = local_File_System;
837 flags.Mount_Flags = local_Mount_Flags;
838 flags.Mount_Options = local_Mount_Options;
839 fs_flags.push_back(flags);
840}
841
Matt Mower2416a502016-04-12 19:54:46 -0500842void TWPartition::Apply_TW_Flag(const unsigned flag, const char* str, const bool val) {
843 switch (flag) {
844 case TWFLAG_ANDSEC:
845 Has_Android_Secure = val;
846 break;
847 case TWFLAG_BACKUP:
848 Can_Be_Backed_Up = val;
849 break;
850 case TWFLAG_BACKUPNAME:
851 Backup_Display_Name = str;
852 break;
853 case TWFLAG_BLOCKSIZE:
854 Format_Block_Size = (unsigned long)(atol(str));
855 break;
856 case TWFLAG_CANBEWIPED:
857 Can_Be_Wiped = val;
858 break;
859 case TWFLAG_CANENCRYPTBACKUP:
860 Can_Encrypt_Backup = val;
861 break;
862 case TWFLAG_DEFAULTS:
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600863 case TWFLAG_WAIT:
864 case TWFLAG_VERIFY:
865 case TWFLAG_CHECK:
866 case TWFLAG_NOTRIM:
867 case TWFLAG_VOLDMANAGED:
868 case TWFLAG_RESIZE:
Matt Mower2416a502016-04-12 19:54:46 -0500869 // Do nothing
870 break;
871 case TWFLAG_DISPLAY:
872 Display_Name = str;
873 break;
874 case TWFLAG_ENCRYPTABLE:
875 case TWFLAG_FORCEENCRYPT:
876 Crypto_Key_Location = str;
877 break;
Ethan Yonkere9afc3d2018-08-30 15:16:27 -0500878 case TWFLAG_FILEENCRYPTION:
879 // This flag isn't used by TWRP but is needed in 9.0 FBE decrypt
880 // fileencryption=ice:aes-256-heh
881 {
882 std::string FBE = str;
Ethan Yonkere9afc3d2018-08-30 15:16:27 -0500883 size_t colon_loc = FBE.find(":");
884 if (colon_loc == std::string::npos) {
Ethan Yonker93382822018-11-01 15:25:31 -0500885 property_set("fbe.contents", FBE.c_str());
886 property_set("fbe.filenames", "");
887 LOGINFO("FBE contents '%s', filenames ''\n", FBE.c_str());
Ethan Yonkere9afc3d2018-08-30 15:16:27 -0500888 break;
889 }
Ethan Yonker93382822018-11-01 15:25:31 -0500890 std::string FBE_contents, FBE_filenames;
Ethan Yonkere9afc3d2018-08-30 15:16:27 -0500891 FBE_contents = FBE.substr(0, colon_loc);
892 FBE_filenames = FBE.substr(colon_loc + 1);
893 property_set("fbe.contents", FBE_contents.c_str());
894 property_set("fbe.filenames", FBE_filenames.c_str());
895 LOGINFO("FBE contents '%s', filenames '%s'\n", FBE_contents.c_str(), FBE_filenames.c_str());
896 }
897 break;
Peter Cai40ed06e2019-05-24 11:38:54 +0800898 case TWFLAG_WRAPPEDKEY:
899 // Set fbe.data.wrappedkey to true
900 {
901 property_set("fbe.data.wrappedkey", "true");
902 LOGINFO("FBE wrapped key enabled\n");
903 }
904 break;
Matt Mower2416a502016-04-12 19:54:46 -0500905 case TWFLAG_FLASHIMG:
906 Can_Flash_Img = val;
907 break;
908 case TWFLAG_FSFLAGS:
Matt Mower4ab42b12016-04-21 13:52:18 -0500909 Process_FS_Flags(str);
Matt Mower2416a502016-04-12 19:54:46 -0500910 break;
911 case TWFLAG_IGNOREBLKID:
912 Ignore_Blkid = val;
913 break;
914 case TWFLAG_LENGTH:
915 Length = atoi(str);
916 break;
917 case TWFLAG_MOUNTTODECRYPT:
918 Mount_To_Decrypt = val;
919 break;
920 case TWFLAG_REMOVABLE:
921 Removable = val;
922 break;
923 case TWFLAG_RETAINLAYOUTVERSION:
924 Retain_Layout_Version = val;
925 break;
926 case TWFLAG_SETTINGSSTORAGE:
927 Is_Settings_Storage = val;
928 if (Is_Settings_Storage)
929 Is_Storage = true;
930 break;
931 case TWFLAG_STORAGE:
932 Is_Storage = val;
933 break;
934 case TWFLAG_STORAGENAME:
935 Storage_Name = str;
936 break;
937 case TWFLAG_SUBPARTITIONOF:
938 Is_SubPartition = true;
939 SubPartition_Of = str;
940 break;
941 case TWFLAG_SYMLINK:
942 Symlink_Path = str;
943 break;
944 case TWFLAG_USERDATAENCRYPTBACKUP:
945 Use_Userdata_Encryption = val;
946 if (Use_Userdata_Encryption)
947 Can_Encrypt_Backup = true;
948 break;
949 case TWFLAG_USERMRF:
950 Use_Rm_Rf = val;
951 break;
952 case TWFLAG_WIPEDURINGFACTORYRESET:
953 Wipe_During_Factory_Reset = val;
954 if (Wipe_During_Factory_Reset) {
955 Can_Be_Wiped = true;
956 Wipe_Available_in_GUI = true;
957 }
958 break;
959 case TWFLAG_WIPEINGUI:
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600960 case TWFLAG_FORMATTABLE:
Matt Mower2416a502016-04-12 19:54:46 -0500961 Wipe_Available_in_GUI = val;
962 if (Wipe_Available_in_GUI)
963 Can_Be_Wiped = true;
964 break;
Ethan Yonker1b190162016-12-05 15:25:19 -0600965 case TWFLAG_SLOTSELECT:
966 SlotSelect = true;
967 break;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600968 case TWFLAG_ALTDEVICE:
969 Alternate_Block_Device = str;
970 break;
Ethan Yonker93382822018-11-01 15:25:31 -0500971 case TWFLAG_KEYDIRECTORY:
972 Key_Directory = str;
Matt Mower2416a502016-04-12 19:54:46 -0500973 default:
974 // Should not get here
975 LOGINFO("Flag identified for processing, but later unmatched: %i\n", flag);
976 break;
977 }
978}
Dees_Troy51127312012-09-08 13:08:49 -0400979
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600980void TWPartition::Process_TW_Flags(char *flags, bool Display_Error, int fstab_ver) {
Matt Mower2416a502016-04-12 19:54:46 -0500981 char separator[2] = {'\n', 0};
982 char *ptr, *savep;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600983 char source_separator = ';';
984
985 if (fstab_ver == 2)
986 source_separator = ',';
Matt Mower2416a502016-04-12 19:54:46 -0500987
988 // Semicolons within double-quotes are not forbidden, so replace
989 // only the semicolons intended as separators with '\n' for strtok
990 for (unsigned i = 0, skip = 0; i < strlen(flags); i++) {
991 if (flags[i] == '\"')
Dees_Troya13d74f2013-03-24 08:54:55 -0500992 skip = !skip;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600993 if (!skip && flags[i] == source_separator)
Matt Mower2416a502016-04-12 19:54:46 -0500994 flags[i] = separator[0];
Dees_Troy51127312012-09-08 13:08:49 -0400995 }
996
Matt Mower2416a502016-04-12 19:54:46 -0500997 // Avoid issues with potentially nested strtok by using strtok_r
998 ptr = strtok_r(flags, separator, &savep);
999 while (ptr) {
1000 int ptr_len = strlen(ptr);
1001 const struct flag_list* tw_flag = tw_flags;
Hashcode62bd9e02013-11-19 21:59:42 -08001002
Matt Mower2416a502016-04-12 19:54:46 -05001003 for (; tw_flag->name; tw_flag++) {
1004 int flag_len = strlen(tw_flag->name);
1005
1006 if (strncmp(ptr, tw_flag->name, flag_len) == 0) {
1007 bool flag_val = false;
1008
1009 if (ptr_len > flag_len && (tw_flag->name)[flag_len-1] != '='
1010 && ptr[flag_len] != '=') {
1011 // Handle flags with same starting string
1012 // (e.g. backup and backupname)
1013 continue;
1014 } else if (ptr_len > flag_len && ptr[flag_len] == '=') {
1015 // Handle flags with dual format: Part 1
1016 // (e.g. backup and backup=y. backup=y handled here)
1017 ptr += flag_len + 1;
1018 TWFunc::Strip_Quotes(ptr);
1019 // Skip flags with empty argument
1020 // (e.g. backup=)
1021 if (strlen(ptr) == 0) {
1022 LOGINFO("Flag missing argument or should not include '=': %s=\n", tw_flag->name);
1023 break;
1024 }
1025 flag_val = strchr("yY1", *ptr) != NULL;
1026 } else if (ptr_len == flag_len
1027 && (tw_flag->name)[flag_len-1] == '=') {
1028 // Skip flags missing argument after =
1029 // (e.g. backupname=)
1030 LOGINFO("Flag missing argument: %s\n", tw_flag->name);
1031 break;
1032 } else if (ptr_len > flag_len
1033 && (tw_flag->name)[flag_len-1] == '=') {
1034 // Handle arguments to flags
1035 // (e.g. backupname="My Stuff")
1036 ptr += flag_len;
1037 TWFunc::Strip_Quotes(ptr);
1038 // Skip flags with empty argument
1039 // (e.g. backupname="")
1040 if (strlen(ptr) == 0) {
1041 LOGINFO("Flag missing argument: %s\n", tw_flag->name);
1042 break;
1043 }
1044 } else if (ptr_len == flag_len) {
1045 // Handle flags with dual format: Part 2
1046 // (e.g. backup and backup=y. backup handled here)
1047 flag_val = true;
Ethan Yonker96af84a2015-01-05 14:58:36 -06001048 } else {
Matt Mower2416a502016-04-12 19:54:46 -05001049 LOGINFO("Flag matched, but could not be processed: %s\n", ptr);
1050 break;
Ethan Yonker96af84a2015-01-05 14:58:36 -06001051 }
Matt Mower2416a502016-04-12 19:54:46 -05001052
1053 Apply_TW_Flag(tw_flag->flag, ptr, flag_val);
1054 break;
Ethan Yonker96af84a2015-01-05 14:58:36 -06001055 }
Matt Mower2416a502016-04-12 19:54:46 -05001056 }
1057 if (tw_flag->name == 0) {
Dees_Troy51127312012-09-08 13:08:49 -04001058 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001059 LOGERR("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -04001060 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001061 LOGINFO("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -04001062 }
Matt Mower2416a502016-04-12 19:54:46 -05001063 ptr = strtok_r(NULL, separator, &savep);
Dees_Troy51127312012-09-08 13:08:49 -04001064 }
Dees_Troy51a0e822012-09-05 15:24:24 -04001065}
1066
Dees_Troy5bf43922012-09-07 16:07:55 -04001067bool TWPartition::Is_File_System(string File_System) {
1068 if (File_System == "ext2" ||
Dees_Troy63c8df72012-09-10 14:02:05 -04001069 File_System == "ext3" ||
Dees_Troy5bf43922012-09-07 16:07:55 -04001070 File_System == "ext4" ||
1071 File_System == "vfat" ||
1072 File_System == "ntfs" ||
1073 File_System == "yaffs2" ||
bigbiff bigbiff3e146522012-11-14 14:32:59 -05001074 File_System == "exfat" ||
Dees_Troye5017042013-08-29 16:38:55 +00001075 File_System == "f2fs" ||
Erfan Abdib859e912019-02-05 18:18:46 +02001076 File_System == "squashfs" ||
Dees_Troy5bf43922012-09-07 16:07:55 -04001077 File_System == "auto")
1078 return true;
1079 else
1080 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001081}
1082
Dees_Troy5bf43922012-09-07 16:07:55 -04001083bool TWPartition::Is_Image(string File_System) {
Dees_Troy5fcd8f92012-10-16 12:22:05 -04001084 if (File_System == "emmc" || File_System == "mtd" || File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -04001085 return true;
1086 else
1087 return false;
1088}
1089
Dees_Troy51127312012-09-08 13:08:49 -04001090bool TWPartition::Make_Dir(string Path, bool Display_Error) {
Ethan Yonker93ac7a02016-11-07 22:05:58 -06001091 if (TWFunc::Get_D_Type_From_Stat(Path) != S_IFDIR)
1092 unlink(Path.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001093 if (!TWFunc::Path_Exists(Path)) {
Dees_Troy51127312012-09-08 13:08:49 -04001094 if (mkdir(Path.c_str(), 0777) == -1) {
1095 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001096 gui_msg(Msg(msg::kError, "create_folder_strerr=Can not create '{1}' folder ({2}).")(Path)(strerror(errno)));
Dees_Troy51127312012-09-08 13:08:49 -04001097 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001098 LOGINFO("Can not create '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -04001099 return false;
1100 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001101 LOGINFO("Created '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -04001102 return true;
1103 }
1104 }
1105 return true;
1106}
1107
Dees_Troy5bf43922012-09-07 16:07:55 -04001108void TWPartition::Setup_File_System(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001109 Can_Be_Mounted = true;
1110 Can_Be_Wiped = true;
1111
Dees_Troy5bf43922012-09-07 16:07:55 -04001112 // Make the mount point folder if it doesn't exist
Dees_Troy51127312012-09-08 13:08:49 -04001113 Make_Dir(Mount_Point, Display_Error);
bigbiffce8f83c2015-12-12 18:30:21 -05001114 Backup_Method = BM_FILES;
Dees_Troy5bf43922012-09-07 16:07:55 -04001115}
1116
Ethan Yonker1b190162016-12-05 15:25:19 -06001117void TWPartition::Setup_Image() {
Dees_Troy5bf43922012-09-07 16:07:55 -04001118 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
1119 Backup_Name = Display_Name;
Gary Peck82599a82012-11-21 16:23:12 -08001120 if (Current_File_System == "emmc")
bigbiffce8f83c2015-12-12 18:30:21 -05001121 Backup_Method = BM_DD;
Gary Peck82599a82012-11-21 16:23:12 -08001122 else if (Current_File_System == "mtd" || Current_File_System == "bml")
bigbiffce8f83c2015-12-12 18:30:21 -05001123 Backup_Method = BM_FLASH_UTILS;
Dees_Troy5bf43922012-09-07 16:07:55 -04001124 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001125 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 -04001126}
1127
Dees_Troye58d5262012-09-21 12:27:57 -04001128void TWPartition::Setup_AndSec(void) {
Dees_Troya13d74f2013-03-24 08:54:55 -05001129 Backup_Display_Name = "Android Secure";
Dees_Troye58d5262012-09-21 12:27:57 -04001130 Backup_Name = "and-sec";
Dees_Troya13d74f2013-03-24 08:54:55 -05001131 Can_Be_Backed_Up = true;
Dees_Troye58d5262012-09-21 12:27:57 -04001132 Has_Android_Secure = true;
1133 Symlink_Path = Mount_Point + "/.android_secure";
1134 Symlink_Mount_Point = "/and-sec";
1135 Backup_Path = Symlink_Mount_Point;
1136 Make_Dir("/and-sec", true);
1137 Recreate_AndSec_Folder();
Kjell Braden3126a112016-06-19 16:58:15 +00001138 Mount_Storage_Retry(true);
Dees_Troye58d5262012-09-21 12:27:57 -04001139}
1140
that9e0593e2014-10-08 00:01:24 +02001141void TWPartition::Setup_Data_Media() {
Ethan Yonker6277c792014-09-15 14:54:30 -05001142 LOGINFO("Setting up '%s' as data/media emulated storage.\n", Mount_Point.c_str());
Ethan Yonker66a19492015-12-10 10:19:45 -06001143 if (Storage_Name.empty() || Storage_Name == "Data")
1144 Storage_Name = "Internal Storage";
Ethan Yonker6277c792014-09-15 14:54:30 -05001145 Has_Data_Media = true;
1146 Is_Storage = true;
Ethan Yonker66a19492015-12-10 10:19:45 -06001147 Storage_Path = Mount_Point + "/media";
Ethan Yonker6277c792014-09-15 14:54:30 -05001148 Symlink_Path = Storage_Path;
Ethan Yonker66a19492015-12-10 10:19:45 -06001149 if (Mount_Point == "/data") {
1150 Is_Settings_Storage = true;
1151 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
1152 Make_Dir("/emmc", false);
1153 Symlink_Mount_Point = "/emmc";
1154 } else {
1155 Make_Dir("/sdcard", false);
1156 Symlink_Mount_Point = "/sdcard";
1157 }
1158 if (Mount(false) && TWFunc::Path_Exists(Mount_Point + "/media/0")) {
1159 Storage_Path = Mount_Point + "/media/0";
1160 Symlink_Path = Storage_Path;
1161 DataManager::SetValue(TW_INTERNAL_PATH, Mount_Point + "/media/0");
1162 UnMount(true);
1163 }
1164 DataManager::SetValue("tw_has_internal", 1);
1165 DataManager::SetValue("tw_has_data_media", 1);
Ethan Yonker3fdcda42016-11-30 12:29:37 -06001166 backup_exclusions.add_absolute_dir("/data/data/com.google.android.music/files");
Darth90864aa52020-01-14 20:58:16 +00001167 backup_exclusions.add_absolute_dir("/data/per_boot"); // DJ9,14Jan2020 - exclude this dir to prevent "error 255" on AOSP ROMs that create and lock it
Ethan Yonker6355b562017-05-01 09:42:17 -05001168 wipe_exclusions.add_absolute_dir(Mount_Point + "/misc/vold"); // adopted storage keys
Ethan Yonker79f88bd2016-12-09 14:52:12 -06001169 ExcludeAll(Mount_Point + "/.layout_version");
1170 ExcludeAll(Mount_Point + "/system/storage.xml");
Ethan Yonker6277c792014-09-15 14:54:30 -05001171 } else {
Ethan Yonker66a19492015-12-10 10:19:45 -06001172 if (Mount(true) && TWFunc::Path_Exists(Mount_Point + "/media/0")) {
1173 Storage_Path = Mount_Point + "/media/0";
1174 Symlink_Path = Storage_Path;
1175 UnMount(true);
1176 }
Ethan Yonker6277c792014-09-15 14:54:30 -05001177 }
Ethan Yonker79f88bd2016-12-09 14:52:12 -06001178 ExcludeAll(Mount_Point + "/media");
Ethan Yonker6277c792014-09-15 14:54:30 -05001179}
1180
Dees_Troy5bf43922012-09-07 16:07:55 -04001181void TWPartition::Find_Real_Block_Device(string& Block, bool Display_Error) {
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06001182 char device[PATH_MAX], realDevice[PATH_MAX];
Dees_Troy5bf43922012-09-07 16:07:55 -04001183
1184 strcpy(device, Block.c_str());
1185 memset(realDevice, 0, sizeof(realDevice));
1186 while (readlink(device, realDevice, sizeof(realDevice)) > 0)
1187 {
1188 strcpy(device, realDevice);
1189 memset(realDevice, 0, sizeof(realDevice));
1190 }
1191
1192 if (device[0] != '/') {
1193 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001194 LOGERR("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001195 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001196 LOGINFO("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001197 return;
Dees_Troy51a0e822012-09-05 15:24:24 -04001198 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -04001199 Block = device;
1200 return;
1201 }
1202}
1203
Kjell Braden3126a112016-06-19 16:58:15 +00001204bool TWPartition::Mount_Storage_Retry(bool Display_Error) {
Dees_Troy8e337f32012-10-13 22:07:49 -04001205 // On some devices, storage doesn't want to mount right away, retry and sleep
Kjell Braden3126a112016-06-19 16:58:15 +00001206 if (!Mount(Display_Error)) {
Dees_Troy8e337f32012-10-13 22:07:49 -04001207 int retry_count = 5;
1208 while (retry_count > 0 && !Mount(false)) {
1209 usleep(500000);
1210 retry_count--;
1211 }
Kjell Braden3126a112016-06-19 16:58:15 +00001212 return Mount(Display_Error);
Dees_Troy8e337f32012-10-13 22:07:49 -04001213 }
Kjell Braden3126a112016-06-19 16:58:15 +00001214 return true;
Dees_Troy8e337f32012-10-13 22:07:49 -04001215}
1216
Dees_Troy38bd7602012-09-14 13:33:53 -04001217bool TWPartition::Find_MTD_Block_Device(string MTD_Name) {
1218 FILE *fp = NULL;
1219 char line[255];
1220
1221 fp = fopen("/proc/mtd", "rt");
1222 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001223 LOGERR("Device does not support /proc/mtd\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001224 return false;
1225 }
1226
1227 while (fgets(line, sizeof(line), fp) != NULL)
1228 {
1229 char device[32], label[32];
1230 unsigned long size = 0;
Dees_Troy38bd7602012-09-14 13:33:53 -04001231 int deviceId;
1232
1233 sscanf(line, "%s %lx %*s %*c%s", device, &size, label);
1234
1235 // Skip header and blank lines
1236 if ((strcmp(device, "dev:") == 0) || (strlen(line) < 8))
1237 continue;
1238
1239 // Strip off the trailing " from the label
1240 label[strlen(label)-1] = '\0';
1241
1242 if (strcmp(label, MTD_Name.c_str()) == 0) {
1243 // We found our device
1244 // Strip off the trailing : from the device
1245 device[strlen(device)-1] = '\0';
1246 if (sscanf(device,"mtd%d", &deviceId) == 1) {
1247 sprintf(device, "/dev/block/mtdblock%d", deviceId);
1248 Primary_Block_Device = device;
Dees_Troy76543db2013-06-19 16:24:30 +00001249 fclose(fp);
1250 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -04001251 }
1252 }
1253 }
1254 fclose(fp);
1255
1256 return false;
1257}
1258
Dees_Troy51127312012-09-08 13:08:49 -04001259bool TWPartition::Get_Size_Via_statfs(bool Display_Error) {
1260 struct statfs st;
1261 string Local_Path = Mount_Point + "/.";
1262
1263 if (!Mount(Display_Error))
1264 return false;
1265
1266 if (statfs(Local_Path.c_str(), &st) != 0) {
1267 if (!Removable) {
1268 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001269 LOGERR("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -04001270 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001271 LOGINFO("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -04001272 }
1273 return false;
1274 }
1275 Size = (st.f_blocks * st.f_bsize);
1276 Used = ((st.f_blocks - st.f_bfree) * st.f_bsize);
1277 Free = (st.f_bfree * st.f_bsize);
1278 Backup_Size = Used;
1279 return true;
1280}
1281
1282bool TWPartition::Get_Size_Via_df(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001283 FILE* fp;
1284 char command[255], line[512];
1285 int include_block = 1;
1286 unsigned int min_len;
1287
1288 if (!Mount(Display_Error))
1289 return false;
1290
Dees_Troy38bd7602012-09-14 13:33:53 -04001291 min_len = Actual_Block_Device.size() + 2;
Dees_Troy51127312012-09-08 13:08:49 -04001292 sprintf(command, "df %s > /tmp/dfoutput.txt", Mount_Point.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001293 TWFunc::Exec_Cmd(command);
Dees_Troy51127312012-09-08 13:08:49 -04001294 fp = fopen("/tmp/dfoutput.txt", "rt");
1295 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001296 LOGINFO("Unable to open /tmp/dfoutput.txt.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001297 return false;
Dees_Troy51127312012-09-08 13:08:49 -04001298 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001299
1300 while (fgets(line, sizeof(line), fp) != NULL)
1301 {
1302 unsigned long blocks, used, available;
1303 char device[64];
1304 char tmpString[64];
1305
1306 if (strncmp(line, "Filesystem", 10) == 0)
1307 continue;
1308 if (strlen(line) < min_len) {
1309 include_block = 0;
1310 continue;
1311 }
1312 if (include_block) {
1313 sscanf(line, "%s %lu %lu %lu", device, &blocks, &used, &available);
1314 } else {
1315 // The device block string is so long that the df information is on the next line
1316 int space_count = 0;
Dees_Troye58d5262012-09-21 12:27:57 -04001317 sprintf(tmpString, "/dev/block/%s", Actual_Block_Device.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001318 while (tmpString[space_count] == 32)
1319 space_count++;
1320 sscanf(line + space_count, "%lu %lu %lu", &blocks, &used, &available);
1321 }
1322
1323 // Adjust block size to byte size
1324 Size = blocks * 1024ULL;
1325 Used = used * 1024ULL;
1326 Free = available * 1024ULL;
1327 Backup_Size = Used;
1328 }
1329 fclose(fp);
1330 return true;
1331}
1332
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001333unsigned long long TWPartition::IOCTL_Get_Block_Size() {
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001334 Find_Actual_Block_Device();
Ethan Yonker483e9f42016-01-11 22:21:18 -06001335
1336 return TWFunc::IOCTL_Get_Block_Size(Actual_Block_Device.c_str());
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001337}
1338
Dees_Troy5bf43922012-09-07 16:07:55 -04001339bool TWPartition::Find_Partition_Size(void) {
1340 FILE* fp;
1341 char line[512];
1342 string tmpdevice;
1343
igoriok87e3d932013-01-31 21:03:53 +02001344 fp = fopen("/proc/dumchar_info", "rt");
1345 if (fp != NULL) {
1346 while (fgets(line, sizeof(line), fp) != NULL)
1347 {
1348 char label[32], device[32];
1349 unsigned long size = 0;
1350
thatd43bf2d2014-09-21 23:13:02 +02001351 sscanf(line, "%s %lx %*x %*u %s", label, &size, device);
igoriok87e3d932013-01-31 21:03:53 +02001352
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05001353 // Skip header, annotation and blank lines
igoriok87e3d932013-01-31 21:03:53 +02001354 if ((strncmp(device, "/dev/", 5) != 0) || (strlen(line) < 8))
1355 continue;
1356
1357 tmpdevice = "/dev/";
1358 tmpdevice += label;
1359 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
1360 Size = size;
1361 fclose(fp);
1362 return true;
1363 }
1364 }
1365 }
1366
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001367 unsigned long long ioctl_size = IOCTL_Get_Block_Size();
1368 if (ioctl_size) {
1369 Size = ioctl_size;
1370 return true;
1371 }
1372
Dees_Troy5bf43922012-09-07 16:07:55 -04001373 // In this case, we'll first get the partitions we care about (with labels)
1374 fp = fopen("/proc/partitions", "rt");
1375 if (fp == NULL)
1376 return false;
1377
1378 while (fgets(line, sizeof(line), fp) != NULL)
1379 {
1380 unsigned long major, minor, blocks;
1381 char device[512];
Dees_Troy5bf43922012-09-07 16:07:55 -04001382
Dees_Troy63c8df72012-09-10 14:02:05 -04001383 if (strlen(line) < 7 || line[0] == 'm') continue;
Dees_Troy5bf43922012-09-07 16:07:55 -04001384 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
1385
1386 tmpdevice = "/dev/block/";
1387 tmpdevice += device;
Dees_Troy38bd7602012-09-14 13:33:53 -04001388 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001389 // Adjust block size to byte size
1390 Size = blocks * 1024ULL;
1391 fclose(fp);
1392 return true;
1393 }
1394 }
1395 fclose(fp);
1396 return false;
1397}
1398
Dees_Troy5bf43922012-09-07 16:07:55 -04001399bool TWPartition::Is_Mounted(void) {
1400 if (!Can_Be_Mounted)
1401 return false;
1402
1403 struct stat st1, st2;
1404 string test_path;
1405
1406 // Check to see if the mount point directory exists
1407 test_path = Mount_Point + "/.";
1408 if (stat(test_path.c_str(), &st1) != 0) return false;
1409
1410 // Check to see if the directory above the mount point exists
1411 test_path = Mount_Point + "/../.";
1412 if (stat(test_path.c_str(), &st2) != 0) return false;
1413
1414 // Compare the device IDs -- if they match then we're (probably) using tmpfs instead of an actual device
1415 int ret = (st1.st_dev != st2.st_dev) ? true : false;
1416
1417 return ret;
1418}
1419
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05001420bool TWPartition::Is_File_System_Writable(void) {
1421 if (!Is_File_System(Current_File_System) || !Is_Mounted())
1422 return false;
1423
1424 string test_path = Mount_Point + "/.";
1425 return (access(test_path.c_str(), W_OK) == 0);
1426}
1427
Dees_Troy5bf43922012-09-07 16:07:55 -04001428bool TWPartition::Mount(bool Display_Error) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001429 int exfat_mounted = 0;
Martin Dünkelmann6d843782018-12-27 23:40:28 +01001430 unsigned int flags = Mount_Flags;
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001431
Dees_Troy5bf43922012-09-07 16:07:55 -04001432 if (Is_Mounted()) {
1433 return true;
1434 } else if (!Can_Be_Mounted) {
1435 return false;
1436 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001437
1438 Find_Actual_Block_Device();
1439
1440 // Check the current file system before mounting
1441 Check_FS_Type();
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001442 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/exfat-fuse")) {
Dees_Troye34c1332013-02-06 19:13:00 +00001443 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 +00001444 LOGINFO("cmd: %s\n", cmd.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001445 string result;
1446 if (TWFunc::Exec_Cmd(cmd, result) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001447 LOGINFO("exfat-fuse failed to mount with result '%s', trying vfat\n", result.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001448 Current_File_System = "vfat";
1449 } else {
1450#ifdef TW_NO_EXFAT_FUSE
1451 UnMount(false);
1452 // We'll let the kernel handle it but using exfat-fuse to detect if the file system is actually exfat
1453 // Some kernels let us mount vfat as exfat which doesn't work out too well
1454#else
1455 exfat_mounted = 1;
1456#endif
1457 }
1458 }
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001459
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001460 if (Current_File_System == "ntfs" && (TWFunc::Path_Exists("/sbin/ntfs-3g") || TWFunc::Path_Exists("/sbin/mount.ntfs"))) {
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001461 string cmd;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001462 string Ntfsmount_Binary = "";
1463
1464 if (TWFunc::Path_Exists("/sbin/ntfs-3g"))
1465 Ntfsmount_Binary = "ntfs-3g";
1466 else if (TWFunc::Path_Exists("/sbin/mount.ntfs"))
1467 Ntfsmount_Binary = "mount.ntfs";
1468
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001469 if (Mount_Read_Only)
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001470 cmd = "/sbin/" + Ntfsmount_Binary + " -o ro " + Actual_Block_Device + " " + Mount_Point;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001471 else
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001472 cmd = "/sbin/" + Ntfsmount_Binary + " " + Actual_Block_Device + " " + Mount_Point;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001473 LOGINFO("cmd: '%s'\n", cmd.c_str());
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001474
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001475 if (TWFunc::Exec_Cmd(cmd) == 0) {
1476 return true;
1477 } else {
1478 LOGINFO("ntfs-3g failed to mount, trying regular mount method.\n");
1479 }
1480 }
1481
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001482 if (Mount_Read_Only)
1483 flags |= MS_RDONLY;
1484
Dees_Troy22042032012-12-18 21:23:08 +00001485 if (Fstab_File_System == "yaffs2") {
1486 // mount an MTD partition as a YAFFS2 filesystem.
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001487 flags = MS_NOATIME | MS_NODEV | MS_NODIRATIME;
1488 if (Mount_Read_Only)
1489 flags |= MS_RDONLY;
Dees_Troy76543db2013-06-19 16:24:30 +00001490 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags, NULL) < 0) {
1491 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags | MS_RDONLY, NULL) < 0) {
1492 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001493 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy76543db2013-06-19 16:24:30 +00001494 else
1495 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
1496 return false;
1497 } else {
1498 LOGINFO("Mounted '%s' (MTD) as RO\n", Mount_Point.c_str());
1499 return true;
1500 }
1501 } else {
1502 struct stat st;
1503 string test_path = Mount_Point;
1504 if (stat(test_path.c_str(), &st) < 0) {
1505 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001506 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy76543db2013-06-19 16:24:30 +00001507 else
1508 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
1509 return false;
1510 }
1511 mode_t new_mode = st.st_mode | S_IXUSR | S_IXGRP | S_IXOTH;
1512 if (new_mode != st.st_mode) {
1513 LOGINFO("Fixing execute permissions for %s\n", Mount_Point.c_str());
1514 if (chmod(Mount_Point.c_str(), new_mode) < 0) {
1515 if (Display_Error)
1516 LOGERR("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
1517 else
1518 LOGINFO("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
1519 return false;
1520 }
1521 }
Dees_Troy22042032012-12-18 21:23:08 +00001522 return true;
Dees_Troy76543db2013-06-19 16:24:30 +00001523 }
thatc7572eb2015-03-31 18:55:30 +02001524 }
1525
1526 string mount_fs = Current_File_System;
1527 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sys/module/texfat"))
1528 mount_fs = "texfat";
1529
1530 if (!exfat_mounted &&
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001531 mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), mount_fs.c_str(), flags, Mount_Options.c_str()) != 0 &&
1532 mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), mount_fs.c_str(), flags, NULL) != 0) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001533#ifdef TW_NO_EXFAT_FUSE
1534 if (Current_File_System == "exfat") {
Dees_Troy2673cec2013-04-02 20:22:16 +00001535 LOGINFO("Mounting exfat failed, trying vfat...\n");
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001536 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), "vfat", 0, NULL) != 0) {
Dees_Troy85f44ed2013-01-09 18:42:36 +00001537 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001538 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy85f44ed2013-01-09 18:42:36 +00001539 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001540 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001541 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 +00001542 return false;
Dees_Troy85f44ed2013-01-09 18:42:36 +00001543 }
Dees_Troyb05ddee2013-01-28 20:24:50 +00001544 } else {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001545#endif
bigbiff bigbiff26774a02014-03-29 18:22:00 -04001546 if (!Removable && Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001547 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001548 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001549 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
1550 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 +00001551 return false;
1552#ifdef TW_NO_EXFAT_FUSE
Dees_Troy85f44ed2013-01-09 18:42:36 +00001553 }
1554#endif
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001555 }
Ethan Yonker253368a2014-11-25 15:00:52 -06001556
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001557 if (Removable)
1558 Update_Size(Display_Error);
1559
xiaolu9416f4f2015-06-04 08:22:23 +08001560 if (!Symlink_Mount_Point.empty() && TWFunc::Path_Exists(Symlink_Path)) {
codelover2a3d4ce2015-03-14 20:26:49 +08001561 string Command = "mount -o bind '" + Symlink_Path + "' '" + Symlink_Mount_Point + "'";
Vojtech Bocek05534202013-09-11 08:11:56 +02001562 TWFunc::Exec_Cmd(Command);
Dees_Troy5bf43922012-09-07 16:07:55 -04001563 }
Chaosmasterda974802020-01-26 21:09:28 +01001564
1565 if (Mount_Point == "/system_root") {
1566 unlink("/system");
1567 mkdir("/system", 0755);
1568 mount("/system_root/system", "/system", "auto", MS_BIND, NULL);
1569 }
1570
Dees_Troy5bf43922012-09-07 16:07:55 -04001571 return true;
1572}
1573
1574bool TWPartition::UnMount(bool Display_Error) {
Chaosmasterda974802020-01-26 21:09:28 +01001575 if (Mount_Point == "/system_root") {
1576 if (umount("/system") == -1)
1577 umount2("/system", MNT_DETACH);
1578 rmdir("/system");
1579 symlink("/system_root/system", "/system");
1580 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001581 if (Is_Mounted()) {
1582 int never_unmount_system;
1583
1584 DataManager::GetValue(TW_DONT_UNMOUNT_SYSTEM, never_unmount_system);
Captain Throwback9d6feb52018-07-27 10:05:24 -04001585 if (never_unmount_system == 1 && Mount_Point == PartitionManager.Get_Android_Root_Path())
Dees_Troy5bf43922012-09-07 16:07:55 -04001586 return true; // Never unmount system if you're not supposed to unmount it
1587
Matt Mowera8e6d832017-02-14 20:20:59 -06001588 if (Is_Storage && MTP_Storage_ID > 0)
Ethan Yonker726a0202014-12-16 20:01:38 -06001589 PartitionManager.Remove_MTP_Storage(MTP_Storage_ID);
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001590
Dees_Troy38bd7602012-09-14 13:33:53 -04001591 if (!Symlink_Mount_Point.empty())
1592 umount(Symlink_Mount_Point.c_str());
1593
Dees_Troyb05ddee2013-01-28 20:24:50 +00001594 umount(Mount_Point.c_str());
1595 if (Is_Mounted()) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001596 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001597 gui_msg(Msg(msg::kError, "fail_unmount=Failed to unmount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy5bf43922012-09-07 16:07:55 -04001598 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001599 LOGINFO("Unable to unmount '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001600 return false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001601 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -04001602 return true;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001603 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001604 } else {
1605 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001606 }
1607}
1608
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05001609bool TWPartition::ReMount(bool Display_Error) {
1610 if (UnMount(Display_Error))
1611 return Mount(Display_Error);
1612 return false;
1613}
1614
1615bool TWPartition::ReMount_RW(bool Display_Error) {
1616 // No need to remount if already mounted rw
1617 if (Is_File_System_Writable())
1618 return true;
1619
1620 bool ro = Mount_Read_Only;
1621 int flags = Mount_Flags;
1622
1623 Mount_Read_Only = false;
1624 Mount_Flags &= ~MS_RDONLY;
1625
1626 bool ret = ReMount(Display_Error);
1627
1628 Mount_Read_Only = ro;
1629 Mount_Flags = flags;
1630
1631 return ret;
1632}
1633
Gary Peck43acadf2012-11-21 21:19:01 -08001634bool TWPartition::Wipe(string New_File_System) {
Ethan Yonker726a0202014-12-16 20:01:38 -06001635 bool wiped = false, update_crypt = false, recreate_media = true;
Dees_Troy16c2b312013-01-15 16:51:18 +00001636 int check;
1637 string Layout_Filename = Mount_Point + "/.layout_version";
bigbiffa2bd7b72020-05-07 21:45:34 -04001638 ext4_encryption_policy policy;
1639
1640 if (Mount_Point == "/data" && TWFunc::get_cache_dir() == AB_CACHE_DIR && Is_Decrypted) {
1641 TWFunc::Get_Encryption_Policy(policy, AB_CACHE_DIR);
1642 }
Dees_Troy16c2b312013-01-15 16:51:18 +00001643
Dees_Troy38bd7602012-09-14 13:33:53 -04001644 if (!Can_Be_Wiped) {
Matt Mower3c366972015-12-25 19:28:31 -06001645 gui_msg(Msg(msg::kError, "cannot_wipe=Partition {1} cannot be wiped.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04001646 return false;
1647 }
1648
Dees_Troyc51f1f92012-09-20 15:32:13 -04001649 if (Mount_Point == "/cache")
Dees_Troy2673cec2013-04-02 20:22:16 +00001650 Log_Offset = 0;
Dees_Troyc51f1f92012-09-20 15:32:13 -04001651
Dees_Troy16c2b312013-01-15 16:51:18 +00001652 if (Retain_Layout_Version && Mount(false) && TWFunc::Path_Exists(Layout_Filename))
1653 TWFunc::copy_file(Layout_Filename, "/.layout_version", 0600);
1654 else
1655 unlink("/.layout_version");
Dees_Troy38bd7602012-09-14 13:33:53 -04001656
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001657 if (Has_Data_Media && Current_File_System == New_File_System) {
Dees_Troy16c2b312013-01-15 16:51:18 +00001658 wiped = Wipe_Data_Without_Wiping_Media();
bigbiffa2bd7b72020-05-07 21:45:34 -04001659 if (Mount_Point == "/data" && TWFunc::get_cache_dir() == AB_CACHE_DIR) {
1660 bool created = Recreate_AB_Cache_Dir(policy);
1661 if (created)
1662 gui_msg(Msg(msg::kWarning, "fbe_wipe_msg=WARNING: {1} wiped. FBE device should be booted into Android and not Recovery to set initial FBE policy after wipe.")(TWFunc::get_cache_dir()));
1663 }
Ethan Yonker5eac2222014-06-11 12:22:55 -05001664 recreate_media = false;
Dees_Troy16c2b312013-01-15 16:51:18 +00001665 } else {
Dees_Troy16c2b312013-01-15 16:51:18 +00001666 DataManager::GetValue(TW_RM_RF_VAR, check);
1667
Hashcodedabfd492013-08-29 22:45:30 -07001668 if (check || Use_Rm_Rf)
Dees_Troy16c2b312013-01-15 16:51:18 +00001669 wiped = Wipe_RMRF();
1670 else if (New_File_System == "ext4")
1671 wiped = Wipe_EXT4();
1672 else if (New_File_System == "ext2" || New_File_System == "ext3")
dianlujitao4879b372018-12-03 18:45:47 +08001673 wiped = Wipe_EXTFS(New_File_System);
Dees_Troy16c2b312013-01-15 16:51:18 +00001674 else if (New_File_System == "vfat")
1675 wiped = Wipe_FAT();
1676 else if (New_File_System == "exfat")
1677 wiped = Wipe_EXFAT();
1678 else if (New_File_System == "yaffs2")
1679 wiped = Wipe_MTD();
Dees_Troye5017042013-08-29 16:38:55 +00001680 else if (New_File_System == "f2fs")
1681 wiped = Wipe_F2FS();
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001682 else if (New_File_System == "ntfs")
1683 wiped = Wipe_NTFS();
Dees_Troy16c2b312013-01-15 16:51:18 +00001684 else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001685 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 +00001686 unlink("/.layout_version");
1687 return false;
1688 }
1689 update_crypt = wiped;
Gary Pecke8bc5d72012-12-21 06:45:25 -08001690 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001691
Gary Pecke8bc5d72012-12-21 06:45:25 -08001692 if (wiped) {
Dees_Troy16c2b312013-01-15 16:51:18 +00001693 if (TWFunc::Path_Exists("/.layout_version") && Mount(false))
1694 TWFunc::copy_file("/.layout_version", Layout_Filename, 0600);
1695
1696 if (update_crypt) {
1697 Setup_File_System(false);
1698 if (Is_Encrypted && !Is_Decrypted) {
1699 // just wiped an encrypted partition back to its unencrypted state
1700 Is_Encrypted = false;
1701 Is_Decrypted = false;
1702 Decrypted_Block_Device = "";
1703 if (Mount_Point == "/data") {
1704 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1705 DataManager::SetValue(TW_IS_DECRYPTED, 0);
1706 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001707 }
1708 }
Ethan Yonker5eac2222014-06-11 12:22:55 -05001709
Ethan Yonker66a19492015-12-10 10:19:45 -06001710 if (Has_Data_Media && recreate_media) {
Ethan Yonker5eac2222014-06-11 12:22:55 -05001711 Recreate_Media_Folder();
1712 }
Matt Mower209c9632016-01-20 12:08:35 -06001713 if (Is_Storage && Mount(false))
1714 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Gary Pecke8bc5d72012-12-21 06:45:25 -08001715 }
Matt Mower209c9632016-01-20 12:08:35 -06001716
Gary Pecke8bc5d72012-12-21 06:45:25 -08001717 return wiped;
Dees_Troy51a0e822012-09-05 15:24:24 -04001718}
1719
Gary Peck43acadf2012-11-21 21:19:01 -08001720bool TWPartition::Wipe() {
Gary Peck82599a82012-11-21 16:23:12 -08001721 if (Is_File_System(Current_File_System))
1722 return Wipe(Current_File_System);
1723 else
1724 return Wipe(Fstab_File_System);
Gary Peck43acadf2012-11-21 21:19:01 -08001725}
1726
Dees_Troye58d5262012-09-21 12:27:57 -04001727bool TWPartition::Wipe_AndSec(void) {
1728 if (!Has_Android_Secure)
1729 return false;
1730
Dees_Troye58d5262012-09-21 12:27:57 -04001731 if (!Mount(true))
1732 return false;
1733
Ethan Yonker74db1572015-10-28 12:44:49 -05001734 gui_msg(Msg("wiping=Wiping {1}")(Backup_Display_Name));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001735 TWFunc::removeDir(Mount_Point + "/.android_secure/", true);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001736 return true;
Dees_Troye58d5262012-09-21 12:27:57 -04001737}
1738
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001739bool TWPartition::Can_Repair() {
Ethan Yonkera2719152015-05-28 09:44:41 -05001740 if (Mount_Read_Only)
1741 return false;
Matt Mower18794c82015-11-11 16:22:45 -06001742 if (Current_File_System == "vfat" && TWFunc::Path_Exists("/sbin/fsck.fat"))
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001743 return true;
1744 else if ((Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") && TWFunc::Path_Exists("/sbin/e2fsck"))
1745 return true;
1746 else if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/fsck.exfat"))
1747 return true;
1748 else if (Current_File_System == "f2fs" && TWFunc::Path_Exists("/sbin/fsck.f2fs"))
1749 return true;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001750 else if (Current_File_System == "ntfs" && (TWFunc::Path_Exists("/sbin/ntfsfix") || TWFunc::Path_Exists("/sbin/fsck.ntfs")))
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001751 return true;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001752 return false;
1753}
1754
1755bool TWPartition::Repair() {
1756 string command;
1757
1758 if (Current_File_System == "vfat") {
Matt Mower18794c82015-11-11 16:22:45 -06001759 if (!TWFunc::Path_Exists("/sbin/fsck.fat")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001760 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.fat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001761 return false;
1762 }
1763 if (!UnMount(true))
1764 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001765 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.fat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001766 Find_Actual_Block_Device();
Matt Mower18794c82015-11-11 16:22:45 -06001767 command = "/sbin/fsck.fat -y " + Actual_Block_Device;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001768 LOGINFO("Repair command: %s\n", command.c_str());
1769 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001770 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001771 return true;
1772 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001773 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001774 return false;
1775 }
1776 }
1777 if (Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") {
1778 if (!TWFunc::Path_Exists("/sbin/e2fsck")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001779 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("e2fsck"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001780 return false;
1781 }
1782 if (!UnMount(true))
1783 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001784 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("e2fsck"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001785 Find_Actual_Block_Device();
Ethan Yonkera2719152015-05-28 09:44:41 -05001786 command = "/sbin/e2fsck -fp " + Actual_Block_Device;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001787 LOGINFO("Repair command: %s\n", command.c_str());
1788 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001789 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001790 return true;
1791 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001792 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001793 return false;
1794 }
1795 }
1796 if (Current_File_System == "exfat") {
1797 if (!TWFunc::Path_Exists("/sbin/fsck.exfat")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001798 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.exfat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001799 return false;
1800 }
1801 if (!UnMount(true))
1802 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001803 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.exfat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001804 Find_Actual_Block_Device();
1805 command = "/sbin/fsck.exfat " + Actual_Block_Device;
1806 LOGINFO("Repair command: %s\n", command.c_str());
1807 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001808 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001809 return true;
1810 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001811 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001812 return false;
1813 }
1814 }
1815 if (Current_File_System == "f2fs") {
1816 if (!TWFunc::Path_Exists("/sbin/fsck.f2fs")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001817 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.f2fs"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001818 return false;
1819 }
1820 if (!UnMount(true))
1821 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001822 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.f2fs"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001823 Find_Actual_Block_Device();
1824 command = "/sbin/fsck.f2fs " + Actual_Block_Device;
1825 LOGINFO("Repair command: %s\n", command.c_str());
1826 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001827 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001828 return true;
1829 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001830 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001831 return false;
1832 }
1833 }
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001834 if (Current_File_System == "ntfs") {
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001835 string Ntfsfix_Binary;
1836 if (TWFunc::Path_Exists("/sbin/ntfsfix"))
1837 Ntfsfix_Binary = "ntfsfix";
1838 else if (TWFunc::Path_Exists("/sbin/fsck.ntfs"))
1839 Ntfsfix_Binary = "fsck.ntfs";
1840 else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001841 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("ntfsfix"));
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001842 return false;
1843 }
1844 if (!UnMount(true))
1845 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001846 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)(Ntfsfix_Binary));
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001847 Find_Actual_Block_Device();
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001848 command = "/sbin/" + Ntfsfix_Binary + " " + Actual_Block_Device;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001849 LOGINFO("Repair command: %s\n", command.c_str());
1850 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001851 gui_msg("done=Done.");
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001852 return true;
1853 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001854 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001855 return false;
1856 }
1857 }
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001858 return false;
1859}
1860
Ethan Yonkera2719152015-05-28 09:44:41 -05001861bool TWPartition::Can_Resize() {
1862 if (Mount_Read_Only)
1863 return false;
1864 if ((Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") && TWFunc::Path_Exists("/sbin/resize2fs"))
1865 return true;
1866 return false;
1867}
1868
1869bool TWPartition::Resize() {
1870 string command;
1871
1872 if (Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") {
1873 if (!Can_Repair()) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001874 LOGINFO("Cannot resize %s because %s cannot be repaired before resizing.\n", Display_Name.c_str(), Display_Name.c_str());
1875 gui_msg(Msg(msg::kError, "cannot_resize=Cannot resize {1}.")(Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001876 return false;
1877 }
1878 if (!TWFunc::Path_Exists("/sbin/resize2fs")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001879 LOGINFO("resize2fs does not exist! Cannot resize!\n");
1880 gui_msg(Msg(msg::kError, "cannot_resize=Cannot resize {1}.")(Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001881 return false;
1882 }
1883 // Repair will unmount so no need to do it twice
Ethan Yonker74db1572015-10-28 12:44:49 -05001884 gui_msg(Msg("repair_resize=Repairing {1} before resizing.")( Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001885 if (!Repair())
1886 return false;
Ethan Yonker74db1572015-10-28 12:44:49 -05001887 gui_msg(Msg("resizing=Resizing {1} using {2}...")(Display_Name)("resize2fs"));
Ethan Yonkera2719152015-05-28 09:44:41 -05001888 Find_Actual_Block_Device();
1889 command = "/sbin/resize2fs " + Actual_Block_Device;
1890 if (Length != 0) {
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001891 unsigned long long Actual_Size = IOCTL_Get_Block_Size();
1892 if (Actual_Size == 0)
1893 return false;
Ethan Yonkera2719152015-05-28 09:44:41 -05001894
Ethan Yonkera2719152015-05-28 09:44:41 -05001895 unsigned long long Block_Count;
1896 if (Length < 0) {
1897 // Reduce overall size by this length
1898 Block_Count = (Actual_Size / 1024LLU) - ((unsigned long long)(Length * -1) / 1024LLU);
1899 } else {
1900 // This is the size, not a size reduction
1901 Block_Count = ((unsigned long long)(Length) / 1024LLU);
1902 }
1903 char temp[256];
1904 sprintf(temp, "%llu", Block_Count);
1905 command += " ";
1906 command += temp;
1907 command += "K";
1908 }
1909 LOGINFO("Resize command: %s\n", command.c_str());
1910 if (TWFunc::Exec_Cmd(command) == 0) {
1911 Update_Size(true);
Ethan Yonker74db1572015-10-28 12:44:49 -05001912 gui_msg("done=Done.");
Ethan Yonkera2719152015-05-28 09:44:41 -05001913 return true;
1914 } else {
1915 Update_Size(true);
Ethan Yonker74db1572015-10-28 12:44:49 -05001916 gui_msg(Msg(msg::kError, "unable_resize=Unable to resize {1}.")(Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001917 return false;
1918 }
1919 }
1920 return false;
1921}
1922
bigbiffce8f83c2015-12-12 18:30:21 -05001923bool TWPartition::Backup(PartitionSettings *part_settings, pid_t *tar_fork_pid) {
1924 if (Backup_Method == BM_FILES)
1925 return Backup_Tar(part_settings, tar_fork_pid);
1926 else if (Backup_Method == BM_DD)
1927 return Backup_Image(part_settings);
1928 else if (Backup_Method == BM_FLASH_UTILS)
1929 return Backup_Dump_Image(part_settings);
Dees_Troy2673cec2013-04-02 20:22:16 +00001930 LOGERR("Unknown backup method for '%s'\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001931 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001932}
1933
bigbiffce8f83c2015-12-12 18:30:21 -05001934bool TWPartition::Restore(PartitionSettings *part_settings) {
Matt Mower3c366972015-12-25 19:28:31 -06001935 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, gui_parse_text("{@restoring_hdr}"));
Ethan Yonkere080c1f2016-09-19 13:50:25 -05001936 LOGINFO("Restore filename is: %s/%s\n", part_settings->Backup_Folder.c_str(), Backup_FileName.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001937
bigbiffce8f83c2015-12-12 18:30:21 -05001938 string Restore_File_System = Get_Restore_File_System(part_settings);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001939
1940 if (Is_File_System(Restore_File_System))
bigbiffce8f83c2015-12-12 18:30:21 -05001941 return Restore_Tar(part_settings);
1942 else if (Is_Image(Restore_File_System))
1943 return Restore_Image(part_settings);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001944
1945 LOGERR("Unknown restore method for '%s'\n", Mount_Point.c_str());
1946 return false;
1947}
1948
bigbiffce8f83c2015-12-12 18:30:21 -05001949string TWPartition::Get_Restore_File_System(PartitionSettings *part_settings) {
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001950 size_t first_period, second_period;
1951 string Restore_File_System;
1952
Gary Peck43acadf2012-11-21 21:19:01 -08001953 // Parse backup filename to extract the file system before wiping
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05001954 first_period = Backup_FileName.find(".");
Gary Peck43acadf2012-11-21 21:19:01 -08001955 if (first_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001956 LOGERR("Unable to find file system (first period).\n");
thatd43bf2d2014-09-21 23:13:02 +02001957 return string();
Gary Peck43acadf2012-11-21 21:19:01 -08001958 }
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05001959 Restore_File_System = Backup_FileName.substr(first_period + 1, Backup_FileName.size() - first_period - 1);
Gary Peck43acadf2012-11-21 21:19:01 -08001960 second_period = Restore_File_System.find(".");
1961 if (second_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001962 LOGERR("Unable to find file system (second period).\n");
thatd43bf2d2014-09-21 23:13:02 +02001963 return string();
Gary Peck43acadf2012-11-21 21:19:01 -08001964 }
1965 Restore_File_System.resize(second_period);
Matt Mowera0cd91d2016-12-30 18:21:42 -06001966 LOGINFO("Restore file system is: '%s'.\n", Restore_File_System.c_str());
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001967 return Restore_File_System;
Dees_Troy51a0e822012-09-05 15:24:24 -04001968}
1969
1970string TWPartition::Backup_Method_By_Name() {
bigbiffce8f83c2015-12-12 18:30:21 -05001971 if (Backup_Method == BM_NONE)
Dees_Troy38bd7602012-09-14 13:33:53 -04001972 return "none";
bigbiffce8f83c2015-12-12 18:30:21 -05001973 else if (Backup_Method == BM_FILES)
Dees_Troy38bd7602012-09-14 13:33:53 -04001974 return "files";
bigbiffce8f83c2015-12-12 18:30:21 -05001975 else if (Backup_Method == BM_DD)
Dees_Troy38bd7602012-09-14 13:33:53 -04001976 return "dd";
bigbiffce8f83c2015-12-12 18:30:21 -05001977 else if (Backup_Method == BM_FLASH_UTILS)
Dees_Troy38bd7602012-09-14 13:33:53 -04001978 return "flash_utils";
1979 else
1980 return "undefined";
1981 return "ERROR!";
Dees_Troy51a0e822012-09-05 15:24:24 -04001982}
1983
1984bool TWPartition::Decrypt(string Password) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001985 LOGINFO("STUB TWPartition::Decrypt, password: '%s'\n", Password.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001986 // Is this needed?
Dees_Troy51a0e822012-09-05 15:24:24 -04001987 return 1;
1988}
1989
1990bool TWPartition::Wipe_Encryption() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001991 bool Save_Data_Media = Has_Data_Media;
Ethan Yonker93382822018-11-01 15:25:31 -05001992 bool ret = false;
1993 BasePartition* base_partition = make_partition();
1994
1995 if (!base_partition->PreWipeEncryption())
1996 goto exit;
Dees_Troy38bd7602012-09-14 13:33:53 -04001997
dianlujitao4879b372018-12-03 18:45:47 +08001998 Find_Actual_Block_Device();
1999 if (!Is_Present) {
2000 LOGINFO("Block device not present, cannot format %s.\n", Display_Name.c_str());
2001 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
2002 return false;
2003 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002004
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06002005#ifdef TW_INCLUDE_CRYPTO
bigbiffa2bd7b72020-05-07 21:45:34 -04002006 if (!UnMount(true))
2007 return false;
Ethan Yonker1b190162016-12-05 15:25:19 -06002008 if (Is_Decrypted && !Decrypted_Block_Device.empty()) {
Matt Mower2b18a532015-02-20 16:58:05 -06002009 if (delete_crypto_blk_dev((char*)("userdata")) != 0) {
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06002010 LOGERR("Error deleting crypto block device, continuing anyway.\n");
2011 }
2012 }
2013#endif
dianlujitao4879b372018-12-03 18:45:47 +08002014 Has_Data_Media = false;
Ethan Yonker93382822018-11-01 15:25:31 -05002015 Decrypted_Block_Device = "";
Dees_Troy74fb2e92013-04-15 14:35:47 +00002016 Is_Decrypted = false;
2017 Is_Encrypted = false;
Gary Pecke8bc5d72012-12-21 06:45:25 -08002018 if (Wipe(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002019 Has_Data_Media = Save_Data_Media;
2020 if (Has_Data_Media && !Symlink_Mount_Point.empty()) {
2021 Recreate_Media_Folder();
Ethan Yonker726a0202014-12-16 20:01:38 -06002022 if (Mount(false))
2023 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04002024 }
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06002025 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
Ethan Yonker83e82572014-04-04 10:59:28 -05002026#ifndef TW_OEM_BUILD
bigbiffa2bd7b72020-05-07 21:45:34 -04002027 if (Is_FBE)
2028 gui_msg(Msg(msg::kWarning, "fbe_wipe_msg=WARNING: {1} wiped. FBE device should be booted into Android and not Recovery to set initial FBE policy after wipe.")(TWFunc::get_cache_dir()));
2029 else
2030 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 -05002031#endif
Ethan Yonker93382822018-11-01 15:25:31 -05002032 ret = true;
2033 if (!Key_Directory.empty())
2034 ret = PartitionManager.Wipe_By_Path(Key_Directory);
2035 if (ret)
2036 ret = base_partition->PostWipeEncryption();
2037 goto exit;
Dees_Troy38bd7602012-09-14 13:33:53 -04002038 } else {
2039 Has_Data_Media = Save_Data_Media;
Ethan Yonker74db1572015-10-28 12:44:49 -05002040 gui_err("format_data_err=Unable to format to remove encryption.");
Ethan Yonker726a0202014-12-16 20:01:38 -06002041 if (Has_Data_Media && Mount(false))
2042 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Ethan Yonker93382822018-11-01 15:25:31 -05002043 goto exit;
Dees_Troy38bd7602012-09-14 13:33:53 -04002044 }
Ethan Yonker93382822018-11-01 15:25:31 -05002045exit:
2046 delete base_partition;
2047 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04002048}
2049
2050void TWPartition::Check_FS_Type() {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05002051 const char* type;
2052 blkid_probe pr;
Dees_Troy5bf43922012-09-07 16:07:55 -04002053
Dees_Troy68cab492012-12-12 19:29:35 +00002054 if (Fstab_File_System == "yaffs2" || Fstab_File_System == "mtd" || Fstab_File_System == "bml" || Ignore_Blkid)
2055 return; // Running blkid on some mtd devices causes a massive crash or needs to be skipped
Dees_Troy5bf43922012-09-07 16:07:55 -04002056
Dees_Troy38bd7602012-09-14 13:33:53 -04002057 Find_Actual_Block_Device();
Dees_Troy8170a922012-09-18 15:40:25 -04002058 if (!Is_Present)
2059 return;
Dees_Troy51127312012-09-08 13:08:49 -04002060
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05002061 pr = blkid_new_probe_from_filename(Actual_Block_Device.c_str());
2062 if (blkid_do_fullprobe(pr)) {
2063 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00002064 LOGINFO("Can't probe device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05002065 return;
Dees_Troy5bf43922012-09-07 16:07:55 -04002066 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02002067
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05002068 if (blkid_probe_lookup_value(pr, "TYPE", &type, NULL) < 0) {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05002069 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00002070 LOGINFO("can't find filesystem on device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05002071 return;
2072 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02002073
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05002074 Current_File_System = type;
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02002075 blkid_free_probe(pr);
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06002076 if (fs_flags.size() > 1) {
2077 std::vector<partition_fs_flags_struct>::iterator iter;
2078 std::vector<partition_fs_flags_struct>::iterator found = fs_flags.begin();
2079
2080 for (iter = fs_flags.begin(); iter != fs_flags.end(); iter++) {
2081 if (iter->File_System == Current_File_System) {
2082 found = iter;
2083 break;
2084 }
2085 }
2086 // If we don't find a match, we default the flags to the first set of flags that we received from the fstab
2087 if (Mount_Flags != found->Mount_Flags || Mount_Options != found->Mount_Options) {
2088 Mount_Flags = found->Mount_Flags;
2089 Mount_Options = found->Mount_Options;
2090 LOGINFO("Mount_Flags: %i, Mount_Options: %s\n", Mount_Flags, Mount_Options.c_str());
2091 }
2092 }
Dees_Troy51a0e822012-09-05 15:24:24 -04002093}
2094
dianlujitao4879b372018-12-03 18:45:47 +08002095bool TWPartition::Wipe_EXTFS(string File_System) {
bigbiffa2bd7b72020-05-07 21:45:34 -04002096 if (!UnMount(true))
2097 return false;
2098
dianlujitao4879b372018-12-03 18:45:47 +08002099#if PLATFORM_SDK_VERSION < 28
2100 if (!TWFunc::Path_Exists("/sbin/mke2fs"))
2101#else
2102 if (!TWFunc::Path_Exists("/sbin/mke2fs") || !TWFunc::Path_Exists("/sbin/e2fsdroid"))
2103#endif
Dees_Troy38bd7602012-09-14 13:33:53 -04002104 return Wipe_RMRF();
2105
dianlujitao4879b372018-12-03 18:45:47 +08002106 int ret;
2107 bool NeedPreserveFooter = true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002108
Ethan Yonker25f20c12014-10-14 09:04:43 -05002109 Find_Actual_Block_Device();
2110 if (!Is_Present) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002111 LOGINFO("Block device not present, cannot wipe %s.\n", Display_Name.c_str());
2112 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Ethan Yonker25f20c12014-10-14 09:04:43 -05002113 return false;
2114 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002115 if (!UnMount(true))
2116 return false;
2117
dianlujitao4879b372018-12-03 18:45:47 +08002118 /**
2119 * On decrypted devices, IOCTL_Get_Block_Size calculates size on device mapper,
2120 * so there's no need to preserve footer.
2121 */
2122 if ((Is_Decrypted && !Decrypted_Block_Device.empty()) ||
2123 Crypto_Key_Location != "footer") {
2124 NeedPreserveFooter = false;
2125 }
2126
2127 unsigned long long dev_sz = TWFunc::IOCTL_Get_Block_Size(Actual_Block_Device.c_str());
2128 if (!dev_sz)
2129 return false;
2130
2131 if (NeedPreserveFooter)
2132 Length < 0 ? dev_sz += Length : dev_sz -= CRYPT_FOOTER_OFFSET;
2133
Dees Troy9a4d7402019-03-21 14:17:28 -04002134 char dout[16];
2135 sprintf(dout, "%llu", dev_sz / 4096);
2136
2137 //string size_str =to_string(dev_sz / 4096);
2138 string size_str = dout;
dianlujitao4879b372018-12-03 18:45:47 +08002139 string Command;
2140
2141 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mke2fs"));
2142
2143 // Execute mke2fs to create empty ext4 filesystem
2144 Command = "mke2fs -t " + File_System + " -b 4096 " + Actual_Block_Device + " " + size_str;
2145 LOGINFO("mke2fs command: %s\n", Command.c_str());
2146 ret = TWFunc::Exec_Cmd(Command);
2147 if (ret) {
2148 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
2149 return false;
2150 }
2151
2152 if (TWFunc::Path_Exists("/sbin/e2fsdroid")) {
dianlujitao3cf05a52019-05-15 15:33:27 +08002153 const string& File_Contexts_Entry = (Mount_Point == "/system_root" ? "/" : Mount_Point);
2154 char *secontext = NULL;
2155 if (!selinux_handle || selabel_lookup(selinux_handle, &secontext, File_Contexts_Entry.c_str(), S_IFDIR) < 0) {
2156 LOGINFO("Cannot lookup security context for '%s'\n", Mount_Point.c_str());
2157 } else {
2158 // Execute e2fsdroid to initialize selinux context
2159 Command = "e2fsdroid -e -S /file_contexts -a " + File_Contexts_Entry + " " + Actual_Block_Device;
2160 LOGINFO("e2fsdroid command: %s\n", Command.c_str());
2161 ret = TWFunc::Exec_Cmd(Command);
2162 if (ret) {
2163 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
2164 return false;
2165 }
dianlujitao4879b372018-12-03 18:45:47 +08002166 }
2167 } else {
2168 LOGINFO("e2fsdroid not present\n");
2169 }
2170
2171 if (NeedPreserveFooter)
2172 Wipe_Crypto_Key();
2173 Current_File_System = File_System;
2174 Recreate_AndSec_Folder();
2175 gui_msg("done=Done.");
2176 return true;
2177}
2178
2179bool TWPartition::Wipe_EXT4() {
2180#ifdef USE_EXT4
Ethan Yonkerf27497f2014-02-09 11:48:33 -06002181 int ret;
dianlujitao4879b372018-12-03 18:45:47 +08002182 bool NeedPreserveFooter = true;
2183
2184 Find_Actual_Block_Device();
2185 if (!Is_Present) {
2186 LOGINFO("Block device not present, cannot wipe %s.\n", Display_Name.c_str());
2187 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
2188 return false;
2189 }
2190 if (!UnMount(true))
2191 return false;
2192
2193 /**
2194 * On decrypted devices, IOCTL_Get_Block_Size calculates size on device mapper,
2195 * so there's no need to preserve footer.
2196 */
2197 if ((Is_Decrypted && !Decrypted_Block_Device.empty()) ||
2198 Crypto_Key_Location != "footer") {
2199 NeedPreserveFooter = false;
2200 }
2201
2202 unsigned long long dev_sz = TWFunc::IOCTL_Get_Block_Size(Actual_Block_Device.c_str());
2203 if (!dev_sz)
2204 return false;
2205
2206 if (NeedPreserveFooter)
2207 Length < 0 ? dev_sz += Length : dev_sz -= CRYPT_FOOTER_OFFSET;
2208
Ethan Yonkerf27497f2014-02-09 11:48:33 -06002209 char *secontext = NULL;
2210
Greg Wallace4b44fef2015-12-29 15:33:24 -05002211 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("make_ext4fs"));
Ethan Yonkerf27497f2014-02-09 11:48:33 -06002212
Dees Troy99c8dbf2014-03-10 16:53:58 +00002213 if (!selinux_handle || selabel_lookup(selinux_handle, &secontext, Mount_Point.c_str(), S_IFDIR) < 0) {
Ethan Yonkerf27497f2014-02-09 11:48:33 -06002214 LOGINFO("Cannot lookup security context for '%s'\n", Mount_Point.c_str());
dianlujitao4879b372018-12-03 18:45:47 +08002215 ret = make_ext4fs(Actual_Block_Device.c_str(), dev_sz, Mount_Point.c_str(), NULL);
Ethan Yonkerf27497f2014-02-09 11:48:33 -06002216 } else {
dianlujitao4879b372018-12-03 18:45:47 +08002217 ret = make_ext4fs(Actual_Block_Device.c_str(), dev_sz, Mount_Point.c_str(), selinux_handle);
Ethan Yonkerf27497f2014-02-09 11:48:33 -06002218 }
2219 if (ret != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002220 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troya95f55c2013-08-17 13:14:43 +00002221 return false;
2222 } else {
dianlujitao4879b372018-12-03 18:45:47 +08002223 if (NeedPreserveFooter)
2224 Wipe_Crypto_Key();
bigbiff bigbiffc49d7062013-10-11 20:28:00 -04002225 string sedir = Mount_Point + "/lost+found";
2226 PartitionManager.Mount_By_Path(sedir.c_str(), true);
2227 rmdir(sedir.c_str());
2228 mkdir(sedir.c_str(), S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP);
Dees_Troya95f55c2013-08-17 13:14:43 +00002229 return true;
2230 }
2231#else
dianlujitao4879b372018-12-03 18:45:47 +08002232 return Wipe_EXTFS("ext4");
Dees_Troya95f55c2013-08-17 13:14:43 +00002233#endif
Dees_Troy51a0e822012-09-05 15:24:24 -04002234}
2235
2236bool TWPartition::Wipe_FAT() {
Vojtech Bocek05534202013-09-11 08:11:56 +02002237 string command;
bigbiffa2bd7b72020-05-07 21:45:34 -04002238 if (!UnMount(true))
2239 return false;
Dees_Troy38bd7602012-09-14 13:33:53 -04002240
Matt Mower18794c82015-11-11 16:22:45 -06002241 if (TWFunc::Path_Exists("/sbin/mkfs.fat")) {
Greg Wallace4b44fef2015-12-29 15:33:24 -05002242 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mkfs.fat"));
Dees_Troy38bd7602012-09-14 13:33:53 -04002243 Find_Actual_Block_Device();
Matt Mower18794c82015-11-11 16:22:45 -06002244 command = "mkfs.fat " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02002245 if (TWFunc::Exec_Cmd(command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08002246 Current_File_System = "vfat";
Dees_Troye58d5262012-09-21 12:27:57 -04002247 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05002248 gui_msg("done=Done.");
Dees_Troy38bd7602012-09-14 13:33:53 -04002249 return true;
2250 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002251 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04002252 return false;
2253 }
2254 return true;
2255 }
2256 else
2257 return Wipe_RMRF();
2258
2259 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04002260}
2261
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002262bool TWPartition::Wipe_EXFAT() {
Vojtech Bocek05534202013-09-11 08:11:56 +02002263 string command;
bigbiffa2bd7b72020-05-07 21:45:34 -04002264 if (!UnMount(true))
2265 return false;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002266
2267 if (TWFunc::Path_Exists("/sbin/mkexfatfs")) {
Greg Wallace4b44fef2015-12-29 15:33:24 -05002268 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mkexfatfs"));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002269 Find_Actual_Block_Device();
2270 command = "mkexfatfs " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02002271 if (TWFunc::Exec_Cmd(command) == 0) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002272 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05002273 gui_msg("done=Done.");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002274 return true;
2275 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002276 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002277 return false;
2278 }
2279 return true;
2280 }
2281 return false;
2282}
2283
Dees_Troy38bd7602012-09-14 13:33:53 -04002284bool TWPartition::Wipe_MTD() {
2285 if (!UnMount(true))
2286 return false;
2287
Greg Wallace4b44fef2015-12-29 15:33:24 -05002288 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("MTD"));
Dees_Troy38bd7602012-09-14 13:33:53 -04002289
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02002290 mtd_scan_partitions();
2291 const MtdPartition* mtd = mtd_find_partition_by_name(MTD_Name.c_str());
2292 if (mtd == NULL) {
2293 LOGERR("No mtd partition named '%s'", MTD_Name.c_str());
2294 return false;
2295 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002296
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02002297 MtdWriteContext* ctx = mtd_write_partition(mtd);
2298 if (ctx == NULL) {
2299 LOGERR("Can't write '%s', failed to format.", MTD_Name.c_str());
2300 return false;
2301 }
2302 if (mtd_erase_blocks(ctx, -1) == -1) {
2303 mtd_write_close(ctx);
2304 LOGERR("Failed to format '%s'", MTD_Name.c_str());
2305 return false;
2306 }
2307 if (mtd_write_close(ctx) != 0) {
2308 LOGERR("Failed to close '%s'", MTD_Name.c_str());
2309 return false;
2310 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08002311 Current_File_System = "yaffs2";
Dees_Troye58d5262012-09-21 12:27:57 -04002312 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05002313 gui_msg("done=Done.");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02002314 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002315}
2316
2317bool TWPartition::Wipe_RMRF() {
Dees_Troy38bd7602012-09-14 13:33:53 -04002318 if (!Mount(true))
2319 return false;
Ethan Yonker726a0202014-12-16 20:01:38 -06002320 // This is the only wipe that leaves the partition mounted, so we
2321 // must manually remove the partition from MTP if it is a storage
2322 // partition.
2323 if (Is_Storage)
2324 PartitionManager.Remove_MTP_Storage(MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04002325
Ethan Yonker74db1572015-10-28 12:44:49 -05002326 gui_msg(Msg("remove_all=Removing all files under '{1}'")(Mount_Point));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002327 TWFunc::removeDir(Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04002328 Recreate_AndSec_Folder();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002329 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002330}
2331
Dees_Troye5017042013-08-29 16:38:55 +00002332bool TWPartition::Wipe_F2FS() {
Vojtech Bocek05534202013-09-11 08:11:56 +02002333 string command;
bigbiffa2bd7b72020-05-07 21:45:34 -04002334 if (!UnMount(true))
2335 return false;
Dees_Troye5017042013-08-29 16:38:55 +00002336
2337 if (TWFunc::Path_Exists("/sbin/mkfs.f2fs")) {
dianlujitao4879b372018-12-03 18:45:47 +08002338 bool NeedPreserveFooter = true;
2339
2340 Find_Actual_Block_Device();
2341 if (!Is_Present) {
2342 LOGINFO("Block device not present, cannot wipe %s.\n", Display_Name.c_str());
2343 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
2344 return false;
2345 }
Dees_Troye5017042013-08-29 16:38:55 +00002346
dianlujitao4879b372018-12-03 18:45:47 +08002347 /**
2348 * On decrypted devices, IOCTL_Get_Block_Size calculates size on device mapper,
2349 * so there's no need to preserve footer.
2350 */
2351 if ((Is_Decrypted && !Decrypted_Block_Device.empty()) ||
2352 Crypto_Key_Location != "footer") {
2353 NeedPreserveFooter = false;
dhacker29a3fa75f2015-01-17 19:42:33 -05002354 }
dianlujitao4879b372018-12-03 18:45:47 +08002355
dianlujitao4879b372018-12-03 18:45:47 +08002356 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mkfs.f2fs"));
Ethan Yonker7e941582019-03-22 08:18:21 -05002357 // First determine if we have the old mkfs.f2fs that uses "-r reserved_bytes"
2358 // or the new mkfs.f2fs that expects the number of sectors as the optional last argument
2359 // Note: some 7.1 trees have the old and some have the new.
2360 command = "mkfs.f2fs | grep \"reserved\" > /tmp/f2fsversiontest";
2361 TWFunc::Exec_Cmd(command, false); // no help argument so printing usage exits with an error code
2362 if (!TWFunc::Path_Exists("/tmp/f2fsversiontest")) {
2363 LOGINFO("Error determining mkfs.f2fs version\n");
2364 return false;
2365 }
2366 if (TWFunc::Get_File_Size("/tmp/f2fsversiontest") <= 0) {
2367 LOGINFO("Using newer mkfs.f2fs\n");
2368 unsigned long long dev_sz = TWFunc::IOCTL_Get_Block_Size(Actual_Block_Device.c_str());
2369 if (!dev_sz)
2370 return false;
2371
2372 if (NeedPreserveFooter)
2373 Length < 0 ? dev_sz += Length : dev_sz -= CRYPT_FOOTER_OFFSET;
2374
2375 char dev_sz_str[48];
2376 sprintf(dev_sz_str, "%llu", (dev_sz / 4096));
2377 command = "mkfs.f2fs -d1 -f -O encrypt -O quota -O verity -w 4096 " + Actual_Block_Device + " " + dev_sz_str;
2378 if (TWFunc::Path_Exists("/sbin/sload.f2fs")) {
2379 command += " && sload.f2fs -t /data " + Actual_Block_Device;
2380 }
2381 } else {
2382 LOGINFO("Using older mkfs.f2fs\n");
2383 command = "mkfs.f2fs -t 0";
2384 if (NeedPreserveFooter) {
2385 // Only use length if we're not decrypted
2386 char len[32];
2387 int mod_length = Length;
2388 if (Length < 0)
2389 mod_length *= -1;
2390 sprintf(len, "%i", mod_length);
2391 command += " -r ";
2392 command += len;
2393 }
2394 command += " " + Actual_Block_Device;
dianlujitao4879b372018-12-03 18:45:47 +08002395 }
2396 LOGINFO("mkfs.f2fs command: %s\n", command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02002397 if (TWFunc::Exec_Cmd(command) == 0) {
dianlujitao4879b372018-12-03 18:45:47 +08002398 if (NeedPreserveFooter)
2399 Wipe_Crypto_Key();
Dees_Troye5017042013-08-29 16:38:55 +00002400 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05002401 gui_msg("done=Done.");
Dees_Troye5017042013-08-29 16:38:55 +00002402 return true;
2403 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002404 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troye5017042013-08-29 16:38:55 +00002405 return false;
2406 }
2407 return true;
2408 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002409 LOGINFO("mkfs.f2fs binary not found, using rm -rf to wipe.\n");
Dees_Troye5017042013-08-29 16:38:55 +00002410 return Wipe_RMRF();
2411 }
2412 return false;
2413}
2414
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002415bool TWPartition::Wipe_NTFS() {
2416 string command;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002417 string Ntfsmake_Binary;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002418
bigbiffa2bd7b72020-05-07 21:45:34 -04002419 if (!UnMount(true))
2420 return false;
2421
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002422 if (TWFunc::Path_Exists("/sbin/mkntfs"))
2423 Ntfsmake_Binary = "mkntfs";
2424 else if (TWFunc::Path_Exists("/sbin/mkfs.ntfs"))
2425 Ntfsmake_Binary = "mkfs.ntfs";
2426 else
2427 return false;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002428
Greg Wallace4b44fef2015-12-29 15:33:24 -05002429 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)(Ntfsmake_Binary));
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002430 Find_Actual_Block_Device();
2431 command = "/sbin/" + Ntfsmake_Binary + " " + Actual_Block_Device;
2432 if (TWFunc::Exec_Cmd(command) == 0) {
2433 Recreate_AndSec_Folder();
2434 gui_msg("done=Done.");
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002435 return true;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002436 } else {
2437 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
2438 return false;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002439 }
2440 return false;
2441}
2442
Dees_Troy51a0e822012-09-05 15:24:24 -04002443bool TWPartition::Wipe_Data_Without_Wiping_Media() {
Ethan Yonker83e82572014-04-04 10:59:28 -05002444#ifdef TW_OEM_BUILD
2445 // In an OEM Build we want to do a full format
2446 return Wipe_Encryption();
2447#else
Ethan Yonker66a19492015-12-10 10:19:45 -06002448 bool ret = false;
Dees_Troy38bd7602012-09-14 13:33:53 -04002449
Dees_Troy38bd7602012-09-14 13:33:53 -04002450 if (!Mount(true))
2451 return false;
Ethan Yonker74db1572015-10-28 12:44:49 -05002452 gui_msg("wiping_data=Wiping data without wiping /data/media ...");
Ethan Yonker66a19492015-12-10 10:19:45 -06002453 ret = Wipe_Data_Without_Wiping_Media_Func(Mount_Point + "/");
2454 if (ret)
2455 gui_msg("done=Done.");
2456 return ret;
2457#endif // ifdef TW_OEM_BUILD
2458}
2459
bigbiffa2bd7b72020-05-07 21:45:34 -04002460bool TWPartition::Recreate_AB_Cache_Dir(const ext4_encryption_policy &policy) {
2461 struct passwd pd;
2462 struct passwd *pwdptr = &pd;
2463 struct passwd *tempPd;
2464 char pwdBuf[512];
2465 int uid = 0, gid = 0;
2466
2467 if ((getpwnam_r("system", pwdptr, pwdBuf, sizeof(pwdBuf), &tempPd)) != 0) {
2468 LOGERR("unable to get system user id\n");
2469 return false;
2470 } else {
2471 struct group grp;
2472 struct group *grpptr = &grp;
2473 struct group *tempGrp;
2474 char grpBuf[512];
2475
2476 if ((getgrnam_r("cache", grpptr, grpBuf, sizeof(grpBuf), &tempGrp)) != 0) {
2477 LOGERR("unable to get cache group id\n");
2478 return false;
2479 } else {
2480 uid = pd.pw_uid;
2481 gid = grp.gr_gid;
2482
2483 if (!TWFunc::Create_Dir_Recursive(AB_CACHE_DIR, S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP, uid, gid)) {
2484 LOGERR("Unable to recreate %s\n", AB_CACHE_DIR);
2485 return false;
2486 }
2487 if (setfilecon(AB_CACHE_DIR, "u:object_r:cache_file:s0") != 0) {
2488 LOGERR("Unable to set contexts for %s\n", AB_CACHE_DIR);
2489 return false;
2490 }
2491 char policy_hex[EXT4_KEY_DESCRIPTOR_SIZE_HEX];
2492 policy_to_hex(policy.master_key_descriptor, policy_hex);
2493 LOGINFO("setting policy for %s: %s\n", policy_hex, AB_CACHE_DIR);
2494 if (sizeof(policy.master_key_descriptor) > 0) {
2495 if (!TWFunc::Set_Encryption_Policy(AB_CACHE_DIR, policy)) {
2496 LOGERR("Unable to set encryption policy for %s\n", AB_CACHE_DIR);
2497 LOGINFO("Removing %s\n", AB_CACHE_DIR);
2498 int ret = TWFunc::removeDir(AB_CACHE_DIR, true);
2499 if (ret == -1) {
2500 LOGERR("Unable to remove %s\n", AB_CACHE_DIR);
2501 }
2502 return false;
2503 }
2504 } else {
2505 LOGERR("Not setting empty policy to %s\n", AB_CACHE_DIR);
2506 return false;
2507 }
2508 }
2509 }
2510 return true;
2511}
2512
Ethan Yonker66a19492015-12-10 10:19:45 -06002513bool TWPartition::Wipe_Data_Without_Wiping_Media_Func(const string& parent __unused) {
2514 string dir;
Dees_Troy38bd7602012-09-14 13:33:53 -04002515
2516 DIR* d;
Ethan Yonker66a19492015-12-10 10:19:45 -06002517 d = opendir(parent.c_str());
Dees_Troy16b74352012-11-14 22:27:31 +00002518 if (d != NULL) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002519 struct dirent* de;
2520 while ((de = readdir(d)) != NULL) {
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002521 if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) continue;
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05002522
Ethan Yonker66a19492015-12-10 10:19:45 -06002523 dir = parent;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002524 dir.append(de->d_name);
Ethan Yonker3fdcda42016-11-30 12:29:37 -06002525 if (wipe_exclusions.check_skip_dirs(dir)) {
Ethan Yonker66a19492015-12-10 10:19:45 -06002526 LOGINFO("skipped '%s'\n", dir.c_str());
2527 continue;
2528 }
Dees_Troyce675462013-01-09 19:48:21 +00002529 if (de->d_type == DT_DIR) {
Ethan Yonker66a19492015-12-10 10:19:45 -06002530 dir.append("/");
2531 if (!Wipe_Data_Without_Wiping_Media_Func(dir)) {
2532 closedir(d);
2533 return false;
2534 }
2535 rmdir(dir.c_str());
bigbiff bigbiff98f1f902013-01-19 18:46:13 -05002536 } 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 +03002537 if (unlink(dir.c_str()) != 0)
Ethan Yonker79f88bd2016-12-09 14:52:12 -06002538 LOGINFO("Unable to unlink '%s': %s\n", dir.c_str(), strerror(errno));
Dees_Troyce675462013-01-09 19:48:21 +00002539 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002540 }
2541 closedir(d);
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05002542
Dees_Troy16b74352012-11-14 22:27:31 +00002543 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -04002544 }
Ethan Yonker74db1572015-10-28 12:44:49 -05002545 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy16b74352012-11-14 22:27:31 +00002546 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04002547}
2548
dianlujitao4879b372018-12-03 18:45:47 +08002549void TWPartition::Wipe_Crypto_Key() {
2550 Find_Actual_Block_Device();
2551 if (Crypto_Key_Location.empty())
2552 return;
2553 else if (Crypto_Key_Location == "footer") {
2554 int fd = open(Actual_Block_Device.c_str(), O_RDWR);
2555 if (fd < 0) {
2556 gui_print_color("warning", "Unable to open '%s' to wipe crypto key\n", Actual_Block_Device.c_str());
2557 return;
2558 }
2559
2560 unsigned int block_count;
2561 if ((ioctl(fd, BLKGETSIZE, &block_count)) == -1) {
2562 gui_print_color("warning", "Unable to get block size for wiping crypto footer.\n");
2563 } else {
2564 int newlen = Length < 0 ? -Length : CRYPT_FOOTER_OFFSET;
2565 off64_t offset = ((off64_t)block_count * 512) - newlen;
2566 if (lseek64(fd, offset, SEEK_SET) == -1) {
2567 gui_print_color("warning", "Unable to lseek64 for wiping crypto footer.\n");
2568 } else {
2569 void* buffer = malloc(newlen);
2570 if (!buffer) {
2571 gui_print_color("warning", "Failed to malloc for wiping crypto footer.\n");
2572 } else {
2573 memset(buffer, 0, newlen);
2574 int ret = write(fd, buffer, newlen);
2575 if (ret != newlen) {
2576 gui_print_color("warning", "Failed to wipe crypto footer.\n");
2577 } else {
2578 LOGINFO("Successfully wiped crypto footer.\n");
2579 }
2580 free(buffer);
2581 }
2582 }
2583 }
2584 close(fd);
2585 } else {
2586 if (TWFunc::IOCTL_Get_Block_Size(Crypto_Key_Location.c_str()) >= 16384LLU) {
2587 string Command = "dd of='" + Crypto_Key_Location + "' if=/dev/zero bs=16384 count=1";
2588 TWFunc::Exec_Cmd(Command);
2589 } else {
2590 LOGINFO("Crypto key location reports size < 16K so not wiping crypto footer.\n");
2591 }
2592 }
2593}
2594
bigbiffce8f83c2015-12-12 18:30:21 -05002595bool TWPartition::Backup_Tar(PartitionSettings *part_settings, pid_t *tar_fork_pid) {
Ethan Yonker472f5062016-02-25 13:47:30 -06002596 string Full_FileName;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002597 twrpTar tar;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05002598
Dees_Troy43d8b002012-09-17 16:00:01 -04002599 if (!Mount(true))
2600 return false;
2601
Ian Macdonalda3b25a32019-06-16 22:39:14 +02002602 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Backup_Display_Name, gui_parse_text("{@backing}"));
Ethan Yonker74db1572015-10-28 12:44:49 -05002603 gui_msg(Msg("backing_up=Backing up {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002604
Ethan Yonker472f5062016-02-25 13:47:30 -06002605 DataManager::GetValue(TW_USE_COMPRESSION_VAR, tar.use_compression);
Matt Mowerbb81e5d2014-03-20 18:05:41 -05002606
Dees_Troy83bd4832013-05-04 12:39:56 +00002607#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
Ethan Yonker472f5062016-02-25 13:47:30 -06002608 if (Can_Encrypt_Backup) {
2609 DataManager::GetValue("tw_encrypt_backup", tar.use_encryption);
2610 if (tar.use_encryption) {
2611 if (Use_Userdata_Encryption)
2612 tar.userdata_encryption = tar.use_encryption;
2613 string Password;
2614 DataManager::GetValue("tw_backup_password", Password);
2615 tar.setpassword(Password);
2616 } else {
2617 tar.use_encryption = 0;
2618 }
Dees_Troy83bd4832013-05-04 12:39:56 +00002619 }
2620#endif
Dees_Troy43d8b002012-09-17 16:00:01 -04002621
Ethan Yonker472f5062016-02-25 13:47:30 -06002622 Backup_FileName = Backup_Name + "." + Current_File_System + ".win";
Ethan Yonkere080c1f2016-09-19 13:50:25 -05002623 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
Ethan Yonker01f4e032017-02-03 15:30:52 -06002624 if (Has_Data_Media)
2625 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));
bigbiffce8f83c2015-12-12 18:30:21 -05002626 tar.part_settings = part_settings;
Ethan Yonker3fdcda42016-11-30 12:29:37 -06002627 tar.backup_exclusions = &backup_exclusions;
Dees Troye0a433a2013-12-02 04:10:37 +00002628 tar.setdir(Backup_Path);
2629 tar.setfn(Full_FileName);
2630 tar.setsize(Backup_Size);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002631 tar.partition_name = Backup_Name;
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002632 tar.backup_folder = part_settings->Backup_Folder;
bigbiffce8f83c2015-12-12 18:30:21 -05002633 if (tar.createTarFork(tar_fork_pid) != 0)
Dees Troye0a433a2013-12-02 04:10:37 +00002634 return false;
Dees_Troy43d8b002012-09-17 16:00:01 -04002635 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002636}
2637
bigbiffce8f83c2015-12-12 18:30:21 -05002638bool TWPartition::Backup_Image(PartitionSettings *part_settings) {
2639 string Full_FileName, adb_file_name;
igoriok87e3d932013-01-31 21:03:53 +02002640
Ethan Yonker74db1572015-10-28 12:44:49 -05002641 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, gui_parse_text("{@backing}"));
2642 gui_msg(Msg("backing_up=Backing up {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002643
Ethan Yonker472f5062016-02-25 13:47:30 -06002644 Backup_FileName = Backup_Name + "." + Current_File_System + ".win";
Dees_Troy43d8b002012-09-17 16:00:01 -04002645
bigbiffce8f83c2015-12-12 18:30:21 -05002646 if (part_settings->adbbackup) {
2647 Full_FileName = TW_ADB_BACKUP;
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002648 adb_file_name = part_settings->Backup_Folder + "/" + Backup_FileName;
bigbiffce8f83c2015-12-12 18:30:21 -05002649 }
2650 else
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002651 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
bigbiffce8f83c2015-12-12 18:30:21 -05002652
2653 part_settings->total_restore_size = Backup_Size;
2654
2655 if (part_settings->adbbackup) {
2656 if (!twadbbu::Write_TWIMG(adb_file_name, Backup_Size))
2657 return false;
2658 }
2659
2660 if (!Raw_Read_Write(part_settings))
Ethan Yonker472f5062016-02-25 13:47:30 -06002661 return false;
2662
bigbiffce8f83c2015-12-12 18:30:21 -05002663 if (part_settings->adbbackup) {
2664 if (!twadbbu::Write_TWEOF())
2665 return false;
Dees_Troy7c2dec82012-09-26 09:49:14 -04002666 }
Dees_Troy43d8b002012-09-17 16:00:01 -04002667 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002668}
2669
bigbiffce8f83c2015-12-12 18:30:21 -05002670bool TWPartition::Raw_Read_Write(PartitionSettings *part_settings) {
2671 unsigned long long RW_Block_Size, Remain = Backup_Size;
2672 int src_fd = -1, dest_fd = -1;
2673 ssize_t bs;
Ethan Yonker472f5062016-02-25 13:47:30 -06002674 bool ret = false;
2675 void* buffer = NULL;
2676 unsigned long long backedup_size = 0;
bigbiffce8f83c2015-12-12 18:30:21 -05002677 string srcfn, destfn;
Ethan Yonker472f5062016-02-25 13:47:30 -06002678
bigbiffce8f83c2015-12-12 18:30:21 -05002679 if (part_settings->PM_Method == PM_BACKUP) {
2680 srcfn = Actual_Block_Device;
2681 if (part_settings->adbbackup)
2682 destfn = TW_ADB_BACKUP;
bigbiff bigbiff38b83c12017-12-28 19:58:52 -05002683 else {
Ethan Yonkere080c1f2016-09-19 13:50:25 -05002684 destfn = part_settings->Backup_Folder + "/" + Backup_FileName;
bigbiff bigbiff38b83c12017-12-28 19:58:52 -05002685 }
bigbiffce8f83c2015-12-12 18:30:21 -05002686 }
2687 else {
2688 destfn = Actual_Block_Device;
2689 if (part_settings->adbbackup) {
2690 srcfn = TW_ADB_RESTORE;
2691 } else {
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002692 srcfn = part_settings->Backup_Folder + "/" + Backup_FileName;
bigbiffce8f83c2015-12-12 18:30:21 -05002693 Remain = TWFunc::Get_File_Size(srcfn);
2694 }
2695 }
Ethan Yonker472f5062016-02-25 13:47:30 -06002696
bigbiffce8f83c2015-12-12 18:30:21 -05002697 src_fd = open(srcfn.c_str(), O_RDONLY | O_LARGEFILE);
Ethan Yonker472f5062016-02-25 13:47:30 -06002698 if (src_fd < 0) {
bigbiffce8f83c2015-12-12 18:30:21 -05002699 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(srcfn.c_str())(strerror(errno)));
Ethan Yonker472f5062016-02-25 13:47:30 -06002700 return false;
2701 }
bigbiffce8f83c2015-12-12 18:30:21 -05002702
2703 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 -06002704 if (dest_fd < 0) {
bigbiffce8f83c2015-12-12 18:30:21 -05002705 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(destfn.c_str())(strerror(errno)));
Ethan Yonker472f5062016-02-25 13:47:30 -06002706 goto exit;
2707 }
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -04002708
bigbiffce8f83c2015-12-12 18:30:21 -05002709 LOGINFO("Reading '%s', writing '%s'\n", srcfn.c_str(), destfn.c_str());
2710
2711 if (part_settings->adbbackup) {
2712 RW_Block_Size = MAX_ADB_READ;
2713 bs = MAX_ADB_READ;
2714 }
2715 else {
2716 RW_Block_Size = 1048576LLU; // 1MB
2717 bs = (ssize_t)(RW_Block_Size);
2718 }
2719
Ethan Yonker472f5062016-02-25 13:47:30 -06002720 buffer = malloc((size_t)bs);
2721 if (!buffer) {
2722 LOGINFO("Raw_Read_Write failed to malloc\n");
2723 goto exit;
2724 }
bigbiffce8f83c2015-12-12 18:30:21 -05002725
2726 if (part_settings->progress)
2727 part_settings->progress->SetPartitionSize(part_settings->total_restore_size);
2728
Ethan Yonker472f5062016-02-25 13:47:30 -06002729 while (Remain > 0) {
2730 if (Remain < RW_Block_Size)
bigbiffce8f83c2015-12-12 18:30:21 -05002731 bs = (ssize_t)(Remain);
Matt Mowera8a89d12016-12-30 18:10:37 -06002732 if (read(src_fd, buffer, bs) != bs) {
Ethan Yonker472f5062016-02-25 13:47:30 -06002733 LOGINFO("Error reading source fd (%s)\n", strerror(errno));
2734 goto exit;
2735 }
2736 if (write(dest_fd, buffer, bs) != bs) {
2737 LOGINFO("Error writing destination fd (%s)\n", strerror(errno));
2738 goto exit;
2739 }
2740 backedup_size += (unsigned long long)(bs);
Matt Mower029a82d2017-01-08 13:12:38 -06002741 Remain -= (unsigned long long)(bs);
bigbiffce8f83c2015-12-12 18:30:21 -05002742 if (part_settings->progress)
2743 part_settings->progress->UpdateSize(backedup_size);
Ethan Yonker472f5062016-02-25 13:47:30 -06002744 if (PartitionManager.Check_Backup_Cancel() != 0)
2745 goto exit;
2746 }
bigbiffce8f83c2015-12-12 18:30:21 -05002747 if (part_settings->progress)
2748 part_settings->progress->UpdateDisplayDetails(true);
Ethan Yonker472f5062016-02-25 13:47:30 -06002749 fsync(dest_fd);
nailyka083dc62016-11-11 21:41:28 +01002750
2751 if (!part_settings->adbbackup && part_settings->PM_Method == PM_BACKUP) {
2752 tw_set_default_metadata(destfn.c_str());
2753 LOGINFO("Restored default metadata for %s\n", destfn.c_str());
2754 }
2755
Ethan Yonker472f5062016-02-25 13:47:30 -06002756 ret = true;
2757exit:
2758 if (src_fd >= 0)
2759 close(src_fd);
2760 if (dest_fd >= 0)
2761 close(dest_fd);
2762 if (buffer)
2763 free(buffer);
2764 return ret;
2765}
2766
bigbiffce8f83c2015-12-12 18:30:21 -05002767bool TWPartition::Backup_Dump_Image(PartitionSettings *part_settings) {
Vojtech Bocek05534202013-09-11 08:11:56 +02002768 string Full_FileName, Command;
Dees_Troy43d8b002012-09-17 16:00:01 -04002769
Ethan Yonker74db1572015-10-28 12:44:49 -05002770 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, gui_parse_text("{@backing}"));
2771 gui_msg(Msg("backing_up=Backing up {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002772
bigbiffce8f83c2015-12-12 18:30:21 -05002773 if (part_settings->progress)
2774 part_settings->progress->SetPartitionSize(Backup_Size);
Dees_Troy43d8b002012-09-17 16:00:01 -04002775
Ethan Yonker472f5062016-02-25 13:47:30 -06002776 Backup_FileName = Backup_Name + "." + Current_File_System + ".win";
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002777 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04002778
2779 Command = "dump_image " + MTD_Name + " '" + Full_FileName + "'";
bigbiffce8f83c2015-12-12 18:30:21 -05002780
Dees_Troy2673cec2013-04-02 20:22:16 +00002781 LOGINFO("Backup command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02002782 TWFunc::Exec_Cmd(Command);
Ethan Yonker4b94cfd2014-12-11 10:00:45 -06002783 tw_set_default_metadata(Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04002784 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
2785 // 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 -06002786 gui_msg(Msg(msg::kError, "backup_size=Backup file size for '{1}' is 0 bytes.")(Full_FileName));
Dees_Troy7c2dec82012-09-26 09:49:14 -04002787 return false;
2788 }
bigbiffce8f83c2015-12-12 18:30:21 -05002789 if (part_settings->progress)
2790 part_settings->progress->UpdateSize(Backup_Size);
2791
Dees_Troy43d8b002012-09-17 16:00:01 -04002792 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002793}
2794
bigbiffce8f83c2015-12-12 18:30:21 -05002795unsigned long long TWPartition::Get_Restore_Size(PartitionSettings *part_settings) {
2796 if (!part_settings->adbbackup) {
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002797 InfoManager restore_info(part_settings->Backup_Folder + "/" + Backup_Name + ".info");
bigbiffce8f83c2015-12-12 18:30:21 -05002798 if (restore_info.LoadValues() == 0) {
2799 if (restore_info.GetValue("backup_size", Restore_Size) == 0) {
2800 LOGINFO("Read info file, restore size is %llu\n", Restore_Size);
2801 return Restore_Size;
2802 }
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002803 }
2804 }
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002805
Matt Mower029a82d2017-01-08 13:12:38 -06002806 string Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
2807 string Restore_File_System = Get_Restore_File_System(part_settings);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002808
2809 if (Is_Image(Restore_File_System)) {
2810 Restore_Size = TWFunc::Get_File_Size(Full_FileName);
2811 return Restore_Size;
2812 }
2813
2814 twrpTar tar;
2815 tar.setdir(Backup_Path);
2816 tar.setfn(Full_FileName);
bigbiffce8f83c2015-12-12 18:30:21 -05002817 tar.backup_name = Full_FileName;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002818#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
2819 string Password;
2820 DataManager::GetValue("tw_restore_password", Password);
2821 if (!Password.empty())
2822 tar.setpassword(Password);
2823#endif
2824 tar.partition_name = Backup_Name;
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002825 tar.backup_folder = part_settings->Backup_Folder;
bigbiffce8f83c2015-12-12 18:30:21 -05002826 tar.part_settings = part_settings;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002827 Restore_Size = tar.get_size();
2828 return Restore_Size;
2829}
2830
bigbiffce8f83c2015-12-12 18:30:21 -05002831bool TWPartition::Restore_Tar(PartitionSettings *part_settings) {
Ethan Yonker472f5062016-02-25 13:47:30 -06002832 string Full_FileName;
Dees Troy4159aed2014-02-28 17:24:43 +00002833 bool ret = false;
bigbiffce8f83c2015-12-12 18:30:21 -05002834 string Restore_File_System = Get_Restore_File_System(part_settings);
Dees_Troy43d8b002012-09-17 16:00:01 -04002835
Dees_Troye58d5262012-09-21 12:27:57 -04002836 if (Has_Android_Secure) {
Dees_Troye58d5262012-09-21 12:27:57 -04002837 if (!Wipe_AndSec())
2838 return false;
Gary Peck43acadf2012-11-21 21:19:01 -08002839 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002840 gui_msg(Msg("wiping=Wiping {1}")(Backup_Display_Name));
Ethan Yonker5eac2222014-06-11 12:22:55 -05002841 if (Has_Data_Media && Mount_Point == "/data" && Restore_File_System != Current_File_System) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002842 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 -05002843 if (!Wipe_Data_Without_Wiping_Media())
2844 return false;
2845 } else {
2846 if (!Wipe(Restore_File_System))
2847 return false;
2848 }
Dees_Troye58d5262012-09-21 12:27:57 -04002849 }
Matt Mower3c366972015-12-25 19:28:31 -06002850 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Backup_Display_Name, gui_parse_text("{@restoring_hdr}"));
Ethan Yonker74db1572015-10-28 12:44:49 -05002851 gui_msg(Msg("restoring=Restoring {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002852
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05002853 // Remount as read/write as needed so we can restore the backup
2854 if (!ReMount_RW(true))
Dees_Troy43d8b002012-09-17 16:00:01 -04002855 return false;
2856
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002857 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
Ethan Yonker87af5632014-02-10 11:56:35 -06002858 twrpTar tar;
bigbiffce8f83c2015-12-12 18:30:21 -05002859 tar.part_settings = part_settings;
Ethan Yonker87af5632014-02-10 11:56:35 -06002860 tar.setdir(Backup_Path);
2861 tar.setfn(Full_FileName);
2862 tar.backup_name = Backup_Name;
2863#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
2864 string Password;
2865 DataManager::GetValue("tw_restore_password", Password);
2866 if (!Password.empty())
2867 tar.setpassword(Password);
2868#endif
bigbiffce8f83c2015-12-12 18:30:21 -05002869 part_settings->progress->SetPartitionSize(Get_Restore_Size(part_settings));
2870 if (tar.extractTarFork() != 0)
Dees Troy4159aed2014-02-28 17:24:43 +00002871 ret = false;
2872 else
2873 ret = true;
2874#ifdef HAVE_CAPABILITIES
2875 // Restore capabilities to the run-as binary
Captain Throwback9d6feb52018-07-27 10:05:24 -04002876 if (Mount_Point == PartitionManager.Get_Android_Root_Path() && Mount(true) && TWFunc::Path_Exists("/system/bin/run-as")) {
Dees Troy4159aed2014-02-28 17:24:43 +00002877 struct vfs_cap_data cap_data;
2878 uint64_t capabilities = (1 << CAP_SETUID) | (1 << CAP_SETGID);
2879
2880 memset(&cap_data, 0, sizeof(cap_data));
2881 cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE;
2882 cap_data.data[0].permitted = (uint32_t) (capabilities & 0xffffffff);
2883 cap_data.data[0].inheritable = 0;
2884 cap_data.data[1].permitted = (uint32_t) (capabilities >> 32);
2885 cap_data.data[1].inheritable = 0;
2886 if (setxattr("/system/bin/run-as", XATTR_NAME_CAPS, &cap_data, sizeof(cap_data), 0) < 0) {
2887 LOGINFO("Failed to reset capabilities of /system/bin/run-as binary.\n");
2888 } else {
2889 LOGINFO("Reset capabilities of /system/bin/run-as binary successful.\n");
2890 }
2891 }
2892#endif
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05002893 if (Mount_Read_Only || Mount_Flags & MS_RDONLY)
2894 // Remount as read only when restoration is complete
2895 ReMount(true);
2896
Dees Troy4159aed2014-02-28 17:24:43 +00002897 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04002898}
2899
bigbiffce8f83c2015-12-12 18:30:21 -05002900bool TWPartition::Restore_Image(PartitionSettings *part_settings) {
Ethan Yonker96af84a2015-01-05 14:58:36 -06002901 string Full_FileName;
bigbiffce8f83c2015-12-12 18:30:21 -05002902 string Restore_File_System = Get_Restore_File_System(part_settings);
Dees_Troy43d8b002012-09-17 16:00:01 -04002903
Matt Mower3c366972015-12-25 19:28:31 -06002904 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Backup_Display_Name, gui_parse_text("{@restoring_hdr}"));
Ethan Yonker74db1572015-10-28 12:44:49 -05002905 gui_msg(Msg("restoring=Restoring {1}...")(Backup_Display_Name));
bigbiffce8f83c2015-12-12 18:30:21 -05002906
2907 if (part_settings->adbbackup)
2908 Full_FileName = TW_ADB_RESTORE;
2909 else
Ethan Yonkere080c1f2016-09-19 13:50:25 -05002910 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
Gary Peck15e623d2012-11-21 21:07:58 -08002911
Ethan Yonker96af84a2015-01-05 14:58:36 -06002912 if (Restore_File_System == "emmc") {
bigbiffce8f83c2015-12-12 18:30:21 -05002913 if (!part_settings->adbbackup)
2914 part_settings->total_restore_size = (uint64_t)(TWFunc::Get_File_Size(Full_FileName));
2915 if (!Raw_Read_Write(part_settings))
Ethan Yonker96af84a2015-01-05 14:58:36 -06002916 return false;
2917 } else if (Restore_File_System == "mtd" || Restore_File_System == "bml") {
bigbiffce8f83c2015-12-12 18:30:21 -05002918 if (!Flash_Image_FI(Full_FileName, part_settings->progress))
2919 return false;
2920 }
2921
2922 if (part_settings->adbbackup) {
2923 if (!twadbbu::Write_TWEOF())
Ethan Yonker96af84a2015-01-05 14:58:36 -06002924 return false;
Gary Peck15e623d2012-11-21 21:07:58 -08002925 }
Dees_Troy43d8b002012-09-17 16:00:01 -04002926 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002927}
Dees_Troy5bf43922012-09-07 16:07:55 -04002928
2929bool TWPartition::Update_Size(bool Display_Error) {
Dees_Troy0550cfb2012-10-13 11:56:13 -04002930 bool ret = false, Was_Already_Mounted = false;
Dees_Troy51127312012-09-08 13:08:49 -04002931
Ethan Yonker1b190162016-12-05 15:25:19 -06002932 Find_Actual_Block_Device();
2933
2934 if (!Can_Be_Mounted && !Is_Encrypted) {
2935 if (TWFunc::Path_Exists(Actual_Block_Device) && Find_Partition_Size()) {
2936 Used = Size;
2937 Backup_Size = Size;
2938 return true;
2939 }
Dees_Troy5bf43922012-09-07 16:07:55 -04002940 return false;
Ethan Yonker1b190162016-12-05 15:25:19 -06002941 }
Dees_Troy5bf43922012-09-07 16:07:55 -04002942
Dees_Troy0550cfb2012-10-13 11:56:13 -04002943 Was_Already_Mounted = Is_Mounted();
Dees_Troy38bd7602012-09-14 13:33:53 -04002944 if (Removable || Is_Encrypted) {
2945 if (!Mount(false))
2946 return true;
2947 } else if (!Mount(Display_Error))
Dees_Troy5bf43922012-09-07 16:07:55 -04002948 return false;
Dees_Troy51127312012-09-08 13:08:49 -04002949
2950 ret = Get_Size_Via_statfs(Display_Error);
Dees_Troy0550cfb2012-10-13 11:56:13 -04002951 if (!ret || Size == 0) {
2952 if (!Get_Size_Via_df(Display_Error)) {
2953 if (!Was_Already_Mounted)
2954 UnMount(false);
Dees_Troy51127312012-09-08 13:08:49 -04002955 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04002956 }
2957 }
Dees_Troy51127312012-09-08 13:08:49 -04002958
Dees_Troy5bf43922012-09-07 16:07:55 -04002959 if (Has_Data_Media) {
2960 if (Mount(Display_Error)) {
Ethan Yonker3fdcda42016-11-30 12:29:37 -06002961 Used = backup_exclusions.Get_Folder_Size(Mount_Point);
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002962 Backup_Size = Used;
2963 int bak = (int)(Used / 1048576LLU);
Dees_Troy51127312012-09-08 13:08:49 -04002964 int fre = (int)(Free / 1048576LLU);
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002965 LOGINFO("Data backup size is %iMB, free: %iMB.\n", bak, fre);
Dees_Troy0550cfb2012-10-13 11:56:13 -04002966 } else {
2967 if (!Was_Already_Mounted)
2968 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04002969 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04002970 }
Dees_Troye58d5262012-09-21 12:27:57 -04002971 } else if (Has_Android_Secure) {
2972 if (Mount(Display_Error))
Ethan Yonker3fdcda42016-11-30 12:29:37 -06002973 Backup_Size = backup_exclusions.Get_Folder_Size(Backup_Path);
Dees_Troy0550cfb2012-10-13 11:56:13 -04002974 else {
2975 if (!Was_Already_Mounted)
2976 UnMount(false);
Dees_Troye58d5262012-09-21 12:27:57 -04002977 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04002978 }
Dees_Troy5bf43922012-09-07 16:07:55 -04002979 }
Dees_Troy0550cfb2012-10-13 11:56:13 -04002980 if (!Was_Already_Mounted)
2981 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04002982 return true;
Dees_Troy51127312012-09-08 13:08:49 -04002983}
Dees_Troy38bd7602012-09-14 13:33:53 -04002984
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06002985bool TWPartition::Find_Wildcard_Block_Devices(const string& Device) {
2986 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
2987 string Path = TWFunc::Get_Path(Device);
2988 string Dev = TWFunc::Get_Filename(Device);
2989 size_t wildcard_index = Dev.find("*");
2990 if (wildcard_index != string::npos)
2991 Dev = Dev.substr(0, wildcard_index);
2992 wildcard_index = Dev.size();
2993 DIR* d = opendir(Path.c_str());
2994 if (d == NULL) {
2995 LOGINFO("Error opening '%s': %s\n", Path.c_str(), strerror(errno));
2996 return false;
2997 }
2998 struct dirent* de;
2999 while ((de = readdir(d)) != NULL) {
3000 if (de->d_type != DT_BLK || strlen(de->d_name) <= wildcard_index || strncmp(de->d_name, Dev.c_str(), wildcard_index) != 0)
3001 continue;
3002
3003 string item = Path + "/";
3004 item.append(de->d_name);
3005 if (PartitionManager.Find_Partition_By_Block_Device(item))
3006 continue;
3007 TWPartition *part = new TWPartition;
3008 char buffer[MAX_FSTAB_LINE_LENGTH];
3009 sprintf(buffer, "%s %s-%i auto defaults defaults", item.c_str(), Mount_Point.c_str(), ++mount_point_index);
Chaosmasterf6e42ce2020-01-27 00:17:04 +01003010 part->Process_Fstab_Line(buffer, false, NULL, false);
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06003011 char display[MAX_FSTAB_LINE_LENGTH];
3012 sprintf(display, "%s %i", Storage_Name.c_str(), mount_point_index);
3013 part->Storage_Name = display;
3014 part->Display_Name = display;
3015 part->Primary_Block_Device = item;
3016 part->Wildcard_Block_Device = false;
3017 part->Is_SubPartition = true;
3018 part->SubPartition_Of = Mount_Point;
3019 part->Is_Storage = Is_Storage;
3020 part->Can_Be_Mounted = true;
3021 part->Removable = true;
3022 part->Can_Be_Wiped = Can_Be_Wiped;
3023 part->Wipe_Available_in_GUI = Wipe_Available_in_GUI;
3024 part->Find_Actual_Block_Device();
3025 part->Update_Size(false);
3026 Has_SubPartition = true;
3027 PartitionManager.Output_Partition(part);
3028 PartitionManager.Add_Partition(part);
3029 }
3030 closedir(d);
3031 return (mount_point_index > 0);
3032}
3033
Dees_Troy38bd7602012-09-14 13:33:53 -04003034void TWPartition::Find_Actual_Block_Device(void) {
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06003035 if (!Sysfs_Entry.empty() && Primary_Block_Device.empty() && Decrypted_Block_Device.empty()) {
3036 /* Sysfs_Entry.empty() indicates if this is a sysfs entry that begins with /device/
3037 * If we have a syfs entry then we are looking for this device from a uevent add.
3038 * The uevent add will set the primary block device based on the data we receive from
3039 * after checking for adopted storage. If the device ends up being adopted, then the
3040 * decrypted block device will be set instead of the primary block device. */
3041 Is_Present = false;
3042 return;
3043 }
3044 if (Wildcard_Block_Device && !Is_Adopted_Storage) {
3045 Is_Present = false;
3046 Actual_Block_Device = "";
3047 Can_Be_Mounted = false;
3048 if (!Find_Wildcard_Block_Devices(Primary_Block_Device)) {
3049 string Dev = Primary_Block_Device.substr(0, Primary_Block_Device.find("*"));
3050 if (TWFunc::Path_Exists(Dev)) {
3051 Is_Present = true;
3052 Can_Be_Mounted = true;
3053 Actual_Block_Device = Dev;
3054 }
3055 }
3056 return;
3057 } else if (Is_Decrypted && !Decrypted_Block_Device.empty()) {
Dees_Troy38bd7602012-09-14 13:33:53 -04003058 Actual_Block_Device = Decrypted_Block_Device;
Ethan Yonker1b190162016-12-05 15:25:19 -06003059 if (TWFunc::Path_Exists(Decrypted_Block_Device)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04003060 Is_Present = true;
Ethan Yonker1b190162016-12-05 15:25:19 -06003061 return;
3062 }
3063 } else if (SlotSelect && TWFunc::Path_Exists(Primary_Block_Device + PartitionManager.Get_Active_Slot_Suffix())) {
3064 Actual_Block_Device = Primary_Block_Device + PartitionManager.Get_Active_Slot_Suffix();
3065 unlink(Primary_Block_Device.c_str());
3066 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
3067 Is_Present = true;
3068 return;
Dees_Troy43d8b002012-09-17 16:00:01 -04003069 } else if (TWFunc::Path_Exists(Primary_Block_Device)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04003070 Is_Present = true;
3071 Actual_Block_Device = Primary_Block_Device;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05003072 return;
3073 }
Ethan Yonker1b190162016-12-05 15:25:19 -06003074 if (!Alternate_Block_Device.empty() && TWFunc::Path_Exists(Alternate_Block_Device)) {
Dees_Troy3f04d032012-10-07 18:20:09 -04003075 Actual_Block_Device = Alternate_Block_Device;
Dees_Troy38bd7602012-09-14 13:33:53 -04003076 Is_Present = true;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05003077 } else {
Dees_Troy38bd7602012-09-14 13:33:53 -04003078 Is_Present = false;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05003079 }
Dees_Troy38bd7602012-09-14 13:33:53 -04003080}
3081
3082void TWPartition::Recreate_Media_Folder(void) {
3083 string Command;
Ethan Yonker66a19492015-12-10 10:19:45 -06003084 string Media_Path = Mount_Point + "/media";
Dees_Troy38bd7602012-09-14 13:33:53 -04003085
Ethan Yonkerbd7492d2016-12-07 13:55:01 -06003086 if (Is_FBE) {
3087 LOGINFO("Not recreating media folder on FBE\n");
3088 return;
3089 }
Dees_Troy38bd7602012-09-14 13:33:53 -04003090 if (!Mount(true)) {
Ethan Yonker66a19492015-12-10 10:19:45 -06003091 gui_msg(Msg(msg::kError, "recreate_folder_err=Unable to recreate {1} folder.")(Media_Path));
3092 } else if (!TWFunc::Path_Exists(Media_Path)) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05003093 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Ethan Yonker66a19492015-12-10 10:19:45 -06003094 LOGINFO("Recreating %s folder.\n", Media_Path.c_str());
3095 mkdir(Media_Path.c_str(), 0770);
xiaolu9416f4f2015-06-04 08:22:23 +08003096 string Internal_path = DataManager::GetStrValue("tw_internal_path");
3097 if (!Internal_path.empty()) {
3098 LOGINFO("Recreating %s folder.\n", Internal_path.c_str());
3099 mkdir(Internal_path.c_str(), 0770);
3100 }
3101#ifdef TW_INTERNAL_STORAGE_PATH
3102 mkdir(EXPAND(TW_INTERNAL_STORAGE_PATH), 0770);
3103#endif
Matt Mower87413642017-01-17 21:14:46 -06003104
thata3d31fb2014-12-21 22:27:40 +01003105 // Afterwards, we will try to set the
3106 // default metadata that we were hopefully able to get during
3107 // early boot.
Ethan Yonker66a19492015-12-10 10:19:45 -06003108 tw_set_default_metadata(Media_Path.c_str());
xiaolu9416f4f2015-06-04 08:22:23 +08003109 if (!Internal_path.empty())
3110 tw_set_default_metadata(Internal_path.c_str());
Matt Mower87413642017-01-17 21:14:46 -06003111
Ethan Yonker5eac2222014-06-11 12:22:55 -05003112 // Toggle mount to ensure that "internal sdcard" gets mounted
bigbiff bigbiff9c754052013-01-09 09:09:08 -05003113 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Ethan Yonker5eac2222014-06-11 12:22:55 -05003114 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Dees_Troy38bd7602012-09-14 13:33:53 -04003115 }
Dees_Troy43d8b002012-09-17 16:00:01 -04003116}
Dees_Troye58d5262012-09-21 12:27:57 -04003117
3118void TWPartition::Recreate_AndSec_Folder(void) {
Dees_Troye58d5262012-09-21 12:27:57 -04003119 if (!Has_Android_Secure)
3120 return;
Dees_Troy2673cec2013-04-02 20:22:16 +00003121 LOGINFO("Creating %s: %s\n", Backup_Display_Name.c_str(), Symlink_Path.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -04003122 if (!Mount(true)) {
Ethan Yonker74db1572015-10-28 12:44:49 -05003123 gui_msg(Msg(msg::kError, "recreate_folder_err=Unable to recreate {1} folder.")(Backup_Name));
Dees_Troye58d5262012-09-21 12:27:57 -04003124 } else if (!TWFunc::Path_Exists(Symlink_Path)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00003125 LOGINFO("Recreating %s folder.\n", Backup_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05003126 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05003127 mkdir(Symlink_Path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
bigbiff bigbiff9c754052013-01-09 09:09:08 -05003128 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04003129 }
3130}
bigbiff7cb4c332014-11-26 20:36:07 -05003131
3132uint64_t TWPartition::Get_Max_FileSize() {
3133 uint64_t maxFileSize = 0;
3134 const uint64_t constGB = (uint64_t) 1024 * 1024 * 1024;
3135 const uint64_t constTB = (uint64_t) constGB * 1024;
3136 const uint64_t constPB = (uint64_t) constTB * 1024;
bigbiff0c532032014-12-21 13:41:26 -05003137 if (Current_File_System == "ext4")
3138 maxFileSize = 16 * constTB; //16 TB
3139 else if (Current_File_System == "vfat")
3140 maxFileSize = 4 * constGB; //4 GB
3141 else if (Current_File_System == "ntfs")
3142 maxFileSize = 256 * constTB; //256 TB
3143 else if (Current_File_System == "exfat")
3144 maxFileSize = 16 * constPB; //16 PB
3145 else if (Current_File_System == "ext3")
3146 maxFileSize = 2 * constTB; //2 TB
3147 else if (Current_File_System == "f2fs")
3148 maxFileSize = 3.94 * constTB; //3.94 TB
bigbiff7cb4c332014-11-26 20:36:07 -05003149 else
3150 maxFileSize = 100000000L;
3151 return maxFileSize - 1;
3152}
3153
bigbiffce8f83c2015-12-12 18:30:21 -05003154bool TWPartition::Flash_Image(PartitionSettings *part_settings) {
3155 string Restore_File_System, full_filename;
Ethan Yonker96af84a2015-01-05 14:58:36 -06003156
Ethan Yonkere080c1f2016-09-19 13:50:25 -05003157 full_filename = part_settings->Backup_Folder + "/" + Backup_FileName;
Ethan Yonker96af84a2015-01-05 14:58:36 -06003158
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05003159 LOGINFO("Image filename is: %s\n", Backup_FileName.c_str());
bigbiffce8f83c2015-12-12 18:30:21 -05003160
3161 if (Backup_Method == BM_FILES) {
Ethan Yonker96af84a2015-01-05 14:58:36 -06003162 LOGERR("Cannot flash images to file systems\n");
3163 return false;
3164 } else if (!Can_Flash_Img) {
3165 LOGERR("Cannot flash images to partitions %s\n", Display_Name.c_str());
3166 return false;
3167 } else {
3168 if (!Find_Partition_Size()) {
3169 LOGERR("Unable to find partition size for '%s'\n", Mount_Point.c_str());
3170 return false;
3171 }
bigbiffce8f83c2015-12-12 18:30:21 -05003172 unsigned long long image_size = TWFunc::Get_File_Size(full_filename);
Ethan Yonker96af84a2015-01-05 14:58:36 -06003173 if (image_size > Size) {
Ethan Yonker74db1572015-10-28 12:44:49 -05003174 LOGINFO("Size (%llu bytes) of image '%s' is larger than target device '%s' (%llu bytes)\n",
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05003175 image_size, Backup_FileName.c_str(), Actual_Block_Device.c_str(), Size);
Ethan Yonker74db1572015-10-28 12:44:49 -05003176 gui_err("img_size_err=Size of image is larger than target device");
Ethan Yonker96af84a2015-01-05 14:58:36 -06003177 return false;
3178 }
bigbiffce8f83c2015-12-12 18:30:21 -05003179 if (Backup_Method == BM_DD) {
3180 if (!part_settings->adbbackup) {
3181 if (Is_Sparse_Image(full_filename)) {
3182 return Flash_Sparse_Image(full_filename);
3183 }
Ethan Yonker472f5062016-02-25 13:47:30 -06003184 }
bigbiffce8f83c2015-12-12 18:30:21 -05003185 return Raw_Read_Write(part_settings);
3186 } else if (Backup_Method == BM_FLASH_UTILS) {
3187 return Flash_Image_FI(full_filename, NULL);
Ethan Yonker472f5062016-02-25 13:47:30 -06003188 }
Ethan Yonker96af84a2015-01-05 14:58:36 -06003189 }
3190
3191 LOGERR("Unknown flash method for '%s'\n", Mount_Point.c_str());
3192 return false;
3193}
3194
Ethan Yonker472f5062016-02-25 13:47:30 -06003195bool TWPartition::Is_Sparse_Image(const string& Filename) {
HashBanged974bb2016-01-30 14:20:09 -05003196 uint32_t magic = 0;
3197 int fd = open(Filename.c_str(), O_RDONLY);
3198 if (fd < 0) {
3199 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Filename)(strerror(errno)));
3200 return false;
3201 }
bigbiffce8f83c2015-12-12 18:30:21 -05003202
HashBanged974bb2016-01-30 14:20:09 -05003203 if (read(fd, &magic, sizeof(magic)) != sizeof(magic)) {
3204 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Filename)(strerror(errno)));
3205 close(fd);
3206 return false;
3207 }
3208 close(fd);
Ethan Yonker472f5062016-02-25 13:47:30 -06003209 if (magic == SPARSE_HEADER_MAGIC)
3210 return true;
3211 return false;
3212}
3213
3214bool TWPartition::Flash_Sparse_Image(const string& Filename) {
3215 string Command;
3216
3217 gui_msg(Msg("flashing=Flashing {1}...")(Display_Name));
3218
3219 Command = "simg2img '" + Filename + "' '" + Actual_Block_Device + "'";
Ethan Yonker96af84a2015-01-05 14:58:36 -06003220 LOGINFO("Flash command: '%s'\n", Command.c_str());
3221 TWFunc::Exec_Cmd(Command);
3222 return true;
3223}
3224
Ethan Yonker472f5062016-02-25 13:47:30 -06003225bool TWPartition::Flash_Image_FI(const string& Filename, ProgressTracking *progress) {
Ethan Yonker96af84a2015-01-05 14:58:36 -06003226 string Command;
Ethan Yonker472f5062016-02-25 13:47:30 -06003227 unsigned long long file_size;
Ethan Yonker96af84a2015-01-05 14:58:36 -06003228
Ethan Yonker74db1572015-10-28 12:44:49 -05003229 gui_msg(Msg("flashing=Flashing {1}...")(Display_Name));
Ethan Yonker472f5062016-02-25 13:47:30 -06003230 if (progress) {
3231 file_size = (unsigned long long)(TWFunc::Get_File_Size(Filename));
3232 progress->SetPartitionSize(file_size);
3233 }
Ethan Yonker96af84a2015-01-05 14:58:36 -06003234 // Sometimes flash image doesn't like to flash due to the first 2KB matching, so we erase first to ensure that it flashes
3235 Command = "erase_image " + MTD_Name;
3236 LOGINFO("Erase command: '%s'\n", Command.c_str());
3237 TWFunc::Exec_Cmd(Command);
3238 Command = "flash_image " + MTD_Name + " '" + Filename + "'";
3239 LOGINFO("Flash command: '%s'\n", Command.c_str());
3240 TWFunc::Exec_Cmd(Command);
Ethan Yonker472f5062016-02-25 13:47:30 -06003241 if (progress)
3242 progress->UpdateSize(file_size);
Ethan Yonker96af84a2015-01-05 14:58:36 -06003243 return true;
3244}
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05003245
3246void TWPartition::Change_Mount_Read_Only(bool new_value) {
3247 Mount_Read_Only = new_value;
3248}
3249
bigbiffce8f83c2015-12-12 18:30:21 -05003250bool TWPartition::Is_Read_Only() {
3251 return Mount_Read_Only;
3252}
3253
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05003254int TWPartition::Check_Lifetime_Writes() {
3255 bool original_read_only = Mount_Read_Only;
3256 int ret = 1;
3257
3258 Mount_Read_Only = true;
3259 if (Mount(false)) {
3260 Find_Actual_Block_Device();
Ethan Yonker54970bb2019-01-17 16:19:24 -06003261 string temp = Actual_Block_Device;
3262 Find_Real_Block_Device(temp, false);
3263 string block = basename(temp.c_str());
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05003264 string file = "/sys/fs/" + Current_File_System + "/" + block + "/lifetime_write_kbytes";
3265 string result;
3266 if (TWFunc::Path_Exists(file)) {
3267 if (TWFunc::read_file(file, result) != 0) {
3268 LOGINFO("Check_Lifetime_Writes of '%s' failed to read_file\n", file.c_str());
3269 } else {
3270 LOGINFO("Check_Lifetime_Writes result: '%s'\n", result.c_str());
3271 if (result == "0") {
3272 ret = 0;
3273 }
3274 }
3275 } else {
3276 LOGINFO("Check_Lifetime_Writes file does not exist '%s'\n", file.c_str());
3277 }
3278 UnMount(true);
3279 } else {
3280 LOGINFO("Check_Lifetime_Writes failed to mount '%s'\n", Mount_Point.c_str());
3281 }
3282 Mount_Read_Only = original_read_only;
3283 return ret;
3284}
Ethan Yonker66a19492015-12-10 10:19:45 -06003285
3286int TWPartition::Decrypt_Adopted() {
3287#ifdef TW_INCLUDE_CRYPTO
3288 int ret = 1;
3289 Is_Adopted_Storage = false;
3290 string Adopted_Key_File = "";
3291
3292 if (!Removable)
3293 return ret;
3294
3295 int fd = open(Alternate_Block_Device.c_str(), O_RDONLY);
3296 if (fd < 0) {
3297 LOGINFO("failed to open '%s'\n", Alternate_Block_Device.c_str());
3298 return ret;
3299 }
3300 char type_guid[80];
3301 char part_guid[80];
3302
3303 if (gpt_disk_get_partition_info(fd, 2, type_guid, part_guid) == 0) {
3304 LOGINFO("type: '%s'\n", type_guid);
3305 LOGINFO("part: '%s'\n", part_guid);
3306 Adopted_GUID = part_guid;
3307 LOGINFO("Adopted_GUID '%s'\n", Adopted_GUID.c_str());
3308 if (strcmp(type_guid, TWGptAndroidExpand) == 0) {
3309 LOGINFO("android_expand found\n");
3310 Adopted_Key_File = "/data/misc/vold/expand_";
3311 Adopted_Key_File += part_guid;
3312 Adopted_Key_File += ".key";
3313 if (TWFunc::Path_Exists(Adopted_Key_File)) {
3314 Is_Adopted_Storage = true;
3315 /* Until we find a use case for this, I think it is safe
3316 * to disable USB Mass Storage whenever adopted storage
3317 * is present.
3318 */
3319 LOGINFO("Detected adopted storage, disabling USB mass storage mode\n");
3320 DataManager::SetValue("tw_has_usb_storage", 0);
3321 }
3322 }
3323 }
3324
3325 if (Is_Adopted_Storage) {
3326 string Adopted_Block_Device = Alternate_Block_Device + "p2";
3327 if (!TWFunc::Path_Exists(Adopted_Block_Device)) {
3328 Adopted_Block_Device = Alternate_Block_Device + "2";
3329 if (!TWFunc::Path_Exists(Adopted_Block_Device)) {
3330 LOGINFO("Adopted block device does not exist\n");
3331 goto exit;
3332 }
3333 }
3334 LOGINFO("key file is '%s', block device '%s'\n", Adopted_Key_File.c_str(), Adopted_Block_Device.c_str());
3335 char crypto_blkdev[MAXPATHLEN];
3336 std::string thekey;
3337 int fdkey = open(Adopted_Key_File.c_str(), O_RDONLY);
3338 if (fdkey < 0) {
3339 LOGINFO("failed to open key file\n");
3340 goto exit;
3341 }
3342 char buf[512];
3343 ssize_t n;
3344 while ((n = read(fdkey, &buf[0], sizeof(buf))) > 0) {
3345 thekey.append(buf, n);
3346 }
3347 close(fdkey);
3348 unsigned char* key = (unsigned char*) thekey.data();
3349 cryptfs_revert_ext_volume(part_guid);
3350
3351 ret = cryptfs_setup_ext_volume(part_guid, Adopted_Block_Device.c_str(), key, thekey.size(), crypto_blkdev);
3352 if (ret == 0) {
3353 LOGINFO("adopted storage new block device: '%s'\n", crypto_blkdev);
3354 Decrypted_Block_Device = crypto_blkdev;
3355 Is_Decrypted = true;
3356 Is_Encrypted = true;
3357 Find_Actual_Block_Device();
Kjell Braden3126a112016-06-19 16:58:15 +00003358 if (!Mount_Storage_Retry(false)) {
Ethan Yonker66a19492015-12-10 10:19:45 -06003359 LOGERR("Failed to mount decrypted adopted storage device\n");
3360 Is_Decrypted = false;
3361 Is_Encrypted = false;
3362 cryptfs_revert_ext_volume(part_guid);
3363 ret = 1;
3364 } else {
Ethan Yonkerfcf3f242016-02-16 12:30:26 -06003365 UnMount(false);
3366 Has_Android_Secure = false;
3367 Symlink_Path = "";
3368 Symlink_Mount_Point = "";
3369 Backup_Name = Mount_Point.substr(1);
3370 Backup_Path = Mount_Point;
3371 TWPartition* sdext = PartitionManager.Find_Partition_By_Path("/sd-ext");
3372 if (sdext && sdext->Actual_Block_Device == Adopted_Block_Device) {
3373 LOGINFO("Removing /sd-ext from partition list due to adopted storage\n");
3374 PartitionManager.Remove_Partition_By_Path("/sd-ext");
3375 }
Ethan Yonker66a19492015-12-10 10:19:45 -06003376 Setup_Data_Media();
3377 Recreate_Media_Folder();
3378 Wipe_Available_in_GUI = true;
3379 Wipe_During_Factory_Reset = true;
3380 Can_Be_Backed_Up = true;
3381 Can_Encrypt_Backup = true;
3382 Use_Userdata_Encryption = true;
3383 Is_Storage = true;
3384 Storage_Name = "Adopted Storage";
3385 Is_SubPartition = true;
3386 SubPartition_Of = "/data";
3387 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
3388 DataManager::SetValue("tw_has_adopted_storage", 1);
3389 }
3390 } else {
3391 LOGERR("Failed to setup adopted storage decryption\n");
3392 }
3393 }
3394exit:
Matt Mower06543e32017-01-06 15:25:26 -06003395 close(fd);
Ethan Yonker66a19492015-12-10 10:19:45 -06003396 return ret;
3397#else
3398 LOGINFO("Decrypt_Adopted: no crypto support\n");
3399 return 1;
3400#endif
3401}
3402
3403void TWPartition::Revert_Adopted() {
3404#ifdef TW_INCLUDE_CRYPTO
3405 if (!Adopted_GUID.empty()) {
3406 PartitionManager.Remove_MTP_Storage(Mount_Point);
3407 UnMount(false);
3408 cryptfs_revert_ext_volume(Adopted_GUID.c_str());
3409 Is_Adopted_Storage = false;
3410 Is_Encrypted = false;
3411 Is_Decrypted = false;
3412 Decrypted_Block_Device = "";
3413 Find_Actual_Block_Device();
3414 Wipe_During_Factory_Reset = false;
3415 Can_Be_Backed_Up = false;
3416 Can_Encrypt_Backup = false;
3417 Use_Userdata_Encryption = false;
3418 Is_SubPartition = false;
3419 SubPartition_Of = "";
3420 Has_Data_Media = false;
3421 Storage_Path = Mount_Point;
3422 if (!Symlink_Mount_Point.empty()) {
3423 TWPartition* Dat = PartitionManager.Find_Partition_By_Path("/data");
3424 if (Dat) {
3425 Dat->UnMount(false);
3426 Dat->Symlink_Mount_Point = Symlink_Mount_Point;
3427 }
3428 Symlink_Mount_Point = "";
3429 }
3430 }
3431#else
3432 LOGINFO("Revert_Adopted: no crypto support\n");
3433#endif
3434}
bigbiff bigbiffb5ecaad2017-03-20 18:53:53 -04003435
3436void TWPartition::Set_Backup_FileName(string fname) {
3437 Backup_FileName = fname;
3438}
3439
3440string TWPartition::Get_Backup_Name() {
3441 return Backup_Name;
3442}