blob: 5626e5c4b3750b8c8e97da03318efc36a6ac6103 [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,
Peter Cai28ea7b02019-05-24 11:38:54 +0800161 TWFLAG_WRAPPEDKEY,
Matt Mower2416a502016-04-12 19:54:46 -0500162};
163
164/* Flags without a trailing '=' are considered dual format flags and can be
165 * written as either 'flagname' or 'flagname=', where the character following
166 * the '=' is Y,y,1 for true and false otherwise.
167 */
168const struct flag_list tw_flags[] = {
169 { "andsec", TWFLAG_ANDSEC },
170 { "backup", TWFLAG_BACKUP },
171 { "backupname=", TWFLAG_BACKUPNAME },
172 { "blocksize=", TWFLAG_BLOCKSIZE },
173 { "canbewiped", TWFLAG_CANBEWIPED },
174 { "canencryptbackup", TWFLAG_CANENCRYPTBACKUP },
175 { "defaults", TWFLAG_DEFAULTS },
176 { "display=", TWFLAG_DISPLAY },
177 { "encryptable=", TWFLAG_ENCRYPTABLE },
Ethan Yonkere9afc3d2018-08-30 15:16:27 -0500178 { "fileencryption=", TWFLAG_FILEENCRYPTION },
Matt Mower2416a502016-04-12 19:54:46 -0500179 { "flashimg", TWFLAG_FLASHIMG },
180 { "forceencrypt=", TWFLAG_FORCEENCRYPT },
181 { "fsflags=", TWFLAG_FSFLAGS },
182 { "ignoreblkid", TWFLAG_IGNOREBLKID },
183 { "length=", TWFLAG_LENGTH },
184 { "mounttodecrypt", TWFLAG_MOUNTTODECRYPT },
185 { "removable", TWFLAG_REMOVABLE },
186 { "retainlayoutversion", TWFLAG_RETAINLAYOUTVERSION },
187 { "settingsstorage", TWFLAG_SETTINGSSTORAGE },
188 { "storage", TWFLAG_STORAGE },
189 { "storagename=", TWFLAG_STORAGENAME },
190 { "subpartitionof=", TWFLAG_SUBPARTITIONOF },
191 { "symlink=", TWFLAG_SYMLINK },
192 { "userdataencryptbackup", TWFLAG_USERDATAENCRYPTBACKUP },
193 { "usermrf", TWFLAG_USERMRF },
194 { "wipeduringfactoryreset", TWFLAG_WIPEDURINGFACTORYRESET },
195 { "wipeingui", TWFLAG_WIPEINGUI },
Ethan Yonker1b190162016-12-05 15:25:19 -0600196 { "slotselect", TWFLAG_SLOTSELECT },
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600197 { "wait", TWFLAG_WAIT },
198 { "verify", TWFLAG_VERIFY },
199 { "check", TWFLAG_CHECK },
200 { "altdevice", TWFLAG_ALTDEVICE },
201 { "notrim", TWFLAG_NOTRIM },
202 { "voldmanaged=", TWFLAG_VOLDMANAGED },
203 { "formattable", TWFLAG_FORMATTABLE },
204 { "resize", TWFLAG_RESIZE },
Ethan Yonker93382822018-11-01 15:25:31 -0500205 { "keydirectory=", TWFLAG_KEYDIRECTORY },
Peter Cai28ea7b02019-05-24 11:38:54 +0800206 { "wrappedkey", TWFLAG_WRAPPEDKEY },
Matt Mower2416a502016-04-12 19:54:46 -0500207 { 0, 0 },
208};
209
that9e0593e2014-10-08 00:01:24 +0200210TWPartition::TWPartition() {
Dees_Troy51a0e822012-09-05 15:24:24 -0400211 Can_Be_Mounted = false;
212 Can_Be_Wiped = false;
Dees_Troya13d74f2013-03-24 08:54:55 -0500213 Can_Be_Backed_Up = false;
Vojtech Bocek1dc30982013-08-30 21:49:30 +0200214 Use_Rm_Rf = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400215 Wipe_During_Factory_Reset = false;
216 Wipe_Available_in_GUI = false;
217 Is_SubPartition = false;
Dees_Troy2691f9d2012-09-24 11:15:49 -0400218 Has_SubPartition = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400219 SubPartition_Of = "";
220 Symlink_Path = "";
221 Symlink_Mount_Point = "";
222 Mount_Point = "";
Dees_Troye58d5262012-09-21 12:27:57 -0400223 Backup_Path = "";
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600224 Wildcard_Block_Device = false;
225 Sysfs_Entry = "";
Dees_Troy38bd7602012-09-14 13:33:53 -0400226 Actual_Block_Device = "";
227 Primary_Block_Device = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400228 Alternate_Block_Device = "";
229 Removable = false;
230 Is_Present = false;
231 Length = 0;
232 Size = 0;
233 Used = 0;
234 Free = 0;
235 Backup_Size = 0;
236 Can_Be_Encrypted = false;
237 Is_Encrypted = false;
238 Is_Decrypted = false;
Ethan Yonkerbd7492d2016-12-07 13:55:01 -0600239 Is_FBE = false;
Ethan Yonker253368a2014-11-25 15:00:52 -0600240 Mount_To_Decrypt = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400241 Decrypted_Block_Device = "";
242 Display_Name = "";
Dees_Troya13d74f2013-03-24 08:54:55 -0500243 Backup_Display_Name = "";
244 Storage_Name = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400245 Backup_Name = "";
Dees_Troy63c8df72012-09-10 14:02:05 -0400246 Backup_FileName = "";
Dees_Troy38bd7602012-09-14 13:33:53 -0400247 MTD_Name = "";
bigbiffce8f83c2015-12-12 18:30:21 -0500248 Backup_Method = BM_NONE;
Dees_Troy83bd4832013-05-04 12:39:56 +0000249 Can_Encrypt_Backup = false;
250 Use_Userdata_Encryption = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400251 Has_Data_Media = false;
Dees_Troye58d5262012-09-21 12:27:57 -0400252 Has_Android_Secure = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400253 Is_Storage = false;
Dees_Troya13d74f2013-03-24 08:54:55 -0500254 Is_Settings_Storage = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400255 Storage_Path = "";
256 Current_File_System = "";
257 Fstab_File_System = "";
Hashcode62bd9e02013-11-19 21:59:42 -0800258 Mount_Flags = 0;
259 Mount_Options = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400260 Format_Block_Size = 0;
Dees_Troy68cab492012-12-12 19:29:35 +0000261 Ignore_Blkid = false;
Dees_Troy16c2b312013-01-15 16:51:18 +0000262 Retain_Layout_Version = false;
dianlujitao4879b372018-12-03 18:45:47 +0800263 Crypto_Key_Location = "";
Ethan Yonker726a0202014-12-16 20:01:38 -0600264 MTP_Storage_ID = 0;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600265 Can_Flash_Img = false;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500266 Mount_Read_Only = false;
Ethan Yonker66a19492015-12-10 10:19:45 -0600267 Is_Adopted_Storage = false;
268 Adopted_GUID = "";
Ethan Yonker1b190162016-12-05 15:25:19 -0600269 SlotSelect = false;
Ethan Yonker93382822018-11-01 15:25:31 -0500270 Key_Directory = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400271}
272
273TWPartition::~TWPartition(void) {
274 // Do nothing
275}
276
Chaosmasterf6e42ce2020-01-27 00:17:04 +0100277bool 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 -0500278 char full_line[MAX_FSTAB_LINE_LENGTH];
Matt Mower2416a502016-04-12 19:54:46 -0500279 char twflags[MAX_FSTAB_LINE_LENGTH] = "";
Dees_Troy5bf43922012-09-07 16:07:55 -0400280 char* ptr;
Matt Mower2b2dd152016-04-26 11:24:08 -0500281 int line_len = strlen(fstab_line), index = 0, item_index = 0;
Dees_Troya13d74f2013-03-24 08:54:55 -0500282 bool skip = false;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600283 int fstab_version = 1, mount_point_index = 0, fs_index = 1, block_device_index = 2;
284 TWPartition *additional_entry = NULL;
285 std::map<string, Flags_Map>::iterator it;
Dees_Troy5bf43922012-09-07 16:07:55 -0400286
Matt Mower2b2dd152016-04-26 11:24:08 -0500287 strlcpy(full_line, fstab_line, sizeof(full_line));
Dees_Troy51127312012-09-08 13:08:49 -0400288 for (index = 0; index < line_len; index++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500289 if (full_line[index] == 34)
290 skip = !skip;
291 if (!skip && full_line[index] <= 32)
Dees_Troy5bf43922012-09-07 16:07:55 -0400292 full_line[index] = '\0';
Dees_Troy5bf43922012-09-07 16:07:55 -0400293 }
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600294 if (line_len < 10)
295 return false; // There can't possibly be a valid fstab line that is less than 10 chars
296 if (strncmp(fstab_line, "/dev/", strlen("/dev/")) == 0 || strncmp(fstab_line, "/devices/", strlen("/devices/")) == 0) {
297 fstab_version = 2;
298 block_device_index = 0;
299 mount_point_index = 1;
300 fs_index = 2;
301 }
302
303 index = 0;
Dees_Troy5bf43922012-09-07 16:07:55 -0400304 while (index < line_len) {
305 while (index < line_len && full_line[index] == '\0')
306 index++;
307 if (index >= line_len)
308 continue;
309 ptr = full_line + index;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600310 if (item_index == mount_point_index) {
311 Mount_Point = ptr;
312 if (fstab_version == 2) {
313 additional_entry = PartitionManager.Find_Partition_By_Path(Mount_Point);
Chaosmasterf6e42ce2020-01-27 00:17:04 +0100314 if (!Sar_Detect && additional_entry) {
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600315 LOGINFO("Found an additional entry for '%s'\n", Mount_Point.c_str());
316 }
317 }
Chaosmasterf6e42ce2020-01-27 00:17:04 +0100318 if(!Sar_Detect)
319 LOGINFO("Processing '%s'\n", Mount_Point.c_str());
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600320 Backup_Path = Mount_Point;
321 Storage_Path = Mount_Point;
322 Display_Name = ptr + 1;
323 Backup_Display_Name = Display_Name;
324 Storage_Name = Display_Name;
325 item_index++;
326 } else if (item_index == fs_index) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400327 // File System
328 Fstab_File_System = ptr;
329 Current_File_System = ptr;
330 item_index++;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600331 } else if (item_index == block_device_index) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400332 // Primary Block Device
Dees_Troy38bd7602012-09-14 13:33:53 -0400333 if (Fstab_File_System == "mtd" || Fstab_File_System == "yaffs2") {
Dees_Troy094207a2012-09-26 12:00:39 -0400334 MTD_Name = ptr;
335 Find_MTD_Block_Device(MTD_Name);
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400336 } else if (Fstab_File_System == "bml") {
337 if (Mount_Point == "/boot")
338 MTD_Name = "boot";
339 else if (Mount_Point == "/recovery")
340 MTD_Name = "recovery";
341 Primary_Block_Device = ptr;
342 if (*ptr != '/')
Dees_Troy2673cec2013-04-02 20:22:16 +0000343 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 -0400344 } else if (*ptr != '/') {
Dees_Troy5bf43922012-09-07 16:07:55 -0400345 if (Display_Error)
Matt Mower2b2dd152016-04-26 11:24:08 -0500346 LOGERR("Invalid block device '%s' in fstab line '%s'", ptr, fstab_line);
Dees_Troy5bf43922012-09-07 16:07:55 -0400347 else
Matt Mower2b2dd152016-04-26 11:24:08 -0500348 LOGINFO("Invalid block device '%s' in fstab line '%s'", ptr, fstab_line);
Matt Mower72c87ce2016-04-26 14:34:56 -0500349 return false;
Dees_Troy38bd7602012-09-14 13:33:53 -0400350 } else {
351 Primary_Block_Device = ptr;
352 Find_Real_Block_Device(Primary_Block_Device, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400353 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400354 item_index++;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600355 } else if (item_index > 2) {
356 if (fstab_version == 2) {
357 if (item_index == 3) {
358 Process_FS_Flags(ptr);
359 if (additional_entry) {
360 additional_entry->Save_FS_Flags(Fstab_File_System, Mount_Flags, Mount_Options);
361 return false; // We save the extra fs flags in the other partition entry and by returning false, this entry will be deleted
362 }
363 } else {
364 strlcpy(twflags, ptr, sizeof(twflags));
365 }
366 item_index++;
367 } else if (*ptr == '/') { // v2 fstab does not allow alternate block devices
Dees_Troy5bf43922012-09-07 16:07:55 -0400368 // Alternate Block Device
369 Alternate_Block_Device = ptr;
370 Find_Real_Block_Device(Alternate_Block_Device, Display_Error);
371 } else if (strlen(ptr) > 7 && strncmp(ptr, "length=", 7) == 0) {
372 // Partition length
373 ptr += 7;
374 Length = atoi(ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400375 } else if (strlen(ptr) > 6 && strncmp(ptr, "flags=", 6) == 0) {
376 // Custom flags, save for later so that new values aren't overwritten by defaults
377 ptr += 6;
Matt Mower2416a502016-04-12 19:54:46 -0500378 strlcpy(twflags, ptr, sizeof(twflags));
Dees_Troy38bd7602012-09-14 13:33:53 -0400379 } else if (strlen(ptr) == 4 && (strncmp(ptr, "NULL", 4) == 0 || strncmp(ptr, "null", 4) == 0 || strncmp(ptr, "null", 4) == 0)) {
380 // Do nothing
Dees_Troy5bf43922012-09-07 16:07:55 -0400381 } else {
382 // Unhandled data
Matt Mower2b2dd152016-04-26 11:24:08 -0500383 LOGINFO("Unhandled fstab information '%s' in fstab line '%s'\n", ptr, fstab_line);
Dees_Troy5bf43922012-09-07 16:07:55 -0400384 }
385 }
386 while (index < line_len && full_line[index] != '\0')
387 index++;
388 }
389
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600390 // override block devices from the v2 fstab with the ones we read from the twrp.flags file in case they are different
391 if (fstab_version == 2 && twrp_flags && twrp_flags->size() > 0) {
392 it = twrp_flags->find(Mount_Point);
393 if (it != twrp_flags->end()) {
394 if (!it->second.Primary_Block_Device.empty()) {
395 Primary_Block_Device = it->second.Primary_Block_Device;
396 Find_Real_Block_Device(Primary_Block_Device, Display_Error);
397 }
398 if (!it->second.Alternate_Block_Device.empty()) {
399 Alternate_Block_Device = it->second.Alternate_Block_Device;
400 Find_Real_Block_Device(Alternate_Block_Device, Display_Error);
401 }
402 }
403 }
404
405 if (strncmp(fstab_line, "/devices/", strlen("/devices/")) == 0) {
406 Sysfs_Entry = Primary_Block_Device;
407 Primary_Block_Device = "";
408 Is_Storage = true;
409 Removable = true;
410 Wipe_Available_in_GUI = true;
411 Wildcard_Block_Device = true;
412 }
413 if (Primary_Block_Device.find("*") != string::npos)
414 Wildcard_Block_Device = true;
415
Chaosmasterf6e42ce2020-01-27 00:17:04 +0100416 if (Sar_Detect) {
417 if(Is_File_System(Fstab_File_System) && (Mount_Point == "/" || Mount_Point == "/system" || Mount_Point == "/system_root"))
418 Find_Actual_Block_Device();
419 else
420 return true;
421 } else if (Mount_Point == "auto") {
dianlujitaob76a73a2020-04-30 20:33:20 +0800422 Mount_Point = "/auto" + to_string(auto_index);
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600423 Backup_Path = Mount_Point;
424 Storage_Path = Mount_Point;
dianlujitaob76a73a2020-04-30 20:33:20 +0800425 Backup_Name = Mount_Point.substr(1);
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600426 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);
dianlujitaob76a73a2020-04-30 20:33:20 +0800445 Backup_Name = Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
Chaosmaster3b682732020-01-25 21:09:28 +0100446 if (Mount_Point == "/" || Mount_Point == "/system" || Mount_Point == "/system_root") {
Dees_Troy5bf43922012-09-07 16:07:55 -0400447 Display_Name = "System";
dianlujitaob76a73a2020-04-30 20:33:20 +0800448 Backup_Name = "system";
Dees_Troya13d74f2013-03-24 08:54:55 -0500449 Backup_Display_Name = Display_Name;
450 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400451 Wipe_Available_in_GUI = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500452 Can_Be_Backed_Up = true;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500453 Mount_Read_Only = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400454 } else if (Mount_Point == "/data") {
455 Display_Name = "Data";
Dees_Troya13d74f2013-03-24 08:54:55 -0500456 Backup_Display_Name = Display_Name;
457 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400458 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400459 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500460 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000461 Can_Encrypt_Backup = true;
462 Use_Userdata_Encryption = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400463 } else if (Mount_Point == "/cache") {
464 Display_Name = "Cache";
Dees_Troya13d74f2013-03-24 08:54:55 -0500465 Backup_Display_Name = Display_Name;
466 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400467 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400468 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500469 Can_Be_Backed_Up = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400470 } else if (Mount_Point == "/datadata") {
Dees_Troy51127312012-09-08 13:08:49 -0400471 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400472 Display_Name = "DataData";
Dees_Troya13d74f2013-03-24 08:54:55 -0500473 Backup_Display_Name = Display_Name;
474 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400475 Is_SubPartition = true;
476 SubPartition_Of = "/data";
Dees_Troy5bf43922012-09-07 16:07:55 -0400477 DataManager::SetValue(TW_HAS_DATADATA, 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500478 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000479 Can_Encrypt_Backup = true;
480 Use_Userdata_Encryption = false; // This whole partition should be encrypted
Dees_Troy5bf43922012-09-07 16:07:55 -0400481 } else if (Mount_Point == "/sd-ext") {
Dees_Troy51127312012-09-08 13:08:49 -0400482 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400483 Display_Name = "SD-Ext";
Dees_Troya13d74f2013-03-24 08:54:55 -0500484 Backup_Display_Name = Display_Name;
485 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400486 Wipe_Available_in_GUI = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400487 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500488 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000489 Can_Encrypt_Backup = true;
490 Use_Userdata_Encryption = true;
Dees_Troy2c50e182012-09-26 20:05:28 -0400491 } else if (Mount_Point == "/boot") {
492 Display_Name = "Boot";
Dees_Troya13d74f2013-03-24 08:54:55 -0500493 Backup_Display_Name = Display_Name;
Dees_Troy2c50e182012-09-26 20:05:28 -0400494 DataManager::SetValue("tw_boot_is_mountable", 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500495 Can_Be_Backed_Up = true;
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500496 } else if (Mount_Point == "/vendor") {
497 Display_Name = "Vendor";
498 Backup_Display_Name = Display_Name;
499 Storage_Name = Display_Name;
500 Mount_Read_Only = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400501 }
502#ifdef TW_EXTERNAL_STORAGE_PATH
503 if (Mount_Point == EXPAND(TW_EXTERNAL_STORAGE_PATH)) {
504 Is_Storage = true;
505 Storage_Path = EXPAND(TW_EXTERNAL_STORAGE_PATH);
Dees_Troyc51f1f92012-09-20 15:32:13 -0400506 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500507 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400508#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000509 if (Mount_Point == "/sdcard" || Mount_Point == "/external_sd" || Mount_Point == "/external_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400510 Is_Storage = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400511 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500512 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400513#endif
Dees_Troyb05ddee2013-01-28 20:24:50 +0000514 }
Dees_Troy8170a922012-09-18 15:40:25 -0400515#ifdef TW_INTERNAL_STORAGE_PATH
516 if (Mount_Point == EXPAND(TW_INTERNAL_STORAGE_PATH)) {
517 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500518 Is_Settings_Storage = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400519 Storage_Path = EXPAND(TW_INTERNAL_STORAGE_PATH);
Dees_Troya13d74f2013-03-24 08:54:55 -0500520 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400521 }
522#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000523 if (Mount_Point == "/emmc" || Mount_Point == "/internal_sd" || Mount_Point == "/internal_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400524 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500525 Is_Settings_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500526 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400527 }
528#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400529 } else if (Is_Image(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400530 Find_Actual_Block_Device();
Ethan Yonker1b190162016-12-05 15:25:19 -0600531 Setup_Image();
Dees_Troya13d74f2013-03-24 08:54:55 -0500532 if (Mount_Point == "/boot") {
533 Display_Name = "Boot";
534 Backup_Display_Name = Display_Name;
535 Can_Be_Backed_Up = true;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600536 Can_Flash_Img = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500537 } else if (Mount_Point == "/recovery") {
538 Display_Name = "Recovery";
539 Backup_Display_Name = Display_Name;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600540 Can_Flash_Img = true;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500541 } else if (Mount_Point == "/system_image") {
542 Display_Name = "System Image";
543 Backup_Display_Name = Display_Name;
HashBanged974bb2016-01-30 14:20:09 -0500544 Can_Flash_Img = true;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500545 Can_Be_Backed_Up = true;
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500546 } else if (Mount_Point == "/vendor_image") {
547 Display_Name = "Vendor Image";
548 Backup_Display_Name = Display_Name;
HashBanged974bb2016-01-30 14:20:09 -0500549 Can_Flash_Img = true;
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500550 Can_Be_Backed_Up = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500551 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400552 }
553
Matt Mower2416a502016-04-12 19:54:46 -0500554 // Process TWRP fstab flags
555 if (strlen(twflags) > 0) {
556 string Prev_Display_Name = Display_Name;
557 string Prev_Storage_Name = Storage_Name;
558 string Prev_Backup_Display_Name = Backup_Display_Name;
559 Display_Name = "";
560 Storage_Name = "";
561 Backup_Display_Name = "";
562
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600563 Process_TW_Flags(twflags, (fstab_version == 1), fstab_version);
564 Save_FS_Flags(Fstab_File_System, Mount_Flags, Mount_Options);
Matt Mower2416a502016-04-12 19:54:46 -0500565
566 bool has_display_name = !Display_Name.empty();
567 bool has_storage_name = !Storage_Name.empty();
568 bool has_backup_name = !Backup_Display_Name.empty();
569 if (!has_display_name) Display_Name = Prev_Display_Name;
570 if (!has_storage_name) Storage_Name = Prev_Storage_Name;
571 if (!has_backup_name) Backup_Display_Name = Prev_Backup_Display_Name;
572
573 if (has_display_name && !has_storage_name)
574 Storage_Name = Display_Name;
575 if (!has_display_name && has_storage_name)
576 Display_Name = Storage_Name;
577 if (has_display_name && !has_backup_name && Backup_Display_Name != "Android Secure")
578 Backup_Display_Name = Display_Name;
579 if (!has_display_name && has_backup_name)
580 Display_Name = Backup_Display_Name;
581 }
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600582
583 if (fstab_version == 2 && twrp_flags && twrp_flags->size() > 0) {
584 it = twrp_flags->find(Mount_Point);
585 if (it != twrp_flags->end()) {
586 char twrpflags[MAX_FSTAB_LINE_LENGTH] = "";
587 int skip = 0;
588 string Flags = it->second.Flags;
589 strcpy(twrpflags, Flags.c_str());
590 if (strlen(twrpflags) > strlen("flags=") && strncmp(twrpflags, "flags=", strlen("flags=")) == 0)
591 skip += strlen("flags=");
592 char* flagptr = twrpflags;
593 flagptr += skip;
594 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
595 }
596 }
nkk7198fc3992017-12-16 16:26:42 +0200597
598 if (Mount_Point == "/persist" && Can_Be_Mounted) {
599 bool mounted = Is_Mounted();
600 if (mounted || Mount(false)) {
601 // Read the backup settings file
602 DataManager::LoadPersistValues();
603 TWFunc::Fixup_Time_On_Boot("/persist/time/");
604 if (!mounted)
605 UnMount(false);
606 }
607 }
608
Chaosmasterf6e42ce2020-01-27 00:17:04 +0100609 if (Is_File_System(Fstab_File_System) && (Mount_Point == "/" || Mount_Point == "/system" || Mount_Point == "/system_root")) {
610 if (Sar_Detect) {
611 Mount_Point = "/s";
612 Mount_Read_Only = true;
613 Can_Be_Mounted = true;
614 } else {
615 Mount_Point = PartitionManager.Get_Android_Root_Path();
616 Backup_Path = Mount_Point;
617 Storage_Path = Mount_Point;
618 Make_Dir(Mount_Point, Display_Error);
619 }
620 }
621
Dees_Troy51127312012-09-08 13:08:49 -0400622 return true;
623}
624
Matt Mower72c87ce2016-04-26 14:34:56 -0500625void TWPartition::Partition_Post_Processing(bool Display_Error) {
626 if (Mount_Point == "/data")
627 Setup_Data_Partition(Display_Error);
628 else if (Mount_Point == "/cache")
629 Setup_Cache_Partition(Display_Error);
630}
631
Ethan Yonkerbd7492d2016-12-07 13:55:01 -0600632void TWPartition::ExcludeAll(const string& path) {
633 backup_exclusions.add_absolute_dir(path);
634 wipe_exclusions.add_absolute_dir(path);
635}
636
Matt Mower72c87ce2016-04-26 14:34:56 -0500637void TWPartition::Setup_Data_Partition(bool Display_Error) {
638 if (Mount_Point != "/data")
639 return;
640
641 // Ensure /data is not mounted as tmpfs for qcom hardware decrypt
642 UnMount(false);
643
644#ifdef TW_INCLUDE_CRYPTO
645 if (datamedia)
646 Setup_Data_Media();
647 Can_Be_Encrypted = true;
648 char crypto_blkdev[255];
649 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
650 if (strcmp(crypto_blkdev, "error") != 0) {
651 DataManager::SetValue(TW_IS_DECRYPTED, 1);
652 Is_Encrypted = true;
653 Is_Decrypted = true;
Ethan Yonker93382822018-11-01 15:25:31 -0500654 if (Key_Directory.empty())
655 Is_FBE = false;
656 else
657 Is_FBE = true;
Ethan Yonkerbd7492d2016-12-07 13:55:01 -0600658 DataManager::SetValue(TW_IS_FBE, 0);
Matt Mower72c87ce2016-04-26 14:34:56 -0500659 Decrypted_Block_Device = crypto_blkdev;
660 LOGINFO("Data already decrypted, new block device: '%s'\n", crypto_blkdev);
661 } else if (!Mount(false)) {
662 if (Is_Present) {
Ethan Yonker93382822018-11-01 15:25:31 -0500663 if (Key_Directory.empty()) {
664 set_partition_data(Actual_Block_Device.c_str(), Crypto_Key_Location.c_str(), Fstab_File_System.c_str());
665 if (cryptfs_check_footer() == 0) {
666 Is_Encrypted = true;
667 Is_Decrypted = false;
668 Can_Be_Mounted = false;
669 Current_File_System = "emmc";
670 Setup_Image();
671 DataManager::SetValue(TW_IS_ENCRYPTED, 1);
672 DataManager::SetValue(TW_CRYPTO_PWTYPE, cryptfs_get_password_type());
673 DataManager::SetValue(TW_CRYPTO_PASSWORD, "");
674 DataManager::SetValue("tw_crypto_display", "");
675 } else {
676 gui_err("mount_data_footer=Could not mount /data and unable to find crypto footer.");
677 }
678 } else {
Matt Mower72c87ce2016-04-26 14:34:56 -0500679 Is_Encrypted = true;
680 Is_Decrypted = false;
Matt Mower72c87ce2016-04-26 14:34:56 -0500681 }
Ethan Yonker93382822018-11-01 15:25:31 -0500682 } else if (Key_Directory.empty()) {
Matt Mower72c87ce2016-04-26 14:34:56 -0500683 LOGERR("Primary block device '%s' for mount point '%s' is not present!\n", Primary_Block_Device.c_str(), Mount_Point.c_str());
684 }
685 } else {
mauronofrio9bf73352019-11-23 22:27:34 +0100686
687 if (!Decrypt_FBE_DE()) {
688 LOGINFO("Trying wrapped key.\n");
689 property_set("fbe.data.wrappedkey", "true");
690 if (!Decrypt_FBE_DE()) {
bigbiffd58ba182020-03-23 10:02:29 -0400691 LOGERR("Unable to decrypt FBE device\n");
mauronofrio9bf73352019-11-23 22:27:34 +0100692 }
693 }
Matt Mower72c87ce2016-04-26 14:34:56 -0500694 }
695 if (datamedia && (!Is_Encrypted || (Is_Encrypted && Is_Decrypted))) {
696 Setup_Data_Media();
697 Recreate_Media_Folder();
698 }
699#else
700 if (datamedia) {
701 Setup_Data_Media();
702 Recreate_Media_Folder();
703 }
704#endif
705}
706
Ethan Yonker93382822018-11-01 15:25:31 -0500707bool TWPartition::Decrypt_FBE_DE() {
708if (TWFunc::Path_Exists("/data/unencrypted/key/version")) {
709 LOGINFO("File Based Encryption is present\n");
710#ifdef TW_INCLUDE_FBE
711 ExcludeAll(Mount_Point + "/convert_fbe");
712 ExcludeAll(Mount_Point + "/unencrypted");
713 //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
714 ExcludeAll(Mount_Point + "/misc/vold/user_keys");
715 //ExcludeAll(Mount_Point + "/system_ce");
716 //ExcludeAll(Mount_Point + "/system_de");
717 //ExcludeAll(Mount_Point + "/misc_ce");
718 //ExcludeAll(Mount_Point + "/misc_de");
719 ExcludeAll(Mount_Point + "/system/gatekeeper.password.key");
720 ExcludeAll(Mount_Point + "/system/gatekeeper.pattern.key");
721 ExcludeAll(Mount_Point + "/system/locksettings.db");
722 //ExcludeAll(Mount_Point + "/system/locksettings.db-shm"); // don't seem to need this one, but the other 2 are needed
723 ExcludeAll(Mount_Point + "/system/locksettings.db-wal");
724 //ExcludeAll(Mount_Point + "/user_de");
725 //ExcludeAll(Mount_Point + "/misc/profiles/cur/0"); // might be important later
726 ExcludeAll(Mount_Point + "/misc/gatekeeper");
727 ExcludeAll(Mount_Point + "/misc/keystore");
728 ExcludeAll(Mount_Point + "/drm/kek.dat");
729 ExcludeAll(Mount_Point + "/system_de/0/spblob"); // contains data needed to decrypt pixel 2
730 int retry_count = 3;
731 while (!Decrypt_DE() && --retry_count)
732 usleep(2000);
733 if (retry_count > 0) {
734 property_set("ro.crypto.state", "encrypted");
735 Is_Encrypted = true;
736 Is_Decrypted = false;
737 Is_FBE = true;
738 DataManager::SetValue(TW_IS_FBE, 1);
739 DataManager::SetValue(TW_IS_ENCRYPTED, 1);
740 string filename;
741 int pwd_type = Get_Password_Type(0, filename);
742 if (pwd_type < 0) {
743 LOGERR("This TWRP does not have synthetic password decrypt support\n");
744 pwd_type = 0; // default password
745 }
746 DataManager::SetValue(TW_CRYPTO_PWTYPE, pwd_type);
747 DataManager::SetValue(TW_CRYPTO_PASSWORD, "");
748 DataManager::SetValue("tw_crypto_display", "");
749 return true;
750 }
751#else
752 LOGERR("FBE found but FBE support not present in TWRP\n");
753#endif
754 }
755 return false;
756}
757
Matt Mower72c87ce2016-04-26 14:34:56 -0500758void TWPartition::Setup_Cache_Partition(bool Display_Error __unused) {
759 if (Mount_Point != "/cache")
760 return;
761
762 if (!Mount(true))
763 return;
764
765 if (!TWFunc::Path_Exists("/cache/recovery/.")) {
766 LOGINFO("Recreating /cache/recovery folder\n");
767 if (mkdir("/cache/recovery", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0)
768 LOGERR("Could not create /cache/recovery\n");
769 }
770}
771
Matt Mower4ab42b12016-04-21 13:52:18 -0500772void TWPartition::Process_FS_Flags(const char *str) {
773 char *options = strdup(str);
774 char *ptr, *savep;
Hashcode62bd9e02013-11-19 21:59:42 -0800775
Matt Mower4ab42b12016-04-21 13:52:18 -0500776 Mount_Options = "";
Hashcode62bd9e02013-11-19 21:59:42 -0800777
Matt Mower4ab42b12016-04-21 13:52:18 -0500778 // Avoid issues with potentially nested strtok by using strtok_r
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600779 for (ptr = strtok_r(options, ",", &savep); ptr; ptr = strtok_r(NULL, ",", &savep)) {
780 char *equals = strstr(ptr, "=");
781 size_t name_len;
Matt Mower4ab42b12016-04-21 13:52:18 -0500782
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600783 if (!equals)
784 name_len = strlen(ptr);
785 else
786 name_len = equals - ptr;
787
788 // There are some flags that we want to ignore in TWRP
789 bool found_match = false;
790 for (const char** ignored_mount_item = ignored_mount_items; *ignored_mount_item; ignored_mount_item++) {
791 if (strncmp(ptr, *ignored_mount_item, name_len) == 0) {
792 found_match = true;
Hashcode62bd9e02013-11-19 21:59:42 -0800793 break;
794 }
795 }
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600796 if (found_match)
797 continue;
Hashcode62bd9e02013-11-19 21:59:42 -0800798
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600799 // mount_flags are never postfixed by '='
800 if (!equals) {
801 const struct flag_list* mount_flag = mount_flags;
802 for (; mount_flag->name; mount_flag++) {
803 if (strcmp(ptr, mount_flag->name) == 0) {
804 if (mount_flag->flag == MS_RDONLY)
805 Mount_Read_Only = true;
806 else
807 Mount_Flags |= (unsigned)mount_flag->flag;
808 found_match = true;
809 break;
810 }
811 }
812 if (found_match)
813 continue;
Matt Mower4ab42b12016-04-21 13:52:18 -0500814 }
815
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600816 // If we aren't ignoring this flag and it's not a mount flag, then it must be a mount option
817 if (!Mount_Options.empty())
818 Mount_Options += ",";
819 Mount_Options += ptr;
Matt Mower4ab42b12016-04-21 13:52:18 -0500820 }
821 free(options);
Hashcode62bd9e02013-11-19 21:59:42 -0800822}
823
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600824void TWPartition::Save_FS_Flags(const string& local_File_System, int local_Mount_Flags, const string& local_Mount_Options) {
825 partition_fs_flags_struct flags;
826 flags.File_System = local_File_System;
827 flags.Mount_Flags = local_Mount_Flags;
828 flags.Mount_Options = local_Mount_Options;
829 fs_flags.push_back(flags);
830}
831
Matt Mower2416a502016-04-12 19:54:46 -0500832void TWPartition::Apply_TW_Flag(const unsigned flag, const char* str, const bool val) {
833 switch (flag) {
834 case TWFLAG_ANDSEC:
835 Has_Android_Secure = val;
836 break;
837 case TWFLAG_BACKUP:
838 Can_Be_Backed_Up = val;
839 break;
840 case TWFLAG_BACKUPNAME:
841 Backup_Display_Name = str;
842 break;
843 case TWFLAG_BLOCKSIZE:
844 Format_Block_Size = (unsigned long)(atol(str));
845 break;
846 case TWFLAG_CANBEWIPED:
847 Can_Be_Wiped = val;
848 break;
849 case TWFLAG_CANENCRYPTBACKUP:
850 Can_Encrypt_Backup = val;
851 break;
852 case TWFLAG_DEFAULTS:
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600853 case TWFLAG_WAIT:
854 case TWFLAG_VERIFY:
855 case TWFLAG_CHECK:
856 case TWFLAG_NOTRIM:
857 case TWFLAG_VOLDMANAGED:
858 case TWFLAG_RESIZE:
Matt Mower2416a502016-04-12 19:54:46 -0500859 // Do nothing
860 break;
861 case TWFLAG_DISPLAY:
862 Display_Name = str;
863 break;
864 case TWFLAG_ENCRYPTABLE:
865 case TWFLAG_FORCEENCRYPT:
866 Crypto_Key_Location = str;
867 break;
Ethan Yonkere9afc3d2018-08-30 15:16:27 -0500868 case TWFLAG_FILEENCRYPTION:
869 // This flag isn't used by TWRP but is needed in 9.0 FBE decrypt
870 // fileencryption=ice:aes-256-heh
871 {
872 std::string FBE = str;
Ethan Yonkere9afc3d2018-08-30 15:16:27 -0500873 size_t colon_loc = FBE.find(":");
874 if (colon_loc == std::string::npos) {
Ethan Yonker93382822018-11-01 15:25:31 -0500875 property_set("fbe.contents", FBE.c_str());
876 property_set("fbe.filenames", "");
877 LOGINFO("FBE contents '%s', filenames ''\n", FBE.c_str());
Ethan Yonkere9afc3d2018-08-30 15:16:27 -0500878 break;
879 }
Ethan Yonker93382822018-11-01 15:25:31 -0500880 std::string FBE_contents, FBE_filenames;
Ethan Yonkere9afc3d2018-08-30 15:16:27 -0500881 FBE_contents = FBE.substr(0, colon_loc);
882 FBE_filenames = FBE.substr(colon_loc + 1);
883 property_set("fbe.contents", FBE_contents.c_str());
884 property_set("fbe.filenames", FBE_filenames.c_str());
885 LOGINFO("FBE contents '%s', filenames '%s'\n", FBE_contents.c_str(), FBE_filenames.c_str());
886 }
887 break;
Peter Cai28ea7b02019-05-24 11:38:54 +0800888 case TWFLAG_WRAPPEDKEY:
889 // Set fbe.data.wrappedkey to true
890 {
891 property_set("fbe.data.wrappedkey", "true");
892 LOGINFO("FBE wrapped key enabled\n");
893 }
894 break;
Matt Mower2416a502016-04-12 19:54:46 -0500895 case TWFLAG_FLASHIMG:
896 Can_Flash_Img = val;
897 break;
898 case TWFLAG_FSFLAGS:
Matt Mower4ab42b12016-04-21 13:52:18 -0500899 Process_FS_Flags(str);
Matt Mower2416a502016-04-12 19:54:46 -0500900 break;
901 case TWFLAG_IGNOREBLKID:
902 Ignore_Blkid = val;
903 break;
904 case TWFLAG_LENGTH:
905 Length = atoi(str);
906 break;
907 case TWFLAG_MOUNTTODECRYPT:
908 Mount_To_Decrypt = val;
909 break;
910 case TWFLAG_REMOVABLE:
911 Removable = val;
912 break;
913 case TWFLAG_RETAINLAYOUTVERSION:
914 Retain_Layout_Version = val;
915 break;
916 case TWFLAG_SETTINGSSTORAGE:
917 Is_Settings_Storage = val;
918 if (Is_Settings_Storage)
919 Is_Storage = true;
920 break;
921 case TWFLAG_STORAGE:
922 Is_Storage = val;
923 break;
924 case TWFLAG_STORAGENAME:
925 Storage_Name = str;
926 break;
927 case TWFLAG_SUBPARTITIONOF:
928 Is_SubPartition = true;
929 SubPartition_Of = str;
930 break;
931 case TWFLAG_SYMLINK:
932 Symlink_Path = str;
933 break;
934 case TWFLAG_USERDATAENCRYPTBACKUP:
935 Use_Userdata_Encryption = val;
936 if (Use_Userdata_Encryption)
937 Can_Encrypt_Backup = true;
938 break;
939 case TWFLAG_USERMRF:
940 Use_Rm_Rf = val;
941 break;
942 case TWFLAG_WIPEDURINGFACTORYRESET:
943 Wipe_During_Factory_Reset = val;
944 if (Wipe_During_Factory_Reset) {
945 Can_Be_Wiped = true;
946 Wipe_Available_in_GUI = true;
947 }
948 break;
949 case TWFLAG_WIPEINGUI:
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600950 case TWFLAG_FORMATTABLE:
Matt Mower2416a502016-04-12 19:54:46 -0500951 Wipe_Available_in_GUI = val;
952 if (Wipe_Available_in_GUI)
953 Can_Be_Wiped = true;
954 break;
Ethan Yonker1b190162016-12-05 15:25:19 -0600955 case TWFLAG_SLOTSELECT:
956 SlotSelect = true;
957 break;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600958 case TWFLAG_ALTDEVICE:
959 Alternate_Block_Device = str;
960 break;
Ethan Yonker93382822018-11-01 15:25:31 -0500961 case TWFLAG_KEYDIRECTORY:
962 Key_Directory = str;
bigbiffd58ba182020-03-23 10:02:29 -0400963 break;
Matt Mower2416a502016-04-12 19:54:46 -0500964 default:
965 // Should not get here
966 LOGINFO("Flag identified for processing, but later unmatched: %i\n", flag);
967 break;
968 }
969}
Dees_Troy51127312012-09-08 13:08:49 -0400970
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600971void TWPartition::Process_TW_Flags(char *flags, bool Display_Error, int fstab_ver) {
Matt Mower2416a502016-04-12 19:54:46 -0500972 char separator[2] = {'\n', 0};
973 char *ptr, *savep;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600974 char source_separator = ';';
975
976 if (fstab_ver == 2)
977 source_separator = ',';
Matt Mower2416a502016-04-12 19:54:46 -0500978
979 // Semicolons within double-quotes are not forbidden, so replace
980 // only the semicolons intended as separators with '\n' for strtok
981 for (unsigned i = 0, skip = 0; i < strlen(flags); i++) {
982 if (flags[i] == '\"')
Dees_Troya13d74f2013-03-24 08:54:55 -0500983 skip = !skip;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600984 if (!skip && flags[i] == source_separator)
Matt Mower2416a502016-04-12 19:54:46 -0500985 flags[i] = separator[0];
Dees_Troy51127312012-09-08 13:08:49 -0400986 }
987
Matt Mower2416a502016-04-12 19:54:46 -0500988 // Avoid issues with potentially nested strtok by using strtok_r
989 ptr = strtok_r(flags, separator, &savep);
990 while (ptr) {
991 int ptr_len = strlen(ptr);
992 const struct flag_list* tw_flag = tw_flags;
Hashcode62bd9e02013-11-19 21:59:42 -0800993
Matt Mower2416a502016-04-12 19:54:46 -0500994 for (; tw_flag->name; tw_flag++) {
995 int flag_len = strlen(tw_flag->name);
996
997 if (strncmp(ptr, tw_flag->name, flag_len) == 0) {
998 bool flag_val = false;
999
1000 if (ptr_len > flag_len && (tw_flag->name)[flag_len-1] != '='
1001 && ptr[flag_len] != '=') {
1002 // Handle flags with same starting string
1003 // (e.g. backup and backupname)
1004 continue;
1005 } else if (ptr_len > flag_len && ptr[flag_len] == '=') {
1006 // Handle flags with dual format: Part 1
1007 // (e.g. backup and backup=y. backup=y handled here)
1008 ptr += flag_len + 1;
1009 TWFunc::Strip_Quotes(ptr);
1010 // Skip flags with empty argument
1011 // (e.g. backup=)
1012 if (strlen(ptr) == 0) {
1013 LOGINFO("Flag missing argument or should not include '=': %s=\n", tw_flag->name);
1014 break;
1015 }
1016 flag_val = strchr("yY1", *ptr) != NULL;
1017 } else if (ptr_len == flag_len
1018 && (tw_flag->name)[flag_len-1] == '=') {
1019 // Skip flags missing argument after =
1020 // (e.g. backupname=)
1021 LOGINFO("Flag missing argument: %s\n", tw_flag->name);
1022 break;
1023 } else if (ptr_len > flag_len
1024 && (tw_flag->name)[flag_len-1] == '=') {
1025 // Handle arguments to flags
1026 // (e.g. backupname="My Stuff")
1027 ptr += flag_len;
1028 TWFunc::Strip_Quotes(ptr);
1029 // Skip flags with empty argument
1030 // (e.g. backupname="")
1031 if (strlen(ptr) == 0) {
1032 LOGINFO("Flag missing argument: %s\n", tw_flag->name);
1033 break;
1034 }
1035 } else if (ptr_len == flag_len) {
1036 // Handle flags with dual format: Part 2
1037 // (e.g. backup and backup=y. backup handled here)
1038 flag_val = true;
Ethan Yonker96af84a2015-01-05 14:58:36 -06001039 } else {
Matt Mower2416a502016-04-12 19:54:46 -05001040 LOGINFO("Flag matched, but could not be processed: %s\n", ptr);
1041 break;
Ethan Yonker96af84a2015-01-05 14:58:36 -06001042 }
Matt Mower2416a502016-04-12 19:54:46 -05001043
1044 Apply_TW_Flag(tw_flag->flag, ptr, flag_val);
1045 break;
Ethan Yonker96af84a2015-01-05 14:58:36 -06001046 }
Matt Mower2416a502016-04-12 19:54:46 -05001047 }
1048 if (tw_flag->name == 0) {
Dees_Troy51127312012-09-08 13:08:49 -04001049 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001050 LOGERR("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -04001051 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001052 LOGINFO("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -04001053 }
Matt Mower2416a502016-04-12 19:54:46 -05001054 ptr = strtok_r(NULL, separator, &savep);
Dees_Troy51127312012-09-08 13:08:49 -04001055 }
Dees_Troy51a0e822012-09-05 15:24:24 -04001056}
1057
Dees_Troy5bf43922012-09-07 16:07:55 -04001058bool TWPartition::Is_File_System(string File_System) {
1059 if (File_System == "ext2" ||
Dees_Troy63c8df72012-09-10 14:02:05 -04001060 File_System == "ext3" ||
Dees_Troy5bf43922012-09-07 16:07:55 -04001061 File_System == "ext4" ||
1062 File_System == "vfat" ||
1063 File_System == "ntfs" ||
1064 File_System == "yaffs2" ||
bigbiff bigbiff3e146522012-11-14 14:32:59 -05001065 File_System == "exfat" ||
Dees_Troye5017042013-08-29 16:38:55 +00001066 File_System == "f2fs" ||
Erfan Abdib859e912019-02-05 18:18:46 +02001067 File_System == "squashfs" ||
Dees_Troy5bf43922012-09-07 16:07:55 -04001068 File_System == "auto")
1069 return true;
1070 else
1071 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001072}
1073
Dees_Troy5bf43922012-09-07 16:07:55 -04001074bool TWPartition::Is_Image(string File_System) {
Dees_Troy5fcd8f92012-10-16 12:22:05 -04001075 if (File_System == "emmc" || File_System == "mtd" || File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -04001076 return true;
1077 else
1078 return false;
1079}
1080
Dees_Troy51127312012-09-08 13:08:49 -04001081bool TWPartition::Make_Dir(string Path, bool Display_Error) {
Ethan Yonker93ac7a02016-11-07 22:05:58 -06001082 if (TWFunc::Get_D_Type_From_Stat(Path) != S_IFDIR)
1083 unlink(Path.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001084 if (!TWFunc::Path_Exists(Path)) {
Dees_Troy51127312012-09-08 13:08:49 -04001085 if (mkdir(Path.c_str(), 0777) == -1) {
1086 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001087 gui_msg(Msg(msg::kError, "create_folder_strerr=Can not create '{1}' folder ({2}).")(Path)(strerror(errno)));
Dees_Troy51127312012-09-08 13:08:49 -04001088 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001089 LOGINFO("Can not create '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -04001090 return false;
1091 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001092 LOGINFO("Created '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -04001093 return true;
1094 }
1095 }
1096 return true;
1097}
1098
Dees_Troy5bf43922012-09-07 16:07:55 -04001099void TWPartition::Setup_File_System(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001100 Can_Be_Mounted = true;
1101 Can_Be_Wiped = true;
1102
Dees_Troy5bf43922012-09-07 16:07:55 -04001103 // Make the mount point folder if it doesn't exist
Dees_Troy51127312012-09-08 13:08:49 -04001104 Make_Dir(Mount_Point, Display_Error);
bigbiffce8f83c2015-12-12 18:30:21 -05001105 Backup_Method = BM_FILES;
Dees_Troy5bf43922012-09-07 16:07:55 -04001106}
1107
Ethan Yonker1b190162016-12-05 15:25:19 -06001108void TWPartition::Setup_Image() {
Dees_Troy5bf43922012-09-07 16:07:55 -04001109 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
1110 Backup_Name = Display_Name;
Gary Peck82599a82012-11-21 16:23:12 -08001111 if (Current_File_System == "emmc")
bigbiffce8f83c2015-12-12 18:30:21 -05001112 Backup_Method = BM_DD;
Gary Peck82599a82012-11-21 16:23:12 -08001113 else if (Current_File_System == "mtd" || Current_File_System == "bml")
bigbiffce8f83c2015-12-12 18:30:21 -05001114 Backup_Method = BM_FLASH_UTILS;
Dees_Troy5bf43922012-09-07 16:07:55 -04001115 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001116 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 -04001117}
1118
Dees_Troye58d5262012-09-21 12:27:57 -04001119void TWPartition::Setup_AndSec(void) {
Dees_Troya13d74f2013-03-24 08:54:55 -05001120 Backup_Display_Name = "Android Secure";
Dees_Troye58d5262012-09-21 12:27:57 -04001121 Backup_Name = "and-sec";
Dees_Troya13d74f2013-03-24 08:54:55 -05001122 Can_Be_Backed_Up = true;
Dees_Troye58d5262012-09-21 12:27:57 -04001123 Has_Android_Secure = true;
1124 Symlink_Path = Mount_Point + "/.android_secure";
1125 Symlink_Mount_Point = "/and-sec";
1126 Backup_Path = Symlink_Mount_Point;
1127 Make_Dir("/and-sec", true);
1128 Recreate_AndSec_Folder();
Kjell Braden3126a112016-06-19 16:58:15 +00001129 Mount_Storage_Retry(true);
Dees_Troye58d5262012-09-21 12:27:57 -04001130}
1131
that9e0593e2014-10-08 00:01:24 +02001132void TWPartition::Setup_Data_Media() {
Ethan Yonker6277c792014-09-15 14:54:30 -05001133 LOGINFO("Setting up '%s' as data/media emulated storage.\n", Mount_Point.c_str());
Ethan Yonker66a19492015-12-10 10:19:45 -06001134 if (Storage_Name.empty() || Storage_Name == "Data")
1135 Storage_Name = "Internal Storage";
Ethan Yonker6277c792014-09-15 14:54:30 -05001136 Has_Data_Media = true;
1137 Is_Storage = true;
Ethan Yonker66a19492015-12-10 10:19:45 -06001138 Storage_Path = Mount_Point + "/media";
Ethan Yonker6277c792014-09-15 14:54:30 -05001139 Symlink_Path = Storage_Path;
Ethan Yonker66a19492015-12-10 10:19:45 -06001140 if (Mount_Point == "/data") {
1141 Is_Settings_Storage = true;
1142 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
1143 Make_Dir("/emmc", false);
1144 Symlink_Mount_Point = "/emmc";
1145 } else {
1146 Make_Dir("/sdcard", false);
1147 Symlink_Mount_Point = "/sdcard";
1148 }
1149 if (Mount(false) && TWFunc::Path_Exists(Mount_Point + "/media/0")) {
1150 Storage_Path = Mount_Point + "/media/0";
1151 Symlink_Path = Storage_Path;
1152 DataManager::SetValue(TW_INTERNAL_PATH, Mount_Point + "/media/0");
1153 UnMount(true);
1154 }
1155 DataManager::SetValue("tw_has_internal", 1);
1156 DataManager::SetValue("tw_has_data_media", 1);
Ethan Yonker3fdcda42016-11-30 12:29:37 -06001157 backup_exclusions.add_absolute_dir("/data/data/com.google.android.music/files");
Ethan Yonker6355b562017-05-01 09:42:17 -05001158 wipe_exclusions.add_absolute_dir(Mount_Point + "/misc/vold"); // adopted storage keys
Ethan Yonker79f88bd2016-12-09 14:52:12 -06001159 ExcludeAll(Mount_Point + "/.layout_version");
1160 ExcludeAll(Mount_Point + "/system/storage.xml");
Ethan Yonker6277c792014-09-15 14:54:30 -05001161 } else {
Ethan Yonker66a19492015-12-10 10:19:45 -06001162 if (Mount(true) && TWFunc::Path_Exists(Mount_Point + "/media/0")) {
1163 Storage_Path = Mount_Point + "/media/0";
1164 Symlink_Path = Storage_Path;
1165 UnMount(true);
1166 }
Ethan Yonker6277c792014-09-15 14:54:30 -05001167 }
Ethan Yonker79f88bd2016-12-09 14:52:12 -06001168 ExcludeAll(Mount_Point + "/media");
Ethan Yonker6277c792014-09-15 14:54:30 -05001169}
1170
Dees_Troy5bf43922012-09-07 16:07:55 -04001171void TWPartition::Find_Real_Block_Device(string& Block, bool Display_Error) {
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06001172 char device[PATH_MAX], realDevice[PATH_MAX];
Dees_Troy5bf43922012-09-07 16:07:55 -04001173
1174 strcpy(device, Block.c_str());
1175 memset(realDevice, 0, sizeof(realDevice));
1176 while (readlink(device, realDevice, sizeof(realDevice)) > 0)
1177 {
1178 strcpy(device, realDevice);
1179 memset(realDevice, 0, sizeof(realDevice));
1180 }
1181
1182 if (device[0] != '/') {
1183 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001184 LOGERR("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001185 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001186 LOGINFO("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001187 return;
Dees_Troy51a0e822012-09-05 15:24:24 -04001188 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -04001189 Block = device;
1190 return;
1191 }
1192}
1193
Kjell Braden3126a112016-06-19 16:58:15 +00001194bool TWPartition::Mount_Storage_Retry(bool Display_Error) {
Dees_Troy8e337f32012-10-13 22:07:49 -04001195 // On some devices, storage doesn't want to mount right away, retry and sleep
Kjell Braden3126a112016-06-19 16:58:15 +00001196 if (!Mount(Display_Error)) {
Dees_Troy8e337f32012-10-13 22:07:49 -04001197 int retry_count = 5;
1198 while (retry_count > 0 && !Mount(false)) {
1199 usleep(500000);
1200 retry_count--;
1201 }
Kjell Braden3126a112016-06-19 16:58:15 +00001202 return Mount(Display_Error);
Dees_Troy8e337f32012-10-13 22:07:49 -04001203 }
Kjell Braden3126a112016-06-19 16:58:15 +00001204 return true;
Dees_Troy8e337f32012-10-13 22:07:49 -04001205}
1206
Dees_Troy38bd7602012-09-14 13:33:53 -04001207bool TWPartition::Find_MTD_Block_Device(string MTD_Name) {
1208 FILE *fp = NULL;
1209 char line[255];
1210
1211 fp = fopen("/proc/mtd", "rt");
1212 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001213 LOGERR("Device does not support /proc/mtd\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001214 return false;
1215 }
1216
1217 while (fgets(line, sizeof(line), fp) != NULL)
1218 {
1219 char device[32], label[32];
1220 unsigned long size = 0;
Dees_Troy38bd7602012-09-14 13:33:53 -04001221 int deviceId;
1222
1223 sscanf(line, "%s %lx %*s %*c%s", device, &size, label);
1224
1225 // Skip header and blank lines
1226 if ((strcmp(device, "dev:") == 0) || (strlen(line) < 8))
1227 continue;
1228
1229 // Strip off the trailing " from the label
1230 label[strlen(label)-1] = '\0';
1231
1232 if (strcmp(label, MTD_Name.c_str()) == 0) {
1233 // We found our device
1234 // Strip off the trailing : from the device
1235 device[strlen(device)-1] = '\0';
1236 if (sscanf(device,"mtd%d", &deviceId) == 1) {
1237 sprintf(device, "/dev/block/mtdblock%d", deviceId);
1238 Primary_Block_Device = device;
Dees_Troy76543db2013-06-19 16:24:30 +00001239 fclose(fp);
1240 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -04001241 }
1242 }
1243 }
1244 fclose(fp);
1245
1246 return false;
1247}
1248
Dees_Troy51127312012-09-08 13:08:49 -04001249bool TWPartition::Get_Size_Via_statfs(bool Display_Error) {
1250 struct statfs st;
1251 string Local_Path = Mount_Point + "/.";
1252
1253 if (!Mount(Display_Error))
1254 return false;
1255
1256 if (statfs(Local_Path.c_str(), &st) != 0) {
1257 if (!Removable) {
1258 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001259 LOGERR("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -04001260 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001261 LOGINFO("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -04001262 }
1263 return false;
1264 }
1265 Size = (st.f_blocks * st.f_bsize);
1266 Used = ((st.f_blocks - st.f_bfree) * st.f_bsize);
1267 Free = (st.f_bfree * st.f_bsize);
1268 Backup_Size = Used;
1269 return true;
1270}
1271
1272bool TWPartition::Get_Size_Via_df(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001273 FILE* fp;
1274 char command[255], line[512];
1275 int include_block = 1;
1276 unsigned int min_len;
1277
1278 if (!Mount(Display_Error))
1279 return false;
1280
Dees_Troy38bd7602012-09-14 13:33:53 -04001281 min_len = Actual_Block_Device.size() + 2;
Dees_Troy51127312012-09-08 13:08:49 -04001282 sprintf(command, "df %s > /tmp/dfoutput.txt", Mount_Point.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001283 TWFunc::Exec_Cmd(command);
Dees_Troy51127312012-09-08 13:08:49 -04001284 fp = fopen("/tmp/dfoutput.txt", "rt");
1285 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001286 LOGINFO("Unable to open /tmp/dfoutput.txt.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001287 return false;
Dees_Troy51127312012-09-08 13:08:49 -04001288 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001289
1290 while (fgets(line, sizeof(line), fp) != NULL)
1291 {
1292 unsigned long blocks, used, available;
1293 char device[64];
1294 char tmpString[64];
1295
1296 if (strncmp(line, "Filesystem", 10) == 0)
1297 continue;
1298 if (strlen(line) < min_len) {
1299 include_block = 0;
1300 continue;
1301 }
1302 if (include_block) {
1303 sscanf(line, "%s %lu %lu %lu", device, &blocks, &used, &available);
1304 } else {
1305 // The device block string is so long that the df information is on the next line
1306 int space_count = 0;
Dees_Troye58d5262012-09-21 12:27:57 -04001307 sprintf(tmpString, "/dev/block/%s", Actual_Block_Device.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001308 while (tmpString[space_count] == 32)
1309 space_count++;
1310 sscanf(line + space_count, "%lu %lu %lu", &blocks, &used, &available);
1311 }
1312
1313 // Adjust block size to byte size
1314 Size = blocks * 1024ULL;
1315 Used = used * 1024ULL;
1316 Free = available * 1024ULL;
1317 Backup_Size = Used;
1318 }
1319 fclose(fp);
1320 return true;
1321}
1322
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001323unsigned long long TWPartition::IOCTL_Get_Block_Size() {
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001324 Find_Actual_Block_Device();
Ethan Yonker483e9f42016-01-11 22:21:18 -06001325
1326 return TWFunc::IOCTL_Get_Block_Size(Actual_Block_Device.c_str());
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001327}
1328
Dees_Troy5bf43922012-09-07 16:07:55 -04001329bool TWPartition::Find_Partition_Size(void) {
1330 FILE* fp;
1331 char line[512];
1332 string tmpdevice;
1333
igoriok87e3d932013-01-31 21:03:53 +02001334 fp = fopen("/proc/dumchar_info", "rt");
1335 if (fp != NULL) {
1336 while (fgets(line, sizeof(line), fp) != NULL)
1337 {
1338 char label[32], device[32];
1339 unsigned long size = 0;
1340
thatd43bf2d2014-09-21 23:13:02 +02001341 sscanf(line, "%s %lx %*x %*u %s", label, &size, device);
igoriok87e3d932013-01-31 21:03:53 +02001342
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05001343 // Skip header, annotation and blank lines
igoriok87e3d932013-01-31 21:03:53 +02001344 if ((strncmp(device, "/dev/", 5) != 0) || (strlen(line) < 8))
1345 continue;
1346
1347 tmpdevice = "/dev/";
1348 tmpdevice += label;
1349 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
1350 Size = size;
1351 fclose(fp);
1352 return true;
1353 }
1354 }
1355 }
1356
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001357 unsigned long long ioctl_size = IOCTL_Get_Block_Size();
1358 if (ioctl_size) {
1359 Size = ioctl_size;
1360 return true;
1361 }
1362
Dees_Troy5bf43922012-09-07 16:07:55 -04001363 // In this case, we'll first get the partitions we care about (with labels)
1364 fp = fopen("/proc/partitions", "rt");
1365 if (fp == NULL)
1366 return false;
1367
1368 while (fgets(line, sizeof(line), fp) != NULL)
1369 {
1370 unsigned long major, minor, blocks;
1371 char device[512];
Dees_Troy5bf43922012-09-07 16:07:55 -04001372
Dees_Troy63c8df72012-09-10 14:02:05 -04001373 if (strlen(line) < 7 || line[0] == 'm') continue;
Dees_Troy5bf43922012-09-07 16:07:55 -04001374 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
1375
1376 tmpdevice = "/dev/block/";
1377 tmpdevice += device;
Dees_Troy38bd7602012-09-14 13:33:53 -04001378 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001379 // Adjust block size to byte size
1380 Size = blocks * 1024ULL;
1381 fclose(fp);
1382 return true;
1383 }
1384 }
1385 fclose(fp);
1386 return false;
1387}
1388
Dees_Troy5bf43922012-09-07 16:07:55 -04001389bool TWPartition::Is_Mounted(void) {
1390 if (!Can_Be_Mounted)
1391 return false;
1392
1393 struct stat st1, st2;
1394 string test_path;
1395
1396 // Check to see if the mount point directory exists
1397 test_path = Mount_Point + "/.";
1398 if (stat(test_path.c_str(), &st1) != 0) return false;
1399
1400 // Check to see if the directory above the mount point exists
1401 test_path = Mount_Point + "/../.";
1402 if (stat(test_path.c_str(), &st2) != 0) return false;
1403
1404 // Compare the device IDs -- if they match then we're (probably) using tmpfs instead of an actual device
1405 int ret = (st1.st_dev != st2.st_dev) ? true : false;
1406
1407 return ret;
1408}
1409
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05001410bool TWPartition::Is_File_System_Writable(void) {
1411 if (!Is_File_System(Current_File_System) || !Is_Mounted())
1412 return false;
1413
1414 string test_path = Mount_Point + "/.";
1415 return (access(test_path.c_str(), W_OK) == 0);
1416}
1417
Dees_Troy5bf43922012-09-07 16:07:55 -04001418bool TWPartition::Mount(bool Display_Error) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001419 int exfat_mounted = 0;
Martin Dünkelmann6d843782018-12-27 23:40:28 +01001420 unsigned int flags = Mount_Flags;
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001421
Dees_Troy5bf43922012-09-07 16:07:55 -04001422 if (Is_Mounted()) {
1423 return true;
1424 } else if (!Can_Be_Mounted) {
1425 return false;
1426 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001427
1428 Find_Actual_Block_Device();
1429
1430 // Check the current file system before mounting
1431 Check_FS_Type();
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001432 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/exfat-fuse")) {
Dees_Troye34c1332013-02-06 19:13:00 +00001433 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 +00001434 LOGINFO("cmd: %s\n", cmd.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001435 string result;
1436 if (TWFunc::Exec_Cmd(cmd, result) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001437 LOGINFO("exfat-fuse failed to mount with result '%s', trying vfat\n", result.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001438 Current_File_System = "vfat";
1439 } else {
1440#ifdef TW_NO_EXFAT_FUSE
1441 UnMount(false);
1442 // We'll let the kernel handle it but using exfat-fuse to detect if the file system is actually exfat
1443 // Some kernels let us mount vfat as exfat which doesn't work out too well
1444#else
1445 exfat_mounted = 1;
1446#endif
1447 }
1448 }
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001449
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001450 if (Current_File_System == "ntfs" && (TWFunc::Path_Exists("/sbin/ntfs-3g") || TWFunc::Path_Exists("/sbin/mount.ntfs"))) {
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001451 string cmd;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001452 string Ntfsmount_Binary = "";
1453
1454 if (TWFunc::Path_Exists("/sbin/ntfs-3g"))
1455 Ntfsmount_Binary = "ntfs-3g";
1456 else if (TWFunc::Path_Exists("/sbin/mount.ntfs"))
1457 Ntfsmount_Binary = "mount.ntfs";
1458
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001459 if (Mount_Read_Only)
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001460 cmd = "/sbin/" + Ntfsmount_Binary + " -o ro " + Actual_Block_Device + " " + Mount_Point;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001461 else
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001462 cmd = "/sbin/" + Ntfsmount_Binary + " " + Actual_Block_Device + " " + Mount_Point;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001463 LOGINFO("cmd: '%s'\n", cmd.c_str());
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001464
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001465 if (TWFunc::Exec_Cmd(cmd) == 0) {
1466 return true;
1467 } else {
1468 LOGINFO("ntfs-3g failed to mount, trying regular mount method.\n");
1469 }
1470 }
1471
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001472 if (Mount_Read_Only)
1473 flags |= MS_RDONLY;
1474
Dees_Troy22042032012-12-18 21:23:08 +00001475 if (Fstab_File_System == "yaffs2") {
1476 // mount an MTD partition as a YAFFS2 filesystem.
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001477 flags = MS_NOATIME | MS_NODEV | MS_NODIRATIME;
1478 if (Mount_Read_Only)
1479 flags |= MS_RDONLY;
Dees_Troy76543db2013-06-19 16:24:30 +00001480 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags, NULL) < 0) {
1481 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags | MS_RDONLY, NULL) < 0) {
1482 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001483 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy76543db2013-06-19 16:24:30 +00001484 else
1485 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
1486 return false;
1487 } else {
1488 LOGINFO("Mounted '%s' (MTD) as RO\n", Mount_Point.c_str());
1489 return true;
1490 }
1491 } else {
1492 struct stat st;
1493 string test_path = Mount_Point;
1494 if (stat(test_path.c_str(), &st) < 0) {
1495 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001496 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy76543db2013-06-19 16:24:30 +00001497 else
1498 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
1499 return false;
1500 }
1501 mode_t new_mode = st.st_mode | S_IXUSR | S_IXGRP | S_IXOTH;
1502 if (new_mode != st.st_mode) {
1503 LOGINFO("Fixing execute permissions for %s\n", Mount_Point.c_str());
1504 if (chmod(Mount_Point.c_str(), new_mode) < 0) {
1505 if (Display_Error)
1506 LOGERR("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
1507 else
1508 LOGINFO("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
1509 return false;
1510 }
1511 }
Dees_Troy22042032012-12-18 21:23:08 +00001512 return true;
Dees_Troy76543db2013-06-19 16:24:30 +00001513 }
thatc7572eb2015-03-31 18:55:30 +02001514 }
1515
1516 string mount_fs = Current_File_System;
1517 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sys/module/texfat"))
1518 mount_fs = "texfat";
1519
1520 if (!exfat_mounted &&
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001521 mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), mount_fs.c_str(), flags, Mount_Options.c_str()) != 0 &&
1522 mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), mount_fs.c_str(), flags, NULL) != 0) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001523#ifdef TW_NO_EXFAT_FUSE
1524 if (Current_File_System == "exfat") {
Dees_Troy2673cec2013-04-02 20:22:16 +00001525 LOGINFO("Mounting exfat failed, trying vfat...\n");
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001526 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), "vfat", 0, NULL) != 0) {
Dees_Troy85f44ed2013-01-09 18:42:36 +00001527 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001528 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy85f44ed2013-01-09 18:42:36 +00001529 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001530 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001531 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 +00001532 return false;
Dees_Troy85f44ed2013-01-09 18:42:36 +00001533 }
Dees_Troyb05ddee2013-01-28 20:24:50 +00001534 } else {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001535#endif
bigbiff bigbiff26774a02014-03-29 18:22:00 -04001536 if (!Removable && Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001537 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001538 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001539 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
1540 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 +00001541 return false;
1542#ifdef TW_NO_EXFAT_FUSE
Dees_Troy85f44ed2013-01-09 18:42:36 +00001543 }
1544#endif
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001545 }
Ethan Yonker253368a2014-11-25 15:00:52 -06001546
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001547 if (Removable)
1548 Update_Size(Display_Error);
1549
xiaolu9416f4f2015-06-04 08:22:23 +08001550 if (!Symlink_Mount_Point.empty() && TWFunc::Path_Exists(Symlink_Path)) {
codelover2a3d4ce2015-03-14 20:26:49 +08001551 string Command = "mount -o bind '" + Symlink_Path + "' '" + Symlink_Mount_Point + "'";
Vojtech Bocek05534202013-09-11 08:11:56 +02001552 TWFunc::Exec_Cmd(Command);
Dees_Troy5bf43922012-09-07 16:07:55 -04001553 }
Chaosmasterda974802020-01-26 21:09:28 +01001554
1555 if (Mount_Point == "/system_root") {
1556 unlink("/system");
1557 mkdir("/system", 0755);
1558 mount("/system_root/system", "/system", "auto", MS_BIND, NULL);
1559 }
1560
Dees_Troy5bf43922012-09-07 16:07:55 -04001561 return true;
1562}
1563
1564bool TWPartition::UnMount(bool Display_Error) {
Chaosmasterda974802020-01-26 21:09:28 +01001565 if (Mount_Point == "/system_root") {
1566 if (umount("/system") == -1)
1567 umount2("/system", MNT_DETACH);
1568 rmdir("/system");
1569 symlink("/system_root/system", "/system");
1570 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001571 if (Is_Mounted()) {
1572 int never_unmount_system;
1573
1574 DataManager::GetValue(TW_DONT_UNMOUNT_SYSTEM, never_unmount_system);
Captain Throwback9d6feb52018-07-27 10:05:24 -04001575 if (never_unmount_system == 1 && Mount_Point == PartitionManager.Get_Android_Root_Path())
Dees_Troy5bf43922012-09-07 16:07:55 -04001576 return true; // Never unmount system if you're not supposed to unmount it
1577
Matt Mowera8e6d832017-02-14 20:20:59 -06001578 if (Is_Storage && MTP_Storage_ID > 0)
Ethan Yonker726a0202014-12-16 20:01:38 -06001579 PartitionManager.Remove_MTP_Storage(MTP_Storage_ID);
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001580
Dees_Troy38bd7602012-09-14 13:33:53 -04001581 if (!Symlink_Mount_Point.empty())
1582 umount(Symlink_Mount_Point.c_str());
1583
Dees_Troyb05ddee2013-01-28 20:24:50 +00001584 umount(Mount_Point.c_str());
1585 if (Is_Mounted()) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001586 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001587 gui_msg(Msg(msg::kError, "fail_unmount=Failed to unmount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy5bf43922012-09-07 16:07:55 -04001588 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001589 LOGINFO("Unable to unmount '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001590 return false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001591 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -04001592 return true;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001593 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001594 } else {
1595 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001596 }
1597}
1598
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05001599bool TWPartition::ReMount(bool Display_Error) {
1600 if (UnMount(Display_Error))
1601 return Mount(Display_Error);
1602 return false;
1603}
1604
1605bool TWPartition::ReMount_RW(bool Display_Error) {
1606 // No need to remount if already mounted rw
1607 if (Is_File_System_Writable())
1608 return true;
1609
1610 bool ro = Mount_Read_Only;
1611 int flags = Mount_Flags;
1612
1613 Mount_Read_Only = false;
1614 Mount_Flags &= ~MS_RDONLY;
1615
1616 bool ret = ReMount(Display_Error);
1617
1618 Mount_Read_Only = ro;
1619 Mount_Flags = flags;
1620
1621 return ret;
1622}
1623
Gary Peck43acadf2012-11-21 21:19:01 -08001624bool TWPartition::Wipe(string New_File_System) {
Ethan Yonker726a0202014-12-16 20:01:38 -06001625 bool wiped = false, update_crypt = false, recreate_media = true;
Dees_Troy16c2b312013-01-15 16:51:18 +00001626 int check;
1627 string Layout_Filename = Mount_Point + "/.layout_version";
1628
Dees_Troy38bd7602012-09-14 13:33:53 -04001629 if (!Can_Be_Wiped) {
Matt Mower3c366972015-12-25 19:28:31 -06001630 gui_msg(Msg(msg::kError, "cannot_wipe=Partition {1} cannot be wiped.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04001631 return false;
1632 }
1633
Dees_Troyc51f1f92012-09-20 15:32:13 -04001634 if (Mount_Point == "/cache")
Dees_Troy2673cec2013-04-02 20:22:16 +00001635 Log_Offset = 0;
Dees_Troyc51f1f92012-09-20 15:32:13 -04001636
Dees_Troy16c2b312013-01-15 16:51:18 +00001637 if (Retain_Layout_Version && Mount(false) && TWFunc::Path_Exists(Layout_Filename))
1638 TWFunc::copy_file(Layout_Filename, "/.layout_version", 0600);
1639 else
1640 unlink("/.layout_version");
Dees_Troy38bd7602012-09-14 13:33:53 -04001641
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001642 if (Has_Data_Media && Current_File_System == New_File_System) {
Dees_Troy16c2b312013-01-15 16:51:18 +00001643 wiped = Wipe_Data_Without_Wiping_Media();
Ethan Yonker5eac2222014-06-11 12:22:55 -05001644 recreate_media = false;
Dees_Troy16c2b312013-01-15 16:51:18 +00001645 } else {
Dees_Troy16c2b312013-01-15 16:51:18 +00001646 DataManager::GetValue(TW_RM_RF_VAR, check);
1647
Hashcodedabfd492013-08-29 22:45:30 -07001648 if (check || Use_Rm_Rf)
Dees_Troy16c2b312013-01-15 16:51:18 +00001649 wiped = Wipe_RMRF();
1650 else if (New_File_System == "ext4")
1651 wiped = Wipe_EXT4();
1652 else if (New_File_System == "ext2" || New_File_System == "ext3")
dianlujitao4879b372018-12-03 18:45:47 +08001653 wiped = Wipe_EXTFS(New_File_System);
Dees_Troy16c2b312013-01-15 16:51:18 +00001654 else if (New_File_System == "vfat")
1655 wiped = Wipe_FAT();
1656 else if (New_File_System == "exfat")
1657 wiped = Wipe_EXFAT();
1658 else if (New_File_System == "yaffs2")
1659 wiped = Wipe_MTD();
Dees_Troye5017042013-08-29 16:38:55 +00001660 else if (New_File_System == "f2fs")
1661 wiped = Wipe_F2FS();
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001662 else if (New_File_System == "ntfs")
1663 wiped = Wipe_NTFS();
Dees_Troy16c2b312013-01-15 16:51:18 +00001664 else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001665 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 +00001666 unlink("/.layout_version");
1667 return false;
1668 }
1669 update_crypt = wiped;
Gary Pecke8bc5d72012-12-21 06:45:25 -08001670 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001671
Gary Pecke8bc5d72012-12-21 06:45:25 -08001672 if (wiped) {
Dees_Troy1c1ac442013-01-17 21:42:14 +00001673 if (Mount_Point == "/cache")
1674 DataManager::Output_Version();
1675
Dees_Troy16c2b312013-01-15 16:51:18 +00001676 if (TWFunc::Path_Exists("/.layout_version") && Mount(false))
1677 TWFunc::copy_file("/.layout_version", Layout_Filename, 0600);
1678
1679 if (update_crypt) {
1680 Setup_File_System(false);
1681 if (Is_Encrypted && !Is_Decrypted) {
1682 // just wiped an encrypted partition back to its unencrypted state
1683 Is_Encrypted = false;
1684 Is_Decrypted = false;
1685 Decrypted_Block_Device = "";
1686 if (Mount_Point == "/data") {
1687 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1688 DataManager::SetValue(TW_IS_DECRYPTED, 0);
1689 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001690 }
1691 }
Ethan Yonker5eac2222014-06-11 12:22:55 -05001692
Ethan Yonker66a19492015-12-10 10:19:45 -06001693 if (Has_Data_Media && recreate_media) {
Ethan Yonker5eac2222014-06-11 12:22:55 -05001694 Recreate_Media_Folder();
1695 }
Matt Mower209c9632016-01-20 12:08:35 -06001696 if (Is_Storage && Mount(false))
1697 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Gary Pecke8bc5d72012-12-21 06:45:25 -08001698 }
Matt Mower209c9632016-01-20 12:08:35 -06001699
Gary Pecke8bc5d72012-12-21 06:45:25 -08001700 return wiped;
Dees_Troy51a0e822012-09-05 15:24:24 -04001701}
1702
Gary Peck43acadf2012-11-21 21:19:01 -08001703bool TWPartition::Wipe() {
Gary Peck82599a82012-11-21 16:23:12 -08001704 if (Is_File_System(Current_File_System))
1705 return Wipe(Current_File_System);
1706 else
1707 return Wipe(Fstab_File_System);
Gary Peck43acadf2012-11-21 21:19:01 -08001708}
1709
Dees_Troye58d5262012-09-21 12:27:57 -04001710bool TWPartition::Wipe_AndSec(void) {
1711 if (!Has_Android_Secure)
1712 return false;
1713
Dees_Troye58d5262012-09-21 12:27:57 -04001714 if (!Mount(true))
1715 return false;
1716
Ethan Yonker74db1572015-10-28 12:44:49 -05001717 gui_msg(Msg("wiping=Wiping {1}")(Backup_Display_Name));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001718 TWFunc::removeDir(Mount_Point + "/.android_secure/", true);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001719 return true;
Dees_Troye58d5262012-09-21 12:27:57 -04001720}
1721
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001722bool TWPartition::Can_Repair() {
Ethan Yonkera2719152015-05-28 09:44:41 -05001723 if (Mount_Read_Only)
1724 return false;
Matt Mower18794c82015-11-11 16:22:45 -06001725 if (Current_File_System == "vfat" && TWFunc::Path_Exists("/sbin/fsck.fat"))
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001726 return true;
1727 else if ((Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") && TWFunc::Path_Exists("/sbin/e2fsck"))
1728 return true;
1729 else if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/fsck.exfat"))
1730 return true;
1731 else if (Current_File_System == "f2fs" && TWFunc::Path_Exists("/sbin/fsck.f2fs"))
1732 return true;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001733 else if (Current_File_System == "ntfs" && (TWFunc::Path_Exists("/sbin/ntfsfix") || TWFunc::Path_Exists("/sbin/fsck.ntfs")))
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001734 return true;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001735 return false;
1736}
1737
1738bool TWPartition::Repair() {
1739 string command;
1740
1741 if (Current_File_System == "vfat") {
Matt Mower18794c82015-11-11 16:22:45 -06001742 if (!TWFunc::Path_Exists("/sbin/fsck.fat")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001743 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.fat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001744 return false;
1745 }
1746 if (!UnMount(true))
1747 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001748 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.fat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001749 Find_Actual_Block_Device();
Matt Mower18794c82015-11-11 16:22:45 -06001750 command = "/sbin/fsck.fat -y " + Actual_Block_Device;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001751 LOGINFO("Repair command: %s\n", command.c_str());
1752 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001753 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001754 return true;
1755 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001756 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001757 return false;
1758 }
1759 }
1760 if (Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") {
1761 if (!TWFunc::Path_Exists("/sbin/e2fsck")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001762 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("e2fsck"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001763 return false;
1764 }
1765 if (!UnMount(true))
1766 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001767 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("e2fsck"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001768 Find_Actual_Block_Device();
Ethan Yonkera2719152015-05-28 09:44:41 -05001769 command = "/sbin/e2fsck -fp " + Actual_Block_Device;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001770 LOGINFO("Repair command: %s\n", command.c_str());
1771 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001772 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001773 return true;
1774 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001775 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001776 return false;
1777 }
1778 }
1779 if (Current_File_System == "exfat") {
1780 if (!TWFunc::Path_Exists("/sbin/fsck.exfat")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001781 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.exfat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001782 return false;
1783 }
1784 if (!UnMount(true))
1785 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001786 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.exfat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001787 Find_Actual_Block_Device();
1788 command = "/sbin/fsck.exfat " + Actual_Block_Device;
1789 LOGINFO("Repair command: %s\n", command.c_str());
1790 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001791 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001792 return true;
1793 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001794 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001795 return false;
1796 }
1797 }
1798 if (Current_File_System == "f2fs") {
1799 if (!TWFunc::Path_Exists("/sbin/fsck.f2fs")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001800 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.f2fs"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001801 return false;
1802 }
1803 if (!UnMount(true))
1804 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001805 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.f2fs"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001806 Find_Actual_Block_Device();
1807 command = "/sbin/fsck.f2fs " + Actual_Block_Device;
1808 LOGINFO("Repair command: %s\n", command.c_str());
1809 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001810 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001811 return true;
1812 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001813 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001814 return false;
1815 }
1816 }
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001817 if (Current_File_System == "ntfs") {
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001818 string Ntfsfix_Binary;
1819 if (TWFunc::Path_Exists("/sbin/ntfsfix"))
1820 Ntfsfix_Binary = "ntfsfix";
1821 else if (TWFunc::Path_Exists("/sbin/fsck.ntfs"))
1822 Ntfsfix_Binary = "fsck.ntfs";
1823 else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001824 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("ntfsfix"));
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001825 return false;
1826 }
1827 if (!UnMount(true))
1828 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001829 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)(Ntfsfix_Binary));
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001830 Find_Actual_Block_Device();
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001831 command = "/sbin/" + Ntfsfix_Binary + " " + Actual_Block_Device;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001832 LOGINFO("Repair command: %s\n", command.c_str());
1833 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001834 gui_msg("done=Done.");
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001835 return true;
1836 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001837 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001838 return false;
1839 }
1840 }
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001841 return false;
1842}
1843
Ethan Yonkera2719152015-05-28 09:44:41 -05001844bool TWPartition::Can_Resize() {
1845 if (Mount_Read_Only)
1846 return false;
1847 if ((Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") && TWFunc::Path_Exists("/sbin/resize2fs"))
1848 return true;
1849 return false;
1850}
1851
1852bool TWPartition::Resize() {
1853 string command;
1854
1855 if (Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") {
1856 if (!Can_Repair()) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001857 LOGINFO("Cannot resize %s because %s cannot be repaired before resizing.\n", Display_Name.c_str(), Display_Name.c_str());
1858 gui_msg(Msg(msg::kError, "cannot_resize=Cannot resize {1}.")(Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001859 return false;
1860 }
1861 if (!TWFunc::Path_Exists("/sbin/resize2fs")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001862 LOGINFO("resize2fs does not exist! Cannot resize!\n");
1863 gui_msg(Msg(msg::kError, "cannot_resize=Cannot resize {1}.")(Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001864 return false;
1865 }
1866 // Repair will unmount so no need to do it twice
Ethan Yonker74db1572015-10-28 12:44:49 -05001867 gui_msg(Msg("repair_resize=Repairing {1} before resizing.")( Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001868 if (!Repair())
1869 return false;
Ethan Yonker74db1572015-10-28 12:44:49 -05001870 gui_msg(Msg("resizing=Resizing {1} using {2}...")(Display_Name)("resize2fs"));
Ethan Yonkera2719152015-05-28 09:44:41 -05001871 Find_Actual_Block_Device();
1872 command = "/sbin/resize2fs " + Actual_Block_Device;
1873 if (Length != 0) {
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001874 unsigned long long Actual_Size = IOCTL_Get_Block_Size();
1875 if (Actual_Size == 0)
1876 return false;
Ethan Yonkera2719152015-05-28 09:44:41 -05001877
Ethan Yonkera2719152015-05-28 09:44:41 -05001878 unsigned long long Block_Count;
1879 if (Length < 0) {
1880 // Reduce overall size by this length
1881 Block_Count = (Actual_Size / 1024LLU) - ((unsigned long long)(Length * -1) / 1024LLU);
1882 } else {
1883 // This is the size, not a size reduction
1884 Block_Count = ((unsigned long long)(Length) / 1024LLU);
1885 }
1886 char temp[256];
1887 sprintf(temp, "%llu", Block_Count);
1888 command += " ";
1889 command += temp;
1890 command += "K";
1891 }
1892 LOGINFO("Resize command: %s\n", command.c_str());
1893 if (TWFunc::Exec_Cmd(command) == 0) {
1894 Update_Size(true);
Ethan Yonker74db1572015-10-28 12:44:49 -05001895 gui_msg("done=Done.");
Ethan Yonkera2719152015-05-28 09:44:41 -05001896 return true;
1897 } else {
1898 Update_Size(true);
Ethan Yonker74db1572015-10-28 12:44:49 -05001899 gui_msg(Msg(msg::kError, "unable_resize=Unable to resize {1}.")(Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001900 return false;
1901 }
1902 }
1903 return false;
1904}
1905
bigbiffce8f83c2015-12-12 18:30:21 -05001906bool TWPartition::Backup(PartitionSettings *part_settings, pid_t *tar_fork_pid) {
1907 if (Backup_Method == BM_FILES)
1908 return Backup_Tar(part_settings, tar_fork_pid);
1909 else if (Backup_Method == BM_DD)
1910 return Backup_Image(part_settings);
1911 else if (Backup_Method == BM_FLASH_UTILS)
1912 return Backup_Dump_Image(part_settings);
Dees_Troy2673cec2013-04-02 20:22:16 +00001913 LOGERR("Unknown backup method for '%s'\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001914 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001915}
1916
bigbiffce8f83c2015-12-12 18:30:21 -05001917bool TWPartition::Restore(PartitionSettings *part_settings) {
Matt Mower3c366972015-12-25 19:28:31 -06001918 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, gui_parse_text("{@restoring_hdr}"));
Ethan Yonkere080c1f2016-09-19 13:50:25 -05001919 LOGINFO("Restore filename is: %s/%s\n", part_settings->Backup_Folder.c_str(), Backup_FileName.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001920
bigbiffce8f83c2015-12-12 18:30:21 -05001921 string Restore_File_System = Get_Restore_File_System(part_settings);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001922
1923 if (Is_File_System(Restore_File_System))
bigbiffce8f83c2015-12-12 18:30:21 -05001924 return Restore_Tar(part_settings);
1925 else if (Is_Image(Restore_File_System))
1926 return Restore_Image(part_settings);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001927
1928 LOGERR("Unknown restore method for '%s'\n", Mount_Point.c_str());
1929 return false;
1930}
1931
bigbiffce8f83c2015-12-12 18:30:21 -05001932string TWPartition::Get_Restore_File_System(PartitionSettings *part_settings) {
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001933 size_t first_period, second_period;
1934 string Restore_File_System;
1935
Gary Peck43acadf2012-11-21 21:19:01 -08001936 // Parse backup filename to extract the file system before wiping
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05001937 first_period = Backup_FileName.find(".");
Gary Peck43acadf2012-11-21 21:19:01 -08001938 if (first_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001939 LOGERR("Unable to find file system (first period).\n");
thatd43bf2d2014-09-21 23:13:02 +02001940 return string();
Gary Peck43acadf2012-11-21 21:19:01 -08001941 }
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05001942 Restore_File_System = Backup_FileName.substr(first_period + 1, Backup_FileName.size() - first_period - 1);
Gary Peck43acadf2012-11-21 21:19:01 -08001943 second_period = Restore_File_System.find(".");
1944 if (second_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001945 LOGERR("Unable to find file system (second period).\n");
thatd43bf2d2014-09-21 23:13:02 +02001946 return string();
Gary Peck43acadf2012-11-21 21:19:01 -08001947 }
1948 Restore_File_System.resize(second_period);
Matt Mowera0cd91d2016-12-30 18:21:42 -06001949 LOGINFO("Restore file system is: '%s'.\n", Restore_File_System.c_str());
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001950 return Restore_File_System;
Dees_Troy51a0e822012-09-05 15:24:24 -04001951}
1952
1953string TWPartition::Backup_Method_By_Name() {
bigbiffce8f83c2015-12-12 18:30:21 -05001954 if (Backup_Method == BM_NONE)
Dees_Troy38bd7602012-09-14 13:33:53 -04001955 return "none";
bigbiffce8f83c2015-12-12 18:30:21 -05001956 else if (Backup_Method == BM_FILES)
Dees_Troy38bd7602012-09-14 13:33:53 -04001957 return "files";
bigbiffce8f83c2015-12-12 18:30:21 -05001958 else if (Backup_Method == BM_DD)
Dees_Troy38bd7602012-09-14 13:33:53 -04001959 return "dd";
bigbiffce8f83c2015-12-12 18:30:21 -05001960 else if (Backup_Method == BM_FLASH_UTILS)
Dees_Troy38bd7602012-09-14 13:33:53 -04001961 return "flash_utils";
1962 else
1963 return "undefined";
1964 return "ERROR!";
Dees_Troy51a0e822012-09-05 15:24:24 -04001965}
1966
1967bool TWPartition::Decrypt(string Password) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001968 LOGINFO("STUB TWPartition::Decrypt, password: '%s'\n", Password.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001969 // Is this needed?
Dees_Troy51a0e822012-09-05 15:24:24 -04001970 return 1;
1971}
1972
1973bool TWPartition::Wipe_Encryption() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001974 bool Save_Data_Media = Has_Data_Media;
Ethan Yonker93382822018-11-01 15:25:31 -05001975 bool ret = false;
1976 BasePartition* base_partition = make_partition();
1977
1978 if (!base_partition->PreWipeEncryption())
1979 goto exit;
Dees_Troy38bd7602012-09-14 13:33:53 -04001980
dianlujitao4879b372018-12-03 18:45:47 +08001981 Find_Actual_Block_Device();
1982 if (!Is_Present) {
1983 LOGINFO("Block device not present, cannot format %s.\n", Display_Name.c_str());
1984 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
1985 return false;
1986 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001987 if (!UnMount(true))
Ethan Yonker93382822018-11-01 15:25:31 -05001988 goto exit;
Dees_Troy38bd7602012-09-14 13:33:53 -04001989
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001990#ifdef TW_INCLUDE_CRYPTO
Ethan Yonker1b190162016-12-05 15:25:19 -06001991 if (Is_Decrypted && !Decrypted_Block_Device.empty()) {
Matt Mower2b18a532015-02-20 16:58:05 -06001992 if (delete_crypto_blk_dev((char*)("userdata")) != 0) {
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001993 LOGERR("Error deleting crypto block device, continuing anyway.\n");
1994 }
1995 }
1996#endif
dianlujitao4879b372018-12-03 18:45:47 +08001997 Has_Data_Media = false;
Ethan Yonker93382822018-11-01 15:25:31 -05001998 Decrypted_Block_Device = "";
Dees_Troy74fb2e92013-04-15 14:35:47 +00001999 Is_Decrypted = false;
2000 Is_Encrypted = false;
Gary Pecke8bc5d72012-12-21 06:45:25 -08002001 if (Wipe(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002002 Has_Data_Media = Save_Data_Media;
2003 if (Has_Data_Media && !Symlink_Mount_Point.empty()) {
2004 Recreate_Media_Folder();
Ethan Yonker726a0202014-12-16 20:01:38 -06002005 if (Mount(false))
2006 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04002007 }
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06002008 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
Ethan Yonker83e82572014-04-04 10:59:28 -05002009#ifndef TW_OEM_BUILD
Ethan Yonker74db1572015-10-28 12:44:49 -05002010 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 -05002011#endif
Ethan Yonker93382822018-11-01 15:25:31 -05002012 ret = true;
2013 if (!Key_Directory.empty())
2014 ret = PartitionManager.Wipe_By_Path(Key_Directory);
2015 if (ret)
2016 ret = base_partition->PostWipeEncryption();
2017 goto exit;
Dees_Troy38bd7602012-09-14 13:33:53 -04002018 } else {
2019 Has_Data_Media = Save_Data_Media;
Ethan Yonker74db1572015-10-28 12:44:49 -05002020 gui_err("format_data_err=Unable to format to remove encryption.");
Ethan Yonker726a0202014-12-16 20:01:38 -06002021 if (Has_Data_Media && Mount(false))
2022 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Ethan Yonker93382822018-11-01 15:25:31 -05002023 goto exit;
Dees_Troy38bd7602012-09-14 13:33:53 -04002024 }
Ethan Yonker93382822018-11-01 15:25:31 -05002025exit:
2026 delete base_partition;
2027 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04002028}
2029
2030void TWPartition::Check_FS_Type() {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05002031 const char* type;
2032 blkid_probe pr;
Dees_Troy5bf43922012-09-07 16:07:55 -04002033
Dees_Troy68cab492012-12-12 19:29:35 +00002034 if (Fstab_File_System == "yaffs2" || Fstab_File_System == "mtd" || Fstab_File_System == "bml" || Ignore_Blkid)
2035 return; // Running blkid on some mtd devices causes a massive crash or needs to be skipped
Dees_Troy5bf43922012-09-07 16:07:55 -04002036
Dees_Troy38bd7602012-09-14 13:33:53 -04002037 Find_Actual_Block_Device();
Dees_Troy8170a922012-09-18 15:40:25 -04002038 if (!Is_Present)
2039 return;
Dees_Troy51127312012-09-08 13:08:49 -04002040
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05002041 pr = blkid_new_probe_from_filename(Actual_Block_Device.c_str());
2042 if (blkid_do_fullprobe(pr)) {
2043 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00002044 LOGINFO("Can't probe device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05002045 return;
Dees_Troy5bf43922012-09-07 16:07:55 -04002046 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02002047
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05002048 if (blkid_probe_lookup_value(pr, "TYPE", &type, NULL) < 0) {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05002049 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00002050 LOGINFO("can't find filesystem on device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05002051 return;
2052 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02002053
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05002054 Current_File_System = type;
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02002055 blkid_free_probe(pr);
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06002056 if (fs_flags.size() > 1) {
2057 std::vector<partition_fs_flags_struct>::iterator iter;
2058 std::vector<partition_fs_flags_struct>::iterator found = fs_flags.begin();
2059
2060 for (iter = fs_flags.begin(); iter != fs_flags.end(); iter++) {
2061 if (iter->File_System == Current_File_System) {
2062 found = iter;
2063 break;
2064 }
2065 }
2066 // If we don't find a match, we default the flags to the first set of flags that we received from the fstab
2067 if (Mount_Flags != found->Mount_Flags || Mount_Options != found->Mount_Options) {
2068 Mount_Flags = found->Mount_Flags;
2069 Mount_Options = found->Mount_Options;
2070 LOGINFO("Mount_Flags: %i, Mount_Options: %s\n", Mount_Flags, Mount_Options.c_str());
2071 }
2072 }
Dees_Troy51a0e822012-09-05 15:24:24 -04002073}
2074
dianlujitao4879b372018-12-03 18:45:47 +08002075bool TWPartition::Wipe_EXTFS(string File_System) {
2076#if PLATFORM_SDK_VERSION < 28
2077 if (!TWFunc::Path_Exists("/sbin/mke2fs"))
2078#else
2079 if (!TWFunc::Path_Exists("/sbin/mke2fs") || !TWFunc::Path_Exists("/sbin/e2fsdroid"))
2080#endif
Dees_Troy38bd7602012-09-14 13:33:53 -04002081 return Wipe_RMRF();
2082
dianlujitao4879b372018-12-03 18:45:47 +08002083 int ret;
2084 bool NeedPreserveFooter = true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002085
Ethan Yonker25f20c12014-10-14 09:04:43 -05002086 Find_Actual_Block_Device();
2087 if (!Is_Present) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002088 LOGINFO("Block device not present, cannot wipe %s.\n", Display_Name.c_str());
2089 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Ethan Yonker25f20c12014-10-14 09:04:43 -05002090 return false;
2091 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002092 if (!UnMount(true))
2093 return false;
2094
dianlujitao4879b372018-12-03 18:45:47 +08002095 /**
2096 * On decrypted devices, IOCTL_Get_Block_Size calculates size on device mapper,
2097 * so there's no need to preserve footer.
2098 */
2099 if ((Is_Decrypted && !Decrypted_Block_Device.empty()) ||
2100 Crypto_Key_Location != "footer") {
2101 NeedPreserveFooter = false;
2102 }
2103
2104 unsigned long long dev_sz = TWFunc::IOCTL_Get_Block_Size(Actual_Block_Device.c_str());
2105 if (!dev_sz)
2106 return false;
2107
2108 if (NeedPreserveFooter)
2109 Length < 0 ? dev_sz += Length : dev_sz -= CRYPT_FOOTER_OFFSET;
2110
Dees Troy9a4d7402019-03-21 14:17:28 -04002111 char dout[16];
2112 sprintf(dout, "%llu", dev_sz / 4096);
2113
2114 //string size_str =to_string(dev_sz / 4096);
2115 string size_str = dout;
dianlujitao4879b372018-12-03 18:45:47 +08002116 string Command;
2117
2118 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mke2fs"));
2119
2120 // Execute mke2fs to create empty ext4 filesystem
2121 Command = "mke2fs -t " + File_System + " -b 4096 " + Actual_Block_Device + " " + size_str;
2122 LOGINFO("mke2fs command: %s\n", Command.c_str());
2123 ret = TWFunc::Exec_Cmd(Command);
2124 if (ret) {
2125 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
2126 return false;
2127 }
2128
2129 if (TWFunc::Path_Exists("/sbin/e2fsdroid")) {
dianlujitao3cf05a52019-05-15 15:33:27 +08002130 const string& File_Contexts_Entry = (Mount_Point == "/system_root" ? "/" : Mount_Point);
2131 char *secontext = NULL;
2132 if (!selinux_handle || selabel_lookup(selinux_handle, &secontext, File_Contexts_Entry.c_str(), S_IFDIR) < 0) {
2133 LOGINFO("Cannot lookup security context for '%s'\n", Mount_Point.c_str());
2134 } else {
2135 // Execute e2fsdroid to initialize selinux context
2136 Command = "e2fsdroid -e -S /file_contexts -a " + File_Contexts_Entry + " " + Actual_Block_Device;
2137 LOGINFO("e2fsdroid command: %s\n", Command.c_str());
2138 ret = TWFunc::Exec_Cmd(Command);
2139 if (ret) {
2140 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
2141 return false;
2142 }
dianlujitao4879b372018-12-03 18:45:47 +08002143 }
2144 } else {
2145 LOGINFO("e2fsdroid not present\n");
2146 }
2147
2148 if (NeedPreserveFooter)
2149 Wipe_Crypto_Key();
2150 Current_File_System = File_System;
2151 Recreate_AndSec_Folder();
2152 gui_msg("done=Done.");
2153 return true;
2154}
2155
2156bool TWPartition::Wipe_EXT4() {
2157#ifdef USE_EXT4
Ethan Yonkerf27497f2014-02-09 11:48:33 -06002158 int ret;
dianlujitao4879b372018-12-03 18:45:47 +08002159 bool NeedPreserveFooter = true;
2160
2161 Find_Actual_Block_Device();
2162 if (!Is_Present) {
2163 LOGINFO("Block device not present, cannot wipe %s.\n", Display_Name.c_str());
2164 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
2165 return false;
2166 }
2167 if (!UnMount(true))
2168 return false;
2169
2170 /**
2171 * On decrypted devices, IOCTL_Get_Block_Size calculates size on device mapper,
2172 * so there's no need to preserve footer.
2173 */
2174 if ((Is_Decrypted && !Decrypted_Block_Device.empty()) ||
2175 Crypto_Key_Location != "footer") {
2176 NeedPreserveFooter = false;
2177 }
2178
2179 unsigned long long dev_sz = TWFunc::IOCTL_Get_Block_Size(Actual_Block_Device.c_str());
2180 if (!dev_sz)
2181 return false;
2182
2183 if (NeedPreserveFooter)
2184 Length < 0 ? dev_sz += Length : dev_sz -= CRYPT_FOOTER_OFFSET;
2185
Ethan Yonkerf27497f2014-02-09 11:48:33 -06002186 char *secontext = NULL;
2187
Greg Wallace4b44fef2015-12-29 15:33:24 -05002188 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("make_ext4fs"));
Ethan Yonkerf27497f2014-02-09 11:48:33 -06002189
Dees Troy99c8dbf2014-03-10 16:53:58 +00002190 if (!selinux_handle || selabel_lookup(selinux_handle, &secontext, Mount_Point.c_str(), S_IFDIR) < 0) {
Ethan Yonkerf27497f2014-02-09 11:48:33 -06002191 LOGINFO("Cannot lookup security context for '%s'\n", Mount_Point.c_str());
dianlujitao4879b372018-12-03 18:45:47 +08002192 ret = make_ext4fs(Actual_Block_Device.c_str(), dev_sz, Mount_Point.c_str(), NULL);
Ethan Yonkerf27497f2014-02-09 11:48:33 -06002193 } else {
dianlujitao4879b372018-12-03 18:45:47 +08002194 ret = make_ext4fs(Actual_Block_Device.c_str(), dev_sz, Mount_Point.c_str(), selinux_handle);
Ethan Yonkerf27497f2014-02-09 11:48:33 -06002195 }
2196 if (ret != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002197 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troya95f55c2013-08-17 13:14:43 +00002198 return false;
2199 } else {
dianlujitao4879b372018-12-03 18:45:47 +08002200 if (NeedPreserveFooter)
2201 Wipe_Crypto_Key();
bigbiff bigbiffc49d7062013-10-11 20:28:00 -04002202 string sedir = Mount_Point + "/lost+found";
2203 PartitionManager.Mount_By_Path(sedir.c_str(), true);
2204 rmdir(sedir.c_str());
2205 mkdir(sedir.c_str(), S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP);
Dees_Troya95f55c2013-08-17 13:14:43 +00002206 return true;
2207 }
2208#else
dianlujitao4879b372018-12-03 18:45:47 +08002209 return Wipe_EXTFS("ext4");
Dees_Troya95f55c2013-08-17 13:14:43 +00002210#endif
Dees_Troy51a0e822012-09-05 15:24:24 -04002211}
2212
2213bool TWPartition::Wipe_FAT() {
Vojtech Bocek05534202013-09-11 08:11:56 +02002214 string command;
Dees_Troy38bd7602012-09-14 13:33:53 -04002215
Matt Mower18794c82015-11-11 16:22:45 -06002216 if (TWFunc::Path_Exists("/sbin/mkfs.fat")) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002217 if (!UnMount(true))
2218 return false;
2219
Greg Wallace4b44fef2015-12-29 15:33:24 -05002220 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mkfs.fat"));
Dees_Troy38bd7602012-09-14 13:33:53 -04002221 Find_Actual_Block_Device();
Matt Mower18794c82015-11-11 16:22:45 -06002222 command = "mkfs.fat " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02002223 if (TWFunc::Exec_Cmd(command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08002224 Current_File_System = "vfat";
Dees_Troye58d5262012-09-21 12:27:57 -04002225 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05002226 gui_msg("done=Done.");
Dees_Troy38bd7602012-09-14 13:33:53 -04002227 return true;
2228 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002229 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04002230 return false;
2231 }
2232 return true;
2233 }
2234 else
2235 return Wipe_RMRF();
2236
2237 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04002238}
2239
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002240bool TWPartition::Wipe_EXFAT() {
Vojtech Bocek05534202013-09-11 08:11:56 +02002241 string command;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002242
2243 if (TWFunc::Path_Exists("/sbin/mkexfatfs")) {
2244 if (!UnMount(true))
2245 return false;
2246
Greg Wallace4b44fef2015-12-29 15:33:24 -05002247 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mkexfatfs"));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002248 Find_Actual_Block_Device();
2249 command = "mkexfatfs " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02002250 if (TWFunc::Exec_Cmd(command) == 0) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002251 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05002252 gui_msg("done=Done.");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002253 return true;
2254 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002255 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002256 return false;
2257 }
2258 return true;
2259 }
2260 return false;
2261}
2262
Dees_Troy38bd7602012-09-14 13:33:53 -04002263bool TWPartition::Wipe_MTD() {
2264 if (!UnMount(true))
2265 return false;
2266
Greg Wallace4b44fef2015-12-29 15:33:24 -05002267 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("MTD"));
Dees_Troy38bd7602012-09-14 13:33:53 -04002268
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02002269 mtd_scan_partitions();
2270 const MtdPartition* mtd = mtd_find_partition_by_name(MTD_Name.c_str());
2271 if (mtd == NULL) {
2272 LOGERR("No mtd partition named '%s'", MTD_Name.c_str());
2273 return false;
2274 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002275
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02002276 MtdWriteContext* ctx = mtd_write_partition(mtd);
2277 if (ctx == NULL) {
2278 LOGERR("Can't write '%s', failed to format.", MTD_Name.c_str());
2279 return false;
2280 }
2281 if (mtd_erase_blocks(ctx, -1) == -1) {
2282 mtd_write_close(ctx);
2283 LOGERR("Failed to format '%s'", MTD_Name.c_str());
2284 return false;
2285 }
2286 if (mtd_write_close(ctx) != 0) {
2287 LOGERR("Failed to close '%s'", MTD_Name.c_str());
2288 return false;
2289 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08002290 Current_File_System = "yaffs2";
Dees_Troye58d5262012-09-21 12:27:57 -04002291 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05002292 gui_msg("done=Done.");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02002293 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002294}
2295
2296bool TWPartition::Wipe_RMRF() {
Dees_Troy38bd7602012-09-14 13:33:53 -04002297 if (!Mount(true))
2298 return false;
Ethan Yonker726a0202014-12-16 20:01:38 -06002299 // This is the only wipe that leaves the partition mounted, so we
2300 // must manually remove the partition from MTP if it is a storage
2301 // partition.
2302 if (Is_Storage)
2303 PartitionManager.Remove_MTP_Storage(MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04002304
Ethan Yonker74db1572015-10-28 12:44:49 -05002305 gui_msg(Msg("remove_all=Removing all files under '{1}'")(Mount_Point));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002306 TWFunc::removeDir(Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04002307 Recreate_AndSec_Folder();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002308 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002309}
2310
Dees_Troye5017042013-08-29 16:38:55 +00002311bool TWPartition::Wipe_F2FS() {
Vojtech Bocek05534202013-09-11 08:11:56 +02002312 string command;
Dees_Troye5017042013-08-29 16:38:55 +00002313
2314 if (TWFunc::Path_Exists("/sbin/mkfs.f2fs")) {
dianlujitao4879b372018-12-03 18:45:47 +08002315 bool NeedPreserveFooter = true;
2316
2317 Find_Actual_Block_Device();
2318 if (!Is_Present) {
2319 LOGINFO("Block device not present, cannot wipe %s.\n", Display_Name.c_str());
2320 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
2321 return false;
2322 }
Dees_Troye5017042013-08-29 16:38:55 +00002323 if (!UnMount(true))
2324 return false;
2325
dianlujitao4879b372018-12-03 18:45:47 +08002326 /**
2327 * On decrypted devices, IOCTL_Get_Block_Size calculates size on device mapper,
2328 * so there's no need to preserve footer.
2329 */
2330 if ((Is_Decrypted && !Decrypted_Block_Device.empty()) ||
2331 Crypto_Key_Location != "footer") {
2332 NeedPreserveFooter = false;
dhacker29a3fa75f2015-01-17 19:42:33 -05002333 }
dianlujitao4879b372018-12-03 18:45:47 +08002334
dianlujitao4879b372018-12-03 18:45:47 +08002335 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mkfs.f2fs"));
Ethan Yonker7e941582019-03-22 08:18:21 -05002336 // First determine if we have the old mkfs.f2fs that uses "-r reserved_bytes"
2337 // or the new mkfs.f2fs that expects the number of sectors as the optional last argument
2338 // Note: some 7.1 trees have the old and some have the new.
2339 command = "mkfs.f2fs | grep \"reserved\" > /tmp/f2fsversiontest";
2340 TWFunc::Exec_Cmd(command, false); // no help argument so printing usage exits with an error code
2341 if (!TWFunc::Path_Exists("/tmp/f2fsversiontest")) {
2342 LOGINFO("Error determining mkfs.f2fs version\n");
2343 return false;
2344 }
2345 if (TWFunc::Get_File_Size("/tmp/f2fsversiontest") <= 0) {
2346 LOGINFO("Using newer mkfs.f2fs\n");
2347 unsigned long long dev_sz = TWFunc::IOCTL_Get_Block_Size(Actual_Block_Device.c_str());
2348 if (!dev_sz)
2349 return false;
2350
2351 if (NeedPreserveFooter)
2352 Length < 0 ? dev_sz += Length : dev_sz -= CRYPT_FOOTER_OFFSET;
2353
2354 char dev_sz_str[48];
2355 sprintf(dev_sz_str, "%llu", (dev_sz / 4096));
2356 command = "mkfs.f2fs -d1 -f -O encrypt -O quota -O verity -w 4096 " + Actual_Block_Device + " " + dev_sz_str;
2357 if (TWFunc::Path_Exists("/sbin/sload.f2fs")) {
2358 command += " && sload.f2fs -t /data " + Actual_Block_Device;
2359 }
2360 } else {
2361 LOGINFO("Using older mkfs.f2fs\n");
2362 command = "mkfs.f2fs -t 0";
2363 if (NeedPreserveFooter) {
2364 // Only use length if we're not decrypted
2365 char len[32];
2366 int mod_length = Length;
2367 if (Length < 0)
2368 mod_length *= -1;
2369 sprintf(len, "%i", mod_length);
2370 command += " -r ";
2371 command += len;
2372 }
2373 command += " " + Actual_Block_Device;
dianlujitao4879b372018-12-03 18:45:47 +08002374 }
2375 LOGINFO("mkfs.f2fs command: %s\n", command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02002376 if (TWFunc::Exec_Cmd(command) == 0) {
dianlujitao4879b372018-12-03 18:45:47 +08002377 if (NeedPreserveFooter)
2378 Wipe_Crypto_Key();
Dees_Troye5017042013-08-29 16:38:55 +00002379 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05002380 gui_msg("done=Done.");
Dees_Troye5017042013-08-29 16:38:55 +00002381 return true;
2382 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002383 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troye5017042013-08-29 16:38:55 +00002384 return false;
2385 }
2386 return true;
2387 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002388 LOGINFO("mkfs.f2fs binary not found, using rm -rf to wipe.\n");
Dees_Troye5017042013-08-29 16:38:55 +00002389 return Wipe_RMRF();
2390 }
2391 return false;
2392}
2393
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002394bool TWPartition::Wipe_NTFS() {
2395 string command;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002396 string Ntfsmake_Binary;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002397
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002398 if (TWFunc::Path_Exists("/sbin/mkntfs"))
2399 Ntfsmake_Binary = "mkntfs";
2400 else if (TWFunc::Path_Exists("/sbin/mkfs.ntfs"))
2401 Ntfsmake_Binary = "mkfs.ntfs";
2402 else
2403 return false;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002404
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002405 if (!UnMount(true))
2406 return false;
2407
Greg Wallace4b44fef2015-12-29 15:33:24 -05002408 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)(Ntfsmake_Binary));
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002409 Find_Actual_Block_Device();
2410 command = "/sbin/" + Ntfsmake_Binary + " " + Actual_Block_Device;
2411 if (TWFunc::Exec_Cmd(command) == 0) {
2412 Recreate_AndSec_Folder();
2413 gui_msg("done=Done.");
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002414 return true;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002415 } else {
2416 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
2417 return false;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002418 }
2419 return false;
2420}
2421
Dees_Troy51a0e822012-09-05 15:24:24 -04002422bool TWPartition::Wipe_Data_Without_Wiping_Media() {
Ethan Yonker83e82572014-04-04 10:59:28 -05002423#ifdef TW_OEM_BUILD
2424 // In an OEM Build we want to do a full format
2425 return Wipe_Encryption();
2426#else
Ethan Yonker66a19492015-12-10 10:19:45 -06002427 bool ret = false;
Dees_Troy38bd7602012-09-14 13:33:53 -04002428
Dees_Troy38bd7602012-09-14 13:33:53 -04002429 if (!Mount(true))
2430 return false;
2431
Ethan Yonker74db1572015-10-28 12:44:49 -05002432 gui_msg("wiping_data=Wiping data without wiping /data/media ...");
Ethan Yonker66a19492015-12-10 10:19:45 -06002433 ret = Wipe_Data_Without_Wiping_Media_Func(Mount_Point + "/");
2434 if (ret)
2435 gui_msg("done=Done.");
2436 return ret;
2437#endif // ifdef TW_OEM_BUILD
2438}
2439
2440bool TWPartition::Wipe_Data_Without_Wiping_Media_Func(const string& parent __unused) {
2441 string dir;
Dees_Troy38bd7602012-09-14 13:33:53 -04002442
2443 DIR* d;
Ethan Yonker66a19492015-12-10 10:19:45 -06002444 d = opendir(parent.c_str());
Dees_Troy16b74352012-11-14 22:27:31 +00002445 if (d != NULL) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002446 struct dirent* de;
2447 while ((de = readdir(d)) != NULL) {
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002448 if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) continue;
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05002449
Ethan Yonker66a19492015-12-10 10:19:45 -06002450 dir = parent;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002451 dir.append(de->d_name);
Ethan Yonker3fdcda42016-11-30 12:29:37 -06002452 if (wipe_exclusions.check_skip_dirs(dir)) {
Ethan Yonker66a19492015-12-10 10:19:45 -06002453 LOGINFO("skipped '%s'\n", dir.c_str());
2454 continue;
2455 }
Dees_Troyce675462013-01-09 19:48:21 +00002456 if (de->d_type == DT_DIR) {
Ethan Yonker66a19492015-12-10 10:19:45 -06002457 dir.append("/");
2458 if (!Wipe_Data_Without_Wiping_Media_Func(dir)) {
2459 closedir(d);
2460 return false;
2461 }
2462 rmdir(dir.c_str());
bigbiff bigbiff98f1f902013-01-19 18:46:13 -05002463 } 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 +03002464 if (unlink(dir.c_str()) != 0)
Ethan Yonker79f88bd2016-12-09 14:52:12 -06002465 LOGINFO("Unable to unlink '%s': %s\n", dir.c_str(), strerror(errno));
Dees_Troyce675462013-01-09 19:48:21 +00002466 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002467 }
2468 closedir(d);
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05002469
Dees_Troy16b74352012-11-14 22:27:31 +00002470 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -04002471 }
Ethan Yonker74db1572015-10-28 12:44:49 -05002472 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy16b74352012-11-14 22:27:31 +00002473 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04002474}
2475
dianlujitao4879b372018-12-03 18:45:47 +08002476void TWPartition::Wipe_Crypto_Key() {
2477 Find_Actual_Block_Device();
2478 if (Crypto_Key_Location.empty())
2479 return;
2480 else if (Crypto_Key_Location == "footer") {
2481 int fd = open(Actual_Block_Device.c_str(), O_RDWR);
2482 if (fd < 0) {
2483 gui_print_color("warning", "Unable to open '%s' to wipe crypto key\n", Actual_Block_Device.c_str());
2484 return;
2485 }
2486
2487 unsigned int block_count;
2488 if ((ioctl(fd, BLKGETSIZE, &block_count)) == -1) {
2489 gui_print_color("warning", "Unable to get block size for wiping crypto footer.\n");
2490 } else {
2491 int newlen = Length < 0 ? -Length : CRYPT_FOOTER_OFFSET;
2492 off64_t offset = ((off64_t)block_count * 512) - newlen;
2493 if (lseek64(fd, offset, SEEK_SET) == -1) {
2494 gui_print_color("warning", "Unable to lseek64 for wiping crypto footer.\n");
2495 } else {
2496 void* buffer = malloc(newlen);
2497 if (!buffer) {
2498 gui_print_color("warning", "Failed to malloc for wiping crypto footer.\n");
2499 } else {
2500 memset(buffer, 0, newlen);
2501 int ret = write(fd, buffer, newlen);
2502 if (ret != newlen) {
2503 gui_print_color("warning", "Failed to wipe crypto footer.\n");
2504 } else {
2505 LOGINFO("Successfully wiped crypto footer.\n");
2506 }
2507 free(buffer);
2508 }
2509 }
2510 }
2511 close(fd);
2512 } else {
2513 if (TWFunc::IOCTL_Get_Block_Size(Crypto_Key_Location.c_str()) >= 16384LLU) {
2514 string Command = "dd of='" + Crypto_Key_Location + "' if=/dev/zero bs=16384 count=1";
2515 TWFunc::Exec_Cmd(Command);
2516 } else {
2517 LOGINFO("Crypto key location reports size < 16K so not wiping crypto footer.\n");
2518 }
2519 }
2520}
2521
bigbiffce8f83c2015-12-12 18:30:21 -05002522bool TWPartition::Backup_Tar(PartitionSettings *part_settings, pid_t *tar_fork_pid) {
Ethan Yonker472f5062016-02-25 13:47:30 -06002523 string Full_FileName;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002524 twrpTar tar;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05002525
Dees_Troy43d8b002012-09-17 16:00:01 -04002526 if (!Mount(true))
2527 return false;
2528
Ian Macdonalda3b25a32019-06-16 22:39:14 +02002529 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Backup_Display_Name, gui_parse_text("{@backing}"));
Ethan Yonker74db1572015-10-28 12:44:49 -05002530 gui_msg(Msg("backing_up=Backing up {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002531
Ethan Yonker472f5062016-02-25 13:47:30 -06002532 DataManager::GetValue(TW_USE_COMPRESSION_VAR, tar.use_compression);
Matt Mowerbb81e5d2014-03-20 18:05:41 -05002533
Dees_Troy83bd4832013-05-04 12:39:56 +00002534#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
Ethan Yonker472f5062016-02-25 13:47:30 -06002535 if (Can_Encrypt_Backup) {
2536 DataManager::GetValue("tw_encrypt_backup", tar.use_encryption);
2537 if (tar.use_encryption) {
2538 if (Use_Userdata_Encryption)
2539 tar.userdata_encryption = tar.use_encryption;
2540 string Password;
2541 DataManager::GetValue("tw_backup_password", Password);
2542 tar.setpassword(Password);
2543 } else {
2544 tar.use_encryption = 0;
2545 }
Dees_Troy83bd4832013-05-04 12:39:56 +00002546 }
2547#endif
Dees_Troy43d8b002012-09-17 16:00:01 -04002548
Ethan Yonker472f5062016-02-25 13:47:30 -06002549 Backup_FileName = Backup_Name + "." + Current_File_System + ".win";
Ethan Yonkere080c1f2016-09-19 13:50:25 -05002550 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
Ethan Yonker01f4e032017-02-03 15:30:52 -06002551 if (Has_Data_Media)
2552 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 -05002553 tar.part_settings = part_settings;
Ethan Yonker3fdcda42016-11-30 12:29:37 -06002554 tar.backup_exclusions = &backup_exclusions;
Dees Troye0a433a2013-12-02 04:10:37 +00002555 tar.setdir(Backup_Path);
2556 tar.setfn(Full_FileName);
2557 tar.setsize(Backup_Size);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002558 tar.partition_name = Backup_Name;
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002559 tar.backup_folder = part_settings->Backup_Folder;
bigbiffce8f83c2015-12-12 18:30:21 -05002560 if (tar.createTarFork(tar_fork_pid) != 0)
Dees Troye0a433a2013-12-02 04:10:37 +00002561 return false;
Dees_Troy43d8b002012-09-17 16:00:01 -04002562 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002563}
2564
bigbiffce8f83c2015-12-12 18:30:21 -05002565bool TWPartition::Backup_Image(PartitionSettings *part_settings) {
2566 string Full_FileName, adb_file_name;
igoriok87e3d932013-01-31 21:03:53 +02002567
Ethan Yonker74db1572015-10-28 12:44:49 -05002568 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, gui_parse_text("{@backing}"));
2569 gui_msg(Msg("backing_up=Backing up {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002570
Ethan Yonker472f5062016-02-25 13:47:30 -06002571 Backup_FileName = Backup_Name + "." + Current_File_System + ".win";
Dees_Troy43d8b002012-09-17 16:00:01 -04002572
bigbiffce8f83c2015-12-12 18:30:21 -05002573 if (part_settings->adbbackup) {
2574 Full_FileName = TW_ADB_BACKUP;
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002575 adb_file_name = part_settings->Backup_Folder + "/" + Backup_FileName;
bigbiffce8f83c2015-12-12 18:30:21 -05002576 }
2577 else
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002578 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
bigbiffce8f83c2015-12-12 18:30:21 -05002579
2580 part_settings->total_restore_size = Backup_Size;
2581
2582 if (part_settings->adbbackup) {
2583 if (!twadbbu::Write_TWIMG(adb_file_name, Backup_Size))
2584 return false;
2585 }
2586
2587 if (!Raw_Read_Write(part_settings))
Ethan Yonker472f5062016-02-25 13:47:30 -06002588 return false;
2589
bigbiffce8f83c2015-12-12 18:30:21 -05002590 if (part_settings->adbbackup) {
2591 if (!twadbbu::Write_TWEOF())
2592 return false;
Dees_Troy7c2dec82012-09-26 09:49:14 -04002593 }
Dees_Troy43d8b002012-09-17 16:00:01 -04002594 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002595}
2596
bigbiffce8f83c2015-12-12 18:30:21 -05002597bool TWPartition::Raw_Read_Write(PartitionSettings *part_settings) {
2598 unsigned long long RW_Block_Size, Remain = Backup_Size;
2599 int src_fd = -1, dest_fd = -1;
2600 ssize_t bs;
Ethan Yonker472f5062016-02-25 13:47:30 -06002601 bool ret = false;
2602 void* buffer = NULL;
2603 unsigned long long backedup_size = 0;
bigbiffce8f83c2015-12-12 18:30:21 -05002604 string srcfn, destfn;
Ethan Yonker472f5062016-02-25 13:47:30 -06002605
bigbiffce8f83c2015-12-12 18:30:21 -05002606 if (part_settings->PM_Method == PM_BACKUP) {
2607 srcfn = Actual_Block_Device;
2608 if (part_settings->adbbackup)
2609 destfn = TW_ADB_BACKUP;
bigbiff bigbiff38b83c12017-12-28 19:58:52 -05002610 else {
Ethan Yonkere080c1f2016-09-19 13:50:25 -05002611 destfn = part_settings->Backup_Folder + "/" + Backup_FileName;
bigbiff bigbiff38b83c12017-12-28 19:58:52 -05002612 }
bigbiffce8f83c2015-12-12 18:30:21 -05002613 }
2614 else {
2615 destfn = Actual_Block_Device;
2616 if (part_settings->adbbackup) {
2617 srcfn = TW_ADB_RESTORE;
2618 } else {
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002619 srcfn = part_settings->Backup_Folder + "/" + Backup_FileName;
bigbiffce8f83c2015-12-12 18:30:21 -05002620 Remain = TWFunc::Get_File_Size(srcfn);
2621 }
2622 }
Ethan Yonker472f5062016-02-25 13:47:30 -06002623
bigbiffce8f83c2015-12-12 18:30:21 -05002624 src_fd = open(srcfn.c_str(), O_RDONLY | O_LARGEFILE);
Ethan Yonker472f5062016-02-25 13:47:30 -06002625 if (src_fd < 0) {
bigbiffce8f83c2015-12-12 18:30:21 -05002626 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(srcfn.c_str())(strerror(errno)));
Ethan Yonker472f5062016-02-25 13:47:30 -06002627 return false;
2628 }
bigbiffce8f83c2015-12-12 18:30:21 -05002629
2630 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 -06002631 if (dest_fd < 0) {
bigbiffce8f83c2015-12-12 18:30:21 -05002632 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(destfn.c_str())(strerror(errno)));
Ethan Yonker472f5062016-02-25 13:47:30 -06002633 goto exit;
2634 }
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -04002635
bigbiffce8f83c2015-12-12 18:30:21 -05002636 LOGINFO("Reading '%s', writing '%s'\n", srcfn.c_str(), destfn.c_str());
2637
2638 if (part_settings->adbbackup) {
2639 RW_Block_Size = MAX_ADB_READ;
2640 bs = MAX_ADB_READ;
2641 }
2642 else {
2643 RW_Block_Size = 1048576LLU; // 1MB
2644 bs = (ssize_t)(RW_Block_Size);
2645 }
2646
Ethan Yonker472f5062016-02-25 13:47:30 -06002647 buffer = malloc((size_t)bs);
2648 if (!buffer) {
2649 LOGINFO("Raw_Read_Write failed to malloc\n");
2650 goto exit;
2651 }
bigbiffce8f83c2015-12-12 18:30:21 -05002652
2653 if (part_settings->progress)
2654 part_settings->progress->SetPartitionSize(part_settings->total_restore_size);
2655
Ethan Yonker472f5062016-02-25 13:47:30 -06002656 while (Remain > 0) {
2657 if (Remain < RW_Block_Size)
bigbiffce8f83c2015-12-12 18:30:21 -05002658 bs = (ssize_t)(Remain);
Matt Mowera8a89d12016-12-30 18:10:37 -06002659 if (read(src_fd, buffer, bs) != bs) {
Ethan Yonker472f5062016-02-25 13:47:30 -06002660 LOGINFO("Error reading source fd (%s)\n", strerror(errno));
2661 goto exit;
2662 }
2663 if (write(dest_fd, buffer, bs) != bs) {
2664 LOGINFO("Error writing destination fd (%s)\n", strerror(errno));
2665 goto exit;
2666 }
2667 backedup_size += (unsigned long long)(bs);
Matt Mower029a82d2017-01-08 13:12:38 -06002668 Remain -= (unsigned long long)(bs);
bigbiffce8f83c2015-12-12 18:30:21 -05002669 if (part_settings->progress)
2670 part_settings->progress->UpdateSize(backedup_size);
Ethan Yonker472f5062016-02-25 13:47:30 -06002671 if (PartitionManager.Check_Backup_Cancel() != 0)
2672 goto exit;
2673 }
bigbiffce8f83c2015-12-12 18:30:21 -05002674 if (part_settings->progress)
2675 part_settings->progress->UpdateDisplayDetails(true);
Ethan Yonker472f5062016-02-25 13:47:30 -06002676 fsync(dest_fd);
nailyka083dc62016-11-11 21:41:28 +01002677
2678 if (!part_settings->adbbackup && part_settings->PM_Method == PM_BACKUP) {
2679 tw_set_default_metadata(destfn.c_str());
2680 LOGINFO("Restored default metadata for %s\n", destfn.c_str());
2681 }
2682
Ethan Yonker472f5062016-02-25 13:47:30 -06002683 ret = true;
2684exit:
2685 if (src_fd >= 0)
2686 close(src_fd);
2687 if (dest_fd >= 0)
2688 close(dest_fd);
2689 if (buffer)
2690 free(buffer);
2691 return ret;
2692}
2693
bigbiffce8f83c2015-12-12 18:30:21 -05002694bool TWPartition::Backup_Dump_Image(PartitionSettings *part_settings) {
Vojtech Bocek05534202013-09-11 08:11:56 +02002695 string Full_FileName, Command;
Dees_Troy43d8b002012-09-17 16:00:01 -04002696
Ethan Yonker74db1572015-10-28 12:44:49 -05002697 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, gui_parse_text("{@backing}"));
2698 gui_msg(Msg("backing_up=Backing up {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002699
bigbiffce8f83c2015-12-12 18:30:21 -05002700 if (part_settings->progress)
2701 part_settings->progress->SetPartitionSize(Backup_Size);
Dees_Troy43d8b002012-09-17 16:00:01 -04002702
Ethan Yonker472f5062016-02-25 13:47:30 -06002703 Backup_FileName = Backup_Name + "." + Current_File_System + ".win";
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002704 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04002705
2706 Command = "dump_image " + MTD_Name + " '" + Full_FileName + "'";
bigbiffce8f83c2015-12-12 18:30:21 -05002707
Dees_Troy2673cec2013-04-02 20:22:16 +00002708 LOGINFO("Backup command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02002709 TWFunc::Exec_Cmd(Command);
Ethan Yonker4b94cfd2014-12-11 10:00:45 -06002710 tw_set_default_metadata(Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04002711 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
2712 // 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 -06002713 gui_msg(Msg(msg::kError, "backup_size=Backup file size for '{1}' is 0 bytes.")(Full_FileName));
Dees_Troy7c2dec82012-09-26 09:49:14 -04002714 return false;
2715 }
bigbiffce8f83c2015-12-12 18:30:21 -05002716 if (part_settings->progress)
2717 part_settings->progress->UpdateSize(Backup_Size);
2718
Dees_Troy43d8b002012-09-17 16:00:01 -04002719 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002720}
2721
bigbiffce8f83c2015-12-12 18:30:21 -05002722unsigned long long TWPartition::Get_Restore_Size(PartitionSettings *part_settings) {
2723 if (!part_settings->adbbackup) {
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002724 InfoManager restore_info(part_settings->Backup_Folder + "/" + Backup_Name + ".info");
bigbiffce8f83c2015-12-12 18:30:21 -05002725 if (restore_info.LoadValues() == 0) {
2726 if (restore_info.GetValue("backup_size", Restore_Size) == 0) {
2727 LOGINFO("Read info file, restore size is %llu\n", Restore_Size);
2728 return Restore_Size;
2729 }
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002730 }
2731 }
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002732
Matt Mower029a82d2017-01-08 13:12:38 -06002733 string Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
2734 string Restore_File_System = Get_Restore_File_System(part_settings);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002735
2736 if (Is_Image(Restore_File_System)) {
2737 Restore_Size = TWFunc::Get_File_Size(Full_FileName);
2738 return Restore_Size;
2739 }
2740
2741 twrpTar tar;
2742 tar.setdir(Backup_Path);
2743 tar.setfn(Full_FileName);
bigbiffce8f83c2015-12-12 18:30:21 -05002744 tar.backup_name = Full_FileName;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002745#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
2746 string Password;
2747 DataManager::GetValue("tw_restore_password", Password);
2748 if (!Password.empty())
2749 tar.setpassword(Password);
2750#endif
2751 tar.partition_name = Backup_Name;
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002752 tar.backup_folder = part_settings->Backup_Folder;
bigbiffce8f83c2015-12-12 18:30:21 -05002753 tar.part_settings = part_settings;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002754 Restore_Size = tar.get_size();
2755 return Restore_Size;
2756}
2757
bigbiffce8f83c2015-12-12 18:30:21 -05002758bool TWPartition::Restore_Tar(PartitionSettings *part_settings) {
Ethan Yonker472f5062016-02-25 13:47:30 -06002759 string Full_FileName;
Dees Troy4159aed2014-02-28 17:24:43 +00002760 bool ret = false;
bigbiffce8f83c2015-12-12 18:30:21 -05002761 string Restore_File_System = Get_Restore_File_System(part_settings);
Dees_Troy43d8b002012-09-17 16:00:01 -04002762
Dees_Troye58d5262012-09-21 12:27:57 -04002763 if (Has_Android_Secure) {
Dees_Troye58d5262012-09-21 12:27:57 -04002764 if (!Wipe_AndSec())
2765 return false;
Gary Peck43acadf2012-11-21 21:19:01 -08002766 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002767 gui_msg(Msg("wiping=Wiping {1}")(Backup_Display_Name));
Ethan Yonker5eac2222014-06-11 12:22:55 -05002768 if (Has_Data_Media && Mount_Point == "/data" && Restore_File_System != Current_File_System) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002769 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 -05002770 if (!Wipe_Data_Without_Wiping_Media())
2771 return false;
2772 } else {
2773 if (!Wipe(Restore_File_System))
2774 return false;
2775 }
Dees_Troye58d5262012-09-21 12:27:57 -04002776 }
Matt Mower3c366972015-12-25 19:28:31 -06002777 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Backup_Display_Name, gui_parse_text("{@restoring_hdr}"));
Ethan Yonker74db1572015-10-28 12:44:49 -05002778 gui_msg(Msg("restoring=Restoring {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002779
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05002780 // Remount as read/write as needed so we can restore the backup
2781 if (!ReMount_RW(true))
Dees_Troy43d8b002012-09-17 16:00:01 -04002782 return false;
2783
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002784 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
Ethan Yonker87af5632014-02-10 11:56:35 -06002785 twrpTar tar;
bigbiffce8f83c2015-12-12 18:30:21 -05002786 tar.part_settings = part_settings;
Ethan Yonker87af5632014-02-10 11:56:35 -06002787 tar.setdir(Backup_Path);
2788 tar.setfn(Full_FileName);
2789 tar.backup_name = Backup_Name;
2790#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
2791 string Password;
2792 DataManager::GetValue("tw_restore_password", Password);
2793 if (!Password.empty())
2794 tar.setpassword(Password);
2795#endif
bigbiffce8f83c2015-12-12 18:30:21 -05002796 part_settings->progress->SetPartitionSize(Get_Restore_Size(part_settings));
2797 if (tar.extractTarFork() != 0)
Dees Troy4159aed2014-02-28 17:24:43 +00002798 ret = false;
2799 else
2800 ret = true;
2801#ifdef HAVE_CAPABILITIES
2802 // Restore capabilities to the run-as binary
Captain Throwback9d6feb52018-07-27 10:05:24 -04002803 if (Mount_Point == PartitionManager.Get_Android_Root_Path() && Mount(true) && TWFunc::Path_Exists("/system/bin/run-as")) {
Dees Troy4159aed2014-02-28 17:24:43 +00002804 struct vfs_cap_data cap_data;
2805 uint64_t capabilities = (1 << CAP_SETUID) | (1 << CAP_SETGID);
2806
2807 memset(&cap_data, 0, sizeof(cap_data));
2808 cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE;
2809 cap_data.data[0].permitted = (uint32_t) (capabilities & 0xffffffff);
2810 cap_data.data[0].inheritable = 0;
2811 cap_data.data[1].permitted = (uint32_t) (capabilities >> 32);
2812 cap_data.data[1].inheritable = 0;
2813 if (setxattr("/system/bin/run-as", XATTR_NAME_CAPS, &cap_data, sizeof(cap_data), 0) < 0) {
2814 LOGINFO("Failed to reset capabilities of /system/bin/run-as binary.\n");
2815 } else {
2816 LOGINFO("Reset capabilities of /system/bin/run-as binary successful.\n");
2817 }
2818 }
2819#endif
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05002820 if (Mount_Read_Only || Mount_Flags & MS_RDONLY)
2821 // Remount as read only when restoration is complete
2822 ReMount(true);
2823
Dees Troy4159aed2014-02-28 17:24:43 +00002824 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04002825}
2826
bigbiffce8f83c2015-12-12 18:30:21 -05002827bool TWPartition::Restore_Image(PartitionSettings *part_settings) {
Ethan Yonker96af84a2015-01-05 14:58:36 -06002828 string Full_FileName;
bigbiffce8f83c2015-12-12 18:30:21 -05002829 string Restore_File_System = Get_Restore_File_System(part_settings);
Dees_Troy43d8b002012-09-17 16:00:01 -04002830
Matt Mower3c366972015-12-25 19:28:31 -06002831 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Backup_Display_Name, gui_parse_text("{@restoring_hdr}"));
Ethan Yonker74db1572015-10-28 12:44:49 -05002832 gui_msg(Msg("restoring=Restoring {1}...")(Backup_Display_Name));
bigbiffce8f83c2015-12-12 18:30:21 -05002833
2834 if (part_settings->adbbackup)
2835 Full_FileName = TW_ADB_RESTORE;
2836 else
Ethan Yonkere080c1f2016-09-19 13:50:25 -05002837 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
Gary Peck15e623d2012-11-21 21:07:58 -08002838
Ethan Yonker96af84a2015-01-05 14:58:36 -06002839 if (Restore_File_System == "emmc") {
bigbiffce8f83c2015-12-12 18:30:21 -05002840 if (!part_settings->adbbackup)
2841 part_settings->total_restore_size = (uint64_t)(TWFunc::Get_File_Size(Full_FileName));
2842 if (!Raw_Read_Write(part_settings))
Ethan Yonker96af84a2015-01-05 14:58:36 -06002843 return false;
2844 } else if (Restore_File_System == "mtd" || Restore_File_System == "bml") {
bigbiffce8f83c2015-12-12 18:30:21 -05002845 if (!Flash_Image_FI(Full_FileName, part_settings->progress))
2846 return false;
2847 }
2848
2849 if (part_settings->adbbackup) {
2850 if (!twadbbu::Write_TWEOF())
Ethan Yonker96af84a2015-01-05 14:58:36 -06002851 return false;
Gary Peck15e623d2012-11-21 21:07:58 -08002852 }
Dees_Troy43d8b002012-09-17 16:00:01 -04002853 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002854}
Dees_Troy5bf43922012-09-07 16:07:55 -04002855
2856bool TWPartition::Update_Size(bool Display_Error) {
Dees_Troy0550cfb2012-10-13 11:56:13 -04002857 bool ret = false, Was_Already_Mounted = false;
Dees_Troy51127312012-09-08 13:08:49 -04002858
Ethan Yonker1b190162016-12-05 15:25:19 -06002859 Find_Actual_Block_Device();
2860
2861 if (!Can_Be_Mounted && !Is_Encrypted) {
2862 if (TWFunc::Path_Exists(Actual_Block_Device) && Find_Partition_Size()) {
2863 Used = Size;
2864 Backup_Size = Size;
2865 return true;
2866 }
Dees_Troy5bf43922012-09-07 16:07:55 -04002867 return false;
Ethan Yonker1b190162016-12-05 15:25:19 -06002868 }
Dees_Troy5bf43922012-09-07 16:07:55 -04002869
Dees_Troy0550cfb2012-10-13 11:56:13 -04002870 Was_Already_Mounted = Is_Mounted();
Dees_Troy38bd7602012-09-14 13:33:53 -04002871 if (Removable || Is_Encrypted) {
2872 if (!Mount(false))
2873 return true;
2874 } else if (!Mount(Display_Error))
Dees_Troy5bf43922012-09-07 16:07:55 -04002875 return false;
Dees_Troy51127312012-09-08 13:08:49 -04002876
2877 ret = Get_Size_Via_statfs(Display_Error);
Dees_Troy0550cfb2012-10-13 11:56:13 -04002878 if (!ret || Size == 0) {
2879 if (!Get_Size_Via_df(Display_Error)) {
2880 if (!Was_Already_Mounted)
2881 UnMount(false);
Dees_Troy51127312012-09-08 13:08:49 -04002882 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04002883 }
2884 }
Dees_Troy51127312012-09-08 13:08:49 -04002885
Dees_Troy5bf43922012-09-07 16:07:55 -04002886 if (Has_Data_Media) {
2887 if (Mount(Display_Error)) {
Ethan Yonker3fdcda42016-11-30 12:29:37 -06002888 Used = backup_exclusions.Get_Folder_Size(Mount_Point);
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002889 Backup_Size = Used;
2890 int bak = (int)(Used / 1048576LLU);
Dees_Troy51127312012-09-08 13:08:49 -04002891 int fre = (int)(Free / 1048576LLU);
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002892 LOGINFO("Data backup size is %iMB, free: %iMB.\n", bak, fre);
Dees_Troy0550cfb2012-10-13 11:56:13 -04002893 } else {
2894 if (!Was_Already_Mounted)
2895 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04002896 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04002897 }
Dees_Troye58d5262012-09-21 12:27:57 -04002898 } else if (Has_Android_Secure) {
2899 if (Mount(Display_Error))
Ethan Yonker3fdcda42016-11-30 12:29:37 -06002900 Backup_Size = backup_exclusions.Get_Folder_Size(Backup_Path);
Dees_Troy0550cfb2012-10-13 11:56:13 -04002901 else {
2902 if (!Was_Already_Mounted)
2903 UnMount(false);
Dees_Troye58d5262012-09-21 12:27:57 -04002904 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04002905 }
Dees_Troy5bf43922012-09-07 16:07:55 -04002906 }
Dees_Troy0550cfb2012-10-13 11:56:13 -04002907 if (!Was_Already_Mounted)
2908 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04002909 return true;
Dees_Troy51127312012-09-08 13:08:49 -04002910}
Dees_Troy38bd7602012-09-14 13:33:53 -04002911
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06002912bool TWPartition::Find_Wildcard_Block_Devices(const string& Device) {
2913 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
2914 string Path = TWFunc::Get_Path(Device);
2915 string Dev = TWFunc::Get_Filename(Device);
2916 size_t wildcard_index = Dev.find("*");
2917 if (wildcard_index != string::npos)
2918 Dev = Dev.substr(0, wildcard_index);
2919 wildcard_index = Dev.size();
2920 DIR* d = opendir(Path.c_str());
2921 if (d == NULL) {
2922 LOGINFO("Error opening '%s': %s\n", Path.c_str(), strerror(errno));
2923 return false;
2924 }
2925 struct dirent* de;
2926 while ((de = readdir(d)) != NULL) {
2927 if (de->d_type != DT_BLK || strlen(de->d_name) <= wildcard_index || strncmp(de->d_name, Dev.c_str(), wildcard_index) != 0)
2928 continue;
2929
2930 string item = Path + "/";
2931 item.append(de->d_name);
2932 if (PartitionManager.Find_Partition_By_Block_Device(item))
2933 continue;
2934 TWPartition *part = new TWPartition;
2935 char buffer[MAX_FSTAB_LINE_LENGTH];
2936 sprintf(buffer, "%s %s-%i auto defaults defaults", item.c_str(), Mount_Point.c_str(), ++mount_point_index);
Chaosmasterf6e42ce2020-01-27 00:17:04 +01002937 part->Process_Fstab_Line(buffer, false, NULL, false);
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06002938 char display[MAX_FSTAB_LINE_LENGTH];
2939 sprintf(display, "%s %i", Storage_Name.c_str(), mount_point_index);
2940 part->Storage_Name = display;
2941 part->Display_Name = display;
2942 part->Primary_Block_Device = item;
2943 part->Wildcard_Block_Device = false;
2944 part->Is_SubPartition = true;
2945 part->SubPartition_Of = Mount_Point;
2946 part->Is_Storage = Is_Storage;
2947 part->Can_Be_Mounted = true;
2948 part->Removable = true;
2949 part->Can_Be_Wiped = Can_Be_Wiped;
2950 part->Wipe_Available_in_GUI = Wipe_Available_in_GUI;
2951 part->Find_Actual_Block_Device();
2952 part->Update_Size(false);
2953 Has_SubPartition = true;
2954 PartitionManager.Output_Partition(part);
2955 PartitionManager.Add_Partition(part);
2956 }
2957 closedir(d);
2958 return (mount_point_index > 0);
2959}
2960
Dees_Troy38bd7602012-09-14 13:33:53 -04002961void TWPartition::Find_Actual_Block_Device(void) {
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06002962 if (!Sysfs_Entry.empty() && Primary_Block_Device.empty() && Decrypted_Block_Device.empty()) {
2963 /* Sysfs_Entry.empty() indicates if this is a sysfs entry that begins with /device/
2964 * If we have a syfs entry then we are looking for this device from a uevent add.
2965 * The uevent add will set the primary block device based on the data we receive from
2966 * after checking for adopted storage. If the device ends up being adopted, then the
2967 * decrypted block device will be set instead of the primary block device. */
2968 Is_Present = false;
2969 return;
2970 }
2971 if (Wildcard_Block_Device && !Is_Adopted_Storage) {
2972 Is_Present = false;
2973 Actual_Block_Device = "";
2974 Can_Be_Mounted = false;
2975 if (!Find_Wildcard_Block_Devices(Primary_Block_Device)) {
2976 string Dev = Primary_Block_Device.substr(0, Primary_Block_Device.find("*"));
2977 if (TWFunc::Path_Exists(Dev)) {
2978 Is_Present = true;
2979 Can_Be_Mounted = true;
2980 Actual_Block_Device = Dev;
2981 }
2982 }
2983 return;
2984 } else if (Is_Decrypted && !Decrypted_Block_Device.empty()) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002985 Actual_Block_Device = Decrypted_Block_Device;
Ethan Yonker1b190162016-12-05 15:25:19 -06002986 if (TWFunc::Path_Exists(Decrypted_Block_Device)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002987 Is_Present = true;
Ethan Yonker1b190162016-12-05 15:25:19 -06002988 return;
2989 }
2990 } else if (SlotSelect && TWFunc::Path_Exists(Primary_Block_Device + PartitionManager.Get_Active_Slot_Suffix())) {
2991 Actual_Block_Device = Primary_Block_Device + PartitionManager.Get_Active_Slot_Suffix();
2992 unlink(Primary_Block_Device.c_str());
2993 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
2994 Is_Present = true;
2995 return;
Dees_Troy43d8b002012-09-17 16:00:01 -04002996 } else if (TWFunc::Path_Exists(Primary_Block_Device)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002997 Is_Present = true;
2998 Actual_Block_Device = Primary_Block_Device;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002999 return;
3000 }
Ethan Yonker1b190162016-12-05 15:25:19 -06003001 if (!Alternate_Block_Device.empty() && TWFunc::Path_Exists(Alternate_Block_Device)) {
Dees_Troy3f04d032012-10-07 18:20:09 -04003002 Actual_Block_Device = Alternate_Block_Device;
Dees_Troy38bd7602012-09-14 13:33:53 -04003003 Is_Present = true;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05003004 } else {
Dees_Troy38bd7602012-09-14 13:33:53 -04003005 Is_Present = false;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05003006 }
Dees_Troy38bd7602012-09-14 13:33:53 -04003007}
3008
3009void TWPartition::Recreate_Media_Folder(void) {
3010 string Command;
Ethan Yonker66a19492015-12-10 10:19:45 -06003011 string Media_Path = Mount_Point + "/media";
Dees_Troy38bd7602012-09-14 13:33:53 -04003012
Ethan Yonkerbd7492d2016-12-07 13:55:01 -06003013 if (Is_FBE) {
3014 LOGINFO("Not recreating media folder on FBE\n");
3015 return;
3016 }
Dees_Troy38bd7602012-09-14 13:33:53 -04003017 if (!Mount(true)) {
Ethan Yonker66a19492015-12-10 10:19:45 -06003018 gui_msg(Msg(msg::kError, "recreate_folder_err=Unable to recreate {1} folder.")(Media_Path));
3019 } else if (!TWFunc::Path_Exists(Media_Path)) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05003020 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Ethan Yonker66a19492015-12-10 10:19:45 -06003021 LOGINFO("Recreating %s folder.\n", Media_Path.c_str());
3022 mkdir(Media_Path.c_str(), 0770);
xiaolu9416f4f2015-06-04 08:22:23 +08003023 string Internal_path = DataManager::GetStrValue("tw_internal_path");
3024 if (!Internal_path.empty()) {
3025 LOGINFO("Recreating %s folder.\n", Internal_path.c_str());
3026 mkdir(Internal_path.c_str(), 0770);
3027 }
3028#ifdef TW_INTERNAL_STORAGE_PATH
3029 mkdir(EXPAND(TW_INTERNAL_STORAGE_PATH), 0770);
3030#endif
Matt Mower87413642017-01-17 21:14:46 -06003031
thata3d31fb2014-12-21 22:27:40 +01003032 // Afterwards, we will try to set the
3033 // default metadata that we were hopefully able to get during
3034 // early boot.
Ethan Yonker66a19492015-12-10 10:19:45 -06003035 tw_set_default_metadata(Media_Path.c_str());
xiaolu9416f4f2015-06-04 08:22:23 +08003036 if (!Internal_path.empty())
3037 tw_set_default_metadata(Internal_path.c_str());
Matt Mower87413642017-01-17 21:14:46 -06003038
Ethan Yonker5eac2222014-06-11 12:22:55 -05003039 // Toggle mount to ensure that "internal sdcard" gets mounted
bigbiff bigbiff9c754052013-01-09 09:09:08 -05003040 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Ethan Yonker5eac2222014-06-11 12:22:55 -05003041 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Dees_Troy38bd7602012-09-14 13:33:53 -04003042 }
Dees_Troy43d8b002012-09-17 16:00:01 -04003043}
Dees_Troye58d5262012-09-21 12:27:57 -04003044
3045void TWPartition::Recreate_AndSec_Folder(void) {
Dees_Troye58d5262012-09-21 12:27:57 -04003046 if (!Has_Android_Secure)
3047 return;
Dees_Troy2673cec2013-04-02 20:22:16 +00003048 LOGINFO("Creating %s: %s\n", Backup_Display_Name.c_str(), Symlink_Path.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -04003049 if (!Mount(true)) {
Ethan Yonker74db1572015-10-28 12:44:49 -05003050 gui_msg(Msg(msg::kError, "recreate_folder_err=Unable to recreate {1} folder.")(Backup_Name));
Dees_Troye58d5262012-09-21 12:27:57 -04003051 } else if (!TWFunc::Path_Exists(Symlink_Path)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00003052 LOGINFO("Recreating %s folder.\n", Backup_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05003053 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05003054 mkdir(Symlink_Path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
bigbiff bigbiff9c754052013-01-09 09:09:08 -05003055 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04003056 }
3057}
bigbiff7cb4c332014-11-26 20:36:07 -05003058
3059uint64_t TWPartition::Get_Max_FileSize() {
3060 uint64_t maxFileSize = 0;
3061 const uint64_t constGB = (uint64_t) 1024 * 1024 * 1024;
3062 const uint64_t constTB = (uint64_t) constGB * 1024;
3063 const uint64_t constPB = (uint64_t) constTB * 1024;
bigbiff0c532032014-12-21 13:41:26 -05003064 if (Current_File_System == "ext4")
3065 maxFileSize = 16 * constTB; //16 TB
3066 else if (Current_File_System == "vfat")
3067 maxFileSize = 4 * constGB; //4 GB
3068 else if (Current_File_System == "ntfs")
3069 maxFileSize = 256 * constTB; //256 TB
3070 else if (Current_File_System == "exfat")
3071 maxFileSize = 16 * constPB; //16 PB
3072 else if (Current_File_System == "ext3")
3073 maxFileSize = 2 * constTB; //2 TB
3074 else if (Current_File_System == "f2fs")
3075 maxFileSize = 3.94 * constTB; //3.94 TB
bigbiff7cb4c332014-11-26 20:36:07 -05003076 else
3077 maxFileSize = 100000000L;
3078 return maxFileSize - 1;
3079}
3080
bigbiffce8f83c2015-12-12 18:30:21 -05003081bool TWPartition::Flash_Image(PartitionSettings *part_settings) {
3082 string Restore_File_System, full_filename;
Ethan Yonker96af84a2015-01-05 14:58:36 -06003083
Ethan Yonkere080c1f2016-09-19 13:50:25 -05003084 full_filename = part_settings->Backup_Folder + "/" + Backup_FileName;
Ethan Yonker96af84a2015-01-05 14:58:36 -06003085
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05003086 LOGINFO("Image filename is: %s\n", Backup_FileName.c_str());
bigbiffce8f83c2015-12-12 18:30:21 -05003087
3088 if (Backup_Method == BM_FILES) {
Ethan Yonker96af84a2015-01-05 14:58:36 -06003089 LOGERR("Cannot flash images to file systems\n");
3090 return false;
3091 } else if (!Can_Flash_Img) {
3092 LOGERR("Cannot flash images to partitions %s\n", Display_Name.c_str());
3093 return false;
3094 } else {
3095 if (!Find_Partition_Size()) {
3096 LOGERR("Unable to find partition size for '%s'\n", Mount_Point.c_str());
3097 return false;
3098 }
bigbiffce8f83c2015-12-12 18:30:21 -05003099 unsigned long long image_size = TWFunc::Get_File_Size(full_filename);
Ethan Yonker96af84a2015-01-05 14:58:36 -06003100 if (image_size > Size) {
Ethan Yonker74db1572015-10-28 12:44:49 -05003101 LOGINFO("Size (%llu bytes) of image '%s' is larger than target device '%s' (%llu bytes)\n",
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05003102 image_size, Backup_FileName.c_str(), Actual_Block_Device.c_str(), Size);
Ethan Yonker74db1572015-10-28 12:44:49 -05003103 gui_err("img_size_err=Size of image is larger than target device");
Ethan Yonker96af84a2015-01-05 14:58:36 -06003104 return false;
3105 }
bigbiffce8f83c2015-12-12 18:30:21 -05003106 if (Backup_Method == BM_DD) {
3107 if (!part_settings->adbbackup) {
3108 if (Is_Sparse_Image(full_filename)) {
3109 return Flash_Sparse_Image(full_filename);
3110 }
Ethan Yonker472f5062016-02-25 13:47:30 -06003111 }
bigbiffce8f83c2015-12-12 18:30:21 -05003112 return Raw_Read_Write(part_settings);
3113 } else if (Backup_Method == BM_FLASH_UTILS) {
3114 return Flash_Image_FI(full_filename, NULL);
Ethan Yonker472f5062016-02-25 13:47:30 -06003115 }
Ethan Yonker96af84a2015-01-05 14:58:36 -06003116 }
3117
3118 LOGERR("Unknown flash method for '%s'\n", Mount_Point.c_str());
3119 return false;
3120}
3121
Ethan Yonker472f5062016-02-25 13:47:30 -06003122bool TWPartition::Is_Sparse_Image(const string& Filename) {
HashBanged974bb2016-01-30 14:20:09 -05003123 uint32_t magic = 0;
3124 int fd = open(Filename.c_str(), O_RDONLY);
3125 if (fd < 0) {
3126 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Filename)(strerror(errno)));
3127 return false;
3128 }
bigbiffce8f83c2015-12-12 18:30:21 -05003129
HashBanged974bb2016-01-30 14:20:09 -05003130 if (read(fd, &magic, sizeof(magic)) != sizeof(magic)) {
3131 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Filename)(strerror(errno)));
3132 close(fd);
3133 return false;
3134 }
3135 close(fd);
Ethan Yonker472f5062016-02-25 13:47:30 -06003136 if (magic == SPARSE_HEADER_MAGIC)
3137 return true;
3138 return false;
3139}
3140
3141bool TWPartition::Flash_Sparse_Image(const string& Filename) {
3142 string Command;
3143
3144 gui_msg(Msg("flashing=Flashing {1}...")(Display_Name));
3145
3146 Command = "simg2img '" + Filename + "' '" + Actual_Block_Device + "'";
Ethan Yonker96af84a2015-01-05 14:58:36 -06003147 LOGINFO("Flash command: '%s'\n", Command.c_str());
3148 TWFunc::Exec_Cmd(Command);
3149 return true;
3150}
3151
Ethan Yonker472f5062016-02-25 13:47:30 -06003152bool TWPartition::Flash_Image_FI(const string& Filename, ProgressTracking *progress) {
Ethan Yonker96af84a2015-01-05 14:58:36 -06003153 string Command;
Ethan Yonker472f5062016-02-25 13:47:30 -06003154 unsigned long long file_size;
Ethan Yonker96af84a2015-01-05 14:58:36 -06003155
Ethan Yonker74db1572015-10-28 12:44:49 -05003156 gui_msg(Msg("flashing=Flashing {1}...")(Display_Name));
Ethan Yonker472f5062016-02-25 13:47:30 -06003157 if (progress) {
3158 file_size = (unsigned long long)(TWFunc::Get_File_Size(Filename));
3159 progress->SetPartitionSize(file_size);
3160 }
Ethan Yonker96af84a2015-01-05 14:58:36 -06003161 // Sometimes flash image doesn't like to flash due to the first 2KB matching, so we erase first to ensure that it flashes
3162 Command = "erase_image " + MTD_Name;
3163 LOGINFO("Erase command: '%s'\n", Command.c_str());
3164 TWFunc::Exec_Cmd(Command);
3165 Command = "flash_image " + MTD_Name + " '" + Filename + "'";
3166 LOGINFO("Flash command: '%s'\n", Command.c_str());
3167 TWFunc::Exec_Cmd(Command);
Ethan Yonker472f5062016-02-25 13:47:30 -06003168 if (progress)
3169 progress->UpdateSize(file_size);
Ethan Yonker96af84a2015-01-05 14:58:36 -06003170 return true;
3171}
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05003172
3173void TWPartition::Change_Mount_Read_Only(bool new_value) {
3174 Mount_Read_Only = new_value;
3175}
3176
bigbiffce8f83c2015-12-12 18:30:21 -05003177bool TWPartition::Is_Read_Only() {
3178 return Mount_Read_Only;
3179}
3180
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05003181int TWPartition::Check_Lifetime_Writes() {
3182 bool original_read_only = Mount_Read_Only;
3183 int ret = 1;
3184
3185 Mount_Read_Only = true;
3186 if (Mount(false)) {
3187 Find_Actual_Block_Device();
Ethan Yonker54970bb2019-01-17 16:19:24 -06003188 string temp = Actual_Block_Device;
3189 Find_Real_Block_Device(temp, false);
3190 string block = basename(temp.c_str());
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05003191 string file = "/sys/fs/" + Current_File_System + "/" + block + "/lifetime_write_kbytes";
3192 string result;
3193 if (TWFunc::Path_Exists(file)) {
3194 if (TWFunc::read_file(file, result) != 0) {
3195 LOGINFO("Check_Lifetime_Writes of '%s' failed to read_file\n", file.c_str());
3196 } else {
3197 LOGINFO("Check_Lifetime_Writes result: '%s'\n", result.c_str());
3198 if (result == "0") {
3199 ret = 0;
3200 }
3201 }
3202 } else {
3203 LOGINFO("Check_Lifetime_Writes file does not exist '%s'\n", file.c_str());
3204 }
3205 UnMount(true);
3206 } else {
3207 LOGINFO("Check_Lifetime_Writes failed to mount '%s'\n", Mount_Point.c_str());
3208 }
3209 Mount_Read_Only = original_read_only;
3210 return ret;
3211}
Ethan Yonker66a19492015-12-10 10:19:45 -06003212
3213int TWPartition::Decrypt_Adopted() {
3214#ifdef TW_INCLUDE_CRYPTO
3215 int ret = 1;
3216 Is_Adopted_Storage = false;
3217 string Adopted_Key_File = "";
3218
3219 if (!Removable)
3220 return ret;
3221
3222 int fd = open(Alternate_Block_Device.c_str(), O_RDONLY);
3223 if (fd < 0) {
3224 LOGINFO("failed to open '%s'\n", Alternate_Block_Device.c_str());
3225 return ret;
3226 }
3227 char type_guid[80];
3228 char part_guid[80];
3229
3230 if (gpt_disk_get_partition_info(fd, 2, type_guid, part_guid) == 0) {
3231 LOGINFO("type: '%s'\n", type_guid);
3232 LOGINFO("part: '%s'\n", part_guid);
3233 Adopted_GUID = part_guid;
3234 LOGINFO("Adopted_GUID '%s'\n", Adopted_GUID.c_str());
3235 if (strcmp(type_guid, TWGptAndroidExpand) == 0) {
3236 LOGINFO("android_expand found\n");
3237 Adopted_Key_File = "/data/misc/vold/expand_";
3238 Adopted_Key_File += part_guid;
3239 Adopted_Key_File += ".key";
3240 if (TWFunc::Path_Exists(Adopted_Key_File)) {
3241 Is_Adopted_Storage = true;
3242 /* Until we find a use case for this, I think it is safe
3243 * to disable USB Mass Storage whenever adopted storage
3244 * is present.
3245 */
3246 LOGINFO("Detected adopted storage, disabling USB mass storage mode\n");
3247 DataManager::SetValue("tw_has_usb_storage", 0);
3248 }
3249 }
3250 }
3251
3252 if (Is_Adopted_Storage) {
3253 string Adopted_Block_Device = Alternate_Block_Device + "p2";
3254 if (!TWFunc::Path_Exists(Adopted_Block_Device)) {
3255 Adopted_Block_Device = Alternate_Block_Device + "2";
3256 if (!TWFunc::Path_Exists(Adopted_Block_Device)) {
3257 LOGINFO("Adopted block device does not exist\n");
3258 goto exit;
3259 }
3260 }
3261 LOGINFO("key file is '%s', block device '%s'\n", Adopted_Key_File.c_str(), Adopted_Block_Device.c_str());
3262 char crypto_blkdev[MAXPATHLEN];
3263 std::string thekey;
3264 int fdkey = open(Adopted_Key_File.c_str(), O_RDONLY);
3265 if (fdkey < 0) {
3266 LOGINFO("failed to open key file\n");
3267 goto exit;
3268 }
3269 char buf[512];
3270 ssize_t n;
3271 while ((n = read(fdkey, &buf[0], sizeof(buf))) > 0) {
3272 thekey.append(buf, n);
3273 }
3274 close(fdkey);
3275 unsigned char* key = (unsigned char*) thekey.data();
3276 cryptfs_revert_ext_volume(part_guid);
3277
3278 ret = cryptfs_setup_ext_volume(part_guid, Adopted_Block_Device.c_str(), key, thekey.size(), crypto_blkdev);
3279 if (ret == 0) {
3280 LOGINFO("adopted storage new block device: '%s'\n", crypto_blkdev);
3281 Decrypted_Block_Device = crypto_blkdev;
3282 Is_Decrypted = true;
3283 Is_Encrypted = true;
3284 Find_Actual_Block_Device();
Kjell Braden3126a112016-06-19 16:58:15 +00003285 if (!Mount_Storage_Retry(false)) {
Ethan Yonker66a19492015-12-10 10:19:45 -06003286 LOGERR("Failed to mount decrypted adopted storage device\n");
3287 Is_Decrypted = false;
3288 Is_Encrypted = false;
3289 cryptfs_revert_ext_volume(part_guid);
3290 ret = 1;
3291 } else {
Ethan Yonkerfcf3f242016-02-16 12:30:26 -06003292 UnMount(false);
3293 Has_Android_Secure = false;
3294 Symlink_Path = "";
3295 Symlink_Mount_Point = "";
3296 Backup_Name = Mount_Point.substr(1);
3297 Backup_Path = Mount_Point;
3298 TWPartition* sdext = PartitionManager.Find_Partition_By_Path("/sd-ext");
3299 if (sdext && sdext->Actual_Block_Device == Adopted_Block_Device) {
3300 LOGINFO("Removing /sd-ext from partition list due to adopted storage\n");
3301 PartitionManager.Remove_Partition_By_Path("/sd-ext");
3302 }
Ethan Yonker66a19492015-12-10 10:19:45 -06003303 Setup_Data_Media();
3304 Recreate_Media_Folder();
3305 Wipe_Available_in_GUI = true;
3306 Wipe_During_Factory_Reset = true;
3307 Can_Be_Backed_Up = true;
3308 Can_Encrypt_Backup = true;
3309 Use_Userdata_Encryption = true;
3310 Is_Storage = true;
3311 Storage_Name = "Adopted Storage";
3312 Is_SubPartition = true;
3313 SubPartition_Of = "/data";
3314 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
3315 DataManager::SetValue("tw_has_adopted_storage", 1);
3316 }
3317 } else {
3318 LOGERR("Failed to setup adopted storage decryption\n");
3319 }
3320 }
3321exit:
Matt Mower06543e32017-01-06 15:25:26 -06003322 close(fd);
Ethan Yonker66a19492015-12-10 10:19:45 -06003323 return ret;
3324#else
3325 LOGINFO("Decrypt_Adopted: no crypto support\n");
3326 return 1;
3327#endif
3328}
3329
3330void TWPartition::Revert_Adopted() {
3331#ifdef TW_INCLUDE_CRYPTO
3332 if (!Adopted_GUID.empty()) {
3333 PartitionManager.Remove_MTP_Storage(Mount_Point);
3334 UnMount(false);
3335 cryptfs_revert_ext_volume(Adopted_GUID.c_str());
3336 Is_Adopted_Storage = false;
3337 Is_Encrypted = false;
3338 Is_Decrypted = false;
3339 Decrypted_Block_Device = "";
3340 Find_Actual_Block_Device();
3341 Wipe_During_Factory_Reset = false;
3342 Can_Be_Backed_Up = false;
3343 Can_Encrypt_Backup = false;
3344 Use_Userdata_Encryption = false;
3345 Is_SubPartition = false;
3346 SubPartition_Of = "";
3347 Has_Data_Media = false;
3348 Storage_Path = Mount_Point;
3349 if (!Symlink_Mount_Point.empty()) {
3350 TWPartition* Dat = PartitionManager.Find_Partition_By_Path("/data");
3351 if (Dat) {
3352 Dat->UnMount(false);
3353 Dat->Symlink_Mount_Point = Symlink_Mount_Point;
3354 }
3355 Symlink_Mount_Point = "";
3356 }
3357 }
3358#else
3359 LOGINFO("Revert_Adopted: no crypto support\n");
3360#endif
3361}
bigbiff bigbiffb5ecaad2017-03-20 18:53:53 -04003362
3363void TWPartition::Set_Backup_FileName(string fname) {
3364 Backup_FileName = fname;
3365}
3366
3367string TWPartition::Get_Backup_Name() {
3368 return Backup_Name;
3369}