blob: 8b73f646b35577d3bc1e61ccf25b59894654ef4e [file] [log] [blame]
Dees Troy3be70a82013-10-22 14:25:12 +00001/*
bigbiff bigbiff56cf5642016-08-19 17:43:45 -04002 Copyright 2013 to 2017 TeamWin
Dees Troy3be70a82013-10-22 14:25:12 +00003 This file is part of TWRP/TeamWin Recovery Project.
4
5 TWRP is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 TWRP is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with TWRP. If not, see <http://www.gnu.org/licenses/>.
17*/
Dees_Troy51a0e822012-09-05 15:24:24 -040018
19#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
22#include <sys/stat.h>
23#include <sys/vfs.h>
Dees_Troy5bf43922012-09-07 16:07:55 -040024#include <sys/mount.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040025#include <unistd.h>
Dees_Troy51127312012-09-08 13:08:49 -040026#include <dirent.h>
thatf54e5392016-01-29 22:04:43 +010027#include <libgen.h>
bigbiffce8f83c2015-12-12 18:30:21 -050028#include <zlib.h>
bigbiff bigbiff9c754052013-01-09 09:09:08 -050029#include <iostream>
30#include <sstream>
Ethan Yonker66a19492015-12-10 10:19:45 -060031#include <sys/param.h>
Ethan Yonker472f5062016-02-25 13:47:30 -060032#include <fcntl.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040033
Dees_Troy657c3092012-09-10 20:32:10 -040034#ifdef TW_INCLUDE_CRYPTO
35 #include "cutils/properties.h"
36#endif
37
bigbiff7b4c7a62015-01-01 19:44:14 -050038#include "libblkid/include/blkid.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040039#include "variables.h"
Dees_Troy2673cec2013-04-02 20:22:16 +000040#include "twcommon.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040041#include "partitions.hpp"
Dees_Troy5bf43922012-09-07 16:07:55 -040042#include "data.hpp"
Dees_Troy43d8b002012-09-17 16:00:01 -040043#include "twrp-functions.hpp"
bigbiff bigbiff9c754052013-01-09 09:09:08 -050044#include "twrpTar.hpp"
Ethan Yonker3fdcda42016-11-30 12:29:37 -060045#include "exclude.hpp"
Ethan Yonker1b7a31b2014-07-03 15:09:22 -050046#include "infomanager.hpp"
Ethan Yonker4b94cfd2014-12-11 10:00:45 -060047#include "set_metadata.h"
Ethan Yonker74db1572015-10-28 12:44:49 -050048#include "gui/gui.hpp"
bigbiffce8f83c2015-12-12 18:30:21 -050049#include "adbbu/libtwadbbu.hpp"
Dees_Troy5bf43922012-09-07 16:07:55 -040050extern "C" {
Dees_Troy38bd7602012-09-14 13:33:53 -040051 #include "mtdutils/mtdutils.h"
52 #include "mtdutils/mounts.h"
Dees_Troya95f55c2013-08-17 13:14:43 +000053#ifdef USE_EXT4
Ethan Yonker8373cfe2017-09-08 06:50:54 -050054 // #include "make_ext4fs.h" TODO need ifdef for android8
55 #include <ext4_utils/make_ext4fs.h>
Dees_Troya95f55c2013-08-17 13:14:43 +000056#endif
Ethan Yonker71413f42014-02-26 13:36:08 -060057
58#ifdef TW_INCLUDE_CRYPTO
Ethan Yonker253368a2014-11-25 15:00:52 -060059 #include "crypto/lollipop/cryptfs.h"
Ethan Yonker66a19492015-12-10 10:19:45 -060060 #include "gpt/gpt.h"
Ethan Yonkerbd7492d2016-12-07 13:55:01 -060061 #ifdef TW_INCLUDE_FBE
62 #include "crypto/ext4crypt/Decrypt.h"
Ethan Yonkerbd7492d2016-12-07 13:55:01 -060063 #endif
Ethan Yonkerbc85b632015-08-09 12:48:14 -050064#else
65 #define CRYPT_FOOTER_OFFSET 0x4000
Ethan Yonker71413f42014-02-26 13:36:08 -060066#endif
Dees_Troy5bf43922012-09-07 16:07:55 -040067}
Matt Mower87413642017-01-17 21:14:46 -060068#include <selinux/selinux.h>
Ethan Yonkerf27497f2014-02-09 11:48:33 -060069#include <selinux/label.h>
Dees Troy4159aed2014-02-28 17:24:43 +000070#ifdef HAVE_CAPABILITIES
71#include <sys/capability.h>
72#include <sys/xattr.h>
73#include <linux/xattr.h>
74#endif
HashBanged974bb2016-01-30 14:20:09 -050075#include <sparse_format.h>
Ethan Yonker472f5062016-02-25 13:47:30 -060076#include "progresstracking.hpp"
Dees_Troy51a0e822012-09-05 15:24:24 -040077
bigbiff bigbiff9c754052013-01-09 09:09:08 -050078using namespace std;
79
Ethan Yonker6e8c27a2016-12-22 17:55:57 -060080static int auto_index = 0; // v2 fstab allows you to specify a mount point of "auto" with no /. These items are given a mount point of /auto* where * == auto_index
81
Dees_Troya95f55c2013-08-17 13:14:43 +000082extern struct selabel_handle *selinux_handle;
Ethan Yonker6277c792014-09-15 14:54:30 -050083extern bool datamedia;
Dees_Troya95f55c2013-08-17 13:14:43 +000084
Hashcode62bd9e02013-11-19 21:59:42 -080085struct flag_list {
86 const char *name;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -060087 unsigned long flag;
Hashcode62bd9e02013-11-19 21:59:42 -080088};
89
Matt Mower4ab42b12016-04-21 13:52:18 -050090const struct flag_list mount_flags[] = {
Ethan Yonker6e8c27a2016-12-22 17:55:57 -060091 { "noatime", MS_NOATIME },
92 { "noexec", MS_NOEXEC },
93 { "nosuid", MS_NOSUID },
94 { "nodev", MS_NODEV },
95 { "nodiratime", MS_NODIRATIME },
96 { "ro", MS_RDONLY },
97 { "rw", 0 },
98 { "remount", MS_REMOUNT },
99 { "bind", MS_BIND },
100 { "rec", MS_REC },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000101#ifdef MS_UNBINDABLE
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600102 { "unbindable", MS_UNBINDABLE },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000103#endif
104#ifdef MS_PRIVATE
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600105 { "private", MS_PRIVATE },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000106#endif
107#ifdef MS_SLAVE
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600108 { "slave", MS_SLAVE },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000109#endif
110#ifdef MS_SHARED
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600111 { "shared", MS_SHARED },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000112#endif
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600113 { "sync", MS_SYNCHRONOUS },
114 { 0, 0 },
115};
116
117const char *ignored_mount_items[] = {
118 "defaults=",
119 "errors=",
120 NULL
Hashcode62bd9e02013-11-19 21:59:42 -0800121};
122
Matt Mower2416a502016-04-12 19:54:46 -0500123enum TW_FSTAB_FLAGS {
124 TWFLAG_DEFAULTS, // Retain position
125 TWFLAG_ANDSEC,
126 TWFLAG_BACKUP,
127 TWFLAG_BACKUPNAME,
128 TWFLAG_BLOCKSIZE,
129 TWFLAG_CANBEWIPED,
130 TWFLAG_CANENCRYPTBACKUP,
131 TWFLAG_DISPLAY,
132 TWFLAG_ENCRYPTABLE,
133 TWFLAG_FLASHIMG,
134 TWFLAG_FORCEENCRYPT,
135 TWFLAG_FSFLAGS,
136 TWFLAG_IGNOREBLKID,
137 TWFLAG_LENGTH,
138 TWFLAG_MOUNTTODECRYPT,
139 TWFLAG_REMOVABLE,
140 TWFLAG_RETAINLAYOUTVERSION,
141 TWFLAG_SETTINGSSTORAGE,
142 TWFLAG_STORAGE,
143 TWFLAG_STORAGENAME,
144 TWFLAG_SUBPARTITIONOF,
145 TWFLAG_SYMLINK,
146 TWFLAG_USERDATAENCRYPTBACKUP,
147 TWFLAG_USERMRF,
148 TWFLAG_WIPEDURINGFACTORYRESET,
149 TWFLAG_WIPEINGUI,
Ethan Yonker1b190162016-12-05 15:25:19 -0600150 TWFLAG_SLOTSELECT,
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600151 TWFLAG_WAIT,
152 TWFLAG_VERIFY,
153 TWFLAG_CHECK,
154 TWFLAG_ALTDEVICE,
155 TWFLAG_NOTRIM,
156 TWFLAG_VOLDMANAGED,
157 TWFLAG_FORMATTABLE,
158 TWFLAG_RESIZE,
Matt Mower2416a502016-04-12 19:54:46 -0500159};
160
161/* Flags without a trailing '=' are considered dual format flags and can be
162 * written as either 'flagname' or 'flagname=', where the character following
163 * the '=' is Y,y,1 for true and false otherwise.
164 */
165const struct flag_list tw_flags[] = {
166 { "andsec", TWFLAG_ANDSEC },
167 { "backup", TWFLAG_BACKUP },
168 { "backupname=", TWFLAG_BACKUPNAME },
169 { "blocksize=", TWFLAG_BLOCKSIZE },
170 { "canbewiped", TWFLAG_CANBEWIPED },
171 { "canencryptbackup", TWFLAG_CANENCRYPTBACKUP },
172 { "defaults", TWFLAG_DEFAULTS },
173 { "display=", TWFLAG_DISPLAY },
174 { "encryptable=", TWFLAG_ENCRYPTABLE },
175 { "flashimg", TWFLAG_FLASHIMG },
176 { "forceencrypt=", TWFLAG_FORCEENCRYPT },
177 { "fsflags=", TWFLAG_FSFLAGS },
178 { "ignoreblkid", TWFLAG_IGNOREBLKID },
179 { "length=", TWFLAG_LENGTH },
180 { "mounttodecrypt", TWFLAG_MOUNTTODECRYPT },
181 { "removable", TWFLAG_REMOVABLE },
182 { "retainlayoutversion", TWFLAG_RETAINLAYOUTVERSION },
183 { "settingsstorage", TWFLAG_SETTINGSSTORAGE },
184 { "storage", TWFLAG_STORAGE },
185 { "storagename=", TWFLAG_STORAGENAME },
186 { "subpartitionof=", TWFLAG_SUBPARTITIONOF },
187 { "symlink=", TWFLAG_SYMLINK },
188 { "userdataencryptbackup", TWFLAG_USERDATAENCRYPTBACKUP },
189 { "usermrf", TWFLAG_USERMRF },
190 { "wipeduringfactoryreset", TWFLAG_WIPEDURINGFACTORYRESET },
191 { "wipeingui", TWFLAG_WIPEINGUI },
Ethan Yonker1b190162016-12-05 15:25:19 -0600192 { "slotselect", TWFLAG_SLOTSELECT },
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600193 { "wait", TWFLAG_WAIT },
194 { "verify", TWFLAG_VERIFY },
195 { "check", TWFLAG_CHECK },
196 { "altdevice", TWFLAG_ALTDEVICE },
197 { "notrim", TWFLAG_NOTRIM },
198 { "voldmanaged=", TWFLAG_VOLDMANAGED },
199 { "formattable", TWFLAG_FORMATTABLE },
200 { "resize", TWFLAG_RESIZE },
Matt Mower2416a502016-04-12 19:54:46 -0500201 { 0, 0 },
202};
203
that9e0593e2014-10-08 00:01:24 +0200204TWPartition::TWPartition() {
Dees_Troy51a0e822012-09-05 15:24:24 -0400205 Can_Be_Mounted = false;
206 Can_Be_Wiped = false;
Dees_Troya13d74f2013-03-24 08:54:55 -0500207 Can_Be_Backed_Up = false;
Vojtech Bocek1dc30982013-08-30 21:49:30 +0200208 Use_Rm_Rf = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400209 Wipe_During_Factory_Reset = false;
210 Wipe_Available_in_GUI = false;
211 Is_SubPartition = false;
Dees_Troy2691f9d2012-09-24 11:15:49 -0400212 Has_SubPartition = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400213 SubPartition_Of = "";
214 Symlink_Path = "";
215 Symlink_Mount_Point = "";
216 Mount_Point = "";
Dees_Troye58d5262012-09-21 12:27:57 -0400217 Backup_Path = "";
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600218 Wildcard_Block_Device = false;
219 Sysfs_Entry = "";
Dees_Troy38bd7602012-09-14 13:33:53 -0400220 Actual_Block_Device = "";
221 Primary_Block_Device = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400222 Alternate_Block_Device = "";
223 Removable = false;
224 Is_Present = false;
225 Length = 0;
226 Size = 0;
227 Used = 0;
228 Free = 0;
229 Backup_Size = 0;
230 Can_Be_Encrypted = false;
231 Is_Encrypted = false;
232 Is_Decrypted = false;
Ethan Yonkerbd7492d2016-12-07 13:55:01 -0600233 Is_FBE = false;
Ethan Yonker253368a2014-11-25 15:00:52 -0600234 Mount_To_Decrypt = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400235 Decrypted_Block_Device = "";
236 Display_Name = "";
Dees_Troya13d74f2013-03-24 08:54:55 -0500237 Backup_Display_Name = "";
238 Storage_Name = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400239 Backup_Name = "";
Dees_Troy63c8df72012-09-10 14:02:05 -0400240 Backup_FileName = "";
Dees_Troy38bd7602012-09-14 13:33:53 -0400241 MTD_Name = "";
bigbiffce8f83c2015-12-12 18:30:21 -0500242 Backup_Method = BM_NONE;
Dees_Troy83bd4832013-05-04 12:39:56 +0000243 Can_Encrypt_Backup = false;
244 Use_Userdata_Encryption = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400245 Has_Data_Media = false;
Dees_Troye58d5262012-09-21 12:27:57 -0400246 Has_Android_Secure = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400247 Is_Storage = false;
Dees_Troya13d74f2013-03-24 08:54:55 -0500248 Is_Settings_Storage = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400249 Storage_Path = "";
250 Current_File_System = "";
251 Fstab_File_System = "";
Hashcode62bd9e02013-11-19 21:59:42 -0800252 Mount_Flags = 0;
253 Mount_Options = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400254 Format_Block_Size = 0;
Dees_Troy68cab492012-12-12 19:29:35 +0000255 Ignore_Blkid = false;
Dees_Troy16c2b312013-01-15 16:51:18 +0000256 Retain_Layout_Version = false;
Ethan Yonker253368a2014-11-25 15:00:52 -0600257 Crypto_Key_Location = "footer";
Ethan Yonker726a0202014-12-16 20:01:38 -0600258 MTP_Storage_ID = 0;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600259 Can_Flash_Img = false;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500260 Mount_Read_Only = false;
Ethan Yonker66a19492015-12-10 10:19:45 -0600261 Is_Adopted_Storage = false;
262 Adopted_GUID = "";
Ethan Yonker1b190162016-12-05 15:25:19 -0600263 SlotSelect = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400264}
265
266TWPartition::~TWPartition(void) {
267 // Do nothing
268}
269
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600270bool TWPartition::Process_Fstab_Line(const char *fstab_line, bool Display_Error, std::map<string, Flags_Map> *twrp_flags) {
Matt Mower2b2dd152016-04-26 11:24:08 -0500271 char full_line[MAX_FSTAB_LINE_LENGTH];
Matt Mower2416a502016-04-12 19:54:46 -0500272 char twflags[MAX_FSTAB_LINE_LENGTH] = "";
Dees_Troy5bf43922012-09-07 16:07:55 -0400273 char* ptr;
Matt Mower2b2dd152016-04-26 11:24:08 -0500274 int line_len = strlen(fstab_line), index = 0, item_index = 0;
Dees_Troya13d74f2013-03-24 08:54:55 -0500275 bool skip = false;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600276 int fstab_version = 1, mount_point_index = 0, fs_index = 1, block_device_index = 2;
277 TWPartition *additional_entry = NULL;
278 std::map<string, Flags_Map>::iterator it;
Dees_Troy5bf43922012-09-07 16:07:55 -0400279
Matt Mower2b2dd152016-04-26 11:24:08 -0500280 strlcpy(full_line, fstab_line, sizeof(full_line));
Dees_Troy51127312012-09-08 13:08:49 -0400281 for (index = 0; index < line_len; index++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500282 if (full_line[index] == 34)
283 skip = !skip;
284 if (!skip && full_line[index] <= 32)
Dees_Troy5bf43922012-09-07 16:07:55 -0400285 full_line[index] = '\0';
Dees_Troy5bf43922012-09-07 16:07:55 -0400286 }
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600287 if (line_len < 10)
288 return false; // There can't possibly be a valid fstab line that is less than 10 chars
289 if (strncmp(fstab_line, "/dev/", strlen("/dev/")) == 0 || strncmp(fstab_line, "/devices/", strlen("/devices/")) == 0) {
290 fstab_version = 2;
291 block_device_index = 0;
292 mount_point_index = 1;
293 fs_index = 2;
294 }
295
296 index = 0;
Dees_Troy5bf43922012-09-07 16:07:55 -0400297 while (index < line_len) {
298 while (index < line_len && full_line[index] == '\0')
299 index++;
300 if (index >= line_len)
301 continue;
302 ptr = full_line + index;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600303 if (item_index == mount_point_index) {
304 Mount_Point = ptr;
305 if (fstab_version == 2) {
306 additional_entry = PartitionManager.Find_Partition_By_Path(Mount_Point);
307 if (additional_entry) {
308 LOGINFO("Found an additional entry for '%s'\n", Mount_Point.c_str());
309 }
310 }
311 LOGINFO("Processing '%s'\n", Mount_Point.c_str());
312 Backup_Path = Mount_Point;
313 Storage_Path = Mount_Point;
314 Display_Name = ptr + 1;
315 Backup_Display_Name = Display_Name;
316 Storage_Name = Display_Name;
317 item_index++;
318 } else if (item_index == fs_index) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400319 // File System
320 Fstab_File_System = ptr;
321 Current_File_System = ptr;
322 item_index++;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600323 } else if (item_index == block_device_index) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400324 // Primary Block Device
Dees_Troy38bd7602012-09-14 13:33:53 -0400325 if (Fstab_File_System == "mtd" || Fstab_File_System == "yaffs2") {
Dees_Troy094207a2012-09-26 12:00:39 -0400326 MTD_Name = ptr;
327 Find_MTD_Block_Device(MTD_Name);
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400328 } else if (Fstab_File_System == "bml") {
329 if (Mount_Point == "/boot")
330 MTD_Name = "boot";
331 else if (Mount_Point == "/recovery")
332 MTD_Name = "recovery";
333 Primary_Block_Device = ptr;
334 if (*ptr != '/')
Dees_Troy2673cec2013-04-02 20:22:16 +0000335 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 -0400336 } else if (*ptr != '/') {
Dees_Troy5bf43922012-09-07 16:07:55 -0400337 if (Display_Error)
Matt Mower2b2dd152016-04-26 11:24:08 -0500338 LOGERR("Invalid block device '%s' in fstab line '%s'", ptr, fstab_line);
Dees_Troy5bf43922012-09-07 16:07:55 -0400339 else
Matt Mower2b2dd152016-04-26 11:24:08 -0500340 LOGINFO("Invalid block device '%s' in fstab line '%s'", ptr, fstab_line);
Matt Mower72c87ce2016-04-26 14:34:56 -0500341 return false;
Dees_Troy38bd7602012-09-14 13:33:53 -0400342 } else {
343 Primary_Block_Device = ptr;
344 Find_Real_Block_Device(Primary_Block_Device, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400345 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400346 item_index++;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600347 } else if (item_index > 2) {
348 if (fstab_version == 2) {
349 if (item_index == 3) {
350 Process_FS_Flags(ptr);
351 if (additional_entry) {
352 additional_entry->Save_FS_Flags(Fstab_File_System, Mount_Flags, Mount_Options);
353 return false; // We save the extra fs flags in the other partition entry and by returning false, this entry will be deleted
354 }
355 } else {
356 strlcpy(twflags, ptr, sizeof(twflags));
357 }
358 item_index++;
359 } else if (*ptr == '/') { // v2 fstab does not allow alternate block devices
Dees_Troy5bf43922012-09-07 16:07:55 -0400360 // Alternate Block Device
361 Alternate_Block_Device = ptr;
362 Find_Real_Block_Device(Alternate_Block_Device, Display_Error);
363 } else if (strlen(ptr) > 7 && strncmp(ptr, "length=", 7) == 0) {
364 // Partition length
365 ptr += 7;
366 Length = atoi(ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400367 } else if (strlen(ptr) > 6 && strncmp(ptr, "flags=", 6) == 0) {
368 // Custom flags, save for later so that new values aren't overwritten by defaults
369 ptr += 6;
Matt Mower2416a502016-04-12 19:54:46 -0500370 strlcpy(twflags, ptr, sizeof(twflags));
Dees_Troy38bd7602012-09-14 13:33:53 -0400371 } else if (strlen(ptr) == 4 && (strncmp(ptr, "NULL", 4) == 0 || strncmp(ptr, "null", 4) == 0 || strncmp(ptr, "null", 4) == 0)) {
372 // Do nothing
Dees_Troy5bf43922012-09-07 16:07:55 -0400373 } else {
374 // Unhandled data
Matt Mower2b2dd152016-04-26 11:24:08 -0500375 LOGINFO("Unhandled fstab information '%s' in fstab line '%s'\n", ptr, fstab_line);
Dees_Troy5bf43922012-09-07 16:07:55 -0400376 }
377 }
378 while (index < line_len && full_line[index] != '\0')
379 index++;
380 }
381
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600382 // override block devices from the v2 fstab with the ones we read from the twrp.flags file in case they are different
383 if (fstab_version == 2 && twrp_flags && twrp_flags->size() > 0) {
384 it = twrp_flags->find(Mount_Point);
385 if (it != twrp_flags->end()) {
386 if (!it->second.Primary_Block_Device.empty()) {
387 Primary_Block_Device = it->second.Primary_Block_Device;
388 Find_Real_Block_Device(Primary_Block_Device, Display_Error);
389 }
390 if (!it->second.Alternate_Block_Device.empty()) {
391 Alternate_Block_Device = it->second.Alternate_Block_Device;
392 Find_Real_Block_Device(Alternate_Block_Device, Display_Error);
393 }
394 }
395 }
396
397 if (strncmp(fstab_line, "/devices/", strlen("/devices/")) == 0) {
398 Sysfs_Entry = Primary_Block_Device;
399 Primary_Block_Device = "";
400 Is_Storage = true;
401 Removable = true;
402 Wipe_Available_in_GUI = true;
403 Wildcard_Block_Device = true;
404 }
405 if (Primary_Block_Device.find("*") != string::npos)
406 Wildcard_Block_Device = true;
407
408 if (Mount_Point == "auto") {
409 Mount_Point = "/auto";
410 char autoi[5];
411 sprintf(autoi, "%i", auto_index);
412 Mount_Point += autoi;
413 Backup_Path = Mount_Point;
414 Storage_Path = Mount_Point;
415 auto_index++;
416 Setup_File_System(Display_Error);
417 Display_Name = "Storage";
418 Backup_Display_Name = Display_Name;
419 Storage_Name = Display_Name;
420 Can_Be_Backed_Up = false;
421 Wipe_Available_in_GUI = true;
422 Is_Storage = true;
423 Removable = true;
424 Wipe_Available_in_GUI = true;
425 } else if (!Is_File_System(Fstab_File_System) && !Is_Image(Fstab_File_System)) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400426 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000427 LOGERR("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400428 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000429 LOGINFO("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Matt Mower72c87ce2016-04-26 14:34:56 -0500430 return false;
Dees_Troy5bf43922012-09-07 16:07:55 -0400431 } else if (Is_File_System(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400432 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400433 Setup_File_System(Display_Error);
434 if (Mount_Point == "/system") {
435 Display_Name = "System";
Dees_Troya13d74f2013-03-24 08:54:55 -0500436 Backup_Display_Name = Display_Name;
437 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400438 Wipe_Available_in_GUI = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500439 Can_Be_Backed_Up = true;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500440 Mount_Read_Only = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400441 } else if (Mount_Point == "/data") {
442 Display_Name = "Data";
Dees_Troya13d74f2013-03-24 08:54:55 -0500443 Backup_Display_Name = Display_Name;
444 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400445 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400446 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500447 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000448 Can_Encrypt_Backup = true;
449 Use_Userdata_Encryption = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400450 } else if (Mount_Point == "/cache") {
451 Display_Name = "Cache";
Dees_Troya13d74f2013-03-24 08:54:55 -0500452 Backup_Display_Name = Display_Name;
453 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400454 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400455 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500456 Can_Be_Backed_Up = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400457 } else if (Mount_Point == "/datadata") {
Dees_Troy51127312012-09-08 13:08:49 -0400458 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400459 Display_Name = "DataData";
Dees_Troya13d74f2013-03-24 08:54:55 -0500460 Backup_Display_Name = Display_Name;
461 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400462 Is_SubPartition = true;
463 SubPartition_Of = "/data";
Dees_Troy5bf43922012-09-07 16:07:55 -0400464 DataManager::SetValue(TW_HAS_DATADATA, 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500465 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000466 Can_Encrypt_Backup = true;
467 Use_Userdata_Encryption = false; // This whole partition should be encrypted
Dees_Troy5bf43922012-09-07 16:07:55 -0400468 } else if (Mount_Point == "/sd-ext") {
Dees_Troy51127312012-09-08 13:08:49 -0400469 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400470 Display_Name = "SD-Ext";
Dees_Troya13d74f2013-03-24 08:54:55 -0500471 Backup_Display_Name = Display_Name;
472 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400473 Wipe_Available_in_GUI = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400474 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500475 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000476 Can_Encrypt_Backup = true;
477 Use_Userdata_Encryption = true;
Dees_Troy2c50e182012-09-26 20:05:28 -0400478 } else if (Mount_Point == "/boot") {
479 Display_Name = "Boot";
Dees_Troya13d74f2013-03-24 08:54:55 -0500480 Backup_Display_Name = Display_Name;
Dees_Troy2c50e182012-09-26 20:05:28 -0400481 DataManager::SetValue("tw_boot_is_mountable", 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500482 Can_Be_Backed_Up = true;
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500483 } else if (Mount_Point == "/vendor") {
484 Display_Name = "Vendor";
485 Backup_Display_Name = Display_Name;
486 Storage_Name = Display_Name;
487 Mount_Read_Only = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400488 }
489#ifdef TW_EXTERNAL_STORAGE_PATH
490 if (Mount_Point == EXPAND(TW_EXTERNAL_STORAGE_PATH)) {
491 Is_Storage = true;
492 Storage_Path = EXPAND(TW_EXTERNAL_STORAGE_PATH);
Dees_Troyc51f1f92012-09-20 15:32:13 -0400493 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500494 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400495#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000496 if (Mount_Point == "/sdcard" || Mount_Point == "/external_sd" || Mount_Point == "/external_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400497 Is_Storage = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400498 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500499 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400500#endif
Dees_Troyb05ddee2013-01-28 20:24:50 +0000501 }
Dees_Troy8170a922012-09-18 15:40:25 -0400502#ifdef TW_INTERNAL_STORAGE_PATH
503 if (Mount_Point == EXPAND(TW_INTERNAL_STORAGE_PATH)) {
504 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500505 Is_Settings_Storage = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400506 Storage_Path = EXPAND(TW_INTERNAL_STORAGE_PATH);
Dees_Troya13d74f2013-03-24 08:54:55 -0500507 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400508 }
509#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000510 if (Mount_Point == "/emmc" || Mount_Point == "/internal_sd" || Mount_Point == "/internal_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400511 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500512 Is_Settings_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500513 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400514 }
515#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400516 } else if (Is_Image(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400517 Find_Actual_Block_Device();
Ethan Yonker1b190162016-12-05 15:25:19 -0600518 Setup_Image();
Dees_Troya13d74f2013-03-24 08:54:55 -0500519 if (Mount_Point == "/boot") {
520 Display_Name = "Boot";
521 Backup_Display_Name = Display_Name;
522 Can_Be_Backed_Up = true;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600523 Can_Flash_Img = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500524 } else if (Mount_Point == "/recovery") {
525 Display_Name = "Recovery";
526 Backup_Display_Name = Display_Name;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600527 Can_Flash_Img = true;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500528 } else if (Mount_Point == "/system_image") {
529 Display_Name = "System Image";
530 Backup_Display_Name = Display_Name;
HashBanged974bb2016-01-30 14:20:09 -0500531 Can_Flash_Img = true;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500532 Can_Be_Backed_Up = true;
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500533 } else if (Mount_Point == "/vendor_image") {
534 Display_Name = "Vendor Image";
535 Backup_Display_Name = Display_Name;
HashBanged974bb2016-01-30 14:20:09 -0500536 Can_Flash_Img = true;
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500537 Can_Be_Backed_Up = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500538 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400539 }
540
Matt Mower2416a502016-04-12 19:54:46 -0500541 // Process TWRP fstab flags
542 if (strlen(twflags) > 0) {
543 string Prev_Display_Name = Display_Name;
544 string Prev_Storage_Name = Storage_Name;
545 string Prev_Backup_Display_Name = Backup_Display_Name;
546 Display_Name = "";
547 Storage_Name = "";
548 Backup_Display_Name = "";
549
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600550 Process_TW_Flags(twflags, (fstab_version == 1), fstab_version);
551 Save_FS_Flags(Fstab_File_System, Mount_Flags, Mount_Options);
Matt Mower2416a502016-04-12 19:54:46 -0500552
553 bool has_display_name = !Display_Name.empty();
554 bool has_storage_name = !Storage_Name.empty();
555 bool has_backup_name = !Backup_Display_Name.empty();
556 if (!has_display_name) Display_Name = Prev_Display_Name;
557 if (!has_storage_name) Storage_Name = Prev_Storage_Name;
558 if (!has_backup_name) Backup_Display_Name = Prev_Backup_Display_Name;
559
560 if (has_display_name && !has_storage_name)
561 Storage_Name = Display_Name;
562 if (!has_display_name && has_storage_name)
563 Display_Name = Storage_Name;
564 if (has_display_name && !has_backup_name && Backup_Display_Name != "Android Secure")
565 Backup_Display_Name = Display_Name;
566 if (!has_display_name && has_backup_name)
567 Display_Name = Backup_Display_Name;
568 }
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600569
570 if (fstab_version == 2 && twrp_flags && twrp_flags->size() > 0) {
571 it = twrp_flags->find(Mount_Point);
572 if (it != twrp_flags->end()) {
573 char twrpflags[MAX_FSTAB_LINE_LENGTH] = "";
574 int skip = 0;
575 string Flags = it->second.Flags;
576 strcpy(twrpflags, Flags.c_str());
577 if (strlen(twrpflags) > strlen("flags=") && strncmp(twrpflags, "flags=", strlen("flags=")) == 0)
578 skip += strlen("flags=");
579 char* flagptr = twrpflags;
580 flagptr += skip;
581 Process_TW_Flags(flagptr, Display_Error, 1); // Forcing the fstab to ver 1 because this data is coming from the /etc/twrp.flags which should be using the TWRP v1 flags format
582 }
583 }
Dees_Troy51127312012-09-08 13:08:49 -0400584 return true;
585}
586
Matt Mower72c87ce2016-04-26 14:34:56 -0500587void TWPartition::Partition_Post_Processing(bool Display_Error) {
588 if (Mount_Point == "/data")
589 Setup_Data_Partition(Display_Error);
590 else if (Mount_Point == "/cache")
591 Setup_Cache_Partition(Display_Error);
592}
593
Ethan Yonkerbd7492d2016-12-07 13:55:01 -0600594void TWPartition::ExcludeAll(const string& path) {
595 backup_exclusions.add_absolute_dir(path);
596 wipe_exclusions.add_absolute_dir(path);
597}
598
Matt Mower72c87ce2016-04-26 14:34:56 -0500599void TWPartition::Setup_Data_Partition(bool Display_Error) {
600 if (Mount_Point != "/data")
601 return;
602
603 // Ensure /data is not mounted as tmpfs for qcom hardware decrypt
604 UnMount(false);
605
606#ifdef TW_INCLUDE_CRYPTO
607 if (datamedia)
608 Setup_Data_Media();
609 Can_Be_Encrypted = true;
610 char crypto_blkdev[255];
611 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
612 if (strcmp(crypto_blkdev, "error") != 0) {
613 DataManager::SetValue(TW_IS_DECRYPTED, 1);
614 Is_Encrypted = true;
615 Is_Decrypted = true;
Ethan Yonkerbd7492d2016-12-07 13:55:01 -0600616 Is_FBE = false;
617 DataManager::SetValue(TW_IS_FBE, 0);
Matt Mower72c87ce2016-04-26 14:34:56 -0500618 Decrypted_Block_Device = crypto_blkdev;
619 LOGINFO("Data already decrypted, new block device: '%s'\n", crypto_blkdev);
620 } else if (!Mount(false)) {
621 if (Is_Present) {
622 set_partition_data(Actual_Block_Device.c_str(), Crypto_Key_Location.c_str(), Fstab_File_System.c_str());
623 if (cryptfs_check_footer() == 0) {
624 Is_Encrypted = true;
625 Is_Decrypted = false;
626 Can_Be_Mounted = false;
627 Current_File_System = "emmc";
Ethan Yonker1b190162016-12-05 15:25:19 -0600628 Setup_Image();
Matt Mower72c87ce2016-04-26 14:34:56 -0500629 DataManager::SetValue(TW_IS_ENCRYPTED, 1);
630 DataManager::SetValue(TW_CRYPTO_PWTYPE, cryptfs_get_password_type());
631 DataManager::SetValue(TW_CRYPTO_PASSWORD, "");
632 DataManager::SetValue("tw_crypto_display", "");
633 } else {
634 gui_err("mount_data_footer=Could not mount /data and unable to find crypto footer.");
635 }
636 } else {
637 LOGERR("Primary block device '%s' for mount point '%s' is not present!\n", Primary_Block_Device.c_str(), Mount_Point.c_str());
638 }
639 } else {
Ethan Yonkerbd7492d2016-12-07 13:55:01 -0600640 if (TWFunc::Path_Exists("/data/unencrypted/key/version")) {
641 LOGINFO("File Based Encryption is present\n");
642#ifdef TW_INCLUDE_FBE
643 ExcludeAll(Mount_Point + "/convert_fbe");
644 ExcludeAll(Mount_Point + "/unencrypted");
645 //ExcludeAll(Mount_Point + "/system/users/0"); // we WILL need to retain some of this if multiple users are present or we just need to delete more folders for the extra users somewhere else
646 ExcludeAll(Mount_Point + "/misc/vold/user_keys");
Ethan Yonker79f88bd2016-12-09 14:52:12 -0600647 //ExcludeAll(Mount_Point + "/system_ce");
648 //ExcludeAll(Mount_Point + "/system_de");
649 //ExcludeAll(Mount_Point + "/misc_ce");
650 //ExcludeAll(Mount_Point + "/misc_de");
Ethan Yonkerbd7492d2016-12-07 13:55:01 -0600651 ExcludeAll(Mount_Point + "/system/gatekeeper.password.key");
652 ExcludeAll(Mount_Point + "/system/gatekeeper.pattern.key");
653 ExcludeAll(Mount_Point + "/system/locksettings.db");
654 //ExcludeAll(Mount_Point + "/system/locksettings.db-shm"); // don't seem to need this one, but the other 2 are needed
655 ExcludeAll(Mount_Point + "/system/locksettings.db-wal");
Ethan Yonker79f88bd2016-12-09 14:52:12 -0600656 //ExcludeAll(Mount_Point + "/user_de");
Ethan Yonkerbd7492d2016-12-07 13:55:01 -0600657 //ExcludeAll(Mount_Point + "/misc/profiles/cur/0"); // might be important later
658 ExcludeAll(Mount_Point + "/misc/gatekeeper");
659 ExcludeAll(Mount_Point + "/drm/kek.dat");
660 int retry_count = 3;
661 while (!Decrypt_DE() && --retry_count)
662 usleep(2000);
663 if (retry_count > 0) {
664 property_set("ro.crypto.state", "encrypted");
665 Is_Encrypted = true;
666 Is_Decrypted = false;
667 Is_FBE = true;
668 DataManager::SetValue(TW_IS_FBE, 1);
669 DataManager::SetValue(TW_IS_ENCRYPTED, 1);
670 string filename;
671 DataManager::SetValue(TW_CRYPTO_PWTYPE, Get_Password_Type(0, filename));
672 DataManager::SetValue(TW_CRYPTO_PASSWORD, "");
673 DataManager::SetValue("tw_crypto_display", "");
674 }
675#else
676 LOGERR("FBE found but FBE support not present in TWRP\n");
677#endif
678 } else {
679 // Filesystem is not encrypted and the mount succeeded, so return to
680 // the original unmounted state
681 UnMount(false);
682 }
Matt Mower72c87ce2016-04-26 14:34:56 -0500683 }
684 if (datamedia && (!Is_Encrypted || (Is_Encrypted && Is_Decrypted))) {
685 Setup_Data_Media();
686 Recreate_Media_Folder();
687 }
688#else
689 if (datamedia) {
690 Setup_Data_Media();
691 Recreate_Media_Folder();
692 }
693#endif
694}
695
696void TWPartition::Setup_Cache_Partition(bool Display_Error __unused) {
697 if (Mount_Point != "/cache")
698 return;
699
700 if (!Mount(true))
701 return;
702
703 if (!TWFunc::Path_Exists("/cache/recovery/.")) {
704 LOGINFO("Recreating /cache/recovery folder\n");
705 if (mkdir("/cache/recovery", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0)
706 LOGERR("Could not create /cache/recovery\n");
707 }
708}
709
Matt Mower4ab42b12016-04-21 13:52:18 -0500710void TWPartition::Process_FS_Flags(const char *str) {
711 char *options = strdup(str);
712 char *ptr, *savep;
Hashcode62bd9e02013-11-19 21:59:42 -0800713
Matt Mower4ab42b12016-04-21 13:52:18 -0500714 Mount_Options = "";
Hashcode62bd9e02013-11-19 21:59:42 -0800715
Matt Mower4ab42b12016-04-21 13:52:18 -0500716 // Avoid issues with potentially nested strtok by using strtok_r
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600717 for (ptr = strtok_r(options, ",", &savep); ptr; ptr = strtok_r(NULL, ",", &savep)) {
718 char *equals = strstr(ptr, "=");
719 size_t name_len;
Matt Mower4ab42b12016-04-21 13:52:18 -0500720
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600721 if (!equals)
722 name_len = strlen(ptr);
723 else
724 name_len = equals - ptr;
725
726 // There are some flags that we want to ignore in TWRP
727 bool found_match = false;
728 for (const char** ignored_mount_item = ignored_mount_items; *ignored_mount_item; ignored_mount_item++) {
729 if (strncmp(ptr, *ignored_mount_item, name_len) == 0) {
730 found_match = true;
Hashcode62bd9e02013-11-19 21:59:42 -0800731 break;
732 }
733 }
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600734 if (found_match)
735 continue;
Hashcode62bd9e02013-11-19 21:59:42 -0800736
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600737 // mount_flags are never postfixed by '='
738 if (!equals) {
739 const struct flag_list* mount_flag = mount_flags;
740 for (; mount_flag->name; mount_flag++) {
741 if (strcmp(ptr, mount_flag->name) == 0) {
742 if (mount_flag->flag == MS_RDONLY)
743 Mount_Read_Only = true;
744 else
745 Mount_Flags |= (unsigned)mount_flag->flag;
746 found_match = true;
747 break;
748 }
749 }
750 if (found_match)
751 continue;
Matt Mower4ab42b12016-04-21 13:52:18 -0500752 }
753
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600754 // If we aren't ignoring this flag and it's not a mount flag, then it must be a mount option
755 if (!Mount_Options.empty())
756 Mount_Options += ",";
757 Mount_Options += ptr;
Matt Mower4ab42b12016-04-21 13:52:18 -0500758 }
759 free(options);
Hashcode62bd9e02013-11-19 21:59:42 -0800760}
761
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600762void TWPartition::Save_FS_Flags(const string& local_File_System, int local_Mount_Flags, const string& local_Mount_Options) {
763 partition_fs_flags_struct flags;
764 flags.File_System = local_File_System;
765 flags.Mount_Flags = local_Mount_Flags;
766 flags.Mount_Options = local_Mount_Options;
767 fs_flags.push_back(flags);
768}
769
Matt Mower2416a502016-04-12 19:54:46 -0500770void TWPartition::Apply_TW_Flag(const unsigned flag, const char* str, const bool val) {
771 switch (flag) {
772 case TWFLAG_ANDSEC:
773 Has_Android_Secure = val;
774 break;
775 case TWFLAG_BACKUP:
776 Can_Be_Backed_Up = val;
777 break;
778 case TWFLAG_BACKUPNAME:
779 Backup_Display_Name = str;
780 break;
781 case TWFLAG_BLOCKSIZE:
782 Format_Block_Size = (unsigned long)(atol(str));
783 break;
784 case TWFLAG_CANBEWIPED:
785 Can_Be_Wiped = val;
786 break;
787 case TWFLAG_CANENCRYPTBACKUP:
788 Can_Encrypt_Backup = val;
789 break;
790 case TWFLAG_DEFAULTS:
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600791 case TWFLAG_WAIT:
792 case TWFLAG_VERIFY:
793 case TWFLAG_CHECK:
794 case TWFLAG_NOTRIM:
795 case TWFLAG_VOLDMANAGED:
796 case TWFLAG_RESIZE:
Matt Mower2416a502016-04-12 19:54:46 -0500797 // Do nothing
798 break;
799 case TWFLAG_DISPLAY:
800 Display_Name = str;
801 break;
802 case TWFLAG_ENCRYPTABLE:
803 case TWFLAG_FORCEENCRYPT:
804 Crypto_Key_Location = str;
805 break;
806 case TWFLAG_FLASHIMG:
807 Can_Flash_Img = val;
808 break;
809 case TWFLAG_FSFLAGS:
Matt Mower4ab42b12016-04-21 13:52:18 -0500810 Process_FS_Flags(str);
Matt Mower2416a502016-04-12 19:54:46 -0500811 break;
812 case TWFLAG_IGNOREBLKID:
813 Ignore_Blkid = val;
814 break;
815 case TWFLAG_LENGTH:
816 Length = atoi(str);
817 break;
818 case TWFLAG_MOUNTTODECRYPT:
819 Mount_To_Decrypt = val;
820 break;
821 case TWFLAG_REMOVABLE:
822 Removable = val;
823 break;
824 case TWFLAG_RETAINLAYOUTVERSION:
825 Retain_Layout_Version = val;
826 break;
827 case TWFLAG_SETTINGSSTORAGE:
828 Is_Settings_Storage = val;
829 if (Is_Settings_Storage)
830 Is_Storage = true;
831 break;
832 case TWFLAG_STORAGE:
833 Is_Storage = val;
834 break;
835 case TWFLAG_STORAGENAME:
836 Storage_Name = str;
837 break;
838 case TWFLAG_SUBPARTITIONOF:
839 Is_SubPartition = true;
840 SubPartition_Of = str;
841 break;
842 case TWFLAG_SYMLINK:
843 Symlink_Path = str;
844 break;
845 case TWFLAG_USERDATAENCRYPTBACKUP:
846 Use_Userdata_Encryption = val;
847 if (Use_Userdata_Encryption)
848 Can_Encrypt_Backup = true;
849 break;
850 case TWFLAG_USERMRF:
851 Use_Rm_Rf = val;
852 break;
853 case TWFLAG_WIPEDURINGFACTORYRESET:
854 Wipe_During_Factory_Reset = val;
855 if (Wipe_During_Factory_Reset) {
856 Can_Be_Wiped = true;
857 Wipe_Available_in_GUI = true;
858 }
859 break;
860 case TWFLAG_WIPEINGUI:
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600861 case TWFLAG_FORMATTABLE:
Matt Mower2416a502016-04-12 19:54:46 -0500862 Wipe_Available_in_GUI = val;
863 if (Wipe_Available_in_GUI)
864 Can_Be_Wiped = true;
865 break;
Ethan Yonker1b190162016-12-05 15:25:19 -0600866 case TWFLAG_SLOTSELECT:
867 SlotSelect = true;
868 break;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600869 case TWFLAG_ALTDEVICE:
870 Alternate_Block_Device = str;
871 break;
Matt Mower2416a502016-04-12 19:54:46 -0500872 default:
873 // Should not get here
874 LOGINFO("Flag identified for processing, but later unmatched: %i\n", flag);
875 break;
876 }
877}
Dees_Troy51127312012-09-08 13:08:49 -0400878
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600879void TWPartition::Process_TW_Flags(char *flags, bool Display_Error, int fstab_ver) {
Matt Mower2416a502016-04-12 19:54:46 -0500880 char separator[2] = {'\n', 0};
881 char *ptr, *savep;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600882 char source_separator = ';';
883
884 if (fstab_ver == 2)
885 source_separator = ',';
Matt Mower2416a502016-04-12 19:54:46 -0500886
887 // Semicolons within double-quotes are not forbidden, so replace
888 // only the semicolons intended as separators with '\n' for strtok
889 for (unsigned i = 0, skip = 0; i < strlen(flags); i++) {
890 if (flags[i] == '\"')
Dees_Troya13d74f2013-03-24 08:54:55 -0500891 skip = !skip;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600892 if (!skip && flags[i] == source_separator)
Matt Mower2416a502016-04-12 19:54:46 -0500893 flags[i] = separator[0];
Dees_Troy51127312012-09-08 13:08:49 -0400894 }
895
Matt Mower2416a502016-04-12 19:54:46 -0500896 // Avoid issues with potentially nested strtok by using strtok_r
897 ptr = strtok_r(flags, separator, &savep);
898 while (ptr) {
899 int ptr_len = strlen(ptr);
900 const struct flag_list* tw_flag = tw_flags;
Hashcode62bd9e02013-11-19 21:59:42 -0800901
Matt Mower2416a502016-04-12 19:54:46 -0500902 for (; tw_flag->name; tw_flag++) {
903 int flag_len = strlen(tw_flag->name);
904
905 if (strncmp(ptr, tw_flag->name, flag_len) == 0) {
906 bool flag_val = false;
907
908 if (ptr_len > flag_len && (tw_flag->name)[flag_len-1] != '='
909 && ptr[flag_len] != '=') {
910 // Handle flags with same starting string
911 // (e.g. backup and backupname)
912 continue;
913 } else if (ptr_len > flag_len && ptr[flag_len] == '=') {
914 // Handle flags with dual format: Part 1
915 // (e.g. backup and backup=y. backup=y handled here)
916 ptr += flag_len + 1;
917 TWFunc::Strip_Quotes(ptr);
918 // Skip flags with empty argument
919 // (e.g. backup=)
920 if (strlen(ptr) == 0) {
921 LOGINFO("Flag missing argument or should not include '=': %s=\n", tw_flag->name);
922 break;
923 }
924 flag_val = strchr("yY1", *ptr) != NULL;
925 } else if (ptr_len == flag_len
926 && (tw_flag->name)[flag_len-1] == '=') {
927 // Skip flags missing argument after =
928 // (e.g. backupname=)
929 LOGINFO("Flag missing argument: %s\n", tw_flag->name);
930 break;
931 } else if (ptr_len > flag_len
932 && (tw_flag->name)[flag_len-1] == '=') {
933 // Handle arguments to flags
934 // (e.g. backupname="My Stuff")
935 ptr += flag_len;
936 TWFunc::Strip_Quotes(ptr);
937 // Skip flags with empty argument
938 // (e.g. backupname="")
939 if (strlen(ptr) == 0) {
940 LOGINFO("Flag missing argument: %s\n", tw_flag->name);
941 break;
942 }
943 } else if (ptr_len == flag_len) {
944 // Handle flags with dual format: Part 2
945 // (e.g. backup and backup=y. backup handled here)
946 flag_val = true;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600947 } else {
Matt Mower2416a502016-04-12 19:54:46 -0500948 LOGINFO("Flag matched, but could not be processed: %s\n", ptr);
949 break;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600950 }
Matt Mower2416a502016-04-12 19:54:46 -0500951
952 Apply_TW_Flag(tw_flag->flag, ptr, flag_val);
953 break;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600954 }
Matt Mower2416a502016-04-12 19:54:46 -0500955 }
956 if (tw_flag->name == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400957 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000958 LOGERR("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400959 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000960 LOGINFO("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400961 }
Matt Mower2416a502016-04-12 19:54:46 -0500962 ptr = strtok_r(NULL, separator, &savep);
Dees_Troy51127312012-09-08 13:08:49 -0400963 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400964}
965
Dees_Troy5bf43922012-09-07 16:07:55 -0400966bool TWPartition::Is_File_System(string File_System) {
967 if (File_System == "ext2" ||
Dees_Troy63c8df72012-09-10 14:02:05 -0400968 File_System == "ext3" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400969 File_System == "ext4" ||
970 File_System == "vfat" ||
971 File_System == "ntfs" ||
972 File_System == "yaffs2" ||
bigbiff bigbiff3e146522012-11-14 14:32:59 -0500973 File_System == "exfat" ||
Dees_Troye5017042013-08-29 16:38:55 +0000974 File_System == "f2fs" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400975 File_System == "auto")
976 return true;
977 else
978 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400979}
980
Dees_Troy5bf43922012-09-07 16:07:55 -0400981bool TWPartition::Is_Image(string File_System) {
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400982 if (File_System == "emmc" || File_System == "mtd" || File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -0400983 return true;
984 else
985 return false;
986}
987
Dees_Troy51127312012-09-08 13:08:49 -0400988bool TWPartition::Make_Dir(string Path, bool Display_Error) {
Ethan Yonker93ac7a02016-11-07 22:05:58 -0600989 if (TWFunc::Get_D_Type_From_Stat(Path) != S_IFDIR)
990 unlink(Path.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400991 if (!TWFunc::Path_Exists(Path)) {
Dees_Troy51127312012-09-08 13:08:49 -0400992 if (mkdir(Path.c_str(), 0777) == -1) {
993 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -0500994 gui_msg(Msg(msg::kError, "create_folder_strerr=Can not create '{1}' folder ({2}).")(Path)(strerror(errno)));
Dees_Troy51127312012-09-08 13:08:49 -0400995 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000996 LOGINFO("Can not create '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400997 return false;
998 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000999 LOGINFO("Created '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -04001000 return true;
1001 }
1002 }
1003 return true;
1004}
1005
Dees_Troy5bf43922012-09-07 16:07:55 -04001006void TWPartition::Setup_File_System(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001007 Can_Be_Mounted = true;
1008 Can_Be_Wiped = true;
1009
Dees_Troy5bf43922012-09-07 16:07:55 -04001010 // Make the mount point folder if it doesn't exist
Dees_Troy51127312012-09-08 13:08:49 -04001011 Make_Dir(Mount_Point, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -04001012 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
1013 Backup_Name = Display_Name;
bigbiffce8f83c2015-12-12 18:30:21 -05001014 Backup_Method = BM_FILES;
Dees_Troy5bf43922012-09-07 16:07:55 -04001015}
1016
Ethan Yonker1b190162016-12-05 15:25:19 -06001017void TWPartition::Setup_Image() {
Dees_Troy5bf43922012-09-07 16:07:55 -04001018 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
1019 Backup_Name = Display_Name;
Gary Peck82599a82012-11-21 16:23:12 -08001020 if (Current_File_System == "emmc")
bigbiffce8f83c2015-12-12 18:30:21 -05001021 Backup_Method = BM_DD;
Gary Peck82599a82012-11-21 16:23:12 -08001022 else if (Current_File_System == "mtd" || Current_File_System == "bml")
bigbiffce8f83c2015-12-12 18:30:21 -05001023 Backup_Method = BM_FLASH_UTILS;
Dees_Troy5bf43922012-09-07 16:07:55 -04001024 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001025 LOGINFO("Unhandled file system '%s' on image '%s'\n", Current_File_System.c_str(), Display_Name.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -04001026}
1027
Dees_Troye58d5262012-09-21 12:27:57 -04001028void TWPartition::Setup_AndSec(void) {
Dees_Troya13d74f2013-03-24 08:54:55 -05001029 Backup_Display_Name = "Android Secure";
Dees_Troye58d5262012-09-21 12:27:57 -04001030 Backup_Name = "and-sec";
Dees_Troya13d74f2013-03-24 08:54:55 -05001031 Can_Be_Backed_Up = true;
Dees_Troye58d5262012-09-21 12:27:57 -04001032 Has_Android_Secure = true;
1033 Symlink_Path = Mount_Point + "/.android_secure";
1034 Symlink_Mount_Point = "/and-sec";
1035 Backup_Path = Symlink_Mount_Point;
1036 Make_Dir("/and-sec", true);
1037 Recreate_AndSec_Folder();
Kjell Braden3126a112016-06-19 16:58:15 +00001038 Mount_Storage_Retry(true);
Dees_Troye58d5262012-09-21 12:27:57 -04001039}
1040
that9e0593e2014-10-08 00:01:24 +02001041void TWPartition::Setup_Data_Media() {
Ethan Yonker6277c792014-09-15 14:54:30 -05001042 LOGINFO("Setting up '%s' as data/media emulated storage.\n", Mount_Point.c_str());
Ethan Yonker66a19492015-12-10 10:19:45 -06001043 if (Storage_Name.empty() || Storage_Name == "Data")
1044 Storage_Name = "Internal Storage";
Ethan Yonker6277c792014-09-15 14:54:30 -05001045 Has_Data_Media = true;
1046 Is_Storage = true;
Ethan Yonker66a19492015-12-10 10:19:45 -06001047 Storage_Path = Mount_Point + "/media";
Ethan Yonker6277c792014-09-15 14:54:30 -05001048 Symlink_Path = Storage_Path;
Ethan Yonker66a19492015-12-10 10:19:45 -06001049 if (Mount_Point == "/data") {
1050 Is_Settings_Storage = true;
1051 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
1052 Make_Dir("/emmc", false);
1053 Symlink_Mount_Point = "/emmc";
1054 } else {
1055 Make_Dir("/sdcard", false);
1056 Symlink_Mount_Point = "/sdcard";
1057 }
1058 if (Mount(false) && TWFunc::Path_Exists(Mount_Point + "/media/0")) {
1059 Storage_Path = Mount_Point + "/media/0";
1060 Symlink_Path = Storage_Path;
1061 DataManager::SetValue(TW_INTERNAL_PATH, Mount_Point + "/media/0");
1062 UnMount(true);
1063 }
1064 DataManager::SetValue("tw_has_internal", 1);
1065 DataManager::SetValue("tw_has_data_media", 1);
Ethan Yonker3fdcda42016-11-30 12:29:37 -06001066 backup_exclusions.add_absolute_dir("/data/data/com.google.android.music/files");
Ethan Yonker6355b562017-05-01 09:42:17 -05001067 wipe_exclusions.add_absolute_dir(Mount_Point + "/misc/vold"); // adopted storage keys
Ethan Yonker79f88bd2016-12-09 14:52:12 -06001068 ExcludeAll(Mount_Point + "/.layout_version");
1069 ExcludeAll(Mount_Point + "/system/storage.xml");
Ethan Yonker6277c792014-09-15 14:54:30 -05001070 } else {
Ethan Yonker66a19492015-12-10 10:19:45 -06001071 if (Mount(true) && TWFunc::Path_Exists(Mount_Point + "/media/0")) {
1072 Storage_Path = Mount_Point + "/media/0";
1073 Symlink_Path = Storage_Path;
1074 UnMount(true);
1075 }
Ethan Yonker6277c792014-09-15 14:54:30 -05001076 }
Ethan Yonker79f88bd2016-12-09 14:52:12 -06001077 ExcludeAll(Mount_Point + "/media");
Ethan Yonker6277c792014-09-15 14:54:30 -05001078}
1079
Dees_Troy5bf43922012-09-07 16:07:55 -04001080void TWPartition::Find_Real_Block_Device(string& Block, bool Display_Error) {
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06001081 char device[PATH_MAX], realDevice[PATH_MAX];
Dees_Troy5bf43922012-09-07 16:07:55 -04001082
1083 strcpy(device, Block.c_str());
1084 memset(realDevice, 0, sizeof(realDevice));
1085 while (readlink(device, realDevice, sizeof(realDevice)) > 0)
1086 {
1087 strcpy(device, realDevice);
1088 memset(realDevice, 0, sizeof(realDevice));
1089 }
1090
1091 if (device[0] != '/') {
1092 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001093 LOGERR("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001094 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001095 LOGINFO("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001096 return;
Dees_Troy51a0e822012-09-05 15:24:24 -04001097 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -04001098 Block = device;
1099 return;
1100 }
1101}
1102
Kjell Braden3126a112016-06-19 16:58:15 +00001103bool TWPartition::Mount_Storage_Retry(bool Display_Error) {
Dees_Troy8e337f32012-10-13 22:07:49 -04001104 // On some devices, storage doesn't want to mount right away, retry and sleep
Kjell Braden3126a112016-06-19 16:58:15 +00001105 if (!Mount(Display_Error)) {
Dees_Troy8e337f32012-10-13 22:07:49 -04001106 int retry_count = 5;
1107 while (retry_count > 0 && !Mount(false)) {
1108 usleep(500000);
1109 retry_count--;
1110 }
Kjell Braden3126a112016-06-19 16:58:15 +00001111 return Mount(Display_Error);
Dees_Troy8e337f32012-10-13 22:07:49 -04001112 }
Kjell Braden3126a112016-06-19 16:58:15 +00001113 return true;
Dees_Troy8e337f32012-10-13 22:07:49 -04001114}
1115
Dees_Troy38bd7602012-09-14 13:33:53 -04001116bool TWPartition::Find_MTD_Block_Device(string MTD_Name) {
1117 FILE *fp = NULL;
1118 char line[255];
1119
1120 fp = fopen("/proc/mtd", "rt");
1121 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001122 LOGERR("Device does not support /proc/mtd\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001123 return false;
1124 }
1125
1126 while (fgets(line, sizeof(line), fp) != NULL)
1127 {
1128 char device[32], label[32];
1129 unsigned long size = 0;
Dees_Troy38bd7602012-09-14 13:33:53 -04001130 int deviceId;
1131
1132 sscanf(line, "%s %lx %*s %*c%s", device, &size, label);
1133
1134 // Skip header and blank lines
1135 if ((strcmp(device, "dev:") == 0) || (strlen(line) < 8))
1136 continue;
1137
1138 // Strip off the trailing " from the label
1139 label[strlen(label)-1] = '\0';
1140
1141 if (strcmp(label, MTD_Name.c_str()) == 0) {
1142 // We found our device
1143 // Strip off the trailing : from the device
1144 device[strlen(device)-1] = '\0';
1145 if (sscanf(device,"mtd%d", &deviceId) == 1) {
1146 sprintf(device, "/dev/block/mtdblock%d", deviceId);
1147 Primary_Block_Device = device;
Dees_Troy76543db2013-06-19 16:24:30 +00001148 fclose(fp);
1149 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -04001150 }
1151 }
1152 }
1153 fclose(fp);
1154
1155 return false;
1156}
1157
Dees_Troy51127312012-09-08 13:08:49 -04001158bool TWPartition::Get_Size_Via_statfs(bool Display_Error) {
1159 struct statfs st;
1160 string Local_Path = Mount_Point + "/.";
1161
1162 if (!Mount(Display_Error))
1163 return false;
1164
1165 if (statfs(Local_Path.c_str(), &st) != 0) {
1166 if (!Removable) {
1167 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001168 LOGERR("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -04001169 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001170 LOGINFO("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -04001171 }
1172 return false;
1173 }
1174 Size = (st.f_blocks * st.f_bsize);
1175 Used = ((st.f_blocks - st.f_bfree) * st.f_bsize);
1176 Free = (st.f_bfree * st.f_bsize);
1177 Backup_Size = Used;
1178 return true;
1179}
1180
1181bool TWPartition::Get_Size_Via_df(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001182 FILE* fp;
1183 char command[255], line[512];
1184 int include_block = 1;
1185 unsigned int min_len;
1186
1187 if (!Mount(Display_Error))
1188 return false;
1189
Dees_Troy38bd7602012-09-14 13:33:53 -04001190 min_len = Actual_Block_Device.size() + 2;
Dees_Troy51127312012-09-08 13:08:49 -04001191 sprintf(command, "df %s > /tmp/dfoutput.txt", Mount_Point.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001192 TWFunc::Exec_Cmd(command);
Dees_Troy51127312012-09-08 13:08:49 -04001193 fp = fopen("/tmp/dfoutput.txt", "rt");
1194 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001195 LOGINFO("Unable to open /tmp/dfoutput.txt.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001196 return false;
Dees_Troy51127312012-09-08 13:08:49 -04001197 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001198
1199 while (fgets(line, sizeof(line), fp) != NULL)
1200 {
1201 unsigned long blocks, used, available;
1202 char device[64];
1203 char tmpString[64];
1204
1205 if (strncmp(line, "Filesystem", 10) == 0)
1206 continue;
1207 if (strlen(line) < min_len) {
1208 include_block = 0;
1209 continue;
1210 }
1211 if (include_block) {
1212 sscanf(line, "%s %lu %lu %lu", device, &blocks, &used, &available);
1213 } else {
1214 // The device block string is so long that the df information is on the next line
1215 int space_count = 0;
Dees_Troye58d5262012-09-21 12:27:57 -04001216 sprintf(tmpString, "/dev/block/%s", Actual_Block_Device.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001217 while (tmpString[space_count] == 32)
1218 space_count++;
1219 sscanf(line + space_count, "%lu %lu %lu", &blocks, &used, &available);
1220 }
1221
1222 // Adjust block size to byte size
1223 Size = blocks * 1024ULL;
1224 Used = used * 1024ULL;
1225 Free = available * 1024ULL;
1226 Backup_Size = Used;
1227 }
1228 fclose(fp);
1229 return true;
1230}
1231
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001232unsigned long long TWPartition::IOCTL_Get_Block_Size() {
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001233 Find_Actual_Block_Device();
Ethan Yonker483e9f42016-01-11 22:21:18 -06001234
1235 return TWFunc::IOCTL_Get_Block_Size(Actual_Block_Device.c_str());
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001236}
1237
Dees_Troy5bf43922012-09-07 16:07:55 -04001238bool TWPartition::Find_Partition_Size(void) {
1239 FILE* fp;
1240 char line[512];
1241 string tmpdevice;
1242
igoriok87e3d932013-01-31 21:03:53 +02001243 fp = fopen("/proc/dumchar_info", "rt");
1244 if (fp != NULL) {
1245 while (fgets(line, sizeof(line), fp) != NULL)
1246 {
1247 char label[32], device[32];
1248 unsigned long size = 0;
1249
thatd43bf2d2014-09-21 23:13:02 +02001250 sscanf(line, "%s %lx %*x %*u %s", label, &size, device);
igoriok87e3d932013-01-31 21:03:53 +02001251
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05001252 // Skip header, annotation and blank lines
igoriok87e3d932013-01-31 21:03:53 +02001253 if ((strncmp(device, "/dev/", 5) != 0) || (strlen(line) < 8))
1254 continue;
1255
1256 tmpdevice = "/dev/";
1257 tmpdevice += label;
1258 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
1259 Size = size;
1260 fclose(fp);
1261 return true;
1262 }
1263 }
1264 }
1265
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001266 unsigned long long ioctl_size = IOCTL_Get_Block_Size();
1267 if (ioctl_size) {
1268 Size = ioctl_size;
1269 return true;
1270 }
1271
Dees_Troy5bf43922012-09-07 16:07:55 -04001272 // In this case, we'll first get the partitions we care about (with labels)
1273 fp = fopen("/proc/partitions", "rt");
1274 if (fp == NULL)
1275 return false;
1276
1277 while (fgets(line, sizeof(line), fp) != NULL)
1278 {
1279 unsigned long major, minor, blocks;
1280 char device[512];
Dees_Troy5bf43922012-09-07 16:07:55 -04001281
Dees_Troy63c8df72012-09-10 14:02:05 -04001282 if (strlen(line) < 7 || line[0] == 'm') continue;
Dees_Troy5bf43922012-09-07 16:07:55 -04001283 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
1284
1285 tmpdevice = "/dev/block/";
1286 tmpdevice += device;
Dees_Troy38bd7602012-09-14 13:33:53 -04001287 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001288 // Adjust block size to byte size
1289 Size = blocks * 1024ULL;
1290 fclose(fp);
1291 return true;
1292 }
1293 }
1294 fclose(fp);
1295 return false;
1296}
1297
Dees_Troy5bf43922012-09-07 16:07:55 -04001298bool TWPartition::Is_Mounted(void) {
1299 if (!Can_Be_Mounted)
1300 return false;
1301
1302 struct stat st1, st2;
1303 string test_path;
1304
1305 // Check to see if the mount point directory exists
1306 test_path = Mount_Point + "/.";
1307 if (stat(test_path.c_str(), &st1) != 0) return false;
1308
1309 // Check to see if the directory above the mount point exists
1310 test_path = Mount_Point + "/../.";
1311 if (stat(test_path.c_str(), &st2) != 0) return false;
1312
1313 // Compare the device IDs -- if they match then we're (probably) using tmpfs instead of an actual device
1314 int ret = (st1.st_dev != st2.st_dev) ? true : false;
1315
1316 return ret;
1317}
1318
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05001319bool TWPartition::Is_File_System_Writable(void) {
1320 if (!Is_File_System(Current_File_System) || !Is_Mounted())
1321 return false;
1322
1323 string test_path = Mount_Point + "/.";
1324 return (access(test_path.c_str(), W_OK) == 0);
1325}
1326
Dees_Troy5bf43922012-09-07 16:07:55 -04001327bool TWPartition::Mount(bool Display_Error) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001328 int exfat_mounted = 0;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001329 unsigned long flags = Mount_Flags;
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001330
Dees_Troy5bf43922012-09-07 16:07:55 -04001331 if (Is_Mounted()) {
1332 return true;
1333 } else if (!Can_Be_Mounted) {
1334 return false;
1335 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001336
1337 Find_Actual_Block_Device();
1338
1339 // Check the current file system before mounting
1340 Check_FS_Type();
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001341 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/exfat-fuse")) {
Dees_Troye34c1332013-02-06 19:13:00 +00001342 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 +00001343 LOGINFO("cmd: %s\n", cmd.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001344 string result;
1345 if (TWFunc::Exec_Cmd(cmd, result) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001346 LOGINFO("exfat-fuse failed to mount with result '%s', trying vfat\n", result.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001347 Current_File_System = "vfat";
1348 } else {
1349#ifdef TW_NO_EXFAT_FUSE
1350 UnMount(false);
1351 // We'll let the kernel handle it but using exfat-fuse to detect if the file system is actually exfat
1352 // Some kernels let us mount vfat as exfat which doesn't work out too well
1353#else
1354 exfat_mounted = 1;
1355#endif
1356 }
1357 }
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001358
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001359 if (Current_File_System == "ntfs" && (TWFunc::Path_Exists("/sbin/ntfs-3g") || TWFunc::Path_Exists("/sbin/mount.ntfs"))) {
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001360 string cmd;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001361 string Ntfsmount_Binary = "";
1362
1363 if (TWFunc::Path_Exists("/sbin/ntfs-3g"))
1364 Ntfsmount_Binary = "ntfs-3g";
1365 else if (TWFunc::Path_Exists("/sbin/mount.ntfs"))
1366 Ntfsmount_Binary = "mount.ntfs";
1367
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001368 if (Mount_Read_Only)
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001369 cmd = "/sbin/" + Ntfsmount_Binary + " -o ro " + Actual_Block_Device + " " + Mount_Point;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001370 else
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001371 cmd = "/sbin/" + Ntfsmount_Binary + " " + Actual_Block_Device + " " + Mount_Point;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001372 LOGINFO("cmd: '%s'\n", cmd.c_str());
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001373
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001374 if (TWFunc::Exec_Cmd(cmd) == 0) {
1375 return true;
1376 } else {
1377 LOGINFO("ntfs-3g failed to mount, trying regular mount method.\n");
1378 }
1379 }
1380
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001381 if (Mount_Read_Only)
1382 flags |= MS_RDONLY;
1383
Dees_Troy22042032012-12-18 21:23:08 +00001384 if (Fstab_File_System == "yaffs2") {
1385 // mount an MTD partition as a YAFFS2 filesystem.
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001386 flags = MS_NOATIME | MS_NODEV | MS_NODIRATIME;
1387 if (Mount_Read_Only)
1388 flags |= MS_RDONLY;
Dees_Troy76543db2013-06-19 16:24:30 +00001389 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags, NULL) < 0) {
1390 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags | MS_RDONLY, NULL) < 0) {
1391 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001392 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy76543db2013-06-19 16:24:30 +00001393 else
1394 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
1395 return false;
1396 } else {
1397 LOGINFO("Mounted '%s' (MTD) as RO\n", Mount_Point.c_str());
1398 return true;
1399 }
1400 } else {
1401 struct stat st;
1402 string test_path = Mount_Point;
1403 if (stat(test_path.c_str(), &st) < 0) {
1404 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001405 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy76543db2013-06-19 16:24:30 +00001406 else
1407 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
1408 return false;
1409 }
1410 mode_t new_mode = st.st_mode | S_IXUSR | S_IXGRP | S_IXOTH;
1411 if (new_mode != st.st_mode) {
1412 LOGINFO("Fixing execute permissions for %s\n", Mount_Point.c_str());
1413 if (chmod(Mount_Point.c_str(), new_mode) < 0) {
1414 if (Display_Error)
1415 LOGERR("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
1416 else
1417 LOGINFO("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
1418 return false;
1419 }
1420 }
Dees_Troy22042032012-12-18 21:23:08 +00001421 return true;
Dees_Troy76543db2013-06-19 16:24:30 +00001422 }
thatc7572eb2015-03-31 18:55:30 +02001423 }
1424
1425 string mount_fs = Current_File_System;
1426 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sys/module/texfat"))
1427 mount_fs = "texfat";
1428
1429 if (!exfat_mounted &&
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001430 mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), mount_fs.c_str(), flags, Mount_Options.c_str()) != 0 &&
1431 mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), mount_fs.c_str(), flags, NULL) != 0) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001432#ifdef TW_NO_EXFAT_FUSE
1433 if (Current_File_System == "exfat") {
Dees_Troy2673cec2013-04-02 20:22:16 +00001434 LOGINFO("Mounting exfat failed, trying vfat...\n");
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001435 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), "vfat", 0, NULL) != 0) {
Dees_Troy85f44ed2013-01-09 18:42:36 +00001436 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001437 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy85f44ed2013-01-09 18:42:36 +00001438 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001439 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001440 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 +00001441 return false;
Dees_Troy85f44ed2013-01-09 18:42:36 +00001442 }
Dees_Troyb05ddee2013-01-28 20:24:50 +00001443 } else {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001444#endif
bigbiff bigbiff26774a02014-03-29 18:22:00 -04001445 if (!Removable && Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001446 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001447 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001448 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
1449 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 +00001450 return false;
1451#ifdef TW_NO_EXFAT_FUSE
Dees_Troy85f44ed2013-01-09 18:42:36 +00001452 }
1453#endif
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001454 }
Ethan Yonker253368a2014-11-25 15:00:52 -06001455
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001456 if (Removable)
1457 Update_Size(Display_Error);
1458
xiaolu9416f4f2015-06-04 08:22:23 +08001459 if (!Symlink_Mount_Point.empty() && TWFunc::Path_Exists(Symlink_Path)) {
codelover2a3d4ce2015-03-14 20:26:49 +08001460 string Command = "mount -o bind '" + Symlink_Path + "' '" + Symlink_Mount_Point + "'";
Vojtech Bocek05534202013-09-11 08:11:56 +02001461 TWFunc::Exec_Cmd(Command);
Dees_Troy5bf43922012-09-07 16:07:55 -04001462 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001463 return true;
1464}
1465
1466bool TWPartition::UnMount(bool Display_Error) {
1467 if (Is_Mounted()) {
1468 int never_unmount_system;
1469
1470 DataManager::GetValue(TW_DONT_UNMOUNT_SYSTEM, never_unmount_system);
1471 if (never_unmount_system == 1 && Mount_Point == "/system")
1472 return true; // Never unmount system if you're not supposed to unmount it
1473
Matt Mowera8e6d832017-02-14 20:20:59 -06001474 if (Is_Storage && MTP_Storage_ID > 0)
Ethan Yonker726a0202014-12-16 20:01:38 -06001475 PartitionManager.Remove_MTP_Storage(MTP_Storage_ID);
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001476
Dees_Troy38bd7602012-09-14 13:33:53 -04001477 if (!Symlink_Mount_Point.empty())
1478 umount(Symlink_Mount_Point.c_str());
1479
Dees_Troyb05ddee2013-01-28 20:24:50 +00001480 umount(Mount_Point.c_str());
1481 if (Is_Mounted()) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001482 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001483 gui_msg(Msg(msg::kError, "fail_unmount=Failed to unmount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy5bf43922012-09-07 16:07:55 -04001484 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001485 LOGINFO("Unable to unmount '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001486 return false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001487 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -04001488 return true;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001489 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001490 } else {
1491 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001492 }
1493}
1494
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05001495bool TWPartition::ReMount(bool Display_Error) {
1496 if (UnMount(Display_Error))
1497 return Mount(Display_Error);
1498 return false;
1499}
1500
1501bool TWPartition::ReMount_RW(bool Display_Error) {
1502 // No need to remount if already mounted rw
1503 if (Is_File_System_Writable())
1504 return true;
1505
1506 bool ro = Mount_Read_Only;
1507 int flags = Mount_Flags;
1508
1509 Mount_Read_Only = false;
1510 Mount_Flags &= ~MS_RDONLY;
1511
1512 bool ret = ReMount(Display_Error);
1513
1514 Mount_Read_Only = ro;
1515 Mount_Flags = flags;
1516
1517 return ret;
1518}
1519
Gary Peck43acadf2012-11-21 21:19:01 -08001520bool TWPartition::Wipe(string New_File_System) {
Ethan Yonker726a0202014-12-16 20:01:38 -06001521 bool wiped = false, update_crypt = false, recreate_media = true;
Dees_Troy16c2b312013-01-15 16:51:18 +00001522 int check;
1523 string Layout_Filename = Mount_Point + "/.layout_version";
1524
Dees_Troy38bd7602012-09-14 13:33:53 -04001525 if (!Can_Be_Wiped) {
Matt Mower3c366972015-12-25 19:28:31 -06001526 gui_msg(Msg(msg::kError, "cannot_wipe=Partition {1} cannot be wiped.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04001527 return false;
1528 }
1529
Dees_Troyc51f1f92012-09-20 15:32:13 -04001530 if (Mount_Point == "/cache")
Dees_Troy2673cec2013-04-02 20:22:16 +00001531 Log_Offset = 0;
Dees_Troyc51f1f92012-09-20 15:32:13 -04001532
Dees_Troy16c2b312013-01-15 16:51:18 +00001533 if (Retain_Layout_Version && Mount(false) && TWFunc::Path_Exists(Layout_Filename))
1534 TWFunc::copy_file(Layout_Filename, "/.layout_version", 0600);
1535 else
1536 unlink("/.layout_version");
Dees_Troy38bd7602012-09-14 13:33:53 -04001537
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001538 if (Has_Data_Media && Current_File_System == New_File_System) {
Dees_Troy16c2b312013-01-15 16:51:18 +00001539 wiped = Wipe_Data_Without_Wiping_Media();
Ethan Yonker5eac2222014-06-11 12:22:55 -05001540 recreate_media = false;
Dees_Troy16c2b312013-01-15 16:51:18 +00001541 } else {
Dees_Troy16c2b312013-01-15 16:51:18 +00001542 DataManager::GetValue(TW_RM_RF_VAR, check);
1543
Hashcodedabfd492013-08-29 22:45:30 -07001544 if (check || Use_Rm_Rf)
Dees_Troy16c2b312013-01-15 16:51:18 +00001545 wiped = Wipe_RMRF();
1546 else if (New_File_System == "ext4")
1547 wiped = Wipe_EXT4();
1548 else if (New_File_System == "ext2" || New_File_System == "ext3")
1549 wiped = Wipe_EXT23(New_File_System);
1550 else if (New_File_System == "vfat")
1551 wiped = Wipe_FAT();
1552 else if (New_File_System == "exfat")
1553 wiped = Wipe_EXFAT();
1554 else if (New_File_System == "yaffs2")
1555 wiped = Wipe_MTD();
Dees_Troye5017042013-08-29 16:38:55 +00001556 else if (New_File_System == "f2fs")
1557 wiped = Wipe_F2FS();
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001558 else if (New_File_System == "ntfs")
1559 wiped = Wipe_NTFS();
Dees_Troy16c2b312013-01-15 16:51:18 +00001560 else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001561 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 +00001562 unlink("/.layout_version");
1563 return false;
1564 }
1565 update_crypt = wiped;
Gary Pecke8bc5d72012-12-21 06:45:25 -08001566 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001567
Gary Pecke8bc5d72012-12-21 06:45:25 -08001568 if (wiped) {
Dees_Troy1c1ac442013-01-17 21:42:14 +00001569 if (Mount_Point == "/cache")
1570 DataManager::Output_Version();
1571
Dees_Troy16c2b312013-01-15 16:51:18 +00001572 if (TWFunc::Path_Exists("/.layout_version") && Mount(false))
1573 TWFunc::copy_file("/.layout_version", Layout_Filename, 0600);
1574
1575 if (update_crypt) {
1576 Setup_File_System(false);
1577 if (Is_Encrypted && !Is_Decrypted) {
1578 // just wiped an encrypted partition back to its unencrypted state
1579 Is_Encrypted = false;
1580 Is_Decrypted = false;
1581 Decrypted_Block_Device = "";
1582 if (Mount_Point == "/data") {
1583 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1584 DataManager::SetValue(TW_IS_DECRYPTED, 0);
1585 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001586 }
1587 }
Ethan Yonker5eac2222014-06-11 12:22:55 -05001588
Ethan Yonker66a19492015-12-10 10:19:45 -06001589 if (Has_Data_Media && recreate_media) {
Ethan Yonker5eac2222014-06-11 12:22:55 -05001590 Recreate_Media_Folder();
1591 }
Matt Mower209c9632016-01-20 12:08:35 -06001592 if (Is_Storage && Mount(false))
1593 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Gary Pecke8bc5d72012-12-21 06:45:25 -08001594 }
Matt Mower209c9632016-01-20 12:08:35 -06001595
Gary Pecke8bc5d72012-12-21 06:45:25 -08001596 return wiped;
Dees_Troy51a0e822012-09-05 15:24:24 -04001597}
1598
Gary Peck43acadf2012-11-21 21:19:01 -08001599bool TWPartition::Wipe() {
Gary Peck82599a82012-11-21 16:23:12 -08001600 if (Is_File_System(Current_File_System))
1601 return Wipe(Current_File_System);
1602 else
1603 return Wipe(Fstab_File_System);
Gary Peck43acadf2012-11-21 21:19:01 -08001604}
1605
Dees_Troye58d5262012-09-21 12:27:57 -04001606bool TWPartition::Wipe_AndSec(void) {
1607 if (!Has_Android_Secure)
1608 return false;
1609
Dees_Troye58d5262012-09-21 12:27:57 -04001610 if (!Mount(true))
1611 return false;
1612
Ethan Yonker74db1572015-10-28 12:44:49 -05001613 gui_msg(Msg("wiping=Wiping {1}")(Backup_Display_Name));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001614 TWFunc::removeDir(Mount_Point + "/.android_secure/", true);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001615 return true;
Dees_Troye58d5262012-09-21 12:27:57 -04001616}
1617
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001618bool TWPartition::Can_Repair() {
Ethan Yonkera2719152015-05-28 09:44:41 -05001619 if (Mount_Read_Only)
1620 return false;
Matt Mower18794c82015-11-11 16:22:45 -06001621 if (Current_File_System == "vfat" && TWFunc::Path_Exists("/sbin/fsck.fat"))
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001622 return true;
1623 else if ((Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") && TWFunc::Path_Exists("/sbin/e2fsck"))
1624 return true;
1625 else if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/fsck.exfat"))
1626 return true;
1627 else if (Current_File_System == "f2fs" && TWFunc::Path_Exists("/sbin/fsck.f2fs"))
1628 return true;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001629 else if (Current_File_System == "ntfs" && (TWFunc::Path_Exists("/sbin/ntfsfix") || TWFunc::Path_Exists("/sbin/fsck.ntfs")))
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001630 return true;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001631 return false;
1632}
1633
1634bool TWPartition::Repair() {
1635 string command;
1636
1637 if (Current_File_System == "vfat") {
Matt Mower18794c82015-11-11 16:22:45 -06001638 if (!TWFunc::Path_Exists("/sbin/fsck.fat")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001639 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.fat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001640 return false;
1641 }
1642 if (!UnMount(true))
1643 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001644 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.fat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001645 Find_Actual_Block_Device();
Matt Mower18794c82015-11-11 16:22:45 -06001646 command = "/sbin/fsck.fat -y " + Actual_Block_Device;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001647 LOGINFO("Repair command: %s\n", command.c_str());
1648 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001649 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001650 return true;
1651 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001652 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001653 return false;
1654 }
1655 }
1656 if (Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") {
1657 if (!TWFunc::Path_Exists("/sbin/e2fsck")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001658 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("e2fsck"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001659 return false;
1660 }
1661 if (!UnMount(true))
1662 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001663 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("e2fsck"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001664 Find_Actual_Block_Device();
Ethan Yonkera2719152015-05-28 09:44:41 -05001665 command = "/sbin/e2fsck -fp " + Actual_Block_Device;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001666 LOGINFO("Repair command: %s\n", command.c_str());
1667 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001668 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001669 return true;
1670 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001671 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001672 return false;
1673 }
1674 }
1675 if (Current_File_System == "exfat") {
1676 if (!TWFunc::Path_Exists("/sbin/fsck.exfat")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001677 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.exfat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001678 return false;
1679 }
1680 if (!UnMount(true))
1681 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001682 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.exfat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001683 Find_Actual_Block_Device();
1684 command = "/sbin/fsck.exfat " + Actual_Block_Device;
1685 LOGINFO("Repair command: %s\n", command.c_str());
1686 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001687 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001688 return true;
1689 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001690 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001691 return false;
1692 }
1693 }
1694 if (Current_File_System == "f2fs") {
1695 if (!TWFunc::Path_Exists("/sbin/fsck.f2fs")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001696 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.f2fs"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001697 return false;
1698 }
1699 if (!UnMount(true))
1700 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001701 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.f2fs"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001702 Find_Actual_Block_Device();
1703 command = "/sbin/fsck.f2fs " + Actual_Block_Device;
1704 LOGINFO("Repair command: %s\n", command.c_str());
1705 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001706 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001707 return true;
1708 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001709 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001710 return false;
1711 }
1712 }
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001713 if (Current_File_System == "ntfs") {
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001714 string Ntfsfix_Binary;
1715 if (TWFunc::Path_Exists("/sbin/ntfsfix"))
1716 Ntfsfix_Binary = "ntfsfix";
1717 else if (TWFunc::Path_Exists("/sbin/fsck.ntfs"))
1718 Ntfsfix_Binary = "fsck.ntfs";
1719 else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001720 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("ntfsfix"));
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001721 return false;
1722 }
1723 if (!UnMount(true))
1724 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001725 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)(Ntfsfix_Binary));
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001726 Find_Actual_Block_Device();
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001727 command = "/sbin/" + Ntfsfix_Binary + " " + Actual_Block_Device;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001728 LOGINFO("Repair command: %s\n", command.c_str());
1729 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001730 gui_msg("done=Done.");
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001731 return true;
1732 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001733 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001734 return false;
1735 }
1736 }
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001737 return false;
1738}
1739
Ethan Yonkera2719152015-05-28 09:44:41 -05001740bool TWPartition::Can_Resize() {
1741 if (Mount_Read_Only)
1742 return false;
1743 if ((Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") && TWFunc::Path_Exists("/sbin/resize2fs"))
1744 return true;
1745 return false;
1746}
1747
1748bool TWPartition::Resize() {
1749 string command;
1750
1751 if (Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") {
1752 if (!Can_Repair()) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001753 LOGINFO("Cannot resize %s because %s cannot be repaired before resizing.\n", Display_Name.c_str(), Display_Name.c_str());
1754 gui_msg(Msg(msg::kError, "cannot_resize=Cannot resize {1}.")(Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001755 return false;
1756 }
1757 if (!TWFunc::Path_Exists("/sbin/resize2fs")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001758 LOGINFO("resize2fs does not exist! Cannot resize!\n");
1759 gui_msg(Msg(msg::kError, "cannot_resize=Cannot resize {1}.")(Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001760 return false;
1761 }
1762 // Repair will unmount so no need to do it twice
Ethan Yonker74db1572015-10-28 12:44:49 -05001763 gui_msg(Msg("repair_resize=Repairing {1} before resizing.")( Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001764 if (!Repair())
1765 return false;
Ethan Yonker74db1572015-10-28 12:44:49 -05001766 gui_msg(Msg("resizing=Resizing {1} using {2}...")(Display_Name)("resize2fs"));
Ethan Yonkera2719152015-05-28 09:44:41 -05001767 Find_Actual_Block_Device();
1768 command = "/sbin/resize2fs " + Actual_Block_Device;
1769 if (Length != 0) {
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001770 unsigned long long Actual_Size = IOCTL_Get_Block_Size();
1771 if (Actual_Size == 0)
1772 return false;
Ethan Yonkera2719152015-05-28 09:44:41 -05001773
Ethan Yonkera2719152015-05-28 09:44:41 -05001774 unsigned long long Block_Count;
1775 if (Length < 0) {
1776 // Reduce overall size by this length
1777 Block_Count = (Actual_Size / 1024LLU) - ((unsigned long long)(Length * -1) / 1024LLU);
1778 } else {
1779 // This is the size, not a size reduction
1780 Block_Count = ((unsigned long long)(Length) / 1024LLU);
1781 }
1782 char temp[256];
1783 sprintf(temp, "%llu", Block_Count);
1784 command += " ";
1785 command += temp;
1786 command += "K";
1787 }
1788 LOGINFO("Resize command: %s\n", command.c_str());
1789 if (TWFunc::Exec_Cmd(command) == 0) {
1790 Update_Size(true);
Ethan Yonker74db1572015-10-28 12:44:49 -05001791 gui_msg("done=Done.");
Ethan Yonkera2719152015-05-28 09:44:41 -05001792 return true;
1793 } else {
1794 Update_Size(true);
Ethan Yonker74db1572015-10-28 12:44:49 -05001795 gui_msg(Msg(msg::kError, "unable_resize=Unable to resize {1}.")(Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001796 return false;
1797 }
1798 }
1799 return false;
1800}
1801
bigbiffce8f83c2015-12-12 18:30:21 -05001802bool TWPartition::Backup(PartitionSettings *part_settings, pid_t *tar_fork_pid) {
1803 if (Backup_Method == BM_FILES)
1804 return Backup_Tar(part_settings, tar_fork_pid);
1805 else if (Backup_Method == BM_DD)
1806 return Backup_Image(part_settings);
1807 else if (Backup_Method == BM_FLASH_UTILS)
1808 return Backup_Dump_Image(part_settings);
Dees_Troy2673cec2013-04-02 20:22:16 +00001809 LOGERR("Unknown backup method for '%s'\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001810 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001811}
1812
bigbiffce8f83c2015-12-12 18:30:21 -05001813bool TWPartition::Restore(PartitionSettings *part_settings) {
Matt Mower3c366972015-12-25 19:28:31 -06001814 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, gui_parse_text("{@restoring_hdr}"));
Ethan Yonkere080c1f2016-09-19 13:50:25 -05001815 LOGINFO("Restore filename is: %s/%s\n", part_settings->Backup_Folder.c_str(), Backup_FileName.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001816
bigbiffce8f83c2015-12-12 18:30:21 -05001817 string Restore_File_System = Get_Restore_File_System(part_settings);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001818
1819 if (Is_File_System(Restore_File_System))
bigbiffce8f83c2015-12-12 18:30:21 -05001820 return Restore_Tar(part_settings);
1821 else if (Is_Image(Restore_File_System))
1822 return Restore_Image(part_settings);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001823
1824 LOGERR("Unknown restore method for '%s'\n", Mount_Point.c_str());
1825 return false;
1826}
1827
bigbiffce8f83c2015-12-12 18:30:21 -05001828string TWPartition::Get_Restore_File_System(PartitionSettings *part_settings) {
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001829 size_t first_period, second_period;
1830 string Restore_File_System;
1831
Gary Peck43acadf2012-11-21 21:19:01 -08001832 // Parse backup filename to extract the file system before wiping
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05001833 first_period = Backup_FileName.find(".");
Gary Peck43acadf2012-11-21 21:19:01 -08001834 if (first_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001835 LOGERR("Unable to find file system (first period).\n");
thatd43bf2d2014-09-21 23:13:02 +02001836 return string();
Gary Peck43acadf2012-11-21 21:19:01 -08001837 }
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05001838 Restore_File_System = Backup_FileName.substr(first_period + 1, Backup_FileName.size() - first_period - 1);
Gary Peck43acadf2012-11-21 21:19:01 -08001839 second_period = Restore_File_System.find(".");
1840 if (second_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001841 LOGERR("Unable to find file system (second period).\n");
thatd43bf2d2014-09-21 23:13:02 +02001842 return string();
Gary Peck43acadf2012-11-21 21:19:01 -08001843 }
1844 Restore_File_System.resize(second_period);
Matt Mowera0cd91d2016-12-30 18:21:42 -06001845 LOGINFO("Restore file system is: '%s'.\n", Restore_File_System.c_str());
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001846 return Restore_File_System;
Dees_Troy51a0e822012-09-05 15:24:24 -04001847}
1848
1849string TWPartition::Backup_Method_By_Name() {
bigbiffce8f83c2015-12-12 18:30:21 -05001850 if (Backup_Method == BM_NONE)
Dees_Troy38bd7602012-09-14 13:33:53 -04001851 return "none";
bigbiffce8f83c2015-12-12 18:30:21 -05001852 else if (Backup_Method == BM_FILES)
Dees_Troy38bd7602012-09-14 13:33:53 -04001853 return "files";
bigbiffce8f83c2015-12-12 18:30:21 -05001854 else if (Backup_Method == BM_DD)
Dees_Troy38bd7602012-09-14 13:33:53 -04001855 return "dd";
bigbiffce8f83c2015-12-12 18:30:21 -05001856 else if (Backup_Method == BM_FLASH_UTILS)
Dees_Troy38bd7602012-09-14 13:33:53 -04001857 return "flash_utils";
1858 else
1859 return "undefined";
1860 return "ERROR!";
Dees_Troy51a0e822012-09-05 15:24:24 -04001861}
1862
1863bool TWPartition::Decrypt(string Password) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001864 LOGINFO("STUB TWPartition::Decrypt, password: '%s'\n", Password.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001865 // Is this needed?
Dees_Troy51a0e822012-09-05 15:24:24 -04001866 return 1;
1867}
1868
1869bool TWPartition::Wipe_Encryption() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001870 bool Save_Data_Media = Has_Data_Media;
1871
1872 if (!UnMount(true))
1873 return false;
1874
Dees_Troy38bd7602012-09-14 13:33:53 -04001875 Has_Data_Media = false;
Dees_Troy74fb2e92013-04-15 14:35:47 +00001876 Decrypted_Block_Device = "";
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001877#ifdef TW_INCLUDE_CRYPTO
Ethan Yonker1b190162016-12-05 15:25:19 -06001878 if (Is_Decrypted && !Decrypted_Block_Device.empty()) {
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001879 if (!UnMount(true))
1880 return false;
Matt Mower2b18a532015-02-20 16:58:05 -06001881 if (delete_crypto_blk_dev((char*)("userdata")) != 0) {
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001882 LOGERR("Error deleting crypto block device, continuing anyway.\n");
1883 }
1884 }
1885#endif
Dees_Troy74fb2e92013-04-15 14:35:47 +00001886 Is_Decrypted = false;
1887 Is_Encrypted = false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001888 Find_Actual_Block_Device();
Ethan Yonkerbc85b632015-08-09 12:48:14 -05001889 if (Crypto_Key_Location == "footer") {
1890 int newlen, fd;
1891 if (Length != 0) {
1892 newlen = Length;
1893 if (newlen < 0)
1894 newlen = newlen * -1;
1895 } else {
1896 newlen = CRYPT_FOOTER_OFFSET;
1897 }
1898 if ((fd = open(Actual_Block_Device.c_str(), O_RDWR)) < 0) {
1899 gui_print_color("warning", "Unable to open '%s' to wipe crypto key\n", Actual_Block_Device.c_str());
1900 } else {
1901 unsigned int block_count;
1902 if ((ioctl(fd, BLKGETSIZE, &block_count)) == -1) {
1903 gui_print_color("warning", "Unable to get block size for wiping crypto footer.\n");
1904 } else {
1905 off64_t offset = ((off64_t)block_count * 512) - newlen;
1906 if (lseek64(fd, offset, SEEK_SET) == -1) {
1907 gui_print_color("warning", "Unable to lseek64 for wiping crypto footer.\n");
1908 } else {
1909 void* buffer = malloc(newlen);
1910 if (!buffer) {
1911 gui_print_color("warning", "Failed to malloc for wiping crypto footer.\n");
1912 } else {
1913 memset(buffer, 0, newlen);
1914 int ret = write(fd, buffer, newlen);
1915 if (ret != newlen) {
1916 gui_print_color("warning", "Failed to wipe crypto footer.\n");
1917 } else {
1918 LOGINFO("Successfully wiped crypto footer.\n");
1919 }
Matt Mower13a8f0b2015-09-26 15:40:03 -05001920 free(buffer);
Ethan Yonkerbc85b632015-08-09 12:48:14 -05001921 }
1922 }
1923 }
1924 close(fd);
1925 }
1926 } else {
Ethan Yonkerc2dafbb2016-03-15 22:20:59 -05001927 if (TWFunc::IOCTL_Get_Block_Size(Crypto_Key_Location.c_str()) >= 16384LLU) {
1928 string Command = "dd of='" + Crypto_Key_Location + "' if=/dev/zero bs=16384 count=1";
1929 TWFunc::Exec_Cmd(Command);
1930 } else {
1931 LOGINFO("Crypto key location reports size < 16K so not wiping crypto footer.\n");
1932 }
Ethan Yonkerbc85b632015-08-09 12:48:14 -05001933 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001934 if (Wipe(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001935 Has_Data_Media = Save_Data_Media;
1936 if (Has_Data_Media && !Symlink_Mount_Point.empty()) {
1937 Recreate_Media_Folder();
Ethan Yonker726a0202014-12-16 20:01:38 -06001938 if (Mount(false))
1939 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04001940 }
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001941 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
Ethan Yonker83e82572014-04-04 10:59:28 -05001942#ifndef TW_OEM_BUILD
Ethan Yonker74db1572015-10-28 12:44:49 -05001943 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 -05001944#endif
Dees_Troy38bd7602012-09-14 13:33:53 -04001945 return true;
1946 } else {
1947 Has_Data_Media = Save_Data_Media;
Ethan Yonker74db1572015-10-28 12:44:49 -05001948 gui_err("format_data_err=Unable to format to remove encryption.");
Ethan Yonker726a0202014-12-16 20:01:38 -06001949 if (Has_Data_Media && Mount(false))
1950 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04001951 return false;
1952 }
1953 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001954}
1955
1956void TWPartition::Check_FS_Type() {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001957 const char* type;
1958 blkid_probe pr;
Dees_Troy5bf43922012-09-07 16:07:55 -04001959
Dees_Troy68cab492012-12-12 19:29:35 +00001960 if (Fstab_File_System == "yaffs2" || Fstab_File_System == "mtd" || Fstab_File_System == "bml" || Ignore_Blkid)
1961 return; // Running blkid on some mtd devices causes a massive crash or needs to be skipped
Dees_Troy5bf43922012-09-07 16:07:55 -04001962
Dees_Troy38bd7602012-09-14 13:33:53 -04001963 Find_Actual_Block_Device();
Dees_Troy8170a922012-09-18 15:40:25 -04001964 if (!Is_Present)
1965 return;
Dees_Troy51127312012-09-08 13:08:49 -04001966
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001967 pr = blkid_new_probe_from_filename(Actual_Block_Device.c_str());
1968 if (blkid_do_fullprobe(pr)) {
1969 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00001970 LOGINFO("Can't probe device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001971 return;
Dees_Troy5bf43922012-09-07 16:07:55 -04001972 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001973
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05001974 if (blkid_probe_lookup_value(pr, "TYPE", &type, NULL) < 0) {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001975 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00001976 LOGINFO("can't find filesystem on device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001977 return;
1978 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001979
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001980 Current_File_System = type;
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001981 blkid_free_probe(pr);
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06001982 if (fs_flags.size() > 1) {
1983 std::vector<partition_fs_flags_struct>::iterator iter;
1984 std::vector<partition_fs_flags_struct>::iterator found = fs_flags.begin();
1985
1986 for (iter = fs_flags.begin(); iter != fs_flags.end(); iter++) {
1987 if (iter->File_System == Current_File_System) {
1988 found = iter;
1989 break;
1990 }
1991 }
1992 // If we don't find a match, we default the flags to the first set of flags that we received from the fstab
1993 if (Mount_Flags != found->Mount_Flags || Mount_Options != found->Mount_Options) {
1994 Mount_Flags = found->Mount_Flags;
1995 Mount_Options = found->Mount_Options;
1996 LOGINFO("Mount_Flags: %i, Mount_Options: %s\n", Mount_Flags, Mount_Options.c_str());
1997 }
1998 }
Dees_Troy51a0e822012-09-05 15:24:24 -04001999}
2000
Gary Peck43acadf2012-11-21 21:19:01 -08002001bool TWPartition::Wipe_EXT23(string File_System) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002002 if (!UnMount(true))
2003 return false;
2004
Dees_Troy43d8b002012-09-17 16:00:01 -04002005 if (TWFunc::Path_Exists("/sbin/mke2fs")) {
Vojtech Bocek05534202013-09-11 08:11:56 +02002006 string command;
Dees_Troy38bd7602012-09-14 13:33:53 -04002007
Greg Wallace4b44fef2015-12-29 15:33:24 -05002008 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mke2fs"));
Dees_Troy38bd7602012-09-14 13:33:53 -04002009 Find_Actual_Block_Device();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002010 command = "mke2fs -t " + File_System + " -m 0 " + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00002011 LOGINFO("mke2fs command: %s\n", command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02002012 if (TWFunc::Exec_Cmd(command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08002013 Current_File_System = File_System;
Dees_Troye58d5262012-09-21 12:27:57 -04002014 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05002015 gui_msg("done=Done.");
Dees_Troy38bd7602012-09-14 13:33:53 -04002016 return true;
2017 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002018 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04002019 return false;
2020 }
2021 } else
2022 return Wipe_RMRF();
2023
2024 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04002025}
2026
2027bool TWPartition::Wipe_EXT4() {
Ethan Yonker25f20c12014-10-14 09:04:43 -05002028 Find_Actual_Block_Device();
2029 if (!Is_Present) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002030 LOGINFO("Block device not present, cannot wipe %s.\n", Display_Name.c_str());
2031 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Ethan Yonker25f20c12014-10-14 09:04:43 -05002032 return false;
2033 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002034 if (!UnMount(true))
2035 return false;
2036
Matt Mower87413642017-01-17 21:14:46 -06002037#if defined(USE_EXT4)
Ethan Yonkerf27497f2014-02-09 11:48:33 -06002038 int ret;
2039 char *secontext = NULL;
2040
Greg Wallace4b44fef2015-12-29 15:33:24 -05002041 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("make_ext4fs"));
Ethan Yonkerf27497f2014-02-09 11:48:33 -06002042
Dees Troy99c8dbf2014-03-10 16:53:58 +00002043 if (!selinux_handle || selabel_lookup(selinux_handle, &secontext, Mount_Point.c_str(), S_IFDIR) < 0) {
Ethan Yonkerf27497f2014-02-09 11:48:33 -06002044 LOGINFO("Cannot lookup security context for '%s'\n", Mount_Point.c_str());
2045 ret = make_ext4fs(Actual_Block_Device.c_str(), Length, Mount_Point.c_str(), NULL);
2046 } else {
2047 ret = make_ext4fs(Actual_Block_Device.c_str(), Length, Mount_Point.c_str(), selinux_handle);
2048 }
2049 if (ret != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002050 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troya95f55c2013-08-17 13:14:43 +00002051 return false;
2052 } else {
bigbiff bigbiffc49d7062013-10-11 20:28:00 -04002053 string sedir = Mount_Point + "/lost+found";
2054 PartitionManager.Mount_By_Path(sedir.c_str(), true);
2055 rmdir(sedir.c_str());
2056 mkdir(sedir.c_str(), S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP);
Dees_Troya95f55c2013-08-17 13:14:43 +00002057 return true;
2058 }
2059#else
Dees_Troy43d8b002012-09-17 16:00:01 -04002060 if (TWFunc::Path_Exists("/sbin/make_ext4fs")) {
Vojtech Bocek05534202013-09-11 08:11:56 +02002061 string Command;
Dees_Troy38bd7602012-09-14 13:33:53 -04002062
Greg Wallace4b44fef2015-12-29 15:33:24 -05002063 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("make_ext4fs"));
Dees_Troy38bd7602012-09-14 13:33:53 -04002064 Find_Actual_Block_Device();
2065 Command = "make_ext4fs";
2066 if (!Is_Decrypted && Length != 0) {
2067 // Only use length if we're not decrypted
2068 char len[32];
2069 sprintf(len, "%i", Length);
2070 Command += " -l ";
2071 Command += len;
2072 }
Dees_Troy5295d582013-09-06 15:51:08 +00002073 if (TWFunc::Path_Exists("/file_contexts")) {
2074 Command += " -S /file_contexts";
2075 }
2076 Command += " -a " + Mount_Point + " " + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00002077 LOGINFO("make_ext4fs command: %s\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02002078 if (TWFunc::Exec_Cmd(Command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08002079 Current_File_System = "ext4";
Dees_Troye58d5262012-09-21 12:27:57 -04002080 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05002081 gui_msg("done=Done.");
Dees_Troy38bd7602012-09-14 13:33:53 -04002082 return true;
2083 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002084 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04002085 return false;
2086 }
2087 } else
Gary Peck43acadf2012-11-21 21:19:01 -08002088 return Wipe_EXT23("ext4");
Dees_Troya95f55c2013-08-17 13:14:43 +00002089#endif
Dees_Troy38bd7602012-09-14 13:33:53 -04002090 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04002091}
2092
2093bool TWPartition::Wipe_FAT() {
Vojtech Bocek05534202013-09-11 08:11:56 +02002094 string command;
Dees_Troy38bd7602012-09-14 13:33:53 -04002095
Matt Mower18794c82015-11-11 16:22:45 -06002096 if (TWFunc::Path_Exists("/sbin/mkfs.fat")) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002097 if (!UnMount(true))
2098 return false;
2099
Greg Wallace4b44fef2015-12-29 15:33:24 -05002100 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mkfs.fat"));
Dees_Troy38bd7602012-09-14 13:33:53 -04002101 Find_Actual_Block_Device();
Matt Mower18794c82015-11-11 16:22:45 -06002102 command = "mkfs.fat " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02002103 if (TWFunc::Exec_Cmd(command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08002104 Current_File_System = "vfat";
Dees_Troye58d5262012-09-21 12:27:57 -04002105 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05002106 gui_msg("done=Done.");
Dees_Troy38bd7602012-09-14 13:33:53 -04002107 return true;
2108 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002109 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04002110 return false;
2111 }
2112 return true;
2113 }
2114 else
2115 return Wipe_RMRF();
2116
2117 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04002118}
2119
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002120bool TWPartition::Wipe_EXFAT() {
Vojtech Bocek05534202013-09-11 08:11:56 +02002121 string command;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002122
2123 if (TWFunc::Path_Exists("/sbin/mkexfatfs")) {
2124 if (!UnMount(true))
2125 return false;
2126
Greg Wallace4b44fef2015-12-29 15:33:24 -05002127 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mkexfatfs"));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002128 Find_Actual_Block_Device();
2129 command = "mkexfatfs " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02002130 if (TWFunc::Exec_Cmd(command) == 0) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002131 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05002132 gui_msg("done=Done.");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002133 return true;
2134 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002135 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002136 return false;
2137 }
2138 return true;
2139 }
2140 return false;
2141}
2142
Dees_Troy38bd7602012-09-14 13:33:53 -04002143bool TWPartition::Wipe_MTD() {
2144 if (!UnMount(true))
2145 return false;
2146
Greg Wallace4b44fef2015-12-29 15:33:24 -05002147 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("MTD"));
Dees_Troy38bd7602012-09-14 13:33:53 -04002148
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02002149 mtd_scan_partitions();
2150 const MtdPartition* mtd = mtd_find_partition_by_name(MTD_Name.c_str());
2151 if (mtd == NULL) {
2152 LOGERR("No mtd partition named '%s'", MTD_Name.c_str());
2153 return false;
2154 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002155
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02002156 MtdWriteContext* ctx = mtd_write_partition(mtd);
2157 if (ctx == NULL) {
2158 LOGERR("Can't write '%s', failed to format.", MTD_Name.c_str());
2159 return false;
2160 }
2161 if (mtd_erase_blocks(ctx, -1) == -1) {
2162 mtd_write_close(ctx);
2163 LOGERR("Failed to format '%s'", MTD_Name.c_str());
2164 return false;
2165 }
2166 if (mtd_write_close(ctx) != 0) {
2167 LOGERR("Failed to close '%s'", MTD_Name.c_str());
2168 return false;
2169 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08002170 Current_File_System = "yaffs2";
Dees_Troye58d5262012-09-21 12:27:57 -04002171 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05002172 gui_msg("done=Done.");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02002173 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002174}
2175
2176bool TWPartition::Wipe_RMRF() {
Dees_Troy38bd7602012-09-14 13:33:53 -04002177 if (!Mount(true))
2178 return false;
Ethan Yonker726a0202014-12-16 20:01:38 -06002179 // This is the only wipe that leaves the partition mounted, so we
2180 // must manually remove the partition from MTP if it is a storage
2181 // partition.
2182 if (Is_Storage)
2183 PartitionManager.Remove_MTP_Storage(MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04002184
Ethan Yonker74db1572015-10-28 12:44:49 -05002185 gui_msg(Msg("remove_all=Removing all files under '{1}'")(Mount_Point));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002186 TWFunc::removeDir(Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04002187 Recreate_AndSec_Folder();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002188 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002189}
2190
Dees_Troye5017042013-08-29 16:38:55 +00002191bool TWPartition::Wipe_F2FS() {
Vojtech Bocek05534202013-09-11 08:11:56 +02002192 string command;
Dees_Troye5017042013-08-29 16:38:55 +00002193
2194 if (TWFunc::Path_Exists("/sbin/mkfs.f2fs")) {
2195 if (!UnMount(true))
2196 return false;
2197
Greg Wallace4b44fef2015-12-29 15:33:24 -05002198 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mkfs.f2fs"));
Dees_Troye5017042013-08-29 16:38:55 +00002199 Find_Actual_Block_Device();
Greg Wallace558aef72016-01-19 21:14:30 -05002200 command = "mkfs.f2fs -t 0";
dhacker29a3fa75f2015-01-17 19:42:33 -05002201 if (!Is_Decrypted && Length != 0) {
2202 // Only use length if we're not decrypted
2203 char len[32];
2204 int mod_length = Length;
2205 if (Length < 0)
2206 mod_length *= -1;
2207 sprintf(len, "%i", mod_length);
2208 command += " -r ";
2209 command += len;
2210 }
2211 command += " " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02002212 if (TWFunc::Exec_Cmd(command) == 0) {
Dees_Troye5017042013-08-29 16:38:55 +00002213 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05002214 gui_msg("done=Done.");
Dees_Troye5017042013-08-29 16:38:55 +00002215 return true;
2216 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002217 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troye5017042013-08-29 16:38:55 +00002218 return false;
2219 }
2220 return true;
2221 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002222 LOGINFO("mkfs.f2fs binary not found, using rm -rf to wipe.\n");
Dees_Troye5017042013-08-29 16:38:55 +00002223 return Wipe_RMRF();
2224 }
2225 return false;
2226}
2227
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002228bool TWPartition::Wipe_NTFS() {
2229 string command;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002230 string Ntfsmake_Binary;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002231
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002232 if (TWFunc::Path_Exists("/sbin/mkntfs"))
2233 Ntfsmake_Binary = "mkntfs";
2234 else if (TWFunc::Path_Exists("/sbin/mkfs.ntfs"))
2235 Ntfsmake_Binary = "mkfs.ntfs";
2236 else
2237 return false;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002238
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002239 if (!UnMount(true))
2240 return false;
2241
Greg Wallace4b44fef2015-12-29 15:33:24 -05002242 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)(Ntfsmake_Binary));
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002243 Find_Actual_Block_Device();
2244 command = "/sbin/" + Ntfsmake_Binary + " " + Actual_Block_Device;
2245 if (TWFunc::Exec_Cmd(command) == 0) {
2246 Recreate_AndSec_Folder();
2247 gui_msg("done=Done.");
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002248 return true;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002249 } else {
2250 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
2251 return false;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002252 }
2253 return false;
2254}
2255
Dees_Troy51a0e822012-09-05 15:24:24 -04002256bool TWPartition::Wipe_Data_Without_Wiping_Media() {
Ethan Yonker83e82572014-04-04 10:59:28 -05002257#ifdef TW_OEM_BUILD
2258 // In an OEM Build we want to do a full format
2259 return Wipe_Encryption();
2260#else
Ethan Yonker66a19492015-12-10 10:19:45 -06002261 bool ret = false;
Dees_Troy38bd7602012-09-14 13:33:53 -04002262
Dees_Troy38bd7602012-09-14 13:33:53 -04002263 if (!Mount(true))
2264 return false;
2265
Ethan Yonker74db1572015-10-28 12:44:49 -05002266 gui_msg("wiping_data=Wiping data without wiping /data/media ...");
Ethan Yonker66a19492015-12-10 10:19:45 -06002267 ret = Wipe_Data_Without_Wiping_Media_Func(Mount_Point + "/");
2268 if (ret)
2269 gui_msg("done=Done.");
2270 return ret;
2271#endif // ifdef TW_OEM_BUILD
2272}
2273
2274bool TWPartition::Wipe_Data_Without_Wiping_Media_Func(const string& parent __unused) {
2275 string dir;
Dees_Troy38bd7602012-09-14 13:33:53 -04002276
2277 DIR* d;
Ethan Yonker66a19492015-12-10 10:19:45 -06002278 d = opendir(parent.c_str());
Dees_Troy16b74352012-11-14 22:27:31 +00002279 if (d != NULL) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002280 struct dirent* de;
2281 while ((de = readdir(d)) != NULL) {
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002282 if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) continue;
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05002283
Ethan Yonker66a19492015-12-10 10:19:45 -06002284 dir = parent;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002285 dir.append(de->d_name);
Ethan Yonker3fdcda42016-11-30 12:29:37 -06002286 if (wipe_exclusions.check_skip_dirs(dir)) {
Ethan Yonker66a19492015-12-10 10:19:45 -06002287 LOGINFO("skipped '%s'\n", dir.c_str());
2288 continue;
2289 }
Dees_Troyce675462013-01-09 19:48:21 +00002290 if (de->d_type == DT_DIR) {
Ethan Yonker66a19492015-12-10 10:19:45 -06002291 dir.append("/");
2292 if (!Wipe_Data_Without_Wiping_Media_Func(dir)) {
2293 closedir(d);
2294 return false;
2295 }
2296 rmdir(dir.c_str());
bigbiff bigbiff98f1f902013-01-19 18:46:13 -05002297 } else if (de->d_type == DT_REG || de->d_type == DT_LNK || de->d_type == DT_FIFO || de->d_type == DT_SOCK) {
nkk719efbe762017-06-13 19:49:05 +03002298 if (unlink(dir.c_str()) != 0)
Ethan Yonker79f88bd2016-12-09 14:52:12 -06002299 LOGINFO("Unable to unlink '%s': %s\n", dir.c_str(), strerror(errno));
Dees_Troyce675462013-01-09 19:48:21 +00002300 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002301 }
2302 closedir(d);
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05002303
Dees_Troy16b74352012-11-14 22:27:31 +00002304 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -04002305 }
Ethan Yonker74db1572015-10-28 12:44:49 -05002306 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy16b74352012-11-14 22:27:31 +00002307 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04002308}
2309
bigbiffce8f83c2015-12-12 18:30:21 -05002310bool TWPartition::Backup_Tar(PartitionSettings *part_settings, pid_t *tar_fork_pid) {
Ethan Yonker472f5062016-02-25 13:47:30 -06002311 string Full_FileName;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002312 twrpTar tar;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05002313
Dees_Troy43d8b002012-09-17 16:00:01 -04002314 if (!Mount(true))
2315 return false;
2316
Dees_Troya13d74f2013-03-24 08:54:55 -05002317 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Backup_Display_Name, "Backing Up");
Ethan Yonker74db1572015-10-28 12:44:49 -05002318 gui_msg(Msg("backing_up=Backing up {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002319
Ethan Yonker472f5062016-02-25 13:47:30 -06002320 DataManager::GetValue(TW_USE_COMPRESSION_VAR, tar.use_compression);
Matt Mowerbb81e5d2014-03-20 18:05:41 -05002321
Dees_Troy83bd4832013-05-04 12:39:56 +00002322#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
Ethan Yonker472f5062016-02-25 13:47:30 -06002323 if (Can_Encrypt_Backup) {
2324 DataManager::GetValue("tw_encrypt_backup", tar.use_encryption);
2325 if (tar.use_encryption) {
2326 if (Use_Userdata_Encryption)
2327 tar.userdata_encryption = tar.use_encryption;
2328 string Password;
2329 DataManager::GetValue("tw_backup_password", Password);
2330 tar.setpassword(Password);
2331 } else {
2332 tar.use_encryption = 0;
2333 }
Dees_Troy83bd4832013-05-04 12:39:56 +00002334 }
2335#endif
Dees_Troy43d8b002012-09-17 16:00:01 -04002336
Ethan Yonker472f5062016-02-25 13:47:30 -06002337 Backup_FileName = Backup_Name + "." + Current_File_System + ".win";
Ethan Yonkere080c1f2016-09-19 13:50:25 -05002338 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
Ethan Yonker01f4e032017-02-03 15:30:52 -06002339 if (Has_Data_Media)
2340 gui_msg(Msg(msg::kWarning, "backup_storage_warning=Backups of {1} do not include any files in internal storage such as pictures or downloads.")(Display_Name));
bigbiffce8f83c2015-12-12 18:30:21 -05002341 tar.part_settings = part_settings;
Ethan Yonker3fdcda42016-11-30 12:29:37 -06002342 tar.backup_exclusions = &backup_exclusions;
Dees Troye0a433a2013-12-02 04:10:37 +00002343 tar.setdir(Backup_Path);
2344 tar.setfn(Full_FileName);
2345 tar.setsize(Backup_Size);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002346 tar.partition_name = Backup_Name;
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002347 tar.backup_folder = part_settings->Backup_Folder;
bigbiffce8f83c2015-12-12 18:30:21 -05002348 if (tar.createTarFork(tar_fork_pid) != 0)
Dees Troye0a433a2013-12-02 04:10:37 +00002349 return false;
Dees_Troy43d8b002012-09-17 16:00:01 -04002350 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002351}
2352
bigbiffce8f83c2015-12-12 18:30:21 -05002353bool TWPartition::Backup_Image(PartitionSettings *part_settings) {
2354 string Full_FileName, adb_file_name;
igoriok87e3d932013-01-31 21:03:53 +02002355
Ethan Yonker74db1572015-10-28 12:44:49 -05002356 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, gui_parse_text("{@backing}"));
2357 gui_msg(Msg("backing_up=Backing up {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002358
Ethan Yonker472f5062016-02-25 13:47:30 -06002359 Backup_FileName = Backup_Name + "." + Current_File_System + ".win";
Dees_Troy43d8b002012-09-17 16:00:01 -04002360
bigbiffce8f83c2015-12-12 18:30:21 -05002361 if (part_settings->adbbackup) {
2362 Full_FileName = TW_ADB_BACKUP;
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002363 adb_file_name = part_settings->Backup_Folder + "/" + Backup_FileName;
bigbiffce8f83c2015-12-12 18:30:21 -05002364 }
2365 else
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002366 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
bigbiffce8f83c2015-12-12 18:30:21 -05002367
2368 part_settings->total_restore_size = Backup_Size;
2369
2370 if (part_settings->adbbackup) {
2371 if (!twadbbu::Write_TWIMG(adb_file_name, Backup_Size))
2372 return false;
2373 }
2374
2375 if (!Raw_Read_Write(part_settings))
Ethan Yonker472f5062016-02-25 13:47:30 -06002376 return false;
2377
bigbiffce8f83c2015-12-12 18:30:21 -05002378 if (part_settings->adbbackup) {
2379 if (!twadbbu::Write_TWEOF())
2380 return false;
Dees_Troy7c2dec82012-09-26 09:49:14 -04002381 }
Dees_Troy43d8b002012-09-17 16:00:01 -04002382 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002383}
2384
bigbiffce8f83c2015-12-12 18:30:21 -05002385bool TWPartition::Raw_Read_Write(PartitionSettings *part_settings) {
2386 unsigned long long RW_Block_Size, Remain = Backup_Size;
2387 int src_fd = -1, dest_fd = -1;
2388 ssize_t bs;
Ethan Yonker472f5062016-02-25 13:47:30 -06002389 bool ret = false;
2390 void* buffer = NULL;
2391 unsigned long long backedup_size = 0;
bigbiffce8f83c2015-12-12 18:30:21 -05002392 string srcfn, destfn;
Ethan Yonker472f5062016-02-25 13:47:30 -06002393
bigbiffce8f83c2015-12-12 18:30:21 -05002394 if (part_settings->PM_Method == PM_BACKUP) {
2395 srcfn = Actual_Block_Device;
2396 if (part_settings->adbbackup)
2397 destfn = TW_ADB_BACKUP;
2398 else
Ethan Yonkere080c1f2016-09-19 13:50:25 -05002399 destfn = part_settings->Backup_Folder + "/" + Backup_FileName;
bigbiffce8f83c2015-12-12 18:30:21 -05002400 }
2401 else {
2402 destfn = Actual_Block_Device;
2403 if (part_settings->adbbackup) {
2404 srcfn = TW_ADB_RESTORE;
2405 } else {
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002406 srcfn = part_settings->Backup_Folder + "/" + Backup_FileName;
bigbiffce8f83c2015-12-12 18:30:21 -05002407 Remain = TWFunc::Get_File_Size(srcfn);
2408 }
2409 }
Ethan Yonker472f5062016-02-25 13:47:30 -06002410
bigbiffce8f83c2015-12-12 18:30:21 -05002411 src_fd = open(srcfn.c_str(), O_RDONLY | O_LARGEFILE);
Ethan Yonker472f5062016-02-25 13:47:30 -06002412 if (src_fd < 0) {
bigbiffce8f83c2015-12-12 18:30:21 -05002413 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(srcfn.c_str())(strerror(errno)));
Ethan Yonker472f5062016-02-25 13:47:30 -06002414 return false;
2415 }
bigbiffce8f83c2015-12-12 18:30:21 -05002416
2417 dest_fd = open(destfn.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_LARGEFILE, S_IRUSR | S_IWUSR);
Ethan Yonker472f5062016-02-25 13:47:30 -06002418 if (dest_fd < 0) {
bigbiffce8f83c2015-12-12 18:30:21 -05002419 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(destfn.c_str())(strerror(errno)));
Ethan Yonker472f5062016-02-25 13:47:30 -06002420 goto exit;
2421 }
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -04002422
bigbiffce8f83c2015-12-12 18:30:21 -05002423 LOGINFO("Reading '%s', writing '%s'\n", srcfn.c_str(), destfn.c_str());
2424
2425 if (part_settings->adbbackup) {
2426 RW_Block_Size = MAX_ADB_READ;
2427 bs = MAX_ADB_READ;
2428 }
2429 else {
2430 RW_Block_Size = 1048576LLU; // 1MB
2431 bs = (ssize_t)(RW_Block_Size);
2432 }
2433
Ethan Yonker472f5062016-02-25 13:47:30 -06002434 buffer = malloc((size_t)bs);
2435 if (!buffer) {
2436 LOGINFO("Raw_Read_Write failed to malloc\n");
2437 goto exit;
2438 }
bigbiffce8f83c2015-12-12 18:30:21 -05002439
2440 if (part_settings->progress)
2441 part_settings->progress->SetPartitionSize(part_settings->total_restore_size);
2442
Ethan Yonker472f5062016-02-25 13:47:30 -06002443 while (Remain > 0) {
2444 if (Remain < RW_Block_Size)
bigbiffce8f83c2015-12-12 18:30:21 -05002445 bs = (ssize_t)(Remain);
Matt Mowera8a89d12016-12-30 18:10:37 -06002446 if (read(src_fd, buffer, bs) != bs) {
Ethan Yonker472f5062016-02-25 13:47:30 -06002447 LOGINFO("Error reading source fd (%s)\n", strerror(errno));
2448 goto exit;
2449 }
2450 if (write(dest_fd, buffer, bs) != bs) {
2451 LOGINFO("Error writing destination fd (%s)\n", strerror(errno));
2452 goto exit;
2453 }
2454 backedup_size += (unsigned long long)(bs);
Matt Mower029a82d2017-01-08 13:12:38 -06002455 Remain -= (unsigned long long)(bs);
bigbiffce8f83c2015-12-12 18:30:21 -05002456 if (part_settings->progress)
2457 part_settings->progress->UpdateSize(backedup_size);
Ethan Yonker472f5062016-02-25 13:47:30 -06002458 if (PartitionManager.Check_Backup_Cancel() != 0)
2459 goto exit;
2460 }
bigbiffce8f83c2015-12-12 18:30:21 -05002461 if (part_settings->progress)
2462 part_settings->progress->UpdateDisplayDetails(true);
Ethan Yonker472f5062016-02-25 13:47:30 -06002463 fsync(dest_fd);
nailyka083dc62016-11-11 21:41:28 +01002464
2465 if (!part_settings->adbbackup && part_settings->PM_Method == PM_BACKUP) {
2466 tw_set_default_metadata(destfn.c_str());
2467 LOGINFO("Restored default metadata for %s\n", destfn.c_str());
2468 }
2469
Ethan Yonker472f5062016-02-25 13:47:30 -06002470 ret = true;
2471exit:
2472 if (src_fd >= 0)
2473 close(src_fd);
2474 if (dest_fd >= 0)
2475 close(dest_fd);
2476 if (buffer)
2477 free(buffer);
2478 return ret;
2479}
2480
bigbiffce8f83c2015-12-12 18:30:21 -05002481bool TWPartition::Backup_Dump_Image(PartitionSettings *part_settings) {
Vojtech Bocek05534202013-09-11 08:11:56 +02002482 string Full_FileName, Command;
Dees_Troy43d8b002012-09-17 16:00:01 -04002483
Ethan Yonker74db1572015-10-28 12:44:49 -05002484 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, gui_parse_text("{@backing}"));
2485 gui_msg(Msg("backing_up=Backing up {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002486
bigbiffce8f83c2015-12-12 18:30:21 -05002487 if (part_settings->progress)
2488 part_settings->progress->SetPartitionSize(Backup_Size);
Dees_Troy43d8b002012-09-17 16:00:01 -04002489
Ethan Yonker472f5062016-02-25 13:47:30 -06002490 Backup_FileName = Backup_Name + "." + Current_File_System + ".win";
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002491 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04002492
2493 Command = "dump_image " + MTD_Name + " '" + Full_FileName + "'";
bigbiffce8f83c2015-12-12 18:30:21 -05002494
Dees_Troy2673cec2013-04-02 20:22:16 +00002495 LOGINFO("Backup command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02002496 TWFunc::Exec_Cmd(Command);
Ethan Yonker4b94cfd2014-12-11 10:00:45 -06002497 tw_set_default_metadata(Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04002498 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
2499 // 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 -06002500 gui_msg(Msg(msg::kError, "backup_size=Backup file size for '{1}' is 0 bytes.")(Full_FileName));
Dees_Troy7c2dec82012-09-26 09:49:14 -04002501 return false;
2502 }
bigbiffce8f83c2015-12-12 18:30:21 -05002503 if (part_settings->progress)
2504 part_settings->progress->UpdateSize(Backup_Size);
2505
Dees_Troy43d8b002012-09-17 16:00:01 -04002506 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002507}
2508
bigbiffce8f83c2015-12-12 18:30:21 -05002509unsigned long long TWPartition::Get_Restore_Size(PartitionSettings *part_settings) {
2510 if (!part_settings->adbbackup) {
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002511 InfoManager restore_info(part_settings->Backup_Folder + "/" + Backup_Name + ".info");
bigbiffce8f83c2015-12-12 18:30:21 -05002512 if (restore_info.LoadValues() == 0) {
2513 if (restore_info.GetValue("backup_size", Restore_Size) == 0) {
2514 LOGINFO("Read info file, restore size is %llu\n", Restore_Size);
2515 return Restore_Size;
2516 }
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002517 }
2518 }
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002519
Matt Mower029a82d2017-01-08 13:12:38 -06002520 string Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
2521 string Restore_File_System = Get_Restore_File_System(part_settings);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002522
2523 if (Is_Image(Restore_File_System)) {
2524 Restore_Size = TWFunc::Get_File_Size(Full_FileName);
2525 return Restore_Size;
2526 }
2527
2528 twrpTar tar;
2529 tar.setdir(Backup_Path);
2530 tar.setfn(Full_FileName);
bigbiffce8f83c2015-12-12 18:30:21 -05002531 tar.backup_name = Full_FileName;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002532#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
2533 string Password;
2534 DataManager::GetValue("tw_restore_password", Password);
2535 if (!Password.empty())
2536 tar.setpassword(Password);
2537#endif
2538 tar.partition_name = Backup_Name;
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002539 tar.backup_folder = part_settings->Backup_Folder;
bigbiffce8f83c2015-12-12 18:30:21 -05002540 tar.part_settings = part_settings;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002541 Restore_Size = tar.get_size();
2542 return Restore_Size;
2543}
2544
bigbiffce8f83c2015-12-12 18:30:21 -05002545bool TWPartition::Restore_Tar(PartitionSettings *part_settings) {
Ethan Yonker472f5062016-02-25 13:47:30 -06002546 string Full_FileName;
Dees Troy4159aed2014-02-28 17:24:43 +00002547 bool ret = false;
bigbiffce8f83c2015-12-12 18:30:21 -05002548 string Restore_File_System = Get_Restore_File_System(part_settings);
Dees_Troy43d8b002012-09-17 16:00:01 -04002549
Dees_Troye58d5262012-09-21 12:27:57 -04002550 if (Has_Android_Secure) {
Dees_Troye58d5262012-09-21 12:27:57 -04002551 if (!Wipe_AndSec())
2552 return false;
Gary Peck43acadf2012-11-21 21:19:01 -08002553 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002554 gui_msg(Msg("wiping=Wiping {1}")(Backup_Display_Name));
Ethan Yonker5eac2222014-06-11 12:22:55 -05002555 if (Has_Data_Media && Mount_Point == "/data" && Restore_File_System != Current_File_System) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002556 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 -05002557 if (!Wipe_Data_Without_Wiping_Media())
2558 return false;
2559 } else {
2560 if (!Wipe(Restore_File_System))
2561 return false;
2562 }
Dees_Troye58d5262012-09-21 12:27:57 -04002563 }
Matt Mower3c366972015-12-25 19:28:31 -06002564 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Backup_Display_Name, gui_parse_text("{@restoring_hdr}"));
Ethan Yonker74db1572015-10-28 12:44:49 -05002565 gui_msg(Msg("restoring=Restoring {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002566
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05002567 // Remount as read/write as needed so we can restore the backup
2568 if (!ReMount_RW(true))
Dees_Troy43d8b002012-09-17 16:00:01 -04002569 return false;
2570
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002571 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
Ethan Yonker87af5632014-02-10 11:56:35 -06002572 twrpTar tar;
bigbiffce8f83c2015-12-12 18:30:21 -05002573 tar.part_settings = part_settings;
Ethan Yonker87af5632014-02-10 11:56:35 -06002574 tar.setdir(Backup_Path);
2575 tar.setfn(Full_FileName);
2576 tar.backup_name = Backup_Name;
2577#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
2578 string Password;
2579 DataManager::GetValue("tw_restore_password", Password);
2580 if (!Password.empty())
2581 tar.setpassword(Password);
2582#endif
bigbiffce8f83c2015-12-12 18:30:21 -05002583 part_settings->progress->SetPartitionSize(Get_Restore_Size(part_settings));
2584 if (tar.extractTarFork() != 0)
Dees Troy4159aed2014-02-28 17:24:43 +00002585 ret = false;
2586 else
2587 ret = true;
2588#ifdef HAVE_CAPABILITIES
2589 // Restore capabilities to the run-as binary
2590 if (Mount_Point == "/system" && Mount(true) && TWFunc::Path_Exists("/system/bin/run-as")) {
2591 struct vfs_cap_data cap_data;
2592 uint64_t capabilities = (1 << CAP_SETUID) | (1 << CAP_SETGID);
2593
2594 memset(&cap_data, 0, sizeof(cap_data));
2595 cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE;
2596 cap_data.data[0].permitted = (uint32_t) (capabilities & 0xffffffff);
2597 cap_data.data[0].inheritable = 0;
2598 cap_data.data[1].permitted = (uint32_t) (capabilities >> 32);
2599 cap_data.data[1].inheritable = 0;
2600 if (setxattr("/system/bin/run-as", XATTR_NAME_CAPS, &cap_data, sizeof(cap_data), 0) < 0) {
2601 LOGINFO("Failed to reset capabilities of /system/bin/run-as binary.\n");
2602 } else {
2603 LOGINFO("Reset capabilities of /system/bin/run-as binary successful.\n");
2604 }
2605 }
2606#endif
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05002607 if (Mount_Read_Only || Mount_Flags & MS_RDONLY)
2608 // Remount as read only when restoration is complete
2609 ReMount(true);
2610
Dees Troy4159aed2014-02-28 17:24:43 +00002611 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04002612}
2613
bigbiffce8f83c2015-12-12 18:30:21 -05002614bool TWPartition::Restore_Image(PartitionSettings *part_settings) {
Ethan Yonker96af84a2015-01-05 14:58:36 -06002615 string Full_FileName;
bigbiffce8f83c2015-12-12 18:30:21 -05002616 string Restore_File_System = Get_Restore_File_System(part_settings);
Dees_Troy43d8b002012-09-17 16:00:01 -04002617
Matt Mower3c366972015-12-25 19:28:31 -06002618 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Backup_Display_Name, gui_parse_text("{@restoring_hdr}"));
Ethan Yonker74db1572015-10-28 12:44:49 -05002619 gui_msg(Msg("restoring=Restoring {1}...")(Backup_Display_Name));
bigbiffce8f83c2015-12-12 18:30:21 -05002620
2621 if (part_settings->adbbackup)
2622 Full_FileName = TW_ADB_RESTORE;
2623 else
Ethan Yonkere080c1f2016-09-19 13:50:25 -05002624 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
Gary Peck15e623d2012-11-21 21:07:58 -08002625
Ethan Yonker96af84a2015-01-05 14:58:36 -06002626 if (Restore_File_System == "emmc") {
bigbiffce8f83c2015-12-12 18:30:21 -05002627 if (!part_settings->adbbackup)
2628 part_settings->total_restore_size = (uint64_t)(TWFunc::Get_File_Size(Full_FileName));
2629 if (!Raw_Read_Write(part_settings))
Ethan Yonker96af84a2015-01-05 14:58:36 -06002630 return false;
2631 } else if (Restore_File_System == "mtd" || Restore_File_System == "bml") {
bigbiffce8f83c2015-12-12 18:30:21 -05002632 if (!Flash_Image_FI(Full_FileName, part_settings->progress))
2633 return false;
2634 }
2635
2636 if (part_settings->adbbackup) {
2637 if (!twadbbu::Write_TWEOF())
Ethan Yonker96af84a2015-01-05 14:58:36 -06002638 return false;
Gary Peck15e623d2012-11-21 21:07:58 -08002639 }
Dees_Troy43d8b002012-09-17 16:00:01 -04002640 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002641}
Dees_Troy5bf43922012-09-07 16:07:55 -04002642
2643bool TWPartition::Update_Size(bool Display_Error) {
Dees_Troy0550cfb2012-10-13 11:56:13 -04002644 bool ret = false, Was_Already_Mounted = false;
Dees_Troy51127312012-09-08 13:08:49 -04002645
Ethan Yonker1b190162016-12-05 15:25:19 -06002646 Find_Actual_Block_Device();
2647
2648 if (!Can_Be_Mounted && !Is_Encrypted) {
2649 if (TWFunc::Path_Exists(Actual_Block_Device) && Find_Partition_Size()) {
2650 Used = Size;
2651 Backup_Size = Size;
2652 return true;
2653 }
Dees_Troy5bf43922012-09-07 16:07:55 -04002654 return false;
Ethan Yonker1b190162016-12-05 15:25:19 -06002655 }
Dees_Troy5bf43922012-09-07 16:07:55 -04002656
Dees_Troy0550cfb2012-10-13 11:56:13 -04002657 Was_Already_Mounted = Is_Mounted();
Dees_Troy38bd7602012-09-14 13:33:53 -04002658 if (Removable || Is_Encrypted) {
2659 if (!Mount(false))
2660 return true;
2661 } else if (!Mount(Display_Error))
Dees_Troy5bf43922012-09-07 16:07:55 -04002662 return false;
Dees_Troy51127312012-09-08 13:08:49 -04002663
2664 ret = Get_Size_Via_statfs(Display_Error);
Dees_Troy0550cfb2012-10-13 11:56:13 -04002665 if (!ret || Size == 0) {
2666 if (!Get_Size_Via_df(Display_Error)) {
2667 if (!Was_Already_Mounted)
2668 UnMount(false);
Dees_Troy51127312012-09-08 13:08:49 -04002669 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04002670 }
2671 }
Dees_Troy51127312012-09-08 13:08:49 -04002672
Dees_Troy5bf43922012-09-07 16:07:55 -04002673 if (Has_Data_Media) {
2674 if (Mount(Display_Error)) {
Ethan Yonker3fdcda42016-11-30 12:29:37 -06002675 Used = backup_exclusions.Get_Folder_Size(Mount_Point);
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002676 Backup_Size = Used;
2677 int bak = (int)(Used / 1048576LLU);
Dees_Troy51127312012-09-08 13:08:49 -04002678 int fre = (int)(Free / 1048576LLU);
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002679 LOGINFO("Data backup size is %iMB, free: %iMB.\n", bak, fre);
Dees_Troy0550cfb2012-10-13 11:56:13 -04002680 } else {
2681 if (!Was_Already_Mounted)
2682 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04002683 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04002684 }
Dees_Troye58d5262012-09-21 12:27:57 -04002685 } else if (Has_Android_Secure) {
2686 if (Mount(Display_Error))
Ethan Yonker3fdcda42016-11-30 12:29:37 -06002687 Backup_Size = backup_exclusions.Get_Folder_Size(Backup_Path);
Dees_Troy0550cfb2012-10-13 11:56:13 -04002688 else {
2689 if (!Was_Already_Mounted)
2690 UnMount(false);
Dees_Troye58d5262012-09-21 12:27:57 -04002691 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04002692 }
Dees_Troy5bf43922012-09-07 16:07:55 -04002693 }
Dees_Troy0550cfb2012-10-13 11:56:13 -04002694 if (!Was_Already_Mounted)
2695 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04002696 return true;
Dees_Troy51127312012-09-08 13:08:49 -04002697}
Dees_Troy38bd7602012-09-14 13:33:53 -04002698
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06002699bool TWPartition::Find_Wildcard_Block_Devices(const string& Device) {
2700 int mount_point_index = 0; // we will need to create separate mount points for each partition found and we use this index to name each one
2701 string Path = TWFunc::Get_Path(Device);
2702 string Dev = TWFunc::Get_Filename(Device);
2703 size_t wildcard_index = Dev.find("*");
2704 if (wildcard_index != string::npos)
2705 Dev = Dev.substr(0, wildcard_index);
2706 wildcard_index = Dev.size();
2707 DIR* d = opendir(Path.c_str());
2708 if (d == NULL) {
2709 LOGINFO("Error opening '%s': %s\n", Path.c_str(), strerror(errno));
2710 return false;
2711 }
2712 struct dirent* de;
2713 while ((de = readdir(d)) != NULL) {
2714 if (de->d_type != DT_BLK || strlen(de->d_name) <= wildcard_index || strncmp(de->d_name, Dev.c_str(), wildcard_index) != 0)
2715 continue;
2716
2717 string item = Path + "/";
2718 item.append(de->d_name);
2719 if (PartitionManager.Find_Partition_By_Block_Device(item))
2720 continue;
2721 TWPartition *part = new TWPartition;
2722 char buffer[MAX_FSTAB_LINE_LENGTH];
2723 sprintf(buffer, "%s %s-%i auto defaults defaults", item.c_str(), Mount_Point.c_str(), ++mount_point_index);
2724 part->Process_Fstab_Line(buffer, false, NULL);
2725 char display[MAX_FSTAB_LINE_LENGTH];
2726 sprintf(display, "%s %i", Storage_Name.c_str(), mount_point_index);
2727 part->Storage_Name = display;
2728 part->Display_Name = display;
2729 part->Primary_Block_Device = item;
2730 part->Wildcard_Block_Device = false;
2731 part->Is_SubPartition = true;
2732 part->SubPartition_Of = Mount_Point;
2733 part->Is_Storage = Is_Storage;
2734 part->Can_Be_Mounted = true;
2735 part->Removable = true;
2736 part->Can_Be_Wiped = Can_Be_Wiped;
2737 part->Wipe_Available_in_GUI = Wipe_Available_in_GUI;
2738 part->Find_Actual_Block_Device();
2739 part->Update_Size(false);
2740 Has_SubPartition = true;
2741 PartitionManager.Output_Partition(part);
2742 PartitionManager.Add_Partition(part);
2743 }
2744 closedir(d);
2745 return (mount_point_index > 0);
2746}
2747
Dees_Troy38bd7602012-09-14 13:33:53 -04002748void TWPartition::Find_Actual_Block_Device(void) {
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06002749 if (!Sysfs_Entry.empty() && Primary_Block_Device.empty() && Decrypted_Block_Device.empty()) {
2750 /* Sysfs_Entry.empty() indicates if this is a sysfs entry that begins with /device/
2751 * If we have a syfs entry then we are looking for this device from a uevent add.
2752 * The uevent add will set the primary block device based on the data we receive from
2753 * after checking for adopted storage. If the device ends up being adopted, then the
2754 * decrypted block device will be set instead of the primary block device. */
2755 Is_Present = false;
2756 return;
2757 }
2758 if (Wildcard_Block_Device && !Is_Adopted_Storage) {
2759 Is_Present = false;
2760 Actual_Block_Device = "";
2761 Can_Be_Mounted = false;
2762 if (!Find_Wildcard_Block_Devices(Primary_Block_Device)) {
2763 string Dev = Primary_Block_Device.substr(0, Primary_Block_Device.find("*"));
2764 if (TWFunc::Path_Exists(Dev)) {
2765 Is_Present = true;
2766 Can_Be_Mounted = true;
2767 Actual_Block_Device = Dev;
2768 }
2769 }
2770 return;
2771 } else if (Is_Decrypted && !Decrypted_Block_Device.empty()) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002772 Actual_Block_Device = Decrypted_Block_Device;
Ethan Yonker1b190162016-12-05 15:25:19 -06002773 if (TWFunc::Path_Exists(Decrypted_Block_Device)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002774 Is_Present = true;
Ethan Yonker1b190162016-12-05 15:25:19 -06002775 return;
2776 }
2777 } else if (SlotSelect && TWFunc::Path_Exists(Primary_Block_Device + PartitionManager.Get_Active_Slot_Suffix())) {
2778 Actual_Block_Device = Primary_Block_Device + PartitionManager.Get_Active_Slot_Suffix();
2779 unlink(Primary_Block_Device.c_str());
2780 symlink(Actual_Block_Device.c_str(), Primary_Block_Device.c_str()); // we create a non-slot symlink pointing to the currently selected slot which may assist zips with installing
2781 Is_Present = true;
2782 return;
Dees_Troy43d8b002012-09-17 16:00:01 -04002783 } else if (TWFunc::Path_Exists(Primary_Block_Device)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002784 Is_Present = true;
2785 Actual_Block_Device = Primary_Block_Device;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002786 return;
2787 }
Ethan Yonker1b190162016-12-05 15:25:19 -06002788 if (!Alternate_Block_Device.empty() && TWFunc::Path_Exists(Alternate_Block_Device)) {
Dees_Troy3f04d032012-10-07 18:20:09 -04002789 Actual_Block_Device = Alternate_Block_Device;
Dees_Troy38bd7602012-09-14 13:33:53 -04002790 Is_Present = true;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002791 } else {
Dees_Troy38bd7602012-09-14 13:33:53 -04002792 Is_Present = false;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002793 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002794}
2795
2796void TWPartition::Recreate_Media_Folder(void) {
2797 string Command;
Ethan Yonker66a19492015-12-10 10:19:45 -06002798 string Media_Path = Mount_Point + "/media";
Dees_Troy38bd7602012-09-14 13:33:53 -04002799
Ethan Yonkerbd7492d2016-12-07 13:55:01 -06002800 if (Is_FBE) {
2801 LOGINFO("Not recreating media folder on FBE\n");
2802 return;
2803 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002804 if (!Mount(true)) {
Ethan Yonker66a19492015-12-10 10:19:45 -06002805 gui_msg(Msg(msg::kError, "recreate_folder_err=Unable to recreate {1} folder.")(Media_Path));
2806 } else if (!TWFunc::Path_Exists(Media_Path)) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002807 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Ethan Yonker66a19492015-12-10 10:19:45 -06002808 LOGINFO("Recreating %s folder.\n", Media_Path.c_str());
2809 mkdir(Media_Path.c_str(), 0770);
xiaolu9416f4f2015-06-04 08:22:23 +08002810 string Internal_path = DataManager::GetStrValue("tw_internal_path");
2811 if (!Internal_path.empty()) {
2812 LOGINFO("Recreating %s folder.\n", Internal_path.c_str());
2813 mkdir(Internal_path.c_str(), 0770);
2814 }
2815#ifdef TW_INTERNAL_STORAGE_PATH
2816 mkdir(EXPAND(TW_INTERNAL_STORAGE_PATH), 0770);
2817#endif
Matt Mower87413642017-01-17 21:14:46 -06002818
thata3d31fb2014-12-21 22:27:40 +01002819 // Afterwards, we will try to set the
2820 // default metadata that we were hopefully able to get during
2821 // early boot.
Ethan Yonker66a19492015-12-10 10:19:45 -06002822 tw_set_default_metadata(Media_Path.c_str());
xiaolu9416f4f2015-06-04 08:22:23 +08002823 if (!Internal_path.empty())
2824 tw_set_default_metadata(Internal_path.c_str());
Matt Mower87413642017-01-17 21:14:46 -06002825
Ethan Yonker5eac2222014-06-11 12:22:55 -05002826 // Toggle mount to ensure that "internal sdcard" gets mounted
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002827 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Ethan Yonker5eac2222014-06-11 12:22:55 -05002828 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Dees_Troy38bd7602012-09-14 13:33:53 -04002829 }
Dees_Troy43d8b002012-09-17 16:00:01 -04002830}
Dees_Troye58d5262012-09-21 12:27:57 -04002831
2832void TWPartition::Recreate_AndSec_Folder(void) {
Dees_Troye58d5262012-09-21 12:27:57 -04002833 if (!Has_Android_Secure)
2834 return;
Dees_Troy2673cec2013-04-02 20:22:16 +00002835 LOGINFO("Creating %s: %s\n", Backup_Display_Name.c_str(), Symlink_Path.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -04002836 if (!Mount(true)) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002837 gui_msg(Msg(msg::kError, "recreate_folder_err=Unable to recreate {1} folder.")(Backup_Name));
Dees_Troye58d5262012-09-21 12:27:57 -04002838 } else if (!TWFunc::Path_Exists(Symlink_Path)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00002839 LOGINFO("Recreating %s folder.\n", Backup_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002840 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05002841 mkdir(Symlink_Path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002842 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04002843 }
2844}
bigbiff7cb4c332014-11-26 20:36:07 -05002845
2846uint64_t TWPartition::Get_Max_FileSize() {
2847 uint64_t maxFileSize = 0;
2848 const uint64_t constGB = (uint64_t) 1024 * 1024 * 1024;
2849 const uint64_t constTB = (uint64_t) constGB * 1024;
2850 const uint64_t constPB = (uint64_t) constTB * 1024;
bigbiff0c532032014-12-21 13:41:26 -05002851 if (Current_File_System == "ext4")
2852 maxFileSize = 16 * constTB; //16 TB
2853 else if (Current_File_System == "vfat")
2854 maxFileSize = 4 * constGB; //4 GB
2855 else if (Current_File_System == "ntfs")
2856 maxFileSize = 256 * constTB; //256 TB
2857 else if (Current_File_System == "exfat")
2858 maxFileSize = 16 * constPB; //16 PB
2859 else if (Current_File_System == "ext3")
2860 maxFileSize = 2 * constTB; //2 TB
2861 else if (Current_File_System == "f2fs")
2862 maxFileSize = 3.94 * constTB; //3.94 TB
bigbiff7cb4c332014-11-26 20:36:07 -05002863 else
2864 maxFileSize = 100000000L;
2865 return maxFileSize - 1;
2866}
2867
bigbiffce8f83c2015-12-12 18:30:21 -05002868bool TWPartition::Flash_Image(PartitionSettings *part_settings) {
2869 string Restore_File_System, full_filename;
Ethan Yonker96af84a2015-01-05 14:58:36 -06002870
Ethan Yonkere080c1f2016-09-19 13:50:25 -05002871 full_filename = part_settings->Backup_Folder + "/" + Backup_FileName;
Ethan Yonker96af84a2015-01-05 14:58:36 -06002872
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002873 LOGINFO("Image filename is: %s\n", Backup_FileName.c_str());
bigbiffce8f83c2015-12-12 18:30:21 -05002874
2875 if (Backup_Method == BM_FILES) {
Ethan Yonker96af84a2015-01-05 14:58:36 -06002876 LOGERR("Cannot flash images to file systems\n");
2877 return false;
2878 } else if (!Can_Flash_Img) {
2879 LOGERR("Cannot flash images to partitions %s\n", Display_Name.c_str());
2880 return false;
2881 } else {
2882 if (!Find_Partition_Size()) {
2883 LOGERR("Unable to find partition size for '%s'\n", Mount_Point.c_str());
2884 return false;
2885 }
bigbiffce8f83c2015-12-12 18:30:21 -05002886 unsigned long long image_size = TWFunc::Get_File_Size(full_filename);
Ethan Yonker96af84a2015-01-05 14:58:36 -06002887 if (image_size > Size) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002888 LOGINFO("Size (%llu bytes) of image '%s' is larger than target device '%s' (%llu bytes)\n",
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002889 image_size, Backup_FileName.c_str(), Actual_Block_Device.c_str(), Size);
Ethan Yonker74db1572015-10-28 12:44:49 -05002890 gui_err("img_size_err=Size of image is larger than target device");
Ethan Yonker96af84a2015-01-05 14:58:36 -06002891 return false;
2892 }
bigbiffce8f83c2015-12-12 18:30:21 -05002893 if (Backup_Method == BM_DD) {
2894 if (!part_settings->adbbackup) {
2895 if (Is_Sparse_Image(full_filename)) {
2896 return Flash_Sparse_Image(full_filename);
2897 }
Ethan Yonker472f5062016-02-25 13:47:30 -06002898 }
bigbiffce8f83c2015-12-12 18:30:21 -05002899 return Raw_Read_Write(part_settings);
2900 } else if (Backup_Method == BM_FLASH_UTILS) {
2901 return Flash_Image_FI(full_filename, NULL);
Ethan Yonker472f5062016-02-25 13:47:30 -06002902 }
Ethan Yonker96af84a2015-01-05 14:58:36 -06002903 }
2904
2905 LOGERR("Unknown flash method for '%s'\n", Mount_Point.c_str());
2906 return false;
2907}
2908
Ethan Yonker472f5062016-02-25 13:47:30 -06002909bool TWPartition::Is_Sparse_Image(const string& Filename) {
HashBanged974bb2016-01-30 14:20:09 -05002910 uint32_t magic = 0;
2911 int fd = open(Filename.c_str(), O_RDONLY);
2912 if (fd < 0) {
2913 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Filename)(strerror(errno)));
2914 return false;
2915 }
bigbiffce8f83c2015-12-12 18:30:21 -05002916
HashBanged974bb2016-01-30 14:20:09 -05002917 if (read(fd, &magic, sizeof(magic)) != sizeof(magic)) {
2918 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Filename)(strerror(errno)));
2919 close(fd);
2920 return false;
2921 }
2922 close(fd);
Ethan Yonker472f5062016-02-25 13:47:30 -06002923 if (magic == SPARSE_HEADER_MAGIC)
2924 return true;
2925 return false;
2926}
2927
2928bool TWPartition::Flash_Sparse_Image(const string& Filename) {
2929 string Command;
2930
2931 gui_msg(Msg("flashing=Flashing {1}...")(Display_Name));
2932
2933 Command = "simg2img '" + Filename + "' '" + Actual_Block_Device + "'";
Ethan Yonker96af84a2015-01-05 14:58:36 -06002934 LOGINFO("Flash command: '%s'\n", Command.c_str());
2935 TWFunc::Exec_Cmd(Command);
2936 return true;
2937}
2938
Ethan Yonker472f5062016-02-25 13:47:30 -06002939bool TWPartition::Flash_Image_FI(const string& Filename, ProgressTracking *progress) {
Ethan Yonker96af84a2015-01-05 14:58:36 -06002940 string Command;
Ethan Yonker472f5062016-02-25 13:47:30 -06002941 unsigned long long file_size;
Ethan Yonker96af84a2015-01-05 14:58:36 -06002942
Ethan Yonker74db1572015-10-28 12:44:49 -05002943 gui_msg(Msg("flashing=Flashing {1}...")(Display_Name));
Ethan Yonker472f5062016-02-25 13:47:30 -06002944 if (progress) {
2945 file_size = (unsigned long long)(TWFunc::Get_File_Size(Filename));
2946 progress->SetPartitionSize(file_size);
2947 }
Ethan Yonker96af84a2015-01-05 14:58:36 -06002948 // Sometimes flash image doesn't like to flash due to the first 2KB matching, so we erase first to ensure that it flashes
2949 Command = "erase_image " + MTD_Name;
2950 LOGINFO("Erase command: '%s'\n", Command.c_str());
2951 TWFunc::Exec_Cmd(Command);
2952 Command = "flash_image " + MTD_Name + " '" + Filename + "'";
2953 LOGINFO("Flash command: '%s'\n", Command.c_str());
2954 TWFunc::Exec_Cmd(Command);
Ethan Yonker472f5062016-02-25 13:47:30 -06002955 if (progress)
2956 progress->UpdateSize(file_size);
Ethan Yonker96af84a2015-01-05 14:58:36 -06002957 return true;
2958}
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05002959
2960void TWPartition::Change_Mount_Read_Only(bool new_value) {
2961 Mount_Read_Only = new_value;
2962}
2963
bigbiffce8f83c2015-12-12 18:30:21 -05002964bool TWPartition::Is_Read_Only() {
2965 return Mount_Read_Only;
2966}
2967
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05002968int TWPartition::Check_Lifetime_Writes() {
2969 bool original_read_only = Mount_Read_Only;
2970 int ret = 1;
2971
2972 Mount_Read_Only = true;
2973 if (Mount(false)) {
2974 Find_Actual_Block_Device();
2975 string block = basename(Actual_Block_Device.c_str());
2976 string file = "/sys/fs/" + Current_File_System + "/" + block + "/lifetime_write_kbytes";
2977 string result;
2978 if (TWFunc::Path_Exists(file)) {
2979 if (TWFunc::read_file(file, result) != 0) {
2980 LOGINFO("Check_Lifetime_Writes of '%s' failed to read_file\n", file.c_str());
2981 } else {
2982 LOGINFO("Check_Lifetime_Writes result: '%s'\n", result.c_str());
2983 if (result == "0") {
2984 ret = 0;
2985 }
2986 }
2987 } else {
2988 LOGINFO("Check_Lifetime_Writes file does not exist '%s'\n", file.c_str());
2989 }
2990 UnMount(true);
2991 } else {
2992 LOGINFO("Check_Lifetime_Writes failed to mount '%s'\n", Mount_Point.c_str());
2993 }
2994 Mount_Read_Only = original_read_only;
2995 return ret;
2996}
Ethan Yonker66a19492015-12-10 10:19:45 -06002997
2998int TWPartition::Decrypt_Adopted() {
2999#ifdef TW_INCLUDE_CRYPTO
3000 int ret = 1;
3001 Is_Adopted_Storage = false;
3002 string Adopted_Key_File = "";
3003
3004 if (!Removable)
3005 return ret;
3006
3007 int fd = open(Alternate_Block_Device.c_str(), O_RDONLY);
3008 if (fd < 0) {
3009 LOGINFO("failed to open '%s'\n", Alternate_Block_Device.c_str());
3010 return ret;
3011 }
3012 char type_guid[80];
3013 char part_guid[80];
3014
3015 if (gpt_disk_get_partition_info(fd, 2, type_guid, part_guid) == 0) {
3016 LOGINFO("type: '%s'\n", type_guid);
3017 LOGINFO("part: '%s'\n", part_guid);
3018 Adopted_GUID = part_guid;
3019 LOGINFO("Adopted_GUID '%s'\n", Adopted_GUID.c_str());
3020 if (strcmp(type_guid, TWGptAndroidExpand) == 0) {
3021 LOGINFO("android_expand found\n");
3022 Adopted_Key_File = "/data/misc/vold/expand_";
3023 Adopted_Key_File += part_guid;
3024 Adopted_Key_File += ".key";
3025 if (TWFunc::Path_Exists(Adopted_Key_File)) {
3026 Is_Adopted_Storage = true;
3027 /* Until we find a use case for this, I think it is safe
3028 * to disable USB Mass Storage whenever adopted storage
3029 * is present.
3030 */
3031 LOGINFO("Detected adopted storage, disabling USB mass storage mode\n");
3032 DataManager::SetValue("tw_has_usb_storage", 0);
3033 }
3034 }
3035 }
3036
3037 if (Is_Adopted_Storage) {
3038 string Adopted_Block_Device = Alternate_Block_Device + "p2";
3039 if (!TWFunc::Path_Exists(Adopted_Block_Device)) {
3040 Adopted_Block_Device = Alternate_Block_Device + "2";
3041 if (!TWFunc::Path_Exists(Adopted_Block_Device)) {
3042 LOGINFO("Adopted block device does not exist\n");
3043 goto exit;
3044 }
3045 }
3046 LOGINFO("key file is '%s', block device '%s'\n", Adopted_Key_File.c_str(), Adopted_Block_Device.c_str());
3047 char crypto_blkdev[MAXPATHLEN];
3048 std::string thekey;
3049 int fdkey = open(Adopted_Key_File.c_str(), O_RDONLY);
3050 if (fdkey < 0) {
3051 LOGINFO("failed to open key file\n");
3052 goto exit;
3053 }
3054 char buf[512];
3055 ssize_t n;
3056 while ((n = read(fdkey, &buf[0], sizeof(buf))) > 0) {
3057 thekey.append(buf, n);
3058 }
3059 close(fdkey);
3060 unsigned char* key = (unsigned char*) thekey.data();
3061 cryptfs_revert_ext_volume(part_guid);
3062
3063 ret = cryptfs_setup_ext_volume(part_guid, Adopted_Block_Device.c_str(), key, thekey.size(), crypto_blkdev);
3064 if (ret == 0) {
3065 LOGINFO("adopted storage new block device: '%s'\n", crypto_blkdev);
3066 Decrypted_Block_Device = crypto_blkdev;
3067 Is_Decrypted = true;
3068 Is_Encrypted = true;
3069 Find_Actual_Block_Device();
Kjell Braden3126a112016-06-19 16:58:15 +00003070 if (!Mount_Storage_Retry(false)) {
Ethan Yonker66a19492015-12-10 10:19:45 -06003071 LOGERR("Failed to mount decrypted adopted storage device\n");
3072 Is_Decrypted = false;
3073 Is_Encrypted = false;
3074 cryptfs_revert_ext_volume(part_guid);
3075 ret = 1;
3076 } else {
Ethan Yonkerfcf3f242016-02-16 12:30:26 -06003077 UnMount(false);
3078 Has_Android_Secure = false;
3079 Symlink_Path = "";
3080 Symlink_Mount_Point = "";
3081 Backup_Name = Mount_Point.substr(1);
3082 Backup_Path = Mount_Point;
3083 TWPartition* sdext = PartitionManager.Find_Partition_By_Path("/sd-ext");
3084 if (sdext && sdext->Actual_Block_Device == Adopted_Block_Device) {
3085 LOGINFO("Removing /sd-ext from partition list due to adopted storage\n");
3086 PartitionManager.Remove_Partition_By_Path("/sd-ext");
3087 }
Ethan Yonker66a19492015-12-10 10:19:45 -06003088 Setup_Data_Media();
3089 Recreate_Media_Folder();
3090 Wipe_Available_in_GUI = true;
3091 Wipe_During_Factory_Reset = true;
3092 Can_Be_Backed_Up = true;
3093 Can_Encrypt_Backup = true;
3094 Use_Userdata_Encryption = true;
3095 Is_Storage = true;
3096 Storage_Name = "Adopted Storage";
3097 Is_SubPartition = true;
3098 SubPartition_Of = "/data";
3099 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
3100 DataManager::SetValue("tw_has_adopted_storage", 1);
3101 }
3102 } else {
3103 LOGERR("Failed to setup adopted storage decryption\n");
3104 }
3105 }
3106exit:
Matt Mower06543e32017-01-06 15:25:26 -06003107 close(fd);
Ethan Yonker66a19492015-12-10 10:19:45 -06003108 return ret;
3109#else
3110 LOGINFO("Decrypt_Adopted: no crypto support\n");
3111 return 1;
3112#endif
3113}
3114
3115void TWPartition::Revert_Adopted() {
3116#ifdef TW_INCLUDE_CRYPTO
3117 if (!Adopted_GUID.empty()) {
3118 PartitionManager.Remove_MTP_Storage(Mount_Point);
3119 UnMount(false);
3120 cryptfs_revert_ext_volume(Adopted_GUID.c_str());
3121 Is_Adopted_Storage = false;
3122 Is_Encrypted = false;
3123 Is_Decrypted = false;
3124 Decrypted_Block_Device = "";
3125 Find_Actual_Block_Device();
3126 Wipe_During_Factory_Reset = false;
3127 Can_Be_Backed_Up = false;
3128 Can_Encrypt_Backup = false;
3129 Use_Userdata_Encryption = false;
3130 Is_SubPartition = false;
3131 SubPartition_Of = "";
3132 Has_Data_Media = false;
3133 Storage_Path = Mount_Point;
3134 if (!Symlink_Mount_Point.empty()) {
3135 TWPartition* Dat = PartitionManager.Find_Partition_By_Path("/data");
3136 if (Dat) {
3137 Dat->UnMount(false);
3138 Dat->Symlink_Mount_Point = Symlink_Mount_Point;
3139 }
3140 Symlink_Mount_Point = "";
3141 }
3142 }
3143#else
3144 LOGINFO("Revert_Adopted: no crypto support\n");
3145#endif
3146}
bigbiff bigbiffb5ecaad2017-03-20 18:53:53 -04003147
3148void TWPartition::Set_Backup_FileName(string fname) {
3149 Backup_FileName = fname;
3150}
3151
3152string TWPartition::Get_Backup_Name() {
3153 return Backup_Name;
3154}