blob: 44403d845a09f755da4c030b527b479e9c88c6d8 [file] [log] [blame]
Dees Troy3be70a82013-10-22 14:25:12 +00001/*
bigbiff bigbiff56cf5642016-08-19 17:43:45 -04002 Copyright 2013 to 2017 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 bigbiff9c754052013-01-09 09:09:08 -050044#include "twrpTar.hpp"
Ethan Yonker3fdcda42016-11-30 12:29:37 -060045#include "exclude.hpp"
Ethan Yonker1b7a31b2014-07-03 15:09:22 -050046#include "infomanager.hpp"
Ethan Yonker4b94cfd2014-12-11 10:00:45 -060047#include "set_metadata.h"
Ethan Yonker74db1572015-10-28 12:44:49 -050048#include "gui/gui.hpp"
bigbiffce8f83c2015-12-12 18:30:21 -050049#include "adbbu/libtwadbbu.hpp"
Dees_Troy5bf43922012-09-07 16:07:55 -040050extern "C" {
Dees_Troy38bd7602012-09-14 13:33:53 -040051 #include "mtdutils/mtdutils.h"
52 #include "mtdutils/mounts.h"
Dees_Troya95f55c2013-08-17 13:14:43 +000053#ifdef USE_EXT4
Ethan Yonker8373cfe2017-09-08 06:50:54 -050054 // #include "make_ext4fs.h" TODO need ifdef for android8
55 #include <ext4_utils/make_ext4fs.h>
Dees_Troya95f55c2013-08-17 13:14:43 +000056#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 Yonker6355b562017-05-01 09:42:17 -0500914 wipe_exclusions.add_absolute_dir(Mount_Point + "/misc/vold"); // adopted storage keys
Ethan Yonker79f88bd2016-12-09 14:52:12 -0600915 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
Matt Mowera8e6d832017-02-14 20:20:59 -06001321 if (Is_Storage && MTP_Storage_ID > 0)
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
bigbiffce8f83c2015-12-12 18:30:21 -05001660bool TWPartition::Restore(PartitionSettings *part_settings) {
Matt Mower3c366972015-12-25 19:28:31 -06001661 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, gui_parse_text("{@restoring_hdr}"));
Ethan Yonkere080c1f2016-09-19 13:50:25 -05001662 LOGINFO("Restore filename is: %s/%s\n", part_settings->Backup_Folder.c_str(), Backup_FileName.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001663
bigbiffce8f83c2015-12-12 18:30:21 -05001664 string Restore_File_System = Get_Restore_File_System(part_settings);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001665
1666 if (Is_File_System(Restore_File_System))
bigbiffce8f83c2015-12-12 18:30:21 -05001667 return Restore_Tar(part_settings);
1668 else if (Is_Image(Restore_File_System))
1669 return Restore_Image(part_settings);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001670
1671 LOGERR("Unknown restore method for '%s'\n", Mount_Point.c_str());
1672 return false;
1673}
1674
bigbiffce8f83c2015-12-12 18:30:21 -05001675string TWPartition::Get_Restore_File_System(PartitionSettings *part_settings) {
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001676 size_t first_period, second_period;
1677 string Restore_File_System;
1678
Gary Peck43acadf2012-11-21 21:19:01 -08001679 // Parse backup filename to extract the file system before wiping
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05001680 first_period = Backup_FileName.find(".");
Gary Peck43acadf2012-11-21 21:19:01 -08001681 if (first_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001682 LOGERR("Unable to find file system (first period).\n");
thatd43bf2d2014-09-21 23:13:02 +02001683 return string();
Gary Peck43acadf2012-11-21 21:19:01 -08001684 }
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05001685 Restore_File_System = Backup_FileName.substr(first_period + 1, Backup_FileName.size() - first_period - 1);
Gary Peck43acadf2012-11-21 21:19:01 -08001686 second_period = Restore_File_System.find(".");
1687 if (second_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001688 LOGERR("Unable to find file system (second period).\n");
thatd43bf2d2014-09-21 23:13:02 +02001689 return string();
Gary Peck43acadf2012-11-21 21:19:01 -08001690 }
1691 Restore_File_System.resize(second_period);
Matt Mowera0cd91d2016-12-30 18:21:42 -06001692 LOGINFO("Restore file system is: '%s'.\n", Restore_File_System.c_str());
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001693 return Restore_File_System;
Dees_Troy51a0e822012-09-05 15:24:24 -04001694}
1695
1696string TWPartition::Backup_Method_By_Name() {
bigbiffce8f83c2015-12-12 18:30:21 -05001697 if (Backup_Method == BM_NONE)
Dees_Troy38bd7602012-09-14 13:33:53 -04001698 return "none";
bigbiffce8f83c2015-12-12 18:30:21 -05001699 else if (Backup_Method == BM_FILES)
Dees_Troy38bd7602012-09-14 13:33:53 -04001700 return "files";
bigbiffce8f83c2015-12-12 18:30:21 -05001701 else if (Backup_Method == BM_DD)
Dees_Troy38bd7602012-09-14 13:33:53 -04001702 return "dd";
bigbiffce8f83c2015-12-12 18:30:21 -05001703 else if (Backup_Method == BM_FLASH_UTILS)
Dees_Troy38bd7602012-09-14 13:33:53 -04001704 return "flash_utils";
1705 else
1706 return "undefined";
1707 return "ERROR!";
Dees_Troy51a0e822012-09-05 15:24:24 -04001708}
1709
1710bool TWPartition::Decrypt(string Password) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001711 LOGINFO("STUB TWPartition::Decrypt, password: '%s'\n", Password.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001712 // Is this needed?
Dees_Troy51a0e822012-09-05 15:24:24 -04001713 return 1;
1714}
1715
1716bool TWPartition::Wipe_Encryption() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001717 bool Save_Data_Media = Has_Data_Media;
1718
1719 if (!UnMount(true))
1720 return false;
1721
Dees_Troy38bd7602012-09-14 13:33:53 -04001722 Has_Data_Media = false;
Dees_Troy74fb2e92013-04-15 14:35:47 +00001723 Decrypted_Block_Device = "";
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001724#ifdef TW_INCLUDE_CRYPTO
Ethan Yonker1b190162016-12-05 15:25:19 -06001725 if (Is_Decrypted && !Decrypted_Block_Device.empty()) {
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001726 if (!UnMount(true))
1727 return false;
Matt Mower2b18a532015-02-20 16:58:05 -06001728 if (delete_crypto_blk_dev((char*)("userdata")) != 0) {
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001729 LOGERR("Error deleting crypto block device, continuing anyway.\n");
1730 }
1731 }
1732#endif
Dees_Troy74fb2e92013-04-15 14:35:47 +00001733 Is_Decrypted = false;
1734 Is_Encrypted = false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001735 Find_Actual_Block_Device();
Ethan Yonkerbc85b632015-08-09 12:48:14 -05001736 if (Crypto_Key_Location == "footer") {
1737 int newlen, fd;
1738 if (Length != 0) {
1739 newlen = Length;
1740 if (newlen < 0)
1741 newlen = newlen * -1;
1742 } else {
1743 newlen = CRYPT_FOOTER_OFFSET;
1744 }
1745 if ((fd = open(Actual_Block_Device.c_str(), O_RDWR)) < 0) {
1746 gui_print_color("warning", "Unable to open '%s' to wipe crypto key\n", Actual_Block_Device.c_str());
1747 } else {
1748 unsigned int block_count;
1749 if ((ioctl(fd, BLKGETSIZE, &block_count)) == -1) {
1750 gui_print_color("warning", "Unable to get block size for wiping crypto footer.\n");
1751 } else {
1752 off64_t offset = ((off64_t)block_count * 512) - newlen;
1753 if (lseek64(fd, offset, SEEK_SET) == -1) {
1754 gui_print_color("warning", "Unable to lseek64 for wiping crypto footer.\n");
1755 } else {
1756 void* buffer = malloc(newlen);
1757 if (!buffer) {
1758 gui_print_color("warning", "Failed to malloc for wiping crypto footer.\n");
1759 } else {
1760 memset(buffer, 0, newlen);
1761 int ret = write(fd, buffer, newlen);
1762 if (ret != newlen) {
1763 gui_print_color("warning", "Failed to wipe crypto footer.\n");
1764 } else {
1765 LOGINFO("Successfully wiped crypto footer.\n");
1766 }
Matt Mower13a8f0b2015-09-26 15:40:03 -05001767 free(buffer);
Ethan Yonkerbc85b632015-08-09 12:48:14 -05001768 }
1769 }
1770 }
1771 close(fd);
1772 }
1773 } else {
Ethan Yonkerc2dafbb2016-03-15 22:20:59 -05001774 if (TWFunc::IOCTL_Get_Block_Size(Crypto_Key_Location.c_str()) >= 16384LLU) {
1775 string Command = "dd of='" + Crypto_Key_Location + "' if=/dev/zero bs=16384 count=1";
1776 TWFunc::Exec_Cmd(Command);
1777 } else {
1778 LOGINFO("Crypto key location reports size < 16K so not wiping crypto footer.\n");
1779 }
Ethan Yonkerbc85b632015-08-09 12:48:14 -05001780 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001781 if (Wipe(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001782 Has_Data_Media = Save_Data_Media;
1783 if (Has_Data_Media && !Symlink_Mount_Point.empty()) {
1784 Recreate_Media_Folder();
Ethan Yonker726a0202014-12-16 20:01:38 -06001785 if (Mount(false))
1786 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04001787 }
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001788 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
Ethan Yonker83e82572014-04-04 10:59:28 -05001789#ifndef TW_OEM_BUILD
Ethan Yonker74db1572015-10-28 12:44:49 -05001790 gui_msg("format_data_msg=You may need to reboot recovery to be able to use /data again.");
Ethan Yonker83e82572014-04-04 10:59:28 -05001791#endif
Dees_Troy38bd7602012-09-14 13:33:53 -04001792 return true;
1793 } else {
1794 Has_Data_Media = Save_Data_Media;
Ethan Yonker74db1572015-10-28 12:44:49 -05001795 gui_err("format_data_err=Unable to format to remove encryption.");
Ethan Yonker726a0202014-12-16 20:01:38 -06001796 if (Has_Data_Media && Mount(false))
1797 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04001798 return false;
1799 }
1800 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001801}
1802
1803void TWPartition::Check_FS_Type() {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001804 const char* type;
1805 blkid_probe pr;
Dees_Troy5bf43922012-09-07 16:07:55 -04001806
Dees_Troy68cab492012-12-12 19:29:35 +00001807 if (Fstab_File_System == "yaffs2" || Fstab_File_System == "mtd" || Fstab_File_System == "bml" || Ignore_Blkid)
1808 return; // Running blkid on some mtd devices causes a massive crash or needs to be skipped
Dees_Troy5bf43922012-09-07 16:07:55 -04001809
Dees_Troy38bd7602012-09-14 13:33:53 -04001810 Find_Actual_Block_Device();
Dees_Troy8170a922012-09-18 15:40:25 -04001811 if (!Is_Present)
1812 return;
Dees_Troy51127312012-09-08 13:08:49 -04001813
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001814 pr = blkid_new_probe_from_filename(Actual_Block_Device.c_str());
1815 if (blkid_do_fullprobe(pr)) {
1816 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00001817 LOGINFO("Can't probe device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001818 return;
Dees_Troy5bf43922012-09-07 16:07:55 -04001819 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001820
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05001821 if (blkid_probe_lookup_value(pr, "TYPE", &type, NULL) < 0) {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001822 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00001823 LOGINFO("can't find filesystem on device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001824 return;
1825 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001826
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001827 Current_File_System = type;
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001828 blkid_free_probe(pr);
Dees_Troy51a0e822012-09-05 15:24:24 -04001829}
1830
Gary Peck43acadf2012-11-21 21:19:01 -08001831bool TWPartition::Wipe_EXT23(string File_System) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001832 if (!UnMount(true))
1833 return false;
1834
Dees_Troy43d8b002012-09-17 16:00:01 -04001835 if (TWFunc::Path_Exists("/sbin/mke2fs")) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001836 string command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001837
Greg Wallace4b44fef2015-12-29 15:33:24 -05001838 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mke2fs"));
Dees_Troy38bd7602012-09-14 13:33:53 -04001839 Find_Actual_Block_Device();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001840 command = "mke2fs -t " + File_System + " -m 0 " + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001841 LOGINFO("mke2fs command: %s\n", command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001842 if (TWFunc::Exec_Cmd(command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001843 Current_File_System = File_System;
Dees_Troye58d5262012-09-21 12:27:57 -04001844 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05001845 gui_msg("done=Done.");
Dees_Troy38bd7602012-09-14 13:33:53 -04001846 return true;
1847 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001848 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04001849 return false;
1850 }
1851 } else
1852 return Wipe_RMRF();
1853
1854 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001855}
1856
1857bool TWPartition::Wipe_EXT4() {
Ethan Yonker25f20c12014-10-14 09:04:43 -05001858 Find_Actual_Block_Device();
1859 if (!Is_Present) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001860 LOGINFO("Block device not present, cannot wipe %s.\n", Display_Name.c_str());
1861 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Ethan Yonker25f20c12014-10-14 09:04:43 -05001862 return false;
1863 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001864 if (!UnMount(true))
1865 return false;
1866
Matt Mower87413642017-01-17 21:14:46 -06001867#if defined(USE_EXT4)
Ethan Yonkerf27497f2014-02-09 11:48:33 -06001868 int ret;
1869 char *secontext = NULL;
1870
Greg Wallace4b44fef2015-12-29 15:33:24 -05001871 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("make_ext4fs"));
Ethan Yonkerf27497f2014-02-09 11:48:33 -06001872
Dees Troy99c8dbf2014-03-10 16:53:58 +00001873 if (!selinux_handle || selabel_lookup(selinux_handle, &secontext, Mount_Point.c_str(), S_IFDIR) < 0) {
Ethan Yonkerf27497f2014-02-09 11:48:33 -06001874 LOGINFO("Cannot lookup security context for '%s'\n", Mount_Point.c_str());
1875 ret = make_ext4fs(Actual_Block_Device.c_str(), Length, Mount_Point.c_str(), NULL);
1876 } else {
1877 ret = make_ext4fs(Actual_Block_Device.c_str(), Length, Mount_Point.c_str(), selinux_handle);
1878 }
1879 if (ret != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001880 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troya95f55c2013-08-17 13:14:43 +00001881 return false;
1882 } else {
bigbiff bigbiffc49d7062013-10-11 20:28:00 -04001883 string sedir = Mount_Point + "/lost+found";
1884 PartitionManager.Mount_By_Path(sedir.c_str(), true);
1885 rmdir(sedir.c_str());
1886 mkdir(sedir.c_str(), S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP);
Dees_Troya95f55c2013-08-17 13:14:43 +00001887 return true;
1888 }
1889#else
Dees_Troy43d8b002012-09-17 16:00:01 -04001890 if (TWFunc::Path_Exists("/sbin/make_ext4fs")) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001891 string Command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001892
Greg Wallace4b44fef2015-12-29 15:33:24 -05001893 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("make_ext4fs"));
Dees_Troy38bd7602012-09-14 13:33:53 -04001894 Find_Actual_Block_Device();
1895 Command = "make_ext4fs";
1896 if (!Is_Decrypted && Length != 0) {
1897 // Only use length if we're not decrypted
1898 char len[32];
1899 sprintf(len, "%i", Length);
1900 Command += " -l ";
1901 Command += len;
1902 }
Dees_Troy5295d582013-09-06 15:51:08 +00001903 if (TWFunc::Path_Exists("/file_contexts")) {
1904 Command += " -S /file_contexts";
1905 }
1906 Command += " -a " + Mount_Point + " " + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001907 LOGINFO("make_ext4fs command: %s\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001908 if (TWFunc::Exec_Cmd(Command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001909 Current_File_System = "ext4";
Dees_Troye58d5262012-09-21 12:27:57 -04001910 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05001911 gui_msg("done=Done.");
Dees_Troy38bd7602012-09-14 13:33:53 -04001912 return true;
1913 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001914 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04001915 return false;
1916 }
1917 } else
Gary Peck43acadf2012-11-21 21:19:01 -08001918 return Wipe_EXT23("ext4");
Dees_Troya95f55c2013-08-17 13:14:43 +00001919#endif
Dees_Troy38bd7602012-09-14 13:33:53 -04001920 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001921}
1922
1923bool TWPartition::Wipe_FAT() {
Vojtech Bocek05534202013-09-11 08:11:56 +02001924 string command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001925
Matt Mower18794c82015-11-11 16:22:45 -06001926 if (TWFunc::Path_Exists("/sbin/mkfs.fat")) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001927 if (!UnMount(true))
1928 return false;
1929
Greg Wallace4b44fef2015-12-29 15:33:24 -05001930 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mkfs.fat"));
Dees_Troy38bd7602012-09-14 13:33:53 -04001931 Find_Actual_Block_Device();
Matt Mower18794c82015-11-11 16:22:45 -06001932 command = "mkfs.fat " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02001933 if (TWFunc::Exec_Cmd(command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001934 Current_File_System = "vfat";
Dees_Troye58d5262012-09-21 12:27:57 -04001935 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05001936 gui_msg("done=Done.");
Dees_Troy38bd7602012-09-14 13:33:53 -04001937 return true;
1938 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001939 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04001940 return false;
1941 }
1942 return true;
1943 }
1944 else
1945 return Wipe_RMRF();
1946
1947 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001948}
1949
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001950bool TWPartition::Wipe_EXFAT() {
Vojtech Bocek05534202013-09-11 08:11:56 +02001951 string command;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001952
1953 if (TWFunc::Path_Exists("/sbin/mkexfatfs")) {
1954 if (!UnMount(true))
1955 return false;
1956
Greg Wallace4b44fef2015-12-29 15:33:24 -05001957 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mkexfatfs"));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001958 Find_Actual_Block_Device();
1959 command = "mkexfatfs " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02001960 if (TWFunc::Exec_Cmd(command) == 0) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001961 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05001962 gui_msg("done=Done.");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001963 return true;
1964 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001965 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001966 return false;
1967 }
1968 return true;
1969 }
1970 return false;
1971}
1972
Dees_Troy38bd7602012-09-14 13:33:53 -04001973bool TWPartition::Wipe_MTD() {
1974 if (!UnMount(true))
1975 return false;
1976
Greg Wallace4b44fef2015-12-29 15:33:24 -05001977 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("MTD"));
Dees_Troy38bd7602012-09-14 13:33:53 -04001978
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001979 mtd_scan_partitions();
1980 const MtdPartition* mtd = mtd_find_partition_by_name(MTD_Name.c_str());
1981 if (mtd == NULL) {
1982 LOGERR("No mtd partition named '%s'", MTD_Name.c_str());
1983 return false;
1984 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001985
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001986 MtdWriteContext* ctx = mtd_write_partition(mtd);
1987 if (ctx == NULL) {
1988 LOGERR("Can't write '%s', failed to format.", MTD_Name.c_str());
1989 return false;
1990 }
1991 if (mtd_erase_blocks(ctx, -1) == -1) {
1992 mtd_write_close(ctx);
1993 LOGERR("Failed to format '%s'", MTD_Name.c_str());
1994 return false;
1995 }
1996 if (mtd_write_close(ctx) != 0) {
1997 LOGERR("Failed to close '%s'", MTD_Name.c_str());
1998 return false;
1999 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08002000 Current_File_System = "yaffs2";
Dees_Troye58d5262012-09-21 12:27:57 -04002001 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05002002 gui_msg("done=Done.");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02002003 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002004}
2005
2006bool TWPartition::Wipe_RMRF() {
Dees_Troy38bd7602012-09-14 13:33:53 -04002007 if (!Mount(true))
2008 return false;
Ethan Yonker726a0202014-12-16 20:01:38 -06002009 // This is the only wipe that leaves the partition mounted, so we
2010 // must manually remove the partition from MTP if it is a storage
2011 // partition.
2012 if (Is_Storage)
2013 PartitionManager.Remove_MTP_Storage(MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04002014
Ethan Yonker74db1572015-10-28 12:44:49 -05002015 gui_msg(Msg("remove_all=Removing all files under '{1}'")(Mount_Point));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002016 TWFunc::removeDir(Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04002017 Recreate_AndSec_Folder();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002018 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002019}
2020
Dees_Troye5017042013-08-29 16:38:55 +00002021bool TWPartition::Wipe_F2FS() {
Vojtech Bocek05534202013-09-11 08:11:56 +02002022 string command;
Dees_Troye5017042013-08-29 16:38:55 +00002023
2024 if (TWFunc::Path_Exists("/sbin/mkfs.f2fs")) {
2025 if (!UnMount(true))
2026 return false;
2027
Greg Wallace4b44fef2015-12-29 15:33:24 -05002028 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mkfs.f2fs"));
Dees_Troye5017042013-08-29 16:38:55 +00002029 Find_Actual_Block_Device();
Greg Wallace558aef72016-01-19 21:14:30 -05002030 command = "mkfs.f2fs -t 0";
dhacker29a3fa75f2015-01-17 19:42:33 -05002031 if (!Is_Decrypted && Length != 0) {
2032 // Only use length if we're not decrypted
2033 char len[32];
2034 int mod_length = Length;
2035 if (Length < 0)
2036 mod_length *= -1;
2037 sprintf(len, "%i", mod_length);
2038 command += " -r ";
2039 command += len;
2040 }
2041 command += " " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02002042 if (TWFunc::Exec_Cmd(command) == 0) {
Dees_Troye5017042013-08-29 16:38:55 +00002043 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05002044 gui_msg("done=Done.");
Dees_Troye5017042013-08-29 16:38:55 +00002045 return true;
2046 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002047 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troye5017042013-08-29 16:38:55 +00002048 return false;
2049 }
2050 return true;
2051 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002052 LOGINFO("mkfs.f2fs binary not found, using rm -rf to wipe.\n");
Dees_Troye5017042013-08-29 16:38:55 +00002053 return Wipe_RMRF();
2054 }
2055 return false;
2056}
2057
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002058bool TWPartition::Wipe_NTFS() {
2059 string command;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002060 string Ntfsmake_Binary;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002061
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002062 if (TWFunc::Path_Exists("/sbin/mkntfs"))
2063 Ntfsmake_Binary = "mkntfs";
2064 else if (TWFunc::Path_Exists("/sbin/mkfs.ntfs"))
2065 Ntfsmake_Binary = "mkfs.ntfs";
2066 else
2067 return false;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002068
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002069 if (!UnMount(true))
2070 return false;
2071
Greg Wallace4b44fef2015-12-29 15:33:24 -05002072 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)(Ntfsmake_Binary));
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002073 Find_Actual_Block_Device();
2074 command = "/sbin/" + Ntfsmake_Binary + " " + Actual_Block_Device;
2075 if (TWFunc::Exec_Cmd(command) == 0) {
2076 Recreate_AndSec_Folder();
2077 gui_msg("done=Done.");
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002078 return true;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002079 } else {
2080 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
2081 return false;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002082 }
2083 return false;
2084}
2085
Dees_Troy51a0e822012-09-05 15:24:24 -04002086bool TWPartition::Wipe_Data_Without_Wiping_Media() {
Ethan Yonker83e82572014-04-04 10:59:28 -05002087#ifdef TW_OEM_BUILD
2088 // In an OEM Build we want to do a full format
2089 return Wipe_Encryption();
2090#else
Ethan Yonker66a19492015-12-10 10:19:45 -06002091 bool ret = false;
Dees_Troy38bd7602012-09-14 13:33:53 -04002092
Dees_Troy38bd7602012-09-14 13:33:53 -04002093 if (!Mount(true))
2094 return false;
2095
Ethan Yonker74db1572015-10-28 12:44:49 -05002096 gui_msg("wiping_data=Wiping data without wiping /data/media ...");
Ethan Yonker66a19492015-12-10 10:19:45 -06002097 ret = Wipe_Data_Without_Wiping_Media_Func(Mount_Point + "/");
2098 if (ret)
2099 gui_msg("done=Done.");
2100 return ret;
2101#endif // ifdef TW_OEM_BUILD
2102}
2103
2104bool TWPartition::Wipe_Data_Without_Wiping_Media_Func(const string& parent __unused) {
2105 string dir;
Dees_Troy38bd7602012-09-14 13:33:53 -04002106
2107 DIR* d;
Ethan Yonker66a19492015-12-10 10:19:45 -06002108 d = opendir(parent.c_str());
Dees_Troy16b74352012-11-14 22:27:31 +00002109 if (d != NULL) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002110 struct dirent* de;
2111 while ((de = readdir(d)) != NULL) {
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002112 if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) continue;
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05002113
Ethan Yonker66a19492015-12-10 10:19:45 -06002114 dir = parent;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002115 dir.append(de->d_name);
Ethan Yonker3fdcda42016-11-30 12:29:37 -06002116 if (wipe_exclusions.check_skip_dirs(dir)) {
Ethan Yonker66a19492015-12-10 10:19:45 -06002117 LOGINFO("skipped '%s'\n", dir.c_str());
2118 continue;
2119 }
Dees_Troyce675462013-01-09 19:48:21 +00002120 if (de->d_type == DT_DIR) {
Ethan Yonker66a19492015-12-10 10:19:45 -06002121 dir.append("/");
2122 if (!Wipe_Data_Without_Wiping_Media_Func(dir)) {
2123 closedir(d);
2124 return false;
2125 }
2126 rmdir(dir.c_str());
bigbiff bigbiff98f1f902013-01-19 18:46:13 -05002127 } else if (de->d_type == DT_REG || de->d_type == DT_LNK || de->d_type == DT_FIFO || de->d_type == DT_SOCK) {
nkk719efbe762017-06-13 19:49:05 +03002128 if (unlink(dir.c_str()) != 0)
Ethan Yonker79f88bd2016-12-09 14:52:12 -06002129 LOGINFO("Unable to unlink '%s': %s\n", dir.c_str(), strerror(errno));
Dees_Troyce675462013-01-09 19:48:21 +00002130 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002131 }
2132 closedir(d);
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05002133
Dees_Troy16b74352012-11-14 22:27:31 +00002134 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -04002135 }
Ethan Yonker74db1572015-10-28 12:44:49 -05002136 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy16b74352012-11-14 22:27:31 +00002137 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04002138}
2139
bigbiffce8f83c2015-12-12 18:30:21 -05002140bool TWPartition::Backup_Tar(PartitionSettings *part_settings, pid_t *tar_fork_pid) {
Ethan Yonker472f5062016-02-25 13:47:30 -06002141 string Full_FileName;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002142 twrpTar tar;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05002143
Dees_Troy43d8b002012-09-17 16:00:01 -04002144 if (!Mount(true))
2145 return false;
2146
Dees_Troya13d74f2013-03-24 08:54:55 -05002147 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Backup_Display_Name, "Backing Up");
Ethan Yonker74db1572015-10-28 12:44:49 -05002148 gui_msg(Msg("backing_up=Backing up {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002149
Ethan Yonker472f5062016-02-25 13:47:30 -06002150 DataManager::GetValue(TW_USE_COMPRESSION_VAR, tar.use_compression);
Matt Mowerbb81e5d2014-03-20 18:05:41 -05002151
Dees_Troy83bd4832013-05-04 12:39:56 +00002152#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
Ethan Yonker472f5062016-02-25 13:47:30 -06002153 if (Can_Encrypt_Backup) {
2154 DataManager::GetValue("tw_encrypt_backup", tar.use_encryption);
2155 if (tar.use_encryption) {
2156 if (Use_Userdata_Encryption)
2157 tar.userdata_encryption = tar.use_encryption;
2158 string Password;
2159 DataManager::GetValue("tw_backup_password", Password);
2160 tar.setpassword(Password);
2161 } else {
2162 tar.use_encryption = 0;
2163 }
Dees_Troy83bd4832013-05-04 12:39:56 +00002164 }
2165#endif
Dees_Troy43d8b002012-09-17 16:00:01 -04002166
Ethan Yonker472f5062016-02-25 13:47:30 -06002167 Backup_FileName = Backup_Name + "." + Current_File_System + ".win";
Ethan Yonkere080c1f2016-09-19 13:50:25 -05002168 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
Ethan Yonker01f4e032017-02-03 15:30:52 -06002169 if (Has_Data_Media)
2170 gui_msg(Msg(msg::kWarning, "backup_storage_warning=Backups of {1} do not include any files in internal storage such as pictures or downloads.")(Display_Name));
bigbiffce8f83c2015-12-12 18:30:21 -05002171 tar.part_settings = part_settings;
Ethan Yonker3fdcda42016-11-30 12:29:37 -06002172 tar.backup_exclusions = &backup_exclusions;
Dees Troye0a433a2013-12-02 04:10:37 +00002173 tar.setdir(Backup_Path);
2174 tar.setfn(Full_FileName);
2175 tar.setsize(Backup_Size);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002176 tar.partition_name = Backup_Name;
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002177 tar.backup_folder = part_settings->Backup_Folder;
bigbiffce8f83c2015-12-12 18:30:21 -05002178 if (tar.createTarFork(tar_fork_pid) != 0)
Dees Troye0a433a2013-12-02 04:10:37 +00002179 return false;
Dees_Troy43d8b002012-09-17 16:00:01 -04002180 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002181}
2182
bigbiffce8f83c2015-12-12 18:30:21 -05002183bool TWPartition::Backup_Image(PartitionSettings *part_settings) {
2184 string Full_FileName, adb_file_name;
igoriok87e3d932013-01-31 21:03:53 +02002185
Ethan Yonker74db1572015-10-28 12:44:49 -05002186 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, gui_parse_text("{@backing}"));
2187 gui_msg(Msg("backing_up=Backing up {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002188
Ethan Yonker472f5062016-02-25 13:47:30 -06002189 Backup_FileName = Backup_Name + "." + Current_File_System + ".win";
Dees_Troy43d8b002012-09-17 16:00:01 -04002190
bigbiffce8f83c2015-12-12 18:30:21 -05002191 if (part_settings->adbbackup) {
2192 Full_FileName = TW_ADB_BACKUP;
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002193 adb_file_name = part_settings->Backup_Folder + "/" + Backup_FileName;
bigbiffce8f83c2015-12-12 18:30:21 -05002194 }
2195 else
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002196 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
bigbiffce8f83c2015-12-12 18:30:21 -05002197
2198 part_settings->total_restore_size = Backup_Size;
2199
2200 if (part_settings->adbbackup) {
2201 if (!twadbbu::Write_TWIMG(adb_file_name, Backup_Size))
2202 return false;
2203 }
2204
2205 if (!Raw_Read_Write(part_settings))
Ethan Yonker472f5062016-02-25 13:47:30 -06002206 return false;
2207
bigbiffce8f83c2015-12-12 18:30:21 -05002208 if (part_settings->adbbackup) {
2209 if (!twadbbu::Write_TWEOF())
2210 return false;
Dees_Troy7c2dec82012-09-26 09:49:14 -04002211 }
Dees_Troy43d8b002012-09-17 16:00:01 -04002212 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002213}
2214
bigbiffce8f83c2015-12-12 18:30:21 -05002215bool TWPartition::Raw_Read_Write(PartitionSettings *part_settings) {
2216 unsigned long long RW_Block_Size, Remain = Backup_Size;
2217 int src_fd = -1, dest_fd = -1;
2218 ssize_t bs;
Ethan Yonker472f5062016-02-25 13:47:30 -06002219 bool ret = false;
2220 void* buffer = NULL;
2221 unsigned long long backedup_size = 0;
bigbiffce8f83c2015-12-12 18:30:21 -05002222 string srcfn, destfn;
Ethan Yonker472f5062016-02-25 13:47:30 -06002223
bigbiffce8f83c2015-12-12 18:30:21 -05002224 if (part_settings->PM_Method == PM_BACKUP) {
2225 srcfn = Actual_Block_Device;
2226 if (part_settings->adbbackup)
2227 destfn = TW_ADB_BACKUP;
2228 else
Ethan Yonkere080c1f2016-09-19 13:50:25 -05002229 destfn = part_settings->Backup_Folder + "/" + Backup_FileName;
bigbiffce8f83c2015-12-12 18:30:21 -05002230 }
2231 else {
2232 destfn = Actual_Block_Device;
2233 if (part_settings->adbbackup) {
2234 srcfn = TW_ADB_RESTORE;
2235 } else {
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002236 srcfn = part_settings->Backup_Folder + "/" + Backup_FileName;
bigbiffce8f83c2015-12-12 18:30:21 -05002237 Remain = TWFunc::Get_File_Size(srcfn);
2238 }
2239 }
Ethan Yonker472f5062016-02-25 13:47:30 -06002240
bigbiffce8f83c2015-12-12 18:30:21 -05002241 src_fd = open(srcfn.c_str(), O_RDONLY | O_LARGEFILE);
Ethan Yonker472f5062016-02-25 13:47:30 -06002242 if (src_fd < 0) {
bigbiffce8f83c2015-12-12 18:30:21 -05002243 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(srcfn.c_str())(strerror(errno)));
Ethan Yonker472f5062016-02-25 13:47:30 -06002244 return false;
2245 }
bigbiffce8f83c2015-12-12 18:30:21 -05002246
2247 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 -06002248 if (dest_fd < 0) {
bigbiffce8f83c2015-12-12 18:30:21 -05002249 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(destfn.c_str())(strerror(errno)));
Ethan Yonker472f5062016-02-25 13:47:30 -06002250 goto exit;
2251 }
bigbiffce8f83c2015-12-12 18:30:21 -05002252
2253 LOGINFO("Reading '%s', writing '%s'\n", srcfn.c_str(), destfn.c_str());
2254
2255 if (part_settings->adbbackup) {
2256 RW_Block_Size = MAX_ADB_READ;
2257 bs = MAX_ADB_READ;
2258 }
2259 else {
2260 RW_Block_Size = 1048576LLU; // 1MB
2261 bs = (ssize_t)(RW_Block_Size);
2262 }
2263
Ethan Yonker472f5062016-02-25 13:47:30 -06002264 buffer = malloc((size_t)bs);
2265 if (!buffer) {
2266 LOGINFO("Raw_Read_Write failed to malloc\n");
2267 goto exit;
2268 }
bigbiffce8f83c2015-12-12 18:30:21 -05002269
2270 if (part_settings->progress)
2271 part_settings->progress->SetPartitionSize(part_settings->total_restore_size);
2272
Ethan Yonker472f5062016-02-25 13:47:30 -06002273 while (Remain > 0) {
2274 if (Remain < RW_Block_Size)
bigbiffce8f83c2015-12-12 18:30:21 -05002275 bs = (ssize_t)(Remain);
Matt Mowera8a89d12016-12-30 18:10:37 -06002276 if (read(src_fd, buffer, bs) != bs) {
Ethan Yonker472f5062016-02-25 13:47:30 -06002277 LOGINFO("Error reading source fd (%s)\n", strerror(errno));
2278 goto exit;
2279 }
2280 if (write(dest_fd, buffer, bs) != bs) {
2281 LOGINFO("Error writing destination fd (%s)\n", strerror(errno));
2282 goto exit;
2283 }
2284 backedup_size += (unsigned long long)(bs);
Matt Mower029a82d2017-01-08 13:12:38 -06002285 Remain -= (unsigned long long)(bs);
bigbiffce8f83c2015-12-12 18:30:21 -05002286 if (part_settings->progress)
2287 part_settings->progress->UpdateSize(backedup_size);
Ethan Yonker472f5062016-02-25 13:47:30 -06002288 if (PartitionManager.Check_Backup_Cancel() != 0)
2289 goto exit;
2290 }
bigbiffce8f83c2015-12-12 18:30:21 -05002291 if (part_settings->progress)
2292 part_settings->progress->UpdateDisplayDetails(true);
Ethan Yonker472f5062016-02-25 13:47:30 -06002293 fsync(dest_fd);
nailyka083dc62016-11-11 21:41:28 +01002294
2295 if (!part_settings->adbbackup && part_settings->PM_Method == PM_BACKUP) {
2296 tw_set_default_metadata(destfn.c_str());
2297 LOGINFO("Restored default metadata for %s\n", destfn.c_str());
2298 }
2299
Ethan Yonker472f5062016-02-25 13:47:30 -06002300 ret = true;
2301exit:
2302 if (src_fd >= 0)
2303 close(src_fd);
2304 if (dest_fd >= 0)
2305 close(dest_fd);
2306 if (buffer)
2307 free(buffer);
2308 return ret;
2309}
2310
bigbiffce8f83c2015-12-12 18:30:21 -05002311bool TWPartition::Backup_Dump_Image(PartitionSettings *part_settings) {
Vojtech Bocek05534202013-09-11 08:11:56 +02002312 string Full_FileName, Command;
Dees_Troy43d8b002012-09-17 16:00:01 -04002313
Ethan Yonker74db1572015-10-28 12:44:49 -05002314 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, gui_parse_text("{@backing}"));
2315 gui_msg(Msg("backing_up=Backing up {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002316
bigbiffce8f83c2015-12-12 18:30:21 -05002317 if (part_settings->progress)
2318 part_settings->progress->SetPartitionSize(Backup_Size);
Dees_Troy43d8b002012-09-17 16:00:01 -04002319
Ethan Yonker472f5062016-02-25 13:47:30 -06002320 Backup_FileName = Backup_Name + "." + Current_File_System + ".win";
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002321 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04002322
2323 Command = "dump_image " + MTD_Name + " '" + Full_FileName + "'";
bigbiffce8f83c2015-12-12 18:30:21 -05002324
Dees_Troy2673cec2013-04-02 20:22:16 +00002325 LOGINFO("Backup command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02002326 TWFunc::Exec_Cmd(Command);
Ethan Yonker4b94cfd2014-12-11 10:00:45 -06002327 tw_set_default_metadata(Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04002328 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
2329 // 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 -06002330 gui_msg(Msg(msg::kError, "backup_size=Backup file size for '{1}' is 0 bytes.")(Full_FileName));
Dees_Troy7c2dec82012-09-26 09:49:14 -04002331 return false;
2332 }
bigbiffce8f83c2015-12-12 18:30:21 -05002333 if (part_settings->progress)
2334 part_settings->progress->UpdateSize(Backup_Size);
2335
Dees_Troy43d8b002012-09-17 16:00:01 -04002336 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002337}
2338
bigbiffce8f83c2015-12-12 18:30:21 -05002339unsigned long long TWPartition::Get_Restore_Size(PartitionSettings *part_settings) {
2340 if (!part_settings->adbbackup) {
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002341 InfoManager restore_info(part_settings->Backup_Folder + "/" + Backup_Name + ".info");
bigbiffce8f83c2015-12-12 18:30:21 -05002342 if (restore_info.LoadValues() == 0) {
2343 if (restore_info.GetValue("backup_size", Restore_Size) == 0) {
2344 LOGINFO("Read info file, restore size is %llu\n", Restore_Size);
2345 return Restore_Size;
2346 }
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002347 }
2348 }
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002349
Matt Mower029a82d2017-01-08 13:12:38 -06002350 string Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
2351 string Restore_File_System = Get_Restore_File_System(part_settings);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002352
2353 if (Is_Image(Restore_File_System)) {
2354 Restore_Size = TWFunc::Get_File_Size(Full_FileName);
2355 return Restore_Size;
2356 }
2357
2358 twrpTar tar;
2359 tar.setdir(Backup_Path);
2360 tar.setfn(Full_FileName);
bigbiffce8f83c2015-12-12 18:30:21 -05002361 tar.backup_name = Full_FileName;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002362#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
2363 string Password;
2364 DataManager::GetValue("tw_restore_password", Password);
2365 if (!Password.empty())
2366 tar.setpassword(Password);
2367#endif
2368 tar.partition_name = Backup_Name;
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002369 tar.backup_folder = part_settings->Backup_Folder;
bigbiffce8f83c2015-12-12 18:30:21 -05002370 tar.part_settings = part_settings;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002371 Restore_Size = tar.get_size();
2372 return Restore_Size;
2373}
2374
bigbiffce8f83c2015-12-12 18:30:21 -05002375bool TWPartition::Restore_Tar(PartitionSettings *part_settings) {
Ethan Yonker472f5062016-02-25 13:47:30 -06002376 string Full_FileName;
Dees Troy4159aed2014-02-28 17:24:43 +00002377 bool ret = false;
bigbiffce8f83c2015-12-12 18:30:21 -05002378 string Restore_File_System = Get_Restore_File_System(part_settings);
Dees_Troy43d8b002012-09-17 16:00:01 -04002379
Dees_Troye58d5262012-09-21 12:27:57 -04002380 if (Has_Android_Secure) {
Dees_Troye58d5262012-09-21 12:27:57 -04002381 if (!Wipe_AndSec())
2382 return false;
Gary Peck43acadf2012-11-21 21:19:01 -08002383 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002384 gui_msg(Msg("wiping=Wiping {1}")(Backup_Display_Name));
Ethan Yonker5eac2222014-06-11 12:22:55 -05002385 if (Has_Data_Media && Mount_Point == "/data" && Restore_File_System != Current_File_System) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002386 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 -05002387 if (!Wipe_Data_Without_Wiping_Media())
2388 return false;
2389 } else {
2390 if (!Wipe(Restore_File_System))
2391 return false;
2392 }
Dees_Troye58d5262012-09-21 12:27:57 -04002393 }
Matt Mower3c366972015-12-25 19:28:31 -06002394 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Backup_Display_Name, gui_parse_text("{@restoring_hdr}"));
Ethan Yonker74db1572015-10-28 12:44:49 -05002395 gui_msg(Msg("restoring=Restoring {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002396
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05002397 // Remount as read/write as needed so we can restore the backup
2398 if (!ReMount_RW(true))
Dees_Troy43d8b002012-09-17 16:00:01 -04002399 return false;
2400
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002401 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
Ethan Yonker87af5632014-02-10 11:56:35 -06002402 twrpTar tar;
bigbiffce8f83c2015-12-12 18:30:21 -05002403 tar.part_settings = part_settings;
Ethan Yonker87af5632014-02-10 11:56:35 -06002404 tar.setdir(Backup_Path);
2405 tar.setfn(Full_FileName);
2406 tar.backup_name = Backup_Name;
2407#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
2408 string Password;
2409 DataManager::GetValue("tw_restore_password", Password);
2410 if (!Password.empty())
2411 tar.setpassword(Password);
2412#endif
bigbiffce8f83c2015-12-12 18:30:21 -05002413 part_settings->progress->SetPartitionSize(Get_Restore_Size(part_settings));
2414 if (tar.extractTarFork() != 0)
Dees Troy4159aed2014-02-28 17:24:43 +00002415 ret = false;
2416 else
2417 ret = true;
2418#ifdef HAVE_CAPABILITIES
2419 // Restore capabilities to the run-as binary
2420 if (Mount_Point == "/system" && Mount(true) && TWFunc::Path_Exists("/system/bin/run-as")) {
2421 struct vfs_cap_data cap_data;
2422 uint64_t capabilities = (1 << CAP_SETUID) | (1 << CAP_SETGID);
2423
2424 memset(&cap_data, 0, sizeof(cap_data));
2425 cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE;
2426 cap_data.data[0].permitted = (uint32_t) (capabilities & 0xffffffff);
2427 cap_data.data[0].inheritable = 0;
2428 cap_data.data[1].permitted = (uint32_t) (capabilities >> 32);
2429 cap_data.data[1].inheritable = 0;
2430 if (setxattr("/system/bin/run-as", XATTR_NAME_CAPS, &cap_data, sizeof(cap_data), 0) < 0) {
2431 LOGINFO("Failed to reset capabilities of /system/bin/run-as binary.\n");
2432 } else {
2433 LOGINFO("Reset capabilities of /system/bin/run-as binary successful.\n");
2434 }
2435 }
2436#endif
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05002437 if (Mount_Read_Only || Mount_Flags & MS_RDONLY)
2438 // Remount as read only when restoration is complete
2439 ReMount(true);
2440
Dees Troy4159aed2014-02-28 17:24:43 +00002441 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04002442}
2443
bigbiffce8f83c2015-12-12 18:30:21 -05002444bool TWPartition::Restore_Image(PartitionSettings *part_settings) {
Ethan Yonker96af84a2015-01-05 14:58:36 -06002445 string Full_FileName;
bigbiffce8f83c2015-12-12 18:30:21 -05002446 string Restore_File_System = Get_Restore_File_System(part_settings);
Dees_Troy43d8b002012-09-17 16:00:01 -04002447
Matt Mower3c366972015-12-25 19:28:31 -06002448 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Backup_Display_Name, gui_parse_text("{@restoring_hdr}"));
Ethan Yonker74db1572015-10-28 12:44:49 -05002449 gui_msg(Msg("restoring=Restoring {1}...")(Backup_Display_Name));
bigbiffce8f83c2015-12-12 18:30:21 -05002450
2451 if (part_settings->adbbackup)
2452 Full_FileName = TW_ADB_RESTORE;
2453 else
Ethan Yonkere080c1f2016-09-19 13:50:25 -05002454 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
Gary Peck15e623d2012-11-21 21:07:58 -08002455
Ethan Yonker96af84a2015-01-05 14:58:36 -06002456 if (Restore_File_System == "emmc") {
bigbiffce8f83c2015-12-12 18:30:21 -05002457 if (!part_settings->adbbackup)
2458 part_settings->total_restore_size = (uint64_t)(TWFunc::Get_File_Size(Full_FileName));
2459 if (!Raw_Read_Write(part_settings))
Ethan Yonker96af84a2015-01-05 14:58:36 -06002460 return false;
2461 } else if (Restore_File_System == "mtd" || Restore_File_System == "bml") {
bigbiffce8f83c2015-12-12 18:30:21 -05002462 if (!Flash_Image_FI(Full_FileName, part_settings->progress))
2463 return false;
2464 }
2465
2466 if (part_settings->adbbackup) {
2467 if (!twadbbu::Write_TWEOF())
Ethan Yonker96af84a2015-01-05 14:58:36 -06002468 return false;
Gary Peck15e623d2012-11-21 21:07:58 -08002469 }
Dees_Troy43d8b002012-09-17 16:00:01 -04002470 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002471}
Dees_Troy5bf43922012-09-07 16:07:55 -04002472
2473bool TWPartition::Update_Size(bool Display_Error) {
Dees_Troy0550cfb2012-10-13 11:56:13 -04002474 bool ret = false, Was_Already_Mounted = false;
Dees_Troy51127312012-09-08 13:08:49 -04002475
Ethan Yonker1b190162016-12-05 15:25:19 -06002476 Find_Actual_Block_Device();
2477
2478 if (!Can_Be_Mounted && !Is_Encrypted) {
2479 if (TWFunc::Path_Exists(Actual_Block_Device) && Find_Partition_Size()) {
2480 Used = Size;
2481 Backup_Size = Size;
2482 return true;
2483 }
Dees_Troy5bf43922012-09-07 16:07:55 -04002484 return false;
Ethan Yonker1b190162016-12-05 15:25:19 -06002485 }
Dees_Troy5bf43922012-09-07 16:07:55 -04002486
Dees_Troy0550cfb2012-10-13 11:56:13 -04002487 Was_Already_Mounted = Is_Mounted();
Dees_Troy38bd7602012-09-14 13:33:53 -04002488 if (Removable || Is_Encrypted) {
2489 if (!Mount(false))
2490 return true;
2491 } else if (!Mount(Display_Error))
Dees_Troy5bf43922012-09-07 16:07:55 -04002492 return false;
Dees_Troy51127312012-09-08 13:08:49 -04002493
2494 ret = Get_Size_Via_statfs(Display_Error);
Dees_Troy0550cfb2012-10-13 11:56:13 -04002495 if (!ret || Size == 0) {
2496 if (!Get_Size_Via_df(Display_Error)) {
2497 if (!Was_Already_Mounted)
2498 UnMount(false);
Dees_Troy51127312012-09-08 13:08:49 -04002499 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04002500 }
2501 }
Dees_Troy51127312012-09-08 13:08:49 -04002502
Dees_Troy5bf43922012-09-07 16:07:55 -04002503 if (Has_Data_Media) {
2504 if (Mount(Display_Error)) {
Ethan Yonker3fdcda42016-11-30 12:29:37 -06002505 Used = backup_exclusions.Get_Folder_Size(Mount_Point);
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002506 Backup_Size = Used;
2507 int bak = (int)(Used / 1048576LLU);
Dees_Troy51127312012-09-08 13:08:49 -04002508 int fre = (int)(Free / 1048576LLU);
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002509 LOGINFO("Data backup size is %iMB, free: %iMB.\n", bak, fre);
Dees_Troy0550cfb2012-10-13 11:56:13 -04002510 } else {
2511 if (!Was_Already_Mounted)
2512 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04002513 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04002514 }
Dees_Troye58d5262012-09-21 12:27:57 -04002515 } else if (Has_Android_Secure) {
2516 if (Mount(Display_Error))
Ethan Yonker3fdcda42016-11-30 12:29:37 -06002517 Backup_Size = backup_exclusions.Get_Folder_Size(Backup_Path);
Dees_Troy0550cfb2012-10-13 11:56:13 -04002518 else {
2519 if (!Was_Already_Mounted)
2520 UnMount(false);
Dees_Troye58d5262012-09-21 12:27:57 -04002521 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04002522 }
Dees_Troy5bf43922012-09-07 16:07:55 -04002523 }
Dees_Troy0550cfb2012-10-13 11:56:13 -04002524 if (!Was_Already_Mounted)
2525 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04002526 return true;
Dees_Troy51127312012-09-08 13:08:49 -04002527}
Dees_Troy38bd7602012-09-14 13:33:53 -04002528
2529void TWPartition::Find_Actual_Block_Device(void) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002530 if (Is_Decrypted && !Decrypted_Block_Device.empty()) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002531 Actual_Block_Device = Decrypted_Block_Device;
Ethan Yonker1b190162016-12-05 15:25:19 -06002532 if (TWFunc::Path_Exists(Decrypted_Block_Device)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002533 Is_Present = true;
Ethan Yonker1b190162016-12-05 15:25:19 -06002534 return;
2535 }
2536 } else if (SlotSelect && TWFunc::Path_Exists(Primary_Block_Device + PartitionManager.Get_Active_Slot_Suffix())) {
2537 Actual_Block_Device = Primary_Block_Device + PartitionManager.Get_Active_Slot_Suffix();
2538 unlink(Primary_Block_Device.c_str());
2539 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
2540 Is_Present = true;
2541 return;
Dees_Troy43d8b002012-09-17 16:00:01 -04002542 } else if (TWFunc::Path_Exists(Primary_Block_Device)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002543 Is_Present = true;
2544 Actual_Block_Device = Primary_Block_Device;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002545 return;
2546 }
Ethan Yonker1b190162016-12-05 15:25:19 -06002547 if (!Alternate_Block_Device.empty() && TWFunc::Path_Exists(Alternate_Block_Device)) {
Dees_Troy3f04d032012-10-07 18:20:09 -04002548 Actual_Block_Device = Alternate_Block_Device;
Dees_Troy38bd7602012-09-14 13:33:53 -04002549 Is_Present = true;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002550 } else {
Dees_Troy38bd7602012-09-14 13:33:53 -04002551 Is_Present = false;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002552 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002553}
2554
2555void TWPartition::Recreate_Media_Folder(void) {
2556 string Command;
Ethan Yonker66a19492015-12-10 10:19:45 -06002557 string Media_Path = Mount_Point + "/media";
Dees_Troy38bd7602012-09-14 13:33:53 -04002558
Ethan Yonkerbd7492d2016-12-07 13:55:01 -06002559 if (Is_FBE) {
2560 LOGINFO("Not recreating media folder on FBE\n");
2561 return;
2562 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002563 if (!Mount(true)) {
Ethan Yonker66a19492015-12-10 10:19:45 -06002564 gui_msg(Msg(msg::kError, "recreate_folder_err=Unable to recreate {1} folder.")(Media_Path));
2565 } else if (!TWFunc::Path_Exists(Media_Path)) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002566 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Ethan Yonker66a19492015-12-10 10:19:45 -06002567 LOGINFO("Recreating %s folder.\n", Media_Path.c_str());
2568 mkdir(Media_Path.c_str(), 0770);
xiaolu9416f4f2015-06-04 08:22:23 +08002569 string Internal_path = DataManager::GetStrValue("tw_internal_path");
2570 if (!Internal_path.empty()) {
2571 LOGINFO("Recreating %s folder.\n", Internal_path.c_str());
2572 mkdir(Internal_path.c_str(), 0770);
2573 }
2574#ifdef TW_INTERNAL_STORAGE_PATH
2575 mkdir(EXPAND(TW_INTERNAL_STORAGE_PATH), 0770);
2576#endif
Matt Mower87413642017-01-17 21:14:46 -06002577
thata3d31fb2014-12-21 22:27:40 +01002578 // Afterwards, we will try to set the
2579 // default metadata that we were hopefully able to get during
2580 // early boot.
Ethan Yonker66a19492015-12-10 10:19:45 -06002581 tw_set_default_metadata(Media_Path.c_str());
xiaolu9416f4f2015-06-04 08:22:23 +08002582 if (!Internal_path.empty())
2583 tw_set_default_metadata(Internal_path.c_str());
Matt Mower87413642017-01-17 21:14:46 -06002584
Ethan Yonker5eac2222014-06-11 12:22:55 -05002585 // Toggle mount to ensure that "internal sdcard" gets mounted
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002586 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Ethan Yonker5eac2222014-06-11 12:22:55 -05002587 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Dees_Troy38bd7602012-09-14 13:33:53 -04002588 }
Dees_Troy43d8b002012-09-17 16:00:01 -04002589}
Dees_Troye58d5262012-09-21 12:27:57 -04002590
2591void TWPartition::Recreate_AndSec_Folder(void) {
Dees_Troye58d5262012-09-21 12:27:57 -04002592 if (!Has_Android_Secure)
2593 return;
Dees_Troy2673cec2013-04-02 20:22:16 +00002594 LOGINFO("Creating %s: %s\n", Backup_Display_Name.c_str(), Symlink_Path.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -04002595 if (!Mount(true)) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002596 gui_msg(Msg(msg::kError, "recreate_folder_err=Unable to recreate {1} folder.")(Backup_Name));
Dees_Troye58d5262012-09-21 12:27:57 -04002597 } else if (!TWFunc::Path_Exists(Symlink_Path)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00002598 LOGINFO("Recreating %s folder.\n", Backup_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002599 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05002600 mkdir(Symlink_Path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002601 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04002602 }
2603}
bigbiff7cb4c332014-11-26 20:36:07 -05002604
2605uint64_t TWPartition::Get_Max_FileSize() {
2606 uint64_t maxFileSize = 0;
2607 const uint64_t constGB = (uint64_t) 1024 * 1024 * 1024;
2608 const uint64_t constTB = (uint64_t) constGB * 1024;
2609 const uint64_t constPB = (uint64_t) constTB * 1024;
bigbiff0c532032014-12-21 13:41:26 -05002610 if (Current_File_System == "ext4")
2611 maxFileSize = 16 * constTB; //16 TB
2612 else if (Current_File_System == "vfat")
2613 maxFileSize = 4 * constGB; //4 GB
2614 else if (Current_File_System == "ntfs")
2615 maxFileSize = 256 * constTB; //256 TB
2616 else if (Current_File_System == "exfat")
2617 maxFileSize = 16 * constPB; //16 PB
2618 else if (Current_File_System == "ext3")
2619 maxFileSize = 2 * constTB; //2 TB
2620 else if (Current_File_System == "f2fs")
2621 maxFileSize = 3.94 * constTB; //3.94 TB
bigbiff7cb4c332014-11-26 20:36:07 -05002622 else
2623 maxFileSize = 100000000L;
2624 return maxFileSize - 1;
2625}
2626
bigbiffce8f83c2015-12-12 18:30:21 -05002627bool TWPartition::Flash_Image(PartitionSettings *part_settings) {
2628 string Restore_File_System, full_filename;
Ethan Yonker96af84a2015-01-05 14:58:36 -06002629
Ethan Yonkere080c1f2016-09-19 13:50:25 -05002630 full_filename = part_settings->Backup_Folder + "/" + Backup_FileName;
Ethan Yonker96af84a2015-01-05 14:58:36 -06002631
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002632 LOGINFO("Image filename is: %s\n", Backup_FileName.c_str());
bigbiffce8f83c2015-12-12 18:30:21 -05002633
2634 if (Backup_Method == BM_FILES) {
Ethan Yonker96af84a2015-01-05 14:58:36 -06002635 LOGERR("Cannot flash images to file systems\n");
2636 return false;
2637 } else if (!Can_Flash_Img) {
2638 LOGERR("Cannot flash images to partitions %s\n", Display_Name.c_str());
2639 return false;
2640 } else {
2641 if (!Find_Partition_Size()) {
2642 LOGERR("Unable to find partition size for '%s'\n", Mount_Point.c_str());
2643 return false;
2644 }
bigbiffce8f83c2015-12-12 18:30:21 -05002645 unsigned long long image_size = TWFunc::Get_File_Size(full_filename);
Ethan Yonker96af84a2015-01-05 14:58:36 -06002646 if (image_size > Size) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002647 LOGINFO("Size (%llu bytes) of image '%s' is larger than target device '%s' (%llu bytes)\n",
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002648 image_size, Backup_FileName.c_str(), Actual_Block_Device.c_str(), Size);
Ethan Yonker74db1572015-10-28 12:44:49 -05002649 gui_err("img_size_err=Size of image is larger than target device");
Ethan Yonker96af84a2015-01-05 14:58:36 -06002650 return false;
2651 }
bigbiffce8f83c2015-12-12 18:30:21 -05002652 if (Backup_Method == BM_DD) {
2653 if (!part_settings->adbbackup) {
2654 if (Is_Sparse_Image(full_filename)) {
2655 return Flash_Sparse_Image(full_filename);
2656 }
Ethan Yonker472f5062016-02-25 13:47:30 -06002657 }
bigbiffce8f83c2015-12-12 18:30:21 -05002658 return Raw_Read_Write(part_settings);
2659 } else if (Backup_Method == BM_FLASH_UTILS) {
2660 return Flash_Image_FI(full_filename, NULL);
Ethan Yonker472f5062016-02-25 13:47:30 -06002661 }
Ethan Yonker96af84a2015-01-05 14:58:36 -06002662 }
2663
2664 LOGERR("Unknown flash method for '%s'\n", Mount_Point.c_str());
2665 return false;
2666}
2667
Ethan Yonker472f5062016-02-25 13:47:30 -06002668bool TWPartition::Is_Sparse_Image(const string& Filename) {
HashBanged974bb2016-01-30 14:20:09 -05002669 uint32_t magic = 0;
2670 int fd = open(Filename.c_str(), O_RDONLY);
2671 if (fd < 0) {
2672 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Filename)(strerror(errno)));
2673 return false;
2674 }
bigbiffce8f83c2015-12-12 18:30:21 -05002675
HashBanged974bb2016-01-30 14:20:09 -05002676 if (read(fd, &magic, sizeof(magic)) != sizeof(magic)) {
2677 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Filename)(strerror(errno)));
2678 close(fd);
2679 return false;
2680 }
2681 close(fd);
Ethan Yonker472f5062016-02-25 13:47:30 -06002682 if (magic == SPARSE_HEADER_MAGIC)
2683 return true;
2684 return false;
2685}
2686
2687bool TWPartition::Flash_Sparse_Image(const string& Filename) {
2688 string Command;
2689
2690 gui_msg(Msg("flashing=Flashing {1}...")(Display_Name));
2691
2692 Command = "simg2img '" + Filename + "' '" + Actual_Block_Device + "'";
Ethan Yonker96af84a2015-01-05 14:58:36 -06002693 LOGINFO("Flash command: '%s'\n", Command.c_str());
2694 TWFunc::Exec_Cmd(Command);
2695 return true;
2696}
2697
Ethan Yonker472f5062016-02-25 13:47:30 -06002698bool TWPartition::Flash_Image_FI(const string& Filename, ProgressTracking *progress) {
Ethan Yonker96af84a2015-01-05 14:58:36 -06002699 string Command;
Ethan Yonker472f5062016-02-25 13:47:30 -06002700 unsigned long long file_size;
Ethan Yonker96af84a2015-01-05 14:58:36 -06002701
Ethan Yonker74db1572015-10-28 12:44:49 -05002702 gui_msg(Msg("flashing=Flashing {1}...")(Display_Name));
Ethan Yonker472f5062016-02-25 13:47:30 -06002703 if (progress) {
2704 file_size = (unsigned long long)(TWFunc::Get_File_Size(Filename));
2705 progress->SetPartitionSize(file_size);
2706 }
Ethan Yonker96af84a2015-01-05 14:58:36 -06002707 // Sometimes flash image doesn't like to flash due to the first 2KB matching, so we erase first to ensure that it flashes
2708 Command = "erase_image " + MTD_Name;
2709 LOGINFO("Erase command: '%s'\n", Command.c_str());
2710 TWFunc::Exec_Cmd(Command);
2711 Command = "flash_image " + MTD_Name + " '" + Filename + "'";
2712 LOGINFO("Flash command: '%s'\n", Command.c_str());
2713 TWFunc::Exec_Cmd(Command);
Ethan Yonker472f5062016-02-25 13:47:30 -06002714 if (progress)
2715 progress->UpdateSize(file_size);
Ethan Yonker96af84a2015-01-05 14:58:36 -06002716 return true;
2717}
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05002718
2719void TWPartition::Change_Mount_Read_Only(bool new_value) {
2720 Mount_Read_Only = new_value;
2721}
2722
bigbiffce8f83c2015-12-12 18:30:21 -05002723bool TWPartition::Is_Read_Only() {
2724 return Mount_Read_Only;
2725}
2726
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05002727int TWPartition::Check_Lifetime_Writes() {
2728 bool original_read_only = Mount_Read_Only;
2729 int ret = 1;
2730
2731 Mount_Read_Only = true;
2732 if (Mount(false)) {
2733 Find_Actual_Block_Device();
2734 string block = basename(Actual_Block_Device.c_str());
2735 string file = "/sys/fs/" + Current_File_System + "/" + block + "/lifetime_write_kbytes";
2736 string result;
2737 if (TWFunc::Path_Exists(file)) {
2738 if (TWFunc::read_file(file, result) != 0) {
2739 LOGINFO("Check_Lifetime_Writes of '%s' failed to read_file\n", file.c_str());
2740 } else {
2741 LOGINFO("Check_Lifetime_Writes result: '%s'\n", result.c_str());
2742 if (result == "0") {
2743 ret = 0;
2744 }
2745 }
2746 } else {
2747 LOGINFO("Check_Lifetime_Writes file does not exist '%s'\n", file.c_str());
2748 }
2749 UnMount(true);
2750 } else {
2751 LOGINFO("Check_Lifetime_Writes failed to mount '%s'\n", Mount_Point.c_str());
2752 }
2753 Mount_Read_Only = original_read_only;
2754 return ret;
2755}
Ethan Yonker66a19492015-12-10 10:19:45 -06002756
2757int TWPartition::Decrypt_Adopted() {
2758#ifdef TW_INCLUDE_CRYPTO
2759 int ret = 1;
2760 Is_Adopted_Storage = false;
2761 string Adopted_Key_File = "";
2762
2763 if (!Removable)
2764 return ret;
2765
2766 int fd = open(Alternate_Block_Device.c_str(), O_RDONLY);
2767 if (fd < 0) {
2768 LOGINFO("failed to open '%s'\n", Alternate_Block_Device.c_str());
2769 return ret;
2770 }
2771 char type_guid[80];
2772 char part_guid[80];
2773
2774 if (gpt_disk_get_partition_info(fd, 2, type_guid, part_guid) == 0) {
2775 LOGINFO("type: '%s'\n", type_guid);
2776 LOGINFO("part: '%s'\n", part_guid);
2777 Adopted_GUID = part_guid;
2778 LOGINFO("Adopted_GUID '%s'\n", Adopted_GUID.c_str());
2779 if (strcmp(type_guid, TWGptAndroidExpand) == 0) {
2780 LOGINFO("android_expand found\n");
2781 Adopted_Key_File = "/data/misc/vold/expand_";
2782 Adopted_Key_File += part_guid;
2783 Adopted_Key_File += ".key";
2784 if (TWFunc::Path_Exists(Adopted_Key_File)) {
2785 Is_Adopted_Storage = true;
2786 /* Until we find a use case for this, I think it is safe
2787 * to disable USB Mass Storage whenever adopted storage
2788 * is present.
2789 */
2790 LOGINFO("Detected adopted storage, disabling USB mass storage mode\n");
2791 DataManager::SetValue("tw_has_usb_storage", 0);
2792 }
2793 }
2794 }
2795
2796 if (Is_Adopted_Storage) {
2797 string Adopted_Block_Device = Alternate_Block_Device + "p2";
2798 if (!TWFunc::Path_Exists(Adopted_Block_Device)) {
2799 Adopted_Block_Device = Alternate_Block_Device + "2";
2800 if (!TWFunc::Path_Exists(Adopted_Block_Device)) {
2801 LOGINFO("Adopted block device does not exist\n");
2802 goto exit;
2803 }
2804 }
2805 LOGINFO("key file is '%s', block device '%s'\n", Adopted_Key_File.c_str(), Adopted_Block_Device.c_str());
2806 char crypto_blkdev[MAXPATHLEN];
2807 std::string thekey;
2808 int fdkey = open(Adopted_Key_File.c_str(), O_RDONLY);
2809 if (fdkey < 0) {
2810 LOGINFO("failed to open key file\n");
2811 goto exit;
2812 }
2813 char buf[512];
2814 ssize_t n;
2815 while ((n = read(fdkey, &buf[0], sizeof(buf))) > 0) {
2816 thekey.append(buf, n);
2817 }
2818 close(fdkey);
2819 unsigned char* key = (unsigned char*) thekey.data();
2820 cryptfs_revert_ext_volume(part_guid);
2821
2822 ret = cryptfs_setup_ext_volume(part_guid, Adopted_Block_Device.c_str(), key, thekey.size(), crypto_blkdev);
2823 if (ret == 0) {
2824 LOGINFO("adopted storage new block device: '%s'\n", crypto_blkdev);
2825 Decrypted_Block_Device = crypto_blkdev;
2826 Is_Decrypted = true;
2827 Is_Encrypted = true;
2828 Find_Actual_Block_Device();
Kjell Braden3126a112016-06-19 16:58:15 +00002829 if (!Mount_Storage_Retry(false)) {
Ethan Yonker66a19492015-12-10 10:19:45 -06002830 LOGERR("Failed to mount decrypted adopted storage device\n");
2831 Is_Decrypted = false;
2832 Is_Encrypted = false;
2833 cryptfs_revert_ext_volume(part_guid);
2834 ret = 1;
2835 } else {
Ethan Yonkerfcf3f242016-02-16 12:30:26 -06002836 UnMount(false);
2837 Has_Android_Secure = false;
2838 Symlink_Path = "";
2839 Symlink_Mount_Point = "";
2840 Backup_Name = Mount_Point.substr(1);
2841 Backup_Path = Mount_Point;
2842 TWPartition* sdext = PartitionManager.Find_Partition_By_Path("/sd-ext");
2843 if (sdext && sdext->Actual_Block_Device == Adopted_Block_Device) {
2844 LOGINFO("Removing /sd-ext from partition list due to adopted storage\n");
2845 PartitionManager.Remove_Partition_By_Path("/sd-ext");
2846 }
Ethan Yonker66a19492015-12-10 10:19:45 -06002847 Setup_Data_Media();
2848 Recreate_Media_Folder();
2849 Wipe_Available_in_GUI = true;
2850 Wipe_During_Factory_Reset = true;
2851 Can_Be_Backed_Up = true;
2852 Can_Encrypt_Backup = true;
2853 Use_Userdata_Encryption = true;
2854 Is_Storage = true;
2855 Storage_Name = "Adopted Storage";
2856 Is_SubPartition = true;
2857 SubPartition_Of = "/data";
2858 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
2859 DataManager::SetValue("tw_has_adopted_storage", 1);
2860 }
2861 } else {
2862 LOGERR("Failed to setup adopted storage decryption\n");
2863 }
2864 }
2865exit:
Matt Mower06543e32017-01-06 15:25:26 -06002866 close(fd);
Ethan Yonker66a19492015-12-10 10:19:45 -06002867 return ret;
2868#else
2869 LOGINFO("Decrypt_Adopted: no crypto support\n");
2870 return 1;
2871#endif
2872}
2873
2874void TWPartition::Revert_Adopted() {
2875#ifdef TW_INCLUDE_CRYPTO
2876 if (!Adopted_GUID.empty()) {
2877 PartitionManager.Remove_MTP_Storage(Mount_Point);
2878 UnMount(false);
2879 cryptfs_revert_ext_volume(Adopted_GUID.c_str());
2880 Is_Adopted_Storage = false;
2881 Is_Encrypted = false;
2882 Is_Decrypted = false;
2883 Decrypted_Block_Device = "";
2884 Find_Actual_Block_Device();
2885 Wipe_During_Factory_Reset = false;
2886 Can_Be_Backed_Up = false;
2887 Can_Encrypt_Backup = false;
2888 Use_Userdata_Encryption = false;
2889 Is_SubPartition = false;
2890 SubPartition_Of = "";
2891 Has_Data_Media = false;
2892 Storage_Path = Mount_Point;
2893 if (!Symlink_Mount_Point.empty()) {
2894 TWPartition* Dat = PartitionManager.Find_Partition_By_Path("/data");
2895 if (Dat) {
2896 Dat->UnMount(false);
2897 Dat->Symlink_Mount_Point = Symlink_Mount_Point;
2898 }
2899 Symlink_Mount_Point = "";
2900 }
2901 }
2902#else
2903 LOGINFO("Revert_Adopted: no crypto support\n");
2904#endif
2905}
bigbiff bigbiffb5ecaad2017-03-20 18:53:53 -04002906
2907void TWPartition::Set_Backup_FileName(string fname) {
2908 Backup_FileName = fname;
2909}
2910
2911string TWPartition::Get_Backup_Name() {
2912 return Backup_Name;
2913}