blob: 00b23b79fa666f6cea869b3a40dbd918a3ade4ce [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 Yonkerbc85b632015-08-09 12:48:14 -050061#else
62 #define CRYPT_FOOTER_OFFSET 0x4000
Ethan Yonker71413f42014-02-26 13:36:08 -060063#endif
Dees_Troy5bf43922012-09-07 16:07:55 -040064}
bigbiff bigbiffc49d7062013-10-11 20:28:00 -040065#ifdef HAVE_SELINUX
66#include "selinux/selinux.h"
Ethan Yonkerf27497f2014-02-09 11:48:33 -060067#include <selinux/label.h>
bigbiff bigbiffc49d7062013-10-11 20:28:00 -040068#endif
Dees Troy4159aed2014-02-28 17:24:43 +000069#ifdef HAVE_CAPABILITIES
70#include <sys/capability.h>
71#include <sys/xattr.h>
72#include <linux/xattr.h>
73#endif
HashBanged974bb2016-01-30 14:20:09 -050074#include <sparse_format.h>
Ethan Yonker472f5062016-02-25 13:47:30 -060075#include "progresstracking.hpp"
Dees_Troy51a0e822012-09-05 15:24:24 -040076
bigbiff bigbiff9c754052013-01-09 09:09:08 -050077using namespace std;
78
Dees_Troya95f55c2013-08-17 13:14:43 +000079extern struct selabel_handle *selinux_handle;
Ethan Yonker6277c792014-09-15 14:54:30 -050080extern bool datamedia;
Dees_Troya95f55c2013-08-17 13:14:43 +000081
Hashcode62bd9e02013-11-19 21:59:42 -080082struct flag_list {
83 const char *name;
84 unsigned flag;
85};
86
Matt Mower4ab42b12016-04-21 13:52:18 -050087const struct flag_list mount_flags[] = {
Hashcode62bd9e02013-11-19 21:59:42 -080088 { "noatime", MS_NOATIME },
89 { "noexec", MS_NOEXEC },
90 { "nosuid", MS_NOSUID },
91 { "nodev", MS_NODEV },
92 { "nodiratime", MS_NODIRATIME },
93 { "ro", MS_RDONLY },
94 { "rw", 0 },
95 { "remount", MS_REMOUNT },
96 { "bind", MS_BIND },
97 { "rec", MS_REC },
Dees Troyc4bc30e2014-02-03 15:04:19 +000098#ifdef MS_UNBINDABLE
Hashcode62bd9e02013-11-19 21:59:42 -080099 { "unbindable", MS_UNBINDABLE },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000100#endif
101#ifdef MS_PRIVATE
Hashcode62bd9e02013-11-19 21:59:42 -0800102 { "private", MS_PRIVATE },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000103#endif
104#ifdef MS_SLAVE
Hashcode62bd9e02013-11-19 21:59:42 -0800105 { "slave", MS_SLAVE },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000106#endif
107#ifdef MS_SHARED
Hashcode62bd9e02013-11-19 21:59:42 -0800108 { "shared", MS_SHARED },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000109#endif
Hashcode62bd9e02013-11-19 21:59:42 -0800110 { "sync", MS_SYNCHRONOUS },
111 { "defaults", 0 },
112 { 0, 0 },
113};
114
Matt Mower2416a502016-04-12 19:54:46 -0500115enum TW_FSTAB_FLAGS {
116 TWFLAG_DEFAULTS, // Retain position
117 TWFLAG_ANDSEC,
118 TWFLAG_BACKUP,
119 TWFLAG_BACKUPNAME,
120 TWFLAG_BLOCKSIZE,
121 TWFLAG_CANBEWIPED,
122 TWFLAG_CANENCRYPTBACKUP,
123 TWFLAG_DISPLAY,
124 TWFLAG_ENCRYPTABLE,
125 TWFLAG_FLASHIMG,
126 TWFLAG_FORCEENCRYPT,
127 TWFLAG_FSFLAGS,
128 TWFLAG_IGNOREBLKID,
129 TWFLAG_LENGTH,
130 TWFLAG_MOUNTTODECRYPT,
131 TWFLAG_REMOVABLE,
132 TWFLAG_RETAINLAYOUTVERSION,
133 TWFLAG_SETTINGSSTORAGE,
134 TWFLAG_STORAGE,
135 TWFLAG_STORAGENAME,
136 TWFLAG_SUBPARTITIONOF,
137 TWFLAG_SYMLINK,
138 TWFLAG_USERDATAENCRYPTBACKUP,
139 TWFLAG_USERMRF,
140 TWFLAG_WIPEDURINGFACTORYRESET,
141 TWFLAG_WIPEINGUI,
Ethan Yonker1b190162016-12-05 15:25:19 -0600142 TWFLAG_SLOTSELECT,
Matt Mower2416a502016-04-12 19:54:46 -0500143};
144
145/* Flags without a trailing '=' are considered dual format flags and can be
146 * written as either 'flagname' or 'flagname=', where the character following
147 * the '=' is Y,y,1 for true and false otherwise.
148 */
149const struct flag_list tw_flags[] = {
150 { "andsec", TWFLAG_ANDSEC },
151 { "backup", TWFLAG_BACKUP },
152 { "backupname=", TWFLAG_BACKUPNAME },
153 { "blocksize=", TWFLAG_BLOCKSIZE },
154 { "canbewiped", TWFLAG_CANBEWIPED },
155 { "canencryptbackup", TWFLAG_CANENCRYPTBACKUP },
156 { "defaults", TWFLAG_DEFAULTS },
157 { "display=", TWFLAG_DISPLAY },
158 { "encryptable=", TWFLAG_ENCRYPTABLE },
159 { "flashimg", TWFLAG_FLASHIMG },
160 { "forceencrypt=", TWFLAG_FORCEENCRYPT },
161 { "fsflags=", TWFLAG_FSFLAGS },
162 { "ignoreblkid", TWFLAG_IGNOREBLKID },
163 { "length=", TWFLAG_LENGTH },
164 { "mounttodecrypt", TWFLAG_MOUNTTODECRYPT },
165 { "removable", TWFLAG_REMOVABLE },
166 { "retainlayoutversion", TWFLAG_RETAINLAYOUTVERSION },
167 { "settingsstorage", TWFLAG_SETTINGSSTORAGE },
168 { "storage", TWFLAG_STORAGE },
169 { "storagename=", TWFLAG_STORAGENAME },
170 { "subpartitionof=", TWFLAG_SUBPARTITIONOF },
171 { "symlink=", TWFLAG_SYMLINK },
172 { "userdataencryptbackup", TWFLAG_USERDATAENCRYPTBACKUP },
173 { "usermrf", TWFLAG_USERMRF },
174 { "wipeduringfactoryreset", TWFLAG_WIPEDURINGFACTORYRESET },
175 { "wipeingui", TWFLAG_WIPEINGUI },
Ethan Yonker1b190162016-12-05 15:25:19 -0600176 { "slotselect", TWFLAG_SLOTSELECT },
Matt Mower2416a502016-04-12 19:54:46 -0500177 { 0, 0 },
178};
179
that9e0593e2014-10-08 00:01:24 +0200180TWPartition::TWPartition() {
Dees_Troy51a0e822012-09-05 15:24:24 -0400181 Can_Be_Mounted = false;
182 Can_Be_Wiped = false;
Dees_Troya13d74f2013-03-24 08:54:55 -0500183 Can_Be_Backed_Up = false;
Vojtech Bocek1dc30982013-08-30 21:49:30 +0200184 Use_Rm_Rf = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400185 Wipe_During_Factory_Reset = false;
186 Wipe_Available_in_GUI = false;
187 Is_SubPartition = false;
Dees_Troy2691f9d2012-09-24 11:15:49 -0400188 Has_SubPartition = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400189 SubPartition_Of = "";
190 Symlink_Path = "";
191 Symlink_Mount_Point = "";
192 Mount_Point = "";
Dees_Troye58d5262012-09-21 12:27:57 -0400193 Backup_Path = "";
Dees_Troy38bd7602012-09-14 13:33:53 -0400194 Actual_Block_Device = "";
195 Primary_Block_Device = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400196 Alternate_Block_Device = "";
197 Removable = false;
198 Is_Present = false;
199 Length = 0;
200 Size = 0;
201 Used = 0;
202 Free = 0;
203 Backup_Size = 0;
204 Can_Be_Encrypted = false;
205 Is_Encrypted = false;
206 Is_Decrypted = false;
Ethan Yonker253368a2014-11-25 15:00:52 -0600207 Mount_To_Decrypt = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400208 Decrypted_Block_Device = "";
209 Display_Name = "";
Dees_Troya13d74f2013-03-24 08:54:55 -0500210 Backup_Display_Name = "";
211 Storage_Name = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400212 Backup_Name = "";
Dees_Troy63c8df72012-09-10 14:02:05 -0400213 Backup_FileName = "";
Dees_Troy38bd7602012-09-14 13:33:53 -0400214 MTD_Name = "";
bigbiffce8f83c2015-12-12 18:30:21 -0500215 Backup_Method = BM_NONE;
Dees_Troy83bd4832013-05-04 12:39:56 +0000216 Can_Encrypt_Backup = false;
217 Use_Userdata_Encryption = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400218 Has_Data_Media = false;
Dees_Troye58d5262012-09-21 12:27:57 -0400219 Has_Android_Secure = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400220 Is_Storage = false;
Dees_Troya13d74f2013-03-24 08:54:55 -0500221 Is_Settings_Storage = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400222 Storage_Path = "";
223 Current_File_System = "";
224 Fstab_File_System = "";
Hashcode62bd9e02013-11-19 21:59:42 -0800225 Mount_Flags = 0;
226 Mount_Options = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400227 Format_Block_Size = 0;
Dees_Troy68cab492012-12-12 19:29:35 +0000228 Ignore_Blkid = false;
Dees_Troy16c2b312013-01-15 16:51:18 +0000229 Retain_Layout_Version = false;
Ethan Yonker253368a2014-11-25 15:00:52 -0600230 Crypto_Key_Location = "footer";
Ethan Yonker726a0202014-12-16 20:01:38 -0600231 MTP_Storage_ID = 0;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600232 Can_Flash_Img = false;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500233 Mount_Read_Only = false;
Ethan Yonker66a19492015-12-10 10:19:45 -0600234 Is_Adopted_Storage = false;
235 Adopted_GUID = "";
Ethan Yonker1b190162016-12-05 15:25:19 -0600236 SlotSelect = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400237}
238
239TWPartition::~TWPartition(void) {
240 // Do nothing
241}
242
Matt Mower2b2dd152016-04-26 11:24:08 -0500243bool TWPartition::Process_Fstab_Line(const char *fstab_line, bool Display_Error) {
244 char full_line[MAX_FSTAB_LINE_LENGTH];
Matt Mower2416a502016-04-12 19:54:46 -0500245 char twflags[MAX_FSTAB_LINE_LENGTH] = "";
Dees_Troy5bf43922012-09-07 16:07:55 -0400246 char* ptr;
Matt Mower2b2dd152016-04-26 11:24:08 -0500247 int line_len = strlen(fstab_line), index = 0, item_index = 0;
Dees_Troya13d74f2013-03-24 08:54:55 -0500248 bool skip = false;
Dees_Troy5bf43922012-09-07 16:07:55 -0400249
Matt Mower2b2dd152016-04-26 11:24:08 -0500250 strlcpy(full_line, fstab_line, sizeof(full_line));
Dees_Troy51127312012-09-08 13:08:49 -0400251 for (index = 0; index < line_len; index++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500252 if (full_line[index] == 34)
253 skip = !skip;
254 if (!skip && full_line[index] <= 32)
Dees_Troy5bf43922012-09-07 16:07:55 -0400255 full_line[index] = '\0';
Dees_Troy5bf43922012-09-07 16:07:55 -0400256 }
Dees_Troy7c2dec82012-09-26 09:49:14 -0400257 Mount_Point = full_line;
Dees_Troy2673cec2013-04-02 20:22:16 +0000258 LOGINFO("Processing '%s'\n", Mount_Point.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -0400259 Backup_Path = Mount_Point;
Dees_Troya13d74f2013-03-24 08:54:55 -0500260 Storage_Path = Mount_Point;
Dees_Troy70737fa2013-04-08 13:19:20 +0000261 Display_Name = full_line + 1;
262 Backup_Display_Name = Display_Name;
263 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400264 index = Mount_Point.size();
265 while (index < line_len) {
266 while (index < line_len && full_line[index] == '\0')
267 index++;
268 if (index >= line_len)
269 continue;
270 ptr = full_line + index;
271 if (item_index == 0) {
272 // File System
273 Fstab_File_System = ptr;
274 Current_File_System = ptr;
275 item_index++;
276 } else if (item_index == 1) {
277 // Primary Block Device
Dees_Troy38bd7602012-09-14 13:33:53 -0400278 if (Fstab_File_System == "mtd" || Fstab_File_System == "yaffs2") {
Dees_Troy094207a2012-09-26 12:00:39 -0400279 MTD_Name = ptr;
280 Find_MTD_Block_Device(MTD_Name);
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400281 } else if (Fstab_File_System == "bml") {
282 if (Mount_Point == "/boot")
283 MTD_Name = "boot";
284 else if (Mount_Point == "/recovery")
285 MTD_Name = "recovery";
286 Primary_Block_Device = ptr;
287 if (*ptr != '/')
Dees_Troy2673cec2013-04-02 20:22:16 +0000288 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 -0400289 } else if (*ptr != '/') {
Dees_Troy5bf43922012-09-07 16:07:55 -0400290 if (Display_Error)
Matt Mower2b2dd152016-04-26 11:24:08 -0500291 LOGERR("Invalid block device '%s' in fstab line '%s'", ptr, fstab_line);
Dees_Troy5bf43922012-09-07 16:07:55 -0400292 else
Matt Mower2b2dd152016-04-26 11:24:08 -0500293 LOGINFO("Invalid block device '%s' in fstab line '%s'", ptr, fstab_line);
Matt Mower72c87ce2016-04-26 14:34:56 -0500294 return false;
Dees_Troy38bd7602012-09-14 13:33:53 -0400295 } else {
296 Primary_Block_Device = ptr;
297 Find_Real_Block_Device(Primary_Block_Device, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400298 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400299 item_index++;
300 } else if (item_index > 1) {
301 if (*ptr == '/') {
302 // Alternate Block Device
303 Alternate_Block_Device = ptr;
304 Find_Real_Block_Device(Alternate_Block_Device, Display_Error);
305 } else if (strlen(ptr) > 7 && strncmp(ptr, "length=", 7) == 0) {
306 // Partition length
307 ptr += 7;
308 Length = atoi(ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400309 } else if (strlen(ptr) > 6 && strncmp(ptr, "flags=", 6) == 0) {
310 // Custom flags, save for later so that new values aren't overwritten by defaults
311 ptr += 6;
Matt Mower2416a502016-04-12 19:54:46 -0500312 strlcpy(twflags, ptr, sizeof(twflags));
Dees_Troy38bd7602012-09-14 13:33:53 -0400313 } else if (strlen(ptr) == 4 && (strncmp(ptr, "NULL", 4) == 0 || strncmp(ptr, "null", 4) == 0 || strncmp(ptr, "null", 4) == 0)) {
314 // Do nothing
Dees_Troy5bf43922012-09-07 16:07:55 -0400315 } else {
316 // Unhandled data
Matt Mower2b2dd152016-04-26 11:24:08 -0500317 LOGINFO("Unhandled fstab information '%s' in fstab line '%s'\n", ptr, fstab_line);
Dees_Troy5bf43922012-09-07 16:07:55 -0400318 }
319 }
320 while (index < line_len && full_line[index] != '\0')
321 index++;
322 }
323
324 if (!Is_File_System(Fstab_File_System) && !Is_Image(Fstab_File_System)) {
325 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000326 LOGERR("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400327 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000328 LOGINFO("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Matt Mower72c87ce2016-04-26 14:34:56 -0500329 return false;
Dees_Troy5bf43922012-09-07 16:07:55 -0400330 } else if (Is_File_System(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400331 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400332 Setup_File_System(Display_Error);
333 if (Mount_Point == "/system") {
334 Display_Name = "System";
Dees_Troya13d74f2013-03-24 08:54:55 -0500335 Backup_Display_Name = Display_Name;
336 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400337 Wipe_Available_in_GUI = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500338 Can_Be_Backed_Up = true;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500339 Mount_Read_Only = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400340 } else if (Mount_Point == "/data") {
341 Display_Name = "Data";
Dees_Troya13d74f2013-03-24 08:54:55 -0500342 Backup_Display_Name = Display_Name;
343 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400344 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400345 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500346 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000347 Can_Encrypt_Backup = true;
348 Use_Userdata_Encryption = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400349 } else if (Mount_Point == "/cache") {
350 Display_Name = "Cache";
Dees_Troya13d74f2013-03-24 08:54:55 -0500351 Backup_Display_Name = Display_Name;
352 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400353 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400354 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500355 Can_Be_Backed_Up = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400356 } else if (Mount_Point == "/datadata") {
Dees_Troy51127312012-09-08 13:08:49 -0400357 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400358 Display_Name = "DataData";
Dees_Troya13d74f2013-03-24 08:54:55 -0500359 Backup_Display_Name = Display_Name;
360 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400361 Is_SubPartition = true;
362 SubPartition_Of = "/data";
Dees_Troy5bf43922012-09-07 16:07:55 -0400363 DataManager::SetValue(TW_HAS_DATADATA, 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500364 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000365 Can_Encrypt_Backup = true;
366 Use_Userdata_Encryption = false; // This whole partition should be encrypted
Dees_Troy5bf43922012-09-07 16:07:55 -0400367 } else if (Mount_Point == "/sd-ext") {
Dees_Troy51127312012-09-08 13:08:49 -0400368 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400369 Display_Name = "SD-Ext";
Dees_Troya13d74f2013-03-24 08:54:55 -0500370 Backup_Display_Name = Display_Name;
371 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400372 Wipe_Available_in_GUI = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400373 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500374 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000375 Can_Encrypt_Backup = true;
376 Use_Userdata_Encryption = true;
Dees_Troy2c50e182012-09-26 20:05:28 -0400377 } else if (Mount_Point == "/boot") {
378 Display_Name = "Boot";
Dees_Troya13d74f2013-03-24 08:54:55 -0500379 Backup_Display_Name = Display_Name;
Dees_Troy2c50e182012-09-26 20:05:28 -0400380 DataManager::SetValue("tw_boot_is_mountable", 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500381 Can_Be_Backed_Up = true;
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500382 } else if (Mount_Point == "/vendor") {
383 Display_Name = "Vendor";
384 Backup_Display_Name = Display_Name;
385 Storage_Name = Display_Name;
386 Mount_Read_Only = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400387 }
388#ifdef TW_EXTERNAL_STORAGE_PATH
389 if (Mount_Point == EXPAND(TW_EXTERNAL_STORAGE_PATH)) {
390 Is_Storage = true;
391 Storage_Path = EXPAND(TW_EXTERNAL_STORAGE_PATH);
Dees_Troyc51f1f92012-09-20 15:32:13 -0400392 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500393 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400394#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000395 if (Mount_Point == "/sdcard" || Mount_Point == "/external_sd" || Mount_Point == "/external_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400396 Is_Storage = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400397 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500398 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400399#endif
Dees_Troyb05ddee2013-01-28 20:24:50 +0000400 }
Dees_Troy8170a922012-09-18 15:40:25 -0400401#ifdef TW_INTERNAL_STORAGE_PATH
402 if (Mount_Point == EXPAND(TW_INTERNAL_STORAGE_PATH)) {
403 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500404 Is_Settings_Storage = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400405 Storage_Path = EXPAND(TW_INTERNAL_STORAGE_PATH);
Dees_Troya13d74f2013-03-24 08:54:55 -0500406 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400407 }
408#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000409 if (Mount_Point == "/emmc" || Mount_Point == "/internal_sd" || Mount_Point == "/internal_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400410 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500411 Is_Settings_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500412 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400413 }
414#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400415 } else if (Is_Image(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400416 Find_Actual_Block_Device();
Ethan Yonker1b190162016-12-05 15:25:19 -0600417 Setup_Image();
Dees_Troya13d74f2013-03-24 08:54:55 -0500418 if (Mount_Point == "/boot") {
419 Display_Name = "Boot";
420 Backup_Display_Name = Display_Name;
421 Can_Be_Backed_Up = true;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600422 Can_Flash_Img = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500423 } else if (Mount_Point == "/recovery") {
424 Display_Name = "Recovery";
425 Backup_Display_Name = Display_Name;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600426 Can_Flash_Img = true;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500427 } else if (Mount_Point == "/system_image") {
428 Display_Name = "System Image";
429 Backup_Display_Name = Display_Name;
HashBanged974bb2016-01-30 14:20:09 -0500430 Can_Flash_Img = true;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500431 Can_Be_Backed_Up = true;
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500432 } else if (Mount_Point == "/vendor_image") {
433 Display_Name = "Vendor Image";
434 Backup_Display_Name = Display_Name;
HashBanged974bb2016-01-30 14:20:09 -0500435 Can_Flash_Img = true;
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500436 Can_Be_Backed_Up = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500437 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400438 }
439
Matt Mower2416a502016-04-12 19:54:46 -0500440 // Process TWRP fstab flags
441 if (strlen(twflags) > 0) {
442 string Prev_Display_Name = Display_Name;
443 string Prev_Storage_Name = Storage_Name;
444 string Prev_Backup_Display_Name = Backup_Display_Name;
445 Display_Name = "";
446 Storage_Name = "";
447 Backup_Display_Name = "";
448
449 Process_TW_Flags(twflags, Display_Error);
450
451 bool has_display_name = !Display_Name.empty();
452 bool has_storage_name = !Storage_Name.empty();
453 bool has_backup_name = !Backup_Display_Name.empty();
454 if (!has_display_name) Display_Name = Prev_Display_Name;
455 if (!has_storage_name) Storage_Name = Prev_Storage_Name;
456 if (!has_backup_name) Backup_Display_Name = Prev_Backup_Display_Name;
457
458 if (has_display_name && !has_storage_name)
459 Storage_Name = Display_Name;
460 if (!has_display_name && has_storage_name)
461 Display_Name = Storage_Name;
462 if (has_display_name && !has_backup_name && Backup_Display_Name != "Android Secure")
463 Backup_Display_Name = Display_Name;
464 if (!has_display_name && has_backup_name)
465 Display_Name = Backup_Display_Name;
466 }
Dees_Troy51127312012-09-08 13:08:49 -0400467 return true;
468}
469
Matt Mower72c87ce2016-04-26 14:34:56 -0500470void TWPartition::Partition_Post_Processing(bool Display_Error) {
471 if (Mount_Point == "/data")
472 Setup_Data_Partition(Display_Error);
473 else if (Mount_Point == "/cache")
474 Setup_Cache_Partition(Display_Error);
475}
476
477void TWPartition::Setup_Data_Partition(bool Display_Error) {
478 if (Mount_Point != "/data")
479 return;
480
481 // Ensure /data is not mounted as tmpfs for qcom hardware decrypt
482 UnMount(false);
483
484#ifdef TW_INCLUDE_CRYPTO
485 if (datamedia)
486 Setup_Data_Media();
487 Can_Be_Encrypted = true;
488 char crypto_blkdev[255];
489 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
490 if (strcmp(crypto_blkdev, "error") != 0) {
491 DataManager::SetValue(TW_IS_DECRYPTED, 1);
492 Is_Encrypted = true;
493 Is_Decrypted = true;
494 Decrypted_Block_Device = crypto_blkdev;
495 LOGINFO("Data already decrypted, new block device: '%s'\n", crypto_blkdev);
496 } else if (!Mount(false)) {
497 if (Is_Present) {
498 set_partition_data(Actual_Block_Device.c_str(), Crypto_Key_Location.c_str(), Fstab_File_System.c_str());
499 if (cryptfs_check_footer() == 0) {
500 Is_Encrypted = true;
501 Is_Decrypted = false;
502 Can_Be_Mounted = false;
503 Current_File_System = "emmc";
Ethan Yonker1b190162016-12-05 15:25:19 -0600504 Setup_Image();
Matt Mower72c87ce2016-04-26 14:34:56 -0500505 DataManager::SetValue(TW_IS_ENCRYPTED, 1);
506 DataManager::SetValue(TW_CRYPTO_PWTYPE, cryptfs_get_password_type());
507 DataManager::SetValue(TW_CRYPTO_PASSWORD, "");
508 DataManager::SetValue("tw_crypto_display", "");
509 } else {
510 gui_err("mount_data_footer=Could not mount /data and unable to find crypto footer.");
511 }
512 } else {
513 LOGERR("Primary block device '%s' for mount point '%s' is not present!\n", Primary_Block_Device.c_str(), Mount_Point.c_str());
514 }
515 } else {
516 // Filesystem is not encrypted and the mount succeeded, so return to
517 // the original unmounted state
518 UnMount(false);
519 }
520 if (datamedia && (!Is_Encrypted || (Is_Encrypted && Is_Decrypted))) {
521 Setup_Data_Media();
522 Recreate_Media_Folder();
523 }
524#else
525 if (datamedia) {
526 Setup_Data_Media();
527 Recreate_Media_Folder();
528 }
529#endif
530}
531
532void TWPartition::Setup_Cache_Partition(bool Display_Error __unused) {
533 if (Mount_Point != "/cache")
534 return;
535
536 if (!Mount(true))
537 return;
538
539 if (!TWFunc::Path_Exists("/cache/recovery/.")) {
540 LOGINFO("Recreating /cache/recovery folder\n");
541 if (mkdir("/cache/recovery", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0)
542 LOGERR("Could not create /cache/recovery\n");
543 }
544}
545
Matt Mower4ab42b12016-04-21 13:52:18 -0500546void TWPartition::Process_FS_Flags(const char *str) {
547 char *options = strdup(str);
548 char *ptr, *savep;
Hashcode62bd9e02013-11-19 21:59:42 -0800549
Matt Mower4ab42b12016-04-21 13:52:18 -0500550 Mount_Options = "";
Hashcode62bd9e02013-11-19 21:59:42 -0800551
Matt Mower4ab42b12016-04-21 13:52:18 -0500552 // Avoid issues with potentially nested strtok by using strtok_r
553 ptr = strtok_r(options, ",", &savep);
554 while (ptr) {
555 const struct flag_list* mount_flag = mount_flags;
556
557 for (; mount_flag->name; mount_flag++) {
558 // mount_flags are never postfixed by '=',
559 // so only match identical strings (including length)
560 if (strcmp(ptr, mount_flag->name) == 0) {
561 Mount_Flags |= mount_flag->flag;
Hashcode62bd9e02013-11-19 21:59:42 -0800562 break;
563 }
564 }
565
Matt Mower4ab42b12016-04-21 13:52:18 -0500566 if (mount_flag->flag == MS_RDONLY)
567 Mount_Read_Only = true;
Hashcode62bd9e02013-11-19 21:59:42 -0800568
Matt Mower4ab42b12016-04-21 13:52:18 -0500569 if (mount_flag->name != 0) {
570 if (!Mount_Options.empty())
571 Mount_Options += ",";
572 Mount_Options += mount_flag->name;
573 } else {
574 LOGINFO("Unhandled mount flag: '%s'\n", ptr);
575 }
576
577 ptr = strtok_r(NULL, ",", &savep);
578 }
579 free(options);
Hashcode62bd9e02013-11-19 21:59:42 -0800580}
581
Matt Mower2416a502016-04-12 19:54:46 -0500582void TWPartition::Apply_TW_Flag(const unsigned flag, const char* str, const bool val) {
583 switch (flag) {
584 case TWFLAG_ANDSEC:
585 Has_Android_Secure = val;
586 break;
587 case TWFLAG_BACKUP:
588 Can_Be_Backed_Up = val;
589 break;
590 case TWFLAG_BACKUPNAME:
591 Backup_Display_Name = str;
592 break;
593 case TWFLAG_BLOCKSIZE:
594 Format_Block_Size = (unsigned long)(atol(str));
595 break;
596 case TWFLAG_CANBEWIPED:
597 Can_Be_Wiped = val;
598 break;
599 case TWFLAG_CANENCRYPTBACKUP:
600 Can_Encrypt_Backup = val;
601 break;
602 case TWFLAG_DEFAULTS:
603 // Do nothing
604 break;
605 case TWFLAG_DISPLAY:
606 Display_Name = str;
607 break;
608 case TWFLAG_ENCRYPTABLE:
609 case TWFLAG_FORCEENCRYPT:
610 Crypto_Key_Location = str;
611 break;
612 case TWFLAG_FLASHIMG:
613 Can_Flash_Img = val;
614 break;
615 case TWFLAG_FSFLAGS:
Matt Mower4ab42b12016-04-21 13:52:18 -0500616 Process_FS_Flags(str);
Matt Mower2416a502016-04-12 19:54:46 -0500617 break;
618 case TWFLAG_IGNOREBLKID:
619 Ignore_Blkid = val;
620 break;
621 case TWFLAG_LENGTH:
622 Length = atoi(str);
623 break;
624 case TWFLAG_MOUNTTODECRYPT:
625 Mount_To_Decrypt = val;
626 break;
627 case TWFLAG_REMOVABLE:
628 Removable = val;
629 break;
630 case TWFLAG_RETAINLAYOUTVERSION:
631 Retain_Layout_Version = val;
632 break;
633 case TWFLAG_SETTINGSSTORAGE:
634 Is_Settings_Storage = val;
635 if (Is_Settings_Storage)
636 Is_Storage = true;
637 break;
638 case TWFLAG_STORAGE:
639 Is_Storage = val;
640 break;
641 case TWFLAG_STORAGENAME:
642 Storage_Name = str;
643 break;
644 case TWFLAG_SUBPARTITIONOF:
645 Is_SubPartition = true;
646 SubPartition_Of = str;
647 break;
648 case TWFLAG_SYMLINK:
649 Symlink_Path = str;
650 break;
651 case TWFLAG_USERDATAENCRYPTBACKUP:
652 Use_Userdata_Encryption = val;
653 if (Use_Userdata_Encryption)
654 Can_Encrypt_Backup = true;
655 break;
656 case TWFLAG_USERMRF:
657 Use_Rm_Rf = val;
658 break;
659 case TWFLAG_WIPEDURINGFACTORYRESET:
660 Wipe_During_Factory_Reset = val;
661 if (Wipe_During_Factory_Reset) {
662 Can_Be_Wiped = true;
663 Wipe_Available_in_GUI = true;
664 }
665 break;
666 case TWFLAG_WIPEINGUI:
667 Wipe_Available_in_GUI = val;
668 if (Wipe_Available_in_GUI)
669 Can_Be_Wiped = true;
670 break;
Ethan Yonker1b190162016-12-05 15:25:19 -0600671 case TWFLAG_SLOTSELECT:
672 SlotSelect = true;
673 break;
Matt Mower2416a502016-04-12 19:54:46 -0500674 default:
675 // Should not get here
676 LOGINFO("Flag identified for processing, but later unmatched: %i\n", flag);
677 break;
678 }
679}
Dees_Troy51127312012-09-08 13:08:49 -0400680
Matt Mower2416a502016-04-12 19:54:46 -0500681void TWPartition::Process_TW_Flags(char *flags, bool Display_Error) {
682 char separator[2] = {'\n', 0};
683 char *ptr, *savep;
684
685 // Semicolons within double-quotes are not forbidden, so replace
686 // only the semicolons intended as separators with '\n' for strtok
687 for (unsigned i = 0, skip = 0; i < strlen(flags); i++) {
688 if (flags[i] == '\"')
Dees_Troya13d74f2013-03-24 08:54:55 -0500689 skip = !skip;
Matt Mower2416a502016-04-12 19:54:46 -0500690 if (!skip && flags[i] == ';')
691 flags[i] = separator[0];
Dees_Troy51127312012-09-08 13:08:49 -0400692 }
693
Matt Mower2416a502016-04-12 19:54:46 -0500694 // Avoid issues with potentially nested strtok by using strtok_r
695 ptr = strtok_r(flags, separator, &savep);
696 while (ptr) {
697 int ptr_len = strlen(ptr);
698 const struct flag_list* tw_flag = tw_flags;
Hashcode62bd9e02013-11-19 21:59:42 -0800699
Matt Mower2416a502016-04-12 19:54:46 -0500700 for (; tw_flag->name; tw_flag++) {
701 int flag_len = strlen(tw_flag->name);
702
703 if (strncmp(ptr, tw_flag->name, flag_len) == 0) {
704 bool flag_val = false;
705
706 if (ptr_len > flag_len && (tw_flag->name)[flag_len-1] != '='
707 && ptr[flag_len] != '=') {
708 // Handle flags with same starting string
709 // (e.g. backup and backupname)
710 continue;
711 } else if (ptr_len > flag_len && ptr[flag_len] == '=') {
712 // Handle flags with dual format: Part 1
713 // (e.g. backup and backup=y. backup=y handled here)
714 ptr += flag_len + 1;
715 TWFunc::Strip_Quotes(ptr);
716 // Skip flags with empty argument
717 // (e.g. backup=)
718 if (strlen(ptr) == 0) {
719 LOGINFO("Flag missing argument or should not include '=': %s=\n", tw_flag->name);
720 break;
721 }
722 flag_val = strchr("yY1", *ptr) != NULL;
723 } else if (ptr_len == flag_len
724 && (tw_flag->name)[flag_len-1] == '=') {
725 // Skip flags missing argument after =
726 // (e.g. backupname=)
727 LOGINFO("Flag missing argument: %s\n", tw_flag->name);
728 break;
729 } else if (ptr_len > flag_len
730 && (tw_flag->name)[flag_len-1] == '=') {
731 // Handle arguments to flags
732 // (e.g. backupname="My Stuff")
733 ptr += flag_len;
734 TWFunc::Strip_Quotes(ptr);
735 // Skip flags with empty argument
736 // (e.g. backupname="")
737 if (strlen(ptr) == 0) {
738 LOGINFO("Flag missing argument: %s\n", tw_flag->name);
739 break;
740 }
741 } else if (ptr_len == flag_len) {
742 // Handle flags with dual format: Part 2
743 // (e.g. backup and backup=y. backup handled here)
744 flag_val = true;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600745 } else {
Matt Mower2416a502016-04-12 19:54:46 -0500746 LOGINFO("Flag matched, but could not be processed: %s\n", ptr);
747 break;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600748 }
Matt Mower2416a502016-04-12 19:54:46 -0500749
750 Apply_TW_Flag(tw_flag->flag, ptr, flag_val);
751 break;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600752 }
Matt Mower2416a502016-04-12 19:54:46 -0500753 }
754 if (tw_flag->name == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400755 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000756 LOGERR("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400757 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000758 LOGINFO("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400759 }
Matt Mower2416a502016-04-12 19:54:46 -0500760 ptr = strtok_r(NULL, separator, &savep);
Dees_Troy51127312012-09-08 13:08:49 -0400761 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400762}
763
Dees_Troy5bf43922012-09-07 16:07:55 -0400764bool TWPartition::Is_File_System(string File_System) {
765 if (File_System == "ext2" ||
Dees_Troy63c8df72012-09-10 14:02:05 -0400766 File_System == "ext3" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400767 File_System == "ext4" ||
768 File_System == "vfat" ||
769 File_System == "ntfs" ||
770 File_System == "yaffs2" ||
bigbiff bigbiff3e146522012-11-14 14:32:59 -0500771 File_System == "exfat" ||
Dees_Troye5017042013-08-29 16:38:55 +0000772 File_System == "f2fs" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400773 File_System == "auto")
774 return true;
775 else
776 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400777}
778
Dees_Troy5bf43922012-09-07 16:07:55 -0400779bool TWPartition::Is_Image(string File_System) {
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400780 if (File_System == "emmc" || File_System == "mtd" || File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -0400781 return true;
782 else
783 return false;
784}
785
Dees_Troy51127312012-09-08 13:08:49 -0400786bool TWPartition::Make_Dir(string Path, bool Display_Error) {
Ethan Yonker93ac7a02016-11-07 22:05:58 -0600787 if (TWFunc::Get_D_Type_From_Stat(Path) != S_IFDIR)
788 unlink(Path.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400789 if (!TWFunc::Path_Exists(Path)) {
Dees_Troy51127312012-09-08 13:08:49 -0400790 if (mkdir(Path.c_str(), 0777) == -1) {
791 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -0500792 gui_msg(Msg(msg::kError, "create_folder_strerr=Can not create '{1}' folder ({2}).")(Path)(strerror(errno)));
Dees_Troy51127312012-09-08 13:08:49 -0400793 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000794 LOGINFO("Can not create '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400795 return false;
796 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000797 LOGINFO("Created '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400798 return true;
799 }
800 }
801 return true;
802}
803
Dees_Troy5bf43922012-09-07 16:07:55 -0400804void TWPartition::Setup_File_System(bool Display_Error) {
805 struct statfs st;
806
807 Can_Be_Mounted = true;
808 Can_Be_Wiped = true;
809
Dees_Troy5bf43922012-09-07 16:07:55 -0400810 // Make the mount point folder if it doesn't exist
Dees_Troy51127312012-09-08 13:08:49 -0400811 Make_Dir(Mount_Point, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400812 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
813 Backup_Name = Display_Name;
bigbiffce8f83c2015-12-12 18:30:21 -0500814 Backup_Method = BM_FILES;
Dees_Troy5bf43922012-09-07 16:07:55 -0400815}
816
Ethan Yonker1b190162016-12-05 15:25:19 -0600817void TWPartition::Setup_Image() {
Dees_Troy5bf43922012-09-07 16:07:55 -0400818 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
819 Backup_Name = Display_Name;
Gary Peck82599a82012-11-21 16:23:12 -0800820 if (Current_File_System == "emmc")
bigbiffce8f83c2015-12-12 18:30:21 -0500821 Backup_Method = BM_DD;
Gary Peck82599a82012-11-21 16:23:12 -0800822 else if (Current_File_System == "mtd" || Current_File_System == "bml")
bigbiffce8f83c2015-12-12 18:30:21 -0500823 Backup_Method = BM_FLASH_UTILS;
Dees_Troy5bf43922012-09-07 16:07:55 -0400824 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000825 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 -0400826}
827
Dees_Troye58d5262012-09-21 12:27:57 -0400828void TWPartition::Setup_AndSec(void) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500829 Backup_Display_Name = "Android Secure";
Dees_Troye58d5262012-09-21 12:27:57 -0400830 Backup_Name = "and-sec";
Dees_Troya13d74f2013-03-24 08:54:55 -0500831 Can_Be_Backed_Up = true;
Dees_Troye58d5262012-09-21 12:27:57 -0400832 Has_Android_Secure = true;
833 Symlink_Path = Mount_Point + "/.android_secure";
834 Symlink_Mount_Point = "/and-sec";
835 Backup_Path = Symlink_Mount_Point;
836 Make_Dir("/and-sec", true);
837 Recreate_AndSec_Folder();
Kjell Braden3126a112016-06-19 16:58:15 +0000838 Mount_Storage_Retry(true);
Dees_Troye58d5262012-09-21 12:27:57 -0400839}
840
that9e0593e2014-10-08 00:01:24 +0200841void TWPartition::Setup_Data_Media() {
Ethan Yonker6277c792014-09-15 14:54:30 -0500842 LOGINFO("Setting up '%s' as data/media emulated storage.\n", Mount_Point.c_str());
Ethan Yonker66a19492015-12-10 10:19:45 -0600843 if (Storage_Name.empty() || Storage_Name == "Data")
844 Storage_Name = "Internal Storage";
Ethan Yonker6277c792014-09-15 14:54:30 -0500845 Has_Data_Media = true;
846 Is_Storage = true;
Ethan Yonker66a19492015-12-10 10:19:45 -0600847 Storage_Path = Mount_Point + "/media";
Ethan Yonker6277c792014-09-15 14:54:30 -0500848 Symlink_Path = Storage_Path;
Ethan Yonker66a19492015-12-10 10:19:45 -0600849 if (Mount_Point == "/data") {
850 Is_Settings_Storage = true;
851 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
852 Make_Dir("/emmc", false);
853 Symlink_Mount_Point = "/emmc";
854 } else {
855 Make_Dir("/sdcard", false);
856 Symlink_Mount_Point = "/sdcard";
857 }
858 if (Mount(false) && TWFunc::Path_Exists(Mount_Point + "/media/0")) {
859 Storage_Path = Mount_Point + "/media/0";
860 Symlink_Path = Storage_Path;
861 DataManager::SetValue(TW_INTERNAL_PATH, Mount_Point + "/media/0");
862 UnMount(true);
863 }
864 DataManager::SetValue("tw_has_internal", 1);
865 DataManager::SetValue("tw_has_data_media", 1);
Ethan Yonker3fdcda42016-11-30 12:29:37 -0600866 backup_exclusions.add_absolute_dir("/data/data/com.google.android.music/files");
867 backup_exclusions.add_absolute_dir(Mount_Point + "/misc/vold");
868 wipe_exclusions.add_absolute_dir(Mount_Point + "/misc/vold");
869 backup_exclusions.add_absolute_dir(Mount_Point + "/.layout_version");
870 wipe_exclusions.add_absolute_dir(Mount_Point + "/.layout_version");
871 backup_exclusions.add_absolute_dir(Mount_Point + "/system/storage.xml");
872 wipe_exclusions.add_absolute_dir(Mount_Point + "/system/storage.xml");
Ethan Yonker6277c792014-09-15 14:54:30 -0500873 } else {
Ethan Yonker66a19492015-12-10 10:19:45 -0600874 if (Mount(true) && TWFunc::Path_Exists(Mount_Point + "/media/0")) {
875 Storage_Path = Mount_Point + "/media/0";
876 Symlink_Path = Storage_Path;
877 UnMount(true);
878 }
Ethan Yonker6277c792014-09-15 14:54:30 -0500879 }
Ethan Yonker3fdcda42016-11-30 12:29:37 -0600880 backup_exclusions.add_absolute_dir(Mount_Point + "/media");
881 wipe_exclusions.add_absolute_dir(Mount_Point + "/media");
Ethan Yonker6277c792014-09-15 14:54:30 -0500882}
883
Dees_Troy5bf43922012-09-07 16:07:55 -0400884void TWPartition::Find_Real_Block_Device(string& Block, bool Display_Error) {
885 char device[512], realDevice[512];
886
887 strcpy(device, Block.c_str());
888 memset(realDevice, 0, sizeof(realDevice));
889 while (readlink(device, realDevice, sizeof(realDevice)) > 0)
890 {
891 strcpy(device, realDevice);
892 memset(realDevice, 0, sizeof(realDevice));
893 }
894
895 if (device[0] != '/') {
896 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000897 LOGERR("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400898 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000899 LOGINFO("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400900 return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400901 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400902 Block = device;
903 return;
904 }
905}
906
Kjell Braden3126a112016-06-19 16:58:15 +0000907bool TWPartition::Mount_Storage_Retry(bool Display_Error) {
Dees_Troy8e337f32012-10-13 22:07:49 -0400908 // On some devices, storage doesn't want to mount right away, retry and sleep
Kjell Braden3126a112016-06-19 16:58:15 +0000909 if (!Mount(Display_Error)) {
Dees_Troy8e337f32012-10-13 22:07:49 -0400910 int retry_count = 5;
911 while (retry_count > 0 && !Mount(false)) {
912 usleep(500000);
913 retry_count--;
914 }
Kjell Braden3126a112016-06-19 16:58:15 +0000915 return Mount(Display_Error);
Dees_Troy8e337f32012-10-13 22:07:49 -0400916 }
Kjell Braden3126a112016-06-19 16:58:15 +0000917 return true;
Dees_Troy8e337f32012-10-13 22:07:49 -0400918}
919
Dees_Troy38bd7602012-09-14 13:33:53 -0400920bool TWPartition::Find_MTD_Block_Device(string MTD_Name) {
921 FILE *fp = NULL;
922 char line[255];
923
924 fp = fopen("/proc/mtd", "rt");
925 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000926 LOGERR("Device does not support /proc/mtd\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400927 return false;
928 }
929
930 while (fgets(line, sizeof(line), fp) != NULL)
931 {
932 char device[32], label[32];
933 unsigned long size = 0;
934 char* fstype = NULL;
935 int deviceId;
936
937 sscanf(line, "%s %lx %*s %*c%s", device, &size, label);
938
939 // Skip header and blank lines
940 if ((strcmp(device, "dev:") == 0) || (strlen(line) < 8))
941 continue;
942
943 // Strip off the trailing " from the label
944 label[strlen(label)-1] = '\0';
945
946 if (strcmp(label, MTD_Name.c_str()) == 0) {
947 // We found our device
948 // Strip off the trailing : from the device
949 device[strlen(device)-1] = '\0';
950 if (sscanf(device,"mtd%d", &deviceId) == 1) {
951 sprintf(device, "/dev/block/mtdblock%d", deviceId);
952 Primary_Block_Device = device;
Dees_Troy76543db2013-06-19 16:24:30 +0000953 fclose(fp);
954 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -0400955 }
956 }
957 }
958 fclose(fp);
959
960 return false;
961}
962
Dees_Troy51127312012-09-08 13:08:49 -0400963bool TWPartition::Get_Size_Via_statfs(bool Display_Error) {
964 struct statfs st;
965 string Local_Path = Mount_Point + "/.";
966
967 if (!Mount(Display_Error))
968 return false;
969
970 if (statfs(Local_Path.c_str(), &st) != 0) {
971 if (!Removable) {
972 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000973 LOGERR("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400974 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000975 LOGINFO("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400976 }
977 return false;
978 }
979 Size = (st.f_blocks * st.f_bsize);
980 Used = ((st.f_blocks - st.f_bfree) * st.f_bsize);
981 Free = (st.f_bfree * st.f_bsize);
982 Backup_Size = Used;
983 return true;
984}
985
986bool TWPartition::Get_Size_Via_df(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400987 FILE* fp;
988 char command[255], line[512];
989 int include_block = 1;
990 unsigned int min_len;
991
992 if (!Mount(Display_Error))
993 return false;
994
Dees_Troy38bd7602012-09-14 13:33:53 -0400995 min_len = Actual_Block_Device.size() + 2;
Dees_Troy51127312012-09-08 13:08:49 -0400996 sprintf(command, "df %s > /tmp/dfoutput.txt", Mount_Point.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +0200997 TWFunc::Exec_Cmd(command);
Dees_Troy51127312012-09-08 13:08:49 -0400998 fp = fopen("/tmp/dfoutput.txt", "rt");
999 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001000 LOGINFO("Unable to open /tmp/dfoutput.txt.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001001 return false;
Dees_Troy51127312012-09-08 13:08:49 -04001002 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001003
1004 while (fgets(line, sizeof(line), fp) != NULL)
1005 {
1006 unsigned long blocks, used, available;
1007 char device[64];
1008 char tmpString[64];
1009
1010 if (strncmp(line, "Filesystem", 10) == 0)
1011 continue;
1012 if (strlen(line) < min_len) {
1013 include_block = 0;
1014 continue;
1015 }
1016 if (include_block) {
1017 sscanf(line, "%s %lu %lu %lu", device, &blocks, &used, &available);
1018 } else {
1019 // The device block string is so long that the df information is on the next line
1020 int space_count = 0;
Dees_Troye58d5262012-09-21 12:27:57 -04001021 sprintf(tmpString, "/dev/block/%s", Actual_Block_Device.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001022 while (tmpString[space_count] == 32)
1023 space_count++;
1024 sscanf(line + space_count, "%lu %lu %lu", &blocks, &used, &available);
1025 }
1026
1027 // Adjust block size to byte size
1028 Size = blocks * 1024ULL;
1029 Used = used * 1024ULL;
1030 Free = available * 1024ULL;
1031 Backup_Size = Used;
1032 }
1033 fclose(fp);
1034 return true;
1035}
1036
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001037unsigned long long TWPartition::IOCTL_Get_Block_Size() {
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001038 Find_Actual_Block_Device();
Ethan Yonker483e9f42016-01-11 22:21:18 -06001039
1040 return TWFunc::IOCTL_Get_Block_Size(Actual_Block_Device.c_str());
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001041}
1042
Dees_Troy5bf43922012-09-07 16:07:55 -04001043bool TWPartition::Find_Partition_Size(void) {
1044 FILE* fp;
1045 char line[512];
1046 string tmpdevice;
1047
igoriok87e3d932013-01-31 21:03:53 +02001048 fp = fopen("/proc/dumchar_info", "rt");
1049 if (fp != NULL) {
1050 while (fgets(line, sizeof(line), fp) != NULL)
1051 {
1052 char label[32], device[32];
1053 unsigned long size = 0;
1054
thatd43bf2d2014-09-21 23:13:02 +02001055 sscanf(line, "%s %lx %*x %*u %s", label, &size, device);
igoriok87e3d932013-01-31 21:03:53 +02001056
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05001057 // Skip header, annotation and blank lines
igoriok87e3d932013-01-31 21:03:53 +02001058 if ((strncmp(device, "/dev/", 5) != 0) || (strlen(line) < 8))
1059 continue;
1060
1061 tmpdevice = "/dev/";
1062 tmpdevice += label;
1063 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
1064 Size = size;
1065 fclose(fp);
1066 return true;
1067 }
1068 }
1069 }
1070
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001071 unsigned long long ioctl_size = IOCTL_Get_Block_Size();
1072 if (ioctl_size) {
1073 Size = ioctl_size;
1074 return true;
1075 }
1076
Dees_Troy5bf43922012-09-07 16:07:55 -04001077 // In this case, we'll first get the partitions we care about (with labels)
1078 fp = fopen("/proc/partitions", "rt");
1079 if (fp == NULL)
1080 return false;
1081
1082 while (fgets(line, sizeof(line), fp) != NULL)
1083 {
1084 unsigned long major, minor, blocks;
1085 char device[512];
1086 char tmpString[64];
1087
Dees_Troy63c8df72012-09-10 14:02:05 -04001088 if (strlen(line) < 7 || line[0] == 'm') continue;
Dees_Troy5bf43922012-09-07 16:07:55 -04001089 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
1090
1091 tmpdevice = "/dev/block/";
1092 tmpdevice += device;
Dees_Troy38bd7602012-09-14 13:33:53 -04001093 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001094 // Adjust block size to byte size
1095 Size = blocks * 1024ULL;
1096 fclose(fp);
1097 return true;
1098 }
1099 }
1100 fclose(fp);
1101 return false;
1102}
1103
Dees_Troy5bf43922012-09-07 16:07:55 -04001104bool TWPartition::Is_Mounted(void) {
1105 if (!Can_Be_Mounted)
1106 return false;
1107
1108 struct stat st1, st2;
1109 string test_path;
1110
1111 // Check to see if the mount point directory exists
1112 test_path = Mount_Point + "/.";
1113 if (stat(test_path.c_str(), &st1) != 0) return false;
1114
1115 // Check to see if the directory above the mount point exists
1116 test_path = Mount_Point + "/../.";
1117 if (stat(test_path.c_str(), &st2) != 0) return false;
1118
1119 // Compare the device IDs -- if they match then we're (probably) using tmpfs instead of an actual device
1120 int ret = (st1.st_dev != st2.st_dev) ? true : false;
1121
1122 return ret;
1123}
1124
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05001125bool TWPartition::Is_File_System_Writable(void) {
1126 if (!Is_File_System(Current_File_System) || !Is_Mounted())
1127 return false;
1128
1129 string test_path = Mount_Point + "/.";
1130 return (access(test_path.c_str(), W_OK) == 0);
1131}
1132
Dees_Troy5bf43922012-09-07 16:07:55 -04001133bool TWPartition::Mount(bool Display_Error) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001134 int exfat_mounted = 0;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001135 unsigned long flags = Mount_Flags;
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001136
Dees_Troy5bf43922012-09-07 16:07:55 -04001137 if (Is_Mounted()) {
1138 return true;
1139 } else if (!Can_Be_Mounted) {
1140 return false;
1141 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001142
1143 Find_Actual_Block_Device();
1144
1145 // Check the current file system before mounting
1146 Check_FS_Type();
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001147 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/exfat-fuse")) {
Dees_Troye34c1332013-02-06 19:13:00 +00001148 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 +00001149 LOGINFO("cmd: %s\n", cmd.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001150 string result;
1151 if (TWFunc::Exec_Cmd(cmd, result) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001152 LOGINFO("exfat-fuse failed to mount with result '%s', trying vfat\n", result.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001153 Current_File_System = "vfat";
1154 } else {
1155#ifdef TW_NO_EXFAT_FUSE
1156 UnMount(false);
1157 // We'll let the kernel handle it but using exfat-fuse to detect if the file system is actually exfat
1158 // Some kernels let us mount vfat as exfat which doesn't work out too well
1159#else
1160 exfat_mounted = 1;
1161#endif
1162 }
1163 }
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001164
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001165 if (Current_File_System == "ntfs" && (TWFunc::Path_Exists("/sbin/ntfs-3g") || TWFunc::Path_Exists("/sbin/mount.ntfs"))) {
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001166 string cmd;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001167 string Ntfsmount_Binary = "";
1168
1169 if (TWFunc::Path_Exists("/sbin/ntfs-3g"))
1170 Ntfsmount_Binary = "ntfs-3g";
1171 else if (TWFunc::Path_Exists("/sbin/mount.ntfs"))
1172 Ntfsmount_Binary = "mount.ntfs";
1173
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001174 if (Mount_Read_Only)
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001175 cmd = "/sbin/" + Ntfsmount_Binary + " -o ro " + Actual_Block_Device + " " + Mount_Point;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001176 else
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001177 cmd = "/sbin/" + Ntfsmount_Binary + " " + Actual_Block_Device + " " + Mount_Point;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001178 LOGINFO("cmd: '%s'\n", cmd.c_str());
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001179
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001180 if (TWFunc::Exec_Cmd(cmd) == 0) {
1181 return true;
1182 } else {
1183 LOGINFO("ntfs-3g failed to mount, trying regular mount method.\n");
1184 }
1185 }
1186
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001187 if (Mount_Read_Only)
1188 flags |= MS_RDONLY;
1189
Dees_Troy22042032012-12-18 21:23:08 +00001190 if (Fstab_File_System == "yaffs2") {
1191 // mount an MTD partition as a YAFFS2 filesystem.
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001192 flags = MS_NOATIME | MS_NODEV | MS_NODIRATIME;
1193 if (Mount_Read_Only)
1194 flags |= MS_RDONLY;
Dees_Troy76543db2013-06-19 16:24:30 +00001195 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags, NULL) < 0) {
1196 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags | MS_RDONLY, NULL) < 0) {
1197 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001198 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy76543db2013-06-19 16:24:30 +00001199 else
1200 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
1201 return false;
1202 } else {
1203 LOGINFO("Mounted '%s' (MTD) as RO\n", Mount_Point.c_str());
1204 return true;
1205 }
1206 } else {
1207 struct stat st;
1208 string test_path = Mount_Point;
1209 if (stat(test_path.c_str(), &st) < 0) {
1210 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001211 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy76543db2013-06-19 16:24:30 +00001212 else
1213 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
1214 return false;
1215 }
1216 mode_t new_mode = st.st_mode | S_IXUSR | S_IXGRP | S_IXOTH;
1217 if (new_mode != st.st_mode) {
1218 LOGINFO("Fixing execute permissions for %s\n", Mount_Point.c_str());
1219 if (chmod(Mount_Point.c_str(), new_mode) < 0) {
1220 if (Display_Error)
1221 LOGERR("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
1222 else
1223 LOGINFO("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
1224 return false;
1225 }
1226 }
Dees_Troy22042032012-12-18 21:23:08 +00001227 return true;
Dees_Troy76543db2013-06-19 16:24:30 +00001228 }
thatc7572eb2015-03-31 18:55:30 +02001229 }
1230
1231 string mount_fs = Current_File_System;
1232 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sys/module/texfat"))
1233 mount_fs = "texfat";
1234
1235 if (!exfat_mounted &&
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001236 mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), mount_fs.c_str(), flags, Mount_Options.c_str()) != 0 &&
1237 mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), mount_fs.c_str(), flags, NULL) != 0) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001238#ifdef TW_NO_EXFAT_FUSE
1239 if (Current_File_System == "exfat") {
Dees_Troy2673cec2013-04-02 20:22:16 +00001240 LOGINFO("Mounting exfat failed, trying vfat...\n");
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001241 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), "vfat", 0, NULL) != 0) {
Dees_Troy85f44ed2013-01-09 18:42:36 +00001242 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001243 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy85f44ed2013-01-09 18:42:36 +00001244 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001245 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001246 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 +00001247 return false;
Dees_Troy85f44ed2013-01-09 18:42:36 +00001248 }
Dees_Troyb05ddee2013-01-28 20:24:50 +00001249 } else {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001250#endif
bigbiff bigbiff26774a02014-03-29 18:22:00 -04001251 if (!Removable && Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001252 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001253 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001254 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
1255 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 +00001256 return false;
1257#ifdef TW_NO_EXFAT_FUSE
Dees_Troy85f44ed2013-01-09 18:42:36 +00001258 }
1259#endif
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001260 }
Ethan Yonker253368a2014-11-25 15:00:52 -06001261
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001262 if (Removable)
1263 Update_Size(Display_Error);
1264
xiaolu9416f4f2015-06-04 08:22:23 +08001265 if (!Symlink_Mount_Point.empty() && TWFunc::Path_Exists(Symlink_Path)) {
codelover2a3d4ce2015-03-14 20:26:49 +08001266 string Command = "mount -o bind '" + Symlink_Path + "' '" + Symlink_Mount_Point + "'";
Vojtech Bocek05534202013-09-11 08:11:56 +02001267 TWFunc::Exec_Cmd(Command);
Dees_Troy5bf43922012-09-07 16:07:55 -04001268 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001269 return true;
1270}
1271
1272bool TWPartition::UnMount(bool Display_Error) {
1273 if (Is_Mounted()) {
1274 int never_unmount_system;
1275
1276 DataManager::GetValue(TW_DONT_UNMOUNT_SYSTEM, never_unmount_system);
1277 if (never_unmount_system == 1 && Mount_Point == "/system")
1278 return true; // Never unmount system if you're not supposed to unmount it
1279
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001280 if (Is_Storage)
Ethan Yonker726a0202014-12-16 20:01:38 -06001281 PartitionManager.Remove_MTP_Storage(MTP_Storage_ID);
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001282
Dees_Troy38bd7602012-09-14 13:33:53 -04001283 if (!Symlink_Mount_Point.empty())
1284 umount(Symlink_Mount_Point.c_str());
1285
Dees_Troyb05ddee2013-01-28 20:24:50 +00001286 umount(Mount_Point.c_str());
1287 if (Is_Mounted()) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001288 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001289 gui_msg(Msg(msg::kError, "fail_unmount=Failed to unmount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy5bf43922012-09-07 16:07:55 -04001290 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001291 LOGINFO("Unable to unmount '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001292 return false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001293 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -04001294 return true;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001295 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001296 } else {
1297 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001298 }
1299}
1300
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05001301bool TWPartition::ReMount(bool Display_Error) {
1302 if (UnMount(Display_Error))
1303 return Mount(Display_Error);
1304 return false;
1305}
1306
1307bool TWPartition::ReMount_RW(bool Display_Error) {
1308 // No need to remount if already mounted rw
1309 if (Is_File_System_Writable())
1310 return true;
1311
1312 bool ro = Mount_Read_Only;
1313 int flags = Mount_Flags;
1314
1315 Mount_Read_Only = false;
1316 Mount_Flags &= ~MS_RDONLY;
1317
1318 bool ret = ReMount(Display_Error);
1319
1320 Mount_Read_Only = ro;
1321 Mount_Flags = flags;
1322
1323 return ret;
1324}
1325
Gary Peck43acadf2012-11-21 21:19:01 -08001326bool TWPartition::Wipe(string New_File_System) {
Ethan Yonker726a0202014-12-16 20:01:38 -06001327 bool wiped = false, update_crypt = false, recreate_media = true;
Dees_Troy16c2b312013-01-15 16:51:18 +00001328 int check;
1329 string Layout_Filename = Mount_Point + "/.layout_version";
1330
Dees_Troy38bd7602012-09-14 13:33:53 -04001331 if (!Can_Be_Wiped) {
Matt Mower3c366972015-12-25 19:28:31 -06001332 gui_msg(Msg(msg::kError, "cannot_wipe=Partition {1} cannot be wiped.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04001333 return false;
1334 }
1335
Dees_Troyc51f1f92012-09-20 15:32:13 -04001336 if (Mount_Point == "/cache")
Dees_Troy2673cec2013-04-02 20:22:16 +00001337 Log_Offset = 0;
Dees_Troyc51f1f92012-09-20 15:32:13 -04001338
Dees_Troy16c2b312013-01-15 16:51:18 +00001339 if (Retain_Layout_Version && Mount(false) && TWFunc::Path_Exists(Layout_Filename))
1340 TWFunc::copy_file(Layout_Filename, "/.layout_version", 0600);
1341 else
1342 unlink("/.layout_version");
Dees_Troy38bd7602012-09-14 13:33:53 -04001343
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001344 if (Has_Data_Media && Current_File_System == New_File_System) {
Dees_Troy16c2b312013-01-15 16:51:18 +00001345 wiped = Wipe_Data_Without_Wiping_Media();
Ethan Yonker5eac2222014-06-11 12:22:55 -05001346 recreate_media = false;
Dees_Troy16c2b312013-01-15 16:51:18 +00001347 } else {
Dees_Troy16c2b312013-01-15 16:51:18 +00001348 DataManager::GetValue(TW_RM_RF_VAR, check);
1349
Hashcodedabfd492013-08-29 22:45:30 -07001350 if (check || Use_Rm_Rf)
Dees_Troy16c2b312013-01-15 16:51:18 +00001351 wiped = Wipe_RMRF();
1352 else if (New_File_System == "ext4")
1353 wiped = Wipe_EXT4();
1354 else if (New_File_System == "ext2" || New_File_System == "ext3")
1355 wiped = Wipe_EXT23(New_File_System);
1356 else if (New_File_System == "vfat")
1357 wiped = Wipe_FAT();
1358 else if (New_File_System == "exfat")
1359 wiped = Wipe_EXFAT();
1360 else if (New_File_System == "yaffs2")
1361 wiped = Wipe_MTD();
Dees_Troye5017042013-08-29 16:38:55 +00001362 else if (New_File_System == "f2fs")
1363 wiped = Wipe_F2FS();
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001364 else if (New_File_System == "ntfs")
1365 wiped = Wipe_NTFS();
Dees_Troy16c2b312013-01-15 16:51:18 +00001366 else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001367 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 +00001368 unlink("/.layout_version");
1369 return false;
1370 }
1371 update_crypt = wiped;
Gary Pecke8bc5d72012-12-21 06:45:25 -08001372 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001373
Gary Pecke8bc5d72012-12-21 06:45:25 -08001374 if (wiped) {
Dees_Troy1c1ac442013-01-17 21:42:14 +00001375 if (Mount_Point == "/cache")
1376 DataManager::Output_Version();
1377
Dees_Troy16c2b312013-01-15 16:51:18 +00001378 if (TWFunc::Path_Exists("/.layout_version") && Mount(false))
1379 TWFunc::copy_file("/.layout_version", Layout_Filename, 0600);
1380
1381 if (update_crypt) {
1382 Setup_File_System(false);
1383 if (Is_Encrypted && !Is_Decrypted) {
1384 // just wiped an encrypted partition back to its unencrypted state
1385 Is_Encrypted = false;
1386 Is_Decrypted = false;
1387 Decrypted_Block_Device = "";
1388 if (Mount_Point == "/data") {
1389 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1390 DataManager::SetValue(TW_IS_DECRYPTED, 0);
1391 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001392 }
1393 }
Ethan Yonker5eac2222014-06-11 12:22:55 -05001394
Ethan Yonker66a19492015-12-10 10:19:45 -06001395 if (Has_Data_Media && recreate_media) {
Ethan Yonker5eac2222014-06-11 12:22:55 -05001396 Recreate_Media_Folder();
1397 }
Matt Mower209c9632016-01-20 12:08:35 -06001398 if (Is_Storage && Mount(false))
1399 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Gary Pecke8bc5d72012-12-21 06:45:25 -08001400 }
Matt Mower209c9632016-01-20 12:08:35 -06001401
Gary Pecke8bc5d72012-12-21 06:45:25 -08001402 return wiped;
Dees_Troy51a0e822012-09-05 15:24:24 -04001403}
1404
Gary Peck43acadf2012-11-21 21:19:01 -08001405bool TWPartition::Wipe() {
Gary Peck82599a82012-11-21 16:23:12 -08001406 if (Is_File_System(Current_File_System))
1407 return Wipe(Current_File_System);
1408 else
1409 return Wipe(Fstab_File_System);
Gary Peck43acadf2012-11-21 21:19:01 -08001410}
1411
Dees_Troye58d5262012-09-21 12:27:57 -04001412bool TWPartition::Wipe_AndSec(void) {
1413 if (!Has_Android_Secure)
1414 return false;
1415
Dees_Troye58d5262012-09-21 12:27:57 -04001416 if (!Mount(true))
1417 return false;
1418
Ethan Yonker74db1572015-10-28 12:44:49 -05001419 gui_msg(Msg("wiping=Wiping {1}")(Backup_Display_Name));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001420 TWFunc::removeDir(Mount_Point + "/.android_secure/", true);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001421 return true;
Dees_Troye58d5262012-09-21 12:27:57 -04001422}
1423
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001424bool TWPartition::Can_Repair() {
Ethan Yonkera2719152015-05-28 09:44:41 -05001425 if (Mount_Read_Only)
1426 return false;
Matt Mower18794c82015-11-11 16:22:45 -06001427 if (Current_File_System == "vfat" && TWFunc::Path_Exists("/sbin/fsck.fat"))
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001428 return true;
1429 else if ((Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") && TWFunc::Path_Exists("/sbin/e2fsck"))
1430 return true;
1431 else if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/fsck.exfat"))
1432 return true;
1433 else if (Current_File_System == "f2fs" && TWFunc::Path_Exists("/sbin/fsck.f2fs"))
1434 return true;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001435 else if (Current_File_System == "ntfs" && (TWFunc::Path_Exists("/sbin/ntfsfix") || TWFunc::Path_Exists("/sbin/fsck.ntfs")))
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001436 return true;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001437 return false;
1438}
1439
1440bool TWPartition::Repair() {
1441 string command;
1442
1443 if (Current_File_System == "vfat") {
Matt Mower18794c82015-11-11 16:22:45 -06001444 if (!TWFunc::Path_Exists("/sbin/fsck.fat")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001445 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.fat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001446 return false;
1447 }
1448 if (!UnMount(true))
1449 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001450 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.fat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001451 Find_Actual_Block_Device();
Matt Mower18794c82015-11-11 16:22:45 -06001452 command = "/sbin/fsck.fat -y " + Actual_Block_Device;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001453 LOGINFO("Repair command: %s\n", command.c_str());
1454 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001455 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001456 return true;
1457 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001458 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001459 return false;
1460 }
1461 }
1462 if (Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") {
1463 if (!TWFunc::Path_Exists("/sbin/e2fsck")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001464 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("e2fsck"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001465 return false;
1466 }
1467 if (!UnMount(true))
1468 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001469 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("e2fsck"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001470 Find_Actual_Block_Device();
Ethan Yonkera2719152015-05-28 09:44:41 -05001471 command = "/sbin/e2fsck -fp " + Actual_Block_Device;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001472 LOGINFO("Repair command: %s\n", command.c_str());
1473 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001474 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001475 return true;
1476 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001477 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001478 return false;
1479 }
1480 }
1481 if (Current_File_System == "exfat") {
1482 if (!TWFunc::Path_Exists("/sbin/fsck.exfat")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001483 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.exfat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001484 return false;
1485 }
1486 if (!UnMount(true))
1487 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001488 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.exfat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001489 Find_Actual_Block_Device();
1490 command = "/sbin/fsck.exfat " + Actual_Block_Device;
1491 LOGINFO("Repair command: %s\n", command.c_str());
1492 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001493 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001494 return true;
1495 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001496 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001497 return false;
1498 }
1499 }
1500 if (Current_File_System == "f2fs") {
1501 if (!TWFunc::Path_Exists("/sbin/fsck.f2fs")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001502 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.f2fs"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001503 return false;
1504 }
1505 if (!UnMount(true))
1506 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001507 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.f2fs"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001508 Find_Actual_Block_Device();
1509 command = "/sbin/fsck.f2fs " + Actual_Block_Device;
1510 LOGINFO("Repair command: %s\n", command.c_str());
1511 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001512 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001513 return true;
1514 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001515 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001516 return false;
1517 }
1518 }
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001519 if (Current_File_System == "ntfs") {
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001520 string Ntfsfix_Binary;
1521 if (TWFunc::Path_Exists("/sbin/ntfsfix"))
1522 Ntfsfix_Binary = "ntfsfix";
1523 else if (TWFunc::Path_Exists("/sbin/fsck.ntfs"))
1524 Ntfsfix_Binary = "fsck.ntfs";
1525 else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001526 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("ntfsfix"));
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001527 return false;
1528 }
1529 if (!UnMount(true))
1530 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001531 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)(Ntfsfix_Binary));
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001532 Find_Actual_Block_Device();
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001533 command = "/sbin/" + Ntfsfix_Binary + " " + Actual_Block_Device;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001534 LOGINFO("Repair command: %s\n", command.c_str());
1535 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001536 gui_msg("done=Done.");
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001537 return true;
1538 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001539 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001540 return false;
1541 }
1542 }
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001543 return false;
1544}
1545
Ethan Yonkera2719152015-05-28 09:44:41 -05001546bool TWPartition::Can_Resize() {
1547 if (Mount_Read_Only)
1548 return false;
1549 if ((Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") && TWFunc::Path_Exists("/sbin/resize2fs"))
1550 return true;
1551 return false;
1552}
1553
1554bool TWPartition::Resize() {
1555 string command;
1556
1557 if (Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") {
1558 if (!Can_Repair()) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001559 LOGINFO("Cannot resize %s because %s cannot be repaired before resizing.\n", Display_Name.c_str(), Display_Name.c_str());
1560 gui_msg(Msg(msg::kError, "cannot_resize=Cannot resize {1}.")(Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001561 return false;
1562 }
1563 if (!TWFunc::Path_Exists("/sbin/resize2fs")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001564 LOGINFO("resize2fs does not exist! Cannot resize!\n");
1565 gui_msg(Msg(msg::kError, "cannot_resize=Cannot resize {1}.")(Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001566 return false;
1567 }
1568 // Repair will unmount so no need to do it twice
Ethan Yonker74db1572015-10-28 12:44:49 -05001569 gui_msg(Msg("repair_resize=Repairing {1} before resizing.")( Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001570 if (!Repair())
1571 return false;
Ethan Yonker74db1572015-10-28 12:44:49 -05001572 gui_msg(Msg("resizing=Resizing {1} using {2}...")(Display_Name)("resize2fs"));
Ethan Yonkera2719152015-05-28 09:44:41 -05001573 Find_Actual_Block_Device();
1574 command = "/sbin/resize2fs " + Actual_Block_Device;
1575 if (Length != 0) {
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001576 unsigned long long Actual_Size = IOCTL_Get_Block_Size();
1577 if (Actual_Size == 0)
1578 return false;
Ethan Yonkera2719152015-05-28 09:44:41 -05001579
Ethan Yonkera2719152015-05-28 09:44:41 -05001580 unsigned long long Block_Count;
1581 if (Length < 0) {
1582 // Reduce overall size by this length
1583 Block_Count = (Actual_Size / 1024LLU) - ((unsigned long long)(Length * -1) / 1024LLU);
1584 } else {
1585 // This is the size, not a size reduction
1586 Block_Count = ((unsigned long long)(Length) / 1024LLU);
1587 }
1588 char temp[256];
1589 sprintf(temp, "%llu", Block_Count);
1590 command += " ";
1591 command += temp;
1592 command += "K";
1593 }
1594 LOGINFO("Resize command: %s\n", command.c_str());
1595 if (TWFunc::Exec_Cmd(command) == 0) {
1596 Update_Size(true);
Ethan Yonker74db1572015-10-28 12:44:49 -05001597 gui_msg("done=Done.");
Ethan Yonkera2719152015-05-28 09:44:41 -05001598 return true;
1599 } else {
1600 Update_Size(true);
Ethan Yonker74db1572015-10-28 12:44:49 -05001601 gui_msg(Msg(msg::kError, "unable_resize=Unable to resize {1}.")(Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001602 return false;
1603 }
1604 }
1605 return false;
1606}
1607
bigbiffce8f83c2015-12-12 18:30:21 -05001608bool TWPartition::Backup(PartitionSettings *part_settings, pid_t *tar_fork_pid) {
1609 if (Backup_Method == BM_FILES)
1610 return Backup_Tar(part_settings, tar_fork_pid);
1611 else if (Backup_Method == BM_DD)
1612 return Backup_Image(part_settings);
1613 else if (Backup_Method == BM_FLASH_UTILS)
1614 return Backup_Dump_Image(part_settings);
Dees_Troy2673cec2013-04-02 20:22:16 +00001615 LOGERR("Unknown backup method for '%s'\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001616 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001617}
1618
James Christopher Adduono79ae0932016-10-25 02:18:32 -04001619bool TWPartition::Check_Restore_File_MD5(const string& Filename) {
1620 twrpDigest md5sum;
1621
1622 md5sum.setfn(Filename);
1623 switch (md5sum.verify_md5digest()) {
1624 case MD5_OK:
1625 gui_msg(Msg("md5_matched=MD5 matched for '{1}'.")(Filename));
1626 return true;
1627 case MD5_FILE_UNREADABLE:
1628 case MD5_NOT_FOUND:
1629 gui_msg(Msg(msg::kError, "no_md5_found=No md5 file found for '{1}'. Please unselect Enable MD5 verification to restore.")(Filename));
1630 break;
1631 case MD5_MATCH_FAIL:
1632 gui_msg(Msg(msg::kError, "md5_fail_match=MD5 failed to match on '{1}'.")(Filename));
1633 break;
1634 }
1635 return false;
1636}
1637
bigbiff bigbiff8fd4b092016-08-30 20:48:53 -04001638bool TWPartition::Check_MD5(PartitionSettings *part_settings) {
James Christopher Adduono79ae0932016-10-25 02:18:32 -04001639 string Full_Filename;
Dees_Troy43d8b002012-09-17 16:00:01 -04001640 char split_filename[512];
1641 int index = 0;
1642
Captain Throwbackff5935f2014-09-23 16:08:34 -04001643 sync();
1644
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05001645 Full_Filename = part_settings->Backup_Folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04001646 if (!TWFunc::Path_Exists(Full_Filename)) {
1647 // This is a split archive, we presume
James Christopher Adduono79ae0932016-10-25 02:18:32 -04001648 memset(split_filename, 0, sizeof(split_filename));
Dees_Troy83bd4832013-05-04 12:39:56 +00001649 while (index < 1000) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001650 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
James Christopher Adduono79ae0932016-10-25 02:18:32 -04001651 if (!TWFunc::Path_Exists(split_filename))
1652 break;
1653 LOGINFO("split_filename: %s\n", split_filename);
1654 if (!Check_Restore_File_MD5(split_filename))
1655 return false;
1656 index++;
Dees_Troy43d8b002012-09-17 16:00:01 -04001657 }
Dees_Troy4a2a1262012-09-18 09:33:47 -04001658 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001659 }
James Christopher Adduono79ae0932016-10-25 02:18:32 -04001660 return Check_Restore_File_MD5(Full_Filename); // Single file archive
Dees_Troy43d8b002012-09-17 16:00:01 -04001661}
1662
bigbiffce8f83c2015-12-12 18:30:21 -05001663bool TWPartition::Restore(PartitionSettings *part_settings) {
Matt Mower3c366972015-12-25 19:28:31 -06001664 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, gui_parse_text("{@restoring_hdr}"));
Ethan Yonkere080c1f2016-09-19 13:50:25 -05001665 LOGINFO("Restore filename is: %s/%s\n", part_settings->Backup_Folder.c_str(), Backup_FileName.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001666
bigbiffce8f83c2015-12-12 18:30:21 -05001667 string Restore_File_System = Get_Restore_File_System(part_settings);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001668
1669 if (Is_File_System(Restore_File_System))
bigbiffce8f83c2015-12-12 18:30:21 -05001670 return Restore_Tar(part_settings);
1671 else if (Is_Image(Restore_File_System))
1672 return Restore_Image(part_settings);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001673
1674 LOGERR("Unknown restore method for '%s'\n", Mount_Point.c_str());
1675 return false;
1676}
1677
bigbiffce8f83c2015-12-12 18:30:21 -05001678string TWPartition::Get_Restore_File_System(PartitionSettings *part_settings) {
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001679 size_t first_period, second_period;
1680 string Restore_File_System;
1681
Gary Peck43acadf2012-11-21 21:19:01 -08001682 // Parse backup filename to extract the file system before wiping
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05001683 first_period = Backup_FileName.find(".");
Gary Peck43acadf2012-11-21 21:19:01 -08001684 if (first_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001685 LOGERR("Unable to find file system (first period).\n");
thatd43bf2d2014-09-21 23:13:02 +02001686 return string();
Gary Peck43acadf2012-11-21 21:19:01 -08001687 }
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05001688 Restore_File_System = Backup_FileName.substr(first_period + 1, Backup_FileName.size() - first_period - 1);
Gary Peck43acadf2012-11-21 21:19:01 -08001689 second_period = Restore_File_System.find(".");
1690 if (second_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001691 LOGERR("Unable to find file system (second period).\n");
thatd43bf2d2014-09-21 23:13:02 +02001692 return string();
Gary Peck43acadf2012-11-21 21:19:01 -08001693 }
1694 Restore_File_System.resize(second_period);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001695 return Restore_File_System;
Dees_Troy51a0e822012-09-05 15:24:24 -04001696}
1697
1698string TWPartition::Backup_Method_By_Name() {
bigbiffce8f83c2015-12-12 18:30:21 -05001699 if (Backup_Method == BM_NONE)
Dees_Troy38bd7602012-09-14 13:33:53 -04001700 return "none";
bigbiffce8f83c2015-12-12 18:30:21 -05001701 else if (Backup_Method == BM_FILES)
Dees_Troy38bd7602012-09-14 13:33:53 -04001702 return "files";
bigbiffce8f83c2015-12-12 18:30:21 -05001703 else if (Backup_Method == BM_DD)
Dees_Troy38bd7602012-09-14 13:33:53 -04001704 return "dd";
bigbiffce8f83c2015-12-12 18:30:21 -05001705 else if (Backup_Method == BM_FLASH_UTILS)
Dees_Troy38bd7602012-09-14 13:33:53 -04001706 return "flash_utils";
1707 else
1708 return "undefined";
1709 return "ERROR!";
Dees_Troy51a0e822012-09-05 15:24:24 -04001710}
1711
1712bool TWPartition::Decrypt(string Password) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001713 LOGINFO("STUB TWPartition::Decrypt, password: '%s'\n", Password.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001714 // Is this needed?
Dees_Troy51a0e822012-09-05 15:24:24 -04001715 return 1;
1716}
1717
1718bool TWPartition::Wipe_Encryption() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001719 bool Save_Data_Media = Has_Data_Media;
1720
1721 if (!UnMount(true))
1722 return false;
1723
Dees_Troy38bd7602012-09-14 13:33:53 -04001724 Has_Data_Media = false;
Dees_Troy74fb2e92013-04-15 14:35:47 +00001725 Decrypted_Block_Device = "";
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001726#ifdef TW_INCLUDE_CRYPTO
Ethan Yonker1b190162016-12-05 15:25:19 -06001727 if (Is_Decrypted && !Decrypted_Block_Device.empty()) {
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001728 if (!UnMount(true))
1729 return false;
Matt Mower2b18a532015-02-20 16:58:05 -06001730 if (delete_crypto_blk_dev((char*)("userdata")) != 0) {
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001731 LOGERR("Error deleting crypto block device, continuing anyway.\n");
1732 }
1733 }
1734#endif
Dees_Troy74fb2e92013-04-15 14:35:47 +00001735 Is_Decrypted = false;
1736 Is_Encrypted = false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001737 Find_Actual_Block_Device();
Ethan Yonkerbc85b632015-08-09 12:48:14 -05001738 if (Crypto_Key_Location == "footer") {
1739 int newlen, fd;
1740 if (Length != 0) {
1741 newlen = Length;
1742 if (newlen < 0)
1743 newlen = newlen * -1;
1744 } else {
1745 newlen = CRYPT_FOOTER_OFFSET;
1746 }
1747 if ((fd = open(Actual_Block_Device.c_str(), O_RDWR)) < 0) {
1748 gui_print_color("warning", "Unable to open '%s' to wipe crypto key\n", Actual_Block_Device.c_str());
1749 } else {
1750 unsigned int block_count;
1751 if ((ioctl(fd, BLKGETSIZE, &block_count)) == -1) {
1752 gui_print_color("warning", "Unable to get block size for wiping crypto footer.\n");
1753 } else {
1754 off64_t offset = ((off64_t)block_count * 512) - newlen;
1755 if (lseek64(fd, offset, SEEK_SET) == -1) {
1756 gui_print_color("warning", "Unable to lseek64 for wiping crypto footer.\n");
1757 } else {
1758 void* buffer = malloc(newlen);
1759 if (!buffer) {
1760 gui_print_color("warning", "Failed to malloc for wiping crypto footer.\n");
1761 } else {
1762 memset(buffer, 0, newlen);
1763 int ret = write(fd, buffer, newlen);
1764 if (ret != newlen) {
1765 gui_print_color("warning", "Failed to wipe crypto footer.\n");
1766 } else {
1767 LOGINFO("Successfully wiped crypto footer.\n");
1768 }
Matt Mower13a8f0b2015-09-26 15:40:03 -05001769 free(buffer);
Ethan Yonkerbc85b632015-08-09 12:48:14 -05001770 }
1771 }
1772 }
1773 close(fd);
1774 }
1775 } else {
Ethan Yonkerc2dafbb2016-03-15 22:20:59 -05001776 if (TWFunc::IOCTL_Get_Block_Size(Crypto_Key_Location.c_str()) >= 16384LLU) {
1777 string Command = "dd of='" + Crypto_Key_Location + "' if=/dev/zero bs=16384 count=1";
1778 TWFunc::Exec_Cmd(Command);
1779 } else {
1780 LOGINFO("Crypto key location reports size < 16K so not wiping crypto footer.\n");
1781 }
Ethan Yonkerbc85b632015-08-09 12:48:14 -05001782 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001783 if (Wipe(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001784 Has_Data_Media = Save_Data_Media;
1785 if (Has_Data_Media && !Symlink_Mount_Point.empty()) {
1786 Recreate_Media_Folder();
Ethan Yonker726a0202014-12-16 20:01:38 -06001787 if (Mount(false))
1788 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04001789 }
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001790 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
Ethan Yonker83e82572014-04-04 10:59:28 -05001791#ifndef TW_OEM_BUILD
Ethan Yonker74db1572015-10-28 12:44:49 -05001792 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 -05001793#endif
Dees_Troy38bd7602012-09-14 13:33:53 -04001794 return true;
1795 } else {
1796 Has_Data_Media = Save_Data_Media;
Ethan Yonker74db1572015-10-28 12:44:49 -05001797 gui_err("format_data_err=Unable to format to remove encryption.");
Ethan Yonker726a0202014-12-16 20:01:38 -06001798 if (Has_Data_Media && Mount(false))
1799 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04001800 return false;
1801 }
1802 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001803}
1804
1805void TWPartition::Check_FS_Type() {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001806 const char* type;
1807 blkid_probe pr;
Dees_Troy5bf43922012-09-07 16:07:55 -04001808
Dees_Troy68cab492012-12-12 19:29:35 +00001809 if (Fstab_File_System == "yaffs2" || Fstab_File_System == "mtd" || Fstab_File_System == "bml" || Ignore_Blkid)
1810 return; // Running blkid on some mtd devices causes a massive crash or needs to be skipped
Dees_Troy5bf43922012-09-07 16:07:55 -04001811
Dees_Troy38bd7602012-09-14 13:33:53 -04001812 Find_Actual_Block_Device();
Dees_Troy8170a922012-09-18 15:40:25 -04001813 if (!Is_Present)
1814 return;
Dees_Troy51127312012-09-08 13:08:49 -04001815
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001816 pr = blkid_new_probe_from_filename(Actual_Block_Device.c_str());
1817 if (blkid_do_fullprobe(pr)) {
1818 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00001819 LOGINFO("Can't probe device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001820 return;
Dees_Troy5bf43922012-09-07 16:07:55 -04001821 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001822
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05001823 if (blkid_probe_lookup_value(pr, "TYPE", &type, NULL) < 0) {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001824 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00001825 LOGINFO("can't find filesystem on device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001826 return;
1827 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001828
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001829 Current_File_System = type;
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001830 blkid_free_probe(pr);
Dees_Troy51a0e822012-09-05 15:24:24 -04001831}
1832
Gary Peck43acadf2012-11-21 21:19:01 -08001833bool TWPartition::Wipe_EXT23(string File_System) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001834 if (!UnMount(true))
1835 return false;
1836
Dees_Troy43d8b002012-09-17 16:00:01 -04001837 if (TWFunc::Path_Exists("/sbin/mke2fs")) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001838 string command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001839
Greg Wallace4b44fef2015-12-29 15:33:24 -05001840 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mke2fs"));
Dees_Troy38bd7602012-09-14 13:33:53 -04001841 Find_Actual_Block_Device();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001842 command = "mke2fs -t " + File_System + " -m 0 " + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001843 LOGINFO("mke2fs command: %s\n", command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001844 if (TWFunc::Exec_Cmd(command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001845 Current_File_System = File_System;
Dees_Troye58d5262012-09-21 12:27:57 -04001846 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05001847 gui_msg("done=Done.");
Dees_Troy38bd7602012-09-14 13:33:53 -04001848 return true;
1849 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001850 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04001851 return false;
1852 }
1853 } else
1854 return Wipe_RMRF();
1855
1856 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001857}
1858
1859bool TWPartition::Wipe_EXT4() {
Ethan Yonker25f20c12014-10-14 09:04:43 -05001860 Find_Actual_Block_Device();
1861 if (!Is_Present) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001862 LOGINFO("Block device not present, cannot wipe %s.\n", Display_Name.c_str());
1863 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Ethan Yonker25f20c12014-10-14 09:04:43 -05001864 return false;
1865 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001866 if (!UnMount(true))
1867 return false;
1868
Dees_Troyb3265ab2013-08-30 02:59:14 +00001869#if defined(HAVE_SELINUX) && defined(USE_EXT4)
Ethan Yonkerf27497f2014-02-09 11:48:33 -06001870 int ret;
1871 char *secontext = NULL;
1872
Greg Wallace4b44fef2015-12-29 15:33:24 -05001873 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("make_ext4fs"));
Ethan Yonkerf27497f2014-02-09 11:48:33 -06001874
Dees Troy99c8dbf2014-03-10 16:53:58 +00001875 if (!selinux_handle || selabel_lookup(selinux_handle, &secontext, Mount_Point.c_str(), S_IFDIR) < 0) {
Ethan Yonkerf27497f2014-02-09 11:48:33 -06001876 LOGINFO("Cannot lookup security context for '%s'\n", Mount_Point.c_str());
1877 ret = make_ext4fs(Actual_Block_Device.c_str(), Length, Mount_Point.c_str(), NULL);
1878 } else {
1879 ret = make_ext4fs(Actual_Block_Device.c_str(), Length, Mount_Point.c_str(), selinux_handle);
1880 }
1881 if (ret != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001882 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troya95f55c2013-08-17 13:14:43 +00001883 return false;
1884 } else {
bigbiff bigbiffc49d7062013-10-11 20:28:00 -04001885 string sedir = Mount_Point + "/lost+found";
1886 PartitionManager.Mount_By_Path(sedir.c_str(), true);
1887 rmdir(sedir.c_str());
1888 mkdir(sedir.c_str(), S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP);
Dees_Troya95f55c2013-08-17 13:14:43 +00001889 return true;
1890 }
1891#else
Dees_Troy43d8b002012-09-17 16:00:01 -04001892 if (TWFunc::Path_Exists("/sbin/make_ext4fs")) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001893 string Command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001894
Greg Wallace4b44fef2015-12-29 15:33:24 -05001895 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("make_ext4fs"));
Dees_Troy38bd7602012-09-14 13:33:53 -04001896 Find_Actual_Block_Device();
1897 Command = "make_ext4fs";
1898 if (!Is_Decrypted && Length != 0) {
1899 // Only use length if we're not decrypted
1900 char len[32];
1901 sprintf(len, "%i", Length);
1902 Command += " -l ";
1903 Command += len;
1904 }
Dees_Troy5295d582013-09-06 15:51:08 +00001905 if (TWFunc::Path_Exists("/file_contexts")) {
1906 Command += " -S /file_contexts";
1907 }
1908 Command += " -a " + Mount_Point + " " + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001909 LOGINFO("make_ext4fs command: %s\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001910 if (TWFunc::Exec_Cmd(Command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001911 Current_File_System = "ext4";
Dees_Troye58d5262012-09-21 12:27:57 -04001912 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05001913 gui_msg("done=Done.");
Dees_Troy38bd7602012-09-14 13:33:53 -04001914 return true;
1915 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001916 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04001917 return false;
1918 }
1919 } else
Gary Peck43acadf2012-11-21 21:19:01 -08001920 return Wipe_EXT23("ext4");
Dees_Troya95f55c2013-08-17 13:14:43 +00001921#endif
Dees_Troy38bd7602012-09-14 13:33:53 -04001922 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001923}
1924
1925bool TWPartition::Wipe_FAT() {
Vojtech Bocek05534202013-09-11 08:11:56 +02001926 string command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001927
Matt Mower18794c82015-11-11 16:22:45 -06001928 if (TWFunc::Path_Exists("/sbin/mkfs.fat")) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001929 if (!UnMount(true))
1930 return false;
1931
Greg Wallace4b44fef2015-12-29 15:33:24 -05001932 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mkfs.fat"));
Dees_Troy38bd7602012-09-14 13:33:53 -04001933 Find_Actual_Block_Device();
Matt Mower18794c82015-11-11 16:22:45 -06001934 command = "mkfs.fat " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02001935 if (TWFunc::Exec_Cmd(command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001936 Current_File_System = "vfat";
Dees_Troye58d5262012-09-21 12:27:57 -04001937 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05001938 gui_msg("done=Done.");
Dees_Troy38bd7602012-09-14 13:33:53 -04001939 return true;
1940 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001941 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04001942 return false;
1943 }
1944 return true;
1945 }
1946 else
1947 return Wipe_RMRF();
1948
1949 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001950}
1951
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001952bool TWPartition::Wipe_EXFAT() {
Vojtech Bocek05534202013-09-11 08:11:56 +02001953 string command;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001954
1955 if (TWFunc::Path_Exists("/sbin/mkexfatfs")) {
1956 if (!UnMount(true))
1957 return false;
1958
Greg Wallace4b44fef2015-12-29 15:33:24 -05001959 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mkexfatfs"));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001960 Find_Actual_Block_Device();
1961 command = "mkexfatfs " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02001962 if (TWFunc::Exec_Cmd(command) == 0) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001963 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05001964 gui_msg("done=Done.");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001965 return true;
1966 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001967 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001968 return false;
1969 }
1970 return true;
1971 }
1972 return false;
1973}
1974
Dees_Troy38bd7602012-09-14 13:33:53 -04001975bool TWPartition::Wipe_MTD() {
1976 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)("MTD"));
Dees_Troy38bd7602012-09-14 13:33:53 -04001980
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001981 mtd_scan_partitions();
1982 const MtdPartition* mtd = mtd_find_partition_by_name(MTD_Name.c_str());
1983 if (mtd == NULL) {
1984 LOGERR("No mtd partition named '%s'", MTD_Name.c_str());
1985 return false;
1986 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001987
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001988 MtdWriteContext* ctx = mtd_write_partition(mtd);
1989 if (ctx == NULL) {
1990 LOGERR("Can't write '%s', failed to format.", MTD_Name.c_str());
1991 return false;
1992 }
1993 if (mtd_erase_blocks(ctx, -1) == -1) {
1994 mtd_write_close(ctx);
1995 LOGERR("Failed to format '%s'", MTD_Name.c_str());
1996 return false;
1997 }
1998 if (mtd_write_close(ctx) != 0) {
1999 LOGERR("Failed to close '%s'", MTD_Name.c_str());
2000 return false;
2001 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08002002 Current_File_System = "yaffs2";
Dees_Troye58d5262012-09-21 12:27:57 -04002003 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05002004 gui_msg("done=Done.");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02002005 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002006}
2007
2008bool TWPartition::Wipe_RMRF() {
Dees_Troy38bd7602012-09-14 13:33:53 -04002009 if (!Mount(true))
2010 return false;
Ethan Yonker726a0202014-12-16 20:01:38 -06002011 // This is the only wipe that leaves the partition mounted, so we
2012 // must manually remove the partition from MTP if it is a storage
2013 // partition.
2014 if (Is_Storage)
2015 PartitionManager.Remove_MTP_Storage(MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04002016
Ethan Yonker74db1572015-10-28 12:44:49 -05002017 gui_msg(Msg("remove_all=Removing all files under '{1}'")(Mount_Point));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002018 TWFunc::removeDir(Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04002019 Recreate_AndSec_Folder();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002020 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002021}
2022
Dees_Troye5017042013-08-29 16:38:55 +00002023bool TWPartition::Wipe_F2FS() {
Vojtech Bocek05534202013-09-11 08:11:56 +02002024 string command;
Dees_Troye5017042013-08-29 16:38:55 +00002025
2026 if (TWFunc::Path_Exists("/sbin/mkfs.f2fs")) {
2027 if (!UnMount(true))
2028 return false;
2029
Greg Wallace4b44fef2015-12-29 15:33:24 -05002030 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mkfs.f2fs"));
Dees_Troye5017042013-08-29 16:38:55 +00002031 Find_Actual_Block_Device();
Greg Wallace558aef72016-01-19 21:14:30 -05002032 command = "mkfs.f2fs -t 0";
dhacker29a3fa75f2015-01-17 19:42:33 -05002033 if (!Is_Decrypted && Length != 0) {
2034 // Only use length if we're not decrypted
2035 char len[32];
2036 int mod_length = Length;
2037 if (Length < 0)
2038 mod_length *= -1;
2039 sprintf(len, "%i", mod_length);
2040 command += " -r ";
2041 command += len;
2042 }
2043 command += " " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02002044 if (TWFunc::Exec_Cmd(command) == 0) {
Dees_Troye5017042013-08-29 16:38:55 +00002045 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05002046 gui_msg("done=Done.");
Dees_Troye5017042013-08-29 16:38:55 +00002047 return true;
2048 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002049 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troye5017042013-08-29 16:38:55 +00002050 return false;
2051 }
2052 return true;
2053 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002054 LOGINFO("mkfs.f2fs binary not found, using rm -rf to wipe.\n");
Dees_Troye5017042013-08-29 16:38:55 +00002055 return Wipe_RMRF();
2056 }
2057 return false;
2058}
2059
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002060bool TWPartition::Wipe_NTFS() {
2061 string command;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002062 string Ntfsmake_Binary;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002063
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002064 if (TWFunc::Path_Exists("/sbin/mkntfs"))
2065 Ntfsmake_Binary = "mkntfs";
2066 else if (TWFunc::Path_Exists("/sbin/mkfs.ntfs"))
2067 Ntfsmake_Binary = "mkfs.ntfs";
2068 else
2069 return false;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002070
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002071 if (!UnMount(true))
2072 return false;
2073
Greg Wallace4b44fef2015-12-29 15:33:24 -05002074 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)(Ntfsmake_Binary));
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002075 Find_Actual_Block_Device();
2076 command = "/sbin/" + Ntfsmake_Binary + " " + Actual_Block_Device;
2077 if (TWFunc::Exec_Cmd(command) == 0) {
2078 Recreate_AndSec_Folder();
2079 gui_msg("done=Done.");
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002080 return true;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002081 } else {
2082 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
2083 return false;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002084 }
2085 return false;
2086}
2087
Dees_Troy51a0e822012-09-05 15:24:24 -04002088bool TWPartition::Wipe_Data_Without_Wiping_Media() {
Ethan Yonker83e82572014-04-04 10:59:28 -05002089#ifdef TW_OEM_BUILD
2090 // In an OEM Build we want to do a full format
2091 return Wipe_Encryption();
2092#else
Ethan Yonker66a19492015-12-10 10:19:45 -06002093 bool ret = false;
Dees_Troy38bd7602012-09-14 13:33:53 -04002094
Dees_Troy38bd7602012-09-14 13:33:53 -04002095 if (!Mount(true))
2096 return false;
2097
Ethan Yonker74db1572015-10-28 12:44:49 -05002098 gui_msg("wiping_data=Wiping data without wiping /data/media ...");
Ethan Yonker66a19492015-12-10 10:19:45 -06002099 ret = Wipe_Data_Without_Wiping_Media_Func(Mount_Point + "/");
2100 if (ret)
2101 gui_msg("done=Done.");
2102 return ret;
2103#endif // ifdef TW_OEM_BUILD
2104}
2105
2106bool TWPartition::Wipe_Data_Without_Wiping_Media_Func(const string& parent __unused) {
2107 string dir;
Dees_Troy38bd7602012-09-14 13:33:53 -04002108
2109 DIR* d;
Ethan Yonker66a19492015-12-10 10:19:45 -06002110 d = opendir(parent.c_str());
Dees_Troy16b74352012-11-14 22:27:31 +00002111 if (d != NULL) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002112 struct dirent* de;
2113 while ((de = readdir(d)) != NULL) {
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002114 if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) continue;
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05002115
Ethan Yonker66a19492015-12-10 10:19:45 -06002116 dir = parent;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002117 dir.append(de->d_name);
Ethan Yonker3fdcda42016-11-30 12:29:37 -06002118 if (wipe_exclusions.check_skip_dirs(dir)) {
Ethan Yonker66a19492015-12-10 10:19:45 -06002119 LOGINFO("skipped '%s'\n", dir.c_str());
2120 continue;
2121 }
Dees_Troyce675462013-01-09 19:48:21 +00002122 if (de->d_type == DT_DIR) {
Ethan Yonker66a19492015-12-10 10:19:45 -06002123 dir.append("/");
2124 if (!Wipe_Data_Without_Wiping_Media_Func(dir)) {
2125 closedir(d);
2126 return false;
2127 }
2128 rmdir(dir.c_str());
bigbiff bigbiff98f1f902013-01-19 18:46:13 -05002129 } 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 +00002130 if (!unlink(dir.c_str()))
Dees_Troy2673cec2013-04-02 20:22:16 +00002131 LOGINFO("Unable to unlink '%s'\n", dir.c_str());
Dees_Troyce675462013-01-09 19:48:21 +00002132 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002133 }
2134 closedir(d);
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05002135
Dees_Troy16b74352012-11-14 22:27:31 +00002136 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -04002137 }
Ethan Yonker74db1572015-10-28 12:44:49 -05002138 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy16b74352012-11-14 22:27:31 +00002139 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04002140}
2141
bigbiffce8f83c2015-12-12 18:30:21 -05002142bool TWPartition::Backup_Tar(PartitionSettings *part_settings, pid_t *tar_fork_pid) {
Ethan Yonker472f5062016-02-25 13:47:30 -06002143 string Full_FileName;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002144 twrpTar tar;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05002145
Dees_Troy43d8b002012-09-17 16:00:01 -04002146 if (!Mount(true))
2147 return false;
2148
Dees_Troya13d74f2013-03-24 08:54:55 -05002149 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Backup_Display_Name, "Backing Up");
Ethan Yonker74db1572015-10-28 12:44:49 -05002150 gui_msg(Msg("backing_up=Backing up {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002151
Ethan Yonker472f5062016-02-25 13:47:30 -06002152 DataManager::GetValue(TW_USE_COMPRESSION_VAR, tar.use_compression);
Matt Mowerbb81e5d2014-03-20 18:05:41 -05002153
Dees_Troy83bd4832013-05-04 12:39:56 +00002154#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
Ethan Yonker472f5062016-02-25 13:47:30 -06002155 if (Can_Encrypt_Backup) {
2156 DataManager::GetValue("tw_encrypt_backup", tar.use_encryption);
2157 if (tar.use_encryption) {
2158 if (Use_Userdata_Encryption)
2159 tar.userdata_encryption = tar.use_encryption;
2160 string Password;
2161 DataManager::GetValue("tw_backup_password", Password);
2162 tar.setpassword(Password);
2163 } else {
2164 tar.use_encryption = 0;
2165 }
Dees_Troy83bd4832013-05-04 12:39:56 +00002166 }
2167#endif
Dees_Troy43d8b002012-09-17 16:00:01 -04002168
Ethan Yonker472f5062016-02-25 13:47:30 -06002169 Backup_FileName = Backup_Name + "." + Current_File_System + ".win";
Ethan Yonkere080c1f2016-09-19 13:50:25 -05002170 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
Dees_Troy83bd4832013-05-04 12:39:56 +00002171 tar.has_data_media = Has_Data_Media;
bigbiffce8f83c2015-12-12 18:30:21 -05002172 tar.part_settings = part_settings;
Ethan Yonker3fdcda42016-11-30 12:29:37 -06002173 tar.backup_exclusions = &backup_exclusions;
Dees Troye0a433a2013-12-02 04:10:37 +00002174 tar.setdir(Backup_Path);
2175 tar.setfn(Full_FileName);
2176 tar.setsize(Backup_Size);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002177 tar.partition_name = Backup_Name;
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002178 tar.backup_folder = part_settings->Backup_Folder;
bigbiffce8f83c2015-12-12 18:30:21 -05002179 if (tar.createTarFork(tar_fork_pid) != 0)
Dees Troye0a433a2013-12-02 04:10:37 +00002180 return false;
Dees_Troy43d8b002012-09-17 16:00:01 -04002181 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002182}
2183
bigbiffce8f83c2015-12-12 18:30:21 -05002184bool TWPartition::Backup_Image(PartitionSettings *part_settings) {
2185 string Full_FileName, adb_file_name;
2186 int adb_control_bu_fd, compressed;
igoriok87e3d932013-01-31 21:03:53 +02002187
Ethan Yonker74db1572015-10-28 12:44:49 -05002188 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, gui_parse_text("{@backing}"));
2189 gui_msg(Msg("backing_up=Backing up {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002190
Ethan Yonker472f5062016-02-25 13:47:30 -06002191 Backup_FileName = Backup_Name + "." + Current_File_System + ".win";
Dees_Troy43d8b002012-09-17 16:00:01 -04002192
bigbiffce8f83c2015-12-12 18:30:21 -05002193 if (part_settings->adbbackup) {
2194 Full_FileName = TW_ADB_BACKUP;
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002195 adb_file_name = part_settings->Backup_Folder + "/" + Backup_FileName;
bigbiffce8f83c2015-12-12 18:30:21 -05002196 }
2197 else
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002198 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
bigbiffce8f83c2015-12-12 18:30:21 -05002199
2200 part_settings->total_restore_size = Backup_Size;
2201
2202 if (part_settings->adbbackup) {
2203 if (!twadbbu::Write_TWIMG(adb_file_name, Backup_Size))
2204 return false;
2205 }
2206
2207 if (!Raw_Read_Write(part_settings))
Ethan Yonker472f5062016-02-25 13:47:30 -06002208 return false;
2209
bigbiffce8f83c2015-12-12 18:30:21 -05002210 if (part_settings->adbbackup) {
2211 if (!twadbbu::Write_TWEOF())
2212 return false;
Dees_Troy7c2dec82012-09-26 09:49:14 -04002213 }
Dees_Troy43d8b002012-09-17 16:00:01 -04002214 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002215}
2216
bigbiffce8f83c2015-12-12 18:30:21 -05002217bool TWPartition::Raw_Read_Write(PartitionSettings *part_settings) {
2218 unsigned long long RW_Block_Size, Remain = Backup_Size;
2219 int src_fd = -1, dest_fd = -1;
2220 ssize_t bs;
Ethan Yonker472f5062016-02-25 13:47:30 -06002221 bool ret = false;
2222 void* buffer = NULL;
2223 unsigned long long backedup_size = 0;
bigbiffce8f83c2015-12-12 18:30:21 -05002224 string srcfn, destfn;
Ethan Yonker472f5062016-02-25 13:47:30 -06002225
bigbiffce8f83c2015-12-12 18:30:21 -05002226 if (part_settings->PM_Method == PM_BACKUP) {
2227 srcfn = Actual_Block_Device;
2228 if (part_settings->adbbackup)
2229 destfn = TW_ADB_BACKUP;
2230 else
Ethan Yonkere080c1f2016-09-19 13:50:25 -05002231 destfn = part_settings->Backup_Folder + "/" + Backup_FileName;
bigbiffce8f83c2015-12-12 18:30:21 -05002232 }
2233 else {
2234 destfn = Actual_Block_Device;
2235 if (part_settings->adbbackup) {
2236 srcfn = TW_ADB_RESTORE;
2237 } else {
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002238 srcfn = part_settings->Backup_Folder + "/" + Backup_FileName;
bigbiffce8f83c2015-12-12 18:30:21 -05002239 Remain = TWFunc::Get_File_Size(srcfn);
2240 }
2241 }
Ethan Yonker472f5062016-02-25 13:47:30 -06002242
bigbiffce8f83c2015-12-12 18:30:21 -05002243 src_fd = open(srcfn.c_str(), O_RDONLY | O_LARGEFILE);
Ethan Yonker472f5062016-02-25 13:47:30 -06002244 if (src_fd < 0) {
bigbiffce8f83c2015-12-12 18:30:21 -05002245 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(srcfn.c_str())(strerror(errno)));
Ethan Yonker472f5062016-02-25 13:47:30 -06002246 return false;
2247 }
bigbiffce8f83c2015-12-12 18:30:21 -05002248
2249 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 -06002250 if (dest_fd < 0) {
bigbiffce8f83c2015-12-12 18:30:21 -05002251 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(destfn.c_str())(strerror(errno)));
Ethan Yonker472f5062016-02-25 13:47:30 -06002252 goto exit;
2253 }
bigbiffce8f83c2015-12-12 18:30:21 -05002254
2255 LOGINFO("Reading '%s', writing '%s'\n", srcfn.c_str(), destfn.c_str());
2256
2257 if (part_settings->adbbackup) {
2258 RW_Block_Size = MAX_ADB_READ;
2259 bs = MAX_ADB_READ;
2260 }
2261 else {
2262 RW_Block_Size = 1048576LLU; // 1MB
2263 bs = (ssize_t)(RW_Block_Size);
2264 }
2265
Ethan Yonker472f5062016-02-25 13:47:30 -06002266 buffer = malloc((size_t)bs);
2267 if (!buffer) {
2268 LOGINFO("Raw_Read_Write failed to malloc\n");
2269 goto exit;
2270 }
bigbiffce8f83c2015-12-12 18:30:21 -05002271
2272 if (part_settings->progress)
2273 part_settings->progress->SetPartitionSize(part_settings->total_restore_size);
2274
Ethan Yonker472f5062016-02-25 13:47:30 -06002275 while (Remain > 0) {
2276 if (Remain < RW_Block_Size)
bigbiffce8f83c2015-12-12 18:30:21 -05002277 bs = (ssize_t)(Remain);
2278 if (read(src_fd, buffer, bs) != bs) {
Ethan Yonker472f5062016-02-25 13:47:30 -06002279 LOGINFO("Error reading source fd (%s)\n", strerror(errno));
2280 goto exit;
2281 }
2282 if (write(dest_fd, buffer, bs) != bs) {
2283 LOGINFO("Error writing destination fd (%s)\n", strerror(errno));
2284 goto exit;
2285 }
2286 backedup_size += (unsigned long long)(bs);
bigbiffce8f83c2015-12-12 18:30:21 -05002287 Remain = Remain - (unsigned long long)(bs);
2288 if (part_settings->progress)
2289 part_settings->progress->UpdateSize(backedup_size);
Ethan Yonker472f5062016-02-25 13:47:30 -06002290 if (PartitionManager.Check_Backup_Cancel() != 0)
2291 goto exit;
2292 }
bigbiffce8f83c2015-12-12 18:30:21 -05002293 if (part_settings->progress)
2294 part_settings->progress->UpdateDisplayDetails(true);
Ethan Yonker472f5062016-02-25 13:47:30 -06002295 fsync(dest_fd);
2296 ret = true;
2297exit:
2298 if (src_fd >= 0)
2299 close(src_fd);
2300 if (dest_fd >= 0)
2301 close(dest_fd);
2302 if (buffer)
2303 free(buffer);
2304 return ret;
2305}
2306
bigbiffce8f83c2015-12-12 18:30:21 -05002307bool TWPartition::Backup_Dump_Image(PartitionSettings *part_settings) {
Vojtech Bocek05534202013-09-11 08:11:56 +02002308 string Full_FileName, Command;
bigbiffce8f83c2015-12-12 18:30:21 -05002309 int use_compression, adb_control_bu_fd;
2310 unsigned long long compressed;
Dees_Troy43d8b002012-09-17 16:00:01 -04002311
Ethan Yonker74db1572015-10-28 12:44:49 -05002312 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, gui_parse_text("{@backing}"));
2313 gui_msg(Msg("backing_up=Backing up {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002314
bigbiffce8f83c2015-12-12 18:30:21 -05002315 if (part_settings->progress)
2316 part_settings->progress->SetPartitionSize(Backup_Size);
Dees_Troy43d8b002012-09-17 16:00:01 -04002317
Ethan Yonker472f5062016-02-25 13:47:30 -06002318 Backup_FileName = Backup_Name + "." + Current_File_System + ".win";
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002319 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04002320
2321 Command = "dump_image " + MTD_Name + " '" + Full_FileName + "'";
bigbiffce8f83c2015-12-12 18:30:21 -05002322
Dees_Troy2673cec2013-04-02 20:22:16 +00002323 LOGINFO("Backup command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02002324 TWFunc::Exec_Cmd(Command);
Ethan Yonker4b94cfd2014-12-11 10:00:45 -06002325 tw_set_default_metadata(Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04002326 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
2327 // 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 -06002328 gui_msg(Msg(msg::kError, "backup_size=Backup file size for '{1}' is 0 bytes.")(Full_FileName));
Dees_Troy7c2dec82012-09-26 09:49:14 -04002329 return false;
2330 }
bigbiffce8f83c2015-12-12 18:30:21 -05002331 if (part_settings->progress)
2332 part_settings->progress->UpdateSize(Backup_Size);
2333
Dees_Troy43d8b002012-09-17 16:00:01 -04002334 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002335}
2336
bigbiffce8f83c2015-12-12 18:30:21 -05002337unsigned long long TWPartition::Get_Restore_Size(PartitionSettings *part_settings) {
2338 if (!part_settings->adbbackup) {
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002339 InfoManager restore_info(part_settings->Backup_Folder + "/" + Backup_Name + ".info");
bigbiffce8f83c2015-12-12 18:30:21 -05002340 if (restore_info.LoadValues() == 0) {
2341 if (restore_info.GetValue("backup_size", Restore_Size) == 0) {
2342 LOGINFO("Read info file, restore size is %llu\n", Restore_Size);
2343 return Restore_Size;
2344 }
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002345 }
2346 }
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002347
bigbiffce8f83c2015-12-12 18:30:21 -05002348 string Full_FileName, Restore_File_System = Get_Restore_File_System(part_settings);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002349
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002350 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002351 if (Is_Image(Restore_File_System)) {
2352 Restore_Size = TWFunc::Get_File_Size(Full_FileName);
2353 return Restore_Size;
2354 }
2355
2356 twrpTar tar;
2357 tar.setdir(Backup_Path);
2358 tar.setfn(Full_FileName);
bigbiffce8f83c2015-12-12 18:30:21 -05002359 tar.backup_name = Full_FileName;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002360#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
2361 string Password;
2362 DataManager::GetValue("tw_restore_password", Password);
2363 if (!Password.empty())
2364 tar.setpassword(Password);
2365#endif
2366 tar.partition_name = Backup_Name;
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002367 tar.backup_folder = part_settings->Backup_Folder;
bigbiffce8f83c2015-12-12 18:30:21 -05002368 tar.part_settings = part_settings;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002369 Restore_Size = tar.get_size();
2370 return Restore_Size;
2371}
2372
bigbiffce8f83c2015-12-12 18:30:21 -05002373bool TWPartition::Restore_Tar(PartitionSettings *part_settings) {
Ethan Yonker472f5062016-02-25 13:47:30 -06002374 string Full_FileName;
Dees Troy4159aed2014-02-28 17:24:43 +00002375 bool ret = false;
bigbiffce8f83c2015-12-12 18:30:21 -05002376 string Restore_File_System = Get_Restore_File_System(part_settings);
Dees_Troy43d8b002012-09-17 16:00:01 -04002377
Dees_Troye58d5262012-09-21 12:27:57 -04002378 if (Has_Android_Secure) {
Dees_Troye58d5262012-09-21 12:27:57 -04002379 if (!Wipe_AndSec())
2380 return false;
Gary Peck43acadf2012-11-21 21:19:01 -08002381 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002382 gui_msg(Msg("wiping=Wiping {1}")(Backup_Display_Name));
Ethan Yonker5eac2222014-06-11 12:22:55 -05002383 if (Has_Data_Media && Mount_Point == "/data" && Restore_File_System != Current_File_System) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002384 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 -05002385 if (!Wipe_Data_Without_Wiping_Media())
2386 return false;
2387 } else {
2388 if (!Wipe(Restore_File_System))
2389 return false;
2390 }
Dees_Troye58d5262012-09-21 12:27:57 -04002391 }
Matt Mower3c366972015-12-25 19:28:31 -06002392 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Backup_Display_Name, gui_parse_text("{@restoring_hdr}"));
Ethan Yonker74db1572015-10-28 12:44:49 -05002393 gui_msg(Msg("restoring=Restoring {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002394
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05002395 // Remount as read/write as needed so we can restore the backup
2396 if (!ReMount_RW(true))
Dees_Troy43d8b002012-09-17 16:00:01 -04002397 return false;
2398
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002399 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
Ethan Yonker87af5632014-02-10 11:56:35 -06002400 twrpTar tar;
bigbiffce8f83c2015-12-12 18:30:21 -05002401 tar.part_settings = part_settings;
Ethan Yonker87af5632014-02-10 11:56:35 -06002402 tar.setdir(Backup_Path);
2403 tar.setfn(Full_FileName);
2404 tar.backup_name = Backup_Name;
2405#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
2406 string Password;
2407 DataManager::GetValue("tw_restore_password", Password);
2408 if (!Password.empty())
2409 tar.setpassword(Password);
2410#endif
bigbiffce8f83c2015-12-12 18:30:21 -05002411 part_settings->progress->SetPartitionSize(Get_Restore_Size(part_settings));
2412 if (tar.extractTarFork() != 0)
Dees Troy4159aed2014-02-28 17:24:43 +00002413 ret = false;
2414 else
2415 ret = true;
2416#ifdef HAVE_CAPABILITIES
2417 // Restore capabilities to the run-as binary
2418 if (Mount_Point == "/system" && Mount(true) && TWFunc::Path_Exists("/system/bin/run-as")) {
2419 struct vfs_cap_data cap_data;
2420 uint64_t capabilities = (1 << CAP_SETUID) | (1 << CAP_SETGID);
2421
2422 memset(&cap_data, 0, sizeof(cap_data));
2423 cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE;
2424 cap_data.data[0].permitted = (uint32_t) (capabilities & 0xffffffff);
2425 cap_data.data[0].inheritable = 0;
2426 cap_data.data[1].permitted = (uint32_t) (capabilities >> 32);
2427 cap_data.data[1].inheritable = 0;
2428 if (setxattr("/system/bin/run-as", XATTR_NAME_CAPS, &cap_data, sizeof(cap_data), 0) < 0) {
2429 LOGINFO("Failed to reset capabilities of /system/bin/run-as binary.\n");
2430 } else {
2431 LOGINFO("Reset capabilities of /system/bin/run-as binary successful.\n");
2432 }
2433 }
2434#endif
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05002435 if (Mount_Read_Only || Mount_Flags & MS_RDONLY)
2436 // Remount as read only when restoration is complete
2437 ReMount(true);
2438
Dees Troy4159aed2014-02-28 17:24:43 +00002439 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04002440}
2441
bigbiffce8f83c2015-12-12 18:30:21 -05002442bool TWPartition::Restore_Image(PartitionSettings *part_settings) {
Ethan Yonker96af84a2015-01-05 14:58:36 -06002443 string Full_FileName;
bigbiffce8f83c2015-12-12 18:30:21 -05002444 string Restore_File_System = Get_Restore_File_System(part_settings);
Dees_Troy43d8b002012-09-17 16:00:01 -04002445
Matt Mower3c366972015-12-25 19:28:31 -06002446 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Backup_Display_Name, gui_parse_text("{@restoring_hdr}"));
Ethan Yonker74db1572015-10-28 12:44:49 -05002447 gui_msg(Msg("restoring=Restoring {1}...")(Backup_Display_Name));
bigbiffce8f83c2015-12-12 18:30:21 -05002448
2449 if (part_settings->adbbackup)
2450 Full_FileName = TW_ADB_RESTORE;
2451 else
Ethan Yonkere080c1f2016-09-19 13:50:25 -05002452 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
Gary Peck15e623d2012-11-21 21:07:58 -08002453
Ethan Yonker96af84a2015-01-05 14:58:36 -06002454 if (Restore_File_System == "emmc") {
bigbiffce8f83c2015-12-12 18:30:21 -05002455 if (!part_settings->adbbackup)
2456 part_settings->total_restore_size = (uint64_t)(TWFunc::Get_File_Size(Full_FileName));
2457 if (!Raw_Read_Write(part_settings))
Ethan Yonker96af84a2015-01-05 14:58:36 -06002458 return false;
2459 } else if (Restore_File_System == "mtd" || Restore_File_System == "bml") {
bigbiffce8f83c2015-12-12 18:30:21 -05002460 if (!Flash_Image_FI(Full_FileName, part_settings->progress))
2461 return false;
2462 }
2463
2464 if (part_settings->adbbackup) {
2465 if (!twadbbu::Write_TWEOF())
Ethan Yonker96af84a2015-01-05 14:58:36 -06002466 return false;
Gary Peck15e623d2012-11-21 21:07:58 -08002467 }
Dees_Troy43d8b002012-09-17 16:00:01 -04002468 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002469}
Dees_Troy5bf43922012-09-07 16:07:55 -04002470
2471bool TWPartition::Update_Size(bool Display_Error) {
Dees_Troy0550cfb2012-10-13 11:56:13 -04002472 bool ret = false, Was_Already_Mounted = false;
Dees_Troy51127312012-09-08 13:08:49 -04002473
Ethan Yonker1b190162016-12-05 15:25:19 -06002474 Find_Actual_Block_Device();
2475
2476 if (!Can_Be_Mounted && !Is_Encrypted) {
2477 if (TWFunc::Path_Exists(Actual_Block_Device) && Find_Partition_Size()) {
2478 Used = Size;
2479 Backup_Size = Size;
2480 return true;
2481 }
Dees_Troy5bf43922012-09-07 16:07:55 -04002482 return false;
Ethan Yonker1b190162016-12-05 15:25:19 -06002483 }
Dees_Troy5bf43922012-09-07 16:07:55 -04002484
Dees_Troy0550cfb2012-10-13 11:56:13 -04002485 Was_Already_Mounted = Is_Mounted();
Dees_Troy38bd7602012-09-14 13:33:53 -04002486 if (Removable || Is_Encrypted) {
2487 if (!Mount(false))
2488 return true;
2489 } else if (!Mount(Display_Error))
Dees_Troy5bf43922012-09-07 16:07:55 -04002490 return false;
Dees_Troy51127312012-09-08 13:08:49 -04002491
2492 ret = Get_Size_Via_statfs(Display_Error);
Dees_Troy0550cfb2012-10-13 11:56:13 -04002493 if (!ret || Size == 0) {
2494 if (!Get_Size_Via_df(Display_Error)) {
2495 if (!Was_Already_Mounted)
2496 UnMount(false);
Dees_Troy51127312012-09-08 13:08:49 -04002497 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04002498 }
2499 }
Dees_Troy51127312012-09-08 13:08:49 -04002500
Dees_Troy5bf43922012-09-07 16:07:55 -04002501 if (Has_Data_Media) {
2502 if (Mount(Display_Error)) {
Dees_Troy51127312012-09-08 13:08:49 -04002503 unsigned long long data_media_used, actual_data;
Ethan Yonker3fdcda42016-11-30 12:29:37 -06002504 Used = backup_exclusions.Get_Folder_Size(Mount_Point);
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002505 Backup_Size = Used;
2506 int bak = (int)(Used / 1048576LLU);
Dees_Troy51127312012-09-08 13:08:49 -04002507 int fre = (int)(Free / 1048576LLU);
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002508 LOGINFO("Data backup size is %iMB, free: %iMB.\n", bak, fre);
Dees_Troy0550cfb2012-10-13 11:56:13 -04002509 } else {
2510 if (!Was_Already_Mounted)
2511 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04002512 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04002513 }
Dees_Troye58d5262012-09-21 12:27:57 -04002514 } else if (Has_Android_Secure) {
2515 if (Mount(Display_Error))
Ethan Yonker3fdcda42016-11-30 12:29:37 -06002516 Backup_Size = backup_exclusions.Get_Folder_Size(Backup_Path);
Dees_Troy0550cfb2012-10-13 11:56:13 -04002517 else {
2518 if (!Was_Already_Mounted)
2519 UnMount(false);
Dees_Troye58d5262012-09-21 12:27:57 -04002520 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04002521 }
Dees_Troy5bf43922012-09-07 16:07:55 -04002522 }
Dees_Troy0550cfb2012-10-13 11:56:13 -04002523 if (!Was_Already_Mounted)
2524 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04002525 return true;
Dees_Troy51127312012-09-08 13:08:49 -04002526}
Dees_Troy38bd7602012-09-14 13:33:53 -04002527
2528void TWPartition::Find_Actual_Block_Device(void) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002529 if (Is_Decrypted && !Decrypted_Block_Device.empty()) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002530 Actual_Block_Device = Decrypted_Block_Device;
Ethan Yonker1b190162016-12-05 15:25:19 -06002531 if (TWFunc::Path_Exists(Decrypted_Block_Device)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002532 Is_Present = true;
Ethan Yonker1b190162016-12-05 15:25:19 -06002533 return;
2534 }
2535 } else if (SlotSelect && TWFunc::Path_Exists(Primary_Block_Device + PartitionManager.Get_Active_Slot_Suffix())) {
2536 Actual_Block_Device = Primary_Block_Device + PartitionManager.Get_Active_Slot_Suffix();
2537 unlink(Primary_Block_Device.c_str());
2538 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
2539 Is_Present = true;
2540 return;
Dees_Troy43d8b002012-09-17 16:00:01 -04002541 } else if (TWFunc::Path_Exists(Primary_Block_Device)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002542 Is_Present = true;
2543 Actual_Block_Device = Primary_Block_Device;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002544 return;
2545 }
Ethan Yonker1b190162016-12-05 15:25:19 -06002546 if (!Alternate_Block_Device.empty() && TWFunc::Path_Exists(Alternate_Block_Device)) {
Dees_Troy3f04d032012-10-07 18:20:09 -04002547 Actual_Block_Device = Alternate_Block_Device;
Dees_Troy38bd7602012-09-14 13:33:53 -04002548 Is_Present = true;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002549 } else {
Dees_Troy38bd7602012-09-14 13:33:53 -04002550 Is_Present = false;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002551 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002552}
2553
2554void TWPartition::Recreate_Media_Folder(void) {
2555 string Command;
Ethan Yonker66a19492015-12-10 10:19:45 -06002556 string Media_Path = Mount_Point + "/media";
Dees_Troy38bd7602012-09-14 13:33:53 -04002557
2558 if (!Mount(true)) {
Ethan Yonker66a19492015-12-10 10:19:45 -06002559 gui_msg(Msg(msg::kError, "recreate_folder_err=Unable to recreate {1} folder.")(Media_Path));
2560 } else if (!TWFunc::Path_Exists(Media_Path)) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002561 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Ethan Yonker66a19492015-12-10 10:19:45 -06002562 LOGINFO("Recreating %s folder.\n", Media_Path.c_str());
2563 mkdir(Media_Path.c_str(), 0770);
xiaolu9416f4f2015-06-04 08:22:23 +08002564 string Internal_path = DataManager::GetStrValue("tw_internal_path");
2565 if (!Internal_path.empty()) {
2566 LOGINFO("Recreating %s folder.\n", Internal_path.c_str());
2567 mkdir(Internal_path.c_str(), 0770);
2568 }
2569#ifdef TW_INTERNAL_STORAGE_PATH
2570 mkdir(EXPAND(TW_INTERNAL_STORAGE_PATH), 0770);
2571#endif
Ethan Yonker5ef301e2014-10-14 10:04:44 -05002572#ifdef HAVE_SELINUX
thata3d31fb2014-12-21 22:27:40 +01002573 // Afterwards, we will try to set the
2574 // default metadata that we were hopefully able to get during
2575 // early boot.
Ethan Yonker66a19492015-12-10 10:19:45 -06002576 tw_set_default_metadata(Media_Path.c_str());
xiaolu9416f4f2015-06-04 08:22:23 +08002577 if (!Internal_path.empty())
2578 tw_set_default_metadata(Internal_path.c_str());
Ethan Yonker5ef301e2014-10-14 10:04:44 -05002579#endif
Ethan Yonker5eac2222014-06-11 12:22:55 -05002580 // Toggle mount to ensure that "internal sdcard" gets mounted
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002581 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Ethan Yonker5eac2222014-06-11 12:22:55 -05002582 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Dees_Troy38bd7602012-09-14 13:33:53 -04002583 }
Dees_Troy43d8b002012-09-17 16:00:01 -04002584}
Dees_Troye58d5262012-09-21 12:27:57 -04002585
2586void TWPartition::Recreate_AndSec_Folder(void) {
Dees_Troye58d5262012-09-21 12:27:57 -04002587 if (!Has_Android_Secure)
2588 return;
Dees_Troy2673cec2013-04-02 20:22:16 +00002589 LOGINFO("Creating %s: %s\n", Backup_Display_Name.c_str(), Symlink_Path.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -04002590 if (!Mount(true)) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002591 gui_msg(Msg(msg::kError, "recreate_folder_err=Unable to recreate {1} folder.")(Backup_Name));
Dees_Troye58d5262012-09-21 12:27:57 -04002592 } else if (!TWFunc::Path_Exists(Symlink_Path)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00002593 LOGINFO("Recreating %s folder.\n", Backup_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002594 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05002595 mkdir(Symlink_Path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002596 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04002597 }
2598}
bigbiff7cb4c332014-11-26 20:36:07 -05002599
2600uint64_t TWPartition::Get_Max_FileSize() {
2601 uint64_t maxFileSize = 0;
2602 const uint64_t constGB = (uint64_t) 1024 * 1024 * 1024;
2603 const uint64_t constTB = (uint64_t) constGB * 1024;
2604 const uint64_t constPB = (uint64_t) constTB * 1024;
2605 const uint64_t constEB = (uint64_t) constPB * 1024;
bigbiff0c532032014-12-21 13:41:26 -05002606 if (Current_File_System == "ext4")
2607 maxFileSize = 16 * constTB; //16 TB
2608 else if (Current_File_System == "vfat")
2609 maxFileSize = 4 * constGB; //4 GB
2610 else if (Current_File_System == "ntfs")
2611 maxFileSize = 256 * constTB; //256 TB
2612 else if (Current_File_System == "exfat")
2613 maxFileSize = 16 * constPB; //16 PB
2614 else if (Current_File_System == "ext3")
2615 maxFileSize = 2 * constTB; //2 TB
2616 else if (Current_File_System == "f2fs")
2617 maxFileSize = 3.94 * constTB; //3.94 TB
bigbiff7cb4c332014-11-26 20:36:07 -05002618 else
2619 maxFileSize = 100000000L;
2620 return maxFileSize - 1;
2621}
2622
bigbiffce8f83c2015-12-12 18:30:21 -05002623bool TWPartition::Flash_Image(PartitionSettings *part_settings) {
2624 string Restore_File_System, full_filename;
Ethan Yonker96af84a2015-01-05 14:58:36 -06002625
Ethan Yonkere080c1f2016-09-19 13:50:25 -05002626 full_filename = part_settings->Backup_Folder + "/" + Backup_FileName;
Ethan Yonker96af84a2015-01-05 14:58:36 -06002627
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002628 LOGINFO("Image filename is: %s\n", Backup_FileName.c_str());
bigbiffce8f83c2015-12-12 18:30:21 -05002629
2630 if (Backup_Method == BM_FILES) {
Ethan Yonker96af84a2015-01-05 14:58:36 -06002631 LOGERR("Cannot flash images to file systems\n");
2632 return false;
2633 } else if (!Can_Flash_Img) {
2634 LOGERR("Cannot flash images to partitions %s\n", Display_Name.c_str());
2635 return false;
2636 } else {
2637 if (!Find_Partition_Size()) {
2638 LOGERR("Unable to find partition size for '%s'\n", Mount_Point.c_str());
2639 return false;
2640 }
bigbiffce8f83c2015-12-12 18:30:21 -05002641 unsigned long long image_size = TWFunc::Get_File_Size(full_filename);
Ethan Yonker96af84a2015-01-05 14:58:36 -06002642 if (image_size > Size) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002643 LOGINFO("Size (%llu bytes) of image '%s' is larger than target device '%s' (%llu bytes)\n",
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002644 image_size, Backup_FileName.c_str(), Actual_Block_Device.c_str(), Size);
Ethan Yonker74db1572015-10-28 12:44:49 -05002645 gui_err("img_size_err=Size of image is larger than target device");
Ethan Yonker96af84a2015-01-05 14:58:36 -06002646 return false;
2647 }
bigbiffce8f83c2015-12-12 18:30:21 -05002648 if (Backup_Method == BM_DD) {
2649 if (!part_settings->adbbackup) {
2650 if (Is_Sparse_Image(full_filename)) {
2651 return Flash_Sparse_Image(full_filename);
2652 }
Ethan Yonker472f5062016-02-25 13:47:30 -06002653 }
bigbiffce8f83c2015-12-12 18:30:21 -05002654 unsigned long long file_size = (unsigned long long)(TWFunc::Get_File_Size(full_filename));
2655 return Raw_Read_Write(part_settings);
2656 } else if (Backup_Method == BM_FLASH_UTILS) {
2657 return Flash_Image_FI(full_filename, NULL);
Ethan Yonker472f5062016-02-25 13:47:30 -06002658 }
Ethan Yonker96af84a2015-01-05 14:58:36 -06002659 }
2660
2661 LOGERR("Unknown flash method for '%s'\n", Mount_Point.c_str());
2662 return false;
2663}
2664
Ethan Yonker472f5062016-02-25 13:47:30 -06002665bool TWPartition::Is_Sparse_Image(const string& Filename) {
HashBanged974bb2016-01-30 14:20:09 -05002666 uint32_t magic = 0;
2667 int fd = open(Filename.c_str(), O_RDONLY);
2668 if (fd < 0) {
2669 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Filename)(strerror(errno)));
2670 return false;
2671 }
bigbiffce8f83c2015-12-12 18:30:21 -05002672
HashBanged974bb2016-01-30 14:20:09 -05002673 if (read(fd, &magic, sizeof(magic)) != sizeof(magic)) {
2674 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Filename)(strerror(errno)));
2675 close(fd);
2676 return false;
2677 }
2678 close(fd);
Ethan Yonker472f5062016-02-25 13:47:30 -06002679 if (magic == SPARSE_HEADER_MAGIC)
2680 return true;
2681 return false;
2682}
2683
2684bool TWPartition::Flash_Sparse_Image(const string& Filename) {
2685 string Command;
2686
2687 gui_msg(Msg("flashing=Flashing {1}...")(Display_Name));
2688
2689 Command = "simg2img '" + Filename + "' '" + Actual_Block_Device + "'";
Ethan Yonker96af84a2015-01-05 14:58:36 -06002690 LOGINFO("Flash command: '%s'\n", Command.c_str());
2691 TWFunc::Exec_Cmd(Command);
2692 return true;
2693}
2694
Ethan Yonker472f5062016-02-25 13:47:30 -06002695bool TWPartition::Flash_Image_FI(const string& Filename, ProgressTracking *progress) {
Ethan Yonker96af84a2015-01-05 14:58:36 -06002696 string Command;
Ethan Yonker472f5062016-02-25 13:47:30 -06002697 unsigned long long file_size;
Ethan Yonker96af84a2015-01-05 14:58:36 -06002698
Ethan Yonker74db1572015-10-28 12:44:49 -05002699 gui_msg(Msg("flashing=Flashing {1}...")(Display_Name));
Ethan Yonker472f5062016-02-25 13:47:30 -06002700 if (progress) {
2701 file_size = (unsigned long long)(TWFunc::Get_File_Size(Filename));
2702 progress->SetPartitionSize(file_size);
2703 }
Ethan Yonker96af84a2015-01-05 14:58:36 -06002704 // Sometimes flash image doesn't like to flash due to the first 2KB matching, so we erase first to ensure that it flashes
2705 Command = "erase_image " + MTD_Name;
2706 LOGINFO("Erase command: '%s'\n", Command.c_str());
2707 TWFunc::Exec_Cmd(Command);
2708 Command = "flash_image " + MTD_Name + " '" + Filename + "'";
2709 LOGINFO("Flash command: '%s'\n", Command.c_str());
2710 TWFunc::Exec_Cmd(Command);
Ethan Yonker472f5062016-02-25 13:47:30 -06002711 if (progress)
2712 progress->UpdateSize(file_size);
Ethan Yonker96af84a2015-01-05 14:58:36 -06002713 return true;
2714}
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05002715
2716void TWPartition::Change_Mount_Read_Only(bool new_value) {
2717 Mount_Read_Only = new_value;
2718}
2719
bigbiffce8f83c2015-12-12 18:30:21 -05002720bool TWPartition::Is_Read_Only() {
2721 return Mount_Read_Only;
2722}
2723
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05002724int TWPartition::Check_Lifetime_Writes() {
2725 bool original_read_only = Mount_Read_Only;
2726 int ret = 1;
2727
2728 Mount_Read_Only = true;
2729 if (Mount(false)) {
2730 Find_Actual_Block_Device();
2731 string block = basename(Actual_Block_Device.c_str());
2732 string file = "/sys/fs/" + Current_File_System + "/" + block + "/lifetime_write_kbytes";
2733 string result;
2734 if (TWFunc::Path_Exists(file)) {
2735 if (TWFunc::read_file(file, result) != 0) {
2736 LOGINFO("Check_Lifetime_Writes of '%s' failed to read_file\n", file.c_str());
2737 } else {
2738 LOGINFO("Check_Lifetime_Writes result: '%s'\n", result.c_str());
2739 if (result == "0") {
2740 ret = 0;
2741 }
2742 }
2743 } else {
2744 LOGINFO("Check_Lifetime_Writes file does not exist '%s'\n", file.c_str());
2745 }
2746 UnMount(true);
2747 } else {
2748 LOGINFO("Check_Lifetime_Writes failed to mount '%s'\n", Mount_Point.c_str());
2749 }
2750 Mount_Read_Only = original_read_only;
2751 return ret;
2752}
Ethan Yonker66a19492015-12-10 10:19:45 -06002753
2754int TWPartition::Decrypt_Adopted() {
2755#ifdef TW_INCLUDE_CRYPTO
2756 int ret = 1;
2757 Is_Adopted_Storage = false;
2758 string Adopted_Key_File = "";
2759
2760 if (!Removable)
2761 return ret;
2762
2763 int fd = open(Alternate_Block_Device.c_str(), O_RDONLY);
2764 if (fd < 0) {
2765 LOGINFO("failed to open '%s'\n", Alternate_Block_Device.c_str());
2766 return ret;
2767 }
2768 char type_guid[80];
2769 char part_guid[80];
2770
2771 if (gpt_disk_get_partition_info(fd, 2, type_guid, part_guid) == 0) {
2772 LOGINFO("type: '%s'\n", type_guid);
2773 LOGINFO("part: '%s'\n", part_guid);
2774 Adopted_GUID = part_guid;
2775 LOGINFO("Adopted_GUID '%s'\n", Adopted_GUID.c_str());
2776 if (strcmp(type_guid, TWGptAndroidExpand) == 0) {
2777 LOGINFO("android_expand found\n");
2778 Adopted_Key_File = "/data/misc/vold/expand_";
2779 Adopted_Key_File += part_guid;
2780 Adopted_Key_File += ".key";
2781 if (TWFunc::Path_Exists(Adopted_Key_File)) {
2782 Is_Adopted_Storage = true;
2783 /* Until we find a use case for this, I think it is safe
2784 * to disable USB Mass Storage whenever adopted storage
2785 * is present.
2786 */
2787 LOGINFO("Detected adopted storage, disabling USB mass storage mode\n");
2788 DataManager::SetValue("tw_has_usb_storage", 0);
2789 }
2790 }
2791 }
2792
2793 if (Is_Adopted_Storage) {
2794 string Adopted_Block_Device = Alternate_Block_Device + "p2";
2795 if (!TWFunc::Path_Exists(Adopted_Block_Device)) {
2796 Adopted_Block_Device = Alternate_Block_Device + "2";
2797 if (!TWFunc::Path_Exists(Adopted_Block_Device)) {
2798 LOGINFO("Adopted block device does not exist\n");
2799 goto exit;
2800 }
2801 }
2802 LOGINFO("key file is '%s', block device '%s'\n", Adopted_Key_File.c_str(), Adopted_Block_Device.c_str());
2803 char crypto_blkdev[MAXPATHLEN];
2804 std::string thekey;
2805 int fdkey = open(Adopted_Key_File.c_str(), O_RDONLY);
2806 if (fdkey < 0) {
2807 LOGINFO("failed to open key file\n");
2808 goto exit;
2809 }
2810 char buf[512];
2811 ssize_t n;
2812 while ((n = read(fdkey, &buf[0], sizeof(buf))) > 0) {
2813 thekey.append(buf, n);
2814 }
2815 close(fdkey);
2816 unsigned char* key = (unsigned char*) thekey.data();
2817 cryptfs_revert_ext_volume(part_guid);
2818
2819 ret = cryptfs_setup_ext_volume(part_guid, Adopted_Block_Device.c_str(), key, thekey.size(), crypto_blkdev);
2820 if (ret == 0) {
2821 LOGINFO("adopted storage new block device: '%s'\n", crypto_blkdev);
2822 Decrypted_Block_Device = crypto_blkdev;
2823 Is_Decrypted = true;
2824 Is_Encrypted = true;
2825 Find_Actual_Block_Device();
Kjell Braden3126a112016-06-19 16:58:15 +00002826 if (!Mount_Storage_Retry(false)) {
Ethan Yonker66a19492015-12-10 10:19:45 -06002827 LOGERR("Failed to mount decrypted adopted storage device\n");
2828 Is_Decrypted = false;
2829 Is_Encrypted = false;
2830 cryptfs_revert_ext_volume(part_guid);
2831 ret = 1;
2832 } else {
Ethan Yonkerfcf3f242016-02-16 12:30:26 -06002833 UnMount(false);
2834 Has_Android_Secure = false;
2835 Symlink_Path = "";
2836 Symlink_Mount_Point = "";
2837 Backup_Name = Mount_Point.substr(1);
2838 Backup_Path = Mount_Point;
2839 TWPartition* sdext = PartitionManager.Find_Partition_By_Path("/sd-ext");
2840 if (sdext && sdext->Actual_Block_Device == Adopted_Block_Device) {
2841 LOGINFO("Removing /sd-ext from partition list due to adopted storage\n");
2842 PartitionManager.Remove_Partition_By_Path("/sd-ext");
2843 }
Ethan Yonker66a19492015-12-10 10:19:45 -06002844 Setup_Data_Media();
2845 Recreate_Media_Folder();
2846 Wipe_Available_in_GUI = true;
2847 Wipe_During_Factory_Reset = true;
2848 Can_Be_Backed_Up = true;
2849 Can_Encrypt_Backup = true;
2850 Use_Userdata_Encryption = true;
2851 Is_Storage = true;
2852 Storage_Name = "Adopted Storage";
2853 Is_SubPartition = true;
2854 SubPartition_Of = "/data";
2855 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
2856 DataManager::SetValue("tw_has_adopted_storage", 1);
2857 }
2858 } else {
2859 LOGERR("Failed to setup adopted storage decryption\n");
2860 }
2861 }
2862exit:
2863 return ret;
2864#else
2865 LOGINFO("Decrypt_Adopted: no crypto support\n");
2866 return 1;
2867#endif
2868}
2869
2870void TWPartition::Revert_Adopted() {
2871#ifdef TW_INCLUDE_CRYPTO
2872 if (!Adopted_GUID.empty()) {
2873 PartitionManager.Remove_MTP_Storage(Mount_Point);
2874 UnMount(false);
2875 cryptfs_revert_ext_volume(Adopted_GUID.c_str());
2876 Is_Adopted_Storage = false;
2877 Is_Encrypted = false;
2878 Is_Decrypted = false;
2879 Decrypted_Block_Device = "";
2880 Find_Actual_Block_Device();
2881 Wipe_During_Factory_Reset = false;
2882 Can_Be_Backed_Up = false;
2883 Can_Encrypt_Backup = false;
2884 Use_Userdata_Encryption = false;
2885 Is_SubPartition = false;
2886 SubPartition_Of = "";
2887 Has_Data_Media = false;
2888 Storage_Path = Mount_Point;
2889 if (!Symlink_Mount_Point.empty()) {
2890 TWPartition* Dat = PartitionManager.Find_Partition_By_Path("/data");
2891 if (Dat) {
2892 Dat->UnMount(false);
2893 Dat->Symlink_Mount_Point = Symlink_Mount_Point;
2894 }
2895 Symlink_Mount_Point = "";
2896 }
2897 }
2898#else
2899 LOGINFO("Revert_Adopted: no crypto support\n");
2900#endif
2901}