blob: 3f2374a47d02a10c7f971a8fa8ba7fb3be2914e8 [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"
bigbiff bigbiff34684ff2013-12-01 21:03:45 -050046#include "twrpDU.hpp"
Ethan Yonker1b7a31b2014-07-03 15:09:22 -050047#include "infomanager.hpp"
Ethan Yonker4b94cfd2014-12-11 10:00:45 -060048#include "set_metadata.h"
Ethan Yonker74db1572015-10-28 12:44:49 -050049#include "gui/gui.hpp"
bigbiffce8f83c2015-12-12 18:30:21 -050050#include "adbbu/libtwadbbu.hpp"
Dees_Troy5bf43922012-09-07 16:07:55 -040051extern "C" {
Dees_Troy38bd7602012-09-14 13:33:53 -040052 #include "mtdutils/mtdutils.h"
53 #include "mtdutils/mounts.h"
Dees_Troya95f55c2013-08-17 13:14:43 +000054#ifdef USE_EXT4
55 #include "make_ext4fs.h"
56#endif
Ethan Yonker71413f42014-02-26 13:36:08 -060057
58#ifdef TW_INCLUDE_CRYPTO
Ethan Yonker253368a2014-11-25 15:00:52 -060059 #include "crypto/lollipop/cryptfs.h"
Ethan Yonker66a19492015-12-10 10:19:45 -060060 #include "gpt/gpt.h"
Ethan Yonkerbc85b632015-08-09 12:48:14 -050061#else
62 #define CRYPT_FOOTER_OFFSET 0x4000
Ethan Yonker71413f42014-02-26 13:36:08 -060063#endif
Dees_Troy5bf43922012-09-07 16:07:55 -040064}
bigbiff bigbiffc49d7062013-10-11 20:28:00 -040065#ifdef HAVE_SELINUX
66#include "selinux/selinux.h"
Ethan Yonkerf27497f2014-02-09 11:48:33 -060067#include <selinux/label.h>
bigbiff bigbiffc49d7062013-10-11 20:28:00 -040068#endif
Dees Troy4159aed2014-02-28 17:24:43 +000069#ifdef HAVE_CAPABILITIES
70#include <sys/capability.h>
71#include <sys/xattr.h>
72#include <linux/xattr.h>
73#endif
HashBanged974bb2016-01-30 14:20:09 -050074#include <sparse_format.h>
Ethan Yonker472f5062016-02-25 13:47:30 -060075#include "progresstracking.hpp"
Dees_Troy51a0e822012-09-05 15:24:24 -040076
bigbiff bigbiff9c754052013-01-09 09:09:08 -050077using namespace std;
78
Dees_Troya95f55c2013-08-17 13:14:43 +000079extern struct selabel_handle *selinux_handle;
Ethan Yonker6277c792014-09-15 14:54:30 -050080extern bool datamedia;
Dees_Troya95f55c2013-08-17 13:14:43 +000081
Hashcode62bd9e02013-11-19 21:59:42 -080082struct flag_list {
83 const char *name;
84 unsigned flag;
85};
86
Matt Mower4ab42b12016-04-21 13:52:18 -050087const struct flag_list mount_flags[] = {
Hashcode62bd9e02013-11-19 21:59:42 -080088 { "noatime", MS_NOATIME },
89 { "noexec", MS_NOEXEC },
90 { "nosuid", MS_NOSUID },
91 { "nodev", MS_NODEV },
92 { "nodiratime", MS_NODIRATIME },
93 { "ro", MS_RDONLY },
94 { "rw", 0 },
95 { "remount", MS_REMOUNT },
96 { "bind", MS_BIND },
97 { "rec", MS_REC },
Dees Troyc4bc30e2014-02-03 15:04:19 +000098#ifdef MS_UNBINDABLE
Hashcode62bd9e02013-11-19 21:59:42 -080099 { "unbindable", MS_UNBINDABLE },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000100#endif
101#ifdef MS_PRIVATE
Hashcode62bd9e02013-11-19 21:59:42 -0800102 { "private", MS_PRIVATE },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000103#endif
104#ifdef MS_SLAVE
Hashcode62bd9e02013-11-19 21:59:42 -0800105 { "slave", MS_SLAVE },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000106#endif
107#ifdef MS_SHARED
Hashcode62bd9e02013-11-19 21:59:42 -0800108 { "shared", MS_SHARED },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000109#endif
Hashcode62bd9e02013-11-19 21:59:42 -0800110 { "sync", MS_SYNCHRONOUS },
111 { "defaults", 0 },
112 { 0, 0 },
113};
114
Matt Mower2416a502016-04-12 19:54:46 -0500115enum TW_FSTAB_FLAGS {
116 TWFLAG_DEFAULTS, // Retain position
117 TWFLAG_ANDSEC,
118 TWFLAG_BACKUP,
119 TWFLAG_BACKUPNAME,
120 TWFLAG_BLOCKSIZE,
121 TWFLAG_CANBEWIPED,
122 TWFLAG_CANENCRYPTBACKUP,
123 TWFLAG_DISPLAY,
124 TWFLAG_ENCRYPTABLE,
125 TWFLAG_FLASHIMG,
126 TWFLAG_FORCEENCRYPT,
127 TWFLAG_FSFLAGS,
128 TWFLAG_IGNOREBLKID,
129 TWFLAG_LENGTH,
130 TWFLAG_MOUNTTODECRYPT,
131 TWFLAG_REMOVABLE,
132 TWFLAG_RETAINLAYOUTVERSION,
133 TWFLAG_SETTINGSSTORAGE,
134 TWFLAG_STORAGE,
135 TWFLAG_STORAGENAME,
136 TWFLAG_SUBPARTITIONOF,
137 TWFLAG_SYMLINK,
138 TWFLAG_USERDATAENCRYPTBACKUP,
139 TWFLAG_USERMRF,
140 TWFLAG_WIPEDURINGFACTORYRESET,
141 TWFLAG_WIPEINGUI,
142};
143
144/* Flags without a trailing '=' are considered dual format flags and can be
145 * written as either 'flagname' or 'flagname=', where the character following
146 * the '=' is Y,y,1 for true and false otherwise.
147 */
148const struct flag_list tw_flags[] = {
149 { "andsec", TWFLAG_ANDSEC },
150 { "backup", TWFLAG_BACKUP },
151 { "backupname=", TWFLAG_BACKUPNAME },
152 { "blocksize=", TWFLAG_BLOCKSIZE },
153 { "canbewiped", TWFLAG_CANBEWIPED },
154 { "canencryptbackup", TWFLAG_CANENCRYPTBACKUP },
155 { "defaults", TWFLAG_DEFAULTS },
156 { "display=", TWFLAG_DISPLAY },
157 { "encryptable=", TWFLAG_ENCRYPTABLE },
158 { "flashimg", TWFLAG_FLASHIMG },
159 { "forceencrypt=", TWFLAG_FORCEENCRYPT },
160 { "fsflags=", TWFLAG_FSFLAGS },
161 { "ignoreblkid", TWFLAG_IGNOREBLKID },
162 { "length=", TWFLAG_LENGTH },
163 { "mounttodecrypt", TWFLAG_MOUNTTODECRYPT },
164 { "removable", TWFLAG_REMOVABLE },
165 { "retainlayoutversion", TWFLAG_RETAINLAYOUTVERSION },
166 { "settingsstorage", TWFLAG_SETTINGSSTORAGE },
167 { "storage", TWFLAG_STORAGE },
168 { "storagename=", TWFLAG_STORAGENAME },
169 { "subpartitionof=", TWFLAG_SUBPARTITIONOF },
170 { "symlink=", TWFLAG_SYMLINK },
171 { "userdataencryptbackup", TWFLAG_USERDATAENCRYPTBACKUP },
172 { "usermrf", TWFLAG_USERMRF },
173 { "wipeduringfactoryreset", TWFLAG_WIPEDURINGFACTORYRESET },
174 { "wipeingui", TWFLAG_WIPEINGUI },
175 { 0, 0 },
176};
177
that9e0593e2014-10-08 00:01:24 +0200178TWPartition::TWPartition() {
Dees_Troy51a0e822012-09-05 15:24:24 -0400179 Can_Be_Mounted = false;
180 Can_Be_Wiped = false;
Dees_Troya13d74f2013-03-24 08:54:55 -0500181 Can_Be_Backed_Up = false;
Vojtech Bocek1dc30982013-08-30 21:49:30 +0200182 Use_Rm_Rf = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400183 Wipe_During_Factory_Reset = false;
184 Wipe_Available_in_GUI = false;
185 Is_SubPartition = false;
Dees_Troy2691f9d2012-09-24 11:15:49 -0400186 Has_SubPartition = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400187 SubPartition_Of = "";
188 Symlink_Path = "";
189 Symlink_Mount_Point = "";
190 Mount_Point = "";
Dees_Troye58d5262012-09-21 12:27:57 -0400191 Backup_Path = "";
Dees_Troy38bd7602012-09-14 13:33:53 -0400192 Actual_Block_Device = "";
193 Primary_Block_Device = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400194 Alternate_Block_Device = "";
195 Removable = false;
196 Is_Present = false;
197 Length = 0;
198 Size = 0;
199 Used = 0;
200 Free = 0;
201 Backup_Size = 0;
202 Can_Be_Encrypted = false;
203 Is_Encrypted = false;
204 Is_Decrypted = false;
Ethan Yonker253368a2014-11-25 15:00:52 -0600205 Mount_To_Decrypt = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400206 Decrypted_Block_Device = "";
207 Display_Name = "";
Dees_Troya13d74f2013-03-24 08:54:55 -0500208 Backup_Display_Name = "";
209 Storage_Name = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400210 Backup_Name = "";
Dees_Troy63c8df72012-09-10 14:02:05 -0400211 Backup_FileName = "";
Dees_Troy38bd7602012-09-14 13:33:53 -0400212 MTD_Name = "";
bigbiffce8f83c2015-12-12 18:30:21 -0500213 Backup_Method = BM_NONE;
Dees_Troy83bd4832013-05-04 12:39:56 +0000214 Can_Encrypt_Backup = false;
215 Use_Userdata_Encryption = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400216 Has_Data_Media = false;
Dees_Troye58d5262012-09-21 12:27:57 -0400217 Has_Android_Secure = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400218 Is_Storage = false;
Dees_Troya13d74f2013-03-24 08:54:55 -0500219 Is_Settings_Storage = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400220 Storage_Path = "";
221 Current_File_System = "";
222 Fstab_File_System = "";
Hashcode62bd9e02013-11-19 21:59:42 -0800223 Mount_Flags = 0;
224 Mount_Options = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400225 Format_Block_Size = 0;
Dees_Troy68cab492012-12-12 19:29:35 +0000226 Ignore_Blkid = false;
Dees_Troy16c2b312013-01-15 16:51:18 +0000227 Retain_Layout_Version = false;
Ethan Yonker253368a2014-11-25 15:00:52 -0600228 Crypto_Key_Location = "footer";
Ethan Yonker726a0202014-12-16 20:01:38 -0600229 MTP_Storage_ID = 0;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600230 Can_Flash_Img = false;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500231 Mount_Read_Only = false;
Ethan Yonker66a19492015-12-10 10:19:45 -0600232 Is_Adopted_Storage = false;
233 Adopted_GUID = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400234}
235
236TWPartition::~TWPartition(void) {
237 // Do nothing
238}
239
Matt Mower2b2dd152016-04-26 11:24:08 -0500240bool TWPartition::Process_Fstab_Line(const char *fstab_line, bool Display_Error) {
241 char full_line[MAX_FSTAB_LINE_LENGTH];
Matt Mower2416a502016-04-12 19:54:46 -0500242 char twflags[MAX_FSTAB_LINE_LENGTH] = "";
Dees_Troy5bf43922012-09-07 16:07:55 -0400243 char* ptr;
Matt Mower2b2dd152016-04-26 11:24:08 -0500244 int line_len = strlen(fstab_line), index = 0, item_index = 0;
Dees_Troya13d74f2013-03-24 08:54:55 -0500245 bool skip = false;
Dees_Troy5bf43922012-09-07 16:07:55 -0400246
Matt Mower2b2dd152016-04-26 11:24:08 -0500247 strlcpy(full_line, fstab_line, sizeof(full_line));
Dees_Troy51127312012-09-08 13:08:49 -0400248 for (index = 0; index < line_len; index++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500249 if (full_line[index] == 34)
250 skip = !skip;
251 if (!skip && full_line[index] <= 32)
Dees_Troy5bf43922012-09-07 16:07:55 -0400252 full_line[index] = '\0';
Dees_Troy5bf43922012-09-07 16:07:55 -0400253 }
Dees_Troy7c2dec82012-09-26 09:49:14 -0400254 Mount_Point = full_line;
Dees_Troy2673cec2013-04-02 20:22:16 +0000255 LOGINFO("Processing '%s'\n", Mount_Point.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -0400256 Backup_Path = Mount_Point;
Dees_Troya13d74f2013-03-24 08:54:55 -0500257 Storage_Path = Mount_Point;
Dees_Troy70737fa2013-04-08 13:19:20 +0000258 Display_Name = full_line + 1;
259 Backup_Display_Name = Display_Name;
260 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400261 index = Mount_Point.size();
262 while (index < line_len) {
263 while (index < line_len && full_line[index] == '\0')
264 index++;
265 if (index >= line_len)
266 continue;
267 ptr = full_line + index;
268 if (item_index == 0) {
269 // File System
270 Fstab_File_System = ptr;
271 Current_File_System = ptr;
272 item_index++;
273 } else if (item_index == 1) {
274 // Primary Block Device
Dees_Troy38bd7602012-09-14 13:33:53 -0400275 if (Fstab_File_System == "mtd" || Fstab_File_System == "yaffs2") {
Dees_Troy094207a2012-09-26 12:00:39 -0400276 MTD_Name = ptr;
277 Find_MTD_Block_Device(MTD_Name);
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400278 } else if (Fstab_File_System == "bml") {
279 if (Mount_Point == "/boot")
280 MTD_Name = "boot";
281 else if (Mount_Point == "/recovery")
282 MTD_Name = "recovery";
283 Primary_Block_Device = ptr;
284 if (*ptr != '/')
Dees_Troy2673cec2013-04-02 20:22:16 +0000285 LOGERR("Until we get better BML support, you will have to find and provide the full block device path to the BML devices e.g. /dev/block/bml9 instead of the partition name\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400286 } else if (*ptr != '/') {
Dees_Troy5bf43922012-09-07 16:07:55 -0400287 if (Display_Error)
Matt Mower2b2dd152016-04-26 11:24:08 -0500288 LOGERR("Invalid block device '%s' in fstab line '%s'", ptr, fstab_line);
Dees_Troy5bf43922012-09-07 16:07:55 -0400289 else
Matt Mower2b2dd152016-04-26 11:24:08 -0500290 LOGINFO("Invalid block device '%s' in fstab line '%s'", ptr, fstab_line);
Matt Mower72c87ce2016-04-26 14:34:56 -0500291 return false;
Dees_Troy38bd7602012-09-14 13:33:53 -0400292 } else {
293 Primary_Block_Device = ptr;
294 Find_Real_Block_Device(Primary_Block_Device, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400295 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400296 item_index++;
297 } else if (item_index > 1) {
298 if (*ptr == '/') {
299 // Alternate Block Device
300 Alternate_Block_Device = ptr;
301 Find_Real_Block_Device(Alternate_Block_Device, Display_Error);
302 } else if (strlen(ptr) > 7 && strncmp(ptr, "length=", 7) == 0) {
303 // Partition length
304 ptr += 7;
305 Length = atoi(ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400306 } else if (strlen(ptr) > 6 && strncmp(ptr, "flags=", 6) == 0) {
307 // Custom flags, save for later so that new values aren't overwritten by defaults
308 ptr += 6;
Matt Mower2416a502016-04-12 19:54:46 -0500309 strlcpy(twflags, ptr, sizeof(twflags));
Dees_Troy38bd7602012-09-14 13:33:53 -0400310 } else if (strlen(ptr) == 4 && (strncmp(ptr, "NULL", 4) == 0 || strncmp(ptr, "null", 4) == 0 || strncmp(ptr, "null", 4) == 0)) {
311 // Do nothing
Dees_Troy5bf43922012-09-07 16:07:55 -0400312 } else {
313 // Unhandled data
Matt Mower2b2dd152016-04-26 11:24:08 -0500314 LOGINFO("Unhandled fstab information '%s' in fstab line '%s'\n", ptr, fstab_line);
Dees_Troy5bf43922012-09-07 16:07:55 -0400315 }
316 }
317 while (index < line_len && full_line[index] != '\0')
318 index++;
319 }
320
321 if (!Is_File_System(Fstab_File_System) && !Is_Image(Fstab_File_System)) {
322 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000323 LOGERR("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400324 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000325 LOGINFO("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Matt Mower72c87ce2016-04-26 14:34:56 -0500326 return false;
Dees_Troy5bf43922012-09-07 16:07:55 -0400327 } else if (Is_File_System(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400328 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400329 Setup_File_System(Display_Error);
330 if (Mount_Point == "/system") {
331 Display_Name = "System";
Dees_Troya13d74f2013-03-24 08:54:55 -0500332 Backup_Display_Name = Display_Name;
333 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400334 Wipe_Available_in_GUI = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500335 Can_Be_Backed_Up = true;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500336 Mount_Read_Only = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400337 } else if (Mount_Point == "/data") {
338 Display_Name = "Data";
Dees_Troya13d74f2013-03-24 08:54:55 -0500339 Backup_Display_Name = Display_Name;
340 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400341 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400342 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500343 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000344 Can_Encrypt_Backup = true;
345 Use_Userdata_Encryption = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400346 } else if (Mount_Point == "/cache") {
347 Display_Name = "Cache";
Dees_Troya13d74f2013-03-24 08:54:55 -0500348 Backup_Display_Name = Display_Name;
349 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400350 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400351 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500352 Can_Be_Backed_Up = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400353 } else if (Mount_Point == "/datadata") {
Dees_Troy51127312012-09-08 13:08:49 -0400354 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400355 Display_Name = "DataData";
Dees_Troya13d74f2013-03-24 08:54:55 -0500356 Backup_Display_Name = Display_Name;
357 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400358 Is_SubPartition = true;
359 SubPartition_Of = "/data";
Dees_Troy5bf43922012-09-07 16:07:55 -0400360 DataManager::SetValue(TW_HAS_DATADATA, 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500361 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000362 Can_Encrypt_Backup = true;
363 Use_Userdata_Encryption = false; // This whole partition should be encrypted
Dees_Troy5bf43922012-09-07 16:07:55 -0400364 } else if (Mount_Point == "/sd-ext") {
Dees_Troy51127312012-09-08 13:08:49 -0400365 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400366 Display_Name = "SD-Ext";
Dees_Troya13d74f2013-03-24 08:54:55 -0500367 Backup_Display_Name = Display_Name;
368 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400369 Wipe_Available_in_GUI = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400370 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500371 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000372 Can_Encrypt_Backup = true;
373 Use_Userdata_Encryption = true;
Dees_Troy2c50e182012-09-26 20:05:28 -0400374 } else if (Mount_Point == "/boot") {
375 Display_Name = "Boot";
Dees_Troya13d74f2013-03-24 08:54:55 -0500376 Backup_Display_Name = Display_Name;
Dees_Troy2c50e182012-09-26 20:05:28 -0400377 DataManager::SetValue("tw_boot_is_mountable", 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500378 Can_Be_Backed_Up = true;
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500379 } else if (Mount_Point == "/vendor") {
380 Display_Name = "Vendor";
381 Backup_Display_Name = Display_Name;
382 Storage_Name = Display_Name;
383 Mount_Read_Only = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400384 }
385#ifdef TW_EXTERNAL_STORAGE_PATH
386 if (Mount_Point == EXPAND(TW_EXTERNAL_STORAGE_PATH)) {
387 Is_Storage = true;
388 Storage_Path = EXPAND(TW_EXTERNAL_STORAGE_PATH);
Dees_Troyc51f1f92012-09-20 15:32:13 -0400389 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500390 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400391#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000392 if (Mount_Point == "/sdcard" || Mount_Point == "/external_sd" || Mount_Point == "/external_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400393 Is_Storage = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400394 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500395 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400396#endif
Dees_Troyb05ddee2013-01-28 20:24:50 +0000397 }
Dees_Troy8170a922012-09-18 15:40:25 -0400398#ifdef TW_INTERNAL_STORAGE_PATH
399 if (Mount_Point == EXPAND(TW_INTERNAL_STORAGE_PATH)) {
400 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500401 Is_Settings_Storage = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400402 Storage_Path = EXPAND(TW_INTERNAL_STORAGE_PATH);
Dees_Troya13d74f2013-03-24 08:54:55 -0500403 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400404 }
405#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000406 if (Mount_Point == "/emmc" || Mount_Point == "/internal_sd" || Mount_Point == "/internal_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400407 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500408 Is_Settings_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500409 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400410 }
411#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400412 } else if (Is_Image(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400413 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400414 Setup_Image(Display_Error);
Dees_Troya13d74f2013-03-24 08:54:55 -0500415 if (Mount_Point == "/boot") {
416 Display_Name = "Boot";
417 Backup_Display_Name = Display_Name;
418 Can_Be_Backed_Up = true;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600419 Can_Flash_Img = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500420 } else if (Mount_Point == "/recovery") {
421 Display_Name = "Recovery";
422 Backup_Display_Name = Display_Name;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600423 Can_Flash_Img = true;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500424 } else if (Mount_Point == "/system_image") {
425 Display_Name = "System Image";
426 Backup_Display_Name = Display_Name;
HashBanged974bb2016-01-30 14:20:09 -0500427 Can_Flash_Img = true;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500428 Can_Be_Backed_Up = true;
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500429 } else if (Mount_Point == "/vendor_image") {
430 Display_Name = "Vendor Image";
431 Backup_Display_Name = Display_Name;
HashBanged974bb2016-01-30 14:20:09 -0500432 Can_Flash_Img = true;
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500433 Can_Be_Backed_Up = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500434 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400435 }
436
Matt Mower2416a502016-04-12 19:54:46 -0500437 // Process TWRP fstab flags
438 if (strlen(twflags) > 0) {
439 string Prev_Display_Name = Display_Name;
440 string Prev_Storage_Name = Storage_Name;
441 string Prev_Backup_Display_Name = Backup_Display_Name;
442 Display_Name = "";
443 Storage_Name = "";
444 Backup_Display_Name = "";
445
446 Process_TW_Flags(twflags, Display_Error);
447
448 bool has_display_name = !Display_Name.empty();
449 bool has_storage_name = !Storage_Name.empty();
450 bool has_backup_name = !Backup_Display_Name.empty();
451 if (!has_display_name) Display_Name = Prev_Display_Name;
452 if (!has_storage_name) Storage_Name = Prev_Storage_Name;
453 if (!has_backup_name) Backup_Display_Name = Prev_Backup_Display_Name;
454
455 if (has_display_name && !has_storage_name)
456 Storage_Name = Display_Name;
457 if (!has_display_name && has_storage_name)
458 Display_Name = Storage_Name;
459 if (has_display_name && !has_backup_name && Backup_Display_Name != "Android Secure")
460 Backup_Display_Name = Display_Name;
461 if (!has_display_name && has_backup_name)
462 Display_Name = Backup_Display_Name;
463 }
Dees_Troy51127312012-09-08 13:08:49 -0400464 return true;
465}
466
Matt Mower72c87ce2016-04-26 14:34:56 -0500467void TWPartition::Partition_Post_Processing(bool Display_Error) {
468 if (Mount_Point == "/data")
469 Setup_Data_Partition(Display_Error);
470 else if (Mount_Point == "/cache")
471 Setup_Cache_Partition(Display_Error);
472}
473
474void TWPartition::Setup_Data_Partition(bool Display_Error) {
475 if (Mount_Point != "/data")
476 return;
477
478 // Ensure /data is not mounted as tmpfs for qcom hardware decrypt
479 UnMount(false);
480
481#ifdef TW_INCLUDE_CRYPTO
482 if (datamedia)
483 Setup_Data_Media();
484 Can_Be_Encrypted = true;
485 char crypto_blkdev[255];
486 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
487 if (strcmp(crypto_blkdev, "error") != 0) {
488 DataManager::SetValue(TW_IS_DECRYPTED, 1);
489 Is_Encrypted = true;
490 Is_Decrypted = true;
491 Decrypted_Block_Device = crypto_blkdev;
492 LOGINFO("Data already decrypted, new block device: '%s'\n", crypto_blkdev);
493 } else if (!Mount(false)) {
494 if (Is_Present) {
495 set_partition_data(Actual_Block_Device.c_str(), Crypto_Key_Location.c_str(), Fstab_File_System.c_str());
496 if (cryptfs_check_footer() == 0) {
497 Is_Encrypted = true;
498 Is_Decrypted = false;
499 Can_Be_Mounted = false;
500 Current_File_System = "emmc";
501 Setup_Image(Display_Error);
502 DataManager::SetValue(TW_IS_ENCRYPTED, 1);
503 DataManager::SetValue(TW_CRYPTO_PWTYPE, cryptfs_get_password_type());
504 DataManager::SetValue(TW_CRYPTO_PASSWORD, "");
505 DataManager::SetValue("tw_crypto_display", "");
506 } else {
507 gui_err("mount_data_footer=Could not mount /data and unable to find crypto footer.");
508 }
509 } else {
510 LOGERR("Primary block device '%s' for mount point '%s' is not present!\n", Primary_Block_Device.c_str(), Mount_Point.c_str());
511 }
512 } else {
513 // Filesystem is not encrypted and the mount succeeded, so return to
514 // the original unmounted state
515 UnMount(false);
516 }
517 if (datamedia && (!Is_Encrypted || (Is_Encrypted && Is_Decrypted))) {
518 Setup_Data_Media();
519 Recreate_Media_Folder();
520 }
521#else
522 if (datamedia) {
523 Setup_Data_Media();
524 Recreate_Media_Folder();
525 }
526#endif
527}
528
529void TWPartition::Setup_Cache_Partition(bool Display_Error __unused) {
530 if (Mount_Point != "/cache")
531 return;
532
533 if (!Mount(true))
534 return;
535
536 if (!TWFunc::Path_Exists("/cache/recovery/.")) {
537 LOGINFO("Recreating /cache/recovery folder\n");
538 if (mkdir("/cache/recovery", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0)
539 LOGERR("Could not create /cache/recovery\n");
540 }
541}
542
Matt Mower4ab42b12016-04-21 13:52:18 -0500543void TWPartition::Process_FS_Flags(const char *str) {
544 char *options = strdup(str);
545 char *ptr, *savep;
Hashcode62bd9e02013-11-19 21:59:42 -0800546
Matt Mower4ab42b12016-04-21 13:52:18 -0500547 Mount_Options = "";
Hashcode62bd9e02013-11-19 21:59:42 -0800548
Matt Mower4ab42b12016-04-21 13:52:18 -0500549 // Avoid issues with potentially nested strtok by using strtok_r
550 ptr = strtok_r(options, ",", &savep);
551 while (ptr) {
552 const struct flag_list* mount_flag = mount_flags;
553
554 for (; mount_flag->name; mount_flag++) {
555 // mount_flags are never postfixed by '=',
556 // so only match identical strings (including length)
557 if (strcmp(ptr, mount_flag->name) == 0) {
558 Mount_Flags |= mount_flag->flag;
Hashcode62bd9e02013-11-19 21:59:42 -0800559 break;
560 }
561 }
562
Matt Mower4ab42b12016-04-21 13:52:18 -0500563 if (mount_flag->flag == MS_RDONLY)
564 Mount_Read_Only = true;
Hashcode62bd9e02013-11-19 21:59:42 -0800565
Matt Mower4ab42b12016-04-21 13:52:18 -0500566 if (mount_flag->name != 0) {
567 if (!Mount_Options.empty())
568 Mount_Options += ",";
569 Mount_Options += mount_flag->name;
570 } else {
571 LOGINFO("Unhandled mount flag: '%s'\n", ptr);
572 }
573
574 ptr = strtok_r(NULL, ",", &savep);
575 }
576 free(options);
Hashcode62bd9e02013-11-19 21:59:42 -0800577}
578
Matt Mower2416a502016-04-12 19:54:46 -0500579void TWPartition::Apply_TW_Flag(const unsigned flag, const char* str, const bool val) {
580 switch (flag) {
581 case TWFLAG_ANDSEC:
582 Has_Android_Secure = val;
583 break;
584 case TWFLAG_BACKUP:
585 Can_Be_Backed_Up = val;
586 break;
587 case TWFLAG_BACKUPNAME:
588 Backup_Display_Name = str;
589 break;
590 case TWFLAG_BLOCKSIZE:
591 Format_Block_Size = (unsigned long)(atol(str));
592 break;
593 case TWFLAG_CANBEWIPED:
594 Can_Be_Wiped = val;
595 break;
596 case TWFLAG_CANENCRYPTBACKUP:
597 Can_Encrypt_Backup = val;
598 break;
599 case TWFLAG_DEFAULTS:
600 // Do nothing
601 break;
602 case TWFLAG_DISPLAY:
603 Display_Name = str;
604 break;
605 case TWFLAG_ENCRYPTABLE:
606 case TWFLAG_FORCEENCRYPT:
607 Crypto_Key_Location = str;
608 break;
609 case TWFLAG_FLASHIMG:
610 Can_Flash_Img = val;
611 break;
612 case TWFLAG_FSFLAGS:
Matt Mower4ab42b12016-04-21 13:52:18 -0500613 Process_FS_Flags(str);
Matt Mower2416a502016-04-12 19:54:46 -0500614 break;
615 case TWFLAG_IGNOREBLKID:
616 Ignore_Blkid = val;
617 break;
618 case TWFLAG_LENGTH:
619 Length = atoi(str);
620 break;
621 case TWFLAG_MOUNTTODECRYPT:
622 Mount_To_Decrypt = val;
623 break;
624 case TWFLAG_REMOVABLE:
625 Removable = val;
626 break;
627 case TWFLAG_RETAINLAYOUTVERSION:
628 Retain_Layout_Version = val;
629 break;
630 case TWFLAG_SETTINGSSTORAGE:
631 Is_Settings_Storage = val;
632 if (Is_Settings_Storage)
633 Is_Storage = true;
634 break;
635 case TWFLAG_STORAGE:
636 Is_Storage = val;
637 break;
638 case TWFLAG_STORAGENAME:
639 Storage_Name = str;
640 break;
641 case TWFLAG_SUBPARTITIONOF:
642 Is_SubPartition = true;
643 SubPartition_Of = str;
644 break;
645 case TWFLAG_SYMLINK:
646 Symlink_Path = str;
647 break;
648 case TWFLAG_USERDATAENCRYPTBACKUP:
649 Use_Userdata_Encryption = val;
650 if (Use_Userdata_Encryption)
651 Can_Encrypt_Backup = true;
652 break;
653 case TWFLAG_USERMRF:
654 Use_Rm_Rf = val;
655 break;
656 case TWFLAG_WIPEDURINGFACTORYRESET:
657 Wipe_During_Factory_Reset = val;
658 if (Wipe_During_Factory_Reset) {
659 Can_Be_Wiped = true;
660 Wipe_Available_in_GUI = true;
661 }
662 break;
663 case TWFLAG_WIPEINGUI:
664 Wipe_Available_in_GUI = val;
665 if (Wipe_Available_in_GUI)
666 Can_Be_Wiped = true;
667 break;
668 default:
669 // Should not get here
670 LOGINFO("Flag identified for processing, but later unmatched: %i\n", flag);
671 break;
672 }
673}
Dees_Troy51127312012-09-08 13:08:49 -0400674
Matt Mower2416a502016-04-12 19:54:46 -0500675void TWPartition::Process_TW_Flags(char *flags, bool Display_Error) {
676 char separator[2] = {'\n', 0};
677 char *ptr, *savep;
678
679 // Semicolons within double-quotes are not forbidden, so replace
680 // only the semicolons intended as separators with '\n' for strtok
681 for (unsigned i = 0, skip = 0; i < strlen(flags); i++) {
682 if (flags[i] == '\"')
Dees_Troya13d74f2013-03-24 08:54:55 -0500683 skip = !skip;
Matt Mower2416a502016-04-12 19:54:46 -0500684 if (!skip && flags[i] == ';')
685 flags[i] = separator[0];
Dees_Troy51127312012-09-08 13:08:49 -0400686 }
687
Matt Mower2416a502016-04-12 19:54:46 -0500688 // Avoid issues with potentially nested strtok by using strtok_r
689 ptr = strtok_r(flags, separator, &savep);
690 while (ptr) {
691 int ptr_len = strlen(ptr);
692 const struct flag_list* tw_flag = tw_flags;
Hashcode62bd9e02013-11-19 21:59:42 -0800693
Matt Mower2416a502016-04-12 19:54:46 -0500694 for (; tw_flag->name; tw_flag++) {
695 int flag_len = strlen(tw_flag->name);
696
697 if (strncmp(ptr, tw_flag->name, flag_len) == 0) {
698 bool flag_val = false;
699
700 if (ptr_len > flag_len && (tw_flag->name)[flag_len-1] != '='
701 && ptr[flag_len] != '=') {
702 // Handle flags with same starting string
703 // (e.g. backup and backupname)
704 continue;
705 } else if (ptr_len > flag_len && ptr[flag_len] == '=') {
706 // Handle flags with dual format: Part 1
707 // (e.g. backup and backup=y. backup=y handled here)
708 ptr += flag_len + 1;
709 TWFunc::Strip_Quotes(ptr);
710 // Skip flags with empty argument
711 // (e.g. backup=)
712 if (strlen(ptr) == 0) {
713 LOGINFO("Flag missing argument or should not include '=': %s=\n", tw_flag->name);
714 break;
715 }
716 flag_val = strchr("yY1", *ptr) != NULL;
717 } else if (ptr_len == flag_len
718 && (tw_flag->name)[flag_len-1] == '=') {
719 // Skip flags missing argument after =
720 // (e.g. backupname=)
721 LOGINFO("Flag missing argument: %s\n", tw_flag->name);
722 break;
723 } else if (ptr_len > flag_len
724 && (tw_flag->name)[flag_len-1] == '=') {
725 // Handle arguments to flags
726 // (e.g. backupname="My Stuff")
727 ptr += flag_len;
728 TWFunc::Strip_Quotes(ptr);
729 // Skip flags with empty argument
730 // (e.g. backupname="")
731 if (strlen(ptr) == 0) {
732 LOGINFO("Flag missing argument: %s\n", tw_flag->name);
733 break;
734 }
735 } else if (ptr_len == flag_len) {
736 // Handle flags with dual format: Part 2
737 // (e.g. backup and backup=y. backup handled here)
738 flag_val = true;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600739 } else {
Matt Mower2416a502016-04-12 19:54:46 -0500740 LOGINFO("Flag matched, but could not be processed: %s\n", ptr);
741 break;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600742 }
Matt Mower2416a502016-04-12 19:54:46 -0500743
744 Apply_TW_Flag(tw_flag->flag, ptr, flag_val);
745 break;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600746 }
Matt Mower2416a502016-04-12 19:54:46 -0500747 }
748 if (tw_flag->name == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400749 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000750 LOGERR("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400751 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000752 LOGINFO("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400753 }
Matt Mower2416a502016-04-12 19:54:46 -0500754 ptr = strtok_r(NULL, separator, &savep);
Dees_Troy51127312012-09-08 13:08:49 -0400755 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400756}
757
Dees_Troy5bf43922012-09-07 16:07:55 -0400758bool TWPartition::Is_File_System(string File_System) {
759 if (File_System == "ext2" ||
Dees_Troy63c8df72012-09-10 14:02:05 -0400760 File_System == "ext3" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400761 File_System == "ext4" ||
762 File_System == "vfat" ||
763 File_System == "ntfs" ||
764 File_System == "yaffs2" ||
bigbiff bigbiff3e146522012-11-14 14:32:59 -0500765 File_System == "exfat" ||
Dees_Troye5017042013-08-29 16:38:55 +0000766 File_System == "f2fs" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400767 File_System == "auto")
768 return true;
769 else
770 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400771}
772
Dees_Troy5bf43922012-09-07 16:07:55 -0400773bool TWPartition::Is_Image(string File_System) {
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400774 if (File_System == "emmc" || File_System == "mtd" || File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -0400775 return true;
776 else
777 return false;
778}
779
Dees_Troy51127312012-09-08 13:08:49 -0400780bool TWPartition::Make_Dir(string Path, bool Display_Error) {
Ethan Yonker93ac7a02016-11-07 22:05:58 -0600781 if (TWFunc::Get_D_Type_From_Stat(Path) != S_IFDIR)
782 unlink(Path.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400783 if (!TWFunc::Path_Exists(Path)) {
Dees_Troy51127312012-09-08 13:08:49 -0400784 if (mkdir(Path.c_str(), 0777) == -1) {
785 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -0500786 gui_msg(Msg(msg::kError, "create_folder_strerr=Can not create '{1}' folder ({2}).")(Path)(strerror(errno)));
Dees_Troy51127312012-09-08 13:08:49 -0400787 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000788 LOGINFO("Can not create '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400789 return false;
790 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000791 LOGINFO("Created '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400792 return true;
793 }
794 }
795 return true;
796}
797
Dees_Troy5bf43922012-09-07 16:07:55 -0400798void TWPartition::Setup_File_System(bool Display_Error) {
799 struct statfs st;
800
801 Can_Be_Mounted = true;
802 Can_Be_Wiped = true;
803
Dees_Troy5bf43922012-09-07 16:07:55 -0400804 // Make the mount point folder if it doesn't exist
Dees_Troy51127312012-09-08 13:08:49 -0400805 Make_Dir(Mount_Point, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400806 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
807 Backup_Name = Display_Name;
bigbiffce8f83c2015-12-12 18:30:21 -0500808 Backup_Method = BM_FILES;
Dees_Troy5bf43922012-09-07 16:07:55 -0400809}
810
811void TWPartition::Setup_Image(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400812 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
813 Backup_Name = Display_Name;
Gary Peck82599a82012-11-21 16:23:12 -0800814 if (Current_File_System == "emmc")
bigbiffce8f83c2015-12-12 18:30:21 -0500815 Backup_Method = BM_DD;
Gary Peck82599a82012-11-21 16:23:12 -0800816 else if (Current_File_System == "mtd" || Current_File_System == "bml")
bigbiffce8f83c2015-12-12 18:30:21 -0500817 Backup_Method = BM_FLASH_UTILS;
Dees_Troy5bf43922012-09-07 16:07:55 -0400818 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000819 LOGINFO("Unhandled file system '%s' on image '%s'\n", Current_File_System.c_str(), Display_Name.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400820 if (Find_Partition_Size()) {
821 Used = Size;
822 Backup_Size = Size;
Dees_Troy51a0e822012-09-05 15:24:24 -0400823 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400824 if (Display_Error)
Dees Troyd932ce12013-10-18 17:12:59 +0000825 LOGERR("Unable to find partition size for '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400826 else
Dees Troyd932ce12013-10-18 17:12:59 +0000827 LOGINFO("Unable to find partition size for '%s'\n", Mount_Point.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400828 }
829}
830
Dees_Troye58d5262012-09-21 12:27:57 -0400831void TWPartition::Setup_AndSec(void) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500832 Backup_Display_Name = "Android Secure";
Dees_Troye58d5262012-09-21 12:27:57 -0400833 Backup_Name = "and-sec";
Dees_Troya13d74f2013-03-24 08:54:55 -0500834 Can_Be_Backed_Up = true;
Dees_Troye58d5262012-09-21 12:27:57 -0400835 Has_Android_Secure = true;
836 Symlink_Path = Mount_Point + "/.android_secure";
837 Symlink_Mount_Point = "/and-sec";
838 Backup_Path = Symlink_Mount_Point;
839 Make_Dir("/and-sec", true);
840 Recreate_AndSec_Folder();
Kjell Braden3126a112016-06-19 16:58:15 +0000841 Mount_Storage_Retry(true);
Dees_Troye58d5262012-09-21 12:27:57 -0400842}
843
that9e0593e2014-10-08 00:01:24 +0200844void TWPartition::Setup_Data_Media() {
Ethan Yonker6277c792014-09-15 14:54:30 -0500845 LOGINFO("Setting up '%s' as data/media emulated storage.\n", Mount_Point.c_str());
Ethan Yonker66a19492015-12-10 10:19:45 -0600846 if (Storage_Name.empty() || Storage_Name == "Data")
847 Storage_Name = "Internal Storage";
Ethan Yonker6277c792014-09-15 14:54:30 -0500848 Has_Data_Media = true;
849 Is_Storage = true;
Ethan Yonker66a19492015-12-10 10:19:45 -0600850 Storage_Path = Mount_Point + "/media";
Ethan Yonker6277c792014-09-15 14:54:30 -0500851 Symlink_Path = Storage_Path;
Ethan Yonker66a19492015-12-10 10:19:45 -0600852 if (Mount_Point == "/data") {
853 Is_Settings_Storage = true;
854 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
855 Make_Dir("/emmc", false);
856 Symlink_Mount_Point = "/emmc";
857 } else {
858 Make_Dir("/sdcard", false);
859 Symlink_Mount_Point = "/sdcard";
860 }
861 if (Mount(false) && TWFunc::Path_Exists(Mount_Point + "/media/0")) {
862 Storage_Path = Mount_Point + "/media/0";
863 Symlink_Path = Storage_Path;
864 DataManager::SetValue(TW_INTERNAL_PATH, Mount_Point + "/media/0");
865 UnMount(true);
866 }
867 DataManager::SetValue("tw_has_internal", 1);
868 DataManager::SetValue("tw_has_data_media", 1);
869 du.add_absolute_dir(Mount_Point + "/misc/vold");
870 du.add_absolute_dir(Mount_Point + "/.layout_version");
871 du.add_absolute_dir(Mount_Point + "/system/storage.xml");
Ethan Yonker6277c792014-09-15 14:54:30 -0500872 } else {
Ethan Yonker66a19492015-12-10 10:19:45 -0600873 if (Mount(true) && TWFunc::Path_Exists(Mount_Point + "/media/0")) {
874 Storage_Path = Mount_Point + "/media/0";
875 Symlink_Path = Storage_Path;
876 UnMount(true);
877 }
Ethan Yonker6277c792014-09-15 14:54:30 -0500878 }
Ethan Yonker66a19492015-12-10 10:19:45 -0600879 du.add_absolute_dir(Mount_Point + "/media");
Ethan Yonker6277c792014-09-15 14:54:30 -0500880}
881
Dees_Troy5bf43922012-09-07 16:07:55 -0400882void TWPartition::Find_Real_Block_Device(string& Block, bool Display_Error) {
883 char device[512], realDevice[512];
884
885 strcpy(device, Block.c_str());
886 memset(realDevice, 0, sizeof(realDevice));
887 while (readlink(device, realDevice, sizeof(realDevice)) > 0)
888 {
889 strcpy(device, realDevice);
890 memset(realDevice, 0, sizeof(realDevice));
891 }
892
893 if (device[0] != '/') {
894 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000895 LOGERR("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400896 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000897 LOGINFO("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400898 return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400899 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400900 Block = device;
901 return;
902 }
903}
904
Kjell Braden3126a112016-06-19 16:58:15 +0000905bool TWPartition::Mount_Storage_Retry(bool Display_Error) {
Dees_Troy8e337f32012-10-13 22:07:49 -0400906 // On some devices, storage doesn't want to mount right away, retry and sleep
Kjell Braden3126a112016-06-19 16:58:15 +0000907 if (!Mount(Display_Error)) {
Dees_Troy8e337f32012-10-13 22:07:49 -0400908 int retry_count = 5;
909 while (retry_count > 0 && !Mount(false)) {
910 usleep(500000);
911 retry_count--;
912 }
Kjell Braden3126a112016-06-19 16:58:15 +0000913 return Mount(Display_Error);
Dees_Troy8e337f32012-10-13 22:07:49 -0400914 }
Kjell Braden3126a112016-06-19 16:58:15 +0000915 return true;
Dees_Troy8e337f32012-10-13 22:07:49 -0400916}
917
Dees_Troy38bd7602012-09-14 13:33:53 -0400918bool TWPartition::Find_MTD_Block_Device(string MTD_Name) {
919 FILE *fp = NULL;
920 char line[255];
921
922 fp = fopen("/proc/mtd", "rt");
923 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000924 LOGERR("Device does not support /proc/mtd\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400925 return false;
926 }
927
928 while (fgets(line, sizeof(line), fp) != NULL)
929 {
930 char device[32], label[32];
931 unsigned long size = 0;
932 char* fstype = NULL;
933 int deviceId;
934
935 sscanf(line, "%s %lx %*s %*c%s", device, &size, label);
936
937 // Skip header and blank lines
938 if ((strcmp(device, "dev:") == 0) || (strlen(line) < 8))
939 continue;
940
941 // Strip off the trailing " from the label
942 label[strlen(label)-1] = '\0';
943
944 if (strcmp(label, MTD_Name.c_str()) == 0) {
945 // We found our device
946 // Strip off the trailing : from the device
947 device[strlen(device)-1] = '\0';
948 if (sscanf(device,"mtd%d", &deviceId) == 1) {
949 sprintf(device, "/dev/block/mtdblock%d", deviceId);
950 Primary_Block_Device = device;
Dees_Troy76543db2013-06-19 16:24:30 +0000951 fclose(fp);
952 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -0400953 }
954 }
955 }
956 fclose(fp);
957
958 return false;
959}
960
Dees_Troy51127312012-09-08 13:08:49 -0400961bool TWPartition::Get_Size_Via_statfs(bool Display_Error) {
962 struct statfs st;
963 string Local_Path = Mount_Point + "/.";
964
965 if (!Mount(Display_Error))
966 return false;
967
968 if (statfs(Local_Path.c_str(), &st) != 0) {
969 if (!Removable) {
970 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000971 LOGERR("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400972 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000973 LOGINFO("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400974 }
975 return false;
976 }
977 Size = (st.f_blocks * st.f_bsize);
978 Used = ((st.f_blocks - st.f_bfree) * st.f_bsize);
979 Free = (st.f_bfree * st.f_bsize);
980 Backup_Size = Used;
981 return true;
982}
983
984bool TWPartition::Get_Size_Via_df(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400985 FILE* fp;
986 char command[255], line[512];
987 int include_block = 1;
988 unsigned int min_len;
989
990 if (!Mount(Display_Error))
991 return false;
992
Dees_Troy38bd7602012-09-14 13:33:53 -0400993 min_len = Actual_Block_Device.size() + 2;
Dees_Troy51127312012-09-08 13:08:49 -0400994 sprintf(command, "df %s > /tmp/dfoutput.txt", Mount_Point.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +0200995 TWFunc::Exec_Cmd(command);
Dees_Troy51127312012-09-08 13:08:49 -0400996 fp = fopen("/tmp/dfoutput.txt", "rt");
997 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000998 LOGINFO("Unable to open /tmp/dfoutput.txt.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400999 return false;
Dees_Troy51127312012-09-08 13:08:49 -04001000 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001001
1002 while (fgets(line, sizeof(line), fp) != NULL)
1003 {
1004 unsigned long blocks, used, available;
1005 char device[64];
1006 char tmpString[64];
1007
1008 if (strncmp(line, "Filesystem", 10) == 0)
1009 continue;
1010 if (strlen(line) < min_len) {
1011 include_block = 0;
1012 continue;
1013 }
1014 if (include_block) {
1015 sscanf(line, "%s %lu %lu %lu", device, &blocks, &used, &available);
1016 } else {
1017 // The device block string is so long that the df information is on the next line
1018 int space_count = 0;
Dees_Troye58d5262012-09-21 12:27:57 -04001019 sprintf(tmpString, "/dev/block/%s", Actual_Block_Device.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001020 while (tmpString[space_count] == 32)
1021 space_count++;
1022 sscanf(line + space_count, "%lu %lu %lu", &blocks, &used, &available);
1023 }
1024
1025 // Adjust block size to byte size
1026 Size = blocks * 1024ULL;
1027 Used = used * 1024ULL;
1028 Free = available * 1024ULL;
1029 Backup_Size = Used;
1030 }
1031 fclose(fp);
1032 return true;
1033}
1034
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001035unsigned long long TWPartition::IOCTL_Get_Block_Size() {
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001036 Find_Actual_Block_Device();
Ethan Yonker483e9f42016-01-11 22:21:18 -06001037
1038 return TWFunc::IOCTL_Get_Block_Size(Actual_Block_Device.c_str());
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001039}
1040
Dees_Troy5bf43922012-09-07 16:07:55 -04001041bool TWPartition::Find_Partition_Size(void) {
1042 FILE* fp;
1043 char line[512];
1044 string tmpdevice;
1045
igoriok87e3d932013-01-31 21:03:53 +02001046 fp = fopen("/proc/dumchar_info", "rt");
1047 if (fp != NULL) {
1048 while (fgets(line, sizeof(line), fp) != NULL)
1049 {
1050 char label[32], device[32];
1051 unsigned long size = 0;
1052
thatd43bf2d2014-09-21 23:13:02 +02001053 sscanf(line, "%s %lx %*x %*u %s", label, &size, device);
igoriok87e3d932013-01-31 21:03:53 +02001054
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05001055 // Skip header, annotation and blank lines
igoriok87e3d932013-01-31 21:03:53 +02001056 if ((strncmp(device, "/dev/", 5) != 0) || (strlen(line) < 8))
1057 continue;
1058
1059 tmpdevice = "/dev/";
1060 tmpdevice += label;
1061 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
1062 Size = size;
1063 fclose(fp);
1064 return true;
1065 }
1066 }
1067 }
1068
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001069 unsigned long long ioctl_size = IOCTL_Get_Block_Size();
1070 if (ioctl_size) {
1071 Size = ioctl_size;
1072 return true;
1073 }
1074
Dees_Troy5bf43922012-09-07 16:07:55 -04001075 // In this case, we'll first get the partitions we care about (with labels)
1076 fp = fopen("/proc/partitions", "rt");
1077 if (fp == NULL)
1078 return false;
1079
1080 while (fgets(line, sizeof(line), fp) != NULL)
1081 {
1082 unsigned long major, minor, blocks;
1083 char device[512];
1084 char tmpString[64];
1085
Dees_Troy63c8df72012-09-10 14:02:05 -04001086 if (strlen(line) < 7 || line[0] == 'm') continue;
Dees_Troy5bf43922012-09-07 16:07:55 -04001087 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
1088
1089 tmpdevice = "/dev/block/";
1090 tmpdevice += device;
Dees_Troy38bd7602012-09-14 13:33:53 -04001091 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001092 // Adjust block size to byte size
1093 Size = blocks * 1024ULL;
1094 fclose(fp);
1095 return true;
1096 }
1097 }
1098 fclose(fp);
1099 return false;
1100}
1101
Dees_Troy5bf43922012-09-07 16:07:55 -04001102bool TWPartition::Is_Mounted(void) {
1103 if (!Can_Be_Mounted)
1104 return false;
1105
1106 struct stat st1, st2;
1107 string test_path;
1108
1109 // Check to see if the mount point directory exists
1110 test_path = Mount_Point + "/.";
1111 if (stat(test_path.c_str(), &st1) != 0) return false;
1112
1113 // Check to see if the directory above the mount point exists
1114 test_path = Mount_Point + "/../.";
1115 if (stat(test_path.c_str(), &st2) != 0) return false;
1116
1117 // Compare the device IDs -- if they match then we're (probably) using tmpfs instead of an actual device
1118 int ret = (st1.st_dev != st2.st_dev) ? true : false;
1119
1120 return ret;
1121}
1122
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05001123bool TWPartition::Is_File_System_Writable(void) {
1124 if (!Is_File_System(Current_File_System) || !Is_Mounted())
1125 return false;
1126
1127 string test_path = Mount_Point + "/.";
1128 return (access(test_path.c_str(), W_OK) == 0);
1129}
1130
Dees_Troy5bf43922012-09-07 16:07:55 -04001131bool TWPartition::Mount(bool Display_Error) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001132 int exfat_mounted = 0;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001133 unsigned long flags = Mount_Flags;
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001134
Dees_Troy5bf43922012-09-07 16:07:55 -04001135 if (Is_Mounted()) {
1136 return true;
1137 } else if (!Can_Be_Mounted) {
1138 return false;
1139 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001140
1141 Find_Actual_Block_Device();
1142
1143 // Check the current file system before mounting
1144 Check_FS_Type();
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001145 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/exfat-fuse")) {
Dees_Troye34c1332013-02-06 19:13:00 +00001146 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 +00001147 LOGINFO("cmd: %s\n", cmd.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001148 string result;
1149 if (TWFunc::Exec_Cmd(cmd, result) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001150 LOGINFO("exfat-fuse failed to mount with result '%s', trying vfat\n", result.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001151 Current_File_System = "vfat";
1152 } else {
1153#ifdef TW_NO_EXFAT_FUSE
1154 UnMount(false);
1155 // We'll let the kernel handle it but using exfat-fuse to detect if the file system is actually exfat
1156 // Some kernels let us mount vfat as exfat which doesn't work out too well
1157#else
1158 exfat_mounted = 1;
1159#endif
1160 }
1161 }
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001162
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001163 if (Current_File_System == "ntfs" && (TWFunc::Path_Exists("/sbin/ntfs-3g") || TWFunc::Path_Exists("/sbin/mount.ntfs"))) {
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001164 string cmd;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001165 string Ntfsmount_Binary = "";
1166
1167 if (TWFunc::Path_Exists("/sbin/ntfs-3g"))
1168 Ntfsmount_Binary = "ntfs-3g";
1169 else if (TWFunc::Path_Exists("/sbin/mount.ntfs"))
1170 Ntfsmount_Binary = "mount.ntfs";
1171
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001172 if (Mount_Read_Only)
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001173 cmd = "/sbin/" + Ntfsmount_Binary + " -o ro " + Actual_Block_Device + " " + Mount_Point;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001174 else
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001175 cmd = "/sbin/" + Ntfsmount_Binary + " " + Actual_Block_Device + " " + Mount_Point;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001176 LOGINFO("cmd: '%s'\n", cmd.c_str());
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001177
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001178 if (TWFunc::Exec_Cmd(cmd) == 0) {
1179 return true;
1180 } else {
1181 LOGINFO("ntfs-3g failed to mount, trying regular mount method.\n");
1182 }
1183 }
1184
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001185 if (Mount_Read_Only)
1186 flags |= MS_RDONLY;
1187
Dees_Troy22042032012-12-18 21:23:08 +00001188 if (Fstab_File_System == "yaffs2") {
1189 // mount an MTD partition as a YAFFS2 filesystem.
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001190 flags = MS_NOATIME | MS_NODEV | MS_NODIRATIME;
1191 if (Mount_Read_Only)
1192 flags |= MS_RDONLY;
Dees_Troy76543db2013-06-19 16:24:30 +00001193 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags, NULL) < 0) {
1194 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags | MS_RDONLY, NULL) < 0) {
1195 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001196 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy76543db2013-06-19 16:24:30 +00001197 else
1198 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
1199 return false;
1200 } else {
1201 LOGINFO("Mounted '%s' (MTD) as RO\n", Mount_Point.c_str());
1202 return true;
1203 }
1204 } else {
1205 struct stat st;
1206 string test_path = Mount_Point;
1207 if (stat(test_path.c_str(), &st) < 0) {
1208 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001209 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy76543db2013-06-19 16:24:30 +00001210 else
1211 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
1212 return false;
1213 }
1214 mode_t new_mode = st.st_mode | S_IXUSR | S_IXGRP | S_IXOTH;
1215 if (new_mode != st.st_mode) {
1216 LOGINFO("Fixing execute permissions for %s\n", Mount_Point.c_str());
1217 if (chmod(Mount_Point.c_str(), new_mode) < 0) {
1218 if (Display_Error)
1219 LOGERR("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
1220 else
1221 LOGINFO("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
1222 return false;
1223 }
1224 }
Dees_Troy22042032012-12-18 21:23:08 +00001225 return true;
Dees_Troy76543db2013-06-19 16:24:30 +00001226 }
thatc7572eb2015-03-31 18:55:30 +02001227 }
1228
1229 string mount_fs = Current_File_System;
1230 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sys/module/texfat"))
1231 mount_fs = "texfat";
1232
1233 if (!exfat_mounted &&
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001234 mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), mount_fs.c_str(), flags, Mount_Options.c_str()) != 0 &&
1235 mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), mount_fs.c_str(), flags, NULL) != 0) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001236#ifdef TW_NO_EXFAT_FUSE
1237 if (Current_File_System == "exfat") {
Dees_Troy2673cec2013-04-02 20:22:16 +00001238 LOGINFO("Mounting exfat failed, trying vfat...\n");
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001239 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), "vfat", 0, NULL) != 0) {
Dees_Troy85f44ed2013-01-09 18:42:36 +00001240 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001241 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy85f44ed2013-01-09 18:42:36 +00001242 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001243 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001244 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 +00001245 return false;
Dees_Troy85f44ed2013-01-09 18:42:36 +00001246 }
Dees_Troyb05ddee2013-01-28 20:24:50 +00001247 } else {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001248#endif
bigbiff bigbiff26774a02014-03-29 18:22:00 -04001249 if (!Removable && 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_Troy3f5c4e82013-02-01 15:16:59 +00001251 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001252 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
1253 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 +00001254 return false;
1255#ifdef TW_NO_EXFAT_FUSE
Dees_Troy85f44ed2013-01-09 18:42:36 +00001256 }
1257#endif
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001258 }
Ethan Yonker253368a2014-11-25 15:00:52 -06001259
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001260 if (Removable)
1261 Update_Size(Display_Error);
1262
xiaolu9416f4f2015-06-04 08:22:23 +08001263 if (!Symlink_Mount_Point.empty() && TWFunc::Path_Exists(Symlink_Path)) {
codelover2a3d4ce2015-03-14 20:26:49 +08001264 string Command = "mount -o bind '" + Symlink_Path + "' '" + Symlink_Mount_Point + "'";
Vojtech Bocek05534202013-09-11 08:11:56 +02001265 TWFunc::Exec_Cmd(Command);
Dees_Troy5bf43922012-09-07 16:07:55 -04001266 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001267 return true;
1268}
1269
1270bool TWPartition::UnMount(bool Display_Error) {
1271 if (Is_Mounted()) {
1272 int never_unmount_system;
1273
1274 DataManager::GetValue(TW_DONT_UNMOUNT_SYSTEM, never_unmount_system);
1275 if (never_unmount_system == 1 && Mount_Point == "/system")
1276 return true; // Never unmount system if you're not supposed to unmount it
1277
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001278 if (Is_Storage)
Ethan Yonker726a0202014-12-16 20:01:38 -06001279 PartitionManager.Remove_MTP_Storage(MTP_Storage_ID);
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001280
Dees_Troy38bd7602012-09-14 13:33:53 -04001281 if (!Symlink_Mount_Point.empty())
1282 umount(Symlink_Mount_Point.c_str());
1283
Dees_Troyb05ddee2013-01-28 20:24:50 +00001284 umount(Mount_Point.c_str());
1285 if (Is_Mounted()) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001286 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001287 gui_msg(Msg(msg::kError, "fail_unmount=Failed to unmount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy5bf43922012-09-07 16:07:55 -04001288 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001289 LOGINFO("Unable to unmount '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001290 return false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001291 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -04001292 return true;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001293 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001294 } else {
1295 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001296 }
1297}
1298
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05001299bool TWPartition::ReMount(bool Display_Error) {
1300 if (UnMount(Display_Error))
1301 return Mount(Display_Error);
1302 return false;
1303}
1304
1305bool TWPartition::ReMount_RW(bool Display_Error) {
1306 // No need to remount if already mounted rw
1307 if (Is_File_System_Writable())
1308 return true;
1309
1310 bool ro = Mount_Read_Only;
1311 int flags = Mount_Flags;
1312
1313 Mount_Read_Only = false;
1314 Mount_Flags &= ~MS_RDONLY;
1315
1316 bool ret = ReMount(Display_Error);
1317
1318 Mount_Read_Only = ro;
1319 Mount_Flags = flags;
1320
1321 return ret;
1322}
1323
Gary Peck43acadf2012-11-21 21:19:01 -08001324bool TWPartition::Wipe(string New_File_System) {
Ethan Yonker726a0202014-12-16 20:01:38 -06001325 bool wiped = false, update_crypt = false, recreate_media = true;
Dees_Troy16c2b312013-01-15 16:51:18 +00001326 int check;
1327 string Layout_Filename = Mount_Point + "/.layout_version";
1328
Dees_Troy38bd7602012-09-14 13:33:53 -04001329 if (!Can_Be_Wiped) {
Matt Mower3c366972015-12-25 19:28:31 -06001330 gui_msg(Msg(msg::kError, "cannot_wipe=Partition {1} cannot be wiped.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04001331 return false;
1332 }
1333
Dees_Troyc51f1f92012-09-20 15:32:13 -04001334 if (Mount_Point == "/cache")
Dees_Troy2673cec2013-04-02 20:22:16 +00001335 Log_Offset = 0;
Dees_Troyc51f1f92012-09-20 15:32:13 -04001336
Dees_Troy16c2b312013-01-15 16:51:18 +00001337 if (Retain_Layout_Version && Mount(false) && TWFunc::Path_Exists(Layout_Filename))
1338 TWFunc::copy_file(Layout_Filename, "/.layout_version", 0600);
1339 else
1340 unlink("/.layout_version");
Dees_Troy38bd7602012-09-14 13:33:53 -04001341
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001342 if (Has_Data_Media && Current_File_System == New_File_System) {
Dees_Troy16c2b312013-01-15 16:51:18 +00001343 wiped = Wipe_Data_Without_Wiping_Media();
Ethan Yonker5eac2222014-06-11 12:22:55 -05001344 recreate_media = false;
Dees_Troy16c2b312013-01-15 16:51:18 +00001345 } else {
Dees_Troy16c2b312013-01-15 16:51:18 +00001346 DataManager::GetValue(TW_RM_RF_VAR, check);
1347
Hashcodedabfd492013-08-29 22:45:30 -07001348 if (check || Use_Rm_Rf)
Dees_Troy16c2b312013-01-15 16:51:18 +00001349 wiped = Wipe_RMRF();
1350 else if (New_File_System == "ext4")
1351 wiped = Wipe_EXT4();
1352 else if (New_File_System == "ext2" || New_File_System == "ext3")
1353 wiped = Wipe_EXT23(New_File_System);
1354 else if (New_File_System == "vfat")
1355 wiped = Wipe_FAT();
1356 else if (New_File_System == "exfat")
1357 wiped = Wipe_EXFAT();
1358 else if (New_File_System == "yaffs2")
1359 wiped = Wipe_MTD();
Dees_Troye5017042013-08-29 16:38:55 +00001360 else if (New_File_System == "f2fs")
1361 wiped = Wipe_F2FS();
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001362 else if (New_File_System == "ntfs")
1363 wiped = Wipe_NTFS();
Dees_Troy16c2b312013-01-15 16:51:18 +00001364 else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001365 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 +00001366 unlink("/.layout_version");
1367 return false;
1368 }
1369 update_crypt = wiped;
Gary Pecke8bc5d72012-12-21 06:45:25 -08001370 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001371
Gary Pecke8bc5d72012-12-21 06:45:25 -08001372 if (wiped) {
Dees_Troy1c1ac442013-01-17 21:42:14 +00001373 if (Mount_Point == "/cache")
1374 DataManager::Output_Version();
1375
Dees_Troy16c2b312013-01-15 16:51:18 +00001376 if (TWFunc::Path_Exists("/.layout_version") && Mount(false))
1377 TWFunc::copy_file("/.layout_version", Layout_Filename, 0600);
1378
1379 if (update_crypt) {
1380 Setup_File_System(false);
1381 if (Is_Encrypted && !Is_Decrypted) {
1382 // just wiped an encrypted partition back to its unencrypted state
1383 Is_Encrypted = false;
1384 Is_Decrypted = false;
1385 Decrypted_Block_Device = "";
1386 if (Mount_Point == "/data") {
1387 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1388 DataManager::SetValue(TW_IS_DECRYPTED, 0);
1389 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001390 }
1391 }
Ethan Yonker5eac2222014-06-11 12:22:55 -05001392
Ethan Yonker66a19492015-12-10 10:19:45 -06001393 if (Has_Data_Media && recreate_media) {
Ethan Yonker5eac2222014-06-11 12:22:55 -05001394 Recreate_Media_Folder();
1395 }
Matt Mower209c9632016-01-20 12:08:35 -06001396 if (Is_Storage && Mount(false))
1397 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Gary Pecke8bc5d72012-12-21 06:45:25 -08001398 }
Matt Mower209c9632016-01-20 12:08:35 -06001399
Gary Pecke8bc5d72012-12-21 06:45:25 -08001400 return wiped;
Dees_Troy51a0e822012-09-05 15:24:24 -04001401}
1402
Gary Peck43acadf2012-11-21 21:19:01 -08001403bool TWPartition::Wipe() {
Gary Peck82599a82012-11-21 16:23:12 -08001404 if (Is_File_System(Current_File_System))
1405 return Wipe(Current_File_System);
1406 else
1407 return Wipe(Fstab_File_System);
Gary Peck43acadf2012-11-21 21:19:01 -08001408}
1409
Dees_Troye58d5262012-09-21 12:27:57 -04001410bool TWPartition::Wipe_AndSec(void) {
1411 if (!Has_Android_Secure)
1412 return false;
1413
Dees_Troye58d5262012-09-21 12:27:57 -04001414 if (!Mount(true))
1415 return false;
1416
Ethan Yonker74db1572015-10-28 12:44:49 -05001417 gui_msg(Msg("wiping=Wiping {1}")(Backup_Display_Name));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001418 TWFunc::removeDir(Mount_Point + "/.android_secure/", true);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001419 return true;
Dees_Troye58d5262012-09-21 12:27:57 -04001420}
1421
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001422bool TWPartition::Can_Repair() {
Ethan Yonkera2719152015-05-28 09:44:41 -05001423 if (Mount_Read_Only)
1424 return false;
Matt Mower18794c82015-11-11 16:22:45 -06001425 if (Current_File_System == "vfat" && TWFunc::Path_Exists("/sbin/fsck.fat"))
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001426 return true;
1427 else if ((Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") && TWFunc::Path_Exists("/sbin/e2fsck"))
1428 return true;
1429 else if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/fsck.exfat"))
1430 return true;
1431 else if (Current_File_System == "f2fs" && TWFunc::Path_Exists("/sbin/fsck.f2fs"))
1432 return true;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001433 else if (Current_File_System == "ntfs" && (TWFunc::Path_Exists("/sbin/ntfsfix") || TWFunc::Path_Exists("/sbin/fsck.ntfs")))
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001434 return true;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001435 return false;
1436}
1437
1438bool TWPartition::Repair() {
1439 string command;
1440
1441 if (Current_File_System == "vfat") {
Matt Mower18794c82015-11-11 16:22:45 -06001442 if (!TWFunc::Path_Exists("/sbin/fsck.fat")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001443 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.fat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001444 return false;
1445 }
1446 if (!UnMount(true))
1447 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001448 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.fat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001449 Find_Actual_Block_Device();
Matt Mower18794c82015-11-11 16:22:45 -06001450 command = "/sbin/fsck.fat -y " + Actual_Block_Device;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001451 LOGINFO("Repair command: %s\n", command.c_str());
1452 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001453 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001454 return true;
1455 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001456 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001457 return false;
1458 }
1459 }
1460 if (Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") {
1461 if (!TWFunc::Path_Exists("/sbin/e2fsck")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001462 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("e2fsck"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001463 return false;
1464 }
1465 if (!UnMount(true))
1466 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001467 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("e2fsck"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001468 Find_Actual_Block_Device();
Ethan Yonkera2719152015-05-28 09:44:41 -05001469 command = "/sbin/e2fsck -fp " + Actual_Block_Device;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001470 LOGINFO("Repair command: %s\n", command.c_str());
1471 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001472 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001473 return true;
1474 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001475 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001476 return false;
1477 }
1478 }
1479 if (Current_File_System == "exfat") {
1480 if (!TWFunc::Path_Exists("/sbin/fsck.exfat")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001481 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.exfat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001482 return false;
1483 }
1484 if (!UnMount(true))
1485 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001486 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.exfat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001487 Find_Actual_Block_Device();
1488 command = "/sbin/fsck.exfat " + Actual_Block_Device;
1489 LOGINFO("Repair command: %s\n", command.c_str());
1490 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001491 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001492 return true;
1493 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001494 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001495 return false;
1496 }
1497 }
1498 if (Current_File_System == "f2fs") {
1499 if (!TWFunc::Path_Exists("/sbin/fsck.f2fs")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001500 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.f2fs"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001501 return false;
1502 }
1503 if (!UnMount(true))
1504 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001505 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.f2fs"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001506 Find_Actual_Block_Device();
1507 command = "/sbin/fsck.f2fs " + Actual_Block_Device;
1508 LOGINFO("Repair command: %s\n", command.c_str());
1509 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001510 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001511 return true;
1512 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001513 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001514 return false;
1515 }
1516 }
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001517 if (Current_File_System == "ntfs") {
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001518 string Ntfsfix_Binary;
1519 if (TWFunc::Path_Exists("/sbin/ntfsfix"))
1520 Ntfsfix_Binary = "ntfsfix";
1521 else if (TWFunc::Path_Exists("/sbin/fsck.ntfs"))
1522 Ntfsfix_Binary = "fsck.ntfs";
1523 else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001524 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("ntfsfix"));
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001525 return false;
1526 }
1527 if (!UnMount(true))
1528 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001529 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)(Ntfsfix_Binary));
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001530 Find_Actual_Block_Device();
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001531 command = "/sbin/" + Ntfsfix_Binary + " " + Actual_Block_Device;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001532 LOGINFO("Repair command: %s\n", command.c_str());
1533 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001534 gui_msg("done=Done.");
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001535 return true;
1536 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001537 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001538 return false;
1539 }
1540 }
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001541 return false;
1542}
1543
Ethan Yonkera2719152015-05-28 09:44:41 -05001544bool TWPartition::Can_Resize() {
1545 if (Mount_Read_Only)
1546 return false;
1547 if ((Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") && TWFunc::Path_Exists("/sbin/resize2fs"))
1548 return true;
1549 return false;
1550}
1551
1552bool TWPartition::Resize() {
1553 string command;
1554
1555 if (Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") {
1556 if (!Can_Repair()) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001557 LOGINFO("Cannot resize %s because %s cannot be repaired before resizing.\n", Display_Name.c_str(), Display_Name.c_str());
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 if (!TWFunc::Path_Exists("/sbin/resize2fs")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001562 LOGINFO("resize2fs does not exist! Cannot resize!\n");
1563 gui_msg(Msg(msg::kError, "cannot_resize=Cannot resize {1}.")(Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001564 return false;
1565 }
1566 // Repair will unmount so no need to do it twice
Ethan Yonker74db1572015-10-28 12:44:49 -05001567 gui_msg(Msg("repair_resize=Repairing {1} before resizing.")( Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001568 if (!Repair())
1569 return false;
Ethan Yonker74db1572015-10-28 12:44:49 -05001570 gui_msg(Msg("resizing=Resizing {1} using {2}...")(Display_Name)("resize2fs"));
Ethan Yonkera2719152015-05-28 09:44:41 -05001571 Find_Actual_Block_Device();
1572 command = "/sbin/resize2fs " + Actual_Block_Device;
1573 if (Length != 0) {
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001574 unsigned long long Actual_Size = IOCTL_Get_Block_Size();
1575 if (Actual_Size == 0)
1576 return false;
Ethan Yonkera2719152015-05-28 09:44:41 -05001577
Ethan Yonkera2719152015-05-28 09:44:41 -05001578 unsigned long long Block_Count;
1579 if (Length < 0) {
1580 // Reduce overall size by this length
1581 Block_Count = (Actual_Size / 1024LLU) - ((unsigned long long)(Length * -1) / 1024LLU);
1582 } else {
1583 // This is the size, not a size reduction
1584 Block_Count = ((unsigned long long)(Length) / 1024LLU);
1585 }
1586 char temp[256];
1587 sprintf(temp, "%llu", Block_Count);
1588 command += " ";
1589 command += temp;
1590 command += "K";
1591 }
1592 LOGINFO("Resize command: %s\n", command.c_str());
1593 if (TWFunc::Exec_Cmd(command) == 0) {
1594 Update_Size(true);
Ethan Yonker74db1572015-10-28 12:44:49 -05001595 gui_msg("done=Done.");
Ethan Yonkera2719152015-05-28 09:44:41 -05001596 return true;
1597 } else {
1598 Update_Size(true);
Ethan Yonker74db1572015-10-28 12:44:49 -05001599 gui_msg(Msg(msg::kError, "unable_resize=Unable to resize {1}.")(Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001600 return false;
1601 }
1602 }
1603 return false;
1604}
1605
bigbiffce8f83c2015-12-12 18:30:21 -05001606bool TWPartition::Backup(PartitionSettings *part_settings, pid_t *tar_fork_pid) {
1607 if (Backup_Method == BM_FILES)
1608 return Backup_Tar(part_settings, tar_fork_pid);
1609 else if (Backup_Method == BM_DD)
1610 return Backup_Image(part_settings);
1611 else if (Backup_Method == BM_FLASH_UTILS)
1612 return Backup_Dump_Image(part_settings);
Dees_Troy2673cec2013-04-02 20:22:16 +00001613 LOGERR("Unknown backup method for '%s'\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001614 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001615}
1616
James Christopher Adduono79ae0932016-10-25 02:18:32 -04001617bool TWPartition::Check_Restore_File_MD5(const string& Filename) {
1618 twrpDigest md5sum;
1619
1620 md5sum.setfn(Filename);
1621 switch (md5sum.verify_md5digest()) {
1622 case MD5_OK:
1623 gui_msg(Msg("md5_matched=MD5 matched for '{1}'.")(Filename));
1624 return true;
1625 case MD5_FILE_UNREADABLE:
1626 case MD5_NOT_FOUND:
1627 gui_msg(Msg(msg::kError, "no_md5_found=No md5 file found for '{1}'. Please unselect Enable MD5 verification to restore.")(Filename));
1628 break;
1629 case MD5_MATCH_FAIL:
1630 gui_msg(Msg(msg::kError, "md5_fail_match=MD5 failed to match on '{1}'.")(Filename));
1631 break;
1632 }
1633 return false;
1634}
1635
bigbiff bigbiff8fd4b092016-08-30 20:48:53 -04001636bool TWPartition::Check_MD5(PartitionSettings *part_settings) {
James Christopher Adduono79ae0932016-10-25 02:18:32 -04001637 string Full_Filename;
Dees_Troy43d8b002012-09-17 16:00:01 -04001638 char split_filename[512];
1639 int index = 0;
1640
Captain Throwbackff5935f2014-09-23 16:08:34 -04001641 sync();
1642
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05001643 Full_Filename = part_settings->Backup_Folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04001644 if (!TWFunc::Path_Exists(Full_Filename)) {
1645 // This is a split archive, we presume
James Christopher Adduono79ae0932016-10-25 02:18:32 -04001646 memset(split_filename, 0, sizeof(split_filename));
Dees_Troy83bd4832013-05-04 12:39:56 +00001647 while (index < 1000) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001648 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
James Christopher Adduono79ae0932016-10-25 02:18:32 -04001649 if (!TWFunc::Path_Exists(split_filename))
1650 break;
1651 LOGINFO("split_filename: %s\n", split_filename);
1652 if (!Check_Restore_File_MD5(split_filename))
1653 return false;
1654 index++;
Dees_Troy43d8b002012-09-17 16:00:01 -04001655 }
Dees_Troy4a2a1262012-09-18 09:33:47 -04001656 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001657 }
James Christopher Adduono79ae0932016-10-25 02:18:32 -04001658 return Check_Restore_File_MD5(Full_Filename); // Single file archive
Dees_Troy43d8b002012-09-17 16:00:01 -04001659}
1660
bigbiffce8f83c2015-12-12 18:30:21 -05001661bool TWPartition::Restore(PartitionSettings *part_settings) {
Matt Mower3c366972015-12-25 19:28:31 -06001662 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, gui_parse_text("{@restoring_hdr}"));
Ethan Yonkere080c1f2016-09-19 13:50:25 -05001663 LOGINFO("Restore filename is: %s/%s\n", part_settings->Backup_Folder.c_str(), Backup_FileName.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001664
bigbiffce8f83c2015-12-12 18:30:21 -05001665 string Restore_File_System = Get_Restore_File_System(part_settings);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001666
1667 if (Is_File_System(Restore_File_System))
bigbiffce8f83c2015-12-12 18:30:21 -05001668 return Restore_Tar(part_settings);
1669 else if (Is_Image(Restore_File_System))
1670 return Restore_Image(part_settings);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001671
1672 LOGERR("Unknown restore method for '%s'\n", Mount_Point.c_str());
1673 return false;
1674}
1675
bigbiffce8f83c2015-12-12 18:30:21 -05001676string TWPartition::Get_Restore_File_System(PartitionSettings *part_settings) {
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001677 size_t first_period, second_period;
1678 string Restore_File_System;
1679
Gary Peck43acadf2012-11-21 21:19:01 -08001680 // Parse backup filename to extract the file system before wiping
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05001681 first_period = Backup_FileName.find(".");
Gary Peck43acadf2012-11-21 21:19:01 -08001682 if (first_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001683 LOGERR("Unable to find file system (first period).\n");
thatd43bf2d2014-09-21 23:13:02 +02001684 return string();
Gary Peck43acadf2012-11-21 21:19:01 -08001685 }
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05001686 Restore_File_System = Backup_FileName.substr(first_period + 1, Backup_FileName.size() - first_period - 1);
Gary Peck43acadf2012-11-21 21:19:01 -08001687 second_period = Restore_File_System.find(".");
1688 if (second_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001689 LOGERR("Unable to find file system (second period).\n");
thatd43bf2d2014-09-21 23:13:02 +02001690 return string();
Gary Peck43acadf2012-11-21 21:19:01 -08001691 }
1692 Restore_File_System.resize(second_period);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001693 return Restore_File_System;
Dees_Troy51a0e822012-09-05 15:24:24 -04001694}
1695
1696string TWPartition::Backup_Method_By_Name() {
bigbiffce8f83c2015-12-12 18:30:21 -05001697 if (Backup_Method == BM_NONE)
Dees_Troy38bd7602012-09-14 13:33:53 -04001698 return "none";
bigbiffce8f83c2015-12-12 18:30:21 -05001699 else if (Backup_Method == BM_FILES)
Dees_Troy38bd7602012-09-14 13:33:53 -04001700 return "files";
bigbiffce8f83c2015-12-12 18:30:21 -05001701 else if (Backup_Method == BM_DD)
Dees_Troy38bd7602012-09-14 13:33:53 -04001702 return "dd";
bigbiffce8f83c2015-12-12 18:30:21 -05001703 else if (Backup_Method == BM_FLASH_UTILS)
Dees_Troy38bd7602012-09-14 13:33:53 -04001704 return "flash_utils";
1705 else
1706 return "undefined";
1707 return "ERROR!";
Dees_Troy51a0e822012-09-05 15:24:24 -04001708}
1709
1710bool TWPartition::Decrypt(string Password) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001711 LOGINFO("STUB TWPartition::Decrypt, password: '%s'\n", Password.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001712 // Is this needed?
Dees_Troy51a0e822012-09-05 15:24:24 -04001713 return 1;
1714}
1715
1716bool TWPartition::Wipe_Encryption() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001717 bool Save_Data_Media = Has_Data_Media;
1718
1719 if (!UnMount(true))
1720 return false;
1721
Dees_Troy38bd7602012-09-14 13:33:53 -04001722 Has_Data_Media = false;
Dees_Troy74fb2e92013-04-15 14:35:47 +00001723 Decrypted_Block_Device = "";
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001724#ifdef TW_INCLUDE_CRYPTO
1725 if (Is_Decrypted) {
1726 if (!UnMount(true))
1727 return false;
Matt Mower2b18a532015-02-20 16:58:05 -06001728 if (delete_crypto_blk_dev((char*)("userdata")) != 0) {
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001729 LOGERR("Error deleting crypto block device, continuing anyway.\n");
1730 }
1731 }
1732#endif
Dees_Troy74fb2e92013-04-15 14:35:47 +00001733 Is_Decrypted = false;
1734 Is_Encrypted = false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001735 Find_Actual_Block_Device();
Ethan Yonkerbc85b632015-08-09 12:48:14 -05001736 if (Crypto_Key_Location == "footer") {
1737 int newlen, fd;
1738 if (Length != 0) {
1739 newlen = Length;
1740 if (newlen < 0)
1741 newlen = newlen * -1;
1742 } else {
1743 newlen = CRYPT_FOOTER_OFFSET;
1744 }
1745 if ((fd = open(Actual_Block_Device.c_str(), O_RDWR)) < 0) {
1746 gui_print_color("warning", "Unable to open '%s' to wipe crypto key\n", Actual_Block_Device.c_str());
1747 } else {
1748 unsigned int block_count;
1749 if ((ioctl(fd, BLKGETSIZE, &block_count)) == -1) {
1750 gui_print_color("warning", "Unable to get block size for wiping crypto footer.\n");
1751 } else {
1752 off64_t offset = ((off64_t)block_count * 512) - newlen;
1753 if (lseek64(fd, offset, SEEK_SET) == -1) {
1754 gui_print_color("warning", "Unable to lseek64 for wiping crypto footer.\n");
1755 } else {
1756 void* buffer = malloc(newlen);
1757 if (!buffer) {
1758 gui_print_color("warning", "Failed to malloc for wiping crypto footer.\n");
1759 } else {
1760 memset(buffer, 0, newlen);
1761 int ret = write(fd, buffer, newlen);
1762 if (ret != newlen) {
1763 gui_print_color("warning", "Failed to wipe crypto footer.\n");
1764 } else {
1765 LOGINFO("Successfully wiped crypto footer.\n");
1766 }
Matt Mower13a8f0b2015-09-26 15:40:03 -05001767 free(buffer);
Ethan Yonkerbc85b632015-08-09 12:48:14 -05001768 }
1769 }
1770 }
1771 close(fd);
1772 }
1773 } else {
Ethan Yonkerc2dafbb2016-03-15 22:20:59 -05001774 if (TWFunc::IOCTL_Get_Block_Size(Crypto_Key_Location.c_str()) >= 16384LLU) {
1775 string Command = "dd of='" + Crypto_Key_Location + "' if=/dev/zero bs=16384 count=1";
1776 TWFunc::Exec_Cmd(Command);
1777 } else {
1778 LOGINFO("Crypto key location reports size < 16K so not wiping crypto footer.\n");
1779 }
Ethan Yonkerbc85b632015-08-09 12:48:14 -05001780 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001781 if (Wipe(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001782 Has_Data_Media = Save_Data_Media;
1783 if (Has_Data_Media && !Symlink_Mount_Point.empty()) {
1784 Recreate_Media_Folder();
Ethan Yonker726a0202014-12-16 20:01:38 -06001785 if (Mount(false))
1786 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04001787 }
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001788 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
Ethan Yonker83e82572014-04-04 10:59:28 -05001789#ifndef TW_OEM_BUILD
Ethan Yonker74db1572015-10-28 12:44:49 -05001790 gui_msg("format_data_msg=You may need to reboot recovery to be able to use /data again.");
Ethan Yonker83e82572014-04-04 10:59:28 -05001791#endif
Dees_Troy38bd7602012-09-14 13:33:53 -04001792 return true;
1793 } else {
1794 Has_Data_Media = Save_Data_Media;
Ethan Yonker74db1572015-10-28 12:44:49 -05001795 gui_err("format_data_err=Unable to format to remove encryption.");
Ethan Yonker726a0202014-12-16 20:01:38 -06001796 if (Has_Data_Media && Mount(false))
1797 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04001798 return false;
1799 }
1800 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001801}
1802
1803void TWPartition::Check_FS_Type() {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001804 const char* type;
1805 blkid_probe pr;
Dees_Troy5bf43922012-09-07 16:07:55 -04001806
Dees_Troy68cab492012-12-12 19:29:35 +00001807 if (Fstab_File_System == "yaffs2" || Fstab_File_System == "mtd" || Fstab_File_System == "bml" || Ignore_Blkid)
1808 return; // Running blkid on some mtd devices causes a massive crash or needs to be skipped
Dees_Troy5bf43922012-09-07 16:07:55 -04001809
Dees_Troy38bd7602012-09-14 13:33:53 -04001810 Find_Actual_Block_Device();
Dees_Troy8170a922012-09-18 15:40:25 -04001811 if (!Is_Present)
1812 return;
Dees_Troy51127312012-09-08 13:08:49 -04001813
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001814 pr = blkid_new_probe_from_filename(Actual_Block_Device.c_str());
1815 if (blkid_do_fullprobe(pr)) {
1816 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00001817 LOGINFO("Can't probe device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001818 return;
Dees_Troy5bf43922012-09-07 16:07:55 -04001819 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001820
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05001821 if (blkid_probe_lookup_value(pr, "TYPE", &type, NULL) < 0) {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001822 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00001823 LOGINFO("can't find filesystem on device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001824 return;
1825 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001826
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001827 Current_File_System = type;
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001828 blkid_free_probe(pr);
Dees_Troy51a0e822012-09-05 15:24:24 -04001829}
1830
Gary Peck43acadf2012-11-21 21:19:01 -08001831bool TWPartition::Wipe_EXT23(string File_System) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001832 if (!UnMount(true))
1833 return false;
1834
Dees_Troy43d8b002012-09-17 16:00:01 -04001835 if (TWFunc::Path_Exists("/sbin/mke2fs")) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001836 string command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001837
Greg Wallace4b44fef2015-12-29 15:33:24 -05001838 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mke2fs"));
Dees_Troy38bd7602012-09-14 13:33:53 -04001839 Find_Actual_Block_Device();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001840 command = "mke2fs -t " + File_System + " -m 0 " + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001841 LOGINFO("mke2fs command: %s\n", command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001842 if (TWFunc::Exec_Cmd(command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001843 Current_File_System = File_System;
Dees_Troye58d5262012-09-21 12:27:57 -04001844 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05001845 gui_msg("done=Done.");
Dees_Troy38bd7602012-09-14 13:33:53 -04001846 return true;
1847 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001848 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04001849 return false;
1850 }
1851 } else
1852 return Wipe_RMRF();
1853
1854 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001855}
1856
1857bool TWPartition::Wipe_EXT4() {
Ethan Yonker25f20c12014-10-14 09:04:43 -05001858 Find_Actual_Block_Device();
1859 if (!Is_Present) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001860 LOGINFO("Block device not present, cannot wipe %s.\n", Display_Name.c_str());
1861 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Ethan Yonker25f20c12014-10-14 09:04:43 -05001862 return false;
1863 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001864 if (!UnMount(true))
1865 return false;
1866
Dees_Troyb3265ab2013-08-30 02:59:14 +00001867#if defined(HAVE_SELINUX) && defined(USE_EXT4)
Ethan Yonkerf27497f2014-02-09 11:48:33 -06001868 int ret;
1869 char *secontext = NULL;
1870
Greg Wallace4b44fef2015-12-29 15:33:24 -05001871 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("make_ext4fs"));
Ethan Yonkerf27497f2014-02-09 11:48:33 -06001872
Dees Troy99c8dbf2014-03-10 16:53:58 +00001873 if (!selinux_handle || selabel_lookup(selinux_handle, &secontext, Mount_Point.c_str(), S_IFDIR) < 0) {
Ethan Yonkerf27497f2014-02-09 11:48:33 -06001874 LOGINFO("Cannot lookup security context for '%s'\n", Mount_Point.c_str());
1875 ret = make_ext4fs(Actual_Block_Device.c_str(), Length, Mount_Point.c_str(), NULL);
1876 } else {
1877 ret = make_ext4fs(Actual_Block_Device.c_str(), Length, Mount_Point.c_str(), selinux_handle);
1878 }
1879 if (ret != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001880 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troya95f55c2013-08-17 13:14:43 +00001881 return false;
1882 } else {
bigbiff bigbiffc49d7062013-10-11 20:28:00 -04001883 string sedir = Mount_Point + "/lost+found";
1884 PartitionManager.Mount_By_Path(sedir.c_str(), true);
1885 rmdir(sedir.c_str());
1886 mkdir(sedir.c_str(), S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP);
Dees_Troya95f55c2013-08-17 13:14:43 +00001887 return true;
1888 }
1889#else
Dees_Troy43d8b002012-09-17 16:00:01 -04001890 if (TWFunc::Path_Exists("/sbin/make_ext4fs")) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001891 string Command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001892
Greg Wallace4b44fef2015-12-29 15:33:24 -05001893 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("make_ext4fs"));
Dees_Troy38bd7602012-09-14 13:33:53 -04001894 Find_Actual_Block_Device();
1895 Command = "make_ext4fs";
1896 if (!Is_Decrypted && Length != 0) {
1897 // Only use length if we're not decrypted
1898 char len[32];
1899 sprintf(len, "%i", Length);
1900 Command += " -l ";
1901 Command += len;
1902 }
Dees_Troy5295d582013-09-06 15:51:08 +00001903 if (TWFunc::Path_Exists("/file_contexts")) {
1904 Command += " -S /file_contexts";
1905 }
1906 Command += " -a " + Mount_Point + " " + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001907 LOGINFO("make_ext4fs command: %s\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001908 if (TWFunc::Exec_Cmd(Command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001909 Current_File_System = "ext4";
Dees_Troye58d5262012-09-21 12:27:57 -04001910 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05001911 gui_msg("done=Done.");
Dees_Troy38bd7602012-09-14 13:33:53 -04001912 return true;
1913 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001914 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04001915 return false;
1916 }
1917 } else
Gary Peck43acadf2012-11-21 21:19:01 -08001918 return Wipe_EXT23("ext4");
Dees_Troya95f55c2013-08-17 13:14:43 +00001919#endif
Dees_Troy38bd7602012-09-14 13:33:53 -04001920 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001921}
1922
1923bool TWPartition::Wipe_FAT() {
Vojtech Bocek05534202013-09-11 08:11:56 +02001924 string command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001925
Matt Mower18794c82015-11-11 16:22:45 -06001926 if (TWFunc::Path_Exists("/sbin/mkfs.fat")) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001927 if (!UnMount(true))
1928 return false;
1929
Greg Wallace4b44fef2015-12-29 15:33:24 -05001930 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mkfs.fat"));
Dees_Troy38bd7602012-09-14 13:33:53 -04001931 Find_Actual_Block_Device();
Matt Mower18794c82015-11-11 16:22:45 -06001932 command = "mkfs.fat " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02001933 if (TWFunc::Exec_Cmd(command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001934 Current_File_System = "vfat";
Dees_Troye58d5262012-09-21 12:27:57 -04001935 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05001936 gui_msg("done=Done.");
Dees_Troy38bd7602012-09-14 13:33:53 -04001937 return true;
1938 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001939 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04001940 return false;
1941 }
1942 return true;
1943 }
1944 else
1945 return Wipe_RMRF();
1946
1947 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001948}
1949
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001950bool TWPartition::Wipe_EXFAT() {
Vojtech Bocek05534202013-09-11 08:11:56 +02001951 string command;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001952
1953 if (TWFunc::Path_Exists("/sbin/mkexfatfs")) {
1954 if (!UnMount(true))
1955 return false;
1956
Greg Wallace4b44fef2015-12-29 15:33:24 -05001957 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mkexfatfs"));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001958 Find_Actual_Block_Device();
1959 command = "mkexfatfs " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02001960 if (TWFunc::Exec_Cmd(command) == 0) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001961 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05001962 gui_msg("done=Done.");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001963 return true;
1964 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001965 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001966 return false;
1967 }
1968 return true;
1969 }
1970 return false;
1971}
1972
Dees_Troy38bd7602012-09-14 13:33:53 -04001973bool TWPartition::Wipe_MTD() {
1974 if (!UnMount(true))
1975 return false;
1976
Greg Wallace4b44fef2015-12-29 15:33:24 -05001977 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("MTD"));
Dees_Troy38bd7602012-09-14 13:33:53 -04001978
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001979 mtd_scan_partitions();
1980 const MtdPartition* mtd = mtd_find_partition_by_name(MTD_Name.c_str());
1981 if (mtd == NULL) {
1982 LOGERR("No mtd partition named '%s'", MTD_Name.c_str());
1983 return false;
1984 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001985
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001986 MtdWriteContext* ctx = mtd_write_partition(mtd);
1987 if (ctx == NULL) {
1988 LOGERR("Can't write '%s', failed to format.", MTD_Name.c_str());
1989 return false;
1990 }
1991 if (mtd_erase_blocks(ctx, -1) == -1) {
1992 mtd_write_close(ctx);
1993 LOGERR("Failed to format '%s'", MTD_Name.c_str());
1994 return false;
1995 }
1996 if (mtd_write_close(ctx) != 0) {
1997 LOGERR("Failed to close '%s'", MTD_Name.c_str());
1998 return false;
1999 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08002000 Current_File_System = "yaffs2";
Dees_Troye58d5262012-09-21 12:27:57 -04002001 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05002002 gui_msg("done=Done.");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02002003 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002004}
2005
2006bool TWPartition::Wipe_RMRF() {
Dees_Troy38bd7602012-09-14 13:33:53 -04002007 if (!Mount(true))
2008 return false;
Ethan Yonker726a0202014-12-16 20:01:38 -06002009 // This is the only wipe that leaves the partition mounted, so we
2010 // must manually remove the partition from MTP if it is a storage
2011 // partition.
2012 if (Is_Storage)
2013 PartitionManager.Remove_MTP_Storage(MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04002014
Ethan Yonker74db1572015-10-28 12:44:49 -05002015 gui_msg(Msg("remove_all=Removing all files under '{1}'")(Mount_Point));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002016 TWFunc::removeDir(Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04002017 Recreate_AndSec_Folder();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002018 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002019}
2020
Dees_Troye5017042013-08-29 16:38:55 +00002021bool TWPartition::Wipe_F2FS() {
Vojtech Bocek05534202013-09-11 08:11:56 +02002022 string command;
Dees_Troye5017042013-08-29 16:38:55 +00002023
2024 if (TWFunc::Path_Exists("/sbin/mkfs.f2fs")) {
2025 if (!UnMount(true))
2026 return false;
2027
Greg Wallace4b44fef2015-12-29 15:33:24 -05002028 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mkfs.f2fs"));
Dees_Troye5017042013-08-29 16:38:55 +00002029 Find_Actual_Block_Device();
Greg Wallace558aef72016-01-19 21:14:30 -05002030 command = "mkfs.f2fs -t 0";
dhacker29a3fa75f2015-01-17 19:42:33 -05002031 if (!Is_Decrypted && Length != 0) {
2032 // Only use length if we're not decrypted
2033 char len[32];
2034 int mod_length = Length;
2035 if (Length < 0)
2036 mod_length *= -1;
2037 sprintf(len, "%i", mod_length);
2038 command += " -r ";
2039 command += len;
2040 }
2041 command += " " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02002042 if (TWFunc::Exec_Cmd(command) == 0) {
Dees_Troye5017042013-08-29 16:38:55 +00002043 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05002044 gui_msg("done=Done.");
Dees_Troye5017042013-08-29 16:38:55 +00002045 return true;
2046 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002047 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troye5017042013-08-29 16:38:55 +00002048 return false;
2049 }
2050 return true;
2051 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002052 LOGINFO("mkfs.f2fs binary not found, using rm -rf to wipe.\n");
Dees_Troye5017042013-08-29 16:38:55 +00002053 return Wipe_RMRF();
2054 }
2055 return false;
2056}
2057
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002058bool TWPartition::Wipe_NTFS() {
2059 string command;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002060 string Ntfsmake_Binary;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002061
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002062 if (TWFunc::Path_Exists("/sbin/mkntfs"))
2063 Ntfsmake_Binary = "mkntfs";
2064 else if (TWFunc::Path_Exists("/sbin/mkfs.ntfs"))
2065 Ntfsmake_Binary = "mkfs.ntfs";
2066 else
2067 return false;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002068
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002069 if (!UnMount(true))
2070 return false;
2071
Greg Wallace4b44fef2015-12-29 15:33:24 -05002072 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)(Ntfsmake_Binary));
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002073 Find_Actual_Block_Device();
2074 command = "/sbin/" + Ntfsmake_Binary + " " + Actual_Block_Device;
2075 if (TWFunc::Exec_Cmd(command) == 0) {
2076 Recreate_AndSec_Folder();
2077 gui_msg("done=Done.");
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002078 return true;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002079 } else {
2080 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
2081 return false;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002082 }
2083 return false;
2084}
2085
Dees_Troy51a0e822012-09-05 15:24:24 -04002086bool TWPartition::Wipe_Data_Without_Wiping_Media() {
Ethan Yonker83e82572014-04-04 10:59:28 -05002087#ifdef TW_OEM_BUILD
2088 // In an OEM Build we want to do a full format
2089 return Wipe_Encryption();
2090#else
Ethan Yonker66a19492015-12-10 10:19:45 -06002091 bool ret = false;
Dees_Troy38bd7602012-09-14 13:33:53 -04002092
Dees_Troy38bd7602012-09-14 13:33:53 -04002093 if (!Mount(true))
2094 return false;
2095
Ethan Yonker74db1572015-10-28 12:44:49 -05002096 gui_msg("wiping_data=Wiping data without wiping /data/media ...");
Ethan Yonker66a19492015-12-10 10:19:45 -06002097 ret = Wipe_Data_Without_Wiping_Media_Func(Mount_Point + "/");
2098 if (ret)
2099 gui_msg("done=Done.");
2100 return ret;
2101#endif // ifdef TW_OEM_BUILD
2102}
2103
2104bool TWPartition::Wipe_Data_Without_Wiping_Media_Func(const string& parent __unused) {
2105 string dir;
Dees_Troy38bd7602012-09-14 13:33:53 -04002106
2107 DIR* d;
Ethan Yonker66a19492015-12-10 10:19:45 -06002108 d = opendir(parent.c_str());
Dees_Troy16b74352012-11-14 22:27:31 +00002109 if (d != NULL) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002110 struct dirent* de;
2111 while ((de = readdir(d)) != NULL) {
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002112 if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) continue;
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05002113
Ethan Yonker66a19492015-12-10 10:19:45 -06002114 dir = parent;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002115 dir.append(de->d_name);
Ethan Yonker66a19492015-12-10 10:19:45 -06002116 if (du.check_skip_dirs(dir)) {
2117 LOGINFO("skipped '%s'\n", dir.c_str());
2118 continue;
2119 }
Dees_Troyce675462013-01-09 19:48:21 +00002120 if (de->d_type == DT_DIR) {
Ethan Yonker66a19492015-12-10 10:19:45 -06002121 dir.append("/");
2122 if (!Wipe_Data_Without_Wiping_Media_Func(dir)) {
2123 closedir(d);
2124 return false;
2125 }
2126 rmdir(dir.c_str());
bigbiff bigbiff98f1f902013-01-19 18:46:13 -05002127 } else if (de->d_type == DT_REG || de->d_type == DT_LNK || de->d_type == DT_FIFO || de->d_type == DT_SOCK) {
Dees_Troyce675462013-01-09 19:48:21 +00002128 if (!unlink(dir.c_str()))
Dees_Troy2673cec2013-04-02 20:22:16 +00002129 LOGINFO("Unable to unlink '%s'\n", dir.c_str());
Dees_Troyce675462013-01-09 19:48:21 +00002130 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002131 }
2132 closedir(d);
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05002133
Dees_Troy16b74352012-11-14 22:27:31 +00002134 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -04002135 }
Ethan Yonker74db1572015-10-28 12:44:49 -05002136 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy16b74352012-11-14 22:27:31 +00002137 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04002138}
2139
bigbiffce8f83c2015-12-12 18:30:21 -05002140bool TWPartition::Backup_Tar(PartitionSettings *part_settings, pid_t *tar_fork_pid) {
Ethan Yonker472f5062016-02-25 13:47:30 -06002141 string Full_FileName;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002142 twrpTar tar;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05002143
Dees_Troy43d8b002012-09-17 16:00:01 -04002144 if (!Mount(true))
2145 return false;
2146
Dees_Troya13d74f2013-03-24 08:54:55 -05002147 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Backup_Display_Name, "Backing Up");
Ethan Yonker74db1572015-10-28 12:44:49 -05002148 gui_msg(Msg("backing_up=Backing up {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002149
Ethan Yonker472f5062016-02-25 13:47:30 -06002150 DataManager::GetValue(TW_USE_COMPRESSION_VAR, tar.use_compression);
Matt Mowerbb81e5d2014-03-20 18:05:41 -05002151
Dees_Troy83bd4832013-05-04 12:39:56 +00002152#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
Ethan Yonker472f5062016-02-25 13:47:30 -06002153 if (Can_Encrypt_Backup) {
2154 DataManager::GetValue("tw_encrypt_backup", tar.use_encryption);
2155 if (tar.use_encryption) {
2156 if (Use_Userdata_Encryption)
2157 tar.userdata_encryption = tar.use_encryption;
2158 string Password;
2159 DataManager::GetValue("tw_backup_password", Password);
2160 tar.setpassword(Password);
2161 } else {
2162 tar.use_encryption = 0;
2163 }
Dees_Troy83bd4832013-05-04 12:39:56 +00002164 }
2165#endif
Dees_Troy43d8b002012-09-17 16:00:01 -04002166
Ethan Yonker472f5062016-02-25 13:47:30 -06002167 Backup_FileName = Backup_Name + "." + Current_File_System + ".win";
Ethan Yonkere080c1f2016-09-19 13:50:25 -05002168 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
Dees_Troy83bd4832013-05-04 12:39:56 +00002169 tar.has_data_media = Has_Data_Media;
bigbiffce8f83c2015-12-12 18:30:21 -05002170 tar.part_settings = part_settings;
Dees Troye0a433a2013-12-02 04:10:37 +00002171 tar.setdir(Backup_Path);
2172 tar.setfn(Full_FileName);
2173 tar.setsize(Backup_Size);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002174 tar.partition_name = Backup_Name;
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002175 tar.backup_folder = part_settings->Backup_Folder;
bigbiffce8f83c2015-12-12 18:30:21 -05002176 if (tar.createTarFork(tar_fork_pid) != 0)
Dees Troye0a433a2013-12-02 04:10:37 +00002177 return false;
Dees_Troy43d8b002012-09-17 16:00:01 -04002178 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002179}
2180
bigbiffce8f83c2015-12-12 18:30:21 -05002181bool TWPartition::Backup_Image(PartitionSettings *part_settings) {
2182 string Full_FileName, adb_file_name;
2183 int adb_control_bu_fd, compressed;
igoriok87e3d932013-01-31 21:03:53 +02002184
Ethan Yonker74db1572015-10-28 12:44:49 -05002185 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, gui_parse_text("{@backing}"));
2186 gui_msg(Msg("backing_up=Backing up {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002187
Ethan Yonker472f5062016-02-25 13:47:30 -06002188 Backup_FileName = Backup_Name + "." + Current_File_System + ".win";
Dees_Troy43d8b002012-09-17 16:00:01 -04002189
bigbiffce8f83c2015-12-12 18:30:21 -05002190 if (part_settings->adbbackup) {
2191 Full_FileName = TW_ADB_BACKUP;
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002192 adb_file_name = part_settings->Backup_Folder + "/" + Backup_FileName;
bigbiffce8f83c2015-12-12 18:30:21 -05002193 }
2194 else
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002195 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
bigbiffce8f83c2015-12-12 18:30:21 -05002196
2197 part_settings->total_restore_size = Backup_Size;
2198
2199 if (part_settings->adbbackup) {
2200 if (!twadbbu::Write_TWIMG(adb_file_name, Backup_Size))
2201 return false;
2202 }
2203
2204 if (!Raw_Read_Write(part_settings))
Ethan Yonker472f5062016-02-25 13:47:30 -06002205 return false;
2206
bigbiffce8f83c2015-12-12 18:30:21 -05002207 if (part_settings->adbbackup) {
2208 if (!twadbbu::Write_TWEOF())
2209 return false;
Dees_Troy7c2dec82012-09-26 09:49:14 -04002210 }
Dees_Troy43d8b002012-09-17 16:00:01 -04002211 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002212}
2213
bigbiffce8f83c2015-12-12 18:30:21 -05002214bool TWPartition::Raw_Read_Write(PartitionSettings *part_settings) {
2215 unsigned long long RW_Block_Size, Remain = Backup_Size;
2216 int src_fd = -1, dest_fd = -1;
2217 ssize_t bs;
Ethan Yonker472f5062016-02-25 13:47:30 -06002218 bool ret = false;
2219 void* buffer = NULL;
2220 unsigned long long backedup_size = 0;
bigbiffce8f83c2015-12-12 18:30:21 -05002221 string srcfn, destfn;
Ethan Yonker472f5062016-02-25 13:47:30 -06002222
bigbiffce8f83c2015-12-12 18:30:21 -05002223 if (part_settings->PM_Method == PM_BACKUP) {
2224 srcfn = Actual_Block_Device;
2225 if (part_settings->adbbackup)
2226 destfn = TW_ADB_BACKUP;
2227 else
Ethan Yonkere080c1f2016-09-19 13:50:25 -05002228 destfn = part_settings->Backup_Folder + "/" + Backup_FileName;
bigbiffce8f83c2015-12-12 18:30:21 -05002229 }
2230 else {
2231 destfn = Actual_Block_Device;
2232 if (part_settings->adbbackup) {
2233 srcfn = TW_ADB_RESTORE;
2234 } else {
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002235 srcfn = part_settings->Backup_Folder + "/" + Backup_FileName;
bigbiffce8f83c2015-12-12 18:30:21 -05002236 Remain = TWFunc::Get_File_Size(srcfn);
2237 }
2238 }
Ethan Yonker472f5062016-02-25 13:47:30 -06002239
bigbiffce8f83c2015-12-12 18:30:21 -05002240 src_fd = open(srcfn.c_str(), O_RDONLY | O_LARGEFILE);
Ethan Yonker472f5062016-02-25 13:47:30 -06002241 if (src_fd < 0) {
bigbiffce8f83c2015-12-12 18:30:21 -05002242 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(srcfn.c_str())(strerror(errno)));
Ethan Yonker472f5062016-02-25 13:47:30 -06002243 return false;
2244 }
bigbiffce8f83c2015-12-12 18:30:21 -05002245
2246 dest_fd = open(destfn.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_LARGEFILE, S_IRUSR | S_IWUSR);
Ethan Yonker472f5062016-02-25 13:47:30 -06002247 if (dest_fd < 0) {
bigbiffce8f83c2015-12-12 18:30:21 -05002248 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(destfn.c_str())(strerror(errno)));
Ethan Yonker472f5062016-02-25 13:47:30 -06002249 goto exit;
2250 }
bigbiffce8f83c2015-12-12 18:30:21 -05002251
2252 LOGINFO("Reading '%s', writing '%s'\n", srcfn.c_str(), destfn.c_str());
2253
2254 if (part_settings->adbbackup) {
2255 RW_Block_Size = MAX_ADB_READ;
2256 bs = MAX_ADB_READ;
2257 }
2258 else {
2259 RW_Block_Size = 1048576LLU; // 1MB
2260 bs = (ssize_t)(RW_Block_Size);
2261 }
2262
Ethan Yonker472f5062016-02-25 13:47:30 -06002263 buffer = malloc((size_t)bs);
2264 if (!buffer) {
2265 LOGINFO("Raw_Read_Write failed to malloc\n");
2266 goto exit;
2267 }
bigbiffce8f83c2015-12-12 18:30:21 -05002268
2269 if (part_settings->progress)
2270 part_settings->progress->SetPartitionSize(part_settings->total_restore_size);
2271
Ethan Yonker472f5062016-02-25 13:47:30 -06002272 while (Remain > 0) {
2273 if (Remain < RW_Block_Size)
bigbiffce8f83c2015-12-12 18:30:21 -05002274 bs = (ssize_t)(Remain);
2275 if (read(src_fd, buffer, bs) != bs) {
Ethan Yonker472f5062016-02-25 13:47:30 -06002276 LOGINFO("Error reading source fd (%s)\n", strerror(errno));
2277 goto exit;
2278 }
2279 if (write(dest_fd, buffer, bs) != bs) {
2280 LOGINFO("Error writing destination fd (%s)\n", strerror(errno));
2281 goto exit;
2282 }
2283 backedup_size += (unsigned long long)(bs);
bigbiffce8f83c2015-12-12 18:30:21 -05002284 Remain = Remain - (unsigned long long)(bs);
2285 if (part_settings->progress)
2286 part_settings->progress->UpdateSize(backedup_size);
Ethan Yonker472f5062016-02-25 13:47:30 -06002287 if (PartitionManager.Check_Backup_Cancel() != 0)
2288 goto exit;
2289 }
bigbiffce8f83c2015-12-12 18:30:21 -05002290 if (part_settings->progress)
2291 part_settings->progress->UpdateDisplayDetails(true);
Ethan Yonker472f5062016-02-25 13:47:30 -06002292 fsync(dest_fd);
2293 ret = true;
2294exit:
2295 if (src_fd >= 0)
2296 close(src_fd);
2297 if (dest_fd >= 0)
2298 close(dest_fd);
2299 if (buffer)
2300 free(buffer);
2301 return ret;
2302}
2303
bigbiffce8f83c2015-12-12 18:30:21 -05002304bool TWPartition::Backup_Dump_Image(PartitionSettings *part_settings) {
Vojtech Bocek05534202013-09-11 08:11:56 +02002305 string Full_FileName, Command;
bigbiffce8f83c2015-12-12 18:30:21 -05002306 int use_compression, adb_control_bu_fd;
2307 unsigned long long compressed;
Dees_Troy43d8b002012-09-17 16:00:01 -04002308
Ethan Yonker74db1572015-10-28 12:44:49 -05002309 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, gui_parse_text("{@backing}"));
2310 gui_msg(Msg("backing_up=Backing up {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002311
bigbiffce8f83c2015-12-12 18:30:21 -05002312 if (part_settings->progress)
2313 part_settings->progress->SetPartitionSize(Backup_Size);
Dees_Troy43d8b002012-09-17 16:00:01 -04002314
Ethan Yonker472f5062016-02-25 13:47:30 -06002315 Backup_FileName = Backup_Name + "." + Current_File_System + ".win";
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002316 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04002317
2318 Command = "dump_image " + MTD_Name + " '" + Full_FileName + "'";
bigbiffce8f83c2015-12-12 18:30:21 -05002319
Dees_Troy2673cec2013-04-02 20:22:16 +00002320 LOGINFO("Backup command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02002321 TWFunc::Exec_Cmd(Command);
Ethan Yonker4b94cfd2014-12-11 10:00:45 -06002322 tw_set_default_metadata(Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04002323 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
2324 // Actual size may not match backup size due to bad blocks on MTD devices so just check for 0 bytes
Matt Mower3c366972015-12-25 19:28:31 -06002325 gui_msg(Msg(msg::kError, "backup_size=Backup file size for '{1}' is 0 bytes.")(Full_FileName));
Dees_Troy7c2dec82012-09-26 09:49:14 -04002326 return false;
2327 }
bigbiffce8f83c2015-12-12 18:30:21 -05002328 if (part_settings->progress)
2329 part_settings->progress->UpdateSize(Backup_Size);
2330
Dees_Troy43d8b002012-09-17 16:00:01 -04002331 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002332}
2333
bigbiffce8f83c2015-12-12 18:30:21 -05002334unsigned long long TWPartition::Get_Restore_Size(PartitionSettings *part_settings) {
2335 if (!part_settings->adbbackup) {
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002336 InfoManager restore_info(part_settings->Backup_Folder + "/" + Backup_Name + ".info");
bigbiffce8f83c2015-12-12 18:30:21 -05002337 if (restore_info.LoadValues() == 0) {
2338 if (restore_info.GetValue("backup_size", Restore_Size) == 0) {
2339 LOGINFO("Read info file, restore size is %llu\n", Restore_Size);
2340 return Restore_Size;
2341 }
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002342 }
2343 }
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002344
bigbiffce8f83c2015-12-12 18:30:21 -05002345 string Full_FileName, Restore_File_System = Get_Restore_File_System(part_settings);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002346
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002347 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002348 if (Is_Image(Restore_File_System)) {
2349 Restore_Size = TWFunc::Get_File_Size(Full_FileName);
2350 return Restore_Size;
2351 }
2352
2353 twrpTar tar;
2354 tar.setdir(Backup_Path);
2355 tar.setfn(Full_FileName);
bigbiffce8f83c2015-12-12 18:30:21 -05002356 tar.backup_name = Full_FileName;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002357#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
2358 string Password;
2359 DataManager::GetValue("tw_restore_password", Password);
2360 if (!Password.empty())
2361 tar.setpassword(Password);
2362#endif
2363 tar.partition_name = Backup_Name;
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002364 tar.backup_folder = part_settings->Backup_Folder;
bigbiffce8f83c2015-12-12 18:30:21 -05002365 tar.part_settings = part_settings;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002366 Restore_Size = tar.get_size();
2367 return Restore_Size;
2368}
2369
bigbiffce8f83c2015-12-12 18:30:21 -05002370bool TWPartition::Restore_Tar(PartitionSettings *part_settings) {
Ethan Yonker472f5062016-02-25 13:47:30 -06002371 string Full_FileName;
Dees Troy4159aed2014-02-28 17:24:43 +00002372 bool ret = false;
bigbiffce8f83c2015-12-12 18:30:21 -05002373 string Restore_File_System = Get_Restore_File_System(part_settings);
Dees_Troy43d8b002012-09-17 16:00:01 -04002374
Dees_Troye58d5262012-09-21 12:27:57 -04002375 if (Has_Android_Secure) {
Dees_Troye58d5262012-09-21 12:27:57 -04002376 if (!Wipe_AndSec())
2377 return false;
Gary Peck43acadf2012-11-21 21:19:01 -08002378 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002379 gui_msg(Msg("wiping=Wiping {1}")(Backup_Display_Name));
Ethan Yonker5eac2222014-06-11 12:22:55 -05002380 if (Has_Data_Media && Mount_Point == "/data" && Restore_File_System != Current_File_System) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002381 gui_msg(Msg(msg::kWarning, "datamedia_fs_restore=WARNING: This /data backup was made with {1} file system! The backup may not boot unless you change back to {1}.")(Restore_File_System));
Ethan Yonker5eac2222014-06-11 12:22:55 -05002382 if (!Wipe_Data_Without_Wiping_Media())
2383 return false;
2384 } else {
2385 if (!Wipe(Restore_File_System))
2386 return false;
2387 }
Dees_Troye58d5262012-09-21 12:27:57 -04002388 }
Matt Mower3c366972015-12-25 19:28:31 -06002389 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Backup_Display_Name, gui_parse_text("{@restoring_hdr}"));
Ethan Yonker74db1572015-10-28 12:44:49 -05002390 gui_msg(Msg("restoring=Restoring {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002391
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05002392 // Remount as read/write as needed so we can restore the backup
2393 if (!ReMount_RW(true))
Dees_Troy43d8b002012-09-17 16:00:01 -04002394 return false;
2395
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002396 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
Ethan Yonker87af5632014-02-10 11:56:35 -06002397 twrpTar tar;
bigbiffce8f83c2015-12-12 18:30:21 -05002398 tar.part_settings = part_settings;
Ethan Yonker87af5632014-02-10 11:56:35 -06002399 tar.setdir(Backup_Path);
2400 tar.setfn(Full_FileName);
2401 tar.backup_name = Backup_Name;
2402#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
2403 string Password;
2404 DataManager::GetValue("tw_restore_password", Password);
2405 if (!Password.empty())
2406 tar.setpassword(Password);
2407#endif
bigbiffce8f83c2015-12-12 18:30:21 -05002408 part_settings->progress->SetPartitionSize(Get_Restore_Size(part_settings));
2409 if (tar.extractTarFork() != 0)
Dees Troy4159aed2014-02-28 17:24:43 +00002410 ret = false;
2411 else
2412 ret = true;
2413#ifdef HAVE_CAPABILITIES
2414 // Restore capabilities to the run-as binary
2415 if (Mount_Point == "/system" && Mount(true) && TWFunc::Path_Exists("/system/bin/run-as")) {
2416 struct vfs_cap_data cap_data;
2417 uint64_t capabilities = (1 << CAP_SETUID) | (1 << CAP_SETGID);
2418
2419 memset(&cap_data, 0, sizeof(cap_data));
2420 cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE;
2421 cap_data.data[0].permitted = (uint32_t) (capabilities & 0xffffffff);
2422 cap_data.data[0].inheritable = 0;
2423 cap_data.data[1].permitted = (uint32_t) (capabilities >> 32);
2424 cap_data.data[1].inheritable = 0;
2425 if (setxattr("/system/bin/run-as", XATTR_NAME_CAPS, &cap_data, sizeof(cap_data), 0) < 0) {
2426 LOGINFO("Failed to reset capabilities of /system/bin/run-as binary.\n");
2427 } else {
2428 LOGINFO("Reset capabilities of /system/bin/run-as binary successful.\n");
2429 }
2430 }
2431#endif
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05002432 if (Mount_Read_Only || Mount_Flags & MS_RDONLY)
2433 // Remount as read only when restoration is complete
2434 ReMount(true);
2435
Dees Troy4159aed2014-02-28 17:24:43 +00002436 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04002437}
2438
bigbiffce8f83c2015-12-12 18:30:21 -05002439bool TWPartition::Restore_Image(PartitionSettings *part_settings) {
Ethan Yonker96af84a2015-01-05 14:58:36 -06002440 string Full_FileName;
bigbiffce8f83c2015-12-12 18:30:21 -05002441 string Restore_File_System = Get_Restore_File_System(part_settings);
Dees_Troy43d8b002012-09-17 16:00:01 -04002442
Matt Mower3c366972015-12-25 19:28:31 -06002443 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Backup_Display_Name, gui_parse_text("{@restoring_hdr}"));
Ethan Yonker74db1572015-10-28 12:44:49 -05002444 gui_msg(Msg("restoring=Restoring {1}...")(Backup_Display_Name));
bigbiffce8f83c2015-12-12 18:30:21 -05002445
2446 if (part_settings->adbbackup)
2447 Full_FileName = TW_ADB_RESTORE;
2448 else
Ethan Yonkere080c1f2016-09-19 13:50:25 -05002449 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
Gary Peck15e623d2012-11-21 21:07:58 -08002450
Ethan Yonker96af84a2015-01-05 14:58:36 -06002451 if (Restore_File_System == "emmc") {
bigbiffce8f83c2015-12-12 18:30:21 -05002452 if (!part_settings->adbbackup)
2453 part_settings->total_restore_size = (uint64_t)(TWFunc::Get_File_Size(Full_FileName));
2454 if (!Raw_Read_Write(part_settings))
Ethan Yonker96af84a2015-01-05 14:58:36 -06002455 return false;
2456 } else if (Restore_File_System == "mtd" || Restore_File_System == "bml") {
bigbiffce8f83c2015-12-12 18:30:21 -05002457 if (!Flash_Image_FI(Full_FileName, part_settings->progress))
2458 return false;
2459 }
2460
2461 if (part_settings->adbbackup) {
2462 if (!twadbbu::Write_TWEOF())
Ethan Yonker96af84a2015-01-05 14:58:36 -06002463 return false;
Gary Peck15e623d2012-11-21 21:07:58 -08002464 }
Dees_Troy43d8b002012-09-17 16:00:01 -04002465 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002466}
Dees_Troy5bf43922012-09-07 16:07:55 -04002467
2468bool TWPartition::Update_Size(bool Display_Error) {
Dees_Troy0550cfb2012-10-13 11:56:13 -04002469 bool ret = false, Was_Already_Mounted = false;
Dees_Troy51127312012-09-08 13:08:49 -04002470
Dees_Troyab10ee22012-09-21 14:27:30 -04002471 if (!Can_Be_Mounted && !Is_Encrypted)
Dees_Troy5bf43922012-09-07 16:07:55 -04002472 return false;
2473
Dees_Troy0550cfb2012-10-13 11:56:13 -04002474 Was_Already_Mounted = Is_Mounted();
Dees_Troy38bd7602012-09-14 13:33:53 -04002475 if (Removable || Is_Encrypted) {
2476 if (!Mount(false))
2477 return true;
2478 } else if (!Mount(Display_Error))
Dees_Troy5bf43922012-09-07 16:07:55 -04002479 return false;
Dees_Troy51127312012-09-08 13:08:49 -04002480
2481 ret = Get_Size_Via_statfs(Display_Error);
Dees_Troy0550cfb2012-10-13 11:56:13 -04002482 if (!ret || Size == 0) {
2483 if (!Get_Size_Via_df(Display_Error)) {
2484 if (!Was_Already_Mounted)
2485 UnMount(false);
Dees_Troy51127312012-09-08 13:08:49 -04002486 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04002487 }
2488 }
Dees_Troy51127312012-09-08 13:08:49 -04002489
Dees_Troy5bf43922012-09-07 16:07:55 -04002490 if (Has_Data_Media) {
2491 if (Mount(Display_Error)) {
Dees_Troy51127312012-09-08 13:08:49 -04002492 unsigned long long data_media_used, actual_data;
Ethan Yonker66a19492015-12-10 10:19:45 -06002493 Used = du.Get_Folder_Size(Mount_Point);
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002494 Backup_Size = Used;
2495 int bak = (int)(Used / 1048576LLU);
Dees_Troy51127312012-09-08 13:08:49 -04002496 int fre = (int)(Free / 1048576LLU);
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002497 LOGINFO("Data backup size is %iMB, free: %iMB.\n", bak, fre);
Dees_Troy0550cfb2012-10-13 11:56:13 -04002498 } else {
2499 if (!Was_Already_Mounted)
2500 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04002501 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04002502 }
Dees_Troye58d5262012-09-21 12:27:57 -04002503 } else if (Has_Android_Secure) {
2504 if (Mount(Display_Error))
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002505 Backup_Size = du.Get_Folder_Size(Backup_Path);
Dees_Troy0550cfb2012-10-13 11:56:13 -04002506 else {
2507 if (!Was_Already_Mounted)
2508 UnMount(false);
Dees_Troye58d5262012-09-21 12:27:57 -04002509 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04002510 }
Dees_Troy5bf43922012-09-07 16:07:55 -04002511 }
Dees_Troy0550cfb2012-10-13 11:56:13 -04002512 if (!Was_Already_Mounted)
2513 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04002514 return true;
Dees_Troy51127312012-09-08 13:08:49 -04002515}
Dees_Troy38bd7602012-09-14 13:33:53 -04002516
2517void TWPartition::Find_Actual_Block_Device(void) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002518 if (Is_Decrypted && !Decrypted_Block_Device.empty()) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002519 Actual_Block_Device = Decrypted_Block_Device;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002520 if (TWFunc::Path_Exists(Decrypted_Block_Device))
Dees_Troy38bd7602012-09-14 13:33:53 -04002521 Is_Present = true;
Dees_Troy43d8b002012-09-17 16:00:01 -04002522 } else if (TWFunc::Path_Exists(Primary_Block_Device)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002523 Is_Present = true;
2524 Actual_Block_Device = Primary_Block_Device;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002525 return;
2526 }
2527 if (Is_Decrypted) {
Dees_Troy43d8b002012-09-17 16:00:01 -04002528 } else if (!Alternate_Block_Device.empty() && TWFunc::Path_Exists(Alternate_Block_Device)) {
Dees_Troy3f04d032012-10-07 18:20:09 -04002529 Actual_Block_Device = Alternate_Block_Device;
Dees_Troy38bd7602012-09-14 13:33:53 -04002530 Is_Present = true;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002531 } else {
Dees_Troy38bd7602012-09-14 13:33:53 -04002532 Is_Present = false;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002533 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002534}
2535
2536void TWPartition::Recreate_Media_Folder(void) {
2537 string Command;
Ethan Yonker66a19492015-12-10 10:19:45 -06002538 string Media_Path = Mount_Point + "/media";
Dees_Troy38bd7602012-09-14 13:33:53 -04002539
2540 if (!Mount(true)) {
Ethan Yonker66a19492015-12-10 10:19:45 -06002541 gui_msg(Msg(msg::kError, "recreate_folder_err=Unable to recreate {1} folder.")(Media_Path));
2542 } else if (!TWFunc::Path_Exists(Media_Path)) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002543 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Ethan Yonker66a19492015-12-10 10:19:45 -06002544 LOGINFO("Recreating %s folder.\n", Media_Path.c_str());
2545 mkdir(Media_Path.c_str(), 0770);
xiaolu9416f4f2015-06-04 08:22:23 +08002546 string Internal_path = DataManager::GetStrValue("tw_internal_path");
2547 if (!Internal_path.empty()) {
2548 LOGINFO("Recreating %s folder.\n", Internal_path.c_str());
2549 mkdir(Internal_path.c_str(), 0770);
2550 }
2551#ifdef TW_INTERNAL_STORAGE_PATH
2552 mkdir(EXPAND(TW_INTERNAL_STORAGE_PATH), 0770);
2553#endif
Ethan Yonker5ef301e2014-10-14 10:04:44 -05002554#ifdef HAVE_SELINUX
thata3d31fb2014-12-21 22:27:40 +01002555 // Afterwards, we will try to set the
2556 // default metadata that we were hopefully able to get during
2557 // early boot.
Ethan Yonker66a19492015-12-10 10:19:45 -06002558 tw_set_default_metadata(Media_Path.c_str());
xiaolu9416f4f2015-06-04 08:22:23 +08002559 if (!Internal_path.empty())
2560 tw_set_default_metadata(Internal_path.c_str());
Ethan Yonker5ef301e2014-10-14 10:04:44 -05002561#endif
Ethan Yonker5eac2222014-06-11 12:22:55 -05002562 // Toggle mount to ensure that "internal sdcard" gets mounted
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002563 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Ethan Yonker5eac2222014-06-11 12:22:55 -05002564 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Dees_Troy38bd7602012-09-14 13:33:53 -04002565 }
Dees_Troy43d8b002012-09-17 16:00:01 -04002566}
Dees_Troye58d5262012-09-21 12:27:57 -04002567
2568void TWPartition::Recreate_AndSec_Folder(void) {
Dees_Troye58d5262012-09-21 12:27:57 -04002569 if (!Has_Android_Secure)
2570 return;
Dees_Troy2673cec2013-04-02 20:22:16 +00002571 LOGINFO("Creating %s: %s\n", Backup_Display_Name.c_str(), Symlink_Path.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -04002572 if (!Mount(true)) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002573 gui_msg(Msg(msg::kError, "recreate_folder_err=Unable to recreate {1} folder.")(Backup_Name));
Dees_Troye58d5262012-09-21 12:27:57 -04002574 } else if (!TWFunc::Path_Exists(Symlink_Path)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00002575 LOGINFO("Recreating %s folder.\n", Backup_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002576 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05002577 mkdir(Symlink_Path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002578 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04002579 }
2580}
bigbiff7cb4c332014-11-26 20:36:07 -05002581
2582uint64_t TWPartition::Get_Max_FileSize() {
2583 uint64_t maxFileSize = 0;
2584 const uint64_t constGB = (uint64_t) 1024 * 1024 * 1024;
2585 const uint64_t constTB = (uint64_t) constGB * 1024;
2586 const uint64_t constPB = (uint64_t) constTB * 1024;
2587 const uint64_t constEB = (uint64_t) constPB * 1024;
bigbiff0c532032014-12-21 13:41:26 -05002588 if (Current_File_System == "ext4")
2589 maxFileSize = 16 * constTB; //16 TB
2590 else if (Current_File_System == "vfat")
2591 maxFileSize = 4 * constGB; //4 GB
2592 else if (Current_File_System == "ntfs")
2593 maxFileSize = 256 * constTB; //256 TB
2594 else if (Current_File_System == "exfat")
2595 maxFileSize = 16 * constPB; //16 PB
2596 else if (Current_File_System == "ext3")
2597 maxFileSize = 2 * constTB; //2 TB
2598 else if (Current_File_System == "f2fs")
2599 maxFileSize = 3.94 * constTB; //3.94 TB
bigbiff7cb4c332014-11-26 20:36:07 -05002600 else
2601 maxFileSize = 100000000L;
2602 return maxFileSize - 1;
2603}
2604
bigbiffce8f83c2015-12-12 18:30:21 -05002605bool TWPartition::Flash_Image(PartitionSettings *part_settings) {
2606 string Restore_File_System, full_filename;
Ethan Yonker96af84a2015-01-05 14:58:36 -06002607
Ethan Yonkere080c1f2016-09-19 13:50:25 -05002608 full_filename = part_settings->Backup_Folder + "/" + Backup_FileName;
Ethan Yonker96af84a2015-01-05 14:58:36 -06002609
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002610 LOGINFO("Image filename is: %s\n", Backup_FileName.c_str());
bigbiffce8f83c2015-12-12 18:30:21 -05002611
2612 if (Backup_Method == BM_FILES) {
Ethan Yonker96af84a2015-01-05 14:58:36 -06002613 LOGERR("Cannot flash images to file systems\n");
2614 return false;
2615 } else if (!Can_Flash_Img) {
2616 LOGERR("Cannot flash images to partitions %s\n", Display_Name.c_str());
2617 return false;
2618 } else {
2619 if (!Find_Partition_Size()) {
2620 LOGERR("Unable to find partition size for '%s'\n", Mount_Point.c_str());
2621 return false;
2622 }
bigbiffce8f83c2015-12-12 18:30:21 -05002623 unsigned long long image_size = TWFunc::Get_File_Size(full_filename);
Ethan Yonker96af84a2015-01-05 14:58:36 -06002624 if (image_size > Size) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002625 LOGINFO("Size (%llu bytes) of image '%s' is larger than target device '%s' (%llu bytes)\n",
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002626 image_size, Backup_FileName.c_str(), Actual_Block_Device.c_str(), Size);
Ethan Yonker74db1572015-10-28 12:44:49 -05002627 gui_err("img_size_err=Size of image is larger than target device");
Ethan Yonker96af84a2015-01-05 14:58:36 -06002628 return false;
2629 }
bigbiffce8f83c2015-12-12 18:30:21 -05002630 if (Backup_Method == BM_DD) {
2631 if (!part_settings->adbbackup) {
2632 if (Is_Sparse_Image(full_filename)) {
2633 return Flash_Sparse_Image(full_filename);
2634 }
Ethan Yonker472f5062016-02-25 13:47:30 -06002635 }
bigbiffce8f83c2015-12-12 18:30:21 -05002636 unsigned long long file_size = (unsigned long long)(TWFunc::Get_File_Size(full_filename));
2637 return Raw_Read_Write(part_settings);
2638 } else if (Backup_Method == BM_FLASH_UTILS) {
2639 return Flash_Image_FI(full_filename, NULL);
Ethan Yonker472f5062016-02-25 13:47:30 -06002640 }
Ethan Yonker96af84a2015-01-05 14:58:36 -06002641 }
2642
2643 LOGERR("Unknown flash method for '%s'\n", Mount_Point.c_str());
2644 return false;
2645}
2646
Ethan Yonker472f5062016-02-25 13:47:30 -06002647bool TWPartition::Is_Sparse_Image(const string& Filename) {
HashBanged974bb2016-01-30 14:20:09 -05002648 uint32_t magic = 0;
2649 int fd = open(Filename.c_str(), O_RDONLY);
2650 if (fd < 0) {
2651 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Filename)(strerror(errno)));
2652 return false;
2653 }
bigbiffce8f83c2015-12-12 18:30:21 -05002654
HashBanged974bb2016-01-30 14:20:09 -05002655 if (read(fd, &magic, sizeof(magic)) != sizeof(magic)) {
2656 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Filename)(strerror(errno)));
2657 close(fd);
2658 return false;
2659 }
2660 close(fd);
Ethan Yonker472f5062016-02-25 13:47:30 -06002661 if (magic == SPARSE_HEADER_MAGIC)
2662 return true;
2663 return false;
2664}
2665
2666bool TWPartition::Flash_Sparse_Image(const string& Filename) {
2667 string Command;
2668
2669 gui_msg(Msg("flashing=Flashing {1}...")(Display_Name));
2670
2671 Command = "simg2img '" + Filename + "' '" + Actual_Block_Device + "'";
Ethan Yonker96af84a2015-01-05 14:58:36 -06002672 LOGINFO("Flash command: '%s'\n", Command.c_str());
2673 TWFunc::Exec_Cmd(Command);
2674 return true;
2675}
2676
Ethan Yonker472f5062016-02-25 13:47:30 -06002677bool TWPartition::Flash_Image_FI(const string& Filename, ProgressTracking *progress) {
Ethan Yonker96af84a2015-01-05 14:58:36 -06002678 string Command;
Ethan Yonker472f5062016-02-25 13:47:30 -06002679 unsigned long long file_size;
Ethan Yonker96af84a2015-01-05 14:58:36 -06002680
Ethan Yonker74db1572015-10-28 12:44:49 -05002681 gui_msg(Msg("flashing=Flashing {1}...")(Display_Name));
Ethan Yonker472f5062016-02-25 13:47:30 -06002682 if (progress) {
2683 file_size = (unsigned long long)(TWFunc::Get_File_Size(Filename));
2684 progress->SetPartitionSize(file_size);
2685 }
Ethan Yonker96af84a2015-01-05 14:58:36 -06002686 // Sometimes flash image doesn't like to flash due to the first 2KB matching, so we erase first to ensure that it flashes
2687 Command = "erase_image " + MTD_Name;
2688 LOGINFO("Erase command: '%s'\n", Command.c_str());
2689 TWFunc::Exec_Cmd(Command);
2690 Command = "flash_image " + MTD_Name + " '" + Filename + "'";
2691 LOGINFO("Flash command: '%s'\n", Command.c_str());
2692 TWFunc::Exec_Cmd(Command);
Ethan Yonker472f5062016-02-25 13:47:30 -06002693 if (progress)
2694 progress->UpdateSize(file_size);
Ethan Yonker96af84a2015-01-05 14:58:36 -06002695 return true;
2696}
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05002697
2698void TWPartition::Change_Mount_Read_Only(bool new_value) {
2699 Mount_Read_Only = new_value;
2700}
2701
bigbiffce8f83c2015-12-12 18:30:21 -05002702bool TWPartition::Is_Read_Only() {
2703 return Mount_Read_Only;
2704}
2705
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05002706int TWPartition::Check_Lifetime_Writes() {
2707 bool original_read_only = Mount_Read_Only;
2708 int ret = 1;
2709
2710 Mount_Read_Only = true;
2711 if (Mount(false)) {
2712 Find_Actual_Block_Device();
2713 string block = basename(Actual_Block_Device.c_str());
2714 string file = "/sys/fs/" + Current_File_System + "/" + block + "/lifetime_write_kbytes";
2715 string result;
2716 if (TWFunc::Path_Exists(file)) {
2717 if (TWFunc::read_file(file, result) != 0) {
2718 LOGINFO("Check_Lifetime_Writes of '%s' failed to read_file\n", file.c_str());
2719 } else {
2720 LOGINFO("Check_Lifetime_Writes result: '%s'\n", result.c_str());
2721 if (result == "0") {
2722 ret = 0;
2723 }
2724 }
2725 } else {
2726 LOGINFO("Check_Lifetime_Writes file does not exist '%s'\n", file.c_str());
2727 }
2728 UnMount(true);
2729 } else {
2730 LOGINFO("Check_Lifetime_Writes failed to mount '%s'\n", Mount_Point.c_str());
2731 }
2732 Mount_Read_Only = original_read_only;
2733 return ret;
2734}
Ethan Yonker66a19492015-12-10 10:19:45 -06002735
2736int TWPartition::Decrypt_Adopted() {
2737#ifdef TW_INCLUDE_CRYPTO
2738 int ret = 1;
2739 Is_Adopted_Storage = false;
2740 string Adopted_Key_File = "";
2741
2742 if (!Removable)
2743 return ret;
2744
2745 int fd = open(Alternate_Block_Device.c_str(), O_RDONLY);
2746 if (fd < 0) {
2747 LOGINFO("failed to open '%s'\n", Alternate_Block_Device.c_str());
2748 return ret;
2749 }
2750 char type_guid[80];
2751 char part_guid[80];
2752
2753 if (gpt_disk_get_partition_info(fd, 2, type_guid, part_guid) == 0) {
2754 LOGINFO("type: '%s'\n", type_guid);
2755 LOGINFO("part: '%s'\n", part_guid);
2756 Adopted_GUID = part_guid;
2757 LOGINFO("Adopted_GUID '%s'\n", Adopted_GUID.c_str());
2758 if (strcmp(type_guid, TWGptAndroidExpand) == 0) {
2759 LOGINFO("android_expand found\n");
2760 Adopted_Key_File = "/data/misc/vold/expand_";
2761 Adopted_Key_File += part_guid;
2762 Adopted_Key_File += ".key";
2763 if (TWFunc::Path_Exists(Adopted_Key_File)) {
2764 Is_Adopted_Storage = true;
2765 /* Until we find a use case for this, I think it is safe
2766 * to disable USB Mass Storage whenever adopted storage
2767 * is present.
2768 */
2769 LOGINFO("Detected adopted storage, disabling USB mass storage mode\n");
2770 DataManager::SetValue("tw_has_usb_storage", 0);
2771 }
2772 }
2773 }
2774
2775 if (Is_Adopted_Storage) {
2776 string Adopted_Block_Device = Alternate_Block_Device + "p2";
2777 if (!TWFunc::Path_Exists(Adopted_Block_Device)) {
2778 Adopted_Block_Device = Alternate_Block_Device + "2";
2779 if (!TWFunc::Path_Exists(Adopted_Block_Device)) {
2780 LOGINFO("Adopted block device does not exist\n");
2781 goto exit;
2782 }
2783 }
2784 LOGINFO("key file is '%s', block device '%s'\n", Adopted_Key_File.c_str(), Adopted_Block_Device.c_str());
2785 char crypto_blkdev[MAXPATHLEN];
2786 std::string thekey;
2787 int fdkey = open(Adopted_Key_File.c_str(), O_RDONLY);
2788 if (fdkey < 0) {
2789 LOGINFO("failed to open key file\n");
2790 goto exit;
2791 }
2792 char buf[512];
2793 ssize_t n;
2794 while ((n = read(fdkey, &buf[0], sizeof(buf))) > 0) {
2795 thekey.append(buf, n);
2796 }
2797 close(fdkey);
2798 unsigned char* key = (unsigned char*) thekey.data();
2799 cryptfs_revert_ext_volume(part_guid);
2800
2801 ret = cryptfs_setup_ext_volume(part_guid, Adopted_Block_Device.c_str(), key, thekey.size(), crypto_blkdev);
2802 if (ret == 0) {
2803 LOGINFO("adopted storage new block device: '%s'\n", crypto_blkdev);
2804 Decrypted_Block_Device = crypto_blkdev;
2805 Is_Decrypted = true;
2806 Is_Encrypted = true;
2807 Find_Actual_Block_Device();
Kjell Braden3126a112016-06-19 16:58:15 +00002808 if (!Mount_Storage_Retry(false)) {
Ethan Yonker66a19492015-12-10 10:19:45 -06002809 LOGERR("Failed to mount decrypted adopted storage device\n");
2810 Is_Decrypted = false;
2811 Is_Encrypted = false;
2812 cryptfs_revert_ext_volume(part_guid);
2813 ret = 1;
2814 } else {
Ethan Yonkerfcf3f242016-02-16 12:30:26 -06002815 UnMount(false);
2816 Has_Android_Secure = false;
2817 Symlink_Path = "";
2818 Symlink_Mount_Point = "";
2819 Backup_Name = Mount_Point.substr(1);
2820 Backup_Path = Mount_Point;
2821 TWPartition* sdext = PartitionManager.Find_Partition_By_Path("/sd-ext");
2822 if (sdext && sdext->Actual_Block_Device == Adopted_Block_Device) {
2823 LOGINFO("Removing /sd-ext from partition list due to adopted storage\n");
2824 PartitionManager.Remove_Partition_By_Path("/sd-ext");
2825 }
Ethan Yonker66a19492015-12-10 10:19:45 -06002826 Setup_Data_Media();
2827 Recreate_Media_Folder();
2828 Wipe_Available_in_GUI = true;
2829 Wipe_During_Factory_Reset = true;
2830 Can_Be_Backed_Up = true;
2831 Can_Encrypt_Backup = true;
2832 Use_Userdata_Encryption = true;
2833 Is_Storage = true;
2834 Storage_Name = "Adopted Storage";
2835 Is_SubPartition = true;
2836 SubPartition_Of = "/data";
2837 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
2838 DataManager::SetValue("tw_has_adopted_storage", 1);
2839 }
2840 } else {
2841 LOGERR("Failed to setup adopted storage decryption\n");
2842 }
2843 }
2844exit:
2845 return ret;
2846#else
2847 LOGINFO("Decrypt_Adopted: no crypto support\n");
2848 return 1;
2849#endif
2850}
2851
2852void TWPartition::Revert_Adopted() {
2853#ifdef TW_INCLUDE_CRYPTO
2854 if (!Adopted_GUID.empty()) {
2855 PartitionManager.Remove_MTP_Storage(Mount_Point);
2856 UnMount(false);
2857 cryptfs_revert_ext_volume(Adopted_GUID.c_str());
2858 Is_Adopted_Storage = false;
2859 Is_Encrypted = false;
2860 Is_Decrypted = false;
2861 Decrypted_Block_Device = "";
2862 Find_Actual_Block_Device();
2863 Wipe_During_Factory_Reset = false;
2864 Can_Be_Backed_Up = false;
2865 Can_Encrypt_Backup = false;
2866 Use_Userdata_Encryption = false;
2867 Is_SubPartition = false;
2868 SubPartition_Of = "";
2869 Has_Data_Media = false;
2870 Storage_Path = Mount_Point;
2871 if (!Symlink_Mount_Point.empty()) {
2872 TWPartition* Dat = PartitionManager.Find_Partition_By_Path("/data");
2873 if (Dat) {
2874 Dat->UnMount(false);
2875 Dat->Symlink_Mount_Point = Symlink_Mount_Point;
2876 }
2877 Symlink_Mount_Point = "";
2878 }
2879 }
2880#else
2881 LOGINFO("Revert_Adopted: no crypto support\n");
2882#endif
2883}