blob: e7b3d2d8a9d0f51e9a8c79bcda6179dad258214d [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
Ethan Yonker007de982018-08-31 14:23:16 -050034#include "cutils/properties.h"
bigbiff7b4c7a62015-01-01 19:44:14 -050035#include "libblkid/include/blkid.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040036#include "variables.h"
Dees_Troy2673cec2013-04-02 20:22:16 +000037#include "twcommon.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040038#include "partitions.hpp"
Dees_Troy5bf43922012-09-07 16:07:55 -040039#include "data.hpp"
Dees_Troy43d8b002012-09-17 16:00:01 -040040#include "twrp-functions.hpp"
bigbiff bigbiff9c754052013-01-09 09:09:08 -050041#include "twrpTar.hpp"
Ethan Yonker3fdcda42016-11-30 12:29:37 -060042#include "exclude.hpp"
Ethan Yonker1b7a31b2014-07-03 15:09:22 -050043#include "infomanager.hpp"
Ethan Yonker4b94cfd2014-12-11 10:00:45 -060044#include "set_metadata.h"
Ethan Yonker74db1572015-10-28 12:44:49 -050045#include "gui/gui.hpp"
bigbiffce8f83c2015-12-12 18:30:21 -050046#include "adbbu/libtwadbbu.hpp"
Dees_Troy5bf43922012-09-07 16:07:55 -040047extern "C" {
Dees_Troy38bd7602012-09-14 13:33:53 -040048 #include "mtdutils/mtdutils.h"
49 #include "mtdutils/mounts.h"
Dees_Troya95f55c2013-08-17 13:14:43 +000050#ifdef USE_EXT4
Ethan Yonker8373cfe2017-09-08 06:50:54 -050051 // #include "make_ext4fs.h" TODO need ifdef for android8
52 #include <ext4_utils/make_ext4fs.h>
Dees_Troya95f55c2013-08-17 13:14:43 +000053#endif
Ethan Yonker71413f42014-02-26 13:36:08 -060054
55#ifdef TW_INCLUDE_CRYPTO
Ethan Yonker253368a2014-11-25 15:00:52 -060056 #include "crypto/lollipop/cryptfs.h"
Ethan Yonker66a19492015-12-10 10:19:45 -060057 #include "gpt/gpt.h"
Ethan Yonkerbd7492d2016-12-07 13:55:01 -060058 #ifdef TW_INCLUDE_FBE
59 #include "crypto/ext4crypt/Decrypt.h"
Ethan Yonkerbd7492d2016-12-07 13:55:01 -060060 #endif
Ethan Yonkerbc85b632015-08-09 12:48:14 -050061#else
62 #define CRYPT_FOOTER_OFFSET 0x4000
Ethan Yonker71413f42014-02-26 13:36:08 -060063#endif
Dees_Troy5bf43922012-09-07 16:07:55 -040064}
Matt Mower87413642017-01-17 21:14:46 -060065#include <selinux/selinux.h>
Ethan Yonkerf27497f2014-02-09 11:48:33 -060066#include <selinux/label.h>
Dees Troy4159aed2014-02-28 17:24:43 +000067#ifdef HAVE_CAPABILITIES
68#include <sys/capability.h>
69#include <sys/xattr.h>
70#include <linux/xattr.h>
71#endif
HashBanged974bb2016-01-30 14:20:09 -050072#include <sparse_format.h>
Ethan Yonker472f5062016-02-25 13:47:30 -060073#include "progresstracking.hpp"
Dees_Troy51a0e822012-09-05 15:24:24 -040074
bigbiff bigbiff9c754052013-01-09 09:09:08 -050075using namespace std;
76
Ethan Yonker6e8c27a2016-12-22 17:55:57 -060077static 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
78
Dees_Troya95f55c2013-08-17 13:14:43 +000079extern struct selabel_handle *selinux_handle;
Ethan Yonker6277c792014-09-15 14:54:30 -050080extern bool datamedia;
Dees_Troya95f55c2013-08-17 13:14:43 +000081
Hashcode62bd9e02013-11-19 21:59:42 -080082struct flag_list {
83 const char *name;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -060084 unsigned long flag;
Hashcode62bd9e02013-11-19 21:59:42 -080085};
86
Matt Mower4ab42b12016-04-21 13:52:18 -050087const struct flag_list mount_flags[] = {
Ethan Yonker6e8c27a2016-12-22 17:55:57 -060088 { "noatime", MS_NOATIME },
89 { "noexec", MS_NOEXEC },
90 { "nosuid", MS_NOSUID },
91 { "nodev", MS_NODEV },
92 { "nodiratime", MS_NODIRATIME },
93 { "ro", MS_RDONLY },
94 { "rw", 0 },
95 { "remount", MS_REMOUNT },
96 { "bind", MS_BIND },
97 { "rec", MS_REC },
Dees Troyc4bc30e2014-02-03 15:04:19 +000098#ifdef MS_UNBINDABLE
Ethan Yonker6e8c27a2016-12-22 17:55:57 -060099 { "unbindable", MS_UNBINDABLE },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000100#endif
101#ifdef MS_PRIVATE
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600102 { "private", MS_PRIVATE },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000103#endif
104#ifdef MS_SLAVE
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600105 { "slave", MS_SLAVE },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000106#endif
107#ifdef MS_SHARED
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600108 { "shared", MS_SHARED },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000109#endif
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600110 { "sync", MS_SYNCHRONOUS },
111 { 0, 0 },
112};
113
114const char *ignored_mount_items[] = {
115 "defaults=",
116 "errors=",
117 NULL
Hashcode62bd9e02013-11-19 21:59:42 -0800118};
119
Matt Mower2416a502016-04-12 19:54:46 -0500120enum TW_FSTAB_FLAGS {
121 TWFLAG_DEFAULTS, // Retain position
122 TWFLAG_ANDSEC,
123 TWFLAG_BACKUP,
124 TWFLAG_BACKUPNAME,
125 TWFLAG_BLOCKSIZE,
126 TWFLAG_CANBEWIPED,
127 TWFLAG_CANENCRYPTBACKUP,
128 TWFLAG_DISPLAY,
129 TWFLAG_ENCRYPTABLE,
Ethan Yonkere9afc3d2018-08-30 15:16:27 -0500130 TWFLAG_FILEENCRYPTION,
Matt Mower2416a502016-04-12 19:54:46 -0500131 TWFLAG_FLASHIMG,
132 TWFLAG_FORCEENCRYPT,
133 TWFLAG_FSFLAGS,
134 TWFLAG_IGNOREBLKID,
135 TWFLAG_LENGTH,
136 TWFLAG_MOUNTTODECRYPT,
137 TWFLAG_REMOVABLE,
138 TWFLAG_RETAINLAYOUTVERSION,
139 TWFLAG_SETTINGSSTORAGE,
140 TWFLAG_STORAGE,
141 TWFLAG_STORAGENAME,
142 TWFLAG_SUBPARTITIONOF,
143 TWFLAG_SYMLINK,
144 TWFLAG_USERDATAENCRYPTBACKUP,
145 TWFLAG_USERMRF,
146 TWFLAG_WIPEDURINGFACTORYRESET,
147 TWFLAG_WIPEINGUI,
Ethan Yonker1b190162016-12-05 15:25:19 -0600148 TWFLAG_SLOTSELECT,
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600149 TWFLAG_WAIT,
150 TWFLAG_VERIFY,
151 TWFLAG_CHECK,
152 TWFLAG_ALTDEVICE,
153 TWFLAG_NOTRIM,
154 TWFLAG_VOLDMANAGED,
155 TWFLAG_FORMATTABLE,
156 TWFLAG_RESIZE,
Matt Mower2416a502016-04-12 19:54:46 -0500157};
158
159/* Flags without a trailing '=' are considered dual format flags and can be
160 * written as either 'flagname' or 'flagname=', where the character following
161 * the '=' is Y,y,1 for true and false otherwise.
162 */
163const struct flag_list tw_flags[] = {
164 { "andsec", TWFLAG_ANDSEC },
165 { "backup", TWFLAG_BACKUP },
166 { "backupname=", TWFLAG_BACKUPNAME },
167 { "blocksize=", TWFLAG_BLOCKSIZE },
168 { "canbewiped", TWFLAG_CANBEWIPED },
169 { "canencryptbackup", TWFLAG_CANENCRYPTBACKUP },
170 { "defaults", TWFLAG_DEFAULTS },
171 { "display=", TWFLAG_DISPLAY },
172 { "encryptable=", TWFLAG_ENCRYPTABLE },
Ethan Yonkere9afc3d2018-08-30 15:16:27 -0500173 { "fileencryption=", TWFLAG_FILEENCRYPTION },
Matt Mower2416a502016-04-12 19:54:46 -0500174 { "flashimg", TWFLAG_FLASHIMG },
175 { "forceencrypt=", TWFLAG_FORCEENCRYPT },
176 { "fsflags=", TWFLAG_FSFLAGS },
177 { "ignoreblkid", TWFLAG_IGNOREBLKID },
178 { "length=", TWFLAG_LENGTH },
179 { "mounttodecrypt", TWFLAG_MOUNTTODECRYPT },
180 { "removable", TWFLAG_REMOVABLE },
181 { "retainlayoutversion", TWFLAG_RETAINLAYOUTVERSION },
182 { "settingsstorage", TWFLAG_SETTINGSSTORAGE },
183 { "storage", TWFLAG_STORAGE },
184 { "storagename=", TWFLAG_STORAGENAME },
185 { "subpartitionof=", TWFLAG_SUBPARTITIONOF },
186 { "symlink=", TWFLAG_SYMLINK },
187 { "userdataencryptbackup", TWFLAG_USERDATAENCRYPTBACKUP },
188 { "usermrf", TWFLAG_USERMRF },
189 { "wipeduringfactoryreset", TWFLAG_WIPEDURINGFACTORYRESET },
190 { "wipeingui", TWFLAG_WIPEINGUI },
Ethan Yonker1b190162016-12-05 15:25:19 -0600191 { "slotselect", TWFLAG_SLOTSELECT },
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600192 { "wait", TWFLAG_WAIT },
193 { "verify", TWFLAG_VERIFY },
194 { "check", TWFLAG_CHECK },
195 { "altdevice", TWFLAG_ALTDEVICE },
196 { "notrim", TWFLAG_NOTRIM },
197 { "voldmanaged=", TWFLAG_VOLDMANAGED },
198 { "formattable", TWFLAG_FORMATTABLE },
199 { "resize", TWFLAG_RESIZE },
Matt Mower2416a502016-04-12 19:54:46 -0500200 { 0, 0 },
201};
202
that9e0593e2014-10-08 00:01:24 +0200203TWPartition::TWPartition() {
Dees_Troy51a0e822012-09-05 15:24:24 -0400204 Can_Be_Mounted = false;
205 Can_Be_Wiped = false;
Dees_Troya13d74f2013-03-24 08:54:55 -0500206 Can_Be_Backed_Up = false;
Vojtech Bocek1dc30982013-08-30 21:49:30 +0200207 Use_Rm_Rf = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400208 Wipe_During_Factory_Reset = false;
209 Wipe_Available_in_GUI = false;
210 Is_SubPartition = false;
Dees_Troy2691f9d2012-09-24 11:15:49 -0400211 Has_SubPartition = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400212 SubPartition_Of = "";
213 Symlink_Path = "";
214 Symlink_Mount_Point = "";
215 Mount_Point = "";
Dees_Troye58d5262012-09-21 12:27:57 -0400216 Backup_Path = "";
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600217 Wildcard_Block_Device = false;
218 Sysfs_Entry = "";
Dees_Troy38bd7602012-09-14 13:33:53 -0400219 Actual_Block_Device = "";
220 Primary_Block_Device = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400221 Alternate_Block_Device = "";
222 Removable = false;
223 Is_Present = false;
224 Length = 0;
225 Size = 0;
226 Used = 0;
227 Free = 0;
228 Backup_Size = 0;
229 Can_Be_Encrypted = false;
230 Is_Encrypted = false;
231 Is_Decrypted = false;
Ethan Yonkerbd7492d2016-12-07 13:55:01 -0600232 Is_FBE = false;
Ethan Yonker253368a2014-11-25 15:00:52 -0600233 Mount_To_Decrypt = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400234 Decrypted_Block_Device = "";
235 Display_Name = "";
Dees_Troya13d74f2013-03-24 08:54:55 -0500236 Backup_Display_Name = "";
237 Storage_Name = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400238 Backup_Name = "";
Dees_Troy63c8df72012-09-10 14:02:05 -0400239 Backup_FileName = "";
Dees_Troy38bd7602012-09-14 13:33:53 -0400240 MTD_Name = "";
bigbiffce8f83c2015-12-12 18:30:21 -0500241 Backup_Method = BM_NONE;
Dees_Troy83bd4832013-05-04 12:39:56 +0000242 Can_Encrypt_Backup = false;
243 Use_Userdata_Encryption = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400244 Has_Data_Media = false;
Dees_Troye58d5262012-09-21 12:27:57 -0400245 Has_Android_Secure = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400246 Is_Storage = false;
Dees_Troya13d74f2013-03-24 08:54:55 -0500247 Is_Settings_Storage = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400248 Storage_Path = "";
249 Current_File_System = "";
250 Fstab_File_System = "";
Hashcode62bd9e02013-11-19 21:59:42 -0800251 Mount_Flags = 0;
252 Mount_Options = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400253 Format_Block_Size = 0;
Dees_Troy68cab492012-12-12 19:29:35 +0000254 Ignore_Blkid = false;
Dees_Troy16c2b312013-01-15 16:51:18 +0000255 Retain_Layout_Version = false;
Ethan Yonker253368a2014-11-25 15:00:52 -0600256 Crypto_Key_Location = "footer";
Ethan Yonker726a0202014-12-16 20:01:38 -0600257 MTP_Storage_ID = 0;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600258 Can_Flash_Img = false;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500259 Mount_Read_Only = false;
Ethan Yonker66a19492015-12-10 10:19:45 -0600260 Is_Adopted_Storage = false;
261 Adopted_GUID = "";
Ethan Yonker1b190162016-12-05 15:25:19 -0600262 SlotSelect = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400263}
264
265TWPartition::~TWPartition(void) {
266 // Do nothing
267}
268
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600269bool 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 -0500270 char full_line[MAX_FSTAB_LINE_LENGTH];
Matt Mower2416a502016-04-12 19:54:46 -0500271 char twflags[MAX_FSTAB_LINE_LENGTH] = "";
Dees_Troy5bf43922012-09-07 16:07:55 -0400272 char* ptr;
Matt Mower2b2dd152016-04-26 11:24:08 -0500273 int line_len = strlen(fstab_line), index = 0, item_index = 0;
Dees_Troya13d74f2013-03-24 08:54:55 -0500274 bool skip = false;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600275 int fstab_version = 1, mount_point_index = 0, fs_index = 1, block_device_index = 2;
276 TWPartition *additional_entry = NULL;
277 std::map<string, Flags_Map>::iterator it;
Dees_Troy5bf43922012-09-07 16:07:55 -0400278
Matt Mower2b2dd152016-04-26 11:24:08 -0500279 strlcpy(full_line, fstab_line, sizeof(full_line));
Dees_Troy51127312012-09-08 13:08:49 -0400280 for (index = 0; index < line_len; index++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500281 if (full_line[index] == 34)
282 skip = !skip;
283 if (!skip && full_line[index] <= 32)
Dees_Troy5bf43922012-09-07 16:07:55 -0400284 full_line[index] = '\0';
Dees_Troy5bf43922012-09-07 16:07:55 -0400285 }
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600286 if (line_len < 10)
287 return false; // There can't possibly be a valid fstab line that is less than 10 chars
288 if (strncmp(fstab_line, "/dev/", strlen("/dev/")) == 0 || strncmp(fstab_line, "/devices/", strlen("/devices/")) == 0) {
289 fstab_version = 2;
290 block_device_index = 0;
291 mount_point_index = 1;
292 fs_index = 2;
293 }
294
295 index = 0;
Dees_Troy5bf43922012-09-07 16:07:55 -0400296 while (index < line_len) {
297 while (index < line_len && full_line[index] == '\0')
298 index++;
299 if (index >= line_len)
300 continue;
301 ptr = full_line + index;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600302 if (item_index == mount_point_index) {
303 Mount_Point = ptr;
304 if (fstab_version == 2) {
305 additional_entry = PartitionManager.Find_Partition_By_Path(Mount_Point);
306 if (additional_entry) {
307 LOGINFO("Found an additional entry for '%s'\n", Mount_Point.c_str());
308 }
309 }
310 LOGINFO("Processing '%s'\n", Mount_Point.c_str());
311 Backup_Path = Mount_Point;
312 Storage_Path = Mount_Point;
313 Display_Name = ptr + 1;
314 Backup_Display_Name = Display_Name;
315 Storage_Name = Display_Name;
316 item_index++;
317 } else if (item_index == fs_index) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400318 // File System
319 Fstab_File_System = ptr;
320 Current_File_System = ptr;
321 item_index++;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600322 } else if (item_index == block_device_index) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400323 // Primary Block Device
Dees_Troy38bd7602012-09-14 13:33:53 -0400324 if (Fstab_File_System == "mtd" || Fstab_File_System == "yaffs2") {
Dees_Troy094207a2012-09-26 12:00:39 -0400325 MTD_Name = ptr;
326 Find_MTD_Block_Device(MTD_Name);
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400327 } else if (Fstab_File_System == "bml") {
328 if (Mount_Point == "/boot")
329 MTD_Name = "boot";
330 else if (Mount_Point == "/recovery")
331 MTD_Name = "recovery";
332 Primary_Block_Device = ptr;
333 if (*ptr != '/')
Dees_Troy2673cec2013-04-02 20:22:16 +0000334 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 -0400335 } else if (*ptr != '/') {
Dees_Troy5bf43922012-09-07 16:07:55 -0400336 if (Display_Error)
Matt Mower2b2dd152016-04-26 11:24:08 -0500337 LOGERR("Invalid block device '%s' in fstab line '%s'", ptr, fstab_line);
Dees_Troy5bf43922012-09-07 16:07:55 -0400338 else
Matt Mower2b2dd152016-04-26 11:24:08 -0500339 LOGINFO("Invalid block device '%s' in fstab line '%s'", ptr, fstab_line);
Matt Mower72c87ce2016-04-26 14:34:56 -0500340 return false;
Dees_Troy38bd7602012-09-14 13:33:53 -0400341 } else {
342 Primary_Block_Device = ptr;
343 Find_Real_Block_Device(Primary_Block_Device, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400344 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400345 item_index++;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600346 } else if (item_index > 2) {
347 if (fstab_version == 2) {
348 if (item_index == 3) {
349 Process_FS_Flags(ptr);
350 if (additional_entry) {
351 additional_entry->Save_FS_Flags(Fstab_File_System, Mount_Flags, Mount_Options);
352 return false; // We save the extra fs flags in the other partition entry and by returning false, this entry will be deleted
353 }
354 } else {
355 strlcpy(twflags, ptr, sizeof(twflags));
356 }
357 item_index++;
358 } else if (*ptr == '/') { // v2 fstab does not allow alternate block devices
Dees_Troy5bf43922012-09-07 16:07:55 -0400359 // Alternate Block Device
360 Alternate_Block_Device = ptr;
361 Find_Real_Block_Device(Alternate_Block_Device, Display_Error);
362 } else if (strlen(ptr) > 7 && strncmp(ptr, "length=", 7) == 0) {
363 // Partition length
364 ptr += 7;
365 Length = atoi(ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400366 } else if (strlen(ptr) > 6 && strncmp(ptr, "flags=", 6) == 0) {
367 // Custom flags, save for later so that new values aren't overwritten by defaults
368 ptr += 6;
Matt Mower2416a502016-04-12 19:54:46 -0500369 strlcpy(twflags, ptr, sizeof(twflags));
Dees_Troy38bd7602012-09-14 13:33:53 -0400370 } else if (strlen(ptr) == 4 && (strncmp(ptr, "NULL", 4) == 0 || strncmp(ptr, "null", 4) == 0 || strncmp(ptr, "null", 4) == 0)) {
371 // Do nothing
Dees_Troy5bf43922012-09-07 16:07:55 -0400372 } else {
373 // Unhandled data
Matt Mower2b2dd152016-04-26 11:24:08 -0500374 LOGINFO("Unhandled fstab information '%s' in fstab line '%s'\n", ptr, fstab_line);
Dees_Troy5bf43922012-09-07 16:07:55 -0400375 }
376 }
377 while (index < line_len && full_line[index] != '\0')
378 index++;
379 }
380
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600381 // override block devices from the v2 fstab with the ones we read from the twrp.flags file in case they are different
382 if (fstab_version == 2 && twrp_flags && twrp_flags->size() > 0) {
383 it = twrp_flags->find(Mount_Point);
384 if (it != twrp_flags->end()) {
385 if (!it->second.Primary_Block_Device.empty()) {
386 Primary_Block_Device = it->second.Primary_Block_Device;
387 Find_Real_Block_Device(Primary_Block_Device, Display_Error);
388 }
389 if (!it->second.Alternate_Block_Device.empty()) {
390 Alternate_Block_Device = it->second.Alternate_Block_Device;
391 Find_Real_Block_Device(Alternate_Block_Device, Display_Error);
392 }
393 }
394 }
395
396 if (strncmp(fstab_line, "/devices/", strlen("/devices/")) == 0) {
397 Sysfs_Entry = Primary_Block_Device;
398 Primary_Block_Device = "";
399 Is_Storage = true;
400 Removable = true;
401 Wipe_Available_in_GUI = true;
402 Wildcard_Block_Device = true;
403 }
404 if (Primary_Block_Device.find("*") != string::npos)
405 Wildcard_Block_Device = true;
406
407 if (Mount_Point == "auto") {
408 Mount_Point = "/auto";
409 char autoi[5];
410 sprintf(autoi, "%i", auto_index);
411 Mount_Point += autoi;
412 Backup_Path = Mount_Point;
413 Storage_Path = Mount_Point;
414 auto_index++;
415 Setup_File_System(Display_Error);
416 Display_Name = "Storage";
417 Backup_Display_Name = Display_Name;
418 Storage_Name = Display_Name;
419 Can_Be_Backed_Up = false;
420 Wipe_Available_in_GUI = true;
421 Is_Storage = true;
422 Removable = true;
423 Wipe_Available_in_GUI = true;
424 } else if (!Is_File_System(Fstab_File_System) && !Is_Image(Fstab_File_System)) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400425 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000426 LOGERR("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400427 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000428 LOGINFO("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Matt Mower72c87ce2016-04-26 14:34:56 -0500429 return false;
Dees_Troy5bf43922012-09-07 16:07:55 -0400430 } else if (Is_File_System(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400431 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400432 Setup_File_System(Display_Error);
433 if (Mount_Point == "/system") {
434 Display_Name = "System";
Dees_Troya13d74f2013-03-24 08:54:55 -0500435 Backup_Display_Name = Display_Name;
436 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400437 Wipe_Available_in_GUI = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500438 Can_Be_Backed_Up = true;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500439 Mount_Read_Only = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400440 } else if (Mount_Point == "/data") {
441 Display_Name = "Data";
Dees_Troya13d74f2013-03-24 08:54:55 -0500442 Backup_Display_Name = Display_Name;
443 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400444 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400445 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500446 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000447 Can_Encrypt_Backup = true;
448 Use_Userdata_Encryption = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400449 } else if (Mount_Point == "/cache") {
450 Display_Name = "Cache";
Dees_Troya13d74f2013-03-24 08:54:55 -0500451 Backup_Display_Name = Display_Name;
452 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400453 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400454 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500455 Can_Be_Backed_Up = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400456 } else if (Mount_Point == "/datadata") {
Dees_Troy51127312012-09-08 13:08:49 -0400457 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400458 Display_Name = "DataData";
Dees_Troya13d74f2013-03-24 08:54:55 -0500459 Backup_Display_Name = Display_Name;
460 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400461 Is_SubPartition = true;
462 SubPartition_Of = "/data";
Dees_Troy5bf43922012-09-07 16:07:55 -0400463 DataManager::SetValue(TW_HAS_DATADATA, 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500464 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000465 Can_Encrypt_Backup = true;
466 Use_Userdata_Encryption = false; // This whole partition should be encrypted
Dees_Troy5bf43922012-09-07 16:07:55 -0400467 } else if (Mount_Point == "/sd-ext") {
Dees_Troy51127312012-09-08 13:08:49 -0400468 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400469 Display_Name = "SD-Ext";
Dees_Troya13d74f2013-03-24 08:54:55 -0500470 Backup_Display_Name = Display_Name;
471 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400472 Wipe_Available_in_GUI = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400473 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500474 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000475 Can_Encrypt_Backup = true;
476 Use_Userdata_Encryption = true;
Dees_Troy2c50e182012-09-26 20:05:28 -0400477 } else if (Mount_Point == "/boot") {
478 Display_Name = "Boot";
Dees_Troya13d74f2013-03-24 08:54:55 -0500479 Backup_Display_Name = Display_Name;
Dees_Troy2c50e182012-09-26 20:05:28 -0400480 DataManager::SetValue("tw_boot_is_mountable", 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500481 Can_Be_Backed_Up = true;
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500482 } else if (Mount_Point == "/vendor") {
483 Display_Name = "Vendor";
484 Backup_Display_Name = Display_Name;
485 Storage_Name = Display_Name;
486 Mount_Read_Only = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400487 }
488#ifdef TW_EXTERNAL_STORAGE_PATH
489 if (Mount_Point == EXPAND(TW_EXTERNAL_STORAGE_PATH)) {
490 Is_Storage = true;
491 Storage_Path = EXPAND(TW_EXTERNAL_STORAGE_PATH);
Dees_Troyc51f1f92012-09-20 15:32:13 -0400492 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500493 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400494#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000495 if (Mount_Point == "/sdcard" || Mount_Point == "/external_sd" || Mount_Point == "/external_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400496 Is_Storage = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400497 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500498 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400499#endif
Dees_Troyb05ddee2013-01-28 20:24:50 +0000500 }
Dees_Troy8170a922012-09-18 15:40:25 -0400501#ifdef TW_INTERNAL_STORAGE_PATH
502 if (Mount_Point == EXPAND(TW_INTERNAL_STORAGE_PATH)) {
503 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500504 Is_Settings_Storage = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400505 Storage_Path = EXPAND(TW_INTERNAL_STORAGE_PATH);
Dees_Troya13d74f2013-03-24 08:54:55 -0500506 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400507 }
508#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000509 if (Mount_Point == "/emmc" || Mount_Point == "/internal_sd" || Mount_Point == "/internal_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400510 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500511 Is_Settings_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500512 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400513 }
514#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400515 } else if (Is_Image(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400516 Find_Actual_Block_Device();
Ethan Yonker1b190162016-12-05 15:25:19 -0600517 Setup_Image();
Dees_Troya13d74f2013-03-24 08:54:55 -0500518 if (Mount_Point == "/boot") {
519 Display_Name = "Boot";
520 Backup_Display_Name = Display_Name;
521 Can_Be_Backed_Up = true;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600522 Can_Flash_Img = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500523 } else if (Mount_Point == "/recovery") {
524 Display_Name = "Recovery";
525 Backup_Display_Name = Display_Name;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600526 Can_Flash_Img = true;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500527 } else if (Mount_Point == "/system_image") {
528 Display_Name = "System Image";
529 Backup_Display_Name = Display_Name;
HashBanged974bb2016-01-30 14:20:09 -0500530 Can_Flash_Img = true;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500531 Can_Be_Backed_Up = true;
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500532 } else if (Mount_Point == "/vendor_image") {
533 Display_Name = "Vendor Image";
534 Backup_Display_Name = Display_Name;
HashBanged974bb2016-01-30 14:20:09 -0500535 Can_Flash_Img = true;
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500536 Can_Be_Backed_Up = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500537 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400538 }
539
Matt Mower2416a502016-04-12 19:54:46 -0500540 // Process TWRP fstab flags
541 if (strlen(twflags) > 0) {
542 string Prev_Display_Name = Display_Name;
543 string Prev_Storage_Name = Storage_Name;
544 string Prev_Backup_Display_Name = Backup_Display_Name;
545 Display_Name = "";
546 Storage_Name = "";
547 Backup_Display_Name = "";
548
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600549 Process_TW_Flags(twflags, (fstab_version == 1), fstab_version);
550 Save_FS_Flags(Fstab_File_System, Mount_Flags, Mount_Options);
Matt Mower2416a502016-04-12 19:54:46 -0500551
552 bool has_display_name = !Display_Name.empty();
553 bool has_storage_name = !Storage_Name.empty();
554 bool has_backup_name = !Backup_Display_Name.empty();
555 if (!has_display_name) Display_Name = Prev_Display_Name;
556 if (!has_storage_name) Storage_Name = Prev_Storage_Name;
557 if (!has_backup_name) Backup_Display_Name = Prev_Backup_Display_Name;
558
559 if (has_display_name && !has_storage_name)
560 Storage_Name = Display_Name;
561 if (!has_display_name && has_storage_name)
562 Display_Name = Storage_Name;
563 if (has_display_name && !has_backup_name && Backup_Display_Name != "Android Secure")
564 Backup_Display_Name = Display_Name;
565 if (!has_display_name && has_backup_name)
566 Display_Name = Backup_Display_Name;
567 }
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600568
569 if (fstab_version == 2 && twrp_flags && twrp_flags->size() > 0) {
570 it = twrp_flags->find(Mount_Point);
571 if (it != twrp_flags->end()) {
572 char twrpflags[MAX_FSTAB_LINE_LENGTH] = "";
573 int skip = 0;
574 string Flags = it->second.Flags;
575 strcpy(twrpflags, Flags.c_str());
576 if (strlen(twrpflags) > strlen("flags=") && strncmp(twrpflags, "flags=", strlen("flags=")) == 0)
577 skip += strlen("flags=");
578 char* flagptr = twrpflags;
579 flagptr += skip;
580 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
581 }
582 }
nkk7198fc3992017-12-16 16:26:42 +0200583
584 if (Mount_Point == "/persist" && Can_Be_Mounted) {
585 bool mounted = Is_Mounted();
586 if (mounted || Mount(false)) {
587 // Read the backup settings file
588 DataManager::LoadPersistValues();
589 TWFunc::Fixup_Time_On_Boot("/persist/time/");
590 if (!mounted)
591 UnMount(false);
592 }
593 }
594
Dees_Troy51127312012-09-08 13:08:49 -0400595 return true;
596}
597
Matt Mower72c87ce2016-04-26 14:34:56 -0500598void TWPartition::Partition_Post_Processing(bool Display_Error) {
599 if (Mount_Point == "/data")
600 Setup_Data_Partition(Display_Error);
601 else if (Mount_Point == "/cache")
602 Setup_Cache_Partition(Display_Error);
603}
604
Ethan Yonkerbd7492d2016-12-07 13:55:01 -0600605void TWPartition::ExcludeAll(const string& path) {
606 backup_exclusions.add_absolute_dir(path);
607 wipe_exclusions.add_absolute_dir(path);
608}
609
Matt Mower72c87ce2016-04-26 14:34:56 -0500610void TWPartition::Setup_Data_Partition(bool Display_Error) {
611 if (Mount_Point != "/data")
612 return;
613
614 // Ensure /data is not mounted as tmpfs for qcom hardware decrypt
615 UnMount(false);
616
617#ifdef TW_INCLUDE_CRYPTO
618 if (datamedia)
619 Setup_Data_Media();
620 Can_Be_Encrypted = true;
621 char crypto_blkdev[255];
622 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
623 if (strcmp(crypto_blkdev, "error") != 0) {
624 DataManager::SetValue(TW_IS_DECRYPTED, 1);
625 Is_Encrypted = true;
626 Is_Decrypted = true;
Ethan Yonkerbd7492d2016-12-07 13:55:01 -0600627 Is_FBE = false;
628 DataManager::SetValue(TW_IS_FBE, 0);
Matt Mower72c87ce2016-04-26 14:34:56 -0500629 Decrypted_Block_Device = crypto_blkdev;
630 LOGINFO("Data already decrypted, new block device: '%s'\n", crypto_blkdev);
631 } else if (!Mount(false)) {
632 if (Is_Present) {
633 set_partition_data(Actual_Block_Device.c_str(), Crypto_Key_Location.c_str(), Fstab_File_System.c_str());
634 if (cryptfs_check_footer() == 0) {
635 Is_Encrypted = true;
636 Is_Decrypted = false;
637 Can_Be_Mounted = false;
638 Current_File_System = "emmc";
Ethan Yonker1b190162016-12-05 15:25:19 -0600639 Setup_Image();
Matt Mower72c87ce2016-04-26 14:34:56 -0500640 DataManager::SetValue(TW_IS_ENCRYPTED, 1);
641 DataManager::SetValue(TW_CRYPTO_PWTYPE, cryptfs_get_password_type());
642 DataManager::SetValue(TW_CRYPTO_PASSWORD, "");
643 DataManager::SetValue("tw_crypto_display", "");
644 } else {
645 gui_err("mount_data_footer=Could not mount /data and unable to find crypto footer.");
646 }
647 } else {
648 LOGERR("Primary block device '%s' for mount point '%s' is not present!\n", Primary_Block_Device.c_str(), Mount_Point.c_str());
649 }
650 } else {
Ethan Yonkerbd7492d2016-12-07 13:55:01 -0600651 if (TWFunc::Path_Exists("/data/unencrypted/key/version")) {
652 LOGINFO("File Based Encryption is present\n");
653#ifdef TW_INCLUDE_FBE
654 ExcludeAll(Mount_Point + "/convert_fbe");
655 ExcludeAll(Mount_Point + "/unencrypted");
656 //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
657 ExcludeAll(Mount_Point + "/misc/vold/user_keys");
Ethan Yonker79f88bd2016-12-09 14:52:12 -0600658 //ExcludeAll(Mount_Point + "/system_ce");
659 //ExcludeAll(Mount_Point + "/system_de");
660 //ExcludeAll(Mount_Point + "/misc_ce");
661 //ExcludeAll(Mount_Point + "/misc_de");
Ethan Yonkerbd7492d2016-12-07 13:55:01 -0600662 ExcludeAll(Mount_Point + "/system/gatekeeper.password.key");
663 ExcludeAll(Mount_Point + "/system/gatekeeper.pattern.key");
664 ExcludeAll(Mount_Point + "/system/locksettings.db");
665 //ExcludeAll(Mount_Point + "/system/locksettings.db-shm"); // don't seem to need this one, but the other 2 are needed
666 ExcludeAll(Mount_Point + "/system/locksettings.db-wal");
Ethan Yonker79f88bd2016-12-09 14:52:12 -0600667 //ExcludeAll(Mount_Point + "/user_de");
Ethan Yonkerbd7492d2016-12-07 13:55:01 -0600668 //ExcludeAll(Mount_Point + "/misc/profiles/cur/0"); // might be important later
669 ExcludeAll(Mount_Point + "/misc/gatekeeper");
Ethan Yonkerfefe5912017-09-30 22:22:13 -0500670 ExcludeAll(Mount_Point + "/misc/keystore");
Ethan Yonkerbd7492d2016-12-07 13:55:01 -0600671 ExcludeAll(Mount_Point + "/drm/kek.dat");
Ethan Yonkerfefe5912017-09-30 22:22:13 -0500672 ExcludeAll(Mount_Point + "/system_de/0/spblob"); // contains data needed to decrypt pixel 2
Ethan Yonkerbd7492d2016-12-07 13:55:01 -0600673 int retry_count = 3;
674 while (!Decrypt_DE() && --retry_count)
675 usleep(2000);
676 if (retry_count > 0) {
677 property_set("ro.crypto.state", "encrypted");
678 Is_Encrypted = true;
679 Is_Decrypted = false;
680 Is_FBE = true;
681 DataManager::SetValue(TW_IS_FBE, 1);
682 DataManager::SetValue(TW_IS_ENCRYPTED, 1);
683 string filename;
Ethan Yonkerfefe5912017-09-30 22:22:13 -0500684 int pwd_type = Get_Password_Type(0, filename);
685 if (pwd_type < 0) {
686 LOGERR("This TWRP does not have synthetic password decrypt support\n");
687 pwd_type = 0; // default password
688 }
689 DataManager::SetValue(TW_CRYPTO_PWTYPE, pwd_type);
Ethan Yonkerbd7492d2016-12-07 13:55:01 -0600690 DataManager::SetValue(TW_CRYPTO_PASSWORD, "");
691 DataManager::SetValue("tw_crypto_display", "");
692 }
693#else
694 LOGERR("FBE found but FBE support not present in TWRP\n");
695#endif
696 } else {
697 // Filesystem is not encrypted and the mount succeeded, so return to
698 // the original unmounted state
699 UnMount(false);
700 }
Matt Mower72c87ce2016-04-26 14:34:56 -0500701 }
702 if (datamedia && (!Is_Encrypted || (Is_Encrypted && Is_Decrypted))) {
703 Setup_Data_Media();
704 Recreate_Media_Folder();
705 }
706#else
707 if (datamedia) {
708 Setup_Data_Media();
709 Recreate_Media_Folder();
710 }
711#endif
712}
713
714void TWPartition::Setup_Cache_Partition(bool Display_Error __unused) {
715 if (Mount_Point != "/cache")
716 return;
717
718 if (!Mount(true))
719 return;
720
721 if (!TWFunc::Path_Exists("/cache/recovery/.")) {
722 LOGINFO("Recreating /cache/recovery folder\n");
723 if (mkdir("/cache/recovery", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0)
724 LOGERR("Could not create /cache/recovery\n");
725 }
726}
727
Matt Mower4ab42b12016-04-21 13:52:18 -0500728void TWPartition::Process_FS_Flags(const char *str) {
729 char *options = strdup(str);
730 char *ptr, *savep;
Hashcode62bd9e02013-11-19 21:59:42 -0800731
Matt Mower4ab42b12016-04-21 13:52:18 -0500732 Mount_Options = "";
Hashcode62bd9e02013-11-19 21:59:42 -0800733
Matt Mower4ab42b12016-04-21 13:52:18 -0500734 // Avoid issues with potentially nested strtok by using strtok_r
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600735 for (ptr = strtok_r(options, ",", &savep); ptr; ptr = strtok_r(NULL, ",", &savep)) {
736 char *equals = strstr(ptr, "=");
737 size_t name_len;
Matt Mower4ab42b12016-04-21 13:52:18 -0500738
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600739 if (!equals)
740 name_len = strlen(ptr);
741 else
742 name_len = equals - ptr;
743
744 // There are some flags that we want to ignore in TWRP
745 bool found_match = false;
746 for (const char** ignored_mount_item = ignored_mount_items; *ignored_mount_item; ignored_mount_item++) {
747 if (strncmp(ptr, *ignored_mount_item, name_len) == 0) {
748 found_match = true;
Hashcode62bd9e02013-11-19 21:59:42 -0800749 break;
750 }
751 }
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600752 if (found_match)
753 continue;
Hashcode62bd9e02013-11-19 21:59:42 -0800754
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600755 // mount_flags are never postfixed by '='
756 if (!equals) {
757 const struct flag_list* mount_flag = mount_flags;
758 for (; mount_flag->name; mount_flag++) {
759 if (strcmp(ptr, mount_flag->name) == 0) {
760 if (mount_flag->flag == MS_RDONLY)
761 Mount_Read_Only = true;
762 else
763 Mount_Flags |= (unsigned)mount_flag->flag;
764 found_match = true;
765 break;
766 }
767 }
768 if (found_match)
769 continue;
Matt Mower4ab42b12016-04-21 13:52:18 -0500770 }
771
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600772 // If we aren't ignoring this flag and it's not a mount flag, then it must be a mount option
773 if (!Mount_Options.empty())
774 Mount_Options += ",";
775 Mount_Options += ptr;
Matt Mower4ab42b12016-04-21 13:52:18 -0500776 }
777 free(options);
Hashcode62bd9e02013-11-19 21:59:42 -0800778}
779
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600780void TWPartition::Save_FS_Flags(const string& local_File_System, int local_Mount_Flags, const string& local_Mount_Options) {
781 partition_fs_flags_struct flags;
782 flags.File_System = local_File_System;
783 flags.Mount_Flags = local_Mount_Flags;
784 flags.Mount_Options = local_Mount_Options;
785 fs_flags.push_back(flags);
786}
787
Matt Mower2416a502016-04-12 19:54:46 -0500788void TWPartition::Apply_TW_Flag(const unsigned flag, const char* str, const bool val) {
789 switch (flag) {
790 case TWFLAG_ANDSEC:
791 Has_Android_Secure = val;
792 break;
793 case TWFLAG_BACKUP:
794 Can_Be_Backed_Up = val;
795 break;
796 case TWFLAG_BACKUPNAME:
797 Backup_Display_Name = str;
798 break;
799 case TWFLAG_BLOCKSIZE:
800 Format_Block_Size = (unsigned long)(atol(str));
801 break;
802 case TWFLAG_CANBEWIPED:
803 Can_Be_Wiped = val;
804 break;
805 case TWFLAG_CANENCRYPTBACKUP:
806 Can_Encrypt_Backup = val;
807 break;
808 case TWFLAG_DEFAULTS:
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600809 case TWFLAG_WAIT:
810 case TWFLAG_VERIFY:
811 case TWFLAG_CHECK:
812 case TWFLAG_NOTRIM:
813 case TWFLAG_VOLDMANAGED:
814 case TWFLAG_RESIZE:
Matt Mower2416a502016-04-12 19:54:46 -0500815 // Do nothing
816 break;
817 case TWFLAG_DISPLAY:
818 Display_Name = str;
819 break;
820 case TWFLAG_ENCRYPTABLE:
821 case TWFLAG_FORCEENCRYPT:
822 Crypto_Key_Location = str;
823 break;
Ethan Yonkere9afc3d2018-08-30 15:16:27 -0500824 case TWFLAG_FILEENCRYPTION:
825 // This flag isn't used by TWRP but is needed in 9.0 FBE decrypt
826 // fileencryption=ice:aes-256-heh
827 {
828 std::string FBE = str;
829 std::string FBE_contents, FBE_filenames;
830 size_t colon_loc = FBE.find(":");
831 if (colon_loc == std::string::npos) {
832 LOGINFO("Invalid fileencryption fstab flag: '%s'\n", str);
833 break;
834 }
835 FBE_contents = FBE.substr(0, colon_loc);
836 FBE_filenames = FBE.substr(colon_loc + 1);
837 property_set("fbe.contents", FBE_contents.c_str());
838 property_set("fbe.filenames", FBE_filenames.c_str());
839 LOGINFO("FBE contents '%s', filenames '%s'\n", FBE_contents.c_str(), FBE_filenames.c_str());
840 }
841 break;
Matt Mower2416a502016-04-12 19:54:46 -0500842 case TWFLAG_FLASHIMG:
843 Can_Flash_Img = val;
844 break;
845 case TWFLAG_FSFLAGS:
Matt Mower4ab42b12016-04-21 13:52:18 -0500846 Process_FS_Flags(str);
Matt Mower2416a502016-04-12 19:54:46 -0500847 break;
848 case TWFLAG_IGNOREBLKID:
849 Ignore_Blkid = val;
850 break;
851 case TWFLAG_LENGTH:
852 Length = atoi(str);
853 break;
854 case TWFLAG_MOUNTTODECRYPT:
855 Mount_To_Decrypt = val;
856 break;
857 case TWFLAG_REMOVABLE:
858 Removable = val;
859 break;
860 case TWFLAG_RETAINLAYOUTVERSION:
861 Retain_Layout_Version = val;
862 break;
863 case TWFLAG_SETTINGSSTORAGE:
864 Is_Settings_Storage = val;
865 if (Is_Settings_Storage)
866 Is_Storage = true;
867 break;
868 case TWFLAG_STORAGE:
869 Is_Storage = val;
870 break;
871 case TWFLAG_STORAGENAME:
872 Storage_Name = str;
873 break;
874 case TWFLAG_SUBPARTITIONOF:
875 Is_SubPartition = true;
876 SubPartition_Of = str;
877 break;
878 case TWFLAG_SYMLINK:
879 Symlink_Path = str;
880 break;
881 case TWFLAG_USERDATAENCRYPTBACKUP:
882 Use_Userdata_Encryption = val;
883 if (Use_Userdata_Encryption)
884 Can_Encrypt_Backup = true;
885 break;
886 case TWFLAG_USERMRF:
887 Use_Rm_Rf = val;
888 break;
889 case TWFLAG_WIPEDURINGFACTORYRESET:
890 Wipe_During_Factory_Reset = val;
891 if (Wipe_During_Factory_Reset) {
892 Can_Be_Wiped = true;
893 Wipe_Available_in_GUI = true;
894 }
895 break;
896 case TWFLAG_WIPEINGUI:
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600897 case TWFLAG_FORMATTABLE:
Matt Mower2416a502016-04-12 19:54:46 -0500898 Wipe_Available_in_GUI = val;
899 if (Wipe_Available_in_GUI)
900 Can_Be_Wiped = true;
901 break;
Ethan Yonker1b190162016-12-05 15:25:19 -0600902 case TWFLAG_SLOTSELECT:
903 SlotSelect = true;
904 break;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600905 case TWFLAG_ALTDEVICE:
906 Alternate_Block_Device = str;
907 break;
Matt Mower2416a502016-04-12 19:54:46 -0500908 default:
909 // Should not get here
910 LOGINFO("Flag identified for processing, but later unmatched: %i\n", flag);
911 break;
912 }
913}
Dees_Troy51127312012-09-08 13:08:49 -0400914
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600915void TWPartition::Process_TW_Flags(char *flags, bool Display_Error, int fstab_ver) {
Matt Mower2416a502016-04-12 19:54:46 -0500916 char separator[2] = {'\n', 0};
917 char *ptr, *savep;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600918 char source_separator = ';';
919
920 if (fstab_ver == 2)
921 source_separator = ',';
Matt Mower2416a502016-04-12 19:54:46 -0500922
923 // Semicolons within double-quotes are not forbidden, so replace
924 // only the semicolons intended as separators with '\n' for strtok
925 for (unsigned i = 0, skip = 0; i < strlen(flags); i++) {
926 if (flags[i] == '\"')
Dees_Troya13d74f2013-03-24 08:54:55 -0500927 skip = !skip;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600928 if (!skip && flags[i] == source_separator)
Matt Mower2416a502016-04-12 19:54:46 -0500929 flags[i] = separator[0];
Dees_Troy51127312012-09-08 13:08:49 -0400930 }
931
Matt Mower2416a502016-04-12 19:54:46 -0500932 // Avoid issues with potentially nested strtok by using strtok_r
933 ptr = strtok_r(flags, separator, &savep);
934 while (ptr) {
935 int ptr_len = strlen(ptr);
936 const struct flag_list* tw_flag = tw_flags;
Hashcode62bd9e02013-11-19 21:59:42 -0800937
Matt Mower2416a502016-04-12 19:54:46 -0500938 for (; tw_flag->name; tw_flag++) {
939 int flag_len = strlen(tw_flag->name);
940
941 if (strncmp(ptr, tw_flag->name, flag_len) == 0) {
942 bool flag_val = false;
943
944 if (ptr_len > flag_len && (tw_flag->name)[flag_len-1] != '='
945 && ptr[flag_len] != '=') {
946 // Handle flags with same starting string
947 // (e.g. backup and backupname)
948 continue;
949 } else if (ptr_len > flag_len && ptr[flag_len] == '=') {
950 // Handle flags with dual format: Part 1
951 // (e.g. backup and backup=y. backup=y handled here)
952 ptr += flag_len + 1;
953 TWFunc::Strip_Quotes(ptr);
954 // Skip flags with empty argument
955 // (e.g. backup=)
956 if (strlen(ptr) == 0) {
957 LOGINFO("Flag missing argument or should not include '=': %s=\n", tw_flag->name);
958 break;
959 }
960 flag_val = strchr("yY1", *ptr) != NULL;
961 } else if (ptr_len == flag_len
962 && (tw_flag->name)[flag_len-1] == '=') {
963 // Skip flags missing argument after =
964 // (e.g. backupname=)
965 LOGINFO("Flag missing argument: %s\n", tw_flag->name);
966 break;
967 } else if (ptr_len > flag_len
968 && (tw_flag->name)[flag_len-1] == '=') {
969 // Handle arguments to flags
970 // (e.g. backupname="My Stuff")
971 ptr += flag_len;
972 TWFunc::Strip_Quotes(ptr);
973 // Skip flags with empty argument
974 // (e.g. backupname="")
975 if (strlen(ptr) == 0) {
976 LOGINFO("Flag missing argument: %s\n", tw_flag->name);
977 break;
978 }
979 } else if (ptr_len == flag_len) {
980 // Handle flags with dual format: Part 2
981 // (e.g. backup and backup=y. backup handled here)
982 flag_val = true;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600983 } else {
Matt Mower2416a502016-04-12 19:54:46 -0500984 LOGINFO("Flag matched, but could not be processed: %s\n", ptr);
985 break;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600986 }
Matt Mower2416a502016-04-12 19:54:46 -0500987
988 Apply_TW_Flag(tw_flag->flag, ptr, flag_val);
989 break;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600990 }
Matt Mower2416a502016-04-12 19:54:46 -0500991 }
992 if (tw_flag->name == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400993 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000994 LOGERR("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400995 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000996 LOGINFO("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400997 }
Matt Mower2416a502016-04-12 19:54:46 -0500998 ptr = strtok_r(NULL, separator, &savep);
Dees_Troy51127312012-09-08 13:08:49 -0400999 }
Dees_Troy51a0e822012-09-05 15:24:24 -04001000}
1001
Dees_Troy5bf43922012-09-07 16:07:55 -04001002bool TWPartition::Is_File_System(string File_System) {
1003 if (File_System == "ext2" ||
Dees_Troy63c8df72012-09-10 14:02:05 -04001004 File_System == "ext3" ||
Dees_Troy5bf43922012-09-07 16:07:55 -04001005 File_System == "ext4" ||
1006 File_System == "vfat" ||
1007 File_System == "ntfs" ||
1008 File_System == "yaffs2" ||
bigbiff bigbiff3e146522012-11-14 14:32:59 -05001009 File_System == "exfat" ||
Dees_Troye5017042013-08-29 16:38:55 +00001010 File_System == "f2fs" ||
Dees_Troy5bf43922012-09-07 16:07:55 -04001011 File_System == "auto")
1012 return true;
1013 else
1014 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001015}
1016
Dees_Troy5bf43922012-09-07 16:07:55 -04001017bool TWPartition::Is_Image(string File_System) {
Dees_Troy5fcd8f92012-10-16 12:22:05 -04001018 if (File_System == "emmc" || File_System == "mtd" || File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -04001019 return true;
1020 else
1021 return false;
1022}
1023
Dees_Troy51127312012-09-08 13:08:49 -04001024bool TWPartition::Make_Dir(string Path, bool Display_Error) {
Ethan Yonker93ac7a02016-11-07 22:05:58 -06001025 if (TWFunc::Get_D_Type_From_Stat(Path) != S_IFDIR)
1026 unlink(Path.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001027 if (!TWFunc::Path_Exists(Path)) {
Dees_Troy51127312012-09-08 13:08:49 -04001028 if (mkdir(Path.c_str(), 0777) == -1) {
1029 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001030 gui_msg(Msg(msg::kError, "create_folder_strerr=Can not create '{1}' folder ({2}).")(Path)(strerror(errno)));
Dees_Troy51127312012-09-08 13:08:49 -04001031 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001032 LOGINFO("Can not create '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -04001033 return false;
1034 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001035 LOGINFO("Created '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -04001036 return true;
1037 }
1038 }
1039 return true;
1040}
1041
Dees_Troy5bf43922012-09-07 16:07:55 -04001042void TWPartition::Setup_File_System(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001043 Can_Be_Mounted = true;
1044 Can_Be_Wiped = true;
1045
Dees_Troy5bf43922012-09-07 16:07:55 -04001046 // Make the mount point folder if it doesn't exist
Dees_Troy51127312012-09-08 13:08:49 -04001047 Make_Dir(Mount_Point, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -04001048 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
1049 Backup_Name = Display_Name;
bigbiffce8f83c2015-12-12 18:30:21 -05001050 Backup_Method = BM_FILES;
Dees_Troy5bf43922012-09-07 16:07:55 -04001051}
1052
Ethan Yonker1b190162016-12-05 15:25:19 -06001053void TWPartition::Setup_Image() {
Dees_Troy5bf43922012-09-07 16:07:55 -04001054 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
1055 Backup_Name = Display_Name;
Gary Peck82599a82012-11-21 16:23:12 -08001056 if (Current_File_System == "emmc")
bigbiffce8f83c2015-12-12 18:30:21 -05001057 Backup_Method = BM_DD;
Gary Peck82599a82012-11-21 16:23:12 -08001058 else if (Current_File_System == "mtd" || Current_File_System == "bml")
bigbiffce8f83c2015-12-12 18:30:21 -05001059 Backup_Method = BM_FLASH_UTILS;
Dees_Troy5bf43922012-09-07 16:07:55 -04001060 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001061 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 -04001062}
1063
Dees_Troye58d5262012-09-21 12:27:57 -04001064void TWPartition::Setup_AndSec(void) {
Dees_Troya13d74f2013-03-24 08:54:55 -05001065 Backup_Display_Name = "Android Secure";
Dees_Troye58d5262012-09-21 12:27:57 -04001066 Backup_Name = "and-sec";
Dees_Troya13d74f2013-03-24 08:54:55 -05001067 Can_Be_Backed_Up = true;
Dees_Troye58d5262012-09-21 12:27:57 -04001068 Has_Android_Secure = true;
1069 Symlink_Path = Mount_Point + "/.android_secure";
1070 Symlink_Mount_Point = "/and-sec";
1071 Backup_Path = Symlink_Mount_Point;
1072 Make_Dir("/and-sec", true);
1073 Recreate_AndSec_Folder();
Kjell Braden3126a112016-06-19 16:58:15 +00001074 Mount_Storage_Retry(true);
Dees_Troye58d5262012-09-21 12:27:57 -04001075}
1076
that9e0593e2014-10-08 00:01:24 +02001077void TWPartition::Setup_Data_Media() {
Ethan Yonker6277c792014-09-15 14:54:30 -05001078 LOGINFO("Setting up '%s' as data/media emulated storage.\n", Mount_Point.c_str());
Ethan Yonker66a19492015-12-10 10:19:45 -06001079 if (Storage_Name.empty() || Storage_Name == "Data")
1080 Storage_Name = "Internal Storage";
Ethan Yonker6277c792014-09-15 14:54:30 -05001081 Has_Data_Media = true;
1082 Is_Storage = true;
Ethan Yonker66a19492015-12-10 10:19:45 -06001083 Storage_Path = Mount_Point + "/media";
Ethan Yonker6277c792014-09-15 14:54:30 -05001084 Symlink_Path = Storage_Path;
Ethan Yonker66a19492015-12-10 10:19:45 -06001085 if (Mount_Point == "/data") {
1086 Is_Settings_Storage = true;
1087 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
1088 Make_Dir("/emmc", false);
1089 Symlink_Mount_Point = "/emmc";
1090 } else {
1091 Make_Dir("/sdcard", false);
1092 Symlink_Mount_Point = "/sdcard";
1093 }
1094 if (Mount(false) && TWFunc::Path_Exists(Mount_Point + "/media/0")) {
1095 Storage_Path = Mount_Point + "/media/0";
1096 Symlink_Path = Storage_Path;
1097 DataManager::SetValue(TW_INTERNAL_PATH, Mount_Point + "/media/0");
1098 UnMount(true);
1099 }
1100 DataManager::SetValue("tw_has_internal", 1);
1101 DataManager::SetValue("tw_has_data_media", 1);
Ethan Yonker3fdcda42016-11-30 12:29:37 -06001102 backup_exclusions.add_absolute_dir("/data/data/com.google.android.music/files");
Ethan Yonker6355b562017-05-01 09:42:17 -05001103 wipe_exclusions.add_absolute_dir(Mount_Point + "/misc/vold"); // adopted storage keys
Ethan Yonker79f88bd2016-12-09 14:52:12 -06001104 ExcludeAll(Mount_Point + "/.layout_version");
1105 ExcludeAll(Mount_Point + "/system/storage.xml");
Ethan Yonker6277c792014-09-15 14:54:30 -05001106 } else {
Ethan Yonker66a19492015-12-10 10:19:45 -06001107 if (Mount(true) && TWFunc::Path_Exists(Mount_Point + "/media/0")) {
1108 Storage_Path = Mount_Point + "/media/0";
1109 Symlink_Path = Storage_Path;
1110 UnMount(true);
1111 }
Ethan Yonker6277c792014-09-15 14:54:30 -05001112 }
Ethan Yonker79f88bd2016-12-09 14:52:12 -06001113 ExcludeAll(Mount_Point + "/media");
Ethan Yonker6277c792014-09-15 14:54:30 -05001114}
1115
Dees_Troy5bf43922012-09-07 16:07:55 -04001116void TWPartition::Find_Real_Block_Device(string& Block, bool Display_Error) {
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06001117 char device[PATH_MAX], realDevice[PATH_MAX];
Dees_Troy5bf43922012-09-07 16:07:55 -04001118
1119 strcpy(device, Block.c_str());
1120 memset(realDevice, 0, sizeof(realDevice));
1121 while (readlink(device, realDevice, sizeof(realDevice)) > 0)
1122 {
1123 strcpy(device, realDevice);
1124 memset(realDevice, 0, sizeof(realDevice));
1125 }
1126
1127 if (device[0] != '/') {
1128 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001129 LOGERR("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001130 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001131 LOGINFO("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001132 return;
Dees_Troy51a0e822012-09-05 15:24:24 -04001133 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -04001134 Block = device;
1135 return;
1136 }
1137}
1138
Kjell Braden3126a112016-06-19 16:58:15 +00001139bool TWPartition::Mount_Storage_Retry(bool Display_Error) {
Dees_Troy8e337f32012-10-13 22:07:49 -04001140 // On some devices, storage doesn't want to mount right away, retry and sleep
Kjell Braden3126a112016-06-19 16:58:15 +00001141 if (!Mount(Display_Error)) {
Dees_Troy8e337f32012-10-13 22:07:49 -04001142 int retry_count = 5;
1143 while (retry_count > 0 && !Mount(false)) {
1144 usleep(500000);
1145 retry_count--;
1146 }
Kjell Braden3126a112016-06-19 16:58:15 +00001147 return Mount(Display_Error);
Dees_Troy8e337f32012-10-13 22:07:49 -04001148 }
Kjell Braden3126a112016-06-19 16:58:15 +00001149 return true;
Dees_Troy8e337f32012-10-13 22:07:49 -04001150}
1151
Dees_Troy38bd7602012-09-14 13:33:53 -04001152bool TWPartition::Find_MTD_Block_Device(string MTD_Name) {
1153 FILE *fp = NULL;
1154 char line[255];
1155
1156 fp = fopen("/proc/mtd", "rt");
1157 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001158 LOGERR("Device does not support /proc/mtd\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001159 return false;
1160 }
1161
1162 while (fgets(line, sizeof(line), fp) != NULL)
1163 {
1164 char device[32], label[32];
1165 unsigned long size = 0;
Dees_Troy38bd7602012-09-14 13:33:53 -04001166 int deviceId;
1167
1168 sscanf(line, "%s %lx %*s %*c%s", device, &size, label);
1169
1170 // Skip header and blank lines
1171 if ((strcmp(device, "dev:") == 0) || (strlen(line) < 8))
1172 continue;
1173
1174 // Strip off the trailing " from the label
1175 label[strlen(label)-1] = '\0';
1176
1177 if (strcmp(label, MTD_Name.c_str()) == 0) {
1178 // We found our device
1179 // Strip off the trailing : from the device
1180 device[strlen(device)-1] = '\0';
1181 if (sscanf(device,"mtd%d", &deviceId) == 1) {
1182 sprintf(device, "/dev/block/mtdblock%d", deviceId);
1183 Primary_Block_Device = device;
Dees_Troy76543db2013-06-19 16:24:30 +00001184 fclose(fp);
1185 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -04001186 }
1187 }
1188 }
1189 fclose(fp);
1190
1191 return false;
1192}
1193
Dees_Troy51127312012-09-08 13:08:49 -04001194bool TWPartition::Get_Size_Via_statfs(bool Display_Error) {
1195 struct statfs st;
1196 string Local_Path = Mount_Point + "/.";
1197
1198 if (!Mount(Display_Error))
1199 return false;
1200
1201 if (statfs(Local_Path.c_str(), &st) != 0) {
1202 if (!Removable) {
1203 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001204 LOGERR("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -04001205 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001206 LOGINFO("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -04001207 }
1208 return false;
1209 }
1210 Size = (st.f_blocks * st.f_bsize);
1211 Used = ((st.f_blocks - st.f_bfree) * st.f_bsize);
1212 Free = (st.f_bfree * st.f_bsize);
1213 Backup_Size = Used;
1214 return true;
1215}
1216
1217bool TWPartition::Get_Size_Via_df(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001218 FILE* fp;
1219 char command[255], line[512];
1220 int include_block = 1;
1221 unsigned int min_len;
1222
1223 if (!Mount(Display_Error))
1224 return false;
1225
Dees_Troy38bd7602012-09-14 13:33:53 -04001226 min_len = Actual_Block_Device.size() + 2;
Dees_Troy51127312012-09-08 13:08:49 -04001227 sprintf(command, "df %s > /tmp/dfoutput.txt", Mount_Point.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001228 TWFunc::Exec_Cmd(command);
Dees_Troy51127312012-09-08 13:08:49 -04001229 fp = fopen("/tmp/dfoutput.txt", "rt");
1230 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001231 LOGINFO("Unable to open /tmp/dfoutput.txt.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001232 return false;
Dees_Troy51127312012-09-08 13:08:49 -04001233 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001234
1235 while (fgets(line, sizeof(line), fp) != NULL)
1236 {
1237 unsigned long blocks, used, available;
1238 char device[64];
1239 char tmpString[64];
1240
1241 if (strncmp(line, "Filesystem", 10) == 0)
1242 continue;
1243 if (strlen(line) < min_len) {
1244 include_block = 0;
1245 continue;
1246 }
1247 if (include_block) {
1248 sscanf(line, "%s %lu %lu %lu", device, &blocks, &used, &available);
1249 } else {
1250 // The device block string is so long that the df information is on the next line
1251 int space_count = 0;
Dees_Troye58d5262012-09-21 12:27:57 -04001252 sprintf(tmpString, "/dev/block/%s", Actual_Block_Device.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001253 while (tmpString[space_count] == 32)
1254 space_count++;
1255 sscanf(line + space_count, "%lu %lu %lu", &blocks, &used, &available);
1256 }
1257
1258 // Adjust block size to byte size
1259 Size = blocks * 1024ULL;
1260 Used = used * 1024ULL;
1261 Free = available * 1024ULL;
1262 Backup_Size = Used;
1263 }
1264 fclose(fp);
1265 return true;
1266}
1267
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001268unsigned long long TWPartition::IOCTL_Get_Block_Size() {
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001269 Find_Actual_Block_Device();
Ethan Yonker483e9f42016-01-11 22:21:18 -06001270
1271 return TWFunc::IOCTL_Get_Block_Size(Actual_Block_Device.c_str());
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001272}
1273
Dees_Troy5bf43922012-09-07 16:07:55 -04001274bool TWPartition::Find_Partition_Size(void) {
1275 FILE* fp;
1276 char line[512];
1277 string tmpdevice;
1278
igoriok87e3d932013-01-31 21:03:53 +02001279 fp = fopen("/proc/dumchar_info", "rt");
1280 if (fp != NULL) {
1281 while (fgets(line, sizeof(line), fp) != NULL)
1282 {
1283 char label[32], device[32];
1284 unsigned long size = 0;
1285
thatd43bf2d2014-09-21 23:13:02 +02001286 sscanf(line, "%s %lx %*x %*u %s", label, &size, device);
igoriok87e3d932013-01-31 21:03:53 +02001287
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05001288 // Skip header, annotation and blank lines
igoriok87e3d932013-01-31 21:03:53 +02001289 if ((strncmp(device, "/dev/", 5) != 0) || (strlen(line) < 8))
1290 continue;
1291
1292 tmpdevice = "/dev/";
1293 tmpdevice += label;
1294 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
1295 Size = size;
1296 fclose(fp);
1297 return true;
1298 }
1299 }
1300 }
1301
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001302 unsigned long long ioctl_size = IOCTL_Get_Block_Size();
1303 if (ioctl_size) {
1304 Size = ioctl_size;
1305 return true;
1306 }
1307
Dees_Troy5bf43922012-09-07 16:07:55 -04001308 // In this case, we'll first get the partitions we care about (with labels)
1309 fp = fopen("/proc/partitions", "rt");
1310 if (fp == NULL)
1311 return false;
1312
1313 while (fgets(line, sizeof(line), fp) != NULL)
1314 {
1315 unsigned long major, minor, blocks;
1316 char device[512];
Dees_Troy5bf43922012-09-07 16:07:55 -04001317
Dees_Troy63c8df72012-09-10 14:02:05 -04001318 if (strlen(line) < 7 || line[0] == 'm') continue;
Dees_Troy5bf43922012-09-07 16:07:55 -04001319 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
1320
1321 tmpdevice = "/dev/block/";
1322 tmpdevice += device;
Dees_Troy38bd7602012-09-14 13:33:53 -04001323 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001324 // Adjust block size to byte size
1325 Size = blocks * 1024ULL;
1326 fclose(fp);
1327 return true;
1328 }
1329 }
1330 fclose(fp);
1331 return false;
1332}
1333
Dees_Troy5bf43922012-09-07 16:07:55 -04001334bool TWPartition::Is_Mounted(void) {
1335 if (!Can_Be_Mounted)
1336 return false;
1337
1338 struct stat st1, st2;
1339 string test_path;
1340
1341 // Check to see if the mount point directory exists
1342 test_path = Mount_Point + "/.";
1343 if (stat(test_path.c_str(), &st1) != 0) return false;
1344
1345 // Check to see if the directory above the mount point exists
1346 test_path = Mount_Point + "/../.";
1347 if (stat(test_path.c_str(), &st2) != 0) return false;
1348
1349 // Compare the device IDs -- if they match then we're (probably) using tmpfs instead of an actual device
1350 int ret = (st1.st_dev != st2.st_dev) ? true : false;
1351
1352 return ret;
1353}
1354
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05001355bool TWPartition::Is_File_System_Writable(void) {
1356 if (!Is_File_System(Current_File_System) || !Is_Mounted())
1357 return false;
1358
1359 string test_path = Mount_Point + "/.";
1360 return (access(test_path.c_str(), W_OK) == 0);
1361}
1362
Dees_Troy5bf43922012-09-07 16:07:55 -04001363bool TWPartition::Mount(bool Display_Error) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001364 int exfat_mounted = 0;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001365 unsigned long flags = Mount_Flags;
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001366
Dees_Troy5bf43922012-09-07 16:07:55 -04001367 if (Is_Mounted()) {
1368 return true;
1369 } else if (!Can_Be_Mounted) {
1370 return false;
1371 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001372
1373 Find_Actual_Block_Device();
1374
1375 // Check the current file system before mounting
1376 Check_FS_Type();
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001377 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/exfat-fuse")) {
Dees_Troye34c1332013-02-06 19:13:00 +00001378 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 +00001379 LOGINFO("cmd: %s\n", cmd.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001380 string result;
1381 if (TWFunc::Exec_Cmd(cmd, result) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001382 LOGINFO("exfat-fuse failed to mount with result '%s', trying vfat\n", result.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001383 Current_File_System = "vfat";
1384 } else {
1385#ifdef TW_NO_EXFAT_FUSE
1386 UnMount(false);
1387 // We'll let the kernel handle it but using exfat-fuse to detect if the file system is actually exfat
1388 // Some kernels let us mount vfat as exfat which doesn't work out too well
1389#else
1390 exfat_mounted = 1;
1391#endif
1392 }
1393 }
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001394
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001395 if (Current_File_System == "ntfs" && (TWFunc::Path_Exists("/sbin/ntfs-3g") || TWFunc::Path_Exists("/sbin/mount.ntfs"))) {
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001396 string cmd;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001397 string Ntfsmount_Binary = "";
1398
1399 if (TWFunc::Path_Exists("/sbin/ntfs-3g"))
1400 Ntfsmount_Binary = "ntfs-3g";
1401 else if (TWFunc::Path_Exists("/sbin/mount.ntfs"))
1402 Ntfsmount_Binary = "mount.ntfs";
1403
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001404 if (Mount_Read_Only)
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001405 cmd = "/sbin/" + Ntfsmount_Binary + " -o ro " + Actual_Block_Device + " " + Mount_Point;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001406 else
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001407 cmd = "/sbin/" + Ntfsmount_Binary + " " + Actual_Block_Device + " " + Mount_Point;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001408 LOGINFO("cmd: '%s'\n", cmd.c_str());
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001409
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001410 if (TWFunc::Exec_Cmd(cmd) == 0) {
1411 return true;
1412 } else {
1413 LOGINFO("ntfs-3g failed to mount, trying regular mount method.\n");
1414 }
1415 }
1416
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001417 if (Mount_Read_Only)
1418 flags |= MS_RDONLY;
1419
Dees_Troy22042032012-12-18 21:23:08 +00001420 if (Fstab_File_System == "yaffs2") {
1421 // mount an MTD partition as a YAFFS2 filesystem.
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001422 flags = MS_NOATIME | MS_NODEV | MS_NODIRATIME;
1423 if (Mount_Read_Only)
1424 flags |= MS_RDONLY;
Dees_Troy76543db2013-06-19 16:24:30 +00001425 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags, NULL) < 0) {
1426 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags | MS_RDONLY, NULL) < 0) {
1427 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001428 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy76543db2013-06-19 16:24:30 +00001429 else
1430 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
1431 return false;
1432 } else {
1433 LOGINFO("Mounted '%s' (MTD) as RO\n", Mount_Point.c_str());
1434 return true;
1435 }
1436 } else {
1437 struct stat st;
1438 string test_path = Mount_Point;
1439 if (stat(test_path.c_str(), &st) < 0) {
1440 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001441 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy76543db2013-06-19 16:24:30 +00001442 else
1443 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
1444 return false;
1445 }
1446 mode_t new_mode = st.st_mode | S_IXUSR | S_IXGRP | S_IXOTH;
1447 if (new_mode != st.st_mode) {
1448 LOGINFO("Fixing execute permissions for %s\n", Mount_Point.c_str());
1449 if (chmod(Mount_Point.c_str(), new_mode) < 0) {
1450 if (Display_Error)
1451 LOGERR("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
1452 else
1453 LOGINFO("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
1454 return false;
1455 }
1456 }
Dees_Troy22042032012-12-18 21:23:08 +00001457 return true;
Dees_Troy76543db2013-06-19 16:24:30 +00001458 }
thatc7572eb2015-03-31 18:55:30 +02001459 }
1460
1461 string mount_fs = Current_File_System;
1462 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sys/module/texfat"))
1463 mount_fs = "texfat";
1464
1465 if (!exfat_mounted &&
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001466 mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), mount_fs.c_str(), flags, Mount_Options.c_str()) != 0 &&
1467 mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), mount_fs.c_str(), flags, NULL) != 0) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001468#ifdef TW_NO_EXFAT_FUSE
1469 if (Current_File_System == "exfat") {
Dees_Troy2673cec2013-04-02 20:22:16 +00001470 LOGINFO("Mounting exfat failed, trying vfat...\n");
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001471 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), "vfat", 0, NULL) != 0) {
Dees_Troy85f44ed2013-01-09 18:42:36 +00001472 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001473 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy85f44ed2013-01-09 18:42:36 +00001474 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001475 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001476 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 +00001477 return false;
Dees_Troy85f44ed2013-01-09 18:42:36 +00001478 }
Dees_Troyb05ddee2013-01-28 20:24:50 +00001479 } else {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001480#endif
bigbiff bigbiff26774a02014-03-29 18:22:00 -04001481 if (!Removable && Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001482 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001483 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001484 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
1485 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 +00001486 return false;
1487#ifdef TW_NO_EXFAT_FUSE
Dees_Troy85f44ed2013-01-09 18:42:36 +00001488 }
1489#endif
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001490 }
Ethan Yonker253368a2014-11-25 15:00:52 -06001491
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001492 if (Removable)
1493 Update_Size(Display_Error);
1494
xiaolu9416f4f2015-06-04 08:22:23 +08001495 if (!Symlink_Mount_Point.empty() && TWFunc::Path_Exists(Symlink_Path)) {
codelover2a3d4ce2015-03-14 20:26:49 +08001496 string Command = "mount -o bind '" + Symlink_Path + "' '" + Symlink_Mount_Point + "'";
Vojtech Bocek05534202013-09-11 08:11:56 +02001497 TWFunc::Exec_Cmd(Command);
Dees_Troy5bf43922012-09-07 16:07:55 -04001498 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001499 return true;
1500}
1501
1502bool TWPartition::UnMount(bool Display_Error) {
1503 if (Is_Mounted()) {
1504 int never_unmount_system;
1505
1506 DataManager::GetValue(TW_DONT_UNMOUNT_SYSTEM, never_unmount_system);
1507 if (never_unmount_system == 1 && Mount_Point == "/system")
1508 return true; // Never unmount system if you're not supposed to unmount it
1509
Matt Mowera8e6d832017-02-14 20:20:59 -06001510 if (Is_Storage && MTP_Storage_ID > 0)
Ethan Yonker726a0202014-12-16 20:01:38 -06001511 PartitionManager.Remove_MTP_Storage(MTP_Storage_ID);
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001512
Dees_Troy38bd7602012-09-14 13:33:53 -04001513 if (!Symlink_Mount_Point.empty())
1514 umount(Symlink_Mount_Point.c_str());
1515
Dees_Troyb05ddee2013-01-28 20:24:50 +00001516 umount(Mount_Point.c_str());
1517 if (Is_Mounted()) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001518 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001519 gui_msg(Msg(msg::kError, "fail_unmount=Failed to unmount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy5bf43922012-09-07 16:07:55 -04001520 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001521 LOGINFO("Unable to unmount '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001522 return false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001523 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -04001524 return true;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001525 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001526 } else {
1527 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001528 }
1529}
1530
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05001531bool TWPartition::ReMount(bool Display_Error) {
1532 if (UnMount(Display_Error))
1533 return Mount(Display_Error);
1534 return false;
1535}
1536
1537bool TWPartition::ReMount_RW(bool Display_Error) {
1538 // No need to remount if already mounted rw
1539 if (Is_File_System_Writable())
1540 return true;
1541
1542 bool ro = Mount_Read_Only;
1543 int flags = Mount_Flags;
1544
1545 Mount_Read_Only = false;
1546 Mount_Flags &= ~MS_RDONLY;
1547
1548 bool ret = ReMount(Display_Error);
1549
1550 Mount_Read_Only = ro;
1551 Mount_Flags = flags;
1552
1553 return ret;
1554}
1555
Gary Peck43acadf2012-11-21 21:19:01 -08001556bool TWPartition::Wipe(string New_File_System) {
Ethan Yonker726a0202014-12-16 20:01:38 -06001557 bool wiped = false, update_crypt = false, recreate_media = true;
Dees_Troy16c2b312013-01-15 16:51:18 +00001558 int check;
1559 string Layout_Filename = Mount_Point + "/.layout_version";
1560
Dees_Troy38bd7602012-09-14 13:33:53 -04001561 if (!Can_Be_Wiped) {
Matt Mower3c366972015-12-25 19:28:31 -06001562 gui_msg(Msg(msg::kError, "cannot_wipe=Partition {1} cannot be wiped.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04001563 return false;
1564 }
1565
Dees_Troyc51f1f92012-09-20 15:32:13 -04001566 if (Mount_Point == "/cache")
Dees_Troy2673cec2013-04-02 20:22:16 +00001567 Log_Offset = 0;
Dees_Troyc51f1f92012-09-20 15:32:13 -04001568
Dees_Troy16c2b312013-01-15 16:51:18 +00001569 if (Retain_Layout_Version && Mount(false) && TWFunc::Path_Exists(Layout_Filename))
1570 TWFunc::copy_file(Layout_Filename, "/.layout_version", 0600);
1571 else
1572 unlink("/.layout_version");
Dees_Troy38bd7602012-09-14 13:33:53 -04001573
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001574 if (Has_Data_Media && Current_File_System == New_File_System) {
Dees_Troy16c2b312013-01-15 16:51:18 +00001575 wiped = Wipe_Data_Without_Wiping_Media();
Ethan Yonker5eac2222014-06-11 12:22:55 -05001576 recreate_media = false;
Dees_Troy16c2b312013-01-15 16:51:18 +00001577 } else {
Dees_Troy16c2b312013-01-15 16:51:18 +00001578 DataManager::GetValue(TW_RM_RF_VAR, check);
1579
Hashcodedabfd492013-08-29 22:45:30 -07001580 if (check || Use_Rm_Rf)
Dees_Troy16c2b312013-01-15 16:51:18 +00001581 wiped = Wipe_RMRF();
1582 else if (New_File_System == "ext4")
1583 wiped = Wipe_EXT4();
1584 else if (New_File_System == "ext2" || New_File_System == "ext3")
1585 wiped = Wipe_EXT23(New_File_System);
1586 else if (New_File_System == "vfat")
1587 wiped = Wipe_FAT();
1588 else if (New_File_System == "exfat")
1589 wiped = Wipe_EXFAT();
1590 else if (New_File_System == "yaffs2")
1591 wiped = Wipe_MTD();
Dees_Troye5017042013-08-29 16:38:55 +00001592 else if (New_File_System == "f2fs")
1593 wiped = Wipe_F2FS();
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001594 else if (New_File_System == "ntfs")
1595 wiped = Wipe_NTFS();
Dees_Troy16c2b312013-01-15 16:51:18 +00001596 else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001597 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 +00001598 unlink("/.layout_version");
1599 return false;
1600 }
1601 update_crypt = wiped;
Gary Pecke8bc5d72012-12-21 06:45:25 -08001602 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001603
Gary Pecke8bc5d72012-12-21 06:45:25 -08001604 if (wiped) {
Dees_Troy1c1ac442013-01-17 21:42:14 +00001605 if (Mount_Point == "/cache")
1606 DataManager::Output_Version();
1607
Dees_Troy16c2b312013-01-15 16:51:18 +00001608 if (TWFunc::Path_Exists("/.layout_version") && Mount(false))
1609 TWFunc::copy_file("/.layout_version", Layout_Filename, 0600);
1610
1611 if (update_crypt) {
1612 Setup_File_System(false);
1613 if (Is_Encrypted && !Is_Decrypted) {
1614 // just wiped an encrypted partition back to its unencrypted state
1615 Is_Encrypted = false;
1616 Is_Decrypted = false;
1617 Decrypted_Block_Device = "";
1618 if (Mount_Point == "/data") {
1619 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1620 DataManager::SetValue(TW_IS_DECRYPTED, 0);
1621 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001622 }
1623 }
Ethan Yonker5eac2222014-06-11 12:22:55 -05001624
Ethan Yonker66a19492015-12-10 10:19:45 -06001625 if (Has_Data_Media && recreate_media) {
Ethan Yonker5eac2222014-06-11 12:22:55 -05001626 Recreate_Media_Folder();
1627 }
Matt Mower209c9632016-01-20 12:08:35 -06001628 if (Is_Storage && Mount(false))
1629 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Gary Pecke8bc5d72012-12-21 06:45:25 -08001630 }
Matt Mower209c9632016-01-20 12:08:35 -06001631
Gary Pecke8bc5d72012-12-21 06:45:25 -08001632 return wiped;
Dees_Troy51a0e822012-09-05 15:24:24 -04001633}
1634
Gary Peck43acadf2012-11-21 21:19:01 -08001635bool TWPartition::Wipe() {
Gary Peck82599a82012-11-21 16:23:12 -08001636 if (Is_File_System(Current_File_System))
1637 return Wipe(Current_File_System);
1638 else
1639 return Wipe(Fstab_File_System);
Gary Peck43acadf2012-11-21 21:19:01 -08001640}
1641
Dees_Troye58d5262012-09-21 12:27:57 -04001642bool TWPartition::Wipe_AndSec(void) {
1643 if (!Has_Android_Secure)
1644 return false;
1645
Dees_Troye58d5262012-09-21 12:27:57 -04001646 if (!Mount(true))
1647 return false;
1648
Ethan Yonker74db1572015-10-28 12:44:49 -05001649 gui_msg(Msg("wiping=Wiping {1}")(Backup_Display_Name));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001650 TWFunc::removeDir(Mount_Point + "/.android_secure/", true);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001651 return true;
Dees_Troye58d5262012-09-21 12:27:57 -04001652}
1653
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001654bool TWPartition::Can_Repair() {
Ethan Yonkera2719152015-05-28 09:44:41 -05001655 if (Mount_Read_Only)
1656 return false;
Matt Mower18794c82015-11-11 16:22:45 -06001657 if (Current_File_System == "vfat" && TWFunc::Path_Exists("/sbin/fsck.fat"))
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001658 return true;
1659 else if ((Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") && TWFunc::Path_Exists("/sbin/e2fsck"))
1660 return true;
1661 else if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/fsck.exfat"))
1662 return true;
1663 else if (Current_File_System == "f2fs" && TWFunc::Path_Exists("/sbin/fsck.f2fs"))
1664 return true;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001665 else if (Current_File_System == "ntfs" && (TWFunc::Path_Exists("/sbin/ntfsfix") || TWFunc::Path_Exists("/sbin/fsck.ntfs")))
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001666 return true;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001667 return false;
1668}
1669
1670bool TWPartition::Repair() {
1671 string command;
1672
1673 if (Current_File_System == "vfat") {
Matt Mower18794c82015-11-11 16:22:45 -06001674 if (!TWFunc::Path_Exists("/sbin/fsck.fat")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001675 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.fat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001676 return false;
1677 }
1678 if (!UnMount(true))
1679 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001680 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.fat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001681 Find_Actual_Block_Device();
Matt Mower18794c82015-11-11 16:22:45 -06001682 command = "/sbin/fsck.fat -y " + Actual_Block_Device;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001683 LOGINFO("Repair command: %s\n", command.c_str());
1684 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001685 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001686 return true;
1687 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001688 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001689 return false;
1690 }
1691 }
1692 if (Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") {
1693 if (!TWFunc::Path_Exists("/sbin/e2fsck")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001694 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("e2fsck"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001695 return false;
1696 }
1697 if (!UnMount(true))
1698 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001699 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("e2fsck"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001700 Find_Actual_Block_Device();
Ethan Yonkera2719152015-05-28 09:44:41 -05001701 command = "/sbin/e2fsck -fp " + Actual_Block_Device;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001702 LOGINFO("Repair command: %s\n", command.c_str());
1703 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001704 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001705 return true;
1706 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001707 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001708 return false;
1709 }
1710 }
1711 if (Current_File_System == "exfat") {
1712 if (!TWFunc::Path_Exists("/sbin/fsck.exfat")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001713 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.exfat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001714 return false;
1715 }
1716 if (!UnMount(true))
1717 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001718 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.exfat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001719 Find_Actual_Block_Device();
1720 command = "/sbin/fsck.exfat " + Actual_Block_Device;
1721 LOGINFO("Repair command: %s\n", command.c_str());
1722 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001723 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001724 return true;
1725 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001726 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001727 return false;
1728 }
1729 }
1730 if (Current_File_System == "f2fs") {
1731 if (!TWFunc::Path_Exists("/sbin/fsck.f2fs")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001732 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.f2fs"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001733 return false;
1734 }
1735 if (!UnMount(true))
1736 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001737 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.f2fs"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001738 Find_Actual_Block_Device();
1739 command = "/sbin/fsck.f2fs " + Actual_Block_Device;
1740 LOGINFO("Repair command: %s\n", command.c_str());
1741 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001742 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001743 return true;
1744 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001745 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001746 return false;
1747 }
1748 }
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001749 if (Current_File_System == "ntfs") {
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001750 string Ntfsfix_Binary;
1751 if (TWFunc::Path_Exists("/sbin/ntfsfix"))
1752 Ntfsfix_Binary = "ntfsfix";
1753 else if (TWFunc::Path_Exists("/sbin/fsck.ntfs"))
1754 Ntfsfix_Binary = "fsck.ntfs";
1755 else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001756 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("ntfsfix"));
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001757 return false;
1758 }
1759 if (!UnMount(true))
1760 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001761 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)(Ntfsfix_Binary));
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001762 Find_Actual_Block_Device();
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001763 command = "/sbin/" + Ntfsfix_Binary + " " + Actual_Block_Device;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001764 LOGINFO("Repair command: %s\n", command.c_str());
1765 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001766 gui_msg("done=Done.");
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001767 return true;
1768 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001769 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001770 return false;
1771 }
1772 }
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001773 return false;
1774}
1775
Ethan Yonkera2719152015-05-28 09:44:41 -05001776bool TWPartition::Can_Resize() {
1777 if (Mount_Read_Only)
1778 return false;
1779 if ((Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") && TWFunc::Path_Exists("/sbin/resize2fs"))
1780 return true;
1781 return false;
1782}
1783
1784bool TWPartition::Resize() {
1785 string command;
1786
1787 if (Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") {
1788 if (!Can_Repair()) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001789 LOGINFO("Cannot resize %s because %s cannot be repaired before resizing.\n", Display_Name.c_str(), Display_Name.c_str());
1790 gui_msg(Msg(msg::kError, "cannot_resize=Cannot resize {1}.")(Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001791 return false;
1792 }
1793 if (!TWFunc::Path_Exists("/sbin/resize2fs")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001794 LOGINFO("resize2fs does not exist! Cannot resize!\n");
1795 gui_msg(Msg(msg::kError, "cannot_resize=Cannot resize {1}.")(Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001796 return false;
1797 }
1798 // Repair will unmount so no need to do it twice
Ethan Yonker74db1572015-10-28 12:44:49 -05001799 gui_msg(Msg("repair_resize=Repairing {1} before resizing.")( Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001800 if (!Repair())
1801 return false;
Ethan Yonker74db1572015-10-28 12:44:49 -05001802 gui_msg(Msg("resizing=Resizing {1} using {2}...")(Display_Name)("resize2fs"));
Ethan Yonkera2719152015-05-28 09:44:41 -05001803 Find_Actual_Block_Device();
1804 command = "/sbin/resize2fs " + Actual_Block_Device;
1805 if (Length != 0) {
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001806 unsigned long long Actual_Size = IOCTL_Get_Block_Size();
1807 if (Actual_Size == 0)
1808 return false;
Ethan Yonkera2719152015-05-28 09:44:41 -05001809
Ethan Yonkera2719152015-05-28 09:44:41 -05001810 unsigned long long Block_Count;
1811 if (Length < 0) {
1812 // Reduce overall size by this length
1813 Block_Count = (Actual_Size / 1024LLU) - ((unsigned long long)(Length * -1) / 1024LLU);
1814 } else {
1815 // This is the size, not a size reduction
1816 Block_Count = ((unsigned long long)(Length) / 1024LLU);
1817 }
1818 char temp[256];
1819 sprintf(temp, "%llu", Block_Count);
1820 command += " ";
1821 command += temp;
1822 command += "K";
1823 }
1824 LOGINFO("Resize command: %s\n", command.c_str());
1825 if (TWFunc::Exec_Cmd(command) == 0) {
1826 Update_Size(true);
Ethan Yonker74db1572015-10-28 12:44:49 -05001827 gui_msg("done=Done.");
Ethan Yonkera2719152015-05-28 09:44:41 -05001828 return true;
1829 } else {
1830 Update_Size(true);
Ethan Yonker74db1572015-10-28 12:44:49 -05001831 gui_msg(Msg(msg::kError, "unable_resize=Unable to resize {1}.")(Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001832 return false;
1833 }
1834 }
1835 return false;
1836}
1837
bigbiffce8f83c2015-12-12 18:30:21 -05001838bool TWPartition::Backup(PartitionSettings *part_settings, pid_t *tar_fork_pid) {
1839 if (Backup_Method == BM_FILES)
1840 return Backup_Tar(part_settings, tar_fork_pid);
1841 else if (Backup_Method == BM_DD)
1842 return Backup_Image(part_settings);
1843 else if (Backup_Method == BM_FLASH_UTILS)
1844 return Backup_Dump_Image(part_settings);
Dees_Troy2673cec2013-04-02 20:22:16 +00001845 LOGERR("Unknown backup method for '%s'\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001846 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001847}
1848
bigbiffce8f83c2015-12-12 18:30:21 -05001849bool TWPartition::Restore(PartitionSettings *part_settings) {
Matt Mower3c366972015-12-25 19:28:31 -06001850 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, gui_parse_text("{@restoring_hdr}"));
Ethan Yonkere080c1f2016-09-19 13:50:25 -05001851 LOGINFO("Restore filename is: %s/%s\n", part_settings->Backup_Folder.c_str(), Backup_FileName.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001852
bigbiffce8f83c2015-12-12 18:30:21 -05001853 string Restore_File_System = Get_Restore_File_System(part_settings);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001854
1855 if (Is_File_System(Restore_File_System))
bigbiffce8f83c2015-12-12 18:30:21 -05001856 return Restore_Tar(part_settings);
1857 else if (Is_Image(Restore_File_System))
1858 return Restore_Image(part_settings);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001859
1860 LOGERR("Unknown restore method for '%s'\n", Mount_Point.c_str());
1861 return false;
1862}
1863
bigbiffce8f83c2015-12-12 18:30:21 -05001864string TWPartition::Get_Restore_File_System(PartitionSettings *part_settings) {
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001865 size_t first_period, second_period;
1866 string Restore_File_System;
1867
Gary Peck43acadf2012-11-21 21:19:01 -08001868 // Parse backup filename to extract the file system before wiping
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05001869 first_period = Backup_FileName.find(".");
Gary Peck43acadf2012-11-21 21:19:01 -08001870 if (first_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001871 LOGERR("Unable to find file system (first period).\n");
thatd43bf2d2014-09-21 23:13:02 +02001872 return string();
Gary Peck43acadf2012-11-21 21:19:01 -08001873 }
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05001874 Restore_File_System = Backup_FileName.substr(first_period + 1, Backup_FileName.size() - first_period - 1);
Gary Peck43acadf2012-11-21 21:19:01 -08001875 second_period = Restore_File_System.find(".");
1876 if (second_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001877 LOGERR("Unable to find file system (second period).\n");
thatd43bf2d2014-09-21 23:13:02 +02001878 return string();
Gary Peck43acadf2012-11-21 21:19:01 -08001879 }
1880 Restore_File_System.resize(second_period);
Matt Mowera0cd91d2016-12-30 18:21:42 -06001881 LOGINFO("Restore file system is: '%s'.\n", Restore_File_System.c_str());
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001882 return Restore_File_System;
Dees_Troy51a0e822012-09-05 15:24:24 -04001883}
1884
1885string TWPartition::Backup_Method_By_Name() {
bigbiffce8f83c2015-12-12 18:30:21 -05001886 if (Backup_Method == BM_NONE)
Dees_Troy38bd7602012-09-14 13:33:53 -04001887 return "none";
bigbiffce8f83c2015-12-12 18:30:21 -05001888 else if (Backup_Method == BM_FILES)
Dees_Troy38bd7602012-09-14 13:33:53 -04001889 return "files";
bigbiffce8f83c2015-12-12 18:30:21 -05001890 else if (Backup_Method == BM_DD)
Dees_Troy38bd7602012-09-14 13:33:53 -04001891 return "dd";
bigbiffce8f83c2015-12-12 18:30:21 -05001892 else if (Backup_Method == BM_FLASH_UTILS)
Dees_Troy38bd7602012-09-14 13:33:53 -04001893 return "flash_utils";
1894 else
1895 return "undefined";
1896 return "ERROR!";
Dees_Troy51a0e822012-09-05 15:24:24 -04001897}
1898
1899bool TWPartition::Decrypt(string Password) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001900 LOGINFO("STUB TWPartition::Decrypt, password: '%s'\n", Password.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001901 // Is this needed?
Dees_Troy51a0e822012-09-05 15:24:24 -04001902 return 1;
1903}
1904
1905bool TWPartition::Wipe_Encryption() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001906 bool Save_Data_Media = Has_Data_Media;
1907
1908 if (!UnMount(true))
1909 return false;
1910
Dees_Troy38bd7602012-09-14 13:33:53 -04001911 Has_Data_Media = false;
Dees_Troy74fb2e92013-04-15 14:35:47 +00001912 Decrypted_Block_Device = "";
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001913#ifdef TW_INCLUDE_CRYPTO
Ethan Yonker1b190162016-12-05 15:25:19 -06001914 if (Is_Decrypted && !Decrypted_Block_Device.empty()) {
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001915 if (!UnMount(true))
1916 return false;
Matt Mower2b18a532015-02-20 16:58:05 -06001917 if (delete_crypto_blk_dev((char*)("userdata")) != 0) {
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001918 LOGERR("Error deleting crypto block device, continuing anyway.\n");
1919 }
1920 }
1921#endif
Dees_Troy74fb2e92013-04-15 14:35:47 +00001922 Is_Decrypted = false;
1923 Is_Encrypted = false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001924 Find_Actual_Block_Device();
Ethan Yonkerbc85b632015-08-09 12:48:14 -05001925 if (Crypto_Key_Location == "footer") {
1926 int newlen, fd;
1927 if (Length != 0) {
1928 newlen = Length;
1929 if (newlen < 0)
1930 newlen = newlen * -1;
1931 } else {
1932 newlen = CRYPT_FOOTER_OFFSET;
1933 }
1934 if ((fd = open(Actual_Block_Device.c_str(), O_RDWR)) < 0) {
1935 gui_print_color("warning", "Unable to open '%s' to wipe crypto key\n", Actual_Block_Device.c_str());
1936 } else {
1937 unsigned int block_count;
1938 if ((ioctl(fd, BLKGETSIZE, &block_count)) == -1) {
1939 gui_print_color("warning", "Unable to get block size for wiping crypto footer.\n");
1940 } else {
1941 off64_t offset = ((off64_t)block_count * 512) - newlen;
1942 if (lseek64(fd, offset, SEEK_SET) == -1) {
1943 gui_print_color("warning", "Unable to lseek64 for wiping crypto footer.\n");
1944 } else {
1945 void* buffer = malloc(newlen);
1946 if (!buffer) {
1947 gui_print_color("warning", "Failed to malloc for wiping crypto footer.\n");
1948 } else {
1949 memset(buffer, 0, newlen);
1950 int ret = write(fd, buffer, newlen);
1951 if (ret != newlen) {
1952 gui_print_color("warning", "Failed to wipe crypto footer.\n");
1953 } else {
1954 LOGINFO("Successfully wiped crypto footer.\n");
1955 }
Matt Mower13a8f0b2015-09-26 15:40:03 -05001956 free(buffer);
Ethan Yonkerbc85b632015-08-09 12:48:14 -05001957 }
1958 }
1959 }
1960 close(fd);
1961 }
1962 } else {
Ethan Yonkerc2dafbb2016-03-15 22:20:59 -05001963 if (TWFunc::IOCTL_Get_Block_Size(Crypto_Key_Location.c_str()) >= 16384LLU) {
1964 string Command = "dd of='" + Crypto_Key_Location + "' if=/dev/zero bs=16384 count=1";
1965 TWFunc::Exec_Cmd(Command);
1966 } else {
1967 LOGINFO("Crypto key location reports size < 16K so not wiping crypto footer.\n");
1968 }
Ethan Yonkerbc85b632015-08-09 12:48:14 -05001969 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001970 if (Wipe(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001971 Has_Data_Media = Save_Data_Media;
1972 if (Has_Data_Media && !Symlink_Mount_Point.empty()) {
1973 Recreate_Media_Folder();
Ethan Yonker726a0202014-12-16 20:01:38 -06001974 if (Mount(false))
1975 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04001976 }
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001977 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
Ethan Yonker83e82572014-04-04 10:59:28 -05001978#ifndef TW_OEM_BUILD
Ethan Yonker74db1572015-10-28 12:44:49 -05001979 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 -05001980#endif
Dees_Troy38bd7602012-09-14 13:33:53 -04001981 return true;
1982 } else {
1983 Has_Data_Media = Save_Data_Media;
Ethan Yonker74db1572015-10-28 12:44:49 -05001984 gui_err("format_data_err=Unable to format to remove encryption.");
Ethan Yonker726a0202014-12-16 20:01:38 -06001985 if (Has_Data_Media && Mount(false))
1986 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04001987 return false;
1988 }
1989 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001990}
1991
1992void TWPartition::Check_FS_Type() {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001993 const char* type;
1994 blkid_probe pr;
Dees_Troy5bf43922012-09-07 16:07:55 -04001995
Dees_Troy68cab492012-12-12 19:29:35 +00001996 if (Fstab_File_System == "yaffs2" || Fstab_File_System == "mtd" || Fstab_File_System == "bml" || Ignore_Blkid)
1997 return; // Running blkid on some mtd devices causes a massive crash or needs to be skipped
Dees_Troy5bf43922012-09-07 16:07:55 -04001998
Dees_Troy38bd7602012-09-14 13:33:53 -04001999 Find_Actual_Block_Device();
Dees_Troy8170a922012-09-18 15:40:25 -04002000 if (!Is_Present)
2001 return;
Dees_Troy51127312012-09-08 13:08:49 -04002002
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05002003 pr = blkid_new_probe_from_filename(Actual_Block_Device.c_str());
2004 if (blkid_do_fullprobe(pr)) {
2005 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00002006 LOGINFO("Can't probe device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05002007 return;
Dees_Troy5bf43922012-09-07 16:07:55 -04002008 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02002009
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05002010 if (blkid_probe_lookup_value(pr, "TYPE", &type, NULL) < 0) {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05002011 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00002012 LOGINFO("can't find filesystem on device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05002013 return;
2014 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02002015
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05002016 Current_File_System = type;
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02002017 blkid_free_probe(pr);
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06002018 if (fs_flags.size() > 1) {
2019 std::vector<partition_fs_flags_struct>::iterator iter;
2020 std::vector<partition_fs_flags_struct>::iterator found = fs_flags.begin();
2021
2022 for (iter = fs_flags.begin(); iter != fs_flags.end(); iter++) {
2023 if (iter->File_System == Current_File_System) {
2024 found = iter;
2025 break;
2026 }
2027 }
2028 // If we don't find a match, we default the flags to the first set of flags that we received from the fstab
2029 if (Mount_Flags != found->Mount_Flags || Mount_Options != found->Mount_Options) {
2030 Mount_Flags = found->Mount_Flags;
2031 Mount_Options = found->Mount_Options;
2032 LOGINFO("Mount_Flags: %i, Mount_Options: %s\n", Mount_Flags, Mount_Options.c_str());
2033 }
2034 }
Dees_Troy51a0e822012-09-05 15:24:24 -04002035}
2036
Gary Peck43acadf2012-11-21 21:19:01 -08002037bool TWPartition::Wipe_EXT23(string File_System) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002038 if (!UnMount(true))
2039 return false;
2040
Dees_Troy43d8b002012-09-17 16:00:01 -04002041 if (TWFunc::Path_Exists("/sbin/mke2fs")) {
Vojtech Bocek05534202013-09-11 08:11:56 +02002042 string command;
Dees_Troy38bd7602012-09-14 13:33:53 -04002043
Greg Wallace4b44fef2015-12-29 15:33:24 -05002044 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mke2fs"));
Dees_Troy38bd7602012-09-14 13:33:53 -04002045 Find_Actual_Block_Device();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002046 command = "mke2fs -t " + File_System + " -m 0 " + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00002047 LOGINFO("mke2fs command: %s\n", command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02002048 if (TWFunc::Exec_Cmd(command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08002049 Current_File_System = File_System;
Dees_Troye58d5262012-09-21 12:27:57 -04002050 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05002051 gui_msg("done=Done.");
Dees_Troy38bd7602012-09-14 13:33:53 -04002052 return true;
2053 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002054 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04002055 return false;
2056 }
2057 } else
2058 return Wipe_RMRF();
2059
2060 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04002061}
2062
2063bool TWPartition::Wipe_EXT4() {
Ethan Yonker25f20c12014-10-14 09:04:43 -05002064 Find_Actual_Block_Device();
2065 if (!Is_Present) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002066 LOGINFO("Block device not present, cannot wipe %s.\n", Display_Name.c_str());
2067 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Ethan Yonker25f20c12014-10-14 09:04:43 -05002068 return false;
2069 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002070 if (!UnMount(true))
2071 return false;
2072
Matt Mower87413642017-01-17 21:14:46 -06002073#if defined(USE_EXT4)
Ethan Yonkerf27497f2014-02-09 11:48:33 -06002074 int ret;
2075 char *secontext = NULL;
2076
Greg Wallace4b44fef2015-12-29 15:33:24 -05002077 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("make_ext4fs"));
Ethan Yonkerf27497f2014-02-09 11:48:33 -06002078
Dees Troy99c8dbf2014-03-10 16:53:58 +00002079 if (!selinux_handle || selabel_lookup(selinux_handle, &secontext, Mount_Point.c_str(), S_IFDIR) < 0) {
Ethan Yonkerf27497f2014-02-09 11:48:33 -06002080 LOGINFO("Cannot lookup security context for '%s'\n", Mount_Point.c_str());
2081 ret = make_ext4fs(Actual_Block_Device.c_str(), Length, Mount_Point.c_str(), NULL);
2082 } else {
2083 ret = make_ext4fs(Actual_Block_Device.c_str(), Length, Mount_Point.c_str(), selinux_handle);
2084 }
2085 if (ret != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002086 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troya95f55c2013-08-17 13:14:43 +00002087 return false;
2088 } else {
bigbiff bigbiffc49d7062013-10-11 20:28:00 -04002089 string sedir = Mount_Point + "/lost+found";
2090 PartitionManager.Mount_By_Path(sedir.c_str(), true);
2091 rmdir(sedir.c_str());
2092 mkdir(sedir.c_str(), S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP);
Dees_Troya95f55c2013-08-17 13:14:43 +00002093 return true;
2094 }
2095#else
Dees_Troy43d8b002012-09-17 16:00:01 -04002096 if (TWFunc::Path_Exists("/sbin/make_ext4fs")) {
Vojtech Bocek05534202013-09-11 08:11:56 +02002097 string Command;
Dees_Troy38bd7602012-09-14 13:33:53 -04002098
Greg Wallace4b44fef2015-12-29 15:33:24 -05002099 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("make_ext4fs"));
Dees_Troy38bd7602012-09-14 13:33:53 -04002100 Find_Actual_Block_Device();
2101 Command = "make_ext4fs";
2102 if (!Is_Decrypted && Length != 0) {
2103 // Only use length if we're not decrypted
2104 char len[32];
2105 sprintf(len, "%i", Length);
2106 Command += " -l ";
2107 Command += len;
2108 }
Dees_Troy5295d582013-09-06 15:51:08 +00002109 if (TWFunc::Path_Exists("/file_contexts")) {
2110 Command += " -S /file_contexts";
2111 }
2112 Command += " -a " + Mount_Point + " " + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00002113 LOGINFO("make_ext4fs command: %s\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02002114 if (TWFunc::Exec_Cmd(Command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08002115 Current_File_System = "ext4";
Dees_Troye58d5262012-09-21 12:27:57 -04002116 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05002117 gui_msg("done=Done.");
Dees_Troy38bd7602012-09-14 13:33:53 -04002118 return true;
2119 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002120 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04002121 return false;
2122 }
2123 } else
Gary Peck43acadf2012-11-21 21:19:01 -08002124 return Wipe_EXT23("ext4");
Dees_Troya95f55c2013-08-17 13:14:43 +00002125#endif
Dees_Troy38bd7602012-09-14 13:33:53 -04002126 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04002127}
2128
2129bool TWPartition::Wipe_FAT() {
Vojtech Bocek05534202013-09-11 08:11:56 +02002130 string command;
Dees_Troy38bd7602012-09-14 13:33:53 -04002131
Matt Mower18794c82015-11-11 16:22:45 -06002132 if (TWFunc::Path_Exists("/sbin/mkfs.fat")) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002133 if (!UnMount(true))
2134 return false;
2135
Greg Wallace4b44fef2015-12-29 15:33:24 -05002136 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mkfs.fat"));
Dees_Troy38bd7602012-09-14 13:33:53 -04002137 Find_Actual_Block_Device();
Matt Mower18794c82015-11-11 16:22:45 -06002138 command = "mkfs.fat " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02002139 if (TWFunc::Exec_Cmd(command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08002140 Current_File_System = "vfat";
Dees_Troye58d5262012-09-21 12:27:57 -04002141 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05002142 gui_msg("done=Done.");
Dees_Troy38bd7602012-09-14 13:33:53 -04002143 return true;
2144 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002145 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04002146 return false;
2147 }
2148 return true;
2149 }
2150 else
2151 return Wipe_RMRF();
2152
2153 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04002154}
2155
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002156bool TWPartition::Wipe_EXFAT() {
Vojtech Bocek05534202013-09-11 08:11:56 +02002157 string command;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002158
2159 if (TWFunc::Path_Exists("/sbin/mkexfatfs")) {
2160 if (!UnMount(true))
2161 return false;
2162
Greg Wallace4b44fef2015-12-29 15:33:24 -05002163 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mkexfatfs"));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002164 Find_Actual_Block_Device();
2165 command = "mkexfatfs " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02002166 if (TWFunc::Exec_Cmd(command) == 0) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002167 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05002168 gui_msg("done=Done.");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002169 return true;
2170 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002171 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002172 return false;
2173 }
2174 return true;
2175 }
2176 return false;
2177}
2178
Dees_Troy38bd7602012-09-14 13:33:53 -04002179bool TWPartition::Wipe_MTD() {
2180 if (!UnMount(true))
2181 return false;
2182
Greg Wallace4b44fef2015-12-29 15:33:24 -05002183 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("MTD"));
Dees_Troy38bd7602012-09-14 13:33:53 -04002184
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02002185 mtd_scan_partitions();
2186 const MtdPartition* mtd = mtd_find_partition_by_name(MTD_Name.c_str());
2187 if (mtd == NULL) {
2188 LOGERR("No mtd partition named '%s'", MTD_Name.c_str());
2189 return false;
2190 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002191
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02002192 MtdWriteContext* ctx = mtd_write_partition(mtd);
2193 if (ctx == NULL) {
2194 LOGERR("Can't write '%s', failed to format.", MTD_Name.c_str());
2195 return false;
2196 }
2197 if (mtd_erase_blocks(ctx, -1) == -1) {
2198 mtd_write_close(ctx);
2199 LOGERR("Failed to format '%s'", MTD_Name.c_str());
2200 return false;
2201 }
2202 if (mtd_write_close(ctx) != 0) {
2203 LOGERR("Failed to close '%s'", MTD_Name.c_str());
2204 return false;
2205 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08002206 Current_File_System = "yaffs2";
Dees_Troye58d5262012-09-21 12:27:57 -04002207 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05002208 gui_msg("done=Done.");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02002209 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002210}
2211
2212bool TWPartition::Wipe_RMRF() {
Dees_Troy38bd7602012-09-14 13:33:53 -04002213 if (!Mount(true))
2214 return false;
Ethan Yonker726a0202014-12-16 20:01:38 -06002215 // This is the only wipe that leaves the partition mounted, so we
2216 // must manually remove the partition from MTP if it is a storage
2217 // partition.
2218 if (Is_Storage)
2219 PartitionManager.Remove_MTP_Storage(MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04002220
Ethan Yonker74db1572015-10-28 12:44:49 -05002221 gui_msg(Msg("remove_all=Removing all files under '{1}'")(Mount_Point));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002222 TWFunc::removeDir(Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04002223 Recreate_AndSec_Folder();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002224 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002225}
2226
Dees_Troye5017042013-08-29 16:38:55 +00002227bool TWPartition::Wipe_F2FS() {
Vojtech Bocek05534202013-09-11 08:11:56 +02002228 string command;
Dees_Troye5017042013-08-29 16:38:55 +00002229
2230 if (TWFunc::Path_Exists("/sbin/mkfs.f2fs")) {
2231 if (!UnMount(true))
2232 return false;
2233
Greg Wallace4b44fef2015-12-29 15:33:24 -05002234 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mkfs.f2fs"));
Dees_Troye5017042013-08-29 16:38:55 +00002235 Find_Actual_Block_Device();
Greg Wallace558aef72016-01-19 21:14:30 -05002236 command = "mkfs.f2fs -t 0";
dhacker29a3fa75f2015-01-17 19:42:33 -05002237 if (!Is_Decrypted && Length != 0) {
2238 // Only use length if we're not decrypted
2239 char len[32];
2240 int mod_length = Length;
2241 if (Length < 0)
2242 mod_length *= -1;
2243 sprintf(len, "%i", mod_length);
2244 command += " -r ";
2245 command += len;
2246 }
2247 command += " " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02002248 if (TWFunc::Exec_Cmd(command) == 0) {
Dees_Troye5017042013-08-29 16:38:55 +00002249 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05002250 gui_msg("done=Done.");
Dees_Troye5017042013-08-29 16:38:55 +00002251 return true;
2252 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002253 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troye5017042013-08-29 16:38:55 +00002254 return false;
2255 }
2256 return true;
2257 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002258 LOGINFO("mkfs.f2fs binary not found, using rm -rf to wipe.\n");
Dees_Troye5017042013-08-29 16:38:55 +00002259 return Wipe_RMRF();
2260 }
2261 return false;
2262}
2263
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002264bool TWPartition::Wipe_NTFS() {
2265 string command;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002266 string Ntfsmake_Binary;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002267
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002268 if (TWFunc::Path_Exists("/sbin/mkntfs"))
2269 Ntfsmake_Binary = "mkntfs";
2270 else if (TWFunc::Path_Exists("/sbin/mkfs.ntfs"))
2271 Ntfsmake_Binary = "mkfs.ntfs";
2272 else
2273 return false;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002274
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002275 if (!UnMount(true))
2276 return false;
2277
Greg Wallace4b44fef2015-12-29 15:33:24 -05002278 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)(Ntfsmake_Binary));
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002279 Find_Actual_Block_Device();
2280 command = "/sbin/" + Ntfsmake_Binary + " " + Actual_Block_Device;
2281 if (TWFunc::Exec_Cmd(command) == 0) {
2282 Recreate_AndSec_Folder();
2283 gui_msg("done=Done.");
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002284 return true;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05002285 } else {
2286 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
2287 return false;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05002288 }
2289 return false;
2290}
2291
Dees_Troy51a0e822012-09-05 15:24:24 -04002292bool TWPartition::Wipe_Data_Without_Wiping_Media() {
Ethan Yonker83e82572014-04-04 10:59:28 -05002293#ifdef TW_OEM_BUILD
2294 // In an OEM Build we want to do a full format
2295 return Wipe_Encryption();
2296#else
Ethan Yonker66a19492015-12-10 10:19:45 -06002297 bool ret = false;
Dees_Troy38bd7602012-09-14 13:33:53 -04002298
Dees_Troy38bd7602012-09-14 13:33:53 -04002299 if (!Mount(true))
2300 return false;
2301
Ethan Yonker74db1572015-10-28 12:44:49 -05002302 gui_msg("wiping_data=Wiping data without wiping /data/media ...");
Ethan Yonker66a19492015-12-10 10:19:45 -06002303 ret = Wipe_Data_Without_Wiping_Media_Func(Mount_Point + "/");
2304 if (ret)
2305 gui_msg("done=Done.");
2306 return ret;
2307#endif // ifdef TW_OEM_BUILD
2308}
2309
2310bool TWPartition::Wipe_Data_Without_Wiping_Media_Func(const string& parent __unused) {
2311 string dir;
Dees_Troy38bd7602012-09-14 13:33:53 -04002312
2313 DIR* d;
Ethan Yonker66a19492015-12-10 10:19:45 -06002314 d = opendir(parent.c_str());
Dees_Troy16b74352012-11-14 22:27:31 +00002315 if (d != NULL) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002316 struct dirent* de;
2317 while ((de = readdir(d)) != NULL) {
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002318 if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) continue;
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05002319
Ethan Yonker66a19492015-12-10 10:19:45 -06002320 dir = parent;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002321 dir.append(de->d_name);
Ethan Yonker3fdcda42016-11-30 12:29:37 -06002322 if (wipe_exclusions.check_skip_dirs(dir)) {
Ethan Yonker66a19492015-12-10 10:19:45 -06002323 LOGINFO("skipped '%s'\n", dir.c_str());
2324 continue;
2325 }
Dees_Troyce675462013-01-09 19:48:21 +00002326 if (de->d_type == DT_DIR) {
Ethan Yonker66a19492015-12-10 10:19:45 -06002327 dir.append("/");
2328 if (!Wipe_Data_Without_Wiping_Media_Func(dir)) {
2329 closedir(d);
2330 return false;
2331 }
2332 rmdir(dir.c_str());
bigbiff bigbiff98f1f902013-01-19 18:46:13 -05002333 } 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 +03002334 if (unlink(dir.c_str()) != 0)
Ethan Yonker79f88bd2016-12-09 14:52:12 -06002335 LOGINFO("Unable to unlink '%s': %s\n", dir.c_str(), strerror(errno));
Dees_Troyce675462013-01-09 19:48:21 +00002336 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002337 }
2338 closedir(d);
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05002339
Dees_Troy16b74352012-11-14 22:27:31 +00002340 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -04002341 }
Ethan Yonker74db1572015-10-28 12:44:49 -05002342 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy16b74352012-11-14 22:27:31 +00002343 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04002344}
2345
bigbiffce8f83c2015-12-12 18:30:21 -05002346bool TWPartition::Backup_Tar(PartitionSettings *part_settings, pid_t *tar_fork_pid) {
Ethan Yonker472f5062016-02-25 13:47:30 -06002347 string Full_FileName;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002348 twrpTar tar;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05002349
Dees_Troy43d8b002012-09-17 16:00:01 -04002350 if (!Mount(true))
2351 return false;
2352
Dees_Troya13d74f2013-03-24 08:54:55 -05002353 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Backup_Display_Name, "Backing Up");
Ethan Yonker74db1572015-10-28 12:44:49 -05002354 gui_msg(Msg("backing_up=Backing up {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002355
Ethan Yonker472f5062016-02-25 13:47:30 -06002356 DataManager::GetValue(TW_USE_COMPRESSION_VAR, tar.use_compression);
Matt Mowerbb81e5d2014-03-20 18:05:41 -05002357
Dees_Troy83bd4832013-05-04 12:39:56 +00002358#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
Ethan Yonker472f5062016-02-25 13:47:30 -06002359 if (Can_Encrypt_Backup) {
2360 DataManager::GetValue("tw_encrypt_backup", tar.use_encryption);
2361 if (tar.use_encryption) {
2362 if (Use_Userdata_Encryption)
2363 tar.userdata_encryption = tar.use_encryption;
2364 string Password;
2365 DataManager::GetValue("tw_backup_password", Password);
2366 tar.setpassword(Password);
2367 } else {
2368 tar.use_encryption = 0;
2369 }
Dees_Troy83bd4832013-05-04 12:39:56 +00002370 }
2371#endif
Dees_Troy43d8b002012-09-17 16:00:01 -04002372
Ethan Yonker472f5062016-02-25 13:47:30 -06002373 Backup_FileName = Backup_Name + "." + Current_File_System + ".win";
Ethan Yonkere080c1f2016-09-19 13:50:25 -05002374 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
Ethan Yonker01f4e032017-02-03 15:30:52 -06002375 if (Has_Data_Media)
2376 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 -05002377 tar.part_settings = part_settings;
Ethan Yonker3fdcda42016-11-30 12:29:37 -06002378 tar.backup_exclusions = &backup_exclusions;
Dees Troye0a433a2013-12-02 04:10:37 +00002379 tar.setdir(Backup_Path);
2380 tar.setfn(Full_FileName);
2381 tar.setsize(Backup_Size);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002382 tar.partition_name = Backup_Name;
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002383 tar.backup_folder = part_settings->Backup_Folder;
bigbiffce8f83c2015-12-12 18:30:21 -05002384 if (tar.createTarFork(tar_fork_pid) != 0)
Dees Troye0a433a2013-12-02 04:10:37 +00002385 return false;
Dees_Troy43d8b002012-09-17 16:00:01 -04002386 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002387}
2388
bigbiffce8f83c2015-12-12 18:30:21 -05002389bool TWPartition::Backup_Image(PartitionSettings *part_settings) {
2390 string Full_FileName, adb_file_name;
igoriok87e3d932013-01-31 21:03:53 +02002391
Ethan Yonker74db1572015-10-28 12:44:49 -05002392 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, gui_parse_text("{@backing}"));
2393 gui_msg(Msg("backing_up=Backing up {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002394
Ethan Yonker472f5062016-02-25 13:47:30 -06002395 Backup_FileName = Backup_Name + "." + Current_File_System + ".win";
Dees_Troy43d8b002012-09-17 16:00:01 -04002396
bigbiffce8f83c2015-12-12 18:30:21 -05002397 if (part_settings->adbbackup) {
2398 Full_FileName = TW_ADB_BACKUP;
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002399 adb_file_name = part_settings->Backup_Folder + "/" + Backup_FileName;
bigbiffce8f83c2015-12-12 18:30:21 -05002400 }
2401 else
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002402 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
bigbiffce8f83c2015-12-12 18:30:21 -05002403
2404 part_settings->total_restore_size = Backup_Size;
2405
2406 if (part_settings->adbbackup) {
2407 if (!twadbbu::Write_TWIMG(adb_file_name, Backup_Size))
2408 return false;
2409 }
2410
2411 if (!Raw_Read_Write(part_settings))
Ethan Yonker472f5062016-02-25 13:47:30 -06002412 return false;
2413
bigbiffce8f83c2015-12-12 18:30:21 -05002414 if (part_settings->adbbackup) {
2415 if (!twadbbu::Write_TWEOF())
2416 return false;
Dees_Troy7c2dec82012-09-26 09:49:14 -04002417 }
Dees_Troy43d8b002012-09-17 16:00:01 -04002418 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002419}
2420
bigbiffce8f83c2015-12-12 18:30:21 -05002421bool TWPartition::Raw_Read_Write(PartitionSettings *part_settings) {
2422 unsigned long long RW_Block_Size, Remain = Backup_Size;
2423 int src_fd = -1, dest_fd = -1;
2424 ssize_t bs;
Ethan Yonker472f5062016-02-25 13:47:30 -06002425 bool ret = false;
2426 void* buffer = NULL;
2427 unsigned long long backedup_size = 0;
bigbiffce8f83c2015-12-12 18:30:21 -05002428 string srcfn, destfn;
Ethan Yonker472f5062016-02-25 13:47:30 -06002429
bigbiffce8f83c2015-12-12 18:30:21 -05002430 if (part_settings->PM_Method == PM_BACKUP) {
2431 srcfn = Actual_Block_Device;
2432 if (part_settings->adbbackup)
2433 destfn = TW_ADB_BACKUP;
bigbiff bigbiff38b83c12017-12-28 19:58:52 -05002434 else {
Ethan Yonkere080c1f2016-09-19 13:50:25 -05002435 destfn = part_settings->Backup_Folder + "/" + Backup_FileName;
bigbiff bigbiff38b83c12017-12-28 19:58:52 -05002436 }
bigbiffce8f83c2015-12-12 18:30:21 -05002437 }
2438 else {
2439 destfn = Actual_Block_Device;
2440 if (part_settings->adbbackup) {
2441 srcfn = TW_ADB_RESTORE;
2442 } else {
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002443 srcfn = part_settings->Backup_Folder + "/" + Backup_FileName;
bigbiffce8f83c2015-12-12 18:30:21 -05002444 Remain = TWFunc::Get_File_Size(srcfn);
2445 }
2446 }
Ethan Yonker472f5062016-02-25 13:47:30 -06002447
bigbiffce8f83c2015-12-12 18:30:21 -05002448 src_fd = open(srcfn.c_str(), O_RDONLY | O_LARGEFILE);
Ethan Yonker472f5062016-02-25 13:47:30 -06002449 if (src_fd < 0) {
bigbiffce8f83c2015-12-12 18:30:21 -05002450 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(srcfn.c_str())(strerror(errno)));
Ethan Yonker472f5062016-02-25 13:47:30 -06002451 return false;
2452 }
bigbiffce8f83c2015-12-12 18:30:21 -05002453
2454 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 -06002455 if (dest_fd < 0) {
bigbiffce8f83c2015-12-12 18:30:21 -05002456 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(destfn.c_str())(strerror(errno)));
Ethan Yonker472f5062016-02-25 13:47:30 -06002457 goto exit;
2458 }
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -04002459
bigbiffce8f83c2015-12-12 18:30:21 -05002460 LOGINFO("Reading '%s', writing '%s'\n", srcfn.c_str(), destfn.c_str());
2461
2462 if (part_settings->adbbackup) {
2463 RW_Block_Size = MAX_ADB_READ;
2464 bs = MAX_ADB_READ;
2465 }
2466 else {
2467 RW_Block_Size = 1048576LLU; // 1MB
2468 bs = (ssize_t)(RW_Block_Size);
2469 }
2470
Ethan Yonker472f5062016-02-25 13:47:30 -06002471 buffer = malloc((size_t)bs);
2472 if (!buffer) {
2473 LOGINFO("Raw_Read_Write failed to malloc\n");
2474 goto exit;
2475 }
bigbiffce8f83c2015-12-12 18:30:21 -05002476
2477 if (part_settings->progress)
2478 part_settings->progress->SetPartitionSize(part_settings->total_restore_size);
2479
Ethan Yonker472f5062016-02-25 13:47:30 -06002480 while (Remain > 0) {
2481 if (Remain < RW_Block_Size)
bigbiffce8f83c2015-12-12 18:30:21 -05002482 bs = (ssize_t)(Remain);
Matt Mowera8a89d12016-12-30 18:10:37 -06002483 if (read(src_fd, buffer, bs) != bs) {
Ethan Yonker472f5062016-02-25 13:47:30 -06002484 LOGINFO("Error reading source fd (%s)\n", strerror(errno));
2485 goto exit;
2486 }
2487 if (write(dest_fd, buffer, bs) != bs) {
2488 LOGINFO("Error writing destination fd (%s)\n", strerror(errno));
2489 goto exit;
2490 }
2491 backedup_size += (unsigned long long)(bs);
Matt Mower029a82d2017-01-08 13:12:38 -06002492 Remain -= (unsigned long long)(bs);
bigbiffce8f83c2015-12-12 18:30:21 -05002493 if (part_settings->progress)
2494 part_settings->progress->UpdateSize(backedup_size);
Ethan Yonker472f5062016-02-25 13:47:30 -06002495 if (PartitionManager.Check_Backup_Cancel() != 0)
2496 goto exit;
2497 }
bigbiffce8f83c2015-12-12 18:30:21 -05002498 if (part_settings->progress)
2499 part_settings->progress->UpdateDisplayDetails(true);
Ethan Yonker472f5062016-02-25 13:47:30 -06002500 fsync(dest_fd);
nailyka083dc62016-11-11 21:41:28 +01002501
2502 if (!part_settings->adbbackup && part_settings->PM_Method == PM_BACKUP) {
2503 tw_set_default_metadata(destfn.c_str());
2504 LOGINFO("Restored default metadata for %s\n", destfn.c_str());
2505 }
2506
Ethan Yonker472f5062016-02-25 13:47:30 -06002507 ret = true;
2508exit:
2509 if (src_fd >= 0)
2510 close(src_fd);
2511 if (dest_fd >= 0)
2512 close(dest_fd);
2513 if (buffer)
2514 free(buffer);
2515 return ret;
2516}
2517
bigbiffce8f83c2015-12-12 18:30:21 -05002518bool TWPartition::Backup_Dump_Image(PartitionSettings *part_settings) {
Vojtech Bocek05534202013-09-11 08:11:56 +02002519 string Full_FileName, Command;
Dees_Troy43d8b002012-09-17 16:00:01 -04002520
Ethan Yonker74db1572015-10-28 12:44:49 -05002521 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, gui_parse_text("{@backing}"));
2522 gui_msg(Msg("backing_up=Backing up {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002523
bigbiffce8f83c2015-12-12 18:30:21 -05002524 if (part_settings->progress)
2525 part_settings->progress->SetPartitionSize(Backup_Size);
Dees_Troy43d8b002012-09-17 16:00:01 -04002526
Ethan Yonker472f5062016-02-25 13:47:30 -06002527 Backup_FileName = Backup_Name + "." + Current_File_System + ".win";
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002528 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04002529
2530 Command = "dump_image " + MTD_Name + " '" + Full_FileName + "'";
bigbiffce8f83c2015-12-12 18:30:21 -05002531
Dees_Troy2673cec2013-04-02 20:22:16 +00002532 LOGINFO("Backup command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02002533 TWFunc::Exec_Cmd(Command);
Ethan Yonker4b94cfd2014-12-11 10:00:45 -06002534 tw_set_default_metadata(Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04002535 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
2536 // 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 -06002537 gui_msg(Msg(msg::kError, "backup_size=Backup file size for '{1}' is 0 bytes.")(Full_FileName));
Dees_Troy7c2dec82012-09-26 09:49:14 -04002538 return false;
2539 }
bigbiffce8f83c2015-12-12 18:30:21 -05002540 if (part_settings->progress)
2541 part_settings->progress->UpdateSize(Backup_Size);
2542
Dees_Troy43d8b002012-09-17 16:00:01 -04002543 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002544}
2545
bigbiffce8f83c2015-12-12 18:30:21 -05002546unsigned long long TWPartition::Get_Restore_Size(PartitionSettings *part_settings) {
2547 if (!part_settings->adbbackup) {
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002548 InfoManager restore_info(part_settings->Backup_Folder + "/" + Backup_Name + ".info");
bigbiffce8f83c2015-12-12 18:30:21 -05002549 if (restore_info.LoadValues() == 0) {
2550 if (restore_info.GetValue("backup_size", Restore_Size) == 0) {
2551 LOGINFO("Read info file, restore size is %llu\n", Restore_Size);
2552 return Restore_Size;
2553 }
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002554 }
2555 }
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002556
Matt Mower029a82d2017-01-08 13:12:38 -06002557 string Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
2558 string Restore_File_System = Get_Restore_File_System(part_settings);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002559
2560 if (Is_Image(Restore_File_System)) {
2561 Restore_Size = TWFunc::Get_File_Size(Full_FileName);
2562 return Restore_Size;
2563 }
2564
2565 twrpTar tar;
2566 tar.setdir(Backup_Path);
2567 tar.setfn(Full_FileName);
bigbiffce8f83c2015-12-12 18:30:21 -05002568 tar.backup_name = Full_FileName;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002569#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
2570 string Password;
2571 DataManager::GetValue("tw_restore_password", Password);
2572 if (!Password.empty())
2573 tar.setpassword(Password);
2574#endif
2575 tar.partition_name = Backup_Name;
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002576 tar.backup_folder = part_settings->Backup_Folder;
bigbiffce8f83c2015-12-12 18:30:21 -05002577 tar.part_settings = part_settings;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002578 Restore_Size = tar.get_size();
2579 return Restore_Size;
2580}
2581
bigbiffce8f83c2015-12-12 18:30:21 -05002582bool TWPartition::Restore_Tar(PartitionSettings *part_settings) {
Ethan Yonker472f5062016-02-25 13:47:30 -06002583 string Full_FileName;
Dees Troy4159aed2014-02-28 17:24:43 +00002584 bool ret = false;
bigbiffce8f83c2015-12-12 18:30:21 -05002585 string Restore_File_System = Get_Restore_File_System(part_settings);
Dees_Troy43d8b002012-09-17 16:00:01 -04002586
Dees_Troye58d5262012-09-21 12:27:57 -04002587 if (Has_Android_Secure) {
Dees_Troye58d5262012-09-21 12:27:57 -04002588 if (!Wipe_AndSec())
2589 return false;
Gary Peck43acadf2012-11-21 21:19:01 -08002590 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002591 gui_msg(Msg("wiping=Wiping {1}")(Backup_Display_Name));
Ethan Yonker5eac2222014-06-11 12:22:55 -05002592 if (Has_Data_Media && Mount_Point == "/data" && Restore_File_System != Current_File_System) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002593 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 -05002594 if (!Wipe_Data_Without_Wiping_Media())
2595 return false;
2596 } else {
2597 if (!Wipe(Restore_File_System))
2598 return false;
2599 }
Dees_Troye58d5262012-09-21 12:27:57 -04002600 }
Matt Mower3c366972015-12-25 19:28:31 -06002601 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Backup_Display_Name, gui_parse_text("{@restoring_hdr}"));
Ethan Yonker74db1572015-10-28 12:44:49 -05002602 gui_msg(Msg("restoring=Restoring {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002603
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05002604 // Remount as read/write as needed so we can restore the backup
2605 if (!ReMount_RW(true))
Dees_Troy43d8b002012-09-17 16:00:01 -04002606 return false;
2607
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002608 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
Ethan Yonker87af5632014-02-10 11:56:35 -06002609 twrpTar tar;
bigbiffce8f83c2015-12-12 18:30:21 -05002610 tar.part_settings = part_settings;
Ethan Yonker87af5632014-02-10 11:56:35 -06002611 tar.setdir(Backup_Path);
2612 tar.setfn(Full_FileName);
2613 tar.backup_name = Backup_Name;
2614#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
2615 string Password;
2616 DataManager::GetValue("tw_restore_password", Password);
2617 if (!Password.empty())
2618 tar.setpassword(Password);
2619#endif
bigbiffce8f83c2015-12-12 18:30:21 -05002620 part_settings->progress->SetPartitionSize(Get_Restore_Size(part_settings));
2621 if (tar.extractTarFork() != 0)
Dees Troy4159aed2014-02-28 17:24:43 +00002622 ret = false;
2623 else
2624 ret = true;
2625#ifdef HAVE_CAPABILITIES
2626 // Restore capabilities to the run-as binary
2627 if (Mount_Point == "/system" && Mount(true) && TWFunc::Path_Exists("/system/bin/run-as")) {
2628 struct vfs_cap_data cap_data;
2629 uint64_t capabilities = (1 << CAP_SETUID) | (1 << CAP_SETGID);
2630
2631 memset(&cap_data, 0, sizeof(cap_data));
2632 cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE;
2633 cap_data.data[0].permitted = (uint32_t) (capabilities & 0xffffffff);
2634 cap_data.data[0].inheritable = 0;
2635 cap_data.data[1].permitted = (uint32_t) (capabilities >> 32);
2636 cap_data.data[1].inheritable = 0;
2637 if (setxattr("/system/bin/run-as", XATTR_NAME_CAPS, &cap_data, sizeof(cap_data), 0) < 0) {
2638 LOGINFO("Failed to reset capabilities of /system/bin/run-as binary.\n");
2639 } else {
2640 LOGINFO("Reset capabilities of /system/bin/run-as binary successful.\n");
2641 }
2642 }
2643#endif
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05002644 if (Mount_Read_Only || Mount_Flags & MS_RDONLY)
2645 // Remount as read only when restoration is complete
2646 ReMount(true);
2647
Dees Troy4159aed2014-02-28 17:24:43 +00002648 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04002649}
2650
bigbiffce8f83c2015-12-12 18:30:21 -05002651bool TWPartition::Restore_Image(PartitionSettings *part_settings) {
Ethan Yonker96af84a2015-01-05 14:58:36 -06002652 string Full_FileName;
bigbiffce8f83c2015-12-12 18:30:21 -05002653 string Restore_File_System = Get_Restore_File_System(part_settings);
Dees_Troy43d8b002012-09-17 16:00:01 -04002654
Matt Mower3c366972015-12-25 19:28:31 -06002655 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Backup_Display_Name, gui_parse_text("{@restoring_hdr}"));
Ethan Yonker74db1572015-10-28 12:44:49 -05002656 gui_msg(Msg("restoring=Restoring {1}...")(Backup_Display_Name));
bigbiffce8f83c2015-12-12 18:30:21 -05002657
2658 if (part_settings->adbbackup)
2659 Full_FileName = TW_ADB_RESTORE;
2660 else
Ethan Yonkere080c1f2016-09-19 13:50:25 -05002661 Full_FileName = part_settings->Backup_Folder + "/" + Backup_FileName;
Gary Peck15e623d2012-11-21 21:07:58 -08002662
Ethan Yonker96af84a2015-01-05 14:58:36 -06002663 if (Restore_File_System == "emmc") {
bigbiffce8f83c2015-12-12 18:30:21 -05002664 if (!part_settings->adbbackup)
2665 part_settings->total_restore_size = (uint64_t)(TWFunc::Get_File_Size(Full_FileName));
2666 if (!Raw_Read_Write(part_settings))
Ethan Yonker96af84a2015-01-05 14:58:36 -06002667 return false;
2668 } else if (Restore_File_System == "mtd" || Restore_File_System == "bml") {
bigbiffce8f83c2015-12-12 18:30:21 -05002669 if (!Flash_Image_FI(Full_FileName, part_settings->progress))
2670 return false;
2671 }
2672
2673 if (part_settings->adbbackup) {
2674 if (!twadbbu::Write_TWEOF())
Ethan Yonker96af84a2015-01-05 14:58:36 -06002675 return false;
Gary Peck15e623d2012-11-21 21:07:58 -08002676 }
Dees_Troy43d8b002012-09-17 16:00:01 -04002677 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002678}
Dees_Troy5bf43922012-09-07 16:07:55 -04002679
2680bool TWPartition::Update_Size(bool Display_Error) {
Dees_Troy0550cfb2012-10-13 11:56:13 -04002681 bool ret = false, Was_Already_Mounted = false;
Dees_Troy51127312012-09-08 13:08:49 -04002682
Ethan Yonker1b190162016-12-05 15:25:19 -06002683 Find_Actual_Block_Device();
2684
2685 if (!Can_Be_Mounted && !Is_Encrypted) {
2686 if (TWFunc::Path_Exists(Actual_Block_Device) && Find_Partition_Size()) {
2687 Used = Size;
2688 Backup_Size = Size;
2689 return true;
2690 }
Dees_Troy5bf43922012-09-07 16:07:55 -04002691 return false;
Ethan Yonker1b190162016-12-05 15:25:19 -06002692 }
Dees_Troy5bf43922012-09-07 16:07:55 -04002693
Dees_Troy0550cfb2012-10-13 11:56:13 -04002694 Was_Already_Mounted = Is_Mounted();
Dees_Troy38bd7602012-09-14 13:33:53 -04002695 if (Removable || Is_Encrypted) {
2696 if (!Mount(false))
2697 return true;
2698 } else if (!Mount(Display_Error))
Dees_Troy5bf43922012-09-07 16:07:55 -04002699 return false;
Dees_Troy51127312012-09-08 13:08:49 -04002700
2701 ret = Get_Size_Via_statfs(Display_Error);
Dees_Troy0550cfb2012-10-13 11:56:13 -04002702 if (!ret || Size == 0) {
2703 if (!Get_Size_Via_df(Display_Error)) {
2704 if (!Was_Already_Mounted)
2705 UnMount(false);
Dees_Troy51127312012-09-08 13:08:49 -04002706 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04002707 }
2708 }
Dees_Troy51127312012-09-08 13:08:49 -04002709
Dees_Troy5bf43922012-09-07 16:07:55 -04002710 if (Has_Data_Media) {
2711 if (Mount(Display_Error)) {
Ethan Yonker3fdcda42016-11-30 12:29:37 -06002712 Used = backup_exclusions.Get_Folder_Size(Mount_Point);
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002713 Backup_Size = Used;
2714 int bak = (int)(Used / 1048576LLU);
Dees_Troy51127312012-09-08 13:08:49 -04002715 int fre = (int)(Free / 1048576LLU);
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002716 LOGINFO("Data backup size is %iMB, free: %iMB.\n", bak, fre);
Dees_Troy0550cfb2012-10-13 11:56:13 -04002717 } else {
2718 if (!Was_Already_Mounted)
2719 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04002720 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04002721 }
Dees_Troye58d5262012-09-21 12:27:57 -04002722 } else if (Has_Android_Secure) {
2723 if (Mount(Display_Error))
Ethan Yonker3fdcda42016-11-30 12:29:37 -06002724 Backup_Size = backup_exclusions.Get_Folder_Size(Backup_Path);
Dees_Troy0550cfb2012-10-13 11:56:13 -04002725 else {
2726 if (!Was_Already_Mounted)
2727 UnMount(false);
Dees_Troye58d5262012-09-21 12:27:57 -04002728 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04002729 }
Dees_Troy5bf43922012-09-07 16:07:55 -04002730 }
Dees_Troy0550cfb2012-10-13 11:56:13 -04002731 if (!Was_Already_Mounted)
2732 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04002733 return true;
Dees_Troy51127312012-09-08 13:08:49 -04002734}
Dees_Troy38bd7602012-09-14 13:33:53 -04002735
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06002736bool TWPartition::Find_Wildcard_Block_Devices(const string& Device) {
2737 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
2738 string Path = TWFunc::Get_Path(Device);
2739 string Dev = TWFunc::Get_Filename(Device);
2740 size_t wildcard_index = Dev.find("*");
2741 if (wildcard_index != string::npos)
2742 Dev = Dev.substr(0, wildcard_index);
2743 wildcard_index = Dev.size();
2744 DIR* d = opendir(Path.c_str());
2745 if (d == NULL) {
2746 LOGINFO("Error opening '%s': %s\n", Path.c_str(), strerror(errno));
2747 return false;
2748 }
2749 struct dirent* de;
2750 while ((de = readdir(d)) != NULL) {
2751 if (de->d_type != DT_BLK || strlen(de->d_name) <= wildcard_index || strncmp(de->d_name, Dev.c_str(), wildcard_index) != 0)
2752 continue;
2753
2754 string item = Path + "/";
2755 item.append(de->d_name);
2756 if (PartitionManager.Find_Partition_By_Block_Device(item))
2757 continue;
2758 TWPartition *part = new TWPartition;
2759 char buffer[MAX_FSTAB_LINE_LENGTH];
2760 sprintf(buffer, "%s %s-%i auto defaults defaults", item.c_str(), Mount_Point.c_str(), ++mount_point_index);
2761 part->Process_Fstab_Line(buffer, false, NULL);
2762 char display[MAX_FSTAB_LINE_LENGTH];
2763 sprintf(display, "%s %i", Storage_Name.c_str(), mount_point_index);
2764 part->Storage_Name = display;
2765 part->Display_Name = display;
2766 part->Primary_Block_Device = item;
2767 part->Wildcard_Block_Device = false;
2768 part->Is_SubPartition = true;
2769 part->SubPartition_Of = Mount_Point;
2770 part->Is_Storage = Is_Storage;
2771 part->Can_Be_Mounted = true;
2772 part->Removable = true;
2773 part->Can_Be_Wiped = Can_Be_Wiped;
2774 part->Wipe_Available_in_GUI = Wipe_Available_in_GUI;
2775 part->Find_Actual_Block_Device();
2776 part->Update_Size(false);
2777 Has_SubPartition = true;
2778 PartitionManager.Output_Partition(part);
2779 PartitionManager.Add_Partition(part);
2780 }
2781 closedir(d);
2782 return (mount_point_index > 0);
2783}
2784
Dees_Troy38bd7602012-09-14 13:33:53 -04002785void TWPartition::Find_Actual_Block_Device(void) {
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06002786 if (!Sysfs_Entry.empty() && Primary_Block_Device.empty() && Decrypted_Block_Device.empty()) {
2787 /* Sysfs_Entry.empty() indicates if this is a sysfs entry that begins with /device/
2788 * If we have a syfs entry then we are looking for this device from a uevent add.
2789 * The uevent add will set the primary block device based on the data we receive from
2790 * after checking for adopted storage. If the device ends up being adopted, then the
2791 * decrypted block device will be set instead of the primary block device. */
2792 Is_Present = false;
2793 return;
2794 }
2795 if (Wildcard_Block_Device && !Is_Adopted_Storage) {
2796 Is_Present = false;
2797 Actual_Block_Device = "";
2798 Can_Be_Mounted = false;
2799 if (!Find_Wildcard_Block_Devices(Primary_Block_Device)) {
2800 string Dev = Primary_Block_Device.substr(0, Primary_Block_Device.find("*"));
2801 if (TWFunc::Path_Exists(Dev)) {
2802 Is_Present = true;
2803 Can_Be_Mounted = true;
2804 Actual_Block_Device = Dev;
2805 }
2806 }
2807 return;
2808 } else if (Is_Decrypted && !Decrypted_Block_Device.empty()) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002809 Actual_Block_Device = Decrypted_Block_Device;
Ethan Yonker1b190162016-12-05 15:25:19 -06002810 if (TWFunc::Path_Exists(Decrypted_Block_Device)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002811 Is_Present = true;
Ethan Yonker1b190162016-12-05 15:25:19 -06002812 return;
2813 }
2814 } else if (SlotSelect && TWFunc::Path_Exists(Primary_Block_Device + PartitionManager.Get_Active_Slot_Suffix())) {
2815 Actual_Block_Device = Primary_Block_Device + PartitionManager.Get_Active_Slot_Suffix();
2816 unlink(Primary_Block_Device.c_str());
2817 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
2818 Is_Present = true;
2819 return;
Dees_Troy43d8b002012-09-17 16:00:01 -04002820 } else if (TWFunc::Path_Exists(Primary_Block_Device)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002821 Is_Present = true;
2822 Actual_Block_Device = Primary_Block_Device;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002823 return;
2824 }
Ethan Yonker1b190162016-12-05 15:25:19 -06002825 if (!Alternate_Block_Device.empty() && TWFunc::Path_Exists(Alternate_Block_Device)) {
Dees_Troy3f04d032012-10-07 18:20:09 -04002826 Actual_Block_Device = Alternate_Block_Device;
Dees_Troy38bd7602012-09-14 13:33:53 -04002827 Is_Present = true;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002828 } else {
Dees_Troy38bd7602012-09-14 13:33:53 -04002829 Is_Present = false;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002830 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002831}
2832
2833void TWPartition::Recreate_Media_Folder(void) {
2834 string Command;
Ethan Yonker66a19492015-12-10 10:19:45 -06002835 string Media_Path = Mount_Point + "/media";
Dees_Troy38bd7602012-09-14 13:33:53 -04002836
Ethan Yonkerbd7492d2016-12-07 13:55:01 -06002837 if (Is_FBE) {
2838 LOGINFO("Not recreating media folder on FBE\n");
2839 return;
2840 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002841 if (!Mount(true)) {
Ethan Yonker66a19492015-12-10 10:19:45 -06002842 gui_msg(Msg(msg::kError, "recreate_folder_err=Unable to recreate {1} folder.")(Media_Path));
2843 } else if (!TWFunc::Path_Exists(Media_Path)) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002844 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Ethan Yonker66a19492015-12-10 10:19:45 -06002845 LOGINFO("Recreating %s folder.\n", Media_Path.c_str());
2846 mkdir(Media_Path.c_str(), 0770);
xiaolu9416f4f2015-06-04 08:22:23 +08002847 string Internal_path = DataManager::GetStrValue("tw_internal_path");
2848 if (!Internal_path.empty()) {
2849 LOGINFO("Recreating %s folder.\n", Internal_path.c_str());
2850 mkdir(Internal_path.c_str(), 0770);
2851 }
2852#ifdef TW_INTERNAL_STORAGE_PATH
2853 mkdir(EXPAND(TW_INTERNAL_STORAGE_PATH), 0770);
2854#endif
Matt Mower87413642017-01-17 21:14:46 -06002855
thata3d31fb2014-12-21 22:27:40 +01002856 // Afterwards, we will try to set the
2857 // default metadata that we were hopefully able to get during
2858 // early boot.
Ethan Yonker66a19492015-12-10 10:19:45 -06002859 tw_set_default_metadata(Media_Path.c_str());
xiaolu9416f4f2015-06-04 08:22:23 +08002860 if (!Internal_path.empty())
2861 tw_set_default_metadata(Internal_path.c_str());
Matt Mower87413642017-01-17 21:14:46 -06002862
Ethan Yonker5eac2222014-06-11 12:22:55 -05002863 // Toggle mount to ensure that "internal sdcard" gets mounted
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002864 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Ethan Yonker5eac2222014-06-11 12:22:55 -05002865 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Dees_Troy38bd7602012-09-14 13:33:53 -04002866 }
Dees_Troy43d8b002012-09-17 16:00:01 -04002867}
Dees_Troye58d5262012-09-21 12:27:57 -04002868
2869void TWPartition::Recreate_AndSec_Folder(void) {
Dees_Troye58d5262012-09-21 12:27:57 -04002870 if (!Has_Android_Secure)
2871 return;
Dees_Troy2673cec2013-04-02 20:22:16 +00002872 LOGINFO("Creating %s: %s\n", Backup_Display_Name.c_str(), Symlink_Path.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -04002873 if (!Mount(true)) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002874 gui_msg(Msg(msg::kError, "recreate_folder_err=Unable to recreate {1} folder.")(Backup_Name));
Dees_Troye58d5262012-09-21 12:27:57 -04002875 } else if (!TWFunc::Path_Exists(Symlink_Path)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00002876 LOGINFO("Recreating %s folder.\n", Backup_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002877 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05002878 mkdir(Symlink_Path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002879 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04002880 }
2881}
bigbiff7cb4c332014-11-26 20:36:07 -05002882
2883uint64_t TWPartition::Get_Max_FileSize() {
2884 uint64_t maxFileSize = 0;
2885 const uint64_t constGB = (uint64_t) 1024 * 1024 * 1024;
2886 const uint64_t constTB = (uint64_t) constGB * 1024;
2887 const uint64_t constPB = (uint64_t) constTB * 1024;
bigbiff0c532032014-12-21 13:41:26 -05002888 if (Current_File_System == "ext4")
2889 maxFileSize = 16 * constTB; //16 TB
2890 else if (Current_File_System == "vfat")
2891 maxFileSize = 4 * constGB; //4 GB
2892 else if (Current_File_System == "ntfs")
2893 maxFileSize = 256 * constTB; //256 TB
2894 else if (Current_File_System == "exfat")
2895 maxFileSize = 16 * constPB; //16 PB
2896 else if (Current_File_System == "ext3")
2897 maxFileSize = 2 * constTB; //2 TB
2898 else if (Current_File_System == "f2fs")
2899 maxFileSize = 3.94 * constTB; //3.94 TB
bigbiff7cb4c332014-11-26 20:36:07 -05002900 else
2901 maxFileSize = 100000000L;
2902 return maxFileSize - 1;
2903}
2904
bigbiffce8f83c2015-12-12 18:30:21 -05002905bool TWPartition::Flash_Image(PartitionSettings *part_settings) {
2906 string Restore_File_System, full_filename;
Ethan Yonker96af84a2015-01-05 14:58:36 -06002907
Ethan Yonkere080c1f2016-09-19 13:50:25 -05002908 full_filename = part_settings->Backup_Folder + "/" + Backup_FileName;
Ethan Yonker96af84a2015-01-05 14:58:36 -06002909
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002910 LOGINFO("Image filename is: %s\n", Backup_FileName.c_str());
bigbiffce8f83c2015-12-12 18:30:21 -05002911
2912 if (Backup_Method == BM_FILES) {
Ethan Yonker96af84a2015-01-05 14:58:36 -06002913 LOGERR("Cannot flash images to file systems\n");
2914 return false;
2915 } else if (!Can_Flash_Img) {
2916 LOGERR("Cannot flash images to partitions %s\n", Display_Name.c_str());
2917 return false;
2918 } else {
2919 if (!Find_Partition_Size()) {
2920 LOGERR("Unable to find partition size for '%s'\n", Mount_Point.c_str());
2921 return false;
2922 }
bigbiffce8f83c2015-12-12 18:30:21 -05002923 unsigned long long image_size = TWFunc::Get_File_Size(full_filename);
Ethan Yonker96af84a2015-01-05 14:58:36 -06002924 if (image_size > Size) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002925 LOGINFO("Size (%llu bytes) of image '%s' is larger than target device '%s' (%llu bytes)\n",
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05002926 image_size, Backup_FileName.c_str(), Actual_Block_Device.c_str(), Size);
Ethan Yonker74db1572015-10-28 12:44:49 -05002927 gui_err("img_size_err=Size of image is larger than target device");
Ethan Yonker96af84a2015-01-05 14:58:36 -06002928 return false;
2929 }
bigbiffce8f83c2015-12-12 18:30:21 -05002930 if (Backup_Method == BM_DD) {
2931 if (!part_settings->adbbackup) {
2932 if (Is_Sparse_Image(full_filename)) {
2933 return Flash_Sparse_Image(full_filename);
2934 }
Ethan Yonker472f5062016-02-25 13:47:30 -06002935 }
bigbiffce8f83c2015-12-12 18:30:21 -05002936 return Raw_Read_Write(part_settings);
2937 } else if (Backup_Method == BM_FLASH_UTILS) {
2938 return Flash_Image_FI(full_filename, NULL);
Ethan Yonker472f5062016-02-25 13:47:30 -06002939 }
Ethan Yonker96af84a2015-01-05 14:58:36 -06002940 }
2941
2942 LOGERR("Unknown flash method for '%s'\n", Mount_Point.c_str());
2943 return false;
2944}
2945
Ethan Yonker472f5062016-02-25 13:47:30 -06002946bool TWPartition::Is_Sparse_Image(const string& Filename) {
HashBanged974bb2016-01-30 14:20:09 -05002947 uint32_t magic = 0;
2948 int fd = open(Filename.c_str(), O_RDONLY);
2949 if (fd < 0) {
2950 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Filename)(strerror(errno)));
2951 return false;
2952 }
bigbiffce8f83c2015-12-12 18:30:21 -05002953
HashBanged974bb2016-01-30 14:20:09 -05002954 if (read(fd, &magic, sizeof(magic)) != sizeof(magic)) {
2955 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Filename)(strerror(errno)));
2956 close(fd);
2957 return false;
2958 }
2959 close(fd);
Ethan Yonker472f5062016-02-25 13:47:30 -06002960 if (magic == SPARSE_HEADER_MAGIC)
2961 return true;
2962 return false;
2963}
2964
2965bool TWPartition::Flash_Sparse_Image(const string& Filename) {
2966 string Command;
2967
2968 gui_msg(Msg("flashing=Flashing {1}...")(Display_Name));
2969
2970 Command = "simg2img '" + Filename + "' '" + Actual_Block_Device + "'";
Ethan Yonker96af84a2015-01-05 14:58:36 -06002971 LOGINFO("Flash command: '%s'\n", Command.c_str());
2972 TWFunc::Exec_Cmd(Command);
2973 return true;
2974}
2975
Ethan Yonker472f5062016-02-25 13:47:30 -06002976bool TWPartition::Flash_Image_FI(const string& Filename, ProgressTracking *progress) {
Ethan Yonker96af84a2015-01-05 14:58:36 -06002977 string Command;
Ethan Yonker472f5062016-02-25 13:47:30 -06002978 unsigned long long file_size;
Ethan Yonker96af84a2015-01-05 14:58:36 -06002979
Ethan Yonker74db1572015-10-28 12:44:49 -05002980 gui_msg(Msg("flashing=Flashing {1}...")(Display_Name));
Ethan Yonker472f5062016-02-25 13:47:30 -06002981 if (progress) {
2982 file_size = (unsigned long long)(TWFunc::Get_File_Size(Filename));
2983 progress->SetPartitionSize(file_size);
2984 }
Ethan Yonker96af84a2015-01-05 14:58:36 -06002985 // Sometimes flash image doesn't like to flash due to the first 2KB matching, so we erase first to ensure that it flashes
2986 Command = "erase_image " + MTD_Name;
2987 LOGINFO("Erase command: '%s'\n", Command.c_str());
2988 TWFunc::Exec_Cmd(Command);
2989 Command = "flash_image " + MTD_Name + " '" + Filename + "'";
2990 LOGINFO("Flash command: '%s'\n", Command.c_str());
2991 TWFunc::Exec_Cmd(Command);
Ethan Yonker472f5062016-02-25 13:47:30 -06002992 if (progress)
2993 progress->UpdateSize(file_size);
Ethan Yonker96af84a2015-01-05 14:58:36 -06002994 return true;
2995}
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05002996
2997void TWPartition::Change_Mount_Read_Only(bool new_value) {
2998 Mount_Read_Only = new_value;
2999}
3000
bigbiffce8f83c2015-12-12 18:30:21 -05003001bool TWPartition::Is_Read_Only() {
3002 return Mount_Read_Only;
3003}
3004
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05003005int TWPartition::Check_Lifetime_Writes() {
3006 bool original_read_only = Mount_Read_Only;
3007 int ret = 1;
3008
3009 Mount_Read_Only = true;
3010 if (Mount(false)) {
3011 Find_Actual_Block_Device();
3012 string block = basename(Actual_Block_Device.c_str());
3013 string file = "/sys/fs/" + Current_File_System + "/" + block + "/lifetime_write_kbytes";
3014 string result;
3015 if (TWFunc::Path_Exists(file)) {
3016 if (TWFunc::read_file(file, result) != 0) {
3017 LOGINFO("Check_Lifetime_Writes of '%s' failed to read_file\n", file.c_str());
3018 } else {
3019 LOGINFO("Check_Lifetime_Writes result: '%s'\n", result.c_str());
3020 if (result == "0") {
3021 ret = 0;
3022 }
3023 }
3024 } else {
3025 LOGINFO("Check_Lifetime_Writes file does not exist '%s'\n", file.c_str());
3026 }
3027 UnMount(true);
3028 } else {
3029 LOGINFO("Check_Lifetime_Writes failed to mount '%s'\n", Mount_Point.c_str());
3030 }
3031 Mount_Read_Only = original_read_only;
3032 return ret;
3033}
Ethan Yonker66a19492015-12-10 10:19:45 -06003034
3035int TWPartition::Decrypt_Adopted() {
3036#ifdef TW_INCLUDE_CRYPTO
3037 int ret = 1;
3038 Is_Adopted_Storage = false;
3039 string Adopted_Key_File = "";
3040
3041 if (!Removable)
3042 return ret;
3043
3044 int fd = open(Alternate_Block_Device.c_str(), O_RDONLY);
3045 if (fd < 0) {
3046 LOGINFO("failed to open '%s'\n", Alternate_Block_Device.c_str());
3047 return ret;
3048 }
3049 char type_guid[80];
3050 char part_guid[80];
3051
3052 if (gpt_disk_get_partition_info(fd, 2, type_guid, part_guid) == 0) {
3053 LOGINFO("type: '%s'\n", type_guid);
3054 LOGINFO("part: '%s'\n", part_guid);
3055 Adopted_GUID = part_guid;
3056 LOGINFO("Adopted_GUID '%s'\n", Adopted_GUID.c_str());
3057 if (strcmp(type_guid, TWGptAndroidExpand) == 0) {
3058 LOGINFO("android_expand found\n");
3059 Adopted_Key_File = "/data/misc/vold/expand_";
3060 Adopted_Key_File += part_guid;
3061 Adopted_Key_File += ".key";
3062 if (TWFunc::Path_Exists(Adopted_Key_File)) {
3063 Is_Adopted_Storage = true;
3064 /* Until we find a use case for this, I think it is safe
3065 * to disable USB Mass Storage whenever adopted storage
3066 * is present.
3067 */
3068 LOGINFO("Detected adopted storage, disabling USB mass storage mode\n");
3069 DataManager::SetValue("tw_has_usb_storage", 0);
3070 }
3071 }
3072 }
3073
3074 if (Is_Adopted_Storage) {
3075 string Adopted_Block_Device = Alternate_Block_Device + "p2";
3076 if (!TWFunc::Path_Exists(Adopted_Block_Device)) {
3077 Adopted_Block_Device = Alternate_Block_Device + "2";
3078 if (!TWFunc::Path_Exists(Adopted_Block_Device)) {
3079 LOGINFO("Adopted block device does not exist\n");
3080 goto exit;
3081 }
3082 }
3083 LOGINFO("key file is '%s', block device '%s'\n", Adopted_Key_File.c_str(), Adopted_Block_Device.c_str());
3084 char crypto_blkdev[MAXPATHLEN];
3085 std::string thekey;
3086 int fdkey = open(Adopted_Key_File.c_str(), O_RDONLY);
3087 if (fdkey < 0) {
3088 LOGINFO("failed to open key file\n");
3089 goto exit;
3090 }
3091 char buf[512];
3092 ssize_t n;
3093 while ((n = read(fdkey, &buf[0], sizeof(buf))) > 0) {
3094 thekey.append(buf, n);
3095 }
3096 close(fdkey);
3097 unsigned char* key = (unsigned char*) thekey.data();
3098 cryptfs_revert_ext_volume(part_guid);
3099
3100 ret = cryptfs_setup_ext_volume(part_guid, Adopted_Block_Device.c_str(), key, thekey.size(), crypto_blkdev);
3101 if (ret == 0) {
3102 LOGINFO("adopted storage new block device: '%s'\n", crypto_blkdev);
3103 Decrypted_Block_Device = crypto_blkdev;
3104 Is_Decrypted = true;
3105 Is_Encrypted = true;
3106 Find_Actual_Block_Device();
Kjell Braden3126a112016-06-19 16:58:15 +00003107 if (!Mount_Storage_Retry(false)) {
Ethan Yonker66a19492015-12-10 10:19:45 -06003108 LOGERR("Failed to mount decrypted adopted storage device\n");
3109 Is_Decrypted = false;
3110 Is_Encrypted = false;
3111 cryptfs_revert_ext_volume(part_guid);
3112 ret = 1;
3113 } else {
Ethan Yonkerfcf3f242016-02-16 12:30:26 -06003114 UnMount(false);
3115 Has_Android_Secure = false;
3116 Symlink_Path = "";
3117 Symlink_Mount_Point = "";
3118 Backup_Name = Mount_Point.substr(1);
3119 Backup_Path = Mount_Point;
3120 TWPartition* sdext = PartitionManager.Find_Partition_By_Path("/sd-ext");
3121 if (sdext && sdext->Actual_Block_Device == Adopted_Block_Device) {
3122 LOGINFO("Removing /sd-ext from partition list due to adopted storage\n");
3123 PartitionManager.Remove_Partition_By_Path("/sd-ext");
3124 }
Ethan Yonker66a19492015-12-10 10:19:45 -06003125 Setup_Data_Media();
3126 Recreate_Media_Folder();
3127 Wipe_Available_in_GUI = true;
3128 Wipe_During_Factory_Reset = true;
3129 Can_Be_Backed_Up = true;
3130 Can_Encrypt_Backup = true;
3131 Use_Userdata_Encryption = true;
3132 Is_Storage = true;
3133 Storage_Name = "Adopted Storage";
3134 Is_SubPartition = true;
3135 SubPartition_Of = "/data";
3136 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
3137 DataManager::SetValue("tw_has_adopted_storage", 1);
3138 }
3139 } else {
3140 LOGERR("Failed to setup adopted storage decryption\n");
3141 }
3142 }
3143exit:
Matt Mower06543e32017-01-06 15:25:26 -06003144 close(fd);
Ethan Yonker66a19492015-12-10 10:19:45 -06003145 return ret;
3146#else
3147 LOGINFO("Decrypt_Adopted: no crypto support\n");
3148 return 1;
3149#endif
3150}
3151
3152void TWPartition::Revert_Adopted() {
3153#ifdef TW_INCLUDE_CRYPTO
3154 if (!Adopted_GUID.empty()) {
3155 PartitionManager.Remove_MTP_Storage(Mount_Point);
3156 UnMount(false);
3157 cryptfs_revert_ext_volume(Adopted_GUID.c_str());
3158 Is_Adopted_Storage = false;
3159 Is_Encrypted = false;
3160 Is_Decrypted = false;
3161 Decrypted_Block_Device = "";
3162 Find_Actual_Block_Device();
3163 Wipe_During_Factory_Reset = false;
3164 Can_Be_Backed_Up = false;
3165 Can_Encrypt_Backup = false;
3166 Use_Userdata_Encryption = false;
3167 Is_SubPartition = false;
3168 SubPartition_Of = "";
3169 Has_Data_Media = false;
3170 Storage_Path = Mount_Point;
3171 if (!Symlink_Mount_Point.empty()) {
3172 TWPartition* Dat = PartitionManager.Find_Partition_By_Path("/data");
3173 if (Dat) {
3174 Dat->UnMount(false);
3175 Dat->Symlink_Mount_Point = Symlink_Mount_Point;
3176 }
3177 Symlink_Mount_Point = "";
3178 }
3179 }
3180#else
3181 LOGINFO("Revert_Adopted: no crypto support\n");
3182#endif
3183}
bigbiff bigbiffb5ecaad2017-03-20 18:53:53 -04003184
3185void TWPartition::Set_Backup_FileName(string fname) {
3186 Backup_FileName = fname;
3187}
3188
3189string TWPartition::Get_Backup_Name() {
3190 return Backup_Name;
3191}