blob: 188a739ea5e1164e7858207666d9f8762e7a254d [file] [log] [blame]
Dees Troy3be70a82013-10-22 14:25:12 +00001/*
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002 Copyright 2013 TeamWin
Dees Troy3be70a82013-10-22 14:25:12 +00003 This file is part of TWRP/TeamWin Recovery Project.
4
5 TWRP is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 TWRP is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with TWRP. If not, see <http://www.gnu.org/licenses/>.
17*/
Dees_Troy51a0e822012-09-05 15:24:24 -040018
19#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
22#include <sys/stat.h>
23#include <sys/vfs.h>
Dees_Troy5bf43922012-09-07 16:07:55 -040024#include <sys/mount.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040025#include <unistd.h>
Dees_Troy51127312012-09-08 13:08:49 -040026#include <dirent.h>
thatf54e5392016-01-29 22:04:43 +010027#include <libgen.h>
bigbiff bigbiff9c754052013-01-09 09:09:08 -050028#include <iostream>
29#include <sstream>
Ethan Yonker66a19492015-12-10 10:19:45 -060030#include <sys/param.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040031
Dees_Troy657c3092012-09-10 20:32:10 -040032#ifdef TW_INCLUDE_CRYPTO
33 #include "cutils/properties.h"
34#endif
35
bigbiff7b4c7a62015-01-01 19:44:14 -050036#include "libblkid/include/blkid.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040037#include "variables.h"
Dees_Troy2673cec2013-04-02 20:22:16 +000038#include "twcommon.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040039#include "partitions.hpp"
Dees_Troy5bf43922012-09-07 16:07:55 -040040#include "data.hpp"
Dees_Troy43d8b002012-09-17 16:00:01 -040041#include "twrp-functions.hpp"
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -050042#include "twrpDigest.hpp"
bigbiff bigbiff9c754052013-01-09 09:09:08 -050043#include "twrpTar.hpp"
bigbiff bigbiff34684ff2013-12-01 21:03:45 -050044#include "twrpDU.hpp"
Ethan Yonker1b7a31b2014-07-03 15:09:22 -050045#include "infomanager.hpp"
Ethan Yonker4b94cfd2014-12-11 10:00:45 -060046#include "set_metadata.h"
Ethan Yonker74db1572015-10-28 12:44:49 -050047#include "gui/gui.hpp"
Dees_Troy5bf43922012-09-07 16:07:55 -040048extern "C" {
Dees_Troy38bd7602012-09-14 13:33:53 -040049 #include "mtdutils/mtdutils.h"
50 #include "mtdutils/mounts.h"
Dees_Troya95f55c2013-08-17 13:14:43 +000051#ifdef USE_EXT4
52 #include "make_ext4fs.h"
53#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 Yonkerbc85b632015-08-09 12:48:14 -050058#else
59 #define CRYPT_FOOTER_OFFSET 0x4000
Ethan Yonker71413f42014-02-26 13:36:08 -060060#endif
Dees_Troy5bf43922012-09-07 16:07:55 -040061}
bigbiff bigbiffc49d7062013-10-11 20:28:00 -040062#ifdef HAVE_SELINUX
63#include "selinux/selinux.h"
Ethan Yonkerf27497f2014-02-09 11:48:33 -060064#include <selinux/label.h>
bigbiff bigbiffc49d7062013-10-11 20:28:00 -040065#endif
Dees Troy4159aed2014-02-28 17:24:43 +000066#ifdef HAVE_CAPABILITIES
67#include <sys/capability.h>
68#include <sys/xattr.h>
69#include <linux/xattr.h>
70#endif
HashBanged974bb2016-01-30 14:20:09 -050071#include <sparse_format.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040072
bigbiff bigbiff9c754052013-01-09 09:09:08 -050073using namespace std;
74
Dees_Troya95f55c2013-08-17 13:14:43 +000075extern struct selabel_handle *selinux_handle;
Ethan Yonker6277c792014-09-15 14:54:30 -050076extern bool datamedia;
Dees_Troya95f55c2013-08-17 13:14:43 +000077
Hashcode62bd9e02013-11-19 21:59:42 -080078struct flag_list {
79 const char *name;
80 unsigned flag;
81};
82
83static struct flag_list mount_flags[] = {
84 { "noatime", MS_NOATIME },
85 { "noexec", MS_NOEXEC },
86 { "nosuid", MS_NOSUID },
87 { "nodev", MS_NODEV },
88 { "nodiratime", MS_NODIRATIME },
89 { "ro", MS_RDONLY },
90 { "rw", 0 },
91 { "remount", MS_REMOUNT },
92 { "bind", MS_BIND },
93 { "rec", MS_REC },
Dees Troyc4bc30e2014-02-03 15:04:19 +000094#ifdef MS_UNBINDABLE
Hashcode62bd9e02013-11-19 21:59:42 -080095 { "unbindable", MS_UNBINDABLE },
Dees Troyc4bc30e2014-02-03 15:04:19 +000096#endif
97#ifdef MS_PRIVATE
Hashcode62bd9e02013-11-19 21:59:42 -080098 { "private", MS_PRIVATE },
Dees Troyc4bc30e2014-02-03 15:04:19 +000099#endif
100#ifdef MS_SLAVE
Hashcode62bd9e02013-11-19 21:59:42 -0800101 { "slave", MS_SLAVE },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000102#endif
103#ifdef MS_SHARED
Hashcode62bd9e02013-11-19 21:59:42 -0800104 { "shared", MS_SHARED },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000105#endif
Hashcode62bd9e02013-11-19 21:59:42 -0800106 { "sync", MS_SYNCHRONOUS },
107 { "defaults", 0 },
108 { 0, 0 },
109};
110
that9e0593e2014-10-08 00:01:24 +0200111TWPartition::TWPartition() {
Dees_Troy51a0e822012-09-05 15:24:24 -0400112 Can_Be_Mounted = false;
113 Can_Be_Wiped = false;
Dees_Troya13d74f2013-03-24 08:54:55 -0500114 Can_Be_Backed_Up = false;
Vojtech Bocek1dc30982013-08-30 21:49:30 +0200115 Use_Rm_Rf = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400116 Wipe_During_Factory_Reset = false;
117 Wipe_Available_in_GUI = false;
118 Is_SubPartition = false;
Dees_Troy2691f9d2012-09-24 11:15:49 -0400119 Has_SubPartition = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400120 SubPartition_Of = "";
121 Symlink_Path = "";
122 Symlink_Mount_Point = "";
123 Mount_Point = "";
Dees_Troye58d5262012-09-21 12:27:57 -0400124 Backup_Path = "";
Dees_Troy38bd7602012-09-14 13:33:53 -0400125 Actual_Block_Device = "";
126 Primary_Block_Device = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400127 Alternate_Block_Device = "";
128 Removable = false;
129 Is_Present = false;
130 Length = 0;
131 Size = 0;
132 Used = 0;
133 Free = 0;
134 Backup_Size = 0;
135 Can_Be_Encrypted = false;
136 Is_Encrypted = false;
137 Is_Decrypted = false;
Ethan Yonker253368a2014-11-25 15:00:52 -0600138 Mount_To_Decrypt = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400139 Decrypted_Block_Device = "";
140 Display_Name = "";
Dees_Troya13d74f2013-03-24 08:54:55 -0500141 Backup_Display_Name = "";
142 Storage_Name = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400143 Backup_Name = "";
Dees_Troy63c8df72012-09-10 14:02:05 -0400144 Backup_FileName = "";
Dees_Troy38bd7602012-09-14 13:33:53 -0400145 MTD_Name = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400146 Backup_Method = NONE;
Dees_Troy83bd4832013-05-04 12:39:56 +0000147 Can_Encrypt_Backup = false;
148 Use_Userdata_Encryption = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400149 Has_Data_Media = false;
Dees_Troye58d5262012-09-21 12:27:57 -0400150 Has_Android_Secure = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400151 Is_Storage = false;
Dees_Troya13d74f2013-03-24 08:54:55 -0500152 Is_Settings_Storage = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400153 Storage_Path = "";
154 Current_File_System = "";
155 Fstab_File_System = "";
Hashcode62bd9e02013-11-19 21:59:42 -0800156 Mount_Flags = 0;
157 Mount_Options = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400158 Format_Block_Size = 0;
Dees_Troy68cab492012-12-12 19:29:35 +0000159 Ignore_Blkid = false;
Dees_Troy16c2b312013-01-15 16:51:18 +0000160 Retain_Layout_Version = false;
Ethan Yonker253368a2014-11-25 15:00:52 -0600161 Crypto_Key_Location = "footer";
Ethan Yonker726a0202014-12-16 20:01:38 -0600162 MTP_Storage_ID = 0;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600163 Can_Flash_Img = false;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500164 Mount_Read_Only = false;
Ethan Yonker66a19492015-12-10 10:19:45 -0600165 Is_Adopted_Storage = false;
166 Adopted_GUID = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400167}
168
169TWPartition::~TWPartition(void) {
170 // Do nothing
171}
172
Dees_Troy5bf43922012-09-07 16:07:55 -0400173bool TWPartition::Process_Fstab_Line(string Line, bool Display_Error) {
174 char full_line[MAX_FSTAB_LINE_LENGTH], item[MAX_FSTAB_LINE_LENGTH];
175 int line_len = Line.size(), index = 0, item_index = 0;
176 char* ptr;
Dees_Troy51127312012-09-08 13:08:49 -0400177 string Flags;
Dees_Troy5bf43922012-09-07 16:07:55 -0400178 strncpy(full_line, Line.c_str(), line_len);
Dees_Troya13d74f2013-03-24 08:54:55 -0500179 bool skip = false;
Dees_Troy5bf43922012-09-07 16:07:55 -0400180
Dees_Troy51127312012-09-08 13:08:49 -0400181 for (index = 0; index < line_len; index++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500182 if (full_line[index] == 34)
183 skip = !skip;
184 if (!skip && full_line[index] <= 32)
Dees_Troy5bf43922012-09-07 16:07:55 -0400185 full_line[index] = '\0';
Dees_Troy5bf43922012-09-07 16:07:55 -0400186 }
Dees_Troy7c2dec82012-09-26 09:49:14 -0400187 Mount_Point = full_line;
Dees_Troy2673cec2013-04-02 20:22:16 +0000188 LOGINFO("Processing '%s'\n", Mount_Point.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -0400189 Backup_Path = Mount_Point;
Dees_Troya13d74f2013-03-24 08:54:55 -0500190 Storage_Path = Mount_Point;
Dees_Troy70737fa2013-04-08 13:19:20 +0000191 Display_Name = full_line + 1;
192 Backup_Display_Name = Display_Name;
193 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400194 index = Mount_Point.size();
195 while (index < line_len) {
196 while (index < line_len && full_line[index] == '\0')
197 index++;
198 if (index >= line_len)
199 continue;
200 ptr = full_line + index;
201 if (item_index == 0) {
202 // File System
203 Fstab_File_System = ptr;
204 Current_File_System = ptr;
205 item_index++;
206 } else if (item_index == 1) {
207 // Primary Block Device
Dees_Troy38bd7602012-09-14 13:33:53 -0400208 if (Fstab_File_System == "mtd" || Fstab_File_System == "yaffs2") {
Dees_Troy094207a2012-09-26 12:00:39 -0400209 MTD_Name = ptr;
210 Find_MTD_Block_Device(MTD_Name);
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400211 } else if (Fstab_File_System == "bml") {
212 if (Mount_Point == "/boot")
213 MTD_Name = "boot";
214 else if (Mount_Point == "/recovery")
215 MTD_Name = "recovery";
216 Primary_Block_Device = ptr;
217 if (*ptr != '/')
Dees_Troy2673cec2013-04-02 20:22:16 +0000218 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 -0400219 } else if (*ptr != '/') {
Dees_Troy5bf43922012-09-07 16:07:55 -0400220 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000221 LOGERR("Invalid block device on '%s', '%s', %i\n", Line.c_str(), ptr, index);
Dees_Troy5bf43922012-09-07 16:07:55 -0400222 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000223 LOGINFO("Invalid block device on '%s', '%s', %i\n", Line.c_str(), ptr, index);
Dees_Troy5bf43922012-09-07 16:07:55 -0400224 return 0;
Dees_Troy38bd7602012-09-14 13:33:53 -0400225 } else {
226 Primary_Block_Device = ptr;
227 Find_Real_Block_Device(Primary_Block_Device, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400228 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400229 item_index++;
230 } else if (item_index > 1) {
231 if (*ptr == '/') {
232 // Alternate Block Device
233 Alternate_Block_Device = ptr;
234 Find_Real_Block_Device(Alternate_Block_Device, Display_Error);
235 } else if (strlen(ptr) > 7 && strncmp(ptr, "length=", 7) == 0) {
236 // Partition length
237 ptr += 7;
238 Length = atoi(ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400239 } else if (strlen(ptr) > 6 && strncmp(ptr, "flags=", 6) == 0) {
240 // Custom flags, save for later so that new values aren't overwritten by defaults
241 ptr += 6;
242 Flags = ptr;
Dees_Troy68cab492012-12-12 19:29:35 +0000243 Process_Flags(Flags, Display_Error);
Dees_Troy38bd7602012-09-14 13:33:53 -0400244 } else if (strlen(ptr) == 4 && (strncmp(ptr, "NULL", 4) == 0 || strncmp(ptr, "null", 4) == 0 || strncmp(ptr, "null", 4) == 0)) {
245 // Do nothing
Dees_Troy5bf43922012-09-07 16:07:55 -0400246 } else {
247 // Unhandled data
Dees_Troy2673cec2013-04-02 20:22:16 +0000248 LOGINFO("Unhandled fstab information: '%s', %i, line: '%s'\n", ptr, index, Line.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400249 }
250 }
251 while (index < line_len && full_line[index] != '\0')
252 index++;
253 }
254
255 if (!Is_File_System(Fstab_File_System) && !Is_Image(Fstab_File_System)) {
256 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000257 LOGERR("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400258 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000259 LOGINFO("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400260 return 0;
261 } else if (Is_File_System(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400262 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400263 Setup_File_System(Display_Error);
264 if (Mount_Point == "/system") {
265 Display_Name = "System";
Dees_Troya13d74f2013-03-24 08:54:55 -0500266 Backup_Display_Name = Display_Name;
267 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400268 Wipe_Available_in_GUI = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500269 Can_Be_Backed_Up = true;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500270 Mount_Read_Only = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400271 } else if (Mount_Point == "/data") {
Dees Troyc657cc02015-01-16 22:48:47 +0000272 UnMount(false); // added in case /data is mounted as tmpfs for qcom hardware decrypt
Dees_Troy5bf43922012-09-07 16:07:55 -0400273 Display_Name = "Data";
Dees_Troya13d74f2013-03-24 08:54:55 -0500274 Backup_Display_Name = Display_Name;
275 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400276 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400277 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500278 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000279 Can_Encrypt_Backup = true;
280 Use_Userdata_Encryption = true;
Ethan Yonker6277c792014-09-15 14:54:30 -0500281 if (datamedia)
that9e0593e2014-10-08 00:01:24 +0200282 Setup_Data_Media();
Dees_Troy5bf43922012-09-07 16:07:55 -0400283#ifdef TW_INCLUDE_CRYPTO
284 Can_Be_Encrypted = true;
Dees_Troy657c3092012-09-10 20:32:10 -0400285 char crypto_blkdev[255];
286 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
287 if (strcmp(crypto_blkdev, "error") != 0) {
Dees_Troy657c3092012-09-10 20:32:10 -0400288 DataManager::SetValue(TW_IS_DECRYPTED, 1);
289 Is_Encrypted = true;
290 Is_Decrypted = true;
291 Decrypted_Block_Device = crypto_blkdev;
Dees_Troy2673cec2013-04-02 20:22:16 +0000292 LOGINFO("Data already decrypted, new block device: '%s'\n", crypto_blkdev);
Dees_Troy657c3092012-09-10 20:32:10 -0400293 } else if (!Mount(false)) {
Ethan Yonker71413f42014-02-26 13:36:08 -0600294 if (Is_Present) {
Ethan Yonker253368a2014-11-25 15:00:52 -0600295 set_partition_data(Actual_Block_Device.c_str(), Crypto_Key_Location.c_str(), Fstab_File_System.c_str());
Ethan Yonker71413f42014-02-26 13:36:08 -0600296 if (cryptfs_check_footer() == 0) {
297 Is_Encrypted = true;
298 Is_Decrypted = false;
299 Can_Be_Mounted = false;
300 Current_File_System = "emmc";
301 Setup_Image(Display_Error);
302 DataManager::SetValue(TW_IS_ENCRYPTED, 1);
Vojtech Bocek7e11ac52015-03-05 23:21:49 +0100303 DataManager::SetValue(TW_CRYPTO_PWTYPE, cryptfs_get_password_type());
Ethan Yonker71413f42014-02-26 13:36:08 -0600304 DataManager::SetValue(TW_CRYPTO_PASSWORD, "");
305 DataManager::SetValue("tw_crypto_display", "");
306 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500307 gui_err("mount_data_footer=Could not mount /data and unable to find crypto footer.");
Ethan Yonker71413f42014-02-26 13:36:08 -0600308 }
309 } else {
310 LOGERR("Primary block device '%s' for mount point '%s' is not present!\n", Primary_Block_Device.c_str(), Mount_Point.c_str());
311 }
Gary Peck82599a82012-11-21 16:23:12 -0800312 } else {
313 // Filesystem is not encrypted and the mount
314 // succeeded, so get it back to the original
315 // unmounted state
316 UnMount(false);
Dees_Troy51127312012-09-08 13:08:49 -0400317 }
Ethan Yonker6277c792014-09-15 14:54:30 -0500318 if (datamedia && (!Is_Encrypted || (Is_Encrypted && Is_Decrypted)))
Dees_Troy9b21af72012-10-01 15:51:46 -0400319 Recreate_Media_Folder();
Dees_Troy9b21af72012-10-01 15:51:46 -0400320#else
Ethan Yonker6277c792014-09-15 14:54:30 -0500321 if (datamedia)
322 Recreate_Media_Folder();
Dees_Troy5bf43922012-09-07 16:07:55 -0400323#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400324 } else if (Mount_Point == "/cache") {
325 Display_Name = "Cache";
Dees_Troya13d74f2013-03-24 08:54:55 -0500326 Backup_Display_Name = Display_Name;
327 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400328 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400329 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500330 Can_Be_Backed_Up = true;
Dees_Troyce2fe772012-09-28 12:34:33 -0400331 if (Mount(false) && !TWFunc::Path_Exists("/cache/recovery/.")) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000332 LOGINFO("Recreating /cache/recovery folder.\n");
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500333 if (mkdir("/cache/recovery", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0)
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500334 return -1;
Dees_Troyb46a6842012-09-25 11:06:46 -0400335 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400336 } else if (Mount_Point == "/datadata") {
Dees_Troy51127312012-09-08 13:08:49 -0400337 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400338 Display_Name = "DataData";
Dees_Troya13d74f2013-03-24 08:54:55 -0500339 Backup_Display_Name = Display_Name;
340 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400341 Is_SubPartition = true;
342 SubPartition_Of = "/data";
Dees_Troy5bf43922012-09-07 16:07:55 -0400343 DataManager::SetValue(TW_HAS_DATADATA, 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500344 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000345 Can_Encrypt_Backup = true;
346 Use_Userdata_Encryption = false; // This whole partition should be encrypted
Dees_Troy5bf43922012-09-07 16:07:55 -0400347 } else if (Mount_Point == "/sd-ext") {
Dees_Troy51127312012-09-08 13:08:49 -0400348 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400349 Display_Name = "SD-Ext";
Dees_Troya13d74f2013-03-24 08:54:55 -0500350 Backup_Display_Name = Display_Name;
351 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400352 Wipe_Available_in_GUI = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400353 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500354 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000355 Can_Encrypt_Backup = true;
356 Use_Userdata_Encryption = true;
Dees_Troy2c50e182012-09-26 20:05:28 -0400357 } else if (Mount_Point == "/boot") {
358 Display_Name = "Boot";
Dees_Troya13d74f2013-03-24 08:54:55 -0500359 Backup_Display_Name = Display_Name;
Dees_Troy2c50e182012-09-26 20:05:28 -0400360 DataManager::SetValue("tw_boot_is_mountable", 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500361 Can_Be_Backed_Up = true;
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500362 } else if (Mount_Point == "/vendor") {
363 Display_Name = "Vendor";
364 Backup_Display_Name = Display_Name;
365 Storage_Name = Display_Name;
366 Mount_Read_Only = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400367 }
368#ifdef TW_EXTERNAL_STORAGE_PATH
369 if (Mount_Point == EXPAND(TW_EXTERNAL_STORAGE_PATH)) {
370 Is_Storage = true;
371 Storage_Path = EXPAND(TW_EXTERNAL_STORAGE_PATH);
Dees_Troyc51f1f92012-09-20 15:32:13 -0400372 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500373 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400374#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000375 if (Mount_Point == "/sdcard" || Mount_Point == "/external_sd" || Mount_Point == "/external_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400376 Is_Storage = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400377 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500378 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400379#endif
Dees_Troyb05ddee2013-01-28 20:24:50 +0000380 }
Dees_Troy8170a922012-09-18 15:40:25 -0400381#ifdef TW_INTERNAL_STORAGE_PATH
382 if (Mount_Point == EXPAND(TW_INTERNAL_STORAGE_PATH)) {
383 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500384 Is_Settings_Storage = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400385 Storage_Path = EXPAND(TW_INTERNAL_STORAGE_PATH);
Dees_Troya13d74f2013-03-24 08:54:55 -0500386 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400387 }
388#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000389 if (Mount_Point == "/emmc" || Mount_Point == "/internal_sd" || Mount_Point == "/internal_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400390 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500391 Is_Settings_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500392 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400393 }
394#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400395 } else if (Is_Image(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400396 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400397 Setup_Image(Display_Error);
Dees_Troya13d74f2013-03-24 08:54:55 -0500398 if (Mount_Point == "/boot") {
399 Display_Name = "Boot";
400 Backup_Display_Name = Display_Name;
401 Can_Be_Backed_Up = true;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600402 Can_Flash_Img = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500403 } else if (Mount_Point == "/recovery") {
404 Display_Name = "Recovery";
405 Backup_Display_Name = Display_Name;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600406 Can_Flash_Img = true;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500407 } else if (Mount_Point == "/system_image") {
408 Display_Name = "System Image";
409 Backup_Display_Name = Display_Name;
HashBanged974bb2016-01-30 14:20:09 -0500410 Can_Flash_Img = true;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500411 Can_Be_Backed_Up = true;
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500412 } else if (Mount_Point == "/vendor_image") {
413 Display_Name = "Vendor Image";
414 Backup_Display_Name = Display_Name;
HashBanged974bb2016-01-30 14:20:09 -0500415 Can_Flash_Img = true;
Ethan Yonker1673e3d2015-10-26 21:51:58 -0500416 Can_Be_Backed_Up = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500417 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400418 }
419
Dees_Troy51127312012-09-08 13:08:49 -0400420 // Process any custom flags
421 if (Flags.size() > 0)
422 Process_Flags(Flags, Display_Error);
Dees_Troy51127312012-09-08 13:08:49 -0400423 return true;
424}
425
Ketut Putu Kumajayabfb72b02015-08-10 16:40:55 +0200426bool TWPartition::Process_FS_Flags(string& Options, int& Flags) {
Hashcode62bd9e02013-11-19 21:59:42 -0800427 int i;
428 char *p;
429 char *savep;
430 char fs_options[250];
431
432 strlcpy(fs_options, Options.c_str(), sizeof(fs_options));
433 Options = "";
434
435 p = strtok_r(fs_options, ",", &savep);
436 while (p) {
437 /* Look for the flag "p" in the flag list "fl"
438 * If not found, the loop exits with fl[i].name being null.
439 */
440 for (i = 0; mount_flags[i].name; i++) {
441 if (strncmp(p, mount_flags[i].name, strlen(mount_flags[i].name)) == 0) {
442 Flags |= mount_flags[i].flag;
443 break;
444 }
445 }
446
447 if (!mount_flags[i].name) {
448 if (Options.size() > 0)
449 Options += ",";
450 Options += p;
451 }
452 p = strtok_r(NULL, ",", &savep);
453 }
454
455 return true;
456}
457
Dees_Troy51127312012-09-08 13:08:49 -0400458bool TWPartition::Process_Flags(string Flags, bool Display_Error) {
459 char flags[MAX_FSTAB_LINE_LENGTH];
Dees_Troya13d74f2013-03-24 08:54:55 -0500460 int flags_len, index = 0, ptr_len;
Dees_Troy51127312012-09-08 13:08:49 -0400461 char* ptr;
Dees_Troya13d74f2013-03-24 08:54:55 -0500462 bool skip = false, has_display_name = false, has_storage_name = false, has_backup_name = false;
Dees_Troy51127312012-09-08 13:08:49 -0400463
464 strcpy(flags, Flags.c_str());
465 flags_len = Flags.size();
466 for (index = 0; index < flags_len; index++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500467 if (flags[index] == 34)
468 skip = !skip;
469 if (!skip && flags[index] == ';')
Dees_Troy51127312012-09-08 13:08:49 -0400470 flags[index] = '\0';
471 }
472
473 index = 0;
474 while (index < flags_len) {
475 while (index < flags_len && flags[index] == '\0')
476 index++;
477 if (index >= flags_len)
478 continue;
479 ptr = flags + index;
Dees_Troya13d74f2013-03-24 08:54:55 -0500480 ptr_len = strlen(ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400481 if (strcmp(ptr, "removable") == 0) {
482 Removable = true;
Ethan Yonker06c3f932014-02-02 22:11:14 -0600483 } else if (strncmp(ptr, "storage", 7) == 0) {
484 if (ptr_len == 7) {
Ethan Yonker06c3f932014-02-02 22:11:14 -0600485 Is_Storage = true;
486 } else if (ptr_len == 9) {
487 ptr += 9;
488 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y') {
489 LOGINFO("storage set to true\n");
490 Is_Storage = true;
491 } else {
492 LOGINFO("storage set to false\n");
493 Is_Storage = false;
494 }
495 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500496 } else if (strcmp(ptr, "settingsstorage") == 0) {
497 Is_Storage = true;
Xueferbfce5042015-04-14 02:02:05 +0800498 Is_Settings_Storage = true;
Matt Mowerbf4efa32014-04-14 23:25:26 -0500499 } else if (strcmp(ptr, "andsec") == 0) {
500 Has_Android_Secure = true;
Dees_Troy63c8df72012-09-10 14:02:05 -0400501 } else if (strcmp(ptr, "canbewiped") == 0) {
502 Can_Be_Wiped = true;
Hashcodedabfd492013-08-29 22:45:30 -0700503 } else if (strcmp(ptr, "usermrf") == 0) {
504 Use_Rm_Rf = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500505 } else if (ptr_len > 7 && strncmp(ptr, "backup=", 7) == 0) {
506 ptr += 7;
507 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y')
508 Can_Be_Backed_Up = true;
509 else
510 Can_Be_Backed_Up = false;
Dees_Troy63c8df72012-09-10 14:02:05 -0400511 } else if (strcmp(ptr, "wipeingui") == 0) {
512 Can_Be_Wiped = true;
513 Wipe_Available_in_GUI = true;
514 } else if (strcmp(ptr, "wipeduringfactoryreset") == 0) {
515 Can_Be_Wiped = true;
516 Wipe_Available_in_GUI = true;
517 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500518 } else if (ptr_len > 15 && strncmp(ptr, "subpartitionof=", 15) == 0) {
Dees_Troy2c4c26f2013-01-28 15:26:43 +0000519 ptr += 15;
Dees_Troy51127312012-09-08 13:08:49 -0400520 Is_SubPartition = true;
521 SubPartition_Of = ptr;
Dees_Troy68cab492012-12-12 19:29:35 +0000522 } else if (strcmp(ptr, "ignoreblkid") == 0) {
523 Ignore_Blkid = true;
Dees_Troy16c2b312013-01-15 16:51:18 +0000524 } else if (strcmp(ptr, "retainlayoutversion") == 0) {
525 Retain_Layout_Version = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500526 } else if (ptr_len > 8 && strncmp(ptr, "symlink=", 8) == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400527 ptr += 8;
528 Symlink_Path = ptr;
Dees_Troya13d74f2013-03-24 08:54:55 -0500529 } else if (ptr_len > 8 && strncmp(ptr, "display=", 8) == 0) {
530 has_display_name = true;
Dees_Troy51127312012-09-08 13:08:49 -0400531 ptr += 8;
Dees_Troya13d74f2013-03-24 08:54:55 -0500532 if (*ptr == '\"') ptr++;
Dees_Troy51127312012-09-08 13:08:49 -0400533 Display_Name = ptr;
Dees_Troya13d74f2013-03-24 08:54:55 -0500534 if (Display_Name.substr(Display_Name.size() - 1, 1) == "\"") {
535 Display_Name.resize(Display_Name.size() - 1);
536 }
537 } else if (ptr_len > 11 && strncmp(ptr, "storagename=", 11) == 0) {
538 has_storage_name = true;
539 ptr += 11;
540 if (*ptr == '\"') ptr++;
541 Storage_Name = ptr;
542 if (Storage_Name.substr(Storage_Name.size() - 1, 1) == "\"") {
543 Storage_Name.resize(Storage_Name.size() - 1);
544 }
545 } else if (ptr_len > 11 && strncmp(ptr, "backupname=", 10) == 0) {
546 has_backup_name = true;
547 ptr += 10;
548 if (*ptr == '\"') ptr++;
549 Backup_Display_Name = ptr;
550 if (Backup_Display_Name.substr(Backup_Display_Name.size() - 1, 1) == "\"") {
551 Backup_Display_Name.resize(Backup_Display_Name.size() - 1);
552 }
553 } else if (ptr_len > 10 && strncmp(ptr, "blocksize=", 10) == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400554 ptr += 10;
Ethan Yonkera2719152015-05-28 09:44:41 -0500555 Format_Block_Size = (unsigned long)(atol(ptr));
Dees_Troya13d74f2013-03-24 08:54:55 -0500556 } else if (ptr_len > 7 && strncmp(ptr, "length=", 7) == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400557 ptr += 7;
558 Length = atoi(ptr);
Dees_Troy83bd4832013-05-04 12:39:56 +0000559 } else if (ptr_len > 17 && strncmp(ptr, "canencryptbackup=", 17) == 0) {
560 ptr += 17;
561 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y')
562 Can_Encrypt_Backup = true;
563 else
564 Can_Encrypt_Backup = false;
565 } else if (ptr_len > 21 && strncmp(ptr, "userdataencryptbackup=", 21) == 0) {
566 ptr += 21;
567 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y') {
568 Can_Encrypt_Backup = true;
569 Use_Userdata_Encryption = true;
570 } else {
571 Use_Userdata_Encryption = false;
572 }
Hashcode62bd9e02013-11-19 21:59:42 -0800573 } else if (ptr_len > 8 && strncmp(ptr, "fsflags=", 8) == 0) {
574 ptr += 8;
575 if (*ptr == '\"') ptr++;
576
577 Mount_Options = ptr;
578 if (Mount_Options.substr(Mount_Options.size() - 1, 1) == "\"") {
579 Mount_Options.resize(Mount_Options.size() - 1);
580 }
581 Process_FS_Flags(Mount_Options, Mount_Flags);
Ethan Yonker253368a2014-11-25 15:00:52 -0600582 } else if ((ptr_len > 12 && strncmp(ptr, "encryptable=", 12) == 0) || (ptr_len > 13 && strncmp(ptr, "forceencrypt=", 13) == 0)) {
583 ptr += 12;
584 if (*ptr == '=') ptr++;
585 if (*ptr == '\"') ptr++;
586
587 Crypto_Key_Location = ptr;
588 if (Crypto_Key_Location.substr(Crypto_Key_Location.size() - 1, 1) == "\"") {
589 Crypto_Key_Location.resize(Crypto_Key_Location.size() - 1);
590 }
591 } else if (ptr_len > 8 && strncmp(ptr, "mounttodecrypt", 14) == 0) {
592 Mount_To_Decrypt = true;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600593 } else if (strncmp(ptr, "flashimg", 8) == 0) {
594 if (ptr_len == 8) {
595 Can_Flash_Img = true;
596 } else if (ptr_len == 10) {
597 ptr += 9;
598 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y') {
599 Can_Flash_Img = true;
600 } else {
601 Can_Flash_Img = false;
602 }
603 }
Dees_Troy51127312012-09-08 13:08:49 -0400604 } else {
605 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000606 LOGERR("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400607 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000608 LOGINFO("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400609 }
610 while (index < flags_len && flags[index] != '\0')
611 index++;
612 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500613 if (has_display_name && !has_storage_name)
614 Storage_Name = Display_Name;
615 if (!has_display_name && has_storage_name)
616 Display_Name = Storage_Name;
Dees_Troy74fb2e92013-04-15 14:35:47 +0000617 if (has_display_name && !has_backup_name && Backup_Display_Name != "Android Secure")
Dees_Troya13d74f2013-03-24 08:54:55 -0500618 Backup_Display_Name = Display_Name;
619 if (!has_display_name && has_backup_name)
620 Display_Name = Backup_Display_Name;
Dees_Troy51127312012-09-08 13:08:49 -0400621 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400622}
623
Dees_Troy5bf43922012-09-07 16:07:55 -0400624bool TWPartition::Is_File_System(string File_System) {
625 if (File_System == "ext2" ||
Dees_Troy63c8df72012-09-10 14:02:05 -0400626 File_System == "ext3" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400627 File_System == "ext4" ||
628 File_System == "vfat" ||
629 File_System == "ntfs" ||
630 File_System == "yaffs2" ||
bigbiff bigbiff3e146522012-11-14 14:32:59 -0500631 File_System == "exfat" ||
Dees_Troye5017042013-08-29 16:38:55 +0000632 File_System == "f2fs" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400633 File_System == "auto")
634 return true;
635 else
636 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400637}
638
Dees_Troy5bf43922012-09-07 16:07:55 -0400639bool TWPartition::Is_Image(string File_System) {
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400640 if (File_System == "emmc" || File_System == "mtd" || File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -0400641 return true;
642 else
643 return false;
644}
645
Dees_Troy51127312012-09-08 13:08:49 -0400646bool TWPartition::Make_Dir(string Path, bool Display_Error) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400647 if (!TWFunc::Path_Exists(Path)) {
Dees_Troy51127312012-09-08 13:08:49 -0400648 if (mkdir(Path.c_str(), 0777) == -1) {
649 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -0500650 gui_msg(Msg(msg::kError, "create_folder_strerr=Can not create '{1}' folder ({2}).")(Path)(strerror(errno)));
Dees_Troy51127312012-09-08 13:08:49 -0400651 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000652 LOGINFO("Can not create '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400653 return false;
654 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000655 LOGINFO("Created '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400656 return true;
657 }
658 }
659 return true;
660}
661
Dees_Troy5bf43922012-09-07 16:07:55 -0400662void TWPartition::Setup_File_System(bool Display_Error) {
663 struct statfs st;
664
665 Can_Be_Mounted = true;
666 Can_Be_Wiped = true;
667
Dees_Troy5bf43922012-09-07 16:07:55 -0400668 // Make the mount point folder if it doesn't exist
Dees_Troy51127312012-09-08 13:08:49 -0400669 Make_Dir(Mount_Point, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400670 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
671 Backup_Name = Display_Name;
672 Backup_Method = FILES;
673}
674
675void TWPartition::Setup_Image(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400676 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
677 Backup_Name = Display_Name;
Gary Peck82599a82012-11-21 16:23:12 -0800678 if (Current_File_System == "emmc")
Dees_Troy5bf43922012-09-07 16:07:55 -0400679 Backup_Method = DD;
Gary Peck82599a82012-11-21 16:23:12 -0800680 else if (Current_File_System == "mtd" || Current_File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -0400681 Backup_Method = FLASH_UTILS;
682 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000683 LOGINFO("Unhandled file system '%s' on image '%s'\n", Current_File_System.c_str(), Display_Name.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400684 if (Find_Partition_Size()) {
685 Used = Size;
686 Backup_Size = Size;
Dees_Troy51a0e822012-09-05 15:24:24 -0400687 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400688 if (Display_Error)
Dees Troyd932ce12013-10-18 17:12:59 +0000689 LOGERR("Unable to find partition size for '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400690 else
Dees Troyd932ce12013-10-18 17:12:59 +0000691 LOGINFO("Unable to find partition size for '%s'\n", Mount_Point.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400692 }
693}
694
Dees_Troye58d5262012-09-21 12:27:57 -0400695void TWPartition::Setup_AndSec(void) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500696 Backup_Display_Name = "Android Secure";
Dees_Troye58d5262012-09-21 12:27:57 -0400697 Backup_Name = "and-sec";
Dees_Troya13d74f2013-03-24 08:54:55 -0500698 Can_Be_Backed_Up = true;
Dees_Troye58d5262012-09-21 12:27:57 -0400699 Has_Android_Secure = true;
700 Symlink_Path = Mount_Point + "/.android_secure";
701 Symlink_Mount_Point = "/and-sec";
702 Backup_Path = Symlink_Mount_Point;
703 Make_Dir("/and-sec", true);
704 Recreate_AndSec_Folder();
Ethan Yonkerd4d10732014-02-03 15:27:52 -0600705 Mount_Storage_Retry();
Dees_Troye58d5262012-09-21 12:27:57 -0400706}
707
that9e0593e2014-10-08 00:01:24 +0200708void TWPartition::Setup_Data_Media() {
Ethan Yonker6277c792014-09-15 14:54:30 -0500709 LOGINFO("Setting up '%s' as data/media emulated storage.\n", Mount_Point.c_str());
Ethan Yonker66a19492015-12-10 10:19:45 -0600710 if (Storage_Name.empty() || Storage_Name == "Data")
711 Storage_Name = "Internal Storage";
Ethan Yonker6277c792014-09-15 14:54:30 -0500712 Has_Data_Media = true;
713 Is_Storage = true;
Ethan Yonker66a19492015-12-10 10:19:45 -0600714 Storage_Path = Mount_Point + "/media";
Ethan Yonker6277c792014-09-15 14:54:30 -0500715 Symlink_Path = Storage_Path;
Ethan Yonker66a19492015-12-10 10:19:45 -0600716 if (Mount_Point == "/data") {
717 Is_Settings_Storage = true;
718 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
719 Make_Dir("/emmc", false);
720 Symlink_Mount_Point = "/emmc";
721 } else {
722 Make_Dir("/sdcard", false);
723 Symlink_Mount_Point = "/sdcard";
724 }
725 if (Mount(false) && TWFunc::Path_Exists(Mount_Point + "/media/0")) {
726 Storage_Path = Mount_Point + "/media/0";
727 Symlink_Path = Storage_Path;
728 DataManager::SetValue(TW_INTERNAL_PATH, Mount_Point + "/media/0");
729 UnMount(true);
730 }
731 DataManager::SetValue("tw_has_internal", 1);
732 DataManager::SetValue("tw_has_data_media", 1);
733 du.add_absolute_dir(Mount_Point + "/misc/vold");
734 du.add_absolute_dir(Mount_Point + "/.layout_version");
735 du.add_absolute_dir(Mount_Point + "/system/storage.xml");
Ethan Yonker6277c792014-09-15 14:54:30 -0500736 } else {
Ethan Yonker66a19492015-12-10 10:19:45 -0600737 if (Mount(true) && TWFunc::Path_Exists(Mount_Point + "/media/0")) {
738 Storage_Path = Mount_Point + "/media/0";
739 Symlink_Path = Storage_Path;
740 UnMount(true);
741 }
Ethan Yonker6277c792014-09-15 14:54:30 -0500742 }
Ethan Yonker66a19492015-12-10 10:19:45 -0600743 du.add_absolute_dir(Mount_Point + "/media");
Ethan Yonker6277c792014-09-15 14:54:30 -0500744}
745
Dees_Troy5bf43922012-09-07 16:07:55 -0400746void TWPartition::Find_Real_Block_Device(string& Block, bool Display_Error) {
747 char device[512], realDevice[512];
748
749 strcpy(device, Block.c_str());
750 memset(realDevice, 0, sizeof(realDevice));
751 while (readlink(device, realDevice, sizeof(realDevice)) > 0)
752 {
753 strcpy(device, realDevice);
754 memset(realDevice, 0, sizeof(realDevice));
755 }
756
757 if (device[0] != '/') {
758 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000759 LOGERR("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400760 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000761 LOGINFO("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400762 return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400763 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400764 Block = device;
765 return;
766 }
767}
768
Dees_Troy8e337f32012-10-13 22:07:49 -0400769void TWPartition::Mount_Storage_Retry(void) {
770 // On some devices, storage doesn't want to mount right away, retry and sleep
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500771 if (!Mount(true)) {
Dees_Troy8e337f32012-10-13 22:07:49 -0400772 int retry_count = 5;
773 while (retry_count > 0 && !Mount(false)) {
774 usleep(500000);
775 retry_count--;
776 }
777 Mount(true);
778 }
779}
780
Dees_Troy38bd7602012-09-14 13:33:53 -0400781bool TWPartition::Find_MTD_Block_Device(string MTD_Name) {
782 FILE *fp = NULL;
783 char line[255];
784
785 fp = fopen("/proc/mtd", "rt");
786 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000787 LOGERR("Device does not support /proc/mtd\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400788 return false;
789 }
790
791 while (fgets(line, sizeof(line), fp) != NULL)
792 {
793 char device[32], label[32];
794 unsigned long size = 0;
795 char* fstype = NULL;
796 int deviceId;
797
798 sscanf(line, "%s %lx %*s %*c%s", device, &size, label);
799
800 // Skip header and blank lines
801 if ((strcmp(device, "dev:") == 0) || (strlen(line) < 8))
802 continue;
803
804 // Strip off the trailing " from the label
805 label[strlen(label)-1] = '\0';
806
807 if (strcmp(label, MTD_Name.c_str()) == 0) {
808 // We found our device
809 // Strip off the trailing : from the device
810 device[strlen(device)-1] = '\0';
811 if (sscanf(device,"mtd%d", &deviceId) == 1) {
812 sprintf(device, "/dev/block/mtdblock%d", deviceId);
813 Primary_Block_Device = device;
Dees_Troy76543db2013-06-19 16:24:30 +0000814 fclose(fp);
815 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -0400816 }
817 }
818 }
819 fclose(fp);
820
821 return false;
822}
823
Dees_Troy51127312012-09-08 13:08:49 -0400824bool TWPartition::Get_Size_Via_statfs(bool Display_Error) {
825 struct statfs st;
826 string Local_Path = Mount_Point + "/.";
827
828 if (!Mount(Display_Error))
829 return false;
830
831 if (statfs(Local_Path.c_str(), &st) != 0) {
832 if (!Removable) {
833 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000834 LOGERR("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400835 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000836 LOGINFO("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400837 }
838 return false;
839 }
840 Size = (st.f_blocks * st.f_bsize);
841 Used = ((st.f_blocks - st.f_bfree) * st.f_bsize);
842 Free = (st.f_bfree * st.f_bsize);
843 Backup_Size = Used;
844 return true;
845}
846
847bool TWPartition::Get_Size_Via_df(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400848 FILE* fp;
849 char command[255], line[512];
850 int include_block = 1;
851 unsigned int min_len;
852
853 if (!Mount(Display_Error))
854 return false;
855
Dees_Troy38bd7602012-09-14 13:33:53 -0400856 min_len = Actual_Block_Device.size() + 2;
Dees_Troy51127312012-09-08 13:08:49 -0400857 sprintf(command, "df %s > /tmp/dfoutput.txt", Mount_Point.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +0200858 TWFunc::Exec_Cmd(command);
Dees_Troy51127312012-09-08 13:08:49 -0400859 fp = fopen("/tmp/dfoutput.txt", "rt");
860 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000861 LOGINFO("Unable to open /tmp/dfoutput.txt.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400862 return false;
Dees_Troy51127312012-09-08 13:08:49 -0400863 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400864
865 while (fgets(line, sizeof(line), fp) != NULL)
866 {
867 unsigned long blocks, used, available;
868 char device[64];
869 char tmpString[64];
870
871 if (strncmp(line, "Filesystem", 10) == 0)
872 continue;
873 if (strlen(line) < min_len) {
874 include_block = 0;
875 continue;
876 }
877 if (include_block) {
878 sscanf(line, "%s %lu %lu %lu", device, &blocks, &used, &available);
879 } else {
880 // The device block string is so long that the df information is on the next line
881 int space_count = 0;
Dees_Troye58d5262012-09-21 12:27:57 -0400882 sprintf(tmpString, "/dev/block/%s", Actual_Block_Device.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400883 while (tmpString[space_count] == 32)
884 space_count++;
885 sscanf(line + space_count, "%lu %lu %lu", &blocks, &used, &available);
886 }
887
888 // Adjust block size to byte size
889 Size = blocks * 1024ULL;
890 Used = used * 1024ULL;
891 Free = available * 1024ULL;
892 Backup_Size = Used;
893 }
894 fclose(fp);
895 return true;
896}
897
Ethan Yonkerd18a8212015-12-14 10:17:00 -0600898unsigned long long TWPartition::IOCTL_Get_Block_Size() {
Ethan Yonkerd18a8212015-12-14 10:17:00 -0600899 Find_Actual_Block_Device();
Ethan Yonker483e9f42016-01-11 22:21:18 -0600900
901 return TWFunc::IOCTL_Get_Block_Size(Actual_Block_Device.c_str());
Ethan Yonkerd18a8212015-12-14 10:17:00 -0600902}
903
Dees_Troy5bf43922012-09-07 16:07:55 -0400904bool TWPartition::Find_Partition_Size(void) {
905 FILE* fp;
906 char line[512];
907 string tmpdevice;
908
igoriok87e3d932013-01-31 21:03:53 +0200909 fp = fopen("/proc/dumchar_info", "rt");
910 if (fp != NULL) {
911 while (fgets(line, sizeof(line), fp) != NULL)
912 {
913 char label[32], device[32];
914 unsigned long size = 0;
915
thatd43bf2d2014-09-21 23:13:02 +0200916 sscanf(line, "%s %lx %*x %*u %s", label, &size, device);
igoriok87e3d932013-01-31 21:03:53 +0200917
bigbiff bigbiff34684ff2013-12-01 21:03:45 -0500918 // Skip header, annotation and blank lines
igoriok87e3d932013-01-31 21:03:53 +0200919 if ((strncmp(device, "/dev/", 5) != 0) || (strlen(line) < 8))
920 continue;
921
922 tmpdevice = "/dev/";
923 tmpdevice += label;
924 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
925 Size = size;
926 fclose(fp);
927 return true;
928 }
929 }
930 }
931
Ethan Yonkerd18a8212015-12-14 10:17:00 -0600932 unsigned long long ioctl_size = IOCTL_Get_Block_Size();
933 if (ioctl_size) {
934 Size = ioctl_size;
935 return true;
936 }
937
Dees_Troy5bf43922012-09-07 16:07:55 -0400938 // In this case, we'll first get the partitions we care about (with labels)
939 fp = fopen("/proc/partitions", "rt");
940 if (fp == NULL)
941 return false;
942
943 while (fgets(line, sizeof(line), fp) != NULL)
944 {
945 unsigned long major, minor, blocks;
946 char device[512];
947 char tmpString[64];
948
Dees_Troy63c8df72012-09-10 14:02:05 -0400949 if (strlen(line) < 7 || line[0] == 'm') continue;
Dees_Troy5bf43922012-09-07 16:07:55 -0400950 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
951
952 tmpdevice = "/dev/block/";
953 tmpdevice += device;
Dees_Troy38bd7602012-09-14 13:33:53 -0400954 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400955 // Adjust block size to byte size
956 Size = blocks * 1024ULL;
957 fclose(fp);
958 return true;
959 }
960 }
961 fclose(fp);
962 return false;
963}
964
Dees_Troy5bf43922012-09-07 16:07:55 -0400965bool TWPartition::Is_Mounted(void) {
966 if (!Can_Be_Mounted)
967 return false;
968
969 struct stat st1, st2;
970 string test_path;
971
972 // Check to see if the mount point directory exists
973 test_path = Mount_Point + "/.";
974 if (stat(test_path.c_str(), &st1) != 0) return false;
975
976 // Check to see if the directory above the mount point exists
977 test_path = Mount_Point + "/../.";
978 if (stat(test_path.c_str(), &st2) != 0) return false;
979
980 // Compare the device IDs -- if they match then we're (probably) using tmpfs instead of an actual device
981 int ret = (st1.st_dev != st2.st_dev) ? true : false;
982
983 return ret;
984}
985
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -0500986bool TWPartition::Is_File_System_Writable(void) {
987 if (!Is_File_System(Current_File_System) || !Is_Mounted())
988 return false;
989
990 string test_path = Mount_Point + "/.";
991 return (access(test_path.c_str(), W_OK) == 0);
992}
993
Dees_Troy5bf43922012-09-07 16:07:55 -0400994bool TWPartition::Mount(bool Display_Error) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000995 int exfat_mounted = 0;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500996 unsigned long flags = Mount_Flags;
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000997
Dees_Troy5bf43922012-09-07 16:07:55 -0400998 if (Is_Mounted()) {
999 return true;
1000 } else if (!Can_Be_Mounted) {
1001 return false;
1002 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001003
1004 Find_Actual_Block_Device();
1005
1006 // Check the current file system before mounting
1007 Check_FS_Type();
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001008 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/exfat-fuse")) {
Dees_Troye34c1332013-02-06 19:13:00 +00001009 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 +00001010 LOGINFO("cmd: %s\n", cmd.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001011 string result;
1012 if (TWFunc::Exec_Cmd(cmd, result) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001013 LOGINFO("exfat-fuse failed to mount with result '%s', trying vfat\n", result.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001014 Current_File_System = "vfat";
1015 } else {
1016#ifdef TW_NO_EXFAT_FUSE
1017 UnMount(false);
1018 // We'll let the kernel handle it but using exfat-fuse to detect if the file system is actually exfat
1019 // Some kernels let us mount vfat as exfat which doesn't work out too well
1020#else
1021 exfat_mounted = 1;
1022#endif
1023 }
1024 }
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001025
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001026 if (Current_File_System == "ntfs" && (TWFunc::Path_Exists("/sbin/ntfs-3g") || TWFunc::Path_Exists("/sbin/mount.ntfs"))) {
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001027 string cmd;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001028 string Ntfsmount_Binary = "";
1029
1030 if (TWFunc::Path_Exists("/sbin/ntfs-3g"))
1031 Ntfsmount_Binary = "ntfs-3g";
1032 else if (TWFunc::Path_Exists("/sbin/mount.ntfs"))
1033 Ntfsmount_Binary = "mount.ntfs";
1034
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001035 if (Mount_Read_Only)
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001036 cmd = "/sbin/" + Ntfsmount_Binary + " -o ro " + Actual_Block_Device + " " + Mount_Point;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001037 else
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001038 cmd = "/sbin/" + Ntfsmount_Binary + " " + Actual_Block_Device + " " + Mount_Point;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001039 LOGINFO("cmd: '%s'\n", cmd.c_str());
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001040
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001041 if (TWFunc::Exec_Cmd(cmd) == 0) {
1042 return true;
1043 } else {
1044 LOGINFO("ntfs-3g failed to mount, trying regular mount method.\n");
1045 }
1046 }
1047
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001048 if (Mount_Read_Only)
1049 flags |= MS_RDONLY;
1050
Dees_Troy22042032012-12-18 21:23:08 +00001051 if (Fstab_File_System == "yaffs2") {
1052 // mount an MTD partition as a YAFFS2 filesystem.
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001053 flags = MS_NOATIME | MS_NODEV | MS_NODIRATIME;
1054 if (Mount_Read_Only)
1055 flags |= MS_RDONLY;
Dees_Troy76543db2013-06-19 16:24:30 +00001056 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags, NULL) < 0) {
1057 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags | MS_RDONLY, NULL) < 0) {
1058 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001059 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy76543db2013-06-19 16:24:30 +00001060 else
1061 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
1062 return false;
1063 } else {
1064 LOGINFO("Mounted '%s' (MTD) as RO\n", Mount_Point.c_str());
1065 return true;
1066 }
1067 } else {
1068 struct stat st;
1069 string test_path = Mount_Point;
1070 if (stat(test_path.c_str(), &st) < 0) {
1071 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001072 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy76543db2013-06-19 16:24:30 +00001073 else
1074 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
1075 return false;
1076 }
1077 mode_t new_mode = st.st_mode | S_IXUSR | S_IXGRP | S_IXOTH;
1078 if (new_mode != st.st_mode) {
1079 LOGINFO("Fixing execute permissions for %s\n", Mount_Point.c_str());
1080 if (chmod(Mount_Point.c_str(), new_mode) < 0) {
1081 if (Display_Error)
1082 LOGERR("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
1083 else
1084 LOGINFO("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
1085 return false;
1086 }
1087 }
Dees_Troy22042032012-12-18 21:23:08 +00001088 return true;
Dees_Troy76543db2013-06-19 16:24:30 +00001089 }
thatc7572eb2015-03-31 18:55:30 +02001090 }
1091
1092 string mount_fs = Current_File_System;
1093 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sys/module/texfat"))
1094 mount_fs = "texfat";
1095
1096 if (!exfat_mounted &&
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001097 mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), mount_fs.c_str(), flags, Mount_Options.c_str()) != 0 &&
1098 mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), mount_fs.c_str(), flags, NULL) != 0) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001099#ifdef TW_NO_EXFAT_FUSE
1100 if (Current_File_System == "exfat") {
Dees_Troy2673cec2013-04-02 20:22:16 +00001101 LOGINFO("Mounting exfat failed, trying vfat...\n");
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001102 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), "vfat", 0, NULL) != 0) {
Dees_Troy85f44ed2013-01-09 18:42:36 +00001103 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001104 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy85f44ed2013-01-09 18:42:36 +00001105 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001106 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001107 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 +00001108 return false;
Dees_Troy85f44ed2013-01-09 18:42:36 +00001109 }
Dees_Troyb05ddee2013-01-28 20:24:50 +00001110 } else {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001111#endif
bigbiff bigbiff26774a02014-03-29 18:22:00 -04001112 if (!Removable && Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001113 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001114 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001115 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
1116 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 +00001117 return false;
1118#ifdef TW_NO_EXFAT_FUSE
Dees_Troy85f44ed2013-01-09 18:42:36 +00001119 }
1120#endif
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001121 }
Ethan Yonker253368a2014-11-25 15:00:52 -06001122
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001123 if (Removable)
1124 Update_Size(Display_Error);
1125
xiaolu9416f4f2015-06-04 08:22:23 +08001126 if (!Symlink_Mount_Point.empty() && TWFunc::Path_Exists(Symlink_Path)) {
codelover2a3d4ce2015-03-14 20:26:49 +08001127 string Command = "mount -o bind '" + Symlink_Path + "' '" + Symlink_Mount_Point + "'";
Vojtech Bocek05534202013-09-11 08:11:56 +02001128 TWFunc::Exec_Cmd(Command);
Dees_Troy5bf43922012-09-07 16:07:55 -04001129 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001130 return true;
1131}
1132
1133bool TWPartition::UnMount(bool Display_Error) {
1134 if (Is_Mounted()) {
1135 int never_unmount_system;
1136
1137 DataManager::GetValue(TW_DONT_UNMOUNT_SYSTEM, never_unmount_system);
1138 if (never_unmount_system == 1 && Mount_Point == "/system")
1139 return true; // Never unmount system if you're not supposed to unmount it
1140
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001141 if (Is_Storage)
Ethan Yonker726a0202014-12-16 20:01:38 -06001142 PartitionManager.Remove_MTP_Storage(MTP_Storage_ID);
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001143
Dees_Troy38bd7602012-09-14 13:33:53 -04001144 if (!Symlink_Mount_Point.empty())
1145 umount(Symlink_Mount_Point.c_str());
1146
Dees_Troyb05ddee2013-01-28 20:24:50 +00001147 umount(Mount_Point.c_str());
1148 if (Is_Mounted()) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001149 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001150 gui_msg(Msg(msg::kError, "fail_unmount=Failed to unmount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy5bf43922012-09-07 16:07:55 -04001151 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001152 LOGINFO("Unable to unmount '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001153 return false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001154 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -04001155 return true;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001156 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001157 } else {
1158 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001159 }
1160}
1161
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05001162bool TWPartition::ReMount(bool Display_Error) {
1163 if (UnMount(Display_Error))
1164 return Mount(Display_Error);
1165 return false;
1166}
1167
1168bool TWPartition::ReMount_RW(bool Display_Error) {
1169 // No need to remount if already mounted rw
1170 if (Is_File_System_Writable())
1171 return true;
1172
1173 bool ro = Mount_Read_Only;
1174 int flags = Mount_Flags;
1175
1176 Mount_Read_Only = false;
1177 Mount_Flags &= ~MS_RDONLY;
1178
1179 bool ret = ReMount(Display_Error);
1180
1181 Mount_Read_Only = ro;
1182 Mount_Flags = flags;
1183
1184 return ret;
1185}
1186
Gary Peck43acadf2012-11-21 21:19:01 -08001187bool TWPartition::Wipe(string New_File_System) {
Ethan Yonker726a0202014-12-16 20:01:38 -06001188 bool wiped = false, update_crypt = false, recreate_media = true;
Dees_Troy16c2b312013-01-15 16:51:18 +00001189 int check;
1190 string Layout_Filename = Mount_Point + "/.layout_version";
1191
Dees_Troy38bd7602012-09-14 13:33:53 -04001192 if (!Can_Be_Wiped) {
Matt Mower3c366972015-12-25 19:28:31 -06001193 gui_msg(Msg(msg::kError, "cannot_wipe=Partition {1} cannot be wiped.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04001194 return false;
1195 }
1196
Dees_Troyc51f1f92012-09-20 15:32:13 -04001197 if (Mount_Point == "/cache")
Dees_Troy2673cec2013-04-02 20:22:16 +00001198 Log_Offset = 0;
Dees_Troyc51f1f92012-09-20 15:32:13 -04001199
Dees_Troy16c2b312013-01-15 16:51:18 +00001200 if (Retain_Layout_Version && Mount(false) && TWFunc::Path_Exists(Layout_Filename))
1201 TWFunc::copy_file(Layout_Filename, "/.layout_version", 0600);
1202 else
1203 unlink("/.layout_version");
Dees_Troy38bd7602012-09-14 13:33:53 -04001204
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001205 if (Has_Data_Media && Current_File_System == New_File_System) {
Dees_Troy16c2b312013-01-15 16:51:18 +00001206 wiped = Wipe_Data_Without_Wiping_Media();
Ethan Yonker5eac2222014-06-11 12:22:55 -05001207 recreate_media = false;
Dees_Troy16c2b312013-01-15 16:51:18 +00001208 } else {
Dees_Troy16c2b312013-01-15 16:51:18 +00001209 DataManager::GetValue(TW_RM_RF_VAR, check);
1210
Hashcodedabfd492013-08-29 22:45:30 -07001211 if (check || Use_Rm_Rf)
Dees_Troy16c2b312013-01-15 16:51:18 +00001212 wiped = Wipe_RMRF();
1213 else if (New_File_System == "ext4")
1214 wiped = Wipe_EXT4();
1215 else if (New_File_System == "ext2" || New_File_System == "ext3")
1216 wiped = Wipe_EXT23(New_File_System);
1217 else if (New_File_System == "vfat")
1218 wiped = Wipe_FAT();
1219 else if (New_File_System == "exfat")
1220 wiped = Wipe_EXFAT();
1221 else if (New_File_System == "yaffs2")
1222 wiped = Wipe_MTD();
Dees_Troye5017042013-08-29 16:38:55 +00001223 else if (New_File_System == "f2fs")
1224 wiped = Wipe_F2FS();
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001225 else if (New_File_System == "ntfs")
1226 wiped = Wipe_NTFS();
Dees_Troy16c2b312013-01-15 16:51:18 +00001227 else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001228 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 +00001229 unlink("/.layout_version");
1230 return false;
1231 }
1232 update_crypt = wiped;
Gary Pecke8bc5d72012-12-21 06:45:25 -08001233 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001234
Gary Pecke8bc5d72012-12-21 06:45:25 -08001235 if (wiped) {
Dees_Troy1c1ac442013-01-17 21:42:14 +00001236 if (Mount_Point == "/cache")
1237 DataManager::Output_Version();
1238
Dees_Troy16c2b312013-01-15 16:51:18 +00001239 if (TWFunc::Path_Exists("/.layout_version") && Mount(false))
1240 TWFunc::copy_file("/.layout_version", Layout_Filename, 0600);
1241
1242 if (update_crypt) {
1243 Setup_File_System(false);
1244 if (Is_Encrypted && !Is_Decrypted) {
1245 // just wiped an encrypted partition back to its unencrypted state
1246 Is_Encrypted = false;
1247 Is_Decrypted = false;
1248 Decrypted_Block_Device = "";
1249 if (Mount_Point == "/data") {
1250 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1251 DataManager::SetValue(TW_IS_DECRYPTED, 0);
1252 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001253 }
1254 }
Ethan Yonker5eac2222014-06-11 12:22:55 -05001255
Ethan Yonker66a19492015-12-10 10:19:45 -06001256 if (Has_Data_Media && recreate_media) {
Ethan Yonker5eac2222014-06-11 12:22:55 -05001257 Recreate_Media_Folder();
1258 }
Matt Mower209c9632016-01-20 12:08:35 -06001259 if (Is_Storage && Mount(false))
1260 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Gary Pecke8bc5d72012-12-21 06:45:25 -08001261 }
Matt Mower209c9632016-01-20 12:08:35 -06001262
Gary Pecke8bc5d72012-12-21 06:45:25 -08001263 return wiped;
Dees_Troy51a0e822012-09-05 15:24:24 -04001264}
1265
Gary Peck43acadf2012-11-21 21:19:01 -08001266bool TWPartition::Wipe() {
Gary Peck82599a82012-11-21 16:23:12 -08001267 if (Is_File_System(Current_File_System))
1268 return Wipe(Current_File_System);
1269 else
1270 return Wipe(Fstab_File_System);
Gary Peck43acadf2012-11-21 21:19:01 -08001271}
1272
Dees_Troye58d5262012-09-21 12:27:57 -04001273bool TWPartition::Wipe_AndSec(void) {
1274 if (!Has_Android_Secure)
1275 return false;
1276
Dees_Troye58d5262012-09-21 12:27:57 -04001277 if (!Mount(true))
1278 return false;
1279
Ethan Yonker74db1572015-10-28 12:44:49 -05001280 gui_msg(Msg("wiping=Wiping {1}")(Backup_Display_Name));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001281 TWFunc::removeDir(Mount_Point + "/.android_secure/", true);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001282 return true;
Dees_Troye58d5262012-09-21 12:27:57 -04001283}
1284
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001285bool TWPartition::Can_Repair() {
Ethan Yonkera2719152015-05-28 09:44:41 -05001286 if (Mount_Read_Only)
1287 return false;
Matt Mower18794c82015-11-11 16:22:45 -06001288 if (Current_File_System == "vfat" && TWFunc::Path_Exists("/sbin/fsck.fat"))
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001289 return true;
1290 else if ((Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") && TWFunc::Path_Exists("/sbin/e2fsck"))
1291 return true;
1292 else if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/fsck.exfat"))
1293 return true;
1294 else if (Current_File_System == "f2fs" && TWFunc::Path_Exists("/sbin/fsck.f2fs"))
1295 return true;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001296 else if (Current_File_System == "ntfs" && (TWFunc::Path_Exists("/sbin/ntfsfix") || TWFunc::Path_Exists("/sbin/fsck.ntfs")))
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001297 return true;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001298 return false;
1299}
1300
1301bool TWPartition::Repair() {
1302 string command;
1303
1304 if (Current_File_System == "vfat") {
Matt Mower18794c82015-11-11 16:22:45 -06001305 if (!TWFunc::Path_Exists("/sbin/fsck.fat")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001306 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.fat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001307 return false;
1308 }
1309 if (!UnMount(true))
1310 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001311 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.fat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001312 Find_Actual_Block_Device();
Matt Mower18794c82015-11-11 16:22:45 -06001313 command = "/sbin/fsck.fat -y " + Actual_Block_Device;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001314 LOGINFO("Repair command: %s\n", command.c_str());
1315 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001316 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001317 return true;
1318 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001319 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001320 return false;
1321 }
1322 }
1323 if (Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") {
1324 if (!TWFunc::Path_Exists("/sbin/e2fsck")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001325 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("e2fsck"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001326 return false;
1327 }
1328 if (!UnMount(true))
1329 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001330 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("e2fsck"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001331 Find_Actual_Block_Device();
Ethan Yonkera2719152015-05-28 09:44:41 -05001332 command = "/sbin/e2fsck -fp " + Actual_Block_Device;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001333 LOGINFO("Repair command: %s\n", command.c_str());
1334 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001335 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001336 return true;
1337 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001338 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001339 return false;
1340 }
1341 }
1342 if (Current_File_System == "exfat") {
1343 if (!TWFunc::Path_Exists("/sbin/fsck.exfat")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001344 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.exfat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001345 return false;
1346 }
1347 if (!UnMount(true))
1348 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001349 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.exfat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001350 Find_Actual_Block_Device();
1351 command = "/sbin/fsck.exfat " + Actual_Block_Device;
1352 LOGINFO("Repair command: %s\n", command.c_str());
1353 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001354 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001355 return true;
1356 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001357 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001358 return false;
1359 }
1360 }
1361 if (Current_File_System == "f2fs") {
1362 if (!TWFunc::Path_Exists("/sbin/fsck.f2fs")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001363 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.f2fs"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001364 return false;
1365 }
1366 if (!UnMount(true))
1367 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001368 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.f2fs"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001369 Find_Actual_Block_Device();
1370 command = "/sbin/fsck.f2fs " + Actual_Block_Device;
1371 LOGINFO("Repair command: %s\n", command.c_str());
1372 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001373 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001374 return true;
1375 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001376 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001377 return false;
1378 }
1379 }
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001380 if (Current_File_System == "ntfs") {
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001381 string Ntfsfix_Binary;
1382 if (TWFunc::Path_Exists("/sbin/ntfsfix"))
1383 Ntfsfix_Binary = "ntfsfix";
1384 else if (TWFunc::Path_Exists("/sbin/fsck.ntfs"))
1385 Ntfsfix_Binary = "fsck.ntfs";
1386 else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001387 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("ntfsfix"));
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001388 return false;
1389 }
1390 if (!UnMount(true))
1391 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001392 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)(Ntfsfix_Binary));
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001393 Find_Actual_Block_Device();
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001394 command = "/sbin/" + Ntfsfix_Binary + " " + Actual_Block_Device;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001395 LOGINFO("Repair command: %s\n", command.c_str());
1396 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001397 gui_msg("done=Done.");
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001398 return true;
1399 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001400 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001401 return false;
1402 }
1403 }
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001404 return false;
1405}
1406
Ethan Yonkera2719152015-05-28 09:44:41 -05001407bool TWPartition::Can_Resize() {
1408 if (Mount_Read_Only)
1409 return false;
1410 if ((Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") && TWFunc::Path_Exists("/sbin/resize2fs"))
1411 return true;
1412 return false;
1413}
1414
1415bool TWPartition::Resize() {
1416 string command;
1417
1418 if (Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") {
1419 if (!Can_Repair()) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001420 LOGINFO("Cannot resize %s because %s cannot be repaired before resizing.\n", Display_Name.c_str(), Display_Name.c_str());
1421 gui_msg(Msg(msg::kError, "cannot_resize=Cannot resize {1}.")(Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001422 return false;
1423 }
1424 if (!TWFunc::Path_Exists("/sbin/resize2fs")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001425 LOGINFO("resize2fs does not exist! Cannot resize!\n");
1426 gui_msg(Msg(msg::kError, "cannot_resize=Cannot resize {1}.")(Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001427 return false;
1428 }
1429 // Repair will unmount so no need to do it twice
Ethan Yonker74db1572015-10-28 12:44:49 -05001430 gui_msg(Msg("repair_resize=Repairing {1} before resizing.")( Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001431 if (!Repair())
1432 return false;
Ethan Yonker74db1572015-10-28 12:44:49 -05001433 gui_msg(Msg("resizing=Resizing {1} using {2}...")(Display_Name)("resize2fs"));
Ethan Yonkera2719152015-05-28 09:44:41 -05001434 Find_Actual_Block_Device();
1435 command = "/sbin/resize2fs " + Actual_Block_Device;
1436 if (Length != 0) {
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001437 unsigned long long Actual_Size = IOCTL_Get_Block_Size();
1438 if (Actual_Size == 0)
1439 return false;
Ethan Yonkera2719152015-05-28 09:44:41 -05001440
Ethan Yonkera2719152015-05-28 09:44:41 -05001441 unsigned long long Block_Count;
1442 if (Length < 0) {
1443 // Reduce overall size by this length
1444 Block_Count = (Actual_Size / 1024LLU) - ((unsigned long long)(Length * -1) / 1024LLU);
1445 } else {
1446 // This is the size, not a size reduction
1447 Block_Count = ((unsigned long long)(Length) / 1024LLU);
1448 }
1449 char temp[256];
1450 sprintf(temp, "%llu", Block_Count);
1451 command += " ";
1452 command += temp;
1453 command += "K";
1454 }
1455 LOGINFO("Resize command: %s\n", command.c_str());
1456 if (TWFunc::Exec_Cmd(command) == 0) {
1457 Update_Size(true);
Ethan Yonker74db1572015-10-28 12:44:49 -05001458 gui_msg("done=Done.");
Ethan Yonkera2719152015-05-28 09:44:41 -05001459 return true;
1460 } else {
1461 Update_Size(true);
Ethan Yonker74db1572015-10-28 12:44:49 -05001462 gui_msg(Msg(msg::kError, "unable_resize=Unable to resize {1}.")(Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001463 return false;
1464 }
1465 }
1466 return false;
1467}
1468
bigbiff7abc5fe2015-01-17 16:53:12 -05001469bool TWPartition::Backup(string backup_folder, const unsigned long long *overall_size, const unsigned long long *other_backups_size, pid_t &tar_fork_pid) {
1470 if (Backup_Method == FILES) {
1471 return Backup_Tar(backup_folder, overall_size, other_backups_size, tar_fork_pid);
1472 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001473 else if (Backup_Method == DD)
1474 return Backup_DD(backup_folder);
1475 else if (Backup_Method == FLASH_UTILS)
1476 return Backup_Dump_Image(backup_folder);
Dees_Troy2673cec2013-04-02 20:22:16 +00001477 LOGERR("Unknown backup method for '%s'\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001478 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001479}
1480
Dees_Troy43d8b002012-09-17 16:00:01 -04001481bool TWPartition::Check_MD5(string restore_folder) {
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001482 string Full_Filename, md5file;
Dees_Troy43d8b002012-09-17 16:00:01 -04001483 char split_filename[512];
1484 int index = 0;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001485 twrpDigest md5sum;
Dees_Troy43d8b002012-09-17 16:00:01 -04001486
Captain Throwbackff5935f2014-09-23 16:08:34 -04001487 sync();
1488
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001489 memset(split_filename, 0, sizeof(split_filename));
Dees_Troy43d8b002012-09-17 16:00:01 -04001490 Full_Filename = restore_folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04001491 if (!TWFunc::Path_Exists(Full_Filename)) {
1492 // This is a split archive, we presume
1493 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
Dees_Troy2673cec2013-04-02 20:22:16 +00001494 LOGINFO("split_filename: %s\n", split_filename);
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001495 md5file = split_filename;
1496 md5file += ".md5";
1497 if (!TWFunc::Path_Exists(md5file)) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001498 gui_msg(Msg(msg::kError, "no_md5_found=No md5 file found for '{1}'. Please unselect Enable MD5 verification to restore.")(split_filename));
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001499 return false;
1500 }
1501 md5sum.setfn(split_filename);
Dees_Troy83bd4832013-05-04 12:39:56 +00001502 while (index < 1000) {
1503 if (TWFunc::Path_Exists(split_filename) && md5sum.verify_md5digest() != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001504 gui_msg(Msg(msg::kError, "md5_fail_match=MD5 failed to match on '{1}'.")(split_filename));
Dees_Troy43d8b002012-09-17 16:00:01 -04001505 return false;
1506 }
1507 index++;
1508 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001509 md5sum.setfn(split_filename);
Dees_Troy43d8b002012-09-17 16:00:01 -04001510 }
Dees_Troy4a2a1262012-09-18 09:33:47 -04001511 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001512 } else {
1513 // Single file archive
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001514 md5file = Full_Filename + ".md5";
1515 if (!TWFunc::Path_Exists(md5file)) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001516 gui_msg(Msg(msg::kError, "no_md5_found=No md5 file found for '{1}'. Please unselect Enable MD5 verification to restore.")(split_filename));
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001517 return false;
1518 }
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001519 md5sum.setfn(Full_Filename);
1520 if (md5sum.verify_md5digest() != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001521 gui_msg(Msg(msg::kError, "md5_fail_match=MD5 failed to match on '{1}'.")(split_filename));
Dees_Troy43d8b002012-09-17 16:00:01 -04001522 return false;
1523 } else
1524 return true;
1525 }
1526 return false;
1527}
1528
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001529bool TWPartition::Restore(string restore_folder, const unsigned long long *total_restore_size, unsigned long long *already_restored_size) {
Gary Peck43acadf2012-11-21 21:19:01 -08001530 string Restore_File_System;
1531
Matt Mower3c366972015-12-25 19:28:31 -06001532 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, gui_parse_text("{@restoring_hdr}"));
Dees_Troy2673cec2013-04-02 20:22:16 +00001533 LOGINFO("Restore filename is: %s\n", Backup_FileName.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001534
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001535 Restore_File_System = Get_Restore_File_System(restore_folder);
1536
1537 if (Is_File_System(Restore_File_System))
1538 return Restore_Tar(restore_folder, Restore_File_System, total_restore_size, already_restored_size);
1539 else if (Is_Image(Restore_File_System)) {
Ethan Yonkerf66324f2015-01-09 16:46:57 -06001540 return Restore_Image(restore_folder, total_restore_size, already_restored_size, Restore_File_System);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001541 }
1542
1543 LOGERR("Unknown restore method for '%s'\n", Mount_Point.c_str());
1544 return false;
1545}
1546
1547string TWPartition::Get_Restore_File_System(string restore_folder) {
1548 size_t first_period, second_period;
1549 string Restore_File_System;
1550
Gary Peck43acadf2012-11-21 21:19:01 -08001551 // Parse backup filename to extract the file system before wiping
1552 first_period = Backup_FileName.find(".");
1553 if (first_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001554 LOGERR("Unable to find file system (first period).\n");
thatd43bf2d2014-09-21 23:13:02 +02001555 return string();
Gary Peck43acadf2012-11-21 21:19:01 -08001556 }
1557 Restore_File_System = Backup_FileName.substr(first_period + 1, Backup_FileName.size() - first_period - 1);
1558 second_period = Restore_File_System.find(".");
1559 if (second_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001560 LOGERR("Unable to find file system (second period).\n");
thatd43bf2d2014-09-21 23:13:02 +02001561 return string();
Gary Peck43acadf2012-11-21 21:19:01 -08001562 }
1563 Restore_File_System.resize(second_period);
Dees_Troy2673cec2013-04-02 20:22:16 +00001564 LOGINFO("Restore file system is: '%s'.\n", Restore_File_System.c_str());
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001565 return Restore_File_System;
Dees_Troy51a0e822012-09-05 15:24:24 -04001566}
1567
1568string TWPartition::Backup_Method_By_Name() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001569 if (Backup_Method == NONE)
1570 return "none";
1571 else if (Backup_Method == FILES)
1572 return "files";
1573 else if (Backup_Method == DD)
1574 return "dd";
1575 else if (Backup_Method == FLASH_UTILS)
1576 return "flash_utils";
1577 else
1578 return "undefined";
1579 return "ERROR!";
Dees_Troy51a0e822012-09-05 15:24:24 -04001580}
1581
1582bool TWPartition::Decrypt(string Password) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001583 LOGINFO("STUB TWPartition::Decrypt, password: '%s'\n", Password.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001584 // Is this needed?
Dees_Troy51a0e822012-09-05 15:24:24 -04001585 return 1;
1586}
1587
1588bool TWPartition::Wipe_Encryption() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001589 bool Save_Data_Media = Has_Data_Media;
1590
1591 if (!UnMount(true))
1592 return false;
1593
Dees_Troy38bd7602012-09-14 13:33:53 -04001594 Has_Data_Media = false;
Dees_Troy74fb2e92013-04-15 14:35:47 +00001595 Decrypted_Block_Device = "";
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001596#ifdef TW_INCLUDE_CRYPTO
1597 if (Is_Decrypted) {
1598 if (!UnMount(true))
1599 return false;
Matt Mower2b18a532015-02-20 16:58:05 -06001600 if (delete_crypto_blk_dev((char*)("userdata")) != 0) {
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001601 LOGERR("Error deleting crypto block device, continuing anyway.\n");
1602 }
1603 }
1604#endif
Dees_Troy74fb2e92013-04-15 14:35:47 +00001605 Is_Decrypted = false;
1606 Is_Encrypted = false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001607 Find_Actual_Block_Device();
Ethan Yonkerbc85b632015-08-09 12:48:14 -05001608 if (Crypto_Key_Location == "footer") {
1609 int newlen, fd;
1610 if (Length != 0) {
1611 newlen = Length;
1612 if (newlen < 0)
1613 newlen = newlen * -1;
1614 } else {
1615 newlen = CRYPT_FOOTER_OFFSET;
1616 }
1617 if ((fd = open(Actual_Block_Device.c_str(), O_RDWR)) < 0) {
1618 gui_print_color("warning", "Unable to open '%s' to wipe crypto key\n", Actual_Block_Device.c_str());
1619 } else {
1620 unsigned int block_count;
1621 if ((ioctl(fd, BLKGETSIZE, &block_count)) == -1) {
1622 gui_print_color("warning", "Unable to get block size for wiping crypto footer.\n");
1623 } else {
1624 off64_t offset = ((off64_t)block_count * 512) - newlen;
1625 if (lseek64(fd, offset, SEEK_SET) == -1) {
1626 gui_print_color("warning", "Unable to lseek64 for wiping crypto footer.\n");
1627 } else {
1628 void* buffer = malloc(newlen);
1629 if (!buffer) {
1630 gui_print_color("warning", "Failed to malloc for wiping crypto footer.\n");
1631 } else {
1632 memset(buffer, 0, newlen);
1633 int ret = write(fd, buffer, newlen);
1634 if (ret != newlen) {
1635 gui_print_color("warning", "Failed to wipe crypto footer.\n");
1636 } else {
1637 LOGINFO("Successfully wiped crypto footer.\n");
1638 }
Matt Mower13a8f0b2015-09-26 15:40:03 -05001639 free(buffer);
Ethan Yonkerbc85b632015-08-09 12:48:14 -05001640 }
1641 }
1642 }
1643 close(fd);
1644 }
1645 } else {
Ethan Yonkerc2dafbb2016-03-15 22:20:59 -05001646 if (TWFunc::IOCTL_Get_Block_Size(Crypto_Key_Location.c_str()) >= 16384LLU) {
1647 string Command = "dd of='" + Crypto_Key_Location + "' if=/dev/zero bs=16384 count=1";
1648 TWFunc::Exec_Cmd(Command);
1649 } else {
1650 LOGINFO("Crypto key location reports size < 16K so not wiping crypto footer.\n");
1651 }
Ethan Yonkerbc85b632015-08-09 12:48:14 -05001652 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001653 if (Wipe(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001654 Has_Data_Media = Save_Data_Media;
1655 if (Has_Data_Media && !Symlink_Mount_Point.empty()) {
1656 Recreate_Media_Folder();
Ethan Yonker726a0202014-12-16 20:01:38 -06001657 if (Mount(false))
1658 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04001659 }
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001660 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
Ethan Yonker83e82572014-04-04 10:59:28 -05001661#ifndef TW_OEM_BUILD
Ethan Yonker74db1572015-10-28 12:44:49 -05001662 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 -05001663#endif
Dees_Troy38bd7602012-09-14 13:33:53 -04001664 return true;
1665 } else {
1666 Has_Data_Media = Save_Data_Media;
Ethan Yonker74db1572015-10-28 12:44:49 -05001667 gui_err("format_data_err=Unable to format to remove encryption.");
Ethan Yonker726a0202014-12-16 20:01:38 -06001668 if (Has_Data_Media && Mount(false))
1669 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04001670 return false;
1671 }
1672 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001673}
1674
1675void TWPartition::Check_FS_Type() {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001676 const char* type;
1677 blkid_probe pr;
Dees_Troy5bf43922012-09-07 16:07:55 -04001678
Dees_Troy68cab492012-12-12 19:29:35 +00001679 if (Fstab_File_System == "yaffs2" || Fstab_File_System == "mtd" || Fstab_File_System == "bml" || Ignore_Blkid)
1680 return; // Running blkid on some mtd devices causes a massive crash or needs to be skipped
Dees_Troy5bf43922012-09-07 16:07:55 -04001681
Dees_Troy38bd7602012-09-14 13:33:53 -04001682 Find_Actual_Block_Device();
Dees_Troy8170a922012-09-18 15:40:25 -04001683 if (!Is_Present)
1684 return;
Dees_Troy51127312012-09-08 13:08:49 -04001685
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001686 pr = blkid_new_probe_from_filename(Actual_Block_Device.c_str());
1687 if (blkid_do_fullprobe(pr)) {
1688 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00001689 LOGINFO("Can't probe device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001690 return;
Dees_Troy5bf43922012-09-07 16:07:55 -04001691 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001692
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05001693 if (blkid_probe_lookup_value(pr, "TYPE", &type, NULL) < 0) {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001694 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00001695 LOGINFO("can't find filesystem on device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001696 return;
1697 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001698
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001699 Current_File_System = type;
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001700 blkid_free_probe(pr);
Dees_Troy51a0e822012-09-05 15:24:24 -04001701}
1702
Gary Peck43acadf2012-11-21 21:19:01 -08001703bool TWPartition::Wipe_EXT23(string File_System) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001704 if (!UnMount(true))
1705 return false;
1706
Dees_Troy43d8b002012-09-17 16:00:01 -04001707 if (TWFunc::Path_Exists("/sbin/mke2fs")) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001708 string command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001709
Greg Wallace4b44fef2015-12-29 15:33:24 -05001710 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mke2fs"));
Dees_Troy38bd7602012-09-14 13:33:53 -04001711 Find_Actual_Block_Device();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001712 command = "mke2fs -t " + File_System + " -m 0 " + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001713 LOGINFO("mke2fs command: %s\n", command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001714 if (TWFunc::Exec_Cmd(command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001715 Current_File_System = File_System;
Dees_Troye58d5262012-09-21 12:27:57 -04001716 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05001717 gui_msg("done=Done.");
Dees_Troy38bd7602012-09-14 13:33:53 -04001718 return true;
1719 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001720 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04001721 return false;
1722 }
1723 } else
1724 return Wipe_RMRF();
1725
1726 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001727}
1728
1729bool TWPartition::Wipe_EXT4() {
Ethan Yonker25f20c12014-10-14 09:04:43 -05001730 Find_Actual_Block_Device();
1731 if (!Is_Present) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001732 LOGINFO("Block device not present, cannot wipe %s.\n", Display_Name.c_str());
1733 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Ethan Yonker25f20c12014-10-14 09:04:43 -05001734 return false;
1735 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001736 if (!UnMount(true))
1737 return false;
1738
Dees_Troyb3265ab2013-08-30 02:59:14 +00001739#if defined(HAVE_SELINUX) && defined(USE_EXT4)
Ethan Yonkerf27497f2014-02-09 11:48:33 -06001740 int ret;
1741 char *secontext = NULL;
1742
Greg Wallace4b44fef2015-12-29 15:33:24 -05001743 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("make_ext4fs"));
Ethan Yonkerf27497f2014-02-09 11:48:33 -06001744
Dees Troy99c8dbf2014-03-10 16:53:58 +00001745 if (!selinux_handle || selabel_lookup(selinux_handle, &secontext, Mount_Point.c_str(), S_IFDIR) < 0) {
Ethan Yonkerf27497f2014-02-09 11:48:33 -06001746 LOGINFO("Cannot lookup security context for '%s'\n", Mount_Point.c_str());
1747 ret = make_ext4fs(Actual_Block_Device.c_str(), Length, Mount_Point.c_str(), NULL);
1748 } else {
1749 ret = make_ext4fs(Actual_Block_Device.c_str(), Length, Mount_Point.c_str(), selinux_handle);
1750 }
1751 if (ret != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001752 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troya95f55c2013-08-17 13:14:43 +00001753 return false;
1754 } else {
bigbiff bigbiffc49d7062013-10-11 20:28:00 -04001755 string sedir = Mount_Point + "/lost+found";
1756 PartitionManager.Mount_By_Path(sedir.c_str(), true);
1757 rmdir(sedir.c_str());
1758 mkdir(sedir.c_str(), S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP);
Dees_Troya95f55c2013-08-17 13:14:43 +00001759 return true;
1760 }
1761#else
Dees_Troy43d8b002012-09-17 16:00:01 -04001762 if (TWFunc::Path_Exists("/sbin/make_ext4fs")) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001763 string Command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001764
Greg Wallace4b44fef2015-12-29 15:33:24 -05001765 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("make_ext4fs"));
Dees_Troy38bd7602012-09-14 13:33:53 -04001766 Find_Actual_Block_Device();
1767 Command = "make_ext4fs";
1768 if (!Is_Decrypted && Length != 0) {
1769 // Only use length if we're not decrypted
1770 char len[32];
1771 sprintf(len, "%i", Length);
1772 Command += " -l ";
1773 Command += len;
1774 }
Dees_Troy5295d582013-09-06 15:51:08 +00001775 if (TWFunc::Path_Exists("/file_contexts")) {
1776 Command += " -S /file_contexts";
1777 }
1778 Command += " -a " + Mount_Point + " " + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001779 LOGINFO("make_ext4fs command: %s\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001780 if (TWFunc::Exec_Cmd(Command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001781 Current_File_System = "ext4";
Dees_Troye58d5262012-09-21 12:27:57 -04001782 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05001783 gui_msg("done=Done.");
Dees_Troy38bd7602012-09-14 13:33:53 -04001784 return true;
1785 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001786 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04001787 return false;
1788 }
1789 } else
Gary Peck43acadf2012-11-21 21:19:01 -08001790 return Wipe_EXT23("ext4");
Dees_Troya95f55c2013-08-17 13:14:43 +00001791#endif
Dees_Troy38bd7602012-09-14 13:33:53 -04001792 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001793}
1794
1795bool TWPartition::Wipe_FAT() {
Vojtech Bocek05534202013-09-11 08:11:56 +02001796 string command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001797
Matt Mower18794c82015-11-11 16:22:45 -06001798 if (TWFunc::Path_Exists("/sbin/mkfs.fat")) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001799 if (!UnMount(true))
1800 return false;
1801
Greg Wallace4b44fef2015-12-29 15:33:24 -05001802 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mkfs.fat"));
Dees_Troy38bd7602012-09-14 13:33:53 -04001803 Find_Actual_Block_Device();
Matt Mower18794c82015-11-11 16:22:45 -06001804 command = "mkfs.fat " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02001805 if (TWFunc::Exec_Cmd(command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001806 Current_File_System = "vfat";
Dees_Troye58d5262012-09-21 12:27:57 -04001807 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05001808 gui_msg("done=Done.");
Dees_Troy38bd7602012-09-14 13:33:53 -04001809 return true;
1810 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001811 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04001812 return false;
1813 }
1814 return true;
1815 }
1816 else
1817 return Wipe_RMRF();
1818
1819 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001820}
1821
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001822bool TWPartition::Wipe_EXFAT() {
Vojtech Bocek05534202013-09-11 08:11:56 +02001823 string command;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001824
1825 if (TWFunc::Path_Exists("/sbin/mkexfatfs")) {
1826 if (!UnMount(true))
1827 return false;
1828
Greg Wallace4b44fef2015-12-29 15:33:24 -05001829 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mkexfatfs"));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001830 Find_Actual_Block_Device();
1831 command = "mkexfatfs " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02001832 if (TWFunc::Exec_Cmd(command) == 0) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001833 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05001834 gui_msg("done=Done.");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001835 return true;
1836 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001837 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001838 return false;
1839 }
1840 return true;
1841 }
1842 return false;
1843}
1844
Dees_Troy38bd7602012-09-14 13:33:53 -04001845bool TWPartition::Wipe_MTD() {
1846 if (!UnMount(true))
1847 return false;
1848
Greg Wallace4b44fef2015-12-29 15:33:24 -05001849 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("MTD"));
Dees_Troy38bd7602012-09-14 13:33:53 -04001850
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001851 mtd_scan_partitions();
1852 const MtdPartition* mtd = mtd_find_partition_by_name(MTD_Name.c_str());
1853 if (mtd == NULL) {
1854 LOGERR("No mtd partition named '%s'", MTD_Name.c_str());
1855 return false;
1856 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001857
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001858 MtdWriteContext* ctx = mtd_write_partition(mtd);
1859 if (ctx == NULL) {
1860 LOGERR("Can't write '%s', failed to format.", MTD_Name.c_str());
1861 return false;
1862 }
1863 if (mtd_erase_blocks(ctx, -1) == -1) {
1864 mtd_write_close(ctx);
1865 LOGERR("Failed to format '%s'", MTD_Name.c_str());
1866 return false;
1867 }
1868 if (mtd_write_close(ctx) != 0) {
1869 LOGERR("Failed to close '%s'", MTD_Name.c_str());
1870 return false;
1871 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001872 Current_File_System = "yaffs2";
Dees_Troye58d5262012-09-21 12:27:57 -04001873 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05001874 gui_msg("done=Done.");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001875 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001876}
1877
1878bool TWPartition::Wipe_RMRF() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001879 if (!Mount(true))
1880 return false;
Ethan Yonker726a0202014-12-16 20:01:38 -06001881 // This is the only wipe that leaves the partition mounted, so we
1882 // must manually remove the partition from MTP if it is a storage
1883 // partition.
1884 if (Is_Storage)
1885 PartitionManager.Remove_MTP_Storage(MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04001886
Ethan Yonker74db1572015-10-28 12:44:49 -05001887 gui_msg(Msg("remove_all=Removing all files under '{1}'")(Mount_Point));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001888 TWFunc::removeDir(Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04001889 Recreate_AndSec_Folder();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001890 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001891}
1892
Dees_Troye5017042013-08-29 16:38:55 +00001893bool TWPartition::Wipe_F2FS() {
Vojtech Bocek05534202013-09-11 08:11:56 +02001894 string command;
Dees_Troye5017042013-08-29 16:38:55 +00001895
1896 if (TWFunc::Path_Exists("/sbin/mkfs.f2fs")) {
1897 if (!UnMount(true))
1898 return false;
1899
Greg Wallace4b44fef2015-12-29 15:33:24 -05001900 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mkfs.f2fs"));
Dees_Troye5017042013-08-29 16:38:55 +00001901 Find_Actual_Block_Device();
Greg Wallace558aef72016-01-19 21:14:30 -05001902 command = "mkfs.f2fs -t 0";
dhacker29a3fa75f2015-01-17 19:42:33 -05001903 if (!Is_Decrypted && Length != 0) {
1904 // Only use length if we're not decrypted
1905 char len[32];
1906 int mod_length = Length;
1907 if (Length < 0)
1908 mod_length *= -1;
1909 sprintf(len, "%i", mod_length);
1910 command += " -r ";
1911 command += len;
1912 }
1913 command += " " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02001914 if (TWFunc::Exec_Cmd(command) == 0) {
Dees_Troye5017042013-08-29 16:38:55 +00001915 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05001916 gui_msg("done=Done.");
Dees_Troye5017042013-08-29 16:38:55 +00001917 return true;
1918 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001919 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troye5017042013-08-29 16:38:55 +00001920 return false;
1921 }
1922 return true;
1923 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001924 LOGINFO("mkfs.f2fs binary not found, using rm -rf to wipe.\n");
Dees_Troye5017042013-08-29 16:38:55 +00001925 return Wipe_RMRF();
1926 }
1927 return false;
1928}
1929
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001930bool TWPartition::Wipe_NTFS() {
1931 string command;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001932 string Ntfsmake_Binary;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001933
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001934 if (TWFunc::Path_Exists("/sbin/mkntfs"))
1935 Ntfsmake_Binary = "mkntfs";
1936 else if (TWFunc::Path_Exists("/sbin/mkfs.ntfs"))
1937 Ntfsmake_Binary = "mkfs.ntfs";
1938 else
1939 return false;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001940
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001941 if (!UnMount(true))
1942 return false;
1943
Greg Wallace4b44fef2015-12-29 15:33:24 -05001944 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)(Ntfsmake_Binary));
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001945 Find_Actual_Block_Device();
1946 command = "/sbin/" + Ntfsmake_Binary + " " + Actual_Block_Device;
1947 if (TWFunc::Exec_Cmd(command) == 0) {
1948 Recreate_AndSec_Folder();
1949 gui_msg("done=Done.");
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001950 return true;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001951 } else {
1952 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
1953 return false;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001954 }
1955 return false;
1956}
1957
Dees_Troy51a0e822012-09-05 15:24:24 -04001958bool TWPartition::Wipe_Data_Without_Wiping_Media() {
Ethan Yonker83e82572014-04-04 10:59:28 -05001959#ifdef TW_OEM_BUILD
1960 // In an OEM Build we want to do a full format
1961 return Wipe_Encryption();
1962#else
Ethan Yonker66a19492015-12-10 10:19:45 -06001963 bool ret = false;
Dees_Troy38bd7602012-09-14 13:33:53 -04001964
Dees_Troy38bd7602012-09-14 13:33:53 -04001965 if (!Mount(true))
1966 return false;
1967
Ethan Yonker74db1572015-10-28 12:44:49 -05001968 gui_msg("wiping_data=Wiping data without wiping /data/media ...");
Ethan Yonker66a19492015-12-10 10:19:45 -06001969 ret = Wipe_Data_Without_Wiping_Media_Func(Mount_Point + "/");
1970 if (ret)
1971 gui_msg("done=Done.");
1972 return ret;
1973#endif // ifdef TW_OEM_BUILD
1974}
1975
1976bool TWPartition::Wipe_Data_Without_Wiping_Media_Func(const string& parent __unused) {
1977 string dir;
Dees_Troy38bd7602012-09-14 13:33:53 -04001978
1979 DIR* d;
Ethan Yonker66a19492015-12-10 10:19:45 -06001980 d = opendir(parent.c_str());
Dees_Troy16b74352012-11-14 22:27:31 +00001981 if (d != NULL) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001982 struct dirent* de;
1983 while ((de = readdir(d)) != NULL) {
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05001984 if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) continue;
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05001985
Ethan Yonker66a19492015-12-10 10:19:45 -06001986 dir = parent;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001987 dir.append(de->d_name);
Ethan Yonker66a19492015-12-10 10:19:45 -06001988 if (du.check_skip_dirs(dir)) {
1989 LOGINFO("skipped '%s'\n", dir.c_str());
1990 continue;
1991 }
Dees_Troyce675462013-01-09 19:48:21 +00001992 if (de->d_type == DT_DIR) {
Ethan Yonker66a19492015-12-10 10:19:45 -06001993 dir.append("/");
1994 if (!Wipe_Data_Without_Wiping_Media_Func(dir)) {
1995 closedir(d);
1996 return false;
1997 }
1998 rmdir(dir.c_str());
bigbiff bigbiff98f1f902013-01-19 18:46:13 -05001999 } else if (de->d_type == DT_REG || de->d_type == DT_LNK || de->d_type == DT_FIFO || de->d_type == DT_SOCK) {
Dees_Troyce675462013-01-09 19:48:21 +00002000 if (!unlink(dir.c_str()))
Dees_Troy2673cec2013-04-02 20:22:16 +00002001 LOGINFO("Unable to unlink '%s'\n", dir.c_str());
Dees_Troyce675462013-01-09 19:48:21 +00002002 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002003 }
2004 closedir(d);
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05002005
Dees_Troy16b74352012-11-14 22:27:31 +00002006 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -04002007 }
Ethan Yonker74db1572015-10-28 12:44:49 -05002008 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy16b74352012-11-14 22:27:31 +00002009 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04002010}
2011
bigbiff7abc5fe2015-01-17 16:53:12 -05002012bool TWPartition::Backup_Tar(string backup_folder, const unsigned long long *overall_size, const unsigned long long *other_backups_size, pid_t &tar_fork_pid) {
Dees_Troy4a2a1262012-09-18 09:33:47 -04002013 char back_name[255], split_index[5];
2014 string Full_FileName, Split_FileName, Tar_Args, Command;
Dees_Troy83bd4832013-05-04 12:39:56 +00002015 int use_compression, use_encryption = 0, index, backup_count;
Dees_Troy4a2a1262012-09-18 09:33:47 -04002016 struct stat st;
Dees_Troy7c2dec82012-09-26 09:49:14 -04002017 unsigned long long total_bsize = 0, file_size;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002018 twrpTar tar;
2019 vector <string> files;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05002020
Dees_Troy43d8b002012-09-17 16:00:01 -04002021 if (!Mount(true))
2022 return false;
2023
Dees_Troya13d74f2013-03-24 08:54:55 -05002024 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Backup_Display_Name, "Backing Up");
Ethan Yonker74db1572015-10-28 12:44:49 -05002025 gui_msg(Msg("backing_up=Backing up {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002026
2027 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
Dees_Troy83bd4832013-05-04 12:39:56 +00002028 tar.use_compression = use_compression;
Matt Mowerbb81e5d2014-03-20 18:05:41 -05002029
Dees_Troy83bd4832013-05-04 12:39:56 +00002030#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
2031 DataManager::GetValue("tw_encrypt_backup", use_encryption);
2032 if (use_encryption && Can_Encrypt_Backup) {
2033 tar.use_encryption = use_encryption;
2034 if (Use_Userdata_Encryption)
2035 tar.userdata_encryption = use_encryption;
Ethan Yonker87af5632014-02-10 11:56:35 -06002036 string Password;
2037 DataManager::GetValue("tw_backup_password", Password);
2038 tar.setpassword(Password);
Dees_Troy83bd4832013-05-04 12:39:56 +00002039 } else {
2040 use_encryption = false;
2041 }
2042#endif
Dees_Troy43d8b002012-09-17 16:00:01 -04002043
2044 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
2045 Backup_FileName = back_name;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002046 Full_FileName = backup_folder + "/" + Backup_FileName;
Dees_Troy83bd4832013-05-04 12:39:56 +00002047 tar.has_data_media = Has_Data_Media;
Dees Troye0a433a2013-12-02 04:10:37 +00002048 Full_FileName = backup_folder + "/" + Backup_FileName;
2049 tar.setdir(Backup_Path);
2050 tar.setfn(Full_FileName);
2051 tar.setsize(Backup_Size);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002052 tar.partition_name = Backup_Name;
2053 tar.backup_folder = backup_folder;
bigbiff7abc5fe2015-01-17 16:53:12 -05002054 if (tar.createTarFork(overall_size, other_backups_size, tar_fork_pid) != 0)
Dees Troye0a433a2013-12-02 04:10:37 +00002055 return false;
Dees_Troy43d8b002012-09-17 16:00:01 -04002056 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002057}
2058
2059bool TWPartition::Backup_DD(string backup_folder) {
codelover352b75e2015-03-29 02:44:07 +08002060 char back_name[255], block_size[32], dd_count[32];
2061 string Full_FileName, Command, DD_BS, DD_COUNT;
Dees_Troy43d8b002012-09-17 16:00:01 -04002062 int use_compression;
codelover352b75e2015-03-29 02:44:07 +08002063 unsigned long long DD_Block_Size, DD_Count;
Dees_Troy43d8b002012-09-17 16:00:01 -04002064
codelover352b75e2015-03-29 02:44:07 +08002065 DD_Block_Size = 16 * 1024 * 1024;
2066 while (Backup_Size % DD_Block_Size != 0) DD_Block_Size >>= 1;
2067
2068 DD_Count = Backup_Size / DD_Block_Size;
2069
2070 sprintf(dd_count, "%llu", DD_Count);
2071 DD_COUNT = dd_count;
2072
2073 sprintf(block_size, "%llu", DD_Block_Size);
2074 DD_BS = block_size;
igoriok87e3d932013-01-31 21:03:53 +02002075
Ethan Yonker74db1572015-10-28 12:44:49 -05002076 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, gui_parse_text("{@backing}"));
2077 gui_msg(Msg("backing_up=Backing up {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002078
2079 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
2080 Backup_FileName = back_name;
2081
2082 Full_FileName = backup_folder + "/" + Backup_FileName;
2083
codelover352b75e2015-03-29 02:44:07 +08002084 Command = "dd if=" + Actual_Block_Device + " of='" + Full_FileName + "'" + " bs=" + DD_BS + " count=" + DD_COUNT;
Dees_Troy2673cec2013-04-02 20:22:16 +00002085 LOGINFO("Backup command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02002086 TWFunc::Exec_Cmd(Command);
Ethan Yonker4b94cfd2014-12-11 10:00:45 -06002087 tw_set_default_metadata(Full_FileName.c_str());
Dees_Troyc154ac22012-10-12 15:36:47 -04002088 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002089 gui_msg(Msg(msg::kError, "backup_size=Backup file size for '{1}' is 0 bytes.")(Full_FileName));
Dees_Troy7c2dec82012-09-26 09:49:14 -04002090 return false;
2091 }
Dees_Troy43d8b002012-09-17 16:00:01 -04002092 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002093}
2094
2095bool TWPartition::Backup_Dump_Image(string backup_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04002096 char back_name[255];
Vojtech Bocek05534202013-09-11 08:11:56 +02002097 string Full_FileName, Command;
Dees_Troy43d8b002012-09-17 16:00:01 -04002098 int use_compression;
2099
Ethan Yonker74db1572015-10-28 12:44:49 -05002100 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, gui_parse_text("{@backing}"));
2101 gui_msg(Msg("backing_up=Backing up {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002102
2103 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
2104 Backup_FileName = back_name;
2105
2106 Full_FileName = backup_folder + "/" + Backup_FileName;
2107
2108 Command = "dump_image " + MTD_Name + " '" + Full_FileName + "'";
Dees_Troy2673cec2013-04-02 20:22:16 +00002109 LOGINFO("Backup command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02002110 TWFunc::Exec_Cmd(Command);
Ethan Yonker4b94cfd2014-12-11 10:00:45 -06002111 tw_set_default_metadata(Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04002112 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
2113 // 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 -06002114 gui_msg(Msg(msg::kError, "backup_size=Backup file size for '{1}' is 0 bytes.")(Full_FileName));
Dees_Troy7c2dec82012-09-26 09:49:14 -04002115 return false;
2116 }
Dees_Troy43d8b002012-09-17 16:00:01 -04002117 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002118}
2119
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002120unsigned long long TWPartition::Get_Restore_Size(string restore_folder) {
2121 InfoManager restore_info(restore_folder + "/" + Backup_Name + ".info");
2122 if (restore_info.LoadValues() == 0) {
2123 if (restore_info.GetValue("backup_size", Restore_Size) == 0) {
2124 LOGINFO("Read info file, restore size is %llu\n", Restore_Size);
2125 return Restore_Size;
2126 }
2127 }
2128 string Full_FileName, Restore_File_System = Get_Restore_File_System(restore_folder);
2129
2130 Full_FileName = restore_folder + "/" + Backup_FileName;
2131
2132 if (Is_Image(Restore_File_System)) {
2133 Restore_Size = TWFunc::Get_File_Size(Full_FileName);
2134 return Restore_Size;
2135 }
2136
2137 twrpTar tar;
2138 tar.setdir(Backup_Path);
2139 tar.setfn(Full_FileName);
2140 tar.backup_name = Backup_Name;
2141#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
2142 string Password;
2143 DataManager::GetValue("tw_restore_password", Password);
2144 if (!Password.empty())
2145 tar.setpassword(Password);
2146#endif
2147 tar.partition_name = Backup_Name;
2148 tar.backup_folder = restore_folder;
2149 Restore_Size = tar.get_size();
2150 return Restore_Size;
2151}
2152
2153bool TWPartition::Restore_Tar(string restore_folder, string Restore_File_System, const unsigned long long *total_restore_size, unsigned long long *already_restored_size) {
Gary Peck43acadf2012-11-21 21:19:01 -08002154 string Full_FileName, Command;
Dees_Troy4a2a1262012-09-18 09:33:47 -04002155 int index = 0;
2156 char split_index[5];
Dees Troy4159aed2014-02-28 17:24:43 +00002157 bool ret = false;
Dees_Troy43d8b002012-09-17 16:00:01 -04002158
Dees_Troye58d5262012-09-21 12:27:57 -04002159 if (Has_Android_Secure) {
Dees_Troye58d5262012-09-21 12:27:57 -04002160 if (!Wipe_AndSec())
2161 return false;
Gary Peck43acadf2012-11-21 21:19:01 -08002162 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002163 gui_msg(Msg("wiping=Wiping {1}")(Backup_Display_Name));
Ethan Yonker5eac2222014-06-11 12:22:55 -05002164 if (Has_Data_Media && Mount_Point == "/data" && Restore_File_System != Current_File_System) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002165 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 -05002166 if (!Wipe_Data_Without_Wiping_Media())
2167 return false;
2168 } else {
2169 if (!Wipe(Restore_File_System))
2170 return false;
2171 }
Dees_Troye58d5262012-09-21 12:27:57 -04002172 }
Matt Mower3c366972015-12-25 19:28:31 -06002173 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Backup_Display_Name, gui_parse_text("{@restoring_hdr}"));
Ethan Yonker74db1572015-10-28 12:44:49 -05002174 gui_msg(Msg("restoring=Restoring {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002175
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05002176 // Remount as read/write as needed so we can restore the backup
2177 if (!ReMount_RW(true))
Dees_Troy43d8b002012-09-17 16:00:01 -04002178 return false;
2179
Dees_Troy4a2a1262012-09-18 09:33:47 -04002180 Full_FileName = restore_folder + "/" + Backup_FileName;
Ethan Yonker87af5632014-02-10 11:56:35 -06002181 twrpTar tar;
2182 tar.setdir(Backup_Path);
2183 tar.setfn(Full_FileName);
2184 tar.backup_name = Backup_Name;
2185#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
2186 string Password;
2187 DataManager::GetValue("tw_restore_password", Password);
2188 if (!Password.empty())
2189 tar.setpassword(Password);
2190#endif
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002191 if (tar.extractTarFork(total_restore_size, already_restored_size) != 0)
Dees Troy4159aed2014-02-28 17:24:43 +00002192 ret = false;
2193 else
2194 ret = true;
2195#ifdef HAVE_CAPABILITIES
2196 // Restore capabilities to the run-as binary
2197 if (Mount_Point == "/system" && Mount(true) && TWFunc::Path_Exists("/system/bin/run-as")) {
2198 struct vfs_cap_data cap_data;
2199 uint64_t capabilities = (1 << CAP_SETUID) | (1 << CAP_SETGID);
2200
2201 memset(&cap_data, 0, sizeof(cap_data));
2202 cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE;
2203 cap_data.data[0].permitted = (uint32_t) (capabilities & 0xffffffff);
2204 cap_data.data[0].inheritable = 0;
2205 cap_data.data[1].permitted = (uint32_t) (capabilities >> 32);
2206 cap_data.data[1].inheritable = 0;
2207 if (setxattr("/system/bin/run-as", XATTR_NAME_CAPS, &cap_data, sizeof(cap_data), 0) < 0) {
2208 LOGINFO("Failed to reset capabilities of /system/bin/run-as binary.\n");
2209 } else {
2210 LOGINFO("Reset capabilities of /system/bin/run-as binary successful.\n");
2211 }
2212 }
2213#endif
James Christopher Adduonod6f94ac2016-02-29 04:26:04 -05002214 if (Mount_Read_Only || Mount_Flags & MS_RDONLY)
2215 // Remount as read only when restoration is complete
2216 ReMount(true);
2217
Dees Troy4159aed2014-02-28 17:24:43 +00002218 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04002219}
2220
Ethan Yonker96af84a2015-01-05 14:58:36 -06002221bool TWPartition::Restore_Image(string restore_folder, const unsigned long long *total_restore_size, unsigned long long *already_restored_size, string Restore_File_System) {
2222 string Full_FileName;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002223 double display_percent, progress_percent;
2224 char size_progress[1024];
Dees_Troy43d8b002012-09-17 16:00:01 -04002225
Matt Mower3c366972015-12-25 19:28:31 -06002226 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Backup_Display_Name, gui_parse_text("{@restoring_hdr}"));
Ethan Yonker74db1572015-10-28 12:44:49 -05002227 gui_msg(Msg("restoring=Restoring {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002228 Full_FileName = restore_folder + "/" + Backup_FileName;
Gary Peck15e623d2012-11-21 21:07:58 -08002229
Ethan Yonker96af84a2015-01-05 14:58:36 -06002230 if (Restore_File_System == "emmc") {
2231 if (!Flash_Image_DD(Full_FileName))
2232 return false;
2233 } else if (Restore_File_System == "mtd" || Restore_File_System == "bml") {
2234 if (!Flash_Image_FI(Full_FileName))
2235 return false;
Gary Peck15e623d2012-11-21 21:07:58 -08002236 }
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002237 display_percent = (double)(Restore_Size + *already_restored_size) / (double)(*total_restore_size) * 100;
2238 sprintf(size_progress, "%lluMB of %lluMB, %i%%", (Restore_Size + *already_restored_size) / 1048576, *total_restore_size / 1048576, (int)(display_percent));
2239 DataManager::SetValue("tw_size_progress", size_progress);
2240 progress_percent = (display_percent / 100);
2241 DataManager::SetProgress((float)(progress_percent));
2242 *already_restored_size += Restore_Size;
Dees_Troy43d8b002012-09-17 16:00:01 -04002243 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002244}
Dees_Troy5bf43922012-09-07 16:07:55 -04002245
2246bool TWPartition::Update_Size(bool Display_Error) {
Dees_Troy0550cfb2012-10-13 11:56:13 -04002247 bool ret = false, Was_Already_Mounted = false;
Dees_Troy51127312012-09-08 13:08:49 -04002248
Dees_Troyab10ee22012-09-21 14:27:30 -04002249 if (!Can_Be_Mounted && !Is_Encrypted)
Dees_Troy5bf43922012-09-07 16:07:55 -04002250 return false;
2251
Dees_Troy0550cfb2012-10-13 11:56:13 -04002252 Was_Already_Mounted = Is_Mounted();
Dees_Troy38bd7602012-09-14 13:33:53 -04002253 if (Removable || Is_Encrypted) {
2254 if (!Mount(false))
2255 return true;
2256 } else if (!Mount(Display_Error))
Dees_Troy5bf43922012-09-07 16:07:55 -04002257 return false;
Dees_Troy51127312012-09-08 13:08:49 -04002258
2259 ret = Get_Size_Via_statfs(Display_Error);
Dees_Troy0550cfb2012-10-13 11:56:13 -04002260 if (!ret || Size == 0) {
2261 if (!Get_Size_Via_df(Display_Error)) {
2262 if (!Was_Already_Mounted)
2263 UnMount(false);
Dees_Troy51127312012-09-08 13:08:49 -04002264 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04002265 }
2266 }
Dees_Troy51127312012-09-08 13:08:49 -04002267
Dees_Troy5bf43922012-09-07 16:07:55 -04002268 if (Has_Data_Media) {
2269 if (Mount(Display_Error)) {
Dees_Troy51127312012-09-08 13:08:49 -04002270 unsigned long long data_media_used, actual_data;
Ethan Yonker66a19492015-12-10 10:19:45 -06002271 Used = du.Get_Folder_Size(Mount_Point);
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002272 Backup_Size = Used;
2273 int bak = (int)(Used / 1048576LLU);
Dees_Troy51127312012-09-08 13:08:49 -04002274 int fre = (int)(Free / 1048576LLU);
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002275 LOGINFO("Data backup size is %iMB, free: %iMB.\n", bak, fre);
Dees_Troy0550cfb2012-10-13 11:56:13 -04002276 } else {
2277 if (!Was_Already_Mounted)
2278 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04002279 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04002280 }
Dees_Troye58d5262012-09-21 12:27:57 -04002281 } else if (Has_Android_Secure) {
2282 if (Mount(Display_Error))
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002283 Backup_Size = du.Get_Folder_Size(Backup_Path);
Dees_Troy0550cfb2012-10-13 11:56:13 -04002284 else {
2285 if (!Was_Already_Mounted)
2286 UnMount(false);
Dees_Troye58d5262012-09-21 12:27:57 -04002287 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04002288 }
Dees_Troy5bf43922012-09-07 16:07:55 -04002289 }
Dees_Troy0550cfb2012-10-13 11:56:13 -04002290 if (!Was_Already_Mounted)
2291 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04002292 return true;
Dees_Troy51127312012-09-08 13:08:49 -04002293}
Dees_Troy38bd7602012-09-14 13:33:53 -04002294
2295void TWPartition::Find_Actual_Block_Device(void) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002296 if (Is_Decrypted && !Decrypted_Block_Device.empty()) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002297 Actual_Block_Device = Decrypted_Block_Device;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002298 if (TWFunc::Path_Exists(Decrypted_Block_Device))
Dees_Troy38bd7602012-09-14 13:33:53 -04002299 Is_Present = true;
Dees_Troy43d8b002012-09-17 16:00:01 -04002300 } else if (TWFunc::Path_Exists(Primary_Block_Device)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002301 Is_Present = true;
2302 Actual_Block_Device = Primary_Block_Device;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002303 return;
2304 }
2305 if (Is_Decrypted) {
Dees_Troy43d8b002012-09-17 16:00:01 -04002306 } else if (!Alternate_Block_Device.empty() && TWFunc::Path_Exists(Alternate_Block_Device)) {
Dees_Troy3f04d032012-10-07 18:20:09 -04002307 Actual_Block_Device = Alternate_Block_Device;
Dees_Troy38bd7602012-09-14 13:33:53 -04002308 Is_Present = true;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002309 } else {
Dees_Troy38bd7602012-09-14 13:33:53 -04002310 Is_Present = false;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002311 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002312}
2313
2314void TWPartition::Recreate_Media_Folder(void) {
2315 string Command;
Ethan Yonker66a19492015-12-10 10:19:45 -06002316 string Media_Path = Mount_Point + "/media";
Dees_Troy38bd7602012-09-14 13:33:53 -04002317
2318 if (!Mount(true)) {
Ethan Yonker66a19492015-12-10 10:19:45 -06002319 gui_msg(Msg(msg::kError, "recreate_folder_err=Unable to recreate {1} folder.")(Media_Path));
2320 } else if (!TWFunc::Path_Exists(Media_Path)) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002321 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Ethan Yonker66a19492015-12-10 10:19:45 -06002322 LOGINFO("Recreating %s folder.\n", Media_Path.c_str());
2323 mkdir(Media_Path.c_str(), 0770);
xiaolu9416f4f2015-06-04 08:22:23 +08002324 string Internal_path = DataManager::GetStrValue("tw_internal_path");
2325 if (!Internal_path.empty()) {
2326 LOGINFO("Recreating %s folder.\n", Internal_path.c_str());
2327 mkdir(Internal_path.c_str(), 0770);
2328 }
2329#ifdef TW_INTERNAL_STORAGE_PATH
2330 mkdir(EXPAND(TW_INTERNAL_STORAGE_PATH), 0770);
2331#endif
Ethan Yonker5ef301e2014-10-14 10:04:44 -05002332#ifdef HAVE_SELINUX
thata3d31fb2014-12-21 22:27:40 +01002333 // Afterwards, we will try to set the
2334 // default metadata that we were hopefully able to get during
2335 // early boot.
Ethan Yonker66a19492015-12-10 10:19:45 -06002336 tw_set_default_metadata(Media_Path.c_str());
xiaolu9416f4f2015-06-04 08:22:23 +08002337 if (!Internal_path.empty())
2338 tw_set_default_metadata(Internal_path.c_str());
Ethan Yonker5ef301e2014-10-14 10:04:44 -05002339#endif
Ethan Yonker5eac2222014-06-11 12:22:55 -05002340 // Toggle mount to ensure that "internal sdcard" gets mounted
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002341 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Ethan Yonker5eac2222014-06-11 12:22:55 -05002342 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Dees_Troy38bd7602012-09-14 13:33:53 -04002343 }
Dees_Troy43d8b002012-09-17 16:00:01 -04002344}
Dees_Troye58d5262012-09-21 12:27:57 -04002345
2346void TWPartition::Recreate_AndSec_Folder(void) {
Dees_Troye58d5262012-09-21 12:27:57 -04002347 if (!Has_Android_Secure)
2348 return;
Dees_Troy2673cec2013-04-02 20:22:16 +00002349 LOGINFO("Creating %s: %s\n", Backup_Display_Name.c_str(), Symlink_Path.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -04002350 if (!Mount(true)) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002351 gui_msg(Msg(msg::kError, "recreate_folder_err=Unable to recreate {1} folder.")(Backup_Name));
Dees_Troye58d5262012-09-21 12:27:57 -04002352 } else if (!TWFunc::Path_Exists(Symlink_Path)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00002353 LOGINFO("Recreating %s folder.\n", Backup_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002354 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05002355 mkdir(Symlink_Path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002356 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04002357 }
2358}
bigbiff7cb4c332014-11-26 20:36:07 -05002359
2360uint64_t TWPartition::Get_Max_FileSize() {
2361 uint64_t maxFileSize = 0;
2362 const uint64_t constGB = (uint64_t) 1024 * 1024 * 1024;
2363 const uint64_t constTB = (uint64_t) constGB * 1024;
2364 const uint64_t constPB = (uint64_t) constTB * 1024;
2365 const uint64_t constEB = (uint64_t) constPB * 1024;
bigbiff0c532032014-12-21 13:41:26 -05002366 if (Current_File_System == "ext4")
2367 maxFileSize = 16 * constTB; //16 TB
2368 else if (Current_File_System == "vfat")
2369 maxFileSize = 4 * constGB; //4 GB
2370 else if (Current_File_System == "ntfs")
2371 maxFileSize = 256 * constTB; //256 TB
2372 else if (Current_File_System == "exfat")
2373 maxFileSize = 16 * constPB; //16 PB
2374 else if (Current_File_System == "ext3")
2375 maxFileSize = 2 * constTB; //2 TB
2376 else if (Current_File_System == "f2fs")
2377 maxFileSize = 3.94 * constTB; //3.94 TB
bigbiff7cb4c332014-11-26 20:36:07 -05002378 else
2379 maxFileSize = 100000000L;
2380 return maxFileSize - 1;
2381}
2382
Ethan Yonker96af84a2015-01-05 14:58:36 -06002383bool TWPartition::Flash_Image(string Filename) {
2384 string Restore_File_System;
2385
2386 LOGINFO("Image filename is: %s\n", Filename.c_str());
2387
2388 if (Backup_Method == FILES) {
2389 LOGERR("Cannot flash images to file systems\n");
2390 return false;
2391 } else if (!Can_Flash_Img) {
2392 LOGERR("Cannot flash images to partitions %s\n", Display_Name.c_str());
2393 return false;
2394 } else {
2395 if (!Find_Partition_Size()) {
2396 LOGERR("Unable to find partition size for '%s'\n", Mount_Point.c_str());
2397 return false;
2398 }
2399 unsigned long long image_size = TWFunc::Get_File_Size(Filename);
2400 if (image_size > Size) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002401 LOGINFO("Size (%llu bytes) of image '%s' is larger than target device '%s' (%llu bytes)\n",
Ethan Yonker96af84a2015-01-05 14:58:36 -06002402 image_size, Filename.c_str(), Actual_Block_Device.c_str(), Size);
Ethan Yonker74db1572015-10-28 12:44:49 -05002403 gui_err("img_size_err=Size of image is larger than target device");
Ethan Yonker96af84a2015-01-05 14:58:36 -06002404 return false;
2405 }
2406 if (Backup_Method == DD)
2407 return Flash_Image_DD(Filename);
2408 else if (Backup_Method == FLASH_UTILS)
2409 return Flash_Image_FI(Filename);
2410 }
2411
2412 LOGERR("Unknown flash method for '%s'\n", Mount_Point.c_str());
2413 return false;
2414}
2415
2416bool TWPartition::Flash_Image_DD(string Filename) {
2417 string Command;
2418
Ethan Yonker74db1572015-10-28 12:44:49 -05002419 gui_msg(Msg("flashing=Flashing {1}...")(Display_Name));
HashBanged974bb2016-01-30 14:20:09 -05002420
2421 uint32_t magic = 0;
2422 int fd = open(Filename.c_str(), O_RDONLY);
2423 if (fd < 0) {
2424 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Filename)(strerror(errno)));
2425 return false;
2426 }
2427 if (read(fd, &magic, sizeof(magic)) != sizeof(magic)) {
2428 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Filename)(strerror(errno)));
2429 close(fd);
2430 return false;
2431 }
2432 close(fd);
2433 if (magic == SPARSE_HEADER_MAGIC) {
2434 Command = "simg2img '" + Filename + "' " + Actual_Block_Device;
2435 } else {
2436 Command = "dd bs=8388608 if='" + Filename + "' of=" + Actual_Block_Device;
2437 }
Ethan Yonker96af84a2015-01-05 14:58:36 -06002438 LOGINFO("Flash command: '%s'\n", Command.c_str());
2439 TWFunc::Exec_Cmd(Command);
2440 return true;
2441}
2442
2443bool TWPartition::Flash_Image_FI(string Filename) {
2444 string Command;
2445
Ethan Yonker74db1572015-10-28 12:44:49 -05002446 gui_msg(Msg("flashing=Flashing {1}...")(Display_Name));
Ethan Yonker96af84a2015-01-05 14:58:36 -06002447 // Sometimes flash image doesn't like to flash due to the first 2KB matching, so we erase first to ensure that it flashes
2448 Command = "erase_image " + MTD_Name;
2449 LOGINFO("Erase command: '%s'\n", Command.c_str());
2450 TWFunc::Exec_Cmd(Command);
2451 Command = "flash_image " + MTD_Name + " '" + Filename + "'";
2452 LOGINFO("Flash command: '%s'\n", Command.c_str());
2453 TWFunc::Exec_Cmd(Command);
2454 return true;
2455}
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05002456
2457void TWPartition::Change_Mount_Read_Only(bool new_value) {
2458 Mount_Read_Only = new_value;
2459}
2460
2461int TWPartition::Check_Lifetime_Writes() {
2462 bool original_read_only = Mount_Read_Only;
2463 int ret = 1;
2464
2465 Mount_Read_Only = true;
2466 if (Mount(false)) {
2467 Find_Actual_Block_Device();
2468 string block = basename(Actual_Block_Device.c_str());
2469 string file = "/sys/fs/" + Current_File_System + "/" + block + "/lifetime_write_kbytes";
2470 string result;
2471 if (TWFunc::Path_Exists(file)) {
2472 if (TWFunc::read_file(file, result) != 0) {
2473 LOGINFO("Check_Lifetime_Writes of '%s' failed to read_file\n", file.c_str());
2474 } else {
2475 LOGINFO("Check_Lifetime_Writes result: '%s'\n", result.c_str());
2476 if (result == "0") {
2477 ret = 0;
2478 }
2479 }
2480 } else {
2481 LOGINFO("Check_Lifetime_Writes file does not exist '%s'\n", file.c_str());
2482 }
2483 UnMount(true);
2484 } else {
2485 LOGINFO("Check_Lifetime_Writes failed to mount '%s'\n", Mount_Point.c_str());
2486 }
2487 Mount_Read_Only = original_read_only;
2488 return ret;
2489}
Ethan Yonker66a19492015-12-10 10:19:45 -06002490
2491int TWPartition::Decrypt_Adopted() {
2492#ifdef TW_INCLUDE_CRYPTO
2493 int ret = 1;
2494 Is_Adopted_Storage = false;
2495 string Adopted_Key_File = "";
2496
2497 if (!Removable)
2498 return ret;
2499
2500 int fd = open(Alternate_Block_Device.c_str(), O_RDONLY);
2501 if (fd < 0) {
2502 LOGINFO("failed to open '%s'\n", Alternate_Block_Device.c_str());
2503 return ret;
2504 }
2505 char type_guid[80];
2506 char part_guid[80];
2507
2508 if (gpt_disk_get_partition_info(fd, 2, type_guid, part_guid) == 0) {
2509 LOGINFO("type: '%s'\n", type_guid);
2510 LOGINFO("part: '%s'\n", part_guid);
2511 Adopted_GUID = part_guid;
2512 LOGINFO("Adopted_GUID '%s'\n", Adopted_GUID.c_str());
2513 if (strcmp(type_guid, TWGptAndroidExpand) == 0) {
2514 LOGINFO("android_expand found\n");
2515 Adopted_Key_File = "/data/misc/vold/expand_";
2516 Adopted_Key_File += part_guid;
2517 Adopted_Key_File += ".key";
2518 if (TWFunc::Path_Exists(Adopted_Key_File)) {
2519 Is_Adopted_Storage = true;
2520 /* Until we find a use case for this, I think it is safe
2521 * to disable USB Mass Storage whenever adopted storage
2522 * is present.
2523 */
2524 LOGINFO("Detected adopted storage, disabling USB mass storage mode\n");
2525 DataManager::SetValue("tw_has_usb_storage", 0);
2526 }
2527 }
2528 }
2529
2530 if (Is_Adopted_Storage) {
2531 string Adopted_Block_Device = Alternate_Block_Device + "p2";
2532 if (!TWFunc::Path_Exists(Adopted_Block_Device)) {
2533 Adopted_Block_Device = Alternate_Block_Device + "2";
2534 if (!TWFunc::Path_Exists(Adopted_Block_Device)) {
2535 LOGINFO("Adopted block device does not exist\n");
2536 goto exit;
2537 }
2538 }
2539 LOGINFO("key file is '%s', block device '%s'\n", Adopted_Key_File.c_str(), Adopted_Block_Device.c_str());
2540 char crypto_blkdev[MAXPATHLEN];
2541 std::string thekey;
2542 int fdkey = open(Adopted_Key_File.c_str(), O_RDONLY);
2543 if (fdkey < 0) {
2544 LOGINFO("failed to open key file\n");
2545 goto exit;
2546 }
2547 char buf[512];
2548 ssize_t n;
2549 while ((n = read(fdkey, &buf[0], sizeof(buf))) > 0) {
2550 thekey.append(buf, n);
2551 }
2552 close(fdkey);
2553 unsigned char* key = (unsigned char*) thekey.data();
2554 cryptfs_revert_ext_volume(part_guid);
2555
2556 ret = cryptfs_setup_ext_volume(part_guid, Adopted_Block_Device.c_str(), key, thekey.size(), crypto_blkdev);
2557 if (ret == 0) {
2558 LOGINFO("adopted storage new block device: '%s'\n", crypto_blkdev);
2559 Decrypted_Block_Device = crypto_blkdev;
2560 Is_Decrypted = true;
2561 Is_Encrypted = true;
2562 Find_Actual_Block_Device();
2563 if (!Mount(false)) {
2564 LOGERR("Failed to mount decrypted adopted storage device\n");
2565 Is_Decrypted = false;
2566 Is_Encrypted = false;
2567 cryptfs_revert_ext_volume(part_guid);
2568 ret = 1;
2569 } else {
Ethan Yonkerfcf3f242016-02-16 12:30:26 -06002570 UnMount(false);
2571 Has_Android_Secure = false;
2572 Symlink_Path = "";
2573 Symlink_Mount_Point = "";
2574 Backup_Name = Mount_Point.substr(1);
2575 Backup_Path = Mount_Point;
2576 TWPartition* sdext = PartitionManager.Find_Partition_By_Path("/sd-ext");
2577 if (sdext && sdext->Actual_Block_Device == Adopted_Block_Device) {
2578 LOGINFO("Removing /sd-ext from partition list due to adopted storage\n");
2579 PartitionManager.Remove_Partition_By_Path("/sd-ext");
2580 }
Ethan Yonker66a19492015-12-10 10:19:45 -06002581 Setup_Data_Media();
2582 Recreate_Media_Folder();
2583 Wipe_Available_in_GUI = true;
2584 Wipe_During_Factory_Reset = true;
2585 Can_Be_Backed_Up = true;
2586 Can_Encrypt_Backup = true;
2587 Use_Userdata_Encryption = true;
2588 Is_Storage = true;
2589 Storage_Name = "Adopted Storage";
2590 Is_SubPartition = true;
2591 SubPartition_Of = "/data";
2592 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
2593 DataManager::SetValue("tw_has_adopted_storage", 1);
2594 }
2595 } else {
2596 LOGERR("Failed to setup adopted storage decryption\n");
2597 }
2598 }
2599exit:
2600 return ret;
2601#else
2602 LOGINFO("Decrypt_Adopted: no crypto support\n");
2603 return 1;
2604#endif
2605}
2606
2607void TWPartition::Revert_Adopted() {
2608#ifdef TW_INCLUDE_CRYPTO
2609 if (!Adopted_GUID.empty()) {
2610 PartitionManager.Remove_MTP_Storage(Mount_Point);
2611 UnMount(false);
2612 cryptfs_revert_ext_volume(Adopted_GUID.c_str());
2613 Is_Adopted_Storage = false;
2614 Is_Encrypted = false;
2615 Is_Decrypted = false;
2616 Decrypted_Block_Device = "";
2617 Find_Actual_Block_Device();
2618 Wipe_During_Factory_Reset = false;
2619 Can_Be_Backed_Up = false;
2620 Can_Encrypt_Backup = false;
2621 Use_Userdata_Encryption = false;
2622 Is_SubPartition = false;
2623 SubPartition_Of = "";
2624 Has_Data_Media = false;
2625 Storage_Path = Mount_Point;
2626 if (!Symlink_Mount_Point.empty()) {
2627 TWPartition* Dat = PartitionManager.Find_Partition_By_Path("/data");
2628 if (Dat) {
2629 Dat->UnMount(false);
2630 Dat->Symlink_Mount_Point = Symlink_Mount_Point;
2631 }
2632 Symlink_Mount_Point = "";
2633 }
2634 }
2635#else
2636 LOGINFO("Revert_Adopted: no crypto support\n");
2637#endif
2638}