blob: bdc8dcb59cf6877f6027d304b46fd41af6a66a55 [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
Matt Mower4ab42b12016-04-21 13:52:18 -050085const struct flag_list mount_flags[] = {
Hashcode62bd9e02013-11-19 21:59:42 -080086 { "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
Matt Mower4ab42b12016-04-21 13:52:18 -0500515void TWPartition::Process_FS_Flags(const char *str) {
516 char *options = strdup(str);
517 char *ptr, *savep;
Hashcode62bd9e02013-11-19 21:59:42 -0800518
Matt Mower4ab42b12016-04-21 13:52:18 -0500519 Mount_Options = "";
Hashcode62bd9e02013-11-19 21:59:42 -0800520
Matt Mower4ab42b12016-04-21 13:52:18 -0500521 // Avoid issues with potentially nested strtok by using strtok_r
522 ptr = strtok_r(options, ",", &savep);
523 while (ptr) {
524 const struct flag_list* mount_flag = mount_flags;
525
526 for (; mount_flag->name; mount_flag++) {
527 // mount_flags are never postfixed by '=',
528 // so only match identical strings (including length)
529 if (strcmp(ptr, mount_flag->name) == 0) {
530 Mount_Flags |= mount_flag->flag;
Hashcode62bd9e02013-11-19 21:59:42 -0800531 break;
532 }
533 }
534
Matt Mower4ab42b12016-04-21 13:52:18 -0500535 if (mount_flag->flag == MS_RDONLY)
536 Mount_Read_Only = true;
Hashcode62bd9e02013-11-19 21:59:42 -0800537
Matt Mower4ab42b12016-04-21 13:52:18 -0500538 if (mount_flag->name != 0) {
539 if (!Mount_Options.empty())
540 Mount_Options += ",";
541 Mount_Options += mount_flag->name;
542 } else {
543 LOGINFO("Unhandled mount flag: '%s'\n", ptr);
544 }
545
546 ptr = strtok_r(NULL, ",", &savep);
547 }
548 free(options);
Hashcode62bd9e02013-11-19 21:59:42 -0800549}
550
Matt Mower2416a502016-04-12 19:54:46 -0500551void TWPartition::Apply_TW_Flag(const unsigned flag, const char* str, const bool val) {
552 switch (flag) {
553 case TWFLAG_ANDSEC:
554 Has_Android_Secure = val;
555 break;
556 case TWFLAG_BACKUP:
557 Can_Be_Backed_Up = val;
558 break;
559 case TWFLAG_BACKUPNAME:
560 Backup_Display_Name = str;
561 break;
562 case TWFLAG_BLOCKSIZE:
563 Format_Block_Size = (unsigned long)(atol(str));
564 break;
565 case TWFLAG_CANBEWIPED:
566 Can_Be_Wiped = val;
567 break;
568 case TWFLAG_CANENCRYPTBACKUP:
569 Can_Encrypt_Backup = val;
570 break;
571 case TWFLAG_DEFAULTS:
572 // Do nothing
573 break;
574 case TWFLAG_DISPLAY:
575 Display_Name = str;
576 break;
577 case TWFLAG_ENCRYPTABLE:
578 case TWFLAG_FORCEENCRYPT:
579 Crypto_Key_Location = str;
580 break;
581 case TWFLAG_FLASHIMG:
582 Can_Flash_Img = val;
583 break;
584 case TWFLAG_FSFLAGS:
Matt Mower4ab42b12016-04-21 13:52:18 -0500585 Process_FS_Flags(str);
Matt Mower2416a502016-04-12 19:54:46 -0500586 break;
587 case TWFLAG_IGNOREBLKID:
588 Ignore_Blkid = val;
589 break;
590 case TWFLAG_LENGTH:
591 Length = atoi(str);
592 break;
593 case TWFLAG_MOUNTTODECRYPT:
594 Mount_To_Decrypt = val;
595 break;
596 case TWFLAG_REMOVABLE:
597 Removable = val;
598 break;
599 case TWFLAG_RETAINLAYOUTVERSION:
600 Retain_Layout_Version = val;
601 break;
602 case TWFLAG_SETTINGSSTORAGE:
603 Is_Settings_Storage = val;
604 if (Is_Settings_Storage)
605 Is_Storage = true;
606 break;
607 case TWFLAG_STORAGE:
608 Is_Storage = val;
609 break;
610 case TWFLAG_STORAGENAME:
611 Storage_Name = str;
612 break;
613 case TWFLAG_SUBPARTITIONOF:
614 Is_SubPartition = true;
615 SubPartition_Of = str;
616 break;
617 case TWFLAG_SYMLINK:
618 Symlink_Path = str;
619 break;
620 case TWFLAG_USERDATAENCRYPTBACKUP:
621 Use_Userdata_Encryption = val;
622 if (Use_Userdata_Encryption)
623 Can_Encrypt_Backup = true;
624 break;
625 case TWFLAG_USERMRF:
626 Use_Rm_Rf = val;
627 break;
628 case TWFLAG_WIPEDURINGFACTORYRESET:
629 Wipe_During_Factory_Reset = val;
630 if (Wipe_During_Factory_Reset) {
631 Can_Be_Wiped = true;
632 Wipe_Available_in_GUI = true;
633 }
634 break;
635 case TWFLAG_WIPEINGUI:
636 Wipe_Available_in_GUI = val;
637 if (Wipe_Available_in_GUI)
638 Can_Be_Wiped = true;
639 break;
640 default:
641 // Should not get here
642 LOGINFO("Flag identified for processing, but later unmatched: %i\n", flag);
643 break;
644 }
645}
Dees_Troy51127312012-09-08 13:08:49 -0400646
Matt Mower2416a502016-04-12 19:54:46 -0500647void TWPartition::Process_TW_Flags(char *flags, bool Display_Error) {
648 char separator[2] = {'\n', 0};
649 char *ptr, *savep;
650
651 // Semicolons within double-quotes are not forbidden, so replace
652 // only the semicolons intended as separators with '\n' for strtok
653 for (unsigned i = 0, skip = 0; i < strlen(flags); i++) {
654 if (flags[i] == '\"')
Dees_Troya13d74f2013-03-24 08:54:55 -0500655 skip = !skip;
Matt Mower2416a502016-04-12 19:54:46 -0500656 if (!skip && flags[i] == ';')
657 flags[i] = separator[0];
Dees_Troy51127312012-09-08 13:08:49 -0400658 }
659
Matt Mower2416a502016-04-12 19:54:46 -0500660 // Avoid issues with potentially nested strtok by using strtok_r
661 ptr = strtok_r(flags, separator, &savep);
662 while (ptr) {
663 int ptr_len = strlen(ptr);
664 const struct flag_list* tw_flag = tw_flags;
Hashcode62bd9e02013-11-19 21:59:42 -0800665
Matt Mower2416a502016-04-12 19:54:46 -0500666 for (; tw_flag->name; tw_flag++) {
667 int flag_len = strlen(tw_flag->name);
668
669 if (strncmp(ptr, tw_flag->name, flag_len) == 0) {
670 bool flag_val = false;
671
672 if (ptr_len > flag_len && (tw_flag->name)[flag_len-1] != '='
673 && ptr[flag_len] != '=') {
674 // Handle flags with same starting string
675 // (e.g. backup and backupname)
676 continue;
677 } else if (ptr_len > flag_len && ptr[flag_len] == '=') {
678 // Handle flags with dual format: Part 1
679 // (e.g. backup and backup=y. backup=y handled here)
680 ptr += flag_len + 1;
681 TWFunc::Strip_Quotes(ptr);
682 // Skip flags with empty argument
683 // (e.g. backup=)
684 if (strlen(ptr) == 0) {
685 LOGINFO("Flag missing argument or should not include '=': %s=\n", tw_flag->name);
686 break;
687 }
688 flag_val = strchr("yY1", *ptr) != NULL;
689 } else if (ptr_len == flag_len
690 && (tw_flag->name)[flag_len-1] == '=') {
691 // Skip flags missing argument after =
692 // (e.g. backupname=)
693 LOGINFO("Flag missing argument: %s\n", tw_flag->name);
694 break;
695 } else if (ptr_len > flag_len
696 && (tw_flag->name)[flag_len-1] == '=') {
697 // Handle arguments to flags
698 // (e.g. backupname="My Stuff")
699 ptr += flag_len;
700 TWFunc::Strip_Quotes(ptr);
701 // Skip flags with empty argument
702 // (e.g. backupname="")
703 if (strlen(ptr) == 0) {
704 LOGINFO("Flag missing argument: %s\n", tw_flag->name);
705 break;
706 }
707 } else if (ptr_len == flag_len) {
708 // Handle flags with dual format: Part 2
709 // (e.g. backup and backup=y. backup handled here)
710 flag_val = true;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600711 } else {
Matt Mower2416a502016-04-12 19:54:46 -0500712 LOGINFO("Flag matched, but could not be processed: %s\n", ptr);
713 break;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600714 }
Matt Mower2416a502016-04-12 19:54:46 -0500715
716 Apply_TW_Flag(tw_flag->flag, ptr, flag_val);
717 break;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600718 }
Matt Mower2416a502016-04-12 19:54:46 -0500719 }
720 if (tw_flag->name == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400721 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000722 LOGERR("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400723 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000724 LOGINFO("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400725 }
Matt Mower2416a502016-04-12 19:54:46 -0500726 ptr = strtok_r(NULL, separator, &savep);
Dees_Troy51127312012-09-08 13:08:49 -0400727 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400728}
729
Dees_Troy5bf43922012-09-07 16:07:55 -0400730bool TWPartition::Is_File_System(string File_System) {
731 if (File_System == "ext2" ||
Dees_Troy63c8df72012-09-10 14:02:05 -0400732 File_System == "ext3" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400733 File_System == "ext4" ||
734 File_System == "vfat" ||
735 File_System == "ntfs" ||
736 File_System == "yaffs2" ||
bigbiff bigbiff3e146522012-11-14 14:32:59 -0500737 File_System == "exfat" ||
Dees_Troye5017042013-08-29 16:38:55 +0000738 File_System == "f2fs" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400739 File_System == "auto")
740 return true;
741 else
742 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400743}
744
Dees_Troy5bf43922012-09-07 16:07:55 -0400745bool TWPartition::Is_Image(string File_System) {
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400746 if (File_System == "emmc" || File_System == "mtd" || File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -0400747 return true;
748 else
749 return false;
750}
751
Dees_Troy51127312012-09-08 13:08:49 -0400752bool TWPartition::Make_Dir(string Path, bool Display_Error) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400753 if (!TWFunc::Path_Exists(Path)) {
Dees_Troy51127312012-09-08 13:08:49 -0400754 if (mkdir(Path.c_str(), 0777) == -1) {
755 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -0500756 gui_msg(Msg(msg::kError, "create_folder_strerr=Can not create '{1}' folder ({2}).")(Path)(strerror(errno)));
Dees_Troy51127312012-09-08 13:08:49 -0400757 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000758 LOGINFO("Can not create '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400759 return false;
760 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000761 LOGINFO("Created '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400762 return true;
763 }
764 }
765 return true;
766}
767
Dees_Troy5bf43922012-09-07 16:07:55 -0400768void TWPartition::Setup_File_System(bool Display_Error) {
769 struct statfs st;
770
771 Can_Be_Mounted = true;
772 Can_Be_Wiped = true;
773
Dees_Troy5bf43922012-09-07 16:07:55 -0400774 // Make the mount point folder if it doesn't exist
Dees_Troy51127312012-09-08 13:08:49 -0400775 Make_Dir(Mount_Point, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400776 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
777 Backup_Name = Display_Name;
778 Backup_Method = FILES;
779}
780
781void TWPartition::Setup_Image(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400782 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
783 Backup_Name = Display_Name;
Gary Peck82599a82012-11-21 16:23:12 -0800784 if (Current_File_System == "emmc")
Dees_Troy5bf43922012-09-07 16:07:55 -0400785 Backup_Method = DD;
Gary Peck82599a82012-11-21 16:23:12 -0800786 else if (Current_File_System == "mtd" || Current_File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -0400787 Backup_Method = FLASH_UTILS;
788 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000789 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 -0400790 if (Find_Partition_Size()) {
791 Used = Size;
792 Backup_Size = Size;
Dees_Troy51a0e822012-09-05 15:24:24 -0400793 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400794 if (Display_Error)
Dees Troyd932ce12013-10-18 17:12:59 +0000795 LOGERR("Unable to find partition size for '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400796 else
Dees Troyd932ce12013-10-18 17:12:59 +0000797 LOGINFO("Unable to find partition size for '%s'\n", Mount_Point.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400798 }
799}
800
Dees_Troye58d5262012-09-21 12:27:57 -0400801void TWPartition::Setup_AndSec(void) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500802 Backup_Display_Name = "Android Secure";
Dees_Troye58d5262012-09-21 12:27:57 -0400803 Backup_Name = "and-sec";
Dees_Troya13d74f2013-03-24 08:54:55 -0500804 Can_Be_Backed_Up = true;
Dees_Troye58d5262012-09-21 12:27:57 -0400805 Has_Android_Secure = true;
806 Symlink_Path = Mount_Point + "/.android_secure";
807 Symlink_Mount_Point = "/and-sec";
808 Backup_Path = Symlink_Mount_Point;
809 Make_Dir("/and-sec", true);
810 Recreate_AndSec_Folder();
Ethan Yonkerd4d10732014-02-03 15:27:52 -0600811 Mount_Storage_Retry();
Dees_Troye58d5262012-09-21 12:27:57 -0400812}
813
that9e0593e2014-10-08 00:01:24 +0200814void TWPartition::Setup_Data_Media() {
Ethan Yonker6277c792014-09-15 14:54:30 -0500815 LOGINFO("Setting up '%s' as data/media emulated storage.\n", Mount_Point.c_str());
Ethan Yonker66a19492015-12-10 10:19:45 -0600816 if (Storage_Name.empty() || Storage_Name == "Data")
817 Storage_Name = "Internal Storage";
Ethan Yonker6277c792014-09-15 14:54:30 -0500818 Has_Data_Media = true;
819 Is_Storage = true;
Ethan Yonker66a19492015-12-10 10:19:45 -0600820 Storage_Path = Mount_Point + "/media";
Ethan Yonker6277c792014-09-15 14:54:30 -0500821 Symlink_Path = Storage_Path;
Ethan Yonker66a19492015-12-10 10:19:45 -0600822 if (Mount_Point == "/data") {
823 Is_Settings_Storage = true;
824 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
825 Make_Dir("/emmc", false);
826 Symlink_Mount_Point = "/emmc";
827 } else {
828 Make_Dir("/sdcard", false);
829 Symlink_Mount_Point = "/sdcard";
830 }
831 if (Mount(false) && TWFunc::Path_Exists(Mount_Point + "/media/0")) {
832 Storage_Path = Mount_Point + "/media/0";
833 Symlink_Path = Storage_Path;
834 DataManager::SetValue(TW_INTERNAL_PATH, Mount_Point + "/media/0");
835 UnMount(true);
836 }
837 DataManager::SetValue("tw_has_internal", 1);
838 DataManager::SetValue("tw_has_data_media", 1);
839 du.add_absolute_dir(Mount_Point + "/misc/vold");
840 du.add_absolute_dir(Mount_Point + "/.layout_version");
841 du.add_absolute_dir(Mount_Point + "/system/storage.xml");
Ethan Yonker6277c792014-09-15 14:54:30 -0500842 } else {
Ethan Yonker66a19492015-12-10 10:19:45 -0600843 if (Mount(true) && TWFunc::Path_Exists(Mount_Point + "/media/0")) {
844 Storage_Path = Mount_Point + "/media/0";
845 Symlink_Path = Storage_Path;
846 UnMount(true);
847 }
Ethan Yonker6277c792014-09-15 14:54:30 -0500848 }
Ethan Yonker66a19492015-12-10 10:19:45 -0600849 du.add_absolute_dir(Mount_Point + "/media");
Ethan Yonker6277c792014-09-15 14:54:30 -0500850}
851
Dees_Troy5bf43922012-09-07 16:07:55 -0400852void TWPartition::Find_Real_Block_Device(string& Block, bool Display_Error) {
853 char device[512], realDevice[512];
854
855 strcpy(device, Block.c_str());
856 memset(realDevice, 0, sizeof(realDevice));
857 while (readlink(device, realDevice, sizeof(realDevice)) > 0)
858 {
859 strcpy(device, realDevice);
860 memset(realDevice, 0, sizeof(realDevice));
861 }
862
863 if (device[0] != '/') {
864 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000865 LOGERR("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400866 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000867 LOGINFO("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400868 return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400869 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400870 Block = device;
871 return;
872 }
873}
874
Dees_Troy8e337f32012-10-13 22:07:49 -0400875void TWPartition::Mount_Storage_Retry(void) {
876 // On some devices, storage doesn't want to mount right away, retry and sleep
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500877 if (!Mount(true)) {
Dees_Troy8e337f32012-10-13 22:07:49 -0400878 int retry_count = 5;
879 while (retry_count > 0 && !Mount(false)) {
880 usleep(500000);
881 retry_count--;
882 }
883 Mount(true);
884 }
885}
886
Dees_Troy38bd7602012-09-14 13:33:53 -0400887bool TWPartition::Find_MTD_Block_Device(string MTD_Name) {
888 FILE *fp = NULL;
889 char line[255];
890
891 fp = fopen("/proc/mtd", "rt");
892 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000893 LOGERR("Device does not support /proc/mtd\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400894 return false;
895 }
896
897 while (fgets(line, sizeof(line), fp) != NULL)
898 {
899 char device[32], label[32];
900 unsigned long size = 0;
901 char* fstype = NULL;
902 int deviceId;
903
904 sscanf(line, "%s %lx %*s %*c%s", device, &size, label);
905
906 // Skip header and blank lines
907 if ((strcmp(device, "dev:") == 0) || (strlen(line) < 8))
908 continue;
909
910 // Strip off the trailing " from the label
911 label[strlen(label)-1] = '\0';
912
913 if (strcmp(label, MTD_Name.c_str()) == 0) {
914 // We found our device
915 // Strip off the trailing : from the device
916 device[strlen(device)-1] = '\0';
917 if (sscanf(device,"mtd%d", &deviceId) == 1) {
918 sprintf(device, "/dev/block/mtdblock%d", deviceId);
919 Primary_Block_Device = device;
Dees_Troy76543db2013-06-19 16:24:30 +0000920 fclose(fp);
921 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -0400922 }
923 }
924 }
925 fclose(fp);
926
927 return false;
928}
929
Dees_Troy51127312012-09-08 13:08:49 -0400930bool TWPartition::Get_Size_Via_statfs(bool Display_Error) {
931 struct statfs st;
932 string Local_Path = Mount_Point + "/.";
933
934 if (!Mount(Display_Error))
935 return false;
936
937 if (statfs(Local_Path.c_str(), &st) != 0) {
938 if (!Removable) {
939 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000940 LOGERR("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400941 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000942 LOGINFO("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400943 }
944 return false;
945 }
946 Size = (st.f_blocks * st.f_bsize);
947 Used = ((st.f_blocks - st.f_bfree) * st.f_bsize);
948 Free = (st.f_bfree * st.f_bsize);
949 Backup_Size = Used;
950 return true;
951}
952
953bool TWPartition::Get_Size_Via_df(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400954 FILE* fp;
955 char command[255], line[512];
956 int include_block = 1;
957 unsigned int min_len;
958
959 if (!Mount(Display_Error))
960 return false;
961
Dees_Troy38bd7602012-09-14 13:33:53 -0400962 min_len = Actual_Block_Device.size() + 2;
Dees_Troy51127312012-09-08 13:08:49 -0400963 sprintf(command, "df %s > /tmp/dfoutput.txt", Mount_Point.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +0200964 TWFunc::Exec_Cmd(command);
Dees_Troy51127312012-09-08 13:08:49 -0400965 fp = fopen("/tmp/dfoutput.txt", "rt");
966 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000967 LOGINFO("Unable to open /tmp/dfoutput.txt.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400968 return false;
Dees_Troy51127312012-09-08 13:08:49 -0400969 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400970
971 while (fgets(line, sizeof(line), fp) != NULL)
972 {
973 unsigned long blocks, used, available;
974 char device[64];
975 char tmpString[64];
976
977 if (strncmp(line, "Filesystem", 10) == 0)
978 continue;
979 if (strlen(line) < min_len) {
980 include_block = 0;
981 continue;
982 }
983 if (include_block) {
984 sscanf(line, "%s %lu %lu %lu", device, &blocks, &used, &available);
985 } else {
986 // The device block string is so long that the df information is on the next line
987 int space_count = 0;
Dees_Troye58d5262012-09-21 12:27:57 -0400988 sprintf(tmpString, "/dev/block/%s", Actual_Block_Device.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400989 while (tmpString[space_count] == 32)
990 space_count++;
991 sscanf(line + space_count, "%lu %lu %lu", &blocks, &used, &available);
992 }
993
994 // Adjust block size to byte size
995 Size = blocks * 1024ULL;
996 Used = used * 1024ULL;
997 Free = available * 1024ULL;
998 Backup_Size = Used;
999 }
1000 fclose(fp);
1001 return true;
1002}
1003
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001004unsigned long long TWPartition::IOCTL_Get_Block_Size() {
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001005 Find_Actual_Block_Device();
Ethan Yonker483e9f42016-01-11 22:21:18 -06001006
1007 return TWFunc::IOCTL_Get_Block_Size(Actual_Block_Device.c_str());
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001008}
1009
Dees_Troy5bf43922012-09-07 16:07:55 -04001010bool TWPartition::Find_Partition_Size(void) {
1011 FILE* fp;
1012 char line[512];
1013 string tmpdevice;
1014
igoriok87e3d932013-01-31 21:03:53 +02001015 fp = fopen("/proc/dumchar_info", "rt");
1016 if (fp != NULL) {
1017 while (fgets(line, sizeof(line), fp) != NULL)
1018 {
1019 char label[32], device[32];
1020 unsigned long size = 0;
1021
thatd43bf2d2014-09-21 23:13:02 +02001022 sscanf(line, "%s %lx %*x %*u %s", label, &size, device);
igoriok87e3d932013-01-31 21:03:53 +02001023
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05001024 // Skip header, annotation and blank lines
igoriok87e3d932013-01-31 21:03:53 +02001025 if ((strncmp(device, "/dev/", 5) != 0) || (strlen(line) < 8))
1026 continue;
1027
1028 tmpdevice = "/dev/";
1029 tmpdevice += label;
1030 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
1031 Size = size;
1032 fclose(fp);
1033 return true;
1034 }
1035 }
1036 }
1037
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001038 unsigned long long ioctl_size = IOCTL_Get_Block_Size();
1039 if (ioctl_size) {
1040 Size = ioctl_size;
1041 return true;
1042 }
1043
Dees_Troy5bf43922012-09-07 16:07:55 -04001044 // In this case, we'll first get the partitions we care about (with labels)
1045 fp = fopen("/proc/partitions", "rt");
1046 if (fp == NULL)
1047 return false;
1048
1049 while (fgets(line, sizeof(line), fp) != NULL)
1050 {
1051 unsigned long major, minor, blocks;
1052 char device[512];
1053 char tmpString[64];
1054
Dees_Troy63c8df72012-09-10 14:02:05 -04001055 if (strlen(line) < 7 || line[0] == 'm') continue;
Dees_Troy5bf43922012-09-07 16:07:55 -04001056 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
1057
1058 tmpdevice = "/dev/block/";
1059 tmpdevice += device;
Dees_Troy38bd7602012-09-14 13:33:53 -04001060 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001061 // Adjust block size to byte size
1062 Size = blocks * 1024ULL;
1063 fclose(fp);
1064 return true;
1065 }
1066 }
1067 fclose(fp);
1068 return false;
1069}
1070
Dees_Troy5bf43922012-09-07 16:07:55 -04001071bool TWPartition::Is_Mounted(void) {
1072 if (!Can_Be_Mounted)
1073 return false;
1074
1075 struct stat st1, st2;
1076 string test_path;
1077
1078 // Check to see if the mount point directory exists
1079 test_path = Mount_Point + "/.";
1080 if (stat(test_path.c_str(), &st1) != 0) return false;
1081
1082 // Check to see if the directory above the mount point exists
1083 test_path = Mount_Point + "/../.";
1084 if (stat(test_path.c_str(), &st2) != 0) return false;
1085
1086 // Compare the device IDs -- if they match then we're (probably) using tmpfs instead of an actual device
1087 int ret = (st1.st_dev != st2.st_dev) ? true : false;
1088
1089 return ret;
1090}
1091
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05001092bool TWPartition::Is_File_System_Writable(void) {
1093 if (!Is_File_System(Current_File_System) || !Is_Mounted())
1094 return false;
1095
1096 string test_path = Mount_Point + "/.";
1097 return (access(test_path.c_str(), W_OK) == 0);
1098}
1099
Dees_Troy5bf43922012-09-07 16:07:55 -04001100bool TWPartition::Mount(bool Display_Error) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001101 int exfat_mounted = 0;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001102 unsigned long flags = Mount_Flags;
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001103
Dees_Troy5bf43922012-09-07 16:07:55 -04001104 if (Is_Mounted()) {
1105 return true;
1106 } else if (!Can_Be_Mounted) {
1107 return false;
1108 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001109
1110 Find_Actual_Block_Device();
1111
1112 // Check the current file system before mounting
1113 Check_FS_Type();
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001114 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/exfat-fuse")) {
Dees_Troye34c1332013-02-06 19:13:00 +00001115 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 +00001116 LOGINFO("cmd: %s\n", cmd.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001117 string result;
1118 if (TWFunc::Exec_Cmd(cmd, result) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001119 LOGINFO("exfat-fuse failed to mount with result '%s', trying vfat\n", result.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001120 Current_File_System = "vfat";
1121 } else {
1122#ifdef TW_NO_EXFAT_FUSE
1123 UnMount(false);
1124 // We'll let the kernel handle it but using exfat-fuse to detect if the file system is actually exfat
1125 // Some kernels let us mount vfat as exfat which doesn't work out too well
1126#else
1127 exfat_mounted = 1;
1128#endif
1129 }
1130 }
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001131
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001132 if (Current_File_System == "ntfs" && (TWFunc::Path_Exists("/sbin/ntfs-3g") || TWFunc::Path_Exists("/sbin/mount.ntfs"))) {
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001133 string cmd;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001134 string Ntfsmount_Binary = "";
1135
1136 if (TWFunc::Path_Exists("/sbin/ntfs-3g"))
1137 Ntfsmount_Binary = "ntfs-3g";
1138 else if (TWFunc::Path_Exists("/sbin/mount.ntfs"))
1139 Ntfsmount_Binary = "mount.ntfs";
1140
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001141 if (Mount_Read_Only)
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001142 cmd = "/sbin/" + Ntfsmount_Binary + " -o ro " + Actual_Block_Device + " " + Mount_Point;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001143 else
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001144 cmd = "/sbin/" + Ntfsmount_Binary + " " + Actual_Block_Device + " " + Mount_Point;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001145 LOGINFO("cmd: '%s'\n", cmd.c_str());
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001146
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001147 if (TWFunc::Exec_Cmd(cmd) == 0) {
1148 return true;
1149 } else {
1150 LOGINFO("ntfs-3g failed to mount, trying regular mount method.\n");
1151 }
1152 }
1153
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001154 if (Mount_Read_Only)
1155 flags |= MS_RDONLY;
1156
Dees_Troy22042032012-12-18 21:23:08 +00001157 if (Fstab_File_System == "yaffs2") {
1158 // mount an MTD partition as a YAFFS2 filesystem.
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001159 flags = MS_NOATIME | MS_NODEV | MS_NODIRATIME;
1160 if (Mount_Read_Only)
1161 flags |= MS_RDONLY;
Dees_Troy76543db2013-06-19 16:24:30 +00001162 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags, NULL) < 0) {
1163 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags | MS_RDONLY, NULL) < 0) {
1164 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001165 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy76543db2013-06-19 16:24:30 +00001166 else
1167 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
1168 return false;
1169 } else {
1170 LOGINFO("Mounted '%s' (MTD) as RO\n", Mount_Point.c_str());
1171 return true;
1172 }
1173 } else {
1174 struct stat st;
1175 string test_path = Mount_Point;
1176 if (stat(test_path.c_str(), &st) < 0) {
1177 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001178 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy76543db2013-06-19 16:24:30 +00001179 else
1180 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
1181 return false;
1182 }
1183 mode_t new_mode = st.st_mode | S_IXUSR | S_IXGRP | S_IXOTH;
1184 if (new_mode != st.st_mode) {
1185 LOGINFO("Fixing execute permissions for %s\n", Mount_Point.c_str());
1186 if (chmod(Mount_Point.c_str(), new_mode) < 0) {
1187 if (Display_Error)
1188 LOGERR("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
1189 else
1190 LOGINFO("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
1191 return false;
1192 }
1193 }
Dees_Troy22042032012-12-18 21:23:08 +00001194 return true;
Dees_Troy76543db2013-06-19 16:24:30 +00001195 }
thatc7572eb2015-03-31 18:55:30 +02001196 }
1197
1198 string mount_fs = Current_File_System;
1199 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sys/module/texfat"))
1200 mount_fs = "texfat";
1201
1202 if (!exfat_mounted &&
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001203 mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), mount_fs.c_str(), flags, Mount_Options.c_str()) != 0 &&
1204 mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), mount_fs.c_str(), flags, NULL) != 0) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001205#ifdef TW_NO_EXFAT_FUSE
1206 if (Current_File_System == "exfat") {
Dees_Troy2673cec2013-04-02 20:22:16 +00001207 LOGINFO("Mounting exfat failed, trying vfat...\n");
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001208 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), "vfat", 0, NULL) != 0) {
Dees_Troy85f44ed2013-01-09 18:42:36 +00001209 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001210 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy85f44ed2013-01-09 18:42:36 +00001211 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001212 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001213 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 +00001214 return false;
Dees_Troy85f44ed2013-01-09 18:42:36 +00001215 }
Dees_Troyb05ddee2013-01-28 20:24:50 +00001216 } else {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001217#endif
bigbiff bigbiff26774a02014-03-29 18:22:00 -04001218 if (!Removable && Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001219 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001220 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001221 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
1222 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 +00001223 return false;
1224#ifdef TW_NO_EXFAT_FUSE
Dees_Troy85f44ed2013-01-09 18:42:36 +00001225 }
1226#endif
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001227 }
Ethan Yonker253368a2014-11-25 15:00:52 -06001228
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001229 if (Removable)
1230 Update_Size(Display_Error);
1231
xiaolu9416f4f2015-06-04 08:22:23 +08001232 if (!Symlink_Mount_Point.empty() && TWFunc::Path_Exists(Symlink_Path)) {
codelover2a3d4ce2015-03-14 20:26:49 +08001233 string Command = "mount -o bind '" + Symlink_Path + "' '" + Symlink_Mount_Point + "'";
Vojtech Bocek05534202013-09-11 08:11:56 +02001234 TWFunc::Exec_Cmd(Command);
Dees_Troy5bf43922012-09-07 16:07:55 -04001235 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001236 return true;
1237}
1238
1239bool TWPartition::UnMount(bool Display_Error) {
1240 if (Is_Mounted()) {
1241 int never_unmount_system;
1242
1243 DataManager::GetValue(TW_DONT_UNMOUNT_SYSTEM, never_unmount_system);
1244 if (never_unmount_system == 1 && Mount_Point == "/system")
1245 return true; // Never unmount system if you're not supposed to unmount it
1246
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001247 if (Is_Storage)
Ethan Yonker726a0202014-12-16 20:01:38 -06001248 PartitionManager.Remove_MTP_Storage(MTP_Storage_ID);
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001249
Dees_Troy38bd7602012-09-14 13:33:53 -04001250 if (!Symlink_Mount_Point.empty())
1251 umount(Symlink_Mount_Point.c_str());
1252
Dees_Troyb05ddee2013-01-28 20:24:50 +00001253 umount(Mount_Point.c_str());
1254 if (Is_Mounted()) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001255 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001256 gui_msg(Msg(msg::kError, "fail_unmount=Failed to unmount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy5bf43922012-09-07 16:07:55 -04001257 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001258 LOGINFO("Unable to unmount '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001259 return false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001260 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -04001261 return true;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001262 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001263 } else {
1264 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001265 }
1266}
1267
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05001268bool TWPartition::ReMount(bool Display_Error) {
1269 if (UnMount(Display_Error))
1270 return Mount(Display_Error);
1271 return false;
1272}
1273
1274bool TWPartition::ReMount_RW(bool Display_Error) {
1275 // No need to remount if already mounted rw
1276 if (Is_File_System_Writable())
1277 return true;
1278
1279 bool ro = Mount_Read_Only;
1280 int flags = Mount_Flags;
1281
1282 Mount_Read_Only = false;
1283 Mount_Flags &= ~MS_RDONLY;
1284
1285 bool ret = ReMount(Display_Error);
1286
1287 Mount_Read_Only = ro;
1288 Mount_Flags = flags;
1289
1290 return ret;
1291}
1292
Gary Peck43acadf2012-11-21 21:19:01 -08001293bool TWPartition::Wipe(string New_File_System) {
Ethan Yonker726a0202014-12-16 20:01:38 -06001294 bool wiped = false, update_crypt = false, recreate_media = true;
Dees_Troy16c2b312013-01-15 16:51:18 +00001295 int check;
1296 string Layout_Filename = Mount_Point + "/.layout_version";
1297
Dees_Troy38bd7602012-09-14 13:33:53 -04001298 if (!Can_Be_Wiped) {
Matt Mower3c366972015-12-25 19:28:31 -06001299 gui_msg(Msg(msg::kError, "cannot_wipe=Partition {1} cannot be wiped.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04001300 return false;
1301 }
1302
Dees_Troyc51f1f92012-09-20 15:32:13 -04001303 if (Mount_Point == "/cache")
Dees_Troy2673cec2013-04-02 20:22:16 +00001304 Log_Offset = 0;
Dees_Troyc51f1f92012-09-20 15:32:13 -04001305
Dees_Troy16c2b312013-01-15 16:51:18 +00001306 if (Retain_Layout_Version && Mount(false) && TWFunc::Path_Exists(Layout_Filename))
1307 TWFunc::copy_file(Layout_Filename, "/.layout_version", 0600);
1308 else
1309 unlink("/.layout_version");
Dees_Troy38bd7602012-09-14 13:33:53 -04001310
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001311 if (Has_Data_Media && Current_File_System == New_File_System) {
Dees_Troy16c2b312013-01-15 16:51:18 +00001312 wiped = Wipe_Data_Without_Wiping_Media();
Ethan Yonker5eac2222014-06-11 12:22:55 -05001313 recreate_media = false;
Dees_Troy16c2b312013-01-15 16:51:18 +00001314 } else {
Dees_Troy16c2b312013-01-15 16:51:18 +00001315 DataManager::GetValue(TW_RM_RF_VAR, check);
1316
Hashcodedabfd492013-08-29 22:45:30 -07001317 if (check || Use_Rm_Rf)
Dees_Troy16c2b312013-01-15 16:51:18 +00001318 wiped = Wipe_RMRF();
1319 else if (New_File_System == "ext4")
1320 wiped = Wipe_EXT4();
1321 else if (New_File_System == "ext2" || New_File_System == "ext3")
1322 wiped = Wipe_EXT23(New_File_System);
1323 else if (New_File_System == "vfat")
1324 wiped = Wipe_FAT();
1325 else if (New_File_System == "exfat")
1326 wiped = Wipe_EXFAT();
1327 else if (New_File_System == "yaffs2")
1328 wiped = Wipe_MTD();
Dees_Troye5017042013-08-29 16:38:55 +00001329 else if (New_File_System == "f2fs")
1330 wiped = Wipe_F2FS();
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001331 else if (New_File_System == "ntfs")
1332 wiped = Wipe_NTFS();
Dees_Troy16c2b312013-01-15 16:51:18 +00001333 else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001334 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 +00001335 unlink("/.layout_version");
1336 return false;
1337 }
1338 update_crypt = wiped;
Gary Pecke8bc5d72012-12-21 06:45:25 -08001339 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001340
Gary Pecke8bc5d72012-12-21 06:45:25 -08001341 if (wiped) {
Dees_Troy1c1ac442013-01-17 21:42:14 +00001342 if (Mount_Point == "/cache")
1343 DataManager::Output_Version();
1344
Dees_Troy16c2b312013-01-15 16:51:18 +00001345 if (TWFunc::Path_Exists("/.layout_version") && Mount(false))
1346 TWFunc::copy_file("/.layout_version", Layout_Filename, 0600);
1347
1348 if (update_crypt) {
1349 Setup_File_System(false);
1350 if (Is_Encrypted && !Is_Decrypted) {
1351 // just wiped an encrypted partition back to its unencrypted state
1352 Is_Encrypted = false;
1353 Is_Decrypted = false;
1354 Decrypted_Block_Device = "";
1355 if (Mount_Point == "/data") {
1356 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1357 DataManager::SetValue(TW_IS_DECRYPTED, 0);
1358 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001359 }
1360 }
Ethan Yonker5eac2222014-06-11 12:22:55 -05001361
Ethan Yonker66a19492015-12-10 10:19:45 -06001362 if (Has_Data_Media && recreate_media) {
Ethan Yonker5eac2222014-06-11 12:22:55 -05001363 Recreate_Media_Folder();
1364 }
Matt Mower209c9632016-01-20 12:08:35 -06001365 if (Is_Storage && Mount(false))
1366 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Gary Pecke8bc5d72012-12-21 06:45:25 -08001367 }
Matt Mower209c9632016-01-20 12:08:35 -06001368
Gary Pecke8bc5d72012-12-21 06:45:25 -08001369 return wiped;
Dees_Troy51a0e822012-09-05 15:24:24 -04001370}
1371
Gary Peck43acadf2012-11-21 21:19:01 -08001372bool TWPartition::Wipe() {
Gary Peck82599a82012-11-21 16:23:12 -08001373 if (Is_File_System(Current_File_System))
1374 return Wipe(Current_File_System);
1375 else
1376 return Wipe(Fstab_File_System);
Gary Peck43acadf2012-11-21 21:19:01 -08001377}
1378
Dees_Troye58d5262012-09-21 12:27:57 -04001379bool TWPartition::Wipe_AndSec(void) {
1380 if (!Has_Android_Secure)
1381 return false;
1382
Dees_Troye58d5262012-09-21 12:27:57 -04001383 if (!Mount(true))
1384 return false;
1385
Ethan Yonker74db1572015-10-28 12:44:49 -05001386 gui_msg(Msg("wiping=Wiping {1}")(Backup_Display_Name));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001387 TWFunc::removeDir(Mount_Point + "/.android_secure/", true);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001388 return true;
Dees_Troye58d5262012-09-21 12:27:57 -04001389}
1390
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001391bool TWPartition::Can_Repair() {
Ethan Yonkera2719152015-05-28 09:44:41 -05001392 if (Mount_Read_Only)
1393 return false;
Matt Mower18794c82015-11-11 16:22:45 -06001394 if (Current_File_System == "vfat" && TWFunc::Path_Exists("/sbin/fsck.fat"))
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001395 return true;
1396 else if ((Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") && TWFunc::Path_Exists("/sbin/e2fsck"))
1397 return true;
1398 else if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/fsck.exfat"))
1399 return true;
1400 else if (Current_File_System == "f2fs" && TWFunc::Path_Exists("/sbin/fsck.f2fs"))
1401 return true;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001402 else if (Current_File_System == "ntfs" && (TWFunc::Path_Exists("/sbin/ntfsfix") || TWFunc::Path_Exists("/sbin/fsck.ntfs")))
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001403 return true;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001404 return false;
1405}
1406
1407bool TWPartition::Repair() {
1408 string command;
1409
1410 if (Current_File_System == "vfat") {
Matt Mower18794c82015-11-11 16:22:45 -06001411 if (!TWFunc::Path_Exists("/sbin/fsck.fat")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001412 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.fat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001413 return false;
1414 }
1415 if (!UnMount(true))
1416 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001417 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.fat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001418 Find_Actual_Block_Device();
Matt Mower18794c82015-11-11 16:22:45 -06001419 command = "/sbin/fsck.fat -y " + Actual_Block_Device;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001420 LOGINFO("Repair command: %s\n", command.c_str());
1421 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001422 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001423 return true;
1424 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001425 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001426 return false;
1427 }
1428 }
1429 if (Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") {
1430 if (!TWFunc::Path_Exists("/sbin/e2fsck")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001431 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("e2fsck"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001432 return false;
1433 }
1434 if (!UnMount(true))
1435 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001436 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("e2fsck"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001437 Find_Actual_Block_Device();
Ethan Yonkera2719152015-05-28 09:44:41 -05001438 command = "/sbin/e2fsck -fp " + Actual_Block_Device;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001439 LOGINFO("Repair command: %s\n", command.c_str());
1440 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001441 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001442 return true;
1443 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001444 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001445 return false;
1446 }
1447 }
1448 if (Current_File_System == "exfat") {
1449 if (!TWFunc::Path_Exists("/sbin/fsck.exfat")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001450 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.exfat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001451 return false;
1452 }
1453 if (!UnMount(true))
1454 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001455 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.exfat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001456 Find_Actual_Block_Device();
1457 command = "/sbin/fsck.exfat " + Actual_Block_Device;
1458 LOGINFO("Repair command: %s\n", command.c_str());
1459 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001460 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001461 return true;
1462 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001463 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001464 return false;
1465 }
1466 }
1467 if (Current_File_System == "f2fs") {
1468 if (!TWFunc::Path_Exists("/sbin/fsck.f2fs")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001469 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.f2fs"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001470 return false;
1471 }
1472 if (!UnMount(true))
1473 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001474 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.f2fs"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001475 Find_Actual_Block_Device();
1476 command = "/sbin/fsck.f2fs " + Actual_Block_Device;
1477 LOGINFO("Repair command: %s\n", command.c_str());
1478 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001479 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001480 return true;
1481 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001482 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001483 return false;
1484 }
1485 }
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001486 if (Current_File_System == "ntfs") {
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001487 string Ntfsfix_Binary;
1488 if (TWFunc::Path_Exists("/sbin/ntfsfix"))
1489 Ntfsfix_Binary = "ntfsfix";
1490 else if (TWFunc::Path_Exists("/sbin/fsck.ntfs"))
1491 Ntfsfix_Binary = "fsck.ntfs";
1492 else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001493 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("ntfsfix"));
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001494 return false;
1495 }
1496 if (!UnMount(true))
1497 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001498 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)(Ntfsfix_Binary));
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001499 Find_Actual_Block_Device();
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001500 command = "/sbin/" + Ntfsfix_Binary + " " + Actual_Block_Device;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001501 LOGINFO("Repair command: %s\n", command.c_str());
1502 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001503 gui_msg("done=Done.");
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001504 return true;
1505 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001506 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001507 return false;
1508 }
1509 }
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001510 return false;
1511}
1512
Ethan Yonkera2719152015-05-28 09:44:41 -05001513bool TWPartition::Can_Resize() {
1514 if (Mount_Read_Only)
1515 return false;
1516 if ((Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") && TWFunc::Path_Exists("/sbin/resize2fs"))
1517 return true;
1518 return false;
1519}
1520
1521bool TWPartition::Resize() {
1522 string command;
1523
1524 if (Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") {
1525 if (!Can_Repair()) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001526 LOGINFO("Cannot resize %s because %s cannot be repaired before resizing.\n", Display_Name.c_str(), Display_Name.c_str());
1527 gui_msg(Msg(msg::kError, "cannot_resize=Cannot resize {1}.")(Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001528 return false;
1529 }
1530 if (!TWFunc::Path_Exists("/sbin/resize2fs")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001531 LOGINFO("resize2fs does not exist! Cannot resize!\n");
1532 gui_msg(Msg(msg::kError, "cannot_resize=Cannot resize {1}.")(Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001533 return false;
1534 }
1535 // Repair will unmount so no need to do it twice
Ethan Yonker74db1572015-10-28 12:44:49 -05001536 gui_msg(Msg("repair_resize=Repairing {1} before resizing.")( Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001537 if (!Repair())
1538 return false;
Ethan Yonker74db1572015-10-28 12:44:49 -05001539 gui_msg(Msg("resizing=Resizing {1} using {2}...")(Display_Name)("resize2fs"));
Ethan Yonkera2719152015-05-28 09:44:41 -05001540 Find_Actual_Block_Device();
1541 command = "/sbin/resize2fs " + Actual_Block_Device;
1542 if (Length != 0) {
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001543 unsigned long long Actual_Size = IOCTL_Get_Block_Size();
1544 if (Actual_Size == 0)
1545 return false;
Ethan Yonkera2719152015-05-28 09:44:41 -05001546
Ethan Yonkera2719152015-05-28 09:44:41 -05001547 unsigned long long Block_Count;
1548 if (Length < 0) {
1549 // Reduce overall size by this length
1550 Block_Count = (Actual_Size / 1024LLU) - ((unsigned long long)(Length * -1) / 1024LLU);
1551 } else {
1552 // This is the size, not a size reduction
1553 Block_Count = ((unsigned long long)(Length) / 1024LLU);
1554 }
1555 char temp[256];
1556 sprintf(temp, "%llu", Block_Count);
1557 command += " ";
1558 command += temp;
1559 command += "K";
1560 }
1561 LOGINFO("Resize command: %s\n", command.c_str());
1562 if (TWFunc::Exec_Cmd(command) == 0) {
1563 Update_Size(true);
Ethan Yonker74db1572015-10-28 12:44:49 -05001564 gui_msg("done=Done.");
Ethan Yonkera2719152015-05-28 09:44:41 -05001565 return true;
1566 } else {
1567 Update_Size(true);
Ethan Yonker74db1572015-10-28 12:44:49 -05001568 gui_msg(Msg(msg::kError, "unable_resize=Unable to resize {1}.")(Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001569 return false;
1570 }
1571 }
1572 return false;
1573}
1574
Ethan Yonker472f5062016-02-25 13:47:30 -06001575bool TWPartition::Backup(const string& backup_folder, pid_t &tar_fork_pid, ProgressTracking *progress) {
bigbiff7abc5fe2015-01-17 16:53:12 -05001576 if (Backup_Method == FILES) {
Ethan Yonker472f5062016-02-25 13:47:30 -06001577 return Backup_Tar(backup_folder, progress, tar_fork_pid);
bigbiff7abc5fe2015-01-17 16:53:12 -05001578 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001579 else if (Backup_Method == DD)
Ethan Yonker472f5062016-02-25 13:47:30 -06001580 return Backup_Image(backup_folder, progress);
Dees_Troy38bd7602012-09-14 13:33:53 -04001581 else if (Backup_Method == FLASH_UTILS)
Ethan Yonker472f5062016-02-25 13:47:30 -06001582 return Backup_Dump_Image(backup_folder, progress);
Dees_Troy2673cec2013-04-02 20:22:16 +00001583 LOGERR("Unknown backup method for '%s'\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001584 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001585}
1586
Dees_Troy43d8b002012-09-17 16:00:01 -04001587bool TWPartition::Check_MD5(string restore_folder) {
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001588 string Full_Filename, md5file;
Dees_Troy43d8b002012-09-17 16:00:01 -04001589 char split_filename[512];
1590 int index = 0;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001591 twrpDigest md5sum;
Dees_Troy43d8b002012-09-17 16:00:01 -04001592
Captain Throwbackff5935f2014-09-23 16:08:34 -04001593 sync();
1594
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001595 memset(split_filename, 0, sizeof(split_filename));
Dees_Troy43d8b002012-09-17 16:00:01 -04001596 Full_Filename = restore_folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04001597 if (!TWFunc::Path_Exists(Full_Filename)) {
1598 // This is a split archive, we presume
1599 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
Dees_Troy2673cec2013-04-02 20:22:16 +00001600 LOGINFO("split_filename: %s\n", split_filename);
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001601 md5file = split_filename;
1602 md5file += ".md5";
1603 if (!TWFunc::Path_Exists(md5file)) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001604 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 -04001605 return false;
1606 }
1607 md5sum.setfn(split_filename);
Dees_Troy83bd4832013-05-04 12:39:56 +00001608 while (index < 1000) {
1609 if (TWFunc::Path_Exists(split_filename) && md5sum.verify_md5digest() != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001610 gui_msg(Msg(msg::kError, "md5_fail_match=MD5 failed to match on '{1}'.")(split_filename));
Dees_Troy43d8b002012-09-17 16:00:01 -04001611 return false;
1612 }
1613 index++;
1614 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001615 md5sum.setfn(split_filename);
Dees_Troy43d8b002012-09-17 16:00:01 -04001616 }
Dees_Troy4a2a1262012-09-18 09:33:47 -04001617 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001618 } else {
1619 // Single file archive
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001620 md5file = Full_Filename + ".md5";
1621 if (!TWFunc::Path_Exists(md5file)) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001622 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 -04001623 return false;
1624 }
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001625 md5sum.setfn(Full_Filename);
1626 if (md5sum.verify_md5digest() != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001627 gui_msg(Msg(msg::kError, "md5_fail_match=MD5 failed to match on '{1}'.")(split_filename));
Dees_Troy43d8b002012-09-17 16:00:01 -04001628 return false;
1629 } else
1630 return true;
1631 }
1632 return false;
1633}
1634
Ethan Yonker472f5062016-02-25 13:47:30 -06001635bool TWPartition::Restore(const string& restore_folder, ProgressTracking *progress) {
Gary Peck43acadf2012-11-21 21:19:01 -08001636 string Restore_File_System;
1637
Matt Mower3c366972015-12-25 19:28:31 -06001638 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, gui_parse_text("{@restoring_hdr}"));
Dees_Troy2673cec2013-04-02 20:22:16 +00001639 LOGINFO("Restore filename is: %s\n", Backup_FileName.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001640
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001641 Restore_File_System = Get_Restore_File_System(restore_folder);
1642
1643 if (Is_File_System(Restore_File_System))
Ethan Yonker472f5062016-02-25 13:47:30 -06001644 return Restore_Tar(restore_folder, Restore_File_System, progress);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001645 else if (Is_Image(Restore_File_System)) {
Ethan Yonker472f5062016-02-25 13:47:30 -06001646 return Restore_Image(restore_folder, Restore_File_System, progress);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001647 }
1648
1649 LOGERR("Unknown restore method for '%s'\n", Mount_Point.c_str());
1650 return false;
1651}
1652
Ethan Yonker472f5062016-02-25 13:47:30 -06001653string TWPartition::Get_Restore_File_System(const string& restore_folder) {
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001654 size_t first_period, second_period;
1655 string Restore_File_System;
1656
Gary Peck43acadf2012-11-21 21:19:01 -08001657 // Parse backup filename to extract the file system before wiping
1658 first_period = Backup_FileName.find(".");
1659 if (first_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001660 LOGERR("Unable to find file system (first period).\n");
thatd43bf2d2014-09-21 23:13:02 +02001661 return string();
Gary Peck43acadf2012-11-21 21:19:01 -08001662 }
1663 Restore_File_System = Backup_FileName.substr(first_period + 1, Backup_FileName.size() - first_period - 1);
1664 second_period = Restore_File_System.find(".");
1665 if (second_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001666 LOGERR("Unable to find file system (second period).\n");
thatd43bf2d2014-09-21 23:13:02 +02001667 return string();
Gary Peck43acadf2012-11-21 21:19:01 -08001668 }
1669 Restore_File_System.resize(second_period);
Dees_Troy2673cec2013-04-02 20:22:16 +00001670 LOGINFO("Restore file system is: '%s'.\n", Restore_File_System.c_str());
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001671 return Restore_File_System;
Dees_Troy51a0e822012-09-05 15:24:24 -04001672}
1673
1674string TWPartition::Backup_Method_By_Name() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001675 if (Backup_Method == NONE)
1676 return "none";
1677 else if (Backup_Method == FILES)
1678 return "files";
1679 else if (Backup_Method == DD)
1680 return "dd";
1681 else if (Backup_Method == FLASH_UTILS)
1682 return "flash_utils";
1683 else
1684 return "undefined";
1685 return "ERROR!";
Dees_Troy51a0e822012-09-05 15:24:24 -04001686}
1687
1688bool TWPartition::Decrypt(string Password) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001689 LOGINFO("STUB TWPartition::Decrypt, password: '%s'\n", Password.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001690 // Is this needed?
Dees_Troy51a0e822012-09-05 15:24:24 -04001691 return 1;
1692}
1693
1694bool TWPartition::Wipe_Encryption() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001695 bool Save_Data_Media = Has_Data_Media;
1696
1697 if (!UnMount(true))
1698 return false;
1699
Dees_Troy38bd7602012-09-14 13:33:53 -04001700 Has_Data_Media = false;
Dees_Troy74fb2e92013-04-15 14:35:47 +00001701 Decrypted_Block_Device = "";
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001702#ifdef TW_INCLUDE_CRYPTO
1703 if (Is_Decrypted) {
1704 if (!UnMount(true))
1705 return false;
Matt Mower2b18a532015-02-20 16:58:05 -06001706 if (delete_crypto_blk_dev((char*)("userdata")) != 0) {
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001707 LOGERR("Error deleting crypto block device, continuing anyway.\n");
1708 }
1709 }
1710#endif
Dees_Troy74fb2e92013-04-15 14:35:47 +00001711