blob: 07d71715482593ff1115ff61c404940064cde6c6 [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>
bigbiff bigbiff9c754052013-01-09 09:09:08 -050028#include <iostream>
29#include <sstream>
Ethan Yonker66a19492015-12-10 10:19:45 -060030#include <sys/param.h>
Ethan Yonker472f5062016-02-25 13:47:30 -060031#include <fcntl.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040032
Dees_Troy657c3092012-09-10 20:32:10 -040033#ifdef TW_INCLUDE_CRYPTO
34 #include "cutils/properties.h"
35#endif
36
bigbiff7b4c7a62015-01-01 19:44:14 -050037#include "libblkid/include/blkid.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040038#include "variables.h"
Dees_Troy2673cec2013-04-02 20:22:16 +000039#include "twcommon.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040040#include "partitions.hpp"
Dees_Troy5bf43922012-09-07 16:07:55 -040041#include "data.hpp"
Dees_Troy43d8b002012-09-17 16:00:01 -040042#include "twrp-functions.hpp"
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -050043#include "twrpDigest.hpp"
bigbiff bigbiff9c754052013-01-09 09:09:08 -050044#include "twrpTar.hpp"
bigbiff bigbiff34684ff2013-12-01 21:03:45 -050045#include "twrpDU.hpp"
Ethan Yonker1b7a31b2014-07-03 15:09:22 -050046#include "infomanager.hpp"
Ethan Yonker4b94cfd2014-12-11 10:00:45 -060047#include "set_metadata.h"
Ethan Yonker74db1572015-10-28 12:44:49 -050048#include "gui/gui.hpp"
Dees_Troy5bf43922012-09-07 16:07:55 -040049extern "C" {
Dees_Troy38bd7602012-09-14 13:33:53 -040050 #include "mtdutils/mtdutils.h"
51 #include "mtdutils/mounts.h"
Dees_Troya95f55c2013-08-17 13:14:43 +000052#ifdef USE_EXT4
53 #include "make_ext4fs.h"
54#endif
Ethan Yonker71413f42014-02-26 13:36:08 -060055
56#ifdef TW_INCLUDE_CRYPTO
Ethan Yonker253368a2014-11-25 15:00:52 -060057 #include "crypto/lollipop/cryptfs.h"
Ethan Yonker66a19492015-12-10 10:19:45 -060058 #include "gpt/gpt.h"
Ethan Yonkerbc85b632015-08-09 12:48:14 -050059#else
60 #define CRYPT_FOOTER_OFFSET 0x4000
Ethan Yonker71413f42014-02-26 13:36:08 -060061#endif
Dees_Troy5bf43922012-09-07 16:07:55 -040062}
bigbiff bigbiffc49d7062013-10-11 20:28:00 -040063#ifdef HAVE_SELINUX
64#include "selinux/selinux.h"
Ethan Yonkerf27497f2014-02-09 11:48:33 -060065#include <selinux/label.h>
bigbiff bigbiffc49d7062013-10-11 20:28:00 -040066#endif
Dees Troy4159aed2014-02-28 17:24:43 +000067#ifdef HAVE_CAPABILITIES
68#include <sys/capability.h>
69#include <sys/xattr.h>
70#include <linux/xattr.h>
71#endif
HashBanged974bb2016-01-30 14:20:09 -050072#include <sparse_format.h>
Ethan Yonker472f5062016-02-25 13:47:30 -060073#include "progresstracking.hpp"
Dees_Troy51a0e822012-09-05 15:24:24 -040074
bigbiff bigbiff9c754052013-01-09 09:09:08 -050075using namespace std;
76
Dees_Troya95f55c2013-08-17 13:14:43 +000077extern struct selabel_handle *selinux_handle;
Ethan Yonker6277c792014-09-15 14:54:30 -050078extern bool datamedia;
Dees_Troya95f55c2013-08-17 13:14:43 +000079
Hashcode62bd9e02013-11-19 21:59:42 -080080struct flag_list {
81 const char *name;
82 unsigned flag;
83};
84
85static struct flag_list mount_flags[] = {
86 { "noatime", MS_NOATIME },
87 { "noexec", MS_NOEXEC },
88 { "nosuid", MS_NOSUID },
89 { "nodev", MS_NODEV },
90 { "nodiratime", MS_NODIRATIME },
91 { "ro", MS_RDONLY },
92 { "rw", 0 },
93 { "remount", MS_REMOUNT },
94 { "bind", MS_BIND },
95 { "rec", MS_REC },
Dees Troyc4bc30e2014-02-03 15:04:19 +000096#ifdef MS_UNBINDABLE
Hashcode62bd9e02013-11-19 21:59:42 -080097 { "unbindable", MS_UNBINDABLE },
Dees Troyc4bc30e2014-02-03 15:04:19 +000098#endif
99#ifdef MS_PRIVATE
Hashcode62bd9e02013-11-19 21:59:42 -0800100 { "private", MS_PRIVATE },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000101#endif
102#ifdef MS_SLAVE
Hashcode62bd9e02013-11-19 21:59:42 -0800103 { "slave", MS_SLAVE },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000104#endif
105#ifdef MS_SHARED
Hashcode62bd9e02013-11-19 21:59:42 -0800106 { "shared", MS_SHARED },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000107#endif
Hashcode62bd9e02013-11-19 21:59:42 -0800108 { "sync", MS_SYNCHRONOUS },
109 { "defaults", 0 },
110 { 0, 0 },
111};
112
Matt Mower2416a502016-04-12 19:54:46 -0500113enum TW_FSTAB_FLAGS {
114 TWFLAG_DEFAULTS, // Retain position
115 TWFLAG_ANDSEC,
116 TWFLAG_BACKUP,
117 TWFLAG_BACKUPNAME,
118 TWFLAG_BLOCKSIZE,
119 TWFLAG_CANBEWIPED,
120 TWFLAG_CANENCRYPTBACKUP,
121 TWFLAG_DISPLAY,
122 TWFLAG_ENCRYPTABLE,
123 TWFLAG_FLASHIMG,
124 TWFLAG_FORCEENCRYPT,
125 TWFLAG_FSFLAGS,
126 TWFLAG_IGNOREBLKID,
127 TWFLAG_LENGTH,
128 TWFLAG_MOUNTTODECRYPT,
129 TWFLAG_REMOVABLE,
130 TWFLAG_RETAINLAYOUTVERSION,
131 TWFLAG_SETTINGSSTORAGE,
132 TWFLAG_STORAGE,
133 TWFLAG_STORAGENAME,
134 TWFLAG_SUBPARTITIONOF,
135 TWFLAG_SYMLINK,
136 TWFLAG_USERDATAENCRYPTBACKUP,
137 TWFLAG_USERMRF,
138 TWFLAG_WIPEDURINGFACTORYRESET,
139 TWFLAG_WIPEINGUI,
140};
141
142/* Flags without a trailing '=' are considered dual format flags and can be
143 * written as either 'flagname' or 'flagname=', where the character following
144 * the '=' is Y,y,1 for true and false otherwise.
145 */
146const struct flag_list tw_flags[] = {
147 { "andsec", TWFLAG_ANDSEC },
148 { "backup", TWFLAG_BACKUP },
149 { "backupname=", TWFLAG_BACKUPNAME },
150 { "blocksize=", TWFLAG_BLOCKSIZE },
151 { "canbewiped", TWFLAG_CANBEWIPED },
152 { "canencryptbackup", TWFLAG_CANENCRYPTBACKUP },
153 { "defaults", TWFLAG_DEFAULTS },
154 { "display=", TWFLAG_DISPLAY },
155 { "encryptable=", TWFLAG_ENCRYPTABLE },
156 { "flashimg", TWFLAG_FLASHIMG },
157 { "forceencrypt=", TWFLAG_FORCEENCRYPT },
158 { "fsflags=", TWFLAG_FSFLAGS },
159 { "ignoreblkid", TWFLAG_IGNOREBLKID },
160 { "length=", TWFLAG_LENGTH },
161 { "mounttodecrypt", TWFLAG_MOUNTTODECRYPT },
162 { "removable", TWFLAG_REMOVABLE },
163 { "retainlayoutversion", TWFLAG_RETAINLAYOUTVERSION },
164 { "settingsstorage", TWFLAG_SETTINGSSTORAGE },
165 { "storage", TWFLAG_STORAGE },
166 { "storagename=", TWFLAG_STORAGENAME },
167 { "subpartitionof=", TWFLAG_SUBPARTITIONOF },
168 { "symlink=", TWFLAG_SYMLINK },
169 { "userdataencryptbackup", TWFLAG_USERDATAENCRYPTBACKUP },
170 { "usermrf", TWFLAG_USERMRF },
171 { "wipeduringfactoryreset", TWFLAG_WIPEDURINGFACTORYRESET },
172 { "wipeingui", TWFLAG_WIPEINGUI },
173 { 0, 0 },
174};
175
that9e0593e2014-10-08 00:01:24 +0200176TWPartition::TWPartition() {
Dees_Troy51a0e822012-09-05 15:24:24 -0400177 Can_Be_Mounted = false;
178 Can_Be_Wiped = false;
Dees_Troya13d74f2013-03-24 08:54:55 -0500179 Can_Be_Backed_Up = false;
Vojtech Bocek1dc30982013-08-30 21:49:30 +0200180 Use_Rm_Rf = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400181 Wipe_During_Factory_Reset = false;
182 Wipe_Available_in_GUI = false;
183 Is_SubPartition = false;
Dees_Troy2691f9d2012-09-24 11:15:49 -0400184 Has_SubPartition = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400185 SubPartition_Of = "";
186 Symlink_Path = "";
187 Symlink_Mount_Point = "";
188 Mount_Point = "";
Dees_Troye58d5262012-09-21 12:27:57 -0400189 Backup_Path = "";
Dees_Troy38bd7602012-09-14 13:33:53 -0400190 Actual_Block_Device = "";
191 Primary_Block_Device = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400192 Alternate_Block_Device = "";
193 Removable = false;
194 Is_Present = false;
195 Length = 0;
196 Size = 0;
197 Used = 0;
198 Free = 0;
199 Backup_Size = 0;
200 Can_Be_Encrypted = false;
201 Is_Encrypted = false;
202 Is_Decrypted = false;
Ethan Yonker253368a2014-11-25 15:00:52 -0600203 Mount_To_Decrypt = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400204 Decrypted_Block_Device = "";
205 Display_Name = "";
Dees_Troya13d74f2013-03-24 08:54:55 -0500206 Backup_Display_Name = "";
207 Storage_Name = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400208 Backup_Name = "";
Dees_Troy63c8df72012-09-10 14:02:05 -0400209 Backup_FileName = "";
Dees_Troy38bd7602012-09-14 13:33:53 -0400210 MTD_Name = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400211 Backup_Method = NONE;
Dees_Troy83bd4832013-05-04 12:39:56 +0000212 Can_Encrypt_Backup = false;
213 Use_Userdata_Encryption = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400214 Has_Data_Media = false;
Dees_Troye58d5262012-09-21 12:27:57 -0400215 Has_Android_Secure = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400216 Is_Storage = false;
Dees_Troya13d74f2013-03-24 08:54:55 -0500217 Is_Settings_Storage = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400218 Storage_Path = "";
219 Current_File_System = "";
220 Fstab_File_System = "";
Hashcode62bd9e02013-11-19 21:59:42 -0800221 Mount_Flags = 0;
222 Mount_Options = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400223 Format_Block_Size = 0;
Dees_Troy68cab492012-12-12 19:29:35 +0000224 Ignore_Blkid = false;
Dees_Troy16c2b312013-01-15 16:51:18 +0000225 Retain_Layout_Version = false;
Ethan Yonker253368a2014-11-25 15:00:52 -0600226 Crypto_Key_Location = "footer";
Ethan Yonker726a0202014-12-16 20:01:38 -0600227 MTP_Storage_ID = 0;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600228 Can_Flash_Img = false;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500229 Mount_Read_Only = false;
Ethan Yonker66a19492015-12-10 10:19:45 -0600230 Is_Adopted_Storage = false;
231 Adopted_GUID = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400232}
233
234TWPartition::~TWPartition(void) {
235 // Do nothing
236}
237
Dees_Troy5bf43922012-09-07 16:07:55 -0400238bool TWPartition::Process_Fstab_Line(string Line, bool Display_Error) {
239 char full_line[MAX_FSTAB_LINE_LENGTH], item[MAX_FSTAB_LINE_LENGTH];
Matt Mower2416a502016-04-12 19:54:46 -0500240 char twflags[MAX_FSTAB_LINE_LENGTH] = "";
Dees_Troy5bf43922012-09-07 16:07:55 -0400241 int line_len = Line.size(), index = 0, item_index = 0;
242 char* ptr;
Dees_Troy5bf43922012-09-07 16:07:55 -0400243 strncpy(full_line, Line.c_str(), line_len);
Dees_Troya13d74f2013-03-24 08:54:55 -0500244 bool skip = false;
Dees_Troy5bf43922012-09-07 16:07:55 -0400245
Dees_Troy51127312012-09-08 13:08:49 -0400246 for (index = 0; index < line_len; index++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500247 if (full_line[index] == 34)
248 skip = !skip;
249 if (!skip && full_line[index] <= 32)
Dees_Troy5bf43922012-09-07 16:07:55 -0400250 full_line[index] = '\0';
Dees_Troy5bf43922012-09-07 16:07:55 -0400251 }
Dees_Troy7c2dec82012-09-26 09:49:14 -0400252 Mount_Point = full_line;
Dees_Troy2673cec2013-04-02 20:22:16 +0000253 LOGINFO("Processing '%s'\n", Mount_Point.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -0400254 Backup_Path = Mount_Point;
Dees_Troya13d74f2013-03-24 08:54:55 -0500255 Storage_Path = Mount_Point;
Dees_Troy70737fa2013-04-08 13:19:20 +0000256 Display_Name = full_line + 1;
257 Backup_Display_Name = Display_Name;
258 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400259 index = Mount_Point.size();
260 while (index < line_len) {
261 while (index < line_len && full_line[index] == '\0')
262 index++;
263 if (index >= line_len)
264 continue;
265 ptr = full_line + index;
266 if (item_index == 0) {
267 // File System
268 Fstab_File_System = ptr;
269 Current_File_System = ptr;
270 item_index++;
271 } else if (item_index == 1) {
272 // Primary Block Device
Dees_Troy38bd7602012-09-14 13:33:53 -0400273 if (Fstab_File_System == "mtd" || Fstab_File_System == "yaffs2") {
Dees_Troy094207a2012-09-26 12:00:39 -0400274 MTD_Name = ptr;
275 Find_MTD_Block_Device(MTD_Name);
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400276 } else if (Fstab_File_System == "bml") {
277 if (Mount_Point == "/boot")
278 MTD_Name = "boot";
279 else if (Mount_Point == "/recovery")
280 MTD_Name = "recovery";
281 Primary_Block_Device = ptr;
282 if (*ptr != '/')
Dees_Troy2673cec2013-04-02 20:22:16 +0000283 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 -0400284 } else if (*ptr != '/') {
Dees_Troy5bf43922012-09-07 16:07:55 -0400285 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000286 LOGERR("Invalid block device on '%s', '%s', %i\n", Line.c_str(), ptr, index);
Dees_Troy5bf43922012-09-07 16:07:55 -0400287 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000288 LOGINFO("Invalid block device on '%s', '%s', %i\n", Line.c_str(), ptr, index);
Dees_Troy5bf43922012-09-07 16:07:55 -0400289 return 0;
Dees_Troy38bd7602012-09-14 13:33:53 -0400290 } else {
291 Primary_Block_Device = ptr;
292 Find_Real_Block_Device(Primary_Block_Device, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400293 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400294 item_index++;
295 } else if (item_index > 1) {
296 if (*ptr == '/') {
297 // Alternate Block Device
298 Alternate_Block_Device = ptr;
299 Find_Real_Block_Device(Alternate_Block_Device, Display_Error);
300 } else if (strlen(ptr) > 7 && strncmp(ptr, "length=", 7) == 0) {
301 // Partition length
302 ptr += 7;
303 Length = atoi(ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400304 } else if (strlen(ptr) > 6 && strncmp(ptr, "flags=", 6) == 0) {
305 // Custom flags, save for later so that new values aren't overwritten by defaults
306 ptr += 6;
Matt Mower2416a502016-04-12 19:54:46 -0500307 strlcpy(twflags, ptr, sizeof(twflags));
308 Process_TW_Flags(twflags, Display_Error);
Dees_Troy38bd7602012-09-14 13:33:53 -0400309 } else if (strlen(ptr) == 4 && (strncmp(ptr, "NULL", 4) == 0 || strncmp(ptr, "null", 4) == 0 || strncmp(ptr, "null", 4) == 0)) {
310 // Do nothing
Dees_Troy5bf43922012-09-07 16:07:55 -0400311 } else {
312 // Unhandled data
Dees_Troy2673cec2013-04-02 20:22:16 +0000313 LOGINFO("Unhandled fstab information: '%s', %i, line: '%s'\n", ptr, index, Line.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400314 }
315 }
316 while (index < line_len && full_line[index] != '\0')
317 index++;
318 }
319
320 if (!Is_File_System(Fstab_File_System) && !Is_Image(Fstab_File_System)) {
321 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000322 LOGERR("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400323 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000324 LOGINFO("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400325 return 0;
326 } else if (Is_File_System(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400327 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400328 Setup_File_System(Display_Error);
329 if (Mount_Point == "/system") {
330 Display_Name = "System";
Dees_Troya13d74f2013-03-24 08:54:55 -0500331 Backup_Display_Name = Display_Name;
332 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400333 Wipe_Available_in_GUI = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500334 Can_Be_Backed_Up = true;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500335 Mount_Read_Only = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400336 } else if (Mount_Point == "/data") {
Dees Troyc657cc02015-01-16 22:48:47 +0000337 UnMount(false); // added in case /data is mounted as tmpfs for qcom hardware decrypt
Dees_Troy5bf43922012-09-07 16:07:55 -0400338 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;
Ethan Yonker6277c792014-09-15 14:54:30 -0500346 if (datamedia)
that9e0593e2014-10-08 00:01:24 +0200347 Setup_Data_Media();
Dees_Troy5bf43922012-09-07 16:07:55 -0400348#ifdef TW_INCLUDE_CRYPTO
349 Can_Be_Encrypted = true;
Dees_Troy657c3092012-09-10 20:32:10 -0400350 char crypto_blkdev[255];
351 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
352 if (strcmp(crypto_blkdev, "error") != 0) {
Dees_Troy657c3092012-09-10 20:32:10 -0400353 DataManager::SetValue(TW_IS_DECRYPTED, 1);
354 Is_Encrypted = true;
355 Is_Decrypted = true;
356 Decrypted_Block_Device = crypto_blkdev;
Dees_Troy2673cec2013-04-02 20:22:16 +0000357 LOGINFO("Data already decrypted, new block device: '%s'\n", crypto_blkdev);
Dees_Troy657c3092012-09-10 20:32:10 -0400358 } else if (!Mount(false)) {
Ethan Yonker71413f42014-02-26 13:36:08 -0600359 if (Is_Present) {
Ethan Yonker253368a2014-11-25 15:00:52 -0600360 set_partition_data(Actual_Block_Device.c_str(), Crypto_Key_Location.c_str(), Fstab_File_System.c_str());
Ethan Yonker71413f42014-02-26 13:36:08 -0600361 if (cryptfs_check_footer() == 0) {
362 Is_Encrypted = true;
363 Is_Decrypted = false;
364 Can_Be_Mounted = false;
365 Current_File_System = "emmc";
366 Setup_Image(Display_Error);
367 DataManager::SetValue(TW_IS_ENCRYPTED, 1);
Vojtech Bocek7e11ac52015-03-05 23:21:49 +0100368 DataManager::SetValue(TW_CRYPTO_PWTYPE, cryptfs_get_password_type());
Ethan Yonker71413f42014-02-26 13:36:08 -0600369 DataManager::SetValue(TW_CRYPTO_PASSWORD, "");
370 DataManager::SetValue("tw_crypto_display", "");
371 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500372 gui_err("mount_data_footer=Could not mount /data and unable to find crypto footer.");
Ethan Yonker71413f42014-02-26 13:36:08 -0600373 }
374 } else {
375 LOGERR("Primary block device '%s' for mount point '%s' is not present!\n", Primary_Block_Device.c_str(), Mount_Point.c_str());
376 }
Gary Peck82599a82012-11-21 16:23:12 -0800377 } else {
378 // Filesystem is not encrypted and the mount
379 // succeeded, so get it back to the original
380 // unmounted state
381 UnMount(false);
Dees_Troy51127312012-09-08 13:08:49 -0400382 }
Ethan Yonker6277c792014-09-15 14:54:30 -0500383 if (datamedia && (!Is_Encrypted || (Is_Encrypted && Is_Decrypted)))
Dees_Troy9b21af72012-10-01 15:51:46 -0400384 Recreate_Media_Folder();
Dees_Troy9b21af72012-10-01 15:51:46 -0400385#else
Ethan Yonker6277c792014-09-15 14:54:30 -0500386 if (datamedia)
387 Recreate_Media_Folder();
Dees_Troy5bf43922012-09-07 16:07:55 -0400388#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400389 } else if (Mount_Point == "/cache") {
390 Display_Name = "Cache";
Dees_Troya13d74f2013-03-24 08:54:55 -0500391 Backup_Display_Name = Display_Name;
392 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400393 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400394 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500395 Can_Be_Backed_Up = true;
Dees_Troyce2fe772012-09-28 12:34:33 -0400396 if (Mount(false) && !TWFunc::Path_Exists("/cache/recovery/.")) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000397 LOGINFO("Recreating /cache/recovery folder.\n");
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500398 if (mkdir("/cache/recovery", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0)
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500399 return -1;
Dees_Troyb46a6842012-09-25 11:06:46 -0400400 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400401 } else if (Mount_Point == "/datadata") {
Dees_Troy51127312012-09-08 13:08:49 -0400402 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400403 Display_Name = "DataData";
Dees_Troya13d74f2013-03-24 08:54:55 -0500404 Backup_Display_Name = Display_Name;
405 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400406 Is_SubPartition = true;
407 SubPartition_Of = "/data";
Dees_Troy5bf43922012-09-07 16:07:55 -0400408 DataManager::SetValue(TW_HAS_DATADATA, 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500409 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000410 Can_Encrypt_Backup = true;
411 Use_Userdata_Encryption = false; // This whole partition should be encrypted
Dees_Troy5bf43922012-09-07 16:07:55 -0400412 } else if (Mount_Point == "/sd-ext") {
Dees_Troy51127312012-09-08 13:08:49 -0400413 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400414 Display_Name = "SD-Ext";
Dees_Troya13d74f2013-03-24 08:54:55 -0500415 Backup_Display_Name = Display_Name;
416 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400417 Wipe_Available_in_GUI = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400418 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500419 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000420 Can_Encrypt_Backup = true;
421 Use_Userdata_Encryption = true;
Dees_Troy2c50e182012-09-26 20:05:28 -0400422 } else if (Mount_Point == "/boot") {
423 Display_Name = "Boot";
Dees_Troya13d74f2013-03-24 08:54:55 -0500424 Backup_Display_Name = Display_Name;
Dees_Troy2c50e182012-09-26 20:05:28 -0400425 DataManager::SetValue("tw_boot_is_mountable", 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500426 Can_Be_Backed_Up = true;
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500427 } else if (Mount_Point == "/vendor") {
428 Display_Name = "Vendor";
429 Backup_Display_Name = Display_Name;
430 Storage_Name = Display_Name;
431 Mount_Read_Only = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400432 }
433#ifdef TW_EXTERNAL_STORAGE_PATH
434 if (Mount_Point == EXPAND(TW_EXTERNAL_STORAGE_PATH)) {
435 Is_Storage = true;
436 Storage_Path = EXPAND(TW_EXTERNAL_STORAGE_PATH);
Dees_Troyc51f1f92012-09-20 15:32:13 -0400437 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500438 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400439#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000440 if (Mount_Point == "/sdcard" || Mount_Point == "/external_sd" || Mount_Point == "/external_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400441 Is_Storage = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400442 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500443 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400444#endif
Dees_Troyb05ddee2013-01-28 20:24:50 +0000445 }
Dees_Troy8170a922012-09-18 15:40:25 -0400446#ifdef TW_INTERNAL_STORAGE_PATH
447 if (Mount_Point == EXPAND(TW_INTERNAL_STORAGE_PATH)) {
448 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500449 Is_Settings_Storage = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400450 Storage_Path = EXPAND(TW_INTERNAL_STORAGE_PATH);
Dees_Troya13d74f2013-03-24 08:54:55 -0500451 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400452 }
453#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000454 if (Mount_Point == "/emmc" || Mount_Point == "/internal_sd" || Mount_Point == "/internal_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400455 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500456 Is_Settings_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500457 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400458 }
459#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400460 } else if (Is_Image(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400461 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400462 Setup_Image(Display_Error);
Dees_Troya13d74f2013-03-24 08:54:55 -0500463 if (Mount_Point == "/boot") {
464 Display_Name = "Boot";
465 Backup_Display_Name = Display_Name;
466 Can_Be_Backed_Up = true;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600467 Can_Flash_Img = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500468 } else if (Mount_Point == "/recovery") {
469 Display_Name = "Recovery";
470 Backup_Display_Name = Display_Name;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600471 Can_Flash_Img = true;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500472 } else if (Mount_Point == "/system_image") {
473 Display_Name = "System Image";
474 Backup_Display_Name = Display_Name;
HashBanged974bb2016-01-30 14:20:09 -0500475 Can_Flash_Img = true;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500476 Can_Be_Backed_Up = true;
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500477 } else if (Mount_Point == "/vendor_image") {
478 Display_Name = "Vendor Image";
479 Backup_Display_Name = Display_Name;
HashBanged974bb2016-01-30 14:20:09 -0500480 Can_Flash_Img = true;
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500481 Can_Be_Backed_Up = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500482 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400483 }
484
Matt Mower2416a502016-04-12 19:54:46 -0500485 // Process TWRP fstab flags
486 if (strlen(twflags) > 0) {
487 string Prev_Display_Name = Display_Name;
488 string Prev_Storage_Name = Storage_Name;
489 string Prev_Backup_Display_Name = Backup_Display_Name;
490 Display_Name = "";
491 Storage_Name = "";
492 Backup_Display_Name = "";
493
494 Process_TW_Flags(twflags, Display_Error);
495
496 bool has_display_name = !Display_Name.empty();
497 bool has_storage_name = !Storage_Name.empty();
498 bool has_backup_name = !Backup_Display_Name.empty();
499 if (!has_display_name) Display_Name = Prev_Display_Name;
500 if (!has_storage_name) Storage_Name = Prev_Storage_Name;
501 if (!has_backup_name) Backup_Display_Name = Prev_Backup_Display_Name;
502
503 if (has_display_name && !has_storage_name)
504 Storage_Name = Display_Name;
505 if (!has_display_name && has_storage_name)
506 Display_Name = Storage_Name;
507 if (has_display_name && !has_backup_name && Backup_Display_Name != "Android Secure")
508 Backup_Display_Name = Display_Name;
509 if (!has_display_name && has_backup_name)
510 Display_Name = Backup_Display_Name;
511 }
Dees_Troy51127312012-09-08 13:08:49 -0400512 return true;
513}
514
Ketut Putu Kumajayabfb72b02015-08-10 16:40:55 +0200515bool TWPartition::Process_FS_Flags(string& Options, int& Flags) {
Hashcode62bd9e02013-11-19 21:59:42 -0800516 int i;
517 char *p;
518 char *savep;
519 char fs_options[250];
520
521 strlcpy(fs_options, Options.c_str(), sizeof(fs_options));
522 Options = "";
523
524 p = strtok_r(fs_options, ",", &savep);
525 while (p) {
526 /* Look for the flag "p" in the flag list "fl"
527 * If not found, the loop exits with fl[i].name being null.
528 */
529 for (i = 0; mount_flags[i].name; i++) {
530 if (strncmp(p, mount_flags[i].name, strlen(mount_flags[i].name)) == 0) {
531 Flags |= mount_flags[i].flag;
532 break;
533 }
534 }
535
536 if (!mount_flags[i].name) {
537 if (Options.size() > 0)
538 Options += ",";
539 Options += p;
540 }
541 p = strtok_r(NULL, ",", &savep);
542 }
543
544 return true;
545}
546
Matt Mower2416a502016-04-12 19:54:46 -0500547void TWPartition::Apply_TW_Flag(const unsigned flag, const char* str, const bool val) {
548 switch (flag) {
549 case TWFLAG_ANDSEC:
550 Has_Android_Secure = val;
551 break;
552 case TWFLAG_BACKUP:
553 Can_Be_Backed_Up = val;
554 break;
555 case TWFLAG_BACKUPNAME:
556 Backup_Display_Name = str;
557 break;
558 case TWFLAG_BLOCKSIZE:
559 Format_Block_Size = (unsigned long)(atol(str));
560 break;
561 case TWFLAG_CANBEWIPED:
562 Can_Be_Wiped = val;
563 break;
564 case TWFLAG_CANENCRYPTBACKUP:
565 Can_Encrypt_Backup = val;
566 break;
567 case TWFLAG_DEFAULTS:
568 // Do nothing
569 break;
570 case TWFLAG_DISPLAY:
571 Display_Name = str;
572 break;
573 case TWFLAG_ENCRYPTABLE:
574 case TWFLAG_FORCEENCRYPT:
575 Crypto_Key_Location = str;
576 break;
577 case TWFLAG_FLASHIMG:
578 Can_Flash_Img = val;
579 break;
580 case TWFLAG_FSFLAGS:
581 Mount_Options = str;
582 Process_FS_Flags(Mount_Options, Mount_Flags);
583 break;
584 case TWFLAG_IGNOREBLKID:
585 Ignore_Blkid = val;
586 break;
587 case TWFLAG_LENGTH:
588 Length = atoi(str);
589 break;
590 case TWFLAG_MOUNTTODECRYPT:
591 Mount_To_Decrypt = val;
592 break;
593 case TWFLAG_REMOVABLE:
594 Removable = val;
595 break;
596 case TWFLAG_RETAINLAYOUTVERSION:
597 Retain_Layout_Version = val;
598 break;
599 case TWFLAG_SETTINGSSTORAGE:
600 Is_Settings_Storage = val;
601 if (Is_Settings_Storage)
602 Is_Storage = true;
603 break;
604 case TWFLAG_STORAGE:
605 Is_Storage = val;
606 break;
607 case TWFLAG_STORAGENAME:
608 Storage_Name = str;
609 break;
610 case TWFLAG_SUBPARTITIONOF:
611 Is_SubPartition = true;
612 SubPartition_Of = str;
613 break;
614 case TWFLAG_SYMLINK:
615 Symlink_Path = str;
616 break;
617 case TWFLAG_USERDATAENCRYPTBACKUP:
618 Use_Userdata_Encryption = val;
619 if (Use_Userdata_Encryption)
620 Can_Encrypt_Backup = true;
621 break;
622 case TWFLAG_USERMRF:
623 Use_Rm_Rf = val;
624 break;
625 case TWFLAG_WIPEDURINGFACTORYRESET:
626 Wipe_During_Factory_Reset = val;
627 if (Wipe_During_Factory_Reset) {
628 Can_Be_Wiped = true;
629 Wipe_Available_in_GUI = true;
630 }
631 break;
632 case TWFLAG_WIPEINGUI:
633 Wipe_Available_in_GUI = val;
634 if (Wipe_Available_in_GUI)
635 Can_Be_Wiped = true;
636 break;
637 default:
638 // Should not get here
639 LOGINFO("Flag identified for processing, but later unmatched: %i\n", flag);
640 break;
641 }
642}
Dees_Troy51127312012-09-08 13:08:49 -0400643
Matt Mower2416a502016-04-12 19:54:46 -0500644void TWPartition::Process_TW_Flags(char *flags, bool Display_Error) {
645 char separator[2] = {'\n', 0};
646 char *ptr, *savep;
647
648 // Semicolons within double-quotes are not forbidden, so replace
649 // only the semicolons intended as separators with '\n' for strtok
650 for (unsigned i = 0, skip = 0; i < strlen(flags); i++) {
651 if (flags[i] == '\"')
Dees_Troya13d74f2013-03-24 08:54:55 -0500652 skip = !skip;
Matt Mower2416a502016-04-12 19:54:46 -0500653 if (!skip && flags[i] == ';')
654 flags[i] = separator[0];
Dees_Troy51127312012-09-08 13:08:49 -0400655 }
656
Matt Mower2416a502016-04-12 19:54:46 -0500657 // Avoid issues with potentially nested strtok by using strtok_r
658 ptr = strtok_r(flags, separator, &savep);
659 while (ptr) {
660 int ptr_len = strlen(ptr);
661 const struct flag_list* tw_flag = tw_flags;
Hashcode62bd9e02013-11-19 21:59:42 -0800662
Matt Mower2416a502016-04-12 19:54:46 -0500663 for (; tw_flag->name; tw_flag++) {
664 int flag_len = strlen(tw_flag->name);
665
666 if (strncmp(ptr, tw_flag->name, flag_len) == 0) {
667 bool flag_val = false;
668
669 if (ptr_len > flag_len && (tw_flag->name)[flag_len-1] != '='
670 && ptr[flag_len] != '=') {
671 // Handle flags with same starting string
672 // (e.g. backup and backupname)
673 continue;
674 } else if (ptr_len > flag_len && ptr[flag_len] == '=') {
675 // Handle flags with dual format: Part 1
676 // (e.g. backup and backup=y. backup=y handled here)
677 ptr += flag_len + 1;
678 TWFunc::Strip_Quotes(ptr);
679 // Skip flags with empty argument
680 // (e.g. backup=)
681 if (strlen(ptr) == 0) {
682 LOGINFO("Flag missing argument or should not include '=': %s=\n", tw_flag->name);
683 break;
684 }
685 flag_val = strchr("yY1", *ptr) != NULL;
686 } else if (ptr_len == flag_len
687 && (tw_flag->name)[flag_len-1] == '=') {
688 // Skip flags missing argument after =
689 // (e.g. backupname=)
690 LOGINFO("Flag missing argument: %s\n", tw_flag->name);
691 break;
692 } else if (ptr_len > flag_len
693 && (tw_flag->name)[flag_len-1] == '=') {
694 // Handle arguments to flags
695 // (e.g. backupname="My Stuff")
696 ptr += flag_len;
697 TWFunc::Strip_Quotes(ptr);
698 // Skip flags with empty argument
699 // (e.g. backupname="")
700 if (strlen(ptr) == 0) {
701 LOGINFO("Flag missing argument: %s\n", tw_flag->name);
702 break;
703 }
704 } else if (ptr_len == flag_len) {
705 // Handle flags with dual format: Part 2
706 // (e.g. backup and backup=y. backup handled here)
707 flag_val = true;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600708 } else {
Matt Mower2416a502016-04-12 19:54:46 -0500709 LOGINFO("Flag matched, but could not be processed: %s\n", ptr);
710 break;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600711 }
Matt Mower2416a502016-04-12 19:54:46 -0500712
713 Apply_TW_Flag(tw_flag->flag, ptr, flag_val);
714 break;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600715 }
Matt Mower2416a502016-04-12 19:54:46 -0500716 }
717 if (tw_flag->name == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400718 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000719 LOGERR("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400720 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000721 LOGINFO("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400722 }
Matt Mower2416a502016-04-12 19:54:46 -0500723 ptr = strtok_r(NULL, separator, &savep);
Dees_Troy51127312012-09-08 13:08:49 -0400724 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400725}
726
Dees_Troy5bf43922012-09-07 16:07:55 -0400727bool TWPartition::Is_File_System(string File_System) {
728 if (File_System == "ext2" ||
Dees_Troy63c8df72012-09-10 14:02:05 -0400729 File_System == "ext3" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400730 File_System == "ext4" ||
731 File_System == "vfat" ||
732 File_System == "ntfs" ||
733 File_System == "yaffs2" ||
bigbiff bigbiff3e146522012-11-14 14:32:59 -0500734 File_System == "exfat" ||
Dees_Troye5017042013-08-29 16:38:55 +0000735 File_System == "f2fs" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400736 File_System == "auto")
737 return true;
738 else
739 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400740}
741
Dees_Troy5bf43922012-09-07 16:07:55 -0400742bool TWPartition::Is_Image(string File_System) {
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400743 if (File_System == "emmc" || File_System == "mtd" || File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -0400744 return true;
745 else
746 return false;
747}
748
Dees_Troy51127312012-09-08 13:08:49 -0400749bool TWPartition::Make_Dir(string Path, bool Display_Error) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400750 if (!TWFunc::Path_Exists(Path)) {
Dees_Troy51127312012-09-08 13:08:49 -0400751 if (mkdir(Path.c_str(), 0777) == -1) {
752 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -0500753 gui_msg(Msg(msg::kError, "create_folder_strerr=Can not create '{1}' folder ({2}).")(Path)(strerror(errno)));
Dees_Troy51127312012-09-08 13:08:49 -0400754 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000755 LOGINFO("Can not create '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400756 return false;
757 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000758 LOGINFO("Created '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400759 return true;
760 }
761 }
762 return true;
763}
764
Dees_Troy5bf43922012-09-07 16:07:55 -0400765void TWPartition::Setup_File_System(bool Display_Error) {
766 struct statfs st;
767
768 Can_Be_Mounted = true;
769 Can_Be_Wiped = true;
770
Dees_Troy5bf43922012-09-07 16:07:55 -0400771 // Make the mount point folder if it doesn't exist
Dees_Troy51127312012-09-08 13:08:49 -0400772 Make_Dir(Mount_Point, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400773 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
774 Backup_Name = Display_Name;
775 Backup_Method = FILES;
776}
777
778void TWPartition::Setup_Image(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400779 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
780 Backup_Name = Display_Name;
Gary Peck82599a82012-11-21 16:23:12 -0800781 if (Current_File_System == "emmc")
Dees_Troy5bf43922012-09-07 16:07:55 -0400782 Backup_Method = DD;
Gary Peck82599a82012-11-21 16:23:12 -0800783 else if (Current_File_System == "mtd" || Current_File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -0400784 Backup_Method = FLASH_UTILS;
785 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000786 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 -0400787 if (Find_Partition_Size()) {
788 Used = Size;
789 Backup_Size = Size;
Dees_Troy51a0e822012-09-05 15:24:24 -0400790 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400791 if (Display_Error)
Dees Troyd932ce12013-10-18 17:12:59 +0000792 LOGERR("Unable to find partition size for '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400793 else
Dees Troyd932ce12013-10-18 17:12:59 +0000794 LOGINFO("Unable to find partition size for '%s'\n", Mount_Point.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400795 }
796}
797
Dees_Troye58d5262012-09-21 12:27:57 -0400798void TWPartition::Setup_AndSec(void) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500799 Backup_Display_Name = "Android Secure";
Dees_Troye58d5262012-09-21 12:27:57 -0400800 Backup_Name = "and-sec";
Dees_Troya13d74f2013-03-24 08:54:55 -0500801 Can_Be_Backed_Up = true;
Dees_Troye58d5262012-09-21 12:27:57 -0400802 Has_Android_Secure = true;
803 Symlink_Path = Mount_Point + "/.android_secure";
804 Symlink_Mount_Point = "/and-sec";
805 Backup_Path = Symlink_Mount_Point;
806 Make_Dir("/and-sec", true);
807 Recreate_AndSec_Folder();
Ethan Yonkerd4d10732014-02-03 15:27:52 -0600808 Mount_Storage_Retry();
Dees_Troye58d5262012-09-21 12:27:57 -0400809}
810
that9e0593e2014-10-08 00:01:24 +0200811void TWPartition::Setup_Data_Media() {
Ethan Yonker6277c792014-09-15 14:54:30 -0500812 LOGINFO("Setting up '%s' as data/media emulated storage.\n", Mount_Point.c_str());
Ethan Yonker66a19492015-12-10 10:19:45 -0600813 if (Storage_Name.empty() || Storage_Name == "Data")
814 Storage_Name = "Internal Storage";
Ethan Yonker6277c792014-09-15 14:54:30 -0500815 Has_Data_Media = true;
816 Is_Storage = true;
Ethan Yonker66a19492015-12-10 10:19:45 -0600817 Storage_Path = Mount_Point + "/media";
Ethan Yonker6277c792014-09-15 14:54:30 -0500818 Symlink_Path = Storage_Path;
Ethan Yonker66a19492015-12-10 10:19:45 -0600819 if (Mount_Point == "/data") {
820 Is_Settings_Storage = true;
821 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
822 Make_Dir("/emmc", false);
823 Symlink_Mount_Point = "/emmc";
824 } else {
825 Make_Dir("/sdcard", false);
826 Symlink_Mount_Point = "/sdcard";
827 }
828 if (Mount(false) && TWFunc::Path_Exists(Mount_Point + "/media/0")) {
829 Storage_Path = Mount_Point + "/media/0";
830 Symlink_Path = Storage_Path;
831 DataManager::SetValue(TW_INTERNAL_PATH, Mount_Point + "/media/0");
832 UnMount(true);
833 }
834 DataManager::SetValue("tw_has_internal", 1);
835 DataManager::SetValue("tw_has_data_media", 1);
836 du.add_absolute_dir(Mount_Point + "/misc/vold");
837 du.add_absolute_dir(Mount_Point + "/.layout_version");
838 du.add_absolute_dir(Mount_Point + "/system/storage.xml");
Ethan Yonker6277c792014-09-15 14:54:30 -0500839 } else {
Ethan Yonker66a19492015-12-10 10:19:45 -0600840 if (Mount(true) && TWFunc::Path_Exists(Mount_Point + "/media/0")) {
841 Storage_Path = Mount_Point + "/media/0";
842 Symlink_Path = Storage_Path;
843 UnMount(true);
844 }
Ethan Yonker6277c792014-09-15 14:54:30 -0500845 }
Ethan Yonker66a19492015-12-10 10:19:45 -0600846 du.add_absolute_dir(Mount_Point + "/media");
Ethan Yonker6277c792014-09-15 14:54:30 -0500847}
848
Dees_Troy5bf43922012-09-07 16:07:55 -0400849void TWPartition::Find_Real_Block_Device(string& Block, bool Display_Error) {
850 char device[512], realDevice[512];
851
852 strcpy(device, Block.c_str());
853 memset(realDevice, 0, sizeof(realDevice));
854 while (readlink(device, realDevice, sizeof(realDevice)) > 0)
855 {
856 strcpy(device, realDevice);
857 memset(realDevice, 0, sizeof(realDevice));
858 }
859
860 if (device[0] != '/') {
861 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000862 LOGERR("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400863 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000864 LOGINFO("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400865 return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400866 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400867 Block = device;
868 return;
869 }
870}
871
Dees_Troy8e337f32012-10-13 22:07:49 -0400872void TWPartition::Mount_Storage_Retry(void) {
873 // On some devices, storage doesn't want to mount right away, retry and sleep
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500874 if (!Mount(true)) {
Dees_Troy8e337f32012-10-13 22:07:49 -0400875 int retry_count = 5;
876 while (retry_count > 0 && !Mount(false)) {
877 usleep(500000);
878 retry_count--;
879 }
880 Mount(true);
881 }
882}
883
Dees_Troy38bd7602012-09-14 13:33:53 -0400884bool TWPartition::Find_MTD_Block_Device(string MTD_Name) {
885 FILE *fp = NULL;
886 char line[255];
887
888 fp = fopen("/proc/mtd", "rt");
889 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000890 LOGERR("Device does not support /proc/mtd\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400891 return false;
892 }
893
894 while (fgets(line, sizeof(line), fp) != NULL)
895 {
896 char device[32], label[32];
897 unsigned long size = 0;
898 char* fstype = NULL;
899 int deviceId;
900
901 sscanf(line, "%s %lx %*s %*c%s", device, &size, label);
902
903 // Skip header and blank lines
904 if ((strcmp(device, "dev:") == 0) || (strlen(line) < 8))
905 continue;
906
907 // Strip off the trailing " from the label
908 label[strlen(label)-1] = '\0';
909
910 if (strcmp(label, MTD_Name.c_str()) == 0) {
911 // We found our device
912 // Strip off the trailing : from the device
913 device[strlen(device)-1] = '\0';
914 if (sscanf(device,"mtd%d", &deviceId) == 1) {
915 sprintf(device, "/dev/block/mtdblock%d", deviceId);
916 Primary_Block_Device = device;
Dees_Troy76543db2013-06-19 16:24:30 +0000917 fclose(fp);
918 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -0400919 }
920 }
921 }
922 fclose(fp);
923
924 return false;
925}
926
Dees_Troy51127312012-09-08 13:08:49 -0400927bool TWPartition::Get_Size_Via_statfs(bool Display_Error) {
928 struct statfs st;
929 string Local_Path = Mount_Point + "/.";
930
931 if (!Mount(Display_Error))
932 return false;
933
934 if (statfs(Local_Path.c_str(), &st) != 0) {
935 if (!Removable) {
936 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000937 LOGERR("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400938 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000939 LOGINFO("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400940 }
941 return false;
942 }
943 Size = (st.f_blocks * st.f_bsize);
944 Used = ((st.f_blocks - st.f_bfree) * st.f_bsize);
945 Free = (st.f_bfree * st.f_bsize);
946 Backup_Size = Used;
947 return true;
948}
949
950bool TWPartition::Get_Size_Via_df(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400951 FILE* fp;
952 char command[255], line[512];
953 int include_block = 1;
954 unsigned int min_len;
955
956 if (!Mount(Display_Error))
957 return false;
958
Dees_Troy38bd7602012-09-14 13:33:53 -0400959 min_len = Actual_Block_Device.size() + 2;
Dees_Troy51127312012-09-08 13:08:49 -0400960 sprintf(command, "df %s > /tmp/dfoutput.txt", Mount_Point.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +0200961 TWFunc::Exec_Cmd(command);
Dees_Troy51127312012-09-08 13:08:49 -0400962 fp = fopen("/tmp/dfoutput.txt", "rt");
963 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000964 LOGINFO("Unable to open /tmp/dfoutput.txt.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400965 return false;
Dees_Troy51127312012-09-08 13:08:49 -0400966 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400967
968 while (fgets(line, sizeof(line), fp) != NULL)
969 {
970 unsigned long blocks, used, available;
971 char device[64];
972 char tmpString[64];
973
974 if (strncmp(line, "Filesystem", 10) == 0)
975 continue;
976 if (strlen(line) < min_len) {
977 include_block = 0;
978 continue;
979 }
980 if (include_block) {
981 sscanf(line, "%s %lu %lu %lu", device, &blocks, &used, &available);
982 } else {
983 // The device block string is so long that the df information is on the next line
984 int space_count = 0;
Dees_Troye58d5262012-09-21 12:27:57 -0400985 sprintf(tmpString, "/dev/block/%s", Actual_Block_Device.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400986 while (tmpString[space_count] == 32)
987 space_count++;
988 sscanf(line + space_count, "%lu %lu %lu", &blocks, &used, &available);
989 }
990
991 // Adjust block size to byte size
992 Size = blocks * 1024ULL;
993 Used = used * 1024ULL;
994 Free = available * 1024ULL;
995 Backup_Size = Used;
996 }
997 fclose(fp);
998 return true;
999}
1000
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001001unsigned long long TWPartition::IOCTL_Get_Block_Size() {
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001002 Find_Actual_Block_Device();
Ethan Yonker483e9f42016-01-11 22:21:18 -06001003
1004 return TWFunc::IOCTL_Get_Block_Size(Actual_Block_Device.c_str());
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001005}
1006
Dees_Troy5bf43922012-09-07 16:07:55 -04001007bool TWPartition::Find_Partition_Size(void) {
1008 FILE* fp;
1009 char line[512];
1010 string tmpdevice;
1011
igoriok87e3d932013-01-31 21:03:53 +02001012 fp = fopen("/proc/dumchar_info", "rt");
1013 if (fp != NULL) {
1014 while (fgets(line, sizeof(line), fp) != NULL)
1015 {
1016 char label[32], device[32];
1017 unsigned long size = 0;
1018
thatd43bf2d2014-09-21 23:13:02 +02001019 sscanf(line, "%s %lx %*x %*u %s", label, &size, device);
igoriok87e3d932013-01-31 21:03:53 +02001020
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05001021 // Skip header, annotation and blank lines
igoriok87e3d932013-01-31 21:03:53 +02001022 if ((strncmp(device, "/dev/", 5) != 0) || (strlen(line) < 8))
1023 continue;
1024
1025 tmpdevice = "/dev/";
1026 tmpdevice += label;
1027 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
1028 Size = size;
1029 fclose(fp);
1030 return true;
1031 }
1032 }
1033 }
1034
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001035 unsigned long long ioctl_size = IOCTL_Get_Block_Size();
1036 if (ioctl_size) {
1037 Size = ioctl_size;
1038 return true;
1039 }
1040
Dees_Troy5bf43922012-09-07 16:07:55 -04001041 // In this case, we'll first get the partitions we care about (with labels)
1042 fp = fopen("/proc/partitions", "rt");
1043 if (fp == NULL)
1044 return false;
1045
1046 while (fgets(line, sizeof(line), fp) != NULL)
1047 {
1048 unsigned long major, minor, blocks;
1049 char device[512];
1050 char tmpString[64];
1051
Dees_Troy63c8df72012-09-10 14:02:05 -04001052 if (strlen(line) < 7 || line[0] == 'm') continue;
Dees_Troy5bf43922012-09-07 16:07:55 -04001053 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
1054
1055 tmpdevice = "/dev/block/";
1056 tmpdevice += device;
Dees_Troy38bd7602012-09-14 13:33:53 -04001057 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001058 // Adjust block size to byte size
1059 Size = blocks * 1024ULL;
1060 fclose(fp);
1061 return true;
1062 }
1063 }
1064 fclose(fp);
1065 return false;
1066}
1067
Dees_Troy5bf43922012-09-07 16:07:55 -04001068bool TWPartition::Is_Mounted(void) {
1069 if (!Can_Be_Mounted)
1070 return false;
1071
1072 struct stat st1, st2;
1073 string test_path;
1074
1075 // Check to see if the mount point directory exists
1076 test_path = Mount_Point + "/.";
1077 if (stat(test_path.c_str(), &st1) != 0) return false;
1078
1079 // Check to see if the directory above the mount point exists
1080 test_path = Mount_Point + "/../.";
1081 if (stat(test_path.c_str(), &st2) != 0) return false;
1082
1083 // Compare the device IDs -- if they match then we're (probably) using tmpfs instead of an actual device
1084 int ret = (st1.st_dev != st2.st_dev) ? true : false;
1085
1086 return ret;
1087}
1088
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05001089bool TWPartition::Is_File_System_Writable(void) {
1090 if (!Is_File_System(Current_File_System) || !Is_Mounted())
1091 return false;
1092
1093 string test_path = Mount_Point + "/.";
1094 return (access(test_path.c_str(), W_OK) == 0);
1095}
1096
Dees_Troy5bf43922012-09-07 16:07:55 -04001097bool TWPartition::Mount(bool Display_Error) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001098 int exfat_mounted = 0;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001099 unsigned long flags = Mount_Flags;
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001100
Dees_Troy5bf43922012-09-07 16:07:55 -04001101 if (Is_Mounted()) {
1102 return true;
1103 } else if (!Can_Be_Mounted) {
1104 return false;
1105 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001106
1107 Find_Actual_Block_Device();
1108
1109 // Check the current file system before mounting
1110 Check_FS_Type();
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001111 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/exfat-fuse")) {
Dees_Troye34c1332013-02-06 19:13:00 +00001112 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 +00001113 LOGINFO("cmd: %s\n", cmd.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001114 string result;
1115 if (TWFunc::Exec_Cmd(cmd, result) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001116 LOGINFO("exfat-fuse failed to mount with result '%s', trying vfat\n", result.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001117 Current_File_System = "vfat";
1118 } else {
1119#ifdef TW_NO_EXFAT_FUSE
1120 UnMount(false);
1121 // We'll let the kernel handle it but using exfat-fuse to detect if the file system is actually exfat
1122 // Some kernels let us mount vfat as exfat which doesn't work out too well
1123#else
1124 exfat_mounted = 1;
1125#endif
1126 }
1127 }
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001128
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001129 if (Current_File_System == "ntfs" && (TWFunc::Path_Exists("/sbin/ntfs-3g") || TWFunc::Path_Exists("/sbin/mount.ntfs"))) {
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001130 string cmd;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001131 string Ntfsmount_Binary = "";
1132
1133 if (TWFunc::Path_Exists("/sbin/ntfs-3g"))
1134 Ntfsmount_Binary = "ntfs-3g";
1135 else if (TWFunc::Path_Exists("/sbin/mount.ntfs"))
1136 Ntfsmount_Binary = "mount.ntfs";
1137
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001138 if (Mount_Read_Only)
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001139 cmd = "/sbin/" + Ntfsmount_Binary + " -o ro " + Actual_Block_Device + " " + Mount_Point;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001140 else
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001141 cmd = "/sbin/" + Ntfsmount_Binary + " " + Actual_Block_Device + " " + Mount_Point;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001142 LOGINFO("cmd: '%s'\n", cmd.c_str());
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001143
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001144 if (TWFunc::Exec_Cmd(cmd) == 0) {
1145 return true;
1146 } else {
1147 LOGINFO("ntfs-3g failed to mount, trying regular mount method.\n");
1148 }
1149 }
1150
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001151 if (Mount_Read_Only)
1152 flags |= MS_RDONLY;
1153
Dees_Troy22042032012-12-18 21:23:08 +00001154 if (Fstab_File_System == "yaffs2") {
1155 // mount an MTD partition as a YAFFS2 filesystem.
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001156 flags = MS_NOATIME | MS_NODEV | MS_NODIRATIME;
1157 if (Mount_Read_Only)
1158 flags |= MS_RDONLY;
Dees_Troy76543db2013-06-19 16:24:30 +00001159 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags, NULL) < 0) {
1160 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags | MS_RDONLY, NULL) < 0) {
1161 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001162 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy76543db2013-06-19 16:24:30 +00001163 else
1164 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
1165 return false;
1166 } else {
1167 LOGINFO("Mounted '%s' (MTD) as RO\n", Mount_Point.c_str());
1168 return true;
1169 }
1170 } else {
1171 struct stat st;
1172 string test_path = Mount_Point;
1173 if (stat(test_path.c_str(), &st) < 0) {
1174 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001175 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy76543db2013-06-19 16:24:30 +00001176 else
1177 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
1178 return false;
1179 }
1180 mode_t new_mode = st.st_mode | S_IXUSR | S_IXGRP | S_IXOTH;
1181 if (new_mode != st.st_mode) {
1182 LOGINFO("Fixing execute permissions for %s\n", Mount_Point.c_str());
1183 if (chmod(Mount_Point.c_str(), new_mode) < 0) {
1184 if (Display_Error)
1185 LOGERR("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
1186 else
1187 LOGINFO("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
1188 return false;
1189 }
1190 }
Dees_Troy22042032012-12-18 21:23:08 +00001191 return true;
Dees_Troy76543db2013-06-19 16:24:30 +00001192 }
thatc7572eb2015-03-31 18:55:30 +02001193 }
1194
1195 string mount_fs = Current_File_System;
1196 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sys/module/texfat"))
1197 mount_fs = "texfat";
1198
1199 if (!exfat_mounted &&
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001200 mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), mount_fs.c_str(), flags, Mount_Options.c_str()) != 0 &&
1201 mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), mount_fs.c_str(), flags, NULL) != 0) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001202#ifdef TW_NO_EXFAT_FUSE
1203 if (Current_File_System == "exfat") {
Dees_Troy2673cec2013-04-02 20:22:16 +00001204 LOGINFO("Mounting exfat failed, trying vfat...\n");
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001205 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), "vfat", 0, NULL) != 0) {
Dees_Troy85f44ed2013-01-09 18:42:36 +00001206 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001207 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy85f44ed2013-01-09 18:42:36 +00001208 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001209 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001210 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 +00001211 return false;
Dees_Troy85f44ed2013-01-09 18:42:36 +00001212 }
Dees_Troyb05ddee2013-01-28 20:24:50 +00001213 } else {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001214#endif
bigbiff bigbiff26774a02014-03-29 18:22:00 -04001215 if (!Removable && Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001216 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001217 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001218 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
1219 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 +00001220 return false;
1221#ifdef TW_NO_EXFAT_FUSE
Dees_Troy85f44ed2013-01-09 18:42:36 +00001222 }
1223#endif
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001224 }
Ethan Yonker253368a2014-11-25 15:00:52 -06001225
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001226 if (Removable)
1227 Update_Size(Display_Error);
1228
xiaolu9416f4f2015-06-04 08:22:23 +08001229 if (!Symlink_Mount_Point.empty() && TWFunc::Path_Exists(Symlink_Path)) {
codelover2a3d4ce2015-03-14 20:26:49 +08001230 string Command = "mount -o bind '" + Symlink_Path + "' '" + Symlink_Mount_Point + "'";
Vojtech Bocek05534202013-09-11 08:11:56 +02001231 TWFunc::Exec_Cmd(Command);
Dees_Troy5bf43922012-09-07 16:07:55 -04001232 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001233 return true;
1234}
1235
1236bool TWPartition::UnMount(bool Display_Error) {
1237 if (Is_Mounted()) {
1238 int never_unmount_system;
1239
1240 DataManager::GetValue(TW_DONT_UNMOUNT_SYSTEM, never_unmount_system);
1241 if (never_unmount_system == 1 && Mount_Point == "/system")
1242 return true; // Never unmount system if you're not supposed to unmount it
1243
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001244 if (Is_Storage)
Ethan Yonker726a0202014-12-16 20:01:38 -06001245 PartitionManager.Remove_MTP_Storage(MTP_Storage_ID);
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001246
Dees_Troy38bd7602012-09-14 13:33:53 -04001247 if (!Symlink_Mount_Point.empty())
1248 umount(Symlink_Mount_Point.c_str());
1249
Dees_Troyb05ddee2013-01-28 20:24:50 +00001250 umount(Mount_Point.c_str());
1251 if (Is_Mounted()) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001252 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001253 gui_msg(Msg(msg::kError, "fail_unmount=Failed to unmount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy5bf43922012-09-07 16:07:55 -04001254 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001255 LOGINFO("Unable to unmount '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001256 return false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001257 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -04001258 return true;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001259 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001260 } else {
1261 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001262 }
1263}
1264
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05001265bool TWPartition::ReMount(bool Display_Error) {
1266 if (UnMount(Display_Error))
1267 return Mount(Display_Error);
1268 return false;
1269}
1270
1271bool TWPartition::ReMount_RW(bool Display_Error) {
1272 // No need to remount if already mounted rw
1273 if (Is_File_System_Writable())
1274 return true;
1275
1276 bool ro = Mount_Read_Only;
1277 int flags = Mount_Flags;
1278
1279 Mount_Read_Only = false;
1280 Mount_Flags &= ~MS_RDONLY;
1281
1282 bool ret = ReMount(Display_Error);
1283
1284 Mount_Read_Only = ro;
1285 Mount_Flags = flags;
1286
1287 return ret;
1288}
1289
Gary Peck43acadf2012-11-21 21:19:01 -08001290bool TWPartition::Wipe(string New_File_System) {
Ethan Yonker726a0202014-12-16 20:01:38 -06001291 bool wiped = false, update_crypt = false, recreate_media = true;
Dees_Troy16c2b312013-01-15 16:51:18 +00001292 int check;
1293 string Layout_Filename = Mount_Point + "/.layout_version";
1294
Dees_Troy38bd7602012-09-14 13:33:53 -04001295 if (!Can_Be_Wiped) {
Matt Mower3c366972015-12-25 19:28:31 -06001296 gui_msg(Msg(msg::kError, "cannot_wipe=Partition {1} cannot be wiped.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04001297 return false;
1298 }
1299
Dees_Troyc51f1f92012-09-20 15:32:13 -04001300 if (Mount_Point == "/cache")
Dees_Troy2673cec2013-04-02 20:22:16 +00001301 Log_Offset = 0;
Dees_Troyc51f1f92012-09-20 15:32:13 -04001302
Dees_Troy16c2b312013-01-15 16:51:18 +00001303 if (Retain_Layout_Version && Mount(false) && TWFunc::Path_Exists(Layout_Filename))
1304 TWFunc::copy_file(Layout_Filename, "/.layout_version", 0600);
1305 else
1306 unlink("/.layout_version");
Dees_Troy38bd7602012-09-14 13:33:53 -04001307
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001308 if (Has_Data_Media && Current_File_System == New_File_System) {
Dees_Troy16c2b312013-01-15 16:51:18 +00001309 wiped = Wipe_Data_Without_Wiping_Media();
Ethan Yonker5eac2222014-06-11 12:22:55 -05001310 recreate_media = false;
Dees_Troy16c2b312013-01-15 16:51:18 +00001311 } else {
Dees_Troy16c2b312013-01-15 16:51:18 +00001312 DataManager::GetValue(TW_RM_RF_VAR, check);
1313
Hashcodedabfd492013-08-29 22:45:30 -07001314 if (check || Use_Rm_Rf)
Dees_Troy16c2b312013-01-15 16:51:18 +00001315 wiped = Wipe_RMRF();
1316 else if (New_File_System == "ext4")
1317 wiped = Wipe_EXT4();
1318 else if (New_File_System == "ext2" || New_File_System == "ext3")
1319 wiped = Wipe_EXT23(New_File_System);
1320 else if (New_File_System == "vfat")
1321 wiped = Wipe_FAT();
1322 else if (New_File_System == "exfat")
1323 wiped = Wipe_EXFAT();
1324 else if (New_File_System == "yaffs2")
1325 wiped = Wipe_MTD();
Dees_Troye5017042013-08-29 16:38:55 +00001326 else if (New_File_System == "f2fs")
1327 wiped = Wipe_F2FS();
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001328 else if (New_File_System == "ntfs")
1329 wiped = Wipe_NTFS();
Dees_Troy16c2b312013-01-15 16:51:18 +00001330 else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001331 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 +00001332 unlink("/.layout_version");
1333 return false;
1334 }
1335 update_crypt = wiped;
Gary Pecke8bc5d72012-12-21 06:45:25 -08001336 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001337
Gary Pecke8bc5d72012-12-21 06:45:25 -08001338 if (wiped) {
Dees_Troy1c1ac442013-01-17 21:42:14 +00001339 if (Mount_Point == "/cache")
1340 DataManager::Output_Version();
1341
Dees_Troy16c2b312013-01-15 16:51:18 +00001342 if (TWFunc::Path_Exists("/.layout_version") && Mount(false))
1343 TWFunc::copy_file("/.layout_version", Layout_Filename, 0600);
1344
1345 if (update_crypt) {
1346 Setup_File_System(false);
1347 if (Is_Encrypted && !Is_Decrypted) {
1348 // just wiped an encrypted partition back to its unencrypted state
1349 Is_Encrypted = false;
1350 Is_Decrypted = false;
1351 Decrypted_Block_Device = "";
1352 if (Mount_Point == "/data") {
1353 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1354 DataManager::SetValue(TW_IS_DECRYPTED, 0);
1355 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001356 }
1357 }
Ethan Yonker5eac2222014-06-11 12:22:55 -05001358
Ethan Yonker66a19492015-12-10 10:19:45 -06001359 if (Has_Data_Media && recreate_media) {
Ethan Yonker5eac2222014-06-11 12:22:55 -05001360 Recreate_Media_Folder();
1361 }
Matt Mower209c9632016-01-20 12:08:35 -06001362 if (Is_Storage && Mount(false))
1363 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Gary Pecke8bc5d72012-12-21 06:45:25 -08001364 }
Matt Mower209c9632016-01-20 12:08:35 -06001365
Gary Pecke8bc5d72012-12-21 06:45:25 -08001366 return wiped;
Dees_Troy51a0e822012-09-05 15:24:24 -04001367}
1368
Gary Peck43acadf2012-11-21 21:19:01 -08001369bool TWPartition::Wipe() {
Gary Peck82599a82012-11-21 16:23:12 -08001370 if (Is_File_System(Current_File_System))
1371 return Wipe(Current_File_System);
1372 else
1373 return Wipe(Fstab_File_System);
Gary Peck43acadf2012-11-21 21:19:01 -08001374}
1375
Dees_Troye58d5262012-09-21 12:27:57 -04001376bool TWPartition::Wipe_AndSec(void) {
1377 if (!Has_Android_Secure)
1378 return false;
1379
Dees_Troye58d5262012-09-21 12:27:57 -04001380 if (!Mount(true))
1381 return false;
1382
Ethan Yonker74db1572015-10-28 12:44:49 -05001383 gui_msg(Msg("wiping=Wiping {1}")(Backup_Display_Name));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001384 TWFunc::removeDir(Mount_Point + "/.android_secure/", true);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001385 return true;
Dees_Troye58d5262012-09-21 12:27:57 -04001386}
1387
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001388bool TWPartition::Can_Repair() {
Ethan Yonkera2719152015-05-28 09:44:41 -05001389 if (Mount_Read_Only)
1390 return false;
Matt Mower18794c82015-11-11 16:22:45 -06001391 if (Current_File_System == "vfat" && TWFunc::Path_Exists("/sbin/fsck.fat"))
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001392 return true;
1393 else if ((Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") && TWFunc::Path_Exists("/sbin/e2fsck"))
1394 return true;
1395 else if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/fsck.exfat"))
1396 return true;
1397 else if (Current_File_System == "f2fs" && TWFunc::Path_Exists("/sbin/fsck.f2fs"))
1398 return true;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001399 else if (Current_File_System == "ntfs" && (TWFunc::Path_Exists("/sbin/ntfsfix") || TWFunc::Path_Exists("/sbin/fsck.ntfs")))
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001400 return true;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001401 return false;
1402}
1403
1404bool TWPartition::Repair() {
1405 string command;
1406
1407 if (Current_File_System == "vfat") {
Matt Mower18794c82015-11-11 16:22:45 -06001408 if (!TWFunc::Path_Exists("/sbin/fsck.fat")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001409 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.fat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001410 return false;
1411 }
1412 if (!UnMount(true))
1413 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001414 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.fat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001415 Find_Actual_Block_Device();
Matt Mower18794c82015-11-11 16:22:45 -06001416 command = "/sbin/fsck.fat -y " + Actual_Block_Device;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001417 LOGINFO("Repair command: %s\n", command.c_str());
1418 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001419 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001420 return true;
1421 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001422 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001423 return false;
1424 }
1425 }
1426 if (Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") {
1427 if (!TWFunc::Path_Exists("/sbin/e2fsck")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001428 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("e2fsck"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001429 return false;
1430 }
1431 if (!UnMount(true))
1432 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001433 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("e2fsck"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001434 Find_Actual_Block_Device();
Ethan Yonkera2719152015-05-28 09:44:41 -05001435 command = "/sbin/e2fsck -fp " + Actual_Block_Device;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001436 LOGINFO("Repair command: %s\n", command.c_str());
1437 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001438 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001439 return true;
1440 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001441 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001442 return false;
1443 }
1444 }
1445 if (Current_File_System == "exfat") {
1446 if (!TWFunc::Path_Exists("/sbin/fsck.exfat")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001447 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.exfat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001448 return false;
1449 }
1450 if (!UnMount(true))
1451 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001452 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.exfat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001453 Find_Actual_Block_Device();
1454 command = "/sbin/fsck.exfat " + Actual_Block_Device;
1455 LOGINFO("Repair command: %s\n", command.c_str());
1456 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001457 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001458 return true;
1459 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001460 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001461 return false;
1462 }
1463 }
1464 if (Current_File_System == "f2fs") {
1465 if (!TWFunc::Path_Exists("/sbin/fsck.f2fs")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001466 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.f2fs"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001467 return false;
1468 }
1469 if (!UnMount(true))
1470 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001471 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.f2fs"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001472 Find_Actual_Block_Device();
1473 command = "/sbin/fsck.f2fs " + Actual_Block_Device;
1474 LOGINFO("Repair command: %s\n", command.c_str());
1475 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001476 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001477 return true;
1478 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001479 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001480 return false;
1481 }
1482 }
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001483 if (Current_File_System == "ntfs") {
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001484 string Ntfsfix_Binary;
1485 if (TWFunc::Path_Exists("/sbin/ntfsfix"))
1486 Ntfsfix_Binary = "ntfsfix";
1487 else if (TWFunc::Path_Exists("/sbin/fsck.ntfs"))
1488 Ntfsfix_Binary = "fsck.ntfs";
1489 else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001490 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("ntfsfix"));
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001491 return false;
1492 }
1493 if (!UnMount(true))
1494 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001495 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)(Ntfsfix_Binary));
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001496 Find_Actual_Block_Device();
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001497 command = "/sbin/" + Ntfsfix_Binary + " " + Actual_Block_Device;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001498 LOGINFO("Repair command: %s\n", command.c_str());
1499 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001500 gui_msg("done=Done.");
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001501 return true;
1502 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001503 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001504 return false;
1505 }
1506 }
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001507 return false;
1508}
1509
Ethan Yonkera2719152015-05-28 09:44:41 -05001510bool TWPartition::Can_Resize() {
1511 if (Mount_Read_Only)
1512 return false;
1513 if ((Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") && TWFunc::Path_Exists("/sbin/resize2fs"))
1514 return true;
1515 return false;
1516}
1517
1518bool TWPartition::Resize() {
1519 string command;
1520
1521 if (Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") {
1522 if (!Can_Repair()) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001523 LOGINFO("Cannot resize %s because %s cannot be repaired before resizing.\n", Display_Name.c_str(), Display_Name.c_str());
1524 gui_msg(Msg(msg::kError, "cannot_resize=Cannot resize {1}.")(Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001525 return false;
1526 }
1527 if (!TWFunc::Path_Exists("/sbin/resize2fs")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001528 LOGINFO("resize2fs does not exist! Cannot resize!\n");
1529 gui_msg(Msg(msg::kError, "cannot_resize=Cannot resize {1}.")(Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001530 return false;
1531 }
1532 // Repair will unmount so no need to do it twice
Ethan Yonker74db1572015-10-28 12:44:49 -05001533 gui_msg(Msg("repair_resize=Repairing {1} before resizing.")( Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001534 if (!Repair())
1535 return false;
Ethan Yonker74db1572015-10-28 12:44:49 -05001536 gui_msg(Msg("resizing=Resizing {1} using {2}...")(Display_Name)("resize2fs"));
Ethan Yonkera2719152015-05-28 09:44:41 -05001537 Find_Actual_Block_Device();
1538 command = "/sbin/resize2fs " + Actual_Block_Device;
1539 if (Length != 0) {
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001540 unsigned long long Actual_Size = IOCTL_Get_Block_Size();
1541 if (Actual_Size == 0)
1542 return false;
Ethan Yonkera2719152015-05-28 09:44:41 -05001543
Ethan Yonkera2719152015-05-28 09:44:41 -05001544 unsigned long long Block_Count;
1545 if (Length < 0) {
1546 // Reduce overall size by this length
1547 Block_Count = (Actual_Size / 1024LLU) - ((unsigned long long)(Length * -1) / 1024LLU);
1548 } else {
1549 // This is the size, not a size reduction
1550 Block_Count = ((unsigned long long)(Length) / 1024LLU);
1551 }
1552 char temp[256];
1553 sprintf(temp, "%llu", Block_Count);
1554 command += " ";
1555 command += temp;
1556 command += "K";
1557 }
1558 LOGINFO("Resize command: %s\n", command.c_str());
1559 if (TWFunc::Exec_Cmd(command) == 0) {
1560 Update_Size(true);
Ethan Yonker74db1572015-10-28 12:44:49 -05001561 gui_msg("done=Done.");
Ethan Yonkera2719152015-05-28 09:44:41 -05001562 return true;
1563 } else {
1564 Update_Size(true);
Ethan Yonker74db1572015-10-28 12:44:49 -05001565 gui_msg(Msg(msg::kError, "unable_resize=Unable to resize {1}.")(Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001566 return false;
1567 }
1568 }
1569 return false;
1570}
1571
Ethan Yonker472f5062016-02-25 13:47:30 -06001572bool TWPartition::Backup(const string& backup_folder, pid_t &tar_fork_pid, ProgressTracking *progress) {
bigbiff7abc5fe2015-01-17 16:53:12 -05001573 if (Backup_Method == FILES) {
Ethan Yonker472f5062016-02-25 13:47:30 -06001574 return Backup_Tar(backup_folder, progress, tar_fork_pid);
bigbiff7abc5fe2015-01-17 16:53:12 -05001575 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001576 else if (Backup_Method == DD)
Ethan Yonker472f5062016-02-25 13:47:30 -06001577 return Backup_Image(backup_folder, progress);
Dees_Troy38bd7602012-09-14 13:33:53 -04001578 else if (Backup_Method == FLASH_UTILS)
Ethan Yonker472f5062016-02-25 13:47:30 -06001579 return Backup_Dump_Image(backup_folder, progress);
Dees_Troy2673cec2013-04-02 20:22:16 +00001580 LOGERR("Unknown backup method for '%s'\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001581 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001582}
1583
Dees_Troy43d8b002012-09-17 16:00:01 -04001584bool TWPartition::Check_MD5(string restore_folder) {
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001585 string Full_Filename, md5file;
Dees_Troy43d8b002012-09-17 16:00:01 -04001586 char split_filename[512];
1587 int index = 0;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001588 twrpDigest md5sum;
Dees_Troy43d8b002012-09-17 16:00:01 -04001589
Captain Throwbackff5935f2014-09-23 16:08:34 -04001590 sync();
1591
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001592 memset(split_filename, 0, sizeof(split_filename));
Dees_Troy43d8b002012-09-17 16:00:01 -04001593 Full_Filename = restore_folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04001594 if (!TWFunc::Path_Exists(Full_Filename)) {
1595 // This is a split archive, we presume
1596 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
Dees_Troy2673cec2013-04-02 20:22:16 +00001597 LOGINFO("split_filename: %s\n", split_filename);
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001598 md5file = split_filename;
1599 md5file += ".md5";
1600 if (!TWFunc::Path_Exists(md5file)) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001601 gui_msg(Msg(msg::kError, "no_md5_found=No md5 file found for '{1}'. Please unselect Enable MD5 verification to restore.")(split_filename));
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001602 return false;
1603 }
1604 md5sum.setfn(split_filename);
Dees_Troy83bd4832013-05-04 12:39:56 +00001605 while (index < 1000) {
1606 if (TWFunc::Path_Exists(split_filename) && md5sum.verify_md5digest() != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001607 gui_msg(Msg(msg::kError, "md5_fail_match=MD5 failed to match on '{1}'.")(split_filename));
Dees_Troy43d8b002012-09-17 16:00:01 -04001608 return false;
1609 }
1610 index++;
1611 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001612 md5sum.setfn(split_filename);
Dees_Troy43d8b002012-09-17 16:00:01 -04001613 }
Dees_Troy4a2a1262012-09-18 09:33:47 -04001614 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001615 } else {
1616 // Single file archive
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001617 md5file = Full_Filename + ".md5";
1618 if (!TWFunc::Path_Exists(md5file)) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001619 gui_msg(Msg(msg::kError, "no_md5_found=No md5 file found for '{1}'. Please unselect Enable MD5 verification to restore.")(split_filename));
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001620 return false;
1621 }
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001622 md5sum.setfn(Full_Filename);
1623 if (md5sum.verify_md5digest() != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001624 gui_msg(Msg(msg::kError, "md5_fail_match=MD5 failed to match on '{1}'.")(split_filename));
Dees_Troy43d8b002012-09-17 16:00:01 -04001625 return false;
1626 } else
1627 return true;
1628 }
1629 return false;
1630}
1631
Ethan Yonker472f5062016-02-25 13:47:30 -06001632bool TWPartition::Restore(const string& restore_folder, ProgressTracking *progress) {
Gary Peck43acadf2012-11-21 21:19:01 -08001633 string Restore_File_System;
1634
Matt Mower3c366972015-12-25 19:28:31 -06001635 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, gui_parse_text("{@restoring_hdr}"));
Dees_Troy2673cec2013-04-02 20:22:16 +00001636 LOGINFO("Restore filename is: %s\n", Backup_FileName.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001637
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001638 Restore_File_System = Get_Restore_File_System(restore_folder);
1639
1640 if (Is_File_System(Restore_File_System))
Ethan Yonker472f5062016-02-25 13:47:30 -06001641 return Restore_Tar(restore_folder, Restore_File_System, progress);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001642 else if (Is_Image(Restore_File_System)) {
Ethan Yonker472f5062016-02-25 13:47:30 -06001643 return Restore_Image(restore_folder, Restore_File_System, progress);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001644 }
1645
1646 LOGERR("Unknown restore method for '%s'\n", Mount_Point.c_str());
1647 return false;
1648}
1649
Ethan Yonker472f5062016-02-25 13:47:30 -06001650string TWPartition::Get_Restore_File_System(const string& restore_folder) {
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001651 size_t first_period, second_period;
1652 string Restore_File_System;
1653
Gary Peck43acadf2012-11-21 21:19:01 -08001654 // Parse backup filename to extract the file system before wiping
1655 first_period = Backup_FileName.find(".");
1656 if (first_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001657 LOGERR("Unable to find file system (first period).\n");
thatd43bf2d2014-09-21 23:13:02 +02001658 return string();
Gary Peck43acadf2012-11-21 21:19:01 -08001659 }
1660 Restore_File_System = Backup_FileName.substr(first_period + 1, Backup_FileName.size() - first_period - 1);
1661 second_period = Restore_File_System.find(".");
1662 if (second_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001663 LOGERR("Unable to find file system (second period).\n");
thatd43bf2d2014-09-21 23:13:02 +02001664 return string();
Gary Peck43acadf2012-11-21 21:19:01 -08001665 }
1666 Restore_File_System.resize(second_period);
Dees_Troy2673cec2013-04-02 20:22:16 +00001667 LOGINFO("Restore file system is: '%s'.\n", Restore_File_System.c_str());
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001668 return Restore_File_System;
Dees_Troy51a0e822012-09-05 15:24:24 -04001669}
1670
1671string TWPartition::Backup_Method_By_Name() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001672 if (Backup_Method == NONE)
1673 return "none";
1674 else if (Backup_Method == FILES)
1675 return "files";
1676 else if (Backup_Method == DD)
1677 return "dd";
1678 else if (Backup_Method == FLASH_UTILS)
1679 return "flash_utils";
1680 else
1681 return "undefined";
1682 return "ERROR!";
Dees_Troy51a0e822012-09-05 15:24:24 -04001683}
1684
1685bool TWPartition::Decrypt(string Password) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001686 LOGINFO("STUB TWPartition::Decrypt, password: '%s'\n", Password.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001687 // Is this needed?
Dees_Troy51a0e822012-09-05 15:24:24 -04001688 return 1;
1689}
1690
1691bool TWPartition::Wipe_Encryption() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001692 bool Save_Data_Media = Has_Data_Media;
1693
1694 if (!UnMount(true))
1695 return false;
1696
Dees_Troy38bd7602012-09-14 13:33:53 -04001697 Has_Data_Media = false;
Dees_Troy74fb2e92013-04-15 14:35:47 +00001698 Decrypted_Block_Device = "";
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001699#ifdef TW_INCLUDE_CRYPTO
1700 if (Is_Decrypted) {
1701 if (!UnMount(true))
1702 return false;
Matt Mower2b18a532015-02-20 16:58:05 -06001703 if (delete_crypto_blk_dev((char*)("userdata")) != 0) {
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001704 LOGERR("Error deleting crypto block device, continuing anyway.\n");
1705 }
1706 }
1707#endif
Dees_Troy74fb2e92013-04-15 14:35:47 +00001708 Is_Decrypted = false;
1709 Is_Encrypted = false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001710 Find_Actual_Block_Device();
Ethan Yonkerbc85b632015-08-09 12:48:14 -05001711 if (Crypto_Key_Location == "footer") {
1712 int newlen, fd;
1713 if (Length != 0) {
1714 newlen = Length;
1715 if (newlen < 0)
1716 newlen = newlen * -1;
1717 } else {
1718 newlen = CRYPT_FOOTER_OFFSET;
1719 }
1720 if ((fd = open(Actual_Block_Device.c_str(), O_RDWR)) < 0) {
1721 gui_print_color(