blob: 1261a2a26b71a43126a65403fe29214d5c4b4901 [file] [log] [blame]
Dees Troy3be70a82013-10-22 14:25:12 +00001/*
Ethan Yonker472f5062016-02-25 13:47:30 -06002 Copyright 2013 to 2016 TeamWin
Dees Troy3be70a82013-10-22 14:25:12 +00003 This file is part of TWRP/TeamWin Recovery Project.
4
5 TWRP is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 TWRP is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with TWRP. If not, see <http://www.gnu.org/licenses/>.
17*/
Dees_Troy51a0e822012-09-05 15:24:24 -040018
19#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
22#include <sys/stat.h>
23#include <sys/vfs.h>
Dees_Troy5bf43922012-09-07 16:07:55 -040024#include <sys/mount.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040025#include <unistd.h>
Dees_Troy51127312012-09-08 13:08:49 -040026#include <dirent.h>
thatf54e5392016-01-29 22:04:43 +010027#include <libgen.h>
bigbiff bigbiff9c754052013-01-09 09:09:08 -050028#include <iostream>
29#include <sstream>
Ethan Yonker66a19492015-12-10 10:19:45 -060030#include <sys/param.h>
Ethan Yonker472f5062016-02-25 13:47:30 -060031#include <fcntl.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040032
Dees_Troy657c3092012-09-10 20:32:10 -040033#ifdef TW_INCLUDE_CRYPTO
34 #include "cutils/properties.h"
35#endif
36
bigbiff7b4c7a62015-01-01 19:44:14 -050037#include "libblkid/include/blkid.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040038#include "variables.h"
Dees_Troy2673cec2013-04-02 20:22:16 +000039#include "twcommon.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040040#include "partitions.hpp"
Dees_Troy5bf43922012-09-07 16:07:55 -040041#include "data.hpp"
Dees_Troy43d8b002012-09-17 16:00:01 -040042#include "twrp-functions.hpp"
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -050043#include "twrpDigest.hpp"
bigbiff bigbiff9c754052013-01-09 09:09:08 -050044#include "twrpTar.hpp"
bigbiff bigbiff34684ff2013-12-01 21:03:45 -050045#include "twrpDU.hpp"
Ethan Yonker1b7a31b2014-07-03 15:09:22 -050046#include "infomanager.hpp"
Ethan Yonker4b94cfd2014-12-11 10:00:45 -060047#include "set_metadata.h"
Ethan Yonker74db1572015-10-28 12:44:49 -050048#include "gui/gui.hpp"
Dees_Troy5bf43922012-09-07 16:07:55 -040049extern "C" {
Dees_Troy38bd7602012-09-14 13:33:53 -040050 #include "mtdutils/mtdutils.h"
51 #include "mtdutils/mounts.h"
Dees_Troya95f55c2013-08-17 13:14:43 +000052#ifdef USE_EXT4
53 #include "make_ext4fs.h"
54#endif
Ethan Yonker71413f42014-02-26 13:36:08 -060055
56#ifdef TW_INCLUDE_CRYPTO
Ethan Yonker253368a2014-11-25 15:00:52 -060057 #include "crypto/lollipop/cryptfs.h"
Ethan Yonker66a19492015-12-10 10:19:45 -060058 #include "gpt/gpt.h"
Ethan Yonkerbc85b632015-08-09 12:48:14 -050059#else
60 #define CRYPT_FOOTER_OFFSET 0x4000
Ethan Yonker71413f42014-02-26 13:36:08 -060061#endif
Dees_Troy5bf43922012-09-07 16:07:55 -040062}
bigbiff bigbiffc49d7062013-10-11 20:28:00 -040063#ifdef HAVE_SELINUX
64#include "selinux/selinux.h"
Ethan Yonkerf27497f2014-02-09 11:48:33 -060065#include <selinux/label.h>
bigbiff bigbiffc49d7062013-10-11 20:28:00 -040066#endif
Dees Troy4159aed2014-02-28 17:24:43 +000067#ifdef HAVE_CAPABILITIES
68#include <sys/capability.h>
69#include <sys/xattr.h>
70#include <linux/xattr.h>
71#endif
HashBanged974bb2016-01-30 14:20:09 -050072#include <sparse_format.h>
Ethan Yonker472f5062016-02-25 13:47:30 -060073#include "progresstracking.hpp"
Dees_Troy51a0e822012-09-05 15:24:24 -040074
bigbiff bigbiff9c754052013-01-09 09:09:08 -050075using namespace std;
76
Dees_Troya95f55c2013-08-17 13:14:43 +000077extern struct selabel_handle *selinux_handle;
Ethan Yonker6277c792014-09-15 14:54:30 -050078extern bool datamedia;
Dees_Troya95f55c2013-08-17 13:14:43 +000079
Hashcode62bd9e02013-11-19 21:59:42 -080080struct flag_list {
81 const char *name;
82 unsigned flag;
83};
84
Matt Mower4ab42b12016-04-21 13:52:18 -050085const struct flag_list mount_flags[] = {
Hashcode62bd9e02013-11-19 21:59:42 -080086 { "noatime", MS_NOATIME },
87 { "noexec", MS_NOEXEC },
88 { "nosuid", MS_NOSUID },
89 { "nodev", MS_NODEV },
90 { "nodiratime", MS_NODIRATIME },
91 { "ro", MS_RDONLY },
92 { "rw", 0 },
93 { "remount", MS_REMOUNT },
94 { "bind", MS_BIND },
95 { "rec", MS_REC },
Dees Troyc4bc30e2014-02-03 15:04:19 +000096#ifdef MS_UNBINDABLE
Hashcode62bd9e02013-11-19 21:59:42 -080097 { "unbindable", MS_UNBINDABLE },
Dees Troyc4bc30e2014-02-03 15:04:19 +000098#endif
99#ifdef MS_PRIVATE
Hashcode62bd9e02013-11-19 21:59:42 -0800100 { "private", MS_PRIVATE },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000101#endif
102#ifdef MS_SLAVE
Hashcode62bd9e02013-11-19 21:59:42 -0800103 { "slave", MS_SLAVE },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000104#endif
105#ifdef MS_SHARED
Hashcode62bd9e02013-11-19 21:59:42 -0800106 { "shared", MS_SHARED },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000107#endif
Hashcode62bd9e02013-11-19 21:59:42 -0800108 { "sync", MS_SYNCHRONOUS },
109 { "defaults", 0 },
110 { 0, 0 },
111};
112
Matt Mower2416a502016-04-12 19:54:46 -0500113enum TW_FSTAB_FLAGS {
114 TWFLAG_DEFAULTS, // Retain position
115 TWFLAG_ANDSEC,
116 TWFLAG_BACKUP,
117 TWFLAG_BACKUPNAME,
118 TWFLAG_BLOCKSIZE,
119 TWFLAG_CANBEWIPED,
120 TWFLAG_CANENCRYPTBACKUP,
121 TWFLAG_DISPLAY,
122 TWFLAG_ENCRYPTABLE,
123 TWFLAG_FLASHIMG,
124 TWFLAG_FORCEENCRYPT,
125 TWFLAG_FSFLAGS,
126 TWFLAG_IGNOREBLKID,
127 TWFLAG_LENGTH,
128 TWFLAG_MOUNTTODECRYPT,
129 TWFLAG_REMOVABLE,
130 TWFLAG_RETAINLAYOUTVERSION,
131 TWFLAG_SETTINGSSTORAGE,
132 TWFLAG_STORAGE,
133 TWFLAG_STORAGENAME,
134 TWFLAG_SUBPARTITIONOF,
135 TWFLAG_SYMLINK,
136 TWFLAG_USERDATAENCRYPTBACKUP,
137 TWFLAG_USERMRF,
138 TWFLAG_WIPEDURINGFACTORYRESET,
139 TWFLAG_WIPEINGUI,
140};
141
142/* Flags without a trailing '=' are considered dual format flags and can be
143 * written as either 'flagname' or 'flagname=', where the character following
144 * the '=' is Y,y,1 for true and false otherwise.
145 */
146const struct flag_list tw_flags[] = {
147 { "andsec", TWFLAG_ANDSEC },
148 { "backup", TWFLAG_BACKUP },
149 { "backupname=", TWFLAG_BACKUPNAME },
150 { "blocksize=", TWFLAG_BLOCKSIZE },
151 { "canbewiped", TWFLAG_CANBEWIPED },
152 { "canencryptbackup", TWFLAG_CANENCRYPTBACKUP },
153 { "defaults", TWFLAG_DEFAULTS },
154 { "display=", TWFLAG_DISPLAY },
155 { "encryptable=", TWFLAG_ENCRYPTABLE },
156 { "flashimg", TWFLAG_FLASHIMG },
157 { "forceencrypt=", TWFLAG_FORCEENCRYPT },
158 { "fsflags=", TWFLAG_FSFLAGS },
159 { "ignoreblkid", TWFLAG_IGNOREBLKID },
160 { "length=", TWFLAG_LENGTH },
161 { "mounttodecrypt", TWFLAG_MOUNTTODECRYPT },
162 { "removable", TWFLAG_REMOVABLE },
163 { "retainlayoutversion", TWFLAG_RETAINLAYOUTVERSION },
164 { "settingsstorage", TWFLAG_SETTINGSSTORAGE },
165 { "storage", TWFLAG_STORAGE },
166 { "storagename=", TWFLAG_STORAGENAME },
167 { "subpartitionof=", TWFLAG_SUBPARTITIONOF },
168 { "symlink=", TWFLAG_SYMLINK },
169 { "userdataencryptbackup", TWFLAG_USERDATAENCRYPTBACKUP },
170 { "usermrf", TWFLAG_USERMRF },
171 { "wipeduringfactoryreset", TWFLAG_WIPEDURINGFACTORYRESET },
172 { "wipeingui", TWFLAG_WIPEINGUI },
173 { 0, 0 },
174};
175
that9e0593e2014-10-08 00:01:24 +0200176TWPartition::TWPartition() {
Dees_Troy51a0e822012-09-05 15:24:24 -0400177 Can_Be_Mounted = false;
178 Can_Be_Wiped = false;
Dees_Troya13d74f2013-03-24 08:54:55 -0500179 Can_Be_Backed_Up = false;
Vojtech Bocek1dc30982013-08-30 21:49:30 +0200180 Use_Rm_Rf = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400181 Wipe_During_Factory_Reset = false;
182 Wipe_Available_in_GUI = false;
183 Is_SubPartition = false;
Dees_Troy2691f9d2012-09-24 11:15:49 -0400184 Has_SubPartition = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400185 SubPartition_Of = "";
186 Symlink_Path = "";
187 Symlink_Mount_Point = "";
188 Mount_Point = "";
Dees_Troye58d5262012-09-21 12:27:57 -0400189 Backup_Path = "";
Dees_Troy38bd7602012-09-14 13:33:53 -0400190 Actual_Block_Device = "";
191 Primary_Block_Device = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400192 Alternate_Block_Device = "";
193 Removable = false;
194 Is_Present = false;
195 Length = 0;
196 Size = 0;
197 Used = 0;
198 Free = 0;
199 Backup_Size = 0;
200 Can_Be_Encrypted = false;
201 Is_Encrypted = false;
202 Is_Decrypted = false;
Ethan Yonker253368a2014-11-25 15:00:52 -0600203 Mount_To_Decrypt = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400204 Decrypted_Block_Device = "";
205 Display_Name = "";
Dees_Troya13d74f2013-03-24 08:54:55 -0500206 Backup_Display_Name = "";
207 Storage_Name = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400208 Backup_Name = "";
Dees_Troy63c8df72012-09-10 14:02:05 -0400209 Backup_FileName = "";
Dees_Troy38bd7602012-09-14 13:33:53 -0400210 MTD_Name = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400211 Backup_Method = NONE;
Dees_Troy83bd4832013-05-04 12:39:56 +0000212 Can_Encrypt_Backup = false;
213 Use_Userdata_Encryption = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400214 Has_Data_Media = false;
Dees_Troye58d5262012-09-21 12:27:57 -0400215 Has_Android_Secure = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400216 Is_Storage = false;
Dees_Troya13d74f2013-03-24 08:54:55 -0500217 Is_Settings_Storage = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400218 Storage_Path = "";
219 Current_File_System = "";
220 Fstab_File_System = "";
Hashcode62bd9e02013-11-19 21:59:42 -0800221 Mount_Flags = 0;
222 Mount_Options = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400223 Format_Block_Size = 0;
Dees_Troy68cab492012-12-12 19:29:35 +0000224 Ignore_Blkid = false;
Dees_Troy16c2b312013-01-15 16:51:18 +0000225 Retain_Layout_Version = false;
Ethan Yonker253368a2014-11-25 15:00:52 -0600226 Crypto_Key_Location = "footer";
Ethan Yonker726a0202014-12-16 20:01:38 -0600227 MTP_Storage_ID = 0;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600228 Can_Flash_Img = false;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500229 Mount_Read_Only = false;
Ethan Yonker66a19492015-12-10 10:19:45 -0600230 Is_Adopted_Storage = false;
231 Adopted_GUID = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400232}
233
234TWPartition::~TWPartition(void) {
235 // Do nothing
236}
237
Matt Mower2b2dd152016-04-26 11:24:08 -0500238bool TWPartition::Process_Fstab_Line(const char *fstab_line, bool Display_Error) {
239 char full_line[MAX_FSTAB_LINE_LENGTH];
Matt Mower2416a502016-04-12 19:54:46 -0500240 char twflags[MAX_FSTAB_LINE_LENGTH] = "";
Dees_Troy5bf43922012-09-07 16:07:55 -0400241 char* ptr;
Matt Mower2b2dd152016-04-26 11:24:08 -0500242 int line_len = strlen(fstab_line), index = 0, item_index = 0;
Dees_Troya13d74f2013-03-24 08:54:55 -0500243 bool skip = false;
Dees_Troy5bf43922012-09-07 16:07:55 -0400244
Matt Mower2b2dd152016-04-26 11:24:08 -0500245 strlcpy(full_line, fstab_line, sizeof(full_line));
Dees_Troy51127312012-09-08 13:08:49 -0400246 for (index = 0; index < line_len; index++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500247 if (full_line[index] == 34)
248 skip = !skip;
249 if (!skip && full_line[index] <= 32)
Dees_Troy5bf43922012-09-07 16:07:55 -0400250 full_line[index] = '\0';
Dees_Troy5bf43922012-09-07 16:07:55 -0400251 }
Dees_Troy7c2dec82012-09-26 09:49:14 -0400252 Mount_Point = full_line;
Dees_Troy2673cec2013-04-02 20:22:16 +0000253 LOGINFO("Processing '%s'\n", Mount_Point.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -0400254 Backup_Path = Mount_Point;
Dees_Troya13d74f2013-03-24 08:54:55 -0500255 Storage_Path = Mount_Point;
Dees_Troy70737fa2013-04-08 13:19:20 +0000256 Display_Name = full_line + 1;
257 Backup_Display_Name = Display_Name;
258 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400259 index = Mount_Point.size();
260 while (index < line_len) {
261 while (index < line_len && full_line[index] == '\0')
262 index++;
263 if (index >= line_len)
264 continue;
265 ptr = full_line + index;
266 if (item_index == 0) {
267 // File System
268 Fstab_File_System = ptr;
269 Current_File_System = ptr;
270 item_index++;
271 } else if (item_index == 1) {
272 // Primary Block Device
Dees_Troy38bd7602012-09-14 13:33:53 -0400273 if (Fstab_File_System == "mtd" || Fstab_File_System == "yaffs2") {
Dees_Troy094207a2012-09-26 12:00:39 -0400274 MTD_Name = ptr;
275 Find_MTD_Block_Device(MTD_Name);
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400276 } else if (Fstab_File_System == "bml") {
277 if (Mount_Point == "/boot")
278 MTD_Name = "boot";
279 else if (Mount_Point == "/recovery")
280 MTD_Name = "recovery";
281 Primary_Block_Device = ptr;
282 if (*ptr != '/')
Dees_Troy2673cec2013-04-02 20:22:16 +0000283 LOGERR("Until we get better BML support, you will have to find and provide the full block device path to the BML devices e.g. /dev/block/bml9 instead of the partition name\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400284 } else if (*ptr != '/') {
Dees_Troy5bf43922012-09-07 16:07:55 -0400285 if (Display_Error)
Matt Mower2b2dd152016-04-26 11:24:08 -0500286 LOGERR("Invalid block device '%s' in fstab line '%s'", ptr, fstab_line);
Dees_Troy5bf43922012-09-07 16:07:55 -0400287 else
Matt Mower2b2dd152016-04-26 11:24:08 -0500288 LOGINFO("Invalid block device '%s' in fstab line '%s'", ptr, fstab_line);
Matt Mower72c87ce2016-04-26 14:34:56 -0500289 return false;
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));
Dees_Troy38bd7602012-09-14 13:33:53 -0400308 } else if (strlen(ptr) == 4 && (strncmp(ptr, "NULL", 4) == 0 || strncmp(ptr, "null", 4) == 0 || strncmp(ptr, "null", 4) == 0)) {
309 // Do nothing
Dees_Troy5bf43922012-09-07 16:07:55 -0400310 } else {
311 // Unhandled data
Matt Mower2b2dd152016-04-26 11:24:08 -0500312 LOGINFO("Unhandled fstab information '%s' in fstab line '%s'\n", ptr, fstab_line);
Dees_Troy5bf43922012-09-07 16:07:55 -0400313 }
314 }
315 while (index < line_len && full_line[index] != '\0')
316 index++;
317 }
318
319 if (!Is_File_System(Fstab_File_System) && !Is_Image(Fstab_File_System)) {
320 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000321 LOGERR("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400322 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000323 LOGINFO("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Matt Mower72c87ce2016-04-26 14:34:56 -0500324 return false;
Dees_Troy5bf43922012-09-07 16:07:55 -0400325 } else if (Is_File_System(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400326 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400327 Setup_File_System(Display_Error);
328 if (Mount_Point == "/system") {
329 Display_Name = "System";
Dees_Troya13d74f2013-03-24 08:54:55 -0500330 Backup_Display_Name = Display_Name;
331 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400332 Wipe_Available_in_GUI = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500333 Can_Be_Backed_Up = true;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500334 Mount_Read_Only = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400335 } else if (Mount_Point == "/data") {
336 Display_Name = "Data";
Dees_Troya13d74f2013-03-24 08:54:55 -0500337 Backup_Display_Name = Display_Name;
338 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400339 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400340 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500341 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000342 Can_Encrypt_Backup = true;
343 Use_Userdata_Encryption = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400344 } else if (Mount_Point == "/cache") {
345 Display_Name = "Cache";
Dees_Troya13d74f2013-03-24 08:54:55 -0500346 Backup_Display_Name = Display_Name;
347 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400348 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400349 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500350 Can_Be_Backed_Up = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400351 } else if (Mount_Point == "/datadata") {
Dees_Troy51127312012-09-08 13:08:49 -0400352 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400353 Display_Name = "DataData";
Dees_Troya13d74f2013-03-24 08:54:55 -0500354 Backup_Display_Name = Display_Name;
355 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400356 Is_SubPartition = true;
357 SubPartition_Of = "/data";
Dees_Troy5bf43922012-09-07 16:07:55 -0400358 DataManager::SetValue(TW_HAS_DATADATA, 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500359 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000360 Can_Encrypt_Backup = true;
361 Use_Userdata_Encryption = false; // This whole partition should be encrypted
Dees_Troy5bf43922012-09-07 16:07:55 -0400362 } else if (Mount_Point == "/sd-ext") {
Dees_Troy51127312012-09-08 13:08:49 -0400363 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400364 Display_Name = "SD-Ext";
Dees_Troya13d74f2013-03-24 08:54:55 -0500365 Backup_Display_Name = Display_Name;
366 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400367 Wipe_Available_in_GUI = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400368 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500369 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000370 Can_Encrypt_Backup = true;
371 Use_Userdata_Encryption = true;
Dees_Troy2c50e182012-09-26 20:05:28 -0400372 } else if (Mount_Point == "/boot") {
373 Display_Name = "Boot";
Dees_Troya13d74f2013-03-24 08:54:55 -0500374 Backup_Display_Name = Display_Name;
Dees_Troy2c50e182012-09-26 20:05:28 -0400375 DataManager::SetValue("tw_boot_is_mountable", 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500376 Can_Be_Backed_Up = true;
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500377 } else if (Mount_Point == "/vendor") {
378 Display_Name = "Vendor";
379 Backup_Display_Name = Display_Name;
380 Storage_Name = Display_Name;
381 Mount_Read_Only = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400382 }
383#ifdef TW_EXTERNAL_STORAGE_PATH
384 if (Mount_Point == EXPAND(TW_EXTERNAL_STORAGE_PATH)) {
385 Is_Storage = true;
386 Storage_Path = EXPAND(TW_EXTERNAL_STORAGE_PATH);
Dees_Troyc51f1f92012-09-20 15:32:13 -0400387 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500388 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400389#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000390 if (Mount_Point == "/sdcard" || Mount_Point == "/external_sd" || Mount_Point == "/external_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400391 Is_Storage = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400392 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500393 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400394#endif
Dees_Troyb05ddee2013-01-28 20:24:50 +0000395 }
Dees_Troy8170a922012-09-18 15:40:25 -0400396#ifdef TW_INTERNAL_STORAGE_PATH
397 if (Mount_Point == EXPAND(TW_INTERNAL_STORAGE_PATH)) {
398 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500399 Is_Settings_Storage = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400400 Storage_Path = EXPAND(TW_INTERNAL_STORAGE_PATH);
Dees_Troya13d74f2013-03-24 08:54:55 -0500401 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400402 }
403#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000404 if (Mount_Point == "/emmc" || Mount_Point == "/internal_sd" || Mount_Point == "/internal_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400405 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500406 Is_Settings_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500407 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400408 }
409#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400410 } else if (Is_Image(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400411 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400412 Setup_Image(Display_Error);
Dees_Troya13d74f2013-03-24 08:54:55 -0500413 if (Mount_Point == "/boot") {
414 Display_Name = "Boot";
415 Backup_Display_Name = Display_Name;
416 Can_Be_Backed_Up = true;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600417 Can_Flash_Img = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500418 } else if (Mount_Point == "/recovery") {
419 Display_Name = "Recovery";
420 Backup_Display_Name = Display_Name;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600421 Can_Flash_Img = true;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500422 } else if (Mount_Point == "/system_image") {
423 Display_Name = "System Image";
424 Backup_Display_Name = Display_Name;
HashBanged974bb2016-01-30 14:20:09 -0500425 Can_Flash_Img = true;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500426 Can_Be_Backed_Up = true;
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500427 } else if (Mount_Point == "/vendor_image") {
428 Display_Name = "Vendor Image";
429 Backup_Display_Name = Display_Name;
HashBanged974bb2016-01-30 14:20:09 -0500430 Can_Flash_Img = true;
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500431 Can_Be_Backed_Up = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500432 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400433 }
434
Matt Mower2416a502016-04-12 19:54:46 -0500435 // Process TWRP fstab flags
436 if (strlen(twflags) > 0) {
437 string Prev_Display_Name = Display_Name;
438 string Prev_Storage_Name = Storage_Name;
439 string Prev_Backup_Display_Name = Backup_Display_Name;
440 Display_Name = "";
441 Storage_Name = "";
442 Backup_Display_Name = "";
443
444 Process_TW_Flags(twflags, Display_Error);
445
446 bool has_display_name = !Display_Name.empty();
447 bool has_storage_name = !Storage_Name.empty();
448 bool has_backup_name = !Backup_Display_Name.empty();
449 if (!has_display_name) Display_Name = Prev_Display_Name;
450 if (!has_storage_name) Storage_Name = Prev_Storage_Name;
451 if (!has_backup_name) Backup_Display_Name = Prev_Backup_Display_Name;
452
453 if (has_display_name && !has_storage_name)
454 Storage_Name = Display_Name;
455 if (!has_display_name && has_storage_name)
456 Display_Name = Storage_Name;
457 if (has_display_name && !has_backup_name && Backup_Display_Name != "Android Secure")
458 Backup_Display_Name = Display_Name;
459 if (!has_display_name && has_backup_name)
460 Display_Name = Backup_Display_Name;
461 }
Dees_Troy51127312012-09-08 13:08:49 -0400462 return true;
463}
464
Matt Mower72c87ce2016-04-26 14:34:56 -0500465void TWPartition::Partition_Post_Processing(bool Display_Error) {
466 if (Mount_Point == "/data")
467 Setup_Data_Partition(Display_Error);
468 else if (Mount_Point == "/cache")
469 Setup_Cache_Partition(Display_Error);
470}
471
472void TWPartition::Setup_Data_Partition(bool Display_Error) {
473 if (Mount_Point != "/data")
474 return;
475
476 // Ensure /data is not mounted as tmpfs for qcom hardware decrypt
477 UnMount(false);
478
479#ifdef TW_INCLUDE_CRYPTO
480 if (datamedia)
481 Setup_Data_Media();
482 Can_Be_Encrypted = true;
483 char crypto_blkdev[255];
484 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
485 if (strcmp(crypto_blkdev, "error") != 0) {
486 DataManager::SetValue(TW_IS_DECRYPTED, 1);
487 Is_Encrypted = true;
488 Is_Decrypted = true;
489 Decrypted_Block_Device = crypto_blkdev;
490 LOGINFO("Data already decrypted, new block device: '%s'\n", crypto_blkdev);
491 } else if (!Mount(false)) {
492 if (Is_Present) {
493 set_partition_data(Actual_Block_Device.c_str(), Crypto_Key_Location.c_str(), Fstab_File_System.c_str());
494 if (cryptfs_check_footer() == 0) {
495 Is_Encrypted = true;
496 Is_Decrypted = false;
497 Can_Be_Mounted = false;
498 Current_File_System = "emmc";
499 Setup_Image(Display_Error);
500 DataManager::SetValue(TW_IS_ENCRYPTED, 1);
501 DataManager::SetValue(TW_CRYPTO_PWTYPE, cryptfs_get_password_type());
502 DataManager::SetValue(TW_CRYPTO_PASSWORD, "");
503 DataManager::SetValue("tw_crypto_display", "");
504 } else {
505 gui_err("mount_data_footer=Could not mount /data and unable to find crypto footer.");
506 }
507 } else {
508 LOGERR("Primary block device '%s' for mount point '%s' is not present!\n", Primary_Block_Device.c_str(), Mount_Point.c_str());
509 }
510 } else {
511 // Filesystem is not encrypted and the mount succeeded, so return to
512 // the original unmounted state
513 UnMount(false);
514 }
515 if (datamedia && (!Is_Encrypted || (Is_Encrypted && Is_Decrypted))) {
516 Setup_Data_Media();
517 Recreate_Media_Folder();
518 }
519#else
520 if (datamedia) {
521 Setup_Data_Media();
522 Recreate_Media_Folder();
523 }
524#endif
525}
526
527void TWPartition::Setup_Cache_Partition(bool Display_Error __unused) {
528 if (Mount_Point != "/cache")
529 return;
530
531 if (!Mount(true))
532 return;
533
534 if (!TWFunc::Path_Exists("/cache/recovery/.")) {
535 LOGINFO("Recreating /cache/recovery folder\n");
536 if (mkdir("/cache/recovery", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0)
537 LOGERR("Could not create /cache/recovery\n");
538 }
539}
540
Matt Mower4ab42b12016-04-21 13:52:18 -0500541void TWPartition::Process_FS_Flags(const char *str) {
542 char *options = strdup(str);
543 char *ptr, *savep;
Hashcode62bd9e02013-11-19 21:59:42 -0800544
Matt Mower4ab42b12016-04-21 13:52:18 -0500545 Mount_Options = "";
Hashcode62bd9e02013-11-19 21:59:42 -0800546
Matt Mower4ab42b12016-04-21 13:52:18 -0500547 // Avoid issues with potentially nested strtok by using strtok_r
548 ptr = strtok_r(options, ",", &savep);
549 while (ptr) {
550 const struct flag_list* mount_flag = mount_flags;
551
552 for (; mount_flag->name; mount_flag++) {
553 // mount_flags are never postfixed by '=',
554 // so only match identical strings (including length)
555 if (strcmp(ptr, mount_flag->name) == 0) {
556 Mount_Flags |= mount_flag->flag;
Hashcode62bd9e02013-11-19 21:59:42 -0800557 break;
558 }
559 }
560
Matt Mower4ab42b12016-04-21 13:52:18 -0500561 if (mount_flag->flag == MS_RDONLY)
562 Mount_Read_Only = true;
Hashcode62bd9e02013-11-19 21:59:42 -0800563
Matt Mower4ab42b12016-04-21 13:52:18 -0500564 if (mount_flag->name != 0) {
565 if (!Mount_Options.empty())
566 Mount_Options += ",";
567 Mount_Options += mount_flag->name;
568 } else {
569 LOGINFO("Unhandled mount flag: '%s'\n", ptr);
570 }
571
572 ptr = strtok_r(NULL, ",", &savep);
573 }
574 free(options);
Hashcode62bd9e02013-11-19 21:59:42 -0800575}
576
Matt Mower2416a502016-04-12 19:54:46 -0500577void TWPartition::Apply_TW_Flag(const unsigned flag, const char* str, const bool val) {
578 switch (flag) {
579 case TWFLAG_ANDSEC:
580 Has_Android_Secure = val;
581 break;
582 case TWFLAG_BACKUP:
583 Can_Be_Backed_Up = val;
584 break;
585 case TWFLAG_BACKUPNAME:
586 Backup_Display_Name = str;
587 break;
588 case TWFLAG_BLOCKSIZE:
589 Format_Block_Size = (unsigned long)(atol(str));
590 break;
591 case TWFLAG_CANBEWIPED:
592 Can_Be_Wiped = val;
593 break;
594 case TWFLAG_CANENCRYPTBACKUP:
595 Can_Encrypt_Backup = val;
596 break;
597 case TWFLAG_DEFAULTS:
598 // Do nothing
599 break;
600 case TWFLAG_DISPLAY:
601 Display_Name = str;
602 break;
603 case TWFLAG_ENCRYPTABLE:
604 case TWFLAG_FORCEENCRYPT:
605 Crypto_Key_Location = str;
606 break;
607 case TWFLAG_FLASHIMG:
608 Can_Flash_Img = val;
609 break;
610 case TWFLAG_FSFLAGS:
Matt Mower4ab42b12016-04-21 13:52:18 -0500611 Process_FS_Flags(str);
Matt Mower2416a502016-04-12 19:54:46 -0500612 break;
613 case TWFLAG_IGNOREBLKID:
614 Ignore_Blkid = val;
615 break;
616 case TWFLAG_LENGTH:
617 Length = atoi(str);
618 break;
619 case TWFLAG_MOUNTTODECRYPT:
620 Mount_To_Decrypt = val;
621 break;
622 case TWFLAG_REMOVABLE:
623 Removable = val;
624 break;
625 case TWFLAG_RETAINLAYOUTVERSION:
626 Retain_Layout_Version = val;
627 break;
628 case TWFLAG_SETTINGSSTORAGE:
629 Is_Settings_Storage = val;
630 if (Is_Settings_Storage)
631 Is_Storage = true;
632 break;
633 case TWFLAG_STORAGE:
634 Is_Storage = val;
635 break;
636 case TWFLAG_STORAGENAME:
637 Storage_Name = str;
638 break;
639 case TWFLAG_SUBPARTITIONOF:
640 Is_SubPartition = true;
641 SubPartition_Of = str;
642 break;
643 case TWFLAG_SYMLINK:
644 Symlink_Path = str;
645 break;
646 case TWFLAG_USERDATAENCRYPTBACKUP:
647 Use_Userdata_Encryption = val;
648 if (Use_Userdata_Encryption)
649 Can_Encrypt_Backup = true;
650 break;
651 case TWFLAG_USERMRF:
652 Use_Rm_Rf = val;
653 break;
654 case TWFLAG_WIPEDURINGFACTORYRESET:
655 Wipe_During_Factory_Reset = val;
656 if (Wipe_During_Factory_Reset) {
657 Can_Be_Wiped = true;
658 Wipe_Available_in_GUI = true;
659 }
660 break;
661 case TWFLAG_WIPEINGUI:
662 Wipe_Available_in_GUI = val;
663 if (Wipe_Available_in_GUI)
664 Can_Be_Wiped = true;
665 break;
666 default:
667 // Should not get here
668 LOGINFO("Flag identified for processing, but later unmatched: %i\n", flag);
669 break;
670 }
671}
Dees_Troy51127312012-09-08 13:08:49 -0400672
Matt Mower2416a502016-04-12 19:54:46 -0500673void TWPartition::Process_TW_Flags(char *flags, bool Display_Error) {
674 char separator[2] = {'\n', 0};
675 char *ptr, *savep;
676
677 // Semicolons within double-quotes are not forbidden, so replace
678 // only the semicolons intended as separators with '\n' for strtok
679 for (unsigned i = 0, skip = 0; i < strlen(flags); i++) {
680 if (flags[i] == '\"')
Dees_Troya13d74f2013-03-24 08:54:55 -0500681 skip = !skip;
Matt Mower2416a502016-04-12 19:54:46 -0500682 if (!skip && flags[i] == ';')
683 flags[i] = separator[0];
Dees_Troy51127312012-09-08 13:08:49 -0400684 }
685
Matt Mower2416a502016-04-12 19:54:46 -0500686 // Avoid issues with potentially nested strtok by using strtok_r
687 ptr = strtok_r(flags, separator, &savep);
688 while (ptr) {
689 int ptr_len = strlen(ptr);
690 const struct flag_list* tw_flag = tw_flags;
Hashcode62bd9e02013-11-19 21:59:42 -0800691
Matt Mower2416a502016-04-12 19:54:46 -0500692 for (; tw_flag->name; tw_flag++) {
693 int flag_len = strlen(tw_flag->name);
694
695 if (strncmp(ptr, tw_flag->name, flag_len) == 0) {
696 bool flag_val = false;
697
698 if (ptr_len > flag_len && (tw_flag->name)[flag_len-1] != '='
699 && ptr[flag_len] != '=') {
700 // Handle flags with same starting string
701 // (e.g. backup and backupname)
702 continue;
703 } else if (ptr_len > flag_len && ptr[flag_len] == '=') {
704 // Handle flags with dual format: Part 1
705 // (e.g. backup and backup=y. backup=y handled here)
706 ptr += flag_len + 1;
707 TWFunc::Strip_Quotes(ptr);
708 // Skip flags with empty argument
709 // (e.g. backup=)
710 if (strlen(ptr) == 0) {
711 LOGINFO("Flag missing argument or should not include '=': %s=\n", tw_flag->name);
712 break;
713 }
714 flag_val = strchr("yY1", *ptr) != NULL;
715 } else if (ptr_len == flag_len
716 && (tw_flag->name)[flag_len-1] == '=') {
717 // Skip flags missing argument after =
718 // (e.g. backupname=)
719 LOGINFO("Flag missing argument: %s\n", tw_flag->name);
720 break;
721 } else if (ptr_len > flag_len
722 && (tw_flag->name)[flag_len-1] == '=') {
723 // Handle arguments to flags
724 // (e.g. backupname="My Stuff")
725 ptr += flag_len;
726 TWFunc::Strip_Quotes(ptr);
727 // Skip flags with empty argument
728 // (e.g. backupname="")
729 if (strlen(ptr) == 0) {
730 LOGINFO("Flag missing argument: %s\n", tw_flag->name);
731 break;
732 }
733 } else if (ptr_len == flag_len) {
734 // Handle flags with dual format: Part 2
735 // (e.g. backup and backup=y. backup handled here)
736 flag_val = true;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600737 } else {
Matt Mower2416a502016-04-12 19:54:46 -0500738 LOGINFO("Flag matched, but could not be processed: %s\n", ptr);
739 break;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600740 }
Matt Mower2416a502016-04-12 19:54:46 -0500741
742 Apply_TW_Flag(tw_flag->flag, ptr, flag_val);
743 break;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600744 }
Matt Mower2416a502016-04-12 19:54:46 -0500745 }
746 if (tw_flag->name == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400747 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000748 LOGERR("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400749 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000750 LOGINFO("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400751 }
Matt Mower2416a502016-04-12 19:54:46 -0500752 ptr = strtok_r(NULL, separator, &savep);
Dees_Troy51127312012-09-08 13:08:49 -0400753 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400754}
755
Dees_Troy5bf43922012-09-07 16:07:55 -0400756bool TWPartition::Is_File_System(string File_System) {
757 if (File_System == "ext2" ||
Dees_Troy63c8df72012-09-10 14:02:05 -0400758 File_System == "ext3" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400759 File_System == "ext4" ||
760 File_System == "vfat" ||
761 File_System == "ntfs" ||
762 File_System == "yaffs2" ||
bigbiff bigbiff3e146522012-11-14 14:32:59 -0500763 File_System == "exfat" ||
Dees_Troye5017042013-08-29 16:38:55 +0000764 File_System == "f2fs" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400765 File_System == "auto")
766 return true;
767 else
768 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400769}
770
Dees_Troy5bf43922012-09-07 16:07:55 -0400771bool TWPartition::Is_Image(string File_System) {
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400772 if (File_System == "emmc" || File_System == "mtd" || File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -0400773 return true;
774 else
775 return false;
776}
777
Dees_Troy51127312012-09-08 13:08:49 -0400778bool TWPartition::Make_Dir(string Path, bool Display_Error) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400779 if (!TWFunc::Path_Exists(Path)) {
Dees_Troy51127312012-09-08 13:08:49 -0400780 if (mkdir(Path.c_str(), 0777) == -1) {
781 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -0500782 gui_msg(Msg(msg::kError, "create_folder_strerr=Can not create '{1}' folder ({2}).")(Path)(strerror(errno)));
Dees_Troy51127312012-09-08 13:08:49 -0400783 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000784 LOGINFO("Can not create '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400785 return false;
786 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000787 LOGINFO("Created '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400788 return true;
789 }
790 }
791 return true;
792}
793
Dees_Troy5bf43922012-09-07 16:07:55 -0400794void TWPartition::Setup_File_System(bool Display_Error) {
795 struct statfs st;
796
797 Can_Be_Mounted = true;
798 Can_Be_Wiped = true;
799
Dees_Troy5bf43922012-09-07 16:07:55 -0400800 // Make the mount point folder if it doesn't exist
Dees_Troy51127312012-09-08 13:08:49 -0400801 Make_Dir(Mount_Point, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400802 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
803 Backup_Name = Display_Name;
804 Backup_Method = FILES;
805}
806
807void TWPartition::Setup_Image(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400808 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
809 Backup_Name = Display_Name;
Gary Peck82599a82012-11-21 16:23:12 -0800810 if (Current_File_System == "emmc")
Dees_Troy5bf43922012-09-07 16:07:55 -0400811 Backup_Method = DD;
Gary Peck82599a82012-11-21 16:23:12 -0800812 else if (Current_File_System == "mtd" || Current_File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -0400813 Backup_Method = FLASH_UTILS;
814 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000815 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 -0400816 if (Find_Partition_Size()) {
817 Used = Size;
818 Backup_Size = Size;
Dees_Troy51a0e822012-09-05 15:24:24 -0400819 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400820 if (Display_Error)
Dees Troyd932ce12013-10-18 17:12:59 +0000821 LOGERR("Unable to find partition size for '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400822 else
Dees Troyd932ce12013-10-18 17:12:59 +0000823 LOGINFO("Unable to find partition size for '%s'\n", Mount_Point.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400824 }
825}
826
Dees_Troye58d5262012-09-21 12:27:57 -0400827void TWPartition::Setup_AndSec(void) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500828 Backup_Display_Name = "Android Secure";
Dees_Troye58d5262012-09-21 12:27:57 -0400829 Backup_Name = "and-sec";
Dees_Troya13d74f2013-03-24 08:54:55 -0500830 Can_Be_Backed_Up = true;
Dees_Troye58d5262012-09-21 12:27:57 -0400831 Has_Android_Secure = true;
832 Symlink_Path = Mount_Point + "/.android_secure";
833 Symlink_Mount_Point = "/and-sec";
834 Backup_Path = Symlink_Mount_Point;
835 Make_Dir("/and-sec", true);
836 Recreate_AndSec_Folder();
Ethan Yonkerd4d10732014-02-03 15:27:52 -0600837 Mount_Storage_Retry();
Dees_Troye58d5262012-09-21 12:27:57 -0400838}
839
that9e0593e2014-10-08 00:01:24 +0200840void TWPartition::Setup_Data_Media() {
Ethan Yonker6277c792014-09-15 14:54:30 -0500841 LOGINFO("Setting up '%s' as data/media emulated storage.\n", Mount_Point.c_str());
Ethan Yonker66a19492015-12-10 10:19:45 -0600842 if (Storage_Name.empty() || Storage_Name == "Data")
843 Storage_Name = "Internal Storage";
Ethan Yonker6277c792014-09-15 14:54:30 -0500844 Has_Data_Media = true;
845 Is_Storage = true;
Ethan Yonker66a19492015-12-10 10:19:45 -0600846 Storage_Path = Mount_Point + "/media";
Ethan Yonker6277c792014-09-15 14:54:30 -0500847 Symlink_Path = Storage_Path;
Ethan Yonker66a19492015-12-10 10:19:45 -0600848 if (Mount_Point == "/data") {
849 Is_Settings_Storage = true;
850 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
851 Make_Dir("/emmc", false);
852 Symlink_Mount_Point = "/emmc";
853 } else {
854 Make_Dir("/sdcard", false);
855 Symlink_Mount_Point = "/sdcard";
856 }
857 if (Mount(false) && TWFunc::Path_Exists(Mount_Point + "/media/0")) {
858 Storage_Path = Mount_Point + "/media/0";
859 Symlink_Path = Storage_Path;
860 DataManager::SetValue(TW_INTERNAL_PATH, Mount_Point + "/media/0");
861 UnMount(true);
862 }
863 DataManager::SetValue("tw_has_internal", 1);
864 DataManager::SetValue("tw_has_data_media", 1);
865 du.add_absolute_dir(Mount_Point + "/misc/vold");
866 du.add_absolute_dir(Mount_Point + "/.layout_version");
867 du.add_absolute_dir(Mount_Point + "/system/storage.xml");
Ethan Yonker6277c792014-09-15 14:54:30 -0500868 } else {
Ethan Yonker66a19492015-12-10 10:19:45 -0600869 if (Mount(true) && TWFunc::Path_Exists(Mount_Point + "/media/0")) {
870 Storage_Path = Mount_Point + "/media/0";
871 Symlink_Path = Storage_Path;
872 UnMount(true);
873 }
Ethan Yonker6277c792014-09-15 14:54:30 -0500874 }
Ethan Yonker66a19492015-12-10 10:19:45 -0600875 du.add_absolute_dir(Mount_Point + "/media");
Ethan Yonker6277c792014-09-15 14:54:30 -0500876}
877
Dees_Troy5bf43922012-09-07 16:07:55 -0400878void TWPartition::Find_Real_Block_Device(string& Block, bool Display_Error) {
879 char device[512], realDevice[512];
880
881 strcpy(device, Block.c_str());
882 memset(realDevice, 0, sizeof(realDevice));
883 while (readlink(device, realDevice, sizeof(realDevice)) > 0)
884 {
885 strcpy(device, realDevice);
886 memset(realDevice, 0, sizeof(realDevice));
887 }
888
889 if (device[0] != '/') {
890 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000891 LOGERR("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400892 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000893 LOGINFO("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400894 return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400895 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400896 Block = device;
897 return;
898 }
899}
900
Dees_Troy8e337f32012-10-13 22:07:49 -0400901void TWPartition::Mount_Storage_Retry(void) {
902 // On some devices, storage doesn't want to mount right away, retry and sleep
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500903 if (!Mount(true)) {
Dees_Troy8e337f32012-10-13 22:07:49 -0400904 int retry_count = 5;
905 while (retry_count > 0 && !Mount(false)) {
906 usleep(500000);
907 retry_count--;
908 }
909 Mount(true);
910 }
911}
912
Dees_Troy38bd7602012-09-14 13:33:53 -0400913bool TWPartition::Find_MTD_Block_Device(string MTD_Name) {
914 FILE *fp = NULL;
915 char line[255];
916
917 fp = fopen("/proc/mtd", "rt");
918 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000919 LOGERR("Device does not support /proc/mtd\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400920 return false;
921 }
922
923 while (fgets(line, sizeof(line), fp) != NULL)
924 {
925 char device[32], label[32];
926 unsigned long size = 0;
927 char* fstype = NULL;
928 int deviceId;
929
930 sscanf(line, "%s %lx %*s %*c%s", device, &size, label);
931
932 // Skip header and blank lines
933 if ((strcmp(device, "dev:") == 0) || (strlen(line) < 8))
934 continue;
935
936 // Strip off the trailing " from the label
937 label[strlen(label)-1] = '\0';
938
939 if (strcmp(label, MTD_Name.c_str()) == 0) {
940 // We found our device
941 // Strip off the trailing : from the device
942 device[strlen(device)-1] = '\0';
943 if (sscanf(device,"mtd%d", &deviceId) == 1) {
944 sprintf(device, "/dev/block/mtdblock%d", deviceId);
945 Primary_Block_Device = device;
Dees_Troy76543db2013-06-19 16:24:30 +0000946 fclose(fp);
947 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -0400948 }
949 }
950 }
951 fclose(fp);
952
953 return false;
954}
955
Dees_Troy51127312012-09-08 13:08:49 -0400956bool TWPartition::Get_Size_Via_statfs(bool Display_Error) {
957 struct statfs st;
958 string Local_Path = Mount_Point + "/.";
959
960 if (!Mount(Display_Error))
961 return false;
962
963 if (statfs(Local_Path.c_str(), &st) != 0) {
964 if (!Removable) {
965 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000966 LOGERR("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400967 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000968 LOGINFO("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400969 }
970 return false;
971 }
972 Size = (st.f_blocks * st.f_bsize);
973 Used = ((st.f_blocks - st.f_bfree) * st.f_bsize);
974 Free = (st.f_bfree * st.f_bsize);
975 Backup_Size = Used;
976 return true;
977}
978
979bool TWPartition::Get_Size_Via_df(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400980 FILE* fp;
981 char command[255], line[512];
982 int include_block = 1;
983 unsigned int min_len;
984
985 if (!Mount(Display_Error))
986 return false;
987
Dees_Troy38bd7602012-09-14 13:33:53 -0400988 min_len = Actual_Block_Device.size() + 2;
Dees_Troy51127312012-09-08 13:08:49 -0400989 sprintf(command, "df %s > /tmp/dfoutput.txt", Mount_Point.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +0200990 TWFunc::Exec_Cmd(command);
Dees_Troy51127312012-09-08 13:08:49 -0400991 fp = fopen("/tmp/dfoutput.txt", "rt");
992 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000993 LOGINFO("Unable to open /tmp/dfoutput.txt.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400994 return false;
Dees_Troy51127312012-09-08 13:08:49 -0400995 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400996
997 while (fgets(line, sizeof(line), fp) != NULL)
998 {
999 unsigned long blocks, used, available;
1000 char device[64];
1001 char tmpString[64];
1002
1003 if (strncmp(line, "Filesystem", 10) == 0)
1004 continue;
1005 if (strlen(line) < min_len) {
1006 include_block = 0;
1007 continue;
1008 }
1009 if (include_block) {
1010 sscanf(line, "%s %lu %lu %lu", device, &blocks, &used, &available);
1011 } else {
1012 // The device block string is so long that the df information is on the next line
1013 int space_count = 0;
Dees_Troye58d5262012-09-21 12:27:57 -04001014 sprintf(tmpString, "/dev/block/%s", Actual_Block_Device.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001015 while (tmpString[space_count] == 32)
1016 space_count++;
1017 sscanf(line + space_count, "%lu %lu %lu", &blocks, &used, &available);
1018 }
1019
1020 // Adjust block size to byte size
1021 Size = blocks * 1024ULL;
1022 Used = used * 1024ULL;
1023 Free = available * 1024ULL;
1024 Backup_Size = Used;
1025 }
1026 fclose(fp);
1027 return true;
1028}
1029
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001030unsigned long long TWPartition::IOCTL_Get_Block_Size() {
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001031 Find_Actual_Block_Device();
Ethan Yonker483e9f42016-01-11 22:21:18 -06001032
1033 return TWFunc::IOCTL_Get_Block_Size(Actual_Block_Device.c_str());
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001034}
1035
Dees_Troy5bf43922012-09-07 16:07:55 -04001036bool TWPartition::Find_Partition_Size(void) {
1037 FILE* fp;
1038 char line[512];
1039 string tmpdevice;
1040
igoriok87e3d932013-01-31 21:03:53 +02001041 fp = fopen("/proc/dumchar_info", "rt");
1042 if (fp != NULL) {
1043 while (fgets(line, sizeof(line), fp) != NULL)
1044 {
1045 char label[32], device[32];
1046 unsigned long size = 0;
1047
thatd43bf2d2014-09-21 23:13:02 +02001048 sscanf(line, "%s %lx %*x %*u %s", label, &size, device);
igoriok87e3d932013-01-31 21:03:53 +02001049
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05001050 // Skip header, annotation and blank lines
igoriok87e3d932013-01-31 21:03:53 +02001051 if ((strncmp(device, "/dev/", 5) != 0) || (strlen(line) < 8))
1052 continue;
1053
1054 tmpdevice = "/dev/";
1055 tmpdevice += label;
1056 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
1057 Size = size;
1058 fclose(fp);
1059 return true;
1060 }
1061 }
1062 }
1063
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001064 unsigned long long ioctl_size = IOCTL_Get_Block_Size();
1065 if (ioctl_size) {
1066 Size = ioctl_size;
1067 return true;
1068 }
1069
Dees_Troy5bf43922012-09-07 16:07:55 -04001070 // In this case, we'll first get the partitions we care about (with labels)
1071 fp = fopen("/proc/partitions", "rt");
1072 if (fp == NULL)
1073 return false;
1074
1075 while (fgets(line, sizeof(line), fp) != NULL)
1076 {
1077 unsigned long major, minor, blocks;
1078 char device[512];
1079 char tmpString[64];
1080
Dees_Troy63c8df72012-09-10 14:02:05 -04001081 if (strlen(line) < 7 || line[0] == 'm') continue;
Dees_Troy5bf43922012-09-07 16:07:55 -04001082 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
1083
1084 tmpdevice = "/dev/block/";
1085 tmpdevice += device;
Dees_Troy38bd7602012-09-14 13:33:53 -04001086 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001087 // Adjust block size to byte size
1088 Size = blocks * 1024ULL;
1089 fclose(fp);
1090 return true;
1091 }
1092 }
1093 fclose(fp);
1094 return false;
1095}
1096
Dees_Troy5bf43922012-09-07 16:07:55 -04001097bool TWPartition::Is_Mounted(void) {
1098 if (!Can_Be_Mounted)
1099 return false;
1100
1101 struct stat st1, st2;
1102 string test_path;
1103
1104 // Check to see if the mount point directory exists
1105 test_path = Mount_Point + "/.";
1106 if (stat(test_path.c_str(), &st1) != 0) return false;
1107
1108 // Check to see if the directory above the mount point exists
1109 test_path = Mount_Point + "/../.";
1110 if (stat(test_path.c_str(), &st2) != 0) return false;
1111
1112 // Compare the device IDs -- if they match then we're (probably) using tmpfs instead of an actual device
1113 int ret = (st1.st_dev != st2.st_dev) ? true : false;
1114
1115 return ret;
1116}
1117
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05001118bool TWPartition::Is_File_System_Writable(void) {
1119 if (!Is_File_System(Current_File_System) || !Is_Mounted())
1120 return false;
1121
1122 string test_path = Mount_Point + "/.";
1123 return (access(test_path.c_str(), W_OK) == 0);
1124}
1125
Dees_Troy5bf43922012-09-07 16:07:55 -04001126bool TWPartition::Mount(bool Display_Error) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001127 int exfat_mounted = 0;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001128 unsigned long flags = Mount_Flags;
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001129
Dees_Troy5bf43922012-09-07 16:07:55 -04001130 if (Is_Mounted()) {
1131 return true;
1132 } else if (!Can_Be_Mounted) {
1133 return false;
1134 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001135
1136 Find_Actual_Block_Device();
1137
1138 // Check the current file system before mounting
1139 Check_FS_Type();
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001140 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/exfat-fuse")) {
Dees_Troye34c1332013-02-06 19:13:00 +00001141 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 +00001142 LOGINFO("cmd: %s\n", cmd.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001143 string result;
1144 if (TWFunc::Exec_Cmd(cmd, result) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001145 LOGINFO("exfat-fuse failed to mount with result '%s', trying vfat\n", result.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001146 Current_File_System = "vfat";
1147 } else {
1148#ifdef TW_NO_EXFAT_FUSE
1149 UnMount(false);
1150 // We'll let the kernel handle it but using exfat-fuse to detect if the file system is actually exfat
1151 // Some kernels let us mount vfat as exfat which doesn't work out too well
1152#else
1153 exfat_mounted = 1;
1154#endif
1155 }
1156 }
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001157
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001158 if (Current_File_System == "ntfs" && (TWFunc::Path_Exists("/sbin/ntfs-3g") || TWFunc::Path_Exists("/sbin/mount.ntfs"))) {
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001159 string cmd;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001160 string Ntfsmount_Binary = "";
1161
1162 if (TWFunc::Path_Exists("/sbin/ntfs-3g"))
1163 Ntfsmount_Binary = "ntfs-3g";
1164 else if (TWFunc::Path_Exists("/sbin/mount.ntfs"))
1165 Ntfsmount_Binary = "mount.ntfs";
1166
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001167 if (Mount_Read_Only)
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001168 cmd = "/sbin/" + Ntfsmount_Binary + " -o ro " + Actual_Block_Device + " " + Mount_Point;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001169 else
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001170 cmd = "/sbin/" + Ntfsmount_Binary + " " + Actual_Block_Device + " " + Mount_Point;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001171 LOGINFO("cmd: '%s'\n", cmd.c_str());
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001172
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001173 if (TWFunc::Exec_Cmd(cmd) == 0) {
1174 return true;
1175 } else {
1176 LOGINFO("ntfs-3g failed to mount, trying regular mount method.\n");
1177 }
1178 }
1179
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001180 if (Mount_Read_Only)
1181 flags |= MS_RDONLY;
1182
Dees_Troy22042032012-12-18 21:23:08 +00001183 if (Fstab_File_System == "yaffs2") {
1184 // mount an MTD partition as a YAFFS2 filesystem.
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001185 flags = MS_NOATIME | MS_NODEV | MS_NODIRATIME;
1186 if (Mount_Read_Only)
1187 flags |= MS_RDONLY;
Dees_Troy76543db2013-06-19 16:24:30 +00001188 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags, NULL) < 0) {
1189 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags | MS_RDONLY, NULL) < 0) {
1190 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001191 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy76543db2013-06-19 16:24:30 +00001192 else
1193 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
1194 return false;
1195 } else {
1196 LOGINFO("Mounted '%s' (MTD) as RO\n", Mount_Point.c_str());
1197 return true;
1198 }
1199 } else {
1200 struct stat st;
1201 string test_path = Mount_Point;
1202 if (stat(test_path.c_str(), &st) < 0) {
1203 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001204 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy76543db2013-06-19 16:24:30 +00001205 else
1206 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
1207 return false;
1208 }
1209 mode_t new_mode = st.st_mode | S_IXUSR | S_IXGRP | S_IXOTH;
1210 if (new_mode != st.st_mode) {
1211 LOGINFO("Fixing execute permissions for %s\n", Mount_Point.c_str());
1212 if (chmod(Mount_Point.c_str(), new_mode) < 0) {
1213 if (Display_Error)
1214 LOGERR("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
1215 else
1216 LOGINFO("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
1217 return false;
1218 }
1219 }
Dees_Troy22042032012-12-18 21:23:08 +00001220 return true;
Dees_Troy76543db2013-06-19 16:24:30 +00001221 }
thatc7572eb2015-03-31 18:55:30 +02001222 }
1223
1224 string mount_fs = Current_File_System;
1225 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sys/module/texfat"))
1226 mount_fs = "texfat";
1227
1228 if (!exfat_mounted &&
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001229 mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), mount_fs.c_str(), flags, Mount_Options.c_str()) != 0 &&
1230 mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), mount_fs.c_str(), flags, NULL) != 0) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001231#ifdef TW_NO_EXFAT_FUSE
1232 if (Current_File_System == "exfat") {
Dees_Troy2673cec2013-04-02 20:22:16 +00001233 LOGINFO("Mounting exfat failed, trying vfat...\n");
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001234 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), "vfat", 0, NULL) != 0) {
Dees_Troy85f44ed2013-01-09 18:42:36 +00001235 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001236 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy85f44ed2013-01-09 18:42:36 +00001237 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001238 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001239 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 +00001240 return false;
Dees_Troy85f44ed2013-01-09 18:42:36 +00001241 }
Dees_Troyb05ddee2013-01-28 20:24:50 +00001242 } else {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001243#endif
bigbiff bigbiff26774a02014-03-29 18:22:00 -04001244 if (!Removable && Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001245 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001246 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001247 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
1248 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 +00001249 return false;
1250#ifdef TW_NO_EXFAT_FUSE
Dees_Troy85f44ed2013-01-09 18:42:36 +00001251 }
1252#endif
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001253 }
Ethan Yonker253368a2014-11-25 15:00:52 -06001254
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001255 if (Removable)
1256 Update_Size(Display_Error);
1257
xiaolu9416f4f2015-06-04 08:22:23 +08001258 if (!Symlink_Mount_Point.empty() && TWFunc::Path_Exists(Symlink_Path)) {
codelover2a3d4ce2015-03-14 20:26:49 +08001259 string Command = "mount -o bind '" + Symlink_Path + "' '" + Symlink_Mount_Point + "'";
Vojtech Bocek05534202013-09-11 08:11:56 +02001260 TWFunc::Exec_Cmd(Command);
Dees_Troy5bf43922012-09-07 16:07:55 -04001261 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001262 return true;
1263}
1264
1265bool TWPartition::UnMount(bool Display_Error) {
1266 if (Is_Mounted()) {
1267 int never_unmount_system;
1268
1269 DataManager::GetValue(TW_DONT_UNMOUNT_SYSTEM, never_unmount_system);
1270 if (never_unmount_system == 1 && Mount_Point == "/system")
1271 return true; // Never unmount system if you're not supposed to unmount it
1272
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001273 if (Is_Storage)
Ethan Yonker726a0202014-12-16 20:01:38 -06001274 PartitionManager.Remove_MTP_Storage(MTP_Storage_ID);
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001275
Dees_Troy38bd7602012-09-14 13:33:53 -04001276 if (!Symlink_Mount_Point.empty())
1277 umount(Symlink_Mount_Point.c_str());
1278
Dees_Troyb05ddee2013-01-28 20:24:50 +00001279 umount(Mount_Point.c_str());
1280 if (Is_Mounted()) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001281 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001282 gui_msg(Msg(msg::kError, "fail_unmount=Failed to unmount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy5bf43922012-09-07 16:07:55 -04001283 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001284 LOGINFO("Unable to unmount '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001285 return false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001286 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -04001287 return true;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001288 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001289 } else {
1290 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001291 }
1292}
1293
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05001294bool TWPartition::ReMount(bool Display_Error) {
1295 if (UnMount(Display_Error))
1296 return Mount(Display_Error);
1297 return false;
1298}
1299
1300bool TWPartition::ReMount_RW(bool Display_Error) {
1301 // No need to remount if already mounted rw
1302 if (Is_File_System_Writable())
1303 return true;
1304
1305 bool ro = Mount_Read_Only;
1306 int flags = Mount_Flags;
1307
1308 Mount_Read_Only = false;
1309 Mount_Flags &= ~MS_RDONLY;
1310
1311 bool ret = ReMount(Display_Error);
1312
1313 Mount_Read_Only = ro;
1314 Mount_Flags = flags;
1315
1316 return ret;
1317}
1318
Gary Peck43acadf2012-11-21 21:19:01 -08001319bool TWPartition::Wipe(string New_File_System) {
Ethan Yonker726a0202014-12-16 20:01:38 -06001320 bool wiped = false, update_crypt = false, recreate_media = true;
Dees_Troy16c2b312013-01-15 16:51:18 +00001321 int check;
1322 string Layout_Filename = Mount_Point + "/.layout_version";
1323
Dees_Troy38bd7602012-09-14 13:33:53 -04001324 if (!Can_Be_Wiped) {
Matt Mower3c366972015-12-25 19:28:31 -06001325 gui_msg(Msg(msg::kError, "cannot_wipe=Partition {1} cannot be wiped.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04001326 return false;
1327 }
1328
Dees_Troyc51f1f92012-09-20 15:32:13 -04001329 if (Mount_Point == "/cache")
Dees_Troy2673cec2013-04-02 20:22:16 +00001330 Log_Offset = 0;
Dees_Troyc51f1f92012-09-20 15:32:13 -04001331
Dees_Troy16c2b312013-01-15 16:51:18 +00001332 if (Retain_Layout_Version && Mount(false) && TWFunc::Path_Exists(Layout_Filename))
1333 TWFunc::copy_file(Layout_Filename, "/.layout_version", 0600);
1334 else
1335 unlink("/.layout_version");
Dees_Troy38bd7602012-09-14 13:33:53 -04001336
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001337 if (Has_Data_Media && Current_File_System == New_File_System) {
Dees_Troy16c2b312013-01-15 16:51:18 +00001338 wiped = Wipe_Data_Without_Wiping_Media();
Ethan Yonker5eac2222014-06-11 12:22:55 -05001339 recreate_media = false;
Dees_Troy16c2b312013-01-15 16:51:18 +00001340 } else {
Dees_Troy16c2b312013-01-15 16:51:18 +00001341 DataManager::GetValue(TW_RM_RF_VAR, check);
1342
Hashcodedabfd492013-08-29 22:45:30 -07001343 if (check || Use_Rm_Rf)
Dees_Troy16c2b312013-01-15 16:51:18 +00001344 wiped = Wipe_RMRF();
1345 else if (New_File_System == "ext4")
1346 wiped = Wipe_EXT4();
1347 else if (New_File_System == "ext2" || New_File_System == "ext3")
1348 wiped = Wipe_EXT23(New_File_System);
1349 else if (New_File_System == "vfat")
1350 wiped = Wipe_FAT();
1351 else if (New_File_System == "exfat")
1352 wiped = Wipe_EXFAT();
1353 else if (New_File_System == "yaffs2")
1354 wiped = Wipe_MTD();
Dees_Troye5017042013-08-29 16:38:55 +00001355 else if (New_File_System == "f2fs")
1356 wiped = Wipe_F2FS();
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001357 else if (New_File_System == "ntfs")
1358 wiped = Wipe_NTFS();
Dees_Troy16c2b312013-01-15 16:51:18 +00001359 else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001360 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 +00001361 unlink("/.layout_version");
1362 return false;
1363 }
1364 update_crypt = wiped;
Gary Pecke8bc5d72012-12-21 06:45:25 -08001365 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001366
Gary Pecke8bc5d72012-12-21 06:45:25 -08001367 if (wiped) {
Dees_Troy1c1ac442013-01-17 21:42:14 +00001368 if (Mount_Point == "/cache")
1369 DataManager::Output_Version();
1370
Dees_Troy16c2b312013-01-15 16:51:18 +00001371 if (TWFunc::Path_Exists("/.layout_version") && Mount(false))
1372 TWFunc::copy_file("/.layout_version", Layout_Filename, 0600);
1373
1374 if (update_crypt) {
1375 Setup_File_System(false);
1376 if (Is_Encrypted && !Is_Decrypted) {
1377 // just wiped an encrypted partition back to its unencrypted state
1378 Is_Encrypted = false;
1379 Is_Decrypted = false;
1380 Decrypted_Block_Device = "";
1381 if (Mount_Point == "/data") {
1382 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1383 DataManager::SetValue(TW_IS_DECRYPTED, 0);
1384 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001385 }
1386 }
Ethan Yonker5eac2222014-06-11 12:22:55 -05001387
Ethan Yonker66a19492015-12-10 10:19:45 -06001388 if (Has_Data_Media && recreate_media) {
Ethan Yonker5eac2222014-06-11 12:22:55 -05001389 Recreate_Media_Folder();
1390 }
Matt Mower209c9632016-01-20 12:08:35 -06001391 if (Is_Storage && Mount(false))
1392 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Gary Pecke8bc5d72012-12-21 06:45:25 -08001393 }
Matt Mower209c9632016-01-20 12:08:35 -06001394
Gary Pecke8bc5d72012-12-21 06:45:25 -08001395 return wiped;
Dees_Troy51a0e822012-09-05 15:24:24 -04001396}
1397
Gary Peck43acadf2012-11-21 21:19:01 -08001398bool TWPartition::Wipe() {
Gary Peck82599a82012-11-21 16:23:12 -08001399 if (Is_File_System(Current_File_System))
1400 return Wipe(Current_File_System);
1401 else
1402 return Wipe(Fstab_File_System);
Gary Peck43acadf2012-11-21 21:19:01 -08001403}
1404
Dees_Troye58d5262012-09-21 12:27:57 -04001405bool TWPartition::Wipe_AndSec(void) {
1406 if (!Has_Android_Secure)
1407 return false;
1408
Dees_Troye58d5262012-09-21 12:27:57 -04001409 if (!Mount(true))
1410 return false;
1411
Ethan Yonker74db1572015-10-28 12:44:49 -05001412 gui_msg(Msg("wiping=Wiping {1}")(Backup_Display_Name));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001413 TWFunc::removeDir(Mount_Point + "/.android_secure/", true);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001414 return true;
Dees_Troye58d5262012-09-21 12:27:57 -04001415}
1416
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001417bool TWPartition::Can_Repair() {
Ethan Yonkera2719152015-05-28 09:44:41 -05001418 if (Mount_Read_Only)
1419 return false;
Matt Mower18794c82015-11-11 16:22:45 -06001420 if (Current_File_System == "vfat" && TWFunc::Path_Exists("/sbin/fsck.fat"))
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001421 return true;
1422 else if ((Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") && TWFunc::Path_Exists("/sbin/e2fsck"))
1423 return true;
1424 else if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/fsck.exfat"))
1425 return true;
1426 else if (Current_File_System == "f2fs" && TWFunc::Path_Exists("/sbin/fsck.f2fs"))
1427 return true;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001428 else if (Current_File_System == "ntfs" && (TWFunc::Path_Exists("/sbin/ntfsfix") || TWFunc::Path_Exists("/sbin/fsck.ntfs")))
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001429 return true;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001430 return false;
1431}
1432
1433bool TWPartition::Repair() {
1434 string command;
1435
1436 if (Current_File_System == "vfat") {
Matt Mower18794c82015-11-11 16:22:45 -06001437 if (!TWFunc::Path_Exists("/sbin/fsck.fat")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001438 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.fat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001439 return false;
1440 }
1441 if (!UnMount(true))
1442 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001443 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.fat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001444 Find_Actual_Block_Device();
Matt Mower18794c82015-11-11 16:22:45 -06001445 command = "/sbin/fsck.fat -y " + Actual_Block_Device;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001446 LOGINFO("Repair command: %s\n", command.c_str());
1447 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001448 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001449 return true;
1450 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001451 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001452 return false;
1453 }
1454 }
1455 if (Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") {
1456 if (!TWFunc::Path_Exists("/sbin/e2fsck")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001457 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("e2fsck"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001458 return false;
1459 }
1460 if (!UnMount(true))
1461 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001462 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("e2fsck"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001463 Find_Actual_Block_Device();
Ethan Yonkera2719152015-05-28 09:44:41 -05001464 command = "/sbin/e2fsck -fp " + Actual_Block_Device;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001465 LOGINFO("Repair command: %s\n", command.c_str());
1466 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001467 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001468 return true;
1469 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001470 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001471 return false;
1472 }
1473 }
1474 if (Current_File_System == "exfat") {
1475 if (!TWFunc::Path_Exists("/sbin/fsck.exfat")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001476 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.exfat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001477 return false;
1478 }
1479 if (!UnMount(true))
1480 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001481 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.exfat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001482 Find_Actual_Block_Device();
1483 command = "/sbin/fsck.exfat " + Actual_Block_Device;
1484 LOGINFO("Repair command: %s\n", command.c_str());
1485 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001486 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001487 return true;
1488 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001489 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001490 return false;
1491 }
1492 }
1493 if (Current_File_System == "f2fs") {
1494 if (!TWFunc::Path_Exists("/sbin/fsck.f2fs")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001495 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.f2fs"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001496 return false;
1497 }
1498 if (!UnMount(true))
1499 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001500 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.f2fs"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001501 Find_Actual_Block_Device();
1502 command = "/sbin/fsck.f2fs " + Actual_Block_Device;
1503 LOGINFO("Repair command: %s\n", command.c_str());
1504 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001505 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001506 return true;
1507 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001508 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001509 return false;
1510 }
1511 }
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001512 if (Current_File_System == "ntfs") {
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001513 string Ntfsfix_Binary;
1514 if (TWFunc::Path_Exists("/sbin/ntfsfix"))
1515 Ntfsfix_Binary = "ntfsfix";
1516 else if (TWFunc::Path_Exists("/sbin/fsck.ntfs"))
1517 Ntfsfix_Binary = "fsck.ntfs";
1518 else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001519 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("ntfsfix"));
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001520 return false;
1521 }
1522 if (!UnMount(true))
1523 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001524 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)(Ntfsfix_Binary));
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001525 Find_Actual_Block_Device();
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001526 command = "/sbin/" + Ntfsfix_Binary + " " + Actual_Block_Device;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001527 LOGINFO("Repair command: %s\n", command.c_str());
1528 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001529 gui_msg("done=Done.");
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001530 return true;
1531 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001532 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001533 return false;
1534 }
1535 }
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001536 return false;
1537}
1538
Ethan Yonkera2719152015-05-28 09:44:41 -05001539bool TWPartition::Can_Resize() {
1540 if (Mount_Read_Only)
1541 return false;
1542 if ((Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") && TWFunc::Path_Exists("/sbin/resize2fs"))
1543 return true;
1544 return false;
1545}
1546
1547bool TWPartition::Resize() {
1548 string command;
1549
1550 if (Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") {
1551 if (!Can_Repair()) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001552 LOGINFO("Cannot resize %s because %s cannot be repaired before resizing.\n", Display_Name.c_str(), Display_Name.c_str());
1553 gui_msg(Msg(msg::kError, "cannot_resize=Cannot resize {1}.")(Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001554 return false;
1555 }
1556 if (!TWFunc::Path_Exists("/sbin/resize2fs")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001557 LOGINFO("resize2fs does not exist! Cannot resize!\n");
1558 gui_msg(Msg(msg::kError, "cannot_resize=Cannot resize {1}.")(Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001559 return false;
1560 }
1561 // Repair will unmount so no need to do it twice
Ethan Yonker74db1572015-10-28 12:44:49 -05001562 gui_msg(Msg("repair_resize=Repairing {1} before resizing.")( Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001563 if (!Repair())
1564 return false;
Ethan Yonker74db1572015-10-28 12:44:49 -05001565 gui_msg(Msg("resizing=Resizing {1} using {2}...")(Display_Name)("resize2fs"));
Ethan Yonkera2719152015-05-28 09:44:41 -05001566 Find_Actual_Block_Device();
1567 command = "/sbin/resize2fs " + Actual_Block_Device;
1568 if (Length != 0) {
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001569 unsigned long long Actual_Size = IOCTL_Get_Block_Size();
1570 if (Actual_Size == 0)
1571 return false;
Ethan Yonkera2719152015-05-28 09:44:41 -05001572
Ethan Yonkera2719152015-05-28 09:44:41 -05001573 unsigned long long Block_Count;
1574 if (Length < 0) {
1575 // Reduce overall size by this length
1576 Block_Count = (Actual_Size / 1024LLU) - ((unsigned long long)(Length * -1) / 1024LLU);
1577 } else {
1578 // This is the size, not a size reduction
1579 Block_Count = ((unsigned long long)(Length) / 1024LLU);
1580 }
1581 char temp[256];
1582 sprintf(temp, "%llu", Block_Count);
1583 command += " ";
1584 command += temp;
1585 command += "K";
1586 }
1587 LOGINFO("Resize command: %s\n", command.c_str());
1588 if (TWFunc::Exec_Cmd(command) == 0) {
1589 Update_Size(true);
Ethan Yonker74db1572015-10-28 12:44:49 -05001590 gui_msg("done=Done.");
Ethan Yonkera2719152015-05-28 09:44:41 -05001591 return true;
1592 } else {
1593 Update_Size(true);
Ethan Yonker74db1572015-10-28 12:44:49 -05001594 gui_msg(Msg(msg::kError, "unable_resize=Unable to resize {1}.")(Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001595 return false;
1596 }
1597 }
1598 return false;
1599}
1600
Ethan Yonker472f5062016-02-25 13:47:30 -06001601bool TWPartition::Backup(const string& backup_folder, pid_t &tar_fork_pid, ProgressTracking *progress) {
bigbiff7abc5fe2015-01-17 16:53:12 -05001602 if (Backup_Method == FILES) {
Ethan Yonker472f5062016-02-25 13:47:30 -06001603 return Backup_Tar(backup_folder, progress, tar_fork_pid);
bigbiff7abc5fe2015-01-17 16:53:12 -05001604 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001605 else if (Backup_Method == DD)
Ethan Yonker472f5062016-02-25 13:47:30 -06001606 return Backup_Image(backup_folder, progress);
Dees_Troy38bd7602012-09-14 13:33:53 -04001607 else if (Backup_Method == FLASH_UTILS)
Ethan Yonker472f5062016-02-25 13:47:30 -06001608 return Backup_Dump_Image(backup_folder, progress);
Dees_Troy2673cec2013-04-02 20:22:16 +00001609 LOGERR("Unknown backup method for '%s'\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001610 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001611}
1612
Dees_Troy43d8b002012-09-17 16:00:01 -04001613bool TWPartition::Check_MD5(string restore_folder) {
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001614 string Full_Filename, md5file;
Dees_Troy43d8b002012-09-17 16:00:01 -04001615 char split_filename[512];
1616 int index = 0;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001617 twrpDigest md5sum;
Dees_Troy43d8b002012-09-17 16:00:01 -04001618
Captain Throwbackff5935f2014-09-23 16:08:34 -04001619 sync();
1620
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001621 memset(split_filename, 0, sizeof(split_filename));
Dees_Troy43d8b002012-09-17 16:00:01 -04001622 Full_Filename = restore_folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04001623 if (!TWFunc::Path_Exists(Full_Filename)) {
1624 // This is a split archive, we presume
1625 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
Dees_Troy2673cec2013-04-02 20:22:16 +00001626 LOGINFO("split_filename: %s\n", split_filename);
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001627 md5file = split_filename;
1628 md5file += ".md5";
1629 if (!TWFunc::Path_Exists(md5file)) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001630 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 -04001631 return false;
1632 }
1633 md5sum.setfn(split_filename);
Dees_Troy83bd4832013-05-04 12:39:56 +00001634 while (index < 1000) {
1635 if (TWFunc::Path_Exists(split_filename) && md5sum.verify_md5digest() != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001636 gui_msg(Msg(msg::kError, "md5_fail_match=MD5 failed to match on '{1}'.")(split_filename));
Dees_Troy43d8b002012-09-17 16:00:01 -04001637 return false;
1638 }
1639 index++;
1640 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001641 md5sum.setfn(split_filename);
Dees_Troy43d8b002012-09-17 16:00:01 -04001642 }
Dees_Troy4a2a1262012-09-18 09:33:47 -04001643 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001644 } else {
1645 // Single file archive
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001646 md5file = Full_Filename + ".md5";
1647 if (!TWFunc::Path_Exists(md5file)) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001648 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 -04001649 return false;
1650 }
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001651 md5sum.setfn(Full_Filename);
1652 if (md5sum.verify_md5digest() != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001653 gui_msg(Msg(msg::kError, "md5_fail_match=MD5 failed to match on '{1}'.")(split_filename));
Dees_Troy43d8b002012-09-17 16:00:01 -04001654 return false;
1655 } else
1656 return true;
1657 }
1658 return false;
1659}
1660
Ethan Yonker472f5062016-02-25 13:47:30 -06001661bool TWPartition::Restore(const string& restore_folder, ProgressTracking *progress) {
Gary Peck43acadf2012-11-21 21:19:01 -08001662 string Restore_File_System;
1663
Matt Mower3c366972015-12-25 19:28:31 -06001664 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, gui_parse_text("{@restoring_hdr}"));
Dees_Troy2673cec2013-04-02 20:22:16 +00001665 LOGINFO("Restore filename is: %s\n", Backup_FileName.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001666
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001667 Restore_File_System = Get_Restore_File_System(restore_folder);
1668
1669 if (Is_File_System(Restore_File_System))
Ethan Yonker472f5062016-02-25 13:47:30 -06001670 return Restore_Tar(restore_folder, Restore_File_System, progress);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001671 else if (Is_Image(Restore_File_System)) {
Ethan Yonker472f5062016-02-25 13:47:30 -06001672 return Restore_Image(restore_folder, Restore_File_System, progress);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001673 }
1674
1675 LOGERR("Unknown restore method for '%s'\n", Mount_Point.c_str());
1676 return false;
1677}
1678
Ethan Yonker472f5062016-02-25 13:47:30 -06001679string TWPartition::Get_Restore_File_System(const string& restore_folder) {
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001680 size_t first_period, second_period;
1681 string Restore_File_System;
1682
Gary Peck43acadf2012-11-21 21:19:01 -08001683 // Parse backup filename to extract the file system before wiping
1684 first_period = Backup_FileName.find(".");
1685 if (first_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001686 LOGERR("Unable to find file system (first period).\n");
thatd43bf2d2014-09-21 23:13:02 +02001687 return string();
Gary Peck43acadf2012-11-21 21:19:01 -08001688 }
1689 Restore_File_System = Backup_FileName.substr(first_period + 1, Backup_FileName.size() - first_period - 1);
1690 second_period = Restore_File_System.find(".");
1691 if (second_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001692 LOGERR("Unable to find file system (second period).\n");
thatd43bf2d2014-09-21 23:13:02 +02001693 return string();
Gary Peck43acadf2012-11-21 21:19:01 -08001694 }
1695 Restore_File_System.resize(second_period);
Dees_Troy2673cec2013-04-02 20:22:16 +00001696 LOGINFO("Restore file system is: '%s'.\n", Restore_File_System.c_str());
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001697 return Restore_File_System;
Dees_Troy51a0e822012-09-05 15:24:24 -04001698}
1699
1700string TWPartition::Backup_Method_By_Name() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001701 if (Backup_Method == NONE)
1702 return "none";
1703 else if (Backup_Method == FILES)
1704 return "files";
1705 else if (Backup_Method == DD)
1706 return "dd";
1707 else if (Backup_Method == FLASH_UTILS)
1708 return "flash_utils";
1709 else
1710 return "undefined";
1711 return "ERROR!";
Dees_Troy51a0e822012-09-05 15:24:24 -04001712}
1713
1714bool TWPartition::Decrypt(string Password) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001715 LOGINFO("STUB TWPartition::Decrypt, password: '%s'\n", Password.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001716 // Is this needed?
Dees_Troy51a0e822012-09-05 15:24:24 -04001717 return 1;
1718}
1719
1720bool TWPartition::Wipe_Encryption() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001721 bool Save_Data_Media = Has_Data_Media;
1722
1723 if (!UnMount(true))
1724 return false;
1725
Dees_Troy38bd7602012-09-14 13:33:53 -04001726 Has_Data_Media = false;
Dees_Troy74fb2e92013-04-15 14:35:47 +00001727 Decrypted_Block_Device = "";
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001728#ifdef TW_INCLUDE_CRYPTO
1729 if (Is_Decrypted) {
1730 if (!UnMount(true))
1731 return false;
Matt Mower2b18a532015-02-20 16:58:05 -06001732 if (delete_crypto_blk_dev((char*)("userdata")) != 0) {
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001733 LOGERR("Error deleting crypto block device, continuing anyway.\n");
1734 }
1735 }
1736#endif
Dees_Troy74fb2e92013-04-15 14:35:47 +00001737 Is_Decrypted = false;
1738 Is_Encrypted = false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001739 Find_Actual_Block_Device();
Ethan Yonkerbc85b632015-08-09 12:48:14 -05001740 if (Crypto_Key_Location == "footer") {
1741 int newlen, fd;
1742 if (Length != 0) {
1743 newlen = Length;
1744 if (newlen < 0)
1745 newlen = newlen * -1;
1746 } else {
1747 newlen = CRYPT_FOOTER_OFFSET;
1748 }
1749 if ((fd = open(Actual_Block_Device.c_str(), O_RDWR)) < 0) {
1750 gui_print_color("warning", "Unable to open '%s' to wipe crypto key\n", Actual_Block_Device.c_str());
1751 } else {
1752 unsigned int block_count;
1753 if ((ioctl(fd, BLKGETSIZE, &block_count)) == -1) {
1754 gui_print_color("warning", "Unable to get block size for wiping crypto footer.\n");
1755 } else {
1756 off64_t offset = ((off64_t)block_count * 512) - newlen;
1757 if (lseek64(fd, offset, SEEK_SET) == -1) {
1758 gui_print_color("warning", "Unable to lseek64 for wiping crypto footer.\n");
1759 } else {
1760 void* buffer = malloc(newlen);
1761 if (!buffer) {
1762 gui_print_color("warning", "Failed to malloc for wiping crypto footer.\n");
1763 } else {
1764 memset(buffer, 0, newlen);
1765 int ret = write(fd, buffer, newlen);
1766 if (ret != newlen) {
1767 gui_print_color("warning", "Failed to wipe crypto footer.\n");
1768 } else {
1769 LOGINFO("Successfully wiped crypto footer.\n");
1770 }
Matt Mower13a8f0b2015-09-26 15:40:03 -05001771 free(buffer);
Ethan Yonkerbc85b632015-08-09 12:48:14 -05001772 }
1773 }
1774 }
1775 close(fd);
1776 }
1777 } else {
Ethan Yonkerc2dafbb2016-03-15 22:20:59 -05001778 if (TWFunc::IOCTL_Get_Block_Size(Crypto_Key_Location.c_str()) >= 16384LLU) {
1779 string Command = "dd of='" + Crypto_Key_Location + "' if=/dev/zero bs=16384 count=1";
1780 TWFunc::Exec_Cmd(Command);
1781 } else {
1782 LOGINFO("Crypto key location reports size < 16K so not wiping crypto footer.\n");
1783 }
Ethan Yonkerbc85b632015-08-09 12:48:14 -05001784 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001785 if (Wipe(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001786 Has_Data_Media = Save_Data_Media;
1787 if (Has_Data_Media && !Symlink_Mount_Point.empty()) {
1788 Recreate_Media_Folder();
Ethan Yonker726a0202014-12-16 20:01:38 -06001789 if (Mount(false))
1790 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04001791 }
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001792 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
Ethan Yonker83e82572014-04-04 10:59:28 -05001793#ifndef TW_OEM_BUILD
Ethan Yonker74db1572015-10-28 12:44:49 -05001794 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 -05001795#endif
Dees_Troy38bd7602012-09-14 13:33:53 -04001796 return true;
1797 } else {
1798 Has_Data_Media = Save_Data_Media;
Ethan Yonker74db1572015-10-28 12:44:49 -05001799 gui_err("format_data_err=Unable to format to remove encryption.");
Ethan Yonker726a0202014-12-16 20:01:38 -06001800 if (Has_Data_Media && Mount(false))
1801 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04001802 return false;
1803 }
1804 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001805}
1806
1807void TWPartition::Check_FS_Type() {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001808 const char* type;
1809 blkid_probe pr;
Dees_Troy5bf43922012-09-07 16:07:55 -04001810
Dees_Troy68cab492012-12-12 19:29:35 +00001811 if (Fstab_File_System == "yaffs2" || Fstab_File_System == "mtd" || Fstab_File_System == "bml" || Ignore_Blkid)
1812 return; // Running blkid on some mtd devices causes a massive crash or needs to be skipped
Dees_Troy5bf43922012-09-07 16:07:55 -04001813
Dees_Troy38bd7602012-09-14 13:33:53 -04001814 Find_Actual_Block_Device();
Dees_Troy8170a922012-09-18 15:40:25 -04001815 if (!Is_Present)
1816 return;
Dees_Troy51127312012-09-08 13:08:49 -04001817
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001818 pr = blkid_new_probe_from_filename(Actual_Block_Device.c_str());
1819 if (blkid_do_fullprobe(pr)) {
1820 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00001821 LOGINFO("Can't probe device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001822 return;
Dees_Troy5bf43922012-09-07 16:07:55 -04001823 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001824
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05001825 if (blkid_probe_lookup_value(pr, "TYPE", &type, NULL) < 0) {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001826 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00001827 LOGINFO("can't find filesystem on device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001828 return;
1829 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001830
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001831 Current_File_System = type;
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001832 blkid_free_probe(pr);
Dees_Troy51a0e822012-09-05 15:24:24 -04001833}
1834
Gary Peck43acadf2012-11-21 21:19:01 -08001835bool TWPartition::Wipe_EXT23(string File_System) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001836 if (!UnMount(true))
1837 return false;
1838
Dees_Troy43d8b002012-09-17 16:00:01 -04001839 if (TWFunc::Path_Exists("/sbin/mke2fs")) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001840 string command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001841
Greg Wallace4b44fef2015-12-29 15:33:24 -05001842 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mke2fs"));
Dees_Troy38bd7602012-09-14 13:33:53 -04001843 Find_Actual_Block_Device();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001844 command = "mke2fs -t " + File_System + " -m 0 " + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001845 LOGINFO("mke2fs command: %s\n", command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001846 if (TWFunc::Exec_Cmd(command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001847 Current_File_System = File_System;
Dees_Troye58d5262012-09-21 12:27:57 -04001848 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05001849 gui_msg("done=Done.");
Dees_Troy38bd7602012-09-14 13:33:53 -04001850 return true;
1851 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001852 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04001853 return false;
1854 }
1855 } else
1856 return Wipe_RMRF();
1857
1858 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001859}
1860
1861bool TWPartition::Wipe_EXT4() {
Ethan Yonker25f20c12014-10-14 09:04:43 -05001862 Find_Actual_Block_Device();
1863 if (!Is_Present) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001864 LOGINFO("Block device not present, cannot wipe %s.\n", Display_Name.c_str());
1865 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Ethan Yonker25f20c12014-10-14 09:04:43 -05001866 return false;
1867 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001868 if (!UnMount(true))
1869 return false;
1870
Dees_Troyb3265ab2013-08-30 02:59:14 +00001871#if defined(HAVE_SELINUX) && defined(USE_EXT4)
Ethan Yonkerf27497f2014-02-09 11:48:33 -06001872 int ret;
1873 char *secontext = NULL;
1874
Greg Wallace4b44fef2015-12-29 15:33:24 -05001875 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("make_ext4fs"));
Ethan Yonkerf27497f2014-02-09 11:48:33 -06001876
Dees Troy99c8dbf2014-03-10 16:53:58 +00001877 if (!selinux_handle || selabel_lookup(selinux_handle, &secontext, Mount_Point.c_str(), S_IFDIR) < 0) {
Ethan Yonkerf27497f2014-02-09 11:48:33 -06001878 LOGINFO("Cannot lookup security context for '%s'\n", Mount_Point.c_str());
1879 ret = make_ext4fs(Actual_Block_Device.c_str(), Length, Mount_Point.c_str(), NULL);
1880 } else {
1881 ret = make_ext4fs(Actual_Block_Device.c_str(), Length, Mount_Point.c_str(), selinux_handle);
1882 }
1883 if (ret != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001884 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troya95f55c2013-08-17 13:14:43 +00001885 return false;
1886 } else {
bigbiff bigbiffc49d7062013-10-11 20:28:00 -04001887 string sedir = Mount_Point + "/lost+found";
1888 PartitionManager.Mount_By_Path(sedir.c_str(), true);
1889 rmdir(sedir.c_str());
1890 mkdir(sedir.c_str(), S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP);
Dees_Troya95f55c2013-08-17 13:14:43 +00001891 return true;
1892 }
1893#else
Dees_Troy43d8b002012-09-17 16:00:01 -04001894 if (TWFunc::Path_Exists("/sbin/make_ext4fs")) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001895 string Command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001896
Greg Wallace4b44fef2015-12-29 15:33:24 -05001897 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("make_ext4fs"));
Dees_Troy38bd7602012-09-14 13:33:53 -04001898 Find_Actual_Block_Device();
1899 Command = "make_ext4fs";
1900 if (!Is_Decrypted && Length != 0) {
1901 // Only use length if we're not decrypted
1902 char len[32];
1903 sprintf(len, "%i", Length);
1904 Command += " -l ";
1905 Command += len;
1906 }
Dees_Troy5295d582013-09-06 15:51:08 +00001907 if (TWFunc::Path_Exists("/file_contexts")) {
1908 Command += " -S /file_contexts";
1909 }
1910 Command += " -a " + Mount_Point + " " + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001911 LOGINFO("make_ext4fs command: %s\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001912 if (TWFunc::Exec_Cmd(Command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001913 Current_File_System = "ext4";
Dees_Troye58d5262012-09-21 12:27:57 -04001914 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05001915 gui_msg("done=Done.");
Dees_Troy38bd7602012-09-14 13:33:53 -04001916 return true;
1917 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001918 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04001919 return false;
1920 }
1921 } else
Gary Peck43acadf2012-11-21 21:19:01 -08001922 return Wipe_EXT23("ext4");
Dees_Troya95f55c2013-08-17 13:14:43 +00001923#endif
Dees_Troy38bd7602012-09-14 13:33:53 -04001924 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001925}
1926
1927bool TWPartition::Wipe_FAT() {
Vojtech Bocek05534202013-09-11 08:11:56 +02001928 string command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001929
Matt Mower18794c82015-11-11 16:22:45 -06001930 if (TWFunc::Path_Exists("/sbin/mkfs.fat")) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001931 if (!UnMount(true))
1932 return false;
1933
Greg Wallace4b44fef2015-12-29 15:33:24 -05001934 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mkfs.fat"));
Dees_Troy38bd7602012-09-14 13:33:53 -04001935 Find_Actual_Block_Device();
Matt Mower18794c82015-11-11 16:22:45 -06001936 command = "mkfs.fat " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02001937 if (TWFunc::Exec_Cmd(command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001938 Current_File_System = "vfat";
Dees_Troye58d5262012-09-21 12:27:57 -04001939 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05001940 gui_msg("done=Done.");
Dees_Troy38bd7602012-09-14 13:33:53 -04001941 return true;
1942 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001943 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04001944 return false;
1945 }
1946 return true;
1947 }
1948 else
1949 return Wipe_RMRF();
1950
1951 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001952}
1953
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001954bool TWPartition::Wipe_EXFAT() {
Vojtech Bocek05534202013-09-11 08:11:56 +02001955 string command;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001956
1957 if (TWFunc::Path_Exists("/sbin/mkexfatfs")) {
1958 if (!UnMount(true))
1959 return false;
1960
Greg Wallace4b44fef2015-12-29 15:33:24 -05001961 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mkexfatfs"));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001962 Find_Actual_Block_Device();
1963 command = "mkexfatfs " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02001964 if (TWFunc::Exec_Cmd(command) == 0) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001965 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05001966 gui_msg("done=Done.");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001967 return true;
1968 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001969 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001970 return false;
1971 }
1972 return true;
1973 }
1974 return false;
1975}
1976
Dees_Troy38bd7602012-09-14 13:33:53 -04001977bool TWPartition::Wipe_MTD() {
1978 if (!UnMount(true))
1979 return false;
1980
Greg Wallace4b44fef2015-12-29 15:33:24 -05001981 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("MTD"));
Dees_Troy38bd7602012-09-14 13:33:53 -04001982
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001983 mtd_scan_partitions();
1984 const MtdPartition* mtd = mtd_find_partition_by_name(MTD_Name.c_str());
1985 if (mtd == NULL) {
1986 LOGERR("No mtd partition named '%s'", MTD_Name.c_str());
1987 return false;
1988 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001989
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001990 MtdWriteContext* ctx = mtd_write_partition(mtd);
1991 if (ctx == NULL) {
1992 LOGERR("Can't write '%s', failed to format.", MTD_Name.c_str());
1993 return false;
1994 }
1995 if (mtd_erase_blocks(ctx, -1) == -1) {
1996 mtd_write_close(ctx);
1997 LOGERR("Failed to format '%s'", MTD_Name.c_str());
1998 return false;
1999 }
2000 if (mtd_write_close(ctx) != 0) {
2001 LOGERR("Failed to close '%s'", MTD_Name.c_str());
2002 return false;
2003 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08002004 Current_File_System = "yaffs2";
Dees_Troye58d5262012-09-21 12:27:57 -04002005 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05002006 gui_msg("done=Done.");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02002007 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002008}
2009
2010bool TWPartition::Wipe_RMRF() {
Dees_Troy38bd7602012-09-14 13:33:53 -04002011 if (!Mount(true))
2012 return false;
Ethan Yonker726a0202014-12-16 20:01:38 -06002013 // This is the only wipe that leaves the partition mounted, so we
2014 // must manually remove the partition from MTP if it is a storage
2015 // partition.
2016 if (Is_Storage)
2017 PartitionManager.Remove_MTP_Storage(MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04002018
Ethan Yonker74db1572015-10-28 12:44:49 -05002019 gui_msg(Msg("remove_all=Removing all files under '{1}'")(Mount_Point));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002020 TWFunc::removeDir(Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04002021 Recreate_AndSec_Folder();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002022 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002023}
2024
Dees_Troye5017042013-08-29 16:38:55 +00002025bool TWPartition::Wipe_F2FS() {
Vojtech Bocek05534202013-09-11 08:11:56 +02002026 string command;
Dees_Troye5017042013-08-29 16:38:55 +00002027
2028 if (TWFunc::Path_Exists("/sbin/mkfs.f2fs")) {
2029 if (!UnMount(true))
2030 return false;
2031
Greg Wallace4b44fef2015-12-29 15:33:24 -05002032 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mkfs.f2fs"));
Dees_Troye5017042013-08-29 16:38:55 +00002033 Find_Actual_Block_Device();
Greg Wallace558aef72016-01-19 21:14:30 -05002034 command = "mkfs.f2fs -t 0";
dhacker29a3fa75f2015-01-17 19:42:33 -05002035 if (!Is_Decrypted && Length != 0) {
2036 // Only use length if we're not decrypted
2037 char len[32];
2038 int mod_length = Length;
2039 if (Length < 0)
2040 mod_length *= -1;
2041 sprintf(len, "%i", mod_length);
2042 command += " -r ";
2043 command += len;
2044 }
2045 command += " " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02002046 if (TWFunc::Exec_Cmd(command) == 0) {
Dees_Troye5017042013-08-29 16:38:55 +00002047 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05002048 gui_msg("done=Done.");
Dees_Troye5017042013-08-29 16:38:55 +00002049 return true;
2050 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002051 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troye5017042013-08-29 16:38:55 +00002052 return false;
2053 }
2054 return true;
2055 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002056 LOGINFO("mkfs.f2fs binary not found, using rm -rf to wipe.\n");
Dees_Troye5017042013-08-29 16:38:55 +00002057 return Wipe_RMRF();
2058 }
2059 return false;
2060}
2061
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002062bool TWPartition::Wipe_NTFS() {
2063 string command;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002064 string Ntfsmake_Binary;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002065
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002066 if (TWFunc::Path_Exists("/sbin/mkntfs"))
2067 Ntfsmake_Binary = "mkntfs";
2068 else if (TWFunc::Path_Exists("/sbin/mkfs.ntfs"))
2069 Ntfsmake_Binary = "mkfs.ntfs";
2070 else
2071 return false;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002072
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002073 if (!UnMount(true))
2074 return false;
2075
Greg Wallace4b44fef2015-12-29 15:33:24 -05002076 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)(Ntfsmake_Binary));
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002077 Find_Actual_Block_Device();
2078 command = "/sbin/" + Ntfsmake_Binary + " " + Actual_Block_Device;
2079 if (TWFunc::Exec_Cmd(command) == 0) {
2080 Recreate_AndSec_Folder();
2081 gui_msg("done=Done.");
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002082 return true;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002083 } else {
2084 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
2085 return false;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002086 }
2087 return false;
2088}
2089
Dees_Troy51a0e822012-09-05 15:24:24 -04002090bool TWPartition::Wipe_Data_Without_Wiping_Media() {
Ethan Yonker83e82572014-04-04 10:59:28 -05002091#ifdef TW_OEM_BUILD
2092 // In an OEM Build we want to do a full format
2093 return Wipe_Encryption();
2094#else
Ethan Yonker66a19492015-12-10 10:19:45 -06002095 bool ret = false;
Dees_Troy38bd7602012-09-14 13:33:53 -04002096
Dees_Troy38bd7602012-09-14 13:33:53 -04002097 if (!Mount(true))
2098 return false;
2099
Ethan Yonker74db1572015-10-28 12:44:49 -05002100 gui_msg("wiping_data=Wiping data without wiping /data/media ...");
Ethan Yonker66a19492015-12-10 10:19:45 -06002101 ret = Wipe_Data_Without_Wiping_Media_Func(Mount_Point + "/");
2102 if (ret)
2103 gui_msg("done=Done.");
2104 return ret;
2105#endif // ifdef TW_OEM_BUILD
2106}
2107
2108bool TWPartition::Wipe_Data_Without_Wiping_Media_Func(const string& parent __unused) {
2109 string dir;
Dees_Troy38bd7602012-09-14 13:33:53 -04002110
2111 DIR* d;
Ethan Yonker66a19492015-12-10 10:19:45 -06002112 d = opendir(parent.c_str());
Dees_Troy16b74352012-11-14 22:27:31 +00002113 if (d != NULL) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002114 struct dirent* de;
2115 while ((de = readdir(d)) != NULL) {
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002116 if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) continue;
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05002117
Ethan Yonker66a19492015-12-10 10:19:45 -06002118 dir = parent;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002119 dir.append(de->d_name);
Ethan Yonker66a19492015-12-10 10:19:45 -06002120 if (du.check_skip_dirs(dir)) {
2121 LOGINFO("skipped '%s'\n", dir.c_str());
2122 continue;
2123 }
Dees_Troyce675462013-01-09 19:48:21 +00002124 if (de->d_type == DT_DIR) {
Ethan Yonker66a19492015-12-10 10:19:45 -06002125 dir.append("/");
2126 if (!Wipe_Data_Without_Wiping_Media_Func(dir)) {
2127 closedir(d);
2128 return false;
2129 }
2130 rmdir(dir.c_str());
bigbiff bigbiff98f1f902013-01-19 18:46:13 -05002131 } 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 +00002132 if (!unlink(dir.c_str()))
Dees_Troy2673cec2013-04-02 20:22:16 +00002133 LOGINFO("Unable to unlink '%s'\n", dir.c_str());
Dees_Troyce675462013-01-09 19:48:21 +00002134 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002135 }
2136 closedir(d);
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05002137
Dees_Troy16b74352012-11-14 22:27:31 +00002138 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -04002139 }
Ethan Yonker74db1572015-10-28 12:44:49 -05002140 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy16b74352012-11-14 22:27:31 +00002141 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04002142}
2143
Ethan Yonker472f5062016-02-25 13:47:30 -06002144bool TWPartition::Backup_Tar(const string& backup_folder, ProgressTracking *progress, pid_t &tar_fork_pid) {
2145 string Full_FileName;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002146 twrpTar tar;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05002147
Dees_Troy43d8b002012-09-17 16:00:01 -04002148 if (!Mount(true))
2149 return false;
2150
Dees_Troya13d74f2013-03-24 08:54:55 -05002151 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Backup_Display_Name, "Backing Up");
Ethan Yonker74db1572015-10-28 12:44:49 -05002152 gui_msg(Msg("backing_up=Backing up {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002153
Ethan Yonker472f5062016-02-25 13:47:30 -06002154 DataManager::GetValue(TW_USE_COMPRESSION_VAR, tar.use_compression);
Matt Mowerbb81e5d2014-03-20 18:05:41 -05002155
Dees_Troy83bd4832013-05-04 12:39:56 +00002156#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
Ethan Yonker472f5062016-02-25 13:47:30 -06002157 if (Can_Encrypt_Backup) {
2158 DataManager::GetValue("tw_encrypt_backup", tar.use_encryption);
2159 if (tar.use_encryption) {
2160 if (Use_Userdata_Encryption)
2161 tar.userdata_encryption = tar.use_encryption;
2162 string Password;
2163 DataManager::GetValue("tw_backup_password", Password);
2164 tar.setpassword(Password);
2165 } else {
2166 tar.use_encryption = 0;
2167 }
Dees_Troy83bd4832013-05-04 12:39:56 +00002168 }
2169#endif
Dees_Troy43d8b002012-09-17 16:00:01 -04002170
Ethan Yonker472f5062016-02-25 13:47:30 -06002171 Backup_FileName = Backup_Name + "." + Current_File_System + ".win";
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002172 Full_FileName = backup_folder + "/" + Backup_FileName;
Dees_Troy83bd4832013-05-04 12:39:56 +00002173 tar.has_data_media = Has_Data_Media;
Dees Troye0a433a2013-12-02 04:10:37 +00002174 Full_FileName = backup_folder + "/" + Backup_FileName;
2175 tar.setdir(Backup_Path);
2176 tar.setfn(Full_FileName);
2177 tar.setsize(Backup_Size);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002178 tar.partition_name = Backup_Name;
2179 tar.backup_folder = backup_folder;
Ethan Yonker472f5062016-02-25 13:47:30 -06002180 if (tar.createTarFork(progress, tar_fork_pid) != 0)
Dees Troye0a433a2013-12-02 04:10:37 +00002181 return false;
Dees_Troy43d8b002012-09-17 16:00:01 -04002182 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002183}
2184
Ethan Yonker472f5062016-02-25 13:47:30 -06002185bool TWPartition::Backup_Image(const string& backup_folder, ProgressTracking *progress) {
2186 string Full_FileName;
igoriok87e3d932013-01-31 21:03:53 +02002187
Ethan Yonker74db1572015-10-28 12:44:49 -05002188 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, gui_parse_text("{@backing}"));
2189 gui_msg(Msg("backing_up=Backing up {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002190
Ethan Yonker472f5062016-02-25 13:47:30 -06002191 Backup_FileName = Backup_Name + "." + Current_File_System + ".win";
Dees_Troy43d8b002012-09-17 16:00:01 -04002192 Full_FileName = backup_folder + "/" + Backup_FileName;
2193
Ethan Yonker472f5062016-02-25 13:47:30 -06002194 if (!Raw_Read_Write(Actual_Block_Device, Full_FileName, Backup_Size, progress))
2195 return false;
2196
Ethan Yonker4b94cfd2014-12-11 10:00:45 -06002197 tw_set_default_metadata(Full_FileName.c_str());
Dees_Troyc154ac22012-10-12 15:36:47 -04002198 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002199 gui_msg(Msg(msg::kError, "backup_size=Backup file size for '{1}' is 0 bytes.")(Full_FileName));
Dees_Troy7c2dec82012-09-26 09:49:14 -04002200 return false;
2201 }
Dees_Troy43d8b002012-09-17 16:00:01 -04002202 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002203}
2204
Ethan Yonker472f5062016-02-25 13:47:30 -06002205bool TWPartition::Raw_Read_Write(const string& input_file, const string& output_file, const unsigned long long input_size, ProgressTracking *progress) {
2206 unsigned long long RW_Block_Size, Remain;
2207 int src_fd = -1, dest_fd = -1, bs;
2208 bool ret = false;
2209 void* buffer = NULL;
2210 unsigned long long backedup_size = 0;
2211
2212 RW_Block_Size = 1048576LLU; // 1MB
2213 Remain = input_size;
2214
2215 src_fd = open(input_file.c_str(), O_RDONLY | O_LARGEFILE);
2216 if (src_fd < 0) {
2217 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(input_file)(strerror(errno)));
2218 return false;
2219 }
2220 dest_fd = open(output_file.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_LARGEFILE, S_IRUSR | S_IWUSR);
2221 if (dest_fd < 0) {
2222 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(output_file)(strerror(errno)));
2223 goto exit;
2224 }
2225 bs = (int)(RW_Block_Size);
2226 buffer = malloc((size_t)bs);
2227 if (!buffer) {
2228 LOGINFO("Raw_Read_Write failed to malloc\n");
2229 goto exit;
2230 }
2231 LOGINFO("Reading '%s', writing '%s'\n", input_file.c_str(), output_file.c_str());
2232 if (progress)
2233 progress->SetPartitionSize(input_size);
2234 while (Remain > 0) {
2235 if (Remain < RW_Block_Size)
2236 bs = (int)(Remain);
2237 if (read(src_fd, buffer, bs) != bs) {
2238 LOGINFO("Error reading source fd (%s)\n", strerror(errno));
2239 goto exit;
2240 }
2241 if (write(dest_fd, buffer, bs) != bs) {
2242 LOGINFO("Error writing destination fd (%s)\n", strerror(errno));
2243 goto exit;
2244 }
2245 backedup_size += (unsigned long long)(bs);
2246 Remain -= (unsigned long long)(bs);
2247 if (progress)
2248 progress->UpdateSize(backedup_size);
2249 if (PartitionManager.Check_Backup_Cancel() != 0)
2250 goto exit;
2251 }
2252 if (progress)
2253 progress->UpdateDisplayDetails(true);
2254 fsync(dest_fd);
2255 ret = true;
2256exit:
2257 if (src_fd >= 0)
2258 close(src_fd);
2259 if (dest_fd >= 0)
2260 close(dest_fd);
2261 if (buffer)
2262 free(buffer);
2263 return ret;
2264}
2265
2266bool TWPartition::Backup_Dump_Image(const string& backup_folder, ProgressTracking *progress) {
Vojtech Bocek05534202013-09-11 08:11:56 +02002267 string Full_FileName, Command;
Dees_Troy43d8b002012-09-17 16:00:01 -04002268
Ethan Yonker74db1572015-10-28 12:44:49 -05002269 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, gui_parse_text("{@backing}"));
2270 gui_msg(Msg("backing_up=Backing up {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002271
Ethan Yonker472f5062016-02-25 13:47:30 -06002272 if (progress)
2273 progress->SetPartitionSize(Backup_Size);
Dees_Troy43d8b002012-09-17 16:00:01 -04002274
Ethan Yonker472f5062016-02-25 13:47:30 -06002275 Backup_FileName = Backup_Name + "." + Current_File_System + ".win";
Dees_Troy43d8b002012-09-17 16:00:01 -04002276 Full_FileName = backup_folder + "/" + Backup_FileName;
2277
2278 Command = "dump_image " + MTD_Name + " '" + Full_FileName + "'";
Dees_Troy2673cec2013-04-02 20:22:16 +00002279 LOGINFO("Backup command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02002280 TWFunc::Exec_Cmd(Command);
Ethan Yonker4b94cfd2014-12-11 10:00:45 -06002281 tw_set_default_metadata(Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04002282 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
2283 // 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 -06002284 gui_msg(Msg(msg::kError, "backup_size=Backup file size for '{1}' is 0 bytes.")(Full_FileName));
Dees_Troy7c2dec82012-09-26 09:49:14 -04002285 return false;
2286 }
Ethan Yonker472f5062016-02-25 13:47:30 -06002287 if (progress)
2288 progress->UpdateSize(Backup_Size);
Dees_Troy43d8b002012-09-17 16:00:01 -04002289 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002290}
2291
Ethan Yonker472f5062016-02-25 13:47:30 -06002292unsigned long long TWPartition::Get_Restore_Size(const string& restore_folder) {
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002293 InfoManager restore_info(restore_folder + "/" + Backup_Name + ".info");
2294 if (restore_info.LoadValues() == 0) {
2295 if (restore_info.GetValue("backup_size", Restore_Size) == 0) {
2296 LOGINFO("Read info file, restore size is %llu\n", Restore_Size);
2297 return Restore_Size;
2298 }
2299 }
2300 string Full_FileName, Restore_File_System = Get_Restore_File_System(restore_folder);
2301
2302 Full_FileName = restore_folder + "/" + Backup_FileName;
2303
2304 if (Is_Image(Restore_File_System)) {
2305 Restore_Size = TWFunc::Get_File_Size(Full_FileName);
2306 return Restore_Size;
2307 }
2308
2309 twrpTar tar;
2310 tar.setdir(Backup_Path);
2311 tar.setfn(Full_FileName);
2312 tar.backup_name = Backup_Name;
2313#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
2314 string Password;
2315 DataManager::GetValue("tw_restore_password", Password);
2316 if (!Password.empty())
2317 tar.setpassword(Password);
2318#endif
2319 tar.partition_name = Backup_Name;
2320 tar.backup_folder = restore_folder;
2321 Restore_Size = tar.get_size();
2322 return Restore_Size;
2323}
2324
Ethan Yonker472f5062016-02-25 13:47:30 -06002325bool TWPartition::Restore_Tar(const string& restore_folder, const string& Restore_File_System, ProgressTracking *progress) {
2326 string Full_FileName;
Dees Troy4159aed2014-02-28 17:24:43 +00002327 bool ret = false;
Dees_Troy43d8b002012-09-17 16:00:01 -04002328
Dees_Troye58d5262012-09-21 12:27:57 -04002329 if (Has_Android_Secure) {
Dees_Troye58d5262012-09-21 12:27:57 -04002330 if (!Wipe_AndSec())
2331 return false;
Gary Peck43acadf2012-11-21 21:19:01 -08002332 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002333 gui_msg(Msg("wiping=Wiping {1}")(Backup_Display_Name));
Ethan Yonker5eac2222014-06-11 12:22:55 -05002334 if (Has_Data_Media && Mount_Point == "/data" && Restore_File_System != Current_File_System) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002335 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 -05002336 if (!Wipe_Data_Without_Wiping_Media())
2337 return false;
2338 } else {
2339 if (!Wipe(Restore_File_System))
2340 return false;
2341 }
Dees_Troye58d5262012-09-21 12:27:57 -04002342 }
Matt Mower3c366972015-12-25 19:28:31 -06002343 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Backup_Display_Name, gui_parse_text("{@restoring_hdr}"));
Ethan Yonker74db1572015-10-28 12:44:49 -05002344 gui_msg(Msg("restoring=Restoring {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002345
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05002346 // Remount as read/write as needed so we can restore the backup
2347 if (!ReMount_RW(true))
Dees_Troy43d8b002012-09-17 16:00:01 -04002348 return false;
2349
Dees_Troy4a2a1262012-09-18 09:33:47 -04002350 Full_FileName = restore_folder + "/" + Backup_FileName;
Ethan Yonker87af5632014-02-10 11:56:35 -06002351 twrpTar tar;
2352 tar.setdir(Backup_Path);
2353 tar.setfn(Full_FileName);
2354 tar.backup_name = Backup_Name;
2355#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
2356 string Password;
2357 DataManager::GetValue("tw_restore_password", Password);
2358 if (!Password.empty())
2359 tar.setpassword(Password);
2360#endif
Ethan Yonker472f5062016-02-25 13:47:30 -06002361 progress->SetPartitionSize(Get_Restore_Size(restore_folder));
2362 if (tar.extractTarFork(progress) != 0)
Dees Troy4159aed2014-02-28 17:24:43 +00002363 ret = false;
2364 else
2365 ret = true;
2366#ifdef HAVE_CAPABILITIES
2367 // Restore capabilities to the run-as binary
2368 if (Mount_Point == "/system" && Mount(true) && TWFunc::Path_Exists("/system/bin/run-as")) {
2369 struct vfs_cap_data cap_data;
2370 uint64_t capabilities = (1 << CAP_SETUID) | (1 << CAP_SETGID);
2371
2372 memset(&cap_data, 0, sizeof(cap_data));
2373 cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE;
2374 cap_data.data[0].permitted = (uint32_t) (capabilities & 0xffffffff);
2375 cap_data.data[0].inheritable = 0;
2376 cap_data.data[1].permitted = (uint32_t) (capabilities >> 32);
2377 cap_data.data[1].inheritable = 0;
2378 if (setxattr("/system/bin/run-as", XATTR_NAME_CAPS, &cap_data, sizeof(cap_data), 0) < 0) {
2379 LOGINFO("Failed to reset capabilities of /system/bin/run-as binary.\n");
2380 } else {
2381 LOGINFO("Reset capabilities of /system/bin/run-as binary successful.\n");
2382 }
2383 }
2384#endif
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05002385 if (Mount_Read_Only || Mount_Flags & MS_RDONLY)
2386 // Remount as read only when restoration is complete
2387 ReMount(true);
2388
Dees Troy4159aed2014-02-28 17:24:43 +00002389 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04002390}
2391
Ethan Yonker472f5062016-02-25 13:47:30 -06002392bool TWPartition::Restore_Image(const string& restore_folder, const string& Restore_File_System, ProgressTracking *progress) {
Ethan Yonker96af84a2015-01-05 14:58:36 -06002393 string Full_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04002394
Matt Mower3c366972015-12-25 19:28:31 -06002395 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Backup_Display_Name, gui_parse_text("{@restoring_hdr}"));
Ethan Yonker74db1572015-10-28 12:44:49 -05002396 gui_msg(Msg("restoring=Restoring {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002397 Full_FileName = restore_folder + "/" + Backup_FileName;
Gary Peck15e623d2012-11-21 21:07:58 -08002398
Ethan Yonker96af84a2015-01-05 14:58:36 -06002399 if (Restore_File_System == "emmc") {
Ethan Yonker472f5062016-02-25 13:47:30 -06002400 unsigned long long file_size = (unsigned long long)(TWFunc::Get_File_Size(Full_FileName));
2401 if (!Raw_Read_Write(Full_FileName, Actual_Block_Device, file_size, progress))
Ethan Yonker96af84a2015-01-05 14:58:36 -06002402 return false;
2403 } else if (Restore_File_System == "mtd" || Restore_File_System == "bml") {
Ethan Yonker472f5062016-02-25 13:47:30 -06002404 if (!Flash_Image_FI(Full_FileName, progress))
Ethan Yonker96af84a2015-01-05 14:58:36 -06002405 return false;
Gary Peck15e623d2012-11-21 21:07:58 -08002406 }
Dees_Troy43d8b002012-09-17 16:00:01 -04002407 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002408}
Dees_Troy5bf43922012-09-07 16:07:55 -04002409
2410bool TWPartition::Update_Size(bool Display_Error) {
Dees_Troy0550cfb2012-10-13 11:56:13 -04002411 bool ret = false, Was_Already_Mounted = false;
Dees_Troy51127312012-09-08 13:08:49 -04002412
Dees_Troyab10ee22012-09-21 14:27:30 -04002413 if (!Can_Be_Mounted && !Is_Encrypted)
Dees_Troy5bf43922012-09-07 16:07:55 -04002414 return false;
2415
Dees_Troy0550cfb2012-10-13 11:56:13 -04002416 Was_Already_Mounted = Is_Mounted();
Dees_Troy38bd7602012-09-14 13:33:53 -04002417 if (Removable || Is_Encrypted) {
2418 if (!Mount(false))
2419 return true;
2420 } else if (!Mount(Display_Error))
Dees_Troy5bf43922012-09-07 16:07:55 -04002421 return false;
Dees_Troy51127312012-09-08 13:08:49 -04002422
2423 ret = Get_Size_Via_statfs(Display_Error);
Dees_Troy0550cfb2012-10-13 11:56:13 -04002424 if (!ret || Size == 0) {
2425 if (!Get_Size_Via_df(Display_Error)) {
2426 if (!Was_Already_Mounted)
2427 UnMount(false);
Dees_Troy51127312012-09-08 13:08:49 -04002428 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04002429 }
2430 }
Dees_Troy51127312012-09-08 13:08:49 -04002431
Dees_Troy5bf43922012-09-07 16:07:55 -04002432 if (Has_Data_Media) {
2433 if (Mount(Display_Error)) {
Dees_Troy51127312012-09-08 13:08:49 -04002434 unsigned long long data_media_used, actual_data;
Ethan Yonker66a19492015-12-10 10:19:45 -06002435 Used = du.Get_Folder_Size(Mount_Point);
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002436 Backup_Size = Used;
2437 int bak = (int)(Used / 1048576LLU);
Dees_Troy51127312012-09-08 13:08:49 -04002438 int fre = (int)(Free / 1048576LLU);
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002439 LOGINFO("Data backup size is %iMB, free: %iMB.\n", bak, fre);
Dees_Troy0550cfb2012-10-13 11:56:13 -04002440 } else {
2441 if (!Was_Already_Mounted)
2442 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04002443 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04002444 }
Dees_Troye58d5262012-09-21 12:27:57 -04002445 } else if (Has_Android_Secure) {
2446 if (Mount(Display_Error))
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002447 Backup_Size = du.Get_Folder_Size(Backup_Path);
Dees_Troy0550cfb2012-10-13 11:56:13 -04002448 else {
2449 if (!Was_Already_Mounted)
2450 UnMount(false);
Dees_Troye58d5262012-09-21 12:27:57 -04002451 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04002452 }
Dees_Troy5bf43922012-09-07 16:07:55 -04002453 }
Dees_Troy0550cfb2012-10-13 11:56:13 -04002454 if (!Was_Already_Mounted)
2455 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04002456 return true;
Dees_Troy51127312012-09-08 13:08:49 -04002457}
Dees_Troy38bd7602012-09-14 13:33:53 -04002458
2459void TWPartition::Find_Actual_Block_Device(void) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002460 if (Is_Decrypted && !Decrypted_Block_Device.empty()) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002461 Actual_Block_Device = Decrypted_Block_Device;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002462 if (TWFunc::Path_Exists(Decrypted_Block_Device))
Dees_Troy38bd7602012-09-14 13:33:53 -04002463 Is_Present = true;
Dees_Troy43d8b002012-09-17 16:00:01 -04002464 } else if (TWFunc::Path_Exists(Primary_Block_Device)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002465 Is_Present = true;
2466 Actual_Block_Device = Primary_Block_Device;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002467 return;
2468 }
2469 if (Is_Decrypted) {
Dees_Troy43d8b002012-09-17 16:00:01 -04002470 } else if (!Alternate_Block_Device.empty() && TWFunc::Path_Exists(Alternate_Block_Device)) {
Dees_Troy3f04d032012-10-07 18:20:09 -04002471 Actual_Block_Device = Alternate_Block_Device;
Dees_Troy38bd7602012-09-14 13:33:53 -04002472 Is_Present = true;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002473 } else {
Dees_Troy38bd7602012-09-14 13:33:53 -04002474 Is_Present = false;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002475 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002476}
2477
2478void TWPartition::Recreate_Media_Folder(void) {
2479 string Command;
Ethan Yonker66a19492015-12-10 10:19:45 -06002480 string Media_Path = Mount_Point + "/media";
Dees_Troy38bd7602012-09-14 13:33:53 -04002481
2482 if (!Mount(true)) {
Ethan Yonker66a19492015-12-10 10:19:45 -06002483 gui_msg(Msg(msg::kError, "recreate_folder_err=Unable to recreate {1} folder.")(Media_Path));
2484 } else if (!TWFunc::Path_Exists(Media_Path)) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002485 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Ethan Yonker66a19492015-12-10 10:19:45 -06002486 LOGINFO("Recreating %s folder.\n", Media_Path.c_str());
2487 mkdir(Media_Path.c_str(), 0770);
xiaolu9416f4f2015-06-04 08:22:23 +08002488 string Internal_path = DataManager::GetStrValue("tw_internal_path");
2489 if (!Internal_path.empty()) {
2490 LOGINFO("Recreating %s folder.\n", Internal_path.c_str());
2491 mkdir(Internal_path.c_str(), 0770);
2492 }
2493#ifdef TW_INTERNAL_STORAGE_PATH
2494 mkdir(EXPAND(TW_INTERNAL_STORAGE_PATH), 0770);
2495#endif
Ethan Yonker5ef301e2014-10-14 10:04:44 -05002496#ifdef HAVE_SELINUX
thata3d31fb2014-12-21 22:27:40 +01002497 // Afterwards, we will try to set the
2498 // default metadata that we were hopefully able to get during
2499 // early boot.
Ethan Yonker66a19492015-12-10 10:19:45 -06002500 tw_set_default_metadata(Media_Path.c_str());
xiaolu9416f4f2015-06-04 08:22:23 +08002501 if (!Internal_path.empty())
2502 tw_set_default_metadata(Internal_path.c_str());
Ethan Yonker5ef301e2014-10-14 10:04:44 -05002503#endif
Ethan Yonker5eac2222014-06-11 12:22:55 -05002504 // Toggle mount to ensure that "internal sdcard" gets mounted
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002505 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Ethan Yonker5eac2222014-06-11 12:22:55 -05002506 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Dees_Troy38bd7602012-09-14 13:33:53 -04002507 }
Dees_Troy43d8b002012-09-17 16:00:01 -04002508}
Dees_Troye58d5262012-09-21 12:27:57 -04002509
2510void TWPartition::Recreate_AndSec_Folder(void) {
Dees_Troye58d5262012-09-21 12:27:57 -04002511 if (!Has_Android_Secure)
2512 return;
Dees_Troy2673cec2013-04-02 20:22:16 +00002513 LOGINFO("Creating %s: %s\n", Backup_Display_Name.c_str(), Symlink_Path.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -04002514 if (!Mount(true)) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002515 gui_msg(Msg(msg::kError, "recreate_folder_err=Unable to recreate {1} folder.")(Backup_Name));
Dees_Troye58d5262012-09-21 12:27:57 -04002516 } else if (!TWFunc::Path_Exists(Symlink_Path)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00002517 LOGINFO("Recreating %s folder.\n", Backup_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002518 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05002519 mkdir(Symlink_Path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002520 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04002521 }
2522}
bigbiff7cb4c332014-11-26 20:36:07 -05002523
2524uint64_t TWPartition::Get_Max_FileSize() {
2525 uint64_t maxFileSize = 0;
2526 const uint64_t constGB = (uint64_t) 1024 * 1024 * 1024;
2527 const uint64_t constTB = (uint64_t) constGB * 1024;
2528 const uint64_t constPB = (uint64_t) constTB * 1024;
2529 const uint64_t constEB = (uint64_t) constPB * 1024;
bigbiff0c532032014-12-21 13:41:26 -05002530 if (Current_File_System == "ext4")
2531 maxFileSize = 16 * constTB; //16 TB
2532 else if (Current_File_System == "vfat")
2533 maxFileSize = 4 * constGB; //4 GB
2534 else if (Current_File_System == "ntfs")
2535 maxFileSize = 256 * constTB; //256 TB
2536 else if (Current_File_System == "exfat")
2537 maxFileSize = 16 * constPB; //16 PB
2538 else if (Current_File_System == "ext3")
2539 maxFileSize = 2 * constTB; //2 TB
2540 else if (Current_File_System == "f2fs")
2541 maxFileSize = 3.94 * constTB; //3.94 TB
bigbiff7cb4c332014-11-26 20:36:07 -05002542 else
2543 maxFileSize = 100000000L;
2544 return maxFileSize - 1;
2545}
2546
Ethan Yonker472f5062016-02-25 13:47:30 -06002547bool TWPartition::Flash_Image(const string& Filename) {
Ethan Yonker96af84a2015-01-05 14:58:36 -06002548 string Restore_File_System;
2549
2550 LOGINFO("Image filename is: %s\n", Filename.c_str());
2551
2552 if (Backup_Method == FILES) {
2553 LOGERR("Cannot flash images to file systems\n");
2554 return false;
2555 } else if (!Can_Flash_Img) {
2556 LOGERR("Cannot flash images to partitions %s\n", Display_Name.c_str());
2557 return false;
2558 } else {
2559 if (!Find_Partition_Size()) {
2560 LOGERR("Unable to find partition size for '%s'\n", Mount_Point.c_str());
2561 return false;
2562 }
2563 unsigned long long image_size = TWFunc::Get_File_Size(Filename);
2564 if (image_size > Size) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002565 LOGINFO("Size (%llu bytes) of image '%s' is larger than target device '%s' (%llu bytes)\n",
Ethan Yonker96af84a2015-01-05 14:58:36 -06002566 image_size, Filename.c_str(), Actual_Block_Device.c_str(), Size);
Ethan Yonker74db1572015-10-28 12:44:49 -05002567 gui_err("img_size_err=Size of image is larger than target device");
Ethan Yonker96af84a2015-01-05 14:58:36 -06002568 return false;
2569 }
Ethan Yonker472f5062016-02-25 13:47:30 -06002570 if (Backup_Method == DD) {
2571 if (Is_Sparse_Image(Filename)) {
2572 return Flash_Sparse_Image(Filename);
2573 }
2574 unsigned long long file_size = (unsigned long long)(TWFunc::Get_File_Size(Filename));
2575 ProgressTracking pt(file_size);
2576 return Raw_Read_Write(Filename, Actual_Block_Device, file_size, &pt);
2577 } else if (Backup_Method == FLASH_UTILS) {
2578 return Flash_Image_FI(Filename, NULL);
2579 }
Ethan Yonker96af84a2015-01-05 14:58:36 -06002580 }
2581
2582 LOGERR("Unknown flash method for '%s'\n", Mount_Point.c_str());
2583 return false;
2584}
2585
Ethan Yonker472f5062016-02-25 13:47:30 -06002586bool TWPartition::Is_Sparse_Image(const string& Filename) {
HashBanged974bb2016-01-30 14:20:09 -05002587 uint32_t magic = 0;
2588 int fd = open(Filename.c_str(), O_RDONLY);
2589 if (fd < 0) {
2590 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Filename)(strerror(errno)));
2591 return false;
2592 }
2593 if (read(fd, &magic, sizeof(magic)) != sizeof(magic)) {
2594 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Filename)(strerror(errno)));
2595 close(fd);
2596 return false;
2597 }
2598 close(fd);
Ethan Yonker472f5062016-02-25 13:47:30 -06002599 if (magic == SPARSE_HEADER_MAGIC)
2600 return true;
2601 return false;
2602}
2603
2604bool TWPartition::Flash_Sparse_Image(const string& Filename) {
2605 string Command;
2606
2607 gui_msg(Msg("flashing=Flashing {1}...")(Display_Name));
2608
2609 Command = "simg2img '" + Filename + "' '" + Actual_Block_Device + "'";
Ethan Yonker96af84a2015-01-05 14:58:36 -06002610 LOGINFO("Flash command: '%s'\n", Command.c_str());
2611 TWFunc::Exec_Cmd(Command);
2612 return true;
2613}
2614
Ethan Yonker472f5062016-02-25 13:47:30 -06002615bool TWPartition::Flash_Image_FI(const string& Filename, ProgressTracking *progress) {
Ethan Yonker96af84a2015-01-05 14:58:36 -06002616 string Command;
Ethan Yonker472f5062016-02-25 13:47:30 -06002617 unsigned long long file_size;
Ethan Yonker96af84a2015-01-05 14:58:36 -06002618
Ethan Yonker74db1572015-10-28 12:44:49 -05002619 gui_msg(Msg("flashing=Flashing {1}...")(Display_Name));
Ethan Yonker472f5062016-02-25 13:47:30 -06002620 if (progress) {
2621 file_size = (unsigned long long)(TWFunc::Get_File_Size(Filename));
2622 progress->SetPartitionSize(file_size);
2623 }
Ethan Yonker96af84a2015-01-05 14:58:36 -06002624 // Sometimes flash image doesn't like to flash due to the first 2KB matching, so we erase first to ensure that it flashes
2625 Command = "erase_image " + MTD_Name;
2626 LOGINFO("Erase command: '%s'\n", Command.c_str());
2627 TWFunc::Exec_Cmd(Command);
2628 Command = "flash_image " + MTD_Name + " '" + Filename + "'";
2629 LOGINFO("Flash command: '%s'\n", Command.c_str());
2630 TWFunc::Exec_Cmd(Command);
Ethan Yonker472f5062016-02-25 13:47:30 -06002631 if (progress)
2632 progress->UpdateSize(file_size);
Ethan Yonker96af84a2015-01-05 14:58:36 -06002633 return true;
2634}
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05002635
2636void TWPartition::Change_Mount_Read_Only(bool new_value) {
2637 Mount_Read_Only = new_value;
2638}
2639
2640int TWPartition::Check_Lifetime_Writes() {
2641 bool original_read_only = Mount_Read_Only;
2642 int ret = 1;
2643
2644 Mount_Read_Only = true;
2645 if (Mount(false)) {
2646 Find_Actual_Block_Device();
2647 string block = basename(Actual_Block_Device.c_str());
2648 string file = "/sys/fs/" + Current_File_System + "/" + block + "/lifetime_write_kbytes";
2649 string result;
2650 if (TWFunc::Path_Exists(file)) {
2651 if (TWFunc::read_file(file, result) != 0) {
2652 LOGINFO("Check_Lifetime_Writes of '%s' failed to read_file\n", file.c_str());
2653 } else {
2654 LOGINFO("Check_Lifetime_Writes result: '%s'\n", result.c_str());
2655 if (result == "0") {
2656 ret = 0;
2657 }
2658 }
2659 } else {
2660 LOGINFO("Check_Lifetime_Writes file does not exist '%s'\n", file.c_str());
2661 }
2662 UnMount(true);
2663 } else {
2664 LOGINFO("Check_Lifetime_Writes failed to mount '%s'\n", Mount_Point.c_str());
2665 }
2666 Mount_Read_Only = original_read_only;
2667 return ret;
2668}
Ethan Yonker66a19492015-12-10 10:19:45 -06002669
2670int TWPartition::Decrypt_Adopted() {
2671#ifdef TW_INCLUDE_CRYPTO
2672 int ret = 1;
2673 Is_Adopted_Storage = false;
2674 string Adopted_Key_File = "";
2675
2676 if (!Removable)
2677 return ret;
2678
2679 int fd = open(Alternate_Block_Device.c_str(), O_RDONLY);
2680 if (fd < 0) {
2681 LOGINFO("failed to open '%s'\n", Alternate_Block_Device.c_str());
2682 return ret;
2683 }
2684 char type_guid[80];
2685 char part_guid[80];
2686
2687 if (gpt_disk_get_partition_info(fd, 2, type_guid, part_guid) == 0) {
2688 LOGINFO("type: '%s'\n", type_guid);
2689 LOGINFO("part: '%s'\n", part_guid);
2690 Adopted_GUID = part_guid;
2691 LOGINFO("Adopted_GUID '%s'\n", Adopted_GUID.c_str());
2692 if (strcmp(type_guid, TWGptAndroidExpand) == 0) {
2693 LOGINFO("android_expand found\n");
2694 Adopted_Key_File = "/data/misc/vold/expand_";
2695 Adopted_Key_File += part_guid;
2696 Adopted_Key_File += ".key";
2697 if (TWFunc::Path_Exists(Adopted_Key_File)) {
2698 Is_Adopted_Storage = true;
2699 /* Until we find a use case for this, I think it is safe
2700 * to disable USB Mass Storage whenever adopted storage
2701 * is present.
2702 */
2703 LOGINFO("Detected adopted storage, disabling USB mass storage mode\n");
2704 DataManager::SetValue("tw_has_usb_storage", 0);
2705 }
2706 }
2707 }
2708
2709 if (Is_Adopted_Storage) {
2710 string Adopted_Block_Device = Alternate_Block_Device + "p2";
2711 if (!TWFunc::Path_Exists(Adopted_Block_Device)) {
2712 Adopted_Block_Device = Alternate_Block_Device + "2";
2713 if (!TWFunc::Path_Exists(Adopted_Block_Device)) {
2714 LOGINFO("Adopted block device does not exist\n");
2715 goto exit;
2716 }
2717 }
2718 LOGINFO("key file is '%s', block device '%s'\n", Adopted_Key_File.c_str(), Adopted_Block_Device.c_str());
2719 char crypto_blkdev[MAXPATHLEN];
2720 std::string thekey;
2721 int fdkey = open(Adopted_Key_File.c_str(), O_RDONLY);
2722 if (fdkey < 0) {
2723 LOGINFO("failed to open key file\n");
2724 goto exit;
2725 }
2726 char buf[512];
2727 ssize_t n;
2728 while ((n = read(fdkey, &buf[0], sizeof(buf))) > 0) {
2729 thekey.append(buf, n);
2730 }
2731 close(fdkey);
2732 unsigned char* key = (unsigned char*) thekey.data();
2733 cryptfs_revert_ext_volume(part_guid);
2734
2735 ret = cryptfs_setup_ext_volume(part_guid, Adopted_Block_Device.c_str(), key, thekey.size(), crypto_blkdev);
2736 if (ret == 0) {
2737 LOGINFO("adopted storage new block device: '%s'\n", crypto_blkdev);
2738 Decrypted_Block_Device = crypto_blkdev;
2739 Is_Decrypted = true;
2740 Is_Encrypted = true;
2741 Find_Actual_Block_Device();
2742 if (!Mount(false)) {
2743 LOGERR("Failed to mount decrypted adopted storage device\n");
2744 Is_Decrypted = false;
2745 Is_Encrypted = false;
2746 cryptfs_revert_ext_volume(part_guid);
2747 ret = 1;
2748 } else {
Ethan Yonkerfcf3f242016-02-16 12:30:26 -06002749 UnMount(false);
2750 Has_Android_Secure = false;
2751 Symlink_Path = "";
2752 Symlink_Mount_Point = "";
2753 Backup_Name = Mount_Point.substr(1);
2754 Backup_Path = Mount_Point;
2755 TWPartition* sdext = PartitionManager.Find_Partition_By_Path("/sd-ext");
2756 if (sdext && sdext->Actual_Block_Device == Adopted_Block_Device) {
2757 LOGINFO("Removing /sd-ext from partition list due to adopted storage\n");
2758 PartitionManager.Remove_Partition_By_Path("/sd-ext");
2759 }
Ethan Yonker66a19492015-12-10 10:19:45 -06002760 Setup_Data_Media();
2761 Recreate_Media_Folder();
2762 Wipe_Available_in_GUI = true;
2763 Wipe_During_Factory_Reset = true;
2764 Can_Be_Backed_Up = true;
2765 Can_Encrypt_Backup = true;
2766 Use_Userdata_Encryption = true;
2767 Is_Storage = true;
2768 Storage_Name = "Adopted Storage";
2769 Is_SubPartition = true;
2770 SubPartition_Of = "/data";
2771 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
2772 DataManager::SetValue("tw_has_adopted_storage", 1);
2773 }
2774 } else {
2775 LOGERR("Failed to setup adopted storage decryption\n");
2776 }
2777 }
2778exit:
2779 return ret;
2780#else
2781 LOGINFO("Decrypt_Adopted: no crypto support\n");
2782 return 1;
2783#endif
2784}
2785
2786void TWPartition::Revert_Adopted() {
2787#ifdef TW_INCLUDE_CRYPTO
2788 if (!Adopted_GUID.empty()) {
2789 PartitionManager.Remove_MTP_Storage(Mount_Point);
2790 UnMount(false);
2791 cryptfs_revert_ext_volume(Adopted_GUID.c_str());
2792 Is_Adopted_Storage = false;
2793 Is_Encrypted = false;
2794 Is_Decrypted = false;
2795 Decrypted_Block_Device = "";
2796 Find_Actual_Block_Device();
2797 Wipe_During_Factory_Reset = false;
2798 Can_Be_Backed_Up = false;
2799 Can_Encrypt_Backup = false;
2800 Use_Userdata_Encryption = false;
2801 Is_SubPartition = false;
2802 SubPartition_Of = "";
2803 Has_Data_Media = false;
2804 Storage_Path = Mount_Point;
2805 if (!Symlink_Mount_Point.empty()) {
2806 TWPartition* Dat = PartitionManager.Find_Partition_By_Path("/data");
2807 if (Dat) {
2808 Dat->UnMount(false);
2809 Dat->Symlink_Mount_Point = Symlink_Mount_Point;
2810 }
2811 Symlink_Mount_Point = "";
2812 }
2813 }
2814#else
2815 LOGINFO("Revert_Adopted: no crypto support\n");
2816#endif
2817}