blob: 81374f8031b47747c1067036ab7db823c34c0aec [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);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001738 return Restore_File_System;
Dees_Troy51a0e822012-09-05 15:24:24 -04001739}
1740
1741string TWPartition::Backup_Method_By_Name() {
bigbiffce8f83c2015-12-12 18:30:21 -05001742 if (Backup_Method == BM_NONE)
Dees_Troy38bd7602012-09-14 13:33:53 -04001743 return "none";
bigbiffce8f83c2015-12-12 18:30:21 -05001744 else if (Backup_Method == BM_FILES)
Dees_Troy38bd7602012-09-14 13:33:53 -04001745 return "files";
bigbiffce8f83c2015-12-12 18:30:21 -05001746 else if (Backup_Method == BM_DD)
Dees_Troy38bd7602012-09-14 13:33:53 -04001747 return "dd";
bigbiffce8f83c2015-12-12 18:30:21 -05001748 else if (Backup_Method == BM_FLASH_UTILS)
Dees_Troy38bd7602012-09-14 13:33:53 -04001749 return "flash_utils";
1750 else
1751 return "undefined";
1752 return "ERROR!";
Dees_Troy51a0e822012-09-05 15:24:24 -04001753}
1754
1755bool TWPartition::Decrypt(string Password) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001756 LOGINFO("STUB TWPartition::Decrypt, password: '%s'\n", Password.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001757 // Is this needed?
Dees_Troy51a0e822012-09-05 15:24:24 -04001758 return 1;
1759}
1760
1761bool TWPartition::Wipe_Encryption() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001762 bool Save_Data_Media = Has_Data_Media;
1763
1764 if (!UnMount(true))
1765 return false;
1766
Dees_Troy38bd7602012-09-14 13:33:53 -04001767 Has_Data_Media = false;
Dees_Troy74fb2e92013-04-15 14:35:47 +00001768 Decrypted_Block_Device = "";
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001769#ifdef TW_INCLUDE_CRYPTO
Ethan Yonker1b190162016-12-05 15:25:19 -06001770 if (Is_Decrypted && !Decrypted_Block_Device.empty()) {
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001771 if (!UnMount(true))
1772 return false;
Matt Mower2b18a532015-02-20 16:58:05 -06001773 if (delete_crypto_blk_dev((char*)("userdata")) != 0) {
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001774 LOGERR("Error deleting crypto block device, continuing anyway.\n");
1775 }
1776 }
1777#endif
Dees_Troy74fb2e92013-04-15 14:35:47 +00001778 Is_Decrypted = false;
1779 Is_Encrypted = false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001780 Find_Actual_Block_Device();
Ethan Yonkerbc85b632015-08-09 12:48:14 -05001781 if (Crypto_Key_Location == "footer") {
1782 int newlen, fd;
1783 if (Length != 0) {
1784 newlen = Length;
1785 if (newlen < 0)
1786 newlen = newlen * -1;
1787 } else {
1788 newlen = CRYPT_FOOTER_OFFSET;
1789 }
1790 if ((fd = open(Actual_Block_Device.c_str(), O_RDWR)) < 0) {
1791 gui_print_color("warning", "Unable to open '%s' to wipe crypto key\n", Actual_Block_Device.c_str());
1792 } else {
1793 unsigned int block_count;
1794 if ((ioctl(fd, BLKGETSIZE, &block_count)) == -1) {
1795 gui_print_color("warning", "Unable to get block size for wiping crypto footer.\n");
1796 } else {
1797 off64_t offset = ((off64_t)block_count * 512) - newlen;
1798 if (lseek64(fd, offset, SEEK_SET) == -1) {
1799 gui_print_color("warning", "Unable to lseek64 for wiping crypto footer.\n");
1800 } else {
1801 void* buffer = malloc(newlen);
1802 if (!buffer) {
1803 gui_print_color("warning", "Failed to malloc for wiping crypto footer.\n");
1804 } else {
1805 memset(buffer, 0, newlen);
1806 int ret = write(fd, buffer, newlen);
1807 if (ret != newlen) {
1808 gui_print_color("warning", "Failed to wipe crypto footer.\n");
1809 } else {
1810 LOGINFO("Successfully wiped crypto footer.\n");
1811 }
Matt Mower13a8f0b2015-09-26 15:40:03 -05001812 free(buffer);
Ethan Yonkerbc85b632015-08-09 12:48:14 -05001813 }
1814 }
1815 }
1816 close(fd);
1817 }
1818 } else {
Ethan Yonkerc2dafbb2016-03-15 22:20:59 -05001819 if (TWFunc::IOCTL_Get_Block_Size(Crypto_Key_Location.c_str()) >= 16384LLU) {
1820 string Command = "dd of='" + Crypto_Key_Location + "' if=/dev/zero bs=16384 count=1";
1821 TWFunc::Exec_Cmd(Command);
1822 } else {
1823 LOGINFO("Crypto key location reports size < 16K so not wiping crypto footer.\n");
1824 }
Ethan Yonkerbc85b632015-08-09 12:48:14 -05001825 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001826 if (Wipe(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001827 Has_Data_Media = Save_Data_Media;
1828 if (Has_Data_Media && !Symlink_Mount_Point.empty()) {
1829 Recreate_Media_Folder();
Ethan Yonker726a0202014-12-16 20:01:38 -06001830 if (Mount(false))
1831 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04001832 }
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001833 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
Ethan Yonker83e82572014-04-04 10:59:28 -05001834#ifndef TW_OEM_BUILD
Ethan Yonker74db1572015-10-28 12:44:49 -05001835 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 -05001836#endif
Dees_Troy38bd7602012-09-14 13:33:53 -04001837 return true;
1838 } else {
1839 Has_Data_Media = Save_Data_Media;
Ethan Yonker74db1572015-10-28 12:44:49 -05001840 gui_err("format_data_err=Unable to format to remove encryption.");
Ethan Yonker726a0202014-12-16 20:01:38 -06001841 if (Has_Data_Media && Mount(false))
1842 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04001843 return false;
1844 }
1845 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001846}
1847
1848void TWPartition::Check_FS_Type() {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001849 const char* type;
1850 blkid_probe pr;
Dees_Troy5bf43922012-09-07 16:07:55 -04001851
Dees_Troy68cab492012-12-12 19:29:35 +00001852 if (Fstab_File_System == "yaffs2" || Fstab_File_System == "mtd" || Fstab_File_System == "bml" || Ignore_Blkid)
1853 return; // Running blkid on some mtd devices causes a massive crash or needs to be skipped
Dees_Troy5bf43922012-09-07 16:07:55 -04001854
Dees_Troy38bd7602012-09-14 13:33:53 -04001855 Find_Actual_Block_Device();
Dees_Troy8170a922012-09-18 15:40:25 -04001856 if (!Is_Present)
1857 return;
Dees_Troy51127312012-09-08 13:08:49 -04001858
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001859 pr = blkid_new_probe_from_filename(Actual_Block_Device.c_str());
1860 if (blkid_do_fullprobe(pr)) {
1861 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00001862 LOGINFO("Can't probe device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001863 return;
Dees_Troy5bf43922012-09-07 16:07:55 -04001864 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001865
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05001866 if (blkid_probe_lookup_value(pr, "TYPE", &type, NULL) < 0) {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001867 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00001868 LOGINFO("can't find filesystem on device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001869 return;
1870 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001871
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001872 Current_File_System = type;
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001873 blkid_free_probe(pr);
Dees_Troy51a0e822012-09-05 15:24:24 -04001874}
1875
Gary Peck43acadf2012-11-21 21:19:01 -08001876bool TWPartition::Wipe_EXT23(string File_System) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001877 if (!UnMount(true))
1878 return false;
1879
Dees_Troy43d8b002012-09-17 16:00:01 -04001880 if (TWFunc::Path_Exists("/sbin/mke2fs")) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001881 string command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001882
Greg Wallace4b44fef2015-12-29 15:33:24 -05001883 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mke2fs"));
Dees_Troy38bd7602012-09-14 13:33:53 -04001884 Find_Actual_Block_Device();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001885 command = "mke2fs -t " + File_System + " -m 0 " + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001886 LOGINFO("mke2fs command: %s\n", command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001887 if (TWFunc::Exec_Cmd(command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001888 Current_File_System = File_System;
Dees_Troye58d5262012-09-21 12:27:57 -04001889 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05001890 gui_msg("done=Done.");
Dees_Troy38bd7602012-09-14 13:33:53 -04001891 return true;
1892 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001893 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04001894 return false;
1895 }
1896 } else
1897 return Wipe_RMRF();
1898
1899 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001900}
1901
1902bool TWPartition::Wipe_EXT4() {
Ethan Yonker25f20c12014-10-14 09:04:43 -05001903 Find_Actual_Block_Device();
1904 if (!Is_Present) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001905 LOGINFO("Block device not present, cannot wipe %s.\n", Display_Name.c_str());
1906 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Ethan Yonker25f20c12014-10-14 09:04:43 -05001907 return false;
1908 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001909 if (!UnMount(true))
1910 return false;
1911
Dees_Troyb3265ab2013-08-30 02:59:14 +00001912#if defined(HAVE_SELINUX) && defined(USE_EXT4)
Ethan Yonkerf27497f2014-02-09 11:48:33 -06001913 int ret;
1914 char *secontext = NULL;
1915
Greg Wallace4b44fef2015-12-29 15:33:24 -05001916 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("make_ext4fs"));
Ethan Yonkerf27497f2014-02-09 11:48:33 -06001917
Dees Troy99c8dbf2014-03-10 16:53:58 +00001918 if (!selinux_handle || selabel_lookup(selinux_handle, &secontext, Mount_Point.c_str(), S_IFDIR) < 0) {
Ethan Yonkerf27497f2014-02-09 11:48:33 -06001919 LOGINFO("Cannot lookup security context for '%s'\n", Mount_Point.c_str());
1920 ret = make_ext4fs(Actual_Block_Device.c_str(), Length, Mount_Point.c_str(), NULL);
1921 } else {
1922 ret = make_ext4fs(Actual_Block_Device.c_str(), Length, Mount_Point.c_str(), selinux_handle);
1923 }
1924 if (ret != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001925 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troya95f55c2013-08-17 13:14:43 +00001926 return false;
1927 } else {
bigbiff bigbiffc49d7062013-10-11 20:28:00 -04001928 string sedir = Mount_Point + "/lost+found";
1929 PartitionManager.Mount_By_Path(sedir.c_str(), true);
1930 rmdir(sedir.c_str());
1931 mkdir(sedir.c_str(), S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP);
Dees_Troya95f55c2013-08-17 13:14:43 +00001932 return true;
1933 }
1934#else
Dees_Troy43d8b002012-09-17 16:00:01 -04001935 if (TWFunc::Path_Exists("/sbin/make_ext4fs")) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001936 string Command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001937
Greg Wallace4b44fef2015-12-29 15:33:24 -05001938 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("make_ext4fs"));
Dees_Troy38bd7602012-09-14 13:33:53 -04001939 Find_Actual_Block_Device();
1940 Command = "make_ext4fs";
1941 if (!Is_Decrypted && Length != 0) {
1942 // Only use length if we're not decrypted
1943 char len[32];
1944 sprintf(len, "%i", Length);
1945 Command += " -l ";
1946 Command += len;
1947 }
Dees_Troy5295d582013-09-06 15:51:08 +00001948 if (TWFunc::Path_Exists("/file_contexts")) {
1949 Command += " -S /file_contexts";
1950 }
1951 Command += " -a " + Mount_Point + " " + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001952 LOGINFO("make_ext4fs command: %s\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001953 if (TWFunc::Exec_Cmd(Command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001954 Current_File_System = "ext4";
Dees_Troye58d5262012-09-21 12:27:57 -04001955 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05001956 gui_msg("done=Done.");
Dees_Troy38bd7602012-09-14 13:33:53 -04001957 return true;
1958 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001959 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04001960 return false;
1961 }
1962 } else
Gary Peck43acadf2012-11-21 21:19:01 -08001963 return Wipe_EXT23("ext4");
Dees_Troya95f55c2013-08-17 13:14:43 +00001964#endif
Dees_Troy38bd7602012-09-14 13:33:53 -04001965 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001966}
1967
1968bool TWPartition::Wipe_FAT() {
Vojtech Bocek05534202013-09-11 08:11:56 +02001969 string command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001970
Matt Mower18794c82015-11-11 16:22:45 -06001971 if (TWFunc::Path_Exists("/sbin/mkfs.fat")) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001972 if (!UnMount(true))
1973 return false;
1974
Greg Wallace4b44fef2015-12-29 15:33:24 -05001975 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mkfs.fat"));
Dees_Troy38bd7602012-09-14 13:33:53 -04001976 Find_Actual_Block_Device();
Matt Mower18794c82015-11-11 16:22:45 -06001977 command = "mkfs.fat " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02001978 if (TWFunc::Exec_Cmd(command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001979 Current_File_System = "vfat";
Dees_Troye58d5262012-09-21 12:27:57 -04001980 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05001981 gui_msg("done=Done.");
Dees_Troy38bd7602012-09-14 13:33:53 -04001982 return true;
1983 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001984 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04001985 return false;
1986 }
1987 return true;
1988 }
1989 else
1990 return Wipe_RMRF();
1991
1992 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001993}
1994
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001995bool TWPartition::Wipe_EXFAT() {
Vojtech Bocek05534202013-09-11 08:11:56 +02001996 string command;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001997
1998 if (TWFunc::Path_Exists("/sbin/mkexfatfs")) {
1999 if (!UnMount(true))
2000 return false;
2001
Greg Wallace4b44fef2015-12-29 15:33:24 -05002002 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mkexfatfs"));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002003 Find_Actual_Block_Device();
2004 command = "mkexfatfs " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02002005 if (TWFunc::Exec_Cmd(command) == 0) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002006 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05002007 gui_msg("done=Done.");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002008 return true;
2009 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002010 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002011 return false;
2012 }
2013 return true;
2014 }
2015 return false;
2016}
2017
Dees_Troy38bd7602012-09-14 13:33:53 -04002018bool TWPartition::Wipe_MTD() {
2019 if (!UnMount(true))
2020 return false;
2021
Greg Wallace4b44fef2015-12-29 15:33:24 -05002022 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("MTD"));
Dees_Troy38bd7602012-09-14 13:33:53 -04002023
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02002024 mtd_scan_partitions();
2025 const MtdPartition* mtd = mtd_find_partition_by_name(MTD_Name.c_str());
2026 if (mtd == NULL) {
2027 LOGERR("No mtd partition named '%s'", MTD_Name.c_str());
2028 return false;
2029 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002030
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02002031 MtdWriteContext* ctx = mtd_write_partition(mtd);
2032 if (ctx == NULL) {
2033 LOGERR("Can't write '%s', failed to format.", MTD_Name.c_str());
2034 return false;
2035 }
2036 if (mtd_erase_blocks(ctx, -1) == -1) {
2037 mtd_write_close(ctx);
2038 LOGERR("Failed to format '%s'", MTD_Name.c_str());
2039 return false;
2040 }
2041 if (mtd_write_close(ctx) != 0) {
2042 LOGERR("Failed to close '%s'", MTD_Name.c_str());
2043 return false;
2044 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08002045 Current_File_System = "yaffs2";
Dees_Troye58d5262012-09-21 12:27:57 -04002046 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05002047 gui_msg("done=Done.");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02002048 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002049}
2050
2051bool TWPartition::Wipe_RMRF() {
Dees_Troy38bd7602012-09-14 13:33:53 -04002052 if (!Mount(true))
2053 return false;
Ethan Yonker726a0202014-12-16 20:01:38 -06002054 // This is the only wipe that leaves the partition mounted, so we
2055 // must manually remove the partition from MTP if it is a storage
2056 // partition.
2057 if (Is_Storage)
2058 PartitionManager.Remove_MTP_Storage(MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04002059
Ethan Yonker74db1572015-10-28 12:44:49 -05002060 gui_msg(Msg("remove_all=Removing all files under '{1}'")(Mount_Point));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002061 TWFunc::removeDir(Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04002062 Recreate_AndSec_Folder();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002063 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002064}
2065
Dees_Troye5017042013-08-29 16:38:55 +00002066bool TWPartition::Wipe_F2FS() {
Vojtech Bocek05534202013-09-11 08:11:56 +02002067 string command;
Dees_Troye5017042013-08-29 16:38:55 +00002068
2069 if (TWFunc::Path_Exists("/sbin/mkfs.f2fs")) {
2070 if (!UnMount(true))
2071 return false;
2072
Greg Wallace4b44fef2015-12-29 15:33:24 -05002073 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mkfs.f2fs"));
Dees_Troye5017042013-08-29 16:38:55 +00002074 Find_Actual_Block_Device();
Greg Wallace558aef72016-01-19 21:14:30 -05002075 command = "mkfs.f2fs -t 0";
dhacker29a3fa75f2015-01-17 19:42:33 -05002076 if (!Is_Decrypted && Length != 0) {
2077 // Only use length if we're not decrypted
2078 char len[32];
2079 int mod_length = Length;
2080 if (Length < 0)
2081 mod_length *= -1;
2082 sprintf(len, "%i", mod_length);
2083 command += " -r ";
2084 command += len;
2085 }
2086 command += " " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02002087 if (TWFunc::Exec_Cmd(command) == 0) {
Dees_Troye5017042013-08-29 16:38:55 +00002088 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05002089 gui_msg("done=Done.");
Dees_Troye5017042013-08-29 16:38:55 +00002090 return true;
2091 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002092 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troye5017042013-08-29 16:38:55 +00002093 return false;
2094 }
2095 return true;
2096 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002097 LOGINFO("mkfs.f2fs binary not found, using rm -rf to wipe.\n");
Dees_Troye5017042013-08-29 16:38:55 +00002098 return Wipe_RMRF();
2099 }
2100 return false;
2101}
2102
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002103bool TWPartition::Wipe_NTFS() {
2104 string command;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002105 string Ntfsmake_Binary;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002106
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002107 if (TWFunc::Path_Exists("/sbin/mkntfs"))
2108 Ntfsmake_Binary = "mkntfs";
2109 else if (TWFunc::Path_Exists("/sbin/mkfs.ntfs"))
2110 Ntfsmake_Binary = "mkfs.ntfs";
2111 else
2112 return false;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002113
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002114 if (!UnMount(true))
2115 return false;
2116
Greg Wallace4b44fef2015-12-29 15:33:24 -05002117 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)(Ntfsmake_Binary));
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002118 Find_Actual_Block_Device();
2119 command = "/sbin/" + Ntfsmake_Binary + " " + Actual_Block_Device;
2120 if (TWFunc::Exec_Cmd(command) == 0) {
2121 Recreate_AndSec_Folder();
2122 gui_msg("done=Done.");
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002123 return true;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002124 } else {
2125 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
2126 return false;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002127 }
2128 return false;
2129}
2130
Dees_Troy51a0e822012-09-05 15:24:24 -04002131bool TWPartition::Wipe_Data_Without_Wiping_Media() {
Ethan Yonker83e82572014-04-04 10:59:28 -05002132#ifdef TW_OEM_BUILD
2133 // In an OEM Build we want to do a full format
2134 return Wipe_Encryption();
2135#else
Ethan Yonker66a19492015-12-10 10:19:45 -06002136 bool ret = false;
Dees_Troy38bd7602012-09-14 13:33:53 -04002137
Dees_Troy38bd7602012-09-14 13:33:53 -04002138 if (!Mount(true))
2139 return false;
2140
Ethan Yonker74db1572015-10-28 12:44:49 -05002141 gui_msg("wiping_data=Wiping data without wiping /data/media ...");
Ethan Yonker66a19492015-12-10 10:19:45 -06002142 ret = Wipe_Data_Without_Wiping_Media_Func(Mount_Point + "/");
2143 if (ret)
2144 gui_msg("done=Done.");
2145 return ret;
2146#endif // ifdef TW_OEM_BUILD
2147}
2148
2149bool TWPartition::Wipe_Data_Without_Wiping_Media_Func(const string& parent __unused) {
2150 string dir;
Dees_Troy38bd7602012-09-14 13:33:53 -04002151
2152 DIR* d;
Ethan Yonker66a19492015-12-10 10:19:45 -06002153 d = opendir(parent.c_str());
Dees_Troy16b74352012-11-14 22:27:31 +00002154 if (d != NULL) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002155 struct dirent* de;
2156 while ((de = readdir(d)) != NULL) {
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002157 if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) continue;
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05002158
Ethan Yonker66a19492015-12-10 10:19:45 -06002159 dir = parent;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002160 dir.append(de->d_name);
Ethan Yonker3fdcda42016-11-30 12:29:37 -06002161 if (wipe_exclusions.check_skip_dirs(dir)) {
Ethan Yonker66a19492015-12-10 10:19:45 -06002162 LOGINFO("skipped '%s'\n", dir.c_str());
2163 continue;
2164 }
Dees_Troyce675462013-01-09 19:48:21 +00002165 if (de->d_type == DT_DIR) {
Ethan Yonker66a19492015-12-10 10:19:45 -06002166 dir.append("/");
2167 if (!Wipe_Data_Without_Wiping_Media_Func(dir)) {
2168 closedir(d);
2169 return false;
2170 }
2171 rmdir(dir.c_str());
bigbiff bigbiff98f1f902013-01-19 18:46:13 -05002172 } 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 +00002173 if (!unlink(dir.c_str()))
Ethan Yonker79f88bd2016-12-09 14:52:12 -06002174 LOGINFO("Unable to unlink '%s': %s\n", dir.c_str(), strerror(errno));
Dees_Troyce675462013-01-09 19:48:21 +00002175 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002176 }
2177 closedir(d);
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05002178
Dees_Troy16b74352012-11-14 22:27:31 +00002179 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -04002180 }
Ethan Yonker74db1572015-10-28 12:44:49 -05002181 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy16b74352012-11-14 22:27:31 +00002182 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04002183}
2184
bigbiffce8f83c2015-12-12 18:30:21 -05002185bool TWPartition::Backup_Tar(PartitionSettings *part_settings, pid_t *tar_fork_pid) {
Ethan Yonker472f5062016-02-25 13:47:30 -06002186 string Full_FileName;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002187 twrpTar tar;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05002188
Dees_Troy43d8b002012-09-17 16:00:01 -04002189 if (!Mount(true))
2190 return false;
2191
Dees_Troya13d74f2013-03-24 08:54:55 -05002192 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Backup_Display_Name, "Backing Up");
Ethan Yonker74db1572015-10-28 12:44:49 -05002193 gui_msg(Msg("backing_up=Backing up {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002194
Ethan Yonker472f5062016-02-25 13:47:30 -06002195 DataManager::GetValue(TW_USE_COMPRESSION_VAR, tar.use_compression);
Matt Mowerbb81e5d2014-03-20 18:05:41 -05002196
Dees_Troy83bd4832013-05-04 12:39:56 +00002197#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
Ethan Yonker472f5062016-02-25 13:47:30 -06002198 if (Can_Encrypt_Backup) {
2199 DataManager::GetValue("tw_encrypt_backup", tar.use_encryption);
2200 if (tar.use_encryption) {
2201 if (Use_Userdata_Encryption)
2202 tar.userdata_encryption = tar.use_encryption;
2203 string Password;
2204 DataManager::GetValue("tw_backup_password", Password);
2205 tar.setpassword(Password);
2206 } else {
2207 tar.use_encryption = 0;
2208 }
Dees_Troy83bd4832013-05-04 12:39:56 +00002209 }
2210#endif
Dees_Troy43d8b002012-09-17 16:00:01 -04002211
Ethan Yonker472f5062016-02-25 13:47:30 -06002212 Backup_FileName = Backup_Name + "." + Current_File_System + ".win";
Ethan Yonkere080c1f2016-09-19 13:50:25 -05002213 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
Dees_Troy83bd4832013-05-04 12:39:56 +00002214 tar.has_data_media = Has_Data_Media;
bigbiffce8f83c2015-12-12 18:30:21 -05002215 tar.part_settings = part_settings;
Ethan Yonker3fdcda42016-11-30 12:29:37 -06002216 tar.backup_exclusions = &backup_exclusions;
Dees Troye0a433a2013-12-02 04:10:37 +00002217 tar.setdir(Backup_Path);
2218 tar.setfn(Full_FileName);
2219 tar.setsize(Backup_Size);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002220 tar.partition_name = Backup_Name;
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002221 tar.backup_folder = part_settings->Backup_Folder;
bigbiffce8f83c2015-12-12 18:30:21 -05002222 if (tar.createTarFork(tar_fork_pid) != 0)
Dees Troye0a433a2013-12-02 04:10:37 +00002223 return false;
Dees_Troy43d8b002012-09-17 16:00:01 -04002224 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002225}
2226
bigbiffce8f83c2015-12-12 18:30:21 -05002227bool TWPartition::Backup_Image(PartitionSettings *part_settings) {
2228 string Full_FileName, adb_file_name;
igoriok87e3d932013-01-31 21:03:53 +02002229
Ethan Yonker74db1572015-10-28 12:44:49 -05002230 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, gui_parse_text("{@backing}"));
2231 gui_msg(Msg("backing_up=Backing up {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002232
Ethan Yonker472f5062016-02-25 13:47:30 -06002233 Backup_FileName = Backup_Name + "." + Current_File_System + ".win";
Dees_Troy43d8b002012-09-17 16:00:01 -04002234
bigbiffce8f83c2015-12-12 18:30:21 -05002235 if (part_settings->adbbackup) {
2236 Full_FileName = TW_ADB_BACKUP;
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002237 adb_file_name = part_settings->Backup_Folder + "/" + Backup_FileName;
bigbiffce8f83c2015-12-12 18:30:21 -05002238 }
2239 else
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002240 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
bigbiffce8f83c2015-12-12 18:30:21 -05002241
2242 part_settings->total_restore_size = Backup_Size;
2243
2244 if (part_settings->adbbackup) {
2245 if (!twadbbu::Write_TWIMG(adb_file_name, Backup_Size))
2246 return false;
2247 }
2248
2249 if (!Raw_Read_Write(part_settings))
Ethan Yonker472f5062016-02-25 13:47:30 -06002250 return false;
2251
bigbiffce8f83c2015-12-12 18:30:21 -05002252 if (part_settings->adbbackup) {
2253 if (!twadbbu::Write_TWEOF())
2254 return false;
Dees_Troy7c2dec82012-09-26 09:49:14 -04002255 }
Dees_Troy43d8b002012-09-17 16:00:01 -04002256 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002257}
2258
bigbiffce8f83c2015-12-12 18:30:21 -05002259bool TWPartition::Raw_Read_Write(PartitionSettings *part_settings) {
2260 unsigned long long RW_Block_Size, Remain = Backup_Size;
2261 int src_fd = -1, dest_fd = -1;
2262 ssize_t bs;
Ethan Yonker472f5062016-02-25 13:47:30 -06002263 bool ret = false;
2264 void* buffer = NULL;
2265 unsigned long long backedup_size = 0;
bigbiffce8f83c2015-12-12 18:30:21 -05002266 string srcfn, destfn;
Ethan Yonker472f5062016-02-25 13:47:30 -06002267
bigbiffce8f83c2015-12-12 18:30:21 -05002268 if (part_settings->PM_Method == PM_BACKUP) {
2269 srcfn = Actual_Block_Device;
2270 if (part_settings->adbbackup)
2271 destfn = TW_ADB_BACKUP;
2272 else
Ethan Yonkere080c1f2016-09-19 13:50:25 -05002273 destfn = part_settings->Backup_Folder + "/" + Backup_FileName;
bigbiffce8f83c2015-12-12 18:30:21 -05002274 }
2275 else {
2276 destfn = Actual_Block_Device;
2277 if (part_settings->adbbackup) {
2278 srcfn = TW_ADB_RESTORE;
2279 } else {
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002280 srcfn = part_settings->Backup_Folder + "/" + Backup_FileName;
bigbiffce8f83c2015-12-12 18:30:21 -05002281 Remain = TWFunc::Get_File_Size(srcfn);
2282 }
2283 }
Ethan Yonker472f5062016-02-25 13:47:30 -06002284
bigbiffce8f83c2015-12-12 18:30:21 -05002285 src_fd = open(srcfn.c_str(), O_RDONLY | O_LARGEFILE);
Ethan Yonker472f5062016-02-25 13:47:30 -06002286 if (src_fd < 0) {
bigbiffce8f83c2015-12-12 18:30:21 -05002287 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(srcfn.c_str())(strerror(errno)));
Ethan Yonker472f5062016-02-25 13:47:30 -06002288 return false;
2289 }
bigbiffce8f83c2015-12-12 18:30:21 -05002290
2291 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 -06002292 if (dest_fd < 0) {
bigbiffce8f83c2015-12-12 18:30:21 -05002293 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(destfn.c_str())(strerror(errno)));
Ethan Yonker472f5062016-02-25 13:47:30 -06002294 goto exit;
2295 }
bigbiffce8f83c2015-12-12 18:30:21 -05002296
2297 LOGINFO("Reading '%s', writing '%s'\n", srcfn.c_str(), destfn.c_str());
2298
2299 if (part_settings->adbbackup) {
2300 RW_Block_Size = MAX_ADB_READ;
2301 bs = MAX_ADB_READ;
2302 }
2303 else {
2304 RW_Block_Size = 1048576LLU; // 1MB
2305 bs = (ssize_t)(RW_Block_Size);
2306 }
2307
Ethan Yonker472f5062016-02-25 13:47:30 -06002308 buffer = malloc((size_t)bs);
2309 if (!buffer) {
2310 LOGINFO("Raw_Read_Write failed to malloc\n");
2311 goto exit;
2312 }
bigbiffce8f83c2015-12-12 18:30:21 -05002313
2314 if (part_settings->progress)
2315 part_settings->progress->SetPartitionSize(part_settings->total_restore_size);
2316
Ethan Yonker472f5062016-02-25 13:47:30 -06002317 while (Remain > 0) {
2318 if (Remain < RW_Block_Size)
bigbiffce8f83c2015-12-12 18:30:21 -05002319 bs = (ssize_t)(Remain);
Matt Mowera8a89d12016-12-30 18:10:37 -06002320 if (read(src_fd, buffer, bs) != bs) {
Ethan Yonker472f5062016-02-25 13:47:30 -06002321 LOGINFO("Error reading source fd (%s)\n", strerror(errno));
2322 goto exit;
2323 }
2324 if (write(dest_fd, buffer, bs) != bs) {
2325 LOGINFO("Error writing destination fd (%s)\n", strerror(errno));
2326 goto exit;
2327 }
2328 backedup_size += (unsigned long long)(bs);
Matt Mower029a82d2017-01-08 13:12:38 -06002329 Remain -= (unsigned long long)(bs);
bigbiffce8f83c2015-12-12 18:30:21 -05002330 if (part_settings->progress)
2331 part_settings->progress->UpdateSize(backedup_size);
Ethan Yonker472f5062016-02-25 13:47:30 -06002332 if (PartitionManager.Check_Backup_Cancel() != 0)
2333 goto exit;
2334 }
bigbiffce8f83c2015-12-12 18:30:21 -05002335 if (part_settings->progress)
2336 part_settings->progress->UpdateDisplayDetails(true);
Ethan Yonker472f5062016-02-25 13:47:30 -06002337 fsync(dest_fd);
nailyka083dc62016-11-11 21:41:28 +01002338
2339 if (!part_settings->adbbackup && part_settings->PM_Method == PM_BACKUP) {
2340 tw_set_default_metadata(destfn.c_str());
2341 LOGINFO("Restored default metadata for %s\n", destfn.c_str());
2342 }
2343
Ethan Yonker472f5062016-02-25 13:47:30 -06002344 ret = true;
2345exit:
2346 if (src_fd >= 0)
2347 close(src_fd);
2348 if (dest_fd >= 0)
2349 close(dest_fd);
2350 if (buffer)
2351 free(buffer);
2352 return ret;
2353}
2354
bigbiffce8f83c2015-12-12 18:30:21 -05002355bool TWPartition::Backup_Dump_Image(PartitionSettings *part_settings) {
Vojtech Bocek05534202013-09-11 08:11:56 +02002356 string Full_FileName, Command;
Dees_Troy43d8b002012-09-17 16:00:01 -04002357
Ethan Yonker74db1572015-10-28 12:44:49 -05002358 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, gui_parse_text("{@backing}"));
2359 gui_msg(Msg("backing_up=Backing up {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002360
bigbiffce8f83c2015-12-12 18:30:21 -05002361 if (part_settings->progress)
2362 part_settings->progress->SetPartitionSize(Backup_Size);
Dees_Troy43d8b002012-09-17 16:00:01 -04002363
Ethan Yonker472f5062016-02-25 13:47:30 -06002364 Backup_FileName = Backup_Name + "." + Current_File_System + ".win";
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002365 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04002366
2367 Command = "dump_image " + MTD_Name + " '" + Full_FileName + "'";
bigbiffce8f83c2015-12-12 18:30:21 -05002368
Dees_Troy2673cec2013-04-02 20:22:16 +00002369 LOGINFO("Backup command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02002370 TWFunc::Exec_Cmd(Command);
Ethan Yonker4b94cfd2014-12-11 10:00:45 -06002371 tw_set_default_metadata(Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04002372 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
2373 // 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 -06002374 gui_msg(Msg(msg::kError, "backup_size=Backup file size for '{1}' is 0 bytes.")(Full_FileName));
Dees_Troy7c2dec82012-09-26 09:49:14 -04002375 return false;
2376 }
bigbiffce8f83c2015-12-12 18:30:21 -05002377 if (part_settings->progress)
2378 part_settings->progress->UpdateSize(Backup_Size);
2379
Dees_Troy43d8b002012-09-17 16:00:01 -04002380 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002381}
2382
bigbiffce8f83c2015-12-12 18:30:21 -05002383unsigned long long TWPartition::Get_Restore_Size(PartitionSettings *part_settings) {
2384 if (!part_settings->adbbackup) {
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002385 InfoManager restore_info(part_settings->Backup_Folder + "/" + Backup_Name + ".info");
bigbiffce8f83c2015-12-12 18:30:21 -05002386 if (restore_info.LoadValues() == 0) {
2387 if (restore_info.GetValue("backup_size", Restore_Size) == 0) {
2388 LOGINFO("Read info file, restore size is %llu\n", Restore_Size);
2389 return Restore_Size;
2390 }
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002391 }
2392 }
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002393
Matt Mower029a82d2017-01-08 13:12:38 -06002394 string Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
2395 string Restore_File_System = Get_Restore_File_System(part_settings);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002396
2397 if (Is_Image(Restore_File_System)) {
2398 Restore_Size = TWFunc::Get_File_Size(Full_FileName);
2399 return Restore_Size;
2400 }
2401
2402 twrpTar tar;
2403 tar.setdir(Backup_Path);
2404 tar.setfn(Full_FileName);
bigbiffce8f83c2015-12-12 18:30:21 -05002405 tar.backup_name = Full_FileName;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002406#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
2407 string Password;
2408 DataManager::GetValue("tw_restore_password", Password);
2409 if (!Password.empty())
2410 tar.setpassword(Password);
2411#endif
2412 tar.partition_name = Backup_Name;
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002413 tar.backup_folder = part_settings->Backup_Folder;
bigbiffce8f83c2015-12-12 18:30:21 -05002414 tar.part_settings = part_settings;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002415 Restore_Size = tar.get_size();
2416 return Restore_Size;
2417}
2418
bigbiffce8f83c2015-12-12 18:30:21 -05002419bool TWPartition::Restore_Tar(PartitionSettings *part_settings) {
Ethan Yonker472f5062016-02-25 13:47:30 -06002420 string Full_FileName;
Dees Troy4159aed2014-02-28 17:24:43 +00002421 bool ret = false;
bigbiffce8f83c2015-12-12 18:30:21 -05002422 string Restore_File_System = Get_Restore_File_System(part_settings);
Dees_Troy43d8b002012-09-17 16:00:01 -04002423
Dees_Troye58d5262012-09-21 12:27:57 -04002424 if (Has_Android_Secure) {
Dees_Troye58d5262012-09-21 12:27:57 -04002425 if (!Wipe_AndSec())
2426 return false;
Gary Peck43acadf2012-11-21 21:19:01 -08002427 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002428 gui_msg(Msg("wiping=Wiping {1}")(Backup_Display_Name));
Ethan Yonker5eac2222014-06-11 12:22:55 -05002429 if (Has_Data_Media && Mount_Point == "/data" && Restore_File_System != Current_File_System) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002430 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 -05002431 if (!Wipe_Data_Without_Wiping_Media())
2432 return false;
2433 } else {
2434 if (!Wipe(Restore_File_System))
2435 return false;
2436 }
Dees_Troye58d5262012-09-21 12:27:57 -04002437 }
Matt Mower3c366972015-12-25 19:28:31 -06002438 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Backup_Display_Name, gui_parse_text("{@restoring_hdr}"));
Ethan Yonker74db1572015-10-28 12:44:49 -05002439 gui_msg(Msg("restoring=Restoring {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002440
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05002441 // Remount as read/write as needed so we can restore the backup
2442 if (!ReMount_RW(true))
Dees_Troy43d8b002012-09-17 16:00:01 -04002443 return false;
2444
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002445 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
Ethan Yonker87af5632014-02-10 11:56:35 -06002446 twrpTar tar;
bigbiffce8f83c2015-12-12 18:30:21 -05002447 tar.part_settings = part_settings;
Ethan Yonker87af5632014-02-10 11:56:35 -06002448 tar.setdir(Backup_Path);
2449 tar.setfn(Full_FileName);
2450 tar.backup_name = Backup_Name;
2451#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
2452 string Password;
2453 DataManager::GetValue("tw_restore_password", Password);
2454 if (!Password.empty())
2455 tar.setpassword(Password);
2456#endif
bigbiffce8f83c2015-12-12 18:30:21 -05002457 part_settings->progress->SetPartitionSize(Get_Restore_Size(part_settings));
2458 if (tar.extractTarFork() != 0)
Dees Troy4159aed2014-02-28 17:24:43 +00002459 ret = false;
2460 else
2461 ret = true;
2462#ifdef HAVE_CAPABILITIES
2463 // Restore capabilities to the run-as binary
2464 if (Mount_Point == "/system" && Mount(true) && TWFunc::Path_Exists("/system/bin/run-as")) {
2465 struct vfs_cap_data cap_data;
2466 uint64_t capabilities = (1 << CAP_SETUID) | (1 << CAP_SETGID);
2467
2468 memset(&cap_data, 0, sizeof(cap_data));
2469 cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE;
2470 cap_data.data[0].permitted = (uint32_t) (capabilities & 0xffffffff);
2471 cap_data.data[0].inheritable = 0;
2472 cap_data.data[1].permitted = (uint32_t) (capabilities >> 32);
2473 cap_data.data[1].inheritable = 0;
2474 if (setxattr("/system/bin/run-as", XATTR_NAME_CAPS, &cap_data, sizeof(cap_data), 0) < 0) {
2475 LOGINFO("Failed to reset capabilities of /system/bin/run-as binary.\n");
2476 } else {
2477 LOGINFO("Reset capabilities of /system/bin/run-as binary successful.\n");
2478 }
2479 }
2480#endif
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05002481 if (Mount_Read_Only || Mount_Flags & MS_RDONLY)
2482 // Remount as read only when restoration is complete
2483 ReMount(true);
2484
Dees Troy4159aed2014-02-28 17:24:43 +00002485 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04002486}
2487
bigbiffce8f83c2015-12-12 18:30:21 -05002488bool TWPartition::Restore_Image(PartitionSettings *part_settings) {
Ethan Yonker96af84a2015-01-05 14:58:36 -06002489 string Full_FileName;
bigbiffce8f83c2015-12-12 18:30:21 -05002490 string Restore_File_System = Get_Restore_File_System(part_settings);
Dees_Troy43d8b002012-09-17 16:00:01 -04002491
Matt Mower3c366972015-12-25 19:28:31 -06002492 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Backup_Display_Name, gui_parse_text("{@restoring_hdr}"));
Ethan Yonker74db1572015-10-28 12:44:49 -05002493 gui_msg(Msg("restoring=Restoring {1}...")(Backup_Display_Name));
bigbiffce8f83c2015-12-12 18:30:21 -05002494
2495 if (part_settings->adbbackup)
2496 Full_FileName = TW_ADB_RESTORE;
2497 else
Ethan Yonkere080c1f2016-09-19 13:50:25 -05002498 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
Gary Peck15e623d2012-11-21 21:07:58 -08002499
Ethan Yonker96af84a2015-01-05 14:58:36 -06002500 if (Restore_File_System == "emmc") {
bigbiffce8f83c2015-12-12 18:30:21 -05002501 if (!part_settings->adbbackup)
2502 part_settings->total_restore_size = (uint64_t)(TWFunc::Get_File_Size(Full_FileName));
2503 if (!Raw_Read_Write(part_settings))
Ethan Yonker96af84a2015-01-05 14:58:36 -06002504 return false;
2505 } else if (Restore_File_System == "mtd" || Restore_File_System == "bml") {
bigbiffce8f83c2015-12-12 18:30:21 -05002506 if (!Flash_Image_FI(Full_FileName, part_settings->progress))
2507 return false;
2508 }
2509
2510 if (part_settings->adbbackup) {
2511 if (!twadbbu::Write_TWEOF())
Ethan Yonker96af84a2015-01-05 14:58:36 -06002512 return false;
Gary Peck15e623d2012-11-21 21:07:58 -08002513 }
Dees_Troy43d8b002012-09-17 16:00:01 -04002514 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002515}
Dees_Troy5bf43922012-09-07 16:07:55 -04002516
2517bool TWPartition::Update_Size(bool Display_Error) {
Dees_Troy0550cfb2012-10-13 11:56:13 -04002518 bool ret = false, Was_Already_Mounted = false;
Dees_Troy51127312012-09-08 13:08:49 -04002519
Ethan Yonker1b190162016-12-05 15:25:19 -06002520 Find_Actual_Block_Device();
2521
2522 if (!Can_Be_Mounted && !Is_Encrypted) {
2523 if (TWFunc::Path_Exists(Actual_Block_Device) && Find_Partition_Size()) {
2524 Used = Size;
2525 Backup_Size = Size;
2526 return true;
2527 }
Dees_Troy5bf43922012-09-07 16:07:55 -04002528 return false;
Ethan Yonker1b190162016-12-05 15:25:19 -06002529 }
Dees_Troy5bf43922012-09-07 16:07:55 -04002530
Dees_Troy0550cfb2012-10-13 11:56:13 -04002531 Was_Already_Mounted = Is_Mounted();
Dees_Troy38bd7602012-09-14 13:33:53 -04002532 if (Removable || Is_Encrypted) {
2533 if (!Mount(false))
2534 return true;
2535 } else if (!Mount(Display_Error))
Dees_Troy5bf43922012-09-07 16:07:55 -04002536 return false;
Dees_Troy51127312012-09-08 13:08:49 -04002537
2538 ret = Get_Size_Via_statfs(Display_Error);
Dees_Troy0550cfb2012-10-13 11:56:13 -04002539 if (!ret || Size == 0) {
2540 if (!Get_Size_Via_df(Display_Error)) {
2541 if (!Was_Already_Mounted)
2542 UnMount(false);
Dees_Troy51127312012-09-08 13:08:49 -04002543 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04002544 }
2545 }
Dees_Troy51127312012-09-08 13:08:49 -04002546
Dees_Troy5bf43922012-09-07 16:07:55 -04002547 if (Has_Data_Media) {
2548 if (Mount(Display_Error)) {
Ethan Yonker3fdcda42016-11-30 12:29:37 -06002549 Used = backup_exclusions.Get_Folder_Size(Mount_Point);
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002550 Backup_Size = Used;
2551 int bak = (int)(Used / 1048576LLU);
Dees_Troy51127312012-09-08 13:08:49 -04002552 int fre = (int)(Free / 1048576LLU);
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002553 LOGINFO("Data backup size is %iMB, free: %iMB.\n", bak, fre);
Dees_Troy0550cfb2012-10-13 11:56:13 -04002554 } else {
2555 if (!Was_Already_Mounted)
2556 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04002557 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04002558 }
Dees_Troye58d5262012-09-21 12:27:57 -04002559 } else if (Has_Android_Secure) {
2560 if (Mount(Display_Error))
Ethan Yonker3fdcda42016-11-30 12:29:37 -06002561 Backup_Size = backup_exclusions.Get_Folder_Size(Backup_Path);
Dees_Troy0550cfb2012-10-13 11:56:13 -04002562 else {
2563 if (!Was_Already_Mounted)
2564 UnMount(false);
Dees_Troye58d5262012-09-21 12:27:57 -04002565 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04002566 }
Dees_Troy5bf43922012-09-07 16:07:55 -04002567 }
Dees_Troy0550cfb2012-10-13 11:56:13 -04002568 if (!Was_Already_Mounted)
2569 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04002570 return true;
Dees_Troy51127312012-09-08 13:08:49 -04002571}
Dees_Troy38bd7602012-09-14 13:33:53 -04002572
2573void TWPartition::Find_Actual_Block_Device(void) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002574 if (Is_Decrypted && !Decrypted_Block_Device.empty()) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002575 Actual_Block_Device = Decrypted_Block_Device;
Ethan Yonker1b190162016-12-05 15:25:19 -06002576 if (TWFunc::Path_Exists(Decrypted_Block_Device)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002577 Is_Present = true;
Ethan Yonker1b190162016-12-05 15:25:19 -06002578 return;
2579 }
2580 } else if (SlotSelect && TWFunc::Path_Exists(Primary_Block_Device + PartitionManager.Get_Active_Slot_Suffix())) {
2581 Actual_Block_Device = Primary_Block_Device + PartitionManager.Get_Active_Slot_Suffix();
2582 unlink(Primary_Block_Device.c_str());
2583 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
2584 Is_Present = true;
2585 return;
Dees_Troy43d8b002012-09-17 16:00:01 -04002586 } else if (TWFunc::Path_Exists(Primary_Block_Device)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002587 Is_Present = true;
2588 Actual_Block_Device = Primary_Block_Device;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002589 return;
2590 }
Ethan Yonker1b190162016-12-05 15:25:19 -06002591 if (!Alternate_Block_Device.empty() && TWFunc::Path_Exists(Alternate_Block_Device)) {
Dees_Troy3f04d032012-10-07 18:20:09 -04002592 Actual_Block_Device = Alternate_Block_Device;
Dees_Troy38bd7602012-09-14 13:33:53 -04002593 Is_Present = true;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002594 } else {
Dees_Troy38bd7602012-09-14 13:33:53 -04002595 Is_Present = false;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002596 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002597}
2598
2599void TWPartition::Recreate_Media_Folder(void) {
2600 string Command;
Ethan Yonker66a19492015-12-10 10:19:45 -06002601 string Media_Path = Mount_Point + "/media";
Dees_Troy38bd7602012-09-14 13:33:53 -04002602
Ethan Yonkerbd7492d2016-12-07 13:55:01 -06002603 if (Is_FBE) {
2604 LOGINFO("Not recreating media folder on FBE\n");
2605 return;
2606 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002607 if (!Mount(true)) {
Ethan Yonker66a19492015-12-10 10:19:45 -06002608 gui_msg(Msg(msg::kError, "recreate_folder_err=Unable to recreate {1} folder.")(Media_Path));
2609 } else if (!TWFunc::Path_Exists(Media_Path)) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002610 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Ethan Yonker66a19492015-12-10 10:19:45 -06002611 LOGINFO("Recreating %s folder.\n", Media_Path.c_str());
2612 mkdir(Media_Path.c_str(), 0770);
xiaolu9416f4f2015-06-04 08:22:23 +08002613 string Internal_path = DataManager::GetStrValue("tw_internal_path");
2614 if (!Internal_path.empty()) {
2615 LOGINFO("Recreating %s folder.\n", Internal_path.c_str());
2616 mkdir(Internal_path.c_str(), 0770);
2617 }
2618#ifdef TW_INTERNAL_STORAGE_PATH
2619 mkdir(EXPAND(TW_INTERNAL_STORAGE_PATH), 0770);
2620#endif
Ethan Yonker5ef301e2014-10-14 10:04:44 -05002621#ifdef HAVE_SELINUX
thata3d31fb2014-12-21 22:27:40 +01002622 // Afterwards, we will try to set the
2623 // default metadata that we were hopefully able to get during
2624 // early boot.
Ethan Yonker66a19492015-12-10 10:19:45 -06002625 tw_set_default_metadata(Media_Path.c_str());
xiaolu9416f4f2015-06-04 08:22:23 +08002626 if (!Internal_path.empty())
2627 tw_set_default_metadata(Internal_path.c_str());
Ethan Yonker5ef301e2014-10-14 10:04:44 -05002628#endif
Ethan Yonker5eac2222014-06-11 12:22:55 -05002629 // Toggle mount to ensure that "internal sdcard" gets mounted
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002630 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Ethan Yonker5eac2222014-06-11 12:22:55 -05002631 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Dees_Troy38bd7602012-09-14 13:33:53 -04002632 }
Dees_Troy43d8b002012-09-17 16:00:01 -04002633}
Dees_Troye58d5262012-09-21 12:27:57 -04002634
2635void TWPartition::Recreate_AndSec_Folder(void) {
Dees_Troye58d5262012-09-21 12:27:57 -04002636 if (!Has_Android_Secure)
2637 return;
Dees_Troy2673cec2013-04-02 20:22:16 +00002638 LOGINFO("Creating %s: %s\n", Backup_Display_Name.c_str(), Symlink_Path.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -04002639 if (!Mount(true)) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002640 gui_msg(Msg(msg::kError, "recreate_folder_err=Unable to recreate {1} folder.")(Backup_Name));
Dees_Troye58d5262012-09-21 12:27:57 -04002641 } else if (!TWFunc::Path_Exists(Symlink_Path)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00002642 LOGINFO("Recreating %s folder.\n", Backup_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002643 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05002644 mkdir(Symlink_Path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002645 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04002646 }
2647}
bigbiff7cb4c332014-11-26 20:36:07 -05002648
2649uint64_t TWPartition::Get_Max_FileSize() {
2650 uint64_t maxFileSize = 0;
2651 const uint64_t constGB = (uint64_t) 1024 * 1024 * 1024;
2652 const uint64_t constTB = (uint64_t) constGB * 1024;
2653 const uint64_t constPB = (uint64_t) constTB * 1024;
bigbiff0c532032014-12-21 13:41:26 -05002654 if (Current_File_System == "ext4")
2655 maxFileSize = 16 * constTB; //16 TB
2656 else if (Current_File_System == "vfat")
2657 maxFileSize = 4 * constGB; //4 GB
2658 else if (Current_File_System == "ntfs")
2659 maxFileSize = 256 * constTB; //256 TB
2660 else if (Current_File_System == "exfat")
2661 maxFileSize = 16 * constPB; //16 PB
2662 else if (Current_File_System == "ext3")
2663 maxFileSize = 2 * constTB; //2 TB
2664 else if (Current_File_System == "f2fs")
2665 maxFileSize = 3.94 * constTB; //3.94 TB
bigbiff7cb4c332014-11-26 20:36:07 -05002666 else
2667 maxFileSize = 100000000L;
2668 return maxFileSize - 1;
2669}
2670
bigbiffce8f83c2015-12-12 18:30:21 -05002671bool TWPartition::Flash_Image(PartitionSettings *part_settings) {
2672 string Restore_File_System, full_filename;
Ethan Yonker96af84a2015-01-05 14:58:36 -06002673
Ethan Yonkere080c1f2016-09-19 13:50:25 -05002674 full_filename = part_settings->Backup_Folder + "/" + Backup_FileName;
Ethan Yonker96af84a2015-01-05 14:58:36 -06002675
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002676 LOGINFO("Image filename is: %s\n", Backup_FileName.c_str());
bigbiffce8f83c2015-12-12 18:30:21 -05002677
2678 if (Backup_Method == BM_FILES) {
Ethan Yonker96af84a2015-01-05 14:58:36 -06002679 LOGERR("Cannot flash images to file systems\n");
2680 return false;
2681 } else if (!Can_Flash_Img) {
2682 LOGERR("Cannot flash images to partitions %s\n", Display_Name.c_str());
2683 return false;
2684 } else {
2685 if (!Find_Partition_Size()) {
2686 LOGERR("Unable to find partition size for '%s'\n", Mount_Point.c_str());
2687 return false;
2688 }
bigbiffce8f83c2015-12-12 18:30:21 -05002689 unsigned long long image_size = TWFunc::Get_File_Size(full_filename);
Ethan Yonker96af84a2015-01-05 14:58:36 -06002690 if (image_size > Size) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002691 LOGINFO("Size (%llu bytes) of image '%s' is larger than target device '%s' (%llu bytes)\n",
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002692 image_size, Backup_FileName.c_str(), Actual_Block_Device.c_str(), Size);
Ethan Yonker74db1572015-10-28 12:44:49 -05002693 gui_err("img_size_err=Size of image is larger than target device");
Ethan Yonker96af84a2015-01-05 14:58:36 -06002694 return false;
2695 }
bigbiffce8f83c2015-12-12 18:30:21 -05002696 if (Backup_Method == BM_DD) {
2697 if (!part_settings->adbbackup) {
2698 if (Is_Sparse_Image(full_filename)) {
2699 return Flash_Sparse_Image(full_filename);
2700 }
Ethan Yonker472f5062016-02-25 13:47:30 -06002701 }
bigbiffce8f83c2015-12-12 18:30:21 -05002702 return Raw_Read_Write(part_settings);
2703 } else if (Backup_Method == BM_FLASH_UTILS) {
2704 return Flash_Image_FI(full_filename, NULL);
Ethan Yonker472f5062016-02-25 13:47:30 -06002705 }
Ethan Yonker96af84a2015-01-05 14:58:36 -06002706 }
2707
2708 LOGERR("Unknown flash method for '%s'\n", Mount_Point.c_str());
2709 return false;
2710}
2711
Ethan Yonker472f5062016-02-25 13:47:30 -06002712bool TWPartition::Is_Sparse_Image(const string& Filename) {
HashBanged974bb2016-01-30 14:20:09 -05002713 uint32_t magic = 0;
2714 int fd = open(Filename.c_str(), O_RDONLY);
2715 if (fd < 0) {
2716 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Filename)(strerror(errno)));
2717 return false;
2718 }
bigbiffce8f83c2015-12-12 18:30:21 -05002719
HashBanged974bb2016-01-30 14:20:09 -05002720 if (read(fd, &magic, sizeof(magic)) != sizeof(magic)) {
2721 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Filename)(strerror(errno)));
2722 close(fd);
2723 return false;
2724 }
2725 close(fd);
Ethan Yonker472f5062016-02-25 13:47:30 -06002726 if (magic == SPARSE_HEADER_MAGIC)
2727 return true;
2728 return false;
2729}
2730
2731bool TWPartition::Flash_Sparse_Image(const string& Filename) {
2732 string Command;
2733
2734 gui_msg(Msg("flashing=Flashing {1}...")(Display_Name));
2735
2736 Command = "simg2img '" + Filename + "' '" + Actual_Block_Device + "'";
Ethan Yonker96af84a2015-01-05 14:58:36 -06002737 LOGINFO("Flash command: '%s'\n", Command.c_str());
2738 TWFunc::Exec_Cmd(Command);
2739 return true;
2740}
2741
Ethan Yonker472f5062016-02-25 13:47:30 -06002742bool TWPartition::Flash_Image_FI(const string& Filename, ProgressTracking *progress) {
Ethan Yonker96af84a2015-01-05 14:58:36 -06002743 string Command;
Ethan Yonker472f5062016-02-25 13:47:30 -06002744 unsigned long long file_size;
Ethan Yonker96af84a2015-01-05 14:58:36 -06002745
Ethan Yonker74db1572015-10-28 12:44:49 -05002746 gui_msg(Msg("flashing=Flashing {1}...")(Display_Name));
Ethan Yonker472f5062016-02-25 13:47:30 -06002747 if (progress) {
2748 file_size = (unsigned long long)(TWFunc::Get_File_Size(Filename));
2749 progress->SetPartitionSize(file_size);
2750 }
Ethan Yonker96af84a2015-01-05 14:58:36 -06002751 // Sometimes flash image doesn't like to flash due to the first 2KB matching, so we erase first to ensure that it flashes
2752 Command = "erase_image " + MTD_Name;
2753 LOGINFO("Erase command: '%s'\n", Command.c_str());
2754 TWFunc::Exec_Cmd(Command);
2755 Command = "flash_image " + MTD_Name + " '" + Filename + "'";
2756 LOGINFO("Flash command: '%s'\n", Command.c_str());
2757 TWFunc::Exec_Cmd(Command);
Ethan Yonker472f5062016-02-25 13:47:30 -06002758 if (progress)
2759 progress->UpdateSize(file_size);
Ethan Yonker96af84a2015-01-05 14:58:36 -06002760 return true;
2761}
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05002762
2763void TWPartition::Change_Mount_Read_Only(bool new_value) {
2764 Mount_Read_Only = new_value;
2765}
2766
bigbiffce8f83c2015-12-12 18:30:21 -05002767bool TWPartition::Is_Read_Only() {
2768 return Mount_Read_Only;
2769}
2770
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05002771int TWPartition::Check_Lifetime_Writes() {
2772 bool original_read_only = Mount_Read_Only;
2773 int ret = 1;
2774
2775 Mount_Read_Only = true;
2776 if (Mount(false)) {
2777 Find_Actual_Block_Device();
2778 string block = basename(Actual_Block_Device.c_str());
2779 string file = "/sys/fs/" + Current_File_System + "/" + block + "/lifetime_write_kbytes";
2780 string result;
2781 if (TWFunc::Path_Exists(file)) {
2782 if (TWFunc::read_file(file, result) != 0) {
2783 LOGINFO("Check_Lifetime_Writes of '%s' failed to read_file\n", file.c_str());
2784 } else {
2785 LOGINFO("Check_Lifetime_Writes result: '%s'\n", result.c_str());
2786 if (result == "0") {
2787 ret = 0;
2788 }
2789 }
2790 } else {
2791 LOGINFO("Check_Lifetime_Writes file does not exist '%s'\n", file.c_str());
2792 }
2793 UnMount(true);
2794 } else {
2795 LOGINFO("Check_Lifetime_Writes failed to mount '%s'\n", Mount_Point.c_str());
2796 }
2797 Mount_Read_Only = original_read_only;
2798 return ret;
2799}
Ethan Yonker66a19492015-12-10 10:19:45 -06002800
2801int TWPartition::Decrypt_Adopted() {
2802#ifdef TW_INCLUDE_CRYPTO
2803 int ret = 1;
2804 Is_Adopted_Storage = false;
2805 string Adopted_Key_File = "";
2806
2807 if (!Removable)
2808 return ret;
2809
2810 int fd = open(Alternate_Block_Device.c_str(), O_RDONLY);
2811 if (fd < 0) {
2812 LOGINFO("failed to open '%s'\n", Alternate_Block_Device.c_str());
2813 return ret;
2814 }
2815 char type_guid[80];
2816 char part_guid[80];
2817
2818 if (gpt_disk_get_partition_info(fd, 2, type_guid, part_guid) == 0) {
2819 LOGINFO("type: '%s'\n", type_guid);
2820 LOGINFO("part: '%s'\n", part_guid);
2821 Adopted_GUID = part_guid;
2822 LOGINFO("Adopted_GUID '%s'\n", Adopted_GUID.c_str());
2823 if (strcmp(type_guid, TWGptAndroidExpand) == 0) {
2824 LOGINFO("android_expand found\n");
2825 Adopted_Key_File = "/data/misc/vold/expand_";
2826 Adopted_Key_File += part_guid;
2827 Adopted_Key_File += ".key";
2828 if (TWFunc::Path_Exists(Adopted_Key_File)) {
2829 Is_Adopted_Storage = true;
2830 /* Until we find a use case for this, I think it is safe
2831 * to disable USB Mass Storage whenever adopted storage
2832 * is present.
2833 */
2834 LOGINFO("Detected adopted storage, disabling USB mass storage mode\n");
2835 DataManager::SetValue("tw_has_usb_storage", 0);
2836 }
2837 }
2838 }
2839
2840 if (Is_Adopted_Storage) {
2841 string Adopted_Block_Device = Alternate_Block_Device + "p2";
2842 if (!TWFunc::Path_Exists(Adopted_Block_Device)) {
2843 Adopted_Block_Device = Alternate_Block_Device + "2";
2844 if (!TWFunc::Path_Exists(Adopted_Block_Device)) {
2845 LOGINFO("Adopted block device does not exist\n");
2846 goto exit;
2847 }
2848 }
2849 LOGINFO("key file is '%s', block device '%s'\n", Adopted_Key_File.c_str(), Adopted_Block_Device.c_str());
2850 char crypto_blkdev[MAXPATHLEN];
2851 std::string thekey;
2852 int fdkey = open(Adopted_Key_File.c_str(), O_RDONLY);
2853 if (fdkey < 0) {
2854 LOGINFO("failed to open key file\n");
2855 goto exit;
2856 }
2857 char buf[512];
2858 ssize_t n;
2859 while ((n = read(fdkey, &buf[0], sizeof(buf))) > 0) {
2860 thekey.append(buf, n);
2861 }
2862 close(fdkey);
2863 unsigned char* key = (unsigned char*) thekey.data();
2864 cryptfs_revert_ext_volume(part_guid);
2865
2866 ret = cryptfs_setup_ext_volume(part_guid, Adopted_Block_Device.c_str(), key, thekey.size(), crypto_blkdev);
2867 if (ret == 0) {
2868 LOGINFO("adopted storage new block device: '%s'\n", crypto_blkdev);
2869 Decrypted_Block_Device = crypto_blkdev;
2870 Is_Decrypted = true;
2871 Is_Encrypted = true;
2872 Find_Actual_Block_Device();
Kjell Braden3126a112016-06-19 16:58:15 +00002873 if (!Mount_Storage_Retry(false)) {
Ethan Yonker66a19492015-12-10 10:19:45 -06002874 LOGERR("Failed to mount decrypted adopted storage device\n");
2875 Is_Decrypted = false;
2876 Is_Encrypted = false;
2877 cryptfs_revert_ext_volume(part_guid);
2878 ret = 1;
2879 } else {
Ethan Yonkerfcf3f242016-02-16 12:30:26 -06002880 UnMount(false);
2881 Has_Android_Secure = false;
2882 Symlink_Path = "";
2883 Symlink_Mount_Point = "";
2884 Backup_Name = Mount_Point.substr(1);
2885 Backup_Path = Mount_Point;
2886 TWPartition* sdext = PartitionManager.Find_Partition_By_Path("/sd-ext");
2887 if (sdext && sdext->Actual_Block_Device == Adopted_Block_Device) {
2888 LOGINFO("Removing /sd-ext from partition list due to adopted storage\n");
2889 PartitionManager.Remove_Partition_By_Path("/sd-ext");
2890 }
Ethan Yonker66a19492015-12-10 10:19:45 -06002891 Setup_Data_Media();
2892 Recreate_Media_Folder();
2893 Wipe_Available_in_GUI = true;
2894 Wipe_During_Factory_Reset = true;
2895 Can_Be_Backed_Up = true;
2896 Can_Encrypt_Backup = true;
2897 Use_Userdata_Encryption = true;
2898 Is_Storage = true;
2899 Storage_Name = "Adopted Storage";
2900 Is_SubPartition = true;
2901 SubPartition_Of = "/data";
2902 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
2903 DataManager::SetValue("tw_has_adopted_storage", 1);
2904 }
2905 } else {
2906 LOGERR("Failed to setup adopted storage decryption\n");
2907 }
2908 }
2909exit:
2910 return ret;
2911#else
2912 LOGINFO("Decrypt_Adopted: no crypto support\n");
2913 return 1;
2914#endif
2915}
2916
2917void TWPartition::Revert_Adopted() {
2918#ifdef TW_INCLUDE_CRYPTO
2919 if (!Adopted_GUID.empty()) {
2920 PartitionManager.Remove_MTP_Storage(Mount_Point);
2921 UnMount(false);
2922 cryptfs_revert_ext_volume(Adopted_GUID.c_str());
2923 Is_Adopted_Storage = false;
2924 Is_Encrypted = false;
2925 Is_Decrypted = false;
2926 Decrypted_Block_Device = "";
2927 Find_Actual_Block_Device();
2928 Wipe_During_Factory_Reset = false;
2929 Can_Be_Backed_Up = false;
2930 Can_Encrypt_Backup = false;
2931 Use_Userdata_Encryption = false;
2932 Is_SubPartition = false;
2933 SubPartition_Of = "";
2934 Has_Data_Media = false;
2935 Storage_Path = Mount_Point;
2936 if (!Symlink_Mount_Point.empty()) {
2937 TWPartition* Dat = PartitionManager.Find_Partition_By_Path("/data");
2938 if (Dat) {
2939 Dat->UnMount(false);
2940 Dat->Symlink_Mount_Point = Symlink_Mount_Point;
2941 }
2942 Symlink_Mount_Point = "";
2943 }
2944 }
2945#else
2946 LOGINFO("Revert_Adopted: no crypto support\n");
2947#endif
2948}