blob: 646959c3ed02526ef292fe5b5d34376a2cbca7cc [file] [log] [blame]
Dees Troy3be70a82013-10-22 14:25:12 +00001/*
bigbiff bigbiff56cf5642016-08-19 17:43:45 -04002 Copyright 2013 to 2017 TeamWin
Dees Troy3be70a82013-10-22 14:25:12 +00003 This file is part of TWRP/TeamWin Recovery Project.
4
5 TWRP is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 TWRP is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with TWRP. If not, see <http://www.gnu.org/licenses/>.
17*/
Dees_Troy51a0e822012-09-05 15:24:24 -040018
19#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
22#include <sys/stat.h>
23#include <sys/vfs.h>
Dees_Troy5bf43922012-09-07 16:07:55 -040024#include <sys/mount.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040025#include <unistd.h>
Dees_Troy51127312012-09-08 13:08:49 -040026#include <dirent.h>
thatf54e5392016-01-29 22:04:43 +010027#include <libgen.h>
bigbiffce8f83c2015-12-12 18:30:21 -050028#include <zlib.h>
bigbiff bigbiff9c754052013-01-09 09:09:08 -050029#include <iostream>
30#include <sstream>
Ethan Yonker66a19492015-12-10 10:19:45 -060031#include <sys/param.h>
Ethan Yonker472f5062016-02-25 13:47:30 -060032#include <fcntl.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040033
Ethan Yonker007de982018-08-31 14:23:16 -050034#include "cutils/properties.h"
bigbiff7b4c7a62015-01-01 19:44:14 -050035#include "libblkid/include/blkid.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040036#include "variables.h"
Dees_Troy2673cec2013-04-02 20:22:16 +000037#include "twcommon.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040038#include "partitions.hpp"
Dees_Troy5bf43922012-09-07 16:07:55 -040039#include "data.hpp"
Dees_Troy43d8b002012-09-17 16:00:01 -040040#include "twrp-functions.hpp"
bigbiff bigbiff9c754052013-01-09 09:09:08 -050041#include "twrpTar.hpp"
Ethan Yonker3fdcda42016-11-30 12:29:37 -060042#include "exclude.hpp"
Ethan Yonker1b7a31b2014-07-03 15:09:22 -050043#include "infomanager.hpp"
Ethan Yonker4b94cfd2014-12-11 10:00:45 -060044#include "set_metadata.h"
Ethan Yonker74db1572015-10-28 12:44:49 -050045#include "gui/gui.hpp"
bigbiffce8f83c2015-12-12 18:30:21 -050046#include "adbbu/libtwadbbu.hpp"
Ethan Yonker98661c12018-10-17 08:39:28 -050047#ifdef TW_INCLUDE_CRYPTO
48 #include "crypto/fde/cryptfs.h"
49 #ifdef TW_INCLUDE_FBE
50 #include "crypto/ext4crypt/Decrypt.h"
51 #endif
52#else
53 #define CRYPT_FOOTER_OFFSET 0x4000
54#endif
Dees_Troy5bf43922012-09-07 16:07:55 -040055extern "C" {
Dees_Troy38bd7602012-09-14 13:33:53 -040056 #include "mtdutils/mtdutils.h"
57 #include "mtdutils/mounts.h"
Dees_Troya95f55c2013-08-17 13:14:43 +000058#ifdef USE_EXT4
Ethan Yonker8373cfe2017-09-08 06:50:54 -050059 // #include "make_ext4fs.h" TODO need ifdef for android8
60 #include <ext4_utils/make_ext4fs.h>
Dees_Troya95f55c2013-08-17 13:14:43 +000061#endif
Ethan Yonker71413f42014-02-26 13:36:08 -060062#ifdef TW_INCLUDE_CRYPTO
Ethan Yonker66a19492015-12-10 10:19:45 -060063 #include "gpt/gpt.h"
Ethan Yonker71413f42014-02-26 13:36:08 -060064#endif
Dees_Troy5bf43922012-09-07 16:07:55 -040065}
Matt Mower87413642017-01-17 21:14:46 -060066#include <selinux/selinux.h>
Ethan Yonkerf27497f2014-02-09 11:48:33 -060067#include <selinux/label.h>
Dees Troy4159aed2014-02-28 17:24:43 +000068#ifdef HAVE_CAPABILITIES
69#include <sys/capability.h>
70#include <sys/xattr.h>
71#include <linux/xattr.h>
72#endif
HashBanged974bb2016-01-30 14:20:09 -050073#include <sparse_format.h>
Ethan Yonker472f5062016-02-25 13:47:30 -060074#include "progresstracking.hpp"
Dees_Troy51a0e822012-09-05 15:24:24 -040075
bigbiff bigbiff9c754052013-01-09 09:09:08 -050076using namespace std;
77
Ethan Yonker6e8c27a2016-12-22 17:55:57 -060078static int auto_index = 0; // v2 fstab allows you to specify a mount point of "auto" with no /. These items are given a mount point of /auto* where * == auto_index
79
Dees_Troya95f55c2013-08-17 13:14:43 +000080extern struct selabel_handle *selinux_handle;
Ethan Yonker6277c792014-09-15 14:54:30 -050081extern bool datamedia;
Dees_Troya95f55c2013-08-17 13:14:43 +000082
Hashcode62bd9e02013-11-19 21:59:42 -080083struct flag_list {
84 const char *name;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -060085 unsigned long flag;
Hashcode62bd9e02013-11-19 21:59:42 -080086};
87
Matt Mower4ab42b12016-04-21 13:52:18 -050088const struct flag_list mount_flags[] = {
Ethan Yonker6e8c27a2016-12-22 17:55:57 -060089 { "noatime", MS_NOATIME },
90 { "noexec", MS_NOEXEC },
91 { "nosuid", MS_NOSUID },
92 { "nodev", MS_NODEV },
93 { "nodiratime", MS_NODIRATIME },
94 { "ro", MS_RDONLY },
95 { "rw", 0 },
96 { "remount", MS_REMOUNT },
97 { "bind", MS_BIND },
98 { "rec", MS_REC },
Dees Troyc4bc30e2014-02-03 15:04:19 +000099#ifdef MS_UNBINDABLE
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600100 { "unbindable", MS_UNBINDABLE },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000101#endif
102#ifdef MS_PRIVATE
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600103 { "private", MS_PRIVATE },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000104#endif
105#ifdef MS_SLAVE
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600106 { "slave", MS_SLAVE },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000107#endif
108#ifdef MS_SHARED
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600109 { "shared", MS_SHARED },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000110#endif
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600111 { "sync", MS_SYNCHRONOUS },
112 { 0, 0 },
113};
114
115const char *ignored_mount_items[] = {
116 "defaults=",
117 "errors=",
Ethan Yonker93382822018-11-01 15:25:31 -0500118 "latemount",
119 "sysfs_path=",
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600120 NULL
Hashcode62bd9e02013-11-19 21:59:42 -0800121};
122
Matt Mower2416a502016-04-12 19:54:46 -0500123enum TW_FSTAB_FLAGS {
124 TWFLAG_DEFAULTS, // Retain position
125 TWFLAG_ANDSEC,
126 TWFLAG_BACKUP,
127 TWFLAG_BACKUPNAME,
128 TWFLAG_BLOCKSIZE,
129 TWFLAG_CANBEWIPED,
130 TWFLAG_CANENCRYPTBACKUP,
131 TWFLAG_DISPLAY,
132 TWFLAG_ENCRYPTABLE,
Ethan Yonkere9afc3d2018-08-30 15:16:27 -0500133 TWFLAG_FILEENCRYPTION,
Matt Mower2416a502016-04-12 19:54:46 -0500134 TWFLAG_FLASHIMG,
135 TWFLAG_FORCEENCRYPT,
136 TWFLAG_FSFLAGS,
137 TWFLAG_IGNOREBLKID,
138 TWFLAG_LENGTH,
139 TWFLAG_MOUNTTODECRYPT,
140 TWFLAG_REMOVABLE,
141 TWFLAG_RETAINLAYOUTVERSION,
142 TWFLAG_SETTINGSSTORAGE,
143 TWFLAG_STORAGE,
144 TWFLAG_STORAGENAME,
145 TWFLAG_SUBPARTITIONOF,
146 TWFLAG_SYMLINK,
147 TWFLAG_USERDATAENCRYPTBACKUP,
148 TWFLAG_USERMRF,
149 TWFLAG_WIPEDURINGFACTORYRESET,
150 TWFLAG_WIPEINGUI,
Ethan Yonker1b190162016-12-05 15:25:19 -0600151 TWFLAG_SLOTSELECT,
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600152 TWFLAG_WAIT,
153 TWFLAG_VERIFY,
154 TWFLAG_CHECK,
155 TWFLAG_ALTDEVICE,
156 TWFLAG_NOTRIM,
157 TWFLAG_VOLDMANAGED,
158 TWFLAG_FORMATTABLE,
159 TWFLAG_RESIZE,
Ethan Yonker93382822018-11-01 15:25:31 -0500160 TWFLAG_KEYDIRECTORY,
Matt Mower2416a502016-04-12 19:54:46 -0500161};
162
163/* Flags without a trailing '=' are considered dual format flags and can be
164 * written as either 'flagname' or 'flagname=', where the character following
165 * the '=' is Y,y,1 for true and false otherwise.
166 */
167const struct flag_list tw_flags[] = {
168 { "andsec", TWFLAG_ANDSEC },
169 { "backup", TWFLAG_BACKUP },
170 { "backupname=", TWFLAG_BACKUPNAME },
171 { "blocksize=", TWFLAG_BLOCKSIZE },
172 { "canbewiped", TWFLAG_CANBEWIPED },
173 { "canencryptbackup", TWFLAG_CANENCRYPTBACKUP },
174 { "defaults", TWFLAG_DEFAULTS },
175 { "display=", TWFLAG_DISPLAY },
176 { "encryptable=", TWFLAG_ENCRYPTABLE },
Ethan Yonkere9afc3d2018-08-30 15:16:27 -0500177 { "fileencryption=", TWFLAG_FILEENCRYPTION },
Matt Mower2416a502016-04-12 19:54:46 -0500178 { "flashimg", TWFLAG_FLASHIMG },
179 { "forceencrypt=", TWFLAG_FORCEENCRYPT },
180 { "fsflags=", TWFLAG_FSFLAGS },
181 { "ignoreblkid", TWFLAG_IGNOREBLKID },
182 { "length=", TWFLAG_LENGTH },
183 { "mounttodecrypt", TWFLAG_MOUNTTODECRYPT },
184 { "removable", TWFLAG_REMOVABLE },
185 { "retainlayoutversion", TWFLAG_RETAINLAYOUTVERSION },
186 { "settingsstorage", TWFLAG_SETTINGSSTORAGE },
187 { "storage", TWFLAG_STORAGE },
188 { "storagename=", TWFLAG_STORAGENAME },
189 { "subpartitionof=", TWFLAG_SUBPARTITIONOF },
190 { "symlink=", TWFLAG_SYMLINK },
191 { "userdataencryptbackup", TWFLAG_USERDATAENCRYPTBACKUP },
192 { "usermrf", TWFLAG_USERMRF },
193 { "wipeduringfactoryreset", TWFLAG_WIPEDURINGFACTORYRESET },
194 { "wipeingui", TWFLAG_WIPEINGUI },
Ethan Yonker1b190162016-12-05 15:25:19 -0600195 { "slotselect", TWFLAG_SLOTSELECT },
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600196 { "wait", TWFLAG_WAIT },
197 { "verify", TWFLAG_VERIFY },
198 { "check", TWFLAG_CHECK },
199 { "altdevice", TWFLAG_ALTDEVICE },
200 { "notrim", TWFLAG_NOTRIM },
201 { "voldmanaged=", TWFLAG_VOLDMANAGED },
202 { "formattable", TWFLAG_FORMATTABLE },
203 { "resize", TWFLAG_RESIZE },
Ethan Yonker93382822018-11-01 15:25:31 -0500204 { "keydirectory=", TWFLAG_KEYDIRECTORY },
Matt Mower2416a502016-04-12 19:54:46 -0500205 { 0, 0 },
206};
207
that9e0593e2014-10-08 00:01:24 +0200208TWPartition::TWPartition() {
Dees_Troy51a0e822012-09-05 15:24:24 -0400209 Can_Be_Mounted = false;
210 Can_Be_Wiped = false;
Dees_Troya13d74f2013-03-24 08:54:55 -0500211 Can_Be_Backed_Up = false;
Vojtech Bocek1dc30982013-08-30 21:49:30 +0200212 Use_Rm_Rf = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400213 Wipe_During_Factory_Reset = false;
214 Wipe_Available_in_GUI = false;
215 Is_SubPartition = false;
Dees_Troy2691f9d2012-09-24 11:15:49 -0400216 Has_SubPartition = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400217 SubPartition_Of = "";
218 Symlink_Path = "";
219 Symlink_Mount_Point = "";
220 Mount_Point = "";
Dees_Troye58d5262012-09-21 12:27:57 -0400221 Backup_Path = "";
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600222 Wildcard_Block_Device = false;
223 Sysfs_Entry = "";
Dees_Troy38bd7602012-09-14 13:33:53 -0400224 Actual_Block_Device = "";
225 Primary_Block_Device = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400226 Alternate_Block_Device = "";
227 Removable = false;
228 Is_Present = false;
229 Length = 0;
230 Size = 0;
231 Used = 0;
232 Free = 0;
233 Backup_Size = 0;
234 Can_Be_Encrypted = false;
235 Is_Encrypted = false;
236 Is_Decrypted = false;
Ethan Yonkerbd7492d2016-12-07 13:55:01 -0600237 Is_FBE = false;
Ethan Yonker253368a2014-11-25 15:00:52 -0600238 Mount_To_Decrypt = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400239 Decrypted_Block_Device = "";
240 Display_Name = "";
Dees_Troya13d74f2013-03-24 08:54:55 -0500241 Backup_Display_Name = "";
242 Storage_Name = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400243 Backup_Name = "";
Dees_Troy63c8df72012-09-10 14:02:05 -0400244 Backup_FileName = "";
Dees_Troy38bd7602012-09-14 13:33:53 -0400245 MTD_Name = "";
bigbiffce8f83c2015-12-12 18:30:21 -0500246 Backup_Method = BM_NONE;
Dees_Troy83bd4832013-05-04 12:39:56 +0000247 Can_Encrypt_Backup = false;
248 Use_Userdata_Encryption = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400249 Has_Data_Media = false;
Dees_Troye58d5262012-09-21 12:27:57 -0400250 Has_Android_Secure = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400251 Is_Storage = false;
Dees_Troya13d74f2013-03-24 08:54:55 -0500252 Is_Settings_Storage = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400253 Storage_Path = "";
254 Current_File_System = "";
255 Fstab_File_System = "";
Hashcode62bd9e02013-11-19 21:59:42 -0800256 Mount_Flags = 0;
257 Mount_Options = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400258 Format_Block_Size = 0;
Dees_Troy68cab492012-12-12 19:29:35 +0000259 Ignore_Blkid = false;
Dees_Troy16c2b312013-01-15 16:51:18 +0000260 Retain_Layout_Version = false;
dianlujitao4879b372018-12-03 18:45:47 +0800261 Crypto_Key_Location = "";
Ethan Yonker726a0202014-12-16 20:01:38 -0600262 MTP_Storage_ID = 0;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600263 Can_Flash_Img = false;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500264 Mount_Read_Only = false;
Ethan Yonker66a19492015-12-10 10:19:45 -0600265 Is_Adopted_Storage = false;
266 Adopted_GUID = "";
Ethan Yonker1b190162016-12-05 15:25:19 -0600267 SlotSelect = false;
Ethan Yonker93382822018-11-01 15:25:31 -0500268 Key_Directory = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400269}
270
271TWPartition::~TWPartition(void) {
272 // Do nothing
273}
274
Chaosmasterf6e42ce2020-01-27 00:17:04 +0100275bool 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 -0500276 char full_line[MAX_FSTAB_LINE_LENGTH];
Matt Mower2416a502016-04-12 19:54:46 -0500277 char twflags[MAX_FSTAB_LINE_LENGTH] = "";
Dees_Troy5bf43922012-09-07 16:07:55 -0400278 char* ptr;
Matt Mower2b2dd152016-04-26 11:24:08 -0500279 int line_len = strlen(fstab_line), index = 0, item_index = 0;
Dees_Troya13d74f2013-03-24 08:54:55 -0500280 bool skip = false;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600281 int fstab_version = 1, mount_point_index = 0, fs_index = 1, block_device_index = 2;
282 TWPartition *additional_entry = NULL;
283 std::map<string, Flags_Map>::iterator it;
Dees_Troy5bf43922012-09-07 16:07:55 -0400284
Matt Mower2b2dd152016-04-26 11:24:08 -0500285 strlcpy(full_line, fstab_line, sizeof(full_line));
Dees_Troy51127312012-09-08 13:08:49 -0400286 for (index = 0; index < line_len; index++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500287 if (full_line[index] == 34)
288 skip = !skip;
289 if (!skip && full_line[index] <= 32)
Dees_Troy5bf43922012-09-07 16:07:55 -0400290 full_line[index] = '\0';
Dees_Troy5bf43922012-09-07 16:07:55 -0400291 }
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600292 if (line_len < 10)
293 return false; // There can't possibly be a valid fstab line that is less than 10 chars
294 if (strncmp(fstab_line, "/dev/", strlen("/dev/")) == 0 || strncmp(fstab_line, "/devices/", strlen("/devices/")) == 0) {
295 fstab_version = 2;
296 block_device_index = 0;
297 mount_point_index = 1;
298 fs_index = 2;
299 }
300
301 index = 0;
Dees_Troy5bf43922012-09-07 16:07:55 -0400302 while (index < line_len) {
303 while (index < line_len && full_line[index] == '\0')
304 index++;
305 if (index >= line_len)
306 continue;
307 ptr = full_line + index;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600308 if (item_index == mount_point_index) {
309 Mount_Point = ptr;
310 if (fstab_version == 2) {
311 additional_entry = PartitionManager.Find_Partition_By_Path(Mount_Point);
Chaosmasterf6e42ce2020-01-27 00:17:04 +0100312 if (!Sar_Detect && additional_entry) {
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600313 LOGINFO("Found an additional entry for '%s'\n", Mount_Point.c_str());
314 }
315 }
Chaosmasterf6e42ce2020-01-27 00:17:04 +0100316 if(!Sar_Detect)
317 LOGINFO("Processing '%s'\n", Mount_Point.c_str());
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600318 Backup_Path = Mount_Point;
319 Storage_Path = Mount_Point;
320 Display_Name = ptr + 1;
321 Backup_Display_Name = Display_Name;
322 Storage_Name = Display_Name;
323 item_index++;
324 } else if (item_index == fs_index) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400325 // File System
326 Fstab_File_System = ptr;
327 Current_File_System = ptr;
328 item_index++;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600329 } else if (item_index == block_device_index) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400330 // Primary Block Device
Dees_Troy38bd7602012-09-14 13:33:53 -0400331 if (Fstab_File_System == "mtd" || Fstab_File_System == "yaffs2") {
Dees_Troy094207a2012-09-26 12:00:39 -0400332 MTD_Name = ptr;
333 Find_MTD_Block_Device(MTD_Name);
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400334 } else if (Fstab_File_System == "bml") {
335 if (Mount_Point == "/boot")
336 MTD_Name = "boot";
337 else if (Mount_Point == "/recovery")
338 MTD_Name = "recovery";
339 Primary_Block_Device = ptr;
340 if (*ptr != '/')
Dees_Troy2673cec2013-04-02 20:22:16 +0000341 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 -0400342 } else if (*ptr != '/') {
Dees_Troy5bf43922012-09-07 16:07:55 -0400343 if (Display_Error)
Matt Mower2b2dd152016-04-26 11:24:08 -0500344 LOGERR("Invalid block device '%s' in fstab line '%s'", ptr, fstab_line);
Dees_Troy5bf43922012-09-07 16:07:55 -0400345 else
Matt Mower2b2dd152016-04-26 11:24:08 -0500346 LOGINFO("Invalid block device '%s' in fstab line '%s'", ptr, fstab_line);
Matt Mower72c87ce2016-04-26 14:34:56 -0500347 return false;
Dees_Troy38bd7602012-09-14 13:33:53 -0400348 } else {
349 Primary_Block_Device = ptr;
350 Find_Real_Block_Device(Primary_Block_Device, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400351 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400352 item_index++;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600353 } else if (item_index > 2) {
354 if (fstab_version == 2) {
355 if (item_index == 3) {
356 Process_FS_Flags(ptr);
357 if (additional_entry) {
358 additional_entry->Save_FS_Flags(Fstab_File_System, Mount_Flags, Mount_Options);
359 return false; // We save the extra fs flags in the other partition entry and by returning false, this entry will be deleted
360 }
361 } else {
362 strlcpy(twflags, ptr, sizeof(twflags));
363 }
364 item_index++;
365 } else if (*ptr == '/') { // v2 fstab does not allow alternate block devices
Dees_Troy5bf43922012-09-07 16:07:55 -0400366 // Alternate Block Device
367 Alternate_Block_Device = ptr;
368 Find_Real_Block_Device(Alternate_Block_Device, Display_Error);
369 } else if (strlen(ptr) > 7 && strncmp(ptr, "length=", 7) == 0) {
370 // Partition length
371 ptr += 7;
372 Length = atoi(ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400373 } else if (strlen(ptr) > 6 && strncmp(ptr, "flags=", 6) == 0) {
374 // Custom flags, save for later so that new values aren't overwritten by defaults
375 ptr += 6;
Matt Mower2416a502016-04-12 19:54:46 -0500376 strlcpy(twflags, ptr, sizeof(twflags));
Dees_Troy38bd7602012-09-14 13:33:53 -0400377 } else if (strlen(ptr) == 4 && (strncmp(ptr, "NULL", 4) == 0 || strncmp(ptr, "null", 4) == 0 || strncmp(ptr, "null", 4) == 0)) {
378 // Do nothing
Dees_Troy5bf43922012-09-07 16:07:55 -0400379 } else {
380 // Unhandled data
Matt Mower2b2dd152016-04-26 11:24:08 -0500381 LOGINFO("Unhandled fstab information '%s' in fstab line '%s'\n", ptr, fstab_line);
Dees_Troy5bf43922012-09-07 16:07:55 -0400382 }
383 }
384 while (index < line_len && full_line[index] != '\0')
385 index++;
386 }
387
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600388 // override block devices from the v2 fstab with the ones we read from the twrp.flags file in case they are different
389 if (fstab_version == 2 && twrp_flags && twrp_flags->size() > 0) {
390 it = twrp_flags->find(Mount_Point);
391 if (it != twrp_flags->end()) {
392 if (!it->second.Primary_Block_Device.empty()) {
393 Primary_Block_Device = it->second.Primary_Block_Device;
394 Find_Real_Block_Device(Primary_Block_Device, Display_Error);
395 }
396 if (!it->second.Alternate_Block_Device.empty()) {
397 Alternate_Block_Device = it->second.Alternate_Block_Device;
398 Find_Real_Block_Device(Alternate_Block_Device, Display_Error);
399 }
400 }
401 }
402
403 if (strncmp(fstab_line, "/devices/", strlen("/devices/")) == 0) {
404 Sysfs_Entry = Primary_Block_Device;
405 Primary_Block_Device = "";
406 Is_Storage = true;
407 Removable = true;
408 Wipe_Available_in_GUI = true;
409 Wildcard_Block_Device = true;
410 }
411 if (Primary_Block_Device.find("*") != string::npos)
412 Wildcard_Block_Device = true;
413
Chaosmasterf6e42ce2020-01-27 00:17:04 +0100414 if (Sar_Detect) {
415 if(Is_File_System(Fstab_File_System) && (Mount_Point == "/" || Mount_Point == "/system" || Mount_Point == "/system_root"))
416 Find_Actual_Block_Device();
417 else
418 return true;
419 } else if (Mount_Point == "auto") {
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600420 Mount_Point = "/auto";
421 char autoi[5];
422 sprintf(autoi, "%i", auto_index);
423 Mount_Point += autoi;
424 Backup_Path = Mount_Point;
425 Storage_Path = Mount_Point;
426 auto_index++;
427 Setup_File_System(Display_Error);
428 Display_Name = "Storage";
429 Backup_Display_Name = Display_Name;
430 Storage_Name = Display_Name;
431 Can_Be_Backed_Up = false;
432 Wipe_Available_in_GUI = true;
433 Is_Storage = true;
434 Removable = true;
435 Wipe_Available_in_GUI = true;
436 } else if (!Is_File_System(Fstab_File_System) && !Is_Image(Fstab_File_System)) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400437 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000438 LOGERR("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400439 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000440 LOGINFO("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Matt Mower72c87ce2016-04-26 14:34:56 -0500441 return false;
Dees_Troy5bf43922012-09-07 16:07:55 -0400442 } else if (Is_File_System(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400443 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400444 Setup_File_System(Display_Error);
Chaosmaster3b682732020-01-25 21:09:28 +0100445 if (Mount_Point == "/" || Mount_Point == "/system" || Mount_Point == "/system_root") {
Dees_Troy5bf43922012-09-07 16:07:55 -0400446 Display_Name = "System";
Dees_Troya13d74f2013-03-24 08:54:55 -0500447 Backup_Display_Name = Display_Name;
448 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400449 Wipe_Available_in_GUI = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500450 Can_Be_Backed_Up = true;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500451 Mount_Read_Only = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400452 } else if (Mount_Point == "/data") {
453 Display_Name = "Data";
Dees_Troya13d74f2013-03-24 08:54:55 -0500454 Backup_Display_Name = Display_Name;
455 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400456 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400457 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500458 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000459 Can_Encrypt_Backup = true;
460 Use_Userdata_Encryption = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400461 } else if (Mount_Point == "/cache") {
462 Display_Name = "Cache";
Dees_Troya13d74f2013-03-24 08:54:55 -0500463 Backup_Display_Name = Display_Name;
464 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400465 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400466 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500467 Can_Be_Backed_Up = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400468 } else if (Mount_Point == "/datadata") {
Dees_Troy51127312012-09-08 13:08:49 -0400469 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400470 Display_Name = "DataData";
Dees_Troya13d74f2013-03-24 08:54:55 -0500471 Backup_Display_Name = Display_Name;
472 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400473 Is_SubPartition = true;
474 SubPartition_Of = "/data";
Dees_Troy5bf43922012-09-07 16:07:55 -0400475 DataManager::SetValue(TW_HAS_DATADATA, 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500476 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000477 Can_Encrypt_Backup = true;
478 Use_Userdata_Encryption = false; // This whole partition should be encrypted
Dees_Troy5bf43922012-09-07 16:07:55 -0400479 } else if (Mount_Point == "/sd-ext") {
Dees_Troy51127312012-09-08 13:08:49 -0400480 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400481 Display_Name = "SD-Ext";
Dees_Troya13d74f2013-03-24 08:54:55 -0500482 Backup_Display_Name = Display_Name;
483 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400484 Wipe_Available_in_GUI = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400485 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500486 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000487 Can_Encrypt_Backup = true;
488 Use_Userdata_Encryption = true;
Dees_Troy2c50e182012-09-26 20:05:28 -0400489 } else if (Mount_Point == "/boot") {
490 Display_Name = "Boot";
Dees_Troya13d74f2013-03-24 08:54:55 -0500491 Backup_Display_Name = Display_Name;
Dees_Troy2c50e182012-09-26 20:05:28 -0400492 DataManager::SetValue("tw_boot_is_mountable", 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500493 Can_Be_Backed_Up = true;
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500494 } else if (Mount_Point == "/vendor") {
495 Display_Name = "Vendor";
496 Backup_Display_Name = Display_Name;
497 Storage_Name = Display_Name;
498 Mount_Read_Only = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400499 }
500#ifdef TW_EXTERNAL_STORAGE_PATH
501 if (Mount_Point == EXPAND(TW_EXTERNAL_STORAGE_PATH)) {
502 Is_Storage = true;
503 Storage_Path = EXPAND(TW_EXTERNAL_STORAGE_PATH);
Dees_Troyc51f1f92012-09-20 15:32:13 -0400504 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500505 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400506#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000507 if (Mount_Point == "/sdcard" || Mount_Point == "/external_sd" || Mount_Point == "/external_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400508 Is_Storage = true;
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#endif
Dees_Troyb05ddee2013-01-28 20:24:50 +0000512 }
Dees_Troy8170a922012-09-18 15:40:25 -0400513#ifdef TW_INTERNAL_STORAGE_PATH
514 if (Mount_Point == EXPAND(TW_INTERNAL_STORAGE_PATH)) {
515 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500516 Is_Settings_Storage = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400517 Storage_Path = EXPAND(TW_INTERNAL_STORAGE_PATH);
Dees_Troya13d74f2013-03-24 08:54:55 -0500518 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400519 }
520#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000521 if (Mount_Point == "/emmc" || Mount_Point == "/internal_sd" || Mount_Point == "/internal_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400522 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500523 Is_Settings_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500524 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400525 }
526#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400527 } else if (Is_Image(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400528 Find_Actual_Block_Device();
Ethan Yonker1b190162016-12-05 15:25:19 -0600529 Setup_Image();
Dees_Troya13d74f2013-03-24 08:54:55 -0500530 if (Mount_Point == "/boot") {
531 Display_Name = "Boot";
532 Backup_Display_Name = Display_Name;
533 Can_Be_Backed_Up = true;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600534 Can_Flash_Img = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500535 } else if (Mount_Point == "/recovery") {
536 Display_Name = "Recovery";
537 Backup_Display_Name = Display_Name;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600538 Can_Flash_Img = true;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500539 } else if (Mount_Point == "/system_image") {
540 Display_Name = "System Image";
541 Backup_Display_Name = Display_Name;
HashBanged974bb2016-01-30 14:20:09 -0500542 Can_Flash_Img = true;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500543 Can_Be_Backed_Up = true;
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500544 } else if (Mount_Point == "/vendor_image") {
545 Display_Name = "Vendor Image";
546 Backup_Display_Name = Display_Name;
HashBanged974bb2016-01-30 14:20:09 -0500547 Can_Flash_Img = true;
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500548 Can_Be_Backed_Up = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500549 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400550 }
551
Matt Mower2416a502016-04-12 19:54:46 -0500552 // Process TWRP fstab flags
553 if (strlen(twflags) > 0) {
554 string Prev_Display_Name = Display_Name;
555 string Prev_Storage_Name = Storage_Name;
556 string Prev_Backup_Display_Name = Backup_Display_Name;
557 Display_Name = "";
558 Storage_Name = "";
559 Backup_Display_Name = "";
560
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600561 Process_TW_Flags(twflags, (fstab_version == 1), fstab_version);
562 Save_FS_Flags(Fstab_File_System, Mount_Flags, Mount_Options);
Matt Mower2416a502016-04-12 19:54:46 -0500563
564 bool has_display_name = !Display_Name.empty();
565 bool has_storage_name = !Storage_Name.empty();
566 bool has_backup_name = !Backup_Display_Name.empty();
567 if (!has_display_name) Display_Name = Prev_Display_Name;
568 if (!has_storage_name) Storage_Name = Prev_Storage_Name;
569 if (!has_backup_name) Backup_Display_Name = Prev_Backup_Display_Name;
570
571 if (has_display_name && !has_storage_name)
572 Storage_Name = Display_Name;
573 if (!has_display_name && has_storage_name)
574 Display_Name = Storage_Name;
575 if (has_display_name && !has_backup_name && Backup_Display_Name != "Android Secure")
576 Backup_Display_Name = Display_Name;
577 if (!has_display_name && has_backup_name)
578 Display_Name = Backup_Display_Name;
579 }
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600580
581 if (fstab_version == 2 && twrp_flags && twrp_flags->size() > 0) {
582 it = twrp_flags->find(Mount_Point);
583 if (it != twrp_flags->end()) {
584 char twrpflags[MAX_FSTAB_LINE_LENGTH] = "";
585 int skip = 0;
586 string Flags = it->second.Flags;
587 strcpy(twrpflags, Flags.c_str());
588 if (strlen(twrpflags) > strlen("flags=") && strncmp(twrpflags, "flags=", strlen("flags=")) == 0)
589 skip += strlen("flags=");
590 char* flagptr = twrpflags;
591 flagptr += skip;
592 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
593 }
594 }
nkk7198fc3992017-12-16 16:26:42 +0200595
596 if (Mount_Point == "/persist" && Can_Be_Mounted) {
597 bool mounted = Is_Mounted();
598 if (mounted || Mount(false)) {
599 // Read the backup settings file
600 DataManager::LoadPersistValues();
601 TWFunc::Fixup_Time_On_Boot("/persist/time/");
602 if (!mounted)
603 UnMount(false);
604 }
605 }
606
Chaosmasterf6e42ce2020-01-27 00:17:04 +0100607 if (Is_File_System(Fstab_File_System) && (Mount_Point == "/" || Mount_Point == "/system" || Mount_Point == "/system_root")) {
608 if (Sar_Detect) {
609 Mount_Point = "/s";
610 Mount_Read_Only = true;
611 Can_Be_Mounted = true;
612 } else {
613 Mount_Point = PartitionManager.Get_Android_Root_Path();
614 Backup_Path = Mount_Point;
615 Storage_Path = Mount_Point;
616 Make_Dir(Mount_Point, Display_Error);
617 }
618 }
619
Dees_Troy51127312012-09-08 13:08:49 -0400620 return true;
621}
622
Matt Mower72c87ce2016-04-26 14:34:56 -0500623void TWPartition::Partition_Post_Processing(bool Display_Error) {
624 if (Mount_Point == "/data")
625 Setup_Data_Partition(Display_Error);
626 else if (Mount_Point == "/cache")
627 Setup_Cache_Partition(Display_Error);
628}
629
Ethan Yonkerbd7492d2016-12-07 13:55:01 -0600630void TWPartition::ExcludeAll(const string& path) {
631 backup_exclusions.add_absolute_dir(path);
632 wipe_exclusions.add_absolute_dir(path);
633}
634
Matt Mower72c87ce2016-04-26 14:34:56 -0500635void TWPartition::Setup_Data_Partition(bool Display_Error) {
636 if (Mount_Point != "/data")
637 return;
638
639 // Ensure /data is not mounted as tmpfs for qcom hardware decrypt
640 UnMount(false);
641
642#ifdef TW_INCLUDE_CRYPTO
643 if (datamedia)
644 Setup_Data_Media();
645 Can_Be_Encrypted = true;
646 char crypto_blkdev[255];
647 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
648 if (strcmp(crypto_blkdev, "error") != 0) {
649 DataManager::SetValue(TW_IS_DECRYPTED, 1);
650 Is_Encrypted = true;
651 Is_Decrypted = true;
Ethan Yonker93382822018-11-01 15:25:31 -0500652 if (Key_Directory.empty())
653 Is_FBE = false;
654 else
655 Is_FBE = true;
Ethan Yonkerbd7492d2016-12-07 13:55:01 -0600656 DataManager::SetValue(TW_IS_FBE, 0);
Matt Mower72c87ce2016-04-26 14:34:56 -0500657 Decrypted_Block_Device = crypto_blkdev;
658 LOGINFO("Data already decrypted, new block device: '%s'\n", crypto_blkdev);
659 } else if (!Mount(false)) {
660 if (Is_Present) {
Ethan Yonker93382822018-11-01 15:25:31 -0500661 if (Key_Directory.empty()) {
662 set_partition_data(Actual_Block_Device.c_str(), Crypto_Key_Location.c_str(), Fstab_File_System.c_str());
663 if (cryptfs_check_footer() == 0) {
664 Is_Encrypted = true;
665 Is_Decrypted = false;
666 Can_Be_Mounted = false;
667 Current_File_System = "emmc";
668 Setup_Image();
669 DataManager::SetValue(TW_IS_ENCRYPTED, 1);
670 DataManager::SetValue(TW_CRYPTO_PWTYPE, cryptfs_get_password_type());
671 DataManager::SetValue(TW_CRYPTO_PASSWORD, "");
672 DataManager::SetValue("tw_crypto_display", "");
673 } else {
674 gui_err("mount_data_footer=Could not mount /data and unable to find crypto footer.");
675 }
676 } else {
Matt Mower72c87ce2016-04-26 14:34:56 -0500677 Is_Encrypted = true;
678 Is_Decrypted = false;
Matt Mower72c87ce2016-04-26 14:34:56 -0500679 }
Ethan Yonker93382822018-11-01 15:25:31 -0500680 } else if (Key_Directory.empty()) {
Matt Mower72c87ce2016-04-26 14:34:56 -0500681 LOGERR("Primary block device '%s' for mount point '%s' is not present!\n", Primary_Block_Device.c_str(), Mount_Point.c_str());
682 }
683 } else {
mauronofrio9bf73352019-11-23 22:27:34 +0100684
685 if (!Decrypt_FBE_DE()) {
686 LOGINFO("Trying wrapped key.\n");
687 property_set("fbe.data.wrappedkey", "true");
688 if (!Decrypt_FBE_DE()) {
Mauronofrio Matarrese6d5d08a2019-12-03 14:25:25 +0100689 LOGINFO("Unable to decrypt device\n");
mauronofrio9bf73352019-11-23 22:27:34 +0100690 }
691 }
Matt Mower72c87ce2016-04-26 14:34:56 -0500692 }
693 if (datamedia && (!Is_Encrypted || (Is_Encrypted && Is_Decrypted))) {
694 Setup_Data_Media();
695 Recreate_Media_Folder();
696 }
697#else
698 if (datamedia) {
699 Setup_Data_Media();
700 Recreate_Media_Folder();
701 }
702#endif
703}
704
Ethan Yonker93382822018-11-01 15:25:31 -0500705bool TWPartition::Decrypt_FBE_DE() {
706if (TWFunc::Path_Exists("/data/unencrypted/key/version")) {
707 LOGINFO("File Based Encryption is present\n");
708#ifdef TW_INCLUDE_FBE
709 ExcludeAll(Mount_Point + "/convert_fbe");
710 ExcludeAll(Mount_Point + "/unencrypted");
711 //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
712 ExcludeAll(Mount_Point + "/misc/vold/user_keys");
713 //ExcludeAll(Mount_Point + "/system_ce");
714 //ExcludeAll(Mount_Point + "/system_de");
715 //ExcludeAll(Mount_Point + "/misc_ce");
716 //ExcludeAll(Mount_Point + "/misc_de");
717 ExcludeAll(Mount_Point + "/system/gatekeeper.password.key");
718 ExcludeAll(Mount_Point + "/system/gatekeeper.pattern.key");
719 ExcludeAll(Mount_Point + "/system/locksettings.db");
720 //ExcludeAll(Mount_Point + "/system/locksettings.db-shm"); // don't seem to need this one, but the other 2 are needed
721 ExcludeAll(Mount_Point + "/system/locksettings.db-wal");
722 //ExcludeAll(Mount_Point + "/user_de");
723 //ExcludeAll(Mount_Point + "/misc/profiles/cur/0"); // might be important later
724 ExcludeAll(Mount_Point + "/misc/gatekeeper");
725 ExcludeAll(Mount_Point + "/misc/keystore");
726 ExcludeAll(Mount_Point + "/drm/kek.dat");
727 ExcludeAll(Mount_Point + "/system_de/0/spblob"); // contains data needed to decrypt pixel 2
728 int retry_count = 3;
729 while (!Decrypt_DE() && --retry_count)
730 usleep(2000);
731 if (retry_count > 0) {
732 property_set("ro.crypto.state", "encrypted");
733 Is_Encrypted = true;
734 Is_Decrypted = false;
735 Is_FBE = true;
736 DataManager::SetValue(TW_IS_FBE, 1);
737 DataManager::SetValue(TW_IS_ENCRYPTED, 1);
738 string filename;
739 int pwd_type = Get_Password_Type(0, filename);
740 if (pwd_type < 0) {
741 LOGERR("This TWRP does not have synthetic password decrypt support\n");
742 pwd_type = 0; // default password
743 }
744 DataManager::SetValue(TW_CRYPTO_PWTYPE, pwd_type);
745 DataManager::SetValue(TW_CRYPTO_PASSWORD, "");
746 DataManager::SetValue("tw_crypto_display", "");
747 return true;
748 }
749#else
750 LOGERR("FBE found but FBE support not present in TWRP\n");
751#endif
752 }
753 return false;
754}
755
Matt Mower72c87ce2016-04-26 14:34:56 -0500756void TWPartition::Setup_Cache_Partition(bool Display_Error __unused) {
757 if (Mount_Point != "/cache")
758 return;
759
760 if (!Mount(true))
761 return;
762
763 if (!TWFunc::Path_Exists("/cache/recovery/.")) {
764 LOGINFO("Recreating /cache/recovery folder\n");
765 if (mkdir("/cache/recovery", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0)
766 LOGERR("Could not create /cache/recovery\n");
767 }
768}
769
Matt Mower4ab42b12016-04-21 13:52:18 -0500770void TWPartition::Process_FS_Flags(const char *str) {
771 char *options = strdup(str);
772 char *ptr, *savep;
Hashcode62bd9e02013-11-19 21:59:42 -0800773
Matt Mower4ab42b12016-04-21 13:52:18 -0500774 Mount_Options = "";
Hashcode62bd9e02013-11-19 21:59:42 -0800775
Matt Mower4ab42b12016-04-21 13:52:18 -0500776 // Avoid issues with potentially nested strtok by using strtok_r
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600777 for (ptr = strtok_r(options, ",", &savep); ptr; ptr = strtok_r(NULL, ",", &savep)) {
778 char *equals = strstr(ptr, "=");
779 size_t name_len;
Matt Mower4ab42b12016-04-21 13:52:18 -0500780
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600781 if (!equals)
782 name_len = strlen(ptr);
783 else
784 name_len = equals - ptr;
785
786 // There are some flags that we want to ignore in TWRP
787 bool found_match = false;
788 for (const char** ignored_mount_item = ignored_mount_items; *ignored_mount_item; ignored_mount_item++) {
789 if (strncmp(ptr, *ignored_mount_item, name_len) == 0) {
790 found_match = true;
Hashcode62bd9e02013-11-19 21:59:42 -0800791 break;
792 }
793 }
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600794 if (found_match)
795 continue;
Hashcode62bd9e02013-11-19 21:59:42 -0800796
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600797 // mount_flags are never postfixed by '='
798 if (!equals) {
799 const struct flag_list* mount_flag = mount_flags;
800 for (; mount_flag->name; mount_flag++) {
801 if (strcmp(ptr, mount_flag->name) == 0) {
802 if (mount_flag->flag == MS_RDONLY)
803 Mount_Read_Only = true;
804 else
805 Mount_Flags |= (unsigned)mount_flag->flag;
806 found_match = true;
807 break;
808 }
809 }
810 if (found_match)
811 continue;
Matt Mower4ab42b12016-04-21 13:52:18 -0500812 }
813
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600814 // If we aren't ignoring this flag and it's not a mount flag, then it must be a mount option
815 if (!Mount_Options.empty())
816 Mount_Options += ",";
817 Mount_Options += ptr;
Matt Mower4ab42b12016-04-21 13:52:18 -0500818 }
819 free(options);
Hashcode62bd9e02013-11-19 21:59:42 -0800820}
821
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600822void TWPartition::Save_FS_Flags(const string& local_File_System, int local_Mount_Flags, const string& local_Mount_Options) {
823 partition_fs_flags_struct flags;
824 flags.File_System = local_File_System;
825 flags.Mount_Flags = local_Mount_Flags;
826 flags.Mount_Options = local_Mount_Options;
827 fs_flags.push_back(flags);
828}
829
Matt Mower2416a502016-04-12 19:54:46 -0500830void TWPartition::Apply_TW_Flag(const unsigned flag, const char* str, const bool val) {
831 switch (flag) {
832 case TWFLAG_ANDSEC:
833 Has_Android_Secure = val;
834 break;
835 case TWFLAG_BACKUP:
836 Can_Be_Backed_Up = val;
837 break;
838 case TWFLAG_BACKUPNAME:
839 Backup_Display_Name = str;
840 break;
841 case TWFLAG_BLOCKSIZE:
842 Format_Block_Size = (unsigned long)(atol(str));
843 break;
844 case TWFLAG_CANBEWIPED:
845 Can_Be_Wiped = val;
846 break;
847 case TWFLAG_CANENCRYPTBACKUP:
848 Can_Encrypt_Backup = val;
849 break;
850 case TWFLAG_DEFAULTS:
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600851 case TWFLAG_WAIT:
852 case TWFLAG_VERIFY:
853 case TWFLAG_CHECK:
854 case TWFLAG_NOTRIM:
855 case TWFLAG_VOLDMANAGED:
856 case TWFLAG_RESIZE:
Matt Mower2416a502016-04-12 19:54:46 -0500857 // Do nothing
858 break;
859 case TWFLAG_DISPLAY:
860 Display_Name = str;
861 break;
862 case TWFLAG_ENCRYPTABLE:
863 case TWFLAG_FORCEENCRYPT:
864 Crypto_Key_Location = str;
865 break;
Ethan Yonkere9afc3d2018-08-30 15:16:27 -0500866 case TWFLAG_FILEENCRYPTION:
867 // This flag isn't used by TWRP but is needed in 9.0 FBE decrypt
868 // fileencryption=ice:aes-256-heh
869 {
870 std::string FBE = str;
Ethan Yonkere9afc3d2018-08-30 15:16:27 -0500871 size_t colon_loc = FBE.find(":");
872 if (colon_loc == std::string::npos) {
Ethan Yonker93382822018-11-01 15:25:31 -0500873 property_set("fbe.contents", FBE.c_str());
874 property_set("fbe.filenames", "");
875 LOGINFO("FBE contents '%s', filenames ''\n", FBE.c_str());
Ethan Yonkere9afc3d2018-08-30 15:16:27 -0500876 break;
877 }
Ethan Yonker93382822018-11-01 15:25:31 -0500878 std::string FBE_contents, FBE_filenames;
Ethan Yonkere9afc3d2018-08-30 15:16:27 -0500879 FBE_contents = FBE.substr(0, colon_loc);
880 FBE_filenames = FBE.substr(colon_loc + 1);
881 property_set("fbe.contents", FBE_contents.c_str());
882 property_set("fbe.filenames", FBE_filenames.c_str());
883 LOGINFO("FBE contents '%s', filenames '%s'\n", FBE_contents.c_str(), FBE_filenames.c_str());
884 }
885 break;
Matt Mower2416a502016-04-12 19:54:46 -0500886 case TWFLAG_FLASHIMG:
887 Can_Flash_Img = val;
888 break;
889 case TWFLAG_FSFLAGS:
Matt Mower4ab42b12016-04-21 13:52:18 -0500890 Process_FS_Flags(str);
Matt Mower2416a502016-04-12 19:54:46 -0500891 break;
892 case TWFLAG_IGNOREBLKID:
893 Ignore_Blkid = val;
894 break;
895 case TWFLAG_LENGTH:
896 Length = atoi(str);
897 break;
898 case TWFLAG_MOUNTTODECRYPT:
899 Mount_To_Decrypt = val;
900 break;
901 case TWFLAG_REMOVABLE:
902 Removable = val;
903 break;
904 case TWFLAG_RETAINLAYOUTVERSION:
905 Retain_Layout_Version = val;
906 break;
907 case TWFLAG_SETTINGSSTORAGE:
908 Is_Settings_Storage = val;
909 if (Is_Settings_Storage)
910 Is_Storage = true;
911 break;
912 case TWFLAG_STORAGE:
913 Is_Storage = val;
914 break;
915 case TWFLAG_STORAGENAME:
916 Storage_Name = str;
917 break;
918 case TWFLAG_SUBPARTITIONOF:
919 Is_SubPartition = true;
920 SubPartition_Of = str;
921 break;
922 case TWFLAG_SYMLINK:
923 Symlink_Path = str;
924 break;
925 case TWFLAG_USERDATAENCRYPTBACKUP:
926 Use_Userdata_Encryption = val;
927 if (Use_Userdata_Encryption)
928 Can_Encrypt_Backup = true;
929 break;
930 case TWFLAG_USERMRF:
931 Use_Rm_Rf = val;
932 break;
933 case TWFLAG_WIPEDURINGFACTORYRESET:
934 Wipe_During_Factory_Reset = val;
935 if (Wipe_During_Factory_Reset) {
936 Can_Be_Wiped = true;
937 Wipe_Available_in_GUI = true;
938 }
939 break;
940 case TWFLAG_WIPEINGUI:
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600941 case TWFLAG_FORMATTABLE:
Matt Mower2416a502016-04-12 19:54:46 -0500942 Wipe_Available_in_GUI = val;
943 if (Wipe_Available_in_GUI)
944 Can_Be_Wiped = true;
945 break;
Ethan Yonker1b190162016-12-05 15:25:19 -0600946 case TWFLAG_SLOTSELECT:
947 SlotSelect = true;
948 break;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600949 case TWFLAG_ALTDEVICE:
950 Alternate_Block_Device = str;
951 break;
Ethan Yonker93382822018-11-01 15:25:31 -0500952 case TWFLAG_KEYDIRECTORY:
953 Key_Directory = str;
Matt Mower2416a502016-04-12 19:54:46 -0500954 default:
955 // Should not get here
956 LOGINFO("Flag identified for processing, but later unmatched: %i\n", flag);
957 break;
958 }
959}
Dees_Troy51127312012-09-08 13:08:49 -0400960
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600961void TWPartition::Process_TW_Flags(char *flags, bool Display_Error, int fstab_ver) {
Matt Mower2416a502016-04-12 19:54:46 -0500962 char separator[2] = {'\n', 0};
963 char *ptr, *savep;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600964 char source_separator = ';';
965
966 if (fstab_ver == 2)
967 source_separator = ',';
Matt Mower2416a502016-04-12 19:54:46 -0500968
969 // Semicolons within double-quotes are not forbidden, so replace
970 // only the semicolons intended as separators with '\n' for strtok
971 for (unsigned i = 0, skip = 0; i < strlen(flags); i++) {
972 if (flags[i] == '\"')
Dees_Troya13d74f2013-03-24 08:54:55 -0500973 skip = !skip;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600974 if (!skip && flags[i] == source_separator)
Matt Mower2416a502016-04-12 19:54:46 -0500975 flags[i] = separator[0];
Dees_Troy51127312012-09-08 13:08:49 -0400976 }
977
Matt Mower2416a502016-04-12 19:54:46 -0500978 // Avoid issues with potentially nested strtok by using strtok_r
979 ptr = strtok_r(flags, separator, &savep);
980 while (ptr) {
981 int ptr_len = strlen(ptr);
982 const struct flag_list* tw_flag = tw_flags;
Hashcode62bd9e02013-11-19 21:59:42 -0800983
Matt Mower2416a502016-04-12 19:54:46 -0500984 for (; tw_flag->name; tw_flag++) {
985 int flag_len = strlen(tw_flag->name);
986
987 if (strncmp(ptr, tw_flag->name, flag_len) == 0) {
988 bool flag_val = false;
989
990 if (ptr_len > flag_len && (tw_flag->name)[flag_len-1] != '='
991 && ptr[flag_len] != '=') {
992 // Handle flags with same starting string
993 // (e.g. backup and backupname)
994 continue;
995 } else if (ptr_len > flag_len && ptr[flag_len] == '=') {
996 // Handle flags with dual format: Part 1
997 // (e.g. backup and backup=y. backup=y handled here)
998 ptr += flag_len + 1;
999 TWFunc::Strip_Quotes(ptr);
1000 // Skip flags with empty argument
1001 // (e.g. backup=)
1002 if (strlen(ptr) == 0) {
1003 LOGINFO("Flag missing argument or should not include '=': %s=\n", tw_flag->name);
1004 break;
1005 }
1006 flag_val = strchr("yY1", *ptr) != NULL;
1007 } else if (ptr_len == flag_len
1008 && (tw_flag->name)[flag_len-1] == '=') {
1009 // Skip flags missing argument after =
1010 // (e.g. backupname=)
1011 LOGINFO("Flag missing argument: %s\n", tw_flag->name);
1012 break;
1013 } else if (ptr_len > flag_len
1014 && (tw_flag->name)[flag_len-1] == '=') {
1015 // Handle arguments to flags
1016 // (e.g. backupname="My Stuff")
1017 ptr += flag_len;
1018 TWFunc::Strip_Quotes(ptr);
1019 // Skip flags with empty argument
1020 // (e.g. backupname="")
1021 if (strlen(ptr) == 0) {
1022 LOGINFO("Flag missing argument: %s\n", tw_flag->name);
1023 break;
1024 }
1025 } else if (ptr_len == flag_len) {
1026 // Handle flags with dual format: Part 2
1027 // (e.g. backup and backup=y. backup handled here)
1028 flag_val = true;
Ethan Yonker96af84a2015-01-05 14:58:36 -06001029 } else {
Matt Mower2416a502016-04-12 19:54:46 -05001030 LOGINFO("Flag matched, but could not be processed: %s\n", ptr);
1031 break;
Ethan Yonker96af84a2015-01-05 14:58:36 -06001032 }
Matt Mower2416a502016-04-12 19:54:46 -05001033
1034 Apply_TW_Flag(tw_flag->flag, ptr, flag_val);
1035 break;
Ethan Yonker96af84a2015-01-05 14:58:36 -06001036 }
Matt Mower2416a502016-04-12 19:54:46 -05001037 }
1038 if (tw_flag->name == 0) {
Dees_Troy51127312012-09-08 13:08:49 -04001039 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001040 LOGERR("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -04001041 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001042 LOGINFO("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -04001043 }
Matt Mower2416a502016-04-12 19:54:46 -05001044 ptr = strtok_r(NULL, separator, &savep);
Dees_Troy51127312012-09-08 13:08:49 -04001045 }
Dees_Troy51a0e822012-09-05 15:24:24 -04001046}
1047
Dees_Troy5bf43922012-09-07 16:07:55 -04001048bool TWPartition::Is_File_System(string File_System) {
1049 if (File_System == "ext2" ||
Dees_Troy63c8df72012-09-10 14:02:05 -04001050 File_System == "ext3" ||
Dees_Troy5bf43922012-09-07 16:07:55 -04001051 File_System == "ext4" ||
1052 File_System == "vfat" ||
1053 File_System == "ntfs" ||
1054 File_System == "yaffs2" ||
bigbiff bigbiff3e146522012-11-14 14:32:59 -05001055 File_System == "exfat" ||
Dees_Troye5017042013-08-29 16:38:55 +00001056 File_System == "f2fs" ||
Erfan Abdib859e912019-02-05 18:18:46 +02001057 File_System == "squashfs" ||
Dees_Troy5bf43922012-09-07 16:07:55 -04001058 File_System == "auto")
1059 return true;
1060 else
1061 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001062}
1063
Dees_Troy5bf43922012-09-07 16:07:55 -04001064bool TWPartition::Is_Image(string File_System) {
Dees_Troy5fcd8f92012-10-16 12:22:05 -04001065 if (File_System == "emmc" || File_System == "mtd" || File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -04001066 return true;
1067 else
1068 return false;
1069}
1070
Dees_Troy51127312012-09-08 13:08:49 -04001071bool TWPartition::Make_Dir(string Path, bool Display_Error) {
Ethan Yonker93ac7a02016-11-07 22:05:58 -06001072 if (TWFunc::Get_D_Type_From_Stat(Path) != S_IFDIR)
1073 unlink(Path.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001074 if (!TWFunc::Path_Exists(Path)) {
Dees_Troy51127312012-09-08 13:08:49 -04001075 if (mkdir(Path.c_str(), 0777) == -1) {
1076 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001077 gui_msg(Msg(msg::kError, "create_folder_strerr=Can not create '{1}' folder ({2}).")(Path)(strerror(errno)));
Dees_Troy51127312012-09-08 13:08:49 -04001078 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001079 LOGINFO("Can not create '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -04001080 return false;
1081 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001082 LOGINFO("Created '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -04001083 return true;
1084 }
1085 }
1086 return true;
1087}
1088
Dees_Troy5bf43922012-09-07 16:07:55 -04001089void TWPartition::Setup_File_System(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001090 Can_Be_Mounted = true;
1091 Can_Be_Wiped = true;
1092
Dees_Troy5bf43922012-09-07 16:07:55 -04001093 // Make the mount point folder if it doesn't exist
Dees_Troy51127312012-09-08 13:08:49 -04001094 Make_Dir(Mount_Point, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -04001095 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
1096 Backup_Name = Display_Name;
bigbiffce8f83c2015-12-12 18:30:21 -05001097 Backup_Method = BM_FILES;
Dees_Troy5bf43922012-09-07 16:07:55 -04001098}
1099
Ethan Yonker1b190162016-12-05 15:25:19 -06001100void TWPartition::Setup_Image() {
Dees_Troy5bf43922012-09-07 16:07:55 -04001101 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
1102 Backup_Name = Display_Name;
Gary Peck82599a82012-11-21 16:23:12 -08001103 if (Current_File_System == "emmc")
bigbiffce8f83c2015-12-12 18:30:21 -05001104 Backup_Method = BM_DD;
Gary Peck82599a82012-11-21 16:23:12 -08001105 else if (Current_File_System == "mtd" || Current_File_System == "bml")
bigbiffce8f83c2015-12-12 18:30:21 -05001106 Backup_Method = BM_FLASH_UTILS;
Dees_Troy5bf43922012-09-07 16:07:55 -04001107 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001108 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 -04001109}
1110
Dees_Troye58d5262012-09-21 12:27:57 -04001111void TWPartition::Setup_AndSec(void) {
Dees_Troya13d74f2013-03-24 08:54:55 -05001112 Backup_Display_Name = "Android Secure";
Dees_Troye58d5262012-09-21 12:27:57 -04001113 Backup_Name = "and-sec";
Dees_Troya13d74f2013-03-24 08:54:55 -05001114 Can_Be_Backed_Up = true;
Dees_Troye58d5262012-09-21 12:27:57 -04001115 Has_Android_Secure = true;
1116 Symlink_Path = Mount_Point + "/.android_secure";
1117 Symlink_Mount_Point = "/and-sec";
1118 Backup_Path = Symlink_Mount_Point;
1119 Make_Dir("/and-sec", true);
1120 Recreate_AndSec_Folder();
Kjell Braden3126a112016-06-19 16:58:15 +00001121 Mount_Storage_Retry(true);
Dees_Troye58d5262012-09-21 12:27:57 -04001122}
1123
that9e0593e2014-10-08 00:01:24 +02001124void TWPartition::Setup_Data_Media() {
Ethan Yonker6277c792014-09-15 14:54:30 -05001125 LOGINFO("Setting up '%s' as data/media emulated storage.\n", Mount_Point.c_str());
Ethan Yonker66a19492015-12-10 10:19:45 -06001126 if (Storage_Name.empty() || Storage_Name == "Data")
1127 Storage_Name = "Internal Storage";
Ethan Yonker6277c792014-09-15 14:54:30 -05001128 Has_Data_Media = true;
1129 Is_Storage = true;
Ethan Yonker66a19492015-12-10 10:19:45 -06001130 Storage_Path = Mount_Point + "/media";
Ethan Yonker6277c792014-09-15 14:54:30 -05001131 Symlink_Path = Storage_Path;
Ethan Yonker66a19492015-12-10 10:19:45 -06001132 if (Mount_Point == "/data") {
1133 Is_Settings_Storage = true;
1134 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
1135 Make_Dir("/emmc", false);
1136 Symlink_Mount_Point = "/emmc";
1137 } else {
1138 Make_Dir("/sdcard", false);
1139 Symlink_Mount_Point = "/sdcard";
1140 }
1141 if (Mount(false) && TWFunc::Path_Exists(Mount_Point + "/media/0")) {
1142 Storage_Path = Mount_Point + "/media/0";
1143 Symlink_Path = Storage_Path;
1144 DataManager::SetValue(TW_INTERNAL_PATH, Mount_Point + "/media/0");
1145 UnMount(true);
1146 }
1147 DataManager::SetValue("tw_has_internal", 1);
1148 DataManager::SetValue("tw_has_data_media", 1);
Ethan Yonker3fdcda42016-11-30 12:29:37 -06001149 backup_exclusions.add_absolute_dir("/data/data/com.google.android.music/files");
Darth90864aa52020-01-14 20:58:16 +00001150 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 -05001151 wipe_exclusions.add_absolute_dir(Mount_Point + "/misc/vold"); // adopted storage keys
Ethan Yonker79f88bd2016-12-09 14:52:12 -06001152 ExcludeAll(Mount_Point + "/.layout_version");
1153 ExcludeAll(Mount_Point + "/system/storage.xml");
Ethan Yonker6277c792014-09-15 14:54:30 -05001154 } else {
Ethan Yonker66a19492015-12-10 10:19:45 -06001155 if (Mount(true) && TWFunc::Path_Exists(Mount_Point + "/media/0")) {
1156 Storage_Path = Mount_Point + "/media/0";
1157 Symlink_Path = Storage_Path;
1158 UnMount(true);
1159 }
Ethan Yonker6277c792014-09-15 14:54:30 -05001160 }
Ethan Yonker79f88bd2016-12-09 14:52:12 -06001161 ExcludeAll(Mount_Point + "/media");
Ethan Yonker6277c792014-09-15 14:54:30 -05001162}
1163
Dees_Troy5bf43922012-09-07 16:07:55 -04001164void TWPartition::Find_Real_Block_Device(string& Block, bool Display_Error) {
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06001165 char device[PATH_MAX], realDevice[PATH_MAX];
Dees_Troy5bf43922012-09-07 16:07:55 -04001166
1167 strcpy(device, Block.c_str());
1168 memset(realDevice, 0, sizeof(realDevice));
1169 while (readlink(device, realDevice, sizeof(realDevice)) > 0)
1170 {
1171 strcpy(device, realDevice);
1172 memset(realDevice, 0, sizeof(realDevice));
1173 }
1174
1175 if (device[0] != '/') {
1176 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001177 LOGERR("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001178 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001179 LOGINFO("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001180 return;
Dees_Troy51a0e822012-09-05 15:24:24 -04001181 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -04001182 Block = device;
1183 return;
1184 }
1185}
1186
Kjell Braden3126a112016-06-19 16:58:15 +00001187bool TWPartition::Mount_Storage_Retry(bool Display_Error) {
Dees_Troy8e337f32012-10-13 22:07:49 -04001188 // On some devices, storage doesn't want to mount right away, retry and sleep
Kjell Braden3126a112016-06-19 16:58:15 +00001189 if (!Mount(Display_Error)) {
Dees_Troy8e337f32012-10-13 22:07:49 -04001190 int retry_count = 5;
1191 while (retry_count > 0 && !Mount(false)) {
1192 usleep(500000);
1193 retry_count--;
1194 }
Kjell Braden3126a112016-06-19 16:58:15 +00001195 return Mount(Display_Error);
Dees_Troy8e337f32012-10-13 22:07:49 -04001196 }
Kjell Braden3126a112016-06-19 16:58:15 +00001197 return true;
Dees_Troy8e337f32012-10-13 22:07:49 -04001198}
1199
Dees_Troy38bd7602012-09-14 13:33:53 -04001200bool TWPartition::Find_MTD_Block_Device(string MTD_Name) {
1201 FILE *fp = NULL;
1202 char line[255];
1203
1204 fp = fopen("/proc/mtd", "rt");
1205 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001206 LOGERR("Device does not support /proc/mtd\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001207 return false;
1208 }
1209
1210 while (fgets(line, sizeof(line), fp) != NULL)
1211 {
1212 char device[32], label[32];
1213 unsigned long size = 0;
Dees_Troy38bd7602012-09-14 13:33:53 -04001214 int deviceId;
1215
1216 sscanf(line, "%s %lx %*s %*c%s", device, &size, label);
1217
1218 // Skip header and blank lines
1219 if ((strcmp(device, "dev:") == 0) || (strlen(line) < 8))
1220 continue;
1221
1222 // Strip off the trailing " from the label
1223 label[strlen(label)-1] = '\0';
1224
1225 if (strcmp(label, MTD_Name.c_str()) == 0) {
1226 // We found our device
1227 // Strip off the trailing : from the device
1228 device[strlen(device)-1] = '\0';
1229 if (sscanf(device,"mtd%d", &deviceId) == 1) {
1230 sprintf(device, "/dev/block/mtdblock%d", deviceId);
1231 Primary_Block_Device = device;
Dees_Troy76543db2013-06-19 16:24:30 +00001232 fclose(fp);
1233 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -04001234 }
1235 }
1236 }
1237 fclose(fp);
1238
1239 return false;
1240}
1241
Dees_Troy51127312012-09-08 13:08:49 -04001242bool TWPartition::Get_Size_Via_statfs(bool Display_Error) {
1243 struct statfs st;
1244 string Local_Path = Mount_Point + "/.";
1245
1246 if (!Mount(Display_Error))
1247 return false;
1248
1249 if (statfs(Local_Path.c_str(), &st) != 0) {
1250 if (!Removable) {
1251 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001252 LOGERR("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -04001253 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001254 LOGINFO("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -04001255 }
1256 return false;
1257 }
1258 Size = (st.f_blocks * st.f_bsize);
1259 Used = ((st.f_blocks - st.f_bfree) * st.f_bsize);
1260 Free = (st.f_bfree * st.f_bsize);
1261 Backup_Size = Used;
1262 return true;
1263}
1264
1265bool TWPartition::Get_Size_Via_df(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001266 FILE* fp;
1267 char command[255], line[512];
1268 int include_block = 1;
1269 unsigned int min_len;
1270
1271 if (!Mount(Display_Error))
1272 return false;
1273
Dees_Troy38bd7602012-09-14 13:33:53 -04001274 min_len = Actual_Block_Device.size() + 2;
Dees_Troy51127312012-09-08 13:08:49 -04001275 sprintf(command, "df %s > /tmp/dfoutput.txt", Mount_Point.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001276 TWFunc::Exec_Cmd(command);
Dees_Troy51127312012-09-08 13:08:49 -04001277 fp = fopen("/tmp/dfoutput.txt", "rt");
1278 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001279 LOGINFO("Unable to open /tmp/dfoutput.txt.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001280 return false;
Dees_Troy51127312012-09-08 13:08:49 -04001281 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001282
1283 while (fgets(line, sizeof(line), fp) != NULL)
1284 {
1285 unsigned long blocks, used, available;
1286 char device[64];
1287 char tmpString[64];
1288
1289 if (strncmp(line, "Filesystem", 10) == 0)
1290 continue;
1291 if (strlen(line) < min_len) {
1292 include_block = 0;
1293 continue;
1294 }
1295 if (include_block) {
1296 sscanf(line, "%s %lu %lu %lu", device, &blocks, &used, &available);
1297 } else {
1298 // The device block string is so long that the df information is on the next line
1299 int space_count = 0;
Dees_Troye58d5262012-09-21 12:27:57 -04001300 sprintf(tmpString, "/dev/block/%s", Actual_Block_Device.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001301 while (tmpString[space_count] == 32)
1302 space_count++;
1303 sscanf(line + space_count, "%lu %lu %lu", &blocks, &used, &available);
1304 }
1305
1306 // Adjust block size to byte size
1307 Size = blocks * 1024ULL;
1308 Used = used * 1024ULL;
1309 Free = available * 1024ULL;
1310 Backup_Size = Used;
1311 }
1312 fclose(fp);
1313 return true;
1314}
1315
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001316unsigned long long TWPartition::IOCTL_Get_Block_Size() {
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001317 Find_Actual_Block_Device();
Ethan Yonker483e9f42016-01-11 22:21:18 -06001318
1319 return TWFunc::IOCTL_Get_Block_Size(Actual_Block_Device.c_str());
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001320}
1321
Dees_Troy5bf43922012-09-07 16:07:55 -04001322bool TWPartition::Find_Partition_Size(void) {
1323 FILE* fp;
1324 char line[512];
1325 string tmpdevice;
1326
igoriok87e3d932013-01-31 21:03:53 +02001327 fp = fopen("/proc/dumchar_info", "rt");
1328 if (fp != NULL) {
1329 while (fgets(line, sizeof(line), fp) != NULL)
1330 {
1331 char label[32], device[32];
1332 unsigned long size = 0;
1333
thatd43bf2d2014-09-21 23:13:02 +02001334 sscanf(line, "%s %lx %*x %*u %s", label, &size, device);
igoriok87e3d932013-01-31 21:03:53 +02001335
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05001336 // Skip header, annotation and blank lines
igoriok87e3d932013-01-31 21:03:53 +02001337 if ((strncmp(device, "/dev/", 5) != 0) || (strlen(line) < 8))
1338 continue;
1339
1340 tmpdevice = "/dev/";
1341 tmpdevice += label;
1342 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
1343 Size = size;
1344 fclose(fp);
1345 return true;
1346 }
1347 }
1348 }
1349
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001350 unsigned long long ioctl_size = IOCTL_Get_Block_Size();
1351 if (ioctl_size) {
1352 Size = ioctl_size;
1353 return true;
1354 }
1355
Dees_Troy5bf43922012-09-07 16:07:55 -04001356 // In this case, we'll first get the partitions we care about (with labels)
1357 fp = fopen("/proc/partitions", "rt");
1358 if (fp == NULL)
1359 return false;
1360
1361 while (fgets(line, sizeof(line), fp) != NULL)
1362 {
1363 unsigned long major, minor, blocks;
1364 char device[512];
Dees_Troy5bf43922012-09-07 16:07:55 -04001365
Dees_Troy63c8df72012-09-10 14:02:05 -04001366 if (strlen(line) < 7 || line[0] == 'm') continue;
Dees_Troy5bf43922012-09-07 16:07:55 -04001367 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
1368
1369 tmpdevice = "/dev/block/";
1370 tmpdevice += device;
Dees_Troy38bd7602012-09-14 13:33:53 -04001371 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001372 // Adjust block size to byte size
1373 Size = blocks * 1024ULL;
1374 fclose(fp);
1375 return true;
1376 }
1377 }
1378 fclose(fp);
1379 return false;
1380}
1381
Dees_Troy5bf43922012-09-07 16:07:55 -04001382bool TWPartition::Is_Mounted(void) {
1383 if (!Can_Be_Mounted)
1384 return false;
1385
1386 struct stat st1, st2;
1387 string test_path;
1388
1389 // Check to see if the mount point directory exists
1390 test_path = Mount_Point + "/.";
1391 if (stat(test_path.c_str(), &st1) != 0) return false;
1392
1393 // Check to see if the directory above the mount point exists
1394 test_path = Mount_Point + "/../.";
1395 if (stat(test_path.c_str(), &st2) != 0) return false;
1396
1397 // Compare the device IDs -- if they match then we're (probably) using tmpfs instead of an actual device
1398 int ret = (st1.st_dev != st2.st_dev) ? true : false;
1399
1400 return ret;
1401}
1402
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05001403bool TWPartition::Is_File_System_Writable(void) {
1404 if (!Is_File_System(Current_File_System) || !Is_Mounted())
1405 return false;
1406
1407 string test_path = Mount_Point + "/.";
1408 return (access(test_path.c_str(), W_OK) == 0);
1409}
1410
Dees_Troy5bf43922012-09-07 16:07:55 -04001411bool TWPartition::Mount(bool Display_Error) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001412 int exfat_mounted = 0;
Martin Dünkelmann6d843782018-12-27 23:40:28 +01001413 unsigned int flags = Mount_Flags;
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001414
Dees_Troy5bf43922012-09-07 16:07:55 -04001415 if (Is_Mounted()) {
1416 return true;
1417 } else if (!Can_Be_Mounted) {
1418 return false;
1419 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001420
1421 Find_Actual_Block_Device();
1422
1423 // Check the current file system before mounting
1424 Check_FS_Type();
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001425 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/exfat-fuse")) {
Dees_Troye34c1332013-02-06 19:13:00 +00001426 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 +00001427 LOGINFO("cmd: %s\n", cmd.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001428 string result;
1429 if (TWFunc::Exec_Cmd(cmd, result) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001430 LOGINFO("exfat-fuse failed to mount with result '%s', trying vfat\n", result.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001431 Current_File_System = "vfat";
1432 } else {
1433#ifdef TW_NO_EXFAT_FUSE
1434 UnMount(false);
1435 // We'll let the kernel handle it but using exfat-fuse to detect if the file system is actually exfat
1436 // Some kernels let us mount vfat as exfat which doesn't work out too well
1437#else
1438 exfat_mounted = 1;
1439#endif
1440 }
1441 }
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001442
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001443 if (Current_File_System == "ntfs" && (TWFunc::Path_Exists("/sbin/ntfs-3g") || TWFunc::Path_Exists("/sbin/mount.ntfs"))) {
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001444 string cmd;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001445 string Ntfsmount_Binary = "";
1446
1447 if (TWFunc::Path_Exists("/sbin/ntfs-3g"))
1448 Ntfsmount_Binary = "ntfs-3g";
1449 else if (TWFunc::Path_Exists("/sbin/mount.ntfs"))
1450 Ntfsmount_Binary = "mount.ntfs";
1451
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001452 if (Mount_Read_Only)
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001453 cmd = "/sbin/" + Ntfsmount_Binary + " -o ro " + Actual_Block_Device + " " + Mount_Point;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001454 else
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001455 cmd = "/sbin/" + Ntfsmount_Binary + " " + Actual_Block_Device + " " + Mount_Point;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001456 LOGINFO("cmd: '%s'\n", cmd.c_str());
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001457
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001458 if (TWFunc::Exec_Cmd(cmd) == 0) {
1459 return true;
1460 } else {
1461 LOGINFO("ntfs-3g failed to mount, trying regular mount method.\n");
1462 }
1463 }
1464
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001465 if (Mount_Read_Only)
1466 flags |= MS_RDONLY;
1467
Dees_Troy22042032012-12-18 21:23:08 +00001468 if (Fstab_File_System == "yaffs2") {
1469 // mount an MTD partition as a YAFFS2 filesystem.
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001470 flags = MS_NOATIME | MS_NODEV | MS_NODIRATIME;
1471 if (Mount_Read_Only)
1472 flags |= MS_RDONLY;
Dees_Troy76543db2013-06-19 16:24:30 +00001473 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags, NULL) < 0) {
1474 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags | MS_RDONLY, NULL) < 0) {
1475 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001476 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy76543db2013-06-19 16:24:30 +00001477 else
1478 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
1479 return false;
1480 } else {
1481 LOGINFO("Mounted '%s' (MTD) as RO\n", Mount_Point.c_str());
1482 return true;
1483 }
1484 } else {
1485 struct stat st;
1486 string test_path = Mount_Point;
1487 if (stat(test_path.c_str(), &st) < 0) {
1488 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001489 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy76543db2013-06-19 16:24:30 +00001490 else
1491 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
1492 return false;
1493 }
1494 mode_t new_mode = st.st_mode | S_IXUSR | S_IXGRP | S_IXOTH;
1495 if (new_mode != st.st_mode) {
1496 LOGINFO("Fixing execute permissions for %s\n", Mount_Point.c_str());
1497 if (chmod(Mount_Point.c_str(), new_mode) < 0) {
1498 if (Display_Error)
1499 LOGERR("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
1500 else
1501 LOGINFO("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
1502 return false;
1503 }
1504 }
Dees_Troy22042032012-12-18 21:23:08 +00001505 return true;
Dees_Troy76543db2013-06-19 16:24:30 +00001506 }
thatc7572eb2015-03-31 18:55:30 +02001507 }
1508
1509 string mount_fs = Current_File_System;
1510 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sys/module/texfat"))
1511 mount_fs = "texfat";
1512
1513 if (!exfat_mounted &&
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001514 mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), mount_fs.c_str(), flags, Mount_Options.c_str()) != 0 &&
1515 mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), mount_fs.c_str(), flags, NULL) != 0) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001516#ifdef TW_NO_EXFAT_FUSE
1517 if (Current_File_System == "exfat") {
Dees_Troy2673cec2013-04-02 20:22:16 +00001518 LOGINFO("Mounting exfat failed, trying vfat...\n");
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001519 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), "vfat", 0, NULL) != 0) {
Dees_Troy85f44ed2013-01-09 18:42:36 +00001520 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001521 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy85f44ed2013-01-09 18:42:36 +00001522 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001523 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001524 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 +00001525 return false;
Dees_Troy85f44ed2013-01-09 18:42:36 +00001526 }
Dees_Troyb05ddee2013-01-28 20:24:50 +00001527 } else {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001528#endif
bigbiff bigbiff26774a02014-03-29 18:22:00 -04001529 if (!Removable && Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001530 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001531 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001532 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
1533 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 +00001534 return false;
1535#ifdef TW_NO_EXFAT_FUSE
Dees_Troy85f44ed2013-01-09 18:42:36 +00001536 }
1537#endif
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001538 }
Ethan Yonker253368a2014-11-25 15:00:52 -06001539
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001540 if (Removable)
1541 Update_Size(Display_Error);
1542
xiaolu9416f4f2015-06-04 08:22:23 +08001543 if (!Symlink_Mount_Point.empty() && TWFunc::Path_Exists(Symlink_Path)) {
codelover2a3d4ce2015-03-14 20:26:49 +08001544 string Command = "mount -o bind '" + Symlink_Path + "' '" + Symlink_Mount_Point + "'";
Vojtech Bocek05534202013-09-11 08:11:56 +02001545 TWFunc::Exec_Cmd(Command);
Dees_Troy5bf43922012-09-07 16:07:55 -04001546 }
Chaosmasterda974802020-01-26 21:09:28 +01001547
1548 if (Mount_Point == "/system_root") {
1549 unlink("/system");
1550 mkdir("/system", 0755);
1551 mount("/system_root/system", "/system", "auto", MS_BIND, NULL);
1552 }
1553
Dees_Troy5bf43922012-09-07 16:07:55 -04001554 return true;
1555}
1556
1557bool TWPartition::UnMount(bool Display_Error) {
Chaosmasterda974802020-01-26 21:09:28 +01001558 if (Mount_Point == "/system_root") {
1559 if (umount("/system") == -1)
1560 umount2("/system", MNT_DETACH);
1561 rmdir("/system");
1562 symlink("/system_root/system", "/system");
1563 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001564 if (Is_Mounted()) {
1565 int never_unmount_system;
1566
1567 DataManager::GetValue(TW_DONT_UNMOUNT_SYSTEM, never_unmount_system);
Captain Throwback9d6feb52018-07-27 10:05:24 -04001568 if (never_unmount_system == 1 && Mount_Point == PartitionManager.Get_Android_Root_Path())
Dees_Troy5bf43922012-09-07 16:07:55 -04001569 return true; // Never unmount system if you're not supposed to unmount it
1570
Matt Mowera8e6d832017-02-14 20:20:59 -06001571 if (Is_Storage && MTP_Storage_ID > 0)
Ethan Yonker726a0202014-12-16 20:01:38 -06001572 PartitionManager.Remove_MTP_Storage(MTP_Storage_ID);
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001573
Dees_Troy38bd7602012-09-14 13:33:53 -04001574 if (!Symlink_Mount_Point.empty())
1575 umount(Symlink_Mount_Point.c_str());
1576
Dees_Troyb05ddee2013-01-28 20:24:50 +00001577 umount(Mount_Point.c_str());
1578 if (Is_Mounted()) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001579 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001580 gui_msg(Msg(msg::kError, "fail_unmount=Failed to unmount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy5bf43922012-09-07 16:07:55 -04001581 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001582 LOGINFO("Unable to unmount '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001583 return false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001584 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -04001585 return true;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001586 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001587 } else {
1588 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001589 }
1590}
1591
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05001592bool TWPartition::ReMount(bool Display_Error) {
1593 if (UnMount(Display_Error))
1594 return Mount(Display_Error);
1595 return false;
1596}
1597
1598bool TWPartition::ReMount_RW(bool Display_Error) {
1599 // No need to remount if already mounted rw
1600 if (Is_File_System_Writable())
1601 return true;
1602
1603 bool ro = Mount_Read_Only;
1604 int flags = Mount_Flags;
1605
1606 Mount_Read_Only = false;
1607 Mount_Flags &= ~MS_RDONLY;
1608
1609 bool ret = ReMount(Display_Error);
1610
1611 Mount_Read_Only = ro;
1612 Mount_Flags = flags;
1613
1614 return ret;
1615}
1616
Gary Peck43acadf2012-11-21 21:19:01 -08001617bool TWPartition::Wipe(string New_File_System) {
Ethan Yonker726a0202014-12-16 20:01:38 -06001618 bool wiped = false, update_crypt = false, recreate_media = true;
Dees_Troy16c2b312013-01-15 16:51:18 +00001619 int check;
1620 string Layout_Filename = Mount_Point + "/.layout_version";
1621
Dees_Troy38bd7602012-09-14 13:33:53 -04001622 if (!Can_Be_Wiped) {
Matt Mower3c366972015-12-25 19:28:31 -06001623 gui_msg(Msg(msg::kError, "cannot_wipe=Partition {1} cannot be wiped.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04001624 return false;
1625 }
1626
Dees_Troyc51f1f92012-09-20 15:32:13 -04001627 if (Mount_Point == "/cache")
Dees_Troy2673cec2013-04-02 20:22:16 +00001628 Log_Offset = 0;
Dees_Troyc51f1f92012-09-20 15:32:13 -04001629
Dees_Troy16c2b312013-01-15 16:51:18 +00001630 if (Retain_Layout_Version && Mount(false) && TWFunc::Path_Exists(Layout_Filename))
1631 TWFunc::copy_file(Layout_Filename, "/.layout_version", 0600);
1632 else
1633 unlink("/.layout_version");
Dees_Troy38bd7602012-09-14 13:33:53 -04001634
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001635 if (Has_Data_Media && Current_File_System == New_File_System) {
Dees_Troy16c2b312013-01-15 16:51:18 +00001636 wiped = Wipe_Data_Without_Wiping_Media();
Ethan Yonker5eac2222014-06-11 12:22:55 -05001637 recreate_media = false;
Dees_Troy16c2b312013-01-15 16:51:18 +00001638 } else {
Dees_Troy16c2b312013-01-15 16:51:18 +00001639 DataManager::GetValue(TW_RM_RF_VAR, check);
1640
Hashcodedabfd492013-08-29 22:45:30 -07001641 if (check || Use_Rm_Rf)
Dees_Troy16c2b312013-01-15 16:51:18 +00001642 wiped = Wipe_RMRF();
1643 else if (New_File_System == "ext4")
1644 wiped = Wipe_EXT4();
1645 else if (New_File_System == "ext2" || New_File_System == "ext3")
dianlujitao4879b372018-12-03 18:45:47 +08001646 wiped = Wipe_EXTFS(New_File_System);
Dees_Troy16c2b312013-01-15 16:51:18 +00001647 else if (New_File_System == "vfat")
1648 wiped = Wipe_FAT();
1649 else if (New_File_System == "exfat")
1650 wiped = Wipe_EXFAT();
1651 else if (New_File_System == "yaffs2")
1652 wiped = Wipe_MTD();
Dees_Troye5017042013-08-29 16:38:55 +00001653 else if (New_File_System == "f2fs")
1654 wiped = Wipe_F2FS();
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001655 else if (New_File_System == "ntfs")
1656 wiped = Wipe_NTFS();
Dees_Troy16c2b312013-01-15 16:51:18 +00001657 else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001658 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 +00001659 unlink("/.layout_version");
1660 return false;
1661 }
1662 update_crypt = wiped;
Gary Pecke8bc5d72012-12-21 06:45:25 -08001663 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001664
Gary Pecke8bc5d72012-12-21 06:45:25 -08001665 if (wiped) {
Dees_Troy1c1ac442013-01-17 21:42:14 +00001666 if (Mount_Point == "/cache")
1667 DataManager::Output_Version();
1668
Dees_Troy16c2b312013-01-15 16:51:18 +00001669 if (TWFunc::Path_Exists("/.layout_version") && Mount(false))
1670 TWFunc::copy_file("/.layout_version", Layout_Filename, 0600);
1671
1672 if (update_crypt) {
1673 Setup_File_System(false);
1674 if (Is_Encrypted && !Is_Decrypted) {
1675 // just wiped an encrypted partition back to its unencrypted state
1676 Is_Encrypted = false;
1677 Is_Decrypted = false;
1678 Decrypted_Block_Device = "";
1679 if (Mount_Point == "/data") {
1680 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1681 DataManager::SetValue(TW_IS_DECRYPTED, 0);
1682 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001683 }
1684 }
Ethan Yonker5eac2222014-06-11 12:22:55 -05001685
Ethan Yonker66a19492015-12-10 10:19:45 -06001686 if (Has_Data_Media && recreate_media) {
Ethan Yonker5eac2222014-06-11 12:22:55 -05001687 Recreate_Media_Folder();
1688 }
Matt Mower209c9632016-01-20 12:08:35 -06001689 if (Is_Storage && Mount(false))
1690 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Gary Pecke8bc5d72012-12-21 06:45:25 -08001691 }
Matt Mower209c9632016-01-20 12:08:35 -06001692
Gary Pecke8bc5d72012-12-21 06:45:25 -08001693 return wiped;
Dees_Troy51a0e822012-09-05 15:24:24 -04001694}
1695
Gary Peck43acadf2012-11-21 21:19:01 -08001696bool TWPartition::Wipe() {
Gary Peck82599a82012-11-21 16:23:12 -08001697 if (Is_File_System(Current_File_System))
1698 return Wipe(Current_File_System);
1699 else
1700 return Wipe(Fstab_File_System);
Gary Peck43acadf2012-11-21 21:19:01 -08001701}
1702
Dees_Troye58d5262012-09-21 12:27:57 -04001703bool TWPartition::Wipe_AndSec(void) {
1704 if (!Has_Android_Secure)
1705 return false;
1706
Dees_Troye58d5262012-09-21 12:27:57 -04001707 if (!Mount(true))
1708 return false;
1709
Ethan Yonker74db1572015-10-28 12:44:49 -05001710 gui_msg(Msg("wiping=Wiping {1}")(Backup_Display_Name));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001711 TWFunc::removeDir(Mount_Point + "/.android_secure/", true);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001712 return true;
Dees_Troye58d5262012-09-21 12:27:57 -04001713}
1714
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001715bool TWPartition::Can_Repair() {
Ethan Yonkera2719152015-05-28 09:44:41 -05001716 if (Mount_Read_Only)
1717 return false;
Matt Mower18794c82015-11-11 16:22:45 -06001718 if (Current_File_System == "vfat" && TWFunc::Path_Exists("/sbin/fsck.fat"))
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001719 return true;
1720 else if ((Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") && TWFunc::Path_Exists("/sbin/e2fsck"))
1721 return true;
1722 else if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/fsck.exfat"))
1723 return true;
1724 else if (Current_File_System == "f2fs" && TWFunc::Path_Exists("/sbin/fsck.f2fs"))
1725 return true;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001726 else if (Current_File_System == "ntfs" && (TWFunc::Path_Exists("/sbin/ntfsfix") || TWFunc::Path_Exists("/sbin/fsck.ntfs")))
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001727 return true;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001728 return false;
1729}
1730
1731bool TWPartition::Repair() {
1732 string command;
1733
1734 if (Current_File_System == "vfat") {
Matt Mower18794c82015-11-11 16:22:45 -06001735 if (!TWFunc::Path_Exists("/sbin/fsck.fat")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001736 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.fat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001737 return false;
1738 }
1739 if (!UnMount(true))
1740 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001741 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.fat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001742 Find_Actual_Block_Device();
Matt Mower18794c82015-11-11 16:22:45 -06001743 command = "/sbin/fsck.fat -y " + Actual_Block_Device;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001744 LOGINFO("Repair command: %s\n", command.c_str());
1745 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001746 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001747 return true;
1748 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001749 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001750 return false;
1751 }
1752 }
1753 if (Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") {
1754 if (!TWFunc::Path_Exists("/sbin/e2fsck")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001755 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("e2fsck"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001756 return false;
1757 }
1758 if (!UnMount(true))
1759 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001760 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("e2fsck"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001761 Find_Actual_Block_Device();
Ethan Yonkera2719152015-05-28 09:44:41 -05001762 command = "/sbin/e2fsck -fp " + Actual_Block_Device;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001763 LOGINFO("Repair command: %s\n", command.c_str());
1764 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001765 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001766 return true;
1767 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001768 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001769 return false;
1770 }
1771 }
1772 if (Current_File_System == "exfat") {
1773 if (!TWFunc::Path_Exists("/sbin/fsck.exfat")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001774 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.exfat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001775 return false;
1776 }
1777 if (!UnMount(true))
1778 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001779 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.exfat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001780 Find_Actual_Block_Device();
1781 command = "/sbin/fsck.exfat " + Actual_Block_Device;
1782 LOGINFO("Repair command: %s\n", command.c_str());
1783 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001784 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001785 return true;
1786 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001787 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001788 return false;
1789 }
1790 }
1791 if (Current_File_System == "f2fs") {
1792 if (!TWFunc::Path_Exists("/sbin/fsck.f2fs")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001793 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.f2fs"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001794 return false;
1795 }
1796 if (!UnMount(true))
1797 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001798 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.f2fs"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001799 Find_Actual_Block_Device();
1800 command = "/sbin/fsck.f2fs " + Actual_Block_Device;
1801 LOGINFO("Repair command: %s\n", command.c_str());
1802 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001803 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001804 return true;
1805 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001806 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001807 return false;
1808 }
1809 }
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001810 if (Current_File_System == "ntfs") {
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001811 string Ntfsfix_Binary;
1812 if (TWFunc::Path_Exists("/sbin/ntfsfix"))
1813 Ntfsfix_Binary = "ntfsfix";
1814 else if (TWFunc::Path_Exists("/sbin/fsck.ntfs"))
1815 Ntfsfix_Binary = "fsck.ntfs";
1816 else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001817 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("ntfsfix"));
Ethan Yonkerb81d9052015-07-09 13:20:53 -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)(Ntfsfix_Binary));
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001823 Find_Actual_Block_Device();
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001824 command = "/sbin/" + Ntfsfix_Binary + " " + Actual_Block_Device;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001825 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 Yonkerb81d9052015-07-09 13:20:53 -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 Yonkerb81d9052015-07-09 13:20:53 -05001831 return false;
1832 }
1833 }
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001834 return false;
1835}
1836
Ethan Yonkera2719152015-05-28 09:44:41 -05001837bool TWPartition::Can_Resize() {
1838 if (Mount_Read_Only)
1839 return false;
1840 if ((Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") && TWFunc::Path_Exists("/sbin/resize2fs"))
1841 return true;
1842 return false;
1843}
1844
1845bool TWPartition::Resize() {
1846 string command;
1847
1848 if (Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") {
1849 if (!Can_Repair()) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001850 LOGINFO("Cannot resize %s because %s cannot be repaired before resizing.\n", Display_Name.c_str(), Display_Name.c_str());
1851 gui_msg(Msg(msg::kError, "cannot_resize=Cannot resize {1}.")(Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001852 return false;
1853 }
1854 if (!TWFunc::Path_Exists("/sbin/resize2fs")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001855 LOGINFO("resize2fs does not exist! Cannot resize!\n");
1856 gui_msg(Msg(msg::kError, "cannot_resize=Cannot resize {1}.")(Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001857 return false;
1858 }
1859 // Repair will unmount so no need to do it twice
Ethan Yonker74db1572015-10-28 12:44:49 -05001860 gui_msg(Msg("repair_resize=Repairing {1} before resizing.")( Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001861 if (!Repair())
1862 return false;
Ethan Yonker74db1572015-10-28 12:44:49 -05001863 gui_msg(Msg("resizing=Resizing {1} using {2}...")(Display_Name)("resize2fs"));
Ethan Yonkera2719152015-05-28 09:44:41 -05001864 Find_Actual_Block_Device();
1865 command = "/sbin/resize2fs " + Actual_Block_Device;
1866 if (Length != 0) {
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001867 unsigned long long Actual_Size = IOCTL_Get_Block_Size();
1868 if (Actual_Size == 0)
1869 return false;
Ethan Yonkera2719152015-05-28 09:44:41 -05001870
Ethan Yonkera2719152015-05-28 09:44:41 -05001871 unsigned long long Block_Count;
1872 if (Length < 0) {
1873 // Reduce overall size by this length
1874 Block_Count = (Actual_Size / 1024LLU) - ((unsigned long long)(Length * -1) / 1024LLU);
1875 } else {
1876 // This is the size, not a size reduction
1877 Block_Count = ((unsigned long long)(Length) / 1024LLU);
1878 }
1879 char temp[256];
1880 sprintf(temp, "%llu", Block_Count);
1881 command += " ";
1882 command += temp;
1883 command += "K";
1884 }
1885 LOGINFO("Resize command: %s\n", command.c_str());
1886 if (TWFunc::Exec_Cmd(command) == 0) {
1887 Update_Size(true);
Ethan Yonker74db1572015-10-28 12:44:49 -05001888 gui_msg("done=Done.");
Ethan Yonkera2719152015-05-28 09:44:41 -05001889 return true;
1890 } else {
1891 Update_Size(true);
Ethan Yonker74db1572015-10-28 12:44:49 -05001892 gui_msg(Msg(msg::kError, "unable_resize=Unable to resize {1}.")(Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001893 return false;
1894 }
1895 }
1896 return false;
1897}
1898
bigbiffce8f83c2015-12-12 18:30:21 -05001899bool TWPartition::Backup(PartitionSettings *part_settings, pid_t *tar_fork_pid) {
1900 if (Backup_Method == BM_FILES)
1901 return Backup_Tar(part_settings, tar_fork_pid);
1902 else if (Backup_Method == BM_DD)
1903 return Backup_Image(part_settings);
1904 else if (Backup_Method == BM_FLASH_UTILS)
1905 return Backup_Dump_Image(part_settings);
Dees_Troy2673cec2013-04-02 20:22:16 +00001906 LOGERR("Unknown backup method for '%s'\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001907 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001908}
1909
bigbiffce8f83c2015-12-12 18:30:21 -05001910bool TWPartition::Restore(PartitionSettings *part_settings) {
Matt Mower3c366972015-12-25 19:28:31 -06001911 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, gui_parse_text("{@restoring_hdr}"));
Ethan Yonkere080c1f2016-09-19 13:50:25 -05001912 LOGINFO("Restore filename is: %s/%s\n", part_settings->Backup_Folder.c_str(), Backup_FileName.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001913
bigbiffce8f83c2015-12-12 18:30:21 -05001914 string Restore_File_System = Get_Restore_File_System(part_settings);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001915
1916 if (Is_File_System(Restore_File_System))
bigbiffce8f83c2015-12-12 18:30:21 -05001917 return Restore_Tar(part_settings);
1918 else if (Is_Image(Restore_File_System))
1919 return Restore_Image(part_settings);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001920
1921 LOGERR("Unknown restore method for '%s'\n", Mount_Point.c_str());
1922 return false;
1923}
1924
bigbiffce8f83c2015-12-12 18:30:21 -05001925string TWPartition::Get_Restore_File_System(PartitionSettings *part_settings) {
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001926 size_t first_period, second_period;
1927 string Restore_File_System;
1928
Gary Peck43acadf2012-11-21 21:19:01 -08001929 // Parse backup filename to extract the file system before wiping
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05001930 first_period = Backup_FileName.find(".");
Gary Peck43acadf2012-11-21 21:19:01 -08001931 if (first_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001932 LOGERR("Unable to find file system (first period).\n");
thatd43bf2d2014-09-21 23:13:02 +02001933 return string();
Gary Peck43acadf2012-11-21 21:19:01 -08001934 }
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05001935 Restore_File_System = Backup_FileName.substr(first_period + 1, Backup_FileName.size() - first_period - 1);
Gary Peck43acadf2012-11-21 21:19:01 -08001936 second_period = Restore_File_System.find(".");
1937 if (second_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001938 LOGERR("Unable to find file system (second period).\n");
thatd43bf2d2014-09-21 23:13:02 +02001939 return string();
Gary Peck43acadf2012-11-21 21:19:01 -08001940 }
1941 Restore_File_System.resize(second_period);
Matt Mowera0cd91d2016-12-30 18:21:42 -06001942 LOGINFO("Restore file system is: '%s'.\n", Restore_File_System.c_str());
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001943 return Restore_File_System;
Dees_Troy51a0e822012-09-05 15:24:24 -04001944}
1945
1946string TWPartition::Backup_Method_By_Name() {
bigbiffce8f83c2015-12-12 18:30:21 -05001947 if (Backup_Method == BM_NONE)
Dees_Troy38bd7602012-09-14 13:33:53 -04001948 return "none";
bigbiffce8f83c2015-12-12 18:30:21 -05001949 else if (Backup_Method == BM_FILES)
Dees_Troy38bd7602012-09-14 13:33:53 -04001950 return "files";
bigbiffce8f83c2015-12-12 18:30:21 -05001951 else if (Backup_Method == BM_DD)
Dees_Troy38bd7602012-09-14 13:33:53 -04001952 return "dd";
bigbiffce8f83c2015-12-12 18:30:21 -05001953 else if (Backup_Method == BM_FLASH_UTILS)
Dees_Troy38bd7602012-09-14 13:33:53 -04001954 return "flash_utils";
1955 else
1956 return "undefined";
1957 return "ERROR!";
Dees_Troy51a0e822012-09-05 15:24:24 -04001958}
1959
1960bool TWPartition::Decrypt(string Password) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001961 LOGINFO("STUB TWPartition::Decrypt, password: '%s'\n", Password.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001962 // Is this needed?
Dees_Troy51a0e822012-09-05 15:24:24 -04001963 return 1;
1964}
1965
1966bool TWPartition::Wipe_Encryption() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001967 bool Save_Data_Media = Has_Data_Media;
Ethan Yonker93382822018-11-01 15:25:31 -05001968 bool ret = false;
1969 BasePartition* base_partition = make_partition();
1970
1971 if (!base_partition->PreWipeEncryption())
1972 goto exit;
Dees_Troy38bd7602012-09-14 13:33:53 -04001973
dianlujitao4879b372018-12-03 18:45:47 +08001974 Find_Actual_Block_Device();
1975 if (!Is_Present) {
1976 LOGINFO("Block device not present, cannot format %s.\n", Display_Name.c_str());
1977 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
1978 return false;
1979 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001980 if (!UnMount(true))
Ethan Yonker93382822018-11-01 15:25:31 -05001981 goto exit;
Dees_Troy38bd7602012-09-14 13:33:53 -04001982
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001983#ifdef TW_INCLUDE_CRYPTO
Ethan Yonker1b190162016-12-05 15:25:19 -06001984 if (Is_Decrypted && !Decrypted_Block_Device.empty()) {
Matt Mower2b18a532015-02-20 16:58:05 -06001985 if (delete_crypto_blk_dev((char*)("userdata")) != 0) {
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001986 LOGERR("Error deleting crypto block device, continuing anyway.\n");
1987 }
1988 }
1989#endif
dianlujitao4879b372018-12-03 18:45:47 +08001990 Has_Data_Media = false;
Ethan Yonker93382822018-11-01 15:25:31 -05001991 Decrypted_Block_Device = "";
Dees_Troy74fb2e92013-04-15 14:35:47 +00001992 Is_Decrypted = false;
1993 Is_Encrypted = false;
Gary Pecke8bc5d72012-12-21 06:45:25 -08001994 if (Wipe(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001995 Has_Data_Media = Save_Data_Media;
1996 if (Has_Data_Media && !Symlink_Mount_Point.empty()) {
1997 Recreate_Media_Folder();
Ethan Yonker726a0202014-12-16 20:01:38 -06001998 if (Mount(false))
1999 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04002000 }
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06002001 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
Ethan Yonker83e82572014-04-04 10:59:28 -05002002#ifndef TW_OEM_BUILD
Ethan Yonker74db1572015-10-28 12:44:49 -05002003 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 -05002004#endif
Ethan Yonker93382822018-11-01 15:25:31 -05002005 ret = true;
2006 if (!Key_Directory.empty())
2007 ret = PartitionManager.Wipe_By_Path(Key_Directory);
2008 if (ret)
2009 ret = base_partition->PostWipeEncryption();
2010 goto exit;
Dees_Troy38bd7602012-09-14 13:33:53 -04002011 } else {
2012 Has_Data_Media = Save_Data_Media;
Ethan Yonker74db1572015-10-28 12:44:49 -05002013 gui_err("format_data_err=Unable to format to remove encryption.");
Ethan Yonker726a0202014-12-16 20:01:38 -06002014 if (Has_Data_Media && Mount(false))
2015 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Ethan Yonker93382822018-11-01 15:25:31 -05002016 goto exit;
Dees_Troy38bd7602012-09-14 13:33:53 -04002017 }
Ethan Yonker93382822018-11-01 15:25:31 -05002018exit:
2019 delete base_partition;
2020 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04002021}
2022
2023void TWPartition::Check_FS_Type() {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05002024 const char* type;
2025 blkid_probe pr;
Dees_Troy5bf43922012-09-07 16:07:55 -04002026
Dees_Troy68cab492012-12-12 19:29:35 +00002027 if (Fstab_File_System == "yaffs2" || Fstab_File_System == "mtd" || Fstab_File_System == "bml" || Ignore_Blkid)
2028 return; // Running blkid on some mtd devices causes a massive crash or needs to be skipped
Dees_Troy5bf43922012-09-07 16:07:55 -04002029
Dees_Troy38bd7602012-09-14 13:33:53 -04002030 Find_Actual_Block_Device();
Dees_Troy8170a922012-09-18 15:40:25 -04002031 if (!Is_Present)
2032 return;
Dees_Troy51127312012-09-08 13:08:49 -04002033
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05002034 pr = blkid_new_probe_from_filename(Actual_Block_Device.c_str());
2035 if (blkid_do_fullprobe(pr)) {
2036 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00002037 LOGINFO("Can't probe device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05002038 return;
Dees_Troy5bf43922012-09-07 16:07:55 -04002039 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02002040
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05002041 if (blkid_probe_lookup_value(pr, "TYPE", &type, NULL) < 0) {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05002042 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00002043 LOGINFO("can't find filesystem on device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05002044 return;
2045 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02002046
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05002047 Current_File_System = type;
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02002048 blkid_free_probe(pr);
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06002049 if (fs_flags.size() > 1) {
2050 std::vector<partition_fs_flags_struct>::iterator iter;
2051 std::vector<partition_fs_flags_struct>::iterator found = fs_flags.begin();
2052
2053 for (iter = fs_flags.begin(); iter != fs_flags.end(); iter++) {
2054 if (iter->File_System == Current_File_System) {
2055 found = iter;
2056 break;
2057 }
2058 }
2059 // If we don't find a match, we default the flags to the first set of flags that we received from the fstab
2060 if (Mount_Flags != found->Mount_Flags || Mount_Options != found->Mount_Options) {
2061 Mount_Flags = found->Mount_Flags;
2062 Mount_Options = found->Mount_Options;
2063 LOGINFO("Mount_Flags: %i, Mount_Options: %s\n", Mount_Flags, Mount_Options.c_str());
2064 }
2065 }
Dees_Troy51a0e822012-09-05 15:24:24 -04002066}
2067
dianlujitao4879b372018-12-03 18:45:47 +08002068bool TWPartition::Wipe_EXTFS(string File_System) {
2069#if PLATFORM_SDK_VERSION < 28
2070 if (!TWFunc::Path_Exists("/sbin/mke2fs"))
2071#else
2072 if (!TWFunc::Path_Exists("/sbin/mke2fs") || !TWFunc::Path_Exists("/sbin/e2fsdroid"))
2073#endif
Dees_Troy38bd7602012-09-14 13:33:53 -04002074 return Wipe_RMRF();
2075
dianlujitao4879b372018-12-03 18:45:47 +08002076 int ret;
2077 bool NeedPreserveFooter = true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002078
Ethan Yonker25f20c12014-10-14 09:04:43 -05002079 Find_Actual_Block_Device();
2080 if (!Is_Present) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002081 LOGINFO("Block device not present, cannot wipe %s.\n", Display_Name.c_str());
2082 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Ethan Yonker25f20c12014-10-14 09:04:43 -05002083 return false;
2084 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002085 if (!UnMount(true))
2086 return false;
2087
dianlujitao4879b372018-12-03 18:45:47 +08002088 /**
2089 * On decrypted devices, IOCTL_Get_Block_Size calculates size on device mapper,
2090 * so there's no need to preserve footer.
2091 */
2092 if ((Is_Decrypted && !Decrypted_Block_Device.empty()) ||
2093 Crypto_Key_Location != "footer") {
2094 NeedPreserveFooter = false;
2095 }
2096
2097 unsigned long long dev_sz = TWFunc::IOCTL_Get_Block_Size(Actual_Block_Device.c_str());
2098 if (!dev_sz)
2099 return false;
2100
2101 if (NeedPreserveFooter)
2102 Length < 0 ? dev_sz += Length : dev_sz -= CRYPT_FOOTER_OFFSET;
2103
Dees Troy9a4d7402019-03-21 14:17:28 -04002104 char dout[16];
2105 sprintf(dout, "%llu", dev_sz / 4096);
2106
2107 //string size_str =to_string(dev_sz / 4096);
2108 string size_str = dout;
dianlujitao4879b372018-12-03 18:45:47 +08002109 string Command;
2110
2111 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mke2fs"));
2112
2113 // Execute mke2fs to create empty ext4 filesystem
2114 Command = "mke2fs -t " + File_System + " -b 4096 " + Actual_Block_Device + " " + size_str;
2115 LOGINFO("mke2fs command: %s\n", Command.c_str());
2116 ret = TWFunc::Exec_Cmd(Command);
2117 if (ret) {
2118 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
2119 return false;
2120 }
2121
2122 if (TWFunc::Path_Exists("/sbin/e2fsdroid")) {
dianlujitao3cf05a52019-05-15 15:33:27 +08002123 const string& File_Contexts_Entry = (Mount_Point == "/system_root" ? "/" : Mount_Point);
2124 char *secontext = NULL;
2125 if (!selinux_handle || selabel_lookup(selinux_handle, &secontext, File_Contexts_Entry.c_str(), S_IFDIR) < 0) {
2126 LOGINFO("Cannot lookup security context for '%s'\n", Mount_Point.c_str());
2127 } else {
2128 // Execute e2fsdroid to initialize selinux context
2129 Command = "e2fsdroid -e -S /file_contexts -a " + File_Contexts_Entry + " " + Actual_Block_Device;
2130 LOGINFO("e2fsdroid command: %s\n", Command.c_str());
2131 ret = TWFunc::Exec_Cmd(Command);
2132 if (ret) {
2133 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
2134 return false;
2135 }
dianlujitao4879b372018-12-03 18:45:47 +08002136 }
2137 } else {
2138 LOGINFO("e2fsdroid not present\n");
2139 }
2140
2141 if (NeedPreserveFooter)
2142 Wipe_Crypto_Key();
2143 Current_File_System = File_System;
2144 Recreate_AndSec_Folder();
2145 gui_msg("done=Done.");
2146 return true;
2147}
2148
2149bool TWPartition::Wipe_EXT4() {
2150#ifdef USE_EXT4
Ethan Yonkerf27497f2014-02-09 11:48:33 -06002151 int ret;
dianlujitao4879b372018-12-03 18:45:47 +08002152 bool NeedPreserveFooter = true;
2153
2154 Find_Actual_Block_Device();
2155 if (!Is_Present) {
2156 LOGINFO("Block device not present, cannot wipe %s.\n", Display_Name.c_str());
2157 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
2158 return false;
2159 }
2160 if (!UnMount(true))
2161 return false;
2162
2163 /**
2164 * On decrypted devices, IOCTL_Get_Block_Size calculates size on device mapper,
2165 * so there's no need to preserve footer.
2166 */
2167 if ((Is_Decrypted && !Decrypted_Block_Device.empty()) ||
2168 Crypto_Key_Location != "footer") {
2169 NeedPreserveFooter = false;
2170 }
2171
2172 unsigned long long dev_sz = TWFunc::IOCTL_Get_Block_Size(Actual_Block_Device.c_str());
2173 if (!dev_sz)
2174 return false;
2175
2176 if (NeedPreserveFooter)
2177 Length < 0 ? dev_sz += Length : dev_sz -= CRYPT_FOOTER_OFFSET;
2178
Ethan Yonkerf27497f2014-02-09 11:48:33 -06002179 char *secontext = NULL;
2180
Greg Wallace4b44fef2015-12-29 15:33:24 -05002181 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("make_ext4fs"));
Ethan Yonkerf27497f2014-02-09 11:48:33 -06002182
Dees Troy99c8dbf2014-03-10 16:53:58 +00002183 if (!selinux_handle || selabel_lookup(selinux_handle, &secontext, Mount_Point.c_str(), S_IFDIR) < 0) {
Ethan Yonkerf27497f2014-02-09 11:48:33 -06002184 LOGINFO("Cannot lookup security context for '%s'\n", Mount_Point.c_str());
dianlujitao4879b372018-12-03 18:45:47 +08002185 ret = make_ext4fs(Actual_Block_Device.c_str(), dev_sz, Mount_Point.c_str(), NULL);
Ethan Yonkerf27497f2014-02-09 11:48:33 -06002186 } else {
dianlujitao4879b372018-12-03 18:45:47 +08002187 ret = make_ext4fs(Actual_Block_Device.c_str(), dev_sz, Mount_Point.c_str(), selinux_handle);
Ethan Yonkerf27497f2014-02-09 11:48:33 -06002188 }
2189 if (ret != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002190 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troya95f55c2013-08-17 13:14:43 +00002191 return false;
2192 } else {
dianlujitao4879b372018-12-03 18:45:47 +08002193 if (NeedPreserveFooter)
2194 Wipe_Crypto_Key();
bigbiff bigbiffc49d7062013-10-11 20:28:00 -04002195 string sedir = Mount_Point + "/lost+found";
2196 PartitionManager.Mount_By_Path(sedir.c_str(), true);
2197 rmdir(sedir.c_str());
2198 mkdir(sedir.c_str(), S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP);
Dees_Troya95f55c2013-08-17 13:14:43 +00002199 return true;
2200 }
2201#else
dianlujitao4879b372018-12-03 18:45:47 +08002202 return Wipe_EXTFS("ext4");
Dees_Troya95f55c2013-08-17 13:14:43 +00002203#endif
Dees_Troy51a0e822012-09-05 15:24:24 -04002204}
2205
2206bool TWPartition::Wipe_FAT() {
Vojtech Bocek05534202013-09-11 08:11:56 +02002207 string command;
Dees_Troy38bd7602012-09-14 13:33:53 -04002208
Matt Mower18794c82015-11-11 16:22:45 -06002209 if (TWFunc::Path_Exists("/sbin/mkfs.fat")) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002210 if (!UnMount(true))
2211 return false;
2212
Greg Wallace4b44fef2015-12-29 15:33:24 -05002213 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mkfs.fat"));
Dees_Troy38bd7602012-09-14 13:33:53 -04002214 Find_Actual_Block_Device();
Matt Mower18794c82015-11-11 16:22:45 -06002215 command = "mkfs.fat " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02002216 if (TWFunc::Exec_Cmd(command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08002217 Current_File_System = "vfat";
Dees_Troye58d5262012-09-21 12:27:57 -04002218 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05002219 gui_msg("done=Done.");
Dees_Troy38bd7602012-09-14 13:33:53 -04002220 return true;
2221 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002222 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04002223 return false;
2224 }
2225 return true;
2226 }
2227 else
2228 return Wipe_RMRF();
2229
2230 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04002231}
2232
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002233bool TWPartition::Wipe_EXFAT() {
Vojtech Bocek05534202013-09-11 08:11:56 +02002234 string command;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002235
2236 if (TWFunc::Path_Exists("/sbin/mkexfatfs")) {
2237 if (!UnMount(true))
2238 return false;
2239
Greg Wallace4b44fef2015-12-29 15:33:24 -05002240 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mkexfatfs"));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002241 Find_Actual_Block_Device();
2242 command = "mkexfatfs " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02002243 if (TWFunc::Exec_Cmd(command) == 0) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002244 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05002245 gui_msg("done=Done.");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002246 return true;
2247 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002248 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002249 return false;
2250 }
2251 return true;
2252 }
2253 return false;
2254}
2255
Dees_Troy38bd7602012-09-14 13:33:53 -04002256bool TWPartition::Wipe_MTD() {
2257 if (!UnMount(true))
2258 return false;
2259
Greg Wallace4b44fef2015-12-29 15:33:24 -05002260 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("MTD"));
Dees_Troy38bd7602012-09-14 13:33:53 -04002261
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02002262 mtd_scan_partitions();
2263 const MtdPartition* mtd = mtd_find_partition_by_name(MTD_Name.c_str());
2264 if (mtd == NULL) {
2265 LOGERR("No mtd partition named '%s'", MTD_Name.c_str());
2266 return false;
2267 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002268
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02002269 MtdWriteContext* ctx = mtd_write_partition(mtd);
2270 if (ctx == NULL) {
2271 LOGERR("Can't write '%s', failed to format.", MTD_Name.c_str());
2272 return false;
2273 }
2274 if (mtd_erase_blocks(ctx, -1) == -1) {
2275 mtd_write_close(ctx);
2276 LOGERR("Failed to format '%s'", MTD_Name.c_str());
2277 return false;
2278 }
2279 if (mtd_write_close(ctx) != 0) {
2280 LOGERR("Failed to close '%s'", MTD_Name.c_str());
2281 return false;
2282 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08002283 Current_File_System = "yaffs2";
Dees_Troye58d5262012-09-21 12:27:57 -04002284 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05002285 gui_msg("done=Done.");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02002286 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002287}
2288
2289bool TWPartition::Wipe_RMRF() {
Dees_Troy38bd7602012-09-14 13:33:53 -04002290 if (!Mount(true))
2291 return false;
Ethan Yonker726a0202014-12-16 20:01:38 -06002292 // This is the only wipe that leaves the partition mounted, so we
2293 // must manually remove the partition from MTP if it is a storage
2294 // partition.
2295 if (Is_Storage)
2296 PartitionManager.Remove_MTP_Storage(MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04002297
Ethan Yonker74db1572015-10-28 12:44:49 -05002298 gui_msg(Msg("remove_all=Removing all files under '{1}'")(Mount_Point));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002299 TWFunc::removeDir(Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04002300 Recreate_AndSec_Folder();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002301 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002302}
2303
Dees_Troye5017042013-08-29 16:38:55 +00002304bool TWPartition::Wipe_F2FS() {
Vojtech Bocek05534202013-09-11 08:11:56 +02002305 string command;
Dees_Troye5017042013-08-29 16:38:55 +00002306
2307 if (TWFunc::Path_Exists("/sbin/mkfs.f2fs")) {
dianlujitao4879b372018-12-03 18:45:47 +08002308 bool NeedPreserveFooter = true;
2309
2310 Find_Actual_Block_Device();
2311 if (!Is_Present) {
2312 LOGINFO("Block device not present, cannot wipe %s.\n", Display_Name.c_str());
2313 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
2314 return false;
2315 }
Dees_Troye5017042013-08-29 16:38:55 +00002316 if (!UnMount(true))
2317 return false;
2318
dianlujitao4879b372018-12-03 18:45:47 +08002319 /**
2320 * On decrypted devices, IOCTL_Get_Block_Size calculates size on device mapper,
2321 * so there's no need to preserve footer.
2322 */
2323 if ((Is_Decrypted && !Decrypted_Block_Device.empty()) ||
2324 Crypto_Key_Location != "footer") {
2325 NeedPreserveFooter = false;
dhacker29a3fa75f2015-01-17 19:42:33 -05002326 }
dianlujitao4879b372018-12-03 18:45:47 +08002327
dianlujitao4879b372018-12-03 18:45:47 +08002328 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mkfs.f2fs"));
Ethan Yonker7e941582019-03-22 08:18:21 -05002329 // First determine if we have the old mkfs.f2fs that uses "-r reserved_bytes"
2330 // or the new mkfs.f2fs that expects the number of sectors as the optional last argument
2331 // Note: some 7.1 trees have the old and some have the new.
2332 command = "mkfs.f2fs | grep \"reserved\" > /tmp/f2fsversiontest";
2333 TWFunc::Exec_Cmd(command, false); // no help argument so printing usage exits with an error code
2334 if (!TWFunc::Path_Exists("/tmp/f2fsversiontest")) {
2335 LOGINFO("Error determining mkfs.f2fs version\n");
2336 return false;
2337 }
2338 if (TWFunc::Get_File_Size("/tmp/f2fsversiontest") <= 0) {
2339 LOGINFO("Using newer mkfs.f2fs\n");
2340 unsigned long long dev_sz = TWFunc::IOCTL_Get_Block_Size(Actual_Block_Device.c_str());
2341 if (!dev_sz)
2342 return false;
2343
2344 if (NeedPreserveFooter)
2345 Length < 0 ? dev_sz += Length : dev_sz -= CRYPT_FOOTER_OFFSET;
2346
2347 char dev_sz_str[48];
2348 sprintf(dev_sz_str, "%llu", (dev_sz / 4096));
2349 command = "mkfs.f2fs -d1 -f -O encrypt -O quota -O verity -w 4096 " + Actual_Block_Device + " " + dev_sz_str;
2350 if (TWFunc::Path_Exists("/sbin/sload.f2fs")) {
2351 command += " && sload.f2fs -t /data " + Actual_Block_Device;
2352 }
2353 } else {
2354 LOGINFO("Using older mkfs.f2fs\n");
2355 command = "mkfs.f2fs -t 0";
2356 if (NeedPreserveFooter) {
2357 // Only use length if we're not decrypted
2358 char len[32];
2359 int mod_length = Length;
2360 if (Length < 0)
2361 mod_length *= -1;
2362 sprintf(len, "%i", mod_length);
2363 command += " -r ";
2364 command += len;
2365 }
2366 command += " " + Actual_Block_Device;
dianlujitao4879b372018-12-03 18:45:47 +08002367 }
2368 LOGINFO("mkfs.f2fs command: %s\n", command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02002369 if (TWFunc::Exec_Cmd(command) == 0) {
dianlujitao4879b372018-12-03 18:45:47 +08002370 if (NeedPreserveFooter)
2371 Wipe_Crypto_Key();
Dees_Troye5017042013-08-29 16:38:55 +00002372 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05002373 gui_msg("done=Done.");
Dees_Troye5017042013-08-29 16:38:55 +00002374 return true;
2375 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002376 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troye5017042013-08-29 16:38:55 +00002377 return false;
2378 }
2379 return true;
2380 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002381 LOGINFO("mkfs.f2fs binary not found, using rm -rf to wipe.\n");
Dees_Troye5017042013-08-29 16:38:55 +00002382 return Wipe_RMRF();
2383 }
2384 return false;
2385}
2386
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002387bool TWPartition::Wipe_NTFS() {
2388 string command;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002389 string Ntfsmake_Binary;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002390
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002391 if (TWFunc::Path_Exists("/sbin/mkntfs"))
2392 Ntfsmake_Binary = "mkntfs";
2393 else if (TWFunc::Path_Exists("/sbin/mkfs.ntfs"))
2394 Ntfsmake_Binary = "mkfs.ntfs";
2395 else
2396 return false;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002397
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002398 if (!UnMount(true))
2399 return false;
2400
Greg Wallace4b44fef2015-12-29 15:33:24 -05002401 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)(Ntfsmake_Binary));
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002402 Find_Actual_Block_Device();
2403 command = "/sbin/" + Ntfsmake_Binary + " " + Actual_Block_Device;
2404 if (TWFunc::Exec_Cmd(command) == 0) {
2405 Recreate_AndSec_Folder();
2406 gui_msg("done=Done.");
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002407 return true;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002408 } else {
2409 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
2410 return false;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002411 }
2412 return false;
2413}
2414
Dees_Troy51a0e822012-09-05 15:24:24 -04002415bool TWPartition::Wipe_Data_Without_Wiping_Media() {
Ethan Yonker83e82572014-04-04 10:59:28 -05002416#ifdef TW_OEM_BUILD
2417 // In an OEM Build we want to do a full format
2418 return Wipe_Encryption();
2419#else
Ethan Yonker66a19492015-12-10 10:19:45 -06002420 bool ret = false;
Dees_Troy38bd7602012-09-14 13:33:53 -04002421
Dees_Troy38bd7602012-09-14 13:33:53 -04002422 if (!Mount(true))
2423 return false;
2424
Ethan Yonker74db1572015-10-28 12:44:49 -05002425 gui_msg("wiping_data=Wiping data without wiping /data/media ...");
Ethan Yonker66a19492015-12-10 10:19:45 -06002426 ret = Wipe_Data_Without_Wiping_Media_Func(Mount_Point + "/");
2427 if (ret)
2428 gui_msg("done=Done.");
2429 return ret;
2430#endif // ifdef TW_OEM_BUILD
2431}
2432
2433bool TWPartition::Wipe_Data_Without_Wiping_Media_Func(const string& parent __unused) {
2434 string dir;
Dees_Troy38bd7602012-09-14 13:33:53 -04002435
2436 DIR* d;
Ethan Yonker66a19492015-12-10 10:19:45 -06002437 d = opendir(parent.c_str());
Dees_Troy16b74352012-11-14 22:27:31 +00002438 if (d != NULL) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002439 struct dirent* de;
2440 while ((de = readdir(d)) != NULL) {
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002441 if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) continue;
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05002442
Ethan Yonker66a19492015-12-10 10:19:45 -06002443 dir = parent;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002444 dir.append(de->d_name);
Ethan Yonker3fdcda42016-11-30 12:29:37 -06002445 if (wipe_exclusions.check_skip_dirs(dir)) {
Ethan Yonker66a19492015-12-10 10:19:45 -06002446 LOGINFO("skipped '%s'\n", dir.c_str());
2447 continue;
2448 }
Dees_Troyce675462013-01-09 19:48:21 +00002449 if (de->d_type == DT_DIR) {
Ethan Yonker66a19492015-12-10 10:19:45 -06002450 dir.append("/");
2451 if (!Wipe_Data_Without_Wiping_Media_Func(dir)) {
2452 closedir(d);
2453 return false;
2454 }
2455 rmdir(dir.c_str());
bigbiff bigbiff98f1f902013-01-19 18:46:13 -05002456 } 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 +03002457 if (unlink(dir.c_str()) != 0)
Ethan Yonker79f88bd2016-12-09 14:52:12 -06002458 LOGINFO("Unable to unlink '%s': %s\n", dir.c_str(), strerror(errno));
Dees_Troyce675462013-01-09 19:48:21 +00002459 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002460 }
2461 closedir(d);
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05002462
Dees_Troy16b74352012-11-14 22:27:31 +00002463 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -04002464 }
Ethan Yonker74db1572015-10-28 12:44:49 -05002465 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy16b74352012-11-14 22:27:31 +00002466 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04002467}
2468
dianlujitao4879b372018-12-03 18:45:47 +08002469void TWPartition::Wipe_Crypto_Key() {
2470 Find_Actual_Block_Device();
2471 if (Crypto_Key_Location.empty())
2472 return;
2473 else if (Crypto_Key_Location == "footer") {
2474 int fd = open(Actual_Block_Device.c_str(), O_RDWR);
2475 if (fd < 0) {
2476 gui_print_color("warning", "Unable to open '%s' to wipe crypto key\n", Actual_Block_Device.c_str());
2477 return;
2478 }
2479
2480 unsigned int block_count;
2481 if ((ioctl(fd, BLKGETSIZE, &block_count)) == -1) {
2482 gui_print_color("warning", "Unable to get block size for wiping crypto footer.\n");
2483 } else {
2484 int newlen = Length < 0 ? -Length : CRYPT_FOOTER_OFFSET;
2485 off64_t offset = ((off64_t)block_count * 512) - newlen;
2486 if (lseek64(fd, offset, SEEK_SET) == -1) {
2487 gui_print_color("warning", "Unable to lseek64 for wiping crypto footer.\n");
2488 } else {
2489 void* buffer = malloc(newlen);
2490 if (!buffer) {
2491 gui_print_color("warning", "Failed to malloc for wiping crypto footer.\n");
2492 } else {
2493 memset(buffer, 0, newlen);
2494 int ret = write(fd, buffer, newlen);
2495 if (ret != newlen) {
2496 gui_print_color("warning", "Failed to wipe crypto footer.\n");
2497 } else {
2498 LOGINFO("Successfully wiped crypto footer.\n");
2499 }
2500 free(buffer);
2501 }
2502 }
2503 }
2504 close(fd);
2505 } else {
2506 if (TWFunc::IOCTL_Get_Block_Size(Crypto_Key_Location.c_str()) >= 16384LLU) {
2507 string Command = "dd of='" + Crypto_Key_Location + "' if=/dev/zero bs=16384 count=1";
2508 TWFunc::Exec_Cmd(Command);
2509 } else {
2510 LOGINFO("Crypto key location reports size < 16K so not wiping crypto footer.\n");
2511 }
2512 }
2513}
2514
bigbiffce8f83c2015-12-12 18:30:21 -05002515bool TWPartition::Backup_Tar(PartitionSettings *part_settings, pid_t *tar_fork_pid) {
Ethan Yonker472f5062016-02-25 13:47:30 -06002516 string Full_FileName;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002517 twrpTar tar;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05002518
Dees_Troy43d8b002012-09-17 16:00:01 -04002519 if (!Mount(true))
2520 return false;
2521
Ian Macdonalda3b25a32019-06-16 22:39:14 +02002522 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Backup_Display_Name, gui_parse_text("{@backing}"));
Ethan Yonker74db1572015-10-28 12:44:49 -05002523 gui_msg(Msg("backing_up=Backing up {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002524
Ethan Yonker472f5062016-02-25 13:47:30 -06002525 DataManager::GetValue(TW_USE_COMPRESSION_VAR, tar.use_compression);
Matt Mowerbb81e5d2014-03-20 18:05:41 -05002526
Dees_Troy83bd4832013-05-04 12:39:56 +00002527#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
Ethan Yonker472f5062016-02-25 13:47:30 -06002528 if (Can_Encrypt_Backup) {
2529 DataManager::GetValue("tw_encrypt_backup", tar.use_encryption);
2530 if (tar.use_encryption) {
2531 if (Use_Userdata_Encryption)
2532 tar.userdata_encryption = tar.use_encryption;
2533 string Password;
2534 DataManager::GetValue("tw_backup_password", Password);
2535 tar.setpassword(Password);
2536 } else {
2537 tar.use_encryption = 0;
2538 }
Dees_Troy83bd4832013-05-04 12:39:56 +00002539 }
2540#endif
Dees_Troy43d8b002012-09-17 16:00:01 -04002541
Ethan Yonker472f5062016-02-25 13:47:30 -06002542 Backup_FileName = Backup_Name + "." + Current_File_System + ".win";
Ethan Yonkere080c1f2016-09-19 13:50:25 -05002543 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
Ethan Yonker01f4e032017-02-03 15:30:52 -06002544 if (Has_Data_Media)
2545 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 -05002546 tar.part_settings = part_settings;
Ethan Yonker3fdcda42016-11-30 12:29:37 -06002547 tar.backup_exclusions = &backup_exclusions;
Dees Troye0a433a2013-12-02 04:10:37 +00002548 tar.setdir(Backup_Path);
2549 tar.setfn(Full_FileName);
2550 tar.setsize(Backup_Size);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002551 tar.partition_name = Backup_Name;
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002552 tar.backup_folder = part_settings->Backup_Folder;
bigbiffce8f83c2015-12-12 18:30:21 -05002553 if (tar.createTarFork(tar_fork_pid) != 0)
Dees Troye0a433a2013-12-02 04:10:37 +00002554 return false;
Dees_Troy43d8b002012-09-17 16:00:01 -04002555 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002556}
2557
bigbiffce8f83c2015-12-12 18:30:21 -05002558bool TWPartition::Backup_Image(PartitionSettings *part_settings) {
2559 string Full_FileName, adb_file_name;
igoriok87e3d932013-01-31 21:03:53 +02002560
Ethan Yonker74db1572015-10-28 12:44:49 -05002561 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, gui_parse_text("{@backing}"));
2562 gui_msg(Msg("backing_up=Backing up {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002563
Ethan Yonker472f5062016-02-25 13:47:30 -06002564 Backup_FileName = Backup_Name + "." + Current_File_System + ".win";
Dees_Troy43d8b002012-09-17 16:00:01 -04002565
bigbiffce8f83c2015-12-12 18:30:21 -05002566 if (part_settings->adbbackup) {
2567 Full_FileName = TW_ADB_BACKUP;
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002568 adb_file_name = part_settings->Backup_Folder + "/" + Backup_FileName;
bigbiffce8f83c2015-12-12 18:30:21 -05002569 }
2570 else
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002571 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
bigbiffce8f83c2015-12-12 18:30:21 -05002572
2573 part_settings->total_restore_size = Backup_Size;
2574
2575 if (part_settings->adbbackup) {
2576 if (!twadbbu::Write_TWIMG(adb_file_name, Backup_Size))
2577 return false;
2578 }
2579
2580 if (!Raw_Read_Write(part_settings))
Ethan Yonker472f5062016-02-25 13:47:30 -06002581 return false;
2582
bigbiffce8f83c2015-12-12 18:30:21 -05002583 if (part_settings->adbbackup) {
2584 if (!twadbbu::Write_TWEOF())
2585 return false;
Dees_Troy7c2dec82012-09-26 09:49:14 -04002586 }
Dees_Troy43d8b002012-09-17 16:00:01 -04002587 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002588}
2589
bigbiffce8f83c2015-12-12 18:30:21 -05002590bool TWPartition::Raw_Read_Write(PartitionSettings *part_settings) {
2591 unsigned long long RW_Block_Size, Remain = Backup_Size;
2592 int src_fd = -1, dest_fd = -1;
2593 ssize_t bs;
Ethan Yonker472f5062016-02-25 13:47:30 -06002594 bool ret = false;
2595 void* buffer = NULL;
2596 unsigned long long backedup_size = 0;
bigbiffce8f83c2015-12-12 18:30:21 -05002597 string srcfn, destfn;
Ethan Yonker472f5062016-02-25 13:47:30 -06002598
bigbiffce8f83c2015-12-12 18:30:21 -05002599 if (part_settings->PM_Method == PM_BACKUP) {
2600 srcfn = Actual_Block_Device;
2601 if (part_settings->adbbackup)
2602 destfn = TW_ADB_BACKUP;
bigbiff bigbiff38b83c12017-12-28 19:58:52 -05002603 else {
Ethan Yonkere080c1f2016-09-19 13:50:25 -05002604 destfn = part_settings->Backup_Folder + "/" + Backup_FileName;
bigbiff bigbiff38b83c12017-12-28 19:58:52 -05002605 }
bigbiffce8f83c2015-12-12 18:30:21 -05002606 }
2607 else {
2608 destfn = Actual_Block_Device;
2609 if (part_settings->adbbackup) {
2610 srcfn = TW_ADB_RESTORE;
2611 } else {
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002612 srcfn = part_settings->Backup_Folder + "/" + Backup_FileName;
bigbiffce8f83c2015-12-12 18:30:21 -05002613 Remain = TWFunc::Get_File_Size(srcfn);
2614 }
2615 }
Ethan Yonker472f5062016-02-25 13:47:30 -06002616
bigbiffce8f83c2015-12-12 18:30:21 -05002617 src_fd = open(srcfn.c_str(), O_RDONLY | O_LARGEFILE);
Ethan Yonker472f5062016-02-25 13:47:30 -06002618 if (src_fd < 0) {
bigbiffce8f83c2015-12-12 18:30:21 -05002619 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(srcfn.c_str())(strerror(errno)));
Ethan Yonker472f5062016-02-25 13:47:30 -06002620 return false;
2621 }
bigbiffce8f83c2015-12-12 18:30:21 -05002622
2623 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 -06002624 if (dest_fd < 0) {
bigbiffce8f83c2015-12-12 18:30:21 -05002625 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(destfn.c_str())(strerror(errno)));
Ethan Yonker472f5062016-02-25 13:47:30 -06002626 goto exit;
2627 }
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -04002628
bigbiffce8f83c2015-12-12 18:30:21 -05002629 LOGINFO("Reading '%s', writing '%s'\n", srcfn.c_str(), destfn.c_str());
2630
2631 if (part_settings->adbbackup) {
2632 RW_Block_Size = MAX_ADB_READ;
2633 bs = MAX_ADB_READ;
2634 }
2635 else {
2636 RW_Block_Size = 1048576LLU; // 1MB
2637 bs = (ssize_t)(RW_Block_Size);
2638 }
2639
Ethan Yonker472f5062016-02-25 13:47:30 -06002640 buffer = malloc((size_t)bs);
2641 if (!buffer) {
2642 LOGINFO("Raw_Read_Write failed to malloc\n");
2643 goto exit;
2644 }
bigbiffce8f83c2015-12-12 18:30:21 -05002645
2646 if (part_settings->progress)
2647 part_settings->progress->SetPartitionSize(part_settings->total_restore_size);
2648
Ethan Yonker472f5062016-02-25 13:47:30 -06002649 while (Remain > 0) {
2650 if (Remain < RW_Block_Size)
bigbiffce8f83c2015-12-12 18:30:21 -05002651 bs = (ssize_t)(Remain);
Matt Mowera8a89d12016-12-30 18:10:37 -06002652 if (read(src_fd, buffer, bs) != bs) {
Ethan Yonker472f5062016-02-25 13:47:30 -06002653 LOGINFO("Error reading source fd (%s)\n", strerror(errno));
2654 goto exit;
2655 }
2656 if (write(dest_fd, buffer, bs) != bs) {
2657 LOGINFO("Error writing destination fd (%s)\n", strerror(errno));
2658 goto exit;
2659 }
2660 backedup_size += (unsigned long long)(bs);
Matt Mower029a82d2017-01-08 13:12:38 -06002661 Remain -= (unsigned long long)(bs);
bigbiffce8f83c2015-12-12 18:30:21 -05002662 if (part_settings->progress)
2663 part_settings->progress->UpdateSize(backedup_size);
Ethan Yonker472f5062016-02-25 13:47:30 -06002664 if (PartitionManager.Check_Backup_Cancel() != 0)
2665 goto exit;
2666 }
bigbiffce8f83c2015-12-12 18:30:21 -05002667 if (part_settings->progress)
2668 part_settings->progress->UpdateDisplayDetails(true);
Ethan Yonker472f5062016-02-25 13:47:30 -06002669 fsync(dest_fd);
nailyka083dc62016-11-11 21:41:28 +01002670
2671 if (!part_settings->adbbackup && part_settings->PM_Method == PM_BACKUP) {
2672 tw_set_default_metadata(destfn.c_str());
2673 LOGINFO("Restored default metadata for %s\n", destfn.c_str());
2674 }
2675
Ethan Yonker472f5062016-02-25 13:47:30 -06002676 ret = true;
2677exit:
2678 if (src_fd >= 0)
2679 close(src_fd);
2680 if (dest_fd >= 0)
2681 close(dest_fd);
2682 if (buffer)
2683 free(buffer);
2684 return ret;
2685}
2686
bigbiffce8f83c2015-12-12 18:30:21 -05002687bool TWPartition::Backup_Dump_Image(PartitionSettings *part_settings) {
Vojtech Bocek05534202013-09-11 08:11:56 +02002688 string Full_FileName, Command;
Dees_Troy43d8b002012-09-17 16:00:01 -04002689
Ethan Yonker74db1572015-10-28 12:44:49 -05002690 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, gui_parse_text("{@backing}"));
2691 gui_msg(Msg("backing_up=Backing up {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002692
bigbiffce8f83c2015-12-12 18:30:21 -05002693 if (part_settings->progress)
2694 part_settings->progress->SetPartitionSize(Backup_Size);
Dees_Troy43d8b002012-09-17 16:00:01 -04002695
Ethan Yonker472f5062016-02-25 13:47:30 -06002696 Backup_FileName = Backup_Name + "." + Current_File_System + ".win";
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002697 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04002698
2699 Command = "dump_image " + MTD_Name + " '" + Full_FileName + "'";
bigbiffce8f83c2015-12-12 18:30:21 -05002700
Dees_Troy2673cec2013-04-02 20:22:16 +00002701 LOGINFO("Backup command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02002702 TWFunc::Exec_Cmd(Command);
Ethan Yonker4b94cfd2014-12-11 10:00:45 -06002703 tw_set_default_metadata(Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04002704 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
2705 // 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 -06002706 gui_msg(Msg(msg::kError, "backup_size=Backup file size for '{1}' is 0 bytes.")(Full_FileName));
Dees_Troy7c2dec82012-09-26 09:49:14 -04002707 return false;
2708 }
bigbiffce8f83c2015-12-12 18:30:21 -05002709 if (part_settings->progress)
2710 part_settings->progress->UpdateSize(Backup_Size);
2711
Dees_Troy43d8b002012-09-17 16:00:01 -04002712 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002713}
2714
bigbiffce8f83c2015-12-12 18:30:21 -05002715unsigned long long TWPartition::Get_Restore_Size(PartitionSettings *part_settings) {
2716 if (!part_settings->adbbackup) {
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002717 InfoManager restore_info(part_settings->Backup_Folder + "/" + Backup_Name + ".info");
bigbiffce8f83c2015-12-12 18:30:21 -05002718 if (restore_info.LoadValues() == 0) {
2719 if (restore_info.GetValue("backup_size", Restore_Size) == 0) {
2720 LOGINFO("Read info file, restore size is %llu\n", Restore_Size);
2721 return Restore_Size;
2722 }
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002723 }
2724 }
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002725
Matt Mower029a82d2017-01-08 13:12:38 -06002726 string Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
2727 string Restore_File_System = Get_Restore_File_System(part_settings);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002728
2729 if (Is_Image(Restore_File_System)) {
2730 Restore_Size = TWFunc::Get_File_Size(Full_FileName);
2731 return Restore_Size;
2732 }
2733
2734 twrpTar tar;
2735 tar.setdir(Backup_Path);
2736 tar.setfn(Full_FileName);
bigbiffce8f83c2015-12-12 18:30:21 -05002737 tar.backup_name = Full_FileName;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002738#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
2739 string Password;
2740 DataManager::GetValue("tw_restore_password", Password);
2741 if (!Password.empty())
2742 tar.setpassword(Password);
2743#endif
2744 tar.partition_name = Backup_Name;
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002745 tar.backup_folder = part_settings->Backup_Folder;
bigbiffce8f83c2015-12-12 18:30:21 -05002746 tar.part_settings = part_settings;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002747 Restore_Size = tar.get_size();
2748 return Restore_Size;
2749}
2750
bigbiffce8f83c2015-12-12 18:30:21 -05002751bool TWPartition::Restore_Tar(PartitionSettings *part_settings) {
Ethan Yonker472f5062016-02-25 13:47:30 -06002752 string Full_FileName;
Dees Troy4159aed2014-02-28 17:24:43 +00002753 bool ret = false;
bigbiffce8f83c2015-12-12 18:30:21 -05002754 string Restore_File_System = Get_Restore_File_System(part_settings);
Dees_Troy43d8b002012-09-17 16:00:01 -04002755
Dees_Troye58d5262012-09-21 12:27:57 -04002756 if (Has_Android_Secure) {
Dees_Troye58d5262012-09-21 12:27:57 -04002757 if (!Wipe_AndSec())
2758 return false;
Gary Peck43acadf2012-11-21 21:19:01 -08002759 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002760 gui_msg(Msg("wiping=Wiping {1}")(Backup_Display_Name));
Ethan Yonker5eac2222014-06-11 12:22:55 -05002761 if (Has_Data_Media && Mount_Point == "/data" && Restore_File_System != Current_File_System) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002762 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 -05002763 if (!Wipe_Data_Without_Wiping_Media())
2764 return false;
2765 } else {
2766 if (!Wipe(Restore_File_System))
2767 return false;
2768 }
Dees_Troye58d5262012-09-21 12:27:57 -04002769 }
Matt Mower3c366972015-12-25 19:28:31 -06002770 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Backup_Display_Name, gui_parse_text("{@restoring_hdr}"));
Ethan Yonker74db1572015-10-28 12:44:49 -05002771 gui_msg(Msg("restoring=Restoring {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002772
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05002773 // Remount as read/write as needed so we can restore the backup
2774 if (!ReMount_RW(true))
Dees_Troy43d8b002012-09-17 16:00:01 -04002775 return false;
2776
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002777 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
Ethan Yonker87af5632014-02-10 11:56:35 -06002778 twrpTar tar;
bigbiffce8f83c2015-12-12 18:30:21 -05002779 tar.part_settings = part_settings;
Ethan Yonker87af5632014-02-10 11:56:35 -06002780 tar.setdir(Backup_Path);
2781 tar.setfn(Full_FileName);
2782 tar.backup_name = Backup_Name;
2783#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
2784 string Password;
2785 DataManager::GetValue("tw_restore_password", Password);
2786 if (!Password.empty())
2787 tar.setpassword(Password);
2788#endif
bigbiffce8f83c2015-12-12 18:30:21 -05002789 part_settings->progress->SetPartitionSize(Get_Restore_Size(part_settings));
2790 if (tar.extractTarFork() != 0)
Dees Troy4159aed2014-02-28 17:24:43 +00002791 ret = false;
2792 else
2793 ret = true;
2794#ifdef HAVE_CAPABILITIES
2795 // Restore capabilities to the run-as binary
Captain Throwback9d6feb52018-07-27 10:05:24 -04002796 if (Mount_Point == PartitionManager.Get_Android_Root_Path() && Mount(true) && TWFunc::Path_Exists("/system/bin/run-as")) {
Dees Troy4159aed2014-02-28 17:24:43 +00002797 struct vfs_cap_data cap_data;
2798 uint64_t capabilities = (1 << CAP_SETUID) | (1 << CAP_SETGID);
2799
2800 memset(&cap_data, 0, sizeof(cap_data));
2801 cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE;
2802 cap_data.data[0].permitted = (uint32_t) (capabilities & 0xffffffff);
2803 cap_data.data[0].inheritable = 0;
2804 cap_data.data[1].permitted = (uint32_t) (capabilities >> 32);
2805 cap_data.data[1].inheritable = 0;
2806 if (setxattr("/system/bin/run-as", XATTR_NAME_CAPS, &cap_data, sizeof(cap_data), 0) < 0) {
2807 LOGINFO("Failed to reset capabilities of /system/bin/run-as binary.\n");
2808 } else {
2809 LOGINFO("Reset capabilities of /system/bin/run-as binary successful.\n");
2810 }
2811 }
2812#endif
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05002813 if (Mount_Read_Only || Mount_Flags & MS_RDONLY)
2814 // Remount as read only when restoration is complete
2815 ReMount(true);
2816
Dees Troy4159aed2014-02-28 17:24:43 +00002817 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04002818}
2819
bigbiffce8f83c2015-12-12 18:30:21 -05002820bool TWPartition::Restore_Image(PartitionSettings *part_settings) {
Ethan Yonker96af84a2015-01-05 14:58:36 -06002821 string Full_FileName;
bigbiffce8f83c2015-12-12 18:30:21 -05002822 string Restore_File_System = Get_Restore_File_System(part_settings);
Dees_Troy43d8b002012-09-17 16:00:01 -04002823
Matt Mower3c366972015-12-25 19:28:31 -06002824 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Backup_Display_Name, gui_parse_text("{@restoring_hdr}"));
Ethan Yonker74db1572015-10-28 12:44:49 -05002825 gui_msg(Msg("restoring=Restoring {1}...")(Backup_Display_Name));
bigbiffce8f83c2015-12-12 18:30:21 -05002826
2827 if (part_settings->adbbackup)
2828 Full_FileName = TW_ADB_RESTORE;
2829 else
Ethan Yonkere080c1f2016-09-19 13:50:25 -05002830 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
Gary Peck15e623d2012-11-21 21:07:58 -08002831
Ethan Yonker96af84a2015-01-05 14:58:36 -06002832 if (Restore_File_System == "emmc") {
bigbiffce8f83c2015-12-12 18:30:21 -05002833 if (!part_settings->adbbackup)
2834 part_settings->total_restore_size = (uint64_t)(TWFunc::Get_File_Size(Full_FileName));
2835 if (!Raw_Read_Write(part_settings))
Ethan Yonker96af84a2015-01-05 14:58:36 -06002836 return false;
2837 } else if (Restore_File_System == "mtd" || Restore_File_System == "bml") {
bigbiffce8f83c2015-12-12 18:30:21 -05002838 if (!Flash_Image_FI(Full_FileName, part_settings->progress))
2839 return false;
2840 }
2841
2842 if (part_settings->adbbackup) {
2843 if (!twadbbu::Write_TWEOF())
Ethan Yonker96af84a2015-01-05 14:58:36 -06002844 return false;
Gary Peck15e623d2012-11-21 21:07:58 -08002845 }
Dees_Troy43d8b002012-09-17 16:00:01 -04002846 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002847}
Dees_Troy5bf43922012-09-07 16:07:55 -04002848
2849bool TWPartition::Update_Size(bool Display_Error) {
Dees_Troy0550cfb2012-10-13 11:56:13 -04002850 bool ret = false, Was_Already_Mounted = false;
Dees_Troy51127312012-09-08 13:08:49 -04002851
Ethan Yonker1b190162016-12-05 15:25:19 -06002852 Find_Actual_Block_Device();
2853
2854 if (!Can_Be_Mounted && !Is_Encrypted) {
2855 if (TWFunc::Path_Exists(Actual_Block_Device) && Find_Partition_Size()) {
2856 Used = Size;
2857 Backup_Size = Size;
2858 return true;
2859 }
Dees_Troy5bf43922012-09-07 16:07:55 -04002860 return false;
Ethan Yonker1b190162016-12-05 15:25:19 -06002861 }
Dees_Troy5bf43922012-09-07 16:07:55 -04002862
Dees_Troy0550cfb2012-10-13 11:56:13 -04002863 Was_Already_Mounted = Is_Mounted();
Dees_Troy38bd7602012-09-14 13:33:53 -04002864 if (Removable || Is_Encrypted) {
2865 if (!Mount(false))
2866 return true;
2867 } else if (!Mount(Display_Error))
Dees_Troy5bf43922012-09-07 16:07:55 -04002868 return false;
Dees_Troy51127312012-09-08 13:08:49 -04002869
2870 ret = Get_Size_Via_statfs(Display_Error);
Dees_Troy0550cfb2012-10-13 11:56:13 -04002871 if (!ret || Size == 0) {
2872 if (!Get_Size_Via_df(Display_Error)) {
2873 if (!Was_Already_Mounted)
2874 UnMount(false);
Dees_Troy51127312012-09-08 13:08:49 -04002875 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04002876 }
2877 }
Dees_Troy51127312012-09-08 13:08:49 -04002878
Dees_Troy5bf43922012-09-07 16:07:55 -04002879 if (Has_Data_Media) {
2880 if (Mount(Display_Error)) {
Ethan Yonker3fdcda42016-11-30 12:29:37 -06002881 Used = backup_exclusions.Get_Folder_Size(Mount_Point);
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002882 Backup_Size = Used;
2883 int bak = (int)(Used / 1048576LLU);
Dees_Troy51127312012-09-08 13:08:49 -04002884 int fre = (int)(Free / 1048576LLU);
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002885 LOGINFO("Data backup size is %iMB, free: %iMB.\n", bak, fre);
Dees_Troy0550cfb2012-10-13 11:56:13 -04002886 } else {
2887 if (!Was_Already_Mounted)
2888 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04002889 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04002890 }
Dees_Troye58d5262012-09-21 12:27:57 -04002891 } else if (Has_Android_Secure) {
2892 if (Mount(Display_Error))
Ethan Yonker3fdcda42016-11-30 12:29:37 -06002893 Backup_Size = backup_exclusions.Get_Folder_Size(Backup_Path);
Dees_Troy0550cfb2012-10-13 11:56:13 -04002894 else {
2895 if (!Was_Already_Mounted)
2896 UnMount(false);
Dees_Troye58d5262012-09-21 12:27:57 -04002897 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04002898 }
Dees_Troy5bf43922012-09-07 16:07:55 -04002899 }
Dees_Troy0550cfb2012-10-13 11:56:13 -04002900 if (!Was_Already_Mounted)
2901 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04002902 return true;
Dees_Troy51127312012-09-08 13:08:49 -04002903}
Dees_Troy38bd7602012-09-14 13:33:53 -04002904
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06002905bool TWPartition::Find_Wildcard_Block_Devices(const string& Device) {
2906 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
2907 string Path = TWFunc::Get_Path(Device);
2908 string Dev = TWFunc::Get_Filename(Device);
2909 size_t wildcard_index = Dev.find("*");
2910 if (wildcard_index != string::npos)
2911 Dev = Dev.substr(0, wildcard_index);
2912 wildcard_index = Dev.size();
2913 DIR* d = opendir(Path.c_str());
2914 if (d == NULL) {
2915 LOGINFO("Error opening '%s': %s\n", Path.c_str(), strerror(errno));
2916 return false;
2917 }
2918 struct dirent* de;
2919 while ((de = readdir(d)) != NULL) {
2920 if (de->d_type != DT_BLK || strlen(de->d_name) <= wildcard_index || strncmp(de->d_name, Dev.c_str(), wildcard_index) != 0)
2921 continue;
2922
2923 string item = Path + "/";
2924 item.append(de->d_name);
2925 if (PartitionManager.Find_Partition_By_Block_Device(item))
2926 continue;
2927 TWPartition *part = new TWPartition;
2928 char buffer[MAX_FSTAB_LINE_LENGTH];
2929 sprintf(buffer, "%s %s-%i auto defaults defaults", item.c_str(), Mount_Point.c_str(), ++mount_point_index);
Chaosmasterf6e42ce2020-01-27 00:17:04 +01002930 part->Process_Fstab_Line(buffer, false, NULL, false);
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06002931 char display[MAX_FSTAB_LINE_LENGTH];
2932 sprintf(display, "%s %i", Storage_Name.c_str(), mount_point_index);
2933 part->Storage_Name = display;
2934 part->Display_Name = display;
2935 part->Primary_Block_Device = item;
2936 part->Wildcard_Block_Device = false;
2937 part->Is_SubPartition = true;
2938 part->SubPartition_Of = Mount_Point;
2939 part->Is_Storage = Is_Storage;
2940 part->Can_Be_Mounted = true;
2941 part->Removable = true;
2942 part->Can_Be_Wiped = Can_Be_Wiped;
2943 part->Wipe_Available_in_GUI = Wipe_Available_in_GUI;
2944 part->Find_Actual_Block_Device();
2945 part->Update_Size(false);
2946 Has_SubPartition = true;
2947 PartitionManager.Output_Partition(part);
2948 PartitionManager.Add_Partition(part);
2949 }
2950 closedir(d);
2951 return (mount_point_index > 0);
2952}
2953
Dees_Troy38bd7602012-09-14 13:33:53 -04002954void TWPartition::Find_Actual_Block_Device(void) {
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06002955 if (!Sysfs_Entry.empty() && Primary_Block_Device.empty() && Decrypted_Block_Device.empty()) {
2956 /* Sysfs_Entry.empty() indicates if this is a sysfs entry that begins with /device/
2957 * If we have a syfs entry then we are looking for this device from a uevent add.
2958 * The uevent add will set the primary block device based on the data we receive from
2959 * after checking for adopted storage. If the device ends up being adopted, then the
2960 * decrypted block device will be set instead of the primary block device. */
2961 Is_Present = false;
2962 return;
2963 }
2964 if (Wildcard_Block_Device && !Is_Adopted_Storage) {
2965 Is_Present = false;
2966 Actual_Block_Device = "";
2967 Can_Be_Mounted = false;
2968 if (!Find_Wildcard_Block_Devices(Primary_Block_Device)) {
2969 string Dev = Primary_Block_Device.substr(0, Primary_Block_Device.find("*"));
2970 if (TWFunc::Path_Exists(Dev)) {
2971 Is_Present = true;
2972 Can_Be_Mounted = true;
2973 Actual_Block_Device = Dev;
2974 }
2975 }
2976 return;
2977 } else if (Is_Decrypted && !Decrypted_Block_Device.empty()) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002978 Actual_Block_Device = Decrypted_Block_Device;
Ethan Yonker1b190162016-12-05 15:25:19 -06002979 if (TWFunc::Path_Exists(Decrypted_Block_Device)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002980 Is_Present = true;
Ethan Yonker1b190162016-12-05 15:25:19 -06002981 return;
2982 }
2983 } else if (SlotSelect && TWFunc::Path_Exists(Primary_Block_Device + PartitionManager.Get_Active_Slot_Suffix())) {
2984 Actual_Block_Device = Primary_Block_Device + PartitionManager.Get_Active_Slot_Suffix();
2985 unlink(Primary_Block_Device.c_str());
2986 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
2987 Is_Present = true;
2988 return;
Dees_Troy43d8b002012-09-17 16:00:01 -04002989 } else if (TWFunc::Path_Exists(Primary_Block_Device)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002990 Is_Present = true;
2991 Actual_Block_Device = Primary_Block_Device;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002992 return;
2993 }
Ethan Yonker1b190162016-12-05 15:25:19 -06002994 if (!Alternate_Block_Device.empty() && TWFunc::Path_Exists(Alternate_Block_Device)) {
Dees_Troy3f04d032012-10-07 18:20:09 -04002995 Actual_Block_Device = Alternate_Block_Device;
Dees_Troy38bd7602012-09-14 13:33:53 -04002996 Is_Present = true;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002997 } else {
Dees_Troy38bd7602012-09-14 13:33:53 -04002998 Is_Present = false;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002999 }
Dees_Troy38bd7602012-09-14 13:33:53 -04003000}
3001
3002void TWPartition::Recreate_Media_Folder(void) {
3003 string Command;
Ethan Yonker66a19492015-12-10 10:19:45 -06003004 string Media_Path = Mount_Point + "/media";
Dees_Troy38bd7602012-09-14 13:33:53 -04003005
Ethan Yonkerbd7492d2016-12-07 13:55:01 -06003006 if (Is_FBE) {
3007 LOGINFO("Not recreating media folder on FBE\n");
3008 return;
3009 }
Dees_Troy38bd7602012-09-14 13:33:53 -04003010 if (!Mount(true)) {
Ethan Yonker66a19492015-12-10 10:19:45 -06003011 gui_msg(Msg(msg::kError, "recreate_folder_err=Unable to recreate {1} folder.")(Media_Path));
3012 } else if (!TWFunc::Path_Exists(Media_Path)) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05003013 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Ethan Yonker66a19492015-12-10 10:19:45 -06003014 LOGINFO("Recreating %s folder.\n", Media_Path.c_str());
3015 mkdir(Media_Path.c_str(), 0770);
xiaolu9416f4f2015-06-04 08:22:23 +08003016 string Internal_path = DataManager::GetStrValue("tw_internal_path");
3017 if (!Internal_path.empty()) {
3018 LOGINFO("Recreating %s folder.\n", Internal_path.c_str());
3019 mkdir(Internal_path.c_str(), 0770);
3020 }
3021#ifdef TW_INTERNAL_STORAGE_PATH
3022 mkdir(EXPAND(TW_INTERNAL_STORAGE_PATH), 0770);
3023#endif
Matt Mower87413642017-01-17 21:14:46 -06003024
thata3d31fb2014-12-21 22:27:40 +01003025 // Afterwards, we will try to set the
3026 // default metadata that we were hopefully able to get during
3027 // early boot.
Ethan Yonker66a19492015-12-10 10:19:45 -06003028 tw_set_default_metadata(Media_Path.c_str());
xiaolu9416f4f2015-06-04 08:22:23 +08003029 if (!Internal_path.empty())
3030 tw_set_default_metadata(Internal_path.c_str());
Matt Mower87413642017-01-17 21:14:46 -06003031
Ethan Yonker5eac2222014-06-11 12:22:55 -05003032 // Toggle mount to ensure that "internal sdcard" gets mounted
bigbiff bigbiff9c754052013-01-09 09:09:08 -05003033 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Ethan Yonker5eac2222014-06-11 12:22:55 -05003034 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Dees_Troy38bd7602012-09-14 13:33:53 -04003035 }
Dees_Troy43d8b002012-09-17 16:00:01 -04003036}
Dees_Troye58d5262012-09-21 12:27:57 -04003037
3038void TWPartition::Recreate_AndSec_Folder(void) {
Dees_Troye58d5262012-09-21 12:27:57 -04003039 if (!Has_Android_Secure)
3040 return;
Dees_Troy2673cec2013-04-02 20:22:16 +00003041 LOGINFO("Creating %s: %s\n", Backup_Display_Name.c_str(), Symlink_Path.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -04003042 if (!Mount(true)) {
Ethan Yonker74db1572015-10-28 12:44:49 -05003043 gui_msg(Msg(msg::kError, "recreate_folder_err=Unable to recreate {1} folder.")(Backup_Name));
Dees_Troye58d5262012-09-21 12:27:57 -04003044 } else if (!TWFunc::Path_Exists(Symlink_Path)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00003045 LOGINFO("Recreating %s folder.\n", Backup_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05003046 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05003047 mkdir(Symlink_Path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
bigbiff bigbiff9c754052013-01-09 09:09:08 -05003048 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04003049 }
3050}
bigbiff7cb4c332014-11-26 20:36:07 -05003051
3052uint64_t TWPartition::Get_Max_FileSize() {
3053 uint64_t maxFileSize = 0;
3054 const uint64_t constGB = (uint64_t) 1024 * 1024 * 1024;
3055 const uint64_t constTB = (uint64_t) constGB * 1024;
3056 const uint64_t constPB = (uint64_t) constTB * 1024;
bigbiff0c532032014-12-21 13:41:26 -05003057 if (Current_File_System == "ext4")
3058 maxFileSize = 16 * constTB; //16 TB
3059 else if (Current_File_System == "vfat")
3060 maxFileSize = 4 * constGB; //4 GB
3061 else if (Current_File_System == "ntfs")
3062 maxFileSize = 256 * constTB; //256 TB
3063 else if (Current_File_System == "exfat")
3064 maxFileSize = 16 * constPB; //16 PB
3065 else if (Current_File_System == "ext3")
3066 maxFileSize = 2 * constTB; //2 TB
3067 else if (Current_File_System == "f2fs")
3068 maxFileSize = 3.94 * constTB; //3.94 TB
bigbiff7cb4c332014-11-26 20:36:07 -05003069 else
3070 maxFileSize = 100000000L;
3071 return maxFileSize - 1;
3072}
3073
bigbiffce8f83c2015-12-12 18:30:21 -05003074bool TWPartition::Flash_Image(PartitionSettings *part_settings) {
3075 string Restore_File_System, full_filename;
Ethan Yonker96af84a2015-01-05 14:58:36 -06003076
Ethan Yonkere080c1f2016-09-19 13:50:25 -05003077 full_filename = part_settings->Backup_Folder + "/" + Backup_FileName;
Ethan Yonker96af84a2015-01-05 14:58:36 -06003078
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05003079 LOGINFO("Image filename is: %s\n", Backup_FileName.c_str());
bigbiffce8f83c2015-12-12 18:30:21 -05003080
3081 if (Backup_Method == BM_FILES) {
Ethan Yonker96af84a2015-01-05 14:58:36 -06003082 LOGERR("Cannot flash images to file systems\n");
3083 return false;
3084 } else if (!Can_Flash_Img) {
3085 LOGERR("Cannot flash images to partitions %s\n", Display_Name.c_str());
3086 return false;
3087 } else {
3088 if (!Find_Partition_Size()) {
3089 LOGERR("Unable to find partition size for '%s'\n", Mount_Point.c_str());
3090 return false;
3091 }
bigbiffce8f83c2015-12-12 18:30:21 -05003092 unsigned long long image_size = TWFunc::Get_File_Size(full_filename);
Ethan Yonker96af84a2015-01-05 14:58:36 -06003093 if (image_size > Size) {
Ethan Yonker74db1572015-10-28 12:44:49 -05003094 LOGINFO("Size (%llu bytes) of image '%s' is larger than target device '%s' (%llu bytes)\n",
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05003095 image_size, Backup_FileName.c_str(), Actual_Block_Device.c_str(), Size);
Ethan Yonker74db1572015-10-28 12:44:49 -05003096 gui_err("img_size_err=Size of image is larger than target device");
Ethan Yonker96af84a2015-01-05 14:58:36 -06003097 return false;
3098 }
bigbiffce8f83c2015-12-12 18:30:21 -05003099 if (Backup_Method == BM_DD) {
3100 if (!part_settings->adbbackup) {
3101 if (Is_Sparse_Image(full_filename)) {
3102 return Flash_Sparse_Image(full_filename);
3103 }
Ethan Yonker472f5062016-02-25 13:47:30 -06003104 }
bigbiffce8f83c2015-12-12 18:30:21 -05003105 return Raw_Read_Write(part_settings);
3106 } else if (Backup_Method == BM_FLASH_UTILS) {
3107 return Flash_Image_FI(full_filename, NULL);
Ethan Yonker472f5062016-02-25 13:47:30 -06003108 }
Ethan Yonker96af84a2015-01-05 14:58:36 -06003109 }
3110
3111 LOGERR("Unknown flash method for '%s'\n", Mount_Point.c_str());
3112 return false;
3113}
3114
Ethan Yonker472f5062016-02-25 13:47:30 -06003115bool TWPartition::Is_Sparse_Image(const string& Filename) {
HashBanged974bb2016-01-30 14:20:09 -05003116 uint32_t magic = 0;
3117 int fd = open(Filename.c_str(), O_RDONLY);
3118 if (fd < 0) {
3119 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Filename)(strerror(errno)));
3120 return false;
3121 }
bigbiffce8f83c2015-12-12 18:30:21 -05003122
HashBanged974bb2016-01-30 14:20:09 -05003123 if (read(fd, &magic, sizeof(magic)) != sizeof(magic)) {
3124 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Filename)(strerror(errno)));
3125 close(fd);
3126 return false;
3127 }
3128 close(fd);
Ethan Yonker472f5062016-02-25 13:47:30 -06003129 if (magic == SPARSE_HEADER_MAGIC)
3130 return true;
3131 return false;
3132}
3133
3134bool TWPartition::Flash_Sparse_Image(const string& Filename) {
3135 string Command;
3136
3137 gui_msg(Msg("flashing=Flashing {1}...")(Display_Name));
3138
3139 Command = "simg2img '" + Filename + "' '" + Actual_Block_Device + "'";
Ethan Yonker96af84a2015-01-05 14:58:36 -06003140 LOGINFO("Flash command: '%s'\n", Command.c_str());
3141 TWFunc::Exec_Cmd(Command);
3142 return true;
3143}
3144
Ethan Yonker472f5062016-02-25 13:47:30 -06003145bool TWPartition::Flash_Image_FI(const string& Filename, ProgressTracking *progress) {
Ethan Yonker96af84a2015-01-05 14:58:36 -06003146 string Command;
Ethan Yonker472f5062016-02-25 13:47:30 -06003147 unsigned long long file_size;
Ethan Yonker96af84a2015-01-05 14:58:36 -06003148
Ethan Yonker74db1572015-10-28 12:44:49 -05003149 gui_msg(Msg("flashing=Flashing {1}...")(Display_Name));
Ethan Yonker472f5062016-02-25 13:47:30 -06003150 if (progress) {
3151 file_size = (unsigned long long)(TWFunc::Get_File_Size(Filename));
3152 progress->SetPartitionSize(file_size);
3153 }
Ethan Yonker96af84a2015-01-05 14:58:36 -06003154 // Sometimes flash image doesn't like to flash due to the first 2KB matching, so we erase first to ensure that it flashes
3155 Command = "erase_image " + MTD_Name;
3156 LOGINFO("Erase command: '%s'\n", Command.c_str());
3157 TWFunc::Exec_Cmd(Command);
3158 Command = "flash_image " + MTD_Name + " '" + Filename + "'";
3159 LOGINFO("Flash command: '%s'\n", Command.c_str());
3160 TWFunc::Exec_Cmd(Command);
Ethan Yonker472f5062016-02-25 13:47:30 -06003161 if (progress)
3162 progress->UpdateSize(file_size);
Ethan Yonker96af84a2015-01-05 14:58:36 -06003163 return true;
3164}
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05003165
3166void TWPartition::Change_Mount_Read_Only(bool new_value) {
3167 Mount_Read_Only = new_value;
3168}
3169
bigbiffce8f83c2015-12-12 18:30:21 -05003170bool TWPartition::Is_Read_Only() {
3171 return Mount_Read_Only;
3172}
3173
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05003174int TWPartition::Check_Lifetime_Writes() {
3175 bool original_read_only = Mount_Read_Only;
3176 int ret = 1;
3177
3178 Mount_Read_Only = true;
3179 if (Mount(false)) {
3180 Find_Actual_Block_Device();
Ethan Yonker54970bb2019-01-17 16:19:24 -06003181 string temp = Actual_Block_Device;
3182 Find_Real_Block_Device(temp, false);
3183 string block = basename(temp.c_str());
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05003184 string file = "/sys/fs/" + Current_File_System + "/" + block + "/lifetime_write_kbytes";
3185 string result;
3186 if (TWFunc::Path_Exists(file)) {
3187 if (TWFunc::read_file(file, result) != 0) {
3188 LOGINFO("Check_Lifetime_Writes of '%s' failed to read_file\n", file.c_str());
3189 } else {
3190 LOGINFO("Check_Lifetime_Writes result: '%s'\n", result.c_str());
3191 if (result == "0") {
3192 ret = 0;
3193 }
3194 }
3195 } else {
3196 LOGINFO("Check_Lifetime_Writes file does not exist '%s'\n", file.c_str());
3197 }
3198 UnMount(true);
3199 } else {
3200 LOGINFO("Check_Lifetime_Writes failed to mount '%s'\n", Mount_Point.c_str());
3201 }
3202 Mount_Read_Only = original_read_only;
3203 return ret;
3204}
Ethan Yonker66a19492015-12-10 10:19:45 -06003205
3206int TWPartition::Decrypt_Adopted() {
3207#ifdef TW_INCLUDE_CRYPTO
3208 int ret = 1;
3209 Is_Adopted_Storage = false;
3210 string Adopted_Key_File = "";
3211
3212 if (!Removable)
3213 return ret;
3214
3215 int fd = open(Alternate_Block_Device.c_str(), O_RDONLY);
3216 if (fd < 0) {
3217 LOGINFO("failed to open '%s'\n", Alternate_Block_Device.c_str());
3218 return ret;
3219 }
3220 char type_guid[80];
3221 char part_guid[80];
3222
3223 if (gpt_disk_get_partition_info(fd, 2, type_guid, part_guid) == 0) {
3224 LOGINFO("type: '%s'\n", type_guid);
3225 LOGINFO("part: '%s'\n", part_guid);
3226 Adopted_GUID = part_guid;
3227 LOGINFO("Adopted_GUID '%s'\n", Adopted_GUID.c_str());
3228 if (strcmp(type_guid, TWGptAndroidExpand) == 0) {
3229 LOGINFO("android_expand found\n");
3230 Adopted_Key_File = "/data/misc/vold/expand_";
3231 Adopted_Key_File += part_guid;
3232 Adopted_Key_File += ".key";
3233 if (TWFunc::Path_Exists(Adopted_Key_File)) {
3234 Is_Adopted_Storage = true;
3235 /* Until we find a use case for this, I think it is safe
3236 * to disable USB Mass Storage whenever adopted storage
3237 * is present.
3238 */
3239 LOGINFO("Detected adopted storage, disabling USB mass storage mode\n");
3240 DataManager::SetValue("tw_has_usb_storage", 0);
3241 }
3242 }
3243 }
3244
3245 if (Is_Adopted_Storage) {
3246 string Adopted_Block_Device = Alternate_Block_Device + "p2";
3247 if (!TWFunc::Path_Exists(Adopted_Block_Device)) {
3248 Adopted_Block_Device = Alternate_Block_Device + "2";
3249 if (!TWFunc::Path_Exists(Adopted_Block_Device)) {
3250 LOGINFO("Adopted block device does not exist\n");
3251 goto exit;
3252 }
3253 }
3254 LOGINFO("key file is '%s', block device '%s'\n", Adopted_Key_File.c_str(), Adopted_Block_Device.c_str());
3255 char crypto_blkdev[MAXPATHLEN];
3256 std::string thekey;
3257 int fdkey = open(Adopted_Key_File.c_str(), O_RDONLY);
3258 if (fdkey < 0) {
3259 LOGINFO("failed to open key file\n");
3260 goto exit;
3261 }
3262 char buf[512];
3263 ssize_t n;
3264 while ((n = read(fdkey, &buf[0], sizeof(buf))) > 0) {
3265 thekey.append(buf, n);
3266 }
3267 close(fdkey);
3268 unsigned char* key = (unsigned char*) thekey.data();
3269 cryptfs_revert_ext_volume(part_guid);
3270
3271 ret = cryptfs_setup_ext_volume(part_guid, Adopted_Block_Device.c_str(), key, thekey.size(), crypto_blkdev);
3272 if (ret == 0) {
3273 LOGINFO("adopted storage new block device: '%s'\n", crypto_blkdev);
3274 Decrypted_Block_Device = crypto_blkdev;
3275 Is_Decrypted = true;
3276 Is_Encrypted = true;
3277 Find_Actual_Block_Device();
Kjell Braden3126a112016-06-19 16:58:15 +00003278 if (!Mount_Storage_Retry(false)) {
Ethan Yonker66a19492015-12-10 10:19:45 -06003279 LOGERR("Failed to mount decrypted adopted storage device\n");
3280 Is_Decrypted = false;
3281 Is_Encrypted = false;
3282 cryptfs_revert_ext_volume(part_guid);
3283 ret = 1;
3284 } else {
Ethan Yonkerfcf3f242016-02-16 12:30:26 -06003285 UnMount(false);
3286 Has_Android_Secure = false;
3287 Symlink_Path = "";
3288 Symlink_Mount_Point = "";
3289 Backup_Name = Mount_Point.substr(1);
3290 Backup_Path = Mount_Point;
3291 TWPartition* sdext = PartitionManager.Find_Partition_By_Path("/sd-ext");
3292 if (sdext && sdext->Actual_Block_Device == Adopted_Block_Device) {
3293 LOGINFO("Removing /sd-ext from partition list due to adopted storage\n");
3294 PartitionManager.Remove_Partition_By_Path("/sd-ext");
3295 }
Ethan Yonker66a19492015-12-10 10:19:45 -06003296 Setup_Data_Media();
3297 Recreate_Media_Folder();
3298 Wipe_Available_in_GUI = true;
3299 Wipe_During_Factory_Reset = true;
3300 Can_Be_Backed_Up = true;
3301 Can_Encrypt_Backup = true;
3302 Use_Userdata_Encryption = true;
3303 Is_Storage = true;
3304 Storage_Name = "Adopted Storage";
3305 Is_SubPartition = true;
3306 SubPartition_Of = "/data";
3307 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
3308 DataManager::SetValue("tw_has_adopted_storage", 1);
3309 }
3310 } else {
3311 LOGERR("Failed to setup adopted storage decryption\n");
3312 }
3313 }
3314exit:
Matt Mower06543e32017-01-06 15:25:26 -06003315 close(fd);
Ethan Yonker66a19492015-12-10 10:19:45 -06003316 return ret;
3317#else
3318 LOGINFO("Decrypt_Adopted: no crypto support\n");
3319 return 1;
3320#endif
3321}
3322
3323void TWPartition::Revert_Adopted() {
3324#ifdef TW_INCLUDE_CRYPTO
3325 if (!Adopted_GUID.empty()) {
3326 PartitionManager.Remove_MTP_Storage(Mount_Point);
3327 UnMount(false);
3328 cryptfs_revert_ext_volume(Adopted_GUID.c_str());
3329 Is_Adopted_Storage = false;
3330 Is_Encrypted = false;
3331 Is_Decrypted = false;
3332 Decrypted_Block_Device = "";
3333 Find_Actual_Block_Device();
3334 Wipe_During_Factory_Reset = false;
3335 Can_Be_Backed_Up = false;
3336 Can_Encrypt_Backup = false;
3337 Use_Userdata_Encryption = false;
3338 Is_SubPartition = false;
3339 SubPartition_Of = "";
3340 Has_Data_Media = false;
3341 Storage_Path = Mount_Point;
3342 if (!Symlink_Mount_Point.empty()) {
3343 TWPartition* Dat = PartitionManager.Find_Partition_By_Path("/data");
3344 if (Dat) {
3345 Dat->UnMount(false);
3346 Dat->Symlink_Mount_Point = Symlink_Mount_Point;
3347 }
3348 Symlink_Mount_Point = "";
3349 }
3350 }
3351#else
3352 LOGINFO("Revert_Adopted: no crypto support\n");
3353#endif
3354}
bigbiff bigbiffb5ecaad2017-03-20 18:53:53 -04003355
3356void TWPartition::Set_Backup_FileName(string fname) {
3357 Backup_FileName = fname;
3358}
3359
3360string TWPartition::Get_Backup_Name() {
3361 return Backup_Name;
3362}