blob: b764d947994f6a3891ec4433ca78d014ff67d5bf [file] [log] [blame]
Dees Troy3be70a82013-10-22 14:25:12 +00001/*
Ethan Yonker472f5062016-02-25 13:47:30 -06002 Copyright 2013 to 2016 TeamWin
Dees Troy3be70a82013-10-22 14:25:12 +00003 This file is part of TWRP/TeamWin Recovery Project.
4
5 TWRP is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 TWRP is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with TWRP. If not, see <http://www.gnu.org/licenses/>.
17*/
Dees_Troy51a0e822012-09-05 15:24:24 -040018
19#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
22#include <sys/stat.h>
23#include <sys/vfs.h>
Dees_Troy5bf43922012-09-07 16:07:55 -040024#include <sys/mount.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040025#include <unistd.h>
Dees_Troy51127312012-09-08 13:08:49 -040026#include <dirent.h>
thatf54e5392016-01-29 22:04:43 +010027#include <libgen.h>
bigbiffce8f83c2015-12-12 18:30:21 -050028#include <zlib.h>
bigbiff bigbiff9c754052013-01-09 09:09:08 -050029#include <iostream>
30#include <sstream>
Ethan Yonker66a19492015-12-10 10:19:45 -060031#include <sys/param.h>
Ethan Yonker472f5062016-02-25 13:47:30 -060032#include <fcntl.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040033
Dees_Troy657c3092012-09-10 20:32:10 -040034#ifdef TW_INCLUDE_CRYPTO
35 #include "cutils/properties.h"
36#endif
37
bigbiff7b4c7a62015-01-01 19:44:14 -050038#include "libblkid/include/blkid.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040039#include "variables.h"
Dees_Troy2673cec2013-04-02 20:22:16 +000040#include "twcommon.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040041#include "partitions.hpp"
Dees_Troy5bf43922012-09-07 16:07:55 -040042#include "data.hpp"
Dees_Troy43d8b002012-09-17 16:00:01 -040043#include "twrp-functions.hpp"
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -050044#include "twrpDigest.hpp"
bigbiff bigbiff9c754052013-01-09 09:09:08 -050045#include "twrpTar.hpp"
Ethan Yonker3fdcda42016-11-30 12:29:37 -060046#include "exclude.hpp"
Ethan Yonker1b7a31b2014-07-03 15:09:22 -050047#include "infomanager.hpp"
Ethan Yonker4b94cfd2014-12-11 10:00:45 -060048#include "set_metadata.h"
Ethan Yonker74db1572015-10-28 12:44:49 -050049#include "gui/gui.hpp"
bigbiffce8f83c2015-12-12 18:30:21 -050050#include "adbbu/libtwadbbu.hpp"
Dees_Troy5bf43922012-09-07 16:07:55 -040051extern "C" {
Dees_Troy38bd7602012-09-14 13:33:53 -040052 #include "mtdutils/mtdutils.h"
53 #include "mtdutils/mounts.h"
Dees_Troya95f55c2013-08-17 13:14:43 +000054#ifdef USE_EXT4
55 #include "make_ext4fs.h"
56#endif
Ethan Yonker71413f42014-02-26 13:36:08 -060057
58#ifdef TW_INCLUDE_CRYPTO
Ethan Yonker253368a2014-11-25 15:00:52 -060059 #include "crypto/lollipop/cryptfs.h"
Ethan Yonker66a19492015-12-10 10:19:45 -060060 #include "gpt/gpt.h"
Ethan Yonkerbd7492d2016-12-07 13:55:01 -060061 #ifdef TW_INCLUDE_FBE
62 #include "crypto/ext4crypt/Decrypt.h"
63 //#include "crypto/ext4crypt/Ext4Crypt.h"
64 #endif
Ethan Yonkerbc85b632015-08-09 12:48:14 -050065#else
66 #define CRYPT_FOOTER_OFFSET 0x4000
Ethan Yonker71413f42014-02-26 13:36:08 -060067#endif
Dees_Troy5bf43922012-09-07 16:07:55 -040068}
bigbiff bigbiffc49d7062013-10-11 20:28:00 -040069#ifdef HAVE_SELINUX
70#include "selinux/selinux.h"
Ethan Yonkerf27497f2014-02-09 11:48:33 -060071#include <selinux/label.h>
bigbiff bigbiffc49d7062013-10-11 20:28:00 -040072#endif
Dees Troy4159aed2014-02-28 17:24:43 +000073#ifdef HAVE_CAPABILITIES
74#include <sys/capability.h>
75#include <sys/xattr.h>
76#include <linux/xattr.h>
77#endif
HashBanged974bb2016-01-30 14:20:09 -050078#include <sparse_format.h>
Ethan Yonker472f5062016-02-25 13:47:30 -060079#include "progresstracking.hpp"
Dees_Troy51a0e822012-09-05 15:24:24 -040080
bigbiff bigbiff9c754052013-01-09 09:09:08 -050081using namespace std;
82
Dees_Troya95f55c2013-08-17 13:14:43 +000083extern struct selabel_handle *selinux_handle;
Ethan Yonker6277c792014-09-15 14:54:30 -050084extern bool datamedia;
Dees_Troya95f55c2013-08-17 13:14:43 +000085
Hashcode62bd9e02013-11-19 21:59:42 -080086struct flag_list {
87 const char *name;
88 unsigned flag;
89};
90
Matt Mower4ab42b12016-04-21 13:52:18 -050091const struct flag_list mount_flags[] = {
Hashcode62bd9e02013-11-19 21:59:42 -080092 { "noatime", MS_NOATIME },
93 { "noexec", MS_NOEXEC },
94 { "nosuid", MS_NOSUID },
95 { "nodev", MS_NODEV },
96 { "nodiratime", MS_NODIRATIME },
97 { "ro", MS_RDONLY },
98 { "rw", 0 },
99 { "remount", MS_REMOUNT },
100 { "bind", MS_BIND },
101 { "rec", MS_REC },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000102#ifdef MS_UNBINDABLE
Hashcode62bd9e02013-11-19 21:59:42 -0800103 { "unbindable", MS_UNBINDABLE },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000104#endif
105#ifdef MS_PRIVATE
Hashcode62bd9e02013-11-19 21:59:42 -0800106 { "private", MS_PRIVATE },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000107#endif
108#ifdef MS_SLAVE
Hashcode62bd9e02013-11-19 21:59:42 -0800109 { "slave", MS_SLAVE },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000110#endif
111#ifdef MS_SHARED
Hashcode62bd9e02013-11-19 21:59:42 -0800112 { "shared", MS_SHARED },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000113#endif
Hashcode62bd9e02013-11-19 21:59:42 -0800114 { "sync", MS_SYNCHRONOUS },
115 { "defaults", 0 },
116 { 0, 0 },
117};
118
Matt Mower2416a502016-04-12 19:54:46 -0500119enum TW_FSTAB_FLAGS {
120 TWFLAG_DEFAULTS, // Retain position
121 TWFLAG_ANDSEC,
122 TWFLAG_BACKUP,
123 TWFLAG_BACKUPNAME,
124 TWFLAG_BLOCKSIZE,
125 TWFLAG_CANBEWIPED,
126 TWFLAG_CANENCRYPTBACKUP,
127 TWFLAG_DISPLAY,
128 TWFLAG_ENCRYPTABLE,
129 TWFLAG_FLASHIMG,
130 TWFLAG_FORCEENCRYPT,
131 TWFLAG_FSFLAGS,
132 TWFLAG_IGNOREBLKID,
133 TWFLAG_LENGTH,
134 TWFLAG_MOUNTTODECRYPT,
135 TWFLAG_REMOVABLE,
136 TWFLAG_RETAINLAYOUTVERSION,
137 TWFLAG_SETTINGSSTORAGE,
138 TWFLAG_STORAGE,
139 TWFLAG_STORAGENAME,
140 TWFLAG_SUBPARTITIONOF,
141 TWFLAG_SYMLINK,
142 TWFLAG_USERDATAENCRYPTBACKUP,
143 TWFLAG_USERMRF,
144 TWFLAG_WIPEDURINGFACTORYRESET,
145 TWFLAG_WIPEINGUI,
Ethan Yonker1b190162016-12-05 15:25:19 -0600146 TWFLAG_SLOTSELECT,
Matt Mower2416a502016-04-12 19:54:46 -0500147};
148
149/* Flags without a trailing '=' are considered dual format flags and can be
150 * written as either 'flagname' or 'flagname=', where the character following
151 * the '=' is Y,y,1 for true and false otherwise.
152 */
153const struct flag_list tw_flags[] = {
154 { "andsec", TWFLAG_ANDSEC },
155 { "backup", TWFLAG_BACKUP },
156 { "backupname=", TWFLAG_BACKUPNAME },
157 { "blocksize=", TWFLAG_BLOCKSIZE },
158 { "canbewiped", TWFLAG_CANBEWIPED },
159 { "canencryptbackup", TWFLAG_CANENCRYPTBACKUP },
160 { "defaults", TWFLAG_DEFAULTS },
161 { "display=", TWFLAG_DISPLAY },
162 { "encryptable=", TWFLAG_ENCRYPTABLE },
163 { "flashimg", TWFLAG_FLASHIMG },
164 { "forceencrypt=", TWFLAG_FORCEENCRYPT },
165 { "fsflags=", TWFLAG_FSFLAGS },
166 { "ignoreblkid", TWFLAG_IGNOREBLKID },
167 { "length=", TWFLAG_LENGTH },
168 { "mounttodecrypt", TWFLAG_MOUNTTODECRYPT },
169 { "removable", TWFLAG_REMOVABLE },
170 { "retainlayoutversion", TWFLAG_RETAINLAYOUTVERSION },
171 { "settingsstorage", TWFLAG_SETTINGSSTORAGE },
172 { "storage", TWFLAG_STORAGE },
173 { "storagename=", TWFLAG_STORAGENAME },
174 { "subpartitionof=", TWFLAG_SUBPARTITIONOF },
175 { "symlink=", TWFLAG_SYMLINK },
176 { "userdataencryptbackup", TWFLAG_USERDATAENCRYPTBACKUP },
177 { "usermrf", TWFLAG_USERMRF },
178 { "wipeduringfactoryreset", TWFLAG_WIPEDURINGFACTORYRESET },
179 { "wipeingui", TWFLAG_WIPEINGUI },
Ethan Yonker1b190162016-12-05 15:25:19 -0600180 { "slotselect", TWFLAG_SLOTSELECT },
Matt Mower2416a502016-04-12 19:54:46 -0500181 { 0, 0 },
182};
183
that9e0593e2014-10-08 00:01:24 +0200184TWPartition::TWPartition() {
Dees_Troy51a0e822012-09-05 15:24:24 -0400185 Can_Be_Mounted = false;
186 Can_Be_Wiped = false;
Dees_Troya13d74f2013-03-24 08:54:55 -0500187 Can_Be_Backed_Up = false;
Vojtech Bocek1dc30982013-08-30 21:49:30 +0200188 Use_Rm_Rf = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400189 Wipe_During_Factory_Reset = false;
190 Wipe_Available_in_GUI = false;
191 Is_SubPartition = false;
Dees_Troy2691f9d2012-09-24 11:15:49 -0400192 Has_SubPartition = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400193 SubPartition_Of = "";
194 Symlink_Path = "";
195 Symlink_Mount_Point = "";
196 Mount_Point = "";
Dees_Troye58d5262012-09-21 12:27:57 -0400197 Backup_Path = "";
Dees_Troy38bd7602012-09-14 13:33:53 -0400198 Actual_Block_Device = "";
199 Primary_Block_Device = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400200 Alternate_Block_Device = "";
201 Removable = false;
202 Is_Present = false;
203 Length = 0;
204 Size = 0;
205 Used = 0;
206 Free = 0;
207 Backup_Size = 0;
208 Can_Be_Encrypted = false;
209 Is_Encrypted = false;
210 Is_Decrypted = false;
Ethan Yonkerbd7492d2016-12-07 13:55:01 -0600211 Is_FBE = false;
Ethan Yonker253368a2014-11-25 15:00:52 -0600212 Mount_To_Decrypt = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400213 Decrypted_Block_Device = "";
214 Display_Name = "";
Dees_Troya13d74f2013-03-24 08:54:55 -0500215 Backup_Display_Name = "";
216 Storage_Name = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400217 Backup_Name = "";
Dees_Troy63c8df72012-09-10 14:02:05 -0400218 Backup_FileName = "";
Dees_Troy38bd7602012-09-14 13:33:53 -0400219 MTD_Name = "";
bigbiffce8f83c2015-12-12 18:30:21 -0500220 Backup_Method = BM_NONE;
Dees_Troy83bd4832013-05-04 12:39:56 +0000221 Can_Encrypt_Backup = false;
222 Use_Userdata_Encryption = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400223 Has_Data_Media = false;
Dees_Troye58d5262012-09-21 12:27:57 -0400224 Has_Android_Secure = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400225 Is_Storage = false;
Dees_Troya13d74f2013-03-24 08:54:55 -0500226 Is_Settings_Storage = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400227 Storage_Path = "";
228 Current_File_System = "";
229 Fstab_File_System = "";
Hashcode62bd9e02013-11-19 21:59:42 -0800230 Mount_Flags = 0;
231 Mount_Options = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400232 Format_Block_Size = 0;
Dees_Troy68cab492012-12-12 19:29:35 +0000233 Ignore_Blkid = false;
Dees_Troy16c2b312013-01-15 16:51:18 +0000234 Retain_Layout_Version = false;
Ethan Yonker253368a2014-11-25 15:00:52 -0600235 Crypto_Key_Location = "footer";
Ethan Yonker726a0202014-12-16 20:01:38 -0600236 MTP_Storage_ID = 0;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600237 Can_Flash_Img = false;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500238 Mount_Read_Only = false;
Ethan Yonker66a19492015-12-10 10:19:45 -0600239 Is_Adopted_Storage = false;
240 Adopted_GUID = "";
Ethan Yonker1b190162016-12-05 15:25:19 -0600241 SlotSelect = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400242}
243
244TWPartition::~TWPartition(void) {
245 // Do nothing
246}
247
Matt Mower2b2dd152016-04-26 11:24:08 -0500248bool TWPartition::Process_Fstab_Line(const char *fstab_line, bool Display_Error) {
249 char full_line[MAX_FSTAB_LINE_LENGTH];
Matt Mower2416a502016-04-12 19:54:46 -0500250 char twflags[MAX_FSTAB_LINE_LENGTH] = "";
Dees_Troy5bf43922012-09-07 16:07:55 -0400251 char* ptr;
Matt Mower2b2dd152016-04-26 11:24:08 -0500252 int line_len = strlen(fstab_line), index = 0, item_index = 0;
Dees_Troya13d74f2013-03-24 08:54:55 -0500253 bool skip = false;
Dees_Troy5bf43922012-09-07 16:07:55 -0400254
Matt Mower2b2dd152016-04-26 11:24:08 -0500255 strlcpy(full_line, fstab_line, sizeof(full_line));
Dees_Troy51127312012-09-08 13:08:49 -0400256 for (index = 0; index < line_len; index++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500257 if (full_line[index] == 34)
258 skip = !skip;
259 if (!skip && full_line[index] <= 32)
Dees_Troy5bf43922012-09-07 16:07:55 -0400260 full_line[index] = '\0';
Dees_Troy5bf43922012-09-07 16:07:55 -0400261 }
Dees_Troy7c2dec82012-09-26 09:49:14 -0400262 Mount_Point = full_line;
Dees_Troy2673cec2013-04-02 20:22:16 +0000263 LOGINFO("Processing '%s'\n", Mount_Point.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -0400264 Backup_Path = Mount_Point;
Dees_Troya13d74f2013-03-24 08:54:55 -0500265 Storage_Path = Mount_Point;
Dees_Troy70737fa2013-04-08 13:19:20 +0000266 Display_Name = full_line + 1;
267 Backup_Display_Name = Display_Name;
268 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400269 index = Mount_Point.size();
270 while (index < line_len) {
271 while (index < line_len && full_line[index] == '\0')
272 index++;
273 if (index >= line_len)
274 continue;
275 ptr = full_line + index;
276 if (item_index == 0) {
277 // File System
278 Fstab_File_System = ptr;
279 Current_File_System = ptr;
280 item_index++;
281 } else if (item_index == 1) {
282 // Primary Block Device
Dees_Troy38bd7602012-09-14 13:33:53 -0400283 if (Fstab_File_System == "mtd" || Fstab_File_System == "yaffs2") {
Dees_Troy094207a2012-09-26 12:00:39 -0400284 MTD_Name = ptr;
285 Find_MTD_Block_Device(MTD_Name);
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400286 } else if (Fstab_File_System == "bml") {
287 if (Mount_Point == "/boot")
288 MTD_Name = "boot";
289 else if (Mount_Point == "/recovery")
290 MTD_Name = "recovery";
291 Primary_Block_Device = ptr;
292 if (*ptr != '/')
Dees_Troy2673cec2013-04-02 20:22:16 +0000293 LOGERR("Until we get better BML support, you will have to find and provide the full block device path to the BML devices e.g. /dev/block/bml9 instead of the partition name\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400294 } else if (*ptr != '/') {
Dees_Troy5bf43922012-09-07 16:07:55 -0400295 if (Display_Error)
Matt Mower2b2dd152016-04-26 11:24:08 -0500296 LOGERR("Invalid block device '%s' in fstab line '%s'", ptr, fstab_line);
Dees_Troy5bf43922012-09-07 16:07:55 -0400297 else
Matt Mower2b2dd152016-04-26 11:24:08 -0500298 LOGINFO("Invalid block device '%s' in fstab line '%s'", ptr, fstab_line);
Matt Mower72c87ce2016-04-26 14:34:56 -0500299 return false;
Dees_Troy38bd7602012-09-14 13:33:53 -0400300 } else {
301 Primary_Block_Device = ptr;
302 Find_Real_Block_Device(Primary_Block_Device, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400303 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400304 item_index++;
305 } else if (item_index > 1) {
306 if (*ptr == '/') {
307 // Alternate Block Device
308 Alternate_Block_Device = ptr;
309 Find_Real_Block_Device(Alternate_Block_Device, Display_Error);
310 } else if (strlen(ptr) > 7 && strncmp(ptr, "length=", 7) == 0) {
311 // Partition length
312 ptr += 7;
313 Length = atoi(ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400314 } else if (strlen(ptr) > 6 && strncmp(ptr, "flags=", 6) == 0) {
315 // Custom flags, save for later so that new values aren't overwritten by defaults
316 ptr += 6;
Matt Mower2416a502016-04-12 19:54:46 -0500317 strlcpy(twflags, ptr, sizeof(twflags));
Dees_Troy38bd7602012-09-14 13:33:53 -0400318 } else if (strlen(ptr) == 4 && (strncmp(ptr, "NULL", 4) == 0 || strncmp(ptr, "null", 4) == 0 || strncmp(ptr, "null", 4) == 0)) {
319 // Do nothing
Dees_Troy5bf43922012-09-07 16:07:55 -0400320 } else {
321 // Unhandled data
Matt Mower2b2dd152016-04-26 11:24:08 -0500322 LOGINFO("Unhandled fstab information '%s' in fstab line '%s'\n", ptr, fstab_line);
Dees_Troy5bf43922012-09-07 16:07:55 -0400323 }
324 }
325 while (index < line_len && full_line[index] != '\0')
326 index++;
327 }
328
329 if (!Is_File_System(Fstab_File_System) && !Is_Image(Fstab_File_System)) {
330 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000331 LOGERR("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400332 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000333 LOGINFO("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Matt Mower72c87ce2016-04-26 14:34:56 -0500334 return false;
Dees_Troy5bf43922012-09-07 16:07:55 -0400335 } else if (Is_File_System(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400336 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400337 Setup_File_System(Display_Error);
338 if (Mount_Point == "/system") {
339 Display_Name = "System";
Dees_Troya13d74f2013-03-24 08:54:55 -0500340 Backup_Display_Name = Display_Name;
341 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400342 Wipe_Available_in_GUI = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500343 Can_Be_Backed_Up = true;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500344 Mount_Read_Only = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400345 } else if (Mount_Point == "/data") {
346 Display_Name = "Data";
Dees_Troya13d74f2013-03-24 08:54:55 -0500347 Backup_Display_Name = Display_Name;
348 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400349 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400350 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500351 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000352 Can_Encrypt_Backup = true;
353 Use_Userdata_Encryption = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400354 } else if (Mount_Point == "/cache") {
355 Display_Name = "Cache";
Dees_Troya13d74f2013-03-24 08:54:55 -0500356 Backup_Display_Name = Display_Name;
357 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400358 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400359 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500360 Can_Be_Backed_Up = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400361 } else if (Mount_Point == "/datadata") {
Dees_Troy51127312012-09-08 13:08:49 -0400362 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400363 Display_Name = "DataData";
Dees_Troya13d74f2013-03-24 08:54:55 -0500364 Backup_Display_Name = Display_Name;
365 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400366 Is_SubPartition = true;
367 SubPartition_Of = "/data";
Dees_Troy5bf43922012-09-07 16:07:55 -0400368 DataManager::SetValue(TW_HAS_DATADATA, 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500369 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000370 Can_Encrypt_Backup = true;
371 Use_Userdata_Encryption = false; // This whole partition should be encrypted
Dees_Troy5bf43922012-09-07 16:07:55 -0400372 } else if (Mount_Point == "/sd-ext") {
Dees_Troy51127312012-09-08 13:08:49 -0400373 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400374 Display_Name = "SD-Ext";
Dees_Troya13d74f2013-03-24 08:54:55 -0500375 Backup_Display_Name = Display_Name;
376 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400377 Wipe_Available_in_GUI = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400378 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500379 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000380 Can_Encrypt_Backup = true;
381 Use_Userdata_Encryption = true;
Dees_Troy2c50e182012-09-26 20:05:28 -0400382 } else if (Mount_Point == "/boot") {
383 Display_Name = "Boot";
Dees_Troya13d74f2013-03-24 08:54:55 -0500384 Backup_Display_Name = Display_Name;
Dees_Troy2c50e182012-09-26 20:05:28 -0400385 DataManager::SetValue("tw_boot_is_mountable", 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500386 Can_Be_Backed_Up = true;
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500387 } else if (Mount_Point == "/vendor") {
388 Display_Name = "Vendor";
389 Backup_Display_Name = Display_Name;
390 Storage_Name = Display_Name;
391 Mount_Read_Only = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400392 }
393#ifdef TW_EXTERNAL_STORAGE_PATH
394 if (Mount_Point == EXPAND(TW_EXTERNAL_STORAGE_PATH)) {
395 Is_Storage = true;
396 Storage_Path = EXPAND(TW_EXTERNAL_STORAGE_PATH);
Dees_Troyc51f1f92012-09-20 15:32:13 -0400397 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500398 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400399#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000400 if (Mount_Point == "/sdcard" || Mount_Point == "/external_sd" || Mount_Point == "/external_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400401 Is_Storage = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400402 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500403 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400404#endif
Dees_Troyb05ddee2013-01-28 20:24:50 +0000405 }
Dees_Troy8170a922012-09-18 15:40:25 -0400406#ifdef TW_INTERNAL_STORAGE_PATH
407 if (Mount_Point == EXPAND(TW_INTERNAL_STORAGE_PATH)) {
408 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500409 Is_Settings_Storage = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400410 Storage_Path = EXPAND(TW_INTERNAL_STORAGE_PATH);
Dees_Troya13d74f2013-03-24 08:54:55 -0500411 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400412 }
413#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000414 if (Mount_Point == "/emmc" || Mount_Point == "/internal_sd" || Mount_Point == "/internal_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400415 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500416 Is_Settings_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500417 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400418 }
419#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400420 } else if (Is_Image(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400421 Find_Actual_Block_Device();
Ethan Yonker1b190162016-12-05 15:25:19 -0600422 Setup_Image();
Dees_Troya13d74f2013-03-24 08:54:55 -0500423 if (Mount_Point == "/boot") {
424 Display_Name = "Boot";
425 Backup_Display_Name = Display_Name;
426 Can_Be_Backed_Up = true;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600427 Can_Flash_Img = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500428 } else if (Mount_Point == "/recovery") {
429 Display_Name = "Recovery";
430 Backup_Display_Name = Display_Name;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600431 Can_Flash_Img = true;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500432 } else if (Mount_Point == "/system_image") {
433 Display_Name = "System Image";
434 Backup_Display_Name = Display_Name;
HashBanged974bb2016-01-30 14:20:09 -0500435 Can_Flash_Img = true;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500436 Can_Be_Backed_Up = true;
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500437 } else if (Mount_Point == "/vendor_image") {
438 Display_Name = "Vendor Image";
439 Backup_Display_Name = Display_Name;
HashBanged974bb2016-01-30 14:20:09 -0500440 Can_Flash_Img = true;
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500441 Can_Be_Backed_Up = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500442 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400443 }
444
Matt Mower2416a502016-04-12 19:54:46 -0500445 // Process TWRP fstab flags
446 if (strlen(twflags) > 0) {
447 string Prev_Display_Name = Display_Name;
448 string Prev_Storage_Name = Storage_Name;
449 string Prev_Backup_Display_Name = Backup_Display_Name;
450 Display_Name = "";
451 Storage_Name = "";
452 Backup_Display_Name = "";
453
454 Process_TW_Flags(twflags, Display_Error);
455
456 bool has_display_name = !Display_Name.empty();
457 bool has_storage_name = !Storage_Name.empty();
458 bool has_backup_name = !Backup_Display_Name.empty();
459 if (!has_display_name) Display_Name = Prev_Display_Name;
460 if (!has_storage_name) Storage_Name = Prev_Storage_Name;
461 if (!has_backup_name) Backup_Display_Name = Prev_Backup_Display_Name;
462
463 if (has_display_name && !has_storage_name)
464 Storage_Name = Display_Name;
465 if (!has_display_name && has_storage_name)
466 Display_Name = Storage_Name;
467 if (has_display_name && !has_backup_name && Backup_Display_Name != "Android Secure")
468 Backup_Display_Name = Display_Name;
469 if (!has_display_name && has_backup_name)
470 Display_Name = Backup_Display_Name;
471 }
Dees_Troy51127312012-09-08 13:08:49 -0400472 return true;
473}
474
Matt Mower72c87ce2016-04-26 14:34:56 -0500475void TWPartition::Partition_Post_Processing(bool Display_Error) {
476 if (Mount_Point == "/data")
477 Setup_Data_Partition(Display_Error);
478 else if (Mount_Point == "/cache")
479 Setup_Cache_Partition(Display_Error);
480}
481
Ethan Yonkerbd7492d2016-12-07 13:55:01 -0600482void TWPartition::ExcludeAll(const string& path) {
483 backup_exclusions.add_absolute_dir(path);
484 wipe_exclusions.add_absolute_dir(path);
485}
486
Matt Mower72c87ce2016-04-26 14:34:56 -0500487void TWPartition::Setup_Data_Partition(bool Display_Error) {
488 if (Mount_Point != "/data")
489 return;
490
491 // Ensure /data is not mounted as tmpfs for qcom hardware decrypt
492 UnMount(false);
493
494#ifdef TW_INCLUDE_CRYPTO
495 if (datamedia)
496 Setup_Data_Media();
497 Can_Be_Encrypted = true;
498 char crypto_blkdev[255];
499 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
500 if (strcmp(crypto_blkdev, "error") != 0) {
501 DataManager::SetValue(TW_IS_DECRYPTED, 1);
502 Is_Encrypted = true;
503 Is_Decrypted = true;
Ethan Yonkerbd7492d2016-12-07 13:55:01 -0600504 Is_FBE = false;
505 DataManager::SetValue(TW_IS_FBE, 0);
Matt Mower72c87ce2016-04-26 14:34:56 -0500506 Decrypted_Block_Device = crypto_blkdev;
507 LOGINFO("Data already decrypted, new block device: '%s'\n", crypto_blkdev);
508 } else if (!Mount(false)) {
509 if (Is_Present) {
510 set_partition_data(Actual_Block_Device.c_str(), Crypto_Key_Location.c_str(), Fstab_File_System.c_str());
511 if (cryptfs_check_footer() == 0) {
512 Is_Encrypted = true;
513 Is_Decrypted = false;
514 Can_Be_Mounted = false;
515 Current_File_System = "emmc";
Ethan Yonker1b190162016-12-05 15:25:19 -0600516 Setup_Image();
Matt Mower72c87ce2016-04-26 14:34:56 -0500517 DataManager::SetValue(TW_IS_ENCRYPTED, 1);
518 DataManager::SetValue(TW_CRYPTO_PWTYPE, cryptfs_get_password_type());
519 DataManager::SetValue(TW_CRYPTO_PASSWORD, "");
520 DataManager::SetValue("tw_crypto_display", "");
521 } else {
522 gui_err("mount_data_footer=Could not mount /data and unable to find crypto footer.");
523 }
524 } else {
525 LOGERR("Primary block device '%s' for mount point '%s' is not present!\n", Primary_Block_Device.c_str(), Mount_Point.c_str());
526 }
527 } else {
Ethan Yonkerbd7492d2016-12-07 13:55:01 -0600528 if (TWFunc::Path_Exists("/data/unencrypted/key/version")) {
529 LOGINFO("File Based Encryption is present\n");
530#ifdef TW_INCLUDE_FBE
531 ExcludeAll(Mount_Point + "/convert_fbe");
532 ExcludeAll(Mount_Point + "/unencrypted");
533 //ExcludeAll(Mount_Point + "/system/users/0"); // we WILL need to retain some of this if multiple users are present or we just need to delete more folders for the extra users somewhere else
534 ExcludeAll(Mount_Point + "/misc/vold/user_keys");
535 ExcludeAll(Mount_Point + "/system_ce");
536 ExcludeAll(Mount_Point + "/system_de");
537 ExcludeAll(Mount_Point + "/misc_ce");
538 ExcludeAll(Mount_Point + "/misc_de");
539 ExcludeAll(Mount_Point + "/system/gatekeeper.password.key");
540 ExcludeAll(Mount_Point + "/system/gatekeeper.pattern.key");
541 ExcludeAll(Mount_Point + "/system/locksettings.db");
542 //ExcludeAll(Mount_Point + "/system/locksettings.db-shm"); // don't seem to need this one, but the other 2 are needed
543 ExcludeAll(Mount_Point + "/system/locksettings.db-wal");
544 ExcludeAll(Mount_Point + "/user_de");
545 //ExcludeAll(Mount_Point + "/misc/profiles/cur/0"); // might be important later
546 ExcludeAll(Mount_Point + "/misc/gatekeeper");
547 ExcludeAll(Mount_Point + "/drm/kek.dat");
548 int retry_count = 3;
549 while (!Decrypt_DE() && --retry_count)
550 usleep(2000);
551 if (retry_count > 0) {
552 property_set("ro.crypto.state", "encrypted");
553 Is_Encrypted = true;
554 Is_Decrypted = false;
555 Is_FBE = true;
556 DataManager::SetValue(TW_IS_FBE, 1);
557 DataManager::SetValue(TW_IS_ENCRYPTED, 1);
558 string filename;
559 DataManager::SetValue(TW_CRYPTO_PWTYPE, Get_Password_Type(0, filename));
560 DataManager::SetValue(TW_CRYPTO_PASSWORD, "");
561 DataManager::SetValue("tw_crypto_display", "");
562 }
563#else
564 LOGERR("FBE found but FBE support not present in TWRP\n");
565#endif
566 } else {
567 // Filesystem is not encrypted and the mount succeeded, so return to
568 // the original unmounted state
569 UnMount(false);
570 }
Matt Mower72c87ce2016-04-26 14:34:56 -0500571 }
572 if (datamedia && (!Is_Encrypted || (Is_Encrypted && Is_Decrypted))) {
573 Setup_Data_Media();
574 Recreate_Media_Folder();
575 }
576#else
577 if (datamedia) {
578 Setup_Data_Media();
579 Recreate_Media_Folder();
580 }
581#endif
582}
583
584void TWPartition::Setup_Cache_Partition(bool Display_Error __unused) {
585 if (Mount_Point != "/cache")
586 return;
587
588 if (!Mount(true))
589 return;
590
591 if (!TWFunc::Path_Exists("/cache/recovery/.")) {
592 LOGINFO("Recreating /cache/recovery folder\n");
593 if (mkdir("/cache/recovery", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0)
594 LOGERR("Could not create /cache/recovery\n");
595 }
596}
597
Matt Mower4ab42b12016-04-21 13:52:18 -0500598void TWPartition::Process_FS_Flags(const char *str) {
599 char *options = strdup(str);
600 char *ptr, *savep;
Hashcode62bd9e02013-11-19 21:59:42 -0800601
Matt Mower4ab42b12016-04-21 13:52:18 -0500602 Mount_Options = "";
Hashcode62bd9e02013-11-19 21:59:42 -0800603
Matt Mower4ab42b12016-04-21 13:52:18 -0500604 // Avoid issues with potentially nested strtok by using strtok_r
605 ptr = strtok_r(options, ",", &savep);
606 while (ptr) {
607 const struct flag_list* mount_flag = mount_flags;
608
609 for (; mount_flag->name; mount_flag++) {
610 // mount_flags are never postfixed by '=',
611 // so only match identical strings (including length)
612 if (strcmp(ptr, mount_flag->name) == 0) {
613 Mount_Flags |= mount_flag->flag;
Hashcode62bd9e02013-11-19 21:59:42 -0800614 break;
615 }
616 }
617
Matt Mower4ab42b12016-04-21 13:52:18 -0500618 if (mount_flag->flag == MS_RDONLY)
619 Mount_Read_Only = true;
Hashcode62bd9e02013-11-19 21:59:42 -0800620
Matt Mower4ab42b12016-04-21 13:52:18 -0500621 if (mount_flag->name != 0) {
622 if (!Mount_Options.empty())
623 Mount_Options += ",";
624 Mount_Options += mount_flag->name;
625 } else {
626 LOGINFO("Unhandled mount flag: '%s'\n", ptr);
627 }
628
629 ptr = strtok_r(NULL, ",", &savep);
630 }
631 free(options);
Hashcode62bd9e02013-11-19 21:59:42 -0800632}
633
Matt Mower2416a502016-04-12 19:54:46 -0500634void TWPartition::Apply_TW_Flag(const unsigned flag, const char* str, const bool val) {
635 switch (flag) {
636 case TWFLAG_ANDSEC:
637 Has_Android_Secure = val;
638 break;
639 case TWFLAG_BACKUP:
640 Can_Be_Backed_Up = val;
641 break;
642 case TWFLAG_BACKUPNAME:
643 Backup_Display_Name = str;
644 break;
645 case TWFLAG_BLOCKSIZE:
646 Format_Block_Size = (unsigned long)(atol(str));
647 break;
648 case TWFLAG_CANBEWIPED:
649 Can_Be_Wiped = val;
650 break;
651 case TWFLAG_CANENCRYPTBACKUP:
652 Can_Encrypt_Backup = val;
653 break;
654 case TWFLAG_DEFAULTS:
655 // Do nothing
656 break;
657 case TWFLAG_DISPLAY:
658 Display_Name = str;
659 break;
660 case TWFLAG_ENCRYPTABLE:
661 case TWFLAG_FORCEENCRYPT:
662 Crypto_Key_Location = str;
663 break;
664 case TWFLAG_FLASHIMG:
665 Can_Flash_Img = val;
666 break;
667 case TWFLAG_FSFLAGS:
Matt Mower4ab42b12016-04-21 13:52:18 -0500668 Process_FS_Flags(str);
Matt Mower2416a502016-04-12 19:54:46 -0500669 break;
670 case TWFLAG_IGNOREBLKID:
671 Ignore_Blkid = val;
672 break;
673 case TWFLAG_LENGTH:
674 Length = atoi(str);
675 break;
676 case TWFLAG_MOUNTTODECRYPT:
677 Mount_To_Decrypt = val;
678 break;
679 case TWFLAG_REMOVABLE:
680 Removable = val;
681 break;
682 case TWFLAG_RETAINLAYOUTVERSION:
683 Retain_Layout_Version = val;
684 break;
685 case TWFLAG_SETTINGSSTORAGE:
686 Is_Settings_Storage = val;
687 if (Is_Settings_Storage)
688 Is_Storage = true;
689 break;
690 case TWFLAG_STORAGE:
691 Is_Storage = val;
692 break;
693 case TWFLAG_STORAGENAME:
694 Storage_Name = str;
695 break;
696 case TWFLAG_SUBPARTITIONOF:
697 Is_SubPartition = true;
698 SubPartition_Of = str;
699 break;
700 case TWFLAG_SYMLINK:
701 Symlink_Path = str;
702 break;
703 case TWFLAG_USERDATAENCRYPTBACKUP:
704 Use_Userdata_Encryption = val;
705 if (Use_Userdata_Encryption)
706 Can_Encrypt_Backup = true;
707 break;
708 case TWFLAG_USERMRF:
709 Use_Rm_Rf = val;
710 break;
711 case TWFLAG_WIPEDURINGFACTORYRESET:
712 Wipe_During_Factory_Reset = val;
713 if (Wipe_During_Factory_Reset) {
714 Can_Be_Wiped = true;
715 Wipe_Available_in_GUI = true;
716 }
717 break;
718 case TWFLAG_WIPEINGUI:
719 Wipe_Available_in_GUI = val;
720 if (Wipe_Available_in_GUI)
721 Can_Be_Wiped = true;
722 break;
Ethan Yonker1b190162016-12-05 15:25:19 -0600723 case TWFLAG_SLOTSELECT:
724 SlotSelect = true;
725 break;
Matt Mower2416a502016-04-12 19:54:46 -0500726 default:
727 // Should not get here
728 LOGINFO("Flag identified for processing, but later unmatched: %i\n", flag);
729 break;
730 }
731}
Dees_Troy51127312012-09-08 13:08:49 -0400732
Matt Mower2416a502016-04-12 19:54:46 -0500733void TWPartition::Process_TW_Flags(char *flags, bool Display_Error) {
734 char separator[2] = {'\n', 0};
735 char *ptr, *savep;
736
737 // Semicolons within double-quotes are not forbidden, so replace
738 // only the semicolons intended as separators with '\n' for strtok
739 for (unsigned i = 0, skip = 0; i < strlen(flags); i++) {
740 if (flags[i] == '\"')
Dees_Troya13d74f2013-03-24 08:54:55 -0500741 skip = !skip;
Matt Mower2416a502016-04-12 19:54:46 -0500742 if (!skip && flags[i] == ';')
743 flags[i] = separator[0];
Dees_Troy51127312012-09-08 13:08:49 -0400744 }
745
Matt Mower2416a502016-04-12 19:54:46 -0500746 // Avoid issues with potentially nested strtok by using strtok_r
747 ptr = strtok_r(flags, separator, &savep);
748 while (ptr) {
749 int ptr_len = strlen(ptr);
750 const struct flag_list* tw_flag = tw_flags;
Hashcode62bd9e02013-11-19 21:59:42 -0800751
Matt Mower2416a502016-04-12 19:54:46 -0500752 for (; tw_flag->name; tw_flag++) {
753 int flag_len = strlen(tw_flag->name);
754
755 if (strncmp(ptr, tw_flag->name, flag_len) == 0) {
756 bool flag_val = false;
757
758 if (ptr_len > flag_len && (tw_flag->name)[flag_len-1] != '='
759 && ptr[flag_len] != '=') {
760 // Handle flags with same starting string
761 // (e.g. backup and backupname)
762 continue;
763 } else if (ptr_len > flag_len && ptr[flag_len] == '=') {
764 // Handle flags with dual format: Part 1
765 // (e.g. backup and backup=y. backup=y handled here)
766 ptr += flag_len + 1;
767 TWFunc::Strip_Quotes(ptr);
768 // Skip flags with empty argument
769 // (e.g. backup=)
770 if (strlen(ptr) == 0) {
771 LOGINFO("Flag missing argument or should not include '=': %s=\n", tw_flag->name);
772 break;
773 }
774 flag_val = strchr("yY1", *ptr) != NULL;
775 } else if (ptr_len == flag_len
776 && (tw_flag->name)[flag_len-1] == '=') {
777 // Skip flags missing argument after =
778 // (e.g. backupname=)
779 LOGINFO("Flag missing argument: %s\n", tw_flag->name);
780 break;
781 } else if (ptr_len > flag_len
782 && (tw_flag->name)[flag_len-1] == '=') {
783 // Handle arguments to flags
784 // (e.g. backupname="My Stuff")
785 ptr += flag_len;
786 TWFunc::Strip_Quotes(ptr);
787 // Skip flags with empty argument
788 // (e.g. backupname="")
789 if (strlen(ptr) == 0) {
790 LOGINFO("Flag missing argument: %s\n", tw_flag->name);
791 break;
792 }
793 } else if (ptr_len == flag_len) {
794 // Handle flags with dual format: Part 2
795 // (e.g. backup and backup=y. backup handled here)
796 flag_val = true;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600797 } else {
Matt Mower2416a502016-04-12 19:54:46 -0500798 LOGINFO("Flag matched, but could not be processed: %s\n", ptr);
799 break;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600800 }
Matt Mower2416a502016-04-12 19:54:46 -0500801
802 Apply_TW_Flag(tw_flag->flag, ptr, flag_val);
803 break;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600804 }
Matt Mower2416a502016-04-12 19:54:46 -0500805 }
806 if (tw_flag->name == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400807 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000808 LOGERR("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400809 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000810 LOGINFO("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400811 }
Matt Mower2416a502016-04-12 19:54:46 -0500812 ptr = strtok_r(NULL, separator, &savep);
Dees_Troy51127312012-09-08 13:08:49 -0400813 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400814}
815
Dees_Troy5bf43922012-09-07 16:07:55 -0400816bool TWPartition::Is_File_System(string File_System) {
817 if (File_System == "ext2" ||
Dees_Troy63c8df72012-09-10 14:02:05 -0400818 File_System == "ext3" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400819 File_System == "ext4" ||
820 File_System == "vfat" ||
821 File_System == "ntfs" ||
822 File_System == "yaffs2" ||
bigbiff bigbiff3e146522012-11-14 14:32:59 -0500823 File_System == "exfat" ||
Dees_Troye5017042013-08-29 16:38:55 +0000824 File_System == "f2fs" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400825 File_System == "auto")
826 return true;
827 else
828 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400829}
830
Dees_Troy5bf43922012-09-07 16:07:55 -0400831bool TWPartition::Is_Image(string File_System) {
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400832 if (File_System == "emmc" || File_System == "mtd" || File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -0400833 return true;
834 else
835 return false;
836}
837
Dees_Troy51127312012-09-08 13:08:49 -0400838bool TWPartition::Make_Dir(string Path, bool Display_Error) {
Ethan Yonker93ac7a02016-11-07 22:05:58 -0600839 if (TWFunc::Get_D_Type_From_Stat(Path) != S_IFDIR)
840 unlink(Path.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400841 if (!TWFunc::Path_Exists(Path)) {
Dees_Troy51127312012-09-08 13:08:49 -0400842 if (mkdir(Path.c_str(), 0777) == -1) {
843 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -0500844 gui_msg(Msg(msg::kError, "create_folder_strerr=Can not create '{1}' folder ({2}).")(Path)(strerror(errno)));
Dees_Troy51127312012-09-08 13:08:49 -0400845 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000846 LOGINFO("Can not create '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400847 return false;
848 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000849 LOGINFO("Created '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400850 return true;
851 }
852 }
853 return true;
854}
855
Dees_Troy5bf43922012-09-07 16:07:55 -0400856void TWPartition::Setup_File_System(bool Display_Error) {
857 struct statfs st;
858
859 Can_Be_Mounted = true;
860 Can_Be_Wiped = true;
861
Dees_Troy5bf43922012-09-07 16:07:55 -0400862 // Make the mount point folder if it doesn't exist
Dees_Troy51127312012-09-08 13:08:49 -0400863 Make_Dir(Mount_Point, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400864 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
865 Backup_Name = Display_Name;
bigbiffce8f83c2015-12-12 18:30:21 -0500866 Backup_Method = BM_FILES;
Dees_Troy5bf43922012-09-07 16:07:55 -0400867}
868
Ethan Yonker1b190162016-12-05 15:25:19 -0600869void TWPartition::Setup_Image() {
Dees_Troy5bf43922012-09-07 16:07:55 -0400870 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
871 Backup_Name = Display_Name;
Gary Peck82599a82012-11-21 16:23:12 -0800872 if (Current_File_System == "emmc")
bigbiffce8f83c2015-12-12 18:30:21 -0500873 Backup_Method = BM_DD;
Gary Peck82599a82012-11-21 16:23:12 -0800874 else if (Current_File_System == "mtd" || Current_File_System == "bml")
bigbiffce8f83c2015-12-12 18:30:21 -0500875 Backup_Method = BM_FLASH_UTILS;
Dees_Troy5bf43922012-09-07 16:07:55 -0400876 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000877 LOGINFO("Unhandled file system '%s' on image '%s'\n", Current_File_System.c_str(), Display_Name.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400878}
879
Dees_Troye58d5262012-09-21 12:27:57 -0400880void TWPartition::Setup_AndSec(void) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500881 Backup_Display_Name = "Android Secure";
Dees_Troye58d5262012-09-21 12:27:57 -0400882 Backup_Name = "and-sec";
Dees_Troya13d74f2013-03-24 08:54:55 -0500883 Can_Be_Backed_Up = true;
Dees_Troye58d5262012-09-21 12:27:57 -0400884 Has_Android_Secure = true;
885 Symlink_Path = Mount_Point + "/.android_secure";
886 Symlink_Mount_Point = "/and-sec";
887 Backup_Path = Symlink_Mount_Point;
888 Make_Dir("/and-sec", true);
889 Recreate_AndSec_Folder();
Kjell Braden3126a112016-06-19 16:58:15 +0000890 Mount_Storage_Retry(true);
Dees_Troye58d5262012-09-21 12:27:57 -0400891}
892
that9e0593e2014-10-08 00:01:24 +0200893void TWPartition::Setup_Data_Media() {
Ethan Yonker6277c792014-09-15 14:54:30 -0500894 LOGINFO("Setting up '%s' as data/media emulated storage.\n", Mount_Point.c_str());
Ethan Yonker66a19492015-12-10 10:19:45 -0600895 if (Storage_Name.empty() || Storage_Name == "Data")
896 Storage_Name = "Internal Storage";
Ethan Yonker6277c792014-09-15 14:54:30 -0500897 Has_Data_Media = true;
898 Is_Storage = true;
Ethan Yonker66a19492015-12-10 10:19:45 -0600899 Storage_Path = Mount_Point + "/media";
Ethan Yonker6277c792014-09-15 14:54:30 -0500900 Symlink_Path = Storage_Path;
Ethan Yonker66a19492015-12-10 10:19:45 -0600901 if (Mount_Point == "/data") {
902 Is_Settings_Storage = true;
903 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
904 Make_Dir("/emmc", false);
905 Symlink_Mount_Point = "/emmc";
906 } else {
907 Make_Dir("/sdcard", false);
908 Symlink_Mount_Point = "/sdcard";
909 }
910 if (Mount(false) && TWFunc::Path_Exists(Mount_Point + "/media/0")) {
911 Storage_Path = Mount_Point + "/media/0";
912 Symlink_Path = Storage_Path;
913 DataManager::SetValue(TW_INTERNAL_PATH, Mount_Point + "/media/0");
914 UnMount(true);
915 }
916 DataManager::SetValue("tw_has_internal", 1);
917 DataManager::SetValue("tw_has_data_media", 1);
Ethan Yonker3fdcda42016-11-30 12:29:37 -0600918 backup_exclusions.add_absolute_dir("/data/data/com.google.android.music/files");
919 backup_exclusions.add_absolute_dir(Mount_Point + "/misc/vold");
920 wipe_exclusions.add_absolute_dir(Mount_Point + "/misc/vold");
921 backup_exclusions.add_absolute_dir(Mount_Point + "/.layout_version");
922 wipe_exclusions.add_absolute_dir(Mount_Point + "/.layout_version");
923 backup_exclusions.add_absolute_dir(Mount_Point + "/system/storage.xml");
924 wipe_exclusions.add_absolute_dir(Mount_Point + "/system/storage.xml");
Ethan Yonker6277c792014-09-15 14:54:30 -0500925 } else {
Ethan Yonker66a19492015-12-10 10:19:45 -0600926 if (Mount(true) && TWFunc::Path_Exists(Mount_Point + "/media/0")) {
927 Storage_Path = Mount_Point + "/media/0";
928 Symlink_Path = Storage_Path;
929 UnMount(true);
930 }
Ethan Yonker6277c792014-09-15 14:54:30 -0500931 }
Ethan Yonker3fdcda42016-11-30 12:29:37 -0600932 backup_exclusions.add_absolute_dir(Mount_Point + "/media");
933 wipe_exclusions.add_absolute_dir(Mount_Point + "/media");
Ethan Yonker6277c792014-09-15 14:54:30 -0500934}
935
Dees_Troy5bf43922012-09-07 16:07:55 -0400936void TWPartition::Find_Real_Block_Device(string& Block, bool Display_Error) {
937 char device[512], realDevice[512];
938
939 strcpy(device, Block.c_str());
940 memset(realDevice, 0, sizeof(realDevice));
941 while (readlink(device, realDevice, sizeof(realDevice)) > 0)
942 {
943 strcpy(device, realDevice);
944 memset(realDevice, 0, sizeof(realDevice));
945 }
946
947 if (device[0] != '/') {
948 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000949 LOGERR("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400950 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000951 LOGINFO("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400952 return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400953 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400954 Block = device;
955 return;
956 }
957}
958
Kjell Braden3126a112016-06-19 16:58:15 +0000959bool TWPartition::Mount_Storage_Retry(bool Display_Error) {
Dees_Troy8e337f32012-10-13 22:07:49 -0400960 // On some devices, storage doesn't want to mount right away, retry and sleep
Kjell Braden3126a112016-06-19 16:58:15 +0000961 if (!Mount(Display_Error)) {
Dees_Troy8e337f32012-10-13 22:07:49 -0400962 int retry_count = 5;
963 while (retry_count > 0 && !Mount(false)) {
964 usleep(500000);
965 retry_count--;
966 }
Kjell Braden3126a112016-06-19 16:58:15 +0000967 return Mount(Display_Error);
Dees_Troy8e337f32012-10-13 22:07:49 -0400968 }
Kjell Braden3126a112016-06-19 16:58:15 +0000969 return true;
Dees_Troy8e337f32012-10-13 22:07:49 -0400970}
971
Dees_Troy38bd7602012-09-14 13:33:53 -0400972bool TWPartition::Find_MTD_Block_Device(string MTD_Name) {
973 FILE *fp = NULL;
974 char line[255];
975
976 fp = fopen("/proc/mtd", "rt");
977 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000978 LOGERR("Device does not support /proc/mtd\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400979 return false;
980 }
981
982 while (fgets(line, sizeof(line), fp) != NULL)
983 {
984 char device[32], label[32];
985 unsigned long size = 0;
986 char* fstype = NULL;
987 int deviceId;
988
989 sscanf(line, "%s %lx %*s %*c%s", device, &size, label);
990
991 // Skip header and blank lines
992 if ((strcmp(device, "dev:") == 0) || (strlen(line) < 8))
993 continue;
994
995 // Strip off the trailing " from the label
996 label[strlen(label)-1] = '\0';
997
998 if (strcmp(label, MTD_Name.c_str()) == 0) {
999 // We found our device
1000 // Strip off the trailing : from the device
1001 device[strlen(device)-1] = '\0';
1002 if (sscanf(device,"mtd%d", &deviceId) == 1) {
1003 sprintf(device, "/dev/block/mtdblock%d", deviceId);
1004 Primary_Block_Device = device;
Dees_Troy76543db2013-06-19 16:24:30 +00001005 fclose(fp);
1006 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -04001007 }
1008 }
1009 }
1010 fclose(fp);
1011
1012 return false;
1013}
1014
Dees_Troy51127312012-09-08 13:08:49 -04001015bool TWPartition::Get_Size_Via_statfs(bool Display_Error) {
1016 struct statfs st;
1017 string Local_Path = Mount_Point + "/.";
1018
1019 if (!Mount(Display_Error))
1020 return false;
1021
1022 if (statfs(Local_Path.c_str(), &st) != 0) {
1023 if (!Removable) {
1024 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001025 LOGERR("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -04001026 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001027 LOGINFO("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -04001028 }
1029 return false;
1030 }
1031 Size = (st.f_blocks * st.f_bsize);
1032 Used = ((st.f_blocks - st.f_bfree) * st.f_bsize);
1033 Free = (st.f_bfree * st.f_bsize);
1034 Backup_Size = Used;
1035 return true;
1036}
1037
1038bool TWPartition::Get_Size_Via_df(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001039 FILE* fp;
1040 char command[255], line[512];
1041 int include_block = 1;
1042 unsigned int min_len;
1043
1044 if (!Mount(Display_Error))
1045 return false;
1046
Dees_Troy38bd7602012-09-14 13:33:53 -04001047 min_len = Actual_Block_Device.size() + 2;
Dees_Troy51127312012-09-08 13:08:49 -04001048 sprintf(command, "df %s > /tmp/dfoutput.txt", Mount_Point.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001049 TWFunc::Exec_Cmd(command);
Dees_Troy51127312012-09-08 13:08:49 -04001050 fp = fopen("/tmp/dfoutput.txt", "rt");
1051 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001052 LOGINFO("Unable to open /tmp/dfoutput.txt.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001053 return false;
Dees_Troy51127312012-09-08 13:08:49 -04001054 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001055
1056 while (fgets(line, sizeof(line), fp) != NULL)
1057 {
1058 unsigned long blocks, used, available;
1059 char device[64];
1060 char tmpString[64];
1061
1062 if (strncmp(line, "Filesystem", 10) == 0)
1063 continue;
1064 if (strlen(line) < min_len) {
1065 include_block = 0;
1066 continue;
1067 }
1068 if (include_block) {
1069 sscanf(line, "%s %lu %lu %lu", device, &blocks, &used, &available);
1070 } else {
1071 // The device block string is so long that the df information is on the next line
1072 int space_count = 0;
Dees_Troye58d5262012-09-21 12:27:57 -04001073 sprintf(tmpString, "/dev/block/%s", Actual_Block_Device.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001074 while (tmpString[space_count] == 32)
1075 space_count++;
1076 sscanf(line + space_count, "%lu %lu %lu", &blocks, &used, &available);
1077 }
1078
1079 // Adjust block size to byte size
1080 Size = blocks * 1024ULL;
1081 Used = used * 1024ULL;
1082 Free = available * 1024ULL;
1083 Backup_Size = Used;
1084 }
1085 fclose(fp);
1086 return true;
1087}
1088
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001089unsigned long long TWPartition::IOCTL_Get_Block_Size() {
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001090 Find_Actual_Block_Device();
Ethan Yonker483e9f42016-01-11 22:21:18 -06001091
1092 return TWFunc::IOCTL_Get_Block_Size(Actual_Block_Device.c_str());
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001093}
1094
Dees_Troy5bf43922012-09-07 16:07:55 -04001095bool TWPartition::Find_Partition_Size(void) {
1096 FILE* fp;
1097 char line[512];
1098 string tmpdevice;
1099
igoriok87e3d932013-01-31 21:03:53 +02001100 fp = fopen("/proc/dumchar_info", "rt");
1101 if (fp != NULL) {
1102 while (fgets(line, sizeof(line), fp) != NULL)
1103 {
1104 char label[32], device[32];
1105 unsigned long size = 0;
1106
thatd43bf2d2014-09-21 23:13:02 +02001107 sscanf(line, "%s %lx %*x %*u %s", label, &size, device);
igoriok87e3d932013-01-31 21:03:53 +02001108
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05001109 // Skip header, annotation and blank lines
igoriok87e3d932013-01-31 21:03:53 +02001110 if ((strncmp(device, "/dev/", 5) != 0) || (strlen(line) < 8))
1111 continue;
1112
1113 tmpdevice = "/dev/";
1114 tmpdevice += label;
1115 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
1116 Size = size;
1117 fclose(fp);
1118 return true;
1119 }
1120 }
1121 }
1122
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001123 unsigned long long ioctl_size = IOCTL_Get_Block_Size();
1124 if (ioctl_size) {
1125 Size = ioctl_size;
1126 return true;
1127 }
1128
Dees_Troy5bf43922012-09-07 16:07:55 -04001129 // In this case, we'll first get the partitions we care about (with labels)
1130 fp = fopen("/proc/partitions", "rt");
1131 if (fp == NULL)
1132 return false;
1133
1134 while (fgets(line, sizeof(line), fp) != NULL)
1135 {
1136 unsigned long major, minor, blocks;
1137 char device[512];
1138 char tmpString[64];
1139
Dees_Troy63c8df72012-09-10 14:02:05 -04001140 if (strlen(line) < 7 || line[0] == 'm') continue;
Dees_Troy5bf43922012-09-07 16:07:55 -04001141 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
1142
1143 tmpdevice = "/dev/block/";
1144 tmpdevice += device;
Dees_Troy38bd7602012-09-14 13:33:53 -04001145 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001146 // Adjust block size to byte size
1147 Size = blocks * 1024ULL;
1148 fclose(fp);
1149 return true;
1150 }
1151 }
1152 fclose(fp);
1153 return false;
1154}
1155
Dees_Troy5bf43922012-09-07 16:07:55 -04001156bool TWPartition::Is_Mounted(void) {
1157 if (!Can_Be_Mounted)
1158 return false;
1159
1160 struct stat st1, st2;
1161 string test_path;
1162
1163 // Check to see if the mount point directory exists
1164 test_path = Mount_Point + "/.";
1165 if (stat(test_path.c_str(), &st1) != 0) return false;
1166
1167 // Check to see if the directory above the mount point exists
1168 test_path = Mount_Point + "/../.";
1169 if (stat(test_path.c_str(), &st2) != 0) return false;
1170
1171 // Compare the device IDs -- if they match then we're (probably) using tmpfs instead of an actual device
1172 int ret = (st1.st_dev != st2.st_dev) ? true : false;
1173
1174 return ret;
1175}
1176
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05001177bool TWPartition::Is_File_System_Writable(void) {
1178 if (!Is_File_System(Current_File_System) || !Is_Mounted())
1179 return false;
1180
1181 string test_path = Mount_Point + "/.";
1182 return (access(test_path.c_str(), W_OK) == 0);
1183}
1184
Dees_Troy5bf43922012-09-07 16:07:55 -04001185bool TWPartition::Mount(bool Display_Error) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001186 int exfat_mounted = 0;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001187 unsigned long flags = Mount_Flags;
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001188
Dees_Troy5bf43922012-09-07 16:07:55 -04001189 if (Is_Mounted()) {
1190 return true;
1191 } else if (!Can_Be_Mounted) {
1192 return false;
1193 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001194
1195 Find_Actual_Block_Device();
1196
1197 // Check the current file system before mounting
1198 Check_FS_Type();
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001199 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/exfat-fuse")) {
Dees_Troye34c1332013-02-06 19:13:00 +00001200 string cmd = "/sbin/exfat-fuse -o big_writes,max_read=131072,max_write=131072 " + Actual_Block_Device + " " + Mount_Point;
Dees_Troy2673cec2013-04-02 20:22:16 +00001201 LOGINFO("cmd: %s\n", cmd.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001202 string result;
1203 if (TWFunc::Exec_Cmd(cmd, result) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001204 LOGINFO("exfat-fuse failed to mount with result '%s', trying vfat\n", result.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001205 Current_File_System = "vfat";
1206 } else {
1207#ifdef TW_NO_EXFAT_FUSE
1208 UnMount(false);
1209 // We'll let the kernel handle it but using exfat-fuse to detect if the file system is actually exfat
1210 // Some kernels let us mount vfat as exfat which doesn't work out too well
1211#else
1212 exfat_mounted = 1;
1213#endif
1214 }
1215 }
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001216
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001217 if (Current_File_System == "ntfs" && (TWFunc::Path_Exists("/sbin/ntfs-3g") || TWFunc::Path_Exists("/sbin/mount.ntfs"))) {
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001218 string cmd;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001219 string Ntfsmount_Binary = "";
1220
1221 if (TWFunc::Path_Exists("/sbin/ntfs-3g"))
1222 Ntfsmount_Binary = "ntfs-3g";
1223 else if (TWFunc::Path_Exists("/sbin/mount.ntfs"))
1224 Ntfsmount_Binary = "mount.ntfs";
1225
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001226 if (Mount_Read_Only)
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001227 cmd = "/sbin/" + Ntfsmount_Binary + " -o ro " + Actual_Block_Device + " " + Mount_Point;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001228 else
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001229 cmd = "/sbin/" + Ntfsmount_Binary + " " + Actual_Block_Device + " " + Mount_Point;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001230 LOGINFO("cmd: '%s'\n", cmd.c_str());
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001231
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001232 if (TWFunc::Exec_Cmd(cmd) == 0) {
1233 return true;
1234 } else {
1235 LOGINFO("ntfs-3g failed to mount, trying regular mount method.\n");
1236 }
1237 }
1238
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001239 if (Mount_Read_Only)
1240 flags |= MS_RDONLY;
1241
Dees_Troy22042032012-12-18 21:23:08 +00001242 if (Fstab_File_System == "yaffs2") {
1243 // mount an MTD partition as a YAFFS2 filesystem.
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001244 flags = MS_NOATIME | MS_NODEV | MS_NODIRATIME;
1245 if (Mount_Read_Only)
1246 flags |= MS_RDONLY;
Dees_Troy76543db2013-06-19 16:24:30 +00001247 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags, NULL) < 0) {
1248 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags | MS_RDONLY, NULL) < 0) {
1249 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001250 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy76543db2013-06-19 16:24:30 +00001251 else
1252 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
1253 return false;
1254 } else {
1255 LOGINFO("Mounted '%s' (MTD) as RO\n", Mount_Point.c_str());
1256 return true;
1257 }
1258 } else {
1259 struct stat st;
1260 string test_path = Mount_Point;
1261 if (stat(test_path.c_str(), &st) < 0) {
1262 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001263 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy76543db2013-06-19 16:24:30 +00001264 else
1265 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
1266 return false;
1267 }
1268 mode_t new_mode = st.st_mode | S_IXUSR | S_IXGRP | S_IXOTH;
1269 if (new_mode != st.st_mode) {
1270 LOGINFO("Fixing execute permissions for %s\n", Mount_Point.c_str());
1271 if (chmod(Mount_Point.c_str(), new_mode) < 0) {
1272 if (Display_Error)
1273 LOGERR("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
1274 else
1275 LOGINFO("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
1276 return false;
1277 }
1278 }
Dees_Troy22042032012-12-18 21:23:08 +00001279 return true;
Dees_Troy76543db2013-06-19 16:24:30 +00001280 }
thatc7572eb2015-03-31 18:55:30 +02001281 }
1282
1283 string mount_fs = Current_File_System;
1284 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sys/module/texfat"))
1285 mount_fs = "texfat";
1286
1287 if (!exfat_mounted &&
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001288 mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), mount_fs.c_str(), flags, Mount_Options.c_str()) != 0 &&
1289 mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), mount_fs.c_str(), flags, NULL) != 0) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001290#ifdef TW_NO_EXFAT_FUSE
1291 if (Current_File_System == "exfat") {
Dees_Troy2673cec2013-04-02 20:22:16 +00001292 LOGINFO("Mounting exfat failed, trying vfat...\n");
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001293 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), "vfat", 0, NULL) != 0) {
Dees_Troy85f44ed2013-01-09 18:42:36 +00001294 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001295 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy85f44ed2013-01-09 18:42:36 +00001296 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001297 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001298 LOGINFO("Actual block device: '%s', current file system: '%s', flags: 0x%8x, options: '%s'\n", Actual_Block_Device.c_str(), Current_File_System.c_str(), flags, Mount_Options.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001299 return false;
Dees_Troy85f44ed2013-01-09 18:42:36 +00001300 }
Dees_Troyb05ddee2013-01-28 20:24:50 +00001301 } else {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001302#endif
bigbiff bigbiff26774a02014-03-29 18:22:00 -04001303 if (!Removable && Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001304 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001305 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001306 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
1307 LOGINFO("Actual block device: '%s', current file system: '%s'\n", Actual_Block_Device.c_str(), Current_File_System.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001308 return false;
1309#ifdef TW_NO_EXFAT_FUSE
Dees_Troy85f44ed2013-01-09 18:42:36 +00001310 }
1311#endif
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001312 }
Ethan Yonker253368a2014-11-25 15:00:52 -06001313
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001314 if (Removable)
1315 Update_Size(Display_Error);
1316
xiaolu9416f4f2015-06-04 08:22:23 +08001317 if (!Symlink_Mount_Point.empty() && TWFunc::Path_Exists(Symlink_Path)) {
codelover2a3d4ce2015-03-14 20:26:49 +08001318 string Command = "mount -o bind '" + Symlink_Path + "' '" + Symlink_Mount_Point + "'";
Vojtech Bocek05534202013-09-11 08:11:56 +02001319 TWFunc::Exec_Cmd(Command);
Dees_Troy5bf43922012-09-07 16:07:55 -04001320 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001321 return true;
1322}
1323
1324bool TWPartition::UnMount(bool Display_Error) {
1325 if (Is_Mounted()) {
1326 int never_unmount_system;
1327
1328 DataManager::GetValue(TW_DONT_UNMOUNT_SYSTEM, never_unmount_system);
1329 if (never_unmount_system == 1 && Mount_Point == "/system")
1330 return true; // Never unmount system if you're not supposed to unmount it
1331
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001332 if (Is_Storage)
Ethan Yonker726a0202014-12-16 20:01:38 -06001333 PartitionManager.Remove_MTP_Storage(MTP_Storage_ID);
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001334
Dees_Troy38bd7602012-09-14 13:33:53 -04001335 if (!Symlink_Mount_Point.empty())
1336 umount(Symlink_Mount_Point.c_str());
1337
Dees_Troyb05ddee2013-01-28 20:24:50 +00001338 umount(Mount_Point.c_str());
1339 if (Is_Mounted()) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001340 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001341 gui_msg(Msg(msg::kError, "fail_unmount=Failed to unmount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy5bf43922012-09-07 16:07:55 -04001342 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001343 LOGINFO("Unable to unmount '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001344 return false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001345 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -04001346 return true;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001347 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001348 } else {
1349 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001350 }
1351}
1352
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05001353bool TWPartition::ReMount(bool Display_Error) {
1354 if (UnMount(Display_Error))
1355 return Mount(Display_Error);
1356 return false;
1357}
1358
1359bool TWPartition::ReMount_RW(bool Display_Error) {
1360 // No need to remount if already mounted rw
1361 if (Is_File_System_Writable())
1362 return true;
1363
1364 bool ro = Mount_Read_Only;
1365 int flags = Mount_Flags;
1366
1367 Mount_Read_Only = false;
1368 Mount_Flags &= ~MS_RDONLY;
1369
1370 bool ret = ReMount(Display_Error);
1371
1372 Mount_Read_Only = ro;
1373 Mount_Flags = flags;
1374
1375 return ret;
1376}
1377
Gary Peck43acadf2012-11-21 21:19:01 -08001378bool TWPartition::Wipe(string New_File_System) {
Ethan Yonker726a0202014-12-16 20:01:38 -06001379 bool wiped = false, update_crypt = false, recreate_media = true;
Dees_Troy16c2b312013-01-15 16:51:18 +00001380 int check;
1381 string Layout_Filename = Mount_Point + "/.layout_version";
1382
Dees_Troy38bd7602012-09-14 13:33:53 -04001383 if (!Can_Be_Wiped) {
Matt Mower3c366972015-12-25 19:28:31 -06001384 gui_msg(Msg(msg::kError, "cannot_wipe=Partition {1} cannot be wiped.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04001385 return false;
1386 }
1387
Dees_Troyc51f1f92012-09-20 15:32:13 -04001388 if (Mount_Point == "/cache")
Dees_Troy2673cec2013-04-02 20:22:16 +00001389 Log_Offset = 0;
Dees_Troyc51f1f92012-09-20 15:32:13 -04001390
Dees_Troy16c2b312013-01-15 16:51:18 +00001391 if (Retain_Layout_Version && Mount(false) && TWFunc::Path_Exists(Layout_Filename))
1392 TWFunc::copy_file(Layout_Filename, "/.layout_version", 0600);
1393 else
1394 unlink("/.layout_version");
Dees_Troy38bd7602012-09-14 13:33:53 -04001395
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001396 if (Has_Data_Media && Current_File_System == New_File_System) {
Dees_Troy16c2b312013-01-15 16:51:18 +00001397 wiped = Wipe_Data_Without_Wiping_Media();
Ethan Yonker5eac2222014-06-11 12:22:55 -05001398 recreate_media = false;
Dees_Troy16c2b312013-01-15 16:51:18 +00001399 } else {
Dees_Troy16c2b312013-01-15 16:51:18 +00001400 DataManager::GetValue(TW_RM_RF_VAR, check);
1401
Hashcodedabfd492013-08-29 22:45:30 -07001402 if (check || Use_Rm_Rf)
Dees_Troy16c2b312013-01-15 16:51:18 +00001403 wiped = Wipe_RMRF();
1404 else if (New_File_System == "ext4")
1405 wiped = Wipe_EXT4();
1406 else if (New_File_System == "ext2" || New_File_System == "ext3")
1407 wiped = Wipe_EXT23(New_File_System);
1408 else if (New_File_System == "vfat")
1409 wiped = Wipe_FAT();
1410 else if (New_File_System == "exfat")
1411 wiped = Wipe_EXFAT();
1412 else if (New_File_System == "yaffs2")
1413 wiped = Wipe_MTD();
Dees_Troye5017042013-08-29 16:38:55 +00001414 else if (New_File_System == "f2fs")
1415 wiped = Wipe_F2FS();
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001416 else if (New_File_System == "ntfs")
1417 wiped = Wipe_NTFS();
Dees_Troy16c2b312013-01-15 16:51:18 +00001418 else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001419 LOGERR("Unable to wipe '%s' -- unknown file system '%s'\n", Mount_Point.c_str(), New_File_System.c_str());
Dees_Troy16c2b312013-01-15 16:51:18 +00001420 unlink("/.layout_version");
1421 return false;
1422 }
1423 update_crypt = wiped;
Gary Pecke8bc5d72012-12-21 06:45:25 -08001424 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001425
Gary Pecke8bc5d72012-12-21 06:45:25 -08001426 if (wiped) {
Dees_Troy1c1ac442013-01-17 21:42:14 +00001427 if (Mount_Point == "/cache")
1428 DataManager::Output_Version();
1429
Dees_Troy16c2b312013-01-15 16:51:18 +00001430 if (TWFunc::Path_Exists("/.layout_version") && Mount(false))
1431 TWFunc::copy_file("/.layout_version", Layout_Filename, 0600);
1432
1433 if (update_crypt) {
1434 Setup_File_System(false);
1435 if (Is_Encrypted && !Is_Decrypted) {
1436 // just wiped an encrypted partition back to its unencrypted state
1437 Is_Encrypted = false;
1438 Is_Decrypted = false;
1439 Decrypted_Block_Device = "";
1440 if (Mount_Point == "/data") {
1441 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1442 DataManager::SetValue(TW_IS_DECRYPTED, 0);
1443 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001444 }
1445 }
Ethan Yonker5eac2222014-06-11 12:22:55 -05001446
Ethan Yonker66a19492015-12-10 10:19:45 -06001447 if (Has_Data_Media && recreate_media) {
Ethan Yonker5eac2222014-06-11 12:22:55 -05001448 Recreate_Media_Folder();
1449 }
Matt Mower209c9632016-01-20 12:08:35 -06001450 if (Is_Storage && Mount(false))
1451 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Gary Pecke8bc5d72012-12-21 06:45:25 -08001452 }
Matt Mower209c9632016-01-20 12:08:35 -06001453
Gary Pecke8bc5d72012-12-21 06:45:25 -08001454 return wiped;
Dees_Troy51a0e822012-09-05 15:24:24 -04001455}
1456
Gary Peck43acadf2012-11-21 21:19:01 -08001457bool TWPartition::Wipe() {
Gary Peck82599a82012-11-21 16:23:12 -08001458 if (Is_File_System(Current_File_System))
1459 return Wipe(Current_File_System);
1460 else
1461 return Wipe(Fstab_File_System);
Gary Peck43acadf2012-11-21 21:19:01 -08001462}
1463
Dees_Troye58d5262012-09-21 12:27:57 -04001464bool TWPartition::Wipe_AndSec(void) {
1465 if (!Has_Android_Secure)
1466 return false;
1467
Dees_Troye58d5262012-09-21 12:27:57 -04001468 if (!Mount(true))
1469 return false;
1470
Ethan Yonker74db1572015-10-28 12:44:49 -05001471 gui_msg(Msg("wiping=Wiping {1}")(Backup_Display_Name));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001472 TWFunc::removeDir(Mount_Point + "/.android_secure/", true);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001473 return true;
Dees_Troye58d5262012-09-21 12:27:57 -04001474}
1475
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001476bool TWPartition::Can_Repair() {
Ethan Yonkera2719152015-05-28 09:44:41 -05001477 if (Mount_Read_Only)
1478 return false;
Matt Mower18794c82015-11-11 16:22:45 -06001479 if (Current_File_System == "vfat" && TWFunc::Path_Exists("/sbin/fsck.fat"))
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001480 return true;
1481 else if ((Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") && TWFunc::Path_Exists("/sbin/e2fsck"))
1482 return true;
1483 else if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/fsck.exfat"))
1484 return true;
1485 else if (Current_File_System == "f2fs" && TWFunc::Path_Exists("/sbin/fsck.f2fs"))
1486 return true;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001487 else if (Current_File_System == "ntfs" && (TWFunc::Path_Exists("/sbin/ntfsfix") || TWFunc::Path_Exists("/sbin/fsck.ntfs")))
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001488 return true;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001489 return false;
1490}
1491
1492bool TWPartition::Repair() {
1493 string command;
1494
1495 if (Current_File_System == "vfat") {
Matt Mower18794c82015-11-11 16:22:45 -06001496 if (!TWFunc::Path_Exists("/sbin/fsck.fat")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001497 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.fat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001498 return false;
1499 }
1500 if (!UnMount(true))
1501 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001502 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.fat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001503 Find_Actual_Block_Device();
Matt Mower18794c82015-11-11 16:22:45 -06001504 command = "/sbin/fsck.fat -y " + Actual_Block_Device;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001505 LOGINFO("Repair command: %s\n", command.c_str());
1506 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001507 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001508 return true;
1509 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001510 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001511 return false;
1512 }
1513 }
1514 if (Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") {
1515 if (!TWFunc::Path_Exists("/sbin/e2fsck")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001516 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("e2fsck"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001517 return false;
1518 }
1519 if (!UnMount(true))
1520 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001521 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("e2fsck"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001522 Find_Actual_Block_Device();
Ethan Yonkera2719152015-05-28 09:44:41 -05001523 command = "/sbin/e2fsck -fp " + Actual_Block_Device;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001524 LOGINFO("Repair command: %s\n", command.c_str());
1525 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001526 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001527 return true;
1528 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001529 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001530 return false;
1531 }
1532 }
1533 if (Current_File_System == "exfat") {
1534 if (!TWFunc::Path_Exists("/sbin/fsck.exfat")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001535 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.exfat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001536 return false;
1537 }
1538 if (!UnMount(true))
1539 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001540 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.exfat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001541 Find_Actual_Block_Device();
1542 command = "/sbin/fsck.exfat " + Actual_Block_Device;
1543 LOGINFO("Repair command: %s\n", command.c_str());
1544 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001545 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001546 return true;
1547 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001548 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001549 return false;
1550 }
1551 }
1552 if (Current_File_System == "f2fs") {
1553 if (!TWFunc::Path_Exists("/sbin/fsck.f2fs")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001554 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.f2fs"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001555 return false;
1556 }
1557 if (!UnMount(true))
1558 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001559 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.f2fs"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001560 Find_Actual_Block_Device();
1561 command = "/sbin/fsck.f2fs " + Actual_Block_Device;
1562 LOGINFO("Repair command: %s\n", command.c_str());
1563 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001564 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001565 return true;
1566 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001567 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001568 return false;
1569 }
1570 }
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001571 if (Current_File_System == "ntfs") {
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001572 string Ntfsfix_Binary;
1573 if (TWFunc::Path_Exists("/sbin/ntfsfix"))
1574 Ntfsfix_Binary = "ntfsfix";
1575 else if (TWFunc::Path_Exists("/sbin/fsck.ntfs"))
1576 Ntfsfix_Binary = "fsck.ntfs";
1577 else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001578 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("ntfsfix"));
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001579 return false;
1580 }
1581 if (!UnMount(true))
1582 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001583 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)(Ntfsfix_Binary));
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001584 Find_Actual_Block_Device();
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001585 command = "/sbin/" + Ntfsfix_Binary + " " + Actual_Block_Device;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001586 LOGINFO("Repair command: %s\n", command.c_str());
1587 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001588 gui_msg("done=Done.");
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001589 return true;
1590 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001591 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001592 return false;
1593 }
1594 }
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001595 return false;
1596}
1597
Ethan Yonkera2719152015-05-28 09:44:41 -05001598bool TWPartition::Can_Resize() {
1599 if (Mount_Read_Only)
1600 return false;
1601 if ((Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") && TWFunc::Path_Exists("/sbin/resize2fs"))
1602 return true;
1603 return false;
1604}
1605
1606bool TWPartition::Resize() {
1607 string command;
1608
1609 if (Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") {
1610 if (!Can_Repair()) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001611 LOGINFO("Cannot resize %s because %s cannot be repaired before resizing.\n", Display_Name.c_str(), Display_Name.c_str());
1612 gui_msg(Msg(msg::kError, "cannot_resize=Cannot resize {1}.")(Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001613 return false;
1614 }
1615 if (!TWFunc::Path_Exists("/sbin/resize2fs")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001616 LOGINFO("resize2fs does not exist! Cannot resize!\n");
1617 gui_msg(Msg(msg::kError, "cannot_resize=Cannot resize {1}.")(Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001618 return false;
1619 }
1620 // Repair will unmount so no need to do it twice
Ethan Yonker74db1572015-10-28 12:44:49 -05001621 gui_msg(Msg("repair_resize=Repairing {1} before resizing.")( Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001622 if (!Repair())
1623 return false;
Ethan Yonker74db1572015-10-28 12:44:49 -05001624 gui_msg(Msg("resizing=Resizing {1} using {2}...")(Display_Name)("resize2fs"));
Ethan Yonkera2719152015-05-28 09:44:41 -05001625 Find_Actual_Block_Device();
1626 command = "/sbin/resize2fs " + Actual_Block_Device;
1627 if (Length != 0) {
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001628 unsigned long long Actual_Size = IOCTL_Get_Block_Size();
1629 if (Actual_Size == 0)
1630 return false;
Ethan Yonkera2719152015-05-28 09:44:41 -05001631
Ethan Yonkera2719152015-05-28 09:44:41 -05001632 unsigned long long Block_Count;
1633 if (Length < 0) {
1634 // Reduce overall size by this length
1635 Block_Count = (Actual_Size / 1024LLU) - ((unsigned long long)(Length * -1) / 1024LLU);
1636 } else {
1637 // This is the size, not a size reduction
1638 Block_Count = ((unsigned long long)(Length) / 1024LLU);
1639 }
1640 char temp[256];
1641 sprintf(temp, "%llu", Block_Count);
1642 command += " ";
1643 command += temp;
1644 command += "K";
1645 }
1646 LOGINFO("Resize command: %s\n", command.c_str());
1647 if (TWFunc::Exec_Cmd(command) == 0) {
1648 Update_Size(true);
Ethan Yonker74db1572015-10-28 12:44:49 -05001649 gui_msg("done=Done.");
Ethan Yonkera2719152015-05-28 09:44:41 -05001650 return true;
1651 } else {
1652 Update_Size(true);
Ethan Yonker74db1572015-10-28 12:44:49 -05001653 gui_msg(Msg(msg::kError, "unable_resize=Unable to resize {1}.")(Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001654 return false;
1655 }
1656 }
1657 return false;
1658}
1659
bigbiffce8f83c2015-12-12 18:30:21 -05001660bool TWPartition::Backup(PartitionSettings *part_settings, pid_t *tar_fork_pid) {
1661 if (Backup_Method == BM_FILES)
1662 return Backup_Tar(part_settings, tar_fork_pid);
1663 else if (Backup_Method == BM_DD)
1664 return Backup_Image(part_settings);
1665 else if (Backup_Method == BM_FLASH_UTILS)
1666 return Backup_Dump_Image(part_settings);
Dees_Troy2673cec2013-04-02 20:22:16 +00001667 LOGERR("Unknown backup method for '%s'\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001668 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001669}
1670
James Christopher Adduono79ae0932016-10-25 02:18:32 -04001671bool TWPartition::Check_Restore_File_MD5(const string& Filename) {
1672 twrpDigest md5sum;
1673
1674 md5sum.setfn(Filename);
1675 switch (md5sum.verify_md5digest()) {
1676 case MD5_OK:
1677 gui_msg(Msg("md5_matched=MD5 matched for '{1}'.")(Filename));
1678 return true;
1679 case MD5_FILE_UNREADABLE:
1680 case MD5_NOT_FOUND:
1681 gui_msg(Msg(msg::kError, "no_md5_found=No md5 file found for '{1}'. Please unselect Enable MD5 verification to restore.")(Filename));
1682 break;
1683 case MD5_MATCH_FAIL:
1684 gui_msg(Msg(msg::kError, "md5_fail_match=MD5 failed to match on '{1}'.")(Filename));
1685 break;
1686 }
1687 return false;
1688}
1689
bigbiff bigbiff8fd4b092016-08-30 20:48:53 -04001690bool TWPartition::Check_MD5(PartitionSettings *part_settings) {
James Christopher Adduono79ae0932016-10-25 02:18:32 -04001691 string Full_Filename;
Dees_Troy43d8b002012-09-17 16:00:01 -04001692 char split_filename[512];
1693 int index = 0;
1694
Captain Throwbackff5935f2014-09-23 16:08:34 -04001695 sync();
1696
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05001697 Full_Filename = part_settings->Backup_Folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04001698 if (!TWFunc::Path_Exists(Full_Filename)) {
1699 // This is a split archive, we presume
James Christopher Adduono79ae0932016-10-25 02:18:32 -04001700 memset(split_filename, 0, sizeof(split_filename));
Dees_Troy83bd4832013-05-04 12:39:56 +00001701 while (index < 1000) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001702 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
James Christopher Adduono79ae0932016-10-25 02:18:32 -04001703 if (!TWFunc::Path_Exists(split_filename))
1704 break;
1705 LOGINFO("split_filename: %s\n", split_filename);
1706 if (!Check_Restore_File_MD5(split_filename))
1707 return false;
1708 index++;
Dees_Troy43d8b002012-09-17 16:00:01 -04001709 }
Dees_Troy4a2a1262012-09-18 09:33:47 -04001710 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001711 }
James Christopher Adduono79ae0932016-10-25 02:18:32 -04001712 return Check_Restore_File_MD5(Full_Filename); // Single file archive
Dees_Troy43d8b002012-09-17 16:00:01 -04001713}
1714
bigbiffce8f83c2015-12-12 18:30:21 -05001715bool TWPartition::Restore(PartitionSettings *part_settings) {
Matt Mower3c366972015-12-25 19:28:31 -06001716 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, gui_parse_text("{@restoring_hdr}"));
Ethan Yonkere080c1f2016-09-19 13:50:25 -05001717 LOGINFO("Restore filename is: %s/%s\n", part_settings->Backup_Folder.c_str(), Backup_FileName.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001718
bigbiffce8f83c2015-12-12 18:30:21 -05001719 string Restore_File_System = Get_Restore_File_System(part_settings);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001720
1721 if (Is_File_System(Restore_File_System))
bigbiffce8f83c2015-12-12 18:30:21 -05001722 return Restore_Tar(part_settings);
1723 else if (Is_Image(Restore_File_System))
1724 return Restore_Image(part_settings);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001725
1726 LOGERR("Unknown restore method for '%s'\n", Mount_Point.c_str());
1727 return false;
1728}
1729
bigbiffce8f83c2015-12-12 18:30:21 -05001730string TWPartition::Get_Restore_File_System(PartitionSettings *part_settings) {
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001731 size_t first_period, second_period;
1732 string Restore_File_System;
1733
Gary Peck43acadf2012-11-21 21:19:01 -08001734 // Parse backup filename to extract the file system before wiping
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05001735 first_period = Backup_FileName.find(".");
Gary Peck43acadf2012-11-21 21:19:01 -08001736 if (first_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001737 LOGERR("Unable to find file system (first period).\n");
thatd43bf2d2014-09-21 23:13:02 +02001738 return string();
Gary Peck43acadf2012-11-21 21:19:01 -08001739 }
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05001740 Restore_File_System = Backup_FileName.substr(first_period + 1, Backup_FileName.size() - first_period - 1);
Gary Peck43acadf2012-11-21 21:19:01 -08001741 second_period = Restore_File_System.find(".");
1742 if (second_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001743 LOGERR("Unable to find file system (second period).\n");
thatd43bf2d2014-09-21 23:13:02 +02001744 return string();
Gary Peck43acadf2012-11-21 21:19:01 -08001745 }
1746 Restore_File_System.resize(second_period);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001747 return Restore_File_System;
Dees_Troy51a0e822012-09-05 15:24:24 -04001748}
1749
1750string TWPartition::Backup_Method_By_Name() {
bigbiffce8f83c2015-12-12 18:30:21 -05001751 if (Backup_Method == BM_NONE)
Dees_Troy38bd7602012-09-14 13:33:53 -04001752 return "none";
bigbiffce8f83c2015-12-12 18:30:21 -05001753 else if (Backup_Method == BM_FILES)
Dees_Troy38bd7602012-09-14 13:33:53 -04001754 return "files";
bigbiffce8f83c2015-12-12 18:30:21 -05001755 else if (Backup_Method == BM_DD)
Dees_Troy38bd7602012-09-14 13:33:53 -04001756 return "dd";
bigbiffce8f83c2015-12-12 18:30:21 -05001757 else if (Backup_Method == BM_FLASH_UTILS)
Dees_Troy38bd7602012-09-14 13:33:53 -04001758 return "flash_utils";
1759 else
1760 return "undefined";
1761 return "ERROR!";
Dees_Troy51a0e822012-09-05 15:24:24 -04001762}
1763
1764bool TWPartition::Decrypt(string Password) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001765 LOGINFO("STUB TWPartition::Decrypt, password: '%s'\n", Password.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001766 // Is this needed?
Dees_Troy51a0e822012-09-05 15:24:24 -04001767 return 1;
1768}
1769
1770bool TWPartition::Wipe_Encryption() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001771 bool Save_Data_Media = Has_Data_Media;
1772
1773 if (!UnMount(true))
1774 return false;
1775
Dees_Troy38bd7602012-09-14 13:33:53 -04001776 Has_Data_Media = false;
Dees_Troy74fb2e92013-04-15 14:35:47 +00001777 Decrypted_Block_Device = "";
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001778#ifdef TW_INCLUDE_CRYPTO
Ethan Yonker1b190162016-12-05 15:25:19 -06001779 if (Is_Decrypted && !Decrypted_Block_Device.empty()) {
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001780 if (!UnMount(true))
1781 return false;
Matt Mower2b18a532015-02-20 16:58:05 -06001782 if (delete_crypto_blk_dev((char*)("userdata")) != 0) {
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001783 LOGERR("Error deleting crypto block device, continuing anyway.\n");
1784 }
1785 }
1786#endif
Dees_Troy74fb2e92013-04-15 14:35:47 +00001787 Is_Decrypted = false;
1788 Is_Encrypted = false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001789 Find_Actual_Block_Device();
Ethan Yonkerbc85b632015-08-09 12:48:14 -05001790 if (Crypto_Key_Location == "footer") {
1791 int newlen, fd;
1792 if (Length != 0) {
1793 newlen = Length;
1794 if (newlen < 0)
1795 newlen = newlen * -1;
1796 } else {
1797 newlen = CRYPT_FOOTER_OFFSET;
1798 }
1799 if ((fd = open(Actual_Block_Device.c_str(), O_RDWR)) < 0) {
1800 gui_print_color("warning", "Unable to open '%s' to wipe crypto key\n", Actual_Block_Device.c_str());
1801 } else {
1802 unsigned int block_count;
1803 if ((ioctl(fd, BLKGETSIZE, &block_count)) == -1) {
1804 gui_print_color("warning", "Unable to get block size for wiping crypto footer.\n");
1805 } else {
1806 off64_t offset = ((off64_t)block_count * 512) - newlen;
1807 if (lseek64(fd, offset, SEEK_SET) == -1) {
1808 gui_print_color("warning", "Unable to lseek64 for wiping crypto footer.\n");
1809 } else {
1810 void* buffer = malloc(newlen);
1811 if (!buffer) {
1812 gui_print_color("warning", "Failed to malloc for wiping crypto footer.\n");
1813 } else {
1814 memset(buffer, 0, newlen);
1815 int ret = write(fd, buffer, newlen);
1816 if (ret != newlen) {
1817 gui_print_color("warning", "Failed to wipe crypto footer.\n");
1818 } else {
1819 LOGINFO("Successfully wiped crypto footer.\n");
1820 }
Matt Mower13a8f0b2015-09-26 15:40:03 -05001821 free(buffer);
Ethan Yonkerbc85b632015-08-09 12:48:14 -05001822 }
1823 }
1824 }
1825 close(fd);
1826 }
1827 } else {
Ethan Yonkerc2dafbb2016-03-15 22:20:59 -05001828 if (TWFunc::IOCTL_Get_Block_Size(Crypto_Key_Location.c_str()) >= 16384LLU) {
1829 string Command = "dd of='" + Crypto_Key_Location + "' if=/dev/zero bs=16384 count=1";
1830 TWFunc::Exec_Cmd(Command);
1831 } else {
1832 LOGINFO("Crypto key location reports size < 16K so not wiping crypto footer.\n");
1833 }
Ethan Yonkerbc85b632015-08-09 12:48:14 -05001834 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001835 if (Wipe(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001836 Has_Data_Media = Save_Data_Media;
1837 if (Has_Data_Media && !Symlink_Mount_Point.empty()) {
1838 Recreate_Media_Folder();
Ethan Yonker726a0202014-12-16 20:01:38 -06001839 if (Mount(false))
1840 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04001841 }
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001842 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
Ethan Yonker83e82572014-04-04 10:59:28 -05001843#ifndef TW_OEM_BUILD
Ethan Yonker74db1572015-10-28 12:44:49 -05001844 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 -05001845#endif
Dees_Troy38bd7602012-09-14 13:33:53 -04001846 return true;
1847 } else {
1848 Has_Data_Media = Save_Data_Media;
Ethan Yonker74db1572015-10-28 12:44:49 -05001849 gui_err("format_data_err=Unable to format to remove encryption.");
Ethan Yonker726a0202014-12-16 20:01:38 -06001850 if (Has_Data_Media && Mount(false))
1851 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04001852 return false;
1853 }
1854 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001855}
1856
1857void TWPartition::Check_FS_Type() {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001858 const char* type;
1859 blkid_probe pr;
Dees_Troy5bf43922012-09-07 16:07:55 -04001860
Dees_Troy68cab492012-12-12 19:29:35 +00001861 if (Fstab_File_System == "yaffs2" || Fstab_File_System == "mtd" || Fstab_File_System == "bml" || Ignore_Blkid)
1862 return; // Running blkid on some mtd devices causes a massive crash or needs to be skipped
Dees_Troy5bf43922012-09-07 16:07:55 -04001863
Dees_Troy38bd7602012-09-14 13:33:53 -04001864 Find_Actual_Block_Device();
Dees_Troy8170a922012-09-18 15:40:25 -04001865 if (!Is_Present)
1866 return;
Dees_Troy51127312012-09-08 13:08:49 -04001867
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001868 pr = blkid_new_probe_from_filename(Actual_Block_Device.c_str());
1869 if (blkid_do_fullprobe(pr)) {
1870 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00001871 LOGINFO("Can't probe device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001872 return;
Dees_Troy5bf43922012-09-07 16:07:55 -04001873 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001874
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05001875 if (blkid_probe_lookup_value(pr, "TYPE", &type, NULL) < 0) {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001876 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00001877 LOGINFO("can't find filesystem on device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001878 return;
1879 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001880
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001881 Current_File_System = type;
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001882 blkid_free_probe(pr);
Dees_Troy51a0e822012-09-05 15:24:24 -04001883}
1884
Gary Peck43acadf2012-11-21 21:19:01 -08001885bool TWPartition::Wipe_EXT23(string File_System) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001886 if (!UnMount(true))
1887 return false;
1888
Dees_Troy43d8b002012-09-17 16:00:01 -04001889 if (TWFunc::Path_Exists("/sbin/mke2fs")) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001890 string command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001891
Greg Wallace4b44fef2015-12-29 15:33:24 -05001892 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mke2fs"));
Dees_Troy38bd7602012-09-14 13:33:53 -04001893 Find_Actual_Block_Device();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001894 command = "mke2fs -t " + File_System + " -m 0 " + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001895 LOGINFO("mke2fs command: %s\n", command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001896 if (TWFunc::Exec_Cmd(command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001897 Current_File_System = File_System;
Dees_Troye58d5262012-09-21 12:27:57 -04001898 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05001899 gui_msg("done=Done.");
Dees_Troy38bd7602012-09-14 13:33:53 -04001900 return true;
1901 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001902 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04001903 return false;
1904 }
1905 } else
1906 return Wipe_RMRF();
1907
1908 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001909}
1910
1911bool TWPartition::Wipe_EXT4() {
Ethan Yonker25f20c12014-10-14 09:04:43 -05001912 Find_Actual_Block_Device();
1913 if (!Is_Present) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001914 LOGINFO("Block device not present, cannot wipe %s.\n", Display_Name.c_str());
1915 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Ethan Yonker25f20c12014-10-14 09:04:43 -05001916 return false;
1917 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001918 if (!UnMount(true))
1919 return false;
1920
Dees_Troyb3265ab2013-08-30 02:59:14 +00001921#if defined(HAVE_SELINUX) && defined(USE_EXT4)
Ethan Yonkerf27497f2014-02-09 11:48:33 -06001922 int ret;
1923 char *secontext = NULL;
1924
Greg Wallace4b44fef2015-12-29 15:33:24 -05001925 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("make_ext4fs"));
Ethan Yonkerf27497f2014-02-09 11:48:33 -06001926
Dees Troy99c8dbf2014-03-10 16:53:58 +00001927 if (!selinux_handle || selabel_lookup(selinux_handle, &secontext, Mount_Point.c_str(), S_IFDIR) < 0) {
Ethan Yonkerf27497f2014-02-09 11:48:33 -06001928 LOGINFO("Cannot lookup security context for '%s'\n", Mount_Point.c_str());
1929 ret = make_ext4fs(Actual_Block_Device.c_str(), Length, Mount_Point.c_str(), NULL);
1930 } else {
1931 ret = make_ext4fs(Actual_Block_Device.c_str(), Length, Mount_Point.c_str(), selinux_handle);
1932 }
1933 if (ret != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001934 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troya95f55c2013-08-17 13:14:43 +00001935 return false;
1936 } else {
bigbiff bigbiffc49d7062013-10-11 20:28:00 -04001937 string sedir = Mount_Point + "/lost+found";
1938 PartitionManager.Mount_By_Path(sedir.c_str(), true);
1939 rmdir(sedir.c_str());
1940 mkdir(sedir.c_str(), S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP);
Dees_Troya95f55c2013-08-17 13:14:43 +00001941 return true;
1942 }
1943#else
Dees_Troy43d8b002012-09-17 16:00:01 -04001944 if (TWFunc::Path_Exists("/sbin/make_ext4fs")) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001945 string Command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001946
Greg Wallace4b44fef2015-12-29 15:33:24 -05001947 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("make_ext4fs"));
Dees_Troy38bd7602012-09-14 13:33:53 -04001948 Find_Actual_Block_Device();
1949 Command = "make_ext4fs";
1950 if (!Is_Decrypted && Length != 0) {
1951 // Only use length if we're not decrypted
1952 char len[32];
1953 sprintf(len, "%i", Length);
1954 Command += " -l ";
1955 Command += len;
1956 }
Dees_Troy5295d582013-09-06 15:51:08 +00001957 if (TWFunc::Path_Exists("/file_contexts")) {
1958 Command += " -S /file_contexts";
1959 }
1960 Command += " -a " + Mount_Point + " " + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001961 LOGINFO("make_ext4fs command: %s\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001962 if (TWFunc::Exec_Cmd(Command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001963 Current_File_System = "ext4";
Dees_Troye58d5262012-09-21 12:27:57 -04001964 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05001965 gui_msg("done=Done.");
Dees_Troy38bd7602012-09-14 13:33:53 -04001966 return true;
1967 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001968 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04001969 return false;
1970 }
1971 } else
Gary Peck43acadf2012-11-21 21:19:01 -08001972 return Wipe_EXT23("ext4");
Dees_Troya95f55c2013-08-17 13:14:43 +00001973#endif
Dees_Troy38bd7602012-09-14 13:33:53 -04001974 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001975}
1976
1977bool TWPartition::Wipe_FAT() {
Vojtech Bocek05534202013-09-11 08:11:56 +02001978 string command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001979
Matt Mower18794c82015-11-11 16:22:45 -06001980 if (TWFunc::Path_Exists("/sbin/mkfs.fat")) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001981 if (!UnMount(true))
1982 return false;
1983
Greg Wallace4b44fef2015-12-29 15:33:24 -05001984 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mkfs.fat"));
Dees_Troy38bd7602012-09-14 13:33:53 -04001985 Find_Actual_Block_Device();
Matt Mower18794c82015-11-11 16:22:45 -06001986 command = "mkfs.fat " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02001987 if (TWFunc::Exec_Cmd(command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001988 Current_File_System = "vfat";
Dees_Troye58d5262012-09-21 12:27:57 -04001989 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05001990 gui_msg("done=Done.");
Dees_Troy38bd7602012-09-14 13:33:53 -04001991 return true;
1992 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001993 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04001994 return false;
1995 }
1996 return true;
1997 }
1998 else
1999 return Wipe_RMRF();
2000
2001 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04002002}
2003
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002004bool TWPartition::Wipe_EXFAT() {
Vojtech Bocek05534202013-09-11 08:11:56 +02002005 string command;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002006
2007 if (TWFunc::Path_Exists("/sbin/mkexfatfs")) {
2008 if (!UnMount(true))
2009 return false;
2010
Greg Wallace4b44fef2015-12-29 15:33:24 -05002011 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mkexfatfs"));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002012 Find_Actual_Block_Device();
2013 command = "mkexfatfs " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02002014 if (TWFunc::Exec_Cmd(command) == 0) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002015 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05002016 gui_msg("done=Done.");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002017 return true;
2018 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002019 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002020 return false;
2021 }
2022 return true;
2023 }
2024 return false;
2025}
2026
Dees_Troy38bd7602012-09-14 13:33:53 -04002027bool TWPartition::Wipe_MTD() {
2028 if (!UnMount(true))
2029 return false;
2030
Greg Wallace4b44fef2015-12-29 15:33:24 -05002031 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("MTD"));
Dees_Troy38bd7602012-09-14 13:33:53 -04002032
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02002033 mtd_scan_partitions();
2034 const MtdPartition* mtd = mtd_find_partition_by_name(MTD_Name.c_str());
2035 if (mtd == NULL) {
2036 LOGERR("No mtd partition named '%s'", MTD_Name.c_str());
2037 return false;
2038 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002039
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02002040 MtdWriteContext* ctx = mtd_write_partition(mtd);
2041 if (ctx == NULL) {
2042 LOGERR("Can't write '%s', failed to format.", MTD_Name.c_str());
2043 return false;
2044 }
2045 if (mtd_erase_blocks(ctx, -1) == -1) {
2046 mtd_write_close(ctx);
2047 LOGERR("Failed to format '%s'", MTD_Name.c_str());
2048 return false;
2049 }
2050 if (mtd_write_close(ctx) != 0) {
2051 LOGERR("Failed to close '%s'", MTD_Name.c_str());
2052 return false;
2053 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08002054 Current_File_System = "yaffs2";
Dees_Troye58d5262012-09-21 12:27:57 -04002055 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05002056 gui_msg("done=Done.");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02002057 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002058}
2059
2060bool TWPartition::Wipe_RMRF() {
Dees_Troy38bd7602012-09-14 13:33:53 -04002061 if (!Mount(true))
2062 return false;
Ethan Yonker726a0202014-12-16 20:01:38 -06002063 // This is the only wipe that leaves the partition mounted, so we
2064 // must manually remove the partition from MTP if it is a storage
2065 // partition.
2066 if (Is_Storage)
2067 PartitionManager.Remove_MTP_Storage(MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04002068
Ethan Yonker74db1572015-10-28 12:44:49 -05002069 gui_msg(Msg("remove_all=Removing all files under '{1}'")(Mount_Point));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002070 TWFunc::removeDir(Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04002071 Recreate_AndSec_Folder();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002072 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002073}
2074
Dees_Troye5017042013-08-29 16:38:55 +00002075bool TWPartition::Wipe_F2FS() {
Vojtech Bocek05534202013-09-11 08:11:56 +02002076 string command;
Dees_Troye5017042013-08-29 16:38:55 +00002077
2078 if (TWFunc::Path_Exists("/sbin/mkfs.f2fs")) {
2079 if (!UnMount(true))
2080 return false;
2081
Greg Wallace4b44fef2015-12-29 15:33:24 -05002082 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mkfs.f2fs"));
Dees_Troye5017042013-08-29 16:38:55 +00002083 Find_Actual_Block_Device();
Greg Wallace558aef72016-01-19 21:14:30 -05002084 command = "mkfs.f2fs -t 0";
dhacker29a3fa75f2015-01-17 19:42:33 -05002085 if (!Is_Decrypted && Length != 0) {
2086 // Only use length if we're not decrypted
2087 char len[32];
2088 int mod_length = Length;
2089 if (Length < 0)
2090 mod_length *= -1;
2091 sprintf(len, "%i", mod_length);
2092 command += " -r ";
2093 command += len;
2094 }
2095 command += " " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02002096 if (TWFunc::Exec_Cmd(command) == 0) {
Dees_Troye5017042013-08-29 16:38:55 +00002097 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05002098 gui_msg("done=Done.");
Dees_Troye5017042013-08-29 16:38:55 +00002099 return true;
2100 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002101 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troye5017042013-08-29 16:38:55 +00002102 return false;
2103 }
2104 return true;
2105 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002106 LOGINFO("mkfs.f2fs binary not found, using rm -rf to wipe.\n");
Dees_Troye5017042013-08-29 16:38:55 +00002107 return Wipe_RMRF();
2108 }
2109 return false;
2110}
2111
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002112bool TWPartition::Wipe_NTFS() {
2113 string command;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002114 string Ntfsmake_Binary;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002115
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002116 if (TWFunc::Path_Exists("/sbin/mkntfs"))
2117 Ntfsmake_Binary = "mkntfs";
2118 else if (TWFunc::Path_Exists("/sbin/mkfs.ntfs"))
2119 Ntfsmake_Binary = "mkfs.ntfs";
2120 else
2121 return false;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002122
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002123 if (!UnMount(true))
2124 return false;
2125
Greg Wallace4b44fef2015-12-29 15:33:24 -05002126 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)(Ntfsmake_Binary));
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002127 Find_Actual_Block_Device();
2128 command = "/sbin/" + Ntfsmake_Binary + " " + Actual_Block_Device;
2129 if (TWFunc::Exec_Cmd(command) == 0) {
2130 Recreate_AndSec_Folder();
2131 gui_msg("done=Done.");
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002132 return true;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002133 } else {
2134 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
2135 return false;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002136 }
2137 return false;
2138}
2139
Dees_Troy51a0e822012-09-05 15:24:24 -04002140bool TWPartition::Wipe_Data_Without_Wiping_Media() {
Ethan Yonker83e82572014-04-04 10:59:28 -05002141#ifdef TW_OEM_BUILD
2142 // In an OEM Build we want to do a full format
2143 return Wipe_Encryption();
2144#else
Ethan Yonker66a19492015-12-10 10:19:45 -06002145 bool ret = false;
Dees_Troy38bd7602012-09-14 13:33:53 -04002146
Dees_Troy38bd7602012-09-14 13:33:53 -04002147 if (!Mount(true))
2148 return false;
2149
Ethan Yonker74db1572015-10-28 12:44:49 -05002150 gui_msg("wiping_data=Wiping data without wiping /data/media ...");
Ethan Yonker66a19492015-12-10 10:19:45 -06002151 ret = Wipe_Data_Without_Wiping_Media_Func(Mount_Point + "/");
2152 if (ret)
2153 gui_msg("done=Done.");
2154 return ret;
2155#endif // ifdef TW_OEM_BUILD
2156}
2157
2158bool TWPartition::Wipe_Data_Without_Wiping_Media_Func(const string& parent __unused) {
2159 string dir;
Dees_Troy38bd7602012-09-14 13:33:53 -04002160
2161 DIR* d;
Ethan Yonker66a19492015-12-10 10:19:45 -06002162 d = opendir(parent.c_str());
Dees_Troy16b74352012-11-14 22:27:31 +00002163 if (d != NULL) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002164 struct dirent* de;
2165 while ((de = readdir(d)) != NULL) {
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002166 if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) continue;
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05002167
Ethan Yonker66a19492015-12-10 10:19:45 -06002168 dir = parent;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002169 dir.append(de->d_name);
Ethan Yonker3fdcda42016-11-30 12:29:37 -06002170 if (wipe_exclusions.check_skip_dirs(dir)) {
Ethan Yonker66a19492015-12-10 10:19:45 -06002171 LOGINFO("skipped '%s'\n", dir.c_str());
2172 continue;
2173 }
Dees_Troyce675462013-01-09 19:48:21 +00002174 if (de->d_type == DT_DIR) {
Ethan Yonker66a19492015-12-10 10:19:45 -06002175 dir.append("/");
2176 if (!Wipe_Data_Without_Wiping_Media_Func(dir)) {
2177 closedir(d);
2178 return false;
2179 }
2180 rmdir(dir.c_str());
bigbiff bigbiff98f1f902013-01-19 18:46:13 -05002181 } 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 +00002182 if (!unlink(dir.c_str()))
Dees_Troy2673cec2013-04-02 20:22:16 +00002183 LOGINFO("Unable to unlink '%s'\n", dir.c_str());
Dees_Troyce675462013-01-09 19:48:21 +00002184 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002185 }
2186 closedir(d);
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05002187
Dees_Troy16b74352012-11-14 22:27:31 +00002188 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -04002189 }
Ethan Yonker74db1572015-10-28 12:44:49 -05002190 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy16b74352012-11-14 22:27:31 +00002191 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04002192}
2193
bigbiffce8f83c2015-12-12 18:30:21 -05002194bool TWPartition::Backup_Tar(PartitionSettings *part_settings, pid_t *tar_fork_pid) {
Ethan Yonker472f5062016-02-25 13:47:30 -06002195 string Full_FileName;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002196 twrpTar tar;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05002197
Dees_Troy43d8b002012-09-17 16:00:01 -04002198 if (!Mount(true))
2199 return false;
2200
Dees_Troya13d74f2013-03-24 08:54:55 -05002201 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Backup_Display_Name, "Backing Up");
Ethan Yonker74db1572015-10-28 12:44:49 -05002202 gui_msg(Msg("backing_up=Backing up {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002203
Ethan Yonker472f5062016-02-25 13:47:30 -06002204 DataManager::GetValue(TW_USE_COMPRESSION_VAR, tar.use_compression);
Matt Mowerbb81e5d2014-03-20 18:05:41 -05002205
Dees_Troy83bd4832013-05-04 12:39:56 +00002206#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
Ethan Yonker472f5062016-02-25 13:47:30 -06002207 if (Can_Encrypt_Backup) {
2208 DataManager::GetValue("tw_encrypt_backup", tar.use_encryption);
2209 if (tar.use_encryption) {
2210 if (Use_Userdata_Encryption)
2211 tar.userdata_encryption = tar.use_encryption;
2212 string Password;
2213 DataManager::GetValue("tw_backup_password", Password);
2214 tar.setpassword(Password);
2215 } else {
2216 tar.use_encryption = 0;
2217 }
Dees_Troy83bd4832013-05-04 12:39:56 +00002218 }
2219#endif
Dees_Troy43d8b002012-09-17 16:00:01 -04002220
Ethan Yonker472f5062016-02-25 13:47:30 -06002221 Backup_FileName = Backup_Name + "." + Current_File_System + ".win";
Ethan Yonkere080c1f2016-09-19 13:50:25 -05002222 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
Dees_Troy83bd4832013-05-04 12:39:56 +00002223 tar.has_data_media = Has_Data_Media;
bigbiffce8f83c2015-12-12 18:30:21 -05002224 tar.part_settings = part_settings;
Ethan Yonker3fdcda42016-11-30 12:29:37 -06002225 tar.backup_exclusions = &backup_exclusions;
Dees Troye0a433a2013-12-02 04:10:37 +00002226 tar.setdir(Backup_Path);
2227 tar.setfn(Full_FileName);
2228 tar.setsize(Backup_Size);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002229 tar.partition_name = Backup_Name;
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002230 tar.backup_folder = part_settings->Backup_Folder;
bigbiffce8f83c2015-12-12 18:30:21 -05002231 if (tar.createTarFork(tar_fork_pid) != 0)
Dees Troye0a433a2013-12-02 04:10:37 +00002232 return false;
Dees_Troy43d8b002012-09-17 16:00:01 -04002233 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002234}
2235
bigbiffce8f83c2015-12-12 18:30:21 -05002236bool TWPartition::Backup_Image(PartitionSettings *part_settings) {
2237 string Full_FileName, adb_file_name;
2238 int adb_control_bu_fd, compressed;
igoriok87e3d932013-01-31 21:03:53 +02002239
Ethan Yonker74db1572015-10-28 12:44:49 -05002240 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, gui_parse_text("{@backing}"));
2241 gui_msg(Msg("backing_up=Backing up {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002242
Ethan Yonker472f5062016-02-25 13:47:30 -06002243 Backup_FileName = Backup_Name + "." + Current_File_System + ".win";
Dees_Troy43d8b002012-09-17 16:00:01 -04002244
bigbiffce8f83c2015-12-12 18:30:21 -05002245 if (part_settings->adbbackup) {
2246 Full_FileName = TW_ADB_BACKUP;
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002247 adb_file_name = part_settings->Backup_Folder + "/" + Backup_FileName;
bigbiffce8f83c2015-12-12 18:30:21 -05002248 }
2249 else
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002250 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
bigbiffce8f83c2015-12-12 18:30:21 -05002251
2252 part_settings->total_restore_size = Backup_Size;
2253
2254 if (part_settings->adbbackup) {
2255 if (!twadbbu::Write_TWIMG(adb_file_name, Backup_Size))
2256 return false;
2257 }
2258
2259 if (!Raw_Read_Write(part_settings))
Ethan Yonker472f5062016-02-25 13:47:30 -06002260 return false;
2261
bigbiffce8f83c2015-12-12 18:30:21 -05002262 if (part_settings->adbbackup) {
2263 if (!twadbbu::Write_TWEOF())
2264 return false;
Dees_Troy7c2dec82012-09-26 09:49:14 -04002265 }
Dees_Troy43d8b002012-09-17 16:00:01 -04002266 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002267}
2268
bigbiffce8f83c2015-12-12 18:30:21 -05002269bool TWPartition::Raw_Read_Write(PartitionSettings *part_settings) {
2270 unsigned long long RW_Block_Size, Remain = Backup_Size;
2271 int src_fd = -1, dest_fd = -1;
2272 ssize_t bs;
Ethan Yonker472f5062016-02-25 13:47:30 -06002273 bool ret = false;
2274 void* buffer = NULL;
2275 unsigned long long backedup_size = 0;
bigbiffce8f83c2015-12-12 18:30:21 -05002276 string srcfn, destfn;
Ethan Yonker472f5062016-02-25 13:47:30 -06002277
bigbiffce8f83c2015-12-12 18:30:21 -05002278 if (part_settings->PM_Method == PM_BACKUP) {
2279 srcfn = Actual_Block_Device;
2280 if (part_settings->adbbackup)
2281 destfn = TW_ADB_BACKUP;
2282 else
Ethan Yonkere080c1f2016-09-19 13:50:25 -05002283 destfn = part_settings->Backup_Folder + "/" + Backup_FileName;
bigbiffce8f83c2015-12-12 18:30:21 -05002284 }
2285 else {
2286 destfn = Actual_Block_Device;
2287 if (part_settings->adbbackup) {
2288 srcfn = TW_ADB_RESTORE;
2289 } else {
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002290 srcfn = part_settings->Backup_Folder + "/" + Backup_FileName;
bigbiffce8f83c2015-12-12 18:30:21 -05002291 Remain = TWFunc::Get_File_Size(srcfn);
2292 }
2293 }
Ethan Yonker472f5062016-02-25 13:47:30 -06002294
bigbiffce8f83c2015-12-12 18:30:21 -05002295 src_fd = open(srcfn.c_str(), O_RDONLY | O_LARGEFILE);
Ethan Yonker472f5062016-02-25 13:47:30 -06002296 if (src_fd < 0) {
bigbiffce8f83c2015-12-12 18:30:21 -05002297 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(srcfn.c_str())(strerror(errno)));
Ethan Yonker472f5062016-02-25 13:47:30 -06002298 return false;
2299 }
bigbiffce8f83c2015-12-12 18:30:21 -05002300
2301 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 -06002302 if (dest_fd < 0) {
bigbiffce8f83c2015-12-12 18:30:21 -05002303 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(destfn.c_str())(strerror(errno)));
Ethan Yonker472f5062016-02-25 13:47:30 -06002304 goto exit;
2305 }
bigbiffce8f83c2015-12-12 18:30:21 -05002306
2307 LOGINFO("Reading '%s', writing '%s'\n", srcfn.c_str(), destfn.c_str());
2308
2309 if (part_settings->adbbackup) {
2310 RW_Block_Size = MAX_ADB_READ;
2311 bs = MAX_ADB_READ;
2312 }
2313 else {
2314 RW_Block_Size = 1048576LLU; // 1MB
2315 bs = (ssize_t)(RW_Block_Size);
2316 }
2317
Ethan Yonker472f5062016-02-25 13:47:30 -06002318 buffer = malloc((size_t)bs);
2319 if (!buffer) {
2320 LOGINFO("Raw_Read_Write failed to malloc\n");
2321 goto exit;
2322 }
bigbiffce8f83c2015-12-12 18:30:21 -05002323
2324 if (part_settings->progress)
2325 part_settings->progress->SetPartitionSize(part_settings->total_restore_size);
2326
Ethan Yonker472f5062016-02-25 13:47:30 -06002327 while (Remain > 0) {
2328 if (Remain < RW_Block_Size)
bigbiffce8f83c2015-12-12 18:30:21 -05002329 bs = (ssize_t)(Remain);
2330 if (read(src_fd, buffer, bs) != bs) {
Ethan Yonker472f5062016-02-25 13:47:30 -06002331 LOGINFO("Error reading source fd (%s)\n", strerror(errno));
2332 goto exit;
2333 }
2334 if (write(dest_fd, buffer, bs) != bs) {
2335 LOGINFO("Error writing destination fd (%s)\n", strerror(errno));
2336 goto exit;
2337 }
2338 backedup_size += (unsigned long long)(bs);
bigbiffce8f83c2015-12-12 18:30:21 -05002339 Remain = Remain - (unsigned long long)(bs);
2340 if (part_settings->progress)
2341 part_settings->progress->UpdateSize(backedup_size);
Ethan Yonker472f5062016-02-25 13:47:30 -06002342 if (PartitionManager.Check_Backup_Cancel() != 0)
2343 goto exit;
2344 }
bigbiffce8f83c2015-12-12 18:30:21 -05002345 if (part_settings->progress)
2346 part_settings->progress->UpdateDisplayDetails(true);
Ethan Yonker472f5062016-02-25 13:47:30 -06002347 fsync(dest_fd);
2348 ret = true;
2349exit:
2350 if (src_fd >= 0)
2351 close(src_fd);
2352 if (dest_fd >= 0)
2353 close(dest_fd);
2354 if (buffer)
2355 free(buffer);
2356 return ret;
2357}
2358
bigbiffce8f83c2015-12-12 18:30:21 -05002359bool TWPartition::Backup_Dump_Image(PartitionSettings *part_settings) {
Vojtech Bocek05534202013-09-11 08:11:56 +02002360 string Full_FileName, Command;
bigbiffce8f83c2015-12-12 18:30:21 -05002361 int use_compression, adb_control_bu_fd;
2362 unsigned long long compressed;
Dees_Troy43d8b002012-09-17 16:00:01 -04002363
Ethan Yonker74db1572015-10-28 12:44:49 -05002364 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, gui_parse_text("{@backing}"));
2365 gui_msg(Msg("backing_up=Backing up {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002366
bigbiffce8f83c2015-12-12 18:30:21 -05002367 if (part_settings->progress)
2368 part_settings->progress->SetPartitionSize(Backup_Size);
Dees_Troy43d8b002012-09-17 16:00:01 -04002369
Ethan Yonker472f5062016-02-25 13:47:30 -06002370 Backup_FileName = Backup_Name + "." + Current_File_System + ".win";
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002371 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04002372
2373 Command = "dump_image " + MTD_Name + " '" + Full_FileName + "'";
bigbiffce8f83c2015-12-12 18:30:21 -05002374
Dees_Troy2673cec2013-04-02 20:22:16 +00002375 LOGINFO("Backup command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02002376 TWFunc::Exec_Cmd(Command);
Ethan Yonker4b94cfd2014-12-11 10:00:45 -06002377 tw_set_default_metadata(Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04002378 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
2379 // 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 -06002380 gui_msg(Msg(msg::kError, "backup_size=Backup file size for '{1}' is 0 bytes.")(Full_FileName));
Dees_Troy7c2dec82012-09-26 09:49:14 -04002381 return false;
2382 }
bigbiffce8f83c2015-12-12 18:30:21 -05002383 if (part_settings->progress)
2384 part_settings->progress->UpdateSize(Backup_Size);
2385
Dees_Troy43d8b002012-09-17 16:00:01 -04002386 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002387}
2388
bigbiffce8f83c2015-12-12 18:30:21 -05002389unsigned long long TWPartition::Get_Restore_Size(PartitionSettings *part_settings) {
2390 if (!part_settings->adbbackup) {
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002391 InfoManager restore_info(part_settings->Backup_Folder + "/" + Backup_Name + ".info");
bigbiffce8f83c2015-12-12 18:30:21 -05002392 if (restore_info.LoadValues() == 0) {
2393 if (restore_info.GetValue("backup_size", Restore_Size) == 0) {
2394 LOGINFO("Read info file, restore size is %llu\n", Restore_Size);
2395 return Restore_Size;
2396 }
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002397 }
2398 }
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002399
bigbiffce8f83c2015-12-12 18:30:21 -05002400 string Full_FileName, Restore_File_System = Get_Restore_File_System(part_settings);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002401
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002402 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002403 if (Is_Image(Restore_File_System)) {
2404 Restore_Size = TWFunc::Get_File_Size(Full_FileName);
2405 return Restore_Size;
2406 }
2407
2408 twrpTar tar;
2409 tar.setdir(Backup_Path);
2410 tar.setfn(Full_FileName);
bigbiffce8f83c2015-12-12 18:30:21 -05002411 tar.backup_name = Full_FileName;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002412#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
2413 string Password;
2414 DataManager::GetValue("tw_restore_password", Password);
2415 if (!Password.empty())
2416 tar.setpassword(Password);
2417#endif
2418 tar.partition_name = Backup_Name;
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002419 tar.backup_folder = part_settings->Backup_Folder;
bigbiffce8f83c2015-12-12 18:30:21 -05002420 tar.part_settings = part_settings;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002421 Restore_Size = tar.get_size();
2422 return Restore_Size;
2423}
2424
bigbiffce8f83c2015-12-12 18:30:21 -05002425bool TWPartition::Restore_Tar(PartitionSettings *part_settings) {
Ethan Yonker472f5062016-02-25 13:47:30 -06002426 string Full_FileName;
Dees Troy4159aed2014-02-28 17:24:43 +00002427 bool ret = false;
bigbiffce8f83c2015-12-12 18:30:21 -05002428 string Restore_File_System = Get_Restore_File_System(part_settings);
Dees_Troy43d8b002012-09-17 16:00:01 -04002429
Dees_Troye58d5262012-09-21 12:27:57 -04002430 if (Has_Android_Secure) {
Dees_Troye58d5262012-09-21 12:27:57 -04002431 if (!Wipe_AndSec())
2432 return false;
Gary Peck43acadf2012-11-21 21:19:01 -08002433 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002434 gui_msg(Msg("wiping=Wiping {1}")(Backup_Display_Name));
Ethan Yonker5eac2222014-06-11 12:22:55 -05002435 if (Has_Data_Media && Mount_Point == "/data" && Restore_File_System != Current_File_System) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002436 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 -05002437 if (!Wipe_Data_Without_Wiping_Media())
2438 return false;
2439 } else {
2440 if (!Wipe(Restore_File_System))
2441 return false;
2442 }
Dees_Troye58d5262012-09-21 12:27:57 -04002443 }
Matt Mower3c366972015-12-25 19:28:31 -06002444 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Backup_Display_Name, gui_parse_text("{@restoring_hdr}"));
Ethan Yonker74db1572015-10-28 12:44:49 -05002445 gui_msg(Msg("restoring=Restoring {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002446
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05002447 // Remount as read/write as needed so we can restore the backup
2448 if (!ReMount_RW(true))
Dees_Troy43d8b002012-09-17 16:00:01 -04002449 return false;
2450
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002451 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
Ethan Yonker87af5632014-02-10 11:56:35 -06002452 twrpTar tar;
bigbiffce8f83c2015-12-12 18:30:21 -05002453 tar.part_settings = part_settings;
Ethan Yonker87af5632014-02-10 11:56:35 -06002454 tar.setdir(Backup_Path);
2455 tar.setfn(Full_FileName);
2456 tar.backup_name = Backup_Name;
2457#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
2458 string Password;
2459 DataManager::GetValue("tw_restore_password", Password);
2460 if (!Password.empty())
2461 tar.setpassword(Password);
2462#endif
bigbiffce8f83c2015-12-12 18:30:21 -05002463 part_settings->progress->SetPartitionSize(Get_Restore_Size(part_settings));
2464 if (tar.extractTarFork() != 0)
Dees Troy4159aed2014-02-28 17:24:43 +00002465 ret = false;
2466 else
2467 ret = true;
2468#ifdef HAVE_CAPABILITIES
2469 // Restore capabilities to the run-as binary
2470 if (Mount_Point == "/system" && Mount(true) && TWFunc::Path_Exists("/system/bin/run-as")) {
2471 struct vfs_cap_data cap_data;
2472 uint64_t capabilities = (1 << CAP_SETUID) | (1 << CAP_SETGID);
2473
2474 memset(&cap_data, 0, sizeof(cap_data));
2475 cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE;
2476 cap_data.data[0].permitted = (uint32_t) (capabilities & 0xffffffff);
2477 cap_data.data[0].inheritable = 0;
2478 cap_data.data[1].permitted = (uint32_t) (capabilities >> 32);
2479 cap_data.data[1].inheritable = 0;
2480 if (setxattr("/system/bin/run-as", XATTR_NAME_CAPS, &cap_data, sizeof(cap_data), 0) < 0) {
2481 LOGINFO("Failed to reset capabilities of /system/bin/run-as binary.\n");
2482 } else {
2483 LOGINFO("Reset capabilities of /system/bin/run-as binary successful.\n");
2484 }
2485 }
2486#endif
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05002487 if (Mount_Read_Only || Mount_Flags & MS_RDONLY)
2488 // Remount as read only when restoration is complete
2489 ReMount(true);
2490
Dees Troy4159aed2014-02-28 17:24:43 +00002491 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04002492}
2493
bigbiffce8f83c2015-12-12 18:30:21 -05002494bool TWPartition::Restore_Image(PartitionSettings *part_settings) {
Ethan Yonker96af84a2015-01-05 14:58:36 -06002495 string Full_FileName;
bigbiffce8f83c2015-12-12 18:30:21 -05002496 string Restore_File_System = Get_Restore_File_System(part_settings);
Dees_Troy43d8b002012-09-17 16:00:01 -04002497
Matt Mower3c366972015-12-25 19:28:31 -06002498 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Backup_Display_Name, gui_parse_text("{@restoring_hdr}"));
Ethan Yonker74db1572015-10-28 12:44:49 -05002499 gui_msg(Msg("restoring=Restoring {1}...")(Backup_Display_Name));
bigbiffce8f83c2015-12-12 18:30:21 -05002500
2501 if (part_settings->adbbackup)
2502 Full_FileName = TW_ADB_RESTORE;
2503 else
Ethan Yonkere080c1f2016-09-19 13:50:25 -05002504 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
Gary Peck15e623d2012-11-21 21:07:58 -08002505
Ethan Yonker96af84a2015-01-05 14:58:36 -06002506 if (Restore_File_System == "emmc") {
bigbiffce8f83c2015-12-12 18:30:21 -05002507 if (!part_settings->adbbackup)
2508 part_settings->total_restore_size = (uint64_t)(TWFunc::Get_File_Size(Full_FileName));
2509 if (!Raw_Read_Write(part_settings))
Ethan Yonker96af84a2015-01-05 14:58:36 -06002510 return false;
2511 } else if (Restore_File_System == "mtd" || Restore_File_System == "bml") {
bigbiffce8f83c2015-12-12 18:30:21 -05002512 if (!Flash_Image_FI(Full_FileName, part_settings->progress))
2513 return false;
2514 }
2515
2516 if (part_settings->adbbackup) {
2517 if (!twadbbu::Write_TWEOF())
Ethan Yonker96af84a2015-01-05 14:58:36 -06002518 return false;
Gary Peck15e623d2012-11-21 21:07:58 -08002519 }
Dees_Troy43d8b002012-09-17 16:00:01 -04002520 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002521}
Dees_Troy5bf43922012-09-07 16:07:55 -04002522
2523bool TWPartition::Update_Size(bool Display_Error) {
Dees_Troy0550cfb2012-10-13 11:56:13 -04002524 bool ret = false, Was_Already_Mounted = false;
Dees_Troy51127312012-09-08 13:08:49 -04002525
Ethan Yonker1b190162016-12-05 15:25:19 -06002526 Find_Actual_Block_Device();
2527
2528 if (!Can_Be_Mounted && !Is_Encrypted) {
2529 if (TWFunc::Path_Exists(Actual_Block_Device) && Find_Partition_Size()) {
2530 Used = Size;
2531 Backup_Size = Size;
2532 return true;
2533 }
Dees_Troy5bf43922012-09-07 16:07:55 -04002534 return false;
Ethan Yonker1b190162016-12-05 15:25:19 -06002535 }
Dees_Troy5bf43922012-09-07 16:07:55 -04002536
Dees_Troy0550cfb2012-10-13 11:56:13 -04002537 Was_Already_Mounted = Is_Mounted();
Dees_Troy38bd7602012-09-14 13:33:53 -04002538 if (Removable || Is_Encrypted) {
2539 if (!Mount(false))
2540 return true;
2541 } else if (!Mount(Display_Error))
Dees_Troy5bf43922012-09-07 16:07:55 -04002542 return false;
Dees_Troy51127312012-09-08 13:08:49 -04002543
2544 ret = Get_Size_Via_statfs(Display_Error);
Dees_Troy0550cfb2012-10-13 11:56:13 -04002545 if (!ret || Size == 0) {
2546 if (!Get_Size_Via_df(Display_Error)) {
2547 if (!Was_Already_Mounted)
2548 UnMount(false);
Dees_Troy51127312012-09-08 13:08:49 -04002549 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04002550 }
2551 }
Dees_Troy51127312012-09-08 13:08:49 -04002552
Dees_Troy5bf43922012-09-07 16:07:55 -04002553 if (Has_Data_Media) {
2554 if (Mount(Display_Error)) {
Dees_Troy51127312012-09-08 13:08:49 -04002555 unsigned long long data_media_used, actual_data;
Ethan Yonker3fdcda42016-11-30 12:29:37 -06002556 Used = backup_exclusions.Get_Folder_Size(Mount_Point);
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002557 Backup_Size = Used;
2558 int bak = (int)(Used / 1048576LLU);
Dees_Troy51127312012-09-08 13:08:49 -04002559 int fre = (int)(Free / 1048576LLU);
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002560 LOGINFO("Data backup size is %iMB, free: %iMB.\n", bak, fre);
Dees_Troy0550cfb2012-10-13 11:56:13 -04002561 } else {
2562 if (!Was_Already_Mounted)
2563 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04002564 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04002565 }
Dees_Troye58d5262012-09-21 12:27:57 -04002566 } else if (Has_Android_Secure) {
2567 if (Mount(Display_Error))
Ethan Yonker3fdcda42016-11-30 12:29:37 -06002568 Backup_Size = backup_exclusions.Get_Folder_Size(Backup_Path);
Dees_Troy0550cfb2012-10-13 11:56:13 -04002569 else {
2570 if (!Was_Already_Mounted)
2571 UnMount(false);
Dees_Troye58d5262012-09-21 12:27:57 -04002572 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04002573 }
Dees_Troy5bf43922012-09-07 16:07:55 -04002574 }
Dees_Troy0550cfb2012-10-13 11:56:13 -04002575 if (!Was_Already_Mounted)
2576 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04002577 return true;
Dees_Troy51127312012-09-08 13:08:49 -04002578}
Dees_Troy38bd7602012-09-14 13:33:53 -04002579
2580void TWPartition::Find_Actual_Block_Device(void) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002581 if (Is_Decrypted && !Decrypted_Block_Device.empty()) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002582 Actual_Block_Device = Decrypted_Block_Device;
Ethan Yonker1b190162016-12-05 15:25:19 -06002583 if (TWFunc::Path_Exists(Decrypted_Block_Device)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002584 Is_Present = true;
Ethan Yonker1b190162016-12-05 15:25:19 -06002585 return;
2586 }
2587 } else if (SlotSelect && TWFunc::Path_Exists(Primary_Block_Device + PartitionManager.Get_Active_Slot_Suffix())) {
2588 Actual_Block_Device = Primary_Block_Device + PartitionManager.Get_Active_Slot_Suffix();
2589 unlink(Primary_Block_Device.c_str());
2590 symlink(Actual_Block_Device.c_str(), Primary_Block_Device.c_str()); // we create a non-slot symlink pointing to the currently selected slot which may assist zips with installing
2591 Is_Present = true;
2592 return;
Dees_Troy43d8b002012-09-17 16:00:01 -04002593 } else if (TWFunc::Path_Exists(Primary_Block_Device)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002594 Is_Present = true;
2595 Actual_Block_Device = Primary_Block_Device;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002596 return;
2597 }
Ethan Yonker1b190162016-12-05 15:25:19 -06002598 if (!Alternate_Block_Device.empty() && TWFunc::Path_Exists(Alternate_Block_Device)) {
Dees_Troy3f04d032012-10-07 18:20:09 -04002599 Actual_Block_Device = Alternate_Block_Device;
Dees_Troy38bd7602012-09-14 13:33:53 -04002600 Is_Present = true;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002601 } else {
Dees_Troy38bd7602012-09-14 13:33:53 -04002602 Is_Present = false;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002603 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002604}
2605
2606void TWPartition::Recreate_Media_Folder(void) {
2607 string Command;
Ethan Yonker66a19492015-12-10 10:19:45 -06002608 string Media_Path = Mount_Point + "/media";
Dees_Troy38bd7602012-09-14 13:33:53 -04002609
Ethan Yonkerbd7492d2016-12-07 13:55:01 -06002610 if (Is_FBE) {
2611 LOGINFO("Not recreating media folder on FBE\n");
2612 return;
2613 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002614 if (!Mount(true)) {
Ethan Yonker66a19492015-12-10 10:19:45 -06002615 gui_msg(Msg(msg::kError, "recreate_folder_err=Unable to recreate {1} folder.")(Media_Path));
2616 } else if (!TWFunc::Path_Exists(Media_Path)) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002617 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Ethan Yonker66a19492015-12-10 10:19:45 -06002618 LOGINFO("Recreating %s folder.\n", Media_Path.c_str());
2619 mkdir(Media_Path.c_str(), 0770);
xiaolu9416f4f2015-06-04 08:22:23 +08002620 string Internal_path = DataManager::GetStrValue("tw_internal_path");
2621 if (!Internal_path.empty()) {
2622 LOGINFO("Recreating %s folder.\n", Internal_path.c_str());
2623 mkdir(Internal_path.c_str(), 0770);
2624 }
2625#ifdef TW_INTERNAL_STORAGE_PATH
2626 mkdir(EXPAND(TW_INTERNAL_STORAGE_PATH), 0770);
2627#endif
Ethan Yonker5ef301e2014-10-14 10:04:44 -05002628#ifdef HAVE_SELINUX
thata3d31fb2014-12-21 22:27:40 +01002629 // Afterwards, we will try to set the
2630 // default metadata that we were hopefully able to get during
2631 // early boot.
Ethan Yonker66a19492015-12-10 10:19:45 -06002632 tw_set_default_metadata(Media_Path.c_str());
xiaolu9416f4f2015-06-04 08:22:23 +08002633 if (!Internal_path.empty())
2634 tw_set_default_metadata(Internal_path.c_str());
Ethan Yonker5ef301e2014-10-14 10:04:44 -05002635#endif
Ethan Yonker5eac2222014-06-11 12:22:55 -05002636 // Toggle mount to ensure that "internal sdcard" gets mounted
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002637 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Ethan Yonker5eac2222014-06-11 12:22:55 -05002638 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Dees_Troy38bd7602012-09-14 13:33:53 -04002639 }
Dees_Troy43d8b002012-09-17 16:00:01 -04002640}
Dees_Troye58d5262012-09-21 12:27:57 -04002641
2642void TWPartition::Recreate_AndSec_Folder(void) {
Dees_Troye58d5262012-09-21 12:27:57 -04002643 if (!Has_Android_Secure)
2644 return;
Dees_Troy2673cec2013-04-02 20:22:16 +00002645 LOGINFO("Creating %s: %s\n", Backup_Display_Name.c_str(), Symlink_Path.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -04002646 if (!Mount(true)) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002647 gui_msg(Msg(msg::kError, "recreate_folder_err=Unable to recreate {1} folder.")(Backup_Name));
Dees_Troye58d5262012-09-21 12:27:57 -04002648 } else if (!TWFunc::Path_Exists(Symlink_Path)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00002649 LOGINFO("Recreating %s folder.\n", Backup_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002650 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05002651 mkdir(Symlink_Path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002652 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04002653 }
2654}
bigbiff7cb4c332014-11-26 20:36:07 -05002655
2656uint64_t TWPartition::Get_Max_FileSize() {
2657 uint64_t maxFileSize = 0;
2658 const uint64_t constGB = (uint64_t) 1024 * 1024 * 1024;
2659 const uint64_t constTB = (uint64_t) constGB * 1024;
2660 const uint64_t constPB = (uint64_t) constTB * 1024;
2661 const uint64_t constEB = (uint64_t) constPB * 1024;
bigbiff0c532032014-12-21 13:41:26 -05002662 if (Current_File_System == "ext4")
2663 maxFileSize = 16 * constTB; //16 TB
2664 else if (Current_File_System == "vfat")
2665 maxFileSize = 4 * constGB; //4 GB
2666 else if (Current_File_System == "ntfs")
2667 maxFileSize = 256 * constTB; //256 TB
2668 else if (Current_File_System == "exfat")
2669 maxFileSize = 16 * constPB; //16 PB
2670 else if (Current_File_System == "ext3")
2671 maxFileSize = 2 * constTB; //2 TB
2672 else if (Current_File_System == "f2fs")
2673 maxFileSize = 3.94 * constTB; //3.94 TB
bigbiff7cb4c332014-11-26 20:36:07 -05002674 else
2675 maxFileSize = 100000000L;
2676 return maxFileSize - 1;
2677}
2678
bigbiffce8f83c2015-12-12 18:30:21 -05002679bool TWPartition::Flash_Image(PartitionSettings *part_settings) {
2680 string Restore_File_System, full_filename;
Ethan Yonker96af84a2015-01-05 14:58:36 -06002681
Ethan Yonkere080c1f2016-09-19 13:50:25 -05002682 full_filename = part_settings->Backup_Folder + "/" + Backup_FileName;
Ethan Yonker96af84a2015-01-05 14:58:36 -06002683
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002684 LOGINFO("Image filename is: %s\n", Backup_FileName.c_str());
bigbiffce8f83c2015-12-12 18:30:21 -05002685
2686 if (Backup_Method == BM_FILES) {
Ethan Yonker96af84a2015-01-05 14:58:36 -06002687 LOGERR("Cannot flash images to file systems\n");
2688 return false;
2689 } else if (!Can_Flash_Img) {
2690 LOGERR("Cannot flash images to partitions %s\n", Display_Name.c_str());
2691 return false;
2692 } else {
2693 if (!Find_Partition_Size()) {
2694 LOGERR("Unable to find partition size for '%s'\n", Mount_Point.c_str());
2695 return false;
2696 }
bigbiffce8f83c2015-12-12 18:30:21 -05002697 unsigned long long image_size = TWFunc::Get_File_Size(full_filename);
Ethan Yonker96af84a2015-01-05 14:58:36 -06002698 if (image_size > Size) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002699 LOGINFO("Size (%llu bytes) of image '%s' is larger than target device '%s' (%llu bytes)\n",
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002700 image_size, Backup_FileName.c_str(), Actual_Block_Device.c_str(), Size);
Ethan Yonker74db1572015-10-28 12:44:49 -05002701 gui_err("img_size_err=Size of image is larger than target device");
Ethan Yonker96af84a2015-01-05 14:58:36 -06002702 return false;
2703 }
bigbiffce8f83c2015-12-12 18:30:21 -05002704 if (Backup_Method == BM_DD) {
2705 if (!part_settings->adbbackup) {
2706 if (Is_Sparse_Image(full_filename)) {
2707 return Flash_Sparse_Image(full_filename);
2708 }
Ethan Yonker472f5062016-02-25 13:47:30 -06002709 }
bigbiffce8f83c2015-12-12 18:30:21 -05002710 unsigned long long file_size = (unsigned long long)(TWFunc::Get_File_Size(full_filename));
2711 return Raw_Read_Write(part_settings);
2712 } else if (Backup_Method == BM_FLASH_UTILS) {
2713 return Flash_Image_FI(full_filename, NULL);
Ethan Yonker472f5062016-02-25 13:47:30 -06002714 }
Ethan Yonker96af84a2015-01-05 14:58:36 -06002715 }
2716
2717 LOGERR("Unknown flash method for '%s'\n", Mount_Point.c_str());
2718 return false;
2719}
2720
Ethan Yonker472f5062016-02-25 13:47:30 -06002721bool TWPartition::Is_Sparse_Image(const string& Filename) {
HashBanged974bb2016-01-30 14:20:09 -05002722 uint32_t magic = 0;
2723 int fd = open(Filename.c_str(), O_RDONLY);
2724 if (fd < 0) {
2725 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Filename)(strerror(errno)));
2726 return false;
2727 }
bigbiffce8f83c2015-12-12 18:30:21 -05002728
HashBanged974bb2016-01-30 14:20:09 -05002729 if (read(fd, &magic, sizeof(magic)) != sizeof(magic)) {
2730 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Filename)(strerror(errno)));
2731 close(fd);
2732 return false;
2733 }
2734 close(fd);
Ethan Yonker472f5062016-02-25 13:47:30 -06002735 if (magic == SPARSE_HEADER_MAGIC)
2736 return true;
2737 return false;
2738}
2739
2740bool TWPartition::Flash_Sparse_Image(const string& Filename) {
2741 string Command;
2742
2743 gui_msg(Msg("flashing=Flashing {1}...")(Display_Name));
2744
2745 Command = "simg2img '" + Filename + "' '" + Actual_Block_Device + "'";
Ethan Yonker96af84a2015-01-05 14:58:36 -06002746 LOGINFO("Flash command: '%s'\n", Command.c_str());
2747 TWFunc::Exec_Cmd(Command);
2748 return true;
2749}
2750
Ethan Yonker472f5062016-02-25 13:47:30 -06002751bool TWPartition::Flash_Image_FI(const string& Filename, ProgressTracking *progress) {
Ethan Yonker96af84a2015-01-05 14:58:36 -06002752 string Command;
Ethan Yonker472f5062016-02-25 13:47:30 -06002753 unsigned long long file_size;
Ethan Yonker96af84a2015-01-05 14:58:36 -06002754
Ethan Yonker74db1572015-10-28 12:44:49 -05002755 gui_msg(Msg("flashing=Flashing {1}...")(Display_Name));
Ethan Yonker472f5062016-02-25 13:47:30 -06002756 if (progress) {
2757 file_size = (unsigned long long)(TWFunc::Get_File_Size(Filename));
2758 progress->SetPartitionSize(file_size);
2759 }
Ethan Yonker96af84a2015-01-05 14:58:36 -06002760 // Sometimes flash image doesn't like to flash due to the first 2KB matching, so we erase first to ensure that it flashes
2761 Command = "erase_image " + MTD_Name;
2762 LOGINFO("Erase command: '%s'\n", Command.c_str());
2763 TWFunc::Exec_Cmd(Command);
2764 Command = "flash_image " + MTD_Name + " '" + Filename + "'";
2765 LOGINFO("Flash command: '%s'\n", Command.c_str());
2766 TWFunc::Exec_Cmd(Command);
Ethan Yonker472f5062016-02-25 13:47:30 -06002767 if (progress)
2768 progress->UpdateSize(file_size);
Ethan Yonker96af84a2015-01-05 14:58:36 -06002769 return true;
2770}
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05002771
2772void TWPartition::Change_Mount_Read_Only(bool new_value) {
2773 Mount_Read_Only = new_value;
2774}
2775
bigbiffce8f83c2015-12-12 18:30:21 -05002776bool TWPartition::Is_Read_Only() {
2777 return Mount_Read_Only;
2778}
2779
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05002780int TWPartition::Check_Lifetime_Writes() {
2781 bool original_read_only = Mount_Read_Only;
2782 int ret = 1;
2783
2784 Mount_Read_Only = true;
2785 if (Mount(false)) {
2786 Find_Actual_Block_Device();
2787 string block = basename(Actual_Block_Device.c_str());
2788 string file = "/sys/fs/" + Current_File_System + "/" + block + "/lifetime_write_kbytes";
2789 string result;
2790 if (TWFunc::Path_Exists(file)) {
2791 if (TWFunc::read_file(file, result) != 0) {
2792 LOGINFO("Check_Lifetime_Writes of '%s' failed to read_file\n", file.c_str());
2793 } else {
2794 LOGINFO("Check_Lifetime_Writes result: '%s'\n", result.c_str());
2795 if (result == "0") {
2796 ret = 0;
2797 }
2798 }
2799 } else {
2800 LOGINFO("Check_Lifetime_Writes file does not exist '%s'\n", file.c_str());
2801 }
2802 UnMount(true);
2803 } else {
2804 LOGINFO("Check_Lifetime_Writes failed to mount '%s'\n", Mount_Point.c_str());
2805 }
2806 Mount_Read_Only = original_read_only;
2807 return ret;
2808}
Ethan Yonker66a19492015-12-10 10:19:45 -06002809
2810int TWPartition::Decrypt_Adopted() {
2811#ifdef TW_INCLUDE_CRYPTO
2812 int ret = 1;
2813 Is_Adopted_Storage = false;
2814 string Adopted_Key_File = "";
2815
2816 if (!Removable)
2817 return ret;
2818
2819 int fd = open(Alternate_Block_Device.c_str(), O_RDONLY);
2820 if (fd < 0) {
2821 LOGINFO("failed to open '%s'\n", Alternate_Block_Device.c_str());
2822 return ret;
2823 }
2824 char type_guid[80];
2825 char part_guid[80];
2826
2827 if (gpt_disk_get_partition_info(fd, 2, type_guid, part_guid) == 0) {
2828 LOGINFO("type: '%s'\n", type_guid);
2829 LOGINFO("part: '%s'\n", part_guid);
2830 Adopted_GUID = part_guid;
2831 LOGINFO("Adopted_GUID '%s'\n", Adopted_GUID.c_str());
2832 if (strcmp(type_guid, TWGptAndroidExpand) == 0) {
2833 LOGINFO("android_expand found\n");
2834 Adopted_Key_File = "/data/misc/vold/expand_";
2835 Adopted_Key_File += part_guid;
2836 Adopted_Key_File += ".key";
2837 if (TWFunc::Path_Exists(Adopted_Key_File)) {
2838 Is_Adopted_Storage = true;
2839 /* Until we find a use case for this, I think it is safe
2840 * to disable USB Mass Storage whenever adopted storage
2841 * is present.
2842 */
2843 LOGINFO("Detected adopted storage, disabling USB mass storage mode\n");
2844 DataManager::SetValue("tw_has_usb_storage", 0);
2845 }
2846 }
2847 }
2848
2849 if (Is_Adopted_Storage) {
2850 string Adopted_Block_Device = Alternate_Block_Device + "p2";
2851 if (!TWFunc::Path_Exists(Adopted_Block_Device)) {
2852 Adopted_Block_Device = Alternate_Block_Device + "2";
2853 if (!TWFunc::Path_Exists(Adopted_Block_Device)) {
2854 LOGINFO("Adopted block device does not exist\n");
2855 goto exit;
2856 }
2857 }
2858 LOGINFO("key file is '%s', block device '%s'\n", Adopted_Key_File.c_str(), Adopted_Block_Device.c_str());
2859 char crypto_blkdev[MAXPATHLEN];
2860 std::string thekey;
2861 int fdkey = open(Adopted_Key_File.c_str(), O_RDONLY);
2862 if (fdkey < 0) {
2863 LOGINFO("failed to open key file\n");
2864 goto exit;
2865 }
2866 char buf[512];
2867 ssize_t n;
2868 while ((n = read(fdkey, &buf[0], sizeof(buf))) > 0) {
2869 thekey.append(buf, n);
2870 }
2871 close(fdkey);
2872 unsigned char* key = (unsigned char*) thekey.data();
2873 cryptfs_revert_ext_volume(part_guid);
2874
2875 ret = cryptfs_setup_ext_volume(part_guid, Adopted_Block_Device.c_str(), key, thekey.size(), crypto_blkdev);
2876 if (ret == 0) {
2877 LOGINFO("adopted storage new block device: '%s'\n", crypto_blkdev);
2878 Decrypted_Block_Device = crypto_blkdev;
2879 Is_Decrypted = true;
2880 Is_Encrypted = true;
2881 Find_Actual_Block_Device();
Kjell Braden3126a112016-06-19 16:58:15 +00002882 if (!Mount_Storage_Retry(false)) {
Ethan Yonker66a19492015-12-10 10:19:45 -06002883 LOGERR("Failed to mount decrypted adopted storage device\n");
2884 Is_Decrypted = false;
2885 Is_Encrypted = false;
2886 cryptfs_revert_ext_volume(part_guid);
2887 ret = 1;
2888 } else {
Ethan Yonkerfcf3f242016-02-16 12:30:26 -06002889 UnMount(false);
2890 Has_Android_Secure = false;
2891 Symlink_Path = "";
2892 Symlink_Mount_Point = "";
2893 Backup_Name = Mount_Point.substr(1);
2894 Backup_Path = Mount_Point;
2895 TWPartition* sdext = PartitionManager.Find_Partition_By_Path("/sd-ext");
2896 if (sdext && sdext->Actual_Block_Device == Adopted_Block_Device) {
2897 LOGINFO("Removing /sd-ext from partition list due to adopted storage\n");
2898 PartitionManager.Remove_Partition_By_Path("/sd-ext");
2899 }
Ethan Yonker66a19492015-12-10 10:19:45 -06002900 Setup_Data_Media();
2901 Recreate_Media_Folder();
2902 Wipe_Available_in_GUI = true;
2903 Wipe_During_Factory_Reset = true;
2904 Can_Be_Backed_Up = true;
2905 Can_Encrypt_Backup = true;
2906 Use_Userdata_Encryption = true;
2907 Is_Storage = true;
2908 Storage_Name = "Adopted Storage";
2909 Is_SubPartition = true;
2910 SubPartition_Of = "/data";
2911 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
2912 DataManager::SetValue("tw_has_adopted_storage", 1);
2913 }
2914 } else {
2915 LOGERR("Failed to setup adopted storage decryption\n");
2916 }
2917 }
2918exit:
2919 return ret;
2920#else
2921 LOGINFO("Decrypt_Adopted: no crypto support\n");
2922 return 1;
2923#endif
2924}
2925
2926void TWPartition::Revert_Adopted() {
2927#ifdef TW_INCLUDE_CRYPTO
2928 if (!Adopted_GUID.empty()) {
2929 PartitionManager.Remove_MTP_Storage(Mount_Point);
2930 UnMount(false);
2931 cryptfs_revert_ext_volume(Adopted_GUID.c_str());
2932 Is_Adopted_Storage = false;
2933 Is_Encrypted = false;
2934 Is_Decrypted = false;
2935 Decrypted_Block_Device = "";
2936 Find_Actual_Block_Device();
2937 Wipe_During_Factory_Reset = false;
2938 Can_Be_Backed_Up = false;
2939 Can_Encrypt_Backup = false;
2940 Use_Userdata_Encryption = false;
2941 Is_SubPartition = false;
2942 SubPartition_Of = "";
2943 Has_Data_Media = false;
2944 Storage_Path = Mount_Point;
2945 if (!Symlink_Mount_Point.empty()) {
2946 TWPartition* Dat = PartitionManager.Find_Partition_By_Path("/data");
2947 if (Dat) {
2948 Dat->UnMount(false);
2949 Dat->Symlink_Mount_Point = Symlink_Mount_Point;
2950 }
2951 Symlink_Mount_Point = "";
2952 }
2953 }
2954#else
2955 LOGINFO("Revert_Adopted: no crypto support\n");
2956#endif
2957}