blob: 183283dce3bebaa88b0d08e18154d00ded39e535 [file] [log] [blame]
Dees Troy3be70a82013-10-22 14:25:12 +00001/*
bigbiff7ba75002020-04-11 20:47:09 -04002 Copyright 2013 to 2020 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>
bigbiff7ba75002020-04-11 20:47:09 -040022
Dees_Troy5bf43922012-09-07 16:07:55 -040023#include <sys/mount.h>
bigbiff7ba75002020-04-11 20:47:09 -040024#include <sys/param.h>
25#include <sys/stat.h>
26#include <sys/types.h>
27#include <sys/vfs.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040028#include <unistd.h>
Dees_Troy51127312012-09-08 13:08:49 -040029#include <dirent.h>
Ethan Yonker472f5062016-02-25 13:47:30 -060030#include <fcntl.h>
bigbiff7ba75002020-04-11 20:47:09 -040031#include <grp.h>
32#include <iostream>
33#include <libgen.h>
34#include <pwd.h>
35#include <zlib.h>
36#include <sstream>
Dees_Troy51a0e822012-09-05 15:24:24 -040037
Ethan Yonker007de982018-08-31 14:23:16 -050038#include "cutils/properties.h"
bigbiff7b4c7a62015-01-01 19:44:14 -050039#include "libblkid/include/blkid.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040040#include "variables.h"
Dees_Troy2673cec2013-04-02 20:22:16 +000041#include "twcommon.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040042#include "partitions.hpp"
Dees_Troy5bf43922012-09-07 16:07:55 -040043#include "data.hpp"
Dees_Troy43d8b002012-09-17 16:00:01 -040044#include "twrp-functions.hpp"
bigbiff bigbiff9c754052013-01-09 09:09:08 -050045#include "twrpTar.hpp"
Ethan Yonker3fdcda42016-11-30 12:29:37 -060046#include "exclude.hpp"
Ethan Yonker1b7a31b2014-07-03 15:09:22 -050047#include "infomanager.hpp"
Ethan Yonker4b94cfd2014-12-11 10:00:45 -060048#include "set_metadata.h"
Ethan Yonker74db1572015-10-28 12:44:49 -050049#include "gui/gui.hpp"
bigbiffce8f83c2015-12-12 18:30:21 -050050#include "adbbu/libtwadbbu.hpp"
Ethan Yonker98661c12018-10-17 08:39:28 -050051#ifdef TW_INCLUDE_CRYPTO
52 #include "crypto/fde/cryptfs.h"
53 #ifdef TW_INCLUDE_FBE
54 #include "crypto/ext4crypt/Decrypt.h"
55 #endif
56#else
57 #define CRYPT_FOOTER_OFFSET 0x4000
58#endif
Dees_Troy5bf43922012-09-07 16:07:55 -040059extern "C" {
Dees_Troy38bd7602012-09-14 13:33:53 -040060 #include "mtdutils/mtdutils.h"
61 #include "mtdutils/mounts.h"
Dees_Troya95f55c2013-08-17 13:14:43 +000062#ifdef USE_EXT4
Ethan Yonker8373cfe2017-09-08 06:50:54 -050063 // #include "make_ext4fs.h" TODO need ifdef for android8
64 #include <ext4_utils/make_ext4fs.h>
Dees_Troya95f55c2013-08-17 13:14:43 +000065#endif
Ethan Yonker71413f42014-02-26 13:36:08 -060066#ifdef TW_INCLUDE_CRYPTO
Ethan Yonker66a19492015-12-10 10:19:45 -060067 #include "gpt/gpt.h"
Ethan Yonker71413f42014-02-26 13:36:08 -060068#endif
Dees_Troy5bf43922012-09-07 16:07:55 -040069}
Matt Mower87413642017-01-17 21:14:46 -060070#include <selinux/selinux.h>
Ethan Yonkerf27497f2014-02-09 11:48:33 -060071#include <selinux/label.h>
Dees Troy4159aed2014-02-28 17:24:43 +000072#ifdef HAVE_CAPABILITIES
73#include <sys/capability.h>
74#include <sys/xattr.h>
75#include <linux/xattr.h>
76#endif
HashBanged974bb2016-01-30 14:20:09 -050077#include <sparse_format.h>
Ethan Yonker472f5062016-02-25 13:47:30 -060078#include "progresstracking.hpp"
Dees_Troy51a0e822012-09-05 15:24:24 -040079
bigbiff bigbiff9c754052013-01-09 09:09:08 -050080using namespace std;
81
Ethan Yonker6e8c27a2016-12-22 17:55:57 -060082static 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
83
Dees_Troya95f55c2013-08-17 13:14:43 +000084extern struct selabel_handle *selinux_handle;
Ethan Yonker6277c792014-09-15 14:54:30 -050085extern bool datamedia;
Dees_Troya95f55c2013-08-17 13:14:43 +000086
Hashcode62bd9e02013-11-19 21:59:42 -080087struct flag_list {
88 const char *name;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -060089 unsigned long flag;
Hashcode62bd9e02013-11-19 21:59:42 -080090};
91
Matt Mower4ab42b12016-04-21 13:52:18 -050092const struct flag_list mount_flags[] = {
Ethan Yonker6e8c27a2016-12-22 17:55:57 -060093 { "noatime", MS_NOATIME },
94 { "noexec", MS_NOEXEC },
95 { "nosuid", MS_NOSUID },
96 { "nodev", MS_NODEV },
97 { "nodiratime", MS_NODIRATIME },
98 { "ro", MS_RDONLY },
99 { "rw", 0 },
100 { "remount", MS_REMOUNT },
101 { "bind", MS_BIND },
102 { "rec", MS_REC },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000103#ifdef MS_UNBINDABLE
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600104 { "unbindable", MS_UNBINDABLE },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000105#endif
106#ifdef MS_PRIVATE
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600107 { "private", MS_PRIVATE },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000108#endif
109#ifdef MS_SLAVE
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600110 { "slave", MS_SLAVE },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000111#endif
112#ifdef MS_SHARED
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600113 { "shared", MS_SHARED },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000114#endif
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600115 { "sync", MS_SYNCHRONOUS },
116 { 0, 0 },
117};
118
119const char *ignored_mount_items[] = {
120 "defaults=",
121 "errors=",
Ethan Yonker93382822018-11-01 15:25:31 -0500122 "latemount",
123 "sysfs_path=",
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600124 NULL
Hashcode62bd9e02013-11-19 21:59:42 -0800125};
126
Matt Mower2416a502016-04-12 19:54:46 -0500127enum TW_FSTAB_FLAGS {
128 TWFLAG_DEFAULTS, // Retain position
129 TWFLAG_ANDSEC,
130 TWFLAG_BACKUP,
131 TWFLAG_BACKUPNAME,
132 TWFLAG_BLOCKSIZE,
133 TWFLAG_CANBEWIPED,
134 TWFLAG_CANENCRYPTBACKUP,
135 TWFLAG_DISPLAY,
136 TWFLAG_ENCRYPTABLE,
Ethan Yonkere9afc3d2018-08-30 15:16:27 -0500137 TWFLAG_FILEENCRYPTION,
Matt Mower2416a502016-04-12 19:54:46 -0500138 TWFLAG_FLASHIMG,
139 TWFLAG_FORCEENCRYPT,
140 TWFLAG_FSFLAGS,
141 TWFLAG_IGNOREBLKID,
142 TWFLAG_LENGTH,
143 TWFLAG_MOUNTTODECRYPT,
144 TWFLAG_REMOVABLE,
Matt Mower2416a502016-04-12 19:54:46 -0500145 TWFLAG_SETTINGSSTORAGE,
146 TWFLAG_STORAGE,
147 TWFLAG_STORAGENAME,
148 TWFLAG_SUBPARTITIONOF,
149 TWFLAG_SYMLINK,
150 TWFLAG_USERDATAENCRYPTBACKUP,
151 TWFLAG_USERMRF,
152 TWFLAG_WIPEDURINGFACTORYRESET,
153 TWFLAG_WIPEINGUI,
Ethan Yonker1b190162016-12-05 15:25:19 -0600154 TWFLAG_SLOTSELECT,
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600155 TWFLAG_WAIT,
156 TWFLAG_VERIFY,
157 TWFLAG_CHECK,
158 TWFLAG_ALTDEVICE,
159 TWFLAG_NOTRIM,
160 TWFLAG_VOLDMANAGED,
161 TWFLAG_FORMATTABLE,
162 TWFLAG_RESIZE,
Ethan Yonker93382822018-11-01 15:25:31 -0500163 TWFLAG_KEYDIRECTORY,
Peter Cai28ea7b02019-05-24 11:38:54 +0800164 TWFLAG_WRAPPEDKEY,
Matt Mower2416a502016-04-12 19:54:46 -0500165};
166
167/* Flags without a trailing '=' are considered dual format flags and can be
168 * written as either 'flagname' or 'flagname=', where the character following
169 * the '=' is Y,y,1 for true and false otherwise.
170 */
171const struct flag_list tw_flags[] = {
172 { "andsec", TWFLAG_ANDSEC },
173 { "backup", TWFLAG_BACKUP },
174 { "backupname=", TWFLAG_BACKUPNAME },
175 { "blocksize=", TWFLAG_BLOCKSIZE },
176 { "canbewiped", TWFLAG_CANBEWIPED },
177 { "canencryptbackup", TWFLAG_CANENCRYPTBACKUP },
178 { "defaults", TWFLAG_DEFAULTS },
179 { "display=", TWFLAG_DISPLAY },
180 { "encryptable=", TWFLAG_ENCRYPTABLE },
Ethan Yonkere9afc3d2018-08-30 15:16:27 -0500181 { "fileencryption=", TWFLAG_FILEENCRYPTION },
Matt Mower2416a502016-04-12 19:54:46 -0500182 { "flashimg", TWFLAG_FLASHIMG },
183 { "forceencrypt=", TWFLAG_FORCEENCRYPT },
184 { "fsflags=", TWFLAG_FSFLAGS },
185 { "ignoreblkid", TWFLAG_IGNOREBLKID },
186 { "length=", TWFLAG_LENGTH },
187 { "mounttodecrypt", TWFLAG_MOUNTTODECRYPT },
188 { "removable", TWFLAG_REMOVABLE },
Matt Mower2416a502016-04-12 19:54:46 -0500189 { "settingsstorage", TWFLAG_SETTINGSSTORAGE },
190 { "storage", TWFLAG_STORAGE },
191 { "storagename=", TWFLAG_STORAGENAME },
192 { "subpartitionof=", TWFLAG_SUBPARTITIONOF },
193 { "symlink=", TWFLAG_SYMLINK },
194 { "userdataencryptbackup", TWFLAG_USERDATAENCRYPTBACKUP },
195 { "usermrf", TWFLAG_USERMRF },
196 { "wipeduringfactoryreset", TWFLAG_WIPEDURINGFACTORYRESET },
197 { "wipeingui", TWFLAG_WIPEINGUI },
Ethan Yonker1b190162016-12-05 15:25:19 -0600198 { "slotselect", TWFLAG_SLOTSELECT },
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600199 { "wait", TWFLAG_WAIT },
200 { "verify", TWFLAG_VERIFY },
201 { "check", TWFLAG_CHECK },
202 { "altdevice", TWFLAG_ALTDEVICE },
203 { "notrim", TWFLAG_NOTRIM },
204 { "voldmanaged=", TWFLAG_VOLDMANAGED },
205 { "formattable", TWFLAG_FORMATTABLE },
206 { "resize", TWFLAG_RESIZE },
Ethan Yonker93382822018-11-01 15:25:31 -0500207 { "keydirectory=", TWFLAG_KEYDIRECTORY },
Peter Cai28ea7b02019-05-24 11:38:54 +0800208 { "wrappedkey", TWFLAG_WRAPPEDKEY },
Matt Mower2416a502016-04-12 19:54:46 -0500209 { 0, 0 },
210};
211
that9e0593e2014-10-08 00:01:24 +0200212TWPartition::TWPartition() {
Dees_Troy51a0e822012-09-05 15:24:24 -0400213 Can_Be_Mounted = false;
214 Can_Be_Wiped = false;
Dees_Troya13d74f2013-03-24 08:54:55 -0500215 Can_Be_Backed_Up = false;
Vojtech Bocek1dc30982013-08-30 21:49:30 +0200216 Use_Rm_Rf = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400217 Wipe_During_Factory_Reset = false;
218 Wipe_Available_in_GUI = false;
219 Is_SubPartition = false;
Dees_Troy2691f9d2012-09-24 11:15:49 -0400220 Has_SubPartition = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400221 SubPartition_Of = "";
222 Symlink_Path = "";
223 Symlink_Mount_Point = "";
224 Mount_Point = "";
Dees_Troye58d5262012-09-21 12:27:57 -0400225 Backup_Path = "";
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600226 Wildcard_Block_Device = false;
227 Sysfs_Entry = "";
Dees_Troy38bd7602012-09-14 13:33:53 -0400228 Actual_Block_Device = "";
229 Primary_Block_Device = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400230 Alternate_Block_Device = "";
231 Removable = false;
232 Is_Present = false;
233 Length = 0;
234 Size = 0;
235 Used = 0;
236 Free = 0;
237 Backup_Size = 0;
238 Can_Be_Encrypted = false;
239 Is_Encrypted = false;
240 Is_Decrypted = false;
Ethan Yonkerbd7492d2016-12-07 13:55:01 -0600241 Is_FBE = false;
Ethan Yonker253368a2014-11-25 15:00:52 -0600242 Mount_To_Decrypt = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400243 Decrypted_Block_Device = "";
244 Display_Name = "";
Dees_Troya13d74f2013-03-24 08:54:55 -0500245 Backup_Display_Name = "";
246 Storage_Name = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400247 Backup_Name = "";
Dees_Troy63c8df72012-09-10 14:02:05 -0400248 Backup_FileName = "";
Dees_Troy38bd7602012-09-14 13:33:53 -0400249 MTD_Name = "";
bigbiffce8f83c2015-12-12 18:30:21 -0500250 Backup_Method = BM_NONE;
Dees_Troy83bd4832013-05-04 12:39:56 +0000251 Can_Encrypt_Backup = false;
252 Use_Userdata_Encryption = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400253 Has_Data_Media = false;
Dees_Troye58d5262012-09-21 12:27:57 -0400254 Has_Android_Secure = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400255 Is_Storage = false;
Dees_Troya13d74f2013-03-24 08:54:55 -0500256 Is_Settings_Storage = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400257 Storage_Path = "";
258 Current_File_System = "";
259 Fstab_File_System = "";
Hashcode62bd9e02013-11-19 21:59:42 -0800260 Mount_Flags = 0;
261 Mount_Options = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400262 Format_Block_Size = 0;
Dees_Troy68cab492012-12-12 19:29:35 +0000263 Ignore_Blkid = false;
dianlujitao4879b372018-12-03 18:45:47 +0800264 Crypto_Key_Location = "";
Ethan Yonker726a0202014-12-16 20:01:38 -0600265 MTP_Storage_ID = 0;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600266 Can_Flash_Img = false;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500267 Mount_Read_Only = false;
Ethan Yonker66a19492015-12-10 10:19:45 -0600268 Is_Adopted_Storage = false;
269 Adopted_GUID = "";
Ethan Yonker1b190162016-12-05 15:25:19 -0600270 SlotSelect = false;
Ethan Yonker93382822018-11-01 15:25:31 -0500271 Key_Directory = "";
bigbiffee7b7ff2020-03-23 15:08:27 -0400272 Is_Super = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400273}
274
275TWPartition::~TWPartition(void) {
276 // Do nothing
277}
278
Chaosmasterf6e42ce2020-01-27 00:17:04 +0100279bool TWPartition::Process_Fstab_Line(const char *fstab_line, bool Display_Error, std::map<string, Flags_Map> *twrp_flags, bool Sar_Detect) {
Matt Mower2b2dd152016-04-26 11:24:08 -0500280 char full_line[MAX_FSTAB_LINE_LENGTH];
Matt Mower2416a502016-04-12 19:54:46 -0500281 char twflags[MAX_FSTAB_LINE_LENGTH] = "";
Dees_Troy5bf43922012-09-07 16:07:55 -0400282 char* ptr;
Matt Mower2b2dd152016-04-26 11:24:08 -0500283 int line_len = strlen(fstab_line), index = 0, item_index = 0;
Dees_Troya13d74f2013-03-24 08:54:55 -0500284 bool skip = false;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600285 int fstab_version = 1, mount_point_index = 0, fs_index = 1, block_device_index = 2;
286 TWPartition *additional_entry = NULL;
287 std::map<string, Flags_Map>::iterator it;
Dees_Troy5bf43922012-09-07 16:07:55 -0400288
Matt Mower2b2dd152016-04-26 11:24:08 -0500289 strlcpy(full_line, fstab_line, sizeof(full_line));
bigbiffee7b7ff2020-03-23 15:08:27 -0400290
Dees_Troy51127312012-09-08 13:08:49 -0400291 for (index = 0; index < line_len; index++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500292 if (full_line[index] == 34)
293 skip = !skip;
294 if (!skip && full_line[index] <= 32)
Dees_Troy5bf43922012-09-07 16:07:55 -0400295 full_line[index] = '\0';
Dees_Troy5bf43922012-09-07 16:07:55 -0400296 }
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600297 if (line_len < 10)
298 return false; // There can't possibly be a valid fstab line that is less than 10 chars
299 if (strncmp(fstab_line, "/dev/", strlen("/dev/")) == 0 || strncmp(fstab_line, "/devices/", strlen("/devices/")) == 0) {
300 fstab_version = 2;
301 block_device_index = 0;
302 mount_point_index = 1;
303 fs_index = 2;
304 }
305
bigbiffee7b7ff2020-03-23 15:08:27 -0400306 Is_Super = PartitionManager.Is_Super_Partition(fstab_line);
307 if (Is_Super) {
308 block_device_index = 0;
309 fstab_version = 2;
310 mount_point_index = 1;
311 fs_index = 2;
312 }
313
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600314 index = 0;
Dees_Troy5bf43922012-09-07 16:07:55 -0400315 while (index < line_len) {
316 while (index < line_len && full_line[index] == '\0')
317 index++;
318 if (index >= line_len)
319 continue;
320 ptr = full_line + index;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600321 if (item_index == mount_point_index) {
322 Mount_Point = ptr;
bigbiffee7b7ff2020-03-23 15:08:27 -0400323 if (fstab_version == 2 && Is_Super == false) {
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600324 additional_entry = PartitionManager.Find_Partition_By_Path(Mount_Point);
Chaosmasterf6e42ce2020-01-27 00:17:04 +0100325 if (!Sar_Detect && additional_entry) {
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600326 LOGINFO("Found an additional entry for '%s'\n", Mount_Point.c_str());
327 }
328 }
Chaosmasterf6e42ce2020-01-27 00:17:04 +0100329 if(!Sar_Detect)
330 LOGINFO("Processing '%s'\n", Mount_Point.c_str());
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600331 Backup_Path = Mount_Point;
332 Storage_Path = Mount_Point;
333 Display_Name = ptr + 1;
334 Backup_Display_Name = Display_Name;
335 Storage_Name = Display_Name;
336 item_index++;
337 } else if (item_index == fs_index) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400338 // File System
339 Fstab_File_System = ptr;
340 Current_File_System = ptr;
341 item_index++;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600342 } else if (item_index == block_device_index) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400343 // Primary Block Device
Dees_Troy38bd7602012-09-14 13:33:53 -0400344 if (Fstab_File_System == "mtd" || Fstab_File_System == "yaffs2") {
Dees_Troy094207a2012-09-26 12:00:39 -0400345 MTD_Name = ptr;
346 Find_MTD_Block_Device(MTD_Name);
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400347 } else if (Fstab_File_System == "bml") {
348 if (Mount_Point == "/boot")
349 MTD_Name = "boot";
350 else if (Mount_Point == "/recovery")
351 MTD_Name = "recovery";
352 Primary_Block_Device = ptr;
353 if (*ptr != '/')
Dees_Troy2673cec2013-04-02 20:22:16 +0000354 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 -0400355 } else if (*ptr != '/') {
bigbiffee7b7ff2020-03-23 15:08:27 -0400356 if (!Is_Super) {
357 if (Display_Error)
358 LOGERR("Invalid block device '%s' in fstab line '%s'", ptr, fstab_line);
359 else
360 LOGINFO("Invalid block device '%s' in fstab line '%s'", ptr, fstab_line);
361 return false;
362 }
Dees_Troy38bd7602012-09-14 13:33:53 -0400363 } else {
364 Primary_Block_Device = ptr;
365 Find_Real_Block_Device(Primary_Block_Device, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400366 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400367 item_index++;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600368 } else if (item_index > 2) {
369 if (fstab_version == 2) {
370 if (item_index == 3) {
371 Process_FS_Flags(ptr);
372 if (additional_entry) {
373 additional_entry->Save_FS_Flags(Fstab_File_System, Mount_Flags, Mount_Options);
374 return false; // We save the extra fs flags in the other partition entry and by returning false, this entry will be deleted
375 }
376 } else {
377 strlcpy(twflags, ptr, sizeof(twflags));
378 }
379 item_index++;
380 } else if (*ptr == '/') { // v2 fstab does not allow alternate block devices
Dees_Troy5bf43922012-09-07 16:07:55 -0400381 // Alternate Block Device
382 Alternate_Block_Device = ptr;
383 Find_Real_Block_Device(Alternate_Block_Device, Display_Error);
384 } else if (strlen(ptr) > 7 && strncmp(ptr, "length=", 7) == 0) {
385 // Partition length
386 ptr += 7;
387 Length = atoi(ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400388 } else if (strlen(ptr) > 6 && strncmp(ptr, "flags=", 6) == 0) {
389 // Custom flags, save for later so that new values aren't overwritten by defaults
390 ptr += 6;
Matt Mower2416a502016-04-12 19:54:46 -0500391 strlcpy(twflags, ptr, sizeof(twflags));
Dees_Troy38bd7602012-09-14 13:33:53 -0400392 } else if (strlen(ptr) == 4 && (strncmp(ptr, "NULL", 4) == 0 || strncmp(ptr, "null", 4) == 0 || strncmp(ptr, "null", 4) == 0)) {
393 // Do nothing
Dees_Troy5bf43922012-09-07 16:07:55 -0400394 } else {
395 // Unhandled data
Matt Mower2b2dd152016-04-26 11:24:08 -0500396 LOGINFO("Unhandled fstab information '%s' in fstab line '%s'\n", ptr, fstab_line);
Dees_Troy5bf43922012-09-07 16:07:55 -0400397 }
398 }
399 while (index < line_len && full_line[index] != '\0')
400 index++;
401 }
402
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600403 // override block devices from the v2 fstab with the ones we read from the twrp.flags file in case they are different
404 if (fstab_version == 2 && twrp_flags && twrp_flags->size() > 0) {
405 it = twrp_flags->find(Mount_Point);
406 if (it != twrp_flags->end()) {
407 if (!it->second.Primary_Block_Device.empty()) {
408 Primary_Block_Device = it->second.Primary_Block_Device;
409 Find_Real_Block_Device(Primary_Block_Device, Display_Error);
410 }
411 if (!it->second.Alternate_Block_Device.empty()) {
412 Alternate_Block_Device = it->second.Alternate_Block_Device;
413 Find_Real_Block_Device(Alternate_Block_Device, Display_Error);
414 }
415 }
416 }
417
418 if (strncmp(fstab_line, "/devices/", strlen("/devices/")) == 0) {
419 Sysfs_Entry = Primary_Block_Device;
420 Primary_Block_Device = "";
421 Is_Storage = true;
422 Removable = true;
423 Wipe_Available_in_GUI = true;
424 Wildcard_Block_Device = true;
425 }
426 if (Primary_Block_Device.find("*") != string::npos)
427 Wildcard_Block_Device = true;
428
Chaosmasterf6e42ce2020-01-27 00:17:04 +0100429 if (Sar_Detect) {
430 if(Is_File_System(Fstab_File_System) && (Mount_Point == "/" || Mount_Point == "/system" || Mount_Point == "/system_root"))
431 Find_Actual_Block_Device();
432 else
433 return true;
434 } else if (Mount_Point == "auto") {
dianlujitaob76a73a2020-04-30 20:33:20 +0800435 Mount_Point = "/auto" + to_string(auto_index);
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600436 Backup_Path = Mount_Point;
437 Storage_Path = Mount_Point;
dianlujitaob76a73a2020-04-30 20:33:20 +0800438 Backup_Name = Mount_Point.substr(1);
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600439 auto_index++;
440 Setup_File_System(Display_Error);
441 Display_Name = "Storage";
442 Backup_Display_Name = Display_Name;
443 Storage_Name = Display_Name;
444 Can_Be_Backed_Up = false;
445 Wipe_Available_in_GUI = true;
446 Is_Storage = true;
447 Removable = true;
448 Wipe_Available_in_GUI = true;
449 } else if (!Is_File_System(Fstab_File_System) && !Is_Image(Fstab_File_System)) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400450 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000451 LOGERR("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400452 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000453 LOGINFO("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Matt Mower72c87ce2016-04-26 14:34:56 -0500454 return false;
Dees_Troy5bf43922012-09-07 16:07:55 -0400455 } else if (Is_File_System(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400456 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400457 Setup_File_System(Display_Error);
dianlujitaob76a73a2020-04-30 20:33:20 +0800458 Backup_Name = Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
Chaosmaster3b682732020-01-25 21:09:28 +0100459 if (Mount_Point == "/" || Mount_Point == "/system" || Mount_Point == "/system_root") {
Dees_Troy5bf43922012-09-07 16:07:55 -0400460 Display_Name = "System";
dianlujitaob76a73a2020-04-30 20:33:20 +0800461 Backup_Name = "system";
Dees_Troya13d74f2013-03-24 08:54:55 -0500462 Backup_Display_Name = Display_Name;
463 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400464 Wipe_Available_in_GUI = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500465 Can_Be_Backed_Up = true;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500466 Mount_Read_Only = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400467 } else if (Mount_Point == "/data") {
468 Display_Name = "Data";
Dees_Troya13d74f2013-03-24 08:54:55 -0500469 Backup_Display_Name = Display_Name;
470 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400471 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400472 Wipe_During_Factory_Reset = true;
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 = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400476 } else if (Mount_Point == "/cache") {
477 Display_Name = "Cache";
Dees_Troya13d74f2013-03-24 08:54:55 -0500478 Backup_Display_Name = Display_Name;
479 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400480 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400481 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500482 Can_Be_Backed_Up = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400483 } else if (Mount_Point == "/datadata") {
Dees_Troy51127312012-09-08 13:08:49 -0400484 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400485 Display_Name = "DataData";
Dees_Troya13d74f2013-03-24 08:54:55 -0500486 Backup_Display_Name = Display_Name;
487 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400488 Is_SubPartition = true;
489 SubPartition_Of = "/data";
Dees_Troy5bf43922012-09-07 16:07:55 -0400490 DataManager::SetValue(TW_HAS_DATADATA, 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500491 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000492 Can_Encrypt_Backup = true;
493 Use_Userdata_Encryption = false; // This whole partition should be encrypted
Dees_Troy5bf43922012-09-07 16:07:55 -0400494 } else if (Mount_Point == "/sd-ext") {
Dees_Troy51127312012-09-08 13:08:49 -0400495 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400496 Display_Name = "SD-Ext";
Dees_Troya13d74f2013-03-24 08:54:55 -0500497 Backup_Display_Name = Display_Name;
498 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400499 Wipe_Available_in_GUI = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400500 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500501 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000502 Can_Encrypt_Backup = true;
503 Use_Userdata_Encryption = true;
Dees_Troy2c50e182012-09-26 20:05:28 -0400504 } else if (Mount_Point == "/boot") {
505 Display_Name = "Boot";
Dees_Troya13d74f2013-03-24 08:54:55 -0500506 Backup_Display_Name = Display_Name;
Dees_Troy2c50e182012-09-26 20:05:28 -0400507 DataManager::SetValue("tw_boot_is_mountable", 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500508 Can_Be_Backed_Up = true;
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500509 } else if (Mount_Point == "/vendor") {
510 Display_Name = "Vendor";
511 Backup_Display_Name = Display_Name;
512 Storage_Name = Display_Name;
513 Mount_Read_Only = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400514 }
515#ifdef TW_EXTERNAL_STORAGE_PATH
516 if (Mount_Point == EXPAND(TW_EXTERNAL_STORAGE_PATH)) {
517 Is_Storage = true;
518 Storage_Path = EXPAND(TW_EXTERNAL_STORAGE_PATH);
Dees_Troyc51f1f92012-09-20 15:32:13 -0400519 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500520 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400521#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000522 if (Mount_Point == "/sdcard" || Mount_Point == "/external_sd" || Mount_Point == "/external_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400523 Is_Storage = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400524 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500525 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400526#endif
Dees_Troyb05ddee2013-01-28 20:24:50 +0000527 }
Dees_Troy8170a922012-09-18 15:40:25 -0400528#ifdef TW_INTERNAL_STORAGE_PATH
529 if (Mount_Point == EXPAND(TW_INTERNAL_STORAGE_PATH)) {
530 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500531 Is_Settings_Storage = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400532 Storage_Path = EXPAND(TW_INTERNAL_STORAGE_PATH);
Dees_Troya13d74f2013-03-24 08:54:55 -0500533 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400534 }
535#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000536 if (Mount_Point == "/emmc" || Mount_Point == "/internal_sd" || Mount_Point == "/internal_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400537 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500538 Is_Settings_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500539 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400540 }
541#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400542 } else if (Is_Image(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400543 Find_Actual_Block_Device();
Ethan Yonker1b190162016-12-05 15:25:19 -0600544 Setup_Image();
Dees_Troya13d74f2013-03-24 08:54:55 -0500545 if (Mount_Point == "/boot") {
546 Display_Name = "Boot";
547 Backup_Display_Name = Display_Name;
548 Can_Be_Backed_Up = true;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600549 Can_Flash_Img = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500550 } else if (Mount_Point == "/recovery") {
551 Display_Name = "Recovery";
552 Backup_Display_Name = Display_Name;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600553 Can_Flash_Img = true;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500554 } else if (Mount_Point == "/system_image") {
555 Display_Name = "System Image";
556 Backup_Display_Name = Display_Name;
HashBanged974bb2016-01-30 14:20:09 -0500557 Can_Flash_Img = true;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500558 Can_Be_Backed_Up = true;
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500559 } else if (Mount_Point == "/vendor_image") {
560 Display_Name = "Vendor Image";
561 Backup_Display_Name = Display_Name;
HashBanged974bb2016-01-30 14:20:09 -0500562 Can_Flash_Img = true;
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500563 Can_Be_Backed_Up = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500564 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400565 }
566
Matt Mower2416a502016-04-12 19:54:46 -0500567 // Process TWRP fstab flags
568 if (strlen(twflags) > 0) {
569 string Prev_Display_Name = Display_Name;
570 string Prev_Storage_Name = Storage_Name;
571 string Prev_Backup_Display_Name = Backup_Display_Name;
572 Display_Name = "";
573 Storage_Name = "";
574 Backup_Display_Name = "";
575
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600576 Process_TW_Flags(twflags, (fstab_version == 1), fstab_version);
577 Save_FS_Flags(Fstab_File_System, Mount_Flags, Mount_Options);
Matt Mower2416a502016-04-12 19:54:46 -0500578
579 bool has_display_name = !Display_Name.empty();
580 bool has_storage_name = !Storage_Name.empty();
581 bool has_backup_name = !Backup_Display_Name.empty();
582 if (!has_display_name) Display_Name = Prev_Display_Name;
583 if (!has_storage_name) Storage_Name = Prev_Storage_Name;
584 if (!has_backup_name) Backup_Display_Name = Prev_Backup_Display_Name;
585
586 if (has_display_name && !has_storage_name)
587 Storage_Name = Display_Name;
588 if (!has_display_name && has_storage_name)
589 Display_Name = Storage_Name;
590 if (has_display_name && !has_backup_name && Backup_Display_Name != "Android Secure")
591 Backup_Display_Name = Display_Name;
592 if (!has_display_name && has_backup_name)
593 Display_Name = Backup_Display_Name;
594 }
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600595
596 if (fstab_version == 2 && twrp_flags && twrp_flags->size() > 0) {
597 it = twrp_flags->find(Mount_Point);
598 if (it != twrp_flags->end()) {
599 char twrpflags[MAX_FSTAB_LINE_LENGTH] = "";
600 int skip = 0;
601 string Flags = it->second.Flags;
602 strcpy(twrpflags, Flags.c_str());
603 if (strlen(twrpflags) > strlen("flags=") && strncmp(twrpflags, "flags=", strlen("flags=")) == 0)
604 skip += strlen("flags=");
605 char* flagptr = twrpflags;
606 flagptr += skip;
607 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
608 }
609 }
nkk7198fc3992017-12-16 16:26:42 +0200610
611 if (Mount_Point == "/persist" && Can_Be_Mounted) {
612 bool mounted = Is_Mounted();
613 if (mounted || Mount(false)) {
614 // Read the backup settings file
615 DataManager::LoadPersistValues();
616 TWFunc::Fixup_Time_On_Boot("/persist/time/");
617 if (!mounted)
618 UnMount(false);
619 }
620 }
621
Chaosmasterf6e42ce2020-01-27 00:17:04 +0100622 if (Is_File_System(Fstab_File_System) && (Mount_Point == "/" || Mount_Point == "/system" || Mount_Point == "/system_root")) {
623 if (Sar_Detect) {
624 Mount_Point = "/s";
625 Mount_Read_Only = true;
626 Can_Be_Mounted = true;
627 } else {
628 Mount_Point = PartitionManager.Get_Android_Root_Path();
629 Backup_Path = Mount_Point;
630 Storage_Path = Mount_Point;
631 Make_Dir(Mount_Point, Display_Error);
632 }
bigbiffee7b7ff2020-03-23 15:08:27 -0400633 if (Is_Super) {
634 Can_Be_Backed_Up = false;
635 Can_Be_Wiped = false;
636 Wipe_Available_in_GUI = false;
637 }
Chaosmasterf6e42ce2020-01-27 00:17:04 +0100638 }
639
Dees_Troy51127312012-09-08 13:08:49 -0400640 return true;
641}
642
Matt Mower72c87ce2016-04-26 14:34:56 -0500643void TWPartition::Partition_Post_Processing(bool Display_Error) {
644 if (Mount_Point == "/data")
645 Setup_Data_Partition(Display_Error);
646 else if (Mount_Point == "/cache")
647 Setup_Cache_Partition(Display_Error);
648}
649
Ethan Yonkerbd7492d2016-12-07 13:55:01 -0600650void TWPartition::ExcludeAll(const string& path) {
651 backup_exclusions.add_absolute_dir(path);
652 wipe_exclusions.add_absolute_dir(path);
653}
654
Matt Mower72c87ce2016-04-26 14:34:56 -0500655void TWPartition::Setup_Data_Partition(bool Display_Error) {
656 if (Mount_Point != "/data")
657 return;
658
659 // Ensure /data is not mounted as tmpfs for qcom hardware decrypt
660 UnMount(false);
661
662#ifdef TW_INCLUDE_CRYPTO
663 if (datamedia)
664 Setup_Data_Media();
665 Can_Be_Encrypted = true;
666 char crypto_blkdev[255];
667 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
668 if (strcmp(crypto_blkdev, "error") != 0) {
bigbiff7ba75002020-04-11 20:47:09 -0400669 Set_FBE_Status();
Matt Mower72c87ce2016-04-26 14:34:56 -0500670 Decrypted_Block_Device = crypto_blkdev;
671 LOGINFO("Data already decrypted, new block device: '%s'\n", crypto_blkdev);
bigbiff7ba75002020-04-11 20:47:09 -0400672 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
Matt Mower72c87ce2016-04-26 14:34:56 -0500673 } else if (!Mount(false)) {
674 if (Is_Present) {
Ethan Yonker93382822018-11-01 15:25:31 -0500675 if (Key_Directory.empty()) {
bigbiffadc599e2020-05-28 19:36:30 +0000676 set_partition_data(Actual_Block_Device.c_str(), Crypto_Key_Location.c_str(), Fstab_File_System.c_str());
Ethan Yonker93382822018-11-01 15:25:31 -0500677 if (cryptfs_check_footer() == 0) {
678 Is_Encrypted = true;
679 Is_Decrypted = false;
680 Can_Be_Mounted = false;
681 Current_File_System = "emmc";
682 Setup_Image();
Ethan Yonker93382822018-11-01 15:25:31 -0500683 DataManager::SetValue(TW_CRYPTO_PWTYPE, cryptfs_get_password_type());
684 DataManager::SetValue(TW_CRYPTO_PASSWORD, "");
685 DataManager::SetValue("tw_crypto_display", "");
686 } else {
687 gui_err("mount_data_footer=Could not mount /data and unable to find crypto footer.");
688 }
689 } else {
Matt Mower72c87ce2016-04-26 14:34:56 -0500690 Is_Encrypted = true;
691 Is_Decrypted = false;
Matt Mower72c87ce2016-04-26 14:34:56 -0500692 }
Ethan Yonker93382822018-11-01 15:25:31 -0500693 } else if (Key_Directory.empty()) {
bigbiffadc599e2020-05-28 19:36:30 +0000694 LOGERR("Primary block device '%s' for mount point '%s' is not present!\n", Primary_Block_Device.c_str(), Mount_Point.c_str());
Matt Mower72c87ce2016-04-26 14:34:56 -0500695 }
696 } else {
bigbiff7ba75002020-04-11 20:47:09 -0400697 Set_FBE_Status();
mauronofrio9bf73352019-11-23 22:27:34 +0100698 if (!Decrypt_FBE_DE()) {
mauronofriofc79aa62019-11-23 23:13:04 +0100699 char wrappedvalue[PROPERTY_VALUE_MAX];
700 property_get("fbe.data.wrappedkey", wrappedvalue, "");
701 std::string wrappedkeyvalue(wrappedvalue);
702 if (wrappedkeyvalue == "true") {
703 LOGERR("Unable to decrypt FBE device\n");
704 } else {
705 LOGINFO("Trying wrapped key.\n");
706 property_set("fbe.data.wrappedkey", "true");
mauronofrio9bf73352019-11-23 22:27:34 +0100707 if (!Decrypt_FBE_DE()) {
bigbiffd58ba182020-03-23 10:02:29 -0400708 LOGERR("Unable to decrypt FBE device\n");
mauronofrio9bf73352019-11-23 22:27:34 +0100709 }
mauronofriofc79aa62019-11-23 23:13:04 +0100710 }
mauronofrio9bf73352019-11-23 22:27:34 +0100711 }
bigbiff7ba75002020-04-11 20:47:09 -0400712 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
Matt Mower72c87ce2016-04-26 14:34:56 -0500713 }
714 if (datamedia && (!Is_Encrypted || (Is_Encrypted && Is_Decrypted))) {
715 Setup_Data_Media();
bigbiff7ba75002020-04-11 20:47:09 -0400716 if (!TWFunc::Is_Mount_Wiped("/data"))
717 Recreate_Media_Folder();
Matt Mower72c87ce2016-04-26 14:34:56 -0500718 }
719#else
720 if (datamedia) {
721 Setup_Data_Media();
722 Recreate_Media_Folder();
723 }
724#endif
725}
726
bigbiff7ba75002020-04-11 20:47:09 -0400727void TWPartition::Set_FBE_Status() {
728 DataManager::SetValue(TW_IS_DECRYPTED, 1);
729 Is_Encrypted = true;
730 Is_Decrypted = true;
731 LOGINFO("Setup_Data_Partition::Key_Directory::%s\n", Key_Directory.c_str());
732 if (Key_Directory.empty()) {
733 Is_FBE = false;
734 DataManager::SetValue(TW_IS_FBE, 0);
735 } else {
736 Is_FBE = true;
737 DataManager::SetValue(TW_IS_FBE, 1);
738 }
739}
740
Ethan Yonker93382822018-11-01 15:25:31 -0500741bool TWPartition::Decrypt_FBE_DE() {
bigbiff7ba75002020-04-11 20:47:09 -0400742 if (TWFunc::Path_Exists("/data/unencrypted/key/version")) {
Captain Throwback87d73d62020-02-17 16:44:50 -0500743 DataManager::SetValue(TW_IS_FBE, 1);
Ethan Yonker93382822018-11-01 15:25:31 -0500744 LOGINFO("File Based Encryption is present\n");
745#ifdef TW_INCLUDE_FBE
bigbiffadc599e2020-05-28 19:36:30 +0000746 ExcludeAll(Mount_Point + "/convert_fbe");
747 ExcludeAll(Mount_Point + "/unencrypted");
748 //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
749 ExcludeAll(Mount_Point + "/misc/vold/user_keys");
bigbiff7ba75002020-04-11 20:47:09 -0400750 ExcludeAll(Mount_Point + "/misc/vold/volume_keys");
bigbiffadc599e2020-05-28 19:36:30 +0000751 //ExcludeAll(Mount_Point + "/system_ce");
752 //ExcludeAll(Mount_Point + "/system_de");
753 //ExcludeAll(Mount_Point + "/misc_ce");
754 //ExcludeAll(Mount_Point + "/misc_de");
755 ExcludeAll(Mount_Point + "/system/gatekeeper.password.key");
756 ExcludeAll(Mount_Point + "/system/gatekeeper.pattern.key");
757 ExcludeAll(Mount_Point + "/system/locksettings.db");
758 //ExcludeAll(Mount_Point + "/system/locksettings.db-shm"); // don't seem to need this one, but the other 2 are needed
759 ExcludeAll(Mount_Point + "/system/locksettings.db-wal");
760 //ExcludeAll(Mount_Point + "/user_de");
761 //ExcludeAll(Mount_Point + "/misc/profiles/cur/0"); // might be important later
762 ExcludeAll(Mount_Point + "/misc/gatekeeper");
763 ExcludeAll(Mount_Point + "/misc/keystore");
764 ExcludeAll(Mount_Point + "/drm/kek.dat");
765 ExcludeAll(Mount_Point + "/system_de/0/spblob"); // contains data needed to decrypt pixel 2
bigbiff7ba75002020-04-11 20:47:09 -0400766 ExcludeAll(Mount_Point + "/per_boot"); // removed each boot by init
767
bigbiffadc599e2020-05-28 19:36:30 +0000768 int retry_count = 3;
769 while (!Decrypt_DE() && --retry_count)
770 usleep(2000);
771 if (retry_count > 0) {
772 property_set("ro.crypto.state", "encrypted");
773 Is_Encrypted = true;
774 Is_Decrypted = false;
775 Is_FBE = true;
776 DataManager::SetValue(TW_IS_FBE, 1);
bigbiffadc599e2020-05-28 19:36:30 +0000777 string filename;
778 int pwd_type = Get_Password_Type(0, filename);
779 if (pwd_type < 0) {
780 LOGERR("This TWRP does not have synthetic password decrypt support\n");
781 pwd_type = 0; // default password
782 }
783 DataManager::SetValue(TW_CRYPTO_PWTYPE, pwd_type);
784 DataManager::SetValue(TW_CRYPTO_PASSWORD, "");
785 DataManager::SetValue("tw_crypto_display", "");
786 return true;
Ethan Yonker93382822018-11-01 15:25:31 -0500787 }
788#else
789 LOGERR("FBE found but FBE support not present in TWRP\n");
790#endif
791 }
Captain Throwback87d73d62020-02-17 16:44:50 -0500792 DataManager::SetValue(TW_IS_FBE, 0);
Ethan Yonker93382822018-11-01 15:25:31 -0500793 return false;
794}
795
Matt Mower72c87ce2016-04-26 14:34:56 -0500796void TWPartition::Setup_Cache_Partition(bool Display_Error __unused) {
797 if (Mount_Point != "/cache")
798 return;
799
800 if (!Mount(true))
801 return;
802
803 if (!TWFunc::Path_Exists("/cache/recovery/.")) {
804 LOGINFO("Recreating /cache/recovery folder\n");
805 if (mkdir("/cache/recovery", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0)
806 LOGERR("Could not create /cache/recovery\n");
807 }
808}
809
Matt Mower4ab42b12016-04-21 13:52:18 -0500810void TWPartition::Process_FS_Flags(const char *str) {
811 char *options = strdup(str);
812 char *ptr, *savep;
Hashcode62bd9e02013-11-19 21:59:42 -0800813
Matt Mower4ab42b12016-04-21 13:52:18 -0500814 Mount_Options = "";
Hashcode62bd9e02013-11-19 21:59:42 -0800815
Matt Mower4ab42b12016-04-21 13:52:18 -0500816 // Avoid issues with potentially nested strtok by using strtok_r
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600817 for (ptr = strtok_r(options, ",", &savep); ptr; ptr = strtok_r(NULL, ",", &savep)) {
818 char *equals = strstr(ptr, "=");
819 size_t name_len;
Matt Mower4ab42b12016-04-21 13:52:18 -0500820
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600821 if (!equals)
822 name_len = strlen(ptr);
823 else
824 name_len = equals - ptr;
825
826 // There are some flags that we want to ignore in TWRP
827 bool found_match = false;
828 for (const char** ignored_mount_item = ignored_mount_items; *ignored_mount_item; ignored_mount_item++) {
829 if (strncmp(ptr, *ignored_mount_item, name_len) == 0) {
830 found_match = true;
Hashcode62bd9e02013-11-19 21:59:42 -0800831 break;
832 }
833 }
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600834 if (found_match)
835 continue;
Hashcode62bd9e02013-11-19 21:59:42 -0800836
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600837 // mount_flags are never postfixed by '='
838 if (!equals) {
839 const struct flag_list* mount_flag = mount_flags;
840 for (; mount_flag->name; mount_flag++) {
841 if (strcmp(ptr, mount_flag->name) == 0) {
842 if (mount_flag->flag == MS_RDONLY)
843 Mount_Read_Only = true;
844 else
845 Mount_Flags |= (unsigned)mount_flag->flag;
846 found_match = true;
847 break;
848 }
849 }
850 if (found_match)
851 continue;
Matt Mower4ab42b12016-04-21 13:52:18 -0500852 }
853
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600854 // If we aren't ignoring this flag and it's not a mount flag, then it must be a mount option
855 if (!Mount_Options.empty())
856 Mount_Options += ",";
857 Mount_Options += ptr;
Matt Mower4ab42b12016-04-21 13:52:18 -0500858 }
859 free(options);
Hashcode62bd9e02013-11-19 21:59:42 -0800860}
861
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600862void TWPartition::Save_FS_Flags(const string& local_File_System, int local_Mount_Flags, const string& local_Mount_Options) {
863 partition_fs_flags_struct flags;
864 flags.File_System = local_File_System;
865 flags.Mount_Flags = local_Mount_Flags;
866 flags.Mount_Options = local_Mount_Options;
867 fs_flags.push_back(flags);
868}
869
Matt Mower2416a502016-04-12 19:54:46 -0500870void TWPartition::Apply_TW_Flag(const unsigned flag, const char* str, const bool val) {
871 switch (flag) {
872 case TWFLAG_ANDSEC:
873 Has_Android_Secure = val;
874 break;
875 case TWFLAG_BACKUP:
876 Can_Be_Backed_Up = val;
877 break;
878 case TWFLAG_BACKUPNAME:
879 Backup_Display_Name = str;
880 break;
881 case TWFLAG_BLOCKSIZE:
882 Format_Block_Size = (unsigned long)(atol(str));
883 break;
884 case TWFLAG_CANBEWIPED:
885 Can_Be_Wiped = val;
886 break;
887 case TWFLAG_CANENCRYPTBACKUP:
888 Can_Encrypt_Backup = val;
889 break;
890 case TWFLAG_DEFAULTS:
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600891 case TWFLAG_WAIT:
892 case TWFLAG_VERIFY:
893 case TWFLAG_CHECK:
894 case TWFLAG_NOTRIM:
895 case TWFLAG_VOLDMANAGED:
896 case TWFLAG_RESIZE:
Matt Mower2416a502016-04-12 19:54:46 -0500897 // Do nothing
898 break;
899 case TWFLAG_DISPLAY:
900 Display_Name = str;
901 break;
902 case TWFLAG_ENCRYPTABLE:
903 case TWFLAG_FORCEENCRYPT:
904 Crypto_Key_Location = str;
905 break;
Ethan Yonkere9afc3d2018-08-30 15:16:27 -0500906 case TWFLAG_FILEENCRYPTION:
907 // This flag isn't used by TWRP but is needed in 9.0 FBE decrypt
908 // fileencryption=ice:aes-256-heh
909 {
910 std::string FBE = str;
Ethan Yonkere9afc3d2018-08-30 15:16:27 -0500911 size_t colon_loc = FBE.find(":");
912 if (colon_loc == std::string::npos) {
Ethan Yonker93382822018-11-01 15:25:31 -0500913 property_set("fbe.contents", FBE.c_str());
914 property_set("fbe.filenames", "");
915 LOGINFO("FBE contents '%s', filenames ''\n", FBE.c_str());
Ethan Yonkere9afc3d2018-08-30 15:16:27 -0500916 break;
917 }
Ethan Yonker93382822018-11-01 15:25:31 -0500918 std::string FBE_contents, FBE_filenames;
Ethan Yonkere9afc3d2018-08-30 15:16:27 -0500919 FBE_contents = FBE.substr(0, colon_loc);
920 FBE_filenames = FBE.substr(colon_loc + 1);
921 property_set("fbe.contents", FBE_contents.c_str());
922 property_set("fbe.filenames", FBE_filenames.c_str());
923 LOGINFO("FBE contents '%s', filenames '%s'\n", FBE_contents.c_str(), FBE_filenames.c_str());
924 }
925 break;
Peter Cai28ea7b02019-05-24 11:38:54 +0800926 case TWFLAG_WRAPPEDKEY:
927 // Set fbe.data.wrappedkey to true
928 {
929 property_set("fbe.data.wrappedkey", "true");
930 LOGINFO("FBE wrapped key enabled\n");
931 }
932 break;
Matt Mower2416a502016-04-12 19:54:46 -0500933 case TWFLAG_FLASHIMG:
934 Can_Flash_Img = val;
935 break;
936 case TWFLAG_FSFLAGS:
Matt Mower4ab42b12016-04-21 13:52:18 -0500937 Process_FS_Flags(str);
Matt Mower2416a502016-04-12 19:54:46 -0500938 break;
939 case TWFLAG_IGNOREBLKID:
940 Ignore_Blkid = val;
941 break;
942 case TWFLAG_LENGTH:
943 Length = atoi(str);
944 break;
945 case TWFLAG_MOUNTTODECRYPT:
946 Mount_To_Decrypt = val;
947 break;
948 case TWFLAG_REMOVABLE:
949 Removable = val;
950 break;
Matt Mower2416a502016-04-12 19:54:46 -0500951 case TWFLAG_SETTINGSSTORAGE:
952 Is_Settings_Storage = val;
953 if (Is_Settings_Storage)
954 Is_Storage = true;
955 break;
956 case TWFLAG_STORAGE:
957 Is_Storage = val;
958 break;
959 case TWFLAG_STORAGENAME:
960 Storage_Name = str;
961 break;
962 case TWFLAG_SUBPARTITIONOF:
963 Is_SubPartition = true;
964 SubPartition_Of = str;
965 break;
966 case TWFLAG_SYMLINK:
967 Symlink_Path = str;
968 break;
969 case TWFLAG_USERDATAENCRYPTBACKUP:
970 Use_Userdata_Encryption = val;
971 if (Use_Userdata_Encryption)
972 Can_Encrypt_Backup = true;
973 break;
974 case TWFLAG_USERMRF:
975 Use_Rm_Rf = val;
976 break;
977 case TWFLAG_WIPEDURINGFACTORYRESET:
978 Wipe_During_Factory_Reset = val;
979 if (Wipe_During_Factory_Reset) {
980 Can_Be_Wiped = true;
981 Wipe_Available_in_GUI = true;
982 }
983 break;
984 case TWFLAG_WIPEINGUI:
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600985 case TWFLAG_FORMATTABLE:
Matt Mower2416a502016-04-12 19:54:46 -0500986 Wipe_Available_in_GUI = val;
987 if (Wipe_Available_in_GUI)
988 Can_Be_Wiped = true;
989 break;
Ethan Yonker1b190162016-12-05 15:25:19 -0600990 case TWFLAG_SLOTSELECT:
991 SlotSelect = true;
992 break;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600993 case TWFLAG_ALTDEVICE:
994 Alternate_Block_Device = str;
995 break;
Ethan Yonker93382822018-11-01 15:25:31 -0500996 case TWFLAG_KEYDIRECTORY:
997 Key_Directory = str;
bigbiffd58ba182020-03-23 10:02:29 -0400998 break;
Matt Mower2416a502016-04-12 19:54:46 -0500999 default:
1000 // Should not get here
1001 LOGINFO("Flag identified for processing, but later unmatched: %i\n", flag);
1002 break;
1003 }
1004}
Dees_Troy51127312012-09-08 13:08:49 -04001005
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06001006void TWPartition::Process_TW_Flags(char *flags, bool Display_Error, int fstab_ver) {
Matt Mower2416a502016-04-12 19:54:46 -05001007 char separator[2] = {'\n', 0};
1008 char *ptr, *savep;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06001009 char source_separator = ';';
1010
1011 if (fstab_ver == 2)
1012 source_separator = ',';
Matt Mower2416a502016-04-12 19:54:46 -05001013
1014 // Semicolons within double-quotes are not forbidden, so replace
1015 // only the semicolons intended as separators with '\n' for strtok
1016 for (unsigned i = 0, skip = 0; i < strlen(flags); i++) {
1017 if (flags[i] == '\"')
Dees_Troya13d74f2013-03-24 08:54:55 -05001018 skip = !skip;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06001019 if (!skip && flags[i] == source_separator)
Matt Mower2416a502016-04-12 19:54:46 -05001020 flags[i] = separator[0];
Dees_Troy51127312012-09-08 13:08:49 -04001021 }
1022
Matt Mower2416a502016-04-12 19:54:46 -05001023 // Avoid issues with potentially nested strtok by using strtok_r
1024 ptr = strtok_r(flags, separator, &savep);
1025 while (ptr) {
1026 int ptr_len = strlen(ptr);
1027 const struct flag_list* tw_flag = tw_flags;
Hashcode62bd9e02013-11-19 21:59:42 -08001028
Matt Mower2416a502016-04-12 19:54:46 -05001029 for (; tw_flag->name; tw_flag++) {
1030 int flag_len = strlen(tw_flag->name);
1031
1032 if (strncmp(ptr, tw_flag->name, flag_len) == 0) {
1033 bool flag_val = false;
1034
1035 if (ptr_len > flag_len && (tw_flag->name)[flag_len-1] != '='
1036 && ptr[flag_len] != '=') {
1037 // Handle flags with same starting string
1038 // (e.g. backup and backupname)
1039 continue;
1040 } else if (ptr_len > flag_len && ptr[flag_len] == '=') {
1041 // Handle flags with dual format: Part 1
1042 // (e.g. backup and backup=y. backup=y handled here)
1043 ptr += flag_len + 1;
1044 TWFunc::Strip_Quotes(ptr);
1045 // Skip flags with empty argument
1046 // (e.g. backup=)
1047 if (strlen(ptr) == 0) {
1048 LOGINFO("Flag missing argument or should not include '=': %s=\n", tw_flag->name);
1049 break;
1050 }
1051 flag_val = strchr("yY1", *ptr) != NULL;
1052 } else if (ptr_len == flag_len
1053 && (tw_flag->name)[flag_len-1] == '=') {
1054 // Skip flags missing argument after =
1055 // (e.g. backupname=)
1056 LOGINFO("Flag missing argument: %s\n", tw_flag->name);
1057 break;
1058 } else if (ptr_len > flag_len
1059 && (tw_flag->name)[flag_len-1] == '=') {
1060 // Handle arguments to flags
1061 // (e.g. backupname="My Stuff")
1062 ptr += flag_len;
1063 TWFunc::Strip_Quotes(ptr);
1064 // Skip flags with empty argument
1065 // (e.g. backupname="")
1066 if (strlen(ptr) == 0) {
1067 LOGINFO("Flag missing argument: %s\n", tw_flag->name);
1068 break;
1069 }
1070 } else if (ptr_len == flag_len) {
1071 // Handle flags with dual format: Part 2
1072 // (e.g. backup and backup=y. backup handled here)
1073 flag_val = true;
Ethan Yonker96af84a2015-01-05 14:58:36 -06001074 } else {
Matt Mower2416a502016-04-12 19:54:46 -05001075 LOGINFO("Flag matched, but could not be processed: %s\n", ptr);
1076 break;
Ethan Yonker96af84a2015-01-05 14:58:36 -06001077 }
Matt Mower2416a502016-04-12 19:54:46 -05001078
1079 Apply_TW_Flag(tw_flag->flag, ptr, flag_val);
1080 break;
Ethan Yonker96af84a2015-01-05 14:58:36 -06001081 }
Matt Mower2416a502016-04-12 19:54:46 -05001082 }
1083 if (tw_flag->name == 0) {
Dees_Troy51127312012-09-08 13:08:49 -04001084 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001085 LOGERR("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -04001086 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001087 LOGINFO("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -04001088 }
Matt Mower2416a502016-04-12 19:54:46 -05001089 ptr = strtok_r(NULL, separator, &savep);
Dees_Troy51127312012-09-08 13:08:49 -04001090 }
Dees_Troy51a0e822012-09-05 15:24:24 -04001091}
1092
Dees_Troy5bf43922012-09-07 16:07:55 -04001093bool TWPartition::Is_File_System(string File_System) {
1094 if (File_System == "ext2" ||
Dees_Troy63c8df72012-09-10 14:02:05 -04001095 File_System == "ext3" ||
Dees_Troy5bf43922012-09-07 16:07:55 -04001096 File_System == "ext4" ||
1097 File_System == "vfat" ||
1098 File_System == "ntfs" ||
1099 File_System == "yaffs2" ||
bigbiff bigbiff3e146522012-11-14 14:32:59 -05001100 File_System == "exfat" ||
Dees_Troye5017042013-08-29 16:38:55 +00001101 File_System == "f2fs" ||
Erfan Abdib859e912019-02-05 18:18:46 +02001102 File_System == "squashfs" ||
Dees_Troy5bf43922012-09-07 16:07:55 -04001103 File_System == "auto")
1104 return true;
1105 else
1106 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001107}
1108
Dees_Troy5bf43922012-09-07 16:07:55 -04001109bool TWPartition::Is_Image(string File_System) {
Dees_Troy5fcd8f92012-10-16 12:22:05 -04001110 if (File_System == "emmc" || File_System == "mtd" || File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -04001111 return true;
1112 else
1113 return false;
1114}
1115
Dees_Troy51127312012-09-08 13:08:49 -04001116bool TWPartition::Make_Dir(string Path, bool Display_Error) {
Ethan Yonker93ac7a02016-11-07 22:05:58 -06001117 if (TWFunc::Get_D_Type_From_Stat(Path) != S_IFDIR)
1118 unlink(Path.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001119 if (!TWFunc::Path_Exists(Path)) {
Dees_Troy51127312012-09-08 13:08:49 -04001120 if (mkdir(Path.c_str(), 0777) == -1) {
1121 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001122 gui_msg(Msg(msg::kError, "create_folder_strerr=Can not create '{1}' folder ({2}).")(Path)(strerror(errno)));
Dees_Troy51127312012-09-08 13:08:49 -04001123 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001124 LOGINFO("Can not create '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -04001125 return false;
1126 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001127 LOGINFO("Created '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -04001128 return true;
1129 }
1130 }
1131 return true;
1132}
1133
Dees_Troy5bf43922012-09-07 16:07:55 -04001134void TWPartition::Setup_File_System(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001135 Can_Be_Mounted = true;
1136 Can_Be_Wiped = true;
1137
Dees_Troy5bf43922012-09-07 16:07:55 -04001138 // Make the mount point folder if it doesn't exist
Dees_Troy51127312012-09-08 13:08:49 -04001139 Make_Dir(Mount_Point, Display_Error);
bigbiffce8f83c2015-12-12 18:30:21 -05001140 Backup_Method = BM_FILES;
Dees_Troy5bf43922012-09-07 16:07:55 -04001141}
1142
Ethan Yonker1b190162016-12-05 15:25:19 -06001143void TWPartition::Setup_Image() {
Dees_Troy5bf43922012-09-07 16:07:55 -04001144 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
1145 Backup_Name = Display_Name;
Gary Peck82599a82012-11-21 16:23:12 -08001146 if (Current_File_System == "emmc")
bigbiffce8f83c2015-12-12 18:30:21 -05001147 Backup_Method = BM_DD;
Gary Peck82599a82012-11-21 16:23:12 -08001148 else if (Current_File_System == "mtd" || Current_File_System == "bml")
bigbiffce8f83c2015-12-12 18:30:21 -05001149 Backup_Method = BM_FLASH_UTILS;
Dees_Troy5bf43922012-09-07 16:07:55 -04001150 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001151 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 -04001152}
1153
Dees_Troye58d5262012-09-21 12:27:57 -04001154void TWPartition::Setup_AndSec(void) {
Dees_Troya13d74f2013-03-24 08:54:55 -05001155 Backup_Display_Name = "Android Secure";
Dees_Troye58d5262012-09-21 12:27:57 -04001156 Backup_Name = "and-sec";
Dees_Troya13d74f2013-03-24 08:54:55 -05001157 Can_Be_Backed_Up = true;
Dees_Troye58d5262012-09-21 12:27:57 -04001158 Has_Android_Secure = true;
1159 Symlink_Path = Mount_Point + "/.android_secure";
1160 Symlink_Mount_Point = "/and-sec";
1161 Backup_Path = Symlink_Mount_Point;
1162 Make_Dir("/and-sec", true);
1163 Recreate_AndSec_Folder();
Kjell Braden3126a112016-06-19 16:58:15 +00001164 Mount_Storage_Retry(true);
Dees_Troye58d5262012-09-21 12:27:57 -04001165}
1166
that9e0593e2014-10-08 00:01:24 +02001167void TWPartition::Setup_Data_Media() {
Ethan Yonker6277c792014-09-15 14:54:30 -05001168 LOGINFO("Setting up '%s' as data/media emulated storage.\n", Mount_Point.c_str());
Ethan Yonker66a19492015-12-10 10:19:45 -06001169 if (Storage_Name.empty() || Storage_Name == "Data")
1170 Storage_Name = "Internal Storage";
Ethan Yonker6277c792014-09-15 14:54:30 -05001171 Has_Data_Media = true;
1172 Is_Storage = true;
Ethan Yonker66a19492015-12-10 10:19:45 -06001173 Storage_Path = Mount_Point + "/media";
Ethan Yonker6277c792014-09-15 14:54:30 -05001174 Symlink_Path = Storage_Path;
Ethan Yonker66a19492015-12-10 10:19:45 -06001175 if (Mount_Point == "/data") {
1176 Is_Settings_Storage = true;
1177 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
1178 Make_Dir("/emmc", false);
1179 Symlink_Mount_Point = "/emmc";
1180 } else {
1181 Make_Dir("/sdcard", false);
1182 Symlink_Mount_Point = "/sdcard";
1183 }
1184 if (Mount(false) && TWFunc::Path_Exists(Mount_Point + "/media/0")) {
1185 Storage_Path = Mount_Point + "/media/0";
1186 Symlink_Path = Storage_Path;
1187 DataManager::SetValue(TW_INTERNAL_PATH, Mount_Point + "/media/0");
1188 UnMount(true);
1189 }
1190 DataManager::SetValue("tw_has_internal", 1);
1191 DataManager::SetValue("tw_has_data_media", 1);
Ethan Yonker3fdcda42016-11-30 12:29:37 -06001192 backup_exclusions.add_absolute_dir("/data/data/com.google.android.music/files");
Ethan Yonker6355b562017-05-01 09:42:17 -05001193 wipe_exclusions.add_absolute_dir(Mount_Point + "/misc/vold"); // adopted storage keys
Ethan Yonker79f88bd2016-12-09 14:52:12 -06001194 ExcludeAll(Mount_Point + "/system/storage.xml");
Ethan Yonker6277c792014-09-15 14:54:30 -05001195 } else {
Ethan Yonker66a19492015-12-10 10:19:45 -06001196 if (Mount(true) && TWFunc::Path_Exists(Mount_Point + "/media/0")) {
1197 Storage_Path = Mount_Point + "/media/0";
1198 Symlink_Path = Storage_Path;
1199 UnMount(true);
1200 }
Ethan Yonker6277c792014-09-15 14:54:30 -05001201 }
Ethan Yonker79f88bd2016-12-09 14:52:12 -06001202 ExcludeAll(Mount_Point + "/media");
Ethan Yonker6277c792014-09-15 14:54:30 -05001203}
1204
Dees_Troy5bf43922012-09-07 16:07:55 -04001205void TWPartition::Find_Real_Block_Device(string& Block, bool Display_Error) {
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06001206 char device[PATH_MAX], realDevice[PATH_MAX];
Dees_Troy5bf43922012-09-07 16:07:55 -04001207
1208 strcpy(device, Block.c_str());
1209 memset(realDevice, 0, sizeof(realDevice));
1210 while (readlink(device, realDevice, sizeof(realDevice)) > 0)
1211 {
1212 strcpy(device, realDevice);
1213 memset(realDevice, 0, sizeof(realDevice));
1214 }
1215
1216 if (device[0] != '/') {
1217 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001218 LOGERR("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001219 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001220 LOGINFO("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001221 return;
Dees_Troy51a0e822012-09-05 15:24:24 -04001222 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -04001223 Block = device;
1224 return;
1225 }
1226}
1227
Kjell Braden3126a112016-06-19 16:58:15 +00001228bool TWPartition::Mount_Storage_Retry(bool Display_Error) {
Dees_Troy8e337f32012-10-13 22:07:49 -04001229 // On some devices, storage doesn't want to mount right away, retry and sleep
Kjell Braden3126a112016-06-19 16:58:15 +00001230 if (!Mount(Display_Error)) {
Dees_Troy8e337f32012-10-13 22:07:49 -04001231 int retry_count = 5;
1232 while (retry_count > 0 && !Mount(false)) {
1233 usleep(500000);
1234 retry_count--;
1235 }
Kjell Braden3126a112016-06-19 16:58:15 +00001236 return Mount(Display_Error);
Dees_Troy8e337f32012-10-13 22:07:49 -04001237 }
Kjell Braden3126a112016-06-19 16:58:15 +00001238 return true;
Dees_Troy8e337f32012-10-13 22:07:49 -04001239}
1240
Dees_Troy38bd7602012-09-14 13:33:53 -04001241bool TWPartition::Find_MTD_Block_Device(string MTD_Name) {
1242 FILE *fp = NULL;
1243 char line[255];
1244
1245 fp = fopen("/proc/mtd", "rt");
1246 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001247 LOGERR("Device does not support /proc/mtd\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001248 return false;
1249 }
1250
1251 while (fgets(line, sizeof(line), fp) != NULL)
1252 {
1253 char device[32], label[32];
1254 unsigned long size = 0;
Dees_Troy38bd7602012-09-14 13:33:53 -04001255 int deviceId;
1256
1257 sscanf(line, "%s %lx %*s %*c%s", device, &size, label);
1258
1259 // Skip header and blank lines
1260 if ((strcmp(device, "dev:") == 0) || (strlen(line) < 8))
1261 continue;
1262
1263 // Strip off the trailing " from the label
1264 label[strlen(label)-1] = '\0';
1265
1266 if (strcmp(label, MTD_Name.c_str()) == 0) {
1267 // We found our device
1268 // Strip off the trailing : from the device
1269 device[strlen(device)-1] = '\0';
1270 if (sscanf(device,"mtd%d", &deviceId) == 1) {
1271 sprintf(device, "/dev/block/mtdblock%d", deviceId);
1272 Primary_Block_Device = device;
Dees_Troy76543db2013-06-19 16:24:30 +00001273 fclose(fp);
1274 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -04001275 }
1276 }
1277 }
1278 fclose(fp);
1279
1280 return false;
1281}
1282
Dees_Troy51127312012-09-08 13:08:49 -04001283bool TWPartition::Get_Size_Via_statfs(bool Display_Error) {
1284 struct statfs st;
1285 string Local_Path = Mount_Point + "/.";
1286
1287 if (!Mount(Display_Error))
1288 return false;
1289
1290 if (statfs(Local_Path.c_str(), &st) != 0) {
1291 if (!Removable) {
1292 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001293 LOGERR("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -04001294 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001295 LOGINFO("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -04001296 }
1297 return false;
1298 }
1299 Size = (st.f_blocks * st.f_bsize);
1300 Used = ((st.f_blocks - st.f_bfree) * st.f_bsize);
1301 Free = (st.f_bfree * st.f_bsize);
1302 Backup_Size = Used;
1303 return true;
1304}
1305
1306bool TWPartition::Get_Size_Via_df(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001307 FILE* fp;
1308 char command[255], line[512];
1309 int include_block = 1;
1310 unsigned int min_len;
1311
1312 if (!Mount(Display_Error))
1313 return false;
1314
Dees_Troy38bd7602012-09-14 13:33:53 -04001315 min_len = Actual_Block_Device.size() + 2;
Dees_Troy51127312012-09-08 13:08:49 -04001316 sprintf(command, "df %s > /tmp/dfoutput.txt", Mount_Point.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001317 TWFunc::Exec_Cmd(command);
Dees_Troy51127312012-09-08 13:08:49 -04001318 fp = fopen("/tmp/dfoutput.txt", "rt");
1319 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001320 LOGINFO("Unable to open /tmp/dfoutput.txt.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001321 return false;
Dees_Troy51127312012-09-08 13:08:49 -04001322 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001323
1324 while (fgets(line, sizeof(line), fp) != NULL)
1325 {
1326 unsigned long blocks, used, available;
1327 char device[64];
1328 char tmpString[64];
1329
1330 if (strncmp(line, "Filesystem", 10) == 0)
1331 continue;
1332 if (strlen(line) < min_len) {
1333 include_block = 0;
1334 continue;
1335 }
1336 if (include_block) {
1337 sscanf(line, "%s %lu %lu %lu", device, &blocks, &used, &available);
1338 } else {
1339 // The device block string is so long that the df information is on the next line
1340 int space_count = 0;
Dees_Troye58d5262012-09-21 12:27:57 -04001341 sprintf(tmpString, "/dev/block/%s", Actual_Block_Device.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001342 while (tmpString[space_count] == 32)
1343 space_count++;
1344 sscanf(line + space_count, "%lu %lu %lu", &blocks, &used, &available);
1345 }
1346
1347 // Adjust block size to byte size
1348 Size = blocks * 1024ULL;
1349 Used = used * 1024ULL;
1350 Free = available * 1024ULL;
1351 Backup_Size = Used;
1352 }
1353 fclose(fp);
1354 return true;
1355}
1356
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001357unsigned long long TWPartition::IOCTL_Get_Block_Size() {
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001358 Find_Actual_Block_Device();
Ethan Yonker483e9f42016-01-11 22:21:18 -06001359
1360 return TWFunc::IOCTL_Get_Block_Size(Actual_Block_Device.c_str());
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001361}
1362
Dees_Troy5bf43922012-09-07 16:07:55 -04001363bool TWPartition::Find_Partition_Size(void) {
1364 FILE* fp;
1365 char line[512];
1366 string tmpdevice;
1367
igoriok87e3d932013-01-31 21:03:53 +02001368 fp = fopen("/proc/dumchar_info", "rt");
1369 if (fp != NULL) {
1370 while (fgets(line, sizeof(line), fp) != NULL)
1371 {
1372 char label[32], device[32];
1373 unsigned long size = 0;
1374
thatd43bf2d2014-09-21 23:13:02 +02001375 sscanf(line, "%s %lx %*x %*u %s", label, &size, device);
igoriok87e3d932013-01-31 21:03:53 +02001376
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05001377 // Skip header, annotation and blank lines
igoriok87e3d932013-01-31 21:03:53 +02001378 if ((strncmp(device, "/dev/", 5) != 0) || (strlen(line) < 8))
1379 continue;
1380
1381 tmpdevice = "/dev/";
1382 tmpdevice += label;
1383 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
1384 Size = size;
1385 fclose(fp);
1386 return true;
1387 }
1388 }
1389 }
1390
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001391 unsigned long long ioctl_size = IOCTL_Get_Block_Size();
1392 if (ioctl_size) {
1393 Size = ioctl_size;
1394 return true;
1395 }
1396
Dees_Troy5bf43922012-09-07 16:07:55 -04001397 // In this case, we'll first get the partitions we care about (with labels)
1398 fp = fopen("/proc/partitions", "rt");
1399 if (fp == NULL)
1400 return false;
1401
1402 while (fgets(line, sizeof(line), fp) != NULL)
1403 {
1404 unsigned long major, minor, blocks;
1405 char device[512];
Dees_Troy5bf43922012-09-07 16:07:55 -04001406
Dees_Troy63c8df72012-09-10 14:02:05 -04001407 if (strlen(line) < 7 || line[0] == 'm') continue;
Dees_Troy5bf43922012-09-07 16:07:55 -04001408 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
1409
1410 tmpdevice = "/dev/block/";
1411 tmpdevice += device;
Dees_Troy38bd7602012-09-14 13:33:53 -04001412 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001413 // Adjust block size to byte size
1414 Size = blocks * 1024ULL;
1415 fclose(fp);
1416 return true;
1417 }
1418 }
1419 fclose(fp);
1420 return false;
1421}
1422
Dees_Troy5bf43922012-09-07 16:07:55 -04001423bool TWPartition::Is_Mounted(void) {
1424 if (!Can_Be_Mounted)
1425 return false;
1426
1427 struct stat st1, st2;
1428 string test_path;
1429
1430 // Check to see if the mount point directory exists
1431 test_path = Mount_Point + "/.";
1432 if (stat(test_path.c_str(), &st1) != 0) return false;
1433
1434 // Check to see if the directory above the mount point exists
1435 test_path = Mount_Point + "/../.";
1436 if (stat(test_path.c_str(), &st2) != 0) return false;
1437
1438 // Compare the device IDs -- if they match then we're (probably) using tmpfs instead of an actual device
1439 int ret = (st1.st_dev != st2.st_dev) ? true : false;
Dees_Troy5bf43922012-09-07 16:07:55 -04001440 return ret;
1441}
1442
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05001443bool TWPartition::Is_File_System_Writable(void) {
1444 if (!Is_File_System(Current_File_System) || !Is_Mounted())
1445 return false;
1446
1447 string test_path = Mount_Point + "/.";
1448 return (access(test_path.c_str(), W_OK) == 0);
1449}
1450
Dees_Troy5bf43922012-09-07 16:07:55 -04001451bool TWPartition::Mount(bool Display_Error) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001452 int exfat_mounted = 0;
Martin Dünkelmann6d843782018-12-27 23:40:28 +01001453 unsigned int flags = Mount_Flags;
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001454
Dees_Troy5bf43922012-09-07 16:07:55 -04001455 if (Is_Mounted()) {
1456 return true;
1457 } else if (!Can_Be_Mounted) {
1458 return false;
1459 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001460
1461 Find_Actual_Block_Device();
1462
1463 // Check the current file system before mounting
1464 Check_FS_Type();
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001465 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/exfat-fuse")) {
Dees_Troye34c1332013-02-06 19:13:00 +00001466 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 +00001467 LOGINFO("cmd: %s\n", cmd.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001468 string result;
1469 if (TWFunc::Exec_Cmd(cmd, result) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001470 LOGINFO("exfat-fuse failed to mount with result '%s', trying vfat\n", result.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001471 Current_File_System = "vfat";
1472 } else {
1473#ifdef TW_NO_EXFAT_FUSE
1474 UnMount(false);
1475 // We'll let the kernel handle it but using exfat-fuse to detect if the file system is actually exfat
1476 // Some kernels let us mount vfat as exfat which doesn't work out too well
1477#else
1478 exfat_mounted = 1;
1479#endif
1480 }
1481 }
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001482
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001483 if (Current_File_System == "ntfs" && (TWFunc::Path_Exists("/sbin/ntfs-3g") || TWFunc::Path_Exists("/sbin/mount.ntfs"))) {
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001484 string cmd;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001485 string Ntfsmount_Binary = "";
1486
1487 if (TWFunc::Path_Exists("/sbin/ntfs-3g"))
1488 Ntfsmount_Binary = "ntfs-3g";
1489 else if (TWFunc::Path_Exists("/sbin/mount.ntfs"))
1490 Ntfsmount_Binary = "mount.ntfs";
1491
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001492 if (Mount_Read_Only)
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001493 cmd = "/sbin/" + Ntfsmount_Binary + " -o ro " + Actual_Block_Device + " " + Mount_Point;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001494 else
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001495 cmd = "/sbin/" + Ntfsmount_Binary + " " + Actual_Block_Device + " " + Mount_Point;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001496 LOGINFO("cmd: '%s'\n", cmd.c_str());
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001497
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001498 if (TWFunc::Exec_Cmd(cmd) == 0) {
1499 return true;
1500 } else {
1501 LOGINFO("ntfs-3g failed to mount, trying regular mount method.\n");
1502 }
1503 }
1504
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001505 if (Mount_Read_Only)
1506 flags |= MS_RDONLY;
1507
Dees_Troy22042032012-12-18 21:23:08 +00001508 if (Fstab_File_System == "yaffs2") {
1509 // mount an MTD partition as a YAFFS2 filesystem.
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001510 flags = MS_NOATIME | MS_NODEV | MS_NODIRATIME;
1511 if (Mount_Read_Only)
1512 flags |= MS_RDONLY;
Dees_Troy76543db2013-06-19 16:24:30 +00001513 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags, NULL) < 0) {
1514 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags | MS_RDONLY, NULL) < 0) {
1515 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001516 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy76543db2013-06-19 16:24:30 +00001517 else
1518 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
1519 return false;
1520 } else {
1521 LOGINFO("Mounted '%s' (MTD) as RO\n", Mount_Point.c_str());
1522 return true;
1523 }
1524 } else {
1525 struct stat st;
1526 string test_path = Mount_Point;
1527 if (stat(test_path.c_str(), &st) < 0) {
1528 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001529 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy76543db2013-06-19 16:24:30 +00001530 else
1531 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
1532 return false;
1533 }
1534 mode_t new_mode = st.st_mode | S_IXUSR | S_IXGRP | S_IXOTH;
1535 if (new_mode != st.st_mode) {
1536 LOGINFO("Fixing execute permissions for %s\n", Mount_Point.c_str());
1537 if (chmod(Mount_Point.c_str(), new_mode) < 0) {
1538 if (Display_Error)
1539 LOGERR("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
1540 else
1541 LOGINFO("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
1542 return false;
1543 }
1544 }
Dees_Troy22042032012-12-18 21:23:08 +00001545 return true;
Dees_Troy76543db2013-06-19 16:24:30 +00001546 }
thatc7572eb2015-03-31 18:55:30 +02001547 }
1548
1549 string mount_fs = Current_File_System;
1550 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sys/module/texfat"))
1551 mount_fs = "texfat";
1552
1553 if (!exfat_mounted &&
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001554 mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), mount_fs.c_str(), flags, Mount_Options.c_str()) != 0 &&
1555 mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), mount_fs.c_str(), flags, NULL) != 0) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001556#ifdef TW_NO_EXFAT_FUSE
1557 if (Current_File_System == "exfat") {
Dees_Troy2673cec2013-04-02 20:22:16 +00001558 LOGINFO("Mounting exfat failed, trying vfat...\n");
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001559 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), "vfat", 0, NULL) != 0) {
Dees_Troy85f44ed2013-01-09 18:42:36 +00001560 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001561 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy85f44ed2013-01-09 18:42:36 +00001562 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001563 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001564 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 +00001565 return false;
Dees_Troy85f44ed2013-01-09 18:42:36 +00001566 }
Dees_Troyb05ddee2013-01-28 20:24:50 +00001567 } else {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001568#endif
bigbiff bigbiff26774a02014-03-29 18:22:00 -04001569 if (!Removable && Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001570 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001571 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001572 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
1573 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 +00001574 return false;
1575#ifdef TW_NO_EXFAT_FUSE
Dees_Troy85f44ed2013-01-09 18:42:36 +00001576 }
1577#endif
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001578 }
Ethan Yonker253368a2014-11-25 15:00:52 -06001579
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001580 if (Removable)
1581 Update_Size(Display_Error);
1582
xiaolu9416f4f2015-06-04 08:22:23 +08001583 if (!Symlink_Mount_Point.empty() && TWFunc::Path_Exists(Symlink_Path)) {
codelover2a3d4ce2015-03-14 20:26:49 +08001584 string Command = "mount -o bind '" + Symlink_Path + "' '" + Symlink_Mount_Point + "'";
Vojtech Bocek05534202013-09-11 08:11:56 +02001585 TWFunc::Exec_Cmd(Command);
Dees_Troy5bf43922012-09-07 16:07:55 -04001586 }
Chaosmasterda974802020-01-26 21:09:28 +01001587
bigbiff32cbabe2020-04-12 19:16:19 -04001588 #ifndef TW_NO_BIND_SYSTEM
Chaosmasterda974802020-01-26 21:09:28 +01001589 if (Mount_Point == "/system_root") {
1590 unlink("/system");
1591 mkdir("/system", 0755);
1592 mount("/system_root/system", "/system", "auto", MS_BIND, NULL);
1593 }
bigbiff7ba75002020-04-11 20:47:09 -04001594#endif
Dees_Troy5bf43922012-09-07 16:07:55 -04001595 return true;
1596}
1597
1598bool TWPartition::UnMount(bool Display_Error) {
Chaosmasterda974802020-01-26 21:09:28 +01001599 if (Mount_Point == "/system_root") {
1600 if (umount("/system") == -1)
1601 umount2("/system", MNT_DETACH);
1602 rmdir("/system");
1603 symlink("/system_root/system", "/system");
1604 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001605 if (Is_Mounted()) {
1606 int never_unmount_system;
1607
1608 DataManager::GetValue(TW_DONT_UNMOUNT_SYSTEM, never_unmount_system);
Captain Throwback9d6feb52018-07-27 10:05:24 -04001609 if (never_unmount_system == 1 && Mount_Point == PartitionManager.Get_Android_Root_Path())
Dees_Troy5bf43922012-09-07 16:07:55 -04001610 return true; // Never unmount system if you're not supposed to unmount it
1611
Matt Mowera8e6d832017-02-14 20:20:59 -06001612 if (Is_Storage && MTP_Storage_ID > 0)
Ethan Yonker726a0202014-12-16 20:01:38 -06001613 PartitionManager.Remove_MTP_Storage(MTP_Storage_ID);
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001614
Dees_Troy38bd7602012-09-14 13:33:53 -04001615 if (!Symlink_Mount_Point.empty())
1616 umount(Symlink_Mount_Point.c_str());
1617
Dees_Troyb05ddee2013-01-28 20:24:50 +00001618 umount(Mount_Point.c_str());
1619 if (Is_Mounted()) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001620 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001621 gui_msg(Msg(msg::kError, "fail_unmount=Failed to unmount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy5bf43922012-09-07 16:07:55 -04001622 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001623 LOGINFO("Unable to unmount '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001624 return false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001625 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -04001626 return true;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001627 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001628 } else {
1629 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001630 }
1631}
1632
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05001633bool TWPartition::ReMount(bool Display_Error) {
1634 if (UnMount(Display_Error))
1635 return Mount(Display_Error);
1636 return false;
1637}
1638
1639bool TWPartition::ReMount_RW(bool Display_Error) {
1640 // No need to remount if already mounted rw
1641 if (Is_File_System_Writable())
1642 return true;
1643
1644 bool ro = Mount_Read_Only;
1645 int flags = Mount_Flags;
1646
1647 Mount_Read_Only = false;
1648 Mount_Flags &= ~MS_RDONLY;
1649
1650 bool ret = ReMount(Display_Error);
1651
1652 Mount_Read_Only = ro;
1653 Mount_Flags = flags;
1654
1655 return ret;
1656}
1657
Gary Peck43acadf2012-11-21 21:19:01 -08001658bool TWPartition::Wipe(string New_File_System) {
Ethan Yonker726a0202014-12-16 20:01:38 -06001659 bool wiped = false, update_crypt = false, recreate_media = true;
Dees_Troy16c2b312013-01-15 16:51:18 +00001660 int check;
bigbiff7ba75002020-04-11 20:47:09 -04001661 fscrypt_encryption_policy policy;
Dees_Troy16c2b312013-01-15 16:51:18 +00001662
Dees_Troy38bd7602012-09-14 13:33:53 -04001663 if (!Can_Be_Wiped) {
Matt Mower3c366972015-12-25 19:28:31 -06001664 gui_msg(Msg(msg::kError, "cannot_wipe=Partition {1} cannot be wiped.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04001665 return false;
1666 }
1667
Dees_Troyc51f1f92012-09-20 15:32:13 -04001668 if (Mount_Point == "/cache")
Dees_Troy2673cec2013-04-02 20:22:16 +00001669 Log_Offset = 0;
Dees_Troyc51f1f92012-09-20 15:32:13 -04001670
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001671 if (Has_Data_Media && Current_File_System == New_File_System) {
Dees_Troy16c2b312013-01-15 16:51:18 +00001672 wiped = Wipe_Data_Without_Wiping_Media();
bigbiff7ba75002020-04-11 20:47:09 -04001673 if (Mount_Point == "/data" && TWFunc::get_cache_dir() == AB_CACHE_DIR && !TWFunc::Is_Mount_Wiped("/data")) {
1674 bool created = Recreate_AB_Cache_Dir(policy);
1675 if (created)
1676 gui_msg(Msg(msg::kWarning, "fbe_wipe_msg=WARNING: {1} wiped. FBE device should be booted into Android and not Recovery to set initial FBE policy after wipe.")(TWFunc::get_cache_dir()));
1677 }
Ethan Yonker5eac2222014-06-11 12:22:55 -05001678 recreate_media = false;
Dees_Troy16c2b312013-01-15 16:51:18 +00001679 } else {
Dees_Troy16c2b312013-01-15 16:51:18 +00001680 DataManager::GetValue(TW_RM_RF_VAR, check);
1681
Hashcodedabfd492013-08-29 22:45:30 -07001682 if (check || Use_Rm_Rf)
Dees_Troy16c2b312013-01-15 16:51:18 +00001683 wiped = Wipe_RMRF();
1684 else if (New_File_System == "ext4")
1685 wiped = Wipe_EXT4();
1686 else if (New_File_System == "ext2" || New_File_System == "ext3")
dianlujitao4879b372018-12-03 18:45:47 +08001687 wiped = Wipe_EXTFS(New_File_System);
Dees_Troy16c2b312013-01-15 16:51:18 +00001688 else if (New_File_System == "vfat")
1689 wiped = Wipe_FAT();
1690 else if (New_File_System == "exfat")
1691 wiped = Wipe_EXFAT();
1692 else if (New_File_System == "yaffs2")
1693 wiped = Wipe_MTD();
Dees_Troye5017042013-08-29 16:38:55 +00001694 else if (New_File_System == "f2fs")
1695 wiped = Wipe_F2FS();
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001696 else if (New_File_System == "ntfs")
1697 wiped = Wipe_NTFS();
Dees_Troy16c2b312013-01-15 16:51:18 +00001698 else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001699 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 +00001700 return false;
1701 }
1702 update_crypt = wiped;
bigbiff7ba75002020-04-11 20:47:09 -04001703 update_crypt = false;
Gary Pecke8bc5d72012-12-21 06:45:25 -08001704 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001705
Gary Pecke8bc5d72012-12-21 06:45:25 -08001706 if (wiped) {
Dees_Troy16c2b312013-01-15 16:51:18 +00001707 if (update_crypt) {
1708 Setup_File_System(false);
1709 if (Is_Encrypted && !Is_Decrypted) {
1710 // just wiped an encrypted partition back to its unencrypted state
1711 Is_Encrypted = false;
1712 Is_Decrypted = false;
1713 Decrypted_Block_Device = "";
1714 if (Mount_Point == "/data") {
1715 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1716 DataManager::SetValue(TW_IS_DECRYPTED, 0);
1717 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001718 }
1719 }
Ethan Yonker5eac2222014-06-11 12:22:55 -05001720
bigbiff7ba75002020-04-11 20:47:09 -04001721 // if (Is_Storage && Mount(false))
1722 // PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Gary Pecke8bc5d72012-12-21 06:45:25 -08001723 }
Matt Mower209c9632016-01-20 12:08:35 -06001724
Gary Pecke8bc5d72012-12-21 06:45:25 -08001725 return wiped;
Dees_Troy51a0e822012-09-05 15:24:24 -04001726}
1727
Gary Peck43acadf2012-11-21 21:19:01 -08001728bool TWPartition::Wipe() {
Gary Peck82599a82012-11-21 16:23:12 -08001729 if (Is_File_System(Current_File_System))
1730 return Wipe(Current_File_System);
1731 else
1732 return Wipe(Fstab_File_System);
Gary Peck43acadf2012-11-21 21:19:01 -08001733}
1734
Dees_Troye58d5262012-09-21 12:27:57 -04001735bool TWPartition::Wipe_AndSec(void) {
1736 if (!Has_Android_Secure)
1737 return false;
1738
Dees_Troye58d5262012-09-21 12:27:57 -04001739 if (!Mount(true))
1740 return false;
1741
Ethan Yonker74db1572015-10-28 12:44:49 -05001742 gui_msg(Msg("wiping=Wiping {1}")(Backup_Display_Name));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001743 TWFunc::removeDir(Mount_Point + "/.android_secure/", true);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001744 return true;
Dees_Troye58d5262012-09-21 12:27:57 -04001745}
1746
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001747bool TWPartition::Can_Repair() {
Ethan Yonkera2719152015-05-28 09:44:41 -05001748 if (Mount_Read_Only)
1749 return false;
Matt Mower18794c82015-11-11 16:22:45 -06001750 if (Current_File_System == "vfat" && TWFunc::Path_Exists("/sbin/fsck.fat"))
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001751 return true;
1752 else if ((Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") && TWFunc::Path_Exists("/sbin/e2fsck"))
1753 return true;
1754 else if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/fsck.exfat"))
1755 return true;
1756 else if (Current_File_System == "f2fs" && TWFunc::Path_Exists("/sbin/fsck.f2fs"))
1757 return true;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001758 else if (Current_File_System == "ntfs" && (TWFunc::Path_Exists("/sbin/ntfsfix") || TWFunc::Path_Exists("/sbin/fsck.ntfs")))
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001759 return true;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001760 return false;
1761}
1762
1763bool TWPartition::Repair() {
1764 string command;
1765
1766 if (Current_File_System == "vfat") {
Matt Mower18794c82015-11-11 16:22:45 -06001767 if (!TWFunc::Path_Exists("/sbin/fsck.fat")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001768 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.fat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001769 return false;
1770 }
1771 if (!UnMount(true))
1772 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001773 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.fat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001774 Find_Actual_Block_Device();
Matt Mower18794c82015-11-11 16:22:45 -06001775 command = "/sbin/fsck.fat -y " + Actual_Block_Device;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001776 LOGINFO("Repair command: %s\n", command.c_str());
1777 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001778 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001779 return true;
1780 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001781 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001782 return false;
1783 }
1784 }
1785 if (Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") {
1786 if (!TWFunc::Path_Exists("/sbin/e2fsck")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001787 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("e2fsck"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001788 return false;
1789 }
1790 if (!UnMount(true))
1791 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001792 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("e2fsck"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001793 Find_Actual_Block_Device();
Ethan Yonkera2719152015-05-28 09:44:41 -05001794 command = "/sbin/e2fsck -fp " + Actual_Block_Device;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001795 LOGINFO("Repair command: %s\n", command.c_str());
1796 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001797 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001798 return true;
1799 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001800 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001801 return false;
1802 }
1803 }
1804 if (Current_File_System == "exfat") {
1805 if (!TWFunc::Path_Exists("/sbin/fsck.exfat")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001806 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.exfat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001807 return false;
1808 }
1809 if (!UnMount(true))
1810 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001811 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.exfat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001812 Find_Actual_Block_Device();
1813 command = "/sbin/fsck.exfat " + Actual_Block_Device;
1814 LOGINFO("Repair command: %s\n", command.c_str());
1815 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001816 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001817 return true;
1818 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001819 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001820 return false;
1821 }
1822 }
1823 if (Current_File_System == "f2fs") {
1824 if (!TWFunc::Path_Exists("/sbin/fsck.f2fs")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001825 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.f2fs"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001826 return false;
1827 }
1828 if (!UnMount(true))
1829 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001830 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.f2fs"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001831 Find_Actual_Block_Device();
1832 command = "/sbin/fsck.f2fs " + Actual_Block_Device;
1833 LOGINFO("Repair command: %s\n", command.c_str());
1834 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001835 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001836 return true;
1837 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001838 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001839 return false;
1840 }
1841 }
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001842 if (Current_File_System == "ntfs") {
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001843 string Ntfsfix_Binary;
1844 if (TWFunc::Path_Exists("/sbin/ntfsfix"))
1845 Ntfsfix_Binary = "ntfsfix";
1846 else if (TWFunc::Path_Exists("/sbin/fsck.ntfs"))
1847 Ntfsfix_Binary = "fsck.ntfs";
1848 else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001849 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("ntfsfix"));
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001850 return false;
1851 }
1852 if (!UnMount(true))
1853 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001854 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)(Ntfsfix_Binary));
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001855 Find_Actual_Block_Device();
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001856 command = "/sbin/" + Ntfsfix_Binary + " " + Actual_Block_Device;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001857 LOGINFO("Repair command: %s\n", command.c_str());
1858 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001859 gui_msg("done=Done.");
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001860 return true;
1861 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001862 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001863 return false;
1864 }
1865 }
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001866 return false;
1867}
1868
Ethan Yonkera2719152015-05-28 09:44:41 -05001869bool TWPartition::Can_Resize() {
1870 if (Mount_Read_Only)
1871 return false;
1872 if ((Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") && TWFunc::Path_Exists("/sbin/resize2fs"))
1873 return true;
1874 return false;
1875}
1876
1877bool TWPartition::Resize() {
1878 string command;
1879
1880 if (Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") {
1881 if (!Can_Repair()) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001882 LOGINFO("Cannot resize %s because %s cannot be repaired before resizing.\n", Display_Name.c_str(), Display_Name.c_str());
1883 gui_msg(Msg(msg::kError, "cannot_resize=Cannot resize {1}.")(Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001884 return false;
1885 }
1886 if (!TWFunc::Path_Exists("/sbin/resize2fs")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001887 LOGINFO("resize2fs does not exist! Cannot resize!\n");
1888 gui_msg(Msg(msg::kError, "cannot_resize=Cannot resize {1}.")(Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001889 return false;
1890 }
1891 // Repair will unmount so no need to do it twice
Ethan Yonker74db1572015-10-28 12:44:49 -05001892 gui_msg(Msg("repair_resize=Repairing {1} before resizing.")( Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001893 if (!Repair())
1894 return false;
Ethan Yonker74db1572015-10-28 12:44:49 -05001895 gui_msg(Msg("resizing=Resizing {1} using {2}...")(Display_Name)("resize2fs"));
Ethan Yonkera2719152015-05-28 09:44:41 -05001896 Find_Actual_Block_Device();
1897 command = "/sbin/resize2fs " + Actual_Block_Device;
1898 if (Length != 0) {
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001899 unsigned long long Actual_Size = IOCTL_Get_Block_Size();
1900 if (Actual_Size == 0)
1901 return false;
Ethan Yonkera2719152015-05-28 09:44:41 -05001902
Ethan Yonkera2719152015-05-28 09:44:41 -05001903 unsigned long long Block_Count;
1904 if (Length < 0) {
1905 // Reduce overall size by this length
1906 Block_Count = (Actual_Size / 1024LLU) - ((unsigned long long)(Length * -1) / 1024LLU);
1907 } else {
1908 // This is the size, not a size reduction
1909 Block_Count = ((unsigned long long)(Length) / 1024LLU);
1910 }
1911 char temp[256];
1912 sprintf(temp, "%llu", Block_Count);
1913 command += " ";
1914 command += temp;
1915 command += "K";
1916 }
1917 LOGINFO("Resize command: %s\n", command.c_str());
1918 if (TWFunc::Exec_Cmd(command) == 0) {
1919 Update_Size(true);
Ethan Yonker74db1572015-10-28 12:44:49 -05001920 gui_msg("done=Done.");
Ethan Yonkera2719152015-05-28 09:44:41 -05001921 return true;
1922 } else {
1923 Update_Size(true);
Ethan Yonker74db1572015-10-28 12:44:49 -05001924 gui_msg(Msg(msg::kError, "unable_resize=Unable to resize {1}.")(Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001925 return false;
1926 }
1927 }
1928 return false;
1929}
1930
bigbiffce8f83c2015-12-12 18:30:21 -05001931bool TWPartition::Backup(PartitionSettings *part_settings, pid_t *tar_fork_pid) {
1932 if (Backup_Method == BM_FILES)
1933 return Backup_Tar(part_settings, tar_fork_pid);
1934 else if (Backup_Method == BM_DD)
1935 return Backup_Image(part_settings);
1936 else if (Backup_Method == BM_FLASH_UTILS)
1937 return Backup_Dump_Image(part_settings);
Dees_Troy2673cec2013-04-02 20:22:16 +00001938 LOGERR("Unknown backup method for '%s'\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001939 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001940}
1941
bigbiffce8f83c2015-12-12 18:30:21 -05001942bool TWPartition::Restore(PartitionSettings *part_settings) {
Matt Mower3c366972015-12-25 19:28:31 -06001943 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, gui_parse_text("{@restoring_hdr}"));
Ethan Yonkere080c1f2016-09-19 13:50:25 -05001944 LOGINFO("Restore filename is: %s/%s\n", part_settings->Backup_Folder.c_str(), Backup_FileName.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001945
bigbiffce8f83c2015-12-12 18:30:21 -05001946 string Restore_File_System = Get_Restore_File_System(part_settings);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001947
1948 if (Is_File_System(Restore_File_System))
bigbiffce8f83c2015-12-12 18:30:21 -05001949 return Restore_Tar(part_settings);
1950 else if (Is_Image(Restore_File_System))
1951 return Restore_Image(part_settings);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001952
1953 LOGERR("Unknown restore method for '%s'\n", Mount_Point.c_str());
1954 return false;
1955}
1956
bigbiffce8f83c2015-12-12 18:30:21 -05001957string TWPartition::Get_Restore_File_System(PartitionSettings *part_settings) {
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001958 size_t first_period, second_period;
1959 string Restore_File_System;
1960
Gary Peck43acadf2012-11-21 21:19:01 -08001961 // Parse backup filename to extract the file system before wiping
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05001962 first_period = Backup_FileName.find(".");
Gary Peck43acadf2012-11-21 21:19:01 -08001963 if (first_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001964 LOGERR("Unable to find file system (first period).\n");
thatd43bf2d2014-09-21 23:13:02 +02001965 return string();
Gary Peck43acadf2012-11-21 21:19:01 -08001966 }
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05001967 Restore_File_System = Backup_FileName.substr(first_period + 1, Backup_FileName.size() - first_period - 1);
Gary Peck43acadf2012-11-21 21:19:01 -08001968 second_period = Restore_File_System.find(".");
1969 if (second_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001970 LOGERR("Unable to find file system (second period).\n");
thatd43bf2d2014-09-21 23:13:02 +02001971 return string();
Gary Peck43acadf2012-11-21 21:19:01 -08001972 }
1973 Restore_File_System.resize(second_period);
Matt Mowera0cd91d2016-12-30 18:21:42 -06001974 LOGINFO("Restore file system is: '%s'.\n", Restore_File_System.c_str());
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001975 return Restore_File_System;
Dees_Troy51a0e822012-09-05 15:24:24 -04001976}
1977
1978string TWPartition::Backup_Method_By_Name() {
bigbiffce8f83c2015-12-12 18:30:21 -05001979 if (Backup_Method == BM_NONE)
Dees_Troy38bd7602012-09-14 13:33:53 -04001980 return "none";
bigbiffce8f83c2015-12-12 18:30:21 -05001981 else if (Backup_Method == BM_FILES)
Dees_Troy38bd7602012-09-14 13:33:53 -04001982 return "files";
bigbiffce8f83c2015-12-12 18:30:21 -05001983 else if (Backup_Method == BM_DD)
Dees_Troy38bd7602012-09-14 13:33:53 -04001984 return "dd";
bigbiffce8f83c2015-12-12 18:30:21 -05001985 else if (Backup_Method == BM_FLASH_UTILS)
Dees_Troy38bd7602012-09-14 13:33:53 -04001986 return "flash_utils";
1987 else
1988 return "undefined";
1989 return "ERROR!";
Dees_Troy51a0e822012-09-05 15:24:24 -04001990}
1991
1992bool TWPartition::Decrypt(string Password) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001993 LOGINFO("STUB TWPartition::Decrypt, password: '%s'\n", Password.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001994 // Is this needed?
Dees_Troy51a0e822012-09-05 15:24:24 -04001995 return 1;
1996}
1997
1998bool TWPartition::Wipe_Encryption() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001999 bool Save_Data_Media = Has_Data_Media;
Ethan Yonker93382822018-11-01 15:25:31 -05002000 bool ret = false;
2001 BasePartition* base_partition = make_partition();
2002
2003 if (!base_partition->PreWipeEncryption())
2004 goto exit;
Dees_Troy38bd7602012-09-14 13:33:53 -04002005
dianlujitao4879b372018-12-03 18:45:47 +08002006 Find_Actual_Block_Device();
2007 if (!Is_Present) {
2008 LOGINFO("Block device not present, cannot format %s.\n", Display_Name.c_str());
2009 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
2010 return false;
2011 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002012
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06002013#ifdef TW_INCLUDE_CRYPTO
bigbiff7ba75002020-04-11 20:47:09 -04002014 if (!UnMount(true))
2015 return false;
Ethan Yonker1b190162016-12-05 15:25:19 -06002016 if (Is_Decrypted && !Decrypted_Block_Device.empty()) {
Matt Mower2b18a532015-02-20 16:58:05 -06002017 if (delete_crypto_blk_dev((char*)("userdata")) != 0) {
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06002018 LOGERR("Error deleting crypto block device, continuing anyway.\n");
2019 }
2020 }
2021#endif
dianlujitao4879b372018-12-03 18:45:47 +08002022 Has_Data_Media = false;
Ethan Yonker93382822018-11-01 15:25:31 -05002023 Decrypted_Block_Device = "";
Dees_Troy74fb2e92013-04-15 14:35:47 +00002024 Is_Decrypted = false;
2025 Is_Encrypted = false;
Gary Pecke8bc5d72012-12-21 06:45:25 -08002026 if (Wipe(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002027 Has_Data_Media = Save_Data_Media;
bigbiff7ba75002020-04-11 20:47:09 -04002028 // if (Has_Data_Media && !Symlink_Mount_Point.empty()) {
2029 // if (Mount(false))
2030 // PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
2031 // }
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06002032 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
Ethan Yonker83e82572014-04-04 10:59:28 -05002033#ifndef TW_OEM_BUILD
bigbiff7ba75002020-04-11 20:47:09 -04002034 if (Is_FBE)
2035 gui_msg(Msg(msg::kWarning, "fbe_wipe_msg=WARNING: {1} wiped. FBE device should be booted into Android and not Recovery to set initial FBE policy after wipe.")(TWFunc::get_cache_dir()));
2036 else
2037 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 -05002038#endif
Ethan Yonker93382822018-11-01 15:25:31 -05002039 ret = true;
2040 if (!Key_Directory.empty())
2041 ret = PartitionManager.Wipe_By_Path(Key_Directory);
2042 if (ret)
2043 ret = base_partition->PostWipeEncryption();
2044 goto exit;
Dees_Troy38bd7602012-09-14 13:33:53 -04002045 } else {
2046 Has_Data_Media = Save_Data_Media;
Ethan Yonker74db1572015-10-28 12:44:49 -05002047 gui_err("format_data_err=Unable to format to remove encryption.");
Ethan Yonker726a0202014-12-16 20:01:38 -06002048 if (Has_Data_Media && Mount(false))
2049 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Ethan Yonker93382822018-11-01 15:25:31 -05002050 goto exit;
Dees_Troy38bd7602012-09-14 13:33:53 -04002051 }
Ethan Yonker93382822018-11-01 15:25:31 -05002052exit:
2053 delete base_partition;
2054 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04002055}
2056
2057void TWPartition::Check_FS_Type() {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05002058 const char* type;
2059 blkid_probe pr;
Dees_Troy5bf43922012-09-07 16:07:55 -04002060
Dees_Troy68cab492012-12-12 19:29:35 +00002061 if (Fstab_File_System == "yaffs2" || Fstab_File_System == "mtd" || Fstab_File_System == "bml" || Ignore_Blkid)
2062 return; // Running blkid on some mtd devices causes a massive crash or needs to be skipped
Dees_Troy5bf43922012-09-07 16:07:55 -04002063
Dees_Troy38bd7602012-09-14 13:33:53 -04002064 Find_Actual_Block_Device();
Dees_Troy8170a922012-09-18 15:40:25 -04002065 if (!Is_Present)
2066 return;
Dees_Troy51127312012-09-08 13:08:49 -04002067
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05002068 pr = blkid_new_probe_from_filename(Actual_Block_Device.c_str());
2069 if (blkid_do_fullprobe(pr)) {
2070 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00002071 LOGINFO("Can't probe device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05002072 return;
Dees_Troy5bf43922012-09-07 16:07:55 -04002073 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02002074
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05002075 if (blkid_probe_lookup_value(pr, "TYPE", &type, NULL) < 0) {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05002076 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00002077 LOGINFO("can't find filesystem on device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05002078 return;
2079 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02002080
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05002081 Current_File_System = type;
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02002082 blkid_free_probe(pr);
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06002083 if (fs_flags.size() > 1) {
2084 std::vector<partition_fs_flags_struct>::iterator iter;
2085 std::vector<partition_fs_flags_struct>::iterator found = fs_flags.begin();
2086
2087 for (iter = fs_flags.begin(); iter != fs_flags.end(); iter++) {
2088 if (iter->File_System == Current_File_System) {
2089 found = iter;
2090 break;
2091 }
2092 }
2093 // If we don't find a match, we default the flags to the first set of flags that we received from the fstab
2094 if (Mount_Flags != found->Mount_Flags || Mount_Options != found->Mount_Options) {
2095 Mount_Flags = found->Mount_Flags;
2096 Mount_Options = found->Mount_Options;
2097 LOGINFO("Mount_Flags: %i, Mount_Options: %s\n", Mount_Flags, Mount_Options.c_str());
2098 }
2099 }
Dees_Troy51a0e822012-09-05 15:24:24 -04002100}
2101
dianlujitao4879b372018-12-03 18:45:47 +08002102bool TWPartition::Wipe_EXTFS(string File_System) {
bigbiff7ba75002020-04-11 20:47:09 -04002103 if (!UnMount(true))
2104 return false;
2105
dianlujitao4879b372018-12-03 18:45:47 +08002106#if PLATFORM_SDK_VERSION < 28
2107 if (!TWFunc::Path_Exists("/sbin/mke2fs"))
2108#else
2109 if (!TWFunc::Path_Exists("/sbin/mke2fs") || !TWFunc::Path_Exists("/sbin/e2fsdroid"))
2110#endif
Dees_Troy38bd7602012-09-14 13:33:53 -04002111 return Wipe_RMRF();
2112
dianlujitao4879b372018-12-03 18:45:47 +08002113 int ret;
2114 bool NeedPreserveFooter = true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002115
Ethan Yonker25f20c12014-10-14 09:04:43 -05002116 Find_Actual_Block_Device();
2117 if (!Is_Present) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002118 LOGINFO("Block device not present, cannot wipe %s.\n", Display_Name.c_str());
2119 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Ethan Yonker25f20c12014-10-14 09:04:43 -05002120 return false;
2121 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002122
dianlujitao4879b372018-12-03 18:45:47 +08002123 /**
2124 * On decrypted devices, IOCTL_Get_Block_Size calculates size on device mapper,
2125 * so there's no need to preserve footer.
2126 */
2127 if ((Is_Decrypted && !Decrypted_Block_Device.empty()) ||
2128 Crypto_Key_Location != "footer") {
2129 NeedPreserveFooter = false;
2130 }
2131
2132 unsigned long long dev_sz = TWFunc::IOCTL_Get_Block_Size(Actual_Block_Device.c_str());
2133 if (!dev_sz)
2134 return false;
2135
2136 if (NeedPreserveFooter)
2137 Length < 0 ? dev_sz += Length : dev_sz -= CRYPT_FOOTER_OFFSET;
2138
Dees Troy9a4d7402019-03-21 14:17:28 -04002139 char dout[16];
2140 sprintf(dout, "%llu", dev_sz / 4096);
2141
2142 //string size_str =to_string(dev_sz / 4096);
2143 string size_str = dout;
dianlujitao4879b372018-12-03 18:45:47 +08002144 string Command;
2145
2146 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mke2fs"));
2147
2148 // Execute mke2fs to create empty ext4 filesystem
2149 Command = "mke2fs -t " + File_System + " -b 4096 " + Actual_Block_Device + " " + size_str;
2150 LOGINFO("mke2fs command: %s\n", Command.c_str());
2151 ret = TWFunc::Exec_Cmd(Command);
2152 if (ret) {
2153 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
2154 return false;
2155 }
2156
2157 if (TWFunc::Path_Exists("/sbin/e2fsdroid")) {
dianlujitao3cf05a52019-05-15 15:33:27 +08002158 const string& File_Contexts_Entry = (Mount_Point == "/system_root" ? "/" : Mount_Point);
2159 char *secontext = NULL;
2160 if (!selinux_handle || selabel_lookup(selinux_handle, &secontext, File_Contexts_Entry.c_str(), S_IFDIR) < 0) {
2161 LOGINFO("Cannot lookup security context for '%s'\n", Mount_Point.c_str());
2162 } else {
2163 // Execute e2fsdroid to initialize selinux context
2164 Command = "e2fsdroid -e -S /file_contexts -a " + File_Contexts_Entry + " " + Actual_Block_Device;
2165 LOGINFO("e2fsdroid command: %s\n", Command.c_str());
2166 ret = TWFunc::Exec_Cmd(Command);
2167 if (ret) {
2168 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
2169 return false;
2170 }
dianlujitao4879b372018-12-03 18:45:47 +08002171 }
2172 } else {
2173 LOGINFO("e2fsdroid not present\n");
2174 }
2175
2176 if (NeedPreserveFooter)
2177 Wipe_Crypto_Key();
2178 Current_File_System = File_System;
2179 Recreate_AndSec_Folder();
2180 gui_msg("done=Done.");
2181 return true;
2182}
2183
2184bool TWPartition::Wipe_EXT4() {
2185#ifdef USE_EXT4
Ethan Yonkerf27497f2014-02-09 11:48:33 -06002186 int ret;
dianlujitao4879b372018-12-03 18:45:47 +08002187 bool NeedPreserveFooter = true;
2188
bigbiff7ba75002020-04-11 20:47:09 -04002189 if (!UnMount(true))
2190 return false;
2191
dianlujitao4879b372018-12-03 18:45:47 +08002192 Find_Actual_Block_Device();
2193 if (!Is_Present) {
2194 LOGINFO("Block device not present, cannot wipe %s.\n", Display_Name.c_str());
2195 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
2196 return false;
2197 }
dianlujitao4879b372018-12-03 18:45:47 +08002198
2199 /**
2200 * On decrypted devices, IOCTL_Get_Block_Size calculates size on device mapper,
2201 * so there's no need to preserve footer.
2202 */
2203 if ((Is_Decrypted && !Decrypted_Block_Device.empty()) ||
2204 Crypto_Key_Location != "footer") {
2205 NeedPreserveFooter = false;
2206 }
2207
2208 unsigned long long dev_sz = TWFunc::IOCTL_Get_Block_Size(Actual_Block_Device.c_str());
2209 if (!dev_sz)
2210 return false;
2211
2212 if (NeedPreserveFooter)
2213 Length < 0 ? dev_sz += Length : dev_sz -= CRYPT_FOOTER_OFFSET;
2214
Ethan Yonkerf27497f2014-02-09 11:48:33 -06002215 char *secontext = NULL;
2216
Greg Wallace4b44fef2015-12-29 15:33:24 -05002217 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("make_ext4fs"));
Ethan Yonkerf27497f2014-02-09 11:48:33 -06002218
Dees Troy99c8dbf2014-03-10 16:53:58 +00002219 if (!selinux_handle || selabel_lookup(selinux_handle, &secontext, Mount_Point.c_str(), S_IFDIR) < 0) {
Ethan Yonkerf27497f2014-02-09 11:48:33 -06002220 LOGINFO("Cannot lookup security context for '%s'\n", Mount_Point.c_str());
dianlujitao4879b372018-12-03 18:45:47 +08002221 ret = make_ext4fs(Actual_Block_Device.c_str(), dev_sz, Mount_Point.c_str(), NULL);
Ethan Yonkerf27497f2014-02-09 11:48:33 -06002222 } else {
dianlujitao4879b372018-12-03 18:45:47 +08002223 ret = make_ext4fs(Actual_Block_Device.c_str(), dev_sz, Mount_Point.c_str(), selinux_handle);
Ethan Yonkerf27497f2014-02-09 11:48:33 -06002224 }
2225 if (ret != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002226 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troya95f55c2013-08-17 13:14:43 +00002227 return false;
2228 } else {
dianlujitao4879b372018-12-03 18:45:47 +08002229 if (NeedPreserveFooter)
2230 Wipe_Crypto_Key();
bigbiff bigbiffc49d7062013-10-11 20:28:00 -04002231 string sedir = Mount_Point + "/lost+found";
2232 PartitionManager.Mount_By_Path(sedir.c_str(), true);
2233 rmdir(sedir.c_str());
2234 mkdir(sedir.c_str(), S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP);
Dees_Troya95f55c2013-08-17 13:14:43 +00002235 return true;
2236 }
2237#else
dianlujitao4879b372018-12-03 18:45:47 +08002238 return Wipe_EXTFS("ext4");
Dees_Troya95f55c2013-08-17 13:14:43 +00002239#endif
Dees_Troy51a0e822012-09-05 15:24:24 -04002240}
2241
2242bool TWPartition::Wipe_FAT() {
Vojtech Bocek05534202013-09-11 08:11:56 +02002243 string command;
Dees_Troy38bd7602012-09-14 13:33:53 -04002244
bigbiff7ba75002020-04-11 20:47:09 -04002245 if (!UnMount(true))
2246 return false;
Dees_Troy38bd7602012-09-14 13:33:53 -04002247
bigbiff7ba75002020-04-11 20:47:09 -04002248 if (TWFunc::Path_Exists("/sbin/mkfs.fat")) {
Greg Wallace4b44fef2015-12-29 15:33:24 -05002249 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mkfs.fat"));
Dees_Troy38bd7602012-09-14 13:33:53 -04002250 Find_Actual_Block_Device();
Matt Mower18794c82015-11-11 16:22:45 -06002251 command = "mkfs.fat " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02002252 if (TWFunc::Exec_Cmd(command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08002253 Current_File_System = "vfat";
Dees_Troye58d5262012-09-21 12:27:57 -04002254 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05002255 gui_msg("done=Done.");
Dees_Troy38bd7602012-09-14 13:33:53 -04002256 return true;
2257 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002258 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04002259 return false;
2260 }
2261 return true;
2262 }
2263 else
2264 return Wipe_RMRF();
2265
2266 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04002267}
2268
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002269bool TWPartition::Wipe_EXFAT() {
Vojtech Bocek05534202013-09-11 08:11:56 +02002270 string command;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002271
bigbiff7ba75002020-04-11 20:47:09 -04002272 if (!UnMount(true))
2273 return false;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002274 if (TWFunc::Path_Exists("/sbin/mkexfatfs")) {
Greg Wallace4b44fef2015-12-29 15:33:24 -05002275 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mkexfatfs"));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002276 Find_Actual_Block_Device();
2277 command = "mkexfatfs " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02002278 if (TWFunc::Exec_Cmd(command) == 0) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002279 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05002280 gui_msg("done=Done.");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002281 return true;
2282 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002283 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002284 return false;
2285 }
2286 return true;
2287 }
2288 return false;
2289}
2290
Dees_Troy38bd7602012-09-14 13:33:53 -04002291bool TWPartition::Wipe_MTD() {
2292 if (!UnMount(true))
2293 return false;
2294
Greg Wallace4b44fef2015-12-29 15:33:24 -05002295 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("MTD"));
Dees_Troy38bd7602012-09-14 13:33:53 -04002296
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02002297 mtd_scan_partitions();
2298 const MtdPartition* mtd = mtd_find_partition_by_name(MTD_Name.c_str());
2299 if (mtd == NULL) {
2300 LOGERR("No mtd partition named '%s'", MTD_Name.c_str());
2301 return false;
2302 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002303
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02002304 MtdWriteContext* ctx = mtd_write_partition(mtd);
2305 if (ctx == NULL) {
2306 LOGERR("Can't write '%s', failed to format.", MTD_Name.c_str());
2307 return false;
2308 }
2309 if (mtd_erase_blocks(ctx, -1) == -1) {
2310 mtd_write_close(ctx);
2311 LOGERR("Failed to format '%s'", MTD_Name.c_str());
2312 return false;
2313 }
2314 if (mtd_write_close(ctx) != 0) {
2315 LOGERR("Failed to close '%s'", MTD_Name.c_str());
2316 return false;
2317 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08002318 Current_File_System = "yaffs2";
Dees_Troye58d5262012-09-21 12:27:57 -04002319 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05002320 gui_msg("done=Done.");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02002321 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002322}
2323
2324bool TWPartition::Wipe_RMRF() {
Dees_Troy38bd7602012-09-14 13:33:53 -04002325 if (!Mount(true))
2326 return false;
Ethan Yonker726a0202014-12-16 20:01:38 -06002327 // This is the only wipe that leaves the partition mounted, so we
2328 // must manually remove the partition from MTP if it is a storage
2329 // partition.
2330 if (Is_Storage)
2331 PartitionManager.Remove_MTP_Storage(MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04002332
Ethan Yonker74db1572015-10-28 12:44:49 -05002333 gui_msg(Msg("remove_all=Removing all files under '{1}'")(Mount_Point));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002334 TWFunc::removeDir(Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04002335 Recreate_AndSec_Folder();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002336 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002337}
2338
Dees_Troye5017042013-08-29 16:38:55 +00002339bool TWPartition::Wipe_F2FS() {
bigbiff7ba75002020-04-11 20:47:09 -04002340 std::string command;
2341 std::string f2fs_bin;
Dees_Troye5017042013-08-29 16:38:55 +00002342
bigbiff7ba75002020-04-11 20:47:09 -04002343 if (!UnMount(true))
2344 return false;
dianlujitao4879b372018-12-03 18:45:47 +08002345
bigbiff7ba75002020-04-11 20:47:09 -04002346 if (TWFunc::Path_Exists("/sbin/mkfs.f2fs"))
2347 f2fs_bin = "/sbin/mkfs.f2fs";
2348 else if (TWFunc::Path_Exists("/sbin/make_f2fs"))
2349 f2fs_bin = "/sbin/make_f2fs";
2350 else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002351 LOGINFO("mkfs.f2fs binary not found, using rm -rf to wipe.\n");
Dees_Troye5017042013-08-29 16:38:55 +00002352 return Wipe_RMRF();
2353 }
bigbiff7ba75002020-04-11 20:47:09 -04002354
2355 bool NeedPreserveFooter = true;
2356
2357 Find_Actual_Block_Device();
2358 if (!Is_Present) {
2359 LOGINFO("Block device not present, cannot wipe %s.\n", Display_Name.c_str());
2360 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
2361 return false;
2362 }
2363
2364 unsigned long long dev_sz = TWFunc::IOCTL_Get_Block_Size(Actual_Block_Device.c_str());
2365 if (!dev_sz)
2366 return false;
2367
2368 if (NeedPreserveFooter)
2369 Length < 0 ? dev_sz += Length : dev_sz -= CRYPT_FOOTER_OFFSET;
2370
2371 char dev_sz_str[48];
2372 sprintf(dev_sz_str, "%llu", (dev_sz / 4096));
2373 command = f2fs_bin + " -d1 -f -O encrypt -O quota -O verity -w 4096 " + Actual_Block_Device + " " + dev_sz_str;
2374 if (TWFunc::Path_Exists("/sbin/sload.f2fs")) {
2375 command += " && sload.f2fs -t /data " + Actual_Block_Device;
2376 }
2377
2378 /**
2379 * On decrypted devices, IOCTL_Get_Block_Size calculates size on device mapper,
2380 * so there's no need to preserve footer.
2381 */
2382 if ((Is_Decrypted && !Decrypted_Block_Device.empty()) ||
2383 Crypto_Key_Location != "footer") {
2384 NeedPreserveFooter = false;
2385 }
2386 LOGINFO("mkfs.f2fs command: %s\n", f2fs_bin.c_str());
2387 if (TWFunc::Exec_Cmd(command) == 0) {
2388 if (NeedPreserveFooter)
2389 Wipe_Crypto_Key();
2390 Recreate_AndSec_Folder();
2391 gui_msg("done=Done.");
2392 return true;
2393 } else {
2394 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
2395 return false;
2396 }
2397 return true;
Dees_Troye5017042013-08-29 16:38:55 +00002398}
2399
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002400bool TWPartition::Wipe_NTFS() {
2401 string command;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002402 string Ntfsmake_Binary;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002403
bigbiff7ba75002020-04-11 20:47:09 -04002404 if (!UnMount(true))
2405 return false;
2406
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002407 if (TWFunc::Path_Exists("/sbin/mkntfs"))
2408 Ntfsmake_Binary = "mkntfs";
2409 else if (TWFunc::Path_Exists("/sbin/mkfs.ntfs"))
2410 Ntfsmake_Binary = "mkfs.ntfs";
2411 else
2412 return false;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002413
Greg Wallace4b44fef2015-12-29 15:33:24 -05002414 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)(Ntfsmake_Binary));
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002415 Find_Actual_Block_Device();
2416 command = "/sbin/" + Ntfsmake_Binary + " " + Actual_Block_Device;
2417 if (TWFunc::Exec_Cmd(command) == 0) {
2418 Recreate_AndSec_Folder();
2419 gui_msg("done=Done.");
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002420 return true;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002421 } else {
2422 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
2423 return false;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002424 }
2425 return false;
2426}
2427
Dees_Troy51a0e822012-09-05 15:24:24 -04002428bool TWPartition::Wipe_Data_Without_Wiping_Media() {
Ethan Yonker83e82572014-04-04 10:59:28 -05002429#ifdef TW_OEM_BUILD
2430 // In an OEM Build we want to do a full format
2431 return Wipe_Encryption();
2432#else
Ethan Yonker66a19492015-12-10 10:19:45 -06002433 bool ret = false;
Dees_Troy38bd7602012-09-14 13:33:53 -04002434
Dees_Troy38bd7602012-09-14 13:33:53 -04002435 if (!Mount(true))
2436 return false;
2437
Ethan Yonker74db1572015-10-28 12:44:49 -05002438 gui_msg("wiping_data=Wiping data without wiping /data/media ...");
Ethan Yonker66a19492015-12-10 10:19:45 -06002439 ret = Wipe_Data_Without_Wiping_Media_Func(Mount_Point + "/");
2440 if (ret)
2441 gui_msg("done=Done.");
2442 return ret;
2443#endif // ifdef TW_OEM_BUILD
2444}
2445
bigbiff7ba75002020-04-11 20:47:09 -04002446bool TWPartition::Recreate_AB_Cache_Dir(const fscrypt_encryption_policy &policy) {
2447 struct passwd pd;
2448 struct passwd *pwdptr = &pd;
2449 struct passwd *tempPd;
2450 char pwdBuf[512];
2451 int uid = 0, gid = 0;
2452
2453 if ((getpwnam_r("system", pwdptr, pwdBuf, sizeof(pwdBuf), &tempPd)) != 0) {
2454 LOGERR("unable to get system user id\n");
2455 return false;
2456 } else {
2457 struct group grp;
2458 struct group *grpptr = &grp;
2459 struct group *tempGrp;
2460 char grpBuf[512];
2461
2462 if ((getgrnam_r("cache", grpptr, grpBuf, sizeof(grpBuf), &tempGrp)) != 0) {
2463 LOGERR("unable to get cache group id\n");
2464 return false;
2465 } else {
2466 uid = pd.pw_uid;
2467 gid = grp.gr_gid;
2468
2469 if (!TWFunc::Create_Dir_Recursive(AB_CACHE_DIR, S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP, uid, gid)) {
2470 LOGERR("Unable to recreate %s\n", AB_CACHE_DIR);
2471 return false;
2472 }
2473 if (setfilecon(AB_CACHE_DIR, "u:object_r:cache_file:s0") != 0) {
2474 LOGERR("Unable to set contexts for %s\n", AB_CACHE_DIR);
2475 return false;
2476 }
2477 char policy_hex[FS_KEY_DESCRIPTOR_SIZE_HEX];
2478 policy_to_hex(policy.master_key_descriptor, policy_hex);
2479 LOGINFO("setting policy for %s: %s\n", policy_hex, AB_CACHE_DIR);
2480 if (sizeof(policy.master_key_descriptor) > 0) {
2481 if (!TWFunc::Set_Encryption_Policy(AB_CACHE_DIR, policy)) {
2482 LOGERR("Unable to set encryption policy for %s\n", AB_CACHE_DIR);
2483 LOGINFO("Removing %s\n", AB_CACHE_DIR);
2484 int ret = TWFunc::removeDir(AB_CACHE_DIR, true);
2485 if (ret == -1) {
2486 LOGERR("Unable to remove %s\n", AB_CACHE_DIR);
2487 }
2488 return false;
2489 }
2490 } else {
2491 LOGERR("Not setting empty policy to %s\n", AB_CACHE_DIR);
2492 return false;
2493 }
2494 }
2495 }
2496 return true;
2497}
2498
Ethan Yonker66a19492015-12-10 10:19:45 -06002499bool TWPartition::Wipe_Data_Without_Wiping_Media_Func(const string& parent __unused) {
2500 string dir;
Dees_Troy38bd7602012-09-14 13:33:53 -04002501
2502 DIR* d;
Ethan Yonker66a19492015-12-10 10:19:45 -06002503 d = opendir(parent.c_str());
Dees_Troy16b74352012-11-14 22:27:31 +00002504 if (d != NULL) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002505 struct dirent* de;
2506 while ((de = readdir(d)) != NULL) {
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002507 if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) continue;
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05002508
Ethan Yonker66a19492015-12-10 10:19:45 -06002509 dir = parent;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002510 dir.append(de->d_name);
Ethan Yonker3fdcda42016-11-30 12:29:37 -06002511 if (wipe_exclusions.check_skip_dirs(dir)) {
Ethan Yonker66a19492015-12-10 10:19:45 -06002512 LOGINFO("skipped '%s'\n", dir.c_str());
2513 continue;
2514 }
Dees_Troyce675462013-01-09 19:48:21 +00002515 if (de->d_type == DT_DIR) {
Ethan Yonker66a19492015-12-10 10:19:45 -06002516 dir.append("/");
2517 if (!Wipe_Data_Without_Wiping_Media_Func(dir)) {
2518 closedir(d);
2519 return false;
2520 }
2521 rmdir(dir.c_str());
bigbiff bigbiff98f1f902013-01-19 18:46:13 -05002522 } 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 +03002523 if (unlink(dir.c_str()) != 0)
Ethan Yonker79f88bd2016-12-09 14:52:12 -06002524 LOGINFO("Unable to unlink '%s': %s\n", dir.c_str(), strerror(errno));
Dees_Troyce675462013-01-09 19:48:21 +00002525 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002526 }
2527 closedir(d);
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05002528
Dees_Troy16b74352012-11-14 22:27:31 +00002529 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -04002530 }
Ethan Yonker74db1572015-10-28 12:44:49 -05002531 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy16b74352012-11-14 22:27:31 +00002532 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04002533}
2534
dianlujitao4879b372018-12-03 18:45:47 +08002535void TWPartition::Wipe_Crypto_Key() {
2536 Find_Actual_Block_Device();
2537 if (Crypto_Key_Location.empty())
2538 return;
2539 else if (Crypto_Key_Location == "footer") {
2540 int fd = open(Actual_Block_Device.c_str(), O_RDWR);
2541 if (fd < 0) {
2542 gui_print_color("warning", "Unable to open '%s' to wipe crypto key\n", Actual_Block_Device.c_str());
2543 return;
2544 }
2545
2546 unsigned int block_count;
2547 if ((ioctl(fd, BLKGETSIZE, &block_count)) == -1) {
2548 gui_print_color("warning", "Unable to get block size for wiping crypto footer.\n");
2549 } else {
2550 int newlen = Length < 0 ? -Length : CRYPT_FOOTER_OFFSET;
2551 off64_t offset = ((off64_t)block_count * 512) - newlen;
2552 if (lseek64(fd, offset, SEEK_SET) == -1) {
2553 gui_print_color("warning", "Unable to lseek64 for wiping crypto footer.\n");
2554 } else {
2555 void* buffer = malloc(newlen);
2556 if (!buffer) {
2557 gui_print_color("warning", "Failed to malloc for wiping crypto footer.\n");
2558 } else {
2559 memset(buffer, 0, newlen);
2560 int ret = write(fd, buffer, newlen);
2561 if (ret != newlen) {
2562 gui_print_color("warning", "Failed to wipe crypto footer.\n");
2563 } else {
2564 LOGINFO("Successfully wiped crypto footer.\n");
2565 }
2566 free(buffer);
2567 }
2568 }
2569 }
2570 close(fd);
2571 } else {
2572 if (TWFunc::IOCTL_Get_Block_Size(Crypto_Key_Location.c_str()) >= 16384LLU) {
2573 string Command = "dd of='" + Crypto_Key_Location + "' if=/dev/zero bs=16384 count=1";
2574 TWFunc::Exec_Cmd(Command);
2575 } else {
2576 LOGINFO("Crypto key location reports size < 16K so not wiping crypto footer.\n");
2577 }
2578 }
2579}
2580
bigbiffce8f83c2015-12-12 18:30:21 -05002581bool TWPartition::Backup_Tar(PartitionSettings *part_settings, pid_t *tar_fork_pid) {
Ethan Yonker472f5062016-02-25 13:47:30 -06002582 string Full_FileName;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002583 twrpTar tar;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05002584
Dees_Troy43d8b002012-09-17 16:00:01 -04002585 if (!Mount(true))
2586 return false;
2587
Ian Macdonalda3b25a32019-06-16 22:39:14 +02002588 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Backup_Display_Name, gui_parse_text("{@backing}"));
Ethan Yonker74db1572015-10-28 12:44:49 -05002589 gui_msg(Msg("backing_up=Backing up {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002590
Ethan Yonker472f5062016-02-25 13:47:30 -06002591 DataManager::GetValue(TW_USE_COMPRESSION_VAR, tar.use_compression);
Matt Mowerbb81e5d2014-03-20 18:05:41 -05002592
Dees_Troy83bd4832013-05-04 12:39:56 +00002593#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
Ethan Yonker472f5062016-02-25 13:47:30 -06002594 if (Can_Encrypt_Backup) {
2595 DataManager::GetValue("tw_encrypt_backup", tar.use_encryption);
2596 if (tar.use_encryption) {
2597 if (Use_Userdata_Encryption)
2598 tar.userdata_encryption = tar.use_encryption;
2599 string Password;
2600 DataManager::GetValue("tw_backup_password", Password);
2601 tar.setpassword(Password);
2602 } else {
2603 tar.use_encryption = 0;
2604 }
Dees_Troy83bd4832013-05-04 12:39:56 +00002605 }
2606#endif
Dees_Troy43d8b002012-09-17 16:00:01 -04002607
Ethan Yonker472f5062016-02-25 13:47:30 -06002608 Backup_FileName = Backup_Name + "." + Current_File_System + ".win";
Ethan Yonkere080c1f2016-09-19 13:50:25 -05002609 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
Ethan Yonker01f4e032017-02-03 15:30:52 -06002610 if (Has_Data_Media)
2611 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 -05002612 tar.part_settings = part_settings;
Ethan Yonker3fdcda42016-11-30 12:29:37 -06002613 tar.backup_exclusions = &backup_exclusions;
Dees Troye0a433a2013-12-02 04:10:37 +00002614 tar.setdir(Backup_Path);
2615 tar.setfn(Full_FileName);
2616 tar.setsize(Backup_Size);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002617 tar.partition_name = Backup_Name;
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002618 tar.backup_folder = part_settings->Backup_Folder;
bigbiffce8f83c2015-12-12 18:30:21 -05002619 if (tar.createTarFork(tar_fork_pid) != 0)
Dees Troye0a433a2013-12-02 04:10:37 +00002620 return false;
Dees_Troy43d8b002012-09-17 16:00:01 -04002621 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002622}
2623
bigbiffce8f83c2015-12-12 18:30:21 -05002624bool TWPartition::Backup_Image(PartitionSettings *part_settings) {
2625 string Full_FileName, adb_file_name;
igoriok87e3d932013-01-31 21:03:53 +02002626
Ethan Yonker74db1572015-10-28 12:44:49 -05002627 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, gui_parse_text("{@backing}"));
2628 gui_msg(Msg("backing_up=Backing up {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002629
Ethan Yonker472f5062016-02-25 13:47:30 -06002630 Backup_FileName = Backup_Name + "." + Current_File_System + ".win";
Dees_Troy43d8b002012-09-17 16:00:01 -04002631
bigbiffce8f83c2015-12-12 18:30:21 -05002632 if (part_settings->adbbackup) {
2633 Full_FileName = TW_ADB_BACKUP;
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002634 adb_file_name = part_settings->Backup_Folder + "/" + Backup_FileName;
bigbiffce8f83c2015-12-12 18:30:21 -05002635 }
2636 else
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002637 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
bigbiffce8f83c2015-12-12 18:30:21 -05002638
2639 part_settings->total_restore_size = Backup_Size;
2640
2641 if (part_settings->adbbackup) {
2642 if (!twadbbu::Write_TWIMG(adb_file_name, Backup_Size))
2643 return false;
2644 }
2645
2646 if (!Raw_Read_Write(part_settings))
Ethan Yonker472f5062016-02-25 13:47:30 -06002647 return false;
2648
bigbiffce8f83c2015-12-12 18:30:21 -05002649 if (part_settings->adbbackup) {
2650 if (!twadbbu::Write_TWEOF())
2651 return false;
Dees_Troy7c2dec82012-09-26 09:49:14 -04002652 }
Dees_Troy43d8b002012-09-17 16:00:01 -04002653 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002654}
2655
bigbiffce8f83c2015-12-12 18:30:21 -05002656bool TWPartition::Raw_Read_Write(PartitionSettings *part_settings) {
2657 unsigned long long RW_Block_Size, Remain = Backup_Size;
2658 int src_fd = -1, dest_fd = -1;
2659 ssize_t bs;
Ethan Yonker472f5062016-02-25 13:47:30 -06002660 bool ret = false;
2661 void* buffer = NULL;
2662 unsigned long long backedup_size = 0;
bigbiffce8f83c2015-12-12 18:30:21 -05002663 string srcfn, destfn;
Ethan Yonker472f5062016-02-25 13:47:30 -06002664
bigbiffce8f83c2015-12-12 18:30:21 -05002665 if (part_settings->PM_Method == PM_BACKUP) {
2666 srcfn = Actual_Block_Device;
2667 if (part_settings->adbbackup)
2668 destfn = TW_ADB_BACKUP;
bigbiff bigbiff38b83c12017-12-28 19:58:52 -05002669 else {
Ethan Yonkere080c1f2016-09-19 13:50:25 -05002670 destfn = part_settings->Backup_Folder + "/" + Backup_FileName;
bigbiff bigbiff38b83c12017-12-28 19:58:52 -05002671 }
bigbiffce8f83c2015-12-12 18:30:21 -05002672 }
2673 else {
2674 destfn = Actual_Block_Device;
2675 if (part_settings->adbbackup) {
2676 srcfn = TW_ADB_RESTORE;
2677 } else {
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002678 srcfn = part_settings->Backup_Folder + "/" + Backup_FileName;
bigbiffce8f83c2015-12-12 18:30:21 -05002679 Remain = TWFunc::Get_File_Size(srcfn);
2680 }
2681 }
Ethan Yonker472f5062016-02-25 13:47:30 -06002682
bigbiffce8f83c2015-12-12 18:30:21 -05002683 src_fd = open(srcfn.c_str(), O_RDONLY | O_LARGEFILE);
Ethan Yonker472f5062016-02-25 13:47:30 -06002684 if (src_fd < 0) {
bigbiffce8f83c2015-12-12 18:30:21 -05002685 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(srcfn.c_str())(strerror(errno)));
Ethan Yonker472f5062016-02-25 13:47:30 -06002686 return false;
2687 }
bigbiffce8f83c2015-12-12 18:30:21 -05002688
2689 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 -06002690 if (dest_fd < 0) {
bigbiffce8f83c2015-12-12 18:30:21 -05002691 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(destfn.c_str())(strerror(errno)));
Ethan Yonker472f5062016-02-25 13:47:30 -06002692 goto exit;
2693 }
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -04002694
bigbiffce8f83c2015-12-12 18:30:21 -05002695 LOGINFO("Reading '%s', writing '%s'\n", srcfn.c_str(), destfn.c_str());
2696
2697 if (part_settings->adbbackup) {
2698 RW_Block_Size = MAX_ADB_READ;
2699 bs = MAX_ADB_READ;
2700 }
2701 else {
2702 RW_Block_Size = 1048576LLU; // 1MB
2703 bs = (ssize_t)(RW_Block_Size);
2704 }
2705
Ethan Yonker472f5062016-02-25 13:47:30 -06002706 buffer = malloc((size_t)bs);
2707 if (!buffer) {
2708 LOGINFO("Raw_Read_Write failed to malloc\n");
2709 goto exit;
2710 }
bigbiffce8f83c2015-12-12 18:30:21 -05002711
2712 if (part_settings->progress)
2713 part_settings->progress->SetPartitionSize(part_settings->total_restore_size);
2714
Ethan Yonker472f5062016-02-25 13:47:30 -06002715 while (Remain > 0) {
2716 if (Remain < RW_Block_Size)
bigbiffce8f83c2015-12-12 18:30:21 -05002717 bs = (ssize_t)(Remain);
Matt Mowera8a89d12016-12-30 18:10:37 -06002718 if (read(src_fd, buffer, bs) != bs) {
Ethan Yonker472f5062016-02-25 13:47:30 -06002719 LOGINFO("Error reading source fd (%s)\n", strerror(errno));
2720 goto exit;
2721 }
2722 if (write(dest_fd, buffer, bs) != bs) {
2723 LOGINFO("Error writing destination fd (%s)\n", strerror(errno));
2724 goto exit;
2725 }
2726 backedup_size += (unsigned long long)(bs);
Matt Mower029a82d2017-01-08 13:12:38 -06002727 Remain -= (unsigned long long)(bs);
bigbiffce8f83c2015-12-12 18:30:21 -05002728 if (part_settings->progress)
2729 part_settings->progress->UpdateSize(backedup_size);
Ethan Yonker472f5062016-02-25 13:47:30 -06002730 if (PartitionManager.Check_Backup_Cancel() != 0)
2731 goto exit;
2732 }
bigbiffce8f83c2015-12-12 18:30:21 -05002733 if (part_settings->progress)
2734 part_settings->progress->UpdateDisplayDetails(true);
Ethan Yonker472f5062016-02-25 13:47:30 -06002735 fsync(dest_fd);
nailyka083dc62016-11-11 21:41:28 +01002736
2737 if (!part_settings->adbbackup && part_settings->PM_Method == PM_BACKUP) {
2738 tw_set_default_metadata(destfn.c_str());
2739 LOGINFO("Restored default metadata for %s\n", destfn.c_str());
2740 }
2741
Ethan Yonker472f5062016-02-25 13:47:30 -06002742 ret = true;
2743exit:
2744 if (src_fd >= 0)
2745 close(src_fd);
2746 if (dest_fd >= 0)
2747 close(dest_fd);
2748 if (buffer)
2749 free(buffer);
2750 return ret;
2751}
2752
bigbiffce8f83c2015-12-12 18:30:21 -05002753bool TWPartition::Backup_Dump_Image(PartitionSettings *part_settings) {
Vojtech Bocek05534202013-09-11 08:11:56 +02002754 string Full_FileName, Command;
Dees_Troy43d8b002012-09-17 16:00:01 -04002755
Ethan Yonker74db1572015-10-28 12:44:49 -05002756 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, gui_parse_text("{@backing}"));
2757 gui_msg(Msg("backing_up=Backing up {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002758
bigbiffce8f83c2015-12-12 18:30:21 -05002759 if (part_settings->progress)
2760 part_settings->progress->SetPartitionSize(Backup_Size);
Dees_Troy43d8b002012-09-17 16:00:01 -04002761
Ethan Yonker472f5062016-02-25 13:47:30 -06002762 Backup_FileName = Backup_Name + "." + Current_File_System + ".win";
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002763 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04002764
2765 Command = "dump_image " + MTD_Name + " '" + Full_FileName + "'";
bigbiffce8f83c2015-12-12 18:30:21 -05002766
Dees_Troy2673cec2013-04-02 20:22:16 +00002767 LOGINFO("Backup command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02002768 TWFunc::Exec_Cmd(Command);
Ethan Yonker4b94cfd2014-12-11 10:00:45 -06002769 tw_set_default_metadata(Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04002770 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
2771 // 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 -06002772 gui_msg(Msg(msg::kError, "backup_size=Backup file size for '{1}' is 0 bytes.")(Full_FileName));
Dees_Troy7c2dec82012-09-26 09:49:14 -04002773 return false;
2774 }
bigbiffce8f83c2015-12-12 18:30:21 -05002775 if (part_settings->progress)
2776 part_settings->progress->UpdateSize(Backup_Size);
2777
Dees_Troy43d8b002012-09-17 16:00:01 -04002778 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002779}
2780
bigbiffce8f83c2015-12-12 18:30:21 -05002781unsigned long long TWPartition::Get_Restore_Size(PartitionSettings *part_settings) {
2782 if (!part_settings->adbbackup) {
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002783 InfoManager restore_info(part_settings->Backup_Folder + "/" + Backup_Name + ".info");
bigbiffce8f83c2015-12-12 18:30:21 -05002784 if (restore_info.LoadValues() == 0) {
2785 if (restore_info.GetValue("backup_size", Restore_Size) == 0) {
2786 LOGINFO("Read info file, restore size is %llu\n", Restore_Size);
2787 return Restore_Size;
2788 }
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002789 }
2790 }
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002791
Matt Mower029a82d2017-01-08 13:12:38 -06002792 string Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
2793 string Restore_File_System = Get_Restore_File_System(part_settings);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002794
2795 if (Is_Image(Restore_File_System)) {
2796 Restore_Size = TWFunc::Get_File_Size(Full_FileName);
2797 return Restore_Size;
2798 }
2799
2800 twrpTar tar;
2801 tar.setdir(Backup_Path);
2802 tar.setfn(Full_FileName);
bigbiffce8f83c2015-12-12 18:30:21 -05002803 tar.backup_name = Full_FileName;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002804#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
2805 string Password;
2806 DataManager::GetValue("tw_restore_password", Password);
2807 if (!Password.empty())
2808 tar.setpassword(Password);
2809#endif
2810 tar.partition_name = Backup_Name;
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002811 tar.backup_folder = part_settings->Backup_Folder;
bigbiffce8f83c2015-12-12 18:30:21 -05002812 tar.part_settings = part_settings;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002813 Restore_Size = tar.get_size();
2814 return Restore_Size;
2815}
2816
bigbiffce8f83c2015-12-12 18:30:21 -05002817bool TWPartition::Restore_Tar(PartitionSettings *part_settings) {
Ethan Yonker472f5062016-02-25 13:47:30 -06002818 string Full_FileName;
Dees Troy4159aed2014-02-28 17:24:43 +00002819 bool ret = false;
bigbiffce8f83c2015-12-12 18:30:21 -05002820 string Restore_File_System = Get_Restore_File_System(part_settings);
Dees_Troy43d8b002012-09-17 16:00:01 -04002821
Dees_Troye58d5262012-09-21 12:27:57 -04002822 if (Has_Android_Secure) {
Dees_Troye58d5262012-09-21 12:27:57 -04002823 if (!Wipe_AndSec())
2824 return false;
Gary Peck43acadf2012-11-21 21:19:01 -08002825 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002826 gui_msg(Msg("wiping=Wiping {1}")(Backup_Display_Name));
Ethan Yonker5eac2222014-06-11 12:22:55 -05002827 if (Has_Data_Media && Mount_Point == "/data" && Restore_File_System != Current_File_System) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002828 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 -05002829 if (!Wipe_Data_Without_Wiping_Media())
2830 return false;
2831 } else {
2832 if (!Wipe(Restore_File_System))
2833 return false;
2834 }
Dees_Troye58d5262012-09-21 12:27:57 -04002835 }
Matt Mower3c366972015-12-25 19:28:31 -06002836 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Backup_Display_Name, gui_parse_text("{@restoring_hdr}"));
Ethan Yonker74db1572015-10-28 12:44:49 -05002837 gui_msg(Msg("restoring=Restoring {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002838
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05002839 // Remount as read/write as needed so we can restore the backup
2840 if (!ReMount_RW(true))
Dees_Troy43d8b002012-09-17 16:00:01 -04002841 return false;
2842
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002843 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
Ethan Yonker87af5632014-02-10 11:56:35 -06002844 twrpTar tar;
bigbiffce8f83c2015-12-12 18:30:21 -05002845 tar.part_settings = part_settings;
Ethan Yonker87af5632014-02-10 11:56:35 -06002846 tar.setdir(Backup_Path);
2847 tar.setfn(Full_FileName);
2848 tar.backup_name = Backup_Name;
2849#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
2850 string Password;
2851 DataManager::GetValue("tw_restore_password", Password);
2852 if (!Password.empty())
2853 tar.setpassword(Password);
2854#endif
bigbiffce8f83c2015-12-12 18:30:21 -05002855 part_settings->progress->SetPartitionSize(Get_Restore_Size(part_settings));
2856 if (tar.extractTarFork() != 0)
Dees Troy4159aed2014-02-28 17:24:43 +00002857 ret = false;
2858 else
2859 ret = true;
2860#ifdef HAVE_CAPABILITIES
2861 // Restore capabilities to the run-as binary
Captain Throwback9d6feb52018-07-27 10:05:24 -04002862 if (Mount_Point == PartitionManager.Get_Android_Root_Path() && Mount(true) && TWFunc::Path_Exists("/system/bin/run-as")) {
Dees Troy4159aed2014-02-28 17:24:43 +00002863 struct vfs_cap_data cap_data;
2864 uint64_t capabilities = (1 << CAP_SETUID) | (1 << CAP_SETGID);
2865
2866 memset(&cap_data, 0, sizeof(cap_data));
2867 cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE;
2868 cap_data.data[0].permitted = (uint32_t) (capabilities & 0xffffffff);
2869 cap_data.data[0].inheritable = 0;
2870 cap_data.data[1].permitted = (uint32_t) (capabilities >> 32);
2871 cap_data.data[1].inheritable = 0;
2872 if (setxattr("/system/bin/run-as", XATTR_NAME_CAPS, &cap_data, sizeof(cap_data), 0) < 0) {
2873 LOGINFO("Failed to reset capabilities of /system/bin/run-as binary.\n");
2874 } else {
2875 LOGINFO("Reset capabilities of /system/bin/run-as binary successful.\n");
2876 }
2877 }
2878#endif
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05002879 if (Mount_Read_Only || Mount_Flags & MS_RDONLY)
2880 // Remount as read only when restoration is complete
2881 ReMount(true);
2882
Dees Troy4159aed2014-02-28 17:24:43 +00002883 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04002884}
2885
bigbiffce8f83c2015-12-12 18:30:21 -05002886bool TWPartition::Restore_Image(PartitionSettings *part_settings) {
Ethan Yonker96af84a2015-01-05 14:58:36 -06002887 string Full_FileName;
bigbiffce8f83c2015-12-12 18:30:21 -05002888 string Restore_File_System = Get_Restore_File_System(part_settings);
Dees_Troy43d8b002012-09-17 16:00:01 -04002889
Matt Mower3c366972015-12-25 19:28:31 -06002890 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Backup_Display_Name, gui_parse_text("{@restoring_hdr}"));
Ethan Yonker74db1572015-10-28 12:44:49 -05002891 gui_msg(Msg("restoring=Restoring {1}...")(Backup_Display_Name));
bigbiffce8f83c2015-12-12 18:30:21 -05002892
2893 if (part_settings->adbbackup)
2894 Full_FileName = TW_ADB_RESTORE;
2895 else
Ethan Yonkere080c1f2016-09-19 13:50:25 -05002896 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
Gary Peck15e623d2012-11-21 21:07:58 -08002897
Ethan Yonker96af84a2015-01-05 14:58:36 -06002898 if (Restore_File_System == "emmc") {
bigbiffce8f83c2015-12-12 18:30:21 -05002899 if (!part_settings->adbbackup)
2900 part_settings->total_restore_size = (uint64_t)(TWFunc::Get_File_Size(Full_FileName));
2901 if (!Raw_Read_Write(part_settings))
Ethan Yonker96af84a2015-01-05 14:58:36 -06002902 return false;
2903 } else if (Restore_File_System == "mtd" || Restore_File_System == "bml") {
bigbiffce8f83c2015-12-12 18:30:21 -05002904 if (!Flash_Image_FI(Full_FileName, part_settings->progress))
2905 return false;
2906 }
2907
2908 if (part_settings->adbbackup) {
2909 if (!twadbbu::Write_TWEOF())
Ethan Yonker96af84a2015-01-05 14:58:36 -06002910 return false;
Gary Peck15e623d2012-11-21 21:07:58 -08002911 }
Dees_Troy43d8b002012-09-17 16:00:01 -04002912 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002913}
Dees_Troy5bf43922012-09-07 16:07:55 -04002914
2915bool TWPartition::Update_Size(bool Display_Error) {
Dees_Troy0550cfb2012-10-13 11:56:13 -04002916 bool ret = false, Was_Already_Mounted = false;
Dees_Troy51127312012-09-08 13:08:49 -04002917
Ethan Yonker1b190162016-12-05 15:25:19 -06002918 Find_Actual_Block_Device();
2919
bigbiffee7b7ff2020-03-23 15:08:27 -04002920 if (Actual_Block_Device.empty())
2921 return false;
2922
Ethan Yonker1b190162016-12-05 15:25:19 -06002923 if (!Can_Be_Mounted && !Is_Encrypted) {
2924 if (TWFunc::Path_Exists(Actual_Block_Device) && Find_Partition_Size()) {
2925 Used = Size;
2926 Backup_Size = Size;
2927 return true;
2928 }
Dees_Troy5bf43922012-09-07 16:07:55 -04002929 return false;
Ethan Yonker1b190162016-12-05 15:25:19 -06002930 }
Dees_Troy5bf43922012-09-07 16:07:55 -04002931
Dees_Troy0550cfb2012-10-13 11:56:13 -04002932 Was_Already_Mounted = Is_Mounted();
bigbiffee7b7ff2020-03-23 15:08:27 -04002933
Dees_Troy38bd7602012-09-14 13:33:53 -04002934 if (Removable || Is_Encrypted) {
2935 if (!Mount(false))
2936 return true;
2937 } else if (!Mount(Display_Error))
Dees_Troy5bf43922012-09-07 16:07:55 -04002938 return false;
Dees_Troy51127312012-09-08 13:08:49 -04002939
2940 ret = Get_Size_Via_statfs(Display_Error);
Dees_Troy0550cfb2012-10-13 11:56:13 -04002941 if (!ret || Size == 0) {
2942 if (!Get_Size_Via_df(Display_Error)) {
2943 if (!Was_Already_Mounted)
2944 UnMount(false);
Dees_Troy51127312012-09-08 13:08:49 -04002945 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04002946 }
2947 }
Dees_Troy51127312012-09-08 13:08:49 -04002948
Dees_Troy5bf43922012-09-07 16:07:55 -04002949 if (Has_Data_Media) {
2950 if (Mount(Display_Error)) {
Ethan Yonker3fdcda42016-11-30 12:29:37 -06002951 Used = backup_exclusions.Get_Folder_Size(Mount_Point);
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002952 Backup_Size = Used;
2953 int bak = (int)(Used / 1048576LLU);
Dees_Troy51127312012-09-08 13:08:49 -04002954 int fre = (int)(Free / 1048576LLU);
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002955 LOGINFO("Data backup size is %iMB, free: %iMB.\n", bak, fre);
Dees_Troy0550cfb2012-10-13 11:56:13 -04002956 } else {
2957 if (!Was_Already_Mounted)
2958 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04002959 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04002960 }
Dees_Troye58d5262012-09-21 12:27:57 -04002961 } else if (Has_Android_Secure) {
2962 if (Mount(Display_Error))
Ethan Yonker3fdcda42016-11-30 12:29:37 -06002963 Backup_Size = backup_exclusions.Get_Folder_Size(Backup_Path);
Dees_Troy0550cfb2012-10-13 11:56:13 -04002964 else {
2965 if (!Was_Already_Mounted)
2966 UnMount(false);
Dees_Troye58d5262012-09-21 12:27:57 -04002967 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04002968 }
Dees_Troy5bf43922012-09-07 16:07:55 -04002969 }
Dees_Troy0550cfb2012-10-13 11:56:13 -04002970 if (!Was_Already_Mounted)
2971 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04002972 return true;
Dees_Troy51127312012-09-08 13:08:49 -04002973}
Dees_Troy38bd7602012-09-14 13:33:53 -04002974
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06002975bool TWPartition::Find_Wildcard_Block_Devices(const string& Device) {
2976 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
2977 string Path = TWFunc::Get_Path(Device);
2978 string Dev = TWFunc::Get_Filename(Device);
2979 size_t wildcard_index = Dev.find("*");
2980 if (wildcard_index != string::npos)
2981 Dev = Dev.substr(0, wildcard_index);
2982 wildcard_index = Dev.size();
2983 DIR* d = opendir(Path.c_str());
2984 if (d == NULL) {
2985 LOGINFO("Error opening '%s': %s\n", Path.c_str(), strerror(errno));
2986 return false;
2987 }
2988 struct dirent* de;
2989 while ((de = readdir(d)) != NULL) {
2990 if (de->d_type != DT_BLK || strlen(de->d_name) <= wildcard_index || strncmp(de->d_name, Dev.c_str(), wildcard_index) != 0)
2991 continue;
2992
2993 string item = Path + "/";
2994 item.append(de->d_name);
2995 if (PartitionManager.Find_Partition_By_Block_Device(item))
2996 continue;
2997 TWPartition *part = new TWPartition;
2998 char buffer[MAX_FSTAB_LINE_LENGTH];
2999 sprintf(buffer, "%s %s-%i auto defaults defaults", item.c_str(), Mount_Point.c_str(), ++mount_point_index);
Chaosmasterf6e42ce2020-01-27 00:17:04 +01003000 part->Process_Fstab_Line(buffer, false, NULL, false);
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06003001 char display[MAX_FSTAB_LINE_LENGTH];
3002 sprintf(display, "%s %i", Storage_Name.c_str(), mount_point_index);
3003 part->Storage_Name = display;
3004 part->Display_Name = display;
3005 part->Primary_Block_Device = item;
3006 part->Wildcard_Block_Device = false;
3007 part->Is_SubPartition = true;
3008 part->SubPartition_Of = Mount_Point;
3009 part->Is_Storage = Is_Storage;
3010 part->Can_Be_Mounted = true;
3011 part->Removable = true;
3012 part->Can_Be_Wiped = Can_Be_Wiped;
3013 part->Wipe_Available_in_GUI = Wipe_Available_in_GUI;
3014 part->Find_Actual_Block_Device();
3015 part->Update_Size(false);
3016 Has_SubPartition = true;
3017 PartitionManager.Output_Partition(part);
3018 PartitionManager.Add_Partition(part);
3019 }
3020 closedir(d);
3021 return (mount_point_index > 0);
3022}
3023
Dees_Troy38bd7602012-09-14 13:33:53 -04003024void TWPartition::Find_Actual_Block_Device(void) {
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06003025 if (!Sysfs_Entry.empty() && Primary_Block_Device.empty() && Decrypted_Block_Device.empty()) {
3026 /* Sysfs_Entry.empty() indicates if this is a sysfs entry that begins with /device/
3027 * If we have a syfs entry then we are looking for this device from a uevent add.
3028 * The uevent add will set the primary block device based on the data we receive from
3029 * after checking for adopted storage. If the device ends up being adopted, then the
3030 * decrypted block device will be set instead of the primary block device. */
3031 Is_Present = false;
3032 return;
3033 }
3034 if (Wildcard_Block_Device && !Is_Adopted_Storage) {
3035 Is_Present = false;
3036 Actual_Block_Device = "";
3037 Can_Be_Mounted = false;
3038 if (!Find_Wildcard_Block_Devices(Primary_Block_Device)) {
3039 string Dev = Primary_Block_Device.substr(0, Primary_Block_Device.find("*"));
3040 if (TWFunc::Path_Exists(Dev)) {
3041 Is_Present = true;
3042 Can_Be_Mounted = true;
3043 Actual_Block_Device = Dev;
3044 }
3045 }
3046 return;
3047 } else if (Is_Decrypted && !Decrypted_Block_Device.empty()) {
Dees_Troy38bd7602012-09-14 13:33:53 -04003048 Actual_Block_Device = Decrypted_Block_Device;
Ethan Yonker1b190162016-12-05 15:25:19 -06003049 if (TWFunc::Path_Exists(Decrypted_Block_Device)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04003050 Is_Present = true;
Ethan Yonker1b190162016-12-05 15:25:19 -06003051 return;
3052 }
3053 } else if (SlotSelect && TWFunc::Path_Exists(Primary_Block_Device + PartitionManager.Get_Active_Slot_Suffix())) {
3054 Actual_Block_Device = Primary_Block_Device + PartitionManager.Get_Active_Slot_Suffix();
3055 unlink(Primary_Block_Device.c_str());
3056 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
3057 Is_Present = true;
3058 return;
Dees_Troy43d8b002012-09-17 16:00:01 -04003059 } else if (TWFunc::Path_Exists(Primary_Block_Device)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04003060 Is_Present = true;
3061 Actual_Block_Device = Primary_Block_Device;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05003062 return;
3063 }
Ethan Yonker1b190162016-12-05 15:25:19 -06003064 if (!Alternate_Block_Device.empty() && TWFunc::Path_Exists(Alternate_Block_Device)) {
Dees_Troy3f04d032012-10-07 18:20:09 -04003065 Actual_Block_Device = Alternate_Block_Device;
Dees_Troy38bd7602012-09-14 13:33:53 -04003066 Is_Present = true;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05003067 } else {
Dees_Troy38bd7602012-09-14 13:33:53 -04003068 Is_Present = false;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05003069 }
Dees_Troy38bd7602012-09-14 13:33:53 -04003070}
3071
3072void TWPartition::Recreate_Media_Folder(void) {
3073 string Command;
Ethan Yonker66a19492015-12-10 10:19:45 -06003074 string Media_Path = Mount_Point + "/media";
Dees_Troy38bd7602012-09-14 13:33:53 -04003075
Ethan Yonkerbd7492d2016-12-07 13:55:01 -06003076 if (Is_FBE) {
3077 LOGINFO("Not recreating media folder on FBE\n");
3078 return;
3079 }
Dees_Troy38bd7602012-09-14 13:33:53 -04003080 if (!Mount(true)) {
Ethan Yonker66a19492015-12-10 10:19:45 -06003081 gui_msg(Msg(msg::kError, "recreate_folder_err=Unable to recreate {1} folder.")(Media_Path));
3082 } else if (!TWFunc::Path_Exists(Media_Path)) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05003083 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Ethan Yonker66a19492015-12-10 10:19:45 -06003084 LOGINFO("Recreating %s folder.\n", Media_Path.c_str());
3085 mkdir(Media_Path.c_str(), 0770);
xiaolu9416f4f2015-06-04 08:22:23 +08003086 string Internal_path = DataManager::GetStrValue("tw_internal_path");
3087 if (!Internal_path.empty()) {
3088 LOGINFO("Recreating %s folder.\n", Internal_path.c_str());
3089 mkdir(Internal_path.c_str(), 0770);
3090 }
3091#ifdef TW_INTERNAL_STORAGE_PATH
3092 mkdir(EXPAND(TW_INTERNAL_STORAGE_PATH), 0770);
3093#endif
Matt Mower87413642017-01-17 21:14:46 -06003094
thata3d31fb2014-12-21 22:27:40 +01003095 // Afterwards, we will try to set the
3096 // default metadata that we were hopefully able to get during
3097 // early boot.
Ethan Yonker66a19492015-12-10 10:19:45 -06003098 tw_set_default_metadata(Media_Path.c_str());
xiaolu9416f4f2015-06-04 08:22:23 +08003099 if (!Internal_path.empty())
3100 tw_set_default_metadata(Internal_path.c_str());
Matt Mower87413642017-01-17 21:14:46 -06003101
Ethan Yonker5eac2222014-06-11 12:22:55 -05003102 // Toggle mount to ensure that "internal sdcard" gets mounted
bigbiff bigbiff9c754052013-01-09 09:09:08 -05003103 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Ethan Yonker5eac2222014-06-11 12:22:55 -05003104 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Dees_Troy38bd7602012-09-14 13:33:53 -04003105 }
Dees_Troy43d8b002012-09-17 16:00:01 -04003106}
Dees_Troye58d5262012-09-21 12:27:57 -04003107
3108void TWPartition::Recreate_AndSec_Folder(void) {
Dees_Troye58d5262012-09-21 12:27:57 -04003109 if (!Has_Android_Secure)
3110 return;
Dees_Troy2673cec2013-04-02 20:22:16 +00003111 LOGINFO("Creating %s: %s\n", Backup_Display_Name.c_str(), Symlink_Path.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -04003112 if (!Mount(true)) {
Ethan Yonker74db1572015-10-28 12:44:49 -05003113 gui_msg(Msg(msg::kError, "recreate_folder_err=Unable to recreate {1} folder.")(Backup_Name));
Dees_Troye58d5262012-09-21 12:27:57 -04003114 } else if (!TWFunc::Path_Exists(Symlink_Path)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00003115 LOGINFO("Recreating %s folder.\n", Backup_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05003116 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05003117 mkdir(Symlink_Path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
bigbiff bigbiff9c754052013-01-09 09:09:08 -05003118 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04003119 }
3120}
bigbiff7cb4c332014-11-26 20:36:07 -05003121
3122uint64_t TWPartition::Get_Max_FileSize() {
3123 uint64_t maxFileSize = 0;
3124 const uint64_t constGB = (uint64_t) 1024 * 1024 * 1024;
3125 const uint64_t constTB = (uint64_t) constGB * 1024;
3126 const uint64_t constPB = (uint64_t) constTB * 1024;
bigbiff0c532032014-12-21 13:41:26 -05003127 if (Current_File_System == "ext4")
3128 maxFileSize = 16 * constTB; //16 TB
3129 else if (Current_File_System == "vfat")
3130 maxFileSize = 4 * constGB; //4 GB
3131 else if (Current_File_System == "ntfs")
3132 maxFileSize = 256 * constTB; //256 TB
3133 else if (Current_File_System == "exfat")
3134 maxFileSize = 16 * constPB; //16 PB
3135 else if (Current_File_System == "ext3")
3136 maxFileSize = 2 * constTB; //2 TB
3137 else if (Current_File_System == "f2fs")
3138 maxFileSize = 3.94 * constTB; //3.94 TB
bigbiff7cb4c332014-11-26 20:36:07 -05003139 else
3140 maxFileSize = 100000000L;
3141 return maxFileSize - 1;
3142}
3143
bigbiffce8f83c2015-12-12 18:30:21 -05003144bool TWPartition::Flash_Image(PartitionSettings *part_settings) {
3145 string Restore_File_System, full_filename;
Ethan Yonker96af84a2015-01-05 14:58:36 -06003146
Ethan Yonkere080c1f2016-09-19 13:50:25 -05003147 full_filename = part_settings->Backup_Folder + "/" + Backup_FileName;
Ethan Yonker96af84a2015-01-05 14:58:36 -06003148
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05003149 LOGINFO("Image filename is: %s\n", Backup_FileName.c_str());
bigbiffce8f83c2015-12-12 18:30:21 -05003150
3151 if (Backup_Method == BM_FILES) {
Ethan Yonker96af84a2015-01-05 14:58:36 -06003152 LOGERR("Cannot flash images to file systems\n");
3153 return false;
3154 } else if (!Can_Flash_Img) {
3155 LOGERR("Cannot flash images to partitions %s\n", Display_Name.c_str());
3156 return false;
3157 } else {
3158 if (!Find_Partition_Size()) {
3159 LOGERR("Unable to find partition size for '%s'\n", Mount_Point.c_str());
3160 return false;
3161 }
bigbiffce8f83c2015-12-12 18:30:21 -05003162 unsigned long long image_size = TWFunc::Get_File_Size(full_filename);
Ethan Yonker96af84a2015-01-05 14:58:36 -06003163 if (image_size > Size) {
Ethan Yonker74db1572015-10-28 12:44:49 -05003164 LOGINFO("Size (%llu bytes) of image '%s' is larger than target device '%s' (%llu bytes)\n",
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05003165 image_size, Backup_FileName.c_str(), Actual_Block_Device.c_str(), Size);
Ethan Yonker74db1572015-10-28 12:44:49 -05003166 gui_err("img_size_err=Size of image is larger than target device");
Ethan Yonker96af84a2015-01-05 14:58:36 -06003167 return false;
3168 }
bigbiffce8f83c2015-12-12 18:30:21 -05003169 if (Backup_Method == BM_DD) {
3170 if (!part_settings->adbbackup) {
3171 if (Is_Sparse_Image(full_filename)) {
3172 return Flash_Sparse_Image(full_filename);
3173 }
Ethan Yonker472f5062016-02-25 13:47:30 -06003174 }
bigbiffce8f83c2015-12-12 18:30:21 -05003175 return Raw_Read_Write(part_settings);
3176 } else if (Backup_Method == BM_FLASH_UTILS) {
3177 return Flash_Image_FI(full_filename, NULL);
Ethan Yonker472f5062016-02-25 13:47:30 -06003178 }
Ethan Yonker96af84a2015-01-05 14:58:36 -06003179 }
3180
3181 LOGERR("Unknown flash method for '%s'\n", Mount_Point.c_str());
3182 return false;
3183}
3184
Ethan Yonker472f5062016-02-25 13:47:30 -06003185bool TWPartition::Is_Sparse_Image(const string& Filename) {
HashBanged974bb2016-01-30 14:20:09 -05003186 uint32_t magic = 0;
3187 int fd = open(Filename.c_str(), O_RDONLY);
3188 if (fd < 0) {
3189 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Filename)(strerror(errno)));
3190 return false;
3191 }
bigbiffce8f83c2015-12-12 18:30:21 -05003192
HashBanged974bb2016-01-30 14:20:09 -05003193 if (read(fd, &magic, sizeof(magic)) != sizeof(magic)) {
3194 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Filename)(strerror(errno)));
3195 close(fd);
3196 return false;
3197 }
3198 close(fd);
Ethan Yonker472f5062016-02-25 13:47:30 -06003199 if (magic == SPARSE_HEADER_MAGIC)
3200 return true;
3201 return false;
3202}
3203
3204bool TWPartition::Flash_Sparse_Image(const string& Filename) {
3205 string Command;
3206
3207 gui_msg(Msg("flashing=Flashing {1}...")(Display_Name));
3208
3209 Command = "simg2img '" + Filename + "' '" + Actual_Block_Device + "'";
Ethan Yonker96af84a2015-01-05 14:58:36 -06003210 LOGINFO("Flash command: '%s'\n", Command.c_str());
3211 TWFunc::Exec_Cmd(Command);
3212 return true;
3213}
3214
Ethan Yonker472f5062016-02-25 13:47:30 -06003215bool TWPartition::Flash_Image_FI(const string& Filename, ProgressTracking *progress) {
Ethan Yonker96af84a2015-01-05 14:58:36 -06003216 string Command;
Ethan Yonker472f5062016-02-25 13:47:30 -06003217 unsigned long long file_size;
Ethan Yonker96af84a2015-01-05 14:58:36 -06003218
Ethan Yonker74db1572015-10-28 12:44:49 -05003219 gui_msg(Msg("flashing=Flashing {1}...")(Display_Name));
Ethan Yonker472f5062016-02-25 13:47:30 -06003220 if (progress) {
3221 file_size = (unsigned long long)(TWFunc::Get_File_Size(Filename));
3222 progress->SetPartitionSize(file_size);
3223 }
Ethan Yonker96af84a2015-01-05 14:58:36 -06003224 // Sometimes flash image doesn't like to flash due to the first 2KB matching, so we erase first to ensure that it flashes
3225 Command = "erase_image " + MTD_Name;
3226 LOGINFO("Erase command: '%s'\n", Command.c_str());
3227 TWFunc::Exec_Cmd(Command);
3228 Command = "flash_image " + MTD_Name + " '" + Filename + "'";
3229 LOGINFO("Flash command: '%s'\n", Command.c_str());
3230 TWFunc::Exec_Cmd(Command);
Ethan Yonker472f5062016-02-25 13:47:30 -06003231 if (progress)
3232 progress->UpdateSize(file_size);
Ethan Yonker96af84a2015-01-05 14:58:36 -06003233 return true;
3234}
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05003235
3236void TWPartition::Change_Mount_Read_Only(bool new_value) {
3237 Mount_Read_Only = new_value;
3238}
3239
bigbiffce8f83c2015-12-12 18:30:21 -05003240bool TWPartition::Is_Read_Only() {
3241 return Mount_Read_Only;
3242}
3243
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05003244int TWPartition::Check_Lifetime_Writes() {
3245 bool original_read_only = Mount_Read_Only;
3246 int ret = 1;
3247
3248 Mount_Read_Only = true;
3249 if (Mount(false)) {
3250 Find_Actual_Block_Device();
Ethan Yonker54970bb2019-01-17 16:19:24 -06003251 string temp = Actual_Block_Device;
3252 Find_Real_Block_Device(temp, false);
3253 string block = basename(temp.c_str());
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05003254 string file = "/sys/fs/" + Current_File_System + "/" + block + "/lifetime_write_kbytes";
3255 string result;
3256 if (TWFunc::Path_Exists(file)) {
3257 if (TWFunc::read_file(file, result) != 0) {
3258 LOGINFO("Check_Lifetime_Writes of '%s' failed to read_file\n", file.c_str());
3259 } else {
3260 LOGINFO("Check_Lifetime_Writes result: '%s'\n", result.c_str());
3261 if (result == "0") {
3262 ret = 0;
3263 }
3264 }
3265 } else {
3266 LOGINFO("Check_Lifetime_Writes file does not exist '%s'\n", file.c_str());
3267 }
3268 UnMount(true);
3269 } else {
3270 LOGINFO("Check_Lifetime_Writes failed to mount '%s'\n", Mount_Point.c_str());
3271 }
3272 Mount_Read_Only = original_read_only;
3273 return ret;
3274}
Ethan Yonker66a19492015-12-10 10:19:45 -06003275
3276int TWPartition::Decrypt_Adopted() {
3277#ifdef TW_INCLUDE_CRYPTO
3278 int ret = 1;
3279 Is_Adopted_Storage = false;
3280 string Adopted_Key_File = "";
3281
3282 if (!Removable)
3283 return ret;
3284
3285 int fd = open(Alternate_Block_Device.c_str(), O_RDONLY);
3286 if (fd < 0) {
3287 LOGINFO("failed to open '%s'\n", Alternate_Block_Device.c_str());
3288 return ret;
3289 }
3290 char type_guid[80];
3291 char part_guid[80];
3292
3293 if (gpt_disk_get_partition_info(fd, 2, type_guid, part_guid) == 0) {
3294 LOGINFO("type: '%s'\n", type_guid);
3295 LOGINFO("part: '%s'\n", part_guid);
3296 Adopted_GUID = part_guid;
3297 LOGINFO("Adopted_GUID '%s'\n", Adopted_GUID.c_str());
3298 if (strcmp(type_guid, TWGptAndroidExpand) == 0) {
3299 LOGINFO("android_expand found\n");
3300 Adopted_Key_File = "/data/misc/vold/expand_";
3301 Adopted_Key_File += part_guid;
3302 Adopted_Key_File += ".key";
3303 if (TWFunc::Path_Exists(Adopted_Key_File)) {
3304 Is_Adopted_Storage = true;
3305 /* Until we find a use case for this, I think it is safe
3306 * to disable USB Mass Storage whenever adopted storage
3307 * is present.
3308 */
3309 LOGINFO("Detected adopted storage, disabling USB mass storage mode\n");
3310 DataManager::SetValue("tw_has_usb_storage", 0);
3311 }
3312 }
3313 }
3314
3315 if (Is_Adopted_Storage) {
3316 string Adopted_Block_Device = Alternate_Block_Device + "p2";
3317 if (!TWFunc::Path_Exists(Adopted_Block_Device)) {
3318 Adopted_Block_Device = Alternate_Block_Device + "2";
3319 if (!TWFunc::Path_Exists(Adopted_Block_Device)) {
3320 LOGINFO("Adopted block device does not exist\n");
3321 goto exit;
3322 }
3323 }
3324 LOGINFO("key file is '%s', block device '%s'\n", Adopted_Key_File.c_str(), Adopted_Block_Device.c_str());
3325 char crypto_blkdev[MAXPATHLEN];
3326 std::string thekey;
3327 int fdkey = open(Adopted_Key_File.c_str(), O_RDONLY);
3328 if (fdkey < 0) {
3329 LOGINFO("failed to open key file\n");
3330 goto exit;
3331 }
3332 char buf[512];
3333 ssize_t n;
3334 while ((n = read(fdkey, &buf[0], sizeof(buf))) > 0) {
3335 thekey.append(buf, n);
3336 }
3337 close(fdkey);
3338 unsigned char* key = (unsigned char*) thekey.data();
3339 cryptfs_revert_ext_volume(part_guid);
3340
3341 ret = cryptfs_setup_ext_volume(part_guid, Adopted_Block_Device.c_str(), key, thekey.size(), crypto_blkdev);
3342 if (ret == 0) {
3343 LOGINFO("adopted storage new block device: '%s'\n", crypto_blkdev);
3344 Decrypted_Block_Device = crypto_blkdev;
3345 Is_Decrypted = true;
3346 Is_Encrypted = true;
3347 Find_Actual_Block_Device();
Kjell Braden3126a112016-06-19 16:58:15 +00003348 if (!Mount_Storage_Retry(false)) {
Ethan Yonker66a19492015-12-10 10:19:45 -06003349 LOGERR("Failed to mount decrypted adopted storage device\n");
3350 Is_Decrypted = false;
3351 Is_Encrypted = false;
3352 cryptfs_revert_ext_volume(part_guid);
3353 ret = 1;
3354 } else {
Ethan Yonkerfcf3f242016-02-16 12:30:26 -06003355 UnMount(false);
3356 Has_Android_Secure = false;
3357 Symlink_Path = "";
3358 Symlink_Mount_Point = "";
3359 Backup_Name = Mount_Point.substr(1);
3360 Backup_Path = Mount_Point;
3361 TWPartition* sdext = PartitionManager.Find_Partition_By_Path("/sd-ext");
3362 if (sdext && sdext->Actual_Block_Device == Adopted_Block_Device) {
3363 LOGINFO("Removing /sd-ext from partition list due to adopted storage\n");
3364 PartitionManager.Remove_Partition_By_Path("/sd-ext");
3365 }
Ethan Yonker66a19492015-12-10 10:19:45 -06003366 Setup_Data_Media();
Ethan Yonker66a19492015-12-10 10:19:45 -06003367 Wipe_Available_in_GUI = true;
3368 Wipe_During_Factory_Reset = true;
3369 Can_Be_Backed_Up = true;
3370 Can_Encrypt_Backup = true;
3371 Use_Userdata_Encryption = true;
3372 Is_Storage = true;
3373 Storage_Name = "Adopted Storage";
3374 Is_SubPartition = true;
3375 SubPartition_Of = "/data";
3376 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
3377 DataManager::SetValue("tw_has_adopted_storage", 1);
3378 }
3379 } else {
3380 LOGERR("Failed to setup adopted storage decryption\n");
3381 }
3382 }
3383exit:
Matt Mower06543e32017-01-06 15:25:26 -06003384 close(fd);
Ethan Yonker66a19492015-12-10 10:19:45 -06003385 return ret;
3386#else
3387 LOGINFO("Decrypt_Adopted: no crypto support\n");
3388 return 1;
3389#endif
3390}
3391
3392void TWPartition::Revert_Adopted() {
3393#ifdef TW_INCLUDE_CRYPTO
3394 if (!Adopted_GUID.empty()) {
3395 PartitionManager.Remove_MTP_Storage(Mount_Point);
3396 UnMount(false);
3397 cryptfs_revert_ext_volume(Adopted_GUID.c_str());
3398 Is_Adopted_Storage = false;
3399 Is_Encrypted = false;
3400 Is_Decrypted = false;
3401 Decrypted_Block_Device = "";
3402 Find_Actual_Block_Device();
3403 Wipe_During_Factory_Reset = false;
3404 Can_Be_Backed_Up = false;
3405 Can_Encrypt_Backup = false;
3406 Use_Userdata_Encryption = false;
3407 Is_SubPartition = false;
3408 SubPartition_Of = "";
3409 Has_Data_Media = false;
3410 Storage_Path = Mount_Point;
3411 if (!Symlink_Mount_Point.empty()) {
3412 TWPartition* Dat = PartitionManager.Find_Partition_By_Path("/data");
3413 if (Dat) {
3414 Dat->UnMount(false);
3415 Dat->Symlink_Mount_Point = Symlink_Mount_Point;
3416 }
3417 Symlink_Mount_Point = "";
3418 }
3419 }
3420#else
3421 LOGINFO("Revert_Adopted: no crypto support\n");
3422#endif
3423}
bigbiff bigbiffb5ecaad2017-03-20 18:53:53 -04003424
3425void TWPartition::Set_Backup_FileName(string fname) {
3426 Backup_FileName = fname;
3427}
3428
3429string TWPartition::Get_Backup_Name() {
3430 return Backup_Name;
3431}
bigbiffee7b7ff2020-03-23 15:08:27 -04003432
3433string TWPartition::Get_Mount_Point() {
3434 return Mount_Point;
3435}
3436
3437void TWPartition::Set_Block_Device(std::string block_device) {
3438 Primary_Block_Device = Actual_Block_Device = block_device;
3439}
3440
3441bool TWPartition::Get_Super_Status() {
3442 return Is_Super;
3443}
3444
3445void TWPartition::Set_Can_Be_Backed_Up(bool val) {
3446 Can_Be_Backed_Up = val;
3447}
3448
3449void TWPartition::Set_Can_Be_Wiped(bool val) {
3450 Can_Be_Wiped = val;
3451 Wipe_Available_in_GUI = val;
3452}