blob: 642c3dd6581bb8912c9d0a1e687b56f80fd50dde [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>
bigbiff bigbiff9c754052013-01-09 09:09:08 -050027#include <iostream>
28#include <sstream>
Dees_Troy51a0e822012-09-05 15:24:24 -040029
Dees_Troy657c3092012-09-10 20:32:10 -040030#ifdef TW_INCLUDE_CRYPTO
31 #include "cutils/properties.h"
32#endif
33
bigbiff bigbiffe60683a2013-02-22 20:55:50 -050034#include "libblkid/blkid.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040035#include "variables.h"
Dees_Troy2673cec2013-04-02 20:22:16 +000036#include "twcommon.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040037#include "partitions.hpp"
Dees_Troy5bf43922012-09-07 16:07:55 -040038#include "data.hpp"
Dees_Troy43d8b002012-09-17 16:00:01 -040039#include "twrp-functions.hpp"
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -050040#include "twrpDigest.hpp"
bigbiff bigbiff9c754052013-01-09 09:09:08 -050041#include "twrpTar.hpp"
bigbiff bigbiff34684ff2013-12-01 21:03:45 -050042#include "twrpDU.hpp"
bigbiff bigbiff6b600f92014-01-05 18:13:43 -050043#include "fixPermissions.hpp"
Ethan Yonker1b7a31b2014-07-03 15:09:22 -050044#include "infomanager.hpp"
Dees_Troy5bf43922012-09-07 16:07:55 -040045extern "C" {
Dees_Troy38bd7602012-09-14 13:33:53 -040046 #include "mtdutils/mtdutils.h"
47 #include "mtdutils/mounts.h"
Dees_Troy85f44ed2013-01-09 18:42:36 +000048#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
49 #include "crypto/libcrypt_samsung/include/libcrypt_samsung.h"
50#endif
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
56 #ifdef TW_INCLUDE_JB_CRYPTO
57 #include "crypto/jb/cryptfs.h"
Ethan Yonker4eca40d2014-11-11 14:52:28 -060058 #elif defined(TW_INCLUDE_L_CRYPTO)
59 #include "crypto/lollipop/cryptfs.h"
Ethan Yonker71413f42014-02-26 13:36:08 -060060 #else
61 #include "crypto/ics/cryptfs.h"
62 #endif
63#endif
Dees_Troy5bf43922012-09-07 16:07:55 -040064}
bigbiff bigbiffc49d7062013-10-11 20:28:00 -040065#ifdef HAVE_SELINUX
66#include "selinux/selinux.h"
Ethan Yonkerf27497f2014-02-09 11:48:33 -060067#include <selinux/label.h>
bigbiff bigbiffc49d7062013-10-11 20:28:00 -040068#endif
Dees Troy4159aed2014-02-28 17:24:43 +000069#ifdef HAVE_CAPABILITIES
70#include <sys/capability.h>
71#include <sys/xattr.h>
72#include <linux/xattr.h>
73#endif
Dees_Troy51a0e822012-09-05 15:24:24 -040074
bigbiff bigbiff9c754052013-01-09 09:09:08 -050075using namespace std;
76
Dees_Troya95f55c2013-08-17 13:14:43 +000077extern struct selabel_handle *selinux_handle;
Ethan Yonker6277c792014-09-15 14:54:30 -050078extern bool datamedia;
Dees_Troya95f55c2013-08-17 13:14:43 +000079
Hashcode62bd9e02013-11-19 21:59:42 -080080struct flag_list {
81 const char *name;
82 unsigned flag;
83};
84
85static struct flag_list mount_flags[] = {
86 { "noatime", MS_NOATIME },
87 { "noexec", MS_NOEXEC },
88 { "nosuid", MS_NOSUID },
89 { "nodev", MS_NODEV },
90 { "nodiratime", MS_NODIRATIME },
91 { "ro", MS_RDONLY },
92 { "rw", 0 },
93 { "remount", MS_REMOUNT },
94 { "bind", MS_BIND },
95 { "rec", MS_REC },
Dees Troyc4bc30e2014-02-03 15:04:19 +000096#ifdef MS_UNBINDABLE
Hashcode62bd9e02013-11-19 21:59:42 -080097 { "unbindable", MS_UNBINDABLE },
Dees Troyc4bc30e2014-02-03 15:04:19 +000098#endif
99#ifdef MS_PRIVATE
Hashcode62bd9e02013-11-19 21:59:42 -0800100 { "private", MS_PRIVATE },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000101#endif
102#ifdef MS_SLAVE
Hashcode62bd9e02013-11-19 21:59:42 -0800103 { "slave", MS_SLAVE },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000104#endif
105#ifdef MS_SHARED
Hashcode62bd9e02013-11-19 21:59:42 -0800106 { "shared", MS_SHARED },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000107#endif
Hashcode62bd9e02013-11-19 21:59:42 -0800108 { "sync", MS_SYNCHRONOUS },
109 { "defaults", 0 },
110 { 0, 0 },
111};
112
that9e0593e2014-10-08 00:01:24 +0200113TWPartition::TWPartition() {
Dees_Troy51a0e822012-09-05 15:24:24 -0400114 Can_Be_Mounted = false;
115 Can_Be_Wiped = false;
Dees_Troya13d74f2013-03-24 08:54:55 -0500116 Can_Be_Backed_Up = false;
Vojtech Bocek1dc30982013-08-30 21:49:30 +0200117 Use_Rm_Rf = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400118 Wipe_During_Factory_Reset = false;
119 Wipe_Available_in_GUI = false;
120 Is_SubPartition = false;
Dees_Troy2691f9d2012-09-24 11:15:49 -0400121 Has_SubPartition = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400122 SubPartition_Of = "";
123 Symlink_Path = "";
124 Symlink_Mount_Point = "";
125 Mount_Point = "";
Dees_Troye58d5262012-09-21 12:27:57 -0400126 Backup_Path = "";
Dees_Troy38bd7602012-09-14 13:33:53 -0400127 Actual_Block_Device = "";
128 Primary_Block_Device = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400129 Alternate_Block_Device = "";
130 Removable = false;
131 Is_Present = false;
132 Length = 0;
133 Size = 0;
134 Used = 0;
135 Free = 0;
136 Backup_Size = 0;
137 Can_Be_Encrypted = false;
138 Is_Encrypted = false;
139 Is_Decrypted = false;
140 Decrypted_Block_Device = "";
141 Display_Name = "";
Dees_Troya13d74f2013-03-24 08:54:55 -0500142 Backup_Display_Name = "";
143 Storage_Name = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400144 Backup_Name = "";
Dees_Troy63c8df72012-09-10 14:02:05 -0400145 Backup_FileName = "";
Dees_Troy38bd7602012-09-14 13:33:53 -0400146 MTD_Name = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400147 Backup_Method = NONE;
Dees_Troy83bd4832013-05-04 12:39:56 +0000148 Can_Encrypt_Backup = false;
149 Use_Userdata_Encryption = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400150 Has_Data_Media = false;
Dees_Troye58d5262012-09-21 12:27:57 -0400151 Has_Android_Secure = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400152 Is_Storage = false;
Dees_Troya13d74f2013-03-24 08:54:55 -0500153 Is_Settings_Storage = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400154 Storage_Path = "";
155 Current_File_System = "";
156 Fstab_File_System = "";
Hashcode62bd9e02013-11-19 21:59:42 -0800157 Mount_Flags = 0;
158 Mount_Options = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400159 Format_Block_Size = 0;
Dees_Troy68cab492012-12-12 19:29:35 +0000160 Ignore_Blkid = false;
Dees_Troy16c2b312013-01-15 16:51:18 +0000161 Retain_Layout_Version = false;
Dees_Troy85f44ed2013-01-09 18:42:36 +0000162#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
163 EcryptFS_Password = "";
164#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400165}
166
167TWPartition::~TWPartition(void) {
168 // Do nothing
169}
170
Dees_Troy5bf43922012-09-07 16:07:55 -0400171bool TWPartition::Process_Fstab_Line(string Line, bool Display_Error) {
172 char full_line[MAX_FSTAB_LINE_LENGTH], item[MAX_FSTAB_LINE_LENGTH];
173 int line_len = Line.size(), index = 0, item_index = 0;
174 char* ptr;
Dees_Troy51127312012-09-08 13:08:49 -0400175 string Flags;
Dees_Troy5bf43922012-09-07 16:07:55 -0400176 strncpy(full_line, Line.c_str(), line_len);
Dees_Troya13d74f2013-03-24 08:54:55 -0500177 bool skip = false;
Dees_Troy5bf43922012-09-07 16:07:55 -0400178
Dees_Troy51127312012-09-08 13:08:49 -0400179 for (index = 0; index < line_len; index++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500180 if (full_line[index] == 34)
181 skip = !skip;
182 if (!skip && full_line[index] <= 32)
Dees_Troy5bf43922012-09-07 16:07:55 -0400183 full_line[index] = '\0';
Dees_Troy5bf43922012-09-07 16:07:55 -0400184 }
Dees_Troy7c2dec82012-09-26 09:49:14 -0400185 Mount_Point = full_line;
Dees_Troy2673cec2013-04-02 20:22:16 +0000186 LOGINFO("Processing '%s'\n", Mount_Point.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -0400187 Backup_Path = Mount_Point;
Dees_Troya13d74f2013-03-24 08:54:55 -0500188 Storage_Path = Mount_Point;
Dees_Troy70737fa2013-04-08 13:19:20 +0000189 Display_Name = full_line + 1;
190 Backup_Display_Name = Display_Name;
191 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400192 index = Mount_Point.size();
193 while (index < line_len) {
194 while (index < line_len && full_line[index] == '\0')
195 index++;
196 if (index >= line_len)
197 continue;
198 ptr = full_line + index;
199 if (item_index == 0) {
200 // File System
201 Fstab_File_System = ptr;
202 Current_File_System = ptr;
203 item_index++;
204 } else if (item_index == 1) {
205 // Primary Block Device
Dees_Troy38bd7602012-09-14 13:33:53 -0400206 if (Fstab_File_System == "mtd" || Fstab_File_System == "yaffs2") {
Dees_Troy094207a2012-09-26 12:00:39 -0400207 MTD_Name = ptr;
208 Find_MTD_Block_Device(MTD_Name);
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400209 } else if (Fstab_File_System == "bml") {
210 if (Mount_Point == "/boot")
211 MTD_Name = "boot";
212 else if (Mount_Point == "/recovery")
213 MTD_Name = "recovery";
214 Primary_Block_Device = ptr;
215 if (*ptr != '/')
Dees_Troy2673cec2013-04-02 20:22:16 +0000216 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 -0400217 } else if (*ptr != '/') {
Dees_Troy5bf43922012-09-07 16:07:55 -0400218 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000219 LOGERR("Invalid block device on '%s', '%s', %i\n", Line.c_str(), ptr, index);
Dees_Troy5bf43922012-09-07 16:07:55 -0400220 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000221 LOGINFO("Invalid block device on '%s', '%s', %i\n", Line.c_str(), ptr, index);
Dees_Troy5bf43922012-09-07 16:07:55 -0400222 return 0;
Dees_Troy38bd7602012-09-14 13:33:53 -0400223 } else {
224 Primary_Block_Device = ptr;
225 Find_Real_Block_Device(Primary_Block_Device, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400226 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400227 item_index++;
228 } else if (item_index > 1) {
229 if (*ptr == '/') {
230 // Alternate Block Device
231 Alternate_Block_Device = ptr;
232 Find_Real_Block_Device(Alternate_Block_Device, Display_Error);
233 } else if (strlen(ptr) > 7 && strncmp(ptr, "length=", 7) == 0) {
234 // Partition length
235 ptr += 7;
236 Length = atoi(ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400237 } else if (strlen(ptr) > 6 && strncmp(ptr, "flags=", 6) == 0) {
238 // Custom flags, save for later so that new values aren't overwritten by defaults
239 ptr += 6;
240 Flags = ptr;
Dees_Troy68cab492012-12-12 19:29:35 +0000241 Process_Flags(Flags, Display_Error);
Dees_Troy38bd7602012-09-14 13:33:53 -0400242 } else if (strlen(ptr) == 4 && (strncmp(ptr, "NULL", 4) == 0 || strncmp(ptr, "null", 4) == 0 || strncmp(ptr, "null", 4) == 0)) {
243 // Do nothing
Dees_Troy5bf43922012-09-07 16:07:55 -0400244 } else {
245 // Unhandled data
Dees_Troy2673cec2013-04-02 20:22:16 +0000246 LOGINFO("Unhandled fstab information: '%s', %i, line: '%s'\n", ptr, index, Line.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400247 }
248 }
249 while (index < line_len && full_line[index] != '\0')
250 index++;
251 }
252
253 if (!Is_File_System(Fstab_File_System) && !Is_Image(Fstab_File_System)) {
254 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000255 LOGERR("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400256 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000257 LOGINFO("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400258 return 0;
259 } else if (Is_File_System(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400260 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400261 Setup_File_System(Display_Error);
262 if (Mount_Point == "/system") {
263 Display_Name = "System";
Dees_Troya13d74f2013-03-24 08:54:55 -0500264 Backup_Display_Name = Display_Name;
265 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400266 Wipe_Available_in_GUI = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500267 Can_Be_Backed_Up = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400268 } else if (Mount_Point == "/data") {
269 Display_Name = "Data";
Dees_Troya13d74f2013-03-24 08:54:55 -0500270 Backup_Display_Name = Display_Name;
271 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400272 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400273 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500274 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000275 Can_Encrypt_Backup = true;
276 Use_Userdata_Encryption = true;
Ethan Yonker6277c792014-09-15 14:54:30 -0500277 if (datamedia)
that9e0593e2014-10-08 00:01:24 +0200278 Setup_Data_Media();
Dees_Troy5bf43922012-09-07 16:07:55 -0400279#ifdef TW_INCLUDE_CRYPTO
280 Can_Be_Encrypted = true;
Ethan Yonker4eca40d2014-11-11 14:52:28 -0600281#ifdef TW_INCLUDE_L_CRYPTO
282 int password_type = cryptfs_get_password_type();
283 if (password_type == CRYPT_TYPE_DEFAULT) {
284 LOGINFO("Device is encrypted with the default password, attempting to decrypt.\n");
285 property_set("ro.crypto.state", "encrypted");
286 if (cryptfs_check_passwd("default_password") == 0) {
287 gui_print("Successfully decrypted with default password.\n");
288 } else {
289 LOGERR("Unable to decrypt with default password.");
290 LOGERR("You may need to perform a Format Data.\n");
291 }
292 } else {
293 DataManager::SetValue("TW_CRYPTO_TYPE", password_type);
294 }
295#endif
Dees_Troy657c3092012-09-10 20:32:10 -0400296 char crypto_blkdev[255];
297 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
298 if (strcmp(crypto_blkdev, "error") != 0) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400299 DataManager::SetValue(TW_DATA_BLK_DEVICE, Primary_Block_Device);
Dees_Troy657c3092012-09-10 20:32:10 -0400300 DataManager::SetValue(TW_IS_DECRYPTED, 1);
301 Is_Encrypted = true;
302 Is_Decrypted = true;
303 Decrypted_Block_Device = crypto_blkdev;
Dees_Troy2673cec2013-04-02 20:22:16 +0000304 LOGINFO("Data already decrypted, new block device: '%s'\n", crypto_blkdev);
Dees_Troy657c3092012-09-10 20:32:10 -0400305 } else if (!Mount(false)) {
Ethan Yonker71413f42014-02-26 13:36:08 -0600306 if (Is_Present) {
Ethan Yonker4eca40d2014-11-11 14:52:28 -0600307#if defined(TW_INCLUDE_JB_CRYPTO) || defined(TW_INCLUDE_L_CRYPTO)
Ethan Yonker71413f42014-02-26 13:36:08 -0600308 // No extra flags needed
309#else
310 property_set("ro.crypto.fs_type", CRYPTO_FS_TYPE);
311 property_set("ro.crypto.fs_real_blkdev", CRYPTO_REAL_BLKDEV);
312 property_set("ro.crypto.fs_mnt_point", CRYPTO_MNT_POINT);
313 property_set("ro.crypto.fs_options", CRYPTO_FS_OPTIONS);
314 property_set("ro.crypto.fs_flags", CRYPTO_FS_FLAGS);
315 property_set("ro.crypto.keyfile.userdata", CRYPTO_KEY_LOC);
316#ifdef CRYPTO_SD_FS_TYPE
317 property_set("ro.crypto.sd_fs_type", CRYPTO_SD_FS_TYPE);
318 property_set("ro.crypto.sd_fs_real_blkdev", CRYPTO_SD_REAL_BLKDEV);
319 property_set("ro.crypto.sd_fs_mnt_point", EXPAND(TW_INTERNAL_STORAGE_PATH));
320#endif
321 property_set("rw.km_fips_status", "ready");
322#endif
323 if (cryptfs_check_footer() == 0) {
324 Is_Encrypted = true;
325 Is_Decrypted = false;
326 Can_Be_Mounted = false;
327 Current_File_System = "emmc";
328 Setup_Image(Display_Error);
329 DataManager::SetValue(TW_IS_ENCRYPTED, 1);
330 DataManager::SetValue(TW_CRYPTO_PASSWORD, "");
331 DataManager::SetValue("tw_crypto_display", "");
332 } else {
333 LOGERR("Could not mount /data and unable to find crypto footer.\n");
334 }
335 } else {
336 LOGERR("Primary block device '%s' for mount point '%s' is not present!\n", Primary_Block_Device.c_str(), Mount_Point.c_str());
337 }
Gary Peck82599a82012-11-21 16:23:12 -0800338 } else {
339 // Filesystem is not encrypted and the mount
340 // succeeded, so get it back to the original
341 // unmounted state
342 UnMount(false);
Dees_Troy51127312012-09-08 13:08:49 -0400343 }
Ethan Yonker6277c792014-09-15 14:54:30 -0500344 if (datamedia && (!Is_Encrypted || (Is_Encrypted && Is_Decrypted)))
Dees_Troy9b21af72012-10-01 15:51:46 -0400345 Recreate_Media_Folder();
Dees_Troy9b21af72012-10-01 15:51:46 -0400346#else
Ethan Yonker6277c792014-09-15 14:54:30 -0500347 if (datamedia)
348 Recreate_Media_Folder();
Dees_Troy5bf43922012-09-07 16:07:55 -0400349#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400350 } else if (Mount_Point == "/cache") {
351 Display_Name = "Cache";
Dees_Troya13d74f2013-03-24 08:54:55 -0500352 Backup_Display_Name = Display_Name;
353 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400354 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400355 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500356 Can_Be_Backed_Up = true;
Dees_Troyce2fe772012-09-28 12:34:33 -0400357 if (Mount(false) && !TWFunc::Path_Exists("/cache/recovery/.")) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000358 LOGINFO("Recreating /cache/recovery folder.\n");
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500359 if (mkdir("/cache/recovery", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0)
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500360 return -1;
Dees_Troyb46a6842012-09-25 11:06:46 -0400361 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400362 } else if (Mount_Point == "/datadata") {
Dees_Troy51127312012-09-08 13:08:49 -0400363 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400364 Display_Name = "DataData";
Dees_Troya13d74f2013-03-24 08:54:55 -0500365 Backup_Display_Name = Display_Name;
366 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400367 Is_SubPartition = true;
368 SubPartition_Of = "/data";
Dees_Troy5bf43922012-09-07 16:07:55 -0400369 DataManager::SetValue(TW_HAS_DATADATA, 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500370 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000371 Can_Encrypt_Backup = true;
372 Use_Userdata_Encryption = false; // This whole partition should be encrypted
Dees_Troy5bf43922012-09-07 16:07:55 -0400373 } else if (Mount_Point == "/sd-ext") {
Dees_Troy51127312012-09-08 13:08:49 -0400374 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400375 Display_Name = "SD-Ext";
Dees_Troya13d74f2013-03-24 08:54:55 -0500376 Backup_Display_Name = Display_Name;
377 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400378 Wipe_Available_in_GUI = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400379 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500380 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000381 Can_Encrypt_Backup = true;
382 Use_Userdata_Encryption = true;
Dees_Troy2c50e182012-09-26 20:05:28 -0400383 } else if (Mount_Point == "/boot") {
384 Display_Name = "Boot";
Dees_Troya13d74f2013-03-24 08:54:55 -0500385 Backup_Display_Name = Display_Name;
Dees_Troy2c50e182012-09-26 20:05:28 -0400386 DataManager::SetValue("tw_boot_is_mountable", 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500387 Can_Be_Backed_Up = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400388 }
389#ifdef TW_EXTERNAL_STORAGE_PATH
390 if (Mount_Point == EXPAND(TW_EXTERNAL_STORAGE_PATH)) {
391 Is_Storage = true;
392 Storage_Path = EXPAND(TW_EXTERNAL_STORAGE_PATH);
Dees_Troyc51f1f92012-09-20 15:32:13 -0400393 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500394 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400395#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000396 if (Mount_Point == "/sdcard" || Mount_Point == "/external_sd" || Mount_Point == "/external_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400397 Is_Storage = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400398 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500399 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400400#endif
Dees_Troyb05ddee2013-01-28 20:24:50 +0000401 }
Dees_Troy8170a922012-09-18 15:40:25 -0400402#ifdef TW_INTERNAL_STORAGE_PATH
403 if (Mount_Point == EXPAND(TW_INTERNAL_STORAGE_PATH)) {
404 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500405 Is_Settings_Storage = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400406 Storage_Path = EXPAND(TW_INTERNAL_STORAGE_PATH);
Dees_Troya13d74f2013-03-24 08:54:55 -0500407 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400408 }
409#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000410 if (Mount_Point == "/emmc" || Mount_Point == "/internal_sd" || Mount_Point == "/internal_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400411 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500412 Is_Settings_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500413 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400414 }
415#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400416 } else if (Is_Image(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400417 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400418 Setup_Image(Display_Error);
Dees_Troya13d74f2013-03-24 08:54:55 -0500419 if (Mount_Point == "/boot") {
420 Display_Name = "Boot";
421 Backup_Display_Name = Display_Name;
422 Can_Be_Backed_Up = true;
423 } else if (Mount_Point == "/recovery") {
424 Display_Name = "Recovery";
425 Backup_Display_Name = Display_Name;
Dees_Troya13d74f2013-03-24 08:54:55 -0500426 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400427 }
428
Dees_Troy51127312012-09-08 13:08:49 -0400429 // Process any custom flags
430 if (Flags.size() > 0)
431 Process_Flags(Flags, Display_Error);
Dees_Troy51127312012-09-08 13:08:49 -0400432 return true;
433}
434
Hashcode62bd9e02013-11-19 21:59:42 -0800435bool TWPartition::Process_FS_Flags(string& Options, int Flags) {
436 int i;
437 char *p;
438 char *savep;
439 char fs_options[250];
440
441 strlcpy(fs_options, Options.c_str(), sizeof(fs_options));
442 Options = "";
443
444 p = strtok_r(fs_options, ",", &savep);
445 while (p) {
446 /* Look for the flag "p" in the flag list "fl"
447 * If not found, the loop exits with fl[i].name being null.
448 */
449 for (i = 0; mount_flags[i].name; i++) {
450 if (strncmp(p, mount_flags[i].name, strlen(mount_flags[i].name)) == 0) {
451 Flags |= mount_flags[i].flag;
452 break;
453 }
454 }
455
456 if (!mount_flags[i].name) {
457 if (Options.size() > 0)
458 Options += ",";
459 Options += p;
460 }
461 p = strtok_r(NULL, ",", &savep);
462 }
463
464 return true;
465}
466
Dees_Troy51127312012-09-08 13:08:49 -0400467bool TWPartition::Process_Flags(string Flags, bool Display_Error) {
468 char flags[MAX_FSTAB_LINE_LENGTH];
Dees_Troya13d74f2013-03-24 08:54:55 -0500469 int flags_len, index = 0, ptr_len;
Dees_Troy51127312012-09-08 13:08:49 -0400470 char* ptr;
Dees_Troya13d74f2013-03-24 08:54:55 -0500471 bool skip = false, has_display_name = false, has_storage_name = false, has_backup_name = false;
Dees_Troy51127312012-09-08 13:08:49 -0400472
473 strcpy(flags, Flags.c_str());
474 flags_len = Flags.size();
475 for (index = 0; index < flags_len; index++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500476 if (flags[index] == 34)
477 skip = !skip;
478 if (!skip && flags[index] == ';')
Dees_Troy51127312012-09-08 13:08:49 -0400479 flags[index] = '\0';
480 }
481
482 index = 0;
483 while (index < flags_len) {
484 while (index < flags_len && flags[index] == '\0')
485 index++;
486 if (index >= flags_len)
487 continue;
488 ptr = flags + index;
Dees_Troya13d74f2013-03-24 08:54:55 -0500489 ptr_len = strlen(ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400490 if (strcmp(ptr, "removable") == 0) {
491 Removable = true;
Ethan Yonker06c3f932014-02-02 22:11:14 -0600492 } else if (strncmp(ptr, "storage", 7) == 0) {
493 if (ptr_len == 7) {
Ethan Yonker06c3f932014-02-02 22:11:14 -0600494 Is_Storage = true;
495 } else if (ptr_len == 9) {
496 ptr += 9;
497 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y') {
498 LOGINFO("storage set to true\n");
499 Is_Storage = true;
500 } else {
501 LOGINFO("storage set to false\n");
502 Is_Storage = false;
503 }
504 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500505 } else if (strcmp(ptr, "settingsstorage") == 0) {
506 Is_Storage = true;
Matt Mowerbf4efa32014-04-14 23:25:26 -0500507 } else if (strcmp(ptr, "andsec") == 0) {
508 Has_Android_Secure = true;
Dees_Troy63c8df72012-09-10 14:02:05 -0400509 } else if (strcmp(ptr, "canbewiped") == 0) {
510 Can_Be_Wiped = true;
Hashcodedabfd492013-08-29 22:45:30 -0700511 } else if (strcmp(ptr, "usermrf") == 0) {
512 Use_Rm_Rf = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500513 } else if (ptr_len > 7 && strncmp(ptr, "backup=", 7) == 0) {
514 ptr += 7;
515 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y')
516 Can_Be_Backed_Up = true;
517 else
518 Can_Be_Backed_Up = false;
Dees_Troy63c8df72012-09-10 14:02:05 -0400519 } else if (strcmp(ptr, "wipeingui") == 0) {
520 Can_Be_Wiped = true;
521 Wipe_Available_in_GUI = true;
522 } else if (strcmp(ptr, "wipeduringfactoryreset") == 0) {
523 Can_Be_Wiped = true;
524 Wipe_Available_in_GUI = true;
525 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500526 } else if (ptr_len > 15 && strncmp(ptr, "subpartitionof=", 15) == 0) {
Dees_Troy2c4c26f2013-01-28 15:26:43 +0000527 ptr += 15;
Dees_Troy51127312012-09-08 13:08:49 -0400528 Is_SubPartition = true;
529 SubPartition_Of = ptr;
Dees_Troy68cab492012-12-12 19:29:35 +0000530 } else if (strcmp(ptr, "ignoreblkid") == 0) {
531 Ignore_Blkid = true;
Dees_Troy16c2b312013-01-15 16:51:18 +0000532 } else if (strcmp(ptr, "retainlayoutversion") == 0) {
533 Retain_Layout_Version = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500534 } else if (ptr_len > 8 && strncmp(ptr, "symlink=", 8) == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400535 ptr += 8;
536 Symlink_Path = ptr;
Dees_Troya13d74f2013-03-24 08:54:55 -0500537 } else if (ptr_len > 8 && strncmp(ptr, "display=", 8) == 0) {
538 has_display_name = true;
Dees_Troy51127312012-09-08 13:08:49 -0400539 ptr += 8;
Dees_Troya13d74f2013-03-24 08:54:55 -0500540 if (*ptr == '\"') ptr++;
Dees_Troy51127312012-09-08 13:08:49 -0400541 Display_Name = ptr;
Dees_Troya13d74f2013-03-24 08:54:55 -0500542 if (Display_Name.substr(Display_Name.size() - 1, 1) == "\"") {
543 Display_Name.resize(Display_Name.size() - 1);
544 }
545 } else if (ptr_len > 11 && strncmp(ptr, "storagename=", 11) == 0) {
546 has_storage_name = true;
547 ptr += 11;
548 if (*ptr == '\"') ptr++;
549 Storage_Name = ptr;
550 if (Storage_Name.substr(Storage_Name.size() - 1, 1) == "\"") {
551 Storage_Name.resize(Storage_Name.size() - 1);
552 }
553 } else if (ptr_len > 11 && strncmp(ptr, "backupname=", 10) == 0) {
554 has_backup_name = true;
555 ptr += 10;
556 if (*ptr == '\"') ptr++;
557 Backup_Display_Name = ptr;
558 if (Backup_Display_Name.substr(Backup_Display_Name.size() - 1, 1) == "\"") {
559 Backup_Display_Name.resize(Backup_Display_Name.size() - 1);
560 }
561 } else if (ptr_len > 10 && strncmp(ptr, "blocksize=", 10) == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400562 ptr += 10;
563 Format_Block_Size = atoi(ptr);
Dees_Troya13d74f2013-03-24 08:54:55 -0500564 } else if (ptr_len > 7 && strncmp(ptr, "length=", 7) == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400565 ptr += 7;
566 Length = atoi(ptr);
Dees_Troy83bd4832013-05-04 12:39:56 +0000567 } else if (ptr_len > 17 && strncmp(ptr, "canencryptbackup=", 17) == 0) {
568 ptr += 17;
569 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y')
570 Can_Encrypt_Backup = true;
571 else
572 Can_Encrypt_Backup = false;
573 } else if (ptr_len > 21 && strncmp(ptr, "userdataencryptbackup=", 21) == 0) {
574 ptr += 21;
575 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y') {
576 Can_Encrypt_Backup = true;
577 Use_Userdata_Encryption = true;
578 } else {
579 Use_Userdata_Encryption = false;
580 }
Hashcode62bd9e02013-11-19 21:59:42 -0800581 } else if (ptr_len > 8 && strncmp(ptr, "fsflags=", 8) == 0) {
582 ptr += 8;
583 if (*ptr == '\"') ptr++;
584
585 Mount_Options = ptr;
586 if (Mount_Options.substr(Mount_Options.size() - 1, 1) == "\"") {
587 Mount_Options.resize(Mount_Options.size() - 1);
588 }
589 Process_FS_Flags(Mount_Options, Mount_Flags);
Dees_Troy51127312012-09-08 13:08:49 -0400590 } else {
591 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000592 LOGERR("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400593 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000594 LOGINFO("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400595 }
596 while (index < flags_len && flags[index] != '\0')
597 index++;
598 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500599 if (has_display_name && !has_storage_name)
600 Storage_Name = Display_Name;
601 if (!has_display_name && has_storage_name)
602 Display_Name = Storage_Name;
Dees_Troy74fb2e92013-04-15 14:35:47 +0000603 if (has_display_name && !has_backup_name && Backup_Display_Name != "Android Secure")
Dees_Troya13d74f2013-03-24 08:54:55 -0500604 Backup_Display_Name = Display_Name;
605 if (!has_display_name && has_backup_name)
606 Display_Name = Backup_Display_Name;
Dees_Troy51127312012-09-08 13:08:49 -0400607 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400608}
609
Dees_Troy5bf43922012-09-07 16:07:55 -0400610bool TWPartition::Is_File_System(string File_System) {
611 if (File_System == "ext2" ||
Dees_Troy63c8df72012-09-10 14:02:05 -0400612 File_System == "ext3" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400613 File_System == "ext4" ||
614 File_System == "vfat" ||
615 File_System == "ntfs" ||
616 File_System == "yaffs2" ||
bigbiff bigbiff3e146522012-11-14 14:32:59 -0500617 File_System == "exfat" ||
Dees_Troye5017042013-08-29 16:38:55 +0000618 File_System == "f2fs" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400619 File_System == "auto")
620 return true;
621 else
622 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400623}
624
Dees_Troy5bf43922012-09-07 16:07:55 -0400625bool TWPartition::Is_Image(string File_System) {
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400626 if (File_System == "emmc" || File_System == "mtd" || File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -0400627 return true;
628 else
629 return false;
630}
631
Dees_Troy51127312012-09-08 13:08:49 -0400632bool TWPartition::Make_Dir(string Path, bool Display_Error) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400633 if (!TWFunc::Path_Exists(Path)) {
Dees_Troy51127312012-09-08 13:08:49 -0400634 if (mkdir(Path.c_str(), 0777) == -1) {
635 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000636 LOGERR("Can not create '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400637 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000638 LOGINFO("Can not create '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400639 return false;
640 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000641 LOGINFO("Created '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400642 return true;
643 }
644 }
645 return true;
646}
647
Dees_Troy5bf43922012-09-07 16:07:55 -0400648void TWPartition::Setup_File_System(bool Display_Error) {
649 struct statfs st;
650
651 Can_Be_Mounted = true;
652 Can_Be_Wiped = true;
653
Dees_Troy5bf43922012-09-07 16:07:55 -0400654 // Make the mount point folder if it doesn't exist
Dees_Troy51127312012-09-08 13:08:49 -0400655 Make_Dir(Mount_Point, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400656 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
657 Backup_Name = Display_Name;
658 Backup_Method = FILES;
659}
660
661void TWPartition::Setup_Image(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400662 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
663 Backup_Name = Display_Name;
Gary Peck82599a82012-11-21 16:23:12 -0800664 if (Current_File_System == "emmc")
Dees_Troy5bf43922012-09-07 16:07:55 -0400665 Backup_Method = DD;
Gary Peck82599a82012-11-21 16:23:12 -0800666 else if (Current_File_System == "mtd" || Current_File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -0400667 Backup_Method = FLASH_UTILS;
668 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000669 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 -0400670 if (Find_Partition_Size()) {
671 Used = Size;
672 Backup_Size = Size;
Dees_Troy51a0e822012-09-05 15:24:24 -0400673 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400674 if (Display_Error)
Dees Troyd932ce12013-10-18 17:12:59 +0000675 LOGERR("Unable to find partition size for '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400676 else
Dees Troyd932ce12013-10-18 17:12:59 +0000677 LOGINFO("Unable to find partition size for '%s'\n", Mount_Point.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400678 }
679}
680
Dees_Troye58d5262012-09-21 12:27:57 -0400681void TWPartition::Setup_AndSec(void) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500682 Backup_Display_Name = "Android Secure";
Dees_Troye58d5262012-09-21 12:27:57 -0400683 Backup_Name = "and-sec";
Dees_Troya13d74f2013-03-24 08:54:55 -0500684 Can_Be_Backed_Up = true;
Dees_Troye58d5262012-09-21 12:27:57 -0400685 Has_Android_Secure = true;
686 Symlink_Path = Mount_Point + "/.android_secure";
687 Symlink_Mount_Point = "/and-sec";
688 Backup_Path = Symlink_Mount_Point;
689 Make_Dir("/and-sec", true);
690 Recreate_AndSec_Folder();
Ethan Yonkerd4d10732014-02-03 15:27:52 -0600691 Mount_Storage_Retry();
Dees_Troye58d5262012-09-21 12:27:57 -0400692}
693
that9e0593e2014-10-08 00:01:24 +0200694void TWPartition::Setup_Data_Media() {
Ethan Yonker6277c792014-09-15 14:54:30 -0500695 LOGINFO("Setting up '%s' as data/media emulated storage.\n", Mount_Point.c_str());
696 Storage_Name = "Internal Storage";
697 Has_Data_Media = true;
698 Is_Storage = true;
699 Is_Settings_Storage = true;
700 Storage_Path = "/data/media";
701 Symlink_Path = Storage_Path;
702 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
703 Make_Dir("/emmc", false);
704 Symlink_Mount_Point = "/emmc";
705 } else {
706 Make_Dir("/sdcard", false);
707 Symlink_Mount_Point = "/sdcard";
708 }
709 if (Mount(false) && TWFunc::Path_Exists("/data/media/0")) {
710 Storage_Path = "/data/media/0";
711 Symlink_Path = Storage_Path;
712 DataManager::SetValue(TW_INTERNAL_PATH, "/data/media/0");
713 UnMount(true);
714 }
Ethan Yonker6277c792014-09-15 14:54:30 -0500715 DataManager::SetValue("tw_has_internal", 1);
716 DataManager::SetValue("tw_has_data_media", 1);
717 du.add_absolute_dir("/data/media");
718}
719
Dees_Troy5bf43922012-09-07 16:07:55 -0400720void TWPartition::Find_Real_Block_Device(string& Block, bool Display_Error) {
721 char device[512], realDevice[512];
722
723 strcpy(device, Block.c_str());
724 memset(realDevice, 0, sizeof(realDevice));
725 while (readlink(device, realDevice, sizeof(realDevice)) > 0)
726 {
727 strcpy(device, realDevice);
728 memset(realDevice, 0, sizeof(realDevice));
729 }
730
731 if (device[0] != '/') {
732 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000733 LOGERR("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400734 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000735 LOGINFO("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400736 return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400737 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400738 Block = device;
739 return;
740 }
741}
742
Dees_Troy8e337f32012-10-13 22:07:49 -0400743void TWPartition::Mount_Storage_Retry(void) {
744 // On some devices, storage doesn't want to mount right away, retry and sleep
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500745 if (!Mount(true)) {
Dees_Troy8e337f32012-10-13 22:07:49 -0400746 int retry_count = 5;
747 while (retry_count > 0 && !Mount(false)) {
748 usleep(500000);
749 retry_count--;
750 }
751 Mount(true);
752 }
753}
754
Dees_Troy38bd7602012-09-14 13:33:53 -0400755bool TWPartition::Find_MTD_Block_Device(string MTD_Name) {
756 FILE *fp = NULL;
757 char line[255];
758
759 fp = fopen("/proc/mtd", "rt");
760 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000761 LOGERR("Device does not support /proc/mtd\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400762 return false;
763 }
764
765 while (fgets(line, sizeof(line), fp) != NULL)
766 {
767 char device[32], label[32];
768 unsigned long size = 0;
769 char* fstype = NULL;
770 int deviceId;
771
772 sscanf(line, "%s %lx %*s %*c%s", device, &size, label);
773
774 // Skip header and blank lines
775 if ((strcmp(device, "dev:") == 0) || (strlen(line) < 8))
776 continue;
777
778 // Strip off the trailing " from the label
779 label[strlen(label)-1] = '\0';
780
781 if (strcmp(label, MTD_Name.c_str()) == 0) {
782 // We found our device
783 // Strip off the trailing : from the device
784 device[strlen(device)-1] = '\0';
785 if (sscanf(device,"mtd%d", &deviceId) == 1) {
786 sprintf(device, "/dev/block/mtdblock%d", deviceId);
787 Primary_Block_Device = device;
Dees_Troy76543db2013-06-19 16:24:30 +0000788 fclose(fp);
789 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -0400790 }
791 }
792 }
793 fclose(fp);
794
795 return false;
796}
797
Dees_Troy51127312012-09-08 13:08:49 -0400798bool TWPartition::Get_Size_Via_statfs(bool Display_Error) {
799 struct statfs st;
800 string Local_Path = Mount_Point + "/.";
801
802 if (!Mount(Display_Error))
803 return false;
804
805 if (statfs(Local_Path.c_str(), &st) != 0) {
806 if (!Removable) {
807 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000808 LOGERR("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400809 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000810 LOGINFO("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400811 }
812 return false;
813 }
814 Size = (st.f_blocks * st.f_bsize);
815 Used = ((st.f_blocks - st.f_bfree) * st.f_bsize);
816 Free = (st.f_bfree * st.f_bsize);
817 Backup_Size = Used;
818 return true;
819}
820
821bool TWPartition::Get_Size_Via_df(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400822 FILE* fp;
823 char command[255], line[512];
824 int include_block = 1;
825 unsigned int min_len;
826
827 if (!Mount(Display_Error))
828 return false;
829
Dees_Troy38bd7602012-09-14 13:33:53 -0400830 min_len = Actual_Block_Device.size() + 2;
Dees_Troy51127312012-09-08 13:08:49 -0400831 sprintf(command, "df %s > /tmp/dfoutput.txt", Mount_Point.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +0200832 TWFunc::Exec_Cmd(command);
Dees_Troy51127312012-09-08 13:08:49 -0400833 fp = fopen("/tmp/dfoutput.txt", "rt");
834 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000835 LOGINFO("Unable to open /tmp/dfoutput.txt.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400836 return false;
Dees_Troy51127312012-09-08 13:08:49 -0400837 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400838
839 while (fgets(line, sizeof(line), fp) != NULL)
840 {
841 unsigned long blocks, used, available;
842 char device[64];
843 char tmpString[64];
844
845 if (strncmp(line, "Filesystem", 10) == 0)
846 continue;
847 if (strlen(line) < min_len) {
848 include_block = 0;
849 continue;
850 }
851 if (include_block) {
852 sscanf(line, "%s %lu %lu %lu", device, &blocks, &used, &available);
853 } else {
854 // The device block string is so long that the df information is on the next line
855 int space_count = 0;
Dees_Troye58d5262012-09-21 12:27:57 -0400856 sprintf(tmpString, "/dev/block/%s", Actual_Block_Device.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400857 while (tmpString[space_count] == 32)
858 space_count++;
859 sscanf(line + space_count, "%lu %lu %lu", &blocks, &used, &available);
860 }
861
862 // Adjust block size to byte size
863 Size = blocks * 1024ULL;
864 Used = used * 1024ULL;
865 Free = available * 1024ULL;
866 Backup_Size = Used;
867 }
868 fclose(fp);
869 return true;
870}
871
Dees_Troy5bf43922012-09-07 16:07:55 -0400872bool TWPartition::Find_Partition_Size(void) {
873 FILE* fp;
874 char line[512];
875 string tmpdevice;
876
igoriok87e3d932013-01-31 21:03:53 +0200877 fp = fopen("/proc/dumchar_info", "rt");
878 if (fp != NULL) {
879 while (fgets(line, sizeof(line), fp) != NULL)
880 {
881 char label[32], device[32];
882 unsigned long size = 0;
883
thatd43bf2d2014-09-21 23:13:02 +0200884 sscanf(line, "%s %lx %*x %*u %s", label, &size, device);
igoriok87e3d932013-01-31 21:03:53 +0200885
bigbiff bigbiff34684ff2013-12-01 21:03:45 -0500886 // Skip header, annotation and blank lines
igoriok87e3d932013-01-31 21:03:53 +0200887 if ((strncmp(device, "/dev/", 5) != 0) || (strlen(line) < 8))
888 continue;
889
890 tmpdevice = "/dev/";
891 tmpdevice += label;
892 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
893 Size = size;
894 fclose(fp);
895 return true;
896 }
897 }
898 }
899
Dees_Troy5bf43922012-09-07 16:07:55 -0400900 // In this case, we'll first get the partitions we care about (with labels)
901 fp = fopen("/proc/partitions", "rt");
902 if (fp == NULL)
903 return false;
904
905 while (fgets(line, sizeof(line), fp) != NULL)
906 {
907 unsigned long major, minor, blocks;
908 char device[512];
909 char tmpString[64];
910
Dees_Troy63c8df72012-09-10 14:02:05 -0400911 if (strlen(line) < 7 || line[0] == 'm') continue;
Dees_Troy5bf43922012-09-07 16:07:55 -0400912 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
913
914 tmpdevice = "/dev/block/";
915 tmpdevice += device;
Dees_Troy38bd7602012-09-14 13:33:53 -0400916 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400917 // Adjust block size to byte size
918 Size = blocks * 1024ULL;
919 fclose(fp);
920 return true;
921 }
922 }
923 fclose(fp);
924 return false;
925}
926
Dees_Troy5bf43922012-09-07 16:07:55 -0400927bool TWPartition::Is_Mounted(void) {
928 if (!Can_Be_Mounted)
929 return false;
930
931 struct stat st1, st2;
932 string test_path;
933
934 // Check to see if the mount point directory exists
935 test_path = Mount_Point + "/.";
936 if (stat(test_path.c_str(), &st1) != 0) return false;
937
938 // Check to see if the directory above the mount point exists
939 test_path = Mount_Point + "/../.";
940 if (stat(test_path.c_str(), &st2) != 0) return false;
941
942 // Compare the device IDs -- if they match then we're (probably) using tmpfs instead of an actual device
943 int ret = (st1.st_dev != st2.st_dev) ? true : false;
944
945 return ret;
946}
947
948bool TWPartition::Mount(bool Display_Error) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000949 int exfat_mounted = 0;
950
Dees_Troy5bf43922012-09-07 16:07:55 -0400951 if (Is_Mounted()) {
952 return true;
953 } else if (!Can_Be_Mounted) {
954 return false;
955 }
Dees_Troy38bd7602012-09-14 13:33:53 -0400956
957 Find_Actual_Block_Device();
958
959 // Check the current file system before mounting
960 Check_FS_Type();
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000961 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/exfat-fuse")) {
Dees_Troye34c1332013-02-06 19:13:00 +0000962 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 +0000963 LOGINFO("cmd: %s\n", cmd.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000964 string result;
965 if (TWFunc::Exec_Cmd(cmd, result) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000966 LOGINFO("exfat-fuse failed to mount with result '%s', trying vfat\n", result.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000967 Current_File_System = "vfat";
968 } else {
969#ifdef TW_NO_EXFAT_FUSE
970 UnMount(false);
971 // We'll let the kernel handle it but using exfat-fuse to detect if the file system is actually exfat
972 // Some kernels let us mount vfat as exfat which doesn't work out too well
973#else
974 exfat_mounted = 1;
975#endif
976 }
977 }
Dees_Troy22042032012-12-18 21:23:08 +0000978 if (Fstab_File_System == "yaffs2") {
979 // mount an MTD partition as a YAFFS2 filesystem.
Dees_Troy76543db2013-06-19 16:24:30 +0000980 const unsigned long flags = MS_NOATIME | MS_NODEV | MS_NODIRATIME;
981 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags, NULL) < 0) {
982 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags | MS_RDONLY, NULL) < 0) {
983 if (Display_Error)
984 LOGERR("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
985 else
986 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
987 return false;
988 } else {
989 LOGINFO("Mounted '%s' (MTD) as RO\n", Mount_Point.c_str());
990 return true;
991 }
992 } else {
993 struct stat st;
994 string test_path = Mount_Point;
995 if (stat(test_path.c_str(), &st) < 0) {
996 if (Display_Error)
997 LOGERR("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
998 else
999 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
1000 return false;
1001 }
1002 mode_t new_mode = st.st_mode | S_IXUSR | S_IXGRP | S_IXOTH;
1003 if (new_mode != st.st_mode) {
1004 LOGINFO("Fixing execute permissions for %s\n", Mount_Point.c_str());
1005 if (chmod(Mount_Point.c_str(), new_mode) < 0) {
1006 if (Display_Error)
1007 LOGERR("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
1008 else
1009 LOGINFO("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
1010 return false;
1011 }
1012 }
Dees_Troy22042032012-12-18 21:23:08 +00001013 return true;
Dees_Troy76543db2013-06-19 16:24:30 +00001014 }
Dees Troy216e0422014-02-07 03:46:42 +00001015 } else if (!exfat_mounted && mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Current_File_System.c_str(), Mount_Flags, Mount_Options.c_str()) != 0 && mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Current_File_System.c_str(), Mount_Flags, NULL) != 0) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001016#ifdef TW_NO_EXFAT_FUSE
1017 if (Current_File_System == "exfat") {
Dees_Troy2673cec2013-04-02 20:22:16 +00001018 LOGINFO("Mounting exfat failed, trying vfat...\n");
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001019 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), "vfat", 0, NULL) != 0) {
Dees_Troy85f44ed2013-01-09 18:42:36 +00001020 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001021 LOGERR("Unable to mount '%s'\n", Mount_Point.c_str());
Dees_Troy85f44ed2013-01-09 18:42:36 +00001022 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001023 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
Hashcode62bd9e02013-11-19 21:59:42 -08001024 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(), Mount_Flags, Mount_Options.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001025 return false;
Dees_Troy85f44ed2013-01-09 18:42:36 +00001026 }
Dees_Troyb05ddee2013-01-28 20:24:50 +00001027 } else {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001028#endif
bigbiff bigbiff26774a02014-03-29 18:22:00 -04001029 if (!Removable && Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001030 LOGERR("Unable to mount '%s'\n", Mount_Point.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001031 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001032 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
1033 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 +00001034 return false;
1035#ifdef TW_NO_EXFAT_FUSE
Dees_Troy85f44ed2013-01-09 18:42:36 +00001036 }
1037#endif
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001038 }
1039#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
1040 string MetaEcfsFile = EXPAND(TW_EXTERNAL_STORAGE_PATH);
1041 MetaEcfsFile += "/.MetaEcfsFile";
1042 if (EcryptFS_Password.size() > 0 && PartitionManager.Mount_By_Path("/data", false) && TWFunc::Path_Exists(MetaEcfsFile)) {
1043 if (mount_ecryptfs_drive(EcryptFS_Password.c_str(), Mount_Point.c_str(), Mount_Point.c_str(), 0) != 0) {
1044 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001045 LOGERR("Unable to mount ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001046 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001047 LOGINFO("Unable to mount ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001048 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001049 LOGINFO("Successfully mounted ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001050 Is_Decrypted = true;
Dees_Troy51127312012-09-08 13:08:49 -04001051 }
Dees_Troy066eb302013-08-23 17:20:32 +00001052 } else if (Mount_Point == EXPAND(TW_EXTERNAL_STORAGE_PATH)) {
1053 if (Is_Decrypted)
1054 LOGINFO("Mounting external storage, '%s' is not encrypted\n", Mount_Point.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001055 Is_Decrypted = false;
1056 }
1057#endif
1058 if (Removable)
1059 Update_Size(Display_Error);
1060
1061 if (!Symlink_Mount_Point.empty()) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001062 string Command = "mount '" + Symlink_Path + "' '" + Symlink_Mount_Point + "'";
1063 TWFunc::Exec_Cmd(Command);
Dees_Troy5bf43922012-09-07 16:07:55 -04001064 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001065 return true;
1066}
1067
1068bool TWPartition::UnMount(bool Display_Error) {
1069 if (Is_Mounted()) {
1070 int never_unmount_system;
1071
1072 DataManager::GetValue(TW_DONT_UNMOUNT_SYSTEM, never_unmount_system);
1073 if (never_unmount_system == 1 && Mount_Point == "/system")
1074 return true; // Never unmount system if you're not supposed to unmount it
1075
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001076 if (Is_Storage)
1077 TWFunc::Toggle_MTP(false);
1078
Dees_Troyc8bafa12013-01-10 15:43:00 +00001079#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
1080 if (EcryptFS_Password.size() > 0) {
1081 if (unmount_ecryptfs_drive(Mount_Point.c_str()) != 0) {
1082 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001083 LOGERR("Unable to unmount ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troyc8bafa12013-01-10 15:43:00 +00001084 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001085 LOGINFO("Unable to unmount ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troyc8bafa12013-01-10 15:43:00 +00001086 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001087 LOGINFO("Successfully unmounted ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troyc8bafa12013-01-10 15:43:00 +00001088 }
1089 }
1090#endif
1091
Dees_Troy38bd7602012-09-14 13:33:53 -04001092 if (!Symlink_Mount_Point.empty())
1093 umount(Symlink_Mount_Point.c_str());
1094
Dees_Troyb05ddee2013-01-28 20:24:50 +00001095 umount(Mount_Point.c_str());
1096 if (Is_Mounted()) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001097 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001098 LOGERR("Unable to unmount '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001099 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001100 LOGINFO("Unable to unmount '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001101 return false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001102 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -04001103 return true;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001104 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001105 } else {
1106 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001107 }
1108}
1109
Gary Peck43acadf2012-11-21 21:19:01 -08001110bool TWPartition::Wipe(string New_File_System) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001111 bool wiped = false, update_crypt = false, recreate_media = true, mtp_toggle = true;
Dees_Troy16c2b312013-01-15 16:51:18 +00001112 int check;
1113 string Layout_Filename = Mount_Point + "/.layout_version";
1114
Dees_Troy38bd7602012-09-14 13:33:53 -04001115 if (!Can_Be_Wiped) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001116 LOGERR("Partition '%s' cannot be wiped.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001117 return false;
1118 }
1119
Dees_Troyc51f1f92012-09-20 15:32:13 -04001120 if (Mount_Point == "/cache")
Dees_Troy2673cec2013-04-02 20:22:16 +00001121 Log_Offset = 0;
Dees_Troyc51f1f92012-09-20 15:32:13 -04001122
Dees_Troyce675462013-01-09 19:48:21 +00001123#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
1124 if (Mount_Point == "/data" && Mount(false)) {
1125 if (TWFunc::Path_Exists("/data/system/edk_p_sd"))
1126 TWFunc::copy_file("/data/system/edk_p_sd", "/tmp/edk_p_sd", 0600);
1127 }
1128#endif
1129
Dees_Troy16c2b312013-01-15 16:51:18 +00001130 if (Retain_Layout_Version && Mount(false) && TWFunc::Path_Exists(Layout_Filename))
1131 TWFunc::copy_file(Layout_Filename, "/.layout_version", 0600);
1132 else
1133 unlink("/.layout_version");
Dees_Troy38bd7602012-09-14 13:33:53 -04001134
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001135 if (Has_Data_Media && Current_File_System == New_File_System) {
Dees_Troy16c2b312013-01-15 16:51:18 +00001136 wiped = Wipe_Data_Without_Wiping_Media();
Ethan Yonker5eac2222014-06-11 12:22:55 -05001137 recreate_media = false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001138 mtp_toggle = false;
Dees_Troy16c2b312013-01-15 16:51:18 +00001139 } else {
Dees_Troy16c2b312013-01-15 16:51:18 +00001140 DataManager::GetValue(TW_RM_RF_VAR, check);
1141
Hashcodedabfd492013-08-29 22:45:30 -07001142 if (check || Use_Rm_Rf)
Dees_Troy16c2b312013-01-15 16:51:18 +00001143 wiped = Wipe_RMRF();
1144 else if (New_File_System == "ext4")
1145 wiped = Wipe_EXT4();
1146 else if (New_File_System == "ext2" || New_File_System == "ext3")
1147 wiped = Wipe_EXT23(New_File_System);
1148 else if (New_File_System == "vfat")
1149 wiped = Wipe_FAT();
1150 else if (New_File_System == "exfat")
1151 wiped = Wipe_EXFAT();
1152 else if (New_File_System == "yaffs2")
1153 wiped = Wipe_MTD();
Dees_Troye5017042013-08-29 16:38:55 +00001154 else if (New_File_System == "f2fs")
1155 wiped = Wipe_F2FS();
Dees_Troy16c2b312013-01-15 16:51:18 +00001156 else {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001157 if (Is_Storage) {
1158 TWFunc::Toggle_MTP(true);
1159 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001160 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 +00001161 unlink("/.layout_version");
1162 return false;
1163 }
1164 update_crypt = wiped;
Gary Pecke8bc5d72012-12-21 06:45:25 -08001165 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001166
Gary Pecke8bc5d72012-12-21 06:45:25 -08001167 if (wiped) {
Dees_Troyce675462013-01-09 19:48:21 +00001168#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
1169 if (Mount_Point == "/data" && Mount(false)) {
1170 if (TWFunc::Path_Exists("/tmp/edk_p_sd")) {
1171 Make_Dir("/data/system", true);
1172 TWFunc::copy_file("/tmp/edk_p_sd", "/data/system/edk_p_sd", 0600);
1173 }
1174 }
1175#endif
Dees_Troy16c2b312013-01-15 16:51:18 +00001176
Dees_Troy1c1ac442013-01-17 21:42:14 +00001177 if (Mount_Point == "/cache")
1178 DataManager::Output_Version();
1179
Dees_Troy16c2b312013-01-15 16:51:18 +00001180 if (TWFunc::Path_Exists("/.layout_version") && Mount(false))
1181 TWFunc::copy_file("/.layout_version", Layout_Filename, 0600);
1182
1183 if (update_crypt) {
1184 Setup_File_System(false);
1185 if (Is_Encrypted && !Is_Decrypted) {
1186 // just wiped an encrypted partition back to its unencrypted state
1187 Is_Encrypted = false;
1188 Is_Decrypted = false;
1189 Decrypted_Block_Device = "";
1190 if (Mount_Point == "/data") {
1191 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1192 DataManager::SetValue(TW_IS_DECRYPTED, 0);
1193 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001194 }
1195 }
Ethan Yonker5eac2222014-06-11 12:22:55 -05001196
1197 if (Mount_Point == "/data" && Has_Data_Media && recreate_media) {
1198 Recreate_Media_Folder();
1199 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001200 }
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001201 if (Is_Storage && mtp_toggle) {
1202 TWFunc::Toggle_MTP(true);
1203 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001204 return wiped;
Dees_Troy51a0e822012-09-05 15:24:24 -04001205}
1206
Gary Peck43acadf2012-11-21 21:19:01 -08001207bool TWPartition::Wipe() {
Gary Peck82599a82012-11-21 16:23:12 -08001208 if (Is_File_System(Current_File_System))
1209 return Wipe(Current_File_System);
1210 else
1211 return Wipe(Fstab_File_System);
Gary Peck43acadf2012-11-21 21:19:01 -08001212}
1213
Dees_Troye58d5262012-09-21 12:27:57 -04001214bool TWPartition::Wipe_AndSec(void) {
1215 if (!Has_Android_Secure)
1216 return false;
1217
Dees_Troye58d5262012-09-21 12:27:57 -04001218 if (!Mount(true))
1219 return false;
1220
Dees_Troy2673cec2013-04-02 20:22:16 +00001221 gui_print("Wiping %s\n", Backup_Display_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001222 TWFunc::removeDir(Mount_Point + "/.android_secure/", true);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001223 return true;
Dees_Troye58d5262012-09-21 12:27:57 -04001224}
1225
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001226bool TWPartition::Can_Repair() {
1227 if (Current_File_System == "vfat" && TWFunc::Path_Exists("/sbin/dosfsck"))
1228 return true;
1229 else if ((Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") && TWFunc::Path_Exists("/sbin/e2fsck"))
1230 return true;
1231 else if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/fsck.exfat"))
1232 return true;
1233 else if (Current_File_System == "f2fs" && TWFunc::Path_Exists("/sbin/fsck.f2fs"))
1234 return true;
1235 return false;
1236}
1237
1238bool TWPartition::Repair() {
1239 string command;
1240
1241 if (Current_File_System == "vfat") {
1242 if (!TWFunc::Path_Exists("/sbin/dosfsck")) {
1243 gui_print("dosfsck does not exist! Cannot repair!\n");
1244 return false;
1245 }
1246 if (!UnMount(true))
1247 return false;
1248 gui_print("Repairing %s using dosfsck...\n", Display_Name.c_str());
1249 Find_Actual_Block_Device();
1250 command = "/sbin/dosfsck -y " + Actual_Block_Device;
1251 LOGINFO("Repair command: %s\n", command.c_str());
1252 if (TWFunc::Exec_Cmd(command) == 0) {
1253 gui_print("Done.\n");
1254 return true;
1255 } else {
1256 LOGERR("Unable to repair '%s'.\n", Mount_Point.c_str());
1257 return false;
1258 }
1259 }
1260 if (Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") {
1261 if (!TWFunc::Path_Exists("/sbin/e2fsck")) {
1262 gui_print("e2fsck does not exist! Cannot repair!\n");
1263 return false;
1264 }
1265 if (!UnMount(true))
1266 return false;
1267 gui_print("Repairing %s using e2fsck...\n", Display_Name.c_str());
1268 Find_Actual_Block_Device();
1269 command = "/sbin/e2fsck -p " + Actual_Block_Device;
1270 LOGINFO("Repair command: %s\n", command.c_str());
1271 if (TWFunc::Exec_Cmd(command) == 0) {
1272 gui_print("Done.\n");
1273 return true;
1274 } else {
1275 LOGERR("Unable to repair '%s'.\n", Mount_Point.c_str());
1276 return false;
1277 }
1278 }
1279 if (Current_File_System == "exfat") {
1280 if (!TWFunc::Path_Exists("/sbin/fsck.exfat")) {
1281 gui_print("fsck.exfat does not exist! Cannot repair!\n");
1282 return false;
1283 }
1284 if (!UnMount(true))
1285 return false;
1286 gui_print("Repairing %s using fsck.exfat...\n", Display_Name.c_str());
1287 Find_Actual_Block_Device();
1288 command = "/sbin/fsck.exfat " + Actual_Block_Device;
1289 LOGINFO("Repair command: %s\n", command.c_str());
1290 if (TWFunc::Exec_Cmd(command) == 0) {
1291 gui_print("Done.\n");
1292 return true;
1293 } else {
1294 LOGERR("Unable to repair '%s'.\n", Mount_Point.c_str());
1295 return false;
1296 }
1297 }
1298 if (Current_File_System == "f2fs") {
1299 if (!TWFunc::Path_Exists("/sbin/fsck.f2fs")) {
1300 gui_print("fsck.f2fs does not exist! Cannot repair!\n");
1301 return false;
1302 }
1303 if (!UnMount(true))
1304 return false;
1305 gui_print("Repairing %s using fsck.f2fs...\n", Display_Name.c_str());
1306 Find_Actual_Block_Device();
1307 command = "/sbin/fsck.f2fs " + Actual_Block_Device;
1308 LOGINFO("Repair command: %s\n", command.c_str());
1309 if (TWFunc::Exec_Cmd(command) == 0) {
1310 gui_print("Done.\n");
1311 return true;
1312 } else {
1313 LOGERR("Unable to repair '%s'.\n", Mount_Point.c_str());
1314 return false;
1315 }
1316 }
1317 return false;
1318}
1319
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001320bool TWPartition::Backup(string backup_folder, const unsigned long long *overall_size, const unsigned long long *other_backups_size) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001321 if (Backup_Method == FILES)
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001322 return Backup_Tar(backup_folder, overall_size, other_backups_size);
Dees_Troy38bd7602012-09-14 13:33:53 -04001323 else if (Backup_Method == DD)
1324 return Backup_DD(backup_folder);
1325 else if (Backup_Method == FLASH_UTILS)
1326 return Backup_Dump_Image(backup_folder);
Dees_Troy2673cec2013-04-02 20:22:16 +00001327 LOGERR("Unknown backup method for '%s'\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001328 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001329}
1330
Dees_Troy43d8b002012-09-17 16:00:01 -04001331bool TWPartition::Check_MD5(string restore_folder) {
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001332 string Full_Filename, md5file;
Dees_Troy43d8b002012-09-17 16:00:01 -04001333 char split_filename[512];
1334 int index = 0;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001335 twrpDigest md5sum;
Dees_Troy43d8b002012-09-17 16:00:01 -04001336
Captain Throwbackff5935f2014-09-23 16:08:34 -04001337 sync();
1338
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001339 memset(split_filename, 0, sizeof(split_filename));
Dees_Troy43d8b002012-09-17 16:00:01 -04001340 Full_Filename = restore_folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04001341 if (!TWFunc::Path_Exists(Full_Filename)) {
1342 // This is a split archive, we presume
1343 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
Dees_Troy2673cec2013-04-02 20:22:16 +00001344 LOGINFO("split_filename: %s\n", split_filename);
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001345 md5file = split_filename;
1346 md5file += ".md5";
1347 if (!TWFunc::Path_Exists(md5file)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001348 LOGERR("No md5 file found for '%s'.\n", split_filename);
1349 LOGERR("Please unselect Enable MD5 verification to restore.\n");
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001350 return false;
1351 }
1352 md5sum.setfn(split_filename);
Dees_Troy83bd4832013-05-04 12:39:56 +00001353 while (index < 1000) {
1354 if (TWFunc::Path_Exists(split_filename) && md5sum.verify_md5digest() != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001355 LOGERR("MD5 failed to match on '%s'.\n", split_filename);
Dees_Troy43d8b002012-09-17 16:00:01 -04001356 return false;
1357 }
1358 index++;
1359 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001360 md5sum.setfn(split_filename);
Dees_Troy43d8b002012-09-17 16:00:01 -04001361 }
Dees_Troy4a2a1262012-09-18 09:33:47 -04001362 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001363 } else {
1364 // Single file archive
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001365 md5file = Full_Filename + ".md5";
1366 if (!TWFunc::Path_Exists(md5file)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001367 LOGERR("No md5 file found for '%s'.\n", Full_Filename.c_str());
1368 LOGERR("Please unselect Enable MD5 verification to restore.\n");
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001369 return false;
1370 }
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001371 md5sum.setfn(Full_Filename);
1372 if (md5sum.verify_md5digest() != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001373 LOGERR("MD5 failed to match on '%s'.\n", Full_Filename.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001374 return false;
1375 } else
1376 return true;
1377 }
1378 return false;
1379}
1380
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001381bool 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 -08001382 string Restore_File_System;
1383
1384 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
Dees_Troy2673cec2013-04-02 20:22:16 +00001385 LOGINFO("Restore filename is: %s\n", Backup_FileName.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001386
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001387 Restore_File_System = Get_Restore_File_System(restore_folder);
1388
1389 if (Is_File_System(Restore_File_System))
1390 return Restore_Tar(restore_folder, Restore_File_System, total_restore_size, already_restored_size);
1391 else if (Is_Image(Restore_File_System)) {
1392 *already_restored_size += TWFunc::Get_File_Size(Backup_Name);
1393 if (Restore_File_System == "emmc")
1394 return Restore_DD(restore_folder, total_restore_size, already_restored_size);
1395 else if (Restore_File_System == "mtd" || Restore_File_System == "bml")
1396 return Restore_Flash_Image(restore_folder, total_restore_size, already_restored_size);
1397 }
1398
1399 LOGERR("Unknown restore method for '%s'\n", Mount_Point.c_str());
1400 return false;
1401}
1402
1403string TWPartition::Get_Restore_File_System(string restore_folder) {
1404 size_t first_period, second_period;
1405 string Restore_File_System;
1406
Gary Peck43acadf2012-11-21 21:19:01 -08001407 // Parse backup filename to extract the file system before wiping
1408 first_period = Backup_FileName.find(".");
1409 if (first_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001410 LOGERR("Unable to find file system (first period).\n");
thatd43bf2d2014-09-21 23:13:02 +02001411 return string();
Gary Peck43acadf2012-11-21 21:19:01 -08001412 }
1413 Restore_File_System = Backup_FileName.substr(first_period + 1, Backup_FileName.size() - first_period - 1);
1414 second_period = Restore_File_System.find(".");
1415 if (second_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001416 LOGERR("Unable to find file system (second period).\n");
thatd43bf2d2014-09-21 23:13:02 +02001417 return string();
Gary Peck43acadf2012-11-21 21:19:01 -08001418 }
1419 Restore_File_System.resize(second_period);
Dees_Troy2673cec2013-04-02 20:22:16 +00001420 LOGINFO("Restore file system is: '%s'.\n", Restore_File_System.c_str());
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001421 return Restore_File_System;
Dees_Troy51a0e822012-09-05 15:24:24 -04001422}
1423
1424string TWPartition::Backup_Method_By_Name() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001425 if (Backup_Method == NONE)
1426 return "none";
1427 else if (Backup_Method == FILES)
1428 return "files";
1429 else if (Backup_Method == DD)
1430 return "dd";
1431 else if (Backup_Method == FLASH_UTILS)
1432 return "flash_utils";
1433 else
1434 return "undefined";
1435 return "ERROR!";
Dees_Troy51a0e822012-09-05 15:24:24 -04001436}
1437
1438bool TWPartition::Decrypt(string Password) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001439 LOGINFO("STUB TWPartition::Decrypt, password: '%s'\n", Password.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001440 // Is this needed?
Dees_Troy51a0e822012-09-05 15:24:24 -04001441 return 1;
1442}
1443
1444bool TWPartition::Wipe_Encryption() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001445 bool Save_Data_Media = Has_Data_Media;
1446
1447 if (!UnMount(true))
1448 return false;
1449
Dees_Troy38bd7602012-09-14 13:33:53 -04001450 Has_Data_Media = false;
Dees_Troy74fb2e92013-04-15 14:35:47 +00001451 Decrypted_Block_Device = "";
1452 Is_Decrypted = false;
1453 Is_Encrypted = false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001454 Find_Actual_Block_Device();
1455 bool mtp_was_enabled = TWFunc::Toggle_MTP(false);
Gary Pecke8bc5d72012-12-21 06:45:25 -08001456 if (Wipe(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001457 Has_Data_Media = Save_Data_Media;
1458 if (Has_Data_Media && !Symlink_Mount_Point.empty()) {
1459 Recreate_Media_Folder();
1460 }
Ethan Yonker83e82572014-04-04 10:59:28 -05001461#ifndef TW_OEM_BUILD
Dees_Troy2673cec2013-04-02 20:22:16 +00001462 gui_print("You may need to reboot recovery to be able to use /data again.\n");
Ethan Yonker83e82572014-04-04 10:59:28 -05001463#endif
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001464 TWFunc::Toggle_MTP(mtp_was_enabled);
Dees_Troy38bd7602012-09-14 13:33:53 -04001465 return true;
1466 } else {
1467 Has_Data_Media = Save_Data_Media;
Dees_Troy2673cec2013-04-02 20:22:16 +00001468 LOGERR("Unable to format to remove encryption.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001469 return false;
1470 }
1471 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001472}
1473
1474void TWPartition::Check_FS_Type() {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001475 const char* type;
1476 blkid_probe pr;
Dees_Troy5bf43922012-09-07 16:07:55 -04001477
Dees_Troy68cab492012-12-12 19:29:35 +00001478 if (Fstab_File_System == "yaffs2" || Fstab_File_System == "mtd" || Fstab_File_System == "bml" || Ignore_Blkid)
1479 return; // Running blkid on some mtd devices causes a massive crash or needs to be skipped
Dees_Troy5bf43922012-09-07 16:07:55 -04001480
Dees_Troy38bd7602012-09-14 13:33:53 -04001481 Find_Actual_Block_Device();
Dees_Troy8170a922012-09-18 15:40:25 -04001482 if (!Is_Present)
1483 return;
Dees_Troy51127312012-09-08 13:08:49 -04001484
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001485 pr = blkid_new_probe_from_filename(Actual_Block_Device.c_str());
1486 if (blkid_do_fullprobe(pr)) {
1487 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00001488 LOGINFO("Can't probe device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001489 return;
Dees_Troy5bf43922012-09-07 16:07:55 -04001490 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001491
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05001492 if (blkid_probe_lookup_value(pr, "TYPE", &type, NULL) < 0) {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001493 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00001494 LOGINFO("can't find filesystem on device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001495 return;
1496 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001497
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001498 Current_File_System = type;
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001499 blkid_free_probe(pr);
Dees_Troy51a0e822012-09-05 15:24:24 -04001500}
1501
Gary Peck43acadf2012-11-21 21:19:01 -08001502bool TWPartition::Wipe_EXT23(string File_System) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001503 if (!UnMount(true))
1504 return false;
1505
Dees_Troy43d8b002012-09-17 16:00:01 -04001506 if (TWFunc::Path_Exists("/sbin/mke2fs")) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001507 string command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001508
Dees_Troy2673cec2013-04-02 20:22:16 +00001509 gui_print("Formatting %s using mke2fs...\n", Display_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001510 Find_Actual_Block_Device();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001511 command = "mke2fs -t " + File_System + " -m 0 " + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001512 LOGINFO("mke2fs command: %s\n", command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001513 if (TWFunc::Exec_Cmd(command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001514 Current_File_System = File_System;
Dees_Troye58d5262012-09-21 12:27:57 -04001515 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001516 gui_print("Done.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001517 return true;
1518 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001519 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001520 return false;
1521 }
1522 } else
1523 return Wipe_RMRF();
1524
1525 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001526}
1527
1528bool TWPartition::Wipe_EXT4() {
Ethan Yonker25f20c12014-10-14 09:04:43 -05001529 Find_Actual_Block_Device();
1530 if (!Is_Present) {
1531 LOGERR("Block device not present, cannot wipe %s.\n", Display_Name.c_str());
1532 return false;
1533 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001534 if (!UnMount(true))
1535 return false;
1536
Dees_Troyb3265ab2013-08-30 02:59:14 +00001537#if defined(HAVE_SELINUX) && defined(USE_EXT4)
Ethan Yonkerf27497f2014-02-09 11:48:33 -06001538 int ret;
1539 char *secontext = NULL;
1540
Dees_Troya95f55c2013-08-17 13:14:43 +00001541 gui_print("Formatting %s using make_ext4fs function.\n", Display_Name.c_str());
Ethan Yonkerf27497f2014-02-09 11:48:33 -06001542
Dees Troy99c8dbf2014-03-10 16:53:58 +00001543 if (!selinux_handle || selabel_lookup(selinux_handle, &secontext, Mount_Point.c_str(), S_IFDIR) < 0) {
Ethan Yonkerf27497f2014-02-09 11:48:33 -06001544 LOGINFO("Cannot lookup security context for '%s'\n", Mount_Point.c_str());
1545 ret = make_ext4fs(Actual_Block_Device.c_str(), Length, Mount_Point.c_str(), NULL);
1546 } else {
1547 ret = make_ext4fs(Actual_Block_Device.c_str(), Length, Mount_Point.c_str(), selinux_handle);
1548 }
1549 if (ret != 0) {
Dees_Troya95f55c2013-08-17 13:14:43 +00001550 LOGERR("Unable to wipe '%s' using function call.\n", Mount_Point.c_str());
1551 return false;
1552 } else {
bigbiff bigbiffc49d7062013-10-11 20:28:00 -04001553 string sedir = Mount_Point + "/lost+found";
1554 PartitionManager.Mount_By_Path(sedir.c_str(), true);
1555 rmdir(sedir.c_str());
1556 mkdir(sedir.c_str(), S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP);
Dees_Troya95f55c2013-08-17 13:14:43 +00001557 return true;
1558 }
1559#else
Dees_Troy43d8b002012-09-17 16:00:01 -04001560 if (TWFunc::Path_Exists("/sbin/make_ext4fs")) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001561 string Command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001562
Dees_Troy2673cec2013-04-02 20:22:16 +00001563 gui_print("Formatting %s using make_ext4fs...\n", Display_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001564 Find_Actual_Block_Device();
1565 Command = "make_ext4fs";
1566 if (!Is_Decrypted && Length != 0) {
1567 // Only use length if we're not decrypted
1568 char len[32];
1569 sprintf(len, "%i", Length);
1570 Command += " -l ";
1571 Command += len;
1572 }
Dees_Troy5295d582013-09-06 15:51:08 +00001573 if (TWFunc::Path_Exists("/file_contexts")) {
1574 Command += " -S /file_contexts";
1575 }
1576 Command += " -a " + Mount_Point + " " + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001577 LOGINFO("make_ext4fs command: %s\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001578 if (TWFunc::Exec_Cmd(Command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001579 Current_File_System = "ext4";
Dees_Troye58d5262012-09-21 12:27:57 -04001580 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001581 gui_print("Done.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001582 return true;
1583 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001584 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001585 return false;
1586 }
1587 } else
Gary Peck43acadf2012-11-21 21:19:01 -08001588 return Wipe_EXT23("ext4");
Dees_Troya95f55c2013-08-17 13:14:43 +00001589#endif
Dees_Troy38bd7602012-09-14 13:33:53 -04001590 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001591}
1592
1593bool TWPartition::Wipe_FAT() {
Vojtech Bocek05534202013-09-11 08:11:56 +02001594 string command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001595
Dees_Troy43d8b002012-09-17 16:00:01 -04001596 if (TWFunc::Path_Exists("/sbin/mkdosfs")) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001597 if (!UnMount(true))
1598 return false;
1599
Dees_Troy2673cec2013-04-02 20:22:16 +00001600 gui_print("Formatting %s using mkdosfs...\n", Display_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001601 Find_Actual_Block_Device();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001602 command = "mkdosfs " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02001603 if (TWFunc::Exec_Cmd(command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001604 Current_File_System = "vfat";
Dees_Troye58d5262012-09-21 12:27:57 -04001605 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001606 gui_print("Done.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001607 return true;
1608 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001609 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001610 return false;
1611 }
1612 return true;
1613 }
1614 else
1615 return Wipe_RMRF();
1616
1617 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001618}
1619
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001620bool TWPartition::Wipe_EXFAT() {
Vojtech Bocek05534202013-09-11 08:11:56 +02001621 string command;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001622
1623 if (TWFunc::Path_Exists("/sbin/mkexfatfs")) {
1624 if (!UnMount(true))
1625 return false;
1626
Dees_Troy2673cec2013-04-02 20:22:16 +00001627 gui_print("Formatting %s using mkexfatfs...\n", Display_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001628 Find_Actual_Block_Device();
1629 command = "mkexfatfs " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02001630 if (TWFunc::Exec_Cmd(command) == 0) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001631 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001632 gui_print("Done.\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001633 return true;
1634 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001635 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001636 return false;
1637 }
1638 return true;
1639 }
1640 return false;
1641}
1642
Dees_Troy38bd7602012-09-14 13:33:53 -04001643bool TWPartition::Wipe_MTD() {
1644 if (!UnMount(true))
1645 return false;
1646
Dees_Troy2673cec2013-04-02 20:22:16 +00001647 gui_print("MTD Formatting \"%s\"\n", MTD_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001648
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001649 mtd_scan_partitions();
1650 const MtdPartition* mtd = mtd_find_partition_by_name(MTD_Name.c_str());
1651 if (mtd == NULL) {
1652 LOGERR("No mtd partition named '%s'", MTD_Name.c_str());
1653 return false;
1654 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001655
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001656 MtdWriteContext* ctx = mtd_write_partition(mtd);
1657 if (ctx == NULL) {
1658 LOGERR("Can't write '%s', failed to format.", MTD_Name.c_str());
1659 return false;
1660 }
1661 if (mtd_erase_blocks(ctx, -1) == -1) {
1662 mtd_write_close(ctx);
1663 LOGERR("Failed to format '%s'", MTD_Name.c_str());
1664 return false;
1665 }
1666 if (mtd_write_close(ctx) != 0) {
1667 LOGERR("Failed to close '%s'", MTD_Name.c_str());
1668 return false;
1669 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001670 Current_File_System = "yaffs2";
Dees_Troye58d5262012-09-21 12:27:57 -04001671 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001672 gui_print("Done.\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001673 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001674}
1675
1676bool TWPartition::Wipe_RMRF() {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001677 if (Is_Storage)
1678 TWFunc::Toggle_MTP(false);
Dees_Troy38bd7602012-09-14 13:33:53 -04001679 if (!Mount(true))
1680 return false;
1681
Dees_Troy2673cec2013-04-02 20:22:16 +00001682 gui_print("Removing all files under '%s'\n", Mount_Point.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001683 TWFunc::removeDir(Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04001684 Recreate_AndSec_Folder();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001685 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001686}
1687
Dees_Troye5017042013-08-29 16:38:55 +00001688bool TWPartition::Wipe_F2FS() {
Vojtech Bocek05534202013-09-11 08:11:56 +02001689 string command;
Dees_Troye5017042013-08-29 16:38:55 +00001690
1691 if (TWFunc::Path_Exists("/sbin/mkfs.f2fs")) {
1692 if (!UnMount(true))
1693 return false;
1694
1695 gui_print("Formatting %s using mkfs.f2fs...\n", Display_Name.c_str());
1696 Find_Actual_Block_Device();
1697 command = "mkfs.f2fs " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02001698 if (TWFunc::Exec_Cmd(command) == 0) {
Dees_Troye5017042013-08-29 16:38:55 +00001699 Recreate_AndSec_Folder();
1700 gui_print("Done.\n");
1701 return true;
1702 } else {
1703 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
1704 return false;
1705 }
1706 return true;
1707 } else {
1708 gui_print("mkfs.f2fs binary not found, using rm -rf to wipe.\n");
1709 return Wipe_RMRF();
1710 }
1711 return false;
1712}
1713
Dees_Troy51a0e822012-09-05 15:24:24 -04001714bool TWPartition::Wipe_Data_Without_Wiping_Media() {
Ethan Yonker83e82572014-04-04 10:59:28 -05001715#ifdef TW_OEM_BUILD
1716 // In an OEM Build we want to do a full format
1717 return Wipe_Encryption();
1718#else
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001719 string dir;
bigbiff bigbiff6b600f92014-01-05 18:13:43 -05001720 #ifdef HAVE_SELINUX
1721 fixPermissions perms;
1722 #endif
Dees_Troy38bd7602012-09-14 13:33:53 -04001723
1724 // This handles wiping data on devices with "sdcard" in /data/media
1725 if (!Mount(true))
1726 return false;
1727
Dees_Troy2673cec2013-04-02 20:22:16 +00001728 gui_print("Wiping data without wiping /data/media ...\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001729
1730 DIR* d;
1731 d = opendir("/data");
Dees_Troy16b74352012-11-14 22:27:31 +00001732 if (d != NULL) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001733 struct dirent* de;
1734 while ((de = readdir(d)) != NULL) {
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05001735 if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) continue;
Dees_Troy16b74352012-11-14 22:27:31 +00001736 // The media folder is the "internal sdcard"
1737 // The .layout_version file is responsible for determining whether 4.2 decides up upgrade
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001738 // the media folder for multi-user.
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05001739 //TODO: convert this to use twrpDU.cpp
Dees_Troy16b74352012-11-14 22:27:31 +00001740 if (strcmp(de->d_name, "media") == 0 || strcmp(de->d_name, ".layout_version") == 0) continue;
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05001741
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001742 dir = "/data/";
1743 dir.append(de->d_name);
Dees_Troyce675462013-01-09 19:48:21 +00001744 if (de->d_type == DT_DIR) {
1745 TWFunc::removeDir(dir, false);
bigbiff bigbiff98f1f902013-01-19 18:46:13 -05001746 } 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 +00001747 if (!unlink(dir.c_str()))
Dees_Troy2673cec2013-04-02 20:22:16 +00001748 LOGINFO("Unable to unlink '%s'\n", dir.c_str());
Dees_Troyce675462013-01-09 19:48:21 +00001749 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001750 }
1751 closedir(d);
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05001752
Dees_Troy2673cec2013-04-02 20:22:16 +00001753 gui_print("Done.\n");
Dees_Troy16b74352012-11-14 22:27:31 +00001754 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -04001755 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001756 gui_print("Dirent failed to open /data, error!\n");
Dees_Troy16b74352012-11-14 22:27:31 +00001757 return false;
Ethan Yonker83e82572014-04-04 10:59:28 -05001758#endif // ifdef TW_OEM_BUILD
Dees_Troy51a0e822012-09-05 15:24:24 -04001759}
1760
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001761bool TWPartition::Backup_Tar(string backup_folder, const unsigned long long *overall_size, const unsigned long long *other_backups_size) {
Dees_Troy4a2a1262012-09-18 09:33:47 -04001762 char back_name[255], split_index[5];
1763 string Full_FileName, Split_FileName, Tar_Args, Command;
Dees_Troy83bd4832013-05-04 12:39:56 +00001764 int use_compression, use_encryption = 0, index, backup_count;
Dees_Troy4a2a1262012-09-18 09:33:47 -04001765 struct stat st;
Dees_Troy7c2dec82012-09-26 09:49:14 -04001766 unsigned long long total_bsize = 0, file_size;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001767 twrpTar tar;
1768 vector <string> files;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001769
Dees_Troy43d8b002012-09-17 16:00:01 -04001770 if (!Mount(true))
1771 return false;
1772
Dees_Troya13d74f2013-03-24 08:54:55 -05001773 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Backup_Display_Name, "Backing Up");
Dees_Troy2673cec2013-04-02 20:22:16 +00001774 gui_print("Backing up %s...\n", Backup_Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001775
1776 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
Dees_Troy83bd4832013-05-04 12:39:56 +00001777 tar.use_compression = use_compression;
Matt Mowerbb81e5d2014-03-20 18:05:41 -05001778
Dees_Troy83bd4832013-05-04 12:39:56 +00001779#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
1780 DataManager::GetValue("tw_encrypt_backup", use_encryption);
1781 if (use_encryption && Can_Encrypt_Backup) {
1782 tar.use_encryption = use_encryption;
1783 if (Use_Userdata_Encryption)
1784 tar.userdata_encryption = use_encryption;
Ethan Yonker87af5632014-02-10 11:56:35 -06001785 string Password;
1786 DataManager::GetValue("tw_backup_password", Password);
1787 tar.setpassword(Password);
Dees_Troy83bd4832013-05-04 12:39:56 +00001788 } else {
1789 use_encryption = false;
1790 }
1791#endif
Dees_Troy43d8b002012-09-17 16:00:01 -04001792
1793 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1794 Backup_FileName = back_name;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001795 Full_FileName = backup_folder + "/" + Backup_FileName;
Dees_Troy83bd4832013-05-04 12:39:56 +00001796 tar.has_data_media = Has_Data_Media;
Dees Troye0a433a2013-12-02 04:10:37 +00001797 Full_FileName = backup_folder + "/" + Backup_FileName;
1798 tar.setdir(Backup_Path);
1799 tar.setfn(Full_FileName);
1800 tar.setsize(Backup_Size);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001801 tar.partition_name = Backup_Name;
1802 tar.backup_folder = backup_folder;
1803 if (tar.createTarFork(overall_size, other_backups_size) != 0)
Dees Troye0a433a2013-12-02 04:10:37 +00001804 return false;
Dees_Troy43d8b002012-09-17 16:00:01 -04001805 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001806}
1807
1808bool TWPartition::Backup_DD(string backup_folder) {
igoriok87e3d932013-01-31 21:03:53 +02001809 char back_name[255], backup_size[32];
Vojtech Bocek05534202013-09-11 08:11:56 +02001810 string Full_FileName, Command, DD_BS;
Dees_Troy43d8b002012-09-17 16:00:01 -04001811 int use_compression;
1812
igoriok87e3d932013-01-31 21:03:53 +02001813 sprintf(backup_size, "%llu", Backup_Size);
1814 DD_BS = backup_size;
1815
Dees_Troyb46a6842012-09-25 11:06:46 -04001816 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
Dees_Troy2673cec2013-04-02 20:22:16 +00001817 gui_print("Backing up %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001818
1819 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1820 Backup_FileName = back_name;
1821
1822 Full_FileName = backup_folder + "/" + Backup_FileName;
1823
igoriok87e3d932013-01-31 21:03:53 +02001824 Command = "dd if=" + Actual_Block_Device + " of='" + Full_FileName + "'" + " bs=" + DD_BS + "c count=1";
Dees_Troy2673cec2013-04-02 20:22:16 +00001825 LOGINFO("Backup command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001826 TWFunc::Exec_Cmd(Command);
Dees_Troyc154ac22012-10-12 15:36:47 -04001827 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001828 LOGERR("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001829 return false;
1830 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001831 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001832}
1833
1834bool TWPartition::Backup_Dump_Image(string backup_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001835 char back_name[255];
Vojtech Bocek05534202013-09-11 08:11:56 +02001836 string Full_FileName, Command;
Dees_Troy43d8b002012-09-17 16:00:01 -04001837 int use_compression;
1838
Dees_Troyb46a6842012-09-25 11:06:46 -04001839 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
Dees_Troy2673cec2013-04-02 20:22:16 +00001840 gui_print("Backing up %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001841
1842 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1843 Backup_FileName = back_name;
1844
1845 Full_FileName = backup_folder + "/" + Backup_FileName;
1846
1847 Command = "dump_image " + MTD_Name + " '" + Full_FileName + "'";
Dees_Troy2673cec2013-04-02 20:22:16 +00001848 LOGINFO("Backup command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001849 TWFunc::Exec_Cmd(Command);
Dees_Troy7c2dec82012-09-26 09:49:14 -04001850 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
1851 // Actual size may not match backup size due to bad blocks on MTD devices so just check for 0 bytes
Dees_Troy2673cec2013-04-02 20:22:16 +00001852 LOGERR("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001853 return false;
1854 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001855 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001856}
1857
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001858unsigned long long TWPartition::Get_Restore_Size(string restore_folder) {
1859 InfoManager restore_info(restore_folder + "/" + Backup_Name + ".info");
1860 if (restore_info.LoadValues() == 0) {
1861 if (restore_info.GetValue("backup_size", Restore_Size) == 0) {
1862 LOGINFO("Read info file, restore size is %llu\n", Restore_Size);
1863 return Restore_Size;
1864 }
1865 }
1866 string Full_FileName, Restore_File_System = Get_Restore_File_System(restore_folder);
1867
1868 Full_FileName = restore_folder + "/" + Backup_FileName;
1869
1870 if (Is_Image(Restore_File_System)) {
1871 Restore_Size = TWFunc::Get_File_Size(Full_FileName);
1872 return Restore_Size;
1873 }
1874
1875 twrpTar tar;
1876 tar.setdir(Backup_Path);
1877 tar.setfn(Full_FileName);
1878 tar.backup_name = Backup_Name;
1879#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
1880 string Password;
1881 DataManager::GetValue("tw_restore_password", Password);
1882 if (!Password.empty())
1883 tar.setpassword(Password);
1884#endif
1885 tar.partition_name = Backup_Name;
1886 tar.backup_folder = restore_folder;
1887 Restore_Size = tar.get_size();
1888 return Restore_Size;
1889}
1890
1891bool 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 -08001892 string Full_FileName, Command;
Dees_Troy4a2a1262012-09-18 09:33:47 -04001893 int index = 0;
1894 char split_index[5];
Dees Troy4159aed2014-02-28 17:24:43 +00001895 bool ret = false;
Dees_Troy43d8b002012-09-17 16:00:01 -04001896
Dees_Troye58d5262012-09-21 12:27:57 -04001897 if (Has_Android_Secure) {
Dees_Troye58d5262012-09-21 12:27:57 -04001898 if (!Wipe_AndSec())
1899 return false;
Gary Peck43acadf2012-11-21 21:19:01 -08001900 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001901 gui_print("Wiping %s...\n", Display_Name.c_str());
Ethan Yonker5eac2222014-06-11 12:22:55 -05001902 if (Has_Data_Media && Mount_Point == "/data" && Restore_File_System != Current_File_System) {
1903 gui_print("WARNING: This /data backup was made with %s file system!\n", Restore_File_System.c_str());
1904 gui_print("The backup may not boot unless you change back to %s.\n", Restore_File_System.c_str());
1905 if (!Wipe_Data_Without_Wiping_Media())
1906 return false;
1907 } else {
1908 if (!Wipe(Restore_File_System))
1909 return false;
1910 }
Dees_Troye58d5262012-09-21 12:27:57 -04001911 }
Dees_Troya13d74f2013-03-24 08:54:55 -05001912 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Backup_Display_Name, "Restoring");
Dees_Troy2673cec2013-04-02 20:22:16 +00001913 gui_print("Restoring %s...\n", Backup_Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001914
1915 if (!Mount(true))
1916 return false;
1917
Dees_Troy4a2a1262012-09-18 09:33:47 -04001918 Full_FileName = restore_folder + "/" + Backup_FileName;
Ethan Yonker87af5632014-02-10 11:56:35 -06001919 twrpTar tar;
1920 tar.setdir(Backup_Path);
1921 tar.setfn(Full_FileName);
1922 tar.backup_name = Backup_Name;
1923#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
1924 string Password;
1925 DataManager::GetValue("tw_restore_password", Password);
1926 if (!Password.empty())
1927 tar.setpassword(Password);
1928#endif
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001929 if (tar.extractTarFork(total_restore_size, already_restored_size) != 0)
Dees Troy4159aed2014-02-28 17:24:43 +00001930 ret = false;
1931 else
1932 ret = true;
1933#ifdef HAVE_CAPABILITIES
1934 // Restore capabilities to the run-as binary
1935 if (Mount_Point == "/system" && Mount(true) && TWFunc::Path_Exists("/system/bin/run-as")) {
1936 struct vfs_cap_data cap_data;
1937 uint64_t capabilities = (1 << CAP_SETUID) | (1 << CAP_SETGID);
1938
1939 memset(&cap_data, 0, sizeof(cap_data));
1940 cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE;
1941 cap_data.data[0].permitted = (uint32_t) (capabilities & 0xffffffff);
1942 cap_data.data[0].inheritable = 0;
1943 cap_data.data[1].permitted = (uint32_t) (capabilities >> 32);
1944 cap_data.data[1].inheritable = 0;
1945 if (setxattr("/system/bin/run-as", XATTR_NAME_CAPS, &cap_data, sizeof(cap_data), 0) < 0) {
1946 LOGINFO("Failed to reset capabilities of /system/bin/run-as binary.\n");
1947 } else {
1948 LOGINFO("Reset capabilities of /system/bin/run-as binary successful.\n");
1949 }
1950 }
1951#endif
1952 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04001953}
1954
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001955bool TWPartition::Restore_DD(string restore_folder, const unsigned long long *total_restore_size, unsigned long long *already_restored_size) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001956 string Full_FileName, Command;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001957 double display_percent, progress_percent;
1958 char size_progress[1024];
Dees_Troy43d8b002012-09-17 16:00:01 -04001959
Dees_Troyda8b55a2012-12-12 19:18:30 +00001960 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
Dees_Troy43d8b002012-09-17 16:00:01 -04001961 Full_FileName = restore_folder + "/" + Backup_FileName;
Gary Peck15e623d2012-11-21 21:07:58 -08001962
1963 if (!Find_Partition_Size()) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001964 LOGERR("Unable to find partition size for '%s'\n", Mount_Point.c_str());
Gary Peck15e623d2012-11-21 21:07:58 -08001965 return false;
1966 }
1967 unsigned long long backup_size = TWFunc::Get_File_Size(Full_FileName);
1968 if (backup_size > Size) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001969 LOGERR("Size (%iMB) of backup '%s' is larger than target device '%s' (%iMB)\n",
Gary Peck15e623d2012-11-21 21:07:58 -08001970 (int)(backup_size / 1048576LLU), Full_FileName.c_str(),
1971 Actual_Block_Device.c_str(), (int)(Size / 1048576LLU));
1972 return false;
1973 }
1974
Dees_Troy2673cec2013-04-02 20:22:16 +00001975 gui_print("Restoring %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001976 Command = "dd bs=4096 if='" + Full_FileName + "' of=" + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001977 LOGINFO("Restore command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001978 TWFunc::Exec_Cmd(Command);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001979 display_percent = (double)(Restore_Size + *already_restored_size) / (double)(*total_restore_size) * 100;
1980 sprintf(size_progress, "%lluMB of %lluMB, %i%%", (Restore_Size + *already_restored_size) / 1048576, *total_restore_size / 1048576, (int)(display_percent));
1981 DataManager::SetValue("tw_size_progress", size_progress);
1982 progress_percent = (display_percent / 100);
1983 DataManager::SetProgress((float)(progress_percent));
1984 *already_restored_size += Restore_Size;
Dees_Troy43d8b002012-09-17 16:00:01 -04001985 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001986}
1987
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001988bool TWPartition::Restore_Flash_Image(string restore_folder, const unsigned long long *total_restore_size, unsigned long long *already_restored_size) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001989 string Full_FileName, Command;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001990 double display_percent, progress_percent;
1991 char size_progress[1024];
Dees_Troy43d8b002012-09-17 16:00:01 -04001992
Dees_Troy2673cec2013-04-02 20:22:16 +00001993 gui_print("Restoring %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001994 Full_FileName = restore_folder + "/" + Backup_FileName;
1995 // Sometimes flash image doesn't like to flash due to the first 2KB matching, so we erase first to ensure that it flashes
1996 Command = "erase_image " + MTD_Name;
Dees_Troy2673cec2013-04-02 20:22:16 +00001997 LOGINFO("Erase command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001998 TWFunc::Exec_Cmd(Command);
Dees_Troy43d8b002012-09-17 16:00:01 -04001999 Command = "flash_image " + MTD_Name + " '" + Full_FileName + "'";
Dees_Troy2673cec2013-04-02 20:22:16 +00002000 LOGINFO("Restore command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02002001 TWFunc::Exec_Cmd(Command);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05002002 display_percent = (double)(Restore_Size + *already_restored_size) / (double)(*total_restore_size) * 100;
2003 sprintf(size_progress, "%lluMB of %lluMB, %i%%", (Restore_Size + *already_restored_size) / 1048576, *total_restore_size / 1048576, (int)(display_percent));
2004 DataManager::SetValue("tw_size_progress", size_progress);
2005 progress_percent = (display_percent / 100);
2006 DataManager::SetProgress((float)(progress_percent));
2007 *already_restored_size += Restore_Size;
Dees_Troy43d8b002012-09-17 16:00:01 -04002008 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04002009}
Dees_Troy5bf43922012-09-07 16:07:55 -04002010
2011bool TWPartition::Update_Size(bool Display_Error) {
Dees_Troy0550cfb2012-10-13 11:56:13 -04002012 bool ret = false, Was_Already_Mounted = false;
Dees_Troy51127312012-09-08 13:08:49 -04002013
Dees_Troyab10ee22012-09-21 14:27:30 -04002014 if (!Can_Be_Mounted && !Is_Encrypted)
Dees_Troy5bf43922012-09-07 16:07:55 -04002015 return false;
2016
Dees_Troy0550cfb2012-10-13 11:56:13 -04002017 Was_Already_Mounted = Is_Mounted();
Dees_Troy38bd7602012-09-14 13:33:53 -04002018 if (Removable || Is_Encrypted) {
2019 if (!Mount(false))
2020 return true;
2021 } else if (!Mount(Display_Error))
Dees_Troy5bf43922012-09-07 16:07:55 -04002022 return false;
Dees_Troy51127312012-09-08 13:08:49 -04002023
2024 ret = Get_Size_Via_statfs(Display_Error);
Dees_Troy0550cfb2012-10-13 11:56:13 -04002025 if (!ret || Size == 0) {
2026 if (!Get_Size_Via_df(Display_Error)) {
2027 if (!Was_Already_Mounted)
2028 UnMount(false);
Dees_Troy51127312012-09-08 13:08:49 -04002029 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04002030 }
2031 }
Dees_Troy51127312012-09-08 13:08:49 -04002032
Dees_Troy5bf43922012-09-07 16:07:55 -04002033 if (Has_Data_Media) {
2034 if (Mount(Display_Error)) {
Dees_Troy51127312012-09-08 13:08:49 -04002035 unsigned long long data_media_used, actual_data;
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002036 Used = du.Get_Folder_Size("/data");
2037 Backup_Size = Used;
2038 int bak = (int)(Used / 1048576LLU);
Dees_Troy51127312012-09-08 13:08:49 -04002039 int fre = (int)(Free / 1048576LLU);
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002040 LOGINFO("Data backup size is %iMB, free: %iMB.\n", bak, fre);
Dees_Troy0550cfb2012-10-13 11:56:13 -04002041 } else {
2042 if (!Was_Already_Mounted)
2043 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04002044 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04002045 }
Dees_Troye58d5262012-09-21 12:27:57 -04002046 } else if (Has_Android_Secure) {
2047 if (Mount(Display_Error))
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002048 Backup_Size = du.Get_Folder_Size(Backup_Path);
Dees_Troy0550cfb2012-10-13 11:56:13 -04002049 else {
2050 if (!Was_Already_Mounted)
2051 UnMount(false);
Dees_Troye58d5262012-09-21 12:27:57 -04002052 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04002053 }
Dees_Troy5bf43922012-09-07 16:07:55 -04002054 }
Dees_Troy0550cfb2012-10-13 11:56:13 -04002055 if (!Was_Already_Mounted)
2056 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04002057 return true;
Dees_Troy51127312012-09-08 13:08:49 -04002058}
Dees_Troy38bd7602012-09-14 13:33:53 -04002059
2060void TWPartition::Find_Actual_Block_Device(void) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002061 if (Is_Decrypted && !Decrypted_Block_Device.empty()) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002062 Actual_Block_Device = Decrypted_Block_Device;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002063 if (TWFunc::Path_Exists(Decrypted_Block_Device))
Dees_Troy38bd7602012-09-14 13:33:53 -04002064 Is_Present = true;
Dees_Troy43d8b002012-09-17 16:00:01 -04002065 } else if (TWFunc::Path_Exists(Primary_Block_Device)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002066 Is_Present = true;
2067 Actual_Block_Device = Primary_Block_Device;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002068 return;
2069 }
2070 if (Is_Decrypted) {
Dees_Troy43d8b002012-09-17 16:00:01 -04002071 } else if (!Alternate_Block_Device.empty() && TWFunc::Path_Exists(Alternate_Block_Device)) {
Dees_Troy3f04d032012-10-07 18:20:09 -04002072 Actual_Block_Device = Alternate_Block_Device;
Dees_Troy38bd7602012-09-14 13:33:53 -04002073 Is_Present = true;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002074 } else {
Dees_Troy38bd7602012-09-14 13:33:53 -04002075 Is_Present = false;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002076 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002077}
2078
2079void TWPartition::Recreate_Media_Folder(void) {
2080 string Command;
2081
bigbiff bigbiff6b600f92014-01-05 18:13:43 -05002082 #ifdef HAVE_SELINUX
2083 fixPermissions perms;
2084 #endif
2085
Dees_Troy38bd7602012-09-14 13:33:53 -04002086 if (!Mount(true)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00002087 LOGERR("Unable to recreate /data/media folder.\n");
Dees_Troyb46a6842012-09-25 11:06:46 -04002088 } else if (!TWFunc::Path_Exists("/data/media")) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002089 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Dees_Troy2673cec2013-04-02 20:22:16 +00002090 LOGINFO("Recreating /data/media folder.\n");
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05002091 mkdir("/data/media", S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
Ethan Yonker5ef301e2014-10-14 10:04:44 -05002092#ifdef HAVE_SELINUX
bigbiff bigbiff6b600f92014-01-05 18:13:43 -05002093 perms.fixDataInternalContexts();
Ethan Yonker5ef301e2014-10-14 10:04:44 -05002094#endif
Ethan Yonker5eac2222014-06-11 12:22:55 -05002095 // Toggle mount to ensure that "internal sdcard" gets mounted
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002096 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Ethan Yonker5eac2222014-06-11 12:22:55 -05002097 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Dees_Troy38bd7602012-09-14 13:33:53 -04002098 }
Dees_Troy43d8b002012-09-17 16:00:01 -04002099}
Dees_Troye58d5262012-09-21 12:27:57 -04002100
2101void TWPartition::Recreate_AndSec_Folder(void) {
Dees_Troye58d5262012-09-21 12:27:57 -04002102 if (!Has_Android_Secure)
2103 return;
Dees_Troy2673cec2013-04-02 20:22:16 +00002104 LOGINFO("Creating %s: %s\n", Backup_Display_Name.c_str(), Symlink_Path.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -04002105 if (!Mount(true)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00002106 LOGERR("Unable to recreate %s folder.\n", Backup_Name.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -04002107 } else if (!TWFunc::Path_Exists(Symlink_Path)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00002108 LOGINFO("Recreating %s folder.\n", Backup_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002109 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05002110 mkdir(Symlink_Path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002111 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04002112 }
2113}