blob: 5a53d619a923cdb99963f9077b9637b363c08763 [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) {
856 struct statfs st;
857
858 Can_Be_Mounted = true;
859 Can_Be_Wiped = true;
860
Dees_Troy5bf43922012-09-07 16:07:55 -0400861 // Make the mount point folder if it doesn't exist
Dees_Troy51127312012-09-08 13:08:49 -0400862 Make_Dir(Mount_Point, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400863 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
864 Backup_Name = Display_Name;
bigbiffce8f83c2015-12-12 18:30:21 -0500865 Backup_Method = BM_FILES;
Dees_Troy5bf43922012-09-07 16:07:55 -0400866}
867
Ethan Yonker1b190162016-12-05 15:25:19 -0600868void TWPartition::Setup_Image() {
Dees_Troy5bf43922012-09-07 16:07:55 -0400869 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
870 Backup_Name = Display_Name;
Gary Peck82599a82012-11-21 16:23:12 -0800871 if (Current_File_System == "emmc")
bigbiffce8f83c2015-12-12 18:30:21 -0500872 Backup_Method = BM_DD;
Gary Peck82599a82012-11-21 16:23:12 -0800873 else if (Current_File_System == "mtd" || Current_File_System == "bml")
bigbiffce8f83c2015-12-12 18:30:21 -0500874 Backup_Method = BM_FLASH_UTILS;
Dees_Troy5bf43922012-09-07 16:07:55 -0400875 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000876 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 -0400877}
878
Dees_Troye58d5262012-09-21 12:27:57 -0400879void TWPartition::Setup_AndSec(void) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500880 Backup_Display_Name = "Android Secure";
Dees_Troye58d5262012-09-21 12:27:57 -0400881 Backup_Name = "and-sec";
Dees_Troya13d74f2013-03-24 08:54:55 -0500882 Can_Be_Backed_Up = true;
Dees_Troye58d5262012-09-21 12:27:57 -0400883 Has_Android_Secure = true;
884 Symlink_Path = Mount_Point + "/.android_secure";
885 Symlink_Mount_Point = "/and-sec";
886 Backup_Path = Symlink_Mount_Point;
887 Make_Dir("/and-sec", true);
888 Recreate_AndSec_Folder();
Kjell Braden3126a112016-06-19 16:58:15 +0000889 Mount_Storage_Retry(true);
Dees_Troye58d5262012-09-21 12:27:57 -0400890}
891
that9e0593e2014-10-08 00:01:24 +0200892void TWPartition::Setup_Data_Media() {
Ethan Yonker6277c792014-09-15 14:54:30 -0500893 LOGINFO("Setting up '%s' as data/media emulated storage.\n", Mount_Point.c_str());
Ethan Yonker66a19492015-12-10 10:19:45 -0600894 if (Storage_Name.empty() || Storage_Name == "Data")
895 Storage_Name = "Internal Storage";
Ethan Yonker6277c792014-09-15 14:54:30 -0500896 Has_Data_Media = true;
897 Is_Storage = true;
Ethan Yonker66a19492015-12-10 10:19:45 -0600898 Storage_Path = Mount_Point + "/media";
Ethan Yonker6277c792014-09-15 14:54:30 -0500899 Symlink_Path = Storage_Path;
Ethan Yonker66a19492015-12-10 10:19:45 -0600900 if (Mount_Point == "/data") {
901 Is_Settings_Storage = true;
902 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
903 Make_Dir("/emmc", false);
904 Symlink_Mount_Point = "/emmc";
905 } else {
906 Make_Dir("/sdcard", false);
907 Symlink_Mount_Point = "/sdcard";
908 }
909 if (Mount(false) && TWFunc::Path_Exists(Mount_Point + "/media/0")) {
910 Storage_Path = Mount_Point + "/media/0";
911 Symlink_Path = Storage_Path;
912 DataManager::SetValue(TW_INTERNAL_PATH, Mount_Point + "/media/0");
913 UnMount(true);
914 }
915 DataManager::SetValue("tw_has_internal", 1);
916 DataManager::SetValue("tw_has_data_media", 1);
Ethan Yonker3fdcda42016-11-30 12:29:37 -0600917 backup_exclusions.add_absolute_dir("/data/data/com.google.android.music/files");
Ethan Yonker79f88bd2016-12-09 14:52:12 -0600918 ExcludeAll(Mount_Point + "/misc/vold");
919 ExcludeAll(Mount_Point + "/.layout_version");
920 ExcludeAll(Mount_Point + "/system/storage.xml");
Ethan Yonker6277c792014-09-15 14:54:30 -0500921 } else {
Ethan Yonker66a19492015-12-10 10:19:45 -0600922 if (Mount(true) && TWFunc::Path_Exists(Mount_Point + "/media/0")) {
923 Storage_Path = Mount_Point + "/media/0";
924 Symlink_Path = Storage_Path;
925 UnMount(true);
926 }
Ethan Yonker6277c792014-09-15 14:54:30 -0500927 }
Ethan Yonker79f88bd2016-12-09 14:52:12 -0600928 ExcludeAll(Mount_Point + "/media");
Ethan Yonker6277c792014-09-15 14:54:30 -0500929}
930
Dees_Troy5bf43922012-09-07 16:07:55 -0400931void TWPartition::Find_Real_Block_Device(string& Block, bool Display_Error) {
932 char device[512], realDevice[512];
933
934 strcpy(device, Block.c_str());
935 memset(realDevice, 0, sizeof(realDevice));
936 while (readlink(device, realDevice, sizeof(realDevice)) > 0)
937 {
938 strcpy(device, realDevice);
939 memset(realDevice, 0, sizeof(realDevice));
940 }
941
942 if (device[0] != '/') {
943 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000944 LOGERR("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400945 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000946 LOGINFO("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400947 return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400948 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400949 Block = device;
950 return;
951 }
952}
953
Kjell Braden3126a112016-06-19 16:58:15 +0000954bool TWPartition::Mount_Storage_Retry(bool Display_Error) {
Dees_Troy8e337f32012-10-13 22:07:49 -0400955 // On some devices, storage doesn't want to mount right away, retry and sleep
Kjell Braden3126a112016-06-19 16:58:15 +0000956 if (!Mount(Display_Error)) {
Dees_Troy8e337f32012-10-13 22:07:49 -0400957 int retry_count = 5;
958 while (retry_count > 0 && !Mount(false)) {
959 usleep(500000);
960 retry_count--;
961 }
Kjell Braden3126a112016-06-19 16:58:15 +0000962 return Mount(Display_Error);
Dees_Troy8e337f32012-10-13 22:07:49 -0400963 }
Kjell Braden3126a112016-06-19 16:58:15 +0000964 return true;
Dees_Troy8e337f32012-10-13 22:07:49 -0400965}
966
Dees_Troy38bd7602012-09-14 13:33:53 -0400967bool TWPartition::Find_MTD_Block_Device(string MTD_Name) {
968 FILE *fp = NULL;
969 char line[255];
970
971 fp = fopen("/proc/mtd", "rt");
972 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000973 LOGERR("Device does not support /proc/mtd\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400974 return false;
975 }
976
977 while (fgets(line, sizeof(line), fp) != NULL)
978 {
979 char device[32], label[32];
980 unsigned long size = 0;
981 char* fstype = NULL;
982 int deviceId;
983
984 sscanf(line, "%s %lx %*s %*c%s", device, &size, label);
985
986 // Skip header and blank lines
987 if ((strcmp(device, "dev:") == 0) || (strlen(line) < 8))
988 continue;
989
990 // Strip off the trailing " from the label
991 label[strlen(label)-1] = '\0';
992
993 if (strcmp(label, MTD_Name.c_str()) == 0) {
994 // We found our device
995 // Strip off the trailing : from the device
996 device[strlen(device)-1] = '\0';
997 if (sscanf(device,"mtd%d", &deviceId) == 1) {
998 sprintf(device, "/dev/block/mtdblock%d", deviceId);
999 Primary_Block_Device = device;
Dees_Troy76543db2013-06-19 16:24:30 +00001000 fclose(fp);
1001 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -04001002 }
1003 }
1004 }
1005 fclose(fp);
1006
1007 return false;
1008}
1009
Dees_Troy51127312012-09-08 13:08:49 -04001010bool TWPartition::Get_Size_Via_statfs(bool Display_Error) {
1011 struct statfs st;
1012 string Local_Path = Mount_Point + "/.";
1013
1014 if (!Mount(Display_Error))
1015 return false;
1016
1017 if (statfs(Local_Path.c_str(), &st) != 0) {
1018 if (!Removable) {
1019 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001020 LOGERR("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -04001021 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001022 LOGINFO("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -04001023 }
1024 return false;
1025 }
1026 Size = (st.f_blocks * st.f_bsize);
1027 Used = ((st.f_blocks - st.f_bfree) * st.f_bsize);
1028 Free = (st.f_bfree * st.f_bsize);
1029 Backup_Size = Used;
1030 return true;
1031}
1032
1033bool TWPartition::Get_Size_Via_df(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001034 FILE* fp;
1035 char command[255], line[512];
1036 int include_block = 1;
1037 unsigned int min_len;
1038
1039 if (!Mount(Display_Error))
1040 return false;
1041
Dees_Troy38bd7602012-09-14 13:33:53 -04001042 min_len = Actual_Block_Device.size() + 2;
Dees_Troy51127312012-09-08 13:08:49 -04001043 sprintf(command, "df %s > /tmp/dfoutput.txt", Mount_Point.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001044 TWFunc::Exec_Cmd(command);
Dees_Troy51127312012-09-08 13:08:49 -04001045 fp = fopen("/tmp/dfoutput.txt", "rt");
1046 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001047 LOGINFO("Unable to open /tmp/dfoutput.txt.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001048 return false;
Dees_Troy51127312012-09-08 13:08:49 -04001049 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001050
1051 while (fgets(line, sizeof(line), fp) != NULL)
1052 {
1053 unsigned long blocks, used, available;
1054 char device[64];
1055 char tmpString[64];
1056
1057 if (strncmp(line, "Filesystem", 10) == 0)
1058 continue;
1059 if (strlen(line) < min_len) {
1060 include_block = 0;
1061 continue;
1062 }
1063 if (include_block) {
1064 sscanf(line, "%s %lu %lu %lu", device, &blocks, &used, &available);
1065 } else {
1066 // The device block string is so long that the df information is on the next line
1067 int space_count = 0;
Dees_Troye58d5262012-09-21 12:27:57 -04001068 sprintf(tmpString, "/dev/block/%s", Actual_Block_Device.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001069 while (tmpString[space_count] == 32)
1070 space_count++;
1071 sscanf(line + space_count, "%lu %lu %lu", &blocks, &used, &available);
1072 }
1073
1074 // Adjust block size to byte size
1075 Size = blocks * 1024ULL;
1076 Used = used * 1024ULL;
1077 Free = available * 1024ULL;
1078 Backup_Size = Used;
1079 }
1080 fclose(fp);
1081 return true;
1082}
1083
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001084unsigned long long TWPartition::IOCTL_Get_Block_Size() {
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001085 Find_Actual_Block_Device();
Ethan Yonker483e9f42016-01-11 22:21:18 -06001086
1087 return TWFunc::IOCTL_Get_Block_Size(Actual_Block_Device.c_str());
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001088}
1089
Dees_Troy5bf43922012-09-07 16:07:55 -04001090bool TWPartition::Find_Partition_Size(void) {
1091 FILE* fp;
1092 char line[512];
1093 string tmpdevice;
1094
igoriok87e3d932013-01-31 21:03:53 +02001095 fp = fopen("/proc/dumchar_info", "rt");
1096 if (fp != NULL) {
1097 while (fgets(line, sizeof(line), fp) != NULL)
1098 {
1099 char label[32], device[32];
1100 unsigned long size = 0;
1101
thatd43bf2d2014-09-21 23:13:02 +02001102 sscanf(line, "%s %lx %*x %*u %s", label, &size, device);
igoriok87e3d932013-01-31 21:03:53 +02001103
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05001104 // Skip header, annotation and blank lines
igoriok87e3d932013-01-31 21:03:53 +02001105 if ((strncmp(device, "/dev/", 5) != 0) || (strlen(line) < 8))
1106 continue;
1107
1108 tmpdevice = "/dev/";
1109 tmpdevice += label;
1110 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
1111 Size = size;
1112 fclose(fp);
1113 return true;
1114 }
1115 }
1116 }
1117
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001118 unsigned long long ioctl_size = IOCTL_Get_Block_Size();
1119 if (ioctl_size) {
1120 Size = ioctl_size;
1121 return true;
1122 }
1123
Dees_Troy5bf43922012-09-07 16:07:55 -04001124 // In this case, we'll first get the partitions we care about (with labels)
1125 fp = fopen("/proc/partitions", "rt");
1126 if (fp == NULL)
1127 return false;
1128
1129 while (fgets(line, sizeof(line), fp) != NULL)
1130 {
1131 unsigned long major, minor, blocks;
1132 char device[512];
1133 char tmpString[64];
1134
Dees_Troy63c8df72012-09-10 14:02:05 -04001135 if (strlen(line) < 7 || line[0] == 'm') continue;
Dees_Troy5bf43922012-09-07 16:07:55 -04001136 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
1137
1138 tmpdevice = "/dev/block/";
1139 tmpdevice += device;
Dees_Troy38bd7602012-09-14 13:33:53 -04001140 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001141 // Adjust block size to byte size
1142 Size = blocks * 1024ULL;
1143 fclose(fp);
1144 return true;
1145 }
1146 }
1147 fclose(fp);
1148 return false;
1149}
1150
Dees_Troy5bf43922012-09-07 16:07:55 -04001151bool TWPartition::Is_Mounted(void) {
1152 if (!Can_Be_Mounted)
1153 return false;
1154
1155 struct stat st1, st2;
1156 string test_path;
1157
1158 // Check to see if the mount point directory exists
1159 test_path = Mount_Point + "/.";
1160 if (stat(test_path.c_str(), &st1) != 0) return false;
1161
1162 // Check to see if the directory above the mount point exists
1163 test_path = Mount_Point + "/../.";
1164 if (stat(test_path.c_str(), &st2) != 0) return false;
1165
1166 // Compare the device IDs -- if they match then we're (probably) using tmpfs instead of an actual device
1167 int ret = (st1.st_dev != st2.st_dev) ? true : false;
1168
1169 return ret;
1170}
1171
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05001172bool TWPartition::Is_File_System_Writable(void) {
1173 if (!Is_File_System(Current_File_System) || !Is_Mounted())
1174 return false;
1175
1176 string test_path = Mount_Point + "/.";
1177 return (access(test_path.c_str(), W_OK) == 0);
1178}
1179
Dees_Troy5bf43922012-09-07 16:07:55 -04001180bool TWPartition::Mount(bool Display_Error) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001181 int exfat_mounted = 0;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001182 unsigned long flags = Mount_Flags;
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001183
Dees_Troy5bf43922012-09-07 16:07:55 -04001184 if (Is_Mounted()) {
1185 return true;
1186 } else if (!Can_Be_Mounted) {
1187 return false;
1188 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001189
1190 Find_Actual_Block_Device();
1191
1192 // Check the current file system before mounting
1193 Check_FS_Type();
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001194 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/exfat-fuse")) {
Dees_Troye34c1332013-02-06 19:13:00 +00001195 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 +00001196 LOGINFO("cmd: %s\n", cmd.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001197 string result;
1198 if (TWFunc::Exec_Cmd(cmd, result) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001199 LOGINFO("exfat-fuse failed to mount with result '%s', trying vfat\n", result.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001200 Current_File_System = "vfat";
1201 } else {
1202#ifdef TW_NO_EXFAT_FUSE
1203 UnMount(false);
1204 // We'll let the kernel handle it but using exfat-fuse to detect if the file system is actually exfat
1205 // Some kernels let us mount vfat as exfat which doesn't work out too well
1206#else
1207 exfat_mounted = 1;
1208#endif
1209 }
1210 }
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001211
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001212 if (Current_File_System == "ntfs" && (TWFunc::Path_Exists("/sbin/ntfs-3g") || TWFunc::Path_Exists("/sbin/mount.ntfs"))) {
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001213 string cmd;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001214 string Ntfsmount_Binary = "";
1215
1216 if (TWFunc::Path_Exists("/sbin/ntfs-3g"))
1217 Ntfsmount_Binary = "ntfs-3g";
1218 else if (TWFunc::Path_Exists("/sbin/mount.ntfs"))
1219 Ntfsmount_Binary = "mount.ntfs";
1220
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001221 if (Mount_Read_Only)
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001222 cmd = "/sbin/" + Ntfsmount_Binary + " -o ro " + Actual_Block_Device + " " + Mount_Point;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001223 else
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001224 cmd = "/sbin/" + Ntfsmount_Binary + " " + Actual_Block_Device + " " + Mount_Point;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001225 LOGINFO("cmd: '%s'\n", cmd.c_str());
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001226
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001227 if (TWFunc::Exec_Cmd(cmd) == 0) {
1228 return true;
1229 } else {
1230 LOGINFO("ntfs-3g failed to mount, trying regular mount method.\n");
1231 }
1232 }
1233
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001234 if (Mount_Read_Only)
1235 flags |= MS_RDONLY;
1236
Dees_Troy22042032012-12-18 21:23:08 +00001237 if (Fstab_File_System == "yaffs2") {
1238 // mount an MTD partition as a YAFFS2 filesystem.
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001239 flags = MS_NOATIME | MS_NODEV | MS_NODIRATIME;
1240 if (Mount_Read_Only)
1241 flags |= MS_RDONLY;
Dees_Troy76543db2013-06-19 16:24:30 +00001242 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags, NULL) < 0) {
1243 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags | MS_RDONLY, NULL) < 0) {
1244 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001245 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy76543db2013-06-19 16:24:30 +00001246 else
1247 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
1248 return false;
1249 } else {
1250 LOGINFO("Mounted '%s' (MTD) as RO\n", Mount_Point.c_str());
1251 return true;
1252 }
1253 } else {
1254 struct stat st;
1255 string test_path = Mount_Point;
1256 if (stat(test_path.c_str(), &st) < 0) {
1257 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001258 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy76543db2013-06-19 16:24:30 +00001259 else
1260 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
1261 return false;
1262 }
1263 mode_t new_mode = st.st_mode | S_IXUSR | S_IXGRP | S_IXOTH;
1264 if (new_mode != st.st_mode) {
1265 LOGINFO("Fixing execute permissions for %s\n", Mount_Point.c_str());
1266 if (chmod(Mount_Point.c_str(), new_mode) < 0) {
1267 if (Display_Error)
1268 LOGERR("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
1269 else
1270 LOGINFO("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
1271 return false;
1272 }
1273 }
Dees_Troy22042032012-12-18 21:23:08 +00001274 return true;
Dees_Troy76543db2013-06-19 16:24:30 +00001275 }
thatc7572eb2015-03-31 18:55:30 +02001276 }
1277
1278 string mount_fs = Current_File_System;
1279 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sys/module/texfat"))
1280 mount_fs = "texfat";
1281
1282 if (!exfat_mounted &&
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001283 mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), mount_fs.c_str(), flags, Mount_Options.c_str()) != 0 &&
1284 mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), mount_fs.c_str(), flags, NULL) != 0) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001285#ifdef TW_NO_EXFAT_FUSE
1286 if (Current_File_System == "exfat") {
Dees_Troy2673cec2013-04-02 20:22:16 +00001287 LOGINFO("Mounting exfat failed, trying vfat...\n");
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001288 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), "vfat", 0, NULL) != 0) {
Dees_Troy85f44ed2013-01-09 18:42:36 +00001289 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001290 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy85f44ed2013-01-09 18:42:36 +00001291 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001292 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001293 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 +00001294 return false;
Dees_Troy85f44ed2013-01-09 18:42:36 +00001295 }
Dees_Troyb05ddee2013-01-28 20:24:50 +00001296 } else {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001297#endif
bigbiff bigbiff26774a02014-03-29 18:22:00 -04001298 if (!Removable && Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001299 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001300 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001301 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
1302 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 +00001303 return false;
1304#ifdef TW_NO_EXFAT_FUSE
Dees_Troy85f44ed2013-01-09 18:42:36 +00001305 }
1306#endif
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001307 }
Ethan Yonker253368a2014-11-25 15:00:52 -06001308
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001309 if (Removable)
1310 Update_Size(Display_Error);
1311
xiaolu9416f4f2015-06-04 08:22:23 +08001312 if (!Symlink_Mount_Point.empty() && TWFunc::Path_Exists(Symlink_Path)) {
codelover2a3d4ce2015-03-14 20:26:49 +08001313 string Command = "mount -o bind '" + Symlink_Path + "' '" + Symlink_Mount_Point + "'";
Vojtech Bocek05534202013-09-11 08:11:56 +02001314 TWFunc::Exec_Cmd(Command);
Dees_Troy5bf43922012-09-07 16:07:55 -04001315 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001316 return true;
1317}
1318
1319bool TWPartition::UnMount(bool Display_Error) {
1320 if (Is_Mounted()) {
1321 int never_unmount_system;
1322
1323 DataManager::GetValue(TW_DONT_UNMOUNT_SYSTEM, never_unmount_system);
1324 if (never_unmount_system == 1 && Mount_Point == "/system")
1325 return true; // Never unmount system if you're not supposed to unmount it
1326
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001327 if (Is_Storage)
Ethan Yonker726a0202014-12-16 20:01:38 -06001328 PartitionManager.Remove_MTP_Storage(MTP_Storage_ID);
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001329
Dees_Troy38bd7602012-09-14 13:33:53 -04001330 if (!Symlink_Mount_Point.empty())
1331 umount(Symlink_Mount_Point.c_str());
1332
Dees_Troyb05ddee2013-01-28 20:24:50 +00001333 umount(Mount_Point.c_str());
1334 if (Is_Mounted()) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001335 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001336 gui_msg(Msg(msg::kError, "fail_unmount=Failed to unmount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy5bf43922012-09-07 16:07:55 -04001337 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001338 LOGINFO("Unable to unmount '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001339 return false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001340 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -04001341 return true;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001342 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001343 } else {
1344 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001345 }
1346}
1347
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05001348bool TWPartition::ReMount(bool Display_Error) {
1349 if (UnMount(Display_Error))
1350 return Mount(Display_Error);
1351 return false;
1352}
1353
1354bool TWPartition::ReMount_RW(bool Display_Error) {
1355 // No need to remount if already mounted rw
1356 if (Is_File_System_Writable())
1357 return true;
1358
1359 bool ro = Mount_Read_Only;
1360 int flags = Mount_Flags;
1361
1362 Mount_Read_Only = false;
1363 Mount_Flags &= ~MS_RDONLY;
1364
1365 bool ret = ReMount(Display_Error);
1366
1367 Mount_Read_Only = ro;
1368 Mount_Flags = flags;
1369
1370 return ret;
1371}
1372
Gary Peck43acadf2012-11-21 21:19:01 -08001373bool TWPartition::Wipe(string New_File_System) {
Ethan Yonker726a0202014-12-16 20:01:38 -06001374 bool wiped = false, update_crypt = false, recreate_media = true;
Dees_Troy16c2b312013-01-15 16:51:18 +00001375 int check;
1376 string Layout_Filename = Mount_Point + "/.layout_version";
1377
Dees_Troy38bd7602012-09-14 13:33:53 -04001378 if (!Can_Be_Wiped) {
Matt Mower3c366972015-12-25 19:28:31 -06001379 gui_msg(Msg(msg::kError, "cannot_wipe=Partition {1} cannot be wiped.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04001380 return false;
1381 }
1382
Dees_Troyc51f1f92012-09-20 15:32:13 -04001383 if (Mount_Point == "/cache")
Dees_Troy2673cec2013-04-02 20:22:16 +00001384 Log_Offset = 0;
Dees_Troyc51f1f92012-09-20 15:32:13 -04001385
Dees_Troy16c2b312013-01-15 16:51:18 +00001386 if (Retain_Layout_Version && Mount(false) && TWFunc::Path_Exists(Layout_Filename))
1387 TWFunc::copy_file(Layout_Filename, "/.layout_version", 0600);
1388 else
1389 unlink("/.layout_version");
Dees_Troy38bd7602012-09-14 13:33:53 -04001390
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001391 if (Has_Data_Media && Current_File_System == New_File_System) {
Dees_Troy16c2b312013-01-15 16:51:18 +00001392 wiped = Wipe_Data_Without_Wiping_Media();
Ethan Yonker5eac2222014-06-11 12:22:55 -05001393 recreate_media = false;
Dees_Troy16c2b312013-01-15 16:51:18 +00001394 } else {
Dees_Troy16c2b312013-01-15 16:51:18 +00001395 DataManager::GetValue(TW_RM_RF_VAR, check);
1396
Hashcodedabfd492013-08-29 22:45:30 -07001397 if (check || Use_Rm_Rf)
Dees_Troy16c2b312013-01-15 16:51:18 +00001398 wiped = Wipe_RMRF();
1399 else if (New_File_System == "ext4")
1400 wiped = Wipe_EXT4();
1401 else if (New_File_System == "ext2" || New_File_System == "ext3")
1402 wiped = Wipe_EXT23(New_File_System);
1403 else if (New_File_System == "vfat")
1404 wiped = Wipe_FAT();
1405 else if (New_File_System == "exfat")
1406 wiped = Wipe_EXFAT();
1407 else if (New_File_System == "yaffs2")
1408 wiped = Wipe_MTD();
Dees_Troye5017042013-08-29 16:38:55 +00001409 else if (New_File_System == "f2fs")
1410 wiped = Wipe_F2FS();
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001411 else if (New_File_System == "ntfs")
1412 wiped = Wipe_NTFS();
Dees_Troy16c2b312013-01-15 16:51:18 +00001413 else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001414 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 +00001415 unlink("/.layout_version");
1416 return false;
1417 }
1418 update_crypt = wiped;
Gary Pecke8bc5d72012-12-21 06:45:25 -08001419 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001420
Gary Pecke8bc5d72012-12-21 06:45:25 -08001421 if (wiped) {
Dees_Troy1c1ac442013-01-17 21:42:14 +00001422 if (Mount_Point == "/cache")
1423 DataManager::Output_Version();
1424
Dees_Troy16c2b312013-01-15 16:51:18 +00001425 if (TWFunc::Path_Exists("/.layout_version") && Mount(false))
1426 TWFunc::copy_file("/.layout_version", Layout_Filename, 0600);
1427
1428 if (update_crypt) {
1429 Setup_File_System(false);
1430 if (Is_Encrypted && !Is_Decrypted) {
1431 // just wiped an encrypted partition back to its unencrypted state
1432 Is_Encrypted = false;
1433 Is_Decrypted = false;
1434 Decrypted_Block_Device = "";
1435 if (Mount_Point == "/data") {
1436 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1437 DataManager::SetValue(TW_IS_DECRYPTED, 0);
1438 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001439 }
1440 }
Ethan Yonker5eac2222014-06-11 12:22:55 -05001441
Ethan Yonker66a19492015-12-10 10:19:45 -06001442 if (Has_Data_Media && recreate_media) {
Ethan Yonker5eac2222014-06-11 12:22:55 -05001443 Recreate_Media_Folder();
1444 }
Matt Mower209c9632016-01-20 12:08:35 -06001445 if (Is_Storage && Mount(false))
1446 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Gary Pecke8bc5d72012-12-21 06:45:25 -08001447 }
Matt Mower209c9632016-01-20 12:08:35 -06001448
Gary Pecke8bc5d72012-12-21 06:45:25 -08001449 return wiped;
Dees_Troy51a0e822012-09-05 15:24:24 -04001450}
1451
Gary Peck43acadf2012-11-21 21:19:01 -08001452bool TWPartition::Wipe() {
Gary Peck82599a82012-11-21 16:23:12 -08001453 if (Is_File_System(Current_File_System))
1454 return Wipe(Current_File_System);
1455 else
1456 return Wipe(Fstab_File_System);
Gary Peck43acadf2012-11-21 21:19:01 -08001457}
1458
Dees_Troye58d5262012-09-21 12:27:57 -04001459bool TWPartition::Wipe_AndSec(void) {
1460 if (!Has_Android_Secure)
1461 return false;
1462
Dees_Troye58d5262012-09-21 12:27:57 -04001463 if (!Mount(true))
1464 return false;
1465
Ethan Yonker74db1572015-10-28 12:44:49 -05001466 gui_msg(Msg("wiping=Wiping {1}")(Backup_Display_Name));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001467 TWFunc::removeDir(Mount_Point + "/.android_secure/", true);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001468 return true;
Dees_Troye58d5262012-09-21 12:27:57 -04001469}
1470
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001471bool TWPartition::Can_Repair() {
Ethan Yonkera2719152015-05-28 09:44:41 -05001472 if (Mount_Read_Only)
1473 return false;
Matt Mower18794c82015-11-11 16:22:45 -06001474 if (Current_File_System == "vfat" && TWFunc::Path_Exists("/sbin/fsck.fat"))
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001475 return true;
1476 else if ((Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") && TWFunc::Path_Exists("/sbin/e2fsck"))
1477 return true;
1478 else if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/fsck.exfat"))
1479 return true;
1480 else if (Current_File_System == "f2fs" && TWFunc::Path_Exists("/sbin/fsck.f2fs"))
1481 return true;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001482 else if (Current_File_System == "ntfs" && (TWFunc::Path_Exists("/sbin/ntfsfix") || TWFunc::Path_Exists("/sbin/fsck.ntfs")))
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001483 return true;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001484 return false;
1485}
1486
1487bool TWPartition::Repair() {
1488 string command;
1489
1490 if (Current_File_System == "vfat") {
Matt Mower18794c82015-11-11 16:22:45 -06001491 if (!TWFunc::Path_Exists("/sbin/fsck.fat")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001492 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.fat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001493 return false;
1494 }
1495 if (!UnMount(true))
1496 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001497 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.fat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001498 Find_Actual_Block_Device();
Matt Mower18794c82015-11-11 16:22:45 -06001499 command = "/sbin/fsck.fat -y " + Actual_Block_Device;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001500 LOGINFO("Repair command: %s\n", command.c_str());
1501 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001502 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001503 return true;
1504 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001505 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001506 return false;
1507 }
1508 }
1509 if (Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") {
1510 if (!TWFunc::Path_Exists("/sbin/e2fsck")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001511 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("e2fsck"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001512 return false;
1513 }
1514 if (!UnMount(true))
1515 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001516 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("e2fsck"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001517 Find_Actual_Block_Device();
Ethan Yonkera2719152015-05-28 09:44:41 -05001518 command = "/sbin/e2fsck -fp " + Actual_Block_Device;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001519 LOGINFO("Repair command: %s\n", command.c_str());
1520 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001521 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001522 return true;
1523 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001524 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001525 return false;
1526 }
1527 }
1528 if (Current_File_System == "exfat") {
1529 if (!TWFunc::Path_Exists("/sbin/fsck.exfat")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001530 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.exfat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001531 return false;
1532 }
1533 if (!UnMount(true))
1534 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001535 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.exfat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001536 Find_Actual_Block_Device();
1537 command = "/sbin/fsck.exfat " + Actual_Block_Device;
1538 LOGINFO("Repair command: %s\n", command.c_str());
1539 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001540 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001541 return true;
1542 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001543 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001544 return false;
1545 }
1546 }
1547 if (Current_File_System == "f2fs") {
1548 if (!TWFunc::Path_Exists("/sbin/fsck.f2fs")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001549 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.f2fs"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001550 return false;
1551 }
1552 if (!UnMount(true))
1553 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001554 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.f2fs"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001555 Find_Actual_Block_Device();
1556 command = "/sbin/fsck.f2fs " + Actual_Block_Device;
1557 LOGINFO("Repair command: %s\n", command.c_str());
1558 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001559 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001560 return true;
1561 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001562 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001563 return false;
1564 }
1565 }
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001566 if (Current_File_System == "ntfs") {
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001567 string Ntfsfix_Binary;
1568 if (TWFunc::Path_Exists("/sbin/ntfsfix"))
1569 Ntfsfix_Binary = "ntfsfix";
1570 else if (TWFunc::Path_Exists("/sbin/fsck.ntfs"))
1571 Ntfsfix_Binary = "fsck.ntfs";
1572 else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001573 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("ntfsfix"));
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001574 return false;
1575 }
1576 if (!UnMount(true))
1577 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001578 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)(Ntfsfix_Binary));
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001579 Find_Actual_Block_Device();
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001580 command = "/sbin/" + Ntfsfix_Binary + " " + Actual_Block_Device;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001581 LOGINFO("Repair command: %s\n", command.c_str());
1582 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001583 gui_msg("done=Done.");
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001584 return true;
1585 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001586 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001587 return false;
1588 }
1589 }
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001590 return false;
1591}
1592
Ethan Yonkera2719152015-05-28 09:44:41 -05001593bool TWPartition::Can_Resize() {
1594 if (Mount_Read_Only)
1595 return false;
1596 if ((Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") && TWFunc::Path_Exists("/sbin/resize2fs"))
1597 return true;
1598 return false;
1599}
1600
1601bool TWPartition::Resize() {
1602 string command;
1603
1604 if (Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") {
1605 if (!Can_Repair()) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001606 LOGINFO("Cannot resize %s because %s cannot be repaired before resizing.\n", Display_Name.c_str(), Display_Name.c_str());
1607 gui_msg(Msg(msg::kError, "cannot_resize=Cannot resize {1}.")(Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001608 return false;
1609 }
1610 if (!TWFunc::Path_Exists("/sbin/resize2fs")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001611 LOGINFO("resize2fs does not exist! Cannot resize!\n");
1612 gui_msg(Msg(msg::kError, "cannot_resize=Cannot resize {1}.")(Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001613 return false;
1614 }
1615 // Repair will unmount so no need to do it twice
Ethan Yonker74db1572015-10-28 12:44:49 -05001616 gui_msg(Msg("repair_resize=Repairing {1} before resizing.")( Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001617 if (!Repair())
1618 return false;
Ethan Yonker74db1572015-10-28 12:44:49 -05001619 gui_msg(Msg("resizing=Resizing {1} using {2}...")(Display_Name)("resize2fs"));
Ethan Yonkera2719152015-05-28 09:44:41 -05001620 Find_Actual_Block_Device();
1621 command = "/sbin/resize2fs " + Actual_Block_Device;
1622 if (Length != 0) {
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001623 unsigned long long Actual_Size = IOCTL_Get_Block_Size();
1624 if (Actual_Size == 0)
1625 return false;
Ethan Yonkera2719152015-05-28 09:44:41 -05001626
Ethan Yonkera2719152015-05-28 09:44:41 -05001627 unsigned long long Block_Count;
1628 if (Length < 0) {
1629 // Reduce overall size by this length
1630 Block_Count = (Actual_Size / 1024LLU) - ((unsigned long long)(Length * -1) / 1024LLU);
1631 } else {
1632 // This is the size, not a size reduction
1633 Block_Count = ((unsigned long long)(Length) / 1024LLU);
1634 }
1635 char temp[256];
1636 sprintf(temp, "%llu", Block_Count);
1637 command += " ";
1638 command += temp;
1639 command += "K";
1640 }
1641 LOGINFO("Resize command: %s\n", command.c_str());
1642 if (TWFunc::Exec_Cmd(command) == 0) {
1643 Update_Size(true);
Ethan Yonker74db1572015-10-28 12:44:49 -05001644 gui_msg("done=Done.");
Ethan Yonkera2719152015-05-28 09:44:41 -05001645 return true;
1646 } else {
1647 Update_Size(true);
Ethan Yonker74db1572015-10-28 12:44:49 -05001648 gui_msg(Msg(msg::kError, "unable_resize=Unable to resize {1}.")(Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001649 return false;
1650 }
1651 }
1652 return false;
1653}
1654
bigbiffce8f83c2015-12-12 18:30:21 -05001655bool TWPartition::Backup(PartitionSettings *part_settings, pid_t *tar_fork_pid) {
1656 if (Backup_Method == BM_FILES)
1657 return Backup_Tar(part_settings, tar_fork_pid);
1658 else if (Backup_Method == BM_DD)
1659 return Backup_Image(part_settings);
1660 else if (Backup_Method == BM_FLASH_UTILS)
1661 return Backup_Dump_Image(part_settings);
Dees_Troy2673cec2013-04-02 20:22:16 +00001662 LOGERR("Unknown backup method for '%s'\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001663 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001664}
1665
James Christopher Adduono79ae0932016-10-25 02:18:32 -04001666bool TWPartition::Check_Restore_File_MD5(const string& Filename) {
1667 twrpDigest md5sum;
1668
1669 md5sum.setfn(Filename);
1670 switch (md5sum.verify_md5digest()) {
1671 case MD5_OK:
1672 gui_msg(Msg("md5_matched=MD5 matched for '{1}'.")(Filename));
1673 return true;
1674 case MD5_FILE_UNREADABLE:
1675 case MD5_NOT_FOUND:
1676 gui_msg(Msg(msg::kError, "no_md5_found=No md5 file found for '{1}'. Please unselect Enable MD5 verification to restore.")(Filename));
1677 break;
1678 case MD5_MATCH_FAIL:
1679 gui_msg(Msg(msg::kError, "md5_fail_match=MD5 failed to match on '{1}'.")(Filename));
1680 break;
1681 }
1682 return false;
1683}
1684
bigbiff bigbiff8fd4b092016-08-30 20:48:53 -04001685bool TWPartition::Check_MD5(PartitionSettings *part_settings) {
James Christopher Adduono79ae0932016-10-25 02:18:32 -04001686 string Full_Filename;
Dees_Troy43d8b002012-09-17 16:00:01 -04001687 char split_filename[512];
1688 int index = 0;
1689
Captain Throwbackff5935f2014-09-23 16:08:34 -04001690 sync();
1691
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05001692 Full_Filename = part_settings->Backup_Folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04001693 if (!TWFunc::Path_Exists(Full_Filename)) {
1694 // This is a split archive, we presume
James Christopher Adduono79ae0932016-10-25 02:18:32 -04001695 memset(split_filename, 0, sizeof(split_filename));
Dees_Troy83bd4832013-05-04 12:39:56 +00001696 while (index < 1000) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001697 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
James Christopher Adduono79ae0932016-10-25 02:18:32 -04001698 if (!TWFunc::Path_Exists(split_filename))
1699 break;
1700 LOGINFO("split_filename: %s\n", split_filename);
1701 if (!Check_Restore_File_MD5(split_filename))
1702 return false;
1703 index++;
Dees_Troy43d8b002012-09-17 16:00:01 -04001704 }
Dees_Troy4a2a1262012-09-18 09:33:47 -04001705 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001706 }
James Christopher Adduono79ae0932016-10-25 02:18:32 -04001707 return Check_Restore_File_MD5(Full_Filename); // Single file archive
Dees_Troy43d8b002012-09-17 16:00:01 -04001708}
1709
bigbiffce8f83c2015-12-12 18:30:21 -05001710bool TWPartition::Restore(PartitionSettings *part_settings) {
Matt Mower3c366972015-12-25 19:28:31 -06001711 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, gui_parse_text("{@restoring_hdr}"));
Ethan Yonkere080c1f2016-09-19 13:50:25 -05001712 LOGINFO("Restore filename is: %s/%s\n", part_settings->Backup_Folder.c_str(), Backup_FileName.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001713
bigbiffce8f83c2015-12-12 18:30:21 -05001714 string Restore_File_System = Get_Restore_File_System(part_settings);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001715
1716 if (Is_File_System(Restore_File_System))
bigbiffce8f83c2015-12-12 18:30:21 -05001717 return Restore_Tar(part_settings);
1718 else if (Is_Image(Restore_File_System))
1719 return Restore_Image(part_settings);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001720
1721 LOGERR("Unknown restore method for '%s'\n", Mount_Point.c_str());
1722 return false;
1723}
1724
bigbiffce8f83c2015-12-12 18:30:21 -05001725string TWPartition::Get_Restore_File_System(PartitionSettings *part_settings) {
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001726 size_t first_period, second_period;
1727 string Restore_File_System;
1728
Gary Peck43acadf2012-11-21 21:19:01 -08001729 // Parse backup filename to extract the file system before wiping
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05001730 first_period = Backup_FileName.find(".");
Gary Peck43acadf2012-11-21 21:19:01 -08001731 if (first_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001732 LOGERR("Unable to find file system (first period).\n");
thatd43bf2d2014-09-21 23:13:02 +02001733 return string();
Gary Peck43acadf2012-11-21 21:19:01 -08001734 }
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05001735 Restore_File_System = Backup_FileName.substr(first_period + 1, Backup_FileName.size() - first_period - 1);
Gary Peck43acadf2012-11-21 21:19:01 -08001736 second_period = Restore_File_System.find(".");
1737 if (second_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001738 LOGERR("Unable to find file system (second period).\n");
thatd43bf2d2014-09-21 23:13:02 +02001739 return string();
Gary Peck43acadf2012-11-21 21:19:01 -08001740 }
1741 Restore_File_System.resize(second_period);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001742 return Restore_File_System;
Dees_Troy51a0e822012-09-05 15:24:24 -04001743}
1744
1745string TWPartition::Backup_Method_By_Name() {
bigbiffce8f83c2015-12-12 18:30:21 -05001746 if (Backup_Method == BM_NONE)
Dees_Troy38bd7602012-09-14 13:33:53 -04001747 return "none";
bigbiffce8f83c2015-12-12 18:30:21 -05001748 else if (Backup_Method == BM_FILES)
Dees_Troy38bd7602012-09-14 13:33:53 -04001749 return "files";
bigbiffce8f83c2015-12-12 18:30:21 -05001750 else if (Backup_Method == BM_DD)
Dees_Troy38bd7602012-09-14 13:33:53 -04001751 return "dd";
bigbiffce8f83c2015-12-12 18:30:21 -05001752 else if (Backup_Method == BM_FLASH_UTILS)
Dees_Troy38bd7602012-09-14 13:33:53 -04001753 return "flash_utils";
1754 else
1755 return "undefined";
1756 return "ERROR!";
Dees_Troy51a0e822012-09-05 15:24:24 -04001757}
1758
1759bool TWPartition::Decrypt(string Password) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001760 LOGINFO("STUB TWPartition::Decrypt, password: '%s'\n", Password.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001761 // Is this needed?
Dees_Troy51a0e822012-09-05 15:24:24 -04001762 return 1;
1763}
1764
1765bool TWPartition::Wipe_Encryption() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001766 bool Save_Data_Media = Has_Data_Media;
1767
1768 if (!UnMount(true))
1769 return false;
1770
Dees_Troy38bd7602012-09-14 13:33:53 -04001771 Has_Data_Media = false;
Dees_Troy74fb2e92013-04-15 14:35:47 +00001772 Decrypted_Block_Device = "";
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001773#ifdef TW_INCLUDE_CRYPTO
Ethan Yonker1b190162016-12-05 15:25:19 -06001774 if (Is_Decrypted && !Decrypted_Block_Device.empty()) {
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001775 if (!UnMount(true))
1776 return false;
Matt Mower2b18a532015-02-20 16:58:05 -06001777 if (delete_crypto_blk_dev((char*)("userdata")) != 0) {
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001778 LOGERR("Error deleting crypto block device, continuing anyway.\n");
1779 }
1780 }
1781#endif
Dees_Troy74fb2e92013-04-15 14:35:47 +00001782 Is_Decrypted = false;
1783 Is_Encrypted = false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001784 Find_Actual_Block_Device();
Ethan Yonkerbc85b632015-08-09 12:48:14 -05001785 if (Crypto_Key_Location == "footer") {
1786 int newlen, fd;
1787 if (Length != 0) {
1788 newlen = Length;
1789 if (newlen < 0)
1790 newlen = newlen * -1;
1791 } else {
1792 newlen = CRYPT_FOOTER_OFFSET;
1793 }
1794 if ((fd = open(Actual_Block_Device.c_str(), O_RDWR)) < 0) {
1795 gui_print_color("warning", "Unable to open '%s' to wipe crypto key\n", Actual_Block_Device.c_str());
1796 } else {
1797 unsigned int block_count;
1798 if ((ioctl(fd, BLKGETSIZE, &block_count)) == -1) {
1799 gui_print_color("warning", "Unable to get block size for wiping crypto footer.\n");
1800 } else {
1801 off64_t offset = ((off64_t)block_count * 512) - newlen;
1802 if (lseek64(fd, offset, SEEK_SET) == -1) {
1803 gui_print_color("warning", "Unable to lseek64 for wiping crypto footer.\n");
1804 } else {
1805 void* buffer = malloc(newlen);
1806 if (!buffer) {
1807 gui_print_color("warning", "Failed to malloc for wiping crypto footer.\n");
1808 } else {
1809 memset(buffer, 0, newlen);
1810 int ret = write(fd, buffer, newlen);
1811 if (ret != newlen) {
1812 gui_print_color("warning", "Failed to wipe crypto footer.\n");
1813 } else {
1814 LOGINFO("Successfully wiped crypto footer.\n");
1815 }
Matt Mower13a8f0b2015-09-26 15:40:03 -05001816 free(buffer);
Ethan Yonkerbc85b632015-08-09 12:48:14 -05001817 }
1818 }
1819 }
1820 close(fd);
1821 }
1822 } else {
Ethan Yonkerc2dafbb2016-03-15 22:20:59 -05001823 if (TWFunc::IOCTL_Get_Block_Size(Crypto_Key_Location.c_str()) >= 16384LLU) {
1824 string Command = "dd of='" + Crypto_Key_Location + "' if=/dev/zero bs=16384 count=1";
1825 TWFunc::Exec_Cmd(Command);
1826 } else {
1827 LOGINFO("Crypto key location reports size < 16K so not wiping crypto footer.\n");
1828 }
Ethan Yonkerbc85b632015-08-09 12:48:14 -05001829 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001830 if (Wipe(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001831 Has_Data_Media = Save_Data_Media;
1832 if (Has_Data_Media && !Symlink_Mount_Point.empty()) {
1833 Recreate_Media_Folder();
Ethan Yonker726a0202014-12-16 20:01:38 -06001834 if (Mount(false))
1835 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04001836 }
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001837 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
Ethan Yonker83e82572014-04-04 10:59:28 -05001838#ifndef TW_OEM_BUILD
Ethan Yonker74db1572015-10-28 12:44:49 -05001839 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 -05001840#endif
Dees_Troy38bd7602012-09-14 13:33:53 -04001841 return true;
1842 } else {
1843 Has_Data_Media = Save_Data_Media;
Ethan Yonker74db1572015-10-28 12:44:49 -05001844 gui_err("format_data_err=Unable to format to remove encryption.");
Ethan Yonker726a0202014-12-16 20:01:38 -06001845 if (Has_Data_Media && Mount(false))
1846 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04001847 return false;
1848 }
1849 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001850}
1851
1852void TWPartition::Check_FS_Type() {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001853 const char* type;
1854 blkid_probe pr;
Dees_Troy5bf43922012-09-07 16:07:55 -04001855
Dees_Troy68cab492012-12-12 19:29:35 +00001856 if (Fstab_File_System == "yaffs2" || Fstab_File_System == "mtd" || Fstab_File_System == "bml" || Ignore_Blkid)
1857 return; // Running blkid on some mtd devices causes a massive crash or needs to be skipped
Dees_Troy5bf43922012-09-07 16:07:55 -04001858
Dees_Troy38bd7602012-09-14 13:33:53 -04001859 Find_Actual_Block_Device();
Dees_Troy8170a922012-09-18 15:40:25 -04001860 if (!Is_Present)
1861 return;
Dees_Troy51127312012-09-08 13:08:49 -04001862
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001863 pr = blkid_new_probe_from_filename(Actual_Block_Device.c_str());
1864 if (blkid_do_fullprobe(pr)) {
1865 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00001866 LOGINFO("Can't probe device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001867 return;
Dees_Troy5bf43922012-09-07 16:07:55 -04001868 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001869
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05001870 if (blkid_probe_lookup_value(pr, "TYPE", &type, NULL) < 0) {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001871 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00001872 LOGINFO("can't find filesystem on device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001873 return;
1874 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001875
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001876 Current_File_System = type;
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001877 blkid_free_probe(pr);
Dees_Troy51a0e822012-09-05 15:24:24 -04001878}
1879
Gary Peck43acadf2012-11-21 21:19:01 -08001880bool TWPartition::Wipe_EXT23(string File_System) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001881 if (!UnMount(true))
1882 return false;
1883
Dees_Troy43d8b002012-09-17 16:00:01 -04001884 if (TWFunc::Path_Exists("/sbin/mke2fs")) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001885 string command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001886
Greg Wallace4b44fef2015-12-29 15:33:24 -05001887 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mke2fs"));
Dees_Troy38bd7602012-09-14 13:33:53 -04001888 Find_Actual_Block_Device();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001889 command = "mke2fs -t " + File_System + " -m 0 " + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001890 LOGINFO("mke2fs command: %s\n", command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001891 if (TWFunc::Exec_Cmd(command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001892 Current_File_System = File_System;
Dees_Troye58d5262012-09-21 12:27:57 -04001893 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05001894 gui_msg("done=Done.");
Dees_Troy38bd7602012-09-14 13:33:53 -04001895 return true;
1896 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001897 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04001898 return false;
1899 }
1900 } else
1901 return Wipe_RMRF();
1902
1903 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001904}
1905
1906bool TWPartition::Wipe_EXT4() {
Ethan Yonker25f20c12014-10-14 09:04:43 -05001907 Find_Actual_Block_Device();
1908 if (!Is_Present) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001909 LOGINFO("Block device not present, cannot wipe %s.\n", Display_Name.c_str());
1910 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Ethan Yonker25f20c12014-10-14 09:04:43 -05001911 return false;
1912 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001913 if (!UnMount(true))
1914 return false;
1915
Dees_Troyb3265ab2013-08-30 02:59:14 +00001916#if defined(HAVE_SELINUX) && defined(USE_EXT4)
Ethan Yonkerf27497f2014-02-09 11:48:33 -06001917 int ret;
1918 char *secontext = NULL;
1919
Greg Wallace4b44fef2015-12-29 15:33:24 -05001920 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("make_ext4fs"));
Ethan Yonkerf27497f2014-02-09 11:48:33 -06001921
Dees Troy99c8dbf2014-03-10 16:53:58 +00001922 if (!selinux_handle || selabel_lookup(selinux_handle, &secontext, Mount_Point.c_str(), S_IFDIR) < 0) {
Ethan Yonkerf27497f2014-02-09 11:48:33 -06001923 LOGINFO("Cannot lookup security context for '%s'\n", Mount_Point.c_str());
1924 ret = make_ext4fs(Actual_Block_Device.c_str(), Length, Mount_Point.c_str(), NULL);
1925 } else {
1926 ret = make_ext4fs(Actual_Block_Device.c_str(), Length, Mount_Point.c_str(), selinux_handle);
1927 }
1928 if (ret != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001929 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troya95f55c2013-08-17 13:14:43 +00001930 return false;
1931 } else {
bigbiff bigbiffc49d7062013-10-11 20:28:00 -04001932 string sedir = Mount_Point + "/lost+found";
1933 PartitionManager.Mount_By_Path(sedir.c_str(), true);
1934 rmdir(sedir.c_str());
1935 mkdir(sedir.c_str(), S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP);
Dees_Troya95f55c2013-08-17 13:14:43 +00001936 return true;
1937 }
1938#else
Dees_Troy43d8b002012-09-17 16:00:01 -04001939 if (TWFunc::Path_Exists("/sbin/make_ext4fs")) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001940 string Command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001941
Greg Wallace4b44fef2015-12-29 15:33:24 -05001942 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("make_ext4fs"));
Dees_Troy38bd7602012-09-14 13:33:53 -04001943 Find_Actual_Block_Device();
1944 Command = "make_ext4fs";
1945 if (!Is_Decrypted && Length != 0) {
1946 // Only use length if we're not decrypted
1947 char len[32];
1948 sprintf(len, "%i", Length);
1949 Command += " -l ";
1950 Command += len;
1951 }
Dees_Troy5295d582013-09-06 15:51:08 +00001952 if (TWFunc::Path_Exists("/file_contexts")) {
1953 Command += " -S /file_contexts";
1954 }
1955 Command += " -a " + Mount_Point + " " + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001956 LOGINFO("make_ext4fs command: %s\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001957 if (TWFunc::Exec_Cmd(Command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001958 Current_File_System = "ext4";
Dees_Troye58d5262012-09-21 12:27:57 -04001959 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05001960 gui_msg("done=Done.");
Dees_Troy38bd7602012-09-14 13:33:53 -04001961 return true;
1962 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001963 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04001964 return false;
1965 }
1966 } else
Gary Peck43acadf2012-11-21 21:19:01 -08001967 return Wipe_EXT23("ext4");
Dees_Troya95f55c2013-08-17 13:14:43 +00001968#endif
Dees_Troy38bd7602012-09-14 13:33:53 -04001969 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001970}
1971
1972bool TWPartition::Wipe_FAT() {
Vojtech Bocek05534202013-09-11 08:11:56 +02001973 string command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001974
Matt Mower18794c82015-11-11 16:22:45 -06001975 if (TWFunc::Path_Exists("/sbin/mkfs.fat")) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001976 if (!UnMount(true))
1977 return false;
1978
Greg Wallace4b44fef2015-12-29 15:33:24 -05001979 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mkfs.fat"));
Dees_Troy38bd7602012-09-14 13:33:53 -04001980 Find_Actual_Block_Device();
Matt Mower18794c82015-11-11 16:22:45 -06001981 command = "mkfs.fat " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02001982 if (TWFunc::Exec_Cmd(command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001983 Current_File_System = "vfat";
Dees_Troye58d5262012-09-21 12:27:57 -04001984 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05001985 gui_msg("done=Done.");
Dees_Troy38bd7602012-09-14 13:33:53 -04001986 return true;
1987 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001988 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04001989 return false;
1990 }
1991 return true;
1992 }
1993 else
1994 return Wipe_RMRF();
1995
1996 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001997}
1998
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001999bool TWPartition::Wipe_EXFAT() {
Vojtech Bocek05534202013-09-11 08:11:56 +02002000 string command;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002001
2002 if (TWFunc::Path_Exists("/sbin/mkexfatfs")) {
2003 if (!UnMount(true))
2004 return false;
2005
Greg Wallace4b44fef2015-12-29 15:33:24 -05002006 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mkexfatfs"));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002007 Find_Actual_Block_Device();
2008 command = "mkexfatfs " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02002009 if (TWFunc::Exec_Cmd(command) == 0) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002010 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05002011 gui_msg("done=Done.");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002012 return true;
2013 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002014 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002015 return false;
2016 }
2017 return true;
2018 }
2019 return false;
2020}
2021
Dees_Troy38bd7602012-09-14 13:33:53 -04002022bool TWPartition::Wipe_MTD() {
2023 if (!UnMount(true))
2024 return false;
2025
Greg Wallace4b44fef2015-12-29 15:33:24 -05002026 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("MTD"));
Dees_Troy38bd7602012-09-14 13:33:53 -04002027
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02002028 mtd_scan_partitions();
2029 const MtdPartition* mtd = mtd_find_partition_by_name(MTD_Name.c_str());
2030 if (mtd == NULL) {
2031 LOGERR("No mtd partition named '%s'", MTD_Name.c_str());
2032 return false;
2033 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002034
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02002035 MtdWriteContext* ctx = mtd_write_partition(mtd);
2036 if (ctx == NULL) {
2037 LOGERR("Can't write '%s', failed to format.", MTD_Name.c_str());
2038 return false;
2039 }
2040 if (mtd_erase_blocks(ctx, -1) == -1) {
2041 mtd_write_close(ctx);
2042 LOGERR("Failed to format '%s'", MTD_Name.c_str());
2043 return false;
2044 }
2045 if (mtd_write_close(ctx) != 0) {
2046 LOGERR("Failed to close '%s'", MTD_Name.c_str());
2047 return false;
2048 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08002049 Current_File_System = "yaffs2";
Dees_Troye58d5262012-09-21 12:27:57 -04002050 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05002051 gui_msg("done=Done.");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02002052 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002053}
2054
2055bool TWPartition::Wipe_RMRF() {
Dees_Troy38bd7602012-09-14 13:33:53 -04002056 if (!Mount(true))
2057 return false;
Ethan Yonker726a0202014-12-16 20:01:38 -06002058 // This is the only wipe that leaves the partition mounted, so we
2059 // must manually remove the partition from MTP if it is a storage
2060 // partition.
2061 if (Is_Storage)
2062 PartitionManager.Remove_MTP_Storage(MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04002063
Ethan Yonker74db1572015-10-28 12:44:49 -05002064 gui_msg(Msg("remove_all=Removing all files under '{1}'")(Mount_Point));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002065 TWFunc::removeDir(Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04002066 Recreate_AndSec_Folder();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002067 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002068}
2069
Dees_Troye5017042013-08-29 16:38:55 +00002070bool TWPartition::Wipe_F2FS() {
Vojtech Bocek05534202013-09-11 08:11:56 +02002071 string command;
Dees_Troye5017042013-08-29 16:38:55 +00002072
2073 if (TWFunc::Path_Exists("/sbin/mkfs.f2fs")) {
2074 if (!UnMount(true))
2075 return false;
2076
Greg Wallace4b44fef2015-12-29 15:33:24 -05002077 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mkfs.f2fs"));
Dees_Troye5017042013-08-29 16:38:55 +00002078 Find_Actual_Block_Device();
Greg Wallace558aef72016-01-19 21:14:30 -05002079 command = "mkfs.f2fs -t 0";
dhacker29a3fa75f2015-01-17 19:42:33 -05002080 if (!Is_Decrypted && Length != 0) {
2081 // Only use length if we're not decrypted
2082 char len[32];
2083 int mod_length = Length;
2084 if (Length < 0)
2085 mod_length *= -1;
2086 sprintf(len, "%i", mod_length);
2087 command += " -r ";
2088 command += len;
2089 }
2090 command += " " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02002091 if (TWFunc::Exec_Cmd(command) == 0) {
Dees_Troye5017042013-08-29 16:38:55 +00002092 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05002093 gui_msg("done=Done.");
Dees_Troye5017042013-08-29 16:38:55 +00002094 return true;
2095 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002096 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troye5017042013-08-29 16:38:55 +00002097 return false;
2098 }
2099 return true;
2100 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002101 LOGINFO("mkfs.f2fs binary not found, using rm -rf to wipe.\n");
Dees_Troye5017042013-08-29 16:38:55 +00002102 return Wipe_RMRF();
2103 }
2104 return false;
2105}
2106
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002107bool TWPartition::Wipe_NTFS() {
2108 string command;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002109 string Ntfsmake_Binary;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002110
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002111 if (TWFunc::Path_Exists("/sbin/mkntfs"))
2112 Ntfsmake_Binary = "mkntfs";
2113 else if (TWFunc::Path_Exists("/sbin/mkfs.ntfs"))
2114 Ntfsmake_Binary = "mkfs.ntfs";
2115 else
2116 return false;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002117
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002118 if (!UnMount(true))
2119 return false;
2120
Greg Wallace4b44fef2015-12-29 15:33:24 -05002121 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)(Ntfsmake_Binary));
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002122 Find_Actual_Block_Device();
2123 command = "/sbin/" + Ntfsmake_Binary + " " + Actual_Block_Device;
2124 if (TWFunc::Exec_Cmd(command) == 0) {
2125 Recreate_AndSec_Folder();
2126 gui_msg("done=Done.");
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002127 return true;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002128 } else {
2129 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
2130 return false;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002131 }
2132 return false;
2133}
2134
Dees_Troy51a0e822012-09-05 15:24:24 -04002135bool TWPartition::Wipe_Data_Without_Wiping_Media() {
Ethan Yonker83e82572014-04-04 10:59:28 -05002136#ifdef TW_OEM_BUILD
2137 // In an OEM Build we want to do a full format
2138 return Wipe_Encryption();
2139#else
Ethan Yonker66a19492015-12-10 10:19:45 -06002140 bool ret = false;
Dees_Troy38bd7602012-09-14 13:33:53 -04002141
Dees_Troy38bd7602012-09-14 13:33:53 -04002142 if (!Mount(true))
2143 return false;
2144
Ethan Yonker74db1572015-10-28 12:44:49 -05002145 gui_msg("wiping_data=Wiping data without wiping /data/media ...");
Ethan Yonker66a19492015-12-10 10:19:45 -06002146 ret = Wipe_Data_Without_Wiping_Media_Func(Mount_Point + "/");
2147 if (ret)
2148 gui_msg("done=Done.");
2149 return ret;
2150#endif // ifdef TW_OEM_BUILD
2151}
2152
2153bool TWPartition::Wipe_Data_Without_Wiping_Media_Func(const string& parent __unused) {
2154 string dir;
Dees_Troy38bd7602012-09-14 13:33:53 -04002155
2156 DIR* d;
Ethan Yonker66a19492015-12-10 10:19:45 -06002157 d = opendir(parent.c_str());
Dees_Troy16b74352012-11-14 22:27:31 +00002158 if (d != NULL) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002159 struct dirent* de;
2160 while ((de = readdir(d)) != NULL) {
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002161 if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) continue;
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05002162
Ethan Yonker66a19492015-12-10 10:19:45 -06002163 dir = parent;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002164 dir.append(de->d_name);
Ethan Yonker3fdcda42016-11-30 12:29:37 -06002165 if (wipe_exclusions.check_skip_dirs(dir)) {
Ethan Yonker66a19492015-12-10 10:19:45 -06002166 LOGINFO("skipped '%s'\n", dir.c_str());
2167 continue;
2168 }
Dees_Troyce675462013-01-09 19:48:21 +00002169 if (de->d_type == DT_DIR) {
Ethan Yonker66a19492015-12-10 10:19:45 -06002170 dir.append("/");
2171 if (!Wipe_Data_Without_Wiping_Media_Func(dir)) {
2172 closedir(d);
2173 return false;
2174 }
2175 rmdir(dir.c_str());
bigbiff bigbiff98f1f902013-01-19 18:46:13 -05002176 } 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 +00002177 if (!unlink(dir.c_str()))
Ethan Yonker79f88bd2016-12-09 14:52:12 -06002178 LOGINFO("Unable to unlink '%s': %s\n", dir.c_str(), strerror(errno));
Dees_Troyce675462013-01-09 19:48:21 +00002179 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002180 }
2181 closedir(d);
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05002182
Dees_Troy16b74352012-11-14 22:27:31 +00002183 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -04002184 }
Ethan Yonker74db1572015-10-28 12:44:49 -05002185 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy16b74352012-11-14 22:27:31 +00002186 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04002187}
2188
bigbiffce8f83c2015-12-12 18:30:21 -05002189bool TWPartition::Backup_Tar(PartitionSettings *part_settings, pid_t *tar_fork_pid) {
Ethan Yonker472f5062016-02-25 13:47:30 -06002190 string Full_FileName;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002191 twrpTar tar;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05002192
Dees_Troy43d8b002012-09-17 16:00:01 -04002193 if (!Mount(true))
2194 return false;
2195
Dees_Troya13d74f2013-03-24 08:54:55 -05002196 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Backup_Display_Name, "Backing Up");
Ethan Yonker74db1572015-10-28 12:44:49 -05002197 gui_msg(Msg("backing_up=Backing up {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002198
Ethan Yonker472f5062016-02-25 13:47:30 -06002199 DataManager::GetValue(TW_USE_COMPRESSION_VAR, tar.use_compression);
Matt Mowerbb81e5d2014-03-20 18:05:41 -05002200
Dees_Troy83bd4832013-05-04 12:39:56 +00002201#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
Ethan Yonker472f5062016-02-25 13:47:30 -06002202 if (Can_Encrypt_Backup) {
2203 DataManager::GetValue("tw_encrypt_backup", tar.use_encryption);
2204 if (tar.use_encryption) {
2205 if (Use_Userdata_Encryption)
2206 tar.userdata_encryption = tar.use_encryption;
2207 string Password;
2208 DataManager::GetValue("tw_backup_password", Password);
2209 tar.setpassword(Password);
2210 } else {
2211 tar.use_encryption = 0;
2212 }
Dees_Troy83bd4832013-05-04 12:39:56 +00002213 }
2214#endif
Dees_Troy43d8b002012-09-17 16:00:01 -04002215
Ethan Yonker472f5062016-02-25 13:47:30 -06002216 Backup_FileName = Backup_Name + "." + Current_File_System + ".win";
Ethan Yonkere080c1f2016-09-19 13:50:25 -05002217 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
Dees_Troy83bd4832013-05-04 12:39:56 +00002218 tar.has_data_media = Has_Data_Media;
bigbiffce8f83c2015-12-12 18:30:21 -05002219 tar.part_settings = part_settings;
Ethan Yonker3fdcda42016-11-30 12:29:37 -06002220 tar.backup_exclusions = &backup_exclusions;
Dees Troye0a433a2013-12-02 04:10:37 +00002221 tar.setdir(Backup_Path);
2222 tar.setfn(Full_FileName);
2223 tar.setsize(Backup_Size);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002224 tar.partition_name = Backup_Name;
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002225 tar.backup_folder = part_settings->Backup_Folder;
bigbiffce8f83c2015-12-12 18:30:21 -05002226 if (tar.createTarFork(tar_fork_pid) != 0)
Dees Troye0a433a2013-12-02 04:10:37 +00002227 return false;
Dees_Troy43d8b002012-09-17 16:00:01 -04002228 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002229}
2230
bigbiffce8f83c2015-12-12 18:30:21 -05002231bool TWPartition::Backup_Image(PartitionSettings *part_settings) {
2232 string Full_FileName, adb_file_name;
2233 int adb_control_bu_fd, compressed;
igoriok87e3d932013-01-31 21:03:53 +02002234
Ethan Yonker74db1572015-10-28 12:44:49 -05002235 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, gui_parse_text("{@backing}"));
2236 gui_msg(Msg("backing_up=Backing up {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002237
Ethan Yonker472f5062016-02-25 13:47:30 -06002238 Backup_FileName = Backup_Name + "." + Current_File_System + ".win";
Dees_Troy43d8b002012-09-17 16:00:01 -04002239
bigbiffce8f83c2015-12-12 18:30:21 -05002240 if (part_settings->adbbackup) {
2241 Full_FileName = TW_ADB_BACKUP;
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002242 adb_file_name = part_settings->Backup_Folder + "/" + Backup_FileName;
bigbiffce8f83c2015-12-12 18:30:21 -05002243 }
2244 else
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002245 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
bigbiffce8f83c2015-12-12 18:30:21 -05002246
2247 part_settings->total_restore_size = Backup_Size;
2248
2249 if (part_settings->adbbackup) {
2250 if (!twadbbu::Write_TWIMG(adb_file_name, Backup_Size))
2251 return false;
2252 }
2253
2254 if (!Raw_Read_Write(part_settings))
Ethan Yonker472f5062016-02-25 13:47:30 -06002255 return false;
2256
bigbiffce8f83c2015-12-12 18:30:21 -05002257 if (part_settings->adbbackup) {
2258 if (!twadbbu::Write_TWEOF())
2259 return false;
Dees_Troy7c2dec82012-09-26 09:49:14 -04002260 }
Dees_Troy43d8b002012-09-17 16:00:01 -04002261 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002262}
2263
bigbiffce8f83c2015-12-12 18:30:21 -05002264bool TWPartition::Raw_Read_Write(PartitionSettings *part_settings) {
2265 unsigned long long RW_Block_Size, Remain = Backup_Size;
2266 int src_fd = -1, dest_fd = -1;
2267 ssize_t bs;
Ethan Yonker472f5062016-02-25 13:47:30 -06002268 bool ret = false;
2269 void* buffer = NULL;
2270 unsigned long long backedup_size = 0;
bigbiffce8f83c2015-12-12 18:30:21 -05002271 string srcfn, destfn;
Ethan Yonker472f5062016-02-25 13:47:30 -06002272
bigbiffce8f83c2015-12-12 18:30:21 -05002273 if (part_settings->PM_Method == PM_BACKUP) {
2274 srcfn = Actual_Block_Device;
2275 if (part_settings->adbbackup)
2276 destfn = TW_ADB_BACKUP;
2277 else
Ethan Yonkere080c1f2016-09-19 13:50:25 -05002278 destfn = part_settings->Backup_Folder + "/" + Backup_FileName;
bigbiffce8f83c2015-12-12 18:30:21 -05002279 }
2280 else {
2281 destfn = Actual_Block_Device;
2282 if (part_settings->adbbackup) {
2283 srcfn = TW_ADB_RESTORE;
2284 } else {
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002285 srcfn = part_settings->Backup_Folder + "/" + Backup_FileName;
bigbiffce8f83c2015-12-12 18:30:21 -05002286 Remain = TWFunc::Get_File_Size(srcfn);
2287 }
2288 }
Ethan Yonker472f5062016-02-25 13:47:30 -06002289
bigbiffce8f83c2015-12-12 18:30:21 -05002290 src_fd = open(srcfn.c_str(), O_RDONLY | O_LARGEFILE);
Ethan Yonker472f5062016-02-25 13:47:30 -06002291 if (src_fd < 0) {
bigbiffce8f83c2015-12-12 18:30:21 -05002292 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(srcfn.c_str())(strerror(errno)));
Ethan Yonker472f5062016-02-25 13:47:30 -06002293 return false;
2294 }
bigbiffce8f83c2015-12-12 18:30:21 -05002295
2296 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 -06002297 if (dest_fd < 0) {
bigbiffce8f83c2015-12-12 18:30:21 -05002298 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(destfn.c_str())(strerror(errno)));
Ethan Yonker472f5062016-02-25 13:47:30 -06002299 goto exit;
2300 }
bigbiffce8f83c2015-12-12 18:30:21 -05002301
2302 LOGINFO("Reading '%s', writing '%s'\n", srcfn.c_str(), destfn.c_str());
2303
2304 if (part_settings->adbbackup) {
2305 RW_Block_Size = MAX_ADB_READ;
2306 bs = MAX_ADB_READ;
2307 }
2308 else {
2309 RW_Block_Size = 1048576LLU; // 1MB
2310 bs = (ssize_t)(RW_Block_Size);
2311 }
2312
Ethan Yonker472f5062016-02-25 13:47:30 -06002313 buffer = malloc((size_t)bs);
2314 if (!buffer) {
2315 LOGINFO("Raw_Read_Write failed to malloc\n");
2316 goto exit;
2317 }
bigbiffce8f83c2015-12-12 18:30:21 -05002318
2319 if (part_settings->progress)
2320 part_settings->progress->SetPartitionSize(part_settings->total_restore_size);
2321
Ethan Yonker472f5062016-02-25 13:47:30 -06002322 while (Remain > 0) {
2323 if (Remain < RW_Block_Size)
bigbiffce8f83c2015-12-12 18:30:21 -05002324 bs = (ssize_t)(Remain);
2325 if (read(src_fd, buffer, bs) != bs) {
Ethan Yonker472f5062016-02-25 13:47:30 -06002326 LOGINFO("Error reading source fd (%s)\n", strerror(errno));
2327 goto exit;
2328 }
2329 if (write(dest_fd, buffer, bs) != bs) {
2330 LOGINFO("Error writing destination fd (%s)\n", strerror(errno));
2331 goto exit;
2332 }
2333 backedup_size += (unsigned long long)(bs);
bigbiffce8f83c2015-12-12 18:30:21 -05002334 Remain = Remain - (unsigned long long)(bs);
2335 if (part_settings->progress)
2336 part_settings->progress->UpdateSize(backedup_size);
Ethan Yonker472f5062016-02-25 13:47:30 -06002337 if (PartitionManager.Check_Backup_Cancel() != 0)
2338 goto exit;
2339 }
bigbiffce8f83c2015-12-12 18:30:21 -05002340 if (part_settings->progress)
2341 part_settings->progress->UpdateDisplayDetails(true);
Ethan Yonker472f5062016-02-25 13:47:30 -06002342 fsync(dest_fd);
2343 ret = true;
2344exit:
2345 if (src_fd >= 0)
2346 close(src_fd);
2347 if (dest_fd >= 0)
2348 close(dest_fd);
2349 if (buffer)
2350 free(buffer);
2351 return ret;
2352}
2353
bigbiffce8f83c2015-12-12 18:30:21 -05002354bool TWPartition::Backup_Dump_Image(PartitionSettings *part_settings) {
Vojtech Bocek05534202013-09-11 08:11:56 +02002355 string Full_FileName, Command;
bigbiffce8f83c2015-12-12 18:30:21 -05002356 int use_compression, adb_control_bu_fd;
2357 unsigned long long compressed;
Dees_Troy43d8b002012-09-17 16:00:01 -04002358
Ethan Yonker74db1572015-10-28 12:44:49 -05002359 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, gui_parse_text("{@backing}"));
2360 gui_msg(Msg("backing_up=Backing up {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002361
bigbiffce8f83c2015-12-12 18:30:21 -05002362 if (part_settings->progress)
2363 part_settings->progress->SetPartitionSize(Backup_Size);
Dees_Troy43d8b002012-09-17 16:00:01 -04002364
Ethan Yonker472f5062016-02-25 13:47:30 -06002365 Backup_FileName = Backup_Name + "." + Current_File_System + ".win";
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002366 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04002367
2368 Command = "dump_image " + MTD_Name + " '" + Full_FileName + "'";
bigbiffce8f83c2015-12-12 18:30:21 -05002369
Dees_Troy2673cec2013-04-02 20:22:16 +00002370 LOGINFO("Backup command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02002371 TWFunc::Exec_Cmd(Command);
Ethan Yonker4b94cfd2014-12-11 10:00:45 -06002372 tw_set_default_metadata(Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04002373 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
2374 // Actual size may not match backup size due to bad blocks on MTD devices so just check for 0 bytes
Matt Mower3c366972015-12-25 19:28:31 -06002375 gui_msg(Msg(msg::kError, "backup_size=Backup file size for '{1}' is 0 bytes.")(Full_FileName));
Dees_Troy7c2dec82012-09-26 09:49:14 -04002376 return false;
2377 }
bigbiffce8f83c2015-12-12 18:30:21 -05002378 if (part_settings->progress)
2379 part_settings->progress->UpdateSize(Backup_Size);
2380
Dees_Troy43d8b002012-09-17 16:00:01 -04002381 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002382}
2383
bigbiffce8f83c2015-12-12 18:30:21 -05002384unsigned long long TWPartition::Get_Restore_Size(PartitionSettings *part_settings) {
2385 if (!part_settings->adbbackup) {
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002386 InfoManager restore_info(part_settings->Backup_Folder + "/" + Backup_Name + ".info");
bigbiffce8f83c2015-12-12 18:30:21 -05002387 if (restore_info.LoadValues() == 0) {
2388 if (restore_info.GetValue("backup_size", Restore_Size) == 0) {
2389 LOGINFO("Read info file, restore size is %llu\n", Restore_Size);
2390 return Restore_Size;
2391 }
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002392 }
2393 }
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002394
bigbiffce8f83c2015-12-12 18:30:21 -05002395 string Full_FileName, Restore_File_System = Get_Restore_File_System(part_settings);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002396
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002397 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002398 if (Is_Image(Restore_File_System)) {
2399 Restore_Size = TWFunc::Get_File_Size(Full_FileName);
2400 return Restore_Size;
2401 }
2402
2403 twrpTar tar;
2404 tar.setdir(Backup_Path);
2405 tar.setfn(Full_FileName);
bigbiffce8f83c2015-12-12 18:30:21 -05002406 tar.backup_name = Full_FileName;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002407#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
2408 string Password;
2409 DataManager::GetValue("tw_restore_password", Password);
2410 if (!Password.empty())
2411 tar.setpassword(Password);
2412#endif
2413 tar.partition_name = Backup_Name;
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002414 tar.backup_folder = part_settings->Backup_Folder;
bigbiffce8f83c2015-12-12 18:30:21 -05002415 tar.part_settings = part_settings;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002416 Restore_Size = tar.get_size();
2417 return Restore_Size;
2418}
2419
bigbiffce8f83c2015-12-12 18:30:21 -05002420bool TWPartition::Restore_Tar(PartitionSettings *part_settings) {
Ethan Yonker472f5062016-02-25 13:47:30 -06002421 string Full_FileName;
Dees Troy4159aed2014-02-28 17:24:43 +00002422 bool ret = false;
bigbiffce8f83c2015-12-12 18:30:21 -05002423 string Restore_File_System = Get_Restore_File_System(part_settings);
Dees_Troy43d8b002012-09-17 16:00:01 -04002424
Dees_Troye58d5262012-09-21 12:27:57 -04002425 if (Has_Android_Secure) {
Dees_Troye58d5262012-09-21 12:27:57 -04002426 if (!Wipe_AndSec())
2427 return false;
Gary Peck43acadf2012-11-21 21:19:01 -08002428 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002429 gui_msg(Msg("wiping=Wiping {1}")(Backup_Display_Name));
Ethan Yonker5eac2222014-06-11 12:22:55 -05002430 if (Has_Data_Media && Mount_Point == "/data" && Restore_File_System != Current_File_System) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002431 gui_msg(Msg(msg::kWarning, "datamedia_fs_restore=WARNING: This /data backup was made with {1} file system! The backup may not boot unless you change back to {1}.")(Restore_File_System));
Ethan Yonker5eac2222014-06-11 12:22:55 -05002432 if (!Wipe_Data_Without_Wiping_Media())
2433 return false;
2434 } else {
2435 if (!Wipe(Restore_File_System))
2436 return false;
2437 }
Dees_Troye58d5262012-09-21 12:27:57 -04002438 }
Matt Mower3c366972015-12-25 19:28:31 -06002439 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Backup_Display_Name, gui_parse_text("{@restoring_hdr}"));
Ethan Yonker74db1572015-10-28 12:44:49 -05002440 gui_msg(Msg("restoring=Restoring {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002441
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05002442 // Remount as read/write as needed so we can restore the backup
2443 if (!ReMount_RW(true))
Dees_Troy43d8b002012-09-17 16:00:01 -04002444 return false;
2445
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002446 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
Ethan Yonker87af5632014-02-10 11:56:35 -06002447 twrpTar tar;
bigbiffce8f83c2015-12-12 18:30:21 -05002448 tar.part_settings = part_settings;
Ethan Yonker87af5632014-02-10 11:56:35 -06002449 tar.setdir(Backup_Path);
2450 tar.setfn(Full_FileName);
2451 tar.backup_name = Backup_Name;
2452#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
2453 string Password;
2454 DataManager::GetValue("tw_restore_password", Password);
2455 if (!Password.empty())
2456 tar.setpassword(Password);
2457#endif
bigbiffce8f83c2015-12-12 18:30:21 -05002458 part_settings->progress->SetPartitionSize(Get_Restore_Size(part_settings));
2459 if (tar.extractTarFork() != 0)
Dees Troy4159aed2014-02-28 17:24:43 +00002460 ret = false;
2461 else
2462 ret = true;
2463#ifdef HAVE_CAPABILITIES
2464 // Restore capabilities to the run-as binary
2465 if (Mount_Point == "/system" && Mount(true) && TWFunc::Path_Exists("/system/bin/run-as")) {
2466 struct vfs_cap_data cap_data;
2467 uint64_t capabilities = (1 << CAP_SETUID) | (1 << CAP_SETGID);
2468
2469 memset(&cap_data, 0, sizeof(cap_data));
2470 cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE;
2471 cap_data.data[0].permitted = (uint32_t) (capabilities & 0xffffffff);
2472 cap_data.data[0].inheritable = 0;
2473 cap_data.data[1].permitted = (uint32_t) (capabilities >> 32);
2474 cap_data.data[1].inheritable = 0;
2475 if (setxattr("/system/bin/run-as", XATTR_NAME_CAPS, &cap_data, sizeof(cap_data), 0) < 0) {
2476 LOGINFO("Failed to reset capabilities of /system/bin/run-as binary.\n");
2477 } else {
2478 LOGINFO("Reset capabilities of /system/bin/run-as binary successful.\n");
2479 }
2480 }
2481#endif
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05002482 if (Mount_Read_Only || Mount_Flags & MS_RDONLY)
2483 // Remount as read only when restoration is complete
2484 ReMount(true);
2485
Dees Troy4159aed2014-02-28 17:24:43 +00002486 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04002487}
2488
bigbiffce8f83c2015-12-12 18:30:21 -05002489bool TWPartition::Restore_Image(PartitionSettings *part_settings) {
Ethan Yonker96af84a2015-01-05 14:58:36 -06002490 string Full_FileName;
bigbiffce8f83c2015-12-12 18:30:21 -05002491 string Restore_File_System = Get_Restore_File_System(part_settings);
Dees_Troy43d8b002012-09-17 16:00:01 -04002492
Matt Mower3c366972015-12-25 19:28:31 -06002493 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Backup_Display_Name, gui_parse_text("{@restoring_hdr}"));
Ethan Yonker74db1572015-10-28 12:44:49 -05002494 gui_msg(Msg("restoring=Restoring {1}...")(Backup_Display_Name));
bigbiffce8f83c2015-12-12 18:30:21 -05002495
2496 if (part_settings->adbbackup)
2497 Full_FileName = TW_ADB_RESTORE;
2498 else
Ethan Yonkere080c1f2016-09-19 13:50:25 -05002499 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
Gary Peck15e623d2012-11-21 21:07:58 -08002500
Ethan Yonker96af84a2015-01-05 14:58:36 -06002501 if (Restore_File_System == "emmc") {
bigbiffce8f83c2015-12-12 18:30:21 -05002502 if (!part_settings->adbbackup)
2503 part_settings->total_restore_size = (uint64_t)(TWFunc::Get_File_Size(Full_FileName));
2504 if (!Raw_Read_Write(part_settings))
Ethan Yonker96af84a2015-01-05 14:58:36 -06002505 return false;
2506 } else if (Restore_File_System == "mtd" || Restore_File_System == "bml") {
bigbiffce8f83c2015-12-12 18:30:21 -05002507 if (!Flash_Image_FI(Full_FileName, part_settings->progress))
2508 return false;
2509 }
2510
2511 if (part_settings->adbbackup) {
2512 if (!twadbbu::Write_TWEOF())
Ethan Yonker96af84a2015-01-05 14:58:36 -06002513 return false;
Gary Peck15e623d2012-11-21 21:07:58 -08002514 }
Dees_Troy43d8b002012-09-17 16:00:01 -04002515 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002516}
Dees_Troy5bf43922012-09-07 16:07:55 -04002517
2518bool TWPartition::Update_Size(bool Display_Error) {
Dees_Troy0550cfb2012-10-13 11:56:13 -04002519 bool ret = false, Was_Already_Mounted = false;
Dees_Troy51127312012-09-08 13:08:49 -04002520
Ethan Yonker1b190162016-12-05 15:25:19 -06002521 Find_Actual_Block_Device();
2522
2523 if (!Can_Be_Mounted && !Is_Encrypted) {
2524 if (TWFunc::Path_Exists(Actual_Block_Device) && Find_Partition_Size()) {
2525 Used = Size;
2526 Backup_Size = Size;
2527 return true;
2528 }
Dees_Troy5bf43922012-09-07 16:07:55 -04002529 return false;
Ethan Yonker1b190162016-12-05 15:25:19 -06002530 }
Dees_Troy5bf43922012-09-07 16:07:55 -04002531
Dees_Troy0550cfb2012-10-13 11:56:13 -04002532 Was_Already_Mounted = Is_Mounted();
Dees_Troy38bd7602012-09-14 13:33:53 -04002533 if (Removable || Is_Encrypted) {
2534 if (!Mount(false))
2535 return true;
2536 } else if (!Mount(Display_Error))
Dees_Troy5bf43922012-09-07 16:07:55 -04002537 return false;
Dees_Troy51127312012-09-08 13:08:49 -04002538
2539 ret = Get_Size_Via_statfs(Display_Error);
Dees_Troy0550cfb2012-10-13 11:56:13 -04002540 if (!ret || Size == 0) {
2541 if (!Get_Size_Via_df(Display_Error)) {
2542 if (!Was_Already_Mounted)
2543 UnMount(false);
Dees_Troy51127312012-09-08 13:08:49 -04002544 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04002545 }
2546 }
Dees_Troy51127312012-09-08 13:08:49 -04002547
Dees_Troy5bf43922012-09-07 16:07:55 -04002548 if (Has_Data_Media) {
2549 if (Mount(Display_Error)) {
Dees_Troy51127312012-09-08 13:08:49 -04002550 unsigned long long data_media_used, actual_data;
Ethan Yonker3fdcda42016-11-30 12:29:37 -06002551 Used = backup_exclusions.Get_Folder_Size(Mount_Point);
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002552 Backup_Size = Used;
2553 int bak = (int)(Used / 1048576LLU);
Dees_Troy51127312012-09-08 13:08:49 -04002554 int fre = (int)(Free / 1048576LLU);
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002555 LOGINFO("Data backup size is %iMB, free: %iMB.\n", bak, fre);
Dees_Troy0550cfb2012-10-13 11:56:13 -04002556 } else {
2557 if (!Was_Already_Mounted)
2558 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04002559 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04002560 }
Dees_Troye58d5262012-09-21 12:27:57 -04002561 } else if (Has_Android_Secure) {
2562 if (Mount(Display_Error))
Ethan Yonker3fdcda42016-11-30 12:29:37 -06002563 Backup_Size = backup_exclusions.Get_Folder_Size(Backup_Path);
Dees_Troy0550cfb2012-10-13 11:56:13 -04002564 else {
2565 if (!Was_Already_Mounted)
2566 UnMount(false);
Dees_Troye58d5262012-09-21 12:27:57 -04002567 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04002568 }
Dees_Troy5bf43922012-09-07 16:07:55 -04002569 }
Dees_Troy0550cfb2012-10-13 11:56:13 -04002570 if (!Was_Already_Mounted)
2571 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04002572 return true;
Dees_Troy51127312012-09-08 13:08:49 -04002573}
Dees_Troy38bd7602012-09-14 13:33:53 -04002574
2575void TWPartition::Find_Actual_Block_Device(void) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002576 if (Is_Decrypted && !Decrypted_Block_Device.empty()) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002577 Actual_Block_Device = Decrypted_Block_Device;
Ethan Yonker1b190162016-12-05 15:25:19 -06002578 if (TWFunc::Path_Exists(Decrypted_Block_Device)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002579 Is_Present = true;
Ethan Yonker1b190162016-12-05 15:25:19 -06002580 return;
2581 }
2582 } else if (SlotSelect && TWFunc::Path_Exists(Primary_Block_Device + PartitionManager.Get_Active_Slot_Suffix())) {
2583 Actual_Block_Device = Primary_Block_Device + PartitionManager.Get_Active_Slot_Suffix();
2584 unlink(Primary_Block_Device.c_str());
2585 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
2586 Is_Present = true;
2587 return;
Dees_Troy43d8b002012-09-17 16:00:01 -04002588 } else if (TWFunc::Path_Exists(Primary_Block_Device)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002589 Is_Present = true;
2590 Actual_Block_Device = Primary_Block_Device;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002591 return;
2592 }
Ethan Yonker1b190162016-12-05 15:25:19 -06002593 if (!Alternate_Block_Device.empty() && TWFunc::Path_Exists(Alternate_Block_Device)) {
Dees_Troy3f04d032012-10-07 18:20:09 -04002594 Actual_Block_Device = Alternate_Block_Device;
Dees_Troy38bd7602012-09-14 13:33:53 -04002595 Is_Present = true;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002596 } else {
Dees_Troy38bd7602012-09-14 13:33:53 -04002597 Is_Present = false;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002598 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002599}
2600
2601void TWPartition::Recreate_Media_Folder(void) {
2602 string Command;
Ethan Yonker66a19492015-12-10 10:19:45 -06002603 string Media_Path = Mount_Point + "/media";
Dees_Troy38bd7602012-09-14 13:33:53 -04002604
Ethan Yonkerbd7492d2016-12-07 13:55:01 -06002605 if (Is_FBE) {
2606 LOGINFO("Not recreating media folder on FBE\n");
2607 return;
2608 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002609 if (!Mount(true)) {
Ethan Yonker66a19492015-12-10 10:19:45 -06002610 gui_msg(Msg(msg::kError, "recreate_folder_err=Unable to recreate {1} folder.")(Media_Path));
2611 } else if (!TWFunc::Path_Exists(Media_Path)) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002612 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Ethan Yonker66a19492015-12-10 10:19:45 -06002613 LOGINFO("Recreating %s folder.\n", Media_Path.c_str());
2614 mkdir(Media_Path.c_str(), 0770);
xiaolu9416f4f2015-06-04 08:22:23 +08002615 string Internal_path = DataManager::GetStrValue("tw_internal_path");
2616 if (!Internal_path.empty()) {
2617 LOGINFO("Recreating %s folder.\n", Internal_path.c_str());
2618 mkdir(Internal_path.c_str(), 0770);
2619 }
2620#ifdef TW_INTERNAL_STORAGE_PATH
2621 mkdir(EXPAND(TW_INTERNAL_STORAGE_PATH), 0770);
2622#endif
Ethan Yonker5ef301e2014-10-14 10:04:44 -05002623#ifdef HAVE_SELINUX
thata3d31fb2014-12-21 22:27:40 +01002624 // Afterwards, we will try to set the
2625 // default metadata that we were hopefully able to get during
2626 // early boot.
Ethan Yonker66a19492015-12-10 10:19:45 -06002627 tw_set_default_metadata(Media_Path.c_str());
xiaolu9416f4f2015-06-04 08:22:23 +08002628 if (!Internal_path.empty())
2629 tw_set_default_metadata(Internal_path.c_str());
Ethan Yonker5ef301e2014-10-14 10:04:44 -05002630#endif
Ethan Yonker5eac2222014-06-11 12:22:55 -05002631 // Toggle mount to ensure that "internal sdcard" gets mounted
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002632 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Ethan Yonker5eac2222014-06-11 12:22:55 -05002633 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Dees_Troy38bd7602012-09-14 13:33:53 -04002634 }
Dees_Troy43d8b002012-09-17 16:00:01 -04002635}
Dees_Troye58d5262012-09-21 12:27:57 -04002636
2637void TWPartition::Recreate_AndSec_Folder(void) {
Dees_Troye58d5262012-09-21 12:27:57 -04002638 if (!Has_Android_Secure)
2639 return;
Dees_Troy2673cec2013-04-02 20:22:16 +00002640 LOGINFO("Creating %s: %s\n", Backup_Display_Name.c_str(), Symlink_Path.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -04002641 if (!Mount(true)) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002642 gui_msg(Msg(msg::kError, "recreate_folder_err=Unable to recreate {1} folder.")(Backup_Name));
Dees_Troye58d5262012-09-21 12:27:57 -04002643 } else if (!TWFunc::Path_Exists(Symlink_Path)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00002644 LOGINFO("Recreating %s folder.\n", Backup_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002645 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05002646 mkdir(Symlink_Path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002647 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04002648 }
2649}
bigbiff7cb4c332014-11-26 20:36:07 -05002650
2651uint64_t TWPartition::Get_Max_FileSize() {
2652 uint64_t maxFileSize = 0;
2653 const uint64_t constGB = (uint64_t) 1024 * 1024 * 1024;
2654 const uint64_t constTB = (uint64_t) constGB * 1024;
2655 const uint64_t constPB = (uint64_t) constTB * 1024;
2656 const uint64_t constEB = (uint64_t) constPB * 1024;
bigbiff0c532032014-12-21 13:41:26 -05002657 if (Current_File_System == "ext4")
2658 maxFileSize = 16 * constTB; //16 TB
2659 else if (Current_File_System == "vfat")
2660 maxFileSize = 4 * constGB; //4 GB
2661 else if (Current_File_System == "ntfs")
2662 maxFileSize = 256 * constTB; //256 TB
2663 else if (Current_File_System == "exfat")
2664 maxFileSize = 16 * constPB; //16 PB
2665 else if (Current_File_System == "ext3")
2666 maxFileSize = 2 * constTB; //2 TB
2667 else if (Current_File_System == "f2fs")
2668 maxFileSize = 3.94 * constTB; //3.94 TB
bigbiff7cb4c332014-11-26 20:36:07 -05002669 else
2670 maxFileSize = 100000000L;
2671 return maxFileSize - 1;
2672}
2673
bigbiffce8f83c2015-12-12 18:30:21 -05002674bool TWPartition::Flash_Image(PartitionSettings *part_settings) {
2675 string Restore_File_System, full_filename;
Ethan Yonker96af84a2015-01-05 14:58:36 -06002676
Ethan Yonkere080c1f2016-09-19 13:50:25 -05002677 full_filename = part_settings->Backup_Folder + "/" + Backup_FileName;
Ethan Yonker96af84a2015-01-05 14:58:36 -06002678
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002679 LOGINFO("Image filename is: %s\n", Backup_FileName.c_str());
bigbiffce8f83c2015-12-12 18:30:21 -05002680
2681 if (Backup_Method == BM_FILES) {
Ethan Yonker96af84a2015-01-05 14:58:36 -06002682 LOGERR("Cannot flash images to file systems\n");
2683 return false;
2684 } else if (!Can_Flash_Img) {
2685 LOGERR("Cannot flash images to partitions %s\n", Display_Name.c_str());
2686 return false;
2687 } else {
2688 if (!Find_Partition_Size()) {
2689 LOGERR("Unable to find partition size for '%s'\n", Mount_Point.c_str());
2690 return false;
2691 }
bigbiffce8f83c2015-12-12 18:30:21 -05002692 unsigned long long image_size = TWFunc::Get_File_Size(full_filename);
Ethan Yonker96af84a2015-01-05 14:58:36 -06002693 if (image_size > Size) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002694 LOGINFO("Size (%llu bytes) of image '%s' is larger than target device '%s' (%llu bytes)\n",
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002695 image_size, Backup_FileName.c_str(), Actual_Block_Device.c_str(), Size);
Ethan Yonker74db1572015-10-28 12:44:49 -05002696 gui_err("img_size_err=Size of image is larger than target device");
Ethan Yonker96af84a2015-01-05 14:58:36 -06002697 return false;
2698 }
bigbiffce8f83c2015-12-12 18:30:21 -05002699 if (Backup_Method == BM_DD) {
2700 if (!part_settings->adbbackup) {
2701 if (Is_Sparse_Image(full_filename)) {
2702 return Flash_Sparse_Image(full_filename);
2703 }
Ethan Yonker472f5062016-02-25 13:47:30 -06002704 }
bigbiffce8f83c2015-12-12 18:30:21 -05002705 unsigned long long file_size = (unsigned long long)(TWFunc::Get_File_Size(full_filename));
2706 return Raw_Read_Write(part_settings);
2707 } else if (Backup_Method == BM_FLASH_UTILS) {
2708 return Flash_Image_FI(full_filename, NULL);
Ethan Yonker472f5062016-02-25 13:47:30 -06002709 }
Ethan Yonker96af84a2015-01-05 14:58:36 -06002710 }
2711
2712 LOGERR("Unknown flash method for '%s'\n", Mount_Point.c_str());
2713 return false;
2714}
2715
Ethan Yonker472f5062016-02-25 13:47:30 -06002716bool TWPartition::Is_Sparse_Image(const string& Filename) {
HashBanged974bb2016-01-30 14:20:09 -05002717 uint32_t magic = 0;
2718 int fd = open(Filename.c_str(), O_RDONLY);
2719 if (fd < 0) {
2720 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Filename)(strerror(errno)));
2721 return false;
2722 }
bigbiffce8f83c2015-12-12 18:30:21 -05002723
HashBanged974bb2016-01-30 14:20:09 -05002724 if (read(fd, &magic, sizeof(magic)) != sizeof(magic)) {
2725 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Filename)(strerror(errno)));
2726 close(fd);
2727 return false;
2728 }
2729 close(fd);
Ethan Yonker472f5062016-02-25 13:47:30 -06002730 if (magic == SPARSE_HEADER_MAGIC)
2731 return true;
2732 return false;
2733}
2734
2735bool TWPartition::Flash_Sparse_Image(const string& Filename) {
2736 string Command;
2737
2738 gui_msg(Msg("flashing=Flashing {1}...")(Display_Name));
2739
2740 Command = "simg2img '" + Filename + "' '" + Actual_Block_Device + "'";
Ethan Yonker96af84a2015-01-05 14:58:36 -06002741 LOGINFO("Flash command: '%s'\n", Command.c_str());
2742 TWFunc::Exec_Cmd(Command);
2743 return true;
2744}
2745
Ethan Yonker472f5062016-02-25 13:47:30 -06002746bool TWPartition::Flash_Image_FI(const string& Filename, ProgressTracking *progress) {
Ethan Yonker96af84a2015-01-05 14:58:36 -06002747 string Command;
Ethan Yonker472f5062016-02-25 13:47:30 -06002748 unsigned long long file_size;
Ethan Yonker96af84a2015-01-05 14:58:36 -06002749
Ethan Yonker74db1572015-10-28 12:44:49 -05002750 gui_msg(Msg("flashing=Flashing {1}...")(Display_Name));
Ethan Yonker472f5062016-02-25 13:47:30 -06002751 if (progress) {
2752 file_size = (unsigned long long)(TWFunc::Get_File_Size(Filename));
2753 progress->SetPartitionSize(file_size);
2754 }
Ethan Yonker96af84a2015-01-05 14:58:36 -06002755 // Sometimes flash image doesn't like to flash due to the first 2KB matching, so we erase first to ensure that it flashes
2756 Command = "erase_image " + MTD_Name;
2757 LOGINFO("Erase command: '%s'\n", Command.c_str());
2758 TWFunc::Exec_Cmd(Command);
2759 Command = "flash_image " + MTD_Name + " '" + Filename + "'";
2760 LOGINFO("Flash command: '%s'\n", Command.c_str());
2761 TWFunc::Exec_Cmd(Command);
Ethan Yonker472f5062016-02-25 13:47:30 -06002762 if (progress)
2763 progress->UpdateSize(file_size);
Ethan Yonker96af84a2015-01-05 14:58:36 -06002764 return true;
2765}
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05002766
2767void TWPartition::Change_Mount_Read_Only(bool new_value) {
2768 Mount_Read_Only = new_value;
2769}
2770
bigbiffce8f83c2015-12-12 18:30:21 -05002771bool TWPartition::Is_Read_Only() {
2772 return Mount_Read_Only;
2773}
2774
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05002775int TWPartition::Check_Lifetime_Writes() {
2776 bool original_read_only = Mount_Read_Only;
2777 int ret = 1;
2778
2779 Mount_Read_Only = true;
2780 if (Mount(false)) {
2781 Find_Actual_Block_Device();
2782 string block = basename(Actual_Block_Device.c_str());
2783 string file = "/sys/fs/" + Current_File_System + "/" + block + "/lifetime_write_kbytes";
2784 string result;
2785 if (TWFunc::Path_Exists(file)) {
2786 if (TWFunc::read_file(file, result) != 0) {
2787 LOGINFO("Check_Lifetime_Writes of '%s' failed to read_file\n", file.c_str());
2788 } else {
2789 LOGINFO("Check_Lifetime_Writes result: '%s'\n", result.c_str());
2790 if (result == "0") {
2791 ret = 0;
2792 }
2793 }
2794 } else {
2795 LOGINFO("Check_Lifetime_Writes file does not exist '%s'\n", file.c_str());
2796 }
2797 UnMount(true);
2798 } else {
2799 LOGINFO("Check_Lifetime_Writes failed to mount '%s'\n", Mount_Point.c_str());
2800 }
2801 Mount_Read_Only = original_read_only;
2802 return ret;
2803}
Ethan Yonker66a19492015-12-10 10:19:45 -06002804
2805int TWPartition::Decrypt_Adopted() {
2806#ifdef TW_INCLUDE_CRYPTO
2807 int ret = 1;
2808 Is_Adopted_Storage = false;
2809 string Adopted_Key_File = "";
2810
2811 if (!Removable)
2812 return ret;
2813
2814 int fd = open(Alternate_Block_Device.c_str(), O_RDONLY);
2815 if (fd < 0) {
2816 LOGINFO("failed to open '%s'\n", Alternate_Block_Device.c_str());
2817 return ret;
2818 }
2819 char type_guid[80];
2820 char part_guid[80];
2821
2822 if (gpt_disk_get_partition_info(fd, 2, type_guid, part_guid) == 0) {
2823 LOGINFO("type: '%s'\n", type_guid);
2824 LOGINFO("part: '%s'\n", part_guid);
2825 Adopted_GUID = part_guid;
2826 LOGINFO("Adopted_GUID '%s'\n", Adopted_GUID.c_str());
2827 if (strcmp(type_guid, TWGptAndroidExpand) == 0) {
2828 LOGINFO("android_expand found\n");
2829 Adopted_Key_File = "/data/misc/vold/expand_";
2830 Adopted_Key_File += part_guid;
2831 Adopted_Key_File += ".key";
2832 if (TWFunc::Path_Exists(Adopted_Key_File)) {
2833 Is_Adopted_Storage = true;
2834 /* Until we find a use case for this, I think it is safe
2835 * to disable USB Mass Storage whenever adopted storage
2836 * is present.
2837 */
2838 LOGINFO("Detected adopted storage, disabling USB mass storage mode\n");
2839 DataManager::SetValue("tw_has_usb_storage", 0);
2840 }
2841 }
2842 }
2843
2844 if (Is_Adopted_Storage) {
2845 string Adopted_Block_Device = Alternate_Block_Device + "p2";
2846 if (!TWFunc::Path_Exists(Adopted_Block_Device)) {
2847 Adopted_Block_Device = Alternate_Block_Device + "2";
2848 if (!TWFunc::Path_Exists(Adopted_Block_Device)) {
2849 LOGINFO("Adopted block device does not exist\n");
2850 goto exit;
2851 }
2852 }
2853 LOGINFO("key file is '%s', block device '%s'\n", Adopted_Key_File.c_str(), Adopted_Block_Device.c_str());
2854 char crypto_blkdev[MAXPATHLEN];
2855 std::string thekey;
2856 int fdkey = open(Adopted_Key_File.c_str(), O_RDONLY);
2857 if (fdkey < 0) {
2858 LOGINFO("failed to open key file\n");
2859 goto exit;
2860 }
2861 char buf[512];
2862 ssize_t n;
2863 while ((n = read(fdkey, &buf[0], sizeof(buf))) > 0) {
2864 thekey.append(buf, n);
2865 }
2866 close(fdkey);
2867 unsigned char* key = (unsigned char*) thekey.data();
2868 cryptfs_revert_ext_volume(part_guid);
2869
2870 ret = cryptfs_setup_ext_volume(part_guid, Adopted_Block_Device.c_str(), key, thekey.size(), crypto_blkdev);
2871 if (ret == 0) {
2872 LOGINFO("adopted storage new block device: '%s'\n", crypto_blkdev);
2873 Decrypted_Block_Device = crypto_blkdev;
2874 Is_Decrypted = true;
2875 Is_Encrypted = true;
2876 Find_Actual_Block_Device();
Kjell Braden3126a112016-06-19 16:58:15 +00002877 if (!Mount_Storage_Retry(false)) {
Ethan Yonker66a19492015-12-10 10:19:45 -06002878 LOGERR("Failed to mount decrypted adopted storage device\n");
2879 Is_Decrypted = false;
2880 Is_Encrypted = false;
2881 cryptfs_revert_ext_volume(part_guid);
2882 ret = 1;
2883 } else {
Ethan Yonkerfcf3f242016-02-16 12:30:26 -06002884 UnMount(false);
2885 Has_Android_Secure = false;
2886 Symlink_Path = "";
2887 Symlink_Mount_Point = "";
2888 Backup_Name = Mount_Point.substr(1);
2889 Backup_Path = Mount_Point;
2890 TWPartition* sdext = PartitionManager.Find_Partition_By_Path("/sd-ext");
2891 if (sdext && sdext->Actual_Block_Device == Adopted_Block_Device) {
2892 LOGINFO("Removing /sd-ext from partition list due to adopted storage\n");
2893 PartitionManager.Remove_Partition_By_Path("/sd-ext");
2894 }
Ethan Yonker66a19492015-12-10 10:19:45 -06002895 Setup_Data_Media();
2896 Recreate_Media_Folder();
2897 Wipe_Available_in_GUI = true;
2898 Wipe_During_Factory_Reset = true;
2899 Can_Be_Backed_Up = true;
2900 Can_Encrypt_Backup = true;
2901 Use_Userdata_Encryption = true;
2902 Is_Storage = true;
2903 Storage_Name = "Adopted Storage";
2904 Is_SubPartition = true;
2905 SubPartition_Of = "/data";
2906 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
2907 DataManager::SetValue("tw_has_adopted_storage", 1);
2908 }
2909 } else {
2910 LOGERR("Failed to setup adopted storage decryption\n");
2911 }
2912 }
2913exit:
2914 return ret;
2915#else
2916 LOGINFO("Decrypt_Adopted: no crypto support\n");
2917 return 1;
2918#endif
2919}
2920
2921void TWPartition::Revert_Adopted() {
2922#ifdef TW_INCLUDE_CRYPTO
2923 if (!Adopted_GUID.empty()) {
2924 PartitionManager.Remove_MTP_Storage(Mount_Point);
2925 UnMount(false);
2926 cryptfs_revert_ext_volume(Adopted_GUID.c_str());
2927 Is_Adopted_Storage = false;
2928 Is_Encrypted = false;
2929 Is_Decrypted = false;
2930 Decrypted_Block_Device = "";
2931 Find_Actual_Block_Device();
2932 Wipe_During_Factory_Reset = false;
2933 Can_Be_Backed_Up = false;
2934 Can_Encrypt_Backup = false;
2935 Use_Userdata_Encryption = false;
2936 Is_SubPartition = false;
2937 SubPartition_Of = "";
2938 Has_Data_Media = false;
2939 Storage_Path = Mount_Point;
2940 if (!Symlink_Mount_Point.empty()) {
2941 TWPartition* Dat = PartitionManager.Find_Partition_By_Path("/data");
2942 if (Dat) {
2943 Dat->UnMount(false);
2944 Dat->Symlink_Mount_Point = Symlink_Mount_Point;
2945 }
2946 Symlink_Mount_Point = "";
2947 }
2948 }
2949#else
2950 LOGINFO("Revert_Adopted: no crypto support\n");
2951#endif
2952}