blob: d038c38f106137e6e5a94ba07b37078a972cf070 [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
Dees_Troy51a0e822012-09-05 15:24:24 -040071
bigbiff bigbiff9c754052013-01-09 09:09:08 -050072using namespace std;
73
Dees_Troya95f55c2013-08-17 13:14:43 +000074extern struct selabel_handle *selinux_handle;
Ethan Yonker6277c792014-09-15 14:54:30 -050075extern bool datamedia;
Dees_Troya95f55c2013-08-17 13:14:43 +000076
Hashcode62bd9e02013-11-19 21:59:42 -080077struct flag_list {
78 const char *name;
79 unsigned flag;
80};
81
82static struct flag_list mount_flags[] = {
83 { "noatime", MS_NOATIME },
84 { "noexec", MS_NOEXEC },
85 { "nosuid", MS_NOSUID },
86 { "nodev", MS_NODEV },
87 { "nodiratime", MS_NODIRATIME },
88 { "ro", MS_RDONLY },
89 { "rw", 0 },
90 { "remount", MS_REMOUNT },
91 { "bind", MS_BIND },
92 { "rec", MS_REC },
Dees Troyc4bc30e2014-02-03 15:04:19 +000093#ifdef MS_UNBINDABLE
Hashcode62bd9e02013-11-19 21:59:42 -080094 { "unbindable", MS_UNBINDABLE },
Dees Troyc4bc30e2014-02-03 15:04:19 +000095#endif
96#ifdef MS_PRIVATE
Hashcode62bd9e02013-11-19 21:59:42 -080097 { "private", MS_PRIVATE },
Dees Troyc4bc30e2014-02-03 15:04:19 +000098#endif
99#ifdef MS_SLAVE
Hashcode62bd9e02013-11-19 21:59:42 -0800100 { "slave", MS_SLAVE },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000101#endif
102#ifdef MS_SHARED
Hashcode62bd9e02013-11-19 21:59:42 -0800103 { "shared", MS_SHARED },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000104#endif
Hashcode62bd9e02013-11-19 21:59:42 -0800105 { "sync", MS_SYNCHRONOUS },
106 { "defaults", 0 },
107 { 0, 0 },
108};
109
that9e0593e2014-10-08 00:01:24 +0200110TWPartition::TWPartition() {
Dees_Troy51a0e822012-09-05 15:24:24 -0400111 Can_Be_Mounted = false;
112 Can_Be_Wiped = false;
Dees_Troya13d74f2013-03-24 08:54:55 -0500113 Can_Be_Backed_Up = false;
Vojtech Bocek1dc30982013-08-30 21:49:30 +0200114 Use_Rm_Rf = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400115 Wipe_During_Factory_Reset = false;
116 Wipe_Available_in_GUI = false;
117 Is_SubPartition = false;
Dees_Troy2691f9d2012-09-24 11:15:49 -0400118 Has_SubPartition = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400119 SubPartition_Of = "";
120 Symlink_Path = "";
121 Symlink_Mount_Point = "";
122 Mount_Point = "";
Dees_Troye58d5262012-09-21 12:27:57 -0400123 Backup_Path = "";
Dees_Troy38bd7602012-09-14 13:33:53 -0400124 Actual_Block_Device = "";
125 Primary_Block_Device = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400126 Alternate_Block_Device = "";
127 Removable = false;
128 Is_Present = false;
129 Length = 0;
130 Size = 0;
131 Used = 0;
132 Free = 0;
133 Backup_Size = 0;
134 Can_Be_Encrypted = false;
135 Is_Encrypted = false;
136 Is_Decrypted = false;
Ethan Yonker253368a2014-11-25 15:00:52 -0600137 Mount_To_Decrypt = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400138 Decrypted_Block_Device = "";
139 Display_Name = "";
Dees_Troya13d74f2013-03-24 08:54:55 -0500140 Backup_Display_Name = "";
141 Storage_Name = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400142 Backup_Name = "";
Dees_Troy63c8df72012-09-10 14:02:05 -0400143 Backup_FileName = "";
Dees_Troy38bd7602012-09-14 13:33:53 -0400144 MTD_Name = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400145 Backup_Method = NONE;
Dees_Troy83bd4832013-05-04 12:39:56 +0000146 Can_Encrypt_Backup = false;
147 Use_Userdata_Encryption = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400148 Has_Data_Media = false;
Dees_Troye58d5262012-09-21 12:27:57 -0400149 Has_Android_Secure = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400150 Is_Storage = false;
Dees_Troya13d74f2013-03-24 08:54:55 -0500151 Is_Settings_Storage = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400152 Storage_Path = "";
153 Current_File_System = "";
154 Fstab_File_System = "";
Hashcode62bd9e02013-11-19 21:59:42 -0800155 Mount_Flags = 0;
156 Mount_Options = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400157 Format_Block_Size = 0;
Dees_Troy68cab492012-12-12 19:29:35 +0000158 Ignore_Blkid = false;
Dees_Troy16c2b312013-01-15 16:51:18 +0000159 Retain_Layout_Version = false;
Ethan Yonker253368a2014-11-25 15:00:52 -0600160 Crypto_Key_Location = "footer";
Ethan Yonker726a0202014-12-16 20:01:38 -0600161 MTP_Storage_ID = 0;
Ethan Yonker96af84a2015-01-05 14:58:36 -0600162 Can_Flash_Img = false;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500163 Mount_Read_Only = false;
Ethan Yonker66a19492015-12-10 10:19:45 -0600164 Is_Adopted_Storage = false;
165 Adopted_GUID = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400166}
167
168TWPartition::~TWPartition(void) {
169 // Do nothing
170}
171
Dees_Troy5bf43922012-09-07 16:07:55 -0400172bool TWPartition::Process_Fstab_Line(string Line, bool Display_Error) {
173 char full_line[MAX_FSTAB_LINE_LENGTH], item[MAX_FSTAB_LINE_LENGTH];
174 int line_len = Line.size(), index = 0, item_index = 0;
175 char* ptr;
Dees_Troy51127312012-09-08 13:08:49 -0400176 string Flags;
Dees_Troy5bf43922012-09-07 16:07:55 -0400177 strncpy(full_line, Line.c_str(), line_len);
Dees_Troya13d74f2013-03-24 08:54:55 -0500178 bool skip = false;
Dees_Troy5bf43922012-09-07 16:07:55 -0400179
Dees_Troy51127312012-09-08 13:08:49 -0400180 for (index = 0; index < line_len; index++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500181 if (full_line[index] == 34)
182 skip = !skip;
183 if (!skip && full_line[index] <= 32)
Dees_Troy5bf43922012-09-07 16:07:55 -0400184 full_line[index] = '\0';
Dees_Troy5bf43922012-09-07 16:07:55 -0400185 }
Dees_Troy7c2dec82012-09-26 09:49:14 -0400186 Mount_Point = full_line;
Dees_Troy2673cec2013-04-02 20:22:16 +0000187 LOGINFO("Processing '%s'\n", Mount_Point.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -0400188 Backup_Path = Mount_Point;
Dees_Troya13d74f2013-03-24 08:54:55 -0500189 Storage_Path = Mount_Point;
Dees_Troy70737fa2013-04-08 13:19:20 +0000190 Display_Name = full_line + 1;
191 Backup_Display_Name = Display_Name;
192 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400193 index = Mount_Point.size();
194 while (index < line_len) {
195 while (index < line_len && full_line[index] == '\0')
196 index++;
197 if (index >= line_len)
198 continue;
199 ptr = full_line + index;
200 if (item_index == 0) {
201 // File System
202 Fstab_File_System = ptr;
203 Current_File_System = ptr;
204 item_index++;
205 } else if (item_index == 1) {
206 // Primary Block Device
Dees_Troy38bd7602012-09-14 13:33:53 -0400207 if (Fstab_File_System == "mtd" || Fstab_File_System == "yaffs2") {
Dees_Troy094207a2012-09-26 12:00:39 -0400208 MTD_Name = ptr;
209 Find_MTD_Block_Device(MTD_Name);
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400210 } else if (Fstab_File_System == "bml") {
211 if (Mount_Point == "/boot")
212 MTD_Name = "boot";
213 else if (Mount_Point == "/recovery")
214 MTD_Name = "recovery";
215 Primary_Block_Device = ptr;
216 if (*ptr != '/')
Dees_Troy2673cec2013-04-02 20:22:16 +0000217 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 -0400218 } else if (*ptr != '/') {
Dees_Troy5bf43922012-09-07 16:07:55 -0400219 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000220 LOGERR("Invalid block device on '%s', '%s', %i\n", Line.c_str(), ptr, index);
Dees_Troy5bf43922012-09-07 16:07:55 -0400221 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000222 LOGINFO("Invalid block device on '%s', '%s', %i\n", Line.c_str(), ptr, index);
Dees_Troy5bf43922012-09-07 16:07:55 -0400223 return 0;
Dees_Troy38bd7602012-09-14 13:33:53 -0400224 } else {
225 Primary_Block_Device = ptr;
226 Find_Real_Block_Device(Primary_Block_Device, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400227 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400228 item_index++;
229 } else if (item_index > 1) {
230 if (*ptr == '/') {
231 // Alternate Block Device
232 Alternate_Block_Device = ptr;
233 Find_Real_Block_Device(Alternate_Block_Device, Display_Error);
234 } else if (strlen(ptr) > 7 && strncmp(ptr, "length=", 7) == 0) {
235 // Partition length
236 ptr += 7;
237 Length = atoi(ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400238 } else if (strlen(ptr) > 6 && strncmp(ptr, "flags=", 6) == 0) {
239 // Custom flags, save for later so that new values aren't overwritten by defaults
240 ptr += 6;
241 Flags = ptr;
Dees_Troy68cab492012-12-12 19:29:35 +0000242 Process_Flags(Flags, Display_Error);
Dees_Troy38bd7602012-09-14 13:33:53 -0400243 } else if (strlen(ptr) == 4 && (strncmp(ptr, "NULL", 4) == 0 || strncmp(ptr, "null", 4) == 0 || strncmp(ptr, "null", 4) == 0)) {
244 // Do nothing
Dees_Troy5bf43922012-09-07 16:07:55 -0400245 } else {
246 // Unhandled data
Dees_Troy2673cec2013-04-02 20:22:16 +0000247 LOGINFO("Unhandled fstab information: '%s', %i, line: '%s'\n", ptr, index, Line.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400248 }
249 }
250 while (index < line_len && full_line[index] != '\0')
251 index++;
252 }
253
254 if (!Is_File_System(Fstab_File_System) && !Is_Image(Fstab_File_System)) {
255 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000256 LOGERR("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400257 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000258 LOGINFO("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400259 return 0;
260 } else if (Is_File_System(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400261 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400262 Setup_File_System(Display_Error);
263 if (Mount_Point == "/system") {
264 Display_Name = "System";
Dees_Troya13d74f2013-03-24 08:54:55 -0500265 Backup_Display_Name = Display_Name;
266 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400267 Wipe_Available_in_GUI = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500268 Can_Be_Backed_Up = true;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500269 Mount_Read_Only = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400270 } else if (Mount_Point == "/data") {
Dees Troyc657cc02015-01-16 22:48:47 +0000271 UnMount(false); // added in case /data is mounted as tmpfs for qcom hardware decrypt
Dees_Troy5bf43922012-09-07 16:07:55 -0400272 Display_Name = "Data";
Dees_Troya13d74f2013-03-24 08:54:55 -0500273 Backup_Display_Name = Display_Name;
274 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400275 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400276 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500277 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000278 Can_Encrypt_Backup = true;
279 Use_Userdata_Encryption = true;
Ethan Yonker6277c792014-09-15 14:54:30 -0500280 if (datamedia)
that9e0593e2014-10-08 00:01:24 +0200281 Setup_Data_Media();
Dees_Troy5bf43922012-09-07 16:07:55 -0400282#ifdef TW_INCLUDE_CRYPTO
283 Can_Be_Encrypted = true;
Dees_Troy657c3092012-09-10 20:32:10 -0400284 char crypto_blkdev[255];
285 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
286 if (strcmp(crypto_blkdev, "error") != 0) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400287 DataManager::SetValue(TW_DATA_BLK_DEVICE, Primary_Block_Device);
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;
410 Can_Flash_Img = false;
411 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;
415 Can_Flash_Img = false;
416 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
986bool TWPartition::Mount(bool Display_Error) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000987 int exfat_mounted = 0;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500988 unsigned long flags = Mount_Flags;
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000989
Dees_Troy5bf43922012-09-07 16:07:55 -0400990 if (Is_Mounted()) {
991 return true;
992 } else if (!Can_Be_Mounted) {
993 return false;
994 }
Dees_Troy38bd7602012-09-14 13:33:53 -0400995
996 Find_Actual_Block_Device();
997
998 // Check the current file system before mounting
999 Check_FS_Type();
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001000 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/exfat-fuse")) {
Dees_Troye34c1332013-02-06 19:13:00 +00001001 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 +00001002 LOGINFO("cmd: %s\n", cmd.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001003 string result;
1004 if (TWFunc::Exec_Cmd(cmd, result) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001005 LOGINFO("exfat-fuse failed to mount with result '%s', trying vfat\n", result.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001006 Current_File_System = "vfat";
1007 } else {
1008#ifdef TW_NO_EXFAT_FUSE
1009 UnMount(false);
1010 // We'll let the kernel handle it but using exfat-fuse to detect if the file system is actually exfat
1011 // Some kernels let us mount vfat as exfat which doesn't work out too well
1012#else
1013 exfat_mounted = 1;
1014#endif
1015 }
1016 }
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001017
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001018 if (Current_File_System == "ntfs" && (TWFunc::Path_Exists("/sbin/ntfs-3g") || TWFunc::Path_Exists("/sbin/mount.ntfs"))) {
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001019 string cmd;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001020 string Ntfsmount_Binary = "";
1021
1022 if (TWFunc::Path_Exists("/sbin/ntfs-3g"))
1023 Ntfsmount_Binary = "ntfs-3g";
1024 else if (TWFunc::Path_Exists("/sbin/mount.ntfs"))
1025 Ntfsmount_Binary = "mount.ntfs";
1026
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001027 if (Mount_Read_Only)
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001028 cmd = "/sbin/" + Ntfsmount_Binary + " -o ro " + Actual_Block_Device + " " + Mount_Point;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001029 else
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001030 cmd = "/sbin/" + Ntfsmount_Binary + " " + Actual_Block_Device + " " + Mount_Point;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001031 LOGINFO("cmd: '%s'\n", cmd.c_str());
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001032
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001033 if (TWFunc::Exec_Cmd(cmd) == 0) {
1034 return true;
1035 } else {
1036 LOGINFO("ntfs-3g failed to mount, trying regular mount method.\n");
1037 }
1038 }
1039
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001040 if (Mount_Read_Only)
1041 flags |= MS_RDONLY;
1042
Dees_Troy22042032012-12-18 21:23:08 +00001043 if (Fstab_File_System == "yaffs2") {
1044 // mount an MTD partition as a YAFFS2 filesystem.
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001045 flags = MS_NOATIME | MS_NODEV | MS_NODIRATIME;
1046 if (Mount_Read_Only)
1047 flags |= MS_RDONLY;
Dees_Troy76543db2013-06-19 16:24:30 +00001048 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags, NULL) < 0) {
1049 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags | MS_RDONLY, NULL) < 0) {
1050 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001051 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy76543db2013-06-19 16:24:30 +00001052 else
1053 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
1054 return false;
1055 } else {
1056 LOGINFO("Mounted '%s' (MTD) as RO\n", Mount_Point.c_str());
1057 return true;
1058 }
1059 } else {
1060 struct stat st;
1061 string test_path = Mount_Point;
1062 if (stat(test_path.c_str(), &st) < 0) {
1063 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001064 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy76543db2013-06-19 16:24:30 +00001065 else
1066 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
1067 return false;
1068 }
1069 mode_t new_mode = st.st_mode | S_IXUSR | S_IXGRP | S_IXOTH;
1070 if (new_mode != st.st_mode) {
1071 LOGINFO("Fixing execute permissions for %s\n", Mount_Point.c_str());
1072 if (chmod(Mount_Point.c_str(), new_mode) < 0) {
1073 if (Display_Error)
1074 LOGERR("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
1075 else
1076 LOGINFO("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
1077 return false;
1078 }
1079 }
Dees_Troy22042032012-12-18 21:23:08 +00001080 return true;
Dees_Troy76543db2013-06-19 16:24:30 +00001081 }
thatc7572eb2015-03-31 18:55:30 +02001082 }
1083
1084 string mount_fs = Current_File_System;
1085 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sys/module/texfat"))
1086 mount_fs = "texfat";
1087
1088 if (!exfat_mounted &&
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001089 mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), mount_fs.c_str(), flags, Mount_Options.c_str()) != 0 &&
1090 mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), mount_fs.c_str(), flags, NULL) != 0) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001091#ifdef TW_NO_EXFAT_FUSE
1092 if (Current_File_System == "exfat") {
Dees_Troy2673cec2013-04-02 20:22:16 +00001093 LOGINFO("Mounting exfat failed, trying vfat...\n");
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001094 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), "vfat", 0, NULL) != 0) {
Dees_Troy85f44ed2013-01-09 18:42:36 +00001095 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001096 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy85f44ed2013-01-09 18:42:36 +00001097 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001098 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001099 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 +00001100 return false;
Dees_Troy85f44ed2013-01-09 18:42:36 +00001101 }
Dees_Troyb05ddee2013-01-28 20:24:50 +00001102 } else {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001103#endif
bigbiff bigbiff26774a02014-03-29 18:22:00 -04001104 if (!Removable && Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001105 gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001106 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001107 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
1108 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 +00001109 return false;
1110#ifdef TW_NO_EXFAT_FUSE
Dees_Troy85f44ed2013-01-09 18:42:36 +00001111 }
1112#endif
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001113 }
Ethan Yonker253368a2014-11-25 15:00:52 -06001114
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001115 if (Removable)
1116 Update_Size(Display_Error);
1117
xiaolu9416f4f2015-06-04 08:22:23 +08001118 if (!Symlink_Mount_Point.empty() && TWFunc::Path_Exists(Symlink_Path)) {
codelover2a3d4ce2015-03-14 20:26:49 +08001119 string Command = "mount -o bind '" + Symlink_Path + "' '" + Symlink_Mount_Point + "'";
Vojtech Bocek05534202013-09-11 08:11:56 +02001120 TWFunc::Exec_Cmd(Command);
Dees_Troy5bf43922012-09-07 16:07:55 -04001121 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001122 return true;
1123}
1124
1125bool TWPartition::UnMount(bool Display_Error) {
1126 if (Is_Mounted()) {
1127 int never_unmount_system;
1128
1129 DataManager::GetValue(TW_DONT_UNMOUNT_SYSTEM, never_unmount_system);
1130 if (never_unmount_system == 1 && Mount_Point == "/system")
1131 return true; // Never unmount system if you're not supposed to unmount it
1132
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001133 if (Is_Storage)
Ethan Yonker726a0202014-12-16 20:01:38 -06001134 PartitionManager.Remove_MTP_Storage(MTP_Storage_ID);
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001135
Dees_Troy38bd7602012-09-14 13:33:53 -04001136 if (!Symlink_Mount_Point.empty())
1137 umount(Symlink_Mount_Point.c_str());
1138
Dees_Troyb05ddee2013-01-28 20:24:50 +00001139 umount(Mount_Point.c_str());
1140 if (Is_Mounted()) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001141 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -05001142 gui_msg(Msg(msg::kError, "fail_unmount=Failed to unmount '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy5bf43922012-09-07 16:07:55 -04001143 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001144 LOGINFO("Unable to unmount '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001145 return false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001146 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -04001147 return true;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001148 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001149 } else {
1150 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001151 }
1152}
1153
Gary Peck43acadf2012-11-21 21:19:01 -08001154bool TWPartition::Wipe(string New_File_System) {
Ethan Yonker726a0202014-12-16 20:01:38 -06001155 bool wiped = false, update_crypt = false, recreate_media = true;
Dees_Troy16c2b312013-01-15 16:51:18 +00001156 int check;
1157 string Layout_Filename = Mount_Point + "/.layout_version";
1158
Dees_Troy38bd7602012-09-14 13:33:53 -04001159 if (!Can_Be_Wiped) {
Matt Mower3c366972015-12-25 19:28:31 -06001160 gui_msg(Msg(msg::kError, "cannot_wipe=Partition {1} cannot be wiped.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04001161 return false;
1162 }
1163
Dees_Troyc51f1f92012-09-20 15:32:13 -04001164 if (Mount_Point == "/cache")
Dees_Troy2673cec2013-04-02 20:22:16 +00001165 Log_Offset = 0;
Dees_Troyc51f1f92012-09-20 15:32:13 -04001166
Dees_Troy16c2b312013-01-15 16:51:18 +00001167 if (Retain_Layout_Version && Mount(false) && TWFunc::Path_Exists(Layout_Filename))
1168 TWFunc::copy_file(Layout_Filename, "/.layout_version", 0600);
1169 else
1170 unlink("/.layout_version");
Dees_Troy38bd7602012-09-14 13:33:53 -04001171
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001172 if (Has_Data_Media && Current_File_System == New_File_System) {
Dees_Troy16c2b312013-01-15 16:51:18 +00001173 wiped = Wipe_Data_Without_Wiping_Media();
Ethan Yonker5eac2222014-06-11 12:22:55 -05001174 recreate_media = false;
Dees_Troy16c2b312013-01-15 16:51:18 +00001175 } else {
Dees_Troy16c2b312013-01-15 16:51:18 +00001176 DataManager::GetValue(TW_RM_RF_VAR, check);
1177
Hashcodedabfd492013-08-29 22:45:30 -07001178 if (check || Use_Rm_Rf)
Dees_Troy16c2b312013-01-15 16:51:18 +00001179 wiped = Wipe_RMRF();
1180 else if (New_File_System == "ext4")
1181 wiped = Wipe_EXT4();
1182 else if (New_File_System == "ext2" || New_File_System == "ext3")
1183 wiped = Wipe_EXT23(New_File_System);
1184 else if (New_File_System == "vfat")
1185 wiped = Wipe_FAT();
1186 else if (New_File_System == "exfat")
1187 wiped = Wipe_EXFAT();
1188 else if (New_File_System == "yaffs2")
1189 wiped = Wipe_MTD();
Dees_Troye5017042013-08-29 16:38:55 +00001190 else if (New_File_System == "f2fs")
1191 wiped = Wipe_F2FS();
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001192 else if (New_File_System == "ntfs")
1193 wiped = Wipe_NTFS();
Dees_Troy16c2b312013-01-15 16:51:18 +00001194 else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001195 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 +00001196 unlink("/.layout_version");
1197 return false;
1198 }
1199 update_crypt = wiped;
Gary Pecke8bc5d72012-12-21 06:45:25 -08001200 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001201
Gary Pecke8bc5d72012-12-21 06:45:25 -08001202 if (wiped) {
Dees_Troy1c1ac442013-01-17 21:42:14 +00001203 if (Mount_Point == "/cache")
1204 DataManager::Output_Version();
1205
Dees_Troy16c2b312013-01-15 16:51:18 +00001206 if (TWFunc::Path_Exists("/.layout_version") && Mount(false))
1207 TWFunc::copy_file("/.layout_version", Layout_Filename, 0600);
1208
1209 if (update_crypt) {
1210 Setup_File_System(false);
1211 if (Is_Encrypted && !Is_Decrypted) {
1212 // just wiped an encrypted partition back to its unencrypted state
1213 Is_Encrypted = false;
1214 Is_Decrypted = false;
1215 Decrypted_Block_Device = "";
1216 if (Mount_Point == "/data") {
1217 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1218 DataManager::SetValue(TW_IS_DECRYPTED, 0);
1219 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001220 }
1221 }
Ethan Yonker5eac2222014-06-11 12:22:55 -05001222
Ethan Yonker66a19492015-12-10 10:19:45 -06001223 if (Has_Data_Media && recreate_media) {
Ethan Yonker5eac2222014-06-11 12:22:55 -05001224 Recreate_Media_Folder();
1225 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001226 }
Ethan Yonker726a0202014-12-16 20:01:38 -06001227 if (Is_Storage) {
1228 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001229 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001230 return wiped;
Dees_Troy51a0e822012-09-05 15:24:24 -04001231}
1232
Gary Peck43acadf2012-11-21 21:19:01 -08001233bool TWPartition::Wipe() {
Gary Peck82599a82012-11-21 16:23:12 -08001234 if (Is_File_System(Current_File_System))
1235 return Wipe(Current_File_System);
1236 else
1237 return Wipe(Fstab_File_System);
Gary Peck43acadf2012-11-21 21:19:01 -08001238}
1239
Dees_Troye58d5262012-09-21 12:27:57 -04001240bool TWPartition::Wipe_AndSec(void) {
1241 if (!Has_Android_Secure)
1242 return false;
1243
Dees_Troye58d5262012-09-21 12:27:57 -04001244 if (!Mount(true))
1245 return false;
1246
Ethan Yonker74db1572015-10-28 12:44:49 -05001247 gui_msg(Msg("wiping=Wiping {1}")(Backup_Display_Name));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001248 TWFunc::removeDir(Mount_Point + "/.android_secure/", true);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001249 return true;
Dees_Troye58d5262012-09-21 12:27:57 -04001250}
1251
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001252bool TWPartition::Can_Repair() {
Ethan Yonkera2719152015-05-28 09:44:41 -05001253 if (Mount_Read_Only)
1254 return false;
Matt Mower18794c82015-11-11 16:22:45 -06001255 if (Current_File_System == "vfat" && TWFunc::Path_Exists("/sbin/fsck.fat"))
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001256 return true;
1257 else if ((Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") && TWFunc::Path_Exists("/sbin/e2fsck"))
1258 return true;
1259 else if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/fsck.exfat"))
1260 return true;
1261 else if (Current_File_System == "f2fs" && TWFunc::Path_Exists("/sbin/fsck.f2fs"))
1262 return true;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001263 else if (Current_File_System == "ntfs" && (TWFunc::Path_Exists("/sbin/ntfsfix") || TWFunc::Path_Exists("/sbin/fsck.ntfs")))
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001264 return true;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001265 return false;
1266}
1267
1268bool TWPartition::Repair() {
1269 string command;
1270
1271 if (Current_File_System == "vfat") {
Matt Mower18794c82015-11-11 16:22:45 -06001272 if (!TWFunc::Path_Exists("/sbin/fsck.fat")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001273 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.fat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001274 return false;
1275 }
1276 if (!UnMount(true))
1277 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001278 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.fat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001279 Find_Actual_Block_Device();
Matt Mower18794c82015-11-11 16:22:45 -06001280 command = "/sbin/fsck.fat -y " + Actual_Block_Device;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001281 LOGINFO("Repair command: %s\n", command.c_str());
1282 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001283 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001284 return true;
1285 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001286 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001287 return false;
1288 }
1289 }
1290 if (Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") {
1291 if (!TWFunc::Path_Exists("/sbin/e2fsck")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001292 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("e2fsck"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001293 return false;
1294 }
1295 if (!UnMount(true))
1296 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001297 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("e2fsck"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001298 Find_Actual_Block_Device();
Ethan Yonkera2719152015-05-28 09:44:41 -05001299 command = "/sbin/e2fsck -fp " + Actual_Block_Device;
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001300 LOGINFO("Repair command: %s\n", command.c_str());
1301 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001302 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001303 return true;
1304 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001305 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001306 return false;
1307 }
1308 }
1309 if (Current_File_System == "exfat") {
1310 if (!TWFunc::Path_Exists("/sbin/fsck.exfat")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001311 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.exfat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001312 return false;
1313 }
1314 if (!UnMount(true))
1315 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001316 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.exfat"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001317 Find_Actual_Block_Device();
1318 command = "/sbin/fsck.exfat " + Actual_Block_Device;
1319 LOGINFO("Repair command: %s\n", command.c_str());
1320 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001321 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001322 return true;
1323 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001324 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001325 return false;
1326 }
1327 }
1328 if (Current_File_System == "f2fs") {
1329 if (!TWFunc::Path_Exists("/sbin/fsck.f2fs")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001330 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.f2fs"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001331 return false;
1332 }
1333 if (!UnMount(true))
1334 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001335 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.f2fs"));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001336 Find_Actual_Block_Device();
1337 command = "/sbin/fsck.f2fs " + Actual_Block_Device;
1338 LOGINFO("Repair command: %s\n", command.c_str());
1339 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001340 gui_msg("done=Done.");
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001341 return true;
1342 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001343 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001344 return false;
1345 }
1346 }
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001347 if (Current_File_System == "ntfs") {
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001348 string Ntfsfix_Binary;
1349 if (TWFunc::Path_Exists("/sbin/ntfsfix"))
1350 Ntfsfix_Binary = "ntfsfix";
1351 else if (TWFunc::Path_Exists("/sbin/fsck.ntfs"))
1352 Ntfsfix_Binary = "fsck.ntfs";
1353 else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001354 gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("ntfsfix"));
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001355 return false;
1356 }
1357 if (!UnMount(true))
1358 return false;
Greg Wallace2d653402015-12-29 14:19:21 -05001359 gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)(Ntfsfix_Binary));
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001360 Find_Actual_Block_Device();
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001361 command = "/sbin/" + Ntfsfix_Binary + " " + Actual_Block_Device;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001362 LOGINFO("Repair command: %s\n", command.c_str());
1363 if (TWFunc::Exec_Cmd(command) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001364 gui_msg("done=Done.");
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001365 return true;
1366 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001367 gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name));
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001368 return false;
1369 }
1370 }
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001371 return false;
1372}
1373
Ethan Yonkera2719152015-05-28 09:44:41 -05001374bool TWPartition::Can_Resize() {
1375 if (Mount_Read_Only)
1376 return false;
1377 if ((Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") && TWFunc::Path_Exists("/sbin/resize2fs"))
1378 return true;
1379 return false;
1380}
1381
1382bool TWPartition::Resize() {
1383 string command;
1384
1385 if (Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") {
1386 if (!Can_Repair()) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001387 LOGINFO("Cannot resize %s because %s cannot be repaired before resizing.\n", Display_Name.c_str(), Display_Name.c_str());
1388 gui_msg(Msg(msg::kError, "cannot_resize=Cannot resize {1}.")(Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001389 return false;
1390 }
1391 if (!TWFunc::Path_Exists("/sbin/resize2fs")) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001392 LOGINFO("resize2fs does not exist! Cannot resize!\n");
1393 gui_msg(Msg(msg::kError, "cannot_resize=Cannot resize {1}.")(Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001394 return false;
1395 }
1396 // Repair will unmount so no need to do it twice
Ethan Yonker74db1572015-10-28 12:44:49 -05001397 gui_msg(Msg("repair_resize=Repairing {1} before resizing.")( Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001398 if (!Repair())
1399 return false;
Ethan Yonker74db1572015-10-28 12:44:49 -05001400 gui_msg(Msg("resizing=Resizing {1} using {2}...")(Display_Name)("resize2fs"));
Ethan Yonkera2719152015-05-28 09:44:41 -05001401 Find_Actual_Block_Device();
1402 command = "/sbin/resize2fs " + Actual_Block_Device;
1403 if (Length != 0) {
Ethan Yonkerd18a8212015-12-14 10:17:00 -06001404 unsigned long long Actual_Size = IOCTL_Get_Block_Size();
1405 if (Actual_Size == 0)
1406 return false;
Ethan Yonkera2719152015-05-28 09:44:41 -05001407
Ethan Yonkera2719152015-05-28 09:44:41 -05001408 unsigned long long Block_Count;
1409 if (Length < 0) {
1410 // Reduce overall size by this length
1411 Block_Count = (Actual_Size / 1024LLU) - ((unsigned long long)(Length * -1) / 1024LLU);
1412 } else {
1413 // This is the size, not a size reduction
1414 Block_Count = ((unsigned long long)(Length) / 1024LLU);
1415 }
1416 char temp[256];
1417 sprintf(temp, "%llu", Block_Count);
1418 command += " ";
1419 command += temp;
1420 command += "K";
1421 }
1422 LOGINFO("Resize command: %s\n", command.c_str());
1423 if (TWFunc::Exec_Cmd(command) == 0) {
1424 Update_Size(true);
Ethan Yonker74db1572015-10-28 12:44:49 -05001425 gui_msg("done=Done.");
Ethan Yonkera2719152015-05-28 09:44:41 -05001426 return true;
1427 } else {
1428 Update_Size(true);
Ethan Yonker74db1572015-10-28 12:44:49 -05001429 gui_msg(Msg(msg::kError, "unable_resize=Unable to resize {1}.")(Display_Name));
Ethan Yonkera2719152015-05-28 09:44:41 -05001430 return false;
1431 }
1432 }
1433 return false;
1434}
1435
bigbiff7abc5fe2015-01-17 16:53:12 -05001436bool TWPartition::Backup(string backup_folder, const unsigned long long *overall_size, const unsigned long long *other_backups_size, pid_t &tar_fork_pid) {
1437 if (Backup_Method == FILES) {
1438 return Backup_Tar(backup_folder, overall_size, other_backups_size, tar_fork_pid);
1439 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001440 else if (Backup_Method == DD)
1441 return Backup_DD(backup_folder);
1442 else if (Backup_Method == FLASH_UTILS)
1443 return Backup_Dump_Image(backup_folder);
Dees_Troy2673cec2013-04-02 20:22:16 +00001444 LOGERR("Unknown backup method for '%s'\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001445 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001446}
1447
Dees_Troy43d8b002012-09-17 16:00:01 -04001448bool TWPartition::Check_MD5(string restore_folder) {
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001449 string Full_Filename, md5file;
Dees_Troy43d8b002012-09-17 16:00:01 -04001450 char split_filename[512];
1451 int index = 0;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001452 twrpDigest md5sum;
Dees_Troy43d8b002012-09-17 16:00:01 -04001453
Captain Throwbackff5935f2014-09-23 16:08:34 -04001454 sync();
1455
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001456 memset(split_filename, 0, sizeof(split_filename));
Dees_Troy43d8b002012-09-17 16:00:01 -04001457 Full_Filename = restore_folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04001458 if (!TWFunc::Path_Exists(Full_Filename)) {
1459 // This is a split archive, we presume
1460 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
Dees_Troy2673cec2013-04-02 20:22:16 +00001461 LOGINFO("split_filename: %s\n", split_filename);
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001462 md5file = split_filename;
1463 md5file += ".md5";
1464 if (!TWFunc::Path_Exists(md5file)) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001465 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 -04001466 return false;
1467 }
1468 md5sum.setfn(split_filename);
Dees_Troy83bd4832013-05-04 12:39:56 +00001469 while (index < 1000) {
1470 if (TWFunc::Path_Exists(split_filename) && md5sum.verify_md5digest() != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001471 gui_msg(Msg(msg::kError, "md5_fail_match=MD5 failed to match on '{1}'.")(split_filename));
Dees_Troy43d8b002012-09-17 16:00:01 -04001472 return false;
1473 }
1474 index++;
1475 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001476 md5sum.setfn(split_filename);
Dees_Troy43d8b002012-09-17 16:00:01 -04001477 }
Dees_Troy4a2a1262012-09-18 09:33:47 -04001478 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001479 } else {
1480 // Single file archive
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001481 md5file = Full_Filename + ".md5";
1482 if (!TWFunc::Path_Exists(md5file)) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001483 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 -04001484 return false;
1485 }
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001486 md5sum.setfn(Full_Filename);
1487 if (md5sum.verify_md5digest() != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001488 gui_msg(Msg(msg::kError, "md5_fail_match=MD5 failed to match on '{1}'.")(split_filename));
Dees_Troy43d8b002012-09-17 16:00:01 -04001489 return false;
1490 } else
1491 return true;
1492 }
1493 return false;
1494}
1495
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001496bool 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 -08001497 string Restore_File_System;
1498
Matt Mower3c366972015-12-25 19:28:31 -06001499 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, gui_parse_text("{@restoring_hdr}"));
Dees_Troy2673cec2013-04-02 20:22:16 +00001500 LOGINFO("Restore filename is: %s\n", Backup_FileName.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001501
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001502 Restore_File_System = Get_Restore_File_System(restore_folder);
1503
1504 if (Is_File_System(Restore_File_System))
1505 return Restore_Tar(restore_folder, Restore_File_System, total_restore_size, already_restored_size);
1506 else if (Is_Image(Restore_File_System)) {
Ethan Yonkerf66324f2015-01-09 16:46:57 -06001507 return Restore_Image(restore_folder, total_restore_size, already_restored_size, Restore_File_System);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001508 }
1509
1510 LOGERR("Unknown restore method for '%s'\n", Mount_Point.c_str());
1511 return false;
1512}
1513
1514string TWPartition::Get_Restore_File_System(string restore_folder) {
1515 size_t first_period, second_period;
1516 string Restore_File_System;
1517
Gary Peck43acadf2012-11-21 21:19:01 -08001518 // Parse backup filename to extract the file system before wiping
1519 first_period = Backup_FileName.find(".");
1520 if (first_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001521 LOGERR("Unable to find file system (first period).\n");
thatd43bf2d2014-09-21 23:13:02 +02001522 return string();
Gary Peck43acadf2012-11-21 21:19:01 -08001523 }
1524 Restore_File_System = Backup_FileName.substr(first_period + 1, Backup_FileName.size() - first_period - 1);
1525 second_period = Restore_File_System.find(".");
1526 if (second_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001527 LOGERR("Unable to find file system (second period).\n");
thatd43bf2d2014-09-21 23:13:02 +02001528 return string();
Gary Peck43acadf2012-11-21 21:19:01 -08001529 }
1530 Restore_File_System.resize(second_period);
Dees_Troy2673cec2013-04-02 20:22:16 +00001531 LOGINFO("Restore file system is: '%s'.\n", Restore_File_System.c_str());
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001532 return Restore_File_System;
Dees_Troy51a0e822012-09-05 15:24:24 -04001533}
1534
1535string TWPartition::Backup_Method_By_Name() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001536 if (Backup_Method == NONE)
1537 return "none";
1538 else if (Backup_Method == FILES)
1539 return "files";
1540 else if (Backup_Method == DD)
1541 return "dd";
1542 else if (Backup_Method == FLASH_UTILS)
1543 return "flash_utils";
1544 else
1545 return "undefined";
1546 return "ERROR!";
Dees_Troy51a0e822012-09-05 15:24:24 -04001547}
1548
1549bool TWPartition::Decrypt(string Password) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001550 LOGINFO("STUB TWPartition::Decrypt, password: '%s'\n", Password.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001551 // Is this needed?
Dees_Troy51a0e822012-09-05 15:24:24 -04001552 return 1;
1553}
1554
1555bool TWPartition::Wipe_Encryption() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001556 bool Save_Data_Media = Has_Data_Media;
1557
1558 if (!UnMount(true))
1559 return false;
1560
Dees_Troy38bd7602012-09-14 13:33:53 -04001561 Has_Data_Media = false;
Dees_Troy74fb2e92013-04-15 14:35:47 +00001562 Decrypted_Block_Device = "";
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001563#ifdef TW_INCLUDE_CRYPTO
1564 if (Is_Decrypted) {
1565 if (!UnMount(true))
1566 return false;
Matt Mower2b18a532015-02-20 16:58:05 -06001567 if (delete_crypto_blk_dev((char*)("userdata")) != 0) {
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001568 LOGERR("Error deleting crypto block device, continuing anyway.\n");
1569 }
1570 }
1571#endif
Dees_Troy74fb2e92013-04-15 14:35:47 +00001572 Is_Decrypted = false;
1573 Is_Encrypted = false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001574 Find_Actual_Block_Device();
Ethan Yonkerbc85b632015-08-09 12:48:14 -05001575 if (Crypto_Key_Location == "footer") {
1576 int newlen, fd;
1577 if (Length != 0) {
1578 newlen = Length;
1579 if (newlen < 0)
1580 newlen = newlen * -1;
1581 } else {
1582 newlen = CRYPT_FOOTER_OFFSET;
1583 }
1584 if ((fd = open(Actual_Block_Device.c_str(), O_RDWR)) < 0) {
1585 gui_print_color("warning", "Unable to open '%s' to wipe crypto key\n", Actual_Block_Device.c_str());
1586 } else {
1587 unsigned int block_count;
1588 if ((ioctl(fd, BLKGETSIZE, &block_count)) == -1) {
1589 gui_print_color("warning", "Unable to get block size for wiping crypto footer.\n");
1590 } else {
1591 off64_t offset = ((off64_t)block_count * 512) - newlen;
1592 if (lseek64(fd, offset, SEEK_SET) == -1) {
1593 gui_print_color("warning", "Unable to lseek64 for wiping crypto footer.\n");
1594 } else {
1595 void* buffer = malloc(newlen);
1596 if (!buffer) {
1597 gui_print_color("warning", "Failed to malloc for wiping crypto footer.\n");
1598 } else {
1599 memset(buffer, 0, newlen);
1600 int ret = write(fd, buffer, newlen);
1601 if (ret != newlen) {
1602 gui_print_color("warning", "Failed to wipe crypto footer.\n");
1603 } else {
1604 LOGINFO("Successfully wiped crypto footer.\n");
1605 }
Matt Mower13a8f0b2015-09-26 15:40:03 -05001606 free(buffer);
Ethan Yonkerbc85b632015-08-09 12:48:14 -05001607 }
1608 }
1609 }
1610 close(fd);
1611 }
1612 } else {
1613 string Command = "flash_image " + Crypto_Key_Location + " /dev/zero";
1614 TWFunc::Exec_Cmd(Command);
1615 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001616 if (Wipe(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001617 Has_Data_Media = Save_Data_Media;
1618 if (Has_Data_Media && !Symlink_Mount_Point.empty()) {
1619 Recreate_Media_Folder();
Ethan Yonker726a0202014-12-16 20:01:38 -06001620 if (Mount(false))
1621 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04001622 }
Ethan Yonkerd79d9bc2014-12-20 15:38:29 -06001623 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
Ethan Yonker83e82572014-04-04 10:59:28 -05001624#ifndef TW_OEM_BUILD
Ethan Yonker74db1572015-10-28 12:44:49 -05001625 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 -05001626#endif
Dees_Troy38bd7602012-09-14 13:33:53 -04001627 return true;
1628 } else {
1629 Has_Data_Media = Save_Data_Media;
Ethan Yonker74db1572015-10-28 12:44:49 -05001630 gui_err("format_data_err=Unable to format to remove encryption.");
Ethan Yonker726a0202014-12-16 20:01:38 -06001631 if (Has_Data_Media && Mount(false))
1632 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04001633 return false;
1634 }
1635 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001636}
1637
1638void TWPartition::Check_FS_Type() {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001639 const char* type;
1640 blkid_probe pr;
Dees_Troy5bf43922012-09-07 16:07:55 -04001641
Dees_Troy68cab492012-12-12 19:29:35 +00001642 if (Fstab_File_System == "yaffs2" || Fstab_File_System == "mtd" || Fstab_File_System == "bml" || Ignore_Blkid)
1643 return; // Running blkid on some mtd devices causes a massive crash or needs to be skipped
Dees_Troy5bf43922012-09-07 16:07:55 -04001644
Dees_Troy38bd7602012-09-14 13:33:53 -04001645 Find_Actual_Block_Device();
Dees_Troy8170a922012-09-18 15:40:25 -04001646 if (!Is_Present)
1647 return;
Dees_Troy51127312012-09-08 13:08:49 -04001648
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001649 pr = blkid_new_probe_from_filename(Actual_Block_Device.c_str());
1650 if (blkid_do_fullprobe(pr)) {
1651 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00001652 LOGINFO("Can't probe device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001653 return;
Dees_Troy5bf43922012-09-07 16:07:55 -04001654 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001655
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05001656 if (blkid_probe_lookup_value(pr, "TYPE", &type, NULL) < 0) {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001657 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00001658 LOGINFO("can't find filesystem on device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001659 return;
1660 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001661
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001662 Current_File_System = type;
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001663 blkid_free_probe(pr);
Dees_Troy51a0e822012-09-05 15:24:24 -04001664}
1665
Gary Peck43acadf2012-11-21 21:19:01 -08001666bool TWPartition::Wipe_EXT23(string File_System) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001667 if (!UnMount(true))
1668 return false;
1669
Dees_Troy43d8b002012-09-17 16:00:01 -04001670 if (TWFunc::Path_Exists("/sbin/mke2fs")) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001671 string command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001672
Greg Wallace4b44fef2015-12-29 15:33:24 -05001673 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mke2fs"));
Dees_Troy38bd7602012-09-14 13:33:53 -04001674 Find_Actual_Block_Device();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001675 command = "mke2fs -t " + File_System + " -m 0 " + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001676 LOGINFO("mke2fs command: %s\n", command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001677 if (TWFunc::Exec_Cmd(command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001678 Current_File_System = File_System;
Dees_Troye58d5262012-09-21 12:27:57 -04001679 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05001680 gui_msg("done=Done.");
Dees_Troy38bd7602012-09-14 13:33:53 -04001681 return true;
1682 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001683 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04001684 return false;
1685 }
1686 } else
1687 return Wipe_RMRF();
1688
1689 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001690}
1691
1692bool TWPartition::Wipe_EXT4() {
Ethan Yonker25f20c12014-10-14 09:04:43 -05001693 Find_Actual_Block_Device();
1694 if (!Is_Present) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001695 LOGINFO("Block device not present, cannot wipe %s.\n", Display_Name.c_str());
1696 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Ethan Yonker25f20c12014-10-14 09:04:43 -05001697 return false;
1698 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001699 if (!UnMount(true))
1700 return false;
1701
Dees_Troyb3265ab2013-08-30 02:59:14 +00001702#if defined(HAVE_SELINUX) && defined(USE_EXT4)
Ethan Yonkerf27497f2014-02-09 11:48:33 -06001703 int ret;
1704 char *secontext = NULL;
1705
Greg Wallace4b44fef2015-12-29 15:33:24 -05001706 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("make_ext4fs"));
Ethan Yonkerf27497f2014-02-09 11:48:33 -06001707
Dees Troy99c8dbf2014-03-10 16:53:58 +00001708 if (!selinux_handle || selabel_lookup(selinux_handle, &secontext, Mount_Point.c_str(), S_IFDIR) < 0) {
Ethan Yonkerf27497f2014-02-09 11:48:33 -06001709 LOGINFO("Cannot lookup security context for '%s'\n", Mount_Point.c_str());
1710 ret = make_ext4fs(Actual_Block_Device.c_str(), Length, Mount_Point.c_str(), NULL);
1711 } else {
1712 ret = make_ext4fs(Actual_Block_Device.c_str(), Length, Mount_Point.c_str(), selinux_handle);
1713 }
1714 if (ret != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001715 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troya95f55c2013-08-17 13:14:43 +00001716 return false;
1717 } else {
bigbiff bigbiffc49d7062013-10-11 20:28:00 -04001718 string sedir = Mount_Point + "/lost+found";
1719 PartitionManager.Mount_By_Path(sedir.c_str(), true);
1720 rmdir(sedir.c_str());
1721 mkdir(sedir.c_str(), S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP);
Dees_Troya95f55c2013-08-17 13:14:43 +00001722 return true;
1723 }
1724#else
Dees_Troy43d8b002012-09-17 16:00:01 -04001725 if (TWFunc::Path_Exists("/sbin/make_ext4fs")) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001726 string Command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001727
Greg Wallace4b44fef2015-12-29 15:33:24 -05001728 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("make_ext4fs"));
Dees_Troy38bd7602012-09-14 13:33:53 -04001729 Find_Actual_Block_Device();
1730 Command = "make_ext4fs";
1731 if (!Is_Decrypted && Length != 0) {
1732 // Only use length if we're not decrypted
1733 char len[32];
1734 sprintf(len, "%i", Length);
1735 Command += " -l ";
1736 Command += len;
1737 }
Dees_Troy5295d582013-09-06 15:51:08 +00001738 if (TWFunc::Path_Exists("/file_contexts")) {
1739 Command += " -S /file_contexts";
1740 }
1741 Command += " -a " + Mount_Point + " " + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001742 LOGINFO("make_ext4fs command: %s\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001743 if (TWFunc::Exec_Cmd(Command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001744 Current_File_System = "ext4";
Dees_Troye58d5262012-09-21 12:27:57 -04001745 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05001746 gui_msg("done=Done.");
Dees_Troy38bd7602012-09-14 13:33:53 -04001747 return true;
1748 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001749 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04001750 return false;
1751 }
1752 } else
Gary Peck43acadf2012-11-21 21:19:01 -08001753 return Wipe_EXT23("ext4");
Dees_Troya95f55c2013-08-17 13:14:43 +00001754#endif
Dees_Troy38bd7602012-09-14 13:33:53 -04001755 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001756}
1757
1758bool TWPartition::Wipe_FAT() {
Vojtech Bocek05534202013-09-11 08:11:56 +02001759 string command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001760
Matt Mower18794c82015-11-11 16:22:45 -06001761 if (TWFunc::Path_Exists("/sbin/mkfs.fat")) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001762 if (!UnMount(true))
1763 return false;
1764
Greg Wallace4b44fef2015-12-29 15:33:24 -05001765 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mkfs.fat"));
Dees_Troy38bd7602012-09-14 13:33:53 -04001766 Find_Actual_Block_Device();
Matt Mower18794c82015-11-11 16:22:45 -06001767 command = "mkfs.fat " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02001768 if (TWFunc::Exec_Cmd(command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001769 Current_File_System = "vfat";
Dees_Troye58d5262012-09-21 12:27:57 -04001770 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05001771 gui_msg("done=Done.");
Dees_Troy38bd7602012-09-14 13:33:53 -04001772 return true;
1773 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001774 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troy38bd7602012-09-14 13:33:53 -04001775 return false;
1776 }
1777 return true;
1778 }
1779 else
1780 return Wipe_RMRF();
1781
1782 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001783}
1784
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001785bool TWPartition::Wipe_EXFAT() {
Vojtech Bocek05534202013-09-11 08:11:56 +02001786 string command;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001787
1788 if (TWFunc::Path_Exists("/sbin/mkexfatfs")) {
1789 if (!UnMount(true))
1790 return false;
1791
Greg Wallace4b44fef2015-12-29 15:33:24 -05001792 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mkexfatfs"));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001793 Find_Actual_Block_Device();
1794 command = "mkexfatfs " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02001795 if (TWFunc::Exec_Cmd(command) == 0) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001796 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05001797 gui_msg("done=Done.");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001798 return true;
1799 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001800 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001801 return false;
1802 }
1803 return true;
1804 }
1805 return false;
1806}
1807
Dees_Troy38bd7602012-09-14 13:33:53 -04001808bool TWPartition::Wipe_MTD() {
1809 if (!UnMount(true))
1810 return false;
1811
Greg Wallace4b44fef2015-12-29 15:33:24 -05001812 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("MTD"));
Dees_Troy38bd7602012-09-14 13:33:53 -04001813
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001814 mtd_scan_partitions();
1815 const MtdPartition* mtd = mtd_find_partition_by_name(MTD_Name.c_str());
1816 if (mtd == NULL) {
1817 LOGERR("No mtd partition named '%s'", MTD_Name.c_str());
1818 return false;
1819 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001820
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001821 MtdWriteContext* ctx = mtd_write_partition(mtd);
1822 if (ctx == NULL) {
1823 LOGERR("Can't write '%s', failed to format.", MTD_Name.c_str());
1824 return false;
1825 }
1826 if (mtd_erase_blocks(ctx, -1) == -1) {
1827 mtd_write_close(ctx);
1828 LOGERR("Failed to format '%s'", MTD_Name.c_str());
1829 return false;
1830 }
1831 if (mtd_write_close(ctx) != 0) {
1832 LOGERR("Failed to close '%s'", MTD_Name.c_str());
1833 return false;
1834 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001835 Current_File_System = "yaffs2";
Dees_Troye58d5262012-09-21 12:27:57 -04001836 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05001837 gui_msg("done=Done.");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001838 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001839}
1840
1841bool TWPartition::Wipe_RMRF() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001842 if (!Mount(true))
1843 return false;
Ethan Yonker726a0202014-12-16 20:01:38 -06001844 // This is the only wipe that leaves the partition mounted, so we
1845 // must manually remove the partition from MTP if it is a storage
1846 // partition.
1847 if (Is_Storage)
1848 PartitionManager.Remove_MTP_Storage(MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04001849
Ethan Yonker74db1572015-10-28 12:44:49 -05001850 gui_msg(Msg("remove_all=Removing all files under '{1}'")(Mount_Point));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001851 TWFunc::removeDir(Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04001852 Recreate_AndSec_Folder();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001853 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001854}
1855
Dees_Troye5017042013-08-29 16:38:55 +00001856bool TWPartition::Wipe_F2FS() {
Vojtech Bocek05534202013-09-11 08:11:56 +02001857 string command;
Dees_Troye5017042013-08-29 16:38:55 +00001858
1859 if (TWFunc::Path_Exists("/sbin/mkfs.f2fs")) {
1860 if (!UnMount(true))
1861 return false;
1862
Greg Wallace4b44fef2015-12-29 15:33:24 -05001863 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)("mkfs.f2fs"));
Dees_Troye5017042013-08-29 16:38:55 +00001864 Find_Actual_Block_Device();
Greg Wallace558aef72016-01-19 21:14:30 -05001865 command = "mkfs.f2fs -t 0";
dhacker29a3fa75f2015-01-17 19:42:33 -05001866 if (!Is_Decrypted && Length != 0) {
1867 // Only use length if we're not decrypted
1868 char len[32];
1869 int mod_length = Length;
1870 if (Length < 0)
1871 mod_length *= -1;
1872 sprintf(len, "%i", mod_length);
1873 command += " -r ";
1874 command += len;
1875 }
1876 command += " " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02001877 if (TWFunc::Exec_Cmd(command) == 0) {
Dees_Troye5017042013-08-29 16:38:55 +00001878 Recreate_AndSec_Folder();
Ethan Yonker74db1572015-10-28 12:44:49 -05001879 gui_msg("done=Done.");
Dees_Troye5017042013-08-29 16:38:55 +00001880 return true;
1881 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001882 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
Dees_Troye5017042013-08-29 16:38:55 +00001883 return false;
1884 }
1885 return true;
1886 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001887 LOGINFO("mkfs.f2fs binary not found, using rm -rf to wipe.\n");
Dees_Troye5017042013-08-29 16:38:55 +00001888 return Wipe_RMRF();
1889 }
1890 return false;
1891}
1892
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001893bool TWPartition::Wipe_NTFS() {
1894 string command;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001895 string Ntfsmake_Binary;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001896
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001897 if (TWFunc::Path_Exists("/sbin/mkntfs"))
1898 Ntfsmake_Binary = "mkntfs";
1899 else if (TWFunc::Path_Exists("/sbin/mkfs.ntfs"))
1900 Ntfsmake_Binary = "mkfs.ntfs";
1901 else
1902 return false;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001903
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001904 if (!UnMount(true))
1905 return false;
1906
Greg Wallace4b44fef2015-12-29 15:33:24 -05001907 gui_msg(Msg("formatting_using=Formatting {1} using {2}...")(Display_Name)(Ntfsmake_Binary));
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001908 Find_Actual_Block_Device();
1909 command = "/sbin/" + Ntfsmake_Binary + " " + Actual_Block_Device;
1910 if (TWFunc::Exec_Cmd(command) == 0) {
1911 Recreate_AndSec_Folder();
1912 gui_msg("done=Done.");
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001913 return true;
Greg Wallaceb39e6c62015-12-29 00:55:26 -05001914 } else {
1915 gui_msg(Msg(msg::kError, "unable_to_wipe=Unable to wipe {1}.")(Display_Name));
1916 return false;
Ethan Yonkerb81d9052015-07-09 13:20:53 -05001917 }
1918 return false;
1919}
1920
Dees_Troy51a0e822012-09-05 15:24:24 -04001921bool TWPartition::Wipe_Data_Without_Wiping_Media() {
Ethan Yonker83e82572014-04-04 10:59:28 -05001922#ifdef TW_OEM_BUILD
1923 // In an OEM Build we want to do a full format
1924 return Wipe_Encryption();
1925#else
Ethan Yonker66a19492015-12-10 10:19:45 -06001926 bool ret = false;
Dees_Troy38bd7602012-09-14 13:33:53 -04001927
Dees_Troy38bd7602012-09-14 13:33:53 -04001928 if (!Mount(true))
1929 return false;
1930
Ethan Yonker74db1572015-10-28 12:44:49 -05001931 gui_msg("wiping_data=Wiping data without wiping /data/media ...");
Ethan Yonker66a19492015-12-10 10:19:45 -06001932 ret = Wipe_Data_Without_Wiping_Media_Func(Mount_Point + "/");
1933 if (ret)
1934 gui_msg("done=Done.");
1935 return ret;
1936#endif // ifdef TW_OEM_BUILD
1937}
1938
1939bool TWPartition::Wipe_Data_Without_Wiping_Media_Func(const string& parent __unused) {
1940 string dir;
Dees_Troy38bd7602012-09-14 13:33:53 -04001941
1942 DIR* d;
Ethan Yonker66a19492015-12-10 10:19:45 -06001943 d = opendir(parent.c_str());
Dees_Troy16b74352012-11-14 22:27:31 +00001944 if (d != NULL) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001945 struct dirent* de;
1946 while ((de = readdir(d)) != NULL) {
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05001947 if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) continue;
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05001948
Ethan Yonker66a19492015-12-10 10:19:45 -06001949 dir = parent;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001950 dir.append(de->d_name);
Ethan Yonker66a19492015-12-10 10:19:45 -06001951 if (du.check_skip_dirs(dir)) {
1952 LOGINFO("skipped '%s'\n", dir.c_str());
1953 continue;
1954 }
Dees_Troyce675462013-01-09 19:48:21 +00001955 if (de->d_type == DT_DIR) {
Ethan Yonker66a19492015-12-10 10:19:45 -06001956 dir.append("/");
1957 if (!Wipe_Data_Without_Wiping_Media_Func(dir)) {
1958 closedir(d);
1959 return false;
1960 }
1961 rmdir(dir.c_str());
bigbiff bigbiff98f1f902013-01-19 18:46:13 -05001962 } 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 +00001963 if (!unlink(dir.c_str()))
Dees_Troy2673cec2013-04-02 20:22:16 +00001964 LOGINFO("Unable to unlink '%s'\n", dir.c_str());
Dees_Troyce675462013-01-09 19:48:21 +00001965 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001966 }
1967 closedir(d);
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05001968
Dees_Troy16b74352012-11-14 22:27:31 +00001969 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -04001970 }
Ethan Yonker74db1572015-10-28 12:44:49 -05001971 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Mount_Point)(strerror(errno)));
Dees_Troy16b74352012-11-14 22:27:31 +00001972 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001973}
1974
bigbiff7abc5fe2015-01-17 16:53:12 -05001975bool 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 -04001976 char back_name[255], split_index[5];
1977 string Full_FileName, Split_FileName, Tar_Args, Command;
Dees_Troy83bd4832013-05-04 12:39:56 +00001978 int use_compression, use_encryption = 0, index, backup_count;
Dees_Troy4a2a1262012-09-18 09:33:47 -04001979 struct stat st;
Dees_Troy7c2dec82012-09-26 09:49:14 -04001980 unsigned long long total_bsize = 0, file_size;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001981 twrpTar tar;
1982 vector <string> files;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001983
Dees_Troy43d8b002012-09-17 16:00:01 -04001984 if (!Mount(true))
1985 return false;
1986
Dees_Troya13d74f2013-03-24 08:54:55 -05001987 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Backup_Display_Name, "Backing Up");
Ethan Yonker74db1572015-10-28 12:44:49 -05001988 gui_msg(Msg("backing_up=Backing up {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04001989
1990 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
Dees_Troy83bd4832013-05-04 12:39:56 +00001991 tar.use_compression = use_compression;
Matt Mowerbb81e5d2014-03-20 18:05:41 -05001992
Dees_Troy83bd4832013-05-04 12:39:56 +00001993#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
1994 DataManager::GetValue("tw_encrypt_backup", use_encryption);
1995 if (use_encryption && Can_Encrypt_Backup) {
1996 tar.use_encryption = use_encryption;
1997 if (Use_Userdata_Encryption)
1998 tar.userdata_encryption = use_encryption;
Ethan Yonker87af5632014-02-10 11:56:35 -06001999 string Password;
2000 DataManager::GetValue("tw_backup_password", Password);
2001 tar.setpassword(Password);
Dees_Troy83bd4832013-05-04 12:39:56 +00002002 } else {
2003 use_encryption = false;
2004 }
2005#endif
Dees_Troy43d8b002012-09-17 16:00:01 -04002006
2007 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
2008 Backup_FileName = back_name;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002009 Full_FileName = backup_folder + "/" + Backup_FileName;
Dees_Troy83bd4832013-05-04 12:39:56 +00002010 tar.has_data_media = Has_Data_Media;
Dees Troye0a433a2013-12-02 04:10:37 +00002011 Full_FileName = backup_folder + "/" + Backup_FileName;
2012 tar.setdir(Backup_Path);
2013 tar.setfn(Full_FileName);
2014 tar.setsize(Backup_Size);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002015 tar.partition_name = Backup_Name;
2016 tar.backup_folder = backup_folder;
bigbiff7abc5fe2015-01-17 16:53:12 -05002017 if (tar.createTarFork(overall_size, other_backups_size, tar_fork_pid) != 0)
Dees Troye0a433a2013-12-02 04:10:37 +00002018 return false;
Dees_Troy43d8b002012-09-17 16:00:01 -04002019 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002020}
2021
2022bool TWPartition::Backup_DD(string backup_folder) {
codelover352b75e2015-03-29 02:44:07 +08002023 char back_name[255], block_size[32], dd_count[32];
2024 string Full_FileName, Command, DD_BS, DD_COUNT;
Dees_Troy43d8b002012-09-17 16:00:01 -04002025 int use_compression;
codelover352b75e2015-03-29 02:44:07 +08002026 unsigned long long DD_Block_Size, DD_Count;
Dees_Troy43d8b002012-09-17 16:00:01 -04002027
codelover352b75e2015-03-29 02:44:07 +08002028 DD_Block_Size = 16 * 1024 * 1024;
2029 while (Backup_Size % DD_Block_Size != 0) DD_Block_Size >>= 1;
2030
2031 DD_Count = Backup_Size / DD_Block_Size;
2032
2033 sprintf(dd_count, "%llu", DD_Count);
2034 DD_COUNT = dd_count;
2035
2036 sprintf(block_size, "%llu", DD_Block_Size);
2037 DD_BS = block_size;
igoriok87e3d932013-01-31 21:03:53 +02002038
Ethan Yonker74db1572015-10-28 12:44:49 -05002039 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, gui_parse_text("{@backing}"));
2040 gui_msg(Msg("backing_up=Backing up {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002041
2042 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
2043 Backup_FileName = back_name;
2044
2045 Full_FileName = backup_folder + "/" + Backup_FileName;
2046
codelover352b75e2015-03-29 02:44:07 +08002047 Command = "dd if=" + Actual_Block_Device + " of='" + Full_FileName + "'" + " bs=" + DD_BS + " count=" + DD_COUNT;
Dees_Troy2673cec2013-04-02 20:22:16 +00002048 LOGINFO("Backup command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02002049 TWFunc::Exec_Cmd(Command);
Ethan Yonker4b94cfd2014-12-11 10:00:45 -06002050 tw_set_default_metadata(Full_FileName.c_str());
Dees_Troyc154ac22012-10-12 15:36:47 -04002051 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002052 gui_msg(Msg(msg::kError, "backup_size=Backup file size for '{1}' is 0 bytes.")(Full_FileName));
Dees_Troy7c2dec82012-09-26 09:49:14 -04002053 return false;
2054 }
Dees_Troy43d8b002012-09-17 16:00:01 -04002055 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002056}
2057
2058bool TWPartition::Backup_Dump_Image(string backup_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04002059 char back_name[255];
Vojtech Bocek05534202013-09-11 08:11:56 +02002060 string Full_FileName, Command;
Dees_Troy43d8b002012-09-17 16:00:01 -04002061 int use_compression;
2062
Ethan Yonker74db1572015-10-28 12:44:49 -05002063 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, gui_parse_text("{@backing}"));
2064 gui_msg(Msg("backing_up=Backing up {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002065
2066 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
2067 Backup_FileName = back_name;
2068
2069 Full_FileName = backup_folder + "/" + Backup_FileName;
2070
2071 Command = "dump_image " + MTD_Name + " '" + Full_FileName + "'";
Dees_Troy2673cec2013-04-02 20:22:16 +00002072 LOGINFO("Backup command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02002073 TWFunc::Exec_Cmd(Command);
Ethan Yonker4b94cfd2014-12-11 10:00:45 -06002074 tw_set_default_metadata(Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04002075 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
2076 // 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 -06002077 gui_msg(Msg(msg::kError, "backup_size=Backup file size for '{1}' is 0 bytes.")(Full_FileName));
Dees_Troy7c2dec82012-09-26 09:49:14 -04002078 return false;
2079 }
Dees_Troy43d8b002012-09-17 16:00:01 -04002080 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002081}
2082
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002083unsigned long long TWPartition::Get_Restore_Size(string restore_folder) {
2084 InfoManager restore_info(restore_folder + "/" + Backup_Name + ".info");
2085 if (restore_info.LoadValues() == 0) {
2086 if (restore_info.GetValue("backup_size", Restore_Size) == 0) {
2087 LOGINFO("Read info file, restore size is %llu\n", Restore_Size);
2088 return Restore_Size;
2089 }
2090 }
2091 string Full_FileName, Restore_File_System = Get_Restore_File_System(restore_folder);
2092
2093 Full_FileName = restore_folder + "/" + Backup_FileName;
2094
2095 if (Is_Image(Restore_File_System)) {
2096 Restore_Size = TWFunc::Get_File_Size(Full_FileName);
2097 return Restore_Size;
2098 }
2099
2100 twrpTar tar;
2101 tar.setdir(Backup_Path);
2102 tar.setfn(Full_FileName);
2103 tar.backup_name = Backup_Name;
2104#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
2105 string Password;
2106 DataManager::GetValue("tw_restore_password", Password);
2107 if (!Password.empty())
2108 tar.setpassword(Password);
2109#endif
2110 tar.partition_name = Backup_Name;
2111 tar.backup_folder = restore_folder;
2112 Restore_Size = tar.get_size();
2113 return Restore_Size;
2114}
2115
2116bool 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 -08002117 string Full_FileName, Command;
Dees_Troy4a2a1262012-09-18 09:33:47 -04002118 int index = 0;
2119 char split_index[5];
Dees Troy4159aed2014-02-28 17:24:43 +00002120 bool ret = false;
Dees_Troy43d8b002012-09-17 16:00:01 -04002121
Dees_Troye58d5262012-09-21 12:27:57 -04002122 if (Has_Android_Secure) {
Dees_Troye58d5262012-09-21 12:27:57 -04002123 if (!Wipe_AndSec())
2124 return false;
Gary Peck43acadf2012-11-21 21:19:01 -08002125 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002126 gui_msg(Msg("wiping=Wiping {1}")(Backup_Display_Name));
Ethan Yonker5eac2222014-06-11 12:22:55 -05002127 if (Has_Data_Media && Mount_Point == "/data" && Restore_File_System != Current_File_System) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002128 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 -05002129 if (!Wipe_Data_Without_Wiping_Media())
2130 return false;
2131 } else {
2132 if (!Wipe(Restore_File_System))
2133 return false;
2134 }
Dees_Troye58d5262012-09-21 12:27:57 -04002135 }
Matt Mower3c366972015-12-25 19:28:31 -06002136 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Backup_Display_Name, gui_parse_text("{@restoring_hdr}"));
Ethan Yonker74db1572015-10-28 12:44:49 -05002137 gui_msg(Msg("restoring=Restoring {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002138
2139 if (!Mount(true))
2140 return false;
2141
Dees_Troy4a2a1262012-09-18 09:33:47 -04002142 Full_FileName = restore_folder + "/" + Backup_FileName;
Ethan Yonker87af5632014-02-10 11:56:35 -06002143 twrpTar tar;
2144 tar.setdir(Backup_Path);
2145 tar.setfn(Full_FileName);
2146 tar.backup_name = Backup_Name;
2147#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
2148 string Password;
2149 DataManager::GetValue("tw_restore_password", Password);
2150 if (!Password.empty())
2151 tar.setpassword(Password);
2152#endif
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002153 if (tar.extractTarFork(total_restore_size, already_restored_size) != 0)
Dees Troy4159aed2014-02-28 17:24:43 +00002154 ret = false;
2155 else
2156 ret = true;
2157#ifdef HAVE_CAPABILITIES
2158 // Restore capabilities to the run-as binary
2159 if (Mount_Point == "/system" && Mount(true) && TWFunc::Path_Exists("/system/bin/run-as")) {
2160 struct vfs_cap_data cap_data;
2161 uint64_t capabilities = (1 << CAP_SETUID) | (1 << CAP_SETGID);
2162
2163 memset(&cap_data, 0, sizeof(cap_data));
2164 cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE;
2165 cap_data.data[0].permitted = (uint32_t) (capabilities & 0xffffffff);
2166 cap_data.data[0].inheritable = 0;
2167 cap_data.data[1].permitted = (uint32_t) (capabilities >> 32);
2168 cap_data.data[1].inheritable = 0;
2169 if (setxattr("/system/bin/run-as", XATTR_NAME_CAPS, &cap_data, sizeof(cap_data), 0) < 0) {
2170 LOGINFO("Failed to reset capabilities of /system/bin/run-as binary.\n");
2171 } else {
2172 LOGINFO("Reset capabilities of /system/bin/run-as binary successful.\n");
2173 }
2174 }
2175#endif
2176 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04002177}
2178
Ethan Yonker96af84a2015-01-05 14:58:36 -06002179bool TWPartition::Restore_Image(string restore_folder, const unsigned long long *total_restore_size, unsigned long long *already_restored_size, string Restore_File_System) {
2180 string Full_FileName;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002181 double display_percent, progress_percent;
2182 char size_progress[1024];
Dees_Troy43d8b002012-09-17 16:00:01 -04002183
Matt Mower3c366972015-12-25 19:28:31 -06002184 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Backup_Display_Name, gui_parse_text("{@restoring_hdr}"));
Ethan Yonker74db1572015-10-28 12:44:49 -05002185 gui_msg(Msg("restoring=Restoring {1}...")(Backup_Display_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04002186 Full_FileName = restore_folder + "/" + Backup_FileName;
Gary Peck15e623d2012-11-21 21:07:58 -08002187
Ethan Yonker96af84a2015-01-05 14:58:36 -06002188 if (Restore_File_System == "emmc") {
2189 if (!Flash_Image_DD(Full_FileName))
2190 return false;
2191 } else if (Restore_File_System == "mtd" || Restore_File_System == "bml") {
2192 if (!Flash_Image_FI(Full_FileName))
2193 return false;
Gary Peck15e623d2012-11-21 21:07:58 -08002194 }
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002195 display_percent = (double)(Restore_Size + *already_restored_size) / (double)(*total_restore_size) * 100;
2196 sprintf(size_progress, "%lluMB of %lluMB, %i%%", (Restore_Size + *already_restored_size) / 1048576, *total_restore_size / 1048576, (int)(display_percent));
2197 DataManager::SetValue("tw_size_progress", size_progress);
2198 progress_percent = (display_percent / 100);
2199 DataManager::SetProgress((float)(progress_percent));
2200 *already_restored_size += Restore_Size;
Dees_Troy43d8b002012-09-17 16:00:01 -04002201 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002202}
Dees_Troy5bf43922012-09-07 16:07:55 -04002203
2204bool TWPartition::Update_Size(bool Display_Error) {
Dees_Troy0550cfb2012-10-13 11:56:13 -04002205 bool ret = false, Was_Already_Mounted = false;
Dees_Troy51127312012-09-08 13:08:49 -04002206
Dees_Troyab10ee22012-09-21 14:27:30 -04002207 if (!Can_Be_Mounted && !Is_Encrypted)
Dees_Troy5bf43922012-09-07 16:07:55 -04002208 return false;
2209
Dees_Troy0550cfb2012-10-13 11:56:13 -04002210 Was_Already_Mounted = Is_Mounted();
Dees_Troy38bd7602012-09-14 13:33:53 -04002211 if (Removable || Is_Encrypted) {
2212 if (!Mount(false))
2213 return true;
2214 } else if (!Mount(Display_Error))
Dees_Troy5bf43922012-09-07 16:07:55 -04002215 return false;
Dees_Troy51127312012-09-08 13:08:49 -04002216
2217 ret = Get_Size_Via_statfs(Display_Error);
Dees_Troy0550cfb2012-10-13 11:56:13 -04002218 if (!ret || Size == 0) {
2219 if (!Get_Size_Via_df(Display_Error)) {
2220 if (!Was_Already_Mounted)
2221 UnMount(false);
Dees_Troy51127312012-09-08 13:08:49 -04002222 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04002223 }
2224 }
Dees_Troy51127312012-09-08 13:08:49 -04002225
Dees_Troy5bf43922012-09-07 16:07:55 -04002226 if (Has_Data_Media) {
2227 if (Mount(Display_Error)) {
Dees_Troy51127312012-09-08 13:08:49 -04002228 unsigned long long data_media_used, actual_data;
Ethan Yonker66a19492015-12-10 10:19:45 -06002229 Used = du.Get_Folder_Size(Mount_Point);
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002230 Backup_Size = Used;
2231 int bak = (int)(Used / 1048576LLU);
Dees_Troy51127312012-09-08 13:08:49 -04002232 int fre = (int)(Free / 1048576LLU);
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002233 LOGINFO("Data backup size is %iMB, free: %iMB.\n", bak, fre);
Dees_Troy0550cfb2012-10-13 11:56:13 -04002234 } else {
2235 if (!Was_Already_Mounted)
2236 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04002237 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04002238 }
Dees_Troye58d5262012-09-21 12:27:57 -04002239 } else if (Has_Android_Secure) {
2240 if (Mount(Display_Error))
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002241 Backup_Size = du.Get_Folder_Size(Backup_Path);
Dees_Troy0550cfb2012-10-13 11:56:13 -04002242 else {
2243 if (!Was_Already_Mounted)
2244 UnMount(false);
Dees_Troye58d5262012-09-21 12:27:57 -04002245 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04002246 }
Dees_Troy5bf43922012-09-07 16:07:55 -04002247 }
Dees_Troy0550cfb2012-10-13 11:56:13 -04002248 if (!Was_Already_Mounted)
2249 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04002250 return true;
Dees_Troy51127312012-09-08 13:08:49 -04002251}
Dees_Troy38bd7602012-09-14 13:33:53 -04002252
2253void TWPartition::Find_Actual_Block_Device(void) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002254 if (Is_Decrypted && !Decrypted_Block_Device.empty()) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002255 Actual_Block_Device = Decrypted_Block_Device;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002256 if (TWFunc::Path_Exists(Decrypted_Block_Device))
Dees_Troy38bd7602012-09-14 13:33:53 -04002257 Is_Present = true;
Dees_Troy43d8b002012-09-17 16:00:01 -04002258 } else if (TWFunc::Path_Exists(Primary_Block_Device)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002259 Is_Present = true;
2260 Actual_Block_Device = Primary_Block_Device;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002261 return;
2262 }
2263 if (Is_Decrypted) {
Dees_Troy43d8b002012-09-17 16:00:01 -04002264 } else if (!Alternate_Block_Device.empty() && TWFunc::Path_Exists(Alternate_Block_Device)) {
Dees_Troy3f04d032012-10-07 18:20:09 -04002265 Actual_Block_Device = Alternate_Block_Device;
Dees_Troy38bd7602012-09-14 13:33:53 -04002266 Is_Present = true;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002267 } else {
Dees_Troy38bd7602012-09-14 13:33:53 -04002268 Is_Present = false;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002269 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002270}
2271
2272void TWPartition::Recreate_Media_Folder(void) {
2273 string Command;
Ethan Yonker66a19492015-12-10 10:19:45 -06002274 string Media_Path = Mount_Point + "/media";
Dees_Troy38bd7602012-09-14 13:33:53 -04002275
2276 if (!Mount(true)) {
Ethan Yonker66a19492015-12-10 10:19:45 -06002277 gui_msg(Msg(msg::kError, "recreate_folder_err=Unable to recreate {1} folder.")(Media_Path));
2278 } else if (!TWFunc::Path_Exists(Media_Path)) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002279 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Ethan Yonker66a19492015-12-10 10:19:45 -06002280 LOGINFO("Recreating %s folder.\n", Media_Path.c_str());
2281 mkdir(Media_Path.c_str(), 0770);
xiaolu9416f4f2015-06-04 08:22:23 +08002282 string Internal_path = DataManager::GetStrValue("tw_internal_path");
2283 if (!Internal_path.empty()) {
2284 LOGINFO("Recreating %s folder.\n", Internal_path.c_str());
2285 mkdir(Internal_path.c_str(), 0770);
2286 }
2287#ifdef TW_INTERNAL_STORAGE_PATH
2288 mkdir(EXPAND(TW_INTERNAL_STORAGE_PATH), 0770);
2289#endif
Ethan Yonker5ef301e2014-10-14 10:04:44 -05002290#ifdef HAVE_SELINUX
thata3d31fb2014-12-21 22:27:40 +01002291 // Afterwards, we will try to set the
2292 // default metadata that we were hopefully able to get during
2293 // early boot.
Ethan Yonker66a19492015-12-10 10:19:45 -06002294 tw_set_default_metadata(Media_Path.c_str());
xiaolu9416f4f2015-06-04 08:22:23 +08002295 if (!Internal_path.empty())
2296 tw_set_default_metadata(Internal_path.c_str());
Ethan Yonker5ef301e2014-10-14 10:04:44 -05002297#endif
Ethan Yonker5eac2222014-06-11 12:22:55 -05002298 // Toggle mount to ensure that "internal sdcard" gets mounted
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002299 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Ethan Yonker5eac2222014-06-11 12:22:55 -05002300 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Dees_Troy38bd7602012-09-14 13:33:53 -04002301 }
Dees_Troy43d8b002012-09-17 16:00:01 -04002302}
Dees_Troye58d5262012-09-21 12:27:57 -04002303
2304void TWPartition::Recreate_AndSec_Folder(void) {
Dees_Troye58d5262012-09-21 12:27:57 -04002305 if (!Has_Android_Secure)
2306 return;
Dees_Troy2673cec2013-04-02 20:22:16 +00002307 LOGINFO("Creating %s: %s\n", Backup_Display_Name.c_str(), Symlink_Path.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -04002308 if (!Mount(true)) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002309 gui_msg(Msg(msg::kError, "recreate_folder_err=Unable to recreate {1} folder.")(Backup_Name));
Dees_Troye58d5262012-09-21 12:27:57 -04002310 } else if (!TWFunc::Path_Exists(Symlink_Path)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00002311 LOGINFO("Recreating %s folder.\n", Backup_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002312 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05002313 mkdir(Symlink_Path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002314 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04002315 }
2316}
bigbiff7cb4c332014-11-26 20:36:07 -05002317
2318uint64_t TWPartition::Get_Max_FileSize() {
2319 uint64_t maxFileSize = 0;
2320 const uint64_t constGB = (uint64_t) 1024 * 1024 * 1024;
2321 const uint64_t constTB = (uint64_t) constGB * 1024;
2322 const uint64_t constPB = (uint64_t) constTB * 1024;
2323 const uint64_t constEB = (uint64_t) constPB * 1024;
bigbiff0c532032014-12-21 13:41:26 -05002324 if (Current_File_System == "ext4")
2325 maxFileSize = 16 * constTB; //16 TB
2326 else if (Current_File_System == "vfat")
2327 maxFileSize = 4 * constGB; //4 GB
2328 else if (Current_File_System == "ntfs")
2329 maxFileSize = 256 * constTB; //256 TB
2330 else if (Current_File_System == "exfat")
2331 maxFileSize = 16 * constPB; //16 PB
2332 else if (Current_File_System == "ext3")
2333 maxFileSize = 2 * constTB; //2 TB
2334 else if (Current_File_System == "f2fs")
2335 maxFileSize = 3.94 * constTB; //3.94 TB
bigbiff7cb4c332014-11-26 20:36:07 -05002336 else
2337 maxFileSize = 100000000L;
2338 return maxFileSize - 1;
2339}
2340
Ethan Yonker96af84a2015-01-05 14:58:36 -06002341bool TWPartition::Flash_Image(string Filename) {
2342 string Restore_File_System;
2343
2344 LOGINFO("Image filename is: %s\n", Filename.c_str());
2345
2346 if (Backup_Method == FILES) {
2347 LOGERR("Cannot flash images to file systems\n");
2348 return false;
2349 } else if (!Can_Flash_Img) {
2350 LOGERR("Cannot flash images to partitions %s\n", Display_Name.c_str());
2351 return false;
2352 } else {
2353 if (!Find_Partition_Size()) {
2354 LOGERR("Unable to find partition size for '%s'\n", Mount_Point.c_str());
2355 return false;
2356 }
2357 unsigned long long image_size = TWFunc::Get_File_Size(Filename);
2358 if (image_size > Size) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002359 LOGINFO("Size (%llu bytes) of image '%s' is larger than target device '%s' (%llu bytes)\n",
Ethan Yonker96af84a2015-01-05 14:58:36 -06002360 image_size, Filename.c_str(), Actual_Block_Device.c_str(), Size);
Ethan Yonker74db1572015-10-28 12:44:49 -05002361 gui_err("img_size_err=Size of image is larger than target device");
Ethan Yonker96af84a2015-01-05 14:58:36 -06002362 return false;
2363 }
2364 if (Backup_Method == DD)
2365 return Flash_Image_DD(Filename);
2366 else if (Backup_Method == FLASH_UTILS)
2367 return Flash_Image_FI(Filename);
2368 }
2369
2370 LOGERR("Unknown flash method for '%s'\n", Mount_Point.c_str());
2371 return false;
2372}
2373
2374bool TWPartition::Flash_Image_DD(string Filename) {
2375 string Command;
2376
Ethan Yonker74db1572015-10-28 12:44:49 -05002377 gui_msg(Msg("flashing=Flashing {1}...")(Display_Name));
codelover352b75e2015-03-29 02:44:07 +08002378 Command = "dd bs=8388608 if='" + Filename + "' of=" + Actual_Block_Device;
Ethan Yonker96af84a2015-01-05 14:58:36 -06002379 LOGINFO("Flash command: '%s'\n", Command.c_str());
2380 TWFunc::Exec_Cmd(Command);
2381 return true;
2382}
2383
2384bool TWPartition::Flash_Image_FI(string Filename) {
2385 string Command;
2386
Ethan Yonker74db1572015-10-28 12:44:49 -05002387 gui_msg(Msg("flashing=Flashing {1}...")(Display_Name));
Ethan Yonker96af84a2015-01-05 14:58:36 -06002388 // Sometimes flash image doesn't like to flash due to the first 2KB matching, so we erase first to ensure that it flashes
2389 Command = "erase_image " + MTD_Name;
2390 LOGINFO("Erase command: '%s'\n", Command.c_str());
2391 TWFunc::Exec_Cmd(Command);
2392 Command = "flash_image " + MTD_Name + " '" + Filename + "'";
2393 LOGINFO("Flash command: '%s'\n", Command.c_str());
2394 TWFunc::Exec_Cmd(Command);
2395 return true;
2396}
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05002397
2398void TWPartition::Change_Mount_Read_Only(bool new_value) {
2399 Mount_Read_Only = new_value;
2400}
2401
2402int TWPartition::Check_Lifetime_Writes() {
2403 bool original_read_only = Mount_Read_Only;
2404 int ret = 1;
2405
2406 Mount_Read_Only = true;
2407 if (Mount(false)) {
2408 Find_Actual_Block_Device();
2409 string block = basename(Actual_Block_Device.c_str());
2410 string file = "/sys/fs/" + Current_File_System + "/" + block + "/lifetime_write_kbytes";
2411 string result;
2412 if (TWFunc::Path_Exists(file)) {
2413 if (TWFunc::read_file(file, result) != 0) {
2414 LOGINFO("Check_Lifetime_Writes of '%s' failed to read_file\n", file.c_str());
2415 } else {
2416 LOGINFO("Check_Lifetime_Writes result: '%s'\n", result.c_str());
2417 if (result == "0") {
2418 ret = 0;
2419 }
2420 }
2421 } else {
2422 LOGINFO("Check_Lifetime_Writes file does not exist '%s'\n", file.c_str());
2423 }
2424 UnMount(true);
2425 } else {
2426 LOGINFO("Check_Lifetime_Writes failed to mount '%s'\n", Mount_Point.c_str());
2427 }
2428 Mount_Read_Only = original_read_only;
2429 return ret;
2430}
Ethan Yonker66a19492015-12-10 10:19:45 -06002431
2432int TWPartition::Decrypt_Adopted() {
2433#ifdef TW_INCLUDE_CRYPTO
2434 int ret = 1;
2435 Is_Adopted_Storage = false;
2436 string Adopted_Key_File = "";
2437
2438 if (!Removable)
2439 return ret;
2440
2441 int fd = open(Alternate_Block_Device.c_str(), O_RDONLY);
2442 if (fd < 0) {
2443 LOGINFO("failed to open '%s'\n", Alternate_Block_Device.c_str());
2444 return ret;
2445 }
2446 char type_guid[80];
2447 char part_guid[80];
2448
2449 if (gpt_disk_get_partition_info(fd, 2, type_guid, part_guid) == 0) {
2450 LOGINFO("type: '%s'\n", type_guid);
2451 LOGINFO("part: '%s'\n", part_guid);
2452 Adopted_GUID = part_guid;
2453 LOGINFO("Adopted_GUID '%s'\n", Adopted_GUID.c_str());
2454 if (strcmp(type_guid, TWGptAndroidExpand) == 0) {
2455 LOGINFO("android_expand found\n");
2456 Adopted_Key_File = "/data/misc/vold/expand_";
2457 Adopted_Key_File += part_guid;
2458 Adopted_Key_File += ".key";
2459 if (TWFunc::Path_Exists(Adopted_Key_File)) {
2460 Is_Adopted_Storage = true;
2461 /* Until we find a use case for this, I think it is safe
2462 * to disable USB Mass Storage whenever adopted storage
2463 * is present.
2464 */
2465 LOGINFO("Detected adopted storage, disabling USB mass storage mode\n");
2466 DataManager::SetValue("tw_has_usb_storage", 0);
2467 }
2468 }
2469 }
2470
2471 if (Is_Adopted_Storage) {
2472 string Adopted_Block_Device = Alternate_Block_Device + "p2";
2473 if (!TWFunc::Path_Exists(Adopted_Block_Device)) {
2474 Adopted_Block_Device = Alternate_Block_Device + "2";
2475 if (!TWFunc::Path_Exists(Adopted_Block_Device)) {
2476 LOGINFO("Adopted block device does not exist\n");
2477 goto exit;
2478 }
2479 }
2480 LOGINFO("key file is '%s', block device '%s'\n", Adopted_Key_File.c_str(), Adopted_Block_Device.c_str());
2481 char crypto_blkdev[MAXPATHLEN];
2482 std::string thekey;
2483 int fdkey = open(Adopted_Key_File.c_str(), O_RDONLY);
2484 if (fdkey < 0) {
2485 LOGINFO("failed to open key file\n");
2486 goto exit;
2487 }
2488 char buf[512];
2489 ssize_t n;
2490 while ((n = read(fdkey, &buf[0], sizeof(buf))) > 0) {
2491 thekey.append(buf, n);
2492 }
2493 close(fdkey);
2494 unsigned char* key = (unsigned char*) thekey.data();
2495 cryptfs_revert_ext_volume(part_guid);
2496
2497 ret = cryptfs_setup_ext_volume(part_guid, Adopted_Block_Device.c_str(), key, thekey.size(), crypto_blkdev);
2498 if (ret == 0) {
2499 LOGINFO("adopted storage new block device: '%s'\n", crypto_blkdev);
2500 Decrypted_Block_Device = crypto_blkdev;
2501 Is_Decrypted = true;
2502 Is_Encrypted = true;
2503 Find_Actual_Block_Device();
2504 if (!Mount(false)) {
2505 LOGERR("Failed to mount decrypted adopted storage device\n");
2506 Is_Decrypted = false;
2507 Is_Encrypted = false;
2508 cryptfs_revert_ext_volume(part_guid);
2509 ret = 1;
2510 } else {
2511 Setup_Data_Media();
2512 Recreate_Media_Folder();
2513 Wipe_Available_in_GUI = true;
2514 Wipe_During_Factory_Reset = true;
2515 Can_Be_Backed_Up = true;
2516 Can_Encrypt_Backup = true;
2517 Use_Userdata_Encryption = true;
2518 Is_Storage = true;
2519 Storage_Name = "Adopted Storage";
2520 Is_SubPartition = true;
2521 SubPartition_Of = "/data";
2522 PartitionManager.Add_MTP_Storage(MTP_Storage_ID);
2523 DataManager::SetValue("tw_has_adopted_storage", 1);
2524 }
2525 } else {
2526 LOGERR("Failed to setup adopted storage decryption\n");
2527 }
2528 }
2529exit:
2530 return ret;
2531#else
2532 LOGINFO("Decrypt_Adopted: no crypto support\n");
2533 return 1;
2534#endif
2535}
2536
2537void TWPartition::Revert_Adopted() {
2538#ifdef TW_INCLUDE_CRYPTO
2539 if (!Adopted_GUID.empty()) {
2540 PartitionManager.Remove_MTP_Storage(Mount_Point);
2541 UnMount(false);
2542 cryptfs_revert_ext_volume(Adopted_GUID.c_str());
2543 Is_Adopted_Storage = false;
2544 Is_Encrypted = false;
2545 Is_Decrypted = false;
2546 Decrypted_Block_Device = "";
2547 Find_Actual_Block_Device();
2548 Wipe_During_Factory_Reset = false;
2549 Can_Be_Backed_Up = false;
2550 Can_Encrypt_Backup = false;
2551 Use_Userdata_Encryption = false;
2552 Is_SubPartition = false;
2553 SubPartition_Of = "";
2554 Has_Data_Media = false;
2555 Storage_Path = Mount_Point;
2556 if (!Symlink_Mount_Point.empty()) {
2557 TWPartition* Dat = PartitionManager.Find_Partition_By_Path("/data");
2558 if (Dat) {
2559 Dat->UnMount(false);
2560 Dat->Symlink_Mount_Point = Symlink_Mount_Point;
2561 }
2562 Symlink_Mount_Point = "";
2563 }
2564 }
2565#else
2566 LOGINFO("Revert_Adopted: no crypto support\n");
2567#endif
2568}