blob: 99f4949ecc59aac96361488be0a37c4f58b2f29c [file] [log] [blame]
Dees Troy3be70a82013-10-22 14:25:12 +00001/*
bigbiffaed1bdf2021-08-03 18:21:39 -04002 Copyright 2013 to 2021 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 <iostream>
32#include <libgen.h>
bigbiff7ba75002020-04-11 20:47:09 -040033#include <zlib.h>
34#include <sstream>
bigbiffcfa875c2021-06-20 16:20:27 -040035#include <android-base/properties.h>
Darth98c5088d2022-08-09 12:54:40 +010036#include <android-base/strings.h>
bigbiffcfa875c2021-06-20 16:20:27 -040037#include <libsnapshot/snapshot.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040038
Ethan Yonker007de982018-08-31 14:23:16 -050039#include "cutils/properties.h"
bigbiff7b4c7a62015-01-01 19:44:14 -050040#include "libblkid/include/blkid.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040041#include "variables.h"
Dees_Troy2673cec2013-04-02 20:22:16 +000042#include "twcommon.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040043#include "partitions.hpp"
Dees_Troy5bf43922012-09-07 16:07:55 -040044#include "data.hpp"
Dees_Troy43d8b002012-09-17 16:00:01 -040045#include "twrp-functions.hpp"
bigbiff bigbiff9c754052013-01-09 09:09:08 -050046#include "twrpTar.hpp"
Ethan Yonker3fdcda42016-11-30 12:29:37 -060047#include "exclude.hpp"
Ethan Yonker1b7a31b2014-07-03 15:09:22 -050048#include "infomanager.hpp"
Ethan Yonker4b94cfd2014-12-11 10:00:45 -060049#include "set_metadata.h"
Ethan Yonker74db1572015-10-28 12:44:49 -050050#include "gui/gui.hpp"
bigbiffce8f83c2015-12-12 18:30:21 -050051#include "adbbu/libtwadbbu.hpp"
Ethan Yonker98661c12018-10-17 08:39:28 -050052#ifdef TW_INCLUDE_CRYPTO
53 #include "crypto/fde/cryptfs.h"
bigbiffa957f072021-03-07 18:20:29 -050054 #include "Decrypt.h"
Ethan Yonker98661c12018-10-17 08:39:28 -050055#else
56 #define CRYPT_FOOTER_OFFSET 0x4000
57#endif
Dees_Troy5bf43922012-09-07 16:07:55 -040058extern "C" {
Dees_Troy38bd7602012-09-14 13:33:53 -040059 #include "mtdutils/mtdutils.h"
60 #include "mtdutils/mounts.h"
Dees_Troya95f55c2013-08-17 13:14:43 +000061#ifdef USE_EXT4
Ethan Yonker8373cfe2017-09-08 06:50:54 -050062 // #include "make_ext4fs.h" TODO need ifdef for android8
63 #include <ext4_utils/make_ext4fs.h>
Dees_Troya95f55c2013-08-17 13:14:43 +000064#endif
Ethan Yonker71413f42014-02-26 13:36:08 -060065#ifdef TW_INCLUDE_CRYPTO
Ethan Yonker66a19492015-12-10 10:19:45 -060066 #include "gpt/gpt.h"
Ethan Yonker71413f42014-02-26 13:36:08 -060067#endif
Dees_Troy5bf43922012-09-07 16:07:55 -040068}
Matt Mower87413642017-01-17 21:14:46 -060069#include <selinux/selinux.h>
Ethan Yonkerf27497f2014-02-09 11:48:33 -060070#include <selinux/label.h>
Dees Troy4159aed2014-02-28 17:24:43 +000071#ifdef HAVE_CAPABILITIES
72#include <sys/capability.h>
73#include <sys/xattr.h>
74#include <linux/xattr.h>
75#endif
HashBanged974bb2016-01-30 14:20:09 -050076#include <sparse_format.h>
Ethan Yonker472f5062016-02-25 13:47:30 -060077#include "progresstracking.hpp"
Dees_Troy51a0e822012-09-05 15:24:24 -040078
bigbiff bigbiff9c754052013-01-09 09:09:08 -050079using namespace std;
80
Ethan Yonker6e8c27a2016-12-22 17:55:57 -060081static 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
82
Dees_Troya95f55c2013-08-17 13:14:43 +000083extern struct selabel_handle *selinux_handle;
Ethan Yonker6277c792014-09-15 14:54:30 -050084extern bool datamedia;
Dees_Troya95f55c2013-08-17 13:14:43 +000085
Hashcode62bd9e02013-11-19 21:59:42 -080086struct flag_list {
87 const char *name;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -060088 unsigned long flag;
Hashcode62bd9e02013-11-19 21:59:42 -080089};
90
Matt Mower4ab42b12016-04-21 13:52:18 -050091const struct flag_list mount_flags[] = {
Ethan Yonker6e8c27a2016-12-22 17:55:57 -060092 { "noatime", MS_NOATIME },
93 { "noexec", MS_NOEXEC },
94 { "nosuid", MS_NOSUID },
95 { "nodev", MS_NODEV },
96 { "nodiratime", MS_NODIRATIME },
97 { "ro", MS_RDONLY },
98 { "rw", 0 },
99 { "remount", MS_REMOUNT },
100 { "bind", MS_BIND },
101 { "rec", MS_REC },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000102#ifdef MS_UNBINDABLE
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600103 { "unbindable", MS_UNBINDABLE },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000104#endif
105#ifdef MS_PRIVATE
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600106 { "private", MS_PRIVATE },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000107#endif
108#ifdef MS_SLAVE
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600109 { "slave", MS_SLAVE },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000110#endif
111#ifdef MS_SHARED
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600112 { "shared", MS_SHARED },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000113#endif
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600114 { "sync", MS_SYNCHRONOUS },
115 { 0, 0 },
116};
117
118const char *ignored_mount_items[] = {
119 "defaults=",
120 "errors=",
Ethan Yonker93382822018-11-01 15:25:31 -0500121 "latemount",
122 "sysfs_path=",
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600123 NULL
Hashcode62bd9e02013-11-19 21:59:42 -0800124};
125
Matt Mower2416a502016-04-12 19:54:46 -0500126enum TW_FSTAB_FLAGS {
127 TWFLAG_DEFAULTS, // Retain position
128 TWFLAG_ANDSEC,
129 TWFLAG_BACKUP,
130 TWFLAG_BACKUPNAME,
131 TWFLAG_BLOCKSIZE,
132 TWFLAG_CANBEWIPED,
133 TWFLAG_CANENCRYPTBACKUP,
134 TWFLAG_DISPLAY,
135 TWFLAG_ENCRYPTABLE,
Ethan Yonkere9afc3d2018-08-30 15:16:27 -0500136 TWFLAG_FILEENCRYPTION,
Captain Throwback32bcdcd2022-02-08 12:45:01 -0500137 TWFLAG_METADATA_ENCRYPTION,
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,
Captain Throwbacke848afc2022-02-08 15:31:44 -0500144 TWFLAG_QUOTA,
Matt Mower2416a502016-04-12 19:54:46 -0500145 TWFLAG_REMOVABLE,
Matt Mower2416a502016-04-12 19:54:46 -0500146 TWFLAG_SETTINGSSTORAGE,
147 TWFLAG_STORAGE,
148 TWFLAG_STORAGENAME,
149 TWFLAG_SUBPARTITIONOF,
150 TWFLAG_SYMLINK,
151 TWFLAG_USERDATAENCRYPTBACKUP,
152 TWFLAG_USERMRF,
153 TWFLAG_WIPEDURINGFACTORYRESET,
154 TWFLAG_WIPEINGUI,
Ethan Yonker1b190162016-12-05 15:25:19 -0600155 TWFLAG_SLOTSELECT,
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600156 TWFLAG_WAIT,
157 TWFLAG_VERIFY,
158 TWFLAG_CHECK,
159 TWFLAG_ALTDEVICE,
160 TWFLAG_NOTRIM,
161 TWFLAG_VOLDMANAGED,
162 TWFLAG_FORMATTABLE,
163 TWFLAG_RESIZE,
Ethan Yonker93382822018-11-01 15:25:31 -0500164 TWFLAG_KEYDIRECTORY,
Peter Cai28ea7b02019-05-24 11:38:54 +0800165 TWFLAG_WRAPPEDKEY,
Peter Cai439d60c2019-10-17 08:49:10 +0800166 TWFLAG_ADOPTED_MOUNT_DELAY,
Peter Caid68e6412019-11-02 19:22:38 +0800167 TWFLAG_DM_USE_ORIGINAL_PATH,
bigbiff998f8392021-08-06 19:19:33 -0400168 TWFLAG_LOGICAL,
Matt Mower2416a502016-04-12 19:54:46 -0500169};
170
171/* Flags without a trailing '=' are considered dual format flags and can be
172 * written as either 'flagname' or 'flagname=', where the character following
173 * the '=' is Y,y,1 for true and false otherwise.
174 */
175const struct flag_list tw_flags[] = {
176 { "andsec", TWFLAG_ANDSEC },
177 { "backup", TWFLAG_BACKUP },
178 { "backupname=", TWFLAG_BACKUPNAME },
179 { "blocksize=", TWFLAG_BLOCKSIZE },
180 { "canbewiped", TWFLAG_CANBEWIPED },
181 { "canencryptbackup", TWFLAG_CANENCRYPTBACKUP },
182 { "defaults", TWFLAG_DEFAULTS },
183 { "display=", TWFLAG_DISPLAY },
184 { "encryptable=", TWFLAG_ENCRYPTABLE },
Ethan Yonkere9afc3d2018-08-30 15:16:27 -0500185 { "fileencryption=", TWFLAG_FILEENCRYPTION },
Captain Throwback32bcdcd2022-02-08 12:45:01 -0500186 { "metadata_encryption=", TWFLAG_METADATA_ENCRYPTION },
Matt Mower2416a502016-04-12 19:54:46 -0500187 { "flashimg", TWFLAG_FLASHIMG },
188 { "forceencrypt=", TWFLAG_FORCEENCRYPT },
189 { "fsflags=", TWFLAG_FSFLAGS },
190 { "ignoreblkid", TWFLAG_IGNOREBLKID },
191 { "length=", TWFLAG_LENGTH },
192 { "mounttodecrypt", TWFLAG_MOUNTTODECRYPT },
Captain Throwbacke848afc2022-02-08 15:31:44 -0500193 { "quota", TWFLAG_QUOTA },
Matt Mower2416a502016-04-12 19:54:46 -0500194 { "removable", TWFLAG_REMOVABLE },
Matt Mower2416a502016-04-12 19:54:46 -0500195 { "settingsstorage", TWFLAG_SETTINGSSTORAGE },
196 { "storage", TWFLAG_STORAGE },
197 { "storagename=", TWFLAG_STORAGENAME },
198 { "subpartitionof=", TWFLAG_SUBPARTITIONOF },
199 { "symlink=", TWFLAG_SYMLINK },
200 { "userdataencryptbackup", TWFLAG_USERDATAENCRYPTBACKUP },
201 { "usermrf", TWFLAG_USERMRF },
202 { "wipeduringfactoryreset", TWFLAG_WIPEDURINGFACTORYRESET },
203 { "wipeingui", TWFLAG_WIPEINGUI },
Ethan Yonker1b190162016-12-05 15:25:19 -0600204 { "slotselect", TWFLAG_SLOTSELECT },
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600205 { "wait", TWFLAG_WAIT },
206 { "verify", TWFLAG_VERIFY },
207 { "check", TWFLAG_CHECK },
208 { "altdevice", TWFLAG_ALTDEVICE },
209 { "notrim", TWFLAG_NOTRIM },
210 { "voldmanaged=", TWFLAG_VOLDMANAGED },
211 { "formattable", TWFLAG_FORMATTABLE },
212 { "resize", TWFLAG_RESIZE },
Ethan Yonker93382822018-11-01 15:25:31 -0500213 { "keydirectory=", TWFLAG_KEYDIRECTORY },
Peter Cai28ea7b02019-05-24 11:38:54 +0800214 { "wrappedkey", TWFLAG_WRAPPEDKEY },
Peter Cai439d60c2019-10-17 08:49:10 +0800215 { "adopted_mount_delay=", TWFLAG_ADOPTED_MOUNT_DELAY },
Peter Caid68e6412019-11-02 19:22:38 +0800216 { "dm_use_original_path", TWFLAG_DM_USE_ORIGINAL_PATH },
bigbiff998f8392021-08-06 19:19:33 -0400217 { "logical", TWFLAG_LOGICAL },
Matt Mower2416a502016-04-12 19:54:46 -0500218 { 0, 0 },
219};
220
that9e0593e2014-10-08 00:01:24 +0200221TWPartition::TWPartition() {
Dees_Troy51a0e822012-09-05 15:24:24 -0400222 Can_Be_Mounted = false;
223 Can_Be_Wiped = false;
Dees_Troya13d74f2013-03-24 08:54:55 -0500224 Can_Be_Backed_Up = false;
Vojtech Bocek1dc30982013-08-30 21:49:30 +0200225 Use_Rm_Rf = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400226 Wipe_During_Factory_Reset = false;
227 Wipe_Available_in_GUI = false;
228 Is_SubPartition = false;
Dees_Troy2691f9d2012-09-24 11:15:49 -0400229 Has_SubPartition = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400230 SubPartition_Of = "";
231 Symlink_Path = "";
232 Symlink_Mount_Point = "";
233 Mount_Point = "";
Dees_Troye58d5262012-09-21 12:27:57 -0400234 Backup_Path = "";
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600235 Wildcard_Block_Device = false;
236 Sysfs_Entry = "";
Dees_Troy38bd7602012-09-14 13:33:53 -0400237 Actual_Block_Device = "";
238 Primary_Block_Device = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400239 Alternate_Block_Device = "";
240 Removable = false;
241 Is_Present = false;
242 Length = 0;
243 Size = 0;
244 Used = 0;
245 Free = 0;
246 Backup_Size = 0;
247 Can_Be_Encrypted = false;
248 Is_Encrypted = false;
249 Is_Decrypted = false;
Ethan Yonkerbd7492d2016-12-07 13:55:01 -0600250 Is_FBE = false;
Ethan Yonker253368a2014-11-25 15:00:52 -0600251 Mount_To_Decrypt = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400252 Decrypted_Block_Device = "";
253 Display_Name = "";
Dees_Troya13d74f2013-03-24 08:54:55 -0500254 Backup_Display_Name = "";
255 Storage_Name = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400256 Backup_Name = "";
Dees_Troy63c8df72012-09-10 14:02:05 -0400257 Backup_FileName = "";
Dees_Troy38bd7602012-09-14 13:33:53 -0400258 MTD_Name = "";
bigbiffce8f83c2015-12-12 18:30:21 -0500259 Backup_Method = BM_NONE;
Dees_Troy83bd4832013-05-04 12:39:56 +0000260 Can_Encrypt_Backup = false;
261 Use_Userdata_Encryption = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400262 Has_Data_Media = false;
Dees_Troye58d5262012-09-21 12:27:57 -0400263 Has_Android_Secure = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400264 Is_Storage = false;
Dees_Troya13d74f2013-03-24 08:54:55 -0500265 Is_Settings_Storage = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400266 Storage_Path = "";
267 Current_File_System = "";
268 Fstab_File_System = "";
Hashcode62bd9e02013-11-19 21:59:42 -0800269 Mount_Flags = 0;
270 Mount_Options = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400271 Format_Block_Size = 0;
Dees_Troy68cab492012-12-12 19:29:35 +0000272 Ignore_Blkid = false;
dianlujitao4879b372018-12-03 18:45:47 +0800273 Crypto_Key_Location = "";
Ethan Yonker726a0202014-12-16 20:01:38 -0600274 MTP_Storage_ID = 0;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600275 Can_Flash_Img = false;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500276 Mount_Read_Only = false;
Ethan Yonker66a19492015-12-10 10:19:45 -0600277 Is_Adopted_Storage = false;
278 Adopted_GUID = "";
Ethan Yonker1b190162016-12-05 15:25:19 -0600279 SlotSelect = false;
Ethan Yonker93382822018-11-01 15:25:31 -0500280 Key_Directory = "";
bigbiffee7b7ff2020-03-23 15:08:27 -0400281 Is_Super = false;
Peter Cai439d60c2019-10-17 08:49:10 +0800282 Adopted_Mount_Delay = 0;
Peter Caid68e6412019-11-02 19:22:38 +0800283 Original_Path = "";
284 Use_Original_Path = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400285}
286
287TWPartition::~TWPartition(void) {
288 // Do nothing
289}
290
bigbiff8da46fa2020-09-08 16:42:34 -0400291bool TWPartition::Process_Fstab_Line(const char *fstab_line, bool Display_Error, std::map<string, Flags_Map> *twrp_flags) {
Matt Mower2b2dd152016-04-26 11:24:08 -0500292 char full_line[MAX_FSTAB_LINE_LENGTH];
Matt Mower2416a502016-04-12 19:54:46 -0500293 char twflags[MAX_FSTAB_LINE_LENGTH] = "";
Dees_Troy5bf43922012-09-07 16:07:55 -0400294 char* ptr;
Matt Mower2b2dd152016-04-26 11:24:08 -0500295 int line_len = strlen(fstab_line), index = 0, item_index = 0;
Dees_Troya13d74f2013-03-24 08:54:55 -0500296 bool skip = false;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600297 int fstab_version = 1, mount_point_index = 0, fs_index = 1, block_device_index = 2;
298 TWPartition *additional_entry = NULL;
299 std::map<string, Flags_Map>::iterator it;
Dees_Troy5bf43922012-09-07 16:07:55 -0400300
Matt Mower2b2dd152016-04-26 11:24:08 -0500301 strlcpy(full_line, fstab_line, sizeof(full_line));
bigbiffee7b7ff2020-03-23 15:08:27 -0400302
Dees_Troy51127312012-09-08 13:08:49 -0400303 for (index = 0; index < line_len; index++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500304 if (full_line[index] == 34)
305 skip = !skip;
306 if (!skip && full_line[index] <= 32)
Dees_Troy5bf43922012-09-07 16:07:55 -0400307 full_line[index] = '\0';
Dees_Troy5bf43922012-09-07 16:07:55 -0400308 }
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600309 if (line_len < 10)
310 return false; // There can't possibly be a valid fstab line that is less than 10 chars
bigbiff998f8392021-08-06 19:19:33 -0400311 if (fstab_line[0] == '#')
312 return false; // skip comments
313
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600314 if (strncmp(fstab_line, "/dev/", strlen("/dev/")) == 0 || strncmp(fstab_line, "/devices/", strlen("/devices/")) == 0) {
315 fstab_version = 2;
316 block_device_index = 0;
317 mount_point_index = 1;
318 fs_index = 2;
319 }
320
bigbiff998f8392021-08-06 19:19:33 -0400321 if (fstab_line[0] != '/') {
bigbiffee7b7ff2020-03-23 15:08:27 -0400322 block_device_index = 0;
323 fstab_version = 2;
324 mount_point_index = 1;
325 fs_index = 2;
326 }
327
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600328 index = 0;
Dees_Troy5bf43922012-09-07 16:07:55 -0400329 while (index < line_len) {
330 while (index < line_len && full_line[index] == '\0')
331 index++;
332 if (index >= line_len)
333 continue;
334 ptr = full_line + index;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600335 if (item_index == mount_point_index) {
336 Mount_Point = ptr;
bigbiffee7b7ff2020-03-23 15:08:27 -0400337 if (fstab_version == 2 && Is_Super == false) {
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600338 additional_entry = PartitionManager.Find_Partition_By_Path(Mount_Point);
bigbiff8da46fa2020-09-08 16:42:34 -0400339 if (additional_entry) {
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600340 LOGINFO("Found an additional entry for '%s'\n", Mount_Point.c_str());
341 }
342 }
bigbiff8da46fa2020-09-08 16:42:34 -0400343 LOGINFO("Processing '%s'\n", Mount_Point.c_str());
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600344 Backup_Path = Mount_Point;
345 Storage_Path = Mount_Point;
346 Display_Name = ptr + 1;
347 Backup_Display_Name = Display_Name;
348 Storage_Name = Display_Name;
349 item_index++;
350 } else if (item_index == fs_index) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400351 // File System
352 Fstab_File_System = ptr;
353 Current_File_System = ptr;
354 item_index++;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600355 } else if (item_index == block_device_index) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400356 // Primary Block Device
Dees_Troy38bd7602012-09-14 13:33:53 -0400357 if (Fstab_File_System == "mtd" || Fstab_File_System == "yaffs2") {
Dees_Troy094207a2012-09-26 12:00:39 -0400358 MTD_Name = ptr;
359 Find_MTD_Block_Device(MTD_Name);
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400360 } else if (Fstab_File_System == "bml") {
361 if (Mount_Point == "/boot")
362 MTD_Name = "boot";
363 else if (Mount_Point == "/recovery")
364 MTD_Name = "recovery";
365 Primary_Block_Device = ptr;
366 if (*ptr != '/')
Dees_Troy2673cec2013-04-02 20:22:16 +0000367 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 -0400368 } else {
369 Primary_Block_Device = ptr;
bigbiff998f8392021-08-06 19:19:33 -0400370 if (*ptr == '/')
371 Find_Real_Block_Device(Primary_Block_Device, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400372 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400373 item_index++;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600374 } else if (item_index > 2) {
375 if (fstab_version == 2) {
376 if (item_index == 3) {
377 Process_FS_Flags(ptr);
378 if (additional_entry) {
379 additional_entry->Save_FS_Flags(Fstab_File_System, Mount_Flags, Mount_Options);
380 return false; // We save the extra fs flags in the other partition entry and by returning false, this entry will be deleted
381 }
382 } else {
383 strlcpy(twflags, ptr, sizeof(twflags));
384 }
385 item_index++;
386 } else if (*ptr == '/') { // v2 fstab does not allow alternate block devices
Dees_Troy5bf43922012-09-07 16:07:55 -0400387 // Alternate Block Device
388 Alternate_Block_Device = ptr;
389 Find_Real_Block_Device(Alternate_Block_Device, Display_Error);
390 } else if (strlen(ptr) > 7 && strncmp(ptr, "length=", 7) == 0) {
391 // Partition length
392 ptr += 7;
393 Length = atoi(ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400394 } else if (strlen(ptr) > 6 && strncmp(ptr, "flags=", 6) == 0) {
395 // Custom flags, save for later so that new values aren't overwritten by defaults
396 ptr += 6;
Matt Mower2416a502016-04-12 19:54:46 -0500397 strlcpy(twflags, ptr, sizeof(twflags));
Dees_Troy38bd7602012-09-14 13:33:53 -0400398 } else if (strlen(ptr) == 4 && (strncmp(ptr, "NULL", 4) == 0 || strncmp(ptr, "null", 4) == 0 || strncmp(ptr, "null", 4) == 0)) {
399 // Do nothing
Dees_Troy5bf43922012-09-07 16:07:55 -0400400 } else {
401 // Unhandled data
Matt Mower2b2dd152016-04-26 11:24:08 -0500402 LOGINFO("Unhandled fstab information '%s' in fstab line '%s'\n", ptr, fstab_line);
Dees_Troy5bf43922012-09-07 16:07:55 -0400403 }
404 }
405 while (index < line_len && full_line[index] != '\0')
406 index++;
407 }
408
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600409 // override block devices from the v2 fstab with the ones we read from the twrp.flags file in case they are different
410 if (fstab_version == 2 && twrp_flags && twrp_flags->size() > 0) {
411 it = twrp_flags->find(Mount_Point);
412 if (it != twrp_flags->end()) {
413 if (!it->second.Primary_Block_Device.empty()) {
414 Primary_Block_Device = it->second.Primary_Block_Device;
415 Find_Real_Block_Device(Primary_Block_Device, Display_Error);
416 }
417 if (!it->second.Alternate_Block_Device.empty()) {
418 Alternate_Block_Device = it->second.Alternate_Block_Device;
419 Find_Real_Block_Device(Alternate_Block_Device, Display_Error);
420 }
421 }
422 }
423
424 if (strncmp(fstab_line, "/devices/", strlen("/devices/")) == 0) {
425 Sysfs_Entry = Primary_Block_Device;
426 Primary_Block_Device = "";
427 Is_Storage = true;
428 Removable = true;
429 Wipe_Available_in_GUI = true;
430 Wildcard_Block_Device = true;
431 }
432 if (Primary_Block_Device.find("*") != string::npos)
433 Wildcard_Block_Device = true;
434
bigbiff8da46fa2020-09-08 16:42:34 -0400435 if (Mount_Point == "auto") {
436 Mount_Point = "/auto";
437 char autoi[5];
438 sprintf(autoi, "%i", auto_index);
439 Mount_Point += autoi;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600440 Backup_Path = Mount_Point;
441 Storage_Path = Mount_Point;
dianlujitaob76a73a2020-04-30 20:33:20 +0800442 Backup_Name = Mount_Point.substr(1);
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600443 auto_index++;
444 Setup_File_System(Display_Error);
445 Display_Name = "Storage";
446 Backup_Display_Name = Display_Name;
447 Storage_Name = Display_Name;
448 Can_Be_Backed_Up = false;
449 Wipe_Available_in_GUI = true;
450 Is_Storage = true;
451 Removable = true;
452 Wipe_Available_in_GUI = true;
453 } else if (!Is_File_System(Fstab_File_System) && !Is_Image(Fstab_File_System)) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400454 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000455 LOGERR("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400456 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000457 LOGINFO("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Matt Mower72c87ce2016-04-26 14:34:56 -0500458 return false;
Dees_Troy5bf43922012-09-07 16:07:55 -0400459 } else if (Is_File_System(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400460 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400461 Setup_File_System(Display_Error);
dianlujitaob76a73a2020-04-30 20:33:20 +0800462 Backup_Name = Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
Chaosmaster3b682732020-01-25 21:09:28 +0100463 if (Mount_Point == "/" || Mount_Point == "/system" || Mount_Point == "/system_root") {
bigbiff8da46fa2020-09-08 16:42:34 -0400464 Mount_Point = PartitionManager.Get_Android_Root_Path();
465 Backup_Path = Mount_Point;
466 Storage_Path = Mount_Point;
Dees_Troy5bf43922012-09-07 16:07:55 -0400467 Display_Name = "System";
dianlujitaob76a73a2020-04-30 20:33:20 +0800468 Backup_Name = "system";
Dees_Troya13d74f2013-03-24 08:54:55 -0500469 Backup_Display_Name = Display_Name;
470 Storage_Name = Display_Name;
bigbiff8da46fa2020-09-08 16:42:34 -0400471 Wipe_Available_in_GUI = false;
472 Can_Be_Backed_Up = false;
473 Can_Be_Wiped = false;
bigbiff8da46fa2020-09-08 16:42:34 -0400474 Make_Dir(PartitionManager.Get_Android_Root_Path(), true);
me-cafebabea021ac22022-07-29 06:34:45 +0800475 } else if (Mount_Point == "/system_ext") {
476 Display_Name = "System_EXT";
477 Backup_Name = "System_EXT";
478 Backup_Display_Name = Display_Name;
479 Storage_Name = Display_Name;
480 Can_Be_Backed_Up = Wipe_Available_in_GUI = Is_Super ? false : true;
Mohd Faraze3948ec2020-08-14 01:40:59 +0000481 } else if (Mount_Point == "/product") {
482 Display_Name = "Product";
483 Backup_Name = "Product";
484 Backup_Display_Name = Display_Name;
485 Storage_Name = Display_Name;
486 Can_Be_Backed_Up = Wipe_Available_in_GUI = Is_Super ? false : true;
Mohd Faraze3948ec2020-08-14 01:40:59 +0000487 } else if (Mount_Point == "/odm") {
488 Display_Name = "ODM";
489 Backup_Name = "ODM";
490 Backup_Display_Name = Display_Name;
491 Storage_Name = Display_Name;
492 Can_Be_Backed_Up = Wipe_Available_in_GUI = Is_Super ? false : true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400493 } else if (Mount_Point == "/data") {
494 Display_Name = "Data";
Dees_Troya13d74f2013-03-24 08:54:55 -0500495 Backup_Display_Name = Display_Name;
496 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400497 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400498 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500499 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000500 Can_Encrypt_Backup = true;
501 Use_Userdata_Encryption = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400502 } else if (Mount_Point == "/cache") {
503 Display_Name = "Cache";
Dees_Troya13d74f2013-03-24 08:54:55 -0500504 Backup_Display_Name = Display_Name;
505 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400506 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400507 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500508 Can_Be_Backed_Up = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400509 } else if (Mount_Point == "/datadata") {
Dees_Troy51127312012-09-08 13:08:49 -0400510 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400511 Display_Name = "DataData";
Dees_Troya13d74f2013-03-24 08:54:55 -0500512 Backup_Display_Name = Display_Name;
513 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400514 Is_SubPartition = true;
515 SubPartition_Of = "/data";
Dees_Troy5bf43922012-09-07 16:07:55 -0400516 DataManager::SetValue(TW_HAS_DATADATA, 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500517 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000518 Can_Encrypt_Backup = true;
519 Use_Userdata_Encryption = false; // This whole partition should be encrypted
Dees_Troy5bf43922012-09-07 16:07:55 -0400520 } else if (Mount_Point == "/sd-ext") {
Dees_Troy51127312012-09-08 13:08:49 -0400521 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400522 Display_Name = "SD-Ext";
Dees_Troya13d74f2013-03-24 08:54:55 -0500523 Backup_Display_Name = Display_Name;
524 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400525 Wipe_Available_in_GUI = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400526 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500527 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000528 Can_Encrypt_Backup = true;
529 Use_Userdata_Encryption = true;
Dees_Troy2c50e182012-09-26 20:05:28 -0400530 } else if (Mount_Point == "/boot") {
531 Display_Name = "Boot";
Dees_Troya13d74f2013-03-24 08:54:55 -0500532 Backup_Display_Name = Display_Name;
Dees_Troy2c50e182012-09-26 20:05:28 -0400533 DataManager::SetValue("tw_boot_is_mountable", 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500534 Can_Be_Backed_Up = true;
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500535 } else if (Mount_Point == "/vendor") {
536 Display_Name = "Vendor";
537 Backup_Display_Name = Display_Name;
538 Storage_Name = Display_Name;
Dees_Troy8170a922012-09-18 15:40:25 -0400539 }
540#ifdef TW_EXTERNAL_STORAGE_PATH
541 if (Mount_Point == EXPAND(TW_EXTERNAL_STORAGE_PATH)) {
542 Is_Storage = true;
543 Storage_Path = EXPAND(TW_EXTERNAL_STORAGE_PATH);
Dees_Troyc51f1f92012-09-20 15:32:13 -0400544 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500545 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400546#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000547 if (Mount_Point == "/sdcard" || Mount_Point == "/external_sd" || Mount_Point == "/external_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400548 Is_Storage = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400549 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500550 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400551#endif
Dees_Troyb05ddee2013-01-28 20:24:50 +0000552 }
Dees_Troy8170a922012-09-18 15:40:25 -0400553#ifdef TW_INTERNAL_STORAGE_PATH
554 if (Mount_Point == EXPAND(TW_INTERNAL_STORAGE_PATH)) {
555 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500556 Is_Settings_Storage = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400557 Storage_Path = EXPAND(TW_INTERNAL_STORAGE_PATH);
Dees_Troya13d74f2013-03-24 08:54:55 -0500558 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400559 }
560#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000561 if (Mount_Point == "/emmc" || Mount_Point == "/internal_sd" || Mount_Point == "/internal_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400562 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500563 Is_Settings_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500564 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400565 }
566#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400567 } else if (Is_Image(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400568 Find_Actual_Block_Device();
Ethan Yonker1b190162016-12-05 15:25:19 -0600569 Setup_Image();
Dees_Troya13d74f2013-03-24 08:54:55 -0500570 if (Mount_Point == "/boot") {
571 Display_Name = "Boot";
572 Backup_Display_Name = Display_Name;
573 Can_Be_Backed_Up = true;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600574 Can_Flash_Img = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500575 } else if (Mount_Point == "/recovery") {
576 Display_Name = "Recovery";
577 Backup_Display_Name = Display_Name;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600578 Can_Flash_Img = true;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500579 } else if (Mount_Point == "/system_image") {
580 Display_Name = "System Image";
581 Backup_Display_Name = Display_Name;
HashBanged974bb2016-01-30 14:20:09 -0500582 Can_Flash_Img = true;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500583 Can_Be_Backed_Up = true;
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500584 } else if (Mount_Point == "/vendor_image") {
585 Display_Name = "Vendor Image";
586 Backup_Display_Name = Display_Name;
HashBanged974bb2016-01-30 14:20:09 -0500587 Can_Flash_Img = true;
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500588 Can_Be_Backed_Up = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500589 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400590 }
591
Matt Mower2416a502016-04-12 19:54:46 -0500592 // Process TWRP fstab flags
593 if (strlen(twflags) > 0) {
594 string Prev_Display_Name = Display_Name;
595 string Prev_Storage_Name = Storage_Name;
596 string Prev_Backup_Display_Name = Backup_Display_Name;
597 Display_Name = "";
598 Storage_Name = "";
599 Backup_Display_Name = "";
600
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600601 Process_TW_Flags(twflags, (fstab_version == 1), fstab_version);
602 Save_FS_Flags(Fstab_File_System, Mount_Flags, Mount_Options);
Matt Mower2416a502016-04-12 19:54:46 -0500603
604 bool has_display_name = !Display_Name.empty();
605 bool has_storage_name = !Storage_Name.empty();
606 bool has_backup_name = !Backup_Display_Name.empty();
607 if (!has_display_name) Display_Name = Prev_Display_Name;
608 if (!has_storage_name) Storage_Name = Prev_Storage_Name;
609 if (!has_backup_name) Backup_Display_Name = Prev_Backup_Display_Name;
610
611 if (has_display_name && !has_storage_name)
612 Storage_Name = Display_Name;
613 if (!has_display_name && has_storage_name)
614 Display_Name = Storage_Name;
615 if (has_display_name && !has_backup_name && Backup_Display_Name != "Android Secure")
616 Backup_Display_Name = Display_Name;
617 if (!has_display_name && has_backup_name)
618 Display_Name = Backup_Display_Name;
619 }
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600620
621 if (fstab_version == 2 && twrp_flags && twrp_flags->size() > 0) {
622 it = twrp_flags->find(Mount_Point);
623 if (it != twrp_flags->end()) {
624 char twrpflags[MAX_FSTAB_LINE_LENGTH] = "";
625 int skip = 0;
626 string Flags = it->second.Flags;
627 strcpy(twrpflags, Flags.c_str());
628 if (strlen(twrpflags) > strlen("flags=") && strncmp(twrpflags, "flags=", strlen("flags=")) == 0)
629 skip += strlen("flags=");
630 char* flagptr = twrpflags;
631 flagptr += skip;
632 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
633 }
634 }
nkk7198fc3992017-12-16 16:26:42 +0200635
636 if (Mount_Point == "/persist" && Can_Be_Mounted) {
637 bool mounted = Is_Mounted();
638 if (mounted || Mount(false)) {
nkk7198fc3992017-12-16 16:26:42 +0200639 TWFunc::Fixup_Time_On_Boot("/persist/time/");
640 if (!mounted)
641 UnMount(false);
642 }
643 }
644
Dees_Troy51127312012-09-08 13:08:49 -0400645 return true;
646}
647
Matt Mower72c87ce2016-04-26 14:34:56 -0500648void TWPartition::Partition_Post_Processing(bool Display_Error) {
649 if (Mount_Point == "/data")
650 Setup_Data_Partition(Display_Error);
651 else if (Mount_Point == "/cache")
652 Setup_Cache_Partition(Display_Error);
653}
654
Ethan Yonkerbd7492d2016-12-07 13:55:01 -0600655void TWPartition::ExcludeAll(const string& path) {
656 backup_exclusions.add_absolute_dir(path);
657 wipe_exclusions.add_absolute_dir(path);
658}
659
Matt Mower72c87ce2016-04-26 14:34:56 -0500660void TWPartition::Setup_Data_Partition(bool Display_Error) {
661 if (Mount_Point != "/data")
662 return;
663
664 // Ensure /data is not mounted as tmpfs for qcom hardware decrypt
665 UnMount(false);
666
667#ifdef TW_INCLUDE_CRYPTO
Darth98bc9da52023-02-05 23:41:03 +0000668 #ifdef TW_PREPARE_DATA_MEDIA_EARLY
669 if (datamedia)
670 Setup_Data_Media();
671 #endif
Matt Mower72c87ce2016-04-26 14:34:56 -0500672 Can_Be_Encrypted = true;
673 char crypto_blkdev[255];
674 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
675 if (strcmp(crypto_blkdev, "error") != 0) {
bigbiff7ba75002020-04-11 20:47:09 -0400676 Set_FBE_Status();
Matt Mower72c87ce2016-04-26 14:34:56 -0500677 Decrypted_Block_Device = crypto_blkdev;
678 LOGINFO("Data already decrypted, new block device: '%s'\n", crypto_blkdev);
Darth98bc9da52023-02-05 23:41:03 +0000679 #ifndef TW_PREPARE_DATA_MEDIA_EARLY
bigbiffcda15882021-12-30 17:01:44 -0500680 if (datamedia)
681 Setup_Data_Media();
Darth98bc9da52023-02-05 23:41:03 +0000682 #endif
bigbiff7ba75002020-04-11 20:47:09 -0400683 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
Matt Mower72c87ce2016-04-26 14:34:56 -0500684 } else if (!Mount(false)) {
685 if (Is_Present) {
Ethan Yonker93382822018-11-01 15:25:31 -0500686 if (Key_Directory.empty()) {
Peter Caid68e6412019-11-02 19:22:38 +0800687 set_partition_data(Use_Original_Path ? Original_Path.c_str() : Actual_Block_Device.c_str(), Crypto_Key_Location.c_str(),
Noah Jacobson81d638d2019-04-28 00:10:07 -0400688 Fstab_File_System.c_str());
Ethan Yonker93382822018-11-01 15:25:31 -0500689 if (cryptfs_check_footer() == 0) {
690 Is_Encrypted = true;
691 Is_Decrypted = false;
692 Can_Be_Mounted = false;
693 Current_File_System = "emmc";
694 Setup_Image();
Ethan Yonker93382822018-11-01 15:25:31 -0500695 DataManager::SetValue(TW_CRYPTO_PWTYPE, cryptfs_get_password_type());
Noah Jacobson81d638d2019-04-28 00:10:07 -0400696 DataManager::SetValue("tw_crypto_pwtype_0", cryptfs_get_password_type());
Ethan Yonker93382822018-11-01 15:25:31 -0500697 DataManager::SetValue(TW_CRYPTO_PASSWORD, "");
698 DataManager::SetValue("tw_crypto_display", "");
bigbiffcda15882021-12-30 17:01:44 -0500699 if (datamedia)
700 Setup_Data_Media();
Ethan Yonker93382822018-11-01 15:25:31 -0500701 } else {
702 gui_err("mount_data_footer=Could not mount /data and unable to find crypto footer.");
703 }
704 } else {
Matt Mower72c87ce2016-04-26 14:34:56 -0500705 Is_Encrypted = true;
706 Is_Decrypted = false;
bigbiffbee26852022-02-09 20:49:00 -0500707 if (datamedia)
708 Setup_Data_Media();
Matt Mower72c87ce2016-04-26 14:34:56 -0500709 }
Ethan Yonker93382822018-11-01 15:25:31 -0500710 } else if (Key_Directory.empty()) {
Noah Jacobson81d638d2019-04-28 00:10:07 -0400711 LOGERR("Primary block device '%s' for mount point '%s' is not present!\n",
712 Primary_Block_Device.c_str(), Mount_Point.c_str());
Matt Mower72c87ce2016-04-26 14:34:56 -0500713 }
714 } else {
bigbiff7ba75002020-04-11 20:47:09 -0400715 Set_FBE_Status();
Captain Throwbackacc7f0f2020-12-29 15:54:33 -0500716 int is_device_fbe;
717 DataManager::GetValue(TW_IS_FBE, is_device_fbe);
Captain Throwback8a8e56c2021-10-01 15:12:24 -0400718 char crypto_state[255];
719 property_get("ro.crypto.state", crypto_state, "error");
720 if (!Decrypt_FBE_DE() && strcmp(crypto_state, "error") != 0) {
721 if (is_device_fbe == 1)
722 LOGERR("Unable to decrypt FBE device\n");
723 } else {
724 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
Darth98bc9da52023-02-05 23:41:03 +0000725 #ifndef TW_PREPARE_DATA_MEDIA_EARLY
bigbiffcda15882021-12-30 17:01:44 -0500726 if (datamedia)
727 Setup_Data_Media();
Darth98bc9da52023-02-05 23:41:03 +0000728 #endif
mauronofrio9bf73352019-11-23 22:27:34 +0100729 }
Matt Mower72c87ce2016-04-26 14:34:56 -0500730 }
731 if (datamedia && (!Is_Encrypted || (Is_Encrypted && Is_Decrypted))) {
732 Setup_Data_Media();
bigbiff25d25b92020-06-19 16:07:38 -0400733 Recreate_Media_Folder();
Matt Mower72c87ce2016-04-26 14:34:56 -0500734 }
735#else
736 if (datamedia) {
737 Setup_Data_Media();
738 Recreate_Media_Folder();
739 }
740#endif
741}
742
bigbiff7ba75002020-04-11 20:47:09 -0400743void TWPartition::Set_FBE_Status() {
744 DataManager::SetValue(TW_IS_DECRYPTED, 1);
745 Is_Encrypted = true;
746 Is_Decrypted = true;
bigbiff7ba75002020-04-11 20:47:09 -0400747 if (Key_Directory.empty()) {
748 Is_FBE = false;
749 DataManager::SetValue(TW_IS_FBE, 0);
750 } else {
Captain Throwbackb5af4f82020-12-29 16:01:10 -0500751 LOGINFO("Setup_Data_Partition::Key_Directory::%s\n", Key_Directory.c_str());
bigbiff7ba75002020-04-11 20:47:09 -0400752 Is_FBE = true;
753 DataManager::SetValue(TW_IS_FBE, 1);
754 }
755}
756
Ethan Yonker93382822018-11-01 15:25:31 -0500757bool TWPartition::Decrypt_FBE_DE() {
bigbiff7ba75002020-04-11 20:47:09 -0400758 if (TWFunc::Path_Exists("/data/unencrypted/key/version")) {
Captain Throwback87d73d62020-02-17 16:44:50 -0500759 DataManager::SetValue(TW_IS_FBE, 1);
Captain Throwback37c3aef2021-10-06 11:38:18 -0400760 PartitionManager.Set_Crypto_State();
761 PartitionManager.Set_Crypto_Type("file");
Ethan Yonker93382822018-11-01 15:25:31 -0500762 LOGINFO("File Based Encryption is present\n");
763#ifdef TW_INCLUDE_FBE
Noah Jacobson81d638d2019-04-28 00:10:07 -0400764 Is_FBE = true;
765 ExcludeAll(Mount_Point + "/convert_fbe");
766 ExcludeAll(Mount_Point + "/unencrypted");
767 ExcludeAll(Mount_Point + "/misc/vold/user_keys");
768 ExcludeAll(Mount_Point + "/misc/vold/volume_keys");
769 ExcludeAll(Mount_Point + "/system/gatekeeper.password.key");
770 ExcludeAll(Mount_Point + "/system/gatekeeper.pattern.key");
771 ExcludeAll(Mount_Point + "/system/locksettings.db");
772 ExcludeAll(Mount_Point + "/system/locksettings.db-wal");
773 ExcludeAll(Mount_Point + "/misc/gatekeeper");
774 ExcludeAll(Mount_Point + "/misc/keystore");
775 ExcludeAll(Mount_Point + "/drm/kek.dat");
bigbiffa957f072021-03-07 18:20:29 -0500776 ExcludeAll(Mount_Point + "/system_de/0/spblob"); // contains data needed to decrypt synthetic password
micky3872ec1df42021-10-14 14:53:06 +0200777 ExcludeAll(Mount_Point + "/system/users/0/gatekeeper.password.key");
778 ExcludeAll(Mount_Point + "/system/users/0/gatekeeper.pattern.key");
Noah Jacobson81d638d2019-04-28 00:10:07 -0400779 ExcludeAll(Mount_Point + "/cache");
780 ExcludeAll(Mount_Point + "/per_boot"); // removed each boot by init
bigbiffa15d02f2021-06-29 20:04:54 -0400781 ExcludeAll(Mount_Point + "/gsi"); // cow devices
782
Noah Jacobson81d638d2019-04-28 00:10:07 -0400783 int retry_count = 3;
784 while (!Decrypt_DE() && --retry_count)
785 usleep(2000);
786 if (retry_count > 0) {
Captain Throwback37c3aef2021-10-06 11:38:18 -0400787 PartitionManager.Set_Crypto_State();
Noah Jacobson81d638d2019-04-28 00:10:07 -0400788 Is_Encrypted = true;
789 Is_Decrypted = false;
790 DataManager::SetValue(TW_IS_ENCRYPTED, 1);
791 string filename;
792 int pwd_type = Get_Password_Type(0, filename);
793 if (pwd_type < 0) {
794 LOGERR("This TWRP does not have synthetic password decrypt support\n");
795 pwd_type = 0; // default password
Ethan Yonker93382822018-11-01 15:25:31 -0500796 }
Noah Jacobson81d638d2019-04-28 00:10:07 -0400797 PartitionManager.Parse_Users(); // after load_all_de_keys() to parse_users
798 std::vector<users_struct>::iterator iter;
799 std::vector<users_struct>* userList = PartitionManager.Get_Users_List();
800 for (iter = userList->begin(); iter != userList->end(); iter++) {
801 if (atoi((*iter).userId.c_str()) != 0) {
802 ExcludeAll(Mount_Point + "/system_de/" + (*iter).userId + "/spblob");
803 ExcludeAll(Mount_Point + "/system/users/" + (*iter).userId + "/gatekeeper.password.key");
804 ExcludeAll(Mount_Point + "/system/users/" + (*iter).userId + "/gatekeeper.pattern.key");
805 ExcludeAll(Mount_Point + "/system/users/" + (*iter).userId + "/locksettings.db");
806 ExcludeAll(Mount_Point + "/system/users/" + (*iter).userId + "/locksettings.db-wal");
807 }
808 }
809 DataManager::SetValue(TW_CRYPTO_PWTYPE, pwd_type);
810 DataManager::SetValue("tw_crypto_pwtype_0", pwd_type);
811 DataManager::SetValue(TW_CRYPTO_PASSWORD, "");
812 DataManager::SetValue("tw_crypto_display", "");
813 return true;
814 }
Ethan Yonker93382822018-11-01 15:25:31 -0500815#else
816 LOGERR("FBE found but FBE support not present in TWRP\n");
817#endif
818 }
Captain Throwback87d73d62020-02-17 16:44:50 -0500819 DataManager::SetValue(TW_IS_FBE, 0);
Ethan Yonker93382822018-11-01 15:25:31 -0500820 return false;
821}
822
Matt Mower72c87ce2016-04-26 14:34:56 -0500823void TWPartition::Setup_Cache_Partition(bool Display_Error __unused) {
824 if (Mount_Point != "/cache")
825 return;
826
827 if (!Mount(true))
828 return;
829
830 if (!TWFunc::Path_Exists("/cache/recovery/.")) {
831 LOGINFO("Recreating /cache/recovery folder\n");
832 if (mkdir("/cache/recovery", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0)
833 LOGERR("Could not create /cache/recovery\n");
834 }
835}
836
Matt Mower4ab42b12016-04-21 13:52:18 -0500837void TWPartition::Process_FS_Flags(const char *str) {
838 char *options = strdup(str);
839 char *ptr, *savep;
Hashcode62bd9e02013-11-19 21:59:42 -0800840
Matt Mower4ab42b12016-04-21 13:52:18 -0500841 Mount_Options = "";
Hashcode62bd9e02013-11-19 21:59:42 -0800842
Matt Mower4ab42b12016-04-21 13:52:18 -0500843 // Avoid issues with potentially nested strtok by using strtok_r
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600844 for (ptr = strtok_r(options, ",", &savep); ptr; ptr = strtok_r(NULL, ",", &savep)) {
845 char *equals = strstr(ptr, "=");
846 size_t name_len;
Matt Mower4ab42b12016-04-21 13:52:18 -0500847
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600848 if (!equals)
849 name_len = strlen(ptr);
850 else
851 name_len = equals - ptr;
852
853 // There are some flags that we want to ignore in TWRP
854 bool found_match = false;
855 for (const char** ignored_mount_item = ignored_mount_items; *ignored_mount_item; ignored_mount_item++) {
856 if (strncmp(ptr, *ignored_mount_item, name_len) == 0) {
857 found_match = true;
Hashcode62bd9e02013-11-19 21:59:42 -0800858 break;
859 }
860 }
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600861 if (found_match)
862 continue;
Hashcode62bd9e02013-11-19 21:59:42 -0800863
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600864 // mount_flags are never postfixed by '='
865 if (!equals) {
866 const struct flag_list* mount_flag = mount_flags;
867 for (; mount_flag->name; mount_flag++) {
868 if (strcmp(ptr, mount_flag->name) == 0) {
869 if (mount_flag->flag == MS_RDONLY)
870 Mount_Read_Only = true;
871 else
872 Mount_Flags |= (unsigned)mount_flag->flag;
873 found_match = true;
874 break;
875 }
876 }
877 if (found_match)
878 continue;
Matt Mower4ab42b12016-04-21 13:52:18 -0500879 }
880
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600881 // If we aren't ignoring this flag and it's not a mount flag, then it must be a mount option
882 if (!Mount_Options.empty())
883 Mount_Options += ",";
884 Mount_Options += ptr;
Matt Mower4ab42b12016-04-21 13:52:18 -0500885 }
886 free(options);
Hashcode62bd9e02013-11-19 21:59:42 -0800887}
888
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600889void TWPartition::Save_FS_Flags(const string& local_File_System, int local_Mount_Flags, const string& local_Mount_Options) {
890 partition_fs_flags_struct flags;
891 flags.File_System = local_File_System;
892 flags.Mount_Flags = local_Mount_Flags;
893 flags.Mount_Options = local_Mount_Options;
894 fs_flags.push_back(flags);
895}
896
Matt Mower2416a502016-04-12 19:54:46 -0500897void TWPartition::Apply_TW_Flag(const unsigned flag, const char* str, const bool val) {
898 switch (flag) {
899 case TWFLAG_ANDSEC:
900 Has_Android_Secure = val;
901 break;
902 case TWFLAG_BACKUP:
903 Can_Be_Backed_Up = val;
904 break;
905 case TWFLAG_BACKUPNAME:
906 Backup_Display_Name = str;
907 break;
908 case TWFLAG_BLOCKSIZE:
909 Format_Block_Size = (unsigned long)(atol(str));
910 break;
911 case TWFLAG_CANBEWIPED:
912 Can_Be_Wiped = val;
913 break;
914 case TWFLAG_CANENCRYPTBACKUP:
915 Can_Encrypt_Backup = val;
916 break;
917 case TWFLAG_DEFAULTS:
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600918 case TWFLAG_WAIT:
919 case TWFLAG_VERIFY:
920 case TWFLAG_CHECK:
921 case TWFLAG_NOTRIM:
922 case TWFLAG_VOLDMANAGED:
923 case TWFLAG_RESIZE:
Matt Mower2416a502016-04-12 19:54:46 -0500924 // Do nothing
925 break;
926 case TWFLAG_DISPLAY:
927 Display_Name = str;
928 break;
929 case TWFLAG_ENCRYPTABLE:
930 case TWFLAG_FORCEENCRYPT:
931 Crypto_Key_Location = str;
932 break;
Ethan Yonkere9afc3d2018-08-30 15:16:27 -0500933 case TWFLAG_FILEENCRYPTION:
934 // This flag isn't used by TWRP but is needed in 9.0 FBE decrypt
935 // fileencryption=ice:aes-256-heh
936 {
937 std::string FBE = str;
Ethan Yonkere9afc3d2018-08-30 15:16:27 -0500938 size_t colon_loc = FBE.find(":");
939 if (colon_loc == std::string::npos) {
Ethan Yonker93382822018-11-01 15:25:31 -0500940 property_set("fbe.contents", FBE.c_str());
941 property_set("fbe.filenames", "");
942 LOGINFO("FBE contents '%s', filenames ''\n", FBE.c_str());
Ethan Yonkere9afc3d2018-08-30 15:16:27 -0500943 break;
944 }
Ethan Yonker93382822018-11-01 15:25:31 -0500945 std::string FBE_contents, FBE_filenames;
Ethan Yonkere9afc3d2018-08-30 15:16:27 -0500946 FBE_contents = FBE.substr(0, colon_loc);
947 FBE_filenames = FBE.substr(colon_loc + 1);
948 property_set("fbe.contents", FBE_contents.c_str());
949 property_set("fbe.filenames", FBE_filenames.c_str());
950 LOGINFO("FBE contents '%s', filenames '%s'\n", FBE_contents.c_str(), FBE_filenames.c_str());
951 }
952 break;
Captain Throwback32bcdcd2022-02-08 12:45:01 -0500953 case TWFLAG_METADATA_ENCRYPTION:
954 // This flag isn't used by TWRP but is needed for FBEv2 metadata decryption
955 // metadata_encryption=aes-256-xts:wrappedkey_v0
956 {
957 std::string META = str;
958 size_t colon_loc = META.find(":");
959 if (colon_loc == std::string::npos) {
960 property_set("metadata.contents", META.c_str());
961 property_set("metadata.filenames", "");
962 LOGINFO("Metadata contents '%s', filenames ''\n", META.c_str());
963 break;
964 }
965 std::string META_contents, META_filenames;
966 META_contents = META.substr(0, colon_loc);
967 META_filenames = META.substr(colon_loc + 1);
968 property_set("metadata.contents", META_contents.c_str());
969 property_set("metadata.filenames", META_filenames.c_str());
970 LOGINFO("Metadata contents '%s', filenames '%s'\n", META_contents.c_str(), META_filenames.c_str());
971 }
972 break;
Peter Cai28ea7b02019-05-24 11:38:54 +0800973 case TWFLAG_WRAPPEDKEY:
bigbiff437d86f2021-09-26 16:57:14 -0400974 // no more processing needed. leaving it here in case we want to do something in the future
Peter Cai28ea7b02019-05-24 11:38:54 +0800975 break;
Matt Mower2416a502016-04-12 19:54:46 -0500976 case TWFLAG_FLASHIMG:
977 Can_Flash_Img = val;
978 break;
979 case TWFLAG_FSFLAGS:
Matt Mower4ab42b12016-04-21 13:52:18 -0500980 Process_FS_Flags(str);
Matt Mower2416a502016-04-12 19:54:46 -0500981 break;
982 case TWFLAG_IGNOREBLKID:
983 Ignore_Blkid = val;
984 break;
985 case TWFLAG_LENGTH:
986 Length = atoi(str);
987 break;
988 case TWFLAG_MOUNTTODECRYPT:
989 Mount_To_Decrypt = val;
990 break;
Captain Throwbacke848afc2022-02-08 15:31:44 -0500991 case TWFLAG_QUOTA:
992 // Filesystem flag - TWRP does not need to process
993 break;
Matt Mower2416a502016-04-12 19:54:46 -0500994 case TWFLAG_REMOVABLE:
995 Removable = val;
996 break;
Matt Mower2416a502016-04-12 19:54:46 -0500997 case TWFLAG_SETTINGSSTORAGE:
998 Is_Settings_Storage = val;
999 if (Is_Settings_Storage)
1000 Is_Storage = true;
1001 break;
1002 case TWFLAG_STORAGE:
1003 Is_Storage = val;
1004 break;
1005 case TWFLAG_STORAGENAME:
1006 Storage_Name = str;
1007 break;
1008 case TWFLAG_SUBPARTITIONOF:
1009 Is_SubPartition = true;
1010 SubPartition_Of = str;
1011 break;
1012 case TWFLAG_SYMLINK:
1013 Symlink_Path = str;
1014 break;
1015 case TWFLAG_USERDATAENCRYPTBACKUP:
1016 Use_Userdata_Encryption = val;
1017 if (Use_Userdata_Encryption)
1018 Can_Encrypt_Backup = true;
1019 break;
1020 case TWFLAG_USERMRF:
1021 Use_Rm_Rf = val;
1022 break;
1023 case TWFLAG_WIPEDURINGFACTORYRESET:
1024 Wipe_During_Factory_Reset = val;
1025 if (Wipe_During_Factory_Reset) {
1026 Can_Be_Wiped = true;
1027 Wipe_Available_in_GUI = true;
1028 }
1029 break;
1030 case TWFLAG_WIPEINGUI:
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06001031 case TWFLAG_FORMATTABLE:
Matt Mower2416a502016-04-12 19:54:46 -05001032 Wipe_Available_in_GUI = val;
1033 if (Wipe_Available_in_GUI)
1034 Can_Be_Wiped = true;
1035 break;
Ethan Yonker1b190162016-12-05 15:25:19 -06001036 case TWFLAG_SLOTSELECT:
1037 SlotSelect = true;
1038 break;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06001039 case TWFLAG_ALTDEVICE:
1040 Alternate_Block_Device = str;
1041 break;
Peter Cai439d60c2019-10-17 08:49:10 +08001042 case TWFLAG_ADOPTED_MOUNT_DELAY:
1043 Adopted_Mount_Delay = atoi(str);
1044 break;
Ethan Yonker93382822018-11-01 15:25:31 -05001045 case TWFLAG_KEYDIRECTORY:
1046 Key_Directory = str;
bigbiffa957f072021-03-07 18:20:29 -05001047 LOGINFO("setting Key_Directory to: %s\n", Key_Directory.c_str());
bigbiffd58ba182020-03-23 10:02:29 -04001048 break;
Peter Caid68e6412019-11-02 19:22:38 +08001049 case TWFLAG_DM_USE_ORIGINAL_PATH:
1050 Use_Original_Path = true;
1051 break;
bigbiff998f8392021-08-06 19:19:33 -04001052 case TWFLAG_LOGICAL:
1053 Is_Super = true;
1054 break;
Matt Mower2416a502016-04-12 19:54:46 -05001055 default:
1056 // Should not get here
1057 LOGINFO("Flag identified for processing, but later unmatched: %i\n", flag);
1058 break;
1059 }
1060}
Dees_Troy51127312012-09-08 13:08:49 -04001061
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06001062void TWPartition::Process_TW_Flags(char *flags, bool Display_Error, int fstab_ver) {
Matt Mower2416a502016-04-12 19:54:46 -05001063 char separator[2] = {'\n', 0};
1064 char *ptr, *savep;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06001065 char source_separator = ';';
1066
1067 if (fstab_ver == 2)
1068 source_separator = ',';
Matt Mower2416a502016-04-12 19:54:46 -05001069
1070 // Semicolons within double-quotes are not forbidden, so replace
1071 // only the semicolons intended as separators with '\n' for strtok
1072 for (unsigned i = 0, skip = 0; i < strlen(flags); i++) {
1073 if (flags[i] == '\"')
Dees_Troya13d74f2013-03-24 08:54:55 -05001074 skip = !skip;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06001075 if (!skip && flags[i] == source_separator)
Matt Mower2416a502016-04-12 19:54:46 -05001076 flags[i] = separator[0];
Dees_Troy51127312012-09-08 13:08:49 -04001077 }
1078
Matt Mower2416a502016-04-12 19:54:46 -05001079 // Avoid issues with potentially nested strtok by using strtok_r
1080 ptr = strtok_r(flags, separator, &savep);
1081 while (ptr) {
1082 int ptr_len = strlen(ptr);
1083 const struct flag_list* tw_flag = tw_flags;
Hashcode62bd9e02013-11-19 21:59:42 -08001084
Matt Mower2416a502016-04-12 19:54:46 -05001085 for (; tw_flag->name; tw_flag++) {
1086 int flag_len = strlen(tw_flag->name);
1087
1088 if (strncmp(ptr, tw_flag->name, flag_len) == 0) {
1089 bool flag_val = false;
1090
1091 if (ptr_len > flag_len && (tw_flag->name)[flag_len-1] != '='
1092 && ptr[flag_len] != '=') {
1093 // Handle flags with same starting string
1094 // (e.g. backup and backupname)
1095 continue;
1096 } else if (ptr_len > flag_len && ptr[flag_len] == '=') {
1097 // Handle flags with dual format: Part 1
1098 // (e.g. backup and backup=y. backup=y handled here)
1099 ptr += flag_len + 1;
1100 TWFunc::Strip_Quotes(ptr);
1101 // Skip flags with empty argument
1102 // (e.g. backup=)
1103 if (strlen(ptr) == 0) {
1104 LOGINFO("Flag missing argument or should not include '=': %s=\n", tw_flag->name);
1105 break;
1106 }
1107 flag_val = strchr("yY1", *ptr) != NULL;
1108 } else if (ptr_len == flag_len
1109 && (tw_flag->name)[flag_len-1] == '=') {
1110 // Skip flags missing argument after =
1111 // (e.g. backupname=)
1112 LOGINFO("Flag missing argument: %s\n", tw_flag->name);
1113 break;
1114 } else if (ptr_len > flag_len
1115 && (tw_flag->name)[flag_len-1] == '=') {
1116 // Handle arguments to flags
1117 // (e.g. backupname="My Stuff")
1118 ptr += flag_len;
1119 TWFunc::Strip_Quotes(ptr);
1120 // Skip flags with empty argument
1121 // (e.g. backupname="")
1122 if (strlen(ptr) == 0) {
1123 LOGINFO("Flag missing argument: %s\n", tw_flag->name);
1124 break;
1125 }
1126 } else if (ptr_len == flag_len) {
1127 // Handle flags with dual format: Part 2
1128 // (e.g. backup and backup=y. backup handled here)
1129 flag_val = true;
Ethan Yonker96af84a2015-01-05 14:58:36 -06001130 } else {
Matt Mower2416a502016-04-12 19:54:46 -05001131 LOGINFO("Flag matched, but could not be processed: %s\n", ptr);
1132 break;
Ethan Yonker96af84a2015-01-05 14:58:36 -06001133 }
Matt Mower2416a502016-04-12 19:54:46 -05001134
1135 Apply_TW_Flag(tw_flag->flag, ptr, flag_val);
1136 break;
Ethan Yonker96af84a2015-01-05 14:58:36 -06001137 }
Matt Mower2416a502016-04-12 19:54:46 -05001138 }
1139 if (tw_flag->name == 0) {
Dees_Troy51127312012-09-08 13:08:49 -04001140 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001141 LOGERR("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -04001142 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001143 LOGINFO("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -04001144 }
Matt Mower2416a502016-04-12 19:54:46 -05001145 ptr = strtok_r(NULL, separator, &savep);
Dees_Troy51127312012-09-08 13:08:49 -04001146 }
Dees_Troy51a0e822012-09-05 15:24:24 -04001147}
1148
Dees_Troy5bf43922012-09-07 16:07:55 -04001149bool TWPartition::Is_File_System(string File_System) {
1150 if (File_System == "ext2" ||
Dees_Troy63c8df72012-09-10 14:02:05 -04001151 File_System == "ext3" ||
Dees_Troy5bf43922012-09-07 16:07:55 -04001152 File_System == "ext4" ||
1153 File_System == "vfat" ||
1154 File_System == "ntfs" ||
1155 File_System == "yaffs2" ||
bigbiff bigbiff3e146522012-11-14 14:32:59 -05001156 File_System == "exfat" ||
Dees_Troye5017042013-08-29 16:38:55 +00001157 File_System == "f2fs" ||
GarfieldHand0b854b2021-11-21 23:46:20 +08001158 File_System == "erofs" ||
Erfan Abdib859e912019-02-05 18:18:46 +02001159 File_System == "squashfs" ||
Dees_Troy5bf43922012-09-07 16:07:55 -04001160 File_System == "auto")
1161 return true;
1162 else
1163 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001164}
1165
Dees_Troy5bf43922012-09-07 16:07:55 -04001166bool TWPartition::Is_Image(string File_System) {
Dees_Troy5fcd8f92012-10-16 12:22:05 -04001167 if (File_System == "emmc" || File_System == "mtd" || File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -04001168 return true;
1169 else
1170 return false;
1171}
1172
Dees_Troy51127312012-09-08 13:08:49 -04001173bool TWPartition::Make_Dir(string Path, bool Display_Error) {
Ethan Yonker93ac7a02016-11-07 22:05:58 -06001174 if (TWFunc::Get_D_Type_From_Stat(Path) != S_IFDIR)
1175 unlink(Path.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001176 if (!TWFunc::Path_Exists(Path)) {
Dees_Troy51127312012-09-08 13:08:49 -04001177 if (mkdir(Path.c_str(), 0777) == -1) {
1178 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001179 gui_msg(Msg(msg::kError, "create_folder_strerr=Can not create '{1}' folder ({2}).")(Path)(strerror(errno)));
Dees_Troy51127312012-09-08 13:08:49 -04001180 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001181 LOGINFO("Can not create '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -04001182 return false;
1183 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001184 LOGINFO("Created '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -04001185 return true;
1186 }
1187 }
1188 return true;
1189}
1190
Dees_Troy5bf43922012-09-07 16:07:55 -04001191void TWPartition::Setup_File_System(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001192 Can_Be_Mounted = true;
1193 Can_Be_Wiped = true;
1194
Dees_Troy5bf43922012-09-07 16:07:55 -04001195 // Make the mount point folder if it doesn't exist
Dees_Troy51127312012-09-08 13:08:49 -04001196 Make_Dir(Mount_Point, Display_Error);
bigbiffce8f83c2015-12-12 18:30:21 -05001197 Backup_Method = BM_FILES;
Dees_Troy5bf43922012-09-07 16:07:55 -04001198}
1199
Ethan Yonker1b190162016-12-05 15:25:19 -06001200void TWPartition::Setup_Image() {
Dees_Troy5bf43922012-09-07 16:07:55 -04001201 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
1202 Backup_Name = Display_Name;
Gary Peck82599a82012-11-21 16:23:12 -08001203 if (Current_File_System == "emmc")
bigbiffce8f83c2015-12-12 18:30:21 -05001204 Backup_Method = BM_DD;
Gary Peck82599a82012-11-21 16:23:12 -08001205 else if (Current_File_System == "mtd" || Current_File_System == "bml")
bigbiffce8f83c2015-12-12 18:30:21 -05001206 Backup_Method = BM_FLASH_UTILS;
Dees_Troy5bf43922012-09-07 16:07:55 -04001207 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001208 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 -04001209}
1210
Dees_Troye58d5262012-09-21 12:27:57 -04001211void TWPartition::Setup_AndSec(void) {
Dees_Troya13d74f2013-03-24 08:54:55 -05001212 Backup_Display_Name = "Android Secure";
Dees_Troye58d5262012-09-21 12:27:57 -04001213 Backup_Name = "and-sec";
Dees_Troya13d74f2013-03-24 08:54:55 -05001214 Can_Be_Backed_Up = true;
Dees_Troye58d5262012-09-21 12:27:57 -04001215 Has_Android_Secure = true;
1216 Symlink_Path = Mount_Point + "/.android_secure";
1217 Symlink_Mount_Point = "/and-sec";
1218 Backup_Path = Symlink_Mount_Point;
1219 Make_Dir("/and-sec", true);
1220 Recreate_AndSec_Folder();
Kjell Braden3126a112016-06-19 16:58:15 +00001221 Mount_Storage_Retry(true);
Dees_Troye58d5262012-09-21 12:27:57 -04001222}
1223
that9e0593e2014-10-08 00:01:24 +02001224void TWPartition::Setup_Data_Media() {
Ethan Yonker6277c792014-09-15 14:54:30 -05001225 LOGINFO("Setting up '%s' as data/media emulated storage.\n", Mount_Point.c_str());
Ethan Yonker66a19492015-12-10 10:19:45 -06001226 if (Storage_Name.empty() || Storage_Name == "Data")
1227 Storage_Name = "Internal Storage";
Ethan Yonker6277c792014-09-15 14:54:30 -05001228 Has_Data_Media = true;
1229 Is_Storage = true;
Ethan Yonker66a19492015-12-10 10:19:45 -06001230 Storage_Path = Mount_Point + "/media";
Ethan Yonker6277c792014-09-15 14:54:30 -05001231 Symlink_Path = Storage_Path;
Ethan Yonker66a19492015-12-10 10:19:45 -06001232 if (Mount_Point == "/data") {
1233 Is_Settings_Storage = true;
1234 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
1235 Make_Dir("/emmc", false);
1236 Symlink_Mount_Point = "/emmc";
1237 } else {
1238 Make_Dir("/sdcard", false);
1239 Symlink_Mount_Point = "/sdcard";
1240 }
Darth98bc9da52023-02-05 23:41:03 +00001241 #ifdef TW_PREPARE_DATA_MEDIA_EARLY
1242 if (Mount(false) && TWFunc::Path_Exists(Mount_Point + "/media/0")) {
1243 #else
bigbiffcda15882021-12-30 17:01:44 -05001244 Mount(false);
1245 if (TWFunc::Path_Exists(Mount_Point + "/media/0")) {
Darth98bc9da52023-02-05 23:41:03 +00001246 #endif
Ethan Yonker66a19492015-12-10 10:19:45 -06001247 Storage_Path = Mount_Point + "/media/0";
1248 Symlink_Path = Storage_Path;
1249 DataManager::SetValue(TW_INTERNAL_PATH, Mount_Point + "/media/0");
1250 UnMount(true);
1251 }
1252 DataManager::SetValue("tw_has_internal", 1);
1253 DataManager::SetValue("tw_has_data_media", 1);
Ethan Yonker3fdcda42016-11-30 12:29:37 -06001254 backup_exclusions.add_absolute_dir("/data/data/com.google.android.music/files");
bigbiff25d25b92020-06-19 16:07:38 -04001255 backup_exclusions.add_absolute_dir("/data/per_boot"); // DJ9,14Jan2020 - exclude this dir to prevent "error 255" on AOSP ROMs that create and lock it
DarthJabba943d70f42020-08-04 10:31:27 +01001256 backup_exclusions.add_absolute_dir("/data/vendor/dumpsys");
bigbiff25d25b92020-06-19 16:07:38 -04001257 backup_exclusions.add_absolute_dir("/data/cache");
nijel880c92d92022-01-14 10:01:32 -05001258 backup_exclusions.add_absolute_dir("/data/misc/apexdata/com.android.art"); // exclude this dir to prevent "error 255" on AOSP Android 12
sekaiacgf4af5f12022-07-06 11:19:45 +08001259 backup_exclusions.add_absolute_dir("/data/extm"); //exclude this dir to prevent "error 255" on MIUI
Ethan Yonker6355b562017-05-01 09:42:17 -05001260 wipe_exclusions.add_absolute_dir(Mount_Point + "/misc/vold"); // adopted storage keys
Ethan Yonker79f88bd2016-12-09 14:52:12 -06001261 ExcludeAll(Mount_Point + "/system/storage.xml");
Darth98c5088d2022-08-09 12:54:40 +01001262
1263 // board-customisable exclusions
1264 #ifdef TW_BACKUP_EXCLUSIONS
1265 std::vector<std::string> user_extra_exclusions = TWFunc::Split_String(TW_BACKUP_EXCLUSIONS, ",");
1266 std::string s1;
1267 for (const std::string& extra_x : user_extra_exclusions) {
1268 s1 = android::base::Trim(extra_x);
1269 if (!s1.empty()) {
1270 backup_exclusions.add_absolute_dir(s1);
1271 LOGINFO("Adding user-defined path '%s' to the backup exclusions\n", s1.c_str());
1272 }
1273 }
1274 #endif
Ethan Yonker6277c792014-09-15 14:54:30 -05001275 } else {
nkk713c1e35f2017-03-24 18:28:03 +02001276 int i;
1277 string path;
1278 for (i = 2; i <= 9; i++) {
1279 path = "/sdcard" + TWFunc::to_string(i);
1280 if (!TWFunc::Path_Exists(path)) {
1281 Make_Dir(path, false);
1282 Symlink_Mount_Point = path;
1283 LOGINFO("'%s' data/media emulated storage symlinked to %s.\n", Mount_Point.c_str(), Symlink_Mount_Point.c_str());
1284 break;
1285 }
1286 }
Ethan Yonker66a19492015-12-10 10:19:45 -06001287 if (Mount(true) && TWFunc::Path_Exists(Mount_Point + "/media/0")) {
1288 Storage_Path = Mount_Point + "/media/0";
1289 Symlink_Path = Storage_Path;
1290 UnMount(true);
1291 }
Ethan Yonker6277c792014-09-15 14:54:30 -05001292 }
Ethan Yonker79f88bd2016-12-09 14:52:12 -06001293 ExcludeAll(Mount_Point + "/media");
Ethan Yonker6277c792014-09-15 14:54:30 -05001294}
1295
Dees_Troy5bf43922012-09-07 16:07:55 -04001296void TWPartition::Find_Real_Block_Device(string& Block, bool Display_Error) {
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06001297 char device[PATH_MAX], realDevice[PATH_MAX];
Dees_Troy5bf43922012-09-07 16:07:55 -04001298
Peter Caid68e6412019-11-02 19:22:38 +08001299 Original_Path = Block;
1300
Dees_Troy5bf43922012-09-07 16:07:55 -04001301 strcpy(device, Block.c_str());
1302 memset(realDevice, 0, sizeof(realDevice));
1303 while (readlink(device, realDevice, sizeof(realDevice)) > 0)
1304 {
1305 strcpy(device, realDevice);
1306 memset(realDevice, 0, sizeof(realDevice));
1307 }
1308
bigbiff998f8392021-08-06 19:19:33 -04001309 Block = device;
1310 return;
Dees_Troy5bf43922012-09-07 16:07:55 -04001311}
1312
Kjell Braden3126a112016-06-19 16:58:15 +00001313bool TWPartition::Mount_Storage_Retry(bool Display_Error) {
Dees_Troy8e337f32012-10-13 22:07:49 -04001314 // On some devices, storage doesn't want to mount right away, retry and sleep
Kjell Braden3126a112016-06-19 16:58:15 +00001315 if (!Mount(Display_Error)) {
Dees_Troy8e337f32012-10-13 22:07:49 -04001316 int retry_count = 5;
1317 while (retry_count > 0 && !Mount(false)) {
1318 usleep(500000);
1319 retry_count--;
1320 }
Kjell Braden3126a112016-06-19 16:58:15 +00001321 return Mount(Display_Error);
Dees_Troy8e337f32012-10-13 22:07:49 -04001322 }
Kjell Braden3126a112016-06-19 16:58:15 +00001323 return true;
Dees_Troy8e337f32012-10-13 22:07:49 -04001324}
1325
Dees_Troy38bd7602012-09-14 13:33:53 -04001326bool TWPartition::Find_MTD_Block_Device(string MTD_Name) {
1327 FILE *fp = NULL;
1328 char line[255];
1329
1330 fp = fopen("/proc/mtd", "rt");
1331 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001332 LOGERR("Device does not support /proc/mtd\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001333 return false;
1334 }
1335
1336 while (fgets(line, sizeof(line), fp) != NULL)
1337 {
1338 char device[32], label[32];
1339 unsigned long size = 0;
Dees_Troy38bd7602012-09-14 13:33:53 -04001340 int deviceId;
1341
1342 sscanf(line, "%s %lx %*s %*c%s", device, &size, label);
1343
1344 // Skip header and blank lines
1345 if ((strcmp(device, "dev:") == 0) || (strlen(line) < 8))
1346 continue;
1347
1348 // Strip off the trailing " from the label
1349 label[strlen(label)-1] = '\0';
1350
1351 if (strcmp(label, MTD_Name.c_str()) == 0) {
1352 // We found our device
1353 // Strip off the trailing : from the device
1354 device[strlen(device)-1] = '\0';
1355 if (sscanf(device,"mtd%d", &deviceId) == 1) {
1356 sprintf(device, "/dev/block/mtdblock%d", deviceId);
1357 Primary_Block_Device = device;
Dees_Troy76543db2013-06-19 16:24:30 +00001358 fclose(fp);
1359 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -04001360 }
1361 }
1362 }
1363 fclose(fp);
1364
1365 return false;
1366}
1367
Dees_Troy51127312012-09-08 13:08:49 -04001368bool TWPartition::Get_Size_Via_statfs(bool Display_Error) {
1369 struct statfs st;
1370 string Local_Path = Mount_Point + "/.";
1371
1372 if (!Mount(Display_Error))
1373 return false;
1374
1375 if (statfs(Local_Path.c_str(), &st) != 0) {
1376 if (!Removable) {
1377 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001378 LOGERR("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -04001379 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001380 LOGINFO("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -04001381 }
1382 return false;
1383 }
1384 Size = (st.f_blocks * st.f_bsize);
1385 Used = ((st.f_blocks - st.f_bfree) * st.f_bsize);
1386 Free = (st.f_bfree * st.f_bsize);
1387 Backup_Size = Used;
1388 return true;
1389}
1390
1391bool TWPartition::Get_Size_Via_df(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001392 FILE* fp;
1393 char command[255], line[512];
1394 int include_block = 1;
1395 unsigned int min_len;
1396
1397 if (!Mount(Display_Error))
1398 return false;
1399
Dees_Troy38bd7602012-09-14 13:33:53 -04001400 min_len = Actual_Block_Device.size() + 2;
Dees_Troy51127312012-09-08 13:08:49 -04001401 sprintf(command, "df %s > /tmp/dfoutput.txt", Mount_Point.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001402 TWFunc::Exec_Cmd(command);
Dees_Troy51127312012-09-08 13:08:49 -04001403 fp = fopen("/tmp/dfoutput.txt", "rt");
1404 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001405 LOGINFO("Unable to open /tmp/dfoutput.txt.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001406 return false;
Dees_Troy51127312012-09-08 13:08:49 -04001407 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001408
1409 while (fgets(line, sizeof(line), fp) != NULL)
1410 {
1411 unsigned long blocks, used, available;
1412 char device[64];
1413 char tmpString[64];
1414
1415 if (strncmp(line, "Filesystem", 10) == 0)
1416 continue;
1417 if (strlen(line) < min_len) {
1418 include_block = 0;
1419 continue;
1420 }
1421 if (include_block) {
1422 sscanf(line, "%s %lu %lu %lu", device, &blocks, &used, &available);
1423 } else {
1424 // The device block string is so long that the df information is on the next line
1425 int space_count = 0;
Dees_Troye58d5262012-09-21 12:27:57 -04001426 sprintf(tmpString, "/dev/block/%s", Actual_Block_Device.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001427 while (tmpString[space_count] == 32)
1428 space_count++;
1429 sscanf(line + space_count, "%lu %lu %lu", &blocks, &used, &available);
1430 }
1431
1432 // Adjust block size to byte size
1433 Size = blocks * 1024ULL;
1434 Used = used * 1024ULL;
1435 Free = available * 1024ULL;
1436 Backup_Size = Used;
1437 }
1438 fclose(fp);
1439 return true;
1440}
1441
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001442unsigned long long TWPartition::IOCTL_Get_Block_Size() {
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001443 Find_Actual_Block_Device();
Ethan Yonker483e9f42016-01-11 22:21:18 -06001444
1445 return TWFunc::IOCTL_Get_Block_Size(Actual_Block_Device.c_str());
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001446}
1447
Dees_Troy5bf43922012-09-07 16:07:55 -04001448bool TWPartition::Find_Partition_Size(void) {
1449 FILE* fp;
1450 char line[512];
1451 string tmpdevice;
1452
igoriok87e3d932013-01-31 21:03:53 +02001453 fp = fopen("/proc/dumchar_info", "rt");
1454 if (fp != NULL) {
1455 while (fgets(line, sizeof(line), fp) != NULL)
1456 {
1457 char label[32], device[32];
1458 unsigned long size = 0;
1459
thatd43bf2d2014-09-21 23:13:02 +02001460 sscanf(line, "%s %lx %*x %*u %s", label, &size, device);
igoriok87e3d932013-01-31 21:03:53 +02001461
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05001462 // Skip header, annotation and blank lines
igoriok87e3d932013-01-31 21:03:53 +02001463 if ((strncmp(device, "/dev/", 5) != 0) || (strlen(line) < 8))
1464 continue;
1465
1466 tmpdevice = "/dev/";
1467 tmpdevice += label;
1468 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
1469 Size = size;
1470 fclose(fp);
1471 return true;
1472 }
1473 }
1474 }
1475
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001476 unsigned long long ioctl_size = IOCTL_Get_Block_Size();
1477 if (ioctl_size) {
1478 Size = ioctl_size;
1479 return true;
1480 }
1481
Dees_Troy5bf43922012-09-07 16:07:55 -04001482 // In this case, we'll first get the partitions we care about (with labels)
1483 fp = fopen("/proc/partitions", "rt");
1484 if (fp == NULL)
1485 return false;
1486
1487 while (fgets(line, sizeof(line), fp) != NULL)
1488 {
1489 unsigned long major, minor, blocks;
1490 char device[512];
Dees_Troy5bf43922012-09-07 16:07:55 -04001491
Dees_Troy63c8df72012-09-10 14:02:05 -04001492 if (strlen(line) < 7 || line[0] == 'm') continue;
Dees_Troy5bf43922012-09-07 16:07:55 -04001493 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
1494
1495 tmpdevice = "/dev/block/";
1496 tmpdevice += device;
Dees_Troy38bd7602012-09-14 13:33:53 -04001497 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001498 // Adjust block size to byte size
1499 Size = blocks * 1024ULL;
1500 fclose(fp);
1501 return true;
1502 }
1503 }
1504 fclose(fp);
1505 return false;
1506}
1507
Dees_Troy5bf43922012-09-07 16:07:55 -04001508bool TWPartition::Is_Mounted(void) {
1509 if (!Can_Be_Mounted)
1510 return false;
1511
1512 struct stat st1, st2;
1513 string test_path;
1514
1515 // Check to see if the mount point directory exists
1516 test_path = Mount_Point + "/.";
1517 if (stat(test_path.c_str(), &st1) != 0) return false;
1518
1519 // Check to see if the directory above the mount point exists
1520 test_path = Mount_Point + "/../.";
1521 if (stat(test_path.c_str(), &st2) != 0) return false;
1522
1523 // Compare the device IDs -- if they match then we're (probably) using tmpfs instead of an actual device
1524 int ret = (st1.st_dev != st2.st_dev) ? true : false;
Dees_Troy5bf43922012-09-07 16:07:55 -04001525 return ret;
1526}
1527
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05001528bool TWPartition::Is_File_System_Writable(void) {
1529 if (!Is_File_System(Current_File_System) || !Is_Mounted())
1530 return false;
1531
1532 string test_path = Mount_Point + "/.";
1533 return (access(test_path.c_str(), W_OK) == 0);
1534}
1535
Dees_Troy5bf43922012-09-07 16:07:55 -04001536bool TWPartition::Mount(bool Display_Error) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001537 int exfat_mounted = 0;
Martin Dünkelmann6d843782018-12-27 23:40:28 +01001538 unsigned int flags = Mount_Flags;
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001539
Dees_Troy5bf43922012-09-07 16:07:55 -04001540 if (Is_Mounted()) {
1541 return true;
1542 } else if (!Can_Be_Mounted) {
1543 return false;
1544 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001545
1546 Find_Actual_Block_Device();
1547
1548 // Check the current file system before mounting
1549 Check_FS_Type();
bigbiffad58e1b2020-07-06 20:24:34 -04001550 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/system/bin/exfat-fuse")) {
1551 string cmd = "/system/bin/exfat-fuse -o big_writes,max_read=131072,max_write=131072 " + Actual_Block_Device + " " + Mount_Point;
Dees_Troy2673cec2013-04-02 20:22:16 +00001552 LOGINFO("cmd: %s\n", cmd.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001553 string result;
bigbiffad58e1b2020-07-06 20:24:34 -04001554 if (TWFunc::Exec_Cmd(cmd, result, false) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001555 LOGINFO("exfat-fuse failed to mount with result '%s', trying vfat\n", result.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001556 Current_File_System = "vfat";
1557 } else {
1558#ifdef TW_NO_EXFAT_FUSE
1559 UnMount(false);
1560 // We'll let the kernel handle it but using exfat-fuse to detect if the file system is actually exfat
1561 // Some kernels let us mount vfat as exfat which doesn't work out too well
1562#else
1563 exfat_mounted = 1;
1564#endif
1565 }
1566 }
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001567
Captain Throwback47bf7202020-10-06 17:39:39 -04001568 if (Current_File_System == "ntfs" && !TWFunc::Path_Exists("/sys/module/tntfs") && (TWFunc::Path_Exists("/system/bin/ntfs-3g") || TWFunc::Path_Exists("/system/bin/mount.ntfs"))) {
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001569 string cmd;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001570 string Ntfsmount_Binary = "";
1571
bigbiffad58e1b2020-07-06 20:24:34 -04001572 if (TWFunc::Path_Exists("/system/bin/ntfs-3g"))
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001573 Ntfsmount_Binary = "ntfs-3g";
bigbiffad58e1b2020-07-06 20:24:34 -04001574 else if (TWFunc::Path_Exists("/system/bin/mount.ntfs"))
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001575 Ntfsmount_Binary = "mount.ntfs";
1576
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001577 if (Mount_Read_Only)
bigbiffad58e1b2020-07-06 20:24:34 -04001578 cmd = "/system/bin/" + Ntfsmount_Binary + " -o ro " + Actual_Block_Device + " " + Mount_Point;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001579 else
bigbiffad58e1b2020-07-06 20:24:34 -04001580 cmd = "/system/bin/" + Ntfsmount_Binary + " " + Actual_Block_Device + " " + Mount_Point;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001581 LOGINFO("cmd: '%s'\n", cmd.c_str());
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001582
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001583 if (TWFunc::Exec_Cmd(cmd) == 0) {
1584 return true;
1585 } else {
1586 LOGINFO("ntfs-3g failed to mount, trying regular mount method.\n");
1587 }
Captain Throwback47bf7202020-10-06 17:39:39 -04001588 } else {
1589 if (Current_File_System == "ntfs" && TWFunc::Path_Exists("/sys/module/tntfs"))
1590 Current_File_System = "tntfs";
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001591 }
1592
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001593 if (Mount_Read_Only)
1594 flags |= MS_RDONLY;
1595
Dees_Troy22042032012-12-18 21:23:08 +00001596 if (Fstab_File_System == "yaffs2") {
1597 // mount an MTD partition as a YAFFS2 filesystem.
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001598 flags = MS_NOATIME | MS_NODEV | MS_NODIRATIME;
1599 if (Mount_Read_Only)
1600 flags |= MS_RDONLY;
Dees_Troy76543db2013-06-19 16:24:30 +00001601 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags, NULL) < 0) {
1602 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags | MS_RDONLY, NULL) < 0) {
1603 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001604 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy76543db2013-06-19 16:24:30 +00001605 else
1606 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
1607 return false;
1608 } else {
1609 LOGINFO("Mounted '%s' (MTD) as RO\n", Mount_Point.c_str());
1610 return true;
1611 }
1612 } else {
1613 struct stat st;
1614 string test_path = Mount_Point;
1615 if (stat(test_path.c_str(), &st) < 0) {
1616 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001617 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy76543db2013-06-19 16:24:30 +00001618 else
1619 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
1620 return false;
1621 }
1622 mode_t new_mode = st.st_mode | S_IXUSR | S_IXGRP | S_IXOTH;
1623 if (new_mode != st.st_mode) {
1624 LOGINFO("Fixing execute permissions for %s\n", Mount_Point.c_str());
1625 if (chmod(Mount_Point.c_str(), new_mode) < 0) {
1626 if (Display_Error)
1627 LOGERR("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
1628 else
1629 LOGINFO("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
1630 return false;
1631 }
1632 }
Dees_Troy22042032012-12-18 21:23:08 +00001633 return true;
Dees_Troy76543db2013-06-19 16:24:30 +00001634 }
thatc7572eb2015-03-31 18:55:30 +02001635 }
1636
1637 string mount_fs = Current_File_System;
1638 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sys/module/texfat"))
1639 mount_fs = "texfat";
1640
1641 if (!exfat_mounted &&
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001642 mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), mount_fs.c_str(), flags, Mount_Options.c_str()) != 0 &&
1643 mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), mount_fs.c_str(), flags, NULL) != 0) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001644#ifdef TW_NO_EXFAT_FUSE
1645 if (Current_File_System == "exfat") {
Dees_Troy2673cec2013-04-02 20:22:16 +00001646 LOGINFO("Mounting exfat failed, trying vfat...\n");
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001647 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), "vfat", 0, NULL) != 0) {
Dees_Troy85f44ed2013-01-09 18:42:36 +00001648 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001649 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy85f44ed2013-01-09 18:42:36 +00001650 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001651 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001652 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 +00001653 return false;
Dees_Troy85f44ed2013-01-09 18:42:36 +00001654 }
Dees_Troyb05ddee2013-01-28 20:24:50 +00001655 } else {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001656#endif
bigbiff bigbiff26774a02014-03-29 18:22:00 -04001657 if (!Removable && Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001658 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001659 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001660 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
1661 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 +00001662 return false;
1663#ifdef TW_NO_EXFAT_FUSE
Dees_Troy85f44ed2013-01-09 18:42:36 +00001664 }
1665#endif
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001666 }
Ethan Yonker253368a2014-11-25 15:00:52 -06001667
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001668 if (Removable)
1669 Update_Size(Display_Error);
1670
bigbiffcda15882021-12-30 17:01:44 -05001671 if (!Symlink_Mount_Point.empty()) {
bigbiffbe4f46c2021-04-24 11:48:54 -04001672 if (!Bind_Mount(false))
1673 return false;
1674 }
1675 return true;
1676}
1677
1678bool TWPartition::Bind_Mount(bool Display_Error) {
1679 if (TWFunc::Path_Exists(Symlink_Path)) {
1680 if (mount(Symlink_Path.c_str(), Symlink_Mount_Point.c_str(), "", MS_BIND, NULL) < 0) {
1681 return false;
1682 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001683 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001684 return true;
1685}
1686
1687bool TWPartition::UnMount(bool Display_Error) {
1688 if (Is_Mounted()) {
1689 int never_unmount_system;
1690
1691 DataManager::GetValue(TW_DONT_UNMOUNT_SYSTEM, never_unmount_system);
Captain Throwback9d6feb52018-07-27 10:05:24 -04001692 if (never_unmount_system == 1 && Mount_Point == PartitionManager.Get_Android_Root_Path())
Dees_Troy5bf43922012-09-07 16:07:55 -04001693 return true; // Never unmount system if you're not supposed to unmount it
1694
Matt Mowera8e6d832017-02-14 20:20:59 -06001695 if (Is_Storage && MTP_Storage_ID > 0)
Ethan Yonker726a0202014-12-16 20:01:38 -06001696 PartitionManager.Remove_MTP_Storage(MTP_Storage_ID);
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001697
Dees_Troy38bd7602012-09-14 13:33:53 -04001698 if (!Symlink_Mount_Point.empty())
1699 umount(Symlink_Mount_Point.c_str());
1700
Dees_Troyb05ddee2013-01-28 20:24:50 +00001701 umount(Mount_Point.c_str());
1702 if (Is_Mounted()) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001703 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001704 gui_msg(Msg(msg::kError, "fail_unmount=Failed to unmount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy5bf43922012-09-07 16:07:55 -04001705 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001706 LOGINFO("Unable to unmount '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001707 return false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001708 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -04001709 return true;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001710 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001711 } else {
1712 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001713 }
1714}
1715
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05001716bool TWPartition::ReMount(bool Display_Error) {
1717 if (UnMount(Display_Error))
1718 return Mount(Display_Error);
1719 return false;
1720}
1721
1722bool TWPartition::ReMount_RW(bool Display_Error) {
1723 // No need to remount if already mounted rw
1724 if (Is_File_System_Writable())
1725 return true;
1726
1727 bool ro = Mount_Read_Only;
1728 int flags = Mount_Flags;
1729
1730 Mount_Read_Only = false;
1731 Mount_Flags &= ~MS_RDONLY;
1732
1733 bool ret = ReMount(Display_Error);
1734
1735 Mount_Read_Only = ro;
1736 Mount_Flags = flags;
1737
1738 return ret;
1739}
1740
Gary Peck43acadf2012-11-21 21:19:01 -08001741bool TWPartition::Wipe(string New_File_System) {
Ethan Yonker726a0202014-12-16 20:01:38 -06001742 bool wiped = false, update_crypt = false, recreate_media = true;
Dees_Troy16c2b312013-01-15 16:51:18 +00001743 int check;
Dees_Troy16c2b312013-01-15 16:51:18 +00001744
Dees_Troy38bd7602012-09-14 13:33:53 -04001745 if (!Can_Be_Wiped) {
Matt Mower3c366972015-12-25 19:28:31 -06001746 gui_msg(Msg(msg::kError, "cannot_wipe=Partition {1} cannot be wiped.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04001747 return false;
1748 }
1749
Dees_Troyc51f1f92012-09-20 15:32:13 -04001750 if (Mount_Point == "/cache")
Dees_Troy2673cec2013-04-02 20:22:16 +00001751 Log_Offset = 0;
Dees_Troyc51f1f92012-09-20 15:32:13 -04001752
bigbiff4a60bee2021-01-23 14:08:03 -05001753 if (Mount_Point == PartitionManager.Get_Android_Root_Path()) {
1754 if (tw_get_default_metadata(PartitionManager.Get_Android_Root_Path().c_str()) != 0) {
1755 gui_msg(Msg(msg::kWarning, "restore_system_context=Unable to get default context for {1} -- Android may not boot.")(PartitionManager.Get_Android_Root_Path()));
1756 }
1757 }
1758
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001759 if (Has_Data_Media && Current_File_System == New_File_System) {
Dees_Troy16c2b312013-01-15 16:51:18 +00001760 wiped = Wipe_Data_Without_Wiping_Media();
bigbiff25d25b92020-06-19 16:07:38 -04001761 if (Mount_Point == "/data" && TWFunc::get_log_dir() == DATA_LOGS_DIR) {
1762 bool created = PartitionManager.Recreate_Logs_Dir();
1763 if (!created)
1764 LOGERR("Unable to create log directory for TWRP\n");
bigbiff7ba75002020-04-11 20:47:09 -04001765 }
Ethan Yonker5eac2222014-06-11 12:22:55 -05001766 recreate_media = false;
Dees_Troy16c2b312013-01-15 16:51:18 +00001767 } else {
Dees_Troy16c2b312013-01-15 16:51:18 +00001768 DataManager::GetValue(TW_RM_RF_VAR, check);
Hashcodedabfd492013-08-29 22:45:30 -07001769 if (check || Use_Rm_Rf)
Dees_Troy16c2b312013-01-15 16:51:18 +00001770 wiped = Wipe_RMRF();
1771 else if (New_File_System == "ext4")
1772 wiped = Wipe_EXT4();
1773 else if (New_File_System == "ext2" || New_File_System == "ext3")
dianlujitao4879b372018-12-03 18:45:47 +08001774 wiped = Wipe_EXTFS(New_File_System);
Dees_Troy16c2b312013-01-15 16:51:18 +00001775 else if (New_File_System == "exfat")
1776 wiped = Wipe_EXFAT();
Captain Throwback47bf7202020-10-06 17:39:39 -04001777 else if (New_File_System == "ntfs" || Current_File_System == "tntfs")
1778 wiped = Wipe_NTFS();
Dees_Troy16c2b312013-01-15 16:51:18 +00001779 else if (New_File_System == "yaffs2")
1780 wiped = Wipe_MTD();
Dees_Troye5017042013-08-29 16:38:55 +00001781 else if (New_File_System == "f2fs")
1782 wiped = Wipe_F2FS();
Captain Throwback47bf7202020-10-06 17:39:39 -04001783 else if (New_File_System == "vfat")
1784 wiped = Wipe_FAT();
Dees_Troy16c2b312013-01-15 16:51:18 +00001785 else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001786 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 +00001787 return false;
1788 }
1789 update_crypt = wiped;
bigbiff7ba75002020-04-11 20:47:09 -04001790 update_crypt = false;
Gary Pecke8bc5d72012-12-21 06:45:25 -08001791 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001792
Gary Pecke8bc5d72012-12-21 06:45:25 -08001793 if (wiped) {
Mohd Faraze1d72622020-06-15 00:28:32 +05301794 if (Mount_Point == "/cache" && TWFunc::get_log_dir() != DATA_LOGS_DIR)
1795 DataManager::Output_Version();
1796
bigbiff4a60bee2021-01-23 14:08:03 -05001797 if (Mount_Point == PartitionManager.Get_Android_Root_Path()) {
1798 tw_set_default_metadata(PartitionManager.Get_Android_Root_Path().c_str());
1799 }
Dees_Troy16c2b312013-01-15 16:51:18 +00001800 if (update_crypt) {
1801 Setup_File_System(false);
1802 if (Is_Encrypted && !Is_Decrypted) {
1803 // just wiped an encrypted partition back to its unencrypted state
1804 Is_Encrypted = false;
1805 Is_Decrypted = false;
1806 Decrypted_Block_Device = "";
1807 if (Mount_Point == "/data") {
1808 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1809 DataManager::SetValue(TW_IS_DECRYPTED, 0);
1810 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001811 }
1812 }
Ethan Yonker5eac2222014-06-11 12:22:55 -05001813
Captain Throwback63573b52021-10-02 20:47:33 -04001814 if (Is_Storage && Mount(false) && !Is_FBE)
bigbiffbe4f46c2021-04-24 11:48:54 -04001815 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Gary Pecke8bc5d72012-12-21 06:45:25 -08001816 }
Matt Mower209c9632016-01-20 12:08:35 -06001817
Gary Pecke8bc5d72012-12-21 06:45:25 -08001818 return wiped;
Dees_Troy51a0e822012-09-05 15:24:24 -04001819}
1820
Gary Peck43acadf2012-11-21 21:19:01 -08001821bool TWPartition::Wipe() {
Gary Peck82599a82012-11-21 16:23:12 -08001822 if (Is_File_System(Current_File_System))
1823 return Wipe(Current_File_System);
1824 else
1825 return Wipe(Fstab_File_System);
Gary Peck43acadf2012-11-21 21:19:01 -08001826}
1827
Dees_Troye58d5262012-09-21 12:27:57 -04001828bool TWPartition::Wipe_AndSec(void) {
1829 if (!Has_Android_Secure)
1830 return false;
1831
Dees_Troye58d5262012-09-21 12:27:57 -04001832 if (!Mount(true))
1833 return false;
1834
Ethan Yonker74db1572015-10-28 12:44:49 -05001835 gui_msg(Msg("wiping=Wiping {1}")(Backup_Display_Name));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001836 TWFunc::removeDir(Mount_Point + "/.android_secure/", true);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001837 return true;
Dees_Troye58d5262012-09-21 12:27:57 -04001838}
1839
bigbiffaed1bdf2021-08-03 18:21:39 -04001840bool TWPartition::Wipe_Data_Cache(void) {
1841 if (!Mount(true))
1842 return false;
1843 gui_msg(Msg("wiping=Wiping {1}")(Mount_Point + "/cache/"));
1844 TWFunc::removeDir(Mount_Point + "/cache/", true);
1845 return true;
1846}
1847
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001848bool TWPartition::Can_Repair() {
Ethan Yonkera2719152015-05-28 09:44:41 -05001849 if (Mount_Read_Only)
1850 return false;
bigbiffad58e1b2020-07-06 20:24:34 -04001851 if (Current_File_System == "vfat" && TWFunc::Path_Exists("/system/bin/fsck.fat"))
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001852 return true;
bigbiffad58e1b2020-07-06 20:24:34 -04001853 else if ((Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") && TWFunc::Path_Exists("/system/bin/e2fsck"))
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001854 return true;
bigbiffad58e1b2020-07-06 20:24:34 -04001855 else if (Current_File_System == "exfat" && TWFunc::Path_Exists("/system/bin/fsck.exfat"))
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001856 return true;
bigbiffad58e1b2020-07-06 20:24:34 -04001857 else if (Current_File_System == "f2fs" && TWFunc::Path_Exists("/system/bin/fsck.f2fs"))
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001858 return true;
Captain Throwback47bf7202020-10-06 17:39:39 -04001859 else if ((Current_File_System == "ntfs" || Current_File_System == "tntfs") && (TWFunc::Path_Exists("/system/bin/ntfsfix") || TWFunc::Path_Exists("/system/bin/fsck.ntfs")))
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001860 return true;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001861 return false;
1862}
1863
1864bool TWPartition::Repair() {
1865 string command;
1866
1867 if (Current_File_System == "vfat") {
bigbiffad58e1b2020-07-06 20:24:34 -04001868 if (!TWFunc::Path_Exists("/system/bin/fsck.fat")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001869 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.fat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001870 return false;
1871 }
1872 if (!UnMount(true))
1873 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001874 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.fat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001875 Find_Actual_Block_Device();
bigbiffad58e1b2020-07-06 20:24:34 -04001876 command = "/system/bin/fsck.fat -y " + Actual_Block_Device;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001877 LOGINFO("Repair command: %s\n", command.c_str());
1878 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001879 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001880 return true;
1881 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001882 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001883 return false;
1884 }
1885 }
1886 if (Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") {
bigbiffad58e1b2020-07-06 20:24:34 -04001887 if (!TWFunc::Path_Exists("/system/bin/e2fsck")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001888 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("e2fsck"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001889 return false;
1890 }
1891 if (!UnMount(true))
1892 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001893 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("e2fsck"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001894 Find_Actual_Block_Device();
bigbiffad58e1b2020-07-06 20:24:34 -04001895 command = "/system/bin/e2fsck -fp " + Actual_Block_Device;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001896 LOGINFO("Repair command: %s\n", command.c_str());
1897 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001898 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001899 return true;
1900 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001901 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001902 return false;
1903 }
1904 }
1905 if (Current_File_System == "exfat") {
bigbiffad58e1b2020-07-06 20:24:34 -04001906 if (!TWFunc::Path_Exists("/system/bin/fsck.exfat")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001907 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.exfat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001908 return false;
1909 }
1910 if (!UnMount(true))
1911 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001912 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.exfat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001913 Find_Actual_Block_Device();
bigbiffad58e1b2020-07-06 20:24:34 -04001914 command = "/system/bin/fsck.exfat " + Actual_Block_Device;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001915 LOGINFO("Repair command: %s\n", command.c_str());
1916 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001917 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001918 return true;
1919 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001920 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001921 return false;
1922 }
1923 }
1924 if (Current_File_System == "f2fs") {
bigbiffad58e1b2020-07-06 20:24:34 -04001925 if (!TWFunc::Path_Exists("/system/bin/fsck.f2fs")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001926 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.f2fs"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001927 return false;
1928 }
1929 if (!UnMount(true))
1930 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001931 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.f2fs"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001932 Find_Actual_Block_Device();
bigbiffad58e1b2020-07-06 20:24:34 -04001933 command = "/system/bin/fsck.f2fs " + Actual_Block_Device;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001934 LOGINFO("Repair command: %s\n", command.c_str());
1935 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001936 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001937 return true;
1938 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001939 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001940 return false;
1941 }
1942 }
Captain Throwback47bf7202020-10-06 17:39:39 -04001943 if (Current_File_System == "ntfs" || Current_File_System == "tntfs") {
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001944 string Ntfsfix_Binary;
bigbiffad58e1b2020-07-06 20:24:34 -04001945 if (TWFunc::Path_Exists("/system/bin/ntfsfix"))
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001946 Ntfsfix_Binary = "ntfsfix";
bigbiffad58e1b2020-07-06 20:24:34 -04001947 else if (TWFunc::Path_Exists("/system/bin/fsck.ntfs"))
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001948 Ntfsfix_Binary = "fsck.ntfs";
1949 else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001950 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("ntfsfix"));
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001951 return false;
1952 }
1953 if (!UnMount(true))
1954 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001955 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)(Ntfsfix_Binary));
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001956 Find_Actual_Block_Device();
bigbiffad58e1b2020-07-06 20:24:34 -04001957 command = "/system/bin/" + Ntfsfix_Binary + " " + Actual_Block_Device;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001958 LOGINFO("Repair command: %s\n", command.c_str());
1959 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001960 gui_msg("done=Done.");
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001961 return true;
1962 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001963 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001964 return false;
1965 }
1966 }
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001967 return false;
1968}
1969
Ethan Yonkera2719152015-05-28 09:44:41 -05001970bool TWPartition::Can_Resize() {
1971 if (Mount_Read_Only)
1972 return false;
bigbiffad58e1b2020-07-06 20:24:34 -04001973 if ((Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") && TWFunc::Path_Exists("/system/bin/resize2fs"))
Ethan Yonkera2719152015-05-28 09:44:41 -05001974 return true;
1975 return false;
1976}
1977
1978bool TWPartition::Resize() {
1979 string command;
1980
1981 if (Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") {
1982 if (!Can_Repair()) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001983 LOGINFO("Cannot resize %s because %s cannot be repaired before resizing.\n", Display_Name.c_str(), Display_Name.c_str());
1984 gui_msg(Msg(msg::kError, "cannot_resize=Cannot resize {1}.")(Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001985 return false;
1986 }
bigbiffad58e1b2020-07-06 20:24:34 -04001987 if (!TWFunc::Path_Exists("/system/bin/resize2fs")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001988 LOGINFO("resize2fs does not exist! Cannot resize!\n");
1989 gui_msg(Msg(msg::kError, "cannot_resize=Cannot resize {1}.")(Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001990 return false;
1991 }
1992 // Repair will unmount so no need to do it twice
Ethan Yonker74db1572015-10-28 12:44:49 -05001993 gui_msg(Msg("repair_resize=Repairing {1} before resizing.")( Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001994 if (!Repair())
1995 return false;
Ethan Yonker74db1572015-10-28 12:44:49 -05001996 gui_msg(Msg("resizing=Resizing {1} using {2}...")(Display_Name)("resize2fs"));
Ethan Yonkera2719152015-05-28 09:44:41 -05001997 Find_Actual_Block_Device();
bigbiffad58e1b2020-07-06 20:24:34 -04001998 command = "/system/bin/resize2fs " + Actual_Block_Device;
Ethan Yonkera2719152015-05-28 09:44:41 -05001999 if (Length != 0) {
Ethan Yonkerd18a8212015-12-14 10:17:00 -06002000 unsigned long long Actual_Size = IOCTL_Get_Block_Size();
2001 if (Actual_Size == 0)
2002 return false;
Ethan Yonkera2719152015-05-28 09:44:41 -05002003
Ethan Yonkera2719152015-05-28 09:44:41 -05002004 unsigned long long Block_Count;
2005 if (Length < 0) {
2006 // Reduce overall size by this length
2007 Block_Count = (Actual_Size / 1024LLU) - ((unsigned long long)(Length * -1) / 1024LLU);
2008 } else {
2009 // This is the size, not a size reduction
2010 Block_Count = ((unsigned long long)(Length) / 1024LLU);
2011 }
2012 char temp[256];
2013 sprintf(temp, "%llu", Block_Count);
2014 command += " ";
2015 command += temp;
2016 command += "K";
2017 }
2018 LOGINFO("Resize command: %s\n", command.c_str());
2019 if (TWFunc::Exec_Cmd(command) == 0) {
2020 Update_Size(true);
Ethan Yonker74db1572015-10-28 12:44:49 -05002021 gui_msg("done=Done.");
Ethan Yonkera2719152015-05-28 09:44:41 -05002022 return true;
2023 } else {
2024 Update_Size(true);
Ethan Yonker74db1572015-10-28 12:44:49 -05002025 gui_msg(Msg(msg::kError, "unable_resize=Unable to resize {1}.")(Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05002026 return false;
2027 }
2028 }
2029 return false;
2030}
2031
bigbiffce8f83c2015-12-12 18:30:21 -05002032bool TWPartition::Backup(PartitionSettings *part_settings, pid_t *tar_fork_pid) {
2033 if (Backup_Method == BM_FILES)
2034 return Backup_Tar(part_settings, tar_fork_pid);
2035 else if (Backup_Method == BM_DD)
2036 return Backup_Image(part_settings);
2037 else if (Backup_Method == BM_FLASH_UTILS)
2038 return Backup_Dump_Image(part_settings);
Dees_Troy2673cec2013-04-02 20:22:16 +00002039 LOGERR("Unknown backup method for '%s'\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04002040 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04002041}
2042
bigbiffce8f83c2015-12-12 18:30:21 -05002043bool TWPartition::Restore(PartitionSettings *part_settings) {
Matt Mower3c366972015-12-25 19:28:31 -06002044 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, gui_parse_text("{@restoring_hdr}"));
Ethan Yonkere080c1f2016-09-19 13:50:25 -05002045 LOGINFO("Restore filename is: %s/%s\n", part_settings->Backup_Folder.c_str(), Backup_FileName.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08002046
bigbiffce8f83c2015-12-12 18:30:21 -05002047 string Restore_File_System = Get_Restore_File_System(part_settings);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002048
2049 if (Is_File_System(Restore_File_System))
bigbiffce8f83c2015-12-12 18:30:21 -05002050 return Restore_Tar(part_settings);
2051 else if (Is_Image(Restore_File_System))
2052 return Restore_Image(part_settings);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002053
2054 LOGERR("Unknown restore method for '%s'\n", Mount_Point.c_str());
2055 return false;
2056}
2057
bigbiffce8f83c2015-12-12 18:30:21 -05002058string TWPartition::Get_Restore_File_System(PartitionSettings *part_settings) {
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002059 size_t first_period, second_period;
2060 string Restore_File_System;
2061
Gary Peck43acadf2012-11-21 21:19:01 -08002062 // Parse backup filename to extract the file system before wiping
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002063 first_period = Backup_FileName.find(".");
Gary Peck43acadf2012-11-21 21:19:01 -08002064 if (first_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00002065 LOGERR("Unable to find file system (first period).\n");
thatd43bf2d2014-09-21 23:13:02 +02002066 return string();
Gary Peck43acadf2012-11-21 21:19:01 -08002067 }
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002068 Restore_File_System = Backup_FileName.substr(first_period + 1, Backup_FileName.size() - first_period - 1);
Gary Peck43acadf2012-11-21 21:19:01 -08002069 second_period = Restore_File_System.find(".");
2070 if (second_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00002071 LOGERR("Unable to find file system (second period).\n");
thatd43bf2d2014-09-21 23:13:02 +02002072 return string();
Gary Peck43acadf2012-11-21 21:19:01 -08002073 }
2074 Restore_File_System.resize(second_period);
Matt Mowera0cd91d2016-12-30 18:21:42 -06002075 LOGINFO("Restore file system is: '%s'.\n", Restore_File_System.c_str());
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002076 return Restore_File_System;
Dees_Troy51a0e822012-09-05 15:24:24 -04002077}
2078
2079string TWPartition::Backup_Method_By_Name() {
bigbiffce8f83c2015-12-12 18:30:21 -05002080 if (Backup_Method == BM_NONE)
Dees_Troy38bd7602012-09-14 13:33:53 -04002081 return "none";
bigbiffce8f83c2015-12-12 18:30:21 -05002082 else if (Backup_Method == BM_FILES)
Dees_Troy38bd7602012-09-14 13:33:53 -04002083 return "files";
bigbiffce8f83c2015-12-12 18:30:21 -05002084 else if (Backup_Method == BM_DD)
Dees_Troy38bd7602012-09-14 13:33:53 -04002085 return "dd";
bigbiffce8f83c2015-12-12 18:30:21 -05002086 else if (Backup_Method == BM_FLASH_UTILS)
Dees_Troy38bd7602012-09-14 13:33:53 -04002087 return "flash_utils";
2088 else
2089 return "undefined";
2090 return "ERROR!";
Dees_Troy51a0e822012-09-05 15:24:24 -04002091}
2092
2093bool TWPartition::Decrypt(string Password) {
Dees_Troy2673cec2013-04-02 20:22:16 +00002094 LOGINFO("STUB TWPartition::Decrypt, password: '%s'\n", Password.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04002095 // Is this needed?
Dees_Troy51a0e822012-09-05 15:24:24 -04002096 return 1;
2097}
2098
2099bool TWPartition::Wipe_Encryption() {
Dees_Troy38bd7602012-09-14 13:33:53 -04002100 bool Save_Data_Media = Has_Data_Media;
Ethan Yonker93382822018-11-01 15:25:31 -05002101 bool ret = false;
2102 BasePartition* base_partition = make_partition();
2103
2104 if (!base_partition->PreWipeEncryption())
2105 goto exit;
Dees_Troy38bd7602012-09-14 13:33:53 -04002106
dianlujitao4879b372018-12-03 18:45:47 +08002107 Find_Actual_Block_Device();
2108 if (!Is_Present) {
2109 LOGINFO("Block device not present, cannot format %s.\n", Display_Name.c_str());
2110 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
2111 return false;
2112 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002113
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06002114#ifdef TW_INCLUDE_CRYPTO
bigbiff7ba75002020-04-11 20:47:09 -04002115 if (!UnMount(true))
2116 return false;
Ethan Yonker1b190162016-12-05 15:25:19 -06002117 if (Is_Decrypted && !Decrypted_Block_Device.empty()) {
Matt Mower2b18a532015-02-20 16:58:05 -06002118 if (delete_crypto_blk_dev((char*)("userdata")) != 0) {
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06002119 LOGERR("Error deleting crypto block device, continuing anyway.\n");
2120 }
2121 }
2122#endif
dianlujitao4879b372018-12-03 18:45:47 +08002123 Has_Data_Media = false;
Ethan Yonker93382822018-11-01 15:25:31 -05002124 Decrypted_Block_Device = "";
Dees_Troy74fb2e92013-04-15 14:35:47 +00002125 Is_Decrypted = false;
2126 Is_Encrypted = false;
Gary Pecke8bc5d72012-12-21 06:45:25 -08002127 if (Wipe(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002128 Has_Data_Media = Save_Data_Media;
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06002129 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
Ethan Yonker83e82572014-04-04 10:59:28 -05002130#ifndef TW_OEM_BUILD
bigbiff25d25b92020-06-19 16:07:38 -04002131 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 -05002132#endif
bigbiff2d548672021-07-09 20:08:44 -04002133 if (Is_FBE) {
2134 gui_msg(Msg(msg::kWarning, "data_media_fbe_msg=TWRP will not recreate /data/media on an FBE device. Please reboot into your rom to create /data/media."));
Captain Throwback63573b52021-10-02 20:47:33 -04002135 } else {
2136 if (Has_Data_Media && !Symlink_Mount_Point.empty()) {
2137 if (Mount(false))
2138 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
2139 }
bigbiff2d548672021-07-09 20:08:44 -04002140 }
2141
Ethan Yonker93382822018-11-01 15:25:31 -05002142 ret = true;
2143 if (!Key_Directory.empty())
2144 ret = PartitionManager.Wipe_By_Path(Key_Directory);
2145 if (ret)
2146 ret = base_partition->PostWipeEncryption();
2147 goto exit;
Dees_Troy38bd7602012-09-14 13:33:53 -04002148 } else {
2149 Has_Data_Media = Save_Data_Media;
Ethan Yonker74db1572015-10-28 12:44:49 -05002150 gui_err("format_data_err=Unable to format to remove encryption.");
Ethan Yonker726a0202014-12-16 20:01:38 -06002151 if (Has_Data_Media && Mount(false))
2152 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Ethan Yonker93382822018-11-01 15:25:31 -05002153 goto exit;
Dees_Troy38bd7602012-09-14 13:33:53 -04002154 }
Ethan Yonker93382822018-11-01 15:25:31 -05002155exit:
2156 delete base_partition;
2157 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04002158}
2159
2160void TWPartition::Check_FS_Type() {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05002161 const char* type;
2162 blkid_probe pr;
Dees_Troy5bf43922012-09-07 16:07:55 -04002163
Dees_Troy68cab492012-12-12 19:29:35 +00002164 if (Fstab_File_System == "yaffs2" || Fstab_File_System == "mtd" || Fstab_File_System == "bml" || Ignore_Blkid)
2165 return; // Running blkid on some mtd devices causes a massive crash or needs to be skipped
Dees_Troy5bf43922012-09-07 16:07:55 -04002166
Dees_Troy38bd7602012-09-14 13:33:53 -04002167 Find_Actual_Block_Device();
Dees_Troy8170a922012-09-18 15:40:25 -04002168 if (!Is_Present)
2169 return;
Dees_Troy51127312012-09-08 13:08:49 -04002170
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05002171 pr = blkid_new_probe_from_filename(Actual_Block_Device.c_str());
2172 if (blkid_do_fullprobe(pr)) {
2173 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00002174 LOGINFO("Can't probe device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05002175 return;
Dees_Troy5bf43922012-09-07 16:07:55 -04002176 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02002177
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05002178 if (blkid_probe_lookup_value(pr, "TYPE", &type, NULL) < 0) {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05002179 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00002180 LOGINFO("can't find filesystem on device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05002181 return;
2182 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02002183
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05002184 Current_File_System = type;
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02002185 blkid_free_probe(pr);
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06002186 if (fs_flags.size() > 1) {
2187 std::vector<partition_fs_flags_struct>::iterator iter;
2188 std::vector<partition_fs_flags_struct>::iterator found = fs_flags.begin();
2189
2190 for (iter = fs_flags.begin(); iter != fs_flags.end(); iter++) {
2191 if (iter->File_System == Current_File_System) {
2192 found = iter;
2193 break;
2194 }
2195 }
2196 // If we don't find a match, we default the flags to the first set of flags that we received from the fstab
2197 if (Mount_Flags != found->Mount_Flags || Mount_Options != found->Mount_Options) {
2198 Mount_Flags = found->Mount_Flags;
2199 Mount_Options = found->Mount_Options;
2200 LOGINFO("Mount_Flags: %i, Mount_Options: %s\n", Mount_Flags, Mount_Options.c_str());
2201 }
2202 }
Dees_Troy51a0e822012-09-05 15:24:24 -04002203}
2204
dianlujitao4879b372018-12-03 18:45:47 +08002205bool TWPartition::Wipe_EXTFS(string File_System) {
bigbiff7ba75002020-04-11 20:47:09 -04002206 if (!UnMount(true))
2207 return false;
2208
dianlujitao4879b372018-12-03 18:45:47 +08002209#if PLATFORM_SDK_VERSION < 28
bigbiffad58e1b2020-07-06 20:24:34 -04002210 if (!TWFunc::Path_Exists("/system/bin/mke2fs"))
dianlujitao4879b372018-12-03 18:45:47 +08002211#else
bigbiffad58e1b2020-07-06 20:24:34 -04002212 if (!TWFunc::Path_Exists("/system/bin/mke2fs") || !TWFunc::Path_Exists("/system/bin/e2fsdroid"))
dianlujitao4879b372018-12-03 18:45:47 +08002213#endif
Dees_Troy38bd7602012-09-14 13:33:53 -04002214 return Wipe_RMRF();
2215
dianlujitao4879b372018-12-03 18:45:47 +08002216 int ret;
2217 bool NeedPreserveFooter = true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002218
Ethan Yonker25f20c12014-10-14 09:04:43 -05002219 Find_Actual_Block_Device();
2220 if (!Is_Present) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002221 LOGINFO("Block device not present, cannot wipe %s.\n", Display_Name.c_str());
2222 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Ethan Yonker25f20c12014-10-14 09:04:43 -05002223 return false;
2224 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002225
dianlujitao4879b372018-12-03 18:45:47 +08002226 /**
2227 * On decrypted devices, IOCTL_Get_Block_Size calculates size on device mapper,
2228 * so there's no need to preserve footer.
2229 */
2230 if ((Is_Decrypted && !Decrypted_Block_Device.empty()) ||
2231 Crypto_Key_Location != "footer") {
2232 NeedPreserveFooter = false;
2233 }
2234
2235 unsigned long long dev_sz = TWFunc::IOCTL_Get_Block_Size(Actual_Block_Device.c_str());
2236 if (!dev_sz)
2237 return false;
2238
2239 if (NeedPreserveFooter)
2240 Length < 0 ? dev_sz += Length : dev_sz -= CRYPT_FOOTER_OFFSET;
2241
Dees Troy9a4d7402019-03-21 14:17:28 -04002242 char dout[16];
2243 sprintf(dout, "%llu", dev_sz / 4096);
2244
2245 //string size_str =to_string(dev_sz / 4096);
2246 string size_str = dout;
dianlujitao4879b372018-12-03 18:45:47 +08002247 string Command;
2248
2249 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mke2fs"));
2250
2251 // Execute mke2fs to create empty ext4 filesystem
2252 Command = "mke2fs -t " + File_System + " -b 4096 " + Actual_Block_Device + " " + size_str;
2253 LOGINFO("mke2fs command: %s\n", Command.c_str());
2254 ret = TWFunc::Exec_Cmd(Command);
2255 if (ret) {
2256 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
2257 return false;
2258 }
2259
bigbiffad58e1b2020-07-06 20:24:34 -04002260 if (TWFunc::Path_Exists("/system/bin/e2fsdroid")) {
dianlujitao3cf05a52019-05-15 15:33:27 +08002261 const string& File_Contexts_Entry = (Mount_Point == "/system_root" ? "/" : Mount_Point);
2262 char *secontext = NULL;
2263 if (!selinux_handle || selabel_lookup(selinux_handle, &secontext, File_Contexts_Entry.c_str(), S_IFDIR) < 0) {
2264 LOGINFO("Cannot lookup security context for '%s'\n", Mount_Point.c_str());
2265 } else {
2266 // Execute e2fsdroid to initialize selinux context
bigbiff5b911ca2020-07-18 10:46:09 -04002267 if (Mount_Point == "/persist") {
2268 Mount(true);
2269 TWFunc::removeDir("/persist/lost+found", false);
2270 UnMount(true);
2271 }
dianlujitao3cf05a52019-05-15 15:33:27 +08002272 Command = "e2fsdroid -e -S /file_contexts -a " + File_Contexts_Entry + " " + Actual_Block_Device;
2273 LOGINFO("e2fsdroid command: %s\n", Command.c_str());
2274 ret = TWFunc::Exec_Cmd(Command);
2275 if (ret) {
2276 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
2277 return false;
2278 }
dianlujitao4879b372018-12-03 18:45:47 +08002279 }
2280 } else {
2281 LOGINFO("e2fsdroid not present\n");
2282 }
2283
2284 if (NeedPreserveFooter)
2285 Wipe_Crypto_Key();
2286 Current_File_System = File_System;
2287 Recreate_AndSec_Folder();
2288 gui_msg("done=Done.");
2289 return true;
2290}
2291
2292bool TWPartition::Wipe_EXT4() {
2293#ifdef USE_EXT4
Ethan Yonkerf27497f2014-02-09 11:48:33 -06002294 int ret;
dianlujitao4879b372018-12-03 18:45:47 +08002295 bool NeedPreserveFooter = true;
2296
bigbiff7ba75002020-04-11 20:47:09 -04002297 if (!UnMount(true))
2298 return false;
2299
dianlujitao4879b372018-12-03 18:45:47 +08002300 Find_Actual_Block_Device();
2301 if (!Is_Present) {
2302 LOGINFO("Block device not present, cannot wipe %s.\n", Display_Name.c_str());
2303 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
2304 return false;
2305 }
dianlujitao4879b372018-12-03 18:45:47 +08002306
2307 /**
2308 * On decrypted devices, IOCTL_Get_Block_Size calculates size on device mapper,
2309 * so there's no need to preserve footer.
2310 */
2311 if ((Is_Decrypted && !Decrypted_Block_Device.empty()) ||
2312 Crypto_Key_Location != "footer") {
2313 NeedPreserveFooter = false;
2314 }
2315
2316 unsigned long long dev_sz = TWFunc::IOCTL_Get_Block_Size(Actual_Block_Device.c_str());
2317 if (!dev_sz)
2318 return false;
2319
2320 if (NeedPreserveFooter)
2321 Length < 0 ? dev_sz += Length : dev_sz -= CRYPT_FOOTER_OFFSET;
2322
Ethan Yonkerf27497f2014-02-09 11:48:33 -06002323 char *secontext = NULL;
2324
Greg Wallace4b44fef2015-12-29 15:33:24 -05002325 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("make_ext4fs"));
Ethan Yonkerf27497f2014-02-09 11:48:33 -06002326
Dees Troy99c8dbf2014-03-10 16:53:58 +00002327 if (!selinux_handle || selabel_lookup(selinux_handle, &secontext, Mount_Point.c_str(), S_IFDIR) < 0) {
Ethan Yonkerf27497f2014-02-09 11:48:33 -06002328 LOGINFO("Cannot lookup security context for '%s'\n", Mount_Point.c_str());
dianlujitao4879b372018-12-03 18:45:47 +08002329 ret = make_ext4fs(Actual_Block_Device.c_str(), dev_sz, Mount_Point.c_str(), NULL);
Ethan Yonkerf27497f2014-02-09 11:48:33 -06002330 } else {
dianlujitao4879b372018-12-03 18:45:47 +08002331 ret = make_ext4fs(Actual_Block_Device.c_str(), dev_sz, Mount_Point.c_str(), selinux_handle);
Ethan Yonkerf27497f2014-02-09 11:48:33 -06002332 }
2333 if (ret != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002334 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troya95f55c2013-08-17 13:14:43 +00002335 return false;
2336 } else {
dianlujitao4879b372018-12-03 18:45:47 +08002337 if (NeedPreserveFooter)
2338 Wipe_Crypto_Key();
bigbiff bigbiffc49d7062013-10-11 20:28:00 -04002339 string sedir = Mount_Point + "/lost+found";
2340 PartitionManager.Mount_By_Path(sedir.c_str(), true);
2341 rmdir(sedir.c_str());
2342 mkdir(sedir.c_str(), S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP);
Dees_Troya95f55c2013-08-17 13:14:43 +00002343 return true;
2344 }
2345#else
dianlujitao4879b372018-12-03 18:45:47 +08002346 return Wipe_EXTFS("ext4");
Dees_Troya95f55c2013-08-17 13:14:43 +00002347#endif
Dees_Troy51a0e822012-09-05 15:24:24 -04002348}
2349
2350bool TWPartition::Wipe_FAT() {
Vojtech Bocek05534202013-09-11 08:11:56 +02002351 string command;
Dees_Troy38bd7602012-09-14 13:33:53 -04002352
bigbiff7ba75002020-04-11 20:47:09 -04002353 if (!UnMount(true))
2354 return false;
Dees_Troy38bd7602012-09-14 13:33:53 -04002355
bigbiffad58e1b2020-07-06 20:24:34 -04002356 if (TWFunc::Path_Exists("/system/bin/mkfs.fat")) {
Greg Wallace4b44fef2015-12-29 15:33:24 -05002357 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mkfs.fat"));
Dees_Troy38bd7602012-09-14 13:33:53 -04002358 Find_Actual_Block_Device();
Matt Mower18794c82015-11-11 16:22:45 -06002359 command = "mkfs.fat " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02002360 if (TWFunc::Exec_Cmd(command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08002361 Current_File_System = "vfat";
Dees_Troye58d5262012-09-21 12:27:57 -04002362 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05002363 gui_msg("done=Done.");
Dees_Troy38bd7602012-09-14 13:33:53 -04002364 return true;
2365 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002366 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04002367 return false;
2368 }
2369 return true;
2370 }
2371 else
2372 return Wipe_RMRF();
2373
2374 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04002375}
2376
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002377bool TWPartition::Wipe_EXFAT() {
Vojtech Bocek05534202013-09-11 08:11:56 +02002378 string command;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002379
bigbiff7ba75002020-04-11 20:47:09 -04002380 if (!UnMount(true))
2381 return false;
bigbiffad58e1b2020-07-06 20:24:34 -04002382 if (TWFunc::Path_Exists("/system/bin/mkexfatfs")) {
Greg Wallace4b44fef2015-12-29 15:33:24 -05002383 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mkexfatfs"));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002384 Find_Actual_Block_Device();
2385 command = "mkexfatfs " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02002386 if (TWFunc::Exec_Cmd(command) == 0) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002387 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05002388 gui_msg("done=Done.");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002389 return true;
2390 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002391 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002392 return false;
2393 }
2394 return true;
2395 }
2396 return false;
2397}
2398
Dees_Troy38bd7602012-09-14 13:33:53 -04002399bool TWPartition::Wipe_MTD() {
2400 if (!UnMount(true))
2401 return false;
2402
Greg Wallace4b44fef2015-12-29 15:33:24 -05002403 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("MTD"));
Dees_Troy38bd7602012-09-14 13:33:53 -04002404
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02002405 mtd_scan_partitions();
2406 const MtdPartition* mtd = mtd_find_partition_by_name(MTD_Name.c_str());
2407 if (mtd == NULL) {
2408 LOGERR("No mtd partition named '%s'", MTD_Name.c_str());
2409 return false;
2410 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002411
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02002412 MtdWriteContext* ctx = mtd_write_partition(mtd);
2413 if (ctx == NULL) {
2414 LOGERR("Can't write '%s', failed to format.", MTD_Name.c_str());
2415 return false;
2416 }
2417 if (mtd_erase_blocks(ctx, -1) == -1) {
2418 mtd_write_close(ctx);
2419 LOGERR("Failed to format '%s'", MTD_Name.c_str());
2420 return false;
2421 }
2422 if (mtd_write_close(ctx) != 0) {
2423 LOGERR("Failed to close '%s'", MTD_Name.c_str());
2424 return false;
2425 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08002426 Current_File_System = "yaffs2";
Dees_Troye58d5262012-09-21 12:27:57 -04002427 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05002428 gui_msg("done=Done.");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02002429 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002430}
2431
2432bool TWPartition::Wipe_RMRF() {
Dees_Troy38bd7602012-09-14 13:33:53 -04002433 if (!Mount(true))
2434 return false;
Ethan Yonker726a0202014-12-16 20:01:38 -06002435 // This is the only wipe that leaves the partition mounted, so we
2436 // must manually remove the partition from MTP if it is a storage
2437 // partition.
2438 if (Is_Storage)
2439 PartitionManager.Remove_MTP_Storage(MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04002440
Ethan Yonker74db1572015-10-28 12:44:49 -05002441 gui_msg(Msg("remove_all=Removing all files under '{1}'")(Mount_Point));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002442 TWFunc::removeDir(Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04002443 Recreate_AndSec_Folder();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002444 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002445}
2446
Dees_Troye5017042013-08-29 16:38:55 +00002447bool TWPartition::Wipe_F2FS() {
systemaddb475c92021-08-12 20:33:20 +02002448 std::string f2fs_command;
Dees_Troye5017042013-08-29 16:38:55 +00002449
bigbiff7ba75002020-04-11 20:47:09 -04002450 if (!UnMount(true))
2451 return false;
dianlujitao4879b372018-12-03 18:45:47 +08002452
bigbiffad58e1b2020-07-06 20:24:34 -04002453 if (TWFunc::Path_Exists("/system/bin/mkfs.f2fs"))
systemaddb475c92021-08-12 20:33:20 +02002454 f2fs_command = "/system/bin/mkfs.f2fs";
bigbiffad58e1b2020-07-06 20:24:34 -04002455 else if (TWFunc::Path_Exists("/system/bin/make_f2fs"))
systemaddb475c92021-08-12 20:33:20 +02002456 f2fs_command = "/system/bin/make_f2fs -g android";
bigbiff7ba75002020-04-11 20:47:09 -04002457 else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002458 LOGINFO("mkfs.f2fs binary not found, using rm -rf to wipe.\n");
Dees_Troye5017042013-08-29 16:38:55 +00002459 return Wipe_RMRF();
2460 }
bigbiff7ba75002020-04-11 20:47:09 -04002461
2462 bool NeedPreserveFooter = true;
systemaddb475c92021-08-12 20:33:20 +02002463 bool needs_casefold = false;
2464 bool needs_projid = false;
bigbiff7ba75002020-04-11 20:47:09 -04002465
2466 Find_Actual_Block_Device();
2467 if (!Is_Present) {
2468 LOGINFO("Block device not present, cannot wipe %s.\n", Display_Name.c_str());
2469 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
2470 return false;
2471 }
systemaddb475c92021-08-12 20:33:20 +02002472
2473 needs_casefold = android::base::GetBoolProperty("external_storage.casefold.enabled", false);
Captain Throwbackd5e2c522022-05-21 10:44:13 -04002474 needs_projid = android::base::GetBoolProperty("external_storage.projid.enabled", false);
bigbiff7ba75002020-04-11 20:47:09 -04002475 unsigned long long dev_sz = TWFunc::IOCTL_Get_Block_Size(Actual_Block_Device.c_str());
2476 if (!dev_sz)
2477 return false;
2478
2479 if (NeedPreserveFooter)
2480 Length < 0 ? dev_sz += Length : dev_sz -= CRYPT_FOOTER_OFFSET;
bigbiff7ba75002020-04-11 20:47:09 -04002481 char dev_sz_str[48];
2482 sprintf(dev_sz_str, "%llu", (dev_sz / 4096));
systemaddb475c92021-08-12 20:33:20 +02002483 if(needs_projid)
2484 f2fs_command += " -O project_quota,extra_attr";
2485
2486 if(needs_casefold)
2487 f2fs_command += " -O casefold -C utf8";
2488
2489 f2fs_command += " " + Actual_Block_Device + " " + dev_sz_str;
2490
Captain Throwbackd5e2c522022-05-21 10:44:13 -04002491 if (TWFunc::Path_Exists("/system/bin/sload_f2fs")) {
2492 f2fs_command += " && sload_f2fs -t /data " + Actual_Block_Device;
bigbiff7ba75002020-04-11 20:47:09 -04002493 }
2494
2495 /**
2496 * On decrypted devices, IOCTL_Get_Block_Size calculates size on device mapper,
2497 * so there's no need to preserve footer.
2498 */
2499 if ((Is_Decrypted && !Decrypted_Block_Device.empty()) ||
2500 Crypto_Key_Location != "footer") {
2501 NeedPreserveFooter = false;
2502 }
systemaddb475c92021-08-12 20:33:20 +02002503 LOGINFO("mkfs.f2fs command: %s\n", f2fs_command.c_str());
2504 if (TWFunc::Exec_Cmd(f2fs_command) == 0) {
bigbiff7ba75002020-04-11 20:47:09 -04002505 if (NeedPreserveFooter)
2506 Wipe_Crypto_Key();
2507 Recreate_AndSec_Folder();
2508 gui_msg("done=Done.");
2509 return true;
2510 } else {
2511 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
2512 return false;
2513 }
2514 return true;
Dees_Troye5017042013-08-29 16:38:55 +00002515}
2516
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002517bool TWPartition::Wipe_NTFS() {
2518 string command;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002519 string Ntfsmake_Binary;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002520
bigbiff7ba75002020-04-11 20:47:09 -04002521 if (!UnMount(true))
2522 return false;
2523
bigbiffad58e1b2020-07-06 20:24:34 -04002524 if (TWFunc::Path_Exists("/system/bin/mkntfs"))
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002525 Ntfsmake_Binary = "mkntfs";
bigbiffad58e1b2020-07-06 20:24:34 -04002526 else if (TWFunc::Path_Exists("/system/bin/mkfs.ntfs"))
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002527 Ntfsmake_Binary = "mkfs.ntfs";
2528 else
2529 return false;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002530
Greg Wallace4b44fef2015-12-29 15:33:24 -05002531 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)(Ntfsmake_Binary));
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002532 Find_Actual_Block_Device();
bigbiffad58e1b2020-07-06 20:24:34 -04002533 command = "/system/bin/" + Ntfsmake_Binary + " " + Actual_Block_Device;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002534 if (TWFunc::Exec_Cmd(command) == 0) {
2535 Recreate_AndSec_Folder();
2536 gui_msg("done=Done.");
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002537 return true;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002538 } else {
2539 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
2540 return false;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002541 }
2542 return false;
2543}
2544
Dees_Troy51a0e822012-09-05 15:24:24 -04002545bool TWPartition::Wipe_Data_Without_Wiping_Media() {
Ethan Yonker83e82572014-04-04 10:59:28 -05002546#ifdef TW_OEM_BUILD
2547 // In an OEM Build we want to do a full format
2548 return Wipe_Encryption();
2549#else
Ethan Yonker66a19492015-12-10 10:19:45 -06002550 bool ret = false;
Dees_Troy38bd7602012-09-14 13:33:53 -04002551
Dees_Troy38bd7602012-09-14 13:33:53 -04002552 if (!Mount(true))
2553 return false;
2554
Ethan Yonker74db1572015-10-28 12:44:49 -05002555 gui_msg("wiping_data=Wiping data without wiping /data/media ...");
Ethan Yonker66a19492015-12-10 10:19:45 -06002556 ret = Wipe_Data_Without_Wiping_Media_Func(Mount_Point + "/");
2557 if (ret)
2558 gui_msg("done=Done.");
2559 return ret;
2560#endif // ifdef TW_OEM_BUILD
2561}
2562
2563bool TWPartition::Wipe_Data_Without_Wiping_Media_Func(const string& parent __unused) {
2564 string dir;
Dees_Troy38bd7602012-09-14 13:33:53 -04002565
2566 DIR* d;
Ethan Yonker66a19492015-12-10 10:19:45 -06002567 d = opendir(parent.c_str());
Dees_Troy16b74352012-11-14 22:27:31 +00002568 if (d != NULL) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002569 struct dirent* de;
2570 while ((de = readdir(d)) != NULL) {
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002571 if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) continue;
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05002572
Ethan Yonker66a19492015-12-10 10:19:45 -06002573 dir = parent;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002574 dir.append(de->d_name);
Ethan Yonker3fdcda42016-11-30 12:29:37 -06002575 if (wipe_exclusions.check_skip_dirs(dir)) {
Ethan Yonker66a19492015-12-10 10:19:45 -06002576 LOGINFO("skipped '%s'\n", dir.c_str());
2577 continue;
2578 }
Dees_Troyce675462013-01-09 19:48:21 +00002579 if (de->d_type == DT_DIR) {
Ethan Yonker66a19492015-12-10 10:19:45 -06002580 dir.append("/");
2581 if (!Wipe_Data_Without_Wiping_Media_Func(dir)) {
2582 closedir(d);
2583 return false;
2584 }
2585 rmdir(dir.c_str());
bigbiff bigbiff98f1f902013-01-19 18:46:13 -05002586 } 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 +03002587 if (unlink(dir.c_str()) != 0)
Ethan Yonker79f88bd2016-12-09 14:52:12 -06002588 LOGINFO("Unable to unlink '%s': %s\n", dir.c_str(), strerror(errno));
Dees_Troyce675462013-01-09 19:48:21 +00002589 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002590 }
2591 closedir(d);
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05002592
Dees_Troy16b74352012-11-14 22:27:31 +00002593 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -04002594 }
Ethan Yonker74db1572015-10-28 12:44:49 -05002595 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy16b74352012-11-14 22:27:31 +00002596 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04002597}
2598
dianlujitao4879b372018-12-03 18:45:47 +08002599void TWPartition::Wipe_Crypto_Key() {
2600 Find_Actual_Block_Device();
2601 if (Crypto_Key_Location.empty())
2602 return;
2603 else if (Crypto_Key_Location == "footer") {
2604 int fd = open(Actual_Block_Device.c_str(), O_RDWR);
2605 if (fd < 0) {
2606 gui_print_color("warning", "Unable to open '%s' to wipe crypto key\n", Actual_Block_Device.c_str());
2607 return;
2608 }
2609
2610 unsigned int block_count;
2611 if ((ioctl(fd, BLKGETSIZE, &block_count)) == -1) {
2612 gui_print_color("warning", "Unable to get block size for wiping crypto footer.\n");
2613 } else {
2614 int newlen = Length < 0 ? -Length : CRYPT_FOOTER_OFFSET;
2615 off64_t offset = ((off64_t)block_count * 512) - newlen;
2616 if (lseek64(fd, offset, SEEK_SET) == -1) {
2617 gui_print_color("warning", "Unable to lseek64 for wiping crypto footer.\n");
2618 } else {
2619 void* buffer = malloc(newlen);
2620 if (!buffer) {
2621 gui_print_color("warning", "Failed to malloc for wiping crypto footer.\n");
2622 } else {
2623 memset(buffer, 0, newlen);
2624 int ret = write(fd, buffer, newlen);
2625 if (ret != newlen) {
2626 gui_print_color("warning", "Failed to wipe crypto footer.\n");
2627 } else {
2628 LOGINFO("Successfully wiped crypto footer.\n");
2629 }
2630 free(buffer);
2631 }
2632 }
2633 }
2634 close(fd);
2635 } else {
2636 if (TWFunc::IOCTL_Get_Block_Size(Crypto_Key_Location.c_str()) >= 16384LLU) {
2637 string Command = "dd of='" + Crypto_Key_Location + "' if=/dev/zero bs=16384 count=1";
2638 TWFunc::Exec_Cmd(Command);
2639 } else {
2640 LOGINFO("Crypto key location reports size < 16K so not wiping crypto footer.\n");
2641 }
2642 }
2643}
2644
bigbiffce8f83c2015-12-12 18:30:21 -05002645bool TWPartition::Backup_Tar(PartitionSettings *part_settings, pid_t *tar_fork_pid) {
Ethan Yonker472f5062016-02-25 13:47:30 -06002646 string Full_FileName;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002647 twrpTar tar;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05002648
Dees_Troy43d8b002012-09-17 16:00:01 -04002649 if (!Mount(true))
2650 return false;
2651
Ian Macdonalda3b25a32019-06-16 22:39:14 +02002652 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Backup_Display_Name, gui_parse_text("{@backing}"));
Ethan Yonker74db1572015-10-28 12:44:49 -05002653 gui_msg(Msg("backing_up=Backing up {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002654
Ethan Yonker472f5062016-02-25 13:47:30 -06002655 DataManager::GetValue(TW_USE_COMPRESSION_VAR, tar.use_compression);
Matt Mowerbb81e5d2014-03-20 18:05:41 -05002656
Dees_Troy83bd4832013-05-04 12:39:56 +00002657#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
Ethan Yonker472f5062016-02-25 13:47:30 -06002658 if (Can_Encrypt_Backup) {
2659 DataManager::GetValue("tw_encrypt_backup", tar.use_encryption);
2660 if (tar.use_encryption) {
2661 if (Use_Userdata_Encryption)
2662 tar.userdata_encryption = tar.use_encryption;
2663 string Password;
2664 DataManager::GetValue("tw_backup_password", Password);
2665 tar.setpassword(Password);
2666 } else {
2667 tar.use_encryption = 0;
2668 }
Dees_Troy83bd4832013-05-04 12:39:56 +00002669 }
2670#endif
Dees_Troy43d8b002012-09-17 16:00:01 -04002671
Ethan Yonker472f5062016-02-25 13:47:30 -06002672 Backup_FileName = Backup_Name + "." + Current_File_System + ".win";
Ethan Yonkere080c1f2016-09-19 13:50:25 -05002673 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
Ethan Yonker01f4e032017-02-03 15:30:52 -06002674 if (Has_Data_Media)
2675 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));
Noah Jacobson81d638d2019-04-28 00:10:07 -04002676 if (Mount_Point == "/data" && DataManager::GetIntValue(TW_IS_FBE)) {
2677 std::vector<users_struct>::iterator iter;
2678 std::vector<users_struct>* userList = PartitionManager.Get_Users_List();
2679 for (iter = userList->begin(); iter != userList->end(); iter++) {
2680 if (!(*iter).isDecrypted && (*iter).userId != "0") {
2681 gui_msg(Msg(msg::kWarning,
2682 "backup_storage_undecrypt_warning=Backup will not include some files from user {1} "
2683 "because the user is not decrypted.")((*iter).userId));
2684 backup_exclusions.add_absolute_dir("/data/system_ce/" + (*iter).userId);
2685 backup_exclusions.add_absolute_dir("/data/misc_ce/" + (*iter).userId);
2686 backup_exclusions.add_absolute_dir("/data/vendor_ce/" + (*iter).userId);
2687 backup_exclusions.add_absolute_dir("/data/media/" + (*iter).userId);
2688 backup_exclusions.add_absolute_dir("/data/user/" + (*iter).userId);
2689 }
2690 }
2691 }
bigbiffce8f83c2015-12-12 18:30:21 -05002692 tar.part_settings = part_settings;
Ethan Yonker3fdcda42016-11-30 12:29:37 -06002693 tar.backup_exclusions = &backup_exclusions;
Dees Troye0a433a2013-12-02 04:10:37 +00002694 tar.setdir(Backup_Path);
2695 tar.setfn(Full_FileName);
2696 tar.setsize(Backup_Size);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002697 tar.partition_name = Backup_Name;
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002698 tar.backup_folder = part_settings->Backup_Folder;
bigbiffce8f83c2015-12-12 18:30:21 -05002699 if (tar.createTarFork(tar_fork_pid) != 0)
Dees Troye0a433a2013-12-02 04:10:37 +00002700 return false;
Dees_Troy43d8b002012-09-17 16:00:01 -04002701 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002702}
2703
bigbiffce8f83c2015-12-12 18:30:21 -05002704bool TWPartition::Backup_Image(PartitionSettings *part_settings) {
2705 string Full_FileName, adb_file_name;
igoriok87e3d932013-01-31 21:03:53 +02002706
Ethan Yonker74db1572015-10-28 12:44:49 -05002707 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, gui_parse_text("{@backing}"));
2708 gui_msg(Msg("backing_up=Backing up {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002709
Ethan Yonker472f5062016-02-25 13:47:30 -06002710 Backup_FileName = Backup_Name + "." + Current_File_System + ".win";
Dees_Troy43d8b002012-09-17 16:00:01 -04002711
bigbiffce8f83c2015-12-12 18:30:21 -05002712 if (part_settings->adbbackup) {
2713 Full_FileName = TW_ADB_BACKUP;
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002714 adb_file_name = part_settings->Backup_Folder + "/" + Backup_FileName;
bigbiffce8f83c2015-12-12 18:30:21 -05002715 }
2716 else
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002717 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
bigbiffce8f83c2015-12-12 18:30:21 -05002718
2719 part_settings->total_restore_size = Backup_Size;
2720
2721 if (part_settings->adbbackup) {
2722 if (!twadbbu::Write_TWIMG(adb_file_name, Backup_Size))
2723 return false;
2724 }
2725
2726 if (!Raw_Read_Write(part_settings))
Ethan Yonker472f5062016-02-25 13:47:30 -06002727 return false;
2728
bigbiffce8f83c2015-12-12 18:30:21 -05002729 if (part_settings->adbbackup) {
2730 if (!twadbbu::Write_TWEOF())
2731 return false;
Dees_Troy7c2dec82012-09-26 09:49:14 -04002732 }
Dees_Troy43d8b002012-09-17 16:00:01 -04002733 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002734}
2735
bigbiffce8f83c2015-12-12 18:30:21 -05002736bool TWPartition::Raw_Read_Write(PartitionSettings *part_settings) {
2737 unsigned long long RW_Block_Size, Remain = Backup_Size;
2738 int src_fd = -1, dest_fd = -1;
2739 ssize_t bs;
Ethan Yonker472f5062016-02-25 13:47:30 -06002740 bool ret = false;
2741 void* buffer = NULL;
2742 unsigned long long backedup_size = 0;
bigbiffce8f83c2015-12-12 18:30:21 -05002743 string srcfn, destfn;
Ethan Yonker472f5062016-02-25 13:47:30 -06002744
bigbiffce8f83c2015-12-12 18:30:21 -05002745 if (part_settings->PM_Method == PM_BACKUP) {
2746 srcfn = Actual_Block_Device;
2747 if (part_settings->adbbackup)
2748 destfn = TW_ADB_BACKUP;
bigbiff bigbiff38b83c12017-12-28 19:58:52 -05002749 else {
Ethan Yonkere080c1f2016-09-19 13:50:25 -05002750 destfn = part_settings->Backup_Folder + "/" + Backup_FileName;
bigbiff bigbiff38b83c12017-12-28 19:58:52 -05002751 }
bigbiffce8f83c2015-12-12 18:30:21 -05002752 }
2753 else {
2754 destfn = Actual_Block_Device;
2755 if (part_settings->adbbackup) {
2756 srcfn = TW_ADB_RESTORE;
2757 } else {
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002758 srcfn = part_settings->Backup_Folder + "/" + Backup_FileName;
bigbiffce8f83c2015-12-12 18:30:21 -05002759 Remain = TWFunc::Get_File_Size(srcfn);
2760 }
2761 }
Ethan Yonker472f5062016-02-25 13:47:30 -06002762
bigbiffce8f83c2015-12-12 18:30:21 -05002763 src_fd = open(srcfn.c_str(), O_RDONLY | O_LARGEFILE);
Ethan Yonker472f5062016-02-25 13:47:30 -06002764 if (src_fd < 0) {
bigbiffce8f83c2015-12-12 18:30:21 -05002765 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(srcfn.c_str())(strerror(errno)));
Ethan Yonker472f5062016-02-25 13:47:30 -06002766 return false;
2767 }
bigbiffce8f83c2015-12-12 18:30:21 -05002768
2769 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 -06002770 if (dest_fd < 0) {
bigbiffce8f83c2015-12-12 18:30:21 -05002771 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(destfn.c_str())(strerror(errno)));
Ethan Yonker472f5062016-02-25 13:47:30 -06002772 goto exit;
2773 }
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -04002774
bigbiffce8f83c2015-12-12 18:30:21 -05002775 LOGINFO("Reading '%s', writing '%s'\n", srcfn.c_str(), destfn.c_str());
2776
2777 if (part_settings->adbbackup) {
2778 RW_Block_Size = MAX_ADB_READ;
2779 bs = MAX_ADB_READ;
2780 }
2781 else {
2782 RW_Block_Size = 1048576LLU; // 1MB
2783 bs = (ssize_t)(RW_Block_Size);
2784 }
2785
Ethan Yonker472f5062016-02-25 13:47:30 -06002786 buffer = malloc((size_t)bs);
2787 if (!buffer) {
2788 LOGINFO("Raw_Read_Write failed to malloc\n");
2789 goto exit;
2790 }
bigbiffce8f83c2015-12-12 18:30:21 -05002791
2792 if (part_settings->progress)
2793 part_settings->progress->SetPartitionSize(part_settings->total_restore_size);
2794
Ethan Yonker472f5062016-02-25 13:47:30 -06002795 while (Remain > 0) {
2796 if (Remain < RW_Block_Size)
bigbiffce8f83c2015-12-12 18:30:21 -05002797 bs = (ssize_t)(Remain);
Matt Mowera8a89d12016-12-30 18:10:37 -06002798 if (read(src_fd, buffer, bs) != bs) {
Ethan Yonker472f5062016-02-25 13:47:30 -06002799 LOGINFO("Error reading source fd (%s)\n", strerror(errno));
2800 goto exit;
2801 }
2802 if (write(dest_fd, buffer, bs) != bs) {
2803 LOGINFO("Error writing destination fd (%s)\n", strerror(errno));
2804 goto exit;
2805 }
2806 backedup_size += (unsigned long long)(bs);
Matt Mower029a82d2017-01-08 13:12:38 -06002807 Remain -= (unsigned long long)(bs);
bigbiffce8f83c2015-12-12 18:30:21 -05002808 if (part_settings->progress)
2809 part_settings->progress->UpdateSize(backedup_size);
Ethan Yonker472f5062016-02-25 13:47:30 -06002810 if (PartitionManager.Check_Backup_Cancel() != 0)
2811 goto exit;
2812 }
bigbiffce8f83c2015-12-12 18:30:21 -05002813 if (part_settings->progress)
2814 part_settings->progress->UpdateDisplayDetails(true);
Ethan Yonker472f5062016-02-25 13:47:30 -06002815 fsync(dest_fd);
nailyka083dc62016-11-11 21:41:28 +01002816
2817 if (!part_settings->adbbackup && part_settings->PM_Method == PM_BACKUP) {
2818 tw_set_default_metadata(destfn.c_str());
2819 LOGINFO("Restored default metadata for %s\n", destfn.c_str());
2820 }
2821
Ethan Yonker472f5062016-02-25 13:47:30 -06002822 ret = true;
2823exit:
2824 if (src_fd >= 0)
2825 close(src_fd);
2826 if (dest_fd >= 0)
2827 close(dest_fd);
2828 if (buffer)
2829 free(buffer);
2830 return ret;
2831}
2832
bigbiffce8f83c2015-12-12 18:30:21 -05002833bool TWPartition::Backup_Dump_Image(PartitionSettings *part_settings) {
Vojtech Bocek05534202013-09-11 08:11:56 +02002834 string Full_FileName, Command;
Dees_Troy43d8b002012-09-17 16:00:01 -04002835
Ethan Yonker74db1572015-10-28 12:44:49 -05002836 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, gui_parse_text("{@backing}"));
2837 gui_msg(Msg("backing_up=Backing up {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002838
bigbiffce8f83c2015-12-12 18:30:21 -05002839 if (part_settings->progress)
2840 part_settings->progress->SetPartitionSize(Backup_Size);
Dees_Troy43d8b002012-09-17 16:00:01 -04002841
Ethan Yonker472f5062016-02-25 13:47:30 -06002842 Backup_FileName = Backup_Name + "." + Current_File_System + ".win";
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002843 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04002844
2845 Command = "dump_image " + MTD_Name + " '" + Full_FileName + "'";
bigbiffce8f83c2015-12-12 18:30:21 -05002846
Dees_Troy2673cec2013-04-02 20:22:16 +00002847 LOGINFO("Backup command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02002848 TWFunc::Exec_Cmd(Command);
Ethan Yonker4b94cfd2014-12-11 10:00:45 -06002849 tw_set_default_metadata(Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04002850 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
2851 // 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 -06002852 gui_msg(Msg(msg::kError, "backup_size=Backup file size for '{1}' is 0 bytes.")(Full_FileName));
Dees_Troy7c2dec82012-09-26 09:49:14 -04002853 return false;
2854 }
bigbiffce8f83c2015-12-12 18:30:21 -05002855 if (part_settings->progress)
2856 part_settings->progress->UpdateSize(Backup_Size);
2857
Dees_Troy43d8b002012-09-17 16:00:01 -04002858 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002859}
2860
bigbiffce8f83c2015-12-12 18:30:21 -05002861unsigned long long TWPartition::Get_Restore_Size(PartitionSettings *part_settings) {
2862 if (!part_settings->adbbackup) {
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002863 InfoManager restore_info(part_settings->Backup_Folder + "/" + Backup_Name + ".info");
bigbiffce8f83c2015-12-12 18:30:21 -05002864 if (restore_info.LoadValues() == 0) {
2865 if (restore_info.GetValue("backup_size", Restore_Size) == 0) {
2866 LOGINFO("Read info file, restore size is %llu\n", Restore_Size);
2867 return Restore_Size;
2868 }
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002869 }
2870 }
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002871
Matt Mower029a82d2017-01-08 13:12:38 -06002872 string Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
2873 string Restore_File_System = Get_Restore_File_System(part_settings);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002874
2875 if (Is_Image(Restore_File_System)) {
2876 Restore_Size = TWFunc::Get_File_Size(Full_FileName);
2877 return Restore_Size;
2878 }
2879
2880 twrpTar tar;
2881 tar.setdir(Backup_Path);
2882 tar.setfn(Full_FileName);
bigbiffce8f83c2015-12-12 18:30:21 -05002883 tar.backup_name = Full_FileName;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002884#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
2885 string Password;
2886 DataManager::GetValue("tw_restore_password", Password);
2887 if (!Password.empty())
2888 tar.setpassword(Password);
2889#endif
2890 tar.partition_name = Backup_Name;
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002891 tar.backup_folder = part_settings->Backup_Folder;
bigbiffce8f83c2015-12-12 18:30:21 -05002892 tar.part_settings = part_settings;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002893 Restore_Size = tar.get_size();
2894 return Restore_Size;
2895}
2896
bigbiffce8f83c2015-12-12 18:30:21 -05002897bool TWPartition::Restore_Tar(PartitionSettings *part_settings) {
Ethan Yonker472f5062016-02-25 13:47:30 -06002898 string Full_FileName;
Dees Troy4159aed2014-02-28 17:24:43 +00002899 bool ret = false;
bigbiffce8f83c2015-12-12 18:30:21 -05002900 string Restore_File_System = Get_Restore_File_System(part_settings);
Dees_Troy43d8b002012-09-17 16:00:01 -04002901
Dees_Troye58d5262012-09-21 12:27:57 -04002902 if (Has_Android_Secure) {
Dees_Troye58d5262012-09-21 12:27:57 -04002903 if (!Wipe_AndSec())
2904 return false;
Gary Peck43acadf2012-11-21 21:19:01 -08002905 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002906 gui_msg(Msg("wiping=Wiping {1}")(Backup_Display_Name));
Ethan Yonker5eac2222014-06-11 12:22:55 -05002907 if (Has_Data_Media && Mount_Point == "/data" && Restore_File_System != Current_File_System) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002908 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 -05002909 if (!Wipe_Data_Without_Wiping_Media())
2910 return false;
2911 } else {
2912 if (!Wipe(Restore_File_System))
2913 return false;
2914 }
Dees_Troye58d5262012-09-21 12:27:57 -04002915 }
Matt Mower3c366972015-12-25 19:28:31 -06002916 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Backup_Display_Name, gui_parse_text("{@restoring_hdr}"));
Ethan Yonker74db1572015-10-28 12:44:49 -05002917 gui_msg(Msg("restoring=Restoring {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002918
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05002919 // Remount as read/write as needed so we can restore the backup
2920 if (!ReMount_RW(true))
Dees_Troy43d8b002012-09-17 16:00:01 -04002921 return false;
2922
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002923 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
Ethan Yonker87af5632014-02-10 11:56:35 -06002924 twrpTar tar;
bigbiffce8f83c2015-12-12 18:30:21 -05002925 tar.part_settings = part_settings;
Ethan Yonker87af5632014-02-10 11:56:35 -06002926 tar.setdir(Backup_Path);
2927 tar.setfn(Full_FileName);
2928 tar.backup_name = Backup_Name;
2929#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
2930 string Password;
2931 DataManager::GetValue("tw_restore_password", Password);
2932 if (!Password.empty())
2933 tar.setpassword(Password);
2934#endif
bigbiffce8f83c2015-12-12 18:30:21 -05002935 part_settings->progress->SetPartitionSize(Get_Restore_Size(part_settings));
2936 if (tar.extractTarFork() != 0)
Dees Troy4159aed2014-02-28 17:24:43 +00002937 ret = false;
2938 else
2939 ret = true;
2940#ifdef HAVE_CAPABILITIES
2941 // Restore capabilities to the run-as binary
Captain Throwback9d6feb52018-07-27 10:05:24 -04002942 if (Mount_Point == PartitionManager.Get_Android_Root_Path() && Mount(true) && TWFunc::Path_Exists("/system/bin/run-as")) {
Dees Troy4159aed2014-02-28 17:24:43 +00002943 struct vfs_cap_data cap_data;
2944 uint64_t capabilities = (1 << CAP_SETUID) | (1 << CAP_SETGID);
2945
2946 memset(&cap_data, 0, sizeof(cap_data));
2947 cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE;
2948 cap_data.data[0].permitted = (uint32_t) (capabilities & 0xffffffff);
2949 cap_data.data[0].inheritable = 0;
2950 cap_data.data[1].permitted = (uint32_t) (capabilities >> 32);
2951 cap_data.data[1].inheritable = 0;
2952 if (setxattr("/system/bin/run-as", XATTR_NAME_CAPS, &cap_data, sizeof(cap_data), 0) < 0) {
2953 LOGINFO("Failed to reset capabilities of /system/bin/run-as binary.\n");
2954 } else {
2955 LOGINFO("Reset capabilities of /system/bin/run-as binary successful.\n");
2956 }
2957 }
2958#endif
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05002959 if (Mount_Read_Only || Mount_Flags & MS_RDONLY)
2960 // Remount as read only when restoration is complete
2961 ReMount(true);
2962
Dees Troy4159aed2014-02-28 17:24:43 +00002963 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04002964}
2965
bigbiffce8f83c2015-12-12 18:30:21 -05002966bool TWPartition::Restore_Image(PartitionSettings *part_settings) {
Ethan Yonker96af84a2015-01-05 14:58:36 -06002967 string Full_FileName;
bigbiffce8f83c2015-12-12 18:30:21 -05002968 string Restore_File_System = Get_Restore_File_System(part_settings);
Dees_Troy43d8b002012-09-17 16:00:01 -04002969
Matt Mower3c366972015-12-25 19:28:31 -06002970 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Backup_Display_Name, gui_parse_text("{@restoring_hdr}"));
Ethan Yonker74db1572015-10-28 12:44:49 -05002971 gui_msg(Msg("restoring=Restoring {1}...")(Backup_Display_Name));
bigbiffce8f83c2015-12-12 18:30:21 -05002972
2973 if (part_settings->adbbackup)
2974 Full_FileName = TW_ADB_RESTORE;
2975 else
Ethan Yonkere080c1f2016-09-19 13:50:25 -05002976 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
Gary Peck15e623d2012-11-21 21:07:58 -08002977
Ethan Yonker96af84a2015-01-05 14:58:36 -06002978 if (Restore_File_System == "emmc") {
bigbiffce8f83c2015-12-12 18:30:21 -05002979 if (!part_settings->adbbackup)
2980 part_settings->total_restore_size = (uint64_t)(TWFunc::Get_File_Size(Full_FileName));
2981 if (!Raw_Read_Write(part_settings))
Ethan Yonker96af84a2015-01-05 14:58:36 -06002982 return false;
2983 } else if (Restore_File_System == "mtd" || Restore_File_System == "bml") {
bigbiffce8f83c2015-12-12 18:30:21 -05002984 if (!Flash_Image_FI(Full_FileName, part_settings->progress))
2985 return false;
2986 }
2987
2988 if (part_settings->adbbackup) {
2989 if (!twadbbu::Write_TWEOF())
Ethan Yonker96af84a2015-01-05 14:58:36 -06002990 return false;
Gary Peck15e623d2012-11-21 21:07:58 -08002991 }
Dees_Troy43d8b002012-09-17 16:00:01 -04002992 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002993}
Dees_Troy5bf43922012-09-07 16:07:55 -04002994
2995bool TWPartition::Update_Size(bool Display_Error) {
me-cafebabe5a0a3ae2022-07-28 03:45:22 +08002996 bool ret = false, Was_Already_Mounted = false, ro = false;
Dees_Troy51127312012-09-08 13:08:49 -04002997
Ethan Yonker1b190162016-12-05 15:25:19 -06002998 Find_Actual_Block_Device();
2999
bigbiffee7b7ff2020-03-23 15:08:27 -04003000 if (Actual_Block_Device.empty())
3001 return false;
3002
me-cafebabe5a0a3ae2022-07-28 03:45:22 +08003003 ro = Mount_Read_Only;
3004 Mount_Read_Only = true;
3005
Ethan Yonker1b190162016-12-05 15:25:19 -06003006 if (!Can_Be_Mounted && !Is_Encrypted) {
3007 if (TWFunc::Path_Exists(Actual_Block_Device) && Find_Partition_Size()) {
3008 Used = Size;
3009 Backup_Size = Size;
me-cafebabe5a0a3ae2022-07-28 03:45:22 +08003010 goto success;
Ethan Yonker1b190162016-12-05 15:25:19 -06003011 }
me-cafebabe5a0a3ae2022-07-28 03:45:22 +08003012 goto fail;
Ethan Yonker1b190162016-12-05 15:25:19 -06003013 }
Dees_Troy5bf43922012-09-07 16:07:55 -04003014
Dees_Troy0550cfb2012-10-13 11:56:13 -04003015 Was_Already_Mounted = Is_Mounted();
bigbiffee7b7ff2020-03-23 15:08:27 -04003016
Dees_Troy38bd7602012-09-14 13:33:53 -04003017 if (Removable || Is_Encrypted) {
3018 if (!Mount(false))
me-cafebabe5a0a3ae2022-07-28 03:45:22 +08003019 goto success;
Dees_Troy38bd7602012-09-14 13:33:53 -04003020 } else if (!Mount(Display_Error))
me-cafebabe5a0a3ae2022-07-28 03:45:22 +08003021 goto fail;
Dees_Troy51127312012-09-08 13:08:49 -04003022
3023 ret = Get_Size_Via_statfs(Display_Error);
Dees_Troy0550cfb2012-10-13 11:56:13 -04003024 if (!ret || Size == 0) {
3025 if (!Get_Size_Via_df(Display_Error)) {
3026 if (!Was_Already_Mounted)
3027 UnMount(false);
me-cafebabe5a0a3ae2022-07-28 03:45:22 +08003028 goto fail;
Dees_Troy0550cfb2012-10-13 11:56:13 -04003029 }
3030 }
Dees_Troy51127312012-09-08 13:08:49 -04003031
Dees_Troy5bf43922012-09-07 16:07:55 -04003032 if (Has_Data_Media) {
3033 if (Mount(Display_Error)) {
Ethan Yonker3fdcda42016-11-30 12:29:37 -06003034 Used = backup_exclusions.Get_Folder_Size(Mount_Point);
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05003035 Backup_Size = Used;
3036 int bak = (int)(Used / 1048576LLU);
Dees_Troy51127312012-09-08 13:08:49 -04003037 int fre = (int)(Free / 1048576LLU);
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05003038 LOGINFO("Data backup size is %iMB, free: %iMB.\n", bak, fre);
Dees_Troy0550cfb2012-10-13 11:56:13 -04003039 } else {
3040 if (!Was_Already_Mounted)
3041 UnMount(false);
me-cafebabe5a0a3ae2022-07-28 03:45:22 +08003042 goto fail;
Dees_Troy0550cfb2012-10-13 11:56:13 -04003043 }
Dees_Troye58d5262012-09-21 12:27:57 -04003044 } else if (Has_Android_Secure) {
3045 if (Mount(Display_Error))
Ethan Yonker3fdcda42016-11-30 12:29:37 -06003046 Backup_Size = backup_exclusions.Get_Folder_Size(Backup_Path);
Dees_Troy0550cfb2012-10-13 11:56:13 -04003047 else {
3048 if (!Was_Already_Mounted)
3049 UnMount(false);
me-cafebabe5a0a3ae2022-07-28 03:45:22 +08003050 goto fail;
Dees_Troy0550cfb2012-10-13 11:56:13 -04003051 }
Dees_Troy5bf43922012-09-07 16:07:55 -04003052 }
Dees_Troy0550cfb2012-10-13 11:56:13 -04003053 if (!Was_Already_Mounted)
3054 UnMount(false);
me-cafebabe5a0a3ae2022-07-28 03:45:22 +08003055success:
3056 Mount_Read_Only = ro;
Dees_Troy5bf43922012-09-07 16:07:55 -04003057 return true;
me-cafebabe5a0a3ae2022-07-28 03:45:22 +08003058fail:
3059 Mount_Read_Only = ro;
3060 return false;
Dees_Troy51127312012-09-08 13:08:49 -04003061}
Dees_Troy38bd7602012-09-14 13:33:53 -04003062
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06003063bool TWPartition::Find_Wildcard_Block_Devices(const string& Device) {
3064 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
3065 string Path = TWFunc::Get_Path(Device);
3066 string Dev = TWFunc::Get_Filename(Device);
3067 size_t wildcard_index = Dev.find("*");
3068 if (wildcard_index != string::npos)
3069 Dev = Dev.substr(0, wildcard_index);
3070 wildcard_index = Dev.size();
3071 DIR* d = opendir(Path.c_str());
3072 if (d == NULL) {
3073 LOGINFO("Error opening '%s': %s\n", Path.c_str(), strerror(errno));
3074 return false;
3075 }
3076 struct dirent* de;
3077 while ((de = readdir(d)) != NULL) {
3078 if (de->d_type != DT_BLK || strlen(de->d_name) <= wildcard_index || strncmp(de->d_name, Dev.c_str(), wildcard_index) != 0)
3079 continue;
3080
3081 string item = Path + "/";
3082 item.append(de->d_name);
3083 if (PartitionManager.Find_Partition_By_Block_Device(item))
3084 continue;
3085 TWPartition *part = new TWPartition;
3086 char buffer[MAX_FSTAB_LINE_LENGTH];
3087 sprintf(buffer, "%s %s-%i auto defaults defaults", item.c_str(), Mount_Point.c_str(), ++mount_point_index);
bigbiff8da46fa2020-09-08 16:42:34 -04003088 part->Process_Fstab_Line(buffer, false, NULL);
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06003089 char display[MAX_FSTAB_LINE_LENGTH];
3090 sprintf(display, "%s %i", Storage_Name.c_str(), mount_point_index);
3091 part->Storage_Name = display;
3092 part->Display_Name = display;
3093 part->Primary_Block_Device = item;
3094 part->Wildcard_Block_Device = false;
3095 part->Is_SubPartition = true;
3096 part->SubPartition_Of = Mount_Point;
3097 part->Is_Storage = Is_Storage;
3098 part->Can_Be_Mounted = true;
3099 part->Removable = true;
3100 part->Can_Be_Wiped = Can_Be_Wiped;
3101 part->Wipe_Available_in_GUI = Wipe_Available_in_GUI;
3102 part->Find_Actual_Block_Device();
3103 part->Update_Size(false);
3104 Has_SubPartition = true;
3105 PartitionManager.Output_Partition(part);
3106 PartitionManager.Add_Partition(part);
3107 }
3108 closedir(d);
3109 return (mount_point_index > 0);
3110}
3111
Dees_Troy38bd7602012-09-14 13:33:53 -04003112void TWPartition::Find_Actual_Block_Device(void) {
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06003113 if (!Sysfs_Entry.empty() && Primary_Block_Device.empty() && Decrypted_Block_Device.empty()) {
3114 /* Sysfs_Entry.empty() indicates if this is a sysfs entry that begins with /device/
3115 * If we have a syfs entry then we are looking for this device from a uevent add.
3116 * The uevent add will set the primary block device based on the data we receive from
3117 * after checking for adopted storage. If the device ends up being adopted, then the
3118 * decrypted block device will be set instead of the primary block device. */
3119 Is_Present = false;
3120 return;
3121 }
3122 if (Wildcard_Block_Device && !Is_Adopted_Storage) {
3123 Is_Present = false;
3124 Actual_Block_Device = "";
3125 Can_Be_Mounted = false;
3126 if (!Find_Wildcard_Block_Devices(Primary_Block_Device)) {
3127 string Dev = Primary_Block_Device.substr(0, Primary_Block_Device.find("*"));
3128 if (TWFunc::Path_Exists(Dev)) {
3129 Is_Present = true;
3130 Can_Be_Mounted = true;
3131 Actual_Block_Device = Dev;
3132 }
3133 }
3134 return;
3135 } else if (Is_Decrypted && !Decrypted_Block_Device.empty()) {
Dees_Troy38bd7602012-09-14 13:33:53 -04003136 Actual_Block_Device = Decrypted_Block_Device;
Ethan Yonker1b190162016-12-05 15:25:19 -06003137 if (TWFunc::Path_Exists(Decrypted_Block_Device)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04003138 Is_Present = true;
Ethan Yonker1b190162016-12-05 15:25:19 -06003139 return;
3140 }
3141 } else if (SlotSelect && TWFunc::Path_Exists(Primary_Block_Device + PartitionManager.Get_Active_Slot_Suffix())) {
3142 Actual_Block_Device = Primary_Block_Device + PartitionManager.Get_Active_Slot_Suffix();
3143 unlink(Primary_Block_Device.c_str());
3144 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
3145 Is_Present = true;
3146 return;
Dees_Troy43d8b002012-09-17 16:00:01 -04003147 } else if (TWFunc::Path_Exists(Primary_Block_Device)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04003148 Is_Present = true;
3149 Actual_Block_Device = Primary_Block_Device;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05003150 return;
3151 }
Ethan Yonker1b190162016-12-05 15:25:19 -06003152 if (!Alternate_Block_Device.empty() && TWFunc::Path_Exists(Alternate_Block_Device)) {
Dees_Troy3f04d032012-10-07 18:20:09 -04003153 Actual_Block_Device = Alternate_Block_Device;
Dees_Troy38bd7602012-09-14 13:33:53 -04003154 Is_Present = true;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05003155 } else {
Dees_Troy38bd7602012-09-14 13:33:53 -04003156 Is_Present = false;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05003157 }
Dees_Troy38bd7602012-09-14 13:33:53 -04003158}
3159
3160void TWPartition::Recreate_Media_Folder(void) {
3161 string Command;
Ethan Yonker66a19492015-12-10 10:19:45 -06003162 string Media_Path = Mount_Point + "/media";
Dees_Troy38bd7602012-09-14 13:33:53 -04003163
Ethan Yonkerbd7492d2016-12-07 13:55:01 -06003164 if (Is_FBE) {
3165 LOGINFO("Not recreating media folder on FBE\n");
3166 return;
3167 }
Dees_Troy38bd7602012-09-14 13:33:53 -04003168 if (!Mount(true)) {
Ethan Yonker66a19492015-12-10 10:19:45 -06003169 gui_msg(Msg(msg::kError, "recreate_folder_err=Unable to recreate {1} folder.")(Media_Path));
3170 } else if (!TWFunc::Path_Exists(Media_Path)) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05003171 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Ethan Yonker66a19492015-12-10 10:19:45 -06003172 LOGINFO("Recreating %s folder.\n", Media_Path.c_str());
3173 mkdir(Media_Path.c_str(), 0770);
xiaolu9416f4f2015-06-04 08:22:23 +08003174 string Internal_path = DataManager::GetStrValue("tw_internal_path");
3175 if (!Internal_path.empty()) {
3176 LOGINFO("Recreating %s folder.\n", Internal_path.c_str());
3177 mkdir(Internal_path.c_str(), 0770);
3178 }
3179#ifdef TW_INTERNAL_STORAGE_PATH
3180 mkdir(EXPAND(TW_INTERNAL_STORAGE_PATH), 0770);
3181#endif
Matt Mower87413642017-01-17 21:14:46 -06003182
thata3d31fb2014-12-21 22:27:40 +01003183 // Afterwards, we will try to set the
3184 // default metadata that we were hopefully able to get during
3185 // early boot.
Ethan Yonker66a19492015-12-10 10:19:45 -06003186 tw_set_default_metadata(Media_Path.c_str());
xiaolu9416f4f2015-06-04 08:22:23 +08003187 if (!Internal_path.empty())
3188 tw_set_default_metadata(Internal_path.c_str());
Matt Mower87413642017-01-17 21:14:46 -06003189
Ethan Yonker5eac2222014-06-11 12:22:55 -05003190 // Toggle mount to ensure that "internal sdcard" gets mounted
bigbiff bigbiff9c754052013-01-09 09:09:08 -05003191 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Ethan Yonker5eac2222014-06-11 12:22:55 -05003192 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Dees_Troy38bd7602012-09-14 13:33:53 -04003193 }
Dees_Troy43d8b002012-09-17 16:00:01 -04003194}
Dees_Troye58d5262012-09-21 12:27:57 -04003195
3196void TWPartition::Recreate_AndSec_Folder(void) {
Dees_Troye58d5262012-09-21 12:27:57 -04003197 if (!Has_Android_Secure)
3198 return;
Dees_Troy2673cec2013-04-02 20:22:16 +00003199 LOGINFO("Creating %s: %s\n", Backup_Display_Name.c_str(), Symlink_Path.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -04003200 if (!Mount(true)) {
Ethan Yonker74db1572015-10-28 12:44:49 -05003201 gui_msg(Msg(msg::kError, "recreate_folder_err=Unable to recreate {1} folder.")(Backup_Name));
Dees_Troye58d5262012-09-21 12:27:57 -04003202 } else if (!TWFunc::Path_Exists(Symlink_Path)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00003203 LOGINFO("Recreating %s folder.\n", Backup_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05003204 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05003205 mkdir(Symlink_Path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
bigbiff bigbiff9c754052013-01-09 09:09:08 -05003206 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04003207 }
3208}
bigbiff7cb4c332014-11-26 20:36:07 -05003209
3210uint64_t TWPartition::Get_Max_FileSize() {
3211 uint64_t maxFileSize = 0;
3212 const uint64_t constGB = (uint64_t) 1024 * 1024 * 1024;
3213 const uint64_t constTB = (uint64_t) constGB * 1024;
3214 const uint64_t constPB = (uint64_t) constTB * 1024;
bigbiff0c532032014-12-21 13:41:26 -05003215 if (Current_File_System == "ext4")
3216 maxFileSize = 16 * constTB; //16 TB
3217 else if (Current_File_System == "vfat")
3218 maxFileSize = 4 * constGB; //4 GB
Captain Throwback47bf7202020-10-06 17:39:39 -04003219 else if (Current_File_System == "ntfs" || Current_File_System == "tntfs")
bigbiff0c532032014-12-21 13:41:26 -05003220 maxFileSize = 256 * constTB; //256 TB
3221 else if (Current_File_System == "exfat")
3222 maxFileSize = 16 * constPB; //16 PB
3223 else if (Current_File_System == "ext3")
3224 maxFileSize = 2 * constTB; //2 TB
3225 else if (Current_File_System == "f2fs")
3226 maxFileSize = 3.94 * constTB; //3.94 TB
bigbiff7cb4c332014-11-26 20:36:07 -05003227 else
3228 maxFileSize = 100000000L;
3229 return maxFileSize - 1;
3230}
3231
bigbiffce8f83c2015-12-12 18:30:21 -05003232bool TWPartition::Flash_Image(PartitionSettings *part_settings) {
3233 string Restore_File_System, full_filename;
Ethan Yonker96af84a2015-01-05 14:58:36 -06003234
Ethan Yonkere080c1f2016-09-19 13:50:25 -05003235 full_filename = part_settings->Backup_Folder + "/" + Backup_FileName;
Ethan Yonker96af84a2015-01-05 14:58:36 -06003236
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05003237 LOGINFO("Image filename is: %s\n", Backup_FileName.c_str());
bigbiffce8f83c2015-12-12 18:30:21 -05003238
3239 if (Backup_Method == BM_FILES) {
Ethan Yonker96af84a2015-01-05 14:58:36 -06003240 LOGERR("Cannot flash images to file systems\n");
3241 return false;
3242 } else if (!Can_Flash_Img) {
3243 LOGERR("Cannot flash images to partitions %s\n", Display_Name.c_str());
3244 return false;
3245 } else {
3246 if (!Find_Partition_Size()) {
3247 LOGERR("Unable to find partition size for '%s'\n", Mount_Point.c_str());
3248 return false;
3249 }
bigbiffce8f83c2015-12-12 18:30:21 -05003250 unsigned long long image_size = TWFunc::Get_File_Size(full_filename);
Ethan Yonker96af84a2015-01-05 14:58:36 -06003251 if (image_size > Size) {
Ethan Yonker74db1572015-10-28 12:44:49 -05003252 LOGINFO("Size (%llu bytes) of image '%s' is larger than target device '%s' (%llu bytes)\n",
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05003253 image_size, Backup_FileName.c_str(), Actual_Block_Device.c_str(), Size);
Ethan Yonker74db1572015-10-28 12:44:49 -05003254 gui_err("img_size_err=Size of image is larger than target device");
Ethan Yonker96af84a2015-01-05 14:58:36 -06003255 return false;
3256 }
bigbiffce8f83c2015-12-12 18:30:21 -05003257 if (Backup_Method == BM_DD) {
3258 if (!part_settings->adbbackup) {
3259 if (Is_Sparse_Image(full_filename)) {
3260 return Flash_Sparse_Image(full_filename);
3261 }
Ethan Yonker472f5062016-02-25 13:47:30 -06003262 }
bigbiffce8f83c2015-12-12 18:30:21 -05003263 return Raw_Read_Write(part_settings);
3264 } else if (Backup_Method == BM_FLASH_UTILS) {
3265 return Flash_Image_FI(full_filename, NULL);
Ethan Yonker472f5062016-02-25 13:47:30 -06003266 }
Ethan Yonker96af84a2015-01-05 14:58:36 -06003267 }
3268
3269 LOGERR("Unknown flash method for '%s'\n", Mount_Point.c_str());
3270 return false;
3271}
3272
Ethan Yonker472f5062016-02-25 13:47:30 -06003273bool TWPartition::Is_Sparse_Image(const string& Filename) {
HashBanged974bb2016-01-30 14:20:09 -05003274 uint32_t magic = 0;
3275 int fd = open(Filename.c_str(), O_RDONLY);
3276 if (fd < 0) {
3277 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Filename)(strerror(errno)));
3278 return false;
3279 }
bigbiffce8f83c2015-12-12 18:30:21 -05003280
HashBanged974bb2016-01-30 14:20:09 -05003281 if (read(fd, &magic, sizeof(magic)) != sizeof(magic)) {
3282 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Filename)(strerror(errno)));
3283 close(fd);
3284 return false;
3285 }
3286 close(fd);
Ethan Yonker472f5062016-02-25 13:47:30 -06003287 if (magic == SPARSE_HEADER_MAGIC)
3288 return true;
3289 return false;
3290}
3291
3292bool TWPartition::Flash_Sparse_Image(const string& Filename) {
3293 string Command;
3294
3295 gui_msg(Msg("flashing=Flashing {1}...")(Display_Name));
3296
3297 Command = "simg2img '" + Filename + "' '" + Actual_Block_Device + "'";
Ethan Yonker96af84a2015-01-05 14:58:36 -06003298 LOGINFO("Flash command: '%s'\n", Command.c_str());
3299 TWFunc::Exec_Cmd(Command);
3300 return true;
3301}
3302
Ethan Yonker472f5062016-02-25 13:47:30 -06003303bool TWPartition::Flash_Image_FI(const string& Filename, ProgressTracking *progress) {
Ethan Yonker96af84a2015-01-05 14:58:36 -06003304 string Command;
Ethan Yonker472f5062016-02-25 13:47:30 -06003305 unsigned long long file_size;
Ethan Yonker96af84a2015-01-05 14:58:36 -06003306
Ethan Yonker74db1572015-10-28 12:44:49 -05003307 gui_msg(Msg("flashing=Flashing {1}...")(Display_Name));
Ethan Yonker472f5062016-02-25 13:47:30 -06003308 if (progress) {
3309 file_size = (unsigned long long)(TWFunc::Get_File_Size(Filename));
3310 progress->SetPartitionSize(file_size);
3311 }
Ethan Yonker96af84a2015-01-05 14:58:36 -06003312 // Sometimes flash image doesn't like to flash due to the first 2KB matching, so we erase first to ensure that it flashes
3313 Command = "erase_image " + MTD_Name;
3314 LOGINFO("Erase command: '%s'\n", Command.c_str());
3315 TWFunc::Exec_Cmd(Command);
3316 Command = "flash_image " + MTD_Name + " '" + Filename + "'";
3317 LOGINFO("Flash command: '%s'\n", Command.c_str());
3318 TWFunc::Exec_Cmd(Command);
Ethan Yonker472f5062016-02-25 13:47:30 -06003319 if (progress)
3320 progress->UpdateSize(file_size);
Ethan Yonker96af84a2015-01-05 14:58:36 -06003321 return true;
3322}
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05003323
3324void TWPartition::Change_Mount_Read_Only(bool new_value) {
3325 Mount_Read_Only = new_value;
3326}
3327
bigbiffce8f83c2015-12-12 18:30:21 -05003328bool TWPartition::Is_Read_Only() {
3329 return Mount_Read_Only;
3330}
3331
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05003332int TWPartition::Check_Lifetime_Writes() {
3333 bool original_read_only = Mount_Read_Only;
3334 int ret = 1;
3335
3336 Mount_Read_Only = true;
3337 if (Mount(false)) {
3338 Find_Actual_Block_Device();
Ethan Yonker54970bb2019-01-17 16:19:24 -06003339 string temp = Actual_Block_Device;
3340 Find_Real_Block_Device(temp, false);
3341 string block = basename(temp.c_str());
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05003342 string file = "/sys/fs/" + Current_File_System + "/" + block + "/lifetime_write_kbytes";
3343 string result;
3344 if (TWFunc::Path_Exists(file)) {
3345 if (TWFunc::read_file(file, result) != 0) {
3346 LOGINFO("Check_Lifetime_Writes of '%s' failed to read_file\n", file.c_str());
3347 } else {
3348 LOGINFO("Check_Lifetime_Writes result: '%s'\n", result.c_str());
3349 if (result == "0") {
3350 ret = 0;
3351 }
3352 }
3353 } else {
3354 LOGINFO("Check_Lifetime_Writes file does not exist '%s'\n", file.c_str());
3355 }
3356 UnMount(true);
3357 } else {
3358 LOGINFO("Check_Lifetime_Writes failed to mount '%s'\n", Mount_Point.c_str());
3359 }
3360 Mount_Read_Only = original_read_only;
3361 return ret;
3362}
Ethan Yonker66a19492015-12-10 10:19:45 -06003363
3364int TWPartition::Decrypt_Adopted() {
3365#ifdef TW_INCLUDE_CRYPTO
3366 int ret = 1;
3367 Is_Adopted_Storage = false;
3368 string Adopted_Key_File = "";
3369
3370 if (!Removable)
3371 return ret;
3372
3373 int fd = open(Alternate_Block_Device.c_str(), O_RDONLY);
3374 if (fd < 0) {
3375 LOGINFO("failed to open '%s'\n", Alternate_Block_Device.c_str());
3376 return ret;
3377 }
3378 char type_guid[80];
3379 char part_guid[80];
3380
nkk713c1e35f2017-03-24 18:28:03 +02003381 uint32_t p_num;
3382 size_t last_digit = Primary_Block_Device.find_last_not_of("0123456789");
3383 if ((last_digit != string::npos) && (last_digit != Primary_Block_Device.length()-1))
3384 p_num = atoi(Primary_Block_Device.substr(last_digit + 1).c_str()) + 1;
3385 else
3386 p_num = 2;
3387
3388 if (gpt_disk_get_partition_info(fd, p_num, type_guid, part_guid) == 0) {
Ethan Yonker66a19492015-12-10 10:19:45 -06003389 LOGINFO("type: '%s'\n", type_guid);
3390 LOGINFO("part: '%s'\n", part_guid);
3391 Adopted_GUID = part_guid;
3392 LOGINFO("Adopted_GUID '%s'\n", Adopted_GUID.c_str());
3393 if (strcmp(type_guid, TWGptAndroidExpand) == 0) {
3394 LOGINFO("android_expand found\n");
3395 Adopted_Key_File = "/data/misc/vold/expand_";
3396 Adopted_Key_File += part_guid;
3397 Adopted_Key_File += ".key";
3398 if (TWFunc::Path_Exists(Adopted_Key_File)) {
3399 Is_Adopted_Storage = true;
3400 /* Until we find a use case for this, I think it is safe
3401 * to disable USB Mass Storage whenever adopted storage
3402 * is present.
3403 */
nkk713c1e35f2017-03-24 18:28:03 +02003404 if (p_num == 2) {
3405 // TODO: Properly detect mixed vs fully adopted storage. Maybe this
3406 // should be moved to partitionmanager instead, and disable after
3407 // checking all partitions. Also the presence of adopted storage does
3408 // not necessarily mean it's being used as Internal Storage
3409 LOGINFO("Detected adopted storage, disabling USB mass storage mode\n");
3410 DataManager::SetValue("tw_has_usb_storage", 0);
3411 }
Ethan Yonker66a19492015-12-10 10:19:45 -06003412 }
3413 }
3414 }
3415
3416 if (Is_Adopted_Storage) {
nkk713c1e35f2017-03-24 18:28:03 +02003417 string Adopted_Block_Device = Alternate_Block_Device + "p" + TWFunc::to_string(p_num);
Ethan Yonker66a19492015-12-10 10:19:45 -06003418 if (!TWFunc::Path_Exists(Adopted_Block_Device)) {
nkk713c1e35f2017-03-24 18:28:03 +02003419 Adopted_Block_Device = Alternate_Block_Device + TWFunc::to_string(p_num);
Ethan Yonker66a19492015-12-10 10:19:45 -06003420 if (!TWFunc::Path_Exists(Adopted_Block_Device)) {
3421 LOGINFO("Adopted block device does not exist\n");
3422 goto exit;
3423 }
3424 }
3425 LOGINFO("key file is '%s', block device '%s'\n", Adopted_Key_File.c_str(), Adopted_Block_Device.c_str());
3426 char crypto_blkdev[MAXPATHLEN];
3427 std::string thekey;
3428 int fdkey = open(Adopted_Key_File.c_str(), O_RDONLY);
3429 if (fdkey < 0) {
3430 LOGINFO("failed to open key file\n");
3431 goto exit;
3432 }
3433 char buf[512];
3434 ssize_t n;
3435 while ((n = read(fdkey, &buf[0], sizeof(buf))) > 0) {
3436 thekey.append(buf, n);
3437 }
3438 close(fdkey);
3439 unsigned char* key = (unsigned char*) thekey.data();
3440 cryptfs_revert_ext_volume(part_guid);
3441
3442 ret = cryptfs_setup_ext_volume(part_guid, Adopted_Block_Device.c_str(), key, thekey.size(), crypto_blkdev);
3443 if (ret == 0) {
3444 LOGINFO("adopted storage new block device: '%s'\n", crypto_blkdev);
3445 Decrypted_Block_Device = crypto_blkdev;
3446 Is_Decrypted = true;
3447 Is_Encrypted = true;
3448 Find_Actual_Block_Device();
Kjell Braden3126a112016-06-19 16:58:15 +00003449 if (!Mount_Storage_Retry(false)) {
Ethan Yonker66a19492015-12-10 10:19:45 -06003450 LOGERR("Failed to mount decrypted adopted storage device\n");
3451 Is_Decrypted = false;
3452 Is_Encrypted = false;
3453 cryptfs_revert_ext_volume(part_guid);
3454 ret = 1;
3455 } else {
Ethan Yonkerfcf3f242016-02-16 12:30:26 -06003456 UnMount(false);
3457 Has_Android_Secure = false;
3458 Symlink_Path = "";
3459 Symlink_Mount_Point = "";
3460 Backup_Name = Mount_Point.substr(1);
3461 Backup_Path = Mount_Point;
3462 TWPartition* sdext = PartitionManager.Find_Partition_By_Path("/sd-ext");
3463 if (sdext && sdext->Actual_Block_Device == Adopted_Block_Device) {
3464 LOGINFO("Removing /sd-ext from partition list due to adopted storage\n");
3465 PartitionManager.Remove_Partition_By_Path("/sd-ext");
3466 }
Ethan Yonker66a19492015-12-10 10:19:45 -06003467 Setup_Data_Media();
Ethan Yonker66a19492015-12-10 10:19:45 -06003468 Wipe_Available_in_GUI = true;
3469 Wipe_During_Factory_Reset = true;
3470 Can_Be_Backed_Up = true;
3471 Can_Encrypt_Backup = true;
3472 Use_Userdata_Encryption = true;
3473 Is_Storage = true;
3474 Storage_Name = "Adopted Storage";
3475 Is_SubPartition = true;
3476 SubPartition_Of = "/data";
3477 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
3478 DataManager::SetValue("tw_has_adopted_storage", 1);
3479 }
3480 } else {
3481 LOGERR("Failed to setup adopted storage decryption\n");
3482 }
3483 }
3484exit:
Matt Mower06543e32017-01-06 15:25:26 -06003485 close(fd);
Ethan Yonker66a19492015-12-10 10:19:45 -06003486 return ret;
3487#else
3488 LOGINFO("Decrypt_Adopted: no crypto support\n");
3489 return 1;
3490#endif
3491}
3492
3493void TWPartition::Revert_Adopted() {
3494#ifdef TW_INCLUDE_CRYPTO
3495 if (!Adopted_GUID.empty()) {
3496 PartitionManager.Remove_MTP_Storage(Mount_Point);
3497 UnMount(false);
3498 cryptfs_revert_ext_volume(Adopted_GUID.c_str());
3499 Is_Adopted_Storage = false;
3500 Is_Encrypted = false;
3501 Is_Decrypted = false;
3502 Decrypted_Block_Device = "";
3503 Find_Actual_Block_Device();
3504 Wipe_During_Factory_Reset = false;
3505 Can_Be_Backed_Up = false;
3506 Can_Encrypt_Backup = false;
3507 Use_Userdata_Encryption = false;
3508 Is_SubPartition = false;
3509 SubPartition_Of = "";
3510 Has_Data_Media = false;
3511 Storage_Path = Mount_Point;
3512 if (!Symlink_Mount_Point.empty()) {
3513 TWPartition* Dat = PartitionManager.Find_Partition_By_Path("/data");
3514 if (Dat) {
3515 Dat->UnMount(false);
3516 Dat->Symlink_Mount_Point = Symlink_Mount_Point;
3517 }
3518 Symlink_Mount_Point = "";
3519 }
3520 }
3521#else
3522 LOGINFO("Revert_Adopted: no crypto support\n");
3523#endif
3524}
bigbiff bigbiffb5ecaad2017-03-20 18:53:53 -04003525
3526void TWPartition::Set_Backup_FileName(string fname) {
3527 Backup_FileName = fname;
3528}
3529
3530string TWPartition::Get_Backup_Name() {
3531 return Backup_Name;
3532}
bigbiffee7b7ff2020-03-23 15:08:27 -04003533
3534string TWPartition::Get_Mount_Point() {
3535 return Mount_Point;
3536}
3537
3538void TWPartition::Set_Block_Device(std::string block_device) {
3539 Primary_Block_Device = Actual_Block_Device = block_device;
3540}
3541
3542bool TWPartition::Get_Super_Status() {
3543 return Is_Super;
3544}
3545
3546void TWPartition::Set_Can_Be_Backed_Up(bool val) {
3547 Can_Be_Backed_Up = val;
3548}
3549
3550void TWPartition::Set_Can_Be_Wiped(bool val) {
3551 Can_Be_Wiped = val;
3552 Wipe_Available_in_GUI = val;
bigbiffad58e1b2020-07-06 20:24:34 -04003553}
3554
3555std::string TWPartition::Get_Backup_FileName() {
3556 return Backup_FileName;
3557}
3558
3559std::string TWPartition::Get_Display_Name() {
3560 return Display_Name;
3561}
3562
3563bool TWPartition::Is_SlotSelect() {
3564 return SlotSelect;
Mohd Faraz815e2402020-09-19 19:31:52 +05303565}