blob: 9012d497e22932c280b6b8d328115745f0ad80f2 [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);
Chaosmaster3b682732020-01-25 21:09:28 +0100439 if (Mount_Point == "/" || Mount_Point == "/system" || Mount_Point == "/system_root") {
440 Mount_Point = PartitionManager.Get_Android_Root_Path();
441 Backup_Path = Mount_Point;
442 Storage_Path = Mount_Point;
Dees_Troy5bf43922012-09-07 16:07:55 -0400443 Display_Name = "System";
Dees_Troya13d74f2013-03-24 08:54:55 -0500444 Backup_Display_Name = Display_Name;
445 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400446 Wipe_Available_in_GUI = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500447 Can_Be_Backed_Up = true;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500448 Mount_Read_Only = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400449 } else if (Mount_Point == "/data") {
450 Display_Name = "Data";
Dees_Troya13d74f2013-03-24 08:54:55 -0500451 Backup_Display_Name = Display_Name;
452 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400453 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400454 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500455 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000456 Can_Encrypt_Backup = true;
457 Use_Userdata_Encryption = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400458 } else if (Mount_Point == "/cache") {
459 Display_Name = "Cache";
Dees_Troya13d74f2013-03-24 08:54:55 -0500460 Backup_Display_Name = Display_Name;
461 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400462 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400463 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500464 Can_Be_Backed_Up = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400465 } else if (Mount_Point == "/datadata") {
Dees_Troy51127312012-09-08 13:08:49 -0400466 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400467 Display_Name = "DataData";
Dees_Troya13d74f2013-03-24 08:54:55 -0500468 Backup_Display_Name = Display_Name;
469 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400470 Is_SubPartition = true;
471 SubPartition_Of = "/data";
Dees_Troy5bf43922012-09-07 16:07:55 -0400472 DataManager::SetValue(TW_HAS_DATADATA, 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500473 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000474 Can_Encrypt_Backup = true;
475 Use_Userdata_Encryption = false; // This whole partition should be encrypted
Dees_Troy5bf43922012-09-07 16:07:55 -0400476 } else if (Mount_Point == "/sd-ext") {
Dees_Troy51127312012-09-08 13:08:49 -0400477 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400478 Display_Name = "SD-Ext";
Dees_Troya13d74f2013-03-24 08:54:55 -0500479 Backup_Display_Name = Display_Name;
480 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400481 Wipe_Available_in_GUI = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400482 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500483 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000484 Can_Encrypt_Backup = true;
485 Use_Userdata_Encryption = true;
Dees_Troy2c50e182012-09-26 20:05:28 -0400486 } else if (Mount_Point == "/boot") {
487 Display_Name = "Boot";
Dees_Troya13d74f2013-03-24 08:54:55 -0500488 Backup_Display_Name = Display_Name;
Dees_Troy2c50e182012-09-26 20:05:28 -0400489 DataManager::SetValue("tw_boot_is_mountable", 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500490 Can_Be_Backed_Up = true;
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500491 } else if (Mount_Point == "/vendor") {
492 Display_Name = "Vendor";
493 Backup_Display_Name = Display_Name;
494 Storage_Name = Display_Name;
495 Mount_Read_Only = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400496 }
497#ifdef TW_EXTERNAL_STORAGE_PATH
498 if (Mount_Point == EXPAND(TW_EXTERNAL_STORAGE_PATH)) {
499 Is_Storage = true;
500 Storage_Path = EXPAND(TW_EXTERNAL_STORAGE_PATH);
Dees_Troyc51f1f92012-09-20 15:32:13 -0400501 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500502 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400503#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000504 if (Mount_Point == "/sdcard" || Mount_Point == "/external_sd" || Mount_Point == "/external_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400505 Is_Storage = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400506 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500507 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400508#endif
Dees_Troyb05ddee2013-01-28 20:24:50 +0000509 }
Dees_Troy8170a922012-09-18 15:40:25 -0400510#ifdef TW_INTERNAL_STORAGE_PATH
511 if (Mount_Point == EXPAND(TW_INTERNAL_STORAGE_PATH)) {
512 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500513 Is_Settings_Storage = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400514 Storage_Path = EXPAND(TW_INTERNAL_STORAGE_PATH);
Dees_Troya13d74f2013-03-24 08:54:55 -0500515 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400516 }
517#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000518 if (Mount_Point == "/emmc" || Mount_Point == "/internal_sd" || Mount_Point == "/internal_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400519 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500520 Is_Settings_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500521 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400522 }
523#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400524 } else if (Is_Image(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400525 Find_Actual_Block_Device();
Ethan Yonker1b190162016-12-05 15:25:19 -0600526 Setup_Image();
Dees_Troya13d74f2013-03-24 08:54:55 -0500527 if (Mount_Point == "/boot") {
528 Display_Name = "Boot";
529 Backup_Display_Name = Display_Name;
530 Can_Be_Backed_Up = true;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600531 Can_Flash_Img = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500532 } else if (Mount_Point == "/recovery") {
533 Display_Name = "Recovery";
534 Backup_Display_Name = Display_Name;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600535 Can_Flash_Img = true;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500536 } else if (Mount_Point == "/system_image") {
537 Display_Name = "System Image";
538 Backup_Display_Name = Display_Name;
HashBanged974bb2016-01-30 14:20:09 -0500539 Can_Flash_Img = true;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500540 Can_Be_Backed_Up = true;
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500541 } else if (Mount_Point == "/vendor_image") {
542 Display_Name = "Vendor Image";
543 Backup_Display_Name = Display_Name;
HashBanged974bb2016-01-30 14:20:09 -0500544 Can_Flash_Img = true;
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500545 Can_Be_Backed_Up = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500546 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400547 }
548
Matt Mower2416a502016-04-12 19:54:46 -0500549 // Process TWRP fstab flags
550 if (strlen(twflags) > 0) {
551 string Prev_Display_Name = Display_Name;
552 string Prev_Storage_Name = Storage_Name;
553 string Prev_Backup_Display_Name = Backup_Display_Name;
554 Display_Name = "";
555 Storage_Name = "";
556 Backup_Display_Name = "";
557
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600558 Process_TW_Flags(twflags, (fstab_version == 1), fstab_version);
559 Save_FS_Flags(Fstab_File_System, Mount_Flags, Mount_Options);
Matt Mower2416a502016-04-12 19:54:46 -0500560
561 bool has_display_name = !Display_Name.empty();
562 bool has_storage_name = !Storage_Name.empty();
563 bool has_backup_name = !Backup_Display_Name.empty();
564 if (!has_display_name) Display_Name = Prev_Display_Name;
565 if (!has_storage_name) Storage_Name = Prev_Storage_Name;
566 if (!has_backup_name) Backup_Display_Name = Prev_Backup_Display_Name;
567
568 if (has_display_name && !has_storage_name)
569 Storage_Name = Display_Name;
570 if (!has_display_name && has_storage_name)
571 Display_Name = Storage_Name;
572 if (has_display_name && !has_backup_name && Backup_Display_Name != "Android Secure")
573 Backup_Display_Name = Display_Name;
574 if (!has_display_name && has_backup_name)
575 Display_Name = Backup_Display_Name;
576 }
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600577
578 if (fstab_version == 2 && twrp_flags && twrp_flags->size() > 0) {
579 it = twrp_flags->find(Mount_Point);
580 if (it != twrp_flags->end()) {
581 char twrpflags[MAX_FSTAB_LINE_LENGTH] = "";
582 int skip = 0;
583 string Flags = it->second.Flags;
584 strcpy(twrpflags, Flags.c_str());
585 if (strlen(twrpflags) > strlen("flags=") && strncmp(twrpflags, "flags=", strlen("flags=")) == 0)
586 skip += strlen("flags=");
587 char* flagptr = twrpflags;
588 flagptr += skip;
589 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
590 }
591 }
nkk7198fc3992017-12-16 16:26:42 +0200592
593 if (Mount_Point == "/persist" && Can_Be_Mounted) {
594 bool mounted = Is_Mounted();
595 if (mounted || Mount(false)) {
596 // Read the backup settings file
597 DataManager::LoadPersistValues();
598 TWFunc::Fixup_Time_On_Boot("/persist/time/");
599 if (!mounted)
600 UnMount(false);
601 }
602 }
603
Dees_Troy51127312012-09-08 13:08:49 -0400604 return true;
605}
606
Matt Mower72c87ce2016-04-26 14:34:56 -0500607void TWPartition::Partition_Post_Processing(bool Display_Error) {
608 if (Mount_Point == "/data")
609 Setup_Data_Partition(Display_Error);
610 else if (Mount_Point == "/cache")
611 Setup_Cache_Partition(Display_Error);
612}
613
Ethan Yonkerbd7492d2016-12-07 13:55:01 -0600614void TWPartition::ExcludeAll(const string& path) {
615 backup_exclusions.add_absolute_dir(path);
616 wipe_exclusions.add_absolute_dir(path);
617}
618
Matt Mower72c87ce2016-04-26 14:34:56 -0500619void TWPartition::Setup_Data_Partition(bool Display_Error) {
620 if (Mount_Point != "/data")
621 return;
622
623 // Ensure /data is not mounted as tmpfs for qcom hardware decrypt
624 UnMount(false);
625
626#ifdef TW_INCLUDE_CRYPTO
627 if (datamedia)
628 Setup_Data_Media();
629 Can_Be_Encrypted = true;
630 char crypto_blkdev[255];
631 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
632 if (strcmp(crypto_blkdev, "error") != 0) {
633 DataManager::SetValue(TW_IS_DECRYPTED, 1);
634 Is_Encrypted = true;
635 Is_Decrypted = true;
Ethan Yonker93382822018-11-01 15:25:31 -0500636 if (Key_Directory.empty())
637 Is_FBE = false;
638 else
639 Is_FBE = true;
Ethan Yonkerbd7492d2016-12-07 13:55:01 -0600640 DataManager::SetValue(TW_IS_FBE, 0);
Matt Mower72c87ce2016-04-26 14:34:56 -0500641 Decrypted_Block_Device = crypto_blkdev;
642 LOGINFO("Data already decrypted, new block device: '%s'\n", crypto_blkdev);
643 } else if (!Mount(false)) {
644 if (Is_Present) {
Ethan Yonker93382822018-11-01 15:25:31 -0500645 if (Key_Directory.empty()) {
646 set_partition_data(Actual_Block_Device.c_str(), Crypto_Key_Location.c_str(), Fstab_File_System.c_str());
647 if (cryptfs_check_footer() == 0) {
648 Is_Encrypted = true;
649 Is_Decrypted = false;
650 Can_Be_Mounted = false;
651 Current_File_System = "emmc";
652 Setup_Image();
653 DataManager::SetValue(TW_IS_ENCRYPTED, 1);
654 DataManager::SetValue(TW_CRYPTO_PWTYPE, cryptfs_get_password_type());
655 DataManager::SetValue(TW_CRYPTO_PASSWORD, "");
656 DataManager::SetValue("tw_crypto_display", "");
657 } else {
658 gui_err("mount_data_footer=Could not mount /data and unable to find crypto footer.");
659 }
660 } else {
Matt Mower72c87ce2016-04-26 14:34:56 -0500661 Is_Encrypted = true;
662 Is_Decrypted = false;
Matt Mower72c87ce2016-04-26 14:34:56 -0500663 }
Ethan Yonker93382822018-11-01 15:25:31 -0500664 } else if (Key_Directory.empty()) {
Matt Mower72c87ce2016-04-26 14:34:56 -0500665 LOGERR("Primary block device '%s' for mount point '%s' is not present!\n", Primary_Block_Device.c_str(), Mount_Point.c_str());
666 }
667 } else {
mauronofrio9bf73352019-11-23 22:27:34 +0100668
669 if (!Decrypt_FBE_DE()) {
670 LOGINFO("Trying wrapped key.\n");
671 property_set("fbe.data.wrappedkey", "true");
672 if (!Decrypt_FBE_DE()) {
Mauronofrio Matarrese6d5d08a2019-12-03 14:25:25 +0100673 LOGINFO("Unable to decrypt device\n");
mauronofrio9bf73352019-11-23 22:27:34 +0100674 }
675 }
Matt Mower72c87ce2016-04-26 14:34:56 -0500676 }
677 if (datamedia && (!Is_Encrypted || (Is_Encrypted && Is_Decrypted))) {
678 Setup_Data_Media();
679 Recreate_Media_Folder();
680 }
681#else
682 if (datamedia) {
683 Setup_Data_Media();
684 Recreate_Media_Folder();
685 }
686#endif
687}
688
Ethan Yonker93382822018-11-01 15:25:31 -0500689bool TWPartition::Decrypt_FBE_DE() {
690if (TWFunc::Path_Exists("/data/unencrypted/key/version")) {
691 LOGINFO("File Based Encryption is present\n");
692#ifdef TW_INCLUDE_FBE
693 ExcludeAll(Mount_Point + "/convert_fbe");
694 ExcludeAll(Mount_Point + "/unencrypted");
695 //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
696 ExcludeAll(Mount_Point + "/misc/vold/user_keys");
697 //ExcludeAll(Mount_Point + "/system_ce");
698 //ExcludeAll(Mount_Point + "/system_de");
699 //ExcludeAll(Mount_Point + "/misc_ce");
700 //ExcludeAll(Mount_Point + "/misc_de");
701 ExcludeAll(Mount_Point + "/system/gatekeeper.password.key");
702 ExcludeAll(Mount_Point + "/system/gatekeeper.pattern.key");
703 ExcludeAll(Mount_Point + "/system/locksettings.db");
704 //ExcludeAll(Mount_Point + "/system/locksettings.db-shm"); // don't seem to need this one, but the other 2 are needed
705 ExcludeAll(Mount_Point + "/system/locksettings.db-wal");
706 //ExcludeAll(Mount_Point + "/user_de");
707 //ExcludeAll(Mount_Point + "/misc/profiles/cur/0"); // might be important later
708 ExcludeAll(Mount_Point + "/misc/gatekeeper");
709 ExcludeAll(Mount_Point + "/misc/keystore");
710 ExcludeAll(Mount_Point + "/drm/kek.dat");
711 ExcludeAll(Mount_Point + "/system_de/0/spblob"); // contains data needed to decrypt pixel 2
712 int retry_count = 3;
713 while (!Decrypt_DE() && --retry_count)
714 usleep(2000);
715 if (retry_count > 0) {
716 property_set("ro.crypto.state", "encrypted");
717 Is_Encrypted = true;
718 Is_Decrypted = false;
719 Is_FBE = true;
720 DataManager::SetValue(TW_IS_FBE, 1);
721 DataManager::SetValue(TW_IS_ENCRYPTED, 1);
722 string filename;
723 int pwd_type = Get_Password_Type(0, filename);
724 if (pwd_type < 0) {
725 LOGERR("This TWRP does not have synthetic password decrypt support\n");
726 pwd_type = 0; // default password
727 }
728 DataManager::SetValue(TW_CRYPTO_PWTYPE, pwd_type);
729 DataManager::SetValue(TW_CRYPTO_PASSWORD, "");
730 DataManager::SetValue("tw_crypto_display", "");
731 return true;
732 }
733#else
734 LOGERR("FBE found but FBE support not present in TWRP\n");
735#endif
736 }
737 return false;
738}
739
Matt Mower72c87ce2016-04-26 14:34:56 -0500740void TWPartition::Setup_Cache_Partition(bool Display_Error __unused) {
741 if (Mount_Point != "/cache")
742 return;
743
744 if (!Mount(true))
745 return;
746
747 if (!TWFunc::Path_Exists("/cache/recovery/.")) {
748 LOGINFO("Recreating /cache/recovery folder\n");
749 if (mkdir("/cache/recovery", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0)
750 LOGERR("Could not create /cache/recovery\n");
751 }
752}
753
Matt Mower4ab42b12016-04-21 13:52:18 -0500754void TWPartition::Process_FS_Flags(const char *str) {
755 char *options = strdup(str);
756 char *ptr, *savep;
Hashcode62bd9e02013-11-19 21:59:42 -0800757
Matt Mower4ab42b12016-04-21 13:52:18 -0500758 Mount_Options = "";
Hashcode62bd9e02013-11-19 21:59:42 -0800759
Matt Mower4ab42b12016-04-21 13:52:18 -0500760 // Avoid issues with potentially nested strtok by using strtok_r
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600761 for (ptr = strtok_r(options, ",", &savep); ptr; ptr = strtok_r(NULL, ",", &savep)) {
762 char *equals = strstr(ptr, "=");
763 size_t name_len;
Matt Mower4ab42b12016-04-21 13:52:18 -0500764
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600765 if (!equals)
766 name_len = strlen(ptr);
767 else
768 name_len = equals - ptr;
769
770 // There are some flags that we want to ignore in TWRP
771 bool found_match = false;
772 for (const char** ignored_mount_item = ignored_mount_items; *ignored_mount_item; ignored_mount_item++) {
773 if (strncmp(ptr, *ignored_mount_item, name_len) == 0) {
774 found_match = true;
Hashcode62bd9e02013-11-19 21:59:42 -0800775 break;
776 }
777 }
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600778 if (found_match)
779 continue;
Hashcode62bd9e02013-11-19 21:59:42 -0800780
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600781 // mount_flags are never postfixed by '='
782 if (!equals) {
783 const struct flag_list* mount_flag = mount_flags;
784 for (; mount_flag->name; mount_flag++) {
785 if (strcmp(ptr, mount_flag->name) == 0) {
786 if (mount_flag->flag == MS_RDONLY)
787 Mount_Read_Only = true;
788 else
789 Mount_Flags |= (unsigned)mount_flag->flag;
790 found_match = true;
791 break;
792 }
793 }
794 if (found_match)
795 continue;
Matt Mower4ab42b12016-04-21 13:52:18 -0500796 }
797
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600798 // If we aren't ignoring this flag and it's not a mount flag, then it must be a mount option
799 if (!Mount_Options.empty())
800 Mount_Options += ",";
801 Mount_Options += ptr;
Matt Mower4ab42b12016-04-21 13:52:18 -0500802 }
803 free(options);
Hashcode62bd9e02013-11-19 21:59:42 -0800804}
805
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600806void TWPartition::Save_FS_Flags(const string& local_File_System, int local_Mount_Flags, const string& local_Mount_Options) {
807 partition_fs_flags_struct flags;
808 flags.File_System = local_File_System;
809 flags.Mount_Flags = local_Mount_Flags;
810 flags.Mount_Options = local_Mount_Options;
811 fs_flags.push_back(flags);
812}
813
Matt Mower2416a502016-04-12 19:54:46 -0500814void TWPartition::Apply_TW_Flag(const unsigned flag, const char* str, const bool val) {
815 switch (flag) {
816 case TWFLAG_ANDSEC:
817 Has_Android_Secure = val;
818 break;
819 case TWFLAG_BACKUP:
820 Can_Be_Backed_Up = val;
821 break;
822 case TWFLAG_BACKUPNAME:
823 Backup_Display_Name = str;
824 break;
825 case TWFLAG_BLOCKSIZE:
826 Format_Block_Size = (unsigned long)(atol(str));
827 break;
828 case TWFLAG_CANBEWIPED:
829 Can_Be_Wiped = val;
830 break;
831 case TWFLAG_CANENCRYPTBACKUP:
832 Can_Encrypt_Backup = val;
833 break;
834 case TWFLAG_DEFAULTS:
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600835 case TWFLAG_WAIT:
836 case TWFLAG_VERIFY:
837 case TWFLAG_CHECK:
838 case TWFLAG_NOTRIM:
839 case TWFLAG_VOLDMANAGED:
840 case TWFLAG_RESIZE:
Matt Mower2416a502016-04-12 19:54:46 -0500841 // Do nothing
842 break;
843 case TWFLAG_DISPLAY:
844 Display_Name = str;
845 break;
846 case TWFLAG_ENCRYPTABLE:
847 case TWFLAG_FORCEENCRYPT:
848 Crypto_Key_Location = str;
849 break;
Ethan Yonkere9afc3d2018-08-30 15:16:27 -0500850 case TWFLAG_FILEENCRYPTION:
851 // This flag isn't used by TWRP but is needed in 9.0 FBE decrypt
852 // fileencryption=ice:aes-256-heh
853 {
854 std::string FBE = str;
Ethan Yonkere9afc3d2018-08-30 15:16:27 -0500855 size_t colon_loc = FBE.find(":");
856 if (colon_loc == std::string::npos) {
Ethan Yonker93382822018-11-01 15:25:31 -0500857 property_set("fbe.contents", FBE.c_str());
858 property_set("fbe.filenames", "");
859 LOGINFO("FBE contents '%s', filenames ''\n", FBE.c_str());
Ethan Yonkere9afc3d2018-08-30 15:16:27 -0500860 break;
861 }
Ethan Yonker93382822018-11-01 15:25:31 -0500862 std::string FBE_contents, FBE_filenames;
Ethan Yonkere9afc3d2018-08-30 15:16:27 -0500863 FBE_contents = FBE.substr(0, colon_loc);
864 FBE_filenames = FBE.substr(colon_loc + 1);
865 property_set("fbe.contents", FBE_contents.c_str());
866 property_set("fbe.filenames", FBE_filenames.c_str());
867 LOGINFO("FBE contents '%s', filenames '%s'\n", FBE_contents.c_str(), FBE_filenames.c_str());
868 }
869 break;
Matt Mower2416a502016-04-12 19:54:46 -0500870 case TWFLAG_FLASHIMG:
871 Can_Flash_Img = val;
872 break;
873 case TWFLAG_FSFLAGS:
Matt Mower4ab42b12016-04-21 13:52:18 -0500874 Process_FS_Flags(str);
Matt Mower2416a502016-04-12 19:54:46 -0500875 break;
876 case TWFLAG_IGNOREBLKID:
877 Ignore_Blkid = val;
878 break;
879 case TWFLAG_LENGTH:
880 Length = atoi(str);
881 break;
882 case TWFLAG_MOUNTTODECRYPT:
883 Mount_To_Decrypt = val;
884 break;
885 case TWFLAG_REMOVABLE:
886 Removable = val;
887 break;
888 case TWFLAG_RETAINLAYOUTVERSION:
889 Retain_Layout_Version = val;
890 break;
891 case TWFLAG_SETTINGSSTORAGE:
892 Is_Settings_Storage = val;
893 if (Is_Settings_Storage)
894 Is_Storage = true;
895 break;
896 case TWFLAG_STORAGE:
897 Is_Storage = val;
898 break;
899 case TWFLAG_STORAGENAME:
900 Storage_Name = str;
901 break;
902 case TWFLAG_SUBPARTITIONOF:
903 Is_SubPartition = true;
904 SubPartition_Of = str;
905 break;
906 case TWFLAG_SYMLINK:
907 Symlink_Path = str;
908 break;
909 case TWFLAG_USERDATAENCRYPTBACKUP:
910 Use_Userdata_Encryption = val;
911 if (Use_Userdata_Encryption)
912 Can_Encrypt_Backup = true;
913 break;
914 case TWFLAG_USERMRF:
915 Use_Rm_Rf = val;
916 break;
917 case TWFLAG_WIPEDURINGFACTORYRESET:
918 Wipe_During_Factory_Reset = val;
919 if (Wipe_During_Factory_Reset) {
920 Can_Be_Wiped = true;
921 Wipe_Available_in_GUI = true;
922 }
923 break;
924 case TWFLAG_WIPEINGUI:
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600925 case TWFLAG_FORMATTABLE:
Matt Mower2416a502016-04-12 19:54:46 -0500926 Wipe_Available_in_GUI = val;
927 if (Wipe_Available_in_GUI)
928 Can_Be_Wiped = true;
929 break;
Ethan Yonker1b190162016-12-05 15:25:19 -0600930 case TWFLAG_SLOTSELECT:
931 SlotSelect = true;
932 break;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600933 case TWFLAG_ALTDEVICE:
934 Alternate_Block_Device = str;
935 break;
Ethan Yonker93382822018-11-01 15:25:31 -0500936 case TWFLAG_KEYDIRECTORY:
937 Key_Directory = str;
Matt Mower2416a502016-04-12 19:54:46 -0500938 default:
939 // Should not get here
940 LOGINFO("Flag identified for processing, but later unmatched: %i\n", flag);
941 break;
942 }
943}
Dees_Troy51127312012-09-08 13:08:49 -0400944
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600945void TWPartition::Process_TW_Flags(char *flags, bool Display_Error, int fstab_ver) {
Matt Mower2416a502016-04-12 19:54:46 -0500946 char separator[2] = {'\n', 0};
947 char *ptr, *savep;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600948 char source_separator = ';';
949
950 if (fstab_ver == 2)
951 source_separator = ',';
Matt Mower2416a502016-04-12 19:54:46 -0500952
953 // Semicolons within double-quotes are not forbidden, so replace
954 // only the semicolons intended as separators with '\n' for strtok
955 for (unsigned i = 0, skip = 0; i < strlen(flags); i++) {
956 if (flags[i] == '\"')
Dees_Troya13d74f2013-03-24 08:54:55 -0500957 skip = !skip;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600958 if (!skip && flags[i] == source_separator)
Matt Mower2416a502016-04-12 19:54:46 -0500959 flags[i] = separator[0];
Dees_Troy51127312012-09-08 13:08:49 -0400960 }
961
Matt Mower2416a502016-04-12 19:54:46 -0500962 // Avoid issues with potentially nested strtok by using strtok_r
963 ptr = strtok_r(flags, separator, &savep);
964 while (ptr) {
965 int ptr_len = strlen(ptr);
966 const struct flag_list* tw_flag = tw_flags;
Hashcode62bd9e02013-11-19 21:59:42 -0800967
Matt Mower2416a502016-04-12 19:54:46 -0500968 for (; tw_flag->name; tw_flag++) {
969 int flag_len = strlen(tw_flag->name);
970
971 if (strncmp(ptr, tw_flag->name, flag_len) == 0) {
972 bool flag_val = false;
973
974 if (ptr_len > flag_len && (tw_flag->name)[flag_len-1] != '='
975 && ptr[flag_len] != '=') {
976 // Handle flags with same starting string
977 // (e.g. backup and backupname)
978 continue;
979 } else if (ptr_len > flag_len && ptr[flag_len] == '=') {
980 // Handle flags with dual format: Part 1
981 // (e.g. backup and backup=y. backup=y handled here)
982 ptr += flag_len + 1;
983 TWFunc::Strip_Quotes(ptr);
984 // Skip flags with empty argument
985 // (e.g. backup=)
986 if (strlen(ptr) == 0) {
987 LOGINFO("Flag missing argument or should not include '=': %s=\n", tw_flag->name);
988 break;
989 }
990 flag_val = strchr("yY1", *ptr) != NULL;
991 } else if (ptr_len == flag_len
992 && (tw_flag->name)[flag_len-1] == '=') {
993 // Skip flags missing argument after =
994 // (e.g. backupname=)
995 LOGINFO("Flag missing argument: %s\n", tw_flag->name);
996 break;
997 } else if (ptr_len > flag_len
998 && (tw_flag->name)[flag_len-1] == '=') {
999 // Handle arguments to flags
1000 // (e.g. backupname="My Stuff")
1001 ptr += flag_len;
1002 TWFunc::Strip_Quotes(ptr);
1003 // Skip flags with empty argument
1004 // (e.g. backupname="")
1005 if (strlen(ptr) == 0) {
1006 LOGINFO("Flag missing argument: %s\n", tw_flag->name);
1007 break;
1008 }
1009 } else if (ptr_len == flag_len) {
1010 // Handle flags with dual format: Part 2
1011 // (e.g. backup and backup=y. backup handled here)
1012 flag_val = true;
Ethan Yonker96af84a2015-01-05 14:58:36 -06001013 } else {
Matt Mower2416a502016-04-12 19:54:46 -05001014 LOGINFO("Flag matched, but could not be processed: %s\n", ptr);
1015 break;
Ethan Yonker96af84a2015-01-05 14:58:36 -06001016 }
Matt Mower2416a502016-04-12 19:54:46 -05001017
1018 Apply_TW_Flag(tw_flag->flag, ptr, flag_val);
1019 break;
Ethan Yonker96af84a2015-01-05 14:58:36 -06001020 }
Matt Mower2416a502016-04-12 19:54:46 -05001021 }
1022 if (tw_flag->name == 0) {
Dees_Troy51127312012-09-08 13:08:49 -04001023 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001024 LOGERR("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -04001025 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001026 LOGINFO("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -04001027 }
Matt Mower2416a502016-04-12 19:54:46 -05001028 ptr = strtok_r(NULL, separator, &savep);
Dees_Troy51127312012-09-08 13:08:49 -04001029 }
Dees_Troy51a0e822012-09-05 15:24:24 -04001030}
1031
Dees_Troy5bf43922012-09-07 16:07:55 -04001032bool TWPartition::Is_File_System(string File_System) {
1033 if (File_System == "ext2" ||
Dees_Troy63c8df72012-09-10 14:02:05 -04001034 File_System == "ext3" ||
Dees_Troy5bf43922012-09-07 16:07:55 -04001035 File_System == "ext4" ||
1036 File_System == "vfat" ||
1037 File_System == "ntfs" ||
1038 File_System == "yaffs2" ||
bigbiff bigbiff3e146522012-11-14 14:32:59 -05001039 File_System == "exfat" ||
Dees_Troye5017042013-08-29 16:38:55 +00001040 File_System == "f2fs" ||
Erfan Abdib859e912019-02-05 18:18:46 +02001041 File_System == "squashfs" ||
Dees_Troy5bf43922012-09-07 16:07:55 -04001042 File_System == "auto")
1043 return true;
1044 else
1045 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001046}
1047
Dees_Troy5bf43922012-09-07 16:07:55 -04001048bool TWPartition::Is_Image(string File_System) {
Dees_Troy5fcd8f92012-10-16 12:22:05 -04001049 if (File_System == "emmc" || File_System == "mtd" || File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -04001050 return true;
1051 else
1052 return false;
1053}
1054
Dees_Troy51127312012-09-08 13:08:49 -04001055bool TWPartition::Make_Dir(string Path, bool Display_Error) {
Ethan Yonker93ac7a02016-11-07 22:05:58 -06001056 if (TWFunc::Get_D_Type_From_Stat(Path) != S_IFDIR)
1057 unlink(Path.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001058 if (!TWFunc::Path_Exists(Path)) {
Dees_Troy51127312012-09-08 13:08:49 -04001059 if (mkdir(Path.c_str(), 0777) == -1) {
1060 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001061 gui_msg(Msg(msg::kError, "create_folder_strerr=Can not create '{1}' folder ({2}).")(Path)(strerror(errno)));
Dees_Troy51127312012-09-08 13:08:49 -04001062 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001063 LOGINFO("Can not create '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -04001064 return false;
1065 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001066 LOGINFO("Created '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -04001067 return true;
1068 }
1069 }
1070 return true;
1071}
1072
Dees_Troy5bf43922012-09-07 16:07:55 -04001073void TWPartition::Setup_File_System(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001074 Can_Be_Mounted = true;
1075 Can_Be_Wiped = true;
1076
Dees_Troy5bf43922012-09-07 16:07:55 -04001077 // Make the mount point folder if it doesn't exist
Dees_Troy51127312012-09-08 13:08:49 -04001078 Make_Dir(Mount_Point, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -04001079 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
1080 Backup_Name = Display_Name;
bigbiffce8f83c2015-12-12 18:30:21 -05001081 Backup_Method = BM_FILES;
Dees_Troy5bf43922012-09-07 16:07:55 -04001082}
1083
Ethan Yonker1b190162016-12-05 15:25:19 -06001084void TWPartition::Setup_Image() {
Dees_Troy5bf43922012-09-07 16:07:55 -04001085 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
1086 Backup_Name = Display_Name;
Gary Peck82599a82012-11-21 16:23:12 -08001087 if (Current_File_System == "emmc")
bigbiffce8f83c2015-12-12 18:30:21 -05001088 Backup_Method = BM_DD;
Gary Peck82599a82012-11-21 16:23:12 -08001089 else if (Current_File_System == "mtd" || Current_File_System == "bml")
bigbiffce8f83c2015-12-12 18:30:21 -05001090 Backup_Method = BM_FLASH_UTILS;
Dees_Troy5bf43922012-09-07 16:07:55 -04001091 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001092 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 -04001093}
1094
Dees_Troye58d5262012-09-21 12:27:57 -04001095void TWPartition::Setup_AndSec(void) {
Dees_Troya13d74f2013-03-24 08:54:55 -05001096 Backup_Display_Name = "Android Secure";
Dees_Troye58d5262012-09-21 12:27:57 -04001097 Backup_Name = "and-sec";
Dees_Troya13d74f2013-03-24 08:54:55 -05001098 Can_Be_Backed_Up = true;
Dees_Troye58d5262012-09-21 12:27:57 -04001099 Has_Android_Secure = true;
1100 Symlink_Path = Mount_Point + "/.android_secure";
1101 Symlink_Mount_Point = "/and-sec";
1102 Backup_Path = Symlink_Mount_Point;
1103 Make_Dir("/and-sec", true);
1104 Recreate_AndSec_Folder();
Kjell Braden3126a112016-06-19 16:58:15 +00001105 Mount_Storage_Retry(true);
Dees_Troye58d5262012-09-21 12:27:57 -04001106}
1107
that9e0593e2014-10-08 00:01:24 +02001108void TWPartition::Setup_Data_Media() {
Ethan Yonker6277c792014-09-15 14:54:30 -05001109 LOGINFO("Setting up '%s' as data/media emulated storage.\n", Mount_Point.c_str());
Ethan Yonker66a19492015-12-10 10:19:45 -06001110 if (Storage_Name.empty() || Storage_Name == "Data")
1111 Storage_Name = "Internal Storage";
Ethan Yonker6277c792014-09-15 14:54:30 -05001112 Has_Data_Media = true;
1113 Is_Storage = true;
Ethan Yonker66a19492015-12-10 10:19:45 -06001114 Storage_Path = Mount_Point + "/media";
Ethan Yonker6277c792014-09-15 14:54:30 -05001115 Symlink_Path = Storage_Path;
Ethan Yonker66a19492015-12-10 10:19:45 -06001116 if (Mount_Point == "/data") {
1117 Is_Settings_Storage = true;
1118 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
1119 Make_Dir("/emmc", false);
1120 Symlink_Mount_Point = "/emmc";
1121 } else {
1122 Make_Dir("/sdcard", false);
1123 Symlink_Mount_Point = "/sdcard";
1124 }
1125 if (Mount(false) && TWFunc::Path_Exists(Mount_Point + "/media/0")) {
1126 Storage_Path = Mount_Point + "/media/0";
1127 Symlink_Path = Storage_Path;
1128 DataManager::SetValue(TW_INTERNAL_PATH, Mount_Point + "/media/0");
1129 UnMount(true);
1130 }
1131 DataManager::SetValue("tw_has_internal", 1);
1132 DataManager::SetValue("tw_has_data_media", 1);
Ethan Yonker3fdcda42016-11-30 12:29:37 -06001133 backup_exclusions.add_absolute_dir("/data/data/com.google.android.music/files");
Darth90864aa52020-01-14 20:58:16 +00001134 backup_exclusions.add_absolute_dir("/data/per_boot"); // DJ9,14Jan2020 - exclude this dir to prevent "error 255" on AOSP ROMs that create and lock it
Ethan Yonker6355b562017-05-01 09:42:17 -05001135 wipe_exclusions.add_absolute_dir(Mount_Point + "/misc/vold"); // adopted storage keys
Ethan Yonker79f88bd2016-12-09 14:52:12 -06001136 ExcludeAll(Mount_Point + "/.layout_version");
1137 ExcludeAll(Mount_Point + "/system/storage.xml");
Ethan Yonker6277c792014-09-15 14:54:30 -05001138 } else {
Ethan Yonker66a19492015-12-10 10:19:45 -06001139 if (Mount(true) && TWFunc::Path_Exists(Mount_Point + "/media/0")) {
1140 Storage_Path = Mount_Point + "/media/0";
1141 Symlink_Path = Storage_Path;
1142 UnMount(true);
1143 }
Ethan Yonker6277c792014-09-15 14:54:30 -05001144 }
Ethan Yonker79f88bd2016-12-09 14:52:12 -06001145 ExcludeAll(Mount_Point + "/media");
Ethan Yonker6277c792014-09-15 14:54:30 -05001146}
1147
Dees_Troy5bf43922012-09-07 16:07:55 -04001148void TWPartition::Find_Real_Block_Device(string& Block, bool Display_Error) {
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06001149 char device[PATH_MAX], realDevice[PATH_MAX];
Dees_Troy5bf43922012-09-07 16:07:55 -04001150
1151 strcpy(device, Block.c_str());
1152 memset(realDevice, 0, sizeof(realDevice));
1153 while (readlink(device, realDevice, sizeof(realDevice)) > 0)
1154 {
1155 strcpy(device, realDevice);
1156 memset(realDevice, 0, sizeof(realDevice));
1157 }
1158
1159 if (device[0] != '/') {
1160 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001161 LOGERR("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001162 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001163 LOGINFO("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001164 return;
Dees_Troy51a0e822012-09-05 15:24:24 -04001165 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -04001166 Block = device;
1167 return;
1168 }
1169}
1170
Kjell Braden3126a112016-06-19 16:58:15 +00001171bool TWPartition::Mount_Storage_Retry(bool Display_Error) {
Dees_Troy8e337f32012-10-13 22:07:49 -04001172 // On some devices, storage doesn't want to mount right away, retry and sleep
Kjell Braden3126a112016-06-19 16:58:15 +00001173 if (!Mount(Display_Error)) {
Dees_Troy8e337f32012-10-13 22:07:49 -04001174 int retry_count = 5;
1175 while (retry_count > 0 && !Mount(false)) {
1176 usleep(500000);
1177 retry_count--;
1178 }
Kjell Braden3126a112016-06-19 16:58:15 +00001179 return Mount(Display_Error);
Dees_Troy8e337f32012-10-13 22:07:49 -04001180 }
Kjell Braden3126a112016-06-19 16:58:15 +00001181 return true;
Dees_Troy8e337f32012-10-13 22:07:49 -04001182}
1183
Dees_Troy38bd7602012-09-14 13:33:53 -04001184bool TWPartition::Find_MTD_Block_Device(string MTD_Name) {
1185 FILE *fp = NULL;
1186 char line[255];
1187
1188 fp = fopen("/proc/mtd", "rt");
1189 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001190 LOGERR("Device does not support /proc/mtd\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001191 return false;
1192 }
1193
1194 while (fgets(line, sizeof(line), fp) != NULL)
1195 {
1196 char device[32], label[32];
1197 unsigned long size = 0;
Dees_Troy38bd7602012-09-14 13:33:53 -04001198 int deviceId;
1199
1200 sscanf(line, "%s %lx %*s %*c%s", device, &size, label);
1201
1202 // Skip header and blank lines
1203 if ((strcmp(device, "dev:") == 0) || (strlen(line) < 8))
1204 continue;
1205
1206 // Strip off the trailing " from the label
1207 label[strlen(label)-1] = '\0';
1208
1209 if (strcmp(label, MTD_Name.c_str()) == 0) {
1210 // We found our device
1211 // Strip off the trailing : from the device
1212 device[strlen(device)-1] = '\0';
1213 if (sscanf(device,"mtd%d", &deviceId) == 1) {
1214 sprintf(device, "/dev/block/mtdblock%d", deviceId);
1215 Primary_Block_Device = device;
Dees_Troy76543db2013-06-19 16:24:30 +00001216 fclose(fp);
1217 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -04001218 }
1219 }
1220 }
1221 fclose(fp);
1222
1223 return false;
1224}
1225
Dees_Troy51127312012-09-08 13:08:49 -04001226bool TWPartition::Get_Size_Via_statfs(bool Display_Error) {
1227 struct statfs st;
1228 string Local_Path = Mount_Point + "/.";
1229
1230 if (!Mount(Display_Error))
1231 return false;
1232
1233 if (statfs(Local_Path.c_str(), &st) != 0) {
1234 if (!Removable) {
1235 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001236 LOGERR("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -04001237 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001238 LOGINFO("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -04001239 }
1240 return false;
1241 }
1242 Size = (st.f_blocks * st.f_bsize);
1243 Used = ((st.f_blocks - st.f_bfree) * st.f_bsize);
1244 Free = (st.f_bfree * st.f_bsize);
1245 Backup_Size = Used;
1246 return true;
1247}
1248
1249bool TWPartition::Get_Size_Via_df(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001250 FILE* fp;
1251 char command[255], line[512];
1252 int include_block = 1;
1253 unsigned int min_len;
1254
1255 if (!Mount(Display_Error))
1256 return false;
1257
Dees_Troy38bd7602012-09-14 13:33:53 -04001258 min_len = Actual_Block_Device.size() + 2;
Dees_Troy51127312012-09-08 13:08:49 -04001259 sprintf(command, "df %s > /tmp/dfoutput.txt", Mount_Point.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001260 TWFunc::Exec_Cmd(command);
Dees_Troy51127312012-09-08 13:08:49 -04001261 fp = fopen("/tmp/dfoutput.txt", "rt");
1262 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001263 LOGINFO("Unable to open /tmp/dfoutput.txt.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001264 return false;
Dees_Troy51127312012-09-08 13:08:49 -04001265 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001266
1267 while (fgets(line, sizeof(line), fp) != NULL)
1268 {
1269 unsigned long blocks, used, available;
1270 char device[64];
1271 char tmpString[64];
1272
1273 if (strncmp(line, "Filesystem", 10) == 0)
1274 continue;
1275 if (strlen(line) < min_len) {
1276 include_block = 0;
1277 continue;
1278 }
1279 if (include_block) {
1280 sscanf(line, "%s %lu %lu %lu", device, &blocks, &used, &available);
1281 } else {
1282 // The device block string is so long that the df information is on the next line
1283 int space_count = 0;
Dees_Troye58d5262012-09-21 12:27:57 -04001284 sprintf(tmpString, "/dev/block/%s", Actual_Block_Device.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001285 while (tmpString[space_count] == 32)
1286 space_count++;
1287 sscanf(line + space_count, "%lu %lu %lu", &blocks, &used, &available);
1288 }
1289
1290 // Adjust block size to byte size
1291 Size = blocks * 1024ULL;
1292 Used = used * 1024ULL;
1293 Free = available * 1024ULL;
1294 Backup_Size = Used;
1295 }
1296 fclose(fp);
1297 return true;
1298}
1299
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001300unsigned long long TWPartition::IOCTL_Get_Block_Size() {
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001301 Find_Actual_Block_Device();
Ethan Yonker483e9f42016-01-11 22:21:18 -06001302
1303 return TWFunc::IOCTL_Get_Block_Size(Actual_Block_Device.c_str());
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001304}
1305
Dees_Troy5bf43922012-09-07 16:07:55 -04001306bool TWPartition::Find_Partition_Size(void) {
1307 FILE* fp;
1308 char line[512];
1309 string tmpdevice;
1310
igoriok87e3d932013-01-31 21:03:53 +02001311 fp = fopen("/proc/dumchar_info", "rt");
1312 if (fp != NULL) {
1313 while (fgets(line, sizeof(line), fp) != NULL)
1314 {
1315 char label[32], device[32];
1316 unsigned long size = 0;
1317
thatd43bf2d2014-09-21 23:13:02 +02001318 sscanf(line, "%s %lx %*x %*u %s", label, &size, device);
igoriok87e3d932013-01-31 21:03:53 +02001319
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05001320 // Skip header, annotation and blank lines
igoriok87e3d932013-01-31 21:03:53 +02001321 if ((strncmp(device, "/dev/", 5) != 0) || (strlen(line) < 8))
1322 continue;
1323
1324 tmpdevice = "/dev/";
1325 tmpdevice += label;
1326 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
1327 Size = size;
1328 fclose(fp);
1329 return true;
1330 }
1331 }
1332 }
1333
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001334 unsigned long long ioctl_size = IOCTL_Get_Block_Size();
1335 if (ioctl_size) {
1336 Size = ioctl_size;
1337 return true;
1338 }
1339
Dees_Troy5bf43922012-09-07 16:07:55 -04001340 // In this case, we'll first get the partitions we care about (with labels)
1341 fp = fopen("/proc/partitions", "rt");
1342 if (fp == NULL)
1343 return false;
1344
1345 while (fgets(line, sizeof(line), fp) != NULL)
1346 {
1347 unsigned long major, minor, blocks;
1348 char device[512];
Dees_Troy5bf43922012-09-07 16:07:55 -04001349
Dees_Troy63c8df72012-09-10 14:02:05 -04001350 if (strlen(line) < 7 || line[0] == 'm') continue;
Dees_Troy5bf43922012-09-07 16:07:55 -04001351 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
1352
1353 tmpdevice = "/dev/block/";
1354 tmpdevice += device;
Dees_Troy38bd7602012-09-14 13:33:53 -04001355 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001356 // Adjust block size to byte size
1357 Size = blocks * 1024ULL;
1358 fclose(fp);
1359 return true;
1360 }
1361 }
1362 fclose(fp);
1363 return false;
1364}
1365
Dees_Troy5bf43922012-09-07 16:07:55 -04001366bool TWPartition::Is_Mounted(void) {
1367 if (!Can_Be_Mounted)
1368 return false;
1369
1370 struct stat st1, st2;
1371 string test_path;
1372
1373 // Check to see if the mount point directory exists
1374 test_path = Mount_Point + "/.";
1375 if (stat(test_path.c_str(), &st1) != 0) return false;
1376
1377 // Check to see if the directory above the mount point exists
1378 test_path = Mount_Point + "/../.";
1379 if (stat(test_path.c_str(), &st2) != 0) return false;
1380
1381 // Compare the device IDs -- if they match then we're (probably) using tmpfs instead of an actual device
1382 int ret = (st1.st_dev != st2.st_dev) ? true : false;
1383
1384 return ret;
1385}
1386
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05001387bool TWPartition::Is_File_System_Writable(void) {
1388 if (!Is_File_System(Current_File_System) || !Is_Mounted())
1389 return false;
1390
1391 string test_path = Mount_Point + "/.";
1392 return (access(test_path.c_str(), W_OK) == 0);
1393}
1394
Dees_Troy5bf43922012-09-07 16:07:55 -04001395bool TWPartition::Mount(bool Display_Error) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001396 int exfat_mounted = 0;
Martin Dünkelmann6d843782018-12-27 23:40:28 +01001397 unsigned int flags = Mount_Flags;
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001398
Dees_Troy5bf43922012-09-07 16:07:55 -04001399 if (Is_Mounted()) {
1400 return true;
1401 } else if (!Can_Be_Mounted) {
1402 return false;
1403 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001404
1405 Find_Actual_Block_Device();
1406
1407 // Check the current file system before mounting
1408 Check_FS_Type();
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001409 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/exfat-fuse")) {
Dees_Troye34c1332013-02-06 19:13:00 +00001410 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 +00001411 LOGINFO("cmd: %s\n", cmd.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001412 string result;
1413 if (TWFunc::Exec_Cmd(cmd, result) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001414 LOGINFO("exfat-fuse failed to mount with result '%s', trying vfat\n", result.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001415 Current_File_System = "vfat";
1416 } else {
1417#ifdef TW_NO_EXFAT_FUSE
1418 UnMount(false);
1419 // We'll let the kernel handle it but using exfat-fuse to detect if the file system is actually exfat
1420 // Some kernels let us mount vfat as exfat which doesn't work out too well
1421#else
1422 exfat_mounted = 1;
1423#endif
1424 }
1425 }
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001426
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001427 if (Current_File_System == "ntfs" && (TWFunc::Path_Exists("/sbin/ntfs-3g") || TWFunc::Path_Exists("/sbin/mount.ntfs"))) {
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001428 string cmd;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001429 string Ntfsmount_Binary = "";
1430
1431 if (TWFunc::Path_Exists("/sbin/ntfs-3g"))
1432 Ntfsmount_Binary = "ntfs-3g";
1433 else if (TWFunc::Path_Exists("/sbin/mount.ntfs"))
1434 Ntfsmount_Binary = "mount.ntfs";
1435
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001436 if (Mount_Read_Only)
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001437 cmd = "/sbin/" + Ntfsmount_Binary + " -o ro " + Actual_Block_Device + " " + Mount_Point;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001438 else
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001439 cmd = "/sbin/" + Ntfsmount_Binary + " " + Actual_Block_Device + " " + Mount_Point;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001440 LOGINFO("cmd: '%s'\n", cmd.c_str());
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001441
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001442 if (TWFunc::Exec_Cmd(cmd) == 0) {
1443 return true;
1444 } else {
1445 LOGINFO("ntfs-3g failed to mount, trying regular mount method.\n");
1446 }
1447 }
1448
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001449 if (Mount_Read_Only)
1450 flags |= MS_RDONLY;
1451
Dees_Troy22042032012-12-18 21:23:08 +00001452 if (Fstab_File_System == "yaffs2") {
1453 // mount an MTD partition as a YAFFS2 filesystem.
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001454 flags = MS_NOATIME | MS_NODEV | MS_NODIRATIME;
1455 if (Mount_Read_Only)
1456 flags |= MS_RDONLY;
Dees_Troy76543db2013-06-19 16:24:30 +00001457 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags, NULL) < 0) {
1458 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags | MS_RDONLY, NULL) < 0) {
1459 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001460 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy76543db2013-06-19 16:24:30 +00001461 else
1462 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
1463 return false;
1464 } else {
1465 LOGINFO("Mounted '%s' (MTD) as RO\n", Mount_Point.c_str());
1466 return true;
1467 }
1468 } else {
1469 struct stat st;
1470 string test_path = Mount_Point;
1471 if (stat(test_path.c_str(), &st) < 0) {
1472 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001473 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy76543db2013-06-19 16:24:30 +00001474 else
1475 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
1476 return false;
1477 }
1478 mode_t new_mode = st.st_mode | S_IXUSR | S_IXGRP | S_IXOTH;
1479 if (new_mode != st.st_mode) {
1480 LOGINFO("Fixing execute permissions for %s\n", Mount_Point.c_str());
1481 if (chmod(Mount_Point.c_str(), new_mode) < 0) {
1482 if (Display_Error)
1483 LOGERR("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
1484 else
1485 LOGINFO("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
1486 return false;
1487 }
1488 }
Dees_Troy22042032012-12-18 21:23:08 +00001489 return true;
Dees_Troy76543db2013-06-19 16:24:30 +00001490 }
thatc7572eb2015-03-31 18:55:30 +02001491 }
1492
1493 string mount_fs = Current_File_System;
1494 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sys/module/texfat"))
1495 mount_fs = "texfat";
1496
1497 if (!exfat_mounted &&
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001498 mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), mount_fs.c_str(), flags, Mount_Options.c_str()) != 0 &&
1499 mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), mount_fs.c_str(), flags, NULL) != 0) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001500#ifdef TW_NO_EXFAT_FUSE
1501 if (Current_File_System == "exfat") {
Dees_Troy2673cec2013-04-02 20:22:16 +00001502 LOGINFO("Mounting exfat failed, trying vfat...\n");
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001503 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), "vfat", 0, NULL) != 0) {
Dees_Troy85f44ed2013-01-09 18:42:36 +00001504 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001505 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy85f44ed2013-01-09 18:42:36 +00001506 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001507 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001508 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 +00001509 return false;
Dees_Troy85f44ed2013-01-09 18:42:36 +00001510 }
Dees_Troyb05ddee2013-01-28 20:24:50 +00001511 } else {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001512#endif
bigbiff bigbiff26774a02014-03-29 18:22:00 -04001513 if (!Removable && Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001514 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001515 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001516 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
1517 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 +00001518 return false;
1519#ifdef TW_NO_EXFAT_FUSE
Dees_Troy85f44ed2013-01-09 18:42:36 +00001520 }
1521#endif
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001522 }
Ethan Yonker253368a2014-11-25 15:00:52 -06001523
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001524 if (Removable)
1525 Update_Size(Display_Error);
1526
xiaolu9416f4f2015-06-04 08:22:23 +08001527 if (!Symlink_Mount_Point.empty() && TWFunc::Path_Exists(Symlink_Path)) {
codelover2a3d4ce2015-03-14 20:26:49 +08001528 string Command = "mount -o bind '" + Symlink_Path + "' '" + Symlink_Mount_Point + "'";
Vojtech Bocek05534202013-09-11 08:11:56 +02001529 TWFunc::Exec_Cmd(Command);
Dees_Troy5bf43922012-09-07 16:07:55 -04001530 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001531 return true;
1532}
1533
1534bool TWPartition::UnMount(bool Display_Error) {
1535 if (Is_Mounted()) {
1536 int never_unmount_system;
1537
1538 DataManager::GetValue(TW_DONT_UNMOUNT_SYSTEM, never_unmount_system);
Captain Throwback9d6feb52018-07-27 10:05:24 -04001539 if (never_unmount_system == 1 && Mount_Point == PartitionManager.Get_Android_Root_Path())
Dees_Troy5bf43922012-09-07 16:07:55 -04001540 return true; // Never unmount system if you're not supposed to unmount it
1541
Matt Mowera8e6d832017-02-14 20:20:59 -06001542 if (Is_Storage && MTP_Storage_ID > 0)
Ethan Yonker726a0202014-12-16 20:01:38 -06001543 PartitionManager.Remove_MTP_Storage(MTP_Storage_ID);
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001544
Dees_Troy38bd7602012-09-14 13:33:53 -04001545 if (!Symlink_Mount_Point.empty())
1546 umount(Symlink_Mount_Point.c_str());
1547
Dees_Troyb05ddee2013-01-28 20:24:50 +00001548 umount(Mount_Point.c_str());
1549 if (Is_Mounted()) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001550 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001551 gui_msg(Msg(msg::kError, "fail_unmount=Failed to unmount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy5bf43922012-09-07 16:07:55 -04001552 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001553 LOGINFO("Unable to unmount '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001554 return false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001555 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -04001556 return true;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001557 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001558 } else {
1559 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001560 }
1561}
1562
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05001563bool TWPartition::ReMount(bool Display_Error) {
1564 if (UnMount(Display_Error))
1565 return Mount(Display_Error);
1566 return false;
1567}
1568
1569bool TWPartition::ReMount_RW(bool Display_Error) {
1570 // No need to remount if already mounted rw
1571 if (Is_File_System_Writable())
1572 return true;
1573
1574 bool ro = Mount_Read_Only;
1575 int flags = Mount_Flags;
1576
1577 Mount_Read_Only = false;
1578 Mount_Flags &= ~MS_RDONLY;
1579
1580 bool ret = ReMount(Display_Error);
1581
1582 Mount_Read_Only = ro;
1583 Mount_Flags = flags;
1584
1585 return ret;
1586}
1587
Gary Peck43acadf2012-11-21 21:19:01 -08001588bool TWPartition::Wipe(string New_File_System) {
Ethan Yonker726a0202014-12-16 20:01:38 -06001589 bool wiped = false, update_crypt = false, recreate_media = true;
Dees_Troy16c2b312013-01-15 16:51:18 +00001590 int check;
1591 string Layout_Filename = Mount_Point + "/.layout_version";
1592
Dees_Troy38bd7602012-09-14 13:33:53 -04001593 if (!Can_Be_Wiped) {
Matt Mower3c366972015-12-25 19:28:31 -06001594 gui_msg(Msg(msg::kError, "cannot_wipe=Partition {1} cannot be wiped.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04001595 return false;
1596 }
1597
Dees_Troyc51f1f92012-09-20 15:32:13 -04001598 if (Mount_Point == "/cache")
Dees_Troy2673cec2013-04-02 20:22:16 +00001599 Log_Offset = 0;
Dees_Troyc51f1f92012-09-20 15:32:13 -04001600
Dees_Troy16c2b312013-01-15 16:51:18 +00001601 if (Retain_Layout_Version && Mount(false) && TWFunc::Path_Exists(Layout_Filename))
1602 TWFunc::copy_file(Layout_Filename, "/.layout_version", 0600);
1603 else
1604 unlink("/.layout_version");
Dees_Troy38bd7602012-09-14 13:33:53 -04001605
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001606 if (Has_Data_Media && Current_File_System == New_File_System) {
Dees_Troy16c2b312013-01-15 16:51:18 +00001607 wiped = Wipe_Data_Without_Wiping_Media();
Ethan Yonker5eac2222014-06-11 12:22:55 -05001608 recreate_media = false;
Dees_Troy16c2b312013-01-15 16:51:18 +00001609 } else {
Dees_Troy16c2b312013-01-15 16:51:18 +00001610 DataManager::GetValue(TW_RM_RF_VAR, check);
1611
Hashcodedabfd492013-08-29 22:45:30 -07001612 if (check || Use_Rm_Rf)
Dees_Troy16c2b312013-01-15 16:51:18 +00001613 wiped = Wipe_RMRF();
1614 else if (New_File_System == "ext4")
1615 wiped = Wipe_EXT4();
1616 else if (New_File_System == "ext2" || New_File_System == "ext3")
dianlujitao4879b372018-12-03 18:45:47 +08001617 wiped = Wipe_EXTFS(New_File_System);
Dees_Troy16c2b312013-01-15 16:51:18 +00001618 else if (New_File_System == "vfat")
1619 wiped = Wipe_FAT();
1620 else if (New_File_System == "exfat")
1621 wiped = Wipe_EXFAT();
1622 else if (New_File_System == "yaffs2")
1623 wiped = Wipe_MTD();
Dees_Troye5017042013-08-29 16:38:55 +00001624 else if (New_File_System == "f2fs")
1625 wiped = Wipe_F2FS();
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001626 else if (New_File_System == "ntfs")
1627 wiped = Wipe_NTFS();
Dees_Troy16c2b312013-01-15 16:51:18 +00001628 else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001629 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 +00001630 unlink("/.layout_version");
1631 return false;
1632 }
1633 update_crypt = wiped;
Gary Pecke8bc5d72012-12-21 06:45:25 -08001634 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001635
Gary Pecke8bc5d72012-12-21 06:45:25 -08001636 if (wiped) {
Dees_Troy1c1ac442013-01-17 21:42:14 +00001637 if (Mount_Point == "/cache")
1638 DataManager::Output_Version();
1639
Dees_Troy16c2b312013-01-15 16:51:18 +00001640 if (TWFunc::Path_Exists("/.layout_version") && Mount(false))
1641 TWFunc::copy_file("/.layout_version", Layout_Filename, 0600);
1642
1643 if (update_crypt) {
1644 Setup_File_System(false);
1645 if (Is_Encrypted && !Is_Decrypted) {
1646 // just wiped an encrypted partition back to its unencrypted state
1647 Is_Encrypted = false;
1648 Is_Decrypted = false;
1649 Decrypted_Block_Device = "";
1650 if (Mount_Point == "/data") {
1651 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1652 DataManager::SetValue(TW_IS_DECRYPTED, 0);
1653 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001654 }
1655 }
Ethan Yonker5eac2222014-06-11 12:22:55 -05001656
Ethan Yonker66a19492015-12-10 10:19:45 -06001657 if (Has_Data_Media && recreate_media) {
Ethan Yonker5eac2222014-06-11 12:22:55 -05001658 Recreate_Media_Folder();
1659 }
Matt Mower209c9632016-01-20 12:08:35 -06001660 if (Is_Storage && Mount(false))
1661 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Gary Pecke8bc5d72012-12-21 06:45:25 -08001662 }
Matt Mower209c9632016-01-20 12:08:35 -06001663
Gary Pecke8bc5d72012-12-21 06:45:25 -08001664 return wiped;
Dees_Troy51a0e822012-09-05 15:24:24 -04001665}
1666
Gary Peck43acadf2012-11-21 21:19:01 -08001667bool TWPartition::Wipe() {
Gary Peck82599a82012-11-21 16:23:12 -08001668 if (Is_File_System(Current_File_System))
1669 return Wipe(Current_File_System);
1670 else
1671 return Wipe(Fstab_File_System);
Gary Peck43acadf2012-11-21 21:19:01 -08001672}
1673
Dees_Troye58d5262012-09-21 12:27:57 -04001674bool TWPartition::Wipe_AndSec(void) {
1675 if (!Has_Android_Secure)
1676 return false;
1677
Dees_Troye58d5262012-09-21 12:27:57 -04001678 if (!Mount(true))
1679 return false;
1680
Ethan Yonker74db1572015-10-28 12:44:49 -05001681 gui_msg(Msg("wiping=Wiping {1}")(Backup_Display_Name));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001682 TWFunc::removeDir(Mount_Point + "/.android_secure/", true);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001683 return true;
Dees_Troye58d5262012-09-21 12:27:57 -04001684}
1685
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001686bool TWPartition::Can_Repair() {
Ethan Yonkera2719152015-05-28 09:44:41 -05001687 if (Mount_Read_Only)
1688 return false;
Matt Mower18794c82015-11-11 16:22:45 -06001689 if (Current_File_System == "vfat" && TWFunc::Path_Exists("/sbin/fsck.fat"))
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001690 return true;
1691 else if ((Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") && TWFunc::Path_Exists("/sbin/e2fsck"))
1692 return true;
1693 else if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/fsck.exfat"))
1694 return true;
1695 else if (Current_File_System == "f2fs" && TWFunc::Path_Exists("/sbin/fsck.f2fs"))
1696 return true;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001697 else if (Current_File_System == "ntfs" && (TWFunc::Path_Exists("/sbin/ntfsfix") || TWFunc::Path_Exists("/sbin/fsck.ntfs")))
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001698 return true;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001699 return false;
1700}
1701
1702bool TWPartition::Repair() {
1703 string command;
1704
1705 if (Current_File_System == "vfat") {
Matt Mower18794c82015-11-11 16:22:45 -06001706 if (!TWFunc::Path_Exists("/sbin/fsck.fat")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001707 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.fat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001708 return false;
1709 }
1710 if (!UnMount(true))
1711 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001712 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.fat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001713 Find_Actual_Block_Device();
Matt Mower18794c82015-11-11 16:22:45 -06001714 command = "/sbin/fsck.fat -y " + Actual_Block_Device;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001715 LOGINFO("Repair command: %s\n", command.c_str());
1716 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001717 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001718 return true;
1719 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001720 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001721 return false;
1722 }
1723 }
1724 if (Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") {
1725 if (!TWFunc::Path_Exists("/sbin/e2fsck")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001726 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("e2fsck"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001727 return false;
1728 }
1729 if (!UnMount(true))
1730 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001731 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("e2fsck"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001732 Find_Actual_Block_Device();
Ethan Yonkera2719152015-05-28 09:44:41 -05001733 command = "/sbin/e2fsck -fp " + Actual_Block_Device;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001734 LOGINFO("Repair command: %s\n", command.c_str());
1735 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001736 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001737 return true;
1738 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001739 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001740 return false;
1741 }
1742 }
1743 if (Current_File_System == "exfat") {
1744 if (!TWFunc::Path_Exists("/sbin/fsck.exfat")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001745 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.exfat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001746 return false;
1747 }
1748 if (!UnMount(true))
1749 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001750 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.exfat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001751 Find_Actual_Block_Device();
1752 command = "/sbin/fsck.exfat " + Actual_Block_Device;
1753 LOGINFO("Repair command: %s\n", command.c_str());
1754 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001755 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001756 return true;
1757 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001758 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001759 return false;
1760 }
1761 }
1762 if (Current_File_System == "f2fs") {
1763 if (!TWFunc::Path_Exists("/sbin/fsck.f2fs")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001764 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.f2fs"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001765 return false;
1766 }
1767 if (!UnMount(true))
1768 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001769 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.f2fs"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001770 Find_Actual_Block_Device();
1771 command = "/sbin/fsck.f2fs " + Actual_Block_Device;
1772 LOGINFO("Repair command: %s\n", command.c_str());
1773 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001774 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001775 return true;
1776 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001777 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001778 return false;
1779 }
1780 }
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001781 if (Current_File_System == "ntfs") {
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001782 string Ntfsfix_Binary;
1783 if (TWFunc::Path_Exists("/sbin/ntfsfix"))
1784 Ntfsfix_Binary = "ntfsfix";
1785 else if (TWFunc::Path_Exists("/sbin/fsck.ntfs"))
1786 Ntfsfix_Binary = "fsck.ntfs";
1787 else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001788 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("ntfsfix"));
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001789 return false;
1790 }
1791 if (!UnMount(true))
1792 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001793 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)(Ntfsfix_Binary));
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001794 Find_Actual_Block_Device();
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001795 command = "/sbin/" + Ntfsfix_Binary + " " + Actual_Block_Device;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001796 LOGINFO("Repair command: %s\n", command.c_str());
1797 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001798 gui_msg("done=Done.");
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001799 return true;
1800 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001801 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001802 return false;
1803 }
1804 }
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001805 return false;
1806}
1807
Ethan Yonkera2719152015-05-28 09:44:41 -05001808bool TWPartition::Can_Resize() {
1809 if (Mount_Read_Only)
1810 return false;
1811 if ((Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") && TWFunc::Path_Exists("/sbin/resize2fs"))
1812 return true;
1813 return false;
1814}
1815
1816bool TWPartition::Resize() {
1817 string command;
1818
1819 if (Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") {
1820 if (!Can_Repair()) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001821 LOGINFO("Cannot resize %s because %s cannot be repaired before resizing.\n", Display_Name.c_str(), Display_Name.c_str());
1822 gui_msg(Msg(msg::kError, "cannot_resize=Cannot resize {1}.")(Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001823 return false;
1824 }
1825 if (!TWFunc::Path_Exists("/sbin/resize2fs")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001826 LOGINFO("resize2fs does not exist! Cannot resize!\n");
1827 gui_msg(Msg(msg::kError, "cannot_resize=Cannot resize {1}.")(Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001828 return false;
1829 }
1830 // Repair will unmount so no need to do it twice
Ethan Yonker74db1572015-10-28 12:44:49 -05001831 gui_msg(Msg("repair_resize=Repairing {1} before resizing.")( Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001832 if (!Repair())
1833 return false;
Ethan Yonker74db1572015-10-28 12:44:49 -05001834 gui_msg(Msg("resizing=Resizing {1} using {2}...")(Display_Name)("resize2fs"));
Ethan Yonkera2719152015-05-28 09:44:41 -05001835 Find_Actual_Block_Device();
1836 command = "/sbin/resize2fs " + Actual_Block_Device;
1837 if (Length != 0) {
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001838 unsigned long long Actual_Size = IOCTL_Get_Block_Size();
1839 if (Actual_Size == 0)
1840 return false;
Ethan Yonkera2719152015-05-28 09:44:41 -05001841
Ethan Yonkera2719152015-05-28 09:44:41 -05001842 unsigned long long Block_Count;
1843 if (Length < 0) {
1844 // Reduce overall size by this length
1845 Block_Count = (Actual_Size / 1024LLU) - ((unsigned long long)(Length * -1) / 1024LLU);
1846 } else {
1847 // This is the size, not a size reduction
1848 Block_Count = ((unsigned long long)(Length) / 1024LLU);
1849 }
1850 char temp[256];
1851 sprintf(temp, "%llu", Block_Count);
1852 command += " ";
1853 command += temp;
1854 command += "K";
1855 }
1856 LOGINFO("Resize command: %s\n", command.c_str());
1857 if (TWFunc::Exec_Cmd(command) == 0) {
1858 Update_Size(true);
Ethan Yonker74db1572015-10-28 12:44:49 -05001859 gui_msg("done=Done.");
Ethan Yonkera2719152015-05-28 09:44:41 -05001860 return true;
1861 } else {
1862 Update_Size(true);
Ethan Yonker74db1572015-10-28 12:44:49 -05001863 gui_msg(Msg(msg::kError, "unable_resize=Unable to resize {1}.")(Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001864 return false;
1865 }
1866 }
1867 return false;
1868}
1869
bigbiffce8f83c2015-12-12 18:30:21 -05001870bool TWPartition::Backup(PartitionSettings *part_settings, pid_t *tar_fork_pid) {
1871 if (Backup_Method == BM_FILES)
1872 return Backup_Tar(part_settings, tar_fork_pid);
1873 else if (Backup_Method == BM_DD)
1874 return Backup_Image(part_settings);
1875 else if (Backup_Method == BM_FLASH_UTILS)
1876 return Backup_Dump_Image(part_settings);
Dees_Troy2673cec2013-04-02 20:22:16 +00001877 LOGERR("Unknown backup method for '%s'\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001878 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001879}
1880
bigbiffce8f83c2015-12-12 18:30:21 -05001881bool TWPartition::Restore(PartitionSettings *part_settings) {
Matt Mower3c366972015-12-25 19:28:31 -06001882 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, gui_parse_text("{@restoring_hdr}"));
Ethan Yonkere080c1f2016-09-19 13:50:25 -05001883 LOGINFO("Restore filename is: %s/%s\n", part_settings->Backup_Folder.c_str(), Backup_FileName.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001884
bigbiffce8f83c2015-12-12 18:30:21 -05001885 string Restore_File_System = Get_Restore_File_System(part_settings);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001886
1887 if (Is_File_System(Restore_File_System))
bigbiffce8f83c2015-12-12 18:30:21 -05001888 return Restore_Tar(part_settings);
1889 else if (Is_Image(Restore_File_System))
1890 return Restore_Image(part_settings);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001891
1892 LOGERR("Unknown restore method for '%s'\n", Mount_Point.c_str());
1893 return false;
1894}
1895
bigbiffce8f83c2015-12-12 18:30:21 -05001896string TWPartition::Get_Restore_File_System(PartitionSettings *part_settings) {
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001897 size_t first_period, second_period;
1898 string Restore_File_System;
1899
Gary Peck43acadf2012-11-21 21:19:01 -08001900 // Parse backup filename to extract the file system before wiping
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05001901 first_period = Backup_FileName.find(".");
Gary Peck43acadf2012-11-21 21:19:01 -08001902 if (first_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001903 LOGERR("Unable to find file system (first period).\n");
thatd43bf2d2014-09-21 23:13:02 +02001904 return string();
Gary Peck43acadf2012-11-21 21:19:01 -08001905 }
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05001906 Restore_File_System = Backup_FileName.substr(first_period + 1, Backup_FileName.size() - first_period - 1);
Gary Peck43acadf2012-11-21 21:19:01 -08001907 second_period = Restore_File_System.find(".");
1908 if (second_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001909 LOGERR("Unable to find file system (second period).\n");
thatd43bf2d2014-09-21 23:13:02 +02001910 return string();
Gary Peck43acadf2012-11-21 21:19:01 -08001911 }
1912 Restore_File_System.resize(second_period);
Matt Mowera0cd91d2016-12-30 18:21:42 -06001913 LOGINFO("Restore file system is: '%s'.\n", Restore_File_System.c_str());
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001914 return Restore_File_System;
Dees_Troy51a0e822012-09-05 15:24:24 -04001915}
1916
1917string TWPartition::Backup_Method_By_Name() {
bigbiffce8f83c2015-12-12 18:30:21 -05001918 if (Backup_Method == BM_NONE)
Dees_Troy38bd7602012-09-14 13:33:53 -04001919 return "none";
bigbiffce8f83c2015-12-12 18:30:21 -05001920 else if (Backup_Method == BM_FILES)
Dees_Troy38bd7602012-09-14 13:33:53 -04001921 return "files";
bigbiffce8f83c2015-12-12 18:30:21 -05001922 else if (Backup_Method == BM_DD)
Dees_Troy38bd7602012-09-14 13:33:53 -04001923 return "dd";
bigbiffce8f83c2015-12-12 18:30:21 -05001924 else if (Backup_Method == BM_FLASH_UTILS)
Dees_Troy38bd7602012-09-14 13:33:53 -04001925 return "flash_utils";
1926 else
1927 return "undefined";
1928 return "ERROR!";
Dees_Troy51a0e822012-09-05 15:24:24 -04001929}
1930
1931bool TWPartition::Decrypt(string Password) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001932 LOGINFO("STUB TWPartition::Decrypt, password: '%s'\n", Password.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001933 // Is this needed?
Dees_Troy51a0e822012-09-05 15:24:24 -04001934 return 1;
1935}
1936
1937bool TWPartition::Wipe_Encryption() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001938 bool Save_Data_Media = Has_Data_Media;
Ethan Yonker93382822018-11-01 15:25:31 -05001939 bool ret = false;
1940 BasePartition* base_partition = make_partition();
1941
1942 if (!base_partition->PreWipeEncryption())
1943 goto exit;
Dees_Troy38bd7602012-09-14 13:33:53 -04001944
dianlujitao4879b372018-12-03 18:45:47 +08001945 Find_Actual_Block_Device();
1946 if (!Is_Present) {
1947 LOGINFO("Block device not present, cannot format %s.\n", Display_Name.c_str());
1948 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
1949 return false;
1950 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001951 if (!UnMount(true))
Ethan Yonker93382822018-11-01 15:25:31 -05001952 goto exit;
Dees_Troy38bd7602012-09-14 13:33:53 -04001953
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001954#ifdef TW_INCLUDE_CRYPTO
Ethan Yonker1b190162016-12-05 15:25:19 -06001955 if (Is_Decrypted && !Decrypted_Block_Device.empty()) {
Matt Mower2b18a532015-02-20 16:58:05 -06001956 if (delete_crypto_blk_dev((char*)("userdata")) != 0) {
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001957 LOGERR("Error deleting crypto block device, continuing anyway.\n");
1958 }
1959 }
1960#endif
dianlujitao4879b372018-12-03 18:45:47 +08001961 Has_Data_Media = false;
Ethan Yonker93382822018-11-01 15:25:31 -05001962 Decrypted_Block_Device = "";
Dees_Troy74fb2e92013-04-15 14:35:47 +00001963 Is_Decrypted = false;
1964 Is_Encrypted = false;
Gary Pecke8bc5d72012-12-21 06:45:25 -08001965 if (Wipe(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001966 Has_Data_Media = Save_Data_Media;
1967 if (Has_Data_Media && !Symlink_Mount_Point.empty()) {
1968 Recreate_Media_Folder();
Ethan Yonker726a0202014-12-16 20:01:38 -06001969 if (Mount(false))
1970 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04001971 }
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001972 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
Ethan Yonker83e82572014-04-04 10:59:28 -05001973#ifndef TW_OEM_BUILD
Ethan Yonker74db1572015-10-28 12:44:49 -05001974 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 -05001975#endif
Ethan Yonker93382822018-11-01 15:25:31 -05001976 ret = true;
1977 if (!Key_Directory.empty())
1978 ret = PartitionManager.Wipe_By_Path(Key_Directory);
1979 if (ret)
1980 ret = base_partition->PostWipeEncryption();
1981 goto exit;
Dees_Troy38bd7602012-09-14 13:33:53 -04001982 } else {
1983 Has_Data_Media = Save_Data_Media;
Ethan Yonker74db1572015-10-28 12:44:49 -05001984 gui_err("format_data_err=Unable to format to remove encryption.");
Ethan Yonker726a0202014-12-16 20:01:38 -06001985 if (Has_Data_Media && Mount(false))
1986 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Ethan Yonker93382822018-11-01 15:25:31 -05001987 goto exit;
Dees_Troy38bd7602012-09-14 13:33:53 -04001988 }
Ethan Yonker93382822018-11-01 15:25:31 -05001989exit:
1990 delete base_partition;
1991 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04001992}
1993
1994void TWPartition::Check_FS_Type() {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001995 const char* type;
1996 blkid_probe pr;
Dees_Troy5bf43922012-09-07 16:07:55 -04001997
Dees_Troy68cab492012-12-12 19:29:35 +00001998 if (Fstab_File_System == "yaffs2" || Fstab_File_System == "mtd" || Fstab_File_System == "bml" || Ignore_Blkid)
1999 return; // Running blkid on some mtd devices causes a massive crash or needs to be skipped
Dees_Troy5bf43922012-09-07 16:07:55 -04002000
Dees_Troy38bd7602012-09-14 13:33:53 -04002001 Find_Actual_Block_Device();
Dees_Troy8170a922012-09-18 15:40:25 -04002002 if (!Is_Present)
2003 return;
Dees_Troy51127312012-09-08 13:08:49 -04002004
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05002005 pr = blkid_new_probe_from_filename(Actual_Block_Device.c_str());
2006 if (blkid_do_fullprobe(pr)) {
2007 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00002008 LOGINFO("Can't probe device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05002009 return;
Dees_Troy5bf43922012-09-07 16:07:55 -04002010 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02002011
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05002012 if (blkid_probe_lookup_value(pr, "TYPE", &type, NULL) < 0) {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05002013 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00002014 LOGINFO("can't find filesystem on device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05002015 return;
2016 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02002017
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05002018 Current_File_System = type;
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02002019 blkid_free_probe(pr);
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06002020 if (fs_flags.size() > 1) {
2021 std::vector<partition_fs_flags_struct>::iterator iter;
2022 std::vector<partition_fs_flags_struct>::iterator found = fs_flags.begin();
2023
2024 for (iter = fs_flags.begin(); iter != fs_flags.end(); iter++) {
2025 if (iter->File_System == Current_File_System) {
2026 found = iter;
2027 break;
2028 }
2029 }
2030 // If we don't find a match, we default the flags to the first set of flags that we received from the fstab
2031 if (Mount_Flags != found->Mount_Flags || Mount_Options != found->Mount_Options) {
2032 Mount_Flags = found->Mount_Flags;
2033 Mount_Options = found->Mount_Options;
2034 LOGINFO("Mount_Flags: %i, Mount_Options: %s\n", Mount_Flags, Mount_Options.c_str());
2035 }
2036 }
Dees_Troy51a0e822012-09-05 15:24:24 -04002037}
2038
dianlujitao4879b372018-12-03 18:45:47 +08002039bool TWPartition::Wipe_EXTFS(string File_System) {
2040#if PLATFORM_SDK_VERSION < 28
2041 if (!TWFunc::Path_Exists("/sbin/mke2fs"))
2042#else
2043 if (!TWFunc::Path_Exists("/sbin/mke2fs") || !TWFunc::Path_Exists("/sbin/e2fsdroid"))
2044#endif
Dees_Troy38bd7602012-09-14 13:33:53 -04002045 return Wipe_RMRF();
2046
dianlujitao4879b372018-12-03 18:45:47 +08002047 int ret;
2048 bool NeedPreserveFooter = true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002049
Ethan Yonker25f20c12014-10-14 09:04:43 -05002050 Find_Actual_Block_Device();
2051 if (!Is_Present) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002052 LOGINFO("Block device not present, cannot wipe %s.\n", Display_Name.c_str());
2053 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Ethan Yonker25f20c12014-10-14 09:04:43 -05002054 return false;
2055 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002056 if (!UnMount(true))
2057 return false;
2058
dianlujitao4879b372018-12-03 18:45:47 +08002059 /**
2060 * On decrypted devices, IOCTL_Get_Block_Size calculates size on device mapper,
2061 * so there's no need to preserve footer.
2062 */
2063 if ((Is_Decrypted && !Decrypted_Block_Device.empty()) ||
2064 Crypto_Key_Location != "footer") {
2065 NeedPreserveFooter = false;
2066 }
2067
2068 unsigned long long dev_sz = TWFunc::IOCTL_Get_Block_Size(Actual_Block_Device.c_str());
2069 if (!dev_sz)
2070 return false;
2071
2072 if (NeedPreserveFooter)
2073 Length < 0 ? dev_sz += Length : dev_sz -= CRYPT_FOOTER_OFFSET;
2074
Dees Troy9a4d7402019-03-21 14:17:28 -04002075 char dout[16];
2076 sprintf(dout, "%llu", dev_sz / 4096);
2077
2078 //string size_str =to_string(dev_sz / 4096);
2079 string size_str = dout;
dianlujitao4879b372018-12-03 18:45:47 +08002080 string Command;
2081
2082 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mke2fs"));
2083
2084 // Execute mke2fs to create empty ext4 filesystem
2085 Command = "mke2fs -t " + File_System + " -b 4096 " + Actual_Block_Device + " " + size_str;
2086 LOGINFO("mke2fs command: %s\n", Command.c_str());
2087 ret = TWFunc::Exec_Cmd(Command);
2088 if (ret) {
2089 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
2090 return false;
2091 }
2092
2093 if (TWFunc::Path_Exists("/sbin/e2fsdroid")) {
dianlujitao3cf05a52019-05-15 15:33:27 +08002094 const string& File_Contexts_Entry = (Mount_Point == "/system_root" ? "/" : Mount_Point);
2095 char *secontext = NULL;
2096 if (!selinux_handle || selabel_lookup(selinux_handle, &secontext, File_Contexts_Entry.c_str(), S_IFDIR) < 0) {
2097 LOGINFO("Cannot lookup security context for '%s'\n", Mount_Point.c_str());
2098 } else {
2099 // Execute e2fsdroid to initialize selinux context
2100 Command = "e2fsdroid -e -S /file_contexts -a " + File_Contexts_Entry + " " + Actual_Block_Device;
2101 LOGINFO("e2fsdroid command: %s\n", Command.c_str());
2102 ret = TWFunc::Exec_Cmd(Command);
2103 if (ret) {
2104 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
2105 return false;
2106 }
dianlujitao4879b372018-12-03 18:45:47 +08002107 }
2108 } else {
2109 LOGINFO("e2fsdroid not present\n");
2110 }
2111
2112 if (NeedPreserveFooter)
2113 Wipe_Crypto_Key();
2114 Current_File_System = File_System;
2115 Recreate_AndSec_Folder();
2116 gui_msg("done=Done.");
2117 return true;
2118}
2119
2120bool TWPartition::Wipe_EXT4() {
2121#ifdef USE_EXT4
Ethan Yonkerf27497f2014-02-09 11:48:33 -06002122 int ret;
dianlujitao4879b372018-12-03 18:45:47 +08002123 bool NeedPreserveFooter = true;
2124
2125 Find_Actual_Block_Device();
2126 if (!Is_Present) {
2127 LOGINFO("Block device not present, cannot wipe %s.\n", Display_Name.c_str());
2128 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
2129 return false;
2130 }
2131 if (!UnMount(true))
2132 return false;
2133
2134 /**
2135 * On decrypted devices, IOCTL_Get_Block_Size calculates size on device mapper,
2136 * so there's no need to preserve footer.
2137 */
2138 if ((Is_Decrypted && !Decrypted_Block_Device.empty()) ||
2139 Crypto_Key_Location != "footer") {
2140 NeedPreserveFooter = false;
2141 }
2142
2143 unsigned long long dev_sz = TWFunc::IOCTL_Get_Block_Size(Actual_Block_Device.c_str());
2144 if (!dev_sz)
2145 return false;
2146
2147 if (NeedPreserveFooter)
2148 Length < 0 ? dev_sz += Length : dev_sz -= CRYPT_FOOTER_OFFSET;
2149
Ethan Yonkerf27497f2014-02-09 11:48:33 -06002150 char *secontext = NULL;
2151
Greg Wallace4b44fef2015-12-29 15:33:24 -05002152 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("make_ext4fs"));
Ethan Yonkerf27497f2014-02-09 11:48:33 -06002153
Dees Troy99c8dbf2014-03-10 16:53:58 +00002154 if (!selinux_handle || selabel_lookup(selinux_handle, &secontext, Mount_Point.c_str(), S_IFDIR) < 0) {
Ethan Yonkerf27497f2014-02-09 11:48:33 -06002155 LOGINFO("Cannot lookup security context for '%s'\n", Mount_Point.c_str());
dianlujitao4879b372018-12-03 18:45:47 +08002156 ret = make_ext4fs(Actual_Block_Device.c_str(), dev_sz, Mount_Point.c_str(), NULL);
Ethan Yonkerf27497f2014-02-09 11:48:33 -06002157 } else {
dianlujitao4879b372018-12-03 18:45:47 +08002158 ret = make_ext4fs(Actual_Block_Device.c_str(), dev_sz, Mount_Point.c_str(), selinux_handle);
Ethan Yonkerf27497f2014-02-09 11:48:33 -06002159 }
2160 if (ret != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002161 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troya95f55c2013-08-17 13:14:43 +00002162 return false;
2163 } else {
dianlujitao4879b372018-12-03 18:45:47 +08002164 if (NeedPreserveFooter)
2165 Wipe_Crypto_Key();
bigbiff bigbiffc49d7062013-10-11 20:28:00 -04002166 string sedir = Mount_Point + "/lost+found";
2167 PartitionManager.Mount_By_Path(sedir.c_str(), true);
2168 rmdir(sedir.c_str());
2169 mkdir(sedir.c_str(), S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP);
Dees_Troya95f55c2013-08-17 13:14:43 +00002170 return true;
2171 }
2172#else
dianlujitao4879b372018-12-03 18:45:47 +08002173 return Wipe_EXTFS("ext4");
Dees_Troya95f55c2013-08-17 13:14:43 +00002174#endif
Dees_Troy51a0e822012-09-05 15:24:24 -04002175}
2176
2177bool TWPartition::Wipe_FAT() {
Vojtech Bocek05534202013-09-11 08:11:56 +02002178 string command;
Dees_Troy38bd7602012-09-14 13:33:53 -04002179
Matt Mower18794c82015-11-11 16:22:45 -06002180 if (TWFunc::Path_Exists("/sbin/mkfs.fat")) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002181 if (!UnMount(true))
2182 return false;
2183
Greg Wallace4b44fef2015-12-29 15:33:24 -05002184 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mkfs.fat"));
Dees_Troy38bd7602012-09-14 13:33:53 -04002185 Find_Actual_Block_Device();
Matt Mower18794c82015-11-11 16:22:45 -06002186 command = "mkfs.fat " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02002187 if (TWFunc::Exec_Cmd(command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08002188 Current_File_System = "vfat";
Dees_Troye58d5262012-09-21 12:27:57 -04002189 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05002190 gui_msg("done=Done.");
Dees_Troy38bd7602012-09-14 13:33:53 -04002191 return true;
2192 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002193 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04002194 return false;
2195 }
2196 return true;
2197 }
2198 else
2199 return Wipe_RMRF();
2200
2201 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04002202}
2203
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002204bool TWPartition::Wipe_EXFAT() {
Vojtech Bocek05534202013-09-11 08:11:56 +02002205 string command;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002206
2207 if (TWFunc::Path_Exists("/sbin/mkexfatfs")) {
2208 if (!UnMount(true))
2209 return false;
2210
Greg Wallace4b44fef2015-12-29 15:33:24 -05002211 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mkexfatfs"));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002212 Find_Actual_Block_Device();
2213 command = "mkexfatfs " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02002214 if (TWFunc::Exec_Cmd(command) == 0) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002215 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05002216 gui_msg("done=Done.");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002217 return true;
2218 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002219 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002220 return false;
2221 }
2222 return true;
2223 }
2224 return false;
2225}
2226
Dees_Troy38bd7602012-09-14 13:33:53 -04002227bool TWPartition::Wipe_MTD() {
2228 if (!UnMount(true))
2229 return false;
2230
Greg Wallace4b44fef2015-12-29 15:33:24 -05002231 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("MTD"));
Dees_Troy38bd7602012-09-14 13:33:53 -04002232
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02002233 mtd_scan_partitions();
2234 const MtdPartition* mtd = mtd_find_partition_by_name(MTD_Name.c_str());
2235 if (mtd == NULL) {
2236 LOGERR("No mtd partition named '%s'", MTD_Name.c_str());
2237 return false;
2238 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002239
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02002240 MtdWriteContext* ctx = mtd_write_partition(mtd);
2241 if (ctx == NULL) {
2242 LOGERR("Can't write '%s', failed to format.", MTD_Name.c_str());
2243 return false;
2244 }
2245 if (mtd_erase_blocks(ctx, -1) == -1) {
2246 mtd_write_close(ctx);
2247 LOGERR("Failed to format '%s'", MTD_Name.c_str());
2248 return false;
2249 }
2250 if (mtd_write_close(ctx) != 0) {
2251 LOGERR("Failed to close '%s'", MTD_Name.c_str());
2252 return false;
2253 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08002254 Current_File_System = "yaffs2";
Dees_Troye58d5262012-09-21 12:27:57 -04002255 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05002256 gui_msg("done=Done.");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02002257 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002258}
2259
2260bool TWPartition::Wipe_RMRF() {
Dees_Troy38bd7602012-09-14 13:33:53 -04002261 if (!Mount(true))
2262 return false;
Ethan Yonker726a0202014-12-16 20:01:38 -06002263 // This is the only wipe that leaves the partition mounted, so we
2264 // must manually remove the partition from MTP if it is a storage
2265 // partition.
2266 if (Is_Storage)
2267 PartitionManager.Remove_MTP_Storage(MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04002268
Ethan Yonker74db1572015-10-28 12:44:49 -05002269 gui_msg(Msg("remove_all=Removing all files under '{1}'")(Mount_Point));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002270 TWFunc::removeDir(Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04002271 Recreate_AndSec_Folder();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002272 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002273}
2274
Dees_Troye5017042013-08-29 16:38:55 +00002275bool TWPartition::Wipe_F2FS() {
Vojtech Bocek05534202013-09-11 08:11:56 +02002276 string command;
Dees_Troye5017042013-08-29 16:38:55 +00002277
2278 if (TWFunc::Path_Exists("/sbin/mkfs.f2fs")) {
dianlujitao4879b372018-12-03 18:45:47 +08002279 bool NeedPreserveFooter = true;
2280
2281 Find_Actual_Block_Device();
2282 if (!Is_Present) {
2283 LOGINFO("Block device not present, cannot wipe %s.\n", Display_Name.c_str());
2284 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
2285 return false;
2286 }
Dees_Troye5017042013-08-29 16:38:55 +00002287 if (!UnMount(true))
2288 return false;
2289
dianlujitao4879b372018-12-03 18:45:47 +08002290 /**
2291 * On decrypted devices, IOCTL_Get_Block_Size calculates size on device mapper,
2292 * so there's no need to preserve footer.
2293 */
2294 if ((Is_Decrypted && !Decrypted_Block_Device.empty()) ||
2295 Crypto_Key_Location != "footer") {
2296 NeedPreserveFooter = false;
dhacker29a3fa75f2015-01-17 19:42:33 -05002297 }
dianlujitao4879b372018-12-03 18:45:47 +08002298
dianlujitao4879b372018-12-03 18:45:47 +08002299 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mkfs.f2fs"));
Ethan Yonker7e941582019-03-22 08:18:21 -05002300 // First determine if we have the old mkfs.f2fs that uses "-r reserved_bytes"
2301 // or the new mkfs.f2fs that expects the number of sectors as the optional last argument
2302 // Note: some 7.1 trees have the old and some have the new.
2303 command = "mkfs.f2fs | grep \"reserved\" > /tmp/f2fsversiontest";
2304 TWFunc::Exec_Cmd(command, false); // no help argument so printing usage exits with an error code
2305 if (!TWFunc::Path_Exists("/tmp/f2fsversiontest")) {
2306 LOGINFO("Error determining mkfs.f2fs version\n");
2307 return false;
2308 }
2309 if (TWFunc::Get_File_Size("/tmp/f2fsversiontest") <= 0) {
2310 LOGINFO("Using newer mkfs.f2fs\n");
2311 unsigned long long dev_sz = TWFunc::IOCTL_Get_Block_Size(Actual_Block_Device.c_str());
2312 if (!dev_sz)
2313 return false;
2314
2315 if (NeedPreserveFooter)
2316 Length < 0 ? dev_sz += Length : dev_sz -= CRYPT_FOOTER_OFFSET;
2317
2318 char dev_sz_str[48];
2319 sprintf(dev_sz_str, "%llu", (dev_sz / 4096));
2320 command = "mkfs.f2fs -d1 -f -O encrypt -O quota -O verity -w 4096 " + Actual_Block_Device + " " + dev_sz_str;
2321 if (TWFunc::Path_Exists("/sbin/sload.f2fs")) {
2322 command += " && sload.f2fs -t /data " + Actual_Block_Device;
2323 }
2324 } else {
2325 LOGINFO("Using older mkfs.f2fs\n");
2326 command = "mkfs.f2fs -t 0";
2327 if (NeedPreserveFooter) {
2328 // Only use length if we're not decrypted
2329 char len[32];
2330 int mod_length = Length;
2331 if (Length < 0)
2332 mod_length *= -1;
2333 sprintf(len, "%i", mod_length);
2334 command += " -r ";
2335 command += len;
2336 }
2337 command += " " + Actual_Block_Device;
dianlujitao4879b372018-12-03 18:45:47 +08002338 }
2339 LOGINFO("mkfs.f2fs command: %s\n", command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02002340 if (TWFunc::Exec_Cmd(command) == 0) {
dianlujitao4879b372018-12-03 18:45:47 +08002341 if (NeedPreserveFooter)
2342 Wipe_Crypto_Key();
Dees_Troye5017042013-08-29 16:38:55 +00002343 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05002344 gui_msg("done=Done.");
Dees_Troye5017042013-08-29 16:38:55 +00002345 return true;
2346 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002347 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troye5017042013-08-29 16:38:55 +00002348 return false;
2349 }
2350 return true;
2351 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002352 LOGINFO("mkfs.f2fs binary not found, using rm -rf to wipe.\n");
Dees_Troye5017042013-08-29 16:38:55 +00002353 return Wipe_RMRF();
2354 }
2355 return false;
2356}
2357
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002358bool TWPartition::Wipe_NTFS() {
2359 string command;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002360 string Ntfsmake_Binary;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002361
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002362 if (TWFunc::Path_Exists("/sbin/mkntfs"))
2363 Ntfsmake_Binary = "mkntfs";
2364 else if (TWFunc::Path_Exists("/sbin/mkfs.ntfs"))
2365 Ntfsmake_Binary = "mkfs.ntfs";
2366 else
2367 return false;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002368
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002369 if (!UnMount(true))
2370 return false;
2371
Greg Wallace4b44fef2015-12-29 15:33:24 -05002372 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)(Ntfsmake_Binary));
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002373 Find_Actual_Block_Device();
2374 command = "/sbin/" + Ntfsmake_Binary + " " + Actual_Block_Device;
2375 if (TWFunc::Exec_Cmd(command) == 0) {
2376 Recreate_AndSec_Folder();
2377 gui_msg("done=Done.");
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002378 return true;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002379 } else {
2380 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
2381 return false;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002382 }
2383 return false;
2384}
2385
Dees_Troy51a0e822012-09-05 15:24:24 -04002386bool TWPartition::Wipe_Data_Without_Wiping_Media() {
Ethan Yonker83e82572014-04-04 10:59:28 -05002387#ifdef TW_OEM_BUILD
2388 // In an OEM Build we want to do a full format
2389 return Wipe_Encryption();
2390#else
Ethan Yonker66a19492015-12-10 10:19:45 -06002391 bool ret = false;
Dees_Troy38bd7602012-09-14 13:33:53 -04002392
Dees_Troy38bd7602012-09-14 13:33:53 -04002393 if (!Mount(true))
2394 return false;
2395
Ethan Yonker74db1572015-10-28 12:44:49 -05002396 gui_msg("wiping_data=Wiping data without wiping /data/media ...");
Ethan Yonker66a19492015-12-10 10:19:45 -06002397 ret = Wipe_Data_Without_Wiping_Media_Func(Mount_Point + "/");
2398 if (ret)
2399 gui_msg("done=Done.");
2400 return ret;
2401#endif // ifdef TW_OEM_BUILD
2402}
2403
2404bool TWPartition::Wipe_Data_Without_Wiping_Media_Func(const string& parent __unused) {
2405 string dir;
Dees_Troy38bd7602012-09-14 13:33:53 -04002406
2407 DIR* d;
Ethan Yonker66a19492015-12-10 10:19:45 -06002408 d = opendir(parent.c_str());
Dees_Troy16b74352012-11-14 22:27:31 +00002409 if (d != NULL) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002410 struct dirent* de;
2411 while ((de = readdir(d)) != NULL) {
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002412 if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) continue;
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05002413
Ethan Yonker66a19492015-12-10 10:19:45 -06002414 dir = parent;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002415 dir.append(de->d_name);
Ethan Yonker3fdcda42016-11-30 12:29:37 -06002416 if (wipe_exclusions.check_skip_dirs(dir)) {
Ethan Yonker66a19492015-12-10 10:19:45 -06002417 LOGINFO("skipped '%s'\n", dir.c_str());
2418 continue;
2419 }
Dees_Troyce675462013-01-09 19:48:21 +00002420 if (de->d_type == DT_DIR) {
Ethan Yonker66a19492015-12-10 10:19:45 -06002421 dir.append("/");
2422 if (!Wipe_Data_Without_Wiping_Media_Func(dir)) {
2423 closedir(d);
2424 return false;
2425 }
2426 rmdir(dir.c_str());
bigbiff bigbiff98f1f902013-01-19 18:46:13 -05002427 } 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 +03002428 if (unlink(dir.c_str()) != 0)
Ethan Yonker79f88bd2016-12-09 14:52:12 -06002429 LOGINFO("Unable to unlink '%s': %s\n", dir.c_str(), strerror(errno));
Dees_Troyce675462013-01-09 19:48:21 +00002430 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002431 }
2432 closedir(d);
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05002433
Dees_Troy16b74352012-11-14 22:27:31 +00002434 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -04002435 }
Ethan Yonker74db1572015-10-28 12:44:49 -05002436 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy16b74352012-11-14 22:27:31 +00002437 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04002438}
2439
dianlujitao4879b372018-12-03 18:45:47 +08002440void TWPartition::Wipe_Crypto_Key() {
2441 Find_Actual_Block_Device();
2442 if (Crypto_Key_Location.empty())
2443 return;
2444 else if (Crypto_Key_Location == "footer") {
2445 int fd = open(Actual_Block_Device.c_str(), O_RDWR);
2446 if (fd < 0) {
2447 gui_print_color("warning", "Unable to open '%s' to wipe crypto key\n", Actual_Block_Device.c_str());
2448 return;
2449 }
2450
2451 unsigned int block_count;
2452 if ((ioctl(fd, BLKGETSIZE, &block_count)) == -1) {
2453 gui_print_color("warning", "Unable to get block size for wiping crypto footer.\n");
2454 } else {
2455 int newlen = Length < 0 ? -Length : CRYPT_FOOTER_OFFSET;
2456 off64_t offset = ((off64_t)block_count * 512) - newlen;
2457 if (lseek64(fd, offset, SEEK_SET) == -1) {
2458 gui_print_color("warning", "Unable to lseek64 for wiping crypto footer.\n");
2459 } else {
2460 void* buffer = malloc(newlen);
2461 if (!buffer) {
2462 gui_print_color("warning", "Failed to malloc for wiping crypto footer.\n");
2463 } else {
2464 memset(buffer, 0, newlen);
2465 int ret = write(fd, buffer, newlen);
2466 if (ret != newlen) {
2467 gui_print_color("warning", "Failed to wipe crypto footer.\n");
2468 } else {
2469 LOGINFO("Successfully wiped crypto footer.\n");
2470 }
2471 free(buffer);
2472 }
2473 }
2474 }
2475 close(fd);
2476 } else {
2477 if (TWFunc::IOCTL_Get_Block_Size(Crypto_Key_Location.c_str()) >= 16384LLU) {
2478 string Command = "dd of='" + Crypto_Key_Location + "' if=/dev/zero bs=16384 count=1";
2479 TWFunc::Exec_Cmd(Command);
2480 } else {
2481 LOGINFO("Crypto key location reports size < 16K so not wiping crypto footer.\n");
2482 }
2483 }
2484}
2485
bigbiffce8f83c2015-12-12 18:30:21 -05002486bool TWPartition::Backup_Tar(PartitionSettings *part_settings, pid_t *tar_fork_pid) {
Ethan Yonker472f5062016-02-25 13:47:30 -06002487 string Full_FileName;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002488 twrpTar tar;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05002489
Dees_Troy43d8b002012-09-17 16:00:01 -04002490 if (!Mount(true))
2491 return false;
2492
Ian Macdonalda3b25a32019-06-16 22:39:14 +02002493 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Backup_Display_Name, gui_parse_text("{@backing}"));
Ethan Yonker74db1572015-10-28 12:44:49 -05002494 gui_msg(Msg("backing_up=Backing up {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002495
Ethan Yonker472f5062016-02-25 13:47:30 -06002496 DataManager::GetValue(TW_USE_COMPRESSION_VAR, tar.use_compression);
Matt Mowerbb81e5d2014-03-20 18:05:41 -05002497
Dees_Troy83bd4832013-05-04 12:39:56 +00002498#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
Ethan Yonker472f5062016-02-25 13:47:30 -06002499 if (Can_Encrypt_Backup) {
2500 DataManager::GetValue("tw_encrypt_backup", tar.use_encryption);
2501 if (tar.use_encryption) {
2502 if (Use_Userdata_Encryption)
2503 tar.userdata_encryption = tar.use_encryption;
2504 string Password;
2505 DataManager::GetValue("tw_backup_password", Password);
2506 tar.setpassword(Password);
2507 } else {
2508 tar.use_encryption = 0;
2509 }
Dees_Troy83bd4832013-05-04 12:39:56 +00002510 }
2511#endif
Dees_Troy43d8b002012-09-17 16:00:01 -04002512
Ethan Yonker472f5062016-02-25 13:47:30 -06002513 Backup_FileName = Backup_Name + "." + Current_File_System + ".win";
Ethan Yonkere080c1f2016-09-19 13:50:25 -05002514 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
Ethan Yonker01f4e032017-02-03 15:30:52 -06002515 if (Has_Data_Media)
2516 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 -05002517 tar.part_settings = part_settings;
Ethan Yonker3fdcda42016-11-30 12:29:37 -06002518 tar.backup_exclusions = &backup_exclusions;
Dees Troye0a433a2013-12-02 04:10:37 +00002519 tar.setdir(Backup_Path);
2520 tar.setfn(Full_FileName);
2521 tar.setsize(Backup_Size);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002522 tar.partition_name = Backup_Name;
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002523 tar.backup_folder = part_settings->Backup_Folder;
bigbiffce8f83c2015-12-12 18:30:21 -05002524 if (tar.createTarFork(tar_fork_pid) != 0)
Dees Troye0a433a2013-12-02 04:10:37 +00002525 return false;
Dees_Troy43d8b002012-09-17 16:00:01 -04002526 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002527}
2528
bigbiffce8f83c2015-12-12 18:30:21 -05002529bool TWPartition::Backup_Image(PartitionSettings *part_settings) {
2530 string Full_FileName, adb_file_name;
igoriok87e3d932013-01-31 21:03:53 +02002531
Ethan Yonker74db1572015-10-28 12:44:49 -05002532 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, gui_parse_text("{@backing}"));
2533 gui_msg(Msg("backing_up=Backing up {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002534
Ethan Yonker472f5062016-02-25 13:47:30 -06002535 Backup_FileName = Backup_Name + "." + Current_File_System + ".win";
Dees_Troy43d8b002012-09-17 16:00:01 -04002536
bigbiffce8f83c2015-12-12 18:30:21 -05002537 if (part_settings->adbbackup) {
2538 Full_FileName = TW_ADB_BACKUP;
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002539 adb_file_name = part_settings->Backup_Folder + "/" + Backup_FileName;
bigbiffce8f83c2015-12-12 18:30:21 -05002540 }
2541 else
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002542 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
bigbiffce8f83c2015-12-12 18:30:21 -05002543
2544 part_settings->total_restore_size = Backup_Size;
2545
2546 if (part_settings->adbbackup) {
2547 if (!twadbbu::Write_TWIMG(adb_file_name, Backup_Size))
2548 return false;
2549 }
2550
2551 if (!Raw_Read_Write(part_settings))
Ethan Yonker472f5062016-02-25 13:47:30 -06002552 return false;
2553
bigbiffce8f83c2015-12-12 18:30:21 -05002554 if (part_settings->adbbackup) {
2555 if (!twadbbu::Write_TWEOF())
2556 return false;
Dees_Troy7c2dec82012-09-26 09:49:14 -04002557 }
Dees_Troy43d8b002012-09-17 16:00:01 -04002558 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002559}
2560
bigbiffce8f83c2015-12-12 18:30:21 -05002561bool TWPartition::Raw_Read_Write(PartitionSettings *part_settings) {
2562 unsigned long long RW_Block_Size, Remain = Backup_Size;
2563 int src_fd = -1, dest_fd = -1;
2564 ssize_t bs;
Ethan Yonker472f5062016-02-25 13:47:30 -06002565 bool ret = false;
2566 void* buffer = NULL;
2567 unsigned long long backedup_size = 0;
bigbiffce8f83c2015-12-12 18:30:21 -05002568 string srcfn, destfn;
Ethan Yonker472f5062016-02-25 13:47:30 -06002569
bigbiffce8f83c2015-12-12 18:30:21 -05002570 if (part_settings->PM_Method == PM_BACKUP) {
2571 srcfn = Actual_Block_Device;
2572 if (part_settings->adbbackup)
2573 destfn = TW_ADB_BACKUP;
bigbiff bigbiff38b83c12017-12-28 19:58:52 -05002574 else {
Ethan Yonkere080c1f2016-09-19 13:50:25 -05002575 destfn = part_settings->Backup_Folder + "/" + Backup_FileName;
bigbiff bigbiff38b83c12017-12-28 19:58:52 -05002576 }
bigbiffce8f83c2015-12-12 18:30:21 -05002577 }
2578 else {
2579 destfn = Actual_Block_Device;
2580 if (part_settings->adbbackup) {
2581 srcfn = TW_ADB_RESTORE;
2582 } else {
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002583 srcfn = part_settings->Backup_Folder + "/" + Backup_FileName;
bigbiffce8f83c2015-12-12 18:30:21 -05002584 Remain = TWFunc::Get_File_Size(srcfn);
2585 }
2586 }
Ethan Yonker472f5062016-02-25 13:47:30 -06002587
bigbiffce8f83c2015-12-12 18:30:21 -05002588 src_fd = open(srcfn.c_str(), O_RDONLY | O_LARGEFILE);
Ethan Yonker472f5062016-02-25 13:47:30 -06002589 if (src_fd < 0) {
bigbiffce8f83c2015-12-12 18:30:21 -05002590 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(srcfn.c_str())(strerror(errno)));
Ethan Yonker472f5062016-02-25 13:47:30 -06002591 return false;
2592 }
bigbiffce8f83c2015-12-12 18:30:21 -05002593
2594 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 -06002595 if (dest_fd < 0) {
bigbiffce8f83c2015-12-12 18:30:21 -05002596 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(destfn.c_str())(strerror(errno)));
Ethan Yonker472f5062016-02-25 13:47:30 -06002597 goto exit;
2598 }
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -04002599
bigbiffce8f83c2015-12-12 18:30:21 -05002600 LOGINFO("Reading '%s', writing '%s'\n", srcfn.c_str(), destfn.c_str());
2601
2602 if (part_settings->adbbackup) {
2603 RW_Block_Size = MAX_ADB_READ;
2604 bs = MAX_ADB_READ;
2605 }
2606 else {
2607 RW_Block_Size = 1048576LLU; // 1MB
2608 bs = (ssize_t)(RW_Block_Size);
2609 }
2610
Ethan Yonker472f5062016-02-25 13:47:30 -06002611 buffer = malloc((size_t)bs);
2612 if (!buffer) {
2613 LOGINFO("Raw_Read_Write failed to malloc\n");
2614 goto exit;
2615 }
bigbiffce8f83c2015-12-12 18:30:21 -05002616
2617 if (part_settings->progress)
2618 part_settings->progress->SetPartitionSize(part_settings->total_restore_size);
2619
Ethan Yonker472f5062016-02-25 13:47:30 -06002620 while (Remain > 0) {
2621 if (Remain < RW_Block_Size)
bigbiffce8f83c2015-12-12 18:30:21 -05002622 bs = (ssize_t)(Remain);
Matt Mowera8a89d12016-12-30 18:10:37 -06002623 if (read(src_fd, buffer, bs) != bs) {
Ethan Yonker472f5062016-02-25 13:47:30 -06002624 LOGINFO("Error reading source fd (%s)\n", strerror(errno));
2625 goto exit;
2626 }
2627 if (write(dest_fd, buffer, bs) != bs) {
2628 LOGINFO("Error writing destination fd (%s)\n", strerror(errno));
2629 goto exit;
2630 }
2631 backedup_size += (unsigned long long)(bs);
Matt Mower029a82d2017-01-08 13:12:38 -06002632 Remain -= (unsigned long long)(bs);
bigbiffce8f83c2015-12-12 18:30:21 -05002633 if (part_settings->progress)
2634 part_settings->progress->UpdateSize(backedup_size);
Ethan Yonker472f5062016-02-25 13:47:30 -06002635 if (PartitionManager.Check_Backup_Cancel() != 0)
2636 goto exit;
2637 }
bigbiffce8f83c2015-12-12 18:30:21 -05002638 if (part_settings->progress)
2639 part_settings->progress->UpdateDisplayDetails(true);
Ethan Yonker472f5062016-02-25 13:47:30 -06002640 fsync(dest_fd);
nailyka083dc62016-11-11 21:41:28 +01002641
2642 if (!part_settings->adbbackup && part_settings->PM_Method == PM_BACKUP) {
2643 tw_set_default_metadata(destfn.c_str());
2644 LOGINFO("Restored default metadata for %s\n", destfn.c_str());
2645 }
2646
Ethan Yonker472f5062016-02-25 13:47:30 -06002647 ret = true;
2648exit:
2649 if (src_fd >= 0)
2650 close(src_fd);
2651 if (dest_fd >= 0)
2652 close(dest_fd);
2653 if (buffer)
2654 free(buffer);
2655 return ret;
2656}
2657
bigbiffce8f83c2015-12-12 18:30:21 -05002658bool TWPartition::Backup_Dump_Image(PartitionSettings *part_settings) {
Vojtech Bocek05534202013-09-11 08:11:56 +02002659 string Full_FileName, Command;
Dees_Troy43d8b002012-09-17 16:00:01 -04002660
Ethan Yonker74db1572015-10-28 12:44:49 -05002661 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, gui_parse_text("{@backing}"));
2662 gui_msg(Msg("backing_up=Backing up {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002663
bigbiffce8f83c2015-12-12 18:30:21 -05002664 if (part_settings->progress)
2665 part_settings->progress->SetPartitionSize(Backup_Size);
Dees_Troy43d8b002012-09-17 16:00:01 -04002666
Ethan Yonker472f5062016-02-25 13:47:30 -06002667 Backup_FileName = Backup_Name + "." + Current_File_System + ".win";
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002668 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04002669
2670 Command = "dump_image " + MTD_Name + " '" + Full_FileName + "'";
bigbiffce8f83c2015-12-12 18:30:21 -05002671
Dees_Troy2673cec2013-04-02 20:22:16 +00002672 LOGINFO("Backup command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02002673 TWFunc::Exec_Cmd(Command);
Ethan Yonker4b94cfd2014-12-11 10:00:45 -06002674 tw_set_default_metadata(Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04002675 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
2676 // 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 -06002677 gui_msg(Msg(msg::kError, "backup_size=Backup file size for '{1}' is 0 bytes.")(Full_FileName));
Dees_Troy7c2dec82012-09-26 09:49:14 -04002678 return false;
2679 }
bigbiffce8f83c2015-12-12 18:30:21 -05002680 if (part_settings->progress)
2681 part_settings->progress->UpdateSize(Backup_Size);
2682
Dees_Troy43d8b002012-09-17 16:00:01 -04002683 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002684}
2685
bigbiffce8f83c2015-12-12 18:30:21 -05002686unsigned long long TWPartition::Get_Restore_Size(PartitionSettings *part_settings) {
2687 if (!part_settings->adbbackup) {
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002688 InfoManager restore_info(part_settings->Backup_Folder + "/" + Backup_Name + ".info");
bigbiffce8f83c2015-12-12 18:30:21 -05002689 if (restore_info.LoadValues() == 0) {
2690 if (restore_info.GetValue("backup_size", Restore_Size) == 0) {
2691 LOGINFO("Read info file, restore size is %llu\n", Restore_Size);
2692 return Restore_Size;
2693 }
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002694 }
2695 }
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002696
Matt Mower029a82d2017-01-08 13:12:38 -06002697 string Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
2698 string Restore_File_System = Get_Restore_File_System(part_settings);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002699
2700 if (Is_Image(Restore_File_System)) {
2701 Restore_Size = TWFunc::Get_File_Size(Full_FileName);
2702 return Restore_Size;
2703 }
2704
2705 twrpTar tar;
2706 tar.setdir(Backup_Path);
2707 tar.setfn(Full_FileName);
bigbiffce8f83c2015-12-12 18:30:21 -05002708 tar.backup_name = Full_FileName;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002709#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
2710 string Password;
2711 DataManager::GetValue("tw_restore_password", Password);
2712 if (!Password.empty())
2713 tar.setpassword(Password);
2714#endif
2715 tar.partition_name = Backup_Name;
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002716 tar.backup_folder = part_settings->Backup_Folder;
bigbiffce8f83c2015-12-12 18:30:21 -05002717 tar.part_settings = part_settings;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002718 Restore_Size = tar.get_size();
2719 return Restore_Size;
2720}
2721
bigbiffce8f83c2015-12-12 18:30:21 -05002722bool TWPartition::Restore_Tar(PartitionSettings *part_settings) {
Ethan Yonker472f5062016-02-25 13:47:30 -06002723 string Full_FileName;
Dees Troy4159aed2014-02-28 17:24:43 +00002724 bool ret = false;
bigbiffce8f83c2015-12-12 18:30:21 -05002725 string Restore_File_System = Get_Restore_File_System(part_settings);
Dees_Troy43d8b002012-09-17 16:00:01 -04002726
Dees_Troye58d5262012-09-21 12:27:57 -04002727 if (Has_Android_Secure) {
Dees_Troye58d5262012-09-21 12:27:57 -04002728 if (!Wipe_AndSec())
2729 return false;
Gary Peck43acadf2012-11-21 21:19:01 -08002730 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002731 gui_msg(Msg("wiping=Wiping {1}")(Backup_Display_Name));
Ethan Yonker5eac2222014-06-11 12:22:55 -05002732 if (Has_Data_Media && Mount_Point == "/data" && Restore_File_System != Current_File_System) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002733 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 -05002734 if (!Wipe_Data_Without_Wiping_Media())
2735 return false;
2736 } else {
2737 if (!Wipe(Restore_File_System))
2738 return false;
2739 }
Dees_Troye58d5262012-09-21 12:27:57 -04002740 }
Matt Mower3c366972015-12-25 19:28:31 -06002741 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Backup_Display_Name, gui_parse_text("{@restoring_hdr}"));
Ethan Yonker74db1572015-10-28 12:44:49 -05002742 gui_msg(Msg("restoring=Restoring {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002743
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05002744 // Remount as read/write as needed so we can restore the backup
2745 if (!ReMount_RW(true))
Dees_Troy43d8b002012-09-17 16:00:01 -04002746 return false;
2747
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002748 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
Ethan Yonker87af5632014-02-10 11:56:35 -06002749 twrpTar tar;
bigbiffce8f83c2015-12-12 18:30:21 -05002750 tar.part_settings = part_settings;
Ethan Yonker87af5632014-02-10 11:56:35 -06002751 tar.setdir(Backup_Path);
2752 tar.setfn(Full_FileName);
2753 tar.backup_name = Backup_Name;
2754#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
2755 string Password;
2756 DataManager::GetValue("tw_restore_password", Password);
2757 if (!Password.empty())
2758 tar.setpassword(Password);
2759#endif
bigbiffce8f83c2015-12-12 18:30:21 -05002760 part_settings->progress->SetPartitionSize(Get_Restore_Size(part_settings));
2761 if (tar.extractTarFork() != 0)
Dees Troy4159aed2014-02-28 17:24:43 +00002762 ret = false;
2763 else
2764 ret = true;
2765#ifdef HAVE_CAPABILITIES
2766 // Restore capabilities to the run-as binary
Captain Throwback9d6feb52018-07-27 10:05:24 -04002767 if (Mount_Point == PartitionManager.Get_Android_Root_Path() && Mount(true) && TWFunc::Path_Exists("/system/bin/run-as")) {
Dees Troy4159aed2014-02-28 17:24:43 +00002768 struct vfs_cap_data cap_data;
2769 uint64_t capabilities = (1 << CAP_SETUID) | (1 << CAP_SETGID);
2770
2771 memset(&cap_data, 0, sizeof(cap_data));
2772 cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE;
2773 cap_data.data[0].permitted = (uint32_t) (capabilities & 0xffffffff);
2774 cap_data.data[0].inheritable = 0;
2775 cap_data.data[1].permitted = (uint32_t) (capabilities >> 32);
2776 cap_data.data[1].inheritable = 0;
2777 if (setxattr("/system/bin/run-as", XATTR_NAME_CAPS, &cap_data, sizeof(cap_data), 0) < 0) {
2778 LOGINFO("Failed to reset capabilities of /system/bin/run-as binary.\n");
2779 } else {
2780 LOGINFO("Reset capabilities of /system/bin/run-as binary successful.\n");
2781 }
2782 }
2783#endif
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05002784 if (Mount_Read_Only || Mount_Flags & MS_RDONLY)
2785 // Remount as read only when restoration is complete
2786 ReMount(true);
2787
Dees Troy4159aed2014-02-28 17:24:43 +00002788 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04002789}
2790
bigbiffce8f83c2015-12-12 18:30:21 -05002791bool TWPartition::Restore_Image(PartitionSettings *part_settings) {
Ethan Yonker96af84a2015-01-05 14:58:36 -06002792 string Full_FileName;
bigbiffce8f83c2015-12-12 18:30:21 -05002793 string Restore_File_System = Get_Restore_File_System(part_settings);
Dees_Troy43d8b002012-09-17 16:00:01 -04002794
Matt Mower3c366972015-12-25 19:28:31 -06002795 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Backup_Display_Name, gui_parse_text("{@restoring_hdr}"));
Ethan Yonker74db1572015-10-28 12:44:49 -05002796 gui_msg(Msg("restoring=Restoring {1}...")(Backup_Display_Name));
bigbiffce8f83c2015-12-12 18:30:21 -05002797
2798 if (part_settings->adbbackup)
2799 Full_FileName = TW_ADB_RESTORE;
2800 else
Ethan Yonkere080c1f2016-09-19 13:50:25 -05002801 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
Gary Peck15e623d2012-11-21 21:07:58 -08002802
Ethan Yonker96af84a2015-01-05 14:58:36 -06002803 if (Restore_File_System == "emmc") {
bigbiffce8f83c2015-12-12 18:30:21 -05002804 if (!part_settings->adbbackup)
2805 part_settings->total_restore_size = (uint64_t)(TWFunc::Get_File_Size(Full_FileName));
2806 if (!Raw_Read_Write(part_settings))
Ethan Yonker96af84a2015-01-05 14:58:36 -06002807 return false;
2808 } else if (Restore_File_System == "mtd" || Restore_File_System == "bml") {
bigbiffce8f83c2015-12-12 18:30:21 -05002809 if (!Flash_Image_FI(Full_FileName, part_settings->progress))
2810 return false;
2811 }
2812
2813 if (part_settings->adbbackup) {
2814 if (!twadbbu::Write_TWEOF())
Ethan Yonker96af84a2015-01-05 14:58:36 -06002815 return false;
Gary Peck15e623d2012-11-21 21:07:58 -08002816 }
Dees_Troy43d8b002012-09-17 16:00:01 -04002817 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002818}
Dees_Troy5bf43922012-09-07 16:07:55 -04002819
2820bool TWPartition::Update_Size(bool Display_Error) {
Dees_Troy0550cfb2012-10-13 11:56:13 -04002821 bool ret = false, Was_Already_Mounted = false;
Dees_Troy51127312012-09-08 13:08:49 -04002822
Ethan Yonker1b190162016-12-05 15:25:19 -06002823 Find_Actual_Block_Device();
2824
2825 if (!Can_Be_Mounted && !Is_Encrypted) {
2826 if (TWFunc::Path_Exists(Actual_Block_Device) && Find_Partition_Size()) {
2827 Used = Size;
2828 Backup_Size = Size;
2829 return true;
2830 }
Dees_Troy5bf43922012-09-07 16:07:55 -04002831 return false;
Ethan Yonker1b190162016-12-05 15:25:19 -06002832 }
Dees_Troy5bf43922012-09-07 16:07:55 -04002833
Dees_Troy0550cfb2012-10-13 11:56:13 -04002834 Was_Already_Mounted = Is_Mounted();
Dees_Troy38bd7602012-09-14 13:33:53 -04002835 if (Removable || Is_Encrypted) {
2836 if (!Mount(false))
2837 return true;
2838 } else if (!Mount(Display_Error))
Dees_Troy5bf43922012-09-07 16:07:55 -04002839 return false;
Dees_Troy51127312012-09-08 13:08:49 -04002840
2841 ret = Get_Size_Via_statfs(Display_Error);
Dees_Troy0550cfb2012-10-13 11:56:13 -04002842 if (!ret || Size == 0) {
2843 if (!Get_Size_Via_df(Display_Error)) {
2844 if (!Was_Already_Mounted)
2845 UnMount(false);
Dees_Troy51127312012-09-08 13:08:49 -04002846 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04002847 }
2848 }
Dees_Troy51127312012-09-08 13:08:49 -04002849
Dees_Troy5bf43922012-09-07 16:07:55 -04002850 if (Has_Data_Media) {
2851 if (Mount(Display_Error)) {
Ethan Yonker3fdcda42016-11-30 12:29:37 -06002852 Used = backup_exclusions.Get_Folder_Size(Mount_Point);
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002853 Backup_Size = Used;
2854 int bak = (int)(Used / 1048576LLU);
Dees_Troy51127312012-09-08 13:08:49 -04002855 int fre = (int)(Free / 1048576LLU);
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002856 LOGINFO("Data backup size is %iMB, free: %iMB.\n", bak, fre);
Dees_Troy0550cfb2012-10-13 11:56:13 -04002857 } else {
2858 if (!Was_Already_Mounted)
2859 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04002860 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04002861 }
Dees_Troye58d5262012-09-21 12:27:57 -04002862 } else if (Has_Android_Secure) {
2863 if (Mount(Display_Error))
Ethan Yonker3fdcda42016-11-30 12:29:37 -06002864 Backup_Size = backup_exclusions.Get_Folder_Size(Backup_Path);
Dees_Troy0550cfb2012-10-13 11:56:13 -04002865 else {
2866 if (!Was_Already_Mounted)
2867 UnMount(false);
Dees_Troye58d5262012-09-21 12:27:57 -04002868 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04002869 }
Dees_Troy5bf43922012-09-07 16:07:55 -04002870 }
Dees_Troy0550cfb2012-10-13 11:56:13 -04002871 if (!Was_Already_Mounted)
2872 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04002873 return true;
Dees_Troy51127312012-09-08 13:08:49 -04002874}
Dees_Troy38bd7602012-09-14 13:33:53 -04002875
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06002876bool TWPartition::Find_Wildcard_Block_Devices(const string& Device) {
2877 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
2878 string Path = TWFunc::Get_Path(Device);
2879 string Dev = TWFunc::Get_Filename(Device);
2880 size_t wildcard_index = Dev.find("*");
2881 if (wildcard_index != string::npos)
2882 Dev = Dev.substr(0, wildcard_index);
2883 wildcard_index = Dev.size();
2884 DIR* d = opendir(Path.c_str());
2885 if (d == NULL) {
2886 LOGINFO("Error opening '%s': %s\n", Path.c_str(), strerror(errno));
2887 return false;
2888 }
2889 struct dirent* de;
2890 while ((de = readdir(d)) != NULL) {
2891 if (de->d_type != DT_BLK || strlen(de->d_name) <= wildcard_index || strncmp(de->d_name, Dev.c_str(), wildcard_index) != 0)
2892 continue;
2893
2894 string item = Path + "/";
2895 item.append(de->d_name);
2896 if (PartitionManager.Find_Partition_By_Block_Device(item))
2897 continue;
2898 TWPartition *part = new TWPartition;
2899 char buffer[MAX_FSTAB_LINE_LENGTH];
2900 sprintf(buffer, "%s %s-%i auto defaults defaults", item.c_str(), Mount_Point.c_str(), ++mount_point_index);
2901 part->Process_Fstab_Line(buffer, false, NULL);
2902 char display[MAX_FSTAB_LINE_LENGTH];
2903 sprintf(display, "%s %i", Storage_Name.c_str(), mount_point_index);
2904 part->Storage_Name = display;
2905 part->Display_Name = display;
2906 part->Primary_Block_Device = item;
2907 part->Wildcard_Block_Device = false;
2908 part->Is_SubPartition = true;
2909 part->SubPartition_Of = Mount_Point;
2910 part->Is_Storage = Is_Storage;
2911 part->Can_Be_Mounted = true;
2912 part->Removable = true;
2913 part->Can_Be_Wiped = Can_Be_Wiped;
2914 part->Wipe_Available_in_GUI = Wipe_Available_in_GUI;
2915 part->Find_Actual_Block_Device();
2916 part->Update_Size(false);
2917 Has_SubPartition = true;
2918 PartitionManager.Output_Partition(part);
2919 PartitionManager.Add_Partition(part);
2920 }
2921 closedir(d);
2922 return (mount_point_index > 0);
2923}
2924
Dees_Troy38bd7602012-09-14 13:33:53 -04002925void TWPartition::Find_Actual_Block_Device(void) {
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06002926 if (!Sysfs_Entry.empty() && Primary_Block_Device.empty() && Decrypted_Block_Device.empty()) {
2927 /* Sysfs_Entry.empty() indicates if this is a sysfs entry that begins with /device/
2928 * If we have a syfs entry then we are looking for this device from a uevent add.
2929 * The uevent add will set the primary block device based on the data we receive from
2930 * after checking for adopted storage. If the device ends up being adopted, then the
2931 * decrypted block device will be set instead of the primary block device. */
2932 Is_Present = false;
2933 return;
2934 }
2935 if (Wildcard_Block_Device && !Is_Adopted_Storage) {
2936 Is_Present = false;
2937 Actual_Block_Device = "";
2938 Can_Be_Mounted = false;
2939 if (!Find_Wildcard_Block_Devices(Primary_Block_Device)) {
2940 string Dev = Primary_Block_Device.substr(0, Primary_Block_Device.find("*"));
2941 if (TWFunc::Path_Exists(Dev)) {
2942 Is_Present = true;
2943 Can_Be_Mounted = true;
2944 Actual_Block_Device = Dev;
2945 }
2946 }
2947 return;
2948 } else if (Is_Decrypted && !Decrypted_Block_Device.empty()) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002949 Actual_Block_Device = Decrypted_Block_Device;
Ethan Yonker1b190162016-12-05 15:25:19 -06002950 if (TWFunc::Path_Exists(Decrypted_Block_Device)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002951 Is_Present = true;
Ethan Yonker1b190162016-12-05 15:25:19 -06002952 return;
2953 }
2954 } else if (SlotSelect && TWFunc::Path_Exists(Primary_Block_Device + PartitionManager.Get_Active_Slot_Suffix())) {
2955 Actual_Block_Device = Primary_Block_Device + PartitionManager.Get_Active_Slot_Suffix();
2956 unlink(Primary_Block_Device.c_str());
2957 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
2958 Is_Present = true;
2959 return;
Dees_Troy43d8b002012-09-17 16:00:01 -04002960 } else if (TWFunc::Path_Exists(Primary_Block_Device)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002961 Is_Present = true;
2962 Actual_Block_Device = Primary_Block_Device;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002963 return;
2964 }
Ethan Yonker1b190162016-12-05 15:25:19 -06002965 if (!Alternate_Block_Device.empty() && TWFunc::Path_Exists(Alternate_Block_Device)) {
Dees_Troy3f04d032012-10-07 18:20:09 -04002966 Actual_Block_Device = Alternate_Block_Device;
Dees_Troy38bd7602012-09-14 13:33:53 -04002967 Is_Present = true;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002968 } else {
Dees_Troy38bd7602012-09-14 13:33:53 -04002969 Is_Present = false;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002970 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002971}
2972
2973void TWPartition::Recreate_Media_Folder(void) {
2974 string Command;
Ethan Yonker66a19492015-12-10 10:19:45 -06002975 string Media_Path = Mount_Point + "/media";
Dees_Troy38bd7602012-09-14 13:33:53 -04002976
Ethan Yonkerbd7492d2016-12-07 13:55:01 -06002977 if (Is_FBE) {
2978 LOGINFO("Not recreating media folder on FBE\n");
2979 return;
2980 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002981 if (!Mount(true)) {
Ethan Yonker66a19492015-12-10 10:19:45 -06002982 gui_msg(Msg(msg::kError, "recreate_folder_err=Unable to recreate {1} folder.")(Media_Path));
2983 } else if (!TWFunc::Path_Exists(Media_Path)) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002984 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Ethan Yonker66a19492015-12-10 10:19:45 -06002985 LOGINFO("Recreating %s folder.\n", Media_Path.c_str());
2986 mkdir(Media_Path.c_str(), 0770);
xiaolu9416f4f2015-06-04 08:22:23 +08002987 string Internal_path = DataManager::GetStrValue("tw_internal_path");
2988 if (!Internal_path.empty()) {
2989 LOGINFO("Recreating %s folder.\n", Internal_path.c_str());
2990 mkdir(Internal_path.c_str(), 0770);
2991 }
2992#ifdef TW_INTERNAL_STORAGE_PATH
2993 mkdir(EXPAND(TW_INTERNAL_STORAGE_PATH), 0770);
2994#endif
Matt Mower87413642017-01-17 21:14:46 -06002995
thata3d31fb2014-12-21 22:27:40 +01002996 // Afterwards, we will try to set the
2997 // default metadata that we were hopefully able to get during
2998 // early boot.
Ethan Yonker66a19492015-12-10 10:19:45 -06002999 tw_set_default_metadata(Media_Path.c_str());
xiaolu9416f4f2015-06-04 08:22:23 +08003000 if (!Internal_path.empty())
3001 tw_set_default_metadata(Internal_path.c_str());
Matt Mower87413642017-01-17 21:14:46 -06003002
Ethan Yonker5eac2222014-06-11 12:22:55 -05003003 // Toggle mount to ensure that "internal sdcard" gets mounted
bigbiff bigbiff9c754052013-01-09 09:09:08 -05003004 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Ethan Yonker5eac2222014-06-11 12:22:55 -05003005 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Dees_Troy38bd7602012-09-14 13:33:53 -04003006 }
Dees_Troy43d8b002012-09-17 16:00:01 -04003007}
Dees_Troye58d5262012-09-21 12:27:57 -04003008
3009void TWPartition::Recreate_AndSec_Folder(void) {
Dees_Troye58d5262012-09-21 12:27:57 -04003010 if (!Has_Android_Secure)
3011 return;
Dees_Troy2673cec2013-04-02 20:22:16 +00003012 LOGINFO("Creating %s: %s\n", Backup_Display_Name.c_str(), Symlink_Path.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -04003013 if (!Mount(true)) {
Ethan Yonker74db1572015-10-28 12:44:49 -05003014 gui_msg(Msg(msg::kError, "recreate_folder_err=Unable to recreate {1} folder.")(Backup_Name));
Dees_Troye58d5262012-09-21 12:27:57 -04003015 } else if (!TWFunc::Path_Exists(Symlink_Path)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00003016 LOGINFO("Recreating %s folder.\n", Backup_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05003017 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05003018 mkdir(Symlink_Path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
bigbiff bigbiff9c754052013-01-09 09:09:08 -05003019 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04003020 }
3021}
bigbiff7cb4c332014-11-26 20:36:07 -05003022
3023uint64_t TWPartition::Get_Max_FileSize() {
3024 uint64_t maxFileSize = 0;
3025 const uint64_t constGB = (uint64_t) 1024 * 1024 * 1024;
3026 const uint64_t constTB = (uint64_t) constGB * 1024;
3027 const uint64_t constPB = (uint64_t) constTB * 1024;
bigbiff0c532032014-12-21 13:41:26 -05003028 if (Current_File_System == "ext4")
3029 maxFileSize = 16 * constTB; //16 TB
3030 else if (Current_File_System == "vfat")
3031 maxFileSize = 4 * constGB; //4 GB
3032 else if (Current_File_System == "ntfs")
3033 maxFileSize = 256 * constTB; //256 TB
3034 else if (Current_File_System == "exfat")
3035 maxFileSize = 16 * constPB; //16 PB
3036 else if (Current_File_System == "ext3")
3037 maxFileSize = 2 * constTB; //2 TB
3038 else if (Current_File_System == "f2fs")
3039 maxFileSize = 3.94 * constTB; //3.94 TB
bigbiff7cb4c332014-11-26 20:36:07 -05003040 else
3041 maxFileSize = 100000000L;
3042 return maxFileSize - 1;
3043}
3044
bigbiffce8f83c2015-12-12 18:30:21 -05003045bool TWPartition::Flash_Image(PartitionSettings *part_settings) {
3046 string Restore_File_System, full_filename;
Ethan Yonker96af84a2015-01-05 14:58:36 -06003047
Ethan Yonkere080c1f2016-09-19 13:50:25 -05003048 full_filename = part_settings->Backup_Folder + "/" + Backup_FileName;
Ethan Yonker96af84a2015-01-05 14:58:36 -06003049
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05003050 LOGINFO("Image filename is: %s\n", Backup_FileName.c_str());
bigbiffce8f83c2015-12-12 18:30:21 -05003051
3052 if (Backup_Method == BM_FILES) {
Ethan Yonker96af84a2015-01-05 14:58:36 -06003053 LOGERR("Cannot flash images to file systems\n");
3054 return false;
3055 } else if (!Can_Flash_Img) {
3056 LOGERR("Cannot flash images to partitions %s\n", Display_Name.c_str());
3057 return false;
3058 } else {
3059 if (!Find_Partition_Size()) {
3060 LOGERR("Unable to find partition size for '%s'\n", Mount_Point.c_str());
3061 return false;
3062 }
bigbiffce8f83c2015-12-12 18:30:21 -05003063 unsigned long long image_size = TWFunc::Get_File_Size(full_filename);
Ethan Yonker96af84a2015-01-05 14:58:36 -06003064 if (image_size > Size) {
Ethan Yonker74db1572015-10-28 12:44:49 -05003065 LOGINFO("Size (%llu bytes) of image '%s' is larger than target device '%s' (%llu bytes)\n",
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05003066 image_size, Backup_FileName.c_str(), Actual_Block_Device.c_str(), Size);
Ethan Yonker74db1572015-10-28 12:44:49 -05003067 gui_err("img_size_err=Size of image is larger than target device");
Ethan Yonker96af84a2015-01-05 14:58:36 -06003068 return false;
3069 }
bigbiffce8f83c2015-12-12 18:30:21 -05003070 if (Backup_Method == BM_DD) {
3071 if (!part_settings->adbbackup) {
3072 if (Is_Sparse_Image(full_filename)) {
3073 return Flash_Sparse_Image(full_filename);
3074 }
Ethan Yonker472f5062016-02-25 13:47:30 -06003075 }
bigbiffce8f83c2015-12-12 18:30:21 -05003076 return Raw_Read_Write(part_settings);
3077 } else if (Backup_Method == BM_FLASH_UTILS) {
3078 return Flash_Image_FI(full_filename, NULL);
Ethan Yonker472f5062016-02-25 13:47:30 -06003079 }
Ethan Yonker96af84a2015-01-05 14:58:36 -06003080 }
3081
3082 LOGERR("Unknown flash method for '%s'\n", Mount_Point.c_str());
3083 return false;
3084}
3085
Ethan Yonker472f5062016-02-25 13:47:30 -06003086bool TWPartition::Is_Sparse_Image(const string& Filename) {
HashBanged974bb2016-01-30 14:20:09 -05003087 uint32_t magic = 0;
3088 int fd = open(Filename.c_str(), O_RDONLY);
3089 if (fd < 0) {
3090 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Filename)(strerror(errno)));
3091 return false;
3092 }
bigbiffce8f83c2015-12-12 18:30:21 -05003093
HashBanged974bb2016-01-30 14:20:09 -05003094 if (read(fd, &magic, sizeof(magic)) != sizeof(magic)) {
3095 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Filename)(strerror(errno)));
3096 close(fd);
3097 return false;
3098 }
3099 close(fd);
Ethan Yonker472f5062016-02-25 13:47:30 -06003100 if (magic == SPARSE_HEADER_MAGIC)
3101 return true;
3102 return false;
3103}
3104
3105bool TWPartition::Flash_Sparse_Image(const string& Filename) {
3106 string Command;
3107
3108 gui_msg(Msg("flashing=Flashing {1}...")(Display_Name));
3109
3110 Command = "simg2img '" + Filename + "' '" + Actual_Block_Device + "'";
Ethan Yonker96af84a2015-01-05 14:58:36 -06003111 LOGINFO("Flash command: '%s'\n", Command.c_str());
3112 TWFunc::Exec_Cmd(Command);
3113 return true;
3114}
3115
Ethan Yonker472f5062016-02-25 13:47:30 -06003116bool TWPartition::Flash_Image_FI(const string& Filename, ProgressTracking *progress) {
Ethan Yonker96af84a2015-01-05 14:58:36 -06003117 string Command;
Ethan Yonker472f5062016-02-25 13:47:30 -06003118 unsigned long long file_size;
Ethan Yonker96af84a2015-01-05 14:58:36 -06003119
Ethan Yonker74db1572015-10-28 12:44:49 -05003120 gui_msg(Msg("flashing=Flashing {1}...")(Display_Name));
Ethan Yonker472f5062016-02-25 13:47:30 -06003121 if (progress) {
3122 file_size = (unsigned long long)(TWFunc::Get_File_Size(Filename));
3123 progress->SetPartitionSize(file_size);
3124 }
Ethan Yonker96af84a2015-01-05 14:58:36 -06003125 // Sometimes flash image doesn't like to flash due to the first 2KB matching, so we erase first to ensure that it flashes
3126 Command = "erase_image " + MTD_Name;
3127 LOGINFO("Erase command: '%s'\n", Command.c_str());
3128 TWFunc::Exec_Cmd(Command);
3129 Command = "flash_image " + MTD_Name + " '" + Filename + "'";
3130 LOGINFO("Flash command: '%s'\n", Command.c_str());
3131 TWFunc::Exec_Cmd(Command);
Ethan Yonker472f5062016-02-25 13:47:30 -06003132 if (progress)
3133 progress->UpdateSize(file_size);
Ethan Yonker96af84a2015-01-05 14:58:36 -06003134 return true;
3135}
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05003136
3137void TWPartition::Change_Mount_Read_Only(bool new_value) {
3138 Mount_Read_Only = new_value;
3139}
3140
bigbiffce8f83c2015-12-12 18:30:21 -05003141bool TWPartition::Is_Read_Only() {
3142 return Mount_Read_Only;
3143}
3144
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05003145int TWPartition::Check_Lifetime_Writes() {
3146 bool original_read_only = Mount_Read_Only;
3147 int ret = 1;
3148
3149 Mount_Read_Only = true;
3150 if (Mount(false)) {
3151 Find_Actual_Block_Device();
Ethan Yonker54970bb2019-01-17 16:19:24 -06003152 string temp = Actual_Block_Device;
3153 Find_Real_Block_Device(temp, false);
3154 string block = basename(temp.c_str());
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05003155 string file = "/sys/fs/" + Current_File_System + "/" + block + "/lifetime_write_kbytes";
3156 string result;
3157 if (TWFunc::Path_Exists(file)) {
3158 if (TWFunc::read_file(file, result) != 0) {
3159 LOGINFO("Check_Lifetime_Writes of '%s' failed to read_file\n", file.c_str());
3160 } else {
3161 LOGINFO("Check_Lifetime_Writes result: '%s'\n", result.c_str());
3162 if (result == "0") {
3163 ret = 0;
3164 }
3165 }
3166 } else {
3167 LOGINFO("Check_Lifetime_Writes file does not exist '%s'\n", file.c_str());
3168 }
3169 UnMount(true);
3170 } else {
3171 LOGINFO("Check_Lifetime_Writes failed to mount '%s'\n", Mount_Point.c_str());
3172 }
3173 Mount_Read_Only = original_read_only;
3174 return ret;
3175}
Ethan Yonker66a19492015-12-10 10:19:45 -06003176
3177int TWPartition::Decrypt_Adopted() {
3178#ifdef TW_INCLUDE_CRYPTO
3179 int ret = 1;
3180 Is_Adopted_Storage = false;
3181 string Adopted_Key_File = "";
3182
3183 if (!Removable)
3184 return ret;
3185
3186 int fd = open(Alternate_Block_Device.c_str(), O_RDONLY);
3187 if (fd < 0) {
3188 LOGINFO("failed to open '%s'\n", Alternate_Block_Device.c_str());
3189 return ret;
3190 }
3191 char type_guid[80];
3192 char part_guid[80];
3193
3194 if (gpt_disk_get_partition_info(fd, 2, type_guid, part_guid) == 0) {
3195 LOGINFO("type: '%s'\n", type_guid);
3196 LOGINFO("part: '%s'\n", part_guid);
3197 Adopted_GUID = part_guid;
3198 LOGINFO("Adopted_GUID '%s'\n", Adopted_GUID.c_str());
3199 if (strcmp(type_guid, TWGptAndroidExpand) == 0) {
3200 LOGINFO("android_expand found\n");
3201 Adopted_Key_File = "/data/misc/vold/expand_";
3202 Adopted_Key_File += part_guid;
3203 Adopted_Key_File += ".key";
3204 if (TWFunc::Path_Exists(Adopted_Key_File)) {
3205 Is_Adopted_Storage = true;
3206 /* Until we find a use case for this, I think it is safe
3207 * to disable USB Mass Storage whenever adopted storage
3208 * is present.
3209 */
3210 LOGINFO("Detected adopted storage, disabling USB mass storage mode\n");
3211 DataManager::SetValue("tw_has_usb_storage", 0);
3212 }
3213 }
3214 }
3215
3216 if (Is_Adopted_Storage) {
3217 string Adopted_Block_Device = Alternate_Block_Device + "p2";
3218 if (!TWFunc::Path_Exists(Adopted_Block_Device)) {
3219 Adopted_Block_Device = Alternate_Block_Device + "2";
3220 if (!TWFunc::Path_Exists(Adopted_Block_Device)) {
3221 LOGINFO("Adopted block device does not exist\n");
3222 goto exit;
3223 }
3224 }
3225 LOGINFO("key file is '%s', block device '%s'\n", Adopted_Key_File.c_str(), Adopted_Block_Device.c_str());
3226 char crypto_blkdev[MAXPATHLEN];
3227 std::string thekey;
3228 int fdkey = open(Adopted_Key_File.c_str(), O_RDONLY);
3229 if (fdkey < 0) {
3230 LOGINFO("failed to open key file\n");
3231 goto exit;
3232 }
3233 char buf[512];
3234 ssize_t n;
3235 while ((n = read(fdkey, &buf[0], sizeof(buf))) > 0) {
3236 thekey.append(buf, n);
3237 }
3238 close(fdkey);
3239 unsigned char* key = (unsigned char*) thekey.data();
3240 cryptfs_revert_ext_volume(part_guid);
3241
3242 ret = cryptfs_setup_ext_volume(part_guid, Adopted_Block_Device.c_str(), key, thekey.size(), crypto_blkdev);
3243 if (ret == 0) {
3244 LOGINFO("adopted storage new block device: '%s'\n", crypto_blkdev);
3245 Decrypted_Block_Device = crypto_blkdev;
3246 Is_Decrypted = true;
3247 Is_Encrypted = true;
3248 Find_Actual_Block_Device();
Kjell Braden3126a112016-06-19 16:58:15 +00003249 if (!Mount_Storage_Retry(false)) {
Ethan Yonker66a19492015-12-10 10:19:45 -06003250 LOGERR("Failed to mount decrypted adopted storage device\n");
3251 Is_Decrypted = false;
3252 Is_Encrypted = false;
3253 cryptfs_revert_ext_volume(part_guid);
3254 ret = 1;
3255 } else {
Ethan Yonkerfcf3f242016-02-16 12:30:26 -06003256 UnMount(false);
3257 Has_Android_Secure = false;
3258 Symlink_Path = "";
3259 Symlink_Mount_Point = "";
3260 Backup_Name = Mount_Point.substr(1);
3261 Backup_Path = Mount_Point;
3262 TWPartition* sdext = PartitionManager.Find_Partition_By_Path("/sd-ext");
3263 if (sdext && sdext->Actual_Block_Device == Adopted_Block_Device) {
3264 LOGINFO("Removing /sd-ext from partition list due to adopted storage\n");
3265 PartitionManager.Remove_Partition_By_Path("/sd-ext");
3266 }
Ethan Yonker66a19492015-12-10 10:19:45 -06003267 Setup_Data_Media();
3268 Recreate_Media_Folder();
3269 Wipe_Available_in_GUI = true;
3270 Wipe_During_Factory_Reset = true;
3271 Can_Be_Backed_Up = true;
3272 Can_Encrypt_Backup = true;
3273 Use_Userdata_Encryption = true;
3274 Is_Storage = true;
3275 Storage_Name = "Adopted Storage";
3276 Is_SubPartition = true;
3277 SubPartition_Of = "/data";
3278 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
3279 DataManager::SetValue("tw_has_adopted_storage", 1);
3280 }
3281 } else {
3282 LOGERR("Failed to setup adopted storage decryption\n");
3283 }
3284 }
3285exit:
Matt Mower06543e32017-01-06 15:25:26 -06003286 close(fd);
Ethan Yonker66a19492015-12-10 10:19:45 -06003287 return ret;
3288#else
3289 LOGINFO("Decrypt_Adopted: no crypto support\n");
3290 return 1;
3291#endif
3292}
3293
3294void TWPartition::Revert_Adopted() {
3295#ifdef TW_INCLUDE_CRYPTO
3296 if (!Adopted_GUID.empty()) {
3297 PartitionManager.Remove_MTP_Storage(Mount_Point);
3298 UnMount(false);
3299 cryptfs_revert_ext_volume(Adopted_GUID.c_str());
3300 Is_Adopted_Storage = false;
3301 Is_Encrypted = false;
3302 Is_Decrypted = false;
3303 Decrypted_Block_Device = "";
3304 Find_Actual_Block_Device();
3305 Wipe_During_Factory_Reset = false;
3306 Can_Be_Backed_Up = false;
3307 Can_Encrypt_Backup = false;
3308 Use_Userdata_Encryption = false;
3309 Is_SubPartition = false;
3310 SubPartition_Of = "";
3311 Has_Data_Media = false;
3312 Storage_Path = Mount_Point;
3313 if (!Symlink_Mount_Point.empty()) {
3314 TWPartition* Dat = PartitionManager.Find_Partition_By_Path("/data");
3315 if (Dat) {
3316 Dat->UnMount(false);
3317 Dat->Symlink_Mount_Point = Symlink_Mount_Point;
3318 }
3319 Symlink_Mount_Point = "";
3320 }
3321 }
3322#else
3323 LOGINFO("Revert_Adopted: no crypto support\n");
3324#endif
3325}
bigbiff bigbiffb5ecaad2017-03-20 18:53:53 -04003326
3327void TWPartition::Set_Backup_FileName(string fname) {
3328 Backup_FileName = fname;
3329}
3330
3331string TWPartition::Get_Backup_Name() {
3332 return Backup_Name;
3333}