blob: bbee37d81cadd1a1666a1025fc5cab033bfd81cb [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"
Ethan Yonkerbd7492d2016-12-07 13:55:01 -060063 #endif
Ethan Yonkerbc85b632015-08-09 12:48:14 -050064#else
65 #define CRYPT_FOOTER_OFFSET 0x4000
Ethan Yonker71413f42014-02-26 13:36:08 -060066#endif
Dees_Troy5bf43922012-09-07 16:07:55 -040067}
Matt Mower87413642017-01-17 21:14:46 -060068#include <selinux/selinux.h>
Ethan Yonkerf27497f2014-02-09 11:48:33 -060069#include <selinux/label.h>
Dees Troy4159aed2014-02-28 17:24:43 +000070#ifdef HAVE_CAPABILITIES
71#include <sys/capability.h>
72#include <sys/xattr.h>
73#include <linux/xattr.h>
74#endif
HashBanged974bb2016-01-30 14:20:09 -050075#include <sparse_format.h>
Ethan Yonker472f5062016-02-25 13:47:30 -060076#include "progresstracking.hpp"
Dees_Troy51a0e822012-09-05 15:24:24 -040077
bigbiff bigbiff9c754052013-01-09 09:09:08 -050078using namespace std;
79
Dees_Troya95f55c2013-08-17 13:14:43 +000080extern struct selabel_handle *selinux_handle;
Ethan Yonker6277c792014-09-15 14:54:30 -050081extern bool datamedia;
Dees_Troya95f55c2013-08-17 13:14:43 +000082
Hashcode62bd9e02013-11-19 21:59:42 -080083struct flag_list {
84 const char *name;
85 unsigned flag;
86};
87
Matt Mower4ab42b12016-04-21 13:52:18 -050088const struct flag_list mount_flags[] = {
Hashcode62bd9e02013-11-19 21:59:42 -080089 { "noatime", MS_NOATIME },
90 { "noexec", MS_NOEXEC },
91 { "nosuid", MS_NOSUID },
92 { "nodev", MS_NODEV },
93 { "nodiratime", MS_NODIRATIME },
94 { "ro", MS_RDONLY },
95 { "rw", 0 },
96 { "remount", MS_REMOUNT },
97 { "bind", MS_BIND },
98 { "rec", MS_REC },
Dees Troyc4bc30e2014-02-03 15:04:19 +000099#ifdef MS_UNBINDABLE
Hashcode62bd9e02013-11-19 21:59:42 -0800100 { "unbindable", MS_UNBINDABLE },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000101#endif
102#ifdef MS_PRIVATE
Hashcode62bd9e02013-11-19 21:59:42 -0800103 { "private", MS_PRIVATE },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000104#endif
105#ifdef MS_SLAVE
Hashcode62bd9e02013-11-19 21:59:42 -0800106 { "slave", MS_SLAVE },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000107#endif
108#ifdef MS_SHARED
Hashcode62bd9e02013-11-19 21:59:42 -0800109 { "shared", MS_SHARED },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000110#endif
Hashcode62bd9e02013-11-19 21:59:42 -0800111 { "sync", MS_SYNCHRONOUS },
112 { "defaults", 0 },
113 { 0, 0 },
114};
115
Matt Mower2416a502016-04-12 19:54:46 -0500116enum TW_FSTAB_FLAGS {
117 TWFLAG_DEFAULTS, // Retain position
118 TWFLAG_ANDSEC,
119 TWFLAG_BACKUP,
120 TWFLAG_BACKUPNAME,
121 TWFLAG_BLOCKSIZE,
122 TWFLAG_CANBEWIPED,
123 TWFLAG_CANENCRYPTBACKUP,
124 TWFLAG_DISPLAY,
125 TWFLAG_ENCRYPTABLE,
126 TWFLAG_FLASHIMG,
127 TWFLAG_FORCEENCRYPT,
128 TWFLAG_FSFLAGS,
129 TWFLAG_IGNOREBLKID,
130 TWFLAG_LENGTH,
131 TWFLAG_MOUNTTODECRYPT,
132 TWFLAG_REMOVABLE,
133 TWFLAG_RETAINLAYOUTVERSION,
134 TWFLAG_SETTINGSSTORAGE,
135 TWFLAG_STORAGE,
136 TWFLAG_STORAGENAME,
137 TWFLAG_SUBPARTITIONOF,
138 TWFLAG_SYMLINK,
139 TWFLAG_USERDATAENCRYPTBACKUP,
140 TWFLAG_USERMRF,
141 TWFLAG_WIPEDURINGFACTORYRESET,
142 TWFLAG_WIPEINGUI,
Ethan Yonker1b190162016-12-05 15:25:19 -0600143 TWFLAG_SLOTSELECT,
Matt Mower2416a502016-04-12 19:54:46 -0500144};
145
146/* Flags without a trailing '=' are considered dual format flags and can be
147 * written as either 'flagname' or 'flagname=', where the character following
148 * the '=' is Y,y,1 for true and false otherwise.
149 */
150const struct flag_list tw_flags[] = {
151 { "andsec", TWFLAG_ANDSEC },
152 { "backup", TWFLAG_BACKUP },
153 { "backupname=", TWFLAG_BACKUPNAME },
154 { "blocksize=", TWFLAG_BLOCKSIZE },
155 { "canbewiped", TWFLAG_CANBEWIPED },
156 { "canencryptbackup", TWFLAG_CANENCRYPTBACKUP },
157 { "defaults", TWFLAG_DEFAULTS },
158 { "display=", TWFLAG_DISPLAY },
159 { "encryptable=", TWFLAG_ENCRYPTABLE },
160 { "flashimg", TWFLAG_FLASHIMG },
161 { "forceencrypt=", TWFLAG_FORCEENCRYPT },
162 { "fsflags=", TWFLAG_FSFLAGS },
163 { "ignoreblkid", TWFLAG_IGNOREBLKID },
164 { "length=", TWFLAG_LENGTH },
165 { "mounttodecrypt", TWFLAG_MOUNTTODECRYPT },
166 { "removable", TWFLAG_REMOVABLE },
167 { "retainlayoutversion", TWFLAG_RETAINLAYOUTVERSION },
168 { "settingsstorage", TWFLAG_SETTINGSSTORAGE },
169 { "storage", TWFLAG_STORAGE },
170 { "storagename=", TWFLAG_STORAGENAME },
171 { "subpartitionof=", TWFLAG_SUBPARTITIONOF },
172 { "symlink=", TWFLAG_SYMLINK },
173 { "userdataencryptbackup", TWFLAG_USERDATAENCRYPTBACKUP },
174 { "usermrf", TWFLAG_USERMRF },
175 { "wipeduringfactoryreset", TWFLAG_WIPEDURINGFACTORYRESET },
176 { "wipeingui", TWFLAG_WIPEINGUI },
Ethan Yonker1b190162016-12-05 15:25:19 -0600177 { "slotselect", TWFLAG_SLOTSELECT },
Matt Mower2416a502016-04-12 19:54:46 -0500178 { 0, 0 },
179};
180
that9e0593e2014-10-08 00:01:24 +0200181TWPartition::TWPartition() {
Dees_Troy51a0e822012-09-05 15:24:24 -0400182 Can_Be_Mounted = false;
183 Can_Be_Wiped = false;
Dees_Troya13d74f2013-03-24 08:54:55 -0500184 Can_Be_Backed_Up = false;
Vojtech Bocek1dc30982013-08-30 21:49:30 +0200185 Use_Rm_Rf = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400186 Wipe_During_Factory_Reset = false;
187 Wipe_Available_in_GUI = false;
188 Is_SubPartition = false;
Dees_Troy2691f9d2012-09-24 11:15:49 -0400189 Has_SubPartition = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400190 SubPartition_Of = "";
191 Symlink_Path = "";
192 Symlink_Mount_Point = "";
193 Mount_Point = "";
Dees_Troye58d5262012-09-21 12:27:57 -0400194 Backup_Path = "";
Dees_Troy38bd7602012-09-14 13:33:53 -0400195 Actual_Block_Device = "";
196 Primary_Block_Device = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400197 Alternate_Block_Device = "";
198 Removable = false;
199 Is_Present = false;
200 Length = 0;
201 Size = 0;
202 Used = 0;
203 Free = 0;
204 Backup_Size = 0;
205 Can_Be_Encrypted = false;
206 Is_Encrypted = false;
207 Is_Decrypted = false;
Ethan Yonkerbd7492d2016-12-07 13:55:01 -0600208 Is_FBE = false;
Ethan Yonker253368a2014-11-25 15:00:52 -0600209 Mount_To_Decrypt = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400210 Decrypted_Block_Device = "";
211 Display_Name = "";
Dees_Troya13d74f2013-03-24 08:54:55 -0500212 Backup_Display_Name = "";
213 Storage_Name = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400214 Backup_Name = "";
Dees_Troy63c8df72012-09-10 14:02:05 -0400215 Backup_FileName = "";
Dees_Troy38bd7602012-09-14 13:33:53 -0400216 MTD_Name = "";
bigbiffce8f83c2015-12-12 18:30:21 -0500217 Backup_Method = BM_NONE;
Dees_Troy83bd4832013-05-04 12:39:56 +0000218 Can_Encrypt_Backup = false;
219 Use_Userdata_Encryption = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400220 Has_Data_Media = false;
Dees_Troye58d5262012-09-21 12:27:57 -0400221 Has_Android_Secure = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400222 Is_Storage = false;
Dees_Troya13d74f2013-03-24 08:54:55 -0500223 Is_Settings_Storage = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400224 Storage_Path = "";
225 Current_File_System = "";
226 Fstab_File_System = "";
Hashcode62bd9e02013-11-19 21:59:42 -0800227 Mount_Flags = 0;
228 Mount_Options = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400229 Format_Block_Size = 0;
Dees_Troy68cab492012-12-12 19:29:35 +0000230 Ignore_Blkid = false;
Dees_Troy16c2b312013-01-15 16:51:18 +0000231 Retain_Layout_Version = false;
Ethan Yonker253368a2014-11-25 15:00:52 -0600232 Crypto_Key_Location = "footer";
Ethan Yonker726a0202014-12-16 20:01:38 -0600233 MTP_Storage_ID = 0;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600234 Can_Flash_Img = false;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500235 Mount_Read_Only = false;
Ethan Yonker66a19492015-12-10 10:19:45 -0600236 Is_Adopted_Storage = false;
237 Adopted_GUID = "";
Ethan Yonker1b190162016-12-05 15:25:19 -0600238 SlotSelect = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400239}
240
241TWPartition::~TWPartition(void) {
242 // Do nothing
243}
244
Matt Mower2b2dd152016-04-26 11:24:08 -0500245bool TWPartition::Process_Fstab_Line(const char *fstab_line, bool Display_Error) {
246 char full_line[MAX_FSTAB_LINE_LENGTH];
Matt Mower2416a502016-04-12 19:54:46 -0500247 char twflags[MAX_FSTAB_LINE_LENGTH] = "";
Dees_Troy5bf43922012-09-07 16:07:55 -0400248 char* ptr;
Matt Mower2b2dd152016-04-26 11:24:08 -0500249 int line_len = strlen(fstab_line), index = 0, item_index = 0;
Dees_Troya13d74f2013-03-24 08:54:55 -0500250 bool skip = false;
Dees_Troy5bf43922012-09-07 16:07:55 -0400251
Matt Mower2b2dd152016-04-26 11:24:08 -0500252 strlcpy(full_line, fstab_line, sizeof(full_line));
Dees_Troy51127312012-09-08 13:08:49 -0400253 for (index = 0; index < line_len; index++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500254 if (full_line[index] == 34)
255 skip = !skip;
256 if (!skip && full_line[index] <= 32)
Dees_Troy5bf43922012-09-07 16:07:55 -0400257 full_line[index] = '\0';
Dees_Troy5bf43922012-09-07 16:07:55 -0400258 }
Dees_Troy7c2dec82012-09-26 09:49:14 -0400259 Mount_Point = full_line;
Dees_Troy2673cec2013-04-02 20:22:16 +0000260 LOGINFO("Processing '%s'\n", Mount_Point.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -0400261 Backup_Path = Mount_Point;
Dees_Troya13d74f2013-03-24 08:54:55 -0500262 Storage_Path = Mount_Point;
Dees_Troy70737fa2013-04-08 13:19:20 +0000263 Display_Name = full_line + 1;
264 Backup_Display_Name = Display_Name;
265 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400266 index = Mount_Point.size();
267 while (index < line_len) {
268 while (index < line_len && full_line[index] == '\0')
269 index++;
270 if (index >= line_len)
271 continue;
272 ptr = full_line + index;
273 if (item_index == 0) {
274 // File System
275 Fstab_File_System = ptr;
276 Current_File_System = ptr;
277 item_index++;
278 } else if (item_index == 1) {
279 // Primary Block Device
Dees_Troy38bd7602012-09-14 13:33:53 -0400280 if (Fstab_File_System == "mtd" || Fstab_File_System == "yaffs2") {
Dees_Troy094207a2012-09-26 12:00:39 -0400281 MTD_Name = ptr;
282 Find_MTD_Block_Device(MTD_Name);
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400283 } else if (Fstab_File_System == "bml") {
284 if (Mount_Point == "/boot")
285 MTD_Name = "boot";
286 else if (Mount_Point == "/recovery")
287 MTD_Name = "recovery";
288 Primary_Block_Device = ptr;
289 if (*ptr != '/')
Dees_Troy2673cec2013-04-02 20:22:16 +0000290 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 -0400291 } else if (*ptr != '/') {
Dees_Troy5bf43922012-09-07 16:07:55 -0400292 if (Display_Error)
Matt Mower2b2dd152016-04-26 11:24:08 -0500293 LOGERR("Invalid block device '%s' in fstab line '%s'", ptr, fstab_line);
Dees_Troy5bf43922012-09-07 16:07:55 -0400294 else
Matt Mower2b2dd152016-04-26 11:24:08 -0500295 LOGINFO("Invalid block device '%s' in fstab line '%s'", ptr, fstab_line);
Matt Mower72c87ce2016-04-26 14:34:56 -0500296 return false;
Dees_Troy38bd7602012-09-14 13:33:53 -0400297 } else {
298 Primary_Block_Device = ptr;
299 Find_Real_Block_Device(Primary_Block_Device, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400300 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400301 item_index++;
302 } else if (item_index > 1) {
303 if (*ptr == '/') {
304 // Alternate Block Device
305 Alternate_Block_Device = ptr;
306 Find_Real_Block_Device(Alternate_Block_Device, Display_Error);
307 } else if (strlen(ptr) > 7 && strncmp(ptr, "length=", 7) == 0) {
308 // Partition length
309 ptr += 7;
310 Length = atoi(ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400311 } else if (strlen(ptr) > 6 && strncmp(ptr, "flags=", 6) == 0) {
312 // Custom flags, save for later so that new values aren't overwritten by defaults
313 ptr += 6;
Matt Mower2416a502016-04-12 19:54:46 -0500314 strlcpy(twflags, ptr, sizeof(twflags));
Dees_Troy38bd7602012-09-14 13:33:53 -0400315 } else if (strlen(ptr) == 4 && (strncmp(ptr, "NULL", 4) == 0 || strncmp(ptr, "null", 4) == 0 || strncmp(ptr, "null", 4) == 0)) {
316 // Do nothing
Dees_Troy5bf43922012-09-07 16:07:55 -0400317 } else {
318 // Unhandled data
Matt Mower2b2dd152016-04-26 11:24:08 -0500319 LOGINFO("Unhandled fstab information '%s' in fstab line '%s'\n", ptr, fstab_line);
Dees_Troy5bf43922012-09-07 16:07:55 -0400320 }
321 }
322 while (index < line_len && full_line[index] != '\0')
323 index++;
324 }
325
326 if (!Is_File_System(Fstab_File_System) && !Is_Image(Fstab_File_System)) {
327 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000328 LOGERR("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400329 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000330 LOGINFO("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Matt Mower72c87ce2016-04-26 14:34:56 -0500331 return false;
Dees_Troy5bf43922012-09-07 16:07:55 -0400332 } else if (Is_File_System(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400333 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400334 Setup_File_System(Display_Error);
335 if (Mount_Point == "/system") {
336 Display_Name = "System";
Dees_Troya13d74f2013-03-24 08:54:55 -0500337 Backup_Display_Name = Display_Name;
338 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400339 Wipe_Available_in_GUI = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500340 Can_Be_Backed_Up = true;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500341 Mount_Read_Only = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400342 } else if (Mount_Point == "/data") {
343 Display_Name = "Data";
Dees_Troya13d74f2013-03-24 08:54:55 -0500344 Backup_Display_Name = Display_Name;
345 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400346 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400347 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500348 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000349 Can_Encrypt_Backup = true;
350 Use_Userdata_Encryption = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400351 } else if (Mount_Point == "/cache") {
352 Display_Name = "Cache";
Dees_Troya13d74f2013-03-24 08:54:55 -0500353 Backup_Display_Name = Display_Name;
354 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400355 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400356 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500357 Can_Be_Backed_Up = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400358 } else if (Mount_Point == "/datadata") {
Dees_Troy51127312012-09-08 13:08:49 -0400359 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400360 Display_Name = "DataData";
Dees_Troya13d74f2013-03-24 08:54:55 -0500361 Backup_Display_Name = Display_Name;
362 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400363 Is_SubPartition = true;
364 SubPartition_Of = "/data";
Dees_Troy5bf43922012-09-07 16:07:55 -0400365 DataManager::SetValue(TW_HAS_DATADATA, 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500366 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000367 Can_Encrypt_Backup = true;
368 Use_Userdata_Encryption = false; // This whole partition should be encrypted
Dees_Troy5bf43922012-09-07 16:07:55 -0400369 } else if (Mount_Point == "/sd-ext") {
Dees_Troy51127312012-09-08 13:08:49 -0400370 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400371 Display_Name = "SD-Ext";
Dees_Troya13d74f2013-03-24 08:54:55 -0500372 Backup_Display_Name = Display_Name;
373 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400374 Wipe_Available_in_GUI = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400375 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500376 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000377 Can_Encrypt_Backup = true;
378 Use_Userdata_Encryption = true;
Dees_Troy2c50e182012-09-26 20:05:28 -0400379 } else if (Mount_Point == "/boot") {
380 Display_Name = "Boot";
Dees_Troya13d74f2013-03-24 08:54:55 -0500381 Backup_Display_Name = Display_Name;
Dees_Troy2c50e182012-09-26 20:05:28 -0400382 DataManager::SetValue("tw_boot_is_mountable", 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500383 Can_Be_Backed_Up = true;
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500384 } else if (Mount_Point == "/vendor") {
385 Display_Name = "Vendor";
386 Backup_Display_Name = Display_Name;
387 Storage_Name = Display_Name;
388 Mount_Read_Only = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400389 }
390#ifdef TW_EXTERNAL_STORAGE_PATH
391 if (Mount_Point == EXPAND(TW_EXTERNAL_STORAGE_PATH)) {
392 Is_Storage = true;
393 Storage_Path = EXPAND(TW_EXTERNAL_STORAGE_PATH);
Dees_Troyc51f1f92012-09-20 15:32:13 -0400394 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500395 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400396#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000397 if (Mount_Point == "/sdcard" || Mount_Point == "/external_sd" || Mount_Point == "/external_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400398 Is_Storage = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400399 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500400 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400401#endif
Dees_Troyb05ddee2013-01-28 20:24:50 +0000402 }
Dees_Troy8170a922012-09-18 15:40:25 -0400403#ifdef TW_INTERNAL_STORAGE_PATH
404 if (Mount_Point == EXPAND(TW_INTERNAL_STORAGE_PATH)) {
405 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500406 Is_Settings_Storage = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400407 Storage_Path = EXPAND(TW_INTERNAL_STORAGE_PATH);
Dees_Troya13d74f2013-03-24 08:54:55 -0500408 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400409 }
410#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000411 if (Mount_Point == "/emmc" || Mount_Point == "/internal_sd" || Mount_Point == "/internal_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400412 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500413 Is_Settings_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500414 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400415 }
416#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400417 } else if (Is_Image(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400418 Find_Actual_Block_Device();
Ethan Yonker1b190162016-12-05 15:25:19 -0600419 Setup_Image();
Dees_Troya13d74f2013-03-24 08:54:55 -0500420 if (Mount_Point == "/boot") {
421 Display_Name = "Boot";
422 Backup_Display_Name = Display_Name;
423 Can_Be_Backed_Up = true;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600424 Can_Flash_Img = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500425 } else if (Mount_Point == "/recovery") {
426 Display_Name = "Recovery";
427 Backup_Display_Name = Display_Name;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600428 Can_Flash_Img = true;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500429 } else if (Mount_Point == "/system_image") {
430 Display_Name = "System Image";
431 Backup_Display_Name = Display_Name;
HashBanged974bb2016-01-30 14:20:09 -0500432 Can_Flash_Img = true;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500433 Can_Be_Backed_Up = true;
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500434 } else if (Mount_Point == "/vendor_image") {
435 Display_Name = "Vendor Image";
436 Backup_Display_Name = Display_Name;
HashBanged974bb2016-01-30 14:20:09 -0500437 Can_Flash_Img = true;
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500438 Can_Be_Backed_Up = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500439 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400440 }
441
Matt Mower2416a502016-04-12 19:54:46 -0500442 // Process TWRP fstab flags
443 if (strlen(twflags) > 0) {
444 string Prev_Display_Name = Display_Name;
445 string Prev_Storage_Name = Storage_Name;
446 string Prev_Backup_Display_Name = Backup_Display_Name;
447 Display_Name = "";
448 Storage_Name = "";
449 Backup_Display_Name = "";
450
451 Process_TW_Flags(twflags, Display_Error);
452
453 bool has_display_name = !Display_Name.empty();
454 bool has_storage_name = !Storage_Name.empty();
455 bool has_backup_name = !Backup_Display_Name.empty();
456 if (!has_display_name) Display_Name = Prev_Display_Name;
457 if (!has_storage_name) Storage_Name = Prev_Storage_Name;
458 if (!has_backup_name) Backup_Display_Name = Prev_Backup_Display_Name;
459
460 if (has_display_name && !has_storage_name)
461 Storage_Name = Display_Name;
462 if (!has_display_name && has_storage_name)
463 Display_Name = Storage_Name;
464 if (has_display_name && !has_backup_name && Backup_Display_Name != "Android Secure")
465 Backup_Display_Name = Display_Name;
466 if (!has_display_name && has_backup_name)
467 Display_Name = Backup_Display_Name;
468 }
Dees_Troy51127312012-09-08 13:08:49 -0400469 return true;
470}
471
Matt Mower72c87ce2016-04-26 14:34:56 -0500472void TWPartition::Partition_Post_Processing(bool Display_Error) {
473 if (Mount_Point == "/data")
474 Setup_Data_Partition(Display_Error);
475 else if (Mount_Point == "/cache")
476 Setup_Cache_Partition(Display_Error);
477}
478
Ethan Yonkerbd7492d2016-12-07 13:55:01 -0600479void TWPartition::ExcludeAll(const string& path) {
480 backup_exclusions.add_absolute_dir(path);
481 wipe_exclusions.add_absolute_dir(path);
482}
483
Matt Mower72c87ce2016-04-26 14:34:56 -0500484void TWPartition::Setup_Data_Partition(bool Display_Error) {
485 if (Mount_Point != "/data")
486 return;
487
488 // Ensure /data is not mounted as tmpfs for qcom hardware decrypt
489 UnMount(false);
490
491#ifdef TW_INCLUDE_CRYPTO
492 if (datamedia)
493 Setup_Data_Media();
494 Can_Be_Encrypted = true;
495 char crypto_blkdev[255];
496 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
497 if (strcmp(crypto_blkdev, "error") != 0) {
498 DataManager::SetValue(TW_IS_DECRYPTED, 1);
499 Is_Encrypted = true;
500 Is_Decrypted = true;
Ethan Yonkerbd7492d2016-12-07 13:55:01 -0600501 Is_FBE = false;
502 DataManager::SetValue(TW_IS_FBE, 0);
Matt Mower72c87ce2016-04-26 14:34:56 -0500503 Decrypted_Block_Device = crypto_blkdev;
504 LOGINFO("Data already decrypted, new block device: '%s'\n", crypto_blkdev);
505 } else if (!Mount(false)) {
506 if (Is_Present) {
507 set_partition_data(Actual_Block_Device.c_str(), Crypto_Key_Location.c_str(), Fstab_File_System.c_str());
508 if (cryptfs_check_footer() == 0) {
509 Is_Encrypted = true;
510 Is_Decrypted = false;
511 Can_Be_Mounted = false;
512 Current_File_System = "emmc";
Ethan Yonker1b190162016-12-05 15:25:19 -0600513 Setup_Image();
Matt Mower72c87ce2016-04-26 14:34:56 -0500514 DataManager::SetValue(TW_IS_ENCRYPTED, 1);
515 DataManager::SetValue(TW_CRYPTO_PWTYPE, cryptfs_get_password_type());
516 DataManager::SetValue(TW_CRYPTO_PASSWORD, "");
517 DataManager::SetValue("tw_crypto_display", "");
518 } else {
519 gui_err("mount_data_footer=Could not mount /data and unable to find crypto footer.");
520 }
521 } else {
522 LOGERR("Primary block device '%s' for mount point '%s' is not present!\n", Primary_Block_Device.c_str(), Mount_Point.c_str());
523 }
524 } else {
Ethan Yonkerbd7492d2016-12-07 13:55:01 -0600525 if (TWFunc::Path_Exists("/data/unencrypted/key/version")) {
526 LOGINFO("File Based Encryption is present\n");
527#ifdef TW_INCLUDE_FBE
528 ExcludeAll(Mount_Point + "/convert_fbe");
529 ExcludeAll(Mount_Point + "/unencrypted");
530 //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
531 ExcludeAll(Mount_Point + "/misc/vold/user_keys");
Ethan Yonker79f88bd2016-12-09 14:52:12 -0600532 //ExcludeAll(Mount_Point + "/system_ce");
533 //ExcludeAll(Mount_Point + "/system_de");
534 //ExcludeAll(Mount_Point + "/misc_ce");
535 //ExcludeAll(Mount_Point + "/misc_de");
Ethan Yonkerbd7492d2016-12-07 13:55:01 -0600536 ExcludeAll(Mount_Point + "/system/gatekeeper.password.key");
537 ExcludeAll(Mount_Point + "/system/gatekeeper.pattern.key");
538 ExcludeAll(Mount_Point + "/system/locksettings.db");
539 //ExcludeAll(Mount_Point + "/system/locksettings.db-shm"); // don't seem to need this one, but the other 2 are needed
540 ExcludeAll(Mount_Point + "/system/locksettings.db-wal");
Ethan Yonker79f88bd2016-12-09 14:52:12 -0600541 //ExcludeAll(Mount_Point + "/user_de");
Ethan Yonkerbd7492d2016-12-07 13:55:01 -0600542 //ExcludeAll(Mount_Point + "/misc/profiles/cur/0"); // might be important later
543 ExcludeAll(Mount_Point + "/misc/gatekeeper");
544 ExcludeAll(Mount_Point + "/drm/kek.dat");
545 int retry_count = 3;
546 while (!Decrypt_DE() && --retry_count)
547 usleep(2000);
548 if (retry_count > 0) {
549 property_set("ro.crypto.state", "encrypted");
550 Is_Encrypted = true;
551 Is_Decrypted = false;
552 Is_FBE = true;
553 DataManager::SetValue(TW_IS_FBE, 1);
554 DataManager::SetValue(TW_IS_ENCRYPTED, 1);
555 string filename;
556 DataManager::SetValue(TW_CRYPTO_PWTYPE, Get_Password_Type(0, filename));
557 DataManager::SetValue(TW_CRYPTO_PASSWORD, "");
558 DataManager::SetValue("tw_crypto_display", "");
559 }
560#else
561 LOGERR("FBE found but FBE support not present in TWRP\n");
562#endif
563 } else {
564 // Filesystem is not encrypted and the mount succeeded, so return to
565 // the original unmounted state
566 UnMount(false);
567 }
Matt Mower72c87ce2016-04-26 14:34:56 -0500568 }
569 if (datamedia && (!Is_Encrypted || (Is_Encrypted && Is_Decrypted))) {
570 Setup_Data_Media();
571 Recreate_Media_Folder();
572 }
573#else
574 if (datamedia) {
575 Setup_Data_Media();
576 Recreate_Media_Folder();
577 }
578#endif
579}
580
581void TWPartition::Setup_Cache_Partition(bool Display_Error __unused) {
582 if (Mount_Point != "/cache")
583 return;
584
585 if (!Mount(true))
586 return;
587
588 if (!TWFunc::Path_Exists("/cache/recovery/.")) {
589 LOGINFO("Recreating /cache/recovery folder\n");
590 if (mkdir("/cache/recovery", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0)
591 LOGERR("Could not create /cache/recovery\n");
592 }
593}
594
Matt Mower4ab42b12016-04-21 13:52:18 -0500595void TWPartition::Process_FS_Flags(const char *str) {
596 char *options = strdup(str);
597 char *ptr, *savep;
Hashcode62bd9e02013-11-19 21:59:42 -0800598
Matt Mower4ab42b12016-04-21 13:52:18 -0500599 Mount_Options = "";
Hashcode62bd9e02013-11-19 21:59:42 -0800600
Matt Mower4ab42b12016-04-21 13:52:18 -0500601 // Avoid issues with potentially nested strtok by using strtok_r
602 ptr = strtok_r(options, ",", &savep);
603 while (ptr) {
604 const struct flag_list* mount_flag = mount_flags;
605
606 for (; mount_flag->name; mount_flag++) {
607 // mount_flags are never postfixed by '=',
608 // so only match identical strings (including length)
609 if (strcmp(ptr, mount_flag->name) == 0) {
610 Mount_Flags |= mount_flag->flag;
Hashcode62bd9e02013-11-19 21:59:42 -0800611 break;
612 }
613 }
614
Matt Mower4ab42b12016-04-21 13:52:18 -0500615 if (mount_flag->flag == MS_RDONLY)
616 Mount_Read_Only = true;
Hashcode62bd9e02013-11-19 21:59:42 -0800617
Matt Mower4ab42b12016-04-21 13:52:18 -0500618 if (mount_flag->name != 0) {
619 if (!Mount_Options.empty())
620 Mount_Options += ",";
621 Mount_Options += mount_flag->name;
622 } else {
623 LOGINFO("Unhandled mount flag: '%s'\n", ptr);
624 }
625
626 ptr = strtok_r(NULL, ",", &savep);
627 }
628 free(options);
Hashcode62bd9e02013-11-19 21:59:42 -0800629}
630
Matt Mower2416a502016-04-12 19:54:46 -0500631void TWPartition::Apply_TW_Flag(const unsigned flag, const char* str, const bool val) {
632 switch (flag) {
633 case TWFLAG_ANDSEC:
634 Has_Android_Secure = val;
635 break;
636 case TWFLAG_BACKUP:
637 Can_Be_Backed_Up = val;
638 break;
639 case TWFLAG_BACKUPNAME:
640 Backup_Display_Name = str;
641 break;
642 case TWFLAG_BLOCKSIZE:
643 Format_Block_Size = (unsigned long)(atol(str));
644 break;
645 case TWFLAG_CANBEWIPED:
646 Can_Be_Wiped = val;
647 break;
648 case TWFLAG_CANENCRYPTBACKUP:
649 Can_Encrypt_Backup = val;
650 break;
651 case TWFLAG_DEFAULTS:
652 // Do nothing
653 break;
654 case TWFLAG_DISPLAY:
655 Display_Name = str;
656 break;
657 case TWFLAG_ENCRYPTABLE:
658 case TWFLAG_FORCEENCRYPT:
659 Crypto_Key_Location = str;
660 break;
661 case TWFLAG_FLASHIMG:
662 Can_Flash_Img = val;
663 break;
664 case TWFLAG_FSFLAGS:
Matt Mower4ab42b12016-04-21 13:52:18 -0500665 Process_FS_Flags(str);
Matt Mower2416a502016-04-12 19:54:46 -0500666 break;
667 case TWFLAG_IGNOREBLKID:
668 Ignore_Blkid = val;
669 break;
670 case TWFLAG_LENGTH:
671 Length = atoi(str);
672 break;
673 case TWFLAG_MOUNTTODECRYPT:
674 Mount_To_Decrypt = val;
675 break;
676 case TWFLAG_REMOVABLE:
677 Removable = val;
678 break;
679 case TWFLAG_RETAINLAYOUTVERSION:
680 Retain_Layout_Version = val;
681 break;
682 case TWFLAG_SETTINGSSTORAGE:
683 Is_Settings_Storage = val;
684 if (Is_Settings_Storage)
685 Is_Storage = true;
686 break;
687 case TWFLAG_STORAGE:
688 Is_Storage = val;
689 break;
690 case TWFLAG_STORAGENAME:
691 Storage_Name = str;
692 break;
693 case TWFLAG_SUBPARTITIONOF:
694 Is_SubPartition = true;
695 SubPartition_Of = str;
696 break;
697 case TWFLAG_SYMLINK:
698 Symlink_Path = str;
699 break;
700 case TWFLAG_USERDATAENCRYPTBACKUP:
701 Use_Userdata_Encryption = val;
702 if (Use_Userdata_Encryption)
703 Can_Encrypt_Backup = true;
704 break;
705 case TWFLAG_USERMRF:
706 Use_Rm_Rf = val;
707 break;
708 case TWFLAG_WIPEDURINGFACTORYRESET:
709 Wipe_During_Factory_Reset = val;
710 if (Wipe_During_Factory_Reset) {
711 Can_Be_Wiped = true;
712 Wipe_Available_in_GUI = true;
713 }
714 break;
715 case TWFLAG_WIPEINGUI:
716 Wipe_Available_in_GUI = val;
717 if (Wipe_Available_in_GUI)
718 Can_Be_Wiped = true;
719 break;
Ethan Yonker1b190162016-12-05 15:25:19 -0600720 case TWFLAG_SLOTSELECT:
721 SlotSelect = true;
722 break;
Matt Mower2416a502016-04-12 19:54:46 -0500723 default:
724 // Should not get here
725 LOGINFO("Flag identified for processing, but later unmatched: %i\n", flag);
726 break;
727 }
728}
Dees_Troy51127312012-09-08 13:08:49 -0400729
Matt Mower2416a502016-04-12 19:54:46 -0500730void TWPartition::Process_TW_Flags(char *flags, bool Display_Error) {
731 char separator[2] = {'\n', 0};
732 char *ptr, *savep;
733
734 // Semicolons within double-quotes are not forbidden, so replace
735 // only the semicolons intended as separators with '\n' for strtok
736 for (unsigned i = 0, skip = 0; i < strlen(flags); i++) {
737 if (flags[i] == '\"')
Dees_Troya13d74f2013-03-24 08:54:55 -0500738 skip = !skip;
Matt Mower2416a502016-04-12 19:54:46 -0500739 if (!skip && flags[i] == ';')
740 flags[i] = separator[0];
Dees_Troy51127312012-09-08 13:08:49 -0400741 }
742
Matt Mower2416a502016-04-12 19:54:46 -0500743 // Avoid issues with potentially nested strtok by using strtok_r
744 ptr = strtok_r(flags, separator, &savep);
745 while (ptr) {
746 int ptr_len = strlen(ptr);
747 const struct flag_list* tw_flag = tw_flags;
Hashcode62bd9e02013-11-19 21:59:42 -0800748
Matt Mower2416a502016-04-12 19:54:46 -0500749 for (; tw_flag->name; tw_flag++) {
750 int flag_len = strlen(tw_flag->name);
751
752 if (strncmp(ptr, tw_flag->name, flag_len) == 0) {
753 bool flag_val = false;
754
755 if (ptr_len > flag_len && (tw_flag->name)[flag_len-1] != '='
756 && ptr[flag_len] != '=') {
757 // Handle flags with same starting string
758 // (e.g. backup and backupname)
759 continue;
760 } else if (ptr_len > flag_len && ptr[flag_len] == '=') {
761 // Handle flags with dual format: Part 1
762 // (e.g. backup and backup=y. backup=y handled here)
763 ptr += flag_len + 1;
764 TWFunc::Strip_Quotes(ptr);
765 // Skip flags with empty argument
766 // (e.g. backup=)
767 if (strlen(ptr) == 0) {
768 LOGINFO("Flag missing argument or should not include '=': %s=\n", tw_flag->name);
769 break;
770 }
771 flag_val = strchr("yY1", *ptr) != NULL;
772 } else if (ptr_len == flag_len
773 && (tw_flag->name)[flag_len-1] == '=') {
774 // Skip flags missing argument after =
775 // (e.g. backupname=)
776 LOGINFO("Flag missing argument: %s\n", tw_flag->name);
777 break;
778 } else if (ptr_len > flag_len
779 && (tw_flag->name)[flag_len-1] == '=') {
780 // Handle arguments to flags
781 // (e.g. backupname="My Stuff")
782 ptr += flag_len;
783 TWFunc::Strip_Quotes(ptr);
784 // Skip flags with empty argument
785 // (e.g. backupname="")
786 if (strlen(ptr) == 0) {
787 LOGINFO("Flag missing argument: %s\n", tw_flag->name);
788 break;
789 }
790 } else if (ptr_len == flag_len) {
791 // Handle flags with dual format: Part 2
792 // (e.g. backup and backup=y. backup handled here)
793 flag_val = true;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600794 } else {
Matt Mower2416a502016-04-12 19:54:46 -0500795 LOGINFO("Flag matched, but could not be processed: %s\n", ptr);
796 break;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600797 }
Matt Mower2416a502016-04-12 19:54:46 -0500798
799 Apply_TW_Flag(tw_flag->flag, ptr, flag_val);
800 break;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600801 }
Matt Mower2416a502016-04-12 19:54:46 -0500802 }
803 if (tw_flag->name == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400804 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000805 LOGERR("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400806 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000807 LOGINFO("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400808 }
Matt Mower2416a502016-04-12 19:54:46 -0500809 ptr = strtok_r(NULL, separator, &savep);
Dees_Troy51127312012-09-08 13:08:49 -0400810 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400811}
812
Dees_Troy5bf43922012-09-07 16:07:55 -0400813bool TWPartition::Is_File_System(string File_System) {
814 if (File_System == "ext2" ||
Dees_Troy63c8df72012-09-10 14:02:05 -0400815 File_System == "ext3" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400816 File_System == "ext4" ||
817 File_System == "vfat" ||
818 File_System == "ntfs" ||
819 File_System == "yaffs2" ||
bigbiff bigbiff3e146522012-11-14 14:32:59 -0500820 File_System == "exfat" ||
Dees_Troye5017042013-08-29 16:38:55 +0000821 File_System == "f2fs" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400822 File_System == "auto")
823 return true;
824 else
825 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400826}
827
Dees_Troy5bf43922012-09-07 16:07:55 -0400828bool TWPartition::Is_Image(string File_System) {
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400829 if (File_System == "emmc" || File_System == "mtd" || File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -0400830 return true;
831 else
832 return false;
833}
834
Dees_Troy51127312012-09-08 13:08:49 -0400835bool TWPartition::Make_Dir(string Path, bool Display_Error) {
Ethan Yonker93ac7a02016-11-07 22:05:58 -0600836 if (TWFunc::Get_D_Type_From_Stat(Path) != S_IFDIR)
837 unlink(Path.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400838 if (!TWFunc::Path_Exists(Path)) {
Dees_Troy51127312012-09-08 13:08:49 -0400839 if (mkdir(Path.c_str(), 0777) == -1) {
840 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -0500841 gui_msg(Msg(msg::kError, "create_folder_strerr=Can not create '{1}' folder ({2}).")(Path)(strerror(errno)));
Dees_Troy51127312012-09-08 13:08:49 -0400842 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000843 LOGINFO("Can not create '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400844 return false;
845 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000846 LOGINFO("Created '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400847 return true;
848 }
849 }
850 return true;
851}
852
Dees_Troy5bf43922012-09-07 16:07:55 -0400853void TWPartition::Setup_File_System(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400854 Can_Be_Mounted = true;
855 Can_Be_Wiped = true;
856
Dees_Troy5bf43922012-09-07 16:07:55 -0400857 // Make the mount point folder if it doesn't exist
Dees_Troy51127312012-09-08 13:08:49 -0400858 Make_Dir(Mount_Point, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400859 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
860 Backup_Name = Display_Name;
bigbiffce8f83c2015-12-12 18:30:21 -0500861 Backup_Method = BM_FILES;
Dees_Troy5bf43922012-09-07 16:07:55 -0400862}
863
Ethan Yonker1b190162016-12-05 15:25:19 -0600864void TWPartition::Setup_Image() {
Dees_Troy5bf43922012-09-07 16:07:55 -0400865 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
866 Backup_Name = Display_Name;
Gary Peck82599a82012-11-21 16:23:12 -0800867 if (Current_File_System == "emmc")
bigbiffce8f83c2015-12-12 18:30:21 -0500868 Backup_Method = BM_DD;
Gary Peck82599a82012-11-21 16:23:12 -0800869 else if (Current_File_System == "mtd" || Current_File_System == "bml")
bigbiffce8f83c2015-12-12 18:30:21 -0500870 Backup_Method = BM_FLASH_UTILS;
Dees_Troy5bf43922012-09-07 16:07:55 -0400871 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000872 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 -0400873}
874
Dees_Troye58d5262012-09-21 12:27:57 -0400875void TWPartition::Setup_AndSec(void) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500876 Backup_Display_Name = "Android Secure";
Dees_Troye58d5262012-09-21 12:27:57 -0400877 Backup_Name = "and-sec";
Dees_Troya13d74f2013-03-24 08:54:55 -0500878 Can_Be_Backed_Up = true;
Dees_Troye58d5262012-09-21 12:27:57 -0400879 Has_Android_Secure = true;
880 Symlink_Path = Mount_Point + "/.android_secure";
881 Symlink_Mount_Point = "/and-sec";
882 Backup_Path = Symlink_Mount_Point;
883 Make_Dir("/and-sec", true);
884 Recreate_AndSec_Folder();
Kjell Braden3126a112016-06-19 16:58:15 +0000885 Mount_Storage_Retry(true);
Dees_Troye58d5262012-09-21 12:27:57 -0400886}
887
that9e0593e2014-10-08 00:01:24 +0200888void TWPartition::Setup_Data_Media() {
Ethan Yonker6277c792014-09-15 14:54:30 -0500889 LOGINFO("Setting up '%s' as data/media emulated storage.\n", Mount_Point.c_str());
Ethan Yonker66a19492015-12-10 10:19:45 -0600890 if (Storage_Name.empty() || Storage_Name == "Data")
891 Storage_Name = "Internal Storage";
Ethan Yonker6277c792014-09-15 14:54:30 -0500892 Has_Data_Media = true;
893 Is_Storage = true;
Ethan Yonker66a19492015-12-10 10:19:45 -0600894 Storage_Path = Mount_Point + "/media";
Ethan Yonker6277c792014-09-15 14:54:30 -0500895 Symlink_Path = Storage_Path;
Ethan Yonker66a19492015-12-10 10:19:45 -0600896 if (Mount_Point == "/data") {
897 Is_Settings_Storage = true;
898 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
899 Make_Dir("/emmc", false);
900 Symlink_Mount_Point = "/emmc";
901 } else {
902 Make_Dir("/sdcard", false);
903 Symlink_Mount_Point = "/sdcard";
904 }
905 if (Mount(false) && TWFunc::Path_Exists(Mount_Point + "/media/0")) {
906 Storage_Path = Mount_Point + "/media/0";
907 Symlink_Path = Storage_Path;
908 DataManager::SetValue(TW_INTERNAL_PATH, Mount_Point + "/media/0");
909 UnMount(true);
910 }
911 DataManager::SetValue("tw_has_internal", 1);
912 DataManager::SetValue("tw_has_data_media", 1);
Ethan Yonker3fdcda42016-11-30 12:29:37 -0600913 backup_exclusions.add_absolute_dir("/data/data/com.google.android.music/files");
Ethan Yonker79f88bd2016-12-09 14:52:12 -0600914 ExcludeAll(Mount_Point + "/misc/vold");
915 ExcludeAll(Mount_Point + "/.layout_version");
916 ExcludeAll(Mount_Point + "/system/storage.xml");
Ethan Yonker6277c792014-09-15 14:54:30 -0500917 } else {
Ethan Yonker66a19492015-12-10 10:19:45 -0600918 if (Mount(true) && TWFunc::Path_Exists(Mount_Point + "/media/0")) {
919 Storage_Path = Mount_Point + "/media/0";
920 Symlink_Path = Storage_Path;
921 UnMount(true);
922 }
Ethan Yonker6277c792014-09-15 14:54:30 -0500923 }
Ethan Yonker79f88bd2016-12-09 14:52:12 -0600924 ExcludeAll(Mount_Point + "/media");
Ethan Yonker6277c792014-09-15 14:54:30 -0500925}
926
Dees_Troy5bf43922012-09-07 16:07:55 -0400927void TWPartition::Find_Real_Block_Device(string& Block, bool Display_Error) {
928 char device[512], realDevice[512];
929
930 strcpy(device, Block.c_str());
931 memset(realDevice, 0, sizeof(realDevice));
932 while (readlink(device, realDevice, sizeof(realDevice)) > 0)
933 {
934 strcpy(device, realDevice);
935 memset(realDevice, 0, sizeof(realDevice));
936 }
937
938 if (device[0] != '/') {
939 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000940 LOGERR("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400941 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000942 LOGINFO("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400943 return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400944 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400945 Block = device;
946 return;
947 }
948}
949
Kjell Braden3126a112016-06-19 16:58:15 +0000950bool TWPartition::Mount_Storage_Retry(bool Display_Error) {
Dees_Troy8e337f32012-10-13 22:07:49 -0400951 // On some devices, storage doesn't want to mount right away, retry and sleep
Kjell Braden3126a112016-06-19 16:58:15 +0000952 if (!Mount(Display_Error)) {
Dees_Troy8e337f32012-10-13 22:07:49 -0400953 int retry_count = 5;
954 while (retry_count > 0 && !Mount(false)) {
955 usleep(500000);
956 retry_count--;
957 }
Kjell Braden3126a112016-06-19 16:58:15 +0000958 return Mount(Display_Error);
Dees_Troy8e337f32012-10-13 22:07:49 -0400959 }
Kjell Braden3126a112016-06-19 16:58:15 +0000960 return true;
Dees_Troy8e337f32012-10-13 22:07:49 -0400961}
962
Dees_Troy38bd7602012-09-14 13:33:53 -0400963bool TWPartition::Find_MTD_Block_Device(string MTD_Name) {
964 FILE *fp = NULL;
965 char line[255];
966
967 fp = fopen("/proc/mtd", "rt");
968 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000969 LOGERR("Device does not support /proc/mtd\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400970 return false;
971 }
972
973 while (fgets(line, sizeof(line), fp) != NULL)
974 {
975 char device[32], label[32];
976 unsigned long size = 0;
Dees_Troy38bd7602012-09-14 13:33:53 -0400977 int deviceId;
978
979 sscanf(line, "%s %lx %*s %*c%s", device, &size, label);
980
981 // Skip header and blank lines
982 if ((strcmp(device, "dev:") == 0) || (strlen(line) < 8))
983 continue;
984
985 // Strip off the trailing " from the label
986 label[strlen(label)-1] = '\0';
987
988 if (strcmp(label, MTD_Name.c_str()) == 0) {
989 // We found our device
990 // Strip off the trailing : from the device
991 device[strlen(device)-1] = '\0';
992 if (sscanf(device,"mtd%d", &deviceId) == 1) {
993 sprintf(device, "/dev/block/mtdblock%d", deviceId);
994 Primary_Block_Device = device;
Dees_Troy76543db2013-06-19 16:24:30 +0000995 fclose(fp);
996 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -0400997 }
998 }
999 }
1000 fclose(fp);
1001
1002 return false;
1003}
1004
Dees_Troy51127312012-09-08 13:08:49 -04001005bool TWPartition::Get_Size_Via_statfs(bool Display_Error) {
1006 struct statfs st;
1007 string Local_Path = Mount_Point + "/.";
1008
1009 if (!Mount(Display_Error))
1010 return false;
1011
1012 if (statfs(Local_Path.c_str(), &st) != 0) {
1013 if (!Removable) {
1014 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001015 LOGERR("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -04001016 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001017 LOGINFO("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -04001018 }
1019 return false;
1020 }
1021 Size = (st.f_blocks * st.f_bsize);
1022 Used = ((st.f_blocks - st.f_bfree) * st.f_bsize);
1023 Free = (st.f_bfree * st.f_bsize);
1024 Backup_Size = Used;
1025 return true;
1026}
1027
1028bool TWPartition::Get_Size_Via_df(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001029 FILE* fp;
1030 char command[255], line[512];
1031 int include_block = 1;
1032 unsigned int min_len;
1033
1034 if (!Mount(Display_Error))
1035 return false;
1036
Dees_Troy38bd7602012-09-14 13:33:53 -04001037 min_len = Actual_Block_Device.size() + 2;
Dees_Troy51127312012-09-08 13:08:49 -04001038 sprintf(command, "df %s > /tmp/dfoutput.txt", Mount_Point.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001039 TWFunc::Exec_Cmd(command);
Dees_Troy51127312012-09-08 13:08:49 -04001040 fp = fopen("/tmp/dfoutput.txt", "rt");
1041 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001042 LOGINFO("Unable to open /tmp/dfoutput.txt.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001043 return false;
Dees_Troy51127312012-09-08 13:08:49 -04001044 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001045
1046 while (fgets(line, sizeof(line), fp) != NULL)
1047 {
1048 unsigned long blocks, used, available;
1049 char device[64];
1050 char tmpString[64];
1051
1052 if (strncmp(line, "Filesystem", 10) == 0)
1053 continue;
1054 if (strlen(line) < min_len) {
1055 include_block = 0;
1056 continue;
1057 }
1058 if (include_block) {
1059 sscanf(line, "%s %lu %lu %lu", device, &blocks, &used, &available);
1060 } else {
1061 // The device block string is so long that the df information is on the next line
1062 int space_count = 0;
Dees_Troye58d5262012-09-21 12:27:57 -04001063 sprintf(tmpString, "/dev/block/%s", Actual_Block_Device.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001064 while (tmpString[space_count] == 32)
1065 space_count++;
1066 sscanf(line + space_count, "%lu %lu %lu", &blocks, &used, &available);
1067 }
1068
1069 // Adjust block size to byte size
1070 Size = blocks * 1024ULL;
1071 Used = used * 1024ULL;
1072 Free = available * 1024ULL;
1073 Backup_Size = Used;
1074 }
1075 fclose(fp);
1076 return true;
1077}
1078
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001079unsigned long long TWPartition::IOCTL_Get_Block_Size() {
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001080 Find_Actual_Block_Device();
Ethan Yonker483e9f42016-01-11 22:21:18 -06001081
1082 return TWFunc::IOCTL_Get_Block_Size(Actual_Block_Device.c_str());
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001083}
1084
Dees_Troy5bf43922012-09-07 16:07:55 -04001085bool TWPartition::Find_Partition_Size(void) {
1086 FILE* fp;
1087 char line[512];
1088 string tmpdevice;
1089
igoriok87e3d932013-01-31 21:03:53 +02001090 fp = fopen("/proc/dumchar_info", "rt");
1091 if (fp != NULL) {
1092 while (fgets(line, sizeof(line), fp) != NULL)
1093 {
1094 char label[32], device[32];
1095 unsigned long size = 0;
1096
thatd43bf2d2014-09-21 23:13:02 +02001097 sscanf(line, "%s %lx %*x %*u %s", label, &size, device);
igoriok87e3d932013-01-31 21:03:53 +02001098
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05001099 // Skip header, annotation and blank lines
igoriok87e3d932013-01-31 21:03:53 +02001100 if ((strncmp(device, "/dev/", 5) != 0) || (strlen(line) < 8))
1101 continue;
1102
1103 tmpdevice = "/dev/";
1104 tmpdevice += label;
1105 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
1106 Size = size;
1107 fclose(fp);
1108 return true;
1109 }
1110 }
1111 }
1112
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001113 unsigned long long ioctl_size = IOCTL_Get_Block_Size();
1114 if (ioctl_size) {
1115 Size = ioctl_size;
1116 return true;
1117 }
1118
Dees_Troy5bf43922012-09-07 16:07:55 -04001119 // In this case, we'll first get the partitions we care about (with labels)
1120 fp = fopen("/proc/partitions", "rt");
1121 if (fp == NULL)
1122 return false;
1123
1124 while (fgets(line, sizeof(line), fp) != NULL)
1125 {
1126 unsigned long major, minor, blocks;
1127 char device[512];
Dees_Troy5bf43922012-09-07 16:07:55 -04001128
Dees_Troy63c8df72012-09-10 14:02:05 -04001129 if (strlen(line) < 7 || line[0] == 'm') continue;
Dees_Troy5bf43922012-09-07 16:07:55 -04001130 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
1131
1132 tmpdevice = "/dev/block/";
1133 tmpdevice += device;
Dees_Troy38bd7602012-09-14 13:33:53 -04001134 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001135 // Adjust block size to byte size
1136 Size = blocks * 1024ULL;
1137 fclose(fp);
1138 return true;
1139 }
1140 }
1141 fclose(fp);
1142 return false;
1143}
1144
Dees_Troy5bf43922012-09-07 16:07:55 -04001145bool TWPartition::Is_Mounted(void) {
1146 if (!Can_Be_Mounted)
1147 return false;
1148
1149 struct stat st1, st2;
1150 string test_path;
1151
1152 // Check to see if the mount point directory exists
1153 test_path = Mount_Point + "/.";
1154 if (stat(test_path.c_str(), &st1) != 0) return false;
1155
1156 // Check to see if the directory above the mount point exists
1157 test_path = Mount_Point + "/../.";
1158 if (stat(test_path.c_str(), &st2) != 0) return false;
1159
1160 // Compare the device IDs -- if they match then we're (probably) using tmpfs instead of an actual device
1161 int ret = (st1.st_dev != st2.st_dev) ? true : false;
1162
1163 return ret;
1164}
1165
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05001166bool TWPartition::Is_File_System_Writable(void) {
1167 if (!Is_File_System(Current_File_System) || !Is_Mounted())
1168 return false;
1169
1170 string test_path = Mount_Point + "/.";
1171 return (access(test_path.c_str(), W_OK) == 0);
1172}
1173
Dees_Troy5bf43922012-09-07 16:07:55 -04001174bool TWPartition::Mount(bool Display_Error) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001175 int exfat_mounted = 0;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001176 unsigned long flags = Mount_Flags;
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001177
Dees_Troy5bf43922012-09-07 16:07:55 -04001178 if (Is_Mounted()) {
1179 return true;
1180 } else if (!Can_Be_Mounted) {
1181 return false;
1182 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001183
1184 Find_Actual_Block_Device();
1185
1186 // Check the current file system before mounting
1187 Check_FS_Type();
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001188 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/exfat-fuse")) {
Dees_Troye34c1332013-02-06 19:13:00 +00001189 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 +00001190 LOGINFO("cmd: %s\n", cmd.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001191 string result;
1192 if (TWFunc::Exec_Cmd(cmd, result) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001193 LOGINFO("exfat-fuse failed to mount with result '%s', trying vfat\n", result.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001194 Current_File_System = "vfat";
1195 } else {
1196#ifdef TW_NO_EXFAT_FUSE
1197 UnMount(false);
1198 // We'll let the kernel handle it but using exfat-fuse to detect if the file system is actually exfat
1199 // Some kernels let us mount vfat as exfat which doesn't work out too well
1200#else
1201 exfat_mounted = 1;
1202#endif
1203 }
1204 }
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001205
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001206 if (Current_File_System == "ntfs" && (TWFunc::Path_Exists("/sbin/ntfs-3g") || TWFunc::Path_Exists("/sbin/mount.ntfs"))) {
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001207 string cmd;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001208 string Ntfsmount_Binary = "";
1209
1210 if (TWFunc::Path_Exists("/sbin/ntfs-3g"))
1211 Ntfsmount_Binary = "ntfs-3g";
1212 else if (TWFunc::Path_Exists("/sbin/mount.ntfs"))
1213 Ntfsmount_Binary = "mount.ntfs";
1214
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001215 if (Mount_Read_Only)
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001216 cmd = "/sbin/" + Ntfsmount_Binary + " -o ro " + Actual_Block_Device + " " + Mount_Point;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001217 else
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001218 cmd = "/sbin/" + Ntfsmount_Binary + " " + Actual_Block_Device + " " + Mount_Point;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001219 LOGINFO("cmd: '%s'\n", cmd.c_str());
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001220
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001221 if (TWFunc::Exec_Cmd(cmd) == 0) {
1222 return true;
1223 } else {
1224 LOGINFO("ntfs-3g failed to mount, trying regular mount method.\n");
1225 }
1226 }
1227
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001228 if (Mount_Read_Only)
1229 flags |= MS_RDONLY;
1230
Dees_Troy22042032012-12-18 21:23:08 +00001231 if (Fstab_File_System == "yaffs2") {
1232 // mount an MTD partition as a YAFFS2 filesystem.
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001233 flags = MS_NOATIME | MS_NODEV | MS_NODIRATIME;
1234 if (Mount_Read_Only)
1235 flags |= MS_RDONLY;
Dees_Troy76543db2013-06-19 16:24:30 +00001236 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags, NULL) < 0) {
1237 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags | MS_RDONLY, NULL) < 0) {
1238 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001239 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy76543db2013-06-19 16:24:30 +00001240 else
1241 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
1242 return false;
1243 } else {
1244 LOGINFO("Mounted '%s' (MTD) as RO\n", Mount_Point.c_str());
1245 return true;
1246 }
1247 } else {
1248 struct stat st;
1249 string test_path = Mount_Point;
1250 if (stat(test_path.c_str(), &st) < 0) {
1251 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001252 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy76543db2013-06-19 16:24:30 +00001253 else
1254 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
1255 return false;
1256 }
1257 mode_t new_mode = st.st_mode | S_IXUSR | S_IXGRP | S_IXOTH;
1258 if (new_mode != st.st_mode) {
1259 LOGINFO("Fixing execute permissions for %s\n", Mount_Point.c_str());
1260 if (chmod(Mount_Point.c_str(), new_mode) < 0) {
1261 if (Display_Error)
1262 LOGERR("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
1263 else
1264 LOGINFO("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
1265 return false;
1266 }
1267 }
Dees_Troy22042032012-12-18 21:23:08 +00001268 return true;
Dees_Troy76543db2013-06-19 16:24:30 +00001269 }
thatc7572eb2015-03-31 18:55:30 +02001270 }
1271
1272 string mount_fs = Current_File_System;
1273 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sys/module/texfat"))
1274 mount_fs = "texfat";
1275
1276 if (!exfat_mounted &&
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001277 mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), mount_fs.c_str(), flags, Mount_Options.c_str()) != 0 &&
1278 mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), mount_fs.c_str(), flags, NULL) != 0) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001279#ifdef TW_NO_EXFAT_FUSE
1280 if (Current_File_System == "exfat") {
Dees_Troy2673cec2013-04-02 20:22:16 +00001281 LOGINFO("Mounting exfat failed, trying vfat...\n");
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001282 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), "vfat", 0, NULL) != 0) {
Dees_Troy85f44ed2013-01-09 18:42:36 +00001283 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001284 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy85f44ed2013-01-09 18:42:36 +00001285 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001286 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001287 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 +00001288 return false;
Dees_Troy85f44ed2013-01-09 18:42:36 +00001289 }
Dees_Troyb05ddee2013-01-28 20:24:50 +00001290 } else {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001291#endif
bigbiff bigbiff26774a02014-03-29 18:22:00 -04001292 if (!Removable && Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001293 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001294 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001295 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
1296 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 +00001297 return false;
1298#ifdef TW_NO_EXFAT_FUSE
Dees_Troy85f44ed2013-01-09 18:42:36 +00001299 }
1300#endif
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001301 }
Ethan Yonker253368a2014-11-25 15:00:52 -06001302
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001303 if (Removable)
1304 Update_Size(Display_Error);
1305
xiaolu9416f4f2015-06-04 08:22:23 +08001306 if (!Symlink_Mount_Point.empty() && TWFunc::Path_Exists(Symlink_Path)) {
codelover2a3d4ce2015-03-14 20:26:49 +08001307 string Command = "mount -o bind '" + Symlink_Path + "' '" + Symlink_Mount_Point + "'";
Vojtech Bocek05534202013-09-11 08:11:56 +02001308 TWFunc::Exec_Cmd(Command);
Dees_Troy5bf43922012-09-07 16:07:55 -04001309 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001310 return true;
1311}
1312
1313bool TWPartition::UnMount(bool Display_Error) {
1314 if (Is_Mounted()) {
1315 int never_unmount_system;
1316
1317 DataManager::GetValue(TW_DONT_UNMOUNT_SYSTEM, never_unmount_system);
1318 if (never_unmount_system == 1 && Mount_Point == "/system")
1319 return true; // Never unmount system if you're not supposed to unmount it
1320
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001321 if (Is_Storage)
Ethan Yonker726a0202014-12-16 20:01:38 -06001322 PartitionManager.Remove_MTP_Storage(MTP_Storage_ID);
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001323
Dees_Troy38bd7602012-09-14 13:33:53 -04001324 if (!Symlink_Mount_Point.empty())
1325 umount(Symlink_Mount_Point.c_str());
1326
Dees_Troyb05ddee2013-01-28 20:24:50 +00001327 umount(Mount_Point.c_str());
1328 if (Is_Mounted()) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001329 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001330 gui_msg(Msg(msg::kError, "fail_unmount=Failed to unmount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy5bf43922012-09-07 16:07:55 -04001331 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001332 LOGINFO("Unable to unmount '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001333 return false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001334 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -04001335 return true;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001336 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001337 } else {
1338 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001339 }
1340}
1341
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05001342bool TWPartition::ReMount(bool Display_Error) {
1343 if (UnMount(Display_Error))
1344 return Mount(Display_Error);
1345 return false;
1346}
1347
1348bool TWPartition::ReMount_RW(bool Display_Error) {
1349 // No need to remount if already mounted rw
1350 if (Is_File_System_Writable())
1351 return true;
1352
1353 bool ro = Mount_Read_Only;
1354 int flags = Mount_Flags;
1355
1356 Mount_Read_Only = false;
1357 Mount_Flags &= ~MS_RDONLY;
1358
1359 bool ret = ReMount(Display_Error);
1360
1361 Mount_Read_Only = ro;
1362 Mount_Flags = flags;
1363
1364 return ret;
1365}
1366
Gary Peck43acadf2012-11-21 21:19:01 -08001367bool TWPartition::Wipe(string New_File_System) {
Ethan Yonker726a0202014-12-16 20:01:38 -06001368 bool wiped = false, update_crypt = false, recreate_media = true;
Dees_Troy16c2b312013-01-15 16:51:18 +00001369 int check;
1370 string Layout_Filename = Mount_Point + "/.layout_version";
1371
Dees_Troy38bd7602012-09-14 13:33:53 -04001372 if (!Can_Be_Wiped) {
Matt Mower3c366972015-12-25 19:28:31 -06001373 gui_msg(Msg(msg::kError, "cannot_wipe=Partition {1} cannot be wiped.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04001374 return false;
1375 }
1376
Dees_Troyc51f1f92012-09-20 15:32:13 -04001377 if (Mount_Point == "/cache")
Dees_Troy2673cec2013-04-02 20:22:16 +00001378 Log_Offset = 0;
Dees_Troyc51f1f92012-09-20 15:32:13 -04001379
Dees_Troy16c2b312013-01-15 16:51:18 +00001380 if (Retain_Layout_Version && Mount(false) && TWFunc::Path_Exists(Layout_Filename))
1381 TWFunc::copy_file(Layout_Filename, "/.layout_version", 0600);
1382 else
1383 unlink("/.layout_version");
Dees_Troy38bd7602012-09-14 13:33:53 -04001384
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001385 if (Has_Data_Media && Current_File_System == New_File_System) {
Dees_Troy16c2b312013-01-15 16:51:18 +00001386 wiped = Wipe_Data_Without_Wiping_Media();
Ethan Yonker5eac2222014-06-11 12:22:55 -05001387 recreate_media = false;
Dees_Troy16c2b312013-01-15 16:51:18 +00001388 } else {
Dees_Troy16c2b312013-01-15 16:51:18 +00001389 DataManager::GetValue(TW_RM_RF_VAR, check);
1390
Hashcodedabfd492013-08-29 22:45:30 -07001391 if (check || Use_Rm_Rf)
Dees_Troy16c2b312013-01-15 16:51:18 +00001392 wiped = Wipe_RMRF();
1393 else if (New_File_System == "ext4")
1394 wiped = Wipe_EXT4();
1395 else if (New_File_System == "ext2" || New_File_System == "ext3")
1396 wiped = Wipe_EXT23(New_File_System);
1397 else if (New_File_System == "vfat")
1398 wiped = Wipe_FAT();
1399 else if (New_File_System == "exfat")
1400 wiped = Wipe_EXFAT();
1401 else if (New_File_System == "yaffs2")
1402 wiped = Wipe_MTD();
Dees_Troye5017042013-08-29 16:38:55 +00001403 else if (New_File_System == "f2fs")
1404 wiped = Wipe_F2FS();
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001405 else if (New_File_System == "ntfs")
1406 wiped = Wipe_NTFS();
Dees_Troy16c2b312013-01-15 16:51:18 +00001407 else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001408 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 +00001409 unlink("/.layout_version");
1410 return false;
1411 }
1412 update_crypt = wiped;
Gary Pecke8bc5d72012-12-21 06:45:25 -08001413 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001414
Gary Pecke8bc5d72012-12-21 06:45:25 -08001415 if (wiped) {
Dees_Troy1c1ac442013-01-17 21:42:14 +00001416 if (Mount_Point == "/cache")
1417 DataManager::Output_Version();
1418
Dees_Troy16c2b312013-01-15 16:51:18 +00001419 if (TWFunc::Path_Exists("/.layout_version") && Mount(false))
1420 TWFunc::copy_file("/.layout_version", Layout_Filename, 0600);
1421
1422 if (update_crypt) {
1423 Setup_File_System(false);
1424 if (Is_Encrypted && !Is_Decrypted) {
1425 // just wiped an encrypted partition back to its unencrypted state
1426 Is_Encrypted = false;
1427 Is_Decrypted = false;
1428 Decrypted_Block_Device = "";
1429 if (Mount_Point == "/data") {
1430 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1431 DataManager::SetValue(TW_IS_DECRYPTED, 0);
1432 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001433 }
1434 }
Ethan Yonker5eac2222014-06-11 12:22:55 -05001435
Ethan Yonker66a19492015-12-10 10:19:45 -06001436 if (Has_Data_Media && recreate_media) {
Ethan Yonker5eac2222014-06-11 12:22:55 -05001437 Recreate_Media_Folder();
1438 }
Matt Mower209c9632016-01-20 12:08:35 -06001439 if (Is_Storage && Mount(false))
1440 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Gary Pecke8bc5d72012-12-21 06:45:25 -08001441 }
Matt Mower209c9632016-01-20 12:08:35 -06001442
Gary Pecke8bc5d72012-12-21 06:45:25 -08001443 return wiped;
Dees_Troy51a0e822012-09-05 15:24:24 -04001444}
1445
Gary Peck43acadf2012-11-21 21:19:01 -08001446bool TWPartition::Wipe() {
Gary Peck82599a82012-11-21 16:23:12 -08001447 if (Is_File_System(Current_File_System))
1448 return Wipe(Current_File_System);
1449 else
1450 return Wipe(Fstab_File_System);
Gary Peck43acadf2012-11-21 21:19:01 -08001451}
1452
Dees_Troye58d5262012-09-21 12:27:57 -04001453bool TWPartition::Wipe_AndSec(void) {
1454 if (!Has_Android_Secure)
1455 return false;
1456
Dees_Troye58d5262012-09-21 12:27:57 -04001457 if (!Mount(true))
1458 return false;
1459
Ethan Yonker74db1572015-10-28 12:44:49 -05001460 gui_msg(Msg("wiping=Wiping {1}")(Backup_Display_Name));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001461 TWFunc::removeDir(Mount_Point + "/.android_secure/", true);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001462 return true;
Dees_Troye58d5262012-09-21 12:27:57 -04001463}
1464
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001465bool TWPartition::Can_Repair() {
Ethan Yonkera2719152015-05-28 09:44:41 -05001466 if (Mount_Read_Only)
1467 return false;
Matt Mower18794c82015-11-11 16:22:45 -06001468 if (Current_File_System == "vfat" && TWFunc::Path_Exists("/sbin/fsck.fat"))
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001469 return true;
1470 else if ((Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") && TWFunc::Path_Exists("/sbin/e2fsck"))
1471 return true;
1472 else if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/fsck.exfat"))
1473 return true;
1474 else if (Current_File_System == "f2fs" && TWFunc::Path_Exists("/sbin/fsck.f2fs"))
1475 return true;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001476 else if (Current_File_System == "ntfs" && (TWFunc::Path_Exists("/sbin/ntfsfix") || TWFunc::Path_Exists("/sbin/fsck.ntfs")))
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001477 return true;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001478 return false;
1479}
1480
1481bool TWPartition::Repair() {
1482 string command;
1483
1484 if (Current_File_System == "vfat") {
Matt Mower18794c82015-11-11 16:22:45 -06001485 if (!TWFunc::Path_Exists("/sbin/fsck.fat")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001486 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.fat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001487 return false;
1488 }
1489 if (!UnMount(true))
1490 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001491 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.fat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001492 Find_Actual_Block_Device();
Matt Mower18794c82015-11-11 16:22:45 -06001493 command = "/sbin/fsck.fat -y " + Actual_Block_Device;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001494 LOGINFO("Repair command: %s\n", command.c_str());
1495 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001496 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001497 return true;
1498 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001499 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001500 return false;
1501 }
1502 }
1503 if (Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") {
1504 if (!TWFunc::Path_Exists("/sbin/e2fsck")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001505 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("e2fsck"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001506 return false;
1507 }
1508 if (!UnMount(true))
1509 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001510 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("e2fsck"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001511 Find_Actual_Block_Device();
Ethan Yonkera2719152015-05-28 09:44:41 -05001512 command = "/sbin/e2fsck -fp " + Actual_Block_Device;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001513 LOGINFO("Repair command: %s\n", command.c_str());
1514 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001515 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001516 return true;
1517 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001518 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001519 return false;
1520 }
1521 }
1522 if (Current_File_System == "exfat") {
1523 if (!TWFunc::Path_Exists("/sbin/fsck.exfat")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001524 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.exfat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001525 return false;
1526 }
1527 if (!UnMount(true))
1528 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001529 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.exfat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001530 Find_Actual_Block_Device();
1531 command = "/sbin/fsck.exfat " + Actual_Block_Device;
1532 LOGINFO("Repair command: %s\n", command.c_str());
1533 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001534 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001535 return true;
1536 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001537 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001538 return false;
1539 }
1540 }
1541 if (Current_File_System == "f2fs") {
1542 if (!TWFunc::Path_Exists("/sbin/fsck.f2fs")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001543 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.f2fs"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001544 return false;
1545 }
1546 if (!UnMount(true))
1547 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001548 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.f2fs"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001549 Find_Actual_Block_Device();
1550 command = "/sbin/fsck.f2fs " + Actual_Block_Device;
1551 LOGINFO("Repair command: %s\n", command.c_str());
1552 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001553 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001554 return true;
1555 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001556 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001557 return false;
1558 }
1559 }
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001560 if (Current_File_System == "ntfs") {
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001561 string Ntfsfix_Binary;
1562 if (TWFunc::Path_Exists("/sbin/ntfsfix"))
1563 Ntfsfix_Binary = "ntfsfix";
1564 else if (TWFunc::Path_Exists("/sbin/fsck.ntfs"))
1565 Ntfsfix_Binary = "fsck.ntfs";
1566 else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001567 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("ntfsfix"));
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001568 return false;
1569 }
1570 if (!UnMount(true))
1571 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001572 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)(Ntfsfix_Binary));
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001573 Find_Actual_Block_Device();
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001574 command = "/sbin/" + Ntfsfix_Binary + " " + Actual_Block_Device;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001575 LOGINFO("Repair command: %s\n", command.c_str());
1576 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001577 gui_msg("done=Done.");
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001578 return true;
1579 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001580 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001581 return false;
1582 }
1583 }
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001584 return false;
1585}
1586
Ethan Yonkera2719152015-05-28 09:44:41 -05001587bool TWPartition::Can_Resize() {
1588 if (Mount_Read_Only)
1589 return false;
1590 if ((Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") && TWFunc::Path_Exists("/sbin/resize2fs"))
1591 return true;
1592 return false;
1593}
1594
1595bool TWPartition::Resize() {
1596 string command;
1597
1598 if (Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") {
1599 if (!Can_Repair()) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001600 LOGINFO("Cannot resize %s because %s cannot be repaired before resizing.\n", Display_Name.c_str(), Display_Name.c_str());
1601 gui_msg(Msg(msg::kError, "cannot_resize=Cannot resize {1}.")(Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001602 return false;
1603 }
1604 if (!TWFunc::Path_Exists("/sbin/resize2fs")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001605 LOGINFO("resize2fs does not exist! Cannot resize!\n");
1606 gui_msg(Msg(msg::kError, "cannot_resize=Cannot resize {1}.")(Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001607 return false;
1608 }
1609 // Repair will unmount so no need to do it twice
Ethan Yonker74db1572015-10-28 12:44:49 -05001610 gui_msg(Msg("repair_resize=Repairing {1} before resizing.")( Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001611 if (!Repair())
1612 return false;
Ethan Yonker74db1572015-10-28 12:44:49 -05001613 gui_msg(Msg("resizing=Resizing {1} using {2}...")(Display_Name)("resize2fs"));
Ethan Yonkera2719152015-05-28 09:44:41 -05001614 Find_Actual_Block_Device();
1615 command = "/sbin/resize2fs " + Actual_Block_Device;
1616 if (Length != 0) {
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001617 unsigned long long Actual_Size = IOCTL_Get_Block_Size();
1618 if (Actual_Size == 0)
1619 return false;
Ethan Yonkera2719152015-05-28 09:44:41 -05001620
Ethan Yonkera2719152015-05-28 09:44:41 -05001621 unsigned long long Block_Count;
1622 if (Length < 0) {
1623 // Reduce overall size by this length
1624 Block_Count = (Actual_Size / 1024LLU) - ((unsigned long long)(Length * -1) / 1024LLU);
1625 } else {
1626 // This is the size, not a size reduction
1627 Block_Count = ((unsigned long long)(Length) / 1024LLU);
1628 }
1629 char temp[256];
1630 sprintf(temp, "%llu", Block_Count);
1631 command += " ";
1632 command += temp;
1633 command += "K";
1634 }
1635 LOGINFO("Resize command: %s\n", command.c_str());
1636 if (TWFunc::Exec_Cmd(command) == 0) {
1637 Update_Size(true);
Ethan Yonker74db1572015-10-28 12:44:49 -05001638 gui_msg("done=Done.");
Ethan Yonkera2719152015-05-28 09:44:41 -05001639 return true;
1640 } else {
1641 Update_Size(true);
Ethan Yonker74db1572015-10-28 12:44:49 -05001642 gui_msg(Msg(msg::kError, "unable_resize=Unable to resize {1}.")(Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001643 return false;
1644 }
1645 }
1646 return false;
1647}
1648
bigbiffce8f83c2015-12-12 18:30:21 -05001649bool TWPartition::Backup(PartitionSettings *part_settings, pid_t *tar_fork_pid) {
1650 if (Backup_Method == BM_FILES)
1651 return Backup_Tar(part_settings, tar_fork_pid);
1652 else if (Backup_Method == BM_DD)
1653 return Backup_Image(part_settings);
1654 else if (Backup_Method == BM_FLASH_UTILS)
1655 return Backup_Dump_Image(part_settings);
Dees_Troy2673cec2013-04-02 20:22:16 +00001656 LOGERR("Unknown backup method for '%s'\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001657 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001658}
1659
James Christopher Adduono79ae0932016-10-25 02:18:32 -04001660bool TWPartition::Check_Restore_File_MD5(const string& Filename) {
1661 twrpDigest md5sum;
1662
1663 md5sum.setfn(Filename);
1664 switch (md5sum.verify_md5digest()) {
1665 case MD5_OK:
1666 gui_msg(Msg("md5_matched=MD5 matched for '{1}'.")(Filename));
1667 return true;
1668 case MD5_FILE_UNREADABLE:
1669 case MD5_NOT_FOUND:
1670 gui_msg(Msg(msg::kError, "no_md5_found=No md5 file found for '{1}'. Please unselect Enable MD5 verification to restore.")(Filename));
1671 break;
1672 case MD5_MATCH_FAIL:
1673 gui_msg(Msg(msg::kError, "md5_fail_match=MD5 failed to match on '{1}'.")(Filename));
1674 break;
1675 }
1676 return false;
1677}
1678
bigbiff bigbiff8fd4b092016-08-30 20:48:53 -04001679bool TWPartition::Check_MD5(PartitionSettings *part_settings) {
James Christopher Adduono79ae0932016-10-25 02:18:32 -04001680 string Full_Filename;
Dees_Troy43d8b002012-09-17 16:00:01 -04001681 char split_filename[512];
1682 int index = 0;
1683
Captain Throwbackff5935f2014-09-23 16:08:34 -04001684 sync();
1685
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05001686 Full_Filename = part_settings->Backup_Folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04001687 if (!TWFunc::Path_Exists(Full_Filename)) {
1688 // This is a split archive, we presume
James Christopher Adduono79ae0932016-10-25 02:18:32 -04001689 memset(split_filename, 0, sizeof(split_filename));
Dees_Troy83bd4832013-05-04 12:39:56 +00001690 while (index < 1000) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001691 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
James Christopher Adduono79ae0932016-10-25 02:18:32 -04001692 if (!TWFunc::Path_Exists(split_filename))
1693 break;
1694 LOGINFO("split_filename: %s\n", split_filename);
1695 if (!Check_Restore_File_MD5(split_filename))
1696 return false;
1697 index++;
Dees_Troy43d8b002012-09-17 16:00:01 -04001698 }
Dees_Troy4a2a1262012-09-18 09:33:47 -04001699 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001700 }
James Christopher Adduono79ae0932016-10-25 02:18:32 -04001701 return Check_Restore_File_MD5(Full_Filename); // Single file archive
Dees_Troy43d8b002012-09-17 16:00:01 -04001702}
1703
bigbiffce8f83c2015-12-12 18:30:21 -05001704bool TWPartition::Restore(PartitionSettings *part_settings) {
Matt Mower3c366972015-12-25 19:28:31 -06001705 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, gui_parse_text("{@restoring_hdr}"));
Ethan Yonkere080c1f2016-09-19 13:50:25 -05001706 LOGINFO("Restore filename is: %s/%s\n", part_settings->Backup_Folder.c_str(), Backup_FileName.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001707
bigbiffce8f83c2015-12-12 18:30:21 -05001708 string Restore_File_System = Get_Restore_File_System(part_settings);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001709
1710 if (Is_File_System(Restore_File_System))
bigbiffce8f83c2015-12-12 18:30:21 -05001711 return Restore_Tar(part_settings);
1712 else if (Is_Image(Restore_File_System))
1713 return Restore_Image(part_settings);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001714
1715 LOGERR("Unknown restore method for '%s'\n", Mount_Point.c_str());
1716 return false;
1717}
1718
bigbiffce8f83c2015-12-12 18:30:21 -05001719string TWPartition::Get_Restore_File_System(PartitionSettings *part_settings) {
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001720 size_t first_period, second_period;
1721 string Restore_File_System;
1722
Gary Peck43acadf2012-11-21 21:19:01 -08001723 // Parse backup filename to extract the file system before wiping
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05001724 first_period = Backup_FileName.find(".");
Gary Peck43acadf2012-11-21 21:19:01 -08001725 if (first_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001726 LOGERR("Unable to find file system (first period).\n");
thatd43bf2d2014-09-21 23:13:02 +02001727 return string();
Gary Peck43acadf2012-11-21 21:19:01 -08001728 }
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05001729 Restore_File_System = Backup_FileName.substr(first_period + 1, Backup_FileName.size() - first_period - 1);
Gary Peck43acadf2012-11-21 21:19:01 -08001730 second_period = Restore_File_System.find(".");
1731 if (second_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001732 LOGERR("Unable to find file system (second period).\n");
thatd43bf2d2014-09-21 23:13:02 +02001733 return string();
Gary Peck43acadf2012-11-21 21:19:01 -08001734 }
1735 Restore_File_System.resize(second_period);
Matt Mowera0cd91d2016-12-30 18:21:42 -06001736 LOGINFO("Restore file system is: '%s'.\n", Restore_File_System.c_str());
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001737 return Restore_File_System;
Dees_Troy51a0e822012-09-05 15:24:24 -04001738}
1739
1740string TWPartition::Backup_Method_By_Name() {
bigbiffce8f83c2015-12-12 18:30:21 -05001741 if (Backup_Method == BM_NONE)
Dees_Troy38bd7602012-09-14 13:33:53 -04001742 return "none";
bigbiffce8f83c2015-12-12 18:30:21 -05001743 else if (Backup_Method == BM_FILES)
Dees_Troy38bd7602012-09-14 13:33:53 -04001744 return "files";
bigbiffce8f83c2015-12-12 18:30:21 -05001745 else if (Backup_Method == BM_DD)
Dees_Troy38bd7602012-09-14 13:33:53 -04001746 return "dd";
bigbiffce8f83c2015-12-12 18:30:21 -05001747 else if (Backup_Method == BM_FLASH_UTILS)
Dees_Troy38bd7602012-09-14 13:33:53 -04001748 return "flash_utils";
1749 else
1750 return "undefined";
1751 return "ERROR!";
Dees_Troy51a0e822012-09-05 15:24:24 -04001752}
1753
1754bool TWPartition::Decrypt(string Password) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001755 LOGINFO("STUB TWPartition::Decrypt, password: '%s'\n", Password.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001756 // Is this needed?
Dees_Troy51a0e822012-09-05 15:24:24 -04001757 return 1;
1758}
1759
1760bool TWPartition::Wipe_Encryption() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001761 bool Save_Data_Media = Has_Data_Media;
1762
1763 if (!UnMount(true))
1764 return false;
1765
Dees_Troy38bd7602012-09-14 13:33:53 -04001766 Has_Data_Media = false;
Dees_Troy74fb2e92013-04-15 14:35:47 +00001767 Decrypted_Block_Device = "";
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001768#ifdef TW_INCLUDE_CRYPTO
Ethan Yonker1b190162016-12-05 15:25:19 -06001769 if (Is_Decrypted && !Decrypted_Block_Device.empty()) {
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001770 if (!UnMount(true))
1771 return false;
Matt Mower2b18a532015-02-20 16:58:05 -06001772 if (delete_crypto_blk_dev((char*)("userdata")) != 0) {
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001773 LOGERR("Error deleting crypto block device, continuing anyway.\n");
1774 }
1775 }
1776#endif
Dees_Troy74fb2e92013-04-15 14:35:47 +00001777 Is_Decrypted = false;
1778 Is_Encrypted = false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001779 Find_Actual_Block_Device();
Ethan Yonkerbc85b632015-08-09 12:48:14 -05001780 if (Crypto_Key_Location == "footer") {
1781 int newlen, fd;
1782 if (Length != 0) {
1783 newlen = Length;
1784 if (newlen < 0)
1785 newlen = newlen * -1;
1786 } else {
1787 newlen = CRYPT_FOOTER_OFFSET;
1788 }
1789 if ((fd = open(Actual_Block_Device.c_str(), O_RDWR)) < 0) {
1790 gui_print_color("warning", "Unable to open '%s' to wipe crypto key\n", Actual_Block_Device.c_str());
1791 } else {
1792 unsigned int block_count;
1793 if ((ioctl(fd, BLKGETSIZE, &block_count)) == -1) {
1794 gui_print_color("warning", "Unable to get block size for wiping crypto footer.\n");
1795 } else {
1796 off64_t offset = ((off64_t)block_count * 512) - newlen;
1797 if (lseek64(fd, offset, SEEK_SET) == -1) {
1798 gui_print_color("warning", "Unable to lseek64 for wiping crypto footer.\n");
1799 } else {
1800 void* buffer = malloc(newlen);
1801 if (!buffer) {
1802 gui_print_color("warning", "Failed to malloc for wiping crypto footer.\n");
1803 } else {
1804 memset(buffer, 0, newlen);
1805 int ret = write(fd, buffer, newlen);
1806 if (ret != newlen) {
1807 gui_print_color("warning", "Failed to wipe crypto footer.\n");
1808 } else {
1809 LOGINFO("Successfully wiped crypto footer.\n");
1810 }
Matt Mower13a8f0b2015-09-26 15:40:03 -05001811 free(buffer);
Ethan Yonkerbc85b632015-08-09 12:48:14 -05001812 }
1813 }
1814 }
1815 close(fd);
1816 }
1817 } else {
Ethan Yonkerc2dafbb2016-03-15 22:20:59 -05001818 if (TWFunc::IOCTL_Get_Block_Size(Crypto_Key_Location.c_str()) >= 16384LLU) {
1819 string Command = "dd of='" + Crypto_Key_Location + "' if=/dev/zero bs=16384 count=1";
1820 TWFunc::Exec_Cmd(Command);
1821 } else {
1822 LOGINFO("Crypto key location reports size < 16K so not wiping crypto footer.\n");
1823 }
Ethan Yonkerbc85b632015-08-09 12:48:14 -05001824 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001825 if (Wipe(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001826 Has_Data_Media = Save_Data_Media;
1827 if (Has_Data_Media && !Symlink_Mount_Point.empty()) {
1828 Recreate_Media_Folder();
Ethan Yonker726a0202014-12-16 20:01:38 -06001829 if (Mount(false))
1830 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04001831 }
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001832 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
Ethan Yonker83e82572014-04-04 10:59:28 -05001833#ifndef TW_OEM_BUILD
Ethan Yonker74db1572015-10-28 12:44:49 -05001834 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 -05001835#endif
Dees_Troy38bd7602012-09-14 13:33:53 -04001836 return true;
1837 } else {
1838 Has_Data_Media = Save_Data_Media;
Ethan Yonker74db1572015-10-28 12:44:49 -05001839 gui_err("format_data_err=Unable to format to remove encryption.");
Ethan Yonker726a0202014-12-16 20:01:38 -06001840 if (Has_Data_Media && Mount(false))
1841 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04001842 return false;
1843 }
1844 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001845}
1846
1847void TWPartition::Check_FS_Type() {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001848 const char* type;
1849 blkid_probe pr;
Dees_Troy5bf43922012-09-07 16:07:55 -04001850
Dees_Troy68cab492012-12-12 19:29:35 +00001851 if (Fstab_File_System == "yaffs2" || Fstab_File_System == "mtd" || Fstab_File_System == "bml" || Ignore_Blkid)
1852 return; // Running blkid on some mtd devices causes a massive crash or needs to be skipped
Dees_Troy5bf43922012-09-07 16:07:55 -04001853
Dees_Troy38bd7602012-09-14 13:33:53 -04001854 Find_Actual_Block_Device();
Dees_Troy8170a922012-09-18 15:40:25 -04001855 if (!Is_Present)
1856 return;
Dees_Troy51127312012-09-08 13:08:49 -04001857
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001858 pr = blkid_new_probe_from_filename(Actual_Block_Device.c_str());
1859 if (blkid_do_fullprobe(pr)) {
1860 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00001861 LOGINFO("Can't probe device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001862 return;
Dees_Troy5bf43922012-09-07 16:07:55 -04001863 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001864
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05001865 if (blkid_probe_lookup_value(pr, "TYPE", &type, NULL) < 0) {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001866 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00001867 LOGINFO("can't find filesystem on device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001868 return;
1869 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001870
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001871 Current_File_System = type;
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001872 blkid_free_probe(pr);
Dees_Troy51a0e822012-09-05 15:24:24 -04001873}
1874
Gary Peck43acadf2012-11-21 21:19:01 -08001875bool TWPartition::Wipe_EXT23(string File_System) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001876 if (!UnMount(true))
1877 return false;
1878
Dees_Troy43d8b002012-09-17 16:00:01 -04001879 if (TWFunc::Path_Exists("/sbin/mke2fs")) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001880 string command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001881
Greg Wallace4b44fef2015-12-29 15:33:24 -05001882 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mke2fs"));
Dees_Troy38bd7602012-09-14 13:33:53 -04001883 Find_Actual_Block_Device();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001884 command = "mke2fs -t " + File_System + " -m 0 " + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001885 LOGINFO("mke2fs command: %s\n", command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001886 if (TWFunc::Exec_Cmd(command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001887 Current_File_System = File_System;
Dees_Troye58d5262012-09-21 12:27:57 -04001888 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05001889 gui_msg("done=Done.");
Dees_Troy38bd7602012-09-14 13:33:53 -04001890 return true;
1891 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001892 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04001893 return false;
1894 }
1895 } else
1896 return Wipe_RMRF();
1897
1898 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001899}
1900
1901bool TWPartition::Wipe_EXT4() {
Ethan Yonker25f20c12014-10-14 09:04:43 -05001902 Find_Actual_Block_Device();
1903 if (!Is_Present) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001904 LOGINFO("Block device not present, cannot wipe %s.\n", Display_Name.c_str());
1905 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Ethan Yonker25f20c12014-10-14 09:04:43 -05001906 return false;
1907 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001908 if (!UnMount(true))
1909 return false;
1910
Matt Mower87413642017-01-17 21:14:46 -06001911#if defined(USE_EXT4)
Ethan Yonkerf27497f2014-02-09 11:48:33 -06001912 int ret;
1913 char *secontext = NULL;
1914
Greg Wallace4b44fef2015-12-29 15:33:24 -05001915 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("make_ext4fs"));
Ethan Yonkerf27497f2014-02-09 11:48:33 -06001916
Dees Troy99c8dbf2014-03-10 16:53:58 +00001917 if (!selinux_handle || selabel_lookup(selinux_handle, &secontext, Mount_Point.c_str(), S_IFDIR) < 0) {
Ethan Yonkerf27497f2014-02-09 11:48:33 -06001918 LOGINFO("Cannot lookup security context for '%s'\n", Mount_Point.c_str());
1919 ret = make_ext4fs(Actual_Block_Device.c_str(), Length, Mount_Point.c_str(), NULL);
1920 } else {
1921 ret = make_ext4fs(Actual_Block_Device.c_str(), Length, Mount_Point.c_str(), selinux_handle);
1922 }
1923 if (ret != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001924 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troya95f55c2013-08-17 13:14:43 +00001925 return false;
1926 } else {
bigbiff bigbiffc49d7062013-10-11 20:28:00 -04001927 string sedir = Mount_Point + "/lost+found";
1928 PartitionManager.Mount_By_Path(sedir.c_str(), true);
1929 rmdir(sedir.c_str());
1930 mkdir(sedir.c_str(), S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP);
Dees_Troya95f55c2013-08-17 13:14:43 +00001931 return true;
1932 }
1933#else
Dees_Troy43d8b002012-09-17 16:00:01 -04001934 if (TWFunc::Path_Exists("/sbin/make_ext4fs")) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001935 string Command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001936
Greg Wallace4b44fef2015-12-29 15:33:24 -05001937 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("make_ext4fs"));
Dees_Troy38bd7602012-09-14 13:33:53 -04001938 Find_Actual_Block_Device();
1939 Command = "make_ext4fs";
1940 if (!Is_Decrypted && Length != 0) {
1941 // Only use length if we're not decrypted
1942 char len[32];
1943 sprintf(len, "%i", Length);
1944 Command += " -l ";
1945 Command += len;
1946 }
Dees_Troy5295d582013-09-06 15:51:08 +00001947 if (TWFunc::Path_Exists("/file_contexts")) {
1948 Command += " -S /file_contexts";
1949 }
1950 Command += " -a " + Mount_Point + " " + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001951 LOGINFO("make_ext4fs command: %s\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001952 if (TWFunc::Exec_Cmd(Command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001953 Current_File_System = "ext4";
Dees_Troye58d5262012-09-21 12:27:57 -04001954 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05001955 gui_msg("done=Done.");
Dees_Troy38bd7602012-09-14 13:33:53 -04001956 return true;
1957 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001958 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04001959 return false;
1960 }
1961 } else
Gary Peck43acadf2012-11-21 21:19:01 -08001962 return Wipe_EXT23("ext4");
Dees_Troya95f55c2013-08-17 13:14:43 +00001963#endif
Dees_Troy38bd7602012-09-14 13:33:53 -04001964 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001965}
1966
1967bool TWPartition::Wipe_FAT() {
Vojtech Bocek05534202013-09-11 08:11:56 +02001968 string command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001969
Matt Mower18794c82015-11-11 16:22:45 -06001970 if (TWFunc::Path_Exists("/sbin/mkfs.fat")) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001971 if (!UnMount(true))
1972 return false;
1973
Greg Wallace4b44fef2015-12-29 15:33:24 -05001974 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mkfs.fat"));
Dees_Troy38bd7602012-09-14 13:33:53 -04001975 Find_Actual_Block_Device();
Matt Mower18794c82015-11-11 16:22:45 -06001976 command = "mkfs.fat " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02001977 if (TWFunc::Exec_Cmd(command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001978 Current_File_System = "vfat";
Dees_Troye58d5262012-09-21 12:27:57 -04001979 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05001980 gui_msg("done=Done.");
Dees_Troy38bd7602012-09-14 13:33:53 -04001981 return true;
1982 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001983 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04001984 return false;
1985 }
1986 return true;
1987 }
1988 else
1989 return Wipe_RMRF();
1990
1991 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001992}
1993
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001994bool TWPartition::Wipe_EXFAT() {
Vojtech Bocek05534202013-09-11 08:11:56 +02001995 string command;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001996
1997 if (TWFunc::Path_Exists("/sbin/mkexfatfs")) {
1998 if (!UnMount(true))
1999 return false;
2000
Greg Wallace4b44fef2015-12-29 15:33:24 -05002001 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mkexfatfs"));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002002 Find_Actual_Block_Device();
2003 command = "mkexfatfs " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02002004 if (TWFunc::Exec_Cmd(command) == 0) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002005 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05002006 gui_msg("done=Done.");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002007 return true;
2008 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002009 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002010 return false;
2011 }
2012 return true;
2013 }
2014 return false;
2015}
2016
Dees_Troy38bd7602012-09-14 13:33:53 -04002017bool TWPartition::Wipe_MTD() {
2018 if (!UnMount(true))
2019 return false;
2020
Greg Wallace4b44fef2015-12-29 15:33:24 -05002021 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("MTD"));
Dees_Troy38bd7602012-09-14 13:33:53 -04002022
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02002023 mtd_scan_partitions();
2024 const MtdPartition* mtd = mtd_find_partition_by_name(MTD_Name.c_str());
2025 if (mtd == NULL) {
2026 LOGERR("No mtd partition named '%s'", MTD_Name.c_str());
2027 return false;
2028 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002029
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02002030 MtdWriteContext* ctx = mtd_write_partition(mtd);
2031 if (ctx == NULL) {
2032 LOGERR("Can't write '%s', failed to format.", MTD_Name.c_str());
2033 return false;
2034 }
2035 if (mtd_erase_blocks(ctx, -1) == -1) {
2036 mtd_write_close(ctx);
2037 LOGERR("Failed to format '%s'", MTD_Name.c_str());
2038 return false;
2039 }
2040 if (mtd_write_close(ctx) != 0) {
2041 LOGERR("Failed to close '%s'", MTD_Name.c_str());
2042 return false;
2043 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08002044 Current_File_System = "yaffs2";
Dees_Troye58d5262012-09-21 12:27:57 -04002045 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05002046 gui_msg("done=Done.");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02002047 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002048}
2049
2050bool TWPartition::Wipe_RMRF() {
Dees_Troy38bd7602012-09-14 13:33:53 -04002051 if (!Mount(true))
2052 return false;
Ethan Yonker726a0202014-12-16 20:01:38 -06002053 // This is the only wipe that leaves the partition mounted, so we
2054 // must manually remove the partition from MTP if it is a storage
2055 // partition.
2056 if (Is_Storage)
2057 PartitionManager.Remove_MTP_Storage(MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04002058
Ethan Yonker74db1572015-10-28 12:44:49 -05002059 gui_msg(Msg("remove_all=Removing all files under '{1}'")(Mount_Point));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002060 TWFunc::removeDir(Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04002061 Recreate_AndSec_Folder();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002062 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002063}
2064
Dees_Troye5017042013-08-29 16:38:55 +00002065bool TWPartition::Wipe_F2FS() {
Vojtech Bocek05534202013-09-11 08:11:56 +02002066 string command;
Dees_Troye5017042013-08-29 16:38:55 +00002067
2068 if (TWFunc::Path_Exists("/sbin/mkfs.f2fs")) {
2069 if (!UnMount(true))
2070 return false;
2071
Greg Wallace4b44fef2015-12-29 15:33:24 -05002072 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mkfs.f2fs"));
Dees_Troye5017042013-08-29 16:38:55 +00002073 Find_Actual_Block_Device();
Greg Wallace558aef72016-01-19 21:14:30 -05002074 command = "mkfs.f2fs -t 0";
dhacker29a3fa75f2015-01-17 19:42:33 -05002075 if (!Is_Decrypted && Length != 0) {
2076 // Only use length if we're not decrypted
2077 char len[32];
2078 int mod_length = Length;
2079 if (Length < 0)
2080 mod_length *= -1;
2081 sprintf(len, "%i", mod_length);
2082 command += " -r ";
2083 command += len;
2084 }
2085 command += " " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02002086 if (TWFunc::Exec_Cmd(command) == 0) {
Dees_Troye5017042013-08-29 16:38:55 +00002087 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05002088 gui_msg("done=Done.");
Dees_Troye5017042013-08-29 16:38:55 +00002089 return true;
2090 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002091 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troye5017042013-08-29 16:38:55 +00002092 return false;
2093 }
2094 return true;
2095 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002096 LOGINFO("mkfs.f2fs binary not found, using rm -rf to wipe.\n");
Dees_Troye5017042013-08-29 16:38:55 +00002097 return Wipe_RMRF();
2098 }
2099 return false;
2100}
2101
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002102bool TWPartition::Wipe_NTFS() {
2103 string command;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002104 string Ntfsmake_Binary;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002105
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002106 if (TWFunc::Path_Exists("/sbin/mkntfs"))
2107 Ntfsmake_Binary = "mkntfs";
2108 else if (TWFunc::Path_Exists("/sbin/mkfs.ntfs"))
2109 Ntfsmake_Binary = "mkfs.ntfs";
2110 else
2111 return false;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002112
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002113 if (!UnMount(true))
2114 return false;
2115
Greg Wallace4b44fef2015-12-29 15:33:24 -05002116 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)(Ntfsmake_Binary));
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002117 Find_Actual_Block_Device();
2118 command = "/sbin/" + Ntfsmake_Binary + " " + Actual_Block_Device;
2119 if (TWFunc::Exec_Cmd(command) == 0) {
2120 Recreate_AndSec_Folder();
2121 gui_msg("done=Done.");
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002122 return true;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002123 } else {
2124 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
2125 return false;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002126 }
2127 return false;
2128}
2129
Dees_Troy51a0e822012-09-05 15:24:24 -04002130bool TWPartition::Wipe_Data_Without_Wiping_Media() {
Ethan Yonker83e82572014-04-04 10:59:28 -05002131#ifdef TW_OEM_BUILD
2132 // In an OEM Build we want to do a full format
2133 return Wipe_Encryption();
2134#else
Ethan Yonker66a19492015-12-10 10:19:45 -06002135 bool ret = false;
Dees_Troy38bd7602012-09-14 13:33:53 -04002136
Dees_Troy38bd7602012-09-14 13:33:53 -04002137 if (!Mount(true))
2138 return false;
2139
Ethan Yonker74db1572015-10-28 12:44:49 -05002140 gui_msg("wiping_data=Wiping data without wiping /data/media ...");
Ethan Yonker66a19492015-12-10 10:19:45 -06002141 ret = Wipe_Data_Without_Wiping_Media_Func(Mount_Point + "/");
2142 if (ret)
2143 gui_msg("done=Done.");
2144 return ret;
2145#endif // ifdef TW_OEM_BUILD
2146}
2147
2148bool TWPartition::Wipe_Data_Without_Wiping_Media_Func(const string& parent __unused) {
2149 string dir;
Dees_Troy38bd7602012-09-14 13:33:53 -04002150
2151 DIR* d;
Ethan Yonker66a19492015-12-10 10:19:45 -06002152 d = opendir(parent.c_str());
Dees_Troy16b74352012-11-14 22:27:31 +00002153 if (d != NULL) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002154 struct dirent* de;
2155 while ((de = readdir(d)) != NULL) {
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002156 if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) continue;
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05002157
Ethan Yonker66a19492015-12-10 10:19:45 -06002158 dir = parent;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002159 dir.append(de->d_name);
Ethan Yonker3fdcda42016-11-30 12:29:37 -06002160 if (wipe_exclusions.check_skip_dirs(dir)) {
Ethan Yonker66a19492015-12-10 10:19:45 -06002161 LOGINFO("skipped '%s'\n", dir.c_str());
2162 continue;
2163 }
Dees_Troyce675462013-01-09 19:48:21 +00002164 if (de->d_type == DT_DIR) {
Ethan Yonker66a19492015-12-10 10:19:45 -06002165 dir.append("/");
2166 if (!Wipe_Data_Without_Wiping_Media_Func(dir)) {
2167 closedir(d);
2168 return false;
2169 }
2170 rmdir(dir.c_str());
bigbiff bigbiff98f1f902013-01-19 18:46:13 -05002171 } 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 +00002172 if (!unlink(dir.c_str()))
Ethan Yonker79f88bd2016-12-09 14:52:12 -06002173 LOGINFO("Unable to unlink '%s': %s\n", dir.c_str(), strerror(errno));
Dees_Troyce675462013-01-09 19:48:21 +00002174 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002175 }
2176 closedir(d);
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05002177
Dees_Troy16b74352012-11-14 22:27:31 +00002178 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -04002179 }
Ethan Yonker74db1572015-10-28 12:44:49 -05002180 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy16b74352012-11-14 22:27:31 +00002181 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04002182}
2183
bigbiffce8f83c2015-12-12 18:30:21 -05002184bool TWPartition::Backup_Tar(PartitionSettings *part_settings, pid_t *tar_fork_pid) {
Ethan Yonker472f5062016-02-25 13:47:30 -06002185 string Full_FileName;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002186 twrpTar tar;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05002187
Dees_Troy43d8b002012-09-17 16:00:01 -04002188 if (!Mount(true))
2189 return false;
2190
Dees_Troya13d74f2013-03-24 08:54:55 -05002191 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Backup_Display_Name, "Backing Up");
Ethan Yonker74db1572015-10-28 12:44:49 -05002192 gui_msg(Msg("backing_up=Backing up {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002193
Ethan Yonker472f5062016-02-25 13:47:30 -06002194 DataManager::GetValue(TW_USE_COMPRESSION_VAR, tar.use_compression);
Matt Mowerbb81e5d2014-03-20 18:05:41 -05002195
Dees_Troy83bd4832013-05-04 12:39:56 +00002196#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
Ethan Yonker472f5062016-02-25 13:47:30 -06002197 if (Can_Encrypt_Backup) {
2198 DataManager::GetValue("tw_encrypt_backup", tar.use_encryption);
2199 if (tar.use_encryption) {
2200 if (Use_Userdata_Encryption)
2201 tar.userdata_encryption = tar.use_encryption;
2202 string Password;
2203 DataManager::GetValue("tw_backup_password", Password);
2204 tar.setpassword(Password);
2205 } else {
2206 tar.use_encryption = 0;
2207 }
Dees_Troy83bd4832013-05-04 12:39:56 +00002208 }
2209#endif
Dees_Troy43d8b002012-09-17 16:00:01 -04002210
Ethan Yonker472f5062016-02-25 13:47:30 -06002211 Backup_FileName = Backup_Name + "." + Current_File_System + ".win";
Ethan Yonkere080c1f2016-09-19 13:50:25 -05002212 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
Dees_Troy83bd4832013-05-04 12:39:56 +00002213 tar.has_data_media = Has_Data_Media;
bigbiffce8f83c2015-12-12 18:30:21 -05002214 tar.part_settings = part_settings;
Ethan Yonker3fdcda42016-11-30 12:29:37 -06002215 tar.backup_exclusions = &backup_exclusions;
Dees Troye0a433a2013-12-02 04:10:37 +00002216 tar.setdir(Backup_Path);
2217 tar.setfn(Full_FileName);
2218 tar.setsize(Backup_Size);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002219 tar.partition_name = Backup_Name;
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002220 tar.backup_folder = part_settings->Backup_Folder;
bigbiffce8f83c2015-12-12 18:30:21 -05002221 if (tar.createTarFork(tar_fork_pid) != 0)
Dees Troye0a433a2013-12-02 04:10:37 +00002222 return false;
Dees_Troy43d8b002012-09-17 16:00:01 -04002223 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002224}
2225
bigbiffce8f83c2015-12-12 18:30:21 -05002226bool TWPartition::Backup_Image(PartitionSettings *part_settings) {
2227 string Full_FileName, adb_file_name;
igoriok87e3d932013-01-31 21:03:53 +02002228
Ethan Yonker74db1572015-10-28 12:44:49 -05002229 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, gui_parse_text("{@backing}"));
2230 gui_msg(Msg("backing_up=Backing up {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002231
Ethan Yonker472f5062016-02-25 13:47:30 -06002232 Backup_FileName = Backup_Name + "." + Current_File_System + ".win";
Dees_Troy43d8b002012-09-17 16:00:01 -04002233
bigbiffce8f83c2015-12-12 18:30:21 -05002234 if (part_settings->adbbackup) {
2235 Full_FileName = TW_ADB_BACKUP;
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002236 adb_file_name = part_settings->Backup_Folder + "/" + Backup_FileName;
bigbiffce8f83c2015-12-12 18:30:21 -05002237 }
2238 else
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002239 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
bigbiffce8f83c2015-12-12 18:30:21 -05002240
2241 part_settings->total_restore_size = Backup_Size;
2242
2243 if (part_settings->adbbackup) {
2244 if (!twadbbu::Write_TWIMG(adb_file_name, Backup_Size))
2245 return false;
2246 }
2247
2248 if (!Raw_Read_Write(part_settings))
Ethan Yonker472f5062016-02-25 13:47:30 -06002249 return false;
2250
bigbiffce8f83c2015-12-12 18:30:21 -05002251 if (part_settings->adbbackup) {
2252 if (!twadbbu::Write_TWEOF())
2253 return false;
Dees_Troy7c2dec82012-09-26 09:49:14 -04002254 }
Dees_Troy43d8b002012-09-17 16:00:01 -04002255 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002256}
2257
bigbiffce8f83c2015-12-12 18:30:21 -05002258bool TWPartition::Raw_Read_Write(PartitionSettings *part_settings) {
2259 unsigned long long RW_Block_Size, Remain = Backup_Size;
2260 int src_fd = -1, dest_fd = -1;
2261 ssize_t bs;
Ethan Yonker472f5062016-02-25 13:47:30 -06002262 bool ret = false;
2263 void* buffer = NULL;
2264 unsigned long long backedup_size = 0;
bigbiffce8f83c2015-12-12 18:30:21 -05002265 string srcfn, destfn;
Ethan Yonker472f5062016-02-25 13:47:30 -06002266
bigbiffce8f83c2015-12-12 18:30:21 -05002267 if (part_settings->PM_Method == PM_BACKUP) {
2268 srcfn = Actual_Block_Device;
2269 if (part_settings->adbbackup)
2270 destfn = TW_ADB_BACKUP;
2271 else
Ethan Yonkere080c1f2016-09-19 13:50:25 -05002272 destfn = part_settings->Backup_Folder + "/" + Backup_FileName;
bigbiffce8f83c2015-12-12 18:30:21 -05002273 }
2274 else {
2275 destfn = Actual_Block_Device;
2276 if (part_settings->adbbackup) {
2277 srcfn = TW_ADB_RESTORE;
2278 } else {
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002279 srcfn = part_settings->Backup_Folder + "/" + Backup_FileName;
bigbiffce8f83c2015-12-12 18:30:21 -05002280 Remain = TWFunc::Get_File_Size(srcfn);
2281 }
2282 }
Ethan Yonker472f5062016-02-25 13:47:30 -06002283
bigbiffce8f83c2015-12-12 18:30:21 -05002284 src_fd = open(srcfn.c_str(), O_RDONLY | O_LARGEFILE);
Ethan Yonker472f5062016-02-25 13:47:30 -06002285 if (src_fd < 0) {
bigbiffce8f83c2015-12-12 18:30:21 -05002286 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(srcfn.c_str())(strerror(errno)));
Ethan Yonker472f5062016-02-25 13:47:30 -06002287 return false;
2288 }
bigbiffce8f83c2015-12-12 18:30:21 -05002289
2290 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 -06002291 if (dest_fd < 0) {
bigbiffce8f83c2015-12-12 18:30:21 -05002292 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(destfn.c_str())(strerror(errno)));
Ethan Yonker472f5062016-02-25 13:47:30 -06002293 goto exit;
2294 }
bigbiffce8f83c2015-12-12 18:30:21 -05002295
2296 LOGINFO("Reading '%s', writing '%s'\n", srcfn.c_str(), destfn.c_str());
2297
2298 if (part_settings->adbbackup) {
2299 RW_Block_Size = MAX_ADB_READ;
2300 bs = MAX_ADB_READ;
2301 }
2302 else {
2303 RW_Block_Size = 1048576LLU; // 1MB
2304 bs = (ssize_t)(RW_Block_Size);
2305 }
2306
Ethan Yonker472f5062016-02-25 13:47:30 -06002307 buffer = malloc((size_t)bs);
2308 if (!buffer) {
2309 LOGINFO("Raw_Read_Write failed to malloc\n");
2310 goto exit;
2311 }
bigbiffce8f83c2015-12-12 18:30:21 -05002312
2313 if (part_settings->progress)
2314 part_settings->progress->SetPartitionSize(part_settings->total_restore_size);
2315
Ethan Yonker472f5062016-02-25 13:47:30 -06002316 while (Remain > 0) {
2317 if (Remain < RW_Block_Size)
bigbiffce8f83c2015-12-12 18:30:21 -05002318 bs = (ssize_t)(Remain);
Matt Mowera8a89d12016-12-30 18:10:37 -06002319 if (read(src_fd, buffer, bs) != bs) {
Ethan Yonker472f5062016-02-25 13:47:30 -06002320 LOGINFO("Error reading source fd (%s)\n", strerror(errno));
2321 goto exit;
2322 }
2323 if (write(dest_fd, buffer, bs) != bs) {
2324 LOGINFO("Error writing destination fd (%s)\n", strerror(errno));
2325 goto exit;
2326 }
2327 backedup_size += (unsigned long long)(bs);
Matt Mower029a82d2017-01-08 13:12:38 -06002328 Remain -= (unsigned long long)(bs);
bigbiffce8f83c2015-12-12 18:30:21 -05002329 if (part_settings->progress)
2330 part_settings->progress->UpdateSize(backedup_size);
Ethan Yonker472f5062016-02-25 13:47:30 -06002331 if (PartitionManager.Check_Backup_Cancel() != 0)
2332 goto exit;
2333 }
bigbiffce8f83c2015-12-12 18:30:21 -05002334 if (part_settings->progress)
2335 part_settings->progress->UpdateDisplayDetails(true);
Ethan Yonker472f5062016-02-25 13:47:30 -06002336 fsync(dest_fd);
nailyka083dc62016-11-11 21:41:28 +01002337
2338 if (!part_settings->adbbackup && part_settings->PM_Method == PM_BACKUP) {
2339 tw_set_default_metadata(destfn.c_str());
2340 LOGINFO("Restored default metadata for %s\n", destfn.c_str());
2341 }
2342
Ethan Yonker472f5062016-02-25 13:47:30 -06002343 ret = true;
2344exit:
2345 if (src_fd >= 0)
2346 close(src_fd);
2347 if (dest_fd >= 0)
2348 close(dest_fd);
2349 if (buffer)
2350 free(buffer);
2351 return ret;
2352}
2353
bigbiffce8f83c2015-12-12 18:30:21 -05002354bool TWPartition::Backup_Dump_Image(PartitionSettings *part_settings) {
Vojtech Bocek05534202013-09-11 08:11:56 +02002355 string Full_FileName, Command;
Dees_Troy43d8b002012-09-17 16:00:01 -04002356
Ethan Yonker74db1572015-10-28 12:44:49 -05002357 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, gui_parse_text("{@backing}"));
2358 gui_msg(Msg("backing_up=Backing up {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002359
bigbiffce8f83c2015-12-12 18:30:21 -05002360 if (part_settings->progress)
2361 part_settings->progress->SetPartitionSize(Backup_Size);
Dees_Troy43d8b002012-09-17 16:00:01 -04002362
Ethan Yonker472f5062016-02-25 13:47:30 -06002363 Backup_FileName = Backup_Name + "." + Current_File_System + ".win";
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002364 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04002365
2366 Command = "dump_image " + MTD_Name + " '" + Full_FileName + "'";
bigbiffce8f83c2015-12-12 18:30:21 -05002367
Dees_Troy2673cec2013-04-02 20:22:16 +00002368 LOGINFO("Backup command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02002369 TWFunc::Exec_Cmd(Command);
Ethan Yonker4b94cfd2014-12-11 10:00:45 -06002370 tw_set_default_metadata(Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04002371 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
2372 // 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 -06002373 gui_msg(Msg(msg::kError, "backup_size=Backup file size for '{1}' is 0 bytes.")(Full_FileName));
Dees_Troy7c2dec82012-09-26 09:49:14 -04002374 return false;
2375 }
bigbiffce8f83c2015-12-12 18:30:21 -05002376 if (part_settings->progress)
2377 part_settings->progress->UpdateSize(Backup_Size);
2378
Dees_Troy43d8b002012-09-17 16:00:01 -04002379 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002380}
2381
bigbiffce8f83c2015-12-12 18:30:21 -05002382unsigned long long TWPartition::Get_Restore_Size(PartitionSettings *part_settings) {
2383 if (!part_settings->adbbackup) {
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002384 InfoManager restore_info(part_settings->Backup_Folder + "/" + Backup_Name + ".info");
bigbiffce8f83c2015-12-12 18:30:21 -05002385 if (restore_info.LoadValues() == 0) {
2386 if (restore_info.GetValue("backup_size", Restore_Size) == 0) {
2387 LOGINFO("Read info file, restore size is %llu\n", Restore_Size);
2388 return Restore_Size;
2389 }
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002390 }
2391 }
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002392
Matt Mower029a82d2017-01-08 13:12:38 -06002393 string Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
2394 string Restore_File_System = Get_Restore_File_System(part_settings);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002395
2396 if (Is_Image(Restore_File_System)) {
2397 Restore_Size = TWFunc::Get_File_Size(Full_FileName);
2398 return Restore_Size;
2399 }
2400
2401 twrpTar tar;
2402 tar.setdir(Backup_Path);
2403 tar.setfn(Full_FileName);
bigbiffce8f83c2015-12-12 18:30:21 -05002404 tar.backup_name = Full_FileName;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002405#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
2406 string Password;
2407 DataManager::GetValue("tw_restore_password", Password);
2408 if (!Password.empty())
2409 tar.setpassword(Password);
2410#endif
2411 tar.partition_name = Backup_Name;
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002412 tar.backup_folder = part_settings->Backup_Folder;
bigbiffce8f83c2015-12-12 18:30:21 -05002413 tar.part_settings = part_settings;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002414 Restore_Size = tar.get_size();
2415 return Restore_Size;
2416}
2417
bigbiffce8f83c2015-12-12 18:30:21 -05002418bool TWPartition::Restore_Tar(PartitionSettings *part_settings) {
Ethan Yonker472f5062016-02-25 13:47:30 -06002419 string Full_FileName;
Dees Troy4159aed2014-02-28 17:24:43 +00002420 bool ret = false;
bigbiffce8f83c2015-12-12 18:30:21 -05002421 string Restore_File_System = Get_Restore_File_System(part_settings);
Dees_Troy43d8b002012-09-17 16:00:01 -04002422
Dees_Troye58d5262012-09-21 12:27:57 -04002423 if (Has_Android_Secure) {
Dees_Troye58d5262012-09-21 12:27:57 -04002424 if (!Wipe_AndSec())
2425 return false;
Gary Peck43acadf2012-11-21 21:19:01 -08002426 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002427 gui_msg(Msg("wiping=Wiping {1}")(Backup_Display_Name));
Ethan Yonker5eac2222014-06-11 12:22:55 -05002428 if (Has_Data_Media && Mount_Point == "/data" && Restore_File_System != Current_File_System) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002429 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 -05002430 if (!Wipe_Data_Without_Wiping_Media())
2431 return false;
2432 } else {
2433 if (!Wipe(Restore_File_System))
2434 return false;
2435 }
Dees_Troye58d5262012-09-21 12:27:57 -04002436 }
Matt Mower3c366972015-12-25 19:28:31 -06002437 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Backup_Display_Name, gui_parse_text("{@restoring_hdr}"));
Ethan Yonker74db1572015-10-28 12:44:49 -05002438 gui_msg(Msg("restoring=Restoring {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002439
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05002440 // Remount as read/write as needed so we can restore the backup
2441 if (!ReMount_RW(true))
Dees_Troy43d8b002012-09-17 16:00:01 -04002442 return false;
2443
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002444 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
Ethan Yonker87af5632014-02-10 11:56:35 -06002445 twrpTar tar;
bigbiffce8f83c2015-12-12 18:30:21 -05002446 tar.part_settings = part_settings;
Ethan Yonker87af5632014-02-10 11:56:35 -06002447 tar.setdir(Backup_Path);
2448 tar.setfn(Full_FileName);
2449 tar.backup_name = Backup_Name;
2450#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
2451 string Password;
2452 DataManager::GetValue("tw_restore_password", Password);
2453 if (!Password.empty())
2454 tar.setpassword(Password);
2455#endif
bigbiffce8f83c2015-12-12 18:30:21 -05002456 part_settings->progress->SetPartitionSize(Get_Restore_Size(part_settings));
2457 if (tar.extractTarFork() != 0)
Dees Troy4159aed2014-02-28 17:24:43 +00002458 ret = false;
2459 else
2460 ret = true;
2461#ifdef HAVE_CAPABILITIES
2462 // Restore capabilities to the run-as binary
2463 if (Mount_Point == "/system" && Mount(true) && TWFunc::Path_Exists("/system/bin/run-as")) {
2464 struct vfs_cap_data cap_data;
2465 uint64_t capabilities = (1 << CAP_SETUID) | (1 << CAP_SETGID);
2466
2467 memset(&cap_data, 0, sizeof(cap_data));
2468 cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE;
2469 cap_data.data[0].permitted = (uint32_t) (capabilities & 0xffffffff);
2470 cap_data.data[0].inheritable = 0;
2471 cap_data.data[1].permitted = (uint32_t) (capabilities >> 32);
2472 cap_data.data[1].inheritable = 0;
2473 if (setxattr("/system/bin/run-as", XATTR_NAME_CAPS, &cap_data, sizeof(cap_data), 0) < 0) {
2474 LOGINFO("Failed to reset capabilities of /system/bin/run-as binary.\n");
2475 } else {
2476 LOGINFO("Reset capabilities of /system/bin/run-as binary successful.\n");
2477 }
2478 }
2479#endif
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05002480 if (Mount_Read_Only || Mount_Flags & MS_RDONLY)
2481 // Remount as read only when restoration is complete
2482 ReMount(true);
2483
Dees Troy4159aed2014-02-28 17:24:43 +00002484 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04002485}
2486
bigbiffce8f83c2015-12-12 18:30:21 -05002487bool TWPartition::Restore_Image(PartitionSettings *part_settings) {
Ethan Yonker96af84a2015-01-05 14:58:36 -06002488 string Full_FileName;
bigbiffce8f83c2015-12-12 18:30:21 -05002489 string Restore_File_System = Get_Restore_File_System(part_settings);
Dees_Troy43d8b002012-09-17 16:00:01 -04002490
Matt Mower3c366972015-12-25 19:28:31 -06002491 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Backup_Display_Name, gui_parse_text("{@restoring_hdr}"));
Ethan Yonker74db1572015-10-28 12:44:49 -05002492 gui_msg(Msg("restoring=Restoring {1}...")(Backup_Display_Name));
bigbiffce8f83c2015-12-12 18:30:21 -05002493
2494 if (part_settings->adbbackup)
2495 Full_FileName = TW_ADB_RESTORE;
2496 else
Ethan Yonkere080c1f2016-09-19 13:50:25 -05002497 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
Gary Peck15e623d2012-11-21 21:07:58 -08002498
Ethan Yonker96af84a2015-01-05 14:58:36 -06002499 if (Restore_File_System == "emmc") {
bigbiffce8f83c2015-12-12 18:30:21 -05002500 if (!part_settings->adbbackup)
2501 part_settings->total_restore_size = (uint64_t)(TWFunc::Get_File_Size(Full_FileName));
2502 if (!Raw_Read_Write(part_settings))
Ethan Yonker96af84a2015-01-05 14:58:36 -06002503 return false;
2504 } else if (Restore_File_System == "mtd" || Restore_File_System == "bml") {
bigbiffce8f83c2015-12-12 18:30:21 -05002505 if (!Flash_Image_FI(Full_FileName, part_settings->progress))
2506 return false;
2507 }
2508
2509 if (part_settings->adbbackup) {
2510 if (!twadbbu::Write_TWEOF())
Ethan Yonker96af84a2015-01-05 14:58:36 -06002511 return false;
Gary Peck15e623d2012-11-21 21:07:58 -08002512 }
Dees_Troy43d8b002012-09-17 16:00:01 -04002513 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002514}
Dees_Troy5bf43922012-09-07 16:07:55 -04002515
2516bool TWPartition::Update_Size(bool Display_Error) {
Dees_Troy0550cfb2012-10-13 11:56:13 -04002517 bool ret = false, Was_Already_Mounted = false;
Dees_Troy51127312012-09-08 13:08:49 -04002518
Ethan Yonker1b190162016-12-05 15:25:19 -06002519 Find_Actual_Block_Device();
2520
2521 if (!Can_Be_Mounted && !Is_Encrypted) {
2522 if (TWFunc::Path_Exists(Actual_Block_Device) && Find_Partition_Size()) {
2523 Used = Size;
2524 Backup_Size = Size;
2525 return true;
2526 }
Dees_Troy5bf43922012-09-07 16:07:55 -04002527 return false;
Ethan Yonker1b190162016-12-05 15:25:19 -06002528 }
Dees_Troy5bf43922012-09-07 16:07:55 -04002529
Dees_Troy0550cfb2012-10-13 11:56:13 -04002530 Was_Already_Mounted = Is_Mounted();
Dees_Troy38bd7602012-09-14 13:33:53 -04002531 if (Removable || Is_Encrypted) {
2532 if (!Mount(false))
2533 return true;
2534 } else if (!Mount(Display_Error))
Dees_Troy5bf43922012-09-07 16:07:55 -04002535 return false;
Dees_Troy51127312012-09-08 13:08:49 -04002536
2537 ret = Get_Size_Via_statfs(Display_Error);
Dees_Troy0550cfb2012-10-13 11:56:13 -04002538 if (!ret || Size == 0) {
2539 if (!Get_Size_Via_df(Display_Error)) {
2540 if (!Was_Already_Mounted)
2541 UnMount(false);
Dees_Troy51127312012-09-08 13:08:49 -04002542 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04002543 }
2544 }
Dees_Troy51127312012-09-08 13:08:49 -04002545
Dees_Troy5bf43922012-09-07 16:07:55 -04002546 if (Has_Data_Media) {
2547 if (Mount(Display_Error)) {
Ethan Yonker3fdcda42016-11-30 12:29:37 -06002548 Used = backup_exclusions.Get_Folder_Size(Mount_Point);
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002549 Backup_Size = Used;
2550 int bak = (int)(Used / 1048576LLU);
Dees_Troy51127312012-09-08 13:08:49 -04002551 int fre = (int)(Free / 1048576LLU);
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002552 LOGINFO("Data backup size is %iMB, free: %iMB.\n", bak, fre);
Dees_Troy0550cfb2012-10-13 11:56:13 -04002553 } else {
2554 if (!Was_Already_Mounted)
2555 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04002556 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04002557 }
Dees_Troye58d5262012-09-21 12:27:57 -04002558 } else if (Has_Android_Secure) {
2559 if (Mount(Display_Error))
Ethan Yonker3fdcda42016-11-30 12:29:37 -06002560 Backup_Size = backup_exclusions.Get_Folder_Size(Backup_Path);
Dees_Troy0550cfb2012-10-13 11:56:13 -04002561 else {
2562 if (!Was_Already_Mounted)
2563 UnMount(false);
Dees_Troye58d5262012-09-21 12:27:57 -04002564 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04002565 }
Dees_Troy5bf43922012-09-07 16:07:55 -04002566 }
Dees_Troy0550cfb2012-10-13 11:56:13 -04002567 if (!Was_Already_Mounted)
2568 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04002569 return true;
Dees_Troy51127312012-09-08 13:08:49 -04002570}
Dees_Troy38bd7602012-09-14 13:33:53 -04002571
2572void TWPartition::Find_Actual_Block_Device(void) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002573 if (Is_Decrypted && !Decrypted_Block_Device.empty()) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002574 Actual_Block_Device = Decrypted_Block_Device;
Ethan Yonker1b190162016-12-05 15:25:19 -06002575 if (TWFunc::Path_Exists(Decrypted_Block_Device)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002576 Is_Present = true;
Ethan Yonker1b190162016-12-05 15:25:19 -06002577 return;
2578 }
2579 } else if (SlotSelect && TWFunc::Path_Exists(Primary_Block_Device + PartitionManager.Get_Active_Slot_Suffix())) {
2580 Actual_Block_Device = Primary_Block_Device + PartitionManager.Get_Active_Slot_Suffix();
2581 unlink(Primary_Block_Device.c_str());
2582 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
2583 Is_Present = true;
2584 return;
Dees_Troy43d8b002012-09-17 16:00:01 -04002585 } else if (TWFunc::Path_Exists(Primary_Block_Device)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002586 Is_Present = true;
2587 Actual_Block_Device = Primary_Block_Device;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002588 return;
2589 }
Ethan Yonker1b190162016-12-05 15:25:19 -06002590 if (!Alternate_Block_Device.empty() && TWFunc::Path_Exists(Alternate_Block_Device)) {
Dees_Troy3f04d032012-10-07 18:20:09 -04002591 Actual_Block_Device = Alternate_Block_Device;
Dees_Troy38bd7602012-09-14 13:33:53 -04002592 Is_Present = true;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002593 } else {
Dees_Troy38bd7602012-09-14 13:33:53 -04002594 Is_Present = false;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002595 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002596}
2597
2598void TWPartition::Recreate_Media_Folder(void) {
2599 string Command;
Ethan Yonker66a19492015-12-10 10:19:45 -06002600 string Media_Path = Mount_Point + "/media";
Dees_Troy38bd7602012-09-14 13:33:53 -04002601
Ethan Yonkerbd7492d2016-12-07 13:55:01 -06002602 if (Is_FBE) {
2603 LOGINFO("Not recreating media folder on FBE\n");
2604 return;
2605 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002606 if (!Mount(true)) {
Ethan Yonker66a19492015-12-10 10:19:45 -06002607 gui_msg(Msg(msg::kError, "recreate_folder_err=Unable to recreate {1} folder.")(Media_Path));
2608 } else if (!TWFunc::Path_Exists(Media_Path)) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002609 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Ethan Yonker66a19492015-12-10 10:19:45 -06002610 LOGINFO("Recreating %s folder.\n", Media_Path.c_str());
2611 mkdir(Media_Path.c_str(), 0770);
xiaolu9416f4f2015-06-04 08:22:23 +08002612 string Internal_path = DataManager::GetStrValue("tw_internal_path");
2613 if (!Internal_path.empty()) {
2614 LOGINFO("Recreating %s folder.\n", Internal_path.c_str());
2615 mkdir(Internal_path.c_str(), 0770);
2616 }
2617#ifdef TW_INTERNAL_STORAGE_PATH
2618 mkdir(EXPAND(TW_INTERNAL_STORAGE_PATH), 0770);
2619#endif
Matt Mower87413642017-01-17 21:14:46 -06002620
thata3d31fb2014-12-21 22:27:40 +01002621 // Afterwards, we will try to set the
2622 // default metadata that we were hopefully able to get during
2623 // early boot.
Ethan Yonker66a19492015-12-10 10:19:45 -06002624 tw_set_default_metadata(Media_Path.c_str());
xiaolu9416f4f2015-06-04 08:22:23 +08002625 if (!Internal_path.empty())
2626 tw_set_default_metadata(Internal_path.c_str());
Matt Mower87413642017-01-17 21:14:46 -06002627
Ethan Yonker5eac2222014-06-11 12:22:55 -05002628 // Toggle mount to ensure that "internal sdcard" gets mounted
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002629 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Ethan Yonker5eac2222014-06-11 12:22:55 -05002630 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Dees_Troy38bd7602012-09-14 13:33:53 -04002631 }
Dees_Troy43d8b002012-09-17 16:00:01 -04002632}
Dees_Troye58d5262012-09-21 12:27:57 -04002633
2634void TWPartition::Recreate_AndSec_Folder(void) {
Dees_Troye58d5262012-09-21 12:27:57 -04002635 if (!Has_Android_Secure)
2636 return;
Dees_Troy2673cec2013-04-02 20:22:16 +00002637 LOGINFO("Creating %s: %s\n", Backup_Display_Name.c_str(), Symlink_Path.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -04002638 if (!Mount(true)) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002639 gui_msg(Msg(msg::kError, "recreate_folder_err=Unable to recreate {1} folder.")(Backup_Name));
Dees_Troye58d5262012-09-21 12:27:57 -04002640 } else if (!TWFunc::Path_Exists(Symlink_Path)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00002641 LOGINFO("Recreating %s folder.\n", Backup_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002642 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05002643 mkdir(Symlink_Path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002644 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04002645 }
2646}
bigbiff7cb4c332014-11-26 20:36:07 -05002647
2648uint64_t TWPartition::Get_Max_FileSize() {
2649 uint64_t maxFileSize = 0;
2650 const uint64_t constGB = (uint64_t) 1024 * 1024 * 1024;
2651 const uint64_t constTB = (uint64_t) constGB * 1024;
2652 const uint64_t constPB = (uint64_t) constTB * 1024;
bigbiff0c532032014-12-21 13:41:26 -05002653 if (Current_File_System == "ext4")
2654 maxFileSize = 16 * constTB; //16 TB
2655 else if (Current_File_System == "vfat")
2656 maxFileSize = 4 * constGB; //4 GB
2657 else if (Current_File_System == "ntfs")
2658 maxFileSize = 256 * constTB; //256 TB
2659 else if (Current_File_System == "exfat")
2660 maxFileSize = 16 * constPB; //16 PB
2661 else if (Current_File_System == "ext3")
2662 maxFileSize = 2 * constTB; //2 TB
2663 else if (Current_File_System == "f2fs")
2664 maxFileSize = 3.94 * constTB; //3.94 TB
bigbiff7cb4c332014-11-26 20:36:07 -05002665 else
2666 maxFileSize = 100000000L;
2667 return maxFileSize - 1;
2668}
2669
bigbiffce8f83c2015-12-12 18:30:21 -05002670bool TWPartition::Flash_Image(PartitionSettings *part_settings) {
2671 string Restore_File_System, full_filename;
Ethan Yonker96af84a2015-01-05 14:58:36 -06002672
Ethan Yonkere080c1f2016-09-19 13:50:25 -05002673 full_filename = part_settings->Backup_Folder + "/" + Backup_FileName;
Ethan Yonker96af84a2015-01-05 14:58:36 -06002674
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002675 LOGINFO("Image filename is: %s\n", Backup_FileName.c_str());
bigbiffce8f83c2015-12-12 18:30:21 -05002676
2677 if (Backup_Method == BM_FILES) {
Ethan Yonker96af84a2015-01-05 14:58:36 -06002678 LOGERR("Cannot flash images to file systems\n");
2679 return false;
2680 } else if (!Can_Flash_Img) {
2681 LOGERR("Cannot flash images to partitions %s\n", Display_Name.c_str());
2682 return false;
2683 } else {
2684 if (!Find_Partition_Size()) {
2685 LOGERR("Unable to find partition size for '%s'\n", Mount_Point.c_str());
2686 return false;
2687 }
bigbiffce8f83c2015-12-12 18:30:21 -05002688 unsigned long long image_size = TWFunc::Get_File_Size(full_filename);
Ethan Yonker96af84a2015-01-05 14:58:36 -06002689 if (image_size > Size) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002690 LOGINFO("Size (%llu bytes) of image '%s' is larger than target device '%s' (%llu bytes)\n",
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002691 image_size, Backup_FileName.c_str(), Actual_Block_Device.c_str(), Size);
Ethan Yonker74db1572015-10-28 12:44:49 -05002692 gui_err("img_size_err=Size of image is larger than target device");
Ethan Yonker96af84a2015-01-05 14:58:36 -06002693 return false;
2694 }
bigbiffce8f83c2015-12-12 18:30:21 -05002695 if (Backup_Method == BM_DD) {
2696 if (!part_settings->adbbackup) {
2697 if (Is_Sparse_Image(full_filename)) {
2698 return Flash_Sparse_Image(full_filename);
2699 }
Ethan Yonker472f5062016-02-25 13:47:30 -06002700 }
bigbiffce8f83c2015-12-12 18:30:21 -05002701 return Raw_Read_Write(part_settings);
2702 } else if (Backup_Method == BM_FLASH_UTILS) {
2703 return Flash_Image_FI(full_filename, NULL);
Ethan Yonker472f5062016-02-25 13:47:30 -06002704 }
Ethan Yonker96af84a2015-01-05 14:58:36 -06002705 }
2706
2707 LOGERR("Unknown flash method for '%s'\n", Mount_Point.c_str());
2708 return false;
2709}
2710
Ethan Yonker472f5062016-02-25 13:47:30 -06002711bool TWPartition::Is_Sparse_Image(const string& Filename) {
HashBanged974bb2016-01-30 14:20:09 -05002712 uint32_t magic = 0;
2713 int fd = open(Filename.c_str(), O_RDONLY);
2714 if (fd < 0) {
2715 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Filename)(strerror(errno)));
2716 return false;
2717 }
bigbiffce8f83c2015-12-12 18:30:21 -05002718
HashBanged974bb2016-01-30 14:20:09 -05002719 if (read(fd, &magic, sizeof(magic)) != sizeof(magic)) {
2720 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Filename)(strerror(errno)));
2721 close(fd);
2722 return false;
2723 }
2724 close(fd);
Ethan Yonker472f5062016-02-25 13:47:30 -06002725 if (magic == SPARSE_HEADER_MAGIC)
2726 return true;
2727 return false;
2728}
2729
2730bool TWPartition::Flash_Sparse_Image(const string& Filename) {
2731 string Command;
2732
2733 gui_msg(Msg("flashing=Flashing {1}...")(Display_Name));
2734
2735 Command = "simg2img '" + Filename + "' '" + Actual_Block_Device + "'";
Ethan Yonker96af84a2015-01-05 14:58:36 -06002736 LOGINFO("Flash command: '%s'\n", Command.c_str());
2737 TWFunc::Exec_Cmd(Command);
2738 return true;
2739}
2740
Ethan Yonker472f5062016-02-25 13:47:30 -06002741bool TWPartition::Flash_Image_FI(const string& Filename, ProgressTracking *progress) {
Ethan Yonker96af84a2015-01-05 14:58:36 -06002742 string Command;
Ethan Yonker472f5062016-02-25 13:47:30 -06002743 unsigned long long file_size;
Ethan Yonker96af84a2015-01-05 14:58:36 -06002744
Ethan Yonker74db1572015-10-28 12:44:49 -05002745 gui_msg(Msg("flashing=Flashing {1}...")(Display_Name));
Ethan Yonker472f5062016-02-25 13:47:30 -06002746 if (progress) {
2747 file_size = (unsigned long long)(TWFunc::Get_File_Size(Filename));
2748 progress->SetPartitionSize(file_size);
2749 }
Ethan Yonker96af84a2015-01-05 14:58:36 -06002750 // Sometimes flash image doesn't like to flash due to the first 2KB matching, so we erase first to ensure that it flashes
2751 Command = "erase_image " + MTD_Name;
2752 LOGINFO("Erase command: '%s'\n", Command.c_str());
2753 TWFunc::Exec_Cmd(Command);
2754 Command = "flash_image " + MTD_Name + " '" + Filename + "'";
2755 LOGINFO("Flash command: '%s'\n", Command.c_str());
2756 TWFunc::Exec_Cmd(Command);
Ethan Yonker472f5062016-02-25 13:47:30 -06002757 if (progress)
2758 progress->UpdateSize(file_size);
Ethan Yonker96af84a2015-01-05 14:58:36 -06002759 return true;
2760}
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05002761
2762void TWPartition::Change_Mount_Read_Only(bool new_value) {
2763 Mount_Read_Only = new_value;
2764}
2765
bigbiffce8f83c2015-12-12 18:30:21 -05002766bool TWPartition::Is_Read_Only() {
2767 return Mount_Read_Only;
2768}
2769
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05002770int TWPartition::Check_Lifetime_Writes() {
2771 bool original_read_only = Mount_Read_Only;
2772 int ret = 1;
2773
2774 Mount_Read_Only = true;
2775 if (Mount(false)) {
2776 Find_Actual_Block_Device();
2777 string block = basename(Actual_Block_Device.c_str());
2778 string file = "/sys/fs/" + Current_File_System + "/" + block + "/lifetime_write_kbytes";
2779 string result;
2780 if (TWFunc::Path_Exists(file)) {
2781 if (TWFunc::read_file(file, result) != 0) {
2782 LOGINFO("Check_Lifetime_Writes of '%s' failed to read_file\n", file.c_str());
2783 } else {
2784 LOGINFO("Check_Lifetime_Writes result: '%s'\n", result.c_str());
2785 if (result == "0") {
2786 ret = 0;
2787 }
2788 }
2789 } else {
2790 LOGINFO("Check_Lifetime_Writes file does not exist '%s'\n", file.c_str());
2791 }
2792 UnMount(true);
2793 } else {
2794 LOGINFO("Check_Lifetime_Writes failed to mount '%s'\n", Mount_Point.c_str());
2795 }
2796 Mount_Read_Only = original_read_only;
2797 return ret;
2798}
Ethan Yonker66a19492015-12-10 10:19:45 -06002799
2800int TWPartition::Decrypt_Adopted() {
2801#ifdef TW_INCLUDE_CRYPTO
2802 int ret = 1;
2803 Is_Adopted_Storage = false;
2804 string Adopted_Key_File = "";
2805
2806 if (!Removable)
2807 return ret;
2808
2809 int fd = open(Alternate_Block_Device.c_str(), O_RDONLY);
2810 if (fd < 0) {
2811 LOGINFO("failed to open '%s'\n", Alternate_Block_Device.c_str());
2812 return ret;
2813 }
2814 char type_guid[80];
2815 char part_guid[80];
2816
2817 if (gpt_disk_get_partition_info(fd, 2, type_guid, part_guid) == 0) {
2818 LOGINFO("type: '%s'\n", type_guid);
2819 LOGINFO("part: '%s'\n", part_guid);
2820 Adopted_GUID = part_guid;
2821 LOGINFO("Adopted_GUID '%s'\n", Adopted_GUID.c_str());
2822 if (strcmp(type_guid, TWGptAndroidExpand) == 0) {
2823 LOGINFO("android_expand found\n");
2824 Adopted_Key_File = "/data/misc/vold/expand_";
2825 Adopted_Key_File += part_guid;
2826 Adopted_Key_File += ".key";
2827 if (TWFunc::Path_Exists(Adopted_Key_File)) {
2828 Is_Adopted_Storage = true;
2829 /* Until we find a use case for this, I think it is safe
2830 * to disable USB Mass Storage whenever adopted storage
2831 * is present.
2832 */
2833 LOGINFO("Detected adopted storage, disabling USB mass storage mode\n");
2834 DataManager::SetValue("tw_has_usb_storage", 0);
2835 }
2836 }
2837 }
2838
2839 if (Is_Adopted_Storage) {
2840 string Adopted_Block_Device = Alternate_Block_Device + "p2";
2841 if (!TWFunc::Path_Exists(Adopted_Block_Device)) {
2842 Adopted_Block_Device = Alternate_Block_Device + "2";
2843 if (!TWFunc::Path_Exists(Adopted_Block_Device)) {
2844 LOGINFO("Adopted block device does not exist\n");
2845 goto exit;
2846 }
2847 }
2848 LOGINFO("key file is '%s', block device '%s'\n", Adopted_Key_File.c_str(), Adopted_Block_Device.c_str());
2849 char crypto_blkdev[MAXPATHLEN];
2850 std::string thekey;
2851 int fdkey = open(Adopted_Key_File.c_str(), O_RDONLY);
2852 if (fdkey < 0) {
2853 LOGINFO("failed to open key file\n");
2854 goto exit;
2855 }
2856 char buf[512];
2857 ssize_t n;
2858 while ((n = read(fdkey, &buf[0], sizeof(buf))) > 0) {
2859 thekey.append(buf, n);
2860 }
2861 close(fdkey);
2862 unsigned char* key = (unsigned char*) thekey.data();
2863 cryptfs_revert_ext_volume(part_guid);
2864
2865 ret = cryptfs_setup_ext_volume(part_guid, Adopted_Block_Device.c_str(), key, thekey.size(), crypto_blkdev);
2866 if (ret == 0) {
2867 LOGINFO("adopted storage new block device: '%s'\n", crypto_blkdev);
2868 Decrypted_Block_Device = crypto_blkdev;
2869 Is_Decrypted = true;
2870 Is_Encrypted = true;
2871 Find_Actual_Block_Device();
Kjell Braden3126a112016-06-19 16:58:15 +00002872 if (!Mount_Storage_Retry(false)) {
Ethan Yonker66a19492015-12-10 10:19:45 -06002873 LOGERR("Failed to mount decrypted adopted storage device\n");
2874 Is_Decrypted = false;
2875 Is_Encrypted = false;
2876 cryptfs_revert_ext_volume(part_guid);
2877 ret = 1;
2878 } else {
Ethan Yonkerfcf3f242016-02-16 12:30:26 -06002879 UnMount(false);
2880 Has_Android_Secure = false;
2881 Symlink_Path = "";
2882 Symlink_Mount_Point = "";
2883 Backup_Name = Mount_Point.substr(1);
2884 Backup_Path = Mount_Point;
2885 TWPartition* sdext = PartitionManager.Find_Partition_By_Path("/sd-ext");
2886 if (sdext && sdext->Actual_Block_Device == Adopted_Block_Device) {
2887 LOGINFO("Removing /sd-ext from partition list due to adopted storage\n");
2888 PartitionManager.Remove_Partition_By_Path("/sd-ext");
2889 }
Ethan Yonker66a19492015-12-10 10:19:45 -06002890 Setup_Data_Media();
2891 Recreate_Media_Folder();
2892 Wipe_Available_in_GUI = true;
2893 Wipe_During_Factory_Reset = true;
2894 Can_Be_Backed_Up = true;
2895 Can_Encrypt_Backup = true;
2896 Use_Userdata_Encryption = true;
2897 Is_Storage = true;
2898 Storage_Name = "Adopted Storage";
2899 Is_SubPartition = true;
2900 SubPartition_Of = "/data";
2901 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
2902 DataManager::SetValue("tw_has_adopted_storage", 1);
2903 }
2904 } else {
2905 LOGERR("Failed to setup adopted storage decryption\n");
2906 }
2907 }
2908exit:
2909 return ret;
2910#else
2911 LOGINFO("Decrypt_Adopted: no crypto support\n");
2912 return 1;
2913#endif
2914}
2915
2916void TWPartition::Revert_Adopted() {
2917#ifdef TW_INCLUDE_CRYPTO
2918 if (!Adopted_GUID.empty()) {
2919 PartitionManager.Remove_MTP_Storage(Mount_Point);
2920 UnMount(false);
2921 cryptfs_revert_ext_volume(Adopted_GUID.c_str());
2922 Is_Adopted_Storage = false;
2923 Is_Encrypted = false;
2924 Is_Decrypted = false;
2925 Decrypted_Block_Device = "";
2926 Find_Actual_Block_Device();
2927 Wipe_During_Factory_Reset = false;
2928 Can_Be_Backed_Up = false;
2929 Can_Encrypt_Backup = false;
2930 Use_Userdata_Encryption = false;
2931 Is_SubPartition = false;
2932 SubPartition_Of = "";
2933 Has_Data_Media = false;
2934 Storage_Path = Mount_Point;
2935 if (!Symlink_Mount_Point.empty()) {
2936 TWPartition* Dat = PartitionManager.Find_Partition_By_Path("/data");
2937 if (Dat) {
2938 Dat->UnMount(false);
2939 Dat->Symlink_Mount_Point = Symlink_Mount_Point;
2940 }
2941 Symlink_Mount_Point = "";
2942 }
2943 }
2944#else
2945 LOGINFO("Revert_Adopted: no crypto support\n");
2946#endif
2947}