blob: 78b11ecab5147773ee8e0faae8258a9d7dd2327d [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,
142};
143
144/* Flags without a trailing '=' are considered dual format flags and can be
145 * written as either 'flagname' or 'flagname=', where the character following
146 * the '=' is Y,y,1 for true and false otherwise.
147 */
148const struct flag_list tw_flags[] = {
149 { "andsec", TWFLAG_ANDSEC },
150 { "backup", TWFLAG_BACKUP },
151 { "backupname=", TWFLAG_BACKUPNAME },
152 { "blocksize=", TWFLAG_BLOCKSIZE },
153 { "canbewiped", TWFLAG_CANBEWIPED },
154 { "canencryptbackup", TWFLAG_CANENCRYPTBACKUP },
155 { "defaults", TWFLAG_DEFAULTS },
156 { "display=", TWFLAG_DISPLAY },
157 { "encryptable=", TWFLAG_ENCRYPTABLE },
158 { "flashimg", TWFLAG_FLASHIMG },
159 { "forceencrypt=", TWFLAG_FORCEENCRYPT },
160 { "fsflags=", TWFLAG_FSFLAGS },
161 { "ignoreblkid", TWFLAG_IGNOREBLKID },
162 { "length=", TWFLAG_LENGTH },
163 { "mounttodecrypt", TWFLAG_MOUNTTODECRYPT },
164 { "removable", TWFLAG_REMOVABLE },
165 { "retainlayoutversion", TWFLAG_RETAINLAYOUTVERSION },
166 { "settingsstorage", TWFLAG_SETTINGSSTORAGE },
167 { "storage", TWFLAG_STORAGE },
168 { "storagename=", TWFLAG_STORAGENAME },
169 { "subpartitionof=", TWFLAG_SUBPARTITIONOF },
170 { "symlink=", TWFLAG_SYMLINK },
171 { "userdataencryptbackup", TWFLAG_USERDATAENCRYPTBACKUP },
172 { "usermrf", TWFLAG_USERMRF },
173 { "wipeduringfactoryreset", TWFLAG_WIPEDURINGFACTORYRESET },
174 { "wipeingui", TWFLAG_WIPEINGUI },
175 { 0, 0 },
176};
177
that9e0593e2014-10-08 00:01:24 +0200178TWPartition::TWPartition() {
Dees_Troy51a0e822012-09-05 15:24:24 -0400179 Can_Be_Mounted = false;
180 Can_Be_Wiped = false;
Dees_Troya13d74f2013-03-24 08:54:55 -0500181 Can_Be_Backed_Up = false;
Vojtech Bocek1dc30982013-08-30 21:49:30 +0200182 Use_Rm_Rf = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400183 Wipe_During_Factory_Reset = false;
184 Wipe_Available_in_GUI = false;
185 Is_SubPartition = false;
Dees_Troy2691f9d2012-09-24 11:15:49 -0400186 Has_SubPartition = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400187 SubPartition_Of = "";
188 Symlink_Path = "";
189 Symlink_Mount_Point = "";
190 Mount_Point = "";
Dees_Troye58d5262012-09-21 12:27:57 -0400191 Backup_Path = "";
Dees_Troy38bd7602012-09-14 13:33:53 -0400192 Actual_Block_Device = "";
193 Primary_Block_Device = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400194 Alternate_Block_Device = "";
195 Removable = false;
196 Is_Present = false;
197 Length = 0;
198 Size = 0;
199 Used = 0;
200 Free = 0;
201 Backup_Size = 0;
202 Can_Be_Encrypted = false;
203 Is_Encrypted = false;
204 Is_Decrypted = false;
Ethan Yonker253368a2014-11-25 15:00:52 -0600205 Mount_To_Decrypt = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400206 Decrypted_Block_Device = "";
207 Display_Name = "";
Dees_Troya13d74f2013-03-24 08:54:55 -0500208 Backup_Display_Name = "";
209 Storage_Name = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400210 Backup_Name = "";
Dees_Troy63c8df72012-09-10 14:02:05 -0400211 Backup_FileName = "";
Dees_Troy38bd7602012-09-14 13:33:53 -0400212 MTD_Name = "";
bigbiffce8f83c2015-12-12 18:30:21 -0500213 Backup_Method = BM_NONE;
Dees_Troy83bd4832013-05-04 12:39:56 +0000214 Can_Encrypt_Backup = false;
215 Use_Userdata_Encryption = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400216 Has_Data_Media = false;
Dees_Troye58d5262012-09-21 12:27:57 -0400217 Has_Android_Secure = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400218 Is_Storage = false;
Dees_Troya13d74f2013-03-24 08:54:55 -0500219 Is_Settings_Storage = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400220 Storage_Path = "";
221 Current_File_System = "";
222 Fstab_File_System = "";
Hashcode62bd9e02013-11-19 21:59:42 -0800223 Mount_Flags = 0;
224 Mount_Options = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400225 Format_Block_Size = 0;
Dees_Troy68cab492012-12-12 19:29:35 +0000226 Ignore_Blkid = false;
Dees_Troy16c2b312013-01-15 16:51:18 +0000227 Retain_Layout_Version = false;
Ethan Yonker253368a2014-11-25 15:00:52 -0600228 Crypto_Key_Location = "footer";
Ethan Yonker726a0202014-12-16 20:01:38 -0600229 MTP_Storage_ID = 0;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600230 Can_Flash_Img = false;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500231 Mount_Read_Only = false;
Ethan Yonker66a19492015-12-10 10:19:45 -0600232 Is_Adopted_Storage = false;
233 Adopted_GUID = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400234}
235
236TWPartition::~TWPartition(void) {
237 // Do nothing
238}
239
Matt Mower2b2dd152016-04-26 11:24:08 -0500240bool TWPartition::Process_Fstab_Line(const char *fstab_line, bool Display_Error) {
241 char full_line[MAX_FSTAB_LINE_LENGTH];
Matt Mower2416a502016-04-12 19:54:46 -0500242 char twflags[MAX_FSTAB_LINE_LENGTH] = "";
Dees_Troy5bf43922012-09-07 16:07:55 -0400243 char* ptr;
Matt Mower2b2dd152016-04-26 11:24:08 -0500244 int line_len = strlen(fstab_line), index = 0, item_index = 0;
Dees_Troya13d74f2013-03-24 08:54:55 -0500245 bool skip = false;
Dees_Troy5bf43922012-09-07 16:07:55 -0400246
Matt Mower2b2dd152016-04-26 11:24:08 -0500247 strlcpy(full_line, fstab_line, sizeof(full_line));
Dees_Troy51127312012-09-08 13:08:49 -0400248 for (index = 0; index < line_len; index++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500249 if (full_line[index] == 34)
250 skip = !skip;
251 if (!skip && full_line[index] <= 32)
Dees_Troy5bf43922012-09-07 16:07:55 -0400252 full_line[index] = '\0';
Dees_Troy5bf43922012-09-07 16:07:55 -0400253 }
Dees_Troy7c2dec82012-09-26 09:49:14 -0400254 Mount_Point = full_line;
Dees_Troy2673cec2013-04-02 20:22:16 +0000255 LOGINFO("Processing '%s'\n", Mount_Point.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -0400256 Backup_Path = Mount_Point;
Dees_Troya13d74f2013-03-24 08:54:55 -0500257 Storage_Path = Mount_Point;
Dees_Troy70737fa2013-04-08 13:19:20 +0000258 Display_Name = full_line + 1;
259 Backup_Display_Name = Display_Name;
260 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400261 index = Mount_Point.size();
262 while (index < line_len) {
263 while (index < line_len && full_line[index] == '\0')
264 index++;
265 if (index >= line_len)
266 continue;
267 ptr = full_line + index;
268 if (item_index == 0) {
269 // File System
270 Fstab_File_System = ptr;
271 Current_File_System = ptr;
272 item_index++;
273 } else if (item_index == 1) {
274 // Primary Block Device
Dees_Troy38bd7602012-09-14 13:33:53 -0400275 if (Fstab_File_System == "mtd" || Fstab_File_System == "yaffs2") {
Dees_Troy094207a2012-09-26 12:00:39 -0400276 MTD_Name = ptr;
277 Find_MTD_Block_Device(MTD_Name);
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400278 } else if (Fstab_File_System == "bml") {
279 if (Mount_Point == "/boot")
280 MTD_Name = "boot";
281 else if (Mount_Point == "/recovery")
282 MTD_Name = "recovery";
283 Primary_Block_Device = ptr;
284 if (*ptr != '/')
Dees_Troy2673cec2013-04-02 20:22:16 +0000285 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 -0400286 } else if (*ptr != '/') {
Dees_Troy5bf43922012-09-07 16:07:55 -0400287 if (Display_Error)
Matt Mower2b2dd152016-04-26 11:24:08 -0500288 LOGERR("Invalid block device '%s' in fstab line '%s'", ptr, fstab_line);
Dees_Troy5bf43922012-09-07 16:07:55 -0400289 else
Matt Mower2b2dd152016-04-26 11:24:08 -0500290 LOGINFO("Invalid block device '%s' in fstab line '%s'", ptr, fstab_line);
Matt Mower72c87ce2016-04-26 14:34:56 -0500291 return false;
Dees_Troy38bd7602012-09-14 13:33:53 -0400292 } else {
293 Primary_Block_Device = ptr;
294 Find_Real_Block_Device(Primary_Block_Device, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400295 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400296 item_index++;
297 } else if (item_index > 1) {
298 if (*ptr == '/') {
299 // Alternate Block Device
300 Alternate_Block_Device = ptr;
301 Find_Real_Block_Device(Alternate_Block_Device, Display_Error);
302 } else if (strlen(ptr) > 7 && strncmp(ptr, "length=", 7) == 0) {
303 // Partition length
304 ptr += 7;
305 Length = atoi(ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400306 } else if (strlen(ptr) > 6 && strncmp(ptr, "flags=", 6) == 0) {
307 // Custom flags, save for later so that new values aren't overwritten by defaults
308 ptr += 6;
Matt Mower2416a502016-04-12 19:54:46 -0500309 strlcpy(twflags, ptr, sizeof(twflags));
Dees_Troy38bd7602012-09-14 13:33:53 -0400310 } else if (strlen(ptr) == 4 && (strncmp(ptr, "NULL", 4) == 0 || strncmp(ptr, "null", 4) == 0 || strncmp(ptr, "null", 4) == 0)) {
311 // Do nothing
Dees_Troy5bf43922012-09-07 16:07:55 -0400312 } else {
313 // Unhandled data
Matt Mower2b2dd152016-04-26 11:24:08 -0500314 LOGINFO("Unhandled fstab information '%s' in fstab line '%s'\n", ptr, fstab_line);
Dees_Troy5bf43922012-09-07 16:07:55 -0400315 }
316 }
317 while (index < line_len && full_line[index] != '\0')
318 index++;
319 }
320
321 if (!Is_File_System(Fstab_File_System) && !Is_Image(Fstab_File_System)) {
322 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000323 LOGERR("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400324 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000325 LOGINFO("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Matt Mower72c87ce2016-04-26 14:34:56 -0500326 return false;
Dees_Troy5bf43922012-09-07 16:07:55 -0400327 } else if (Is_File_System(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400328 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400329 Setup_File_System(Display_Error);
330 if (Mount_Point == "/system") {
331 Display_Name = "System";
Dees_Troya13d74f2013-03-24 08:54:55 -0500332 Backup_Display_Name = Display_Name;
333 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400334 Wipe_Available_in_GUI = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500335 Can_Be_Backed_Up = true;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500336 Mount_Read_Only = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400337 } else if (Mount_Point == "/data") {
338 Display_Name = "Data";
Dees_Troya13d74f2013-03-24 08:54:55 -0500339 Backup_Display_Name = Display_Name;
340 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400341 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400342 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500343 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000344 Can_Encrypt_Backup = true;
345 Use_Userdata_Encryption = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400346 } else if (Mount_Point == "/cache") {
347 Display_Name = "Cache";
Dees_Troya13d74f2013-03-24 08:54:55 -0500348 Backup_Display_Name = Display_Name;
349 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400350 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400351 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500352 Can_Be_Backed_Up = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400353 } else if (Mount_Point == "/datadata") {
Dees_Troy51127312012-09-08 13:08:49 -0400354 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400355 Display_Name = "DataData";
Dees_Troya13d74f2013-03-24 08:54:55 -0500356 Backup_Display_Name = Display_Name;
357 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400358 Is_SubPartition = true;
359 SubPartition_Of = "/data";
Dees_Troy5bf43922012-09-07 16:07:55 -0400360 DataManager::SetValue(TW_HAS_DATADATA, 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500361 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000362 Can_Encrypt_Backup = true;
363 Use_Userdata_Encryption = false; // This whole partition should be encrypted
Dees_Troy5bf43922012-09-07 16:07:55 -0400364 } else if (Mount_Point == "/sd-ext") {
Dees_Troy51127312012-09-08 13:08:49 -0400365 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400366 Display_Name = "SD-Ext";
Dees_Troya13d74f2013-03-24 08:54:55 -0500367 Backup_Display_Name = Display_Name;
368 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400369 Wipe_Available_in_GUI = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400370 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500371 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000372 Can_Encrypt_Backup = true;
373 Use_Userdata_Encryption = true;
Dees_Troy2c50e182012-09-26 20:05:28 -0400374 } else if (Mount_Point == "/boot") {
375 Display_Name = "Boot";
Dees_Troya13d74f2013-03-24 08:54:55 -0500376 Backup_Display_Name = Display_Name;
Dees_Troy2c50e182012-09-26 20:05:28 -0400377 DataManager::SetValue("tw_boot_is_mountable", 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500378 Can_Be_Backed_Up = true;
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500379 } else if (Mount_Point == "/vendor") {
380 Display_Name = "Vendor";
381 Backup_Display_Name = Display_Name;
382 Storage_Name = Display_Name;
383 Mount_Read_Only = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400384 }
385#ifdef TW_EXTERNAL_STORAGE_PATH
386 if (Mount_Point == EXPAND(TW_EXTERNAL_STORAGE_PATH)) {
387 Is_Storage = true;
388 Storage_Path = EXPAND(TW_EXTERNAL_STORAGE_PATH);
Dees_Troyc51f1f92012-09-20 15:32:13 -0400389 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500390 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400391#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000392 if (Mount_Point == "/sdcard" || Mount_Point == "/external_sd" || Mount_Point == "/external_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400393 Is_Storage = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400394 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500395 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400396#endif
Dees_Troyb05ddee2013-01-28 20:24:50 +0000397 }
Dees_Troy8170a922012-09-18 15:40:25 -0400398#ifdef TW_INTERNAL_STORAGE_PATH
399 if (Mount_Point == EXPAND(TW_INTERNAL_STORAGE_PATH)) {
400 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500401 Is_Settings_Storage = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400402 Storage_Path = EXPAND(TW_INTERNAL_STORAGE_PATH);
Dees_Troya13d74f2013-03-24 08:54:55 -0500403 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400404 }
405#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000406 if (Mount_Point == "/emmc" || Mount_Point == "/internal_sd" || Mount_Point == "/internal_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400407 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500408 Is_Settings_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500409 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400410 }
411#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400412 } else if (Is_Image(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400413 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400414 Setup_Image(Display_Error);
Dees_Troya13d74f2013-03-24 08:54:55 -0500415 if (Mount_Point == "/boot") {
416 Display_Name = "Boot";
417 Backup_Display_Name = Display_Name;
418 Can_Be_Backed_Up = true;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600419 Can_Flash_Img = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500420 } else if (Mount_Point == "/recovery") {
421 Display_Name = "Recovery";
422 Backup_Display_Name = Display_Name;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600423 Can_Flash_Img = true;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500424 } else if (Mount_Point == "/system_image") {
425 Display_Name = "System Image";
426 Backup_Display_Name = Display_Name;
HashBanged974bb2016-01-30 14:20:09 -0500427 Can_Flash_Img = true;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500428 Can_Be_Backed_Up = true;
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500429 } else if (Mount_Point == "/vendor_image") {
430 Display_Name = "Vendor Image";
431 Backup_Display_Name = Display_Name;
HashBanged974bb2016-01-30 14:20:09 -0500432 Can_Flash_Img = true;
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500433 Can_Be_Backed_Up = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500434 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400435 }
436
Matt Mower2416a502016-04-12 19:54:46 -0500437 // Process TWRP fstab flags
438 if (strlen(twflags) > 0) {
439 string Prev_Display_Name = Display_Name;
440 string Prev_Storage_Name = Storage_Name;
441 string Prev_Backup_Display_Name = Backup_Display_Name;
442 Display_Name = "";
443 Storage_Name = "";
444 Backup_Display_Name = "";
445
446 Process_TW_Flags(twflags, Display_Error);
447
448 bool has_display_name = !Display_Name.empty();
449 bool has_storage_name = !Storage_Name.empty();
450 bool has_backup_name = !Backup_Display_Name.empty();
451 if (!has_display_name) Display_Name = Prev_Display_Name;
452 if (!has_storage_name) Storage_Name = Prev_Storage_Name;
453 if (!has_backup_name) Backup_Display_Name = Prev_Backup_Display_Name;
454
455 if (has_display_name && !has_storage_name)
456 Storage_Name = Display_Name;
457 if (!has_display_name && has_storage_name)
458 Display_Name = Storage_Name;
459 if (has_display_name && !has_backup_name && Backup_Display_Name != "Android Secure")
460 Backup_Display_Name = Display_Name;
461 if (!has_display_name && has_backup_name)
462 Display_Name = Backup_Display_Name;
463 }
Dees_Troy51127312012-09-08 13:08:49 -0400464 return true;
465}
466
Matt Mower72c87ce2016-04-26 14:34:56 -0500467void TWPartition::Partition_Post_Processing(bool Display_Error) {
468 if (Mount_Point == "/data")
469 Setup_Data_Partition(Display_Error);
470 else if (Mount_Point == "/cache")
471 Setup_Cache_Partition(Display_Error);
472}
473
474void TWPartition::Setup_Data_Partition(bool Display_Error) {
475 if (Mount_Point != "/data")
476 return;
477
478 // Ensure /data is not mounted as tmpfs for qcom hardware decrypt
479 UnMount(false);
480
481#ifdef TW_INCLUDE_CRYPTO
482 if (datamedia)
483 Setup_Data_Media();
484 Can_Be_Encrypted = true;
485 char crypto_blkdev[255];
486 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
487 if (strcmp(crypto_blkdev, "error") != 0) {
488 DataManager::SetValue(TW_IS_DECRYPTED, 1);
489 Is_Encrypted = true;
490 Is_Decrypted = true;
491 Decrypted_Block_Device = crypto_blkdev;
492 LOGINFO("Data already decrypted, new block device: '%s'\n", crypto_blkdev);
493 } else if (!Mount(false)) {
494 if (Is_Present) {
495 set_partition_data(Actual_Block_Device.c_str(), Crypto_Key_Location.c_str(), Fstab_File_System.c_str());
496 if (cryptfs_check_footer() == 0) {
497 Is_Encrypted = true;
498 Is_Decrypted = false;
499 Can_Be_Mounted = false;
500 Current_File_System = "emmc";
501 Setup_Image(Display_Error);
502 DataManager::SetValue(TW_IS_ENCRYPTED, 1);
503 DataManager::SetValue(TW_CRYPTO_PWTYPE, cryptfs_get_password_type());
504 DataManager::SetValue(TW_CRYPTO_PASSWORD, "");
505 DataManager::SetValue("tw_crypto_display", "");
506 } else {
507 gui_err("mount_data_footer=Could not mount /data and unable to find crypto footer.");
508 }
509 } else {
510 LOGERR("Primary block device '%s' for mount point '%s' is not present!\n", Primary_Block_Device.c_str(), Mount_Point.c_str());
511 }
512 } else {
513 // Filesystem is not encrypted and the mount succeeded, so return to
514 // the original unmounted state
515 UnMount(false);
516 }
517 if (datamedia && (!Is_Encrypted || (Is_Encrypted && Is_Decrypted))) {
518 Setup_Data_Media();
519 Recreate_Media_Folder();
520 }
521#else
522 if (datamedia) {
523 Setup_Data_Media();
524 Recreate_Media_Folder();
525 }
526#endif
527}
528
529void TWPartition::Setup_Cache_Partition(bool Display_Error __unused) {
530 if (Mount_Point != "/cache")
531 return;
532
533 if (!Mount(true))
534 return;
535
536 if (!TWFunc::Path_Exists("/cache/recovery/.")) {
537 LOGINFO("Recreating /cache/recovery folder\n");
538 if (mkdir("/cache/recovery", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0)
539 LOGERR("Could not create /cache/recovery\n");
540 }
541}
542
Matt Mower4ab42b12016-04-21 13:52:18 -0500543void TWPartition::Process_FS_Flags(const char *str) {
544 char *options = strdup(str);
545 char *ptr, *savep;
Hashcode62bd9e02013-11-19 21:59:42 -0800546
Matt Mower4ab42b12016-04-21 13:52:18 -0500547 Mount_Options = "";
Hashcode62bd9e02013-11-19 21:59:42 -0800548
Matt Mower4ab42b12016-04-21 13:52:18 -0500549 // Avoid issues with potentially nested strtok by using strtok_r
550 ptr = strtok_r(options, ",", &savep);
551 while (ptr) {
552 const struct flag_list* mount_flag = mount_flags;
553
554 for (; mount_flag->name; mount_flag++) {
555 // mount_flags are never postfixed by '=',
556 // so only match identical strings (including length)
557 if (strcmp(ptr, mount_flag->name) == 0) {
558 Mount_Flags |= mount_flag->flag;
Hashcode62bd9e02013-11-19 21:59:42 -0800559 break;
560 }
561 }
562
Matt Mower4ab42b12016-04-21 13:52:18 -0500563 if (mount_flag->flag == MS_RDONLY)
564 Mount_Read_Only = true;
Hashcode62bd9e02013-11-19 21:59:42 -0800565
Matt Mower4ab42b12016-04-21 13:52:18 -0500566 if (mount_flag->name != 0) {
567 if (!Mount_Options.empty())
568 Mount_Options += ",";
569 Mount_Options += mount_flag->name;
570 } else {
571 LOGINFO("Unhandled mount flag: '%s'\n", ptr);
572 }
573
574 ptr = strtok_r(NULL, ",", &savep);
575 }
576 free(options);
Hashcode62bd9e02013-11-19 21:59:42 -0800577}
578
Matt Mower2416a502016-04-12 19:54:46 -0500579void TWPartition::Apply_TW_Flag(const unsigned flag, const char* str, const bool val) {
580 switch (flag) {
581 case TWFLAG_ANDSEC:
582 Has_Android_Secure = val;
583 break;
584 case TWFLAG_BACKUP:
585 Can_Be_Backed_Up = val;
586 break;
587 case TWFLAG_BACKUPNAME:
588 Backup_Display_Name = str;
589 break;
590 case TWFLAG_BLOCKSIZE:
591 Format_Block_Size = (unsigned long)(atol(str));
592 break;
593 case TWFLAG_CANBEWIPED:
594 Can_Be_Wiped = val;
595 break;
596 case TWFLAG_CANENCRYPTBACKUP:
597 Can_Encrypt_Backup = val;
598 break;
599 case TWFLAG_DEFAULTS:
600 // Do nothing
601 break;
602 case TWFLAG_DISPLAY:
603 Display_Name = str;
604 break;
605 case TWFLAG_ENCRYPTABLE:
606 case TWFLAG_FORCEENCRYPT:
607 Crypto_Key_Location = str;
608 break;
609 case TWFLAG_FLASHIMG:
610 Can_Flash_Img = val;
611 break;
612 case TWFLAG_FSFLAGS:
Matt Mower4ab42b12016-04-21 13:52:18 -0500613 Process_FS_Flags(str);
Matt Mower2416a502016-04-12 19:54:46 -0500614 break;
615 case TWFLAG_IGNOREBLKID:
616 Ignore_Blkid = val;
617 break;
618 case TWFLAG_LENGTH:
619 Length = atoi(str);
620 break;
621 case TWFLAG_MOUNTTODECRYPT:
622 Mount_To_Decrypt = val;
623 break;
624 case TWFLAG_REMOVABLE:
625 Removable = val;
626 break;
627 case TWFLAG_RETAINLAYOUTVERSION:
628 Retain_Layout_Version = val;
629 break;
630 case TWFLAG_SETTINGSSTORAGE:
631 Is_Settings_Storage = val;
632 if (Is_Settings_Storage)
633 Is_Storage = true;
634 break;
635 case TWFLAG_STORAGE:
636 Is_Storage = val;
637 break;
638 case TWFLAG_STORAGENAME:
639 Storage_Name = str;
640 break;
641 case TWFLAG_SUBPARTITIONOF:
642 Is_SubPartition = true;
643 SubPartition_Of = str;
644 break;
645 case TWFLAG_SYMLINK:
646 Symlink_Path = str;
647 break;
648 case TWFLAG_USERDATAENCRYPTBACKUP:
649 Use_Userdata_Encryption = val;
650 if (Use_Userdata_Encryption)
651 Can_Encrypt_Backup = true;
652 break;
653 case TWFLAG_USERMRF:
654 Use_Rm_Rf = val;
655 break;
656 case TWFLAG_WIPEDURINGFACTORYRESET:
657 Wipe_During_Factory_Reset = val;
658 if (Wipe_During_Factory_Reset) {
659 Can_Be_Wiped = true;
660 Wipe_Available_in_GUI = true;
661 }
662 break;
663 case TWFLAG_WIPEINGUI:
664 Wipe_Available_in_GUI = val;
665 if (Wipe_Available_in_GUI)
666 Can_Be_Wiped = true;
667 break;
668 default:
669 // Should not get here
670 LOGINFO("Flag identified for processing, but later unmatched: %i\n", flag);
671 break;
672 }
673}
Dees_Troy51127312012-09-08 13:08:49 -0400674
Matt Mower2416a502016-04-12 19:54:46 -0500675void TWPartition::Process_TW_Flags(char *flags, bool Display_Error) {
676 char separator[2] = {'\n', 0};
677 char *ptr, *savep;
678
679 // Semicolons within double-quotes are not forbidden, so replace
680 // only the semicolons intended as separators with '\n' for strtok
681 for (unsigned i = 0, skip = 0; i < strlen(flags); i++) {
682 if (flags[i] == '\"')
Dees_Troya13d74f2013-03-24 08:54:55 -0500683 skip = !skip;
Matt Mower2416a502016-04-12 19:54:46 -0500684 if (!skip && flags[i] == ';')
685 flags[i] = separator[0];
Dees_Troy51127312012-09-08 13:08:49 -0400686 }
687
Matt Mower2416a502016-04-12 19:54:46 -0500688 // Avoid issues with potentially nested strtok by using strtok_r
689 ptr = strtok_r(flags, separator, &savep);
690 while (ptr) {
691 int ptr_len = strlen(ptr);
692 const struct flag_list* tw_flag = tw_flags;
Hashcode62bd9e02013-11-19 21:59:42 -0800693
Matt Mower2416a502016-04-12 19:54:46 -0500694 for (; tw_flag->name; tw_flag++) {
695 int flag_len = strlen(tw_flag->name);
696
697 if (strncmp(ptr, tw_flag->name, flag_len) == 0) {
698 bool flag_val = false;
699
700 if (ptr_len > flag_len && (tw_flag->name)[flag_len-1] != '='
701 && ptr[flag_len] != '=') {
702 // Handle flags with same starting string
703 // (e.g. backup and backupname)
704 continue;
705 } else if (ptr_len > flag_len && ptr[flag_len] == '=') {
706 // Handle flags with dual format: Part 1
707 // (e.g. backup and backup=y. backup=y handled here)
708 ptr += flag_len + 1;
709 TWFunc::Strip_Quotes(ptr);
710 // Skip flags with empty argument
711 // (e.g. backup=)
712 if (strlen(ptr) == 0) {
713 LOGINFO("Flag missing argument or should not include '=': %s=\n", tw_flag->name);
714 break;
715 }
716 flag_val = strchr("yY1", *ptr) != NULL;
717 } else if (ptr_len == flag_len
718 && (tw_flag->name)[flag_len-1] == '=') {
719 // Skip flags missing argument after =
720 // (e.g. backupname=)
721 LOGINFO("Flag missing argument: %s\n", tw_flag->name);
722 break;
723 } else if (ptr_len > flag_len
724 && (tw_flag->name)[flag_len-1] == '=') {
725 // Handle arguments to flags
726 // (e.g. backupname="My Stuff")
727 ptr += flag_len;
728 TWFunc::Strip_Quotes(ptr);
729 // Skip flags with empty argument
730 // (e.g. backupname="")
731 if (strlen(ptr) == 0) {
732 LOGINFO("Flag missing argument: %s\n", tw_flag->name);
733 break;
734 }
735 } else if (ptr_len == flag_len) {
736 // Handle flags with dual format: Part 2
737 // (e.g. backup and backup=y. backup handled here)
738 flag_val = true;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600739 } else {
Matt Mower2416a502016-04-12 19:54:46 -0500740 LOGINFO("Flag matched, but could not be processed: %s\n", ptr);
741 break;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600742 }
Matt Mower2416a502016-04-12 19:54:46 -0500743
744 Apply_TW_Flag(tw_flag->flag, ptr, flag_val);
745 break;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600746 }
Matt Mower2416a502016-04-12 19:54:46 -0500747 }
748 if (tw_flag->name == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400749 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000750 LOGERR("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400751 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000752 LOGINFO("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400753 }
Matt Mower2416a502016-04-12 19:54:46 -0500754 ptr = strtok_r(NULL, separator, &savep);
Dees_Troy51127312012-09-08 13:08:49 -0400755 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400756}
757
Dees_Troy5bf43922012-09-07 16:07:55 -0400758bool TWPartition::Is_File_System(string File_System) {
759 if (File_System == "ext2" ||
Dees_Troy63c8df72012-09-10 14:02:05 -0400760 File_System == "ext3" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400761 File_System == "ext4" ||
762 File_System == "vfat" ||
763 File_System == "ntfs" ||
764 File_System == "yaffs2" ||
bigbiff bigbiff3e146522012-11-14 14:32:59 -0500765 File_System == "exfat" ||
Dees_Troye5017042013-08-29 16:38:55 +0000766 File_System == "f2fs" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400767 File_System == "auto")
768 return true;
769 else
770 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400771}
772
Dees_Troy5bf43922012-09-07 16:07:55 -0400773bool TWPartition::Is_Image(string File_System) {
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400774 if (File_System == "emmc" || File_System == "mtd" || File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -0400775 return true;
776 else
777 return false;
778}
779
Dees_Troy51127312012-09-08 13:08:49 -0400780bool TWPartition::Make_Dir(string Path, bool Display_Error) {
Ethan Yonker93ac7a02016-11-07 22:05:58 -0600781 if (TWFunc::Get_D_Type_From_Stat(Path) != S_IFDIR)
782 unlink(Path.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400783 if (!TWFunc::Path_Exists(Path)) {
Dees_Troy51127312012-09-08 13:08:49 -0400784 if (mkdir(Path.c_str(), 0777) == -1) {
785 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -0500786 gui_msg(Msg(msg::kError, "create_folder_strerr=Can not create '{1}' folder ({2}).")(Path)(strerror(errno)));
Dees_Troy51127312012-09-08 13:08:49 -0400787 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000788 LOGINFO("Can not create '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400789 return false;
790 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000791 LOGINFO("Created '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400792 return true;
793 }
794 }
795 return true;
796}
797
Dees_Troy5bf43922012-09-07 16:07:55 -0400798void TWPartition::Setup_File_System(bool Display_Error) {
799 struct statfs st;
800
801 Can_Be_Mounted = true;
802 Can_Be_Wiped = true;
803
Dees_Troy5bf43922012-09-07 16:07:55 -0400804 // Make the mount point folder if it doesn't exist
Dees_Troy51127312012-09-08 13:08:49 -0400805 Make_Dir(Mount_Point, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400806 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
807 Backup_Name = Display_Name;
bigbiffce8f83c2015-12-12 18:30:21 -0500808 Backup_Method = BM_FILES;
Dees_Troy5bf43922012-09-07 16:07:55 -0400809}
810
811void TWPartition::Setup_Image(bool 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;
Gary Peck82599a82012-11-21 16:23:12 -0800814 if (Current_File_System == "emmc")
bigbiffce8f83c2015-12-12 18:30:21 -0500815 Backup_Method = BM_DD;
Gary Peck82599a82012-11-21 16:23:12 -0800816 else if (Current_File_System == "mtd" || Current_File_System == "bml")
bigbiffce8f83c2015-12-12 18:30:21 -0500817 Backup_Method = BM_FLASH_UTILS;
Dees_Troy5bf43922012-09-07 16:07:55 -0400818 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000819 LOGINFO("Unhandled file system '%s' on image '%s'\n", Current_File_System.c_str(), Display_Name.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400820 if (Find_Partition_Size()) {
821 Used = Size;
822 Backup_Size = Size;
Dees_Troy51a0e822012-09-05 15:24:24 -0400823 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400824 if (Display_Error)
Dees Troyd932ce12013-10-18 17:12:59 +0000825 LOGERR("Unable to find partition size for '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400826 else
Dees Troyd932ce12013-10-18 17:12:59 +0000827 LOGINFO("Unable to find partition size for '%s'\n", Mount_Point.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400828 }
829}
830
Dees_Troye58d5262012-09-21 12:27:57 -0400831void TWPartition::Setup_AndSec(void) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500832 Backup_Display_Name = "Android Secure";
Dees_Troye58d5262012-09-21 12:27:57 -0400833 Backup_Name = "and-sec";
Dees_Troya13d74f2013-03-24 08:54:55 -0500834 Can_Be_Backed_Up = true;
Dees_Troye58d5262012-09-21 12:27:57 -0400835 Has_Android_Secure = true;
836 Symlink_Path = Mount_Point + "/.android_secure";
837 Symlink_Mount_Point = "/and-sec";
838 Backup_Path = Symlink_Mount_Point;
839 Make_Dir("/and-sec", true);
840 Recreate_AndSec_Folder();
Kjell Braden3126a112016-06-19 16:58:15 +0000841 Mount_Storage_Retry(true);
Dees_Troye58d5262012-09-21 12:27:57 -0400842}
843
that9e0593e2014-10-08 00:01:24 +0200844void TWPartition::Setup_Data_Media() {
Ethan Yonker6277c792014-09-15 14:54:30 -0500845 LOGINFO("Setting up '%s' as data/media emulated storage.\n", Mount_Point.c_str());
Ethan Yonker66a19492015-12-10 10:19:45 -0600846 if (Storage_Name.empty() || Storage_Name == "Data")
847 Storage_Name = "Internal Storage";
Ethan Yonker6277c792014-09-15 14:54:30 -0500848 Has_Data_Media = true;
849 Is_Storage = true;
Ethan Yonker66a19492015-12-10 10:19:45 -0600850 Storage_Path = Mount_Point + "/media";
Ethan Yonker6277c792014-09-15 14:54:30 -0500851 Symlink_Path = Storage_Path;
Ethan Yonker66a19492015-12-10 10:19:45 -0600852 if (Mount_Point == "/data") {
853 Is_Settings_Storage = true;
854 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
855 Make_Dir("/emmc", false);
856 Symlink_Mount_Point = "/emmc";
857 } else {
858 Make_Dir("/sdcard", false);
859 Symlink_Mount_Point = "/sdcard";
860 }
861 if (Mount(false) && TWFunc::Path_Exists(Mount_Point + "/media/0")) {
862 Storage_Path = Mount_Point + "/media/0";
863 Symlink_Path = Storage_Path;
864 DataManager::SetValue(TW_INTERNAL_PATH, Mount_Point + "/media/0");
865 UnMount(true);
866 }
867 DataManager::SetValue("tw_has_internal", 1);
868 DataManager::SetValue("tw_has_data_media", 1);
Ethan Yonker3fdcda42016-11-30 12:29:37 -0600869 backup_exclusions.add_absolute_dir("/data/data/com.google.android.music/files");
870 backup_exclusions.add_absolute_dir(Mount_Point + "/misc/vold");
871 wipe_exclusions.add_absolute_dir(Mount_Point + "/misc/vold");
872 backup_exclusions.add_absolute_dir(Mount_Point + "/.layout_version");
873 wipe_exclusions.add_absolute_dir(Mount_Point + "/.layout_version");
874 backup_exclusions.add_absolute_dir(Mount_Point + "/system/storage.xml");
875 wipe_exclusions.add_absolute_dir(Mount_Point + "/system/storage.xml");
Ethan Yonker6277c792014-09-15 14:54:30 -0500876 } else {
Ethan Yonker66a19492015-12-10 10:19:45 -0600877 if (Mount(true) && TWFunc::Path_Exists(Mount_Point + "/media/0")) {
878 Storage_Path = Mount_Point + "/media/0";
879 Symlink_Path = Storage_Path;
880 UnMount(true);
881 }
Ethan Yonker6277c792014-09-15 14:54:30 -0500882 }
Ethan Yonker3fdcda42016-11-30 12:29:37 -0600883 backup_exclusions.add_absolute_dir(Mount_Point + "/media");
884 wipe_exclusions.add_absolute_dir(Mount_Point + "/media");
Ethan Yonker6277c792014-09-15 14:54:30 -0500885}
886
Dees_Troy5bf43922012-09-07 16:07:55 -0400887void TWPartition::Find_Real_Block_Device(string& Block, bool Display_Error) {
888 char device[512], realDevice[512];
889
890 strcpy(device, Block.c_str());
891 memset(realDevice, 0, sizeof(realDevice));
892 while (readlink(device, realDevice, sizeof(realDevice)) > 0)
893 {
894 strcpy(device, realDevice);
895 memset(realDevice, 0, sizeof(realDevice));
896 }
897
898 if (device[0] != '/') {
899 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000900 LOGERR("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400901 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000902 LOGINFO("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400903 return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400904 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400905 Block = device;
906 return;
907 }
908}
909
Kjell Braden3126a112016-06-19 16:58:15 +0000910bool TWPartition::Mount_Storage_Retry(bool Display_Error) {
Dees_Troy8e337f32012-10-13 22:07:49 -0400911 // On some devices, storage doesn't want to mount right away, retry and sleep
Kjell Braden3126a112016-06-19 16:58:15 +0000912 if (!Mount(Display_Error)) {
Dees_Troy8e337f32012-10-13 22:07:49 -0400913 int retry_count = 5;
914 while (retry_count > 0 && !Mount(false)) {
915 usleep(500000);
916 retry_count--;
917 }
Kjell Braden3126a112016-06-19 16:58:15 +0000918 return Mount(Display_Error);
Dees_Troy8e337f32012-10-13 22:07:49 -0400919 }
Kjell Braden3126a112016-06-19 16:58:15 +0000920 return true;
Dees_Troy8e337f32012-10-13 22:07:49 -0400921}
922
Dees_Troy38bd7602012-09-14 13:33:53 -0400923bool TWPartition::Find_MTD_Block_Device(string MTD_Name) {
924 FILE *fp = NULL;
925 char line[255];
926
927 fp = fopen("/proc/mtd", "rt");
928 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000929 LOGERR("Device does not support /proc/mtd\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400930 return false;
931 }
932
933 while (fgets(line, sizeof(line), fp) != NULL)
934 {
935 char device[32], label[32];
936 unsigned long size = 0;
937 char* fstype = NULL;
938 int deviceId;
939
940 sscanf(line, "%s %lx %*s %*c%s", device, &size, label);
941
942 // Skip header and blank lines
943 if ((strcmp(device, "dev:") == 0) || (strlen(line) < 8))
944 continue;
945
946 // Strip off the trailing " from the label
947 label[strlen(label)-1] = '\0';
948
949 if (strcmp(label, MTD_Name.c_str()) == 0) {
950 // We found our device
951 // Strip off the trailing : from the device
952 device[strlen(device)-1] = '\0';
953 if (sscanf(device,"mtd%d", &deviceId) == 1) {
954 sprintf(device, "/dev/block/mtdblock%d", deviceId);
955 Primary_Block_Device = device;
Dees_Troy76543db2013-06-19 16:24:30 +0000956 fclose(fp);
957 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -0400958 }
959 }
960 }
961 fclose(fp);
962
963 return false;
964}
965
Dees_Troy51127312012-09-08 13:08:49 -0400966bool TWPartition::Get_Size_Via_statfs(bool Display_Error) {
967 struct statfs st;
968 string Local_Path = Mount_Point + "/.";
969
970 if (!Mount(Display_Error))
971 return false;
972
973 if (statfs(Local_Path.c_str(), &st) != 0) {
974 if (!Removable) {
975 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000976 LOGERR("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400977 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000978 LOGINFO("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400979 }
980 return false;
981 }
982 Size = (st.f_blocks * st.f_bsize);
983 Used = ((st.f_blocks - st.f_bfree) * st.f_bsize);
984 Free = (st.f_bfree * st.f_bsize);
985 Backup_Size = Used;
986 return true;
987}
988
989bool TWPartition::Get_Size_Via_df(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400990 FILE* fp;
991 char command[255], line[512];
992 int include_block = 1;
993 unsigned int min_len;
994
995 if (!Mount(Display_Error))
996 return false;
997
Dees_Troy38bd7602012-09-14 13:33:53 -0400998 min_len = Actual_Block_Device.size() + 2;
Dees_Troy51127312012-09-08 13:08:49 -0400999 sprintf(command, "df %s > /tmp/dfoutput.txt", Mount_Point.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001000 TWFunc::Exec_Cmd(command);
Dees_Troy51127312012-09-08 13:08:49 -04001001 fp = fopen("/tmp/dfoutput.txt", "rt");
1002 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001003 LOGINFO("Unable to open /tmp/dfoutput.txt.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001004 return false;
Dees_Troy51127312012-09-08 13:08:49 -04001005 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001006
1007 while (fgets(line, sizeof(line), fp) != NULL)
1008 {
1009 unsigned long blocks, used, available;
1010 char device[64];
1011 char tmpString[64];
1012
1013 if (strncmp(line, "Filesystem", 10) == 0)
1014 continue;
1015 if (strlen(line) < min_len) {
1016 include_block = 0;
1017 continue;
1018 }
1019 if (include_block) {
1020 sscanf(line, "%s %lu %lu %lu", device, &blocks, &used, &available);
1021 } else {
1022 // The device block string is so long that the df information is on the next line
1023 int space_count = 0;
Dees_Troye58d5262012-09-21 12:27:57 -04001024 sprintf(tmpString, "/dev/block/%s", Actual_Block_Device.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001025 while (tmpString[space_count] == 32)
1026 space_count++;
1027 sscanf(line + space_count, "%lu %lu %lu", &blocks, &used, &available);
1028 }
1029
1030 // Adjust block size to byte size
1031 Size = blocks * 1024ULL;
1032 Used = used * 1024ULL;
1033 Free = available * 1024ULL;
1034 Backup_Size = Used;
1035 }
1036 fclose(fp);
1037 return true;
1038}
1039
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001040unsigned long long TWPartition::IOCTL_Get_Block_Size() {
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001041 Find_Actual_Block_Device();
Ethan Yonker483e9f42016-01-11 22:21:18 -06001042
1043 return TWFunc::IOCTL_Get_Block_Size(Actual_Block_Device.c_str());
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001044}
1045
Dees_Troy5bf43922012-09-07 16:07:55 -04001046bool TWPartition::Find_Partition_Size(void) {
1047 FILE* fp;
1048 char line[512];
1049 string tmpdevice;
1050
igoriok87e3d932013-01-31 21:03:53 +02001051 fp = fopen("/proc/dumchar_info", "rt");
1052 if (fp != NULL) {
1053 while (fgets(line, sizeof(line), fp) != NULL)
1054 {
1055 char label[32], device[32];
1056 unsigned long size = 0;
1057
thatd43bf2d2014-09-21 23:13:02 +02001058 sscanf(line, "%s %lx %*x %*u %s", label, &size, device);
igoriok87e3d932013-01-31 21:03:53 +02001059
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05001060 // Skip header, annotation and blank lines
igoriok87e3d932013-01-31 21:03:53 +02001061 if ((strncmp(device, "/dev/", 5) != 0) || (strlen(line) < 8))
1062 continue;
1063
1064 tmpdevice = "/dev/";
1065 tmpdevice += label;
1066 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
1067 Size = size;
1068 fclose(fp);
1069 return true;
1070 }
1071 }
1072 }
1073
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001074 unsigned long long ioctl_size = IOCTL_Get_Block_Size();
1075 if (ioctl_size) {
1076 Size = ioctl_size;
1077 return true;
1078 }
1079
Dees_Troy5bf43922012-09-07 16:07:55 -04001080 // In this case, we'll first get the partitions we care about (with labels)
1081 fp = fopen("/proc/partitions", "rt");
1082 if (fp == NULL)
1083 return false;
1084
1085 while (fgets(line, sizeof(line), fp) != NULL)
1086 {
1087 unsigned long major, minor, blocks;
1088 char device[512];
1089 char tmpString[64];
1090
Dees_Troy63c8df72012-09-10 14:02:05 -04001091 if (strlen(line) < 7 || line[0] == 'm') continue;
Dees_Troy5bf43922012-09-07 16:07:55 -04001092 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
1093
1094 tmpdevice = "/dev/block/";
1095 tmpdevice += device;
Dees_Troy38bd7602012-09-14 13:33:53 -04001096 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001097 // Adjust block size to byte size
1098 Size = blocks * 1024ULL;
1099 fclose(fp);
1100 return true;
1101 }
1102 }
1103 fclose(fp);
1104 return false;
1105}
1106
Dees_Troy5bf43922012-09-07 16:07:55 -04001107bool TWPartition::Is_Mounted(void) {
1108 if (!Can_Be_Mounted)
1109 return false;
1110
1111 struct stat st1, st2;
1112 string test_path;
1113
1114 // Check to see if the mount point directory exists
1115 test_path = Mount_Point + "/.";
1116 if (stat(test_path.c_str(), &st1) != 0) return false;
1117
1118 // Check to see if the directory above the mount point exists
1119 test_path = Mount_Point + "/../.";
1120 if (stat(test_path.c_str(), &st2) != 0) return false;
1121
1122 // Compare the device IDs -- if they match then we're (probably) using tmpfs instead of an actual device
1123 int ret = (st1.st_dev != st2.st_dev) ? true : false;
1124
1125 return ret;
1126}
1127
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05001128bool TWPartition::Is_File_System_Writable(void) {
1129 if (!Is_File_System(Current_File_System) || !Is_Mounted())
1130 return false;
1131
1132 string test_path = Mount_Point + "/.";
1133 return (access(test_path.c_str(), W_OK) == 0);
1134}
1135
Dees_Troy5bf43922012-09-07 16:07:55 -04001136bool TWPartition::Mount(bool Display_Error) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001137 int exfat_mounted = 0;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001138 unsigned long flags = Mount_Flags;
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001139
Dees_Troy5bf43922012-09-07 16:07:55 -04001140 if (Is_Mounted()) {
1141 return true;
1142 } else if (!Can_Be_Mounted) {
1143 return false;
1144 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001145
1146 Find_Actual_Block_Device();
1147
1148 // Check the current file system before mounting
1149 Check_FS_Type();
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001150 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/exfat-fuse")) {
Dees_Troye34c1332013-02-06 19:13:00 +00001151 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 +00001152 LOGINFO("cmd: %s\n", cmd.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001153 string result;
1154 if (TWFunc::Exec_Cmd(cmd, result) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001155 LOGINFO("exfat-fuse failed to mount with result '%s', trying vfat\n", result.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001156 Current_File_System = "vfat";
1157 } else {
1158#ifdef TW_NO_EXFAT_FUSE
1159 UnMount(false);
1160 // We'll let the kernel handle it but using exfat-fuse to detect if the file system is actually exfat
1161 // Some kernels let us mount vfat as exfat which doesn't work out too well
1162#else
1163 exfat_mounted = 1;
1164#endif
1165 }
1166 }
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001167
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001168 if (Current_File_System == "ntfs" && (TWFunc::Path_Exists("/sbin/ntfs-3g") || TWFunc::Path_Exists("/sbin/mount.ntfs"))) {
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001169 string cmd;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001170 string Ntfsmount_Binary = "";
1171
1172 if (TWFunc::Path_Exists("/sbin/ntfs-3g"))
1173 Ntfsmount_Binary = "ntfs-3g";
1174 else if (TWFunc::Path_Exists("/sbin/mount.ntfs"))
1175 Ntfsmount_Binary = "mount.ntfs";
1176
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001177 if (Mount_Read_Only)
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001178 cmd = "/sbin/" + Ntfsmount_Binary + " -o ro " + Actual_Block_Device + " " + Mount_Point;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001179 else
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001180 cmd = "/sbin/" + Ntfsmount_Binary + " " + Actual_Block_Device + " " + Mount_Point;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001181 LOGINFO("cmd: '%s'\n", cmd.c_str());
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001182
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001183 if (TWFunc::Exec_Cmd(cmd) == 0) {
1184 return true;
1185 } else {
1186 LOGINFO("ntfs-3g failed to mount, trying regular mount method.\n");
1187 }
1188 }
1189
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001190 if (Mount_Read_Only)
1191 flags |= MS_RDONLY;
1192
Dees_Troy22042032012-12-18 21:23:08 +00001193 if (Fstab_File_System == "yaffs2") {
1194 // mount an MTD partition as a YAFFS2 filesystem.
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001195 flags = MS_NOATIME | MS_NODEV | MS_NODIRATIME;
1196 if (Mount_Read_Only)
1197 flags |= MS_RDONLY;
Dees_Troy76543db2013-06-19 16:24:30 +00001198 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags, NULL) < 0) {
1199 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags | MS_RDONLY, NULL) < 0) {
1200 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001201 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy76543db2013-06-19 16:24:30 +00001202 else
1203 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
1204 return false;
1205 } else {
1206 LOGINFO("Mounted '%s' (MTD) as RO\n", Mount_Point.c_str());
1207 return true;
1208 }
1209 } else {
1210 struct stat st;
1211 string test_path = Mount_Point;
1212 if (stat(test_path.c_str(), &st) < 0) {
1213 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001214 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy76543db2013-06-19 16:24:30 +00001215 else
1216 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
1217 return false;
1218 }
1219 mode_t new_mode = st.st_mode | S_IXUSR | S_IXGRP | S_IXOTH;
1220 if (new_mode != st.st_mode) {
1221 LOGINFO("Fixing execute permissions for %s\n", Mount_Point.c_str());
1222 if (chmod(Mount_Point.c_str(), new_mode) < 0) {
1223 if (Display_Error)
1224 LOGERR("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
1225 else
1226 LOGINFO("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
1227 return false;
1228 }
1229 }
Dees_Troy22042032012-12-18 21:23:08 +00001230 return true;
Dees_Troy76543db2013-06-19 16:24:30 +00001231 }
thatc7572eb2015-03-31 18:55:30 +02001232 }
1233
1234 string mount_fs = Current_File_System;
1235 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sys/module/texfat"))
1236 mount_fs = "texfat";
1237
1238 if (!exfat_mounted &&
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001239 mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), mount_fs.c_str(), flags, Mount_Options.c_str()) != 0 &&
1240 mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), mount_fs.c_str(), flags, NULL) != 0) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001241#ifdef TW_NO_EXFAT_FUSE
1242 if (Current_File_System == "exfat") {
Dees_Troy2673cec2013-04-02 20:22:16 +00001243 LOGINFO("Mounting exfat failed, trying vfat...\n");
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001244 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), "vfat", 0, NULL) != 0) {
Dees_Troy85f44ed2013-01-09 18:42:36 +00001245 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001246 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy85f44ed2013-01-09 18:42:36 +00001247 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001248 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001249 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 +00001250 return false;
Dees_Troy85f44ed2013-01-09 18:42:36 +00001251 }
Dees_Troyb05ddee2013-01-28 20:24:50 +00001252 } else {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001253#endif
bigbiff bigbiff26774a02014-03-29 18:22:00 -04001254 if (!Removable && Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001255 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001256 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001257 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
1258 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 +00001259 return false;
1260#ifdef TW_NO_EXFAT_FUSE
Dees_Troy85f44ed2013-01-09 18:42:36 +00001261 }
1262#endif
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001263 }
Ethan Yonker253368a2014-11-25 15:00:52 -06001264
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001265 if (Removable)
1266 Update_Size(Display_Error);
1267
xiaolu9416f4f2015-06-04 08:22:23 +08001268 if (!Symlink_Mount_Point.empty() && TWFunc::Path_Exists(Symlink_Path)) {
codelover2a3d4ce2015-03-14 20:26:49 +08001269 string Command = "mount -o bind '" + Symlink_Path + "' '" + Symlink_Mount_Point + "'";
Vojtech Bocek05534202013-09-11 08:11:56 +02001270 TWFunc::Exec_Cmd(Command);
Dees_Troy5bf43922012-09-07 16:07:55 -04001271 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001272 return true;
1273}
1274
1275bool TWPartition::UnMount(bool Display_Error) {
1276 if (Is_Mounted()) {
1277 int never_unmount_system;
1278
1279 DataManager::GetValue(TW_DONT_UNMOUNT_SYSTEM, never_unmount_system);
1280 if (never_unmount_system == 1 && Mount_Point == "/system")
1281 return true; // Never unmount system if you're not supposed to unmount it
1282
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001283 if (Is_Storage)
Ethan Yonker726a0202014-12-16 20:01:38 -06001284 PartitionManager.Remove_MTP_Storage(MTP_Storage_ID);
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001285
Dees_Troy38bd7602012-09-14 13:33:53 -04001286 if (!Symlink_Mount_Point.empty())
1287 umount(Symlink_Mount_Point.c_str());
1288
Dees_Troyb05ddee2013-01-28 20:24:50 +00001289 umount(Mount_Point.c_str());
1290 if (Is_Mounted()) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001291 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001292 gui_msg(Msg(msg::kError, "fail_unmount=Failed to unmount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy5bf43922012-09-07 16:07:55 -04001293 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001294 LOGINFO("Unable to unmount '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001295 return false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001296 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -04001297 return true;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001298 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001299 } else {
1300 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001301 }
1302}
1303
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05001304bool TWPartition::ReMount(bool Display_Error) {
1305 if (UnMount(Display_Error))
1306 return Mount(Display_Error);
1307 return false;
1308}
1309
1310bool TWPartition::ReMount_RW(bool Display_Error) {
1311 // No need to remount if already mounted rw
1312 if (Is_File_System_Writable())
1313 return true;
1314
1315 bool ro = Mount_Read_Only;
1316 int flags = Mount_Flags;
1317
1318 Mount_Read_Only = false;
1319 Mount_Flags &= ~MS_RDONLY;
1320
1321 bool ret = ReMount(Display_Error);
1322
1323 Mount_Read_Only = ro;
1324 Mount_Flags = flags;
1325
1326 return ret;
1327}
1328
Gary Peck43acadf2012-11-21 21:19:01 -08001329bool TWPartition::Wipe(string New_File_System) {
Ethan Yonker726a0202014-12-16 20:01:38 -06001330 bool wiped = false, update_crypt = false, recreate_media = true;
Dees_Troy16c2b312013-01-15 16:51:18 +00001331 int check;
1332 string Layout_Filename = Mount_Point + "/.layout_version";
1333
Dees_Troy38bd7602012-09-14 13:33:53 -04001334 if (!Can_Be_Wiped) {
Matt Mower3c366972015-12-25 19:28:31 -06001335 gui_msg(Msg(msg::kError, "cannot_wipe=Partition {1} cannot be wiped.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04001336 return false;
1337 }
1338
Dees_Troyc51f1f92012-09-20 15:32:13 -04001339 if (Mount_Point == "/cache")
Dees_Troy2673cec2013-04-02 20:22:16 +00001340 Log_Offset = 0;
Dees_Troyc51f1f92012-09-20 15:32:13 -04001341
Dees_Troy16c2b312013-01-15 16:51:18 +00001342 if (Retain_Layout_Version && Mount(false) && TWFunc::Path_Exists(Layout_Filename))
1343 TWFunc::copy_file(Layout_Filename, "/.layout_version", 0600);
1344 else
1345 unlink("/.layout_version");
Dees_Troy38bd7602012-09-14 13:33:53 -04001346
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001347 if (Has_Data_Media && Current_File_System == New_File_System) {
Dees_Troy16c2b312013-01-15 16:51:18 +00001348 wiped = Wipe_Data_Without_Wiping_Media();
Ethan Yonker5eac2222014-06-11 12:22:55 -05001349 recreate_media = false;
Dees_Troy16c2b312013-01-15 16:51:18 +00001350 } else {
Dees_Troy16c2b312013-01-15 16:51:18 +00001351 DataManager::GetValue(TW_RM_RF_VAR, check);
1352
Hashcodedabfd492013-08-29 22:45:30 -07001353 if (check || Use_Rm_Rf)
Dees_Troy16c2b312013-01-15 16:51:18 +00001354 wiped = Wipe_RMRF();
1355 else if (New_File_System == "ext4")
1356 wiped = Wipe_EXT4();
1357 else if (New_File_System == "ext2" || New_File_System == "ext3")
1358 wiped = Wipe_EXT23(New_File_System);
1359 else if (New_File_System == "vfat")
1360 wiped = Wipe_FAT();
1361 else if (New_File_System == "exfat")
1362 wiped = Wipe_EXFAT();
1363 else if (New_File_System == "yaffs2")
1364 wiped = Wipe_MTD();
Dees_Troye5017042013-08-29 16:38:55 +00001365 else if (New_File_System == "f2fs")
1366 wiped = Wipe_F2FS();
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001367 else if (New_File_System == "ntfs")
1368 wiped = Wipe_NTFS();
Dees_Troy16c2b312013-01-15 16:51:18 +00001369 else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001370 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 +00001371 unlink("/.layout_version");
1372 return false;
1373 }
1374 update_crypt = wiped;
Gary Pecke8bc5d72012-12-21 06:45:25 -08001375 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001376
Gary Pecke8bc5d72012-12-21 06:45:25 -08001377 if (wiped) {
Dees_Troy1c1ac442013-01-17 21:42:14 +00001378 if (Mount_Point == "/cache")
1379 DataManager::Output_Version();
1380
Dees_Troy16c2b312013-01-15 16:51:18 +00001381 if (TWFunc::Path_Exists("/.layout_version") && Mount(false))
1382 TWFunc::copy_file("/.layout_version", Layout_Filename, 0600);
1383
1384 if (update_crypt) {
1385 Setup_File_System(false);
1386 if (Is_Encrypted && !Is_Decrypted) {
1387 // just wiped an encrypted partition back to its unencrypted state
1388 Is_Encrypted = false;
1389 Is_Decrypted = false;
1390 Decrypted_Block_Device = "";
1391 if (Mount_Point == "/data") {
1392 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1393 DataManager::SetValue(TW_IS_DECRYPTED, 0);
1394 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001395 }
1396 }
Ethan Yonker5eac2222014-06-11 12:22:55 -05001397
Ethan Yonker66a19492015-12-10 10:19:45 -06001398 if (Has_Data_Media && recreate_media) {
Ethan Yonker5eac2222014-06-11 12:22:55 -05001399 Recreate_Media_Folder();
1400 }
Matt Mower209c9632016-01-20 12:08:35 -06001401 if (Is_Storage && Mount(false))
1402 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Gary Pecke8bc5d72012-12-21 06:45:25 -08001403 }
Matt Mower209c9632016-01-20 12:08:35 -06001404
Gary Pecke8bc5d72012-12-21 06:45:25 -08001405 return wiped;
Dees_Troy51a0e822012-09-05 15:24:24 -04001406}
1407
Gary Peck43acadf2012-11-21 21:19:01 -08001408bool TWPartition::Wipe() {
Gary Peck82599a82012-11-21 16:23:12 -08001409 if (Is_File_System(Current_File_System))
1410 return Wipe(Current_File_System);
1411 else
1412 return Wipe(Fstab_File_System);
Gary Peck43acadf2012-11-21 21:19:01 -08001413}
1414
Dees_Troye58d5262012-09-21 12:27:57 -04001415bool TWPartition::Wipe_AndSec(void) {
1416 if (!Has_Android_Secure)
1417 return false;
1418
Dees_Troye58d5262012-09-21 12:27:57 -04001419 if (!Mount(true))
1420 return false;
1421
Ethan Yonker74db1572015-10-28 12:44:49 -05001422 gui_msg(Msg("wiping=Wiping {1}")(Backup_Display_Name));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001423 TWFunc::removeDir(Mount_Point + "/.android_secure/", true);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001424 return true;
Dees_Troye58d5262012-09-21 12:27:57 -04001425}
1426
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001427bool TWPartition::Can_Repair() {
Ethan Yonkera2719152015-05-28 09:44:41 -05001428 if (Mount_Read_Only)
1429 return false;
Matt Mower18794c82015-11-11 16:22:45 -06001430 if (Current_File_System == "vfat" && TWFunc::Path_Exists("/sbin/fsck.fat"))
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001431 return true;
1432 else if ((Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") && TWFunc::Path_Exists("/sbin/e2fsck"))
1433 return true;
1434 else if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/fsck.exfat"))
1435 return true;
1436 else if (Current_File_System == "f2fs" && TWFunc::Path_Exists("/sbin/fsck.f2fs"))
1437 return true;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001438 else if (Current_File_System == "ntfs" && (TWFunc::Path_Exists("/sbin/ntfsfix") || TWFunc::Path_Exists("/sbin/fsck.ntfs")))
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001439 return true;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001440 return false;
1441}
1442
1443bool TWPartition::Repair() {
1444 string command;
1445
1446 if (Current_File_System == "vfat") {
Matt Mower18794c82015-11-11 16:22:45 -06001447 if (!TWFunc::Path_Exists("/sbin/fsck.fat")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001448 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.fat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001449 return false;
1450 }
1451 if (!UnMount(true))
1452 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001453 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.fat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001454 Find_Actual_Block_Device();
Matt Mower18794c82015-11-11 16:22:45 -06001455 command = "/sbin/fsck.fat -y " + Actual_Block_Device;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001456 LOGINFO("Repair command: %s\n", command.c_str());
1457 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001458 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001459 return true;
1460 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001461 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001462 return false;
1463 }
1464 }
1465 if (Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") {
1466 if (!TWFunc::Path_Exists("/sbin/e2fsck")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001467 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("e2fsck"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001468 return false;
1469 }
1470 if (!UnMount(true))
1471 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001472 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("e2fsck"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001473 Find_Actual_Block_Device();
Ethan Yonkera2719152015-05-28 09:44:41 -05001474 command = "/sbin/e2fsck -fp " + Actual_Block_Device;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001475 LOGINFO("Repair command: %s\n", command.c_str());
1476 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001477 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001478 return true;
1479 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001480 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001481 return false;
1482 }
1483 }
1484 if (Current_File_System == "exfat") {
1485 if (!TWFunc::Path_Exists("/sbin/fsck.exfat")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001486 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.exfat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001487 return false;
1488 }
1489 if (!UnMount(true))
1490 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001491 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.exfat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001492 Find_Actual_Block_Device();
1493 command = "/sbin/fsck.exfat " + Actual_Block_Device;
1494 LOGINFO("Repair command: %s\n", command.c_str());
1495 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001496 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001497 return true;
1498 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001499 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001500 return false;
1501 }
1502 }
1503 if (Current_File_System == "f2fs") {
1504 if (!TWFunc::Path_Exists("/sbin/fsck.f2fs")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001505 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.f2fs"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001506 return false;
1507 }
1508 if (!UnMount(true))
1509 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001510 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.f2fs"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001511 Find_Actual_Block_Device();
1512 command = "/sbin/fsck.f2fs " + Actual_Block_Device;
1513 LOGINFO("Repair command: %s\n", command.c_str());
1514 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001515 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001516 return true;
1517 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001518 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001519 return false;
1520 }
1521 }
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001522 if (Current_File_System == "ntfs") {
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001523 string Ntfsfix_Binary;
1524 if (TWFunc::Path_Exists("/sbin/ntfsfix"))
1525 Ntfsfix_Binary = "ntfsfix";
1526 else if (TWFunc::Path_Exists("/sbin/fsck.ntfs"))
1527 Ntfsfix_Binary = "fsck.ntfs";
1528 else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001529 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("ntfsfix"));
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001530 return false;
1531 }
1532 if (!UnMount(true))
1533 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001534 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)(Ntfsfix_Binary));
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001535 Find_Actual_Block_Device();
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001536 command = "/sbin/" + Ntfsfix_Binary + " " + Actual_Block_Device;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001537 LOGINFO("Repair command: %s\n", command.c_str());
1538 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001539 gui_msg("done=Done.");
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001540 return true;
1541 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001542 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001543 return false;
1544 }
1545 }
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001546 return false;
1547}
1548
Ethan Yonkera2719152015-05-28 09:44:41 -05001549bool TWPartition::Can_Resize() {
1550 if (Mount_Read_Only)
1551 return false;
1552 if ((Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") && TWFunc::Path_Exists("/sbin/resize2fs"))
1553 return true;
1554 return false;
1555}
1556
1557bool TWPartition::Resize() {
1558 string command;
1559
1560 if (Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") {
1561 if (!Can_Repair()) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001562 LOGINFO("Cannot resize %s because %s cannot be repaired before resizing.\n", Display_Name.c_str(), Display_Name.c_str());
1563 gui_msg(Msg(msg::kError, "cannot_resize=Cannot resize {1}.")(Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001564 return false;
1565 }
1566 if (!TWFunc::Path_Exists("/sbin/resize2fs")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001567 LOGINFO("resize2fs does not exist! Cannot resize!\n");
1568 gui_msg(Msg(msg::kError, "cannot_resize=Cannot resize {1}.")(Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001569 return false;
1570 }
1571 // Repair will unmount so no need to do it twice
Ethan Yonker74db1572015-10-28 12:44:49 -05001572 gui_msg(Msg("repair_resize=Repairing {1} before resizing.")( Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001573 if (!Repair())
1574 return false;
Ethan Yonker74db1572015-10-28 12:44:49 -05001575 gui_msg(Msg("resizing=Resizing {1} using {2}...")(Display_Name)("resize2fs"));
Ethan Yonkera2719152015-05-28 09:44:41 -05001576 Find_Actual_Block_Device();
1577 command = "/sbin/resize2fs " + Actual_Block_Device;
1578 if (Length != 0) {
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001579 unsigned long long Actual_Size = IOCTL_Get_Block_Size();
1580 if (Actual_Size == 0)
1581 return false;
Ethan Yonkera2719152015-05-28 09:44:41 -05001582
Ethan Yonkera2719152015-05-28 09:44:41 -05001583 unsigned long long Block_Count;
1584 if (Length < 0) {
1585 // Reduce overall size by this length
1586 Block_Count = (Actual_Size / 1024LLU) - ((unsigned long long)(Length * -1) / 1024LLU);
1587 } else {
1588 // This is the size, not a size reduction
1589 Block_Count = ((unsigned long long)(Length) / 1024LLU);
1590 }
1591 char temp[256];
1592 sprintf(temp, "%llu", Block_Count);
1593 command += " ";
1594 command += temp;
1595 command += "K";
1596 }
1597 LOGINFO("Resize command: %s\n", command.c_str());
1598 if (TWFunc::Exec_Cmd(command) == 0) {
1599 Update_Size(true);
Ethan Yonker74db1572015-10-28 12:44:49 -05001600 gui_msg("done=Done.");
Ethan Yonkera2719152015-05-28 09:44:41 -05001601 return true;
1602 } else {
1603 Update_Size(true);
Ethan Yonker74db1572015-10-28 12:44:49 -05001604 gui_msg(Msg(msg::kError, "unable_resize=Unable to resize {1}.")(Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001605 return false;
1606 }
1607 }
1608 return false;
1609}
1610
bigbiffce8f83c2015-12-12 18:30:21 -05001611bool TWPartition::Backup(PartitionSettings *part_settings, pid_t *tar_fork_pid) {
1612 if (Backup_Method == BM_FILES)
1613 return Backup_Tar(part_settings, tar_fork_pid);
1614 else if (Backup_Method == BM_DD)
1615 return Backup_Image(part_settings);
1616 else if (Backup_Method == BM_FLASH_UTILS)
1617 return Backup_Dump_Image(part_settings);
Dees_Troy2673cec2013-04-02 20:22:16 +00001618 LOGERR("Unknown backup method for '%s'\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001619 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001620}
1621
James Christopher Adduono79ae0932016-10-25 02:18:32 -04001622bool TWPartition::Check_Restore_File_MD5(const string& Filename) {
1623 twrpDigest md5sum;
1624
1625 md5sum.setfn(Filename);
1626 switch (md5sum.verify_md5digest()) {
1627 case MD5_OK:
1628 gui_msg(Msg("md5_matched=MD5 matched for '{1}'.")(Filename));
1629 return true;
1630 case MD5_FILE_UNREADABLE:
1631 case MD5_NOT_FOUND:
1632 gui_msg(Msg(msg::kError, "no_md5_found=No md5 file found for '{1}'. Please unselect Enable MD5 verification to restore.")(Filename));
1633 break;
1634 case MD5_MATCH_FAIL:
1635 gui_msg(Msg(msg::kError, "md5_fail_match=MD5 failed to match on '{1}'.")(Filename));
1636 break;
1637 }
1638 return false;
1639}
1640
bigbiff bigbiff8fd4b092016-08-30 20:48:53 -04001641bool TWPartition::Check_MD5(PartitionSettings *part_settings) {
James Christopher Adduono79ae0932016-10-25 02:18:32 -04001642 string Full_Filename;
Dees_Troy43d8b002012-09-17 16:00:01 -04001643 char split_filename[512];
1644 int index = 0;
1645
Captain Throwbackff5935f2014-09-23 16:08:34 -04001646 sync();
1647
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05001648 Full_Filename = part_settings->Backup_Folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04001649 if (!TWFunc::Path_Exists(Full_Filename)) {
1650 // This is a split archive, we presume
James Christopher Adduono79ae0932016-10-25 02:18:32 -04001651 memset(split_filename, 0, sizeof(split_filename));
Dees_Troy83bd4832013-05-04 12:39:56 +00001652 while (index < 1000) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001653 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
James Christopher Adduono79ae0932016-10-25 02:18:32 -04001654 if (!TWFunc::Path_Exists(split_filename))
1655 break;
1656 LOGINFO("split_filename: %s\n", split_filename);
1657 if (!Check_Restore_File_MD5(split_filename))
1658 return false;
1659 index++;
Dees_Troy43d8b002012-09-17 16:00:01 -04001660 }
Dees_Troy4a2a1262012-09-18 09:33:47 -04001661 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001662 }
James Christopher Adduono79ae0932016-10-25 02:18:32 -04001663 return Check_Restore_File_MD5(Full_Filename); // Single file archive
Dees_Troy43d8b002012-09-17 16:00:01 -04001664}
1665
bigbiffce8f83c2015-12-12 18:30:21 -05001666bool TWPartition::Restore(PartitionSettings *part_settings) {
Matt Mower3c366972015-12-25 19:28:31 -06001667 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, gui_parse_text("{@restoring_hdr}"));
Ethan Yonkere080c1f2016-09-19 13:50:25 -05001668 LOGINFO("Restore filename is: %s/%s\n", part_settings->Backup_Folder.c_str(), Backup_FileName.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001669
bigbiffce8f83c2015-12-12 18:30:21 -05001670 string Restore_File_System = Get_Restore_File_System(part_settings);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001671
1672 if (Is_File_System(Restore_File_System))
bigbiffce8f83c2015-12-12 18:30:21 -05001673 return Restore_Tar(part_settings);
1674 else if (Is_Image(Restore_File_System))
1675 return Restore_Image(part_settings);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001676
1677 LOGERR("Unknown restore method for '%s'\n", Mount_Point.c_str());
1678 return false;
1679}
1680
bigbiffce8f83c2015-12-12 18:30:21 -05001681string TWPartition::Get_Restore_File_System(PartitionSettings *part_settings) {
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001682 size_t first_period, second_period;
1683 string Restore_File_System;
1684
Gary Peck43acadf2012-11-21 21:19:01 -08001685 // Parse backup filename to extract the file system before wiping
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05001686 first_period = Backup_FileName.find(".");
Gary Peck43acadf2012-11-21 21:19:01 -08001687 if (first_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001688 LOGERR("Unable to find file system (first period).\n");
thatd43bf2d2014-09-21 23:13:02 +02001689 return string();
Gary Peck43acadf2012-11-21 21:19:01 -08001690 }
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05001691 Restore_File_System = Backup_FileName.substr(first_period + 1, Backup_FileName.size() - first_period - 1);
Gary Peck43acadf2012-11-21 21:19:01 -08001692 second_period = Restore_File_System.find(".");
1693 if (second_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001694 LOGERR("Unable to find file system (second period).\n");
thatd43bf2d2014-09-21 23:13:02 +02001695 return string();
Gary Peck43acadf2012-11-21 21:19:01 -08001696 }
1697 Restore_File_System.resize(second_period);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001698 return Restore_File_System;
Dees_Troy51a0e822012-09-05 15:24:24 -04001699}
1700
1701string TWPartition::Backup_Method_By_Name() {
bigbiffce8f83c2015-12-12 18:30:21 -05001702 if (Backup_Method == BM_NONE)
Dees_Troy38bd7602012-09-14 13:33:53 -04001703 return "none";
bigbiffce8f83c2015-12-12 18:30:21 -05001704 else if (Backup_Method == BM_FILES)
Dees_Troy38bd7602012-09-14 13:33:53 -04001705 return "files";
bigbiffce8f83c2015-12-12 18:30:21 -05001706 else if (Backup_Method == BM_DD)
Dees_Troy38bd7602012-09-14 13:33:53 -04001707 return "dd";
bigbiffce8f83c2015-12-12 18:30:21 -05001708 else if (Backup_Method == BM_FLASH_UTILS)
Dees_Troy38bd7602012-09-14 13:33:53 -04001709 return "flash_utils";
1710 else
1711 return "undefined";
1712 return "ERROR!";
Dees_Troy51a0e822012-09-05 15:24:24 -04001713}
1714
1715bool TWPartition::Decrypt(string Password) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001716 LOGINFO("STUB TWPartition::Decrypt, password: '%s'\n", Password.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001717 // Is this needed?
Dees_Troy51a0e822012-09-05 15:24:24 -04001718 return 1;
1719}
1720
1721bool TWPartition::Wipe_Encryption() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001722 bool Save_Data_Media = Has_Data_Media;
1723
1724 if (!UnMount(true))
1725 return false;
1726
Dees_Troy38bd7602012-09-14 13:33:53 -04001727 Has_Data_Media = false;
Dees_Troy74fb2e92013-04-15 14:35:47 +00001728 Decrypted_Block_Device = "";
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001729#ifdef TW_INCLUDE_CRYPTO
1730 if (Is_Decrypted) {
1731 if (!UnMount(true))
1732 return false;
Matt Mower2b18a532015-02-20 16:58:05 -06001733 if (delete_crypto_blk_dev((char*)("userdata")) != 0) {
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001734 LOGERR("Error deleting crypto block device, continuing anyway.\n");
1735 }
1736 }
1737#endif
Dees_Troy74fb2e92013-04-15 14:35:47 +00001738 Is_Decrypted = false;
1739 Is_Encrypted = false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001740 Find_Actual_Block_Device();
Ethan Yonkerbc85b632015-08-09 12:48:14 -05001741 if (Crypto_Key_Location == "footer") {
1742 int newlen, fd;
1743 if (Length != 0) {
1744 newlen = Length;