blob: dfa57fafcc5adfedf0e6e4e44c8c387ccff78662 [file] [log] [blame]
Dees Troy3be70a82013-10-22 14:25:12 +00001/*
bigbiff bigbiff56cf5642016-08-19 17:43:45 -04002 Copyright 2013 to 2017 TeamWin
Dees Troy3be70a82013-10-22 14:25:12 +00003 This file is part of TWRP/TeamWin Recovery Project.
4
5 TWRP is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 TWRP is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with TWRP. If not, see <http://www.gnu.org/licenses/>.
17*/
Dees_Troy51a0e822012-09-05 15:24:24 -040018
19#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
22#include <sys/stat.h>
23#include <sys/vfs.h>
Dees_Troy5bf43922012-09-07 16:07:55 -040024#include <sys/mount.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040025#include <unistd.h>
Dees_Troy51127312012-09-08 13:08:49 -040026#include <dirent.h>
thatf54e5392016-01-29 22:04:43 +010027#include <libgen.h>
bigbiffce8f83c2015-12-12 18:30:21 -050028#include <zlib.h>
bigbiff bigbiff9c754052013-01-09 09:09:08 -050029#include <iostream>
30#include <sstream>
Ethan Yonker66a19492015-12-10 10:19:45 -060031#include <sys/param.h>
Ethan Yonker472f5062016-02-25 13:47:30 -060032#include <fcntl.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040033
Dees_Troy657c3092012-09-10 20:32:10 -040034#ifdef TW_INCLUDE_CRYPTO
35 #include "cutils/properties.h"
36#endif
37
bigbiff7b4c7a62015-01-01 19:44:14 -050038#include "libblkid/include/blkid.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040039#include "variables.h"
Dees_Troy2673cec2013-04-02 20:22:16 +000040#include "twcommon.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040041#include "partitions.hpp"
Dees_Troy5bf43922012-09-07 16:07:55 -040042#include "data.hpp"
Dees_Troy43d8b002012-09-17 16:00:01 -040043#include "twrp-functions.hpp"
bigbiff bigbiff9c754052013-01-09 09:09:08 -050044#include "twrpTar.hpp"
Ethan Yonker3fdcda42016-11-30 12:29:37 -060045#include "exclude.hpp"
Ethan Yonker1b7a31b2014-07-03 15:09:22 -050046#include "infomanager.hpp"
Ethan Yonker4b94cfd2014-12-11 10:00:45 -060047#include "set_metadata.h"
Ethan Yonker74db1572015-10-28 12:44:49 -050048#include "gui/gui.hpp"
bigbiffce8f83c2015-12-12 18:30:21 -050049#include "adbbu/libtwadbbu.hpp"
Dees_Troy5bf43922012-09-07 16:07:55 -040050extern "C" {
Dees_Troy38bd7602012-09-14 13:33:53 -040051 #include "mtdutils/mtdutils.h"
52 #include "mtdutils/mounts.h"
Dees_Troya95f55c2013-08-17 13:14:43 +000053#ifdef USE_EXT4
Ethan Yonker8373cfe2017-09-08 06:50:54 -050054 // #include "make_ext4fs.h" TODO need ifdef for android8
55 #include <ext4_utils/make_ext4fs.h>
Dees_Troya95f55c2013-08-17 13:14:43 +000056#endif
Ethan Yonker71413f42014-02-26 13:36:08 -060057
58#ifdef TW_INCLUDE_CRYPTO
Ethan Yonker253368a2014-11-25 15:00:52 -060059 #include "crypto/lollipop/cryptfs.h"
Ethan Yonker66a19492015-12-10 10:19:45 -060060 #include "gpt/gpt.h"
Ethan Yonkerbd7492d2016-12-07 13:55:01 -060061 #ifdef TW_INCLUDE_FBE
62 #include "crypto/ext4crypt/Decrypt.h"
Ethan Yonkerbd7492d2016-12-07 13:55:01 -060063 #endif
Ethan Yonkerbc85b632015-08-09 12:48:14 -050064#else
65 #define CRYPT_FOOTER_OFFSET 0x4000
Ethan Yonker71413f42014-02-26 13:36:08 -060066#endif
Dees_Troy5bf43922012-09-07 16:07:55 -040067}
Matt Mower87413642017-01-17 21:14:46 -060068#include <selinux/selinux.h>
Ethan Yonkerf27497f2014-02-09 11:48:33 -060069#include <selinux/label.h>
Dees Troy4159aed2014-02-28 17:24:43 +000070#ifdef HAVE_CAPABILITIES
71#include <sys/capability.h>
72#include <sys/xattr.h>
73#include <linux/xattr.h>
74#endif
HashBanged974bb2016-01-30 14:20:09 -050075#include <sparse_format.h>
Ethan Yonker472f5062016-02-25 13:47:30 -060076#include "progresstracking.hpp"
Dees_Troy51a0e822012-09-05 15:24:24 -040077
bigbiff bigbiff9c754052013-01-09 09:09:08 -050078using namespace std;
79
Ethan Yonker6e8c27a2016-12-22 17:55:57 -060080static int auto_index = 0; // v2 fstab allows you to specify a mount point of "auto" with no /. These items are given a mount point of /auto* where * == auto_index
81
Dees_Troya95f55c2013-08-17 13:14:43 +000082extern struct selabel_handle *selinux_handle;
Ethan Yonker6277c792014-09-15 14:54:30 -050083extern bool datamedia;
Dees_Troya95f55c2013-08-17 13:14:43 +000084
Hashcode62bd9e02013-11-19 21:59:42 -080085struct flag_list {
86 const char *name;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -060087 unsigned long flag;
Hashcode62bd9e02013-11-19 21:59:42 -080088};
89
Matt Mower4ab42b12016-04-21 13:52:18 -050090const struct flag_list mount_flags[] = {
Ethan Yonker6e8c27a2016-12-22 17:55:57 -060091 { "noatime", MS_NOATIME },
92 { "noexec", MS_NOEXEC },
93 { "nosuid", MS_NOSUID },
94 { "nodev", MS_NODEV },
95 { "nodiratime", MS_NODIRATIME },
96 { "ro", MS_RDONLY },
97 { "rw", 0 },
98 { "remount", MS_REMOUNT },
99 { "bind", MS_BIND },
100 { "rec", MS_REC },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000101#ifdef MS_UNBINDABLE
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600102 { "unbindable", MS_UNBINDABLE },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000103#endif
104#ifdef MS_PRIVATE
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600105 { "private", MS_PRIVATE },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000106#endif
107#ifdef MS_SLAVE
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600108 { "slave", MS_SLAVE },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000109#endif
110#ifdef MS_SHARED
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600111 { "shared", MS_SHARED },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000112#endif
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600113 { "sync", MS_SYNCHRONOUS },
114 { 0, 0 },
115};
116
117const char *ignored_mount_items[] = {
118 "defaults=",
119 "errors=",
120 NULL
Hashcode62bd9e02013-11-19 21:59:42 -0800121};
122
Matt Mower2416a502016-04-12 19:54:46 -0500123enum TW_FSTAB_FLAGS {
124 TWFLAG_DEFAULTS, // Retain position
125 TWFLAG_ANDSEC,
126 TWFLAG_BACKUP,
127 TWFLAG_BACKUPNAME,
128 TWFLAG_BLOCKSIZE,
129 TWFLAG_CANBEWIPED,
130 TWFLAG_CANENCRYPTBACKUP,
131 TWFLAG_DISPLAY,
132 TWFLAG_ENCRYPTABLE,
Ethan Yonkere9afc3d2018-08-30 15:16:27 -0500133 TWFLAG_FILEENCRYPTION,
Matt Mower2416a502016-04-12 19:54:46 -0500134 TWFLAG_FLASHIMG,
135 TWFLAG_FORCEENCRYPT,
136 TWFLAG_FSFLAGS,
137 TWFLAG_IGNOREBLKID,
138 TWFLAG_LENGTH,
139 TWFLAG_MOUNTTODECRYPT,
140 TWFLAG_REMOVABLE,
141 TWFLAG_RETAINLAYOUTVERSION,
142 TWFLAG_SETTINGSSTORAGE,
143 TWFLAG_STORAGE,
144 TWFLAG_STORAGENAME,
145 TWFLAG_SUBPARTITIONOF,
146 TWFLAG_SYMLINK,
147 TWFLAG_USERDATAENCRYPTBACKUP,
148 TWFLAG_USERMRF,
149 TWFLAG_WIPEDURINGFACTORYRESET,
150 TWFLAG_WIPEINGUI,
Ethan Yonker1b190162016-12-05 15:25:19 -0600151 TWFLAG_SLOTSELECT,
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600152 TWFLAG_WAIT,
153 TWFLAG_VERIFY,
154 TWFLAG_CHECK,
155 TWFLAG_ALTDEVICE,
156 TWFLAG_NOTRIM,
157 TWFLAG_VOLDMANAGED,
158 TWFLAG_FORMATTABLE,
159 TWFLAG_RESIZE,
Matt Mower2416a502016-04-12 19:54:46 -0500160};
161
162/* Flags without a trailing '=' are considered dual format flags and can be
163 * written as either 'flagname' or 'flagname=', where the character following
164 * the '=' is Y,y,1 for true and false otherwise.
165 */
166const struct flag_list tw_flags[] = {
167 { "andsec", TWFLAG_ANDSEC },
168 { "backup", TWFLAG_BACKUP },
169 { "backupname=", TWFLAG_BACKUPNAME },
170 { "blocksize=", TWFLAG_BLOCKSIZE },
171 { "canbewiped", TWFLAG_CANBEWIPED },
172 { "canencryptbackup", TWFLAG_CANENCRYPTBACKUP },
173 { "defaults", TWFLAG_DEFAULTS },
174 { "display=", TWFLAG_DISPLAY },
175 { "encryptable=", TWFLAG_ENCRYPTABLE },
Ethan Yonkere9afc3d2018-08-30 15:16:27 -0500176 { "fileencryption=", TWFLAG_FILEENCRYPTION },
Matt Mower2416a502016-04-12 19:54:46 -0500177 { "flashimg", TWFLAG_FLASHIMG },
178 { "forceencrypt=", TWFLAG_FORCEENCRYPT },
179 { "fsflags=", TWFLAG_FSFLAGS },
180 { "ignoreblkid", TWFLAG_IGNOREBLKID },
181 { "length=", TWFLAG_LENGTH },
182 { "mounttodecrypt", TWFLAG_MOUNTTODECRYPT },
183 { "removable", TWFLAG_REMOVABLE },
184 { "retainlayoutversion", TWFLAG_RETAINLAYOUTVERSION },
185 { "settingsstorage", TWFLAG_SETTINGSSTORAGE },
186 { "storage", TWFLAG_STORAGE },
187 { "storagename=", TWFLAG_STORAGENAME },
188 { "subpartitionof=", TWFLAG_SUBPARTITIONOF },
189 { "symlink=", TWFLAG_SYMLINK },
190 { "userdataencryptbackup", TWFLAG_USERDATAENCRYPTBACKUP },
191 { "usermrf", TWFLAG_USERMRF },
192 { "wipeduringfactoryreset", TWFLAG_WIPEDURINGFACTORYRESET },
193 { "wipeingui", TWFLAG_WIPEINGUI },
Ethan Yonker1b190162016-12-05 15:25:19 -0600194 { "slotselect", TWFLAG_SLOTSELECT },
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600195 { "wait", TWFLAG_WAIT },
196 { "verify", TWFLAG_VERIFY },
197 { "check", TWFLAG_CHECK },
198 { "altdevice", TWFLAG_ALTDEVICE },
199 { "notrim", TWFLAG_NOTRIM },
200 { "voldmanaged=", TWFLAG_VOLDMANAGED },
201 { "formattable", TWFLAG_FORMATTABLE },
202 { "resize", TWFLAG_RESIZE },
Matt Mower2416a502016-04-12 19:54:46 -0500203 { 0, 0 },
204};
205
that9e0593e2014-10-08 00:01:24 +0200206TWPartition::TWPartition() {
Dees_Troy51a0e822012-09-05 15:24:24 -0400207 Can_Be_Mounted = false;
208 Can_Be_Wiped = false;
Dees_Troya13d74f2013-03-24 08:54:55 -0500209 Can_Be_Backed_Up = false;
Vojtech Bocek1dc30982013-08-30 21:49:30 +0200210 Use_Rm_Rf = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400211 Wipe_During_Factory_Reset = false;
212 Wipe_Available_in_GUI = false;
213 Is_SubPartition = false;
Dees_Troy2691f9d2012-09-24 11:15:49 -0400214 Has_SubPartition = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400215 SubPartition_Of = "";
216 Symlink_Path = "";
217 Symlink_Mount_Point = "";
218 Mount_Point = "";
Dees_Troye58d5262012-09-21 12:27:57 -0400219 Backup_Path = "";
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600220 Wildcard_Block_Device = false;
221 Sysfs_Entry = "";
Dees_Troy38bd7602012-09-14 13:33:53 -0400222 Actual_Block_Device = "";
223 Primary_Block_Device = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400224 Alternate_Block_Device = "";
225 Removable = false;
226 Is_Present = false;
227 Length = 0;
228 Size = 0;
229 Used = 0;
230 Free = 0;
231 Backup_Size = 0;
232 Can_Be_Encrypted = false;
233 Is_Encrypted = false;
234 Is_Decrypted = false;
Ethan Yonkerbd7492d2016-12-07 13:55:01 -0600235 Is_FBE = false;
Ethan Yonker253368a2014-11-25 15:00:52 -0600236 Mount_To_Decrypt = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400237 Decrypted_Block_Device = "";
238 Display_Name = "";
Dees_Troya13d74f2013-03-24 08:54:55 -0500239 Backup_Display_Name = "";
240 Storage_Name = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400241 Backup_Name = "";
Dees_Troy63c8df72012-09-10 14:02:05 -0400242 Backup_FileName = "";
Dees_Troy38bd7602012-09-14 13:33:53 -0400243 MTD_Name = "";
bigbiffce8f83c2015-12-12 18:30:21 -0500244 Backup_Method = BM_NONE;
Dees_Troy83bd4832013-05-04 12:39:56 +0000245 Can_Encrypt_Backup = false;
246 Use_Userdata_Encryption = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400247 Has_Data_Media = false;
Dees_Troye58d5262012-09-21 12:27:57 -0400248 Has_Android_Secure = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400249 Is_Storage = false;
Dees_Troya13d74f2013-03-24 08:54:55 -0500250 Is_Settings_Storage = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400251 Storage_Path = "";
252 Current_File_System = "";
253 Fstab_File_System = "";
Hashcode62bd9e02013-11-19 21:59:42 -0800254 Mount_Flags = 0;
255 Mount_Options = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400256 Format_Block_Size = 0;
Dees_Troy68cab492012-12-12 19:29:35 +0000257 Ignore_Blkid = false;
Dees_Troy16c2b312013-01-15 16:51:18 +0000258 Retain_Layout_Version = false;
Ethan Yonker253368a2014-11-25 15:00:52 -0600259 Crypto_Key_Location = "footer";
Ethan Yonker726a0202014-12-16 20:01:38 -0600260 MTP_Storage_ID = 0;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600261 Can_Flash_Img = false;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500262 Mount_Read_Only = false;
Ethan Yonker66a19492015-12-10 10:19:45 -0600263 Is_Adopted_Storage = false;
264 Adopted_GUID = "";
Ethan Yonker1b190162016-12-05 15:25:19 -0600265 SlotSelect = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400266}
267
268TWPartition::~TWPartition(void) {
269 // Do nothing
270}
271
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600272bool TWPartition::Process_Fstab_Line(const char *fstab_line, bool Display_Error, std::map<string, Flags_Map> *twrp_flags) {
Matt Mower2b2dd152016-04-26 11:24:08 -0500273 char full_line[MAX_FSTAB_LINE_LENGTH];
Matt Mower2416a502016-04-12 19:54:46 -0500274 char twflags[MAX_FSTAB_LINE_LENGTH] = "";
Dees_Troy5bf43922012-09-07 16:07:55 -0400275 char* ptr;
Matt Mower2b2dd152016-04-26 11:24:08 -0500276 int line_len = strlen(fstab_line), index = 0, item_index = 0;
Dees_Troya13d74f2013-03-24 08:54:55 -0500277 bool skip = false;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600278 int fstab_version = 1, mount_point_index = 0, fs_index = 1, block_device_index = 2;
279 TWPartition *additional_entry = NULL;
280 std::map<string, Flags_Map>::iterator it;
Dees_Troy5bf43922012-09-07 16:07:55 -0400281
Matt Mower2b2dd152016-04-26 11:24:08 -0500282 strlcpy(full_line, fstab_line, sizeof(full_line));
Dees_Troy51127312012-09-08 13:08:49 -0400283 for (index = 0; index < line_len; index++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500284 if (full_line[index] == 34)
285 skip = !skip;
286 if (!skip && full_line[index] <= 32)
Dees_Troy5bf43922012-09-07 16:07:55 -0400287 full_line[index] = '\0';
Dees_Troy5bf43922012-09-07 16:07:55 -0400288 }
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600289 if (line_len < 10)
290 return false; // There can't possibly be a valid fstab line that is less than 10 chars
291 if (strncmp(fstab_line, "/dev/", strlen("/dev/")) == 0 || strncmp(fstab_line, "/devices/", strlen("/devices/")) == 0) {
292 fstab_version = 2;
293 block_device_index = 0;
294 mount_point_index = 1;
295 fs_index = 2;
296 }
297
298 index = 0;
Dees_Troy5bf43922012-09-07 16:07:55 -0400299 while (index < line_len) {
300 while (index < line_len && full_line[index] == '\0')
301 index++;
302 if (index >= line_len)
303 continue;
304 ptr = full_line + index;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600305 if (item_index == mount_point_index) {
306 Mount_Point = ptr;
307 if (fstab_version == 2) {
308 additional_entry = PartitionManager.Find_Partition_By_Path(Mount_Point);
309 if (additional_entry) {
310 LOGINFO("Found an additional entry for '%s'\n", Mount_Point.c_str());
311 }
312 }
313 LOGINFO("Processing '%s'\n", Mount_Point.c_str());
314 Backup_Path = Mount_Point;
315 Storage_Path = Mount_Point;
316 Display_Name = ptr + 1;
317 Backup_Display_Name = Display_Name;
318 Storage_Name = Display_Name;
319 item_index++;
320 } else if (item_index == fs_index) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400321 // File System
322 Fstab_File_System = ptr;
323 Current_File_System = ptr;
324 item_index++;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600325 } else if (item_index == block_device_index) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400326 // Primary Block Device
Dees_Troy38bd7602012-09-14 13:33:53 -0400327 if (Fstab_File_System == "mtd" || Fstab_File_System == "yaffs2") {
Dees_Troy094207a2012-09-26 12:00:39 -0400328 MTD_Name = ptr;
329 Find_MTD_Block_Device(MTD_Name);
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400330 } else if (Fstab_File_System == "bml") {
331 if (Mount_Point == "/boot")
332 MTD_Name = "boot";
333 else if (Mount_Point == "/recovery")
334 MTD_Name = "recovery";
335 Primary_Block_Device = ptr;
336 if (*ptr != '/')
Dees_Troy2673cec2013-04-02 20:22:16 +0000337 LOGERR("Until we get better BML support, you will have to find and provide the full block device path to the BML devices e.g. /dev/block/bml9 instead of the partition name\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400338 } else if (*ptr != '/') {
Dees_Troy5bf43922012-09-07 16:07:55 -0400339 if (Display_Error)
Matt Mower2b2dd152016-04-26 11:24:08 -0500340 LOGERR("Invalid block device '%s' in fstab line '%s'", ptr, fstab_line);
Dees_Troy5bf43922012-09-07 16:07:55 -0400341 else
Matt Mower2b2dd152016-04-26 11:24:08 -0500342 LOGINFO("Invalid block device '%s' in fstab line '%s'", ptr, fstab_line);
Matt Mower72c87ce2016-04-26 14:34:56 -0500343 return false;
Dees_Troy38bd7602012-09-14 13:33:53 -0400344 } else {
345 Primary_Block_Device = ptr;
346 Find_Real_Block_Device(Primary_Block_Device, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400347 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400348 item_index++;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600349 } else if (item_index > 2) {
350 if (fstab_version == 2) {
351 if (item_index == 3) {
352 Process_FS_Flags(ptr);
353 if (additional_entry) {
354 additional_entry->Save_FS_Flags(Fstab_File_System, Mount_Flags, Mount_Options);
355 return false; // We save the extra fs flags in the other partition entry and by returning false, this entry will be deleted
356 }
357 } else {
358 strlcpy(twflags, ptr, sizeof(twflags));
359 }
360 item_index++;
361 } else if (*ptr == '/') { // v2 fstab does not allow alternate block devices
Dees_Troy5bf43922012-09-07 16:07:55 -0400362 // Alternate Block Device
363 Alternate_Block_Device = ptr;
364 Find_Real_Block_Device(Alternate_Block_Device, Display_Error);
365 } else if (strlen(ptr) > 7 && strncmp(ptr, "length=", 7) == 0) {
366 // Partition length
367 ptr += 7;
368 Length = atoi(ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400369 } else if (strlen(ptr) > 6 && strncmp(ptr, "flags=", 6) == 0) {
370 // Custom flags, save for later so that new values aren't overwritten by defaults
371 ptr += 6;
Matt Mower2416a502016-04-12 19:54:46 -0500372 strlcpy(twflags, ptr, sizeof(twflags));
Dees_Troy38bd7602012-09-14 13:33:53 -0400373 } else if (strlen(ptr) == 4 && (strncmp(ptr, "NULL", 4) == 0 || strncmp(ptr, "null", 4) == 0 || strncmp(ptr, "null", 4) == 0)) {
374 // Do nothing
Dees_Troy5bf43922012-09-07 16:07:55 -0400375 } else {
376 // Unhandled data
Matt Mower2b2dd152016-04-26 11:24:08 -0500377 LOGINFO("Unhandled fstab information '%s' in fstab line '%s'\n", ptr, fstab_line);
Dees_Troy5bf43922012-09-07 16:07:55 -0400378 }
379 }
380 while (index < line_len && full_line[index] != '\0')
381 index++;
382 }
383
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600384 // override block devices from the v2 fstab with the ones we read from the twrp.flags file in case they are different
385 if (fstab_version == 2 && twrp_flags && twrp_flags->size() > 0) {
386 it = twrp_flags->find(Mount_Point);
387 if (it != twrp_flags->end()) {
388 if (!it->second.Primary_Block_Device.empty()) {
389 Primary_Block_Device = it->second.Primary_Block_Device;
390 Find_Real_Block_Device(Primary_Block_Device, Display_Error);
391 }
392 if (!it->second.Alternate_Block_Device.empty()) {
393 Alternate_Block_Device = it->second.Alternate_Block_Device;
394 Find_Real_Block_Device(Alternate_Block_Device, Display_Error);
395 }
396 }
397 }
398
399 if (strncmp(fstab_line, "/devices/", strlen("/devices/")) == 0) {
400 Sysfs_Entry = Primary_Block_Device;
401 Primary_Block_Device = "";
402 Is_Storage = true;
403 Removable = true;
404 Wipe_Available_in_GUI = true;
405 Wildcard_Block_Device = true;
406 }
407 if (Primary_Block_Device.find("*") != string::npos)
408 Wildcard_Block_Device = true;
409
410 if (Mount_Point == "auto") {
411 Mount_Point = "/auto";
412 char autoi[5];
413 sprintf(autoi, "%i", auto_index);
414 Mount_Point += autoi;
415 Backup_Path = Mount_Point;
416 Storage_Path = Mount_Point;
417 auto_index++;
418 Setup_File_System(Display_Error);
419 Display_Name = "Storage";
420 Backup_Display_Name = Display_Name;
421 Storage_Name = Display_Name;
422 Can_Be_Backed_Up = false;
423 Wipe_Available_in_GUI = true;
424 Is_Storage = true;
425 Removable = true;
426 Wipe_Available_in_GUI = true;
427 } else if (!Is_File_System(Fstab_File_System) && !Is_Image(Fstab_File_System)) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400428 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000429 LOGERR("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400430 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000431 LOGINFO("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Matt Mower72c87ce2016-04-26 14:34:56 -0500432 return false;
Dees_Troy5bf43922012-09-07 16:07:55 -0400433 } else if (Is_File_System(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400434 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400435 Setup_File_System(Display_Error);
436 if (Mount_Point == "/system") {
437 Display_Name = "System";
Dees_Troya13d74f2013-03-24 08:54:55 -0500438 Backup_Display_Name = Display_Name;
439 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400440 Wipe_Available_in_GUI = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500441 Can_Be_Backed_Up = true;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500442 Mount_Read_Only = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400443 } else if (Mount_Point == "/data") {
444 Display_Name = "Data";
Dees_Troya13d74f2013-03-24 08:54:55 -0500445 Backup_Display_Name = Display_Name;
446 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400447 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400448 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500449 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000450 Can_Encrypt_Backup = true;
451 Use_Userdata_Encryption = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400452 } else if (Mount_Point == "/cache") {
453 Display_Name = "Cache";
Dees_Troya13d74f2013-03-24 08:54:55 -0500454 Backup_Display_Name = Display_Name;
455 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400456 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400457 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500458 Can_Be_Backed_Up = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400459 } else if (Mount_Point == "/datadata") {
Dees_Troy51127312012-09-08 13:08:49 -0400460 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400461 Display_Name = "DataData";
Dees_Troya13d74f2013-03-24 08:54:55 -0500462 Backup_Display_Name = Display_Name;
463 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400464 Is_SubPartition = true;
465 SubPartition_Of = "/data";
Dees_Troy5bf43922012-09-07 16:07:55 -0400466 DataManager::SetValue(TW_HAS_DATADATA, 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500467 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000468 Can_Encrypt_Backup = true;
469 Use_Userdata_Encryption = false; // This whole partition should be encrypted
Dees_Troy5bf43922012-09-07 16:07:55 -0400470 } else if (Mount_Point == "/sd-ext") {
Dees_Troy51127312012-09-08 13:08:49 -0400471 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400472 Display_Name = "SD-Ext";
Dees_Troya13d74f2013-03-24 08:54:55 -0500473 Backup_Display_Name = Display_Name;
474 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400475 Wipe_Available_in_GUI = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400476 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500477 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000478 Can_Encrypt_Backup = true;
479 Use_Userdata_Encryption = true;
Dees_Troy2c50e182012-09-26 20:05:28 -0400480 } else if (Mount_Point == "/boot") {
481 Display_Name = "Boot";
Dees_Troya13d74f2013-03-24 08:54:55 -0500482 Backup_Display_Name = Display_Name;
Dees_Troy2c50e182012-09-26 20:05:28 -0400483 DataManager::SetValue("tw_boot_is_mountable", 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500484 Can_Be_Backed_Up = true;
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500485 } else if (Mount_Point == "/vendor") {
486 Display_Name = "Vendor";
487 Backup_Display_Name = Display_Name;
488 Storage_Name = Display_Name;
489 Mount_Read_Only = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400490 }
491#ifdef TW_EXTERNAL_STORAGE_PATH
492 if (Mount_Point == EXPAND(TW_EXTERNAL_STORAGE_PATH)) {
493 Is_Storage = true;
494 Storage_Path = EXPAND(TW_EXTERNAL_STORAGE_PATH);
Dees_Troyc51f1f92012-09-20 15:32:13 -0400495 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500496 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400497#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000498 if (Mount_Point == "/sdcard" || Mount_Point == "/external_sd" || Mount_Point == "/external_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400499 Is_Storage = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400500 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500501 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400502#endif
Dees_Troyb05ddee2013-01-28 20:24:50 +0000503 }
Dees_Troy8170a922012-09-18 15:40:25 -0400504#ifdef TW_INTERNAL_STORAGE_PATH
505 if (Mount_Point == EXPAND(TW_INTERNAL_STORAGE_PATH)) {
506 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500507 Is_Settings_Storage = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400508 Storage_Path = EXPAND(TW_INTERNAL_STORAGE_PATH);
Dees_Troya13d74f2013-03-24 08:54:55 -0500509 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400510 }
511#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000512 if (Mount_Point == "/emmc" || Mount_Point == "/internal_sd" || Mount_Point == "/internal_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400513 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500514 Is_Settings_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500515 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400516 }
517#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400518 } else if (Is_Image(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400519 Find_Actual_Block_Device();
Ethan Yonker1b190162016-12-05 15:25:19 -0600520 Setup_Image();
Dees_Troya13d74f2013-03-24 08:54:55 -0500521 if (Mount_Point == "/boot") {
522 Display_Name = "Boot";
523 Backup_Display_Name = Display_Name;
524 Can_Be_Backed_Up = true;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600525 Can_Flash_Img = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500526 } else if (Mount_Point == "/recovery") {
527 Display_Name = "Recovery";
528 Backup_Display_Name = Display_Name;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600529 Can_Flash_Img = true;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500530 } else if (Mount_Point == "/system_image") {
531 Display_Name = "System Image";
532 Backup_Display_Name = Display_Name;
HashBanged974bb2016-01-30 14:20:09 -0500533 Can_Flash_Img = true;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500534 Can_Be_Backed_Up = true;
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500535 } else if (Mount_Point == "/vendor_image") {
536 Display_Name = "Vendor Image";
537 Backup_Display_Name = Display_Name;
HashBanged974bb2016-01-30 14:20:09 -0500538 Can_Flash_Img = true;
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500539 Can_Be_Backed_Up = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500540 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400541 }
542
Matt Mower2416a502016-04-12 19:54:46 -0500543 // Process TWRP fstab flags
544 if (strlen(twflags) > 0) {
545 string Prev_Display_Name = Display_Name;
546 string Prev_Storage_Name = Storage_Name;
547 string Prev_Backup_Display_Name = Backup_Display_Name;
548 Display_Name = "";
549 Storage_Name = "";
550 Backup_Display_Name = "";
551
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600552 Process_TW_Flags(twflags, (fstab_version == 1), fstab_version);
553 Save_FS_Flags(Fstab_File_System, Mount_Flags, Mount_Options);
Matt Mower2416a502016-04-12 19:54:46 -0500554
555 bool has_display_name = !Display_Name.empty();
556 bool has_storage_name = !Storage_Name.empty();
557 bool has_backup_name = !Backup_Display_Name.empty();
558 if (!has_display_name) Display_Name = Prev_Display_Name;
559 if (!has_storage_name) Storage_Name = Prev_Storage_Name;
560 if (!has_backup_name) Backup_Display_Name = Prev_Backup_Display_Name;
561
562 if (has_display_name && !has_storage_name)
563 Storage_Name = Display_Name;
564 if (!has_display_name && has_storage_name)
565 Display_Name = Storage_Name;
566 if (has_display_name && !has_backup_name && Backup_Display_Name != "Android Secure")
567 Backup_Display_Name = Display_Name;
568 if (!has_display_name && has_backup_name)
569 Display_Name = Backup_Display_Name;
570 }
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600571
572 if (fstab_version == 2 && twrp_flags && twrp_flags->size() > 0) {
573 it = twrp_flags->find(Mount_Point);
574 if (it != twrp_flags->end()) {
575 char twrpflags[MAX_FSTAB_LINE_LENGTH] = "";
576 int skip = 0;
577 string Flags = it->second.Flags;
578 strcpy(twrpflags, Flags.c_str());
579 if (strlen(twrpflags) > strlen("flags=") && strncmp(twrpflags, "flags=", strlen("flags=")) == 0)
580 skip += strlen("flags=");
581 char* flagptr = twrpflags;
582 flagptr += skip;
583 Process_TW_Flags(flagptr, Display_Error, 1); // Forcing the fstab to ver 1 because this data is coming from the /etc/twrp.flags which should be using the TWRP v1 flags format
584 }
585 }
nkk7198fc3992017-12-16 16:26:42 +0200586
587 if (Mount_Point == "/persist" && Can_Be_Mounted) {
588 bool mounted = Is_Mounted();
589 if (mounted || Mount(false)) {
590 // Read the backup settings file
591 DataManager::LoadPersistValues();
592 TWFunc::Fixup_Time_On_Boot("/persist/time/");
593 if (!mounted)
594 UnMount(false);
595 }
596 }
597
Dees_Troy51127312012-09-08 13:08:49 -0400598 return true;
599}
600
Matt Mower72c87ce2016-04-26 14:34:56 -0500601void TWPartition::Partition_Post_Processing(bool Display_Error) {
602 if (Mount_Point == "/data")
603 Setup_Data_Partition(Display_Error);
604 else if (Mount_Point == "/cache")
605 Setup_Cache_Partition(Display_Error);
606}
607
Ethan Yonkerbd7492d2016-12-07 13:55:01 -0600608void TWPartition::ExcludeAll(const string& path) {
609 backup_exclusions.add_absolute_dir(path);
610 wipe_exclusions.add_absolute_dir(path);
611}
612
Matt Mower72c87ce2016-04-26 14:34:56 -0500613void TWPartition::Setup_Data_Partition(bool Display_Error) {
614 if (Mount_Point != "/data")
615 return;
616
617 // Ensure /data is not mounted as tmpfs for qcom hardware decrypt
618 UnMount(false);
619
620#ifdef TW_INCLUDE_CRYPTO
621 if (datamedia)
622 Setup_Data_Media();
623 Can_Be_Encrypted = true;
624 char crypto_blkdev[255];
625 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
626 if (strcmp(crypto_blkdev, "error") != 0) {
627 DataManager::SetValue(TW_IS_DECRYPTED, 1);
628 Is_Encrypted = true;
629 Is_Decrypted = true;
Ethan Yonkerbd7492d2016-12-07 13:55:01 -0600630 Is_FBE = false;
631 DataManager::SetValue(TW_IS_FBE, 0);
Matt Mower72c87ce2016-04-26 14:34:56 -0500632 Decrypted_Block_Device = crypto_blkdev;
633 LOGINFO("Data already decrypted, new block device: '%s'\n", crypto_blkdev);
634 } else if (!Mount(false)) {
635 if (Is_Present) {
636 set_partition_data(Actual_Block_Device.c_str(), Crypto_Key_Location.c_str(), Fstab_File_System.c_str());
637 if (cryptfs_check_footer() == 0) {
638 Is_Encrypted = true;
639 Is_Decrypted = false;
640 Can_Be_Mounted = false;
641 Current_File_System = "emmc";
Ethan Yonker1b190162016-12-05 15:25:19 -0600642 Setup_Image();
Matt Mower72c87ce2016-04-26 14:34:56 -0500643 DataManager::SetValue(TW_IS_ENCRYPTED, 1);
644 DataManager::SetValue(TW_CRYPTO_PWTYPE, cryptfs_get_password_type());
645 DataManager::SetValue(TW_CRYPTO_PASSWORD, "");
646 DataManager::SetValue("tw_crypto_display", "");
647 } else {
648 gui_err("mount_data_footer=Could not mount /data and unable to find crypto footer.");
649 }
650 } else {
651 LOGERR("Primary block device '%s' for mount point '%s' is not present!\n", Primary_Block_Device.c_str(), Mount_Point.c_str());
652 }
653 } else {
Ethan Yonkerbd7492d2016-12-07 13:55:01 -0600654 if (TWFunc::Path_Exists("/data/unencrypted/key/version")) {
655 LOGINFO("File Based Encryption is present\n");
656#ifdef TW_INCLUDE_FBE
657 ExcludeAll(Mount_Point + "/convert_fbe");
658 ExcludeAll(Mount_Point + "/unencrypted");
659 //ExcludeAll(Mount_Point + "/system/users/0"); // we WILL need to retain some of this if multiple users are present or we just need to delete more folders for the extra users somewhere else
660 ExcludeAll(Mount_Point + "/misc/vold/user_keys");
Ethan Yonker79f88bd2016-12-09 14:52:12 -0600661 //ExcludeAll(Mount_Point + "/system_ce");
662 //ExcludeAll(Mount_Point + "/system_de");
663 //ExcludeAll(Mount_Point + "/misc_ce");
664 //ExcludeAll(Mount_Point + "/misc_de");
Ethan Yonkerbd7492d2016-12-07 13:55:01 -0600665 ExcludeAll(Mount_Point + "/system/gatekeeper.password.key");
666 ExcludeAll(Mount_Point + "/system/gatekeeper.pattern.key");
667 ExcludeAll(Mount_Point + "/system/locksettings.db");
668 //ExcludeAll(Mount_Point + "/system/locksettings.db-shm"); // don't seem to need this one, but the other 2 are needed
669 ExcludeAll(Mount_Point + "/system/locksettings.db-wal");
Ethan Yonker79f88bd2016-12-09 14:52:12 -0600670 //ExcludeAll(Mount_Point + "/user_de");
Ethan Yonkerbd7492d2016-12-07 13:55:01 -0600671 //ExcludeAll(Mount_Point + "/misc/profiles/cur/0"); // might be important later
672 ExcludeAll(Mount_Point + "/misc/gatekeeper");
Ethan Yonkerfefe5912017-09-30 22:22:13 -0500673 ExcludeAll(Mount_Point + "/misc/keystore");
Ethan Yonkerbd7492d2016-12-07 13:55:01 -0600674 ExcludeAll(Mount_Point + "/drm/kek.dat");
Ethan Yonkerfefe5912017-09-30 22:22:13 -0500675 ExcludeAll(Mount_Point + "/system_de/0/spblob"); // contains data needed to decrypt pixel 2
Ethan Yonkerbd7492d2016-12-07 13:55:01 -0600676 int retry_count = 3;
677 while (!Decrypt_DE() && --retry_count)
678 usleep(2000);
679 if (retry_count > 0) {
680 property_set("ro.crypto.state", "encrypted");
681 Is_Encrypted = true;
682 Is_Decrypted = false;
683 Is_FBE = true;
684 DataManager::SetValue(TW_IS_FBE, 1);
685 DataManager::SetValue(TW_IS_ENCRYPTED, 1);
686 string filename;
Ethan Yonkerfefe5912017-09-30 22:22:13 -0500687 int pwd_type = Get_Password_Type(0, filename);
688 if (pwd_type < 0) {
689 LOGERR("This TWRP does not have synthetic password decrypt support\n");
690 pwd_type = 0; // default password
691 }
692 DataManager::SetValue(TW_CRYPTO_PWTYPE, pwd_type);
Ethan Yonkerbd7492d2016-12-07 13:55:01 -0600693 DataManager::SetValue(TW_CRYPTO_PASSWORD, "");
694 DataManager::SetValue("tw_crypto_display", "");
695 }
696#else
697 LOGERR("FBE found but FBE support not present in TWRP\n");
698#endif
699 } else {
700 // Filesystem is not encrypted and the mount succeeded, so return to
701 // the original unmounted state
702 UnMount(false);
703 }
Matt Mower72c87ce2016-04-26 14:34:56 -0500704 }
705 if (datamedia && (!Is_Encrypted || (Is_Encrypted && Is_Decrypted))) {
706 Setup_Data_Media();
707 Recreate_Media_Folder();
708 }
709#else
710 if (datamedia) {
711 Setup_Data_Media();
712 Recreate_Media_Folder();
713 }
714#endif
715}
716
717void TWPartition::Setup_Cache_Partition(bool Display_Error __unused) {
718 if (Mount_Point != "/cache")
719 return;
720
721 if (!Mount(true))
722 return;
723
724 if (!TWFunc::Path_Exists("/cache/recovery/.")) {
725 LOGINFO("Recreating /cache/recovery folder\n");
726 if (mkdir("/cache/recovery", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0)
727 LOGERR("Could not create /cache/recovery\n");
728 }
729}
730
Matt Mower4ab42b12016-04-21 13:52:18 -0500731void TWPartition::Process_FS_Flags(const char *str) {
732 char *options = strdup(str);
733 char *ptr, *savep;
Hashcode62bd9e02013-11-19 21:59:42 -0800734
Matt Mower4ab42b12016-04-21 13:52:18 -0500735 Mount_Options = "";
Hashcode62bd9e02013-11-19 21:59:42 -0800736
Matt Mower4ab42b12016-04-21 13:52:18 -0500737 // Avoid issues with potentially nested strtok by using strtok_r
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600738 for (ptr = strtok_r(options, ",", &savep); ptr; ptr = strtok_r(NULL, ",", &savep)) {
739 char *equals = strstr(ptr, "=");
740 size_t name_len;
Matt Mower4ab42b12016-04-21 13:52:18 -0500741
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600742 if (!equals)
743 name_len = strlen(ptr);
744 else
745 name_len = equals - ptr;
746
747 // There are some flags that we want to ignore in TWRP
748 bool found_match = false;
749 for (const char** ignored_mount_item = ignored_mount_items; *ignored_mount_item; ignored_mount_item++) {
750 if (strncmp(ptr, *ignored_mount_item, name_len) == 0) {
751 found_match = true;
Hashcode62bd9e02013-11-19 21:59:42 -0800752 break;
753 }
754 }
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600755 if (found_match)
756 continue;
Hashcode62bd9e02013-11-19 21:59:42 -0800757
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600758 // mount_flags are never postfixed by '='
759 if (!equals) {
760 const struct flag_list* mount_flag = mount_flags;
761 for (; mount_flag->name; mount_flag++) {
762 if (strcmp(ptr, mount_flag->name) == 0) {
763 if (mount_flag->flag == MS_RDONLY)
764 Mount_Read_Only = true;
765 else
766 Mount_Flags |= (unsigned)mount_flag->flag;
767 found_match = true;
768 break;
769 }
770 }
771 if (found_match)
772 continue;
Matt Mower4ab42b12016-04-21 13:52:18 -0500773 }
774
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600775 // If we aren't ignoring this flag and it's not a mount flag, then it must be a mount option
776 if (!Mount_Options.empty())
777 Mount_Options += ",";
778 Mount_Options += ptr;
Matt Mower4ab42b12016-04-21 13:52:18 -0500779 }
780 free(options);
Hashcode62bd9e02013-11-19 21:59:42 -0800781}
782
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600783void TWPartition::Save_FS_Flags(const string& local_File_System, int local_Mount_Flags, const string& local_Mount_Options) {
784 partition_fs_flags_struct flags;
785 flags.File_System = local_File_System;
786 flags.Mount_Flags = local_Mount_Flags;
787 flags.Mount_Options = local_Mount_Options;
788 fs_flags.push_back(flags);
789}
790
Matt Mower2416a502016-04-12 19:54:46 -0500791void TWPartition::Apply_TW_Flag(const unsigned flag, const char* str, const bool val) {
792 switch (flag) {
793 case TWFLAG_ANDSEC:
794 Has_Android_Secure = val;
795 break;
796 case TWFLAG_BACKUP:
797 Can_Be_Backed_Up = val;
798 break;
799 case TWFLAG_BACKUPNAME:
800 Backup_Display_Name = str;
801 break;
802 case TWFLAG_BLOCKSIZE:
803 Format_Block_Size = (unsigned long)(atol(str));
804 break;
805 case TWFLAG_CANBEWIPED:
806 Can_Be_Wiped = val;
807 break;
808 case TWFLAG_CANENCRYPTBACKUP:
809 Can_Encrypt_Backup = val;
810 break;
811 case TWFLAG_DEFAULTS:
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600812 case TWFLAG_WAIT:
813 case TWFLAG_VERIFY:
814 case TWFLAG_CHECK:
815 case TWFLAG_NOTRIM:
816 case TWFLAG_VOLDMANAGED:
817 case TWFLAG_RESIZE:
Matt Mower2416a502016-04-12 19:54:46 -0500818 // Do nothing
819 break;
820 case TWFLAG_DISPLAY:
821 Display_Name = str;
822 break;
823 case TWFLAG_ENCRYPTABLE:
824 case TWFLAG_FORCEENCRYPT:
825 Crypto_Key_Location = str;
826 break;
Ethan Yonkere9afc3d2018-08-30 15:16:27 -0500827 case TWFLAG_FILEENCRYPTION:
828 // This flag isn't used by TWRP but is needed in 9.0 FBE decrypt
829 // fileencryption=ice:aes-256-heh
830 {
831 std::string FBE = str;
832 std::string FBE_contents, FBE_filenames;
833 size_t colon_loc = FBE.find(":");
834 if (colon_loc == std::string::npos) {
835 LOGINFO("Invalid fileencryption fstab flag: '%s'\n", str);
836 break;
837 }
838 FBE_contents = FBE.substr(0, colon_loc);
839 FBE_filenames = FBE.substr(colon_loc + 1);
840 property_set("fbe.contents", FBE_contents.c_str());
841 property_set("fbe.filenames", FBE_filenames.c_str());
842 LOGINFO("FBE contents '%s', filenames '%s'\n", FBE_contents.c_str(), FBE_filenames.c_str());
843 }
844 break;
Matt Mower2416a502016-04-12 19:54:46 -0500845 case TWFLAG_FLASHIMG:
846 Can_Flash_Img = val;
847 break;
848 case TWFLAG_FSFLAGS:
Matt Mower4ab42b12016-04-21 13:52:18 -0500849 Process_FS_Flags(str);
Matt Mower2416a502016-04-12 19:54:46 -0500850 break;
851 case TWFLAG_IGNOREBLKID:
852 Ignore_Blkid = val;
853 break;
854 case TWFLAG_LENGTH:
855 Length = atoi(str);
856 break;
857 case TWFLAG_MOUNTTODECRYPT:
858 Mount_To_Decrypt = val;
859 break;
860 case TWFLAG_REMOVABLE:
861 Removable = val;
862 break;
863 case TWFLAG_RETAINLAYOUTVERSION:
864 Retain_Layout_Version = val;
865 break;
866 case TWFLAG_SETTINGSSTORAGE:
867 Is_Settings_Storage = val;
868 if (Is_Settings_Storage)
869 Is_Storage = true;
870 break;
871 case TWFLAG_STORAGE:
872 Is_Storage = val;
873 break;
874 case TWFLAG_STORAGENAME:
875 Storage_Name = str;
876 break;
877 case TWFLAG_SUBPARTITIONOF:
878 Is_SubPartition = true;
879 SubPartition_Of = str;
880 break;
881 case TWFLAG_SYMLINK:
882 Symlink_Path = str;
883 break;
884 case TWFLAG_USERDATAENCRYPTBACKUP:
885 Use_Userdata_Encryption = val;
886 if (Use_Userdata_Encryption)
887 Can_Encrypt_Backup = true;
888 break;
889 case TWFLAG_USERMRF:
890 Use_Rm_Rf = val;
891 break;
892 case TWFLAG_WIPEDURINGFACTORYRESET:
893 Wipe_During_Factory_Reset = val;
894 if (Wipe_During_Factory_Reset) {
895 Can_Be_Wiped = true;
896 Wipe_Available_in_GUI = true;
897 }
898 break;
899 case TWFLAG_WIPEINGUI:
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600900 case TWFLAG_FORMATTABLE:
Matt Mower2416a502016-04-12 19:54:46 -0500901 Wipe_Available_in_GUI = val;
902 if (Wipe_Available_in_GUI)
903 Can_Be_Wiped = true;
904 break;
Ethan Yonker1b190162016-12-05 15:25:19 -0600905 case TWFLAG_SLOTSELECT:
906 SlotSelect = true;
907 break;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600908 case TWFLAG_ALTDEVICE:
909 Alternate_Block_Device = str;
910 break;
Matt Mower2416a502016-04-12 19:54:46 -0500911 default:
912 // Should not get here
913 LOGINFO("Flag identified for processing, but later unmatched: %i\n", flag);
914 break;
915 }
916}
Dees_Troy51127312012-09-08 13:08:49 -0400917
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600918void TWPartition::Process_TW_Flags(char *flags, bool Display_Error, int fstab_ver) {
Matt Mower2416a502016-04-12 19:54:46 -0500919 char separator[2] = {'\n', 0};
920 char *ptr, *savep;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600921 char source_separator = ';';
922
923 if (fstab_ver == 2)
924 source_separator = ',';
Matt Mower2416a502016-04-12 19:54:46 -0500925
926 // Semicolons within double-quotes are not forbidden, so replace
927 // only the semicolons intended as separators with '\n' for strtok
928 for (unsigned i = 0, skip = 0; i < strlen(flags); i++) {
929 if (flags[i] == '\"')
Dees_Troya13d74f2013-03-24 08:54:55 -0500930 skip = !skip;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600931 if (!skip && flags[i] == source_separator)
Matt Mower2416a502016-04-12 19:54:46 -0500932 flags[i] = separator[0];
Dees_Troy51127312012-09-08 13:08:49 -0400933 }
934
Matt Mower2416a502016-04-12 19:54:46 -0500935 // Avoid issues with potentially nested strtok by using strtok_r
936 ptr = strtok_r(flags, separator, &savep);
937 while (ptr) {
938 int ptr_len = strlen(ptr);
939 const struct flag_list* tw_flag = tw_flags;
Hashcode62bd9e02013-11-19 21:59:42 -0800940
Matt Mower2416a502016-04-12 19:54:46 -0500941 for (; tw_flag->name; tw_flag++) {
942 int flag_len = strlen(tw_flag->name);
943
944 if (strncmp(ptr, tw_flag->name, flag_len) == 0) {
945 bool flag_val = false;
946
947 if (ptr_len > flag_len && (tw_flag->name)[flag_len-1] != '='
948 && ptr[flag_len] != '=') {
949 // Handle flags with same starting string
950 // (e.g. backup and backupname)
951 continue;
952 } else if (ptr_len > flag_len && ptr[flag_len] == '=') {
953 // Handle flags with dual format: Part 1
954 // (e.g. backup and backup=y. backup=y handled here)
955 ptr += flag_len + 1;
956 TWFunc::Strip_Quotes(ptr);
957 // Skip flags with empty argument
958 // (e.g. backup=)
959 if (strlen(ptr) == 0) {
960 LOGINFO("Flag missing argument or should not include '=': %s=\n", tw_flag->name);
961 break;
962 }
963 flag_val = strchr("yY1", *ptr) != NULL;
964 } else if (ptr_len == flag_len
965 && (tw_flag->name)[flag_len-1] == '=') {
966 // Skip flags missing argument after =
967 // (e.g. backupname=)
968 LOGINFO("Flag missing argument: %s\n", tw_flag->name);
969 break;
970 } else if (ptr_len > flag_len
971 && (tw_flag->name)[flag_len-1] == '=') {
972 // Handle arguments to flags
973 // (e.g. backupname="My Stuff")
974 ptr += flag_len;
975 TWFunc::Strip_Quotes(ptr);
976 // Skip flags with empty argument
977 // (e.g. backupname="")
978 if (strlen(ptr) == 0) {
979 LOGINFO("Flag missing argument: %s\n", tw_flag->name);
980 break;
981 }
982 } else if (ptr_len == flag_len) {
983 // Handle flags with dual format: Part 2
984 // (e.g. backup and backup=y. backup handled here)
985 flag_val = true;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600986 } else {
Matt Mower2416a502016-04-12 19:54:46 -0500987 LOGINFO("Flag matched, but could not be processed: %s\n", ptr);
988 break;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600989 }
Matt Mower2416a502016-04-12 19:54:46 -0500990
991 Apply_TW_Flag(tw_flag->flag, ptr, flag_val);
992 break;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600993 }
Matt Mower2416a502016-04-12 19:54:46 -0500994 }
995 if (tw_flag->name == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400996 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000997 LOGERR("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400998 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000999 LOGINFO("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -04001000 }
Matt Mower2416a502016-04-12 19:54:46 -05001001 ptr = strtok_r(NULL, separator, &savep);
Dees_Troy51127312012-09-08 13:08:49 -04001002 }
Dees_Troy51a0e822012-09-05 15:24:24 -04001003}
1004
Dees_Troy5bf43922012-09-07 16:07:55 -04001005bool TWPartition::Is_File_System(string File_System) {
1006 if (File_System == "ext2" ||
Dees_Troy63c8df72012-09-10 14:02:05 -04001007 File_System == "ext3" ||
Dees_Troy5bf43922012-09-07 16:07:55 -04001008 File_System == "ext4" ||
1009 File_System == "vfat" ||
1010 File_System == "ntfs" ||
1011 File_System == "yaffs2" ||
bigbiff bigbiff3e146522012-11-14 14:32:59 -05001012 File_System == "exfat" ||
Dees_Troye5017042013-08-29 16:38:55 +00001013 File_System == "f2fs" ||
Dees_Troy5bf43922012-09-07 16:07:55 -04001014 File_System == "auto")
1015 return true;
1016 else
1017 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001018}
1019
Dees_Troy5bf43922012-09-07 16:07:55 -04001020bool TWPartition::Is_Image(string File_System) {
Dees_Troy5fcd8f92012-10-16 12:22:05 -04001021 if (File_System == "emmc" || File_System == "mtd" || File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -04001022 return true;
1023 else
1024 return false;
1025}
1026
Dees_Troy51127312012-09-08 13:08:49 -04001027bool TWPartition::Make_Dir(string Path, bool Display_Error) {
Ethan Yonker93ac7a02016-11-07 22:05:58 -06001028 if (TWFunc::Get_D_Type_From_Stat(Path) != S_IFDIR)
1029 unlink(Path.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001030 if (!TWFunc::Path_Exists(Path)) {
Dees_Troy51127312012-09-08 13:08:49 -04001031 if (mkdir(Path.c_str(), 0777) == -1) {
1032 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001033 gui_msg(Msg(msg::kError, "create_folder_strerr=Can not create '{1}' folder ({2}).")(Path)(strerror(errno)));
Dees_Troy51127312012-09-08 13:08:49 -04001034 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001035 LOGINFO("Can not create '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -04001036 return false;
1037 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001038 LOGINFO("Created '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -04001039 return true;
1040 }
1041 }
1042 return true;
1043}
1044
Dees_Troy5bf43922012-09-07 16:07:55 -04001045void TWPartition::Setup_File_System(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001046 Can_Be_Mounted = true;
1047 Can_Be_Wiped = true;
1048
Dees_Troy5bf43922012-09-07 16:07:55 -04001049 // Make the mount point folder if it doesn't exist
Dees_Troy51127312012-09-08 13:08:49 -04001050 Make_Dir(Mount_Point, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -04001051 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
1052 Backup_Name = Display_Name;
bigbiffce8f83c2015-12-12 18:30:21 -05001053 Backup_Method = BM_FILES;
Dees_Troy5bf43922012-09-07 16:07:55 -04001054}
1055
Ethan Yonker1b190162016-12-05 15:25:19 -06001056void TWPartition::Setup_Image() {
Dees_Troy5bf43922012-09-07 16:07:55 -04001057 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
1058 Backup_Name = Display_Name;
Gary Peck82599a82012-11-21 16:23:12 -08001059 if (Current_File_System == "emmc")
bigbiffce8f83c2015-12-12 18:30:21 -05001060 Backup_Method = BM_DD;
Gary Peck82599a82012-11-21 16:23:12 -08001061 else if (Current_File_System == "mtd" || Current_File_System == "bml")
bigbiffce8f83c2015-12-12 18:30:21 -05001062 Backup_Method = BM_FLASH_UTILS;
Dees_Troy5bf43922012-09-07 16:07:55 -04001063 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001064 LOGINFO("Unhandled file system '%s' on image '%s'\n", Current_File_System.c_str(), Display_Name.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -04001065}
1066
Dees_Troye58d5262012-09-21 12:27:57 -04001067void TWPartition::Setup_AndSec(void) {
Dees_Troya13d74f2013-03-24 08:54:55 -05001068 Backup_Display_Name = "Android Secure";
Dees_Troye58d5262012-09-21 12:27:57 -04001069 Backup_Name = "and-sec";
Dees_Troya13d74f2013-03-24 08:54:55 -05001070 Can_Be_Backed_Up = true;
Dees_Troye58d5262012-09-21 12:27:57 -04001071 Has_Android_Secure = true;
1072 Symlink_Path = Mount_Point + "/.android_secure";
1073 Symlink_Mount_Point = "/and-sec";
1074 Backup_Path = Symlink_Mount_Point;
1075 Make_Dir("/and-sec", true);
1076 Recreate_AndSec_Folder();
Kjell Braden3126a112016-06-19 16:58:15 +00001077 Mount_Storage_Retry(true);
Dees_Troye58d5262012-09-21 12:27:57 -04001078}
1079
that9e0593e2014-10-08 00:01:24 +02001080void TWPartition::Setup_Data_Media() {
Ethan Yonker6277c792014-09-15 14:54:30 -05001081 LOGINFO("Setting up '%s' as data/media emulated storage.\n", Mount_Point.c_str());
Ethan Yonker66a19492015-12-10 10:19:45 -06001082 if (Storage_Name.empty() || Storage_Name == "Data")
1083 Storage_Name = "Internal Storage";
Ethan Yonker6277c792014-09-15 14:54:30 -05001084 Has_Data_Media = true;
1085 Is_Storage = true;
Ethan Yonker66a19492015-12-10 10:19:45 -06001086 Storage_Path = Mount_Point + "/media";
Ethan Yonker6277c792014-09-15 14:54:30 -05001087 Symlink_Path = Storage_Path;
Ethan Yonker66a19492015-12-10 10:19:45 -06001088 if (Mount_Point == "/data") {
1089 Is_Settings_Storage = true;
1090 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
1091 Make_Dir("/emmc", false);
1092 Symlink_Mount_Point = "/emmc";
1093 } else {
1094 Make_Dir("/sdcard", false);
1095 Symlink_Mount_Point = "/sdcard";
1096 }
1097 if (Mount(false) && TWFunc::Path_Exists(Mount_Point + "/media/0")) {
1098 Storage_Path = Mount_Point + "/media/0";
1099 Symlink_Path = Storage_Path;
1100 DataManager::SetValue(TW_INTERNAL_PATH, Mount_Point + "/media/0");
1101 UnMount(true);
1102 }
1103 DataManager::SetValue("tw_has_internal", 1);
1104 DataManager::SetValue("tw_has_data_media", 1);
Ethan Yonker3fdcda42016-11-30 12:29:37 -06001105 backup_exclusions.add_absolute_dir("/data/data/com.google.android.music/files");
Ethan Yonker6355b562017-05-01 09:42:17 -05001106 wipe_exclusions.add_absolute_dir(Mount_Point + "/misc/vold"); // adopted storage keys
Ethan Yonker79f88bd2016-12-09 14:52:12 -06001107 ExcludeAll(Mount_Point + "/.layout_version");
1108 ExcludeAll(Mount_Point + "/system/storage.xml");
Ethan Yonker6277c792014-09-15 14:54:30 -05001109 } else {
Ethan Yonker66a19492015-12-10 10:19:45 -06001110 if (Mount(true) && TWFunc::Path_Exists(Mount_Point + "/media/0")) {
1111 Storage_Path = Mount_Point + "/media/0";
1112 Symlink_Path = Storage_Path;
1113 UnMount(true);
1114 }
Ethan Yonker6277c792014-09-15 14:54:30 -05001115 }
Ethan Yonker79f88bd2016-12-09 14:52:12 -06001116 ExcludeAll(Mount_Point + "/media");
Ethan Yonker6277c792014-09-15 14:54:30 -05001117}
1118
Dees_Troy5bf43922012-09-07 16:07:55 -04001119void TWPartition::Find_Real_Block_Device(string& Block, bool Display_Error) {
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06001120 char device[PATH_MAX], realDevice[PATH_MAX];
Dees_Troy5bf43922012-09-07 16:07:55 -04001121
1122 strcpy(device, Block.c_str());
1123 memset(realDevice, 0, sizeof(realDevice));
1124 while (readlink(device, realDevice, sizeof(realDevice)) > 0)
1125 {
1126 strcpy(device, realDevice);
1127 memset(realDevice, 0, sizeof(realDevice));
1128 }
1129
1130 if (device[0] != '/') {
1131 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001132 LOGERR("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001133 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001134 LOGINFO("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001135 return;
Dees_Troy51a0e822012-09-05 15:24:24 -04001136 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -04001137 Block = device;
1138 return;
1139 }
1140}
1141
Kjell Braden3126a112016-06-19 16:58:15 +00001142bool TWPartition::Mount_Storage_Retry(bool Display_Error) {
Dees_Troy8e337f32012-10-13 22:07:49 -04001143 // On some devices, storage doesn't want to mount right away, retry and sleep
Kjell Braden3126a112016-06-19 16:58:15 +00001144 if (!Mount(Display_Error)) {
Dees_Troy8e337f32012-10-13 22:07:49 -04001145 int retry_count = 5;
1146 while (retry_count > 0 && !Mount(false)) {
1147 usleep(500000);
1148 retry_count--;
1149 }
Kjell Braden3126a112016-06-19 16:58:15 +00001150 return Mount(Display_Error);
Dees_Troy8e337f32012-10-13 22:07:49 -04001151 }
Kjell Braden3126a112016-06-19 16:58:15 +00001152 return true;
Dees_Troy8e337f32012-10-13 22:07:49 -04001153}
1154
Dees_Troy38bd7602012-09-14 13:33:53 -04001155bool TWPartition::Find_MTD_Block_Device(string MTD_Name) {
1156 FILE *fp = NULL;
1157 char line[255];
1158
1159 fp = fopen("/proc/mtd", "rt");
1160 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001161 LOGERR("Device does not support /proc/mtd\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001162 return false;
1163 }
1164
1165 while (fgets(line, sizeof(line), fp) != NULL)
1166 {
1167 char device[32], label[32];
1168 unsigned long size = 0;
Dees_Troy38bd7602012-09-14 13:33:53 -04001169 int deviceId;
1170
1171 sscanf(line, "%s %lx %*s %*c%s", device, &size, label);
1172
1173 // Skip header and blank lines
1174 if ((strcmp(device, "dev:") == 0) || (strlen(line) < 8))
1175 continue;
1176
1177 // Strip off the trailing " from the label
1178 label[strlen(label)-1] = '\0';
1179
1180 if (strcmp(label, MTD_Name.c_str()) == 0) {
1181 // We found our device
1182 // Strip off the trailing : from the device
1183 device[strlen(device)-1] = '\0';
1184 if (sscanf(device,"mtd%d", &deviceId) == 1) {
1185 sprintf(device, "/dev/block/mtdblock%d", deviceId);
1186 Primary_Block_Device = device;
Dees_Troy76543db2013-06-19 16:24:30 +00001187 fclose(fp);
1188 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -04001189 }
1190 }
1191 }
1192 fclose(fp);
1193
1194 return false;
1195}
1196
Dees_Troy51127312012-09-08 13:08:49 -04001197bool TWPartition::Get_Size_Via_statfs(bool Display_Error) {
1198 struct statfs st;
1199 string Local_Path = Mount_Point + "/.";
1200
1201 if (!Mount(Display_Error))
1202 return false;
1203
1204 if (statfs(Local_Path.c_str(), &st) != 0) {
1205 if (!Removable) {
1206 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001207 LOGERR("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -04001208 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001209 LOGINFO("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -04001210 }
1211 return false;
1212 }
1213 Size = (st.f_blocks * st.f_bsize);
1214 Used = ((st.f_blocks - st.f_bfree) * st.f_bsize);
1215 Free = (st.f_bfree * st.f_bsize);
1216 Backup_Size = Used;
1217 return true;
1218}
1219
1220bool TWPartition::Get_Size_Via_df(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001221 FILE* fp;
1222 char command[255], line[512];
1223 int include_block = 1;
1224 unsigned int min_len;
1225
1226 if (!Mount(Display_Error))
1227 return false;
1228
Dees_Troy38bd7602012-09-14 13:33:53 -04001229 min_len = Actual_Block_Device.size() + 2;
Dees_Troy51127312012-09-08 13:08:49 -04001230 sprintf(command, "df %s > /tmp/dfoutput.txt", Mount_Point.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001231 TWFunc::Exec_Cmd(command);
Dees_Troy51127312012-09-08 13:08:49 -04001232 fp = fopen("/tmp/dfoutput.txt", "rt");
1233 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001234 LOGINFO("Unable to open /tmp/dfoutput.txt.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001235 return false;
Dees_Troy51127312012-09-08 13:08:49 -04001236 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001237
1238 while (fgets(line, sizeof(line), fp) != NULL)
1239 {
1240 unsigned long blocks, used, available;
1241 char device[64];
1242 char tmpString[64];
1243
1244 if (strncmp(line, "Filesystem", 10) == 0)
1245 continue;
1246 if (strlen(line) < min_len) {
1247 include_block = 0;
1248 continue;
1249 }
1250 if (include_block) {
1251 sscanf(line, "%s %lu %lu %lu", device, &blocks, &used, &available);
1252 } else {
1253 // The device block string is so long that the df information is on the next line
1254 int space_count = 0;
Dees_Troye58d5262012-09-21 12:27:57 -04001255 sprintf(tmpString, "/dev/block/%s", Actual_Block_Device.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001256 while (tmpString[space_count] == 32)
1257 space_count++;
1258 sscanf(line + space_count, "%lu %lu %lu", &blocks, &used, &available);
1259 }
1260
1261 // Adjust block size to byte size
1262 Size = blocks * 1024ULL;
1263 Used = used * 1024ULL;
1264 Free = available * 1024ULL;
1265 Backup_Size = Used;
1266 }
1267 fclose(fp);
1268 return true;
1269}
1270
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001271unsigned long long TWPartition::IOCTL_Get_Block_Size() {
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001272 Find_Actual_Block_Device();
Ethan Yonker483e9f42016-01-11 22:21:18 -06001273
1274 return TWFunc::IOCTL_Get_Block_Size(Actual_Block_Device.c_str());
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001275}
1276
Dees_Troy5bf43922012-09-07 16:07:55 -04001277bool TWPartition::Find_Partition_Size(void) {
1278 FILE* fp;
1279 char line[512];
1280 string tmpdevice;
1281
igoriok87e3d932013-01-31 21:03:53 +02001282 fp = fopen("/proc/dumchar_info", "rt");
1283 if (fp != NULL) {
1284 while (fgets(line, sizeof(line), fp) != NULL)
1285 {
1286 char label[32], device[32];
1287 unsigned long size = 0;
1288
thatd43bf2d2014-09-21 23:13:02 +02001289 sscanf(line, "%s %lx %*x %*u %s", label, &size, device);
igoriok87e3d932013-01-31 21:03:53 +02001290
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05001291 // Skip header, annotation and blank lines
igoriok87e3d932013-01-31 21:03:53 +02001292 if ((strncmp(device, "/dev/", 5) != 0) || (strlen(line) < 8))
1293 continue;
1294
1295 tmpdevice = "/dev/";
1296 tmpdevice += label;
1297 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
1298 Size = size;
1299 fclose(fp);
1300 return true;
1301 }
1302 }
1303 }
1304
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001305 unsigned long long ioctl_size = IOCTL_Get_Block_Size();
1306 if (ioctl_size) {
1307 Size = ioctl_size;
1308 return true;
1309 }
1310
Dees_Troy5bf43922012-09-07 16:07:55 -04001311 // In this case, we'll first get the partitions we care about (with labels)
1312 fp = fopen("/proc/partitions", "rt");
1313 if (fp == NULL)
1314 return false;
1315
1316 while (fgets(line, sizeof(line), fp) != NULL)
1317 {
1318 unsigned long major, minor, blocks;
1319 char device[512];
Dees_Troy5bf43922012-09-07 16:07:55 -04001320
Dees_Troy63c8df72012-09-10 14:02:05 -04001321 if (strlen(line) < 7 || line[0] == 'm') continue;
Dees_Troy5bf43922012-09-07 16:07:55 -04001322 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
1323
1324 tmpdevice = "/dev/block/";
1325 tmpdevice += device;
Dees_Troy38bd7602012-09-14 13:33:53 -04001326 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001327 // Adjust block size to byte size
1328 Size = blocks * 1024ULL;
1329 fclose(fp);
1330 return true;
1331 }
1332 }
1333 fclose(fp);
1334 return false;
1335}
1336
Dees_Troy5bf43922012-09-07 16:07:55 -04001337bool TWPartition::Is_Mounted(void) {
1338 if (!Can_Be_Mounted)
1339 return false;
1340
1341 struct stat st1, st2;
1342 string test_path;
1343
1344 // Check to see if the mount point directory exists
1345 test_path = Mount_Point + "/.";
1346 if (stat(test_path.c_str(), &st1) != 0) return false;
1347
1348 // Check to see if the directory above the mount point exists
1349 test_path = Mount_Point + "/../.";
1350 if (stat(test_path.c_str(), &st2) != 0) return false;
1351
1352 // Compare the device IDs -- if they match then we're (probably) using tmpfs instead of an actual device
1353 int ret = (st1.st_dev != st2.st_dev) ? true : false;
1354
1355 return ret;
1356}
1357
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05001358bool TWPartition::Is_File_System_Writable(void) {
1359 if (!Is_File_System(Current_File_System) || !Is_Mounted())
1360 return false;
1361
1362 string test_path = Mount_Point + "/.";
1363 return (access(test_path.c_str(), W_OK) == 0);
1364}
1365
Dees_Troy5bf43922012-09-07 16:07:55 -04001366bool TWPartition::Mount(bool Display_Error) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001367 int exfat_mounted = 0;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001368 unsigned long flags = Mount_Flags;
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001369
Dees_Troy5bf43922012-09-07 16:07:55 -04001370 if (Is_Mounted()) {
1371 return true;
1372 } else if (!Can_Be_Mounted) {
1373 return false;
1374 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001375
1376 Find_Actual_Block_Device();
1377
1378 // Check the current file system before mounting
1379 Check_FS_Type();
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001380 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/exfat-fuse")) {
Dees_Troye34c1332013-02-06 19:13:00 +00001381 string cmd = "/sbin/exfat-fuse -o big_writes,max_read=131072,max_write=131072 " + Actual_Block_Device + " " + Mount_Point;
Dees_Troy2673cec2013-04-02 20:22:16 +00001382 LOGINFO("cmd: %s\n", cmd.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001383 string result;
1384 if (TWFunc::Exec_Cmd(cmd, result) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001385 LOGINFO("exfat-fuse failed to mount with result '%s', trying vfat\n", result.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001386 Current_File_System = "vfat";
1387 } else {
1388#ifdef TW_NO_EXFAT_FUSE
1389 UnMount(false);
1390 // We'll let the kernel handle it but using exfat-fuse to detect if the file system is actually exfat
1391 // Some kernels let us mount vfat as exfat which doesn't work out too well
1392#else
1393 exfat_mounted = 1;
1394#endif
1395 }
1396 }
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001397
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001398 if (Current_File_System == "ntfs" && (TWFunc::Path_Exists("/sbin/ntfs-3g") || TWFunc::Path_Exists("/sbin/mount.ntfs"))) {
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001399 string cmd;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001400 string Ntfsmount_Binary = "";
1401
1402 if (TWFunc::Path_Exists("/sbin/ntfs-3g"))
1403 Ntfsmount_Binary = "ntfs-3g";
1404 else if (TWFunc::Path_Exists("/sbin/mount.ntfs"))
1405 Ntfsmount_Binary = "mount.ntfs";
1406
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001407 if (Mount_Read_Only)
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001408 cmd = "/sbin/" + Ntfsmount_Binary + " -o ro " + Actual_Block_Device + " " + Mount_Point;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001409 else
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001410 cmd = "/sbin/" + Ntfsmount_Binary + " " + Actual_Block_Device + " " + Mount_Point;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001411 LOGINFO("cmd: '%s'\n", cmd.c_str());
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001412
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001413 if (TWFunc::Exec_Cmd(cmd) == 0) {
1414 return true;
1415 } else {
1416 LOGINFO("ntfs-3g failed to mount, trying regular mount method.\n");
1417 }
1418 }
1419
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001420 if (Mount_Read_Only)
1421 flags |= MS_RDONLY;
1422
Dees_Troy22042032012-12-18 21:23:08 +00001423 if (Fstab_File_System == "yaffs2") {
1424 // mount an MTD partition as a YAFFS2 filesystem.
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001425 flags = MS_NOATIME | MS_NODEV | MS_NODIRATIME;
1426 if (Mount_Read_Only)
1427 flags |= MS_RDONLY;
Dees_Troy76543db2013-06-19 16:24:30 +00001428 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags, NULL) < 0) {
1429 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags | MS_RDONLY, NULL) < 0) {
1430 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001431 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy76543db2013-06-19 16:24:30 +00001432 else
1433 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
1434 return false;
1435 } else {
1436 LOGINFO("Mounted '%s' (MTD) as RO\n", Mount_Point.c_str());
1437 return true;
1438 }
1439 } else {
1440 struct stat st;
1441 string test_path = Mount_Point;
1442 if (stat(test_path.c_str(), &st) < 0) {
1443 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001444 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy76543db2013-06-19 16:24:30 +00001445 else
1446 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
1447 return false;
1448 }
1449 mode_t new_mode = st.st_mode | S_IXUSR | S_IXGRP | S_IXOTH;
1450 if (new_mode != st.st_mode) {
1451 LOGINFO("Fixing execute permissions for %s\n", Mount_Point.c_str());
1452 if (chmod(Mount_Point.c_str(), new_mode) < 0) {
1453 if (Display_Error)
1454 LOGERR("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
1455 else
1456 LOGINFO("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
1457 return false;
1458 }
1459 }
Dees_Troy22042032012-12-18 21:23:08 +00001460 return true;
Dees_Troy76543db2013-06-19 16:24:30 +00001461 }
thatc7572eb2015-03-31 18:55:30 +02001462 }
1463
1464 string mount_fs = Current_File_System;
1465 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sys/module/texfat"))
1466 mount_fs = "texfat";
1467
1468 if (!exfat_mounted &&
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001469 mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), mount_fs.c_str(), flags, Mount_Options.c_str()) != 0 &&
1470 mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), mount_fs.c_str(), flags, NULL) != 0) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001471#ifdef TW_NO_EXFAT_FUSE
1472 if (Current_File_System == "exfat") {
Dees_Troy2673cec2013-04-02 20:22:16 +00001473 LOGINFO("Mounting exfat failed, trying vfat...\n");
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001474 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), "vfat", 0, NULL) != 0) {
Dees_Troy85f44ed2013-01-09 18:42:36 +00001475 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001476 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy85f44ed2013-01-09 18:42:36 +00001477 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001478 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001479 LOGINFO("Actual block device: '%s', current file system: '%s', flags: 0x%8x, options: '%s'\n", Actual_Block_Device.c_str(), Current_File_System.c_str(), flags, Mount_Options.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001480 return false;
Dees_Troy85f44ed2013-01-09 18:42:36 +00001481 }
Dees_Troyb05ddee2013-01-28 20:24:50 +00001482 } else {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001483#endif
bigbiff bigbiff26774a02014-03-29 18:22:00 -04001484 if (!Removable && Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001485 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001486 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001487 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
1488 LOGINFO("Actual block device: '%s', current file system: '%s'\n", Actual_Block_Device.c_str(), Current_File_System.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001489 return false;
1490#ifdef TW_NO_EXFAT_FUSE
Dees_Troy85f44ed2013-01-09 18:42:36 +00001491 }
1492#endif
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001493 }
Ethan Yonker253368a2014-11-25 15:00:52 -06001494
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001495 if (Removable)
1496 Update_Size(Display_Error);
1497
xiaolu9416f4f2015-06-04 08:22:23 +08001498 if (!Symlink_Mount_Point.empty() && TWFunc::Path_Exists(Symlink_Path)) {
codelover2a3d4ce2015-03-14 20:26:49 +08001499 string Command = "mount -o bind '" + Symlink_Path + "' '" + Symlink_Mount_Point + "'";
Vojtech Bocek05534202013-09-11 08:11:56 +02001500 TWFunc::Exec_Cmd(Command);
Dees_Troy5bf43922012-09-07 16:07:55 -04001501 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001502 return true;
1503}
1504
1505bool TWPartition::UnMount(bool Display_Error) {
1506 if (Is_Mounted()) {
1507 int never_unmount_system;
1508
1509 DataManager::GetValue(TW_DONT_UNMOUNT_SYSTEM, never_unmount_system);
1510 if (never_unmount_system == 1 && Mount_Point == "/system")
1511 return true; // Never unmount system if you're not supposed to unmount it
1512
Matt Mowera8e6d832017-02-14 20:20:59 -06001513 if (Is_Storage && MTP_Storage_ID > 0)
Ethan Yonker726a0202014-12-16 20:01:38 -06001514 PartitionManager.Remove_MTP_Storage(MTP_Storage_ID);
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001515
Dees_Troy38bd7602012-09-14 13:33:53 -04001516 if (!Symlink_Mount_Point.empty())
1517 umount(Symlink_Mount_Point.c_str());
1518
Dees_Troyb05ddee2013-01-28 20:24:50 +00001519 umount(Mount_Point.c_str());
1520 if (Is_Mounted()) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001521 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001522 gui_msg(Msg(msg::kError, "fail_unmount=Failed to unmount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy5bf43922012-09-07 16:07:55 -04001523 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001524 LOGINFO("Unable to unmount '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001525 return false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001526 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -04001527 return true;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001528 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001529 } else {
1530 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001531 }
1532}
1533
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05001534bool TWPartition::ReMount(bool Display_Error) {
1535 if (UnMount(Display_Error))
1536 return Mount(Display_Error);
1537 return false;
1538}
1539
1540bool TWPartition::ReMount_RW(bool Display_Error) {
1541 // No need to remount if already mounted rw
1542 if (Is_File_System_Writable())
1543 return true;
1544
1545 bool ro = Mount_Read_Only;
1546 int flags = Mount_Flags;
1547
1548 Mount_Read_Only = false;
1549 Mount_Flags &= ~MS_RDONLY;
1550
1551 bool ret = ReMount(Display_Error);
1552
1553 Mount_Read_Only = ro;
1554 Mount_Flags = flags;
1555
1556 return ret;
1557}
1558
Gary Peck43acadf2012-11-21 21:19:01 -08001559bool TWPartition::Wipe(string New_File_System) {
Ethan Yonker726a0202014-12-16 20:01:38 -06001560 bool wiped = false, update_crypt = false, recreate_media = true;
Dees_Troy16c2b312013-01-15 16:51:18 +00001561 int check;
1562 string Layout_Filename = Mount_Point + "/.layout_version";
1563
Dees_Troy38bd7602012-09-14 13:33:53 -04001564 if (!Can_Be_Wiped) {
Matt Mower3c366972015-12-25 19:28:31 -06001565 gui_msg(Msg(msg::kError, "cannot_wipe=Partition {1} cannot be wiped.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04001566 return false;
1567 }
1568
Dees_Troyc51f1f92012-09-20 15:32:13 -04001569 if (Mount_Point == "/cache")
Dees_Troy2673cec2013-04-02 20:22:16 +00001570 Log_Offset = 0;
Dees_Troyc51f1f92012-09-20 15:32:13 -04001571
Dees_Troy16c2b312013-01-15 16:51:18 +00001572 if (Retain_Layout_Version && Mount(false) && TWFunc::Path_Exists(Layout_Filename))
1573 TWFunc::copy_file(Layout_Filename, "/.layout_version", 0600);
1574 else
1575 unlink("/.layout_version");
Dees_Troy38bd7602012-09-14 13:33:53 -04001576
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001577 if (Has_Data_Media && Current_File_System == New_File_System) {
Dees_Troy16c2b312013-01-15 16:51:18 +00001578 wiped = Wipe_Data_Without_Wiping_Media();
Ethan Yonker5eac2222014-06-11 12:22:55 -05001579 recreate_media = false;
Dees_Troy16c2b312013-01-15 16:51:18 +00001580 } else {
Dees_Troy16c2b312013-01-15 16:51:18 +00001581 DataManager::GetValue(TW_RM_RF_VAR, check);
1582
Hashcodedabfd492013-08-29 22:45:30 -07001583 if (check || Use_Rm_Rf)
Dees_Troy16c2b312013-01-15 16:51:18 +00001584 wiped = Wipe_RMRF();
1585 else if (New_File_System == "ext4")
1586 wiped = Wipe_EXT4();
1587 else if (New_File_System == "ext2" || New_File_System == "ext3")
1588 wiped = Wipe_EXT23(New_File_System);
1589 else if (New_File_System == "vfat")
1590 wiped = Wipe_FAT();
1591 else if (New_File_System == "exfat")
1592 wiped = Wipe_EXFAT();
1593 else if (New_File_System == "yaffs2")
1594 wiped = Wipe_MTD();
Dees_Troye5017042013-08-29 16:38:55 +00001595 else if (New_File_System == "f2fs")
1596 wiped = Wipe_F2FS();
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001597 else if (New_File_System == "ntfs")
1598 wiped = Wipe_NTFS();
Dees_Troy16c2b312013-01-15 16:51:18 +00001599 else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001600 LOGERR("Unable to wipe '%s' -- unknown file system '%s'\n", Mount_Point.c_str(), New_File_System.c_str());
Dees_Troy16c2b312013-01-15 16:51:18 +00001601 unlink("/.layout_version");
1602 return false;
1603 }
1604 update_crypt = wiped;
Gary Pecke8bc5d72012-12-21 06:45:25 -08001605 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001606
Gary Pecke8bc5d72012-12-21 06:45:25 -08001607 if (wiped) {
Dees_Troy1c1ac442013-01-17 21:42:14 +00001608 if (Mount_Point == "/cache")
1609 DataManager::Output_Version();
1610
Dees_Troy16c2b312013-01-15 16:51:18 +00001611 if (TWFunc::Path_Exists("/.layout_version") && Mount(false))
1612 TWFunc::copy_file("/.layout_version", Layout_Filename, 0600);
1613
1614 if (update_crypt) {
1615 Setup_File_System(false);
1616 if (Is_Encrypted && !Is_Decrypted) {
1617 // just wiped an encrypted partition back to its unencrypted state
1618 Is_Encrypted = false;
1619 Is_Decrypted = false;
1620 Decrypted_Block_Device = "";
1621 if (Mount_Point == "/data") {
1622 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1623 DataManager::SetValue(TW_IS_DECRYPTED, 0);
1624 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001625 }
1626 }
Ethan Yonker5eac2222014-06-11 12:22:55 -05001627
Ethan Yonker66a19492015-12-10 10:19:45 -06001628 if (Has_Data_Media && recreate_media) {
Ethan Yonker5eac2222014-06-11 12:22:55 -05001629 Recreate_Media_Folder();
1630 }
Matt Mower209c9632016-01-20 12:08:35 -06001631 if (Is_Storage && Mount(false))
1632 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Gary Pecke8bc5d72012-12-21 06:45:25 -08001633 }
Matt Mower209c9632016-01-20 12:08:35 -06001634
Gary Pecke8bc5d72012-12-21 06:45:25 -08001635 return wiped;
Dees_Troy51a0e822012-09-05 15:24:24 -04001636}
1637
Gary Peck43acadf2012-11-21 21:19:01 -08001638bool TWPartition::Wipe() {
Gary Peck82599a82012-11-21 16:23:12 -08001639 if (Is_File_System(Current_File_System))
1640 return Wipe(Current_File_System);
1641 else
1642 return Wipe(Fstab_File_System);
Gary Peck43acadf2012-11-21 21:19:01 -08001643}
1644
Dees_Troye58d5262012-09-21 12:27:57 -04001645bool TWPartition::Wipe_AndSec(void) {
1646 if (!Has_Android_Secure)
1647 return false;
1648
Dees_Troye58d5262012-09-21 12:27:57 -04001649 if (!Mount(true))
1650 return false;
1651
Ethan Yonker74db1572015-10-28 12:44:49 -05001652 gui_msg(Msg("wiping=Wiping {1}")(Backup_Display_Name));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001653 TWFunc::removeDir(Mount_Point + "/.android_secure/", true);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001654 return true;
Dees_Troye58d5262012-09-21 12:27:57 -04001655}
1656
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001657bool TWPartition::Can_Repair() {
Ethan Yonkera2719152015-05-28 09:44:41 -05001658 if (Mount_Read_Only)
1659 return false;
Matt Mower18794c82015-11-11 16:22:45 -06001660 if (Current_File_System == "vfat" && TWFunc::Path_Exists("/sbin/fsck.fat"))
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001661 return true;
1662 else if ((Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") && TWFunc::Path_Exists("/sbin/e2fsck"))
1663 return true;
1664 else if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/fsck.exfat"))
1665 return true;
1666 else if (Current_File_System == "f2fs" && TWFunc::Path_Exists("/sbin/fsck.f2fs"))
1667 return true;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001668 else if (Current_File_System == "ntfs" && (TWFunc::Path_Exists("/sbin/ntfsfix") || TWFunc::Path_Exists("/sbin/fsck.ntfs")))
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001669 return true;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001670 return false;
1671}
1672
1673bool TWPartition::Repair() {
1674 string command;
1675
1676 if (Current_File_System == "vfat") {
Matt Mower18794c82015-11-11 16:22:45 -06001677 if (!TWFunc::Path_Exists("/sbin/fsck.fat")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001678 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.fat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001679 return false;
1680 }
1681 if (!UnMount(true))
1682 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001683 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.fat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001684 Find_Actual_Block_Device();
Matt Mower18794c82015-11-11 16:22:45 -06001685 command = "/sbin/fsck.fat -y " + Actual_Block_Device;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001686 LOGINFO("Repair command: %s\n", command.c_str());
1687 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001688 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001689 return true;
1690 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001691 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001692 return false;
1693 }
1694 }
1695 if (Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") {
1696 if (!TWFunc::Path_Exists("/sbin/e2fsck")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001697 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("e2fsck"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001698 return false;
1699 }
1700 if (!UnMount(true))
1701 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001702 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("e2fsck"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001703 Find_Actual_Block_Device();
Ethan Yonkera2719152015-05-28 09:44:41 -05001704 command = "/sbin/e2fsck -fp " + Actual_Block_Device;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001705 LOGINFO("Repair command: %s\n", command.c_str());
1706 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001707 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001708 return true;
1709 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001710 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001711 return false;
1712 }
1713 }
1714 if (Current_File_System == "exfat") {
1715 if (!TWFunc::Path_Exists("/sbin/fsck.exfat")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001716 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.exfat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001717 return false;
1718 }
1719 if (!UnMount(true))
1720 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001721 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.exfat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001722 Find_Actual_Block_Device();
1723 command = "/sbin/fsck.exfat " + Actual_Block_Device;
1724 LOGINFO("Repair command: %s\n", command.c_str());
1725 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001726 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001727 return true;
1728 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001729 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001730 return false;
1731 }
1732 }
1733 if (Current_File_System == "f2fs") {
1734 if (!TWFunc::Path_Exists("/sbin/fsck.f2fs")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001735 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.f2fs"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001736 return false;
1737 }
1738 if (!UnMount(true))
1739 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001740 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.f2fs"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001741 Find_Actual_Block_Device();
1742 command = "/sbin/fsck.f2fs " + Actual_Block_Device;
1743 LOGINFO("Repair command: %s\n", command.c_str());
1744 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001745 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001746 return true;
1747 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001748 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001749 return false;
1750 }
1751 }
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001752 if (Current_File_System == "ntfs") {
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001753 string Ntfsfix_Binary;
1754 if (TWFunc::Path_Exists("/sbin/ntfsfix"))
1755 Ntfsfix_Binary = "ntfsfix";
1756 else if (TWFunc::Path_Exists("/sbin/fsck.ntfs"))
1757 Ntfsfix_Binary = "fsck.ntfs";
1758 else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001759 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("ntfsfix"));
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001760 return false;
1761 }
1762 if (!UnMount(true))
1763 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001764 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)(Ntfsfix_Binary));
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001765 Find_Actual_Block_Device();
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001766 command = "/sbin/" + Ntfsfix_Binary + " " + Actual_Block_Device;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001767 LOGINFO("Repair command: %s\n", command.c_str());
1768 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001769 gui_msg("done=Done.");
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001770 return true;
1771 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001772 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001773 return false;
1774 }
1775 }
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001776 return false;
1777}
1778
Ethan Yonkera2719152015-05-28 09:44:41 -05001779bool TWPartition::Can_Resize() {
1780 if (Mount_Read_Only)
1781 return false;
1782 if ((Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") && TWFunc::Path_Exists("/sbin/resize2fs"))
1783 return true;
1784 return false;
1785}
1786
1787bool TWPartition::Resize() {
1788 string command;
1789
1790 if (Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") {
1791 if (!Can_Repair()) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001792 LOGINFO("Cannot resize %s because %s cannot be repaired before resizing.\n", Display_Name.c_str(), Display_Name.c_str());
1793 gui_msg(Msg(msg::kError, "cannot_resize=Cannot resize {1}.")(Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001794 return false;
1795 }
1796 if (!TWFunc::Path_Exists("/sbin/resize2fs")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001797 LOGINFO("resize2fs does not exist! Cannot resize!\n");
1798 gui_msg(Msg(msg::kError, "cannot_resize=Cannot resize {1}.")(Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001799 return false;
1800 }
1801 // Repair will unmount so no need to do it twice
Ethan Yonker74db1572015-10-28 12:44:49 -05001802 gui_msg(Msg("repair_resize=Repairing {1} before resizing.")( Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001803 if (!Repair())
1804 return false;
Ethan Yonker74db1572015-10-28 12:44:49 -05001805 gui_msg(Msg("resizing=Resizing {1} using {2}...")(Display_Name)("resize2fs"));
Ethan Yonkera2719152015-05-28 09:44:41 -05001806 Find_Actual_Block_Device();
1807 command = "/sbin/resize2fs " + Actual_Block_Device;
1808 if (Length != 0) {
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001809 unsigned long long Actual_Size = IOCTL_Get_Block_Size();
1810 if (Actual_Size == 0)
1811 return false;
Ethan Yonkera2719152015-05-28 09:44:41 -05001812
Ethan Yonkera2719152015-05-28 09:44:41 -05001813 unsigned long long Block_Count;
1814 if (Length < 0) {
1815 // Reduce overall size by this length
1816 Block_Count = (Actual_Size / 1024LLU) - ((unsigned long long)(Length * -1) / 1024LLU);
1817 } else {
1818 // This is the size, not a size reduction
1819 Block_Count = ((unsigned long long)(Length) / 1024LLU);
1820 }
1821 char temp[256];
1822 sprintf(temp, "%llu", Block_Count);
1823 command += " ";
1824 command += temp;
1825 command += "K";
1826 }
1827 LOGINFO("Resize command: %s\n", command.c_str());
1828 if (TWFunc::Exec_Cmd(command) == 0) {
1829 Update_Size(true);
Ethan Yonker74db1572015-10-28 12:44:49 -05001830 gui_msg("done=Done.");
Ethan Yonkera2719152015-05-28 09:44:41 -05001831 return true;
1832 } else {
1833 Update_Size(true);
Ethan Yonker74db1572015-10-28 12:44:49 -05001834 gui_msg(Msg(msg::kError, "unable_resize=Unable to resize {1}.")(Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001835 return false;
1836 }
1837 }
1838 return false;
1839}
1840
bigbiffce8f83c2015-12-12 18:30:21 -05001841bool TWPartition::Backup(PartitionSettings *part_settings, pid_t *tar_fork_pid) {
1842 if (Backup_Method == BM_FILES)
1843 return Backup_Tar(part_settings, tar_fork_pid);
1844 else if (Backup_Method == BM_DD)
1845 return Backup_Image(part_settings);
1846 else if (Backup_Method == BM_FLASH_UTILS)
1847 return Backup_Dump_Image(part_settings);
Dees_Troy2673cec2013-04-02 20:22:16 +00001848 LOGERR("Unknown backup method for '%s'\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001849 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001850}
1851
bigbiffce8f83c2015-12-12 18:30:21 -05001852bool TWPartition::Restore(PartitionSettings *part_settings) {
Matt Mower3c366972015-12-25 19:28:31 -06001853 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, gui_parse_text("{@restoring_hdr}"));
Ethan Yonkere080c1f2016-09-19 13:50:25 -05001854 LOGINFO("Restore filename is: %s/%s\n", part_settings->Backup_Folder.c_str(), Backup_FileName.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001855
bigbiffce8f83c2015-12-12 18:30:21 -05001856 string Restore_File_System = Get_Restore_File_System(part_settings);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001857
1858 if (Is_File_System(Restore_File_System))
bigbiffce8f83c2015-12-12 18:30:21 -05001859 return Restore_Tar(part_settings);
1860 else if (Is_Image(Restore_File_System))
1861 return Restore_Image(part_settings);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001862
1863 LOGERR("Unknown restore method for '%s'\n", Mount_Point.c_str());
1864 return false;
1865}
1866
bigbiffce8f83c2015-12-12 18:30:21 -05001867string TWPartition::Get_Restore_File_System(PartitionSettings *part_settings) {
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001868 size_t first_period, second_period;
1869 string Restore_File_System;
1870
Gary Peck43acadf2012-11-21 21:19:01 -08001871 // Parse backup filename to extract the file system before wiping
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05001872 first_period = Backup_FileName.find(".");
Gary Peck43acadf2012-11-21 21:19:01 -08001873 if (first_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001874 LOGERR("Unable to find file system (first period).\n");
thatd43bf2d2014-09-21 23:13:02 +02001875 return string();
Gary Peck43acadf2012-11-21 21:19:01 -08001876 }
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05001877 Restore_File_System = Backup_FileName.substr(first_period + 1, Backup_FileName.size() - first_period - 1);
Gary Peck43acadf2012-11-21 21:19:01 -08001878 second_period = Restore_File_System.find(".");
1879 if (second_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001880 LOGERR("Unable to find file system (second period).\n");
thatd43bf2d2014-09-21 23:13:02 +02001881 return string();
Gary Peck43acadf2012-11-21 21:19:01 -08001882 }
1883 Restore_File_System.resize(second_period);
Matt Mowera0cd91d2016-12-30 18:21:42 -06001884 LOGINFO("Restore file system is: '%s'.\n", Restore_File_System.c_str());
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001885 return Restore_File_System;
Dees_Troy51a0e822012-09-05 15:24:24 -04001886}
1887
1888string TWPartition::Backup_Method_By_Name() {
bigbiffce8f83c2015-12-12 18:30:21 -05001889 if (Backup_Method == BM_NONE)
Dees_Troy38bd7602012-09-14 13:33:53 -04001890 return "none";
bigbiffce8f83c2015-12-12 18:30:21 -05001891 else if (Backup_Method == BM_FILES)
Dees_Troy38bd7602012-09-14 13:33:53 -04001892 return "files";
bigbiffce8f83c2015-12-12 18:30:21 -05001893 else if (Backup_Method == BM_DD)
Dees_Troy38bd7602012-09-14 13:33:53 -04001894 return "dd";
bigbiffce8f83c2015-12-12 18:30:21 -05001895 else if (Backup_Method == BM_FLASH_UTILS)
Dees_Troy38bd7602012-09-14 13:33:53 -04001896 return "flash_utils";
1897 else
1898 return "undefined";
1899 return "ERROR!";
Dees_Troy51a0e822012-09-05 15:24:24 -04001900}
1901
1902bool TWPartition::Decrypt(string Password) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001903 LOGINFO("STUB TWPartition::Decrypt, password: '%s'\n", Password.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001904 // Is this needed?
Dees_Troy51a0e822012-09-05 15:24:24 -04001905 return 1;
1906}
1907
1908bool TWPartition::Wipe_Encryption() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001909 bool Save_Data_Media = Has_Data_Media;
1910
1911 if (!UnMount(true))
1912 return false;
1913
Dees_Troy38bd7602012-09-14 13:33:53 -04001914 Has_Data_Media = false;
Dees_Troy74fb2e92013-04-15 14:35:47 +00001915 Decrypted_Block_Device = "";
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001916#ifdef TW_INCLUDE_CRYPTO
Ethan Yonker1b190162016-12-05 15:25:19 -06001917 if (Is_Decrypted && !Decrypted_Block_Device.empty()) {
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001918 if (!UnMount(true))
1919 return false;
Matt Mower2b18a532015-02-20 16:58:05 -06001920 if (delete_crypto_blk_dev((char*)("userdata")) != 0) {
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001921 LOGERR("Error deleting crypto block device, continuing anyway.\n");
1922 }
1923 }
1924#endif
Dees_Troy74fb2e92013-04-15 14:35:47 +00001925 Is_Decrypted = false;
1926 Is_Encrypted = false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001927 Find_Actual_Block_Device();
Ethan Yonkerbc85b632015-08-09 12:48:14 -05001928 if (Crypto_Key_Location == "footer") {
1929 int newlen, fd;
1930 if (Length != 0) {
1931 newlen = Length;
1932 if (newlen < 0)
1933 newlen = newlen * -1;
1934 } else {
1935 newlen = CRYPT_FOOTER_OFFSET;
1936 }
1937 if ((fd = open(Actual_Block_Device.c_str(), O_RDWR)) < 0) {
1938 gui_print_color("warning", "Unable to open '%s' to wipe crypto key\n", Actual_Block_Device.c_str());
1939 } else {
1940 unsigned int block_count;
1941 if ((ioctl(fd, BLKGETSIZE, &block_count)) == -1) {
1942 gui_print_color("warning", "Unable to get block size for wiping crypto footer.\n");
1943 } else {
1944 off64_t offset = ((off64_t)block_count * 512) - newlen;
1945 if (lseek64(fd, offset, SEEK_SET) == -1) {
1946 gui_print_color("warning", "Unable to lseek64 for wiping crypto footer.\n");
1947 } else {
1948 void* buffer = malloc(newlen);
1949 if (!buffer) {
1950 gui_print_color("warning", "Failed to malloc for wiping crypto footer.\n");
1951 } else {
1952 memset(buffer, 0, newlen);
1953 int ret = write(fd, buffer, newlen);
1954 if (ret != newlen) {
1955 gui_print_color("warning", "Failed to wipe crypto footer.\n");
1956 } else {
1957 LOGINFO("Successfully wiped crypto footer.\n");
1958 }
Matt Mower13a8f0b2015-09-26 15:40:03 -05001959 free(buffer);
Ethan Yonkerbc85b632015-08-09 12:48:14 -05001960 }
1961 }
1962 }
1963 close(fd);
1964 }
1965 } else {
Ethan Yonkerc2dafbb2016-03-15 22:20:59 -05001966 if (TWFunc::IOCTL_Get_Block_Size(Crypto_Key_Location.c_str()) >= 16384LLU) {
1967 string Command = "dd of='" + Crypto_Key_Location + "' if=/dev/zero bs=16384 count=1";
1968 TWFunc::Exec_Cmd(Command);
1969 } else {
1970 LOGINFO("Crypto key location reports size < 16K so not wiping crypto footer.\n");
1971 }
Ethan Yonkerbc85b632015-08-09 12:48:14 -05001972 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001973 if (Wipe(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001974 Has_Data_Media = Save_Data_Media;
1975 if (Has_Data_Media && !Symlink_Mount_Point.empty()) {
1976 Recreate_Media_Folder();
Ethan Yonker726a0202014-12-16 20:01:38 -06001977 if (Mount(false))
1978 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04001979 }
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001980 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
Ethan Yonker83e82572014-04-04 10:59:28 -05001981#ifndef TW_OEM_BUILD
Ethan Yonker74db1572015-10-28 12:44:49 -05001982 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 -05001983#endif
Dees_Troy38bd7602012-09-14 13:33:53 -04001984 return true;
1985 } else {
1986 Has_Data_Media = Save_Data_Media;
Ethan Yonker74db1572015-10-28 12:44:49 -05001987 gui_err("format_data_err=Unable to format to remove encryption.");
Ethan Yonker726a0202014-12-16 20:01:38 -06001988 if (Has_Data_Media && Mount(false))
1989 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04001990 return false;
1991 }
1992 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001993}
1994
1995void TWPartition::Check_FS_Type() {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001996 const char* type;
1997 blkid_probe pr;
Dees_Troy5bf43922012-09-07 16:07:55 -04001998
Dees_Troy68cab492012-12-12 19:29:35 +00001999 if (Fstab_File_System == "yaffs2" || Fstab_File_System == "mtd" || Fstab_File_System == "bml" || Ignore_Blkid)
2000 return; // Running blkid on some mtd devices causes a massive crash or needs to be skipped
Dees_Troy5bf43922012-09-07 16:07:55 -04002001
Dees_Troy38bd7602012-09-14 13:33:53 -04002002 Find_Actual_Block_Device();
Dees_Troy8170a922012-09-18 15:40:25 -04002003 if (!Is_Present)
2004 return;
Dees_Troy51127312012-09-08 13:08:49 -04002005
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05002006 pr = blkid_new_probe_from_filename(Actual_Block_Device.c_str());
2007 if (blkid_do_fullprobe(pr)) {
2008 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00002009 LOGINFO("Can't probe device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05002010 return;
Dees_Troy5bf43922012-09-07 16:07:55 -04002011 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02002012
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05002013 if (blkid_probe_lookup_value(pr, "TYPE", &type, NULL) < 0) {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05002014 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00002015 LOGINFO("can't find filesystem on device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05002016 return;
2017 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02002018
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05002019 Current_File_System = type;
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02002020 blkid_free_probe(pr);
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06002021 if (fs_flags.size() > 1) {
2022 std::vector<partition_fs_flags_struct>::iterator iter;
2023 std::vector<partition_fs_flags_struct>::iterator found = fs_flags.begin();
2024
2025 for (iter = fs_flags.begin(); iter != fs_flags.end(); iter++) {
2026 if (iter->File_System == Current_File_System) {
2027 found = iter;
2028 break;
2029 }
2030 }
2031 // If we don't find a match, we default the flags to the first set of flags that we received from the fstab
2032 if (Mount_Flags != found->Mount_Flags || Mount_Options != found->Mount_Options) {
2033 Mount_Flags = found->Mount_Flags;
2034 Mount_Options = found->Mount_Options;
2035 LOGINFO("Mount_Flags: %i, Mount_Options: %s\n", Mount_Flags, Mount_Options.c_str());
2036 }
2037 }
Dees_Troy51a0e822012-09-05 15:24:24 -04002038}
2039
Gary Peck43acadf2012-11-21 21:19:01 -08002040bool TWPartition::Wipe_EXT23(string File_System) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002041 if (!UnMount(true))
2042 return false;
2043
Dees_Troy43d8b002012-09-17 16:00:01 -04002044 if (TWFunc::Path_Exists("/sbin/mke2fs")) {
Vojtech Bocek05534202013-09-11 08:11:56 +02002045 string command;
Dees_Troy38bd7602012-09-14 13:33:53 -04002046
Greg Wallace4b44fef2015-12-29 15:33:24 -05002047 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mke2fs"));
Dees_Troy38bd7602012-09-14 13:33:53 -04002048 Find_Actual_Block_Device();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002049 command = "mke2fs -t " + File_System + " -m 0 " + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00002050 LOGINFO("mke2fs command: %s\n", command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02002051 if (TWFunc::Exec_Cmd(command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08002052 Current_File_System = File_System;
Dees_Troye58d5262012-09-21 12:27:57 -04002053 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05002054 gui_msg("done=Done.");
Dees_Troy38bd7602012-09-14 13:33:53 -04002055 return true;
2056 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002057 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04002058 return false;
2059 }
2060 } else
2061 return Wipe_RMRF();
2062
2063 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04002064}
2065
2066bool TWPartition::Wipe_EXT4() {
Ethan Yonker25f20c12014-10-14 09:04:43 -05002067 Find_Actual_Block_Device();
2068 if (!Is_Present) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002069 LOGINFO("Block device not present, cannot wipe %s.\n", Display_Name.c_str());
2070 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Ethan Yonker25f20c12014-10-14 09:04:43 -05002071 return false;
2072 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002073 if (!UnMount(true))
2074 return false;
2075
Matt Mower87413642017-01-17 21:14:46 -06002076#if defined(USE_EXT4)
Ethan Yonkerf27497f2014-02-09 11:48:33 -06002077 int ret;
2078 char *secontext = NULL;
2079
Greg Wallace4b44fef2015-12-29 15:33:24 -05002080 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("make_ext4fs"));
Ethan Yonkerf27497f2014-02-09 11:48:33 -06002081
Dees Troy99c8dbf2014-03-10 16:53:58 +00002082 if (!selinux_handle || selabel_lookup(selinux_handle, &secontext, Mount_Point.c_str(), S_IFDIR) < 0) {
Ethan Yonkerf27497f2014-02-09 11:48:33 -06002083 LOGINFO("Cannot lookup security context for '%s'\n", Mount_Point.c_str());
2084 ret = make_ext4fs(Actual_Block_Device.c_str(), Length, Mount_Point.c_str(), NULL);
2085 } else {
2086 ret = make_ext4fs(Actual_Block_Device.c_str(), Length, Mount_Point.c_str(), selinux_handle);
2087 }
2088 if (ret != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002089 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troya95f55c2013-08-17 13:14:43 +00002090 return false;
2091 } else {
bigbiff bigbiffc49d7062013-10-11 20:28:00 -04002092 string sedir = Mount_Point + "/lost+found";
2093 PartitionManager.Mount_By_Path(sedir.c_str(), true);
2094 rmdir(sedir.c_str());
2095 mkdir(sedir.c_str(), S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP);
Dees_Troya95f55c2013-08-17 13:14:43 +00002096 return true;
2097 }
2098#else
Dees_Troy43d8b002012-09-17 16:00:01 -04002099 if (TWFunc::Path_Exists("/sbin/make_ext4fs")) {
Vojtech Bocek05534202013-09-11 08:11:56 +02002100 string Command;
Dees_Troy38bd7602012-09-14 13:33:53 -04002101
Greg Wallace4b44fef2015-12-29 15:33:24 -05002102 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("make_ext4fs"));
Dees_Troy38bd7602012-09-14 13:33:53 -04002103 Find_Actual_Block_Device();
2104 Command = "make_ext4fs";
2105 if (!Is_Decrypted && Length != 0) {
2106 // Only use length if we're not decrypted
2107 char len[32];
2108 sprintf(len, "%i", Length);
2109 Command += " -l ";
2110 Command += len;
2111 }
Dees_Troy5295d582013-09-06 15:51:08 +00002112 if (TWFunc::Path_Exists("/file_contexts")) {
2113 Command += " -S /file_contexts";
2114 }
2115 Command += " -a " + Mount_Point + " " + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00002116 LOGINFO("make_ext4fs command: %s\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02002117 if (TWFunc::Exec_Cmd(Command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08002118 Current_File_System = "ext4";
Dees_Troye58d5262012-09-21 12:27:57 -04002119 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05002120 gui_msg("done=Done.");
Dees_Troy38bd7602012-09-14 13:33:53 -04002121 return true;
2122 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002123 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04002124 return false;
2125 }
2126 } else
Gary Peck43acadf2012-11-21 21:19:01 -08002127 return Wipe_EXT23("ext4");
Dees_Troya95f55c2013-08-17 13:14:43 +00002128#endif
Dees_Troy38bd7602012-09-14 13:33:53 -04002129 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04002130}
2131
2132bool TWPartition::Wipe_FAT() {
Vojtech Bocek05534202013-09-11 08:11:56 +02002133 string command;
Dees_Troy38bd7602012-09-14 13:33:53 -04002134
Matt Mower18794c82015-11-11 16:22:45 -06002135 if (TWFunc::Path_Exists("/sbin/mkfs.fat")) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002136 if (!UnMount(true))
2137 return false;
2138
Greg Wallace4b44fef2015-12-29 15:33:24 -05002139 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mkfs.fat"));
Dees_Troy38bd7602012-09-14 13:33:53 -04002140 Find_Actual_Block_Device();
Matt Mower18794c82015-11-11 16:22:45 -06002141 command = "mkfs.fat " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02002142 if (TWFunc::Exec_Cmd(command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08002143 Current_File_System = "vfat";
Dees_Troye58d5262012-09-21 12:27:57 -04002144 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05002145 gui_msg("done=Done.");
Dees_Troy38bd7602012-09-14 13:33:53 -04002146 return true;
2147 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002148 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04002149 return false;
2150 }
2151 return true;
2152 }
2153 else
2154 return Wipe_RMRF();
2155
2156 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04002157}
2158
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002159bool TWPartition::Wipe_EXFAT() {
Vojtech Bocek05534202013-09-11 08:11:56 +02002160 string command;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002161
2162 if (TWFunc::Path_Exists("/sbin/mkexfatfs")) {
2163 if (!UnMount(true))
2164 return false;
2165
Greg Wallace4b44fef2015-12-29 15:33:24 -05002166 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mkexfatfs"));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002167 Find_Actual_Block_Device();
2168 command = "mkexfatfs " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02002169 if (TWFunc::Exec_Cmd(command) == 0) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002170 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05002171 gui_msg("done=Done.");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002172 return true;
2173 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002174 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002175 return false;
2176 }
2177 return true;
2178 }
2179 return false;
2180}
2181
Dees_Troy38bd7602012-09-14 13:33:53 -04002182bool TWPartition::Wipe_MTD() {
2183 if (!UnMount(true))
2184 return false;
2185
Greg Wallace4b44fef2015-12-29 15:33:24 -05002186 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("MTD"));
Dees_Troy38bd7602012-09-14 13:33:53 -04002187
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02002188 mtd_scan_partitions();
2189 const MtdPartition* mtd = mtd_find_partition_by_name(MTD_Name.c_str());
2190 if (mtd == NULL) {
2191 LOGERR("No mtd partition named '%s'", MTD_Name.c_str());
2192 return false;
2193 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002194
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02002195 MtdWriteContext* ctx = mtd_write_partition(mtd);
2196 if (ctx == NULL) {
2197 LOGERR("Can't write '%s', failed to format.", MTD_Name.c_str());
2198 return false;
2199 }
2200 if (mtd_erase_blocks(ctx, -1) == -1) {
2201 mtd_write_close(ctx);
2202 LOGERR("Failed to format '%s'", MTD_Name.c_str());
2203 return false;
2204 }
2205 if (mtd_write_close(ctx) != 0) {
2206 LOGERR("Failed to close '%s'", MTD_Name.c_str());
2207 return false;
2208 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08002209 Current_File_System = "yaffs2";
Dees_Troye58d5262012-09-21 12:27:57 -04002210 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05002211 gui_msg("done=Done.");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02002212 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002213}
2214
2215bool TWPartition::Wipe_RMRF() {
Dees_Troy38bd7602012-09-14 13:33:53 -04002216 if (!Mount(true))
2217 return false;
Ethan Yonker726a0202014-12-16 20:01:38 -06002218 // This is the only wipe that leaves the partition mounted, so we
2219 // must manually remove the partition from MTP if it is a storage
2220 // partition.
2221 if (Is_Storage)
2222 PartitionManager.Remove_MTP_Storage(MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04002223
Ethan Yonker74db1572015-10-28 12:44:49 -05002224 gui_msg(Msg("remove_all=Removing all files under '{1}'")(Mount_Point));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002225 TWFunc::removeDir(Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04002226 Recreate_AndSec_Folder();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002227 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002228}
2229
Dees_Troye5017042013-08-29 16:38:55 +00002230bool TWPartition::Wipe_F2FS() {
Vojtech Bocek05534202013-09-11 08:11:56 +02002231 string command;
Dees_Troye5017042013-08-29 16:38:55 +00002232
2233 if (TWFunc::Path_Exists("/sbin/mkfs.f2fs")) {
2234 if (!UnMount(true))
2235 return false;
2236
Greg Wallace4b44fef2015-12-29 15:33:24 -05002237 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mkfs.f2fs"));
Dees_Troye5017042013-08-29 16:38:55 +00002238 Find_Actual_Block_Device();
Greg Wallace558aef72016-01-19 21:14:30 -05002239 command = "mkfs.f2fs -t 0";
dhacker29a3fa75f2015-01-17 19:42:33 -05002240 if (!Is_Decrypted && Length != 0) {
2241 // Only use length if we're not decrypted
2242 char len[32];
2243 int mod_length = Length;
2244 if (Length < 0)
2245 mod_length *= -1;
2246 sprintf(len, "%i", mod_length);
2247 command += " -r ";
2248 command += len;
2249 }
2250 command += " " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02002251 if (TWFunc::Exec_Cmd(command) == 0) {
Dees_Troye5017042013-08-29 16:38:55 +00002252 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05002253 gui_msg("done=Done.");
Dees_Troye5017042013-08-29 16:38:55 +00002254 return true;
2255 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002256 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troye5017042013-08-29 16:38:55 +00002257 return false;
2258 }
2259 return true;
2260 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002261 LOGINFO("mkfs.f2fs binary not found, using rm -rf to wipe.\n");
Dees_Troye5017042013-08-29 16:38:55 +00002262 return Wipe_RMRF();
2263 }
2264 return false;
2265}
2266
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002267bool TWPartition::Wipe_NTFS() {
2268 string command;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002269 string Ntfsmake_Binary;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002270
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002271 if (TWFunc::Path_Exists("/sbin/mkntfs"))
2272 Ntfsmake_Binary = "mkntfs";
2273 else if (TWFunc::Path_Exists("/sbin/mkfs.ntfs"))
2274 Ntfsmake_Binary = "mkfs.ntfs";
2275 else
2276 return false;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002277
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002278 if (!UnMount(true))
2279 return false;
2280
Greg Wallace4b44fef2015-12-29 15:33:24 -05002281 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)(Ntfsmake_Binary));
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002282 Find_Actual_Block_Device();
2283 command = "/sbin/" + Ntfsmake_Binary + " " + Actual_Block_Device;
2284 if (TWFunc::Exec_Cmd(command) == 0) {
2285 Recreate_AndSec_Folder();
2286 gui_msg("done=Done.");
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002287 return true;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002288 } else {
2289 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
2290 return false;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002291 }
2292 return false;
2293}
2294
Dees_Troy51a0e822012-09-05 15:24:24 -04002295bool TWPartition::Wipe_Data_Without_Wiping_Media() {
Ethan Yonker83e82572014-04-04 10:59:28 -05002296#ifdef TW_OEM_BUILD
2297 // In an OEM Build we want to do a full format
2298 return Wipe_Encryption();
2299#else
Ethan Yonker66a19492015-12-10 10:19:45 -06002300 bool ret = false;
Dees_Troy38bd7602012-09-14 13:33:53 -04002301
Dees_Troy38bd7602012-09-14 13:33:53 -04002302 if (!Mount(true))
2303 return false;
2304
Ethan Yonker74db1572015-10-28 12:44:49 -05002305 gui_msg("wiping_data=Wiping data without wiping /data/media ...");
Ethan Yonker66a19492015-12-10 10:19:45 -06002306 ret = Wipe_Data_Without_Wiping_Media_Func(Mount_Point + "/");
2307 if (ret)
2308 gui_msg("done=Done.");
2309 return ret;
2310#endif // ifdef TW_OEM_BUILD
2311}
2312
2313bool TWPartition::Wipe_Data_Without_Wiping_Media_Func(const string& parent __unused) {
2314 string dir;
Dees_Troy38bd7602012-09-14 13:33:53 -04002315
2316 DIR* d;
Ethan Yonker66a19492015-12-10 10:19:45 -06002317 d = opendir(parent.c_str());
Dees_Troy16b74352012-11-14 22:27:31 +00002318 if (d != NULL) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002319 struct dirent* de;
2320 while ((de = readdir(d)) != NULL) {
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002321 if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) continue;
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05002322
Ethan Yonker66a19492015-12-10 10:19:45 -06002323 dir = parent;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002324 dir.append(de->d_name);
Ethan Yonker3fdcda42016-11-30 12:29:37 -06002325 if (wipe_exclusions.check_skip_dirs(dir)) {
Ethan Yonker66a19492015-12-10 10:19:45 -06002326 LOGINFO("skipped '%s'\n", dir.c_str());
2327 continue;
2328 }
Dees_Troyce675462013-01-09 19:48:21 +00002329 if (de->d_type == DT_DIR) {
Ethan Yonker66a19492015-12-10 10:19:45 -06002330 dir.append("/");
2331 if (!Wipe_Data_Without_Wiping_Media_Func(dir)) {
2332 closedir(d);
2333 return false;
2334 }
2335 rmdir(dir.c_str());
bigbiff bigbiff98f1f902013-01-19 18:46:13 -05002336 } 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 +03002337 if (unlink(dir.c_str()) != 0)
Ethan Yonker79f88bd2016-12-09 14:52:12 -06002338 LOGINFO("Unable to unlink '%s': %s\n", dir.c_str(), strerror(errno));
Dees_Troyce675462013-01-09 19:48:21 +00002339 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002340 }
2341 closedir(d);
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05002342
Dees_Troy16b74352012-11-14 22:27:31 +00002343 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -04002344 }
Ethan Yonker74db1572015-10-28 12:44:49 -05002345 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy16b74352012-11-14 22:27:31 +00002346 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04002347}
2348
bigbiffce8f83c2015-12-12 18:30:21 -05002349bool TWPartition::Backup_Tar(PartitionSettings *part_settings, pid_t *tar_fork_pid) {
Ethan Yonker472f5062016-02-25 13:47:30 -06002350 string Full_FileName;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002351 twrpTar tar;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05002352
Dees_Troy43d8b002012-09-17 16:00:01 -04002353 if (!Mount(true))
2354 return false;
2355
Dees_Troya13d74f2013-03-24 08:54:55 -05002356 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Backup_Display_Name, "Backing Up");
Ethan Yonker74db1572015-10-28 12:44:49 -05002357 gui_msg(Msg("backing_up=Backing up {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002358
Ethan Yonker472f5062016-02-25 13:47:30 -06002359 DataManager::GetValue(TW_USE_COMPRESSION_VAR, tar.use_compression);
Matt Mowerbb81e5d2014-03-20 18:05:41 -05002360
Dees_Troy83bd4832013-05-04 12:39:56 +00002361#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
Ethan Yonker472f5062016-02-25 13:47:30 -06002362 if (Can_Encrypt_Backup) {
2363 DataManager::GetValue("tw_encrypt_backup", tar.use_encryption);
2364 if (tar.use_encryption) {
2365 if (Use_Userdata_Encryption)
2366 tar.userdata_encryption = tar.use_encryption;
2367 string Password;
2368 DataManager::GetValue("tw_backup_password", Password);
2369 tar.setpassword(Password);
2370 } else {
2371 tar.use_encryption = 0;
2372 }
Dees_Troy83bd4832013-05-04 12:39:56 +00002373 }
2374#endif
Dees_Troy43d8b002012-09-17 16:00:01 -04002375
Ethan Yonker472f5062016-02-25 13:47:30 -06002376 Backup_FileName = Backup_Name + "." + Current_File_System + ".win";
Ethan Yonkere080c1f2016-09-19 13:50:25 -05002377 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
Ethan Yonker01f4e032017-02-03 15:30:52 -06002378 if (Has_Data_Media)
2379 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 -05002380 tar.part_settings = part_settings;
Ethan Yonker3fdcda42016-11-30 12:29:37 -06002381 tar.backup_exclusions = &backup_exclusions;
Dees Troye0a433a2013-12-02 04:10:37 +00002382 tar.setdir(Backup_Path);
2383 tar.setfn(Full_FileName);
2384 tar.setsize(Backup_Size);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002385 tar.partition_name = Backup_Name;
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002386 tar.backup_folder = part_settings->Backup_Folder;
bigbiffce8f83c2015-12-12 18:30:21 -05002387 if (tar.createTarFork(tar_fork_pid) != 0)
Dees Troye0a433a2013-12-02 04:10:37 +00002388 return false;
Dees_Troy43d8b002012-09-17 16:00:01 -04002389 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002390}
2391
bigbiffce8f83c2015-12-12 18:30:21 -05002392bool TWPartition::Backup_Image(PartitionSettings *part_settings) {
2393 string Full_FileName, adb_file_name;
igoriok87e3d932013-01-31 21:03:53 +02002394
Ethan Yonker74db1572015-10-28 12:44:49 -05002395 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, gui_parse_text("{@backing}"));
2396 gui_msg(Msg("backing_up=Backing up {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002397
Ethan Yonker472f5062016-02-25 13:47:30 -06002398 Backup_FileName = Backup_Name + "." + Current_File_System + ".win";
Dees_Troy43d8b002012-09-17 16:00:01 -04002399
bigbiffce8f83c2015-12-12 18:30:21 -05002400 if (part_settings->adbbackup) {
2401 Full_FileName = TW_ADB_BACKUP;
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002402 adb_file_name = part_settings->Backup_Folder + "/" + Backup_FileName;
bigbiffce8f83c2015-12-12 18:30:21 -05002403 }
2404 else
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002405 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
bigbiffce8f83c2015-12-12 18:30:21 -05002406
2407 part_settings->total_restore_size = Backup_Size;
2408
2409 if (part_settings->adbbackup) {
2410 if (!twadbbu::Write_TWIMG(adb_file_name, Backup_Size))
2411 return false;
2412 }
2413
2414 if (!Raw_Read_Write(part_settings))
Ethan Yonker472f5062016-02-25 13:47:30 -06002415 return false;
2416
bigbiffce8f83c2015-12-12 18:30:21 -05002417 if (part_settings->adbbackup) {
2418 if (!twadbbu::Write_TWEOF())
2419 return false;
Dees_Troy7c2dec82012-09-26 09:49:14 -04002420 }
Dees_Troy43d8b002012-09-17 16:00:01 -04002421 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002422}
2423
bigbiffce8f83c2015-12-12 18:30:21 -05002424bool TWPartition::Raw_Read_Write(PartitionSettings *part_settings) {
2425 unsigned long long RW_Block_Size, Remain = Backup_Size;
2426 int src_fd = -1, dest_fd = -1;
2427 ssize_t bs;
Ethan Yonker472f5062016-02-25 13:47:30 -06002428 bool ret = false;
2429 void* buffer = NULL;
2430 unsigned long long backedup_size = 0;
bigbiffce8f83c2015-12-12 18:30:21 -05002431 string srcfn, destfn;
Ethan Yonker472f5062016-02-25 13:47:30 -06002432
bigbiffce8f83c2015-12-12 18:30:21 -05002433 if (part_settings->PM_Method == PM_BACKUP) {
2434 srcfn = Actual_Block_Device;
2435 if (part_settings->adbbackup)
2436 destfn = TW_ADB_BACKUP;
bigbiff bigbiff38b83c12017-12-28 19:58:52 -05002437 else {
Ethan Yonkere080c1f2016-09-19 13:50:25 -05002438 destfn = part_settings->Backup_Folder + "/" + Backup_FileName;
bigbiff bigbiff38b83c12017-12-28 19:58:52 -05002439 }
bigbiffce8f83c2015-12-12 18:30:21 -05002440 }
2441 else {
2442 destfn = Actual_Block_Device;
2443 if (part_settings->adbbackup) {
2444 srcfn = TW_ADB_RESTORE;
2445 } else {
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002446 srcfn = part_settings->Backup_Folder + "/" + Backup_FileName;
bigbiffce8f83c2015-12-12 18:30:21 -05002447 Remain = TWFunc::Get_File_Size(srcfn);
2448 }
2449 }
Ethan Yonker472f5062016-02-25 13:47:30 -06002450
bigbiffce8f83c2015-12-12 18:30:21 -05002451 src_fd = open(srcfn.c_str(), O_RDONLY | O_LARGEFILE);
Ethan Yonker472f5062016-02-25 13:47:30 -06002452 if (src_fd < 0) {
bigbiffce8f83c2015-12-12 18:30:21 -05002453 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(srcfn.c_str())(strerror(errno)));
Ethan Yonker472f5062016-02-25 13:47:30 -06002454 return false;
2455 }
bigbiffce8f83c2015-12-12 18:30:21 -05002456
2457 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 -06002458 if (dest_fd < 0) {
bigbiffce8f83c2015-12-12 18:30:21 -05002459 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(destfn.c_str())(strerror(errno)));
Ethan Yonker472f5062016-02-25 13:47:30 -06002460 goto exit;
2461 }
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -04002462
bigbiffce8f83c2015-12-12 18:30:21 -05002463 LOGINFO("Reading '%s', writing '%s'\n", srcfn.c_str(), destfn.c_str());
2464
2465 if (part_settings->adbbackup) {
2466 RW_Block_Size = MAX_ADB_READ;
2467 bs = MAX_ADB_READ;
2468 }
2469 else {
2470 RW_Block_Size = 1048576LLU; // 1MB
2471 bs = (ssize_t)(RW_Block_Size);
2472 }
2473
Ethan Yonker472f5062016-02-25 13:47:30 -06002474 buffer = malloc((size_t)bs);
2475 if (!buffer) {
2476 LOGINFO("Raw_Read_Write failed to malloc\n");
2477 goto exit;
2478 }
bigbiffce8f83c2015-12-12 18:30:21 -05002479
2480 if (part_settings->progress)
2481 part_settings->progress->SetPartitionSize(part_settings->total_restore_size);
2482
Ethan Yonker472f5062016-02-25 13:47:30 -06002483 while (Remain > 0) {
2484 if (Remain < RW_Block_Size)
bigbiffce8f83c2015-12-12 18:30:21 -05002485 bs = (ssize_t)(Remain);
Matt Mowera8a89d12016-12-30 18:10:37 -06002486 if (read(src_fd, buffer, bs) != bs) {
Ethan Yonker472f5062016-02-25 13:47:30 -06002487 LOGINFO("Error reading source fd (%s)\n", strerror(errno));
2488 goto exit;
2489 }
2490 if (write(dest_fd, buffer, bs) != bs) {
2491 LOGINFO("Error writing destination fd (%s)\n", strerror(errno));
2492 goto exit;
2493 }
2494 backedup_size += (unsigned long long)(bs);
Matt Mower029a82d2017-01-08 13:12:38 -06002495 Remain -= (unsigned long long)(bs);
bigbiffce8f83c2015-12-12 18:30:21 -05002496 if (part_settings->progress)
2497 part_settings->progress->UpdateSize(backedup_size);
Ethan Yonker472f5062016-02-25 13:47:30 -06002498 if (PartitionManager.Check_Backup_Cancel() != 0)
2499 goto exit;
2500 }
bigbiffce8f83c2015-12-12 18:30:21 -05002501 if (part_settings->progress)
2502 part_settings->progress->UpdateDisplayDetails(true);
Ethan Yonker472f5062016-02-25 13:47:30 -06002503 fsync(dest_fd);
nailyka083dc62016-11-11 21:41:28 +01002504
2505 if (!part_settings->adbbackup && part_settings->PM_Method == PM_BACKUP) {
2506 tw_set_default_metadata(destfn.c_str());
2507 LOGINFO("Restored default metadata for %s\n", destfn.c_str());
2508 }
2509
Ethan Yonker472f5062016-02-25 13:47:30 -06002510 ret = true;
2511exit:
2512 if (src_fd >= 0)
2513 close(src_fd);
2514 if (dest_fd >= 0)
2515 close(dest_fd);
2516 if (buffer)
2517 free(buffer);
2518 return ret;
2519}
2520
bigbiffce8f83c2015-12-12 18:30:21 -05002521bool TWPartition::Backup_Dump_Image(PartitionSettings *part_settings) {
Vojtech Bocek05534202013-09-11 08:11:56 +02002522 string Full_FileName, Command;
Dees_Troy43d8b002012-09-17 16:00:01 -04002523
Ethan Yonker74db1572015-10-28 12:44:49 -05002524 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, gui_parse_text("{@backing}"));
2525 gui_msg(Msg("backing_up=Backing up {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002526
bigbiffce8f83c2015-12-12 18:30:21 -05002527 if (part_settings->progress)
2528 part_settings->progress->SetPartitionSize(Backup_Size);
Dees_Troy43d8b002012-09-17 16:00:01 -04002529
Ethan Yonker472f5062016-02-25 13:47:30 -06002530 Backup_FileName = Backup_Name + "." + Current_File_System + ".win";
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002531 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04002532
2533 Command = "dump_image " + MTD_Name + " '" + Full_FileName + "'";
bigbiffce8f83c2015-12-12 18:30:21 -05002534
Dees_Troy2673cec2013-04-02 20:22:16 +00002535 LOGINFO("Backup command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02002536 TWFunc::Exec_Cmd(Command);
Ethan Yonker4b94cfd2014-12-11 10:00:45 -06002537 tw_set_default_metadata(Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04002538 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
2539 // 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 -06002540 gui_msg(Msg(msg::kError, "backup_size=Backup file size for '{1}' is 0 bytes.")(Full_FileName));
Dees_Troy7c2dec82012-09-26 09:49:14 -04002541 return false;
2542 }
bigbiffce8f83c2015-12-12 18:30:21 -05002543 if (part_settings->progress)
2544 part_settings->progress->UpdateSize(Backup_Size);
2545
Dees_Troy43d8b002012-09-17 16:00:01 -04002546 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002547}
2548
bigbiffce8f83c2015-12-12 18:30:21 -05002549unsigned long long TWPartition::Get_Restore_Size(PartitionSettings *part_settings) {
2550 if (!part_settings->adbbackup) {
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002551 InfoManager restore_info(part_settings->Backup_Folder + "/" + Backup_Name + ".info");
bigbiffce8f83c2015-12-12 18:30:21 -05002552 if (restore_info.LoadValues() == 0) {
2553 if (restore_info.GetValue("backup_size", Restore_Size) == 0) {
2554 LOGINFO("Read info file, restore size is %llu\n", Restore_Size);
2555 return Restore_Size;
2556 }
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002557 }
2558 }
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002559
Matt Mower029a82d2017-01-08 13:12:38 -06002560 string Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
2561 string Restore_File_System = Get_Restore_File_System(part_settings);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002562
2563 if (Is_Image(Restore_File_System)) {
2564 Restore_Size = TWFunc::Get_File_Size(Full_FileName);
2565 return Restore_Size;
2566 }
2567
2568 twrpTar tar;
2569 tar.setdir(Backup_Path);
2570 tar.setfn(Full_FileName);
bigbiffce8f83c2015-12-12 18:30:21 -05002571 tar.backup_name = Full_FileName;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002572#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
2573 string Password;
2574 DataManager::GetValue("tw_restore_password", Password);
2575 if (!Password.empty())
2576 tar.setpassword(Password);
2577#endif
2578 tar.partition_name = Backup_Name;
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002579 tar.backup_folder = part_settings->Backup_Folder;
bigbiffce8f83c2015-12-12 18:30:21 -05002580 tar.part_settings = part_settings;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002581 Restore_Size = tar.get_size();
2582 return Restore_Size;
2583}
2584
bigbiffce8f83c2015-12-12 18:30:21 -05002585bool TWPartition::Restore_Tar(PartitionSettings *part_settings) {
Ethan Yonker472f5062016-02-25 13:47:30 -06002586 string Full_FileName;
Dees Troy4159aed2014-02-28 17:24:43 +00002587 bool ret = false;
bigbiffce8f83c2015-12-12 18:30:21 -05002588 string Restore_File_System = Get_Restore_File_System(part_settings);
Dees_Troy43d8b002012-09-17 16:00:01 -04002589
Dees_Troye58d5262012-09-21 12:27:57 -04002590 if (Has_Android_Secure) {
Dees_Troye58d5262012-09-21 12:27:57 -04002591 if (!Wipe_AndSec())
2592 return false;
Gary Peck43acadf2012-11-21 21:19:01 -08002593 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002594 gui_msg(Msg("wiping=Wiping {1}")(Backup_Display_Name));
Ethan Yonker5eac2222014-06-11 12:22:55 -05002595 if (Has_Data_Media && Mount_Point == "/data" && Restore_File_System != Current_File_System) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002596 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 -05002597 if (!Wipe_Data_Without_Wiping_Media())
2598 return false;
2599 } else {
2600 if (!Wipe(Restore_File_System))
2601 return false;
2602 }
Dees_Troye58d5262012-09-21 12:27:57 -04002603 }
Matt Mower3c366972015-12-25 19:28:31 -06002604 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Backup_Display_Name, gui_parse_text("{@restoring_hdr}"));
Ethan Yonker74db1572015-10-28 12:44:49 -05002605 gui_msg(Msg("restoring=Restoring {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002606
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05002607 // Remount as read/write as needed so we can restore the backup
2608 if (!ReMount_RW(true))
Dees_Troy43d8b002012-09-17 16:00:01 -04002609 return false;
2610
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002611 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
Ethan Yonker87af5632014-02-10 11:56:35 -06002612 twrpTar tar;
bigbiffce8f83c2015-12-12 18:30:21 -05002613 tar.part_settings = part_settings;
Ethan Yonker87af5632014-02-10 11:56:35 -06002614 tar.setdir(Backup_Path);
2615 tar.setfn(Full_FileName);
2616 tar.backup_name = Backup_Name;
2617#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
2618 string Password;
2619 DataManager::GetValue("tw_restore_password", Password);
2620 if (!Password.empty())
2621 tar.setpassword(Password);
2622#endif
bigbiffce8f83c2015-12-12 18:30:21 -05002623 part_settings->progress->SetPartitionSize(Get_Restore_Size(part_settings));
2624 if (tar.extractTarFork() != 0)
Dees Troy4159aed2014-02-28 17:24:43 +00002625 ret = false;
2626 else
2627 ret = true;
2628#ifdef HAVE_CAPABILITIES
2629 // Restore capabilities to the run-as binary
2630 if (Mount_Point == "/system" && Mount(true) && TWFunc::Path_Exists("/system/bin/run-as")) {
2631 struct vfs_cap_data cap_data;
2632 uint64_t capabilities = (1 << CAP_SETUID) | (1 << CAP_SETGID);
2633
2634 memset(&cap_data, 0, sizeof(cap_data));
2635 cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE;
2636 cap_data.data[0].permitted = (uint32_t) (capabilities & 0xffffffff);
2637 cap_data.data[0].inheritable = 0;
2638 cap_data.data[1].permitted = (uint32_t) (capabilities >> 32);
2639 cap_data.data[1].inheritable = 0;
2640 if (setxattr("/system/bin/run-as", XATTR_NAME_CAPS, &cap_data, sizeof(cap_data), 0) < 0) {
2641 LOGINFO("Failed to reset capabilities of /system/bin/run-as binary.\n");
2642 } else {
2643 LOGINFO("Reset capabilities of /system/bin/run-as binary successful.\n");
2644 }
2645 }
2646#endif
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05002647 if (Mount_Read_Only || Mount_Flags & MS_RDONLY)
2648 // Remount as read only when restoration is complete
2649 ReMount(true);
2650
Dees Troy4159aed2014-02-28 17:24:43 +00002651 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04002652}
2653
bigbiffce8f83c2015-12-12 18:30:21 -05002654bool TWPartition::Restore_Image(PartitionSettings *part_settings) {
Ethan Yonker96af84a2015-01-05 14:58:36 -06002655 string Full_FileName;
bigbiffce8f83c2015-12-12 18:30:21 -05002656 string Restore_File_System = Get_Restore_File_System(part_settings);
Dees_Troy43d8b002012-09-17 16:00:01 -04002657
Matt Mower3c366972015-12-25 19:28:31 -06002658 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Backup_Display_Name, gui_parse_text("{@restoring_hdr}"));
Ethan Yonker74db1572015-10-28 12:44:49 -05002659 gui_msg(Msg("restoring=Restoring {1}...")(Backup_Display_Name));
bigbiffce8f83c2015-12-12 18:30:21 -05002660
2661 if (part_settings->adbbackup)
2662 Full_FileName = TW_ADB_RESTORE;
2663 else
Ethan Yonkere080c1f2016-09-19 13:50:25 -05002664 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
Gary Peck15e623d2012-11-21 21:07:58 -08002665
Ethan Yonker96af84a2015-01-05 14:58:36 -06002666 if (Restore_File_System == "emmc") {
bigbiffce8f83c2015-12-12 18:30:21 -05002667 if (!part_settings->adbbackup)
2668 part_settings->total_restore_size = (uint64_t)(TWFunc::Get_File_Size(Full_FileName));
2669 if (!Raw_Read_Write(part_settings))
Ethan Yonker96af84a2015-01-05 14:58:36 -06002670 return false;
2671 } else if (Restore_File_System == "mtd" || Restore_File_System == "bml") {
bigbiffce8f83c2015-12-12 18:30:21 -05002672 if (!Flash_Image_FI(Full_FileName, part_settings->progress))
2673 return false;
2674 }
2675
2676 if (part_settings->adbbackup) {
2677 if (!twadbbu::Write_TWEOF())
Ethan Yonker96af84a2015-01-05 14:58:36 -06002678 return false;
Gary Peck15e623d2012-11-21 21:07:58 -08002679 }
Dees_Troy43d8b002012-09-17 16:00:01 -04002680 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002681}
Dees_Troy5bf43922012-09-07 16:07:55 -04002682
2683bool TWPartition::Update_Size(bool Display_Error) {
Dees_Troy0550cfb2012-10-13 11:56:13 -04002684 bool ret = false, Was_Already_Mounted = false;
Dees_Troy51127312012-09-08 13:08:49 -04002685
Ethan Yonker1b190162016-12-05 15:25:19 -06002686 Find_Actual_Block_Device();
2687
2688 if (!Can_Be_Mounted && !Is_Encrypted) {
2689 if (TWFunc::Path_Exists(Actual_Block_Device) && Find_Partition_Size()) {
2690 Used = Size;
2691 Backup_Size = Size;
2692 return true;
2693 }
Dees_Troy5bf43922012-09-07 16:07:55 -04002694 return false;
Ethan Yonker1b190162016-12-05 15:25:19 -06002695 }
Dees_Troy5bf43922012-09-07 16:07:55 -04002696
Dees_Troy0550cfb2012-10-13 11:56:13 -04002697 Was_Already_Mounted = Is_Mounted();
Dees_Troy38bd7602012-09-14 13:33:53 -04002698 if (Removable || Is_Encrypted) {
2699 if (!Mount(false))
2700 return true;
2701 } else if (!Mount(Display_Error))
Dees_Troy5bf43922012-09-07 16:07:55 -04002702 return false;
Dees_Troy51127312012-09-08 13:08:49 -04002703
2704 ret = Get_Size_Via_statfs(Display_Error);
Dees_Troy0550cfb2012-10-13 11:56:13 -04002705 if (!ret || Size == 0) {
2706 if (!Get_Size_Via_df(Display_Error)) {
2707 if (!Was_Already_Mounted)
2708 UnMount(false);
Dees_Troy51127312012-09-08 13:08:49 -04002709 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04002710 }
2711 }
Dees_Troy51127312012-09-08 13:08:49 -04002712
Dees_Troy5bf43922012-09-07 16:07:55 -04002713 if (Has_Data_Media) {
2714 if (Mount(Display_Error)) {
Ethan Yonker3fdcda42016-11-30 12:29:37 -06002715 Used = backup_exclusions.Get_Folder_Size(Mount_Point);
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002716 Backup_Size = Used;
2717 int bak = (int)(Used / 1048576LLU);
Dees_Troy51127312012-09-08 13:08:49 -04002718 int fre = (int)(Free / 1048576LLU);
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002719 LOGINFO("Data backup size is %iMB, free: %iMB.\n", bak, fre);
Dees_Troy0550cfb2012-10-13 11:56:13 -04002720 } else {
2721 if (!Was_Already_Mounted)
2722 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04002723 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04002724 }
Dees_Troye58d5262012-09-21 12:27:57 -04002725 } else if (Has_Android_Secure) {
2726 if (Mount(Display_Error))
Ethan Yonker3fdcda42016-11-30 12:29:37 -06002727 Backup_Size = backup_exclusions.Get_Folder_Size(Backup_Path);
Dees_Troy0550cfb2012-10-13 11:56:13 -04002728 else {
2729 if (!Was_Already_Mounted)
2730 UnMount(false);
Dees_Troye58d5262012-09-21 12:27:57 -04002731 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04002732 }
Dees_Troy5bf43922012-09-07 16:07:55 -04002733 }
Dees_Troy0550cfb2012-10-13 11:56:13 -04002734 if (!Was_Already_Mounted)
2735 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04002736 return true;
Dees_Troy51127312012-09-08 13:08:49 -04002737}
Dees_Troy38bd7602012-09-14 13:33:53 -04002738
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06002739bool TWPartition::Find_Wildcard_Block_Devices(const string& Device) {
2740 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
2741 string Path = TWFunc::Get_Path(Device);
2742 string Dev = TWFunc::Get_Filename(Device);
2743 size_t wildcard_index = Dev.find("*");
2744 if (wildcard_index != string::npos)
2745 Dev = Dev.substr(0, wildcard_index);
2746 wildcard_index = Dev.size();
2747 DIR* d = opendir(Path.c_str());
2748 if (d == NULL) {
2749 LOGINFO("Error opening '%s': %s\n", Path.c_str(), strerror(errno));
2750 return false;
2751 }
2752 struct dirent* de;
2753 while ((de = readdir(d)) != NULL) {
2754 if (de->d_type != DT_BLK || strlen(de->d_name) <= wildcard_index || strncmp(de->d_name, Dev.c_str(), wildcard_index) != 0)
2755 continue;
2756
2757 string item = Path + "/";
2758 item.append(de->d_name);
2759 if (PartitionManager.Find_Partition_By_Block_Device(item))
2760 continue;
2761 TWPartition *part = new TWPartition;
2762 char buffer[MAX_FSTAB_LINE_LENGTH];
2763 sprintf(buffer, "%s %s-%i auto defaults defaults", item.c_str(), Mount_Point.c_str(), ++mount_point_index);
2764 part->Process_Fstab_Line(buffer, false, NULL);
2765 char display[MAX_FSTAB_LINE_LENGTH];
2766 sprintf(display, "%s %i", Storage_Name.c_str(), mount_point_index);
2767 part->Storage_Name = display;
2768 part->Display_Name = display;
2769 part->Primary_Block_Device = item;
2770 part->Wildcard_Block_Device = false;
2771 part->Is_SubPartition = true;
2772 part->SubPartition_Of = Mount_Point;
2773 part->Is_Storage = Is_Storage;
2774 part->Can_Be_Mounted = true;
2775 part->Removable = true;
2776 part->Can_Be_Wiped = Can_Be_Wiped;
2777 part->Wipe_Available_in_GUI = Wipe_Available_in_GUI;
2778 part->Find_Actual_Block_Device();
2779 part->Update_Size(false);
2780 Has_SubPartition = true;
2781 PartitionManager.Output_Partition(part);
2782 PartitionManager.Add_Partition(part);
2783 }
2784 closedir(d);
2785 return (mount_point_index > 0);
2786}
2787
Dees_Troy38bd7602012-09-14 13:33:53 -04002788void TWPartition::Find_Actual_Block_Device(void) {
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06002789 if (!Sysfs_Entry.empty() && Primary_Block_Device.empty() && Decrypted_Block_Device.empty()) {
2790 /* Sysfs_Entry.empty() indicates if this is a sysfs entry that begins with /device/
2791 * If we have a syfs entry then we are looking for this device from a uevent add.
2792 * The uevent add will set the primary block device based on the data we receive from
2793 * after checking for adopted storage. If the device ends up being adopted, then the
2794 * decrypted block device will be set instead of the primary block device. */
2795 Is_Present = false;
2796 return;
2797 }
2798 if (Wildcard_Block_Device && !Is_Adopted_Storage) {
2799 Is_Present = false;
2800 Actual_Block_Device = "";
2801 Can_Be_Mounted = false;
2802 if (!Find_Wildcard_Block_Devices(Primary_Block_Device)) {
2803 string Dev = Primary_Block_Device.substr(0, Primary_Block_Device.find("*"));
2804 if (TWFunc::Path_Exists(Dev)) {
2805 Is_Present = true;
2806 Can_Be_Mounted = true;
2807 Actual_Block_Device = Dev;
2808 }
2809 }
2810 return;
2811 } else if (Is_Decrypted && !Decrypted_Block_Device.empty()) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002812 Actual_Block_Device = Decrypted_Block_Device;
Ethan Yonker1b190162016-12-05 15:25:19 -06002813 if (TWFunc::Path_Exists(Decrypted_Block_Device)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002814 Is_Present = true;
Ethan Yonker1b190162016-12-05 15:25:19 -06002815 return;
2816 }
2817 } else if (SlotSelect && TWFunc::Path_Exists(Primary_Block_Device + PartitionManager.Get_Active_Slot_Suffix())) {
2818 Actual_Block_Device = Primary_Block_Device + PartitionManager.Get_Active_Slot_Suffix();
2819 unlink(Primary_Block_Device.c_str());
2820 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
2821 Is_Present = true;
2822 return;
Dees_Troy43d8b002012-09-17 16:00:01 -04002823 } else if (TWFunc::Path_Exists(Primary_Block_Device)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002824 Is_Present = true;
2825 Actual_Block_Device = Primary_Block_Device;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002826 return;
2827 }
Ethan Yonker1b190162016-12-05 15:25:19 -06002828 if (!Alternate_Block_Device.empty() && TWFunc::Path_Exists(Alternate_Block_Device)) {
Dees_Troy3f04d032012-10-07 18:20:09 -04002829 Actual_Block_Device = Alternate_Block_Device;
Dees_Troy38bd7602012-09-14 13:33:53 -04002830 Is_Present = true;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002831 } else {
Dees_Troy38bd7602012-09-14 13:33:53 -04002832 Is_Present = false;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002833 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002834}
2835
2836void TWPartition::Recreate_Media_Folder(void) {
2837 string Command;
Ethan Yonker66a19492015-12-10 10:19:45 -06002838 string Media_Path = Mount_Point + "/media";
Dees_Troy38bd7602012-09-14 13:33:53 -04002839
Ethan Yonkerbd7492d2016-12-07 13:55:01 -06002840 if (Is_FBE) {
2841 LOGINFO("Not recreating media folder on FBE\n");
2842 return;
2843 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002844 if (!Mount(true)) {
Ethan Yonker66a19492015-12-10 10:19:45 -06002845 gui_msg(Msg(msg::kError, "recreate_folder_err=Unable to recreate {1} folder.")(Media_Path));
2846 } else if (!TWFunc::Path_Exists(Media_Path)) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002847 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Ethan Yonker66a19492015-12-10 10:19:45 -06002848 LOGINFO("Recreating %s folder.\n", Media_Path.c_str());
2849 mkdir(Media_Path.c_str(), 0770);
xiaolu9416f4f2015-06-04 08:22:23 +08002850 string Internal_path = DataManager::GetStrValue("tw_internal_path");
2851 if (!Internal_path.empty()) {
2852 LOGINFO("Recreating %s folder.\n", Internal_path.c_str());
2853 mkdir(Internal_path.c_str(), 0770);
2854 }
2855#ifdef TW_INTERNAL_STORAGE_PATH
2856 mkdir(EXPAND(TW_INTERNAL_STORAGE_PATH), 0770);
2857#endif
Matt Mower87413642017-01-17 21:14:46 -06002858
thata3d31fb2014-12-21 22:27:40 +01002859 // Afterwards, we will try to set the
2860 // default metadata that we were hopefully able to get during
2861 // early boot.
Ethan Yonker66a19492015-12-10 10:19:45 -06002862 tw_set_default_metadata(Media_Path.c_str());
xiaolu9416f4f2015-06-04 08:22:23 +08002863 if (!Internal_path.empty())
2864 tw_set_default_metadata(Internal_path.c_str());
Matt Mower87413642017-01-17 21:14:46 -06002865
Ethan Yonker5eac2222014-06-11 12:22:55 -05002866 // Toggle mount to ensure that "internal sdcard" gets mounted
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002867 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Ethan Yonker5eac2222014-06-11 12:22:55 -05002868 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Dees_Troy38bd7602012-09-14 13:33:53 -04002869 }
Dees_Troy43d8b002012-09-17 16:00:01 -04002870}
Dees_Troye58d5262012-09-21 12:27:57 -04002871
2872void TWPartition::Recreate_AndSec_Folder(void) {
Dees_Troye58d5262012-09-21 12:27:57 -04002873 if (!Has_Android_Secure)
2874 return;
Dees_Troy2673cec2013-04-02 20:22:16 +00002875 LOGINFO("Creating %s: %s\n", Backup_Display_Name.c_str(), Symlink_Path.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -04002876 if (!Mount(true)) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002877 gui_msg(Msg(msg::kError, "recreate_folder_err=Unable to recreate {1} folder.")(Backup_Name));
Dees_Troye58d5262012-09-21 12:27:57 -04002878 } else if (!TWFunc::Path_Exists(Symlink_Path)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00002879 LOGINFO("Recreating %s folder.\n", Backup_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002880 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05002881 mkdir(Symlink_Path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002882 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04002883 }
2884}
bigbiff7cb4c332014-11-26 20:36:07 -05002885
2886uint64_t TWPartition::Get_Max_FileSize() {
2887 uint64_t maxFileSize = 0;
2888 const uint64_t constGB = (uint64_t) 1024 * 1024 * 1024;
2889 const uint64_t constTB = (uint64_t) constGB * 1024;
2890 const uint64_t constPB = (uint64_t) constTB * 1024;
bigbiff0c532032014-12-21 13:41:26 -05002891 if (Current_File_System == "ext4")
2892 maxFileSize = 16 * constTB; //16 TB
2893 else if (Current_File_System == "vfat")
2894 maxFileSize = 4 * constGB; //4 GB
2895 else if (Current_File_System == "ntfs")
2896 maxFileSize = 256 * constTB; //256 TB
2897 else if (Current_File_System == "exfat")
2898 maxFileSize = 16 * constPB; //16 PB
2899 else if (Current_File_System == "ext3")
2900 maxFileSize = 2 * constTB; //2 TB
2901 else if (Current_File_System == "f2fs")
2902 maxFileSize = 3.94 * constTB; //3.94 TB
bigbiff7cb4c332014-11-26 20:36:07 -05002903 else
2904 maxFileSize = 100000000L;
2905 return maxFileSize - 1;
2906}
2907
bigbiffce8f83c2015-12-12 18:30:21 -05002908bool TWPartition::Flash_Image(PartitionSettings *part_settings) {
2909 string Restore_File_System, full_filename;
Ethan Yonker96af84a2015-01-05 14:58:36 -06002910
Ethan Yonkere080c1f2016-09-19 13:50:25 -05002911 full_filename = part_settings->Backup_Folder + "/" + Backup_FileName;
Ethan Yonker96af84a2015-01-05 14:58:36 -06002912
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002913 LOGINFO("Image filename is: %s\n", Backup_FileName.c_str());
bigbiffce8f83c2015-12-12 18:30:21 -05002914
2915 if (Backup_Method == BM_FILES) {
Ethan Yonker96af84a2015-01-05 14:58:36 -06002916 LOGERR("Cannot flash images to file systems\n");
2917 return false;
2918 } else if (!Can_Flash_Img) {
2919 LOGERR("Cannot flash images to partitions %s\n", Display_Name.c_str());
2920 return false;
2921 } else {
2922 if (!Find_Partition_Size()) {
2923 LOGERR("Unable to find partition size for '%s'\n", Mount_Point.c_str());
2924 return false;
2925 }
bigbiffce8f83c2015-12-12 18:30:21 -05002926 unsigned long long image_size = TWFunc::Get_File_Size(full_filename);
Ethan Yonker96af84a2015-01-05 14:58:36 -06002927 if (image_size > Size) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002928 LOGINFO("Size (%llu bytes) of image '%s' is larger than target device '%s' (%llu bytes)\n",
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002929 image_size, Backup_FileName.c_str(), Actual_Block_Device.c_str(), Size);
Ethan Yonker74db1572015-10-28 12:44:49 -05002930 gui_err("img_size_err=Size of image is larger than target device");
Ethan Yonker96af84a2015-01-05 14:58:36 -06002931 return false;
2932 }
bigbiffce8f83c2015-12-12 18:30:21 -05002933 if (Backup_Method == BM_DD) {
2934 if (!part_settings->adbbackup) {
2935 if (Is_Sparse_Image(full_filename)) {
2936 return Flash_Sparse_Image(full_filename);
2937 }
Ethan Yonker472f5062016-02-25 13:47:30 -06002938 }
bigbiffce8f83c2015-12-12 18:30:21 -05002939 return Raw_Read_Write(part_settings);
2940 } else if (Backup_Method == BM_FLASH_UTILS) {
2941 return Flash_Image_FI(full_filename, NULL);
Ethan Yonker472f5062016-02-25 13:47:30 -06002942 }
Ethan Yonker96af84a2015-01-05 14:58:36 -06002943 }
2944
2945 LOGERR("Unknown flash method for '%s'\n", Mount_Point.c_str());
2946 return false;
2947}
2948
Ethan Yonker472f5062016-02-25 13:47:30 -06002949bool TWPartition::Is_Sparse_Image(const string& Filename) {
HashBanged974bb2016-01-30 14:20:09 -05002950 uint32_t magic = 0;
2951 int fd = open(Filename.c_str(), O_RDONLY);
2952 if (fd < 0) {
2953 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Filename)(strerror(errno)));
2954 return false;
2955 }
bigbiffce8f83c2015-12-12 18:30:21 -05002956
HashBanged974bb2016-01-30 14:20:09 -05002957 if (read(fd, &magic, sizeof(magic)) != sizeof(magic)) {
2958 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Filename)(strerror(errno)));
2959 close(fd);
2960 return false;
2961 }
2962 close(fd);
Ethan Yonker472f5062016-02-25 13:47:30 -06002963 if (magic == SPARSE_HEADER_MAGIC)
2964 return true;
2965 return false;
2966}
2967
2968bool TWPartition::Flash_Sparse_Image(const string& Filename) {
2969 string Command;
2970
2971 gui_msg(Msg("flashing=Flashing {1}...")(Display_Name));
2972
2973 Command = "simg2img '" + Filename + "' '" + Actual_Block_Device + "'";
Ethan Yonker96af84a2015-01-05 14:58:36 -06002974 LOGINFO("Flash command: '%s'\n", Command.c_str());
2975 TWFunc::Exec_Cmd(Command);
2976 return true;
2977}
2978
Ethan Yonker472f5062016-02-25 13:47:30 -06002979bool TWPartition::Flash_Image_FI(const string& Filename, ProgressTracking *progress) {
Ethan Yonker96af84a2015-01-05 14:58:36 -06002980 string Command;
Ethan Yonker472f5062016-02-25 13:47:30 -06002981 unsigned long long file_size;
Ethan Yonker96af84a2015-01-05 14:58:36 -06002982
Ethan Yonker74db1572015-10-28 12:44:49 -05002983 gui_msg(Msg("flashing=Flashing {1}...")(Display_Name));
Ethan Yonker472f5062016-02-25 13:47:30 -06002984 if (progress) {
2985 file_size = (unsigned long long)(TWFunc::Get_File_Size(Filename));
2986 progress->SetPartitionSize(file_size);
2987 }
Ethan Yonker96af84a2015-01-05 14:58:36 -06002988 // Sometimes flash image doesn't like to flash due to the first 2KB matching, so we erase first to ensure that it flashes
2989 Command = "erase_image " + MTD_Name;
2990 LOGINFO("Erase command: '%s'\n", Command.c_str());
2991 TWFunc::Exec_Cmd(Command);
2992 Command = "flash_image " + MTD_Name + " '" + Filename + "'";
2993 LOGINFO("Flash command: '%s'\n", Command.c_str());
2994 TWFunc::Exec_Cmd(Command);
Ethan Yonker472f5062016-02-25 13:47:30 -06002995 if (progress)
2996 progress->UpdateSize(file_size);
Ethan Yonker96af84a2015-01-05 14:58:36 -06002997 return true;
2998}
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05002999
3000void TWPartition::Change_Mount_Read_Only(bool new_value) {
3001 Mount_Read_Only = new_value;
3002}
3003
bigbiffce8f83c2015-12-12 18:30:21 -05003004bool TWPartition::Is_Read_Only() {
3005 return Mount_Read_Only;
3006}
3007
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05003008int TWPartition::Check_Lifetime_Writes() {
3009 bool original_read_only = Mount_Read_Only;
3010 int ret = 1;
3011
3012 Mount_Read_Only = true;
3013 if (Mount(false)) {
3014 Find_Actual_Block_Device();
3015 string block = basename(Actual_Block_Device.c_str());
3016 string file = "/sys/fs/" + Current_File_System + "/" + block + "/lifetime_write_kbytes";
3017 string result;
3018 if (TWFunc::Path_Exists(file)) {
3019 if (TWFunc::read_file(file, result) != 0) {
3020 LOGINFO("Check_Lifetime_Writes of '%s' failed to read_file\n", file.c_str());
3021 } else {
3022 LOGINFO("Check_Lifetime_Writes result: '%s'\n", result.c_str());
3023 if (result == "0") {
3024 ret = 0;
3025 }
3026 }
3027 } else {
3028 LOGINFO("Check_Lifetime_Writes file does not exist '%s'\n", file.c_str());
3029 }
3030 UnMount(true);
3031 } else {
3032 LOGINFO("Check_Lifetime_Writes failed to mount '%s'\n", Mount_Point.c_str());
3033 }
3034 Mount_Read_Only = original_read_only;
3035 return ret;
3036}
Ethan Yonker66a19492015-12-10 10:19:45 -06003037
3038int TWPartition::Decrypt_Adopted() {
3039#ifdef TW_INCLUDE_CRYPTO
3040 int ret = 1;
3041 Is_Adopted_Storage = false;
3042 string Adopted_Key_File = "";
3043
3044 if (!Removable)
3045 return ret;
3046
3047 int fd = open(Alternate_Block_Device.c_str(), O_RDONLY);
3048 if (fd < 0) {
3049 LOGINFO("failed to open '%s'\n", Alternate_Block_Device.c_str());
3050 return ret;
3051 }
3052 char type_guid[80];
3053 char part_guid[80];
3054
3055 if (gpt_disk_get_partition_info(fd, 2, type_guid, part_guid) == 0) {
3056 LOGINFO("type: '%s'\n", type_guid);
3057 LOGINFO("part: '%s'\n", part_guid);
3058 Adopted_GUID = part_guid;
3059 LOGINFO("Adopted_GUID '%s'\n", Adopted_GUID.c_str());
3060 if (strcmp(type_guid, TWGptAndroidExpand) == 0) {
3061 LOGINFO("android_expand found\n");
3062 Adopted_Key_File = "/data/misc/vold/expand_";
3063 Adopted_Key_File += part_guid;
3064 Adopted_Key_File += ".key";
3065 if (TWFunc::Path_Exists(Adopted_Key_File)) {
3066 Is_Adopted_Storage = true;
3067 /* Until we find a use case for this, I think it is safe
3068 * to disable USB Mass Storage whenever adopted storage
3069 * is present.
3070 */
3071 LOGINFO("Detected adopted storage, disabling USB mass storage mode\n");
3072 DataManager::SetValue("tw_has_usb_storage", 0);
3073 }
3074 }
3075 }
3076
3077 if (Is_Adopted_Storage) {
3078 string Adopted_Block_Device = Alternate_Block_Device + "p2";
3079 if (!TWFunc::Path_Exists(Adopted_Block_Device)) {
3080 Adopted_Block_Device = Alternate_Block_Device + "2";
3081 if (!TWFunc::Path_Exists(Adopted_Block_Device)) {
3082 LOGINFO("Adopted block device does not exist\n");
3083 goto exit;
3084 }
3085 }
3086 LOGINFO("key file is '%s', block device '%s'\n", Adopted_Key_File.c_str(), Adopted_Block_Device.c_str());
3087 char crypto_blkdev[MAXPATHLEN];
3088 std::string thekey;
3089 int fdkey = open(Adopted_Key_File.c_str(), O_RDONLY);
3090 if (fdkey < 0) {
3091 LOGINFO("failed to open key file\n");
3092 goto exit;
3093 }
3094 char buf[512];
3095 ssize_t n;
3096 while ((n = read(fdkey, &buf[0], sizeof(buf))) > 0) {
3097 thekey.append(buf, n);
3098 }
3099 close(fdkey);
3100 unsigned char* key = (unsigned char*) thekey.data();
3101 cryptfs_revert_ext_volume(part_guid);
3102
3103 ret = cryptfs_setup_ext_volume(part_guid, Adopted_Block_Device.c_str(), key, thekey.size(), crypto_blkdev);
3104 if (ret == 0) {
3105 LOGINFO("adopted storage new block device: '%s'\n", crypto_blkdev);
3106 Decrypted_Block_Device = crypto_blkdev;
3107 Is_Decrypted = true;
3108 Is_Encrypted = true;
3109 Find_Actual_Block_Device();
Kjell Braden3126a112016-06-19 16:58:15 +00003110 if (!Mount_Storage_Retry(false)) {
Ethan Yonker66a19492015-12-10 10:19:45 -06003111 LOGERR("Failed to mount decrypted adopted storage device\n");
3112 Is_Decrypted = false;
3113 Is_Encrypted = false;
3114 cryptfs_revert_ext_volume(part_guid);
3115 ret = 1;
3116 } else {
Ethan Yonkerfcf3f242016-02-16 12:30:26 -06003117 UnMount(false);
3118 Has_Android_Secure = false;
3119 Symlink_Path = "";
3120 Symlink_Mount_Point = "";
3121 Backup_Name = Mount_Point.substr(1);
3122 Backup_Path = Mount_Point;
3123 TWPartition* sdext = PartitionManager.Find_Partition_By_Path("/sd-ext");
3124 if (sdext && sdext->Actual_Block_Device == Adopted_Block_Device) {
3125 LOGINFO("Removing /sd-ext from partition list due to adopted storage\n");
3126 PartitionManager.Remove_Partition_By_Path("/sd-ext");
3127 }
Ethan Yonker66a19492015-12-10 10:19:45 -06003128 Setup_Data_Media();
3129 Recreate_Media_Folder();
3130 Wipe_Available_in_GUI = true;
3131 Wipe_During_Factory_Reset = true;
3132 Can_Be_Backed_Up = true;
3133 Can_Encrypt_Backup = true;
3134 Use_Userdata_Encryption = true;
3135 Is_Storage = true;
3136 Storage_Name = "Adopted Storage";
3137 Is_SubPartition = true;
3138 SubPartition_Of = "/data";
3139 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
3140 DataManager::SetValue("tw_has_adopted_storage", 1);
3141 }
3142 } else {
3143 LOGERR("Failed to setup adopted storage decryption\n");
3144 }
3145 }
3146exit:
Matt Mower06543e32017-01-06 15:25:26 -06003147 close(fd);
Ethan Yonker66a19492015-12-10 10:19:45 -06003148 return ret;
3149#else
3150 LOGINFO("Decrypt_Adopted: no crypto support\n");
3151 return 1;
3152#endif
3153}
3154
3155void TWPartition::Revert_Adopted() {
3156#ifdef TW_INCLUDE_CRYPTO
3157 if (!Adopted_GUID.empty()) {
3158 PartitionManager.Remove_MTP_Storage(Mount_Point);
3159 UnMount(false);
3160 cryptfs_revert_ext_volume(Adopted_GUID.c_str());
3161 Is_Adopted_Storage = false;
3162 Is_Encrypted = false;
3163 Is_Decrypted = false;
3164 Decrypted_Block_Device = "";
3165 Find_Actual_Block_Device();
3166 Wipe_During_Factory_Reset = false;
3167 Can_Be_Backed_Up = false;
3168 Can_Encrypt_Backup = false;
3169 Use_Userdata_Encryption = false;
3170 Is_SubPartition = false;
3171 SubPartition_Of = "";
3172 Has_Data_Media = false;
3173 Storage_Path = Mount_Point;
3174 if (!Symlink_Mount_Point.empty()) {
3175 TWPartition* Dat = PartitionManager.Find_Partition_By_Path("/data");
3176 if (Dat) {
3177 Dat->UnMount(false);
3178 Dat->Symlink_Mount_Point = Symlink_Mount_Point;
3179 }
3180 Symlink_Mount_Point = "";
3181 }
3182 }
3183#else
3184 LOGINFO("Revert_Adopted: no crypto support\n");
3185#endif
3186}
bigbiff bigbiffb5ecaad2017-03-20 18:53:53 -04003187
3188void TWPartition::Set_Backup_FileName(string fname) {
3189 Backup_FileName = fname;
3190}
3191
3192string TWPartition::Get_Backup_Name() {
3193 return Backup_Name;
3194}