blob: 581593caab793bfc9005690fbed1c891d7431d36 [file] [log] [blame]
Dees Troy3be70a82013-10-22 14:25:12 +00001/*
bigbiff bigbiff56cf5642016-08-19 17:43:45 -04002 Copyright 2013 to 2017 TeamWin
Dees Troy3be70a82013-10-22 14:25:12 +00003 This file is part of TWRP/TeamWin Recovery Project.
4
5 TWRP is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 TWRP is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with TWRP. If not, see <http://www.gnu.org/licenses/>.
17*/
Dees_Troy51a0e822012-09-05 15:24:24 -040018
19#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
22#include <sys/stat.h>
23#include <sys/vfs.h>
Dees_Troy5bf43922012-09-07 16:07:55 -040024#include <sys/mount.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040025#include <unistd.h>
Dees_Troy51127312012-09-08 13:08:49 -040026#include <dirent.h>
thatf54e5392016-01-29 22:04:43 +010027#include <libgen.h>
bigbiffce8f83c2015-12-12 18:30:21 -050028#include <zlib.h>
bigbiff bigbiff9c754052013-01-09 09:09:08 -050029#include <iostream>
30#include <sstream>
Ethan Yonker66a19492015-12-10 10:19:45 -060031#include <sys/param.h>
Ethan Yonker472f5062016-02-25 13:47:30 -060032#include <fcntl.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040033
Ethan Yonker007de982018-08-31 14:23:16 -050034#include "cutils/properties.h"
bigbiff7b4c7a62015-01-01 19:44:14 -050035#include "libblkid/include/blkid.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040036#include "variables.h"
Dees_Troy2673cec2013-04-02 20:22:16 +000037#include "twcommon.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040038#include "partitions.hpp"
Dees_Troy5bf43922012-09-07 16:07:55 -040039#include "data.hpp"
Dees_Troy43d8b002012-09-17 16:00:01 -040040#include "twrp-functions.hpp"
bigbiff bigbiff9c754052013-01-09 09:09:08 -050041#include "twrpTar.hpp"
Ethan Yonker3fdcda42016-11-30 12:29:37 -060042#include "exclude.hpp"
Ethan Yonker1b7a31b2014-07-03 15:09:22 -050043#include "infomanager.hpp"
Ethan Yonker4b94cfd2014-12-11 10:00:45 -060044#include "set_metadata.h"
Ethan Yonker74db1572015-10-28 12:44:49 -050045#include "gui/gui.hpp"
bigbiffce8f83c2015-12-12 18:30:21 -050046#include "adbbu/libtwadbbu.hpp"
Ethan Yonker98661c12018-10-17 08:39:28 -050047#ifdef TW_INCLUDE_CRYPTO
48 #include "crypto/fde/cryptfs.h"
49 #ifdef TW_INCLUDE_FBE
50 #include "crypto/ext4crypt/Decrypt.h"
51 #endif
52#else
53 #define CRYPT_FOOTER_OFFSET 0x4000
54#endif
Dees_Troy5bf43922012-09-07 16:07:55 -040055extern "C" {
Dees_Troy38bd7602012-09-14 13:33:53 -040056 #include "mtdutils/mtdutils.h"
57 #include "mtdutils/mounts.h"
Dees_Troya95f55c2013-08-17 13:14:43 +000058#ifdef USE_EXT4
Ethan Yonker8373cfe2017-09-08 06:50:54 -050059 // #include "make_ext4fs.h" TODO need ifdef for android8
60 #include <ext4_utils/make_ext4fs.h>
Dees_Troya95f55c2013-08-17 13:14:43 +000061#endif
Ethan Yonker71413f42014-02-26 13:36:08 -060062#ifdef TW_INCLUDE_CRYPTO
Ethan Yonker66a19492015-12-10 10:19:45 -060063 #include "gpt/gpt.h"
Ethan Yonker71413f42014-02-26 13:36:08 -060064#endif
Dees_Troy5bf43922012-09-07 16:07:55 -040065}
Matt Mower87413642017-01-17 21:14:46 -060066#include <selinux/selinux.h>
Ethan Yonkerf27497f2014-02-09 11:48:33 -060067#include <selinux/label.h>
Dees Troy4159aed2014-02-28 17:24:43 +000068#ifdef HAVE_CAPABILITIES
69#include <sys/capability.h>
70#include <sys/xattr.h>
71#include <linux/xattr.h>
72#endif
HashBanged974bb2016-01-30 14:20:09 -050073#include <sparse_format.h>
Ethan Yonker472f5062016-02-25 13:47:30 -060074#include "progresstracking.hpp"
Dees_Troy51a0e822012-09-05 15:24:24 -040075
bigbiff bigbiff9c754052013-01-09 09:09:08 -050076using namespace std;
77
Ethan Yonker6e8c27a2016-12-22 17:55:57 -060078static int auto_index = 0; // v2 fstab allows you to specify a mount point of "auto" with no /. These items are given a mount point of /auto* where * == auto_index
79
Dees_Troya95f55c2013-08-17 13:14:43 +000080extern struct selabel_handle *selinux_handle;
Ethan Yonker6277c792014-09-15 14:54:30 -050081extern bool datamedia;
Dees_Troya95f55c2013-08-17 13:14:43 +000082
Hashcode62bd9e02013-11-19 21:59:42 -080083struct flag_list {
84 const char *name;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -060085 unsigned long flag;
Hashcode62bd9e02013-11-19 21:59:42 -080086};
87
Matt Mower4ab42b12016-04-21 13:52:18 -050088const struct flag_list mount_flags[] = {
Ethan Yonker6e8c27a2016-12-22 17:55:57 -060089 { "noatime", MS_NOATIME },
90 { "noexec", MS_NOEXEC },
91 { "nosuid", MS_NOSUID },
92 { "nodev", MS_NODEV },
93 { "nodiratime", MS_NODIRATIME },
94 { "ro", MS_RDONLY },
95 { "rw", 0 },
96 { "remount", MS_REMOUNT },
97 { "bind", MS_BIND },
98 { "rec", MS_REC },
Dees Troyc4bc30e2014-02-03 15:04:19 +000099#ifdef MS_UNBINDABLE
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600100 { "unbindable", MS_UNBINDABLE },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000101#endif
102#ifdef MS_PRIVATE
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600103 { "private", MS_PRIVATE },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000104#endif
105#ifdef MS_SLAVE
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600106 { "slave", MS_SLAVE },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000107#endif
108#ifdef MS_SHARED
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600109 { "shared", MS_SHARED },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000110#endif
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600111 { "sync", MS_SYNCHRONOUS },
112 { 0, 0 },
113};
114
115const char *ignored_mount_items[] = {
116 "defaults=",
117 "errors=",
Ethan Yonker93382822018-11-01 15:25:31 -0500118 "latemount",
119 "sysfs_path=",
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600120 NULL
Hashcode62bd9e02013-11-19 21:59:42 -0800121};
122
Matt Mower2416a502016-04-12 19:54:46 -0500123enum TW_FSTAB_FLAGS {
124 TWFLAG_DEFAULTS, // Retain position
125 TWFLAG_ANDSEC,
126 TWFLAG_BACKUP,
127 TWFLAG_BACKUPNAME,
128 TWFLAG_BLOCKSIZE,
129 TWFLAG_CANBEWIPED,
130 TWFLAG_CANENCRYPTBACKUP,
131 TWFLAG_DISPLAY,
132 TWFLAG_ENCRYPTABLE,
Ethan Yonkere9afc3d2018-08-30 15:16:27 -0500133 TWFLAG_FILEENCRYPTION,
Matt Mower2416a502016-04-12 19:54:46 -0500134 TWFLAG_FLASHIMG,
135 TWFLAG_FORCEENCRYPT,
136 TWFLAG_FSFLAGS,
137 TWFLAG_IGNOREBLKID,
138 TWFLAG_LENGTH,
139 TWFLAG_MOUNTTODECRYPT,
140 TWFLAG_REMOVABLE,
141 TWFLAG_RETAINLAYOUTVERSION,
142 TWFLAG_SETTINGSSTORAGE,
143 TWFLAG_STORAGE,
144 TWFLAG_STORAGENAME,
145 TWFLAG_SUBPARTITIONOF,
146 TWFLAG_SYMLINK,
147 TWFLAG_USERDATAENCRYPTBACKUP,
148 TWFLAG_USERMRF,
149 TWFLAG_WIPEDURINGFACTORYRESET,
150 TWFLAG_WIPEINGUI,
Ethan Yonker1b190162016-12-05 15:25:19 -0600151 TWFLAG_SLOTSELECT,
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600152 TWFLAG_WAIT,
153 TWFLAG_VERIFY,
154 TWFLAG_CHECK,
155 TWFLAG_ALTDEVICE,
156 TWFLAG_NOTRIM,
157 TWFLAG_VOLDMANAGED,
158 TWFLAG_FORMATTABLE,
159 TWFLAG_RESIZE,
Ethan Yonker93382822018-11-01 15:25:31 -0500160 TWFLAG_KEYDIRECTORY,
Matt Mower2416a502016-04-12 19:54:46 -0500161};
162
163/* Flags without a trailing '=' are considered dual format flags and can be
164 * written as either 'flagname' or 'flagname=', where the character following
165 * the '=' is Y,y,1 for true and false otherwise.
166 */
167const struct flag_list tw_flags[] = {
168 { "andsec", TWFLAG_ANDSEC },
169 { "backup", TWFLAG_BACKUP },
170 { "backupname=", TWFLAG_BACKUPNAME },
171 { "blocksize=", TWFLAG_BLOCKSIZE },
172 { "canbewiped", TWFLAG_CANBEWIPED },
173 { "canencryptbackup", TWFLAG_CANENCRYPTBACKUP },
174 { "defaults", TWFLAG_DEFAULTS },
175 { "display=", TWFLAG_DISPLAY },
176 { "encryptable=", TWFLAG_ENCRYPTABLE },
Ethan Yonkere9afc3d2018-08-30 15:16:27 -0500177 { "fileencryption=", TWFLAG_FILEENCRYPTION },
Matt Mower2416a502016-04-12 19:54:46 -0500178 { "flashimg", TWFLAG_FLASHIMG },
179 { "forceencrypt=", TWFLAG_FORCEENCRYPT },
180 { "fsflags=", TWFLAG_FSFLAGS },
181 { "ignoreblkid", TWFLAG_IGNOREBLKID },
182 { "length=", TWFLAG_LENGTH },
183 { "mounttodecrypt", TWFLAG_MOUNTTODECRYPT },
184 { "removable", TWFLAG_REMOVABLE },
185 { "retainlayoutversion", TWFLAG_RETAINLAYOUTVERSION },
186 { "settingsstorage", TWFLAG_SETTINGSSTORAGE },
187 { "storage", TWFLAG_STORAGE },
188 { "storagename=", TWFLAG_STORAGENAME },
189 { "subpartitionof=", TWFLAG_SUBPARTITIONOF },
190 { "symlink=", TWFLAG_SYMLINK },
191 { "userdataencryptbackup", TWFLAG_USERDATAENCRYPTBACKUP },
192 { "usermrf", TWFLAG_USERMRF },
193 { "wipeduringfactoryreset", TWFLAG_WIPEDURINGFACTORYRESET },
194 { "wipeingui", TWFLAG_WIPEINGUI },
Ethan Yonker1b190162016-12-05 15:25:19 -0600195 { "slotselect", TWFLAG_SLOTSELECT },
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600196 { "wait", TWFLAG_WAIT },
197 { "verify", TWFLAG_VERIFY },
198 { "check", TWFLAG_CHECK },
199 { "altdevice", TWFLAG_ALTDEVICE },
200 { "notrim", TWFLAG_NOTRIM },
201 { "voldmanaged=", TWFLAG_VOLDMANAGED },
202 { "formattable", TWFLAG_FORMATTABLE },
203 { "resize", TWFLAG_RESIZE },
Ethan Yonker93382822018-11-01 15:25:31 -0500204 { "keydirectory=", TWFLAG_KEYDIRECTORY },
Matt Mower2416a502016-04-12 19:54:46 -0500205 { 0, 0 },
206};
207
that9e0593e2014-10-08 00:01:24 +0200208TWPartition::TWPartition() {
Dees_Troy51a0e822012-09-05 15:24:24 -0400209 Can_Be_Mounted = false;
210 Can_Be_Wiped = false;
Dees_Troya13d74f2013-03-24 08:54:55 -0500211 Can_Be_Backed_Up = false;
Vojtech Bocek1dc30982013-08-30 21:49:30 +0200212 Use_Rm_Rf = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400213 Wipe_During_Factory_Reset = false;
214 Wipe_Available_in_GUI = false;
215 Is_SubPartition = false;
Dees_Troy2691f9d2012-09-24 11:15:49 -0400216 Has_SubPartition = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400217 SubPartition_Of = "";
218 Symlink_Path = "";
219 Symlink_Mount_Point = "";
220 Mount_Point = "";
Dees_Troye58d5262012-09-21 12:27:57 -0400221 Backup_Path = "";
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600222 Wildcard_Block_Device = false;
223 Sysfs_Entry = "";
Dees_Troy38bd7602012-09-14 13:33:53 -0400224 Actual_Block_Device = "";
225 Primary_Block_Device = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400226 Alternate_Block_Device = "";
227 Removable = false;
228 Is_Present = false;
229 Length = 0;
230 Size = 0;
231 Used = 0;
232 Free = 0;
233 Backup_Size = 0;
234 Can_Be_Encrypted = false;
235 Is_Encrypted = false;
236 Is_Decrypted = false;
Ethan Yonkerbd7492d2016-12-07 13:55:01 -0600237 Is_FBE = false;
Ethan Yonker253368a2014-11-25 15:00:52 -0600238 Mount_To_Decrypt = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400239 Decrypted_Block_Device = "";
240 Display_Name = "";
Dees_Troya13d74f2013-03-24 08:54:55 -0500241 Backup_Display_Name = "";
242 Storage_Name = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400243 Backup_Name = "";
Dees_Troy63c8df72012-09-10 14:02:05 -0400244 Backup_FileName = "";
Dees_Troy38bd7602012-09-14 13:33:53 -0400245 MTD_Name = "";
bigbiffce8f83c2015-12-12 18:30:21 -0500246 Backup_Method = BM_NONE;
Dees_Troy83bd4832013-05-04 12:39:56 +0000247 Can_Encrypt_Backup = false;
248 Use_Userdata_Encryption = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400249 Has_Data_Media = false;
Dees_Troye58d5262012-09-21 12:27:57 -0400250 Has_Android_Secure = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400251 Is_Storage = false;
Dees_Troya13d74f2013-03-24 08:54:55 -0500252 Is_Settings_Storage = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400253 Storage_Path = "";
254 Current_File_System = "";
255 Fstab_File_System = "";
Hashcode62bd9e02013-11-19 21:59:42 -0800256 Mount_Flags = 0;
257 Mount_Options = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400258 Format_Block_Size = 0;
Dees_Troy68cab492012-12-12 19:29:35 +0000259 Ignore_Blkid = false;
Dees_Troy16c2b312013-01-15 16:51:18 +0000260 Retain_Layout_Version = false;
dianlujitao4879b372018-12-03 18:45:47 +0800261 Crypto_Key_Location = "";
Ethan Yonker726a0202014-12-16 20:01:38 -0600262 MTP_Storage_ID = 0;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600263 Can_Flash_Img = false;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500264 Mount_Read_Only = false;
Ethan Yonker66a19492015-12-10 10:19:45 -0600265 Is_Adopted_Storage = false;
266 Adopted_GUID = "";
Ethan Yonker1b190162016-12-05 15:25:19 -0600267 SlotSelect = false;
Ethan Yonker93382822018-11-01 15:25:31 -0500268 Key_Directory = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400269}
270
271TWPartition::~TWPartition(void) {
272 // Do nothing
273}
274
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600275bool TWPartition::Process_Fstab_Line(const char *fstab_line, bool Display_Error, std::map<string, Flags_Map> *twrp_flags) {
Matt Mower2b2dd152016-04-26 11:24:08 -0500276 char full_line[MAX_FSTAB_LINE_LENGTH];
Matt Mower2416a502016-04-12 19:54:46 -0500277 char twflags[MAX_FSTAB_LINE_LENGTH] = "";
Dees_Troy5bf43922012-09-07 16:07:55 -0400278 char* ptr;
Matt Mower2b2dd152016-04-26 11:24:08 -0500279 int line_len = strlen(fstab_line), index = 0, item_index = 0;
Dees_Troya13d74f2013-03-24 08:54:55 -0500280 bool skip = false;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600281 int fstab_version = 1, mount_point_index = 0, fs_index = 1, block_device_index = 2;
282 TWPartition *additional_entry = NULL;
283 std::map<string, Flags_Map>::iterator it;
Dees_Troy5bf43922012-09-07 16:07:55 -0400284
Matt Mower2b2dd152016-04-26 11:24:08 -0500285 strlcpy(full_line, fstab_line, sizeof(full_line));
Dees_Troy51127312012-09-08 13:08:49 -0400286 for (index = 0; index < line_len; index++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500287 if (full_line[index] == 34)
288 skip = !skip;
289 if (!skip && full_line[index] <= 32)
Dees_Troy5bf43922012-09-07 16:07:55 -0400290 full_line[index] = '\0';
Dees_Troy5bf43922012-09-07 16:07:55 -0400291 }
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600292 if (line_len < 10)
293 return false; // There can't possibly be a valid fstab line that is less than 10 chars
294 if (strncmp(fstab_line, "/dev/", strlen("/dev/")) == 0 || strncmp(fstab_line, "/devices/", strlen("/devices/")) == 0) {
295 fstab_version = 2;
296 block_device_index = 0;
297 mount_point_index = 1;
298 fs_index = 2;
299 }
300
301 index = 0;
Dees_Troy5bf43922012-09-07 16:07:55 -0400302 while (index < line_len) {
303 while (index < line_len && full_line[index] == '\0')
304 index++;
305 if (index >= line_len)
306 continue;
307 ptr = full_line + index;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600308 if (item_index == mount_point_index) {
309 Mount_Point = ptr;
310 if (fstab_version == 2) {
311 additional_entry = PartitionManager.Find_Partition_By_Path(Mount_Point);
312 if (additional_entry) {
313 LOGINFO("Found an additional entry for '%s'\n", Mount_Point.c_str());
314 }
315 }
316 LOGINFO("Processing '%s'\n", Mount_Point.c_str());
317 Backup_Path = Mount_Point;
318 Storage_Path = Mount_Point;
319 Display_Name = ptr + 1;
320 Backup_Display_Name = Display_Name;
321 Storage_Name = Display_Name;
322 item_index++;
323 } else if (item_index == fs_index) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400324 // File System
325 Fstab_File_System = ptr;
326 Current_File_System = ptr;
327 item_index++;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600328 } else if (item_index == block_device_index) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400329 // Primary Block Device
Dees_Troy38bd7602012-09-14 13:33:53 -0400330 if (Fstab_File_System == "mtd" || Fstab_File_System == "yaffs2") {
Dees_Troy094207a2012-09-26 12:00:39 -0400331 MTD_Name = ptr;
332 Find_MTD_Block_Device(MTD_Name);
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400333 } else if (Fstab_File_System == "bml") {
334 if (Mount_Point == "/boot")
335 MTD_Name = "boot";
336 else if (Mount_Point == "/recovery")
337 MTD_Name = "recovery";
338 Primary_Block_Device = ptr;
339 if (*ptr != '/')
Dees_Troy2673cec2013-04-02 20:22:16 +0000340 LOGERR("Until we get better BML support, you will have to find and provide the full block device path to the BML devices e.g. /dev/block/bml9 instead of the partition name\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400341 } else if (*ptr != '/') {
Dees_Troy5bf43922012-09-07 16:07:55 -0400342 if (Display_Error)
Matt Mower2b2dd152016-04-26 11:24:08 -0500343 LOGERR("Invalid block device '%s' in fstab line '%s'", ptr, fstab_line);
Dees_Troy5bf43922012-09-07 16:07:55 -0400344 else
Matt Mower2b2dd152016-04-26 11:24:08 -0500345 LOGINFO("Invalid block device '%s' in fstab line '%s'", ptr, fstab_line);
Matt Mower72c87ce2016-04-26 14:34:56 -0500346 return false;
Dees_Troy38bd7602012-09-14 13:33:53 -0400347 } else {
348 Primary_Block_Device = ptr;
349 Find_Real_Block_Device(Primary_Block_Device, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400350 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400351 item_index++;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600352 } else if (item_index > 2) {
353 if (fstab_version == 2) {
354 if (item_index == 3) {
355 Process_FS_Flags(ptr);
356 if (additional_entry) {
357 additional_entry->Save_FS_Flags(Fstab_File_System, Mount_Flags, Mount_Options);
358 return false; // We save the extra fs flags in the other partition entry and by returning false, this entry will be deleted
359 }
360 } else {
361 strlcpy(twflags, ptr, sizeof(twflags));
362 }
363 item_index++;
364 } else if (*ptr == '/') { // v2 fstab does not allow alternate block devices
Dees_Troy5bf43922012-09-07 16:07:55 -0400365 // Alternate Block Device
366 Alternate_Block_Device = ptr;
367 Find_Real_Block_Device(Alternate_Block_Device, Display_Error);
368 } else if (strlen(ptr) > 7 && strncmp(ptr, "length=", 7) == 0) {
369 // Partition length
370 ptr += 7;
371 Length = atoi(ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400372 } else if (strlen(ptr) > 6 && strncmp(ptr, "flags=", 6) == 0) {
373 // Custom flags, save for later so that new values aren't overwritten by defaults
374 ptr += 6;
Matt Mower2416a502016-04-12 19:54:46 -0500375 strlcpy(twflags, ptr, sizeof(twflags));
Dees_Troy38bd7602012-09-14 13:33:53 -0400376 } else if (strlen(ptr) == 4 && (strncmp(ptr, "NULL", 4) == 0 || strncmp(ptr, "null", 4) == 0 || strncmp(ptr, "null", 4) == 0)) {
377 // Do nothing
Dees_Troy5bf43922012-09-07 16:07:55 -0400378 } else {
379 // Unhandled data
Matt Mower2b2dd152016-04-26 11:24:08 -0500380 LOGINFO("Unhandled fstab information '%s' in fstab line '%s'\n", ptr, fstab_line);
Dees_Troy5bf43922012-09-07 16:07:55 -0400381 }
382 }
383 while (index < line_len && full_line[index] != '\0')
384 index++;
385 }
386
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600387 // override block devices from the v2 fstab with the ones we read from the twrp.flags file in case they are different
388 if (fstab_version == 2 && twrp_flags && twrp_flags->size() > 0) {
389 it = twrp_flags->find(Mount_Point);
390 if (it != twrp_flags->end()) {
391 if (!it->second.Primary_Block_Device.empty()) {
392 Primary_Block_Device = it->second.Primary_Block_Device;
393 Find_Real_Block_Device(Primary_Block_Device, Display_Error);
394 }
395 if (!it->second.Alternate_Block_Device.empty()) {
396 Alternate_Block_Device = it->second.Alternate_Block_Device;
397 Find_Real_Block_Device(Alternate_Block_Device, Display_Error);
398 }
399 }
400 }
401
402 if (strncmp(fstab_line, "/devices/", strlen("/devices/")) == 0) {
403 Sysfs_Entry = Primary_Block_Device;
404 Primary_Block_Device = "";
405 Is_Storage = true;
406 Removable = true;
407 Wipe_Available_in_GUI = true;
408 Wildcard_Block_Device = true;
409 }
410 if (Primary_Block_Device.find("*") != string::npos)
411 Wildcard_Block_Device = true;
412
413 if (Mount_Point == "auto") {
414 Mount_Point = "/auto";
415 char autoi[5];
416 sprintf(autoi, "%i", auto_index);
417 Mount_Point += autoi;
418 Backup_Path = Mount_Point;
419 Storage_Path = Mount_Point;
420 auto_index++;
421 Setup_File_System(Display_Error);
422 Display_Name = "Storage";
423 Backup_Display_Name = Display_Name;
424 Storage_Name = Display_Name;
425 Can_Be_Backed_Up = false;
426 Wipe_Available_in_GUI = true;
427 Is_Storage = true;
428 Removable = true;
429 Wipe_Available_in_GUI = true;
430 } else if (!Is_File_System(Fstab_File_System) && !Is_Image(Fstab_File_System)) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400431 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000432 LOGERR("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400433 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000434 LOGINFO("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Matt Mower72c87ce2016-04-26 14:34:56 -0500435 return false;
Dees_Troy5bf43922012-09-07 16:07:55 -0400436 } else if (Is_File_System(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400437 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400438 Setup_File_System(Display_Error);
Captain Throwback9d6feb52018-07-27 10:05:24 -0400439 if (Mount_Point == PartitionManager.Get_Android_Root_Path()) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400440 Display_Name = "System";
Dees_Troya13d74f2013-03-24 08:54:55 -0500441 Backup_Display_Name = Display_Name;
442 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400443 Wipe_Available_in_GUI = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500444 Can_Be_Backed_Up = true;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500445 Mount_Read_Only = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400446 } else if (Mount_Point == "/data") {
447 Display_Name = "Data";
Dees_Troya13d74f2013-03-24 08:54:55 -0500448 Backup_Display_Name = Display_Name;
449 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400450 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400451 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500452 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000453 Can_Encrypt_Backup = true;
454 Use_Userdata_Encryption = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400455 } else if (Mount_Point == "/cache") {
456 Display_Name = "Cache";
Dees_Troya13d74f2013-03-24 08:54:55 -0500457 Backup_Display_Name = Display_Name;
458 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400459 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400460 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500461 Can_Be_Backed_Up = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400462 } else if (Mount_Point == "/datadata") {
Dees_Troy51127312012-09-08 13:08:49 -0400463 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400464 Display_Name = "DataData";
Dees_Troya13d74f2013-03-24 08:54:55 -0500465 Backup_Display_Name = Display_Name;
466 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400467 Is_SubPartition = true;
468 SubPartition_Of = "/data";
Dees_Troy5bf43922012-09-07 16:07:55 -0400469 DataManager::SetValue(TW_HAS_DATADATA, 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500470 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000471 Can_Encrypt_Backup = true;
472 Use_Userdata_Encryption = false; // This whole partition should be encrypted
Dees_Troy5bf43922012-09-07 16:07:55 -0400473 } else if (Mount_Point == "/sd-ext") {
Dees_Troy51127312012-09-08 13:08:49 -0400474 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400475 Display_Name = "SD-Ext";
Dees_Troya13d74f2013-03-24 08:54:55 -0500476 Backup_Display_Name = Display_Name;
477 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400478 Wipe_Available_in_GUI = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400479 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500480 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000481 Can_Encrypt_Backup = true;
482 Use_Userdata_Encryption = true;
Dees_Troy2c50e182012-09-26 20:05:28 -0400483 } else if (Mount_Point == "/boot") {
484 Display_Name = "Boot";
Dees_Troya13d74f2013-03-24 08:54:55 -0500485 Backup_Display_Name = Display_Name;
Dees_Troy2c50e182012-09-26 20:05:28 -0400486 DataManager::SetValue("tw_boot_is_mountable", 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500487 Can_Be_Backed_Up = true;
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500488 } else if (Mount_Point == "/vendor") {
489 Display_Name = "Vendor";
490 Backup_Display_Name = Display_Name;
491 Storage_Name = Display_Name;
492 Mount_Read_Only = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400493 }
494#ifdef TW_EXTERNAL_STORAGE_PATH
495 if (Mount_Point == EXPAND(TW_EXTERNAL_STORAGE_PATH)) {
496 Is_Storage = true;
497 Storage_Path = EXPAND(TW_EXTERNAL_STORAGE_PATH);
Dees_Troyc51f1f92012-09-20 15:32:13 -0400498 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500499 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400500#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000501 if (Mount_Point == "/sdcard" || Mount_Point == "/external_sd" || Mount_Point == "/external_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400502 Is_Storage = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400503 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500504 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400505#endif
Dees_Troyb05ddee2013-01-28 20:24:50 +0000506 }
Dees_Troy8170a922012-09-18 15:40:25 -0400507#ifdef TW_INTERNAL_STORAGE_PATH
508 if (Mount_Point == EXPAND(TW_INTERNAL_STORAGE_PATH)) {
509 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500510 Is_Settings_Storage = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400511 Storage_Path = EXPAND(TW_INTERNAL_STORAGE_PATH);
Dees_Troya13d74f2013-03-24 08:54:55 -0500512 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400513 }
514#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000515 if (Mount_Point == "/emmc" || Mount_Point == "/internal_sd" || Mount_Point == "/internal_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400516 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500517 Is_Settings_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500518 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400519 }
520#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400521 } else if (Is_Image(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400522 Find_Actual_Block_Device();
Ethan Yonker1b190162016-12-05 15:25:19 -0600523 Setup_Image();
Dees_Troya13d74f2013-03-24 08:54:55 -0500524 if (Mount_Point == "/boot") {
525 Display_Name = "Boot";
526 Backup_Display_Name = Display_Name;
527 Can_Be_Backed_Up = true;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600528 Can_Flash_Img = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500529 } else if (Mount_Point == "/recovery") {
530 Display_Name = "Recovery";
531 Backup_Display_Name = Display_Name;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600532 Can_Flash_Img = true;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500533 } else if (Mount_Point == "/system_image") {
534 Display_Name = "System Image";
535 Backup_Display_Name = Display_Name;
HashBanged974bb2016-01-30 14:20:09 -0500536 Can_Flash_Img = true;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500537 Can_Be_Backed_Up = true;
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500538 } else if (Mount_Point == "/vendor_image") {
539 Display_Name = "Vendor Image";
540 Backup_Display_Name = Display_Name;
HashBanged974bb2016-01-30 14:20:09 -0500541 Can_Flash_Img = true;
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500542 Can_Be_Backed_Up = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500543 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400544 }
545
Matt Mower2416a502016-04-12 19:54:46 -0500546 // Process TWRP fstab flags
547 if (strlen(twflags) > 0) {
548 string Prev_Display_Name = Display_Name;
549 string Prev_Storage_Name = Storage_Name;
550 string Prev_Backup_Display_Name = Backup_Display_Name;
551 Display_Name = "";
552 Storage_Name = "";
553 Backup_Display_Name = "";
554
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600555 Process_TW_Flags(twflags, (fstab_version == 1), fstab_version);
556 Save_FS_Flags(Fstab_File_System, Mount_Flags, Mount_Options);
Matt Mower2416a502016-04-12 19:54:46 -0500557
558 bool has_display_name = !Display_Name.empty();
559 bool has_storage_name = !Storage_Name.empty();
560 bool has_backup_name = !Backup_Display_Name.empty();
561 if (!has_display_name) Display_Name = Prev_Display_Name;
562 if (!has_storage_name) Storage_Name = Prev_Storage_Name;
563 if (!has_backup_name) Backup_Display_Name = Prev_Backup_Display_Name;
564
565 if (has_display_name && !has_storage_name)
566 Storage_Name = Display_Name;
567 if (!has_display_name && has_storage_name)
568 Display_Name = Storage_Name;
569 if (has_display_name && !has_backup_name && Backup_Display_Name != "Android Secure")
570 Backup_Display_Name = Display_Name;
571 if (!has_display_name && has_backup_name)
572 Display_Name = Backup_Display_Name;
573 }
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600574
575 if (fstab_version == 2 && twrp_flags && twrp_flags->size() > 0) {
576 it = twrp_flags->find(Mount_Point);
577 if (it != twrp_flags->end()) {
578 char twrpflags[MAX_FSTAB_LINE_LENGTH] = "";
579 int skip = 0;
580 string Flags = it->second.Flags;
581 strcpy(twrpflags, Flags.c_str());
582 if (strlen(twrpflags) > strlen("flags=") && strncmp(twrpflags, "flags=", strlen("flags=")) == 0)
583 skip += strlen("flags=");
584 char* flagptr = twrpflags;
585 flagptr += skip;
586 Process_TW_Flags(flagptr, Display_Error, 1); // Forcing the fstab to ver 1 because this data is coming from the /etc/twrp.flags which should be using the TWRP v1 flags format
587 }
588 }
nkk7198fc3992017-12-16 16:26:42 +0200589
590 if (Mount_Point == "/persist" && Can_Be_Mounted) {
591 bool mounted = Is_Mounted();
592 if (mounted || Mount(false)) {
593 // Read the backup settings file
594 DataManager::LoadPersistValues();
595 TWFunc::Fixup_Time_On_Boot("/persist/time/");
596 if (!mounted)
597 UnMount(false);
598 }
599 }
600
Dees_Troy51127312012-09-08 13:08:49 -0400601 return true;
602}
603
Matt Mower72c87ce2016-04-26 14:34:56 -0500604void TWPartition::Partition_Post_Processing(bool Display_Error) {
605 if (Mount_Point == "/data")
606 Setup_Data_Partition(Display_Error);
607 else if (Mount_Point == "/cache")
608 Setup_Cache_Partition(Display_Error);
609}
610
Ethan Yonkerbd7492d2016-12-07 13:55:01 -0600611void TWPartition::ExcludeAll(const string& path) {
612 backup_exclusions.add_absolute_dir(path);
613 wipe_exclusions.add_absolute_dir(path);
614}
615
Matt Mower72c87ce2016-04-26 14:34:56 -0500616void TWPartition::Setup_Data_Partition(bool Display_Error) {
617 if (Mount_Point != "/data")
618 return;
619
620 // Ensure /data is not mounted as tmpfs for qcom hardware decrypt
621 UnMount(false);
622
623#ifdef TW_INCLUDE_CRYPTO
624 if (datamedia)
625 Setup_Data_Media();
626 Can_Be_Encrypted = true;
627 char crypto_blkdev[255];
628 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
629 if (strcmp(crypto_blkdev, "error") != 0) {
630 DataManager::SetValue(TW_IS_DECRYPTED, 1);
631 Is_Encrypted = true;
632 Is_Decrypted = true;
Ethan Yonker93382822018-11-01 15:25:31 -0500633 if (Key_Directory.empty())
634 Is_FBE = false;
635 else
636 Is_FBE = true;
Ethan Yonkerbd7492d2016-12-07 13:55:01 -0600637 DataManager::SetValue(TW_IS_FBE, 0);
Matt Mower72c87ce2016-04-26 14:34:56 -0500638 Decrypted_Block_Device = crypto_blkdev;
639 LOGINFO("Data already decrypted, new block device: '%s'\n", crypto_blkdev);
640 } else if (!Mount(false)) {
641 if (Is_Present) {
Ethan Yonker93382822018-11-01 15:25:31 -0500642 if (Key_Directory.empty()) {
643 set_partition_data(Actual_Block_Device.c_str(), Crypto_Key_Location.c_str(), Fstab_File_System.c_str());
644 if (cryptfs_check_footer() == 0) {
645 Is_Encrypted = true;
646 Is_Decrypted = false;
647 Can_Be_Mounted = false;
648 Current_File_System = "emmc";
649 Setup_Image();
650 DataManager::SetValue(TW_IS_ENCRYPTED, 1);
651 DataManager::SetValue(TW_CRYPTO_PWTYPE, cryptfs_get_password_type());
652 DataManager::SetValue(TW_CRYPTO_PASSWORD, "");
653 DataManager::SetValue("tw_crypto_display", "");
654 } else {
655 gui_err("mount_data_footer=Could not mount /data and unable to find crypto footer.");
656 }
657 } else {
Matt Mower72c87ce2016-04-26 14:34:56 -0500658 Is_Encrypted = true;
659 Is_Decrypted = false;
Matt Mower72c87ce2016-04-26 14:34:56 -0500660 }
Ethan Yonker93382822018-11-01 15:25:31 -0500661 } else if (Key_Directory.empty()) {
Matt Mower72c87ce2016-04-26 14:34:56 -0500662 LOGERR("Primary block device '%s' for mount point '%s' is not present!\n", Primary_Block_Device.c_str(), Mount_Point.c_str());
663 }
664 } else {
mauronofrio9bf73352019-11-23 22:27:34 +0100665
666 if (!Decrypt_FBE_DE()) {
667 LOGINFO("Trying wrapped key.\n");
668 property_set("fbe.data.wrappedkey", "true");
669 if (!Decrypt_FBE_DE()) {
bigbiffd58ba182020-03-23 10:02:29 -0400670 LOGERR("Unable to decrypt FBE device\n");
mauronofrio9bf73352019-11-23 22:27:34 +0100671 }
672 }
Matt Mower72c87ce2016-04-26 14:34:56 -0500673 }
674 if (datamedia && (!Is_Encrypted || (Is_Encrypted && Is_Decrypted))) {
675 Setup_Data_Media();
676 Recreate_Media_Folder();
677 }
678#else
679 if (datamedia) {
680 Setup_Data_Media();
681 Recreate_Media_Folder();
682 }
683#endif
684}
685
Ethan Yonker93382822018-11-01 15:25:31 -0500686bool TWPartition::Decrypt_FBE_DE() {
687if (TWFunc::Path_Exists("/data/unencrypted/key/version")) {
688 LOGINFO("File Based Encryption is present\n");
689#ifdef TW_INCLUDE_FBE
690 ExcludeAll(Mount_Point + "/convert_fbe");
691 ExcludeAll(Mount_Point + "/unencrypted");
692 //ExcludeAll(Mount_Point + "/system/users/0"); // we WILL need to retain some of this if multiple users are present or we just need to delete more folders for the extra users somewhere else
693 ExcludeAll(Mount_Point + "/misc/vold/user_keys");
694 //ExcludeAll(Mount_Point + "/system_ce");
695 //ExcludeAll(Mount_Point + "/system_de");
696 //ExcludeAll(Mount_Point + "/misc_ce");
697 //ExcludeAll(Mount_Point + "/misc_de");
698 ExcludeAll(Mount_Point + "/system/gatekeeper.password.key");
699 ExcludeAll(Mount_Point + "/system/gatekeeper.pattern.key");
700 ExcludeAll(Mount_Point + "/system/locksettings.db");
701 //ExcludeAll(Mount_Point + "/system/locksettings.db-shm"); // don't seem to need this one, but the other 2 are needed
702 ExcludeAll(Mount_Point + "/system/locksettings.db-wal");
703 //ExcludeAll(Mount_Point + "/user_de");
704 //ExcludeAll(Mount_Point + "/misc/profiles/cur/0"); // might be important later
705 ExcludeAll(Mount_Point + "/misc/gatekeeper");
706 ExcludeAll(Mount_Point + "/misc/keystore");
707 ExcludeAll(Mount_Point + "/drm/kek.dat");
708 ExcludeAll(Mount_Point + "/system_de/0/spblob"); // contains data needed to decrypt pixel 2
709 int retry_count = 3;
710 while (!Decrypt_DE() && --retry_count)
711 usleep(2000);
712 if (retry_count > 0) {
713 property_set("ro.crypto.state", "encrypted");
714 Is_Encrypted = true;
715 Is_Decrypted = false;
716 Is_FBE = true;
717 DataManager::SetValue(TW_IS_FBE, 1);
718 DataManager::SetValue(TW_IS_ENCRYPTED, 1);
719 string filename;
720 int pwd_type = Get_Password_Type(0, filename);
721 if (pwd_type < 0) {
722 LOGERR("This TWRP does not have synthetic password decrypt support\n");
723 pwd_type = 0; // default password
724 }
725 DataManager::SetValue(TW_CRYPTO_PWTYPE, pwd_type);
726 DataManager::SetValue(TW_CRYPTO_PASSWORD, "");
727 DataManager::SetValue("tw_crypto_display", "");
728 return true;
729 }
730#else
731 LOGERR("FBE found but FBE support not present in TWRP\n");
732#endif
733 }
734 return false;
735}
736
Matt Mower72c87ce2016-04-26 14:34:56 -0500737void TWPartition::Setup_Cache_Partition(bool Display_Error __unused) {
738 if (Mount_Point != "/cache")
739 return;
740
741 if (!Mount(true))
742 return;
743
744 if (!TWFunc::Path_Exists("/cache/recovery/.")) {
745 LOGINFO("Recreating /cache/recovery folder\n");
746 if (mkdir("/cache/recovery", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0)
747 LOGERR("Could not create /cache/recovery\n");
748 }
749}
750
Matt Mower4ab42b12016-04-21 13:52:18 -0500751void TWPartition::Process_FS_Flags(const char *str) {
752 char *options = strdup(str);
753 char *ptr, *savep;
Hashcode62bd9e02013-11-19 21:59:42 -0800754
Matt Mower4ab42b12016-04-21 13:52:18 -0500755 Mount_Options = "";
Hashcode62bd9e02013-11-19 21:59:42 -0800756
Matt Mower4ab42b12016-04-21 13:52:18 -0500757 // Avoid issues with potentially nested strtok by using strtok_r
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600758 for (ptr = strtok_r(options, ",", &savep); ptr; ptr = strtok_r(NULL, ",", &savep)) {
759 char *equals = strstr(ptr, "=");
760 size_t name_len;
Matt Mower4ab42b12016-04-21 13:52:18 -0500761
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600762 if (!equals)
763 name_len = strlen(ptr);
764 else
765 name_len = equals - ptr;
766
767 // There are some flags that we want to ignore in TWRP
768 bool found_match = false;
769 for (const char** ignored_mount_item = ignored_mount_items; *ignored_mount_item; ignored_mount_item++) {
770 if (strncmp(ptr, *ignored_mount_item, name_len) == 0) {
771 found_match = true;
Hashcode62bd9e02013-11-19 21:59:42 -0800772 break;
773 }
774 }
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600775 if (found_match)
776 continue;
Hashcode62bd9e02013-11-19 21:59:42 -0800777
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600778 // mount_flags are never postfixed by '='
779 if (!equals) {
780 const struct flag_list* mount_flag = mount_flags;
781 for (; mount_flag->name; mount_flag++) {
782 if (strcmp(ptr, mount_flag->name) == 0) {
783 if (mount_flag->flag == MS_RDONLY)
784 Mount_Read_Only = true;
785 else
786 Mount_Flags |= (unsigned)mount_flag->flag;
787 found_match = true;
788 break;
789 }
790 }
791 if (found_match)
792 continue;
Matt Mower4ab42b12016-04-21 13:52:18 -0500793 }
794
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600795 // If we aren't ignoring this flag and it's not a mount flag, then it must be a mount option
796 if (!Mount_Options.empty())
797 Mount_Options += ",";
798 Mount_Options += ptr;
Matt Mower4ab42b12016-04-21 13:52:18 -0500799 }
800 free(options);
Hashcode62bd9e02013-11-19 21:59:42 -0800801}
802
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600803void TWPartition::Save_FS_Flags(const string& local_File_System, int local_Mount_Flags, const string& local_Mount_Options) {
804 partition_fs_flags_struct flags;
805 flags.File_System = local_File_System;
806 flags.Mount_Flags = local_Mount_Flags;
807 flags.Mount_Options = local_Mount_Options;
808 fs_flags.push_back(flags);
809}
810
Matt Mower2416a502016-04-12 19:54:46 -0500811void TWPartition::Apply_TW_Flag(const unsigned flag, const char* str, const bool val) {
812 switch (flag) {
813 case TWFLAG_ANDSEC:
814 Has_Android_Secure = val;
815 break;
816 case TWFLAG_BACKUP:
817 Can_Be_Backed_Up = val;
818 break;
819 case TWFLAG_BACKUPNAME:
820 Backup_Display_Name = str;
821 break;
822 case TWFLAG_BLOCKSIZE:
823 Format_Block_Size = (unsigned long)(atol(str));
824 break;
825 case TWFLAG_CANBEWIPED:
826 Can_Be_Wiped = val;
827 break;
828 case TWFLAG_CANENCRYPTBACKUP:
829 Can_Encrypt_Backup = val;
830 break;
831 case TWFLAG_DEFAULTS:
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600832 case TWFLAG_WAIT:
833 case TWFLAG_VERIFY:
834 case TWFLAG_CHECK:
835 case TWFLAG_NOTRIM:
836 case TWFLAG_VOLDMANAGED:
837 case TWFLAG_RESIZE:
Matt Mower2416a502016-04-12 19:54:46 -0500838 // Do nothing
839 break;
840 case TWFLAG_DISPLAY:
841 Display_Name = str;
842 break;
843 case TWFLAG_ENCRYPTABLE:
844 case TWFLAG_FORCEENCRYPT:
845 Crypto_Key_Location = str;
846 break;
Ethan Yonkere9afc3d2018-08-30 15:16:27 -0500847 case TWFLAG_FILEENCRYPTION:
848 // This flag isn't used by TWRP but is needed in 9.0 FBE decrypt
849 // fileencryption=ice:aes-256-heh
850 {
851 std::string FBE = str;
Ethan Yonkere9afc3d2018-08-30 15:16:27 -0500852 size_t colon_loc = FBE.find(":");
853 if (colon_loc == std::string::npos) {
Ethan Yonker93382822018-11-01 15:25:31 -0500854 property_set("fbe.contents", FBE.c_str());
855 property_set("fbe.filenames", "");
856 LOGINFO("FBE contents '%s', filenames ''\n", FBE.c_str());
Ethan Yonkere9afc3d2018-08-30 15:16:27 -0500857 break;
858 }
Ethan Yonker93382822018-11-01 15:25:31 -0500859 std::string FBE_contents, FBE_filenames;
Ethan Yonkere9afc3d2018-08-30 15:16:27 -0500860 FBE_contents = FBE.substr(0, colon_loc);
861 FBE_filenames = FBE.substr(colon_loc + 1);
862 property_set("fbe.contents", FBE_contents.c_str());
863 property_set("fbe.filenames", FBE_filenames.c_str());
864 LOGINFO("FBE contents '%s', filenames '%s'\n", FBE_contents.c_str(), FBE_filenames.c_str());
865 }
866 break;
Matt Mower2416a502016-04-12 19:54:46 -0500867 case TWFLAG_FLASHIMG:
868 Can_Flash_Img = val;
869 break;
870 case TWFLAG_FSFLAGS:
Matt Mower4ab42b12016-04-21 13:52:18 -0500871 Process_FS_Flags(str);
Matt Mower2416a502016-04-12 19:54:46 -0500872 break;
873 case TWFLAG_IGNOREBLKID:
874 Ignore_Blkid = val;
875 break;
876 case TWFLAG_LENGTH:
877 Length = atoi(str);
878 break;
879 case TWFLAG_MOUNTTODECRYPT:
880 Mount_To_Decrypt = val;
881 break;
882 case TWFLAG_REMOVABLE:
883 Removable = val;
884 break;
885 case TWFLAG_RETAINLAYOUTVERSION:
886 Retain_Layout_Version = val;
887 break;
888 case TWFLAG_SETTINGSSTORAGE:
889 Is_Settings_Storage = val;
890 if (Is_Settings_Storage)
891 Is_Storage = true;
892 break;
893 case TWFLAG_STORAGE:
894 Is_Storage = val;
895 break;
896 case TWFLAG_STORAGENAME:
897 Storage_Name = str;
898 break;
899 case TWFLAG_SUBPARTITIONOF:
900 Is_SubPartition = true;
901 SubPartition_Of = str;
902 break;
903 case TWFLAG_SYMLINK:
904 Symlink_Path = str;
905 break;
906 case TWFLAG_USERDATAENCRYPTBACKUP:
907 Use_Userdata_Encryption = val;
908 if (Use_Userdata_Encryption)
909 Can_Encrypt_Backup = true;
910 break;
911 case TWFLAG_USERMRF:
912 Use_Rm_Rf = val;
913 break;
914 case TWFLAG_WIPEDURINGFACTORYRESET:
915 Wipe_During_Factory_Reset = val;
916 if (Wipe_During_Factory_Reset) {
917 Can_Be_Wiped = true;
918 Wipe_Available_in_GUI = true;
919 }
920 break;
921 case TWFLAG_WIPEINGUI:
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600922 case TWFLAG_FORMATTABLE:
Matt Mower2416a502016-04-12 19:54:46 -0500923 Wipe_Available_in_GUI = val;
924 if (Wipe_Available_in_GUI)
925 Can_Be_Wiped = true;
926 break;
Ethan Yonker1b190162016-12-05 15:25:19 -0600927 case TWFLAG_SLOTSELECT:
928 SlotSelect = true;
929 break;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600930 case TWFLAG_ALTDEVICE:
931 Alternate_Block_Device = str;
932 break;
Ethan Yonker93382822018-11-01 15:25:31 -0500933 case TWFLAG_KEYDIRECTORY:
934 Key_Directory = str;
bigbiffd58ba182020-03-23 10:02:29 -0400935 break;
Matt Mower2416a502016-04-12 19:54:46 -0500936 default:
937 // Should not get here
938 LOGINFO("Flag identified for processing, but later unmatched: %i\n", flag);
939 break;
940 }
941}
Dees_Troy51127312012-09-08 13:08:49 -0400942
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600943void TWPartition::Process_TW_Flags(char *flags, bool Display_Error, int fstab_ver) {
Matt Mower2416a502016-04-12 19:54:46 -0500944 char separator[2] = {'\n', 0};
945 char *ptr, *savep;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600946 char source_separator = ';';
947
948 if (fstab_ver == 2)
949 source_separator = ',';
Matt Mower2416a502016-04-12 19:54:46 -0500950
951 // Semicolons within double-quotes are not forbidden, so replace
952 // only the semicolons intended as separators with '\n' for strtok
953 for (unsigned i = 0, skip = 0; i < strlen(flags); i++) {
954 if (flags[i] == '\"')
Dees_Troya13d74f2013-03-24 08:54:55 -0500955 skip = !skip;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600956 if (!skip && flags[i] == source_separator)
Matt Mower2416a502016-04-12 19:54:46 -0500957 flags[i] = separator[0];
Dees_Troy51127312012-09-08 13:08:49 -0400958 }
959
Matt Mower2416a502016-04-12 19:54:46 -0500960 // Avoid issues with potentially nested strtok by using strtok_r
961 ptr = strtok_r(flags, separator, &savep);
962 while (ptr) {
963 int ptr_len = strlen(ptr);
964 const struct flag_list* tw_flag = tw_flags;
Hashcode62bd9e02013-11-19 21:59:42 -0800965
Matt Mower2416a502016-04-12 19:54:46 -0500966 for (; tw_flag->name; tw_flag++) {
967 int flag_len = strlen(tw_flag->name);
968
969 if (strncmp(ptr, tw_flag->name, flag_len) == 0) {
970 bool flag_val = false;
971
972 if (ptr_len > flag_len && (tw_flag->name)[flag_len-1] != '='
973 && ptr[flag_len] != '=') {
974 // Handle flags with same starting string
975 // (e.g. backup and backupname)
976 continue;
977 } else if (ptr_len > flag_len && ptr[flag_len] == '=') {
978 // Handle flags with dual format: Part 1
979 // (e.g. backup and backup=y. backup=y handled here)
980 ptr += flag_len + 1;
981 TWFunc::Strip_Quotes(ptr);
982 // Skip flags with empty argument
983 // (e.g. backup=)
984 if (strlen(ptr) == 0) {
985 LOGINFO("Flag missing argument or should not include '=': %s=\n", tw_flag->name);
986 break;
987 }
988 flag_val = strchr("yY1", *ptr) != NULL;
989 } else if (ptr_len == flag_len
990 && (tw_flag->name)[flag_len-1] == '=') {
991 // Skip flags missing argument after =
992 // (e.g. backupname=)
993 LOGINFO("Flag missing argument: %s\n", tw_flag->name);
994 break;
995 } else if (ptr_len > flag_len
996 && (tw_flag->name)[flag_len-1] == '=') {
997 // Handle arguments to flags
998 // (e.g. backupname="My Stuff")
999 ptr += flag_len;
1000 TWFunc::Strip_Quotes(ptr);
1001 // Skip flags with empty argument
1002 // (e.g. backupname="")
1003 if (strlen(ptr) == 0) {
1004 LOGINFO("Flag missing argument: %s\n", tw_flag->name);
1005 break;
1006 }
1007 } else if (ptr_len == flag_len) {
1008 // Handle flags with dual format: Part 2
1009 // (e.g. backup and backup=y. backup handled here)
1010 flag_val = true;
Ethan Yonker96af84a2015-01-05 14:58:36 -06001011 } else {
Matt Mower2416a502016-04-12 19:54:46 -05001012 LOGINFO("Flag matched, but could not be processed: %s\n", ptr);
1013 break;
Ethan Yonker96af84a2015-01-05 14:58:36 -06001014 }
Matt Mower2416a502016-04-12 19:54:46 -05001015
1016 Apply_TW_Flag(tw_flag->flag, ptr, flag_val);
1017 break;
Ethan Yonker96af84a2015-01-05 14:58:36 -06001018 }
Matt Mower2416a502016-04-12 19:54:46 -05001019 }
1020 if (tw_flag->name == 0) {
Dees_Troy51127312012-09-08 13:08:49 -04001021 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001022 LOGERR("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -04001023 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001024 LOGINFO("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -04001025 }
Matt Mower2416a502016-04-12 19:54:46 -05001026 ptr = strtok_r(NULL, separator, &savep);
Dees_Troy51127312012-09-08 13:08:49 -04001027 }
Dees_Troy51a0e822012-09-05 15:24:24 -04001028}
1029
Dees_Troy5bf43922012-09-07 16:07:55 -04001030bool TWPartition::Is_File_System(string File_System) {
1031 if (File_System == "ext2" ||
Dees_Troy63c8df72012-09-10 14:02:05 -04001032 File_System == "ext3" ||
Dees_Troy5bf43922012-09-07 16:07:55 -04001033 File_System == "ext4" ||
1034 File_System == "vfat" ||
1035 File_System == "ntfs" ||
1036 File_System == "yaffs2" ||
bigbiff bigbiff3e146522012-11-14 14:32:59 -05001037 File_System == "exfat" ||
Dees_Troye5017042013-08-29 16:38:55 +00001038 File_System == "f2fs" ||
Erfan Abdib859e912019-02-05 18:18:46 +02001039 File_System == "squashfs" ||
Dees_Troy5bf43922012-09-07 16:07:55 -04001040 File_System == "auto")
1041 return true;
1042 else
1043 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001044}
1045
Dees_Troy5bf43922012-09-07 16:07:55 -04001046bool TWPartition::Is_Image(string File_System) {
Dees_Troy5fcd8f92012-10-16 12:22:05 -04001047 if (File_System == "emmc" || File_System == "mtd" || File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -04001048 return true;
1049 else
1050 return false;
1051}
1052
Dees_Troy51127312012-09-08 13:08:49 -04001053bool TWPartition::Make_Dir(string Path, bool Display_Error) {
Ethan Yonker93ac7a02016-11-07 22:05:58 -06001054 if (TWFunc::Get_D_Type_From_Stat(Path) != S_IFDIR)
1055 unlink(Path.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001056 if (!TWFunc::Path_Exists(Path)) {
Dees_Troy51127312012-09-08 13:08:49 -04001057 if (mkdir(Path.c_str(), 0777) == -1) {
1058 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001059 gui_msg(Msg(msg::kError, "create_folder_strerr=Can not create '{1}' folder ({2}).")(Path)(strerror(errno)));
Dees_Troy51127312012-09-08 13:08:49 -04001060 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001061 LOGINFO("Can not create '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -04001062 return false;
1063 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001064 LOGINFO("Created '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -04001065 return true;
1066 }
1067 }
1068 return true;
1069}
1070
Dees_Troy5bf43922012-09-07 16:07:55 -04001071void TWPartition::Setup_File_System(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001072 Can_Be_Mounted = true;
1073 Can_Be_Wiped = true;
1074
Dees_Troy5bf43922012-09-07 16:07:55 -04001075 // Make the mount point folder if it doesn't exist
Dees_Troy51127312012-09-08 13:08:49 -04001076 Make_Dir(Mount_Point, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -04001077 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
1078 Backup_Name = Display_Name;
bigbiffce8f83c2015-12-12 18:30:21 -05001079 Backup_Method = BM_FILES;
Dees_Troy5bf43922012-09-07 16:07:55 -04001080}
1081
Ethan Yonker1b190162016-12-05 15:25:19 -06001082void TWPartition::Setup_Image() {
Dees_Troy5bf43922012-09-07 16:07:55 -04001083 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
1084 Backup_Name = Display_Name;
Gary Peck82599a82012-11-21 16:23:12 -08001085 if (Current_File_System == "emmc")
bigbiffce8f83c2015-12-12 18:30:21 -05001086 Backup_Method = BM_DD;
Gary Peck82599a82012-11-21 16:23:12 -08001087 else if (Current_File_System == "mtd" || Current_File_System == "bml")
bigbiffce8f83c2015-12-12 18:30:21 -05001088 Backup_Method = BM_FLASH_UTILS;
Dees_Troy5bf43922012-09-07 16:07:55 -04001089 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001090 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 -04001091}
1092
Dees_Troye58d5262012-09-21 12:27:57 -04001093void TWPartition::Setup_AndSec(void) {
Dees_Troya13d74f2013-03-24 08:54:55 -05001094 Backup_Display_Name = "Android Secure";
Dees_Troye58d5262012-09-21 12:27:57 -04001095 Backup_Name = "and-sec";
Dees_Troya13d74f2013-03-24 08:54:55 -05001096 Can_Be_Backed_Up = true;
Dees_Troye58d5262012-09-21 12:27:57 -04001097 Has_Android_Secure = true;
1098 Symlink_Path = Mount_Point + "/.android_secure";
1099 Symlink_Mount_Point = "/and-sec";
1100 Backup_Path = Symlink_Mount_Point;
1101 Make_Dir("/and-sec", true);
1102 Recreate_AndSec_Folder();
Kjell Braden3126a112016-06-19 16:58:15 +00001103 Mount_Storage_Retry(true);
Dees_Troye58d5262012-09-21 12:27:57 -04001104}
1105
that9e0593e2014-10-08 00:01:24 +02001106void TWPartition::Setup_Data_Media() {
Ethan Yonker6277c792014-09-15 14:54:30 -05001107 LOGINFO("Setting up '%s' as data/media emulated storage.\n", Mount_Point.c_str());
Ethan Yonker66a19492015-12-10 10:19:45 -06001108 if (Storage_Name.empty() || Storage_Name == "Data")
1109 Storage_Name = "Internal Storage";
Ethan Yonker6277c792014-09-15 14:54:30 -05001110 Has_Data_Media = true;
1111 Is_Storage = true;
Ethan Yonker66a19492015-12-10 10:19:45 -06001112 Storage_Path = Mount_Point + "/media";
Ethan Yonker6277c792014-09-15 14:54:30 -05001113 Symlink_Path = Storage_Path;
Ethan Yonker66a19492015-12-10 10:19:45 -06001114 if (Mount_Point == "/data") {
1115 Is_Settings_Storage = true;
1116 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
1117 Make_Dir("/emmc", false);
1118 Symlink_Mount_Point = "/emmc";
1119 } else {
1120 Make_Dir("/sdcard", false);
1121 Symlink_Mount_Point = "/sdcard";
1122 }
1123 if (Mount(false) && TWFunc::Path_Exists(Mount_Point + "/media/0")) {
1124 Storage_Path = Mount_Point + "/media/0";
1125 Symlink_Path = Storage_Path;
1126 DataManager::SetValue(TW_INTERNAL_PATH, Mount_Point + "/media/0");
1127 UnMount(true);
1128 }
1129 DataManager::SetValue("tw_has_internal", 1);
1130 DataManager::SetValue("tw_has_data_media", 1);
Ethan Yonker3fdcda42016-11-30 12:29:37 -06001131 backup_exclusions.add_absolute_dir("/data/data/com.google.android.music/files");
Ethan Yonker6355b562017-05-01 09:42:17 -05001132 wipe_exclusions.add_absolute_dir(Mount_Point + "/misc/vold"); // adopted storage keys
Ethan Yonker79f88bd2016-12-09 14:52:12 -06001133 ExcludeAll(Mount_Point + "/.layout_version");
1134 ExcludeAll(Mount_Point + "/system/storage.xml");
Ethan Yonker6277c792014-09-15 14:54:30 -05001135 } else {
Ethan Yonker66a19492015-12-10 10:19:45 -06001136 if (Mount(true) && TWFunc::Path_Exists(Mount_Point + "/media/0")) {
1137 Storage_Path = Mount_Point + "/media/0";
1138 Symlink_Path = Storage_Path;
1139 UnMount(true);
1140 }
Ethan Yonker6277c792014-09-15 14:54:30 -05001141 }
Ethan Yonker79f88bd2016-12-09 14:52:12 -06001142 ExcludeAll(Mount_Point + "/media");
Ethan Yonker6277c792014-09-15 14:54:30 -05001143}
1144
Dees_Troy5bf43922012-09-07 16:07:55 -04001145void TWPartition::Find_Real_Block_Device(string& Block, bool Display_Error) {
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06001146 char device[PATH_MAX], realDevice[PATH_MAX];
Dees_Troy5bf43922012-09-07 16:07:55 -04001147
1148 strcpy(device, Block.c_str());
1149 memset(realDevice, 0, sizeof(realDevice));
1150 while (readlink(device, realDevice, sizeof(realDevice)) > 0)
1151 {
1152 strcpy(device, realDevice);
1153 memset(realDevice, 0, sizeof(realDevice));
1154 }
1155
1156 if (device[0] != '/') {
1157 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001158 LOGERR("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001159 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001160 LOGINFO("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001161 return;
Dees_Troy51a0e822012-09-05 15:24:24 -04001162 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -04001163 Block = device;
1164 return;
1165 }
1166}
1167
Kjell Braden3126a112016-06-19 16:58:15 +00001168bool TWPartition::Mount_Storage_Retry(bool Display_Error) {
Dees_Troy8e337f32012-10-13 22:07:49 -04001169 // On some devices, storage doesn't want to mount right away, retry and sleep
Kjell Braden3126a112016-06-19 16:58:15 +00001170 if (!Mount(Display_Error)) {
Dees_Troy8e337f32012-10-13 22:07:49 -04001171 int retry_count = 5;
1172 while (retry_count > 0 && !Mount(false)) {
1173 usleep(500000);
1174 retry_count--;
1175 }
Kjell Braden3126a112016-06-19 16:58:15 +00001176 return Mount(Display_Error);
Dees_Troy8e337f32012-10-13 22:07:49 -04001177 }
Kjell Braden3126a112016-06-19 16:58:15 +00001178 return true;
Dees_Troy8e337f32012-10-13 22:07:49 -04001179}
1180
Dees_Troy38bd7602012-09-14 13:33:53 -04001181bool TWPartition::Find_MTD_Block_Device(string MTD_Name) {
1182 FILE *fp = NULL;
1183 char line[255];
1184
1185 fp = fopen("/proc/mtd", "rt");
1186 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001187 LOGERR("Device does not support /proc/mtd\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001188 return false;
1189 }
1190
1191 while (fgets(line, sizeof(line), fp) != NULL)
1192 {
1193 char device[32], label[32];
1194 unsigned long size = 0;
Dees_Troy38bd7602012-09-14 13:33:53 -04001195 int deviceId;
1196
1197 sscanf(line, "%s %lx %*s %*c%s", device, &size, label);
1198
1199 // Skip header and blank lines
1200 if ((strcmp(device, "dev:") == 0) || (strlen(line) < 8))
1201 continue;
1202
1203 // Strip off the trailing " from the label
1204 label[strlen(label)-1] = '\0';
1205
1206 if (strcmp(label, MTD_Name.c_str()) == 0) {
1207 // We found our device
1208 // Strip off the trailing : from the device
1209 device[strlen(device)-1] = '\0';
1210 if (sscanf(device,"mtd%d", &deviceId) == 1) {
1211 sprintf(device, "/dev/block/mtdblock%d", deviceId);
1212 Primary_Block_Device = device;
Dees_Troy76543db2013-06-19 16:24:30 +00001213 fclose(fp);
1214 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -04001215 }
1216 }
1217 }
1218 fclose(fp);
1219
1220 return false;
1221}
1222
Dees_Troy51127312012-09-08 13:08:49 -04001223bool TWPartition::Get_Size_Via_statfs(bool Display_Error) {
1224 struct statfs st;
1225 string Local_Path = Mount_Point + "/.";
1226
1227 if (!Mount(Display_Error))
1228 return false;
1229
1230 if (statfs(Local_Path.c_str(), &st) != 0) {
1231 if (!Removable) {
1232 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001233 LOGERR("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -04001234 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001235 LOGINFO("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -04001236 }
1237 return false;
1238 }
1239 Size = (st.f_blocks * st.f_bsize);
1240 Used = ((st.f_blocks - st.f_bfree) * st.f_bsize);
1241 Free = (st.f_bfree * st.f_bsize);
1242 Backup_Size = Used;
1243 return true;
1244}
1245
1246bool TWPartition::Get_Size_Via_df(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001247 FILE* fp;
1248 char command[255], line[512];
1249 int include_block = 1;
1250 unsigned int min_len;
1251
1252 if (!Mount(Display_Error))
1253 return false;
1254
Dees_Troy38bd7602012-09-14 13:33:53 -04001255 min_len = Actual_Block_Device.size() + 2;
Dees_Troy51127312012-09-08 13:08:49 -04001256 sprintf(command, "df %s > /tmp/dfoutput.txt", Mount_Point.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001257 TWFunc::Exec_Cmd(command);
Dees_Troy51127312012-09-08 13:08:49 -04001258 fp = fopen("/tmp/dfoutput.txt", "rt");
1259 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001260 LOGINFO("Unable to open /tmp/dfoutput.txt.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001261 return false;
Dees_Troy51127312012-09-08 13:08:49 -04001262 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001263
1264 while (fgets(line, sizeof(line), fp) != NULL)
1265 {
1266 unsigned long blocks, used, available;
1267 char device[64];
1268 char tmpString[64];
1269
1270 if (strncmp(line, "Filesystem", 10) == 0)
1271 continue;
1272 if (strlen(line) < min_len) {
1273 include_block = 0;
1274 continue;
1275 }
1276 if (include_block) {
1277 sscanf(line, "%s %lu %lu %lu", device, &blocks, &used, &available);
1278 } else {
1279 // The device block string is so long that the df information is on the next line
1280 int space_count = 0;
Dees_Troye58d5262012-09-21 12:27:57 -04001281 sprintf(tmpString, "/dev/block/%s", Actual_Block_Device.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001282 while (tmpString[space_count] == 32)
1283 space_count++;
1284 sscanf(line + space_count, "%lu %lu %lu", &blocks, &used, &available);
1285 }
1286
1287 // Adjust block size to byte size
1288 Size = blocks * 1024ULL;
1289 Used = used * 1024ULL;
1290 Free = available * 1024ULL;
1291 Backup_Size = Used;
1292 }
1293 fclose(fp);
1294 return true;
1295}
1296
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001297unsigned long long TWPartition::IOCTL_Get_Block_Size() {
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001298 Find_Actual_Block_Device();
Ethan Yonker483e9f42016-01-11 22:21:18 -06001299
1300 return TWFunc::IOCTL_Get_Block_Size(Actual_Block_Device.c_str());
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001301}
1302
Dees_Troy5bf43922012-09-07 16:07:55 -04001303bool TWPartition::Find_Partition_Size(void) {
1304 FILE* fp;
1305 char line[512];
1306 string tmpdevice;
1307
igoriok87e3d932013-01-31 21:03:53 +02001308 fp = fopen("/proc/dumchar_info", "rt");
1309 if (fp != NULL) {
1310 while (fgets(line, sizeof(line), fp) != NULL)
1311 {
1312 char label[32], device[32];
1313 unsigned long size = 0;
1314
thatd43bf2d2014-09-21 23:13:02 +02001315 sscanf(line, "%s %lx %*x %*u %s", label, &size, device);
igoriok87e3d932013-01-31 21:03:53 +02001316
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05001317 // Skip header, annotation and blank lines
igoriok87e3d932013-01-31 21:03:53 +02001318 if ((strncmp(device, "/dev/", 5) != 0) || (strlen(line) < 8))
1319 continue;
1320
1321 tmpdevice = "/dev/";
1322 tmpdevice += label;
1323 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
1324 Size = size;
1325 fclose(fp);
1326 return true;
1327 }
1328 }
1329 }
1330
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001331 unsigned long long ioctl_size = IOCTL_Get_Block_Size();
1332 if (ioctl_size) {
1333 Size = ioctl_size;
1334 return true;
1335 }
1336
Dees_Troy5bf43922012-09-07 16:07:55 -04001337 // In this case, we'll first get the partitions we care about (with labels)
1338 fp = fopen("/proc/partitions", "rt");
1339 if (fp == NULL)
1340 return false;
1341
1342 while (fgets(line, sizeof(line), fp) != NULL)
1343 {
1344 unsigned long major, minor, blocks;
1345 char device[512];
Dees_Troy5bf43922012-09-07 16:07:55 -04001346
Dees_Troy63c8df72012-09-10 14:02:05 -04001347 if (strlen(line) < 7 || line[0] == 'm') continue;
Dees_Troy5bf43922012-09-07 16:07:55 -04001348 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
1349
1350 tmpdevice = "/dev/block/";
1351 tmpdevice += device;
Dees_Troy38bd7602012-09-14 13:33:53 -04001352 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001353 // Adjust block size to byte size
1354 Size = blocks * 1024ULL;
1355 fclose(fp);
1356 return true;
1357 }
1358 }
1359 fclose(fp);
1360 return false;
1361}
1362
Dees_Troy5bf43922012-09-07 16:07:55 -04001363bool TWPartition::Is_Mounted(void) {
1364 if (!Can_Be_Mounted)
1365 return false;
1366
1367 struct stat st1, st2;
1368 string test_path;
1369
1370 // Check to see if the mount point directory exists
1371 test_path = Mount_Point + "/.";
1372 if (stat(test_path.c_str(), &st1) != 0) return false;
1373
1374 // Check to see if the directory above the mount point exists
1375 test_path = Mount_Point + "/../.";
1376 if (stat(test_path.c_str(), &st2) != 0) return false;
1377
1378 // Compare the device IDs -- if they match then we're (probably) using tmpfs instead of an actual device
1379 int ret = (st1.st_dev != st2.st_dev) ? true : false;
1380
1381 return ret;
1382}
1383
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05001384bool TWPartition::Is_File_System_Writable(void) {
1385 if (!Is_File_System(Current_File_System) || !Is_Mounted())
1386 return false;
1387
1388 string test_path = Mount_Point + "/.";
1389 return (access(test_path.c_str(), W_OK) == 0);
1390}
1391
Dees_Troy5bf43922012-09-07 16:07:55 -04001392bool TWPartition::Mount(bool Display_Error) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001393 int exfat_mounted = 0;
Martin Dünkelmann6d843782018-12-27 23:40:28 +01001394 unsigned int flags = Mount_Flags;
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001395
Dees_Troy5bf43922012-09-07 16:07:55 -04001396 if (Is_Mounted()) {
1397 return true;
1398 } else if (!Can_Be_Mounted) {
1399 return false;
1400 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001401
1402 Find_Actual_Block_Device();
1403
1404 // Check the current file system before mounting
1405 Check_FS_Type();
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001406 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/exfat-fuse")) {
Dees_Troye34c1332013-02-06 19:13:00 +00001407 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 +00001408 LOGINFO("cmd: %s\n", cmd.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001409 string result;
1410 if (TWFunc::Exec_Cmd(cmd, result) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001411 LOGINFO("exfat-fuse failed to mount with result '%s', trying vfat\n", result.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001412 Current_File_System = "vfat";
1413 } else {
1414#ifdef TW_NO_EXFAT_FUSE
1415 UnMount(false);
1416 // We'll let the kernel handle it but using exfat-fuse to detect if the file system is actually exfat
1417 // Some kernels let us mount vfat as exfat which doesn't work out too well
1418#else
1419 exfat_mounted = 1;
1420#endif
1421 }
1422 }
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001423
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001424 if (Current_File_System == "ntfs" && (TWFunc::Path_Exists("/sbin/ntfs-3g") || TWFunc::Path_Exists("/sbin/mount.ntfs"))) {
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001425 string cmd;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001426 string Ntfsmount_Binary = "";
1427
1428 if (TWFunc::Path_Exists("/sbin/ntfs-3g"))
1429 Ntfsmount_Binary = "ntfs-3g";
1430 else if (TWFunc::Path_Exists("/sbin/mount.ntfs"))
1431 Ntfsmount_Binary = "mount.ntfs";
1432
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001433 if (Mount_Read_Only)
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001434 cmd = "/sbin/" + Ntfsmount_Binary + " -o ro " + Actual_Block_Device + " " + Mount_Point;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001435 else
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001436 cmd = "/sbin/" + Ntfsmount_Binary + " " + Actual_Block_Device + " " + Mount_Point;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001437 LOGINFO("cmd: '%s'\n", cmd.c_str());
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001438
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001439 if (TWFunc::Exec_Cmd(cmd) == 0) {
1440 return true;
1441 } else {
1442 LOGINFO("ntfs-3g failed to mount, trying regular mount method.\n");
1443 }
1444 }
1445
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001446 if (Mount_Read_Only)
1447 flags |= MS_RDONLY;
1448
Dees_Troy22042032012-12-18 21:23:08 +00001449 if (Fstab_File_System == "yaffs2") {
1450 // mount an MTD partition as a YAFFS2 filesystem.
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001451 flags = MS_NOATIME | MS_NODEV | MS_NODIRATIME;
1452 if (Mount_Read_Only)
1453 flags |= MS_RDONLY;
Dees_Troy76543db2013-06-19 16:24:30 +00001454 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags, NULL) < 0) {
1455 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags | MS_RDONLY, NULL) < 0) {
1456 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001457 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy76543db2013-06-19 16:24:30 +00001458 else
1459 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
1460 return false;
1461 } else {
1462 LOGINFO("Mounted '%s' (MTD) as RO\n", Mount_Point.c_str());
1463 return true;
1464 }
1465 } else {
1466 struct stat st;
1467 string test_path = Mount_Point;
1468 if (stat(test_path.c_str(), &st) < 0) {
1469 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001470 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy76543db2013-06-19 16:24:30 +00001471 else
1472 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
1473 return false;
1474 }
1475 mode_t new_mode = st.st_mode | S_IXUSR | S_IXGRP | S_IXOTH;
1476 if (new_mode != st.st_mode) {
1477 LOGINFO("Fixing execute permissions for %s\n", Mount_Point.c_str());
1478 if (chmod(Mount_Point.c_str(), new_mode) < 0) {
1479 if (Display_Error)
1480 LOGERR("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
1481 else
1482 LOGINFO("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
1483 return false;
1484 }
1485 }
Dees_Troy22042032012-12-18 21:23:08 +00001486 return true;
Dees_Troy76543db2013-06-19 16:24:30 +00001487 }
thatc7572eb2015-03-31 18:55:30 +02001488 }
1489
1490 string mount_fs = Current_File_System;
1491 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sys/module/texfat"))
1492 mount_fs = "texfat";
1493
1494 if (!exfat_mounted &&
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001495 mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), mount_fs.c_str(), flags, Mount_Options.c_str()) != 0 &&
1496 mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), mount_fs.c_str(), flags, NULL) != 0) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001497#ifdef TW_NO_EXFAT_FUSE
1498 if (Current_File_System == "exfat") {
Dees_Troy2673cec2013-04-02 20:22:16 +00001499 LOGINFO("Mounting exfat failed, trying vfat...\n");
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001500 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), "vfat", 0, NULL) != 0) {
Dees_Troy85f44ed2013-01-09 18:42:36 +00001501 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_Troy85f44ed2013-01-09 18:42:36 +00001503 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001504 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001505 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 +00001506 return false;
Dees_Troy85f44ed2013-01-09 18:42:36 +00001507 }
Dees_Troyb05ddee2013-01-28 20:24:50 +00001508 } else {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001509#endif
bigbiff bigbiff26774a02014-03-29 18:22:00 -04001510 if (!Removable && Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001511 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001512 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001513 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
1514 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 +00001515 return false;
1516#ifdef TW_NO_EXFAT_FUSE
Dees_Troy85f44ed2013-01-09 18:42:36 +00001517 }
1518#endif
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001519 }
Ethan Yonker253368a2014-11-25 15:00:52 -06001520
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001521 if (Removable)
1522 Update_Size(Display_Error);
1523
xiaolu9416f4f2015-06-04 08:22:23 +08001524 if (!Symlink_Mount_Point.empty() && TWFunc::Path_Exists(Symlink_Path)) {
codelover2a3d4ce2015-03-14 20:26:49 +08001525 string Command = "mount -o bind '" + Symlink_Path + "' '" + Symlink_Mount_Point + "'";
Vojtech Bocek05534202013-09-11 08:11:56 +02001526 TWFunc::Exec_Cmd(Command);
Dees_Troy5bf43922012-09-07 16:07:55 -04001527 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001528 return true;
1529}
1530
1531bool TWPartition::UnMount(bool Display_Error) {
1532 if (Is_Mounted()) {
1533 int never_unmount_system;
1534
1535 DataManager::GetValue(TW_DONT_UNMOUNT_SYSTEM, never_unmount_system);
Captain Throwback9d6feb52018-07-27 10:05:24 -04001536 if (never_unmount_system == 1 && Mount_Point == PartitionManager.Get_Android_Root_Path())
Dees_Troy5bf43922012-09-07 16:07:55 -04001537 return true; // Never unmount system if you're not supposed to unmount it
1538
Matt Mowera8e6d832017-02-14 20:20:59 -06001539 if (Is_Storage && MTP_Storage_ID > 0)
Ethan Yonker726a0202014-12-16 20:01:38 -06001540 PartitionManager.Remove_MTP_Storage(MTP_Storage_ID);
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001541
Dees_Troy38bd7602012-09-14 13:33:53 -04001542 if (!Symlink_Mount_Point.empty())
1543 umount(Symlink_Mount_Point.c_str());
1544
Dees_Troyb05ddee2013-01-28 20:24:50 +00001545 umount(Mount_Point.c_str());
1546 if (Is_Mounted()) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001547 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001548 gui_msg(Msg(msg::kError, "fail_unmount=Failed to unmount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy5bf43922012-09-07 16:07:55 -04001549 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001550 LOGINFO("Unable to unmount '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001551 return false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001552 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -04001553 return true;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001554 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001555 } else {
1556 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001557 }
1558}
1559
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05001560bool TWPartition::ReMount(bool Display_Error) {
1561 if (UnMount(Display_Error))
1562 return Mount(Display_Error);
1563 return false;
1564}
1565
1566bool TWPartition::ReMount_RW(bool Display_Error) {
1567 // No need to remount if already mounted rw
1568 if (Is_File_System_Writable())
1569 return true;
1570
1571 bool ro = Mount_Read_Only;
1572 int flags = Mount_Flags;
1573
1574 Mount_Read_Only = false;
1575 Mount_Flags &= ~MS_RDONLY;
1576
1577 bool ret = ReMount(Display_Error);
1578
1579 Mount_Read_Only = ro;
1580 Mount_Flags = flags;
1581
1582 return ret;
1583}
1584
Gary Peck43acadf2012-11-21 21:19:01 -08001585bool TWPartition::Wipe(string New_File_System) {
Ethan Yonker726a0202014-12-16 20:01:38 -06001586 bool wiped = false, update_crypt = false, recreate_media = true;
Dees_Troy16c2b312013-01-15 16:51:18 +00001587 int check;
1588 string Layout_Filename = Mount_Point + "/.layout_version";
1589
Dees_Troy38bd7602012-09-14 13:33:53 -04001590 if (!Can_Be_Wiped) {
Matt Mower3c366972015-12-25 19:28:31 -06001591 gui_msg(Msg(msg::kError, "cannot_wipe=Partition {1} cannot be wiped.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04001592 return false;
1593 }
1594
Dees_Troyc51f1f92012-09-20 15:32:13 -04001595 if (Mount_Point == "/cache")
Dees_Troy2673cec2013-04-02 20:22:16 +00001596 Log_Offset = 0;
Dees_Troyc51f1f92012-09-20 15:32:13 -04001597
Dees_Troy16c2b312013-01-15 16:51:18 +00001598 if (Retain_Layout_Version && Mount(false) && TWFunc::Path_Exists(Layout_Filename))
1599 TWFunc::copy_file(Layout_Filename, "/.layout_version", 0600);
1600 else
1601 unlink("/.layout_version");
Dees_Troy38bd7602012-09-14 13:33:53 -04001602
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001603 if (Has_Data_Media && Current_File_System == New_File_System) {
Dees_Troy16c2b312013-01-15 16:51:18 +00001604 wiped = Wipe_Data_Without_Wiping_Media();
Ethan Yonker5eac2222014-06-11 12:22:55 -05001605 recreate_media = false;
Dees_Troy16c2b312013-01-15 16:51:18 +00001606 } else {
Dees_Troy16c2b312013-01-15 16:51:18 +00001607 DataManager::GetValue(TW_RM_RF_VAR, check);
1608
Hashcodedabfd492013-08-29 22:45:30 -07001609 if (check || Use_Rm_Rf)
Dees_Troy16c2b312013-01-15 16:51:18 +00001610 wiped = Wipe_RMRF();
1611 else if (New_File_System == "ext4")
1612 wiped = Wipe_EXT4();
1613 else if (New_File_System == "ext2" || New_File_System == "ext3")
dianlujitao4879b372018-12-03 18:45:47 +08001614 wiped = Wipe_EXTFS(New_File_System);
Dees_Troy16c2b312013-01-15 16:51:18 +00001615 else if (New_File_System == "vfat")
1616 wiped = Wipe_FAT();
1617 else if (New_File_System == "exfat")
1618 wiped = Wipe_EXFAT();
1619 else if (New_File_System == "yaffs2")
1620 wiped = Wipe_MTD();
Dees_Troye5017042013-08-29 16:38:55 +00001621 else if (New_File_System == "f2fs")
1622 wiped = Wipe_F2FS();
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001623 else if (New_File_System == "ntfs")
1624 wiped = Wipe_NTFS();
Dees_Troy16c2b312013-01-15 16:51:18 +00001625 else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001626 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 +00001627 unlink("/.layout_version");
1628 return false;
1629 }
1630 update_crypt = wiped;
Gary Pecke8bc5d72012-12-21 06:45:25 -08001631 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001632
Gary Pecke8bc5d72012-12-21 06:45:25 -08001633 if (wiped) {
Dees_Troy1c1ac442013-01-17 21:42:14 +00001634 if (Mount_Point == "/cache")
1635 DataManager::Output_Version();
1636
Dees_Troy16c2b312013-01-15 16:51:18 +00001637 if (TWFunc::Path_Exists("/.layout_version") && Mount(false))
1638 TWFunc::copy_file("/.layout_version", Layout_Filename, 0600);
1639
1640 if (update_crypt) {
1641 Setup_File_System(false);
1642 if (Is_Encrypted && !Is_Decrypted) {
1643 // just wiped an encrypted partition back to its unencrypted state
1644 Is_Encrypted = false;
1645 Is_Decrypted = false;
1646 Decrypted_Block_Device = "";
1647 if (Mount_Point == "/data") {
1648 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1649 DataManager::SetValue(TW_IS_DECRYPTED, 0);
1650 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001651 }
1652 }
Ethan Yonker5eac2222014-06-11 12:22:55 -05001653
Ethan Yonker66a19492015-12-10 10:19:45 -06001654 if (Has_Data_Media && recreate_media) {
Ethan Yonker5eac2222014-06-11 12:22:55 -05001655 Recreate_Media_Folder();
1656 }
Matt Mower209c9632016-01-20 12:08:35 -06001657 if (Is_Storage && Mount(false))
1658 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Gary Pecke8bc5d72012-12-21 06:45:25 -08001659 }
Matt Mower209c9632016-01-20 12:08:35 -06001660
Gary Pecke8bc5d72012-12-21 06:45:25 -08001661 return wiped;
Dees_Troy51a0e822012-09-05 15:24:24 -04001662}
1663
Gary Peck43acadf2012-11-21 21:19:01 -08001664bool TWPartition::Wipe() {
Gary Peck82599a82012-11-21 16:23:12 -08001665 if (Is_File_System(Current_File_System))
1666 return Wipe(Current_File_System);
1667 else
1668 return Wipe(Fstab_File_System);
Gary Peck43acadf2012-11-21 21:19:01 -08001669}
1670
Dees_Troye58d5262012-09-21 12:27:57 -04001671bool TWPartition::Wipe_AndSec(void) {
1672 if (!Has_Android_Secure)
1673 return false;
1674
Dees_Troye58d5262012-09-21 12:27:57 -04001675 if (!Mount(true))
1676 return false;
1677
Ethan Yonker74db1572015-10-28 12:44:49 -05001678 gui_msg(Msg("wiping=Wiping {1}")(Backup_Display_Name));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001679 TWFunc::removeDir(Mount_Point + "/.android_secure/", true);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001680 return true;
Dees_Troye58d5262012-09-21 12:27:57 -04001681}
1682
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001683bool TWPartition::Can_Repair() {
Ethan Yonkera2719152015-05-28 09:44:41 -05001684 if (Mount_Read_Only)
1685 return false;
Matt Mower18794c82015-11-11 16:22:45 -06001686 if (Current_File_System == "vfat" && TWFunc::Path_Exists("/sbin/fsck.fat"))
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001687 return true;
1688 else if ((Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") && TWFunc::Path_Exists("/sbin/e2fsck"))
1689 return true;
1690 else if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/fsck.exfat"))
1691 return true;
1692 else if (Current_File_System == "f2fs" && TWFunc::Path_Exists("/sbin/fsck.f2fs"))
1693 return true;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001694 else if (Current_File_System == "ntfs" && (TWFunc::Path_Exists("/sbin/ntfsfix") || TWFunc::Path_Exists("/sbin/fsck.ntfs")))
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001695 return true;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001696 return false;
1697}
1698
1699bool TWPartition::Repair() {
1700 string command;
1701
1702 if (Current_File_System == "vfat") {
Matt Mower18794c82015-11-11 16:22:45 -06001703 if (!TWFunc::Path_Exists("/sbin/fsck.fat")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001704 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.fat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001705 return false;
1706 }
1707 if (!UnMount(true))
1708 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001709 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.fat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001710 Find_Actual_Block_Device();
Matt Mower18794c82015-11-11 16:22:45 -06001711 command = "/sbin/fsck.fat -y " + Actual_Block_Device;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001712 LOGINFO("Repair command: %s\n", command.c_str());
1713 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001714 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001715 return true;
1716 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001717 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001718 return false;
1719 }
1720 }
1721 if (Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") {
1722 if (!TWFunc::Path_Exists("/sbin/e2fsck")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001723 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("e2fsck"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001724 return false;
1725 }
1726 if (!UnMount(true))
1727 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001728 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("e2fsck"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001729 Find_Actual_Block_Device();
Ethan Yonkera2719152015-05-28 09:44:41 -05001730 command = "/sbin/e2fsck -fp " + Actual_Block_Device;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001731 LOGINFO("Repair command: %s\n", command.c_str());
1732 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001733 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001734 return true;
1735 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001736 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001737 return false;
1738 }
1739 }
1740 if (Current_File_System == "exfat") {
1741 if (!TWFunc::Path_Exists("/sbin/fsck.exfat")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001742 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.exfat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001743 return false;
1744 }
1745 if (!UnMount(true))
1746 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001747 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.exfat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001748 Find_Actual_Block_Device();
1749 command = "/sbin/fsck.exfat " + Actual_Block_Device;
1750 LOGINFO("Repair command: %s\n", command.c_str());
1751 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001752 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001753 return true;
1754 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001755 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001756 return false;
1757 }
1758 }
1759 if (Current_File_System == "f2fs") {
1760 if (!TWFunc::Path_Exists("/sbin/fsck.f2fs")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001761 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.f2fs"));
Ethan Yonker