blob: 359d70c86a0a2a1b492e89203e66990ebdc412c3 [file] [log] [blame]
Dees Troy3be70a82013-10-22 14:25:12 +00001/*
Ethan Yonker472f5062016-02-25 13:47:30 -06002 Copyright 2013 to 2016 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 bigbiffcdcfee42013-02-27 21:11:26 -050044#include "twrpDigest.hpp"
bigbiff bigbiff9c754052013-01-09 09:09:08 -050045#include "twrpTar.hpp"
Ethan Yonker3fdcda42016-11-30 12:29:37 -060046#include "exclude.hpp"
Ethan Yonker1b7a31b2014-07-03 15:09:22 -050047#include "infomanager.hpp"
Ethan Yonker4b94cfd2014-12-11 10:00:45 -060048#include "set_metadata.h"
Ethan Yonker74db1572015-10-28 12:44:49 -050049#include "gui/gui.hpp"
bigbiffce8f83c2015-12-12 18:30:21 -050050#include "adbbu/libtwadbbu.hpp"
Dees_Troy5bf43922012-09-07 16:07:55 -040051extern "C" {
Dees_Troy38bd7602012-09-14 13:33:53 -040052 #include "mtdutils/mtdutils.h"
53 #include "mtdutils/mounts.h"
Dees_Troya95f55c2013-08-17 13:14:43 +000054#ifdef USE_EXT4
55 #include "make_ext4fs.h"
56#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}
bigbiff bigbiffc49d7062013-10-11 20:28:00 -040068#ifdef HAVE_SELINUX
69#include "selinux/selinux.h"
Ethan Yonkerf27497f2014-02-09 11:48:33 -060070#include <selinux/label.h>
bigbiff bigbiffc49d7062013-10-11 20:28:00 -040071#endif
Dees Troy4159aed2014-02-28 17:24:43 +000072#ifdef HAVE_CAPABILITIES
73#include <sys/capability.h>
74#include <sys/xattr.h>
75#include <linux/xattr.h>
76#endif
HashBanged974bb2016-01-30 14:20:09 -050077#include <sparse_format.h>
Ethan Yonker472f5062016-02-25 13:47:30 -060078#include "progresstracking.hpp"
Dees_Troy51a0e822012-09-05 15:24:24 -040079
bigbiff bigbiff9c754052013-01-09 09:09:08 -050080using namespace std;
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;
87 unsigned flag;
88};
89
Matt Mower4ab42b12016-04-21 13:52:18 -050090const struct flag_list mount_flags[] = {
Hashcode62bd9e02013-11-19 21:59:42 -080091 { "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
Hashcode62bd9e02013-11-19 21:59:42 -0800102 { "unbindable", MS_UNBINDABLE },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000103#endif
104#ifdef MS_PRIVATE
Hashcode62bd9e02013-11-19 21:59:42 -0800105 { "private", MS_PRIVATE },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000106#endif
107#ifdef MS_SLAVE
Hashcode62bd9e02013-11-19 21:59:42 -0800108 { "slave", MS_SLAVE },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000109#endif
110#ifdef MS_SHARED
Hashcode62bd9e02013-11-19 21:59:42 -0800111 { "shared", MS_SHARED },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000112#endif
Hashcode62bd9e02013-11-19 21:59:42 -0800113 { "sync", MS_SYNCHRONOUS },
114 { "defaults", 0 },
115 { 0, 0 },
116};
117
Matt Mower2416a502016-04-12 19:54:46 -0500118enum TW_FSTAB_FLAGS {
119 TWFLAG_DEFAULTS, // Retain position
120 TWFLAG_ANDSEC,
121 TWFLAG_BACKUP,
122 TWFLAG_BACKUPNAME,
123 TWFLAG_BLOCKSIZE,
124 TWFLAG_CANBEWIPED,
125 TWFLAG_CANENCRYPTBACKUP,
126 TWFLAG_DISPLAY,
127 TWFLAG_ENCRYPTABLE,
128 TWFLAG_FLASHIMG,
129 TWFLAG_FORCEENCRYPT,
130 TWFLAG_FSFLAGS,
131 TWFLAG_IGNOREBLKID,
132 TWFLAG_LENGTH,
133 TWFLAG_MOUNTTODECRYPT,
134 TWFLAG_REMOVABLE,
135 TWFLAG_RETAINLAYOUTVERSION,
136 TWFLAG_SETTINGSSTORAGE,
137 TWFLAG_STORAGE,
138 TWFLAG_STORAGENAME,
139 TWFLAG_SUBPARTITIONOF,
140 TWFLAG_SYMLINK,
141 TWFLAG_USERDATAENCRYPTBACKUP,
142 TWFLAG_USERMRF,
143 TWFLAG_WIPEDURINGFACTORYRESET,
144 TWFLAG_WIPEINGUI,
Ethan Yonker1b190162016-12-05 15:25:19 -0600145 TWFLAG_SLOTSELECT,
Matt Mower2416a502016-04-12 19:54:46 -0500146};
147
148/* Flags without a trailing '=' are considered dual format flags and can be
149 * written as either 'flagname' or 'flagname=', where the character following
150 * the '=' is Y,y,1 for true and false otherwise.
151 */
152const struct flag_list tw_flags[] = {
153 { "andsec", TWFLAG_ANDSEC },
154 { "backup", TWFLAG_BACKUP },
155 { "backupname=", TWFLAG_BACKUPNAME },
156 { "blocksize=", TWFLAG_BLOCKSIZE },
157 { "canbewiped", TWFLAG_CANBEWIPED },
158 { "canencryptbackup", TWFLAG_CANENCRYPTBACKUP },
159 { "defaults", TWFLAG_DEFAULTS },
160 { "display=", TWFLAG_DISPLAY },
161 { "encryptable=", TWFLAG_ENCRYPTABLE },
162 { "flashimg", TWFLAG_FLASHIMG },
163 { "forceencrypt=", TWFLAG_FORCEENCRYPT },
164 { "fsflags=", TWFLAG_FSFLAGS },
165 { "ignoreblkid", TWFLAG_IGNOREBLKID },
166 { "length=", TWFLAG_LENGTH },
167 { "mounttodecrypt", TWFLAG_MOUNTTODECRYPT },
168 { "removable", TWFLAG_REMOVABLE },
169 { "retainlayoutversion", TWFLAG_RETAINLAYOUTVERSION },
170 { "settingsstorage", TWFLAG_SETTINGSSTORAGE },
171 { "storage", TWFLAG_STORAGE },
172 { "storagename=", TWFLAG_STORAGENAME },
173 { "subpartitionof=", TWFLAG_SUBPARTITIONOF },
174 { "symlink=", TWFLAG_SYMLINK },
175 { "userdataencryptbackup", TWFLAG_USERDATAENCRYPTBACKUP },
176 { "usermrf", TWFLAG_USERMRF },
177 { "wipeduringfactoryreset", TWFLAG_WIPEDURINGFACTORYRESET },
178 { "wipeingui", TWFLAG_WIPEINGUI },
Ethan Yonker1b190162016-12-05 15:25:19 -0600179 { "slotselect", TWFLAG_SLOTSELECT },
Matt Mower2416a502016-04-12 19:54:46 -0500180 { 0, 0 },
181};
182
that9e0593e2014-10-08 00:01:24 +0200183TWPartition::TWPartition() {
Dees_Troy51a0e822012-09-05 15:24:24 -0400184 Can_Be_Mounted = false;
185 Can_Be_Wiped = false;
Dees_Troya13d74f2013-03-24 08:54:55 -0500186 Can_Be_Backed_Up = false;
Vojtech Bocek1dc30982013-08-30 21:49:30 +0200187 Use_Rm_Rf = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400188 Wipe_During_Factory_Reset = false;
189 Wipe_Available_in_GUI = false;
190 Is_SubPartition = false;
Dees_Troy2691f9d2012-09-24 11:15:49 -0400191 Has_SubPartition = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400192 SubPartition_Of = "";
193 Symlink_Path = "";
194 Symlink_Mount_Point = "";
195 Mount_Point = "";
Dees_Troye58d5262012-09-21 12:27:57 -0400196 Backup_Path = "";
Dees_Troy38bd7602012-09-14 13:33:53 -0400197 Actual_Block_Device = "";
198 Primary_Block_Device = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400199 Alternate_Block_Device = "";
200 Removable = false;
201 Is_Present = false;
202 Length = 0;
203 Size = 0;
204 Used = 0;
205 Free = 0;
206 Backup_Size = 0;
207 Can_Be_Encrypted = false;
208 Is_Encrypted = false;
209 Is_Decrypted = false;
Ethan Yonkerbd7492d2016-12-07 13:55:01 -0600210 Is_FBE = false;
Ethan Yonker253368a2014-11-25 15:00:52 -0600211 Mount_To_Decrypt = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400212 Decrypted_Block_Device = "";
213 Display_Name = "";
Dees_Troya13d74f2013-03-24 08:54:55 -0500214 Backup_Display_Name = "";
215 Storage_Name = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400216 Backup_Name = "";
Dees_Troy63c8df72012-09-10 14:02:05 -0400217 Backup_FileName = "";
Dees_Troy38bd7602012-09-14 13:33:53 -0400218 MTD_Name = "";
bigbiffce8f83c2015-12-12 18:30:21 -0500219 Backup_Method = BM_NONE;
Dees_Troy83bd4832013-05-04 12:39:56 +0000220 Can_Encrypt_Backup = false;
221 Use_Userdata_Encryption = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400222 Has_Data_Media = false;
Dees_Troye58d5262012-09-21 12:27:57 -0400223 Has_Android_Secure = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400224 Is_Storage = false;
Dees_Troya13d74f2013-03-24 08:54:55 -0500225 Is_Settings_Storage = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400226 Storage_Path = "";
227 Current_File_System = "";
228 Fstab_File_System = "";
Hashcode62bd9e02013-11-19 21:59:42 -0800229 Mount_Flags = 0;
230 Mount_Options = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400231 Format_Block_Size = 0;
Dees_Troy68cab492012-12-12 19:29:35 +0000232 Ignore_Blkid = false;
Dees_Troy16c2b312013-01-15 16:51:18 +0000233 Retain_Layout_Version = false;
Ethan Yonker253368a2014-11-25 15:00:52 -0600234 Crypto_Key_Location = "footer";
Ethan Yonker726a0202014-12-16 20:01:38 -0600235 MTP_Storage_ID = 0;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600236 Can_Flash_Img = false;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500237 Mount_Read_Only = false;
Ethan Yonker66a19492015-12-10 10:19:45 -0600238 Is_Adopted_Storage = false;
239 Adopted_GUID = "";
Ethan Yonker1b190162016-12-05 15:25:19 -0600240 SlotSelect = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400241}
242
243TWPartition::~TWPartition(void) {
244 // Do nothing
245}
246
Matt Mower2b2dd152016-04-26 11:24:08 -0500247bool TWPartition::Process_Fstab_Line(const char *fstab_line, bool Display_Error) {
248 char full_line[MAX_FSTAB_LINE_LENGTH];
Matt Mower2416a502016-04-12 19:54:46 -0500249 char twflags[MAX_FSTAB_LINE_LENGTH] = "";
Dees_Troy5bf43922012-09-07 16:07:55 -0400250 char* ptr;
Matt Mower2b2dd152016-04-26 11:24:08 -0500251 int line_len = strlen(fstab_line), index = 0, item_index = 0;
Dees_Troya13d74f2013-03-24 08:54:55 -0500252 bool skip = false;
Dees_Troy5bf43922012-09-07 16:07:55 -0400253
Matt Mower2b2dd152016-04-26 11:24:08 -0500254 strlcpy(full_line, fstab_line, sizeof(full_line));
Dees_Troy51127312012-09-08 13:08:49 -0400255 for (index = 0; index < line_len; index++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500256 if (full_line[index] == 34)
257 skip = !skip;
258 if (!skip && full_line[index] <= 32)
Dees_Troy5bf43922012-09-07 16:07:55 -0400259 full_line[index] = '\0';
Dees_Troy5bf43922012-09-07 16:07:55 -0400260 }
Dees_Troy7c2dec82012-09-26 09:49:14 -0400261 Mount_Point = full_line;
Dees_Troy2673cec2013-04-02 20:22:16 +0000262 LOGINFO("Processing '%s'\n", Mount_Point.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -0400263 Backup_Path = Mount_Point;
Dees_Troya13d74f2013-03-24 08:54:55 -0500264 Storage_Path = Mount_Point;
Dees_Troy70737fa2013-04-08 13:19:20 +0000265 Display_Name = full_line + 1;
266 Backup_Display_Name = Display_Name;
267 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400268 index = Mount_Point.size();
269 while (index < line_len) {
270 while (index < line_len && full_line[index] == '\0')
271 index++;
272 if (index >= line_len)
273 continue;
274 ptr = full_line + index;
275 if (item_index == 0) {
276 // File System
277 Fstab_File_System = ptr;
278 Current_File_System = ptr;
279 item_index++;
280 } else if (item_index == 1) {
281 // Primary Block Device
Dees_Troy38bd7602012-09-14 13:33:53 -0400282 if (Fstab_File_System == "mtd" || Fstab_File_System == "yaffs2") {
Dees_Troy094207a2012-09-26 12:00:39 -0400283 MTD_Name = ptr;
284 Find_MTD_Block_Device(MTD_Name);
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400285 } else if (Fstab_File_System == "bml") {
286 if (Mount_Point == "/boot")
287 MTD_Name = "boot";
288 else if (Mount_Point == "/recovery")
289 MTD_Name = "recovery";
290 Primary_Block_Device = ptr;
291 if (*ptr != '/')
Dees_Troy2673cec2013-04-02 20:22:16 +0000292 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 -0400293 } else if (*ptr != '/') {
Dees_Troy5bf43922012-09-07 16:07:55 -0400294 if (Display_Error)
Matt Mower2b2dd152016-04-26 11:24:08 -0500295 LOGERR("Invalid block device '%s' in fstab line '%s'", ptr, fstab_line);
Dees_Troy5bf43922012-09-07 16:07:55 -0400296 else
Matt Mower2b2dd152016-04-26 11:24:08 -0500297 LOGINFO("Invalid block device '%s' in fstab line '%s'", ptr, fstab_line);
Matt Mower72c87ce2016-04-26 14:34:56 -0500298 return false;
Dees_Troy38bd7602012-09-14 13:33:53 -0400299 } else {
300 Primary_Block_Device = ptr;
301 Find_Real_Block_Device(Primary_Block_Device, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400302 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400303 item_index++;
304 } else if (item_index > 1) {
305 if (*ptr == '/') {
306 // Alternate Block Device
307 Alternate_Block_Device = ptr;
308 Find_Real_Block_Device(Alternate_Block_Device, Display_Error);
309 } else if (strlen(ptr) > 7 && strncmp(ptr, "length=", 7) == 0) {
310 // Partition length
311 ptr += 7;
312 Length = atoi(ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400313 } else if (strlen(ptr) > 6 && strncmp(ptr, "flags=", 6) == 0) {
314 // Custom flags, save for later so that new values aren't overwritten by defaults
315 ptr += 6;
Matt Mower2416a502016-04-12 19:54:46 -0500316 strlcpy(twflags, ptr, sizeof(twflags));
Dees_Troy38bd7602012-09-14 13:33:53 -0400317 } else if (strlen(ptr) == 4 && (strncmp(ptr, "NULL", 4) == 0 || strncmp(ptr, "null", 4) == 0 || strncmp(ptr, "null", 4) == 0)) {
318 // Do nothing
Dees_Troy5bf43922012-09-07 16:07:55 -0400319 } else {
320 // Unhandled data
Matt Mower2b2dd152016-04-26 11:24:08 -0500321 LOGINFO("Unhandled fstab information '%s' in fstab line '%s'\n", ptr, fstab_line);
Dees_Troy5bf43922012-09-07 16:07:55 -0400322 }
323 }
324 while (index < line_len && full_line[index] != '\0')
325 index++;
326 }
327
328 if (!Is_File_System(Fstab_File_System) && !Is_Image(Fstab_File_System)) {
329 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000330 LOGERR("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400331 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000332 LOGINFO("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Matt Mower72c87ce2016-04-26 14:34:56 -0500333 return false;
Dees_Troy5bf43922012-09-07 16:07:55 -0400334 } else if (Is_File_System(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400335 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400336 Setup_File_System(Display_Error);
337 if (Mount_Point == "/system") {
338 Display_Name = "System";
Dees_Troya13d74f2013-03-24 08:54:55 -0500339 Backup_Display_Name = Display_Name;
340 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400341 Wipe_Available_in_GUI = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500342 Can_Be_Backed_Up = true;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500343 Mount_Read_Only = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400344 } else if (Mount_Point == "/data") {
345 Display_Name = "Data";
Dees_Troya13d74f2013-03-24 08:54:55 -0500346 Backup_Display_Name = Display_Name;
347 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400348 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400349 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500350 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000351 Can_Encrypt_Backup = true;
352 Use_Userdata_Encryption = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400353 } else if (Mount_Point == "/cache") {
354 Display_Name = "Cache";
Dees_Troya13d74f2013-03-24 08:54:55 -0500355 Backup_Display_Name = Display_Name;
356 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400357 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400358 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500359 Can_Be_Backed_Up = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400360 } else if (Mount_Point == "/datadata") {
Dees_Troy51127312012-09-08 13:08:49 -0400361 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400362 Display_Name = "DataData";
Dees_Troya13d74f2013-03-24 08:54:55 -0500363 Backup_Display_Name = Display_Name;
364 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400365 Is_SubPartition = true;
366 SubPartition_Of = "/data";
Dees_Troy5bf43922012-09-07 16:07:55 -0400367 DataManager::SetValue(TW_HAS_DATADATA, 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500368 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000369 Can_Encrypt_Backup = true;
370 Use_Userdata_Encryption = false; // This whole partition should be encrypted
Dees_Troy5bf43922012-09-07 16:07:55 -0400371 } else if (Mount_Point == "/sd-ext") {
Dees_Troy51127312012-09-08 13:08:49 -0400372 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400373 Display_Name = "SD-Ext";
Dees_Troya13d74f2013-03-24 08:54:55 -0500374 Backup_Display_Name = Display_Name;
375 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400376 Wipe_Available_in_GUI = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400377 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500378 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000379 Can_Encrypt_Backup = true;
380 Use_Userdata_Encryption = true;
Dees_Troy2c50e182012-09-26 20:05:28 -0400381 } else if (Mount_Point == "/boot") {
382 Display_Name = "Boot";
Dees_Troya13d74f2013-03-24 08:54:55 -0500383 Backup_Display_Name = Display_Name;
Dees_Troy2c50e182012-09-26 20:05:28 -0400384 DataManager::SetValue("tw_boot_is_mountable", 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500385 Can_Be_Backed_Up = true;
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500386 } else if (Mount_Point == "/vendor") {
387 Display_Name = "Vendor";
388 Backup_Display_Name = Display_Name;
389 Storage_Name = Display_Name;
390 Mount_Read_Only = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400391 }
392#ifdef TW_EXTERNAL_STORAGE_PATH
393 if (Mount_Point == EXPAND(TW_EXTERNAL_STORAGE_PATH)) {
394 Is_Storage = true;
395 Storage_Path = EXPAND(TW_EXTERNAL_STORAGE_PATH);
Dees_Troyc51f1f92012-09-20 15:32:13 -0400396 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500397 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400398#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000399 if (Mount_Point == "/sdcard" || Mount_Point == "/external_sd" || Mount_Point == "/external_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400400 Is_Storage = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400401 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500402 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400403#endif
Dees_Troyb05ddee2013-01-28 20:24:50 +0000404 }
Dees_Troy8170a922012-09-18 15:40:25 -0400405#ifdef TW_INTERNAL_STORAGE_PATH
406 if (Mount_Point == EXPAND(TW_INTERNAL_STORAGE_PATH)) {
407 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500408 Is_Settings_Storage = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400409 Storage_Path = EXPAND(TW_INTERNAL_STORAGE_PATH);
Dees_Troya13d74f2013-03-24 08:54:55 -0500410 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400411 }
412#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000413 if (Mount_Point == "/emmc" || Mount_Point == "/internal_sd" || Mount_Point == "/internal_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400414 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500415 Is_Settings_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500416 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400417 }
418#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400419 } else if (Is_Image(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400420 Find_Actual_Block_Device();
Ethan Yonker1b190162016-12-05 15:25:19 -0600421 Setup_Image();
Dees_Troya13d74f2013-03-24 08:54:55 -0500422 if (Mount_Point == "/boot") {
423 Display_Name = "Boot";
424 Backup_Display_Name = Display_Name;
425 Can_Be_Backed_Up = true;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600426 Can_Flash_Img = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500427 } else if (Mount_Point == "/recovery") {
428 Display_Name = "Recovery";
429 Backup_Display_Name = Display_Name;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600430 Can_Flash_Img = true;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500431 } else if (Mount_Point == "/system_image") {
432 Display_Name = "System Image";
433 Backup_Display_Name = Display_Name;
HashBanged974bb2016-01-30 14:20:09 -0500434 Can_Flash_Img = true;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500435 Can_Be_Backed_Up = true;
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500436 } else if (Mount_Point == "/vendor_image") {
437 Display_Name = "Vendor Image";
438 Backup_Display_Name = Display_Name;
HashBanged974bb2016-01-30 14:20:09 -0500439 Can_Flash_Img = true;
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500440 Can_Be_Backed_Up = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500441 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400442 }
443
Matt Mower2416a502016-04-12 19:54:46 -0500444 // Process TWRP fstab flags
445 if (strlen(twflags) > 0) {
446 string Prev_Display_Name = Display_Name;
447 string Prev_Storage_Name = Storage_Name;
448 string Prev_Backup_Display_Name = Backup_Display_Name;
449 Display_Name = "";
450 Storage_Name = "";
451 Backup_Display_Name = "";
452
453 Process_TW_Flags(twflags, Display_Error);
454
455 bool has_display_name = !Display_Name.empty();
456 bool has_storage_name = !Storage_Name.empty();
457 bool has_backup_name = !Backup_Display_Name.empty();
458 if (!has_display_name) Display_Name = Prev_Display_Name;
459 if (!has_storage_name) Storage_Name = Prev_Storage_Name;
460 if (!has_backup_name) Backup_Display_Name = Prev_Backup_Display_Name;
461
462 if (has_display_name && !has_storage_name)
463 Storage_Name = Display_Name;
464 if (!has_display_name && has_storage_name)
465 Display_Name = Storage_Name;
466 if (has_display_name && !has_backup_name && Backup_Display_Name != "Android Secure")
467 Backup_Display_Name = Display_Name;
468 if (!has_display_name && has_backup_name)
469 Display_Name = Backup_Display_Name;
470 }
Dees_Troy51127312012-09-08 13:08:49 -0400471 return true;
472}
473
Matt Mower72c87ce2016-04-26 14:34:56 -0500474void TWPartition::Partition_Post_Processing(bool Display_Error) {
475 if (Mount_Point == "/data")
476 Setup_Data_Partition(Display_Error);
477 else if (Mount_Point == "/cache")
478 Setup_Cache_Partition(Display_Error);
479}
480
Ethan Yonkerbd7492d2016-12-07 13:55:01 -0600481void TWPartition::ExcludeAll(const string& path) {
482 backup_exclusions.add_absolute_dir(path);
483 wipe_exclusions.add_absolute_dir(path);
484}
485
Matt Mower72c87ce2016-04-26 14:34:56 -0500486void TWPartition::Setup_Data_Partition(bool Display_Error) {
487 if (Mount_Point != "/data")
488 return;
489
490 // Ensure /data is not mounted as tmpfs for qcom hardware decrypt
491 UnMount(false);
492
493#ifdef TW_INCLUDE_CRYPTO
494 if (datamedia)
495 Setup_Data_Media();
496 Can_Be_Encrypted = true;
497 char crypto_blkdev[255];
498 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
499 if (strcmp(crypto_blkdev, "error") != 0) {
500 DataManager::SetValue(TW_IS_DECRYPTED, 1);
501 Is_Encrypted = true;
502 Is_Decrypted = true;
Ethan Yonkerbd7492d2016-12-07 13:55:01 -0600503 Is_FBE = false;
504 DataManager::SetValue(TW_IS_FBE, 0);
Matt Mower72c87ce2016-04-26 14:34:56 -0500505 Decrypted_Block_Device = crypto_blkdev;
506 LOGINFO("Data already decrypted, new block device: '%s'\n", crypto_blkdev);
507 } else if (!Mount(false)) {
508 if (Is_Present) {
509 set_partition_data(Actual_Block_Device.c_str(), Crypto_Key_Location.c_str(), Fstab_File_System.c_str());
510 if (cryptfs_check_footer() == 0) {
511 Is_Encrypted = true;
512 Is_Decrypted = false;
513 Can_Be_Mounted = false;
514 Current_File_System = "emmc";
Ethan Yonker1b190162016-12-05 15:25:19 -0600515 Setup_Image();
Matt Mower72c87ce2016-04-26 14:34:56 -0500516 DataManager::SetValue(TW_IS_ENCRYPTED, 1);
517 DataManager::SetValue(TW_CRYPTO_PWTYPE, cryptfs_get_password_type());
518 DataManager::SetValue(TW_CRYPTO_PASSWORD, "");
519 DataManager::SetValue("tw_crypto_display", "");
520 } else {
521 gui_err("mount_data_footer=Could not mount /data and unable to find crypto footer.");
522 }
523 } else {
524 LOGERR("Primary block device '%s' for mount point '%s' is not present!\n", Primary_Block_Device.c_str(), Mount_Point.c_str());
525 }
526 } else {
Ethan Yonkerbd7492d2016-12-07 13:55:01 -0600527 if (TWFunc::Path_Exists("/data/unencrypted/key/version")) {
528 LOGINFO("File Based Encryption is present\n");
529#ifdef TW_INCLUDE_FBE
530 ExcludeAll(Mount_Point + "/convert_fbe");
531 ExcludeAll(Mount_Point + "/unencrypted");
532 //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
533 ExcludeAll(Mount_Point + "/misc/vold/user_keys");
Ethan Yonker79f88bd2016-12-09 14:52:12 -0600534 //ExcludeAll(Mount_Point + "/system_ce");
535 //ExcludeAll(Mount_Point + "/system_de");
536 //ExcludeAll(Mount_Point + "/misc_ce");
537 //ExcludeAll(Mount_Point + "/misc_de");
Ethan Yonkerbd7492d2016-12-07 13:55:01 -0600538 ExcludeAll(Mount_Point + "/system/gatekeeper.password.key");
539 ExcludeAll(Mount_Point + "/system/gatekeeper.pattern.key");
540 ExcludeAll(Mount_Point + "/system/locksettings.db");
541 //ExcludeAll(Mount_Point + "/system/locksettings.db-shm"); // don't seem to need this one, but the other 2 are needed
542 ExcludeAll(Mount_Point + "/system/locksettings.db-wal");
Ethan Yonker79f88bd2016-12-09 14:52:12 -0600543 //ExcludeAll(Mount_Point + "/user_de");
Ethan Yonkerbd7492d2016-12-07 13:55:01 -0600544 //ExcludeAll(Mount_Point + "/misc/profiles/cur/0"); // might be important later
545 ExcludeAll(Mount_Point + "/misc/gatekeeper");
546 ExcludeAll(Mount_Point + "/drm/kek.dat");
547 int retry_count = 3;
548 while (!Decrypt_DE() && --retry_count)
549 usleep(2000);
550 if (retry_count > 0) {
551 property_set("ro.crypto.state", "encrypted");
552 Is_Encrypted = true;
553 Is_Decrypted = false;
554 Is_FBE = true;
555 DataManager::SetValue(TW_IS_FBE, 1);
556 DataManager::SetValue(TW_IS_ENCRYPTED, 1);
557 string filename;
558 DataManager::SetValue(TW_CRYPTO_PWTYPE, Get_Password_Type(0, filename));
559 DataManager::SetValue(TW_CRYPTO_PASSWORD, "");
560 DataManager::SetValue("tw_crypto_display", "");
561 }
562#else
563 LOGERR("FBE found but FBE support not present in TWRP\n");
564#endif
565 } else {
566 // Filesystem is not encrypted and the mount succeeded, so return to
567 // the original unmounted state
568 UnMount(false);
569 }
Matt Mower72c87ce2016-04-26 14:34:56 -0500570 }
571 if (datamedia && (!Is_Encrypted || (Is_Encrypted && Is_Decrypted))) {
572 Setup_Data_Media();
573 Recreate_Media_Folder();
574 }
575#else
576 if (datamedia) {
577 Setup_Data_Media();
578 Recreate_Media_Folder();
579 }
580#endif
581}
582
583void TWPartition::Setup_Cache_Partition(bool Display_Error __unused) {
584 if (Mount_Point != "/cache")
585 return;
586
587 if (!Mount(true))
588 return;
589
590 if (!TWFunc::Path_Exists("/cache/recovery/.")) {
591 LOGINFO("Recreating /cache/recovery folder\n");
592 if (mkdir("/cache/recovery", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0)
593 LOGERR("Could not create /cache/recovery\n");
594 }
595}
596
Matt Mower4ab42b12016-04-21 13:52:18 -0500597void TWPartition::Process_FS_Flags(const char *str) {
598 char *options = strdup(str);
599 char *ptr, *savep;
Hashcode62bd9e02013-11-19 21:59:42 -0800600
Matt Mower4ab42b12016-04-21 13:52:18 -0500601 Mount_Options = "";
Hashcode62bd9e02013-11-19 21:59:42 -0800602
Matt Mower4ab42b12016-04-21 13:52:18 -0500603 // Avoid issues with potentially nested strtok by using strtok_r
604 ptr = strtok_r(options, ",", &savep);
605 while (ptr) {
606 const struct flag_list* mount_flag = mount_flags;
607
608 for (; mount_flag->name; mount_flag++) {
609 // mount_flags are never postfixed by '=',
610 // so only match identical strings (including length)
611 if (strcmp(ptr, mount_flag->name) == 0) {
612 Mount_Flags |= mount_flag->flag;
Hashcode62bd9e02013-11-19 21:59:42 -0800613 break;
614 }
615 }
616
Matt Mower4ab42b12016-04-21 13:52:18 -0500617 if (mount_flag->flag == MS_RDONLY)
618 Mount_Read_Only = true;
Hashcode62bd9e02013-11-19 21:59:42 -0800619
Matt Mower4ab42b12016-04-21 13:52:18 -0500620 if (mount_flag->name != 0) {
621 if (!Mount_Options.empty())
622 Mount_Options += ",";
623 Mount_Options += mount_flag->name;
624 } else {
625 LOGINFO("Unhandled mount flag: '%s'\n", ptr);
626 }
627
628 ptr = strtok_r(NULL, ",", &savep);
629 }
630 free(options);
Hashcode62bd9e02013-11-19 21:59:42 -0800631}
632
Matt Mower2416a502016-04-12 19:54:46 -0500633void TWPartition::Apply_TW_Flag(const unsigned flag, const char* str, const bool val) {
634 switch (flag) {
635 case TWFLAG_ANDSEC:
636 Has_Android_Secure = val;
637 break;
638 case TWFLAG_BACKUP:
639 Can_Be_Backed_Up = val;
640 break;
641 case TWFLAG_BACKUPNAME:
642 Backup_Display_Name = str;
643 break;
644 case TWFLAG_BLOCKSIZE:
645 Format_Block_Size = (unsigned long)(atol(str));
646 break;
647 case TWFLAG_CANBEWIPED:
648 Can_Be_Wiped = val;
649 break;
650 case TWFLAG_CANENCRYPTBACKUP:
651 Can_Encrypt_Backup = val;
652 break;
653 case TWFLAG_DEFAULTS:
654 // Do nothing
655 break;
656 case TWFLAG_DISPLAY:
657 Display_Name = str;
658 break;
659 case TWFLAG_ENCRYPTABLE:
660 case TWFLAG_FORCEENCRYPT:
661 Crypto_Key_Location = str;
662 break;
663 case TWFLAG_FLASHIMG:
664 Can_Flash_Img = val;
665 break;
666 case TWFLAG_FSFLAGS:
Matt Mower4ab42b12016-04-21 13:52:18 -0500667 Process_FS_Flags(str);
Matt Mower2416a502016-04-12 19:54:46 -0500668 break;
669 case TWFLAG_IGNOREBLKID:
670 Ignore_Blkid = val;
671 break;
672 case TWFLAG_LENGTH:
673 Length = atoi(str);
674 break;
675 case TWFLAG_MOUNTTODECRYPT:
676 Mount_To_Decrypt = val;
677 break;
678 case TWFLAG_REMOVABLE:
679 Removable = val;
680 break;
681 case TWFLAG_RETAINLAYOUTVERSION:
682 Retain_Layout_Version = val;
683 break;
684 case TWFLAG_SETTINGSSTORAGE:
685 Is_Settings_Storage = val;
686 if (Is_Settings_Storage)
687 Is_Storage = true;
688 break;
689 case TWFLAG_STORAGE:
690 Is_Storage = val;
691 break;
692 case TWFLAG_STORAGENAME:
693 Storage_Name = str;
694 break;
695 case TWFLAG_SUBPARTITIONOF:
696 Is_SubPartition = true;
697 SubPartition_Of = str;
698 break;
699 case TWFLAG_SYMLINK:
700 Symlink_Path = str;
701 break;
702 case TWFLAG_USERDATAENCRYPTBACKUP:
703 Use_Userdata_Encryption = val;
704 if (Use_Userdata_Encryption)
705 Can_Encrypt_Backup = true;
706 break;
707 case TWFLAG_USERMRF:
708 Use_Rm_Rf = val;
709 break;
710 case TWFLAG_WIPEDURINGFACTORYRESET:
711 Wipe_During_Factory_Reset = val;
712 if (Wipe_During_Factory_Reset) {
713 Can_Be_Wiped = true;
714 Wipe_Available_in_GUI = true;
715 }
716 break;
717 case TWFLAG_WIPEINGUI:
718 Wipe_Available_in_GUI = val;
719 if (Wipe_Available_in_GUI)
720 Can_Be_Wiped = true;
721 break;
Ethan Yonker1b190162016-12-05 15:25:19 -0600722 case TWFLAG_SLOTSELECT:
723 SlotSelect = true;
724 break;
Matt Mower2416a502016-04-12 19:54:46 -0500725 default:
726 // Should not get here
727 LOGINFO("Flag identified for processing, but later unmatched: %i\n", flag);
728 break;
729 }
730}
Dees_Troy51127312012-09-08 13:08:49 -0400731
Matt Mower2416a502016-04-12 19:54:46 -0500732void TWPartition::Process_TW_Flags(char *flags, bool Display_Error) {
733 char separator[2] = {'\n', 0};
734 char *ptr, *savep;
735
736 // Semicolons within double-quotes are not forbidden, so replace
737 // only the semicolons intended as separators with '\n' for strtok
738 for (unsigned i = 0, skip = 0; i < strlen(flags); i++) {
739 if (flags[i] == '\"')
Dees_Troya13d74f2013-03-24 08:54:55 -0500740 skip = !skip;
Matt Mower2416a502016-04-12 19:54:46 -0500741 if (!skip && flags[i] == ';')
742 flags[i] = separator[0];
Dees_Troy51127312012-09-08 13:08:49 -0400743 }
744
Matt Mower2416a502016-04-12 19:54:46 -0500745 // Avoid issues with potentially nested strtok by using strtok_r
746 ptr = strtok_r(flags, separator, &savep);
747 while (ptr) {
748 int ptr_len = strlen(ptr);
749 const struct flag_list* tw_flag = tw_flags;
Hashcode62bd9e02013-11-19 21:59:42 -0800750
Matt Mower2416a502016-04-12 19:54:46 -0500751 for (; tw_flag->name; tw_flag++) {
752 int flag_len = strlen(tw_flag->name);
753
754 if (strncmp(ptr, tw_flag->name, flag_len) == 0) {
755 bool flag_val = false;
756
757 if (ptr_len > flag_len && (tw_flag->name)[flag_len-1] != '='
758 && ptr[flag_len] != '=') {
759 // Handle flags with same starting string
760 // (e.g. backup and backupname)
761 continue;
762 } else if (ptr_len > flag_len && ptr[flag_len] == '=') {
763 // Handle flags with dual format: Part 1
764 // (e.g. backup and backup=y. backup=y handled here)
765 ptr += flag_len + 1;
766 TWFunc::Strip_Quotes(ptr);
767 // Skip flags with empty argument
768 // (e.g. backup=)
769 if (strlen(ptr) == 0) {
770 LOGINFO("Flag missing argument or should not include '=': %s=\n", tw_flag->name);
771 break;
772 }
773 flag_val = strchr("yY1", *ptr) != NULL;
774 } else if (ptr_len == flag_len
775 && (tw_flag->name)[flag_len-1] == '=') {
776 // Skip flags missing argument after =
777 // (e.g. backupname=)
778 LOGINFO("Flag missing argument: %s\n", tw_flag->name);
779 break;
780 } else if (ptr_len > flag_len
781 && (tw_flag->name)[flag_len-1] == '=') {
782 // Handle arguments to flags
783 // (e.g. backupname="My Stuff")
784 ptr += flag_len;
785 TWFunc::Strip_Quotes(ptr);
786 // Skip flags with empty argument
787 // (e.g. backupname="")
788 if (strlen(ptr) == 0) {
789 LOGINFO("Flag missing argument: %s\n", tw_flag->name);
790 break;
791 }
792 } else if (ptr_len == flag_len) {
793 // Handle flags with dual format: Part 2
794 // (e.g. backup and backup=y. backup handled here)
795 flag_val = true;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600796 } else {
Matt Mower2416a502016-04-12 19:54:46 -0500797 LOGINFO("Flag matched, but could not be processed: %s\n", ptr);
798 break;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600799 }
Matt Mower2416a502016-04-12 19:54:46 -0500800
801 Apply_TW_Flag(tw_flag->flag, ptr, flag_val);
802 break;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600803 }
Matt Mower2416a502016-04-12 19:54:46 -0500804 }
805 if (tw_flag->name == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400806 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000807 LOGERR("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400808 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000809 LOGINFO("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400810 }
Matt Mower2416a502016-04-12 19:54:46 -0500811 ptr = strtok_r(NULL, separator, &savep);
Dees_Troy51127312012-09-08 13:08:49 -0400812 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400813}
814
Dees_Troy5bf43922012-09-07 16:07:55 -0400815bool TWPartition::Is_File_System(string File_System) {
816 if (File_System == "ext2" ||
Dees_Troy63c8df72012-09-10 14:02:05 -0400817 File_System == "ext3" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400818 File_System == "ext4" ||
819 File_System == "vfat" ||
820 File_System == "ntfs" ||
821 File_System == "yaffs2" ||
bigbiff bigbiff3e146522012-11-14 14:32:59 -0500822 File_System == "exfat" ||
Dees_Troye5017042013-08-29 16:38:55 +0000823 File_System == "f2fs" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400824 File_System == "auto")
825 return true;
826 else
827 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400828}
829
Dees_Troy5bf43922012-09-07 16:07:55 -0400830bool TWPartition::Is_Image(string File_System) {
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400831 if (File_System == "emmc" || File_System == "mtd" || File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -0400832 return true;
833 else
834 return false;
835}
836
Dees_Troy51127312012-09-08 13:08:49 -0400837bool TWPartition::Make_Dir(string Path, bool Display_Error) {
Ethan Yonker93ac7a02016-11-07 22:05:58 -0600838 if (TWFunc::Get_D_Type_From_Stat(Path) != S_IFDIR)
839 unlink(Path.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400840 if (!TWFunc::Path_Exists(Path)) {
Dees_Troy51127312012-09-08 13:08:49 -0400841 if (mkdir(Path.c_str(), 0777) == -1) {
842 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -0500843 gui_msg(Msg(msg::kError, "create_folder_strerr=Can not create '{1}' folder ({2}).")(Path)(strerror(errno)));
Dees_Troy51127312012-09-08 13:08:49 -0400844 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000845 LOGINFO("Can not create '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400846 return false;
847 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000848 LOGINFO("Created '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400849 return true;
850 }
851 }
852 return true;
853}
854
Dees_Troy5bf43922012-09-07 16:07:55 -0400855void TWPartition::Setup_File_System(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400856 Can_Be_Mounted = true;
857 Can_Be_Wiped = true;
858
Dees_Troy5bf43922012-09-07 16:07:55 -0400859 // Make the mount point folder if it doesn't exist
Dees_Troy51127312012-09-08 13:08:49 -0400860 Make_Dir(Mount_Point, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400861 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
862 Backup_Name = Display_Name;
bigbiffce8f83c2015-12-12 18:30:21 -0500863 Backup_Method = BM_FILES;
Dees_Troy5bf43922012-09-07 16:07:55 -0400864}
865
Ethan Yonker1b190162016-12-05 15:25:19 -0600866void TWPartition::Setup_Image() {
Dees_Troy5bf43922012-09-07 16:07:55 -0400867 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
868 Backup_Name = Display_Name;
Gary Peck82599a82012-11-21 16:23:12 -0800869 if (Current_File_System == "emmc")
bigbiffce8f83c2015-12-12 18:30:21 -0500870 Backup_Method = BM_DD;
Gary Peck82599a82012-11-21 16:23:12 -0800871 else if (Current_File_System == "mtd" || Current_File_System == "bml")
bigbiffce8f83c2015-12-12 18:30:21 -0500872 Backup_Method = BM_FLASH_UTILS;
Dees_Troy5bf43922012-09-07 16:07:55 -0400873 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000874 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 -0400875}
876
Dees_Troye58d5262012-09-21 12:27:57 -0400877void TWPartition::Setup_AndSec(void) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500878 Backup_Display_Name = "Android Secure";
Dees_Troye58d5262012-09-21 12:27:57 -0400879 Backup_Name = "and-sec";
Dees_Troya13d74f2013-03-24 08:54:55 -0500880 Can_Be_Backed_Up = true;
Dees_Troye58d5262012-09-21 12:27:57 -0400881 Has_Android_Secure = true;
882 Symlink_Path = Mount_Point + "/.android_secure";
883 Symlink_Mount_Point = "/and-sec";
884 Backup_Path = Symlink_Mount_Point;
885 Make_Dir("/and-sec", true);
886 Recreate_AndSec_Folder();
Kjell Braden3126a112016-06-19 16:58:15 +0000887 Mount_Storage_Retry(true);
Dees_Troye58d5262012-09-21 12:27:57 -0400888}
889
that9e0593e2014-10-08 00:01:24 +0200890void TWPartition::Setup_Data_Media() {
Ethan Yonker6277c792014-09-15 14:54:30 -0500891 LOGINFO("Setting up '%s' as data/media emulated storage.\n", Mount_Point.c_str());
Ethan Yonker66a19492015-12-10 10:19:45 -0600892 if (Storage_Name.empty() || Storage_Name == "Data")
893 Storage_Name = "Internal Storage";
Ethan Yonker6277c792014-09-15 14:54:30 -0500894 Has_Data_Media = true;
895 Is_Storage = true;
Ethan Yonker66a19492015-12-10 10:19:45 -0600896 Storage_Path = Mount_Point + "/media";
Ethan Yonker6277c792014-09-15 14:54:30 -0500897 Symlink_Path = Storage_Path;
Ethan Yonker66a19492015-12-10 10:19:45 -0600898 if (Mount_Point == "/data") {
899 Is_Settings_Storage = true;
900 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
901 Make_Dir("/emmc", false);
902 Symlink_Mount_Point = "/emmc";
903 } else {
904 Make_Dir("/sdcard", false);
905 Symlink_Mount_Point = "/sdcard";
906 }
907 if (Mount(false) && TWFunc::Path_Exists(Mount_Point + "/media/0")) {
908 Storage_Path = Mount_Point + "/media/0";
909 Symlink_Path = Storage_Path;
910 DataManager::SetValue(TW_INTERNAL_PATH, Mount_Point + "/media/0");
911 UnMount(true);
912 }
913 DataManager::SetValue("tw_has_internal", 1);
914 DataManager::SetValue("tw_has_data_media", 1);
Ethan Yonker3fdcda42016-11-30 12:29:37 -0600915 backup_exclusions.add_absolute_dir("/data/data/com.google.android.music/files");
Ethan Yonker79f88bd2016-12-09 14:52:12 -0600916 ExcludeAll(Mount_Point + "/misc/vold");
917 ExcludeAll(Mount_Point + "/.layout_version");
918 ExcludeAll(Mount_Point + "/system/storage.xml");
Ethan Yonker6277c792014-09-15 14:54:30 -0500919 } else {
Ethan Yonker66a19492015-12-10 10:19:45 -0600920 if (Mount(true) && TWFunc::Path_Exists(Mount_Point + "/media/0")) {
921 Storage_Path = Mount_Point + "/media/0";
922 Symlink_Path = Storage_Path;
923 UnMount(true);
924 }
Ethan Yonker6277c792014-09-15 14:54:30 -0500925 }
Ethan Yonker79f88bd2016-12-09 14:52:12 -0600926 ExcludeAll(Mount_Point + "/media");
Ethan Yonker6277c792014-09-15 14:54:30 -0500927}
928
Dees_Troy5bf43922012-09-07 16:07:55 -0400929void TWPartition::Find_Real_Block_Device(string& Block, bool Display_Error) {
930 char device[512], realDevice[512];
931
932 strcpy(device, Block.c_str());
933 memset(realDevice, 0, sizeof(realDevice));
934 while (readlink(device, realDevice, sizeof(realDevice)) > 0)
935 {
936 strcpy(device, realDevice);
937 memset(realDevice, 0, sizeof(realDevice));
938 }
939
940 if (device[0] != '/') {
941 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000942 LOGERR("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400943 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000944 LOGINFO("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400945 return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400946 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400947 Block = device;
948 return;
949 }
950}
951
Kjell Braden3126a112016-06-19 16:58:15 +0000952bool TWPartition::Mount_Storage_Retry(bool Display_Error) {
Dees_Troy8e337f32012-10-13 22:07:49 -0400953 // On some devices, storage doesn't want to mount right away, retry and sleep
Kjell Braden3126a112016-06-19 16:58:15 +0000954 if (!Mount(Display_Error)) {
Dees_Troy8e337f32012-10-13 22:07:49 -0400955 int retry_count = 5;
956 while (retry_count > 0 && !Mount(false)) {
957 usleep(500000);
958 retry_count--;
959 }
Kjell Braden3126a112016-06-19 16:58:15 +0000960 return Mount(Display_Error);
Dees_Troy8e337f32012-10-13 22:07:49 -0400961 }
Kjell Braden3126a112016-06-19 16:58:15 +0000962 return true;
Dees_Troy8e337f32012-10-13 22:07:49 -0400963}
964
Dees_Troy38bd7602012-09-14 13:33:53 -0400965bool TWPartition::Find_MTD_Block_Device(string MTD_Name) {
966 FILE *fp = NULL;
967 char line[255];
968
969 fp = fopen("/proc/mtd", "rt");
970 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000971 LOGERR("Device does not support /proc/mtd\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400972 return false;
973 }
974
975 while (fgets(line, sizeof(line), fp) != NULL)
976 {
977 char device[32], label[32];
978 unsigned long size = 0;
Dees_Troy38bd7602012-09-14 13:33:53 -0400979 int deviceId;
980
981 sscanf(line, "%s %lx %*s %*c%s", device, &size, label);
982
983 // Skip header and blank lines
984 if ((strcmp(device, "dev:") == 0) || (strlen(line) < 8))
985 continue;
986
987 // Strip off the trailing " from the label
988 label[strlen(label)-1] = '\0';
989
990 if (strcmp(label, MTD_Name.c_str()) == 0) {
991 // We found our device
992 // Strip off the trailing : from the device
993 device[strlen(device)-1] = '\0';
994 if (sscanf(device,"mtd%d", &deviceId) == 1) {
995 sprintf(device, "/dev/block/mtdblock%d", deviceId);
996 Primary_Block_Device = device;
Dees_Troy76543db2013-06-19 16:24:30 +0000997 fclose(fp);
998 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -0400999 }
1000 }
1001 }
1002 fclose(fp);
1003
1004 return false;
1005}
1006
Dees_Troy51127312012-09-08 13:08:49 -04001007bool TWPartition::Get_Size_Via_statfs(bool Display_Error) {
1008 struct statfs st;
1009 string Local_Path = Mount_Point + "/.";
1010
1011 if (!Mount(Display_Error))
1012 return false;
1013
1014 if (statfs(Local_Path.c_str(), &st) != 0) {
1015 if (!Removable) {
1016 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001017 LOGERR("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -04001018 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001019 LOGINFO("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -04001020 }
1021 return false;
1022 }
1023 Size = (st.f_blocks * st.f_bsize);
1024 Used = ((st.f_blocks - st.f_bfree) * st.f_bsize);
1025 Free = (st.f_bfree * st.f_bsize);
1026 Backup_Size = Used;
1027 return true;
1028}
1029
1030bool TWPartition::Get_Size_Via_df(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001031 FILE* fp;
1032 char command[255], line[512];
1033 int include_block = 1;
1034 unsigned int min_len;
1035
1036 if (!Mount(Display_Error))
1037 return false;
1038
Dees_Troy38bd7602012-09-14 13:33:53 -04001039 min_len = Actual_Block_Device.size() + 2;
Dees_Troy51127312012-09-08 13:08:49 -04001040 sprintf(command, "df %s > /tmp/dfoutput.txt", Mount_Point.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001041 TWFunc::Exec_Cmd(command);
Dees_Troy51127312012-09-08 13:08:49 -04001042 fp = fopen("/tmp/dfoutput.txt", "rt");
1043 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001044 LOGINFO("Unable to open /tmp/dfoutput.txt.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001045 return false;
Dees_Troy51127312012-09-08 13:08:49 -04001046 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001047
1048 while (fgets(line, sizeof(line), fp) != NULL)
1049 {
1050 unsigned long blocks, used, available;
1051 char device[64];
1052 char tmpString[64];
1053
1054 if (strncmp(line, "Filesystem", 10) == 0)
1055 continue;
1056 if (strlen(line) < min_len) {
1057 include_block = 0;
1058 continue;
1059 }
1060 if (include_block) {
1061 sscanf(line, "%s %lu %lu %lu", device, &blocks, &used, &available);
1062 } else {
1063 // The device block string is so long that the df information is on the next line
1064 int space_count = 0;
Dees_Troye58d5262012-09-21 12:27:57 -04001065 sprintf(tmpString, "/dev/block/%s", Actual_Block_Device.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001066 while (tmpString[space_count] == 32)
1067 space_count++;
1068 sscanf(line + space_count, "%lu %lu %lu", &blocks, &used, &available);
1069 }
1070
1071 // Adjust block size to byte size
1072 Size = blocks * 1024ULL;
1073 Used = used * 1024ULL;
1074 Free = available * 1024ULL;
1075 Backup_Size = Used;
1076 }
1077 fclose(fp);
1078 return true;
1079}
1080
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001081unsigned long long TWPartition::IOCTL_Get_Block_Size() {
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001082 Find_Actual_Block_Device();
Ethan Yonker483e9f42016-01-11 22:21:18 -06001083
1084 return TWFunc::IOCTL_Get_Block_Size(Actual_Block_Device.c_str());
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001085}
1086
Dees_Troy5bf43922012-09-07 16:07:55 -04001087bool TWPartition::Find_Partition_Size(void) {
1088 FILE* fp;
1089 char line[512];
1090 string tmpdevice;
1091
igoriok87e3d932013-01-31 21:03:53 +02001092 fp = fopen("/proc/dumchar_info", "rt");
1093 if (fp != NULL) {
1094 while (fgets(line, sizeof(line), fp) != NULL)
1095 {
1096 char label[32], device[32];
1097 unsigned long size = 0;
1098
thatd43bf2d2014-09-21 23:13:02 +02001099 sscanf(line, "%s %lx %*x %*u %s", label, &size, device);
igoriok87e3d932013-01-31 21:03:53 +02001100
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05001101 // Skip header, annotation and blank lines
igoriok87e3d932013-01-31 21:03:53 +02001102 if ((strncmp(device, "/dev/", 5) != 0) || (strlen(line) < 8))
1103 continue;
1104
1105 tmpdevice = "/dev/";
1106 tmpdevice += label;
1107 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
1108 Size = size;
1109 fclose(fp);
1110 return true;
1111 }
1112 }
1113 }
1114
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001115 unsigned long long ioctl_size = IOCTL_Get_Block_Size();
1116 if (ioctl_size) {
1117 Size = ioctl_size;
1118 return true;
1119 }
1120
Dees_Troy5bf43922012-09-07 16:07:55 -04001121 // In this case, we'll first get the partitions we care about (with labels)
1122 fp = fopen("/proc/partitions", "rt");
1123 if (fp == NULL)
1124 return false;
1125
1126 while (fgets(line, sizeof(line), fp) != NULL)
1127 {
1128 unsigned long major, minor, blocks;
1129 char device[512];
Dees_Troy5bf43922012-09-07 16:07:55 -04001130
Dees_Troy63c8df72012-09-10 14:02:05 -04001131 if (strlen(line) < 7 || line[0] == 'm') continue;
Dees_Troy5bf43922012-09-07 16:07:55 -04001132 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
1133
1134 tmpdevice = "/dev/block/";
1135 tmpdevice += device;
Dees_Troy38bd7602012-09-14 13:33:53 -04001136 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001137 // Adjust block size to byte size
1138 Size = blocks * 1024ULL;
1139 fclose(fp);
1140 return true;
1141 }
1142 }
1143 fclose(fp);
1144 return false;
1145}
1146
Dees_Troy5bf43922012-09-07 16:07:55 -04001147bool TWPartition::Is_Mounted(void) {
1148 if (!Can_Be_Mounted)
1149 return false;
1150
1151 struct stat st1, st2;
1152 string test_path;
1153
1154 // Check to see if the mount point directory exists
1155 test_path = Mount_Point + "/.";
1156 if (stat(test_path.c_str(), &st1) != 0) return false;
1157
1158 // Check to see if the directory above the mount point exists
1159 test_path = Mount_Point + "/../.";
1160 if (stat(test_path.c_str(), &st2) != 0) return false;
1161
1162 // Compare the device IDs -- if they match then we're (probably) using tmpfs instead of an actual device
1163 int ret = (st1.st_dev != st2.st_dev) ? true : false;
1164
1165 return ret;
1166}
1167
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05001168bool TWPartition::Is_File_System_Writable(void) {
1169 if (!Is_File_System(Current_File_System) || !Is_Mounted())
1170 return false;
1171
1172 string test_path = Mount_Point + "/.";
1173 return (access(test_path.c_str(), W_OK) == 0);
1174}
1175
Dees_Troy5bf43922012-09-07 16:07:55 -04001176bool TWPartition::Mount(bool Display_Error) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001177 int exfat_mounted = 0;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001178 unsigned long flags = Mount_Flags;
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001179
Dees_Troy5bf43922012-09-07 16:07:55 -04001180 if (Is_Mounted()) {
1181 return true;
1182 } else if (!Can_Be_Mounted) {
1183 return false;
1184 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001185
1186 Find_Actual_Block_Device();
1187
1188 // Check the current file system before mounting
1189 Check_FS_Type();
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001190 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/exfat-fuse")) {
Dees_Troye34c1332013-02-06 19:13:00 +00001191 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 +00001192 LOGINFO("cmd: %s\n", cmd.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001193 string result;
1194 if (TWFunc::Exec_Cmd(cmd, result) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001195 LOGINFO("exfat-fuse failed to mount with result '%s', trying vfat\n", result.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001196 Current_File_System = "vfat";
1197 } else {
1198#ifdef TW_NO_EXFAT_FUSE
1199 UnMount(false);
1200 // We'll let the kernel handle it but using exfat-fuse to detect if the file system is actually exfat
1201 // Some kernels let us mount vfat as exfat which doesn't work out too well
1202#else
1203 exfat_mounted = 1;
1204#endif
1205 }
1206 }
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001207
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001208 if (Current_File_System == "ntfs" && (TWFunc::Path_Exists("/sbin/ntfs-3g") || TWFunc::Path_Exists("/sbin/mount.ntfs"))) {
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001209 string cmd;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001210 string Ntfsmount_Binary = "";
1211
1212 if (TWFunc::Path_Exists("/sbin/ntfs-3g"))
1213 Ntfsmount_Binary = "ntfs-3g";
1214 else if (TWFunc::Path_Exists("/sbin/mount.ntfs"))
1215 Ntfsmount_Binary = "mount.ntfs";
1216
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001217 if (Mount_Read_Only)
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001218 cmd = "/sbin/" + Ntfsmount_Binary + " -o ro " + Actual_Block_Device + " " + Mount_Point;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001219 else
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001220 cmd = "/sbin/" + Ntfsmount_Binary + " " + Actual_Block_Device + " " + Mount_Point;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001221 LOGINFO("cmd: '%s'\n", cmd.c_str());
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001222
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001223 if (TWFunc::Exec_Cmd(cmd) == 0) {
1224 return true;
1225 } else {
1226 LOGINFO("ntfs-3g failed to mount, trying regular mount method.\n");
1227 }
1228 }
1229
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001230 if (Mount_Read_Only)
1231 flags |= MS_RDONLY;
1232
Dees_Troy22042032012-12-18 21:23:08 +00001233 if (Fstab_File_System == "yaffs2") {
1234 // mount an MTD partition as a YAFFS2 filesystem.
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001235 flags = MS_NOATIME | MS_NODEV | MS_NODIRATIME;
1236 if (Mount_Read_Only)
1237 flags |= MS_RDONLY;
Dees_Troy76543db2013-06-19 16:24:30 +00001238 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags, NULL) < 0) {
1239 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags | MS_RDONLY, NULL) < 0) {
1240 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001241 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy76543db2013-06-19 16:24:30 +00001242 else
1243 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
1244 return false;
1245 } else {
1246 LOGINFO("Mounted '%s' (MTD) as RO\n", Mount_Point.c_str());
1247 return true;
1248 }
1249 } else {
1250 struct stat st;
1251 string test_path = Mount_Point;
1252 if (stat(test_path.c_str(), &st) < 0) {
1253 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001254 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy76543db2013-06-19 16:24:30 +00001255 else
1256 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
1257 return false;
1258 }
1259 mode_t new_mode = st.st_mode | S_IXUSR | S_IXGRP | S_IXOTH;
1260 if (new_mode != st.st_mode) {
1261 LOGINFO("Fixing execute permissions for %s\n", Mount_Point.c_str());
1262 if (chmod(Mount_Point.c_str(), new_mode) < 0) {
1263 if (Display_Error)
1264 LOGERR("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
1265 else
1266 LOGINFO("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
1267 return false;
1268 }
1269 }
Dees_Troy22042032012-12-18 21:23:08 +00001270 return true;
Dees_Troy76543db2013-06-19 16:24:30 +00001271 }
thatc7572eb2015-03-31 18:55:30 +02001272 }
1273
1274 string mount_fs = Current_File_System;
1275 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sys/module/texfat"))
1276 mount_fs = "texfat";
1277
1278 if (!exfat_mounted &&
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001279 mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), mount_fs.c_str(), flags, Mount_Options.c_str()) != 0 &&
1280 mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), mount_fs.c_str(), flags, NULL) != 0) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001281#ifdef TW_NO_EXFAT_FUSE
1282 if (Current_File_System == "exfat") {
Dees_Troy2673cec2013-04-02 20:22:16 +00001283 LOGINFO("Mounting exfat failed, trying vfat...\n");
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001284 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), "vfat", 0, NULL) != 0) {
Dees_Troy85f44ed2013-01-09 18:42:36 +00001285 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001286 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy85f44ed2013-01-09 18:42:36 +00001287 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001288 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001289 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 +00001290 return false;
Dees_Troy85f44ed2013-01-09 18:42:36 +00001291 }
Dees_Troyb05ddee2013-01-28 20:24:50 +00001292 } else {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001293#endif
bigbiff bigbiff26774a02014-03-29 18:22:00 -04001294 if (!Removable && Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001295 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001296 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001297 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
1298 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 +00001299 return false;
1300#ifdef TW_NO_EXFAT_FUSE
Dees_Troy85f44ed2013-01-09 18:42:36 +00001301 }
1302#endif
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001303 }
Ethan Yonker253368a2014-11-25 15:00:52 -06001304
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001305 if (Removable)
1306 Update_Size(Display_Error);
1307
xiaolu9416f4f2015-06-04 08:22:23 +08001308 if (!Symlink_Mount_Point.empty() && TWFunc::Path_Exists(Symlink_Path)) {
codelover2a3d4ce2015-03-14 20:26:49 +08001309 string Command = "mount -o bind '" + Symlink_Path + "' '" + Symlink_Mount_Point + "'";
Vojtech Bocek05534202013-09-11 08:11:56 +02001310 TWFunc::Exec_Cmd(Command);
Dees_Troy5bf43922012-09-07 16:07:55 -04001311 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001312 return true;
1313}
1314
1315bool TWPartition::UnMount(bool Display_Error) {
1316 if (Is_Mounted()) {
1317 int never_unmount_system;
1318
1319 DataManager::GetValue(TW_DONT_UNMOUNT_SYSTEM, never_unmount_system);
1320 if (never_unmount_system == 1 && Mount_Point == "/system")
1321 return true; // Never unmount system if you're not supposed to unmount it
1322
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001323 if (Is_Storage)
Ethan Yonker726a0202014-12-16 20:01:38 -06001324 PartitionManager.Remove_MTP_Storage(MTP_Storage_ID);
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001325
Dees_Troy38bd7602012-09-14 13:33:53 -04001326 if (!Symlink_Mount_Point.empty())
1327 umount(Symlink_Mount_Point.c_str());
1328
Dees_Troyb05ddee2013-01-28 20:24:50 +00001329 umount(Mount_Point.c_str());
1330 if (Is_Mounted()) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001331 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001332 gui_msg(Msg(msg::kError, "fail_unmount=Failed to unmount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy5bf43922012-09-07 16:07:55 -04001333 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001334 LOGINFO("Unable to unmount '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001335 return false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001336 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -04001337 return true;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001338 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001339 } else {
1340 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001341 }
1342}
1343
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05001344bool TWPartition::ReMount(bool Display_Error) {
1345 if (UnMount(Display_Error))
1346 return Mount(Display_Error);
1347 return false;
1348}
1349
1350bool TWPartition::ReMount_RW(bool Display_Error) {
1351 // No need to remount if already mounted rw
1352 if (Is_File_System_Writable())
1353 return true;
1354
1355 bool ro = Mount_Read_Only;
1356 int flags = Mount_Flags;
1357
1358 Mount_Read_Only = false;
1359 Mount_Flags &= ~MS_RDONLY;
1360
1361 bool ret = ReMount(Display_Error);
1362
1363 Mount_Read_Only = ro;
1364 Mount_Flags = flags;
1365
1366 return ret;
1367}
1368
Gary Peck43acadf2012-11-21 21:19:01 -08001369bool TWPartition::Wipe(string New_File_System) {
Ethan Yonker726a0202014-12-16 20:01:38 -06001370 bool wiped = false, update_crypt = false, recreate_media = true;
Dees_Troy16c2b312013-01-15 16:51:18 +00001371 int check;
1372 string Layout_Filename = Mount_Point + "/.layout_version";
1373
Dees_Troy38bd7602012-09-14 13:33:53 -04001374 if (!Can_Be_Wiped) {
Matt Mower3c366972015-12-25 19:28:31 -06001375 gui_msg(Msg(msg::kError, "cannot_wipe=Partition {1} cannot be wiped.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04001376 return false;
1377 }
1378
Dees_Troyc51f1f92012-09-20 15:32:13 -04001379 if (Mount_Point == "/cache")
Dees_Troy2673cec2013-04-02 20:22:16 +00001380 Log_Offset = 0;
Dees_Troyc51f1f92012-09-20 15:32:13 -04001381
Dees_Troy16c2b312013-01-15 16:51:18 +00001382 if (Retain_Layout_Version && Mount(false) && TWFunc::Path_Exists(Layout_Filename))
1383 TWFunc::copy_file(Layout_Filename, "/.layout_version", 0600);
1384 else
1385 unlink("/.layout_version");
Dees_Troy38bd7602012-09-14 13:33:53 -04001386
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001387 if (Has_Data_Media && Current_File_System == New_File_System) {
Dees_Troy16c2b312013-01-15 16:51:18 +00001388 wiped = Wipe_Data_Without_Wiping_Media();
Ethan Yonker5eac2222014-06-11 12:22:55 -05001389 recreate_media = false;
Dees_Troy16c2b312013-01-15 16:51:18 +00001390 } else {
Dees_Troy16c2b312013-01-15 16:51:18 +00001391 DataManager::GetValue(TW_RM_RF_VAR, check);
1392
Hashcodedabfd492013-08-29 22:45:30 -07001393 if (check || Use_Rm_Rf)
Dees_Troy16c2b312013-01-15 16:51:18 +00001394 wiped = Wipe_RMRF();
1395 else if (New_File_System == "ext4")
1396 wiped = Wipe_EXT4();
1397 else if (New_File_System == "ext2" || New_File_System == "ext3")
1398 wiped = Wipe_EXT23(New_File_System);
1399 else if (New_File_System == "vfat")
1400 wiped = Wipe_FAT();
1401 else if (New_File_System == "exfat")
1402 wiped = Wipe_EXFAT();
1403 else if (New_File_System == "yaffs2")
1404 wiped = Wipe_MTD();
Dees_Troye5017042013-08-29 16:38:55 +00001405 else if (New_File_System == "f2fs")
1406 wiped = Wipe_F2FS();
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001407 else if (New_File_System == "ntfs")
1408 wiped = Wipe_NTFS();
Dees_Troy16c2b312013-01-15 16:51:18 +00001409 else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001410 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 +00001411 unlink("/.layout_version");
1412 return false;
1413 }
1414 update_crypt = wiped;
Gary Pecke8bc5d72012-12-21 06:45:25 -08001415 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001416
Gary Pecke8bc5d72012-12-21 06:45:25 -08001417 if (wiped) {
Dees_Troy1c1ac442013-01-17 21:42:14 +00001418 if (Mount_Point == "/cache")
1419 DataManager::Output_Version();
1420
Dees_Troy16c2b312013-01-15 16:51:18 +00001421 if (TWFunc::Path_Exists("/.layout_version") && Mount(false))
1422 TWFunc::copy_file("/.layout_version", Layout_Filename, 0600);
1423
1424 if (update_crypt) {
1425 Setup_File_System(false);
1426 if (Is_Encrypted && !Is_Decrypted) {
1427 // just wiped an encrypted partition back to its unencrypted state
1428 Is_Encrypted = false;
1429 Is_Decrypted = false;
1430 Decrypted_Block_Device = "";
1431 if (Mount_Point == "/data") {
1432 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1433 DataManager::SetValue(TW_IS_DECRYPTED, 0);
1434 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001435 }
1436 }
Ethan Yonker5eac2222014-06-11 12:22:55 -05001437
Ethan Yonker66a19492015-12-10 10:19:45 -06001438 if (Has_Data_Media && recreate_media) {
Ethan Yonker5eac2222014-06-11 12:22:55 -05001439 Recreate_Media_Folder();
1440 }
Matt Mower209c9632016-01-20 12:08:35 -06001441 if (Is_Storage && Mount(false))
1442 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Gary Pecke8bc5d72012-12-21 06:45:25 -08001443 }
Matt Mower209c9632016-01-20 12:08:35 -06001444
Gary Pecke8bc5d72012-12-21 06:45:25 -08001445 return wiped;
Dees_Troy51a0e822012-09-05 15:24:24 -04001446}
1447
Gary Peck43acadf2012-11-21 21:19:01 -08001448bool TWPartition::Wipe() {
Gary Peck82599a82012-11-21 16:23:12 -08001449 if (Is_File_System(Current_File_System))
1450 return Wipe(Current_File_System);
1451 else
1452 return Wipe(Fstab_File_System);
Gary Peck43acadf2012-11-21 21:19:01 -08001453}
1454
Dees_Troye58d5262012-09-21 12:27:57 -04001455bool TWPartition::Wipe_AndSec(void) {
1456 if (!Has_Android_Secure)
1457 return false;
1458
Dees_Troye58d5262012-09-21 12:27:57 -04001459 if (!Mount(true))
1460 return false;
1461
Ethan Yonker74db1572015-10-28 12:44:49 -05001462 gui_msg(Msg("wiping=Wiping {1}")(Backup_Display_Name));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001463 TWFunc::removeDir(Mount_Point + "/.android_secure/", true);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001464 return true;
Dees_Troye58d5262012-09-21 12:27:57 -04001465}
1466
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001467bool TWPartition::Can_Repair() {
Ethan Yonkera2719152015-05-28 09:44:41 -05001468 if (Mount_Read_Only)
1469 return false;
Matt Mower18794c82015-11-11 16:22:45 -06001470 if (Current_File_System == "vfat" && TWFunc::Path_Exists("/sbin/fsck.fat"))
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001471 return true;
1472 else if ((Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") && TWFunc::Path_Exists("/sbin/e2fsck"))
1473 return true;
1474 else if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/fsck.exfat"))
1475 return true;
1476 else if (Current_File_System == "f2fs" && TWFunc::Path_Exists("/sbin/fsck.f2fs"))
1477 return true;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001478 else if (Current_File_System == "ntfs" && (TWFunc::Path_Exists("/sbin/ntfsfix") || TWFunc::Path_Exists("/sbin/fsck.ntfs")))
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001479 return true;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001480 return false;
1481}
1482
1483bool TWPartition::Repair() {
1484 string command;
1485
1486 if (Current_File_System == "vfat") {
Matt Mower18794c82015-11-11 16:22:45 -06001487 if (!TWFunc::Path_Exists("/sbin/fsck.fat")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001488 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.fat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001489 return false;
1490 }
1491 if (!UnMount(true))
1492 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001493 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.fat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001494 Find_Actual_Block_Device();
Matt Mower18794c82015-11-11 16:22:45 -06001495 command = "/sbin/fsck.fat -y " + Actual_Block_Device;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001496 LOGINFO("Repair command: %s\n", command.c_str());
1497 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001498 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001499 return true;
1500 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001501 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001502 return false;
1503 }
1504 }
1505 if (Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") {
1506 if (!TWFunc::Path_Exists("/sbin/e2fsck")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001507 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("e2fsck"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001508 return false;
1509 }
1510 if (!UnMount(true))
1511 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001512 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("e2fsck"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001513 Find_Actual_Block_Device();
Ethan Yonkera2719152015-05-28 09:44:41 -05001514 command = "/sbin/e2fsck -fp " + Actual_Block_Device;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001515 LOGINFO("Repair command: %s\n", command.c_str());
1516 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001517 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001518 return true;
1519 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001520 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001521 return false;
1522 }
1523 }
1524 if (Current_File_System == "exfat") {
1525 if (!TWFunc::Path_Exists("/sbin/fsck.exfat")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001526 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.exfat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001527 return false;
1528 }
1529 if (!UnMount(true))
1530 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001531 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.exfat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001532 Find_Actual_Block_Device();
1533 command = "/sbin/fsck.exfat " + Actual_Block_Device;
1534 LOGINFO("Repair command: %s\n", command.c_str());
1535 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001536 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001537 return true;
1538 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001539 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001540 return false;
1541 }
1542 }
1543 if (Current_File_System == "f2fs") {
1544 if (!TWFunc::Path_Exists("/sbin/fsck.f2fs")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001545 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.f2fs"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001546 return false;
1547 }
1548 if (!UnMount(true))
1549 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001550 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.f2fs"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001551 Find_Actual_Block_Device();
1552 command = "/sbin/fsck.f2fs " + Actual_Block_Device;
1553 LOGINFO("Repair command: %s\n", command.c_str());
1554 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001555 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001556 return true;
1557 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001558 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001559 return false;
1560 }
1561 }
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001562 if (Current_File_System == "ntfs") {
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001563 string Ntfsfix_Binary;
1564 if (TWFunc::Path_Exists("/sbin/ntfsfix"))
1565 Ntfsfix_Binary = "ntfsfix";
1566 else if (TWFunc::Path_Exists("/sbin/fsck.ntfs"))
1567 Ntfsfix_Binary = "fsck.ntfs";
1568 else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001569 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("ntfsfix"));
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001570 return false;
1571 }
1572 if (!UnMount(true))
1573 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001574 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)(Ntfsfix_Binary));
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001575 Find_Actual_Block_Device();
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001576 command = "/sbin/" + Ntfsfix_Binary + " " + Actual_Block_Device;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001577 LOGINFO("Repair command: %s\n", command.c_str());
1578 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001579 gui_msg("done=Done.");
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001580 return true;
1581 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001582 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001583 return false;
1584 }
1585 }
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001586 return false;
1587}
1588
Ethan Yonkera2719152015-05-28 09:44:41 -05001589bool TWPartition::Can_Resize() {
1590 if (Mount_Read_Only)
1591 return false;
1592 if ((Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") && TWFunc::Path_Exists("/sbin/resize2fs"))
1593 return true;
1594 return false;
1595}
1596
1597bool TWPartition::Resize() {
1598 string command;
1599
1600 if (Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") {
1601 if (!Can_Repair()) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001602 LOGINFO("Cannot resize %s because %s cannot be repaired before resizing.\n", Display_Name.c_str(), Display_Name.c_str());
1603 gui_msg(Msg(msg::kError, "cannot_resize=Cannot resize {1}.")(Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001604 return false;
1605 }
1606 if (!TWFunc::Path_Exists("/sbin/resize2fs")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001607 LOGINFO("resize2fs does not exist! Cannot resize!\n");
1608 gui_msg(Msg(msg::kError, "cannot_resize=Cannot resize {1}.")(Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001609 return false;
1610 }
1611 // Repair will unmount so no need to do it twice
Ethan Yonker74db1572015-10-28 12:44:49 -05001612 gui_msg(Msg("repair_resize=Repairing {1} before resizing.")( Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001613 if (!Repair())
1614 return false;
Ethan Yonker74db1572015-10-28 12:44:49 -05001615 gui_msg(Msg("resizing=Resizing {1} using {2}...")(Display_Name)("resize2fs"));
Ethan Yonkera2719152015-05-28 09:44:41 -05001616 Find_Actual_Block_Device();
1617 command = "/sbin/resize2fs " + Actual_Block_Device;
1618 if (Length != 0) {
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001619 unsigned long long Actual_Size = IOCTL_Get_Block_Size();
1620 if (Actual_Size == 0)
1621 return false;
Ethan Yonkera2719152015-05-28 09:44:41 -05001622
Ethan Yonkera2719152015-05-28 09:44:41 -05001623 unsigned long long Block_Count;
1624 if (Length < 0) {
1625 // Reduce overall size by this length
1626 Block_Count = (Actual_Size / 1024LLU) - ((unsigned long long)(Length * -1) / 1024LLU);
1627 } else {
1628 // This is the size, not a size reduction
1629 Block_Count = ((unsigned long long)(Length) / 1024LLU);
1630 }
1631 char temp[256];
1632 sprintf(temp, "%llu", Block_Count);
1633 command += " ";
1634 command += temp;
1635 command += "K";
1636 }
1637 LOGINFO("Resize command: %s\n", command.c_str());
1638 if (TWFunc::Exec_Cmd(command) == 0) {
1639 Update_Size(true);
Ethan Yonker74db1572015-10-28 12:44:49 -05001640 gui_msg("done=Done.");
Ethan Yonkera2719152015-05-28 09:44:41 -05001641 return true;
1642 } else {
1643 Update_Size(true);
Ethan Yonker74db1572015-10-28 12:44:49 -05001644 gui_msg(Msg(msg::kError, "unable_resize=Unable to resize {1}.")(Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001645 return false;
1646 }
1647 }
1648 return false;
1649}
1650
bigbiffce8f83c2015-12-12 18:30:21 -05001651bool TWPartition::Backup(PartitionSettings *part_settings, pid_t *tar_fork_pid) {
1652 if (Backup_Method == BM_FILES)
1653 return Backup_Tar(part_settings, tar_fork_pid);
1654 else if (Backup_Method == BM_DD)
1655 return Backup_Image(part_settings);
1656 else if (Backup_Method == BM_FLASH_UTILS)
1657 return Backup_Dump_Image(part_settings);
Dees_Troy2673cec2013-04-02 20:22:16 +00001658 LOGERR("Unknown backup method for '%s'\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001659 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001660}
1661
James Christopher Adduono79ae0932016-10-25 02:18:32 -04001662bool TWPartition::Check_Restore_File_MD5(const string& Filename) {
1663 twrpDigest md5sum;
1664
1665 md5sum.setfn(Filename);
1666 switch (md5sum.verify_md5digest()) {
1667 case MD5_OK:
1668 gui_msg(Msg("md5_matched=MD5 matched for '{1}'.")(Filename));
1669 return true;
1670 case MD5_FILE_UNREADABLE:
1671 case MD5_NOT_FOUND:
1672 gui_msg(Msg(msg::kError, "no_md5_found=No md5 file found for '{1}'. Please unselect Enable MD5 verification to restore.")(Filename));
1673 break;
1674 case MD5_MATCH_FAIL:
1675 gui_msg(Msg(msg::kError, "md5_fail_match=MD5 failed to match on '{1}'.")(Filename));
1676 break;
1677 }
1678 return false;
1679}
1680
bigbiff bigbiff8fd4b092016-08-30 20:48:53 -04001681bool TWPartition::Check_MD5(PartitionSettings *part_settings) {
James Christopher Adduono79ae0932016-10-25 02:18:32 -04001682 string Full_Filename;
Dees_Troy43d8b002012-09-17 16:00:01 -04001683 char split_filename[512];
1684 int index = 0;
1685
Captain Throwbackff5935f2014-09-23 16:08:34 -04001686 sync();
1687
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05001688 Full_Filename = part_settings->Backup_Folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04001689 if (!TWFunc::Path_Exists(Full_Filename)) {
1690 // This is a split archive, we presume
James Christopher Adduono79ae0932016-10-25 02:18:32 -04001691 memset(split_filename, 0, sizeof(split_filename));
Dees_Troy83bd4832013-05-04 12:39:56 +00001692 while (index < 1000) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001693 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
James Christopher Adduono79ae0932016-10-25 02:18:32 -04001694 if (!TWFunc::Path_Exists(split_filename))
1695 break;
1696 LOGINFO("split_filename: %s\n", split_filename);
1697 if (!Check_Restore_File_MD5(split_filename))
1698 return false;
1699 index++;
Dees_Troy43d8b002012-09-17 16:00:01 -04001700 }
Dees_Troy4a2a1262012-09-18 09:33:47 -04001701 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001702 }
James Christopher Adduono79ae0932016-10-25 02:18:32 -04001703 return Check_Restore_File_MD5(Full_Filename); // Single file archive
Dees_Troy43d8b002012-09-17 16:00:01 -04001704}
1705
bigbiffce8f83c2015-12-12 18:30:21 -05001706bool TWPartition::Restore(PartitionSettings *part_settings) {
Matt Mower3c366972015-12-25 19:28:31 -06001707 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, gui_parse_text("{@restoring_hdr}"));
Ethan Yonkere080c1f2016-09-19 13:50:25 -05001708 LOGINFO("Restore filename is: %s/%s\n", part_settings->Backup_Folder.c_str(), Backup_FileName.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001709
bigbiffce8f83c2015-12-12 18:30:21 -05001710 string Restore_File_System = Get_Restore_File_System(part_settings);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001711
1712 if (Is_File_System(Restore_File_System))
bigbiffce8f83c2015-12-12 18:30:21 -05001713 return Restore_Tar(part_settings);
1714 else if (Is_Image(Restore_File_System))
1715 return Restore_Image(part_settings);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001716
1717 LOGERR("Unknown restore method for '%s'\n", Mount_Point.c_str());
1718 return false;
1719}
1720
bigbiffce8f83c2015-12-12 18:30:21 -05001721string TWPartition::Get_Restore_File_System(PartitionSettings *part_settings) {
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001722 size_t first_period, second_period;
1723 string Restore_File_System;
1724
Gary Peck43acadf2012-11-21 21:19:01 -08001725 // Parse backup filename to extract the file system before wiping
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05001726 first_period = Backup_FileName.find(".");
Gary Peck43acadf2012-11-21 21:19:01 -08001727 if (first_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001728 LOGERR("Unable to find file system (first period).\n");
thatd43bf2d2014-09-21 23:13:02 +02001729 return string();
Gary Peck43acadf2012-11-21 21:19:01 -08001730 }
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05001731 Restore_File_System = Backup_FileName.substr(first_period + 1, Backup_FileName.size() - first_period - 1);
Gary Peck43acadf2012-11-21 21:19:01 -08001732 second_period = Restore_File_System.find(".");
1733 if (second_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001734 LOGERR("Unable to find file system (second period).\n");
thatd43bf2d2014-09-21 23:13:02 +02001735 return string();
Gary Peck43acadf2012-11-21 21:19:01 -08001736 }
1737 Restore_File_System.resize(second_period);
Matt Mowera0cd91d2016-12-30 18:21:42 -06001738 LOGINFO("Restore file system is: '%s'.\n", Restore_File_System.c_str());
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001739 return Restore_File_System;
Dees_Troy51a0e822012-09-05 15:24:24 -04001740}
1741
1742string TWPartition::Backup_Method_By_Name() {
bigbiffce8f83c2015-12-12 18:30:21 -05001743 if (Backup_Method == BM_NONE)
Dees_Troy38bd7602012-09-14 13:33:53 -04001744 return "none";
bigbiffce8f83c2015-12-12 18:30:21 -05001745 else if (Backup_Method == BM_FILES)
Dees_Troy38bd7602012-09-14 13:33:53 -04001746 return "files";
bigbiffce8f83c2015-12-12 18:30:21 -05001747 else if (Backup_Method == BM_DD)
Dees_Troy38bd7602012-09-14 13:33:53 -04001748 return "dd";
bigbiffce8f83c2015-12-12 18:30:21 -05001749 else if (Backup_Method == BM_FLASH_UTILS)
Dees_Troy38bd7602012-09-14 13:33:53 -04001750 return "flash_utils";
1751 else
1752 return "undefined";
1753 return "ERROR!";
Dees_Troy51a0e822012-09-05 15:24:24 -04001754}
1755
1756bool TWPartition::Decrypt(string Password) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001757 LOGINFO("STUB TWPartition::Decrypt, password: '%s'\n", Password.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001758 // Is this needed?
Dees_Troy51a0e822012-09-05 15:24:24 -04001759 return 1;
1760}
1761
1762bool TWPartition::Wipe_Encryption() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001763 bool Save_Data_Media = Has_Data_Media;
1764
1765 if (!UnMount(true))
1766 return false;
1767
Dees_Troy38bd7602012-09-14 13:33:53 -04001768 Has_Data_Media = false;
Dees_Troy74fb2e92013-04-15 14:35:47 +00001769 Decrypted_Block_Device = "";
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001770#ifdef TW_INCLUDE_CRYPTO
Ethan Yonker1b190162016-12-05 15:25:19 -06001771 if (Is_Decrypted && !Decrypted_Block_Device.empty()) {
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001772 if (!UnMount(true))
1773 return false;
Matt Mower2b18a532015-02-20 16:58:05 -06001774 if (delete_crypto_blk_dev((char*)("userdata")) != 0) {
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001775 LOGERR("Error deleting crypto block device, continuing anyway.\n");
1776 }
1777 }
1778#endif
Dees_Troy74fb2e92013-04-15 14:35:47 +00001779 Is_Decrypted = false;
1780 Is_Encrypted = false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001781 Find_Actual_Block_Device();
Ethan Yonkerbc85b632015-08-09 12:48:14 -05001782 if (Crypto_Key_Location == "footer") {
1783 int newlen, fd;
1784 if (Length != 0) {
1785 newlen = Length;
1786 if (newlen < 0)
1787 newlen = newlen * -1;
1788 } else {
1789 newlen = CRYPT_FOOTER_OFFSET;
1790 }
1791 if ((fd = open(Actual_Block_Device.c_str(), O_RDWR)) < 0) {
1792 gui_print_color("warning", "Unable to open '%s' to wipe crypto key\n", Actual_Block_Device.c_str());
1793 } else {
1794 unsigned int block_count;
1795 if ((ioctl(fd, BLKGETSIZE, &block_count)) == -1) {
1796 gui_print_color("warning", "Unable to get block size for wiping crypto footer.\n");
1797 } else {
1798 off64_t offset = ((off64_t)block_count * 512) - newlen;
1799 if (lseek64(fd, offset, SEEK_SET) == -1) {
1800 gui_print_color("warning", "Unable to lseek64 for wiping crypto footer.\n");
1801 } else {
1802 void* buffer = malloc(newlen);
1803 if (!buffer) {
1804 gui_print_color("warning", "Failed to malloc for wiping crypto footer.\n");
1805 } else {
1806 memset(buffer, 0, newlen);
1807 int ret = write(fd, buffer, newlen);
1808 if (ret != newlen) {
1809 gui_print_color("warning", "Failed to wipe crypto footer.\n");
1810 } else {
1811 LOGINFO("Successfully wiped crypto footer.\n");
1812 }
Matt Mower13a8f0b2015-09-26 15:40:03 -05001813 free(buffer);
Ethan Yonkerbc85b632015-08-09 12:48:14 -05001814 }
1815 }
1816 }
1817 close(fd);
1818 }
1819 } else {
Ethan Yonkerc2dafbb2016-03-15 22:20:59 -05001820 if (TWFunc::IOCTL_Get_Block_Size(Crypto_Key_Location.c_str()) >= 16384LLU) {
1821 string Command = "dd of='" + Crypto_Key_Location + "' if=/dev/zero bs=16384 count=1";
1822 TWFunc::Exec_Cmd(Command);
1823 } else {
1824 LOGINFO("Crypto key location reports size < 16K so not wiping crypto footer.\n");
1825 }
Ethan Yonkerbc85b632015-08-09 12:48:14 -05001826 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001827 if (Wipe(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001828 Has_Data_Media = Save_Data_Media;
1829 if (Has_Data_Media && !Symlink_Mount_Point.empty()) {
1830 Recreate_Media_Folder();
Ethan Yonker726a0202014-12-16 20:01:38 -06001831 if (Mount(false))
1832 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04001833 }
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001834 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
Ethan Yonker83e82572014-04-04 10:59:28 -05001835#ifndef TW_OEM_BUILD
Ethan Yonker74db1572015-10-28 12:44:49 -05001836 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 -05001837#endif
Dees_Troy38bd7602012-09-14 13:33:53 -04001838 return true;
1839 } else {
1840 Has_Data_Media = Save_Data_Media;
Ethan Yonker74db1572015-10-28 12:44:49 -05001841 gui_err("format_data_err=Unable to format to remove encryption.");
Ethan Yonker726a0202014-12-16 20:01:38 -06001842 if (Has_Data_Media && Mount(false))
1843 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04001844 return false;
1845 }
1846 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001847}
1848
1849void TWPartition::Check_FS_Type() {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001850 const char* type;
1851 blkid_probe pr;
Dees_Troy5bf43922012-09-07 16:07:55 -04001852
Dees_Troy68cab492012-12-12 19:29:35 +00001853 if (Fstab_File_System == "yaffs2" || Fstab_File_System == "mtd" || Fstab_File_System == "bml" || Ignore_Blkid)
1854 return; // Running blkid on some mtd devices causes a massive crash or needs to be skipped
Dees_Troy5bf43922012-09-07 16:07:55 -04001855
Dees_Troy38bd7602012-09-14 13:33:53 -04001856 Find_Actual_Block_Device();
Dees_Troy8170a922012-09-18 15:40:25 -04001857 if (!Is_Present)
1858 return;
Dees_Troy51127312012-09-08 13:08:49 -04001859
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001860 pr = blkid_new_probe_from_filename(Actual_Block_Device.c_str());
1861 if (blkid_do_fullprobe(pr)) {
1862 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00001863 LOGINFO("Can't probe device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001864 return;
Dees_Troy5bf43922012-09-07 16:07:55 -04001865 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001866
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05001867 if (blkid_probe_lookup_value(pr, "TYPE", &type, NULL) < 0) {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001868 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00001869 LOGINFO("can't find filesystem on device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001870 return;
1871 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001872
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001873 Current_File_System = type;
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001874 blkid_free_probe(pr);
Dees_Troy51a0e822012-09-05 15:24:24 -04001875}
1876
Gary Peck43acadf2012-11-21 21:19:01 -08001877bool TWPartition::Wipe_EXT23(string File_System) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001878 if (!UnMount(true))
1879 return false;
1880
Dees_Troy43d8b002012-09-17 16:00:01 -04001881 if (TWFunc::Path_Exists("/sbin/mke2fs")) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001882 string command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001883
Greg Wallace4b44fef2015-12-29 15:33:24 -05001884 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mke2fs"));
Dees_Troy38bd7602012-09-14 13:33:53 -04001885 Find_Actual_Block_Device();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001886 command = "mke2fs -t " + File_System + " -m 0 " + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001887 LOGINFO("mke2fs command: %s\n", command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001888 if (TWFunc::Exec_Cmd(command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001889 Current_File_System = File_System;
Dees_Troye58d5262012-09-21 12:27:57 -04001890 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05001891 gui_msg("done=Done.");
Dees_Troy38bd7602012-09-14 13:33:53 -04001892 return true;
1893 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001894 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04001895 return false;
1896 }
1897 } else
1898 return Wipe_RMRF();
1899
1900 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001901}
1902
1903bool TWPartition::Wipe_EXT4() {
Ethan Yonker25f20c12014-10-14 09:04:43 -05001904 Find_Actual_Block_Device();
1905 if (!Is_Present) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001906 LOGINFO("Block device not present, cannot wipe %s.\n", Display_Name.c_str());
1907 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Ethan Yonker25f20c12014-10-14 09:04:43 -05001908 return false;
1909 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001910 if (!UnMount(true))
1911 return false;
1912
Dees_Troyb3265ab2013-08-30 02:59:14 +00001913#if defined(HAVE_SELINUX) && defined(USE_EXT4)
Ethan Yonkerf27497f2014-02-09 11:48:33 -06001914 int ret;
1915 char *secontext = NULL;
1916
Greg Wallace4b44fef2015-12-29 15:33:24 -05001917 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("make_ext4fs"));
Ethan Yonkerf27497f2014-02-09 11:48:33 -06001918
Dees Troy99c8dbf2014-03-10 16:53:58 +00001919 if (!selinux_handle || selabel_lookup(selinux_handle, &secontext, Mount_Point.c_str(), S_IFDIR) < 0) {
Ethan Yonkerf27497f2014-02-09 11:48:33 -06001920 LOGINFO("Cannot lookup security context for '%s'\n", Mount_Point.c_str());
1921 ret = make_ext4fs(Actual_Block_Device.c_str(), Length, Mount_Point.c_str(), NULL);
1922 } else {
1923 ret = make_ext4fs(Actual_Block_Device.c_str(), Length, Mount_Point.c_str(), selinux_handle);
1924 }
1925 if (ret != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001926 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troya95f55c2013-08-17 13:14:43 +00001927 return false;
1928 } else {
bigbiff bigbiffc49d7062013-10-11 20:28:00 -04001929 string sedir = Mount_Point + "/lost+found";
1930 PartitionManager.Mount_By_Path(sedir.c_str(), true);
1931 rmdir(sedir.c_str());
1932 mkdir(sedir.c_str(), S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP);
Dees_Troya95f55c2013-08-17 13:14:43 +00001933 return true;
1934 }
1935#else
Dees_Troy43d8b002012-09-17 16:00:01 -04001936 if (TWFunc::Path_Exists("/sbin/make_ext4fs")) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001937 string Command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001938
Greg Wallace4b44fef2015-12-29 15:33:24 -05001939 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("make_ext4fs"));
Dees_Troy38bd7602012-09-14 13:33:53 -04001940 Find_Actual_Block_Device();
1941 Command = "make_ext4fs";
1942 if (!Is_Decrypted && Length != 0) {
1943 // Only use length if we're not decrypted
1944 char len[32];
1945 sprintf(len, "%i", Length);
1946 Command += " -l ";
1947 Command += len;
1948 }
Dees_Troy5295d582013-09-06 15:51:08 +00001949 if (TWFunc::Path_Exists("/file_contexts")) {
1950 Command += " -S /file_contexts";
1951 }
1952 Command += " -a " + Mount_Point + " " + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001953 LOGINFO("make_ext4fs command: %s\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001954 if (TWFunc::Exec_Cmd(Command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001955 Current_File_System = "ext4";
Dees_Troye58d5262012-09-21 12:27:57 -04001956 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05001957 gui_msg("done=Done.");
Dees_Troy38bd7602012-09-14 13:33:53 -04001958 return true;
1959 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001960 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04001961 return false;
1962 }
1963 } else
Gary Peck43acadf2012-11-21 21:19:01 -08001964 return Wipe_EXT23("ext4");
Dees_Troya95f55c2013-08-17 13:14:43 +00001965#endif
Dees_Troy38bd7602012-09-14 13:33:53 -04001966 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001967}
1968
1969bool TWPartition::Wipe_FAT() {
Vojtech Bocek05534202013-09-11 08:11:56 +02001970 string command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001971
Matt Mower18794c82015-11-11 16:22:45 -06001972 if (TWFunc::Path_Exists("/sbin/mkfs.fat")) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001973 if (!UnMount(true))
1974 return false;
1975
Greg Wallace4b44fef2015-12-29 15:33:24 -05001976 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mkfs.fat"));
Dees_Troy38bd7602012-09-14 13:33:53 -04001977 Find_Actual_Block_Device();
Matt Mower18794c82015-11-11 16:22:45 -06001978 command = "mkfs.fat " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02001979 if (TWFunc::Exec_Cmd(command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001980 Current_File_System = "vfat";
Dees_Troye58d5262012-09-21 12:27:57 -04001981 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05001982 gui_msg("done=Done.");
Dees_Troy38bd7602012-09-14 13:33:53 -04001983 return true;
1984 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001985 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04001986 return false;
1987 }
1988 return true;
1989 }
1990 else
1991 return Wipe_RMRF();
1992
1993 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001994}
1995
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001996bool TWPartition::Wipe_EXFAT() {
Vojtech Bocek05534202013-09-11 08:11:56 +02001997 string command;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001998
1999 if (TWFunc::Path_Exists("/sbin/mkexfatfs")) {
2000 if (!UnMount(true))
2001 return false;
2002
Greg Wallace4b44fef2015-12-29 15:33:24 -05002003 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mkexfatfs"));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002004 Find_Actual_Block_Device();
2005 command = "mkexfatfs " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02002006 if (TWFunc::Exec_Cmd(command) == 0) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002007 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05002008 gui_msg("done=Done.");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002009 return true;
2010 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002011 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002012 return false;
2013 }
2014 return true;
2015 }
2016 return false;
2017}
2018
Dees_Troy38bd7602012-09-14 13:33:53 -04002019bool TWPartition::Wipe_MTD() {
2020 if (!UnMount(true))
2021 return false;
2022
Greg Wallace4b44fef2015-12-29 15:33:24 -05002023 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("MTD"));
Dees_Troy38bd7602012-09-14 13:33:53 -04002024
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02002025 mtd_scan_partitions();
2026 const MtdPartition* mtd = mtd_find_partition_by_name(MTD_Name.c_str());
2027 if (mtd == NULL) {
2028 LOGERR("No mtd partition named '%s'", MTD_Name.c_str());
2029 return false;
2030 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002031
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02002032 MtdWriteContext* ctx = mtd_write_partition(mtd);
2033 if (ctx == NULL) {
2034 LOGERR("Can't write '%s', failed to format.", MTD_Name.c_str());
2035 return false;
2036 }
2037 if (mtd_erase_blocks(ctx, -1) == -1) {
2038 mtd_write_close(ctx);
2039 LOGERR("Failed to format '%s'", MTD_Name.c_str());
2040 return false;
2041 }
2042 if (mtd_write_close(ctx) != 0) {
2043 LOGERR("Failed to close '%s'", MTD_Name.c_str());
2044 return false;
2045 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08002046 Current_File_System = "yaffs2";
Dees_Troye58d5262012-09-21 12:27:57 -04002047 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05002048 gui_msg("done=Done.");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02002049 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002050}
2051
2052bool TWPartition::Wipe_RMRF() {
Dees_Troy38bd7602012-09-14 13:33:53 -04002053 if (!Mount(true))
2054 return false;
Ethan Yonker726a0202014-12-16 20:01:38 -06002055 // This is the only wipe that leaves the partition mounted, so we
2056 // must manually remove the partition from MTP if it is a storage
2057 // partition.
2058 if (Is_Storage)
2059 PartitionManager.Remove_MTP_Storage(MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04002060
Ethan Yonker74db1572015-10-28 12:44:49 -05002061 gui_msg(Msg("remove_all=Removing all files under '{1}'")(Mount_Point));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002062 TWFunc::removeDir(Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04002063 Recreate_AndSec_Folder();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002064 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002065}
2066
Dees_Troye5017042013-08-29 16:38:55 +00002067bool TWPartition::Wipe_F2FS() {
Vojtech Bocek05534202013-09-11 08:11:56 +02002068 string command;
Dees_Troye5017042013-08-29 16:38:55 +00002069
2070 if (TWFunc::Path_Exists("/sbin/mkfs.f2fs")) {
2071 if (!UnMount(true))
2072 return false;
2073
Greg Wallace4b44fef2015-12-29 15:33:24 -05002074 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mkfs.f2fs"));
Dees_Troye5017042013-08-29 16:38:55 +00002075 Find_Actual_Block_Device();
Greg Wallace558aef72016-01-19 21:14:30 -05002076 command = "mkfs.f2fs -t 0";
dhacker29a3fa75f2015-01-17 19:42:33 -05002077 if (!Is_Decrypted && Length != 0) {
2078 // Only use length if we're not decrypted
2079 char len[32];
2080 int mod_length = Length;
2081 if (Length < 0)
2082 mod_length *= -1;
2083 sprintf(len, "%i", mod_length);
2084 command += " -r ";
2085 command += len;
2086 }
2087 command += " " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02002088 if (TWFunc::Exec_Cmd(command) == 0) {
Dees_Troye5017042013-08-29 16:38:55 +00002089 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05002090 gui_msg("done=Done.");
Dees_Troye5017042013-08-29 16:38:55 +00002091 return true;
2092 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002093 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troye5017042013-08-29 16:38:55 +00002094 return false;
2095 }
2096 return true;
2097 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002098 LOGINFO("mkfs.f2fs binary not found, using rm -rf to wipe.\n");
Dees_Troye5017042013-08-29 16:38:55 +00002099 return Wipe_RMRF();
2100 }
2101 return false;
2102}
2103
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002104bool TWPartition::Wipe_NTFS() {
2105 string command;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002106 string Ntfsmake_Binary;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002107
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002108 if (TWFunc::Path_Exists("/sbin/mkntfs"))
2109 Ntfsmake_Binary = "mkntfs";
2110 else if (TWFunc::Path_Exists("/sbin/mkfs.ntfs"))
2111 Ntfsmake_Binary = "mkfs.ntfs";
2112 else
2113 return false;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002114
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002115 if (!UnMount(true))
2116 return false;
2117
Greg Wallace4b44fef2015-12-29 15:33:24 -05002118 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)(Ntfsmake_Binary));
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002119 Find_Actual_Block_Device();
2120 command = "/sbin/" + Ntfsmake_Binary + " " + Actual_Block_Device;
2121 if (TWFunc::Exec_Cmd(command) == 0) {
2122 Recreate_AndSec_Folder();
2123 gui_msg("done=Done.");
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002124 return true;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002125 } else {
2126 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
2127 return false;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002128 }
2129 return false;
2130}
2131
Dees_Troy51a0e822012-09-05 15:24:24 -04002132bool TWPartition::Wipe_Data_Without_Wiping_Media() {
Ethan Yonker83e82572014-04-04 10:59:28 -05002133#ifdef TW_OEM_BUILD
2134 // In an OEM Build we want to do a full format
2135 return Wipe_Encryption();
2136#else
Ethan Yonker66a19492015-12-10 10:19:45 -06002137 bool ret = false;
Dees_Troy38bd7602012-09-14 13:33:53 -04002138
Dees_Troy38bd7602012-09-14 13:33:53 -04002139 if (!Mount(true))
2140 return false;
2141
Ethan Yonker74db1572015-10-28 12:44:49 -05002142 gui_msg("wiping_data=Wiping data without wiping /data/media ...");
Ethan Yonker66a19492015-12-10 10:19:45 -06002143 ret = Wipe_Data_Without_Wiping_Media_Func(Mount_Point + "/");
2144 if (ret)
2145 gui_msg("done=Done.");
2146 return ret;
2147#endif // ifdef TW_OEM_BUILD
2148}
2149
2150bool TWPartition::Wipe_Data_Without_Wiping_Media_Func(const string& parent __unused) {
2151 string dir;
Dees_Troy38bd7602012-09-14 13:33:53 -04002152
2153 DIR* d;
Ethan Yonker66a19492015-12-10 10:19:45 -06002154 d = opendir(parent.c_str());
Dees_Troy16b74352012-11-14 22:27:31 +00002155 if (d != NULL) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002156 struct dirent* de;
2157 while ((de = readdir(d)) != NULL) {
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002158 if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) continue;
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05002159
Ethan Yonker66a19492015-12-10 10:19:45 -06002160 dir = parent;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002161 dir.append(de->d_name);
Ethan Yonker3fdcda42016-11-30 12:29:37 -06002162 if (wipe_exclusions.check_skip_dirs(dir)) {
Ethan Yonker66a19492015-12-10 10:19:45 -06002163 LOGINFO("skipped '%s'\n", dir.c_str());
2164 continue;
2165 }
Dees_Troyce675462013-01-09 19:48:21 +00002166 if (de->d_type == DT_DIR) {
Ethan Yonker66a19492015-12-10 10:19:45 -06002167 dir.append("/");
2168 if (!Wipe_Data_Without_Wiping_Media_Func(dir)) {
2169 closedir(d);
2170 return false;
2171 }
2172 rmdir(dir.c_str());
bigbiff bigbiff98f1f902013-01-19 18:46:13 -05002173 } else if (de->d_type == DT_REG || de->d_type == DT_LNK || de->d_type == DT_FIFO || de->d_type == DT_SOCK) {
Dees_Troyce675462013-01-09 19:48:21 +00002174 if (!unlink(dir.c_str()))
Ethan Yonker79f88bd2016-12-09 14:52:12 -06002175 LOGINFO("Unable to unlink '%s': %s\n", dir.c_str(), strerror(errno));
Dees_Troyce675462013-01-09 19:48:21 +00002176 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002177 }
2178 closedir(d);
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05002179
Dees_Troy16b74352012-11-14 22:27:31 +00002180 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -04002181 }
Ethan Yonker74db1572015-10-28 12:44:49 -05002182 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy16b74352012-11-14 22:27:31 +00002183 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04002184}
2185
bigbiffce8f83c2015-12-12 18:30:21 -05002186bool TWPartition::Backup_Tar(PartitionSettings *part_settings, pid_t *tar_fork_pid) {
Ethan Yonker472f5062016-02-25 13:47:30 -06002187 string Full_FileName;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002188 twrpTar tar;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05002189
Dees_Troy43d8b002012-09-17 16:00:01 -04002190 if (!Mount(true))
2191 return false;
2192
Dees_Troya13d74f2013-03-24 08:54:55 -05002193 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Backup_Display_Name, "Backing Up");
Ethan Yonker74db1572015-10-28 12:44:49 -05002194 gui_msg(Msg("backing_up=Backing up {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002195
Ethan Yonker472f5062016-02-25 13:47:30 -06002196 DataManager::GetValue(TW_USE_COMPRESSION_VAR, tar.use_compression);
Matt Mowerbb81e5d2014-03-20 18:05:41 -05002197
Dees_Troy83bd4832013-05-04 12:39:56 +00002198#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
Ethan Yonker472f5062016-02-25 13:47:30 -06002199 if (Can_Encrypt_Backup) {
2200 DataManager::GetValue("tw_encrypt_backup", tar.use_encryption);
2201 if (tar.use_encryption) {
2202 if (Use_Userdata_Encryption)
2203 tar.userdata_encryption = tar.use_encryption;
2204 string Password;
2205 DataManager::GetValue("tw_backup_password", Password);
2206 tar.setpassword(Password);
2207 } else {
2208 tar.use_encryption = 0;
2209 }
Dees_Troy83bd4832013-05-04 12:39:56 +00002210 }
2211#endif
Dees_Troy43d8b002012-09-17 16:00:01 -04002212
Ethan Yonker472f5062016-02-25 13:47:30 -06002213 Backup_FileName = Backup_Name + "." + Current_File_System + ".win";
Ethan Yonkere080c1f2016-09-19 13:50:25 -05002214 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
Dees_Troy83bd4832013-05-04 12:39:56 +00002215 tar.has_data_media = Has_Data_Media;
bigbiffce8f83c2015-12-12 18:30:21 -05002216 tar.part_settings = part_settings;
Ethan Yonker3fdcda42016-11-30 12:29:37 -06002217 tar.backup_exclusions = &backup_exclusions;
Dees Troye0a433a2013-12-02 04:10:37 +00002218 tar.setdir(Backup_Path);
2219 tar.setfn(Full_FileName);
2220 tar.setsize(Backup_Size);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002221 tar.partition_name = Backup_Name;
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002222 tar.backup_folder = part_settings->Backup_Folder;
bigbiffce8f83c2015-12-12 18:30:21 -05002223 if (tar.createTarFork(tar_fork_pid) != 0)
Dees Troye0a433a2013-12-02 04:10:37 +00002224 return false;
Dees_Troy43d8b002012-09-17 16:00:01 -04002225 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002226}
2227
bigbiffce8f83c2015-12-12 18:30:21 -05002228bool TWPartition::Backup_Image(PartitionSettings *part_settings) {
2229 string Full_FileName, adb_file_name;
igoriok87e3d932013-01-31 21:03:53 +02002230
Ethan Yonker74db1572015-10-28 12:44:49 -05002231 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, gui_parse_text("{@backing}"));
2232 gui_msg(Msg("backing_up=Backing up {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002233
Ethan Yonker472f5062016-02-25 13:47:30 -06002234 Backup_FileName = Backup_Name + "." + Current_File_System + ".win";
Dees_Troy43d8b002012-09-17 16:00:01 -04002235
bigbiffce8f83c2015-12-12 18:30:21 -05002236 if (part_settings->adbbackup) {
2237 Full_FileName = TW_ADB_BACKUP;
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002238 adb_file_name = part_settings->Backup_Folder + "/" + Backup_FileName;
bigbiffce8f83c2015-12-12 18:30:21 -05002239 }
2240 else
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002241 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
bigbiffce8f83c2015-12-12 18:30:21 -05002242
2243 part_settings->total_restore_size = Backup_Size;
2244
2245 if (part_settings->adbbackup) {
2246 if (!twadbbu::Write_TWIMG(adb_file_name, Backup_Size))
2247 return false;
2248 }
2249
2250 if (!Raw_Read_Write(part_settings))
Ethan Yonker472f5062016-02-25 13:47:30 -06002251 return false;
2252
bigbiffce8f83c2015-12-12 18:30:21 -05002253 if (part_settings->adbbackup) {
2254 if (!twadbbu::Write_TWEOF())
2255 return false;
Dees_Troy7c2dec82012-09-26 09:49:14 -04002256 }
Dees_Troy43d8b002012-09-17 16:00:01 -04002257 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002258}
2259
bigbiffce8f83c2015-12-12 18:30:21 -05002260bool TWPartition::Raw_Read_Write(PartitionSettings *part_settings) {
2261 unsigned long long RW_Block_Size, Remain = Backup_Size;
2262 int src_fd = -1, dest_fd = -1;
2263 ssize_t bs;
Ethan Yonker472f5062016-02-25 13:47:30 -06002264 bool ret = false;
2265 void* buffer = NULL;
2266 unsigned long long backedup_size = 0;
bigbiffce8f83c2015-12-12 18:30:21 -05002267 string srcfn, destfn;
Ethan Yonker472f5062016-02-25 13:47:30 -06002268
bigbiffce8f83c2015-12-12 18:30:21 -05002269 if (part_settings->PM_Method == PM_BACKUP) {
2270 srcfn = Actual_Block_Device;
2271 if (part_settings->adbbackup)
2272 destfn = TW_ADB_BACKUP;
2273 else
Ethan Yonkere080c1f2016-09-19 13:50:25 -05002274 destfn = part_settings->Backup_Folder + "/" + Backup_FileName;
bigbiffce8f83c2015-12-12 18:30:21 -05002275 }
2276 else {
2277 destfn = Actual_Block_Device;
2278 if (part_settings->adbbackup) {
2279 srcfn = TW_ADB_RESTORE;
2280 } else {
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002281 srcfn = part_settings->Backup_Folder + "/" + Backup_FileName;
bigbiffce8f83c2015-12-12 18:30:21 -05002282 Remain = TWFunc::Get_File_Size(srcfn);
2283 }
2284 }
Ethan Yonker472f5062016-02-25 13:47:30 -06002285
bigbiffce8f83c2015-12-12 18:30:21 -05002286 src_fd = open(srcfn.c_str(), O_RDONLY | O_LARGEFILE);
Ethan Yonker472f5062016-02-25 13:47:30 -06002287 if (src_fd < 0) {
bigbiffce8f83c2015-12-12 18:30:21 -05002288 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(srcfn.c_str())(strerror(errno)));
Ethan Yonker472f5062016-02-25 13:47:30 -06002289 return false;
2290 }
bigbiffce8f83c2015-12-12 18:30:21 -05002291
2292 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 -06002293 if (dest_fd < 0) {
bigbiffce8f83c2015-12-12 18:30:21 -05002294 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(destfn.c_str())(strerror(errno)));
Ethan Yonker472f5062016-02-25 13:47:30 -06002295 goto exit;
2296 }
bigbiffce8f83c2015-12-12 18:30:21 -05002297
2298 LOGINFO("Reading '%s', writing '%s'\n", srcfn.c_str(), destfn.c_str());
2299
2300 if (part_settings->adbbackup) {
2301 RW_Block_Size = MAX_ADB_READ;
2302 bs = MAX_ADB_READ;
2303 }
2304 else {
2305 RW_Block_Size = 1048576LLU; // 1MB
2306 bs = (ssize_t)(RW_Block_Size);
2307 }
2308
Ethan Yonker472f5062016-02-25 13:47:30 -06002309 buffer = malloc((size_t)bs);
2310 if (!buffer) {
2311 LOGINFO("Raw_Read_Write failed to malloc\n");
2312 goto exit;
2313 }
bigbiffce8f83c2015-12-12 18:30:21 -05002314
2315 if (part_settings->progress)
2316 part_settings->progress->SetPartitionSize(part_settings->total_restore_size);
2317
Ethan Yonker472f5062016-02-25 13:47:30 -06002318 while (Remain > 0) {
2319 if (Remain < RW_Block_Size)
bigbiffce8f83c2015-12-12 18:30:21 -05002320 bs = (ssize_t)(Remain);
Matt Mowera8a89d12016-12-30 18:10:37 -06002321 if (read(src_fd, buffer, bs) != bs) {
Ethan Yonker472f5062016-02-25 13:47:30 -06002322 LOGINFO("Error reading source fd (%s)\n", strerror(errno));
2323 goto exit;
2324 }
2325 if (write(dest_fd, buffer, bs) != bs) {
2326 LOGINFO("Error writing destination fd (%s)\n", strerror(errno));
2327 goto exit;
2328 }
2329 backedup_size += (unsigned long long)(bs);
Matt Mower029a82d2017-01-08 13:12:38 -06002330 Remain -= (unsigned long long)(bs);
bigbiffce8f83c2015-12-12 18:30:21 -05002331 if (part_settings->progress)
2332 part_settings->progress->UpdateSize(backedup_size);
Ethan Yonker472f5062016-02-25 13:47:30 -06002333 if (PartitionManager.Check_Backup_Cancel() != 0)
2334 goto exit;
2335 }
bigbiffce8f83c2015-12-12 18:30:21 -05002336 if (part_settings->progress)
2337 part_settings->progress->UpdateDisplayDetails(true);
Ethan Yonker472f5062016-02-25 13:47:30 -06002338 fsync(dest_fd);
nailyka083dc62016-11-11 21:41:28 +01002339
2340 if (!part_settings->adbbackup && part_settings->PM_Method == PM_BACKUP) {
2341 tw_set_default_metadata(destfn.c_str());
2342 LOGINFO("Restored default metadata for %s\n", destfn.c_str());
2343 }
2344
Ethan Yonker472f5062016-02-25 13:47:30 -06002345 ret = true;
2346exit:
2347 if (src_fd >= 0)
2348 close(src_fd);
2349 if (dest_fd >= 0)
2350 close(dest_fd);
2351 if (buffer)
2352 free(buffer);
2353 return ret;
2354}
2355
bigbiffce8f83c2015-12-12 18:30:21 -05002356bool TWPartition::Backup_Dump_Image(PartitionSettings *part_settings) {
Vojtech Bocek05534202013-09-11 08:11:56 +02002357 string Full_FileName, Command;
Dees_Troy43d8b002012-09-17 16:00:01 -04002358
Ethan Yonker74db1572015-10-28 12:44:49 -05002359 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, gui_parse_text("{@backing}"));
2360 gui_msg(Msg("backing_up=Backing up {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002361
bigbiffce8f83c2015-12-12 18:30:21 -05002362 if (part_settings->progress)
2363 part_settings->progress->SetPartitionSize(Backup_Size);
Dees_Troy43d8b002012-09-17 16:00:01 -04002364
Ethan Yonker472f5062016-02-25 13:47:30 -06002365 Backup_FileName = Backup_Name + "." + Current_File_System + ".win";
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002366 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04002367
2368 Command = "dump_image " + MTD_Name + " '" + Full_FileName + "'";
bigbiffce8f83c2015-12-12 18:30:21 -05002369
Dees_Troy2673cec2013-04-02 20:22:16 +00002370 LOGINFO("Backup command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02002371 TWFunc::Exec_Cmd(Command);
Ethan Yonker4b94cfd2014-12-11 10:00:45 -06002372 tw_set_default_metadata(Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04002373 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
2374 // 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 -06002375 gui_msg(Msg(msg::kError, "backup_size=Backup file size for '{1}' is 0 bytes.")(Full_FileName));
Dees_Troy7c2dec82012-09-26 09:49:14 -04002376 return false;
2377 }
bigbiffce8f83c2015-12-12 18:30:21 -05002378 if (part_settings->progress)
2379 part_settings->progress->UpdateSize(Backup_Size);
2380
Dees_Troy43d8b002012-09-17 16:00:01 -04002381 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002382}
2383
bigbiffce8f83c2015-12-12 18:30:21 -05002384unsigned long long TWPartition::Get_Restore_Size(PartitionSettings *part_settings) {
2385 if (!part_settings->adbbackup) {
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002386 InfoManager restore_info(part_settings->Backup_Folder + "/" + Backup_Name + ".info");
bigbiffce8f83c2015-12-12 18:30:21 -05002387 if (restore_info.LoadValues() == 0) {
2388 if (restore_info.GetValue("backup_size", Restore_Size) == 0) {
2389 LOGINFO("Read info file, restore size is %llu\n", Restore_Size);
2390 return Restore_Size;
2391 }
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002392 }
2393 }
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002394
Matt Mower029a82d2017-01-08 13:12:38 -06002395 string Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
2396 string Restore_File_System = Get_Restore_File_System(part_settings);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002397
2398 if (Is_Image(Restore_File_System)) {
2399 Restore_Size = TWFunc::Get_File_Size(Full_FileName);
2400 return Restore_Size;
2401 }
2402
2403 twrpTar tar;
2404 tar.setdir(Backup_Path);
2405 tar.setfn(Full_FileName);
bigbiffce8f83c2015-12-12 18:30:21 -05002406 tar.backup_name = Full_FileName;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002407#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
2408 string Password;
2409 DataManager::GetValue("tw_restore_password", Password);
2410 if (!Password.empty())
2411 tar.setpassword(Password);
2412#endif
2413 tar.partition_name = Backup_Name;
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002414 tar.backup_folder = part_settings->Backup_Folder;
bigbiffce8f83c2015-12-12 18:30:21 -05002415 tar.part_settings = part_settings;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002416 Restore_Size = tar.get_size();
2417 return Restore_Size;
2418}
2419
bigbiffce8f83c2015-12-12 18:30:21 -05002420bool TWPartition::Restore_Tar(PartitionSettings *part_settings) {
Ethan Yonker472f5062016-02-25 13:47:30 -06002421 string Full_FileName;
Dees Troy4159aed2014-02-28 17:24:43 +00002422 bool ret = false;
bigbiffce8f83c2015-12-12 18:30:21 -05002423 string Restore_File_System = Get_Restore_File_System(part_settings);
Dees_Troy43d8b002012-09-17 16:00:01 -04002424
Dees_Troye58d5262012-09-21 12:27:57 -04002425 if (Has_Android_Secure) {
Dees_Troye58d5262012-09-21 12:27:57 -04002426 if (!Wipe_AndSec())
2427 return false;
Gary Peck43acadf2012-11-21 21:19:01 -08002428 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002429 gui_msg(Msg("wiping=Wiping {1}")(Backup_Display_Name));
Ethan Yonker5eac2222014-06-11 12:22:55 -05002430 if (Has_Data_Media && Mount_Point == "/data" && Restore_File_System != Current_File_System) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002431 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 -05002432 if (!Wipe_Data_Without_Wiping_Media())
2433 return false;
2434 } else {
2435 if (!Wipe(Restore_File_System))
2436 return false;
2437 }
Dees_Troye58d5262012-09-21 12:27:57 -04002438 }
Matt Mower3c366972015-12-25 19:28:31 -06002439 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Backup_Display_Name, gui_parse_text("{@restoring_hdr}"));
Ethan Yonker74db1572015-10-28 12:44:49 -05002440 gui_msg(Msg("restoring=Restoring {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002441
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05002442 // Remount as read/write as needed so we can restore the backup
2443 if (!ReMount_RW(true))
Dees_Troy43d8b002012-09-17 16:00:01 -04002444 return false;
2445
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002446 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
Ethan Yonker87af5632014-02-10 11:56:35 -06002447 twrpTar tar;
bigbiffce8f83c2015-12-12 18:30:21 -05002448 tar.part_settings = part_settings;
Ethan Yonker87af5632014-02-10 11:56:35 -06002449 tar.setdir(Backup_Path);
2450 tar.setfn(Full_FileName);
2451 tar.backup_name = Backup_Name;
2452#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
2453 string Password;
2454 DataManager::GetValue("tw_restore_password", Password);
2455 if (!Password.empty())
2456 tar.setpassword(Password);
2457#endif
bigbiffce8f83c2015-12-12 18:30:21 -05002458 part_settings->progress->SetPartitionSize(Get_Restore_Size(part_settings));
2459 if (tar.extractTarFork() != 0)
Dees Troy4159aed2014-02-28 17:24:43 +00002460 ret = false;
2461 else
2462 ret = true;
2463#ifdef HAVE_CAPABILITIES
2464 // Restore capabilities to the run-as binary
2465 if (Mount_Point == "/system" && Mount(true) && TWFunc::Path_Exists("/system/bin/run-as")) {
2466 struct vfs_cap_data cap_data;
2467 uint64_t capabilities = (1 << CAP_SETUID) | (1 << CAP_SETGID);
2468
2469 memset(&cap_data, 0, sizeof(cap_data));
2470 cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE;
2471 cap_data.data[0].permitted = (uint32_t) (capabilities & 0xffffffff);
2472 cap_data.data[0].inheritable = 0;
2473 cap_data.data[1].permitted = (uint32_t) (capabilities >> 32);
2474 cap_data.data[1].inheritable = 0;
2475 if (setxattr("/system/bin/run-as", XATTR_NAME_CAPS, &cap_data, sizeof(cap_data), 0) < 0) {
2476 LOGINFO("Failed to reset capabilities of /system/bin/run-as binary.\n");
2477 } else {
2478 LOGINFO("Reset capabilities of /system/bin/run-as binary successful.\n");
2479 }
2480 }
2481#endif
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05002482 if (Mount_Read_Only || Mount_Flags & MS_RDONLY)
2483 // Remount as read only when restoration is complete
2484 ReMount(true);
2485
Dees Troy4159aed2014-02-28 17:24:43 +00002486 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04002487}
2488
bigbiffce8f83c2015-12-12 18:30:21 -05002489bool TWPartition::Restore_Image(PartitionSettings *part_settings) {
Ethan Yonker96af84a2015-01-05 14:58:36 -06002490 string Full_FileName;
bigbiffce8f83c2015-12-12 18:30:21 -05002491 string Restore_File_System = Get_Restore_File_System(part_settings);
Dees_Troy43d8b002012-09-17 16:00:01 -04002492
Matt Mower3c366972015-12-25 19:28:31 -06002493 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Backup_Display_Name, gui_parse_text("{@restoring_hdr}"));
Ethan Yonker74db1572015-10-28 12:44:49 -05002494 gui_msg(Msg("restoring=Restoring {1}...")(Backup_Display_Name));
bigbiffce8f83c2015-12-12 18:30:21 -05002495
2496 if (part_settings->adbbackup)
2497 Full_FileName = TW_ADB_RESTORE;
2498 else
Ethan Yonkere080c1f2016-09-19 13:50:25 -05002499 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
Gary Peck15e623d2012-11-21 21:07:58 -08002500
Ethan Yonker96af84a2015-01-05 14:58:36 -06002501 if (Restore_File_System == "emmc") {
bigbiffce8f83c2015-12-12 18:30:21 -05002502 if (!part_settings->adbbackup)
2503 part_settings->total_restore_size = (uint64_t)(TWFunc::Get_File_Size(Full_FileName));
2504 if (!Raw_Read_Write(part_settings))
Ethan Yonker96af84a2015-01-05 14:58:36 -06002505 return false;
2506 } else if (Restore_File_System == "mtd" || Restore_File_System == "bml") {
bigbiffce8f83c2015-12-12 18:30:21 -05002507 if (!Flash_Image_FI(Full_FileName, part_settings->progress))
2508 return false;
2509 }
2510
2511 if (part_settings->adbbackup) {
2512 if (!twadbbu::Write_TWEOF())
Ethan Yonker96af84a2015-01-05 14:58:36 -06002513 return false;
Gary Peck15e623d2012-11-21 21:07:58 -08002514 }
Dees_Troy43d8b002012-09-17 16:00:01 -04002515 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002516}
Dees_Troy5bf43922012-09-07 16:07:55 -04002517
2518bool TWPartition::Update_Size(bool Display_Error) {
Dees_Troy0550cfb2012-10-13 11:56:13 -04002519 bool ret = false, Was_Already_Mounted = false;
Dees_Troy51127312012-09-08 13:08:49 -04002520
Ethan Yonker1b190162016-12-05 15:25:19 -06002521 Find_Actual_Block_Device();
2522
2523 if (!Can_Be_Mounted && !Is_Encrypted) {
2524 if (TWFunc::Path_Exists(Actual_Block_Device) && Find_Partition_Size()) {
2525 Used = Size;
2526 Backup_Size = Size;
2527 return true;
2528 }
Dees_Troy5bf43922012-09-07 16:07:55 -04002529 return false;
Ethan Yonker1b190162016-12-05 15:25:19 -06002530 }
Dees_Troy5bf43922012-09-07 16:07:55 -04002531
Dees_Troy0550cfb2012-10-13 11:56:13 -04002532 Was_Already_Mounted = Is_Mounted();
Dees_Troy38bd7602012-09-14 13:33:53 -04002533 if (Removable || Is_Encrypted) {
2534 if (!Mount(false))
2535 return true;
2536 } else if (!Mount(Display_Error))
Dees_Troy5bf43922012-09-07 16:07:55 -04002537 return false;
Dees_Troy51127312012-09-08 13:08:49 -04002538
2539 ret = Get_Size_Via_statfs(Display_Error);
Dees_Troy0550cfb2012-10-13 11:56:13 -04002540 if (!ret || Size == 0) {
2541 if (!Get_Size_Via_df(Display_Error)) {
2542 if (!Was_Already_Mounted)
2543 UnMount(false);
Dees_Troy51127312012-09-08 13:08:49 -04002544 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04002545 }
2546 }
Dees_Troy51127312012-09-08 13:08:49 -04002547
Dees_Troy5bf43922012-09-07 16:07:55 -04002548 if (Has_Data_Media) {
2549 if (Mount(Display_Error)) {
Ethan Yonker3fdcda42016-11-30 12:29:37 -06002550 Used = backup_exclusions.Get_Folder_Size(Mount_Point);
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002551 Backup_Size = Used;
2552 int bak = (int)(Used / 1048576LLU);
Dees_Troy51127312012-09-08 13:08:49 -04002553 int fre = (int)(Free / 1048576LLU);
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002554 LOGINFO("Data backup size is %iMB, free: %iMB.\n", bak, fre);
Dees_Troy0550cfb2012-10-13 11:56:13 -04002555 } else {
2556 if (!Was_Already_Mounted)
2557 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04002558 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04002559 }
Dees_Troye58d5262012-09-21 12:27:57 -04002560 } else if (Has_Android_Secure) {
2561 if (Mount(Display_Error))
Ethan Yonker3fdcda42016-11-30 12:29:37 -06002562 Backup_Size = backup_exclusions.Get_Folder_Size(Backup_Path);
Dees_Troy0550cfb2012-10-13 11:56:13 -04002563 else {
2564 if (!Was_Already_Mounted)
2565 UnMount(false);
Dees_Troye58d5262012-09-21 12:27:57 -04002566 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04002567 }
Dees_Troy5bf43922012-09-07 16:07:55 -04002568 }
Dees_Troy0550cfb2012-10-13 11:56:13 -04002569 if (!Was_Already_Mounted)
2570 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04002571 return true;
Dees_Troy51127312012-09-08 13:08:49 -04002572}
Dees_Troy38bd7602012-09-14 13:33:53 -04002573
2574void TWPartition::Find_Actual_Block_Device(void) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002575 if (Is_Decrypted && !Decrypted_Block_Device.empty()) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002576 Actual_Block_Device = Decrypted_Block_Device;
Ethan Yonker1b190162016-12-05 15:25:19 -06002577 if (TWFunc::Path_Exists(Decrypted_Block_Device)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002578 Is_Present = true;
Ethan Yonker1b190162016-12-05 15:25:19 -06002579 return;
2580 }
2581 } else if (SlotSelect && TWFunc::Path_Exists(Primary_Block_Device + PartitionManager.Get_Active_Slot_Suffix())) {
2582 Actual_Block_Device = Primary_Block_Device + PartitionManager.Get_Active_Slot_Suffix();
2583 unlink(Primary_Block_Device.c_str());
2584 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
2585 Is_Present = true;
2586 return;
Dees_Troy43d8b002012-09-17 16:00:01 -04002587 } else if (TWFunc::Path_Exists(Primary_Block_Device)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002588 Is_Present = true;
2589 Actual_Block_Device = Primary_Block_Device;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002590 return;
2591 }
Ethan Yonker1b190162016-12-05 15:25:19 -06002592 if (!Alternate_Block_Device.empty() && TWFunc::Path_Exists(Alternate_Block_Device)) {
Dees_Troy3f04d032012-10-07 18:20:09 -04002593 Actual_Block_Device = Alternate_Block_Device;
Dees_Troy38bd7602012-09-14 13:33:53 -04002594 Is_Present = true;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002595 } else {
Dees_Troy38bd7602012-09-14 13:33:53 -04002596 Is_Present = false;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002597 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002598}
2599
2600void TWPartition::Recreate_Media_Folder(void) {
2601 string Command;
Ethan Yonker66a19492015-12-10 10:19:45 -06002602 string Media_Path = Mount_Point + "/media";
Dees_Troy38bd7602012-09-14 13:33:53 -04002603
Ethan Yonkerbd7492d2016-12-07 13:55:01 -06002604 if (Is_FBE) {
2605 LOGINFO("Not recreating media folder on FBE\n");
2606 return;
2607 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002608 if (!Mount(true)) {
Ethan Yonker66a19492015-12-10 10:19:45 -06002609 gui_msg(Msg(msg::kError, "recreate_folder_err=Unable to recreate {1} folder.")(Media_Path));
2610 } else if (!TWFunc::Path_Exists(Media_Path)) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002611 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Ethan Yonker66a19492015-12-10 10:19:45 -06002612 LOGINFO("Recreating %s folder.\n", Media_Path.c_str());
2613 mkdir(Media_Path.c_str(), 0770);
xiaolu9416f4f2015-06-04 08:22:23 +08002614 string Internal_path = DataManager::GetStrValue("tw_internal_path");
2615 if (!Internal_path.empty()) {
2616 LOGINFO("Recreating %s folder.\n", Internal_path.c_str());
2617 mkdir(Internal_path.c_str(), 0770);
2618 }
2619#ifdef TW_INTERNAL_STORAGE_PATH
2620 mkdir(EXPAND(TW_INTERNAL_STORAGE_PATH), 0770);
2621#endif
Ethan Yonker5ef301e2014-10-14 10:04:44 -05002622#ifdef HAVE_SELINUX
thata3d31fb2014-12-21 22:27:40 +01002623 // Afterwards, we will try to set the
2624 // default metadata that we were hopefully able to get during
2625 // early boot.
Ethan Yonker66a19492015-12-10 10:19:45 -06002626 tw_set_default_metadata(Media_Path.c_str());
xiaolu9416f4f2015-06-04 08:22:23 +08002627 if (!Internal_path.empty())
2628 tw_set_default_metadata(Internal_path.c_str());
Ethan Yonker5ef301e2014-10-14 10:04:44 -05002629#endif
Ethan Yonker5eac2222014-06-11 12:22:55 -05002630 // Toggle mount to ensure that "internal sdcard" gets mounted
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002631 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Ethan Yonker5eac2222014-06-11 12:22:55 -05002632 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Dees_Troy38bd7602012-09-14 13:33:53 -04002633 }
Dees_Troy43d8b002012-09-17 16:00:01 -04002634}
Dees_Troye58d5262012-09-21 12:27:57 -04002635
2636void TWPartition::Recreate_AndSec_Folder(void) {
Dees_Troye58d5262012-09-21 12:27:57 -04002637 if (!Has_Android_Secure)
2638 return;
Dees_Troy2673cec2013-04-02 20:22:16 +00002639 LOGINFO("Creating %s: %s\n", Backup_Display_Name.c_str(), Symlink_Path.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -04002640 if (!Mount(true)) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002641 gui_msg(Msg(msg::kError, "recreate_folder_err=Unable to recreate {1} folder.")(Backup_Name));
Dees_Troye58d5262012-09-21 12:27:57 -04002642 } else if (!TWFunc::Path_Exists(Symlink_Path)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00002643 LOGINFO("Recreating %s folder.\n", Backup_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002644 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05002645 mkdir(Symlink_Path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002646 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04002647 }
2648}
bigbiff7cb4c332014-11-26 20:36:07 -05002649
2650uint64_t TWPartition::Get_Max_FileSize() {
2651 uint64_t maxFileSize = 0;
2652 const uint64_t constGB = (uint64_t) 1024 * 1024 * 1024;
2653 const uint64_t constTB = (uint64_t) constGB * 1024;
2654 const uint64_t constPB = (uint64_t) constTB * 1024;
bigbiff0c532032014-12-21 13:41:26 -05002655 if (Current_File_System == "ext4")
2656 maxFileSize = 16 * constTB; //16 TB
2657 else if (Current_File_System == "vfat")
2658 maxFileSize = 4 * constGB; //4 GB
2659 else if (Current_File_System == "ntfs")
2660 maxFileSize = 256 * constTB; //256 TB
2661 else if (Current_File_System == "exfat")
2662 maxFileSize = 16 * constPB; //16 PB
2663 else if (Current_File_System == "ext3")
2664 maxFileSize = 2 * constTB; //2 TB
2665 else if (Current_File_System == "f2fs")
2666 maxFileSize = 3.94 * constTB; //3.94 TB
bigbiff7cb4c332014-11-26 20:36:07 -05002667 else
2668 maxFileSize = 100000000L;
2669 return maxFileSize - 1;
2670}
2671
bigbiffce8f83c2015-12-12 18:30:21 -05002672bool TWPartition::Flash_Image(PartitionSettings *part_settings) {
2673 string Restore_File_System, full_filename;
Ethan Yonker96af84a2015-01-05 14:58:36 -06002674
Ethan Yonkere080c1f2016-09-19 13:50:25 -05002675 full_filename = part_settings->Backup_Folder + "/" + Backup_FileName;
Ethan Yonker96af84a2015-01-05 14:58:36 -06002676
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002677 LOGINFO("Image filename is: %s\n", Backup_FileName.c_str());
bigbiffce8f83c2015-12-12 18:30:21 -05002678
2679 if (Backup_Method == BM_FILES) {
Ethan Yonker96af84a2015-01-05 14:58:36 -06002680 LOGERR("Cannot flash images to file systems\n");
2681 return false;
2682 } else if (!Can_Flash_Img) {
2683 LOGERR("Cannot flash images to partitions %s\n", Display_Name.c_str());
2684 return false;
2685 } else {
2686 if (!Find_Partition_Size()) {
2687 LOGERR("Unable to find partition size for '%s'\n", Mount_Point.c_str());
2688 return false;
2689 }
bigbiffce8f83c2015-12-12 18:30:21 -05002690 unsigned long long image_size = TWFunc::Get_File_Size(full_filename);
Ethan Yonker96af84a2015-01-05 14:58:36 -06002691 if (image_size > Size) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002692 LOGINFO("Size (%llu bytes) of image '%s' is larger than target device '%s' (%llu bytes)\n",
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002693 image_size, Backup_FileName.c_str(), Actual_Block_Device.c_str(), Size);
Ethan Yonker74db1572015-10-28 12:44:49 -05002694 gui_err("img_size_err=Size of image is larger than target device");
Ethan Yonker96af84a2015-01-05 14:58:36 -06002695 return false;
2696 }
bigbiffce8f83c2015-12-12 18:30:21 -05002697 if (Backup_Method == BM_DD) {
2698 if (!part_settings->adbbackup) {
2699 if (Is_Sparse_Image(full_filename)) {
2700 return Flash_Sparse_Image(full_filename);
2701 }
Ethan Yonker472f5062016-02-25 13:47:30 -06002702 }
bigbiffce8f83c2015-12-12 18:30:21 -05002703 return Raw_Read_Write(part_settings);
2704 } else if (Backup_Method == BM_FLASH_UTILS) {
2705 return Flash_Image_FI(full_filename, NULL);
Ethan Yonker472f5062016-02-25 13:47:30 -06002706 }
Ethan Yonker96af84a2015-01-05 14:58:36 -06002707 }
2708
2709 LOGERR("Unknown flash method for '%s'\n", Mount_Point.c_str());
2710 return false;
2711}
2712
Ethan Yonker472f5062016-02-25 13:47:30 -06002713bool TWPartition::Is_Sparse_Image(const string& Filename) {
HashBanged974bb2016-01-30 14:20:09 -05002714 uint32_t magic = 0;
2715 int fd = open(Filename.c_str(), O_RDONLY);
2716 if (fd < 0) {
2717 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Filename)(strerror(errno)));
2718 return false;
2719 }
bigbiffce8f83c2015-12-12 18:30:21 -05002720
HashBanged974bb2016-01-30 14:20:09 -05002721 if (read(fd, &magic, sizeof(magic)) != sizeof(magic)) {
2722 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Filename)(strerror(errno)));
2723 close(fd);
2724 return false;
2725 }
2726 close(fd);
Ethan Yonker472f5062016-02-25 13:47:30 -06002727 if (magic == SPARSE_HEADER_MAGIC)
2728 return true;
2729 return false;
2730}
2731
2732bool TWPartition::Flash_Sparse_Image(const string& Filename) {
2733 string Command;
2734
2735 gui_msg(Msg("flashing=Flashing {1}...")(Display_Name));
2736
2737 Command = "simg2img '" + Filename + "' '" + Actual_Block_Device + "'";
Ethan Yonker96af84a2015-01-05 14:58:36 -06002738 LOGINFO("Flash command: '%s'\n", Command.c_str());
2739 TWFunc::Exec_Cmd(Command);
2740 return true;
2741}
2742
Ethan Yonker472f5062016-02-25 13:47:30 -06002743bool TWPartition::Flash_Image_FI(const string& Filename, ProgressTracking *progress) {
Ethan Yonker96af84a2015-01-05 14:58:36 -06002744 string Command;
Ethan Yonker472f5062016-02-25 13:47:30 -06002745 unsigned long long file_size;
Ethan Yonker96af84a2015-01-05 14:58:36 -06002746
Ethan Yonker74db1572015-10-28 12:44:49 -05002747 gui_msg(Msg("flashing=Flashing {1}...")(Display_Name));
Ethan Yonker472f5062016-02-25 13:47:30 -06002748 if (progress) {
2749 file_size = (unsigned long long)(TWFunc::Get_File_Size(Filename));
2750 progress->SetPartitionSize(file_size);
2751 }
Ethan Yonker96af84a2015-01-05 14:58:36 -06002752 // Sometimes flash image doesn't like to flash due to the first 2KB matching, so we erase first to ensure that it flashes
2753 Command = "erase_image " + MTD_Name;
2754 LOGINFO("Erase command: '%s'\n", Command.c_str());
2755 TWFunc::Exec_Cmd(Command);
2756 Command = "flash_image " + MTD_Name + " '" + Filename + "'";
2757 LOGINFO("Flash command: '%s'\n", Command.c_str());
2758 TWFunc::Exec_Cmd(Command);
Ethan Yonker472f5062016-02-25 13:47:30 -06002759 if (progress)
2760 progress->UpdateSize(file_size);
Ethan Yonker96af84a2015-01-05 14:58:36 -06002761 return true;
2762}
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05002763
2764void TWPartition::Change_Mount_Read_Only(bool new_value) {
2765 Mount_Read_Only = new_value;
2766}
2767
bigbiffce8f83c2015-12-12 18:30:21 -05002768bool TWPartition::Is_Read_Only() {
2769 return Mount_Read_Only;
2770}
2771
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05002772int TWPartition::Check_Lifetime_Writes() {
2773 bool original_read_only = Mount_Read_Only;
2774 int ret = 1;
2775
2776 Mount_Read_Only = true;
2777 if (Mount(false)) {
2778 Find_Actual_Block_Device();
2779 string block = basename(Actual_Block_Device.c_str());
2780 string file = "/sys/fs/" + Current_File_System + "/" + block + "/lifetime_write_kbytes";
2781 string result;
2782 if (TWFunc::Path_Exists(file)) {
2783 if (TWFunc::read_file(file, result) != 0) {
2784 LOGINFO("Check_Lifetime_Writes of '%s' failed to read_file\n", file.c_str());
2785 } else {
2786 LOGINFO("Check_Lifetime_Writes result: '%s'\n", result.c_str());
2787 if (result == "0") {
2788 ret = 0;
2789 }
2790 }
2791 } else {
2792 LOGINFO("Check_Lifetime_Writes file does not exist '%s'\n", file.c_str());
2793 }
2794 UnMount(true);
2795 } else {
2796 LOGINFO("Check_Lifetime_Writes failed to mount '%s'\n", Mount_Point.c_str());
2797 }
2798 Mount_Read_Only = original_read_only;
2799 return ret;
2800}
Ethan Yonker66a19492015-12-10 10:19:45 -06002801
2802int TWPartition::Decrypt_Adopted() {
2803#ifdef TW_INCLUDE_CRYPTO
2804 int ret = 1;
2805 Is_Adopted_Storage = false;
2806 string Adopted_Key_File = "";
2807
2808 if (!Removable)
2809 return ret;
2810
2811 int fd = open(Alternate_Block_Device.c_str(), O_RDONLY);
2812 if (fd < 0) {
2813 LOGINFO("failed to open '%s'\n", Alternate_Block_Device.c_str());
2814 return ret;
2815 }
2816 char type_guid[80];
2817 char part_guid[80];
2818
2819 if (gpt_disk_get_partition_info(fd, 2, type_guid, part_guid) == 0) {
2820 LOGINFO("type: '%s'\n", type_guid);
2821 LOGINFO("part: '%s'\n", part_guid);
2822 Adopted_GUID = part_guid;
2823 LOGINFO("Adopted_GUID '%s'\n", Adopted_GUID.c_str());
2824 if (strcmp(type_guid, TWGptAndroidExpand) == 0) {
2825 LOGINFO("android_expand found\n");
2826 Adopted_Key_File = "/data/misc/vold/expand_";
2827 Adopted_Key_File += part_guid;
2828 Adopted_Key_File += ".key";
2829 if (TWFunc::Path_Exists(Adopted_Key_File)) {
2830 Is_Adopted_Storage = true;
2831 /* Until we find a use case for this, I think it is safe
2832 * to disable USB Mass Storage whenever adopted storage
2833 * is present.
2834 */
2835 LOGINFO("Detected adopted storage, disabling USB mass storage mode\n");
2836 DataManager::SetValue("tw_has_usb_storage", 0);
2837 }
2838 }
2839 }
2840
2841 if (Is_Adopted_Storage) {
2842 string Adopted_Block_Device = Alternate_Block_Device + "p2";
2843 if (!TWFunc::Path_Exists(Adopted_Block_Device)) {
2844 Adopted_Block_Device = Alternate_Block_Device + "2";
2845 if (!TWFunc::Path_Exists(Adopted_Block_Device)) {
2846 LOGINFO("Adopted block device does not exist\n");
2847 goto exit;
2848 }
2849 }
2850 LOGINFO("key file is '%s', block device '%s'\n", Adopted_Key_File.c_str(), Adopted_Block_Device.c_str());
2851 char crypto_blkdev[MAXPATHLEN];
2852 std::string thekey;
2853 int fdkey = open(Adopted_Key_File.c_str(), O_RDONLY);
2854 if (fdkey < 0) {
2855 LOGINFO("failed to open key file\n");
2856 goto exit;
2857 }
2858 char buf[512];
2859 ssize_t n;
2860 while ((n = read(fdkey, &buf[0], sizeof(buf))) > 0) {
2861 thekey.append(buf, n);
2862 }
2863 close(fdkey);
2864 unsigned char* key = (unsigned char*) thekey.data();
2865 cryptfs_revert_ext_volume(part_guid);
2866
2867 ret = cryptfs_setup_ext_volume(part_guid, Adopted_Block_Device.c_str(), key, thekey.size(), crypto_blkdev);
2868 if (ret == 0) {
2869 LOGINFO("adopted storage new block device: '%s'\n", crypto_blkdev);
2870 Decrypted_Block_Device = crypto_blkdev;
2871 Is_Decrypted = true;
2872 Is_Encrypted = true;
2873 Find_Actual_Block_Device();
Kjell Braden3126a112016-06-19 16:58:15 +00002874 if (!Mount_Storage_Retry(false)) {
Ethan Yonker66a19492015-12-10 10:19:45 -06002875 LOGERR("Failed to mount decrypted adopted storage device\n");
2876 Is_Decrypted = false;
2877 Is_Encrypted = false;
2878 cryptfs_revert_ext_volume(part_guid);
2879 ret = 1;
2880 } else {
Ethan Yonkerfcf3f242016-02-16 12:30:26 -06002881 UnMount(false);
2882 Has_Android_Secure = false;
2883 Symlink_Path = "";
2884 Symlink_Mount_Point = "";
2885 Backup_Name = Mount_Point.substr(1);
2886 Backup_Path = Mount_Point;
2887 TWPartition* sdext = PartitionManager.Find_Partition_By_Path("/sd-ext");
2888 if (sdext && sdext->Actual_Block_Device == Adopted_Block_Device) {
2889 LOGINFO("Removing /sd-ext from partition list due to adopted storage\n");
2890 PartitionManager.Remove_Partition_By_Path("/sd-ext");
2891 }
Ethan Yonker66a19492015-12-10 10:19:45 -06002892 Setup_Data_Media();
2893 Recreate_Media_Folder();
2894 Wipe_Available_in_GUI = true;
2895 Wipe_During_Factory_Reset = true;
2896 Can_Be_Backed_Up = true;
2897 Can_Encrypt_Backup = true;
2898 Use_Userdata_Encryption = true;
2899 Is_Storage = true;
2900 Storage_Name = "Adopted Storage";
2901 Is_SubPartition = true;
2902 SubPartition_Of = "/data";
2903 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
2904 DataManager::SetValue("tw_has_adopted_storage", 1);
2905 }
2906 } else {
2907 LOGERR("Failed to setup adopted storage decryption\n");
2908 }
2909 }
2910exit:
2911 return ret;
2912#else
2913 LOGINFO("Decrypt_Adopted: no crypto support\n");
2914 return 1;
2915#endif
2916}
2917
2918void TWPartition::Revert_Adopted() {
2919#ifdef TW_INCLUDE_CRYPTO
2920 if (!Adopted_GUID.empty()) {
2921 PartitionManager.Remove_MTP_Storage(Mount_Point);
2922 UnMount(false);
2923 cryptfs_revert_ext_volume(Adopted_GUID.c_str());
2924 Is_Adopted_Storage = false;
2925 Is_Encrypted = false;
2926 Is_Decrypted = false;
2927 Decrypted_Block_Device = "";
2928 Find_Actual_Block_Device();
2929 Wipe_During_Factory_Reset = false;
2930 Can_Be_Backed_Up = false;
2931 Can_Encrypt_Backup = false;
2932 Use_Userdata_Encryption = false;
2933 Is_SubPartition = false;
2934 SubPartition_Of = "";
2935 Has_Data_Media = false;
2936 Storage_Path = Mount_Point;
2937 if (!Symlink_Mount_Point.empty()) {
2938 TWPartition* Dat = PartitionManager.Find_Partition_By_Path("/data");
2939 if (Dat) {
2940 Dat->UnMount(false);
2941 Dat->Symlink_Mount_Point = Symlink_Mount_Point;
2942 }
2943 Symlink_Mount_Point = "";
2944 }
2945 }
2946#else
2947 LOGINFO("Revert_Adopted: no crypto support\n");
2948#endif
2949}