blob: 1261a2a26b71a43126a65403fe29214d5c4b4901 [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
Matt Mower2b2dd152016-04-26 11:24:08 -0500238bool TWPartition::Process_Fstab_Line(const char *fstab_line, bool Display_Error) {
239 char full_line[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 char* ptr;
Matt Mower2b2dd152016-04-26 11:24:08 -0500242 int line_len = strlen(fstab_line), index = 0, item_index = 0;
Dees_Troya13d74f2013-03-24 08:54:55 -0500243 bool skip = false;
Dees_Troy5bf43922012-09-07 16:07:55 -0400244
Matt Mower2b2dd152016-04-26 11:24:08 -0500245 strlcpy(full_line, fstab_line, sizeof(full_line));
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)
Matt Mower2b2dd152016-04-26 11:24:08 -0500286 LOGERR("Invalid block device '%s' in fstab line '%s'", ptr, fstab_line);
Dees_Troy5bf43922012-09-07 16:07:55 -0400287 else
Matt Mower2b2dd152016-04-26 11:24:08 -0500288 LOGINFO("Invalid block device '%s' in fstab line '%s'", ptr, fstab_line);
Matt Mower72c87ce2016-04-26 14:34:56 -0500289 return false;
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));
Dees_Troy38bd7602012-09-14 13:33:53 -0400308 } else if (strlen(ptr) == 4 && (strncmp(ptr, "NULL", 4) == 0 || strncmp(ptr, "null", 4) == 0 || strncmp(ptr, "null", 4) == 0)) {
309 // Do nothing
Dees_Troy5bf43922012-09-07 16:07:55 -0400310 } else {
311 // Unhandled data
Matt Mower2b2dd152016-04-26 11:24:08 -0500312 LOGINFO("Unhandled fstab information '%s' in fstab line '%s'\n", ptr, fstab_line);
Dees_Troy5bf43922012-09-07 16:07:55 -0400313 }
314 }
315 while (index < line_len && full_line[index] != '\0')
316 index++;
317 }
318
319 if (!Is_File_System(Fstab_File_System) && !Is_Image(Fstab_File_System)) {
320 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000321 LOGERR("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400322 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000323 LOGINFO("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Matt Mower72c87ce2016-04-26 14:34:56 -0500324 return false;
Dees_Troy5bf43922012-09-07 16:07:55 -0400325 } else if (Is_File_System(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400326 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400327 Setup_File_System(Display_Error);
328 if (Mount_Point == "/system") {
329 Display_Name = "System";
Dees_Troya13d74f2013-03-24 08:54:55 -0500330 Backup_Display_Name = Display_Name;
331 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400332 Wipe_Available_in_GUI = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500333 Can_Be_Backed_Up = true;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500334 Mount_Read_Only = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400335 } else if (Mount_Point == "/data") {
336 Display_Name = "Data";
Dees_Troya13d74f2013-03-24 08:54:55 -0500337 Backup_Display_Name = Display_Name;
338 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400339 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400340 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500341 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000342 Can_Encrypt_Backup = true;
343 Use_Userdata_Encryption = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400344 } else if (Mount_Point == "/cache") {
345 Display_Name = "Cache";
Dees_Troya13d74f2013-03-24 08:54:55 -0500346 Backup_Display_Name = Display_Name;
347 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400348 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400349 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500350 Can_Be_Backed_Up = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400351 } else if (Mount_Point == "/datadata") {
Dees_Troy51127312012-09-08 13:08:49 -0400352 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400353 Display_Name = "DataData";
Dees_Troya13d74f2013-03-24 08:54:55 -0500354 Backup_Display_Name = Display_Name;
355 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400356 Is_SubPartition = true;
357 SubPartition_Of = "/data";
Dees_Troy5bf43922012-09-07 16:07:55 -0400358 DataManager::SetValue(TW_HAS_DATADATA, 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500359 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000360 Can_Encrypt_Backup = true;
361 Use_Userdata_Encryption = false; // This whole partition should be encrypted
Dees_Troy5bf43922012-09-07 16:07:55 -0400362 } else if (Mount_Point == "/sd-ext") {
Dees_Troy51127312012-09-08 13:08:49 -0400363 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400364 Display_Name = "SD-Ext";
Dees_Troya13d74f2013-03-24 08:54:55 -0500365 Backup_Display_Name = Display_Name;
366 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400367 Wipe_Available_in_GUI = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400368 Removable = true;
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 = true;
Dees_Troy2c50e182012-09-26 20:05:28 -0400372 } else if (Mount_Point == "/boot") {
373 Display_Name = "Boot";
Dees_Troya13d74f2013-03-24 08:54:55 -0500374 Backup_Display_Name = Display_Name;
Dees_Troy2c50e182012-09-26 20:05:28 -0400375 DataManager::SetValue("tw_boot_is_mountable", 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500376 Can_Be_Backed_Up = true;
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500377 } else if (Mount_Point == "/vendor") {
378 Display_Name = "Vendor";
379 Backup_Display_Name = Display_Name;
380 Storage_Name = Display_Name;
381 Mount_Read_Only = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400382 }
383#ifdef TW_EXTERNAL_STORAGE_PATH
384 if (Mount_Point == EXPAND(TW_EXTERNAL_STORAGE_PATH)) {
385 Is_Storage = true;
386 Storage_Path = EXPAND(TW_EXTERNAL_STORAGE_PATH);
Dees_Troyc51f1f92012-09-20 15:32:13 -0400387 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500388 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400389#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000390 if (Mount_Point == "/sdcard" || Mount_Point == "/external_sd" || Mount_Point == "/external_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400391 Is_Storage = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400392 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500393 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400394#endif
Dees_Troyb05ddee2013-01-28 20:24:50 +0000395 }
Dees_Troy8170a922012-09-18 15:40:25 -0400396#ifdef TW_INTERNAL_STORAGE_PATH
397 if (Mount_Point == EXPAND(TW_INTERNAL_STORAGE_PATH)) {
398 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500399 Is_Settings_Storage = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400400 Storage_Path = EXPAND(TW_INTERNAL_STORAGE_PATH);
Dees_Troya13d74f2013-03-24 08:54:55 -0500401 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400402 }
403#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000404 if (Mount_Point == "/emmc" || Mount_Point == "/internal_sd" || Mount_Point == "/internal_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400405 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500406 Is_Settings_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500407 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400408 }
409#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400410 } else if (Is_Image(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400411 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400412 Setup_Image(Display_Error);
Dees_Troya13d74f2013-03-24 08:54:55 -0500413 if (Mount_Point == "/boot") {
414 Display_Name = "Boot";
415 Backup_Display_Name = Display_Name;
416 Can_Be_Backed_Up = true;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600417 Can_Flash_Img = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500418 } else if (Mount_Point == "/recovery") {
419 Display_Name = "Recovery";
420 Backup_Display_Name = Display_Name;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600421 Can_Flash_Img = true;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500422 } else if (Mount_Point == "/system_image") {
423 Display_Name = "System Image";
424 Backup_Display_Name = Display_Name;
HashBanged974bb2016-01-30 14:20:09 -0500425 Can_Flash_Img = true;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500426 Can_Be_Backed_Up = true;
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500427 } else if (Mount_Point == "/vendor_image") {
428 Display_Name = "Vendor Image";
429 Backup_Display_Name = Display_Name;
HashBanged974bb2016-01-30 14:20:09 -0500430 Can_Flash_Img = true;
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500431 Can_Be_Backed_Up = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500432 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400433 }
434
Matt Mower2416a502016-04-12 19:54:46 -0500435 // Process TWRP fstab flags
436 if (strlen(twflags) > 0) {
437 string Prev_Display_Name = Display_Name;
438 string Prev_Storage_Name = Storage_Name;
439 string Prev_Backup_Display_Name = Backup_Display_Name;
440 Display_Name = "";
441 Storage_Name = "";
442 Backup_Display_Name = "";
443
444 Process_TW_Flags(twflags, Display_Error);
445
446 bool has_display_name = !Display_Name.empty();
447 bool has_storage_name = !Storage_Name.empty();
448 bool has_backup_name = !Backup_Display_Name.empty();
449 if (!has_display_name) Display_Name = Prev_Display_Name;
450 if (!has_storage_name) Storage_Name = Prev_Storage_Name;
451 if (!has_backup_name) Backup_Display_Name = Prev_Backup_Display_Name;
452
453 if (has_display_name && !has_storage_name)
454 Storage_Name = Display_Name;
455 if (!has_display_name && has_storage_name)
456 Display_Name = Storage_Name;
457 if (has_display_name && !has_backup_name && Backup_Display_Name != "Android Secure")
458 Backup_Display_Name = Display_Name;
459 if (!has_display_name && has_backup_name)
460 Display_Name = Backup_Display_Name;
461 }
Dees_Troy51127312012-09-08 13:08:49 -0400462 return true;
463}
464
Matt Mower72c87ce2016-04-26 14:34:56 -0500465void TWPartition::Partition_Post_Processing(bool Display_Error) {
466 if (Mount_Point == "/data")
467 Setup_Data_Partition(Display_Error);
468 else if (Mount_Point == "/cache")
469 Setup_Cache_Partition(Display_Error);
470}
471
472void TWPartition::Setup_Data_Partition(bool Display_Error) {
473 if (Mount_Point != "/data")
474 return;
475
476 // Ensure /data is not mounted as tmpfs for qcom hardware decrypt
477 UnMount(false);
478
479#ifdef TW_INCLUDE_CRYPTO
480 if (datamedia)
481 Setup_Data_Media();
482 Can_Be_Encrypted = true;
483 char crypto_blkdev[255];
484 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
485 if (strcmp(crypto_blkdev, "error") != 0) {
486 DataManager::SetValue(TW_IS_DECRYPTED, 1);
487 Is_Encrypted = true;
488 Is_Decrypted = true;
489 Decrypted_Block_Device = crypto_blkdev;
490 LOGINFO("Data already decrypted, new block device: '%s'\n", crypto_blkdev);
491 } else if (!Mount(false)) {
492 if (Is_Present) {
493 set_partition_data(Actual_Block_Device.c_str(), Crypto_Key_Location.c_str(), Fstab_File_System.c_str());
494 if (cryptfs_check_footer() == 0) {
495 Is_Encrypted = true;
496 Is_Decrypted = false;
497 Can_Be_Mounted = false;
498 Current_File_System = "emmc";
499 Setup_Image(Display_Error);
500 DataManager::SetValue(TW_IS_ENCRYPTED, 1);
501 DataManager::SetValue(TW_CRYPTO_PWTYPE, cryptfs_get_password_type());
502 DataManager::SetValue(TW_CRYPTO_PASSWORD, "");
503 DataManager::SetValue("tw_crypto_display", "");
504 } else {
505 gui_err("mount_data_footer=Could not mount /data and unable to find crypto footer.");
506 }
507 } else {
508 LOGERR("Primary block device '%s' for mount point '%s' is not present!\n", Primary_Block_Device.c_str(), Mount_Point.c_str());
509 }
510 } else {
511 // Filesystem is not encrypted and the mount succeeded, so return to
512 // the original unmounted state
513 UnMount(false);
514 }
515 if (datamedia && (!Is_Encrypted || (Is_Encrypted && Is_Decrypted))) {
516 Setup_Data_Media();
517 Recreate_Media_Folder();
518 }
519#else
520 if (datamedia) {
521 Setup_Data_Media();
522 Recreate_Media_Folder();
523 }
524#endif
525}
526
527void TWPartition::Setup_Cache_Partition(bool Display_Error __unused) {
528 if (Mount_Point != "/cache")
529 return;
530
531 if (!Mount(true))
532 return;
533
534 if (!TWFunc::Path_Exists("/cache/recovery/.")) {
535 LOGINFO("Recreating /cache/recovery folder\n");
536 if (mkdir("/cache/recovery", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0)
537 LOGERR("Could not create /cache/recovery\n");
538 }
539}
540
Matt Mower4ab42b12016-04-21 13:52:18 -0500541void TWPartition::Process_FS_Flags(const char *str) {
542 char *options = strdup(str);
543 char *ptr, *savep;
Hashcode62bd9e02013-11-19 21:59:42 -0800544
Matt Mower4ab42b12016-04-21 13:52:18 -0500545 Mount_Options = "";
Hashcode62bd9e02013-11-19 21:59:42 -0800546
Matt Mower4ab42b12016-04-21 13:52:18 -0500547 // Avoid issues with potentially nested strtok by using strtok_r
548 ptr = strtok_r(options, ",", &savep);
549 while (ptr) {
550 const struct flag_list* mount_flag = mount_flags;
551
552 for (; mount_flag->name; mount_flag++) {
553 // mount_flags are never postfixed by '=',
554 // so only match identical strings (including length)
555 if (strcmp(ptr, mount_flag->name) == 0) {
556 Mount_Flags |= mount_flag->flag;
Hashcode62bd9e02013-11-19 21:59:42 -0800557 break;
558 }
559 }
560
Matt Mower4ab42b12016-04-21 13:52:18 -0500561 if (mount_flag->flag == MS_RDONLY)
562 Mount_Read_Only = true;
Hashcode62bd9e02013-11-19 21:59:42 -0800563
Matt Mower4ab42b12016-04-21 13:52:18 -0500564 if (mount_flag->name != 0) {
565 if (!Mount_Options.empty())
566 Mount_Options += ",";
567 Mount_Options += mount_flag->name;
568 } else {
569 LOGINFO("Unhandled mount flag: '%s'\n", ptr);
570 }
571
572 ptr = strtok_r(NULL, ",", &savep);
573 }
574 free(options);
Hashcode62bd9e02013-11-19 21:59:42 -0800575}
576
Matt Mower2416a502016-04-12 19:54:46 -0500577void TWPartition::Apply_TW_Flag(const unsigned flag, const char* str, const bool val) {
578 switch (flag) {
579 case TWFLAG_ANDSEC:
580 Has_Android_Secure = val;
581 break;
582 case TWFLAG_BACKUP:
583 Can_Be_Backed_Up = val;
584 break;
585 case TWFLAG_BACKUPNAME:
586 Backup_Display_Name = str;
587 break;
588 case TWFLAG_BLOCKSIZE:
589 Format_Block_Size = (unsigned long)(atol(str));
590 break;
591 case TWFLAG_CANBEWIPED:
592 Can_Be_Wiped = val;
593 break;
594 case TWFLAG_CANENCRYPTBACKUP:
595 Can_Encrypt_Backup = val;
596 break;
597 case TWFLAG_DEFAULTS:
598 // Do nothing
599 break;
600 case TWFLAG_DISPLAY:
601 Display_Name = str;
602 break;
603 case TWFLAG_ENCRYPTABLE:
604 case TWFLAG_FORCEENCRYPT:
605 Crypto_Key_Location = str;
606 break;
607 case TWFLAG_FLASHIMG:
608 Can_Flash_Img = val;
609 break;
610 case TWFLAG_FSFLAGS:
Matt Mower4ab42b12016-04-21 13:52:18 -0500611 Process_FS_Flags(str);
Matt Mower2416a502016-04-12 19:54:46 -0500612 break;
613 case TWFLAG_IGNOREBLKID:
614 Ignore_Blkid = val;
615 break;
616 case TWFLAG_LENGTH:
617 Length = atoi(str);
618 break;
619 case TWFLAG_MOUNTTODECRYPT:
620 Mount_To_Decrypt = val;
621 break;
622 case TWFLAG_REMOVABLE:
623 Removable = val;
624 break;
625 case TWFLAG_RETAINLAYOUTVERSION:
626 Retain_Layout_Version = val;
627 break;
628 case TWFLAG_SETTINGSSTORAGE:
629 Is_Settings_Storage = val;
630 if (Is_Settings_Storage)
631 Is_Storage = true;
632 break;
633 case TWFLAG_STORAGE:
634 Is_Storage = val;
635 break;
636 case TWFLAG_STORAGENAME:
637 Storage_Name = str;
638 break;
639 case TWFLAG_SUBPARTITIONOF:
640 Is_SubPartition = true;
641 SubPartition_Of = str;
642 break;
643 case TWFLAG_SYMLINK:
644 Symlink_Path = str;
645 break;
646 case TWFLAG_USERDATAENCRYPTBACKUP:
647 Use_Userdata_Encryption = val;
648 if (Use_Userdata_Encryption)
649 Can_Encrypt_Backup = true;
650 break;
651 case TWFLAG_USERMRF:
652 Use_Rm_Rf = val;
653 break;
654 case TWFLAG_WIPEDURINGFACTORYRESET:
655 Wipe_During_Factory_Reset = val;
656 if (Wipe_During_Factory_Reset) {
657 Can_Be_Wiped = true;
658 Wipe_Available_in_GUI = true;
659 }
660 break;
661 case TWFLAG_WIPEINGUI:
662 Wipe_Available_in_GUI = val;
663 if (Wipe_Available_in_GUI)
664 Can_Be_Wiped = true;
665 break;
666 default:
667 // Should not get here
668 LOGINFO("Flag identified for processing, but later unmatched: %i\n", flag);
669 break;
670 }
671}
Dees_Troy51127312012-09-08 13:08:49 -0400672
Matt Mower2416a502016-04-12 19:54:46 -0500673void TWPartition::Process_TW_Flags(char *flags, bool Display_Error) {
674 char separator[2] = {'\n', 0};
675 char *ptr, *savep;
676
677 // Semicolons within double-quotes are not forbidden, so replace
678 // only the semicolons intended as separators with '\n' for strtok
679 for (unsigned i = 0, skip = 0; i < strlen(flags); i++) {
680 if (flags[i] == '\"')
Dees_Troya13d74f2013-03-24 08:54:55 -0500681 skip = !skip;
Matt Mower2416a502016-04-12 19:54:46 -0500682 if (!skip && flags[i] == ';')
683 flags[i] = separator[0];
Dees_Troy51127312012-09-08 13:08:49 -0400684 }
685
Matt Mower2416a502016-04-12 19:54:46 -0500686 // Avoid issues with potentially nested strtok by using strtok_r
687 ptr = strtok_r(flags, separator, &savep);
688 while (ptr) {
689 int ptr_len = strlen(ptr);
690 const struct flag_list* tw_flag = tw_flags;
Hashcode62bd9e02013-11-19 21:59:42 -0800691
Matt Mower2416a502016-04-12 19:54:46 -0500692 for (; tw_flag->name; tw_flag++) {
693 int flag_len = strlen(tw_flag->name);
694
695 if (strncmp(ptr, tw_flag->name, flag_len) == 0) {
696 bool flag_val = false;
697
698 if (ptr_len > flag_len && (tw_flag->name)[flag_len-1] != '='
699 && ptr[flag_len] != '=') {
700 // Handle flags with same starting string
701 // (e.g. backup and backupname)
702 continue;
703 } else if (ptr_len > flag_len && ptr[flag_len] == '=') {
704 // Handle flags with dual format: Part 1
705 // (e.g. backup and backup=y. backup=y handled here)
706 ptr += flag_len + 1;
707 TWFunc::Strip_Quotes(ptr);
708 // Skip flags with empty argument
709 // (e.g. backup=)
710 if (strlen(ptr) == 0) {
711 LOGINFO("Flag missing argument or should not include '=': %s=\n", tw_flag->name);
712 break;
713 }
714 flag_val = strchr("yY1", *ptr) != NULL;
715 } else if (ptr_len == flag_len
716 && (tw_flag->name)[flag_len-1] == '=') {
717 // Skip flags missing argument after =
718 // (e.g. backupname=)
719 LOGINFO("Flag missing argument: %s\n", tw_flag->name);
720 break;
721 } else if (ptr_len > flag_len
722 && (tw_flag->name)[flag_len-1] == '=') {
723 // Handle arguments to flags
724 // (e.g. backupname="My Stuff")
725 ptr += flag_len;
726 TWFunc::Strip_Quotes(ptr);
727 // Skip flags with empty argument
728 // (e.g. backupname="")
729 if (strlen(ptr) == 0) {
730 LOGINFO("Flag missing argument: %s\n", tw_flag->name);
731 break;
732 }
733 } else if (ptr_len == flag_len) {
734 // Handle flags with dual format: Part 2
735 // (e.g. backup and backup=y. backup handled here)
736 flag_val = true;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600737 } else {
Matt Mower2416a502016-04-12 19:54:46 -0500738 LOGINFO("Flag matched, but could not be processed: %s\n", ptr);
739 break;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600740 }
Matt Mower2416a502016-04-12 19:54:46 -0500741
742 Apply_TW_Flag(tw_flag->flag, ptr, flag_val);
743 break;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600744 }
Matt Mower2416a502016-04-12 19:54:46 -0500745 }
746 if (tw_flag->name == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400747 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000748 LOGERR("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400749 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000750 LOGINFO("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400751 }
Matt Mower2416a502016-04-12 19:54:46 -0500752 ptr = strtok_r(NULL, separator, &savep);
Dees_Troy51127312012-09-08 13:08:49 -0400753 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400754}
755
Dees_Troy5bf43922012-09-07 16:07:55 -0400756bool TWPartition::Is_File_System(string File_System) {
757 if (File_System == "ext2" ||
Dees_Troy63c8df72012-09-10 14:02:05 -0400758 File_System == "ext3" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400759 File_System == "ext4" ||
760 File_System == "vfat" ||
761 File_System == "ntfs" ||
762 File_System == "yaffs2" ||
bigbiff bigbiff3e146522012-11-14 14:32:59 -0500763 File_System == "exfat" ||
Dees_Troye5017042013-08-29 16:38:55 +0000764 File_System == "f2fs" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400765 File_System == "auto")
766 return true;
767 else
768 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400769}
770
Dees_Troy5bf43922012-09-07 16:07:55 -0400771bool TWPartition::Is_Image(string File_System) {
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400772 if (File_System == "emmc" || File_System == "mtd" || File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -0400773 return true;
774 else
775 return false;
776}
777
Dees_Troy51127312012-09-08 13:08:49 -0400778bool TWPartition::Make_Dir(string Path, bool Display_Error) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400779 if (!TWFunc::Path_Exists(Path)) {
Dees_Troy51127312012-09-08 13:08:49 -0400780 if (mkdir(Path.c_str(), 0777) == -1) {
781 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -0500782 gui_msg(Msg(msg::kError, "create_folder_strerr=Can not create '{1}' folder ({2}).")(Path)(strerror(errno)));
Dees_Troy51127312012-09-08 13:08:49 -0400783 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000784 LOGINFO("Can not create '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400785 return false;
786 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000787 LOGINFO("Created '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400788 return true;
789 }
790 }
791 return true;
792}
793
Dees_Troy5bf43922012-09-07 16:07:55 -0400794void TWPartition::Setup_File_System(bool Display_Error) {
795 struct statfs st;
796
797 Can_Be_Mounted = true;
798 Can_Be_Wiped = true;
799
Dees_Troy5bf43922012-09-07 16:07:55 -0400800 // Make the mount point folder if it doesn't exist
Dees_Troy51127312012-09-08 13:08:49 -0400801 Make_Dir(Mount_Point, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400802 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
803 Backup_Name = Display_Name;
804 Backup_Method = FILES;
805}
806
807void TWPartition::Setup_Image(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400808 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
809 Backup_Name = Display_Name;
Gary Peck82599a82012-11-21 16:23:12 -0800810 if (Current_File_System == "emmc")
Dees_Troy5bf43922012-09-07 16:07:55 -0400811 Backup_Method = DD;
Gary Peck82599a82012-11-21 16:23:12 -0800812 else if (Current_File_System == "mtd" || Current_File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -0400813 Backup_Method = FLASH_UTILS;
814 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000815 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 -0400816 if (Find_Partition_Size()) {
817 Used = Size;
818 Backup_Size = Size;
Dees_Troy51a0e822012-09-05 15:24:24 -0400819 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400820 if (Display_Error)
Dees Troyd932ce12013-10-18 17:12:59 +0000821 LOGERR("Unable to find partition size for '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400822 else
Dees Troyd932ce12013-10-18 17:12:59 +0000823 LOGINFO("Unable to find partition size for '%s'\n", Mount_Point.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400824 }
825}
826
Dees_Troye58d5262012-09-21 12:27:57 -0400827void TWPartition::Setup_AndSec(void) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500828 Backup_Display_Name = "Android Secure";
Dees_Troye58d5262012-09-21 12:27:57 -0400829 Backup_Name = "and-sec";
Dees_Troya13d74f2013-03-24 08:54:55 -0500830 Can_Be_Backed_Up = true;
Dees_Troye58d5262012-09-21 12:27:57 -0400831 Has_Android_Secure = true;
832 Symlink_Path = Mount_Point + "/.android_secure";
833 Symlink_Mount_Point = "/and-sec";
834 Backup_Path = Symlink_Mount_Point;
835 Make_Dir("/and-sec", true);
836 Recreate_AndSec_Folder();
Ethan Yonkerd4d10732014-02-03 15:27:52 -0600837 Mount_Storage_Retry();
Dees_Troye58d5262012-09-21 12:27:57 -0400838}
839
that9e0593e2014-10-08 00:01:24 +0200840void TWPartition::Setup_Data_Media() {
Ethan Yonker6277c792014-09-15 14:54:30 -0500841 LOGINFO("Setting up '%s' as data/media emulated storage.\n", Mount_Point.c_str());
Ethan Yonker66a19492015-12-10 10:19:45 -0600842 if (Storage_Name.empty() || Storage_Name == "Data")
843 Storage_Name = "Internal Storage";
Ethan Yonker6277c792014-09-15 14:54:30 -0500844 Has_Data_Media = true;
845 Is_Storage = true;
Ethan Yonker66a19492015-12-10 10:19:45 -0600846 Storage_Path = Mount_Point + "/media";
Ethan Yonker6277c792014-09-15 14:54:30 -0500847 Symlink_Path = Storage_Path;
Ethan Yonker66a19492015-12-10 10:19:45 -0600848 if (Mount_Point == "/data") {
849 Is_Settings_Storage = true;
850 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
851 Make_Dir("/emmc", false);
852 Symlink_Mount_Point = "/emmc";
853 } else {
854 Make_Dir("/sdcard", false);
855 Symlink_Mount_Point = "/sdcard";
856 }
857 if (Mount(false) && TWFunc::Path_Exists(Mount_Point + "/media/0")) {
858 Storage_Path = Mount_Point + "/media/0";
859 Symlink_Path = Storage_Path;
860 DataManager::SetValue(TW_INTERNAL_PATH, Mount_Point + "/media/0");
861 UnMount(true);
862 }
863 DataManager::SetValue("tw_has_internal", 1);
864 DataManager::SetValue("tw_has_data_media", 1);
865 du.add_absolute_dir(Mount_Point + "/misc/vold");
866 du.add_absolute_dir(Mount_Point + "/.layout_version");
867 du.add_absolute_dir(Mount_Point + "/system/storage.xml");
Ethan Yonker6277c792014-09-15 14:54:30 -0500868 } else {
Ethan Yonker66a19492015-12-10 10:19:45 -0600869 if (Mount(true) && TWFunc::Path_Exists(Mount_Point + "/media/0")) {
870 Storage_Path = Mount_Point + "/media/0";
871 Symlink_Path = Storage_Path;
872 UnMount(true);
873 }
Ethan Yonker6277c792014-09-15 14:54:30 -0500874 }
Ethan Yonker66a19492015-12-10 10:19:45 -0600875 du.add_absolute_dir(Mount_Point + "/media");
Ethan Yonker6277c792014-09-15 14:54:30 -0500876}
877
Dees_Troy5bf43922012-09-07 16:07:55 -0400878void TWPartition::Find_Real_Block_Device(string& Block, bool Display_Error) {
879 char device[512], realDevice[512];
880
881 strcpy(device, Block.c_str());
882 memset(realDevice, 0, sizeof(realDevice));
883 while (readlink(device, realDevice, sizeof(realDevice)) > 0)
884 {
885 strcpy(device, realDevice);
886 memset(realDevice, 0, sizeof(realDevice));
887 }
888
889 if (device[0] != '/') {
890 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000891 LOGERR("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400892 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000893 LOGINFO("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400894 return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400895 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400896 Block = device;
897 return;
898 }
899}
900
Dees_Troy8e337f32012-10-13 22:07:49 -0400901void TWPartition::Mount_Storage_Retry(void) {
902 // On some devices, storage doesn't want to mount right away, retry and sleep
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500903 if (!Mount(true)) {
Dees_Troy8e337f32012-10-13 22:07:49 -0400904 int retry_count = 5;
905 while (retry_count > 0 && !Mount(false)) {
906 usleep(500000);
907 retry_count--;
908 }
909 Mount(true);
910 }
911}
912
Dees_Troy38bd7602012-09-14 13:33:53 -0400913bool TWPartition::Find_MTD_Block_Device(string MTD_Name) {
914 FILE *fp = NULL;
915 char line[255];
916
917 fp = fopen("/proc/mtd", "rt");
918 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000919 LOGERR("Device does not support /proc/mtd\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400920 return false;
921 }
922
923 while (fgets(line, sizeof(line), fp) != NULL)
924 {
925 char device[32], label[32];
926 unsigned long size = 0;
927 char* fstype = NULL;
928 int deviceId;
929
930 sscanf(line, "%s %lx %*s %*c%s", device, &size, label);
931
932 // Skip header and blank lines
933 if ((strcmp(device, "dev:") == 0) || (strlen(line) < 8))
934 continue;
935
936 // Strip off the trailing " from the label
937 label[strlen(label)-1] = '\0';
938
939 if (strcmp(label, MTD_Name.c_str()) == 0) {
940 // We found our device
941 // Strip off the trailing : from the device
942 device[strlen(device)-1] = '\0';
943 if (sscanf(device,"mtd%d", &deviceId) == 1) {
944 sprintf(device, "/dev/block/mtdblock%d", deviceId);
945 Primary_Block_Device = device;
Dees_Troy76543db2013-06-19 16:24:30 +0000946 fclose(fp);
947 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -0400948 }
949 }
950 }
951 fclose(fp);
952
953 return false;
954}
955
Dees_Troy51127312012-09-08 13:08:49 -0400956bool TWPartition::Get_Size_Via_statfs(bool Display_Error) {
957 struct statfs st;
958 string Local_Path = Mount_Point + "/.";
959
960 if (!Mount(Display_Error))
961 return false;
962
963 if (statfs(Local_Path.c_str(), &st) != 0) {
964 if (!Removable) {
965 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000966 LOGERR("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400967 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000968 LOGINFO("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400969 }
970 return false;
971 }
972 Size = (st.f_blocks * st.f_bsize);
973 Used = ((st.f_blocks - st.f_bfree) * st.f_bsize);
974 Free = (st.f_bfree * st.f_bsize);
975 Backup_Size = Used;
976 return true;
977}
978
979bool TWPartition::Get_Size_Via_df(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400980 FILE* fp;
981 char command[255], line[512];
982 int include_block = 1;
983 unsigned int min_len;
984
985 if (!Mount(Display_Error))
986 return false;
987
Dees_Troy38bd7602012-09-14 13:33:53 -0400988 min_len = Actual_Block_Device.size() + 2;
Dees_Troy51127312012-09-08 13:08:49 -0400989 sprintf(command, "df %s > /tmp/dfoutput.txt", Mount_Point.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +0200990 TWFunc::Exec_Cmd(command);
Dees_Troy51127312012-09-08 13:08:49 -0400991 fp = fopen("/tmp/dfoutput.txt", "rt");
992 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000993 LOGINFO("Unable to open /tmp/dfoutput.txt.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400994 return false;
Dees_Troy51127312012-09-08 13:08:49 -0400995 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400996
997 while (fgets(line, sizeof(line), fp) != NULL)
998 {
999 unsigned long blocks, used, available;
1000 char device[64];
1001 char tmpString[64];
1002
1003 if (strncmp(line, "Filesystem", 10) == 0)
1004 continue;
1005 if (strlen(line) < min_len) {
1006 include_block = 0;
1007 continue;
1008 }
1009 if (include_block) {
1010 sscanf(line, "%s %lu %lu %lu", device, &blocks, &used, &available);
1011 } else {
1012 // The device block string is so long that the df information is on the next line
1013 int space_count = 0;
Dees_Troye58d5262012-09-21 12:27:57 -04001014 sprintf(tmpString, "/dev/block/%s", Actual_Block_Device.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001015 while (tmpString[space_count] == 32)
1016 space_count++;
1017 sscanf(line + space_count, "%lu %lu %lu", &blocks, &used, &available);
1018 }
1019
1020 // Adjust block size to byte size
1021 Size = blocks * 1024ULL;
1022 Used = used * 1024ULL;
1023 Free = available * 1024ULL;
1024 Backup_Size = Used;
1025 }
1026 fclose(fp);
1027 return true;
1028}
1029
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001030unsigned long long TWPartition::IOCTL_Get_Block_Size() {
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001031 Find_Actual_Block_Device();
Ethan Yonker483e9f42016-01-11 22:21:18 -06001032
1033 return TWFunc::IOCTL_Get_Block_Size(Actual_Block_Device.c_str());
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001034}
1035
Dees_Troy5bf43922012-09-07 16:07:55 -04001036bool TWPartition::Find_Partition_Size(void) {
1037 FILE* fp;
1038 char line[512];
1039 string tmpdevice;
1040
igoriok87e3d932013-01-31 21:03:53 +02001041 fp = fopen("/proc/dumchar_info", "rt");
1042 if (fp != NULL) {
1043 while (fgets(line, sizeof(line), fp) != NULL)
1044 {
1045 char label[32], device[32];
1046 unsigned long size = 0;
1047
thatd43bf2d2014-09-21 23:13:02 +02001048 sscanf(line, "%s %lx %*x %*u %s", label, &size, device);
igoriok87e3d932013-01-31 21:03:53 +02001049
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05001050 // Skip header, annotation and blank lines
igoriok87e3d932013-01-31 21:03:53 +02001051 if ((strncmp(device, "/dev/", 5) != 0) || (strlen(line) < 8))
1052 continue;
1053
1054 tmpdevice = "/dev/";
1055 tmpdevice += label;
1056 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
1057 Size = size;
1058 fclose(fp);
1059 return true;
1060 }
1061 }
1062 }
1063
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001064 unsigned long long ioctl_size = IOCTL_Get_Block_Size();
1065 if (ioctl_size) {
1066 Size = ioctl_size;
1067 return true;
1068 }
1069
Dees_Troy5bf43922012-09-07 16:07:55 -04001070 // In this case, we'll first get the partitions we care about (with labels)
1071 fp = fopen("/proc/partitions", "rt");
1072 if (fp == NULL)
1073 return false;
1074
1075 while (fgets(line, sizeof(line), fp) != NULL)
1076 {
1077 unsigned long major, minor, blocks;
1078 char device[512];
1079 char tmpString[64];
1080
Dees_Troy63c8df72012-09-10 14:02:05 -04001081 if (strlen(line) < 7 || line[0] == 'm') continue;
Dees_Troy5bf43922012-09-07 16:07:55 -04001082 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
1083
1084 tmpdevice = "/dev/block/";
1085 tmpdevice += device;
Dees_Troy38bd7602012-09-14 13:33:53 -04001086 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001087 // Adjust block size to byte size
1088 Size = blocks * 1024ULL;
1089 fclose(fp);
1090 return true;
1091 }
1092 }
1093 fclose(fp);
1094 return false;
1095}
1096
Dees_Troy5bf43922012-09-07 16:07:55 -04001097bool TWPartition::Is_Mounted(void) {
1098 if (!Can_Be_Mounted)
1099 return false;
1100
1101 struct stat st1, st2;
1102 string test_path;
1103
1104 // Check to see if the mount point directory exists
1105 test_path = Mount_Point + "/.";
1106 if (stat(test_path.c_str(), &st1) != 0) return false;
1107
1108 // Check to see if the directory above the mount point exists
1109 test_path = Mount_Point + "/../.";
1110 if (stat(test_path.c_str(), &st2) != 0) return false;
1111
1112 // Compare the device IDs -- if they match then we're (probably) using tmpfs instead of an actual device
1113 int ret = (st1.st_dev != st2.st_dev) ? true : false;
1114
1115 return ret;
1116}
1117
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05001118bool TWPartition::Is_File_System_Writable(void) {
1119 if (!Is_File_System(Current_File_System) || !Is_Mounted())
1120 return false;
1121
1122 string test_path = Mount_Point + "/.";
1123 return (access(test_path.c_str(), W_OK) == 0);
1124}
1125
Dees_Troy5bf43922012-09-07 16:07:55 -04001126bool TWPartition::Mount(bool Display_Error) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001127 int exfat_mounted = 0;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001128 unsigned long flags = Mount_Flags;
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001129
Dees_Troy5bf43922012-09-07 16:07:55 -04001130 if (Is_Mounted()) {
1131 return true;
1132 } else if (!Can_Be_Mounted) {
1133 return false;
1134 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001135
1136 Find_Actual_Block_Device();
1137
1138 // Check the current file system before mounting
1139 Check_FS_Type();
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001140 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/exfat-fuse")) {
Dees_Troye34c1332013-02-06 19:13:00 +00001141 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 +00001142 LOGINFO("cmd: %s\n", cmd.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001143 string result;
1144 if (TWFunc::Exec_Cmd(cmd, result) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001145 LOGINFO("exfat-fuse failed to mount with result '%s', trying vfat\n", result.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001146 Current_File_System = "vfat";
1147 } else {
1148#ifdef TW_NO_EXFAT_FUSE
1149 UnMount(false);
1150 // We'll let the kernel handle it but using exfat-fuse to detect if the file system is actually exfat
1151 // Some kernels let us mount vfat as exfat which doesn't work out too well
1152#else
1153 exfat_mounted = 1;
1154#endif
1155 }
1156 }
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001157
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001158 if (Current_File_System == "ntfs" && (TWFunc::Path_Exists("/sbin/ntfs-3g") || TWFunc::Path_Exists("/sbin/mount.ntfs"))) {
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001159 string cmd;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001160 string Ntfsmount_Binary = "";
1161
1162 if (TWFunc::Path_Exists("/sbin/ntfs-3g"))
1163 Ntfsmount_Binary = "ntfs-3g";
1164 else if (TWFunc::Path_Exists("/sbin/mount.ntfs"))
1165 Ntfsmount_Binary = "mount.ntfs";
1166
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001167 if (Mount_Read_Only)
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001168 cmd = "/sbin/" + Ntfsmount_Binary + " -o ro " + Actual_Block_Device + " " + Mount_Point;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001169 else
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001170 cmd = "/sbin/" + Ntfsmount_Binary + " " + Actual_Block_Device + " " + Mount_Point;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001171 LOGINFO("cmd: '%s'\n", cmd.c_str());
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001172
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001173 if (TWFunc::Exec_Cmd(cmd) == 0) {
1174 return true;
1175 } else {
1176 LOGINFO("ntfs-3g failed to mount, trying regular mount method.\n");
1177 }
1178 }
1179
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001180 if (Mount_Read_Only)
1181 flags |= MS_RDONLY;
1182
Dees_Troy22042032012-12-18 21:23:08 +00001183 if (Fstab_File_System == "yaffs2") {
1184 // mount an MTD partition as a YAFFS2 filesystem.
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001185 flags = MS_NOATIME | MS_NODEV | MS_NODIRATIME;
1186 if (Mount_Read_Only)
1187 flags |= MS_RDONLY;
Dees_Troy76543db2013-06-19 16:24:30 +00001188 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags, NULL) < 0) {
1189 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags | MS_RDONLY, NULL) < 0) {
1190 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001191 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy76543db2013-06-19 16:24:30 +00001192 else
1193 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
1194 return false;
1195 } else {
1196 LOGINFO("Mounted '%s' (MTD) as RO\n", Mount_Point.c_str());
1197 return true;
1198 }
1199 } else {
1200 struct stat st;
1201 string test_path = Mount_Point;
1202 if (stat(test_path.c_str(), &st) < 0) {
1203 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001204 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy76543db2013-06-19 16:24:30 +00001205 else
1206 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
1207 return false;
1208 }
1209 mode_t new_mode = st.st_mode | S_IXUSR | S_IXGRP | S_IXOTH;
1210 if (new_mode != st.st_mode) {
1211 LOGINFO("Fixing execute permissions for %s\n", Mount_Point.c_str());
1212 if (chmod(Mount_Point.c_str(), new_mode) < 0) {
1213 if (Display_Error)
1214 LOGERR("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
1215 else
1216 LOGINFO("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
1217 return false;
1218 }
1219 }
Dees_Troy22042032012-12-18 21:23:08 +00001220 return true;
Dees_Troy76543db2013-06-19 16:24:30 +00001221 }
thatc7572eb2015-03-31 18:55:30 +02001222 }
1223
1224 string mount_fs = Current_File_System;
1225 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sys/module/texfat"))
1226 mount_fs = "texfat";
1227
1228 if (!exfat_mounted &&
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001229 mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), mount_fs.c_str(), flags, Mount_Options.c_str()) != 0 &&
1230 mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), mount_fs.c_str(), flags, NULL) != 0) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001231#ifdef TW_NO_EXFAT_FUSE
1232 if (Current_File_System == "exfat") {
Dees_Troy2673cec2013-04-02 20:22:16 +00001233 LOGINFO("Mounting exfat failed, trying vfat...\n");
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001234 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), "vfat", 0, NULL) != 0) {
Dees_Troy85f44ed2013-01-09 18:42:36 +00001235 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001236 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy85f44ed2013-01-09 18:42:36 +00001237 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001238 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001239 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 +00001240 return false;
Dees_Troy85f44ed2013-01-09 18:42:36 +00001241 }
Dees_Troyb05ddee2013-01-28 20:24:50 +00001242 } else {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001243#endif
bigbiff bigbiff26774a02014-03-29 18:22:00 -04001244 if (!Removable && Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001245 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001246 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001247 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
1248 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 +00001249 return false;
1250#ifdef TW_NO_EXFAT_FUSE
Dees_Troy85f44ed2013-01-09 18:42:36 +00001251 }
1252#endif
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001253 }
Ethan Yonker253368a2014-11-25 15:00:52 -06001254
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001255 if (Removable)
1256 Update_Size(Display_Error);
1257
xiaolu9416f4f2015-06-04 08:22:23 +08001258 if (!Symlink_Mount_Point.empty() && TWFunc::Path_Exists(Symlink_Path)) {
codelover2a3d4ce2015-03-14 20:26:49 +08001259 string Command = "mount -o bind '" + Symlink_Path + "' '" + Symlink_Mount_Point + "'";
Vojtech Bocek05534202013-09-11 08:11:56 +02001260 TWFunc::Exec_Cmd(Command);
Dees_Troy5bf43922012-09-07 16:07:55 -04001261 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001262 return true;
1263}
1264
1265bool TWPartition::UnMount(bool Display_Error) {
1266 if (Is_Mounted()) {
1267 int never_unmount_system;
1268
1269 DataManager::GetValue(TW_DONT_UNMOUNT_SYSTEM, never_unmount_system);
1270 if (never_unmount_system == 1 && Mount_Point == "/system")
1271 return true; // Never unmount system if you're not supposed to unmount it
1272
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001273 if (Is_Storage)
Ethan Yonker726a0202014-12-16 20:01:38 -06001274 PartitionManager.Remove_MTP_Storage(MTP_Storage_ID);
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001275
Dees_Troy38bd7602012-09-14 13:33:53 -04001276 if (!Symlink_Mount_Point.empty())
1277 umount(Symlink_Mount_Point.c_str());
1278
Dees_Troyb05ddee2013-01-28 20:24:50 +00001279 umount(Mount_Point.c_str());
1280 if (Is_Mounted()) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001281 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001282 gui_msg(Msg(msg::kError, "fail_unmount=Failed to unmount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy5bf43922012-09-07 16:07:55 -04001283 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001284 LOGINFO("Unable to unmount '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001285 return false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001286 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -04001287 return true;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001288 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001289 } else {
1290 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001291 }
1292}
1293
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05001294bool TWPartition::ReMount(bool Display_Error) {
1295 if (UnMount(Display_Error))
1296 return Mount(Display_Error);
1297 return false;
1298}
1299
1300bool TWPartition::ReMount_RW(bool Display_Error) {
1301 // No need to remount if already mounted rw
1302 if (Is_File_System_Writable())
1303 return true;
1304
1305 bool ro = Mount_Read_Only;
1306 int flags = Mount_Flags;
1307
1308 Mount_Read_Only = false;
1309 Mount_Flags &= ~MS_RDONLY;
1310
1311 bool ret = ReMount(Display_Error);
1312
1313 Mount_Read_Only = ro;
1314 Mount_Flags = flags;
1315
1316 return ret;
1317}
1318
Gary Peck43acadf2012-11-21 21:19:01 -08001319bool TWPartition::Wipe(string New_File_System) {
Ethan Yonker726a0202014-12-16 20:01:38 -06001320 bool wiped = false, update_crypt = false, recreate_media = true;
Dees_Troy16c2b312013-01-15 16:51:18 +00001321 int check;
1322 string Layout_Filename = Mount_Point + "/.layout_version";
1323
Dees_Troy38bd7602012-09-14 13:33:53 -04001324 if (!Can_Be_Wiped) {
Matt Mower3c366972015-12-25 19:28:31 -06001325 gui_msg(Msg(msg::kError, "cannot_wipe=Partition {1} cannot be wiped.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04001326 return false;
1327 }
1328
Dees_Troyc51f1f92012-09-20 15:32:13 -04001329 if (Mount_Point == "/cache")
Dees_Troy2673cec2013-04-02 20:22:16 +00001330 Log_Offset = 0;
Dees_Troyc51f1f92012-09-20 15:32:13 -04001331
Dees_Troy16c2b312013-01-15 16:51:18 +00001332 if (Retain_Layout_Version && Mount(false) && TWFunc::Path_Exists(Layout_Filename))
1333 TWFunc::copy_file(Layout_Filename, "/.layout_version", 0600);
1334 else
1335 unlink("/.layout_version");
Dees_Troy38bd7602012-09-14 13:33:53 -04001336
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001337 if (Has_Data_Media && Current_File_System == New_File_System) {
Dees_Troy16c2b312013-01-15 16:51:18 +00001338 wiped = Wipe_Data_Without_Wiping_Media();
Ethan Yonker5eac2222014-06-11 12:22:55 -05001339 recreate_media = false;
Dees_Troy16c2b312013-01-15 16:51:18 +00001340 } else {
Dees_Troy16c2b312013-01-15 16:51:18 +00001341 DataManager::GetValue(TW_RM_RF_VAR, check);
1342
Hashcodedabfd492013-08-29 22:45:30 -07001343 if (check || Use_Rm_Rf)
Dees_Troy16c2b312013-01-15 16:51:18 +00001344 wiped = Wipe_RMRF();
1345 else if (New_File_System == "ext4")
1346 wiped = Wipe_EXT4();
1347 else if (New_File_System == "ext2" || New_File_System == "ext3")
1348 wiped = Wipe_EXT23(New_File_System);
1349 else if (New_File_System == "vfat")
1350 wiped = Wipe_FAT();
1351 else if (New_File_System == "exfat")
1352 wiped = Wipe_EXFAT();
1353 else if (New_File_System == "yaffs2")
1354 wiped = Wipe_MTD();
Dees_Troye5017042013-08-29 16:38:55 +00001355 else if (New_File_System == "f2fs")
1356 wiped = Wipe_F2FS();
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001357 else if (New_File_System == "ntfs")
1358 wiped = Wipe_NTFS();
Dees_Troy16c2b312013-01-15 16:51:18 +00001359 else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001360 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 +00001361 unlink("/.layout_version");
1362 return false;
1363 }
1364 update_crypt = wiped;
Gary Pecke8bc5d72012-12-21 06:45:25 -08001365 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001366
Gary Pecke8bc5d72012-12-21 06:45:25 -08001367 if (wiped) {
Dees_Troy1c1ac442013-01-17 21:42:14 +00001368 if (Mount_Point == "/cache")
1369 DataManager::Output_Version();
1370
Dees_Troy16c2b312013-01-15 16:51:18 +00001371 if (TWFunc::Path_Exists("/.layout_version") && Mount(false))
1372 TWFunc::copy_file("/.layout_version", Layout_Filename, 0600);
1373
1374 if (update_crypt) {
1375 Setup_File_System(false);
1376 if (Is_Encrypted && !Is_Decrypted) {
1377 // just wiped an encrypted partition back to its unencrypted state
1378 Is_Encrypted = false;
1379 Is_Decrypted = false;
1380 Decrypted_Block_Device = "";
1381 if (Mount_Point == "/data") {
1382 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1383 DataManager::SetValue(TW_IS_DECRYPTED, 0);
1384 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001385 }
1386 }
Ethan Yonker5eac2222014-06-11 12:22:55 -05001387
Ethan Yonker66a19492015-12-10 10:19:45 -06001388 if (Has_Data_Media && recreate_media) {
Ethan Yonker5eac2222014-06-11 12:22:55 -05001389 Recreate_Media_Folder();
1390 }
Matt Mower209c9632016-01-20 12:08:35 -06001391 if (Is_Storage && Mount(false))
1392 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Gary Pecke8bc5d72012-12-21 06:45:25 -08001393 }
Matt Mower209c9632016-01-20 12:08:35 -06001394
Gary Pecke8bc5d72012-12-21 06:45:25 -08001395 return wiped;
Dees_Troy51a0e822012-09-05 15:24:24 -04001396}
1397
Gary Peck43acadf2012-11-21 21:19:01 -08001398bool TWPartition::Wipe() {
Gary Peck82599a82012-11-21 16:23:12 -08001399 if (Is_File_System(Current_File_System))
1400 return Wipe(Current_File_System);
1401 else
1402 return Wipe(Fstab_File_System);
Gary Peck43acadf2012-11-21 21:19:01 -08001403}
1404
Dees_Troye58d5262012-09-21 12:27:57 -04001405bool TWPartition::Wipe_AndSec(void) {
1406 if (!Has_Android_Secure)
1407 return false;
1408
Dees_Troye58d5262012-09-21 12:27:57 -04001409 if (!Mount(true))
1410 return false;
1411
Ethan Yonker74db1572015-10-28 12:44:49 -05001412 gui_msg(Msg("wiping=Wiping {1}")(Backup_Display_Name));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001413 TWFunc::removeDir(Mount_Point + "/.android_secure/", true);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001414 return true;
Dees_Troye58d5262012-09-21 12:27:57 -04001415}
1416
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001417bool TWPartition::Can_Repair() {
Ethan Yonkera2719152015-05-28 09:44:41 -05001418 if (Mount_Read_Only)
1419 return false;
Matt Mower18794c82015-11-11 16:22:45 -06001420 if (Current_File_System == "vfat" && TWFunc::Path_Exists("/sbin/fsck.fat"))
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001421 return true;
1422 else if ((Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") && TWFunc::Path_Exists("/sbin/e2fsck"))
1423 return true;
1424 else if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/fsck.exfat"))
1425 return true;
1426 else if (Current_File_System == "f2fs" && TWFunc::Path_Exists("/sbin/fsck.f2fs"))
1427 return true;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001428 else if (Current_File_System == "ntfs" && (TWFunc::Path_Exists("/sbin/ntfsfix") || TWFunc::Path_Exists("/sbin/fsck.ntfs")))
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001429 return true;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001430 return false;
1431}
1432
1433bool TWPartition::Repair() {
1434 string command;
1435
1436 if (Current_File_System == "vfat") {
Matt Mower18794c82015-11-11 16:22:45 -06001437 if (!TWFunc::Path_Exists("/sbin/fsck.fat")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001438 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.fat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001439 return false;
1440 }
1441 if (!UnMount(true))
1442 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001443 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.fat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001444 Find_Actual_Block_Device();
Matt Mower18794c82015-11-11 16:22:45 -06001445 command = "/sbin/fsck.fat -y " + Actual_Block_Device;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001446 LOGINFO("Repair command: %s\n", command.c_str());
1447 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001448 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001449 return true;
1450 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001451 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001452 return false;
1453 }
1454 }
1455 if (Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") {
1456 if (!TWFunc::Path_Exists("/sbin/e2fsck")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001457 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("e2fsck"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001458 return false;
1459 }
1460 if (!UnMount(true))
1461 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001462 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("e2fsck"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001463 Find_Actual_Block_Device();
Ethan Yonkera2719152015-05-28 09:44:41 -05001464 command = "/sbin/e2fsck -fp " + Actual_Block_Device;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001465 LOGINFO("Repair command: %s\n", command.c_str());
1466 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001467 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001468 return true;
1469 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001470 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001471 return false;
1472 }
1473 }
1474 if (Current_File_System == "exfat") {
1475 if (!TWFunc::Path_Exists("/sbin/fsck.exfat")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001476 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.exfat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001477 return false;
1478 }
1479 if (!UnMount(true))
1480 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001481 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.exfat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001482 Find_Actual_Block_Device();
1483 command = "/sbin/fsck.exfat " + Actual_Block_Device;
1484 LOGINFO("Repair command: %s\n", command.c_str());
1485 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001486 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001487 return true;
1488 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001489 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001490 return false;
1491 }
1492 }
1493 if (Current_File_System == "f2fs") {
1494 if (!TWFunc::Path_Exists("/sbin/fsck.f2fs")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001495 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.f2fs"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001496 return false;
1497 }
1498 if (!UnMount(true))
1499 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001500 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.f2fs"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001501 Find_Actual_Block_Device();
1502 command = "/sbin/fsck.f2fs " + Actual_Block_Device;
1503 LOGINFO("Repair command: %s\n", command.c_str());
1504 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001505 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001506 return true;
1507 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001508 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001509 return false;
1510 }
1511 }
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001512 if (Current_File_System == "ntfs") {
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001513 string Ntfsfix_Binary;
1514 if (TWFunc::Path_Exists("/sbin/ntfsfix"))
1515 Ntfsfix_Binary = "ntfsfix";
1516 else if (TWFunc::Path_Exists("/sbin/fsck.ntfs"))
1517 Ntfsfix_Binary = "fsck.ntfs";
1518 else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001519 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("ntfsfix"));
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001520 return false;
1521 }
1522 if (!UnMount(true))
1523 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001524 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)(Ntfsfix_Binary));
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001525 Find_Actual_Block_Device();
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001526 command = "/sbin/" + Ntfsfix_Binary + " " + Actual_Block_Device;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001527 LOGINFO("Repair command: %s\n", command.c_str());
1528 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001529 gui_msg("done=Done.");
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001530 return true;
1531 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001532 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001533 return false;
1534 }
1535 }
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001536 return false;
1537}
1538
Ethan Yonkera2719152015-05-28 09:44:41 -05001539bool TWPartition::Can_Resize() {
1540 if (Mount_Read_Only)
1541 return false;
1542 if ((Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") && TWFunc::Path_Exists("/sbin/resize2fs"))
1543 return true;
1544 return false;
1545}
1546
1547bool TWPartition::Resize() {
1548 string command;
1549
1550 if (Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") {
1551 if (!Can_Repair()) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001552 LOGINFO("Cannot resize %s because %s cannot be repaired before resizing.\n", Display_Name.c_str(), Display_Name.c_str());
1553 gui_msg(Msg(msg::kError, "cannot_resize=Cannot resize {1}.")(Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001554 return false;
1555 }
1556 if (!TWFunc::Path_Exists("/sbin/resize2fs")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001557 LOGINFO("resize2fs does not exist! Cannot resize!\n");
1558 gui_msg(Msg(msg::kError, "cannot_resize=Cannot resize {1}.")(Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001559 return false;
1560 }
1561 // Repair will unmount so no need to do it twice
Ethan Yonker74db1572015-10-28 12:44:49 -05001562 gui_msg(Msg("repair_resize=Repairing {1} before resizing.")( Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001563 if (!Repair())
1564 return false;
Ethan Yonker74db1572015-10-28 12:44:49 -05001565 gui_msg(Msg("resizing=Resizing {1} using {2}...")(Display_Name)("resize2fs"));
Ethan Yonkera2719152015-05-28 09:44:41 -05001566 Find_Actual_Block_Device();
1567 command = "/sbin/resize2fs " + Actual_Block_Device;
1568 if (Length != 0) {
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001569 unsigned long long Actual_Size = IOCTL_Get_Block_Size();
1570 if (Actual_Size == 0)
1571 return false;
Ethan Yonkera2719152015-05-28 09:44:41 -05001572
Ethan Yonkera2719152015-05-28 09:44:41 -05001573 unsigned long long Block_Count;
1574 if (Length < 0) {
1575 // Reduce overall size by this length
1576 Block_Count = (Actual_Size / 1024LLU) - ((unsigned long long)(Length * -1) / 1024LLU);
1577 } else {
1578 // This is the size, not a size reduction
1579 Block_Count = ((unsigned long long)(Length) / 1024LLU);
1580 }
1581 char temp[256];
1582 sprintf(temp, "%llu", Block_Count);
1583 command += " ";
1584 command += temp;
1585 command += "K";
1586 }
1587 LOGINFO("Resize command: %s\n", command.c_str());
1588 if (TWFunc::Exec_Cmd(command) == 0) {
1589 Update_Size(true);
Ethan Yonker74db1572015-10-28 12:44:49 -05001590 gui_msg("done=Done.");
Ethan Yonkera2719152015-05-28 09:44:41 -05001591 return true;
1592 } else {
1593 Update_Size(true);
Ethan Yonker74db1572015-10-28 12:44:49 -05001594 gui_msg(Msg(msg::kError, "unable_resize=Unable to resize {1}.")(Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001595 return false;
1596 }
1597 }
1598 return false;
1599}
1600
Ethan Yonker472f5062016-02-25 13:47:30 -06001601bool TWPartition::Backup(const string& backup_folder, pid_t &tar_fork_pid, ProgressTracking *progress) {
bigbiff7abc5fe2015-01-17 16:53:12 -05001602 if (Backup_Method == FILES) {
Ethan Yonker472f5062016-02-25 13:47:30 -06001603 return Backup_Tar(backup_folder, progress, tar_fork_pid);
bigbiff7abc5fe2015-01-17 16:53:12 -05001604 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001605 else if (Backup_Method == DD)
Ethan Yonker472f5062016-02-25 13:47:30 -06001606 return Backup_Image(backup_folder, progress);
Dees_Troy38bd7602012-09-14 13:33:53 -04001607 else if (Backup_Method == FLASH_UTILS)
Ethan Yonker472f5062016-02-25 13:47:30 -06001608 return Backup_Dump_Image(backup_folder, progress);
Dees_Troy2673cec2013-04-02 20:22:16 +00001609 LOGERR("Unknown backup method for '%s'\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001610 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001611}
1612
Dees_Troy43d8b002012-09-17 16:00:01 -04001613bool TWPartition::Check_MD5(string restore_folder) {
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001614 string Full_Filename, md5file;
Dees_Troy43d8b002012-09-17 16:00:01 -04001615 char split_filename[512];
1616 int index = 0;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001617 twrpDigest md5sum;
Dees_Troy43d8b002012-09-17 16:00:01 -04001618
Captain Throwbackff5935f2014-09-23 16:08:34 -04001619 sync();
1620
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001621 memset(split_filename, 0, sizeof(split_filename));
Dees_Troy43d8b002012-09-17 16:00:01 -04001622 Full_Filename = restore_folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04001623 if (!TWFunc::Path_Exists(Full_Filename)) {
1624 // This is a split archive, we presume
1625 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
Dees_Troy2673cec2013-04-02 20:22:16 +00001626 LOGINFO("split_filename: %s\n", split_filename);
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001627 md5file = split_filename;
1628 md5file += ".md5";
1629 if (!TWFunc::Path_Exists(md5file)) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001630 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 -04001631 return false;
1632 }
1633 md5sum.setfn(split_filename);
Dees_Troy83bd4832013-05-04 12:39:56 +00001634 while (index < 1000) {
1635 if (TWFunc::Path_Exists(split_filename) && md5sum.verify_md5digest() != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001636 gui_msg(Msg(msg::kError, "md5_fail_match=MD5 failed to match on '{1}'.")(split_filename));
Dees_Troy43d8b002012-09-17 16:00:01 -04001637 return false;
1638 }
1639 index++;
1640 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001641 md5sum.setfn(split_filename);
Dees_Troy43d8b002012-09-17 16:00:01 -04001642 }
Dees_Troy4a2a1262012-09-18 09:33:47 -04001643 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001644 } else {
1645 // Single file archive
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001646 md5file = Full_Filename + ".md5";
1647 if (!TWFunc::Path_Exists(md5file)) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001648 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 -04001649 return false;
1650 }
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001651 md5sum.setfn(Full_Filename);
1652 if (md5sum.verify_md5digest() != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001653 gui_msg(Msg(msg::kError, "md5_fail_match=MD5 failed to match on '{1}'.")(split_filename));
Dees_Troy43d8b002012-09-17 16:00:01 -04001654 return false;
1655 } else
1656 return true;
1657 }
1658 return false;
1659}
1660
Ethan Yonker472f5062016-02-25 13:47:30 -06001661bool TWPartition::Restore(const string& restore_folder, ProgressTracking *progress) {
Gary Peck43acadf2012-11-21 21:19:01 -08001662 string Restore_File_System;
1663
Matt Mower3c366972015-12-25 19:28:31 -06001664 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, gui_parse_text("{@restoring_hdr}"));
Dees_Troy2673cec2013-04-02 20:22:16 +00001665 LOGINFO("Restore filename is: %s\n", Backup_FileName.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001666
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001667 Restore_File_System = Get_Restore_File_System(restore_folder);
1668
1669 if (Is_File_System(Restore_File_System))
Ethan Yonker472f5062016-02-25 13:47:30 -06001670 return Restore_Tar(restore_folder, Restore_File_System, progress);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001671 else if (Is_Image(Restore_File_System)) {
Ethan Yonker472f5062016-02-25 13:47:30 -06001672 return Restore_Image(restore_folder, Restore_File_System, progress);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001673 }
1674
1675 LOGERR("Unknown restore method for '%s'\n", Mount_Point.c_str());
1676 return false;
1677}
1678
Ethan Yonker472f5062016-02-25 13:47:30 -06001679string TWPartition::Get_Restore_File_System(const string& restore_folder) {
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001680 size_t first_period, second_period;
1681 string Restore_File_System;
1682
Gary Peck43acadf2012-11-21 21:19:01 -08001683 // Parse backup filename to extract the file system before wiping
1684 first_period = Backup_FileName.find(".");
1685 if (first_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001686 LOGERR("Unable to find file system (first period).\n");
thatd43bf2d2014-09-21 23:13:02 +02001687 return string();
Gary Peck43acadf2012-11-21 21:19:01 -08001688 }
1689 Restore_File_System = Backup_FileName.substr(first_period + 1, Backup_FileName.size() - first_period - 1);
1690 second_period = Restore_File_System.find(".");
1691 if (second_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001692 LOGERR("Unable to find file system (second period).\n");
thatd43bf2d2014-09-21 23:13:02 +02001693 return string();
Gary Peck43acadf2012-11-21 21:19:01 -08001694 }
1695 Restore_File_System.resize(second_period);
Dees_Troy2673cec2013-04-02 20:22:16 +00001696 LOGINFO("Restore file system is: '%s'.\n", Restore_File_System.c_str());
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001697 return Restore_File_System;
Dees_Troy51a0e822012-09-05 15:24:24 -04001698}
1699
1700string TWPartition::Backup_Method_By_Name() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001701 if (Backup_Method == NONE)
1702 return "none";
1703 else if (Backup_Method == FILES)
1704 return "files";
1705 else if (Backup_Method == DD)
1706 return "dd";
1707 else if (Backup_Method == FLASH_UTILS)
1708 return "flash_utils";
1709 else
1710 return "undefined";
1711 return "ERROR!";
Dees_Troy51a0e822012-09-05 15:24:24 -04001712}
1713
1714bool TWPartition::Decrypt(string Password) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001715 LOGINFO("STUB TWPartition::Decrypt, password: '%s'\n", Password.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001716 // Is this needed?
Dees_Troy51a0e822012-09-05 15:24:24 -04001717 return 1;
1718}
1719
1720bool TWPartition::Wipe_Encryption() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001721 bool Save_Data_Media = Has_Data_Media;
1722
1723 if (!UnMount(true))
1724 return false;
1725
Dees_Troy38bd7602012-09-14 13:33:53 -04001726 Has_Data_Media = false;
Dees_Troy74fb2e92013-04-15 14:35:47 +00001727 Decrypted_Block_Device = "";
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001728#ifdef TW_INCLUDE_CRYPTO
1729 if (Is_Decrypted) {
1730 if (!UnMount(true))
1731 return false;
Matt Mower2b18a532015-02-20 16:58:05 -06001732 if (delete_crypto_blk_dev((char*)("userdata")) != 0) {
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001733 LOGERR("Error deleting crypto block device, continuing anyway.\n");
1734 }
1735 }
1736#endif
Dees_Troy74fb2e92013-04-15 14:35:47 +00001737 Is_Decrypted = false;
1738 Is_Encrypted = false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001739 Find_Actual_Block_Device();
Ethan Yonkerbc85b632015-08-09 12:48:14 -05001740 if (Crypto_Key_Location == "footer") {
1741 int newlen, fd;
1742 if (Length != 0) {
1743 newlen = Length;
1744 if (newlen < 0)
1745 newlen = newlen * -1;
1746 }