blob: 07d71715482593ff1115ff61c404940064cde6c6 [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
85static struct flag_list mount_flags[] = {
86 { "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
Dees_Troy5bf43922012-09-07 16:07:55 -0400238bool TWPartition::Process_Fstab_Line(string Line, bool Display_Error) {
239 char full_line[MAX_FSTAB_LINE_LENGTH], item[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 int line_len = Line.size(), index = 0, item_index = 0;
242 char* ptr;
Dees_Troy5bf43922012-09-07 16:07:55 -0400243 strncpy(full_line, Line.c_str(), line_len);
Dees_Troya13d74f2013-03-24 08:54:55 -0500244 bool skip = false;
Dees_Troy5bf43922012-09-07 16:07:55 -0400245
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)
Dees_Troy2673cec2013-04-02 20:22:16 +0000286 LOGERR("Invalid block device on '%s', '%s', %i\n", Line.c_str(), ptr, index);
Dees_Troy5bf43922012-09-07 16:07:55 -0400287 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000288 LOGINFO("Invalid block device on '%s', '%s', %i\n", Line.c_str(), ptr, index);
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
Dees_Troy2673cec2013-04-02 20:22:16 +0000313 LOGINFO("Unhandled fstab information: '%s', %i, line: '%s'\n", ptr, index, Line.c_str());
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
Ketut Putu Kumajayabfb72b02015-08-10 16:40:55 +0200515bool TWPartition::Process_FS_Flags(string& Options, int& Flags) {
Hashcode62bd9e02013-11-19 21:59:42 -0800516 int i;
517 char *p;
518 char *savep;
519 char fs_options[250];
520
521 strlcpy(fs_options, Options.c_str(), sizeof(fs_options));
522 Options = "";
523
524 p = strtok_r(fs_options, ",", &savep);
525 while (p) {
526 /* Look for the flag "p" in the flag list "fl"
527 * If not found, the loop exits with fl[i].name being null.
528 */
529 for (i = 0; mount_flags[i].name; i++) {
530 if (strncmp(p, mount_flags[i].name, strlen(mount_flags[i].name)) == 0) {
531 Flags |= mount_flags[i].flag;
532 break;
533 }
534 }
535
536 if (!mount_flags[i].name) {
537 if (Options.size() > 0)
538 Options += ",";
539 Options += p;
540 }
541 p = strtok_r(NULL, ",", &savep);
542 }
543
544 return true;
545}
546
Matt Mower2416a502016-04-12 19:54:46 -0500547void TWPartition::Apply_TW_Flag(const unsigned flag, const char* str, const bool val) {
548 switch (flag) {
549 case TWFLAG_ANDSEC:
550 Has_Android_Secure = val;
551 break;
552 case TWFLAG_BACKUP:
553 Can_Be_Backed_Up = val;
554 break;
555 case TWFLAG_BACKUPNAME:
556 Backup_Display_Name = str;
557 break;
558 case TWFLAG_BLOCKSIZE:
559 Format_Block_Size = (unsigned long)(atol(str));
560 break;
561 case TWFLAG_CANBEWIPED:
562 Can_Be_Wiped = val;
563 break;
564 case TWFLAG_CANENCRYPTBACKUP:
565 Can_Encrypt_Backup = val;
566 break;
567 case TWFLAG_DEFAULTS:
568 // Do nothing
569 break;
570 case TWFLAG_DISPLAY:
571 Display_Name = str;
572 break;
573 case TWFLAG_ENCRYPTABLE:
574 case TWFLAG_FORCEENCRYPT:
575 Crypto_Key_Location = str;
576 break;
577 case TWFLAG_FLASHIMG:
578 Can_Flash_Img = val;
579 break;
580 case TWFLAG_FSFLAGS:
581 Mount_Options = str;
582 Process_FS_Flags(Mount_Options, Mount_Flags);
583 break;
584 case TWFLAG_IGNOREBLKID:
585 Ignore_Blkid = val;
586 break;
587 case TWFLAG_LENGTH:
588 Length = atoi(str);
589 break;
590 case TWFLAG_MOUNTTODECRYPT:
591 Mount_To_Decrypt = val;
592 break;
593 case TWFLAG_REMOVABLE:
594 Removable = val;
595 break;
596 case TWFLAG_RETAINLAYOUTVERSION:
597 Retain_Layout_Version = val;
598 break;
599 case TWFLAG_SETTINGSSTORAGE:
600 Is_Settings_Storage = val;
601 if (Is_Settings_Storage)
602 Is_Storage = true;
603 break;
604 case TWFLAG_STORAGE:
605 Is_Storage = val;
606 break;
607 case TWFLAG_STORAGENAME:
608 Storage_Name = str;
609 break;
610 case TWFLAG_SUBPARTITIONOF:
611 Is_SubPartition = true;
612 SubPartition_Of = str;
613 break;
614 case TWFLAG_SYMLINK:
615 Symlink_Path = str;
616 break;
617 case TWFLAG_USERDATAENCRYPTBACKUP:
618 Use_Userdata_Encryption = val;
619 if (Use_Userdata_Encryption)
620 Can_Encrypt_Backup = true;
621 break;
622 case TWFLAG_USERMRF:
623 Use_Rm_Rf = val;
624 break;
625 case TWFLAG_WIPEDURINGFACTORYRESET:
626 Wipe_During_Factory_Reset = val;
627 if (Wipe_During_Factory_Reset) {
628 Can_Be_Wiped = true;
629 Wipe_Available_in_GUI = true;
630 }
631 break;
632 case TWFLAG_WIPEINGUI:
633 Wipe_Available_in_GUI = val;
634 if (Wipe_Available_in_GUI)
635 Can_Be_Wiped = true;
636 break;
637 default:
638 // Should not get here
639 LOGINFO("Flag identified for processing, but later unmatched: %i\n", flag);
640 break;
641 }
642}
Dees_Troy51127312012-09-08 13:08:49 -0400643
Matt Mower2416a502016-04-12 19:54:46 -0500644void TWPartition::Process_TW_Flags(char *flags, bool Display_Error) {
645 char separator[2] = {'\n', 0};
646 char *ptr, *savep;
647
648 // Semicolons within double-quotes are not forbidden, so replace
649 // only the semicolons intended as separators with '\n' for strtok
650 for (unsigned i = 0, skip = 0; i < strlen(flags); i++) {
651 if (flags[i] == '\"')
Dees_Troya13d74f2013-03-24 08:54:55 -0500652 skip = !skip;
Matt Mower2416a502016-04-12 19:54:46 -0500653 if (!skip && flags[i] == ';')
654 flags[i] = separator[0];
Dees_Troy51127312012-09-08 13:08:49 -0400655 }
656
Matt Mower2416a502016-04-12 19:54:46 -0500657 // Avoid issues with potentially nested strtok by using strtok_r
658 ptr = strtok_r(flags, separator, &savep);
659 while (ptr) {
660 int ptr_len = strlen(ptr);
661 const struct flag_list* tw_flag = tw_flags;
Hashcode62bd9e02013-11-19 21:59:42 -0800662
Matt Mower2416a502016-04-12 19:54:46 -0500663 for (; tw_flag->name; tw_flag++) {
664 int flag_len = strlen(tw_flag->name);
665
666 if (strncmp(ptr, tw_flag->name, flag_len) == 0) {
667 bool flag_val = false;
668
669 if (ptr_len > flag_len && (tw_flag->name)[flag_len-1] != '='
670 && ptr[flag_len] != '=') {
671 // Handle flags with same starting string
672 // (e.g. backup and backupname)
673 continue;
674 } else if (ptr_len > flag_len && ptr[flag_len] == '=') {
675 // Handle flags with dual format: Part 1
676 // (e.g. backup and backup=y. backup=y handled here)
677 ptr += flag_len + 1;
678 TWFunc::Strip_Quotes(ptr);
679 // Skip flags with empty argument
680 // (e.g. backup=)
681 if (strlen(ptr) == 0) {
682 LOGINFO("Flag missing argument or should not include '=': %s=\n", tw_flag->name);
683 break;
684 }
685 flag_val = strchr("yY1", *ptr) != NULL;
686 } else if (ptr_len == flag_len
687 && (tw_flag->name)[flag_len-1] == '=') {
688 // Skip flags missing argument after =
689 // (e.g. backupname=)
690 LOGINFO("Flag missing argument: %s\n", tw_flag->name);
691 break;
692 } else if (ptr_len > flag_len
693 && (tw_flag->name)[flag_len-1] == '=') {
694 // Handle arguments to flags
695 // (e.g. backupname="My Stuff")
696 ptr += flag_len;
697 TWFunc::Strip_Quotes(ptr);
698 // Skip flags with empty argument
699 // (e.g. backupname="")
700 if (strlen(ptr) == 0) {
701 LOGINFO("Flag missing argument: %s\n", tw_flag->name);
702 break;
703 }
704 } else if (ptr_len == flag_len) {
705 // Handle flags with dual format: Part 2
706 // (e.g. backup and backup=y. backup handled here)
707 flag_val = true;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600708 } else {
Matt Mower2416a502016-04-12 19:54:46 -0500709 LOGINFO("Flag matched, but could not be processed: %s\n", ptr);
710 break;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600711 }
Matt Mower2416a502016-04-12 19:54:46 -0500712
713 Apply_TW_Flag(tw_flag->flag, ptr, flag_val);
714 break;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600715 }
Matt Mower2416a502016-04-12 19:54:46 -0500716 }
717 if (tw_flag->name == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400718 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000719 LOGERR("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400720 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000721 LOGINFO("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400722 }
Matt Mower2416a502016-04-12 19:54:46 -0500723 ptr = strtok_r(NULL, separator, &savep);
Dees_Troy51127312012-09-08 13:08:49 -0400724 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400725}
726
Dees_Troy5bf43922012-09-07 16:07:55 -0400727bool TWPartition::Is_File_System(string File_System) {
728 if (File_System == "ext2" ||
Dees_Troy63c8df72012-09-10 14:02:05 -0400729 File_System == "ext3" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400730 File_System == "ext4" ||
731 File_System == "vfat" ||
732 File_System == "ntfs" ||
733 File_System == "yaffs2" ||
bigbiff bigbiff3e146522012-11-14 14:32:59 -0500734 File_System == "exfat" ||
Dees_Troye5017042013-08-29 16:38:55 +0000735 File_System == "f2fs" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400736 File_System == "auto")
737 return true;
738 else
739 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400740}
741
Dees_Troy5bf43922012-09-07 16:07:55 -0400742bool TWPartition::Is_Image(string File_System) {
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400743 if (File_System == "emmc" || File_System == "mtd" || File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -0400744 return true;
745 else
746 return false;
747}
748
Dees_Troy51127312012-09-08 13:08:49 -0400749bool TWPartition::Make_Dir(string Path, bool Display_Error) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400750 if (!TWFunc::Path_Exists(Path)) {
Dees_Troy51127312012-09-08 13:08:49 -0400751 if (mkdir(Path.c_str(), 0777) == -1) {
752 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -0500753 gui_msg(Msg(msg::kError, "create_folder_strerr=Can not create '{1}' folder ({2}).")(Path)(strerror(errno)));
Dees_Troy51127312012-09-08 13:08:49 -0400754 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000755 LOGINFO("Can not create '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400756 return false;
757 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000758 LOGINFO("Created '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400759 return true;
760 }
761 }
762 return true;
763}
764
Dees_Troy5bf43922012-09-07 16:07:55 -0400765void TWPartition::Setup_File_System(bool Display_Error) {
766 struct statfs st;
767
768 Can_Be_Mounted = true;
769 Can_Be_Wiped = true;
770
Dees_Troy5bf43922012-09-07 16:07:55 -0400771 // Make the mount point folder if it doesn't exist
Dees_Troy51127312012-09-08 13:08:49 -0400772 Make_Dir(Mount_Point, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400773 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
774 Backup_Name = Display_Name;
775 Backup_Method = FILES;
776}
777
778void TWPartition::Setup_Image(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400779 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
780 Backup_Name = Display_Name;
Gary Peck82599a82012-11-21 16:23:12 -0800781 if (Current_File_System == "emmc")
Dees_Troy5bf43922012-09-07 16:07:55 -0400782 Backup_Method = DD;
Gary Peck82599a82012-11-21 16:23:12 -0800783 else if (Current_File_System == "mtd" || Current_File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -0400784 Backup_Method = FLASH_UTILS;
785 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000786 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 -0400787 if (Find_Partition_Size()) {
788 Used = Size;
789 Backup_Size = Size;
Dees_Troy51a0e822012-09-05 15:24:24 -0400790 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400791 if (Display_Error)
Dees Troyd932ce12013-10-18 17:12:59 +0000792 LOGERR("Unable to find partition size for '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400793 else
Dees Troyd932ce12013-10-18 17:12:59 +0000794 LOGINFO("Unable to find partition size for '%s'\n", Mount_Point.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400795 }
796}
797
Dees_Troye58d5262012-09-21 12:27:57 -0400798void TWPartition::Setup_AndSec(void) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500799 Backup_Display_Name = "Android Secure";
Dees_Troye58d5262012-09-21 12:27:57 -0400800 Backup_Name = "and-sec";
Dees_Troya13d74f2013-03-24 08:54:55 -0500801 Can_Be_Backed_Up = true;
Dees_Troye58d5262012-09-21 12:27:57 -0400802 Has_Android_Secure = true;
803 Symlink_Path = Mount_Point + "/.android_secure";
804 Symlink_Mount_Point = "/and-sec";
805 Backup_Path = Symlink_Mount_Point;
806 Make_Dir("/and-sec", true);
807 Recreate_AndSec_Folder();
Ethan Yonkerd4d10732014-02-03 15:27:52 -0600808 Mount_Storage_Retry();
Dees_Troye58d5262012-09-21 12:27:57 -0400809}
810
that9e0593e2014-10-08 00:01:24 +0200811void TWPartition::Setup_Data_Media() {
Ethan Yonker6277c792014-09-15 14:54:30 -0500812 LOGINFO("Setting up '%s' as data/media emulated storage.\n", Mount_Point.c_str());
Ethan Yonker66a19492015-12-10 10:19:45 -0600813 if (Storage_Name.empty() || Storage_Name == "Data")
814 Storage_Name = "Internal Storage";
Ethan Yonker6277c792014-09-15 14:54:30 -0500815 Has_Data_Media = true;
816 Is_Storage = true;
Ethan Yonker66a19492015-12-10 10:19:45 -0600817 Storage_Path = Mount_Point + "/media";
Ethan Yonker6277c792014-09-15 14:54:30 -0500818 Symlink_Path = Storage_Path;
Ethan Yonker66a19492015-12-10 10:19:45 -0600819 if (Mount_Point == "/data") {
820 Is_Settings_Storage = true;
821 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
822 Make_Dir("/emmc", false);
823 Symlink_Mount_Point = "/emmc";
824 } else {
825 Make_Dir("/sdcard", false);
826 Symlink_Mount_Point = "/sdcard";
827 }
828 if (Mount(false) && TWFunc::Path_Exists(Mount_Point + "/media/0")) {
829 Storage_Path = Mount_Point + "/media/0";
830 Symlink_Path = Storage_Path;
831 DataManager::SetValue(TW_INTERNAL_PATH, Mount_Point + "/media/0");
832 UnMount(true);
833 }
834 DataManager::SetValue("tw_has_internal", 1);
835 DataManager::SetValue("tw_has_data_media", 1);
836 du.add_absolute_dir(Mount_Point + "/misc/vold");
837 du.add_absolute_dir(Mount_Point + "/.layout_version");
838 du.add_absolute_dir(Mount_Point + "/system/storage.xml");
Ethan Yonker6277c792014-09-15 14:54:30 -0500839 } else {
Ethan Yonker66a19492015-12-10 10:19:45 -0600840 if (Mount(true) && TWFunc::Path_Exists(Mount_Point + "/media/0")) {
841 Storage_Path = Mount_Point + "/media/0";
842 Symlink_Path = Storage_Path;
843 UnMount(true);
844 }
Ethan Yonker6277c792014-09-15 14:54:30 -0500845 }
Ethan Yonker66a19492015-12-10 10:19:45 -0600846 du.add_absolute_dir(Mount_Point + "/media");
Ethan Yonker6277c792014-09-15 14:54:30 -0500847}
848
Dees_Troy5bf43922012-09-07 16:07:55 -0400849void TWPartition::Find_Real_Block_Device(string& Block, bool Display_Error) {
850 char device[512], realDevice[512];
851
852 strcpy(device, Block.c_str());
853 memset(realDevice, 0, sizeof(realDevice));
854 while (readlink(device, realDevice, sizeof(realDevice)) > 0)
855 {
856 strcpy(device, realDevice);
857 memset(realDevice, 0, sizeof(realDevice));
858 }
859
860 if (device[0] != '/') {
861 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000862 LOGERR("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400863 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000864 LOGINFO("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400865 return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400866 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400867 Block = device;
868 return;
869 }
870}
871
Dees_Troy8e337f32012-10-13 22:07:49 -0400872void TWPartition::Mount_Storage_Retry(void) {
873 // On some devices, storage doesn't want to mount right away, retry and sleep
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500874 if (!Mount(true)) {
Dees_Troy8e337f32012-10-13 22:07:49 -0400875 int retry_count = 5;
876 while (retry_count > 0 && !Mount(false)) {
877 usleep(500000);
878 retry_count--;
879 }
880 Mount(true);
881 }
882}
883
Dees_Troy38bd7602012-09-14 13:33:53 -0400884bool TWPartition::Find_MTD_Block_Device(string MTD_Name) {
885 FILE *fp = NULL;
886 char line[255];
887
888 fp = fopen("/proc/mtd", "rt");
889 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000890 LOGERR("Device does not support /proc/mtd\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400891 return false;
892 }
893
894 while (fgets(line, sizeof(line), fp) != NULL)
895 {
896 char device[32], label[32];
897 unsigned long size = 0;
898 char* fstype = NULL;
899 int deviceId;
900
901 sscanf(line, "%s %lx %*s %*c%s", device, &size, label);
902
903 // Skip header and blank lines
904 if ((strcmp(device, "dev:") == 0) || (strlen(line) < 8))
905 continue;
906
907 // Strip off the trailing " from the label
908 label[strlen(label)-1] = '\0';
909
910 if (strcmp(label, MTD_Name.c_str()) == 0) {
911 // We found our device
912 // Strip off the trailing : from the device
913 device[strlen(device)-1] = '\0';
914 if (sscanf(device,"mtd%d", &deviceId) == 1) {
915 sprintf(device, "/dev/block/mtdblock%d", deviceId);
916 Primary_Block_Device = device;
Dees_Troy76543db2013-06-19 16:24:30 +0000917 fclose(fp);
918 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -0400919 }
920 }
921 }
922 fclose(fp);
923
924 return false;
925}
926
Dees_Troy51127312012-09-08 13:08:49 -0400927bool TWPartition::Get_Size_Via_statfs(bool Display_Error) {
928 struct statfs st;
929 string Local_Path = Mount_Point + "/.";
930
931 if (!Mount(Display_Error))
932 return false;
933
934 if (statfs(Local_Path.c_str(), &st) != 0) {
935 if (!Removable) {
936 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000937 LOGERR("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400938 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000939 LOGINFO("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400940 }
941 return false;
942 }
943 Size = (st.f_blocks * st.f_bsize);
944 Used = ((st.f_blocks - st.f_bfree) * st.f_bsize);
945 Free = (st.f_bfree * st.f_bsize);
946 Backup_Size = Used;
947 return true;
948}
949
950bool TWPartition::Get_Size_Via_df(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400951 FILE* fp;
952 char command[255], line[512];
953 int include_block = 1;
954 unsigned int min_len;
955
956 if (!Mount(Display_Error))
957 return false;
958
Dees_Troy38bd7602012-09-14 13:33:53 -0400959 min_len = Actual_Block_Device.size() + 2;
Dees_Troy51127312012-09-08 13:08:49 -0400960 sprintf(command, "df %s > /tmp/dfoutput.txt", Mount_Point.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +0200961 TWFunc::Exec_Cmd(command);
Dees_Troy51127312012-09-08 13:08:49 -0400962 fp = fopen("/tmp/dfoutput.txt", "rt");
963 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000964 LOGINFO("Unable to open /tmp/dfoutput.txt.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400965 return false;
Dees_Troy51127312012-09-08 13:08:49 -0400966 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400967
968 while (fgets(line, sizeof(line), fp) != NULL)
969 {
970 unsigned long blocks, used, available;
971 char device[64];
972 char tmpString[64];
973
974 if (strncmp(line, "Filesystem", 10) == 0)
975 continue;
976 if (strlen(line) < min_len) {
977 include_block = 0;
978 continue;
979 }
980 if (include_block) {
981 sscanf(line, "%s %lu %lu %lu", device, &blocks, &used, &available);
982 } else {
983 // The device block string is so long that the df information is on the next line
984 int space_count = 0;
Dees_Troye58d5262012-09-21 12:27:57 -0400985 sprintf(tmpString, "/dev/block/%s", Actual_Block_Device.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400986 while (tmpString[space_count] == 32)
987 space_count++;
988 sscanf(line + space_count, "%lu %lu %lu", &blocks, &used, &available);
989 }
990
991 // Adjust block size to byte size
992 Size = blocks * 1024ULL;
993 Used = used * 1024ULL;
994 Free = available * 1024ULL;
995 Backup_Size = Used;
996 }
997 fclose(fp);
998 return true;
999}
1000
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001001unsigned long long TWPartition::IOCTL_Get_Block_Size() {
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001002 Find_Actual_Block_Device();
Ethan Yonker483e9f42016-01-11 22:21:18 -06001003
1004 return TWFunc::IOCTL_Get_Block_Size(Actual_Block_Device.c_str());
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001005}
1006
Dees_Troy5bf43922012-09-07 16:07:55 -04001007bool TWPartition::Find_Partition_Size(void) {
1008 FILE* fp;
1009 char line[512];
1010 string tmpdevice;
1011
igoriok87e3d932013-01-31 21:03:53 +02001012 fp = fopen("/proc/dumchar_info", "rt");
1013 if (fp != NULL) {
1014 while (fgets(line, sizeof(line), fp) != NULL)
1015 {
1016 char label[32], device[32];
1017 unsigned long size = 0;
1018
thatd43bf2d2014-09-21 23:13:02 +02001019 sscanf(line, "%s %lx %*x %*u %s", label, &size, device);
igoriok87e3d932013-01-31 21:03:53 +02001020
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05001021 // Skip header, annotation and blank lines
igoriok87e3d932013-01-31 21:03:53 +02001022 if ((strncmp(device, "/dev/", 5) != 0) || (strlen(line) < 8))
1023 continue;
1024
1025 tmpdevice = "/dev/";
1026 tmpdevice += label;
1027 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
1028 Size = size;
1029 fclose(fp);
1030 return true;
1031 }
1032 }
1033 }
1034
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001035 unsigned long long ioctl_size = IOCTL_Get_Block_Size();
1036 if (ioctl_size) {
1037 Size = ioctl_size;
1038 return true;
1039 }
1040
Dees_Troy5bf43922012-09-07 16:07:55 -04001041 // In this case, we'll first get the partitions we care about (with labels)
1042 fp = fopen("/proc/partitions", "rt");
1043 if (fp == NULL)
1044 return false;
1045
1046 while (fgets(line, sizeof(line), fp) != NULL)
1047 {
1048 unsigned long major, minor, blocks;
1049 char device[512];
1050 char tmpString[64];
1051
Dees_Troy63c8df72012-09-10 14:02:05 -04001052 if (strlen(line) < 7 || line[0] == 'm') continue;
Dees_Troy5bf43922012-09-07 16:07:55 -04001053 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
1054
1055 tmpdevice = "/dev/block/";
1056 tmpdevice += device;
Dees_Troy38bd7602012-09-14 13:33:53 -04001057 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001058 // Adjust block size to byte size
1059 Size = blocks * 1024ULL;
1060 fclose(fp);
1061 return true;
1062 }
1063 }
1064 fclose(fp);
1065 return false;
1066}
1067
Dees_Troy5bf43922012-09-07 16:07:55 -04001068bool TWPartition::Is_Mounted(void) {
1069 if (!Can_Be_Mounted)
1070 return false;
1071
1072 struct stat st1, st2;
1073 string test_path;
1074
1075 // Check to see if the mount point directory exists
1076 test_path = Mount_Point + "/.";
1077 if (stat(test_path.c_str(), &st1) != 0) return false;
1078
1079 // Check to see if the directory above the mount point exists
1080 test_path = Mount_Point + "/../.";
1081 if (stat(test_path.c_str(), &st2) != 0) return false;
1082
1083 // Compare the device IDs -- if they match then we're (probably) using tmpfs instead of an actual device
1084 int ret = (st1.st_dev != st2.st_dev) ? true : false;
1085
1086 return ret;
1087}
1088
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05001089bool TWPartition::Is_File_System_Writable(void) {
1090 if (!Is_File_System(Current_File_System) || !Is_Mounted())
1091 return false;
1092
1093 string test_path = Mount_Point + "/.";
1094 return (access(test_path.c_str(), W_OK) == 0);
1095}
1096
Dees_Troy5bf43922012-09-07 16:07:55 -04001097bool TWPartition::Mount(bool Display_Error) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001098 int exfat_mounted = 0;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001099 unsigned long flags = Mount_Flags;
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001100
Dees_Troy5bf43922012-09-07 16:07:55 -04001101 if (Is_Mounted()) {
1102 return true;
1103 } else if (!Can_Be_Mounted) {
1104 return false;
1105 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001106
1107 Find_Actual_Block_Device();
1108
1109 // Check the current file system before mounting
1110 Check_FS_Type();
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001111 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/exfat-fuse")) {
Dees_Troye34c1332013-02-06 19:13:00 +00001112 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 +00001113 LOGINFO("cmd: %s\n", cmd.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001114 string result;
1115 if (TWFunc::Exec_Cmd(cmd, result) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001116 LOGINFO("exfat-fuse failed to mount with result '%s', trying vfat\n", result.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001117 Current_File_System = "vfat";
1118 } else {
1119#ifdef TW_NO_EXFAT_FUSE
1120 UnMount(false);
1121 // We'll let the kernel handle it but using exfat-fuse to detect if the file system is actually exfat
1122 // Some kernels let us mount vfat as exfat which doesn't work out too well
1123#else
1124 exfat_mounted = 1;
1125#endif
1126 }
1127 }
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001128
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001129 if (Current_File_System == "ntfs" && (TWFunc::Path_Exists("/sbin/ntfs-3g") || TWFunc::Path_Exists("/sbin/mount.ntfs"))) {
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001130 string cmd;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001131 string Ntfsmount_Binary = "";
1132
1133 if (TWFunc::Path_Exists("/sbin/ntfs-3g"))
1134 Ntfsmount_Binary = "ntfs-3g";
1135 else if (TWFunc::Path_Exists("/sbin/mount.ntfs"))
1136 Ntfsmount_Binary = "mount.ntfs";
1137
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001138 if (Mount_Read_Only)
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001139 cmd = "/sbin/" + Ntfsmount_Binary + " -o ro " + Actual_Block_Device + " " + Mount_Point;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001140 else
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001141 cmd = "/sbin/" + Ntfsmount_Binary + " " + Actual_Block_Device + " " + Mount_Point;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001142 LOGINFO("cmd: '%s'\n", cmd.c_str());
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001143
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001144 if (TWFunc::Exec_Cmd(cmd) == 0) {
1145 return true;
1146 } else {
1147 LOGINFO("ntfs-3g failed to mount, trying regular mount method.\n");
1148 }
1149 }
1150
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001151 if (Mount_Read_Only)
1152 flags |= MS_RDONLY;
1153
Dees_Troy22042032012-12-18 21:23:08 +00001154 if (Fstab_File_System == "yaffs2") {
1155 // mount an MTD partition as a YAFFS2 filesystem.
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001156 flags = MS_NOATIME | MS_NODEV | MS_NODIRATIME;
1157 if (Mount_Read_Only)
1158 flags |= MS_RDONLY;
Dees_Troy76543db2013-06-19 16:24:30 +00001159 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags, NULL) < 0) {
1160 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags | MS_RDONLY, NULL) < 0) {
1161 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001162 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy76543db2013-06-19 16:24:30 +00001163 else
1164 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
1165 return false;
1166 } else {
1167 LOGINFO("Mounted '%s' (MTD) as RO\n", Mount_Point.c_str());
1168 return true;
1169 }
1170 } else {
1171 struct stat st;
1172 string test_path = Mount_Point;
1173 if (stat(test_path.c_str(), &st) < 0) {
1174 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001175 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy76543db2013-06-19 16:24:30 +00001176 else
1177 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
1178 return false;
1179 }
1180 mode_t new_mode = st.st_mode | S_IXUSR | S_IXGRP | S_IXOTH;
1181 if (new_mode != st.st_mode) {
1182 LOGINFO("Fixing execute permissions for %s\n", Mount_Point.c_str());
1183 if (chmod(Mount_Point.c_str(), new_mode) < 0) {
1184 if (Display_Error)
1185 LOGERR("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
1186 else
1187 LOGINFO("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
1188 return false;
1189 }
1190 }
Dees_Troy22042032012-12-18 21:23:08 +00001191 return true;
Dees_Troy76543db2013-06-19 16:24:30 +00001192 }
thatc7572eb2015-03-31 18:55:30 +02001193 }
1194
1195 string mount_fs = Current_File_System;
1196 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sys/module/texfat"))
1197 mount_fs = "texfat";
1198
1199 if (!exfat_mounted &&
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001200 mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), mount_fs.c_str(), flags, Mount_Options.c_str()) != 0 &&
1201 mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), mount_fs.c_str(), flags, NULL) != 0) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001202#ifdef TW_NO_EXFAT_FUSE
1203 if (Current_File_System == "exfat") {
Dees_Troy2673cec2013-04-02 20:22:16 +00001204 LOGINFO("Mounting exfat failed, trying vfat...\n");
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001205 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), "vfat", 0, NULL) != 0) {
Dees_Troy85f44ed2013-01-09 18:42:36 +00001206 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001207 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy85f44ed2013-01-09 18:42:36 +00001208 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001209 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001210 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 +00001211 return false;
Dees_Troy85f44ed2013-01-09 18:42:36 +00001212 }
Dees_Troyb05ddee2013-01-28 20:24:50 +00001213 } else {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001214#endif
bigbiff bigbiff26774a02014-03-29 18:22:00 -04001215 if (!Removable && Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001216 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001217 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001218 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
1219 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 +00001220 return false;
1221#ifdef TW_NO_EXFAT_FUSE
Dees_Troy85f44ed2013-01-09 18:42:36 +00001222 }
1223#endif
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001224 }
Ethan Yonker253368a2014-11-25 15:00:52 -06001225
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001226 if (Removable)
1227 Update_Size(Display_Error);
1228
xiaolu9416f4f2015-06-04 08:22:23 +08001229 if (!Symlink_Mount_Point.empty() && TWFunc::Path_Exists(Symlink_Path)) {
codelover2a3d4ce2015-03-14 20:26:49 +08001230 string Command = "mount -o bind '" + Symlink_Path + "' '" + Symlink_Mount_Point + "'";
Vojtech Bocek05534202013-09-11 08:11:56 +02001231 TWFunc::Exec_Cmd(Command);
Dees_Troy5bf43922012-09-07 16:07:55 -04001232 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001233 return true;
1234}
1235
1236bool TWPartition::UnMount(bool Display_Error) {
1237 if (Is_Mounted()) {
1238 int never_unmount_system;
1239
1240 DataManager::GetValue(TW_DONT_UNMOUNT_SYSTEM, never_unmount_system);
1241 if (never_unmount_system == 1 && Mount_Point == "/system")
1242 return true; // Never unmount system if you're not supposed to unmount it
1243
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001244 if (Is_Storage)
Ethan Yonker726a0202014-12-16 20:01:38 -06001245 PartitionManager.Remove_MTP_Storage(MTP_Storage_ID);
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001246
Dees_Troy38bd7602012-09-14 13:33:53 -04001247 if (!Symlink_Mount_Point.empty())
1248 umount(Symlink_Mount_Point.c_str());
1249
Dees_Troyb05ddee2013-01-28 20:24:50 +00001250 umount(Mount_Point.c_str());
1251 if (Is_Mounted()) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001252 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001253 gui_msg(Msg(msg::kError, "fail_unmount=Failed to unmount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy5bf43922012-09-07 16:07:55 -04001254 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001255 LOGINFO("Unable to unmount '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001256 return false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001257 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -04001258 return true;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001259 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001260 } else {
1261 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001262 }
1263}
1264
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05001265bool TWPartition::ReMount(bool Display_Error) {
1266 if (UnMount(Display_Error))
1267 return Mount(Display_Error);
1268 return false;
1269}
1270
1271bool TWPartition::ReMount_RW(bool Display_Error) {
1272 // No need to remount if already mounted rw
1273 if (Is_File_System_Writable())
1274 return true;
1275
1276 bool ro = Mount_Read_Only;
1277 int flags = Mount_Flags;
1278
1279 Mount_Read_Only = false;
1280 Mount_Flags &= ~MS_RDONLY;
1281
1282 bool ret = ReMount(Display_Error);
1283
1284 Mount_Read_Only = ro;
1285 Mount_Flags = flags;
1286
1287 return ret;
1288}
1289
Gary Peck43acadf2012-11-21 21:19:01 -08001290bool TWPartition::Wipe(string New_File_System) {
Ethan Yonker726a0202014-12-16 20:01:38 -06001291 bool wiped = false, update_crypt = false, recreate_media = true;
Dees_Troy16c2b312013-01-15 16:51:18 +00001292 int check;
1293 string Layout_Filename = Mount_Point + "/.layout_version";
1294
Dees_Troy38bd7602012-09-14 13:33:53 -04001295 if (!Can_Be_Wiped) {
Matt Mower3c366972015-12-25 19:28:31 -06001296 gui_msg(Msg(msg::kError, "cannot_wipe=Partition {1} cannot be wiped.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04001297 return false;
1298 }
1299
Dees_Troyc51f1f92012-09-20 15:32:13 -04001300 if (Mount_Point == "/cache")
Dees_Troy2673cec2013-04-02 20:22:16 +00001301 Log_Offset = 0;
Dees_Troyc51f1f92012-09-20 15:32:13 -04001302
Dees_Troy16c2b312013-01-15 16:51:18 +00001303 if (Retain_Layout_Version && Mount(false) && TWFunc::Path_Exists(Layout_Filename))
1304 TWFunc::copy_file(Layout_Filename, "/.layout_version", 0600);
1305 else
1306 unlink("/.layout_version");
Dees_Troy38bd7602012-09-14 13:33:53 -04001307
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001308 if (Has_Data_Media && Current_File_System == New_File_System) {
Dees_Troy16c2b312013-01-15 16:51:18 +00001309 wiped = Wipe_Data_Without_Wiping_Media();
Ethan Yonker5eac2222014-06-11 12:22:55 -05001310 recreate_media = false;
Dees_Troy16c2b312013-01-15 16:51:18 +00001311 } else {
Dees_Troy16c2b312013-01-15 16:51:18 +00001312 DataManager::GetValue(TW_RM_RF_VAR, check);
1313
Hashcodedabfd492013-08-29 22:45:30 -07001314 if (check || Use_Rm_Rf)
Dees_Troy16c2b312013-01-15 16:51:18 +00001315 wiped = Wipe_RMRF();
1316 else if (New_File_System == "ext4")
1317 wiped = Wipe_EXT4();
1318 else if (New_File_System == "ext2" || New_File_System == "ext3")
1319 wiped = Wipe_EXT23(New_File_System);
1320 else if (New_File_System == "vfat")
1321 wiped = Wipe_FAT();
1322 else if (New_File_System == "exfat")
1323 wiped = Wipe_EXFAT();
1324 else if (New_File_System == "yaffs2")
1325 wiped = Wipe_MTD();
Dees_Troye5017042013-08-29 16:38:55 +00001326 else if (New_File_System == "f2fs")
1327 wiped = Wipe_F2FS();
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001328 else if (New_File_System == "ntfs")
1329 wiped = Wipe_NTFS();
Dees_Troy16c2b312013-01-15 16:51:18 +00001330 else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001331 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 +00001332 unlink("/.layout_version");
1333 return false;
1334 }
1335 update_crypt = wiped;
Gary Pecke8bc5d72012-12-21 06:45:25 -08001336 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001337
Gary Pecke8bc5d72012-12-21 06:45:25 -08001338 if (wiped) {
Dees_Troy1c1ac442013-01-17 21:42:14 +00001339 if (Mount_Point == "/cache")
1340 DataManager::Output_Version();
1341
Dees_Troy16c2b312013-01-15 16:51:18 +00001342 if (TWFunc::Path_Exists("/.layout_version") && Mount(false))
1343 TWFunc::copy_file("/.layout_version", Layout_Filename, 0600);
1344
1345 if (update_crypt) {
1346 Setup_File_System(false);
1347 if (Is_Encrypted && !Is_Decrypted) {
1348 // just wiped an encrypted partition back to its unencrypted state
1349 Is_Encrypted = false;
1350 Is_Decrypted = false;
1351 Decrypted_Block_Device = "";
1352 if (Mount_Point == "/data") {
1353 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1354 DataManager::SetValue(TW_IS_DECRYPTED, 0);
1355 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001356 }
1357 }
Ethan Yonker5eac2222014-06-11 12:22:55 -05001358
Ethan Yonker66a19492015-12-10 10:19:45 -06001359 if (Has_Data_Media && recreate_media) {
Ethan Yonker5eac2222014-06-11 12:22:55 -05001360 Recreate_Media_Folder();
1361 }
Matt Mower209c9632016-01-20 12:08:35 -06001362 if (Is_Storage && Mount(false))
1363 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Gary Pecke8bc5d72012-12-21 06:45:25 -08001364 }
Matt Mower209c9632016-01-20 12:08:35 -06001365
Gary Pecke8bc5d72012-12-21 06:45:25 -08001366 return wiped;
Dees_Troy51a0e822012-09-05 15:24:24 -04001367}
1368
Gary Peck43acadf2012-11-21 21:19:01 -08001369bool TWPartition::Wipe() {
Gary Peck82599a82012-11-21 16:23:12 -08001370 if (Is_File_System(Current_File_System))
1371 return Wipe(Current_File_System);
1372 else
1373 return Wipe(Fstab_File_System);
Gary Peck43acadf2012-11-21 21:19:01 -08001374}
1375
Dees_Troye58d5262012-09-21 12:27:57 -04001376bool TWPartition::Wipe_AndSec(void) {
1377 if (!Has_Android_Secure)
1378 return false;
1379
Dees_Troye58d5262012-09-21 12:27:57 -04001380 if (!Mount(true))
1381 return false;
1382
Ethan Yonker74db1572015-10-28 12:44:49 -05001383 gui_msg(Msg("wiping=Wiping {1}")(Backup_Display_Name));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001384 TWFunc::removeDir(Mount_Point + "/.android_secure/", true);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001385 return true;
Dees_Troye58d5262012-09-21 12:27:57 -04001386}
1387
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001388bool TWPartition::Can_Repair() {
Ethan Yonkera2719152015-05-28 09:44:41 -05001389 if (Mount_Read_Only)
1390 return false;
Matt Mower18794c82015-11-11 16:22:45 -06001391 if (Current_File_System == "vfat" && TWFunc::Path_Exists("/sbin/fsck.fat"))
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001392 return true;
1393 else if ((Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") && TWFunc::Path_Exists("/sbin/e2fsck"))
1394 return true;
1395 else if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/fsck.exfat"))
1396 return true;
1397 else if (Current_File_System == "f2fs" && TWFunc::Path_Exists("/sbin/fsck.f2fs"))
1398 return true;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001399 else if (Current_File_System == "ntfs" && (TWFunc::Path_Exists("/sbin/ntfsfix") || TWFunc::Path_Exists("/sbin/fsck.ntfs")))
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001400 return true;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001401 return false;
1402}
1403
1404bool TWPartition::Repair() {
1405 string command;
1406
1407 if (Current_File_System == "vfat") {
Matt Mower18794c82015-11-11 16:22:45 -06001408 if (!TWFunc::Path_Exists("/sbin/fsck.fat")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001409 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.fat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001410 return false;
1411 }
1412 if (!UnMount(true))
1413 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001414 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.fat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001415 Find_Actual_Block_Device();
Matt Mower18794c82015-11-11 16:22:45 -06001416 command = "/sbin/fsck.fat -y " + Actual_Block_Device;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001417 LOGINFO("Repair command: %s\n", command.c_str());
1418 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001419 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001420 return true;
1421 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001422 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001423 return false;
1424 }
1425 }
1426 if (Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") {
1427 if (!TWFunc::Path_Exists("/sbin/e2fsck")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001428 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("e2fsck"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001429 return false;
1430 }
1431 if (!UnMount(true))
1432 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001433 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("e2fsck"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001434 Find_Actual_Block_Device();
Ethan Yonkera2719152015-05-28 09:44:41 -05001435 command = "/sbin/e2fsck -fp " + Actual_Block_Device;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001436 LOGINFO("Repair command: %s\n", command.c_str());
1437 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001438 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001439 return true;
1440 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001441 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001442 return false;
1443 }
1444 }
1445 if (Current_File_System == "exfat") {
1446 if (!TWFunc::Path_Exists("/sbin/fsck.exfat")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001447 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.exfat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001448 return false;
1449 }
1450 if (!UnMount(true))
1451 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001452 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.exfat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001453 Find_Actual_Block_Device();
1454 command = "/sbin/fsck.exfat " + Actual_Block_Device;
1455 LOGINFO("Repair command: %s\n", command.c_str());
1456 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001457 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001458 return true;
1459 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001460 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001461 return false;
1462 }
1463 }
1464 if (Current_File_System == "f2fs") {
1465 if (!TWFunc::Path_Exists("/sbin/fsck.f2fs")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001466 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.f2fs"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001467 return false;
1468 }
1469 if (!UnMount(true))
1470 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001471 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.f2fs"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001472 Find_Actual_Block_Device();
1473 command = "/sbin/fsck.f2fs " + Actual_Block_Device;
1474 LOGINFO("Repair command: %s\n", command.c_str());
1475 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001476 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001477 return true;
1478 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001479 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001480 return false;
1481 }
1482 }
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001483 if (Current_File_System == "ntfs") {
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001484 string Ntfsfix_Binary;
1485 if (TWFunc::Path_Exists("/sbin/ntfsfix"))
1486 Ntfsfix_Binary = "ntfsfix";
1487 else if (TWFunc::Path_Exists("/sbin/fsck.ntfs"))
1488 Ntfsfix_Binary = "fsck.ntfs";
1489 else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001490 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("ntfsfix"));
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001491 return false;
1492 }
1493 if (!UnMount(true))
1494 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001495 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)(Ntfsfix_Binary));
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001496 Find_Actual_Block_Device();
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001497 command = "/sbin/" + Ntfsfix_Binary + " " + Actual_Block_Device;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001498 LOGINFO("Repair command: %s\n", command.c_str());
1499 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001500 gui_msg("done=Done.");
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001501 return true;
1502 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001503 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001504 return false;
1505 }
1506 }
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001507 return false;
1508}
1509
Ethan Yonkera2719152015-05-28 09:44:41 -05001510bool TWPartition::Can_Resize() {
1511 if (Mount_Read_Only)
1512 return false;
1513 if ((Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") && TWFunc::Path_Exists("/sbin/resize2fs"))
1514 return true;
1515 return false;
1516}
1517
1518bool TWPartition::Resize() {
1519 string command;
1520
1521 if (Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") {
1522 if (!Can_Repair()) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001523 LOGINFO("Cannot resize %s because %s cannot be repaired before resizing.\n", Display_Name.c_str(), Display_Name.c_str());
1524 gui_msg(Msg(msg::kError, "cannot_resize=Cannot resize {1}.")(Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001525 return false;
1526 }
1527 if (!TWFunc::Path_Exists("/sbin/resize2fs")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001528 LOGINFO("resize2fs does not exist! Cannot resize!\n");
1529 gui_msg(Msg(msg::kError, "cannot_resize=Cannot resize {1}.")(Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001530 return false;
1531 }
1532 // Repair will unmount so no need to do it twice
Ethan Yonker74db1572015-10-28 12:44:49 -05001533 gui_msg(Msg("repair_resize=Repairing {1} before resizing.")( Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001534 if (!Repair())
1535 return false;
Ethan Yonker74db1572015-10-28 12:44:49 -05001536 gui_msg(Msg("resizing=Resizing {1} using {2}...")(Display_Name)("resize2fs"));
Ethan Yonkera2719152015-05-28 09:44:41 -05001537 Find_Actual_Block_Device();
1538 command = "/sbin/resize2fs " + Actual_Block_Device;
1539 if (Length != 0) {
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001540 unsigned long long Actual_Size = IOCTL_Get_Block_Size();
1541 if (Actual_Size == 0)
1542 return false;
Ethan Yonkera2719152015-05-28 09:44:41 -05001543
Ethan Yonkera2719152015-05-28 09:44:41 -05001544 unsigned long long Block_Count;
1545 if (Length < 0) {
1546 // Reduce overall size by this length
1547 Block_Count = (Actual_Size / 1024LLU) - ((unsigned long long)(Length * -1) / 1024LLU);
1548 } else {
1549 // This is the size, not a size reduction
1550 Block_Count = ((unsigned long long)(Length) / 1024LLU);
1551 }
1552 char temp[256];
1553 sprintf(temp, "%llu", Block_Count);
1554 command += " ";
1555 command += temp;
1556 command += "K";
1557 }
1558 LOGINFO("Resize command: %s\n", command.c_str());
1559 if (TWFunc::Exec_Cmd(command) == 0) {
1560 Update_Size(true);
Ethan Yonker74db1572015-10-28 12:44:49 -05001561 gui_msg("done=Done.");
Ethan Yonkera2719152015-05-28 09:44:41 -05001562 return true;
1563 } else {
1564 Update_Size(true);
Ethan Yonker74db1572015-10-28 12:44:49 -05001565 gui_msg(Msg(msg::kError, "unable_resize=Unable to resize {1}.")(Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001566 return false;
1567 }
1568 }
1569 return false;
1570}
1571
Ethan Yonker472f5062016-02-25 13:47:30 -06001572bool TWPartition::Backup(const string& backup_folder, pid_t &tar_fork_pid, ProgressTracking *progress) {
bigbiff7abc5fe2015-01-17 16:53:12 -05001573 if (Backup_Method == FILES) {
Ethan Yonker472f5062016-02-25 13:47:30 -06001574 return Backup_Tar(backup_folder, progress, tar_fork_pid);
bigbiff7abc5fe2015-01-17 16:53:12 -05001575 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001576 else if (Backup_Method == DD)
Ethan Yonker472f5062016-02-25 13:47:30 -06001577 return Backup_Image(backup_folder, progress);
Dees_Troy38bd7602012-09-14 13:33:53 -04001578 else if (Backup_Method == FLASH_UTILS)
Ethan Yonker472f5062016-02-25 13:47:30 -06001579 return Backup_Dump_Image(backup_folder, progress);
Dees_Troy2673cec2013-04-02 20:22:16 +00001580 LOGERR("Unknown backup method for '%s'\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001581 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001582}
1583
Dees_Troy43d8b002012-09-17 16:00:01 -04001584bool TWPartition::Check_MD5(string restore_folder) {
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001585 string Full_Filename, md5file;
Dees_Troy43d8b002012-09-17 16:00:01 -04001586 char split_filename[512];
1587 int index = 0;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001588 twrpDigest md5sum;
Dees_Troy43d8b002012-09-17 16:00:01 -04001589
Captain Throwbackff5935f2014-09-23 16:08:34 -04001590 sync();
1591
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001592 memset(split_filename, 0, sizeof(split_filename));
Dees_Troy43d8b002012-09-17 16:00:01 -04001593 Full_Filename = restore_folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04001594 if (!TWFunc::Path_Exists(Full_Filename)) {
1595 // This is a split archive, we presume
1596 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
Dees_Troy2673cec2013-04-02 20:22:16 +00001597 LOGINFO("split_filename: %s\n", split_filename);
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001598 md5file = split_filename;
1599 md5file += ".md5";
1600 if (!TWFunc::Path_Exists(md5file)) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001601 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 -04001602 return false;
1603 }
1604 md5sum.setfn(split_filename);
Dees_Troy83bd4832013-05-04 12:39:56 +00001605 while (index < 1000) {
1606 if (TWFunc::Path_Exists(split_filename) && md5sum.verify_md5digest() != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001607 gui_msg(Msg(msg::kError, "md5_fail_match=MD5 failed to match on '{1}'.")(split_filename));
Dees_Troy43d8b002012-09-17 16:00:01 -04001608 return false;
1609 }
1610 index++;
1611 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001612 md5sum.setfn(split_filename);
Dees_Troy43d8b002012-09-17 16:00:01 -04001613 }
Dees_Troy4a2a1262012-09-18 09:33:47 -04001614 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001615 } else {
1616 // Single file archive
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001617 md5file = Full_Filename + ".md5";
1618 if (!TWFunc::Path_Exists(md5file)) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001619 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 -04001620 return false;
1621 }
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001622 md5sum.setfn(Full_Filename);
1623 if (md5sum.verify_md5digest() != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001624 gui_msg(Msg(msg::kError, "md5_fail_match=MD5 failed to match on '{1}'.")(split_filename));
Dees_Troy43d8b002012-09-17 16:00:01 -04001625 return false;
1626 } else
1627 return true;
1628 }
1629 return false;
1630}
1631
Ethan Yonker472f5062016-02-25 13:47:30 -06001632bool TWPartition::Restore(const string& restore_folder, ProgressTracking *progress) {
Gary Peck43acadf2012-11-21 21:19:01 -08001633 string Restore_File_System;
1634
Matt Mower3c366972015-12-25 19:28:31 -06001635 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, gui_parse_text("{@restoring_hdr}"));
Dees_Troy2673cec2013-04-02 20:22:16 +00001636 LOGINFO("Restore filename is: %s\n", Backup_FileName.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001637
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001638 Restore_File_System = Get_Restore_File_System(restore_folder);
1639
1640 if (Is_File_System(Restore_File_System))
Ethan Yonker472f5062016-02-25 13:47:30 -06001641 return Restore_Tar(restore_folder, Restore_File_System, progress);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001642 else if (Is_Image(Restore_File_System)) {
Ethan Yonker472f5062016-02-25 13:47:30 -06001643 return Restore_Image(restore_folder, Restore_File_System, progress);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001644 }
1645
1646 LOGERR("Unknown restore method for '%s'\n", Mount_Point.c_str());
1647 return false;
1648}
1649
Ethan Yonker472f5062016-02-25 13:47:30 -06001650string TWPartition::Get_Restore_File_System(const string& restore_folder) {
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001651 size_t first_period, second_period;
1652 string Restore_File_System;
1653
Gary Peck43acadf2012-11-21 21:19:01 -08001654 // Parse backup filename to extract the file system before wiping
1655 first_period = Backup_FileName.find(".");
1656 if (first_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001657 LOGERR("Unable to find file system (first period).\n");
thatd43bf2d2014-09-21 23:13:02 +02001658 return string();
Gary Peck43acadf2012-11-21 21:19:01 -08001659 }
1660 Restore_File_System = Backup_FileName.substr(first_period + 1, Backup_FileName.size() - first_period - 1);
1661 second_period = Restore_File_System.find(".");
1662 if (second_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001663 LOGERR("Unable to find file system (second period).\n");
thatd43bf2d2014-09-21 23:13:02 +02001664 return string();
Gary Peck43acadf2012-11-21 21:19:01 -08001665 }
1666 Restore_File_System.resize(second_period);
Dees_Troy2673cec2013-04-02 20:22:16 +00001667 LOGINFO("Restore file system is: '%s'.\n", Restore_File_System.c_str());
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001668 return Restore_File_System;
Dees_Troy51a0e822012-09-05 15:24:24 -04001669}
1670
1671string TWPartition::Backup_Method_By_Name() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001672 if (Backup_Method == NONE)
1673 return "none";
1674 else if (Backup_Method == FILES)
1675 return "files";
1676 else if (Backup_Method == DD)
1677 return "dd";
1678 else if (Backup_Method == FLASH_UTILS)
1679 return "flash_utils";
1680 else
1681 return "undefined";
1682 return "ERROR!";
Dees_Troy51a0e822012-09-05 15:24:24 -04001683}
1684
1685bool TWPartition::Decrypt(string Password) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001686 LOGINFO("STUB TWPartition::Decrypt, password: '%s'\n", Password.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001687 // Is this needed?
Dees_Troy51a0e822012-09-05 15:24:24 -04001688 return 1;
1689}
1690
1691bool TWPartition::Wipe_Encryption() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001692 bool Save_Data_Media = Has_Data_Media;
1693
1694 if (!UnMount(true))
1695 return false;
1696
Dees_Troy38bd7602012-09-14 13:33:53 -04001697 Has_Data_Media = false;
Dees_Troy74fb2e92013-04-15 14:35:47 +00001698 Decrypted_Block_Device = "";
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001699#ifdef TW_INCLUDE_CRYPTO
1700 if (Is_Decrypted) {
1701 if (!UnMount(true))
1702 return false;
Matt Mower2b18a532015-02-20 16:58:05 -06001703 if (delete_crypto_blk_dev((char*)("userdata")) != 0) {
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001704 LOGERR("Error deleting crypto block device, continuing anyway.\n");
1705 }
1706 }
1707#endif
Dees_Troy74fb2e92013-04-15 14:35:47 +00001708 Is_Decrypted = false;
1709 Is_Encrypted = false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001710 Find_Actual_Block_Device();
Ethan Yonkerbc85b632015-08-09 12:48:14 -05001711 if (Crypto_Key_Location == "footer") {
1712 int newlen, fd;
1713 if (Length != 0) {
1714 newlen = Length;
1715 if (newlen < 0)
1716 newlen = newlen * -1;
1717 } else {
1718 newlen = CRYPT_FOOTER_OFFSET;
1719 }
1720 if ((fd = open(Actual_Block_Device.c_str(), O_RDWR)) < 0) {
1721 gui_print_color("warning", "Unable to open '%s' to wipe crypto key\n", Actual_Block_Device.c_str());
1722 } else {
1723 unsigned int block_count;
1724 if ((ioctl(fd, BLKGETSIZE, &block_count)) == -1) {
1725 gui_print_color("warning", "Unable to get block size for wiping crypto footer.\n");
1726 } else {
1727 off64_t offset = ((off64_t)block_count * 512) - newlen;
1728 if (lseek64(fd, offset, SEEK_SET) == -1) {
1729 gui_print_color("warning", "Unable to lseek64 for wiping crypto footer.\n");
1730 } else {
1731 void* buffer = malloc(newlen);
1732 if (!buffer) {
1733 gui_print_color("warning", "Failed to malloc for wiping crypto footer.\n");
1734 } else {
1735 memset(buffer, 0, newlen);
1736 int ret = write(fd, buffer, newlen);
1737 if (ret != newlen) {
1738 gui_print_color("warning", "Failed to wipe crypto footer.\n");
1739 } else {
1740 LOGINFO("Successfully wiped crypto footer.\n");
1741 }
Matt Mower13a8f0b2015-09-26 15:40:03 -05001742 free(buffer);
Ethan Yonkerbc85b632015-08-09 12:48:14 -05001743 }
1744 }
1745 }
1746 close(fd);
1747 }
1748 } else {
Ethan Yonkerc2dafbb2016-03-15 22:20:59 -05001749 if (TWFunc::IOCTL_Get_Block_Size(Crypto_Key_Location.c_str()) >= 16384LLU) {
1750 string Command = "dd of='" + Crypto_Key_Location + "' if=/dev/zero bs=16384 count=1";
1751 TWFunc::Exec_Cmd(Command);
1752 } else {
1753 LOGINFO("Crypto key location reports size < 16K so not wiping crypto footer.\n");
1754 }
Ethan Yonkerbc85b632015-08-09 12:48:14 -05001755 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001756 if (Wipe(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001757 Has_Data_Media = Save_Data_Media;
1758 if (Has_Data_Media && !Symlink_Mount_Point.empty()) {
1759 Recreate_Media_Folder();
Ethan Yonker726a0202014-12-16 20:01:38 -06001760 if (Mount(false))
1761 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04001762 }
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001763 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
Ethan Yonker83e82572014-04-04 10:59:28 -05001764#ifndef TW_OEM_BUILD
Ethan Yonker74db1572015-10-28 12:44:49 -05001765 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 -05001766#endif
Dees_Troy38bd7602012-09-14 13:33:53 -04001767 return true;
1768 } else {
1769 Has_Data_Media = Save_Data_Media;
Ethan Yonker74db1572015-10-28 12:44:49 -05001770 gui_err("format_data_err=Unable to format to remove encryption.");
Ethan Yonker726a0202014-12-16 20:01:38 -06001771 if (Has_Data_Media && Mount(false))
1772 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04001773 return false;
1774 }
1775 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001776}
1777
1778void TWPartition::Check_FS_Type() {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001779 const char* type;
1780 blkid_probe pr;
Dees_Troy5bf43922012-09-07 16:07:55 -04001781
Dees_Troy68cab492012-12-12 19:29:35 +00001782 if (Fstab_File_System == "yaffs2" || Fstab_File_System == "mtd" || Fstab_File_System == "bml" || Ignore_Blkid)
1783 return; // Running blkid on some mtd devices causes a massive crash or needs to be skipped
Dees_Troy5bf43922012-09-07 16:07:55 -04001784
Dees_Troy38bd7602012-09-14 13:33:53 -04001785 Find_Actual_Block_Device();
Dees_Troy8170a922012-09-18 15:40:25 -04001786 if (!Is_Present)
1787 return;
Dees_Troy51127312012-09-08 13:08:49 -04001788
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001789 pr = blkid_new_probe_from_filename(Actual_Block_Device.c_str());
1790 if (blkid_do_fullprobe(pr)) {
1791 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00001792 LOGINFO("Can't probe device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001793 return;
Dees_Troy5bf43922012-09-07 16:07:55 -04001794 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001795
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05001796 if (blkid_probe_lookup_value(pr, "TYPE", &type, NULL) < 0) {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001797 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00001798 LOGINFO("can't find filesystem on device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001799 return;
1800 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001801
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001802 Current_File_System = type;
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001803 blkid_free_probe(pr);
Dees_Troy51a0e822012-09-05 15:24:24 -04001804}
1805
Gary Peck43acadf2012-11-21 21:19:01 -08001806bool TWPartition::Wipe_EXT23(string File_System) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001807 if (!UnMount(true))
1808 return false;
1809
Dees_Troy43d8b002012-09-17 16:00:01 -04001810 if (TWFunc::Path_Exists("/sbin/mke2fs")) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001811 string command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001812
Greg Wallace4b44fef2015-12-29 15:33:24 -05001813 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mke2fs"));
Dees_Troy38bd7602012-09-14 13:33:53 -04001814 Find_Actual_Block_Device();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001815 command = "mke2fs -t " + File_System + " -m 0 " + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001816 LOGINFO("mke2fs command: %s\n", command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001817 if (TWFunc::Exec_Cmd(command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001818 Current_File_System = File_System;
Dees_Troye58d5262012-09-21 12:27:57 -04001819 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05001820 gui_msg("done=Done.");
Dees_Troy38bd7602012-09-14 13:33:53 -04001821 return true;
1822 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001823 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04001824 return false;
1825 }
1826 } else
1827 return Wipe_RMRF();
1828
1829 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001830}
1831
1832bool TWPartition::Wipe_EXT4() {
Ethan Yonker25f20c12014-10-14 09:04:43 -05001833 Find_Actual_Block_Device();
1834 if (!Is_Present) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001835 LOGINFO("Block device not present, cannot wipe %s.\n", Display_Name.c_str());
1836 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Ethan Yonker25f20c12014-10-14 09:04:43 -05001837 return false;
1838 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001839 if (!UnMount(true))
1840 return false;
1841
Dees_Troyb3265ab2013-08-30 02:59:14 +00001842#if defined(HAVE_SELINUX) && defined(USE_EXT4)
Ethan Yonkerf27497f2014-02-09 11:48:33 -06001843 int ret;
1844 char *secontext = NULL;
1845
Greg Wallace4b44fef2015-12-29 15:33:24 -05001846 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("make_ext4fs"));
Ethan Yonkerf27497f2014-02-09 11:48:33 -06001847
Dees Troy99c8dbf2014-03-10 16:53:58 +00001848 if (!selinux_handle || selabel_lookup(selinux_handle, &secontext, Mount_Point.c_str(), S_IFDIR) < 0) {
Ethan Yonkerf27497f2014-02-09 11:48:33 -06001849 LOGINFO("Cannot lookup security context for '%s'\n", Mount_Point.c_str());
1850 ret = make_ext4fs(Actual_Block_Device.c_str(), Length, Mount_Point.c_str(), NULL);
1851 } else {
1852 ret = make_ext4fs(Actual_Block_Device.c_str(), Length, Mount_Point.c_str(), selinux_handle);
1853 }
1854 if (ret != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001855 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troya95f55c2013-08-17 13:14:43 +00001856 return false;
1857 } else {
bigbiff bigbiffc49d7062013-10-11 20:28:00 -04001858 string sedir = Mount_Point + "/lost+found";
1859 PartitionManager.Mount_By_Path(sedir.c_str(), true);
1860 rmdir(sedir.c_str());
1861 mkdir(sedir.c_str(), S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP);
Dees_Troya95f55c2013-08-17 13:14:43 +00001862 return true;
1863 }
1864#else
Dees_Troy43d8b002012-09-17 16:00:01 -04001865 if (TWFunc::Path_Exists("/sbin/make_ext4fs")) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001866 string Command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001867
Greg Wallace4b44fef2015-12-29 15:33:24 -05001868 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("make_ext4fs"));
Dees_Troy38bd7602012-09-14 13:33:53 -04001869 Find_Actual_Block_Device();
1870 Command = "make_ext4fs";
1871 if (!Is_Decrypted && Length != 0) {
1872 // Only use length if we're not decrypted
1873 char len[32];
1874 sprintf(len, "%i", Length);
1875 Command += " -l ";
1876 Command += len;
1877 }
Dees_Troy5295d582013-09-06 15:51:08 +00001878 if (TWFunc::Path_Exists("/file_contexts")) {
1879 Command += " -S /file_contexts";
1880 }
1881 Command += " -a " + Mount_Point + " " + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001882 LOGINFO("make_ext4fs command: %s\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001883 if (TWFunc::Exec_Cmd(Command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001884 Current_File_System = "ext4";
Dees_Troye58d5262012-09-21 12:27:57 -04001885 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05001886 gui_msg("done=Done.");
Dees_Troy38bd7602012-09-14 13:33:53 -04001887 return true;
1888 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001889 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04001890 return false;
1891 }
1892 } else
Gary Peck43acadf2012-11-21 21:19:01 -08001893 return Wipe_EXT23("ext4");
Dees_Troya95f55c2013-08-17 13:14:43 +00001894#endif
Dees_Troy38bd7602012-09-14 13:33:53 -04001895 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001896}
1897
1898bool TWPartition::Wipe_FAT() {
Vojtech Bocek05534202013-09-11 08:11:56 +02001899 string command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001900
Matt Mower18794c82015-11-11 16:22:45 -06001901 if (TWFunc::Path_Exists("/sbin/mkfs.fat")) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001902 if (!UnMount(true))
1903 return false;
1904
Greg Wallace4b44fef2015-12-29 15:33:24 -05001905 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mkfs.fat"));
Dees_Troy38bd7602012-09-14 13:33:53 -04001906 Find_Actual_Block_Device();
Matt Mower18794c82015-11-11 16:22:45 -06001907 command = "mkfs.fat " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02001908 if (TWFunc::Exec_Cmd(command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001909 Current_File_System = "vfat";
Dees_Troye58d5262012-09-21 12:27:57 -04001910 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05001911 gui_msg("done=Done.");
Dees_Troy38bd7602012-09-14 13:33:53 -04001912 return true;
1913 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001914 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04001915 return false;
1916 }
1917 return true;
1918 }
1919 else
1920 return Wipe_RMRF();
1921
1922 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001923}
1924
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001925bool TWPartition::Wipe_EXFAT() {
Vojtech Bocek05534202013-09-11 08:11:56 +02001926 string command;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001927
1928 if (TWFunc::Path_Exists("/sbin/mkexfatfs")) {
1929 if (!UnMount(true))
1930 return false;
1931
Greg Wallace4b44fef2015-12-29 15:33:24 -05001932 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mkexfatfs"));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001933 Find_Actual_Block_Device();
1934 command = "mkexfatfs " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02001935 if (TWFunc::Exec_Cmd(command) == 0) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001936 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05001937 gui_msg("done=Done.");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001938 return true;
1939 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001940 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001941 return false;
1942 }
1943 return true;
1944 }
1945 return false;
1946}
1947
Dees_Troy38bd7602012-09-14 13:33:53 -04001948bool TWPartition::Wipe_MTD() {
1949 if (!UnMount(true))
1950 return false;
1951
Greg Wallace4b44fef2015-12-29 15:33:24 -05001952 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("MTD"));
Dees_Troy38bd7602012-09-14 13:33:53 -04001953
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001954 mtd_scan_partitions();
1955 const MtdPartition* mtd = mtd_find_partition_by_name(MTD_Name.c_str());
1956 if (mtd == NULL) {
1957 LOGERR("No mtd partition named '%s'", MTD_Name.c_str());
1958 return false;
1959 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001960
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001961 MtdWriteContext* ctx = mtd_write_partition(mtd);
1962 if (ctx == NULL) {
1963 LOGERR("Can't write '%s', failed to format.", MTD_Name.c_str());
1964 return false;
1965 }
1966 if (mtd_erase_blocks(ctx, -1) == -1) {
1967 mtd_write_close(ctx);
1968 LOGERR("Failed to format '%s'", MTD_Name.c_str());
1969 return false;
1970 }
1971 if (mtd_write_close(ctx) != 0) {
1972 LOGERR("Failed to close '%s'", MTD_Name.c_str());
1973 return false;
1974 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001975 Current_File_System = "yaffs2";
Dees_Troye58d5262012-09-21 12:27:57 -04001976 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05001977 gui_msg("done=Done.");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001978 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001979}
1980
1981bool TWPartition::Wipe_RMRF() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001982 if (!Mount(true))
1983 return false;
Ethan Yonker726a0202014-12-16 20:01:38 -06001984 // This is the only wipe that leaves the partition mounted, so we
1985 // must manually remove the partition from MTP if it is a storage
1986 // partition.
1987 if (Is_Storage)
1988 PartitionManager.Remove_MTP_Storage(MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04001989
Ethan Yonker74db1572015-10-28 12:44:49 -05001990 gui_msg(Msg("remove_all=Removing all files under '{1}'")(Mount_Point));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001991 TWFunc::removeDir(Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04001992 Recreate_AndSec_Folder();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001993 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001994}
1995
Dees_Troye5017042013-08-29 16:38:55 +00001996bool TWPartition::Wipe_F2FS() {
Vojtech Bocek05534202013-09-11 08:11:56 +02001997 string command;
Dees_Troye5017042013-08-29 16:38:55 +00001998
1999 if (TWFunc::Path_Exists("/sbin/mkfs.f2fs")) {
2000 if (!UnMount(true))
2001 return false;
2002
Greg Wallace4b44fef2015-12-29 15:33:24 -05002003 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mkfs.f2fs"));
Dees_Troye5017042013-08-29 16:38:55 +00002004 Find_Actual_Block_Device();
Greg Wallace558aef72016-01-19 21:14:30 -05002005 command = "mkfs.f2fs -t 0";
dhacker29a3fa75f2015-01-17 19:42:33 -05002006 if (!Is_Decrypted && Length != 0) {
2007 // Only use length if we're not decrypted
2008 char len[32];
2009 int mod_length = Length;
2010 if (Length < 0)
2011 mod_length *= -1;
2012 sprintf(len, "%i", mod_length);
2013 command += " -r ";
2014 command += len;
2015 }
2016 command += " " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02002017 if (TWFunc::Exec_Cmd(command) == 0) {
Dees_Troye5017042013-08-29 16:38:55 +00002018 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05002019 gui_msg("done=Done.");
Dees_Troye5017042013-08-29 16:38:55 +00002020 return true;
2021 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002022 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troye5017042013-08-29 16:38:55 +00002023 return false;
2024 }
2025 return true;
2026 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002027 LOGINFO("mkfs.f2fs binary not found, using rm -rf to wipe.\n");
Dees_Troye5017042013-08-29 16:38:55 +00002028 return Wipe_RMRF();
2029 }
2030 return false;
2031}
2032
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002033bool TWPartition::Wipe_NTFS() {
2034 string command;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002035 string Ntfsmake_Binary;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002036
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002037 if (TWFunc::Path_Exists("/sbin/mkntfs"))
2038 Ntfsmake_Binary = "mkntfs";
2039 else if (TWFunc::Path_Exists("/sbin/mkfs.ntfs"))
2040 Ntfsmake_Binary = "mkfs.ntfs";
2041 else
2042 return false;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002043
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002044 if (!UnMount(true))
2045 return false;
2046
Greg Wallace4b44fef2015-12-29 15:33:24 -05002047 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)(Ntfsmake_Binary));
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002048 Find_Actual_Block_Device();
2049 command = "/sbin/" + Ntfsmake_Binary + " " + Actual_Block_Device;
2050 if (TWFunc::Exec_Cmd(command) == 0) {
2051 Recreate_AndSec_Folder();
2052 gui_msg("done=Done.");
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002053 return true;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002054 } else {
2055 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
2056 return false;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002057 }
2058 return false;
2059}
2060
Dees_Troy51a0e822012-09-05 15:24:24 -04002061bool TWPartition::Wipe_Data_Without_Wiping_Media() {
Ethan Yonker83e82572014-04-04 10:59:28 -05002062#ifdef TW_OEM_BUILD
2063 // In an OEM Build we want to do a full format
2064 return Wipe_Encryption();
2065#else
Ethan Yonker66a19492015-12-10 10:19:45 -06002066 bool ret = false;
Dees_Troy38bd7602012-09-14 13:33:53 -04002067
Dees_Troy38bd7602012-09-14 13:33:53 -04002068 if (!Mount(true))
2069 return false;
2070
Ethan Yonker74db1572015-10-28 12:44:49 -05002071 gui_msg("wiping_data=Wiping data without wiping /data/media ...");
Ethan Yonker66a19492015-12-10 10:19:45 -06002072 ret = Wipe_Data_Without_Wiping_Media_Func(Mount_Point + "/");
2073 if (ret)
2074 gui_msg("done=Done.");
2075 return ret;
2076#endif // ifdef TW_OEM_BUILD
2077}
2078
2079bool TWPartition::Wipe_Data_Without_Wiping_Media_Func(const string& parent __unused) {
2080 string dir;
Dees_Troy38bd7602012-09-14 13:33:53 -04002081
2082 DIR* d;
Ethan Yonker66a19492015-12-10 10:19:45 -06002083 d = opendir(parent.c_str());
Dees_Troy16b74352012-11-14 22:27:31 +00002084 if (d != NULL) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002085 struct dirent* de;
2086 while ((de = readdir(d)) != NULL) {
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002087 if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) continue;
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05002088
Ethan Yonker66a19492015-12-10 10:19:45 -06002089 dir = parent;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002090 dir.append(de->d_name);
Ethan Yonker66a19492015-12-10 10:19:45 -06002091 if (du.check_skip_dirs(dir)) {
2092 LOGINFO("skipped '%s'\n", dir.c_str());
2093 continue;
2094 }
Dees_Troyce675462013-01-09 19:48:21 +00002095 if (de->d_type == DT_DIR) {
Ethan Yonker66a19492015-12-10 10:19:45 -06002096 dir.append("/");
2097 if (!Wipe_Data_Without_Wiping_Media_Func(dir)) {
2098 closedir(d);
2099 return false;
2100 }
2101 rmdir(dir.c_str());
bigbiff bigbiff98f1f902013-01-19 18:46:13 -05002102 } 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 +00002103 if (!unlink(dir.c_str()))
Dees_Troy2673cec2013-04-02 20:22:16 +00002104 LOGINFO("Unable to unlink '%s'\n", dir.c_str());
Dees_Troyce675462013-01-09 19:48:21 +00002105 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002106 }
2107 closedir(d);
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05002108
Dees_Troy16b74352012-11-14 22:27:31 +00002109 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -04002110 }
Ethan Yonker74db1572015-10-28 12:44:49 -05002111 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy16b74352012-11-14 22:27:31 +00002112 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04002113}
2114
Ethan Yonker472f5062016-02-25 13:47:30 -06002115bool TWPartition::Backup_Tar(const string& backup_folder, ProgressTracking *progress, pid_t &tar_fork_pid) {
2116 string Full_FileName;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002117 twrpTar tar;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05002118
Dees_Troy43d8b002012-09-17 16:00:01 -04002119 if (!Mount(true))
2120 return false;
2121
Dees_Troya13d74f2013-03-24 08:54:55 -05002122 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Backup_Display_Name, "Backing Up");
Ethan Yonker74db1572015-10-28 12:44:49 -05002123 gui_msg(Msg("backing_up=Backing up {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002124
Ethan Yonker472f5062016-02-25 13:47:30 -06002125 DataManager::GetValue(TW_USE_COMPRESSION_VAR, tar.use_compression);
Matt Mowerbb81e5d2014-03-20 18:05:41 -05002126
Dees_Troy83bd4832013-05-04 12:39:56 +00002127#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
Ethan Yonker472f5062016-02-25 13:47:30 -06002128 if (Can_Encrypt_Backup) {
2129 DataManager::GetValue("tw_encrypt_backup", tar.use_encryption);
2130 if (tar.use_encryption) {
2131 if (Use_Userdata_Encryption)
2132 tar.userdata_encryption = tar.use_encryption;
2133 string Password;
2134 DataManager::GetValue("tw_backup_password", Password);
2135 tar.setpassword(Password);
2136 } else {
2137 tar.use_encryption = 0;
2138 }
Dees_Troy83bd4832013-05-04 12:39:56 +00002139 }
2140#endif
Dees_Troy43d8b002012-09-17 16:00:01 -04002141
Ethan Yonker472f5062016-02-25 13:47:30 -06002142 Backup_FileName = Backup_Name + "." + Current_File_System + ".win";
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002143 Full_FileName = backup_folder + "/" + Backup_FileName;
Dees_Troy83bd4832013-05-04 12:39:56 +00002144 tar.has_data_media = Has_Data_Media;
Dees Troye0a433a2013-12-02 04:10:37 +00002145 Full_FileName = backup_folder + "/" + Backup_FileName;
2146 tar.setdir(Backup_Path);
2147 tar.setfn(Full_FileName);
2148 tar.setsize(Backup_Size);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002149 tar.partition_name = Backup_Name;
2150 tar.backup_folder = backup_folder;
Ethan Yonker472f5062016-02-25 13:47:30 -06002151 if (tar.createTarFork(progress, tar_fork_pid) != 0)
Dees Troye0a433a2013-12-02 04:10:37 +00002152 return false;
Dees_Troy43d8b002012-09-17 16:00:01 -04002153 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002154}
2155
Ethan Yonker472f5062016-02-25 13:47:30 -06002156bool TWPartition::Backup_Image(const string& backup_folder, ProgressTracking *progress) {
2157 string Full_FileName;
igoriok87e3d932013-01-31 21:03:53 +02002158
Ethan Yonker74db1572015-10-28 12:44:49 -05002159 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, gui_parse_text("{@backing}"));
2160 gui_msg(Msg("backing_up=Backing up {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002161
Ethan Yonker472f5062016-02-25 13:47:30 -06002162 Backup_FileName = Backup_Name + "." + Current_File_System + ".win";
Dees_Troy43d8b002012-09-17 16:00:01 -04002163 Full_FileName = backup_folder + "/" + Backup_FileName;
2164
Ethan Yonker472f5062016-02-25 13:47:30 -06002165 if (!Raw_Read_Write(Actual_Block_Device, Full_FileName, Backup_Size, progress))
2166 return false;
2167
Ethan Yonker4b94cfd2014-12-11 10:00:45 -06002168 tw_set_default_metadata(Full_FileName.c_str());
Dees_Troyc154ac22012-10-12 15:36:47 -04002169 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002170 gui_msg(Msg(msg::kError, "backup_size=Backup file size for '{1}' is 0 bytes.")(Full_FileName));
Dees_Troy7c2dec82012-09-26 09:49:14 -04002171 return false;
2172 }
Dees_Troy43d8b002012-09-17 16:00:01 -04002173 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002174}
2175
Ethan Yonker472f5062016-02-25 13:47:30 -06002176bool TWPartition::Raw_Read_Write(const string& input_file, const string& output_file, const unsigned long long input_size, ProgressTracking *progress) {
2177 unsigned long long RW_Block_Size, Remain;
2178 int src_fd = -1, dest_fd = -1, bs;
2179 bool ret = false;
2180 void* buffer = NULL;
2181 unsigned long long backedup_size = 0;
2182
2183 RW_Block_Size = 1048576LLU; // 1MB
2184 Remain = input_size;
2185
2186 src_fd = open(input_file.c_str(), O_RDONLY | O_LARGEFILE);
2187 if (src_fd < 0) {
2188 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(input_file)(strerror(errno)));
2189 return false;
2190 }
2191 dest_fd = open(output_file.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_LARGEFILE, S_IRUSR | S_IWUSR);
2192 if (dest_fd < 0) {
2193 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(output_file)(strerror(errno)));
2194 goto exit;
2195 }
2196 bs = (int)(RW_Block_Size);
2197 buffer = malloc((size_t)bs);
2198 if (!buffer) {
2199 LOGINFO("Raw_Read_Write failed to malloc\n");
2200 goto exit;
2201 }
2202 LOGINFO("Reading '%s', writing '%s'\n", input_file.c_str(), output_file.c_str());
2203 if (progress)
2204 progress->SetPartitionSize(input_size);
2205 while (Remain > 0) {
2206 if (Remain < RW_Block_Size)
2207 bs = (int)(Remain);
2208 if (read(src_fd, buffer, bs) != bs) {
2209 LOGINFO("Error reading source fd (%s)\n", strerror(errno));
2210 goto exit;
2211 }
2212 if (write(dest_fd, buffer, bs) != bs) {
2213 LOGINFO("Error writing destination fd (%s)\n", strerror(errno));
2214 goto exit;
2215 }
2216 backedup_size += (unsigned long long)(bs);
2217 Remain -= (unsigned long long)(bs);
2218 if (progress)
2219 progress->UpdateSize(backedup_size);
2220 if (PartitionManager.Check_Backup_Cancel() != 0)
2221 goto exit;
2222 }
2223 if (progress)
2224 progress->UpdateDisplayDetails(true);
2225 fsync(dest_fd);
2226 ret = true;
2227exit:
2228 if (src_fd >= 0)
2229 close(src_fd);
2230 if (dest_fd >= 0)
2231 close(dest_fd);
2232 if (buffer)
2233 free(buffer);
2234 return ret;
2235}
2236
2237bool TWPartition::Backup_Dump_Image(const string& backup_folder, ProgressTracking *progress) {
Vojtech Bocek05534202013-09-11 08:11:56 +02002238 string Full_FileName, Command;
Dees_Troy43d8b002012-09-17 16:00:01 -04002239
Ethan Yonker74db1572015-10-28 12:44:49 -05002240 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, gui_parse_text("{@backing}"));
2241 gui_msg(Msg("backing_up=Backing up {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002242
Ethan Yonker472f5062016-02-25 13:47:30 -06002243 if (progress)
2244 progress->SetPartitionSize(Backup_Size);
Dees_Troy43d8b002012-09-17 16:00:01 -04002245
Ethan Yonker472f5062016-02-25 13:47:30 -06002246 Backup_FileName = Backup_Name + "." + Current_File_System + ".win";
Dees_Troy43d8b002012-09-17 16:00:01 -04002247 Full_FileName = backup_folder + "/" + Backup_FileName;
2248
2249 Command = "dump_image " + MTD_Name + " '" + Full_FileName + "'";
Dees_Troy2673cec2013-04-02 20:22:16 +00002250 LOGINFO("Backup command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02002251 TWFunc::Exec_Cmd(Command);
Ethan Yonker4b94cfd2014-12-11 10:00:45 -06002252 tw_set_default_metadata(Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04002253 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
2254 // 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 -06002255 gui_msg(Msg(msg::kError, "backup_size=Backup file size for '{1}' is 0 bytes.")(Full_FileName));
Dees_Troy7c2dec82012-09-26 09:49:14 -04002256 return false;
2257 }
Ethan Yonker472f5062016-02-25 13:47:30 -06002258 if (progress)
2259 progress->UpdateSize(Backup_Size);
Dees_Troy43d8b002012-09-17 16:00:01 -04002260 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002261}
2262
Ethan Yonker472f5062016-02-25 13:47:30 -06002263unsigned long long TWPartition::Get_Restore_Size(const string& restore_folder) {
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002264 InfoManager restore_info(restore_folder + "/" + Backup_Name + ".info");
2265 if (restore_info.LoadValues() == 0) {
2266 if (restore_info.GetValue("backup_size", Restore_Size) == 0) {
2267 LOGINFO("Read info file, restore size is %llu\n", Restore_Size);
2268 return Restore_Size;
2269 }
2270 }
2271 string Full_FileName, Restore_File_System = Get_Restore_File_System(restore_folder);
2272
2273 Full_FileName = restore_folder + "/" + Backup_FileName;
2274
2275 if (Is_Image(Restore_File_System)) {
2276 Restore_Size = TWFunc::Get_File_Size(Full_FileName);
2277 return Restore_Size;
2278 }
2279
2280 twrpTar tar;
2281 tar.setdir(Backup_Path);
2282 tar.setfn(Full_FileName);
2283 tar.backup_name = Backup_Name;
2284#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
2285 string Password;
2286 DataManager::GetValue("tw_restore_password", Password);
2287 if (!Password.empty())
2288 tar.setpassword(Password);
2289#endif
2290 tar.partition_name = Backup_Name;
2291 tar.backup_folder = restore_folder;
2292 Restore_Size = tar.get_size();
2293 return Restore_Size;
2294}
2295
Ethan Yonker472f5062016-02-25 13:47:30 -06002296bool TWPartition::Restore_Tar(const string& restore_folder, const string& Restore_File_System, ProgressTracking *progress) {
2297 string Full_FileName;
Dees Troy4159aed2014-02-28 17:24:43 +00002298 bool ret = false;
Dees_Troy43d8b002012-09-17 16:00:01 -04002299
Dees_Troye58d5262012-09-21 12:27:57 -04002300 if (Has_Android_Secure) {
Dees_Troye58d5262012-09-21 12:27:57 -04002301 if (!Wipe_AndSec())
2302 return false;
Gary Peck43acadf2012-11-21 21:19:01 -08002303 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002304 gui_msg(Msg("wiping=Wiping {1}")(Backup_Display_Name));
Ethan Yonker5eac2222014-06-11 12:22:55 -05002305 if (Has_Data_Media && Mount_Point == "/data" && Restore_File_System != Current_File_System) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002306 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 -05002307 if (!Wipe_Data_Without_Wiping_Media())
2308 return false;
2309 } else {
2310 if (!Wipe(Restore_File_System))
2311 return false;
2312 }
Dees_Troye58d5262012-09-21 12:27:57 -04002313 }
Matt Mower3c366972015-12-25 19:28:31 -06002314 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Backup_Display_Name, gui_parse_text("{@restoring_hdr}"));
Ethan Yonker74db1572015-10-28 12:44:49 -05002315 gui_msg(Msg("restoring=Restoring {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002316
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05002317 // Remount as read/write as needed so we can restore the backup
2318 if (!ReMount_RW(true))
Dees_Troy43d8b002012-09-17 16:00:01 -04002319 return false;
2320
Dees_Troy4a2a1262012-09-18 09:33:47 -04002321 Full_FileName = restore_folder + "/" + Backup_FileName;
Ethan Yonker87af5632014-02-10 11:56:35 -06002322 twrpTar tar;
2323 tar.setdir(Backup_Path);
2324 tar.setfn(Full_FileName);
2325 tar.backup_name = Backup_Name;
2326#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
2327 string Password;
2328 DataManager::GetValue("tw_restore_password", Password);
2329 if (!Password.empty())
2330 tar.setpassword(Password);
2331#endif
Ethan Yonker472f5062016-02-25 13:47:30 -06002332 progress->SetPartitionSize(Get_Restore_Size(restore_folder));
2333 if (tar.extractTarFork(progress) != 0)
Dees Troy4159aed2014-02-28 17:24:43 +00002334 ret = false;
2335 else
2336 ret = true;
2337#ifdef HAVE_CAPABILITIES
2338 // Restore capabilities to the run-as binary
2339 if (Mount_Point == "/system" && Mount(true) && TWFunc::Path_Exists("/system/bin/run-as")) {
2340 struct vfs_cap_data cap_data;
2341 uint64_t capabilities = (1 << CAP_SETUID) | (1 << CAP_SETGID);
2342
2343 memset(&cap_data, 0, sizeof(cap_data));
2344 cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE;
2345 cap_data.data[0].permitted = (uint32_t) (capabilities & 0xffffffff);
2346 cap_data.data[0].inheritable = 0;
2347 cap_data.data[1].permitted = (uint32_t) (capabilities >> 32);
2348 cap_data.data[1].inheritable = 0;
2349 if (setxattr("/system/bin/run-as", XATTR_NAME_CAPS, &cap_data, sizeof(cap_data), 0) < 0) {
2350 LOGINFO("Failed to reset capabilities of /system/bin/run-as binary.\n");
2351 } else {
2352 LOGINFO("Reset capabilities of /system/bin/run-as binary successful.\n");
2353 }
2354 }
2355#endif
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05002356 if (Mount_Read_Only || Mount_Flags & MS_RDONLY)
2357 // Remount as read only when restoration is complete
2358 ReMount(true);
2359
Dees Troy4159aed2014-02-28 17:24:43 +00002360 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04002361}
2362
Ethan Yonker472f5062016-02-25 13:47:30 -06002363bool TWPartition::Restore_Image(const string& restore_folder, const string& Restore_File_System, ProgressTracking *progress) {
Ethan Yonker96af84a2015-01-05 14:58:36 -06002364 string Full_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04002365
Matt Mower3c366972015-12-25 19:28:31 -06002366 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Backup_Display_Name, gui_parse_text("{@restoring_hdr}"));
Ethan Yonker74db1572015-10-28 12:44:49 -05002367 gui_msg(Msg("restoring=Restoring {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002368 Full_FileName = restore_folder + "/" + Backup_FileName;
Gary Peck15e623d2012-11-21 21:07:58 -08002369
Ethan Yonker96af84a2015-01-05 14:58:36 -06002370 if (Restore_File_System == "emmc") {
Ethan Yonker472f5062016-02-25 13:47:30 -06002371 unsigned long long file_size = (unsigned long long)(TWFunc::Get_File_Size(Full_FileName));
2372 if (!Raw_Read_Write(Full_FileName, Actual_Block_Device, file_size, progress))
Ethan Yonker96af84a2015-01-05 14:58:36 -06002373 return false;
2374 } else if (Restore_File_System == "mtd" || Restore_File_System == "bml") {
Ethan Yonker472f5062016-02-25 13:47:30 -06002375 if (!Flash_Image_FI(Full_FileName, progress))
Ethan Yonker96af84a2015-01-05 14:58:36 -06002376 return false;
Gary Peck15e623d2012-11-21 21:07:58 -08002377 }
Dees_Troy43d8b002012-09-17 16:00:01 -04002378 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002379}
Dees_Troy5bf43922012-09-07 16:07:55 -04002380
2381bool TWPartition::Update_Size(bool Display_Error) {
Dees_Troy0550cfb2012-10-13 11:56:13 -04002382 bool ret = false, Was_Already_Mounted = false;
Dees_Troy51127312012-09-08 13:08:49 -04002383
Dees_Troyab10ee22012-09-21 14:27:30 -04002384 if (!Can_Be_Mounted && !Is_Encrypted)
Dees_Troy5bf43922012-09-07 16:07:55 -04002385 return false;
2386
Dees_Troy0550cfb2012-10-13 11:56:13 -04002387 Was_Already_Mounted = Is_Mounted();
Dees_Troy38bd7602012-09-14 13:33:53 -04002388 if (Removable || Is_Encrypted) {
2389 if (!Mount(false))
2390 return true;
2391 } else if (!Mount(Display_Error))
Dees_Troy5bf43922012-09-07 16:07:55 -04002392 return false;
Dees_Troy51127312012-09-08 13:08:49 -04002393
2394 ret = Get_Size_Via_statfs(Display_Error);
Dees_Troy0550cfb2012-10-13 11:56:13 -04002395 if (!ret || Size == 0) {
2396 if (!Get_Size_Via_df(Display_Error)) {
2397 if (!Was_Already_Mounted)
2398 UnMount(false);
Dees_Troy51127312012-09-08 13:08:49 -04002399 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04002400 }
2401 }
Dees_Troy51127312012-09-08 13:08:49 -04002402
Dees_Troy5bf43922012-09-07 16:07:55 -04002403 if (Has_Data_Media) {
2404 if (Mount(Display_Error)) {
Dees_Troy51127312012-09-08 13:08:49 -04002405 unsigned long long data_media_used, actual_data;
Ethan Yonker66a19492015-12-10 10:19:45 -06002406 Used = du.Get_Folder_Size(Mount_Point);
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002407 Backup_Size = Used;
2408 int bak = (int)(Used / 1048576LLU);
Dees_Troy51127312012-09-08 13:08:49 -04002409 int fre = (int)(Free / 1048576LLU);
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002410 LOGINFO("Data backup size is %iMB, free: %iMB.\n", bak, fre);
Dees_Troy0550cfb2012-10-13 11:56:13 -04002411 } else {
2412 if (!Was_Already_Mounted)
2413 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04002414 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04002415 }
Dees_Troye58d5262012-09-21 12:27:57 -04002416 } else if (Has_Android_Secure) {
2417 if (Mount(Display_Error))
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002418 Backup_Size = du.Get_Folder_Size(Backup_Path);
Dees_Troy0550cfb2012-10-13 11:56:13 -04002419 else {
2420 if (!Was_Already_Mounted)
2421 UnMount(false);
Dees_Troye58d5262012-09-21 12:27:57 -04002422 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04002423 }
Dees_Troy5bf43922012-09-07 16:07:55 -04002424 }
Dees_Troy0550cfb2012-10-13 11:56:13 -04002425 if (!Was_Already_Mounted)
2426 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04002427 return true;
Dees_Troy51127312012-09-08 13:08:49 -04002428}
Dees_Troy38bd7602012-09-14 13:33:53 -04002429
2430void TWPartition::Find_Actual_Block_Device(void) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002431 if (Is_Decrypted && !Decrypted_Block_Device.empty()) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002432 Actual_Block_Device = Decrypted_Block_Device;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002433 if (TWFunc::Path_Exists(Decrypted_Block_Device))
Dees_Troy38bd7602012-09-14 13:33:53 -04002434 Is_Present = true;
Dees_Troy43d8b002012-09-17 16:00:01 -04002435 } else if (TWFunc::Path_Exists(Primary_Block_Device)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002436 Is_Present = true;
2437 Actual_Block_Device = Primary_Block_Device;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002438 return;
2439 }
2440 if (Is_Decrypted) {
Dees_Troy43d8b002012-09-17 16:00:01 -04002441 } else if (!Alternate_Block_Device.empty() && TWFunc::Path_Exists(Alternate_Block_Device)) {
Dees_Troy3f04d032012-10-07 18:20:09 -04002442 Actual_Block_Device = Alternate_Block_Device;
Dees_Troy38bd7602012-09-14 13:33:53 -04002443 Is_Present = true;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002444 } else {
Dees_Troy38bd7602012-09-14 13:33:53 -04002445 Is_Present = false;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002446 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002447}
2448
2449void TWPartition::Recreate_Media_Folder(void) {
2450 string Command;
Ethan Yonker66a19492015-12-10 10:19:45 -06002451 string Media_Path = Mount_Point + "/media";
Dees_Troy38bd7602012-09-14 13:33:53 -04002452
2453 if (!Mount(true)) {
Ethan Yonker66a19492015-12-10 10:19:45 -06002454 gui_msg(Msg(msg::kError, "recreate_folder_err=Unable to recreate {1} folder.")(Media_Path));
2455 } else if (!TWFunc::Path_Exists(Media_Path)) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002456 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Ethan Yonker66a19492015-12-10 10:19:45 -06002457 LOGINFO("Recreating %s folder.\n", Media_Path.c_str());
2458 mkdir(Media_Path.c_str(), 0770);
xiaolu9416f4f2015-06-04 08:22:23 +08002459 string Internal_path = DataManager::GetStrValue("tw_internal_path");
2460 if (!Internal_path.empty()) {
2461 LOGINFO("Recreating %s folder.\n", Internal_path.c_str());
2462 mkdir(Internal_path.c_str(), 0770);
2463 }
2464#ifdef TW_INTERNAL_STORAGE_PATH
2465 mkdir(EXPAND(TW_INTERNAL_STORAGE_PATH), 0770);
2466#endif
Ethan Yonker5ef301e2014-10-14 10:04:44 -05002467#ifdef HAVE_SELINUX
thata3d31fb2014-12-21 22:27:40 +01002468 // Afterwards, we will try to set the
2469 // default metadata that we were hopefully able to get during
2470 // early boot.
Ethan Yonker66a19492015-12-10 10:19:45 -06002471 tw_set_default_metadata(Media_Path.c_str());
xiaolu9416f4f2015-06-04 08:22:23 +08002472 if (!Internal_path.empty())
2473 tw_set_default_metadata(Internal_path.c_str());
Ethan Yonker5ef301e2014-10-14 10:04:44 -05002474#endif
Ethan Yonker5eac2222014-06-11 12:22:55 -05002475 // Toggle mount to ensure that "internal sdcard" gets mounted
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002476 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Ethan Yonker5eac2222014-06-11 12:22:55 -05002477 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Dees_Troy38bd7602012-09-14 13:33:53 -04002478 }
Dees_Troy43d8b002012-09-17 16:00:01 -04002479}
Dees_Troye58d5262012-09-21 12:27:57 -04002480
2481void TWPartition::Recreate_AndSec_Folder(void) {
Dees_Troye58d5262012-09-21 12:27:57 -04002482 if (!Has_Android_Secure)
2483 return;
Dees_Troy2673cec2013-04-02 20:22:16 +00002484 LOGINFO("Creating %s: %s\n", Backup_Display_Name.c_str(), Symlink_Path.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -04002485 if (!Mount(true)) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002486 gui_msg(Msg(msg::kError, "recreate_folder_err=Unable to recreate {1} folder.")(Backup_Name));
Dees_Troye58d5262012-09-21 12:27:57 -04002487 } else if (!TWFunc::Path_Exists(Symlink_Path)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00002488 LOGINFO("Recreating %s folder.\n", Backup_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002489 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05002490 mkdir(Symlink_Path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002491 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04002492 }
2493}
bigbiff7cb4c332014-11-26 20:36:07 -05002494
2495uint64_t TWPartition::Get_Max_FileSize() {
2496 uint64_t maxFileSize = 0;
2497 const uint64_t constGB = (uint64_t) 1024 * 1024 * 1024;
2498 const uint64_t constTB = (uint64_t) constGB * 1024;
2499 const uint64_t constPB = (uint64_t) constTB * 1024;
2500 const uint64_t constEB = (uint64_t) constPB * 1024;
bigbiff0c532032014-12-21 13:41:26 -05002501 if (Current_File_System == "ext4")
2502 maxFileSize = 16 * constTB; //16 TB
2503 else if (Current_File_System == "vfat")
2504 maxFileSize = 4 * constGB; //4 GB
2505 else if (Current_File_System == "ntfs")
2506 maxFileSize = 256 * constTB; //256 TB
2507 else if (Current_File_System == "exfat")
2508 maxFileSize = 16 * constPB; //16 PB
2509 else if (Current_File_System == "ext3")
2510 maxFileSize = 2 * constTB; //2 TB
2511 else if (Current_File_System == "f2fs")
2512 maxFileSize = 3.94 * constTB; //3.94 TB
bigbiff7cb4c332014-11-26 20:36:07 -05002513 else
2514 maxFileSize = 100000000L;
2515 return maxFileSize - 1;
2516}
2517
Ethan Yonker472f5062016-02-25 13:47:30 -06002518bool TWPartition::Flash_Image(const string& Filename) {
Ethan Yonker96af84a2015-01-05 14:58:36 -06002519 string Restore_File_System;
2520
2521 LOGINFO("Image filename is: %s\n", Filename.c_str());
2522
2523 if (Backup_Method == FILES) {
2524 LOGERR("Cannot flash images to file systems\n");
2525 return false;
2526 } else if (!Can_Flash_Img) {
2527 LOGERR("Cannot flash images to partitions %s\n", Display_Name.c_str());
2528 return false;
2529 } else {
2530 if (!Find_Partition_Size()) {
2531 LOGERR("Unable to find partition size for '%s'\n", Mount_Point.c_str());
2532 return false;
2533 }
2534 unsigned long long image_size = TWFunc::Get_File_Size(Filename);
2535 if (image_size > Size) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002536 LOGINFO("Size (%llu bytes) of image '%s' is larger than target device '%s' (%llu bytes)\n",
Ethan Yonker96af84a2015-01-05 14:58:36 -06002537 image_size, Filename.c_str(), Actual_Block_Device.c_str(), Size);
Ethan Yonker74db1572015-10-28 12:44:49 -05002538 gui_err("img_size_err=Size of image is larger than target device");
Ethan Yonker96af84a2015-01-05 14:58:36 -06002539 return false;
2540 }
Ethan Yonker472f5062016-02-25 13:47:30 -06002541 if (Backup_Method == DD) {
2542 if (Is_Sparse_Image(Filename)) {
2543 return Flash_Sparse_Image(Filename);
2544 }
2545 unsigned long long file_size = (unsigned long long)(TWFunc::Get_File_Size(Filename));
2546 ProgressTracking pt(file_size);
2547 return Raw_Read_Write(Filename, Actual_Block_Device, file_size, &pt);
2548 } else if (Backup_Method == FLASH_UTILS) {
2549 return Flash_Image_FI(Filename, NULL);
2550 }
Ethan Yonker96af84a2015-01-05 14:58:36 -06002551 }
2552
2553 LOGERR("Unknown flash method for '%s'\n", Mount_Point.c_str());
2554 return false;
2555}
2556
Ethan Yonker472f5062016-02-25 13:47:30 -06002557bool TWPartition::Is_Sparse_Image(const string& Filename) {
HashBanged974bb2016-01-30 14:20:09 -05002558 uint32_t magic = 0;
2559 int fd = open(Filename.c_str(), O_RDONLY);
2560 if (fd < 0) {
2561 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Filename)(strerror(errno)));
2562 return false;
2563 }
2564 if (read(fd, &magic, sizeof(magic)) != sizeof(magic)) {
2565 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Filename)(strerror(errno)));
2566 close(fd);
2567 return false;
2568 }
2569 close(fd);
Ethan Yonker472f5062016-02-25 13:47:30 -06002570 if (magic == SPARSE_HEADER_MAGIC)
2571 return true;
2572 return false;
2573}
2574
2575bool TWPartition::Flash_Sparse_Image(const string& Filename) {
2576 string Command;
2577
2578 gui_msg(Msg("flashing=Flashing {1}...")(Display_Name));
2579
2580 Command = "simg2img '" + Filename + "' '" + Actual_Block_Device + "'";
Ethan Yonker96af84a2015-01-05 14:58:36 -06002581 LOGINFO("Flash command: '%s'\n", Command.c_str());
2582 TWFunc::Exec_Cmd(Command);
2583 return true;
2584}
2585
Ethan Yonker472f5062016-02-25 13:47:30 -06002586bool TWPartition::Flash_Image_FI(const string& Filename, ProgressTracking *progress) {
Ethan Yonker96af84a2015-01-05 14:58:36 -06002587 string Command;
Ethan Yonker472f5062016-02-25 13:47:30 -06002588 unsigned long long file_size;
Ethan Yonker96af84a2015-01-05 14:58:36 -06002589
Ethan Yonker74db1572015-10-28 12:44:49 -05002590 gui_msg(Msg("flashing=Flashing {1}...")(Display_Name));
Ethan Yonker472f5062016-02-25 13:47:30 -06002591 if (progress) {
2592 file_size = (unsigned long long)(TWFunc::Get_File_Size(Filename));
2593 progress->SetPartitionSize(file_size);
2594 }
Ethan Yonker96af84a2015-01-05 14:58:36 -06002595 // Sometimes flash image doesn't like to flash due to the first 2KB matching, so we erase first to ensure that it flashes
2596 Command = "erase_image " + MTD_Name;
2597 LOGINFO("Erase command: '%s'\n", Command.c_str());
2598 TWFunc::Exec_Cmd(Command);
2599 Command = "flash_image " + MTD_Name + " '" + Filename + "'";
2600 LOGINFO("Flash command: '%s'\n", Command.c_str());
2601 TWFunc::Exec_Cmd(Command);
Ethan Yonker472f5062016-02-25 13:47:30 -06002602 if (progress)
2603 progress->UpdateSize(file_size);
Ethan Yonker96af84a2015-01-05 14:58:36 -06002604 return true;
2605}
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05002606
2607void TWPartition::Change_Mount_Read_Only(bool new_value) {
2608 Mount_Read_Only = new_value;
2609}
2610
2611int TWPartition::Check_Lifetime_Writes() {
2612 bool original_read_only = Mount_Read_Only;
2613 int ret = 1;
2614
2615 Mount_Read_Only = true;
2616 if (Mount(false)) {
2617 Find_Actual_Block_Device();
2618 string block = basename(Actual_Block_Device.c_str());
2619 string file = "/sys/fs/" + Current_File_System + "/" + block + "/lifetime_write_kbytes";
2620 string result;
2621 if (TWFunc::Path_Exists(file)) {
2622 if (TWFunc::read_file(file, result) != 0) {
2623 LOGINFO("Check_Lifetime_Writes of '%s' failed to read_file\n", file.c_str());
2624 } else {
2625 LOGINFO("Check_Lifetime_Writes result: '%s'\n", result.c_str());
2626 if (result == "0") {
2627 ret = 0;
2628 }
2629 }
2630 } else {
2631 LOGINFO("Check_Lifetime_Writes file does not exist '%s'\n", file.c_str());
2632 }
2633 UnMount(true);
2634 } else {
2635 LOGINFO("Check_Lifetime_Writes failed to mount '%s'\n", Mount_Point.c_str());
2636 }
2637 Mount_Read_Only = original_read_only;
2638 return ret;
2639}
Ethan Yonker66a19492015-12-10 10:19:45 -06002640
2641int TWPartition::Decrypt_Adopted() {
2642#ifdef TW_INCLUDE_CRYPTO
2643 int ret = 1;
2644 Is_Adopted_Storage = false;
2645 string Adopted_Key_File = "";
2646
2647 if (!Removable)
2648 return ret;
2649
2650 int fd = open(Alternate_Block_Device.c_str(), O_RDONLY);
2651 if (fd < 0) {
2652 LOGINFO("failed to open '%s'\n", Alternate_Block_Device.c_str());
2653 return ret;
2654 }
2655 char type_guid[80];
2656 char part_guid[80];
2657
2658 if (gpt_disk_get_partition_info(fd, 2, type_guid, part_guid) == 0) {
2659 LOGINFO("type: '%s'\n", type_guid);
2660 LOGINFO("part: '%s'\n", part_guid);
2661 Adopted_GUID = part_guid;
2662 LOGINFO("Adopted_GUID '%s'\n", Adopted_GUID.c_str());
2663 if (strcmp(type_guid, TWGptAndroidExpand) == 0) {
2664 LOGINFO("android_expand found\n");
2665 Adopted_Key_File = "/data/misc/vold/expand_";
2666 Adopted_Key_File += part_guid;
2667 Adopted_Key_File += ".key";
2668 if (TWFunc::Path_Exists(Adopted_Key_File)) {
2669 Is_Adopted_Storage = true;
2670 /* Until we find a use case for this, I think it is safe
2671 * to disable USB Mass Storage whenever adopted storage
2672 * is present.
2673 */
2674 LOGINFO("Detected adopted storage, disabling USB mass storage mode\n");
2675 DataManager::SetValue("tw_has_usb_storage", 0);
2676 }
2677 }
2678 }
2679
2680 if (Is_Adopted_Storage) {
2681 string Adopted_Block_Device = Alternate_Block_Device + "p2";
2682 if (!TWFunc::Path_Exists(Adopted_Block_Device)) {
2683 Adopted_Block_Device = Alternate_Block_Device + "2";
2684 if (!TWFunc::Path_Exists(Adopted_Block_Device)) {
2685 LOGINFO("Adopted block device does not exist\n");
2686 goto exit;
2687 }
2688 }
2689 LOGINFO("key file is '%s', block device '%s'\n", Adopted_Key_File.c_str(), Adopted_Block_Device.c_str());
2690 char crypto_blkdev[MAXPATHLEN];
2691 std::string thekey;
2692 int fdkey = open(Adopted_Key_File.c_str(), O_RDONLY);
2693 if (fdkey < 0) {
2694 LOGINFO("failed to open key file\n");
2695 goto exit;
2696 }
2697 char buf[512];
2698 ssize_t n;
2699 while ((n = read(fdkey, &buf[0], sizeof(buf))) > 0) {
2700 thekey.append(buf, n);
2701 }
2702 close(fdkey);
2703 unsigned char* key = (unsigned char*) thekey.data();
2704 cryptfs_revert_ext_volume(part_guid);
2705
2706 ret = cryptfs_setup_ext_volume(part_guid, Adopted_Block_Device.c_str(), key, thekey.size(), crypto_blkdev);
2707 if (ret == 0) {
2708 LOGINFO("adopted storage new block device: '%s'\n", crypto_blkdev);
2709 Decrypted_Block_Device = crypto_blkdev;
2710 Is_Decrypted = true;
2711 Is_Encrypted = true;
2712 Find_Actual_Block_Device();
2713 if (!Mount(false)) {
2714 LOGERR("Failed to mount decrypted adopted storage device\n");
2715 Is_Decrypted = false;
2716 Is_Encrypted = false;
2717 cryptfs_revert_ext_volume(part_guid);
2718 ret = 1;
2719 } else {
Ethan Yonkerfcf3f242016-02-16 12:30:26 -06002720 UnMount(false);
2721 Has_Android_Secure = false;
2722 Symlink_Path = "";
2723 Symlink_Mount_Point = "";
2724 Backup_Name = Mount_Point.substr(1);
2725 Backup_Path = Mount_Point;
2726 TWPartition* sdext = PartitionManager.Find_Partition_By_Path("/sd-ext");
2727 if (sdext && sdext->Actual_Block_Device == Adopted_Block_Device) {
2728 LOGINFO("Removing /sd-ext from partition list due to adopted storage\n");
2729 PartitionManager.Remove_Partition_By_Path("/sd-ext");
2730 }
Ethan Yonker66a19492015-12-10 10:19:45 -06002731 Setup_Data_Media();
2732 Recreate_Media_Folder();
2733 Wipe_Available_in_GUI = true;
2734 Wipe_During_Factory_Reset = true;
2735 Can_Be_Backed_Up = true;
2736 Can_Encrypt_Backup = true;
2737 Use_Userdata_Encryption = true;
2738 Is_Storage = true;
2739 Storage_Name = "Adopted Storage";
2740 Is_SubPartition = true;
2741 SubPartition_Of = "/data";
2742 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
2743 DataManager::SetValue("tw_has_adopted_storage", 1);
2744 }
2745 } else {
2746 LOGERR("Failed to setup adopted storage decryption\n");
2747 }
2748 }
2749exit:
2750 return ret;
2751#else
2752 LOGINFO("Decrypt_Adopted: no crypto support\n");
2753 return 1;
2754#endif
2755}
2756
2757void TWPartition::Revert_Adopted() {
2758#ifdef TW_INCLUDE_CRYPTO
2759 if (!Adopted_GUID.empty()) {
2760 PartitionManager.Remove_MTP_Storage(Mount_Point);
2761 UnMount(false);
2762 cryptfs_revert_ext_volume(Adopted_GUID.c_str());
2763 Is_Adopted_Storage = false;
2764 Is_Encrypted = false;
2765 Is_Decrypted = false;
2766 Decrypted_Block_Device = "";
2767 Find_Actual_Block_Device();
2768 Wipe_During_Factory_Reset = false;
2769 Can_Be_Backed_Up = false;
2770 Can_Encrypt_Backup = false;
2771 Use_Userdata_Encryption = false;
2772 Is_SubPartition = false;
2773 SubPartition_Of = "";
2774 Has_Data_Media = false;
2775 Storage_Path = Mount_Point;
2776 if (!Symlink_Mount_Point.empty()) {
2777 TWPartition* Dat = PartitionManager.Find_Partition_By_Path("/data");
2778 if (Dat) {
2779 Dat->UnMount(false);
2780 Dat->Symlink_Mount_Point = Symlink_Mount_Point;
2781 }
2782 Symlink_Mount_Point = "";
2783 }
2784 }
2785#else
2786 LOGINFO("Revert_Adopted: no crypto support\n");
2787#endif
2788}