blob: b764d947994f6a3891ec4433ca78d014ff67d5bf [file] [log] [blame]
Dees Troy3be70a82013-10-22 14:25:12 +00001/*
Ethan Yonker472f5062016-02-25 13:47:30 -06002 Copyright 2013 to 2016 TeamWin
Dees Troy3be70a82013-10-22 14:25:12 +00003 This file is part of TWRP/TeamWin Recovery Project.
4
5 TWRP is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 TWRP is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with TWRP. If not, see <http://www.gnu.org/licenses/>.
17*/
Dees_Troy51a0e822012-09-05 15:24:24 -040018
19#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
22#include <sys/stat.h>
23#include <sys/vfs.h>
Dees_Troy5bf43922012-09-07 16:07:55 -040024#include <sys/mount.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040025#include <unistd.h>
Dees_Troy51127312012-09-08 13:08:49 -040026#include <dirent.h>
thatf54e5392016-01-29 22:04:43 +010027#include <libgen.h>
bigbiffce8f83c2015-12-12 18:30:21 -050028#include <zlib.h>
bigbiff bigbiff9c754052013-01-09 09:09:08 -050029#include <iostream>
30#include <sstream>
Ethan Yonker66a19492015-12-10 10:19:45 -060031#include <sys/param.h>
Ethan Yonker472f5062016-02-25 13:47:30 -060032#include <fcntl.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040033
Dees_Troy657c3092012-09-10 20:32:10 -040034#ifdef TW_INCLUDE_CRYPTO
35 #include "cutils/properties.h"
36#endif
37
bigbiff7b4c7a62015-01-01 19:44:14 -050038#include "libblkid/include/blkid.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040039#include "variables.h"
Dees_Troy2673cec2013-04-02 20:22:16 +000040#include "twcommon.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040041#include "partitions.hpp"
Dees_Troy5bf43922012-09-07 16:07:55 -040042#include "data.hpp"
Dees_Troy43d8b002012-09-17 16:00:01 -040043#include "twrp-functions.hpp"
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -050044#include "twrpDigest.hpp"
bigbiff bigbiff9c754052013-01-09 09:09:08 -050045#include "twrpTar.hpp"
Ethan Yonker3fdcda42016-11-30 12:29:37 -060046#include "exclude.hpp"
Ethan Yonker1b7a31b2014-07-03 15:09:22 -050047#include "infomanager.hpp"
Ethan Yonker4b94cfd2014-12-11 10:00:45 -060048#include "set_metadata.h"
Ethan Yonker74db1572015-10-28 12:44:49 -050049#include "gui/gui.hpp"
bigbiffce8f83c2015-12-12 18:30:21 -050050#include "adbbu/libtwadbbu.hpp"
Dees_Troy5bf43922012-09-07 16:07:55 -040051extern "C" {
Dees_Troy38bd7602012-09-14 13:33:53 -040052 #include "mtdutils/mtdutils.h"
53 #include "mtdutils/mounts.h"
Dees_Troya95f55c2013-08-17 13:14:43 +000054#ifdef USE_EXT4
55 #include "make_ext4fs.h"
56#endif
Ethan Yonker71413f42014-02-26 13:36:08 -060057
58#ifdef TW_INCLUDE_CRYPTO
Ethan Yonker253368a2014-11-25 15:00:52 -060059 #include "crypto/lollipop/cryptfs.h"
Ethan Yonker66a19492015-12-10 10:19:45 -060060 #include "gpt/gpt.h"
Ethan Yonkerbd7492d2016-12-07 13:55:01 -060061 #ifdef TW_INCLUDE_FBE
62 #include "crypto/ext4crypt/Decrypt.h"
63 //#include "crypto/ext4crypt/Ext4Crypt.h"
64 #endif
Ethan Yonkerbc85b632015-08-09 12:48:14 -050065#else
66 #define CRYPT_FOOTER_OFFSET 0x4000
Ethan Yonker71413f42014-02-26 13:36:08 -060067#endif
Dees_Troy5bf43922012-09-07 16:07:55 -040068}
bigbiff bigbiffc49d7062013-10-11 20:28:00 -040069#ifdef HAVE_SELINUX
70#include "selinux/selinux.h"
Ethan Yonkerf27497f2014-02-09 11:48:33 -060071#include <selinux/label.h>
bigbiff bigbiffc49d7062013-10-11 20:28:00 -040072#endif
Dees Troy4159aed2014-02-28 17:24:43 +000073#ifdef HAVE_CAPABILITIES
74#include <sys/capability.h>
75#include <sys/xattr.h>
76#include <linux/xattr.h>
77#endif
HashBanged974bb2016-01-30 14:20:09 -050078#include <sparse_format.h>
Ethan Yonker472f5062016-02-25 13:47:30 -060079#include "progresstracking.hpp"
Dees_Troy51a0e822012-09-05 15:24:24 -040080
bigbiff bigbiff9c754052013-01-09 09:09:08 -050081using namespace std;
82
Dees_Troya95f55c2013-08-17 13:14:43 +000083extern struct selabel_handle *selinux_handle;
Ethan Yonker6277c792014-09-15 14:54:30 -050084extern bool datamedia;
Dees_Troya95f55c2013-08-17 13:14:43 +000085
Hashcode62bd9e02013-11-19 21:59:42 -080086struct flag_list {
87 const char *name;
88 unsigned flag;
89};
90
Matt Mower4ab42b12016-04-21 13:52:18 -050091const struct flag_list mount_flags[] = {
Hashcode62bd9e02013-11-19 21:59:42 -080092 { "noatime", MS_NOATIME },
93 { "noexec", MS_NOEXEC },
94 { "nosuid", MS_NOSUID },
95 { "nodev", MS_NODEV },
96 { "nodiratime", MS_NODIRATIME },
97 { "ro", MS_RDONLY },
98 { "rw", 0 },
99 { "remount", MS_REMOUNT },
100 { "bind", MS_BIND },
101 { "rec", MS_REC },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000102#ifdef MS_UNBINDABLE
Hashcode62bd9e02013-11-19 21:59:42 -0800103 { "unbindable", MS_UNBINDABLE },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000104#endif
105#ifdef MS_PRIVATE
Hashcode62bd9e02013-11-19 21:59:42 -0800106 { "private", MS_PRIVATE },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000107#endif
108#ifdef MS_SLAVE
Hashcode62bd9e02013-11-19 21:59:42 -0800109 { "slave", MS_SLAVE },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000110#endif
111#ifdef MS_SHARED
Hashcode62bd9e02013-11-19 21:59:42 -0800112 { "shared", MS_SHARED },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000113#endif
Hashcode62bd9e02013-11-19 21:59:42 -0800114 { "sync", MS_SYNCHRONOUS },
115 { "defaults", 0 },
116 { 0, 0 },
117};
118
Matt Mower2416a502016-04-12 19:54:46 -0500119enum TW_FSTAB_FLAGS {
120 TWFLAG_DEFAULTS, // Retain position
121 TWFLAG_ANDSEC,
122 TWFLAG_BACKUP,
123 TWFLAG_BACKUPNAME,
124 TWFLAG_BLOCKSIZE,
125 TWFLAG_CANBEWIPED,
126 TWFLAG_CANENCRYPTBACKUP,
127 TWFLAG_DISPLAY,
128 TWFLAG_ENCRYPTABLE,
129 TWFLAG_FLASHIMG,
130 TWFLAG_FORCEENCRYPT,
131 TWFLAG_FSFLAGS,
132 TWFLAG_IGNOREBLKID,
133 TWFLAG_LENGTH,
134 TWFLAG_MOUNTTODECRYPT,
135 TWFLAG_REMOVABLE,
136 TWFLAG_RETAINLAYOUTVERSION,
137 TWFLAG_SETTINGSSTORAGE,
138 TWFLAG_STORAGE,
139 TWFLAG_STORAGENAME,
140 TWFLAG_SUBPARTITIONOF,
141 TWFLAG_SYMLINK,
142 TWFLAG_USERDATAENCRYPTBACKUP,
143 TWFLAG_USERMRF,
144 TWFLAG_WIPEDURINGFACTORYRESET,
145 TWFLAG_WIPEINGUI,
Ethan Yonker1b190162016-12-05 15:25:19 -0600146 TWFLAG_SLOTSELECT,
Matt Mower2416a502016-04-12 19:54:46 -0500147};
148
149/* Flags without a trailing '=' are considered dual format flags and can be
150 * written as either 'flagname' or 'flagname=', where the character following
151 * the '=' is Y,y,1 for true and false otherwise.
152 */
153const struct flag_list tw_flags[] = {
154 { "andsec", TWFLAG_ANDSEC },
155 { "backup", TWFLAG_BACKUP },
156 { "backupname=", TWFLAG_BACKUPNAME },
157 { "blocksize=", TWFLAG_BLOCKSIZE },
158 { "canbewiped", TWFLAG_CANBEWIPED },
159 { "canencryptbackup", TWFLAG_CANENCRYPTBACKUP },
160 { "defaults", TWFLAG_DEFAULTS },
161 { "display=", TWFLAG_DISPLAY },
162 { "encryptable=", TWFLAG_ENCRYPTABLE },
163 { "flashimg", TWFLAG_FLASHIMG },
164 { "forceencrypt=", TWFLAG_FORCEENCRYPT },
165 { "fsflags=", TWFLAG_FSFLAGS },
166 { "ignoreblkid", TWFLAG_IGNOREBLKID },
167 { "length=", TWFLAG_LENGTH },
168 { "mounttodecrypt", TWFLAG_MOUNTTODECRYPT },
169 { "removable", TWFLAG_REMOVABLE },
170 { "retainlayoutversion", TWFLAG_RETAINLAYOUTVERSION },
171 { "settingsstorage", TWFLAG_SETTINGSSTORAGE },
172 { "storage", TWFLAG_STORAGE },
173 { "storagename=", TWFLAG_STORAGENAME },
174 { "subpartitionof=", TWFLAG_SUBPARTITIONOF },
175 { "symlink=", TWFLAG_SYMLINK },
176 { "userdataencryptbackup", TWFLAG_USERDATAENCRYPTBACKUP },
177 { "usermrf", TWFLAG_USERMRF },
178 { "wipeduringfactoryreset", TWFLAG_WIPEDURINGFACTORYRESET },
179 { "wipeingui", TWFLAG_WIPEINGUI },
Ethan Yonker1b190162016-12-05 15:25:19 -0600180 { "slotselect", TWFLAG_SLOTSELECT },
Matt Mower2416a502016-04-12 19:54:46 -0500181 { 0, 0 },
182};
183
that9e0593e2014-10-08 00:01:24 +0200184TWPartition::TWPartition() {
Dees_Troy51a0e822012-09-05 15:24:24 -0400185 Can_Be_Mounted = false;
186 Can_Be_Wiped = false;
Dees_Troya13d74f2013-03-24 08:54:55 -0500187 Can_Be_Backed_Up = false;
Vojtech Bocek1dc30982013-08-30 21:49:30 +0200188 Use_Rm_Rf = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400189 Wipe_During_Factory_Reset = false;
190 Wipe_Available_in_GUI = false;
191 Is_SubPartition = false;
Dees_Troy2691f9d2012-09-24 11:15:49 -0400192 Has_SubPartition = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400193 SubPartition_Of = "";
194 Symlink_Path = "";
195 Symlink_Mount_Point = "";
196 Mount_Point = "";
Dees_Troye58d5262012-09-21 12:27:57 -0400197 Backup_Path = "";
Dees_Troy38bd7602012-09-14 13:33:53 -0400198 Actual_Block_Device = "";
199 Primary_Block_Device = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400200 Alternate_Block_Device = "";
201 Removable = false;
202 Is_Present = false;
203 Length = 0;
204 Size = 0;
205 Used = 0;
206 Free = 0;
207 Backup_Size = 0;
208 Can_Be_Encrypted = false;
209 Is_Encrypted = false;
210 Is_Decrypted = false;
Ethan Yonkerbd7492d2016-12-07 13:55:01 -0600211 Is_FBE = false;
Ethan Yonker253368a2014-11-25 15:00:52 -0600212 Mount_To_Decrypt = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400213 Decrypted_Block_Device = "";
214 Display_Name = "";
Dees_Troya13d74f2013-03-24 08:54:55 -0500215 Backup_Display_Name = "";
216 Storage_Name = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400217 Backup_Name = "";
Dees_Troy63c8df72012-09-10 14:02:05 -0400218 Backup_FileName = "";
Dees_Troy38bd7602012-09-14 13:33:53 -0400219 MTD_Name = "";
bigbiffce8f83c2015-12-12 18:30:21 -0500220 Backup_Method = BM_NONE;
Dees_Troy83bd4832013-05-04 12:39:56 +0000221 Can_Encrypt_Backup = false;
222 Use_Userdata_Encryption = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400223 Has_Data_Media = false;
Dees_Troye58d5262012-09-21 12:27:57 -0400224 Has_Android_Secure = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400225 Is_Storage = false;
Dees_Troya13d74f2013-03-24 08:54:55 -0500226 Is_Settings_Storage = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400227 Storage_Path = "";
228 Current_File_System = "";
229 Fstab_File_System = "";
Hashcode62bd9e02013-11-19 21:59:42 -0800230 Mount_Flags = 0;
231 Mount_Options = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400232 Format_Block_Size = 0;
Dees_Troy68cab492012-12-12 19:29:35 +0000233 Ignore_Blkid = false;
Dees_Troy16c2b312013-01-15 16:51:18 +0000234 Retain_Layout_Version = false;
Ethan Yonker253368a2014-11-25 15:00:52 -0600235 Crypto_Key_Location = "footer";
Ethan Yonker726a0202014-12-16 20:01:38 -0600236 MTP_Storage_ID = 0;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600237 Can_Flash_Img = false;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500238 Mount_Read_Only = false;
Ethan Yonker66a19492015-12-10 10:19:45 -0600239 Is_Adopted_Storage = false;
240 Adopted_GUID = "";
Ethan Yonker1b190162016-12-05 15:25:19 -0600241 SlotSelect = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400242}
243
244TWPartition::~TWPartition(void) {
245 // Do nothing
246}
247
Matt Mower2b2dd152016-04-26 11:24:08 -0500248bool TWPartition::Process_Fstab_Line(const char *fstab_line, bool Display_Error) {
249 char full_line[MAX_FSTAB_LINE_LENGTH];
Matt Mower2416a502016-04-12 19:54:46 -0500250 char twflags[MAX_FSTAB_LINE_LENGTH] = "";
Dees_Troy5bf43922012-09-07 16:07:55 -0400251 char* ptr;
Matt Mower2b2dd152016-04-26 11:24:08 -0500252 int line_len = strlen(fstab_line), index = 0, item_index = 0;
Dees_Troya13d74f2013-03-24 08:54:55 -0500253 bool skip = false;
Dees_Troy5bf43922012-09-07 16:07:55 -0400254
Matt Mower2b2dd152016-04-26 11:24:08 -0500255 strlcpy(full_line, fstab_line, sizeof(full_line));
Dees_Troy51127312012-09-08 13:08:49 -0400256 for (index = 0; index < line_len; index++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500257 if (full_line[index] == 34)
258 skip = !skip;
259 if (!skip && full_line[index] <= 32)
Dees_Troy5bf43922012-09-07 16:07:55 -0400260 full_line[index] = '\0';
Dees_Troy5bf43922012-09-07 16:07:55 -0400261 }
Dees_Troy7c2dec82012-09-26 09:49:14 -0400262 Mount_Point = full_line;
Dees_Troy2673cec2013-04-02 20:22:16 +0000263 LOGINFO("Processing '%s'\n", Mount_Point.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -0400264 Backup_Path = Mount_Point;
Dees_Troya13d74f2013-03-24 08:54:55 -0500265 Storage_Path = Mount_Point;
Dees_Troy70737fa2013-04-08 13:19:20 +0000266 Display_Name = full_line + 1;
267 Backup_Display_Name = Display_Name;
268 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400269 index = Mount_Point.size();
270 while (index < line_len) {
271 while (index < line_len && full_line[index] == '\0')
272 index++;
273 if (index >= line_len)
274 continue;
275 ptr = full_line + index;
276 if (item_index == 0) {
277 // File System
278 Fstab_File_System = ptr;
279 Current_File_System = ptr;
280 item_index++;
281 } else if (item_index == 1) {
282 // Primary Block Device
Dees_Troy38bd7602012-09-14 13:33:53 -0400283 if (Fstab_File_System == "mtd" || Fstab_File_System == "yaffs2") {
Dees_Troy094207a2012-09-26 12:00:39 -0400284 MTD_Name = ptr;
285 Find_MTD_Block_Device(MTD_Name);
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400286 } else if (Fstab_File_System == "bml") {
287 if (Mount_Point == "/boot")
288 MTD_Name = "boot";
289 else if (Mount_Point == "/recovery")
290 MTD_Name = "recovery";
291 Primary_Block_Device = ptr;
292 if (*ptr != '/')
Dees_Troy2673cec2013-04-02 20:22:16 +0000293 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 -0400294 } else if (*ptr != '/') {
Dees_Troy5bf43922012-09-07 16:07:55 -0400295 if (Display_Error)
Matt Mower2b2dd152016-04-26 11:24:08 -0500296 LOGERR("Invalid block device '%s' in fstab line '%s'", ptr, fstab_line);
Dees_Troy5bf43922012-09-07 16:07:55 -0400297 else
Matt Mower2b2dd152016-04-26 11:24:08 -0500298 LOGINFO("Invalid block device '%s' in fstab line '%s'", ptr, fstab_line);
Matt Mower72c87ce2016-04-26 14:34:56 -0500299 return false;
Dees_Troy38bd7602012-09-14 13:33:53 -0400300 } else {
301 Primary_Block_Device = ptr;
302 Find_Real_Block_Device(Primary_Block_Device, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400303 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400304 item_index++;
305 } else if (item_index > 1) {
306 if (*ptr == '/') {
307 // Alternate Block Device
308 Alternate_Block_Device = ptr;
309 Find_Real_Block_Device(Alternate_Block_Device, Display_Error);
310 } else if (strlen(ptr) > 7 && strncmp(ptr, "length=", 7) == 0) {
311 // Partition length
312 ptr += 7;
313 Length = atoi(ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400314 } else if (strlen(ptr) > 6 && strncmp(ptr, "flags=", 6) == 0) {
315 // Custom flags, save for later so that new values aren't overwritten by defaults
316 ptr += 6;
Matt Mower2416a502016-04-12 19:54:46 -0500317 strlcpy(twflags, ptr, sizeof(twflags));
Dees_Troy38bd7602012-09-14 13:33:53 -0400318 } else if (strlen(ptr) == 4 && (strncmp(ptr, "NULL", 4) == 0 || strncmp(ptr, "null", 4) == 0 || strncmp(ptr, "null", 4) == 0)) {
319 // Do nothing
Dees_Troy5bf43922012-09-07 16:07:55 -0400320 } else {
321 // Unhandled data
Matt Mower2b2dd152016-04-26 11:24:08 -0500322 LOGINFO("Unhandled fstab information '%s' in fstab line '%s'\n", ptr, fstab_line);
Dees_Troy5bf43922012-09-07 16:07:55 -0400323 }
324 }
325 while (index < line_len && full_line[index] != '\0')
326 index++;
327 }
328
329 if (!Is_File_System(Fstab_File_System) && !Is_Image(Fstab_File_System)) {
330 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000331 LOGERR("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400332 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000333 LOGINFO("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Matt Mower72c87ce2016-04-26 14:34:56 -0500334 return false;
Dees_Troy5bf43922012-09-07 16:07:55 -0400335 } else if (Is_File_System(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400336 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400337 Setup_File_System(Display_Error);
338 if (Mount_Point == "/system") {
339 Display_Name = "System";
Dees_Troya13d74f2013-03-24 08:54:55 -0500340 Backup_Display_Name = Display_Name;
341 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400342 Wipe_Available_in_GUI = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500343 Can_Be_Backed_Up = true;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500344 Mount_Read_Only = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400345 } else if (Mount_Point == "/data") {
346 Display_Name = "Data";
Dees_Troya13d74f2013-03-24 08:54:55 -0500347 Backup_Display_Name = Display_Name;
348 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400349 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400350 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500351 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000352 Can_Encrypt_Backup = true;
353 Use_Userdata_Encryption = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400354 } else if (Mount_Point == "/cache") {
355 Display_Name = "Cache";
Dees_Troya13d74f2013-03-24 08:54:55 -0500356 Backup_Display_Name = Display_Name;
357 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400358 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400359 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500360 Can_Be_Backed_Up = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400361 } else if (Mount_Point == "/datadata") {
Dees_Troy51127312012-09-08 13:08:49 -0400362 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400363 Display_Name = "DataData";
Dees_Troya13d74f2013-03-24 08:54:55 -0500364 Backup_Display_Name = Display_Name;
365 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400366 Is_SubPartition = true;
367 SubPartition_Of = "/data";
Dees_Troy5bf43922012-09-07 16:07:55 -0400368 DataManager::SetValue(TW_HAS_DATADATA, 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500369 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000370 Can_Encrypt_Backup = true;
371 Use_Userdata_Encryption = false; // This whole partition should be encrypted
Dees_Troy5bf43922012-09-07 16:07:55 -0400372 } else if (Mount_Point == "/sd-ext") {
Dees_Troy51127312012-09-08 13:08:49 -0400373 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400374 Display_Name = "SD-Ext";
Dees_Troya13d74f2013-03-24 08:54:55 -0500375 Backup_Display_Name = Display_Name;
376 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400377 Wipe_Available_in_GUI = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400378 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500379 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000380 Can_Encrypt_Backup = true;
381 Use_Userdata_Encryption = true;
Dees_Troy2c50e182012-09-26 20:05:28 -0400382 } else if (Mount_Point == "/boot") {
383 Display_Name = "Boot";
Dees_Troya13d74f2013-03-24 08:54:55 -0500384 Backup_Display_Name = Display_Name;
Dees_Troy2c50e182012-09-26 20:05:28 -0400385 DataManager::SetValue("tw_boot_is_mountable", 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500386 Can_Be_Backed_Up = true;
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500387 } else if (Mount_Point == "/vendor") {
388 Display_Name = "Vendor";
389 Backup_Display_Name = Display_Name;
390 Storage_Name = Display_Name;
391 Mount_Read_Only = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400392 }
393#ifdef TW_EXTERNAL_STORAGE_PATH
394 if (Mount_Point == EXPAND(TW_EXTERNAL_STORAGE_PATH)) {
395 Is_Storage = true;
396 Storage_Path = EXPAND(TW_EXTERNAL_STORAGE_PATH);
Dees_Troyc51f1f92012-09-20 15:32:13 -0400397 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500398 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400399#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000400 if (Mount_Point == "/sdcard" || Mount_Point == "/external_sd" || Mount_Point == "/external_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400401 Is_Storage = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400402 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500403 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400404#endif
Dees_Troyb05ddee2013-01-28 20:24:50 +0000405 }
Dees_Troy8170a922012-09-18 15:40:25 -0400406#ifdef TW_INTERNAL_STORAGE_PATH
407 if (Mount_Point == EXPAND(TW_INTERNAL_STORAGE_PATH)) {
408 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500409 Is_Settings_Storage = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400410 Storage_Path = EXPAND(TW_INTERNAL_STORAGE_PATH);
Dees_Troya13d74f2013-03-24 08:54:55 -0500411 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400412 }
413#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000414 if (Mount_Point == "/emmc" || Mount_Point == "/internal_sd" || Mount_Point == "/internal_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400415 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500416 Is_Settings_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500417 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400418 }
419#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400420 } else if (Is_Image(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400421 Find_Actual_Block_Device();
Ethan Yonker1b190162016-12-05 15:25:19 -0600422 Setup_Image();
Dees_Troya13d74f2013-03-24 08:54:55 -0500423 if (Mount_Point == "/boot") {
424 Display_Name = "Boot";
425 Backup_Display_Name = Display_Name;
426 Can_Be_Backed_Up = true;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600427 Can_Flash_Img = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500428 } else if (Mount_Point == "/recovery") {
429 Display_Name = "Recovery";
430 Backup_Display_Name = Display_Name;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600431 Can_Flash_Img = true;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500432 } else if (Mount_Point == "/system_image") {
433 Display_Name = "System Image";
434 Backup_Display_Name = Display_Name;
HashBanged974bb2016-01-30 14:20:09 -0500435 Can_Flash_Img = true;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500436 Can_Be_Backed_Up = true;
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500437 } else if (Mount_Point == "/vendor_image") {
438 Display_Name = "Vendor Image";
439 Backup_Display_Name = Display_Name;
HashBanged974bb2016-01-30 14:20:09 -0500440 Can_Flash_Img = true;
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500441 Can_Be_Backed_Up = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500442 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400443 }
444
Matt Mower2416a502016-04-12 19:54:46 -0500445 // Process TWRP fstab flags
446 if (strlen(twflags) > 0) {
447 string Prev_Display_Name = Display_Name;
448 string Prev_Storage_Name = Storage_Name;
449 string Prev_Backup_Display_Name = Backup_Display_Name;
450 Display_Name = "";
451 Storage_Name = "";
452 Backup_Display_Name = "";
453
454 Process_TW_Flags(twflags, Display_Error);
455
456 bool has_display_name = !Display_Name.empty();
457 bool has_storage_name = !Storage_Name.empty();
458 bool has_backup_name = !Backup_Display_Name.empty();
459 if (!has_display_name) Display_Name = Prev_Display_Name;
460 if (!has_storage_name) Storage_Name = Prev_Storage_Name;
461 if (!has_backup_name) Backup_Display_Name = Prev_Backup_Display_Name;
462
463 if (has_display_name && !has_storage_name)
464 Storage_Name = Display_Name;
465 if (!has_display_name && has_storage_name)
466 Display_Name = Storage_Name;
467 if (has_display_name && !has_backup_name && Backup_Display_Name != "Android Secure")
468 Backup_Display_Name = Display_Name;
469 if (!has_display_name && has_backup_name)
470 Display_Name = Backup_Display_Name;
471 }
Dees_Troy51127312012-09-08 13:08:49 -0400472 return true;
473}
474
Matt Mower72c87ce2016-04-26 14:34:56 -0500475void TWPartition::Partition_Post_Processing(bool Display_Error) {
476 if (Mount_Point == "/data")
477 Setup_Data_Partition(Display_Error);
478 else if (Mount_Point == "/cache")
479 Setup_Cache_Partition(Display_Error);
480}
481
Ethan Yonkerbd7492d2016-12-07 13:55:01 -0600482void TWPartition::ExcludeAll(const string& path) {
483 backup_exclusions.add_absolute_dir(path);
484 wipe_exclusions.add_absolute_dir(path);
485}
486
Matt Mower72c87ce2016-04-26 14:34:56 -0500487void TWPartition::Setup_Data_Partition(bool Display_Error) {
488 if (Mount_Point != "/data")
489 return;
490
491 // Ensure /data is not mounted as tmpfs for qcom hardware decrypt
492 UnMount(false);
493
494#ifdef TW_INCLUDE_CRYPTO
495 if (datamedia)
496 Setup_Data_Media();
497 Can_Be_Encrypted = true;
498 char crypto_blkdev[255];
499 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
500 if (strcmp(crypto_blkdev, "error") != 0) {
501 DataManager::SetValue(TW_IS_DECRYPTED, 1);
502 Is_Encrypted = true;
503 Is_Decrypted = true;
Ethan Yonkerbd7492d2016-12-07 13:55:01 -0600504 Is_FBE = false;
505 DataManager::SetValue(TW_IS_FBE, 0);
Matt Mower72c87ce2016-04-26 14:34:56 -0500506 Decrypted_Block_Device = crypto_blkdev;
507 LOGINFO("Data already decrypted, new block device: '%s'\n", crypto_blkdev);
508 } else if (!Mount(false)) {
509 if (Is_Present) {
510 set_partition_data(Actual_Block_Device.c_str(), Crypto_Key_Location.c_str(), Fstab_File_System.c_str());
511 if (cryptfs_check_footer() == 0) {
512 Is_Encrypted = true;
513 Is_Decrypted = false;
514 Can_Be_Mounted = false;
515 Current_File_System = "emmc";
Ethan Yonker1b190162016-12-05 15:25:19 -0600516 Setup_Image();
Matt Mower72c87ce2016-04-26 14:34:56 -0500517 DataManager::SetValue(TW_IS_ENCRYPTED, 1);
518 DataManager::SetValue(TW_CRYPTO_PWTYPE, cryptfs_get_password_type());
519 DataManager::SetValue(TW_CRYPTO_PASSWORD, "");
520 DataManager::SetValue("tw_crypto_display", "");
521 } else {
522 gui_err("mount_data_footer=Could not mount /data and unable to find crypto footer.");
523 }
524 } else {
525 LOGERR("Primary block device '%s' for mount point '%s' is not present!\n", Primary_Block_Device.c_str(), Mount_Point.c_str());
526 }
527 } else {
Ethan Yonkerbd7492d2016-12-07 13:55:01 -0600528 if (TWFunc::Path_Exists("/data/unencrypted/key/version")) {
529 LOGINFO("File Based Encryption is present\n");
530#ifdef TW_INCLUDE_FBE
531 ExcludeAll(Mount_Point + "/convert_fbe");
532 ExcludeAll(Mount_Point + "/unencrypted");
533 //ExcludeAll(Mount_Point + "/system/users/0"); // we WILL need to retain some of this if multiple users are present or we just need to delete more folders for the extra users somewhere else
534 ExcludeAll(Mount_Point + "/misc/vold/user_keys");
535 ExcludeAll(Mount_Point + "/system_ce");
536 ExcludeAll(Mount_Point + "/system_de");
537 ExcludeAll(Mount_Point + "/misc_ce");
538 ExcludeAll(Mount_Point + "/misc_de");
539 ExcludeAll(Mount_Point + "/system/gatekeeper.password.key");
540 ExcludeAll(Mount_Point + "/system/gatekeeper.pattern.key");
541 ExcludeAll(Mount_Point + "/system/locksettings.db");
542 //ExcludeAll(Mount_Point + "/system/locksettings.db-shm"); // don't seem to need this one, but the other 2 are needed
543 ExcludeAll(Mount_Point + "/system/locksettings.db-wal");
544 ExcludeAll(Mount_Point + "/user_de");
545 //ExcludeAll(Mount_Point + "/misc/profiles/cur/0"); // might be important later
546 ExcludeAll(Mount_Point + "/misc/gatekeeper");
547 ExcludeAll(Mount_Point + "/drm/kek.dat");
548 int retry_count = 3;
549 while (!Decrypt_DE() && --retry_count)
550 usleep(2000);
551 if (retry_count > 0) {
552 property_set("ro.crypto.state", "encrypted");
553 Is_Encrypted = true;
554 Is_Decrypted = false;
555 Is_FBE = true;
556 DataManager::SetValue(TW_IS_FBE, 1);
557 DataManager::SetValue(TW_IS_ENCRYPTED, 1);
558 string filename;
559 DataManager::SetValue(TW_CRYPTO_PWTYPE, Get_Password_Type(0, filename));
560 DataManager::SetValue(TW_CRYPTO_PASSWORD, "");
561 DataManager::SetValue("tw_crypto_display", "");
562 }
563#else
564 LOGERR("FBE found but FBE support not present in TWRP\n");
565#endif
566 } else {
567 // Filesystem is not encrypted and the mount succeeded, so return to
568 // the original unmounted state
569 UnMount(false);
570 }
Matt Mower72c87ce2016-04-26 14:34:56 -0500571 }
572 if (datamedia && (!Is_Encrypted || (Is_Encrypted && Is_Decrypted))) {
573 Setup_Data_Media();
574 Recreate_Media_Folder();
575 }
576#else
577 if (datamedia) {
578 Setup_Data_Media();
579 Recreate_Media_Folder();
580 }
581#endif
582}
583
584void TWPartition::Setup_Cache_Partition(bool Display_Error __unused) {
585 if (Mount_Point != "/cache")
586 return;
587
588 if (!Mount(true))
589 return;
590
591 if (!TWFunc::Path_Exists("/cache/recovery/.")) {
592 LOGINFO("Recreating /cache/recovery folder\n");
593 if (mkdir("/cache/recovery", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0)
594 LOGERR("Could not create /cache/recovery\n");
595 }
596}
597
Matt Mower4ab42b12016-04-21 13:52:18 -0500598void TWPartition::Process_FS_Flags(const char *str) {
599 char *options = strdup(str);
600 char *ptr, *savep;
Hashcode62bd9e02013-11-19 21:59:42 -0800601
Matt Mower4ab42b12016-04-21 13:52:18 -0500602 Mount_Options = "";
Hashcode62bd9e02013-11-19 21:59:42 -0800603
Matt Mower4ab42b12016-04-21 13:52:18 -0500604 // Avoid issues with potentially nested strtok by using strtok_r
605 ptr = strtok_r(options, ",", &savep);
606 while (ptr) {
607 const struct flag_list* mount_flag = mount_flags;
608
609 for (; mount_flag->name; mount_flag++) {
610 // mount_flags are never postfixed by '=',
611 // so only match identical strings (including length)
612 if (strcmp(ptr, mount_flag->name) == 0) {
613 Mount_Flags |= mount_flag->flag;
Hashcode62bd9e02013-11-19 21:59:42 -0800614 break;
615 }
616 }
617
Matt Mower4ab42b12016-04-21 13:52:18 -0500618 if (mount_flag->flag == MS_RDONLY)
619 Mount_Read_Only = true;
Hashcode62bd9e02013-11-19 21:59:42 -0800620
Matt Mower4ab42b12016-04-21 13:52:18 -0500621 if (mount_flag->name != 0) {
622 if (!Mount_Options.empty())
623 Mount_Options += ",";
624 Mount_Options += mount_flag->name;
625 } else {
626 LOGINFO("Unhandled mount flag: '%s'\n", ptr);
627 }
628
629 ptr = strtok_r(NULL, ",", &savep);
630 }
631 free(options);
Hashcode62bd9e02013-11-19 21:59:42 -0800632}
633
Matt Mower2416a502016-04-12 19:54:46 -0500634void TWPartition::Apply_TW_Flag(const unsigned flag, const char* str, const bool val) {
635 switch (flag) {
636 case TWFLAG_ANDSEC:
637 Has_Android_Secure = val;
638 break;
639 case TWFLAG_BACKUP:
640 Can_Be_Backed_Up = val;
641 break;
642 case TWFLAG_BACKUPNAME:
643 Backup_Display_Name = str;
644 break;
645 case TWFLAG_BLOCKSIZE:
646 Format_Block_Size = (unsigned long)(atol(str));
647 break;
648 case TWFLAG_CANBEWIPED:
649 Can_Be_Wiped = val;
650 break;
651 case TWFLAG_CANENCRYPTBACKUP:
652 Can_Encrypt_Backup = val;
653 break;
654 case TWFLAG_DEFAULTS:
655 // Do nothing
656 break;
657 case TWFLAG_DISPLAY:
658 Display_Name = str;
659 break;
660 case TWFLAG_ENCRYPTABLE:
661 case TWFLAG_FORCEENCRYPT:
662 Crypto_Key_Location = str;
663 break;
664 case TWFLAG_FLASHIMG:
665 Can_Flash_Img = val;
666 break;
667 case TWFLAG_FSFLAGS:
Matt Mower4ab42b12016-04-21 13:52:18 -0500668 Process_FS_Flags(str);
Matt Mower2416a502016-04-12 19:54:46 -0500669 break;
670 case TWFLAG_IGNOREBLKID:
671 Ignore_Blkid = val;
672 break;
673 case TWFLAG_LENGTH:
674 Length = atoi(str);
675 break;
676 case TWFLAG_MOUNTTODECRYPT:
677 Mount_To_Decrypt = val;
678 break;
679 case TWFLAG_REMOVABLE:
680 Removable = val;
681 break;
682 case TWFLAG_RETAINLAYOUTVERSION:
683 Retain_Layout_Version = val;
684 break;
685 case TWFLAG_SETTINGSSTORAGE:
686 Is_Settings_Storage = val;
687 if (Is_Settings_Storage)
688 Is_Storage = true;
689 break;
690 case TWFLAG_STORAGE:
691 Is_Storage = val;
692 break;
693 case TWFLAG_STORAGENAME:
694 Storage_Name = str;
695 break;
696 case TWFLAG_SUBPARTITIONOF:
697 Is_SubPartition = true;
698 SubPartition_Of = str;
699 break;
700 case TWFLAG_SYMLINK:
701 Symlink_Path = str;
702 break;
703 case TWFLAG_USERDATAENCRYPTBACKUP:
704 Use_Userdata_Encryption = val;
705 if (Use_Userdata_Encryption)
706 Can_Encrypt_Backup = true;
707 break;
708 case TWFLAG_USERMRF:
709 Use_Rm_Rf = val;
710 break;
711 case TWFLAG_WIPEDURINGFACTORYRESET:
712 Wipe_During_Factory_Reset = val;
713 if (Wipe_During_Factory_Reset) {
714 Can_Be_Wiped = true;
715 Wipe_Available_in_GUI = true;
716 }
717 break;
718 case TWFLAG_WIPEINGUI:
719 Wipe_Available_in_GUI = val;
720 if (Wipe_Available_in_GUI)
721 Can_Be_Wiped = true;
722 break;
Ethan Yonker1b190162016-12-05 15:25:19 -0600723 case TWFLAG_SLOTSELECT:
724 SlotSelect = true;
725 break;
Matt Mower2416a502016-04-12 19:54:46 -0500726 default:
727 // Should not get here
728 LOGINFO("Flag identified for processing, but later unmatched: %i\n", flag);
729 break;
730 }
731}
Dees_Troy51127312012-09-08 13:08:49 -0400732
Matt Mower2416a502016-04-12 19:54:46 -0500733void TWPartition::Process_TW_Flags(char *flags, bool Display_Error) {
734 char separator[2] = {'\n', 0};
735 char *ptr, *savep;
736
737 // Semicolons within double-quotes are not forbidden, so replace
738 // only the semicolons intended as separators with '\n' for strtok
739 for (unsigned i = 0, skip = 0; i < strlen(flags); i++) {
740 if (flags[i] == '\"')
Dees_Troya13d74f2013-03-24 08:54:55 -0500741 skip = !skip;
Matt Mower2416a502016-04-12 19:54:46 -0500742 if (!skip && flags[i] == ';')
743 flags[i] = separator[0];
Dees_Troy51127312012-09-08 13:08:49 -0400744 }
745
Matt Mower2416a502016-04-12 19:54:46 -0500746 // Avoid issues with potentially nested strtok by using strtok_r
747 ptr = strtok_r(flags, separator, &savep);
748 while (ptr) {
749 int ptr_len = strlen(ptr);
750 const struct flag_list* tw_flag = tw_flags;
Hashcode62bd9e02013-11-19 21:59:42 -0800751
Matt Mower2416a502016-04-12 19:54:46 -0500752 for (; tw_flag->name; tw_flag++) {
753 int flag_len = strlen(tw_flag->name);
754
755 if (strncmp(ptr, tw_flag->name, flag_len) == 0) {
756 bool flag_val = false;
757
758 if (ptr_len > flag_len && (tw_flag->name)[flag_len-1] != '='
759 && ptr[flag_len] != '=') {
760 // Handle flags with same starting string
761 // (e.g. backup and backupname)
762 continue;
763 } else if (ptr_len > flag_len && ptr[flag_len] == '=') {
764 // Handle flags with dual format: Part 1
765 // (e.g. backup and backup=y. backup=y handled here)
766 ptr += flag_len + 1;
767 TWFunc::Strip_Quotes(ptr);
768 // Skip flags with empty argument
769 // (e.g. backup=)
770 if (strlen(ptr) == 0) {
771 LOGINFO("Flag missing argument or should not include '=': %s=\n", tw_flag->name);
772 break;
773 }
774 flag_val = strchr("yY1", *ptr) != NULL;
775 } else if (ptr_len == flag_len
776 && (tw_flag->name)[flag_len-1] == '=') {
777 // Skip flags missing argument after =
778 // (e.g. backupname=)
779 LOGINFO("Flag missing argument: %s\n", tw_flag->name);
780 break;
781 } else if (ptr_len > flag_len
782 && (tw_flag->name)[flag_len-1] == '=') {
783 // Handle arguments to flags
784 // (e.g. backupname="My Stuff")
785 ptr += flag_len;
786 TWFunc::Strip_Quotes(ptr);
787 // Skip flags with empty argument
788 // (e.g. backupname="")
789 if (strlen(ptr) == 0) {
790 LOGINFO("Flag missing argument: %s\n", tw_flag->name);
791 break;
792 }
793 } else if (ptr_len == flag_len) {
794 // Handle flags with dual format: Part 2
795 // (e.g. backup and backup=y. backup handled here)
796 flag_val = true;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600797 } else {
Matt Mower2416a502016-04-12 19:54:46 -0500798 LOGINFO("Flag matched, but could not be processed: %s\n", ptr);
799 break;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600800 }
Matt Mower2416a502016-04-12 19:54:46 -0500801
802 Apply_TW_Flag(tw_flag->flag, ptr, flag_val);
803 break;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600804 }
Matt Mower2416a502016-04-12 19:54:46 -0500805 }
806 if (tw_flag->name == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400807 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000808 LOGERR("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400809 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000810 LOGINFO("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400811 }
Matt Mower2416a502016-04-12 19:54:46 -0500812 ptr = strtok_r(NULL, separator, &savep);
Dees_Troy51127312012-09-08 13:08:49 -0400813 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400814}
815
Dees_Troy5bf43922012-09-07 16:07:55 -0400816bool TWPartition::Is_File_System(string File_System) {
817 if (File_System == "ext2" ||
Dees_Troy63c8df72012-09-10 14:02:05 -0400818 File_System == "ext3" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400819 File_System == "ext4" ||
820 File_System == "vfat" ||
821 File_System == "ntfs" ||
822 File_System == "yaffs2" ||
bigbiff bigbiff3e146522012-11-14 14:32:59 -0500823 File_System == "exfat" ||
Dees_Troye5017042013-08-29 16:38:55 +0000824 File_System == "f2fs" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400825 File_System == "auto")
826 return true;
827 else
828 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400829}
830
Dees_Troy5bf43922012-09-07 16:07:55 -0400831bool TWPartition::Is_Image(string File_System) {
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400832 if (File_System == "emmc" || File_System == "mtd" || File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -0400833 return true;
834 else
835 return false;
836}
837
Dees_Troy51127312012-09-08 13:08:49 -0400838bool TWPartition::Make_Dir(string Path, bool Display_Error) {
Ethan Yonker93ac7a02016-11-07 22:05:58 -0600839 if (TWFunc::Get_D_Type_From_Stat(Path) != S_IFDIR)
840 unlink(Path.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400841 if (!TWFunc::Path_Exists(Path)) {
Dees_Troy51127312012-09-08 13:08:49 -0400842 if (mkdir(Path.c_str(), 0777) == -1) {
843 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -0500844 gui_msg(Msg(msg::kError, "create_folder_strerr=Can not create '{1}' folder ({2}).")(Path)(strerror(errno)));
Dees_Troy51127312012-09-08 13:08:49 -0400845 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000846 LOGINFO("Can not create '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400847 return false;
848 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000849 LOGINFO("Created '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400850 return true;
851 }
852 }
853 return true;
854}
855
Dees_Troy5bf43922012-09-07 16:07:55 -0400856void TWPartition::Setup_File_System(bool Display_Error) {
857 struct statfs st;
858
859 Can_Be_Mounted = true;
860 Can_Be_Wiped = true;
861
Dees_Troy5bf43922012-09-07 16:07:55 -0400862 // Make the mount point folder if it doesn't exist
Dees_Troy51127312012-09-08 13:08:49 -0400863 Make_Dir(Mount_Point, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400864 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
865 Backup_Name = Display_Name;
bigbiffce8f83c2015-12-12 18:30:21 -0500866 Backup_Method = BM_FILES;
Dees_Troy5bf43922012-09-07 16:07:55 -0400867}
868
Ethan Yonker1b190162016-12-05 15:25:19 -0600869void TWPartition::Setup_Image() {
Dees_Troy5bf43922012-09-07 16:07:55 -0400870 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
871 Backup_Name = Display_Name;
Gary Peck82599a82012-11-21 16:23:12 -0800872 if (Current_File_System == "emmc")
bigbiffce8f83c2015-12-12 18:30:21 -0500873 Backup_Method = BM_DD;
Gary Peck82599a82012-11-21 16:23:12 -0800874 else if (Current_File_System == "mtd" || Current_File_System == "bml")
bigbiffce8f83c2015-12-12 18:30:21 -0500875 Backup_Method = BM_FLASH_UTILS;
Dees_Troy5bf43922012-09-07 16:07:55 -0400876 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000877 LOGINFO("Unhandled file system '%s' on image '%s'\n", Current_File_System.c_str(), Display_Name.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400878}
879
Dees_Troye58d5262012-09-21 12:27:57 -0400880void TWPartition::Setup_AndSec(void) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500881 Backup_Display_Name = "Android Secure";
Dees_Troye58d5262012-09-21 12:27:57 -0400882 Backup_Name = "and-sec";
Dees_Troya13d74f2013-03-24 08:54:55 -0500883 Can_Be_Backed_Up = true;
Dees_Troye58d5262012-09-21 12:27:57 -0400884 Has_Android_Secure = true;
885 Symlink_Path = Mount_Point + "/.android_secure";
886 Symlink_Mount_Point = "/and-sec";
887 Backup_Path = Symlink_Mount_Point;
888 Make_Dir("/and-sec", true);
889 Recreate_AndSec_Folder();
Kjell Braden3126a112016-06-19 16:58:15 +0000890 Mount_Storage_Retry(true);
Dees_Troye58d5262012-09-21 12:27:57 -0400891}
892
that9e0593e2014-10-08 00:01:24 +0200893void TWPartition::Setup_Data_Media() {
Ethan Yonker6277c792014-09-15 14:54:30 -0500894 LOGINFO("Setting up '%s' as data/media emulated storage.\n", Mount_Point.c_str());
Ethan Yonker66a19492015-12-10 10:19:45 -0600895 if (Storage_Name.empty() || Storage_Name == "Data")
896 Storage_Name = "Internal Storage";
Ethan Yonker6277c792014-09-15 14:54:30 -0500897 Has_Data_Media = true;
898 Is_Storage = true;
Ethan Yonker66a19492015-12-10 10:19:45 -0600899 Storage_Path = Mount_Point + "/media";
Ethan Yonker6277c792014-09-15 14:54:30 -0500900 Symlink_Path = Storage_Path;
Ethan Yonker66a19492015-12-10 10:19:45 -0600901 if (Mount_Point == "/data") {
902 Is_Settings_Storage = true;
903 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
904 Make_Dir("/emmc", false);
905 Symlink_Mount_Point = "/emmc";
906 } else {
907 Make_Dir("/sdcard", false);
908 Symlink_Mount_Point = "/sdcard";
909 }
910 if (Mount(false) && TWFunc::Path_Exists(Mount_Point + "/media/0")) {
911 Storage_Path = Mount_Point + "/media/0";
912 Symlink_Path = Storage_Path;
913 DataManager::SetValue(TW_INTERNAL_PATH, Mount_Point + "/media/0");
914 UnMount(true);
915 }
916 DataManager::SetValue("tw_has_internal", 1);
917 DataManager::SetValue("tw_has_data_media", 1);
Ethan Yonker3fdcda42016-11-30 12:29:37 -0600918 backup_exclusions.add_absolute_dir("/data/data/com.google.android.music/files");
919 backup_exclusions.add_absolute_dir(Mount_Point + "/misc/vold");
920 wipe_exclusions.add_absolute_dir(Mount_Point + "/misc/vold");
921 backup_exclusions.add_absolute_dir(Mount_Point + "/.layout_version");
922 wipe_exclusions.add_absolute_dir(Mount_Point + "/.layout_version");
923 backup_exclusions.add_absolute_dir(Mount_Point + "/system/storage.xml");
924 wipe_exclusions.add_absolute_dir(Mount_Point + "/system/storage.xml");
Ethan Yonker6277c792014-09-15 14:54:30 -0500925 } else {
Ethan Yonker66a19492015-12-10 10:19:45 -0600926 if (Mount(true) && TWFunc::Path_Exists(Mount_Point + "/media/0")) {
927 Storage_Path = Mount_Point + "/media/0";
928 Symlink_Path = Storage_Path;
929 UnMount(true);
930 }
Ethan Yonker6277c792014-09-15 14:54:30 -0500931 }
Ethan Yonker3fdcda42016-11-30 12:29:37 -0600932 backup_exclusions.add_absolute_dir(Mount_Point + "/media");
933 wipe_exclusions.add_absolute_dir(Mount_Point + "/media");
Ethan Yonker6277c792014-09-15 14:54:30 -0500934}
935
Dees_Troy5bf43922012-09-07 16:07:55 -0400936void TWPartition::Find_Real_Block_Device(string& Block, bool Display_Error) {
937 char device[512], realDevice[512];
938
939 strcpy(device, Block.c_str());
940 memset(realDevice, 0, sizeof(realDevice));
941 while (readlink(device, realDevice, sizeof(realDevice)) > 0)
942 {
943 strcpy(device, realDevice);
944 memset(realDevice, 0, sizeof(realDevice));
945 }
946
947 if (device[0] != '/') {
948 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000949 LOGERR("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400950 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000951 LOGINFO("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400952 return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400953 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400954 Block = device;
955 return;
956 }
957}
958
Kjell Braden3126a112016-06-19 16:58:15 +0000959bool TWPartition::Mount_Storage_Retry(bool Display_Error) {
Dees_Troy8e337f32012-10-13 22:07:49 -0400960 // On some devices, storage doesn't want to mount right away, retry and sleep
Kjell Braden3126a112016-06-19 16:58:15 +0000961 if (!Mount(Display_Error)) {
Dees_Troy8e337f32012-10-13 22:07:49 -0400962 int retry_count = 5;
963 while (retry_count > 0 && !Mount(false)) {
964 usleep(500000);
965 retry_count--;
966 }
Kjell Braden3126a112016-06-19 16:58:15 +0000967 return Mount(Display_Error);
Dees_Troy8e337f32012-10-13 22:07:49 -0400968 }
Kjell Braden3126a112016-06-19 16:58:15 +0000969 return true;
Dees_Troy8e337f32012-10-13 22:07:49 -0400970}
971
Dees_Troy38bd7602012-09-14 13:33:53 -0400972bool TWPartition::Find_MTD_Block_Device(string MTD_Name) {
973 FILE *fp = NULL;
974 char line[255];
975
976 fp = fopen("/proc/mtd", "rt");
977 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000978 LOGERR("Device does not support /proc/mtd\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400979 return false;
980 }
981
982 while (fgets(line, sizeof(line), fp) != NULL)
983 {
984 char device[32], label[32];
985 unsigned long size = 0;
986 char* fstype = NULL;
987 int deviceId;
988
989 sscanf(line, "%s %lx %*s %*c%s", device, &size, label);
990
991 // Skip header and blank lines
992 if ((strcmp(device, "dev:") == 0) || (strlen(line) < 8))
993 continue;
994
995 // Strip off the trailing " from the label
996 label[strlen(label)-1] = '\0';
997
998 if (strcmp(label, MTD_Name.c_str()) == 0) {
999 // We found our device
1000 // Strip off the trailing : from the device
1001 device[strlen(device)-1] = '\0';
1002 if (sscanf(device,"mtd%d", &deviceId) == 1) {
1003 sprintf(device, "/dev/block/mtdblock%d", deviceId);
1004 Primary_Block_Device = device;
Dees_Troy76543db2013-06-19 16:24:30 +00001005 fclose(fp);
1006 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -04001007 }
1008 }
1009 }
1010 fclose(fp);
1011
1012 return false;
1013}
1014
Dees_Troy51127312012-09-08 13:08:49 -04001015bool TWPartition::Get_Size_Via_statfs(bool Display_Error) {
1016 struct statfs st;
1017 string Local_Path = Mount_Point + "/.";
1018
1019 if (!Mount(Display_Error))
1020 return false;
1021
1022 if (statfs(Local_Path.c_str(), &st) != 0) {
1023 if (!Removable) {
1024 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001025 LOGERR("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -04001026 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001027 LOGINFO("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -04001028 }
1029 return false;
1030 }
1031 Size = (st.f_blocks * st.f_bsize);
1032 Used = ((st.f_blocks - st.f_bfree) * st.f_bsize);
1033 Free = (st.f_bfree * st.f_bsize);
1034 Backup_Size = Used;
1035 return true;
1036}
1037
1038bool TWPartition::Get_Size_Via_df(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001039 FILE* fp;
1040 char command[255], line[512];
1041 int include_block = 1;
1042 unsigned int min_len;
1043
1044 if (!Mount(Display_Error))
1045 return false;
1046
Dees_Troy38bd7602012-09-14 13:33:53 -04001047 min_len = Actual_Block_Device.size() + 2;
Dees_Troy51127312012-09-08 13:08:49 -04001048 sprintf(command, "df %s > /tmp/dfoutput.txt", Mount_Point.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001049 TWFunc::Exec_Cmd(command);
Dees_Troy51127312012-09-08 13:08:49 -04001050 fp = fopen("/tmp/dfoutput.txt", "rt");
1051 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001052 LOGINFO("Unable to open /tmp/dfoutput.txt.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001053 return false;
Dees_Troy51127312012-09-08 13:08:49 -04001054 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001055
1056 while (fgets(line, sizeof(line), fp) != NULL)
1057 {
1058 unsigned long blocks, used, available;
1059 char device[64];
1060 char tmpString[64];
1061
1062 if (strncmp(line, "Filesystem", 10) == 0)
1063 continue;
1064 if (strlen(line) < min_len) {
1065 include_block = 0;
1066 continue;
1067 }
1068 if (include_block) {
1069 sscanf(line, "%s %lu %lu %lu", device, &blocks, &used, &available);
1070 } else {
1071 // The device block string is so long that the df information is on the next line
1072 int space_count = 0;
Dees_Troye58d5262012-09-21 12:27:57 -04001073 sprintf(tmpString, "/dev/block/%s", Actual_Block_Device.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001074 while (tmpString[space_count] == 32)
1075 space_count++;
1076 sscanf(line + space_count, "%lu %lu %lu", &blocks, &used, &available);
1077 }
1078
1079 // Adjust block size to byte size
1080 Size = blocks * 1024ULL;
1081 Used = used * 1024ULL;
1082 Free = available * 1024ULL;
1083 Backup_Size = Used;
1084 }
1085 fclose(fp);
1086 return true;
1087}
1088
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001089unsigned long long TWPartition::IOCTL_Get_Block_Size() {
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001090 Find_Actual_Block_Device();
Ethan Yonker483e9f42016-01-11 22:21:18 -06001091
1092 return TWFunc::IOCTL_Get_Block_Size(Actual_Block_Device.c_str());
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001093}
1094
Dees_Troy5bf43922012-09-07 16:07:55 -04001095bool TWPartition::Find_Partition_Size(void) {
1096 FILE* fp;
1097 char line[512];
1098 string tmpdevice;
1099
igoriok87e3d932013-01-31 21:03:53 +02001100 fp = fopen("/proc/dumchar_info", "rt");
1101 if (fp != NULL) {
1102 while (fgets(line, sizeof(line), fp) != NULL)
1103 {
1104 char label[32], device[32];
1105 unsigned long size = 0;
1106
thatd43bf2d2014-09-21 23:13:02 +02001107 sscanf(line, "%s %lx %*x %*u %s", label, &size, device);
igoriok87e3d932013-01-31 21:03:53 +02001108
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05001109 // Skip header, annotation and blank lines
igoriok87e3d932013-01-31 21:03:53 +02001110 if ((strncmp(device, "/dev/", 5) != 0) || (strlen(line) < 8))
1111 continue;
1112
1113 tmpdevice = "/dev/";
1114 tmpdevice += label;
1115 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
1116 Size = size;
1117 fclose(fp);
1118 return true;
1119 }
1120 }
1121 }
1122
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001123 unsigned long long ioctl_size = IOCTL_Get_Block_Size();
1124 if (ioctl_size) {
1125 Size = ioctl_size;
1126 return true;
1127 }
1128
Dees_Troy5bf43922012-09-07 16:07:55 -04001129 // In this case, we'll first get the partitions we care about (with labels)
1130 fp = fopen("/proc/partitions", "rt");
1131 if (fp == NULL)
1132 return false;
1133
1134 while (fgets(line, sizeof(line), fp) != NULL)
1135 {
1136 unsigned long major, minor, blocks;
1137 char device[512];
1138 char tmpString[64];
1139
Dees_Troy63c8df72012-09-10 14:02:05 -04001140 if (strlen(line) < 7 || line[0] == 'm') continue;
Dees_Troy5bf43922012-09-07 16:07:55 -04001141 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
1142
1143 tmpdevice = "/dev/block/";
1144 tmpdevice += device;
Dees_Troy38bd7602012-09-14 13:33:53 -04001145 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001146 // Adjust block size to byte size
1147 Size = blocks * 1024ULL;
1148 fclose(fp);
1149 return true;
1150 }
1151 }
1152 fclose(fp);
1153 return false;
1154}
1155
Dees_Troy5bf43922012-09-07 16:07:55 -04001156bool TWPartition::Is_Mounted(void) {
1157 if (!Can_Be_Mounted)
1158 return false;
1159
1160 struct stat st1, st2;
1161 string test_path;
1162
1163 // Check to see if the mount point directory exists
1164 test_path = Mount_Point + "/.";
1165 if (stat(test_path.c_str(), &st1) != 0) return false;
1166
1167 // Check to see if the directory above the mount point exists
1168 test_path = Mount_Point + "/../.";
1169 if (stat(test_path.c_str(), &st2) != 0) return false;
1170
1171 // Compare the device IDs -- if they match then we're (probably) using tmpfs instead of an actual device
1172 int ret = (st1.st_dev != st2.st_dev) ? true : false;
1173
1174 return ret;
1175}
1176
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05001177bool TWPartition::Is_File_System_Writable(void) {
1178 if (!Is_File_System(Current_File_System) || !Is_Mounted())
1179 return false;
1180
1181 string test_path = Mount_Point + "/.";
1182 return (access(test_path.c_str(), W_OK) == 0);
1183}
1184
Dees_Troy5bf43922012-09-07 16:07:55 -04001185bool TWPartition::Mount(bool Display_Error) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001186 int exfat_mounted = 0;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001187 unsigned long flags = Mount_Flags;
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001188
Dees_Troy5bf43922012-09-07 16:07:55 -04001189 if (Is_Mounted()) {
1190 return true;
1191 } else if (!Can_Be_Mounted) {
1192 return false;
1193 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001194
1195 Find_Actual_Block_Device();
1196
1197 // Check the current file system before mounting
1198 Check_FS_Type();
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001199 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/exfat-fuse")) {
Dees_Troye34c1332013-02-06 19:13:00 +00001200 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 +00001201 LOGINFO("cmd: %s\n", cmd.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001202 string result;
1203 if (TWFunc::Exec_Cmd(cmd, result) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001204 LOGINFO("exfat-fuse failed to mount with result '%s', trying vfat\n", result.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001205 Current_File_System = "vfat";
1206 } else {
1207#ifdef TW_NO_EXFAT_FUSE
1208 UnMount(false);
1209 // We'll let the kernel handle it but using exfat-fuse to detect if the file system is actually exfat
1210 // Some kernels let us mount vfat as exfat which doesn't work out too well
1211#else
1212 exfat_mounted = 1;
1213#endif
1214 }
1215 }
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001216
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001217 if (Current_File_System == "ntfs" && (TWFunc::Path_Exists("/sbin/ntfs-3g") || TWFunc::Path_Exists("/sbin/mount.ntfs"))) {
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001218 string cmd;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001219 string Ntfsmount_Binary = "";
1220
1221 if (TWFunc::Path_Exists("/sbin/ntfs-3g"))
1222 Ntfsmount_Binary = "ntfs-3g";
1223 else if (TWFunc::Path_Exists("/sbin/mount.ntfs"))
1224 Ntfsmount_Binary = "mount.ntfs";
1225
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001226 if (Mount_Read_Only)
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001227 cmd = "/sbin/" + Ntfsmount_Binary + " -o ro " + Actual_Block_Device + " " + Mount_Point;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001228 else
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001229 cmd = "/sbin/" + Ntfsmount_Binary + " " + Actual_Block_Device + " " + Mount_Point;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001230 LOGINFO("cmd: '%s'\n", cmd.c_str());
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001231
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001232 if (TWFunc::Exec_Cmd(cmd) == 0) {
1233 return true;
1234 } else {
1235 LOGINFO("ntfs-3g failed to mount, trying regular mount method.\n");
1236 }
1237 }
1238
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001239 if (Mount_Read_Only)
1240 flags |= MS_RDONLY;
1241
Dees_Troy22042032012-12-18 21:23:08 +00001242 if (Fstab_File_System == "yaffs2") {
1243 // mount an MTD partition as a YAFFS2 filesystem.
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001244 flags = MS_NOATIME | MS_NODEV | MS_NODIRATIME;
1245 if (Mount_Read_Only)
1246 flags |= MS_RDONLY;
Dees_Troy76543db2013-06-19 16:24:30 +00001247 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags, NULL) < 0) {
1248 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags | MS_RDONLY, NULL) < 0) {
1249 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001250 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy76543db2013-06-19 16:24:30 +00001251 else
1252 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
1253 return false;
1254 } else {
1255 LOGINFO("Mounted '%s' (MTD) as RO\n", Mount_Point.c_str());
1256 return true;
1257 }
1258 } else {
1259 struct stat st;
1260 string test_path = Mount_Point;
1261 if (stat(test_path.c_str(), &st) < 0) {
1262 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001263 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy76543db2013-06-19 16:24:30 +00001264 else
1265 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
1266 return false;
1267 }
1268 mode_t new_mode = st.st_mode | S_IXUSR | S_IXGRP | S_IXOTH;
1269 if (new_mode != st.st_mode) {
1270 LOGINFO("Fixing execute permissions for %s\n", Mount_Point.c_str());
1271 if (chmod(Mount_Point.c_str(), new_mode) < 0) {
1272 if (Display_Error)
1273 LOGERR("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
1274 else
1275 LOGINFO("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
1276 return false;
1277 }
1278 }
Dees_Troy22042032012-12-18 21:23:08 +00001279 return true;
Dees_Troy76543db2013-06-19 16:24:30 +00001280 }
thatc7572eb2015-03-31 18:55:30 +02001281 }
1282
1283 string mount_fs = Current_File_System;
1284 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sys/module/texfat"))
1285 mount_fs = "texfat";
1286
1287 if (!exfat_mounted &&
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001288 mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), mount_fs.c_str(), flags, Mount_Options.c_str()) != 0 &&
1289 mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), mount_fs.c_str(), flags, NULL) != 0) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001290#ifdef TW_NO_EXFAT_FUSE
1291 if (Current_File_System == "exfat") {
Dees_Troy2673cec2013-04-02 20:22:16 +00001292 LOGINFO("Mounting exfat failed, trying vfat...\n");
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001293 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), "vfat", 0, NULL) != 0) {
Dees_Troy85f44ed2013-01-09 18:42:36 +00001294 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001295 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy85f44ed2013-01-09 18:42:36 +00001296 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001297 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001298 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 +00001299 return false;
Dees_Troy85f44ed2013-01-09 18:42:36 +00001300 }
Dees_Troyb05ddee2013-01-28 20:24:50 +00001301 } else {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001302#endif
bigbiff bigbiff26774a02014-03-29 18:22:00 -04001303 if (!Removable && Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001304 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001305 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001306 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
1307 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 +00001308 return false;
1309#ifdef TW_NO_EXFAT_FUSE
Dees_Troy85f44ed2013-01-09 18:42:36 +00001310 }
1311#endif
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001312 }
Ethan Yonker253368a2014-11-25 15:00:52 -06001313
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001314 if (Removable)
1315 Update_Size(Display_Error);
1316
xiaolu9416f4f2015-06-04 08:22:23 +08001317 if (!Symlink_Mount_Point.empty() && TWFunc::Path_Exists(Symlink_Path)) {
codelover2a3d4ce2015-03-14 20:26:49 +08001318 string Command = "mount -o bind '" + Symlink_Path + "' '" + Symlink_Mount_Point + "'";
Vojtech Bocek05534202013-09-11 08:11:56 +02001319 TWFunc::Exec_Cmd(Command);
Dees_Troy5bf43922012-09-07 16:07:55 -04001320 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001321 return true;
1322}
1323
1324bool TWPartition::UnMount(bool Display_Error) {
1325 if (Is_Mounted()) {
1326 int never_unmount_system;
1327
1328 DataManager::GetValue(TW_DONT_UNMOUNT_SYSTEM, never_unmount_system);
1329 if (never_unmount_system == 1 && Mount_Point == "/system")
1330 return true; // Never unmount system if you're not supposed to unmount it
1331
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001332 if (Is_Storage)
Ethan Yonker726a0202014-12-16 20:01:38 -06001333 PartitionManager.Remove_MTP_Storage(MTP_Storage_ID);
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001334
Dees_Troy38bd7602012-09-14 13:33:53 -04001335 if (!Symlink_Mount_Point.empty())
1336 umount(Symlink_Mount_Point.c_str());
1337
Dees_Troyb05ddee2013-01-28 20:24:50 +00001338 umount(Mount_Point.c_str());
1339 if (Is_Mounted()) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001340 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001341 gui_msg(Msg(msg::kError, "fail_unmount=Failed to unmount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy5bf43922012-09-07 16:07:55 -04001342 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001343 LOGINFO("Unable to unmount '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001344 return false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001345 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -04001346 return true;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001347 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001348 } else {
1349 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001350 }
1351}
1352
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05001353bool TWPartition::ReMount(bool Display_Error) {
1354 if (UnMount(Display_Error))
1355 return Mount(Display_Error);
1356 return false;
1357}
1358
1359bool TWPartition::ReMount_RW(bool Display_Error) {
1360 // No need to remount if already mounted rw
1361 if (Is_File_System_Writable())
1362 return true;
1363
1364 bool ro = Mount_Read_Only;
1365 int flags = Mount_Flags;
1366
1367 Mount_Read_Only = false;
1368 Mount_Flags &= ~MS_RDONLY;
1369
1370 bool ret = ReMount(Display_Error);
1371
1372 Mount_Read_Only = ro;
1373 Mount_Flags = flags;
1374
1375 return ret;
1376}
1377
Gary Peck43acadf2012-11-21 21:19:01 -08001378bool TWPartition::Wipe(string New_File_System) {
Ethan Yonker726a0202014-12-16 20:01:38 -06001379 bool wiped = false, update_crypt = false, recreate_media = true;
Dees_Troy16c2b312013-01-15 16:51:18 +00001380 int check;
1381 string Layout_Filename = Mount_Point + "/.layout_version";
1382
Dees_Troy38bd7602012-09-14 13:33:53 -04001383 if (!Can_Be_Wiped) {
Matt Mower3c366972015-12-25 19:28:31 -06001384 gui_msg(Msg(msg::kError, "cannot_wipe=Partition {1} cannot be wiped.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04001385 return false;
1386 }
1387
Dees_Troyc51f1f92012-09-20 15:32:13 -04001388 if (Mount_Point == "/cache")
Dees_Troy2673cec2013-04-02 20:22:16 +00001389 Log_Offset = 0;
Dees_Troyc51f1f92012-09-20 15:32:13 -04001390
Dees_Troy16c2b312013-01-15 16:51:18 +00001391 if (Retain_Layout_Version && Mount(false) && TWFunc::Path_Exists(Layout_Filename))
1392 TWFunc::copy_file(Layout_Filename, "/.layout_version", 0600);
1393 else
1394 unlink("/.layout_version");
Dees_Troy38bd7602012-09-14 13:33:53 -04001395
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001396 if (Has_Data_Media && Current_File_System == New_File_System) {
Dees_Troy16c2b312013-01-15 16:51:18 +00001397 wiped = Wipe_Data_Without_Wiping_Media();
Ethan Yonker5eac2222014-06-11 12:22:55 -05001398 recreate_media = false;
Dees_Troy16c2b312013-01-15 16:51:18 +00001399 } else {
Dees_Troy16c2b312013-01-15 16:51:18 +00001400 DataManager::GetValue(TW_RM_RF_VAR, check);
1401
Hashcodedabfd492013-08-29 22:45:30 -07001402 if (check || Use_Rm_Rf)
Dees_Troy16c2b312013-01-15 16:51:18 +00001403 wiped = Wipe_RMRF();
1404 else if (New_File_System == "ext4")
1405 wiped = Wipe_EXT4();
1406 else if (New_File_System == "ext2" || New_File_System == "ext3")
1407 wiped = Wipe_EXT23(New_File_System);
1408 else if (New_File_System == "vfat")
1409 wiped = Wipe_FAT();
1410 else if (New_File_System == "exfat")
1411 wiped = Wipe_EXFAT();
1412 else if (New_File_System == "yaffs2")
1413 wiped = Wipe_MTD();
Dees_Troye5017042013-08-29 16:38:55 +00001414 else if (New_File_System == "f2fs")
1415 wiped = Wipe_F2FS();
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001416 else if (New_File_System == "ntfs")
1417 wiped = Wipe_NTFS();
Dees_Troy16c2b312013-01-15 16:51:18 +00001418 else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001419 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 +00001420 unlink("/.layout_version");
1421 return false;
1422 }
1423 update_crypt = wiped;
Gary Pecke8bc5d72012-12-21 06:45:25 -08001424 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001425
Gary Pecke8bc5d72012-12-21 06:45:25 -08001426 if (wiped) {
Dees_Troy1c1ac442013-01-17 21:42:14 +00001427 if (Mount_Point == "/cache")
1428 DataManager::Output_Version();
1429
Dees_Troy16c2b312013-01-15 16:51:18 +00001430 if (TWFunc::Path_Exists("/.layout_version") && Mount(false))
1431 TWFunc::copy_file("/.layout_version", Layout_Filename, 0600);
1432
1433 if (update_crypt) {
1434 Setup_File_System(false);
1435 if (Is_Encrypted && !Is_Decrypted) {
1436 // just wiped an encrypted partition back to its unencrypted state
1437 Is_Encrypted = false;
1438 Is_Decrypted = false;
1439 Decrypted_Block_Device = "";
1440 if (Mount_Point == "/data") {
1441 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1442 DataManager::SetValue(TW_IS_DECRYPTED, 0);
1443 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001444 }
1445 }
Ethan Yonker5eac2222014-06-11 12:22:55 -05001446
Ethan Yonker66a19492015-12-10 10:19:45 -06001447 if (Has_Data_Media && recreate_media) {
Ethan Yonker5eac2222014-06-11 12:22:55 -05001448 Recreate_Media_Folder();
1449 }
Matt Mower209c9632016-01-20 12:08:35 -06001450 if (Is_Storage && Mount(false))
1451 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Gary Pecke8bc5d72012-12-21 06:45:25 -08001452 }
Matt Mower209c9632016-01-20 12:08:35 -06001453
Gary Pecke8bc5d72012-12-21 06:45:25 -08001454 return wiped;
Dees_Troy51a0e822012-09-05 15:24:24 -04001455}
1456
Gary Peck43acadf2012-11-21 21:19:01 -08001457bool TWPartition::Wipe() {
Gary Peck82599a82012-11-21 16:23:12 -08001458 if (Is_File_System(Current_File_System))
1459 return Wipe(Current_File_System);
1460 else
1461 return Wipe(Fstab_File_System);
Gary Peck43acadf2012-11-21 21:19:01 -08001462}
1463
Dees_Troye58d5262012-09-21 12:27:57 -04001464bool TWPartition::Wipe_AndSec(void) {
1465 if (!Has_Android_Secure)
1466 return false;
1467
Dees_Troye58d5262012-09-21 12:27:57 -04001468 if (!Mount(true))
1469 return false;
1470
Ethan Yonker74db1572015-10-28 12:44:49 -05001471 gui_msg(Msg("wiping=Wiping {1}")(Backup_Display_Name));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001472 TWFunc::removeDir(Mount_Point + "/.android_secure/", true);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001473 return true;
Dees_Troye58d5262012-09-21 12:27:57 -04001474}
1475
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001476bool TWPartition::Can_Repair() {
Ethan Yonkera2719152015-05-28 09:44:41 -05001477 if (Mount_Read_Only)
1478 return false;
Matt Mower18794c82015-11-11 16:22:45 -06001479 if (Current_File_System == "vfat" && TWFunc::Path_Exists("/sbin/fsck.fat"))
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001480 return true;
1481 else if ((Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") && TWFunc::Path_Exists("/sbin/e2fsck"))
1482 return true;
1483 else if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/fsck.exfat"))
1484 return true;
1485 else if (Current_File_System == "f2fs" && TWFunc::Path_Exists("/sbin/fsck.f2fs"))
1486 return true;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001487 else if (Current_File_System == "ntfs" && (TWFunc::Path_Exists("/sbin/ntfsfix") || TWFunc::Path_Exists("/sbin/fsck.ntfs")))
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001488 return true;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001489 return false;
1490}
1491
1492bool TWPartition::Repair() {
1493 string command;
1494
1495 if (Current_File_System == "vfat") {
Matt Mower18794c82015-11-11 16:22:45 -06001496 if (!TWFunc::Path_Exists("/sbin/fsck.fat")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001497 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.fat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001498 return false;
1499 }
1500 if (!UnMount(true))
1501 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001502 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.fat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001503 Find_Actual_Block_Device();
Matt Mower18794c82015-11-11 16:22:45 -06001504 command = "/sbin/fsck.fat -y " + Actual_Block_Device;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001505 LOGINFO("Repair command: %s\n", command.c_str());
1506 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001507 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001508 return true;
1509 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001510 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001511 return false;
1512 }
1513 }
1514 if (Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") {
1515 if (!TWFunc::Path_Exists("/sbin/e2fsck")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001516 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("e2fsck"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001517 return false;
1518 }
1519 if (!UnMount(true))
1520 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001521 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("e2fsck"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001522 Find_Actual_Block_Device();
Ethan Yonkera2719152015-05-28 09:44:41 -05001523 command = "/sbin/e2fsck -fp " + Actual_Block_Device;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001524 LOGINFO("Repair command: %s\n", command.c_str());
1525 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001526 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001527 return true;
1528 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001529 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001530 return false;
1531 }
1532 }
1533 if (Current_File_System == "exfat") {
1534 if (!TWFunc::Path_Exists("/sbin/fsck.exfat")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001535 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.exfat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001536 return false;
1537 }
1538 if (!UnMount(true))
1539 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001540 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.exfat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001541 Find_Actual_Block_Device();
1542 command = "/sbin/fsck.exfat " + Actual_Block_Device;
1543 LOGINFO("Repair command: %s\n", command.c_str());
1544 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001545 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001546 return true;
1547 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001548 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001549 return false;
1550 }
1551 }
1552 if (Current_File_System == "f2fs") {
1553 if (!TWFunc::Path_Exists("/sbin/fsck.f2fs")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001554 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.f2fs"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001555 return false;
1556 }
1557 if (!UnMount(true))
1558 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001559 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.f2fs"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001560 Find_Actual_Block_Device();
1561 command = "/sbin/fsck.f2fs " + Actual_Block_Device;
1562 LOGINFO("Repair command: %s\n", command.c_str());
1563 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001564 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001565 return true;
1566 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001567 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001568 return false;
1569 }
1570 }
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001571 if (Current_File_System == "ntfs") {
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001572 string Ntfsfix_Binary;
1573 if (TWFunc::Path_Exists("/sbin/ntfsfix"))
1574 Ntfsfix_Binary = "ntfsfix";
1575 else if (TWFunc::Path_Exists("/sbin/fsck.ntfs"))
1576 Ntfsfix_Binary = "fsck.ntfs";
1577 else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001578 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("ntfsfix"));
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001579 return false;
1580 }
1581 if (!UnMount(true))
1582 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001583 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)(Ntfsfix_Binary));
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001584 Find_Actual_Block_Device();
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001585 command = "/sbin/" + Ntfsfix_Binary + " " + Actual_Block_Device;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001586 LOGINFO("Repair command: %s\n", command.c_str());
1587 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001588 gui_msg("done=Done.");
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001589 return true;
1590 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001591 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001592 return false;
1593 }
1594 }
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001595 return false;
1596}
1597
Ethan Yonkera2719152015-05-28 09:44:41 -05001598bool TWPartition::Can_Resize() {
1599 if (Mount_Read_Only)
1600 return false;
1601 if ((Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") && TWFunc::Path_Exists("/sbin/resize2fs"))
1602 return true;
1603 return false;
1604}
1605
1606bool TWPartition::Resize() {
1607 string command;
1608
1609 if (Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") {
1610 if (!Can_Repair()) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001611 LOGINFO("Cannot resize %s because %s cannot be repaired before resizing.\n", Display_Name.c_str(), Display_Name.c_str());
1612 gui_msg(Msg(msg::kError, "cannot_resize=Cannot resize {1}.")(Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001613 return false;
1614 }
1615 if (!TWFunc::Path_Exists("/sbin/resize2fs")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001616 LOGINFO("resize2fs does not exist! Cannot resize!\n");
1617 gui_msg(Msg(msg::kError, "cannot_resize=Cannot resize {1}.")(Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001618 return false;
1619 }
1620 // Repair will unmount so no need to do it twice
Ethan Yonker74db1572015-10-28 12:44:49 -05001621 gui_msg(Msg("repair_resize=Repairing {1} before resizing.")( Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001622 if (!Repair())
1623 return false;
Ethan Yonker74db1572015-10-28 12:44:49 -05001624 gui_msg(Msg("resizing=Resizing {1} using {2}...")(Display_Name)("resize2fs"));
Ethan Yonkera2719152015-05-28 09:44:41 -05001625 Find_Actual_Block_Device();
1626 command = "/sbin/resize2fs " + Actual_Block_Device;
1627 if (Length != 0) {
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001628 unsigned long long Actual_Size = IOCTL_Get_Block_Size();
1629 if (Actual_Size == 0)
1630 return false;
Ethan Yonkera2719152015-05-28 09:44:41 -05001631
Ethan Yonkera2719152015-05-28 09:44:41 -05001632 unsigned long long Block_Count;
1633 if (Length < 0) {
1634 // Reduce overall size by this length
1635 Block_Count = (Actual_Size / 1024LLU) - ((unsigned long long)(Length * -1) / 1024LLU);
1636 } else {
1637 // This is the size, not a size reduction
1638 Block_Count = ((unsigned long long)(Length) / 1024LLU);
1639 }
1640 char temp[256];
1641 sprintf(temp, "%llu", Block_Count);
1642 command += " ";
1643 command += temp;
1644 command += "K";
1645 }
1646 LOGINFO("Resize command: %s\n", command.c_str());
1647 if (TWFunc::Exec_Cmd(command) == 0) {
1648 Update_Size(true);
Ethan Yonker74db1572015-10-28 12:44:49 -05001649 gui_msg("done=Done.");
Ethan Yonkera2719152015-05-28 09:44:41 -05001650 return true;
1651 } else {
1652 Update_Size(true);
Ethan Yonker74db1572015-10-28 12:44:49 -05001653 gui_msg(Msg(msg::kError, "unable_resize=Unable to resize {1}.")(Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001654 return false;
1655 }
1656 }
1657 return false;
1658}
1659
bigbiffce8f83c2015-12-12 18:30:21 -05001660bool TWPartition::Backup(PartitionSettings *part_settings, pid_t *tar_fork_pid) {
1661 if (Backup_Method == BM_FILES)
1662 return Backup_Tar(part_settings, tar_fork_pid);
1663 else if (Backup_Method == BM_DD)
1664 return Backup_Image(part_settings);
1665 else if (Backup_Method == BM_FLASH_UTILS)
1666 return Backup_Dump_Image(part_settings);
Dees_Troy2673cec2013-04-02 20:22:16 +00001667 LOGERR("Unknown backup method for '%s'\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001668 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001669}
1670
James Christopher Adduono79ae0932016-10-25 02:18:32 -04001671bool TWPartition::Check_Restore_File_MD5(const string& Filename) {
1672 twrpDigest md5sum;
1673
1674 md5sum.setfn(Filename);
1675 switch (md5sum.verify_md5digest()) {
1676 case MD5_OK:
1677 gui_msg(Msg("md5_matched=MD5 matched for '{1}'.")(Filename));
1678 return true;
1679 case MD5_FILE_UNREADABLE:
1680 case MD5_NOT_FOUND:
1681 gui_msg(Msg(msg::kError, "no_md5_found=No md5 file found for '{1}'. Please unselect Enable MD5 verification to restore.")(Filename));
1682 break;
1683 case MD5_MATCH_FAIL:
1684 gui_msg(Msg(msg::kError, "md5_fail_match=MD5 failed to match on '{1}'.")(Filename));
1685 break;
1686 }
1687 return false;
1688}
1689
bigbiff bigbiff8fd4b092016-08-30 20:48:53 -04001690bool TWPartition::Check_MD5(PartitionSettings *part_settings) {
James Christopher Adduono79ae0932016-10-25 02:18:32 -04001691 string Full_Filename;
Dees_Troy43d8b002012-09-17 16:00:01 -04001692 char split_filename[512];
1693 int index = 0;
1694
Captain Throwbackff5935f2014-09-23 16:08:34 -04001695 sync();
1696
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05001697 Full_Filename = part_settings->Backup_Folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04001698 if (!TWFunc::Path_Exists(Full_Filename)) {
1699 // This is a split archive, we presume
James Christopher Adduono79ae0932016-10-25 02:18:32 -04001700 memset(split_filename, 0, sizeof(split_filename));
Dees_Troy83bd4832013-05-04 12:39:56 +00001701 while (index < 1000) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001702 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
James Christopher Adduono79ae0932016-10-25 02:18:32 -04001703 if (!TWFunc::Path_Exists(split_filename))
1704 break;
1705 LOGINFO("split_filename: %s\n", split_filename);
1706 if (!Check_Restore_File_MD5(split_filename))
1707 return false;
1708 index++;
Dees_Troy43d8b002012-09-17 16:00:01 -04001709 }
Dees_Troy4a2a1262012-09-18 09:33:47 -04001710 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001711 }
James Christopher Adduono79ae0932016-10-25 02:18:32 -04001712 return Check_Restore_File_MD5(Full_Filename); // Single file archive
Dees_Troy43d8b002012-09-17 16:00:01 -04001713}
1714
bigbiffce8f83c2015-12-12 18:30:21 -05001715bool TWPartition::Restore(PartitionSettings *part_settings) {
Matt Mower3c366972015-12-25 19:28:31 -06001716 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, gui_parse_text("{@restoring_hdr}"));
Ethan Yonkere080c1f2016-09-19 13:50:25 -05001717 LOGINFO("Restore filename is: %s/%s\n", part_settings->Backup_Folder.c_str(), Backup_FileName.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001718
bigbiffce8f83c2015-12-12 18:30:21 -05001719 string Restore_File_System = Get_Restore_File_System(part_settings);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001720
1721 if (Is_File_System(Restore_File_System))
bigbiffce8f83c2015-12-12 18:30:21 -05001722 return Restore_Tar(part_settings);
1723 else if (Is_Image(Restore_File_System))
1724 return Restore_Image(part_settings);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001725
1726 LOGERR("Unknown restore method for '%s'\n", Mount_Point.c_str());
1727 return false;
1728}
1729
bigbiffce8f83c2015-12-12 18:30:21 -05001730string TWPartition::Get_Restore_File_System(PartitionSettings *part_settings) {
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001731 size_t first_period, second_period;
1732 string Restore_File_System;
1733
Gary Peck43acadf2012-11-21 21:19:01 -08001734 // Parse backup filename to extract the file system before wiping
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05001735 first_period = Backup_FileName.find(".");
Gary Peck43acadf2012-11-21 21:19:01 -08001736 if (first_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001737 LOGERR("Unable to find file system (first period).\n");
thatd43bf2d2014-09-21 23:13:02 +02001738 return string();
Gary Peck43acadf2012-11-21 21:19:01 -08001739 }
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05001740 Restore_File_System = Backup_FileName.substr(first_period + 1, Backup_FileName.size() - first_period - 1);
Gary Peck43acadf2012-11-21 21:19:01 -08001741 second_period = Restore_File_System.find(".");
1742 if (second_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001743 LOGERR("Unable to find file system (second period).\n");
thatd43bf2d2014-09-21 23:13:02 +02001744 return string();
Gary Peck43acadf2012-11-21 21:19:01 -08001745 }
1746 Restore_File_System.resize(second_period);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001747 return Restore_File_System;
Dees_Troy51a0e822012-09-05 15:24:24 -04001748}
1749
1750string TWPartition::Backup_Method_By_Name() {
bigbiffce8f83c2015-12-12 18:30:21 -05001751 if