blob: 5bbde2cc9bedf5a5ed730148cbf2bd81cdb17152 [file] [log] [blame]
Dees Troy3be70a82013-10-22 14:25:12 +00001/*
Ethan Yonker472f5062016-02-25 13:47:30 -06002 Copyright 2013 to 2016 TeamWin
Dees Troy3be70a82013-10-22 14:25:12 +00003 This file is part of TWRP/TeamWin Recovery Project.
4
5 TWRP is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 TWRP is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with TWRP. If not, see <http://www.gnu.org/licenses/>.
17*/
Dees_Troy51a0e822012-09-05 15:24:24 -040018
19#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
22#include <sys/stat.h>
23#include <sys/vfs.h>
Dees_Troy5bf43922012-09-07 16:07:55 -040024#include <sys/mount.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040025#include <unistd.h>
Dees_Troy51127312012-09-08 13:08:49 -040026#include <dirent.h>
thatf54e5392016-01-29 22:04:43 +010027#include <libgen.h>
bigbiff bigbiff9c754052013-01-09 09:09:08 -050028#include <iostream>
29#include <sstream>
Ethan Yonker66a19492015-12-10 10:19:45 -060030#include <sys/param.h>
Ethan Yonker472f5062016-02-25 13:47:30 -060031#include <fcntl.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040032
Dees_Troy657c3092012-09-10 20:32:10 -040033#ifdef TW_INCLUDE_CRYPTO
34 #include "cutils/properties.h"
35#endif
36
bigbiff7b4c7a62015-01-01 19:44:14 -050037#include "libblkid/include/blkid.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040038#include "variables.h"
Dees_Troy2673cec2013-04-02 20:22:16 +000039#include "twcommon.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040040#include "partitions.hpp"
Dees_Troy5bf43922012-09-07 16:07:55 -040041#include "data.hpp"
Dees_Troy43d8b002012-09-17 16:00:01 -040042#include "twrp-functions.hpp"
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -050043#include "twrpDigest.hpp"
bigbiff bigbiff9c754052013-01-09 09:09:08 -050044#include "twrpTar.hpp"
bigbiff bigbiff34684ff2013-12-01 21:03:45 -050045#include "twrpDU.hpp"
Ethan Yonker1b7a31b2014-07-03 15:09:22 -050046#include "infomanager.hpp"
Ethan Yonker4b94cfd2014-12-11 10:00:45 -060047#include "set_metadata.h"
Ethan Yonker74db1572015-10-28 12:44:49 -050048#include "gui/gui.hpp"
Dees_Troy5bf43922012-09-07 16:07:55 -040049extern "C" {
Dees_Troy38bd7602012-09-14 13:33:53 -040050 #include "mtdutils/mtdutils.h"
51 #include "mtdutils/mounts.h"
Dees_Troya95f55c2013-08-17 13:14:43 +000052#ifdef USE_EXT4
53 #include "make_ext4fs.h"
54#endif
Ethan Yonker71413f42014-02-26 13:36:08 -060055
56#ifdef TW_INCLUDE_CRYPTO
Ethan Yonker253368a2014-11-25 15:00:52 -060057 #include "crypto/lollipop/cryptfs.h"
Ethan Yonker66a19492015-12-10 10:19:45 -060058 #include "gpt/gpt.h"
Ethan Yonkerbc85b632015-08-09 12:48:14 -050059#else
60 #define CRYPT_FOOTER_OFFSET 0x4000
Ethan Yonker71413f42014-02-26 13:36:08 -060061#endif
Dees_Troy5bf43922012-09-07 16:07:55 -040062}
bigbiff bigbiffc49d7062013-10-11 20:28:00 -040063#ifdef HAVE_SELINUX
64#include "selinux/selinux.h"
Ethan Yonkerf27497f2014-02-09 11:48:33 -060065#include <selinux/label.h>
bigbiff bigbiffc49d7062013-10-11 20:28:00 -040066#endif
Dees Troy4159aed2014-02-28 17:24:43 +000067#ifdef HAVE_CAPABILITIES
68#include <sys/capability.h>
69#include <sys/xattr.h>
70#include <linux/xattr.h>
71#endif
HashBanged974bb2016-01-30 14:20:09 -050072#include <sparse_format.h>
Ethan Yonker472f5062016-02-25 13:47:30 -060073#include "progresstracking.hpp"
Dees_Troy51a0e822012-09-05 15:24:24 -040074
bigbiff bigbiff9c754052013-01-09 09:09:08 -050075using namespace std;
76
Dees_Troya95f55c2013-08-17 13:14:43 +000077extern struct selabel_handle *selinux_handle;
Ethan Yonker6277c792014-09-15 14:54:30 -050078extern bool datamedia;
Dees_Troya95f55c2013-08-17 13:14:43 +000079
Hashcode62bd9e02013-11-19 21:59:42 -080080struct flag_list {
81 const char *name;
82 unsigned flag;
83};
84
Matt Mower4ab42b12016-04-21 13:52:18 -050085const struct flag_list mount_flags[] = {
Hashcode62bd9e02013-11-19 21:59:42 -080086 { "noatime", MS_NOATIME },
87 { "noexec", MS_NOEXEC },
88 { "nosuid", MS_NOSUID },
89 { "nodev", MS_NODEV },
90 { "nodiratime", MS_NODIRATIME },
91 { "ro", MS_RDONLY },
92 { "rw", 0 },
93 { "remount", MS_REMOUNT },
94 { "bind", MS_BIND },
95 { "rec", MS_REC },
Dees Troyc4bc30e2014-02-03 15:04:19 +000096#ifdef MS_UNBINDABLE
Hashcode62bd9e02013-11-19 21:59:42 -080097 { "unbindable", MS_UNBINDABLE },
Dees Troyc4bc30e2014-02-03 15:04:19 +000098#endif
99#ifdef MS_PRIVATE
Hashcode62bd9e02013-11-19 21:59:42 -0800100 { "private", MS_PRIVATE },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000101#endif
102#ifdef MS_SLAVE
Hashcode62bd9e02013-11-19 21:59:42 -0800103 { "slave", MS_SLAVE },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000104#endif
105#ifdef MS_SHARED
Hashcode62bd9e02013-11-19 21:59:42 -0800106 { "shared", MS_SHARED },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000107#endif
Hashcode62bd9e02013-11-19 21:59:42 -0800108 { "sync", MS_SYNCHRONOUS },
109 { "defaults", 0 },
110 { 0, 0 },
111};
112
Matt Mower2416a502016-04-12 19:54:46 -0500113enum TW_FSTAB_FLAGS {
114 TWFLAG_DEFAULTS, // Retain position
115 TWFLAG_ANDSEC,
116 TWFLAG_BACKUP,
117 TWFLAG_BACKUPNAME,
118 TWFLAG_BLOCKSIZE,
119 TWFLAG_CANBEWIPED,
120 TWFLAG_CANENCRYPTBACKUP,
121 TWFLAG_DISPLAY,
122 TWFLAG_ENCRYPTABLE,
123 TWFLAG_FLASHIMG,
124 TWFLAG_FORCEENCRYPT,
125 TWFLAG_FSFLAGS,
126 TWFLAG_IGNOREBLKID,
127 TWFLAG_LENGTH,
128 TWFLAG_MOUNTTODECRYPT,
129 TWFLAG_REMOVABLE,
130 TWFLAG_RETAINLAYOUTVERSION,
131 TWFLAG_SETTINGSSTORAGE,
132 TWFLAG_STORAGE,
133 TWFLAG_STORAGENAME,
134 TWFLAG_SUBPARTITIONOF,
135 TWFLAG_SYMLINK,
136 TWFLAG_USERDATAENCRYPTBACKUP,
137 TWFLAG_USERMRF,
138 TWFLAG_WIPEDURINGFACTORYRESET,
139 TWFLAG_WIPEINGUI,
140};
141
142/* Flags without a trailing '=' are considered dual format flags and can be
143 * written as either 'flagname' or 'flagname=', where the character following
144 * the '=' is Y,y,1 for true and false otherwise.
145 */
146const struct flag_list tw_flags[] = {
147 { "andsec", TWFLAG_ANDSEC },
148 { "backup", TWFLAG_BACKUP },
149 { "backupname=", TWFLAG_BACKUPNAME },
150 { "blocksize=", TWFLAG_BLOCKSIZE },
151 { "canbewiped", TWFLAG_CANBEWIPED },
152 { "canencryptbackup", TWFLAG_CANENCRYPTBACKUP },
153 { "defaults", TWFLAG_DEFAULTS },
154 { "display=", TWFLAG_DISPLAY },
155 { "encryptable=", TWFLAG_ENCRYPTABLE },
156 { "flashimg", TWFLAG_FLASHIMG },
157 { "forceencrypt=", TWFLAG_FORCEENCRYPT },
158 { "fsflags=", TWFLAG_FSFLAGS },
159 { "ignoreblkid", TWFLAG_IGNOREBLKID },
160 { "length=", TWFLAG_LENGTH },
161 { "mounttodecrypt", TWFLAG_MOUNTTODECRYPT },
162 { "removable", TWFLAG_REMOVABLE },
163 { "retainlayoutversion", TWFLAG_RETAINLAYOUTVERSION },
164 { "settingsstorage", TWFLAG_SETTINGSSTORAGE },
165 { "storage", TWFLAG_STORAGE },
166 { "storagename=", TWFLAG_STORAGENAME },
167 { "subpartitionof=", TWFLAG_SUBPARTITIONOF },
168 { "symlink=", TWFLAG_SYMLINK },
169 { "userdataencryptbackup", TWFLAG_USERDATAENCRYPTBACKUP },
170 { "usermrf", TWFLAG_USERMRF },
171 { "wipeduringfactoryreset", TWFLAG_WIPEDURINGFACTORYRESET },
172 { "wipeingui", TWFLAG_WIPEINGUI },
173 { 0, 0 },
174};
175
that9e0593e2014-10-08 00:01:24 +0200176TWPartition::TWPartition() {
Dees_Troy51a0e822012-09-05 15:24:24 -0400177 Can_Be_Mounted = false;
178 Can_Be_Wiped = false;
Dees_Troya13d74f2013-03-24 08:54:55 -0500179 Can_Be_Backed_Up = false;
Vojtech Bocek1dc30982013-08-30 21:49:30 +0200180 Use_Rm_Rf = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400181 Wipe_During_Factory_Reset = false;
182 Wipe_Available_in_GUI = false;
183 Is_SubPartition = false;
Dees_Troy2691f9d2012-09-24 11:15:49 -0400184 Has_SubPartition = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400185 SubPartition_Of = "";
186 Symlink_Path = "";
187 Symlink_Mount_Point = "";
188 Mount_Point = "";
Dees_Troye58d5262012-09-21 12:27:57 -0400189 Backup_Path = "";
Dees_Troy38bd7602012-09-14 13:33:53 -0400190 Actual_Block_Device = "";
191 Primary_Block_Device = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400192 Alternate_Block_Device = "";
193 Removable = false;
194 Is_Present = false;
195 Length = 0;
196 Size = 0;
197 Used = 0;
198 Free = 0;
199 Backup_Size = 0;
200 Can_Be_Encrypted = false;
201 Is_Encrypted = false;
202 Is_Decrypted = false;
Ethan Yonker253368a2014-11-25 15:00:52 -0600203 Mount_To_Decrypt = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400204 Decrypted_Block_Device = "";
205 Display_Name = "";
Dees_Troya13d74f2013-03-24 08:54:55 -0500206 Backup_Display_Name = "";
207 Storage_Name = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400208 Backup_Name = "";
Dees_Troy63c8df72012-09-10 14:02:05 -0400209 Backup_FileName = "";
Dees_Troy38bd7602012-09-14 13:33:53 -0400210 MTD_Name = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400211 Backup_Method = NONE;
Dees_Troy83bd4832013-05-04 12:39:56 +0000212 Can_Encrypt_Backup = false;
213 Use_Userdata_Encryption = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400214 Has_Data_Media = false;
Dees_Troye58d5262012-09-21 12:27:57 -0400215 Has_Android_Secure = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400216 Is_Storage = false;
Dees_Troya13d74f2013-03-24 08:54:55 -0500217 Is_Settings_Storage = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400218 Storage_Path = "";
219 Current_File_System = "";
220 Fstab_File_System = "";
Hashcode62bd9e02013-11-19 21:59:42 -0800221 Mount_Flags = 0;
222 Mount_Options = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400223 Format_Block_Size = 0;
Dees_Troy68cab492012-12-12 19:29:35 +0000224 Ignore_Blkid = false;
Dees_Troy16c2b312013-01-15 16:51:18 +0000225 Retain_Layout_Version = false;
Ethan Yonker253368a2014-11-25 15:00:52 -0600226 Crypto_Key_Location = "footer";
Ethan Yonker726a0202014-12-16 20:01:38 -0600227 MTP_Storage_ID = 0;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600228 Can_Flash_Img = false;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500229 Mount_Read_Only = false;
Ethan Yonker66a19492015-12-10 10:19:45 -0600230 Is_Adopted_Storage = false;
231 Adopted_GUID = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400232}
233
234TWPartition::~TWPartition(void) {
235 // Do nothing
236}
237
Matt Mower2b2dd152016-04-26 11:24:08 -0500238bool TWPartition::Process_Fstab_Line(const char *fstab_line, bool Display_Error) {
239 char full_line[MAX_FSTAB_LINE_LENGTH];
Matt Mower2416a502016-04-12 19:54:46 -0500240 char twflags[MAX_FSTAB_LINE_LENGTH] = "";
Dees_Troy5bf43922012-09-07 16:07:55 -0400241 char* ptr;
Matt Mower2b2dd152016-04-26 11:24:08 -0500242 int line_len = strlen(fstab_line), index = 0, item_index = 0;
Dees_Troya13d74f2013-03-24 08:54:55 -0500243 bool skip = false;
Dees_Troy5bf43922012-09-07 16:07:55 -0400244
Matt Mower2b2dd152016-04-26 11:24:08 -0500245 strlcpy(full_line, fstab_line, sizeof(full_line));
Dees_Troy51127312012-09-08 13:08:49 -0400246 for (index = 0; index < line_len; index++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500247 if (full_line[index] == 34)
248 skip = !skip;
249 if (!skip && full_line[index] <= 32)
Dees_Troy5bf43922012-09-07 16:07:55 -0400250 full_line[index] = '\0';
Dees_Troy5bf43922012-09-07 16:07:55 -0400251 }
Dees_Troy7c2dec82012-09-26 09:49:14 -0400252 Mount_Point = full_line;
Dees_Troy2673cec2013-04-02 20:22:16 +0000253 LOGINFO("Processing '%s'\n", Mount_Point.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -0400254 Backup_Path = Mount_Point;
Dees_Troya13d74f2013-03-24 08:54:55 -0500255 Storage_Path = Mount_Point;
Dees_Troy70737fa2013-04-08 13:19:20 +0000256 Display_Name = full_line + 1;
257 Backup_Display_Name = Display_Name;
258 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400259 index = Mount_Point.size();
260 while (index < line_len) {
261 while (index < line_len && full_line[index] == '\0')
262 index++;
263 if (index >= line_len)
264 continue;
265 ptr = full_line + index;
266 if (item_index == 0) {
267 // File System
268 Fstab_File_System = ptr;
269 Current_File_System = ptr;
270 item_index++;
271 } else if (item_index == 1) {
272 // Primary Block Device
Dees_Troy38bd7602012-09-14 13:33:53 -0400273 if (Fstab_File_System == "mtd" || Fstab_File_System == "yaffs2") {
Dees_Troy094207a2012-09-26 12:00:39 -0400274 MTD_Name = ptr;
275 Find_MTD_Block_Device(MTD_Name);
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400276 } else if (Fstab_File_System == "bml") {
277 if (Mount_Point == "/boot")
278 MTD_Name = "boot";
279 else if (Mount_Point == "/recovery")
280 MTD_Name = "recovery";
281 Primary_Block_Device = ptr;
282 if (*ptr != '/')
Dees_Troy2673cec2013-04-02 20:22:16 +0000283 LOGERR("Until we get better BML support, you will have to find and provide the full block device path to the BML devices e.g. /dev/block/bml9 instead of the partition name\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400284 } else if (*ptr != '/') {
Dees_Troy5bf43922012-09-07 16:07:55 -0400285 if (Display_Error)
Matt Mower2b2dd152016-04-26 11:24:08 -0500286 LOGERR("Invalid block device '%s' in fstab line '%s'", ptr, fstab_line);
Dees_Troy5bf43922012-09-07 16:07:55 -0400287 else
Matt Mower2b2dd152016-04-26 11:24:08 -0500288 LOGINFO("Invalid block device '%s' in fstab line '%s'", ptr, fstab_line);
Dees_Troy5bf43922012-09-07 16:07:55 -0400289 return 0;
Dees_Troy38bd7602012-09-14 13:33:53 -0400290 } else {
291 Primary_Block_Device = ptr;
292 Find_Real_Block_Device(Primary_Block_Device, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400293 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400294 item_index++;
295 } else if (item_index > 1) {
296 if (*ptr == '/') {
297 // Alternate Block Device
298 Alternate_Block_Device = ptr;
299 Find_Real_Block_Device(Alternate_Block_Device, Display_Error);
300 } else if (strlen(ptr) > 7 && strncmp(ptr, "length=", 7) == 0) {
301 // Partition length
302 ptr += 7;
303 Length = atoi(ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400304 } else if (strlen(ptr) > 6 && strncmp(ptr, "flags=", 6) == 0) {
305 // Custom flags, save for later so that new values aren't overwritten by defaults
306 ptr += 6;
Matt Mower2416a502016-04-12 19:54:46 -0500307 strlcpy(twflags, ptr, sizeof(twflags));
308 Process_TW_Flags(twflags, Display_Error);
Dees_Troy38bd7602012-09-14 13:33:53 -0400309 } else if (strlen(ptr) == 4 && (strncmp(ptr, "NULL", 4) == 0 || strncmp(ptr, "null", 4) == 0 || strncmp(ptr, "null", 4) == 0)) {
310 // Do nothing
Dees_Troy5bf43922012-09-07 16:07:55 -0400311 } else {
312 // Unhandled data
Matt Mower2b2dd152016-04-26 11:24:08 -0500313 LOGINFO("Unhandled fstab information '%s' in fstab line '%s'\n", ptr, fstab_line);
Dees_Troy5bf43922012-09-07 16:07:55 -0400314 }
315 }
316 while (index < line_len && full_line[index] != '\0')
317 index++;
318 }
319
320 if (!Is_File_System(Fstab_File_System) && !Is_Image(Fstab_File_System)) {
321 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000322 LOGERR("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400323 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000324 LOGINFO("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400325 return 0;
326 } else if (Is_File_System(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400327 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400328 Setup_File_System(Display_Error);
329 if (Mount_Point == "/system") {
330 Display_Name = "System";
Dees_Troya13d74f2013-03-24 08:54:55 -0500331 Backup_Display_Name = Display_Name;
332 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400333 Wipe_Available_in_GUI = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500334 Can_Be_Backed_Up = true;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500335 Mount_Read_Only = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400336 } else if (Mount_Point == "/data") {
Dees Troyc657cc02015-01-16 22:48:47 +0000337 UnMount(false); // added in case /data is mounted as tmpfs for qcom hardware decrypt
Dees_Troy5bf43922012-09-07 16:07:55 -0400338 Display_Name = "Data";
Dees_Troya13d74f2013-03-24 08:54:55 -0500339 Backup_Display_Name = Display_Name;
340 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400341 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400342 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500343 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000344 Can_Encrypt_Backup = true;
345 Use_Userdata_Encryption = true;
Ethan Yonker6277c792014-09-15 14:54:30 -0500346 if (datamedia)
that9e0593e2014-10-08 00:01:24 +0200347 Setup_Data_Media();
Dees_Troy5bf43922012-09-07 16:07:55 -0400348#ifdef TW_INCLUDE_CRYPTO
349 Can_Be_Encrypted = true;
Dees_Troy657c3092012-09-10 20:32:10 -0400350 char crypto_blkdev[255];
351 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
352 if (strcmp(crypto_blkdev, "error") != 0) {
Dees_Troy657c3092012-09-10 20:32:10 -0400353 DataManager::SetValue(TW_IS_DECRYPTED, 1);
354 Is_Encrypted = true;
355 Is_Decrypted = true;
356 Decrypted_Block_Device = crypto_blkdev;
Dees_Troy2673cec2013-04-02 20:22:16 +0000357 LOGINFO("Data already decrypted, new block device: '%s'\n", crypto_blkdev);
Dees_Troy657c3092012-09-10 20:32:10 -0400358 } else if (!Mount(false)) {
Ethan Yonker71413f42014-02-26 13:36:08 -0600359 if (Is_Present) {
Ethan Yonker253368a2014-11-25 15:00:52 -0600360 set_partition_data(Actual_Block_Device.c_str(), Crypto_Key_Location.c_str(), Fstab_File_System.c_str());
Ethan Yonker71413f42014-02-26 13:36:08 -0600361 if (cryptfs_check_footer() == 0) {
362 Is_Encrypted = true;
363 Is_Decrypted = false;
364 Can_Be_Mounted = false;
365 Current_File_System = "emmc";
366 Setup_Image(Display_Error);
367 DataManager::SetValue(TW_IS_ENCRYPTED, 1);
Vojtech Bocek7e11ac52015-03-05 23:21:49 +0100368 DataManager::SetValue(TW_CRYPTO_PWTYPE, cryptfs_get_password_type());
Ethan Yonker71413f42014-02-26 13:36:08 -0600369 DataManager::SetValue(TW_CRYPTO_PASSWORD, "");
370 DataManager::SetValue("tw_crypto_display", "");
371 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500372 gui_err("mount_data_footer=Could not mount /data and unable to find crypto footer.");
Ethan Yonker71413f42014-02-26 13:36:08 -0600373 }
374 } else {
375 LOGERR("Primary block device '%s' for mount point '%s' is not present!\n", Primary_Block_Device.c_str(), Mount_Point.c_str());
376 }
Gary Peck82599a82012-11-21 16:23:12 -0800377 } else {
378 // Filesystem is not encrypted and the mount
379 // succeeded, so get it back to the original
380 // unmounted state
381 UnMount(false);
Dees_Troy51127312012-09-08 13:08:49 -0400382 }
Ethan Yonker6277c792014-09-15 14:54:30 -0500383 if (datamedia && (!Is_Encrypted || (Is_Encrypted && Is_Decrypted)))
Dees_Troy9b21af72012-10-01 15:51:46 -0400384 Recreate_Media_Folder();
Dees_Troy9b21af72012-10-01 15:51:46 -0400385#else
Ethan Yonker6277c792014-09-15 14:54:30 -0500386 if (datamedia)
387 Recreate_Media_Folder();
Dees_Troy5bf43922012-09-07 16:07:55 -0400388#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400389 } else if (Mount_Point == "/cache") {
390 Display_Name = "Cache";
Dees_Troya13d74f2013-03-24 08:54:55 -0500391 Backup_Display_Name = Display_Name;
392 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400393 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400394 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500395 Can_Be_Backed_Up = true;
Dees_Troyce2fe772012-09-28 12:34:33 -0400396 if (Mount(false) && !TWFunc::Path_Exists("/cache/recovery/.")) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000397 LOGINFO("Recreating /cache/recovery folder.\n");
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500398 if (mkdir("/cache/recovery", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0)
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500399 return -1;
Dees_Troyb46a6842012-09-25 11:06:46 -0400400 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400401 } else if (Mount_Point == "/datadata") {
Dees_Troy51127312012-09-08 13:08:49 -0400402 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400403 Display_Name = "DataData";
Dees_Troya13d74f2013-03-24 08:54:55 -0500404 Backup_Display_Name = Display_Name;
405 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400406 Is_SubPartition = true;
407 SubPartition_Of = "/data";
Dees_Troy5bf43922012-09-07 16:07:55 -0400408 DataManager::SetValue(TW_HAS_DATADATA, 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500409 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000410 Can_Encrypt_Backup = true;
411 Use_Userdata_Encryption = false; // This whole partition should be encrypted
Dees_Troy5bf43922012-09-07 16:07:55 -0400412 } else if (Mount_Point == "/sd-ext") {
Dees_Troy51127312012-09-08 13:08:49 -0400413 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400414 Display_Name = "SD-Ext";
Dees_Troya13d74f2013-03-24 08:54:55 -0500415 Backup_Display_Name = Display_Name;
416 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400417 Wipe_Available_in_GUI = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400418 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500419 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000420 Can_Encrypt_Backup = true;
421 Use_Userdata_Encryption = true;
Dees_Troy2c50e182012-09-26 20:05:28 -0400422 } else if (Mount_Point == "/boot") {
423 Display_Name = "Boot";
Dees_Troya13d74f2013-03-24 08:54:55 -0500424 Backup_Display_Name = Display_Name;
Dees_Troy2c50e182012-09-26 20:05:28 -0400425 DataManager::SetValue("tw_boot_is_mountable", 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500426 Can_Be_Backed_Up = true;
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500427 } else if (Mount_Point == "/vendor") {
428 Display_Name = "Vendor";
429 Backup_Display_Name = Display_Name;
430 Storage_Name = Display_Name;
431 Mount_Read_Only = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400432 }
433#ifdef TW_EXTERNAL_STORAGE_PATH
434 if (Mount_Point == EXPAND(TW_EXTERNAL_STORAGE_PATH)) {
435 Is_Storage = true;
436 Storage_Path = EXPAND(TW_EXTERNAL_STORAGE_PATH);
Dees_Troyc51f1f92012-09-20 15:32:13 -0400437 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500438 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400439#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000440 if (Mount_Point == "/sdcard" || Mount_Point == "/external_sd" || Mount_Point == "/external_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400441 Is_Storage = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400442 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500443 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400444#endif
Dees_Troyb05ddee2013-01-28 20:24:50 +0000445 }
Dees_Troy8170a922012-09-18 15:40:25 -0400446#ifdef TW_INTERNAL_STORAGE_PATH
447 if (Mount_Point == EXPAND(TW_INTERNAL_STORAGE_PATH)) {
448 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500449 Is_Settings_Storage = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400450 Storage_Path = EXPAND(TW_INTERNAL_STORAGE_PATH);
Dees_Troya13d74f2013-03-24 08:54:55 -0500451 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400452 }
453#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000454 if (Mount_Point == "/emmc" || Mount_Point == "/internal_sd" || Mount_Point == "/internal_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400455 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500456 Is_Settings_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500457 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400458 }
459#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400460 } else if (Is_Image(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400461 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400462 Setup_Image(Display_Error);
Dees_Troya13d74f2013-03-24 08:54:55 -0500463 if (Mount_Point == "/boot") {
464 Display_Name = "Boot";
465 Backup_Display_Name = Display_Name;
466 Can_Be_Backed_Up = true;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600467 Can_Flash_Img = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500468 } else if (Mount_Point == "/recovery") {
469 Display_Name = "Recovery";
470 Backup_Display_Name = Display_Name;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600471 Can_Flash_Img = true;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500472 } else if (Mount_Point == "/system_image") {
473 Display_Name = "System Image";
474 Backup_Display_Name = Display_Name;
HashBanged974bb2016-01-30 14:20:09 -0500475 Can_Flash_Img = true;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500476 Can_Be_Backed_Up = true;
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500477 } else if (Mount_Point == "/vendor_image") {
478 Display_Name = "Vendor Image";
479 Backup_Display_Name = Display_Name;
HashBanged974bb2016-01-30 14:20:09 -0500480 Can_Flash_Img = true;
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500481 Can_Be_Backed_Up = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500482 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400483 }
484
Matt Mower2416a502016-04-12 19:54:46 -0500485 // Process TWRP fstab flags
486 if (strlen(twflags) > 0) {
487 string Prev_Display_Name = Display_Name;
488 string Prev_Storage_Name = Storage_Name;
489 string Prev_Backup_Display_Name = Backup_Display_Name;
490 Display_Name = "";
491 Storage_Name = "";
492 Backup_Display_Name = "";
493
494 Process_TW_Flags(twflags, Display_Error);
495
496 bool has_display_name = !Display_Name.empty();
497 bool has_storage_name = !Storage_Name.empty();
498 bool has_backup_name = !Backup_Display_Name.empty();
499 if (!has_display_name) Display_Name = Prev_Display_Name;
500 if (!has_storage_name) Storage_Name = Prev_Storage_Name;
501 if (!has_backup_name) Backup_Display_Name = Prev_Backup_Display_Name;
502
503 if (has_display_name && !has_storage_name)
504 Storage_Name = Display_Name;
505 if (!has_display_name && has_storage_name)
506 Display_Name = Storage_Name;
507 if (has_display_name && !has_backup_name && Backup_Display_Name != "Android Secure")
508 Backup_Display_Name = Display_Name;
509 if (!has_display_name && has_backup_name)
510 Display_Name = Backup_Display_Name;
511 }
Dees_Troy51127312012-09-08 13:08:49 -0400512 return true;
513}
514
Matt Mower4ab42b12016-04-21 13:52:18 -0500515void TWPartition::Process_FS_Flags(const char *str) {
516 char *options = strdup(str);
517 char *ptr, *savep;
Hashcode62bd9e02013-11-19 21:59:42 -0800518
Matt Mower4ab42b12016-04-21 13:52:18 -0500519 Mount_Options = "";
Hashcode62bd9e02013-11-19 21:59:42 -0800520
Matt Mower4ab42b12016-04-21 13:52:18 -0500521 // Avoid issues with potentially nested strtok by using strtok_r
522 ptr = strtok_r(options, ",", &savep);
523 while (ptr) {
524 const struct flag_list* mount_flag = mount_flags;
525
526 for (; mount_flag->name; mount_flag++) {
527 // mount_flags are never postfixed by '=',
528 // so only match identical strings (including length)
529 if (strcmp(ptr, mount_flag->name) == 0) {
530 Mount_Flags |= mount_flag->flag;
Hashcode62bd9e02013-11-19 21:59:42 -0800531 break;
532 }
533 }
534
Matt Mower4ab42b12016-04-21 13:52:18 -0500535 if (mount_flag->flag == MS_RDONLY)
536 Mount_Read_Only = true;
Hashcode62bd9e02013-11-19 21:59:42 -0800537
Matt Mower4ab42b12016-04-21 13:52:18 -0500538 if (mount_flag->name != 0) {
539 if (!Mount_Options.empty())
540 Mount_Options += ",";
541 Mount_Options += mount_flag->name;
542 } else {
543 LOGINFO("Unhandled mount flag: '%s'\n", ptr);
544 }
545
546 ptr = strtok_r(NULL, ",", &savep);
547 }
548 free(options);
Hashcode62bd9e02013-11-19 21:59:42 -0800549}
550
Matt Mower2416a502016-04-12 19:54:46 -0500551void TWPartition::Apply_TW_Flag(const unsigned flag, const char* str, const bool val) {
552 switch (flag) {
553 case TWFLAG_ANDSEC:
554 Has_Android_Secure = val;
555 break;
556 case TWFLAG_BACKUP:
557 Can_Be_Backed_Up = val;
558 break;
559 case TWFLAG_BACKUPNAME:
560 Backup_Display_Name = str;
561 break;
562 case TWFLAG_BLOCKSIZE:
563 Format_Block_Size = (unsigned long)(atol(str));
564 break;
565 case TWFLAG_CANBEWIPED:
566 Can_Be_Wiped = val;
567 break;
568 case TWFLAG_CANENCRYPTBACKUP:
569 Can_Encrypt_Backup = val;
570 break;
571 case TWFLAG_DEFAULTS:
572 // Do nothing
573 break;
574 case TWFLAG_DISPLAY:
575 Display_Name = str;
576 break;
577 case TWFLAG_ENCRYPTABLE:
578 case TWFLAG_FORCEENCRYPT:
579 Crypto_Key_Location = str;
580 break;
581 case TWFLAG_FLASHIMG:
582 Can_Flash_Img = val;
583 break;
584 case TWFLAG_FSFLAGS:
Matt Mower4ab42b12016-04-21 13:52:18 -0500585 Process_FS_Flags(str);
Matt Mower2416a502016-04-12 19:54:46 -0500586 break;
587 case TWFLAG_IGNOREBLKID:
588 Ignore_Blkid = val;
589 break;
590 case TWFLAG_LENGTH:
591 Length = atoi(str);
592 break;
593 case TWFLAG_MOUNTTODECRYPT:
594 Mount_To_Decrypt = val;
595 break;
596 case TWFLAG_REMOVABLE:
597 Removable = val;
598 break;
599 case TWFLAG_RETAINLAYOUTVERSION:
600 Retain_Layout_Version = val;
601 break;
602 case TWFLAG_SETTINGSSTORAGE:
603 Is_Settings_Storage = val;
604 if (Is_Settings_Storage)
605 Is_Storage = true;
606 break;
607 case TWFLAG_STORAGE:
608 Is_Storage = val;
609 break;
610 case TWFLAG_STORAGENAME:
611 Storage_Name = str;
612 break;
613 case TWFLAG_SUBPARTITIONOF:
614 Is_SubPartition = true;
615 SubPartition_Of = str;
616 break;
617 case TWFLAG_SYMLINK:
618 Symlink_Path = str;
619 break;
620 case TWFLAG_USERDATAENCRYPTBACKUP:
621 Use_Userdata_Encryption = val;
622 if (Use_Userdata_Encryption)
623 Can_Encrypt_Backup = true;
624 break;
625 case TWFLAG_USERMRF:
626 Use_Rm_Rf = val;
627 break;
628 case TWFLAG_WIPEDURINGFACTORYRESET:
629 Wipe_During_Factory_Reset = val;
630 if (Wipe_During_Factory_Reset) {
631 Can_Be_Wiped = true;
632 Wipe_Available_in_GUI = true;
633 }
634 break;
635 case TWFLAG_WIPEINGUI:
636 Wipe_Available_in_GUI = val;
637 if (Wipe_Available_in_GUI)
638 Can_Be_Wiped = true;
639 break;
640 default:
641 // Should not get here
642 LOGINFO("Flag identified for processing, but later unmatched: %i\n", flag);
643 break;
644 }
645}
Dees_Troy51127312012-09-08 13:08:49 -0400646
Matt Mower2416a502016-04-12 19:54:46 -0500647void TWPartition::Process_TW_Flags(char *flags, bool Display_Error) {
648 char separator[2] = {'\n', 0};
649 char *ptr, *savep;
650
651 // Semicolons within double-quotes are not forbidden, so replace
652 // only the semicolons intended as separators with '\n' for strtok
653 for (unsigned i = 0, skip = 0; i < strlen(flags); i++) {
654 if (flags[i] == '\"')
Dees_Troya13d74f2013-03-24 08:54:55 -0500655 skip = !skip;
Matt Mower2416a502016-04-12 19:54:46 -0500656 if (!skip && flags[i] == ';')
657 flags[i] = separator[0];
Dees_Troy51127312012-09-08 13:08:49 -0400658 }
659
Matt Mower2416a502016-04-12 19:54:46 -0500660 // Avoid issues with potentially nested strtok by using strtok_r
661 ptr = strtok_r(flags, separator, &savep);
662 while (ptr) {
663 int ptr_len = strlen(ptr);
664 const struct flag_list* tw_flag = tw_flags;
Hashcode62bd9e02013-11-19 21:59:42 -0800665
Matt Mower2416a502016-04-12 19:54:46 -0500666 for (; tw_flag->name; tw_flag++) {
667 int flag_len = strlen(tw_flag->name);
668
669 if (strncmp(ptr, tw_flag->name, flag_len) == 0) {
670 bool flag_val = false;
671
672 if (ptr_len > flag_len && (tw_flag->name)[flag_len-1] != '='
673 && ptr[flag_len] != '=') {
674 // Handle flags with same starting string
675 // (e.g. backup and backupname)
676 continue;
677 } else if (ptr_len > flag_len && ptr[flag_len] == '=') {
678 // Handle flags with dual format: Part 1
679 // (e.g. backup and backup=y. backup=y handled here)
680 ptr += flag_len + 1;
681 TWFunc::Strip_Quotes(ptr);
682 // Skip flags with empty argument
683 // (e.g. backup=)
684 if (strlen(ptr) == 0) {
685 LOGINFO("Flag missing argument or should not include '=': %s=\n", tw_flag->name);
686 break;
687 }
688 flag_val = strchr("yY1", *ptr) != NULL;
689 } else if (ptr_len == flag_len
690 && (tw_flag->name)[flag_len-1] == '=') {
691 // Skip flags missing argument after =
692 // (e.g. backupname=)
693 LOGINFO("Flag missing argument: %s\n", tw_flag->name);
694 break;
695 } else if (ptr_len > flag_len
696 && (tw_flag->name)[flag_len-1] == '=') {
697 // Handle arguments to flags
698 // (e.g. backupname="My Stuff")
699 ptr += flag_len;
700 TWFunc::Strip_Quotes(ptr);
701 // Skip flags with empty argument
702 // (e.g. backupname="")
703 if (strlen(ptr) == 0) {
704 LOGINFO("Flag missing argument: %s\n", tw_flag->name);
705 break;
706 }
707 } else if (ptr_len == flag_len) {
708 // Handle flags with dual format: Part 2
709 // (e.g. backup and backup=y. backup handled here)
710 flag_val = true;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600711 } else {
Matt Mower2416a502016-04-12 19:54:46 -0500712 LOGINFO("Flag matched, but could not be processed: %s\n", ptr);
713 break;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600714 }
Matt Mower2416a502016-04-12 19:54:46 -0500715
716 Apply_TW_Flag(tw_flag->flag, ptr, flag_val);
717 break;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600718 }
Matt Mower2416a502016-04-12 19:54:46 -0500719 }
720 if (tw_flag->name == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400721 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000722 LOGERR("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400723 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000724 LOGINFO("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400725 }
Matt Mower2416a502016-04-12 19:54:46 -0500726 ptr = strtok_r(NULL, separator, &savep);
Dees_Troy51127312012-09-08 13:08:49 -0400727 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400728}
729
Dees_Troy5bf43922012-09-07 16:07:55 -0400730bool TWPartition::Is_File_System(string File_System) {
731 if (File_System == "ext2" ||
Dees_Troy63c8df72012-09-10 14:02:05 -0400732 File_System == "ext3" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400733 File_System == "ext4" ||
734 File_System == "vfat" ||
735 File_System == "ntfs" ||
736 File_System == "yaffs2" ||
bigbiff bigbiff3e146522012-11-14 14:32:59 -0500737 File_System == "exfat" ||
Dees_Troye5017042013-08-29 16:38:55 +0000738 File_System == "f2fs" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400739 File_System == "auto")
740 return true;
741 else
742 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400743}
744
Dees_Troy5bf43922012-09-07 16:07:55 -0400745bool TWPartition::Is_Image(string File_System) {
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400746 if (File_System == "emmc" || File_System == "mtd" || File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -0400747 return true;
748 else
749 return false;
750}
751
Dees_Troy51127312012-09-08 13:08:49 -0400752bool TWPartition::Make_Dir(string Path, bool Display_Error) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400753 if (!TWFunc::Path_Exists(Path)) {
Dees_Troy51127312012-09-08 13:08:49 -0400754 if (mkdir(Path.c_str(), 0777) == -1) {
755 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -0500756 gui_msg(Msg(msg::kError, "create_folder_strerr=Can not create '{1}' folder ({2}).")(Path)(strerror(errno)));
Dees_Troy51127312012-09-08 13:08:49 -0400757 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000758 LOGINFO("Can not create '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400759 return false;
760 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000761 LOGINFO("Created '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400762 return true;
763 }
764 }
765 return true;
766}
767
Dees_Troy5bf43922012-09-07 16:07:55 -0400768void TWPartition::Setup_File_System(bool Display_Error) {
769 struct statfs st;
770
771 Can_Be_Mounted = true;
772 Can_Be_Wiped = true;
773
Dees_Troy5bf43922012-09-07 16:07:55 -0400774 // Make the mount point folder if it doesn't exist
Dees_Troy51127312012-09-08 13:08:49 -0400775 Make_Dir(Mount_Point, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400776 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
777 Backup_Name = Display_Name;
778 Backup_Method = FILES;
779}
780
781void TWPartition::Setup_Image(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400782 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
783 Backup_Name = Display_Name;
Gary Peck82599a82012-11-21 16:23:12 -0800784 if (Current_File_System == "emmc")
Dees_Troy5bf43922012-09-07 16:07:55 -0400785 Backup_Method = DD;
Gary Peck82599a82012-11-21 16:23:12 -0800786 else if (Current_File_System == "mtd" || Current_File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -0400787 Backup_Method = FLASH_UTILS;
788 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000789 LOGINFO("Unhandled file system '%s' on image '%s'\n", Current_File_System.c_str(), Display_Name.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400790 if (Find_Partition_Size()) {
791 Used = Size;
792 Backup_Size = Size;
Dees_Troy51a0e822012-09-05 15:24:24 -0400793 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400794 if (Display_Error)
Dees Troyd932ce12013-10-18 17:12:59 +0000795 LOGERR("Unable to find partition size for '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400796 else
Dees Troyd932ce12013-10-18 17:12:59 +0000797 LOGINFO("Unable to find partition size for '%s'\n", Mount_Point.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400798 }
799}
800
Dees_Troye58d5262012-09-21 12:27:57 -0400801void TWPartition::Setup_AndSec(void) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500802 Backup_Display_Name = "Android Secure";
Dees_Troye58d5262012-09-21 12:27:57 -0400803 Backup_Name = "and-sec";
Dees_Troya13d74f2013-03-24 08:54:55 -0500804 Can_Be_Backed_Up = true;
Dees_Troye58d5262012-09-21 12:27:57 -0400805 Has_Android_Secure = true;
806 Symlink_Path = Mount_Point + "/.android_secure";
807 Symlink_Mount_Point = "/and-sec";
808 Backup_Path = Symlink_Mount_Point;
809 Make_Dir("/and-sec", true);
810 Recreate_AndSec_Folder();
Ethan Yonkerd4d10732014-02-03 15:27:52 -0600811 Mount_Storage_Retry();
Dees_Troye58d5262012-09-21 12:27:57 -0400812}
813
that9e0593e2014-10-08 00:01:24 +0200814void TWPartition::Setup_Data_Media() {
Ethan Yonker6277c792014-09-15 14:54:30 -0500815 LOGINFO("Setting up '%s' as data/media emulated storage.\n", Mount_Point.c_str());
Ethan Yonker66a19492015-12-10 10:19:45 -0600816 if (Storage_Name.empty() || Storage_Name == "Data")
817 Storage_Name = "Internal Storage";
Ethan Yonker6277c792014-09-15 14:54:30 -0500818 Has_Data_Media = true;
819 Is_Storage = true;
Ethan Yonker66a19492015-12-10 10:19:45 -0600820 Storage_Path = Mount_Point + "/media";
Ethan Yonker6277c792014-09-15 14:54:30 -0500821 Symlink_Path = Storage_Path;
Ethan Yonker66a19492015-12-10 10:19:45 -0600822 if (Mount_Point == "/data") {
823 Is_Settings_Storage = true;
824 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
825 Make_Dir("/emmc", false);
826 Symlink_Mount_Point = "/emmc";
827 } else {
828 Make_Dir("/sdcard", false);
829 Symlink_Mount_Point = "/sdcard";
830 }
831 if (Mount(false) && TWFunc::Path_Exists(Mount_Point + "/media/0")) {
832 Storage_Path = Mount_Point + "/media/0";
833 Symlink_Path = Storage_Path;
834 DataManager::SetValue(TW_INTERNAL_PATH, Mount_Point + "/media/0");
835 UnMount(true);
836 }
837 DataManager::SetValue("tw_has_internal", 1);
838 DataManager::SetValue("tw_has_data_media", 1);
839 du.add_absolute_dir(Mount_Point + "/misc/vold");
840 du.add_absolute_dir(Mount_Point + "/.layout_version");
841 du.add_absolute_dir(Mount_Point + "/system/storage.xml");
Ethan Yonker6277c792014-09-15 14:54:30 -0500842 } else {
Ethan Yonker66a19492015-12-10 10:19:45 -0600843 if (Mount(true) && TWFunc::Path_Exists(Mount_Point + "/media/0")) {
844 Storage_Path = Mount_Point + "/media/0";
845 Symlink_Path = Storage_Path;
846 UnMount(true);
847 }
Ethan Yonker6277c792014-09-15 14:54:30 -0500848 }
Ethan Yonker66a19492015-12-10 10:19:45 -0600849 du.add_absolute_dir(Mount_Point + "/media");
Ethan Yonker6277c792014-09-15 14:54:30 -0500850}
851
Dees_Troy5bf43922012-09-07 16:07:55 -0400852void TWPartition::Find_Real_Block_Device(string& Block, bool Display_Error) {
853 char device[512], realDevice[512];
854
855 strcpy(device, Block.c_str());
856 memset(realDevice, 0, sizeof(realDevice));
857 while (readlink(device, realDevice, sizeof(realDevice)) > 0)
858 {
859 strcpy(device, realDevice);
860 memset(realDevice, 0, sizeof(realDevice));
861 }
862
863 if (device[0] != '/') {
864 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000865 LOGERR("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400866 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000867 LOGINFO("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400868 return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400869 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400870 Block = device;
871 return;
872 }
873}
874
Dees_Troy8e337f32012-10-13 22:07:49 -0400875void TWPartition::Mount_Storage_Retry(void) {
876 // On some devices, storage doesn't want to mount right away, retry and sleep
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500877 if (!Mount(true)) {
Dees_Troy8e337f32012-10-13 22:07:49 -0400878 int retry_count = 5;
879 while (retry_count > 0 && !Mount(false)) {
880 usleep(500000);
881 retry_count--;
882 }
883 Mount(true);
884 }
885}
886
Dees_Troy38bd7602012-09-14 13:33:53 -0400887bool TWPartition::Find_MTD_Block_Device(string MTD_Name) {
888 FILE *fp = NULL;
889 char line[255];
890
891 fp = fopen("/proc/mtd", "rt");
892 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000893 LOGERR("Device does not support /proc/mtd\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400894 return false;
895 }
896
897 while (fgets(line, sizeof(line), fp) != NULL)
898 {
899 char device[32], label[32];
900 unsigned long size = 0;
901 char* fstype = NULL;
902 int deviceId;
903
904 sscanf(line, "%s %lx %*s %*c%s", device, &size, label);
905
906 // Skip header and blank lines
907 if ((strcmp(device, "dev:") == 0) || (strlen(line) < 8))
908 continue;
909
910 // Strip off the trailing " from the label
911 label[strlen(label)-1] = '\0';
912
913 if (strcmp(label, MTD_Name.c_str()) == 0) {
914 // We found our device
915 // Strip off the trailing : from the device
916 device[strlen(device)-1] = '\0';
917 if (sscanf(device,"mtd%d", &deviceId) == 1) {
918 sprintf(device, "/dev/block/mtdblock%d", deviceId);
919 Primary_Block_Device = device;
Dees_Troy76543db2013-06-19 16:24:30 +0000920 fclose(fp);
921 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -0400922 }
923 }
924 }
925 fclose(fp);
926
927 return false;
928}
929
Dees_Troy51127312012-09-08 13:08:49 -0400930bool TWPartition::Get_Size_Via_statfs(bool Display_Error) {
931 struct statfs st;
932 string Local_Path = Mount_Point + "/.";
933
934 if (!Mount(Display_Error))
935 return false;
936
937 if (statfs(Local_Path.c_str(), &st) != 0) {
938 if (!Removable) {
939 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000940 LOGERR("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400941 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000942 LOGINFO("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400943 }
944 return false;
945 }
946 Size = (st.f_blocks * st.f_bsize);
947 Used = ((st.f_blocks - st.f_bfree) * st.f_bsize);
948 Free = (st.f_bfree * st.f_bsize);
949 Backup_Size = Used;
950 return true;
951}
952
953bool TWPartition::Get_Size_Via_df(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400954 FILE* fp;
955 char command[255], line[512];
956 int include_block = 1;
957 unsigned int min_len;
958
959 if (!Mount(Display_Error))
960 return false;
961
Dees_Troy38bd7602012-09-14 13:33:53 -0400962 min_len = Actual_Block_Device.size() + 2;
Dees_Troy51127312012-09-08 13:08:49 -0400963 sprintf(command, "df %s > /tmp/dfoutput.txt", Mount_Point.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +0200964 TWFunc::Exec_Cmd(command);
Dees_Troy51127312012-09-08 13:08:49 -0400965 fp = fopen("/tmp/dfoutput.txt", "rt");
966 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000967 LOGINFO("Unable to open /tmp/dfoutput.txt.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400968 return false;
Dees_Troy51127312012-09-08 13:08:49 -0400969 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400970
971 while (fgets(line, sizeof(line), fp) != NULL)
972 {
973 unsigned long blocks, used, available;
974 char device[64];
975 char tmpString[64];
976
977 if (strncmp(line, "Filesystem", 10) == 0)
978 continue;
979 if (strlen(line) < min_len) {
980 include_block = 0;
981 continue;
982 }
983 if (include_block) {
984 sscanf(line, "%s %lu %lu %lu", device, &blocks, &used, &available);
985 } else {
986 // The device block string is so long that the df information is on the next line
987 int space_count = 0;
Dees_Troye58d5262012-09-21 12:27:57 -0400988 sprintf(tmpString, "/dev/block/%s", Actual_Block_Device.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400989 while (tmpString[space_count] == 32)
990 space_count++;
991 sscanf(line + space_count, "%lu %lu %lu", &blocks, &used, &available);
992 }
993
994 // Adjust block size to byte size
995 Size = blocks * 1024ULL;
996 Used = used * 1024ULL;
997 Free = available * 1024ULL;
998 Backup_Size = Used;
999 }
1000 fclose(fp);
1001 return true;
1002}
1003
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001004unsigned long long TWPartition::IOCTL_Get_Block_Size() {
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001005 Find_Actual_Block_Device();
Ethan Yonker483e9f42016-01-11 22:21:18 -06001006
1007 return TWFunc::IOCTL_Get_Block_Size(Actual_Block_Device.c_str());
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001008}
1009
Dees_Troy5bf43922012-09-07 16:07:55 -04001010bool TWPartition::Find_Partition_Size(void) {
1011 FILE* fp;
1012 char line[512];
1013 string tmpdevice;
1014
igoriok87e3d932013-01-31 21:03:53 +02001015 fp = fopen("/proc/dumchar_info", "rt");
1016 if (fp != NULL) {
1017 while (fgets(line, sizeof(line), fp) != NULL)
1018 {
1019 char label[32], device[32];
1020 unsigned long size = 0;
1021
thatd43bf2d2014-09-21 23:13:02 +02001022 sscanf(line, "%s %lx %*x %*u %s", label, &size, device);
igoriok87e3d932013-01-31 21:03:53 +02001023
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05001024 // Skip header, annotation and blank lines
igoriok87e3d932013-01-31 21:03:53 +02001025 if ((strncmp(device, "/dev/", 5) != 0) || (strlen(line) < 8))
1026 continue;
1027
1028 tmpdevice = "/dev/";
1029 tmpdevice += label;
1030 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
1031 Size = size;
1032 fclose(fp);
1033 return true;
1034 }
1035 }
1036 }
1037
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001038 unsigned long long ioctl_size = IOCTL_Get_Block_Size();
1039 if (ioctl_size) {
1040 Size = ioctl_size;
1041 return true;
1042 }
1043
Dees_Troy5bf43922012-09-07 16:07:55 -04001044 // In this case, we'll first get the partitions we care about (with labels)
1045 fp = fopen("/proc/partitions", "rt");
1046 if (fp == NULL)
1047 return false;
1048
1049 while (fgets(line, sizeof(line), fp) != NULL)
1050 {
1051 unsigned long major, minor, blocks;
1052 char device[512];
1053 char tmpString[64];
1054
Dees_Troy63c8df72012-09-10 14:02:05 -04001055 if (strlen(line) < 7 || line[0] == 'm') continue;
Dees_Troy5bf43922012-09-07 16:07:55 -04001056 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
1057
1058 tmpdevice = "/dev/block/";
1059 tmpdevice += device;
Dees_Troy38bd7602012-09-14 13:33:53 -04001060 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001061 // Adjust block size to byte size
1062 Size = blocks * 1024ULL;
1063 fclose(fp);
1064 return true;
1065 }
1066 }
1067 fclose(fp);
1068 return false;
1069}
1070
Dees_Troy5bf43922012-09-07 16:07:55 -04001071bool TWPartition::Is_Mounted(void) {
1072 if (!Can_Be_Mounted)
1073 return false;
1074
1075 struct stat st1, st2;
1076 string test_path;
1077
1078 // Check to see if the mount point directory exists
1079 test_path = Mount_Point + "/.";
1080 if (stat(test_path.c_str(), &st1) != 0) return false;
1081
1082 // Check to see if the directory above the mount point exists
1083 test_path = Mount_Point + "/../.";
1084 if (stat(test_path.c_str(), &st2) != 0) return false;
1085
1086 // Compare the device IDs -- if they match then we're (probably) using tmpfs instead of an actual device
1087 int ret = (st1.st_dev != st2.st_dev) ? true : false;
1088
1089 return ret;
1090}
1091
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05001092bool TWPartition::Is_File_System_Writable(void) {
1093 if (!Is_File_System(Current_File_System) || !Is_Mounted())
1094 return false;
1095
1096 string test_path = Mount_Point + "/.";
1097 return (access(test_path.c_str(), W_OK) == 0);
1098}
1099
Dees_Troy5bf43922012-09-07 16:07:55 -04001100bool TWPartition::Mount(bool Display_Error) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001101 int exfat_mounted = 0;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001102 unsigned long flags = Mount_Flags;
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001103
Dees_Troy5bf43922012-09-07 16:07:55 -04001104 if (Is_Mounted()) {
1105 return true;
1106 } else if (!Can_Be_Mounted) {
1107 return false;
1108 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001109
1110 Find_Actual_Block_Device();
1111
1112 // Check the current file system before mounting
1113 Check_FS_Type();
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001114 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/exfat-fuse")) {
Dees_Troye34c1332013-02-06 19:13:00 +00001115 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 +00001116 LOGINFO("cmd: %s\n", cmd.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001117 string result;
1118 if (TWFunc::Exec_Cmd(cmd, result) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001119 LOGINFO("exfat-fuse failed to mount with result '%s', trying vfat\n", result.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001120 Current_File_System = "vfat";
1121 } else {
1122#ifdef TW_NO_EXFAT_FUSE
1123 UnMount(false);
1124 // We'll let the kernel handle it but using exfat-fuse to detect if the file system is actually exfat
1125 // Some kernels let us mount vfat as exfat which doesn't work out too well
1126#else
1127 exfat_mounted = 1;
1128#endif
1129 }
1130 }
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001131
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001132 if (Current_File_System == "ntfs" && (TWFunc::Path_Exists("/sbin/ntfs-3g") || TWFunc::Path_Exists("/sbin/mount.ntfs"))) {
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001133 string cmd;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001134 string Ntfsmount_Binary = "";
1135
1136 if (TWFunc::Path_Exists("/sbin/ntfs-3g"))
1137 Ntfsmount_Binary = "ntfs-3g";
1138 else if (TWFunc::Path_Exists("/sbin/mount.ntfs"))
1139 Ntfsmount_Binary = "mount.ntfs";
1140
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001141 if (Mount_Read_Only)
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001142 cmd = "/sbin/" + Ntfsmount_Binary + " -o ro " + Actual_Block_Device + " " + Mount_Point;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001143 else
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001144 cmd = "/sbin/" + Ntfsmount_Binary + " " + Actual_Block_Device + " " + Mount_Point;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001145 LOGINFO("cmd: '%s'\n", cmd.c_str());
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001146
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001147 if (TWFunc::Exec_Cmd(cmd) == 0) {
1148 return true;
1149 } else {
1150 LOGINFO("ntfs-3g failed to mount, trying regular mount method.\n");
1151 }
1152 }
1153
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001154 if (Mount_Read_Only)
1155 flags |= MS_RDONLY;
1156
Dees_Troy22042032012-12-18 21:23:08 +00001157 if (Fstab_File_System == "yaffs2") {
1158 // mount an MTD partition as a YAFFS2 filesystem.
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001159 flags = MS_NOATIME | MS_NODEV | MS_NODIRATIME;
1160 if (Mount_Read_Only)
1161 flags |= MS_RDONLY;
Dees_Troy76543db2013-06-19 16:24:30 +00001162 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags, NULL) < 0) {
1163 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags | MS_RDONLY, NULL) < 0) {
1164 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001165 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy76543db2013-06-19 16:24:30 +00001166 else
1167 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
1168 return false;
1169 } else {
1170 LOGINFO("Mounted '%s' (MTD) as RO\n", Mount_Point.c_str());
1171 return true;
1172 }
1173 } else {
1174 struct stat st;
1175 string test_path = Mount_Point;
1176 if (stat(test_path.c_str(), &st) < 0) {
1177 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001178 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy76543db2013-06-19 16:24:30 +00001179 else
1180 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
1181 return false;
1182 }
1183 mode_t new_mode = st.st_mode | S_IXUSR | S_IXGRP | S_IXOTH;
1184 if (new_mode != st.st_mode) {
1185 LOGINFO("Fixing execute permissions for %s\n", Mount_Point.c_str());
1186 if (chmod(Mount_Point.c_str(), new_mode) < 0) {
1187 if (Display_Error)
1188 LOGERR("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
1189 else
1190 LOGINFO("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
1191 return false;
1192 }
1193 }
Dees_Troy22042032012-12-18 21:23:08 +00001194 return true;
Dees_Troy76543db2013-06-19 16:24:30 +00001195 }
thatc7572eb2015-03-31 18:55:30 +02001196 }
1197
1198 string mount_fs = Current_File_System;
1199 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sys/module/texfat"))
1200 mount_fs = "texfat";
1201
1202 if (!exfat_mounted &&
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001203 mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), mount_fs.c_str(), flags, Mount_Options.c_str()) != 0 &&
1204 mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), mount_fs.c_str(), flags, NULL) != 0) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001205#ifdef TW_NO_EXFAT_FUSE
1206 if (Current_File_System == "exfat") {
Dees_Troy2673cec2013-04-02 20:22:16 +00001207 LOGINFO("Mounting exfat failed, trying vfat...\n");
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001208 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), "vfat", 0, NULL) != 0) {
Dees_Troy85f44ed2013-01-09 18:42:36 +00001209 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001210 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy85f44ed2013-01-09 18:42:36 +00001211 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001212 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001213 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 +00001214 return false;
Dees_Troy85f44ed2013-01-09 18:42:36 +00001215 }
Dees_Troyb05ddee2013-01-28 20:24:50 +00001216 } else {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001217#endif
bigbiff bigbiff26774a02014-03-29 18:22:00 -04001218 if (!Removable && Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001219 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001220 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001221 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
1222 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 +00001223 return false;
1224#ifdef TW_NO_EXFAT_FUSE
Dees_Troy85f44ed2013-01-09 18:42:36 +00001225 }
1226#endif
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001227 }
Ethan Yonker253368a2014-11-25 15:00:52 -06001228
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001229 if (Removable)
1230 Update_Size(Display_Error);
1231
xiaolu9416f4f2015-06-04 08:22:23 +08001232 if (!Symlink_Mount_Point.empty() && TWFunc::Path_Exists(Symlink_Path)) {
codelover2a3d4ce2015-03-14 20:26:49 +08001233 string Command = "mount -o bind '" + Symlink_Path + "' '" + Symlink_Mount_Point + "'";
Vojtech Bocek05534202013-09-11 08:11:56 +02001234 TWFunc::Exec_Cmd(Command);
Dees_Troy5bf43922012-09-07 16:07:55 -04001235 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001236 return true;
1237}
1238
1239bool TWPartition::UnMount(bool Display_Error) {
1240 if (Is_Mounted()) {
1241 int never_unmount_system;
1242
1243 DataManager::GetValue(TW_DONT_UNMOUNT_SYSTEM, never_unmount_system);
1244 if (never_unmount_system == 1 && Mount_Point == "/system")
1245 return true; // Never unmount system if you're not supposed to unmount it
1246
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001247 if (Is_Storage)
Ethan Yonker726a0202014-12-16 20:01:38 -06001248 PartitionManager.Remove_MTP_Storage(MTP_Storage_ID);
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001249
Dees_Troy38bd7602012-09-14 13:33:53 -04001250 if (!Symlink_Mount_Point.empty())
1251 umount(Symlink_Mount_Point.c_str());
1252
Dees_Troyb05ddee2013-01-28 20:24:50 +00001253 umount(Mount_Point.c_str());
1254 if (Is_Mounted()) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001255 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001256 gui_msg(Msg(msg::kError, "fail_unmount=Failed to unmount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy5bf43922012-09-07 16:07:55 -04001257 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001258 LOGINFO("Unable to unmount '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001259 return false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001260 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -04001261 return true;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001262 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001263 } else {
1264 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001265 }
1266}
1267
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05001268bool TWPartition::ReMount(bool Display_Error) {
1269 if (UnMount(Display_Error))
1270 return Mount(Display_Error);
1271 return false;
1272}
1273
1274bool TWPartition::ReMount_RW(bool Display_Error) {
1275 // No need to remount if already mounted rw
1276 if (Is_File_System_Writable())
1277 return true;
1278
1279 bool ro = Mount_Read_Only;
1280 int flags = Mount_Flags;
1281
1282 Mount_Read_Only = false;
1283 Mount_Flags &= ~MS_RDONLY;
1284
1285 bool ret = ReMount(Display_Error);
1286
1287 Mount_Read_Only = ro;
1288 Mount_Flags = flags;
1289
1290 return ret;
1291}
1292
Gary Peck43acadf2012-11-21 21:19:01 -08001293bool TWPartition::Wipe(string New_File_System) {
Ethan Yonker726a0202014-12-16 20:01:38 -06001294 bool wiped = false, update_crypt = false, recreate_media = true;
Dees_Troy16c2b312013-01-15 16:51:18 +00001295 int check;
1296 string Layout_Filename = Mount_Point + "/.layout_version";
1297
Dees_Troy38bd7602012-09-14 13:33:53 -04001298 if (!Can_Be_Wiped) {
Matt Mower3c366972015-12-25 19:28:31 -06001299 gui_msg(Msg(msg::kError, "cannot_wipe=Partition {1} cannot be wiped.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04001300 return false;
1301 }
1302
Dees_Troyc51f1f92012-09-20 15:32:13 -04001303 if (Mount_Point == "/cache")
Dees_Troy2673cec2013-04-02 20:22:16 +00001304 Log_Offset = 0;
Dees_Troyc51f1f92012-09-20 15:32:13 -04001305
Dees_Troy16c2b312013-01-15 16:51:18 +00001306 if (Retain_Layout_Version && Mount(false) && TWFunc::Path_Exists(Layout_Filename))
1307 TWFunc::copy_file(Layout_Filename, "/.layout_version", 0600);
1308 else
1309 unlink("/.layout_version");
Dees_Troy38bd7602012-09-14 13:33:53 -04001310
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001311 if (Has_Data_Media && Current_File_System == New_File_System) {
Dees_Troy16c2b312013-01-15 16:51:18 +00001312 wiped = Wipe_Data_Without_Wiping_Media();
Ethan Yonker5eac2222014-06-11 12:22:55 -05001313 recreate_media = false;
Dees_Troy16c2b312013-01-15 16:51:18 +00001314 } else {
Dees_Troy16c2b312013-01-15 16:51:18 +00001315 DataManager::GetValue(TW_RM_RF_VAR, check);
1316
Hashcodedabfd492013-08-29 22:45:30 -07001317 if (check || Use_Rm_Rf)
Dees_Troy16c2b312013-01-15 16:51:18 +00001318 wiped = Wipe_RMRF();
1319 else if (New_File_System == "ext4")
1320 wiped = Wipe_EXT4();
1321 else if (New_File_System == "ext2" || New_File_System == "ext3")
1322 wiped = Wipe_EXT23(New_File_System);
1323 else if (New_File_System == "vfat")
1324 wiped = Wipe_FAT();
1325 else if (New_File_System == "exfat")
1326 wiped = Wipe_EXFAT();
1327 else if (New_File_System == "yaffs2")
1328 wiped = Wipe_MTD();
Dees_Troye5017042013-08-29 16:38:55 +00001329 else if (New_File_System == "f2fs")
1330 wiped = Wipe_F2FS();
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001331 else if (New_File_System == "ntfs")
1332 wiped = Wipe_NTFS();
Dees_Troy16c2b312013-01-15 16:51:18 +00001333 else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001334 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 +00001335 unlink("/.layout_version");
1336 return false;
1337 }
1338 update_crypt = wiped;
Gary Pecke8bc5d72012-12-21 06:45:25 -08001339 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001340
Gary Pecke8bc5d72012-12-21 06:45:25 -08001341 if (wiped) {
Dees_Troy1c1ac442013-01-17 21:42:14 +00001342 if (Mount_Point == "/cache")
1343 DataManager::Output_Version();
1344
Dees_Troy16c2b312013-01-15 16:51:18 +00001345 if (TWFunc::Path_Exists("/.layout_version") && Mount(false))
1346 TWFunc::copy_file("/.layout_version", Layout_Filename, 0600);
1347
1348 if (update_crypt) {
1349 Setup_File_System(false);
1350 if (Is_Encrypted && !Is_Decrypted) {
1351 // just wiped an encrypted partition back to its unencrypted state
1352 Is_Encrypted = false;
1353 Is_Decrypted = false;
1354 Decrypted_Block_Device = "";
1355 if (Mount_Point == "/data") {
1356 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1357 DataManager::SetValue(TW_IS_DECRYPTED, 0);
1358 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001359 }
1360 }
Ethan Yonker5eac2222014-06-11 12:22:55 -05001361
Ethan Yonker66a19492015-12-10 10:19:45 -06001362 if (Has_Data_Media && recreate_media) {
Ethan Yonker5eac2222014-06-11 12:22:55 -05001363 Recreate_Media_Folder();
1364 }
Matt Mower209c9632016-01-20 12:08:35 -06001365 if (Is_Storage && Mount(false))
1366 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Gary Pecke8bc5d72012-12-21 06:45:25 -08001367 }
Matt Mower209c9632016-01-20 12:08:35 -06001368
Gary Pecke8bc5d72012-12-21 06:45:25 -08001369 return wiped;
Dees_Troy51a0e822012-09-05 15:24:24 -04001370}
1371
Gary Peck43acadf2012-11-21 21:19:01 -08001372bool TWPartition::Wipe() {
Gary Peck82599a82012-11-21 16:23:12 -08001373 if (Is_File_System(Current_File_System))
1374 return Wipe(Current_File_System);
1375 else
1376 return Wipe(Fstab_File_System);
Gary Peck43acadf2012-11-21 21:19:01 -08001377}
1378
Dees_Troye58d5262012-09-21 12:27:57 -04001379bool TWPartition::Wipe_AndSec(void) {
1380 if (!Has_Android_Secure)
1381 return false;
1382
Dees_Troye58d5262012-09-21 12:27:57 -04001383 if (!Mount(true))
1384 return false;
1385
Ethan Yonker74db1572015-10-28 12:44:49 -05001386 gui_msg(Msg("wiping=Wiping {1}")(Backup_Display_Name));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001387 TWFunc::removeDir(Mount_Point + "/.android_secure/", true);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001388 return true;
Dees_Troye58d5262012-09-21 12:27:57 -04001389}
1390
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001391bool TWPartition::Can_Repair() {
Ethan Yonkera2719152015-05-28 09:44:41 -05001392 if (Mount_Read_Only)
1393 return false;
Matt Mower18794c82015-11-11 16:22:45 -06001394 if (Current_File_System == "vfat" && TWFunc::Path_Exists("/sbin/fsck.fat"))
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001395 return true;
1396 else if ((Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") && TWFunc::Path_Exists("/sbin/e2fsck"))
1397 return true;
1398 else if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/fsck.exfat"))
1399 return true;
1400 else if (Current_File_System == "f2fs" && TWFunc::Path_Exists("/sbin/fsck.f2fs"))
1401 return true;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001402 else if (Current_File_System == "ntfs" && (TWFunc::Path_Exists("/sbin/ntfsfix") || TWFunc::Path_Exists("/sbin/fsck.ntfs")))
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001403 return true;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001404 return false;
1405}
1406
1407bool TWPartition::Repair() {
1408 string command;
1409
1410 if (Current_File_System == "vfat") {
Matt Mower18794c82015-11-11 16:22:45 -06001411 if (!TWFunc::Path_Exists("/sbin/fsck.fat")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001412 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.fat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001413 return false;
1414 }
1415 if (!UnMount(true))
1416 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001417 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.fat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001418 Find_Actual_Block_Device();
Matt Mower18794c82015-11-11 16:22:45 -06001419 command = "/sbin/fsck.fat -y " + Actual_Block_Device;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001420 LOGINFO("Repair command: %s\n", command.c_str());
1421 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001422 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001423 return true;
1424 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001425 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001426 return false;
1427 }
1428 }
1429 if (Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") {
1430 if (!TWFunc::Path_Exists("/sbin/e2fsck")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001431 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("e2fsck"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001432 return false;
1433 }
1434 if (!UnMount(true))
1435 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001436 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("e2fsck"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001437 Find_Actual_Block_Device();
Ethan Yonkera2719152015-05-28 09:44:41 -05001438 command = "/sbin/e2fsck -fp " + Actual_Block_Device;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001439 LOGINFO("Repair command: %s\n", command.c_str());
1440 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001441 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001442 return true;
1443 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001444 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001445 return false;
1446 }
1447 }
1448 if (Current_File_System == "exfat") {
1449 if (!TWFunc::Path_Exists("/sbin/fsck.exfat")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001450 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.exfat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001451 return false;
1452 }
1453 if (!UnMount(true))
1454 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001455 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.exfat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001456 Find_Actual_Block_Device();
1457 command = "/sbin/fsck.exfat " + Actual_Block_Device;
1458 LOGINFO("Repair command: %s\n", command.c_str());
1459 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001460 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001461 return true;
1462 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001463 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001464 return false;
1465 }
1466 }
1467 if (Current_File_System == "f2fs") {
1468 if (!TWFunc::Path_Exists("/sbin/fsck.f2fs")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001469 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.f2fs"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001470 return false;
1471 }
1472 if (!UnMount(true))
1473 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001474 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.f2fs"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001475 Find_Actual_Block_Device();
1476 command = "/sbin/fsck.f2fs " + Actual_Block_Device;
1477 LOGINFO("Repair command: %s\n", command.c_str());
1478 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001479 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001480 return true;
1481 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001482 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001483 return false;
1484 }
1485 }
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001486 if (Current_File_System == "ntfs") {
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001487 string Ntfsfix_Binary;
1488 if (TWFunc::Path_Exists("/sbin/ntfsfix"))
1489 Ntfsfix_Binary = "ntfsfix";
1490 else if (TWFunc::Path_Exists("/sbin/fsck.ntfs"))
1491 Ntfsfix_Binary = "fsck.ntfs";
1492 else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001493 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("ntfsfix"));
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001494 return false;
1495 }
1496 if (!UnMount(true))
1497 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001498 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)(Ntfsfix_Binary));
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001499 Find_Actual_Block_Device();
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001500 command = "/sbin/" + Ntfsfix_Binary + " " + Actual_Block_Device;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001501 LOGINFO("Repair command: %s\n", command.c_str());
1502 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001503 gui_msg("done=Done.");
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001504 return true;
1505 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001506 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001507 return false;
1508 }
1509 }
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001510 return false;
1511}
1512
Ethan Yonkera2719152015-05-28 09:44:41 -05001513bool TWPartition::Can_Resize() {
1514 if (Mount_Read_Only)
1515 return false;
1516 if ((Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") && TWFunc::Path_Exists("/sbin/resize2fs"))
1517 return true;
1518 return false;
1519}
1520
1521bool TWPartition::Resize() {
1522 string command;
1523
1524 if (Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") {
1525 if (!Can_Repair()) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001526 LOGINFO("Cannot resize %s because %s cannot be repaired before resizing.\n", Display_Name.c_str(), Display_Name.c_str());
1527 gui_msg(Msg(msg::kError, "cannot_resize=Cannot resize {1}.")(Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001528 return false;
1529 }
1530 if (!TWFunc::Path_Exists("/sbin/resize2fs")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001531 LOGINFO("resize2fs does not exist! Cannot resize!\n");
1532 gui_msg(Msg(msg::kError, "cannot_resize=Cannot resize {1}.")(Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001533 return false;
1534 }
1535 // Repair will unmount so no need to do it twice
Ethan Yonker74db1572015-10-28 12:44:49 -05001536 gui_msg(Msg("repair_resize=Repairing {1} before resizing.")( Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001537 if (!Repair())
1538 return false;
Ethan Yonker74db1572015-10-28 12:44:49 -05001539 gui_msg(Msg("resizing=Resizing {1} using {2}...")(Display_Name)("resize2fs"));
Ethan Yonkera2719152015-05-28 09:44:41 -05001540 Find_Actual_Block_Device();
1541 command = "/sbin/resize2fs " + Actual_Block_Device;
1542 if (Length != 0) {
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001543 unsigned long long Actual_Size = IOCTL_Get_Block_Size();
1544 if (Actual_Size == 0)
1545 return false;
Ethan Yonkera2719152015-05-28 09:44:41 -05001546
Ethan Yonkera2719152015-05-28 09:44:41 -05001547 unsigned long long Block_Count;
1548 if (Length < 0) {
1549 // Reduce overall size by this length
1550 Block_Count = (Actual_Size / 1024LLU) - ((unsigned long long)(Length * -1) / 1024LLU);
1551 } else {
1552 // This is the size, not a size reduction
1553 Block_Count = ((unsigned long long)(Length) / 1024LLU);
1554 }
1555 char temp[256];
1556 sprintf(temp, "%llu", Block_Count);
1557 command += " ";
1558 command += temp;
1559 command += "K";
1560 }
1561 LOGINFO("Resize command: %s\n", command.c_str());
1562 if (TWFunc::Exec_Cmd(command) == 0) {
1563 Update_Size(true);
Ethan Yonker74db1572015-10-28 12:44:49 -05001564 gui_msg("done=Done.");
Ethan Yonkera2719152015-05-28 09:44:41 -05001565 return true;
1566 } else {
1567 Update_Size(true);
Ethan Yonker74db1572015-10-28 12:44:49 -05001568 gui_msg(Msg(msg::kError, "unable_resize=Unable to resize {1}.")(Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001569 return false;
1570 }
1571 }
1572 return false;
1573}
1574
Ethan Yonker472f5062016-02-25 13:47:30 -06001575bool TWPartition::Backup(const string& backup_folder, pid_t &tar_fork_pid, ProgressTracking *progress) {
bigbiff7abc5fe2015-01-17 16:53:12 -05001576 if (Backup_Method == FILES) {
Ethan Yonker472f5062016-02-25 13:47:30 -06001577 return Backup_Tar(backup_folder, progress, tar_fork_pid);
bigbiff7abc5fe2015-01-17 16:53:12 -05001578 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001579 else if (Backup_Method == DD)
Ethan Yonker472f5062016-02-25 13:47:30 -06001580 return Backup_Image(backup_folder, progress);
Dees_Troy38bd7602012-09-14 13:33:53 -04001581 else if (Backup_Method == FLASH_UTILS)
Ethan Yonker472f5062016-02-25 13:47:30 -06001582 return Backup_Dump_Image(backup_folder, progress);
Dees_Troy2673cec2013-04-02 20:22:16 +00001583 LOGERR("Unknown backup method for '%s'\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001584 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001585}
1586
Dees_Troy43d8b002012-09-17 16:00:01 -04001587bool TWPartition::Check_MD5(string restore_folder) {
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001588 string Full_Filename, md5file;
Dees_Troy43d8b002012-09-17 16:00:01 -04001589 char split_filename[512];
1590 int index = 0;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001591 twrpDigest md5sum;
Dees_Troy43d8b002012-09-17 16:00:01 -04001592
Captain Throwbackff5935f2014-09-23 16:08:34 -04001593 sync();
1594
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001595 memset(split_filename, 0, sizeof(split_filename));
Dees_Troy43d8b002012-09-17 16:00:01 -04001596 Full_Filename = restore_folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04001597 if (!TWFunc::Path_Exists(Full_Filename)) {
1598 // This is a split archive, we presume
1599 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
Dees_Troy2673cec2013-04-02 20:22:16 +00001600 LOGINFO("split_filename: %s\n", split_filename);
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001601 md5file = split_filename;
1602 md5file += ".md5";
1603 if (!TWFunc::Path_Exists(md5file)) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001604 gui_msg(Msg(msg::kError, "no_md5_found=No md5 file found for '{1}'. Please unselect Enable MD5 verification to restore.")(split_filename));
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001605 return false;
1606 }
1607 md5sum.setfn(split_filename);
Dees_Troy83bd4832013-05-04 12:39:56 +00001608 while (index < 1000) {
1609 if (TWFunc::Path_Exists(split_filename) && md5sum.verify_md5digest() != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001610 gui_msg(Msg(msg::kError, "md5_fail_match=MD5 failed to match on '{1}'.")(split_filename));
Dees_Troy43d8b002012-09-17 16:00:01 -04001611 return false;
1612 }
1613 index++;
1614 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001615 md5sum.setfn(split_filename);
Dees_Troy43d8b002012-09-17 16:00:01 -04001616 }
Dees_Troy4a2a1262012-09-18 09:33:47 -04001617 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001618 } else {
1619 // Single file archive
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001620 md5file = Full_Filename + ".md5";
1621 if (!TWFunc::Path_Exists(md5file)) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001622 gui_msg(Msg(msg::kError, "no_md5_found=No md5 file found for '{1}'. Please unselect Enable MD5 verification to restore.")(split_filename));
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001623 return false;
1624 }
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001625 md5sum.setfn(Full_Filename);
1626 if (md5sum.verify_md5digest() != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001627 gui_msg(Msg(msg::kError, "md5_fail_match=MD5 failed to match on '{1}'.")(split_filename));
Dees_Troy43d8b002012-09-17 16:00:01 -04001628 return false;
1629 } else
1630 return true;
1631 }
1632 return false;
1633}
1634
Ethan Yonker472f5062016-02-25 13:47:30 -06001635bool TWPartition::Restore(const string& restore_folder, ProgressTracking *progress) {
Gary Peck43acadf2012-11-21 21:19:01 -08001636 string Restore_File_System;
1637
Matt Mower3c366972015-12-25 19:28:31 -06001638 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, gui_parse_text("{@restoring_hdr}"));
Dees_Troy2673cec2013-04-02 20:22:16 +00001639 LOGINFO("Restore filename is: %s\n", Backup_FileName.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001640
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001641 Restore_File_System = Get_Restore_File_System(restore_folder);
1642
1643 if (Is_File_System(Restore_File_System))
Ethan Yonker472f5062016-02-25 13:47:30 -06001644 return Restore_Tar(restore_folder, Restore_File_System, progress);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001645 else if (Is_Image(Restore_File_System)) {
Ethan Yonker472f5062016-02-25 13:47:30 -06001646 return Restore_Image(restore_folder, Restore_File_System, progress);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001647 }
1648
1649 LOGERR("Unknown restore method for '%s'\n", Mount_Point.c_str());
1650 return false;
1651}
1652
Ethan Yonker472f5062016-02-25 13:47:30 -06001653string TWPartition::Get_Restore_File_System(const string& restore_folder) {
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001654 size_t first_period, second_period;
1655 string Restore_File_System;
1656
Gary Peck43acadf2012-11-21 21:19:01 -08001657 // Parse backup filename to extract the file system before wiping
1658 first_period = Backup_FileName.find(".");
1659 if (first_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001660 LOGERR("Unable to find file system (first period).\n");
thatd43bf2d2014-09-21 23:13:02 +02001661 return string();
Gary Peck43acadf2012-11-21 21:19:01 -08001662 }
1663 Restore_File_System = Backup_FileName.substr(first_period + 1, Backup_FileName.size() - first_period - 1);
1664 second_period = Restore_File_System.find(".");
1665 if (second_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001666 LOGERR("Unable to find file system (second period).\n");
thatd43bf2d2014-09-21 23:13:02 +02001667 return string();
Gary Peck43acadf2012-11-21 21:19:01 -08001668 }
1669 Restore_File_System.resize(second_period);
Dees_Troy2673cec2013-04-02 20:22:16 +00001670 LOGINFO("Restore file system is: '%s'.\n", Restore_File_System.c_str());
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001671 return Restore_File_System;
Dees_Troy51a0e822012-09-05 15:24:24 -04001672}
1673
1674string TWPartition::Backup_Method_By_Name() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001675 if (Backup_Method == NONE)
1676 return "none";
1677 else if (Backup_Method == FILES)
1678 return "files";
1679 else if (Backup_Method == DD)
1680 return "dd";
1681 else if (Backup_Method == FLASH_UTILS)
1682 return "flash_utils";
1683 else
1684 return "undefined";
1685 return "ERROR!";
Dees_Troy51a0e822012-09-05 15:24:24 -04001686}
1687
1688bool TWPartition::Decrypt(string Password) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001689 LOGINFO("STUB TWPartition::Decrypt, password: '%s'\n", Password.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001690 // Is this needed?
Dees_Troy51a0e822012-09-05 15:24:24 -04001691 return 1;
1692}
1693
1694bool TWPartition::Wipe_Encryption() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001695 bool Save_Data_Media = Has_Data_Media;
1696
1697 if (!UnMount(true))
1698 return false;
1699
Dees_Troy38bd7602012-09-14 13:33:53 -04001700 Has_Data_Media = false;
Dees_Troy74fb2e92013-04-15 14:35:47 +00001701 Decrypted_Block_Device = "";
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001702#ifdef TW_INCLUDE_CRYPTO
1703 if (Is_Decrypted) {
1704 if (!UnMount(true))
1705 return false;
Matt Mower2b18a532015-02-20 16:58:05 -06001706 if (delete_crypto_blk_dev((char*)("userdata")) != 0) {
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001707 LOGERR("Error deleting crypto block device, continuing anyway.\n");
1708 }
1709 }
1710#endif
Dees_Troy74fb2e92013-04-15 14:35:47 +00001711 Is_Decrypted = false;
1712 Is_Encrypted = false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001713 Find_Actual_Block_Device();
Ethan Yonkerbc85b632015-08-09 12:48:14 -05001714 if (Crypto_Key_Location == "footer") {
1715 int newlen, fd;
1716 if (Length != 0) {
1717 newlen = Length;
1718 if (newlen < 0)
1719 newlen = newlen * -1;
1720 } else {
1721 newlen = CRYPT_FOOTER_OFFSET;
1722 }
1723 if ((fd = open(Actual_Block_Device.c_str(), O_RDWR)) < 0) {
1724 gui_print_color("warning", "Unable to open '%s' to wipe crypto key\n", Actual_Block_Device.c_str());
1725 } else {
1726 unsigned int block_count;
1727 if ((ioctl(fd, BLKGETSIZE, &block_count)) == -1) {
1728 gui_print_color("warning", "Unable to get block size for wiping crypto footer.\n");
1729 } else {
1730 off64_t offset = ((off64_t)block_count * 512) - newlen;
1731 if (lseek64(fd, offset, SEEK_SET) == -1) {
1732 gui_print_color("warning", "Unable to lseek64 for wiping crypto footer.\n");
1733 } else {
1734 void* buffer = malloc(newlen);
1735 if (!buffer) {
1736 gui_print_color("warning", "Failed to malloc for wiping crypto footer.\n");
1737 } else {
1738 memset(buffer, 0, newlen);
1739 int ret = write(fd, buffer, newlen);
1740 if (ret != newlen) {
1741 gui_print_color("warning", "Failed to wipe crypto footer.\n");
1742 } else {
1743 LOGINFO("Successfully wiped crypto footer.\n");
1744 }
Matt Mower13a8f0b2015-09-26 15:40:03 -05001745 free(buffer);
Ethan Yonkerbc85b632015-08-09 12:48:14 -05001746 }
1747 }
1748 }
1749 close(fd);
1750 }
1751 } else {
Ethan Yonkerc2dafbb2016-03-15 22:20:59 -05001752 if (TWFunc::IOCTL_Get_Block_Size(Crypto_Key_Location.c_str()) >= 16384LLU) {
1753 string Command = "dd of='" + Crypto_Key_Location + "' if=/dev/zero bs=16384 count=1";
1754 TWFunc::Exec_Cmd(Command);
1755 } else {
1756 LOGINFO("Crypto key location reports size < 16K so not wiping crypto footer.\n");
1757 }
Ethan Yonkerbc85b632015-08-09 12:48:14 -05001758 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001759 if (Wipe(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001760 Has_Data_Media = Save_Data_Media;
1761 if (Has_Data_Media && !Symlink_Mount_Point.empty()) {
1762 Recreate_Media_Folder();
Ethan Yonker726a0202014-12-16 20:01:38 -06001763 if (Mount(false))
1764 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04001765 }
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001766 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
Ethan Yonker83e82572014-04-04 10:59:28 -05001767#ifndef TW_OEM_BUILD
Ethan Yonker74db1572015-10-28 12:44:49 -05001768 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 -05001769#endif
Dees_Troy38bd7602012-09-14 13:33:53 -04001770 return true;
1771 } else {
1772 Has_Data_Media = Save_Data_Media;
Ethan Yonker74db1572015-10-28 12:44:49 -05001773 gui_err("format_data_err=Unable to format to remove encryption.");
Ethan Yonker726a0202014-12-16 20:01:38 -06001774 if (Has_Data_Media && Mount(false))
1775 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04001776 return false;
1777 }
1778 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001779}
1780
1781void TWPartition::Check_FS_Type() {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001782 const char* type;
1783 blkid_probe pr;
Dees_Troy5bf43922012-09-07 16:07:55 -04001784
Dees_Troy68cab492012-12-12 19:29:35 +00001785 if (Fstab_File_System == "yaffs2" || Fstab_File_System == "mtd" || Fstab_File_System == "bml" || Ignore_Blkid)
1786 return; // Running blkid on some mtd devices causes a massive crash or needs to be skipped
Dees_Troy5bf43922012-09-07 16:07:55 -04001787
Dees_Troy38bd7602012-09-14 13:33:53 -04001788 Find_Actual_Block_Device();
Dees_Troy8170a922012-09-18 15:40:25 -04001789 if (!Is_Present)
1790 return;
Dees_Troy51127312012-09-08 13:08:49 -04001791
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001792 pr = blkid_new_probe_from_filename(Actual_Block_Device.c_str());
1793 if (blkid_do_fullprobe(pr)) {
1794 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00001795 LOGINFO("Can't probe device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001796 return;
Dees_Troy5bf43922012-09-07 16:07:55 -04001797 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001798
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05001799 if (blkid_probe_lookup_value(pr, "TYPE", &type, NULL) < 0) {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001800 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00001801 LOGINFO("can't find filesystem on device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001802 return;
1803 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001804
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001805 Current_File_System = type;
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001806 blkid_free_probe(pr);
Dees_Troy51a0e822012-09-05 15:24:24 -04001807}
1808
Gary Peck43acadf2012-11-21 21:19:01 -08001809bool TWPartition::Wipe_EXT23(string File_System) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001810 if (!UnMount(true))
1811 return false;
1812
Dees_Troy43d8b002012-09-17 16:00:01 -04001813 if (TWFunc::Path_Exists("/sbin/mke2fs")) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001814 string command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001815
Greg Wallace4b44fef2015-12-29 15:33:24 -05001816 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mke2fs"));
Dees_Troy38bd7602012-09-14 13:33:53 -04001817 Find_Actual_Block_Device();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001818 command = "mke2fs -t " + File_System + " -m 0 " + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001819 LOGINFO("mke2fs command: %s\n", command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001820 if (TWFunc::Exec_Cmd(command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001821 Current_File_System = File_System;
Dees_Troye58d5262012-09-21 12:27:57 -04001822 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05001823 gui_msg("done=Done.");
Dees_Troy38bd7602012-09-14 13:33:53 -04001824 return true;
1825 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001826 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04001827 return false;
1828 }
1829 } else
1830 return Wipe_RMRF();
1831
1832 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001833}
1834
1835bool TWPartition::Wipe_EXT4() {
Ethan Yonker25f20c12014-10-14 09:04:43 -05001836 Find_Actual_Block_Device();
1837 if (!Is_Present) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001838 LOGINFO("Block device not present, cannot wipe %s.\n", Display_Name.c_str());
1839 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Ethan Yonker25f20c12014-10-14 09:04:43 -05001840 return false;
1841 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001842 if (!UnMount(true))
1843 return false;
1844
Dees_Troyb3265ab2013-08-30 02:59:14 +00001845#if defined(HAVE_SELINUX) && defined(USE_EXT4)
Ethan Yonkerf27497f2014-02-09 11:48:33 -06001846 int ret;
1847 char *secontext = NULL;
1848
Greg Wallace4b44fef2015-12-29 15:33:24 -05001849 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("make_ext4fs"));
Ethan Yonkerf27497f2014-02-09 11:48:33 -06001850
Dees Troy99c8dbf2014-03-10 16:53:58 +00001851 if (!selinux_handle || selabel_lookup(selinux_handle, &secontext, Mount_Point.c_str(), S_IFDIR) < 0) {
Ethan Yonkerf27497f2014-02-09 11:48:33 -06001852 LOGINFO("Cannot lookup security context for '%s'\n", Mount_Point.c_str());
1853 ret = make_ext4fs(Actual_Block_Device.c_str(), Length, Mount_Point.c_str(), NULL);
1854 } else {
1855 ret = make_ext4fs(Actual_Block_Device.c_str(), Length, Mount_Point.c_str(), selinux_handle);
1856 }
1857 if (ret != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001858 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troya95f55c2013-08-17 13:14:43 +00001859 return false;
1860 } else {
bigbiff bigbiffc49d7062013-10-11 20:28:00 -04001861 string sedir = Mount_Point + "/lost+found";
1862 PartitionManager.Mount_By_Path(sedir.c_str(), true);
1863 rmdir(sedir.c_str());
1864 mkdir(sedir.c_str(), S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP);
Dees_Troya95f55c2013-08-17 13:14:43 +00001865 return true;
1866 }
1867#else
Dees_Troy43d8b002012-09-17 16:00:01 -04001868 if (TWFunc::Path_Exists("/sbin/make_ext4fs")) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001869 string Command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001870
Greg Wallace4b44fef2015-12-29 15:33:24 -05001871 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("make_ext4fs"));
Dees_Troy38bd7602012-09-14 13:33:53 -04001872 Find_Actual_Block_Device();
1873 Command = "make_ext4fs";
1874 if (!Is_Decrypted && Length != 0) {
1875 // Only use length if we're not decrypted
1876 char len[32];
1877 sprintf(len, "%i", Length);
1878 Command += " -l ";
1879 Command += len;
1880 }
Dees_Troy5295d582013-09-06 15:51:08 +00001881 if (TWFunc::Path_Exists("/file_contexts")) {
1882 Command += " -S /file_contexts";
1883 }
1884 Command += " -a " + Mount_Point + " " + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001885 LOGINFO("make_ext4fs command: %s\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001886 if (TWFunc::Exec_Cmd(Command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001887 Current_File_System = "ext4";
Dees_Troye58d5262012-09-21 12:27:57 -04001888 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05001889 gui_msg("done=Done.");
Dees_Troy38bd7602012-09-14 13:33:53 -04001890 return true;
1891 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001892 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04001893 return false;
1894 }
1895 } else
Gary Peck43acadf2012-11-21 21:19:01 -08001896 return Wipe_EXT23("ext4");
Dees_Troya95f55c2013-08-17 13:14:43 +00001897#endif
Dees_Troy38bd7602012-09-14 13:33:53 -04001898 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001899}
1900
1901bool TWPartition::Wipe_FAT() {
Vojtech Bocek05534202013-09-11 08:11:56 +02001902 string command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001903
Matt Mower18794c82015-11-11 16:22:45 -06001904 if (TWFunc::Path_Exists("/sbin/mkfs.fat")) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001905 if (!UnMount(true))
1906 return false;
1907
Greg Wallace4b44fef2015-12-29 15:33:24 -05001908 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mkfs.fat"));
Dees_Troy38bd7602012-09-14 13:33:53 -04001909 Find_Actual_Block_Device();
Matt Mower18794c82015-11-11 16:22:45 -06001910 command = "mkfs.fat " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02001911 if (TWFunc::Exec_Cmd(command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001912 Current_File_System = "vfat";
Dees_Troye58d5262012-09-21 12:27:57 -04001913 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05001914 gui_msg("done=Done.");
Dees_Troy38bd7602012-09-14 13:33:53 -04001915 return true;
1916 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001917 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04001918 return false;
1919 }
1920 return true;
1921 }
1922 else
1923 return Wipe_RMRF();
1924
1925 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001926}
1927
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001928bool TWPartition::Wipe_EXFAT() {
Vojtech Bocek05534202013-09-11 08:11:56 +02001929 string command;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001930
1931 if (TWFunc::Path_Exists("/sbin/mkexfatfs")) {
1932 if (!UnMount(true))
1933 return false;
1934
Greg Wallace4b44fef2015-12-29 15:33:24 -05001935 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mkexfatfs"));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001936 Find_Actual_Block_Device();
1937 command = "mkexfatfs " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02001938 if (TWFunc::Exec_Cmd(command) == 0) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001939 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05001940 gui_msg("done=Done.");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001941 return true;
1942 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001943 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001944 return false;
1945 }
1946 return true;
1947 }
1948 return false;
1949}
1950
Dees_Troy38bd7602012-09-14 13:33:53 -04001951bool TWPartition::Wipe_MTD() {
1952 if (!UnMount(true))
1953 return false;
1954
Greg Wallace4b44fef2015-12-29 15:33:24 -05001955 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("MTD"));
Dees_Troy38bd7602012-09-14 13:33:53 -04001956
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001957 mtd_scan_partitions();
1958 const MtdPartition* mtd = mtd_find_partition_by_name(MTD_Name.c_str());
1959 if (mtd == NULL) {
1960 LOGERR("No mtd partition named '%s'", MTD_Name.c_str());
1961 return false;
1962 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001963
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001964 MtdWriteContext* ctx = mtd_write_partition(mtd);
1965 if (ctx == NULL) {
1966 LOGERR("Can't write '%s', failed to format.", MTD_Name.c_str());
1967 return false;
1968 }
1969 if (mtd_erase_blocks(ctx, -1) == -1) {
1970 mtd_write_close(ctx);
1971 LOGERR("Failed to format '%s'", MTD_Name.c_str());
1972 return false;
1973 }
1974 if (mtd_write_close(ctx) != 0) {
1975 LOGERR("Failed to close '%s'", MTD_Name.c_str());
1976 return false;
1977 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001978 Current_File_System = "yaffs2";
Dees_Troye58d5262012-09-21 12:27:57 -04001979 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05001980 gui_msg("done=Done.");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001981 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001982}
1983
1984bool TWPartition::Wipe_RMRF() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001985 if (!Mount(true))
1986 return false;
Ethan Yonker726a0202014-12-16 20:01:38 -06001987 // This is the only wipe that leaves the partition mounted, so we
1988 // must manually remove the partition from MTP if it is a storage
1989 // partition.
1990 if (Is_Storage)
1991 PartitionManager.Remove_MTP_Storage(MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04001992
Ethan Yonker74db1572015-10-28 12:44:49 -05001993 gui_msg(Msg("remove_all=Removing all files under '{1}'")(Mount_Point));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001994 TWFunc::removeDir(Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04001995 Recreate_AndSec_Folder();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001996 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001997}
1998
Dees_Troye5017042013-08-29 16:38:55 +00001999bool TWPartition::Wipe_F2FS() {
Vojtech Bocek05534202013-09-11 08:11:56 +02002000 string command;
Dees_Troye5017042013-08-29 16:38:55 +00002001
2002 if (TWFunc::Path_Exists("/sbin/mkfs.f2fs")) {
2003 if (!UnMount(true))
2004 return false;
2005
Greg Wallace4b44fef2015-12-29 15:33:24 -05002006 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mkfs.f2fs"));
Dees_Troye5017042013-08-29 16:38:55 +00002007 Find_Actual_Block_Device();
Greg Wallace558aef72016-01-19 21:14:30 -05002008 command = "mkfs.f2fs -t 0";
dhacker29a3fa75f2015-01-17 19:42:33 -05002009 if (!Is_Decrypted && Length != 0) {
2010 // Only use length if we're not decrypted
2011 char len[32];
2012 int mod_length = Length;
2013 if (Length < 0)
2014 mod_length *= -1;
2015 sprintf(len, "%i", mod_length);
2016 command += " -r ";
2017 command += len;
2018 }
2019 command += " " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02002020 if (TWFunc::Exec_Cmd(command) == 0) {
Dees_Troye5017042013-08-29 16:38:55 +00002021 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05002022 gui_msg("done=Done.");
Dees_Troye5017042013-08-29 16:38:55 +00002023 return true;
2024 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002025 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troye5017042013-08-29 16:38:55 +00002026 return false;
2027 }
2028 return true;
2029 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002030 LOGINFO("mkfs.f2fs binary not found, using rm -rf to wipe.\n");
Dees_Troye5017042013-08-29 16:38:55 +00002031 return Wipe_RMRF();
2032 }
2033 return false;
2034}
2035
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002036bool TWPartition::Wipe_NTFS() {
2037 string command;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002038 string Ntfsmake_Binary;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002039
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002040 if (TWFunc::Path_Exists("/sbin/mkntfs"))
2041 Ntfsmake_Binary = "mkntfs";
2042 else if (TWFunc::Path_Exists("/sbin/mkfs.ntfs"))
2043 Ntfsmake_Binary = "mkfs.ntfs";
2044 else
2045 return false;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002046
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002047 if (!UnMount(true))
2048 return false;
2049
Greg Wallace4b44fef2015-12-29 15:33:24 -05002050 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)(Ntfsmake_Binary));
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002051 Find_Actual_Block_Device();
2052 command = "/sbin/" + Ntfsmake_Binary + " " + Actual_Block_Device;
2053 if (TWFunc::Exec_Cmd(command) == 0) {
2054 Recreate_AndSec_Folder();
2055 gui_msg("done=Done.");
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002056 return true;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002057 } else {
2058 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
2059 return false;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002060 }
2061 return false;
2062}
2063
Dees_Troy51a0e822012-09-05 15:24:24 -04002064bool TWPartition::Wipe_Data_Without_Wiping_Media() {
Ethan Yonker83e82572014-04-04 10:59:28 -05002065#ifdef TW_OEM_BUILD
2066 // In an OEM Build we want to do a full format
2067 return Wipe_Encryption();
2068#else
Ethan Yonker66a19492015-12-10 10:19:45 -06002069 bool ret = false;
Dees_Troy38bd7602012-09-14 13:33:53 -04002070
Dees_Troy38bd7602012-09-14 13:33:53 -04002071 if (!Mount(true))
2072 return false;
2073
Ethan Yonker74db1572015-10-28 12:44:49 -05002074 gui_msg("wiping_data=Wiping data without wiping /data/media ...");
Ethan Yonker66a19492015-12-10 10:19:45 -06002075 ret = Wipe_Data_Without_Wiping_Media_Func(Mount_Point + "/");
2076 if (ret)
2077 gui_msg("done=Done.");
2078 return ret;
2079#endif // ifdef TW_OEM_BUILD
2080}
2081
2082bool TWPartition::Wipe_Data_Without_Wiping_Media_Func(const string& parent __unused) {
2083 string dir;
Dees_Troy38bd7602012-09-14 13:33:53 -04002084
2085 DIR* d;
Ethan Yonker66a19492015-12-10 10:19:45 -06002086 d = opendir(parent.c_str());
Dees_Troy16b74352012-11-14 22:27:31 +00002087 if (d != NULL) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002088 struct dirent* de;
2089 while ((de = readdir(d)) != NULL) {
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002090 if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) continue;
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05002091
Ethan Yonker66a19492015-12-10 10:19:45 -06002092 dir = parent;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002093 dir.append(de->d_name);
Ethan Yonker66a19492015-12-10 10:19:45 -06002094 if (du.check_skip_dirs(dir)) {
2095 LOGINFO("skipped '%s'\n", dir.c_str());
2096 continue;
2097 }
Dees_Troyce675462013-01-09 19:48:21 +00002098 if (de->d_type == DT_DIR) {
Ethan Yonker66a19492015-12-10 10:19:45 -06002099 dir.append("/");
2100 if (!Wipe_Data_Without_Wiping_Media_Func(dir)) {
2101 closedir(d);
2102 return false;
2103 }
2104 rmdir(dir.c_str());
bigbiff bigbiff98f1f902013-01-19 18:46:13 -05002105 } else if (de->d_type == DT_REG || de->d_type == DT_LNK || de->d_type == DT_FIFO || de->d_type == DT_SOCK) {
Dees_Troyce675462013-01-09 19:48:21 +00002106 if (!unlink(dir.c_str()))
Dees_Troy2673cec2013-04-02 20:22:16 +00002107 LOGINFO("Unable to unlink '%s'\n", dir.c_str());
Dees_Troyce675462013-01-09 19:48:21 +00002108 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002109 }
2110 closedir(d);
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05002111
Dees_Troy16b74352012-11-14 22:27:31 +00002112 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -04002113 }
Ethan Yonker74db1572015-10-28 12:44:49 -05002114 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy16b74352012-11-14 22:27:31 +00002115 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04002116}
2117
Ethan Yonker472f5062016-02-25 13:47:30 -06002118bool TWPartition::Backup_Tar(const string& backup_folder, ProgressTracking *progress, pid_t &tar_fork_pid) {
2119 string Full_FileName;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002120 twrpTar tar;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05002121
Dees_Troy43d8b002012-09-17 16:00:01 -04002122 if (!Mount(true))
2123 return false;
2124
Dees_Troya13d74f2013-03-24 08:54:55 -05002125 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Backup_Display_Name, "Backing Up");
Ethan Yonker74db1572015-10-28 12:44:49 -05002126 gui_msg(Msg("backing_up=Backing up {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002127
Ethan Yonker472f5062016-02-25 13:47:30 -06002128 DataManager::GetValue(TW_USE_COMPRESSION_VAR, tar.use_compression);
Matt Mowerbb81e5d2014-03-20 18:05:41 -05002129
Dees_Troy83bd4832013-05-04 12:39:56 +00002130#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
Ethan Yonker472f5062016-02-25 13:47:30 -06002131 if (Can_Encrypt_Backup) {
2132 DataManager::GetValue("tw_encrypt_backup", tar.use_encryption);
2133 if (tar.use_encryption) {
2134 if (Use_Userdata_Encryption)
2135 tar.userdata_encryption = tar.use_encryption;
2136 string Password;
2137 DataManager::GetValue("tw_backup_password", Password);
2138 tar.setpassword(Password);
2139 } else {
2140 tar.use_encryption = 0;
2141 }
Dees_Troy83bd4832013-05-04 12:39:56 +00002142 }
2143#endif
Dees_Troy43d8b002012-09-17 16:00:01 -04002144
Ethan Yonker472f5062016-02-25 13:47:30 -06002145 Backup_FileName = Backup_Name + "." + Current_File_System + ".win";
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002146 Full_FileName = backup_folder + "/" + Backup_FileName;
Dees_Troy83bd4832013-05-04 12:39:56 +00002147 tar.has_data_media = Has_Data_Media;
Dees Troye0a433a2013-12-02 04:10:37 +00002148 Full_FileName = backup_folder + "/" + Backup_FileName;
2149 tar.setdir(Backup_Path);
2150 tar.setfn(Full_FileName);
2151 tar.setsize(Backup_Size);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002152 tar.partition_name = Backup_Name;
2153 tar.backup_folder = backup_folder;
Ethan Yonker472f5062016-02-25 13:47:30 -06002154 if (tar.createTarFork(progress, tar_fork_pid) != 0)
Dees Troye0a433a2013-12-02 04:10:37 +00002155 return false;
Dees_Troy43d8b002012-09-17 16:00:01 -04002156 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002157}
2158
Ethan Yonker472f5062016-02-25 13:47:30 -06002159bool TWPartition::Backup_Image(const string& backup_folder, ProgressTracking *progress) {
2160 string Full_FileName;
igoriok87e3d932013-01-31 21:03:53 +02002161
Ethan Yonker74db1572015-10-28 12:44:49 -05002162 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, gui_parse_text("{@backing}"));
2163 gui_msg(Msg("backing_up=Backing up {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002164
Ethan Yonker472f5062016-02-25 13:47:30 -06002165 Backup_FileName = Backup_Name + "." + Current_File_System + ".win";
Dees_Troy43d8b002012-09-17 16:00:01 -04002166 Full_FileName = backup_folder + "/" + Backup_FileName;
2167
Ethan Yonker472f5062016-02-25 13:47:30 -06002168 if (!Raw_Read_Write(Actual_Block_Device, Full_FileName, Backup_Size, progress))
2169 return false;
2170
Ethan Yonker4b94cfd2014-12-11 10:00:45 -06002171 tw_set_default_metadata(Full_FileName.c_str());
Dees_Troyc154ac22012-10-12 15:36:47 -04002172 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002173 gui_msg(Msg(msg::kError, "backup_size=Backup file size for '{1}' is 0 bytes.")(Full_FileName));
Dees_Troy7c2dec82012-09-26 09:49:14 -04002174 return false;
2175 }
Dees_Troy43d8b002012-09-17 16:00:01 -04002176 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002177}
2178
Ethan Yonker472f5062016-02-25 13:47:30 -06002179bool TWPartition::Raw_Read_Write(const string& input_file, const string& output_file, const unsigned long long input_size, ProgressTracking *progress) {
2180 unsigned long long RW_Block_Size, Remain;
2181 int src_fd = -1, dest_fd = -1, bs;
2182 bool ret = false;
2183 void* buffer = NULL;
2184 unsigned long long backedup_size = 0;
2185
2186 RW_Block_Size = 1048576LLU; // 1MB
2187 Remain = input_size;
2188
2189 src_fd = open(input_file.c_str(), O_RDONLY | O_LARGEFILE);
2190 if (src_fd < 0) {
2191 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(input_file)(strerror(errno)));
2192 return false;
2193 }
2194 dest_fd = open(output_file.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_LARGEFILE, S_IRUSR | S_IWUSR);
2195 if (dest_fd < 0) {
2196 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(output_file)(strerror(errno)));
2197 goto exit;
2198 }
2199 bs = (int)(RW_Block_Size);
2200 buffer = malloc((size_t)bs);
2201 if (!buffer) {
2202 LOGINFO("Raw_Read_Write failed to malloc\n");
2203 goto exit;
2204 }
2205 LOGINFO("Reading '%s', writing '%s'\n", input_file.c_str(), output_file.c_str());
2206 if (progress)
2207 progress->SetPartitionSize(input_size);
2208 while (Remain > 0) {
2209 if (Remain < RW_Block_Size)
2210 bs = (int)(Remain);
2211 if (read(src_fd, buffer, bs) != bs) {
2212 LOGINFO("Error reading source fd (%s)\n", strerror(errno));
2213 goto exit;
2214 }
2215 if (write(dest_fd, buffer, bs) != bs) {
2216 LOGINFO("Error writing destination fd (%s)\n", strerror(errno));
2217 goto exit;
2218 }
2219 backedup_size += (unsigned long long)(bs);
2220 Remain -= (unsigned long long)(bs);
2221 if (progress)
2222 progress->UpdateSize(backedup_size);
2223 if (PartitionManager.Check_Backup_Cancel() != 0)
2224 goto exit;
2225 }
2226 if (progress)
2227 progress->UpdateDisplayDetails(true);
2228 fsync(dest_fd);
2229 ret = true;
2230exit:
2231 if (src_fd >= 0)
2232 close(src_fd);
2233 if (dest_fd >= 0)
2234 close(dest_fd);
2235 if (buffer)
2236 free(buffer);
2237 return ret;
2238}
2239
2240bool TWPartition::Backup_Dump_Image(const string& backup_folder, ProgressTracking *progress) {
Vojtech Bocek05534202013-09-11 08:11:56 +02002241 string Full_FileName, Command;
Dees_Troy43d8b002012-09-17 16:00:01 -04002242
Ethan Yonker74db1572015-10-28 12:44:49 -05002243 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, gui_parse_text("{@backing}"));
2244 gui_msg(Msg("backing_up=Backing up {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002245
Ethan Yonker472f5062016-02-25 13:47:30 -06002246 if (progress)
2247 progress->SetPartitionSize(Backup_Size);
Dees_Troy43d8b002012-09-17 16:00:01 -04002248
Ethan Yonker472f5062016-02-25 13:47:30 -06002249 Backup_FileName = Backup_Name + "." + Current_File_System + ".win";
Dees_Troy43d8b002012-09-17 16:00:01 -04002250 Full_FileName = backup_folder + "/" + Backup_FileName;
2251
2252 Command = "dump_image " + MTD_Name + " '" + Full_FileName + "'";
Dees_Troy2673cec2013-04-02 20:22:16 +00002253 LOGINFO("Backup command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02002254 TWFunc::Exec_Cmd(Command);
Ethan Yonker4b94cfd2014-12-11 10:00:45 -06002255 tw_set_default_metadata(Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04002256 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
2257 // 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 -06002258 gui_msg(Msg(msg::kError, "backup_size=Backup file size for '{1}' is 0 bytes.")(Full_FileName));
Dees_Troy7c2dec82012-09-26 09:49:14 -04002259 return false;
2260 }
Ethan Yonker472f5062016-02-25 13:47:30 -06002261 if (progress)
2262 progress->UpdateSize(Backup_Size);
Dees_Troy43d8b002012-09-17 16:00:01 -04002263 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002264}
2265
Ethan Yonker472f5062016-02-25 13:47:30 -06002266unsigned long long TWPartition::Get_Restore_Size(const string& restore_folder) {
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002267 InfoManager restore_info(restore_folder + "/" + Backup_Name + ".info");
2268 if (restore_info.LoadValues() == 0) {
2269 if (restore_info.GetValue("backup_size", Restore_Size) == 0) {
2270 LOGINFO("Read info file, restore size is %llu\n", Restore_Size);
2271 return Restore_Size;
2272 }
2273 }
2274 string Full_FileName, Restore_File_System = Get_Restore_File_System(restore_folder);
2275
2276 Full_FileName = restore_folder + "/" + Backup_FileName;
2277
2278 if (Is_Image(Restore_File_System)) {
2279 Restore_Size = TWFunc::Get_File_Size(Full_FileName);
2280 return Restore_Size;
2281 }
2282
2283 twrpTar tar;
2284 tar.setdir(Backup_Path);
2285 tar.setfn(Full_FileName);
2286 tar.backup_name = Backup_Name;
2287#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
2288 string Password;
2289 DataManager::GetValue("tw_restore_password", Password);
2290 if (!Password.empty())
2291 tar.setpassword(Password);
2292#endif
2293 tar.partition_name = Backup_Name;
2294 tar.backup_folder = restore_folder;
2295 Restore_Size = tar.get_size();
2296 return Restore_Size;
2297}
2298
Ethan Yonker472f5062016-02-25 13:47:30 -06002299bool TWPartition::Restore_Tar(const string& restore_folder, const string& Restore_File_System, ProgressTracking *progress) {
2300 string Full_FileName;
Dees Troy4159aed2014-02-28 17:24:43 +00002301 bool ret = false;
Dees_Troy43d8b002012-09-17 16:00:01 -04002302
Dees_Troye58d5262012-09-21 12:27:57 -04002303 if (Has_Android_Secure) {
Dees_Troye58d5262012-09-21 12:27:57 -04002304 if (!Wipe_AndSec())
2305 return false;
Gary Peck43acadf2012-11-21 21:19:01 -08002306 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002307 gui_msg(Msg("wiping=Wiping {1}")(Backup_Display_Name));
Ethan Yonker5eac2222014-06-11 12:22:55 -05002308 if (Has_Data_Media && Mount_Point == "/data" && Restore_File_System != Current_File_System) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002309 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 -05002310 if (!Wipe_Data_Without_Wiping_Media())
2311 return false;
2312 } else {
2313 if (!Wipe(Restore_File_System))
2314 return false;
2315 }
Dees_Troye58d5262012-09-21 12:27:57 -04002316 }
Matt Mower3c366972015-12-25 19:28:31 -06002317 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Backup_Display_Name, gui_parse_text("{@restoring_hdr}"));
Ethan Yonker74db1572015-10-28 12:44:49 -05002318 gui_msg(Msg("restoring=Restoring {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002319
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05002320 // Remount as read/write as needed so we can restore the backup
2321 if (!ReMount_RW(true))
Dees_Troy43d8b002012-09-17 16:00:01 -04002322 return false;
2323
Dees_Troy4a2a1262012-09-18 09:33:47 -04002324 Full_FileName = restore_folder + "/" + Backup_FileName;
Ethan Yonker87af5632014-02-10 11:56:35 -06002325 twrpTar tar;
2326 tar.setdir(Backup_Path);
2327 tar.setfn(Full_FileName);
2328 tar.backup_name = Backup_Name;
2329#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
2330 string Password;
2331 DataManager::GetValue("tw_restore_password", Password);
2332 if (!Password.empty())
2333 tar.setpassword(Password);
2334#endif
Ethan Yonker472f5062016-02-25 13:47:30 -06002335 progress->SetPartitionSize(Get_Restore_Size(restore_folder));
2336 if (tar.extractTarFork(progress) != 0)
Dees Troy4159aed2014-02-28 17:24:43 +00002337 ret = false;
2338 else
2339 ret = true;
2340#ifdef HAVE_CAPABILITIES
2341 // Restore capabilities to the run-as binary
2342 if (Mount_Point == "/system" && Mount(true) && TWFunc::Path_Exists("/system/bin/run-as")) {
2343 struct vfs_cap_data cap_data;
2344 uint64_t capabilities = (1 << CAP_SETUID) | (1 << CAP_SETGID);
2345
2346 memset(&cap_data, 0, sizeof(cap_data));
2347 cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE;
2348 cap_data.data[0].permitted = (uint32_t) (capabilities & 0xffffffff);
2349 cap_data.data[0].inheritable = 0;
2350 cap_data.data[1].permitted = (uint32_t) (capabilities >> 32);
2351 cap_data.data[1].inheritable = 0;
2352 if (setxattr("/system/bin/run-as", XATTR_NAME_CAPS, &cap_data, sizeof(cap_data), 0) < 0) {
2353 LOGINFO("Failed to reset capabilities of /system/bin/run-as binary.\n");
2354 } else {
2355 LOGINFO("Reset capabilities of /system/bin/run-as binary successful.\n");
2356 }
2357 }
2358#endif
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05002359 if (Mount_Read_Only || Mount_Flags & MS_RDONLY)
2360 // Remount as read only when restoration is complete
2361 ReMount(true);
2362
Dees Troy4159aed2014-02-28 17:24:43 +00002363 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04002364}
2365
Ethan Yonker472f5062016-02-25 13:47:30 -06002366bool TWPartition::Restore_Image(const string& restore_folder, const string& Restore_File_System, ProgressTracking *progress) {
Ethan Yonker96af84a2015-01-05 14:58:36 -06002367 string Full_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04002368
Matt Mower3c366972015-12-25 19:28:31 -06002369 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Backup_Display_Name, gui_parse_text("{@restoring_hdr}"));
Ethan Yonker74db1572015-10-28 12:44:49 -05002370 gui_msg(Msg("restoring=Restoring {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002371 Full_FileName = restore_folder + "/" + Backup_FileName;
Gary Peck15e623d2012-11-21 21:07:58 -08002372
Ethan Yonker96af84a2015-01-05 14:58:36 -06002373 if (Restore_File_System == "emmc") {
Ethan Yonker472f5062016-02-25 13:47:30 -06002374 unsigned long long file_size = (unsigned long long)(TWFunc::Get_File_Size(Full_FileName));
2375 if (!Raw_Read_Write(Full_FileName, Actual_Block_Device, file_size, progress))
Ethan Yonker96af84a2015-01-05 14:58:36 -06002376 return false;
2377 } else if (Restore_File_System == "mtd" || Restore_File_System == "bml") {
Ethan Yonker472f5062016-02-25 13:47:30 -06002378 if (!Flash_Image_FI(Full_FileName, progress))
Ethan Yonker96af84a2015-01-05 14:58:36 -06002379 return false;
Gary Peck15e623d2012-11-21 21:07:58 -08002380 }
Dees_Troy43d8b002012-09-17 16:00:01 -04002381 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002382}
Dees_Troy5bf43922012-09-07 16:07:55 -04002383
2384bool TWPartition::Update_Size(bool Display_Error) {
Dees_Troy0550cfb2012-10-13 11:56:13 -04002385 bool ret = false, Was_Already_Mounted = false;
Dees_Troy51127312012-09-08 13:08:49 -04002386
Dees_Troyab10ee22012-09-21 14:27:30 -04002387 if (!Can_Be_Mounted && !Is_Encrypted)
Dees_Troy5bf43922012-09-07 16:07:55 -04002388 return false;
2389
Dees_Troy0550cfb2012-10-13 11:56:13 -04002390 Was_Already_Mounted = Is_Mounted();
Dees_Troy38bd7602012-09-14 13:33:53 -04002391 if (Removable || Is_Encrypted) {
2392 if (!Mount(false))
2393 return true;
2394 } else if (!Mount(Display_Error))
Dees_Troy5bf43922012-09-07 16:07:55 -04002395 return false;
Dees_Troy51127312012-09-08 13:08:49 -04002396
2397 ret = Get_Size_Via_statfs(Display_Error);
Dees_Troy0550cfb2012-10-13 11:56:13 -04002398 if (!ret || Size == 0) {
2399 if (!Get_Size_Via_df(Display_Error)) {
2400 if (!Was_Already_Mounted)
2401 UnMount(false);
Dees_Troy51127312012-09-08 13:08:49 -04002402 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04002403 }
2404 }
Dees_Troy51127312012-09-08 13:08:49 -04002405
Dees_Troy5bf43922012-09-07 16:07:55 -04002406 if (Has_Data_Media) {
2407 if (Mount(Display_Error)) {
Dees_Troy51127312012-09-08 13:08:49 -04002408 unsigned long long data_media_used, actual_data;
Ethan Yonker66a19492015-12-10 10:19:45 -06002409 Used = du.Get_Folder_Size(Mount_Point);
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002410 Backup_Size = Used;
2411 int bak = (int)(Used / 1048576LLU);
Dees_Troy51127312012-09-08 13:08:49 -04002412 int fre = (int)(Free / 1048576LLU);
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002413 LOGINFO("Data backup size is %iMB, free: %iMB.\n", bak, fre);
Dees_Troy0550cfb2012-10-13 11:56:13 -04002414 } else {
2415 if (!Was_Already_Mounted)
2416 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04002417 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04002418 }
Dees_Troye58d5262012-09-21 12:27:57 -04002419 } else if (Has_Android_Secure) {
2420 if (Mount(Display_Error))
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002421 Backup_Size = du.Get_Folder_Size(Backup_Path);
Dees_Troy0550cfb2012-10-13 11:56:13 -04002422 else {
2423 if (!Was_Already_Mounted)
2424 UnMount(false);
Dees_Troye58d5262012-09-21 12:27:57 -04002425 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04002426 }
Dees_Troy5bf43922012-09-07 16:07:55 -04002427 }
Dees_Troy0550cfb2012-10-13 11:56:13 -04002428 if (!Was_Already_Mounted)
2429 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04002430 return true;
Dees_Troy51127312012-09-08 13:08:49 -04002431}
Dees_Troy38bd7602012-09-14 13:33:53 -04002432
2433void TWPartition::Find_Actual_Block_Device(void) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002434 if (Is_Decrypted && !Decrypted_Block_Device.empty()) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002435 Actual_Block_Device = Decrypted_Block_Device;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002436 if (TWFunc::Path_Exists(Decrypted_Block_Device))
Dees_Troy38bd7602012-09-14 13:33:53 -04002437 Is_Present = true;
Dees_Troy43d8b002012-09-17 16:00:01 -04002438 } else if (TWFunc::Path_Exists(Primary_Block_Device)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002439 Is_Present = true;
2440 Actual_Block_Device = Primary_Block_Device;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002441 return;
2442 }
2443 if (Is_Decrypted) {
Dees_Troy43d8b002012-09-17 16:00:01 -04002444 } else if (!Alternate_Block_Device.empty() && TWFunc::Path_Exists(Alternate_Block_Device)) {
Dees_Troy3f04d032012-10-07 18:20:09 -04002445 Actual_Block_Device = Alternate_Block_Device;
Dees_Troy38bd7602012-09-14 13:33:53 -04002446 Is_Present = true;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002447 } else {
Dees_Troy38bd7602012-09-14 13:33:53 -04002448 Is_Present = false;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002449 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002450}
2451
2452void TWPartition::Recreate_Media_Folder(void) {
2453 string Command;
Ethan Yonker66a19492015-12-10 10:19:45 -06002454 string Media_Path = Mount_Point + "/media";
Dees_Troy38bd7602012-09-14 13:33:53 -04002455
2456 if (!Mount(true)) {
Ethan Yonker66a19492015-12-10 10:19:45 -06002457 gui_msg(Msg(msg::kError, "recreate_folder_err=Unable to recreate {1} folder.")(Media_Path));
2458 } else if (!TWFunc::Path_Exists(Media_Path)) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002459 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Ethan Yonker66a19492015-12-10 10:19:45 -06002460 LOGINFO("Recreating %s folder.\n", Media_Path.c_str());
2461 mkdir(Media_Path.c_str(), 0770);
xiaolu9416f4f2015-06-04 08:22:23 +08002462 string Internal_path = DataManager::GetStrValue("tw_internal_path");
2463 if (!Internal_path.empty()) {
2464 LOGINFO("Recreating %s folder.\n", Internal_path.c_str());
2465 mkdir(Internal_path.c_str(), 0770);
2466 }
2467#ifdef TW_INTERNAL_STORAGE_PATH
2468 mkdir(EXPAND(TW_INTERNAL_STORAGE_PATH), 0770);
2469#endif
Ethan Yonker5ef301e2014-10-14 10:04:44 -05002470#ifdef HAVE_SELINUX
thata3d31fb2014-12-21 22:27:40 +01002471 // Afterwards, we will try to set the
2472 // default metadata that we were hopefully able to get during
2473 // early boot.
Ethan Yonker66a19492015-12-10 10:19:45 -06002474 tw_set_default_metadata(Media_Path.c_str());
xiaolu9416f4f2015-06-04 08:22:23 +08002475 if (!Internal_path.empty())
2476 tw_set_default_metadata(Internal_path.c_str());
Ethan Yonker5ef301e2014-10-14 10:04:44 -05002477#endif
Ethan Yonker5eac2222014-06-11 12:22:55 -05002478 // Toggle mount to ensure that "internal sdcard" gets mounted
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002479 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Ethan Yonker5eac2222014-06-11 12:22:55 -05002480 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Dees_Troy38bd7602012-09-14 13:33:53 -04002481 }
Dees_Troy43d8b002012-09-17 16:00:01 -04002482}
Dees_Troye58d5262012-09-21 12:27:57 -04002483
2484void TWPartition::Recreate_AndSec_Folder(void) {
Dees_Troye58d5262012-09-21 12:27:57 -04002485 if (!Has_Android_Secure)
2486 return;
Dees_Troy2673cec2013-04-02 20:22:16 +00002487 LOGINFO("Creating %s: %s\n", Backup_Display_Name.c_str(), Symlink_Path.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -04002488 if (!Mount(true)) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002489 gui_msg(Msg(msg::kError, "recreate_folder_err=Unable to recreate {1} folder.")(Backup_Name));
Dees_Troye58d5262012-09-21 12:27:57 -04002490 } else if (!TWFunc::Path_Exists(Symlink_Path)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00002491 LOGINFO("Recreating %s folder.\n", Backup_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002492 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05002493 mkdir(Symlink_Path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002494 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04002495 }
2496}
bigbiff7cb4c332014-11-26 20:36:07 -05002497
2498uint64_t TWPartition::Get_Max_FileSize() {
2499 uint64_t maxFileSize = 0;
2500 const uint64_t constGB = (uint64_t) 1024 * 1024 * 1024;
2501 const uint64_t constTB = (uint64_t) constGB * 1024;
2502 const uint64_t constPB = (uint64_t) constTB * 1024;
2503 const uint64_t constEB = (uint64_t) constPB * 1024;
bigbiff0c532032014-12-21 13:41:26 -05002504 if (Current_File_System == "ext4")
2505 maxFileSize = 16 * constTB; //16 TB
2506 else if (Current_File_System == "vfat")
2507 maxFileSize = 4 * constGB; //4 GB
2508 else if (Current_File_System == "ntfs")
2509 maxFileSize = 256 * constTB; //256 TB
2510 else if (Current_File_System == "exfat")
2511 maxFileSize = 16 * constPB; //16 PB
2512 else if (Current_File_System == "ext3")
2513 maxFileSize = 2 * constTB; //2 TB
2514 else if (Current_File_System == "f2fs")
2515 maxFileSize = 3.94 * constTB; //3.94 TB
bigbiff7cb4c332014-11-26 20:36:07 -05002516 else
2517 maxFileSize = 100000000L;
2518 return maxFileSize - 1;
2519}
2520
Ethan Yonker472f5062016-02-25 13:47:30 -06002521bool TWPartition::Flash_Image(const string& Filename) {
Ethan Yonker96af84a2015-01-05 14:58:36 -06002522 string Restore_File_System;
2523
2524 LOGINFO("Image filename is: %s\n", Filename.c_str());
2525
2526 if (Backup_Method == FILES) {
2527 LOGERR("Cannot flash images to file systems\n");
2528 return false;
2529 } else if (!Can_Flash_Img) {
2530 LOGERR("Cannot flash images to partitions %s\n", Display_Name.c_str());
2531 return false;
2532 } else {
2533 if (!Find_Partition_Size()) {
2534 LOGERR("Unable to find partition size for '%s'\n", Mount_Point.c_str());
2535 return false;
2536 }
2537 unsigned long long image_size = TWFunc::Get_File_Size(Filename);
2538 if (image_size > Size) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002539 LOGINFO("Size (%llu bytes) of image '%s' is larger than target device '%s' (%llu bytes)\n",
Ethan Yonker96af84a2015-01-05 14:58:36 -06002540 image_size, Filename.c_str(), Actual_Block_Device.c_str(), Size);
Ethan Yonker74db1572015-10-28 12:44:49 -05002541 gui_err("img_size_err=Size of image is larger than target device");
Ethan Yonker96af84a2015-01-05 14:58:36 -06002542 return false;
2543 }
Ethan Yonker472f5062016-02-25 13:47:30 -06002544 if (Backup_Method == DD) {
2545 if (Is_Sparse_Image(Filename)) {
2546 return Flash_Sparse_Image(Filename);
2547 }
2548 unsigned long long file_size = (unsigned long long)(TWFunc::Get_File_Size(Filename));
2549 ProgressTracking pt(file_size);
2550 return Raw_Read_Write(Filename, Actual_Block_Device, file_size, &pt);
2551 } else if (Backup_Method == FLASH_UTILS) {
2552 return Flash_Image_FI(Filename, NULL);
2553 }
Ethan Yonker96af84a2015-01-05 14:58:36 -06002554 }
2555
2556 LOGERR("Unknown flash method for '%s'\n", Mount_Point.c_str());
2557 return false;
2558}
2559
Ethan Yonker472f5062016-02-25 13:47:30 -06002560bool TWPartition::Is_Sparse_Image(const string& Filename) {
HashBanged974bb2016-01-30 14:20:09 -05002561 uint32_t magic = 0;
2562 int fd = open(Filename.c_str(), O_RDONLY);
2563 if (fd < 0) {
2564 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Filename)(strerror(errno)));
2565 return false;
2566 }
2567 if (read(fd, &magic, sizeof(magic)) != sizeof(magic)) {
2568 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Filename)(strerror(errno)));
2569 close(fd);
2570 return false;
2571 }
2572 close(fd);
Ethan Yonker472f5062016-02-25 13:47:30 -06002573 if (magic == SPARSE_HEADER_MAGIC)
2574 return true;
2575 return false;
2576}
2577
2578bool TWPartition::Flash_Sparse_Image(const string& Filename) {
2579 string Command;
2580
2581 gui_msg(Msg("flashing=Flashing {1}...")(Display_Name));
2582
2583 Command = "simg2img '" + Filename + "' '" + Actual_Block_Device + "'";
Ethan Yonker96af84a2015-01-05 14:58:36 -06002584 LOGINFO("Flash command: '%s'\n", Command.c_str());
2585 TWFunc::Exec_Cmd(Command);
2586 return true;
2587}
2588
Ethan Yonker472f5062016-02-25 13:47:30 -06002589bool TWPartition::Flash_Image_FI(const string& Filename, ProgressTracking *progress) {
Ethan Yonker96af84a2015-01-05 14:58:36 -06002590 string Command;
Ethan Yonker472f5062016-02-25 13:47:30 -06002591 unsigned long long file_size;
Ethan Yonker96af84a2015-01-05 14:58:36 -06002592
Ethan Yonker74db1572015-10-28 12:44:49 -05002593 gui_msg(Msg("flashing=Flashing {1}...")(Display_Name));
Ethan Yonker472f5062016-02-25 13:47:30 -06002594 if (progress) {
2595 file_size = (unsigned long long)(TWFunc::Get_File_Size(Filename));
2596 progress->SetPartitionSize(file_size);
2597 }
Ethan Yonker96af84a2015-01-05 14:58:36 -06002598 // Sometimes flash image doesn't like to flash due to the first 2KB matching, so we erase first to ensure that it flashes
2599 Command = "erase_image " + MTD_Name;
2600 LOGINFO("Erase command: '%s'\n", Command.c_str());
2601 TWFunc::Exec_Cmd(Command);
2602 Command = "flash_image " + MTD_Name + " '" + Filename + "'";
2603 LOGINFO("Flash command: '%s'\n", Command.c_str());
2604 TWFunc::Exec_Cmd(Command);
Ethan Yonker472f5062016-02-25 13:47:30 -06002605 if (progress)
2606 progress->UpdateSize(file_size);
Ethan Yonker96af84a2015-01-05 14:58:36 -06002607 return true;
2608}
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05002609
2610void TWPartition::Change_Mount_Read_Only(bool new_value) {
2611 Mount_Read_Only = new_value;
2612}
2613
2614int TWPartition::Check_Lifetime_Writes() {
2615 bool original_read_only = Mount_Read_Only;
2616 int ret = 1;
2617
2618 Mount_Read_Only = true;
2619 if (Mount(false)) {
2620 Find_Actual_Block_Device();
2621 string block = basename(Actual_Block_Device.c_str());
2622 string file = "/sys/fs/" + Current_File_System + "/" + block + "/lifetime_write_kbytes";
2623 string result;
2624 if (TWFunc::Path_Exists(file)) {
2625 if (TWFunc::read_file(file, result) != 0) {
2626 LOGINFO("Check_Lifetime_Writes of '%s' failed to read_file\n", file.c_str());
2627 } else {
2628 LOGINFO("Check_Lifetime_Writes result: '%s'\n", result.c_str());
2629 if (result == "0") {
2630 ret = 0;
2631 }
2632 }
2633 } else {
2634 LOGINFO("Check_Lifetime_Writes file does not exist '%s'\n", file.c_str());
2635 }
2636 UnMount(true);
2637 } else {
2638 LOGINFO("Check_Lifetime_Writes failed to mount '%s'\n", Mount_Point.c_str());
2639 }
2640 Mount_Read_Only = original_read_only;
2641 return ret;
2642}
Ethan Yonker66a19492015-12-10 10:19:45 -06002643
2644int TWPartition::Decrypt_Adopted() {
2645#ifdef TW_INCLUDE_CRYPTO
2646 int ret = 1;
2647 Is_Adopted_Storage = false;
2648 string Adopted_Key_File = "";
2649
2650 if (!Removable)
2651 return ret;
2652
2653 int fd = open(Alternate_Block_Device.c_str(), O_RDONLY);
2654 if (fd < 0) {
2655 LOGINFO("failed to open '%s'\n", Alternate_Block_Device.c_str());
2656 return ret;
2657 }
2658 char type_guid[80];
2659 char part_guid[80];
2660
2661 if (gpt_disk_get_partition_info(fd, 2, type_guid, part_guid) == 0) {
2662 LOGINFO("type: '%s'\n", type_guid);
2663 LOGINFO("part: '%s'\n", part_guid);
2664 Adopted_GUID = part_guid;
2665 LOGINFO("Adopted_GUID '%s'\n", Adopted_GUID.c_str());
2666 if (strcmp(type_guid, TWGptAndroidExpand) == 0) {
2667 LOGINFO("android_expand found\n");
2668 Adopted_Key_File = "/data/misc/vold/expand_";
2669 Adopted_Key_File += part_guid;
2670 Adopted_Key_File += ".key";
2671 if (TWFunc::Path_Exists(Adopted_Key_File)) {
2672 Is_Adopted_Storage = true;
2673 /* Until we find a use case for this, I think it is safe
2674 * to disable USB Mass Storage whenever adopted storage
2675 * is present.
2676 */
2677 LOGINFO("Detected adopted storage, disabling USB mass storage mode\n");
2678 DataManager::SetValue("tw_has_usb_storage", 0);
2679 }
2680 }
2681 }
2682
2683 if (Is_Adopted_Storage) {
2684 string Adopted_Block_Device = Alternate_Block_Device + "p2";
2685 if (!TWFunc::Path_Exists(Adopted_Block_Device)) {
2686 Adopted_Block_Device = Alternate_Block_Device + "2";
2687 if (!TWFunc::Path_Exists(Adopted_Block_Device)) {
2688 LOGINFO("Adopted block device does not exist\n");
2689 goto exit;
2690 }
2691 }
2692 LOGINFO("key file is '%s', block device '%s'\n", Adopted_Key_File.c_str(), Adopted_Block_Device.c_str());
2693 char crypto_blkdev[MAXPATHLEN];
2694 std::string thekey;
2695 int fdkey = open(Adopted_Key_File.c_str(), O_RDONLY);
2696 if (fdkey < 0) {
2697 LOGINFO("failed to open key file\n");
2698 goto exit;
2699 }
2700 char buf[512];
2701 ssize_t n;
2702 while ((n = read(fdkey, &buf[0], sizeof(buf))) > 0) {
2703 thekey.append(buf, n);
2704 }
2705 close(fdkey);
2706 unsigned char* key = (unsigned char*) thekey.data();
2707 cryptfs_revert_ext_volume(part_guid);
2708
2709 ret = cryptfs_setup_ext_volume(part_guid, Adopted_Block_Device.c_str(), key, thekey.size(), crypto_blkdev);
2710 if (ret == 0) {
2711 LOGINFO("adopted storage new block device: '%s'\n", crypto_blkdev);
2712 Decrypted_Block_Device = crypto_blkdev;
2713 Is_Decrypted = true;
2714 Is_Encrypted = true;
2715 Find_Actual_Block_Device();
2716 if (!Mount(false)) {
2717 LOGERR("Failed to mount decrypted adopted storage device\n");
2718 Is_Decrypted = false;
2719 Is_Encrypted = false;
2720 cryptfs_revert_ext_volume(part_guid);
2721 ret = 1;
2722 } else {
Ethan Yonkerfcf3f242016-02-16 12:30:26 -06002723 UnMount(false);
2724 Has_Android_Secure = false;
2725 Symlink_Path = "";
2726 Symlink_Mount_Point = "";
2727 Backup_Name = Mount_Point.substr(1);
2728 Backup_Path = Mount_Point;
2729 TWPartition* sdext = PartitionManager.Find_Partition_By_Path("/sd-ext");
2730 if (sdext && sdext->Actual_Block_Device == Adopted_Block_Device) {
2731 LOGINFO("Removing /sd-ext from partition list due to adopted storage\n");
2732 PartitionManager.Remove_Partition_By_Path("/sd-ext");
2733 }
Ethan Yonker66a19492015-12-10 10:19:45 -06002734 Setup_Data_Media();
2735 Recreate_Media_Folder();
2736 Wipe_Available_in_GUI = true;
2737 Wipe_During_Factory_Reset = true;
2738 Can_Be_Backed_Up = true;
2739 Can_Encrypt_Backup = true;
2740 Use_Userdata_Encryption = true;
2741 Is_Storage = true;
2742 Storage_Name = "Adopted Storage";
2743 Is_SubPartition = true;
2744 SubPartition_Of = "/data";
2745 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
2746 DataManager::SetValue("tw_has_adopted_storage", 1);
2747 }
2748 } else {
2749 LOGERR("Failed to setup adopted storage decryption\n");
2750 }
2751 }
2752exit:
2753 return ret;
2754#else
2755 LOGINFO("Decrypt_Adopted: no crypto support\n");
2756 return 1;
2757#endif
2758}
2759
2760void TWPartition::Revert_Adopted() {
2761#ifdef TW_INCLUDE_CRYPTO
2762 if (!Adopted_GUID.empty()) {
2763 PartitionManager.Remove_MTP_Storage(Mount_Point);
2764 UnMount(false);
2765 cryptfs_revert_ext_volume(Adopted_GUID.c_str());
2766 Is_Adopted_Storage = false;
2767 Is_Encrypted = false;
2768 Is_Decrypted = false;
2769 Decrypted_Block_Device = "";
2770 Find_Actual_Block_Device();
2771 Wipe_During_Factory_Reset = false;
2772 Can_Be_Backed_Up = false;
2773 Can_Encrypt_Backup = false;
2774 Use_Userdata_Encryption = false;
2775 Is_SubPartition = false;
2776 SubPartition_Of = "";
2777 Has_Data_Media = false;
2778 Storage_Path = Mount_Point;
2779 if (!Symlink_Mount_Point.empty()) {
2780 TWPartition* Dat = PartitionManager.Find_Partition_By_Path("/data");
2781 if (Dat) {
2782 Dat->UnMount(false);
2783 Dat->Symlink_Mount_Point = Symlink_Mount_Point;
2784 }
2785 Symlink_Mount_Point = "";
2786 }
2787 }
2788#else
2789 LOGINFO("Revert_Adopted: no crypto support\n");
2790#endif
2791}