blob: 7f4189a638dd077739bf270f48678fec8c69440b [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;
bigbiffadc599e2020-05-28 19:36:30 +0000654 if (Key_Directory.empty())
Ethan Yonker93382822018-11-01 15:25:31 -0500655 Is_FBE = false;
bigbiffadc599e2020-05-28 19:36:30 +0000656 else
Ethan Yonker93382822018-11-01 15:25:31 -0500657 Is_FBE = true;
bigbiffadc599e2020-05-28 19:36:30 +0000658 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()) {
bigbiffadc599e2020-05-28 19:36:30 +0000664 set_partition_data(Actual_Block_Device.c_str(), Crypto_Key_Location.c_str(), Fstab_File_System.c_str());
Ethan Yonker93382822018-11-01 15:25:31 -0500665 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()) {
bigbiffadc599e2020-05-28 19:36:30 +0000683 LOGERR("Primary block device '%s' for mount point '%s' is not present!\n", Primary_Block_Device.c_str(), Mount_Point.c_str());
Matt Mower72c87ce2016-04-26 14:34:56 -0500684 }
685 } else {
bigbiffadc599e2020-05-28 19:36:30 +0000686
mauronofrio9bf73352019-11-23 22:27:34 +0100687 if (!Decrypt_FBE_DE()) {
mauronofriofc79aa62019-11-23 23:13:04 +0100688 char wrappedvalue[PROPERTY_VALUE_MAX];
689 property_get("fbe.data.wrappedkey", wrappedvalue, "");
690 std::string wrappedkeyvalue(wrappedvalue);
691 if (wrappedkeyvalue == "true") {
692 LOGERR("Unable to decrypt FBE device\n");
693 } else {
694 LOGINFO("Trying wrapped key.\n");
695 property_set("fbe.data.wrappedkey", "true");
mauronofrio9bf73352019-11-23 22:27:34 +0100696 if (!Decrypt_FBE_DE()) {
bigbiffd58ba182020-03-23 10:02:29 -0400697 LOGERR("Unable to decrypt FBE device\n");
mauronofrio9bf73352019-11-23 22:27:34 +0100698 }
mauronofriofc79aa62019-11-23 23:13:04 +0100699 }
mauronofrio9bf73352019-11-23 22:27:34 +0100700 }
Matt Mower72c87ce2016-04-26 14:34:56 -0500701 }
702 if (datamedia && (!Is_Encrypted || (Is_Encrypted && Is_Decrypted))) {
703 Setup_Data_Media();
704 Recreate_Media_Folder();
705 }
706#else
707 if (datamedia) {
708 Setup_Data_Media();
709 Recreate_Media_Folder();
710 }
711#endif
712}
713
Ethan Yonker93382822018-11-01 15:25:31 -0500714bool TWPartition::Decrypt_FBE_DE() {
715if (TWFunc::Path_Exists("/data/unencrypted/key/version")) {
716 LOGINFO("File Based Encryption is present\n");
717#ifdef TW_INCLUDE_FBE
bigbiffadc599e2020-05-28 19:36:30 +0000718 ExcludeAll(Mount_Point + "/convert_fbe");
719 ExcludeAll(Mount_Point + "/unencrypted");
720 //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
721 ExcludeAll(Mount_Point + "/misc/vold/user_keys");
722 //ExcludeAll(Mount_Point + "/system_ce");
723 //ExcludeAll(Mount_Point + "/system_de");
724 //ExcludeAll(Mount_Point + "/misc_ce");
725 //ExcludeAll(Mount_Point + "/misc_de");
726 ExcludeAll(Mount_Point + "/system/gatekeeper.password.key");
727 ExcludeAll(Mount_Point + "/system/gatekeeper.pattern.key");
728 ExcludeAll(Mount_Point + "/system/locksettings.db");
729 //ExcludeAll(Mount_Point + "/system/locksettings.db-shm"); // don't seem to need this one, but the other 2 are needed
730 ExcludeAll(Mount_Point + "/system/locksettings.db-wal");
731 //ExcludeAll(Mount_Point + "/user_de");
732 //ExcludeAll(Mount_Point + "/misc/profiles/cur/0"); // might be important later
733 ExcludeAll(Mount_Point + "/misc/gatekeeper");
734 ExcludeAll(Mount_Point + "/misc/keystore");
735 ExcludeAll(Mount_Point + "/drm/kek.dat");
736 ExcludeAll(Mount_Point + "/system_de/0/spblob"); // contains data needed to decrypt pixel 2
737 int retry_count = 3;
738 while (!Decrypt_DE() && --retry_count)
739 usleep(2000);
740 if (retry_count > 0) {
741 property_set("ro.crypto.state", "encrypted");
742 Is_Encrypted = true;
743 Is_Decrypted = false;
744 Is_FBE = true;
745 DataManager::SetValue(TW_IS_FBE, 1);
746 DataManager::SetValue(TW_IS_ENCRYPTED, 1);
747 string filename;
748 int pwd_type = Get_Password_Type(0, filename);
749 if (pwd_type < 0) {
750 LOGERR("This TWRP does not have synthetic password decrypt support\n");
751 pwd_type = 0; // default password
752 }
753 DataManager::SetValue(TW_CRYPTO_PWTYPE, pwd_type);
754 DataManager::SetValue(TW_CRYPTO_PASSWORD, "");
755 DataManager::SetValue("tw_crypto_display", "");
756 return true;
Ethan Yonker93382822018-11-01 15:25:31 -0500757 }
758#else
759 LOGERR("FBE found but FBE support not present in TWRP\n");
760#endif
761 }
762 return false;
763}
764
Matt Mower72c87ce2016-04-26 14:34:56 -0500765void TWPartition::Setup_Cache_Partition(bool Display_Error __unused) {
766 if (Mount_Point != "/cache")
767 return;
768
769 if (!Mount(true))
770 return;
771
772 if (!TWFunc::Path_Exists("/cache/recovery/.")) {
773 LOGINFO("Recreating /cache/recovery folder\n");
774 if (mkdir("/cache/recovery", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0)
775 LOGERR("Could not create /cache/recovery\n");
776 }
777}
778
Matt Mower4ab42b12016-04-21 13:52:18 -0500779void TWPartition::Process_FS_Flags(const char *str) {
780 char *options = strdup(str);
781 char *ptr, *savep;
Hashcode62bd9e02013-11-19 21:59:42 -0800782
Matt Mower4ab42b12016-04-21 13:52:18 -0500783 Mount_Options = "";
Hashcode62bd9e02013-11-19 21:59:42 -0800784
Matt Mower4ab42b12016-04-21 13:52:18 -0500785 // Avoid issues with potentially nested strtok by using strtok_r
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600786 for (ptr = strtok_r(options, ",", &savep); ptr; ptr = strtok_r(NULL, ",", &savep)) {
787 char *equals = strstr(ptr, "=");
788 size_t name_len;
Matt Mower4ab42b12016-04-21 13:52:18 -0500789
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600790 if (!equals)
791 name_len = strlen(ptr);
792 else
793 name_len = equals - ptr;
794
795 // There are some flags that we want to ignore in TWRP
796 bool found_match = false;
797 for (const char** ignored_mount_item = ignored_mount_items; *ignored_mount_item; ignored_mount_item++) {
798 if (strncmp(ptr, *ignored_mount_item, name_len) == 0) {
799 found_match = true;
Hashcode62bd9e02013-11-19 21:59:42 -0800800 break;
801 }
802 }
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600803 if (found_match)
804 continue;
Hashcode62bd9e02013-11-19 21:59:42 -0800805
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600806 // mount_flags are never postfixed by '='
807 if (!equals) {
808 const struct flag_list* mount_flag = mount_flags;
809 for (; mount_flag->name; mount_flag++) {
810 if (strcmp(ptr, mount_flag->name) == 0) {
811 if (mount_flag->flag == MS_RDONLY)
812 Mount_Read_Only = true;
813 else
814 Mount_Flags |= (unsigned)mount_flag->flag;
815 found_match = true;
816 break;
817 }
818 }
819 if (found_match)
820 continue;
Matt Mower4ab42b12016-04-21 13:52:18 -0500821 }
822
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600823 // If we aren't ignoring this flag and it's not a mount flag, then it must be a mount option
824 if (!Mount_Options.empty())
825 Mount_Options += ",";
826 Mount_Options += ptr;
Matt Mower4ab42b12016-04-21 13:52:18 -0500827 }
828 free(options);
Hashcode62bd9e02013-11-19 21:59:42 -0800829}
830
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600831void TWPartition::Save_FS_Flags(const string& local_File_System, int local_Mount_Flags, const string& local_Mount_Options) {
832 partition_fs_flags_struct flags;
833 flags.File_System = local_File_System;
834 flags.Mount_Flags = local_Mount_Flags;
835 flags.Mount_Options = local_Mount_Options;
836 fs_flags.push_back(flags);
837}
838
Matt Mower2416a502016-04-12 19:54:46 -0500839void TWPartition::Apply_TW_Flag(const unsigned flag, const char* str, const bool val) {
840 switch (flag) {
841 case TWFLAG_ANDSEC:
842 Has_Android_Secure = val;
843 break;
844 case TWFLAG_BACKUP:
845 Can_Be_Backed_Up = val;
846 break;
847 case TWFLAG_BACKUPNAME:
848 Backup_Display_Name = str;
849 break;
850 case TWFLAG_BLOCKSIZE:
851 Format_Block_Size = (unsigned long)(atol(str));
852 break;
853 case TWFLAG_CANBEWIPED:
854 Can_Be_Wiped = val;
855 break;
856 case TWFLAG_CANENCRYPTBACKUP:
857 Can_Encrypt_Backup = val;
858 break;
859 case TWFLAG_DEFAULTS:
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600860 case TWFLAG_WAIT:
861 case TWFLAG_VERIFY:
862 case TWFLAG_CHECK:
863 case TWFLAG_NOTRIM:
864 case TWFLAG_VOLDMANAGED:
865 case TWFLAG_RESIZE:
Matt Mower2416a502016-04-12 19:54:46 -0500866 // Do nothing
867 break;
868 case TWFLAG_DISPLAY:
869 Display_Name = str;
870 break;
871 case TWFLAG_ENCRYPTABLE:
872 case TWFLAG_FORCEENCRYPT:
873 Crypto_Key_Location = str;
874 break;
Ethan Yonkere9afc3d2018-08-30 15:16:27 -0500875 case TWFLAG_FILEENCRYPTION:
876 // This flag isn't used by TWRP but is needed in 9.0 FBE decrypt
877 // fileencryption=ice:aes-256-heh
878 {
879 std::string FBE = str;
Ethan Yonkere9afc3d2018-08-30 15:16:27 -0500880 size_t colon_loc = FBE.find(":");
881 if (colon_loc == std::string::npos) {
Ethan Yonker93382822018-11-01 15:25:31 -0500882 property_set("fbe.contents", FBE.c_str());
883 property_set("fbe.filenames", "");
884 LOGINFO("FBE contents '%s', filenames ''\n", FBE.c_str());
Ethan Yonkere9afc3d2018-08-30 15:16:27 -0500885 break;
886 }
Ethan Yonker93382822018-11-01 15:25:31 -0500887 std::string FBE_contents, FBE_filenames;
Ethan Yonkere9afc3d2018-08-30 15:16:27 -0500888 FBE_contents = FBE.substr(0, colon_loc);
889 FBE_filenames = FBE.substr(colon_loc + 1);
890 property_set("fbe.contents", FBE_contents.c_str());
891 property_set("fbe.filenames", FBE_filenames.c_str());
892 LOGINFO("FBE contents '%s', filenames '%s'\n", FBE_contents.c_str(), FBE_filenames.c_str());
893 }
894 break;
Peter Cai28ea7b02019-05-24 11:38:54 +0800895 case TWFLAG_WRAPPEDKEY:
896 // Set fbe.data.wrappedkey to true
897 {
898 property_set("fbe.data.wrappedkey", "true");
899 LOGINFO("FBE wrapped key enabled\n");
900 }
901 break;
Matt Mower2416a502016-04-12 19:54:46 -0500902 case TWFLAG_FLASHIMG:
903 Can_Flash_Img = val;
904 break;
905 case TWFLAG_FSFLAGS:
Matt Mower4ab42b12016-04-21 13:52:18 -0500906 Process_FS_Flags(str);
Matt Mower2416a502016-04-12 19:54:46 -0500907 break;
908 case TWFLAG_IGNOREBLKID:
909 Ignore_Blkid = val;
910 break;
911 case TWFLAG_LENGTH:
912 Length = atoi(str);
913 break;
914 case TWFLAG_MOUNTTODECRYPT:
915 Mount_To_Decrypt = val;
916 break;
917 case TWFLAG_REMOVABLE:
918 Removable = val;
919 break;
920 case TWFLAG_RETAINLAYOUTVERSION:
921 Retain_Layout_Version = val;
922 break;
923 case TWFLAG_SETTINGSSTORAGE:
924 Is_Settings_Storage = val;
925 if (Is_Settings_Storage)
926 Is_Storage = true;
927 break;
928 case TWFLAG_STORAGE:
929 Is_Storage = val;
930 break;
931 case TWFLAG_STORAGENAME:
932 Storage_Name = str;
933 break;
934 case TWFLAG_SUBPARTITIONOF:
935 Is_SubPartition = true;
936 SubPartition_Of = str;
937 break;
938 case TWFLAG_SYMLINK:
939 Symlink_Path = str;
940 break;
941 case TWFLAG_USERDATAENCRYPTBACKUP:
942 Use_Userdata_Encryption = val;
943 if (Use_Userdata_Encryption)
944 Can_Encrypt_Backup = true;
945 break;
946 case TWFLAG_USERMRF:
947 Use_Rm_Rf = val;
948 break;
949 case TWFLAG_WIPEDURINGFACTORYRESET:
950 Wipe_During_Factory_Reset = val;
951 if (Wipe_During_Factory_Reset) {
952 Can_Be_Wiped = true;
953 Wipe_Available_in_GUI = true;
954 }
955 break;
956 case TWFLAG_WIPEINGUI:
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600957 case TWFLAG_FORMATTABLE:
Matt Mower2416a502016-04-12 19:54:46 -0500958 Wipe_Available_in_GUI = val;
959 if (Wipe_Available_in_GUI)
960 Can_Be_Wiped = true;
961 break;
Ethan Yonker1b190162016-12-05 15:25:19 -0600962 case TWFLAG_SLOTSELECT:
963 SlotSelect = true;
964 break;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600965 case TWFLAG_ALTDEVICE:
966 Alternate_Block_Device = str;
967 break;
Ethan Yonker93382822018-11-01 15:25:31 -0500968 case TWFLAG_KEYDIRECTORY:
969 Key_Directory = str;
bigbiffd58ba182020-03-23 10:02:29 -0400970 break;
Matt Mower2416a502016-04-12 19:54:46 -0500971 default:
972 // Should not get here
973 LOGINFO("Flag identified for processing, but later unmatched: %i\n", flag);
974 break;
975 }
976}
Dees_Troy51127312012-09-08 13:08:49 -0400977
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600978void TWPartition::Process_TW_Flags(char *flags, bool Display_Error, int fstab_ver) {
Matt Mower2416a502016-04-12 19:54:46 -0500979 char separator[2] = {'\n', 0};
980 char *ptr, *savep;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600981 char source_separator = ';';
982
983 if (fstab_ver == 2)
984 source_separator = ',';
Matt Mower2416a502016-04-12 19:54:46 -0500985
986 // Semicolons within double-quotes are not forbidden, so replace
987 // only the semicolons intended as separators with '\n' for strtok
988 for (unsigned i = 0, skip = 0; i < strlen(flags); i++) {
989 if (flags[i] == '\"')
Dees_Troya13d74f2013-03-24 08:54:55 -0500990 skip = !skip;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600991 if (!skip && flags[i] == source_separator)
Matt Mower2416a502016-04-12 19:54:46 -0500992 flags[i] = separator[0];
Dees_Troy51127312012-09-08 13:08:49 -0400993 }
994
Matt Mower2416a502016-04-12 19:54:46 -0500995 // Avoid issues with potentially nested strtok by using strtok_r
996 ptr = strtok_r(flags, separator, &savep);
997 while (ptr) {
998 int ptr_len = strlen(ptr);
999 const struct flag_list* tw_flag = tw_flags;
Hashcode62bd9e02013-11-19 21:59:42 -08001000
Matt Mower2416a502016-04-12 19:54:46 -05001001 for (; tw_flag->name; tw_flag++) {
1002 int flag_len = strlen(tw_flag->name);
1003
1004 if (strncmp(ptr, tw_flag->name, flag_len) == 0) {
1005 bool flag_val = false;
1006
1007 if (ptr_len > flag_len && (tw_flag->name)[flag_len-1] != '='
1008 && ptr[flag_len] != '=') {
1009 // Handle flags with same starting string
1010 // (e.g. backup and backupname)
1011 continue;
1012 } else if (ptr_len > flag_len && ptr[flag_len] == '=') {
1013 // Handle flags with dual format: Part 1
1014 // (e.g. backup and backup=y. backup=y handled here)
1015 ptr += flag_len + 1;
1016 TWFunc::Strip_Quotes(ptr);
1017 // Skip flags with empty argument
1018 // (e.g. backup=)
1019 if (strlen(ptr) == 0) {
1020 LOGINFO("Flag missing argument or should not include '=': %s=\n", tw_flag->name);
1021 break;
1022 }
1023 flag_val = strchr("yY1", *ptr) != NULL;
1024 } else if (ptr_len == flag_len
1025 && (tw_flag->name)[flag_len-1] == '=') {
1026 // Skip flags missing argument after =
1027 // (e.g. backupname=)
1028 LOGINFO("Flag missing argument: %s\n", tw_flag->name);
1029 break;
1030 } else if (ptr_len > flag_len
1031 && (tw_flag->name)[flag_len-1] == '=') {
1032 // Handle arguments to flags
1033 // (e.g. backupname="My Stuff")
1034 ptr += flag_len;
1035 TWFunc::Strip_Quotes(ptr);
1036 // Skip flags with empty argument
1037 // (e.g. backupname="")
1038 if (strlen(ptr) == 0) {
1039 LOGINFO("Flag missing argument: %s\n", tw_flag->name);
1040 break;
1041 }
1042 } else if (ptr_len == flag_len) {
1043 // Handle flags with dual format: Part 2
1044 // (e.g. backup and backup=y. backup handled here)
1045 flag_val = true;
Ethan Yonker96af84a2015-01-05 14:58:36 -06001046 } else {
Matt Mower2416a502016-04-12 19:54:46 -05001047 LOGINFO("Flag matched, but could not be processed: %s\n", ptr);
1048 break;
Ethan Yonker96af84a2015-01-05 14:58:36 -06001049 }
Matt Mower2416a502016-04-12 19:54:46 -05001050
1051 Apply_TW_Flag(tw_flag->flag, ptr, flag_val);
1052 break;
Ethan Yonker96af84a2015-01-05 14:58:36 -06001053 }
Matt Mower2416a502016-04-12 19:54:46 -05001054 }
1055 if (tw_flag->name == 0) {
Dees_Troy51127312012-09-08 13:08:49 -04001056 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001057 LOGERR("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -04001058 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001059 LOGINFO("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -04001060 }
Matt Mower2416a502016-04-12 19:54:46 -05001061 ptr = strtok_r(NULL, separator, &savep);
Dees_Troy51127312012-09-08 13:08:49 -04001062 }
Dees_Troy51a0e822012-09-05 15:24:24 -04001063}
1064
Dees_Troy5bf43922012-09-07 16:07:55 -04001065bool TWPartition::Is_File_System(string File_System) {
1066 if (File_System == "ext2" ||
Dees_Troy63c8df72012-09-10 14:02:05 -04001067 File_System == "ext3" ||
Dees_Troy5bf43922012-09-07 16:07:55 -04001068 File_System == "ext4" ||
1069 File_System == "vfat" ||
1070 File_System == "ntfs" ||
1071 File_System == "yaffs2" ||
bigbiff bigbiff3e146522012-11-14 14:32:59 -05001072 File_System == "exfat" ||
Dees_Troye5017042013-08-29 16:38:55 +00001073 File_System == "f2fs" ||
Erfan Abdib859e912019-02-05 18:18:46 +02001074 File_System == "squashfs" ||
Dees_Troy5bf43922012-09-07 16:07:55 -04001075 File_System == "auto")
1076 return true;
1077 else
1078 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001079}
1080
Dees_Troy5bf43922012-09-07 16:07:55 -04001081bool TWPartition::Is_Image(string File_System) {
Dees_Troy5fcd8f92012-10-16 12:22:05 -04001082 if (File_System == "emmc" || File_System == "mtd" || File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -04001083 return true;
1084 else
1085 return false;
1086}
1087
Dees_Troy51127312012-09-08 13:08:49 -04001088bool TWPartition::Make_Dir(string Path, bool Display_Error) {
Ethan Yonker93ac7a02016-11-07 22:05:58 -06001089 if (TWFunc::Get_D_Type_From_Stat(Path) != S_IFDIR)
1090 unlink(Path.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001091 if (!TWFunc::Path_Exists(Path)) {
Dees_Troy51127312012-09-08 13:08:49 -04001092 if (mkdir(Path.c_str(), 0777) == -1) {
1093 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001094 gui_msg(Msg(msg::kError, "create_folder_strerr=Can not create '{1}' folder ({2}).")(Path)(strerror(errno)));
Dees_Troy51127312012-09-08 13:08:49 -04001095 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001096 LOGINFO("Can not create '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -04001097 return false;
1098 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001099 LOGINFO("Created '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -04001100 return true;
1101 }
1102 }
1103 return true;
1104}
1105
Dees_Troy5bf43922012-09-07 16:07:55 -04001106void TWPartition::Setup_File_System(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001107 Can_Be_Mounted = true;
1108 Can_Be_Wiped = true;
1109
Dees_Troy5bf43922012-09-07 16:07:55 -04001110 // Make the mount point folder if it doesn't exist
Dees_Troy51127312012-09-08 13:08:49 -04001111 Make_Dir(Mount_Point, Display_Error);
bigbiffce8f83c2015-12-12 18:30:21 -05001112 Backup_Method = BM_FILES;
Dees_Troy5bf43922012-09-07 16:07:55 -04001113}
1114
Ethan Yonker1b190162016-12-05 15:25:19 -06001115void TWPartition::Setup_Image() {
Dees_Troy5bf43922012-09-07 16:07:55 -04001116 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
1117 Backup_Name = Display_Name;
Gary Peck82599a82012-11-21 16:23:12 -08001118 if (Current_File_System == "emmc")
bigbiffce8f83c2015-12-12 18:30:21 -05001119 Backup_Method = BM_DD;
Gary Peck82599a82012-11-21 16:23:12 -08001120 else if (Current_File_System == "mtd" || Current_File_System == "bml")
bigbiffce8f83c2015-12-12 18:30:21 -05001121 Backup_Method = BM_FLASH_UTILS;
Dees_Troy5bf43922012-09-07 16:07:55 -04001122 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001123 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 -04001124}
1125
Dees_Troye58d5262012-09-21 12:27:57 -04001126void TWPartition::Setup_AndSec(void) {
Dees_Troya13d74f2013-03-24 08:54:55 -05001127 Backup_Display_Name = "Android Secure";
Dees_Troye58d5262012-09-21 12:27:57 -04001128 Backup_Name = "and-sec";
Dees_Troya13d74f2013-03-24 08:54:55 -05001129 Can_Be_Backed_Up = true;
Dees_Troye58d5262012-09-21 12:27:57 -04001130 Has_Android_Secure = true;
1131 Symlink_Path = Mount_Point + "/.android_secure";
1132 Symlink_Mount_Point = "/and-sec";
1133 Backup_Path = Symlink_Mount_Point;
1134 Make_Dir("/and-sec", true);
1135 Recreate_AndSec_Folder();
Kjell Braden3126a112016-06-19 16:58:15 +00001136 Mount_Storage_Retry(true);
Dees_Troye58d5262012-09-21 12:27:57 -04001137}
1138
that9e0593e2014-10-08 00:01:24 +02001139void TWPartition::Setup_Data_Media() {
Ethan Yonker6277c792014-09-15 14:54:30 -05001140 LOGINFO("Setting up '%s' as data/media emulated storage.\n", Mount_Point.c_str());
Ethan Yonker66a19492015-12-10 10:19:45 -06001141 if (Storage_Name.empty() || Storage_Name == "Data")
1142 Storage_Name = "Internal Storage";
Ethan Yonker6277c792014-09-15 14:54:30 -05001143 Has_Data_Media = true;
1144 Is_Storage = true;
Ethan Yonker66a19492015-12-10 10:19:45 -06001145 Storage_Path = Mount_Point + "/media";
Ethan Yonker6277c792014-09-15 14:54:30 -05001146 Symlink_Path = Storage_Path;
Ethan Yonker66a19492015-12-10 10:19:45 -06001147 if (Mount_Point == "/data") {
1148 Is_Settings_Storage = true;
1149 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
1150 Make_Dir("/emmc", false);
1151 Symlink_Mount_Point = "/emmc";
1152 } else {
1153 Make_Dir("/sdcard", false);
1154 Symlink_Mount_Point = "/sdcard";
1155 }
1156 if (Mount(false) && TWFunc::Path_Exists(Mount_Point + "/media/0")) {
1157 Storage_Path = Mount_Point + "/media/0";
1158 Symlink_Path = Storage_Path;
1159 DataManager::SetValue(TW_INTERNAL_PATH, Mount_Point + "/media/0");
1160 UnMount(true);
1161 }
1162 DataManager::SetValue("tw_has_internal", 1);
1163 DataManager::SetValue("tw_has_data_media", 1);
Ethan Yonker3fdcda42016-11-30 12:29:37 -06001164 backup_exclusions.add_absolute_dir("/data/data/com.google.android.music/files");
Ethan Yonker6355b562017-05-01 09:42:17 -05001165 wipe_exclusions.add_absolute_dir(Mount_Point + "/misc/vold"); // adopted storage keys
Ethan Yonker79f88bd2016-12-09 14:52:12 -06001166 ExcludeAll(Mount_Point + "/.layout_version");
1167 ExcludeAll(Mount_Point + "/system/storage.xml");
Ethan Yonker6277c792014-09-15 14:54:30 -05001168 } else {
Ethan Yonker66a19492015-12-10 10:19:45 -06001169 if (Mount(true) && TWFunc::Path_Exists(Mount_Point + "/media/0")) {
1170 Storage_Path = Mount_Point + "/media/0";
1171 Symlink_Path = Storage_Path;
1172 UnMount(true);
1173 }
Ethan Yonker6277c792014-09-15 14:54:30 -05001174 }
Ethan Yonker79f88bd2016-12-09 14:52:12 -06001175 ExcludeAll(Mount_Point + "/media");
Ethan Yonker6277c792014-09-15 14:54:30 -05001176}
1177
Dees_Troy5bf43922012-09-07 16:07:55 -04001178void TWPartition::Find_Real_Block_Device(string& Block, bool Display_Error) {
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06001179 char device[PATH_MAX], realDevice[PATH_MAX];
Dees_Troy5bf43922012-09-07 16:07:55 -04001180
1181 strcpy(device, Block.c_str());
1182 memset(realDevice, 0, sizeof(realDevice));
1183 while (readlink(device, realDevice, sizeof(realDevice)) > 0)
1184 {
1185 strcpy(device, realDevice);
1186 memset(realDevice, 0, sizeof(realDevice));
1187 }
1188
1189 if (device[0] != '/') {
1190 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001191 LOGERR("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001192 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001193 LOGINFO("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001194 return;
Dees_Troy51a0e822012-09-05 15:24:24 -04001195 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -04001196 Block = device;
1197 return;
1198 }
1199}
1200
Kjell Braden3126a112016-06-19 16:58:15 +00001201bool TWPartition::Mount_Storage_Retry(bool Display_Error) {
Dees_Troy8e337f32012-10-13 22:07:49 -04001202 // On some devices, storage doesn't want to mount right away, retry and sleep
Kjell Braden3126a112016-06-19 16:58:15 +00001203 if (!Mount(Display_Error)) {
Dees_Troy8e337f32012-10-13 22:07:49 -04001204 int retry_count = 5;
1205 while (retry_count > 0 && !Mount(false)) {
1206 usleep(500000);
1207 retry_count--;
1208 }
Kjell Braden3126a112016-06-19 16:58:15 +00001209 return Mount(Display_Error);
Dees_Troy8e337f32012-10-13 22:07:49 -04001210 }
Kjell Braden3126a112016-06-19 16:58:15 +00001211 return true;
Dees_Troy8e337f32012-10-13 22:07:49 -04001212}
1213
Dees_Troy38bd7602012-09-14 13:33:53 -04001214bool TWPartition::Find_MTD_Block_Device(string MTD_Name) {
1215 FILE *fp = NULL;
1216 char line[255];
1217
1218 fp = fopen("/proc/mtd", "rt");
1219 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001220 LOGERR("Device does not support /proc/mtd\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001221 return false;
1222 }
1223
1224 while (fgets(line, sizeof(line), fp) != NULL)
1225 {
1226 char device[32], label[32];
1227 unsigned long size = 0;
Dees_Troy38bd7602012-09-14 13:33:53 -04001228 int deviceId;
1229
1230 sscanf(line, "%s %lx %*s %*c%s", device, &size, label);
1231
1232 // Skip header and blank lines
1233 if ((strcmp(device, "dev:") == 0) || (strlen(line) < 8))
1234 continue;
1235
1236 // Strip off the trailing " from the label
1237 label[strlen(label)-1] = '\0';
1238
1239 if (strcmp(label, MTD_Name.c_str()) == 0) {
1240 // We found our device
1241 // Strip off the trailing : from the device
1242 device[strlen(device)-1] = '\0';
1243 if (sscanf(device,"mtd%d", &deviceId) == 1) {
1244 sprintf(device, "/dev/block/mtdblock%d", deviceId);
1245 Primary_Block_Device = device;
Dees_Troy76543db2013-06-19 16:24:30 +00001246 fclose(fp);
1247 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -04001248 }
1249 }
1250 }
1251 fclose(fp);
1252
1253 return false;
1254}
1255
Dees_Troy51127312012-09-08 13:08:49 -04001256bool TWPartition::Get_Size_Via_statfs(bool Display_Error) {
1257 struct statfs st;
1258 string Local_Path = Mount_Point + "/.";
1259
1260 if (!Mount(Display_Error))
1261 return false;
1262
1263 if (statfs(Local_Path.c_str(), &st) != 0) {
1264 if (!Removable) {
1265 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001266 LOGERR("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -04001267 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001268 LOGINFO("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -04001269 }
1270 return false;
1271 }
1272 Size = (st.f_blocks * st.f_bsize);
1273 Used = ((st.f_blocks - st.f_bfree) * st.f_bsize);
1274 Free = (st.f_bfree * st.f_bsize);
1275 Backup_Size = Used;
1276 return true;
1277}
1278
1279bool TWPartition::Get_Size_Via_df(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001280 FILE* fp;
1281 char command[255], line[512];
1282 int include_block = 1;
1283 unsigned int min_len;
1284
1285 if (!Mount(Display_Error))
1286 return false;
1287
Dees_Troy38bd7602012-09-14 13:33:53 -04001288 min_len = Actual_Block_Device.size() + 2;
Dees_Troy51127312012-09-08 13:08:49 -04001289 sprintf(command, "df %s > /tmp/dfoutput.txt", Mount_Point.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001290 TWFunc::Exec_Cmd(command);
Dees_Troy51127312012-09-08 13:08:49 -04001291 fp = fopen("/tmp/dfoutput.txt", "rt");
1292 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001293 LOGINFO("Unable to open /tmp/dfoutput.txt.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001294 return false;
Dees_Troy51127312012-09-08 13:08:49 -04001295 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001296
1297 while (fgets(line, sizeof(line), fp) != NULL)
1298 {
1299 unsigned long blocks, used, available;
1300 char device[64];
1301 char tmpString[64];
1302
1303 if (strncmp(line, "Filesystem", 10) == 0)
1304 continue;
1305 if (strlen(line) < min_len) {
1306 include_block = 0;
1307 continue;
1308 }
1309 if (include_block) {
1310 sscanf(line, "%s %lu %lu %lu", device, &blocks, &used, &available);
1311 } else {
1312 // The device block string is so long that the df information is on the next line
1313 int space_count = 0;
Dees_Troye58d5262012-09-21 12:27:57 -04001314 sprintf(tmpString, "/dev/block/%s", Actual_Block_Device.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001315 while (tmpString[space_count] == 32)
1316 space_count++;
1317 sscanf(line + space_count, "%lu %lu %lu", &blocks, &used, &available);
1318 }
1319
1320 // Adjust block size to byte size
1321 Size = blocks * 1024ULL;
1322 Used = used * 1024ULL;
1323 Free = available * 1024ULL;
1324 Backup_Size = Used;
1325 }
1326 fclose(fp);
1327 return true;
1328}
1329
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001330unsigned long long TWPartition::IOCTL_Get_Block_Size() {
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001331 Find_Actual_Block_Device();
Ethan Yonker483e9f42016-01-11 22:21:18 -06001332
1333 return TWFunc::IOCTL_Get_Block_Size(Actual_Block_Device.c_str());
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001334}
1335
Dees_Troy5bf43922012-09-07 16:07:55 -04001336bool TWPartition::Find_Partition_Size(void) {
1337 FILE* fp;
1338 char line[512];
1339 string tmpdevice;
1340
igoriok87e3d932013-01-31 21:03:53 +02001341 fp = fopen("/proc/dumchar_info", "rt");
1342 if (fp != NULL) {
1343 while (fgets(line, sizeof(line), fp) != NULL)
1344 {
1345 char label[32], device[32];
1346 unsigned long size = 0;
1347
thatd43bf2d2014-09-21 23:13:02 +02001348 sscanf(line, "%s %lx %*x %*u %s", label, &size, device);
igoriok87e3d932013-01-31 21:03:53 +02001349
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05001350 // Skip header, annotation and blank lines
igoriok87e3d932013-01-31 21:03:53 +02001351 if ((strncmp(device, "/dev/", 5) != 0) || (strlen(line) < 8))
1352 continue;
1353
1354 tmpdevice = "/dev/";
1355 tmpdevice += label;
1356 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
1357 Size = size;
1358 fclose(fp);
1359 return true;
1360 }
1361 }
1362 }
1363
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001364 unsigned long long ioctl_size = IOCTL_Get_Block_Size();
1365 if (ioctl_size) {
1366 Size = ioctl_size;
1367 return true;
1368 }
1369
Dees_Troy5bf43922012-09-07 16:07:55 -04001370 // In this case, we'll first get the partitions we care about (with labels)
1371 fp = fopen("/proc/partitions", "rt");
1372 if (fp == NULL)
1373 return false;
1374
1375 while (fgets(line, sizeof(line), fp) != NULL)
1376 {
1377 unsigned long major, minor, blocks;
1378 char device[512];
Dees_Troy5bf43922012-09-07 16:07:55 -04001379
Dees_Troy63c8df72012-09-10 14:02:05 -04001380 if (strlen(line) < 7 || line[0] == 'm') continue;
Dees_Troy5bf43922012-09-07 16:07:55 -04001381 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
1382
1383 tmpdevice = "/dev/block/";
1384 tmpdevice += device;
Dees_Troy38bd7602012-09-14 13:33:53 -04001385 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001386 // Adjust block size to byte size
1387 Size = blocks * 1024ULL;
1388 fclose(fp);
1389 return true;
1390 }
1391 }
1392 fclose(fp);
1393 return false;
1394}
1395
Dees_Troy5bf43922012-09-07 16:07:55 -04001396bool TWPartition::Is_Mounted(void) {
1397 if (!Can_Be_Mounted)
1398 return false;
1399
1400 struct stat st1, st2;
1401 string test_path;
1402
1403 // Check to see if the mount point directory exists
1404 test_path = Mount_Point + "/.";
1405 if (stat(test_path.c_str(), &st1) != 0) return false;
1406
1407 // Check to see if the directory above the mount point exists
1408 test_path = Mount_Point + "/../.";
1409 if (stat(test_path.c_str(), &st2) != 0) return false;
1410
1411 // Compare the device IDs -- if they match then we're (probably) using tmpfs instead of an actual device
1412 int ret = (st1.st_dev != st2.st_dev) ? true : false;
1413
1414 return ret;
1415}
1416
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05001417bool TWPartition::Is_File_System_Writable(void) {
1418 if (!Is_File_System(Current_File_System) || !Is_Mounted())
1419 return false;
1420
1421 string test_path = Mount_Point + "/.";
1422 return (access(test_path.c_str(), W_OK) == 0);
1423}
1424
Dees_Troy5bf43922012-09-07 16:07:55 -04001425bool TWPartition::Mount(bool Display_Error) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001426 int exfat_mounted = 0;
Martin Dünkelmann6d843782018-12-27 23:40:28 +01001427 unsigned int flags = Mount_Flags;
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001428
Dees_Troy5bf43922012-09-07 16:07:55 -04001429 if (Is_Mounted()) {
1430 return true;
1431 } else if (!Can_Be_Mounted) {
1432 return false;
1433 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001434
1435 Find_Actual_Block_Device();
1436
1437 // Check the current file system before mounting
1438 Check_FS_Type();
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001439 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/exfat-fuse")) {
Dees_Troye34c1332013-02-06 19:13:00 +00001440 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 +00001441 LOGINFO("cmd: %s\n", cmd.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001442 string result;
1443 if (TWFunc::Exec_Cmd(cmd, result) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001444 LOGINFO("exfat-fuse failed to mount with result '%s', trying vfat\n", result.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001445 Current_File_System = "vfat";
1446 } else {
1447#ifdef TW_NO_EXFAT_FUSE
1448 UnMount(false);
1449 // We'll let the kernel handle it but using exfat-fuse to detect if the file system is actually exfat
1450 // Some kernels let us mount vfat as exfat which doesn't work out too well
1451#else
1452 exfat_mounted = 1;
1453#endif
1454 }
1455 }
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001456
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001457 if (Current_File_System == "ntfs" && (TWFunc::Path_Exists("/sbin/ntfs-3g") || TWFunc::Path_Exists("/sbin/mount.ntfs"))) {
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001458 string cmd;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001459 string Ntfsmount_Binary = "";
1460
1461 if (TWFunc::Path_Exists("/sbin/ntfs-3g"))
1462 Ntfsmount_Binary = "ntfs-3g";
1463 else if (TWFunc::Path_Exists("/sbin/mount.ntfs"))
1464 Ntfsmount_Binary = "mount.ntfs";
1465
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001466 if (Mount_Read_Only)
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001467 cmd = "/sbin/" + Ntfsmount_Binary + " -o ro " + Actual_Block_Device + " " + Mount_Point;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001468 else
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001469 cmd = "/sbin/" + Ntfsmount_Binary + " " + Actual_Block_Device + " " + Mount_Point;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001470 LOGINFO("cmd: '%s'\n", cmd.c_str());
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001471
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001472 if (TWFunc::Exec_Cmd(cmd) == 0) {
1473 return true;
1474 } else {
1475 LOGINFO("ntfs-3g failed to mount, trying regular mount method.\n");
1476 }
1477 }
1478
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001479 if (Mount_Read_Only)
1480 flags |= MS_RDONLY;
1481
Dees_Troy22042032012-12-18 21:23:08 +00001482 if (Fstab_File_System == "yaffs2") {
1483 // mount an MTD partition as a YAFFS2 filesystem.
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001484 flags = MS_NOATIME | MS_NODEV | MS_NODIRATIME;
1485 if (Mount_Read_Only)
1486 flags |= MS_RDONLY;
Dees_Troy76543db2013-06-19 16:24:30 +00001487 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags, NULL) < 0) {
1488 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags | MS_RDONLY, NULL) < 0) {
1489 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001490 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy76543db2013-06-19 16:24:30 +00001491 else
1492 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
1493 return false;
1494 } else {
1495 LOGINFO("Mounted '%s' (MTD) as RO\n", Mount_Point.c_str());
1496 return true;
1497 }
1498 } else {
1499 struct stat st;
1500 string test_path = Mount_Point;
1501 if (stat(test_path.c_str(), &st) < 0) {
1502 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001503 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy76543db2013-06-19 16:24:30 +00001504 else
1505 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
1506 return false;
1507 }
1508 mode_t new_mode = st.st_mode | S_IXUSR | S_IXGRP | S_IXOTH;
1509 if (new_mode != st.st_mode) {
1510 LOGINFO("Fixing execute permissions for %s\n", Mount_Point.c_str());
1511 if (chmod(Mount_Point.c_str(), new_mode) < 0) {
1512 if (Display_Error)
1513 LOGERR("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
1514 else
1515 LOGINFO("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
1516 return false;
1517 }
1518 }
Dees_Troy22042032012-12-18 21:23:08 +00001519 return true;
Dees_Troy76543db2013-06-19 16:24:30 +00001520 }
thatc7572eb2015-03-31 18:55:30 +02001521 }
1522
1523 string mount_fs = Current_File_System;
1524 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sys/module/texfat"))
1525 mount_fs = "texfat";
1526
1527 if (!exfat_mounted &&
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001528 mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), mount_fs.c_str(), flags, Mount_Options.c_str()) != 0 &&
1529 mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), mount_fs.c_str(), flags, NULL) != 0) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001530#ifdef TW_NO_EXFAT_FUSE
1531 if (Current_File_System == "exfat") {
Dees_Troy2673cec2013-04-02 20:22:16 +00001532 LOGINFO("Mounting exfat failed, trying vfat...\n");
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001533 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), "vfat", 0, NULL) != 0) {
Dees_Troy85f44ed2013-01-09 18:42:36 +00001534 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001535 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy85f44ed2013-01-09 18:42:36 +00001536 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001537 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001538 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 +00001539 return false;
Dees_Troy85f44ed2013-01-09 18:42:36 +00001540 }
Dees_Troyb05ddee2013-01-28 20:24:50 +00001541 } else {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001542#endif
bigbiff bigbiff26774a02014-03-29 18:22:00 -04001543 if (!Removable && Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001544 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001545 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001546 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
1547 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 +00001548 return false;
1549#ifdef TW_NO_EXFAT_FUSE
Dees_Troy85f44ed2013-01-09 18:42:36 +00001550 }
1551#endif
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001552 }
Ethan Yonker253368a2014-11-25 15:00:52 -06001553
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001554 if (Removable)
1555 Update_Size(Display_Error);
1556
xiaolu9416f4f2015-06-04 08:22:23 +08001557 if (!Symlink_Mount_Point.empty() && TWFunc::Path_Exists(Symlink_Path)) {
codelover2a3d4ce2015-03-14 20:26:49 +08001558 string Command = "mount -o bind '" + Symlink_Path + "' '" + Symlink_Mount_Point + "'";
Vojtech Bocek05534202013-09-11 08:11:56 +02001559 TWFunc::Exec_Cmd(Command);
Dees_Troy5bf43922012-09-07 16:07:55 -04001560 }
Chaosmasterda974802020-01-26 21:09:28 +01001561
1562 if (Mount_Point == "/system_root") {
1563 unlink("/system");
1564 mkdir("/system", 0755);
1565 mount("/system_root/system", "/system", "auto", MS_BIND, NULL);
1566 }
1567
Dees_Troy5bf43922012-09-07 16:07:55 -04001568 return true;
1569}
1570
1571bool TWPartition::UnMount(bool Display_Error) {
Chaosmasterda974802020-01-26 21:09:28 +01001572 if (Mount_Point == "/system_root") {
1573 if (umount("/system") == -1)
1574 umount2("/system", MNT_DETACH);
1575 rmdir("/system");
1576 symlink("/system_root/system", "/system");
1577 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001578 if (Is_Mounted()) {
1579 int never_unmount_system;
1580
1581 DataManager::GetValue(TW_DONT_UNMOUNT_SYSTEM, never_unmount_system);
Captain Throwback9d6feb52018-07-27 10:05:24 -04001582 if (never_unmount_system == 1 && Mount_Point == PartitionManager.Get_Android_Root_Path())
Dees_Troy5bf43922012-09-07 16:07:55 -04001583 return true; // Never unmount system if you're not supposed to unmount it
1584
Matt Mowera8e6d832017-02-14 20:20:59 -06001585 if (Is_Storage && MTP_Storage_ID > 0)
Ethan Yonker726a0202014-12-16 20:01:38 -06001586 PartitionManager.Remove_MTP_Storage(MTP_Storage_ID);
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001587
Dees_Troy38bd7602012-09-14 13:33:53 -04001588 if (!Symlink_Mount_Point.empty())
1589 umount(Symlink_Mount_Point.c_str());
1590
Dees_Troyb05ddee2013-01-28 20:24:50 +00001591 umount(Mount_Point.c_str());
1592 if (Is_Mounted()) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001593 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001594 gui_msg(Msg(msg::kError, "fail_unmount=Failed to unmount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy5bf43922012-09-07 16:07:55 -04001595 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001596 LOGINFO("Unable to unmount '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001597 return false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001598 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -04001599 return true;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001600 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001601 } else {
1602 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001603 }
1604}
1605
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05001606bool TWPartition::ReMount(bool Display_Error) {
1607 if (UnMount(Display_Error))
1608 return Mount(Display_Error);
1609 return false;
1610}
1611
1612bool TWPartition::ReMount_RW(bool Display_Error) {
1613 // No need to remount if already mounted rw
1614 if (Is_File_System_Writable())
1615 return true;
1616
1617 bool ro = Mount_Read_Only;
1618 int flags = Mount_Flags;
1619
1620 Mount_Read_Only = false;
1621 Mount_Flags &= ~MS_RDONLY;
1622
1623 bool ret = ReMount(Display_Error);
1624
1625 Mount_Read_Only = ro;
1626 Mount_Flags = flags;
1627
1628 return ret;
1629}
1630
Gary Peck43acadf2012-11-21 21:19:01 -08001631bool TWPartition::Wipe(string New_File_System) {
Ethan Yonker726a0202014-12-16 20:01:38 -06001632 bool wiped = false, update_crypt = false, recreate_media = true;
Dees_Troy16c2b312013-01-15 16:51:18 +00001633 int check;
1634 string Layout_Filename = Mount_Point + "/.layout_version";
1635
Dees_Troy38bd7602012-09-14 13:33:53 -04001636 if (!Can_Be_Wiped) {
Matt Mower3c366972015-12-25 19:28:31 -06001637 gui_msg(Msg(msg::kError, "cannot_wipe=Partition {1} cannot be wiped.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04001638 return false;
1639 }
1640
Dees_Troyc51f1f92012-09-20 15:32:13 -04001641 if (Mount_Point == "/cache")
Dees_Troy2673cec2013-04-02 20:22:16 +00001642 Log_Offset = 0;
Dees_Troyc51f1f92012-09-20 15:32:13 -04001643
Dees_Troy16c2b312013-01-15 16:51:18 +00001644 if (Retain_Layout_Version && Mount(false) && TWFunc::Path_Exists(Layout_Filename))
1645 TWFunc::copy_file(Layout_Filename, "/.layout_version", 0600);
1646 else
1647 unlink("/.layout_version");
Dees_Troy38bd7602012-09-14 13:33:53 -04001648
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001649 if (Has_Data_Media && Current_File_System == New_File_System) {
Dees_Troy16c2b312013-01-15 16:51:18 +00001650 wiped = Wipe_Data_Without_Wiping_Media();
Ethan Yonker5eac2222014-06-11 12:22:55 -05001651 recreate_media = false;
Dees_Troy16c2b312013-01-15 16:51:18 +00001652 } else {
Dees_Troy16c2b312013-01-15 16:51:18 +00001653 DataManager::GetValue(TW_RM_RF_VAR, check);
1654
Hashcodedabfd492013-08-29 22:45:30 -07001655 if (check || Use_Rm_Rf)
Dees_Troy16c2b312013-01-15 16:51:18 +00001656 wiped = Wipe_RMRF();
1657 else if (New_File_System == "ext4")
1658 wiped = Wipe_EXT4();
1659 else if (New_File_System == "ext2" || New_File_System == "ext3")
dianlujitao4879b372018-12-03 18:45:47 +08001660 wiped = Wipe_EXTFS(New_File_System);
Dees_Troy16c2b312013-01-15 16:51:18 +00001661 else if (New_File_System == "vfat")
1662 wiped = Wipe_FAT();
1663 else if (New_File_System == "exfat")
1664 wiped = Wipe_EXFAT();
1665 else if (New_File_System == "yaffs2")
1666 wiped = Wipe_MTD();
Dees_Troye5017042013-08-29 16:38:55 +00001667 else if (New_File_System == "f2fs")
1668 wiped = Wipe_F2FS();
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001669 else if (New_File_System == "ntfs")
1670 wiped = Wipe_NTFS();
Dees_Troy16c2b312013-01-15 16:51:18 +00001671 else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001672 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 +00001673 unlink("/.layout_version");
1674 return false;
1675 }
1676 update_crypt = wiped;
Gary Pecke8bc5d72012-12-21 06:45:25 -08001677 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001678
Gary Pecke8bc5d72012-12-21 06:45:25 -08001679 if (wiped) {
Dees_Troy1c1ac442013-01-17 21:42:14 +00001680 if (Mount_Point == "/cache")
1681 DataManager::Output_Version();
1682
Dees_Troy16c2b312013-01-15 16:51:18 +00001683 if (TWFunc::Path_Exists("/.layout_version") && Mount(false))
1684 TWFunc::copy_file("/.layout_version", Layout_Filename, 0600);
1685
1686 if (update_crypt) {
1687 Setup_File_System(false);
1688 if (Is_Encrypted && !Is_Decrypted) {
1689 // just wiped an encrypted partition back to its unencrypted state
1690 Is_Encrypted = false;
1691 Is_Decrypted = false;
1692 Decrypted_Block_Device = "";
1693 if (Mount_Point == "/data") {
1694 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1695 DataManager::SetValue(TW_IS_DECRYPTED, 0);
1696 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001697 }
1698 }
Ethan Yonker5eac2222014-06-11 12:22:55 -05001699
Ethan Yonker66a19492015-12-10 10:19:45 -06001700 if (Has_Data_Media && recreate_media) {
Ethan Yonker5eac2222014-06-11 12:22:55 -05001701 Recreate_Media_Folder();
1702 }
Matt Mower209c9632016-01-20 12:08:35 -06001703 if (Is_Storage && Mount(false))
1704 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Gary Pecke8bc5d72012-12-21 06:45:25 -08001705 }
Matt Mower209c9632016-01-20 12:08:35 -06001706
Gary Pecke8bc5d72012-12-21 06:45:25 -08001707 return wiped;
Dees_Troy51a0e822012-09-05 15:24:24 -04001708}
1709
Gary Peck43acadf2012-11-21 21:19:01 -08001710bool TWPartition::Wipe() {
Gary Peck82599a82012-11-21 16:23:12 -08001711 if (Is_File_System(Current_File_System))
1712 return Wipe(Current_File_System);
1713 else
1714 return Wipe(Fstab_File_System);
Gary Peck43acadf2012-11-21 21:19:01 -08001715}
1716
Dees_Troye58d5262012-09-21 12:27:57 -04001717bool TWPartition::Wipe_AndSec(void) {
1718 if (!Has_Android_Secure)
1719 return false;
1720
Dees_Troye58d5262012-09-21 12:27:57 -04001721 if (!Mount(true))
1722 return false;
1723
Ethan Yonker74db1572015-10-28 12:44:49 -05001724 gui_msg(Msg("wiping=Wiping {1}")(Backup_Display_Name));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001725 TWFunc::removeDir(Mount_Point + "/.android_secure/", true);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001726 return true;
Dees_Troye58d5262012-09-21 12:27:57 -04001727}
1728
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001729bool TWPartition::Can_Repair() {
Ethan Yonkera2719152015-05-28 09:44:41 -05001730 if (Mount_Read_Only)
1731 return false;
Matt Mower18794c82015-11-11 16:22:45 -06001732 if (Current_File_System == "vfat" && TWFunc::Path_Exists("/sbin/fsck.fat"))
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001733 return true;
1734 else if ((Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") && TWFunc::Path_Exists("/sbin/e2fsck"))
1735 return true;
1736 else if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/fsck.exfat"))
1737 return true;
1738 else if (Current_File_System == "f2fs" && TWFunc::Path_Exists("/sbin/fsck.f2fs"))
1739 return true;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001740 else if (Current_File_System == "ntfs" && (TWFunc::Path_Exists("/sbin/ntfsfix") || TWFunc::Path_Exists("/sbin/fsck.ntfs")))
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001741 return true;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001742 return false;
1743}
1744
1745bool TWPartition::Repair() {
1746 string command;
1747
1748 if (Current_File_System == "vfat") {
Matt Mower18794c82015-11-11 16:22:45 -06001749 if (!TWFunc::Path_Exists("/sbin/fsck.fat")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001750 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.fat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001751 return false;
1752 }
1753 if (!UnMount(true))
1754 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001755 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.fat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001756 Find_Actual_Block_Device();
Matt Mower18794c82015-11-11 16:22:45 -06001757 command = "/sbin/fsck.fat -y " + Actual_Block_Device;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001758 LOGINFO("Repair command: %s\n", command.c_str());
1759 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001760 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001761 return true;
1762 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001763 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001764 return false;
1765 }
1766 }
1767 if (Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") {
1768 if (!TWFunc::Path_Exists("/sbin/e2fsck")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001769 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("e2fsck"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001770 return false;
1771 }
1772 if (!UnMount(true))
1773 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001774 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("e2fsck"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001775 Find_Actual_Block_Device();
Ethan Yonkera2719152015-05-28 09:44:41 -05001776 command = "/sbin/e2fsck -fp " + Actual_Block_Device;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001777 LOGINFO("Repair command: %s\n", command.c_str());
1778 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001779 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001780 return true;
1781 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001782 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001783 return false;
1784 }
1785 }
1786 if (Current_File_System == "exfat") {
1787 if (!TWFunc::Path_Exists("/sbin/fsck.exfat")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001788 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.exfat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001789 return false;
1790 }
1791 if (!UnMount(true))
1792 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001793 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.exfat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001794 Find_Actual_Block_Device();
1795 command = "/sbin/fsck.exfat " + Actual_Block_Device;
1796 LOGINFO("Repair command: %s\n", command.c_str());
1797 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001798 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001799 return true;
1800 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001801 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001802 return false;
1803 }
1804 }
1805 if (Current_File_System == "f2fs") {
1806 if (!TWFunc::Path_Exists("/sbin/fsck.f2fs")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001807 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.f2fs"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001808 return false;
1809 }
1810 if (!UnMount(true))
1811 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001812 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.f2fs"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001813 Find_Actual_Block_Device();
1814 command = "/sbin/fsck.f2fs " + Actual_Block_Device;
1815 LOGINFO("Repair command: %s\n", command.c_str());
1816 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001817 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001818 return true;
1819 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001820 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001821 return false;
1822 }
1823 }
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001824 if (Current_File_System == "ntfs") {
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001825 string Ntfsfix_Binary;
1826 if (TWFunc::Path_Exists("/sbin/ntfsfix"))
1827 Ntfsfix_Binary = "ntfsfix";
1828 else if (TWFunc::Path_Exists("/sbin/fsck.ntfs"))
1829 Ntfsfix_Binary = "fsck.ntfs";
1830 else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001831 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("ntfsfix"));
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001832 return false;
1833 }
1834 if (!UnMount(true))
1835 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001836 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)(Ntfsfix_Binary));
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001837 Find_Actual_Block_Device();
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001838 command = "/sbin/" + Ntfsfix_Binary + " " + Actual_Block_Device;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001839 LOGINFO("Repair command: %s\n", command.c_str());
1840 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001841 gui_msg("done=Done.");
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001842 return true;
1843 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001844 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001845 return false;
1846 }
1847 }
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001848 return false;
1849}
1850
Ethan Yonkera2719152015-05-28 09:44:41 -05001851bool TWPartition::Can_Resize() {
1852 if (Mount_Read_Only)
1853 return false;
1854 if ((Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") && TWFunc::Path_Exists("/sbin/resize2fs"))
1855 return true;
1856 return false;
1857}
1858
1859bool TWPartition::Resize() {
1860 string command;
1861
1862 if (Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") {
1863 if (!Can_Repair()) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001864 LOGINFO("Cannot resize %s because %s cannot be repaired before resizing.\n", Display_Name.c_str(), Display_Name.c_str());
1865 gui_msg(Msg(msg::kError, "cannot_resize=Cannot resize {1}.")(Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001866 return false;
1867 }
1868 if (!TWFunc::Path_Exists("/sbin/resize2fs")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001869 LOGINFO("resize2fs does not exist! Cannot resize!\n");
1870 gui_msg(Msg(msg::kError, "cannot_resize=Cannot resize {1}.")(Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001871 return false;
1872 }
1873 // Repair will unmount so no need to do it twice
Ethan Yonker74db1572015-10-28 12:44:49 -05001874 gui_msg(Msg("repair_resize=Repairing {1} before resizing.")( Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001875 if (!Repair())
1876 return false;
Ethan Yonker74db1572015-10-28 12:44:49 -05001877 gui_msg(Msg("resizing=Resizing {1} using {2}...")(Display_Name)("resize2fs"));
Ethan Yonkera2719152015-05-28 09:44:41 -05001878 Find_Actual_Block_Device();
1879 command = "/sbin/resize2fs " + Actual_Block_Device;
1880 if (Length != 0) {
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001881 unsigned long long Actual_Size = IOCTL_Get_Block_Size();
1882 if (Actual_Size == 0)
1883 return false;
Ethan Yonkera2719152015-05-28 09:44:41 -05001884
Ethan Yonkera2719152015-05-28 09:44:41 -05001885 unsigned long long Block_Count;
1886 if (Length < 0) {
1887 // Reduce overall size by this length
1888 Block_Count = (Actual_Size / 1024LLU) - ((unsigned long long)(Length * -1) / 1024LLU);
1889 } else {
1890 // This is the size, not a size reduction
1891 Block_Count = ((unsigned long long)(Length) / 1024LLU);
1892 }
1893 char temp[256];
1894 sprintf(temp, "%llu", Block_Count);
1895 command += " ";
1896 command += temp;
1897 command += "K";
1898 }
1899 LOGINFO("Resize command: %s\n", command.c_str());
1900 if (TWFunc::Exec_Cmd(command) == 0) {
1901 Update_Size(true);
Ethan Yonker74db1572015-10-28 12:44:49 -05001902 gui_msg("done=Done.");
Ethan Yonkera2719152015-05-28 09:44:41 -05001903 return true;
1904 } else {
1905 Update_Size(true);
Ethan Yonker74db1572015-10-28 12:44:49 -05001906 gui_msg(Msg(msg::kError, "unable_resize=Unable to resize {1}.")(Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001907 return false;
1908 }
1909 }
1910 return false;
1911}
1912
bigbiffce8f83c2015-12-12 18:30:21 -05001913bool TWPartition::Backup(PartitionSettings *part_settings, pid_t *tar_fork_pid) {
1914 if (Backup_Method == BM_FILES)
1915 return Backup_Tar(part_settings, tar_fork_pid);
1916 else if (Backup_Method == BM_DD)
1917 return Backup_Image(part_settings);
1918 else if (Backup_Method == BM_FLASH_UTILS)
1919 return Backup_Dump_Image(part_settings);
Dees_Troy2673cec2013-04-02 20:22:16 +00001920 LOGERR("Unknown backup method for '%s'\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001921 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001922}
1923
bigbiffce8f83c2015-12-12 18:30:21 -05001924bool TWPartition::Restore(PartitionSettings *part_settings) {
Matt Mower3c366972015-12-25 19:28:31 -06001925 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, gui_parse_text("{@restoring_hdr}"));
Ethan Yonkere080c1f2016-09-19 13:50:25 -05001926 LOGINFO("Restore filename is: %s/%s\n", part_settings->Backup_Folder.c_str(), Backup_FileName.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001927
bigbiffce8f83c2015-12-12 18:30:21 -05001928 string Restore_File_System = Get_Restore_File_System(part_settings);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001929
1930 if (Is_File_System(Restore_File_System))
bigbiffce8f83c2015-12-12 18:30:21 -05001931 return Restore_Tar(part_settings);
1932 else if (Is_Image(Restore_File_System))
1933 return Restore_Image(part_settings);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001934
1935 LOGERR("Unknown restore method for '%s'\n", Mount_Point.c_str());
1936 return false;
1937}
1938
bigbiffce8f83c2015-12-12 18:30:21 -05001939string TWPartition::Get_Restore_File_System(PartitionSettings *part_settings) {
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001940 size_t first_period, second_period;
1941 string Restore_File_System;
1942
Gary Peck43acadf2012-11-21 21:19:01 -08001943 // Parse backup filename to extract the file system before wiping
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05001944 first_period = Backup_FileName.find(".");
Gary Peck43acadf2012-11-21 21:19:01 -08001945 if (first_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001946 LOGERR("Unable to find file system (first period).\n");
thatd43bf2d2014-09-21 23:13:02 +02001947 return string();
Gary Peck43acadf2012-11-21 21:19:01 -08001948 }
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05001949 Restore_File_System = Backup_FileName.substr(first_period + 1, Backup_FileName.size() - first_period - 1);
Gary Peck43acadf2012-11-21 21:19:01 -08001950 second_period = Restore_File_System.find(".");
1951 if (second_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001952 LOGERR("Unable to find file system (second period).\n");
thatd43bf2d2014-09-21 23:13:02 +02001953 return string();
Gary Peck43acadf2012-11-21 21:19:01 -08001954 }
1955 Restore_File_System.resize(second_period);
Matt Mowera0cd91d2016-12-30 18:21:42 -06001956 LOGINFO("Restore file system is: '%s'.\n", Restore_File_System.c_str());
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001957 return Restore_File_System;
Dees_Troy51a0e822012-09-05 15:24:24 -04001958}
1959
1960string TWPartition::Backup_Method_By_Name() {
bigbiffce8f83c2015-12-12 18:30:21 -05001961 if (Backup_Method == BM_NONE)
Dees_Troy38bd7602012-09-14 13:33:53 -04001962 return "none";
bigbiffce8f83c2015-12-12 18:30:21 -05001963 else if (Backup_Method == BM_FILES)
Dees_Troy38bd7602012-09-14 13:33:53 -04001964 return "files";
bigbiffce8f83c2015-12-12 18:30:21 -05001965 else if (Backup_Method == BM_DD)
Dees_Troy38bd7602012-09-14 13:33:53 -04001966 return "dd";
bigbiffce8f83c2015-12-12 18:30:21 -05001967 else if (Backup_Method == BM_FLASH_UTILS)
Dees_Troy38bd7602012-09-14 13:33:53 -04001968 return "flash_utils";
1969 else
1970 return "undefined";
1971 return "ERROR!";
Dees_Troy51a0e822012-09-05 15:24:24 -04001972}
1973
1974bool TWPartition::Decrypt(string Password) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001975 LOGINFO("STUB TWPartition::Decrypt, password: '%s'\n", Password.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001976 // Is this needed?
Dees_Troy51a0e822012-09-05 15:24:24 -04001977 return 1;
1978}
1979
1980bool TWPartition::Wipe_Encryption() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001981 bool Save_Data_Media = Has_Data_Media;
Ethan Yonker93382822018-11-01 15:25:31 -05001982 bool ret = false;
1983 BasePartition* base_partition = make_partition();
1984
1985 if (!base_partition->PreWipeEncryption())
1986 goto exit;
Dees_Troy38bd7602012-09-14 13:33:53 -04001987
dianlujitao4879b372018-12-03 18:45:47 +08001988 Find_Actual_Block_Device();
1989 if (!Is_Present) {
1990 LOGINFO("Block device not present, cannot format %s.\n", Display_Name.c_str());
1991 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
1992 return false;
1993 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001994 if (!UnMount(true))
Ethan Yonker93382822018-11-01 15:25:31 -05001995 goto exit;
Dees_Troy38bd7602012-09-14 13:33:53 -04001996
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001997#ifdef TW_INCLUDE_CRYPTO
Ethan Yonker1b190162016-12-05 15:25:19 -06001998 if (Is_Decrypted && !Decrypted_Block_Device.empty()) {
Matt Mower2b18a532015-02-20 16:58:05 -06001999 if (delete_crypto_blk_dev((char*)("userdata")) != 0) {
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06002000 LOGERR("Error deleting crypto block device, continuing anyway.\n");
2001 }
2002 }
2003#endif
dianlujitao4879b372018-12-03 18:45:47 +08002004 Has_Data_Media = false;
Ethan Yonker93382822018-11-01 15:25:31 -05002005 Decrypted_Block_Device = "";
Dees_Troy74fb2e92013-04-15 14:35:47 +00002006 Is_Decrypted = false;
2007 Is_Encrypted = false;
Gary Pecke8bc5d72012-12-21 06:45:25 -08002008 if (Wipe(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002009 Has_Data_Media = Save_Data_Media;
2010 if (Has_Data_Media && !Symlink_Mount_Point.empty()) {
2011 Recreate_Media_Folder();
Ethan Yonker726a0202014-12-16 20:01:38 -06002012 if (Mount(false))
2013 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04002014 }
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06002015 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
Ethan Yonker83e82572014-04-04 10:59:28 -05002016#ifndef TW_OEM_BUILD
Ethan Yonker74db1572015-10-28 12:44:49 -05002017 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 -05002018#endif
Ethan Yonker93382822018-11-01 15:25:31 -05002019 ret = true;
2020 if (!Key_Directory.empty())
2021 ret = PartitionManager.Wipe_By_Path(Key_Directory);
2022 if (ret)
2023 ret = base_partition->PostWipeEncryption();
2024 goto exit;
Dees_Troy38bd7602012-09-14 13:33:53 -04002025 } else {
2026 Has_Data_Media = Save_Data_Media;
Ethan Yonker74db1572015-10-28 12:44:49 -05002027 gui_err("format_data_err=Unable to format to remove encryption.");
Ethan Yonker726a0202014-12-16 20:01:38 -06002028 if (Has_Data_Media && Mount(false))
2029 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Ethan Yonker93382822018-11-01 15:25:31 -05002030 goto exit;
Dees_Troy38bd7602012-09-14 13:33:53 -04002031 }
Ethan Yonker93382822018-11-01 15:25:31 -05002032exit:
2033 delete base_partition;
2034 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04002035}
2036
2037void TWPartition::Check_FS_Type() {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05002038 const char* type;
2039 blkid_probe pr;
Dees_Troy5bf43922012-09-07 16:07:55 -04002040
Dees_Troy68cab492012-12-12 19:29:35 +00002041 if (Fstab_File_System == "yaffs2" || Fstab_File_System == "mtd" || Fstab_File_System == "bml" || Ignore_Blkid)
2042 return; // Running blkid on some mtd devices causes a massive crash or needs to be skipped
Dees_Troy5bf43922012-09-07 16:07:55 -04002043
Dees_Troy38bd7602012-09-14 13:33:53 -04002044 Find_Actual_Block_Device();
Dees_Troy8170a922012-09-18 15:40:25 -04002045 if (!Is_Present)
2046 return;
Dees_Troy51127312012-09-08 13:08:49 -04002047
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05002048 pr = blkid_new_probe_from_filename(Actual_Block_Device.c_str());
2049 if (blkid_do_fullprobe(pr)) {
2050 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00002051 LOGINFO("Can't probe device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05002052 return;
Dees_Troy5bf43922012-09-07 16:07:55 -04002053 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02002054
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05002055 if (blkid_probe_lookup_value(pr, "TYPE", &type, NULL) < 0) {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05002056 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00002057 LOGINFO("can't find filesystem on device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05002058 return;
2059 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02002060
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05002061 Current_File_System = type;
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02002062 blkid_free_probe(pr);
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06002063 if (fs_flags.size() > 1) {
2064 std::vector<partition_fs_flags_struct>::iterator iter;
2065 std::vector<partition_fs_flags_struct>::iterator found = fs_flags.begin();
2066
2067 for (iter = fs_flags.begin(); iter != fs_flags.end(); iter++) {
2068 if (iter->File_System == Current_File_System) {
2069 found = iter;
2070 break;
2071 }
2072 }
2073 // If we don't find a match, we default the flags to the first set of flags that we received from the fstab
2074 if (Mount_Flags != found->Mount_Flags || Mount_Options != found->Mount_Options) {
2075 Mount_Flags = found->Mount_Flags;
2076 Mount_Options = found->Mount_Options;
2077 LOGINFO("Mount_Flags: %i, Mount_Options: %s\n", Mount_Flags, Mount_Options.c_str());
2078 }
2079 }
Dees_Troy51a0e822012-09-05 15:24:24 -04002080}
2081
dianlujitao4879b372018-12-03 18:45:47 +08002082bool TWPartition::Wipe_EXTFS(string File_System) {
2083#if PLATFORM_SDK_VERSION < 28
2084 if (!TWFunc::Path_Exists("/sbin/mke2fs"))
2085#else
2086 if (!TWFunc::Path_Exists("/sbin/mke2fs") || !TWFunc::Path_Exists("/sbin/e2fsdroid"))
2087#endif
Dees_Troy38bd7602012-09-14 13:33:53 -04002088 return Wipe_RMRF();
2089
dianlujitao4879b372018-12-03 18:45:47 +08002090 int ret;
2091 bool NeedPreserveFooter = true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002092
Ethan Yonker25f20c12014-10-14 09:04:43 -05002093 Find_Actual_Block_Device();
2094 if (!Is_Present) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002095 LOGINFO("Block device not present, cannot wipe %s.\n", Display_Name.c_str());
2096 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Ethan Yonker25f20c12014-10-14 09:04:43 -05002097 return false;
2098 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002099 if (!UnMount(true))
2100 return false;
2101
dianlujitao4879b372018-12-03 18:45:47 +08002102 /**
2103 * On decrypted devices, IOCTL_Get_Block_Size calculates size on device mapper,
2104 * so there's no need to preserve footer.
2105 */
2106 if ((Is_Decrypted && !Decrypted_Block_Device.empty()) ||
2107 Crypto_Key_Location != "footer") {
2108 NeedPreserveFooter = false;
2109 }
2110
2111 unsigned long long dev_sz = TWFunc::IOCTL_Get_Block_Size(Actual_Block_Device.c_str());
2112 if (!dev_sz)
2113 return false;
2114
2115 if (NeedPreserveFooter)
2116 Length < 0 ? dev_sz += Length : dev_sz -= CRYPT_FOOTER_OFFSET;
2117
Dees Troy9a4d7402019-03-21 14:17:28 -04002118 char dout[16];
2119 sprintf(dout, "%llu", dev_sz / 4096);
2120
2121 //string size_str =to_string(dev_sz / 4096);
2122 string size_str = dout;
dianlujitao4879b372018-12-03 18:45:47 +08002123 string Command;
2124
2125 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mke2fs"));
2126
2127 // Execute mke2fs to create empty ext4 filesystem
2128 Command = "mke2fs -t " + File_System + " -b 4096 " + Actual_Block_Device + " " + size_str;
2129 LOGINFO("mke2fs command: %s\n", Command.c_str());
2130 ret = TWFunc::Exec_Cmd(Command);
2131 if (ret) {
2132 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
2133 return false;
2134 }
2135
2136 if (TWFunc::Path_Exists("/sbin/e2fsdroid")) {
dianlujitao3cf05a52019-05-15 15:33:27 +08002137 const string& File_Contexts_Entry = (Mount_Point == "/system_root" ? "/" : Mount_Point);
2138 char *secontext = NULL;
2139 if (!selinux_handle || selabel_lookup(selinux_handle, &secontext, File_Contexts_Entry.c_str(), S_IFDIR) < 0) {
2140 LOGINFO("Cannot lookup security context for '%s'\n", Mount_Point.c_str());
2141 } else {
2142 // Execute e2fsdroid to initialize selinux context
2143 Command = "e2fsdroid -e -S /file_contexts -a " + File_Contexts_Entry + " " + Actual_Block_Device;
2144 LOGINFO("e2fsdroid command: %s\n", Command.c_str());
2145 ret = TWFunc::Exec_Cmd(Command);
2146 if (ret) {
2147 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
2148 return false;
2149 }
dianlujitao4879b372018-12-03 18:45:47 +08002150 }
2151 } else {
2152 LOGINFO("e2fsdroid not present\n");
2153 }
2154
2155 if (NeedPreserveFooter)
2156 Wipe_Crypto_Key();
2157 Current_File_System = File_System;
2158 Recreate_AndSec_Folder();
2159 gui_msg("done=Done.");
2160 return true;
2161}
2162
2163bool TWPartition::Wipe_EXT4() {
2164#ifdef USE_EXT4
Ethan Yonkerf27497f2014-02-09 11:48:33 -06002165 int ret;
dianlujitao4879b372018-12-03 18:45:47 +08002166 bool NeedPreserveFooter = true;
2167
2168 Find_Actual_Block_Device();
2169 if (!Is_Present) {
2170 LOGINFO("Block device not present, cannot wipe %s.\n", Display_Name.c_str());
2171 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
2172 return false;
2173 }
2174 if (!UnMount(true))
2175 return false;
2176
2177 /**
2178 * On decrypted devices, IOCTL_Get_Block_Size calculates size on device mapper,
2179 * so there's no need to preserve footer.
2180 */
2181 if ((Is_Decrypted && !Decrypted_Block_Device.empty()) ||
2182 Crypto_Key_Location != "footer") {
2183 NeedPreserveFooter = false;
2184 }
2185
2186 unsigned long long dev_sz = TWFunc::IOCTL_Get_Block_Size(Actual_Block_Device.c_str());
2187 if (!dev_sz)
2188 return false;
2189
2190 if (NeedPreserveFooter)
2191 Length < 0 ? dev_sz += Length : dev_sz -= CRYPT_FOOTER_OFFSET;
2192
Ethan Yonkerf27497f2014-02-09 11:48:33 -06002193 char *secontext = NULL;
2194
Greg Wallace4b44fef2015-12-29 15:33:24 -05002195 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("make_ext4fs"));
Ethan Yonkerf27497f2014-02-09 11:48:33 -06002196
Dees Troy99c8dbf2014-03-10 16:53:58 +00002197 if (!selinux_handle || selabel_lookup(selinux_handle, &secontext, Mount_Point.c_str(), S_IFDIR) < 0) {
Ethan Yonkerf27497f2014-02-09 11:48:33 -06002198 LOGINFO("Cannot lookup security context for '%s'\n", Mount_Point.c_str());
dianlujitao4879b372018-12-03 18:45:47 +08002199 ret = make_ext4fs(Actual_Block_Device.c_str(), dev_sz, Mount_Point.c_str(), NULL);
Ethan Yonkerf27497f2014-02-09 11:48:33 -06002200 } else {
dianlujitao4879b372018-12-03 18:45:47 +08002201 ret = make_ext4fs(Actual_Block_Device.c_str(), dev_sz, Mount_Point.c_str(), selinux_handle);
Ethan Yonkerf27497f2014-02-09 11:48:33 -06002202 }
2203 if (ret != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002204 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troya95f55c2013-08-17 13:14:43 +00002205 return false;
2206 } else {
dianlujitao4879b372018-12-03 18:45:47 +08002207 if (NeedPreserveFooter)
2208 Wipe_Crypto_Key();
bigbiff bigbiffc49d7062013-10-11 20:28:00 -04002209 string sedir = Mount_Point + "/lost+found";
2210 PartitionManager.Mount_By_Path(sedir.c_str(), true);
2211 rmdir(sedir.c_str());
2212 mkdir(sedir.c_str(), S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP);
Dees_Troya95f55c2013-08-17 13:14:43 +00002213 return true;
2214 }
2215#else
dianlujitao4879b372018-12-03 18:45:47 +08002216 return Wipe_EXTFS("ext4");
Dees_Troya95f55c2013-08-17 13:14:43 +00002217#endif
Dees_Troy51a0e822012-09-05 15:24:24 -04002218}
2219
2220bool TWPartition::Wipe_FAT() {
Vojtech Bocek05534202013-09-11 08:11:56 +02002221 string command;
Dees_Troy38bd7602012-09-14 13:33:53 -04002222
Matt Mower18794c82015-11-11 16:22:45 -06002223 if (TWFunc::Path_Exists("/sbin/mkfs.fat")) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002224 if (!UnMount(true))
2225 return false;
2226
Greg Wallace4b44fef2015-12-29 15:33:24 -05002227 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mkfs.fat"));
Dees_Troy38bd7602012-09-14 13:33:53 -04002228 Find_Actual_Block_Device();
Matt Mower18794c82015-11-11 16:22:45 -06002229 command = "mkfs.fat " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02002230 if (TWFunc::Exec_Cmd(command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08002231 Current_File_System = "vfat";
Dees_Troye58d5262012-09-21 12:27:57 -04002232 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05002233 gui_msg("done=Done.");
Dees_Troy38bd7602012-09-14 13:33:53 -04002234 return true;
2235 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002236 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04002237 return false;
2238 }
2239 return true;
2240 }
2241 else
2242 return Wipe_RMRF();
2243
2244 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04002245}
2246
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002247bool TWPartition::Wipe_EXFAT() {
Vojtech Bocek05534202013-09-11 08:11:56 +02002248 string command;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002249
2250 if (TWFunc::Path_Exists("/sbin/mkexfatfs")) {
2251 if (!UnMount(true))
2252 return false;
2253
Greg Wallace4b44fef2015-12-29 15:33:24 -05002254 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mkexfatfs"));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002255 Find_Actual_Block_Device();
2256 command = "mkexfatfs " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02002257 if (TWFunc::Exec_Cmd(command) == 0) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002258 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05002259 gui_msg("done=Done.");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002260 return true;
2261 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002262 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002263 return false;
2264 }
2265 return true;
2266 }
2267 return false;
2268}
2269
Dees_Troy38bd7602012-09-14 13:33:53 -04002270bool TWPartition::Wipe_MTD() {
2271 if (!UnMount(true))
2272 return false;
2273
Greg Wallace4b44fef2015-12-29 15:33:24 -05002274 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("MTD"));
Dees_Troy38bd7602012-09-14 13:33:53 -04002275
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02002276 mtd_scan_partitions();
2277 const MtdPartition* mtd = mtd_find_partition_by_name(MTD_Name.c_str());
2278 if (mtd == NULL) {
2279 LOGERR("No mtd partition named '%s'", MTD_Name.c_str());
2280 return false;
2281 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002282
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02002283 MtdWriteContext* ctx = mtd_write_partition(mtd);
2284 if (ctx == NULL) {
2285 LOGERR("Can't write '%s', failed to format.", MTD_Name.c_str());
2286 return false;
2287 }
2288 if (mtd_erase_blocks(ctx, -1) == -1) {
2289 mtd_write_close(ctx);
2290 LOGERR("Failed to format '%s'", MTD_Name.c_str());
2291 return false;
2292 }
2293 if (mtd_write_close(ctx) != 0) {
2294 LOGERR("Failed to close '%s'", MTD_Name.c_str());
2295 return false;
2296 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08002297 Current_File_System = "yaffs2";
Dees_Troye58d5262012-09-21 12:27:57 -04002298 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05002299 gui_msg("done=Done.");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02002300 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002301}
2302
2303bool TWPartition::Wipe_RMRF() {
Dees_Troy38bd7602012-09-14 13:33:53 -04002304 if (!Mount(true))
2305 return false;
Ethan Yonker726a0202014-12-16 20:01:38 -06002306 // This is the only wipe that leaves the partition mounted, so we
2307 // must manually remove the partition from MTP if it is a storage
2308 // partition.
2309 if (Is_Storage)
2310 PartitionManager.Remove_MTP_Storage(MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04002311
Ethan Yonker74db1572015-10-28 12:44:49 -05002312 gui_msg(Msg("remove_all=Removing all files under '{1}'")(Mount_Point));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002313 TWFunc::removeDir(Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04002314 Recreate_AndSec_Folder();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002315 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002316}
2317
Dees_Troye5017042013-08-29 16:38:55 +00002318bool TWPartition::Wipe_F2FS() {
Vojtech Bocek05534202013-09-11 08:11:56 +02002319 string command;
Dees_Troye5017042013-08-29 16:38:55 +00002320
2321 if (TWFunc::Path_Exists("/sbin/mkfs.f2fs")) {
dianlujitao4879b372018-12-03 18:45:47 +08002322 bool NeedPreserveFooter = true;
2323
2324 Find_Actual_Block_Device();
2325 if (!Is_Present) {
2326 LOGINFO("Block device not present, cannot wipe %s.\n", Display_Name.c_str());
2327 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
2328 return false;
2329 }
Dees_Troye5017042013-08-29 16:38:55 +00002330 if (!UnMount(true))
2331 return false;
2332
dianlujitao4879b372018-12-03 18:45:47 +08002333 /**
2334 * On decrypted devices, IOCTL_Get_Block_Size calculates size on device mapper,
2335 * so there's no need to preserve footer.
2336 */
2337 if ((Is_Decrypted && !Decrypted_Block_Device.empty()) ||
2338 Crypto_Key_Location != "footer") {
2339 NeedPreserveFooter = false;
dhacker29a3fa75f2015-01-17 19:42:33 -05002340 }
dianlujitao4879b372018-12-03 18:45:47 +08002341
dianlujitao4879b372018-12-03 18:45:47 +08002342 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mkfs.f2fs"));
Ethan Yonker7e941582019-03-22 08:18:21 -05002343 // First determine if we have the old mkfs.f2fs that uses "-r reserved_bytes"
2344 // or the new mkfs.f2fs that expects the number of sectors as the optional last argument
2345 // Note: some 7.1 trees have the old and some have the new.
2346 command = "mkfs.f2fs | grep \"reserved\" > /tmp/f2fsversiontest";
2347 TWFunc::Exec_Cmd(command, false); // no help argument so printing usage exits with an error code
2348 if (!TWFunc::Path_Exists("/tmp/f2fsversiontest")) {
2349 LOGINFO("Error determining mkfs.f2fs version\n");
2350 return false;
2351 }
2352 if (TWFunc::Get_File_Size("/tmp/f2fsversiontest") <= 0) {
2353 LOGINFO("Using newer mkfs.f2fs\n");
2354 unsigned long long dev_sz = TWFunc::IOCTL_Get_Block_Size(Actual_Block_Device.c_str());
2355 if (!dev_sz)
2356 return false;
2357
2358 if (NeedPreserveFooter)
2359 Length < 0 ? dev_sz += Length : dev_sz -= CRYPT_FOOTER_OFFSET;
2360
2361 char dev_sz_str[48];
2362 sprintf(dev_sz_str, "%llu", (dev_sz / 4096));
2363 command = "mkfs.f2fs -d1 -f -O encrypt -O quota -O verity -w 4096 " + Actual_Block_Device + " " + dev_sz_str;
2364 if (TWFunc::Path_Exists("/sbin/sload.f2fs")) {
2365 command += " && sload.f2fs -t /data " + Actual_Block_Device;
2366 }
2367 } else {
2368 LOGINFO("Using older mkfs.f2fs\n");
2369 command = "mkfs.f2fs -t 0";
2370 if (NeedPreserveFooter) {
2371 // Only use length if we're not decrypted
2372 char len[32];
2373 int mod_length = Length;
2374 if (Length < 0)
2375 mod_length *= -1;
2376 sprintf(len, "%i", mod_length);
2377 command += " -r ";
2378 command += len;
2379 }
2380 command += " " + Actual_Block_Device;
dianlujitao4879b372018-12-03 18:45:47 +08002381 }
2382 LOGINFO("mkfs.f2fs command: %s\n", command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02002383 if (TWFunc::Exec_Cmd(command) == 0) {
dianlujitao4879b372018-12-03 18:45:47 +08002384 if (NeedPreserveFooter)
2385 Wipe_Crypto_Key();
Dees_Troye5017042013-08-29 16:38:55 +00002386 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05002387 gui_msg("done=Done.");
Dees_Troye5017042013-08-29 16:38:55 +00002388 return true;
2389 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002390 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troye5017042013-08-29 16:38:55 +00002391 return false;
2392 }
2393 return true;
2394 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002395 LOGINFO("mkfs.f2fs binary not found, using rm -rf to wipe.\n");
Dees_Troye5017042013-08-29 16:38:55 +00002396 return Wipe_RMRF();
2397 }
2398 return false;
2399}
2400
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002401bool TWPartition::Wipe_NTFS() {
2402 string command;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002403 string Ntfsmake_Binary;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002404
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002405 if (TWFunc::Path_Exists("/sbin/mkntfs"))
2406 Ntfsmake_Binary = "mkntfs";
2407 else if (TWFunc::Path_Exists("/sbin/mkfs.ntfs"))
2408 Ntfsmake_Binary = "mkfs.ntfs";
2409 else
2410 return false;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002411
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002412 if (!UnMount(true))
2413 return false;
2414
Greg Wallace4b44fef2015-12-29 15:33:24 -05002415 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)(Ntfsmake_Binary));
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002416 Find_Actual_Block_Device();
2417 command = "/sbin/" + Ntfsmake_Binary + " " + Actual_Block_Device;
2418 if (TWFunc::Exec_Cmd(command) == 0) {
2419 Recreate_AndSec_Folder();
2420 gui_msg("done=Done.");
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002421 return true;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002422 } else {
2423 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
2424 return false;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002425 }
2426 return false;
2427}
2428
Dees_Troy51a0e822012-09-05 15:24:24 -04002429bool TWPartition::Wipe_Data_Without_Wiping_Media() {
Ethan Yonker83e82572014-04-04 10:59:28 -05002430#ifdef TW_OEM_BUILD
2431 // In an OEM Build we want to do a full format
2432 return Wipe_Encryption();
2433#else
Ethan Yonker66a19492015-12-10 10:19:45 -06002434 bool ret = false;
Dees_Troy38bd7602012-09-14 13:33:53 -04002435
Dees_Troy38bd7602012-09-14 13:33:53 -04002436 if (!Mount(true))
2437 return false;
2438
Ethan Yonker74db1572015-10-28 12:44:49 -05002439 gui_msg("wiping_data=Wiping data without wiping /data/media ...");
Ethan Yonker66a19492015-12-10 10:19:45 -06002440 ret = Wipe_Data_Without_Wiping_Media_Func(Mount_Point + "/");
2441 if (ret)
2442 gui_msg("done=Done.");
2443 return ret;
2444#endif // ifdef TW_OEM_BUILD
2445}
2446
2447bool TWPartition::Wipe_Data_Without_Wiping_Media_Func(const string& parent __unused) {
2448 string dir;
Dees_Troy38bd7602012-09-14 13:33:53 -04002449
2450 DIR* d;
Ethan Yonker66a19492015-12-10 10:19:45 -06002451 d = opendir(parent.c_str());
Dees_Troy16b74352012-11-14 22:27:31 +00002452 if (d != NULL) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002453 struct dirent* de;
2454 while ((de = readdir(d)) != NULL) {
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002455 if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) continue;
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05002456
Ethan Yonker66a19492015-12-10 10:19:45 -06002457 dir = parent;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002458 dir.append(de->d_name);
Ethan Yonker3fdcda42016-11-30 12:29:37 -06002459 if (wipe_exclusions.check_skip_dirs(dir)) {
Ethan Yonker66a19492015-12-10 10:19:45 -06002460 LOGINFO("skipped '%s'\n", dir.c_str());
2461 continue;
2462 }
Dees_Troyce675462013-01-09 19:48:21 +00002463 if (de->d_type == DT_DIR) {
Ethan Yonker66a19492015-12-10 10:19:45 -06002464 dir.append("/");
2465 if (!Wipe_Data_Without_Wiping_Media_Func(dir)) {
2466 closedir(d);
2467 return false;
2468 }
2469 rmdir(dir.c_str());
bigbiff bigbiff98f1f902013-01-19 18:46:13 -05002470 } 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 +03002471 if (unlink(dir.c_str()) != 0)
Ethan Yonker79f88bd2016-12-09 14:52:12 -06002472 LOGINFO("Unable to unlink '%s': %s\n", dir.c_str(), strerror(errno));
Dees_Troyce675462013-01-09 19:48:21 +00002473 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002474 }
2475 closedir(d);
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05002476
Dees_Troy16b74352012-11-14 22:27:31 +00002477 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -04002478 }
Ethan Yonker74db1572015-10-28 12:44:49 -05002479 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy16b74352012-11-14 22:27:31 +00002480 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04002481}
2482
dianlujitao4879b372018-12-03 18:45:47 +08002483void TWPartition::Wipe_Crypto_Key() {
2484 Find_Actual_Block_Device();
2485 if (Crypto_Key_Location.empty())
2486 return;
2487 else if (Crypto_Key_Location == "footer") {
2488 int fd = open(Actual_Block_Device.c_str(), O_RDWR);
2489 if (fd < 0) {
2490 gui_print_color("warning", "Unable to open '%s' to wipe crypto key\n", Actual_Block_Device.c_str());
2491 return;
2492 }
2493
2494 unsigned int block_count;
2495 if ((ioctl(fd, BLKGETSIZE, &block_count)) == -1) {
2496 gui_print_color("warning", "Unable to get block size for wiping crypto footer.\n");
2497 } else {
2498 int newlen = Length < 0 ? -Length : CRYPT_FOOTER_OFFSET;
2499 off64_t offset = ((off64_t)block_count * 512) - newlen;
2500 if (lseek64(fd, offset, SEEK_SET) == -1) {
2501 gui_print_color("warning", "Unable to lseek64 for wiping crypto footer.\n");
2502 } else {
2503 void* buffer = malloc(newlen);
2504 if (!buffer) {
2505 gui_print_color("warning", "Failed to malloc for wiping crypto footer.\n");
2506 } else {
2507 memset(buffer, 0, newlen);
2508 int ret = write(fd, buffer, newlen);
2509 if (ret != newlen) {
2510 gui_print_color("warning", "Failed to wipe crypto footer.\n");
2511 } else {
2512 LOGINFO("Successfully wiped crypto footer.\n");
2513 }
2514 free(buffer);
2515 }
2516 }
2517 }
2518 close(fd);
2519 } else {
2520 if (TWFunc::IOCTL_Get_Block_Size(Crypto_Key_Location.c_str()) >= 16384LLU) {
2521 string Command = "dd of='" + Crypto_Key_Location + "' if=/dev/zero bs=16384 count=1";
2522 TWFunc::Exec_Cmd(Command);
2523 } else {
2524 LOGINFO("Crypto key location reports size < 16K so not wiping crypto footer.\n");
2525 }
2526 }
2527}
2528
bigbiffce8f83c2015-12-12 18:30:21 -05002529bool TWPartition::Backup_Tar(PartitionSettings *part_settings, pid_t *tar_fork_pid) {
Ethan Yonker472f5062016-02-25 13:47:30 -06002530 string Full_FileName;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002531 twrpTar tar;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05002532
Dees_Troy43d8b002012-09-17 16:00:01 -04002533 if (!Mount(true))
2534 return false;
2535
Ian Macdonalda3b25a32019-06-16 22:39:14 +02002536 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Backup_Display_Name, gui_parse_text("{@backing}"));
Ethan Yonker74db1572015-10-28 12:44:49 -05002537 gui_msg(Msg("backing_up=Backing up {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002538
Ethan Yonker472f5062016-02-25 13:47:30 -06002539 DataManager::GetValue(TW_USE_COMPRESSION_VAR, tar.use_compression);
Matt Mowerbb81e5d2014-03-20 18:05:41 -05002540
Dees_Troy83bd4832013-05-04 12:39:56 +00002541#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
Ethan Yonker472f5062016-02-25 13:47:30 -06002542 if (Can_Encrypt_Backup) {
2543 DataManager::GetValue("tw_encrypt_backup", tar.use_encryption);
2544 if (tar.use_encryption) {
2545 if (Use_Userdata_Encryption)
2546 tar.userdata_encryption = tar.use_encryption;
2547 string Password;
2548 DataManager::GetValue("tw_backup_password", Password);
2549 tar.setpassword(Password);
2550 } else {
2551 tar.use_encryption = 0;
2552 }
Dees_Troy83bd4832013-05-04 12:39:56 +00002553 }
2554#endif
Dees_Troy43d8b002012-09-17 16:00:01 -04002555
Ethan Yonker472f5062016-02-25 13:47:30 -06002556 Backup_FileName = Backup_Name + "." + Current_File_System + ".win";
Ethan Yonkere080c1f2016-09-19 13:50:25 -05002557 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
Ethan Yonker01f4e032017-02-03 15:30:52 -06002558 if (Has_Data_Media)
2559 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 -05002560 tar.part_settings = part_settings;
Ethan Yonker3fdcda42016-11-30 12:29:37 -06002561 tar.backup_exclusions = &backup_exclusions;
Dees Troye0a433a2013-12-02 04:10:37 +00002562 tar.setdir(Backup_Path);
2563 tar.setfn(Full_FileName);
2564 tar.setsize(Backup_Size);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002565 tar.partition_name = Backup_Name;
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002566 tar.backup_folder = part_settings->Backup_Folder;
bigbiffce8f83c2015-12-12 18:30:21 -05002567 if (tar.createTarFork(tar_fork_pid) != 0)
Dees Troye0a433a2013-12-02 04:10:37 +00002568 return false;
Dees_Troy43d8b002012-09-17 16:00:01 -04002569 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002570}
2571
bigbiffce8f83c2015-12-12 18:30:21 -05002572bool TWPartition::Backup_Image(PartitionSettings *part_settings) {
2573 string Full_FileName, adb_file_name;
igoriok87e3d932013-01-31 21:03:53 +02002574
Ethan Yonker74db1572015-10-28 12:44:49 -05002575 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, gui_parse_text("{@backing}"));
2576 gui_msg(Msg("backing_up=Backing up {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002577
Ethan Yonker472f5062016-02-25 13:47:30 -06002578 Backup_FileName = Backup_Name + "." + Current_File_System + ".win";
Dees_Troy43d8b002012-09-17 16:00:01 -04002579
bigbiffce8f83c2015-12-12 18:30:21 -05002580 if (part_settings->adbbackup) {
2581 Full_FileName = TW_ADB_BACKUP;
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002582 adb_file_name = part_settings->Backup_Folder + "/" + Backup_FileName;
bigbiffce8f83c2015-12-12 18:30:21 -05002583 }
2584 else
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002585 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
bigbiffce8f83c2015-12-12 18:30:21 -05002586
2587 part_settings->total_restore_size = Backup_Size;
2588
2589 if (part_settings->adbbackup) {
2590 if (!twadbbu::Write_TWIMG(adb_file_name, Backup_Size))
2591 return false;
2592 }
2593
2594 if (!Raw_Read_Write(part_settings))
Ethan Yonker472f5062016-02-25 13:47:30 -06002595 return false;
2596
bigbiffce8f83c2015-12-12 18:30:21 -05002597 if (part_settings->adbbackup) {
2598 if (!twadbbu::Write_TWEOF())
2599 return false;
Dees_Troy7c2dec82012-09-26 09:49:14 -04002600 }
Dees_Troy43d8b002012-09-17 16:00:01 -04002601 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002602}
2603
bigbiffce8f83c2015-12-12 18:30:21 -05002604bool TWPartition::Raw_Read_Write(PartitionSettings *part_settings) {
2605 unsigned long long RW_Block_Size, Remain = Backup_Size;
2606 int src_fd = -1, dest_fd = -1;
2607 ssize_t bs;
Ethan Yonker472f5062016-02-25 13:47:30 -06002608 bool ret = false;
2609 void* buffer = NULL;
2610 unsigned long long backedup_size = 0;
bigbiffce8f83c2015-12-12 18:30:21 -05002611 string srcfn, destfn;
Ethan Yonker472f5062016-02-25 13:47:30 -06002612
bigbiffce8f83c2015-12-12 18:30:21 -05002613 if (part_settings->PM_Method == PM_BACKUP) {
2614 srcfn = Actual_Block_Device;
2615 if (part_settings->adbbackup)
2616 destfn = TW_ADB_BACKUP;
bigbiff bigbiff38b83c12017-12-28 19:58:52 -05002617 else {
Ethan Yonkere080c1f2016-09-19 13:50:25 -05002618 destfn = part_settings->Backup_Folder + "/" + Backup_FileName;
bigbiff bigbiff38b83c12017-12-28 19:58:52 -05002619 }
bigbiffce8f83c2015-12-12 18:30:21 -05002620 }
2621 else {
2622 destfn = Actual_Block_Device;
2623 if (part_settings->adbbackup) {
2624 srcfn = TW_ADB_RESTORE;
2625 } else {
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002626 srcfn = part_settings->Backup_Folder + "/" + Backup_FileName;
bigbiffce8f83c2015-12-12 18:30:21 -05002627 Remain = TWFunc::Get_File_Size(srcfn);
2628 }
2629 }
Ethan Yonker472f5062016-02-25 13:47:30 -06002630
bigbiffce8f83c2015-12-12 18:30:21 -05002631 src_fd = open(srcfn.c_str(), O_RDONLY | O_LARGEFILE);
Ethan Yonker472f5062016-02-25 13:47:30 -06002632 if (src_fd < 0) {
bigbiffce8f83c2015-12-12 18:30:21 -05002633 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(srcfn.c_str())(strerror(errno)));
Ethan Yonker472f5062016-02-25 13:47:30 -06002634 return false;
2635 }
bigbiffce8f83c2015-12-12 18:30:21 -05002636
2637 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 -06002638 if (dest_fd < 0) {
bigbiffce8f83c2015-12-12 18:30:21 -05002639 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(destfn.c_str())(strerror(errno)));
Ethan Yonker472f5062016-02-25 13:47:30 -06002640 goto exit;
2641 }
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -04002642
bigbiffce8f83c2015-12-12 18:30:21 -05002643 LOGINFO("Reading '%s', writing '%s'\n", srcfn.c_str(), destfn.c_str());
2644
2645 if (part_settings->adbbackup) {
2646 RW_Block_Size = MAX_ADB_READ;
2647 bs = MAX_ADB_READ;
2648 }
2649 else {
2650 RW_Block_Size = 1048576LLU; // 1MB
2651 bs = (ssize_t)(RW_Block_Size);
2652 }
2653
Ethan Yonker472f5062016-02-25 13:47:30 -06002654 buffer = malloc((size_t)bs);
2655 if (!buffer) {
2656 LOGINFO("Raw_Read_Write failed to malloc\n");
2657 goto exit;
2658 }
bigbiffce8f83c2015-12-12 18:30:21 -05002659
2660 if (part_settings->progress)
2661 part_settings->progress->SetPartitionSize(part_settings->total_restore_size);
2662
Ethan Yonker472f5062016-02-25 13:47:30 -06002663 while (Remain > 0) {
2664 if (Remain < RW_Block_Size)
bigbiffce8f83c2015-12-12 18:30:21 -05002665 bs = (ssize_t)(Remain);
Matt Mowera8a89d12016-12-30 18:10:37 -06002666 if (read(src_fd, buffer, bs) != bs) {
Ethan Yonker472f5062016-02-25 13:47:30 -06002667 LOGINFO("Error reading source fd (%s)\n", strerror(errno));
2668 goto exit;
2669 }
2670 if (write(dest_fd, buffer, bs) != bs) {
2671 LOGINFO("Error writing destination fd (%s)\n", strerror(errno));
2672 goto exit;
2673 }
2674 backedup_size += (unsigned long long)(bs);
Matt Mower029a82d2017-01-08 13:12:38 -06002675 Remain -= (unsigned long long)(bs);
bigbiffce8f83c2015-12-12 18:30:21 -05002676 if (part_settings->progress)
2677 part_settings->progress->UpdateSize(backedup_size);
Ethan Yonker472f5062016-02-25 13:47:30 -06002678 if (PartitionManager.Check_Backup_Cancel() != 0)
2679 goto exit;
2680 }
bigbiffce8f83c2015-12-12 18:30:21 -05002681 if (part_settings->progress)
2682 part_settings->progress->UpdateDisplayDetails(true);
Ethan Yonker472f5062016-02-25 13:47:30 -06002683 fsync(dest_fd);
nailyka083dc62016-11-11 21:41:28 +01002684
2685 if (!part_settings->adbbackup && part_settings->PM_Method == PM_BACKUP) {
2686 tw_set_default_metadata(destfn.c_str());
2687 LOGINFO("Restored default metadata for %s\n", destfn.c_str());
2688 }
2689
Ethan Yonker472f5062016-02-25 13:47:30 -06002690 ret = true;
2691exit:
2692 if (src_fd >= 0)
2693 close(src_fd);
2694 if (dest_fd >= 0)
2695 close(dest_fd);
2696 if (buffer)
2697 free(buffer);
2698 return ret;
2699}
2700
bigbiffce8f83c2015-12-12 18:30:21 -05002701bool TWPartition::Backup_Dump_Image(PartitionSettings *part_settings) {
Vojtech Bocek05534202013-09-11 08:11:56 +02002702 string Full_FileName, Command;
Dees_Troy43d8b002012-09-17 16:00:01 -04002703
Ethan Yonker74db1572015-10-28 12:44:49 -05002704 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, gui_parse_text("{@backing}"));
2705 gui_msg(Msg("backing_up=Backing up {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002706
bigbiffce8f83c2015-12-12 18:30:21 -05002707 if (part_settings->progress)
2708 part_settings->progress->SetPartitionSize(Backup_Size);
Dees_Troy43d8b002012-09-17 16:00:01 -04002709
Ethan Yonker472f5062016-02-25 13:47:30 -06002710 Backup_FileName = Backup_Name + "." + Current_File_System + ".win";
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002711 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04002712
2713 Command = "dump_image " + MTD_Name + " '" + Full_FileName + "'";
bigbiffce8f83c2015-12-12 18:30:21 -05002714
Dees_Troy2673cec2013-04-02 20:22:16 +00002715 LOGINFO("Backup command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02002716 TWFunc::Exec_Cmd(Command);
Ethan Yonker4b94cfd2014-12-11 10:00:45 -06002717 tw_set_default_metadata(Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04002718 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
2719 // 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 -06002720 gui_msg(Msg(msg::kError, "backup_size=Backup file size for '{1}' is 0 bytes.")(Full_FileName));
Dees_Troy7c2dec82012-09-26 09:49:14 -04002721 return false;
2722 }
bigbiffce8f83c2015-12-12 18:30:21 -05002723 if (part_settings->progress)
2724 part_settings->progress->UpdateSize(Backup_Size);
2725
Dees_Troy43d8b002012-09-17 16:00:01 -04002726 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002727}
2728
bigbiffce8f83c2015-12-12 18:30:21 -05002729unsigned long long TWPartition::Get_Restore_Size(PartitionSettings *part_settings) {
2730 if (!part_settings->adbbackup) {
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002731 InfoManager restore_info(part_settings->Backup_Folder + "/" + Backup_Name + ".info");
bigbiffce8f83c2015-12-12 18:30:21 -05002732 if (restore_info.LoadValues() == 0) {
2733 if (restore_info.GetValue("backup_size", Restore_Size) == 0) {
2734 LOGINFO("Read info file, restore size is %llu\n", Restore_Size);
2735 return Restore_Size;
2736 }
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002737 }
2738 }
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002739
Matt Mower029a82d2017-01-08 13:12:38 -06002740 string Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
2741 string Restore_File_System = Get_Restore_File_System(part_settings);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002742
2743 if (Is_Image(Restore_File_System)) {
2744 Restore_Size = TWFunc::Get_File_Size(Full_FileName);
2745 return Restore_Size;
2746 }
2747
2748 twrpTar tar;
2749 tar.setdir(Backup_Path);
2750 tar.setfn(Full_FileName);
bigbiffce8f83c2015-12-12 18:30:21 -05002751 tar.backup_name = Full_FileName;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002752#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
2753 string Password;
2754 DataManager::GetValue("tw_restore_password", Password);
2755 if (!Password.empty())
2756 tar.setpassword(Password);
2757#endif
2758 tar.partition_name = Backup_Name;
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002759 tar.backup_folder = part_settings->Backup_Folder;
bigbiffce8f83c2015-12-12 18:30:21 -05002760 tar.part_settings = part_settings;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002761 Restore_Size = tar.get_size();
2762 return Restore_Size;
2763}
2764
bigbiffce8f83c2015-12-12 18:30:21 -05002765bool TWPartition::Restore_Tar(PartitionSettings *part_settings) {
Ethan Yonker472f5062016-02-25 13:47:30 -06002766 string Full_FileName;
Dees Troy4159aed2014-02-28 17:24:43 +00002767 bool ret = false;
bigbiffce8f83c2015-12-12 18:30:21 -05002768 string Restore_File_System = Get_Restore_File_System(part_settings);
Dees_Troy43d8b002012-09-17 16:00:01 -04002769
Dees_Troye58d5262012-09-21 12:27:57 -04002770 if (Has_Android_Secure) {
Dees_Troye58d5262012-09-21 12:27:57 -04002771 if (!Wipe_AndSec())
2772 return false;
Gary Peck43acadf2012-11-21 21:19:01 -08002773 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002774 gui_msg(Msg("wiping=Wiping {1}")(Backup_Display_Name));
Ethan Yonker5eac2222014-06-11 12:22:55 -05002775 if (Has_Data_Media && Mount_Point == "/data" && Restore_File_System != Current_File_System) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002776 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 -05002777 if (!Wipe_Data_Without_Wiping_Media())
2778 return false;
2779 } else {
2780 if (!Wipe(Restore_File_System))
2781 return false;
2782 }
Dees_Troye58d5262012-09-21 12:27:57 -04002783 }
Matt Mower3c366972015-12-25 19:28:31 -06002784 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Backup_Display_Name, gui_parse_text("{@restoring_hdr}"));
Ethan Yonker74db1572015-10-28 12:44:49 -05002785 gui_msg(Msg("restoring=Restoring {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002786
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05002787 // Remount as read/write as needed so we can restore the backup
2788 if (!ReMount_RW(true))
Dees_Troy43d8b002012-09-17 16:00:01 -04002789 return false;
2790
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002791 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
Ethan Yonker87af5632014-02-10 11:56:35 -06002792 twrpTar tar;
bigbiffce8f83c2015-12-12 18:30:21 -05002793 tar.part_settings = part_settings;
Ethan Yonker87af5632014-02-10 11:56:35 -06002794 tar.setdir(Backup_Path);
2795 tar.setfn(Full_FileName);
2796 tar.backup_name = Backup_Name;
2797#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
2798 string Password;
2799 DataManager::GetValue("tw_restore_password", Password);
2800 if (!Password.empty())
2801 tar.setpassword(Password);
2802#endif
bigbiffce8f83c2015-12-12 18:30:21 -05002803 part_settings->progress->SetPartitionSize(Get_Restore_Size(part_settings));
2804 if (tar.extractTarFork() != 0)
Dees Troy4159aed2014-02-28 17:24:43 +00002805 ret = false;
2806 else
2807 ret = true;
2808#ifdef HAVE_CAPABILITIES
2809 // Restore capabilities to the run-as binary
Captain Throwback9d6feb52018-07-27 10:05:24 -04002810 if (Mount_Point == PartitionManager.Get_Android_Root_Path() && Mount(true) && TWFunc::Path_Exists("/system/bin/run-as")) {
Dees Troy4159aed2014-02-28 17:24:43 +00002811 struct vfs_cap_data cap_data;
2812 uint64_t capabilities = (1 << CAP_SETUID) | (1 << CAP_SETGID);
2813
2814 memset(&cap_data, 0, sizeof(cap_data));
2815 cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE;
2816 cap_data.data[0].permitted = (uint32_t) (capabilities & 0xffffffff);
2817 cap_data.data[0].inheritable = 0;
2818 cap_data.data[1].permitted = (uint32_t) (capabilities >> 32);
2819 cap_data.data[1].inheritable = 0;
2820 if (setxattr("/system/bin/run-as", XATTR_NAME_CAPS, &cap_data, sizeof(cap_data), 0) < 0) {
2821 LOGINFO("Failed to reset capabilities of /system/bin/run-as binary.\n");
2822 } else {
2823 LOGINFO("Reset capabilities of /system/bin/run-as binary successful.\n");
2824 }
2825 }
2826#endif
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05002827 if (Mount_Read_Only || Mount_Flags & MS_RDONLY)
2828 // Remount as read only when restoration is complete
2829 ReMount(true);
2830
Dees Troy4159aed2014-02-28 17:24:43 +00002831 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04002832}
2833
bigbiffce8f83c2015-12-12 18:30:21 -05002834bool TWPartition::Restore_Image(PartitionSettings *part_settings) {
Ethan Yonker96af84a2015-01-05 14:58:36 -06002835 string Full_FileName;
bigbiffce8f83c2015-12-12 18:30:21 -05002836 string Restore_File_System = Get_Restore_File_System(part_settings);
Dees_Troy43d8b002012-09-17 16:00:01 -04002837
Matt Mower3c366972015-12-25 19:28:31 -06002838 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Backup_Display_Name, gui_parse_text("{@restoring_hdr}"));
Ethan Yonker74db1572015-10-28 12:44:49 -05002839 gui_msg(Msg("restoring=Restoring {1}...")(Backup_Display_Name));
bigbiffce8f83c2015-12-12 18:30:21 -05002840
2841 if (part_settings->adbbackup)
2842 Full_FileName = TW_ADB_RESTORE;
2843 else
Ethan Yonkere080c1f2016-09-19 13:50:25 -05002844 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
Gary Peck15e623d2012-11-21 21:07:58 -08002845
Ethan Yonker96af84a2015-01-05 14:58:36 -06002846 if (Restore_File_System == "emmc") {
bigbiffce8f83c2015-12-12 18:30:21 -05002847 if (!part_settings->adbbackup)
2848 part_settings->total_restore_size = (uint64_t)(TWFunc::Get_File_Size(Full_FileName));
2849 if (!Raw_Read_Write(part_settings))
Ethan Yonker96af84a2015-01-05 14:58:36 -06002850 return false;
2851 } else if (Restore_File_System == "mtd" || Restore_File_System == "bml") {
bigbiffce8f83c2015-12-12 18:30:21 -05002852 if (!Flash_Image_FI(Full_FileName, part_settings->progress))
2853 return false;
2854 }
2855
2856 if (part_settings->adbbackup) {
2857 if (!twadbbu::Write_TWEOF())
Ethan Yonker96af84a2015-01-05 14:58:36 -06002858 return false;
Gary Peck15e623d2012-11-21 21:07:58 -08002859 }
Dees_Troy43d8b002012-09-17 16:00:01 -04002860 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002861}
Dees_Troy5bf43922012-09-07 16:07:55 -04002862
2863bool TWPartition::Update_Size(bool Display_Error) {
Dees_Troy0550cfb2012-10-13 11:56:13 -04002864 bool ret = false, Was_Already_Mounted = false;
Dees_Troy51127312012-09-08 13:08:49 -04002865
Ethan Yonker1b190162016-12-05 15:25:19 -06002866 Find_Actual_Block_Device();
2867
2868 if (!Can_Be_Mounted && !Is_Encrypted) {
2869 if (TWFunc::Path_Exists(Actual_Block_Device) && Find_Partition_Size()) {
2870 Used = Size;
2871 Backup_Size = Size;
2872 return true;
2873 }
Dees_Troy5bf43922012-09-07 16:07:55 -04002874 return false;
Ethan Yonker1b190162016-12-05 15:25:19 -06002875 }
Dees_Troy5bf43922012-09-07 16:07:55 -04002876
Dees_Troy0550cfb2012-10-13 11:56:13 -04002877 Was_Already_Mounted = Is_Mounted();
Dees_Troy38bd7602012-09-14 13:33:53 -04002878 if (Removable || Is_Encrypted) {
2879 if (!Mount(false))
2880 return true;
2881 } else if (!Mount(Display_Error))
Dees_Troy5bf43922012-09-07 16:07:55 -04002882 return false;
Dees_Troy51127312012-09-08 13:08:49 -04002883
2884 ret = Get_Size_Via_statfs(Display_Error);
Dees_Troy0550cfb2012-10-13 11:56:13 -04002885 if (!ret || Size == 0) {
2886 if (!Get_Size_Via_df(Display_Error)) {
2887 if (!Was_Already_Mounted)
2888 UnMount(false);
Dees_Troy51127312012-09-08 13:08:49 -04002889 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04002890 }
2891 }
Dees_Troy51127312012-09-08 13:08:49 -04002892
Dees_Troy5bf43922012-09-07 16:07:55 -04002893 if (Has_Data_Media) {
2894 if (Mount(Display_Error)) {
Ethan Yonker3fdcda42016-11-30 12:29:37 -06002895 Used = backup_exclusions.Get_Folder_Size(Mount_Point);
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002896 Backup_Size = Used;
2897 int bak = (int)(Used / 1048576LLU);
Dees_Troy51127312012-09-08 13:08:49 -04002898 int fre = (int)(Free / 1048576LLU);
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002899 LOGINFO("Data backup size is %iMB, free: %iMB.\n", bak, fre);
Dees_Troy0550cfb2012-10-13 11:56:13 -04002900 } else {
2901 if (!Was_Already_Mounted)
2902 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04002903 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04002904 }
Dees_Troye58d5262012-09-21 12:27:57 -04002905 } else if (Has_Android_Secure) {
2906 if (Mount(Display_Error))
Ethan Yonker3fdcda42016-11-30 12:29:37 -06002907 Backup_Size = backup_exclusions.Get_Folder_Size(Backup_Path);
Dees_Troy0550cfb2012-10-13 11:56:13 -04002908 else {
2909 if (!Was_Already_Mounted)
2910 UnMount(false);
Dees_Troye58d5262012-09-21 12:27:57 -04002911 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04002912 }
Dees_Troy5bf43922012-09-07 16:07:55 -04002913 }
Dees_Troy0550cfb2012-10-13 11:56:13 -04002914 if (!Was_Already_Mounted)
2915 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04002916 return true;
Dees_Troy51127312012-09-08 13:08:49 -04002917}
Dees_Troy38bd7602012-09-14 13:33:53 -04002918
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06002919bool TWPartition::Find_Wildcard_Block_Devices(const string& Device) {
2920 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
2921 string Path = TWFunc::Get_Path(Device);
2922 string Dev = TWFunc::Get_Filename(Device);
2923 size_t wildcard_index = Dev.find("*");
2924 if (wildcard_index != string::npos)
2925 Dev = Dev.substr(0, wildcard_index);
2926 wildcard_index = Dev.size();
2927 DIR* d = opendir(Path.c_str());
2928 if (d == NULL) {
2929 LOGINFO("Error opening '%s': %s\n", Path.c_str(), strerror(errno));
2930 return false;
2931 }
2932 struct dirent* de;
2933 while ((de = readdir(d)) != NULL) {
2934 if (de->d_type != DT_BLK || strlen(de->d_name) <= wildcard_index || strncmp(de->d_name, Dev.c_str(), wildcard_index) != 0)
2935 continue;
2936
2937 string item = Path + "/";
2938 item.append(de->d_name);
2939 if (PartitionManager.Find_Partition_By_Block_Device(item))
2940 continue;
2941 TWPartition *part = new TWPartition;
2942 char buffer[MAX_FSTAB_LINE_LENGTH];
2943 sprintf(buffer, "%s %s-%i auto defaults defaults", item.c_str(), Mount_Point.c_str(), ++mount_point_index);
Chaosmasterf6e42ce2020-01-27 00:17:04 +01002944 part->Process_Fstab_Line(buffer, false, NULL, false);
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06002945 char display[MAX_FSTAB_LINE_LENGTH];
2946 sprintf(display, "%s %i", Storage_Name.c_str(), mount_point_index);
2947 part->Storage_Name = display;
2948 part->Display_Name = display;
2949 part->Primary_Block_Device = item;
2950 part->Wildcard_Block_Device = false;
2951 part->Is_SubPartition = true;
2952 part->SubPartition_Of = Mount_Point;
2953 part->Is_Storage = Is_Storage;
2954 part->Can_Be_Mounted = true;
2955 part->Removable = true;
2956 part->Can_Be_Wiped = Can_Be_Wiped;
2957 part->Wipe_Available_in_GUI = Wipe_Available_in_GUI;
2958 part->Find_Actual_Block_Device();
2959 part->Update_Size(false);
2960 Has_SubPartition = true;
2961 PartitionManager.Output_Partition(part);
2962 PartitionManager.Add_Partition(part);
2963 }
2964 closedir(d);
2965 return (mount_point_index > 0);
2966}
2967
Dees_Troy38bd7602012-09-14 13:33:53 -04002968void TWPartition::Find_Actual_Block_Device(void) {
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06002969 if (!Sysfs_Entry.empty() && Primary_Block_Device.empty() && Decrypted_Block_Device.empty()) {
2970 /* Sysfs_Entry.empty() indicates if this is a sysfs entry that begins with /device/
2971 * If we have a syfs entry then we are looking for this device from a uevent add.
2972 * The uevent add will set the primary block device based on the data we receive from
2973 * after checking for adopted storage. If the device ends up being adopted, then the
2974 * decrypted block device will be set instead of the primary block device. */
2975 Is_Present = false;
2976 return;
2977 }
2978 if (Wildcard_Block_Device && !Is_Adopted_Storage) {
2979 Is_Present = false;
2980 Actual_Block_Device = "";
2981 Can_Be_Mounted = false;
2982 if (!Find_Wildcard_Block_Devices(Primary_Block_Device)) {
2983 string Dev = Primary_Block_Device.substr(0, Primary_Block_Device.find("*"));
2984 if (TWFunc::Path_Exists(Dev)) {
2985 Is_Present = true;
2986 Can_Be_Mounted = true;
2987 Actual_Block_Device = Dev;
2988 }
2989 }
2990 return;
2991 } else if (Is_Decrypted && !Decrypted_Block_Device.empty()) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002992 Actual_Block_Device = Decrypted_Block_Device;
Ethan Yonker1b190162016-12-05 15:25:19 -06002993 if (TWFunc::Path_Exists(Decrypted_Block_Device)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002994 Is_Present = true;
Ethan Yonker1b190162016-12-05 15:25:19 -06002995 return;
2996 }
2997 } else if (SlotSelect && TWFunc::Path_Exists(Primary_Block_Device + PartitionManager.Get_Active_Slot_Suffix())) {
2998 Actual_Block_Device = Primary_Block_Device + PartitionManager.Get_Active_Slot_Suffix();
2999 unlink(Primary_Block_Device.c_str());
3000 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
3001 Is_Present = true;
3002 return;
Dees_Troy43d8b002012-09-17 16:00:01 -04003003 } else if (TWFunc::Path_Exists(Primary_Block_Device)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04003004 Is_Present = true;
3005 Actual_Block_Device = Primary_Block_Device;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05003006 return;
3007 }
Ethan Yonker1b190162016-12-05 15:25:19 -06003008 if (!Alternate_Block_Device.empty() && TWFunc::Path_Exists(Alternate_Block_Device)) {
Dees_Troy3f04d032012-10-07 18:20:09 -04003009 Actual_Block_Device = Alternate_Block_Device;
Dees_Troy38bd7602012-09-14 13:33:53 -04003010 Is_Present = true;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05003011 } else {
Dees_Troy38bd7602012-09-14 13:33:53 -04003012 Is_Present = false;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05003013 }
Dees_Troy38bd7602012-09-14 13:33:53 -04003014}
3015
3016void TWPartition::Recreate_Media_Folder(void) {
3017 string Command;
Ethan Yonker66a19492015-12-10 10:19:45 -06003018 string Media_Path = Mount_Point + "/media";
Dees_Troy38bd7602012-09-14 13:33:53 -04003019
Ethan Yonkerbd7492d2016-12-07 13:55:01 -06003020 if (Is_FBE) {
3021 LOGINFO("Not recreating media folder on FBE\n");
3022 return;
3023 }
Dees_Troy38bd7602012-09-14 13:33:53 -04003024 if (!Mount(true)) {
Ethan Yonker66a19492015-12-10 10:19:45 -06003025 gui_msg(Msg(msg::kError, "recreate_folder_err=Unable to recreate {1} folder.")(Media_Path));
3026 } else if (!TWFunc::Path_Exists(Media_Path)) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05003027 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Ethan Yonker66a19492015-12-10 10:19:45 -06003028 LOGINFO("Recreating %s folder.\n", Media_Path.c_str());
3029 mkdir(Media_Path.c_str(), 0770);
xiaolu9416f4f2015-06-04 08:22:23 +08003030 string Internal_path = DataManager::GetStrValue("tw_internal_path");
3031 if (!Internal_path.empty()) {
3032 LOGINFO("Recreating %s folder.\n", Internal_path.c_str());
3033 mkdir(Internal_path.c_str(), 0770);
3034 }
3035#ifdef TW_INTERNAL_STORAGE_PATH
3036 mkdir(EXPAND(TW_INTERNAL_STORAGE_PATH), 0770);
3037#endif
Matt Mower87413642017-01-17 21:14:46 -06003038
thata3d31fb2014-12-21 22:27:40 +01003039 // Afterwards, we will try to set the
3040 // default metadata that we were hopefully able to get during
3041 // early boot.
Ethan Yonker66a19492015-12-10 10:19:45 -06003042 tw_set_default_metadata(Media_Path.c_str());
xiaolu9416f4f2015-06-04 08:22:23 +08003043 if (!Internal_path.empty())
3044 tw_set_default_metadata(Internal_path.c_str());
Matt Mower87413642017-01-17 21:14:46 -06003045
Ethan Yonker5eac2222014-06-11 12:22:55 -05003046 // Toggle mount to ensure that "internal sdcard" gets mounted
bigbiff bigbiff9c754052013-01-09 09:09:08 -05003047 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Ethan Yonker5eac2222014-06-11 12:22:55 -05003048 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Dees_Troy38bd7602012-09-14 13:33:53 -04003049 }
Dees_Troy43d8b002012-09-17 16:00:01 -04003050}
Dees_Troye58d5262012-09-21 12:27:57 -04003051
3052void TWPartition::Recreate_AndSec_Folder(void) {
Dees_Troye58d5262012-09-21 12:27:57 -04003053 if (!Has_Android_Secure)
3054 return;
Dees_Troy2673cec2013-04-02 20:22:16 +00003055 LOGINFO("Creating %s: %s\n", Backup_Display_Name.c_str(), Symlink_Path.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -04003056 if (!Mount(true)) {
Ethan Yonker74db1572015-10-28 12:44:49 -05003057 gui_msg(Msg(msg::kError, "recreate_folder_err=Unable to recreate {1} folder.")(Backup_Name));
Dees_Troye58d5262012-09-21 12:27:57 -04003058 } else if (!TWFunc::Path_Exists(Symlink_Path)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00003059 LOGINFO("Recreating %s folder.\n", Backup_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05003060 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05003061 mkdir(Symlink_Path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
bigbiff bigbiff9c754052013-01-09 09:09:08 -05003062 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04003063 }
3064}
bigbiff7cb4c332014-11-26 20:36:07 -05003065
3066uint64_t TWPartition::Get_Max_FileSize() {
3067 uint64_t maxFileSize = 0;
3068 const uint64_t constGB = (uint64_t) 1024 * 1024 * 1024;
3069 const uint64_t constTB = (uint64_t) constGB * 1024;
3070 const uint64_t constPB = (uint64_t) constTB * 1024;
bigbiff0c532032014-12-21 13:41:26 -05003071 if (Current_File_System == "ext4")
3072 maxFileSize = 16 * constTB; //16 TB
3073 else if (Current_File_System == "vfat")
3074 maxFileSize = 4 * constGB; //4 GB
3075 else if (Current_File_System == "ntfs")
3076 maxFileSize = 256 * constTB; //256 TB
3077 else if (Current_File_System == "exfat")
3078 maxFileSize = 16 * constPB; //16 PB
3079 else if (Current_File_System == "ext3")
3080 maxFileSize = 2 * constTB; //2 TB
3081 else if (Current_File_System == "f2fs")
3082 maxFileSize = 3.94 * constTB; //3.94 TB
bigbiff7cb4c332014-11-26 20:36:07 -05003083 else
3084 maxFileSize = 100000000L;
3085 return maxFileSize - 1;
3086}
3087
bigbiffce8f83c2015-12-12 18:30:21 -05003088bool TWPartition::Flash_Image(PartitionSettings *part_settings) {
3089 string Restore_File_System, full_filename;
Ethan Yonker96af84a2015-01-05 14:58:36 -06003090
Ethan Yonkere080c1f2016-09-19 13:50:25 -05003091 full_filename = part_settings->Backup_Folder + "/" + Backup_FileName;
Ethan Yonker96af84a2015-01-05 14:58:36 -06003092
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05003093 LOGINFO("Image filename is: %s\n", Backup_FileName.c_str());
bigbiffce8f83c2015-12-12 18:30:21 -05003094
3095 if (Backup_Method == BM_FILES) {
Ethan Yonker96af84a2015-01-05 14:58:36 -06003096 LOGERR("Cannot flash images to file systems\n");
3097 return false;
3098 } else if (!Can_Flash_Img) {
3099 LOGERR("Cannot flash images to partitions %s\n", Display_Name.c_str());
3100 return false;
3101 } else {
3102 if (!Find_Partition_Size()) {
3103 LOGERR("Unable to find partition size for '%s'\n", Mount_Point.c_str());
3104 return false;
3105 }
bigbiffce8f83c2015-12-12 18:30:21 -05003106 unsigned long long image_size = TWFunc::Get_File_Size(full_filename);
Ethan Yonker96af84a2015-01-05 14:58:36 -06003107 if (image_size > Size) {
Ethan Yonker74db1572015-10-28 12:44:49 -05003108 LOGINFO("Size (%llu bytes) of image '%s' is larger than target device '%s' (%llu bytes)\n",
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05003109 image_size, Backup_FileName.c_str(), Actual_Block_Device.c_str(), Size);
Ethan Yonker74db1572015-10-28 12:44:49 -05003110 gui_err("img_size_err=Size of image is larger than target device");
Ethan Yonker96af84a2015-01-05 14:58:36 -06003111 return false;
3112 }
bigbiffce8f83c2015-12-12 18:30:21 -05003113 if (Backup_Method == BM_DD) {
3114 if (!part_settings->adbbackup) {
3115 if (Is_Sparse_Image(full_filename)) {
3116 return Flash_Sparse_Image(full_filename);
3117 }
Ethan Yonker472f5062016-02-25 13:47:30 -06003118 }
bigbiffce8f83c2015-12-12 18:30:21 -05003119 return Raw_Read_Write(part_settings);
3120 } else if (Backup_Method == BM_FLASH_UTILS) {
3121 return Flash_Image_FI(full_filename, NULL);
Ethan Yonker472f5062016-02-25 13:47:30 -06003122 }
Ethan Yonker96af84a2015-01-05 14:58:36 -06003123 }
3124
3125 LOGERR("Unknown flash method for '%s'\n", Mount_Point.c_str());
3126 return false;
3127}
3128
Ethan Yonker472f5062016-02-25 13:47:30 -06003129bool TWPartition::Is_Sparse_Image(const string& Filename) {
HashBanged974bb2016-01-30 14:20:09 -05003130 uint32_t magic = 0;
3131 int fd = open(Filename.c_str(), O_RDONLY);
3132 if (fd < 0) {
3133 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Filename)(strerror(errno)));
3134 return false;
3135 }
bigbiffce8f83c2015-12-12 18:30:21 -05003136
HashBanged974bb2016-01-30 14:20:09 -05003137 if (read(fd, &magic, sizeof(magic)) != sizeof(magic)) {
3138 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Filename)(strerror(errno)));
3139 close(fd);
3140 return false;
3141 }
3142 close(fd);
Ethan Yonker472f5062016-02-25 13:47:30 -06003143 if (magic == SPARSE_HEADER_MAGIC)
3144 return true;
3145 return false;
3146}
3147
3148bool TWPartition::Flash_Sparse_Image(const string& Filename) {
3149 string Command;
3150
3151 gui_msg(Msg("flashing=Flashing {1}...")(Display_Name));
3152
3153 Command = "simg2img '" + Filename + "' '" + Actual_Block_Device + "'";
Ethan Yonker96af84a2015-01-05 14:58:36 -06003154 LOGINFO("Flash command: '%s'\n", Command.c_str());
3155 TWFunc::Exec_Cmd(Command);
3156 return true;
3157}
3158
Ethan Yonker472f5062016-02-25 13:47:30 -06003159bool TWPartition::Flash_Image_FI(const string& Filename, ProgressTracking *progress) {
Ethan Yonker96af84a2015-01-05 14:58:36 -06003160 string Command;
Ethan Yonker472f5062016-02-25 13:47:30 -06003161 unsigned long long file_size;
Ethan Yonker96af84a2015-01-05 14:58:36 -06003162
Ethan Yonker74db1572015-10-28 12:44:49 -05003163 gui_msg(Msg("flashing=Flashing {1}...")(Display_Name));
Ethan Yonker472f5062016-02-25 13:47:30 -06003164 if (progress) {
3165 file_size = (unsigned long long)(TWFunc::Get_File_Size(Filename));
3166 progress->SetPartitionSize(file_size);
3167 }
Ethan Yonker96af84a2015-01-05 14:58:36 -06003168 // Sometimes flash image doesn't like to flash due to the first 2KB matching, so we erase first to ensure that it flashes
3169 Command = "erase_image " + MTD_Name;
3170 LOGINFO("Erase command: '%s'\n", Command.c_str());
3171 TWFunc::Exec_Cmd(Command);
3172 Command = "flash_image " + MTD_Name + " '" + Filename + "'";
3173 LOGINFO("Flash command: '%s'\n", Command.c_str());
3174 TWFunc::Exec_Cmd(Command);
Ethan Yonker472f5062016-02-25 13:47:30 -06003175 if (progress)
3176 progress->UpdateSize(file_size);
Ethan Yonker96af84a2015-01-05 14:58:36 -06003177 return true;
3178}
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05003179
3180void TWPartition::Change_Mount_Read_Only(bool new_value) {
3181 Mount_Read_Only = new_value;
3182}
3183
bigbiffce8f83c2015-12-12 18:30:21 -05003184bool TWPartition::Is_Read_Only() {
3185 return Mount_Read_Only;
3186}
3187
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05003188int TWPartition::Check_Lifetime_Writes() {
3189 bool original_read_only = Mount_Read_Only;
3190 int ret = 1;
3191
3192 Mount_Read_Only = true;
3193 if (Mount(false)) {
3194 Find_Actual_Block_Device();
Ethan Yonker54970bb2019-01-17 16:19:24 -06003195 string temp = Actual_Block_Device;
3196 Find_Real_Block_Device(temp, false);
3197 string block = basename(temp.c_str());
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05003198 string file = "/sys/fs/" + Current_File_System + "/" + block + "/lifetime_write_kbytes";
3199 string result;
3200 if (TWFunc::Path_Exists(file)) {
3201 if (TWFunc::read_file(file, result) != 0) {
3202 LOGINFO("Check_Lifetime_Writes of '%s' failed to read_file\n", file.c_str());
3203 } else {
3204 LOGINFO("Check_Lifetime_Writes result: '%s'\n", result.c_str());
3205 if (result == "0") {
3206 ret = 0;
3207 }
3208 }
3209 } else {
3210 LOGINFO("Check_Lifetime_Writes file does not exist '%s'\n", file.c_str());
3211 }
3212 UnMount(true);
3213 } else {
3214 LOGINFO("Check_Lifetime_Writes failed to mount '%s'\n", Mount_Point.c_str());
3215 }
3216 Mount_Read_Only = original_read_only;
3217 return ret;
3218}
Ethan Yonker66a19492015-12-10 10:19:45 -06003219
3220int TWPartition::Decrypt_Adopted() {
3221#ifdef TW_INCLUDE_CRYPTO
3222 int ret = 1;
3223 Is_Adopted_Storage = false;
3224 string Adopted_Key_File = "";
3225
3226 if (!Removable)
3227 return ret;
3228
3229 int fd = open(Alternate_Block_Device.c_str(), O_RDONLY);
3230 if (fd < 0) {
3231 LOGINFO("failed to open '%s'\n", Alternate_Block_Device.c_str());
3232 return ret;
3233 }
3234 char type_guid[80];
3235 char part_guid[80];
3236
3237 if (gpt_disk_get_partition_info(fd, 2, type_guid, part_guid) == 0) {
3238 LOGINFO("type: '%s'\n", type_guid);
3239 LOGINFO("part: '%s'\n", part_guid);
3240 Adopted_GUID = part_guid;
3241 LOGINFO("Adopted_GUID '%s'\n", Adopted_GUID.c_str());
3242 if (strcmp(type_guid, TWGptAndroidExpand) == 0) {
3243 LOGINFO("android_expand found\n");
3244 Adopted_Key_File = "/data/misc/vold/expand_";
3245 Adopted_Key_File += part_guid;
3246 Adopted_Key_File += ".key";
3247 if (TWFunc::Path_Exists(Adopted_Key_File)) {
3248 Is_Adopted_Storage = true;
3249 /* Until we find a use case for this, I think it is safe
3250 * to disable USB Mass Storage whenever adopted storage
3251 * is present.
3252 */
3253 LOGINFO("Detected adopted storage, disabling USB mass storage mode\n");
3254 DataManager::SetValue("tw_has_usb_storage", 0);
3255 }
3256 }
3257 }
3258
3259 if (Is_Adopted_Storage) {
3260 string Adopted_Block_Device = Alternate_Block_Device + "p2";
3261 if (!TWFunc::Path_Exists(Adopted_Block_Device)) {
3262 Adopted_Block_Device = Alternate_Block_Device + "2";
3263 if (!TWFunc::Path_Exists(Adopted_Block_Device)) {
3264 LOGINFO("Adopted block device does not exist\n");
3265 goto exit;
3266 }
3267 }
3268 LOGINFO("key file is '%s', block device '%s'\n", Adopted_Key_File.c_str(), Adopted_Block_Device.c_str());
3269 char crypto_blkdev[MAXPATHLEN];
3270 std::string thekey;
3271 int fdkey = open(Adopted_Key_File.c_str(), O_RDONLY);
3272 if (fdkey < 0) {
3273 LOGINFO("failed to open key file\n");
3274 goto exit;
3275 }
3276 char buf[512];
3277 ssize_t n;
3278 while ((n = read(fdkey, &buf[0], sizeof(buf))) > 0) {
3279 thekey.append(buf, n);
3280 }
3281 close(fdkey);
3282 unsigned char* key = (unsigned char*) thekey.data();
3283 cryptfs_revert_ext_volume(part_guid);
3284
3285 ret = cryptfs_setup_ext_volume(part_guid, Adopted_Block_Device.c_str(), key, thekey.size(), crypto_blkdev);
3286 if (ret == 0) {
3287 LOGINFO("adopted storage new block device: '%s'\n", crypto_blkdev);
3288 Decrypted_Block_Device = crypto_blkdev;
3289 Is_Decrypted = true;
3290 Is_Encrypted = true;
3291 Find_Actual_Block_Device();
Kjell Braden3126a112016-06-19 16:58:15 +00003292 if (!Mount_Storage_Retry(false)) {
Ethan Yonker66a19492015-12-10 10:19:45 -06003293 LOGERR("Failed to mount decrypted adopted storage device\n");
3294 Is_Decrypted = false;
3295 Is_Encrypted = false;
3296 cryptfs_revert_ext_volume(part_guid);
3297 ret = 1;
3298 } else {
Ethan Yonkerfcf3f242016-02-16 12:30:26 -06003299 UnMount(false);
3300 Has_Android_Secure = false;
3301 Symlink_Path = "";
3302 Symlink_Mount_Point = "";
3303 Backup_Name = Mount_Point.substr(1);
3304 Backup_Path = Mount_Point;
3305 TWPartition* sdext = PartitionManager.Find_Partition_By_Path("/sd-ext");
3306 if (sdext && sdext->Actual_Block_Device == Adopted_Block_Device) {
3307 LOGINFO("Removing /sd-ext from partition list due to adopted storage\n");
3308 PartitionManager.Remove_Partition_By_Path("/sd-ext");
3309 }
Ethan Yonker66a19492015-12-10 10:19:45 -06003310 Setup_Data_Media();
3311 Recreate_Media_Folder();
3312 Wipe_Available_in_GUI = true;
3313 Wipe_During_Factory_Reset = true;
3314 Can_Be_Backed_Up = true;
3315 Can_Encrypt_Backup = true;
3316 Use_Userdata_Encryption = true;
3317 Is_Storage = true;
3318 Storage_Name = "Adopted Storage";
3319 Is_SubPartition = true;
3320 SubPartition_Of = "/data";
3321 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
3322 DataManager::SetValue("tw_has_adopted_storage", 1);
3323 }
3324 } else {
3325 LOGERR("Failed to setup adopted storage decryption\n");
3326 }
3327 }
3328exit:
Matt Mower06543e32017-01-06 15:25:26 -06003329 close(fd);
Ethan Yonker66a19492015-12-10 10:19:45 -06003330 return ret;
3331#else
3332 LOGINFO("Decrypt_Adopted: no crypto support\n");
3333 return 1;
3334#endif
3335}
3336
3337void TWPartition::Revert_Adopted() {
3338#ifdef TW_INCLUDE_CRYPTO
3339 if (!Adopted_GUID.empty()) {
3340 PartitionManager.Remove_MTP_Storage(Mount_Point);
3341 UnMount(false);
3342 cryptfs_revert_ext_volume(Adopted_GUID.c_str());
3343 Is_Adopted_Storage = false;
3344 Is_Encrypted = false;
3345 Is_Decrypted = false;
3346 Decrypted_Block_Device = "";
3347 Find_Actual_Block_Device();
3348 Wipe_During_Factory_Reset = false;
3349 Can_Be_Backed_Up = false;
3350 Can_Encrypt_Backup = false;
3351 Use_Userdata_Encryption = false;
3352 Is_SubPartition = false;
3353 SubPartition_Of = "";
3354 Has_Data_Media = false;
3355 Storage_Path = Mount_Point;
3356 if (!Symlink_Mount_Point.empty()) {
3357 TWPartition* Dat = PartitionManager.Find_Partition_By_Path("/data");
3358 if (Dat) {
3359 Dat->UnMount(false);
3360 Dat->Symlink_Mount_Point = Symlink_Mount_Point;
3361 }
3362 Symlink_Mount_Point = "";
3363 }
3364 }
3365#else
3366 LOGINFO("Revert_Adopted: no crypto support\n");
3367#endif
3368}
bigbiff bigbiffb5ecaad2017-03-20 18:53:53 -04003369
3370void TWPartition::Set_Backup_FileName(string fname) {
3371 Backup_FileName = fname;
3372}
3373
3374string TWPartition::Get_Backup_Name() {
3375 return Backup_Name;
3376}