blob: dfa57fafcc5adfedf0e6e4e44c8c387ccff78662 [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
Dees_Troy657c3092012-09-10 20:32:10 -040034#ifdef TW_INCLUDE_CRYPTO
35 #include "cutils/properties.h"
36#endif
37
bigbiff7b4c7a62015-01-01 19:44:14 -050038#include "libblkid/include/blkid.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040039#include "variables.h"
Dees_Troy2673cec2013-04-02 20:22:16 +000040#include "twcommon.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040041#include "partitions.hpp"
Dees_Troy5bf43922012-09-07 16:07:55 -040042#include "data.hpp"
Dees_Troy43d8b002012-09-17 16:00:01 -040043#include "twrp-functions.hpp"
bigbiff bigbiff9c754052013-01-09 09:09:08 -050044#include "twrpTar.hpp"
Ethan Yonker3fdcda42016-11-30 12:29:37 -060045#include "exclude.hpp"
Ethan Yonker1b7a31b2014-07-03 15:09:22 -050046#include "infomanager.hpp"
Ethan Yonker4b94cfd2014-12-11 10:00:45 -060047#include "set_metadata.h"
Ethan Yonker74db1572015-10-28 12:44:49 -050048#include "gui/gui.hpp"
bigbiffce8f83c2015-12-12 18:30:21 -050049#include "adbbu/libtwadbbu.hpp"
Dees_Troy5bf43922012-09-07 16:07:55 -040050extern "C" {
Dees_Troy38bd7602012-09-14 13:33:53 -040051 #include "mtdutils/mtdutils.h"
52 #include "mtdutils/mounts.h"
Dees_Troya95f55c2013-08-17 13:14:43 +000053#ifdef USE_EXT4
Ethan Yonker8373cfe2017-09-08 06:50:54 -050054 // #include "make_ext4fs.h" TODO need ifdef for android8
55 #include <ext4_utils/make_ext4fs.h>
Dees_Troya95f55c2013-08-17 13:14:43 +000056#endif
Ethan Yonker71413f42014-02-26 13:36:08 -060057
58#ifdef TW_INCLUDE_CRYPTO
Ethan Yonker253368a2014-11-25 15:00:52 -060059 #include "crypto/lollipop/cryptfs.h"
Ethan Yonker66a19492015-12-10 10:19:45 -060060 #include "gpt/gpt.h"
Ethan Yonkerbd7492d2016-12-07 13:55:01 -060061 #ifdef TW_INCLUDE_FBE
62 #include "crypto/ext4crypt/Decrypt.h"
Ethan Yonkerbd7492d2016-12-07 13:55:01 -060063 #endif
Ethan Yonkerbc85b632015-08-09 12:48:14 -050064#else
65 #define CRYPT_FOOTER_OFFSET 0x4000
Ethan Yonker71413f42014-02-26 13:36:08 -060066#endif
Dees_Troy5bf43922012-09-07 16:07:55 -040067}
Matt Mower87413642017-01-17 21:14:46 -060068#include <selinux/selinux.h>
Ethan Yonkerf27497f2014-02-09 11:48:33 -060069#include <selinux/label.h>
Dees Troy4159aed2014-02-28 17:24:43 +000070#ifdef HAVE_CAPABILITIES
71#include <sys/capability.h>
72#include <sys/xattr.h>
73#include <linux/xattr.h>
74#endif
HashBanged974bb2016-01-30 14:20:09 -050075#include <sparse_format.h>
Ethan Yonker472f5062016-02-25 13:47:30 -060076#include "progresstracking.hpp"
Dees_Troy51a0e822012-09-05 15:24:24 -040077
bigbiff bigbiff9c754052013-01-09 09:09:08 -050078using namespace std;
79
Ethan Yonker6e8c27a2016-12-22 17:55:57 -060080static 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
81
Dees_Troya95f55c2013-08-17 13:14:43 +000082extern struct selabel_handle *selinux_handle;
Ethan Yonker6277c792014-09-15 14:54:30 -050083extern bool datamedia;
Dees_Troya95f55c2013-08-17 13:14:43 +000084
Hashcode62bd9e02013-11-19 21:59:42 -080085struct flag_list {
86 const char *name;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -060087 unsigned long flag;
Hashcode62bd9e02013-11-19 21:59:42 -080088};
89
Matt Mower4ab42b12016-04-21 13:52:18 -050090const struct flag_list mount_flags[] = {
Ethan Yonker6e8c27a2016-12-22 17:55:57 -060091 { "noatime", MS_NOATIME },
92 { "noexec", MS_NOEXEC },
93 { "nosuid", MS_NOSUID },
94 { "nodev", MS_NODEV },
95 { "nodiratime", MS_NODIRATIME },
96 { "ro", MS_RDONLY },
97 { "rw", 0 },
98 { "remount", MS_REMOUNT },
99 { "bind", MS_BIND },
100 { "rec", MS_REC },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000101#ifdef MS_UNBINDABLE
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600102 { "unbindable", MS_UNBINDABLE },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000103#endif
104#ifdef MS_PRIVATE
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600105 { "private", MS_PRIVATE },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000106#endif
107#ifdef MS_SLAVE
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600108 { "slave", MS_SLAVE },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000109#endif
110#ifdef MS_SHARED
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600111 { "shared", MS_SHARED },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000112#endif
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600113 { "sync", MS_SYNCHRONOUS },
114 { 0, 0 },
115};
116
117const char *ignored_mount_items[] = {
118 "defaults=",
119 "errors=",
120 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,
Matt Mower2416a502016-04-12 19:54:46 -0500160};
161
162/* Flags without a trailing '=' are considered dual format flags and can be
163 * written as either 'flagname' or 'flagname=', where the character following
164 * the '=' is Y,y,1 for true and false otherwise.
165 */
166const struct flag_list tw_flags[] = {
167 { "andsec", TWFLAG_ANDSEC },
168 { "backup", TWFLAG_BACKUP },
169 { "backupname=", TWFLAG_BACKUPNAME },
170 { "blocksize=", TWFLAG_BLOCKSIZE },
171 { "canbewiped", TWFLAG_CANBEWIPED },
172 { "canencryptbackup", TWFLAG_CANENCRYPTBACKUP },
173 { "defaults", TWFLAG_DEFAULTS },
174 { "display=", TWFLAG_DISPLAY },
175 { "encryptable=", TWFLAG_ENCRYPTABLE },
Ethan Yonkere9afc3d2018-08-30 15:16:27 -0500176 { "fileencryption=", TWFLAG_FILEENCRYPTION },
Matt Mower2416a502016-04-12 19:54:46 -0500177 { "flashimg", TWFLAG_FLASHIMG },
178 { "forceencrypt=", TWFLAG_FORCEENCRYPT },
179 { "fsflags=", TWFLAG_FSFLAGS },
180 { "ignoreblkid", TWFLAG_IGNOREBLKID },
181 { "length=", TWFLAG_LENGTH },
182 { "mounttodecrypt", TWFLAG_MOUNTTODECRYPT },
183 { "removable", TWFLAG_REMOVABLE },
184 { "retainlayoutversion", TWFLAG_RETAINLAYOUTVERSION },
185 { "settingsstorage", TWFLAG_SETTINGSSTORAGE },
186 { "storage", TWFLAG_STORAGE },
187 { "storagename=", TWFLAG_STORAGENAME },
188 { "subpartitionof=", TWFLAG_SUBPARTITIONOF },
189 { "symlink=", TWFLAG_SYMLINK },
190 { "userdataencryptbackup", TWFLAG_USERDATAENCRYPTBACKUP },
191 { "usermrf", TWFLAG_USERMRF },
192 { "wipeduringfactoryreset", TWFLAG_WIPEDURINGFACTORYRESET },
193 { "wipeingui", TWFLAG_WIPEINGUI },
Ethan Yonker1b190162016-12-05 15:25:19 -0600194 { "slotselect", TWFLAG_SLOTSELECT },
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600195 { "wait", TWFLAG_WAIT },
196 { "verify", TWFLAG_VERIFY },
197 { "check", TWFLAG_CHECK },
198 { "altdevice", TWFLAG_ALTDEVICE },
199 { "notrim", TWFLAG_NOTRIM },
200 { "voldmanaged=", TWFLAG_VOLDMANAGED },
201 { "formattable", TWFLAG_FORMATTABLE },
202 { "resize", TWFLAG_RESIZE },
Matt Mower2416a502016-04-12 19:54:46 -0500203 { 0, 0 },
204};
205
that9e0593e2014-10-08 00:01:24 +0200206TWPartition::TWPartition() {
Dees_Troy51a0e822012-09-05 15:24:24 -0400207 Can_Be_Mounted = false;
208 Can_Be_Wiped = false;
Dees_Troya13d74f2013-03-24 08:54:55 -0500209 Can_Be_Backed_Up = false;
Vojtech Bocek1dc30982013-08-30 21:49:30 +0200210 Use_Rm_Rf = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400211 Wipe_During_Factory_Reset = false;
212 Wipe_Available_in_GUI = false;
213 Is_SubPartition = false;
Dees_Troy2691f9d2012-09-24 11:15:49 -0400214 Has_SubPartition = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400215 SubPartition_Of = "";
216 Symlink_Path = "";
217 Symlink_Mount_Point = "";
218 Mount_Point = "";
Dees_Troye58d5262012-09-21 12:27:57 -0400219 Backup_Path = "";
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600220 Wildcard_Block_Device = false;
221 Sysfs_Entry = "";
Dees_Troy38bd7602012-09-14 13:33:53 -0400222 Actual_Block_Device = "";
223 Primary_Block_Device = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400224 Alternate_Block_Device = "";
225 Removable = false;
226 Is_Present = false;
227 Length = 0;
228 Size = 0;
229 Used = 0;
230 Free = 0;
231 Backup_Size = 0;
232 Can_Be_Encrypted = false;
233 Is_Encrypted = false;
234 Is_Decrypted = false;
Ethan Yonkerbd7492d2016-12-07 13:55:01 -0600235 Is_FBE = false;
Ethan Yonker253368a2014-11-25 15:00:52 -0600236 Mount_To_Decrypt = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400237 Decrypted_Block_Device = "";
238 Display_Name = "";
Dees_Troya13d74f2013-03-24 08:54:55 -0500239 Backup_Display_Name = "";
240 Storage_Name = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400241 Backup_Name = "";
Dees_Troy63c8df72012-09-10 14:02:05 -0400242 Backup_FileName = "";
Dees_Troy38bd7602012-09-14 13:33:53 -0400243 MTD_Name = "";
bigbiffce8f83c2015-12-12 18:30:21 -0500244 Backup_Method = BM_NONE;
Dees_Troy83bd4832013-05-04 12:39:56 +0000245 Can_Encrypt_Backup = false;
246 Use_Userdata_Encryption = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400247 Has_Data_Media = false;
Dees_Troye58d5262012-09-21 12:27:57 -0400248 Has_Android_Secure = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400249 Is_Storage = false;
Dees_Troya13d74f2013-03-24 08:54:55 -0500250 Is_Settings_Storage = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400251 Storage_Path = "";
252 Current_File_System = "";
253 Fstab_File_System = "";
Hashcode62bd9e02013-11-19 21:59:42 -0800254 Mount_Flags = 0;
255 Mount_Options = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400256 Format_Block_Size = 0;
Dees_Troy68cab492012-12-12 19:29:35 +0000257 Ignore_Blkid = false;
Dees_Troy16c2b312013-01-15 16:51:18 +0000258 Retain_Layout_Version = false;
Ethan Yonker253368a2014-11-25 15:00:52 -0600259 Crypto_Key_Location = "footer";
Ethan Yonker726a0202014-12-16 20:01:38 -0600260 MTP_Storage_ID = 0;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600261 Can_Flash_Img = false;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500262 Mount_Read_Only = false;
Ethan Yonker66a19492015-12-10 10:19:45 -0600263 Is_Adopted_Storage = false;
264 Adopted_GUID = "";
Ethan Yonker1b190162016-12-05 15:25:19 -0600265 SlotSelect = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400266}
267
268TWPartition::~TWPartition(void) {
269 // Do nothing
270}
271
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600272bool 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 -0500273 char full_line[MAX_FSTAB_LINE_LENGTH];
Matt Mower2416a502016-04-12 19:54:46 -0500274 char twflags[MAX_FSTAB_LINE_LENGTH] = "";
Dees_Troy5bf43922012-09-07 16:07:55 -0400275 char* ptr;
Matt Mower2b2dd152016-04-26 11:24:08 -0500276 int line_len = strlen(fstab_line), index = 0, item_index = 0;
Dees_Troya13d74f2013-03-24 08:54:55 -0500277 bool skip = false;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600278 int fstab_version = 1, mount_point_index = 0, fs_index = 1, block_device_index = 2;
279 TWPartition *additional_entry = NULL;
280 std::map<string, Flags_Map>::iterator it;
Dees_Troy5bf43922012-09-07 16:07:55 -0400281
Matt Mower2b2dd152016-04-26 11:24:08 -0500282 strlcpy(full_line, fstab_line, sizeof(full_line));
Dees_Troy51127312012-09-08 13:08:49 -0400283 for (index = 0; index < line_len; index++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500284 if (full_line[index] == 34)
285 skip = !skip;
286 if (!skip && full_line[index] <= 32)
Dees_Troy5bf43922012-09-07 16:07:55 -0400287 full_line[index] = '\0';
Dees_Troy5bf43922012-09-07 16:07:55 -0400288 }
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600289 if (line_len < 10)
290 return false; // There can't possibly be a valid fstab line that is less than 10 chars
291 if (strncmp(fstab_line, "/dev/", strlen("/dev/")) == 0 || strncmp(fstab_line, "/devices/", strlen("/devices/")) == 0) {
292 fstab_version = 2;
293 block_device_index = 0;
294 mount_point_index = 1;
295 fs_index = 2;
296 }
297
298 index = 0;
Dees_Troy5bf43922012-09-07 16:07:55 -0400299 while (index < line_len) {
300 while (index < line_len && full_line[index] == '\0')
301 index++;
302 if (index >= line_len)
303 continue;
304 ptr = full_line + index;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600305 if (item_index == mount_point_index) {
306 Mount_Point = ptr;
307 if (fstab_version == 2) {
308 additional_entry = PartitionManager.Find_Partition_By_Path(Mount_Point);
309 if (additional_entry) {
310 LOGINFO("Found an additional entry for '%s'\n", Mount_Point.c_str());
311 }
312 }
313 LOGINFO("Processing '%s'\n", Mount_Point.c_str());
314 Backup_Path = Mount_Point;
315 Storage_Path = Mount_Point;
316 Display_Name = ptr + 1;
317 Backup_Display_Name = Display_Name;
318 Storage_Name = Display_Name;
319 item_index++;
320 } else if (item_index == fs_index) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400321 // File System
322 Fstab_File_System = ptr;
323 Current_File_System = ptr;
324 item_index++;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600325 } else if (item_index == block_device_index) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400326 // Primary Block Device
Dees_Troy38bd7602012-09-14 13:33:53 -0400327 if (Fstab_File_System == "mtd" || Fstab_File_System == "yaffs2") {
Dees_Troy094207a2012-09-26 12:00:39 -0400328 MTD_Name = ptr;
329 Find_MTD_Block_Device(MTD_Name);
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400330 } else if (Fstab_File_System == "bml") {
331 if (Mount_Point == "/boot")
332 MTD_Name = "boot";
333 else if (Mount_Point == "/recovery")
334 MTD_Name = "recovery";
335 Primary_Block_Device = ptr;
336 if (*ptr != '/')
Dees_Troy2673cec2013-04-02 20:22:16 +0000337 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 -0400338 } else if (*ptr != '/') {
Dees_Troy5bf43922012-09-07 16:07:55 -0400339 if (Display_Error)
Matt Mower2b2dd152016-04-26 11:24:08 -0500340 LOGERR("Invalid block device '%s' in fstab line '%s'", ptr, fstab_line);
Dees_Troy5bf43922012-09-07 16:07:55 -0400341 else
Matt Mower2b2dd152016-04-26 11:24:08 -0500342 LOGINFO("Invalid block device '%s' in fstab line '%s'", ptr, fstab_line);
Matt Mower72c87ce2016-04-26 14:34:56 -0500343 return false;
Dees_Troy38bd7602012-09-14 13:33:53 -0400344 } else {
345 Primary_Block_Device = ptr;
346 Find_Real_Block_Device(Primary_Block_Device, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400347 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400348 item_index++;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600349 } else if (item_index > 2) {
350 if (fstab_version == 2) {
351 if (item_index == 3) {
352 Process_FS_Flags(ptr);
353 if (additional_entry) {
354 additional_entry->Save_FS_Flags(Fstab_File_System, Mount_Flags, Mount_Options);
355 return false; // We save the extra fs flags in the other partition entry and by returning false, this entry will be deleted
356 }
357 } else {
358 strlcpy(twflags, ptr, sizeof(twflags));
359 }
360 item_index++;
361 } else if (*ptr == '/') { // v2 fstab does not allow alternate block devices
Dees_Troy5bf43922012-09-07 16:07:55 -0400362 // Alternate Block Device
363 Alternate_Block_Device = ptr;
364 Find_Real_Block_Device(Alternate_Block_Device, Display_Error);
365 } else if (strlen(ptr) > 7 && strncmp(ptr, "length=", 7) == 0) {
366 // Partition length
367 ptr += 7;
368 Length = atoi(ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400369 } else if (strlen(ptr) > 6 && strncmp(ptr, "flags=", 6) == 0) {
370 // Custom flags, save for later so that new values aren't overwritten by defaults
371 ptr += 6;
Matt Mower2416a502016-04-12 19:54:46 -0500372 strlcpy(twflags, ptr, sizeof(twflags));
Dees_Troy38bd7602012-09-14 13:33:53 -0400373 } else if (strlen(ptr) == 4 && (strncmp(ptr, "NULL", 4) == 0 || strncmp(ptr, "null", 4) == 0 || strncmp(ptr, "null", 4) == 0)) {
374 // Do nothing
Dees_Troy5bf43922012-09-07 16:07:55 -0400375 } else {
376 // Unhandled data
Matt Mower2b2dd152016-04-26 11:24:08 -0500377 LOGINFO("Unhandled fstab information '%s' in fstab line '%s'\n", ptr, fstab_line);
Dees_Troy5bf43922012-09-07 16:07:55 -0400378 }
379 }
380 while (index < line_len && full_line[index] != '\0')
381 index++;
382 }
383
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600384 // override block devices from the v2 fstab with the ones we read from the twrp.flags file in case they are different
385 if (fstab_version == 2 && twrp_flags && twrp_flags->size() > 0) {
386 it = twrp_flags->find(Mount_Point);
387 if (it != twrp_flags->end()) {
388 if (!it->second.Primary_Block_Device.empty()) {
389 Primary_Block_Device = it->second.Primary_Block_Device;
390 Find_Real_Block_Device(Primary_Block_Device, Display_Error);
391 }
392 if (!it->second.Alternate_Block_Device.empty()) {
393 Alternate_Block_Device = it->second.Alternate_Block_Device;
394 Find_Real_Block_Device(Alternate_Block_Device, Display_Error);
395 }
396 }
397 }
398
399 if (strncmp(fstab_line, "/devices/", strlen("/devices/")) == 0) {
400 Sysfs_Entry = Primary_Block_Device;
401 Primary_Block_Device = "";
402 Is_Storage = true;
403 Removable = true;
404 Wipe_Available_in_GUI = true;
405 Wildcard_Block_Device = true;
406 }
407 if (Primary_Block_Device.find("*") != string::npos)
408 Wildcard_Block_Device = true;
409
410 if (Mount_Point == "auto") {
411 Mount_Point = "/auto";
412 char autoi[5];
413 sprintf(autoi, "%i", auto_index);
414 Mount_Point += autoi;
415 Backup_Path = Mount_Point;
416 Storage_Path = Mount_Point;
417 auto_index++;
418 Setup_File_System(Display_Error);
419 Display_Name = "Storage";
420 Backup_Display_Name = Display_Name;
421 Storage_Name = Display_Name;
422 Can_Be_Backed_Up = false;
423 Wipe_Available_in_GUI = true;
424 Is_Storage = true;
425 Removable = true;
426 Wipe_Available_in_GUI = true;
427 } else if (!Is_File_System(Fstab_File_System) && !Is_Image(Fstab_File_System)) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400428 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000429 LOGERR("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400430 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000431 LOGINFO("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Matt Mower72c87ce2016-04-26 14:34:56 -0500432 return false;
Dees_Troy5bf43922012-09-07 16:07:55 -0400433 } else if (Is_File_System(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400434 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400435 Setup_File_System(Display_Error);
436 if (Mount_Point == "/system") {
437 Display_Name = "System";
Dees_Troya13d74f2013-03-24 08:54:55 -0500438 Backup_Display_Name = Display_Name;
439 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400440 Wipe_Available_in_GUI = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500441 Can_Be_Backed_Up = true;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500442 Mount_Read_Only = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400443 } else if (Mount_Point == "/data") {
444 Display_Name = "Data";
Dees_Troya13d74f2013-03-24 08:54:55 -0500445 Backup_Display_Name = Display_Name;
446 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400447 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400448 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500449 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000450 Can_Encrypt_Backup = true;
451 Use_Userdata_Encryption = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400452 } else if (Mount_Point == "/cache") {
453 Display_Name = "Cache";
Dees_Troya13d74f2013-03-24 08:54:55 -0500454 Backup_Display_Name = Display_Name;
455 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400456 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400457 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500458 Can_Be_Backed_Up = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400459 } else if (Mount_Point == "/datadata") {
Dees_Troy51127312012-09-08 13:08:49 -0400460 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400461 Display_Name = "DataData";
Dees_Troya13d74f2013-03-24 08:54:55 -0500462 Backup_Display_Name = Display_Name;
463 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400464 Is_SubPartition = true;
465 SubPartition_Of = "/data";
Dees_Troy5bf43922012-09-07 16:07:55 -0400466 DataManager::SetValue(TW_HAS_DATADATA, 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500467 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000468 Can_Encrypt_Backup = true;
469 Use_Userdata_Encryption = false; // This whole partition should be encrypted
Dees_Troy5bf43922012-09-07 16:07:55 -0400470 } else if (Mount_Point == "/sd-ext") {
Dees_Troy51127312012-09-08 13:08:49 -0400471 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400472 Display_Name = "SD-Ext";
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 Wipe_Available_in_GUI = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400476 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500477 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000478 Can_Encrypt_Backup = true;
479 Use_Userdata_Encryption = true;
Dees_Troy2c50e182012-09-26 20:05:28 -0400480 } else if (Mount_Point == "/boot") {
481 Display_Name = "Boot";
Dees_Troya13d74f2013-03-24 08:54:55 -0500482 Backup_Display_Name = Display_Name;
Dees_Troy2c50e182012-09-26 20:05:28 -0400483 DataManager::SetValue("tw_boot_is_mountable", 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500484 Can_Be_Backed_Up = true;
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500485 } else if (Mount_Point == "/vendor") {
486 Display_Name = "Vendor";
487 Backup_Display_Name = Display_Name;
488 Storage_Name = Display_Name;
489 Mount_Read_Only = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400490 }
491#ifdef TW_EXTERNAL_STORAGE_PATH
492 if (Mount_Point == EXPAND(TW_EXTERNAL_STORAGE_PATH)) {
493 Is_Storage = true;
494 Storage_Path = EXPAND(TW_EXTERNAL_STORAGE_PATH);
Dees_Troyc51f1f92012-09-20 15:32:13 -0400495 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500496 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400497#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000498 if (Mount_Point == "/sdcard" || Mount_Point == "/external_sd" || Mount_Point == "/external_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400499 Is_Storage = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400500 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500501 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400502#endif
Dees_Troyb05ddee2013-01-28 20:24:50 +0000503 }
Dees_Troy8170a922012-09-18 15:40:25 -0400504#ifdef TW_INTERNAL_STORAGE_PATH
505 if (Mount_Point == EXPAND(TW_INTERNAL_STORAGE_PATH)) {
506 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500507 Is_Settings_Storage = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400508 Storage_Path = EXPAND(TW_INTERNAL_STORAGE_PATH);
Dees_Troya13d74f2013-03-24 08:54:55 -0500509 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400510 }
511#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000512 if (Mount_Point == "/emmc" || Mount_Point == "/internal_sd" || Mount_Point == "/internal_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400513 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500514 Is_Settings_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500515 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400516 }
517#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400518 } else if (Is_Image(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400519 Find_Actual_Block_Device();
Ethan Yonker1b190162016-12-05 15:25:19 -0600520 Setup_Image();
Dees_Troya13d74f2013-03-24 08:54:55 -0500521 if (Mount_Point == "/boot") {
522 Display_Name = "Boot";
523 Backup_Display_Name = Display_Name;
524 Can_Be_Backed_Up = true;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600525 Can_Flash_Img = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500526 } else if (Mount_Point == "/recovery") {
527 Display_Name = "Recovery";
528 Backup_Display_Name = Display_Name;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600529 Can_Flash_Img = true;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500530 } else if (Mount_Point == "/system_image") {
531 Display_Name = "System Image";
532 Backup_Display_Name = Display_Name;
HashBanged974bb2016-01-30 14:20:09 -0500533 Can_Flash_Img = true;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500534 Can_Be_Backed_Up = true;
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500535 } else if (Mount_Point == "/vendor_image") {
536 Display_Name = "Vendor Image";
537 Backup_Display_Name = Display_Name;
HashBanged974bb2016-01-30 14:20:09 -0500538 Can_Flash_Img = true;
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500539 Can_Be_Backed_Up = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500540 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400541 }
542
Matt Mower2416a502016-04-12 19:54:46 -0500543 // Process TWRP fstab flags
544 if (strlen(twflags) > 0) {
545 string Prev_Display_Name = Display_Name;
546 string Prev_Storage_Name = Storage_Name;
547 string Prev_Backup_Display_Name = Backup_Display_Name;
548 Display_Name = "";
549 Storage_Name = "";
550 Backup_Display_Name = "";
551
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600552 Process_TW_Flags(twflags, (fstab_version == 1), fstab_version);
553 Save_FS_Flags(Fstab_File_System, Mount_Flags, Mount_Options);
Matt Mower2416a502016-04-12 19:54:46 -0500554
555 bool has_display_name = !Display_Name.empty();
556 bool has_storage_name = !Storage_Name.empty();
557 bool has_backup_name = !Backup_Display_Name.empty();
558 if (!has_display_name) Display_Name = Prev_Display_Name;
559 if (!has_storage_name) Storage_Name = Prev_Storage_Name;
560 if (!has_backup_name) Backup_Display_Name = Prev_Backup_Display_Name;
561
562 if (has_display_name && !has_storage_name)
563 Storage_Name = Display_Name;
564 if (!has_display_name && has_storage_name)
565 Display_Name = Storage_Name;
566 if (has_display_name && !has_backup_name && Backup_Display_Name != "Android Secure")
567 Backup_Display_Name = Display_Name;
568 if (!has_display_name && has_backup_name)
569 Display_Name = Backup_Display_Name;
570 }
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600571
572 if (fstab_version == 2 && twrp_flags && twrp_flags->size() > 0) {
573 it = twrp_flags->find(Mount_Point);
574 if (it != twrp_flags->end()) {
575 char twrpflags[MAX_FSTAB_LINE_LENGTH] = "";
576 int skip = 0;
577 string Flags = it->second.Flags;
578 strcpy(twrpflags, Flags.c_str());
579 if (strlen(twrpflags) > strlen("flags=") && strncmp(twrpflags, "flags=", strlen("flags=")) == 0)
580 skip += strlen("flags=");
581 char* flagptr = twrpflags;
582 flagptr += skip;
583 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
584 }
585 }
nkk7198fc3992017-12-16 16:26:42 +0200586
587 if (Mount_Point == "/persist" && Can_Be_Mounted) {
588 bool mounted = Is_Mounted();
589 if (mounted || Mount(false)) {
590 // Read the backup settings file
591 DataManager::LoadPersistValues();
592 TWFunc::Fixup_Time_On_Boot("/persist/time/");
593 if (!mounted)
594 UnMount(false);
595 }
596 }
597
Dees_Troy51127312012-09-08 13:08:49 -0400598 return true;
599}
600
Matt Mower72c87ce2016-04-26 14:34:56 -0500601void TWPartition::Partition_Post_Processing(bool Display_Error) {
602 if (Mount_Point == "/data")
603 Setup_Data_Partition(Display_Error);
604 else if (Mount_Point == "/cache")
605 Setup_Cache_Partition(Display_Error);
606}
607
Ethan Yonkerbd7492d2016-12-07 13:55:01 -0600608void TWPartition::ExcludeAll(const string& path) {
609 backup_exclusions.add_absolute_dir(path);
610 wipe_exclusions.add_absolute_dir(path);
611}
612
Matt Mower72c87ce2016-04-26 14:34:56 -0500613void TWPartition::Setup_Data_Partition(bool Display_Error) {
614 if (Mount_Point != "/data")
615 return;
616
617 // Ensure /data is not mounted as tmpfs for qcom hardware decrypt
618 UnMount(false);
619
620#ifdef TW_INCLUDE_CRYPTO
621 if (datamedia)
622 Setup_Data_Media();
623 Can_Be_Encrypted = true;
624 char crypto_blkdev[255];
625 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
626 if (strcmp(crypto_blkdev, "error") != 0) {
627 DataManager::SetValue(TW_IS_DECRYPTED, 1);
628 Is_Encrypted = true;
629 Is_Decrypted = true;
Ethan Yonkerbd7492d2016-12-07 13:55:01 -0600630 Is_FBE = false;
631 DataManager::SetValue(TW_IS_FBE, 0);
Matt Mower72c87ce2016-04-26 14:34:56 -0500632 Decrypted_Block_Device = crypto_blkdev;
633 LOGINFO("Data already decrypted, new block device: '%s'\n", crypto_blkdev);
634 } else if (!Mount(false)) {
635 if (Is_Present) {
636 set_partition_data(Actual_Block_Device.c_str(), Crypto_Key_Location.c_str(), Fstab_File_System.c_str());
637 if (cryptfs_check_footer() == 0) {
638 Is_Encrypted = true;
639 Is_Decrypted = false;
640 Can_Be_Mounted = false;
641 Current_File_System = "emmc";
Ethan Yonker1b190162016-12-05 15:25:19 -0600642 Setup_Image();
Matt Mower72c87ce2016-04-26 14:34:56 -0500643 DataManager::SetValue(TW_IS_ENCRYPTED, 1);
644 DataManager::SetValue(TW_CRYPTO_PWTYPE, cryptfs_get_password_type());
645 DataManager::SetValue(TW_CRYPTO_PASSWORD, "");
646 DataManager::SetValue("tw_crypto_display", "");
647 } else {
648 gui_err("mount_data_footer=Could not mount /data and unable to find crypto footer.");
649 }
650 } else {
651 LOGERR("Primary block device '%s' for mount point '%s' is not present!\n", Primary_Block_Device.c_str(), Mount_Point.c_str());
652 }
653 } else {
Ethan Yonkerbd7492d2016-12-07 13:55:01 -0600654 if (TWFunc::Path_Exists("/data/unencrypted/key/version")) {
655 LOGINFO("File Based Encryption is present\n");
656#ifdef TW_INCLUDE_FBE
657 ExcludeAll(Mount_Point + "/convert_fbe");
658 ExcludeAll(Mount_Point + "/unencrypted");
659 //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
660 ExcludeAll(Mount_Point + "/misc/vold/user_keys");
Ethan Yonker79f88bd2016-12-09 14:52:12 -0600661 //ExcludeAll(Mount_Point + "/system_ce");
662 //ExcludeAll(Mount_Point + "/system_de");
663 //ExcludeAll(Mount_Point + "/misc_ce");
664 //ExcludeAll(Mount_Point + "/misc_de");
Ethan Yonkerbd7492d2016-12-07 13:55:01 -0600665 ExcludeAll(Mount_Point + "/system/gatekeeper.password.key");
666 ExcludeAll(Mount_Point + "/system/gatekeeper.pattern.key");
667 ExcludeAll(Mount_Point + "/system/locksettings.db");
668 //ExcludeAll(Mount_Point + "/system/locksettings.db-shm"); // don't seem to need this one, but the other 2 are needed
669 ExcludeAll(Mount_Point + "/system/locksettings.db-wal");
Ethan Yonker79f88bd2016-12-09 14:52:12 -0600670 //ExcludeAll(Mount_Point + "/user_de");
Ethan Yonkerbd7492d2016-12-07 13:55:01 -0600671 //ExcludeAll(Mount_Point + "/misc/profiles/cur/0"); // might be important later
672 ExcludeAll(Mount_Point + "/misc/gatekeeper");
Ethan Yonkerfefe5912017-09-30 22:22:13 -0500673 ExcludeAll(Mount_Point + "/misc/keystore");
Ethan Yonkerbd7492d2016-12-07 13:55:01 -0600674 ExcludeAll(Mount_Point + "/drm/kek.dat");
Ethan Yonkerfefe5912017-09-30 22:22:13 -0500675 ExcludeAll(Mount_Point + "/system_de/0/spblob"); // contains data needed to decrypt pixel 2
Ethan Yonkerbd7492d2016-12-07 13:55:01 -0600676 int retry_count = 3;
677 while (!Decrypt_DE() && --retry_count)
678 usleep(2000);
679 if (retry_count > 0) {
680 property_set("ro.crypto.state", "encrypted");
681 Is_Encrypted = true;
682 Is_Decrypted = false;
683 Is_FBE = true;
684 DataManager::SetValue(TW_IS_FBE, 1);
685 DataManager::SetValue(TW_IS_ENCRYPTED, 1);
686 string filename;
Ethan Yonkerfefe5912017-09-30 22:22:13 -0500687 int pwd_type = Get_Password_Type(0, filename);
688 if (pwd_type < 0) {
689 LOGERR("This TWRP does not have synthetic password decrypt support\n");
690 pwd_type = 0; // default password
691 }
692 DataManager::SetValue(TW_CRYPTO_PWTYPE, pwd_type);
Ethan Yonkerbd7492d2016-12-07 13:55:01 -0600693 DataManager::SetValue(TW_CRYPTO_PASSWORD, "");
694 DataManager::SetValue("tw_crypto_display", "");
695 }
696#else
697 LOGERR("FBE found but FBE support not present in TWRP\n");
698#endif
699 } else {
700 // Filesystem is not encrypted and the mount succeeded, so return to
701 // the original unmounted state
702 UnMount(false);
703 }
Matt Mower72c87ce2016-04-26 14:34:56 -0500704 }
705 if (datamedia && (!Is_Encrypted || (Is_Encrypted && Is_Decrypted))) {
706 Setup_Data_Media();
707 Recreate_Media_Folder();
708 }
709#else
710 if (datamedia) {
711 Setup_Data_Media();
712 Recreate_Media_Folder();
713 }
714#endif
715}
716
717void TWPartition::Setup_Cache_Partition(bool Display_Error __unused) {
718 if (Mount_Point != "/cache")
719 return;
720
721 if (!Mount(true))
722 return;
723
724 if (!TWFunc::Path_Exists("/cache/recovery/.")) {
725 LOGINFO("Recreating /cache/recovery folder\n");
726 if (mkdir("/cache/recovery", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0)
727 LOGERR("Could not create /cache/recovery\n");
728 }
729}
730
Matt Mower4ab42b12016-04-21 13:52:18 -0500731void TWPartition::Process_FS_Flags(const char *str) {
732 char *options = strdup(str);
733 char *ptr, *savep;
Hashcode62bd9e02013-11-19 21:59:42 -0800734
Matt Mower4ab42b12016-04-21 13:52:18 -0500735 Mount_Options = "";
Hashcode62bd9e02013-11-19 21:59:42 -0800736
Matt Mower4ab42b12016-04-21 13:52:18 -0500737 // Avoid issues with potentially nested strtok by using strtok_r
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600738 for (ptr = strtok_r(options, ",", &savep); ptr; ptr = strtok_r(NULL, ",", &savep)) {
739 char *equals = strstr(ptr, "=");
740 size_t name_len;
Matt Mower4ab42b12016-04-21 13:52:18 -0500741
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600742 if (!equals)
743 name_len = strlen(ptr);
744 else
745 name_len = equals - ptr;
746
747 // There are some flags that we want to ignore in TWRP
748 bool found_match = false;
749 for (const char** ignored_mount_item = ignored_mount_items; *ignored_mount_item; ignored_mount_item++) {
750 if (strncmp(ptr, *ignored_mount_item, name_len) == 0) {
751 found_match = true;
Hashcode62bd9e02013-11-19 21:59:42 -0800752 break;
753 }
754 }
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600755 if (found_match)
756 continue;
Hashcode62bd9e02013-11-19 21:59:42 -0800757
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600758 // mount_flags are never postfixed by '='
759 if (!equals) {
760 const struct flag_list* mount_flag = mount_flags;
761 for (; mount_flag->name; mount_flag++) {
762 if (strcmp(ptr, mount_flag->name) == 0) {
763 if (mount_flag->flag == MS_RDONLY)
764 Mount_Read_Only = true;
765 else
766 Mount_Flags |= (unsigned)mount_flag->flag;
767 found_match = true;
768 break;
769 }
770 }
771 if (found_match)
772 continue;
Matt Mower4ab42b12016-04-21 13:52:18 -0500773 }
774
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600775 // If we aren't ignoring this flag and it's not a mount flag, then it must be a mount option
776 if (!Mount_Options.empty())
777 Mount_Options += ",";
778 Mount_Options += ptr;
Matt Mower4ab42b12016-04-21 13:52:18 -0500779 }
780 free(options);
Hashcode62bd9e02013-11-19 21:59:42 -0800781}
782
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600783void TWPartition::Save_FS_Flags(const string& local_File_System, int local_Mount_Flags, const string& local_Mount_Options) {
784 partition_fs_flags_struct flags;
785 flags.File_System = local_File_System;
786 flags.Mount_Flags = local_Mount_Flags;
787 flags.Mount_Options = local_Mount_Options;
788 fs_flags.push_back(flags);
789}
790
Matt Mower2416a502016-04-12 19:54:46 -0500791void TWPartition::Apply_TW_Flag(const unsigned flag, const char* str, const bool val) {
792 switch (flag) {
793 case TWFLAG_ANDSEC:
794 Has_Android_Secure = val;
795 break;
796 case TWFLAG_BACKUP:
797 Can_Be_Backed_Up = val;
798 break;
799 case TWFLAG_BACKUPNAME:
800 Backup_Display_Name = str;
801 break;
802 case TWFLAG_BLOCKSIZE:
803 Format_Block_Size = (unsigned long)(atol(str));
804 break;
805 case TWFLAG_CANBEWIPED:
806 Can_Be_Wiped = val;
807 break;
808 case TWFLAG_CANENCRYPTBACKUP:
809 Can_Encrypt_Backup = val;
810 break;
811 case TWFLAG_DEFAULTS:
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600812 case TWFLAG_WAIT:
813 case TWFLAG_VERIFY:
814 case TWFLAG_CHECK:
815 case TWFLAG_NOTRIM:
816 case TWFLAG_VOLDMANAGED:
817 case TWFLAG_RESIZE:
Matt Mower2416a502016-04-12 19:54:46 -0500818 // Do nothing
819 break;
820 case TWFLAG_DISPLAY:
821 Display_Name = str;
822 break;
823 case TWFLAG_ENCRYPTABLE:
824 case TWFLAG_FORCEENCRYPT:
825 Crypto_Key_Location = str;
826 break;
Ethan Yonkere9afc3d2018-08-30 15:16:27 -0500827 case TWFLAG_FILEENCRYPTION:
828 // This flag isn't used by TWRP but is needed in 9.0 FBE decrypt
829 // fileencryption=ice:aes-256-heh
830 {
831 std::string FBE = str;
832 std::string FBE_contents, FBE_filenames;
833 size_t colon_loc = FBE.find(":");
834 if (colon_loc == std::string::npos) {
835 LOGINFO("Invalid fileencryption fstab flag: '%s'\n", str);
836 break;
837 }
838 FBE_contents = FBE.substr(0, colon_loc);
839 FBE_filenames = FBE.substr(colon_loc + 1);
840 property_set("fbe.contents", FBE_contents.c_str());
841 property_set("fbe.filenames", FBE_filenames.c_str());
842 LOGINFO("FBE contents '%s', filenames '%s'\n", FBE_contents.c_str(), FBE_filenames.c_str());
843 }
844 break;
Matt Mower2416a502016-04-12 19:54:46 -0500845 case TWFLAG_FLASHIMG:
846 Can_Flash_Img = val;
847 break;
848 case TWFLAG_FSFLAGS:
Matt Mower4ab42b12016-04-21 13:52:18 -0500849 Process_FS_Flags(str);
Matt Mower2416a502016-04-12 19:54:46 -0500850 break;
851 case TWFLAG_IGNOREBLKID:
852 Ignore_Blkid = val;
853 break;
854 case TWFLAG_LENGTH:
855 Length = atoi(str);
856 break;
857 case TWFLAG_MOUNTTODECRYPT:
858 Mount_To_Decrypt = val;
859 break;
860 case TWFLAG_REMOVABLE:
861 Removable = val;
862 break;
863 case TWFLAG_RETAINLAYOUTVERSION:
864 Retain_Layout_Version = val;
865 break;
866 case TWFLAG_SETTINGSSTORAGE:
867 Is_Settings_Storage = val;
868 if (Is_Settings_Storage)
869 Is_Storage = true;
870 break;
871 case TWFLAG_STORAGE:
872 Is_Storage = val;
873 break;
874 case TWFLAG_STORAGENAME:
875 Storage_Name = str;
876 break;
877 case TWFLAG_SUBPARTITIONOF:
878 Is_SubPartition = true;
879 SubPartition_Of = str;
880 break;
881 case TWFLAG_SYMLINK:
882 Symlink_Path = str;
883 break;
884 case TWFLAG_USERDATAENCRYPTBACKUP:
885 Use_Userdata_Encryption = val;
886 if (Use_Userdata_Encryption)
887 Can_Encrypt_Backup = true;
888 break;
889 case TWFLAG_USERMRF:
890 Use_Rm_Rf = val;
891 break;
892 case TWFLAG_WIPEDURINGFACTORYRESET:
893 Wipe_During_Factory_Reset = val;
894 if (Wipe_During_Factory_Reset) {
895 Can_Be_Wiped = true;
896 Wipe_Available_in_GUI = true;
897 }
898 break;
899 case TWFLAG_WIPEINGUI:
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600900 case TWFLAG_FORMATTABLE:
Matt Mower2416a502016-04-12 19:54:46 -0500901 Wipe_Available_in_GUI = val;
902 if (Wipe_Available_in_GUI)
903 Can_Be_Wiped = true;
904 break;
Ethan Yonker1b190162016-12-05 15:25:19 -0600905 case TWFLAG_SLOTSELECT:
906 SlotSelect = true;
907 break;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600908 case TWFLAG_ALTDEVICE:
909 Alternate_Block_Device = str;
910 break;
Matt Mower2416a502016-04-12 19:54:46 -0500911 default:
912 // Should not get here
913 LOGINFO("Flag identified for processing, but later unmatched: %i\n", flag);
914 break;
915 }
916}
Dees_Troy51127312012-09-08 13:08:49 -0400917
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600918void TWPartition::Process_TW_Flags(char *flags, bool Display_Error, int fstab_ver) {
Matt Mower2416a502016-04-12 19:54:46 -0500919 char separator[2] = {'\n', 0};
920 char *ptr, *savep;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600921 char source_separator = ';';
922
923 if (fstab_ver == 2)
924 source_separator = ',';
Matt Mower2416a502016-04-12 19:54:46 -0500925
926 // Semicolons within double-quotes are not forbidden, so replace
927 // only the semicolons intended as separators with '\n' for strtok
928 for (unsigned i = 0, skip = 0; i < strlen(flags); i++) {
929 if (flags[i] == '\"')
Dees_Troya13d74f2013-03-24 08:54:55 -0500930 skip = !skip;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600931 if (!skip && flags[i] == source_separator)
Matt Mower2416a502016-04-12 19:54:46 -0500932 flags[i] = separator[0];
Dees_Troy51127312012-09-08 13:08:49 -0400933 }
934
Matt Mower2416a502016-04-12 19:54:46 -0500935 // Avoid issues with potentially nested strtok by using strtok_r
936 ptr = strtok_r(flags, separator, &savep);
937 while (ptr) {
938 int ptr_len = strlen(ptr);
939 const struct flag_list* tw_flag = tw_flags;
Hashcode62bd9e02013-11-19 21:59:42 -0800940
Matt Mower2416a502016-04-12 19:54:46 -0500941 for (; tw_flag->name; tw_flag++) {
942 int flag_len = strlen(tw_flag->name);
943
944 if (strncmp(ptr, tw_flag->name, flag_len) == 0) {
945 bool flag_val = false;
946
947 if (ptr_len > flag_len && (tw_flag->name)[flag_len-1] != '='
948 && ptr[flag_len] != '=') {
949 // Handle flags with same starting string
950 // (e.g. backup and backupname)
951 continue;
952 } else if (ptr_len > flag_len && ptr[flag_len] == '=') {
953 // Handle flags with dual format: Part 1
954 // (e.g. backup and backup=y. backup=y handled here)
955 ptr += flag_len + 1;
956 TWFunc::Strip_Quotes(ptr);
957 // Skip flags with empty argument
958 // (e.g. backup=)
959 if (strlen(ptr) == 0) {
960 LOGINFO("Flag missing argument or should not include '=': %s=\n", tw_flag->name);
961 break;
962 }
963 flag_val = strchr("yY1", *ptr) != NULL;
964 } else if (ptr_len == flag_len
965 && (tw_flag->name)[flag_len-1] == '=') {
966 // Skip flags missing argument after =
967 // (e.g. backupname=)
968 LOGINFO("Flag missing argument: %s\n", tw_flag->name);
969 break;
970 } else if (ptr_len > flag_len
971 && (tw_flag->name)[flag_len-1] == '=') {
972 // Handle arguments to flags
973 // (e.g. backupname="My Stuff")
974 ptr += flag_len;
975 TWFunc::Strip_Quotes(ptr);
976 // Skip flags with empty argument
977 // (e.g. backupname="")
978 if (strlen(ptr) == 0) {
979 LOGINFO("Flag missing argument: %s\n", tw_flag->name);
980 break;
981 }
982 } else if (ptr_len == flag_len) {
983 // Handle flags with dual format: Part 2
984 // (e.g. backup and backup=y. backup handled here)
985 flag_val = true;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600986 } else {
Matt Mower2416a502016-04-12 19:54:46 -0500987 LOGINFO("Flag matched, but could not be processed: %s\n", ptr);
988 break;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600989 }
Matt Mower2416a502016-04-12 19:54:46 -0500990
991 Apply_TW_Flag(tw_flag->flag, ptr, flag_val);
992 break;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600993 }
Matt Mower2416a502016-04-12 19:54:46 -0500994 }
995 if (tw_flag->name == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400996 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000997 LOGERR("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400998 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000999 LOGINFO("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -04001000 }
Matt Mower2416a502016-04-12 19:54:46 -05001001 ptr = strtok_r(NULL, separator, &savep);
Dees_Troy51127312012-09-08 13:08:49 -04001002 }
Dees_Troy51a0e822012-09-05 15:24:24 -04001003}
1004
Dees_Troy5bf43922012-09-07 16:07:55 -04001005bool TWPartition::Is_File_System(string File_System) {
1006 if (File_System == "ext2" ||
Dees_Troy63c8df72012-09-10 14:02:05 -04001007 File_System == "ext3" ||
Dees_Troy5bf43922012-09-07 16:07:55 -04001008 File_System == "ext4" ||
1009 File_System == "vfat" ||
1010 File_System == "ntfs" ||
1011 File_System == "yaffs2" ||
bigbiff bigbiff3e146522012-11-14 14:32:59 -05001012 File_System == "exfat" ||
Dees_Troye5017042013-08-29 16:38:55 +00001013 File_System == "f2fs" ||
Dees_Troy5bf43922012-09-07 16:07:55 -04001014 File_System == "auto")
1015 return true;
1016 else
1017 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001018}
1019
Dees_Troy5bf43922012-09-07 16:07:55 -04001020bool TWPartition::Is_Image(string File_System) {
Dees_Troy5fcd8f92012-10-16 12:22:05 -04001021 if (File_System == "emmc" || File_System == "mtd" || File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -04001022 return true;
1023 else
1024 return false;
1025}
1026
Dees_Troy51127312012-09-08 13:08:49 -04001027bool TWPartition::Make_Dir(string Path, bool Display_Error) {
Ethan Yonker93ac7a02016-11-07 22:05:58 -06001028 if (TWFunc::Get_D_Type_From_Stat(Path) != S_IFDIR)
1029 unlink(Path.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001030 if (!TWFunc::Path_Exists(Path)) {
Dees_Troy51127312012-09-08 13:08:49 -04001031 if (mkdir(Path.c_str(), 0777) == -1) {
1032 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001033 gui_msg(Msg(msg::kError, "create_folder_strerr=Can not create '{1}' folder ({2}).")(Path)(strerror(errno)));
Dees_Troy51127312012-09-08 13:08:49 -04001034 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001035 LOGINFO("Can not create '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -04001036 return false;
1037 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001038 LOGINFO("Created '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -04001039 return true;
1040 }
1041 }
1042 return true;
1043}
1044
Dees_Troy5bf43922012-09-07 16:07:55 -04001045void TWPartition::Setup_File_System(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001046 Can_Be_Mounted = true;
1047 Can_Be_Wiped = true;
1048
Dees_Troy5bf43922012-09-07 16:07:55 -04001049 // Make the mount point folder if it doesn't exist
Dees_Troy51127312012-09-08 13:08:49 -04001050 Make_Dir(Mount_Point, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -04001051 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
1052 Backup_Name = Display_Name;
bigbiffce8f83c2015-12-12 18:30:21 -05001053 Backup_Method = BM_FILES;
Dees_Troy5bf43922012-09-07 16:07:55 -04001054}
1055
Ethan Yonker1b190162016-12-05 15:25:19 -06001056void TWPartition::Setup_Image() {
Dees_Troy5bf43922012-09-07 16:07:55 -04001057 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
1058 Backup_Name = Display_Name;
Gary Peck82599a82012-11-21 16:23:12 -08001059 if (Current_File_System == "emmc")
bigbiffce8f83c2015-12-12 18:30:21 -05001060 Backup_Method = BM_DD;
Gary Peck82599a82012-11-21 16:23:12 -08001061 else if (Current_File_System == "mtd" || Current_File_System == "bml")
bigbiffce8f83c2015-12-12 18:30:21 -05001062 Backup_Method = BM_FLASH_UTILS;
Dees_Troy5bf43922012-09-07 16:07:55 -04001063 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001064 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 -04001065}
1066
Dees_Troye58d5262012-09-21 12:27:57 -04001067void TWPartition::Setup_AndSec(void) {
Dees_Troya13d74f2013-03-24 08:54:55 -05001068 Backup_Display_Name = "Android Secure";
Dees_Troye58d5262012-09-21 12:27:57 -04001069 Backup_Name = "and-sec";
Dees_Troya13d74f2013-03-24 08:54:55 -05001070 Can_Be_Backed_Up = true;
Dees_Troye58d5262012-09-21 12:27:57 -04001071 Has_Android_Secure = true;
1072 Symlink_Path = Mount_Point + "/.android_secure";
1073 Symlink_Mount_Point = "/and-sec";
1074 Backup_Path = Symlink_Mount_Point;
1075 Make_Dir("/and-sec", true);
1076 Recreate_AndSec_Folder();
Kjell Braden3126a112016-06-19 16:58:15 +00001077 Mount_Storage_Retry(true);
Dees_Troye58d5262012-09-21 12:27:57 -04001078}
1079
that9e0593e2014-10-08 00:01:24 +02001080void TWPartition::Setup_Data_Media() {
Ethan Yonker6277c792014-09-15 14:54:30 -05001081 LOGINFO("Setting up '%s' as data/media emulated storage.\n", Mount_Point.c_str());
Ethan Yonker66a19492015-12-10 10:19:45 -06001082 if (Storage_Name.empty() || Storage_Name == "Data")
1083 Storage_Name = "Internal Storage";
Ethan Yonker6277c792014-09-15 14:54:30 -05001084 Has_Data_Media = true;
1085 Is_Storage = true;
Ethan Yonker66a19492015-12-10 10:19:45 -06001086 Storage_Path = Mount_Point + "/media";
Ethan Yonker6277c792014-09-15 14:54:30 -05001087 Symlink_Path = Storage_Path;
Ethan Yonker66a19492015-12-10 10:19:45 -06001088 if (Mount_Point == "/data") {
1089 Is_Settings_Storage = true;
1090 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
1091 Make_Dir("/emmc", false);
1092 Symlink_Mount_Point = "/emmc";
1093 } else {
1094 Make_Dir("/sdcard", false);
1095 Symlink_Mount_Point = "/sdcard";
1096 }
1097 if (Mount(false) && TWFunc::Path_Exists(Mount_Point + "/media/0")) {
1098 Storage_Path = Mount_Point + "/media/0";
1099 Symlink_Path = Storage_Path;
1100 DataManager::SetValue(TW_INTERNAL_PATH, Mount_Point + "/media/0");
1101 UnMount(true);
1102 }
1103 DataManager::SetValue("tw_has_internal", 1);
1104 DataManager::SetValue("tw_has_data_media", 1);
Ethan Yonker3fdcda42016-11-30 12:29:37 -06001105 backup_exclusions.add_absolute_dir("/data/data/com.google.android.music/files");
Ethan Yonker6355b562017-05-01 09:42:17 -05001106 wipe_exclusions.add_absolute_dir(Mount_Point + "/misc/vold"); // adopted storage keys
Ethan Yonker79f88bd2016-12-09 14:52:12 -06001107 ExcludeAll(Mount_Point + "/.layout_version");
1108 ExcludeAll(Mount_Point + "/system/storage.xml");
Ethan Yonker6277c792014-09-15 14:54:30 -05001109 } else {
Ethan Yonker66a19492015-12-10 10:19:45 -06001110 if (Mount(true) && TWFunc::Path_Exists(Mount_Point + "/media/0")) {
1111 Storage_Path = Mount_Point + "/media/0";
1112 Symlink_Path = Storage_Path;
1113 UnMount(true);
1114 }
Ethan Yonker6277c792014-09-15 14:54:30 -05001115 }
Ethan Yonker79f88bd2016-12-09 14:52:12 -06001116 ExcludeAll(Mount_Point + "/media");
Ethan Yonker6277c792014-09-15 14:54:30 -05001117}
1118
Dees_Troy5bf43922012-09-07 16:07:55 -04001119void TWPartition::Find_Real_Block_Device(string& Block, bool Display_Error) {
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06001120 char device[PATH_MAX], realDevice[PATH_MAX];
Dees_Troy5bf43922012-09-07 16:07:55 -04001121
1122 strcpy(device, Block.c_str());
1123 memset(realDevice, 0, sizeof(realDevice));
1124 while (readlink(device, realDevice, sizeof(realDevice)) > 0)
1125 {
1126 strcpy(device, realDevice);
1127 memset(realDevice, 0, sizeof(realDevice));
1128 }
1129
1130 if (device[0] != '/') {
1131 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001132 LOGERR("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001133 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001134 LOGINFO("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001135 return;
Dees_Troy51a0e822012-09-05 15:24:24 -04001136 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -04001137 Block = device;
1138 return;
1139 }
1140}
1141
Kjell Braden3126a112016-06-19 16:58:15 +00001142bool TWPartition::Mount_Storage_Retry(bool Display_Error) {
Dees_Troy8e337f32012-10-13 22:07:49 -04001143 // On some devices, storage doesn't want to mount right away, retry and sleep
Kjell Braden3126a112016-06-19 16:58:15 +00001144 if (!Mount(Display_Error)) {
Dees_Troy8e337f32012-10-13 22:07:49 -04001145 int retry_count = 5;
1146 while (retry_count > 0 && !Mount(false)) {
1147 usleep(500000);
1148 retry_count--;
1149 }
Kjell Braden3126a112016-06-19 16:58:15 +00001150 return Mount(Display_Error);
Dees_Troy8e337f32012-10-13 22:07:49 -04001151 }
Kjell Braden3126a112016-06-19 16:58:15 +00001152 return true;
Dees_Troy8e337f32012-10-13 22:07:49 -04001153}
1154
Dees_Troy38bd7602012-09-14 13:33:53 -04001155bool TWPartition::Find_MTD_Block_Device(string MTD_Name) {
1156 FILE *fp = NULL;
1157 char line[255];
1158
1159 fp = fopen("/proc/mtd", "rt");
1160 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001161 LOGERR("Device does not support /proc/mtd\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001162 return false;
1163 }
1164
1165 while (fgets(line, sizeof(line), fp) != NULL)
1166 {
1167 char device[32], label[32];
1168 unsigned long size = 0;
Dees_Troy38bd7602012-09-14 13:33:53 -04001169 int deviceId;
1170
1171 sscanf(line, "%s %lx %*s %*c%s", device, &size, label);
1172
1173 // Skip header and blank lines
1174 if ((strcmp(device, "dev:") == 0) || (strlen(line) < 8))
1175 continue;
1176
1177 // Strip off the trailing " from the label
1178 label[strlen(label)-1] = '\0';
1179
1180 if (strcmp(label, MTD_Name.c_str()) == 0) {
1181 // We found our device
1182 // Strip off the trailing : from the device
1183 device[strlen(device)-1] = '\0';
1184 if (sscanf(device,"mtd%d", &deviceId) == 1) {
1185 sprintf(device, "/dev/block/mtdblock%d", deviceId);
1186 Primary_Block_Device = device;
Dees_Troy76543db2013-06-19 16:24:30 +00001187 fclose(fp);
1188 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -04001189 }
1190 }
1191 }
1192 fclose(fp);
1193
1194 return false;
1195}
1196
Dees_Troy51127312012-09-08 13:08:49 -04001197bool TWPartition::Get_Size_Via_statfs(bool Display_Error) {
1198 struct statfs st;
1199 string Local_Path = Mount_Point + "/.";
1200
1201 if (!Mount(Display_Error))
1202 return false;
1203
1204 if (statfs(Local_Path.c_str(), &st) != 0) {
1205 if (!Removable) {
1206 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001207 LOGERR("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -04001208 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001209 LOGINFO("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -04001210 }
1211 return false;
1212 }
1213 Size = (st.f_blocks * st.f_bsize);
1214 Used = ((st.f_blocks - st.f_bfree) * st.f_bsize);
1215 Free = (st.f_bfree * st.f_bsize);
1216 Backup_Size = Used;
1217 return true;
1218}
1219
1220bool TWPartition::Get_Size_Via_df(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001221 FILE* fp;
1222 char command[255], line[512];
1223 int include_block = 1;
1224 unsigned int min_len;
1225
1226 if (!Mount(Display_Error))
1227 return false;
1228
Dees_Troy38bd7602012-09-14 13:33:53 -04001229 min_len = Actual_Block_Device.size() + 2;
Dees_Troy51127312012-09-08 13:08:49 -04001230 sprintf(command, "df %s > /tmp/dfoutput.txt", Mount_Point.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001231 TWFunc::Exec_Cmd(command);
Dees_Troy51127312012-09-08 13:08:49 -04001232 fp = fopen("/tmp/dfoutput.txt", "rt");
1233 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001234 LOGINFO("Unable to open /tmp/dfoutput.txt.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001235 return false;
Dees_Troy51127312012-09-08 13:08:49 -04001236 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001237
1238 while (fgets(line, sizeof(line), fp) != NULL)
1239 {
1240 unsigned long blocks, used, available;
1241 char device[64];
1242 char tmpString[64];
1243
1244 if (strncmp(line, "Filesystem", 10) == 0)
1245 continue;
1246 if (strlen(line) < min_len) {
1247 include_block = 0;
1248 continue;
1249 }
1250 if (include_block) {
1251 sscanf(line, "%s %lu %lu %lu", device, &blocks, &used, &available);
1252 } else {
1253 // The device block string is so long that the df information is on the next line
1254 int space_count = 0;
Dees_Troye58d5262012-09-21 12:27:57 -04001255 sprintf(tmpString, "/dev/block/%s", Actual_Block_Device.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001256 while (tmpString[space_count] == 32)
1257 space_count++;
1258 sscanf(line + space_count, "%lu %lu %lu", &blocks, &used, &available);
1259 }
1260
1261 // Adjust block size to byte size
1262 Size = blocks * 1024ULL;
1263 Used = used * 1024ULL;
1264 Free = available * 1024ULL;
1265 Backup_Size = Used;
1266 }
1267 fclose(fp);
1268 return true;
1269}
1270
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001271unsigned long long TWPartition::IOCTL_Get_Block_Size() {
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001272 Find_Actual_Block_Device();
Ethan Yonker483e9f42016-01-11 22:21:18 -06001273
1274 return TWFunc::IOCTL_Get_Block_Size(Actual_Block_Device.c_str());
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001275}
1276
Dees_Troy5bf43922012-09-07 16:07:55 -04001277bool TWPartition::Find_Partition_Size(void) {
1278 FILE* fp;
1279 char line[512];
1280 string tmpdevice;
1281
igoriok87e3d932013-01-31 21:03:53 +02001282 fp = fopen("/proc/dumchar_info", "rt");
1283 if (fp != NULL) {
1284 while (fgets(line, sizeof(line), fp) != NULL)
1285 {
1286 char label[32], device[32];
1287 unsigned long size = 0;
1288
thatd43bf2d2014-09-21 23:13:02 +02001289 sscanf(line, "%s %lx %*x %*u %s", label, &size, device);
igoriok87e3d932013-01-31 21:03:53 +02001290
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05001291 // Skip header, annotation and blank lines
igoriok87e3d932013-01-31 21:03:53 +02001292 if ((strncmp(device, "/dev/", 5) != 0) || (strlen(line) < 8))
1293 continue;
1294
1295 tmpdevice = "/dev/";
1296 tmpdevice += label;
1297 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
1298 Size = size;
1299 fclose(fp);
1300 return true;
1301 }
1302 }
1303 }
1304
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001305 unsigned long long ioctl_size = IOCTL_Get_Block_Size();
1306 if (ioctl_size) {
1307 Size = ioctl_size;
1308 return true;
1309 }
1310
Dees_Troy5bf43922012-09-07 16:07:55 -04001311 // In this case, we'll first get the partitions we care about (with labels)
1312 fp = fopen("/proc/partitions", "rt");
1313 if (fp == NULL)
1314 return false;
1315
1316 while (fgets(line, sizeof(line), fp) != NULL)
1317 {
1318 unsigned long major, minor, blocks;
1319 char device[512];
Dees_Troy5bf43922012-09-07 16:07:55 -04001320
Dees_Troy63c8df72012-09-10 14:02:05 -04001321 if (strlen(line) < 7 || line[0] == 'm') continue;
Dees_Troy5bf43922012-09-07 16:07:55 -04001322 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
1323
1324 tmpdevice = "/dev/block/";
1325 tmpdevice += device;
Dees_Troy38bd7602012-09-14 13:33:53 -04001326 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001327 // Adjust block size to byte size
1328 Size = blocks * 1024ULL;
1329 fclose(fp);
1330 return true;
1331 }
1332 }
1333 fclose(fp);
1334 return false;
1335}
1336
Dees_Troy5bf43922012-09-07 16:07:55 -04001337bool TWPartition::Is_Mounted(void) {
1338 if (!Can_Be_Mounted)
1339 return false;
1340
1341 struct stat st1, st2;
1342 string test_path;
1343
1344 // Check to see if the mount point directory exists
1345 test_path = Mount_Point + "/.";
1346 if (stat(test_path.c_str(), &st1) != 0) return false;
1347
1348 // Check to see if the directory above the mount point exists
1349 test_path = Mount_Point + "/../.";
1350 if (stat(test_path.c_str(), &st2) != 0) return false;
1351
1352 // Compare the device IDs -- if they match then we're (probably) using tmpfs instead of an actual device
1353 int ret = (st1.st_dev != st2.st_dev) ? true : false;
1354
1355 return ret;
1356}
1357
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05001358bool TWPartition::Is_File_System_Writable(void) {
1359 if (!Is_File_System(Current_File_System) || !Is_Mounted())
1360 return false;
1361
1362 string test_path = Mount_Point + "/.";
1363 return (access(test_path.c_str(), W_OK) == 0);
1364}
1365
Dees_Troy5bf43922012-09-07 16:07:55 -04001366bool TWPartition::Mount(bool Display_Error) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001367 int exfat_mounted = 0;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001368 unsigned long flags = Mount_Flags;
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001369
Dees_Troy5bf43922012-09-07 16:07:55 -04001370 if (Is_Mounted()) {
1371 return true;
1372 } else if (!Can_Be_Mounted) {
1373 return false;
1374 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001375
1376 Find_Actual_Block_Device();
1377
1378 // Check the current file system before mounting
1379 Check_FS_Type();
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001380 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/exfat-fuse")) {
Dees_Troye34c1332013-02-06 19:13:00 +00001381 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 +00001382 LOGINFO("cmd: %s\n", cmd.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001383 string result;
1384 if (TWFunc::Exec_Cmd(cmd, result) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001385 LOGINFO("exfat-fuse failed to mount with result '%s', trying vfat\n", result.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001386 Current_File_System = "vfat";
1387 } else {
1388#ifdef TW_NO_EXFAT_FUSE
1389 UnMount(false);
1390 // We'll let the kernel handle it but using exfat-fuse to detect if the file system is actually exfat
1391 // Some kernels let us mount vfat as exfat which doesn't work out too well
1392#else
1393 exfat_mounted = 1;
1394#endif
1395 }
1396 }
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001397
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001398 if (Current_File_System == "ntfs" && (TWFunc::Path_Exists("/sbin/ntfs-3g") || TWFunc::Path_Exists("/sbin/mount.ntfs"))) {
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001399 string cmd;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001400 string Ntfsmount_Binary = "";
1401
1402 if (TWFunc::Path_Exists("/sbin/ntfs-3g"))
1403 Ntfsmount_Binary = "ntfs-3g";
1404 else if (TWFunc::Path_Exists("/sbin/mount.ntfs"))
1405 Ntfsmount_Binary = "mount.ntfs";
1406
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001407 if (Mount_Read_Only)
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001408 cmd = "/sbin/" + Ntfsmount_Binary + " -o ro " + Actual_Block_Device + " " + Mount_Point;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001409 else
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001410 cmd = "/sbin/" + Ntfsmount_Binary + " " + Actual_Block_Device + " " + Mount_Point;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001411 LOGINFO("cmd: '%s'\n", cmd.c_str());
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001412
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001413 if (TWFunc::Exec_Cmd(cmd) == 0) {
1414 return true;
1415 } else {
1416 LOGINFO("ntfs-3g failed to mount, trying regular mount method.\n");
1417 }
1418 }
1419
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001420 if (Mount_Read_Only)
1421 flags |= MS_RDONLY;
1422
Dees_Troy22042032012-12-18 21:23:08 +00001423 if (Fstab_File_System == "yaffs2") {
1424 // mount an MTD partition as a YAFFS2 filesystem.
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001425 flags = MS_NOATIME | MS_NODEV | MS_NODIRATIME;
1426 if (Mount_Read_Only)
1427 flags |= MS_RDONLY;
Dees_Troy76543db2013-06-19 16:24:30 +00001428 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags, NULL) < 0) {
1429 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags | MS_RDONLY, NULL) < 0) {
1430 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001431 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy76543db2013-06-19 16:24:30 +00001432 else
1433 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
1434 return false;
1435 } else {
1436 LOGINFO("Mounted '%s' (MTD) as RO\n", Mount_Point.c_str());
1437 return true;
1438 }
1439 } else {
1440 struct stat st;
1441 string test_path = Mount_Point;
1442 if (stat(test_path.c_str(), &st) < 0) {
1443 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001444 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy76543db2013-06-19 16:24:30 +00001445 else
1446 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
1447 return false;
1448 }
1449 mode_t new_mode = st.st_mode | S_IXUSR | S_IXGRP | S_IXOTH;
1450 if (new_mode != st.st_mode) {
1451 LOGINFO("Fixing execute permissions for %s\n", Mount_Point.c_str());
1452 if (chmod(Mount_Point.c_str(), new_mode) < 0) {
1453 if (Display_Error)
1454 LOGERR("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
1455 else
1456 LOGINFO("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
1457 return false;
1458 }
1459 }
Dees_Troy22042032012-12-18 21:23:08 +00001460 return true;
Dees_Troy76543db2013-06-19 16:24:30 +00001461 }
thatc7572eb2015-03-31 18:55:30 +02001462 }
1463
1464 string mount_fs = Current_File_System;
1465 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sys/module/texfat"))
1466 mount_fs = "texfat";
1467
1468 if (!exfat_mounted &&
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001469 mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), mount_fs.c_str(), flags, Mount_Options.c_str()) != 0 &&
1470 mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), mount_fs.c_str(), flags, NULL) != 0) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001471#ifdef TW_NO_EXFAT_FUSE
1472 if (Current_File_System == "exfat") {
Dees_Troy2673cec2013-04-02 20:22:16 +00001473 LOGINFO("Mounting exfat failed, trying vfat...\n");
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001474 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), "vfat", 0, NULL) != 0) {
Dees_Troy85f44ed2013-01-09 18:42:36 +00001475 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001476 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy85f44ed2013-01-09 18:42:36 +00001477 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001478 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001479 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 +00001480 return false;
Dees_Troy85f44ed2013-01-09 18:42:36 +00001481 }
Dees_Troyb05ddee2013-01-28 20:24:50 +00001482 } else {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001483#endif
bigbiff bigbiff26774a02014-03-29 18:22:00 -04001484 if (!Removable && Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001485 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001486 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001487 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
1488 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 +00001489 return false;
1490#ifdef TW_NO_EXFAT_FUSE
Dees_Troy85f44ed2013-01-09 18:42:36 +00001491 }
1492#endif
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001493 }
Ethan Yonker253368a2014-11-25 15:00:52 -06001494
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001495 if (Removable)
1496 Update_Size(Display_Error);
1497
xiaolu9416f4f2015-06-04 08:22:23 +08001498 if (!Symlink_Mount_Point.empty() && TWFunc::Path_Exists(Symlink_Path)) {
codelover2a3d4ce2015-03-14 20:26:49 +08001499 string Command = "mount -o bind '" + Symlink_Path + "' '" + Symlink_Mount_Point + "'";
Vojtech Bocek05534202013-09-11 08:11:56 +02001500 TWFunc::Exec_Cmd(Command);
Dees_Troy5bf43922012-09-07 16:07:55 -04001501 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001502 return true;
1503}
1504
1505bool TWPartition::UnMount(bool Display_Error) {
1506 if (Is_Mounted()) {
1507 int never_unmount_system;
1508
1509 DataManager::GetValue(TW_DONT_UNMOUNT_SYSTEM, never_unmount_system);
1510 if (never_unmount_system == 1 && Mount_Point == "/system")
1511 return true; // Never unmount system if you're not supposed to unmount it
1512
Matt Mowera8e6d832017-02-14 20:20:59 -06001513 if (Is_Storage && MTP_Storage_ID > 0)
Ethan Yonker726a0202014-12-16 20:01:38 -06001514 PartitionManager.Remove_MTP_Storage(MTP_Storage_ID);
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001515
Dees_Troy38bd7602012-09-14 13:33:53 -04001516 if (!Symlink_Mount_Point.empty())
1517 umount(Symlink_Mount_Point.c_str());
1518
Dees_Troyb05ddee2013-01-28 20:24:50 +00001519 umount(Mount_Point.c_str());
1520 if (Is_Mounted()) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001521 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001522 gui_msg(Msg(msg::kError, "fail_unmount=Failed to unmount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy5bf43922012-09-07 16:07:55 -04001523 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001524 LOGINFO("Unable to unmount '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001525 return false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001526 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -04001527 return true;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001528 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001529 } else {
1530 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001531 }
1532}
1533
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05001534bool TWPartition::ReMount(bool Display_Error) {
1535 if (UnMount(Display_Error))
1536 return Mount(Display_Error);
1537 return false;
1538}
1539
1540bool TWPartition::ReMount_RW(bool Display_Error) {
1541 // No need to remount if already mounted rw
1542 if (Is_File_System_Writable())
1543 return true;
1544
1545 bool ro = Mount_Read_Only;
1546 int flags = Mount_Flags;
1547
1548 Mount_Read_Only = false;
1549 Mount_Flags &= ~MS_RDONLY;
1550
1551 bool ret = ReMount(Display_Error);
1552
1553 Mount_Read_Only = ro;
1554 Mount_Flags = flags;
1555
1556 return ret;
1557}
1558
Gary Peck43acadf2012-11-21 21:19:01 -08001559bool TWPartition::Wipe(string New_File_System) {
Ethan Yonker726a0202014-12-16 20:01:38 -06001560 bool wiped = false, update_crypt = false, recreate_media = true;
Dees_Troy16c2b312013-01-15 16:51:18 +00001561 int check;
1562 string Layout_Filename = Mount_Point + "/.layout_version";
1563
Dees_Troy38bd7602012-09-14 13:33:53 -04001564 if (!Can_Be_Wiped) {
Matt Mower3c366972015-12-25 19:28:31 -06001565 gui_msg(Msg(msg::kError, "cannot_wipe=Partition {1} cannot be wiped.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04001566 return false;
1567 }
1568
Dees_Troyc51f1f92012-09-20 15:32:13 -04001569 if (Mount_Point == "/cache")
Dees_Troy2673cec2013-04-02 20:22:16 +00001570 Log_Offset = 0;
Dees_Troyc51f1f92012-09-20 15:32:13 -04001571
Dees_Troy16c2b312013-01-15 16:51:18 +00001572 if (Retain_Layout_Version && Mount(false) && TWFunc::Path_Exists(Layout_Filename))
1573 TWFunc::copy_file(Layout_Filename, "/.layout_version", 0600);
1574 else
1575 unlink("/.layout_version");
Dees_Troy38bd7602012-09-14 13:33:53 -04001576
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001577 if (Has_Data_Media && Current_File_System == New_File_System) {
Dees_Troy16c2b312013-01-15 16:51:18 +00001578 wiped = Wipe_Data_Without_Wiping_Media();
Ethan Yonker5eac2222014-06-11 12:22:55 -05001579 recreate_media = false;
Dees_Troy16c2b312013-01-15 16:51:18 +00001580 } else {
Dees_Troy16c2b312013-01-15 16:51:18 +00001581 DataManager::GetValue(TW_RM_RF_VAR, check);
1582
Hashcodedabfd492013-08-29 22:45:30 -07001583 if (check || Use_Rm_Rf)
Dees_Troy16c2b312013-01-15 16:51:18 +00001584 wiped = Wipe_RMRF();
1585 else if (New_File_System == "ext4")
1586 wiped = Wipe_EXT4();
1587 else if (New_File_System == "ext2" || New_File_System == "ext3")
1588 wiped = Wipe_EXT23(New_File_System);
1589 else if (New_File_System == "vfat")
1590 wiped = Wipe_FAT();
1591 else if (New_File_System == "exfat")
1592 wiped = Wipe_EXFAT();
1593 else if (New_File_System == "yaffs2")
1594 wiped = Wipe_MTD();
Dees_Troye5017042013-08-29 16:38:55 +00001595 else if (New_File_System == "f2fs")
1596 wiped = Wipe_F2FS();
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001597 else if (New_File_System == "ntfs")
1598 wiped = Wipe_NTFS();
Dees_Troy16c2b312013-01-15 16:51:18 +00001599 else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001600 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 +00001601 unlink("/.layout_version");
1602 return false;
1603 }
1604 update_crypt = wiped;
Gary Pecke8bc5d72012-12-21 06:45:25 -08001605 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001606
Gary Pecke8bc5d72012-12-21 06:45:25 -08001607 if (wiped) {
Dees_Troy1c1ac442013-01-17 21:42:14 +00001608 if (Mount_Point == "/cache")
1609 DataManager::Output_Version();
1610
Dees_Troy16c2b312013-01-15 16:51:18 +00001611 if (TWFunc::Path_Exists("/.layout_version") && Mount(false))
1612 TWFunc::copy_file("/.layout_version", Layout_Filename, 0600);
1613
1614 if (update_crypt) {
1615 Setup_File_System(false);
1616 if (Is_Encrypted && !Is_Decrypted) {
1617 // just wiped an encrypted partition back to its unencrypted state
1618 Is_Encrypted = false;
1619 Is_Decrypted = false;
1620 Decrypted_Block_Device = "";
1621 if (Mount_Point == "/data") {
1622 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1623 DataManager::SetValue(TW_IS_DECRYPTED, 0);
1624 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001625 }
1626 }
Ethan Yonker5eac2222014-06-11 12:22:55 -05001627
Ethan Yonker66a19492015-12-10 10:19:45 -06001628 if (Has_Data_Media && recreate_media) {
Ethan Yonker5eac2222014-06-11 12:22:55 -05001629 Recreate_Media_Folder();
1630 }
Matt Mower209c9632016-01-20 12:08:35 -06001631 if (Is_Storage && Mount(false))
1632 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Gary Pecke8bc5d72012-12-21 06:45:25 -08001633 }
Matt Mower209c9632016-01-20 12:08:35 -06001634
Gary Pecke8bc5d72012-12-21 06:45:25 -08001635 return wiped;
Dees_Troy51a0e822012-09-05 15:24:24 -04001636}
1637
Gary Peck43acadf2012-11-21 21:19:01 -08001638bool TWPartition::Wipe() {
Gary Peck82599a82012-11-21 16:23:12 -08001639 if (Is_File_System(Current_File_System))
1640 return Wipe(Current_File_System);
1641 else
1642 return Wipe(Fstab_File_System);
Gary Peck43acadf2012-11-21 21:19:01 -08001643}
1644
Dees_Troye58d5262012-09-21 12:27:57 -04001645bool TWPartition::Wipe_AndSec(void) {
1646 if (!Has_Android_Secure)
1647 return false;
1648
Dees_Troye58d5262012-09-21 12:27:57 -04001649 if (!Mount(true))
1650 return false;
1651
Ethan Yonker74db1572015-10-28 12:44:49 -05001652 gui_msg(Msg("wiping=Wiping {1}")(Backup_Display_Name));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001653 TWFunc::removeDir(Mount_Point + "/.android_secure/", true);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001654 return true;
Dees_Troye58d5262012-09-21 12:27:57 -04001655}
1656
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001657bool TWPartition::Can_Repair() {
Ethan Yonkera2719152015-05-28 09:44:41 -05001658 if (Mount_Read_Only)
1659 return false;
Matt Mower18794c82015-11-11 16:22:45 -06001660 if (Current_File_System == "vfat" && TWFunc::Path_Exists("/sbin/fsck.fat"))
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001661 return true;
1662 else if ((Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") && TWFunc::Path_Exists("/sbin/e2fsck"))
1663 return true;
1664 else if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/fsck.exfat"))
1665 return true;
1666 else if (Current_File_System == "f2fs" && TWFunc::Path_Exists("/sbin/fsck.f2fs"))
1667 return true;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001668 else if (Current_File_System == "ntfs" && (TWFunc::Path_Exists("/sbin/ntfsfix") || TWFunc::Path_Exists("/sbin/fsck.ntfs")))
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001669 return true;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001670 return false;
1671}
1672
1673bool TWPartition::Repair() {
1674 string command;
1675
1676 if (Current_File_System == "vfat") {
Matt Mower18794c82015-11-11 16:22:45 -06001677 if (!TWFunc::Path_Exists("/sbin/fsck.fat")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001678 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.fat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001679 return false;
1680 }
1681 if (!UnMount(true))
1682 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001683 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.fat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001684 Find_Actual_Block_Device();
Matt Mower18794c82015-11-11 16:22:45 -06001685 command = "/sbin/fsck.fat -y " + Actual_Block_Device;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001686 LOGINFO("Repair command: %s\n", command.c_str());
1687 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001688 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001689 return true;
1690 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001691 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001692 return false;
1693 }
1694 }
1695 if (Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") {
1696 if (!TWFunc::Path_Exists("/sbin/e2fsck")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001697 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("e2fsck"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001698 return false;
1699 }
1700 if (!UnMount(true))
1701 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001702 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("e2fsck"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001703 Find_Actual_Block_Device();
Ethan Yonkera2719152015-05-28 09:44:41 -05001704 command = "/sbin/e2fsck -fp " + Actual_Block_Device;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001705 LOGINFO("Repair command: %s\n", command.c_str());
1706 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001707 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001708 return true;
1709 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001710 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001711 return false;
1712 }
1713 }
1714 if (Current_File_System == "exfat") {
1715 if (!TWFunc::Path_Exists("/sbin/fsck.exfat")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001716 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.exfat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001717 return false;
1718 }
1719 if (!UnMount(true))
1720 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001721 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.exfat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001722 Find_Actual_Block_Device();
1723 command = "/sbin/fsck.exfat " + Actual_Block_Device;
1724 LOGINFO("Repair command: %s\n", command.c_str());
1725 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001726 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001727 return true;
1728 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001729 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001730 return false;
1731 }
1732 }
1733 if (Current_File_System == "f2fs") {
1734 if (!TWFunc::Path_Exists("/sbin/fsck.f2fs")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001735 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.f2fs"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001736 return false;
1737 }
1738 if (!UnMount(true))
1739 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001740 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.f2fs"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001741 Find_Actual_Block_Device();
1742 command = "/sbin/fsck.f2fs " + Actual_Block_Device;
1743 LOGINFO("Repair command: %s\n", command.c_str());
1744 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001745 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001746 return true;
1747 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001748 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001749 return false;
1750 }
1751 }
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001752 if (Current_File_System == "ntfs") {
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001753 string Ntfsfix_Binary;
1754 if (TWFunc::Path_Exists("/sbin/ntfsfix"))
1755 Ntfsfix_Binary = "ntfsfix";
1756 else if (TWFunc::Path_Exists("/sbin/fsck.ntfs"))
1757 Ntfsfix_Binary = "fsck.ntfs";
1758 else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001759 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("ntfsfix"));
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001760 return false;
1761 }
1762 if (!UnMount(true))
1763 return false;
Greg Wallace