blob: acf382a7fc8a1f813dae1fc350835d0ae1be5c82 [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"
Dees_Troy5bf43922012-09-07 16:07:55 -040047extern "C" {
Dees_Troy38bd7602012-09-14 13:33:53 -040048 #include "mtdutils/mtdutils.h"
49 #include "mtdutils/mounts.h"
Dees_Troya95f55c2013-08-17 13:14:43 +000050#ifdef USE_EXT4
Ethan Yonker8373cfe2017-09-08 06:50:54 -050051 // #include "make_ext4fs.h" TODO need ifdef for android8
52 #include <ext4_utils/make_ext4fs.h>
Dees_Troya95f55c2013-08-17 13:14:43 +000053#endif
Ethan Yonker71413f42014-02-26 13:36:08 -060054
55#ifdef TW_INCLUDE_CRYPTO
Ethan Yonker253368a2014-11-25 15:00:52 -060056 #include "crypto/lollipop/cryptfs.h"
Ethan Yonker66a19492015-12-10 10:19:45 -060057 #include "gpt/gpt.h"
Ethan Yonkerbd7492d2016-12-07 13:55:01 -060058 #ifdef TW_INCLUDE_FBE
59 #include "crypto/ext4crypt/Decrypt.h"
Ethan Yonkerbd7492d2016-12-07 13:55:01 -060060 #endif
Ethan Yonkerbc85b632015-08-09 12:48:14 -050061#else
62 #define CRYPT_FOOTER_OFFSET 0x4000
Ethan Yonker71413f42014-02-26 13:36:08 -060063#endif
Dees_Troy5bf43922012-09-07 16:07:55 -040064}
Matt Mower87413642017-01-17 21:14:46 -060065#include <selinux/selinux.h>
Ethan Yonkerf27497f2014-02-09 11:48:33 -060066#include <selinux/label.h>
Dees Troy4159aed2014-02-28 17:24:43 +000067#ifdef HAVE_CAPABILITIES
68#include <sys/capability.h>
69#include <sys/xattr.h>
70#include <linux/xattr.h>
71#endif
HashBanged974bb2016-01-30 14:20:09 -050072#include <sparse_format.h>
Ethan Yonker472f5062016-02-25 13:47:30 -060073#include "progresstracking.hpp"
Dees_Troy51a0e822012-09-05 15:24:24 -040074
bigbiff bigbiff9c754052013-01-09 09:09:08 -050075using namespace std;
76
Ethan Yonker6e8c27a2016-12-22 17:55:57 -060077static 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
78
Dees_Troya95f55c2013-08-17 13:14:43 +000079extern struct selabel_handle *selinux_handle;
Ethan Yonker6277c792014-09-15 14:54:30 -050080extern bool datamedia;
Dees_Troya95f55c2013-08-17 13:14:43 +000081
Hashcode62bd9e02013-11-19 21:59:42 -080082struct flag_list {
83 const char *name;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -060084 unsigned long flag;
Hashcode62bd9e02013-11-19 21:59:42 -080085};
86
Matt Mower4ab42b12016-04-21 13:52:18 -050087const struct flag_list mount_flags[] = {
Ethan Yonker6e8c27a2016-12-22 17:55:57 -060088 { "noatime", MS_NOATIME },
89 { "noexec", MS_NOEXEC },
90 { "nosuid", MS_NOSUID },
91 { "nodev", MS_NODEV },
92 { "nodiratime", MS_NODIRATIME },
93 { "ro", MS_RDONLY },
94 { "rw", 0 },
95 { "remount", MS_REMOUNT },
96 { "bind", MS_BIND },
97 { "rec", MS_REC },
Dees Troyc4bc30e2014-02-03 15:04:19 +000098#ifdef MS_UNBINDABLE
Ethan Yonker6e8c27a2016-12-22 17:55:57 -060099 { "unbindable", MS_UNBINDABLE },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000100#endif
101#ifdef MS_PRIVATE
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600102 { "private", MS_PRIVATE },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000103#endif
104#ifdef MS_SLAVE
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600105 { "slave", MS_SLAVE },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000106#endif
107#ifdef MS_SHARED
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600108 { "shared", MS_SHARED },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000109#endif
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600110 { "sync", MS_SYNCHRONOUS },
111 { 0, 0 },
112};
113
114const char *ignored_mount_items[] = {
115 "defaults=",
116 "errors=",
Ethan Yonker93382822018-11-01 15:25:31 -0500117 "latemount",
118 "sysfs_path=",
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600119 NULL
Hashcode62bd9e02013-11-19 21:59:42 -0800120};
121
Matt Mower2416a502016-04-12 19:54:46 -0500122enum TW_FSTAB_FLAGS {
123 TWFLAG_DEFAULTS, // Retain position
124 TWFLAG_ANDSEC,
125 TWFLAG_BACKUP,
126 TWFLAG_BACKUPNAME,
127 TWFLAG_BLOCKSIZE,
128 TWFLAG_CANBEWIPED,
129 TWFLAG_CANENCRYPTBACKUP,
130 TWFLAG_DISPLAY,
131 TWFLAG_ENCRYPTABLE,
Ethan Yonkere9afc3d2018-08-30 15:16:27 -0500132 TWFLAG_FILEENCRYPTION,
Matt Mower2416a502016-04-12 19:54:46 -0500133 TWFLAG_FLASHIMG,
134 TWFLAG_FORCEENCRYPT,
135 TWFLAG_FSFLAGS,
136 TWFLAG_IGNOREBLKID,
137 TWFLAG_LENGTH,
138 TWFLAG_MOUNTTODECRYPT,
139 TWFLAG_REMOVABLE,
140 TWFLAG_RETAINLAYOUTVERSION,
141 TWFLAG_SETTINGSSTORAGE,
142 TWFLAG_STORAGE,
143 TWFLAG_STORAGENAME,
144 TWFLAG_SUBPARTITIONOF,
145 TWFLAG_SYMLINK,
146 TWFLAG_USERDATAENCRYPTBACKUP,
147 TWFLAG_USERMRF,
148 TWFLAG_WIPEDURINGFACTORYRESET,
149 TWFLAG_WIPEINGUI,
Ethan Yonker1b190162016-12-05 15:25:19 -0600150 TWFLAG_SLOTSELECT,
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600151 TWFLAG_WAIT,
152 TWFLAG_VERIFY,
153 TWFLAG_CHECK,
154 TWFLAG_ALTDEVICE,
155 TWFLAG_NOTRIM,
156 TWFLAG_VOLDMANAGED,
157 TWFLAG_FORMATTABLE,
158 TWFLAG_RESIZE,
Ethan Yonker93382822018-11-01 15:25:31 -0500159 TWFLAG_KEYDIRECTORY,
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 },
Ethan Yonker93382822018-11-01 15:25:31 -0500203 { "keydirectory=", TWFLAG_KEYDIRECTORY },
Matt Mower2416a502016-04-12 19:54:46 -0500204 { 0, 0 },
205};
206
that9e0593e2014-10-08 00:01:24 +0200207TWPartition::TWPartition() {
Dees_Troy51a0e822012-09-05 15:24:24 -0400208 Can_Be_Mounted = false;
209 Can_Be_Wiped = false;
Dees_Troya13d74f2013-03-24 08:54:55 -0500210 Can_Be_Backed_Up = false;
Vojtech Bocek1dc30982013-08-30 21:49:30 +0200211 Use_Rm_Rf = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400212 Wipe_During_Factory_Reset = false;
213 Wipe_Available_in_GUI = false;
214 Is_SubPartition = false;
Dees_Troy2691f9d2012-09-24 11:15:49 -0400215 Has_SubPartition = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400216 SubPartition_Of = "";
217 Symlink_Path = "";
218 Symlink_Mount_Point = "";
219 Mount_Point = "";
Dees_Troye58d5262012-09-21 12:27:57 -0400220 Backup_Path = "";
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600221 Wildcard_Block_Device = false;
222 Sysfs_Entry = "";
Dees_Troy38bd7602012-09-14 13:33:53 -0400223 Actual_Block_Device = "";
224 Primary_Block_Device = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400225 Alternate_Block_Device = "";
226 Removable = false;
227 Is_Present = false;
228 Length = 0;
229 Size = 0;
230 Used = 0;
231 Free = 0;
232 Backup_Size = 0;
233 Can_Be_Encrypted = false;
234 Is_Encrypted = false;
235 Is_Decrypted = false;
Ethan Yonkerbd7492d2016-12-07 13:55:01 -0600236 Is_FBE = false;
Ethan Yonker253368a2014-11-25 15:00:52 -0600237 Mount_To_Decrypt = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400238 Decrypted_Block_Device = "";
239 Display_Name = "";
Dees_Troya13d74f2013-03-24 08:54:55 -0500240 Backup_Display_Name = "";
241 Storage_Name = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400242 Backup_Name = "";
Dees_Troy63c8df72012-09-10 14:02:05 -0400243 Backup_FileName = "";
Dees_Troy38bd7602012-09-14 13:33:53 -0400244 MTD_Name = "";
bigbiffce8f83c2015-12-12 18:30:21 -0500245 Backup_Method = BM_NONE;
Dees_Troy83bd4832013-05-04 12:39:56 +0000246 Can_Encrypt_Backup = false;
247 Use_Userdata_Encryption = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400248 Has_Data_Media = false;
Dees_Troye58d5262012-09-21 12:27:57 -0400249 Has_Android_Secure = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400250 Is_Storage = false;
Dees_Troya13d74f2013-03-24 08:54:55 -0500251 Is_Settings_Storage = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400252 Storage_Path = "";
253 Current_File_System = "";
254 Fstab_File_System = "";
Hashcode62bd9e02013-11-19 21:59:42 -0800255 Mount_Flags = 0;
256 Mount_Options = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400257 Format_Block_Size = 0;
Dees_Troy68cab492012-12-12 19:29:35 +0000258 Ignore_Blkid = false;
Dees_Troy16c2b312013-01-15 16:51:18 +0000259 Retain_Layout_Version = false;
Ethan Yonker253368a2014-11-25 15:00:52 -0600260 Crypto_Key_Location = "footer";
Ethan Yonker726a0202014-12-16 20:01:38 -0600261 MTP_Storage_ID = 0;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600262 Can_Flash_Img = false;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500263 Mount_Read_Only = false;
Ethan Yonker66a19492015-12-10 10:19:45 -0600264 Is_Adopted_Storage = false;
265 Adopted_GUID = "";
Ethan Yonker1b190162016-12-05 15:25:19 -0600266 SlotSelect = false;
Ethan Yonker93382822018-11-01 15:25:31 -0500267 Key_Directory = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400268}
269
270TWPartition::~TWPartition(void) {
271 // Do nothing
272}
273
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600274bool 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 -0500275 char full_line[MAX_FSTAB_LINE_LENGTH];
Matt Mower2416a502016-04-12 19:54:46 -0500276 char twflags[MAX_FSTAB_LINE_LENGTH] = "";
Dees_Troy5bf43922012-09-07 16:07:55 -0400277 char* ptr;
Matt Mower2b2dd152016-04-26 11:24:08 -0500278 int line_len = strlen(fstab_line), index = 0, item_index = 0;
Dees_Troya13d74f2013-03-24 08:54:55 -0500279 bool skip = false;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600280 int fstab_version = 1, mount_point_index = 0, fs_index = 1, block_device_index = 2;
281 TWPartition *additional_entry = NULL;
282 std::map<string, Flags_Map>::iterator it;
Dees_Troy5bf43922012-09-07 16:07:55 -0400283
Matt Mower2b2dd152016-04-26 11:24:08 -0500284 strlcpy(full_line, fstab_line, sizeof(full_line));
Dees_Troy51127312012-09-08 13:08:49 -0400285 for (index = 0; index < line_len; index++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500286 if (full_line[index] == 34)
287 skip = !skip;
288 if (!skip && full_line[index] <= 32)
Dees_Troy5bf43922012-09-07 16:07:55 -0400289 full_line[index] = '\0';
Dees_Troy5bf43922012-09-07 16:07:55 -0400290 }
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600291 if (line_len < 10)
292 return false; // There can't possibly be a valid fstab line that is less than 10 chars
293 if (strncmp(fstab_line, "/dev/", strlen("/dev/")) == 0 || strncmp(fstab_line, "/devices/", strlen("/devices/")) == 0) {
294 fstab_version = 2;
295 block_device_index = 0;
296 mount_point_index = 1;
297 fs_index = 2;
298 }
299
300 index = 0;
Dees_Troy5bf43922012-09-07 16:07:55 -0400301 while (index < line_len) {
302 while (index < line_len && full_line[index] == '\0')
303 index++;
304 if (index >= line_len)
305 continue;
306 ptr = full_line + index;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600307 if (item_index == mount_point_index) {
308 Mount_Point = ptr;
309 if (fstab_version == 2) {
310 additional_entry = PartitionManager.Find_Partition_By_Path(Mount_Point);
311 if (additional_entry) {
312 LOGINFO("Found an additional entry for '%s'\n", Mount_Point.c_str());
313 }
314 }
315 LOGINFO("Processing '%s'\n", Mount_Point.c_str());
316 Backup_Path = Mount_Point;
317 Storage_Path = Mount_Point;
318 Display_Name = ptr + 1;
319 Backup_Display_Name = Display_Name;
320 Storage_Name = Display_Name;
321 item_index++;
322 } else if (item_index == fs_index) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400323 // File System
324 Fstab_File_System = ptr;
325 Current_File_System = ptr;
326 item_index++;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600327 } else if (item_index == block_device_index) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400328 // Primary Block Device
Dees_Troy38bd7602012-09-14 13:33:53 -0400329 if (Fstab_File_System == "mtd" || Fstab_File_System == "yaffs2") {
Dees_Troy094207a2012-09-26 12:00:39 -0400330 MTD_Name = ptr;
331 Find_MTD_Block_Device(MTD_Name);
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400332 } else if (Fstab_File_System == "bml") {
333 if (Mount_Point == "/boot")
334 MTD_Name = "boot";
335 else if (Mount_Point == "/recovery")
336 MTD_Name = "recovery";
337 Primary_Block_Device = ptr;
338 if (*ptr != '/')
Dees_Troy2673cec2013-04-02 20:22:16 +0000339 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 -0400340 } else if (*ptr != '/') {
Dees_Troy5bf43922012-09-07 16:07:55 -0400341 if (Display_Error)
Matt Mower2b2dd152016-04-26 11:24:08 -0500342 LOGERR("Invalid block device '%s' in fstab line '%s'", ptr, fstab_line);
Dees_Troy5bf43922012-09-07 16:07:55 -0400343 else
Matt Mower2b2dd152016-04-26 11:24:08 -0500344 LOGINFO("Invalid block device '%s' in fstab line '%s'", ptr, fstab_line);
Matt Mower72c87ce2016-04-26 14:34:56 -0500345 return false;
Dees_Troy38bd7602012-09-14 13:33:53 -0400346 } else {
347 Primary_Block_Device = ptr;
348 Find_Real_Block_Device(Primary_Block_Device, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400349 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400350 item_index++;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600351 } else if (item_index > 2) {
352 if (fstab_version == 2) {
353 if (item_index == 3) {
354 Process_FS_Flags(ptr);
355 if (additional_entry) {
356 additional_entry->Save_FS_Flags(Fstab_File_System, Mount_Flags, Mount_Options);
357 return false; // We save the extra fs flags in the other partition entry and by returning false, this entry will be deleted
358 }
359 } else {
360 strlcpy(twflags, ptr, sizeof(twflags));
361 }
362 item_index++;
363 } else if (*ptr == '/') { // v2 fstab does not allow alternate block devices
Dees_Troy5bf43922012-09-07 16:07:55 -0400364 // Alternate Block Device
365 Alternate_Block_Device = ptr;
366 Find_Real_Block_Device(Alternate_Block_Device, Display_Error);
367 } else if (strlen(ptr) > 7 && strncmp(ptr, "length=", 7) == 0) {
368 // Partition length
369 ptr += 7;
370 Length = atoi(ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400371 } else if (strlen(ptr) > 6 && strncmp(ptr, "flags=", 6) == 0) {
372 // Custom flags, save for later so that new values aren't overwritten by defaults
373 ptr += 6;
Matt Mower2416a502016-04-12 19:54:46 -0500374 strlcpy(twflags, ptr, sizeof(twflags));
Dees_Troy38bd7602012-09-14 13:33:53 -0400375 } else if (strlen(ptr) == 4 && (strncmp(ptr, "NULL", 4) == 0 || strncmp(ptr, "null", 4) == 0 || strncmp(ptr, "null", 4) == 0)) {
376 // Do nothing
Dees_Troy5bf43922012-09-07 16:07:55 -0400377 } else {
378 // Unhandled data
Matt Mower2b2dd152016-04-26 11:24:08 -0500379 LOGINFO("Unhandled fstab information '%s' in fstab line '%s'\n", ptr, fstab_line);
Dees_Troy5bf43922012-09-07 16:07:55 -0400380 }
381 }
382 while (index < line_len && full_line[index] != '\0')
383 index++;
384 }
385
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600386 // override block devices from the v2 fstab with the ones we read from the twrp.flags file in case they are different
387 if (fstab_version == 2 && twrp_flags && twrp_flags->size() > 0) {
388 it = twrp_flags->find(Mount_Point);
389 if (it != twrp_flags->end()) {
390 if (!it->second.Primary_Block_Device.empty()) {
391 Primary_Block_Device = it->second.Primary_Block_Device;
392 Find_Real_Block_Device(Primary_Block_Device, Display_Error);
393 }
394 if (!it->second.Alternate_Block_Device.empty()) {
395 Alternate_Block_Device = it->second.Alternate_Block_Device;
396 Find_Real_Block_Device(Alternate_Block_Device, Display_Error);
397 }
398 }
399 }
400
401 if (strncmp(fstab_line, "/devices/", strlen("/devices/")) == 0) {
402 Sysfs_Entry = Primary_Block_Device;
403 Primary_Block_Device = "";
404 Is_Storage = true;
405 Removable = true;
406 Wipe_Available_in_GUI = true;
407 Wildcard_Block_Device = true;
408 }
409 if (Primary_Block_Device.find("*") != string::npos)
410 Wildcard_Block_Device = true;
411
412 if (Mount_Point == "auto") {
413 Mount_Point = "/auto";
414 char autoi[5];
415 sprintf(autoi, "%i", auto_index);
416 Mount_Point += autoi;
417 Backup_Path = Mount_Point;
418 Storage_Path = Mount_Point;
419 auto_index++;
420 Setup_File_System(Display_Error);
421 Display_Name = "Storage";
422 Backup_Display_Name = Display_Name;
423 Storage_Name = Display_Name;
424 Can_Be_Backed_Up = false;
425 Wipe_Available_in_GUI = true;
426 Is_Storage = true;
427 Removable = true;
428 Wipe_Available_in_GUI = true;
429 } else if (!Is_File_System(Fstab_File_System) && !Is_Image(Fstab_File_System)) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400430 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000431 LOGERR("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400432 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000433 LOGINFO("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Matt Mower72c87ce2016-04-26 14:34:56 -0500434 return false;
Dees_Troy5bf43922012-09-07 16:07:55 -0400435 } else if (Is_File_System(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400436 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400437 Setup_File_System(Display_Error);
Captain Throwback9d6feb52018-07-27 10:05:24 -0400438 if (Mount_Point == PartitionManager.Get_Android_Root_Path()) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400439 Display_Name = "System";
Dees_Troya13d74f2013-03-24 08:54:55 -0500440 Backup_Display_Name = Display_Name;
441 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400442 Wipe_Available_in_GUI = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500443 Can_Be_Backed_Up = true;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500444 Mount_Read_Only = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400445 } else if (Mount_Point == "/data") {
446 Display_Name = "Data";
Dees_Troya13d74f2013-03-24 08:54:55 -0500447 Backup_Display_Name = Display_Name;
448 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400449 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400450 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500451 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000452 Can_Encrypt_Backup = true;
453 Use_Userdata_Encryption = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400454 } else if (Mount_Point == "/cache") {
455 Display_Name = "Cache";
Dees_Troya13d74f2013-03-24 08:54:55 -0500456 Backup_Display_Name = Display_Name;
457 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400458 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400459 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500460 Can_Be_Backed_Up = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400461 } else if (Mount_Point == "/datadata") {
Dees_Troy51127312012-09-08 13:08:49 -0400462 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400463 Display_Name = "DataData";
Dees_Troya13d74f2013-03-24 08:54:55 -0500464 Backup_Display_Name = Display_Name;
465 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400466 Is_SubPartition = true;
467 SubPartition_Of = "/data";
Dees_Troy5bf43922012-09-07 16:07:55 -0400468 DataManager::SetValue(TW_HAS_DATADATA, 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500469 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000470 Can_Encrypt_Backup = true;
471 Use_Userdata_Encryption = false; // This whole partition should be encrypted
Dees_Troy5bf43922012-09-07 16:07:55 -0400472 } else if (Mount_Point == "/sd-ext") {
Dees_Troy51127312012-09-08 13:08:49 -0400473 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400474 Display_Name = "SD-Ext";
Dees_Troya13d74f2013-03-24 08:54:55 -0500475 Backup_Display_Name = Display_Name;
476 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400477 Wipe_Available_in_GUI = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400478 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500479 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000480 Can_Encrypt_Backup = true;
481 Use_Userdata_Encryption = true;
Dees_Troy2c50e182012-09-26 20:05:28 -0400482 } else if (Mount_Point == "/boot") {
483 Display_Name = "Boot";
Dees_Troya13d74f2013-03-24 08:54:55 -0500484 Backup_Display_Name = Display_Name;
Dees_Troy2c50e182012-09-26 20:05:28 -0400485 DataManager::SetValue("tw_boot_is_mountable", 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500486 Can_Be_Backed_Up = true;
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500487 } else if (Mount_Point == "/vendor") {
488 Display_Name = "Vendor";
489 Backup_Display_Name = Display_Name;
490 Storage_Name = Display_Name;
491 Mount_Read_Only = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400492 }
493#ifdef TW_EXTERNAL_STORAGE_PATH
494 if (Mount_Point == EXPAND(TW_EXTERNAL_STORAGE_PATH)) {
495 Is_Storage = true;
496 Storage_Path = EXPAND(TW_EXTERNAL_STORAGE_PATH);
Dees_Troyc51f1f92012-09-20 15:32:13 -0400497 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500498 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400499#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000500 if (Mount_Point == "/sdcard" || Mount_Point == "/external_sd" || Mount_Point == "/external_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400501 Is_Storage = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400502 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500503 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400504#endif
Dees_Troyb05ddee2013-01-28 20:24:50 +0000505 }
Dees_Troy8170a922012-09-18 15:40:25 -0400506#ifdef TW_INTERNAL_STORAGE_PATH
507 if (Mount_Point == EXPAND(TW_INTERNAL_STORAGE_PATH)) {
508 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500509 Is_Settings_Storage = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400510 Storage_Path = EXPAND(TW_INTERNAL_STORAGE_PATH);
Dees_Troya13d74f2013-03-24 08:54:55 -0500511 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400512 }
513#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000514 if (Mount_Point == "/emmc" || Mount_Point == "/internal_sd" || Mount_Point == "/internal_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400515 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500516 Is_Settings_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500517 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400518 }
519#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400520 } else if (Is_Image(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400521 Find_Actual_Block_Device();
Ethan Yonker1b190162016-12-05 15:25:19 -0600522 Setup_Image();
Dees_Troya13d74f2013-03-24 08:54:55 -0500523 if (Mount_Point == "/boot") {
524 Display_Name = "Boot";
525 Backup_Display_Name = Display_Name;
526 Can_Be_Backed_Up = true;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600527 Can_Flash_Img = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500528 } else if (Mount_Point == "/recovery") {
529 Display_Name = "Recovery";
530 Backup_Display_Name = Display_Name;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600531 Can_Flash_Img = true;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500532 } else if (Mount_Point == "/system_image") {
533 Display_Name = "System Image";
534 Backup_Display_Name = Display_Name;
HashBanged974bb2016-01-30 14:20:09 -0500535 Can_Flash_Img = true;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500536 Can_Be_Backed_Up = true;
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500537 } else if (Mount_Point == "/vendor_image") {
538 Display_Name = "Vendor Image";
539 Backup_Display_Name = Display_Name;
HashBanged974bb2016-01-30 14:20:09 -0500540 Can_Flash_Img = true;
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500541 Can_Be_Backed_Up = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500542 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400543 }
544
Matt Mower2416a502016-04-12 19:54:46 -0500545 // Process TWRP fstab flags
546 if (strlen(twflags) > 0) {
547 string Prev_Display_Name = Display_Name;
548 string Prev_Storage_Name = Storage_Name;
549 string Prev_Backup_Display_Name = Backup_Display_Name;
550 Display_Name = "";
551 Storage_Name = "";
552 Backup_Display_Name = "";
553
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600554 Process_TW_Flags(twflags, (fstab_version == 1), fstab_version);
555 Save_FS_Flags(Fstab_File_System, Mount_Flags, Mount_Options);
Matt Mower2416a502016-04-12 19:54:46 -0500556
557 bool has_display_name = !Display_Name.empty();
558 bool has_storage_name = !Storage_Name.empty();
559 bool has_backup_name = !Backup_Display_Name.empty();
560 if (!has_display_name) Display_Name = Prev_Display_Name;
561 if (!has_storage_name) Storage_Name = Prev_Storage_Name;
562 if (!has_backup_name) Backup_Display_Name = Prev_Backup_Display_Name;
563
564 if (has_display_name && !has_storage_name)
565 Storage_Name = Display_Name;
566 if (!has_display_name && has_storage_name)
567 Display_Name = Storage_Name;
568 if (has_display_name && !has_backup_name && Backup_Display_Name != "Android Secure")
569 Backup_Display_Name = Display_Name;
570 if (!has_display_name && has_backup_name)
571 Display_Name = Backup_Display_Name;
572 }
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600573
574 if (fstab_version == 2 && twrp_flags && twrp_flags->size() > 0) {
575 it = twrp_flags->find(Mount_Point);
576 if (it != twrp_flags->end()) {
577 char twrpflags[MAX_FSTAB_LINE_LENGTH] = "";
578 int skip = 0;
579 string Flags = it->second.Flags;
580 strcpy(twrpflags, Flags.c_str());
581 if (strlen(twrpflags) > strlen("flags=") && strncmp(twrpflags, "flags=", strlen("flags=")) == 0)
582 skip += strlen("flags=");
583 char* flagptr = twrpflags;
584 flagptr += skip;
585 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
586 }
587 }
nkk7198fc3992017-12-16 16:26:42 +0200588
589 if (Mount_Point == "/persist" && Can_Be_Mounted) {
590 bool mounted = Is_Mounted();
591 if (mounted || Mount(false)) {
592 // Read the backup settings file
593 DataManager::LoadPersistValues();
594 TWFunc::Fixup_Time_On_Boot("/persist/time/");
595 if (!mounted)
596 UnMount(false);
597 }
598 }
599
Dees_Troy51127312012-09-08 13:08:49 -0400600 return true;
601}
602
Matt Mower72c87ce2016-04-26 14:34:56 -0500603void TWPartition::Partition_Post_Processing(bool Display_Error) {
604 if (Mount_Point == "/data")
605 Setup_Data_Partition(Display_Error);
606 else if (Mount_Point == "/cache")
607 Setup_Cache_Partition(Display_Error);
608}
609
Ethan Yonkerbd7492d2016-12-07 13:55:01 -0600610void TWPartition::ExcludeAll(const string& path) {
611 backup_exclusions.add_absolute_dir(path);
612 wipe_exclusions.add_absolute_dir(path);
613}
614
Matt Mower72c87ce2016-04-26 14:34:56 -0500615void TWPartition::Setup_Data_Partition(bool Display_Error) {
616 if (Mount_Point != "/data")
617 return;
618
619 // Ensure /data is not mounted as tmpfs for qcom hardware decrypt
620 UnMount(false);
621
622#ifdef TW_INCLUDE_CRYPTO
623 if (datamedia)
624 Setup_Data_Media();
625 Can_Be_Encrypted = true;
626 char crypto_blkdev[255];
627 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
628 if (strcmp(crypto_blkdev, "error") != 0) {
629 DataManager::SetValue(TW_IS_DECRYPTED, 1);
630 Is_Encrypted = true;
631 Is_Decrypted = true;
Ethan Yonker93382822018-11-01 15:25:31 -0500632 if (Key_Directory.empty())
633 Is_FBE = false;
634 else
635 Is_FBE = true;
Ethan Yonkerbd7492d2016-12-07 13:55:01 -0600636 DataManager::SetValue(TW_IS_FBE, 0);
Matt Mower72c87ce2016-04-26 14:34:56 -0500637 Decrypted_Block_Device = crypto_blkdev;
638 LOGINFO("Data already decrypted, new block device: '%s'\n", crypto_blkdev);
639 } else if (!Mount(false)) {
640 if (Is_Present) {
Ethan Yonker93382822018-11-01 15:25:31 -0500641 if (Key_Directory.empty()) {
642 set_partition_data(Actual_Block_Device.c_str(), Crypto_Key_Location.c_str(), Fstab_File_System.c_str());
643 if (cryptfs_check_footer() == 0) {
644 Is_Encrypted = true;
645 Is_Decrypted = false;
646 Can_Be_Mounted = false;
647 Current_File_System = "emmc";
648 Setup_Image();
649 DataManager::SetValue(TW_IS_ENCRYPTED, 1);
650 DataManager::SetValue(TW_CRYPTO_PWTYPE, cryptfs_get_password_type());
651 DataManager::SetValue(TW_CRYPTO_PASSWORD, "");
652 DataManager::SetValue("tw_crypto_display", "");
653 } else {
654 gui_err("mount_data_footer=Could not mount /data and unable to find crypto footer.");
655 }
656 } else {
Matt Mower72c87ce2016-04-26 14:34:56 -0500657 Is_Encrypted = true;
658 Is_Decrypted = false;
Matt Mower72c87ce2016-04-26 14:34:56 -0500659 }
Ethan Yonker93382822018-11-01 15:25:31 -0500660 } else if (Key_Directory.empty()) {
Matt Mower72c87ce2016-04-26 14:34:56 -0500661 LOGERR("Primary block device '%s' for mount point '%s' is not present!\n", Primary_Block_Device.c_str(), Mount_Point.c_str());
662 }
663 } else {
Ethan Yonker93382822018-11-01 15:25:31 -0500664 Decrypt_FBE_DE();
Matt Mower72c87ce2016-04-26 14:34:56 -0500665 }
666 if (datamedia && (!Is_Encrypted || (Is_Encrypted && Is_Decrypted))) {
667 Setup_Data_Media();
668 Recreate_Media_Folder();
669 }
670#else
671 if (datamedia) {
672 Setup_Data_Media();
673 Recreate_Media_Folder();
674 }
675#endif
676}
677
Ethan Yonker93382822018-11-01 15:25:31 -0500678bool TWPartition::Decrypt_FBE_DE() {
679if (TWFunc::Path_Exists("/data/unencrypted/key/version")) {
680 LOGINFO("File Based Encryption is present\n");
681#ifdef TW_INCLUDE_FBE
682 ExcludeAll(Mount_Point + "/convert_fbe");
683 ExcludeAll(Mount_Point + "/unencrypted");
684 //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
685 ExcludeAll(Mount_Point + "/misc/vold/user_keys");
686 //ExcludeAll(Mount_Point + "/system_ce");
687 //ExcludeAll(Mount_Point + "/system_de");
688 //ExcludeAll(Mount_Point + "/misc_ce");
689 //ExcludeAll(Mount_Point + "/misc_de");
690 ExcludeAll(Mount_Point + "/system/gatekeeper.password.key");
691 ExcludeAll(Mount_Point + "/system/gatekeeper.pattern.key");
692 ExcludeAll(Mount_Point + "/system/locksettings.db");
693 //ExcludeAll(Mount_Point + "/system/locksettings.db-shm"); // don't seem to need this one, but the other 2 are needed
694 ExcludeAll(Mount_Point + "/system/locksettings.db-wal");
695 //ExcludeAll(Mount_Point + "/user_de");
696 //ExcludeAll(Mount_Point + "/misc/profiles/cur/0"); // might be important later
697 ExcludeAll(Mount_Point + "/misc/gatekeeper");
698 ExcludeAll(Mount_Point + "/misc/keystore");
699 ExcludeAll(Mount_Point + "/drm/kek.dat");
700 ExcludeAll(Mount_Point + "/system_de/0/spblob"); // contains data needed to decrypt pixel 2
701 int retry_count = 3;
702 while (!Decrypt_DE() && --retry_count)
703 usleep(2000);
704 if (retry_count > 0) {
705 property_set("ro.crypto.state", "encrypted");
706 Is_Encrypted = true;
707 Is_Decrypted = false;
708 Is_FBE = true;
709 DataManager::SetValue(TW_IS_FBE, 1);
710 DataManager::SetValue(TW_IS_ENCRYPTED, 1);
711 string filename;
712 int pwd_type = Get_Password_Type(0, filename);
713 if (pwd_type < 0) {
714 LOGERR("This TWRP does not have synthetic password decrypt support\n");
715 pwd_type = 0; // default password
716 }
717 DataManager::SetValue(TW_CRYPTO_PWTYPE, pwd_type);
718 DataManager::SetValue(TW_CRYPTO_PASSWORD, "");
719 DataManager::SetValue("tw_crypto_display", "");
720 return true;
721 }
722#else
723 LOGERR("FBE found but FBE support not present in TWRP\n");
724#endif
725 }
726 return false;
727}
728
Matt Mower72c87ce2016-04-26 14:34:56 -0500729void TWPartition::Setup_Cache_Partition(bool Display_Error __unused) {
730 if (Mount_Point != "/cache")
731 return;
732
733 if (!Mount(true))
734 return;
735
736 if (!TWFunc::Path_Exists("/cache/recovery/.")) {
737 LOGINFO("Recreating /cache/recovery folder\n");
738 if (mkdir("/cache/recovery", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0)
739 LOGERR("Could not create /cache/recovery\n");
740 }
741}
742
Matt Mower4ab42b12016-04-21 13:52:18 -0500743void TWPartition::Process_FS_Flags(const char *str) {
744 char *options = strdup(str);
745 char *ptr, *savep;
Hashcode62bd9e02013-11-19 21:59:42 -0800746
Matt Mower4ab42b12016-04-21 13:52:18 -0500747 Mount_Options = "";
Hashcode62bd9e02013-11-19 21:59:42 -0800748
Matt Mower4ab42b12016-04-21 13:52:18 -0500749 // Avoid issues with potentially nested strtok by using strtok_r
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600750 for (ptr = strtok_r(options, ",", &savep); ptr; ptr = strtok_r(NULL, ",", &savep)) {
751 char *equals = strstr(ptr, "=");
752 size_t name_len;
Matt Mower4ab42b12016-04-21 13:52:18 -0500753
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600754 if (!equals)
755 name_len = strlen(ptr);
756 else
757 name_len = equals - ptr;
758
759 // There are some flags that we want to ignore in TWRP
760 bool found_match = false;
761 for (const char** ignored_mount_item = ignored_mount_items; *ignored_mount_item; ignored_mount_item++) {
762 if (strncmp(ptr, *ignored_mount_item, name_len) == 0) {
763 found_match = true;
Hashcode62bd9e02013-11-19 21:59:42 -0800764 break;
765 }
766 }
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600767 if (found_match)
768 continue;
Hashcode62bd9e02013-11-19 21:59:42 -0800769
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600770 // mount_flags are never postfixed by '='
771 if (!equals) {
772 const struct flag_list* mount_flag = mount_flags;
773 for (; mount_flag->name; mount_flag++) {
774 if (strcmp(ptr, mount_flag->name) == 0) {
775 if (mount_flag->flag == MS_RDONLY)
776 Mount_Read_Only = true;
777 else
778 Mount_Flags |= (unsigned)mount_flag->flag;
779 found_match = true;
780 break;
781 }
782 }
783 if (found_match)
784 continue;
Matt Mower4ab42b12016-04-21 13:52:18 -0500785 }
786
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600787 // If we aren't ignoring this flag and it's not a mount flag, then it must be a mount option
788 if (!Mount_Options.empty())
789 Mount_Options += ",";
790 Mount_Options += ptr;
Matt Mower4ab42b12016-04-21 13:52:18 -0500791 }
792 free(options);
Hashcode62bd9e02013-11-19 21:59:42 -0800793}
794
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600795void TWPartition::Save_FS_Flags(const string& local_File_System, int local_Mount_Flags, const string& local_Mount_Options) {
796 partition_fs_flags_struct flags;
797 flags.File_System = local_File_System;
798 flags.Mount_Flags = local_Mount_Flags;
799 flags.Mount_Options = local_Mount_Options;
800 fs_flags.push_back(flags);
801}
802
Matt Mower2416a502016-04-12 19:54:46 -0500803void TWPartition::Apply_TW_Flag(const unsigned flag, const char* str, const bool val) {
804 switch (flag) {
805 case TWFLAG_ANDSEC:
806 Has_Android_Secure = val;
807 break;
808 case TWFLAG_BACKUP:
809 Can_Be_Backed_Up = val;
810 break;
811 case TWFLAG_BACKUPNAME:
812 Backup_Display_Name = str;
813 break;
814 case TWFLAG_BLOCKSIZE:
815 Format_Block_Size = (unsigned long)(atol(str));
816 break;
817 case TWFLAG_CANBEWIPED:
818 Can_Be_Wiped = val;
819 break;
820 case TWFLAG_CANENCRYPTBACKUP:
821 Can_Encrypt_Backup = val;
822 break;
823 case TWFLAG_DEFAULTS:
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600824 case TWFLAG_WAIT:
825 case TWFLAG_VERIFY:
826 case TWFLAG_CHECK:
827 case TWFLAG_NOTRIM:
828 case TWFLAG_VOLDMANAGED:
829 case TWFLAG_RESIZE:
Matt Mower2416a502016-04-12 19:54:46 -0500830 // Do nothing
831 break;
832 case TWFLAG_DISPLAY:
833 Display_Name = str;
834 break;
835 case TWFLAG_ENCRYPTABLE:
836 case TWFLAG_FORCEENCRYPT:
837 Crypto_Key_Location = str;
838 break;
Ethan Yonkere9afc3d2018-08-30 15:16:27 -0500839 case TWFLAG_FILEENCRYPTION:
840 // This flag isn't used by TWRP but is needed in 9.0 FBE decrypt
841 // fileencryption=ice:aes-256-heh
842 {
843 std::string FBE = str;
Ethan Yonkere9afc3d2018-08-30 15:16:27 -0500844 size_t colon_loc = FBE.find(":");
845 if (colon_loc == std::string::npos) {
Ethan Yonker93382822018-11-01 15:25:31 -0500846 property_set("fbe.contents", FBE.c_str());
847 property_set("fbe.filenames", "");
848 LOGINFO("FBE contents '%s', filenames ''\n", FBE.c_str());
Ethan Yonkere9afc3d2018-08-30 15:16:27 -0500849 break;
850 }
Ethan Yonker93382822018-11-01 15:25:31 -0500851 std::string FBE_contents, FBE_filenames;
Ethan Yonkere9afc3d2018-08-30 15:16:27 -0500852 FBE_contents = FBE.substr(0, colon_loc);
853 FBE_filenames = FBE.substr(colon_loc + 1);
854 property_set("fbe.contents", FBE_contents.c_str());
855 property_set("fbe.filenames", FBE_filenames.c_str());
856 LOGINFO("FBE contents '%s', filenames '%s'\n", FBE_contents.c_str(), FBE_filenames.c_str());
857 }
858 break;
Matt Mower2416a502016-04-12 19:54:46 -0500859 case TWFLAG_FLASHIMG:
860 Can_Flash_Img = val;
861 break;
862 case TWFLAG_FSFLAGS:
Matt Mower4ab42b12016-04-21 13:52:18 -0500863 Process_FS_Flags(str);
Matt Mower2416a502016-04-12 19:54:46 -0500864 break;
865 case TWFLAG_IGNOREBLKID:
866 Ignore_Blkid = val;
867 break;
868 case TWFLAG_LENGTH:
869 Length = atoi(str);
870 break;
871 case TWFLAG_MOUNTTODECRYPT:
872 Mount_To_Decrypt = val;
873 break;
874 case TWFLAG_REMOVABLE:
875 Removable = val;
876 break;
877 case TWFLAG_RETAINLAYOUTVERSION:
878 Retain_Layout_Version = val;
879 break;
880 case TWFLAG_SETTINGSSTORAGE:
881 Is_Settings_Storage = val;
882 if (Is_Settings_Storage)
883 Is_Storage = true;
884 break;
885 case TWFLAG_STORAGE:
886 Is_Storage = val;
887 break;
888 case TWFLAG_STORAGENAME:
889 Storage_Name = str;
890 break;
891 case TWFLAG_SUBPARTITIONOF:
892 Is_SubPartition = true;
893 SubPartition_Of = str;
894 break;
895 case TWFLAG_SYMLINK:
896 Symlink_Path = str;
897 break;
898 case TWFLAG_USERDATAENCRYPTBACKUP:
899 Use_Userdata_Encryption = val;
900 if (Use_Userdata_Encryption)
901 Can_Encrypt_Backup = true;
902 break;
903 case TWFLAG_USERMRF:
904 Use_Rm_Rf = val;
905 break;
906 case TWFLAG_WIPEDURINGFACTORYRESET:
907 Wipe_During_Factory_Reset = val;
908 if (Wipe_During_Factory_Reset) {
909 Can_Be_Wiped = true;
910 Wipe_Available_in_GUI = true;
911 }
912 break;
913 case TWFLAG_WIPEINGUI:
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600914 case TWFLAG_FORMATTABLE:
Matt Mower2416a502016-04-12 19:54:46 -0500915 Wipe_Available_in_GUI = val;
916 if (Wipe_Available_in_GUI)
917 Can_Be_Wiped = true;
918 break;
Ethan Yonker1b190162016-12-05 15:25:19 -0600919 case TWFLAG_SLOTSELECT:
920 SlotSelect = true;
921 break;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600922 case TWFLAG_ALTDEVICE:
923 Alternate_Block_Device = str;
924 break;
Ethan Yonker93382822018-11-01 15:25:31 -0500925 case TWFLAG_KEYDIRECTORY:
926 Key_Directory = str;
Matt Mower2416a502016-04-12 19:54:46 -0500927 default:
928 // Should not get here
929 LOGINFO("Flag identified for processing, but later unmatched: %i\n", flag);
930 break;
931 }
932}
Dees_Troy51127312012-09-08 13:08:49 -0400933
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600934void TWPartition::Process_TW_Flags(char *flags, bool Display_Error, int fstab_ver) {
Matt Mower2416a502016-04-12 19:54:46 -0500935 char separator[2] = {'\n', 0};
936 char *ptr, *savep;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600937 char source_separator = ';';
938
939 if (fstab_ver == 2)
940 source_separator = ',';
Matt Mower2416a502016-04-12 19:54:46 -0500941
942 // Semicolons within double-quotes are not forbidden, so replace
943 // only the semicolons intended as separators with '\n' for strtok
944 for (unsigned i = 0, skip = 0; i < strlen(flags); i++) {
945 if (flags[i] == '\"')
Dees_Troya13d74f2013-03-24 08:54:55 -0500946 skip = !skip;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600947 if (!skip && flags[i] == source_separator)
Matt Mower2416a502016-04-12 19:54:46 -0500948 flags[i] = separator[0];
Dees_Troy51127312012-09-08 13:08:49 -0400949 }
950
Matt Mower2416a502016-04-12 19:54:46 -0500951 // Avoid issues with potentially nested strtok by using strtok_r
952 ptr = strtok_r(flags, separator, &savep);
953 while (ptr) {
954 int ptr_len = strlen(ptr);
955 const struct flag_list* tw_flag = tw_flags;
Hashcode62bd9e02013-11-19 21:59:42 -0800956
Matt Mower2416a502016-04-12 19:54:46 -0500957 for (; tw_flag->name; tw_flag++) {
958 int flag_len = strlen(tw_flag->name);
959
960 if (strncmp(ptr, tw_flag->name, flag_len) == 0) {
961 bool flag_val = false;
962
963 if (ptr_len > flag_len && (tw_flag->name)[flag_len-1] != '='
964 && ptr[flag_len] != '=') {
965 // Handle flags with same starting string
966 // (e.g. backup and backupname)
967 continue;
968 } else if (ptr_len > flag_len && ptr[flag_len] == '=') {
969 // Handle flags with dual format: Part 1
970 // (e.g. backup and backup=y. backup=y handled here)
971 ptr += flag_len + 1;
972 TWFunc::Strip_Quotes(ptr);
973 // Skip flags with empty argument
974 // (e.g. backup=)
975 if (strlen(ptr) == 0) {
976 LOGINFO("Flag missing argument or should not include '=': %s=\n", tw_flag->name);
977 break;
978 }
979 flag_val = strchr("yY1", *ptr) != NULL;
980 } else if (ptr_len == flag_len
981 && (tw_flag->name)[flag_len-1] == '=') {
982 // Skip flags missing argument after =
983 // (e.g. backupname=)
984 LOGINFO("Flag missing argument: %s\n", tw_flag->name);
985 break;
986 } else if (ptr_len > flag_len
987 && (tw_flag->name)[flag_len-1] == '=') {
988 // Handle arguments to flags
989 // (e.g. backupname="My Stuff")
990 ptr += flag_len;
991 TWFunc::Strip_Quotes(ptr);
992 // Skip flags with empty argument
993 // (e.g. backupname="")
994 if (strlen(ptr) == 0) {
995 LOGINFO("Flag missing argument: %s\n", tw_flag->name);
996 break;
997 }
998 } else if (ptr_len == flag_len) {
999 // Handle flags with dual format: Part 2
1000 // (e.g. backup and backup=y. backup handled here)
1001 flag_val = true;
Ethan Yonker96af84a2015-01-05 14:58:36 -06001002 } else {
Matt Mower2416a502016-04-12 19:54:46 -05001003 LOGINFO("Flag matched, but could not be processed: %s\n", ptr);
1004 break;
Ethan Yonker96af84a2015-01-05 14:58:36 -06001005 }
Matt Mower2416a502016-04-12 19:54:46 -05001006
1007 Apply_TW_Flag(tw_flag->flag, ptr, flag_val);
1008 break;
Ethan Yonker96af84a2015-01-05 14:58:36 -06001009 }
Matt Mower2416a502016-04-12 19:54:46 -05001010 }
1011 if (tw_flag->name == 0) {
Dees_Troy51127312012-09-08 13:08:49 -04001012 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001013 LOGERR("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -04001014 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001015 LOGINFO("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -04001016 }
Matt Mower2416a502016-04-12 19:54:46 -05001017 ptr = strtok_r(NULL, separator, &savep);
Dees_Troy51127312012-09-08 13:08:49 -04001018 }
Dees_Troy51a0e822012-09-05 15:24:24 -04001019}
1020
Dees_Troy5bf43922012-09-07 16:07:55 -04001021bool TWPartition::Is_File_System(string File_System) {
1022 if (File_System == "ext2" ||
Dees_Troy63c8df72012-09-10 14:02:05 -04001023 File_System == "ext3" ||
Dees_Troy5bf43922012-09-07 16:07:55 -04001024 File_System == "ext4" ||
1025 File_System == "vfat" ||
1026 File_System == "ntfs" ||
1027 File_System == "yaffs2" ||
bigbiff bigbiff3e146522012-11-14 14:32:59 -05001028 File_System == "exfat" ||
Dees_Troye5017042013-08-29 16:38:55 +00001029 File_System == "f2fs" ||
Dees_Troy5bf43922012-09-07 16:07:55 -04001030 File_System == "auto")
1031 return true;
1032 else
1033 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001034}
1035
Dees_Troy5bf43922012-09-07 16:07:55 -04001036bool TWPartition::Is_Image(string File_System) {
Dees_Troy5fcd8f92012-10-16 12:22:05 -04001037 if (File_System == "emmc" || File_System == "mtd" || File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -04001038 return true;
1039 else
1040 return false;
1041}
1042
Dees_Troy51127312012-09-08 13:08:49 -04001043bool TWPartition::Make_Dir(string Path, bool Display_Error) {
Ethan Yonker93ac7a02016-11-07 22:05:58 -06001044 if (TWFunc::Get_D_Type_From_Stat(Path) != S_IFDIR)
1045 unlink(Path.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001046 if (!TWFunc::Path_Exists(Path)) {
Dees_Troy51127312012-09-08 13:08:49 -04001047 if (mkdir(Path.c_str(), 0777) == -1) {
1048 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001049 gui_msg(Msg(msg::kError, "create_folder_strerr=Can not create '{1}' folder ({2}).")(Path)(strerror(errno)));
Dees_Troy51127312012-09-08 13:08:49 -04001050 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001051 LOGINFO("Can not create '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -04001052 return false;
1053 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001054 LOGINFO("Created '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -04001055 return true;
1056 }
1057 }
1058 return true;
1059}
1060
Dees_Troy5bf43922012-09-07 16:07:55 -04001061void TWPartition::Setup_File_System(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001062 Can_Be_Mounted = true;
1063 Can_Be_Wiped = true;
1064
Dees_Troy5bf43922012-09-07 16:07:55 -04001065 // Make the mount point folder if it doesn't exist
Dees_Troy51127312012-09-08 13:08:49 -04001066 Make_Dir(Mount_Point, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -04001067 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
1068 Backup_Name = Display_Name;
bigbiffce8f83c2015-12-12 18:30:21 -05001069 Backup_Method = BM_FILES;
Dees_Troy5bf43922012-09-07 16:07:55 -04001070}
1071
Ethan Yonker1b190162016-12-05 15:25:19 -06001072void TWPartition::Setup_Image() {
Dees_Troy5bf43922012-09-07 16:07:55 -04001073 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
1074 Backup_Name = Display_Name;
Gary Peck82599a82012-11-21 16:23:12 -08001075 if (Current_File_System == "emmc")
bigbiffce8f83c2015-12-12 18:30:21 -05001076 Backup_Method = BM_DD;
Gary Peck82599a82012-11-21 16:23:12 -08001077 else if (Current_File_System == "mtd" || Current_File_System == "bml")
bigbiffce8f83c2015-12-12 18:30:21 -05001078 Backup_Method = BM_FLASH_UTILS;
Dees_Troy5bf43922012-09-07 16:07:55 -04001079 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001080 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 -04001081}
1082
Dees_Troye58d5262012-09-21 12:27:57 -04001083void TWPartition::Setup_AndSec(void) {
Dees_Troya13d74f2013-03-24 08:54:55 -05001084 Backup_Display_Name = "Android Secure";
Dees_Troye58d5262012-09-21 12:27:57 -04001085 Backup_Name = "and-sec";
Dees_Troya13d74f2013-03-24 08:54:55 -05001086 Can_Be_Backed_Up = true;
Dees_Troye58d5262012-09-21 12:27:57 -04001087 Has_Android_Secure = true;
1088 Symlink_Path = Mount_Point + "/.android_secure";
1089 Symlink_Mount_Point = "/and-sec";
1090 Backup_Path = Symlink_Mount_Point;
1091 Make_Dir("/and-sec", true);
1092 Recreate_AndSec_Folder();
Kjell Braden3126a112016-06-19 16:58:15 +00001093 Mount_Storage_Retry(true);
Dees_Troye58d5262012-09-21 12:27:57 -04001094}
1095
that9e0593e2014-10-08 00:01:24 +02001096void TWPartition::Setup_Data_Media() {
Ethan Yonker6277c792014-09-15 14:54:30 -05001097 LOGINFO("Setting up '%s' as data/media emulated storage.\n", Mount_Point.c_str());
Ethan Yonker66a19492015-12-10 10:19:45 -06001098 if (Storage_Name.empty() || Storage_Name == "Data")
1099 Storage_Name = "Internal Storage";
Ethan Yonker6277c792014-09-15 14:54:30 -05001100 Has_Data_Media = true;
1101 Is_Storage = true;
Ethan Yonker66a19492015-12-10 10:19:45 -06001102 Storage_Path = Mount_Point + "/media";
Ethan Yonker6277c792014-09-15 14:54:30 -05001103 Symlink_Path = Storage_Path;
Ethan Yonker66a19492015-12-10 10:19:45 -06001104 if (Mount_Point == "/data") {
1105 Is_Settings_Storage = true;
1106 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
1107 Make_Dir("/emmc", false);
1108 Symlink_Mount_Point = "/emmc";
1109 } else {
1110 Make_Dir("/sdcard", false);
1111 Symlink_Mount_Point = "/sdcard";
1112 }
1113 if (Mount(false) && TWFunc::Path_Exists(Mount_Point + "/media/0")) {
1114 Storage_Path = Mount_Point + "/media/0";
1115 Symlink_Path = Storage_Path;
1116 DataManager::SetValue(TW_INTERNAL_PATH, Mount_Point + "/media/0");
1117 UnMount(true);
1118 }
1119 DataManager::SetValue("tw_has_internal", 1);
1120 DataManager::SetValue("tw_has_data_media", 1);
Ethan Yonker3fdcda42016-11-30 12:29:37 -06001121 backup_exclusions.add_absolute_dir("/data/data/com.google.android.music/files");
Ethan Yonker6355b562017-05-01 09:42:17 -05001122 wipe_exclusions.add_absolute_dir(Mount_Point + "/misc/vold"); // adopted storage keys
Ethan Yonker79f88bd2016-12-09 14:52:12 -06001123 ExcludeAll(Mount_Point + "/.layout_version");
1124 ExcludeAll(Mount_Point + "/system/storage.xml");
Ethan Yonker6277c792014-09-15 14:54:30 -05001125 } else {
Ethan Yonker66a19492015-12-10 10:19:45 -06001126 if (Mount(true) && TWFunc::Path_Exists(Mount_Point + "/media/0")) {
1127 Storage_Path = Mount_Point + "/media/0";
1128 Symlink_Path = Storage_Path;
1129 UnMount(true);
1130 }
Ethan Yonker6277c792014-09-15 14:54:30 -05001131 }
Ethan Yonker79f88bd2016-12-09 14:52:12 -06001132 ExcludeAll(Mount_Point + "/media");
Ethan Yonker6277c792014-09-15 14:54:30 -05001133}
1134
Dees_Troy5bf43922012-09-07 16:07:55 -04001135void TWPartition::Find_Real_Block_Device(string& Block, bool Display_Error) {
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06001136 char device[PATH_MAX], realDevice[PATH_MAX];
Dees_Troy5bf43922012-09-07 16:07:55 -04001137
1138 strcpy(device, Block.c_str());
1139 memset(realDevice, 0, sizeof(realDevice));
1140 while (readlink(device, realDevice, sizeof(realDevice)) > 0)
1141 {
1142 strcpy(device, realDevice);
1143 memset(realDevice, 0, sizeof(realDevice));
1144 }
1145
1146 if (device[0] != '/') {
1147 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001148 LOGERR("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001149 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001150 LOGINFO("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001151 return;
Dees_Troy51a0e822012-09-05 15:24:24 -04001152 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -04001153 Block = device;
1154 return;
1155 }
1156}
1157
Kjell Braden3126a112016-06-19 16:58:15 +00001158bool TWPartition::Mount_Storage_Retry(bool Display_Error) {
Dees_Troy8e337f32012-10-13 22:07:49 -04001159 // On some devices, storage doesn't want to mount right away, retry and sleep
Kjell Braden3126a112016-06-19 16:58:15 +00001160 if (!Mount(Display_Error)) {
Dees_Troy8e337f32012-10-13 22:07:49 -04001161 int retry_count = 5;
1162 while (retry_count > 0 && !Mount(false)) {
1163 usleep(500000);
1164 retry_count--;
1165 }
Kjell Braden3126a112016-06-19 16:58:15 +00001166 return Mount(Display_Error);
Dees_Troy8e337f32012-10-13 22:07:49 -04001167 }
Kjell Braden3126a112016-06-19 16:58:15 +00001168 return true;
Dees_Troy8e337f32012-10-13 22:07:49 -04001169}
1170
Dees_Troy38bd7602012-09-14 13:33:53 -04001171bool TWPartition::Find_MTD_Block_Device(string MTD_Name) {
1172 FILE *fp = NULL;
1173 char line[255];
1174
1175 fp = fopen("/proc/mtd", "rt");
1176 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001177 LOGERR("Device does not support /proc/mtd\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001178 return false;
1179 }
1180
1181 while (fgets(line, sizeof(line), fp) != NULL)
1182 {
1183 char device[32], label[32];
1184 unsigned long size = 0;
Dees_Troy38bd7602012-09-14 13:33:53 -04001185 int deviceId;
1186
1187 sscanf(line, "%s %lx %*s %*c%s", device, &size, label);
1188
1189 // Skip header and blank lines
1190 if ((strcmp(device, "dev:") == 0) || (strlen(line) < 8))
1191 continue;
1192
1193 // Strip off the trailing " from the label
1194 label[strlen(label)-1] = '\0';
1195
1196 if (strcmp(label, MTD_Name.c_str()) == 0) {
1197 // We found our device
1198 // Strip off the trailing : from the device
1199 device[strlen(device)-1] = '\0';
1200 if (sscanf(device,"mtd%d", &deviceId) == 1) {
1201 sprintf(device, "/dev/block/mtdblock%d", deviceId);
1202 Primary_Block_Device = device;
Dees_Troy76543db2013-06-19 16:24:30 +00001203 fclose(fp);
1204 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -04001205 }
1206 }
1207 }
1208 fclose(fp);
1209
1210 return false;
1211}
1212
Dees_Troy51127312012-09-08 13:08:49 -04001213bool TWPartition::Get_Size_Via_statfs(bool Display_Error) {
1214 struct statfs st;
1215 string Local_Path = Mount_Point + "/.";
1216
1217 if (!Mount(Display_Error))
1218 return false;
1219
1220 if (statfs(Local_Path.c_str(), &st) != 0) {
1221 if (!Removable) {
1222 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001223 LOGERR("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -04001224 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001225 LOGINFO("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -04001226 }
1227 return false;
1228 }
1229 Size = (st.f_blocks * st.f_bsize);
1230 Used = ((st.f_blocks - st.f_bfree) * st.f_bsize);
1231 Free = (st.f_bfree * st.f_bsize);
1232 Backup_Size = Used;
1233 return true;
1234}
1235
1236bool TWPartition::Get_Size_Via_df(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001237 FILE* fp;
1238 char command[255], line[512];
1239 int include_block = 1;
1240 unsigned int min_len;
1241
1242 if (!Mount(Display_Error))
1243 return false;
1244
Dees_Troy38bd7602012-09-14 13:33:53 -04001245 min_len = Actual_Block_Device.size() + 2;
Dees_Troy51127312012-09-08 13:08:49 -04001246 sprintf(command, "df %s > /tmp/dfoutput.txt", Mount_Point.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001247 TWFunc::Exec_Cmd(command);
Dees_Troy51127312012-09-08 13:08:49 -04001248 fp = fopen("/tmp/dfoutput.txt", "rt");
1249 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001250 LOGINFO("Unable to open /tmp/dfoutput.txt.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001251 return false;
Dees_Troy51127312012-09-08 13:08:49 -04001252 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001253
1254 while (fgets(line, sizeof(line), fp) != NULL)
1255 {
1256 unsigned long blocks, used, available;
1257 char device[64];
1258 char tmpString[64];
1259
1260 if (strncmp(line, "Filesystem", 10) == 0)
1261 continue;
1262 if (strlen(line) < min_len) {
1263 include_block = 0;
1264 continue;
1265 }
1266 if (include_block) {
1267 sscanf(line, "%s %lu %lu %lu", device, &blocks, &used, &available);
1268 } else {
1269 // The device block string is so long that the df information is on the next line
1270 int space_count = 0;
Dees_Troye58d5262012-09-21 12:27:57 -04001271 sprintf(tmpString, "/dev/block/%s", Actual_Block_Device.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001272 while (tmpString[space_count] == 32)
1273 space_count++;
1274 sscanf(line + space_count, "%lu %lu %lu", &blocks, &used, &available);
1275 }
1276
1277 // Adjust block size to byte size
1278 Size = blocks * 1024ULL;
1279 Used = used * 1024ULL;
1280 Free = available * 1024ULL;
1281 Backup_Size = Used;
1282 }
1283 fclose(fp);
1284 return true;
1285}
1286
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001287unsigned long long TWPartition::IOCTL_Get_Block_Size() {
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001288 Find_Actual_Block_Device();
Ethan Yonker483e9f42016-01-11 22:21:18 -06001289
1290 return TWFunc::IOCTL_Get_Block_Size(Actual_Block_Device.c_str());
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001291}
1292
Dees_Troy5bf43922012-09-07 16:07:55 -04001293bool TWPartition::Find_Partition_Size(void) {
1294 FILE* fp;
1295 char line[512];
1296 string tmpdevice;
1297
igoriok87e3d932013-01-31 21:03:53 +02001298 fp = fopen("/proc/dumchar_info", "rt");
1299 if (fp != NULL) {
1300 while (fgets(line, sizeof(line), fp) != NULL)
1301 {
1302 char label[32], device[32];
1303 unsigned long size = 0;
1304
thatd43bf2d2014-09-21 23:13:02 +02001305 sscanf(line, "%s %lx %*x %*u %s", label, &size, device);
igoriok87e3d932013-01-31 21:03:53 +02001306
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05001307 // Skip header, annotation and blank lines
igoriok87e3d932013-01-31 21:03:53 +02001308 if ((strncmp(device, "/dev/", 5) != 0) || (strlen(line) < 8))
1309 continue;
1310
1311 tmpdevice = "/dev/";
1312 tmpdevice += label;
1313 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
1314 Size = size;
1315 fclose(fp);
1316 return true;
1317 }
1318 }
1319 }
1320
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001321 unsigned long long ioctl_size = IOCTL_Get_Block_Size();
1322 if (ioctl_size) {
1323 Size = ioctl_size;
1324 return true;
1325 }
1326
Dees_Troy5bf43922012-09-07 16:07:55 -04001327 // In this case, we'll first get the partitions we care about (with labels)
1328 fp = fopen("/proc/partitions", "rt");
1329 if (fp == NULL)
1330 return false;
1331
1332 while (fgets(line, sizeof(line), fp) != NULL)
1333 {
1334 unsigned long major, minor, blocks;
1335 char device[512];
Dees_Troy5bf43922012-09-07 16:07:55 -04001336
Dees_Troy63c8df72012-09-10 14:02:05 -04001337 if (strlen(line) < 7 || line[0] == 'm') continue;
Dees_Troy5bf43922012-09-07 16:07:55 -04001338 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
1339
1340 tmpdevice = "/dev/block/";
1341 tmpdevice += device;
Dees_Troy38bd7602012-09-14 13:33:53 -04001342 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001343 // Adjust block size to byte size
1344 Size = blocks * 1024ULL;
1345 fclose(fp);
1346 return true;
1347 }
1348 }
1349 fclose(fp);
1350 return false;
1351}
1352
Dees_Troy5bf43922012-09-07 16:07:55 -04001353bool TWPartition::Is_Mounted(void) {
1354 if (!Can_Be_Mounted)
1355 return false;
1356
1357 struct stat st1, st2;
1358 string test_path;
1359
1360 // Check to see if the mount point directory exists
1361 test_path = Mount_Point + "/.";
1362 if (stat(test_path.c_str(), &st1) != 0) return false;
1363
1364 // Check to see if the directory above the mount point exists
1365 test_path = Mount_Point + "/../.";
1366 if (stat(test_path.c_str(), &st2) != 0) return false;
1367
1368 // Compare the device IDs -- if they match then we're (probably) using tmpfs instead of an actual device
1369 int ret = (st1.st_dev != st2.st_dev) ? true : false;
1370
1371 return ret;
1372}
1373
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05001374bool TWPartition::Is_File_System_Writable(void) {
1375 if (!Is_File_System(Current_File_System) || !Is_Mounted())
1376 return false;
1377
1378 string test_path = Mount_Point + "/.";
1379 return (access(test_path.c_str(), W_OK) == 0);
1380}
1381
Dees_Troy5bf43922012-09-07 16:07:55 -04001382bool TWPartition::Mount(bool Display_Error) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001383 int exfat_mounted = 0;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001384 unsigned long flags = Mount_Flags;
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001385
Dees_Troy5bf43922012-09-07 16:07:55 -04001386 if (Is_Mounted()) {
1387 return true;
1388 } else if (!Can_Be_Mounted) {
1389 return false;
1390 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001391
1392 Find_Actual_Block_Device();
1393
1394 // Check the current file system before mounting
1395 Check_FS_Type();
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001396 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/exfat-fuse")) {
Dees_Troye34c1332013-02-06 19:13:00 +00001397 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 +00001398 LOGINFO("cmd: %s\n", cmd.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001399 string result;
1400 if (TWFunc::Exec_Cmd(cmd, result) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001401 LOGINFO("exfat-fuse failed to mount with result '%s', trying vfat\n", result.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001402 Current_File_System = "vfat";
1403 } else {
1404#ifdef TW_NO_EXFAT_FUSE
1405 UnMount(false);
1406 // We'll let the kernel handle it but using exfat-fuse to detect if the file system is actually exfat
1407 // Some kernels let us mount vfat as exfat which doesn't work out too well
1408#else
1409 exfat_mounted = 1;
1410#endif
1411 }
1412 }
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001413
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001414 if (Current_File_System == "ntfs" && (TWFunc::Path_Exists("/sbin/ntfs-3g") || TWFunc::Path_Exists("/sbin/mount.ntfs"))) {
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001415 string cmd;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001416 string Ntfsmount_Binary = "";
1417
1418 if (TWFunc::Path_Exists("/sbin/ntfs-3g"))
1419 Ntfsmount_Binary = "ntfs-3g";
1420 else if (TWFunc::Path_Exists("/sbin/mount.ntfs"))
1421 Ntfsmount_Binary = "mount.ntfs";
1422
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001423 if (Mount_Read_Only)
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001424 cmd = "/sbin/" + Ntfsmount_Binary + " -o ro " + Actual_Block_Device + " " + Mount_Point;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001425 else
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001426 cmd = "/sbin/" + Ntfsmount_Binary + " " + Actual_Block_Device + " " + Mount_Point;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001427 LOGINFO("cmd: '%s'\n", cmd.c_str());
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001428
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001429 if (TWFunc::Exec_Cmd(cmd) == 0) {
1430 return true;
1431 } else {
1432 LOGINFO("ntfs-3g failed to mount, trying regular mount method.\n");
1433 }
1434 }
1435
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001436 if (Mount_Read_Only)
1437 flags |= MS_RDONLY;
1438
Dees_Troy22042032012-12-18 21:23:08 +00001439 if (Fstab_File_System == "yaffs2") {
1440 // mount an MTD partition as a YAFFS2 filesystem.
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001441 flags = MS_NOATIME | MS_NODEV | MS_NODIRATIME;
1442 if (Mount_Read_Only)
1443 flags |= MS_RDONLY;
Dees_Troy76543db2013-06-19 16:24:30 +00001444 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags, NULL) < 0) {
1445 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags | MS_RDONLY, NULL) < 0) {
1446 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001447 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy76543db2013-06-19 16:24:30 +00001448 else
1449 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
1450 return false;
1451 } else {
1452 LOGINFO("Mounted '%s' (MTD) as RO\n", Mount_Point.c_str());
1453 return true;
1454 }
1455 } else {
1456 struct stat st;
1457 string test_path = Mount_Point;
1458 if (stat(test_path.c_str(), &st) < 0) {
1459 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001460 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy76543db2013-06-19 16:24:30 +00001461 else
1462 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
1463 return false;
1464 }
1465 mode_t new_mode = st.st_mode | S_IXUSR | S_IXGRP | S_IXOTH;
1466 if (new_mode != st.st_mode) {
1467 LOGINFO("Fixing execute permissions for %s\n", Mount_Point.c_str());
1468 if (chmod(Mount_Point.c_str(), new_mode) < 0) {
1469 if (Display_Error)
1470 LOGERR("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
1471 else
1472 LOGINFO("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
1473 return false;
1474 }
1475 }
Dees_Troy22042032012-12-18 21:23:08 +00001476 return true;
Dees_Troy76543db2013-06-19 16:24:30 +00001477 }
thatc7572eb2015-03-31 18:55:30 +02001478 }
1479
1480 string mount_fs = Current_File_System;
1481 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sys/module/texfat"))
1482 mount_fs = "texfat";
1483
1484 if (!exfat_mounted &&
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001485 mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), mount_fs.c_str(), flags, Mount_Options.c_str()) != 0 &&
1486 mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), mount_fs.c_str(), flags, NULL) != 0) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001487#ifdef TW_NO_EXFAT_FUSE
1488 if (Current_File_System == "exfat") {
Dees_Troy2673cec2013-04-02 20:22:16 +00001489 LOGINFO("Mounting exfat failed, trying vfat...\n");
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001490 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), "vfat", 0, NULL) != 0) {
Dees_Troy85f44ed2013-01-09 18:42:36 +00001491 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001492 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy85f44ed2013-01-09 18:42:36 +00001493 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001494 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001495 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 +00001496 return false;
Dees_Troy85f44ed2013-01-09 18:42:36 +00001497 }
Dees_Troyb05ddee2013-01-28 20:24:50 +00001498 } else {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001499#endif
bigbiff bigbiff26774a02014-03-29 18:22:00 -04001500 if (!Removable && Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001501 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001502 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001503 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
1504 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 +00001505 return false;
1506#ifdef TW_NO_EXFAT_FUSE
Dees_Troy85f44ed2013-01-09 18:42:36 +00001507 }
1508#endif
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001509 }
Ethan Yonker253368a2014-11-25 15:00:52 -06001510
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001511 if (Removable)
1512 Update_Size(Display_Error);
1513
xiaolu9416f4f2015-06-04 08:22:23 +08001514 if (!Symlink_Mount_Point.empty() && TWFunc::Path_Exists(Symlink_Path)) {
codelover2a3d4ce2015-03-14 20:26:49 +08001515 string Command = "mount -o bind '" + Symlink_Path + "' '" + Symlink_Mount_Point + "'";
Vojtech Bocek05534202013-09-11 08:11:56 +02001516 TWFunc::Exec_Cmd(Command);
Dees_Troy5bf43922012-09-07 16:07:55 -04001517 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001518 return true;
1519}
1520
1521bool TWPartition::UnMount(bool Display_Error) {
1522 if (Is_Mounted()) {
1523 int never_unmount_system;
1524
1525 DataManager::GetValue(TW_DONT_UNMOUNT_SYSTEM, never_unmount_system);
Captain Throwback9d6feb52018-07-27 10:05:24 -04001526 if (never_unmount_system == 1 && Mount_Point == PartitionManager.Get_Android_Root_Path())
Dees_Troy5bf43922012-09-07 16:07:55 -04001527 return true; // Never unmount system if you're not supposed to unmount it
1528
Matt Mowera8e6d832017-02-14 20:20:59 -06001529 if (Is_Storage && MTP_Storage_ID > 0)
Ethan Yonker726a0202014-12-16 20:01:38 -06001530 PartitionManager.Remove_MTP_Storage(MTP_Storage_ID);
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001531
Dees_Troy38bd7602012-09-14 13:33:53 -04001532 if (!Symlink_Mount_Point.empty())
1533 umount(Symlink_Mount_Point.c_str());
1534
Dees_Troyb05ddee2013-01-28 20:24:50 +00001535 umount(Mount_Point.c_str());
1536 if (Is_Mounted()) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001537 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001538 gui_msg(Msg(msg::kError, "fail_unmount=Failed to unmount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy5bf43922012-09-07 16:07:55 -04001539 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001540 LOGINFO("Unable to unmount '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001541 return false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001542 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -04001543 return true;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001544 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001545 } else {
1546 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001547 }
1548}
1549
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05001550bool TWPartition::ReMount(bool Display_Error) {
1551 if (UnMount(Display_Error))
1552 return Mount(Display_Error);
1553 return false;
1554}
1555
1556bool TWPartition::ReMount_RW(bool Display_Error) {
1557 // No need to remount if already mounted rw
1558 if (Is_File_System_Writable())
1559 return true;
1560
1561 bool ro = Mount_Read_Only;
1562 int flags = Mount_Flags;
1563
1564 Mount_Read_Only = false;
1565 Mount_Flags &= ~MS_RDONLY;
1566
1567 bool ret = ReMount(Display_Error);
1568
1569 Mount_Read_Only = ro;
1570 Mount_Flags = flags;
1571
1572 return ret;
1573}
1574
Gary Peck43acadf2012-11-21 21:19:01 -08001575bool TWPartition::Wipe(string New_File_System) {
Ethan Yonker726a0202014-12-16 20:01:38 -06001576 bool wiped = false, update_crypt = false, recreate_media = true;
Dees_Troy16c2b312013-01-15 16:51:18 +00001577 int check;
1578 string Layout_Filename = Mount_Point + "/.layout_version";
1579
Dees_Troy38bd7602012-09-14 13:33:53 -04001580 if (!Can_Be_Wiped) {
Matt Mower3c366972015-12-25 19:28:31 -06001581 gui_msg(Msg(msg::kError, "cannot_wipe=Partition {1} cannot be wiped.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04001582 return false;
1583 }
1584
Dees_Troyc51f1f92012-09-20 15:32:13 -04001585 if (Mount_Point == "/cache")
Dees_Troy2673cec2013-04-02 20:22:16 +00001586 Log_Offset = 0;
Dees_Troyc51f1f92012-09-20 15:32:13 -04001587
Dees_Troy16c2b312013-01-15 16:51:18 +00001588 if (Retain_Layout_Version && Mount(false) && TWFunc::Path_Exists(Layout_Filename))
1589 TWFunc::copy_file(Layout_Filename, "/.layout_version", 0600);
1590 else
1591 unlink("/.layout_version");
Dees_Troy38bd7602012-09-14 13:33:53 -04001592
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001593 if (Has_Data_Media && Current_File_System == New_File_System) {
Dees_Troy16c2b312013-01-15 16:51:18 +00001594 wiped = Wipe_Data_Without_Wiping_Media();
Ethan Yonker5eac2222014-06-11 12:22:55 -05001595 recreate_media = false;
Dees_Troy16c2b312013-01-15 16:51:18 +00001596 } else {
Dees_Troy16c2b312013-01-15 16:51:18 +00001597 DataManager::GetValue(TW_RM_RF_VAR, check);
1598
Hashcodedabfd492013-08-29 22:45:30 -07001599 if (check || Use_Rm_Rf)
Dees_Troy16c2b312013-01-15 16:51:18 +00001600 wiped = Wipe_RMRF();
1601 else if (New_File_System == "ext4")
1602 wiped = Wipe_EXT4();
1603 else if (New_File_System == "ext2" || New_File_System == "ext3")
1604 wiped = Wipe_EXT23(New_File_System);
1605 else if (New_File_System == "vfat")
1606 wiped = Wipe_FAT();
1607 else if (New_File_System == "exfat")
1608 wiped = Wipe_EXFAT();
1609 else if (New_File_System == "yaffs2")
1610 wiped = Wipe_MTD();
Dees_Troye5017042013-08-29 16:38:55 +00001611 else if (New_File_System == "f2fs")
1612 wiped = Wipe_F2FS();
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001613 else if (New_File_System == "ntfs")
1614 wiped = Wipe_NTFS();
Dees_Troy16c2b312013-01-15 16:51:18 +00001615 else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001616 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 +00001617 unlink("/.layout_version");
1618 return false;
1619 }
1620 update_crypt = wiped;
Gary Pecke8bc5d72012-12-21 06:45:25 -08001621 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001622
Gary Pecke8bc5d72012-12-21 06:45:25 -08001623 if (wiped) {
Dees_Troy1c1ac442013-01-17 21:42:14 +00001624 if (Mount_Point == "/cache")
1625 DataManager::Output_Version();
1626
Dees_Troy16c2b312013-01-15 16:51:18 +00001627 if (TWFunc::Path_Exists("/.layout_version") && Mount(false))
1628 TWFunc::copy_file("/.layout_version", Layout_Filename, 0600);
1629
1630 if (update_crypt) {
1631 Setup_File_System(false);
1632 if (Is_Encrypted && !Is_Decrypted) {
1633 // just wiped an encrypted partition back to its unencrypted state
1634 Is_Encrypted = false;
1635 Is_Decrypted = false;
1636 Decrypted_Block_Device = "";
1637 if (Mount_Point == "/data") {
1638 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1639 DataManager::SetValue(TW_IS_DECRYPTED, 0);
1640 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001641 }
1642 }
Ethan Yonker5eac2222014-06-11 12:22:55 -05001643
Ethan Yonker66a19492015-12-10 10:19:45 -06001644 if (Has_Data_Media && recreate_media) {
Ethan Yonker5eac2222014-06-11 12:22:55 -05001645 Recreate_Media_Folder();
1646 }
Matt Mower209c9632016-01-20 12:08:35 -06001647 if (Is_Storage && Mount(false))
1648 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Gary Pecke8bc5d72012-12-21 06:45:25 -08001649 }
Matt Mower209c9632016-01-20 12:08:35 -06001650
Gary Pecke8bc5d72012-12-21 06:45:25 -08001651 return wiped;
Dees_Troy51a0e822012-09-05 15:24:24 -04001652}
1653
Gary Peck43acadf2012-11-21 21:19:01 -08001654bool TWPartition::Wipe() {
Gary Peck82599a82012-11-21 16:23:12 -08001655 if (Is_File_System(Current_File_System))
1656 return Wipe(Current_File_System);
1657 else
1658 return Wipe(Fstab_File_System);
Gary Peck43acadf2012-11-21 21:19:01 -08001659}
1660
Dees_Troye58d5262012-09-21 12:27:57 -04001661bool TWPartition::Wipe_AndSec(void) {
1662 if (!Has_Android_Secure)
1663 return false;
1664
Dees_Troye58d5262012-09-21 12:27:57 -04001665 if (!Mount(true))
1666 return false;
1667
Ethan Yonker74db1572015-10-28 12:44:49 -05001668 gui_msg(Msg("wiping=Wiping {1}")(Backup_Display_Name));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001669 TWFunc::removeDir(Mount_Point + "/.android_secure/", true);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001670 return true;
Dees_Troye58d5262012-09-21 12:27:57 -04001671}
1672
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001673bool TWPartition::Can_Repair() {
Ethan Yonkera2719152015-05-28 09:44:41 -05001674 if (Mount_Read_Only)
1675 return false;
Matt Mower18794c82015-11-11 16:22:45 -06001676 if (Current_File_System == "vfat" && TWFunc::Path_Exists("/sbin/fsck.fat"))
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001677 return true;
1678 else if ((Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") && TWFunc::Path_Exists("/sbin/e2fsck"))
1679 return true;
1680 else if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/fsck.exfat"))
1681 return true;
1682 else if (Current_File_System == "f2fs" && TWFunc::Path_Exists("/sbin/fsck.f2fs"))
1683 return true;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001684 else if (Current_File_System == "ntfs" && (TWFunc::Path_Exists("/sbin/ntfsfix") || TWFunc::Path_Exists("/sbin/fsck.ntfs")))
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001685 return true;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001686 return false;
1687}
1688
1689bool TWPartition::Repair() {
1690 string command;
1691
1692 if (Current_File_System == "vfat") {
Matt Mower18794c82015-11-11 16:22:45 -06001693 if (!TWFunc::Path_Exists("/sbin/fsck.fat")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001694 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.fat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001695 return false;
1696 }
1697 if (!UnMount(true))
1698 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001699 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.fat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001700 Find_Actual_Block_Device();
Matt Mower18794c82015-11-11 16:22:45 -06001701 command = "/sbin/fsck.fat -y " + Actual_Block_Device;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001702 LOGINFO("Repair command: %s\n", command.c_str());
1703 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001704 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001705 return true;
1706 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001707 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001708 return false;
1709 }
1710 }
1711 if (Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") {
1712 if (!TWFunc::Path_Exists("/sbin/e2fsck")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001713 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("e2fsck"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001714 return false;
1715 }
1716 if (!UnMount(true))
1717 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001718 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("e2fsck"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001719 Find_Actual_Block_Device();
Ethan Yonkera2719152015-05-28 09:44:41 -05001720 command = "/sbin/e2fsck -fp " + Actual_Block_Device;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001721 LOGINFO("Repair command: %s\n", command.c_str());
1722 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001723 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001724 return true;
1725 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001726 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001727 return false;
1728 }
1729 }
1730 if (Current_File_System == "exfat") {
1731 if (!TWFunc::Path_Exists("/sbin/fsck.exfat")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001732 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.exfat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001733 return false;
1734 }
1735 if (!UnMount(true))
1736 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001737 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.exfat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001738 Find_Actual_Block_Device();
1739 command = "/sbin/fsck.exfat " + Actual_Block_Device;
1740 LOGINFO("Repair command: %s\n", command.c_str());
1741 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001742 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001743 return true;
1744 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001745 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001746 return false;
1747 }
1748 }
1749 if (Current_File_System == "f2fs") {
1750 if (!TWFunc::Path_Exists("/sbin/fsck.f2fs")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001751 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.f2fs"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001752 return false;
1753 }
1754 if (!UnMount(true))
1755 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001756 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.f2fs"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001757 Find_Actual_Block_Device();
1758 command = "/sbin/fsck.f2fs " + Actual_Block_Device;
1759 LOGINFO("Repair command: %s\n", command.c_str());
1760 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001761 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001762 return true;
1763 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001764 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001765 return false;
1766 }
1767 }
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001768 if (Current_File_System == "ntfs") {
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001769 string Ntfsfix_Binary;
1770 if (TWFunc::Path_Exists("/sbin/ntfsfix"))
1771 Ntfsfix_Binary = "ntfsfix";
1772 else if (TWFunc::Path_Exists("/sbin/fsck.ntfs"))
1773 Ntfsfix_Binary = "fsck.ntfs";
1774 else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001775 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("ntfsfix"));
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001776 return false;
1777 }
1778 if (!UnMount(true))
1779 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001780 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)(Ntfsfix_Binary));
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001781 Find_Actual_Block_Device();
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001782 command = "/sbin/" + Ntfsfix_Binary + " " + Actual_Block_Device;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001783 LOGINFO("Repair command: %s\n", command.c_str());
1784 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001785 gui_msg("done=Done.");
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001786 return true;
1787 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001788 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001789 return false;
1790 }
1791 }
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001792 return false;
1793}
1794
Ethan Yonkera2719152015-05-28 09:44:41 -05001795bool TWPartition::Can_Resize() {
1796 if (Mount_Read_Only)
1797 return false;
1798 if ((Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") && TWFunc::Path_Exists("/sbin/resize2fs"))
1799 return true;
1800 return false;
1801}
1802
1803bool TWPartition::Resize() {
1804 string command;
1805
1806 if (Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") {
1807 if (!Can_Repair()) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001808 LOGINFO("Cannot resize %s because %s cannot be repaired before resizing.\n", Display_Name.c_str(), Display_Name.c_str());
1809 gui_msg(Msg(msg::kError, "cannot_resize=Cannot resize {1}.")(Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001810 return false;
1811 }
1812 if (!TWFunc::Path_Exists("/sbin/resize2fs")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001813 LOGINFO("resize2fs does not exist! Cannot resize!\n");
1814 gui_msg(Msg(msg::kError, "cannot_resize=Cannot resize {1}.")(Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001815 return false;
1816 }
1817 // Repair will unmount so no need to do it twice
Ethan Yonker74db1572015-10-28 12:44:49 -05001818 gui_msg(Msg("repair_resize=Repairing {1} before resizing.")( Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001819 if (!Repair())
1820 return false;
Ethan Yonker74db1572015-10-28 12:44:49 -05001821 gui_msg(Msg("resizing=Resizing {1} using {2}...")(Display_Name)("resize2fs"));
Ethan Yonkera2719152015-05-28 09:44:41 -05001822 Find_Actual_Block_Device();
1823 command = "/sbin/resize2fs " + Actual_Block_Device;
1824 if (Length != 0) {
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001825 unsigned long long Actual_Size = IOCTL_Get_Block_Size();
1826 if (Actual_Size == 0)
1827 return false;
Ethan Yonkera2719152015-05-28 09:44:41 -05001828
Ethan Yonkera2719152015-05-28 09:44:41 -05001829 unsigned long long Block_Count;
1830 if (Length < 0) {
1831 // Reduce overall size by this length
1832 Block_Count = (Actual_Size / 1024LLU) - ((unsigned long long)(Length * -1) / 1024LLU);
1833 } else {
1834 // This is the size, not a size reduction
1835 Block_Count = ((unsigned long long)(Length) / 1024LLU);
1836 }
1837 char temp[256];
1838 sprintf(temp, "%llu", Block_Count);
1839 command += " ";
1840 command += temp;
1841 command += "K";
1842 }
1843 LOGINFO("Resize command: %s\n", command.c_str());
1844 if (TWFunc::Exec_Cmd(command) == 0) {
1845 Update_Size(true);
Ethan Yonker74db1572015-10-28 12:44:49 -05001846 gui_msg("done=Done.");
Ethan Yonkera2719152015-05-28 09:44:41 -05001847 return true;
1848 } else {
1849 Update_Size(true);
Ethan Yonker74db1572015-10-28 12:44:49 -05001850 gui_msg(Msg(msg::kError, "unable_resize=Unable to resize {1}.")(Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001851 return false;
1852 }
1853 }
1854 return false;
1855}
1856
bigbiffce8f83c2015-12-12 18:30:21 -05001857bool TWPartition::Backup(PartitionSettings *part_settings, pid_t *tar_fork_pid) {
1858 if (Backup_Method == BM_FILES)
1859 return Backup_Tar(part_settings, tar_fork_pid);
1860 else if (Backup_Method == BM_DD)
1861 return Backup_Image(part_settings);
1862 else if (Backup_Method == BM_FLASH_UTILS)
1863 return Backup_Dump_Image(part_settings);
Dees_Troy2673cec2013-04-02 20:22:16 +00001864 LOGERR("Unknown backup method for '%s'\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001865 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001866}
1867
bigbiffce8f83c2015-12-12 18:30:21 -05001868bool TWPartition::Restore(PartitionSettings *part_settings) {
Matt Mower3c366972015-12-25 19:28:31 -06001869 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, gui_parse_text("{@restoring_hdr}"));
Ethan Yonkere080c1f2016-09-19 13:50:25 -05001870 LOGINFO("Restore filename is: %s/%s\n", part_settings->Backup_Folder.c_str(), Backup_FileName.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001871
bigbiffce8f83c2015-12-12 18:30:21 -05001872 string Restore_File_System = Get_Restore_File_System(part_settings);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001873
1874 if (Is_File_System(Restore_File_System))
bigbiffce8f83c2015-12-12 18:30:21 -05001875 return Restore_Tar(part_settings);
1876 else if (Is_Image(Restore_File_System))
1877 return Restore_Image(part_settings);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001878
1879 LOGERR("Unknown restore method for '%s'\n", Mount_Point.c_str());
1880 return false;
1881}
1882
bigbiffce8f83c2015-12-12 18:30:21 -05001883string TWPartition::Get_Restore_File_System(PartitionSettings *part_settings) {
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001884 size_t first_period, second_period;
1885 string Restore_File_System;
1886
Gary Peck43acadf2012-11-21 21:19:01 -08001887 // Parse backup filename to extract the file system before wiping
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05001888 first_period = Backup_FileName.find(".");
Gary Peck43acadf2012-11-21 21:19:01 -08001889 if (first_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001890 LOGERR("Unable to find file system (first period).\n");
thatd43bf2d2014-09-21 23:13:02 +02001891 return string();
Gary Peck43acadf2012-11-21 21:19:01 -08001892 }
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05001893 Restore_File_System = Backup_FileName.substr(first_period + 1, Backup_FileName.size() - first_period - 1);
Gary Peck43acadf2012-11-21 21:19:01 -08001894 second_period = Restore_File_System.find(".");
1895 if (second_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001896 LOGERR("Unable to find file system (second period).\n");
thatd43bf2d2014-09-21 23:13:02 +02001897 return string();
Gary Peck43acadf2012-11-21 21:19:01 -08001898 }
1899 Restore_File_System.resize(second_period);
Matt Mowera0cd91d2016-12-30 18:21:42 -06001900 LOGINFO("Restore file system is: '%s'.\n", Restore_File_System.c_str());
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001901 return Restore_File_System;
Dees_Troy51a0e822012-09-05 15:24:24 -04001902}
1903
1904string TWPartition::Backup_Method_By_Name() {
bigbiffce8f83c2015-12-12 18:30:21 -05001905 if (Backup_Method == BM_NONE)
Dees_Troy38bd7602012-09-14 13:33:53 -04001906 return "none";
bigbiffce8f83c2015-12-12 18:30:21 -05001907 else if (Backup_Method == BM_FILES)
Dees_Troy38bd7602012-09-14 13:33:53 -04001908 return "files";
bigbiffce8f83c2015-12-12 18:30:21 -05001909 else if (Backup_Method == BM_DD)
Dees_Troy38bd7602012-09-14 13:33:53 -04001910 return "dd";
bigbiffce8f83c2015-12-12 18:30:21 -05001911 else if (Backup_Method == BM_FLASH_UTILS)
Dees_Troy38bd7602012-09-14 13:33:53 -04001912 return "flash_utils";
1913 else
1914 return "undefined";
1915 return "ERROR!";
Dees_Troy51a0e822012-09-05 15:24:24 -04001916}
1917
1918bool TWPartition::Decrypt(string Password) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001919 LOGINFO("STUB TWPartition::Decrypt, password: '%s'\n", Password.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001920 // Is this needed?
Dees_Troy51a0e822012-09-05 15:24:24 -04001921 return 1;
1922}
1923
1924bool TWPartition::Wipe_Encryption() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001925 bool Save_Data_Media = Has_Data_Media;
Ethan Yonker93382822018-11-01 15:25:31 -05001926 bool ret = false;
1927 BasePartition* base_partition = make_partition();
1928
1929 if (!base_partition->PreWipeEncryption())
1930 goto exit;
Dees_Troy38bd7602012-09-14 13:33:53 -04001931
1932 if (!UnMount(true))
Ethan Yonker93382822018-11-01 15:25:31 -05001933 goto exit;
Dees_Troy38bd7602012-09-14 13:33:53 -04001934
Dees_Troy38bd7602012-09-14 13:33:53 -04001935 Has_Data_Media = false;
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001936#ifdef TW_INCLUDE_CRYPTO
Ethan Yonker1b190162016-12-05 15:25:19 -06001937 if (Is_Decrypted && !Decrypted_Block_Device.empty()) {
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001938 if (!UnMount(true))
Ethan Yonker93382822018-11-01 15:25:31 -05001939 goto exit;
Matt Mower2b18a532015-02-20 16:58:05 -06001940 if (delete_crypto_blk_dev((char*)("userdata")) != 0) {
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001941 LOGERR("Error deleting crypto block device, continuing anyway.\n");
1942 }
1943 }
1944#endif
Ethan Yonker93382822018-11-01 15:25:31 -05001945 Decrypted_Block_Device = "";
Dees_Troy74fb2e92013-04-15 14:35:47 +00001946 Is_Decrypted = false;
1947 Is_Encrypted = false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001948 Find_Actual_Block_Device();
Ethan Yonkerbc85b632015-08-09 12:48:14 -05001949 if (Crypto_Key_Location == "footer") {
1950 int newlen, fd;
1951 if (Length != 0) {
1952 newlen = Length;
1953 if (newlen < 0)
1954 newlen = newlen * -1;
1955 } else {
1956 newlen = CRYPT_FOOTER_OFFSET;
1957 }
1958 if ((fd = open(Actual_Block_Device.c_str(), O_RDWR)) < 0) {
1959 gui_print_color("warning", "Unable to open '%s' to wipe crypto key\n", Actual_Block_Device.c_str());
1960 } else {
1961 unsigned int block_count;
1962 if ((ioctl(fd, BLKGETSIZE, &block_count)) == -1) {
1963 gui_print_color("warning", "Unable to get block size for wiping crypto footer.\n");
1964 } else {
1965 off64_t offset = ((off64_t)block_count * 512) - newlen;
1966 if (lseek64(fd, offset, SEEK_SET) == -1) {
1967 gui_print_color("warning", "Unable to lseek64 for wiping crypto footer.\n");
1968 } else {
1969 void* buffer = malloc(newlen);
1970 if (!buffer) {
1971 gui_print_color("warning", "Failed to malloc for wiping crypto footer.\n");
1972 } else {
1973 memset(buffer, 0, newlen);
1974 int ret = write(fd, buffer, newlen);
1975 if (ret != newlen) {
1976 gui_print_color("warning", "Failed to wipe crypto footer.\n");
1977 } else {
1978 LOGINFO("Successfully wiped crypto footer.\n");
1979 }
Matt Mower13a8f0b2015-09-26 15:40:03 -05001980 free(buffer);
Ethan Yonkerbc85b632015-08-09 12:48:14 -05001981 }
1982 }
1983 }
1984 close(fd);
1985 }
1986 } else {
Ethan Yonkerc2dafbb2016-03-15 22:20:59 -05001987 if (TWFunc::IOCTL_Get_Block_Size(Crypto_Key_Location.c_str()) >= 16384LLU) {
1988 string Command = "dd of='" + Crypto_Key_Location + "' if=/dev/zero bs=16384 count=1";
1989 TWFunc::Exec_Cmd(Command);
1990 } else {
1991 LOGINFO("Crypto key location reports size < 16K so not wiping crypto footer.\n");
1992 }
Ethan Yonkerbc85b632015-08-09 12:48:14 -05001993 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001994 if (Wipe(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001995 Has_Data_Media = Save_Data_Media;
1996 if (Has_Data_Media && !Symlink_Mount_Point.empty()) {
1997 Recreate_Media_Folder();
Ethan Yonker726a0202014-12-16 20:01:38 -06001998 if (Mount(false))
1999 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04002000 }
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06002001 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
Ethan Yonker83e82572014-04-04 10:59:28 -05002002#ifndef TW_OEM_BUILD
Ethan Yonker74db1572015-10-28 12:44:49 -05002003 gui_msg("format_data_msg=You may need to reboot recovery to be able to use /data again.");
Ethan Yonker83e82572014-04-04 10:59:28 -05002004#endif
Ethan Yonker93382822018-11-01 15:25:31 -05002005 ret = true;
2006 if (!Key_Directory.empty())
2007 ret = PartitionManager.Wipe_By_Path(Key_Directory);
2008 if (ret)
2009 ret = base_partition->PostWipeEncryption();
2010 goto exit;
Dees_Troy38bd7602012-09-14 13:33:53 -04002011 } else {
2012 Has_Data_Media = Save_Data_Media;
Ethan Yonker74db1572015-10-28 12:44:49 -05002013 gui_err("format_data_err=Unable to format to remove encryption.");
Ethan Yonker726a0202014-12-16 20:01:38 -06002014 if (Has_Data_Media && Mount(false))
2015 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Ethan Yonker93382822018-11-01 15:25:31 -05002016 goto exit;
Dees_Troy38bd7602012-09-14 13:33:53 -04002017 }
Ethan Yonker93382822018-11-01 15:25:31 -05002018exit:
2019 delete base_partition;
2020 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04002021}
2022
2023void TWPartition::Check_FS_Type() {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05002024 const char* type;
2025 blkid_probe pr;
Dees_Troy5bf43922012-09-07 16:07:55 -04002026
Dees_Troy68cab492012-12-12 19:29:35 +00002027 if (Fstab_File_System == "yaffs2" || Fstab_File_System == "mtd" || Fstab_File_System == "bml" || Ignore_Blkid)
2028 return; // Running blkid on some mtd devices causes a massive crash or needs to be skipped
Dees_Troy5bf43922012-09-07 16:07:55 -04002029
Dees_Troy38bd7602012-09-14 13:33:53 -04002030 Find_Actual_Block_Device();
Dees_Troy8170a922012-09-18 15:40:25 -04002031 if (!Is_Present)
2032 return;
Dees_Troy51127312012-09-08 13:08:49 -04002033
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05002034 pr = blkid_new_probe_from_filename(Actual_Block_Device.c_str());
2035 if (blkid_do_fullprobe(pr)) {
2036 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00002037 LOGINFO("Can't probe device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05002038 return;
Dees_Troy5bf43922012-09-07 16:07:55 -04002039 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02002040
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05002041 if (blkid_probe_lookup_value(pr, "TYPE", &type, NULL) < 0) {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05002042 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00002043 LOGINFO("can't find filesystem on device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05002044 return;
2045 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02002046
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05002047 Current_File_System = type;
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02002048 blkid_free_probe(pr);
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06002049 if (fs_flags.size() > 1) {
2050 std::vector<partition_fs_flags_struct>::iterator iter;
2051 std::vector<partition_fs_flags_struct>::iterator found = fs_flags.begin();
2052
2053 for (iter = fs_flags.begin(); iter != fs_flags.end(); iter++) {
2054 if (iter->File_System == Current_File_System) {
2055 found = iter;
2056 break;
2057 }
2058 }
2059 // If we don't find a match, we default the flags to the first set of flags that we received from the fstab
2060 if (Mount_Flags != found->Mount_Flags || Mount_Options != found->Mount_Options) {
2061 Mount_Flags = found->Mount_Flags;
2062 Mount_Options = found->Mount_Options;
2063 LOGINFO("Mount_Flags: %i, Mount_Options: %s\n", Mount_Flags, Mount_Options.c_str());
2064 }
2065 }
Dees_Troy51a0e822012-09-05 15:24:24 -04002066}
2067
Gary Peck43acadf2012-11-21 21:19:01 -08002068bool TWPartition::Wipe_EXT23(string File_System) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002069 if (!UnMount(true))
2070 return false;
2071
Dees_Troy43d8b002012-09-17 16:00:01 -04002072 if (TWFunc::Path_Exists("/sbin/mke2fs")) {
Vojtech Bocek05534202013-09-11 08:11:56 +02002073 string command;
Dees_Troy38bd7602012-09-14 13:33:53 -04002074
Greg Wallace4b44fef2015-12-29 15:33:24 -05002075 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mke2fs"));
Dees_Troy38bd7602012-09-14 13:33:53 -04002076 Find_Actual_Block_Device();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002077 command = "mke2fs -t " + File_System + " -m 0 " + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00002078 LOGINFO("mke2fs command: %s\n", command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02002079 if (TWFunc::Exec_Cmd(command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08002080 Current_File_System = File_System;
Dees_Troye58d5262012-09-21 12:27:57 -04002081 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05002082 gui_msg("done=Done.");
Dees_Troy38bd7602012-09-14 13:33:53 -04002083 return true;
2084 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002085 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04002086 return false;
2087 }
2088 } else
2089 return Wipe_RMRF();
2090
2091 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04002092}
2093
2094bool TWPartition::Wipe_EXT4() {
Ethan Yonker25f20c12014-10-14 09:04:43 -05002095 Find_Actual_Block_Device();
2096 if (!Is_Present) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002097 LOGINFO("Block device not present, cannot wipe %s.\n", Display_Name.c_str());
2098 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Ethan Yonker25f20c12014-10-14 09:04:43 -05002099 return false;
2100 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002101 if (!UnMount(true))
2102 return false;
2103
Matt Mower87413642017-01-17 21:14:46 -06002104#if defined(USE_EXT4)
Ethan Yonkerf27497f2014-02-09 11:48:33 -06002105 int ret;
2106 char *secontext = NULL;
2107
Greg Wallace4b44fef2015-12-29 15:33:24 -05002108 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("make_ext4fs"));
Ethan Yonkerf27497f2014-02-09 11:48:33 -06002109
Dees Troy99c8dbf2014-03-10 16:53:58 +00002110 if (!selinux_handle || selabel_lookup(selinux_handle, &secontext, Mount_Point.c_str(), S_IFDIR) < 0) {
Ethan Yonkerf27497f2014-02-09 11:48:33 -06002111 LOGINFO("Cannot lookup security context for '%s'\n", Mount_Point.c_str());
2112 ret = make_ext4fs(Actual_Block_Device.c_str(), Length, Mount_Point.c_str(), NULL);
2113 } else {
2114 ret = make_ext4fs(Actual_Block_Device.c_str(), Length, Mount_Point.c_str(), selinux_handle);
2115 }
2116 if (ret != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002117 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troya95f55c2013-08-17 13:14:43 +00002118 return false;
2119 } else {
bigbiff bigbiffc49d7062013-10-11 20:28:00 -04002120 string sedir = Mount_Point + "/lost+found";
2121 PartitionManager.Mount_By_Path(sedir.c_str(), true);
2122 rmdir(sedir.c_str());
2123 mkdir(sedir.c_str(), S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP);
Dees_Troya95f55c2013-08-17 13:14:43 +00002124 return true;
2125 }
2126#else
Dees_Troy43d8b002012-09-17 16:00:01 -04002127 if (TWFunc::Path_Exists("/sbin/make_ext4fs")) {
Vojtech Bocek05534202013-09-11 08:11:56 +02002128 string Command;
Dees_Troy38bd7602012-09-14 13:33:53 -04002129
Greg Wallace4b44fef2015-12-29 15:33:24 -05002130 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("make_ext4fs"));
Dees_Troy38bd7602012-09-14 13:33:53 -04002131 Find_Actual_Block_Device();
2132 Command = "make_ext4fs";
2133 if (!Is_Decrypted && Length != 0) {
2134 // Only use length if we're not decrypted
2135 char len[32];
2136 sprintf(len, "%i", Length);
2137 Command += " -l ";
2138 Command += len;
2139 }
Dees_Troy5295d582013-09-06 15:51:08 +00002140 if (TWFunc::Path_Exists("/file_contexts")) {
2141 Command += " -S /file_contexts";
2142 }
2143 Command += " -a " + Mount_Point + " " + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00002144 LOGINFO("make_ext4fs command: %s\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02002145 if (TWFunc::Exec_Cmd(Command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08002146 Current_File_System = "ext4";
Dees_Troye58d5262012-09-21 12:27:57 -04002147 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05002148 gui_msg("done=Done.");
Dees_Troy38bd7602012-09-14 13:33:53 -04002149 return true;
2150 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002151 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04002152 return false;
2153 }
2154 } else
Gary Peck43acadf2012-11-21 21:19:01 -08002155 return Wipe_EXT23("ext4");
Dees_Troya95f55c2013-08-17 13:14:43 +00002156#endif
Dees_Troy38bd7602012-09-14 13:33:53 -04002157 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04002158}
2159
2160bool TWPartition::Wipe_FAT() {
Vojtech Bocek05534202013-09-11 08:11:56 +02002161 string command;
Dees_Troy38bd7602012-09-14 13:33:53 -04002162
Matt Mower18794c82015-11-11 16:22:45 -06002163 if (TWFunc::Path_Exists("/sbin/mkfs.fat")) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002164 if (!UnMount(true))
2165 return false;
2166
Greg Wallace4b44fef2015-12-29 15:33:24 -05002167 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mkfs.fat"));
Dees_Troy38bd7602012-09-14 13:33:53 -04002168 Find_Actual_Block_Device();
Matt Mower18794c82015-11-11 16:22:45 -06002169 command = "mkfs.fat " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02002170 if (TWFunc::Exec_Cmd(command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08002171 Current_File_System = "vfat";
Dees_Troye58d5262012-09-21 12:27:57 -04002172 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05002173 gui_msg("done=Done.");
Dees_Troy38bd7602012-09-14 13:33:53 -04002174 return true;
2175 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002176 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04002177 return false;
2178 }
2179 return true;
2180 }
2181 else
2182 return Wipe_RMRF();
2183
2184 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04002185}
2186
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002187bool TWPartition::Wipe_EXFAT() {
Vojtech Bocek05534202013-09-11 08:11:56 +02002188 string command;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002189
2190 if (TWFunc::Path_Exists("/sbin/mkexfatfs")) {
2191 if (!UnMount(true))
2192 return false;
2193
Greg Wallace4b44fef2015-12-29 15:33:24 -05002194 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mkexfatfs"));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002195 Find_Actual_Block_Device();
2196 command = "mkexfatfs " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02002197 if (TWFunc::Exec_Cmd(command) == 0) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002198 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05002199 gui_msg("done=Done.");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002200 return true;
2201 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002202 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002203 return false;
2204 }
2205 return true;
2206 }
2207 return false;
2208}
2209
Dees_Troy38bd7602012-09-14 13:33:53 -04002210bool TWPartition::Wipe_MTD() {
2211 if (!UnMount(true))
2212 return false;
2213
Greg Wallace4b44fef2015-12-29 15:33:24 -05002214 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("MTD"));
Dees_Troy38bd7602012-09-14 13:33:53 -04002215
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02002216 mtd_scan_partitions();
2217 const MtdPartition* mtd = mtd_find_partition_by_name(MTD_Name.c_str());
2218 if (mtd == NULL) {
2219 LOGERR("No mtd partition named '%s'", MTD_Name.c_str());
2220 return false;
2221 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002222
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02002223 MtdWriteContext* ctx = mtd_write_partition(mtd);
2224 if (ctx == NULL) {
2225 LOGERR("Can't write '%s', failed to format.", MTD_Name.c_str());
2226 return false;
2227 }
2228 if (mtd_erase_blocks(ctx, -1) == -1) {
2229 mtd_write_close(ctx);
2230 LOGERR("Failed to format '%s'", MTD_Name.c_str());
2231 return false;
2232 }
2233 if (mtd_write_close(ctx) != 0) {
2234 LOGERR("Failed to close '%s'", MTD_Name.c_str());
2235 return false;
2236 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08002237 Current_File_System = "yaffs2";
Dees_Troye58d5262012-09-21 12:27:57 -04002238 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05002239 gui_msg("done=Done.");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02002240 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002241}
2242
2243bool TWPartition::Wipe_RMRF() {
Dees_Troy38bd7602012-09-14 13:33:53 -04002244 if (!Mount(true))
2245 return false;
Ethan Yonker726a0202014-12-16 20:01:38 -06002246 // This is the only wipe that leaves the partition mounted, so we
2247 // must manually remove the partition from MTP if it is a storage
2248 // partition.
2249 if (Is_Storage)
2250 PartitionManager.Remove_MTP_Storage(MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04002251
Ethan Yonker74db1572015-10-28 12:44:49 -05002252 gui_msg(Msg("remove_all=Removing all files under '{1}'")(Mount_Point));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002253 TWFunc::removeDir(Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04002254 Recreate_AndSec_Folder();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002255 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002256}
2257
Dees_Troye5017042013-08-29 16:38:55 +00002258bool TWPartition::Wipe_F2FS() {
Vojtech Bocek05534202013-09-11 08:11:56 +02002259 string command;
Dees_Troye5017042013-08-29 16:38:55 +00002260
2261 if (TWFunc::Path_Exists("/sbin/mkfs.f2fs")) {
2262 if (!UnMount(true))
2263 return false;
2264
Greg Wallace4b44fef2015-12-29 15:33:24 -05002265 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mkfs.f2fs"));
Dees_Troye5017042013-08-29 16:38:55 +00002266 Find_Actual_Block_Device();
Ethan Yonker93382822018-11-01 15:25:31 -05002267 if (!TWFunc::Path_Exists("/sbin/sload.f2fs")) {
2268 command = "mkfs.f2fs -t 0";
2269 if (!Is_Decrypted && Length != 0) {
2270 // Only use length if we're not decrypted
2271 char len[32];
2272 int mod_length = Length;
2273 if (Length < 0)
2274 mod_length *= -1;
2275 sprintf(len, "%i", mod_length);
2276 command += " -r ";
2277 command += len;
2278 }
2279 command += " " + Actual_Block_Device;
2280 } else {
2281 unsigned long long size = IOCTL_Get_Block_Size() + Length;
2282 command = "mkfs.f2fs -d1 -f -O encrypt -O quota -O verity -w 4096 " + Actual_Block_Device + " " + std::to_string(size / 4096) + " && sload.f2fs -t /data " + Actual_Block_Device;
dhacker29a3fa75f2015-01-17 19:42:33 -05002283 }
Vojtech Bocek05534202013-09-11 08:11:56 +02002284 if (TWFunc::Exec_Cmd(command) == 0) {
Dees_Troye5017042013-08-29 16:38:55 +00002285 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05002286 gui_msg("done=Done.");
Dees_Troye5017042013-08-29 16:38:55 +00002287 return true;
2288 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002289 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troye5017042013-08-29 16:38:55 +00002290 return false;
2291 }
2292 return true;
2293 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002294 LOGINFO("mkfs.f2fs binary not found, using rm -rf to wipe.\n");
Dees_Troye5017042013-08-29 16:38:55 +00002295 return Wipe_RMRF();
2296 }
2297 return false;
2298}
2299
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002300bool TWPartition::Wipe_NTFS() {
2301 string command;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002302 string Ntfsmake_Binary;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002303
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002304 if (TWFunc::Path_Exists("/sbin/mkntfs"))
2305 Ntfsmake_Binary = "mkntfs";
2306 else if (TWFunc::Path_Exists("/sbin/mkfs.ntfs"))
2307 Ntfsmake_Binary = "mkfs.ntfs";
2308 else
2309 return false;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002310
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002311 if (!UnMount(true))
2312 return false;
2313
Greg Wallace4b44fef2015-12-29 15:33:24 -05002314 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)(Ntfsmake_Binary));
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002315 Find_Actual_Block_Device();
2316 command = "/sbin/" + Ntfsmake_Binary + " " + Actual_Block_Device;
2317 if (TWFunc::Exec_Cmd(command) == 0) {
2318 Recreate_AndSec_Folder();
2319 gui_msg("done=Done.");
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002320 return true;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002321 } else {
2322 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
2323 return false;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002324 }
2325 return false;
2326}
2327
Dees_Troy51a0e822012-09-05 15:24:24 -04002328bool TWPartition::Wipe_Data_Without_Wiping_Media() {
Ethan Yonker83e82572014-04-04 10:59:28 -05002329#ifdef TW_OEM_BUILD
2330 // In an OEM Build we want to do a full format
2331 return Wipe_Encryption();
2332#else
Ethan Yonker66a19492015-12-10 10:19:45 -06002333 bool ret = false;
Dees_Troy38bd7602012-09-14 13:33:53 -04002334
Dees_Troy38bd7602012-09-14 13:33:53 -04002335 if (!Mount(true))
2336 return false;
2337
Ethan Yonker74db1572015-10-28 12:44:49 -05002338 gui_msg("wiping_data=Wiping data without wiping /data/media ...");
Ethan Yonker66a19492015-12-10 10:19:45 -06002339 ret = Wipe_Data_Without_Wiping_Media_Func(Mount_Point + "/");
2340 if (ret)
2341 gui_msg("done=Done.");
2342 return ret;
2343#endif // ifdef TW_OEM_BUILD
2344}
2345
2346bool TWPartition::Wipe_Data_Without_Wiping_Media_Func(const string& parent __unused) {
2347 string dir;
Dees_Troy38bd7602012-09-14 13:33:53 -04002348
2349 DIR* d;
Ethan Yonker66a19492015-12-10 10:19:45 -06002350 d = opendir(parent.c_str());
Dees_Troy16b74352012-11-14 22:27:31 +00002351 if (d != NULL) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002352 struct dirent* de;
2353 while ((de = readdir(d)) != NULL) {
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002354 if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) continue;
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05002355
Ethan Yonker66a19492015-12-10 10:19:45 -06002356 dir = parent;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002357 dir.append(de->d_name);
Ethan Yonker3fdcda42016-11-30 12:29:37 -06002358 if (wipe_exclusions.check_skip_dirs(dir)) {
Ethan Yonker66a19492015-12-10 10:19:45 -06002359 LOGINFO("skipped '%s'\n", dir.c_str());
2360 continue;
2361 }
Dees_Troyce675462013-01-09 19:48:21 +00002362 if (de->d_type == DT_DIR) {
Ethan Yonker66a19492015-12-10 10:19:45 -06002363 dir.append("/");
2364 if (!Wipe_Data_Without_Wiping_Media_Func(dir)) {
2365 closedir(d);
2366 return false;
2367 }
2368 rmdir(dir.c_str());
bigbiff bigbiff98f1f902013-01-19 18:46:13 -05002369 } else if (de->d_type == DT_REG || de->d_type == DT_LNK || de->d_type == DT_FIFO || de->d_type == DT_SOCK) {
nkk719efbe762017-06-13 19:49:05 +03002370 if (unlink(dir.c_str()) != 0)
Ethan Yonker79f88bd2016-12-09 14:52:12 -06002371 LOGINFO("Unable to unlink '%s': %s\n", dir.c_str(), strerror(errno));
Dees_Troyce675462013-01-09 19:48:21 +00002372 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002373 }
2374 closedir(d);
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05002375
Dees_Troy16b74352012-11-14 22:27:31 +00002376 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -04002377 }
Ethan Yonker74db1572015-10-28 12:44:49 -05002378 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy16b74352012-11-14 22:27:31 +00002379 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04002380}
2381
bigbiffce8f83c2015-12-12 18:30:21 -05002382bool TWPartition::Backup_Tar(PartitionSettings *part_settings, pid_t *tar_fork_pid) {
Ethan Yonker472f5062016-02-25 13:47:30 -06002383 string Full_FileName;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002384 twrpTar tar;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05002385
Dees_Troy43d8b002012-09-17 16:00:01 -04002386 if (!Mount(true))
2387 return false;
2388
Dees_Troya13d74f2013-03-24 08:54:55 -05002389 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Backup_Display_Name, "Backing Up");
Ethan Yonker74db1572015-10-28 12:44:49 -05002390 gui_msg(Msg("backing_up=Backing up {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002391
Ethan Yonker472f5062016-02-25 13:47:30 -06002392 DataManager::GetValue(TW_USE_COMPRESSION_VAR, tar.use_compression);
Matt Mowerbb81e5d2014-03-20 18:05:41 -05002393
Dees_Troy83bd4832013-05-04 12:39:56 +00002394#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
Ethan Yonker472f5062016-02-25 13:47:30 -06002395 if (Can_Encrypt_Backup) {
2396 DataManager::GetValue("tw_encrypt_backup", tar.use_encryption);
2397 if (tar.use_encryption) {
2398 if (Use_Userdata_Encryption)
2399 tar.userdata_encryption = tar.use_encryption;
2400 string Password;
2401 DataManager::GetValue("tw_backup_password", Password);
2402 tar.setpassword(Password);
2403 } else {
2404 tar.use_encryption = 0;
2405 }
Dees_Troy83bd4832013-05-04 12:39:56 +00002406 }
2407#endif
Dees_Troy43d8b002012-09-17 16:00:01 -04002408
Ethan Yonker472f5062016-02-25 13:47:30 -06002409 Backup_FileName = Backup_Name + "." + Current_File_System + ".win";
Ethan Yonkere080c1f2016-09-19 13:50:25 -05002410 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
Ethan Yonker01f4e032017-02-03 15:30:52 -06002411 if (Has_Data_Media)
2412 gui_msg(Msg(msg::kWarning, "backup_storage_warning=Backups of {1} do not include any files in internal storage such as pictures or downloads.")(Display_Name));
bigbiffce8f83c2015-12-12 18:30:21 -05002413 tar.part_settings = part_settings;
Ethan Yonker3fdcda42016-11-30 12:29:37 -06002414 tar.backup_exclusions = &backup_exclusions;
Dees Troye0a433a2013-12-02 04:10:37 +00002415 tar.setdir(Backup_Path);
2416 tar.setfn(Full_FileName);
2417 tar.setsize(Backup_Size);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002418 tar.partition_name = Backup_Name;
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002419 tar.backup_folder = part_settings->Backup_Folder;
bigbiffce8f83c2015-12-12 18:30:21 -05002420 if (tar.createTarFork(tar_fork_pid) != 0)
Dees Troye0a433a2013-12-02 04:10:37 +00002421 return false;
Dees_Troy43d8b002012-09-17 16:00:01 -04002422 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002423}
2424
bigbiffce8f83c2015-12-12 18:30:21 -05002425bool TWPartition::Backup_Image(PartitionSettings *part_settings) {
2426 string Full_FileName, adb_file_name;
igoriok87e3d932013-01-31 21:03:53 +02002427
Ethan Yonker74db1572015-10-28 12:44:49 -05002428 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, gui_parse_text("{@backing}"));
2429 gui_msg(Msg("backing_up=Backing up {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002430
Ethan Yonker472f5062016-02-25 13:47:30 -06002431 Backup_FileName = Backup_Name + "." + Current_File_System + ".win";
Dees_Troy43d8b002012-09-17 16:00:01 -04002432
bigbiffce8f83c2015-12-12 18:30:21 -05002433 if (part_settings->adbbackup) {
2434 Full_FileName = TW_ADB_BACKUP;
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002435 adb_file_name = part_settings->Backup_Folder + "/" + Backup_FileName;
bigbiffce8f83c2015-12-12 18:30:21 -05002436 }
2437 else
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002438 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
bigbiffce8f83c2015-12-12 18:30:21 -05002439
2440 part_settings->total_restore_size = Backup_Size;
2441
2442 if (part_settings->adbbackup) {
2443 if (!twadbbu::Write_TWIMG(adb_file_name, Backup_Size))
2444 return false;
2445 }
2446
2447 if (!Raw_Read_Write(part_settings))
Ethan Yonker472f5062016-02-25 13:47:30 -06002448 return false;
2449
bigbiffce8f83c2015-12-12 18:30:21 -05002450 if (part_settings->adbbackup) {
2451 if (!twadbbu::Write_TWEOF())
2452 return false;
Dees_Troy7c2dec82012-09-26 09:49:14 -04002453 }
Dees_Troy43d8b002012-09-17 16:00:01 -04002454 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002455}
2456
bigbiffce8f83c2015-12-12 18:30:21 -05002457bool TWPartition::Raw_Read_Write(PartitionSettings *part_settings) {
2458 unsigned long long RW_Block_Size, Remain = Backup_Size;
2459 int src_fd = -1, dest_fd = -1;
2460 ssize_t bs;
Ethan Yonker472f5062016-02-25 13:47:30 -06002461 bool ret = false;
2462 void* buffer = NULL;
2463 unsigned long long backedup_size = 0;
bigbiffce8f83c2015-12-12 18:30:21 -05002464 string srcfn, destfn;
Ethan Yonker472f5062016-02-25 13:47:30 -06002465
bigbiffce8f83c2015-12-12 18:30:21 -05002466 if (part_settings->PM_Method == PM_BACKUP) {
2467 srcfn = Actual_Block_Device;
2468 if (part_settings->adbbackup)
2469 destfn = TW_ADB_BACKUP;
bigbiff bigbiff38b83c12017-12-28 19:58:52 -05002470 else {
Ethan Yonkere080c1f2016-09-19 13:50:25 -05002471 destfn = part_settings->Backup_Folder + "/" + Backup_FileName;
bigbiff bigbiff38b83c12017-12-28 19:58:52 -05002472 }
bigbiffce8f83c2015-12-12 18:30:21 -05002473 }
2474 else {
2475 destfn = Actual_Block_Device;
2476 if (part_settings->adbbackup) {
2477 srcfn = TW_ADB_RESTORE;
2478 } else {
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002479 srcfn = part_settings->Backup_Folder + "/" + Backup_FileName;
bigbiffce8f83c2015-12-12 18:30:21 -05002480 Remain = TWFunc::Get_File_Size(srcfn);
2481 }
2482 }
Ethan Yonker472f5062016-02-25 13:47:30 -06002483
bigbiffce8f83c2015-12-12 18:30:21 -05002484 src_fd = open(srcfn.c_str(), O_RDONLY | O_LARGEFILE);
Ethan Yonker472f5062016-02-25 13:47:30 -06002485 if (src_fd < 0) {
bigbiffce8f83c2015-12-12 18:30:21 -05002486 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(srcfn.c_str())(strerror(errno)));
Ethan Yonker472f5062016-02-25 13:47:30 -06002487 return false;
2488 }
bigbiffce8f83c2015-12-12 18:30:21 -05002489
2490 dest_fd = open(destfn.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_LARGEFILE, S_IRUSR | S_IWUSR);
Ethan Yonker472f5062016-02-25 13:47:30 -06002491 if (dest_fd < 0) {
bigbiffce8f83c2015-12-12 18:30:21 -05002492 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(destfn.c_str())(strerror(errno)));
Ethan Yonker472f5062016-02-25 13:47:30 -06002493 goto exit;
2494 }
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -04002495
bigbiffce8f83c2015-12-12 18:30:21 -05002496 LOGINFO("Reading '%s', writing '%s'\n", srcfn.c_str(), destfn.c_str());
2497
2498 if (part_settings->adbbackup) {
2499 RW_Block_Size = MAX_ADB_READ;
2500 bs = MAX_ADB_READ;
2501 }
2502 else {
2503 RW_Block_Size = 1048576LLU; // 1MB
2504 bs = (ssize_t)(RW_Block_Size);
2505 }
2506
Ethan Yonker472f5062016-02-25 13:47:30 -06002507 buffer = malloc((size_t)bs);
2508 if (!buffer) {
2509 LOGINFO("Raw_Read_Write failed to malloc\n");
2510 goto exit;
2511 }
bigbiffce8f83c2015-12-12 18:30:21 -05002512
2513 if (part_settings->progress)
2514 part_settings->progress->SetPartitionSize(part_settings->total_restore_size);
2515
Ethan Yonker472f5062016-02-25 13:47:30 -06002516 while (Remain > 0) {
2517 if (Remain < RW_Block_Size)
bigbiffce8f83c2015-12-12 18:30:21 -05002518 bs = (ssize_t)(Remain);
Matt Mowera8a89d12016-12-30 18:10:37 -06002519 if (read(src_fd, buffer, bs) != bs) {
Ethan Yonker472f5062016-02-25 13:47:30 -06002520 LOGINFO("Error reading source fd (%s)\n", strerror(errno));
2521 goto exit;
2522 }
2523 if (write(dest_fd, buffer, bs) != bs) {
2524 LOGINFO("Error writing destination fd (%s)\n", strerror(errno));
2525 goto exit;
2526 }
2527 backedup_size += (unsigned long long)(bs);
Matt Mower029a82d2017-01-08 13:12:38 -06002528 Remain -= (unsigned long long)(bs);
bigbiffce8f83c2015-12-12 18:30:21 -05002529 if (part_settings->progress)
2530 part_settings->progress->UpdateSize(backedup_size);
Ethan Yonker472f5062016-02-25 13:47:30 -06002531 if (PartitionManager.Check_Backup_Cancel() != 0)
2532 goto exit;
2533 }
bigbiffce8f83c2015-12-12 18:30:21 -05002534 if (part_settings->progress)
2535 part_settings->progress->UpdateDisplayDetails(true);
Ethan Yonker472f5062016-02-25 13:47:30 -06002536 fsync(dest_fd);
nailyka083dc62016-11-11 21:41:28 +01002537
2538 if (!part_settings->adbbackup && part_settings->PM_Method == PM_BACKUP) {
2539 tw_set_default_metadata(destfn.c_str());
2540 LOGINFO("Restored default metadata for %s\n", destfn.c_str());
2541 }
2542
Ethan Yonker472f5062016-02-25 13:47:30 -06002543 ret = true;
2544exit:
2545 if (src_fd >= 0)
2546 close(src_fd);
2547 if (dest_fd >= 0)
2548 close(dest_fd);
2549 if (buffer)
2550 free(buffer);
2551 return ret;
2552}
2553
bigbiffce8f83c2015-12-12 18:30:21 -05002554bool TWPartition::Backup_Dump_Image(PartitionSettings *part_settings) {
Vojtech Bocek05534202013-09-11 08:11:56 +02002555 string Full_FileName, Command;
Dees_Troy43d8b002012-09-17 16:00:01 -04002556
Ethan Yonker74db1572015-10-28 12:44:49 -05002557 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, gui_parse_text("{@backing}"));
2558 gui_msg(Msg("backing_up=Backing up {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002559
bigbiffce8f83c2015-12-12 18:30:21 -05002560 if (part_settings->progress)
2561 part_settings->progress->SetPartitionSize(Backup_Size);
Dees_Troy43d8b002012-09-17 16:00:01 -04002562
Ethan Yonker472f5062016-02-25 13:47:30 -06002563 Backup_FileName = Backup_Name + "." + Current_File_System + ".win";
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002564 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04002565
2566 Command = "dump_image " + MTD_Name + " '" + Full_FileName + "'";
bigbiffce8f83c2015-12-12 18:30:21 -05002567
Dees_Troy2673cec2013-04-02 20:22:16 +00002568 LOGINFO("Backup command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02002569 TWFunc::Exec_Cmd(Command);
Ethan Yonker4b94cfd2014-12-11 10:00:45 -06002570 tw_set_default_metadata(Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04002571 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
2572 // Actual size may not match backup size due to bad blocks on MTD devices so just check for 0 bytes
Matt Mower3c366972015-12-25 19:28:31 -06002573 gui_msg(Msg(msg::kError, "backup_size=Backup file size for '{1}' is 0 bytes.")(Full_FileName));
Dees_Troy7c2dec82012-09-26 09:49:14 -04002574 return false;
2575 }
bigbiffce8f83c2015-12-12 18:30:21 -05002576 if (part_settings->progress)
2577 part_settings->progress->UpdateSize(Backup_Size);
2578
Dees_Troy43d8b002012-09-17 16:00:01 -04002579 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002580}
2581
bigbiffce8f83c2015-12-12 18:30:21 -05002582unsigned long long TWPartition::Get_Restore_Size(PartitionSettings *part_settings) {
2583 if (!part_settings->adbbackup) {
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002584 InfoManager restore_info(part_settings->Backup_Folder + "/" + Backup_Name + ".info");
bigbiffce8f83c2015-12-12 18:30:21 -05002585 if (restore_info.LoadValues() == 0) {
2586 if (restore_info.GetValue("backup_size", Restore_Size) == 0) {
2587 LOGINFO("Read info file, restore size is %llu\n", Restore_Size);
2588 return Restore_Size;
2589 }
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002590 }
2591 }
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002592
Matt Mower029a82d2017-01-08 13:12:38 -06002593 string Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
2594 string Restore_File_System = Get_Restore_File_System(part_settings);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002595
2596 if (Is_Image(Restore_File_System)) {
2597 Restore_Size = TWFunc::Get_File_Size(Full_FileName);
2598 return Restore_Size;
2599 }
2600
2601 twrpTar tar;
2602 tar.setdir(Backup_Path);
2603 tar.setfn(Full_FileName);
bigbiffce8f83c2015-12-12 18:30:21 -05002604 tar.backup_name = Full_FileName;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002605#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
2606 string Password;
2607 DataManager::GetValue("tw_restore_password", Password);
2608 if (!Password.empty())
2609 tar.setpassword(Password);
2610#endif
2611 tar.partition_name = Backup_Name;
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002612 tar.backup_folder = part_settings->Backup_Folder;
bigbiffce8f83c2015-12-12 18:30:21 -05002613 tar.part_settings = part_settings;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002614 Restore_Size = tar.get_size();
2615 return Restore_Size;
2616}
2617
bigbiffce8f83c2015-12-12 18:30:21 -05002618bool TWPartition::Restore_Tar(PartitionSettings *part_settings) {
Ethan Yonker472f5062016-02-25 13:47:30 -06002619 string Full_FileName;
Dees Troy4159aed2014-02-28 17:24:43 +00002620 bool ret = false;
bigbiffce8f83c2015-12-12 18:30:21 -05002621 string Restore_File_System = Get_Restore_File_System(part_settings);
Dees_Troy43d8b002012-09-17 16:00:01 -04002622
Dees_Troye58d5262012-09-21 12:27:57 -04002623 if (Has_Android_Secure) {
Dees_Troye58d5262012-09-21 12:27:57 -04002624 if (!Wipe_AndSec())
2625 return false;
Gary Peck43acadf2012-11-21 21:19:01 -08002626 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002627 gui_msg(Msg("wiping=Wiping {1}")(Backup_Display_Name));
Ethan Yonker5eac2222014-06-11 12:22:55 -05002628 if (Has_Data_Media && Mount_Point == "/data" && Restore_File_System != Current_File_System) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002629 gui_msg(Msg(msg::kWarning, "datamedia_fs_restore=WARNING: This /data backup was made with {1} file system! The backup may not boot unless you change back to {1}.")(Restore_File_System));
Ethan Yonker5eac2222014-06-11 12:22:55 -05002630 if (!Wipe_Data_Without_Wiping_Media())
2631 return false;
2632 } else {
2633 if (!Wipe(Restore_File_System))
2634 return false;
2635 }
Dees_Troye58d5262012-09-21 12:27:57 -04002636 }
Matt Mower3c366972015-12-25 19:28:31 -06002637 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Backup_Display_Name, gui_parse_text("{@restoring_hdr}"));
Ethan Yonker74db1572015-10-28 12:44:49 -05002638 gui_msg(Msg("restoring=Restoring {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002639
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05002640 // Remount as read/write as needed so we can restore the backup
2641 if (!ReMount_RW(true))
Dees_Troy43d8b002012-09-17 16:00:01 -04002642 return false;
2643
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002644 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
Ethan Yonker87af5632014-02-10 11:56:35 -06002645 twrpTar tar;
bigbiffce8f83c2015-12-12 18:30:21 -05002646 tar.part_settings = part_settings;
Ethan Yonker87af5632014-02-10 11:56:35 -06002647 tar.setdir(Backup_Path);
2648 tar.setfn(Full_FileName);
2649 tar.backup_name = Backup_Name;
2650#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
2651 string Password;
2652 DataManager::GetValue("tw_restore_password", Password);
2653 if (!Password.empty())
2654 tar.setpassword(Password);
2655#endif
bigbiffce8f83c2015-12-12 18:30:21 -05002656 part_settings->progress->SetPartitionSize(Get_Restore_Size(part_settings));
2657 if (tar.extractTarFork() != 0)
Dees Troy4159aed2014-02-28 17:24:43 +00002658 ret = false;
2659 else
2660 ret = true;
2661#ifdef HAVE_CAPABILITIES
2662 // Restore capabilities to the run-as binary
Captain Throwback9d6feb52018-07-27 10:05:24 -04002663 if (Mount_Point == PartitionManager.Get_Android_Root_Path() && Mount(true) && TWFunc::Path_Exists("/system/bin/run-as")) {
Dees Troy4159aed2014-02-28 17:24:43 +00002664 struct vfs_cap_data cap_data;
2665 uint64_t capabilities = (1 << CAP_SETUID) | (1 << CAP_SETGID);
2666
2667 memset(&cap_data, 0, sizeof(cap_data));
2668 cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE;
2669 cap_data.data[0].permitted = (uint32_t) (capabilities & 0xffffffff);
2670 cap_data.data[0].inheritable = 0;
2671 cap_data.data[1].permitted = (uint32_t) (capabilities >> 32);
2672 cap_data.data[1].inheritable = 0;
2673 if (setxattr("/system/bin/run-as", XATTR_NAME_CAPS, &cap_data, sizeof(cap_data), 0) < 0) {
2674 LOGINFO("Failed to reset capabilities of /system/bin/run-as binary.\n");
2675 } else {
2676 LOGINFO("Reset capabilities of /system/bin/run-as binary successful.\n");
2677 }
2678 }
2679#endif
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05002680 if (Mount_Read_Only || Mount_Flags & MS_RDONLY)
2681 // Remount as read only when restoration is complete
2682 ReMount(true);
2683
Dees Troy4159aed2014-02-28 17:24:43 +00002684 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04002685}
2686
bigbiffce8f83c2015-12-12 18:30:21 -05002687bool TWPartition::Restore_Image(PartitionSettings *part_settings) {
Ethan Yonker96af84a2015-01-05 14:58:36 -06002688 string Full_FileName;
bigbiffce8f83c2015-12-12 18:30:21 -05002689 string Restore_File_System = Get_Restore_File_System(part_settings);
Dees_Troy43d8b002012-09-17 16:00:01 -04002690
Matt Mower3c366972015-12-25 19:28:31 -06002691 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Backup_Display_Name, gui_parse_text("{@restoring_hdr}"));
Ethan Yonker74db1572015-10-28 12:44:49 -05002692 gui_msg(Msg("restoring=Restoring {1}...")(Backup_Display_Name));
bigbiffce8f83c2015-12-12 18:30:21 -05002693
2694 if (part_settings->adbbackup)
2695 Full_FileName = TW_ADB_RESTORE;
2696 else
Ethan Yonkere080c1f2016-09-19 13:50:25 -05002697 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
Gary Peck15e623d2012-11-21 21:07:58 -08002698
Ethan Yonker96af84a2015-01-05 14:58:36 -06002699 if (Restore_File_System == "emmc") {
bigbiffce8f83c2015-12-12 18:30:21 -05002700 if (!part_settings->adbbackup)
2701 part_settings->total_restore_size = (uint64_t)(TWFunc::Get_File_Size(Full_FileName));
2702 if (!Raw_Read_Write(part_settings))
Ethan Yonker96af84a2015-01-05 14:58:36 -06002703 return false;
2704 } else if (Restore_File_System == "mtd" || Restore_File_System == "bml") {
bigbiffce8f83c2015-12-12 18:30:21 -05002705 if (!Flash_Image_FI(Full_FileName, part_settings->progress))
2706 return false;
2707 }
2708
2709 if (part_settings->adbbackup) {
2710 if (!twadbbu::Write_TWEOF())
Ethan Yonker96af84a2015-01-05 14:58:36 -06002711 return false;
Gary Peck15e623d2012-11-21 21:07:58 -08002712 }
Dees_Troy43d8b002012-09-17 16:00:01 -04002713 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002714}
Dees_Troy5bf43922012-09-07 16:07:55 -04002715
2716bool TWPartition::Update_Size(bool Display_Error) {
Dees_Troy0550cfb2012-10-13 11:56:13 -04002717 bool ret = false, Was_Already_Mounted = false;
Dees_Troy51127312012-09-08 13:08:49 -04002718
Ethan Yonker1b190162016-12-05 15:25:19 -06002719 Find_Actual_Block_Device();
2720
2721 if (!Can_Be_Mounted && !Is_Encrypted) {
2722 if (TWFunc::Path_Exists(Actual_Block_Device) && Find_Partition_Size()) {
2723 Used = Size;
2724 Backup_Size = Size;
2725 return true;
2726 }
Dees_Troy5bf43922012-09-07 16:07:55 -04002727 return false;
Ethan Yonker1b190162016-12-05 15:25:19 -06002728 }
Dees_Troy5bf43922012-09-07 16:07:55 -04002729
Dees_Troy0550cfb2012-10-13 11:56:13 -04002730 Was_Already_Mounted = Is_Mounted();
Dees_Troy38bd7602012-09-14 13:33:53 -04002731 if (Removable || Is_Encrypted) {
2732 if (!Mount(false))
2733 return true;
2734 } else if (!Mount(Display_Error))
Dees_Troy5bf43922012-09-07 16:07:55 -04002735 return false;
Dees_Troy51127312012-09-08 13:08:49 -04002736
2737 ret = Get_Size_Via_statfs(Display_Error);
Dees_Troy0550cfb2012-10-13 11:56:13 -04002738 if (!ret || Size == 0) {
2739 if (!Get_Size_Via_df(Display_Error)) {
2740 if (!Was_Already_Mounted)
2741 UnMount(false);
Dees_Troy51127312012-09-08 13:08:49 -04002742 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04002743 }
2744 }
Dees_Troy51127312012-09-08 13:08:49 -04002745
Dees_Troy5bf43922012-09-07 16:07:55 -04002746 if (Has_Data_Media) {
2747 if (Mount(Display_Error)) {
Ethan Yonker3fdcda42016-11-30 12:29:37 -06002748 Used = backup_exclusions.Get_Folder_Size(Mount_Point);
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002749 Backup_Size = Used;
2750 int bak = (int)(Used / 1048576LLU);
Dees_Troy51127312012-09-08 13:08:49 -04002751 int fre = (int)(Free / 1048576LLU);
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002752 LOGINFO("Data backup size is %iMB, free: %iMB.\n", bak, fre);
Dees_Troy0550cfb2012-10-13 11:56:13 -04002753 } else {
2754 if (!Was_Already_Mounted)
2755 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04002756 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04002757 }
Dees_Troye58d5262012-09-21 12:27:57 -04002758 } else if (Has_Android_Secure) {
2759 if (Mount(Display_Error))
Ethan Yonker3fdcda42016-11-30 12:29:37 -06002760 Backup_Size = backup_exclusions.Get_Folder_Size(Backup_Path);
Dees_Troy0550cfb2012-10-13 11:56:13 -04002761 else {
2762 if (!Was_Already_Mounted)
2763 UnMount(false);
Dees_Troye58d5262012-09-21 12:27:57 -04002764 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04002765 }
Dees_Troy5bf43922012-09-07 16:07:55 -04002766 }
Dees_Troy0550cfb2012-10-13 11:56:13 -04002767 if (!Was_Already_Mounted)
2768 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04002769 return true;
Dees_Troy51127312012-09-08 13:08:49 -04002770}
Dees_Troy38bd7602012-09-14 13:33:53 -04002771
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06002772bool TWPartition::Find_Wildcard_Block_Devices(const string& Device) {
2773 int mount_point_index = 0; // we will need to create separate mount points for each partition found and we use this index to name each one
2774 string Path = TWFunc::Get_Path(Device);
2775 string Dev = TWFunc::Get_Filename(Device);
2776 size_t wildcard_index = Dev.find("*");
2777 if (wildcard_index != string::npos)
2778 Dev = Dev.substr(0, wildcard_index);
2779 wildcard_index = Dev.size();
2780 DIR* d = opendir(Path.c_str());
2781 if (d == NULL) {
2782 LOGINFO("Error opening '%s': %s\n", Path.c_str(), strerror(errno));
2783 return false;
2784 }
2785 struct dirent* de;
2786 while ((de = readdir(d)) != NULL) {
2787 if (de->d_type != DT_BLK || strlen(de->d_name) <= wildcard_index || strncmp(de->d_name, Dev.c_str(), wildcard_index) != 0)
2788 continue;
2789
2790 string item = Path + "/";
2791 item.append(de->d_name);
2792 if (PartitionManager.Find_Partition_By_Block_Device(item))
2793 continue;
2794 TWPartition *part = new TWPartition;
2795 char buffer[MAX_FSTAB_LINE_LENGTH];
2796 sprintf(buffer, "%s %s-%i auto defaults defaults", item.c_str(), Mount_Point.c_str(), ++mount_point_index);
2797 part->Process_Fstab_Line(buffer, false, NULL);
2798 char display[MAX_FSTAB_LINE_LENGTH];
2799 sprintf(display, "%s %i", Storage_Name.c_str(), mount_point_index);
2800 part->Storage_Name = display;
2801 part->Display_Name = display;
2802 part->Primary_Block_Device = item;
2803 part->Wildcard_Block_Device = false;
2804 part->Is_SubPartition = true;
2805 part->SubPartition_Of = Mount_Point;
2806 part->Is_Storage = Is_Storage;
2807 part->Can_Be_Mounted = true;
2808 part->Removable = true;
2809 part->Can_Be_Wiped = Can_Be_Wiped;
2810 part->Wipe_Available_in_GUI = Wipe_Available_in_GUI;
2811 part->Find_Actual_Block_Device();
2812 part->Update_Size(false);
2813 Has_SubPartition = true;
2814 PartitionManager.Output_Partition(part);
2815 PartitionManager.Add_Partition(part);
2816 }
2817 closedir(d);
2818 return (mount_point_index > 0);
2819}
2820
Dees_Troy38bd7602012-09-14 13:33:53 -04002821void TWPartition::Find_Actual_Block_Device(void) {
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06002822 if (!Sysfs_Entry.empty() && Primary_Block_Device.empty() && Decrypted_Block_Device.empty()) {
2823 /* Sysfs_Entry.empty() indicates if this is a sysfs entry that begins with /device/
2824 * If we have a syfs entry then we are looking for this device from a uevent add.
2825 * The uevent add will set the primary block device based on the data we receive from
2826 * after checking for adopted storage. If the device ends up being adopted, then the
2827 * decrypted block device will be set instead of the primary block device. */
2828 Is_Present = false;
2829 return;
2830 }
2831 if (Wildcard_Block_Device && !Is_Adopted_Storage) {
2832 Is_Present = false;
2833 Actual_Block_Device = "";
2834 Can_Be_Mounted = false;
2835 if (!Find_Wildcard_Block_Devices(Primary_Block_Device)) {
2836 string Dev = Primary_Block_Device.substr(0, Primary_Block_Device.find("*"));
2837 if (TWFunc::Path_Exists(Dev)) {
2838 Is_Present = true;
2839 Can_Be_Mounted = true;
2840 Actual_Block_Device = Dev;
2841 }
2842 }
2843 return;
2844 } else if (Is_Decrypted && !Decrypted_Block_Device.empty()) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002845 Actual_Block_Device = Decrypted_Block_Device;
Ethan Yonker1b190162016-12-05 15:25:19 -06002846 if (TWFunc::Path_Exists(Decrypted_Block_Device)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002847 Is_Present = true;
Ethan Yonker1b190162016-12-05 15:25:19 -06002848 return;
2849 }
2850 } else if (SlotSelect && TWFunc::Path_Exists(Primary_Block_Device + PartitionManager.Get_Active_Slot_Suffix())) {
2851 Actual_Block_Device = Primary_Block_Device + PartitionManager.Get_Active_Slot_Suffix();
2852 unlink(Primary_Block_Device.c_str());
2853 symlink(Actual_Block_Device.c_str(), Primary_Block_Device.c_str()); // we create a non-slot symlink pointing to the currently selected slot which may assist zips with installing
2854 Is_Present = true;
2855 return;
Dees_Troy43d8b002012-09-17 16:00:01 -04002856 } else if (TWFunc::Path_Exists(Primary_Block_Device)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002857 Is_Present = true;
2858 Actual_Block_Device = Primary_Block_Device;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002859 return;
2860 }
Ethan Yonker1b190162016-12-05 15:25:19 -06002861 if (!Alternate_Block_Device.empty() && TWFunc::Path_Exists(Alternate_Block_Device)) {
Dees_Troy3f04d032012-10-07 18:20:09 -04002862 Actual_Block_Device = Alternate_Block_Device;
Dees_Troy38bd7602012-09-14 13:33:53 -04002863 Is_Present = true;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002864 } else {
Dees_Troy38bd7602012-09-14 13:33:53 -04002865 Is_Present = false;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002866 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002867}
2868
2869void TWPartition::Recreate_Media_Folder(void) {
2870 string Command;
Ethan Yonker66a19492015-12-10 10:19:45 -06002871 string Media_Path = Mount_Point + "/media";
Dees_Troy38bd7602012-09-14 13:33:53 -04002872
Ethan Yonkerbd7492d2016-12-07 13:55:01 -06002873 if (Is_FBE) {
2874 LOGINFO("Not recreating media folder on FBE\n");
2875 return;
2876 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002877 if (!Mount(true)) {
Ethan Yonker66a19492015-12-10 10:19:45 -06002878 gui_msg(Msg(msg::kError, "recreate_folder_err=Unable to recreate {1} folder.")(Media_Path));
2879 } else if (!TWFunc::Path_Exists(Media_Path)) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002880 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Ethan Yonker66a19492015-12-10 10:19:45 -06002881 LOGINFO("Recreating %s folder.\n", Media_Path.c_str());
2882 mkdir(Media_Path.c_str(), 0770);
xiaolu9416f4f2015-06-04 08:22:23 +08002883 string Internal_path = DataManager::GetStrValue("tw_internal_path");
2884 if (!Internal_path.empty()) {
2885 LOGINFO("Recreating %s folder.\n", Internal_path.c_str());
2886 mkdir(Internal_path.c_str(), 0770);
2887 }
2888#ifdef TW_INTERNAL_STORAGE_PATH
2889 mkdir(EXPAND(TW_INTERNAL_STORAGE_PATH), 0770);
2890#endif
Matt Mower87413642017-01-17 21:14:46 -06002891
thata3d31fb2014-12-21 22:27:40 +01002892 // Afterwards, we will try to set the
2893 // default metadata that we were hopefully able to get during
2894 // early boot.
Ethan Yonker66a19492015-12-10 10:19:45 -06002895 tw_set_default_metadata(Media_Path.c_str());
xiaolu9416f4f2015-06-04 08:22:23 +08002896 if (!Internal_path.empty())
2897 tw_set_default_metadata(Internal_path.c_str());
Matt Mower87413642017-01-17 21:14:46 -06002898
Ethan Yonker5eac2222014-06-11 12:22:55 -05002899 // Toggle mount to ensure that "internal sdcard" gets mounted
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002900 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Ethan Yonker5eac2222014-06-11 12:22:55 -05002901 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Dees_Troy38bd7602012-09-14 13:33:53 -04002902 }
Dees_Troy43d8b002012-09-17 16:00:01 -04002903}
Dees_Troye58d5262012-09-21 12:27:57 -04002904
2905void TWPartition::Recreate_AndSec_Folder(void) {
Dees_Troye58d5262012-09-21 12:27:57 -04002906 if (!Has_Android_Secure)
2907 return;
Dees_Troy2673cec2013-04-02 20:22:16 +00002908 LOGINFO("Creating %s: %s\n", Backup_Display_Name.c_str(), Symlink_Path.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -04002909 if (!Mount(true)) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002910 gui_msg(Msg(msg::kError, "recreate_folder_err=Unable to recreate {1} folder.")(Backup_Name));
Dees_Troye58d5262012-09-21 12:27:57 -04002911 } else if (!TWFunc::Path_Exists(Symlink_Path)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00002912 LOGINFO("Recreating %s folder.\n", Backup_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002913 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05002914 mkdir(Symlink_Path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002915 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04002916 }
2917}
bigbiff7cb4c332014-11-26 20:36:07 -05002918
2919uint64_t TWPartition::Get_Max_FileSize() {
2920 uint64_t maxFileSize = 0;
2921 const uint64_t constGB = (uint64_t) 1024 * 1024 * 1024;
2922 const uint64_t constTB = (uint64_t) constGB * 1024;
2923 const uint64_t constPB = (uint64_t) constTB * 1024;
bigbiff0c532032014-12-21 13:41:26 -05002924 if (Current_File_System == "ext4")
2925 maxFileSize = 16 * constTB; //16 TB
2926 else if (Current_File_System == "vfat")
2927 maxFileSize = 4 * constGB; //4 GB
2928 else if (Current_File_System == "ntfs")
2929 maxFileSize = 256 * constTB; //256 TB
2930 else if (Current_File_System == "exfat")
2931 maxFileSize = 16 * constPB; //16 PB
2932 else if (Current_File_System == "ext3")
2933 maxFileSize = 2 * constTB; //2 TB
2934 else if (Current_File_System == "f2fs")
2935 maxFileSize = 3.94 * constTB; //3.94 TB
bigbiff7cb4c332014-11-26 20:36:07 -05002936 else
2937 maxFileSize = 100000000L;
2938 return maxFileSize - 1;
2939}
2940
bigbiffce8f83c2015-12-12 18:30:21 -05002941bool TWPartition::Flash_Image(PartitionSettings *part_settings) {
2942 string Restore_File_System, full_filename;
Ethan Yonker96af84a2015-01-05 14:58:36 -06002943
Ethan Yonkere080c1f2016-09-19 13:50:25 -05002944 full_filename = part_settings->Backup_Folder + "/" + Backup_FileName;
Ethan Yonker96af84a2015-01-05 14:58:36 -06002945
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002946 LOGINFO("Image filename is: %s\n", Backup_FileName.c_str());
bigbiffce8f83c2015-12-12 18:30:21 -05002947
2948 if (Backup_Method == BM_FILES) {
Ethan Yonker96af84a2015-01-05 14:58:36 -06002949 LOGERR("Cannot flash images to file systems\n");
2950 return false;
2951 } else if (!Can_Flash_Img) {
2952 LOGERR("Cannot flash images to partitions %s\n", Display_Name.c_str());
2953 return false;
2954 } else {
2955 if (!Find_Partition_Size()) {
2956 LOGERR("Unable to find partition size for '%s'\n", Mount_Point.c_str());
2957 return false;
2958 }
bigbiffce8f83c2015-12-12 18:30:21 -05002959 unsigned long long image_size = TWFunc::Get_File_Size(full_filename);
Ethan Yonker96af84a2015-01-05 14:58:36 -06002960 if (image_size > Size) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002961 LOGINFO("Size (%llu bytes) of image '%s' is larger than target device '%s' (%llu bytes)\n",
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002962 image_size, Backup_FileName.c_str(), Actual_Block_Device.c_str(), Size);
Ethan Yonker74db1572015-10-28 12:44:49 -05002963 gui_err("img_size_err=Size of image is larger than target device");
Ethan Yonker96af84a2015-01-05 14:58:36 -06002964 return false;
2965 }
bigbiffce8f83c2015-12-12 18:30:21 -05002966 if (Backup_Method == BM_DD) {
2967 if (!part_settings->adbbackup) {
2968 if (Is_Sparse_Image(full_filename)) {
2969 return Flash_Sparse_Image(full_filename);
2970 }
Ethan Yonker472f5062016-02-25 13:47:30 -06002971 }
bigbiffce8f83c2015-12-12 18:30:21 -05002972 return Raw_Read_Write(part_settings);
2973 } else if (Backup_Method == BM_FLASH_UTILS) {
2974 return Flash_Image_FI(full_filename, NULL);
Ethan Yonker472f5062016-02-25 13:47:30 -06002975 }
Ethan Yonker96af84a2015-01-05 14:58:36 -06002976 }
2977
2978 LOGERR("Unknown flash method for '%s'\n", Mount_Point.c_str());
2979 return false;
2980}
2981
Ethan Yonker472f5062016-02-25 13:47:30 -06002982bool TWPartition::Is_Sparse_Image(const string& Filename) {
HashBanged974bb2016-01-30 14:20:09 -05002983 uint32_t magic = 0;
2984 int fd = open(Filename.c_str(), O_RDONLY);
2985 if (fd < 0) {
2986 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Filename)(strerror(errno)));
2987 return false;
2988 }
bigbiffce8f83c2015-12-12 18:30:21 -05002989
HashBanged974bb2016-01-30 14:20:09 -05002990 if (read(fd, &magic, sizeof(magic)) != sizeof(magic)) {
2991 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Filename)(strerror(errno)));
2992 close(fd);
2993 return false;
2994 }
2995 close(fd);
Ethan Yonker472f5062016-02-25 13:47:30 -06002996 if (magic == SPARSE_HEADER_MAGIC)
2997 return true;
2998 return false;
2999}
3000
3001bool TWPartition::Flash_Sparse_Image(const string& Filename) {
3002 string Command;
3003
3004 gui_msg(Msg("flashing=Flashing {1}...")(Display_Name));
3005
3006 Command = "simg2img '" + Filename + "' '" + Actual_Block_Device + "'";
Ethan Yonker96af84a2015-01-05 14:58:36 -06003007 LOGINFO("Flash command: '%s'\n", Command.c_str());
3008 TWFunc::Exec_Cmd(Command);
3009 return true;
3010}
3011
Ethan Yonker472f5062016-02-25 13:47:30 -06003012bool TWPartition::Flash_Image_FI(const string& Filename, ProgressTracking *progress) {
Ethan Yonker96af84a2015-01-05 14:58:36 -06003013 string Command;
Ethan Yonker472f5062016-02-25 13:47:30 -06003014 unsigned long long file_size;
Ethan Yonker96af84a2015-01-05 14:58:36 -06003015
Ethan Yonker74db1572015-10-28 12:44:49 -05003016 gui_msg(Msg("flashing=Flashing {1}...")(Display_Name));
Ethan Yonker472f5062016-02-25 13:47:30 -06003017 if (progress) {
3018 file_size = (unsigned long long)(TWFunc::Get_File_Size(Filename));
3019 progress->SetPartitionSize(file_size);
3020 }
Ethan Yonker96af84a2015-01-05 14:58:36 -06003021 // Sometimes flash image doesn't like to flash due to the first 2KB matching, so we erase first to ensure that it flashes
3022 Command = "erase_image " + MTD_Name;
3023 LOGINFO("Erase command: '%s'\n", Command.c_str());
3024 TWFunc::Exec_Cmd(Command);
3025 Command = "flash_image " + MTD_Name + " '" + Filename + "'";
3026 LOGINFO("Flash command: '%s'\n", Command.c_str());
3027 TWFunc::Exec_Cmd(Command);
Ethan Yonker472f5062016-02-25 13:47:30 -06003028 if (progress)
3029 progress->UpdateSize(file_size);
Ethan Yonker96af84a2015-01-05 14:58:36 -06003030 return true;
3031}
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05003032
3033void TWPartition::Change_Mount_Read_Only(bool new_value) {
3034 Mount_Read_Only = new_value;
3035}
3036
bigbiffce8f83c2015-12-12 18:30:21 -05003037bool TWPartition::Is_Read_Only() {
3038 return Mount_Read_Only;
3039}
3040
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05003041int TWPartition::Check_Lifetime_Writes() {
3042 bool original_read_only = Mount_Read_Only;
3043 int ret = 1;
3044
3045 Mount_Read_Only = true;
3046 if (Mount(false)) {
3047 Find_Actual_Block_Device();
Ethan Yonker54970bb2019-01-17 16:19:24 -06003048 string temp = Actual_Block_Device;
3049 Find_Real_Block_Device(temp, false);
3050 string block = basename(temp.c_str());
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05003051 string file = "/sys/fs/" + Current_File_System + "/" + block + "/lifetime_write_kbytes";
3052 string result;
3053 if (TWFunc::Path_Exists(file)) {
3054 if (TWFunc::read_file(file, result) != 0) {
3055 LOGINFO("Check_Lifetime_Writes of '%s' failed to read_file\n", file.c_str());
3056 } else {
3057 LOGINFO("Check_Lifetime_Writes result: '%s'\n", result.c_str());
3058 if (result == "0") {
3059 ret = 0;
3060 }
3061 }
3062 } else {
3063 LOGINFO("Check_Lifetime_Writes file does not exist '%s'\n", file.c_str());
3064 }
3065 UnMount(true);
3066 } else {
3067 LOGINFO("Check_Lifetime_Writes failed to mount '%s'\n", Mount_Point.c_str());
3068 }
3069 Mount_Read_Only = original_read_only;
3070 return ret;
3071}
Ethan Yonker66a19492015-12-10 10:19:45 -06003072
3073int TWPartition::Decrypt_Adopted() {
3074#ifdef TW_INCLUDE_CRYPTO
3075 int ret = 1;
3076 Is_Adopted_Storage = false;
3077 string Adopted_Key_File = "";
3078
3079 if (!Removable)
3080 return ret;
3081
3082 int fd = open(Alternate_Block_Device.c_str(), O_RDONLY);
3083 if (fd < 0) {
3084 LOGINFO("failed to open '%s'\n", Alternate_Block_Device.c_str());
3085 return ret;
3086 }
3087 char type_guid[80];
3088 char part_guid[80];
3089
3090 if (gpt_disk_get_partition_info(fd, 2, type_guid, part_guid) == 0) {
3091 LOGINFO("type: '%s'\n", type_guid);
3092 LOGINFO("part: '%s'\n", part_guid);
3093 Adopted_GUID = part_guid;
3094 LOGINFO("Adopted_GUID '%s'\n", Adopted_GUID.c_str());
3095 if (strcmp(type_guid, TWGptAndroidExpand) == 0) {
3096 LOGINFO("android_expand found\n");
3097 Adopted_Key_File = "/data/misc/vold/expand_";
3098 Adopted_Key_File += part_guid;
3099 Adopted_Key_File += ".key";
3100 if (TWFunc::Path_Exists(Adopted_Key_File)) {
3101 Is_Adopted_Storage = true;
3102 /* Until we find a use case for this, I think it is safe
3103 * to disable USB Mass Storage whenever adopted storage
3104 * is present.
3105 */
3106 LOGINFO("Detected adopted storage, disabling USB mass storage mode\n");
3107 DataManager::SetValue("tw_has_usb_storage", 0);
3108 }
3109 }
3110 }
3111
3112 if (Is_Adopted_Storage) {
3113 string Adopted_Block_Device = Alternate_Block_Device + "p2";
3114 if (!TWFunc::Path_Exists(Adopted_Block_Device)) {
3115 Adopted_Block_Device = Alternate_Block_Device + "2";
3116 if (!TWFunc::Path_Exists(Adopted_Block_Device)) {
3117 LOGINFO("Adopted block device does not exist\n");
3118 goto exit;
3119 }
3120 }
3121 LOGINFO("key file is '%s', block device '%s'\n", Adopted_Key_File.c_str(), Adopted_Block_Device.c_str());
3122 char crypto_blkdev[MAXPATHLEN];
3123 std::string thekey;
3124 int fdkey = open(Adopted_Key_File.c_str(), O_RDONLY);
3125 if (fdkey < 0) {
3126 LOGINFO("failed to open key file\n");
3127 goto exit;
3128 }
3129 char buf[512];
3130 ssize_t n;
3131 while ((n = read(fdkey, &buf[0], sizeof(buf))) > 0) {
3132 thekey.append(buf, n);
3133 }
3134 close(fdkey);
3135 unsigned char* key = (unsigned char*) thekey.data();
3136 cryptfs_revert_ext_volume(part_guid);
3137
3138 ret = cryptfs_setup_ext_volume(part_guid, Adopted_Block_Device.c_str(), key, thekey.size(), crypto_blkdev);
3139 if (ret == 0) {
3140 LOGINFO("adopted storage new block device: '%s'\n", crypto_blkdev);
3141 Decrypted_Block_Device = crypto_blkdev;
3142 Is_Decrypted = true;
3143 Is_Encrypted = true;
3144 Find_Actual_Block_Device();
Kjell Braden3126a112016-06-19 16:58:15 +00003145 if (!Mount_Storage_Retry(false)) {
Ethan Yonker66a19492015-12-10 10:19:45 -06003146 LOGERR("Failed to mount decrypted adopted storage device\n");
3147 Is_Decrypted = false;
3148 Is_Encrypted = false;
3149 cryptfs_revert_ext_volume(part_guid);
3150 ret = 1;
3151 } else {
Ethan Yonkerfcf3f242016-02-16 12:30:26 -06003152 UnMount(false);
3153 Has_Android_Secure = false;
3154 Symlink_Path = "";
3155 Symlink_Mount_Point = "";
3156 Backup_Name = Mount_Point.substr(1);
3157 Backup_Path = Mount_Point;
3158 TWPartition* sdext = PartitionManager.Find_Partition_By_Path("/sd-ext");
3159 if (sdext && sdext->Actual_Block_Device == Adopted_Block_Device) {
3160 LOGINFO("Removing /sd-ext from partition list due to adopted storage\n");
3161 PartitionManager.Remove_Partition_By_Path("/sd-ext");
3162 }
Ethan Yonker66a19492015-12-10 10:19:45 -06003163 Setup_Data_Media();
3164 Recreate_Media_Folder();
3165 Wipe_Available_in_GUI = true;
3166 Wipe_During_Factory_Reset = true;
3167 Can_Be_Backed_Up = true;
3168 Can_Encrypt_Backup = true;
3169 Use_Userdata_Encryption = true;
3170 Is_Storage = true;
3171 Storage_Name = "Adopted Storage";
3172 Is_SubPartition = true;
3173 SubPartition_Of = "/data";
3174 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
3175 DataManager::SetValue("tw_has_adopted_storage", 1);
3176 }
3177 } else {
3178 LOGERR("Failed to setup adopted storage decryption\n");
3179 }
3180 }
3181exit:
Matt Mower06543e32017-01-06 15:25:26 -06003182 close(fd);
Ethan Yonker66a19492015-12-10 10:19:45 -06003183 return ret;
3184#else
3185 LOGINFO("Decrypt_Adopted: no crypto support\n");
3186 return 1;
3187#endif
3188}
3189
3190void TWPartition::Revert_Adopted() {
3191#ifdef TW_INCLUDE_CRYPTO
3192 if (!Adopted_GUID.empty()) {
3193 PartitionManager.Remove_MTP_Storage(Mount_Point);
3194 UnMount(false);
3195 cryptfs_revert_ext_volume(Adopted_GUID.c_str());
3196 Is_Adopted_Storage = false;
3197 Is_Encrypted = false;
3198 Is_Decrypted = false;
3199 Decrypted_Block_Device = "";
3200 Find_Actual_Block_Device();
3201 Wipe_During_Factory_Reset = false;
3202 Can_Be_Backed_Up = false;
3203 Can_Encrypt_Backup = false;
3204 Use_Userdata_Encryption = false;
3205 Is_SubPartition = false;
3206 SubPartition_Of = "";
3207 Has_Data_Media = false;
3208 Storage_Path = Mount_Point;
3209 if (!Symlink_Mount_Point.empty()) {
3210 TWPartition* Dat = PartitionManager.Find_Partition_By_Path("/data");
3211 if (Dat) {
3212 Dat->UnMount(false);
3213 Dat->Symlink_Mount_Point = Symlink_Mount_Point;
3214 }
3215 Symlink_Mount_Point = "";
3216 }
3217 }
3218#else
3219 LOGINFO("Revert_Adopted: no crypto support\n");
3220#endif
3221}
bigbiff bigbiffb5ecaad2017-03-20 18:53:53 -04003222
3223void TWPartition::Set_Backup_FileName(string fname) {
3224 Backup_FileName = fname;
3225}
3226
3227string TWPartition::Get_Backup_Name() {
3228 return Backup_Name;
3229}