blob: df9960d12e81c4355109ab8e881a360afe385739 [file] [log] [blame]
Dees Troy3be70a82013-10-22 14:25:12 +00001/*
bigbiff bigbiff56cf5642016-08-19 17:43:45 -04002 Copyright 2013 to 2017 TeamWin
Dees Troy3be70a82013-10-22 14:25:12 +00003 This file is part of TWRP/TeamWin Recovery Project.
4
5 TWRP is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 TWRP is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with TWRP. If not, see <http://www.gnu.org/licenses/>.
17*/
Dees_Troy51a0e822012-09-05 15:24:24 -040018
19#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
22#include <sys/stat.h>
23#include <sys/vfs.h>
Dees_Troy5bf43922012-09-07 16:07:55 -040024#include <sys/mount.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040025#include <unistd.h>
Dees_Troy51127312012-09-08 13:08:49 -040026#include <dirent.h>
thatf54e5392016-01-29 22:04:43 +010027#include <libgen.h>
bigbiffce8f83c2015-12-12 18:30:21 -050028#include <zlib.h>
bigbiff bigbiff9c754052013-01-09 09:09:08 -050029#include <iostream>
30#include <sstream>
Ethan Yonker66a19492015-12-10 10:19:45 -060031#include <sys/param.h>
Ethan Yonker472f5062016-02-25 13:47:30 -060032#include <fcntl.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040033
Ethan Yonker007de982018-08-31 14:23:16 -050034#include "cutils/properties.h"
bigbiff7b4c7a62015-01-01 19:44:14 -050035#include "libblkid/include/blkid.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040036#include "variables.h"
Dees_Troy2673cec2013-04-02 20:22:16 +000037#include "twcommon.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040038#include "partitions.hpp"
Dees_Troy5bf43922012-09-07 16:07:55 -040039#include "data.hpp"
Dees_Troy43d8b002012-09-17 16:00:01 -040040#include "twrp-functions.hpp"
bigbiff bigbiff9c754052013-01-09 09:09:08 -050041#include "twrpTar.hpp"
Ethan Yonker3fdcda42016-11-30 12:29:37 -060042#include "exclude.hpp"
Ethan Yonker1b7a31b2014-07-03 15:09:22 -050043#include "infomanager.hpp"
Ethan Yonker4b94cfd2014-12-11 10:00:45 -060044#include "set_metadata.h"
Ethan Yonker74db1572015-10-28 12:44:49 -050045#include "gui/gui.hpp"
bigbiffce8f83c2015-12-12 18:30:21 -050046#include "adbbu/libtwadbbu.hpp"
Ethan Yonker98661c12018-10-17 08:39:28 -050047#ifdef TW_INCLUDE_CRYPTO
48 #include "crypto/fde/cryptfs.h"
49 #ifdef TW_INCLUDE_FBE
50 #include "crypto/ext4crypt/Decrypt.h"
51 #endif
52#else
53 #define CRYPT_FOOTER_OFFSET 0x4000
54#endif
Dees_Troy5bf43922012-09-07 16:07:55 -040055extern "C" {
Dees_Troy38bd7602012-09-14 13:33:53 -040056 #include "mtdutils/mtdutils.h"
57 #include "mtdutils/mounts.h"
Dees_Troya95f55c2013-08-17 13:14:43 +000058#ifdef USE_EXT4
Ethan Yonker8373cfe2017-09-08 06:50:54 -050059 // #include "make_ext4fs.h" TODO need ifdef for android8
60 #include <ext4_utils/make_ext4fs.h>
Dees_Troya95f55c2013-08-17 13:14:43 +000061#endif
Ethan Yonker71413f42014-02-26 13:36:08 -060062#ifdef TW_INCLUDE_CRYPTO
Ethan Yonker66a19492015-12-10 10:19:45 -060063 #include "gpt/gpt.h"
Ethan Yonker71413f42014-02-26 13:36:08 -060064#endif
Dees_Troy5bf43922012-09-07 16:07:55 -040065}
Matt Mower87413642017-01-17 21:14:46 -060066#include <selinux/selinux.h>
Ethan Yonkerf27497f2014-02-09 11:48:33 -060067#include <selinux/label.h>
Dees Troy4159aed2014-02-28 17:24:43 +000068#ifdef HAVE_CAPABILITIES
69#include <sys/capability.h>
70#include <sys/xattr.h>
71#include <linux/xattr.h>
72#endif
HashBanged974bb2016-01-30 14:20:09 -050073#include <sparse_format.h>
Ethan Yonker472f5062016-02-25 13:47:30 -060074#include "progresstracking.hpp"
Dees_Troy51a0e822012-09-05 15:24:24 -040075
bigbiff bigbiff9c754052013-01-09 09:09:08 -050076using namespace std;
77
Ethan Yonker6e8c27a2016-12-22 17:55:57 -060078static int auto_index = 0; // v2 fstab allows you to specify a mount point of "auto" with no /. These items are given a mount point of /auto* where * == auto_index
79
Dees_Troya95f55c2013-08-17 13:14:43 +000080extern struct selabel_handle *selinux_handle;
Ethan Yonker6277c792014-09-15 14:54:30 -050081extern bool datamedia;
Dees_Troya95f55c2013-08-17 13:14:43 +000082
Hashcode62bd9e02013-11-19 21:59:42 -080083struct flag_list {
84 const char *name;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -060085 unsigned long flag;
Hashcode62bd9e02013-11-19 21:59:42 -080086};
87
Matt Mower4ab42b12016-04-21 13:52:18 -050088const struct flag_list mount_flags[] = {
Ethan Yonker6e8c27a2016-12-22 17:55:57 -060089 { "noatime", MS_NOATIME },
90 { "noexec", MS_NOEXEC },
91 { "nosuid", MS_NOSUID },
92 { "nodev", MS_NODEV },
93 { "nodiratime", MS_NODIRATIME },
94 { "ro", MS_RDONLY },
95 { "rw", 0 },
96 { "remount", MS_REMOUNT },
97 { "bind", MS_BIND },
98 { "rec", MS_REC },
Dees Troyc4bc30e2014-02-03 15:04:19 +000099#ifdef MS_UNBINDABLE
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600100 { "unbindable", MS_UNBINDABLE },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000101#endif
102#ifdef MS_PRIVATE
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600103 { "private", MS_PRIVATE },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000104#endif
105#ifdef MS_SLAVE
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600106 { "slave", MS_SLAVE },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000107#endif
108#ifdef MS_SHARED
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600109 { "shared", MS_SHARED },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000110#endif
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600111 { "sync", MS_SYNCHRONOUS },
112 { 0, 0 },
113};
114
115const char *ignored_mount_items[] = {
116 "defaults=",
117 "errors=",
Ethan Yonker93382822018-11-01 15:25:31 -0500118 "latemount",
119 "sysfs_path=",
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600120 NULL
Hashcode62bd9e02013-11-19 21:59:42 -0800121};
122
Matt Mower2416a502016-04-12 19:54:46 -0500123enum TW_FSTAB_FLAGS {
124 TWFLAG_DEFAULTS, // Retain position
125 TWFLAG_ANDSEC,
126 TWFLAG_BACKUP,
127 TWFLAG_BACKUPNAME,
128 TWFLAG_BLOCKSIZE,
129 TWFLAG_CANBEWIPED,
130 TWFLAG_CANENCRYPTBACKUP,
131 TWFLAG_DISPLAY,
132 TWFLAG_ENCRYPTABLE,
Ethan Yonkere9afc3d2018-08-30 15:16:27 -0500133 TWFLAG_FILEENCRYPTION,
Matt Mower2416a502016-04-12 19:54:46 -0500134 TWFLAG_FLASHIMG,
135 TWFLAG_FORCEENCRYPT,
136 TWFLAG_FSFLAGS,
137 TWFLAG_IGNOREBLKID,
138 TWFLAG_LENGTH,
139 TWFLAG_MOUNTTODECRYPT,
140 TWFLAG_REMOVABLE,
141 TWFLAG_RETAINLAYOUTVERSION,
142 TWFLAG_SETTINGSSTORAGE,
143 TWFLAG_STORAGE,
144 TWFLAG_STORAGENAME,
145 TWFLAG_SUBPARTITIONOF,
146 TWFLAG_SYMLINK,
147 TWFLAG_USERDATAENCRYPTBACKUP,
148 TWFLAG_USERMRF,
149 TWFLAG_WIPEDURINGFACTORYRESET,
150 TWFLAG_WIPEINGUI,
Ethan Yonker1b190162016-12-05 15:25:19 -0600151 TWFLAG_SLOTSELECT,
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600152 TWFLAG_WAIT,
153 TWFLAG_VERIFY,
154 TWFLAG_CHECK,
155 TWFLAG_ALTDEVICE,
156 TWFLAG_NOTRIM,
157 TWFLAG_VOLDMANAGED,
158 TWFLAG_FORMATTABLE,
159 TWFLAG_RESIZE,
Ethan Yonker93382822018-11-01 15:25:31 -0500160 TWFLAG_KEYDIRECTORY,
Peter Cai28ea7b02019-05-24 11:38:54 +0800161 TWFLAG_WRAPPEDKEY,
Matt Mower2416a502016-04-12 19:54:46 -0500162};
163
164/* Flags without a trailing '=' are considered dual format flags and can be
165 * written as either 'flagname' or 'flagname=', where the character following
166 * the '=' is Y,y,1 for true and false otherwise.
167 */
168const struct flag_list tw_flags[] = {
169 { "andsec", TWFLAG_ANDSEC },
170 { "backup", TWFLAG_BACKUP },
171 { "backupname=", TWFLAG_BACKUPNAME },
172 { "blocksize=", TWFLAG_BLOCKSIZE },
173 { "canbewiped", TWFLAG_CANBEWIPED },
174 { "canencryptbackup", TWFLAG_CANENCRYPTBACKUP },
175 { "defaults", TWFLAG_DEFAULTS },
176 { "display=", TWFLAG_DISPLAY },
177 { "encryptable=", TWFLAG_ENCRYPTABLE },
Ethan Yonkere9afc3d2018-08-30 15:16:27 -0500178 { "fileencryption=", TWFLAG_FILEENCRYPTION },
Matt Mower2416a502016-04-12 19:54:46 -0500179 { "flashimg", TWFLAG_FLASHIMG },
180 { "forceencrypt=", TWFLAG_FORCEENCRYPT },
181 { "fsflags=", TWFLAG_FSFLAGS },
182 { "ignoreblkid", TWFLAG_IGNOREBLKID },
183 { "length=", TWFLAG_LENGTH },
184 { "mounttodecrypt", TWFLAG_MOUNTTODECRYPT },
185 { "removable", TWFLAG_REMOVABLE },
186 { "retainlayoutversion", TWFLAG_RETAINLAYOUTVERSION },
187 { "settingsstorage", TWFLAG_SETTINGSSTORAGE },
188 { "storage", TWFLAG_STORAGE },
189 { "storagename=", TWFLAG_STORAGENAME },
190 { "subpartitionof=", TWFLAG_SUBPARTITIONOF },
191 { "symlink=", TWFLAG_SYMLINK },
192 { "userdataencryptbackup", TWFLAG_USERDATAENCRYPTBACKUP },
193 { "usermrf", TWFLAG_USERMRF },
194 { "wipeduringfactoryreset", TWFLAG_WIPEDURINGFACTORYRESET },
195 { "wipeingui", TWFLAG_WIPEINGUI },
Ethan Yonker1b190162016-12-05 15:25:19 -0600196 { "slotselect", TWFLAG_SLOTSELECT },
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600197 { "wait", TWFLAG_WAIT },
198 { "verify", TWFLAG_VERIFY },
199 { "check", TWFLAG_CHECK },
200 { "altdevice", TWFLAG_ALTDEVICE },
201 { "notrim", TWFLAG_NOTRIM },
202 { "voldmanaged=", TWFLAG_VOLDMANAGED },
203 { "formattable", TWFLAG_FORMATTABLE },
204 { "resize", TWFLAG_RESIZE },
Ethan Yonker93382822018-11-01 15:25:31 -0500205 { "keydirectory=", TWFLAG_KEYDIRECTORY },
Peter Cai28ea7b02019-05-24 11:38:54 +0800206 { "wrappedkey", TWFLAG_WRAPPEDKEY },
Matt Mower2416a502016-04-12 19:54:46 -0500207 { 0, 0 },
208};
209
that9e0593e2014-10-08 00:01:24 +0200210TWPartition::TWPartition() {
Dees_Troy51a0e822012-09-05 15:24:24 -0400211 Can_Be_Mounted = false;
212 Can_Be_Wiped = false;
Dees_Troya13d74f2013-03-24 08:54:55 -0500213 Can_Be_Backed_Up = false;
Vojtech Bocek1dc30982013-08-30 21:49:30 +0200214 Use_Rm_Rf = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400215 Wipe_During_Factory_Reset = false;
216 Wipe_Available_in_GUI = false;
217 Is_SubPartition = false;
Dees_Troy2691f9d2012-09-24 11:15:49 -0400218 Has_SubPartition = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400219 SubPartition_Of = "";
220 Symlink_Path = "";
221 Symlink_Mount_Point = "";
222 Mount_Point = "";
Dees_Troye58d5262012-09-21 12:27:57 -0400223 Backup_Path = "";
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600224 Wildcard_Block_Device = false;
225 Sysfs_Entry = "";
Dees_Troy38bd7602012-09-14 13:33:53 -0400226 Actual_Block_Device = "";
227 Primary_Block_Device = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400228 Alternate_Block_Device = "";
229 Removable = false;
230 Is_Present = false;
231 Length = 0;
232 Size = 0;
233 Used = 0;
234 Free = 0;
235 Backup_Size = 0;
236 Can_Be_Encrypted = false;
237 Is_Encrypted = false;
238 Is_Decrypted = false;
Ethan Yonkerbd7492d2016-12-07 13:55:01 -0600239 Is_FBE = false;
Ethan Yonker253368a2014-11-25 15:00:52 -0600240 Mount_To_Decrypt = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400241 Decrypted_Block_Device = "";
242 Display_Name = "";
Dees_Troya13d74f2013-03-24 08:54:55 -0500243 Backup_Display_Name = "";
244 Storage_Name = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400245 Backup_Name = "";
Dees_Troy63c8df72012-09-10 14:02:05 -0400246 Backup_FileName = "";
Dees_Troy38bd7602012-09-14 13:33:53 -0400247 MTD_Name = "";
bigbiffce8f83c2015-12-12 18:30:21 -0500248 Backup_Method = BM_NONE;
Dees_Troy83bd4832013-05-04 12:39:56 +0000249 Can_Encrypt_Backup = false;
250 Use_Userdata_Encryption = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400251 Has_Data_Media = false;
Dees_Troye58d5262012-09-21 12:27:57 -0400252 Has_Android_Secure = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400253 Is_Storage = false;
Dees_Troya13d74f2013-03-24 08:54:55 -0500254 Is_Settings_Storage = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400255 Storage_Path = "";
256 Current_File_System = "";
257 Fstab_File_System = "";
Hashcode62bd9e02013-11-19 21:59:42 -0800258 Mount_Flags = 0;
259 Mount_Options = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400260 Format_Block_Size = 0;
Dees_Troy68cab492012-12-12 19:29:35 +0000261 Ignore_Blkid = false;
Dees_Troy16c2b312013-01-15 16:51:18 +0000262 Retain_Layout_Version = false;
dianlujitao4879b372018-12-03 18:45:47 +0800263 Crypto_Key_Location = "";
Ethan Yonker726a0202014-12-16 20:01:38 -0600264 MTP_Storage_ID = 0;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600265 Can_Flash_Img = false;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500266 Mount_Read_Only = false;
Ethan Yonker66a19492015-12-10 10:19:45 -0600267 Is_Adopted_Storage = false;
268 Adopted_GUID = "";
Ethan Yonker1b190162016-12-05 15:25:19 -0600269 SlotSelect = false;
Ethan Yonker93382822018-11-01 15:25:31 -0500270 Key_Directory = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400271}
272
273TWPartition::~TWPartition(void) {
274 // Do nothing
275}
276
Chaosmasterf6e42ce2020-01-27 00:17:04 +0100277bool TWPartition::Process_Fstab_Line(const char *fstab_line, bool Display_Error, std::map<string, Flags_Map> *twrp_flags, bool Sar_Detect) {
Matt Mower2b2dd152016-04-26 11:24:08 -0500278 char full_line[MAX_FSTAB_LINE_LENGTH];
Matt Mower2416a502016-04-12 19:54:46 -0500279 char twflags[MAX_FSTAB_LINE_LENGTH] = "";
Dees_Troy5bf43922012-09-07 16:07:55 -0400280 char* ptr;
Matt Mower2b2dd152016-04-26 11:24:08 -0500281 int line_len = strlen(fstab_line), index = 0, item_index = 0;
Dees_Troya13d74f2013-03-24 08:54:55 -0500282 bool skip = false;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600283 int fstab_version = 1, mount_point_index = 0, fs_index = 1, block_device_index = 2;
284 TWPartition *additional_entry = NULL;
285 std::map<string, Flags_Map>::iterator it;
Dees_Troy5bf43922012-09-07 16:07:55 -0400286
Matt Mower2b2dd152016-04-26 11:24:08 -0500287 strlcpy(full_line, fstab_line, sizeof(full_line));
Dees_Troy51127312012-09-08 13:08:49 -0400288 for (index = 0; index < line_len; index++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500289 if (full_line[index] == 34)
290 skip = !skip;
291 if (!skip && full_line[index] <= 32)
Dees_Troy5bf43922012-09-07 16:07:55 -0400292 full_line[index] = '\0';
Dees_Troy5bf43922012-09-07 16:07:55 -0400293 }
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600294 if (line_len < 10)
295 return false; // There can't possibly be a valid fstab line that is less than 10 chars
296 if (strncmp(fstab_line, "/dev/", strlen("/dev/")) == 0 || strncmp(fstab_line, "/devices/", strlen("/devices/")) == 0) {
297 fstab_version = 2;
298 block_device_index = 0;
299 mount_point_index = 1;
300 fs_index = 2;
301 }
302
303 index = 0;
Dees_Troy5bf43922012-09-07 16:07:55 -0400304 while (index < line_len) {
305 while (index < line_len && full_line[index] == '\0')
306 index++;
307 if (index >= line_len)
308 continue;
309 ptr = full_line + index;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600310 if (item_index == mount_point_index) {
311 Mount_Point = ptr;
312 if (fstab_version == 2) {
313 additional_entry = PartitionManager.Find_Partition_By_Path(Mount_Point);
Chaosmasterf6e42ce2020-01-27 00:17:04 +0100314 if (!Sar_Detect && additional_entry) {
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600315 LOGINFO("Found an additional entry for '%s'\n", Mount_Point.c_str());
316 }
317 }
Chaosmasterf6e42ce2020-01-27 00:17:04 +0100318 if(!Sar_Detect)
319 LOGINFO("Processing '%s'\n", Mount_Point.c_str());
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600320 Backup_Path = Mount_Point;
321 Storage_Path = Mount_Point;
322 Display_Name = ptr + 1;
323 Backup_Display_Name = Display_Name;
324 Storage_Name = Display_Name;
325 item_index++;
326 } else if (item_index == fs_index) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400327 // File System
328 Fstab_File_System = ptr;
329 Current_File_System = ptr;
330 item_index++;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600331 } else if (item_index == block_device_index) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400332 // Primary Block Device
Dees_Troy38bd7602012-09-14 13:33:53 -0400333 if (Fstab_File_System == "mtd" || Fstab_File_System == "yaffs2") {
Dees_Troy094207a2012-09-26 12:00:39 -0400334 MTD_Name = ptr;
335 Find_MTD_Block_Device(MTD_Name);
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400336 } else if (Fstab_File_System == "bml") {
337 if (Mount_Point == "/boot")
338 MTD_Name = "boot";
339 else if (Mount_Point == "/recovery")
340 MTD_Name = "recovery";
341 Primary_Block_Device = ptr;
342 if (*ptr != '/')
Dees_Troy2673cec2013-04-02 20:22:16 +0000343 LOGERR("Until we get better BML support, you will have to find and provide the full block device path to the BML devices e.g. /dev/block/bml9 instead of the partition name\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400344 } else if (*ptr != '/') {
Dees_Troy5bf43922012-09-07 16:07:55 -0400345 if (Display_Error)
Matt Mower2b2dd152016-04-26 11:24:08 -0500346 LOGERR("Invalid block device '%s' in fstab line '%s'", ptr, fstab_line);
Dees_Troy5bf43922012-09-07 16:07:55 -0400347 else
Matt Mower2b2dd152016-04-26 11:24:08 -0500348 LOGINFO("Invalid block device '%s' in fstab line '%s'", ptr, fstab_line);
Matt Mower72c87ce2016-04-26 14:34:56 -0500349 return false;
Dees_Troy38bd7602012-09-14 13:33:53 -0400350 } else {
351 Primary_Block_Device = ptr;
352 Find_Real_Block_Device(Primary_Block_Device, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400353 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400354 item_index++;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600355 } else if (item_index > 2) {
356 if (fstab_version == 2) {
357 if (item_index == 3) {
358 Process_FS_Flags(ptr);
359 if (additional_entry) {
360 additional_entry->Save_FS_Flags(Fstab_File_System, Mount_Flags, Mount_Options);
361 return false; // We save the extra fs flags in the other partition entry and by returning false, this entry will be deleted
362 }
363 } else {
364 strlcpy(twflags, ptr, sizeof(twflags));
365 }
366 item_index++;
367 } else if (*ptr == '/') { // v2 fstab does not allow alternate block devices
Dees_Troy5bf43922012-09-07 16:07:55 -0400368 // Alternate Block Device
369 Alternate_Block_Device = ptr;
370 Find_Real_Block_Device(Alternate_Block_Device, Display_Error);
371 } else if (strlen(ptr) > 7 && strncmp(ptr, "length=", 7) == 0) {
372 // Partition length
373 ptr += 7;
374 Length = atoi(ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400375 } else if (strlen(ptr) > 6 && strncmp(ptr, "flags=", 6) == 0) {
376 // Custom flags, save for later so that new values aren't overwritten by defaults
377 ptr += 6;
Matt Mower2416a502016-04-12 19:54:46 -0500378 strlcpy(twflags, ptr, sizeof(twflags));
Dees_Troy38bd7602012-09-14 13:33:53 -0400379 } else if (strlen(ptr) == 4 && (strncmp(ptr, "NULL", 4) == 0 || strncmp(ptr, "null", 4) == 0 || strncmp(ptr, "null", 4) == 0)) {
380 // Do nothing
Dees_Troy5bf43922012-09-07 16:07:55 -0400381 } else {
382 // Unhandled data
Matt Mower2b2dd152016-04-26 11:24:08 -0500383 LOGINFO("Unhandled fstab information '%s' in fstab line '%s'\n", ptr, fstab_line);
Dees_Troy5bf43922012-09-07 16:07:55 -0400384 }
385 }
386 while (index < line_len && full_line[index] != '\0')
387 index++;
388 }
389
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600390 // override block devices from the v2 fstab with the ones we read from the twrp.flags file in case they are different
391 if (fstab_version == 2 && twrp_flags && twrp_flags->size() > 0) {
392 it = twrp_flags->find(Mount_Point);
393 if (it != twrp_flags->end()) {
394 if (!it->second.Primary_Block_Device.empty()) {
395 Primary_Block_Device = it->second.Primary_Block_Device;
396 Find_Real_Block_Device(Primary_Block_Device, Display_Error);
397 }
398 if (!it->second.Alternate_Block_Device.empty()) {
399 Alternate_Block_Device = it->second.Alternate_Block_Device;
400 Find_Real_Block_Device(Alternate_Block_Device, Display_Error);
401 }
402 }
403 }
404
405 if (strncmp(fstab_line, "/devices/", strlen("/devices/")) == 0) {
406 Sysfs_Entry = Primary_Block_Device;
407 Primary_Block_Device = "";
408 Is_Storage = true;
409 Removable = true;
410 Wipe_Available_in_GUI = true;
411 Wildcard_Block_Device = true;
412 }
413 if (Primary_Block_Device.find("*") != string::npos)
414 Wildcard_Block_Device = true;
415
Chaosmasterf6e42ce2020-01-27 00:17:04 +0100416 if (Sar_Detect) {
417 if(Is_File_System(Fstab_File_System) && (Mount_Point == "/" || Mount_Point == "/system" || Mount_Point == "/system_root"))
418 Find_Actual_Block_Device();
419 else
420 return true;
421 } else if (Mount_Point == "auto") {
dianlujitaob76a73a2020-04-30 20:33:20 +0800422 Mount_Point = "/auto" + to_string(auto_index);
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600423 Backup_Path = Mount_Point;
424 Storage_Path = Mount_Point;
dianlujitaob76a73a2020-04-30 20:33:20 +0800425 Backup_Name = Mount_Point.substr(1);
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600426 auto_index++;
427 Setup_File_System(Display_Error);
428 Display_Name = "Storage";
429 Backup_Display_Name = Display_Name;
430 Storage_Name = Display_Name;
431 Can_Be_Backed_Up = false;
432 Wipe_Available_in_GUI = true;
433 Is_Storage = true;
434 Removable = true;
435 Wipe_Available_in_GUI = true;
436 } else if (!Is_File_System(Fstab_File_System) && !Is_Image(Fstab_File_System)) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400437 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000438 LOGERR("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400439 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000440 LOGINFO("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Matt Mower72c87ce2016-04-26 14:34:56 -0500441 return false;
Dees_Troy5bf43922012-09-07 16:07:55 -0400442 } else if (Is_File_System(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400443 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400444 Setup_File_System(Display_Error);
dianlujitaob76a73a2020-04-30 20:33:20 +0800445 Backup_Name = Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
Chaosmaster3b682732020-01-25 21:09:28 +0100446 if (Mount_Point == "/" || Mount_Point == "/system" || Mount_Point == "/system_root") {
Dees_Troy5bf43922012-09-07 16:07:55 -0400447 Display_Name = "System";
dianlujitaob76a73a2020-04-30 20:33:20 +0800448 Backup_Name = "system";
Dees_Troya13d74f2013-03-24 08:54:55 -0500449 Backup_Display_Name = Display_Name;
450 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400451 Wipe_Available_in_GUI = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500452 Can_Be_Backed_Up = true;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500453 Mount_Read_Only = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400454 } else if (Mount_Point == "/data") {
455 Display_Name = "Data";
Dees_Troya13d74f2013-03-24 08:54:55 -0500456 Backup_Display_Name = Display_Name;
457 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400458 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400459 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500460 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000461 Can_Encrypt_Backup = true;
462 Use_Userdata_Encryption = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400463 } else if (Mount_Point == "/cache") {
464 Display_Name = "Cache";
Dees_Troya13d74f2013-03-24 08:54:55 -0500465 Backup_Display_Name = Display_Name;
466 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400467 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400468 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500469 Can_Be_Backed_Up = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400470 } else if (Mount_Point == "/datadata") {
Dees_Troy51127312012-09-08 13:08:49 -0400471 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400472 Display_Name = "DataData";
Dees_Troya13d74f2013-03-24 08:54:55 -0500473 Backup_Display_Name = Display_Name;
474 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400475 Is_SubPartition = true;
476 SubPartition_Of = "/data";
Dees_Troy5bf43922012-09-07 16:07:55 -0400477 DataManager::SetValue(TW_HAS_DATADATA, 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500478 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000479 Can_Encrypt_Backup = true;
480 Use_Userdata_Encryption = false; // This whole partition should be encrypted
Dees_Troy5bf43922012-09-07 16:07:55 -0400481 } else if (Mount_Point == "/sd-ext") {
Dees_Troy51127312012-09-08 13:08:49 -0400482 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400483 Display_Name = "SD-Ext";
Dees_Troya13d74f2013-03-24 08:54:55 -0500484 Backup_Display_Name = Display_Name;
485 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400486 Wipe_Available_in_GUI = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400487 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500488 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000489 Can_Encrypt_Backup = true;
490 Use_Userdata_Encryption = true;
Dees_Troy2c50e182012-09-26 20:05:28 -0400491 } else if (Mount_Point == "/boot") {
492 Display_Name = "Boot";
Dees_Troya13d74f2013-03-24 08:54:55 -0500493 Backup_Display_Name = Display_Name;
Dees_Troy2c50e182012-09-26 20:05:28 -0400494 DataManager::SetValue("tw_boot_is_mountable", 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500495 Can_Be_Backed_Up = true;
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500496 } else if (Mount_Point == "/vendor") {
497 Display_Name = "Vendor";
498 Backup_Display_Name = Display_Name;
499 Storage_Name = Display_Name;
500 Mount_Read_Only = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400501 }
502#ifdef TW_EXTERNAL_STORAGE_PATH
503 if (Mount_Point == EXPAND(TW_EXTERNAL_STORAGE_PATH)) {
504 Is_Storage = true;
505 Storage_Path = EXPAND(TW_EXTERNAL_STORAGE_PATH);
Dees_Troyc51f1f92012-09-20 15:32:13 -0400506 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500507 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400508#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000509 if (Mount_Point == "/sdcard" || Mount_Point == "/external_sd" || Mount_Point == "/external_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400510 Is_Storage = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400511 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500512 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400513#endif
Dees_Troyb05ddee2013-01-28 20:24:50 +0000514 }
Dees_Troy8170a922012-09-18 15:40:25 -0400515#ifdef TW_INTERNAL_STORAGE_PATH
516 if (Mount_Point == EXPAND(TW_INTERNAL_STORAGE_PATH)) {
517 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500518 Is_Settings_Storage = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400519 Storage_Path = EXPAND(TW_INTERNAL_STORAGE_PATH);
Dees_Troya13d74f2013-03-24 08:54:55 -0500520 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400521 }
522#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000523 if (Mount_Point == "/emmc" || Mount_Point == "/internal_sd" || Mount_Point == "/internal_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400524 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500525 Is_Settings_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500526 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400527 }
528#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400529 } else if (Is_Image(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400530 Find_Actual_Block_Device();
Ethan Yonker1b190162016-12-05 15:25:19 -0600531 Setup_Image();
Dees_Troya13d74f2013-03-24 08:54:55 -0500532 if (Mount_Point == "/boot") {
533 Display_Name = "Boot";
534 Backup_Display_Name = Display_Name;
535 Can_Be_Backed_Up = true;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600536 Can_Flash_Img = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500537 } else if (Mount_Point == "/recovery") {
538 Display_Name = "Recovery";
539 Backup_Display_Name = Display_Name;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600540 Can_Flash_Img = true;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500541 } else if (Mount_Point == "/system_image") {
542 Display_Name = "System Image";
543 Backup_Display_Name = Display_Name;
HashBanged974bb2016-01-30 14:20:09 -0500544 Can_Flash_Img = true;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500545 Can_Be_Backed_Up = true;
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500546 } else if (Mount_Point == "/vendor_image") {
547 Display_Name = "Vendor Image";
548 Backup_Display_Name = Display_Name;
HashBanged974bb2016-01-30 14:20:09 -0500549 Can_Flash_Img = true;
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500550 Can_Be_Backed_Up = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500551 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400552 }
553
Matt Mower2416a502016-04-12 19:54:46 -0500554 // Process TWRP fstab flags
555 if (strlen(twflags) > 0) {
556 string Prev_Display_Name = Display_Name;
557 string Prev_Storage_Name = Storage_Name;
558 string Prev_Backup_Display_Name = Backup_Display_Name;
559 Display_Name = "";
560 Storage_Name = "";
561 Backup_Display_Name = "";
562
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600563 Process_TW_Flags(twflags, (fstab_version == 1), fstab_version);
564 Save_FS_Flags(Fstab_File_System, Mount_Flags, Mount_Options);
Matt Mower2416a502016-04-12 19:54:46 -0500565
566 bool has_display_name = !Display_Name.empty();
567 bool has_storage_name = !Storage_Name.empty();
568 bool has_backup_name = !Backup_Display_Name.empty();
569 if (!has_display_name) Display_Name = Prev_Display_Name;
570 if (!has_storage_name) Storage_Name = Prev_Storage_Name;
571 if (!has_backup_name) Backup_Display_Name = Prev_Backup_Display_Name;
572
573 if (has_display_name && !has_storage_name)
574 Storage_Name = Display_Name;
575 if (!has_display_name && has_storage_name)
576 Display_Name = Storage_Name;
577 if (has_display_name && !has_backup_name && Backup_Display_Name != "Android Secure")
578 Backup_Display_Name = Display_Name;
579 if (!has_display_name && has_backup_name)
580 Display_Name = Backup_Display_Name;
581 }
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600582
583 if (fstab_version == 2 && twrp_flags && twrp_flags->size() > 0) {
584 it = twrp_flags->find(Mount_Point);
585 if (it != twrp_flags->end()) {
586 char twrpflags[MAX_FSTAB_LINE_LENGTH] = "";
587 int skip = 0;
588 string Flags = it->second.Flags;
589 strcpy(twrpflags, Flags.c_str());
590 if (strlen(twrpflags) > strlen("flags=") && strncmp(twrpflags, "flags=", strlen("flags=")) == 0)
591 skip += strlen("flags=");
592 char* flagptr = twrpflags;
593 flagptr += skip;
594 Process_TW_Flags(flagptr, Display_Error, 1); // Forcing the fstab to ver 1 because this data is coming from the /etc/twrp.flags which should be using the TWRP v1 flags format
595 }
596 }
nkk7198fc3992017-12-16 16:26:42 +0200597
598 if (Mount_Point == "/persist" && Can_Be_Mounted) {
599 bool mounted = Is_Mounted();
600 if (mounted || Mount(false)) {
601 // Read the backup settings file
602 DataManager::LoadPersistValues();
603 TWFunc::Fixup_Time_On_Boot("/persist/time/");
604 if (!mounted)
605 UnMount(false);
606 }
607 }
608
Chaosmasterf6e42ce2020-01-27 00:17:04 +0100609 if (Is_File_System(Fstab_File_System) && (Mount_Point == "/" || Mount_Point == "/system" || Mount_Point == "/system_root")) {
610 if (Sar_Detect) {
611 Mount_Point = "/s";
612 Mount_Read_Only = true;
613 Can_Be_Mounted = true;
614 } else {
615 Mount_Point = PartitionManager.Get_Android_Root_Path();
616 Backup_Path = Mount_Point;
617 Storage_Path = Mount_Point;
618 Make_Dir(Mount_Point, Display_Error);
619 }
620 }
621
Dees_Troy51127312012-09-08 13:08:49 -0400622 return true;
623}
624
Matt Mower72c87ce2016-04-26 14:34:56 -0500625void TWPartition::Partition_Post_Processing(bool Display_Error) {
626 if (Mount_Point == "/data")
627 Setup_Data_Partition(Display_Error);
628 else if (Mount_Point == "/cache")
629 Setup_Cache_Partition(Display_Error);
630}
631
Ethan Yonkerbd7492d2016-12-07 13:55:01 -0600632void TWPartition::ExcludeAll(const string& path) {
633 backup_exclusions.add_absolute_dir(path);
634 wipe_exclusions.add_absolute_dir(path);
635}
636
Matt Mower72c87ce2016-04-26 14:34:56 -0500637void TWPartition::Setup_Data_Partition(bool Display_Error) {
638 if (Mount_Point != "/data")
639 return;
640
641 // Ensure /data is not mounted as tmpfs for qcom hardware decrypt
642 UnMount(false);
643
644#ifdef TW_INCLUDE_CRYPTO
645 if (datamedia)
646 Setup_Data_Media();
647 Can_Be_Encrypted = true;
648 char crypto_blkdev[255];
649 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
650 if (strcmp(crypto_blkdev, "error") != 0) {
651 DataManager::SetValue(TW_IS_DECRYPTED, 1);
652 Is_Encrypted = true;
653 Is_Decrypted = true;
Noah Jacobson0b25b1d2019-04-28 00:10:07 -0400654 if (Key_Directory.empty()) {
Ethan Yonker93382822018-11-01 15:25:31 -0500655 Is_FBE = false;
Noah Jacobson0b25b1d2019-04-28 00:10:07 -0400656 DataManager::SetValue(TW_IS_FBE, 0);
657 } else {
Ethan Yonker93382822018-11-01 15:25:31 -0500658 Is_FBE = true;
Noah Jacobson0b25b1d2019-04-28 00:10:07 -0400659 DataManager::SetValue(TW_IS_FBE, 1);
660 }
Matt Mower72c87ce2016-04-26 14:34:56 -0500661 Decrypted_Block_Device = crypto_blkdev;
662 LOGINFO("Data already decrypted, new block device: '%s'\n", crypto_blkdev);
663 } else if (!Mount(false)) {
664 if (Is_Present) {
Ethan Yonker93382822018-11-01 15:25:31 -0500665 if (Key_Directory.empty()) {
Noah Jacobson0b25b1d2019-04-28 00:10:07 -0400666 set_partition_data(Actual_Block_Device.c_str(), Crypto_Key_Location.c_str(),
667 Fstab_File_System.c_str());
Ethan Yonker93382822018-11-01 15:25:31 -0500668 if (cryptfs_check_footer() == 0) {
669 Is_Encrypted = true;
670 Is_Decrypted = false;
671 Can_Be_Mounted = false;
672 Current_File_System = "emmc";
673 Setup_Image();
674 DataManager::SetValue(TW_IS_ENCRYPTED, 1);
675 DataManager::SetValue(TW_CRYPTO_PWTYPE, cryptfs_get_password_type());
Noah Jacobson0b25b1d2019-04-28 00:10:07 -0400676 DataManager::SetValue("tw_crypto_pwtype_0", cryptfs_get_password_type());
Ethan Yonker93382822018-11-01 15:25:31 -0500677 DataManager::SetValue(TW_CRYPTO_PASSWORD, "");
678 DataManager::SetValue("tw_crypto_display", "");
679 } else {
680 gui_err("mount_data_footer=Could not mount /data and unable to find crypto footer.");
681 }
682 } else {
Matt Mower72c87ce2016-04-26 14:34:56 -0500683 Is_Encrypted = true;
684 Is_Decrypted = false;
Matt Mower72c87ce2016-04-26 14:34:56 -0500685 }
Ethan Yonker93382822018-11-01 15:25:31 -0500686 } else if (Key_Directory.empty()) {
Noah Jacobson0b25b1d2019-04-28 00:10:07 -0400687 LOGERR("Primary block device '%s' for mount point '%s' is not present!\n",
688 Primary_Block_Device.c_str(), Mount_Point.c_str());
Matt Mower72c87ce2016-04-26 14:34:56 -0500689 }
690 } else {
mauronofrio9bf73352019-11-23 22:27:34 +0100691 if (!Decrypt_FBE_DE()) {
mauronofriofc79aa62019-11-23 23:13:04 +0100692 char wrappedvalue[PROPERTY_VALUE_MAX];
693 property_get("fbe.data.wrappedkey", wrappedvalue, "");
694 std::string wrappedkeyvalue(wrappedvalue);
695 if (wrappedkeyvalue == "true") {
696 LOGERR("Unable to decrypt FBE device\n");
697 } else {
698 LOGINFO("Trying wrapped key.\n");
699 property_set("fbe.data.wrappedkey", "true");
mauronofrio9bf73352019-11-23 22:27:34 +0100700 if (!Decrypt_FBE_DE()) {
bigbiffd58ba182020-03-23 10:02:29 -0400701 LOGERR("Unable to decrypt FBE device\n");
mauronofrio9bf73352019-11-23 22:27:34 +0100702 }
mauronofriofc79aa62019-11-23 23:13:04 +0100703 }
mauronofrio9bf73352019-11-23 22:27:34 +0100704 }
Matt Mower72c87ce2016-04-26 14:34:56 -0500705 }
706 if (datamedia && (!Is_Encrypted || (Is_Encrypted && Is_Decrypted))) {
707 Setup_Data_Media();
708 Recreate_Media_Folder();
709 }
710#else
711 if (datamedia) {
712 Setup_Data_Media();
713 Recreate_Media_Folder();
714 }
715#endif
716}
717
Ethan Yonker93382822018-11-01 15:25:31 -0500718bool TWPartition::Decrypt_FBE_DE() {
719if (TWFunc::Path_Exists("/data/unencrypted/key/version")) {
720 LOGINFO("File Based Encryption is present\n");
721#ifdef TW_INCLUDE_FBE
Noah Jacobson0b25b1d2019-04-28 00:10:07 -0400722 Is_FBE = true;
723 DataManager::SetValue(TW_IS_FBE, 1);
724 ExcludeAll(Mount_Point + "/convert_fbe");
725 ExcludeAll(Mount_Point + "/unencrypted");
726 ExcludeAll(Mount_Point + "/misc/vold/user_keys");
727 ExcludeAll(Mount_Point + "/system/gatekeeper.password.key");
728 ExcludeAll(Mount_Point + "/system/gatekeeper.pattern.key");
729 ExcludeAll(Mount_Point + "/system/locksettings.db");
730 ExcludeAll(Mount_Point + "/system/locksettings.db-wal");
731 ExcludeAll(Mount_Point + "/misc/gatekeeper");
732 ExcludeAll(Mount_Point + "/misc/keystore");
733 ExcludeAll(Mount_Point + "/drm/kek.dat");
734 ExcludeAll(Mount_Point + "/system_de/0/spblob"); // contains data needed to decrypt pixel 2
735 ExcludeAll(Mount_Point + "/system/users/0/gatekeeper.password.key");
736 ExcludeAll(Mount_Point + "/system/users/0/gatekeeper.pattern.key");
737 ExcludeAll(Mount_Point + "/cache");
738 int retry_count = 3;
739 while (!Decrypt_DE() && --retry_count)
740 usleep(2000);
741 PartitionManager.Parse_Users(); // after load_all_de_keys() to parse_users
742 std::vector<users_struct>::iterator iter;
743 std::vector<users_struct>* userList = PartitionManager.Get_Users_List();
744 for (iter = userList->begin(); iter != userList->end(); iter++) {
745 if (atoi((*iter).userId.c_str()) != 0) {
746 ExcludeAll(Mount_Point + "/system_de/" + (*iter).userId + "/spblob");
747 ExcludeAll(Mount_Point + "/system/users/" + (*iter).userId + "/gatekeeper.password.key");
748 ExcludeAll(Mount_Point + "/system/users/" + (*iter).userId + "/gatekeeper.pattern.key");
749 ExcludeAll(Mount_Point + "/system/users/" + (*iter).userId + "/locksettings.db");
750 ExcludeAll(Mount_Point + "/system/users/" + (*iter).userId + "/locksettings.db-wal");
Ethan Yonker93382822018-11-01 15:25:31 -0500751 }
Noah Jacobson0b25b1d2019-04-28 00:10:07 -0400752 }
753 if (retry_count > 0) {
754 property_set("ro.crypto.state", "encrypted");
755 Is_Encrypted = true;
756 Is_Decrypted = false;
757 DataManager::SetValue(TW_IS_ENCRYPTED, 1);
758 string filename;
759 int pwd_type = Get_Password_Type(0, filename);
760 if (pwd_type < 0) {
761 LOGERR("This TWRP does not have synthetic password decrypt support\n");
762 pwd_type = 0; // default password
763 }
764 DataManager::SetValue(TW_CRYPTO_PWTYPE, pwd_type);
765 DataManager::SetValue("tw_crypto_pwtype_0", pwd_type);
766 DataManager::SetValue(TW_CRYPTO_PASSWORD, "");
767 DataManager::SetValue("tw_crypto_display", "");
768 return true;
769 }
Ethan Yonker93382822018-11-01 15:25:31 -0500770#else
771 LOGERR("FBE found but FBE support not present in TWRP\n");
772#endif
773 }
774 return false;
775}
776
Matt Mower72c87ce2016-04-26 14:34:56 -0500777void TWPartition::Setup_Cache_Partition(bool Display_Error __unused) {
778 if (Mount_Point != "/cache")
779 return;
780
781 if (!Mount(true))
782 return;
783
784 if (!TWFunc::Path_Exists("/cache/recovery/.")) {
785 LOGINFO("Recreating /cache/recovery folder\n");
786 if (mkdir("/cache/recovery", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0)
787 LOGERR("Could not create /cache/recovery\n");
788 }
789}
790
Matt Mower4ab42b12016-04-21 13:52:18 -0500791void TWPartition::Process_FS_Flags(const char *str) {
792 char *options = strdup(str);
793 char *ptr, *savep;
Hashcode62bd9e02013-11-19 21:59:42 -0800794
Matt Mower4ab42b12016-04-21 13:52:18 -0500795 Mount_Options = "";
Hashcode62bd9e02013-11-19 21:59:42 -0800796
Matt Mower4ab42b12016-04-21 13:52:18 -0500797 // Avoid issues with potentially nested strtok by using strtok_r
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600798 for (ptr = strtok_r(options, ",", &savep); ptr; ptr = strtok_r(NULL, ",", &savep)) {
799 char *equals = strstr(ptr, "=");
800 size_t name_len;
Matt Mower4ab42b12016-04-21 13:52:18 -0500801
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600802 if (!equals)
803 name_len = strlen(ptr);
804 else
805 name_len = equals - ptr;
806
807 // There are some flags that we want to ignore in TWRP
808 bool found_match = false;
809 for (const char** ignored_mount_item = ignored_mount_items; *ignored_mount_item; ignored_mount_item++) {
810 if (strncmp(ptr, *ignored_mount_item, name_len) == 0) {
811 found_match = true;
Hashcode62bd9e02013-11-19 21:59:42 -0800812 break;
813 }
814 }
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600815 if (found_match)
816 continue;
Hashcode62bd9e02013-11-19 21:59:42 -0800817
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600818 // mount_flags are never postfixed by '='
819 if (!equals) {
820 const struct flag_list* mount_flag = mount_flags;
821 for (; mount_flag->name; mount_flag++) {
822 if (strcmp(ptr, mount_flag->name) == 0) {
823 if (mount_flag->flag == MS_RDONLY)
824 Mount_Read_Only = true;
825 else
826 Mount_Flags |= (unsigned)mount_flag->flag;
827 found_match = true;
828 break;
829 }
830 }
831 if (found_match)
832 continue;
Matt Mower4ab42b12016-04-21 13:52:18 -0500833 }
834
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600835 // If we aren't ignoring this flag and it's not a mount flag, then it must be a mount option
836 if (!Mount_Options.empty())
837 Mount_Options += ",";
838 Mount_Options += ptr;
Matt Mower4ab42b12016-04-21 13:52:18 -0500839 }
840 free(options);
Hashcode62bd9e02013-11-19 21:59:42 -0800841}
842
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600843void TWPartition::Save_FS_Flags(const string& local_File_System, int local_Mount_Flags, const string& local_Mount_Options) {
844 partition_fs_flags_struct flags;
845 flags.File_System = local_File_System;
846 flags.Mount_Flags = local_Mount_Flags;
847 flags.Mount_Options = local_Mount_Options;
848 fs_flags.push_back(flags);
849}
850
Matt Mower2416a502016-04-12 19:54:46 -0500851void TWPartition::Apply_TW_Flag(const unsigned flag, const char* str, const bool val) {
852 switch (flag) {
853 case TWFLAG_ANDSEC:
854 Has_Android_Secure = val;
855 break;
856 case TWFLAG_BACKUP:
857 Can_Be_Backed_Up = val;
858 break;
859 case TWFLAG_BACKUPNAME:
860 Backup_Display_Name = str;
861 break;
862 case TWFLAG_BLOCKSIZE:
863 Format_Block_Size = (unsigned long)(atol(str));
864 break;
865 case TWFLAG_CANBEWIPED:
866 Can_Be_Wiped = val;
867 break;
868 case TWFLAG_CANENCRYPTBACKUP:
869 Can_Encrypt_Backup = val;
870 break;
871 case TWFLAG_DEFAULTS:
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600872 case TWFLAG_WAIT:
873 case TWFLAG_VERIFY:
874 case TWFLAG_CHECK:
875 case TWFLAG_NOTRIM:
876 case TWFLAG_VOLDMANAGED:
877 case TWFLAG_RESIZE:
Matt Mower2416a502016-04-12 19:54:46 -0500878 // Do nothing
879 break;
880 case TWFLAG_DISPLAY:
881 Display_Name = str;
882 break;
883 case TWFLAG_ENCRYPTABLE:
884 case TWFLAG_FORCEENCRYPT:
885 Crypto_Key_Location = str;
886 break;
Ethan Yonkere9afc3d2018-08-30 15:16:27 -0500887 case TWFLAG_FILEENCRYPTION:
888 // This flag isn't used by TWRP but is needed in 9.0 FBE decrypt
889 // fileencryption=ice:aes-256-heh
890 {
891 std::string FBE = str;
Ethan Yonkere9afc3d2018-08-30 15:16:27 -0500892 size_t colon_loc = FBE.find(":");
893 if (colon_loc == std::string::npos) {
Ethan Yonker93382822018-11-01 15:25:31 -0500894 property_set("fbe.contents", FBE.c_str());
895 property_set("fbe.filenames", "");
896 LOGINFO("FBE contents '%s', filenames ''\n", FBE.c_str());
Ethan Yonkere9afc3d2018-08-30 15:16:27 -0500897 break;
898 }
Ethan Yonker93382822018-11-01 15:25:31 -0500899 std::string FBE_contents, FBE_filenames;
Ethan Yonkere9afc3d2018-08-30 15:16:27 -0500900 FBE_contents = FBE.substr(0, colon_loc);
901 FBE_filenames = FBE.substr(colon_loc + 1);
902 property_set("fbe.contents", FBE_contents.c_str());
903 property_set("fbe.filenames", FBE_filenames.c_str());
904 LOGINFO("FBE contents '%s', filenames '%s'\n", FBE_contents.c_str(), FBE_filenames.c_str());
905 }
906 break;
Peter Cai28ea7b02019-05-24 11:38:54 +0800907 case TWFLAG_WRAPPEDKEY:
908 // Set fbe.data.wrappedkey to true
909 {
910 property_set("fbe.data.wrappedkey", "true");
911 LOGINFO("FBE wrapped key enabled\n");
912 }
913 break;
Matt Mower2416a502016-04-12 19:54:46 -0500914 case TWFLAG_FLASHIMG:
915 Can_Flash_Img = val;
916 break;
917 case TWFLAG_FSFLAGS:
Matt Mower4ab42b12016-04-21 13:52:18 -0500918 Process_FS_Flags(str);
Matt Mower2416a502016-04-12 19:54:46 -0500919 break;
920 case TWFLAG_IGNOREBLKID:
921 Ignore_Blkid = val;
922 break;
923 case TWFLAG_LENGTH:
924 Length = atoi(str);
925 break;
926 case TWFLAG_MOUNTTODECRYPT:
927 Mount_To_Decrypt = val;
928 break;
929 case TWFLAG_REMOVABLE:
930 Removable = val;
931 break;
932 case TWFLAG_RETAINLAYOUTVERSION:
933 Retain_Layout_Version = val;
934 break;
935 case TWFLAG_SETTINGSSTORAGE:
936 Is_Settings_Storage = val;
937 if (Is_Settings_Storage)
938 Is_Storage = true;
939 break;
940 case TWFLAG_STORAGE:
941 Is_Storage = val;
942 break;
943 case TWFLAG_STORAGENAME:
944 Storage_Name = str;
945 break;
946 case TWFLAG_SUBPARTITIONOF:
947 Is_SubPartition = true;
948 SubPartition_Of = str;
949 break;
950 case TWFLAG_SYMLINK:
951 Symlink_Path = str;
952 break;
953 case TWFLAG_USERDATAENCRYPTBACKUP:
954 Use_Userdata_Encryption = val;
955 if (Use_Userdata_Encryption)
956 Can_Encrypt_Backup = true;
957 break;
958 case TWFLAG_USERMRF:
959 Use_Rm_Rf = val;
960 break;
961 case TWFLAG_WIPEDURINGFACTORYRESET:
962 Wipe_During_Factory_Reset = val;
963 if (Wipe_During_Factory_Reset) {
964 Can_Be_Wiped = true;
965 Wipe_Available_in_GUI = true;
966 }
967 break;
968 case TWFLAG_WIPEINGUI:
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600969 case TWFLAG_FORMATTABLE:
Matt Mower2416a502016-04-12 19:54:46 -0500970 Wipe_Available_in_GUI = val;
971 if (Wipe_Available_in_GUI)
972 Can_Be_Wiped = true;
973 break;
Ethan Yonker1b190162016-12-05 15:25:19 -0600974 case TWFLAG_SLOTSELECT:
975 SlotSelect = true;
976 break;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600977 case TWFLAG_ALTDEVICE:
978 Alternate_Block_Device = str;
979 break;
Ethan Yonker93382822018-11-01 15:25:31 -0500980 case TWFLAG_KEYDIRECTORY:
981 Key_Directory = str;
bigbiffd58ba182020-03-23 10:02:29 -0400982 break;
Matt Mower2416a502016-04-12 19:54:46 -0500983 default:
984 // Should not get here
985 LOGINFO("Flag identified for processing, but later unmatched: %i\n", flag);
986 break;
987 }
988}
Dees_Troy51127312012-09-08 13:08:49 -0400989
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600990void TWPartition::Process_TW_Flags(char *flags, bool Display_Error, int fstab_ver) {
Matt Mower2416a502016-04-12 19:54:46 -0500991 char separator[2] = {'\n', 0};
992 char *ptr, *savep;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600993 char source_separator = ';';
994
995 if (fstab_ver == 2)
996 source_separator = ',';
Matt Mower2416a502016-04-12 19:54:46 -0500997
998 // Semicolons within double-quotes are not forbidden, so replace
999 // only the semicolons intended as separators with '\n' for strtok
1000 for (unsigned i = 0, skip = 0; i < strlen(flags); i++) {
1001 if (flags[i] == '\"')
Dees_Troya13d74f2013-03-24 08:54:55 -05001002 skip = !skip;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06001003 if (!skip && flags[i] == source_separator)
Matt Mower2416a502016-04-12 19:54:46 -05001004 flags[i] = separator[0];
Dees_Troy51127312012-09-08 13:08:49 -04001005 }
1006
Matt Mower2416a502016-04-12 19:54:46 -05001007 // Avoid issues with potentially nested strtok by using strtok_r
1008 ptr = strtok_r(flags, separator, &savep);
1009 while (ptr) {
1010 int ptr_len = strlen(ptr);
1011 const struct flag_list* tw_flag = tw_flags;
Hashcode62bd9e02013-11-19 21:59:42 -08001012
Matt Mower2416a502016-04-12 19:54:46 -05001013 for (; tw_flag->name; tw_flag++) {
1014 int flag_len = strlen(tw_flag->name);
1015
1016 if (strncmp(ptr, tw_flag->name, flag_len) == 0) {
1017 bool flag_val = false;
1018
1019 if (ptr_len > flag_len && (tw_flag->name)[flag_len-1] != '='
1020 && ptr[flag_len] != '=') {
1021 // Handle flags with same starting string
1022 // (e.g. backup and backupname)
1023 continue;
1024 } else if (ptr_len > flag_len && ptr[flag_len] == '=') {
1025 // Handle flags with dual format: Part 1
1026 // (e.g. backup and backup=y. backup=y handled here)
1027 ptr += flag_len + 1;
1028 TWFunc::Strip_Quotes(ptr);
1029 // Skip flags with empty argument
1030 // (e.g. backup=)
1031 if (strlen(ptr) == 0) {
1032 LOGINFO("Flag missing argument or should not include '=': %s=\n", tw_flag->name);
1033 break;
1034 }
1035 flag_val = strchr("yY1", *ptr) != NULL;
1036 } else if (ptr_len == flag_len
1037 && (tw_flag->name)[flag_len-1] == '=') {
1038 // Skip flags missing argument after =
1039 // (e.g. backupname=)
1040 LOGINFO("Flag missing argument: %s\n", tw_flag->name);
1041 break;
1042 } else if (ptr_len > flag_len
1043 && (tw_flag->name)[flag_len-1] == '=') {
1044 // Handle arguments to flags
1045 // (e.g. backupname="My Stuff")
1046 ptr += flag_len;
1047 TWFunc::Strip_Quotes(ptr);
1048 // Skip flags with empty argument
1049 // (e.g. backupname="")
1050 if (strlen(ptr) == 0) {
1051 LOGINFO("Flag missing argument: %s\n", tw_flag->name);
1052 break;
1053 }
1054 } else if (ptr_len == flag_len) {
1055 // Handle flags with dual format: Part 2
1056 // (e.g. backup and backup=y. backup handled here)
1057 flag_val = true;
Ethan Yonker96af84a2015-01-05 14:58:36 -06001058 } else {
Matt Mower2416a502016-04-12 19:54:46 -05001059 LOGINFO("Flag matched, but could not be processed: %s\n", ptr);
1060 break;
Ethan Yonker96af84a2015-01-05 14:58:36 -06001061 }
Matt Mower2416a502016-04-12 19:54:46 -05001062
1063 Apply_TW_Flag(tw_flag->flag, ptr, flag_val);
1064 break;
Ethan Yonker96af84a2015-01-05 14:58:36 -06001065 }
Matt Mower2416a502016-04-12 19:54:46 -05001066 }
1067 if (tw_flag->name == 0) {
Dees_Troy51127312012-09-08 13:08:49 -04001068 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001069 LOGERR("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -04001070 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001071 LOGINFO("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -04001072 }
Matt Mower2416a502016-04-12 19:54:46 -05001073 ptr = strtok_r(NULL, separator, &savep);
Dees_Troy51127312012-09-08 13:08:49 -04001074 }
Dees_Troy51a0e822012-09-05 15:24:24 -04001075}
1076
Dees_Troy5bf43922012-09-07 16:07:55 -04001077bool TWPartition::Is_File_System(string File_System) {
1078 if (File_System == "ext2" ||
Dees_Troy63c8df72012-09-10 14:02:05 -04001079 File_System == "ext3" ||
Dees_Troy5bf43922012-09-07 16:07:55 -04001080 File_System == "ext4" ||
1081 File_System == "vfat" ||
1082 File_System == "ntfs" ||
1083 File_System == "yaffs2" ||
bigbiff bigbiff3e146522012-11-14 14:32:59 -05001084 File_System == "exfat" ||
Dees_Troye5017042013-08-29 16:38:55 +00001085 File_System == "f2fs" ||
Erfan Abdib859e912019-02-05 18:18:46 +02001086 File_System == "squashfs" ||
Dees_Troy5bf43922012-09-07 16:07:55 -04001087 File_System == "auto")
1088 return true;
1089 else
1090 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001091}
1092
Dees_Troy5bf43922012-09-07 16:07:55 -04001093bool TWPartition::Is_Image(string File_System) {
Dees_Troy5fcd8f92012-10-16 12:22:05 -04001094 if (File_System == "emmc" || File_System == "mtd" || File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -04001095 return true;
1096 else
1097 return false;
1098}
1099
Dees_Troy51127312012-09-08 13:08:49 -04001100bool TWPartition::Make_Dir(string Path, bool Display_Error) {
Ethan Yonker93ac7a02016-11-07 22:05:58 -06001101 if (TWFunc::Get_D_Type_From_Stat(Path) != S_IFDIR)
1102 unlink(Path.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001103 if (!TWFunc::Path_Exists(Path)) {
Dees_Troy51127312012-09-08 13:08:49 -04001104 if (mkdir(Path.c_str(), 0777) == -1) {
1105 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001106 gui_msg(Msg(msg::kError, "create_folder_strerr=Can not create '{1}' folder ({2}).")(Path)(strerror(errno)));
Dees_Troy51127312012-09-08 13:08:49 -04001107 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001108 LOGINFO("Can not create '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -04001109 return false;
1110 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001111 LOGINFO("Created '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -04001112 return true;
1113 }
1114 }
1115 return true;
1116}
1117
Dees_Troy5bf43922012-09-07 16:07:55 -04001118void TWPartition::Setup_File_System(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001119 Can_Be_Mounted = true;
1120 Can_Be_Wiped = true;
1121
Dees_Troy5bf43922012-09-07 16:07:55 -04001122 // Make the mount point folder if it doesn't exist
Dees_Troy51127312012-09-08 13:08:49 -04001123 Make_Dir(Mount_Point, Display_Error);
bigbiffce8f83c2015-12-12 18:30:21 -05001124 Backup_Method = BM_FILES;
Dees_Troy5bf43922012-09-07 16:07:55 -04001125}
1126
Ethan Yonker1b190162016-12-05 15:25:19 -06001127void TWPartition::Setup_Image() {
Dees_Troy5bf43922012-09-07 16:07:55 -04001128 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
1129 Backup_Name = Display_Name;
Gary Peck82599a82012-11-21 16:23:12 -08001130 if (Current_File_System == "emmc")
bigbiffce8f83c2015-12-12 18:30:21 -05001131 Backup_Method = BM_DD;
Gary Peck82599a82012-11-21 16:23:12 -08001132 else if (Current_File_System == "mtd" || Current_File_System == "bml")
bigbiffce8f83c2015-12-12 18:30:21 -05001133 Backup_Method = BM_FLASH_UTILS;
Dees_Troy5bf43922012-09-07 16:07:55 -04001134 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001135 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 -04001136}
1137
Dees_Troye58d5262012-09-21 12:27:57 -04001138void TWPartition::Setup_AndSec(void) {
Dees_Troya13d74f2013-03-24 08:54:55 -05001139 Backup_Display_Name = "Android Secure";
Dees_Troye58d5262012-09-21 12:27:57 -04001140 Backup_Name = "and-sec";
Dees_Troya13d74f2013-03-24 08:54:55 -05001141 Can_Be_Backed_Up = true;
Dees_Troye58d5262012-09-21 12:27:57 -04001142 Has_Android_Secure = true;
1143 Symlink_Path = Mount_Point + "/.android_secure";
1144 Symlink_Mount_Point = "/and-sec";
1145 Backup_Path = Symlink_Mount_Point;
1146 Make_Dir("/and-sec", true);
1147 Recreate_AndSec_Folder();
Kjell Braden3126a112016-06-19 16:58:15 +00001148 Mount_Storage_Retry(true);
Dees_Troye58d5262012-09-21 12:27:57 -04001149}
1150
that9e0593e2014-10-08 00:01:24 +02001151void TWPartition::Setup_Data_Media() {
Ethan Yonker6277c792014-09-15 14:54:30 -05001152 LOGINFO("Setting up '%s' as data/media emulated storage.\n", Mount_Point.c_str());
Ethan Yonker66a19492015-12-10 10:19:45 -06001153 if (Storage_Name.empty() || Storage_Name == "Data")
1154 Storage_Name = "Internal Storage";
Ethan Yonker6277c792014-09-15 14:54:30 -05001155 Has_Data_Media = true;
1156 Is_Storage = true;
Ethan Yonker66a19492015-12-10 10:19:45 -06001157 Storage_Path = Mount_Point + "/media";
Ethan Yonker6277c792014-09-15 14:54:30 -05001158 Symlink_Path = Storage_Path;
Ethan Yonker66a19492015-12-10 10:19:45 -06001159 if (Mount_Point == "/data") {
1160 Is_Settings_Storage = true;
1161 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
1162 Make_Dir("/emmc", false);
1163 Symlink_Mount_Point = "/emmc";
1164 } else {
1165 Make_Dir("/sdcard", false);
1166 Symlink_Mount_Point = "/sdcard";
1167 }
1168 if (Mount(false) && TWFunc::Path_Exists(Mount_Point + "/media/0")) {
1169 Storage_Path = Mount_Point + "/media/0";
1170 Symlink_Path = Storage_Path;
1171 DataManager::SetValue(TW_INTERNAL_PATH, Mount_Point + "/media/0");
1172 UnMount(true);
1173 }
1174 DataManager::SetValue("tw_has_internal", 1);
1175 DataManager::SetValue("tw_has_data_media", 1);
Ethan Yonker3fdcda42016-11-30 12:29:37 -06001176 backup_exclusions.add_absolute_dir("/data/data/com.google.android.music/files");
Ethan Yonker6355b562017-05-01 09:42:17 -05001177 wipe_exclusions.add_absolute_dir(Mount_Point + "/misc/vold"); // adopted storage keys
Ethan Yonker79f88bd2016-12-09 14:52:12 -06001178 ExcludeAll(Mount_Point + "/.layout_version");
1179 ExcludeAll(Mount_Point + "/system/storage.xml");
Ethan Yonker6277c792014-09-15 14:54:30 -05001180 } else {
Ethan Yonker66a19492015-12-10 10:19:45 -06001181 if (Mount(true) && TWFunc::Path_Exists(Mount_Point + "/media/0")) {
1182 Storage_Path = Mount_Point + "/media/0";
1183 Symlink_Path = Storage_Path;
1184 UnMount(true);
1185 }
Ethan Yonker6277c792014-09-15 14:54:30 -05001186 }
Ethan Yonker79f88bd2016-12-09 14:52:12 -06001187 ExcludeAll(Mount_Point + "/media");
Ethan Yonker6277c792014-09-15 14:54:30 -05001188}
1189
Dees_Troy5bf43922012-09-07 16:07:55 -04001190void TWPartition::Find_Real_Block_Device(string& Block, bool Display_Error) {
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06001191 char device[PATH_MAX], realDevice[PATH_MAX];
Dees_Troy5bf43922012-09-07 16:07:55 -04001192
1193 strcpy(device, Block.c_str());
1194 memset(realDevice, 0, sizeof(realDevice));
1195 while (readlink(device, realDevice, sizeof(realDevice)) > 0)
1196 {
1197 strcpy(device, realDevice);
1198 memset(realDevice, 0, sizeof(realDevice));
1199 }
1200
1201 if (device[0] != '/') {
1202 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001203 LOGERR("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001204 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001205 LOGINFO("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001206 return;
Dees_Troy51a0e822012-09-05 15:24:24 -04001207 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -04001208 Block = device;
1209 return;
1210 }
1211}
1212
Kjell Braden3126a112016-06-19 16:58:15 +00001213bool TWPartition::Mount_Storage_Retry(bool Display_Error) {
Dees_Troy8e337f32012-10-13 22:07:49 -04001214 // On some devices, storage doesn't want to mount right away, retry and sleep
Kjell Braden3126a112016-06-19 16:58:15 +00001215 if (!Mount(Display_Error)) {
Dees_Troy8e337f32012-10-13 22:07:49 -04001216 int retry_count = 5;
1217 while (retry_count > 0 && !Mount(false)) {
1218 usleep(500000);
1219 retry_count--;
1220 }
Kjell Braden3126a112016-06-19 16:58:15 +00001221 return Mount(Display_Error);
Dees_Troy8e337f32012-10-13 22:07:49 -04001222 }
Kjell Braden3126a112016-06-19 16:58:15 +00001223 return true;
Dees_Troy8e337f32012-10-13 22:07:49 -04001224}
1225
Dees_Troy38bd7602012-09-14 13:33:53 -04001226bool TWPartition::Find_MTD_Block_Device(string MTD_Name) {
1227 FILE *fp = NULL;
1228 char line[255];
1229
1230 fp = fopen("/proc/mtd", "rt");
1231 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001232 LOGERR("Device does not support /proc/mtd\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001233 return false;
1234 }
1235
1236 while (fgets(line, sizeof(line), fp) != NULL)
1237 {
1238 char device[32], label[32];
1239 unsigned long size = 0;
Dees_Troy38bd7602012-09-14 13:33:53 -04001240 int deviceId;
1241
1242 sscanf(line, "%s %lx %*s %*c%s", device, &size, label);
1243
1244 // Skip header and blank lines
1245 if ((strcmp(device, "dev:") == 0) || (strlen(line) < 8))
1246 continue;
1247
1248 // Strip off the trailing " from the label
1249 label[strlen(label)-1] = '\0';
1250
1251 if (strcmp(label, MTD_Name.c_str()) == 0) {
1252 // We found our device
1253 // Strip off the trailing : from the device
1254 device[strlen(device)-1] = '\0';
1255 if (sscanf(device,"mtd%d", &deviceId) == 1) {
1256 sprintf(device, "/dev/block/mtdblock%d", deviceId);
1257 Primary_Block_Device = device;
Dees_Troy76543db2013-06-19 16:24:30 +00001258 fclose(fp);
1259 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -04001260 }
1261 }
1262 }
1263 fclose(fp);
1264
1265 return false;
1266}
1267
Dees_Troy51127312012-09-08 13:08:49 -04001268bool TWPartition::Get_Size_Via_statfs(bool Display_Error) {
1269 struct statfs st;
1270 string Local_Path = Mount_Point + "/.";
1271
1272 if (!Mount(Display_Error))
1273 return false;
1274
1275 if (statfs(Local_Path.c_str(), &st) != 0) {
1276 if (!Removable) {
1277 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001278 LOGERR("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -04001279 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001280 LOGINFO("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -04001281 }
1282 return false;
1283 }
1284 Size = (st.f_blocks * st.f_bsize);
1285 Used = ((st.f_blocks - st.f_bfree) * st.f_bsize);
1286 Free = (st.f_bfree * st.f_bsize);
1287 Backup_Size = Used;
1288 return true;
1289}
1290
1291bool TWPartition::Get_Size_Via_df(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001292 FILE* fp;
1293 char command[255], line[512];
1294 int include_block = 1;
1295 unsigned int min_len;
1296
1297 if (!Mount(Display_Error))
1298 return false;
1299
Dees_Troy38bd7602012-09-14 13:33:53 -04001300 min_len = Actual_Block_Device.size() + 2;
Dees_Troy51127312012-09-08 13:08:49 -04001301 sprintf(command, "df %s > /tmp/dfoutput.txt", Mount_Point.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001302 TWFunc::Exec_Cmd(command);
Dees_Troy51127312012-09-08 13:08:49 -04001303 fp = fopen("/tmp/dfoutput.txt", "rt");
1304 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001305 LOGINFO("Unable to open /tmp/dfoutput.txt.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001306 return false;
Dees_Troy51127312012-09-08 13:08:49 -04001307 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001308
1309 while (fgets(line, sizeof(line), fp) != NULL)
1310 {
1311 unsigned long blocks, used, available;
1312 char device[64];
1313 char tmpString[64];
1314
1315 if (strncmp(line, "Filesystem", 10) == 0)
1316 continue;
1317 if (strlen(line) < min_len) {
1318 include_block = 0;
1319 continue;
1320 }
1321 if (include_block) {
1322 sscanf(line, "%s %lu %lu %lu", device, &blocks, &used, &available);
1323 } else {
1324 // The device block string is so long that the df information is on the next line
1325 int space_count = 0;
Dees_Troye58d5262012-09-21 12:27:57 -04001326 sprintf(tmpString, "/dev/block/%s", Actual_Block_Device.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001327 while (tmpString[space_count] == 32)
1328 space_count++;
1329 sscanf(line + space_count, "%lu %lu %lu", &blocks, &used, &available);
1330 }
1331
1332 // Adjust block size to byte size
1333 Size = blocks * 1024ULL;
1334 Used = used * 1024ULL;
1335 Free = available * 1024ULL;
1336 Backup_Size = Used;
1337 }
1338 fclose(fp);
1339 return true;
1340}
1341
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001342unsigned long long TWPartition::IOCTL_Get_Block_Size() {
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001343 Find_Actual_Block_Device();
Ethan Yonker483e9f42016-01-11 22:21:18 -06001344
1345 return TWFunc::IOCTL_Get_Block_Size(Actual_Block_Device.c_str());
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001346}
1347
Dees_Troy5bf43922012-09-07 16:07:55 -04001348bool TWPartition::Find_Partition_Size(void) {
1349 FILE* fp;
1350 char line[512];
1351 string tmpdevice;
1352
igoriok87e3d932013-01-31 21:03:53 +02001353 fp = fopen("/proc/dumchar_info", "rt");
1354 if (fp != NULL) {
1355 while (fgets(line, sizeof(line), fp) != NULL)
1356 {
1357 char label[32], device[32];
1358 unsigned long size = 0;
1359
thatd43bf2d2014-09-21 23:13:02 +02001360 sscanf(line, "%s %lx %*x %*u %s", label, &size, device);
igoriok87e3d932013-01-31 21:03:53 +02001361
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05001362 // Skip header, annotation and blank lines
igoriok87e3d932013-01-31 21:03:53 +02001363 if ((strncmp(device, "/dev/", 5) != 0) || (strlen(line) < 8))
1364 continue;
1365
1366 tmpdevice = "/dev/";
1367 tmpdevice += label;
1368 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
1369 Size = size;
1370 fclose(fp);
1371 return true;
1372 }
1373 }
1374 }
1375
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001376 unsigned long long ioctl_size = IOCTL_Get_Block_Size();
1377 if (ioctl_size) {
1378 Size = ioctl_size;
1379 return true;
1380 }
1381
Dees_Troy5bf43922012-09-07 16:07:55 -04001382 // In this case, we'll first get the partitions we care about (with labels)
1383 fp = fopen("/proc/partitions", "rt");
1384 if (fp == NULL)
1385 return false;
1386
1387 while (fgets(line, sizeof(line), fp) != NULL)
1388 {
1389 unsigned long major, minor, blocks;
1390 char device[512];
Dees_Troy5bf43922012-09-07 16:07:55 -04001391
Dees_Troy63c8df72012-09-10 14:02:05 -04001392 if (strlen(line) < 7 || line[0] == 'm') continue;
Dees_Troy5bf43922012-09-07 16:07:55 -04001393 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
1394
1395 tmpdevice = "/dev/block/";
1396 tmpdevice += device;
Dees_Troy38bd7602012-09-14 13:33:53 -04001397 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001398 // Adjust block size to byte size
1399 Size = blocks * 1024ULL;
1400 fclose(fp);
1401 return true;
1402 }
1403 }
1404 fclose(fp);
1405 return false;
1406}
1407
Dees_Troy5bf43922012-09-07 16:07:55 -04001408bool TWPartition::Is_Mounted(void) {
1409 if (!Can_Be_Mounted)
1410 return false;
1411
1412 struct stat st1, st2;
1413 string test_path;
1414
1415 // Check to see if the mount point directory exists
1416 test_path = Mount_Point + "/.";
1417 if (stat(test_path.c_str(), &st1) != 0) return false;
1418
1419 // Check to see if the directory above the mount point exists
1420 test_path = Mount_Point + "/../.";
1421 if (stat(test_path.c_str(), &st2) != 0) return false;
1422
1423 // Compare the device IDs -- if they match then we're (probably) using tmpfs instead of an actual device
1424 int ret = (st1.st_dev != st2.st_dev) ? true : false;
1425
1426 return ret;
1427}
1428
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05001429bool TWPartition::Is_File_System_Writable(void) {
1430 if (!Is_File_System(Current_File_System) || !Is_Mounted())
1431 return false;
1432
1433 string test_path = Mount_Point + "/.";
1434 return (access(test_path.c_str(), W_OK) == 0);
1435}
1436
Dees_Troy5bf43922012-09-07 16:07:55 -04001437bool TWPartition::Mount(bool Display_Error) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001438 int exfat_mounted = 0;
Martin Dünkelmann6d843782018-12-27 23:40:28 +01001439 unsigned int flags = Mount_Flags;
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001440
Dees_Troy5bf43922012-09-07 16:07:55 -04001441 if (Is_Mounted()) {
1442 return true;
1443 } else if (!Can_Be_Mounted) {
1444 return false;
1445 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001446
1447 Find_Actual_Block_Device();
1448
1449 // Check the current file system before mounting
1450 Check_FS_Type();
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001451 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/exfat-fuse")) {
Dees_Troye34c1332013-02-06 19:13:00 +00001452 string cmd = "/sbin/exfat-fuse -o big_writes,max_read=131072,max_write=131072 " + Actual_Block_Device + " " + Mount_Point;
Dees_Troy2673cec2013-04-02 20:22:16 +00001453 LOGINFO("cmd: %s\n", cmd.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001454 string result;
1455 if (TWFunc::Exec_Cmd(cmd, result) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001456 LOGINFO("exfat-fuse failed to mount with result '%s', trying vfat\n", result.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001457 Current_File_System = "vfat";
1458 } else {
1459#ifdef TW_NO_EXFAT_FUSE
1460 UnMount(false);
1461 // We'll let the kernel handle it but using exfat-fuse to detect if the file system is actually exfat
1462 // Some kernels let us mount vfat as exfat which doesn't work out too well
1463#else
1464 exfat_mounted = 1;
1465#endif
1466 }
1467 }
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001468
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001469 if (Current_File_System == "ntfs" && (TWFunc::Path_Exists("/sbin/ntfs-3g") || TWFunc::Path_Exists("/sbin/mount.ntfs"))) {
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001470 string cmd;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001471 string Ntfsmount_Binary = "";
1472
1473 if (TWFunc::Path_Exists("/sbin/ntfs-3g"))
1474 Ntfsmount_Binary = "ntfs-3g";
1475 else if (TWFunc::Path_Exists("/sbin/mount.ntfs"))
1476 Ntfsmount_Binary = "mount.ntfs";
1477
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001478 if (Mount_Read_Only)
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001479 cmd = "/sbin/" + Ntfsmount_Binary + " -o ro " + Actual_Block_Device + " " + Mount_Point;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001480 else
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001481 cmd = "/sbin/" + Ntfsmount_Binary + " " + Actual_Block_Device + " " + Mount_Point;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001482 LOGINFO("cmd: '%s'\n", cmd.c_str());
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001483
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001484 if (TWFunc::Exec_Cmd(cmd) == 0) {
1485 return true;
1486 } else {
1487 LOGINFO("ntfs-3g failed to mount, trying regular mount method.\n");
1488 }
1489 }
1490
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001491 if (Mount_Read_Only)
1492 flags |= MS_RDONLY;
1493
Dees_Troy22042032012-12-18 21:23:08 +00001494 if (Fstab_File_System == "yaffs2") {
1495 // mount an MTD partition as a YAFFS2 filesystem.
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001496 flags = MS_NOATIME | MS_NODEV | MS_NODIRATIME;
1497 if (Mount_Read_Only)
1498 flags |= MS_RDONLY;
Dees_Troy76543db2013-06-19 16:24:30 +00001499 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags, NULL) < 0) {
1500 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags | MS_RDONLY, NULL) < 0) {
1501 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001502 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy76543db2013-06-19 16:24:30 +00001503 else
1504 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
1505 return false;
1506 } else {
1507 LOGINFO("Mounted '%s' (MTD) as RO\n", Mount_Point.c_str());
1508 return true;
1509 }
1510 } else {
1511 struct stat st;
1512 string test_path = Mount_Point;
1513 if (stat(test_path.c_str(), &st) < 0) {
1514 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001515 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy76543db2013-06-19 16:24:30 +00001516 else
1517 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
1518 return false;
1519 }
1520 mode_t new_mode = st.st_mode | S_IXUSR | S_IXGRP | S_IXOTH;
1521 if (new_mode != st.st_mode) {
1522 LOGINFO("Fixing execute permissions for %s\n", Mount_Point.c_str());
1523 if (chmod(Mount_Point.c_str(), new_mode) < 0) {
1524 if (Display_Error)
1525 LOGERR("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
1526 else
1527 LOGINFO("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
1528 return false;
1529 }
1530 }
Dees_Troy22042032012-12-18 21:23:08 +00001531 return true;
Dees_Troy76543db2013-06-19 16:24:30 +00001532 }
thatc7572eb2015-03-31 18:55:30 +02001533 }
1534
1535 string mount_fs = Current_File_System;
1536 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sys/module/texfat"))
1537 mount_fs = "texfat";
1538
1539 if (!exfat_mounted &&
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001540 mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), mount_fs.c_str(), flags, Mount_Options.c_str()) != 0 &&
1541 mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), mount_fs.c_str(), flags, NULL) != 0) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001542#ifdef TW_NO_EXFAT_FUSE
1543 if (Current_File_System == "exfat") {
Dees_Troy2673cec2013-04-02 20:22:16 +00001544 LOGINFO("Mounting exfat failed, trying vfat...\n");
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001545 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), "vfat", 0, NULL) != 0) {
Dees_Troy85f44ed2013-01-09 18:42:36 +00001546 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001547 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy85f44ed2013-01-09 18:42:36 +00001548 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001549 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001550 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 +00001551 return false;
Dees_Troy85f44ed2013-01-09 18:42:36 +00001552 }
Dees_Troyb05ddee2013-01-28 20:24:50 +00001553 } else {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001554#endif
bigbiff bigbiff26774a02014-03-29 18:22:00 -04001555 if (!Removable && Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001556 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001557 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001558 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
1559 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 +00001560 return false;
1561#ifdef TW_NO_EXFAT_FUSE
Dees_Troy85f44ed2013-01-09 18:42:36 +00001562 }
1563#endif
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001564 }
Ethan Yonker253368a2014-11-25 15:00:52 -06001565
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001566 if (Removable)
1567 Update_Size(Display_Error);
1568
xiaolu9416f4f2015-06-04 08:22:23 +08001569 if (!Symlink_Mount_Point.empty() && TWFunc::Path_Exists(Symlink_Path)) {
codelover2a3d4ce2015-03-14 20:26:49 +08001570 string Command = "mount -o bind '" + Symlink_Path + "' '" + Symlink_Mount_Point + "'";
Vojtech Bocek05534202013-09-11 08:11:56 +02001571 TWFunc::Exec_Cmd(Command);
Dees_Troy5bf43922012-09-07 16:07:55 -04001572 }
Chaosmasterda974802020-01-26 21:09:28 +01001573
1574 if (Mount_Point == "/system_root") {
1575 unlink("/system");
1576 mkdir("/system", 0755);
1577 mount("/system_root/system", "/system", "auto", MS_BIND, NULL);
1578 }
1579
Dees_Troy5bf43922012-09-07 16:07:55 -04001580 return true;
1581}
1582
1583bool TWPartition::UnMount(bool Display_Error) {
Chaosmasterda974802020-01-26 21:09:28 +01001584 if (Mount_Point == "/system_root") {
1585 if (umount("/system") == -1)
1586 umount2("/system", MNT_DETACH);
1587 rmdir("/system");
1588 symlink("/system_root/system", "/system");
1589 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001590 if (Is_Mounted()) {
1591 int never_unmount_system;
1592
1593 DataManager::GetValue(TW_DONT_UNMOUNT_SYSTEM, never_unmount_system);
Captain Throwback9d6feb52018-07-27 10:05:24 -04001594 if (never_unmount_system == 1 && Mount_Point == PartitionManager.Get_Android_Root_Path())
Dees_Troy5bf43922012-09-07 16:07:55 -04001595 return true; // Never unmount system if you're not supposed to unmount it
1596
Matt Mowera8e6d832017-02-14 20:20:59 -06001597 if (Is_Storage && MTP_Storage_ID > 0)
Ethan Yonker726a0202014-12-16 20:01:38 -06001598 PartitionManager.Remove_MTP_Storage(MTP_Storage_ID);
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001599
Dees_Troy38bd7602012-09-14 13:33:53 -04001600 if (!Symlink_Mount_Point.empty())
1601 umount(Symlink_Mount_Point.c_str());
1602
Dees_Troyb05ddee2013-01-28 20:24:50 +00001603 umount(Mount_Point.c_str());
1604 if (Is_Mounted()) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001605 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001606 gui_msg(Msg(msg::kError, "fail_unmount=Failed to unmount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy5bf43922012-09-07 16:07:55 -04001607 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001608 LOGINFO("Unable to unmount '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001609 return false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001610 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -04001611 return true;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001612 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001613 } else {
1614 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001615 }
1616}
1617
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05001618bool TWPartition::ReMount(bool Display_Error) {
1619 if (UnMount(Display_Error))
1620 return Mount(Display_Error);
1621 return false;
1622}
1623
1624bool TWPartition::ReMount_RW(bool Display_Error) {
1625 // No need to remount if already mounted rw
1626 if (Is_File_System_Writable())
1627 return true;
1628
1629 bool ro = Mount_Read_Only;
1630 int flags = Mount_Flags;
1631
1632 Mount_Read_Only = false;
1633 Mount_Flags &= ~MS_RDONLY;
1634
1635 bool ret = ReMount(Display_Error);
1636
1637 Mount_Read_Only = ro;
1638 Mount_Flags = flags;
1639
1640 return ret;
1641}
1642
Gary Peck43acadf2012-11-21 21:19:01 -08001643bool TWPartition::Wipe(string New_File_System) {
Ethan Yonker726a0202014-12-16 20:01:38 -06001644 bool wiped = false, update_crypt = false, recreate_media = true;
Dees_Troy16c2b312013-01-15 16:51:18 +00001645 int check;
1646 string Layout_Filename = Mount_Point + "/.layout_version";
1647
Dees_Troy38bd7602012-09-14 13:33:53 -04001648 if (!Can_Be_Wiped) {
Matt Mower3c366972015-12-25 19:28:31 -06001649 gui_msg(Msg(msg::kError, "cannot_wipe=Partition {1} cannot be wiped.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04001650 return false;
1651 }
1652
Dees_Troyc51f1f92012-09-20 15:32:13 -04001653 if (Mount_Point == "/cache")
Dees_Troy2673cec2013-04-02 20:22:16 +00001654 Log_Offset = 0;
Dees_Troyc51f1f92012-09-20 15:32:13 -04001655
Dees_Troy16c2b312013-01-15 16:51:18 +00001656 if (Retain_Layout_Version && Mount(false) && TWFunc::Path_Exists(Layout_Filename))
1657 TWFunc::copy_file(Layout_Filename, "/.layout_version", 0600);
1658 else
1659 unlink("/.layout_version");
Dees_Troy38bd7602012-09-14 13:33:53 -04001660
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001661 if (Has_Data_Media && Current_File_System == New_File_System) {
Dees_Troy16c2b312013-01-15 16:51:18 +00001662 wiped = Wipe_Data_Without_Wiping_Media();
Ethan Yonker5eac2222014-06-11 12:22:55 -05001663 recreate_media = false;
Dees_Troy16c2b312013-01-15 16:51:18 +00001664 } else {
Dees_Troy16c2b312013-01-15 16:51:18 +00001665 DataManager::GetValue(TW_RM_RF_VAR, check);
1666
Hashcodedabfd492013-08-29 22:45:30 -07001667 if (check || Use_Rm_Rf)
Dees_Troy16c2b312013-01-15 16:51:18 +00001668 wiped = Wipe_RMRF();
1669 else if (New_File_System == "ext4")
1670 wiped = Wipe_EXT4();
1671 else if (New_File_System == "ext2" || New_File_System == "ext3")
dianlujitao4879b372018-12-03 18:45:47 +08001672 wiped = Wipe_EXTFS(New_File_System);
Dees_Troy16c2b312013-01-15 16:51:18 +00001673 else if (New_File_System == "vfat")
1674 wiped = Wipe_FAT();
1675 else if (New_File_System == "exfat")
1676 wiped = Wipe_EXFAT();
1677 else if (New_File_System == "yaffs2")
1678 wiped = Wipe_MTD();
Dees_Troye5017042013-08-29 16:38:55 +00001679 else if (New_File_System == "f2fs")
1680 wiped = Wipe_F2FS();
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001681 else if (New_File_System == "ntfs")
1682 wiped = Wipe_NTFS();
Dees_Troy16c2b312013-01-15 16:51:18 +00001683 else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001684 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 +00001685 unlink("/.layout_version");
1686 return false;
1687 }
1688 update_crypt = wiped;
Gary Pecke8bc5d72012-12-21 06:45:25 -08001689 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001690
Gary Pecke8bc5d72012-12-21 06:45:25 -08001691 if (wiped) {
Dees_Troy1c1ac442013-01-17 21:42:14 +00001692 if (Mount_Point == "/cache")
1693 DataManager::Output_Version();
1694
Dees_Troy16c2b312013-01-15 16:51:18 +00001695 if (TWFunc::Path_Exists("/.layout_version") && Mount(false))
1696 TWFunc::copy_file("/.layout_version", Layout_Filename, 0600);
1697
1698 if (update_crypt) {
1699 Setup_File_System(false);
1700 if (Is_Encrypted && !Is_Decrypted) {
1701 // just wiped an encrypted partition back to its unencrypted state
1702 Is_Encrypted = false;
1703 Is_Decrypted = false;
1704 Decrypted_Block_Device = "";
1705 if (Mount_Point == "/data") {
1706 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1707 DataManager::SetValue(TW_IS_DECRYPTED, 0);
1708 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001709 }
1710 }
Ethan Yonker5eac2222014-06-11 12:22:55 -05001711
Ethan Yonker66a19492015-12-10 10:19:45 -06001712 if (Has_Data_Media && recreate_media) {
Ethan Yonker5eac2222014-06-11 12:22:55 -05001713 Recreate_Media_Folder();
1714 }
Matt Mower209c9632016-01-20 12:08:35 -06001715 if (Is_Storage && Mount(false))
1716 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Gary Pecke8bc5d72012-12-21 06:45:25 -08001717 }
Matt Mower209c9632016-01-20 12:08:35 -06001718
Gary Pecke8bc5d72012-12-21 06:45:25 -08001719 return wiped;
Dees_Troy51a0e822012-09-05 15:24:24 -04001720}
1721
Gary Peck43acadf2012-11-21 21:19:01 -08001722bool TWPartition::Wipe() {
Gary Peck82599a82012-11-21 16:23:12 -08001723 if (Is_File_System(Current_File_System))
1724 return Wipe(Current_File_System);
1725 else
1726 return Wipe(Fstab_File_System);
Gary Peck43acadf2012-11-21 21:19:01 -08001727}
1728
Dees_Troye58d5262012-09-21 12:27:57 -04001729bool TWPartition::Wipe_AndSec(void) {
1730 if (!Has_Android_Secure)
1731 return false;
1732
Dees_Troye58d5262012-09-21 12:27:57 -04001733 if (!Mount(true))
1734 return false;
1735
Ethan Yonker74db1572015-10-28 12:44:49 -05001736 gui_msg(Msg("wiping=Wiping {1}")(Backup_Display_Name));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001737 TWFunc::removeDir(Mount_Point + "/.android_secure/", true);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001738 return true;
Dees_Troye58d5262012-09-21 12:27:57 -04001739}
1740
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001741bool TWPartition::Can_Repair() {
Ethan Yonkera2719152015-05-28 09:44:41 -05001742 if (Mount_Read_Only)
1743 return false;
Matt Mower18794c82015-11-11 16:22:45 -06001744 if (Current_File_System == "vfat" && TWFunc::Path_Exists("/sbin/fsck.fat"))
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001745 return true;
1746 else if ((Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") && TWFunc::Path_Exists("/sbin/e2fsck"))
1747 return true;
1748 else if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/fsck.exfat"))
1749 return true;
1750 else if (Current_File_System == "f2fs" && TWFunc::Path_Exists("/sbin/fsck.f2fs"))
1751 return true;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001752 else if (Current_File_System == "ntfs" && (TWFunc::Path_Exists("/sbin/ntfsfix") || TWFunc::Path_Exists("/sbin/fsck.ntfs")))
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001753 return true;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001754 return false;
1755}
1756
1757bool TWPartition::Repair() {
1758 string command;
1759
1760 if (Current_File_System == "vfat") {
Matt Mower18794c82015-11-11 16:22:45 -06001761 if (!TWFunc::Path_Exists("/sbin/fsck.fat")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001762 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.fat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001763 return false;
1764 }
1765 if (!UnMount(true))
1766 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001767 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.fat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001768 Find_Actual_Block_Device();
Matt Mower18794c82015-11-11 16:22:45 -06001769 command = "/sbin/fsck.fat -y " + Actual_Block_Device;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001770 LOGINFO("Repair command: %s\n", command.c_str());
1771 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001772 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001773 return true;
1774 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001775 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001776 return false;
1777 }
1778 }
1779 if (Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") {
1780 if (!TWFunc::Path_Exists("/sbin/e2fsck")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001781 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("e2fsck"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001782 return false;
1783 }
1784 if (!UnMount(true))
1785 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001786 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("e2fsck"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001787 Find_Actual_Block_Device();
Ethan Yonkera2719152015-05-28 09:44:41 -05001788 command = "/sbin/e2fsck -fp " + Actual_Block_Device;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001789 LOGINFO("Repair command: %s\n", command.c_str());
1790 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001791 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001792 return true;
1793 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001794 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001795 return false;
1796 }
1797 }
1798 if (Current_File_System == "exfat") {
1799 if (!TWFunc::Path_Exists("/sbin/fsck.exfat")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001800 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.exfat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001801 return false;
1802 }
1803 if (!UnMount(true))
1804 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001805 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.exfat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001806 Find_Actual_Block_Device();
1807 command = "/sbin/fsck.exfat " + Actual_Block_Device;
1808 LOGINFO("Repair command: %s\n", command.c_str());
1809 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001810 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001811 return true;
1812 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001813 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001814 return false;
1815 }
1816 }
1817 if (Current_File_System == "f2fs") {
1818 if (!TWFunc::Path_Exists("/sbin/fsck.f2fs")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001819 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.f2fs"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001820 return false;
1821 }
1822 if (!UnMount(true))
1823 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001824 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.f2fs"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001825 Find_Actual_Block_Device();
1826 command = "/sbin/fsck.f2fs " + Actual_Block_Device;
1827 LOGINFO("Repair command: %s\n", command.c_str());
1828 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001829 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001830 return true;
1831 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001832 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001833 return false;
1834 }
1835 }
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001836 if (Current_File_System == "ntfs") {
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001837 string Ntfsfix_Binary;
1838 if (TWFunc::Path_Exists("/sbin/ntfsfix"))
1839 Ntfsfix_Binary = "ntfsfix";
1840 else if (TWFunc::Path_Exists("/sbin/fsck.ntfs"))
1841 Ntfsfix_Binary = "fsck.ntfs";
1842 else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001843 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("ntfsfix"));
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001844 return false;
1845 }
1846 if (!UnMount(true))
1847 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001848 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)(Ntfsfix_Binary));
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001849 Find_Actual_Block_Device();
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001850 command = "/sbin/" + Ntfsfix_Binary + " " + Actual_Block_Device;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001851 LOGINFO("Repair command: %s\n", command.c_str());
1852 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001853 gui_msg("done=Done.");
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001854 return true;
1855 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001856 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001857 return false;
1858 }
1859 }
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001860 return false;
1861}
1862
Ethan Yonkera2719152015-05-28 09:44:41 -05001863bool TWPartition::Can_Resize() {
1864 if (Mount_Read_Only)
1865 return false;
1866 if ((Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") && TWFunc::Path_Exists("/sbin/resize2fs"))
1867 return true;
1868 return false;
1869}
1870
1871bool TWPartition::Resize() {
1872 string command;
1873
1874 if (Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") {
1875 if (!Can_Repair()) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001876 LOGINFO("Cannot resize %s because %s cannot be repaired before resizing.\n", Display_Name.c_str(), Display_Name.c_str());
1877 gui_msg(Msg(msg::kError, "cannot_resize=Cannot resize {1}.")(Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001878 return false;
1879 }
1880 if (!TWFunc::Path_Exists("/sbin/resize2fs")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001881 LOGINFO("resize2fs does not exist! Cannot resize!\n");
1882 gui_msg(Msg(msg::kError, "cannot_resize=Cannot resize {1}.")(Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001883 return false;
1884 }
1885 // Repair will unmount so no need to do it twice
Ethan Yonker74db1572015-10-28 12:44:49 -05001886 gui_msg(Msg("repair_resize=Repairing {1} before resizing.")( Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001887 if (!Repair())
1888 return false;
Ethan Yonker74db1572015-10-28 12:44:49 -05001889 gui_msg(Msg("resizing=Resizing {1} using {2}...")(Display_Name)("resize2fs"));
Ethan Yonkera2719152015-05-28 09:44:41 -05001890 Find_Actual_Block_Device();
1891 command = "/sbin/resize2fs " + Actual_Block_Device;
1892 if (Length != 0) {
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001893 unsigned long long Actual_Size = IOCTL_Get_Block_Size();
1894 if (Actual_Size == 0)
1895 return false;
Ethan Yonkera2719152015-05-28 09:44:41 -05001896
Ethan Yonkera2719152015-05-28 09:44:41 -05001897 unsigned long long Block_Count;
1898 if (Length < 0) {
1899 // Reduce overall size by this length
1900 Block_Count = (Actual_Size / 1024LLU) - ((unsigned long long)(Length * -1) / 1024LLU);
1901 } else {
1902 // This is the size, not a size reduction
1903 Block_Count = ((unsigned long long)(Length) / 1024LLU);
1904 }
1905 char temp[256];
1906 sprintf(temp, "%llu", Block_Count);
1907 command += " ";
1908 command += temp;
1909 command += "K";
1910 }
1911 LOGINFO("Resize command: %s\n", command.c_str());
1912 if (TWFunc::Exec_Cmd(command) == 0) {
1913 Update_Size(true);
Ethan Yonker74db1572015-10-28 12:44:49 -05001914 gui_msg("done=Done.");
Ethan Yonkera2719152015-05-28 09:44:41 -05001915 return true;
1916 } else {
1917 Update_Size(true);
Ethan Yonker74db1572015-10-28 12:44:49 -05001918 gui_msg(Msg(msg::kError, "unable_resize=Unable to resize {1}.")(Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001919 return false;
1920 }
1921 }
1922 return false;
1923}
1924
bigbiffce8f83c2015-12-12 18:30:21 -05001925bool TWPartition::Backup(PartitionSettings *part_settings, pid_t *tar_fork_pid) {
1926 if (Backup_Method == BM_FILES)
1927 return Backup_Tar(part_settings, tar_fork_pid);
1928 else if (Backup_Method == BM_DD)
1929 return Backup_Image(part_settings);
1930 else if (Backup_Method == BM_FLASH_UTILS)
1931 return Backup_Dump_Image(part_settings);
Dees_Troy2673cec2013-04-02 20:22:16 +00001932 LOGERR("Unknown backup method for '%s'\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001933 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001934}
1935
bigbiffce8f83c2015-12-12 18:30:21 -05001936bool TWPartition::Restore(PartitionSettings *part_settings) {
Matt Mower3c366972015-12-25 19:28:31 -06001937 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, gui_parse_text("{@restoring_hdr}"));
Ethan Yonkere080c1f2016-09-19 13:50:25 -05001938 LOGINFO("Restore filename is: %s/%s\n", part_settings->Backup_Folder.c_str(), Backup_FileName.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001939
bigbiffce8f83c2015-12-12 18:30:21 -05001940 string Restore_File_System = Get_Restore_File_System(part_settings);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001941
1942 if (Is_File_System(Restore_File_System))
bigbiffce8f83c2015-12-12 18:30:21 -05001943 return Restore_Tar(part_settings);
1944 else if (Is_Image(Restore_File_System))
1945 return Restore_Image(part_settings);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001946
1947 LOGERR("Unknown restore method for '%s'\n", Mount_Point.c_str());
1948 return false;
1949}
1950
bigbiffce8f83c2015-12-12 18:30:21 -05001951string TWPartition::Get_Restore_File_System(PartitionSettings *part_settings) {
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001952 size_t first_period, second_period;
1953 string Restore_File_System;
1954
Gary Peck43acadf2012-11-21 21:19:01 -08001955 // Parse backup filename to extract the file system before wiping
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05001956 first_period = Backup_FileName.find(".");
Gary Peck43acadf2012-11-21 21:19:01 -08001957 if (first_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001958 LOGERR("Unable to find file system (first period).\n");
thatd43bf2d2014-09-21 23:13:02 +02001959 return string();
Gary Peck43acadf2012-11-21 21:19:01 -08001960 }
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05001961 Restore_File_System = Backup_FileName.substr(first_period + 1, Backup_FileName.size() - first_period - 1);
Gary Peck43acadf2012-11-21 21:19:01 -08001962 second_period = Restore_File_System.find(".");
1963 if (second_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001964 LOGERR("Unable to find file system (second period).\n");
thatd43bf2d2014-09-21 23:13:02 +02001965 return string();
Gary Peck43acadf2012-11-21 21:19:01 -08001966 }
1967 Restore_File_System.resize(second_period);
Matt Mowera0cd91d2016-12-30 18:21:42 -06001968 LOGINFO("Restore file system is: '%s'.\n", Restore_File_System.c_str());
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001969 return Restore_File_System;
Dees_Troy51a0e822012-09-05 15:24:24 -04001970}
1971
1972string TWPartition::Backup_Method_By_Name() {
bigbiffce8f83c2015-12-12 18:30:21 -05001973 if (Backup_Method == BM_NONE)
Dees_Troy38bd7602012-09-14 13:33:53 -04001974 return "none";
bigbiffce8f83c2015-12-12 18:30:21 -05001975 else if (Backup_Method == BM_FILES)
Dees_Troy38bd7602012-09-14 13:33:53 -04001976 return "files";
bigbiffce8f83c2015-12-12 18:30:21 -05001977 else if (Backup_Method == BM_DD)
Dees_Troy38bd7602012-09-14 13:33:53 -04001978 return "dd";
bigbiffce8f83c2015-12-12 18:30:21 -05001979 else if (Backup_Method == BM_FLASH_UTILS)
Dees_Troy38bd7602012-09-14 13:33:53 -04001980 return "flash_utils";
1981 else
1982 return "undefined";
1983 return "ERROR!";
Dees_Troy51a0e822012-09-05 15:24:24 -04001984}
1985
1986bool TWPartition::Decrypt(string Password) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001987 LOGINFO("STUB TWPartition::Decrypt, password: '%s'\n", Password.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001988 // Is this needed?
Dees_Troy51a0e822012-09-05 15:24:24 -04001989 return 1;
1990}
1991
1992bool TWPartition::Wipe_Encryption() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001993 bool Save_Data_Media = Has_Data_Media;
Ethan Yonker93382822018-11-01 15:25:31 -05001994 bool ret = false;
1995 BasePartition* base_partition = make_partition();
1996
1997 if (!base_partition->PreWipeEncryption())
1998 goto exit;
Dees_Troy38bd7602012-09-14 13:33:53 -04001999
dianlujitao4879b372018-12-03 18:45:47 +08002000 Find_Actual_Block_Device();
2001 if (!Is_Present) {
2002 LOGINFO("Block device not present, cannot format %s.\n", Display_Name.c_str());
2003 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
2004 return false;
2005 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002006 if (!UnMount(true))
Ethan Yonker93382822018-11-01 15:25:31 -05002007 goto exit;
Dees_Troy38bd7602012-09-14 13:33:53 -04002008
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06002009#ifdef TW_INCLUDE_CRYPTO
Ethan Yonker1b190162016-12-05 15:25:19 -06002010 if (Is_Decrypted && !Decrypted_Block_Device.empty()) {
Matt Mower2b18a532015-02-20 16:58:05 -06002011 if (delete_crypto_blk_dev((char*)("userdata")) != 0) {
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06002012 LOGERR("Error deleting crypto block device, continuing anyway.\n");
2013 }
2014 }
2015#endif
dianlujitao4879b372018-12-03 18:45:47 +08002016 Has_Data_Media = false;
Ethan Yonker93382822018-11-01 15:25:31 -05002017 Decrypted_Block_Device = "";
Dees_Troy74fb2e92013-04-15 14:35:47 +00002018 Is_Decrypted = false;
2019 Is_Encrypted = false;
Gary Pecke8bc5d72012-12-21 06:45:25 -08002020 if (Wipe(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002021 Has_Data_Media = Save_Data_Media;
2022 if (Has_Data_Media && !Symlink_Mount_Point.empty()) {
2023 Recreate_Media_Folder();
Ethan Yonker726a0202014-12-16 20:01:38 -06002024 if (Mount(false))
2025 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04002026 }
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06002027 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
Ethan Yonker83e82572014-04-04 10:59:28 -05002028#ifndef TW_OEM_BUILD
Ethan Yonker74db1572015-10-28 12:44:49 -05002029 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 -05002030#endif
Ethan Yonker93382822018-11-01 15:25:31 -05002031 ret = true;
2032 if (!Key_Directory.empty())
2033 ret = PartitionManager.Wipe_By_Path(Key_Directory);
2034 if (ret)
2035 ret = base_partition->PostWipeEncryption();
2036 goto exit;
Dees_Troy38bd7602012-09-14 13:33:53 -04002037 } else {
2038 Has_Data_Media = Save_Data_Media;
Ethan Yonker74db1572015-10-28 12:44:49 -05002039 gui_err("format_data_err=Unable to format to remove encryption.");
Ethan Yonker726a0202014-12-16 20:01:38 -06002040 if (Has_Data_Media && Mount(false))
2041 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Ethan Yonker93382822018-11-01 15:25:31 -05002042 goto exit;
Dees_Troy38bd7602012-09-14 13:33:53 -04002043 }
Ethan Yonker93382822018-11-01 15:25:31 -05002044exit:
2045 delete base_partition;
2046 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04002047}
2048
2049void TWPartition::Check_FS_Type() {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05002050 const char* type;
2051 blkid_probe pr;
Dees_Troy5bf43922012-09-07 16:07:55 -04002052
Dees_Troy68cab492012-12-12 19:29:35 +00002053 if (Fstab_File_System == "yaffs2" || Fstab_File_System == "mtd" || Fstab_File_System == "bml" || Ignore_Blkid)
2054 return; // Running blkid on some mtd devices causes a massive crash or needs to be skipped
Dees_Troy5bf43922012-09-07 16:07:55 -04002055
Dees_Troy38bd7602012-09-14 13:33:53 -04002056 Find_Actual_Block_Device();
Dees_Troy8170a922012-09-18 15:40:25 -04002057 if (!Is_Present)
2058 return;
Dees_Troy51127312012-09-08 13:08:49 -04002059
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05002060 pr = blkid_new_probe_from_filename(Actual_Block_Device.c_str());
2061 if (blkid_do_fullprobe(pr)) {
2062 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00002063 LOGINFO("Can't probe device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05002064 return;
Dees_Troy5bf43922012-09-07 16:07:55 -04002065 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02002066
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05002067 if (blkid_probe_lookup_value(pr, "TYPE", &type, NULL) < 0) {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05002068 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00002069 LOGINFO("can't find filesystem on device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05002070 return;
2071 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02002072
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05002073 Current_File_System = type;
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02002074 blkid_free_probe(pr);
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06002075 if (fs_flags.size() > 1) {
2076 std::vector<partition_fs_flags_struct>::iterator iter;
2077 std::vector<partition_fs_flags_struct>::iterator found = fs_flags.begin();
2078
2079 for (iter = fs_flags.begin(); iter != fs_flags.end(); iter++) {
2080 if (iter->File_System == Current_File_System) {
2081 found = iter;
2082 break;
2083 }
2084 }
2085 // If we don't find a match, we default the flags to the first set of flags that we received from the fstab
2086 if (Mount_Flags != found->Mount_Flags || Mount_Options != found->Mount_Options) {
2087 Mount_Flags = found->Mount_Flags;
2088 Mount_Options = found->Mount_Options;
2089 LOGINFO("Mount_Flags: %i, Mount_Options: %s\n", Mount_Flags, Mount_Options.c_str());
2090 }
2091 }
Dees_Troy51a0e822012-09-05 15:24:24 -04002092}
2093
dianlujitao4879b372018-12-03 18:45:47 +08002094bool TWPartition::Wipe_EXTFS(string File_System) {
2095#if PLATFORM_SDK_VERSION < 28
2096 if (!TWFunc::Path_Exists("/sbin/mke2fs"))
2097#else
2098 if (!TWFunc::Path_Exists("/sbin/mke2fs") || !TWFunc::Path_Exists("/sbin/e2fsdroid"))
2099#endif
Dees_Troy38bd7602012-09-14 13:33:53 -04002100 return Wipe_RMRF();
2101
dianlujitao4879b372018-12-03 18:45:47 +08002102 int ret;
2103 bool NeedPreserveFooter = true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002104
Ethan Yonker25f20c12014-10-14 09:04:43 -05002105 Find_Actual_Block_Device();
2106 if (!Is_Present) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002107 LOGINFO("Block device not present, cannot wipe %s.\n", Display_Name.c_str());
2108 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Ethan Yonker25f20c12014-10-14 09:04:43 -05002109 return false;
2110 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002111 if (!UnMount(true))
2112 return false;
2113
dianlujitao4879b372018-12-03 18:45:47 +08002114 /**
2115 * On decrypted devices, IOCTL_Get_Block_Size calculates size on device mapper,
2116 * so there's no need to preserve footer.
2117 */
2118 if ((Is_Decrypted && !Decrypted_Block_Device.empty()) ||
2119 Crypto_Key_Location != "footer") {
2120 NeedPreserveFooter = false;
2121 }
2122
2123 unsigned long long dev_sz = TWFunc::IOCTL_Get_Block_Size(Actual_Block_Device.c_str());
2124 if (!dev_sz)
2125 return false;
2126
2127 if (NeedPreserveFooter)
2128 Length < 0 ? dev_sz += Length : dev_sz -= CRYPT_FOOTER_OFFSET;
2129
Dees Troy9a4d7402019-03-21 14:17:28 -04002130 char dout[16];
2131 sprintf(dout, "%llu", dev_sz / 4096);
2132
2133 //string size_str =to_string(dev_sz / 4096);
2134 string size_str = dout;
dianlujitao4879b372018-12-03 18:45:47 +08002135 string Command;
2136
2137 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mke2fs"));
2138
2139 // Execute mke2fs to create empty ext4 filesystem
2140 Command = "mke2fs -t " + File_System + " -b 4096 " + Actual_Block_Device + " " + size_str;
2141 LOGINFO("mke2fs command: %s\n", Command.c_str());
2142 ret = TWFunc::Exec_Cmd(Command);
2143 if (ret) {
2144 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
2145 return false;
2146 }
2147
2148 if (TWFunc::Path_Exists("/sbin/e2fsdroid")) {
dianlujitao3cf05a52019-05-15 15:33:27 +08002149 const string& File_Contexts_Entry = (Mount_Point == "/system_root" ? "/" : Mount_Point);
2150 char *secontext = NULL;
2151 if (!selinux_handle || selabel_lookup(selinux_handle, &secontext, File_Contexts_Entry.c_str(), S_IFDIR) < 0) {
2152 LOGINFO("Cannot lookup security context for '%s'\n", Mount_Point.c_str());
2153 } else {
2154 // Execute e2fsdroid to initialize selinux context
2155 Command = "e2fsdroid -e -S /file_contexts -a " + File_Contexts_Entry + " " + Actual_Block_Device;
2156 LOGINFO("e2fsdroid command: %s\n", Command.c_str());
2157 ret = TWFunc::Exec_Cmd(Command);
2158 if (ret) {
2159 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
2160 return false;
2161 }
dianlujitao4879b372018-12-03 18:45:47 +08002162 }
2163 } else {
2164 LOGINFO("e2fsdroid not present\n");
2165 }
2166
2167 if (NeedPreserveFooter)
2168 Wipe_Crypto_Key();
2169 Current_File_System = File_System;
2170 Recreate_AndSec_Folder();
2171 gui_msg("done=Done.");
2172 return true;
2173}
2174
2175bool TWPartition::Wipe_EXT4() {
2176#ifdef USE_EXT4
Ethan Yonkerf27497f2014-02-09 11:48:33 -06002177 int ret;
dianlujitao4879b372018-12-03 18:45:47 +08002178 bool NeedPreserveFooter = true;
2179
2180 Find_Actual_Block_Device();
2181 if (!Is_Present) {
2182 LOGINFO("Block device not present, cannot wipe %s.\n", Display_Name.c_str());
2183 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
2184 return false;
2185 }
2186 if (!UnMount(true))
2187 return false;
2188
2189 /**
2190 * On decrypted devices, IOCTL_Get_Block_Size calculates size on device mapper,
2191 * so there's no need to preserve footer.
2192 */
2193 if ((Is_Decrypted && !Decrypted_Block_Device.empty()) ||
2194 Crypto_Key_Location != "footer") {
2195 NeedPreserveFooter = false;
2196 }
2197
2198 unsigned long long dev_sz = TWFunc::IOCTL_Get_Block_Size(Actual_Block_Device.c_str());
2199 if (!dev_sz)
2200 return false;
2201
2202 if (NeedPreserveFooter)
2203 Length < 0 ? dev_sz += Length : dev_sz -= CRYPT_FOOTER_OFFSET;
2204
Ethan Yonkerf27497f2014-02-09 11:48:33 -06002205 char *secontext = NULL;
2206
Greg Wallace4b44fef2015-12-29 15:33:24 -05002207 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("make_ext4fs"));
Ethan Yonkerf27497f2014-02-09 11:48:33 -06002208
Dees Troy99c8dbf2014-03-10 16:53:58 +00002209 if (!selinux_handle || selabel_lookup(selinux_handle, &secontext, Mount_Point.c_str(), S_IFDIR) < 0) {
Ethan Yonkerf27497f2014-02-09 11:48:33 -06002210 LOGINFO("Cannot lookup security context for '%s'\n", Mount_Point.c_str());
dianlujitao4879b372018-12-03 18:45:47 +08002211 ret = make_ext4fs(Actual_Block_Device.c_str(), dev_sz, Mount_Point.c_str(), NULL);
Ethan Yonkerf27497f2014-02-09 11:48:33 -06002212 } else {
dianlujitao4879b372018-12-03 18:45:47 +08002213 ret = make_ext4fs(Actual_Block_Device.c_str(), dev_sz, Mount_Point.c_str(), selinux_handle);
Ethan Yonkerf27497f2014-02-09 11:48:33 -06002214 }
2215 if (ret != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002216 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troya95f55c2013-08-17 13:14:43 +00002217 return false;
2218 } else {
dianlujitao4879b372018-12-03 18:45:47 +08002219 if (NeedPreserveFooter)
2220 Wipe_Crypto_Key();
bigbiff bigbiffc49d7062013-10-11 20:28:00 -04002221 string sedir = Mount_Point + "/lost+found";
2222 PartitionManager.Mount_By_Path(sedir.c_str(), true);
2223 rmdir(sedir.c_str());
2224 mkdir(sedir.c_str(), S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP);
Dees_Troya95f55c2013-08-17 13:14:43 +00002225 return true;
2226 }
2227#else
dianlujitao4879b372018-12-03 18:45:47 +08002228 return Wipe_EXTFS("ext4");
Dees_Troya95f55c2013-08-17 13:14:43 +00002229#endif
Dees_Troy51a0e822012-09-05 15:24:24 -04002230}
2231
2232bool TWPartition::Wipe_FAT() {
Vojtech Bocek05534202013-09-11 08:11:56 +02002233 string command;
Dees_Troy38bd7602012-09-14 13:33:53 -04002234
Matt Mower18794c82015-11-11 16:22:45 -06002235 if (TWFunc::Path_Exists("/sbin/mkfs.fat")) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002236 if (!UnMount(true))
2237 return false;
2238
Greg Wallace4b44fef2015-12-29 15:33:24 -05002239 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mkfs.fat"));
Dees_Troy38bd7602012-09-14 13:33:53 -04002240 Find_Actual_Block_Device();
Matt Mower18794c82015-11-11 16:22:45 -06002241 command = "mkfs.fat " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02002242 if (TWFunc::Exec_Cmd(command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08002243 Current_File_System = "vfat";
Dees_Troye58d5262012-09-21 12:27:57 -04002244 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05002245 gui_msg("done=Done.");
Dees_Troy38bd7602012-09-14 13:33:53 -04002246 return true;
2247 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002248 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04002249 return false;
2250 }
2251 return true;
2252 }
2253 else
2254 return Wipe_RMRF();
2255
2256 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04002257}
2258
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002259bool TWPartition::Wipe_EXFAT() {
Vojtech Bocek05534202013-09-11 08:11:56 +02002260 string command;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002261
2262 if (TWFunc::Path_Exists("/sbin/mkexfatfs")) {
2263 if (!UnMount(true))
2264 return false;
2265
Greg Wallace4b44fef2015-12-29 15:33:24 -05002266 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mkexfatfs"));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002267 Find_Actual_Block_Device();
2268 command = "mkexfatfs " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02002269 if (TWFunc::Exec_Cmd(command) == 0) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002270 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05002271 gui_msg("done=Done.");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002272 return true;
2273 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002274 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002275 return false;
2276 }
2277 return true;
2278 }
2279 return false;
2280}
2281
Dees_Troy38bd7602012-09-14 13:33:53 -04002282bool TWPartition::Wipe_MTD() {
2283 if (!UnMount(true))
2284 return false;
2285
Greg Wallace4b44fef2015-12-29 15:33:24 -05002286 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("MTD"));
Dees_Troy38bd7602012-09-14 13:33:53 -04002287
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02002288 mtd_scan_partitions();
2289 const MtdPartition* mtd = mtd_find_partition_by_name(MTD_Name.c_str());
2290 if (mtd == NULL) {
2291 LOGERR("No mtd partition named '%s'", MTD_Name.c_str());
2292 return false;
2293 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002294
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02002295 MtdWriteContext* ctx = mtd_write_partition(mtd);
2296 if (ctx == NULL) {
2297 LOGERR("Can't write '%s', failed to format.", MTD_Name.c_str());
2298 return false;
2299 }
2300 if (mtd_erase_blocks(ctx, -1) == -1) {
2301 mtd_write_close(ctx);
2302 LOGERR("Failed to format '%s'", MTD_Name.c_str());
2303 return false;
2304 }
2305 if (mtd_write_close(ctx) != 0) {
2306 LOGERR("Failed to close '%s'", MTD_Name.c_str());
2307 return false;
2308 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08002309 Current_File_System = "yaffs2";
Dees_Troye58d5262012-09-21 12:27:57 -04002310 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05002311 gui_msg("done=Done.");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02002312 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002313}
2314
2315bool TWPartition::Wipe_RMRF() {
Dees_Troy38bd7602012-09-14 13:33:53 -04002316 if (!Mount(true))
2317 return false;
Ethan Yonker726a0202014-12-16 20:01:38 -06002318 // This is the only wipe that leaves the partition mounted, so we
2319 // must manually remove the partition from MTP if it is a storage
2320 // partition.
2321 if (Is_Storage)
2322 PartitionManager.Remove_MTP_Storage(MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04002323
Ethan Yonker74db1572015-10-28 12:44:49 -05002324 gui_msg(Msg("remove_all=Removing all files under '{1}'")(Mount_Point));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002325 TWFunc::removeDir(Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04002326 Recreate_AndSec_Folder();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002327 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002328}
2329
Dees_Troye5017042013-08-29 16:38:55 +00002330bool TWPartition::Wipe_F2FS() {
Vojtech Bocek05534202013-09-11 08:11:56 +02002331 string command;
Dees_Troye5017042013-08-29 16:38:55 +00002332
2333 if (TWFunc::Path_Exists("/sbin/mkfs.f2fs")) {
dianlujitao4879b372018-12-03 18:45:47 +08002334 bool NeedPreserveFooter = true;
2335
2336 Find_Actual_Block_Device();
2337 if (!Is_Present) {
2338 LOGINFO("Block device not present, cannot wipe %s.\n", Display_Name.c_str());
2339 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
2340 return false;
2341 }
Dees_Troye5017042013-08-29 16:38:55 +00002342 if (!UnMount(true))
2343 return false;
2344
dianlujitao4879b372018-12-03 18:45:47 +08002345 /**
2346 * On decrypted devices, IOCTL_Get_Block_Size calculates size on device mapper,
2347 * so there's no need to preserve footer.
2348 */
2349 if ((Is_Decrypted && !Decrypted_Block_Device.empty()) ||
2350 Crypto_Key_Location != "footer") {
2351 NeedPreserveFooter = false;
dhacker29a3fa75f2015-01-17 19:42:33 -05002352 }
dianlujitao4879b372018-12-03 18:45:47 +08002353
dianlujitao4879b372018-12-03 18:45:47 +08002354 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mkfs.f2fs"));
Ethan Yonker7e941582019-03-22 08:18:21 -05002355 // First determine if we have the old mkfs.f2fs that uses "-r reserved_bytes"
2356 // or the new mkfs.f2fs that expects the number of sectors as the optional last argument
2357 // Note: some 7.1 trees have the old and some have the new.
2358 command = "mkfs.f2fs | grep \"reserved\" > /tmp/f2fsversiontest";
2359 TWFunc::Exec_Cmd(command, false); // no help argument so printing usage exits with an error code
2360 if (!TWFunc::Path_Exists("/tmp/f2fsversiontest")) {
2361 LOGINFO("Error determining mkfs.f2fs version\n");
2362 return false;
2363 }
2364 if (TWFunc::Get_File_Size("/tmp/f2fsversiontest") <= 0) {
2365 LOGINFO("Using newer mkfs.f2fs\n");
2366 unsigned long long dev_sz = TWFunc::IOCTL_Get_Block_Size(Actual_Block_Device.c_str());
2367 if (!dev_sz)
2368 return false;
2369
2370 if (NeedPreserveFooter)
2371 Length < 0 ? dev_sz += Length : dev_sz -= CRYPT_FOOTER_OFFSET;
2372
2373 char dev_sz_str[48];
2374 sprintf(dev_sz_str, "%llu", (dev_sz / 4096));
2375 command = "mkfs.f2fs -d1 -f -O encrypt -O quota -O verity -w 4096 " + Actual_Block_Device + " " + dev_sz_str;
2376 if (TWFunc::Path_Exists("/sbin/sload.f2fs")) {
2377 command += " && sload.f2fs -t /data " + Actual_Block_Device;
2378 }
2379 } else {
2380 LOGINFO("Using older mkfs.f2fs\n");
2381 command = "mkfs.f2fs -t 0";
2382 if (NeedPreserveFooter) {
2383 // Only use length if we're not decrypted
2384 char len[32];
2385 int mod_length = Length;
2386 if (Length < 0)
2387 mod_length *= -1;
2388 sprintf(len, "%i", mod_length);
2389 command += " -r ";
2390 command += len;
2391 }
2392 command += " " + Actual_Block_Device;
dianlujitao4879b372018-12-03 18:45:47 +08002393 }
2394 LOGINFO("mkfs.f2fs command: %s\n", command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02002395 if (TWFunc::Exec_Cmd(command) == 0) {
dianlujitao4879b372018-12-03 18:45:47 +08002396 if (NeedPreserveFooter)
2397 Wipe_Crypto_Key();
Dees_Troye5017042013-08-29 16:38:55 +00002398 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05002399 gui_msg("done=Done.");
Dees_Troye5017042013-08-29 16:38:55 +00002400 return true;
2401 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002402 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troye5017042013-08-29 16:38:55 +00002403 return false;
2404 }
2405 return true;
2406 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002407 LOGINFO("mkfs.f2fs binary not found, using rm -rf to wipe.\n");
Dees_Troye5017042013-08-29 16:38:55 +00002408 return Wipe_RMRF();
2409 }
2410 return false;
2411}
2412
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002413bool TWPartition::Wipe_NTFS() {
2414 string command;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002415 string Ntfsmake_Binary;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002416
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002417 if (TWFunc::Path_Exists("/sbin/mkntfs"))
2418 Ntfsmake_Binary = "mkntfs";
2419 else if (TWFunc::Path_Exists("/sbin/mkfs.ntfs"))
2420 Ntfsmake_Binary = "mkfs.ntfs";
2421 else
2422 return false;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002423
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002424 if (!UnMount(true))
2425 return false;
2426
Greg Wallace4b44fef2015-12-29 15:33:24 -05002427 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)(Ntfsmake_Binary));
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002428 Find_Actual_Block_Device();
2429 command = "/sbin/" + Ntfsmake_Binary + " " + Actual_Block_Device;
2430 if (TWFunc::Exec_Cmd(command) == 0) {
2431 Recreate_AndSec_Folder();
2432 gui_msg("done=Done.");
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002433 return true;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002434 } else {
2435 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
2436 return false;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002437 }
2438 return false;
2439}
2440
Dees_Troy51a0e822012-09-05 15:24:24 -04002441bool TWPartition::Wipe_Data_Without_Wiping_Media() {
Ethan Yonker83e82572014-04-04 10:59:28 -05002442#ifdef TW_OEM_BUILD
2443 // In an OEM Build we want to do a full format
2444 return Wipe_Encryption();
2445#else
Ethan Yonker66a19492015-12-10 10:19:45 -06002446 bool ret = false;
Dees_Troy38bd7602012-09-14 13:33:53 -04002447
Dees_Troy38bd7602012-09-14 13:33:53 -04002448 if (!Mount(true))
2449 return false;
2450
Ethan Yonker74db1572015-10-28 12:44:49 -05002451 gui_msg("wiping_data=Wiping data without wiping /data/media ...");
Ethan Yonker66a19492015-12-10 10:19:45 -06002452 ret = Wipe_Data_Without_Wiping_Media_Func(Mount_Point + "/");
2453 if (ret)
2454 gui_msg("done=Done.");
2455 return ret;
2456#endif // ifdef TW_OEM_BUILD
2457}
2458
2459bool TWPartition::Wipe_Data_Without_Wiping_Media_Func(const string& parent __unused) {
2460 string dir;
Dees_Troy38bd7602012-09-14 13:33:53 -04002461
2462 DIR* d;
Ethan Yonker66a19492015-12-10 10:19:45 -06002463 d = opendir(parent.c_str());
Dees_Troy16b74352012-11-14 22:27:31 +00002464 if (d != NULL) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002465 struct dirent* de;
2466 while ((de = readdir(d)) != NULL) {
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002467 if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) continue;
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05002468
Ethan Yonker66a19492015-12-10 10:19:45 -06002469 dir = parent;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002470 dir.append(de->d_name);
Ethan Yonker3fdcda42016-11-30 12:29:37 -06002471 if (wipe_exclusions.check_skip_dirs(dir)) {
Ethan Yonker66a19492015-12-10 10:19:45 -06002472 LOGINFO("skipped '%s'\n", dir.c_str());
2473 continue;
2474 }
Dees_Troyce675462013-01-09 19:48:21 +00002475 if (de->d_type == DT_DIR) {
Ethan Yonker66a19492015-12-10 10:19:45 -06002476 dir.append("/");
2477 if (!Wipe_Data_Without_Wiping_Media_Func(dir)) {
2478 closedir(d);
2479 return false;
2480 }
2481 rmdir(dir.c_str());
bigbiff bigbiff98f1f902013-01-19 18:46:13 -05002482 } 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 +03002483 if (unlink(dir.c_str()) != 0)
Ethan Yonker79f88bd2016-12-09 14:52:12 -06002484 LOGINFO("Unable to unlink '%s': %s\n", dir.c_str(), strerror(errno));
Dees_Troyce675462013-01-09 19:48:21 +00002485 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002486 }
2487 closedir(d);
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05002488
Dees_Troy16b74352012-11-14 22:27:31 +00002489 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -04002490 }
Ethan Yonker74db1572015-10-28 12:44:49 -05002491 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy16b74352012-11-14 22:27:31 +00002492 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04002493}
2494
dianlujitao4879b372018-12-03 18:45:47 +08002495void TWPartition::Wipe_Crypto_Key() {
2496 Find_Actual_Block_Device();
2497 if (Crypto_Key_Location.empty())
2498 return;
2499 else if (Crypto_Key_Location == "footer") {
2500 int fd = open(Actual_Block_Device.c_str(), O_RDWR);
2501 if (fd < 0) {
2502 gui_print_color("warning", "Unable to open '%s' to wipe crypto key\n", Actual_Block_Device.c_str());
2503 return;
2504 }
2505
2506 unsigned int block_count;
2507 if ((ioctl(fd, BLKGETSIZE, &block_count)) == -1) {
2508 gui_print_color("warning", "Unable to get block size for wiping crypto footer.\n");
2509 } else {
2510 int newlen = Length < 0 ? -Length : CRYPT_FOOTER_OFFSET;
2511 off64_t offset = ((off64_t)block_count * 512) - newlen;
2512 if (lseek64(fd, offset, SEEK_SET) == -1) {
2513 gui_print_color("warning", "Unable to lseek64 for wiping crypto footer.\n");
2514 } else {
2515 void* buffer = malloc(newlen);
2516 if (!buffer) {
2517 gui_print_color("warning", "Failed to malloc for wiping crypto footer.\n");
2518 } else {
2519 memset(buffer, 0, newlen);
2520 int ret = write(fd, buffer, newlen);
2521 if (ret != newlen) {
2522 gui_print_color("warning", "Failed to wipe crypto footer.\n");
2523 } else {
2524 LOGINFO("Successfully wiped crypto footer.\n");
2525 }
2526 free(buffer);
2527 }
2528 }
2529 }
2530 close(fd);
2531 } else {
2532 if (TWFunc::IOCTL_Get_Block_Size(Crypto_Key_Location.c_str()) >= 16384LLU) {
2533 string Command = "dd of='" + Crypto_Key_Location + "' if=/dev/zero bs=16384 count=1";
2534 TWFunc::Exec_Cmd(Command);
2535 } else {
2536 LOGINFO("Crypto key location reports size < 16K so not wiping crypto footer.\n");
2537 }
2538 }
2539}
2540
bigbiffce8f83c2015-12-12 18:30:21 -05002541bool TWPartition::Backup_Tar(PartitionSettings *part_settings, pid_t *tar_fork_pid) {
Ethan Yonker472f5062016-02-25 13:47:30 -06002542 string Full_FileName;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002543 twrpTar tar;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05002544
Dees_Troy43d8b002012-09-17 16:00:01 -04002545 if (!Mount(true))
2546 return false;
2547
Ian Macdonalda3b25a32019-06-16 22:39:14 +02002548 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Backup_Display_Name, gui_parse_text("{@backing}"));
Ethan Yonker74db1572015-10-28 12:44:49 -05002549 gui_msg(Msg("backing_up=Backing up {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002550
Ethan Yonker472f5062016-02-25 13:47:30 -06002551 DataManager::GetValue(TW_USE_COMPRESSION_VAR, tar.use_compression);
Matt Mowerbb81e5d2014-03-20 18:05:41 -05002552
Dees_Troy83bd4832013-05-04 12:39:56 +00002553#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
Ethan Yonker472f5062016-02-25 13:47:30 -06002554 if (Can_Encrypt_Backup) {
2555 DataManager::GetValue("tw_encrypt_backup", tar.use_encryption);
2556 if (tar.use_encryption) {
2557 if (Use_Userdata_Encryption)
2558 tar.userdata_encryption = tar.use_encryption;
2559 string Password;
2560 DataManager::GetValue("tw_backup_password", Password);
2561 tar.setpassword(Password);
2562 } else {
2563 tar.use_encryption = 0;
2564 }
Dees_Troy83bd4832013-05-04 12:39:56 +00002565 }
2566#endif
Dees_Troy43d8b002012-09-17 16:00:01 -04002567
Ethan Yonker472f5062016-02-25 13:47:30 -06002568 Backup_FileName = Backup_Name + "." + Current_File_System + ".win";
Ethan Yonkere080c1f2016-09-19 13:50:25 -05002569 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
Ethan Yonker01f4e032017-02-03 15:30:52 -06002570 if (Has_Data_Media)
2571 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 Jacobson0b25b1d2019-04-28 00:10:07 -04002572 if (Mount_Point == "/data" && DataManager::GetIntValue(TW_IS_FBE)) {
2573 std::vector<users_struct>::iterator iter;
2574 std::vector<users_struct>* userList = PartitionManager.Get_Users_List();
2575 for (iter = userList->begin(); iter != userList->end(); iter++) {
2576 if (!(*iter).isDecrypted && (*iter).userId != "0") {
2577 gui_msg(Msg(msg::kWarning,
2578 "backup_storage_undecrypt_warning=Backup will not include some files from user {1} "
2579 "because the user is not decrypted.")((*iter).userId));
2580 backup_exclusions.add_absolute_dir("/data/system_ce/" + (*iter).userId);
2581 backup_exclusions.add_absolute_dir("/data/misc_ce/" + (*iter).userId);
2582 backup_exclusions.add_absolute_dir("/data/vendor_ce/" + (*iter).userId);
2583 backup_exclusions.add_absolute_dir("/data/media/" + (*iter).userId);
2584 backup_exclusions.add_absolute_dir("/data/user/" + (*iter).userId);
2585 }
2586 }
2587 }
bigbiffce8f83c2015-12-12 18:30:21 -05002588 tar.part_settings = part_settings;
Ethan Yonker3fdcda42016-11-30 12:29:37 -06002589 tar.backup_exclusions = &backup_exclusions;
Dees Troye0a433a2013-12-02 04:10:37 +00002590 tar.setdir(Backup_Path);
2591 tar.setfn(Full_FileName);
2592 tar.setsize(Backup_Size);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002593 tar.partition_name = Backup_Name;
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002594 tar.backup_folder = part_settings->Backup_Folder;
bigbiffce8f83c2015-12-12 18:30:21 -05002595 if (tar.createTarFork(tar_fork_pid) != 0)
Dees Troye0a433a2013-12-02 04:10:37 +00002596 return false;
Dees_Troy43d8b002012-09-17 16:00:01 -04002597 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002598}
2599
bigbiffce8f83c2015-12-12 18:30:21 -05002600bool TWPartition::Backup_Image(PartitionSettings *part_settings) {
2601 string Full_FileName, adb_file_name;
igoriok87e3d932013-01-31 21:03:53 +02002602
Ethan Yonker74db1572015-10-28 12:44:49 -05002603 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, gui_parse_text("{@backing}"));
2604 gui_msg(Msg("backing_up=Backing up {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002605
Ethan Yonker472f5062016-02-25 13:47:30 -06002606 Backup_FileName = Backup_Name + "." + Current_File_System + ".win";
Dees_Troy43d8b002012-09-17 16:00:01 -04002607
bigbiffce8f83c2015-12-12 18:30:21 -05002608 if (part_settings->adbbackup) {
2609 Full_FileName = TW_ADB_BACKUP;
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002610 adb_file_name = part_settings->Backup_Folder + "/" + Backup_FileName;
bigbiffce8f83c2015-12-12 18:30:21 -05002611 }
2612 else
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002613 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
bigbiffce8f83c2015-12-12 18:30:21 -05002614
2615 part_settings->total_restore_size = Backup_Size;
2616
2617 if (part_settings->adbbackup) {
2618 if (!twadbbu::Write_TWIMG(adb_file_name, Backup_Size))
2619 return false;
2620 }
2621
2622 if (!Raw_Read_Write(part_settings))
Ethan Yonker472f5062016-02-25 13:47:30 -06002623 return false;
2624
bigbiffce8f83c2015-12-12 18:30:21 -05002625 if (part_settings->adbbackup) {
2626 if (!twadbbu::Write_TWEOF())
2627 return false;
Dees_Troy7c2dec82012-09-26 09:49:14 -04002628 }
Dees_Troy43d8b002012-09-17 16:00:01 -04002629 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002630}
2631
bigbiffce8f83c2015-12-12 18:30:21 -05002632bool TWPartition::Raw_Read_Write(PartitionSettings *part_settings) {
2633 unsigned long long RW_Block_Size, Remain = Backup_Size;
2634 int src_fd = -1, dest_fd = -1;
2635 ssize_t bs;
Ethan Yonker472f5062016-02-25 13:47:30 -06002636 bool ret = false;
2637 void* buffer = NULL;
2638 unsigned long long backedup_size = 0;
bigbiffce8f83c2015-12-12 18:30:21 -05002639 string srcfn, destfn;
Ethan Yonker472f5062016-02-25 13:47:30 -06002640
bigbiffce8f83c2015-12-12 18:30:21 -05002641 if (part_settings->PM_Method == PM_BACKUP) {
2642 srcfn = Actual_Block_Device;
2643 if (part_settings->adbbackup)
2644 destfn = TW_ADB_BACKUP;
bigbiff bigbiff38b83c12017-12-28 19:58:52 -05002645 else {
Ethan Yonkere080c1f2016-09-19 13:50:25 -05002646 destfn = part_settings->Backup_Folder + "/" + Backup_FileName;
bigbiff bigbiff38b83c12017-12-28 19:58:52 -05002647 }
bigbiffce8f83c2015-12-12 18:30:21 -05002648 }
2649 else {
2650 destfn = Actual_Block_Device;
2651 if (part_settings->adbbackup) {
2652 srcfn = TW_ADB_RESTORE;
2653 } else {
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002654 srcfn = part_settings->Backup_Folder + "/" + Backup_FileName;
bigbiffce8f83c2015-12-12 18:30:21 -05002655 Remain = TWFunc::Get_File_Size(srcfn);
2656 }
2657 }
Ethan Yonker472f5062016-02-25 13:47:30 -06002658
bigbiffce8f83c2015-12-12 18:30:21 -05002659 src_fd = open(srcfn.c_str(), O_RDONLY | O_LARGEFILE);
Ethan Yonker472f5062016-02-25 13:47:30 -06002660 if (src_fd < 0) {
bigbiffce8f83c2015-12-12 18:30:21 -05002661 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(srcfn.c_str())(strerror(errno)));
Ethan Yonker472f5062016-02-25 13:47:30 -06002662 return false;
2663 }
bigbiffce8f83c2015-12-12 18:30:21 -05002664
2665 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 -06002666 if (dest_fd < 0) {
bigbiffce8f83c2015-12-12 18:30:21 -05002667 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(destfn.c_str())(strerror(errno)));
Ethan Yonker472f5062016-02-25 13:47:30 -06002668 goto exit;
2669 }
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -04002670
bigbiffce8f83c2015-12-12 18:30:21 -05002671 LOGINFO("Reading '%s', writing '%s'\n", srcfn.c_str(), destfn.c_str());
2672
2673 if (part_settings->adbbackup) {
2674 RW_Block_Size = MAX_ADB_READ;
2675 bs = MAX_ADB_READ;
2676 }
2677 else {
2678 RW_Block_Size = 1048576LLU; // 1MB
2679 bs = (ssize_t)(RW_Block_Size);
2680 }
2681
Ethan Yonker472f5062016-02-25 13:47:30 -06002682 buffer = malloc((size_t)bs);
2683 if (!buffer) {
2684 LOGINFO("Raw_Read_Write failed to malloc\n");
2685 goto exit;
2686 }
bigbiffce8f83c2015-12-12 18:30:21 -05002687
2688 if (part_settings->progress)
2689 part_settings->progress->SetPartitionSize(part_settings->total_restore_size);
2690
Ethan Yonker472f5062016-02-25 13:47:30 -06002691 while (Remain > 0) {
2692 if (Remain < RW_Block_Size)
bigbiffce8f83c2015-12-12 18:30:21 -05002693 bs = (ssize_t)(Remain);
Matt Mowera8a89d12016-12-30 18:10:37 -06002694 if (read(src_fd, buffer, bs) != bs) {
Ethan Yonker472f5062016-02-25 13:47:30 -06002695 LOGINFO("Error reading source fd (%s)\n", strerror(errno));
2696 goto exit;
2697 }
2698 if (write(dest_fd, buffer, bs) != bs) {
2699 LOGINFO("Error writing destination fd (%s)\n", strerror(errno));
2700 goto exit;
2701 }
2702 backedup_size += (unsigned long long)(bs);
Matt Mower029a82d2017-01-08 13:12:38 -06002703 Remain -= (unsigned long long)(bs);
bigbiffce8f83c2015-12-12 18:30:21 -05002704 if (part_settings->progress)
2705 part_settings->progress->UpdateSize(backedup_size);
Ethan Yonker472f5062016-02-25 13:47:30 -06002706 if (PartitionManager.Check_Backup_Cancel() != 0)
2707 goto exit;
2708 }
bigbiffce8f83c2015-12-12 18:30:21 -05002709 if (part_settings->progress)
2710 part_settings->progress->UpdateDisplayDetails(true);
Ethan Yonker472f5062016-02-25 13:47:30 -06002711 fsync(dest_fd);
nailyka083dc62016-11-11 21:41:28 +01002712
2713 if (!part_settings->adbbackup && part_settings->PM_Method == PM_BACKUP) {
2714 tw_set_default_metadata(destfn.c_str());
2715 LOGINFO("Restored default metadata for %s\n", destfn.c_str());
2716 }
2717
Ethan Yonker472f5062016-02-25 13:47:30 -06002718 ret = true;
2719exit:
2720 if (src_fd >= 0)
2721 close(src_fd);
2722 if (dest_fd >= 0)
2723 close(dest_fd);
2724 if (buffer)
2725 free(buffer);
2726 return ret;
2727}
2728
bigbiffce8f83c2015-12-12 18:30:21 -05002729bool TWPartition::Backup_Dump_Image(PartitionSettings *part_settings) {
Vojtech Bocek05534202013-09-11 08:11:56 +02002730 string Full_FileName, Command;
Dees_Troy43d8b002012-09-17 16:00:01 -04002731
Ethan Yonker74db1572015-10-28 12:44:49 -05002732 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, gui_parse_text("{@backing}"));
2733 gui_msg(Msg("backing_up=Backing up {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002734
bigbiffce8f83c2015-12-12 18:30:21 -05002735 if (part_settings->progress)
2736 part_settings->progress->SetPartitionSize(Backup_Size);
Dees_Troy43d8b002012-09-17 16:00:01 -04002737
Ethan Yonker472f5062016-02-25 13:47:30 -06002738 Backup_FileName = Backup_Name + "." + Current_File_System + ".win";
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002739 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04002740
2741 Command = "dump_image " + MTD_Name + " '" + Full_FileName + "'";
bigbiffce8f83c2015-12-12 18:30:21 -05002742
Dees_Troy2673cec2013-04-02 20:22:16 +00002743 LOGINFO("Backup command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02002744 TWFunc::Exec_Cmd(Command);
Ethan Yonker4b94cfd2014-12-11 10:00:45 -06002745 tw_set_default_metadata(Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04002746 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
2747 // 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 -06002748 gui_msg(Msg(msg::kError, "backup_size=Backup file size for '{1}' is 0 bytes.")(Full_FileName));
Dees_Troy7c2dec82012-09-26 09:49:14 -04002749 return false;
2750 }
bigbiffce8f83c2015-12-12 18:30:21 -05002751 if (part_settings->progress)
2752 part_settings->progress->UpdateSize(Backup_Size);
2753
Dees_Troy43d8b002012-09-17 16:00:01 -04002754 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002755}
2756
bigbiffce8f83c2015-12-12 18:30:21 -05002757unsigned long long TWPartition::Get_Restore_Size(PartitionSettings *part_settings) {
2758 if (!part_settings->adbbackup) {
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002759 InfoManager restore_info(part_settings->Backup_Folder + "/" + Backup_Name + ".info");
bigbiffce8f83c2015-12-12 18:30:21 -05002760 if (restore_info.LoadValues() == 0) {
2761 if (restore_info.GetValue("backup_size", Restore_Size) == 0) {
2762 LOGINFO("Read info file, restore size is %llu\n", Restore_Size);
2763 return Restore_Size;
2764 }
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002765 }
2766 }
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002767
Matt Mower029a82d2017-01-08 13:12:38 -06002768 string Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
2769 string Restore_File_System = Get_Restore_File_System(part_settings);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002770
2771 if (Is_Image(Restore_File_System)) {
2772 Restore_Size = TWFunc::Get_File_Size(Full_FileName);
2773 return Restore_Size;
2774 }
2775
2776 twrpTar tar;
2777 tar.setdir(Backup_Path);
2778 tar.setfn(Full_FileName);
bigbiffce8f83c2015-12-12 18:30:21 -05002779 tar.backup_name = Full_FileName;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002780#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
2781 string Password;
2782 DataManager::GetValue("tw_restore_password", Password);
2783 if (!Password.empty())
2784 tar.setpassword(Password);
2785#endif
2786 tar.partition_name = Backup_Name;
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002787 tar.backup_folder = part_settings->Backup_Folder;
bigbiffce8f83c2015-12-12 18:30:21 -05002788 tar.part_settings = part_settings;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002789 Restore_Size = tar.get_size();
2790 return Restore_Size;
2791}
2792
bigbiffce8f83c2015-12-12 18:30:21 -05002793bool TWPartition::Restore_Tar(PartitionSettings *part_settings) {
Ethan Yonker472f5062016-02-25 13:47:30 -06002794 string Full_FileName;
Dees Troy4159aed2014-02-28 17:24:43 +00002795 bool ret = false;
bigbiffce8f83c2015-12-12 18:30:21 -05002796 string Restore_File_System = Get_Restore_File_System(part_settings);
Dees_Troy43d8b002012-09-17 16:00:01 -04002797
Dees_Troye58d5262012-09-21 12:27:57 -04002798 if (Has_Android_Secure) {
Dees_Troye58d5262012-09-21 12:27:57 -04002799 if (!Wipe_AndSec())
2800 return false;
Gary Peck43acadf2012-11-21 21:19:01 -08002801 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002802 gui_msg(Msg("wiping=Wiping {1}")(Backup_Display_Name));
Ethan Yonker5eac2222014-06-11 12:22:55 -05002803 if (Has_Data_Media && Mount_Point == "/data" && Restore_File_System != Current_File_System) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002804 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 -05002805 if (!Wipe_Data_Without_Wiping_Media())
2806 return false;
2807 } else {
2808 if (!Wipe(Restore_File_System))
2809 return false;
2810 }
Dees_Troye58d5262012-09-21 12:27:57 -04002811 }
Matt Mower3c366972015-12-25 19:28:31 -06002812 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Backup_Display_Name, gui_parse_text("{@restoring_hdr}"));
Ethan Yonker74db1572015-10-28 12:44:49 -05002813 gui_msg(Msg("restoring=Restoring {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002814
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05002815 // Remount as read/write as needed so we can restore the backup
2816 if (!ReMount_RW(true))
Dees_Troy43d8b002012-09-17 16:00:01 -04002817 return false;
2818
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002819 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
Ethan Yonker87af5632014-02-10 11:56:35 -06002820 twrpTar tar;
bigbiffce8f83c2015-12-12 18:30:21 -05002821 tar.part_settings = part_settings;
Ethan Yonker87af5632014-02-10 11:56:35 -06002822 tar.setdir(Backup_Path);
2823 tar.setfn(Full_FileName);
2824 tar.backup_name = Backup_Name;
2825#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
2826 string Password;
2827 DataManager::GetValue("tw_restore_password", Password);
2828 if (!Password.empty())
2829 tar.setpassword(Password);
2830#endif
bigbiffce8f83c2015-12-12 18:30:21 -05002831 part_settings->progress->SetPartitionSize(Get_Restore_Size(part_settings));
2832 if (tar.extractTarFork() != 0)
Dees Troy4159aed2014-02-28 17:24:43 +00002833 ret = false;
2834 else
2835 ret = true;
2836#ifdef HAVE_CAPABILITIES
2837 // Restore capabilities to the run-as binary
Captain Throwback9d6feb52018-07-27 10:05:24 -04002838 if (Mount_Point == PartitionManager.Get_Android_Root_Path() && Mount(true) && TWFunc::Path_Exists("/system/bin/run-as")) {
Dees Troy4159aed2014-02-28 17:24:43 +00002839 struct vfs_cap_data cap_data;
2840 uint64_t capabilities = (1 << CAP_SETUID) | (1 << CAP_SETGID);
2841
2842 memset(&cap_data, 0, sizeof(cap_data));
2843 cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE;
2844 cap_data.data[0].permitted = (uint32_t) (capabilities & 0xffffffff);
2845 cap_data.data[0].inheritable = 0;
2846 cap_data.data[1].permitted = (uint32_t) (capabilities >> 32);
2847 cap_data.data[1].inheritable = 0;
2848 if (setxattr("/system/bin/run-as", XATTR_NAME_CAPS, &cap_data, sizeof(cap_data), 0) < 0) {
2849 LOGINFO("Failed to reset capabilities of /system/bin/run-as binary.\n");
2850 } else {
2851 LOGINFO("Reset capabilities of /system/bin/run-as binary successful.\n");
2852 }
2853 }
2854#endif
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05002855 if (Mount_Read_Only || Mount_Flags & MS_RDONLY)
2856 // Remount as read only when restoration is complete
2857 ReMount(true);
2858
Dees Troy4159aed2014-02-28 17:24:43 +00002859 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04002860}
2861
bigbiffce8f83c2015-12-12 18:30:21 -05002862bool TWPartition::Restore_Image(PartitionSettings *part_settings) {
Ethan Yonker96af84a2015-01-05 14:58:36 -06002863 string Full_FileName;
bigbiffce8f83c2015-12-12 18:30:21 -05002864 string Restore_File_System = Get_Restore_File_System(part_settings);
Dees_Troy43d8b002012-09-17 16:00:01 -04002865
Matt Mower3c366972015-12-25 19:28:31 -06002866 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Backup_Display_Name, gui_parse_text("{@restoring_hdr}"));
Ethan Yonker74db1572015-10-28 12:44:49 -05002867 gui_msg(Msg("restoring=Restoring {1}...")(Backup_Display_Name));
bigbiffce8f83c2015-12-12 18:30:21 -05002868
2869 if (part_settings->adbbackup)
2870 Full_FileName = TW_ADB_RESTORE;
2871 else
Ethan Yonkere080c1f2016-09-19 13:50:25 -05002872 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
Gary Peck15e623d2012-11-21 21:07:58 -08002873
Ethan Yonker96af84a2015-01-05 14:58:36 -06002874 if (Restore_File_System == "emmc") {
bigbiffce8f83c2015-12-12 18:30:21 -05002875 if (!part_settings->adbbackup)
2876 part_settings->total_restore_size = (uint64_t)(TWFunc::Get_File_Size(Full_FileName));
2877 if (!Raw_Read_Write(part_settings))
Ethan Yonker96af84a2015-01-05 14:58:36 -06002878 return false;
2879 } else if (Restore_File_System == "mtd" || Restore_File_System == "bml") {
bigbiffce8f83c2015-12-12 18:30:21 -05002880 if (!Flash_Image_FI(Full_FileName, part_settings->progress))
2881 return false;
2882 }
2883
2884 if (part_settings->adbbackup) {
2885 if (!twadbbu::Write_TWEOF())
Ethan Yonker96af84a2015-01-05 14:58:36 -06002886 return false;
Gary Peck15e623d2012-11-21 21:07:58 -08002887 }
Dees_Troy43d8b002012-09-17 16:00:01 -04002888 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002889}
Dees_Troy5bf43922012-09-07 16:07:55 -04002890
2891bool TWPartition::Update_Size(bool Display_Error) {
Dees_Troy0550cfb2012-10-13 11:56:13 -04002892 bool ret = false, Was_Already_Mounted = false;
Dees_Troy51127312012-09-08 13:08:49 -04002893
Ethan Yonker1b190162016-12-05 15:25:19 -06002894 Find_Actual_Block_Device();
2895
2896 if (!Can_Be_Mounted && !Is_Encrypted) {
2897 if (TWFunc::Path_Exists(Actual_Block_Device) && Find_Partition_Size()) {
2898 Used = Size;
2899 Backup_Size = Size;
2900 return true;
2901 }
Dees_Troy5bf43922012-09-07 16:07:55 -04002902 return false;
Ethan Yonker1b190162016-12-05 15:25:19 -06002903 }
Dees_Troy5bf43922012-09-07 16:07:55 -04002904
Dees_Troy0550cfb2012-10-13 11:56:13 -04002905 Was_Already_Mounted = Is_Mounted();
Dees_Troy38bd7602012-09-14 13:33:53 -04002906 if (Removable || Is_Encrypted) {
2907 if (!Mount(false))
2908 return true;
2909 } else if (!Mount(Display_Error))
Dees_Troy5bf43922012-09-07 16:07:55 -04002910 return false;
Dees_Troy51127312012-09-08 13:08:49 -04002911
2912 ret = Get_Size_Via_statfs(Display_Error);
Dees_Troy0550cfb2012-10-13 11:56:13 -04002913 if (!ret || Size == 0) {
2914 if (!Get_Size_Via_df(Display_Error)) {
2915 if (!Was_Already_Mounted)
2916 UnMount(false);
Dees_Troy51127312012-09-08 13:08:49 -04002917 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04002918 }
2919 }
Dees_Troy51127312012-09-08 13:08:49 -04002920
Dees_Troy5bf43922012-09-07 16:07:55 -04002921 if (Has_Data_Media) {
2922 if (Mount(Display_Error)) {
Ethan Yonker3fdcda42016-11-30 12:29:37 -06002923 Used = backup_exclusions.Get_Folder_Size(Mount_Point);
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002924 Backup_Size = Used;
2925 int bak = (int)(Used / 1048576LLU);
Dees_Troy51127312012-09-08 13:08:49 -04002926 int fre = (int)(Free / 1048576LLU);
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002927 LOGINFO("Data backup size is %iMB, free: %iMB.\n", bak, fre);
Dees_Troy0550cfb2012-10-13 11:56:13 -04002928 } else {
2929 if (!Was_Already_Mounted)
2930 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04002931 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04002932 }
Dees_Troye58d5262012-09-21 12:27:57 -04002933 } else if (Has_Android_Secure) {
2934 if (Mount(Display_Error))
Ethan Yonker3fdcda42016-11-30 12:29:37 -06002935 Backup_Size = backup_exclusions.Get_Folder_Size(Backup_Path);
Dees_Troy0550cfb2012-10-13 11:56:13 -04002936 else {
2937 if (!Was_Already_Mounted)
2938 UnMount(false);
Dees_Troye58d5262012-09-21 12:27:57 -04002939 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04002940 }
Dees_Troy5bf43922012-09-07 16:07:55 -04002941 }
Dees_Troy0550cfb2012-10-13 11:56:13 -04002942 if (!Was_Already_Mounted)
2943 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04002944 return true;
Dees_Troy51127312012-09-08 13:08:49 -04002945}
Dees_Troy38bd7602012-09-14 13:33:53 -04002946
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06002947bool TWPartition::Find_Wildcard_Block_Devices(const string& Device) {
2948 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
2949 string Path = TWFunc::Get_Path(Device);
2950 string Dev = TWFunc::Get_Filename(Device);
2951 size_t wildcard_index = Dev.find("*");
2952 if (wildcard_index != string::npos)
2953 Dev = Dev.substr(0, wildcard_index);
2954 wildcard_index = Dev.size();
2955 DIR* d = opendir(Path.c_str());
2956 if (d == NULL) {
2957 LOGINFO("Error opening '%s': %s\n", Path.c_str(), strerror(errno));
2958 return false;
2959 }
2960 struct dirent* de;
2961 while ((de = readdir(d)) != NULL) {
2962 if (de->d_type != DT_BLK || strlen(de->d_name) <= wildcard_index || strncmp(de->d_name, Dev.c_str(), wildcard_index) != 0)
2963 continue;
2964
2965 string item = Path + "/";
2966 item.append(de->d_name);
2967 if (PartitionManager.Find_Partition_By_Block_Device(item))
2968 continue;
2969 TWPartition *part = new TWPartition;
2970 char buffer[MAX_FSTAB_LINE_LENGTH];
2971 sprintf(buffer, "%s %s-%i auto defaults defaults", item.c_str(), Mount_Point.c_str(), ++mount_point_index);
Chaosmasterf6e42ce2020-01-27 00:17:04 +01002972 part->Process_Fstab_Line(buffer, false, NULL, false);
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06002973 char display[MAX_FSTAB_LINE_LENGTH];
2974 sprintf(display, "%s %i", Storage_Name.c_str(), mount_point_index);
2975 part->Storage_Name = display;
2976 part->Display_Name = display;
2977 part->Primary_Block_Device = item;
2978 part->Wildcard_Block_Device = false;
2979 part->Is_SubPartition = true;
2980 part->SubPartition_Of = Mount_Point;
2981 part->Is_Storage = Is_Storage;
2982 part->Can_Be_Mounted = true;
2983 part->Removable = true;
2984 part->Can_Be_Wiped = Can_Be_Wiped;
2985 part->Wipe_Available_in_GUI = Wipe_Available_in_GUI;
2986 part->Find_Actual_Block_Device();
2987 part->Update_Size(false);
2988 Has_SubPartition = true;
2989 PartitionManager.Output_Partition(part);
2990 PartitionManager.Add_Partition(part);
2991 }
2992 closedir(d);
2993 return (mount_point_index > 0);
2994}
2995
Dees_Troy38bd7602012-09-14 13:33:53 -04002996void TWPartition::Find_Actual_Block_Device(void) {
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06002997 if (!Sysfs_Entry.empty() && Primary_Block_Device.empty() && Decrypted_Block_Device.empty()) {
2998 /* Sysfs_Entry.empty() indicates if this is a sysfs entry that begins with /device/
2999 * If we have a syfs entry then we are looking for this device from a uevent add.
3000 * The uevent add will set the primary block device based on the data we receive from
3001 * after checking for adopted storage. If the device ends up being adopted, then the
3002 * decrypted block device will be set instead of the primary block device. */
3003 Is_Present = false;
3004 return;
3005 }
3006 if (Wildcard_Block_Device && !Is_Adopted_Storage) {
3007 Is_Present = false;
3008 Actual_Block_Device = "";
3009 Can_Be_Mounted = false;
3010 if (!Find_Wildcard_Block_Devices(Primary_Block_Device)) {
3011 string Dev = Primary_Block_Device.substr(0, Primary_Block_Device.find("*"));
3012 if (TWFunc::Path_Exists(Dev)) {
3013 Is_Present = true;
3014 Can_Be_Mounted = true;
3015 Actual_Block_Device = Dev;
3016 }
3017 }
3018 return;
3019 } else if (Is_Decrypted && !Decrypted_Block_Device.empty()) {
Dees_Troy38bd7602012-09-14 13:33:53 -04003020 Actual_Block_Device = Decrypted_Block_Device;
Ethan Yonker1b190162016-12-05 15:25:19 -06003021 if (TWFunc::Path_Exists(Decrypted_Block_Device)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04003022 Is_Present = true;
Ethan Yonker1b190162016-12-05 15:25:19 -06003023 return;
3024 }
3025 } else if (SlotSelect && TWFunc::Path_Exists(Primary_Block_Device + PartitionManager.Get_Active_Slot_Suffix())) {
3026 Actual_Block_Device = Primary_Block_Device + PartitionManager.Get_Active_Slot_Suffix();
3027 unlink(Primary_Block_Device.c_str());
3028 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
3029 Is_Present = true;
3030 return;
Dees_Troy43d8b002012-09-17 16:00:01 -04003031 } else if (TWFunc::Path_Exists(Primary_Block_Device)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04003032 Is_Present = true;
3033 Actual_Block_Device = Primary_Block_Device;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05003034 return;
3035 }
Ethan Yonker1b190162016-12-05 15:25:19 -06003036 if (!Alternate_Block_Device.empty() && TWFunc::Path_Exists(Alternate_Block_Device)) {
Dees_Troy3f04d032012-10-07 18:20:09 -04003037 Actual_Block_Device = Alternate_Block_Device;
Dees_Troy38bd7602012-09-14 13:33:53 -04003038 Is_Present = true;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05003039 } else {
Dees_Troy38bd7602012-09-14 13:33:53 -04003040 Is_Present = false;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05003041 }
Dees_Troy38bd7602012-09-14 13:33:53 -04003042}
3043
3044void TWPartition::Recreate_Media_Folder(void) {
3045 string Command;
Ethan Yonker66a19492015-12-10 10:19:45 -06003046 string Media_Path = Mount_Point + "/media";
Dees_Troy38bd7602012-09-14 13:33:53 -04003047
Ethan Yonkerbd7492d2016-12-07 13:55:01 -06003048 if (Is_FBE) {
3049 LOGINFO("Not recreating media folder on FBE\n");
3050 return;
3051 }
Dees_Troy38bd7602012-09-14 13:33:53 -04003052 if (!Mount(true)) {
Ethan Yonker66a19492015-12-10 10:19:45 -06003053 gui_msg(Msg(msg::kError, "recreate_folder_err=Unable to recreate {1} folder.")(Media_Path));
3054 } else if (!TWFunc::Path_Exists(Media_Path)) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05003055 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Ethan Yonker66a19492015-12-10 10:19:45 -06003056 LOGINFO("Recreating %s folder.\n", Media_Path.c_str());
3057 mkdir(Media_Path.c_str(), 0770);
xiaolu9416f4f2015-06-04 08:22:23 +08003058 string Internal_path = DataManager::GetStrValue("tw_internal_path");
3059 if (!Internal_path.empty()) {
3060 LOGINFO("Recreating %s folder.\n", Internal_path.c_str());
3061 mkdir(Internal_path.c_str(), 0770);
3062 }
3063#ifdef TW_INTERNAL_STORAGE_PATH
3064 mkdir(EXPAND(TW_INTERNAL_STORAGE_PATH), 0770);
3065#endif
Matt Mower87413642017-01-17 21:14:46 -06003066
thata3d31fb2014-12-21 22:27:40 +01003067 // Afterwards, we will try to set the
3068 // default metadata that we were hopefully able to get during
3069 // early boot.
Ethan Yonker66a19492015-12-10 10:19:45 -06003070 tw_set_default_metadata(Media_Path.c_str());
xiaolu9416f4f2015-06-04 08:22:23 +08003071 if (!Internal_path.empty())
3072 tw_set_default_metadata(Internal_path.c_str());
Matt Mower87413642017-01-17 21:14:46 -06003073
Ethan Yonker5eac2222014-06-11 12:22:55 -05003074 // Toggle mount to ensure that "internal sdcard" gets mounted
bigbiff bigbiff9c754052013-01-09 09:09:08 -05003075 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Ethan Yonker5eac2222014-06-11 12:22:55 -05003076 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Dees_Troy38bd7602012-09-14 13:33:53 -04003077 }
Dees_Troy43d8b002012-09-17 16:00:01 -04003078}
Dees_Troye58d5262012-09-21 12:27:57 -04003079
3080void TWPartition::Recreate_AndSec_Folder(void) {
Dees_Troye58d5262012-09-21 12:27:57 -04003081 if (!Has_Android_Secure)
3082 return;
Dees_Troy2673cec2013-04-02 20:22:16 +00003083 LOGINFO("Creating %s: %s\n", Backup_Display_Name.c_str(), Symlink_Path.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -04003084 if (!Mount(true)) {
Ethan Yonker74db1572015-10-28 12:44:49 -05003085 gui_msg(Msg(msg::kError, "recreate_folder_err=Unable to recreate {1} folder.")(Backup_Name));
Dees_Troye58d5262012-09-21 12:27:57 -04003086 } else if (!TWFunc::Path_Exists(Symlink_Path)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00003087 LOGINFO("Recreating %s folder.\n", Backup_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05003088 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05003089 mkdir(Symlink_Path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
bigbiff bigbiff9c754052013-01-09 09:09:08 -05003090 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04003091 }
3092}
bigbiff7cb4c332014-11-26 20:36:07 -05003093
3094uint64_t TWPartition::Get_Max_FileSize() {
3095 uint64_t maxFileSize = 0;
3096 const uint64_t constGB = (uint64_t) 1024 * 1024 * 1024;
3097 const uint64_t constTB = (uint64_t) constGB * 1024;
3098 const uint64_t constPB = (uint64_t) constTB * 1024;
bigbiff0c532032014-12-21 13:41:26 -05003099 if (Current_File_System == "ext4")
3100 maxFileSize = 16 * constTB; //16 TB
3101 else if (Current_File_System == "vfat")
3102 maxFileSize = 4 * constGB; //4 GB
3103 else if (Current_File_System == "ntfs")
3104 maxFileSize = 256 * constTB; //256 TB
3105 else if (Current_File_System == "exfat")
3106 maxFileSize = 16 * constPB; //16 PB
3107 else if (Current_File_System == "ext3")
3108 maxFileSize = 2 * constTB; //2 TB
3109 else if (Current_File_System == "f2fs")
3110 maxFileSize = 3.94 * constTB; //3.94 TB
bigbiff7cb4c332014-11-26 20:36:07 -05003111 else
3112 maxFileSize = 100000000L;
3113 return maxFileSize - 1;
3114}
3115
bigbiffce8f83c2015-12-12 18:30:21 -05003116bool TWPartition::Flash_Image(PartitionSettings *part_settings) {
3117 string Restore_File_System, full_filename;
Ethan Yonker96af84a2015-01-05 14:58:36 -06003118
Ethan Yonkere080c1f2016-09-19 13:50:25 -05003119 full_filename = part_settings->Backup_Folder + "/" + Backup_FileName;
Ethan Yonker96af84a2015-01-05 14:58:36 -06003120
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05003121 LOGINFO("Image filename is: %s\n", Backup_FileName.c_str());
bigbiffce8f83c2015-12-12 18:30:21 -05003122
3123 if (Backup_Method == BM_FILES) {
Ethan Yonker96af84a2015-01-05 14:58:36 -06003124 LOGERR("Cannot flash images to file systems\n");
3125 return false;
3126 } else if (!Can_Flash_Img) {
3127 LOGERR("Cannot flash images to partitions %s\n", Display_Name.c_str());
3128 return false;
3129 } else {
3130 if (!Find_Partition_Size()) {
3131 LOGERR("Unable to find partition size for '%s'\n", Mount_Point.c_str());
3132 return false;
3133 }
bigbiffce8f83c2015-12-12 18:30:21 -05003134 unsigned long long image_size = TWFunc::Get_File_Size(full_filename);
Ethan Yonker96af84a2015-01-05 14:58:36 -06003135 if (image_size > Size) {
Ethan Yonker74db1572015-10-28 12:44:49 -05003136 LOGINFO("Size (%llu bytes) of image '%s' is larger than target device '%s' (%llu bytes)\n",
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05003137 image_size, Backup_FileName.c_str(), Actual_Block_Device.c_str(), Size);
Ethan Yonker74db1572015-10-28 12:44:49 -05003138 gui_err("img_size_err=Size of image is larger than target device");
Ethan Yonker96af84a2015-01-05 14:58:36 -06003139 return false;
3140 }
bigbiffce8f83c2015-12-12 18:30:21 -05003141 if (Backup_Method == BM_DD) {
3142 if (!part_settings->adbbackup) {
3143 if (Is_Sparse_Image(full_filename)) {
3144 return Flash_Sparse_Image(full_filename);
3145 }
Ethan Yonker472f5062016-02-25 13:47:30 -06003146 }
bigbiffce8f83c2015-12-12 18:30:21 -05003147 return Raw_Read_Write(part_settings);
3148 } else if (Backup_Method == BM_FLASH_UTILS) {
3149 return Flash_Image_FI(full_filename, NULL);
Ethan Yonker472f5062016-02-25 13:47:30 -06003150 }
Ethan Yonker96af84a2015-01-05 14:58:36 -06003151 }
3152
3153 LOGERR("Unknown flash method for '%s'\n", Mount_Point.c_str());
3154 return false;
3155}
3156
Ethan Yonker472f5062016-02-25 13:47:30 -06003157bool TWPartition::Is_Sparse_Image(const string& Filename) {
HashBanged974bb2016-01-30 14:20:09 -05003158 uint32_t magic = 0;
3159 int fd = open(Filename.c_str(), O_RDONLY);
3160 if (fd < 0) {
3161 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Filename)(strerror(errno)));
3162 return false;
3163 }
bigbiffce8f83c2015-12-12 18:30:21 -05003164
HashBanged974bb2016-01-30 14:20:09 -05003165 if (read(fd, &magic, sizeof(magic)) != sizeof(magic)) {
3166 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Filename)(strerror(errno)));
3167 close(fd);
3168 return false;
3169 }
3170 close(fd);
Ethan Yonker472f5062016-02-25 13:47:30 -06003171 if (magic == SPARSE_HEADER_MAGIC)
3172 return true;
3173 return false;
3174}
3175
3176bool TWPartition::Flash_Sparse_Image(const string& Filename) {
3177 string Command;
3178
3179 gui_msg(Msg("flashing=Flashing {1}...")(Display_Name));
3180
3181 Command = "simg2img '" + Filename + "' '" + Actual_Block_Device + "'";
Ethan Yonker96af84a2015-01-05 14:58:36 -06003182 LOGINFO("Flash command: '%s'\n", Command.c_str());
3183 TWFunc::Exec_Cmd(Command);
3184 return true;
3185}
3186
Ethan Yonker472f5062016-02-25 13:47:30 -06003187bool TWPartition::Flash_Image_FI(const string& Filename, ProgressTracking *progress) {
Ethan Yonker96af84a2015-01-05 14:58:36 -06003188 string Command;
Ethan Yonker472f5062016-02-25 13:47:30 -06003189 unsigned long long file_size;
Ethan Yonker96af84a2015-01-05 14:58:36 -06003190
Ethan Yonker74db1572015-10-28 12:44:49 -05003191 gui_msg(Msg("flashing=Flashing {1}...")(Display_Name));
Ethan Yonker472f5062016-02-25 13:47:30 -06003192 if (progress) {
3193 file_size = (unsigned long long)(TWFunc::Get_File_Size(Filename));
3194 progress->SetPartitionSize(file_size);
3195 }
Ethan Yonker96af84a2015-01-05 14:58:36 -06003196 // Sometimes flash image doesn't like to flash due to the first 2KB matching, so we erase first to ensure that it flashes
3197 Command = "erase_image " + MTD_Name;
3198 LOGINFO("Erase command: '%s'\n", Command.c_str());
3199 TWFunc::Exec_Cmd(Command);
3200 Command = "flash_image " + MTD_Name + " '" + Filename + "'";
3201 LOGINFO("Flash command: '%s'\n", Command.c_str());
3202 TWFunc::Exec_Cmd(Command);
Ethan Yonker472f5062016-02-25 13:47:30 -06003203 if (progress)
3204 progress->UpdateSize(file_size);
Ethan Yonker96af84a2015-01-05 14:58:36 -06003205 return true;
3206}
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05003207
3208void TWPartition::Change_Mount_Read_Only(bool new_value) {
3209 Mount_Read_Only = new_value;
3210}
3211
bigbiffce8f83c2015-12-12 18:30:21 -05003212bool TWPartition::Is_Read_Only() {
3213 return Mount_Read_Only;
3214}
3215
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05003216int TWPartition::Check_Lifetime_Writes() {
3217 bool original_read_only = Mount_Read_Only;
3218 int ret = 1;
3219
3220 Mount_Read_Only = true;
3221 if (Mount(false)) {
3222 Find_Actual_Block_Device();
Ethan Yonker54970bb2019-01-17 16:19:24 -06003223 string temp = Actual_Block_Device;
3224 Find_Real_Block_Device(temp, false);
3225 string block = basename(temp.c_str());
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05003226 string file = "/sys/fs/" + Current_File_System + "/" + block + "/lifetime_write_kbytes";
3227 string result;
3228 if (TWFunc::Path_Exists(file)) {
3229 if (TWFunc::read_file(file, result) != 0) {
3230 LOGINFO("Check_Lifetime_Writes of '%s' failed to read_file\n", file.c_str());
3231 } else {
3232 LOGINFO("Check_Lifetime_Writes result: '%s'\n", result.c_str());
3233 if (result == "0") {
3234 ret = 0;
3235 }
3236 }
3237 } else {
3238 LOGINFO("Check_Lifetime_Writes file does not exist '%s'\n", file.c_str());
3239 }
3240 UnMount(true);
3241 } else {
3242 LOGINFO("Check_Lifetime_Writes failed to mount '%s'\n", Mount_Point.c_str());
3243 }
3244 Mount_Read_Only = original_read_only;
3245 return ret;
3246}
Ethan Yonker66a19492015-12-10 10:19:45 -06003247
3248int TWPartition::Decrypt_Adopted() {
3249#ifdef TW_INCLUDE_CRYPTO
3250 int ret = 1;
3251 Is_Adopted_Storage = false;
3252 string Adopted_Key_File = "";
3253
3254 if (!Removable)
3255 return ret;
3256
3257 int fd = open(Alternate_Block_Device.c_str(), O_RDONLY);
3258 if (fd < 0) {
3259 LOGINFO("failed to open '%s'\n", Alternate_Block_Device.c_str());
3260 return ret;
3261 }
3262 char type_guid[80];
3263 char part_guid[80];
3264
3265 if (gpt_disk_get_partition_info(fd, 2, type_guid, part_guid) == 0) {
3266 LOGINFO("type: '%s'\n", type_guid);
3267 LOGINFO("part: '%s'\n", part_guid);
3268 Adopted_GUID = part_guid;
3269 LOGINFO("Adopted_GUID '%s'\n", Adopted_GUID.c_str());
3270 if (strcmp(type_guid, TWGptAndroidExpand) == 0) {
3271 LOGINFO("android_expand found\n");
3272 Adopted_Key_File = "/data/misc/vold/expand_";
3273 Adopted_Key_File += part_guid;
3274 Adopted_Key_File += ".key";
3275 if (TWFunc::Path_Exists(Adopted_Key_File)) {
3276 Is_Adopted_Storage = true;
3277 /* Until we find a use case for this, I think it is safe
3278 * to disable USB Mass Storage whenever adopted storage
3279 * is present.
3280 */
3281 LOGINFO("Detected adopted storage, disabling USB mass storage mode\n");
3282 DataManager::SetValue("tw_has_usb_storage", 0);
3283 }
3284 }
3285 }
3286
3287 if (Is_Adopted_Storage) {
3288 string Adopted_Block_Device = Alternate_Block_Device + "p2";
3289 if (!TWFunc::Path_Exists(Adopted_Block_Device)) {
3290 Adopted_Block_Device = Alternate_Block_Device + "2";
3291 if (!TWFunc::Path_Exists(Adopted_Block_Device)) {
3292 LOGINFO("Adopted block device does not exist\n");
3293 goto exit;
3294 }
3295 }
3296 LOGINFO("key file is '%s', block device '%s'\n", Adopted_Key_File.c_str(), Adopted_Block_Device.c_str());
3297 char crypto_blkdev[MAXPATHLEN];
3298 std::string thekey;
3299 int fdkey = open(Adopted_Key_File.c_str(), O_RDONLY);
3300 if (fdkey < 0) {
3301 LOGINFO("failed to open key file\n");
3302 goto exit;
3303 }
3304 char buf[512];
3305 ssize_t n;
3306 while ((n = read(fdkey, &buf[0], sizeof(buf))) > 0) {
3307 thekey.append(buf, n);
3308 }
3309 close(fdkey);
3310 unsigned char* key = (unsigned char*) thekey.data();
3311 cryptfs_revert_ext_volume(part_guid);
3312
3313 ret = cryptfs_setup_ext_volume(part_guid, Adopted_Block_Device.c_str(), key, thekey.size(), crypto_blkdev);
3314 if (ret == 0) {
3315 LOGINFO("adopted storage new block device: '%s'\n", crypto_blkdev);
3316 Decrypted_Block_Device = crypto_blkdev;
3317 Is_Decrypted = true;
3318 Is_Encrypted = true;
3319 Find_Actual_Block_Device();
Kjell Braden3126a112016-06-19 16:58:15 +00003320 if (!Mount_Storage_Retry(false)) {
Ethan Yonker66a19492015-12-10 10:19:45 -06003321 LOGERR("Failed to mount decrypted adopted storage device\n");
3322 Is_Decrypted = false;
3323 Is_Encrypted = false;
3324 cryptfs_revert_ext_volume(part_guid);
3325 ret = 1;
3326 } else {
Ethan Yonkerfcf3f242016-02-16 12:30:26 -06003327 UnMount(false);
3328 Has_Android_Secure = false;
3329 Symlink_Path = "";
3330 Symlink_Mount_Point = "";
3331 Backup_Name = Mount_Point.substr(1);
3332 Backup_Path = Mount_Point;
3333 TWPartition* sdext = PartitionManager.Find_Partition_By_Path("/sd-ext");
3334 if (sdext && sdext->Actual_Block_Device == Adopted_Block_Device) {
3335 LOGINFO("Removing /sd-ext from partition list due to adopted storage\n");
3336 PartitionManager.Remove_Partition_By_Path("/sd-ext");
3337 }
Ethan Yonker66a19492015-12-10 10:19:45 -06003338 Setup_Data_Media();
3339 Recreate_Media_Folder();
3340 Wipe_Available_in_GUI = true;
3341 Wipe_During_Factory_Reset = true;
3342 Can_Be_Backed_Up = true;
3343 Can_Encrypt_Backup = true;
3344 Use_Userdata_Encryption = true;
3345 Is_Storage = true;
3346 Storage_Name = "Adopted Storage";
3347 Is_SubPartition = true;
3348 SubPartition_Of = "/data";
3349 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
3350 DataManager::SetValue("tw_has_adopted_storage", 1);
3351 }
3352 } else {
3353 LOGERR("Failed to setup adopted storage decryption\n");
3354 }
3355 }
3356exit:
Matt Mower06543e32017-01-06 15:25:26 -06003357 close(fd);
Ethan Yonker66a19492015-12-10 10:19:45 -06003358 return ret;
3359#else
3360 LOGINFO("Decrypt_Adopted: no crypto support\n");
3361 return 1;
3362#endif
3363}
3364
3365void TWPartition::Revert_Adopted() {
3366#ifdef TW_INCLUDE_CRYPTO
3367 if (!Adopted_GUID.empty()) {
3368 PartitionManager.Remove_MTP_Storage(Mount_Point);
3369 UnMount(false);
3370 cryptfs_revert_ext_volume(Adopted_GUID.c_str());
3371 Is_Adopted_Storage = false;
3372 Is_Encrypted = false;
3373 Is_Decrypted = false;
3374 Decrypted_Block_Device = "";
3375 Find_Actual_Block_Device();
3376 Wipe_During_Factory_Reset = false;
3377 Can_Be_Backed_Up = false;
3378 Can_Encrypt_Backup = false;
3379 Use_Userdata_Encryption = false;
3380 Is_SubPartition = false;
3381 SubPartition_Of = "";
3382 Has_Data_Media = false;
3383 Storage_Path = Mount_Point;
3384 if (!Symlink_Mount_Point.empty()) {
3385 TWPartition* Dat = PartitionManager.Find_Partition_By_Path("/data");
3386 if (Dat) {
3387 Dat->UnMount(false);
3388 Dat->Symlink_Mount_Point = Symlink_Mount_Point;
3389 }
3390 Symlink_Mount_Point = "";
3391 }
3392 }
3393#else
3394 LOGINFO("Revert_Adopted: no crypto support\n");
3395#endif
3396}
bigbiff bigbiffb5ecaad2017-03-20 18:53:53 -04003397
3398void TWPartition::Set_Backup_FileName(string fname) {
3399 Backup_FileName = fname;
3400}
3401
3402string TWPartition::Get_Backup_Name() {
3403 return Backup_Name;
3404}