blob: aa82c09ec28a1c1b5f86260a1c9d6b65979c1efd [file] [log] [blame]
Dees Troy3be70a82013-10-22 14:25:12 +00001/*
bigbiff bigbiff56cf5642016-08-19 17:43:45 -04002 Copyright 2013 to 2017 TeamWin
Dees Troy3be70a82013-10-22 14:25:12 +00003 This file is part of TWRP/TeamWin Recovery Project.
4
5 TWRP is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 TWRP is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with TWRP. If not, see <http://www.gnu.org/licenses/>.
17*/
Dees_Troy51a0e822012-09-05 15:24:24 -040018
19#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
22#include <sys/stat.h>
23#include <sys/vfs.h>
Dees_Troy5bf43922012-09-07 16:07:55 -040024#include <sys/mount.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040025#include <unistd.h>
Dees_Troy51127312012-09-08 13:08:49 -040026#include <dirent.h>
thatf54e5392016-01-29 22:04:43 +010027#include <libgen.h>
bigbiffce8f83c2015-12-12 18:30:21 -050028#include <zlib.h>
bigbiff bigbiff9c754052013-01-09 09:09:08 -050029#include <iostream>
30#include <sstream>
Ethan Yonker66a19492015-12-10 10:19:45 -060031#include <sys/param.h>
Ethan Yonker472f5062016-02-25 13:47:30 -060032#include <fcntl.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040033
Ethan Yonker007de982018-08-31 14:23:16 -050034#include "cutils/properties.h"
bigbiff7b4c7a62015-01-01 19:44:14 -050035#include "libblkid/include/blkid.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040036#include "variables.h"
Dees_Troy2673cec2013-04-02 20:22:16 +000037#include "twcommon.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040038#include "partitions.hpp"
Dees_Troy5bf43922012-09-07 16:07:55 -040039#include "data.hpp"
Dees_Troy43d8b002012-09-17 16:00:01 -040040#include "twrp-functions.hpp"
bigbiff bigbiff9c754052013-01-09 09:09:08 -050041#include "twrpTar.hpp"
Ethan Yonker3fdcda42016-11-30 12:29:37 -060042#include "exclude.hpp"
Ethan Yonker1b7a31b2014-07-03 15:09:22 -050043#include "infomanager.hpp"
Ethan Yonker4b94cfd2014-12-11 10:00:45 -060044#include "set_metadata.h"
Ethan Yonker74db1572015-10-28 12:44:49 -050045#include "gui/gui.hpp"
bigbiffce8f83c2015-12-12 18:30:21 -050046#include "adbbu/libtwadbbu.hpp"
Ethan Yonker98661c12018-10-17 08:39:28 -050047#ifdef TW_INCLUDE_CRYPTO
48 #include "crypto/fde/cryptfs.h"
49 #ifdef TW_INCLUDE_FBE
50 #include "crypto/ext4crypt/Decrypt.h"
51 #endif
52#else
53 #define CRYPT_FOOTER_OFFSET 0x4000
54#endif
Dees_Troy5bf43922012-09-07 16:07:55 -040055extern "C" {
Dees_Troy38bd7602012-09-14 13:33:53 -040056 #include "mtdutils/mtdutils.h"
57 #include "mtdutils/mounts.h"
Dees_Troya95f55c2013-08-17 13:14:43 +000058#ifdef USE_EXT4
Ethan Yonker8373cfe2017-09-08 06:50:54 -050059 // #include "make_ext4fs.h" TODO need ifdef for android8
60 #include <ext4_utils/make_ext4fs.h>
Dees_Troya95f55c2013-08-17 13:14:43 +000061#endif
Ethan Yonker71413f42014-02-26 13:36:08 -060062#ifdef TW_INCLUDE_CRYPTO
Ethan Yonker66a19492015-12-10 10:19:45 -060063 #include "gpt/gpt.h"
Ethan Yonker71413f42014-02-26 13:36:08 -060064#endif
Dees_Troy5bf43922012-09-07 16:07:55 -040065}
Matt Mower87413642017-01-17 21:14:46 -060066#include <selinux/selinux.h>
Ethan Yonkerf27497f2014-02-09 11:48:33 -060067#include <selinux/label.h>
Dees Troy4159aed2014-02-28 17:24:43 +000068#ifdef HAVE_CAPABILITIES
69#include <sys/capability.h>
70#include <sys/xattr.h>
71#include <linux/xattr.h>
72#endif
HashBanged974bb2016-01-30 14:20:09 -050073#include <sparse_format.h>
Ethan Yonker472f5062016-02-25 13:47:30 -060074#include "progresstracking.hpp"
Dees_Troy51a0e822012-09-05 15:24:24 -040075
bigbiff bigbiff9c754052013-01-09 09:09:08 -050076using namespace std;
77
Ethan Yonker6e8c27a2016-12-22 17:55:57 -060078static int auto_index = 0; // v2 fstab allows you to specify a mount point of "auto" with no /. These items are given a mount point of /auto* where * == auto_index
79
Dees_Troya95f55c2013-08-17 13:14:43 +000080extern struct selabel_handle *selinux_handle;
Ethan Yonker6277c792014-09-15 14:54:30 -050081extern bool datamedia;
Dees_Troya95f55c2013-08-17 13:14:43 +000082
Hashcode62bd9e02013-11-19 21:59:42 -080083struct flag_list {
84 const char *name;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -060085 unsigned long flag;
Hashcode62bd9e02013-11-19 21:59:42 -080086};
87
Matt Mower4ab42b12016-04-21 13:52:18 -050088const struct flag_list mount_flags[] = {
Ethan Yonker6e8c27a2016-12-22 17:55:57 -060089 { "noatime", MS_NOATIME },
90 { "noexec", MS_NOEXEC },
91 { "nosuid", MS_NOSUID },
92 { "nodev", MS_NODEV },
93 { "nodiratime", MS_NODIRATIME },
94 { "ro", MS_RDONLY },
95 { "rw", 0 },
96 { "remount", MS_REMOUNT },
97 { "bind", MS_BIND },
98 { "rec", MS_REC },
Dees Troyc4bc30e2014-02-03 15:04:19 +000099#ifdef MS_UNBINDABLE
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600100 { "unbindable", MS_UNBINDABLE },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000101#endif
102#ifdef MS_PRIVATE
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600103 { "private", MS_PRIVATE },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000104#endif
105#ifdef MS_SLAVE
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600106 { "slave", MS_SLAVE },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000107#endif
108#ifdef MS_SHARED
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600109 { "shared", MS_SHARED },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000110#endif
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600111 { "sync", MS_SYNCHRONOUS },
112 { 0, 0 },
113};
114
115const char *ignored_mount_items[] = {
116 "defaults=",
117 "errors=",
Ethan Yonker93382822018-11-01 15:25:31 -0500118 "latemount",
119 "sysfs_path=",
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600120 NULL
Hashcode62bd9e02013-11-19 21:59:42 -0800121};
122
Matt Mower2416a502016-04-12 19:54:46 -0500123enum TW_FSTAB_FLAGS {
124 TWFLAG_DEFAULTS, // Retain position
125 TWFLAG_ANDSEC,
126 TWFLAG_BACKUP,
127 TWFLAG_BACKUPNAME,
128 TWFLAG_BLOCKSIZE,
129 TWFLAG_CANBEWIPED,
130 TWFLAG_CANENCRYPTBACKUP,
131 TWFLAG_DISPLAY,
132 TWFLAG_ENCRYPTABLE,
Ethan Yonkere9afc3d2018-08-30 15:16:27 -0500133 TWFLAG_FILEENCRYPTION,
Matt Mower2416a502016-04-12 19:54:46 -0500134 TWFLAG_FLASHIMG,
135 TWFLAG_FORCEENCRYPT,
136 TWFLAG_FSFLAGS,
137 TWFLAG_IGNOREBLKID,
138 TWFLAG_LENGTH,
139 TWFLAG_MOUNTTODECRYPT,
140 TWFLAG_REMOVABLE,
141 TWFLAG_RETAINLAYOUTVERSION,
142 TWFLAG_SETTINGSSTORAGE,
143 TWFLAG_STORAGE,
144 TWFLAG_STORAGENAME,
145 TWFLAG_SUBPARTITIONOF,
146 TWFLAG_SYMLINK,
147 TWFLAG_USERDATAENCRYPTBACKUP,
148 TWFLAG_USERMRF,
149 TWFLAG_WIPEDURINGFACTORYRESET,
150 TWFLAG_WIPEINGUI,
Ethan Yonker1b190162016-12-05 15:25:19 -0600151 TWFLAG_SLOTSELECT,
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600152 TWFLAG_WAIT,
153 TWFLAG_VERIFY,
154 TWFLAG_CHECK,
155 TWFLAG_ALTDEVICE,
156 TWFLAG_NOTRIM,
157 TWFLAG_VOLDMANAGED,
158 TWFLAG_FORMATTABLE,
159 TWFLAG_RESIZE,
Ethan Yonker93382822018-11-01 15:25:31 -0500160 TWFLAG_KEYDIRECTORY,
Matt Mower2416a502016-04-12 19:54:46 -0500161};
162
163/* Flags without a trailing '=' are considered dual format flags and can be
164 * written as either 'flagname' or 'flagname=', where the character following
165 * the '=' is Y,y,1 for true and false otherwise.
166 */
167const struct flag_list tw_flags[] = {
168 { "andsec", TWFLAG_ANDSEC },
169 { "backup", TWFLAG_BACKUP },
170 { "backupname=", TWFLAG_BACKUPNAME },
171 { "blocksize=", TWFLAG_BLOCKSIZE },
172 { "canbewiped", TWFLAG_CANBEWIPED },
173 { "canencryptbackup", TWFLAG_CANENCRYPTBACKUP },
174 { "defaults", TWFLAG_DEFAULTS },
175 { "display=", TWFLAG_DISPLAY },
176 { "encryptable=", TWFLAG_ENCRYPTABLE },
Ethan Yonkere9afc3d2018-08-30 15:16:27 -0500177 { "fileencryption=", TWFLAG_FILEENCRYPTION },
Matt Mower2416a502016-04-12 19:54:46 -0500178 { "flashimg", TWFLAG_FLASHIMG },
179 { "forceencrypt=", TWFLAG_FORCEENCRYPT },
180 { "fsflags=", TWFLAG_FSFLAGS },
181 { "ignoreblkid", TWFLAG_IGNOREBLKID },
182 { "length=", TWFLAG_LENGTH },
183 { "mounttodecrypt", TWFLAG_MOUNTTODECRYPT },
184 { "removable", TWFLAG_REMOVABLE },
185 { "retainlayoutversion", TWFLAG_RETAINLAYOUTVERSION },
186 { "settingsstorage", TWFLAG_SETTINGSSTORAGE },
187 { "storage", TWFLAG_STORAGE },
188 { "storagename=", TWFLAG_STORAGENAME },
189 { "subpartitionof=", TWFLAG_SUBPARTITIONOF },
190 { "symlink=", TWFLAG_SYMLINK },
191 { "userdataencryptbackup", TWFLAG_USERDATAENCRYPTBACKUP },
192 { "usermrf", TWFLAG_USERMRF },
193 { "wipeduringfactoryreset", TWFLAG_WIPEDURINGFACTORYRESET },
194 { "wipeingui", TWFLAG_WIPEINGUI },
Ethan Yonker1b190162016-12-05 15:25:19 -0600195 { "slotselect", TWFLAG_SLOTSELECT },
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600196 { "wait", TWFLAG_WAIT },
197 { "verify", TWFLAG_VERIFY },
198 { "check", TWFLAG_CHECK },
199 { "altdevice", TWFLAG_ALTDEVICE },
200 { "notrim", TWFLAG_NOTRIM },
201 { "voldmanaged=", TWFLAG_VOLDMANAGED },
202 { "formattable", TWFLAG_FORMATTABLE },
203 { "resize", TWFLAG_RESIZE },
Ethan Yonker93382822018-11-01 15:25:31 -0500204 { "keydirectory=", TWFLAG_KEYDIRECTORY },
Matt Mower2416a502016-04-12 19:54:46 -0500205 { 0, 0 },
206};
207
that9e0593e2014-10-08 00:01:24 +0200208TWPartition::TWPartition() {
Dees_Troy51a0e822012-09-05 15:24:24 -0400209 Can_Be_Mounted = false;
210 Can_Be_Wiped = false;
Dees_Troya13d74f2013-03-24 08:54:55 -0500211 Can_Be_Backed_Up = false;
Vojtech Bocek1dc30982013-08-30 21:49:30 +0200212 Use_Rm_Rf = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400213 Wipe_During_Factory_Reset = false;
214 Wipe_Available_in_GUI = false;
215 Is_SubPartition = false;
Dees_Troy2691f9d2012-09-24 11:15:49 -0400216 Has_SubPartition = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400217 SubPartition_Of = "";
218 Symlink_Path = "";
219 Symlink_Mount_Point = "";
220 Mount_Point = "";
Dees_Troye58d5262012-09-21 12:27:57 -0400221 Backup_Path = "";
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600222 Wildcard_Block_Device = false;
223 Sysfs_Entry = "";
Dees_Troy38bd7602012-09-14 13:33:53 -0400224 Actual_Block_Device = "";
225 Primary_Block_Device = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400226 Alternate_Block_Device = "";
227 Removable = false;
228 Is_Present = false;
229 Length = 0;
230 Size = 0;
231 Used = 0;
232 Free = 0;
233 Backup_Size = 0;
234 Can_Be_Encrypted = false;
235 Is_Encrypted = false;
236 Is_Decrypted = false;
Ethan Yonkerbd7492d2016-12-07 13:55:01 -0600237 Is_FBE = false;
Ethan Yonker253368a2014-11-25 15:00:52 -0600238 Mount_To_Decrypt = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400239 Decrypted_Block_Device = "";
240 Display_Name = "";
Dees_Troya13d74f2013-03-24 08:54:55 -0500241 Backup_Display_Name = "";
242 Storage_Name = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400243 Backup_Name = "";
Dees_Troy63c8df72012-09-10 14:02:05 -0400244 Backup_FileName = "";
Dees_Troy38bd7602012-09-14 13:33:53 -0400245 MTD_Name = "";
bigbiffce8f83c2015-12-12 18:30:21 -0500246 Backup_Method = BM_NONE;
Dees_Troy83bd4832013-05-04 12:39:56 +0000247 Can_Encrypt_Backup = false;
248 Use_Userdata_Encryption = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400249 Has_Data_Media = false;
Dees_Troye58d5262012-09-21 12:27:57 -0400250 Has_Android_Secure = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400251 Is_Storage = false;
Dees_Troya13d74f2013-03-24 08:54:55 -0500252 Is_Settings_Storage = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400253 Storage_Path = "";
254 Current_File_System = "";
255 Fstab_File_System = "";
Hashcode62bd9e02013-11-19 21:59:42 -0800256 Mount_Flags = 0;
257 Mount_Options = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400258 Format_Block_Size = 0;
Dees_Troy68cab492012-12-12 19:29:35 +0000259 Ignore_Blkid = false;
Dees_Troy16c2b312013-01-15 16:51:18 +0000260 Retain_Layout_Version = false;
dianlujitao4879b372018-12-03 18:45:47 +0800261 Crypto_Key_Location = "";
Ethan Yonker726a0202014-12-16 20:01:38 -0600262 MTP_Storage_ID = 0;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600263 Can_Flash_Img = false;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500264 Mount_Read_Only = false;
Ethan Yonker66a19492015-12-10 10:19:45 -0600265 Is_Adopted_Storage = false;
266 Adopted_GUID = "";
Ethan Yonker1b190162016-12-05 15:25:19 -0600267 SlotSelect = false;
Ethan Yonker93382822018-11-01 15:25:31 -0500268 Key_Directory = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400269}
270
271TWPartition::~TWPartition(void) {
272 // Do nothing
273}
274
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600275bool TWPartition::Process_Fstab_Line(const char *fstab_line, bool Display_Error, std::map<string, Flags_Map> *twrp_flags) {
Matt Mower2b2dd152016-04-26 11:24:08 -0500276 char full_line[MAX_FSTAB_LINE_LENGTH];
Matt Mower2416a502016-04-12 19:54:46 -0500277 char twflags[MAX_FSTAB_LINE_LENGTH] = "";
Dees_Troy5bf43922012-09-07 16:07:55 -0400278 char* ptr;
Matt Mower2b2dd152016-04-26 11:24:08 -0500279 int line_len = strlen(fstab_line), index = 0, item_index = 0;
Dees_Troya13d74f2013-03-24 08:54:55 -0500280 bool skip = false;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600281 int fstab_version = 1, mount_point_index = 0, fs_index = 1, block_device_index = 2;
282 TWPartition *additional_entry = NULL;
283 std::map<string, Flags_Map>::iterator it;
Dees_Troy5bf43922012-09-07 16:07:55 -0400284
Matt Mower2b2dd152016-04-26 11:24:08 -0500285 strlcpy(full_line, fstab_line, sizeof(full_line));
Dees_Troy51127312012-09-08 13:08:49 -0400286 for (index = 0; index < line_len; index++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500287 if (full_line[index] == 34)
288 skip = !skip;
289 if (!skip && full_line[index] <= 32)
Dees_Troy5bf43922012-09-07 16:07:55 -0400290 full_line[index] = '\0';
Dees_Troy5bf43922012-09-07 16:07:55 -0400291 }
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600292 if (line_len < 10)
293 return false; // There can't possibly be a valid fstab line that is less than 10 chars
294 if (strncmp(fstab_line, "/dev/", strlen("/dev/")) == 0 || strncmp(fstab_line, "/devices/", strlen("/devices/")) == 0) {
295 fstab_version = 2;
296 block_device_index = 0;
297 mount_point_index = 1;
298 fs_index = 2;
299 }
300
301 index = 0;
Dees_Troy5bf43922012-09-07 16:07:55 -0400302 while (index < line_len) {
303 while (index < line_len && full_line[index] == '\0')
304 index++;
305 if (index >= line_len)
306 continue;
307 ptr = full_line + index;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600308 if (item_index == mount_point_index) {
309 Mount_Point = ptr;
310 if (fstab_version == 2) {
311 additional_entry = PartitionManager.Find_Partition_By_Path(Mount_Point);
312 if (additional_entry) {
313 LOGINFO("Found an additional entry for '%s'\n", Mount_Point.c_str());
314 }
315 }
316 LOGINFO("Processing '%s'\n", Mount_Point.c_str());
317 Backup_Path = Mount_Point;
318 Storage_Path = Mount_Point;
319 Display_Name = ptr + 1;
320 Backup_Display_Name = Display_Name;
321 Storage_Name = Display_Name;
322 item_index++;
323 } else if (item_index == fs_index) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400324 // File System
325 Fstab_File_System = ptr;
326 Current_File_System = ptr;
327 item_index++;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600328 } else if (item_index == block_device_index) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400329 // Primary Block Device
Dees_Troy38bd7602012-09-14 13:33:53 -0400330 if (Fstab_File_System == "mtd" || Fstab_File_System == "yaffs2") {
Dees_Troy094207a2012-09-26 12:00:39 -0400331 MTD_Name = ptr;
332 Find_MTD_Block_Device(MTD_Name);
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400333 } else if (Fstab_File_System == "bml") {
334 if (Mount_Point == "/boot")
335 MTD_Name = "boot";
336 else if (Mount_Point == "/recovery")
337 MTD_Name = "recovery";
338 Primary_Block_Device = ptr;
339 if (*ptr != '/')
Dees_Troy2673cec2013-04-02 20:22:16 +0000340 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 -0400341 } else if (*ptr != '/') {
Dees_Troy5bf43922012-09-07 16:07:55 -0400342 if (Display_Error)
Matt Mower2b2dd152016-04-26 11:24:08 -0500343 LOGERR("Invalid block device '%s' in fstab line '%s'", ptr, fstab_line);
Dees_Troy5bf43922012-09-07 16:07:55 -0400344 else
Matt Mower2b2dd152016-04-26 11:24:08 -0500345 LOGINFO("Invalid block device '%s' in fstab line '%s'", ptr, fstab_line);
Matt Mower72c87ce2016-04-26 14:34:56 -0500346 return false;
Dees_Troy38bd7602012-09-14 13:33:53 -0400347 } else {
348 Primary_Block_Device = ptr;
349 Find_Real_Block_Device(Primary_Block_Device, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400350 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400351 item_index++;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600352 } else if (item_index > 2) {
353 if (fstab_version == 2) {
354 if (item_index == 3) {
355 Process_FS_Flags(ptr);
356 if (additional_entry) {
357 additional_entry->Save_FS_Flags(Fstab_File_System, Mount_Flags, Mount_Options);
358 return false; // We save the extra fs flags in the other partition entry and by returning false, this entry will be deleted
359 }
360 } else {
361 strlcpy(twflags, ptr, sizeof(twflags));
362 }
363 item_index++;
364 } else if (*ptr == '/') { // v2 fstab does not allow alternate block devices
Dees_Troy5bf43922012-09-07 16:07:55 -0400365 // Alternate Block Device
366 Alternate_Block_Device = ptr;
367 Find_Real_Block_Device(Alternate_Block_Device, Display_Error);
368 } else if (strlen(ptr) > 7 && strncmp(ptr, "length=", 7) == 0) {
369 // Partition length
370 ptr += 7;
371 Length = atoi(ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400372 } else if (strlen(ptr) > 6 && strncmp(ptr, "flags=", 6) == 0) {
373 // Custom flags, save for later so that new values aren't overwritten by defaults
374 ptr += 6;
Matt Mower2416a502016-04-12 19:54:46 -0500375 strlcpy(twflags, ptr, sizeof(twflags));
Dees_Troy38bd7602012-09-14 13:33:53 -0400376 } else if (strlen(ptr) == 4 && (strncmp(ptr, "NULL", 4) == 0 || strncmp(ptr, "null", 4) == 0 || strncmp(ptr, "null", 4) == 0)) {
377 // Do nothing
Dees_Troy5bf43922012-09-07 16:07:55 -0400378 } else {
379 // Unhandled data
Matt Mower2b2dd152016-04-26 11:24:08 -0500380 LOGINFO("Unhandled fstab information '%s' in fstab line '%s'\n", ptr, fstab_line);
Dees_Troy5bf43922012-09-07 16:07:55 -0400381 }
382 }
383 while (index < line_len && full_line[index] != '\0')
384 index++;
385 }
386
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600387 // override block devices from the v2 fstab with the ones we read from the twrp.flags file in case they are different
388 if (fstab_version == 2 && twrp_flags && twrp_flags->size() > 0) {
389 it = twrp_flags->find(Mount_Point);
390 if (it != twrp_flags->end()) {
391 if (!it->second.Primary_Block_Device.empty()) {
392 Primary_Block_Device = it->second.Primary_Block_Device;
393 Find_Real_Block_Device(Primary_Block_Device, Display_Error);
394 }
395 if (!it->second.Alternate_Block_Device.empty()) {
396 Alternate_Block_Device = it->second.Alternate_Block_Device;
397 Find_Real_Block_Device(Alternate_Block_Device, Display_Error);
398 }
399 }
400 }
401
402 if (strncmp(fstab_line, "/devices/", strlen("/devices/")) == 0) {
403 Sysfs_Entry = Primary_Block_Device;
404 Primary_Block_Device = "";
405 Is_Storage = true;
406 Removable = true;
407 Wipe_Available_in_GUI = true;
408 Wildcard_Block_Device = true;
409 }
410 if (Primary_Block_Device.find("*") != string::npos)
411 Wildcard_Block_Device = true;
412
413 if (Mount_Point == "auto") {
414 Mount_Point = "/auto";
415 char autoi[5];
416 sprintf(autoi, "%i", auto_index);
417 Mount_Point += autoi;
418 Backup_Path = Mount_Point;
419 Storage_Path = Mount_Point;
420 auto_index++;
421 Setup_File_System(Display_Error);
422 Display_Name = "Storage";
423 Backup_Display_Name = Display_Name;
424 Storage_Name = Display_Name;
425 Can_Be_Backed_Up = false;
426 Wipe_Available_in_GUI = true;
427 Is_Storage = true;
428 Removable = true;
429 Wipe_Available_in_GUI = true;
430 } else if (!Is_File_System(Fstab_File_System) && !Is_Image(Fstab_File_System)) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400431 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000432 LOGERR("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400433 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000434 LOGINFO("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Matt Mower72c87ce2016-04-26 14:34:56 -0500435 return false;
Dees_Troy5bf43922012-09-07 16:07:55 -0400436 } else if (Is_File_System(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400437 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400438 Setup_File_System(Display_Error);
Captain Throwback9d6feb52018-07-27 10:05:24 -0400439 if (Mount_Point == PartitionManager.Get_Android_Root_Path()) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400440 Display_Name = "System";
Dees_Troya13d74f2013-03-24 08:54:55 -0500441 Backup_Display_Name = Display_Name;
442 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400443 Wipe_Available_in_GUI = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500444 Can_Be_Backed_Up = true;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500445 Mount_Read_Only = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400446 } else if (Mount_Point == "/data") {
447 Display_Name = "Data";
Dees_Troya13d74f2013-03-24 08:54:55 -0500448 Backup_Display_Name = Display_Name;
449 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400450 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400451 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500452 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000453 Can_Encrypt_Backup = true;
454 Use_Userdata_Encryption = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400455 } else if (Mount_Point == "/cache") {
456 Display_Name = "Cache";
Dees_Troya13d74f2013-03-24 08:54:55 -0500457 Backup_Display_Name = Display_Name;
458 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400459 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400460 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500461 Can_Be_Backed_Up = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400462 } else if (Mount_Point == "/datadata") {
Dees_Troy51127312012-09-08 13:08:49 -0400463 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400464 Display_Name = "DataData";
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 Is_SubPartition = true;
468 SubPartition_Of = "/data";
Dees_Troy5bf43922012-09-07 16:07:55 -0400469 DataManager::SetValue(TW_HAS_DATADATA, 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500470 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000471 Can_Encrypt_Backup = true;
472 Use_Userdata_Encryption = false; // This whole partition should be encrypted
Dees_Troy5bf43922012-09-07 16:07:55 -0400473 } else if (Mount_Point == "/sd-ext") {
Dees_Troy51127312012-09-08 13:08:49 -0400474 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400475 Display_Name = "SD-Ext";
Dees_Troya13d74f2013-03-24 08:54:55 -0500476 Backup_Display_Name = Display_Name;
477 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400478 Wipe_Available_in_GUI = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400479 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500480 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000481 Can_Encrypt_Backup = true;
482 Use_Userdata_Encryption = true;
Dees_Troy2c50e182012-09-26 20:05:28 -0400483 } else if (Mount_Point == "/boot") {
484 Display_Name = "Boot";
Dees_Troya13d74f2013-03-24 08:54:55 -0500485 Backup_Display_Name = Display_Name;
Dees_Troy2c50e182012-09-26 20:05:28 -0400486 DataManager::SetValue("tw_boot_is_mountable", 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500487 Can_Be_Backed_Up = true;
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500488 } else if (Mount_Point == "/vendor") {
489 Display_Name = "Vendor";
490 Backup_Display_Name = Display_Name;
491 Storage_Name = Display_Name;
492 Mount_Read_Only = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400493 }
494#ifdef TW_EXTERNAL_STORAGE_PATH
495 if (Mount_Point == EXPAND(TW_EXTERNAL_STORAGE_PATH)) {
496 Is_Storage = true;
497 Storage_Path = EXPAND(TW_EXTERNAL_STORAGE_PATH);
Dees_Troyc51f1f92012-09-20 15:32:13 -0400498 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500499 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400500#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000501 if (Mount_Point == "/sdcard" || Mount_Point == "/external_sd" || Mount_Point == "/external_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400502 Is_Storage = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400503 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500504 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400505#endif
Dees_Troyb05ddee2013-01-28 20:24:50 +0000506 }
Dees_Troy8170a922012-09-18 15:40:25 -0400507#ifdef TW_INTERNAL_STORAGE_PATH
508 if (Mount_Point == EXPAND(TW_INTERNAL_STORAGE_PATH)) {
509 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500510 Is_Settings_Storage = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400511 Storage_Path = EXPAND(TW_INTERNAL_STORAGE_PATH);
Dees_Troya13d74f2013-03-24 08:54:55 -0500512 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400513 }
514#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000515 if (Mount_Point == "/emmc" || Mount_Point == "/internal_sd" || Mount_Point == "/internal_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400516 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500517 Is_Settings_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500518 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400519 }
520#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400521 } else if (Is_Image(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400522 Find_Actual_Block_Device();
Ethan Yonker1b190162016-12-05 15:25:19 -0600523 Setup_Image();
Dees_Troya13d74f2013-03-24 08:54:55 -0500524 if (Mount_Point == "/boot") {
525 Display_Name = "Boot";
526 Backup_Display_Name = Display_Name;
527 Can_Be_Backed_Up = true;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600528 Can_Flash_Img = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500529 } else if (Mount_Point == "/recovery") {
530 Display_Name = "Recovery";
531 Backup_Display_Name = Display_Name;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600532 Can_Flash_Img = true;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500533 } else if (Mount_Point == "/system_image") {
534 Display_Name = "System Image";
535 Backup_Display_Name = Display_Name;
HashBanged974bb2016-01-30 14:20:09 -0500536 Can_Flash_Img = true;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500537 Can_Be_Backed_Up = true;
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500538 } else if (Mount_Point == "/vendor_image") {
539 Display_Name = "Vendor Image";
540 Backup_Display_Name = Display_Name;
HashBanged974bb2016-01-30 14:20:09 -0500541 Can_Flash_Img = true;
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500542 Can_Be_Backed_Up = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500543 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400544 }
545
Matt Mower2416a502016-04-12 19:54:46 -0500546 // Process TWRP fstab flags
547 if (strlen(twflags) > 0) {
548 string Prev_Display_Name = Display_Name;
549 string Prev_Storage_Name = Storage_Name;
550 string Prev_Backup_Display_Name = Backup_Display_Name;
551 Display_Name = "";
552 Storage_Name = "";
553 Backup_Display_Name = "";
554
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600555 Process_TW_Flags(twflags, (fstab_version == 1), fstab_version);
556 Save_FS_Flags(Fstab_File_System, Mount_Flags, Mount_Options);
Matt Mower2416a502016-04-12 19:54:46 -0500557
558 bool has_display_name = !Display_Name.empty();
559 bool has_storage_name = !Storage_Name.empty();
560 bool has_backup_name = !Backup_Display_Name.empty();
561 if (!has_display_name) Display_Name = Prev_Display_Name;
562 if (!has_storage_name) Storage_Name = Prev_Storage_Name;
563 if (!has_backup_name) Backup_Display_Name = Prev_Backup_Display_Name;
564
565 if (has_display_name && !has_storage_name)
566 Storage_Name = Display_Name;
567 if (!has_display_name && has_storage_name)
568 Display_Name = Storage_Name;
569 if (has_display_name && !has_backup_name && Backup_Display_Name != "Android Secure")
570 Backup_Display_Name = Display_Name;
571 if (!has_display_name && has_backup_name)
572 Display_Name = Backup_Display_Name;
573 }
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600574
575 if (fstab_version == 2 && twrp_flags && twrp_flags->size() > 0) {
576 it = twrp_flags->find(Mount_Point);
577 if (it != twrp_flags->end()) {
578 char twrpflags[MAX_FSTAB_LINE_LENGTH] = "";
579 int skip = 0;
580 string Flags = it->second.Flags;
581 strcpy(twrpflags, Flags.c_str());
582 if (strlen(twrpflags) > strlen("flags=") && strncmp(twrpflags, "flags=", strlen("flags=")) == 0)
583 skip += strlen("flags=");
584 char* flagptr = twrpflags;
585 flagptr += skip;
586 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
587 }
588 }
nkk7198fc3992017-12-16 16:26:42 +0200589
590 if (Mount_Point == "/persist" && Can_Be_Mounted) {
591 bool mounted = Is_Mounted();
592 if (mounted || Mount(false)) {
593 // Read the backup settings file
594 DataManager::LoadPersistValues();
595 TWFunc::Fixup_Time_On_Boot("/persist/time/");
596 if (!mounted)
597 UnMount(false);
598 }
599 }
600
Dees_Troy51127312012-09-08 13:08:49 -0400601 return true;
602}
603
Matt Mower72c87ce2016-04-26 14:34:56 -0500604void TWPartition::Partition_Post_Processing(bool Display_Error) {
605 if (Mount_Point == "/data")
606 Setup_Data_Partition(Display_Error);
607 else if (Mount_Point == "/cache")
608 Setup_Cache_Partition(Display_Error);
609}
610
Ethan Yonkerbd7492d2016-12-07 13:55:01 -0600611void TWPartition::ExcludeAll(const string& path) {
612 backup_exclusions.add_absolute_dir(path);
613 wipe_exclusions.add_absolute_dir(path);
614}
615
Matt Mower72c87ce2016-04-26 14:34:56 -0500616void TWPartition::Setup_Data_Partition(bool Display_Error) {
617 if (Mount_Point != "/data")
618 return;
619
620 // Ensure /data is not mounted as tmpfs for qcom hardware decrypt
621 UnMount(false);
622
623#ifdef TW_INCLUDE_CRYPTO
624 if (datamedia)
625 Setup_Data_Media();
626 Can_Be_Encrypted = true;
627 char crypto_blkdev[255];
628 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
629 if (strcmp(crypto_blkdev, "error") != 0) {
630 DataManager::SetValue(TW_IS_DECRYPTED, 1);
631 Is_Encrypted = true;
632 Is_Decrypted = true;
Ethan Yonker93382822018-11-01 15:25:31 -0500633 if (Key_Directory.empty())
634 Is_FBE = false;
635 else
636 Is_FBE = true;
Ethan Yonkerbd7492d2016-12-07 13:55:01 -0600637 DataManager::SetValue(TW_IS_FBE, 0);
Matt Mower72c87ce2016-04-26 14:34:56 -0500638 Decrypted_Block_Device = crypto_blkdev;
639 LOGINFO("Data already decrypted, new block device: '%s'\n", crypto_blkdev);
640 } else if (!Mount(false)) {
641 if (Is_Present) {
Ethan Yonker93382822018-11-01 15:25:31 -0500642 if (Key_Directory.empty()) {
643 set_partition_data(Actual_Block_Device.c_str(), Crypto_Key_Location.c_str(), Fstab_File_System.c_str());
644 if (cryptfs_check_footer() == 0) {
645 Is_Encrypted = true;
646 Is_Decrypted = false;
647 Can_Be_Mounted = false;
648 Current_File_System = "emmc";
649 Setup_Image();
650 DataManager::SetValue(TW_IS_ENCRYPTED, 1);
651 DataManager::SetValue(TW_CRYPTO_PWTYPE, cryptfs_get_password_type());
652 DataManager::SetValue(TW_CRYPTO_PASSWORD, "");
653 DataManager::SetValue("tw_crypto_display", "");
654 } else {
655 gui_err("mount_data_footer=Could not mount /data and unable to find crypto footer.");
656 }
657 } else {
Matt Mower72c87ce2016-04-26 14:34:56 -0500658 Is_Encrypted = true;
659 Is_Decrypted = false;
Matt Mower72c87ce2016-04-26 14:34:56 -0500660 }
Ethan Yonker93382822018-11-01 15:25:31 -0500661 } else if (Key_Directory.empty()) {
Matt Mower72c87ce2016-04-26 14:34:56 -0500662 LOGERR("Primary block device '%s' for mount point '%s' is not present!\n", Primary_Block_Device.c_str(), Mount_Point.c_str());
663 }
664 } else {
mauronofrio9bf73352019-11-23 22:27:34 +0100665
666 if (!Decrypt_FBE_DE()) {
667 LOGINFO("Trying wrapped key.\n");
668 property_set("fbe.data.wrappedkey", "true");
669 if (!Decrypt_FBE_DE()) {
670 LOGERR("Unable to decrypt FBE device\n");
671 }
672 }
Matt Mower72c87ce2016-04-26 14:34:56 -0500673 }
674 if (datamedia && (!Is_Encrypted || (Is_Encrypted && Is_Decrypted))) {
675 Setup_Data_Media();
676 Recreate_Media_Folder();
677 }
678#else
679 if (datamedia) {
680 Setup_Data_Media();
681 Recreate_Media_Folder();
682 }
683#endif
684}
685
Ethan Yonker93382822018-11-01 15:25:31 -0500686bool TWPartition::Decrypt_FBE_DE() {
687if (TWFunc::Path_Exists("/data/unencrypted/key/version")) {
688 LOGINFO("File Based Encryption is present\n");
689#ifdef TW_INCLUDE_FBE
690 ExcludeAll(Mount_Point + "/convert_fbe");
691 ExcludeAll(Mount_Point + "/unencrypted");
692 //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
693 ExcludeAll(Mount_Point + "/misc/vold/user_keys");
694 //ExcludeAll(Mount_Point + "/system_ce");
695 //ExcludeAll(Mount_Point + "/system_de");
696 //ExcludeAll(Mount_Point + "/misc_ce");
697 //ExcludeAll(Mount_Point + "/misc_de");
698 ExcludeAll(Mount_Point + "/system/gatekeeper.password.key");
699 ExcludeAll(Mount_Point + "/system/gatekeeper.pattern.key");
700 ExcludeAll(Mount_Point + "/system/locksettings.db");
701 //ExcludeAll(Mount_Point + "/system/locksettings.db-shm"); // don't seem to need this one, but the other 2 are needed
702 ExcludeAll(Mount_Point + "/system/locksettings.db-wal");
703 //ExcludeAll(Mount_Point + "/user_de");
704 //ExcludeAll(Mount_Point + "/misc/profiles/cur/0"); // might be important later
705 ExcludeAll(Mount_Point + "/misc/gatekeeper");
706 ExcludeAll(Mount_Point + "/misc/keystore");
707 ExcludeAll(Mount_Point + "/drm/kek.dat");
708 ExcludeAll(Mount_Point + "/system_de/0/spblob"); // contains data needed to decrypt pixel 2
709 int retry_count = 3;
710 while (!Decrypt_DE() && --retry_count)
711 usleep(2000);
712 if (retry_count > 0) {
713 property_set("ro.crypto.state", "encrypted");
714 Is_Encrypted = true;
715 Is_Decrypted = false;
716 Is_FBE = true;
717 DataManager::SetValue(TW_IS_FBE, 1);
718 DataManager::SetValue(TW_IS_ENCRYPTED, 1);
719 string filename;
720 int pwd_type = Get_Password_Type(0, filename);
721 if (pwd_type < 0) {
722 LOGERR("This TWRP does not have synthetic password decrypt support\n");
723 pwd_type = 0; // default password
724 }
725 DataManager::SetValue(TW_CRYPTO_PWTYPE, pwd_type);
726 DataManager::SetValue(TW_CRYPTO_PASSWORD, "");
727 DataManager::SetValue("tw_crypto_display", "");
728 return true;
729 }
730#else
731 LOGERR("FBE found but FBE support not present in TWRP\n");
732#endif
733 }
734 return false;
735}
736
Matt Mower72c87ce2016-04-26 14:34:56 -0500737void TWPartition::Setup_Cache_Partition(bool Display_Error __unused) {
738 if (Mount_Point != "/cache")
739 return;
740
741 if (!Mount(true))
742 return;
743
744 if (!TWFunc::Path_Exists("/cache/recovery/.")) {
745 LOGINFO("Recreating /cache/recovery folder\n");
746 if (mkdir("/cache/recovery", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0)
747 LOGERR("Could not create /cache/recovery\n");
748 }
749}
750
Matt Mower4ab42b12016-04-21 13:52:18 -0500751void TWPartition::Process_FS_Flags(const char *str) {
752 char *options = strdup(str);
753 char *ptr, *savep;
Hashcode62bd9e02013-11-19 21:59:42 -0800754
Matt Mower4ab42b12016-04-21 13:52:18 -0500755 Mount_Options = "";
Hashcode62bd9e02013-11-19 21:59:42 -0800756
Matt Mower4ab42b12016-04-21 13:52:18 -0500757 // Avoid issues with potentially nested strtok by using strtok_r
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600758 for (ptr = strtok_r(options, ",", &savep); ptr; ptr = strtok_r(NULL, ",", &savep)) {
759 char *equals = strstr(ptr, "=");
760 size_t name_len;
Matt Mower4ab42b12016-04-21 13:52:18 -0500761
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600762 if (!equals)
763 name_len = strlen(ptr);
764 else
765 name_len = equals - ptr;
766
767 // There are some flags that we want to ignore in TWRP
768 bool found_match = false;
769 for (const char** ignored_mount_item = ignored_mount_items; *ignored_mount_item; ignored_mount_item++) {
770 if (strncmp(ptr, *ignored_mount_item, name_len) == 0) {
771 found_match = true;
Hashcode62bd9e02013-11-19 21:59:42 -0800772 break;
773 }
774 }
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600775 if (found_match)
776 continue;
Hashcode62bd9e02013-11-19 21:59:42 -0800777
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600778 // mount_flags are never postfixed by '='
779 if (!equals) {
780 const struct flag_list* mount_flag = mount_flags;
781 for (; mount_flag->name; mount_flag++) {
782 if (strcmp(ptr, mount_flag->name) == 0) {
783 if (mount_flag->flag == MS_RDONLY)
784 Mount_Read_Only = true;
785 else
786 Mount_Flags |= (unsigned)mount_flag->flag;
787 found_match = true;
788 break;
789 }
790 }
791 if (found_match)
792 continue;
Matt Mower4ab42b12016-04-21 13:52:18 -0500793 }
794
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600795 // If we aren't ignoring this flag and it's not a mount flag, then it must be a mount option
796 if (!Mount_Options.empty())
797 Mount_Options += ",";
798 Mount_Options += ptr;
Matt Mower4ab42b12016-04-21 13:52:18 -0500799 }
800 free(options);
Hashcode62bd9e02013-11-19 21:59:42 -0800801}
802
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600803void TWPartition::Save_FS_Flags(const string& local_File_System, int local_Mount_Flags, const string& local_Mount_Options) {
804 partition_fs_flags_struct flags;
805 flags.File_System = local_File_System;
806 flags.Mount_Flags = local_Mount_Flags;
807 flags.Mount_Options = local_Mount_Options;
808 fs_flags.push_back(flags);
809}
810
Matt Mower2416a502016-04-12 19:54:46 -0500811void TWPartition::Apply_TW_Flag(const unsigned flag, const char* str, const bool val) {
812 switch (flag) {
813 case TWFLAG_ANDSEC:
814 Has_Android_Secure = val;
815 break;
816 case TWFLAG_BACKUP:
817 Can_Be_Backed_Up = val;
818 break;
819 case TWFLAG_BACKUPNAME:
820 Backup_Display_Name = str;
821 break;
822 case TWFLAG_BLOCKSIZE:
823 Format_Block_Size = (unsigned long)(atol(str));
824 break;
825 case TWFLAG_CANBEWIPED:
826 Can_Be_Wiped = val;
827 break;
828 case TWFLAG_CANENCRYPTBACKUP:
829 Can_Encrypt_Backup = val;
830 break;
831 case TWFLAG_DEFAULTS:
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600832 case TWFLAG_WAIT:
833 case TWFLAG_VERIFY:
834 case TWFLAG_CHECK:
835 case TWFLAG_NOTRIM:
836 case TWFLAG_VOLDMANAGED:
837 case TWFLAG_RESIZE:
Matt Mower2416a502016-04-12 19:54:46 -0500838 // Do nothing
839 break;
840 case TWFLAG_DISPLAY:
841 Display_Name = str;
842 break;
843 case TWFLAG_ENCRYPTABLE:
844 case TWFLAG_FORCEENCRYPT:
845 Crypto_Key_Location = str;
846 break;
Ethan Yonkere9afc3d2018-08-30 15:16:27 -0500847 case TWFLAG_FILEENCRYPTION:
848 // This flag isn't used by TWRP but is needed in 9.0 FBE decrypt
849 // fileencryption=ice:aes-256-heh
850 {
851 std::string FBE = str;
Ethan Yonkere9afc3d2018-08-30 15:16:27 -0500852 size_t colon_loc = FBE.find(":");
853 if (colon_loc == std::string::npos) {
Ethan Yonker93382822018-11-01 15:25:31 -0500854 property_set("fbe.contents", FBE.c_str());
855 property_set("fbe.filenames", "");
856 LOGINFO("FBE contents '%s', filenames ''\n", FBE.c_str());
Ethan Yonkere9afc3d2018-08-30 15:16:27 -0500857 break;
858 }
Ethan Yonker93382822018-11-01 15:25:31 -0500859 std::string FBE_contents, FBE_filenames;
Ethan Yonkere9afc3d2018-08-30 15:16:27 -0500860 FBE_contents = FBE.substr(0, colon_loc);
861 FBE_filenames = FBE.substr(colon_loc + 1);
862 property_set("fbe.contents", FBE_contents.c_str());
863 property_set("fbe.filenames", FBE_filenames.c_str());
864 LOGINFO("FBE contents '%s', filenames '%s'\n", FBE_contents.c_str(), FBE_filenames.c_str());
865 }
866 break;
Matt Mower2416a502016-04-12 19:54:46 -0500867 case TWFLAG_FLASHIMG:
868 Can_Flash_Img = val;
869 break;
870 case TWFLAG_FSFLAGS:
Matt Mower4ab42b12016-04-21 13:52:18 -0500871 Process_FS_Flags(str);
Matt Mower2416a502016-04-12 19:54:46 -0500872 break;
873 case TWFLAG_IGNOREBLKID:
874 Ignore_Blkid = val;
875 break;
876 case TWFLAG_LENGTH:
877 Length = atoi(str);
878 break;
879 case TWFLAG_MOUNTTODECRYPT:
880 Mount_To_Decrypt = val;
881 break;
882 case TWFLAG_REMOVABLE:
883 Removable = val;
884 break;
885 case TWFLAG_RETAINLAYOUTVERSION:
886 Retain_Layout_Version = val;
887 break;
888 case TWFLAG_SETTINGSSTORAGE:
889 Is_Settings_Storage = val;
890 if (Is_Settings_Storage)
891 Is_Storage = true;
892 break;
893 case TWFLAG_STORAGE:
894 Is_Storage = val;
895 break;
896 case TWFLAG_STORAGENAME:
897 Storage_Name = str;
898 break;
899 case TWFLAG_SUBPARTITIONOF:
900 Is_SubPartition = true;
901 SubPartition_Of = str;
902 break;
903 case TWFLAG_SYMLINK:
904 Symlink_Path = str;
905 break;
906 case TWFLAG_USERDATAENCRYPTBACKUP:
907 Use_Userdata_Encryption = val;
908 if (Use_Userdata_Encryption)
909 Can_Encrypt_Backup = true;
910 break;
911 case TWFLAG_USERMRF:
912 Use_Rm_Rf = val;
913 break;
914 case TWFLAG_WIPEDURINGFACTORYRESET:
915 Wipe_During_Factory_Reset = val;
916 if (Wipe_During_Factory_Reset) {
917 Can_Be_Wiped = true;
918 Wipe_Available_in_GUI = true;
919 }
920 break;
921 case TWFLAG_WIPEINGUI:
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600922 case TWFLAG_FORMATTABLE:
Matt Mower2416a502016-04-12 19:54:46 -0500923 Wipe_Available_in_GUI = val;
924 if (Wipe_Available_in_GUI)
925 Can_Be_Wiped = true;
926 break;
Ethan Yonker1b190162016-12-05 15:25:19 -0600927 case TWFLAG_SLOTSELECT:
928 SlotSelect = true;
929 break;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600930 case TWFLAG_ALTDEVICE:
931 Alternate_Block_Device = str;
932 break;
Ethan Yonker93382822018-11-01 15:25:31 -0500933 case TWFLAG_KEYDIRECTORY:
934 Key_Directory = str;
Matt Mower2416a502016-04-12 19:54:46 -0500935 default:
936 // Should not get here
937 LOGINFO("Flag identified for processing, but later unmatched: %i\n", flag);
938 break;
939 }
940}
Dees_Troy51127312012-09-08 13:08:49 -0400941
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600942void TWPartition::Process_TW_Flags(char *flags, bool Display_Error, int fstab_ver) {
Matt Mower2416a502016-04-12 19:54:46 -0500943 char separator[2] = {'\n', 0};
944 char *ptr, *savep;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600945 char source_separator = ';';
946
947 if (fstab_ver == 2)
948 source_separator = ',';
Matt Mower2416a502016-04-12 19:54:46 -0500949
950 // Semicolons within double-quotes are not forbidden, so replace
951 // only the semicolons intended as separators with '\n' for strtok
952 for (unsigned i = 0, skip = 0; i < strlen(flags); i++) {
953 if (flags[i] == '\"')
Dees_Troya13d74f2013-03-24 08:54:55 -0500954 skip = !skip;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600955 if (!skip && flags[i] == source_separator)
Matt Mower2416a502016-04-12 19:54:46 -0500956 flags[i] = separator[0];
Dees_Troy51127312012-09-08 13:08:49 -0400957 }
958
Matt Mower2416a502016-04-12 19:54:46 -0500959 // Avoid issues with potentially nested strtok by using strtok_r
960 ptr = strtok_r(flags, separator, &savep);
961 while (ptr) {
962 int ptr_len = strlen(ptr);
963 const struct flag_list* tw_flag = tw_flags;
Hashcode62bd9e02013-11-19 21:59:42 -0800964
Matt Mower2416a502016-04-12 19:54:46 -0500965 for (; tw_flag->name; tw_flag++) {
966 int flag_len = strlen(tw_flag->name);
967
968 if (strncmp(ptr, tw_flag->name, flag_len) == 0) {
969 bool flag_val = false;
970
971 if (ptr_len > flag_len && (tw_flag->name)[flag_len-1] != '='
972 && ptr[flag_len] != '=') {
973 // Handle flags with same starting string
974 // (e.g. backup and backupname)
975 continue;
976 } else if (ptr_len > flag_len && ptr[flag_len] == '=') {
977 // Handle flags with dual format: Part 1
978 // (e.g. backup and backup=y. backup=y handled here)
979 ptr += flag_len + 1;
980 TWFunc::Strip_Quotes(ptr);
981 // Skip flags with empty argument
982 // (e.g. backup=)
983 if (strlen(ptr) == 0) {
984 LOGINFO("Flag missing argument or should not include '=': %s=\n", tw_flag->name);
985 break;
986 }
987 flag_val = strchr("yY1", *ptr) != NULL;
988 } else if (ptr_len == flag_len
989 && (tw_flag->name)[flag_len-1] == '=') {
990 // Skip flags missing argument after =
991 // (e.g. backupname=)
992 LOGINFO("Flag missing argument: %s\n", tw_flag->name);
993 break;
994 } else if (ptr_len > flag_len
995 && (tw_flag->name)[flag_len-1] == '=') {
996 // Handle arguments to flags
997 // (e.g. backupname="My Stuff")
998 ptr += flag_len;
999 TWFunc::Strip_Quotes(ptr);
1000 // Skip flags with empty argument
1001 // (e.g. backupname="")
1002 if (strlen(ptr) == 0) {
1003 LOGINFO("Flag missing argument: %s\n", tw_flag->name);
1004 break;
1005 }
1006 } else if (ptr_len == flag_len) {
1007 // Handle flags with dual format: Part 2
1008 // (e.g. backup and backup=y. backup handled here)
1009 flag_val = true;
Ethan Yonker96af84a2015-01-05 14:58:36 -06001010 } else {
Matt Mower2416a502016-04-12 19:54:46 -05001011 LOGINFO("Flag matched, but could not be processed: %s\n", ptr);
1012 break;
Ethan Yonker96af84a2015-01-05 14:58:36 -06001013 }
Matt Mower2416a502016-04-12 19:54:46 -05001014
1015 Apply_TW_Flag(tw_flag->flag, ptr, flag_val);
1016 break;
Ethan Yonker96af84a2015-01-05 14:58:36 -06001017 }
Matt Mower2416a502016-04-12 19:54:46 -05001018 }
1019 if (tw_flag->name == 0) {
Dees_Troy51127312012-09-08 13:08:49 -04001020 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001021 LOGERR("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -04001022 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001023 LOGINFO("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -04001024 }
Matt Mower2416a502016-04-12 19:54:46 -05001025 ptr = strtok_r(NULL, separator, &savep);
Dees_Troy51127312012-09-08 13:08:49 -04001026 }
Dees_Troy51a0e822012-09-05 15:24:24 -04001027}
1028
Dees_Troy5bf43922012-09-07 16:07:55 -04001029bool TWPartition::Is_File_System(string File_System) {
1030 if (File_System == "ext2" ||
Dees_Troy63c8df72012-09-10 14:02:05 -04001031 File_System == "ext3" ||
Dees_Troy5bf43922012-09-07 16:07:55 -04001032 File_System == "ext4" ||
1033 File_System == "vfat" ||
1034 File_System == "ntfs" ||
1035 File_System == "yaffs2" ||
bigbiff bigbiff3e146522012-11-14 14:32:59 -05001036 File_System == "exfat" ||
Dees_Troye5017042013-08-29 16:38:55 +00001037 File_System == "f2fs" ||
Erfan Abdib859e912019-02-05 18:18:46 +02001038 File_System == "squashfs" ||
Dees_Troy5bf43922012-09-07 16:07:55 -04001039 File_System == "auto")
1040 return true;
1041 else
1042 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001043}
1044
Dees_Troy5bf43922012-09-07 16:07:55 -04001045bool TWPartition::Is_Image(string File_System) {
Dees_Troy5fcd8f92012-10-16 12:22:05 -04001046 if (File_System == "emmc" || File_System == "mtd" || File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -04001047 return true;
1048 else
1049 return false;
1050}
1051
Dees_Troy51127312012-09-08 13:08:49 -04001052bool TWPartition::Make_Dir(string Path, bool Display_Error) {
Ethan Yonker93ac7a02016-11-07 22:05:58 -06001053 if (TWFunc::Get_D_Type_From_Stat(Path) != S_IFDIR)
1054 unlink(Path.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001055 if (!TWFunc::Path_Exists(Path)) {
Dees_Troy51127312012-09-08 13:08:49 -04001056 if (mkdir(Path.c_str(), 0777) == -1) {
1057 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001058 gui_msg(Msg(msg::kError, "create_folder_strerr=Can not create '{1}' folder ({2}).")(Path)(strerror(errno)));
Dees_Troy51127312012-09-08 13:08:49 -04001059 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001060 LOGINFO("Can not create '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -04001061 return false;
1062 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001063 LOGINFO("Created '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -04001064 return true;
1065 }
1066 }
1067 return true;
1068}
1069
Dees_Troy5bf43922012-09-07 16:07:55 -04001070void TWPartition::Setup_File_System(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001071 Can_Be_Mounted = true;
1072 Can_Be_Wiped = true;
1073
Dees_Troy5bf43922012-09-07 16:07:55 -04001074 // Make the mount point folder if it doesn't exist
Dees_Troy51127312012-09-08 13:08:49 -04001075 Make_Dir(Mount_Point, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -04001076 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
1077 Backup_Name = Display_Name;
bigbiffce8f83c2015-12-12 18:30:21 -05001078 Backup_Method = BM_FILES;
Dees_Troy5bf43922012-09-07 16:07:55 -04001079}
1080
Ethan Yonker1b190162016-12-05 15:25:19 -06001081void TWPartition::Setup_Image() {
Dees_Troy5bf43922012-09-07 16:07:55 -04001082 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
1083 Backup_Name = Display_Name;
Gary Peck82599a82012-11-21 16:23:12 -08001084 if (Current_File_System == "emmc")
bigbiffce8f83c2015-12-12 18:30:21 -05001085 Backup_Method = BM_DD;
Gary Peck82599a82012-11-21 16:23:12 -08001086 else if (Current_File_System == "mtd" || Current_File_System == "bml")
bigbiffce8f83c2015-12-12 18:30:21 -05001087 Backup_Method = BM_FLASH_UTILS;
Dees_Troy5bf43922012-09-07 16:07:55 -04001088 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001089 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 -04001090}
1091
Dees_Troye58d5262012-09-21 12:27:57 -04001092void TWPartition::Setup_AndSec(void) {
Dees_Troya13d74f2013-03-24 08:54:55 -05001093 Backup_Display_Name = "Android Secure";
Dees_Troye58d5262012-09-21 12:27:57 -04001094 Backup_Name = "and-sec";
Dees_Troya13d74f2013-03-24 08:54:55 -05001095 Can_Be_Backed_Up = true;
Dees_Troye58d5262012-09-21 12:27:57 -04001096 Has_Android_Secure = true;
1097 Symlink_Path = Mount_Point + "/.android_secure";
1098 Symlink_Mount_Point = "/and-sec";
1099 Backup_Path = Symlink_Mount_Point;
1100 Make_Dir("/and-sec", true);
1101 Recreate_AndSec_Folder();
Kjell Braden3126a112016-06-19 16:58:15 +00001102 Mount_Storage_Retry(true);
Dees_Troye58d5262012-09-21 12:27:57 -04001103}
1104
that9e0593e2014-10-08 00:01:24 +02001105void TWPartition::Setup_Data_Media() {
Ethan Yonker6277c792014-09-15 14:54:30 -05001106 LOGINFO("Setting up '%s' as data/media emulated storage.\n", Mount_Point.c_str());
Ethan Yonker66a19492015-12-10 10:19:45 -06001107 if (Storage_Name.empty() || Storage_Name == "Data")
1108 Storage_Name = "Internal Storage";
Ethan Yonker6277c792014-09-15 14:54:30 -05001109 Has_Data_Media = true;
1110 Is_Storage = true;
Ethan Yonker66a19492015-12-10 10:19:45 -06001111 Storage_Path = Mount_Point + "/media";
Ethan Yonker6277c792014-09-15 14:54:30 -05001112 Symlink_Path = Storage_Path;
Ethan Yonker66a19492015-12-10 10:19:45 -06001113 if (Mount_Point == "/data") {
1114 Is_Settings_Storage = true;
1115 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
1116 Make_Dir("/emmc", false);
1117 Symlink_Mount_Point = "/emmc";
1118 } else {
1119 Make_Dir("/sdcard", false);
1120 Symlink_Mount_Point = "/sdcard";
1121 }
1122 if (Mount(false) && TWFunc::Path_Exists(Mount_Point + "/media/0")) {
1123 Storage_Path = Mount_Point + "/media/0";
1124 Symlink_Path = Storage_Path;
1125 DataManager::SetValue(TW_INTERNAL_PATH, Mount_Point + "/media/0");
1126 UnMount(true);
1127 }
1128 DataManager::SetValue("tw_has_internal", 1);
1129 DataManager::SetValue("tw_has_data_media", 1);
Ethan Yonker3fdcda42016-11-30 12:29:37 -06001130 backup_exclusions.add_absolute_dir("/data/data/com.google.android.music/files");
Ethan Yonker6355b562017-05-01 09:42:17 -05001131 wipe_exclusions.add_absolute_dir(Mount_Point + "/misc/vold"); // adopted storage keys
Ethan Yonker79f88bd2016-12-09 14:52:12 -06001132 ExcludeAll(Mount_Point + "/.layout_version");
1133 ExcludeAll(Mount_Point + "/system/storage.xml");
Ethan Yonker6277c792014-09-15 14:54:30 -05001134 } else {
Ethan Yonker66a19492015-12-10 10:19:45 -06001135 if (Mount(true) && TWFunc::Path_Exists(Mount_Point + "/media/0")) {
1136 Storage_Path = Mount_Point + "/media/0";
1137 Symlink_Path = Storage_Path;
1138 UnMount(true);
1139 }
Ethan Yonker6277c792014-09-15 14:54:30 -05001140 }
Ethan Yonker79f88bd2016-12-09 14:52:12 -06001141 ExcludeAll(Mount_Point + "/media");
Ethan Yonker6277c792014-09-15 14:54:30 -05001142}
1143
Dees_Troy5bf43922012-09-07 16:07:55 -04001144void TWPartition::Find_Real_Block_Device(string& Block, bool Display_Error) {
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06001145 char device[PATH_MAX], realDevice[PATH_MAX];
Dees_Troy5bf43922012-09-07 16:07:55 -04001146
1147 strcpy(device, Block.c_str());
1148 memset(realDevice, 0, sizeof(realDevice));
1149 while (readlink(device, realDevice, sizeof(realDevice)) > 0)
1150 {
1151 strcpy(device, realDevice);
1152 memset(realDevice, 0, sizeof(realDevice));
1153 }
1154
1155 if (device[0] != '/') {
1156 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001157 LOGERR("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001158 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001159 LOGINFO("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001160 return;
Dees_Troy51a0e822012-09-05 15:24:24 -04001161 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -04001162 Block = device;
1163 return;
1164 }
1165}
1166
Kjell Braden3126a112016-06-19 16:58:15 +00001167bool TWPartition::Mount_Storage_Retry(bool Display_Error) {
Dees_Troy8e337f32012-10-13 22:07:49 -04001168 // On some devices, storage doesn't want to mount right away, retry and sleep
Kjell Braden3126a112016-06-19 16:58:15 +00001169 if (!Mount(Display_Error)) {
Dees_Troy8e337f32012-10-13 22:07:49 -04001170 int retry_count = 5;
1171 while (retry_count > 0 && !Mount(false)) {
1172 usleep(500000);
1173 retry_count--;
1174 }
Kjell Braden3126a112016-06-19 16:58:15 +00001175 return Mount(Display_Error);
Dees_Troy8e337f32012-10-13 22:07:49 -04001176 }
Kjell Braden3126a112016-06-19 16:58:15 +00001177 return true;
Dees_Troy8e337f32012-10-13 22:07:49 -04001178}
1179
Dees_Troy38bd7602012-09-14 13:33:53 -04001180bool TWPartition::Find_MTD_Block_Device(string MTD_Name) {
1181 FILE *fp = NULL;
1182 char line[255];
1183
1184 fp = fopen("/proc/mtd", "rt");
1185 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001186 LOGERR("Device does not support /proc/mtd\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001187 return false;
1188 }
1189
1190 while (fgets(line, sizeof(line), fp) != NULL)
1191 {
1192 char device[32], label[32];
1193 unsigned long size = 0;
Dees_Troy38bd7602012-09-14 13:33:53 -04001194 int deviceId;
1195
1196 sscanf(line, "%s %lx %*s %*c%s", device, &size, label);
1197
1198 // Skip header and blank lines
1199 if ((strcmp(device, "dev:") == 0) || (strlen(line) < 8))
1200 continue;
1201
1202 // Strip off the trailing " from the label
1203 label[strlen(label)-1] = '\0';
1204
1205 if (strcmp(label, MTD_Name.c_str()) == 0) {
1206 // We found our device
1207 // Strip off the trailing : from the device
1208 device[strlen(device)-1] = '\0';
1209 if (sscanf(device,"mtd%d", &deviceId) == 1) {
1210 sprintf(device, "/dev/block/mtdblock%d", deviceId);
1211 Primary_Block_Device = device;
Dees_Troy76543db2013-06-19 16:24:30 +00001212 fclose(fp);
1213 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -04001214 }
1215 }
1216 }
1217 fclose(fp);
1218
1219 return false;
1220}
1221
Dees_Troy51127312012-09-08 13:08:49 -04001222bool TWPartition::Get_Size_Via_statfs(bool Display_Error) {
1223 struct statfs st;
1224 string Local_Path = Mount_Point + "/.";
1225
1226 if (!Mount(Display_Error))
1227 return false;
1228
1229 if (statfs(Local_Path.c_str(), &st) != 0) {
1230 if (!Removable) {
1231 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001232 LOGERR("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -04001233 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001234 LOGINFO("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -04001235 }
1236 return false;
1237 }
1238 Size = (st.f_blocks * st.f_bsize);
1239 Used = ((st.f_blocks - st.f_bfree) * st.f_bsize);
1240 Free = (st.f_bfree * st.f_bsize);
1241 Backup_Size = Used;
1242 return true;
1243}
1244
1245bool TWPartition::Get_Size_Via_df(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001246 FILE* fp;
1247 char command[255], line[512];
1248 int include_block = 1;
1249 unsigned int min_len;
1250
1251 if (!Mount(Display_Error))
1252 return false;
1253
Dees_Troy38bd7602012-09-14 13:33:53 -04001254 min_len = Actual_Block_Device.size() + 2;
Dees_Troy51127312012-09-08 13:08:49 -04001255 sprintf(command, "df %s > /tmp/dfoutput.txt", Mount_Point.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001256 TWFunc::Exec_Cmd(command);
Dees_Troy51127312012-09-08 13:08:49 -04001257 fp = fopen("/tmp/dfoutput.txt", "rt");
1258 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001259 LOGINFO("Unable to open /tmp/dfoutput.txt.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001260 return false;
Dees_Troy51127312012-09-08 13:08:49 -04001261 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001262
1263 while (fgets(line, sizeof(line), fp) != NULL)
1264 {
1265 unsigned long blocks, used, available;
1266 char device[64];
1267 char tmpString[64];
1268
1269 if (strncmp(line, "Filesystem", 10) == 0)
1270 continue;
1271 if (strlen(line) < min_len) {
1272 include_block = 0;
1273 continue;
1274 }
1275 if (include_block) {
1276 sscanf(line, "%s %lu %lu %lu", device, &blocks, &used, &available);
1277 } else {
1278 // The device block string is so long that the df information is on the next line
1279 int space_count = 0;
Dees_Troye58d5262012-09-21 12:27:57 -04001280 sprintf(tmpString, "/dev/block/%s", Actual_Block_Device.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001281 while (tmpString[space_count] == 32)
1282 space_count++;
1283 sscanf(line + space_count, "%lu %lu %lu", &blocks, &used, &available);
1284 }
1285
1286 // Adjust block size to byte size
1287 Size = blocks * 1024ULL;
1288 Used = used * 1024ULL;
1289 Free = available * 1024ULL;
1290 Backup_Size = Used;
1291 }
1292 fclose(fp);
1293 return true;
1294}
1295
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001296unsigned long long TWPartition::IOCTL_Get_Block_Size() {
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001297 Find_Actual_Block_Device();
Ethan Yonker483e9f42016-01-11 22:21:18 -06001298
1299 return TWFunc::IOCTL_Get_Block_Size(Actual_Block_Device.c_str());
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001300}
1301
Dees_Troy5bf43922012-09-07 16:07:55 -04001302bool TWPartition::Find_Partition_Size(void) {
1303 FILE* fp;
1304 char line[512];
1305 string tmpdevice;
1306
igoriok87e3d932013-01-31 21:03:53 +02001307 fp = fopen("/proc/dumchar_info", "rt");
1308 if (fp != NULL) {
1309 while (fgets(line, sizeof(line), fp) != NULL)
1310 {
1311 char label[32], device[32];
1312 unsigned long size = 0;
1313
thatd43bf2d2014-09-21 23:13:02 +02001314 sscanf(line, "%s %lx %*x %*u %s", label, &size, device);
igoriok87e3d932013-01-31 21:03:53 +02001315
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05001316 // Skip header, annotation and blank lines
igoriok87e3d932013-01-31 21:03:53 +02001317 if ((strncmp(device, "/dev/", 5) != 0) || (strlen(line) < 8))
1318 continue;
1319
1320 tmpdevice = "/dev/";
1321 tmpdevice += label;
1322 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
1323 Size = size;
1324 fclose(fp);
1325 return true;
1326 }
1327 }
1328 }
1329
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001330 unsigned long long ioctl_size = IOCTL_Get_Block_Size();
1331 if (ioctl_size) {
1332 Size = ioctl_size;
1333 return true;
1334 }
1335
Dees_Troy5bf43922012-09-07 16:07:55 -04001336 // In this case, we'll first get the partitions we care about (with labels)
1337 fp = fopen("/proc/partitions", "rt");
1338 if (fp == NULL)
1339 return false;
1340
1341 while (fgets(line, sizeof(line), fp) != NULL)
1342 {
1343 unsigned long major, minor, blocks;
1344 char device[512];
Dees_Troy5bf43922012-09-07 16:07:55 -04001345
Dees_Troy63c8df72012-09-10 14:02:05 -04001346 if (strlen(line) < 7 || line[0] == 'm') continue;
Dees_Troy5bf43922012-09-07 16:07:55 -04001347 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
1348
1349 tmpdevice = "/dev/block/";
1350 tmpdevice += device;
Dees_Troy38bd7602012-09-14 13:33:53 -04001351 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001352 // Adjust block size to byte size
1353 Size = blocks * 1024ULL;
1354 fclose(fp);
1355 return true;
1356 }
1357 }
1358 fclose(fp);
1359 return false;
1360}
1361
Dees_Troy5bf43922012-09-07 16:07:55 -04001362bool TWPartition::Is_Mounted(void) {
1363 if (!Can_Be_Mounted)
1364 return false;
1365
1366 struct stat st1, st2;
1367 string test_path;
1368
1369 // Check to see if the mount point directory exists
1370 test_path = Mount_Point + "/.";
1371 if (stat(test_path.c_str(), &st1) != 0) return false;
1372
1373 // Check to see if the directory above the mount point exists
1374 test_path = Mount_Point + "/../.";
1375 if (stat(test_path.c_str(), &st2) != 0) return false;
1376
1377 // Compare the device IDs -- if they match then we're (probably) using tmpfs instead of an actual device
1378 int ret = (st1.st_dev != st2.st_dev) ? true : false;
1379
1380 return ret;
1381}
1382
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05001383bool TWPartition::Is_File_System_Writable(void) {
1384 if (!Is_File_System(Current_File_System) || !Is_Mounted())
1385 return false;
1386
1387 string test_path = Mount_Point + "/.";
1388 return (access(test_path.c_str(), W_OK) == 0);
1389}
1390
Dees_Troy5bf43922012-09-07 16:07:55 -04001391bool TWPartition::Mount(bool Display_Error) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001392 int exfat_mounted = 0;
Martin Dünkelmann6d843782018-12-27 23:40:28 +01001393 unsigned int flags = Mount_Flags;
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001394
Dees_Troy5bf43922012-09-07 16:07:55 -04001395 if (Is_Mounted()) {
1396 return true;
1397 } else if (!Can_Be_Mounted) {
1398 return false;
1399 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001400
1401 Find_Actual_Block_Device();
1402
1403 // Check the current file system before mounting
1404 Check_FS_Type();
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001405 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/exfat-fuse")) {
Dees_Troye34c1332013-02-06 19:13:00 +00001406 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 +00001407 LOGINFO("cmd: %s\n", cmd.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001408 string result;
1409 if (TWFunc::Exec_Cmd(cmd, result) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001410 LOGINFO("exfat-fuse failed to mount with result '%s', trying vfat\n", result.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001411 Current_File_System = "vfat";
1412 } else {
1413#ifdef TW_NO_EXFAT_FUSE
1414 UnMount(false);
1415 // We'll let the kernel handle it but using exfat-fuse to detect if the file system is actually exfat
1416 // Some kernels let us mount vfat as exfat which doesn't work out too well
1417#else
1418 exfat_mounted = 1;
1419#endif
1420 }
1421 }
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001422
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001423 if (Current_File_System == "ntfs" && (TWFunc::Path_Exists("/sbin/ntfs-3g") || TWFunc::Path_Exists("/sbin/mount.ntfs"))) {
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001424 string cmd;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001425 string Ntfsmount_Binary = "";
1426
1427 if (TWFunc::Path_Exists("/sbin/ntfs-3g"))
1428 Ntfsmount_Binary = "ntfs-3g";
1429 else if (TWFunc::Path_Exists("/sbin/mount.ntfs"))
1430 Ntfsmount_Binary = "mount.ntfs";
1431
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001432 if (Mount_Read_Only)
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001433 cmd = "/sbin/" + Ntfsmount_Binary + " -o ro " + Actual_Block_Device + " " + Mount_Point;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001434 else
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001435 cmd = "/sbin/" + Ntfsmount_Binary + " " + Actual_Block_Device + " " + Mount_Point;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001436 LOGINFO("cmd: '%s'\n", cmd.c_str());
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001437
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001438 if (TWFunc::Exec_Cmd(cmd) == 0) {
1439 return true;
1440 } else {
1441 LOGINFO("ntfs-3g failed to mount, trying regular mount method.\n");
1442 }
1443 }
1444
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001445 if (Mount_Read_Only)
1446 flags |= MS_RDONLY;
1447
Dees_Troy22042032012-12-18 21:23:08 +00001448 if (Fstab_File_System == "yaffs2") {
1449 // mount an MTD partition as a YAFFS2 filesystem.
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001450 flags = MS_NOATIME | MS_NODEV | MS_NODIRATIME;
1451 if (Mount_Read_Only)
1452 flags |= MS_RDONLY;
Dees_Troy76543db2013-06-19 16:24:30 +00001453 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags, NULL) < 0) {
1454 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags | MS_RDONLY, NULL) < 0) {
1455 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001456 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy76543db2013-06-19 16:24:30 +00001457 else
1458 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
1459 return false;
1460 } else {
1461 LOGINFO("Mounted '%s' (MTD) as RO\n", Mount_Point.c_str());
1462 return true;
1463 }
1464 } else {
1465 struct stat st;
1466 string test_path = Mount_Point;
1467 if (stat(test_path.c_str(), &st) < 0) {
1468 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001469 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy76543db2013-06-19 16:24:30 +00001470 else
1471 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
1472 return false;
1473 }
1474 mode_t new_mode = st.st_mode | S_IXUSR | S_IXGRP | S_IXOTH;
1475 if (new_mode != st.st_mode) {
1476 LOGINFO("Fixing execute permissions for %s\n", Mount_Point.c_str());
1477 if (chmod(Mount_Point.c_str(), new_mode) < 0) {
1478 if (Display_Error)
1479 LOGERR("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
1480 else
1481 LOGINFO("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
1482 return false;
1483 }
1484 }
Dees_Troy22042032012-12-18 21:23:08 +00001485 return true;
Dees_Troy76543db2013-06-19 16:24:30 +00001486 }
thatc7572eb2015-03-31 18:55:30 +02001487 }
1488
1489 string mount_fs = Current_File_System;
1490 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sys/module/texfat"))
1491 mount_fs = "texfat";
1492
1493 if (!exfat_mounted &&
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001494 mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), mount_fs.c_str(), flags, Mount_Options.c_str()) != 0 &&
1495 mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), mount_fs.c_str(), flags, NULL) != 0) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001496#ifdef TW_NO_EXFAT_FUSE
1497 if (Current_File_System == "exfat") {
Dees_Troy2673cec2013-04-02 20:22:16 +00001498 LOGINFO("Mounting exfat failed, trying vfat...\n");
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001499 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), "vfat", 0, NULL) != 0) {
Dees_Troy85f44ed2013-01-09 18:42:36 +00001500 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001501 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy85f44ed2013-01-09 18:42:36 +00001502 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001503 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001504 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 +00001505 return false;
Dees_Troy85f44ed2013-01-09 18:42:36 +00001506 }
Dees_Troyb05ddee2013-01-28 20:24:50 +00001507 } else {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001508#endif
bigbiff bigbiff26774a02014-03-29 18:22:00 -04001509 if (!Removable && Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001510 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001511 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001512 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
1513 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 +00001514 return false;
1515#ifdef TW_NO_EXFAT_FUSE
Dees_Troy85f44ed2013-01-09 18:42:36 +00001516 }
1517#endif
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001518 }
Ethan Yonker253368a2014-11-25 15:00:52 -06001519
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001520 if (Removable)
1521 Update_Size(Display_Error);
1522
xiaolu9416f4f2015-06-04 08:22:23 +08001523 if (!Symlink_Mount_Point.empty() && TWFunc::Path_Exists(Symlink_Path)) {
codelover2a3d4ce2015-03-14 20:26:49 +08001524 string Command = "mount -o bind '" + Symlink_Path + "' '" + Symlink_Mount_Point + "'";
Vojtech Bocek05534202013-09-11 08:11:56 +02001525 TWFunc::Exec_Cmd(Command);
Dees_Troy5bf43922012-09-07 16:07:55 -04001526 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001527 return true;
1528}
1529
1530bool TWPartition::UnMount(bool Display_Error) {
1531 if (Is_Mounted()) {
1532 int never_unmount_system;
1533
1534 DataManager::GetValue(TW_DONT_UNMOUNT_SYSTEM, never_unmount_system);
Captain Throwback9d6feb52018-07-27 10:05:24 -04001535 if (never_unmount_system == 1 && Mount_Point == PartitionManager.Get_Android_Root_Path())
Dees_Troy5bf43922012-09-07 16:07:55 -04001536 return true; // Never unmount system if you're not supposed to unmount it
1537
Matt Mowera8e6d832017-02-14 20:20:59 -06001538 if (Is_Storage && MTP_Storage_ID > 0)
Ethan Yonker726a0202014-12-16 20:01:38 -06001539 PartitionManager.Remove_MTP_Storage(MTP_Storage_ID);
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001540
Dees_Troy38bd7602012-09-14 13:33:53 -04001541 if (!Symlink_Mount_Point.empty())
1542 umount(Symlink_Mount_Point.c_str());
1543
Dees_Troyb05ddee2013-01-28 20:24:50 +00001544 umount(Mount_Point.c_str());
1545 if (Is_Mounted()) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001546 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001547 gui_msg(Msg(msg::kError, "fail_unmount=Failed to unmount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy5bf43922012-09-07 16:07:55 -04001548 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001549 LOGINFO("Unable to unmount '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001550 return false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001551 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -04001552 return true;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001553 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001554 } else {
1555 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001556 }
1557}
1558
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05001559bool TWPartition::ReMount(bool Display_Error) {
1560 if (UnMount(Display_Error))
1561 return Mount(Display_Error);
1562 return false;
1563}
1564
1565bool TWPartition::ReMount_RW(bool Display_Error) {
1566 // No need to remount if already mounted rw
1567 if (Is_File_System_Writable())
1568 return true;
1569
1570 bool ro = Mount_Read_Only;
1571 int flags = Mount_Flags;
1572
1573 Mount_Read_Only = false;
1574 Mount_Flags &= ~MS_RDONLY;
1575
1576 bool ret = ReMount(Display_Error);
1577
1578 Mount_Read_Only = ro;
1579 Mount_Flags = flags;
1580
1581 return ret;
1582}
1583
Gary Peck43acadf2012-11-21 21:19:01 -08001584bool TWPartition::Wipe(string New_File_System) {
Ethan Yonker726a0202014-12-16 20:01:38 -06001585 bool wiped = false, update_crypt = false, recreate_media = true;
Dees_Troy16c2b312013-01-15 16:51:18 +00001586 int check;
1587 string Layout_Filename = Mount_Point + "/.layout_version";
1588
Dees_Troy38bd7602012-09-14 13:33:53 -04001589 if (!Can_Be_Wiped) {
Matt Mower3c366972015-12-25 19:28:31 -06001590 gui_msg(Msg(msg::kError, "cannot_wipe=Partition {1} cannot be wiped.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04001591 return false;
1592 }
1593
Dees_Troyc51f1f92012-09-20 15:32:13 -04001594 if (Mount_Point == "/cache")
Dees_Troy2673cec2013-04-02 20:22:16 +00001595 Log_Offset = 0;
Dees_Troyc51f1f92012-09-20 15:32:13 -04001596
Dees_Troy16c2b312013-01-15 16:51:18 +00001597 if (Retain_Layout_Version && Mount(false) && TWFunc::Path_Exists(Layout_Filename))
1598 TWFunc::copy_file(Layout_Filename, "/.layout_version", 0600);
1599 else
1600 unlink("/.layout_version");
Dees_Troy38bd7602012-09-14 13:33:53 -04001601
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001602 if (Has_Data_Media && Current_File_System == New_File_System) {
Dees_Troy16c2b312013-01-15 16:51:18 +00001603 wiped = Wipe_Data_Without_Wiping_Media();
Ethan Yonker5eac2222014-06-11 12:22:55 -05001604 recreate_media = false;
Dees_Troy16c2b312013-01-15 16:51:18 +00001605 } else {
Dees_Troy16c2b312013-01-15 16:51:18 +00001606 DataManager::GetValue(TW_RM_RF_VAR, check);
1607
Hashcodedabfd492013-08-29 22:45:30 -07001608 if (check || Use_Rm_Rf)
Dees_Troy16c2b312013-01-15 16:51:18 +00001609 wiped = Wipe_RMRF();
1610 else if (New_File_System == "ext4")
1611 wiped = Wipe_EXT4();
1612 else if (New_File_System == "ext2" || New_File_System == "ext3")
dianlujitao4879b372018-12-03 18:45:47 +08001613 wiped = Wipe_EXTFS(New_File_System);
Dees_Troy16c2b312013-01-15 16:51:18 +00001614 else if (New_File_System == "vfat")
1615 wiped = Wipe_FAT();
1616 else if (New_File_System == "exfat")
1617 wiped = Wipe_EXFAT();
1618 else if (New_File_System == "yaffs2")
1619 wiped = Wipe_MTD();
Dees_Troye5017042013-08-29 16:38:55 +00001620 else if (New_File_System == "f2fs")
1621 wiped = Wipe_F2FS();
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001622 else if (New_File_System == "ntfs")
1623 wiped = Wipe_NTFS();
Dees_Troy16c2b312013-01-15 16:51:18 +00001624 else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001625 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 +00001626 unlink("/.layout_version");
1627 return false;
1628 }
1629 update_crypt = wiped;
Gary Pecke8bc5d72012-12-21 06:45:25 -08001630 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001631
Gary Pecke8bc5d72012-12-21 06:45:25 -08001632 if (wiped) {
Dees_Troy1c1ac442013-01-17 21:42:14 +00001633 if (Mount_Point == "/cache")
1634 DataManager::Output_Version();
1635
Dees_Troy16c2b312013-01-15 16:51:18 +00001636 if (TWFunc::Path_Exists("/.layout_version") && Mount(false))
1637 TWFunc::copy_file("/.layout_version", Layout_Filename, 0600);
1638
1639 if (update_crypt) {
1640 Setup_File_System(false);
1641 if (Is_Encrypted && !Is_Decrypted) {
1642 // just wiped an encrypted partition back to its unencrypted state
1643 Is_Encrypted = false;
1644 Is_Decrypted = false;
1645 Decrypted_Block_Device = "";
1646 if (Mount_Point == "/data") {
1647 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1648 DataManager::SetValue(TW_IS_DECRYPTED, 0);
1649 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001650 }
1651 }
Ethan Yonker5eac2222014-06-11 12:22:55 -05001652
Ethan Yonker66a19492015-12-10 10:19:45 -06001653 if (Has_Data_Media && recreate_media) {
Ethan Yonker5eac2222014-06-11 12:22:55 -05001654 Recreate_Media_Folder();
1655 }
Matt Mower209c9632016-01-20 12:08:35 -06001656 if (Is_Storage && Mount(false))
1657 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Gary Pecke8bc5d72012-12-21 06:45:25 -08001658 }
Matt Mower209c9632016-01-20 12:08:35 -06001659
Gary Pecke8bc5d72012-12-21 06:45:25 -08001660 return wiped;
Dees_Troy51a0e822012-09-05 15:24:24 -04001661}
1662
Gary Peck43acadf2012-11-21 21:19:01 -08001663bool TWPartition::Wipe() {
Gary Peck82599a82012-11-21 16:23:12 -08001664 if (Is_File_System(Current_File_System))
1665 return Wipe(Current_File_System);
1666 else
1667 return Wipe(Fstab_File_System);
Gary Peck43acadf2012-11-21 21:19:01 -08001668}
1669
Dees_Troye58d5262012-09-21 12:27:57 -04001670bool TWPartition::Wipe_AndSec(void) {
1671 if (!Has_Android_Secure)
1672 return false;
1673
Dees_Troye58d5262012-09-21 12:27:57 -04001674 if (!Mount(true))
1675 return false;
1676
Ethan Yonker74db1572015-10-28 12:44:49 -05001677 gui_msg(Msg("wiping=Wiping {1}")(Backup_Display_Name));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001678 TWFunc::removeDir(Mount_Point + "/.android_secure/", true);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001679 return true;
Dees_Troye58d5262012-09-21 12:27:57 -04001680}
1681
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001682bool TWPartition::Can_Repair() {
Ethan Yonkera2719152015-05-28 09:44:41 -05001683 if (Mount_Read_Only)
1684 return false;
Matt Mower18794c82015-11-11 16:22:45 -06001685 if (Current_File_System == "vfat" && TWFunc::Path_Exists("/sbin/fsck.fat"))
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001686 return true;
1687 else if ((Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") && TWFunc::Path_Exists("/sbin/e2fsck"))
1688 return true;
1689 else if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/fsck.exfat"))
1690 return true;
1691 else if (Current_File_System == "f2fs" && TWFunc::Path_Exists("/sbin/fsck.f2fs"))
1692 return true;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001693 else if (Current_File_System == "ntfs" && (TWFunc::Path_Exists("/sbin/ntfsfix") || TWFunc::Path_Exists("/sbin/fsck.ntfs")))
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001694 return true;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001695 return false;
1696}
1697
1698bool TWPartition::Repair() {
1699 string command;
1700
1701 if (Current_File_System == "vfat") {
Matt Mower18794c82015-11-11 16:22:45 -06001702 if (!TWFunc::Path_Exists("/sbin/fsck.fat")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001703 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.fat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001704 return false;
1705 }
1706 if (!UnMount(true))
1707 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001708 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.fat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001709 Find_Actual_Block_Device();
Matt Mower18794c82015-11-11 16:22:45 -06001710 command = "/sbin/fsck.fat -y " + Actual_Block_Device;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001711 LOGINFO("Repair command: %s\n", command.c_str());
1712 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001713 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001714 return true;
1715 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001716 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001717 return false;
1718 }
1719 }
1720 if (Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") {
1721 if (!TWFunc::Path_Exists("/sbin/e2fsck")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001722 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("e2fsck"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001723 return false;
1724 }
1725 if (!UnMount(true))
1726 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001727 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("e2fsck"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001728 Find_Actual_Block_Device();
Ethan Yonkera2719152015-05-28 09:44:41 -05001729 command = "/sbin/e2fsck -fp " + Actual_Block_Device;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001730 LOGINFO("Repair command: %s\n", command.c_str());
1731 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001732 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001733 return true;
1734 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001735 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001736 return false;
1737 }
1738 }
1739 if (Current_File_System == "exfat") {
1740 if (!TWFunc::Path_Exists("/sbin/fsck.exfat")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001741 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.exfat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001742 return false;
1743 }
1744 if (!UnMount(true))
1745 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001746 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.exfat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001747 Find_Actual_Block_Device();
1748 command = "/sbin/fsck.exfat " + Actual_Block_Device;
1749 LOGINFO("Repair command: %s\n", command.c_str());
1750 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001751 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001752 return true;
1753 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001754 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001755 return false;
1756 }
1757 }
1758 if (Current_File_System == "f2fs") {
1759 if (!TWFunc::Path_Exists("/sbin/fsck.f2fs")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001760 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.f2fs"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001761 return false;
1762 }
1763 if (!UnMount(true))
1764 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001765 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.f2fs"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001766 Find_Actual_Block_Device();
1767 command = "/sbin/fsck.f2fs " + Actual_Block_Device;
1768 LOGINFO("Repair command: %s\n", command.c_str());
1769 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001770 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001771 return true;
1772 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001773 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001774 return false;
1775 }
1776 }
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001777 if (Current_File_System == "ntfs") {
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001778 string Ntfsfix_Binary;
1779 if (TWFunc::Path_Exists("/sbin/ntfsfix"))
1780 Ntfsfix_Binary = "ntfsfix";
1781 else if (TWFunc::Path_Exists("/sbin/fsck.ntfs"))
1782 Ntfsfix_Binary = "fsck.ntfs";
1783 else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001784 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("ntfsfix"));
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001785 return false;
1786 }
1787 if (!UnMount(true))
1788 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001789 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)(Ntfsfix_Binary));
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001790 Find_Actual_Block_Device();
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001791 command = "/sbin/" + Ntfsfix_Binary + " " + Actual_Block_Device;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001792 LOGINFO("Repair command: %s\n", command.c_str());
1793 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001794 gui_msg("done=Done.");
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001795 return true;
1796 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001797 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001798 return false;
1799 }
1800 }
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001801 return false;
1802}
1803
Ethan Yonkera2719152015-05-28 09:44:41 -05001804bool TWPartition::Can_Resize() {
1805 if (Mount_Read_Only)
1806 return false;
1807 if ((Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") && TWFunc::Path_Exists("/sbin/resize2fs"))
1808 return true;
1809 return false;
1810}
1811
1812bool TWPartition::Resize() {
1813 string command;
1814
1815 if (Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") {
1816 if (!Can_Repair()) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001817 LOGINFO("Cannot resize %s because %s cannot be repaired before resizing.\n", Display_Name.c_str(), Display_Name.c_str());
1818 gui_msg(Msg(msg::kError, "cannot_resize=Cannot resize {1}.")(Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001819 return false;
1820 }
1821 if (!TWFunc::Path_Exists("/sbin/resize2fs")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001822 LOGINFO("resize2fs does not exist! Cannot resize!\n");
1823 gui_msg(Msg(msg::kError, "cannot_resize=Cannot resize {1}.")(Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001824 return false;
1825 }
1826 // Repair will unmount so no need to do it twice
Ethan Yonker74db1572015-10-28 12:44:49 -05001827 gui_msg(Msg("repair_resize=Repairing {1} before resizing.")( Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001828 if (!Repair())
1829 return false;
Ethan Yonker74db1572015-10-28 12:44:49 -05001830 gui_msg(Msg("resizing=Resizing {1} using {2}...")(Display_Name)("resize2fs"));
Ethan Yonkera2719152015-05-28 09:44:41 -05001831 Find_Actual_Block_Device();
1832 command = "/sbin/resize2fs " + Actual_Block_Device;
1833 if (Length != 0) {
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001834 unsigned long long Actual_Size = IOCTL_Get_Block_Size();
1835 if (Actual_Size == 0)
1836 return false;
Ethan Yonkera2719152015-05-28 09:44:41 -05001837
Ethan Yonkera2719152015-05-28 09:44:41 -05001838 unsigned long long Block_Count;
1839 if (Length < 0) {
1840 // Reduce overall size by this length
1841 Block_Count = (Actual_Size / 1024LLU) - ((unsigned long long)(Length * -1) / 1024LLU);
1842 } else {
1843 // This is the size, not a size reduction
1844 Block_Count = ((unsigned long long)(Length) / 1024LLU);
1845 }
1846 char temp[256];
1847 sprintf(temp, "%llu", Block_Count);
1848 command += " ";
1849 command += temp;
1850 command += "K";
1851 }
1852 LOGINFO("Resize command: %s\n", command.c_str());
1853 if (TWFunc::Exec_Cmd(command) == 0) {
1854 Update_Size(true);
Ethan Yonker74db1572015-10-28 12:44:49 -05001855 gui_msg("done=Done.");
Ethan Yonkera2719152015-05-28 09:44:41 -05001856 return true;
1857 } else {
1858 Update_Size(true);
Ethan Yonker74db1572015-10-28 12:44:49 -05001859 gui_msg(Msg(msg::kError, "unable_resize=Unable to resize {1}.")(Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001860 return false;
1861 }
1862 }
1863 return false;
1864}
1865
bigbiffce8f83c2015-12-12 18:30:21 -05001866bool TWPartition::Backup(PartitionSettings *part_settings, pid_t *tar_fork_pid) {
1867 if (Backup_Method == BM_FILES)
1868 return Backup_Tar(part_settings, tar_fork_pid);
1869 else if (Backup_Method == BM_DD)
1870 return Backup_Image(part_settings);
1871 else if (Backup_Method == BM_FLASH_UTILS)
1872 return Backup_Dump_Image(part_settings);
Dees_Troy2673cec2013-04-02 20:22:16 +00001873 LOGERR("Unknown backup method for '%s'\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001874 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001875}
1876
bigbiffce8f83c2015-12-12 18:30:21 -05001877bool TWPartition::Restore(PartitionSettings *part_settings) {
Matt Mower3c366972015-12-25 19:28:31 -06001878 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, gui_parse_text("{@restoring_hdr}"));
Ethan Yonkere080c1f2016-09-19 13:50:25 -05001879 LOGINFO("Restore filename is: %s/%s\n", part_settings->Backup_Folder.c_str(), Backup_FileName.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001880
bigbiffce8f83c2015-12-12 18:30:21 -05001881 string Restore_File_System = Get_Restore_File_System(part_settings);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001882
1883 if (Is_File_System(Restore_File_System))
bigbiffce8f83c2015-12-12 18:30:21 -05001884 return Restore_Tar(part_settings);
1885 else if (Is_Image(Restore_File_System))
1886 return Restore_Image(part_settings);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001887
1888 LOGERR("Unknown restore method for '%s'\n", Mount_Point.c_str());
1889 return false;
1890}
1891
bigbiffce8f83c2015-12-12 18:30:21 -05001892string TWPartition::Get_Restore_File_System(PartitionSettings *part_settings) {
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001893 size_t first_period, second_period;
1894 string Restore_File_System;
1895
Gary Peck43acadf2012-11-21 21:19:01 -08001896 // Parse backup filename to extract the file system before wiping
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05001897 first_period = Backup_FileName.find(".");
Gary Peck43acadf2012-11-21 21:19:01 -08001898 if (first_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001899 LOGERR("Unable to find file system (first period).\n");
thatd43bf2d2014-09-21 23:13:02 +02001900 return string();
Gary Peck43acadf2012-11-21 21:19:01 -08001901 }
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05001902 Restore_File_System = Backup_FileName.substr(first_period + 1, Backup_FileName.size() - first_period - 1);
Gary Peck43acadf2012-11-21 21:19:01 -08001903 second_period = Restore_File_System.find(".");
1904 if (second_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001905 LOGERR("Unable to find file system (second period).\n");
thatd43bf2d2014-09-21 23:13:02 +02001906 return string();
Gary Peck43acadf2012-11-21 21:19:01 -08001907 }
1908 Restore_File_System.resize(second_period);
Matt Mowera0cd91d2016-12-30 18:21:42 -06001909 LOGINFO("Restore file system is: '%s'.\n", Restore_File_System.c_str());
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001910 return Restore_File_System;
Dees_Troy51a0e822012-09-05 15:24:24 -04001911}
1912
1913string TWPartition::Backup_Method_By_Name() {
bigbiffce8f83c2015-12-12 18:30:21 -05001914 if (Backup_Method == BM_NONE)
Dees_Troy38bd7602012-09-14 13:33:53 -04001915 return "none";
bigbiffce8f83c2015-12-12 18:30:21 -05001916 else if (Backup_Method == BM_FILES)
Dees_Troy38bd7602012-09-14 13:33:53 -04001917 return "files";
bigbiffce8f83c2015-12-12 18:30:21 -05001918 else if (Backup_Method == BM_DD)
Dees_Troy38bd7602012-09-14 13:33:53 -04001919 return "dd";
bigbiffce8f83c2015-12-12 18:30:21 -05001920 else if (Backup_Method == BM_FLASH_UTILS)
Dees_Troy38bd7602012-09-14 13:33:53 -04001921 return "flash_utils";
1922 else
1923 return "undefined";
1924 return "ERROR!";
Dees_Troy51a0e822012-09-05 15:24:24 -04001925}
1926
1927bool TWPartition::Decrypt(string Password) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001928 LOGINFO("STUB TWPartition::Decrypt, password: '%s'\n", Password.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001929 // Is this needed?
Dees_Troy51a0e822012-09-05 15:24:24 -04001930 return 1;
1931}
1932
1933bool TWPartition::Wipe_Encryption() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001934 bool Save_Data_Media = Has_Data_Media;
Ethan Yonker93382822018-11-01 15:25:31 -05001935 bool ret = false;
1936 BasePartition* base_partition = make_partition();
1937
1938 if (!base_partition->PreWipeEncryption())
1939 goto exit;
Dees_Troy38bd7602012-09-14 13:33:53 -04001940
dianlujitao4879b372018-12-03 18:45:47 +08001941 Find_Actual_Block_Device();
1942 if (!Is_Present) {
1943 LOGINFO("Block device not present, cannot format %s.\n", Display_Name.c_str());
1944 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
1945 return false;
1946 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001947 if (!UnMount(true))
Ethan Yonker93382822018-11-01 15:25:31 -05001948 goto exit;
Dees_Troy38bd7602012-09-14 13:33:53 -04001949
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001950#ifdef TW_INCLUDE_CRYPTO
Ethan Yonker1b190162016-12-05 15:25:19 -06001951 if (Is_Decrypted && !Decrypted_Block_Device.empty()) {
Matt Mower2b18a532015-02-20 16:58:05 -06001952 if (delete_crypto_blk_dev((char*)("userdata")) != 0) {
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001953 LOGERR("Error deleting crypto block device, continuing anyway.\n");
1954 }
1955 }
1956#endif
dianlujitao4879b372018-12-03 18:45:47 +08001957 Has_Data_Media = false;
Ethan Yonker93382822018-11-01 15:25:31 -05001958 Decrypted_Block_Device = "";
Dees_Troy74fb2e92013-04-15 14:35:47 +00001959 Is_Decrypted = false;
1960 Is_Encrypted = false;
Gary Pecke8bc5d72012-12-21 06:45:25 -08001961 if (Wipe(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001962 Has_Data_Media = Save_Data_Media;
1963 if (Has_Data_Media && !Symlink_Mount_Point.empty()) {
1964 Recreate_Media_Folder();
Ethan Yonker726a0202014-12-16 20:01:38 -06001965 if (Mount(false))
1966 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04001967 }
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001968 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
Ethan Yonker83e82572014-04-04 10:59:28 -05001969#ifndef TW_OEM_BUILD
Ethan Yonker74db1572015-10-28 12:44:49 -05001970 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 -05001971#endif
Ethan Yonker93382822018-11-01 15:25:31 -05001972 ret = true;
1973 if (!Key_Directory.empty())
1974 ret = PartitionManager.Wipe_By_Path(Key_Directory);
1975 if (ret)
1976 ret = base_partition->PostWipeEncryption();
1977 goto exit;
Dees_Troy38bd7602012-09-14 13:33:53 -04001978 } else {
1979 Has_Data_Media = Save_Data_Media;
Ethan Yonker74db1572015-10-28 12:44:49 -05001980 gui_err("format_data_err=Unable to format to remove encryption.");
Ethan Yonker726a0202014-12-16 20:01:38 -06001981 if (Has_Data_Media && Mount(false))
1982 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Ethan Yonker93382822018-11-01 15:25:31 -05001983 goto exit;
Dees_Troy38bd7602012-09-14 13:33:53 -04001984 }
Ethan Yonker93382822018-11-01 15:25:31 -05001985exit:
1986 delete base_partition;
1987 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04001988}
1989
1990void TWPartition::Check_FS_Type() {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001991 const char* type;
1992 blkid_probe pr;
Dees_Troy5bf43922012-09-07 16:07:55 -04001993
Dees_Troy68cab492012-12-12 19:29:35 +00001994 if (Fstab_File_System == "yaffs2" || Fstab_File_System == "mtd" || Fstab_File_System == "bml" || Ignore_Blkid)
1995 return; // Running blkid on some mtd devices causes a massive crash or needs to be skipped
Dees_Troy5bf43922012-09-07 16:07:55 -04001996
Dees_Troy38bd7602012-09-14 13:33:53 -04001997 Find_Actual_Block_Device();
Dees_Troy8170a922012-09-18 15:40:25 -04001998 if (!Is_Present)
1999 return;
Dees_Troy51127312012-09-08 13:08:49 -04002000
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05002001 pr = blkid_new_probe_from_filename(Actual_Block_Device.c_str());
2002 if (blkid_do_fullprobe(pr)) {
2003 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00002004 LOGINFO("Can't probe device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05002005 return;
Dees_Troy5bf43922012-09-07 16:07:55 -04002006 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02002007
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05002008 if (blkid_probe_lookup_value(pr, "TYPE", &type, NULL) < 0) {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05002009 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00002010 LOGINFO("can't find filesystem on device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05002011 return;
2012 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02002013
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05002014 Current_File_System = type;
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02002015 blkid_free_probe(pr);
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06002016 if (fs_flags.size() > 1) {
2017 std::vector<partition_fs_flags_struct>::iterator iter;
2018 std::vector<partition_fs_flags_struct>::iterator found = fs_flags.begin();
2019
2020 for (iter = fs_flags.begin(); iter != fs_flags.end(); iter++) {
2021 if (iter->File_System == Current_File_System) {
2022 found = iter;
2023 break;
2024 }
2025 }
2026 // If we don't find a match, we default the flags to the first set of flags that we received from the fstab
2027 if (Mount_Flags != found->Mount_Flags || Mount_Options != found->Mount_Options) {
2028 Mount_Flags = found->Mount_Flags;
2029 Mount_Options = found->Mount_Options;
2030 LOGINFO("Mount_Flags: %i, Mount_Options: %s\n", Mount_Flags, Mount_Options.c_str());
2031 }
2032 }
Dees_Troy51a0e822012-09-05 15:24:24 -04002033}
2034
dianlujitao4879b372018-12-03 18:45:47 +08002035bool TWPartition::Wipe_EXTFS(string File_System) {
2036#if PLATFORM_SDK_VERSION < 28
2037 if (!TWFunc::Path_Exists("/sbin/mke2fs"))
2038#else
2039 if (!TWFunc::Path_Exists("/sbin/mke2fs") || !TWFunc::Path_Exists("/sbin/e2fsdroid"))
2040#endif
Dees_Troy38bd7602012-09-14 13:33:53 -04002041 return Wipe_RMRF();
2042
dianlujitao4879b372018-12-03 18:45:47 +08002043 int ret;
2044 bool NeedPreserveFooter = true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002045
Ethan Yonker25f20c12014-10-14 09:04:43 -05002046 Find_Actual_Block_Device();
2047 if (!Is_Present) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002048 LOGINFO("Block device not present, cannot wipe %s.\n", Display_Name.c_str());
2049 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Ethan Yonker25f20c12014-10-14 09:04:43 -05002050 return false;
2051 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002052 if (!UnMount(true))
2053 return false;
2054
dianlujitao4879b372018-12-03 18:45:47 +08002055 /**
2056 * On decrypted devices, IOCTL_Get_Block_Size calculates size on device mapper,
2057 * so there's no need to preserve footer.
2058 */
2059 if ((Is_Decrypted && !Decrypted_Block_Device.empty()) ||
2060 Crypto_Key_Location != "footer") {
2061 NeedPreserveFooter = false;
2062 }
2063
2064 unsigned long long dev_sz = TWFunc::IOCTL_Get_Block_Size(Actual_Block_Device.c_str());
2065 if (!dev_sz)
2066 return false;
2067
2068 if (NeedPreserveFooter)
2069 Length < 0 ? dev_sz += Length : dev_sz -= CRYPT_FOOTER_OFFSET;
2070
Dees Troy9a4d7402019-03-21 14:17:28 -04002071 char dout[16];
2072 sprintf(dout, "%llu", dev_sz / 4096);
2073
2074 //string size_str =to_string(dev_sz / 4096);
2075 string size_str = dout;
dianlujitao4879b372018-12-03 18:45:47 +08002076 string Command;
2077
2078 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mke2fs"));
2079
2080 // Execute mke2fs to create empty ext4 filesystem
2081 Command = "mke2fs -t " + File_System + " -b 4096 " + Actual_Block_Device + " " + size_str;
2082 LOGINFO("mke2fs command: %s\n", Command.c_str());
2083 ret = TWFunc::Exec_Cmd(Command);
2084 if (ret) {
2085 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
2086 return false;
2087 }
2088
2089 if (TWFunc::Path_Exists("/sbin/e2fsdroid")) {
dianlujitao3cf05a52019-05-15 15:33:27 +08002090 const string& File_Contexts_Entry = (Mount_Point == "/system_root" ? "/" : Mount_Point);
2091 char *secontext = NULL;
2092 if (!selinux_handle || selabel_lookup(selinux_handle, &secontext, File_Contexts_Entry.c_str(), S_IFDIR) < 0) {
2093 LOGINFO("Cannot lookup security context for '%s'\n", Mount_Point.c_str());
2094 } else {
2095 // Execute e2fsdroid to initialize selinux context
2096 Command = "e2fsdroid -e -S /file_contexts -a " + File_Contexts_Entry + " " + Actual_Block_Device;
2097 LOGINFO("e2fsdroid command: %s\n", Command.c_str());
2098 ret = TWFunc::Exec_Cmd(Command);
2099 if (ret) {
2100 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
2101 return false;
2102 }
dianlujitao4879b372018-12-03 18:45:47 +08002103 }
2104 } else {
2105 LOGINFO("e2fsdroid not present\n");
2106 }
2107
2108 if (NeedPreserveFooter)
2109 Wipe_Crypto_Key();
2110 Current_File_System = File_System;
2111 Recreate_AndSec_Folder();
2112 gui_msg("done=Done.");
2113 return true;
2114}
2115
2116bool TWPartition::Wipe_EXT4() {
2117#ifdef USE_EXT4
Ethan Yonkerf27497f2014-02-09 11:48:33 -06002118 int ret;
dianlujitao4879b372018-12-03 18:45:47 +08002119 bool NeedPreserveFooter = true;
2120
2121 Find_Actual_Block_Device();
2122 if (!Is_Present) {
2123 LOGINFO("Block device not present, cannot wipe %s.\n", Display_Name.c_str());
2124 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
2125 return false;
2126 }
2127 if (!UnMount(true))
2128 return false;
2129
2130 /**
2131 * On decrypted devices, IOCTL_Get_Block_Size calculates size on device mapper,
2132 * so there's no need to preserve footer.
2133 */
2134 if ((Is_Decrypted && !Decrypted_Block_Device.empty()) ||
2135 Crypto_Key_Location != "footer") {
2136 NeedPreserveFooter = false;
2137 }
2138
2139 unsigned long long dev_sz = TWFunc::IOCTL_Get_Block_Size(Actual_Block_Device.c_str());
2140 if (!dev_sz)
2141 return false;
2142
2143 if (NeedPreserveFooter)
2144 Length < 0 ? dev_sz += Length : dev_sz -= CRYPT_FOOTER_OFFSET;
2145
Ethan Yonkerf27497f2014-02-09 11:48:33 -06002146 char *secontext = NULL;
2147
Greg Wallace4b44fef2015-12-29 15:33:24 -05002148 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("make_ext4fs"));
Ethan Yonkerf27497f2014-02-09 11:48:33 -06002149
Dees Troy99c8dbf2014-03-10 16:53:58 +00002150 if (!selinux_handle || selabel_lookup(selinux_handle, &secontext, Mount_Point.c_str(), S_IFDIR) < 0) {
Ethan Yonkerf27497f2014-02-09 11:48:33 -06002151 LOGINFO("Cannot lookup security context for '%s'\n", Mount_Point.c_str());
dianlujitao4879b372018-12-03 18:45:47 +08002152 ret = make_ext4fs(Actual_Block_Device.c_str(), dev_sz, Mount_Point.c_str(), NULL);
Ethan Yonkerf27497f2014-02-09 11:48:33 -06002153 } else {
dianlujitao4879b372018-12-03 18:45:47 +08002154 ret = make_ext4fs(Actual_Block_Device.c_str(), dev_sz, Mount_Point.c_str(), selinux_handle);
Ethan Yonkerf27497f2014-02-09 11:48:33 -06002155 }
2156 if (ret != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002157 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troya95f55c2013-08-17 13:14:43 +00002158 return false;
2159 } else {
dianlujitao4879b372018-12-03 18:45:47 +08002160 if (NeedPreserveFooter)
2161 Wipe_Crypto_Key();
bigbiff bigbiffc49d7062013-10-11 20:28:00 -04002162 string sedir = Mount_Point + "/lost+found";
2163 PartitionManager.Mount_By_Path(sedir.c_str(), true);
2164 rmdir(sedir.c_str());
2165 mkdir(sedir.c_str(), S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP);
Dees_Troya95f55c2013-08-17 13:14:43 +00002166 return true;
2167 }
2168#else
dianlujitao4879b372018-12-03 18:45:47 +08002169 return Wipe_EXTFS("ext4");
Dees_Troya95f55c2013-08-17 13:14:43 +00002170#endif
Dees_Troy51a0e822012-09-05 15:24:24 -04002171}
2172
2173bool TWPartition::Wipe_FAT() {
Vojtech Bocek05534202013-09-11 08:11:56 +02002174 string command;
Dees_Troy38bd7602012-09-14 13:33:53 -04002175
Matt Mower18794c82015-11-11 16:22:45 -06002176 if (TWFunc::Path_Exists("/sbin/mkfs.fat")) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002177 if (!UnMount(true))
2178 return false;
2179
Greg Wallace4b44fef2015-12-29 15:33:24 -05002180 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mkfs.fat"));
Dees_Troy38bd7602012-09-14 13:33:53 -04002181 Find_Actual_Block_Device();
Matt Mower18794c82015-11-11 16:22:45 -06002182 command = "mkfs.fat " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02002183 if (TWFunc::Exec_Cmd(command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08002184 Current_File_System = "vfat";
Dees_Troye58d5262012-09-21 12:27:57 -04002185 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05002186 gui_msg("done=Done.");
Dees_Troy38bd7602012-09-14 13:33:53 -04002187 return true;
2188 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002189 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04002190 return false;
2191 }
2192 return true;
2193 }
2194 else
2195 return Wipe_RMRF();
2196
2197 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04002198}
2199
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002200bool TWPartition::Wipe_EXFAT() {
Vojtech Bocek05534202013-09-11 08:11:56 +02002201 string command;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002202
2203 if (TWFunc::Path_Exists("/sbin/mkexfatfs")) {
2204 if (!UnMount(true))
2205 return false;
2206
Greg Wallace4b44fef2015-12-29 15:33:24 -05002207 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mkexfatfs"));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002208 Find_Actual_Block_Device();
2209 command = "mkexfatfs " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02002210 if (TWFunc::Exec_Cmd(command) == 0) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002211 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05002212 gui_msg("done=Done.");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002213 return true;
2214 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002215 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002216 return false;
2217 }
2218 return true;
2219 }
2220 return false;
2221}
2222
Dees_Troy38bd7602012-09-14 13:33:53 -04002223bool TWPartition::Wipe_MTD() {
2224 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)("MTD"));
Dees_Troy38bd7602012-09-14 13:33:53 -04002228
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02002229 mtd_scan_partitions();
2230 const MtdPartition* mtd = mtd_find_partition_by_name(MTD_Name.c_str());
2231 if (mtd == NULL) {
2232 LOGERR("No mtd partition named '%s'", MTD_Name.c_str());
2233 return false;
2234 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002235
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02002236 MtdWriteContext* ctx = mtd_write_partition(mtd);
2237 if (ctx == NULL) {
2238 LOGERR("Can't write '%s', failed to format.", MTD_Name.c_str());
2239 return false;
2240 }
2241 if (mtd_erase_blocks(ctx, -1) == -1) {
2242 mtd_write_close(ctx);
2243 LOGERR("Failed to format '%s'", MTD_Name.c_str());
2244 return false;
2245 }
2246 if (mtd_write_close(ctx) != 0) {
2247 LOGERR("Failed to close '%s'", MTD_Name.c_str());
2248 return false;
2249 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08002250 Current_File_System = "yaffs2";
Dees_Troye58d5262012-09-21 12:27:57 -04002251 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05002252 gui_msg("done=Done.");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02002253 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002254}
2255
2256bool TWPartition::Wipe_RMRF() {
Dees_Troy38bd7602012-09-14 13:33:53 -04002257 if (!Mount(true))
2258 return false;
Ethan Yonker726a0202014-12-16 20:01:38 -06002259 // This is the only wipe that leaves the partition mounted, so we
2260 // must manually remove the partition from MTP if it is a storage
2261 // partition.
2262 if (Is_Storage)
2263 PartitionManager.Remove_MTP_Storage(MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04002264
Ethan Yonker74db1572015-10-28 12:44:49 -05002265 gui_msg(Msg("remove_all=Removing all files under '{1}'")(Mount_Point));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002266 TWFunc::removeDir(Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04002267 Recreate_AndSec_Folder();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002268 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002269}
2270
Dees_Troye5017042013-08-29 16:38:55 +00002271bool TWPartition::Wipe_F2FS() {
Vojtech Bocek05534202013-09-11 08:11:56 +02002272 string command;
Dees_Troye5017042013-08-29 16:38:55 +00002273
2274 if (TWFunc::Path_Exists("/sbin/mkfs.f2fs")) {
dianlujitao4879b372018-12-03 18:45:47 +08002275 bool NeedPreserveFooter = true;
2276
2277 Find_Actual_Block_Device();
2278 if (!Is_Present) {
2279 LOGINFO("Block device not present, cannot wipe %s.\n", Display_Name.c_str());
2280 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
2281 return false;
2282 }
Dees_Troye5017042013-08-29 16:38:55 +00002283 if (!UnMount(true))
2284 return false;
2285
dianlujitao4879b372018-12-03 18:45:47 +08002286 /**
2287 * On decrypted devices, IOCTL_Get_Block_Size calculates size on device mapper,
2288 * so there's no need to preserve footer.
2289 */
2290 if ((Is_Decrypted && !Decrypted_Block_Device.empty()) ||
2291 Crypto_Key_Location != "footer") {
2292 NeedPreserveFooter = false;
dhacker29a3fa75f2015-01-17 19:42:33 -05002293 }
dianlujitao4879b372018-12-03 18:45:47 +08002294
dianlujitao4879b372018-12-03 18:45:47 +08002295 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mkfs.f2fs"));
Ethan Yonker7e941582019-03-22 08:18:21 -05002296 // First determine if we have the old mkfs.f2fs that uses "-r reserved_bytes"
2297 // or the new mkfs.f2fs that expects the number of sectors as the optional last argument
2298 // Note: some 7.1 trees have the old and some have the new.
2299 command = "mkfs.f2fs | grep \"reserved\" > /tmp/f2fsversiontest";
2300 TWFunc::Exec_Cmd(command, false); // no help argument so printing usage exits with an error code
2301 if (!TWFunc::Path_Exists("/tmp/f2fsversiontest")) {
2302 LOGINFO("Error determining mkfs.f2fs version\n");
2303 return false;
2304 }
2305 if (TWFunc::Get_File_Size("/tmp/f2fsversiontest") <= 0) {
2306 LOGINFO("Using newer mkfs.f2fs\n");
2307 unsigned long long dev_sz = TWFunc::IOCTL_Get_Block_Size(Actual_Block_Device.c_str());
2308 if (!dev_sz)
2309 return false;
2310
2311 if (NeedPreserveFooter)
2312 Length < 0 ? dev_sz += Length : dev_sz -= CRYPT_FOOTER_OFFSET;
2313
2314 char dev_sz_str[48];
2315 sprintf(dev_sz_str, "%llu", (dev_sz / 4096));
2316 command = "mkfs.f2fs -d1 -f -O encrypt -O quota -O verity -w 4096 " + Actual_Block_Device + " " + dev_sz_str;
2317 if (TWFunc::Path_Exists("/sbin/sload.f2fs")) {
2318 command += " && sload.f2fs -t /data " + Actual_Block_Device;
2319 }
2320 } else {
2321 LOGINFO("Using older mkfs.f2fs\n");
2322 command = "mkfs.f2fs -t 0";
2323 if (NeedPreserveFooter) {
2324 // Only use length if we're not decrypted
2325 char len[32];
2326 int mod_length = Length;
2327 if (Length < 0)
2328 mod_length *= -1;
2329 sprintf(len, "%i", mod_length);
2330 command += " -r ";
2331 command += len;
2332 }
2333 command += " " + Actual_Block_Device;
dianlujitao4879b372018-12-03 18:45:47 +08002334 }
2335 LOGINFO("mkfs.f2fs command: %s\n", command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02002336 if (TWFunc::Exec_Cmd(command) == 0) {
dianlujitao4879b372018-12-03 18:45:47 +08002337 if (NeedPreserveFooter)
2338 Wipe_Crypto_Key();
Dees_Troye5017042013-08-29 16:38:55 +00002339 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05002340 gui_msg("done=Done.");
Dees_Troye5017042013-08-29 16:38:55 +00002341 return true;
2342 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002343 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troye5017042013-08-29 16:38:55 +00002344 return false;
2345 }
2346 return true;
2347 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002348 LOGINFO("mkfs.f2fs binary not found, using rm -rf to wipe.\n");
Dees_Troye5017042013-08-29 16:38:55 +00002349 return Wipe_RMRF();
2350 }
2351 return false;
2352}
2353
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002354bool TWPartition::Wipe_NTFS() {
2355 string command;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002356 string Ntfsmake_Binary;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002357
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002358 if (TWFunc::Path_Exists("/sbin/mkntfs"))
2359 Ntfsmake_Binary = "mkntfs";
2360 else if (TWFunc::Path_Exists("/sbin/mkfs.ntfs"))
2361 Ntfsmake_Binary = "mkfs.ntfs";
2362 else
2363 return false;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002364
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002365 if (!UnMount(true))
2366 return false;
2367
Greg Wallace4b44fef2015-12-29 15:33:24 -05002368 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)(Ntfsmake_Binary));
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002369 Find_Actual_Block_Device();
2370 command = "/sbin/" + Ntfsmake_Binary + " " + Actual_Block_Device;
2371 if (TWFunc::Exec_Cmd(command) == 0) {
2372 Recreate_AndSec_Folder();
2373 gui_msg("done=Done.");
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002374 return true;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002375 } else {
2376 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
2377 return false;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002378 }
2379 return false;
2380}
2381
Dees_Troy51a0e822012-09-05 15:24:24 -04002382bool TWPartition::Wipe_Data_Without_Wiping_Media() {
Ethan Yonker83e82572014-04-04 10:59:28 -05002383#ifdef TW_OEM_BUILD
2384 // In an OEM Build we want to do a full format
2385 return Wipe_Encryption();
2386#else
Ethan Yonker66a19492015-12-10 10:19:45 -06002387 bool ret = false;
Dees_Troy38bd7602012-09-14 13:33:53 -04002388
Dees_Troy38bd7602012-09-14 13:33:53 -04002389 if (!Mount(true))
2390 return false;
2391
Ethan Yonker74db1572015-10-28 12:44:49 -05002392 gui_msg("wiping_data=Wiping data without wiping /data/media ...");
Ethan Yonker66a19492015-12-10 10:19:45 -06002393 ret = Wipe_Data_Without_Wiping_Media_Func(Mount_Point + "/");
2394 if (ret)
2395 gui_msg("done=Done.");
2396 return ret;
2397#endif // ifdef TW_OEM_BUILD
2398}
2399
2400bool TWPartition::Wipe_Data_Without_Wiping_Media_Func(const string& parent __unused) {
2401 string dir;
Dees_Troy38bd7602012-09-14 13:33:53 -04002402
2403 DIR* d;
Ethan Yonker66a19492015-12-10 10:19:45 -06002404 d = opendir(parent.c_str());
Dees_Troy16b74352012-11-14 22:27:31 +00002405 if (d != NULL) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002406 struct dirent* de;
2407 while ((de = readdir(d)) != NULL) {
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002408 if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) continue;
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05002409
Ethan Yonker66a19492015-12-10 10:19:45 -06002410 dir = parent;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002411 dir.append(de->d_name);
Ethan Yonker3fdcda42016-11-30 12:29:37 -06002412 if (wipe_exclusions.check_skip_dirs(dir)) {
Ethan Yonker66a19492015-12-10 10:19:45 -06002413 LOGINFO("skipped '%s'\n", dir.c_str());
2414 continue;
2415 }
Dees_Troyce675462013-01-09 19:48:21 +00002416 if (de->d_type == DT_DIR) {
Ethan Yonker66a19492015-12-10 10:19:45 -06002417 dir.append("/");
2418 if (!Wipe_Data_Without_Wiping_Media_Func(dir)) {
2419 closedir(d);
2420 return false;
2421 }
2422 rmdir(dir.c_str());
bigbiff bigbiff98f1f902013-01-19 18:46:13 -05002423 } 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 +03002424 if (unlink(dir.c_str()) != 0)
Ethan Yonker79f88bd2016-12-09 14:52:12 -06002425 LOGINFO("Unable to unlink '%s': %s\n", dir.c_str(), strerror(errno));
Dees_Troyce675462013-01-09 19:48:21 +00002426 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002427 }
2428 closedir(d);
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05002429
Dees_Troy16b74352012-11-14 22:27:31 +00002430 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -04002431 }
Ethan Yonker74db1572015-10-28 12:44:49 -05002432 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy16b74352012-11-14 22:27:31 +00002433 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04002434}
2435
dianlujitao4879b372018-12-03 18:45:47 +08002436void TWPartition::Wipe_Crypto_Key() {
2437 Find_Actual_Block_Device();
2438 if (Crypto_Key_Location.empty())
2439 return;
2440 else if (Crypto_Key_Location == "footer") {
2441 int fd = open(Actual_Block_Device.c_str(), O_RDWR);
2442 if (fd < 0) {
2443 gui_print_color("warning", "Unable to open '%s' to wipe crypto key\n", Actual_Block_Device.c_str());
2444 return;
2445 }
2446
2447 unsigned int block_count;
2448 if ((ioctl(fd, BLKGETSIZE, &block_count)) == -1) {
2449 gui_print_color("warning", "Unable to get block size for wiping crypto footer.\n");
2450 } else {
2451 int newlen = Length < 0 ? -Length : CRYPT_FOOTER_OFFSET;
2452 off64_t offset = ((off64_t)block_count * 512) - newlen;
2453 if (lseek64(fd, offset, SEEK_SET) == -1) {
2454 gui_print_color("warning", "Unable to lseek64 for wiping crypto footer.\n");
2455 } else {
2456 void* buffer = malloc(newlen);
2457 if (!buffer) {
2458 gui_print_color("warning", "Failed to malloc for wiping crypto footer.\n");
2459 } else {
2460 memset(buffer, 0, newlen);
2461 int ret = write(fd, buffer, newlen);
2462 if (ret != newlen) {
2463 gui_print_color("warning", "Failed to wipe crypto footer.\n");
2464 } else {
2465 LOGINFO("Successfully wiped crypto footer.\n");
2466 }
2467 free(buffer);
2468 }
2469 }
2470 }
2471 close(fd);
2472 } else {
2473 if (TWFunc::IOCTL_Get_Block_Size(Crypto_Key_Location.c_str()) >= 16384LLU) {
2474 string Command = "dd of='" + Crypto_Key_Location + "' if=/dev/zero bs=16384 count=1";
2475 TWFunc::Exec_Cmd(Command);
2476 } else {
2477 LOGINFO("Crypto key location reports size < 16K so not wiping crypto footer.\n");
2478 }
2479 }
2480}
2481
bigbiffce8f83c2015-12-12 18:30:21 -05002482bool TWPartition::Backup_Tar(PartitionSettings *part_settings, pid_t *tar_fork_pid) {
Ethan Yonker472f5062016-02-25 13:47:30 -06002483 string Full_FileName;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002484 twrpTar tar;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05002485
Dees_Troy43d8b002012-09-17 16:00:01 -04002486 if (!Mount(true))
2487 return false;
2488
Ian Macdonalda3b25a32019-06-16 22:39:14 +02002489 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Backup_Display_Name, gui_parse_text("{@backing}"));
Ethan Yonker74db1572015-10-28 12:44:49 -05002490 gui_msg(Msg("backing_up=Backing up {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002491
Ethan Yonker472f5062016-02-25 13:47:30 -06002492 DataManager::GetValue(TW_USE_COMPRESSION_VAR, tar.use_compression);
Matt Mowerbb81e5d2014-03-20 18:05:41 -05002493
Dees_Troy83bd4832013-05-04 12:39:56 +00002494#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
Ethan Yonker472f5062016-02-25 13:47:30 -06002495 if (Can_Encrypt_Backup) {
2496 DataManager::GetValue("tw_encrypt_backup", tar.use_encryption);
2497 if (tar.use_encryption) {
2498 if (Use_Userdata_Encryption)
2499 tar.userdata_encryption = tar.use_encryption;
2500 string Password;
2501 DataManager::GetValue("tw_backup_password", Password);
2502 tar.setpassword(Password);
2503 } else {
2504 tar.use_encryption = 0;
2505 }
Dees_Troy83bd4832013-05-04 12:39:56 +00002506 }
2507#endif
Dees_Troy43d8b002012-09-17 16:00:01 -04002508
Ethan Yonker472f5062016-02-25 13:47:30 -06002509 Backup_FileName = Backup_Name + "." + Current_File_System + ".win";
Ethan Yonkere080c1f2016-09-19 13:50:25 -05002510 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
Ethan Yonker01f4e032017-02-03 15:30:52 -06002511 if (Has_Data_Media)
2512 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 -05002513 tar.part_settings = part_settings;
Ethan Yonker3fdcda42016-11-30 12:29:37 -06002514 tar.backup_exclusions = &backup_exclusions;
Dees Troye0a433a2013-12-02 04:10:37 +00002515 tar.setdir(Backup_Path);
2516 tar.setfn(Full_FileName);
2517 tar.setsize(Backup_Size);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002518 tar.partition_name = Backup_Name;
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002519 tar.backup_folder = part_settings->Backup_Folder;
bigbiffce8f83c2015-12-12 18:30:21 -05002520 if (tar.createTarFork(tar_fork_pid) != 0)
Dees Troye0a433a2013-12-02 04:10:37 +00002521 return false;
Dees_Troy43d8b002012-09-17 16:00:01 -04002522 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002523}
2524
bigbiffce8f83c2015-12-12 18:30:21 -05002525bool TWPartition::Backup_Image(PartitionSettings *part_settings) {
2526 string Full_FileName, adb_file_name;
igoriok87e3d932013-01-31 21:03:53 +02002527
Ethan Yonker74db1572015-10-28 12:44:49 -05002528 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, gui_parse_text("{@backing}"));
2529 gui_msg(Msg("backing_up=Backing up {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002530
Ethan Yonker472f5062016-02-25 13:47:30 -06002531 Backup_FileName = Backup_Name + "." + Current_File_System + ".win";
Dees_Troy43d8b002012-09-17 16:00:01 -04002532
bigbiffce8f83c2015-12-12 18:30:21 -05002533 if (part_settings->adbbackup) {
2534 Full_FileName = TW_ADB_BACKUP;
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002535 adb_file_name = part_settings->Backup_Folder + "/" + Backup_FileName;
bigbiffce8f83c2015-12-12 18:30:21 -05002536 }
2537 else
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002538 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
bigbiffce8f83c2015-12-12 18:30:21 -05002539
2540 part_settings->total_restore_size = Backup_Size;
2541
2542 if (part_settings->adbbackup) {
2543 if (!twadbbu::Write_TWIMG(adb_file_name, Backup_Size))
2544 return false;
2545 }
2546
2547 if (!Raw_Read_Write(part_settings))
Ethan Yonker472f5062016-02-25 13:47:30 -06002548 return false;
2549
bigbiffce8f83c2015-12-12 18:30:21 -05002550 if (part_settings->adbbackup) {
2551 if (!twadbbu::Write_TWEOF())
2552 return false;
Dees_Troy7c2dec82012-09-26 09:49:14 -04002553 }
Dees_Troy43d8b002012-09-17 16:00:01 -04002554 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002555}
2556
bigbiffce8f83c2015-12-12 18:30:21 -05002557bool TWPartition::Raw_Read_Write(PartitionSettings *part_settings) {
2558 unsigned long long RW_Block_Size, Remain = Backup_Size;
2559 int src_fd = -1, dest_fd = -1;
2560 ssize_t bs;
Ethan Yonker472f5062016-02-25 13:47:30 -06002561 bool ret = false;
2562 void* buffer = NULL;
2563 unsigned long long backedup_size = 0;
bigbiffce8f83c2015-12-12 18:30:21 -05002564 string srcfn, destfn;
Ethan Yonker472f5062016-02-25 13:47:30 -06002565
bigbiffce8f83c2015-12-12 18:30:21 -05002566 if (part_settings->PM_Method == PM_BACKUP) {
2567 srcfn = Actual_Block_Device;
2568 if (part_settings->adbbackup)
2569 destfn = TW_ADB_BACKUP;
bigbiff bigbiff38b83c12017-12-28 19:58:52 -05002570 else {
Ethan Yonkere080c1f2016-09-19 13:50:25 -05002571 destfn = part_settings->Backup_Folder + "/" + Backup_FileName;
bigbiff bigbiff38b83c12017-12-28 19:58:52 -05002572 }
bigbiffce8f83c2015-12-12 18:30:21 -05002573 }
2574 else {
2575 destfn = Actual_Block_Device;
2576 if (part_settings->adbbackup) {
2577 srcfn = TW_ADB_RESTORE;
2578 } else {
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002579 srcfn = part_settings->Backup_Folder + "/" + Backup_FileName;
bigbiffce8f83c2015-12-12 18:30:21 -05002580 Remain = TWFunc::Get_File_Size(srcfn);
2581 }
2582 }
Ethan Yonker472f5062016-02-25 13:47:30 -06002583
bigbiffce8f83c2015-12-12 18:30:21 -05002584 src_fd = open(srcfn.c_str(), O_RDONLY | O_LARGEFILE);
Ethan Yonker472f5062016-02-25 13:47:30 -06002585 if (src_fd < 0) {
bigbiffce8f83c2015-12-12 18:30:21 -05002586 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(srcfn.c_str())(strerror(errno)));
Ethan Yonker472f5062016-02-25 13:47:30 -06002587 return false;
2588 }
bigbiffce8f83c2015-12-12 18:30:21 -05002589
2590 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 -06002591 if (dest_fd < 0) {
bigbiffce8f83c2015-12-12 18:30:21 -05002592 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(destfn.c_str())(strerror(errno)));
Ethan Yonker472f5062016-02-25 13:47:30 -06002593 goto exit;
2594 }
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -04002595
bigbiffce8f83c2015-12-12 18:30:21 -05002596 LOGINFO("Reading '%s', writing '%s'\n", srcfn.c_str(), destfn.c_str());
2597
2598 if (part_settings->adbbackup) {
2599 RW_Block_Size = MAX_ADB_READ;
2600 bs = MAX_ADB_READ;
2601 }
2602 else {
2603 RW_Block_Size = 1048576LLU; // 1MB
2604 bs = (ssize_t)(RW_Block_Size);
2605 }
2606
Ethan Yonker472f5062016-02-25 13:47:30 -06002607 buffer = malloc((size_t)bs);
2608 if (!buffer) {
2609 LOGINFO("Raw_Read_Write failed to malloc\n");
2610 goto exit;
2611 }
bigbiffce8f83c2015-12-12 18:30:21 -05002612
2613 if (part_settings->progress)
2614 part_settings->progress->SetPartitionSize(part_settings->total_restore_size);
2615
Ethan Yonker472f5062016-02-25 13:47:30 -06002616 while (Remain > 0) {
2617 if (Remain < RW_Block_Size)
bigbiffce8f83c2015-12-12 18:30:21 -05002618 bs = (ssize_t)(Remain);
Matt Mowera8a89d12016-12-30 18:10:37 -06002619 if (read(src_fd, buffer, bs) != bs) {
Ethan Yonker472f5062016-02-25 13:47:30 -06002620 LOGINFO("Error reading source fd (%s)\n", strerror(errno));
2621 goto exit;
2622 }
2623 if (write(dest_fd, buffer, bs) != bs) {
2624 LOGINFO("Error writing destination fd (%s)\n", strerror(errno));
2625 goto exit;
2626 }
2627 backedup_size += (unsigned long long)(bs);
Matt Mower029a82d2017-01-08 13:12:38 -06002628 Remain -= (unsigned long long)(bs);
bigbiffce8f83c2015-12-12 18:30:21 -05002629 if (part_settings->progress)
2630 part_settings->progress->UpdateSize(backedup_size);
Ethan Yonker472f5062016-02-25 13:47:30 -06002631 if (PartitionManager.Check_Backup_Cancel() != 0)
2632 goto exit;
2633 }
bigbiffce8f83c2015-12-12 18:30:21 -05002634 if (part_settings->progress)
2635 part_settings->progress->UpdateDisplayDetails(true);
Ethan Yonker472f5062016-02-25 13:47:30 -06002636 fsync(dest_fd);
nailyka083dc62016-11-11 21:41:28 +01002637
2638 if (!part_settings->adbbackup && part_settings->PM_Method == PM_BACKUP) {
2639 tw_set_default_metadata(destfn.c_str());
2640 LOGINFO("Restored default metadata for %s\n", destfn.c_str());
2641 }
2642
Ethan Yonker472f5062016-02-25 13:47:30 -06002643 ret = true;
2644exit:
2645 if (src_fd >= 0)
2646 close(src_fd);
2647 if (dest_fd >= 0)
2648 close(dest_fd);
2649 if (buffer)
2650 free(buffer);
2651 return ret;
2652}
2653
bigbiffce8f83c2015-12-12 18:30:21 -05002654bool TWPartition::Backup_Dump_Image(PartitionSettings *part_settings) {
Vojtech Bocek05534202013-09-11 08:11:56 +02002655 string Full_FileName, Command;
Dees_Troy43d8b002012-09-17 16:00:01 -04002656
Ethan Yonker74db1572015-10-28 12:44:49 -05002657 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, gui_parse_text("{@backing}"));
2658 gui_msg(Msg("backing_up=Backing up {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002659
bigbiffce8f83c2015-12-12 18:30:21 -05002660 if (part_settings->progress)
2661 part_settings->progress->SetPartitionSize(Backup_Size);
Dees_Troy43d8b002012-09-17 16:00:01 -04002662
Ethan Yonker472f5062016-02-25 13:47:30 -06002663 Backup_FileName = Backup_Name + "." + Current_File_System + ".win";
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002664 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04002665
2666 Command = "dump_image " + MTD_Name + " '" + Full_FileName + "'";
bigbiffce8f83c2015-12-12 18:30:21 -05002667
Dees_Troy2673cec2013-04-02 20:22:16 +00002668 LOGINFO("Backup command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02002669 TWFunc::Exec_Cmd(Command);
Ethan Yonker4b94cfd2014-12-11 10:00:45 -06002670 tw_set_default_metadata(Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04002671 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
2672 // 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 -06002673 gui_msg(Msg(msg::kError, "backup_size=Backup file size for '{1}' is 0 bytes.")(Full_FileName));
Dees_Troy7c2dec82012-09-26 09:49:14 -04002674 return false;
2675 }
bigbiffce8f83c2015-12-12 18:30:21 -05002676 if (part_settings->progress)
2677 part_settings->progress->UpdateSize(Backup_Size);
2678
Dees_Troy43d8b002012-09-17 16:00:01 -04002679 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002680}
2681
bigbiffce8f83c2015-12-12 18:30:21 -05002682unsigned long long TWPartition::Get_Restore_Size(PartitionSettings *part_settings) {
2683 if (!part_settings->adbbackup) {
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002684 InfoManager restore_info(part_settings->Backup_Folder + "/" + Backup_Name + ".info");
bigbiffce8f83c2015-12-12 18:30:21 -05002685 if (restore_info.LoadValues() == 0) {
2686 if (restore_info.GetValue("backup_size", Restore_Size) == 0) {
2687 LOGINFO("Read info file, restore size is %llu\n", Restore_Size);
2688 return Restore_Size;
2689 }
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002690 }
2691 }
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002692
Matt Mower029a82d2017-01-08 13:12:38 -06002693 string Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
2694 string Restore_File_System = Get_Restore_File_System(part_settings);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002695
2696 if (Is_Image(Restore_File_System)) {
2697 Restore_Size = TWFunc::Get_File_Size(Full_FileName);
2698 return Restore_Size;
2699 }
2700
2701 twrpTar tar;
2702 tar.setdir(Backup_Path);
2703 tar.setfn(Full_FileName);
bigbiffce8f83c2015-12-12 18:30:21 -05002704 tar.backup_name = Full_FileName;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002705#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
2706 string Password;
2707 DataManager::GetValue("tw_restore_password", Password);
2708 if (!Password.empty())
2709 tar.setpassword(Password);
2710#endif
2711 tar.partition_name = Backup_Name;
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002712 tar.backup_folder = part_settings->Backup_Folder;
bigbiffce8f83c2015-12-12 18:30:21 -05002713 tar.part_settings = part_settings;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002714 Restore_Size = tar.get_size();
2715 return Restore_Size;
2716}
2717
bigbiffce8f83c2015-12-12 18:30:21 -05002718bool TWPartition::Restore_Tar(PartitionSettings *part_settings) {
Ethan Yonker472f5062016-02-25 13:47:30 -06002719 string Full_FileName;
Dees Troy4159aed2014-02-28 17:24:43 +00002720 bool ret = false;
bigbiffce8f83c2015-12-12 18:30:21 -05002721 string Restore_File_System = Get_Restore_File_System(part_settings);
Dees_Troy43d8b002012-09-17 16:00:01 -04002722
Dees_Troye58d5262012-09-21 12:27:57 -04002723 if (Has_Android_Secure) {
Dees_Troye58d5262012-09-21 12:27:57 -04002724 if (!Wipe_AndSec())
2725 return false;
Gary Peck43acadf2012-11-21 21:19:01 -08002726 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002727 gui_msg(Msg("wiping=Wiping {1}")(Backup_Display_Name));
Ethan Yonker5eac2222014-06-11 12:22:55 -05002728 if (Has_Data_Media && Mount_Point == "/data" && Restore_File_System != Current_File_System) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002729 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 -05002730 if (!Wipe_Data_Without_Wiping_Media())
2731 return false;
2732 } else {
2733 if (!Wipe(Restore_File_System))
2734 return false;
2735 }
Dees_Troye58d5262012-09-21 12:27:57 -04002736 }
Matt Mower3c366972015-12-25 19:28:31 -06002737 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Backup_Display_Name, gui_parse_text("{@restoring_hdr}"));
Ethan Yonker74db1572015-10-28 12:44:49 -05002738 gui_msg(Msg("restoring=Restoring {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002739
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05002740 // Remount as read/write as needed so we can restore the backup
2741 if (!ReMount_RW(true))
Dees_Troy43d8b002012-09-17 16:00:01 -04002742 return false;
2743
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002744 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
Ethan Yonker87af5632014-02-10 11:56:35 -06002745 twrpTar tar;
bigbiffce8f83c2015-12-12 18:30:21 -05002746 tar.part_settings = part_settings;
Ethan Yonker87af5632014-02-10 11:56:35 -06002747 tar.setdir(Backup_Path);
2748 tar.setfn(Full_FileName);
2749 tar.backup_name = Backup_Name;
2750#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
2751 string Password;
2752 DataManager::GetValue("tw_restore_password", Password);
2753 if (!Password.empty())
2754 tar.setpassword(Password);
2755#endif
bigbiffce8f83c2015-12-12 18:30:21 -05002756 part_settings->progress->SetPartitionSize(Get_Restore_Size(part_settings));
2757 if (tar.extractTarFork() != 0)
Dees Troy4159aed2014-02-28 17:24:43 +00002758 ret = false;
2759 else
2760 ret = true;
2761#ifdef HAVE_CAPABILITIES
2762 // Restore capabilities to the run-as binary
Captain Throwback9d6feb52018-07-27 10:05:24 -04002763 if (Mount_Point == PartitionManager.Get_Android_Root_Path() && Mount(true) && TWFunc::Path_Exists("/system/bin/run-as")) {
Dees Troy4159aed2014-02-28 17:24:43 +00002764 struct vfs_cap_data cap_data;
2765 uint64_t capabilities = (1 << CAP_SETUID) | (1 << CAP_SETGID);
2766
2767 memset(&cap_data, 0, sizeof(cap_data));
2768 cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE;
2769 cap_data.data[0].permitted = (uint32_t) (capabilities & 0xffffffff);
2770 cap_data.data[0].inheritable = 0;
2771 cap_data.data[1].permitted = (uint32_t) (capabilities >> 32);
2772 cap_data.data[1].inheritable = 0;
2773 if (setxattr("/system/bin/run-as", XATTR_NAME_CAPS, &cap_data, sizeof(cap_data), 0) < 0) {
2774 LOGINFO("Failed to reset capabilities of /system/bin/run-as binary.\n");
2775 } else {
2776 LOGINFO("Reset capabilities of /system/bin/run-as binary successful.\n");
2777 }
2778 }
2779#endif
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05002780 if (Mount_Read_Only || Mount_Flags & MS_RDONLY)
2781 // Remount as read only when restoration is complete
2782 ReMount(true);
2783
Dees Troy4159aed2014-02-28 17:24:43 +00002784 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04002785}
2786
bigbiffce8f83c2015-12-12 18:30:21 -05002787bool TWPartition::Restore_Image(PartitionSettings *part_settings) {
Ethan Yonker96af84a2015-01-05 14:58:36 -06002788 string Full_FileName;
bigbiffce8f83c2015-12-12 18:30:21 -05002789 string Restore_File_System = Get_Restore_File_System(part_settings);
Dees_Troy43d8b002012-09-17 16:00:01 -04002790
Matt Mower3c366972015-12-25 19:28:31 -06002791 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Backup_Display_Name, gui_parse_text("{@restoring_hdr}"));
Ethan Yonker74db1572015-10-28 12:44:49 -05002792 gui_msg(Msg("restoring=Restoring {1}...")(Backup_Display_Name));
bigbiffce8f83c2015-12-12 18:30:21 -05002793
2794 if (part_settings->adbbackup)
2795 Full_FileName = TW_ADB_RESTORE;
2796 else
Ethan Yonkere080c1f2016-09-19 13:50:25 -05002797 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
Gary Peck15e623d2012-11-21 21:07:58 -08002798
Ethan Yonker96af84a2015-01-05 14:58:36 -06002799 if (Restore_File_System == "emmc") {
bigbiffce8f83c2015-12-12 18:30:21 -05002800 if (!part_settings->adbbackup)
2801 part_settings->total_restore_size = (uint64_t)(TWFunc::Get_File_Size(Full_FileName));
2802 if (!Raw_Read_Write(part_settings))
Ethan Yonker96af84a2015-01-05 14:58:36 -06002803 return false;
2804 } else if (Restore_File_System == "mtd" || Restore_File_System == "bml") {
bigbiffce8f83c2015-12-12 18:30:21 -05002805 if (!Flash_Image_FI(Full_FileName, part_settings->progress))
2806 return false;
2807 }
2808
2809 if (part_settings->adbbackup) {
2810 if (!twadbbu::Write_TWEOF())
Ethan Yonker96af84a2015-01-05 14:58:36 -06002811 return false;
Gary Peck15e623d2012-11-21 21:07:58 -08002812 }
Dees_Troy43d8b002012-09-17 16:00:01 -04002813 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002814}
Dees_Troy5bf43922012-09-07 16:07:55 -04002815
2816bool TWPartition::Update_Size(bool Display_Error) {
Dees_Troy0550cfb2012-10-13 11:56:13 -04002817 bool ret = false, Was_Already_Mounted = false;
Dees_Troy51127312012-09-08 13:08:49 -04002818
Ethan Yonker1b190162016-12-05 15:25:19 -06002819 Find_Actual_Block_Device();
2820
2821 if (!Can_Be_Mounted && !Is_Encrypted) {
2822 if (TWFunc::Path_Exists(Actual_Block_Device) && Find_Partition_Size()) {
2823 Used = Size;
2824 Backup_Size = Size;
2825 return true;
2826 }
Dees_Troy5bf43922012-09-07 16:07:55 -04002827 return false;
Ethan Yonker1b190162016-12-05 15:25:19 -06002828 }
Dees_Troy5bf43922012-09-07 16:07:55 -04002829
Dees_Troy0550cfb2012-10-13 11:56:13 -04002830 Was_Already_Mounted = Is_Mounted();
Dees_Troy38bd7602012-09-14 13:33:53 -04002831 if (Removable || Is_Encrypted) {
2832 if (!Mount(false))
2833 return true;
2834 } else if (!Mount(Display_Error))
Dees_Troy5bf43922012-09-07 16:07:55 -04002835 return false;
Dees_Troy51127312012-09-08 13:08:49 -04002836
2837 ret = Get_Size_Via_statfs(Display_Error);
Dees_Troy0550cfb2012-10-13 11:56:13 -04002838 if (!ret || Size == 0) {
2839 if (!Get_Size_Via_df(Display_Error)) {
2840 if (!Was_Already_Mounted)
2841 UnMount(false);
Dees_Troy51127312012-09-08 13:08:49 -04002842 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04002843 }
2844 }
Dees_Troy51127312012-09-08 13:08:49 -04002845
Dees_Troy5bf43922012-09-07 16:07:55 -04002846 if (Has_Data_Media) {
2847 if (Mount(Display_Error)) {
Ethan Yonker3fdcda42016-11-30 12:29:37 -06002848 Used = backup_exclusions.Get_Folder_Size(Mount_Point);
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002849 Backup_Size = Used;
2850 int bak = (int)(Used / 1048576LLU);
Dees_Troy51127312012-09-08 13:08:49 -04002851 int fre = (int)(Free / 1048576LLU);
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002852 LOGINFO("Data backup size is %iMB, free: %iMB.\n", bak, fre);
Dees_Troy0550cfb2012-10-13 11:56:13 -04002853 } else {
2854 if (!Was_Already_Mounted)
2855 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04002856 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04002857 }
Dees_Troye58d5262012-09-21 12:27:57 -04002858 } else if (Has_Android_Secure) {
2859 if (Mount(Display_Error))
Ethan Yonker3fdcda42016-11-30 12:29:37 -06002860 Backup_Size = backup_exclusions.Get_Folder_Size(Backup_Path);
Dees_Troy0550cfb2012-10-13 11:56:13 -04002861 else {
2862 if (!Was_Already_Mounted)
2863 UnMount(false);
Dees_Troye58d5262012-09-21 12:27:57 -04002864 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04002865 }
Dees_Troy5bf43922012-09-07 16:07:55 -04002866 }
Dees_Troy0550cfb2012-10-13 11:56:13 -04002867 if (!Was_Already_Mounted)
2868 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04002869 return true;
Dees_Troy51127312012-09-08 13:08:49 -04002870}
Dees_Troy38bd7602012-09-14 13:33:53 -04002871
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06002872bool TWPartition::Find_Wildcard_Block_Devices(const string& Device) {
2873 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
2874 string Path = TWFunc::Get_Path(Device);
2875 string Dev = TWFunc::Get_Filename(Device);
2876 size_t wildcard_index = Dev.find("*");
2877 if (wildcard_index != string::npos)
2878 Dev = Dev.substr(0, wildcard_index);
2879 wildcard_index = Dev.size();
2880 DIR* d = opendir(Path.c_str());
2881 if (d == NULL) {
2882 LOGINFO("Error opening '%s': %s\n", Path.c_str(), strerror(errno));
2883 return false;
2884 }
2885 struct dirent* de;
2886 while ((de = readdir(d)) != NULL) {
2887 if (de->d_type != DT_BLK || strlen(de->d_name) <= wildcard_index || strncmp(de->d_name, Dev.c_str(), wildcard_index) != 0)
2888 continue;
2889
2890 string item = Path + "/";
2891 item.append(de->d_name);
2892 if (PartitionManager.Find_Partition_By_Block_Device(item))
2893 continue;
2894 TWPartition *part = new TWPartition;
2895 char buffer[MAX_FSTAB_LINE_LENGTH];
2896 sprintf(buffer, "%s %s-%i auto defaults defaults", item.c_str(), Mount_Point.c_str(), ++mount_point_index);
2897 part->Process_Fstab_Line(buffer, false, NULL);
2898 char display[MAX_FSTAB_LINE_LENGTH];
2899 sprintf(display, "%s %i", Storage_Name.c_str(), mount_point_index);
2900 part->Storage_Name = display;
2901 part->Display_Name = display;
2902 part->Primary_Block_Device = item;
2903 part->Wildcard_Block_Device = false;
2904 part->Is_SubPartition = true;
2905 part->SubPartition_Of = Mount_Point;
2906 part->Is_Storage = Is_Storage;
2907 part->Can_Be_Mounted = true;
2908 part->Removable = true;
2909 part->Can_Be_Wiped = Can_Be_Wiped;
2910 part->Wipe_Available_in_GUI = Wipe_Available_in_GUI;
2911 part->Find_Actual_Block_Device();
2912 part->Update_Size(false);
2913 Has_SubPartition = true;
2914 PartitionManager.Output_Partition(part);
2915 PartitionManager.Add_Partition(part);
2916 }
2917 closedir(d);
2918 return (mount_point_index > 0);
2919}
2920
Dees_Troy38bd7602012-09-14 13:33:53 -04002921void TWPartition::Find_Actual_Block_Device(void) {
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06002922 if (!Sysfs_Entry.empty() && Primary_Block_Device.empty() && Decrypted_Block_Device.empty()) {
2923 /* Sysfs_Entry.empty() indicates if this is a sysfs entry that begins with /device/
2924 * If we have a syfs entry then we are looking for this device from a uevent add.
2925 * The uevent add will set the primary block device based on the data we receive from
2926 * after checking for adopted storage. If the device ends up being adopted, then the
2927 * decrypted block device will be set instead of the primary block device. */
2928 Is_Present = false;
2929 return;
2930 }
2931 if (Wildcard_Block_Device && !Is_Adopted_Storage) {
2932 Is_Present = false;
2933 Actual_Block_Device = "";
2934 Can_Be_Mounted = false;
2935 if (!Find_Wildcard_Block_Devices(Primary_Block_Device)) {
2936 string Dev = Primary_Block_Device.substr(0, Primary_Block_Device.find("*"));
2937 if (TWFunc::Path_Exists(Dev)) {
2938 Is_Present = true;
2939 Can_Be_Mounted = true;
2940 Actual_Block_Device = Dev;
2941 }
2942 }
2943 return;
2944 } else if (Is_Decrypted && !Decrypted_Block_Device.empty()) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002945 Actual_Block_Device = Decrypted_Block_Device;
Ethan Yonker1b190162016-12-05 15:25:19 -06002946 if (TWFunc::Path_Exists(Decrypted_Block_Device)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002947 Is_Present = true;
Ethan Yonker1b190162016-12-05 15:25:19 -06002948 return;
2949 }
2950 } else if (SlotSelect && TWFunc::Path_Exists(Primary_Block_Device + PartitionManager.Get_Active_Slot_Suffix())) {
2951 Actual_Block_Device = Primary_Block_Device + PartitionManager.Get_Active_Slot_Suffix();
2952 unlink(Primary_Block_Device.c_str());
2953 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
2954 Is_Present = true;
2955 return;
Dees_Troy43d8b002012-09-17 16:00:01 -04002956 } else if (TWFunc::Path_Exists(Primary_Block_Device)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002957 Is_Present = true;
2958 Actual_Block_Device = Primary_Block_Device;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002959 return;
2960 }
Ethan Yonker1b190162016-12-05 15:25:19 -06002961 if (!Alternate_Block_Device.empty() && TWFunc::Path_Exists(Alternate_Block_Device)) {
Dees_Troy3f04d032012-10-07 18:20:09 -04002962 Actual_Block_Device = Alternate_Block_Device;
Dees_Troy38bd7602012-09-14 13:33:53 -04002963 Is_Present = true;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002964 } else {
Dees_Troy38bd7602012-09-14 13:33:53 -04002965 Is_Present = false;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002966 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002967}
2968
2969void TWPartition::Recreate_Media_Folder(void) {
2970 string Command;
Ethan Yonker66a19492015-12-10 10:19:45 -06002971 string Media_Path = Mount_Point + "/media";
Dees_Troy38bd7602012-09-14 13:33:53 -04002972
Ethan Yonkerbd7492d2016-12-07 13:55:01 -06002973 if (Is_FBE) {
2974 LOGINFO("Not recreating media folder on FBE\n");
2975 return;
2976 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002977 if (!Mount(true)) {
Ethan Yonker66a19492015-12-10 10:19:45 -06002978 gui_msg(Msg(msg::kError, "recreate_folder_err=Unable to recreate {1} folder.")(Media_Path));
2979 } else if (!TWFunc::Path_Exists(Media_Path)) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002980 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Ethan Yonker66a19492015-12-10 10:19:45 -06002981 LOGINFO("Recreating %s folder.\n", Media_Path.c_str());
2982 mkdir(Media_Path.c_str(), 0770);
xiaolu9416f4f2015-06-04 08:22:23 +08002983 string Internal_path = DataManager::GetStrValue("tw_internal_path");
2984 if (!Internal_path.empty()) {
2985 LOGINFO("Recreating %s folder.\n", Internal_path.c_str());
2986 mkdir(Internal_path.c_str(), 0770);
2987 }
2988#ifdef TW_INTERNAL_STORAGE_PATH
2989 mkdir(EXPAND(TW_INTERNAL_STORAGE_PATH), 0770);
2990#endif
Matt Mower87413642017-01-17 21:14:46 -06002991
thata3d31fb2014-12-21 22:27:40 +01002992 // Afterwards, we will try to set the
2993 // default metadata that we were hopefully able to get during
2994 // early boot.
Ethan Yonker66a19492015-12-10 10:19:45 -06002995 tw_set_default_metadata(Media_Path.c_str());
xiaolu9416f4f2015-06-04 08:22:23 +08002996 if (!Internal_path.empty())
2997 tw_set_default_metadata(Internal_path.c_str());
Matt Mower87413642017-01-17 21:14:46 -06002998
Ethan Yonker5eac2222014-06-11 12:22:55 -05002999 // Toggle mount to ensure that "internal sdcard" gets mounted
bigbiff bigbiff9c754052013-01-09 09:09:08 -05003000 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Ethan Yonker5eac2222014-06-11 12:22:55 -05003001 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Dees_Troy38bd7602012-09-14 13:33:53 -04003002 }
Dees_Troy43d8b002012-09-17 16:00:01 -04003003}
Dees_Troye58d5262012-09-21 12:27:57 -04003004
3005void TWPartition::Recreate_AndSec_Folder(void) {
Dees_Troye58d5262012-09-21 12:27:57 -04003006 if (!Has_Android_Secure)
3007 return;
Dees_Troy2673cec2013-04-02 20:22:16 +00003008 LOGINFO("Creating %s: %s\n", Backup_Display_Name.c_str(), Symlink_Path.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -04003009 if (!Mount(true)) {
Ethan Yonker74db1572015-10-28 12:44:49 -05003010 gui_msg(Msg(msg::kError, "recreate_folder_err=Unable to recreate {1} folder.")(Backup_Name));
Dees_Troye58d5262012-09-21 12:27:57 -04003011 } else if (!TWFunc::Path_Exists(Symlink_Path)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00003012 LOGINFO("Recreating %s folder.\n", Backup_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05003013 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05003014 mkdir(Symlink_Path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
bigbiff bigbiff9c754052013-01-09 09:09:08 -05003015 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04003016 }
3017}
bigbiff7cb4c332014-11-26 20:36:07 -05003018
3019uint64_t TWPartition::Get_Max_FileSize() {
3020 uint64_t maxFileSize = 0;
3021 const uint64_t constGB = (uint64_t) 1024 * 1024 * 1024;
3022 const uint64_t constTB = (uint64_t) constGB * 1024;
3023 const uint64_t constPB = (uint64_t) constTB * 1024;
bigbiff0c532032014-12-21 13:41:26 -05003024 if (Current_File_System == "ext4")
3025 maxFileSize = 16 * constTB; //16 TB
3026 else if (Current_File_System == "vfat")
3027 maxFileSize = 4 * constGB; //4 GB
3028 else if (Current_File_System == "ntfs")
3029 maxFileSize = 256 * constTB; //256 TB
3030 else if (Current_File_System == "exfat")
3031 maxFileSize = 16 * constPB; //16 PB
3032 else if (Current_File_System == "ext3")
3033 maxFileSize = 2 * constTB; //2 TB
3034 else if (Current_File_System == "f2fs")
3035 maxFileSize = 3.94 * constTB; //3.94 TB
bigbiff7cb4c332014-11-26 20:36:07 -05003036 else
3037 maxFileSize = 100000000L;
3038 return maxFileSize - 1;
3039}
3040
bigbiffce8f83c2015-12-12 18:30:21 -05003041bool TWPartition::Flash_Image(PartitionSettings *part_settings) {
3042 string Restore_File_System, full_filename;
Ethan Yonker96af84a2015-01-05 14:58:36 -06003043
Ethan Yonkere080c1f2016-09-19 13:50:25 -05003044 full_filename = part_settings->Backup_Folder + "/" + Backup_FileName;
Ethan Yonker96af84a2015-01-05 14:58:36 -06003045
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05003046 LOGINFO("Image filename is: %s\n", Backup_FileName.c_str());
bigbiffce8f83c2015-12-12 18:30:21 -05003047
3048 if (Backup_Method == BM_FILES) {
Ethan Yonker96af84a2015-01-05 14:58:36 -06003049 LOGERR("Cannot flash images to file systems\n");
3050 return false;
3051 } else if (!Can_Flash_Img) {
3052 LOGERR("Cannot flash images to partitions %s\n", Display_Name.c_str());
3053 return false;
3054 } else {
3055 if (!Find_Partition_Size()) {
3056 LOGERR("Unable to find partition size for '%s'\n", Mount_Point.c_str());
3057 return false;
3058 }
bigbiffce8f83c2015-12-12 18:30:21 -05003059 unsigned long long image_size = TWFunc::Get_File_Size(full_filename);
Ethan Yonker96af84a2015-01-05 14:58:36 -06003060 if (image_size > Size) {
Ethan Yonker74db1572015-10-28 12:44:49 -05003061 LOGINFO("Size (%llu bytes) of image '%s' is larger than target device '%s' (%llu bytes)\n",
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05003062 image_size, Backup_FileName.c_str(), Actual_Block_Device.c_str(), Size);
Ethan Yonker74db1572015-10-28 12:44:49 -05003063 gui_err("img_size_err=Size of image is larger than target device");
Ethan Yonker96af84a2015-01-05 14:58:36 -06003064 return false;
3065 }
bigbiffce8f83c2015-12-12 18:30:21 -05003066 if (Backup_Method == BM_DD) {
3067 if (!part_settings->adbbackup) {
3068 if (Is_Sparse_Image(full_filename)) {
3069 return Flash_Sparse_Image(full_filename);
3070 }
Ethan Yonker472f5062016-02-25 13:47:30 -06003071 }
bigbiffce8f83c2015-12-12 18:30:21 -05003072 return Raw_Read_Write(part_settings);
3073 } else if (Backup_Method == BM_FLASH_UTILS) {
3074 return Flash_Image_FI(full_filename, NULL);
Ethan Yonker472f5062016-02-25 13:47:30 -06003075 }
Ethan Yonker96af84a2015-01-05 14:58:36 -06003076 }
3077
3078 LOGERR("Unknown flash method for '%s'\n", Mount_Point.c_str());
3079 return false;
3080}
3081
Ethan Yonker472f5062016-02-25 13:47:30 -06003082bool TWPartition::Is_Sparse_Image(const string& Filename) {
HashBanged974bb2016-01-30 14:20:09 -05003083 uint32_t magic = 0;
3084 int fd = open(Filename.c_str(), O_RDONLY);
3085 if (fd < 0) {
3086 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Filename)(strerror(errno)));
3087 return false;
3088 }
bigbiffce8f83c2015-12-12 18:30:21 -05003089
HashBanged974bb2016-01-30 14:20:09 -05003090 if (read(fd, &magic, sizeof(magic)) != sizeof(magic)) {
3091 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Filename)(strerror(errno)));
3092 close(fd);
3093 return false;
3094 }
3095 close(fd);
Ethan Yonker472f5062016-02-25 13:47:30 -06003096 if (magic == SPARSE_HEADER_MAGIC)
3097 return true;
3098 return false;
3099}
3100
3101bool TWPartition::Flash_Sparse_Image(const string& Filename) {
3102 string Command;
3103
3104 gui_msg(Msg("flashing=Flashing {1}...")(Display_Name));
3105
3106 Command = "simg2img '" + Filename + "' '" + Actual_Block_Device + "'";
Ethan Yonker96af84a2015-01-05 14:58:36 -06003107 LOGINFO("Flash command: '%s'\n", Command.c_str());
3108 TWFunc::Exec_Cmd(Command);
3109 return true;
3110}
3111
Ethan Yonker472f5062016-02-25 13:47:30 -06003112bool TWPartition::Flash_Image_FI(const string& Filename, ProgressTracking *progress) {
Ethan Yonker96af84a2015-01-05 14:58:36 -06003113 string Command;
Ethan Yonker472f5062016-02-25 13:47:30 -06003114 unsigned long long file_size;
Ethan Yonker96af84a2015-01-05 14:58:36 -06003115
Ethan Yonker74db1572015-10-28 12:44:49 -05003116 gui_msg(Msg("flashing=Flashing {1}...")(Display_Name));
Ethan Yonker472f5062016-02-25 13:47:30 -06003117 if (progress) {
3118 file_size = (unsigned long long)(TWFunc::Get_File_Size(Filename));
3119 progress->SetPartitionSize(file_size);
3120 }
Ethan Yonker96af84a2015-01-05 14:58:36 -06003121 // Sometimes flash image doesn't like to flash due to the first 2KB matching, so we erase first to ensure that it flashes
3122 Command = "erase_image " + MTD_Name;
3123 LOGINFO("Erase command: '%s'\n", Command.c_str());
3124 TWFunc::Exec_Cmd(Command);
3125 Command = "flash_image " + MTD_Name + " '" + Filename + "'";
3126 LOGINFO("Flash command: '%s'\n", Command.c_str());
3127 TWFunc::Exec_Cmd(Command);
Ethan Yonker472f5062016-02-25 13:47:30 -06003128 if (progress)
3129 progress->UpdateSize(file_size);
Ethan Yonker96af84a2015-01-05 14:58:36 -06003130 return true;
3131}
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05003132
3133void TWPartition::Change_Mount_Read_Only(bool new_value) {
3134 Mount_Read_Only = new_value;
3135}
3136
bigbiffce8f83c2015-12-12 18:30:21 -05003137bool TWPartition::Is_Read_Only() {
3138 return Mount_Read_Only;
3139}
3140
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05003141int TWPartition::Check_Lifetime_Writes() {
3142 bool original_read_only = Mount_Read_Only;
3143 int ret = 1;
3144
3145 Mount_Read_Only = true;
3146 if (Mount(false)) {
3147 Find_Actual_Block_Device();
Ethan Yonker54970bb2019-01-17 16:19:24 -06003148 string temp = Actual_Block_Device;
3149 Find_Real_Block_Device(temp, false);
3150 string block = basename(temp.c_str());
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05003151 string file = "/sys/fs/" + Current_File_System + "/" + block + "/lifetime_write_kbytes";
3152 string result;
3153 if (TWFunc::Path_Exists(file)) {
3154 if (TWFunc::read_file(file, result) != 0) {
3155 LOGINFO("Check_Lifetime_Writes of '%s' failed to read_file\n", file.c_str());
3156 } else {
3157 LOGINFO("Check_Lifetime_Writes result: '%s'\n", result.c_str());
3158 if (result == "0") {
3159 ret = 0;
3160 }
3161 }
3162 } else {
3163 LOGINFO("Check_Lifetime_Writes file does not exist '%s'\n", file.c_str());
3164 }
3165 UnMount(true);
3166 } else {
3167 LOGINFO("Check_Lifetime_Writes failed to mount '%s'\n", Mount_Point.c_str());
3168 }
3169 Mount_Read_Only = original_read_only;
3170 return ret;
3171}
Ethan Yonker66a19492015-12-10 10:19:45 -06003172
3173int TWPartition::Decrypt_Adopted() {
3174#ifdef TW_INCLUDE_CRYPTO
3175 int ret = 1;
3176 Is_Adopted_Storage = false;
3177 string Adopted_Key_File = "";
3178
3179 if (!Removable)
3180 return ret;
3181
3182 int fd = open(Alternate_Block_Device.c_str(), O_RDONLY);
3183 if (fd < 0) {
3184 LOGINFO("failed to open '%s'\n", Alternate_Block_Device.c_str());
3185 return ret;
3186 }
3187 char type_guid[80];
3188 char part_guid[80];
3189
3190 if (gpt_disk_get_partition_info(fd, 2, type_guid, part_guid) == 0) {
3191 LOGINFO("type: '%s'\n", type_guid);
3192 LOGINFO("part: '%s'\n", part_guid);
3193 Adopted_GUID = part_guid;
3194 LOGINFO("Adopted_GUID '%s'\n", Adopted_GUID.c_str());
3195 if (strcmp(type_guid, TWGptAndroidExpand) == 0) {
3196 LOGINFO("android_expand found\n");
3197 Adopted_Key_File = "/data/misc/vold/expand_";
3198 Adopted_Key_File += part_guid;
3199 Adopted_Key_File += ".key";
3200 if (TWFunc::Path_Exists(Adopted_Key_File)) {
3201 Is_Adopted_Storage = true;
3202 /* Until we find a use case for this, I think it is safe
3203 * to disable USB Mass Storage whenever adopted storage
3204 * is present.
3205 */
3206 LOGINFO("Detected adopted storage, disabling USB mass storage mode\n");
3207 DataManager::SetValue("tw_has_usb_storage", 0);
3208 }
3209 }
3210 }
3211
3212 if (Is_Adopted_Storage) {
3213 string Adopted_Block_Device = Alternate_Block_Device + "p2";
3214 if (!TWFunc::Path_Exists(Adopted_Block_Device)) {
3215 Adopted_Block_Device = Alternate_Block_Device + "2";
3216 if (!TWFunc::Path_Exists(Adopted_Block_Device)) {
3217 LOGINFO("Adopted block device does not exist\n");
3218 goto exit;
3219 }
3220 }
3221 LOGINFO("key file is '%s', block device '%s'\n", Adopted_Key_File.c_str(), Adopted_Block_Device.c_str());
3222 char crypto_blkdev[MAXPATHLEN];
3223 std::string thekey;
3224 int fdkey = open(Adopted_Key_File.c_str(), O_RDONLY);
3225 if (fdkey < 0) {
3226 LOGINFO("failed to open key file\n");
3227 goto exit;
3228 }
3229 char buf[512];
3230 ssize_t n;
3231 while ((n = read(fdkey, &buf[0], sizeof(buf))) > 0) {
3232 thekey.append(buf, n);
3233 }
3234 close(fdkey);
3235 unsigned char* key = (unsigned char*) thekey.data();
3236 cryptfs_revert_ext_volume(part_guid);
3237
3238 ret = cryptfs_setup_ext_volume(part_guid, Adopted_Block_Device.c_str(), key, thekey.size(), crypto_blkdev);
3239 if (ret == 0) {
3240 LOGINFO("adopted storage new block device: '%s'\n", crypto_blkdev);
3241 Decrypted_Block_Device = crypto_blkdev;
3242 Is_Decrypted = true;
3243 Is_Encrypted = true;
3244 Find_Actual_Block_Device();
Kjell Braden3126a112016-06-19 16:58:15 +00003245 if (!Mount_Storage_Retry(false)) {
Ethan Yonker66a19492015-12-10 10:19:45 -06003246 LOGERR("Failed to mount decrypted adopted storage device\n");
3247 Is_Decrypted = false;
3248 Is_Encrypted = false;
3249 cryptfs_revert_ext_volume(part_guid);
3250 ret = 1;
3251 } else {
Ethan Yonkerfcf3f242016-02-16 12:30:26 -06003252 UnMount(false);
3253 Has_Android_Secure = false;
3254 Symlink_Path = "";
3255 Symlink_Mount_Point = "";
3256 Backup_Name = Mount_Point.substr(1);
3257 Backup_Path = Mount_Point;
3258 TWPartition* sdext = PartitionManager.Find_Partition_By_Path("/sd-ext");
3259 if (sdext && sdext->Actual_Block_Device == Adopted_Block_Device) {
3260 LOGINFO("Removing /sd-ext from partition list due to adopted storage\n");
3261 PartitionManager.Remove_Partition_By_Path("/sd-ext");
3262 }
Ethan Yonker66a19492015-12-10 10:19:45 -06003263 Setup_Data_Media();
3264 Recreate_Media_Folder();
3265 Wipe_Available_in_GUI = true;
3266 Wipe_During_Factory_Reset = true;
3267 Can_Be_Backed_Up = true;
3268 Can_Encrypt_Backup = true;
3269 Use_Userdata_Encryption = true;
3270 Is_Storage = true;
3271 Storage_Name = "Adopted Storage";
3272 Is_SubPartition = true;
3273 SubPartition_Of = "/data";
3274 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
3275 DataManager::SetValue("tw_has_adopted_storage", 1);
3276 }
3277 } else {
3278 LOGERR("Failed to setup adopted storage decryption\n");
3279 }
3280 }
3281exit:
Matt Mower06543e32017-01-06 15:25:26 -06003282 close(fd);
Ethan Yonker66a19492015-12-10 10:19:45 -06003283 return ret;
3284#else
3285 LOGINFO("Decrypt_Adopted: no crypto support\n");
3286 return 1;
3287#endif
3288}
3289
3290void TWPartition::Revert_Adopted() {
3291#ifdef TW_INCLUDE_CRYPTO
3292 if (!Adopted_GUID.empty()) {
3293 PartitionManager.Remove_MTP_Storage(Mount_Point);
3294 UnMount(false);
3295 cryptfs_revert_ext_volume(Adopted_GUID.c_str());
3296 Is_Adopted_Storage = false;
3297 Is_Encrypted = false;
3298 Is_Decrypted = false;
3299 Decrypted_Block_Device = "";
3300 Find_Actual_Block_Device();
3301 Wipe_During_Factory_Reset = false;
3302 Can_Be_Backed_Up = false;
3303 Can_Encrypt_Backup = false;
3304 Use_Userdata_Encryption = false;
3305 Is_SubPartition = false;
3306 SubPartition_Of = "";
3307 Has_Data_Media = false;
3308 Storage_Path = Mount_Point;
3309 if (!Symlink_Mount_Point.empty()) {
3310 TWPartition* Dat = PartitionManager.Find_Partition_By_Path("/data");
3311 if (Dat) {
3312 Dat->UnMount(false);
3313 Dat->Symlink_Mount_Point = Symlink_Mount_Point;
3314 }
3315 Symlink_Mount_Point = "";
3316 }
3317 }
3318#else
3319 LOGINFO("Revert_Adopted: no crypto support\n");
3320#endif
3321}
bigbiff bigbiffb5ecaad2017-03-20 18:53:53 -04003322
3323void TWPartition::Set_Backup_FileName(string fname) {
3324 Backup_FileName = fname;
3325}
3326
3327string TWPartition::Get_Backup_Name() {
3328 return Backup_Name;
3329}