blob: 4bd545b172b9052c0beb3b52e80c1026b8c4bb0f [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;
Dees_Troy657c3092012-09-10 20:32:10 -0400281 char crypto_blkdev[255];
282 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
283 if (strcmp(crypto_blkdev, "error") != 0) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400284 DataManager::SetValue(TW_DATA_BLK_DEVICE, Primary_Block_Device);
Dees_Troy657c3092012-09-10 20:32:10 -0400285 DataManager::SetValue(TW_IS_DECRYPTED, 1);
286 Is_Encrypted = true;
287 Is_Decrypted = true;
288 Decrypted_Block_Device = crypto_blkdev;
Dees_Troy2673cec2013-04-02 20:22:16 +0000289 LOGINFO("Data already decrypted, new block device: '%s'\n", crypto_blkdev);
Dees_Troy657c3092012-09-10 20:32:10 -0400290 } else if (!Mount(false)) {
Ethan Yonker71413f42014-02-26 13:36:08 -0600291 if (Is_Present) {
Ethan Yonker4eca40d2014-11-11 14:52:28 -0600292#if defined(TW_INCLUDE_JB_CRYPTO) || defined(TW_INCLUDE_L_CRYPTO)
Ethan Yonker71413f42014-02-26 13:36:08 -0600293 // No extra flags needed
294#else
295 property_set("ro.crypto.fs_type", CRYPTO_FS_TYPE);
296 property_set("ro.crypto.fs_real_blkdev", CRYPTO_REAL_BLKDEV);
297 property_set("ro.crypto.fs_mnt_point", CRYPTO_MNT_POINT);
298 property_set("ro.crypto.fs_options", CRYPTO_FS_OPTIONS);
299 property_set("ro.crypto.fs_flags", CRYPTO_FS_FLAGS);
300 property_set("ro.crypto.keyfile.userdata", CRYPTO_KEY_LOC);
301#ifdef CRYPTO_SD_FS_TYPE
302 property_set("ro.crypto.sd_fs_type", CRYPTO_SD_FS_TYPE);
303 property_set("ro.crypto.sd_fs_real_blkdev", CRYPTO_SD_REAL_BLKDEV);
304 property_set("ro.crypto.sd_fs_mnt_point", EXPAND(TW_INTERNAL_STORAGE_PATH));
305#endif
306 property_set("rw.km_fips_status", "ready");
307#endif
308 if (cryptfs_check_footer() == 0) {
309 Is_Encrypted = true;
310 Is_Decrypted = false;
311 Can_Be_Mounted = false;
312 Current_File_System = "emmc";
313 Setup_Image(Display_Error);
314 DataManager::SetValue(TW_IS_ENCRYPTED, 1);
315 DataManager::SetValue(TW_CRYPTO_PASSWORD, "");
316 DataManager::SetValue("tw_crypto_display", "");
317 } else {
318 LOGERR("Could not mount /data and unable to find crypto footer.\n");
319 }
320 } else {
321 LOGERR("Primary block device '%s' for mount point '%s' is not present!\n", Primary_Block_Device.c_str(), Mount_Point.c_str());
322 }
Gary Peck82599a82012-11-21 16:23:12 -0800323 } else {
324 // Filesystem is not encrypted and the mount
325 // succeeded, so get it back to the original
326 // unmounted state
327 UnMount(false);
Dees_Troy51127312012-09-08 13:08:49 -0400328 }
Ethan Yonker6277c792014-09-15 14:54:30 -0500329 if (datamedia && (!Is_Encrypted || (Is_Encrypted && Is_Decrypted)))
Dees_Troy9b21af72012-10-01 15:51:46 -0400330 Recreate_Media_Folder();
Dees_Troy9b21af72012-10-01 15:51:46 -0400331#else
Ethan Yonker6277c792014-09-15 14:54:30 -0500332 if (datamedia)
333 Recreate_Media_Folder();
Dees_Troy5bf43922012-09-07 16:07:55 -0400334#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400335 } else if (Mount_Point == "/cache") {
336 Display_Name = "Cache";
Dees_Troya13d74f2013-03-24 08:54:55 -0500337 Backup_Display_Name = Display_Name;
338 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400339 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400340 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500341 Can_Be_Backed_Up = true;
Dees_Troyce2fe772012-09-28 12:34:33 -0400342 if (Mount(false) && !TWFunc::Path_Exists("/cache/recovery/.")) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000343 LOGINFO("Recreating /cache/recovery folder.\n");
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500344 if (mkdir("/cache/recovery", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0)
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500345 return -1;
Dees_Troyb46a6842012-09-25 11:06:46 -0400346 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400347 } else if (Mount_Point == "/datadata") {
Dees_Troy51127312012-09-08 13:08:49 -0400348 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400349 Display_Name = "DataData";
Dees_Troya13d74f2013-03-24 08:54:55 -0500350 Backup_Display_Name = Display_Name;
351 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400352 Is_SubPartition = true;
353 SubPartition_Of = "/data";
Dees_Troy5bf43922012-09-07 16:07:55 -0400354 DataManager::SetValue(TW_HAS_DATADATA, 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500355 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000356 Can_Encrypt_Backup = true;
357 Use_Userdata_Encryption = false; // This whole partition should be encrypted
Dees_Troy5bf43922012-09-07 16:07:55 -0400358 } else if (Mount_Point == "/sd-ext") {
Dees_Troy51127312012-09-08 13:08:49 -0400359 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400360 Display_Name = "SD-Ext";
Dees_Troya13d74f2013-03-24 08:54:55 -0500361 Backup_Display_Name = Display_Name;
362 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400363 Wipe_Available_in_GUI = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400364 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500365 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000366 Can_Encrypt_Backup = true;
367 Use_Userdata_Encryption = true;
Dees_Troy2c50e182012-09-26 20:05:28 -0400368 } else if (Mount_Point == "/boot") {
369 Display_Name = "Boot";
Dees_Troya13d74f2013-03-24 08:54:55 -0500370 Backup_Display_Name = Display_Name;
Dees_Troy2c50e182012-09-26 20:05:28 -0400371 DataManager::SetValue("tw_boot_is_mountable", 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500372 Can_Be_Backed_Up = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400373 }
374#ifdef TW_EXTERNAL_STORAGE_PATH
375 if (Mount_Point == EXPAND(TW_EXTERNAL_STORAGE_PATH)) {
376 Is_Storage = true;
377 Storage_Path = EXPAND(TW_EXTERNAL_STORAGE_PATH);
Dees_Troyc51f1f92012-09-20 15:32:13 -0400378 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500379 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400380#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000381 if (Mount_Point == "/sdcard" || Mount_Point == "/external_sd" || Mount_Point == "/external_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400382 Is_Storage = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400383 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500384 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400385#endif
Dees_Troyb05ddee2013-01-28 20:24:50 +0000386 }
Dees_Troy8170a922012-09-18 15:40:25 -0400387#ifdef TW_INTERNAL_STORAGE_PATH
388 if (Mount_Point == EXPAND(TW_INTERNAL_STORAGE_PATH)) {
389 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500390 Is_Settings_Storage = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400391 Storage_Path = EXPAND(TW_INTERNAL_STORAGE_PATH);
Dees_Troya13d74f2013-03-24 08:54:55 -0500392 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400393 }
394#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000395 if (Mount_Point == "/emmc" || Mount_Point == "/internal_sd" || Mount_Point == "/internal_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400396 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500397 Is_Settings_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500398 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400399 }
400#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400401 } else if (Is_Image(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400402 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400403 Setup_Image(Display_Error);
Dees_Troya13d74f2013-03-24 08:54:55 -0500404 if (Mount_Point == "/boot") {
405 Display_Name = "Boot";
406 Backup_Display_Name = Display_Name;
407 Can_Be_Backed_Up = true;
408 } else if (Mount_Point == "/recovery") {
409 Display_Name = "Recovery";
410 Backup_Display_Name = Display_Name;
Dees_Troya13d74f2013-03-24 08:54:55 -0500411 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400412 }
413
Dees_Troy51127312012-09-08 13:08:49 -0400414 // Process any custom flags
415 if (Flags.size() > 0)
416 Process_Flags(Flags, Display_Error);
Dees_Troy51127312012-09-08 13:08:49 -0400417 return true;
418}
419
Hashcode62bd9e02013-11-19 21:59:42 -0800420bool TWPartition::Process_FS_Flags(string& Options, int Flags) {
421 int i;
422 char *p;
423 char *savep;
424 char fs_options[250];
425
426 strlcpy(fs_options, Options.c_str(), sizeof(fs_options));
427 Options = "";
428
429 p = strtok_r(fs_options, ",", &savep);
430 while (p) {
431 /* Look for the flag "p" in the flag list "fl"
432 * If not found, the loop exits with fl[i].name being null.
433 */
434 for (i = 0; mount_flags[i].name; i++) {
435 if (strncmp(p, mount_flags[i].name, strlen(mount_flags[i].name)) == 0) {
436 Flags |= mount_flags[i].flag;
437 break;
438 }
439 }
440
441 if (!mount_flags[i].name) {
442 if (Options.size() > 0)
443 Options += ",";
444 Options += p;
445 }
446 p = strtok_r(NULL, ",", &savep);
447 }
448
449 return true;
450}
451
Dees_Troy51127312012-09-08 13:08:49 -0400452bool TWPartition::Process_Flags(string Flags, bool Display_Error) {
453 char flags[MAX_FSTAB_LINE_LENGTH];
Dees_Troya13d74f2013-03-24 08:54:55 -0500454 int flags_len, index = 0, ptr_len;
Dees_Troy51127312012-09-08 13:08:49 -0400455 char* ptr;
Dees_Troya13d74f2013-03-24 08:54:55 -0500456 bool skip = false, has_display_name = false, has_storage_name = false, has_backup_name = false;
Dees_Troy51127312012-09-08 13:08:49 -0400457
458 strcpy(flags, Flags.c_str());
459 flags_len = Flags.size();
460 for (index = 0; index < flags_len; index++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500461 if (flags[index] == 34)
462 skip = !skip;
463 if (!skip && flags[index] == ';')
Dees_Troy51127312012-09-08 13:08:49 -0400464 flags[index] = '\0';
465 }
466
467 index = 0;
468 while (index < flags_len) {
469 while (index < flags_len && flags[index] == '\0')
470 index++;
471 if (index >= flags_len)
472 continue;
473 ptr = flags + index;
Dees_Troya13d74f2013-03-24 08:54:55 -0500474 ptr_len = strlen(ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400475 if (strcmp(ptr, "removable") == 0) {
476 Removable = true;
Ethan Yonker06c3f932014-02-02 22:11:14 -0600477 } else if (strncmp(ptr, "storage", 7) == 0) {
478 if (ptr_len == 7) {
Ethan Yonker06c3f932014-02-02 22:11:14 -0600479 Is_Storage = true;
480 } else if (ptr_len == 9) {
481 ptr += 9;
482 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y') {
483 LOGINFO("storage set to true\n");
484 Is_Storage = true;
485 } else {
486 LOGINFO("storage set to false\n");
487 Is_Storage = false;
488 }
489 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500490 } else if (strcmp(ptr, "settingsstorage") == 0) {
491 Is_Storage = true;
Matt Mowerbf4efa32014-04-14 23:25:26 -0500492 } else if (strcmp(ptr, "andsec") == 0) {
493 Has_Android_Secure = true;
Dees_Troy63c8df72012-09-10 14:02:05 -0400494 } else if (strcmp(ptr, "canbewiped") == 0) {
495 Can_Be_Wiped = true;
Hashcodedabfd492013-08-29 22:45:30 -0700496 } else if (strcmp(ptr, "usermrf") == 0) {
497 Use_Rm_Rf = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500498 } else if (ptr_len > 7 && strncmp(ptr, "backup=", 7) == 0) {
499 ptr += 7;
500 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y')
501 Can_Be_Backed_Up = true;
502 else
503 Can_Be_Backed_Up = false;
Dees_Troy63c8df72012-09-10 14:02:05 -0400504 } else if (strcmp(ptr, "wipeingui") == 0) {
505 Can_Be_Wiped = true;
506 Wipe_Available_in_GUI = true;
507 } else if (strcmp(ptr, "wipeduringfactoryreset") == 0) {
508 Can_Be_Wiped = true;
509 Wipe_Available_in_GUI = true;
510 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500511 } else if (ptr_len > 15 && strncmp(ptr, "subpartitionof=", 15) == 0) {
Dees_Troy2c4c26f2013-01-28 15:26:43 +0000512 ptr += 15;
Dees_Troy51127312012-09-08 13:08:49 -0400513 Is_SubPartition = true;
514 SubPartition_Of = ptr;
Dees_Troy68cab492012-12-12 19:29:35 +0000515 } else if (strcmp(ptr, "ignoreblkid") == 0) {
516 Ignore_Blkid = true;
Dees_Troy16c2b312013-01-15 16:51:18 +0000517 } else if (strcmp(ptr, "retainlayoutversion") == 0) {
518 Retain_Layout_Version = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500519 } else if (ptr_len > 8 && strncmp(ptr, "symlink=", 8) == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400520 ptr += 8;
521 Symlink_Path = ptr;
Dees_Troya13d74f2013-03-24 08:54:55 -0500522 } else if (ptr_len > 8 && strncmp(ptr, "display=", 8) == 0) {
523 has_display_name = true;
Dees_Troy51127312012-09-08 13:08:49 -0400524 ptr += 8;
Dees_Troya13d74f2013-03-24 08:54:55 -0500525 if (*ptr == '\"') ptr++;
Dees_Troy51127312012-09-08 13:08:49 -0400526 Display_Name = ptr;
Dees_Troya13d74f2013-03-24 08:54:55 -0500527 if (Display_Name.substr(Display_Name.size() - 1, 1) == "\"") {
528 Display_Name.resize(Display_Name.size() - 1);
529 }
530 } else if (ptr_len > 11 && strncmp(ptr, "storagename=", 11) == 0) {
531 has_storage_name = true;
532 ptr += 11;
533 if (*ptr == '\"') ptr++;
534 Storage_Name = ptr;
535 if (Storage_Name.substr(Storage_Name.size() - 1, 1) == "\"") {
536 Storage_Name.resize(Storage_Name.size() - 1);
537 }
538 } else if (ptr_len > 11 && strncmp(ptr, "backupname=", 10) == 0) {
539 has_backup_name = true;
540 ptr += 10;
541 if (*ptr == '\"') ptr++;
542 Backup_Display_Name = ptr;
543 if (Backup_Display_Name.substr(Backup_Display_Name.size() - 1, 1) == "\"") {
544 Backup_Display_Name.resize(Backup_Display_Name.size() - 1);
545 }
546 } else if (ptr_len > 10 && strncmp(ptr, "blocksize=", 10) == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400547 ptr += 10;
548 Format_Block_Size = atoi(ptr);
Dees_Troya13d74f2013-03-24 08:54:55 -0500549 } else if (ptr_len > 7 && strncmp(ptr, "length=", 7) == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400550 ptr += 7;
551 Length = atoi(ptr);
Dees_Troy83bd4832013-05-04 12:39:56 +0000552 } else if (ptr_len > 17 && strncmp(ptr, "canencryptbackup=", 17) == 0) {
553 ptr += 17;
554 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y')
555 Can_Encrypt_Backup = true;
556 else
557 Can_Encrypt_Backup = false;
558 } else if (ptr_len > 21 && strncmp(ptr, "userdataencryptbackup=", 21) == 0) {
559 ptr += 21;
560 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y') {
561 Can_Encrypt_Backup = true;
562 Use_Userdata_Encryption = true;
563 } else {
564 Use_Userdata_Encryption = false;
565 }
Hashcode62bd9e02013-11-19 21:59:42 -0800566 } else if (ptr_len > 8 && strncmp(ptr, "fsflags=", 8) == 0) {
567 ptr += 8;
568 if (*ptr == '\"') ptr++;
569
570 Mount_Options = ptr;
571 if (Mount_Options.substr(Mount_Options.size() - 1, 1) == "\"") {
572 Mount_Options.resize(Mount_Options.size() - 1);
573 }
574 Process_FS_Flags(Mount_Options, Mount_Flags);
Dees_Troy51127312012-09-08 13:08:49 -0400575 } else {
576 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000577 LOGERR("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400578 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000579 LOGINFO("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400580 }
581 while (index < flags_len && flags[index] != '\0')
582 index++;
583 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500584 if (has_display_name && !has_storage_name)
585 Storage_Name = Display_Name;
586 if (!has_display_name && has_storage_name)
587 Display_Name = Storage_Name;
Dees_Troy74fb2e92013-04-15 14:35:47 +0000588 if (has_display_name && !has_backup_name && Backup_Display_Name != "Android Secure")
Dees_Troya13d74f2013-03-24 08:54:55 -0500589 Backup_Display_Name = Display_Name;
590 if (!has_display_name && has_backup_name)
591 Display_Name = Backup_Display_Name;
Dees_Troy51127312012-09-08 13:08:49 -0400592 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400593}
594
Dees_Troy5bf43922012-09-07 16:07:55 -0400595bool TWPartition::Is_File_System(string File_System) {
596 if (File_System == "ext2" ||
Dees_Troy63c8df72012-09-10 14:02:05 -0400597 File_System == "ext3" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400598 File_System == "ext4" ||
599 File_System == "vfat" ||
600 File_System == "ntfs" ||
601 File_System == "yaffs2" ||
bigbiff bigbiff3e146522012-11-14 14:32:59 -0500602 File_System == "exfat" ||
Dees_Troye5017042013-08-29 16:38:55 +0000603 File_System == "f2fs" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400604 File_System == "auto")
605 return true;
606 else
607 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400608}
609
Dees_Troy5bf43922012-09-07 16:07:55 -0400610bool TWPartition::Is_Image(string File_System) {
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400611 if (File_System == "emmc" || File_System == "mtd" || File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -0400612 return true;
613 else
614 return false;
615}
616
Dees_Troy51127312012-09-08 13:08:49 -0400617bool TWPartition::Make_Dir(string Path, bool Display_Error) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400618 if (!TWFunc::Path_Exists(Path)) {
Dees_Troy51127312012-09-08 13:08:49 -0400619 if (mkdir(Path.c_str(), 0777) == -1) {
620 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000621 LOGERR("Can not create '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400622 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000623 LOGINFO("Can not create '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400624 return false;
625 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000626 LOGINFO("Created '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400627 return true;
628 }
629 }
630 return true;
631}
632
Dees_Troy5bf43922012-09-07 16:07:55 -0400633void TWPartition::Setup_File_System(bool Display_Error) {
634 struct statfs st;
635
636 Can_Be_Mounted = true;
637 Can_Be_Wiped = true;
638
Dees_Troy5bf43922012-09-07 16:07:55 -0400639 // Make the mount point folder if it doesn't exist
Dees_Troy51127312012-09-08 13:08:49 -0400640 Make_Dir(Mount_Point, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400641 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
642 Backup_Name = Display_Name;
643 Backup_Method = FILES;
644}
645
646void TWPartition::Setup_Image(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400647 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
648 Backup_Name = Display_Name;
Gary Peck82599a82012-11-21 16:23:12 -0800649 if (Current_File_System == "emmc")
Dees_Troy5bf43922012-09-07 16:07:55 -0400650 Backup_Method = DD;
Gary Peck82599a82012-11-21 16:23:12 -0800651 else if (Current_File_System == "mtd" || Current_File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -0400652 Backup_Method = FLASH_UTILS;
653 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000654 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 -0400655 if (Find_Partition_Size()) {
656 Used = Size;
657 Backup_Size = Size;
Dees_Troy51a0e822012-09-05 15:24:24 -0400658 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400659 if (Display_Error)
Dees Troyd932ce12013-10-18 17:12:59 +0000660 LOGERR("Unable to find partition size for '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400661 else
Dees Troyd932ce12013-10-18 17:12:59 +0000662 LOGINFO("Unable to find partition size for '%s'\n", Mount_Point.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400663 }
664}
665
Dees_Troye58d5262012-09-21 12:27:57 -0400666void TWPartition::Setup_AndSec(void) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500667 Backup_Display_Name = "Android Secure";
Dees_Troye58d5262012-09-21 12:27:57 -0400668 Backup_Name = "and-sec";
Dees_Troya13d74f2013-03-24 08:54:55 -0500669 Can_Be_Backed_Up = true;
Dees_Troye58d5262012-09-21 12:27:57 -0400670 Has_Android_Secure = true;
671 Symlink_Path = Mount_Point + "/.android_secure";
672 Symlink_Mount_Point = "/and-sec";
673 Backup_Path = Symlink_Mount_Point;
674 Make_Dir("/and-sec", true);
675 Recreate_AndSec_Folder();
Ethan Yonkerd4d10732014-02-03 15:27:52 -0600676 Mount_Storage_Retry();
Dees_Troye58d5262012-09-21 12:27:57 -0400677}
678
that9e0593e2014-10-08 00:01:24 +0200679void TWPartition::Setup_Data_Media() {
Ethan Yonker6277c792014-09-15 14:54:30 -0500680 LOGINFO("Setting up '%s' as data/media emulated storage.\n", Mount_Point.c_str());
681 Storage_Name = "Internal Storage";
682 Has_Data_Media = true;
683 Is_Storage = true;
684 Is_Settings_Storage = true;
685 Storage_Path = "/data/media";
686 Symlink_Path = Storage_Path;
687 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
688 Make_Dir("/emmc", false);
689 Symlink_Mount_Point = "/emmc";
690 } else {
691 Make_Dir("/sdcard", false);
692 Symlink_Mount_Point = "/sdcard";
693 }
694 if (Mount(false) && TWFunc::Path_Exists("/data/media/0")) {
695 Storage_Path = "/data/media/0";
696 Symlink_Path = Storage_Path;
697 DataManager::SetValue(TW_INTERNAL_PATH, "/data/media/0");
698 UnMount(true);
699 }
Ethan Yonker6277c792014-09-15 14:54:30 -0500700 DataManager::SetValue("tw_has_internal", 1);
701 DataManager::SetValue("tw_has_data_media", 1);
702 du.add_absolute_dir("/data/media");
703}
704
Dees_Troy5bf43922012-09-07 16:07:55 -0400705void TWPartition::Find_Real_Block_Device(string& Block, bool Display_Error) {
706 char device[512], realDevice[512];
707
708 strcpy(device, Block.c_str());
709 memset(realDevice, 0, sizeof(realDevice));
710 while (readlink(device, realDevice, sizeof(realDevice)) > 0)
711 {
712 strcpy(device, realDevice);
713 memset(realDevice, 0, sizeof(realDevice));
714 }
715
716 if (device[0] != '/') {
717 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000718 LOGERR("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400719 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000720 LOGINFO("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400721 return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400722 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400723 Block = device;
724 return;
725 }
726}
727
Dees_Troy8e337f32012-10-13 22:07:49 -0400728void TWPartition::Mount_Storage_Retry(void) {
729 // On some devices, storage doesn't want to mount right away, retry and sleep
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500730 if (!Mount(true)) {
Dees_Troy8e337f32012-10-13 22:07:49 -0400731 int retry_count = 5;
732 while (retry_count > 0 && !Mount(false)) {
733 usleep(500000);
734 retry_count--;
735 }
736 Mount(true);
737 }
738}
739
Dees_Troy38bd7602012-09-14 13:33:53 -0400740bool TWPartition::Find_MTD_Block_Device(string MTD_Name) {
741 FILE *fp = NULL;
742 char line[255];
743
744 fp = fopen("/proc/mtd", "rt");
745 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000746 LOGERR("Device does not support /proc/mtd\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400747 return false;
748 }
749
750 while (fgets(line, sizeof(line), fp) != NULL)
751 {
752 char device[32], label[32];
753 unsigned long size = 0;
754 char* fstype = NULL;
755 int deviceId;
756
757 sscanf(line, "%s %lx %*s %*c%s", device, &size, label);
758
759 // Skip header and blank lines
760 if ((strcmp(device, "dev:") == 0) || (strlen(line) < 8))
761 continue;
762
763 // Strip off the trailing " from the label
764 label[strlen(label)-1] = '\0';
765
766 if (strcmp(label, MTD_Name.c_str()) == 0) {
767 // We found our device
768 // Strip off the trailing : from the device
769 device[strlen(device)-1] = '\0';
770 if (sscanf(device,"mtd%d", &deviceId) == 1) {
771 sprintf(device, "/dev/block/mtdblock%d", deviceId);
772 Primary_Block_Device = device;
Dees_Troy76543db2013-06-19 16:24:30 +0000773 fclose(fp);
774 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -0400775 }
776 }
777 }
778 fclose(fp);
779
780 return false;
781}
782
Dees_Troy51127312012-09-08 13:08:49 -0400783bool TWPartition::Get_Size_Via_statfs(bool Display_Error) {
784 struct statfs st;
785 string Local_Path = Mount_Point + "/.";
786
787 if (!Mount(Display_Error))
788 return false;
789
790 if (statfs(Local_Path.c_str(), &st) != 0) {
791 if (!Removable) {
792 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000793 LOGERR("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400794 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000795 LOGINFO("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400796 }
797 return false;
798 }
799 Size = (st.f_blocks * st.f_bsize);
800 Used = ((st.f_blocks - st.f_bfree) * st.f_bsize);
801 Free = (st.f_bfree * st.f_bsize);
802 Backup_Size = Used;
803 return true;
804}
805
806bool TWPartition::Get_Size_Via_df(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400807 FILE* fp;
808 char command[255], line[512];
809 int include_block = 1;
810 unsigned int min_len;
811
812 if (!Mount(Display_Error))
813 return false;
814
Dees_Troy38bd7602012-09-14 13:33:53 -0400815 min_len = Actual_Block_Device.size() + 2;
Dees_Troy51127312012-09-08 13:08:49 -0400816 sprintf(command, "df %s > /tmp/dfoutput.txt", Mount_Point.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +0200817 TWFunc::Exec_Cmd(command);
Dees_Troy51127312012-09-08 13:08:49 -0400818 fp = fopen("/tmp/dfoutput.txt", "rt");
819 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000820 LOGINFO("Unable to open /tmp/dfoutput.txt.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400821 return false;
Dees_Troy51127312012-09-08 13:08:49 -0400822 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400823
824 while (fgets(line, sizeof(line), fp) != NULL)
825 {
826 unsigned long blocks, used, available;
827 char device[64];
828 char tmpString[64];
829
830 if (strncmp(line, "Filesystem", 10) == 0)
831 continue;
832 if (strlen(line) < min_len) {
833 include_block = 0;
834 continue;
835 }
836 if (include_block) {
837 sscanf(line, "%s %lu %lu %lu", device, &blocks, &used, &available);
838 } else {
839 // The device block string is so long that the df information is on the next line
840 int space_count = 0;
Dees_Troye58d5262012-09-21 12:27:57 -0400841 sprintf(tmpString, "/dev/block/%s", Actual_Block_Device.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400842 while (tmpString[space_count] == 32)
843 space_count++;
844 sscanf(line + space_count, "%lu %lu %lu", &blocks, &used, &available);
845 }
846
847 // Adjust block size to byte size
848 Size = blocks * 1024ULL;
849 Used = used * 1024ULL;
850 Free = available * 1024ULL;
851 Backup_Size = Used;
852 }
853 fclose(fp);
854 return true;
855}
856
Dees_Troy5bf43922012-09-07 16:07:55 -0400857bool TWPartition::Find_Partition_Size(void) {
858 FILE* fp;
859 char line[512];
860 string tmpdevice;
861
igoriok87e3d932013-01-31 21:03:53 +0200862 fp = fopen("/proc/dumchar_info", "rt");
863 if (fp != NULL) {
864 while (fgets(line, sizeof(line), fp) != NULL)
865 {
866 char label[32], device[32];
867 unsigned long size = 0;
868
thatd43bf2d2014-09-21 23:13:02 +0200869 sscanf(line, "%s %lx %*x %*u %s", label, &size, device);
igoriok87e3d932013-01-31 21:03:53 +0200870
bigbiff bigbiff34684ff2013-12-01 21:03:45 -0500871 // Skip header, annotation and blank lines
igoriok87e3d932013-01-31 21:03:53 +0200872 if ((strncmp(device, "/dev/", 5) != 0) || (strlen(line) < 8))
873 continue;
874
875 tmpdevice = "/dev/";
876 tmpdevice += label;
877 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
878 Size = size;
879 fclose(fp);
880 return true;
881 }
882 }
883 }
884
Dees_Troy5bf43922012-09-07 16:07:55 -0400885 // In this case, we'll first get the partitions we care about (with labels)
886 fp = fopen("/proc/partitions", "rt");
887 if (fp == NULL)
888 return false;
889
890 while (fgets(line, sizeof(line), fp) != NULL)
891 {
892 unsigned long major, minor, blocks;
893 char device[512];
894 char tmpString[64];
895
Dees_Troy63c8df72012-09-10 14:02:05 -0400896 if (strlen(line) < 7 || line[0] == 'm') continue;
Dees_Troy5bf43922012-09-07 16:07:55 -0400897 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
898
899 tmpdevice = "/dev/block/";
900 tmpdevice += device;
Dees_Troy38bd7602012-09-14 13:33:53 -0400901 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400902 // Adjust block size to byte size
903 Size = blocks * 1024ULL;
904 fclose(fp);
905 return true;
906 }
907 }
908 fclose(fp);
909 return false;
910}
911
Dees_Troy5bf43922012-09-07 16:07:55 -0400912bool TWPartition::Is_Mounted(void) {
913 if (!Can_Be_Mounted)
914 return false;
915
916 struct stat st1, st2;
917 string test_path;
918
919 // Check to see if the mount point directory exists
920 test_path = Mount_Point + "/.";
921 if (stat(test_path.c_str(), &st1) != 0) return false;
922
923 // Check to see if the directory above the mount point exists
924 test_path = Mount_Point + "/../.";
925 if (stat(test_path.c_str(), &st2) != 0) return false;
926
927 // Compare the device IDs -- if they match then we're (probably) using tmpfs instead of an actual device
928 int ret = (st1.st_dev != st2.st_dev) ? true : false;
929
930 return ret;
931}
932
933bool TWPartition::Mount(bool Display_Error) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000934 int exfat_mounted = 0;
935
Dees_Troy5bf43922012-09-07 16:07:55 -0400936 if (Is_Mounted()) {
937 return true;
938 } else if (!Can_Be_Mounted) {
939 return false;
940 }
Dees_Troy38bd7602012-09-14 13:33:53 -0400941
942 Find_Actual_Block_Device();
943
944 // Check the current file system before mounting
945 Check_FS_Type();
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000946 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/exfat-fuse")) {
Dees_Troye34c1332013-02-06 19:13:00 +0000947 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 +0000948 LOGINFO("cmd: %s\n", cmd.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000949 string result;
950 if (TWFunc::Exec_Cmd(cmd, result) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000951 LOGINFO("exfat-fuse failed to mount with result '%s', trying vfat\n", result.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000952 Current_File_System = "vfat";
953 } else {
954#ifdef TW_NO_EXFAT_FUSE
955 UnMount(false);
956 // We'll let the kernel handle it but using exfat-fuse to detect if the file system is actually exfat
957 // Some kernels let us mount vfat as exfat which doesn't work out too well
958#else
959 exfat_mounted = 1;
960#endif
961 }
962 }
Dees_Troy22042032012-12-18 21:23:08 +0000963 if (Fstab_File_System == "yaffs2") {
964 // mount an MTD partition as a YAFFS2 filesystem.
Dees_Troy76543db2013-06-19 16:24:30 +0000965 const unsigned long flags = MS_NOATIME | MS_NODEV | MS_NODIRATIME;
966 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags, NULL) < 0) {
967 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags | MS_RDONLY, NULL) < 0) {
968 if (Display_Error)
969 LOGERR("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
970 else
971 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
972 return false;
973 } else {
974 LOGINFO("Mounted '%s' (MTD) as RO\n", Mount_Point.c_str());
975 return true;
976 }
977 } else {
978 struct stat st;
979 string test_path = Mount_Point;
980 if (stat(test_path.c_str(), &st) < 0) {
981 if (Display_Error)
982 LOGERR("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
983 else
984 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
985 return false;
986 }
987 mode_t new_mode = st.st_mode | S_IXUSR | S_IXGRP | S_IXOTH;
988 if (new_mode != st.st_mode) {
989 LOGINFO("Fixing execute permissions for %s\n", Mount_Point.c_str());
990 if (chmod(Mount_Point.c_str(), new_mode) < 0) {
991 if (Display_Error)
992 LOGERR("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
993 else
994 LOGINFO("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
995 return false;
996 }
997 }
Dees_Troy22042032012-12-18 21:23:08 +0000998 return true;
Dees_Troy76543db2013-06-19 16:24:30 +0000999 }
Dees Troy216e0422014-02-07 03:46:42 +00001000 } 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 +00001001#ifdef TW_NO_EXFAT_FUSE
1002 if (Current_File_System == "exfat") {
Dees_Troy2673cec2013-04-02 20:22:16 +00001003 LOGINFO("Mounting exfat failed, trying vfat...\n");
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001004 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), "vfat", 0, NULL) != 0) {
Dees_Troy85f44ed2013-01-09 18:42:36 +00001005 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001006 LOGERR("Unable to mount '%s'\n", Mount_Point.c_str());
Dees_Troy85f44ed2013-01-09 18:42:36 +00001007 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001008 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
Hashcode62bd9e02013-11-19 21:59:42 -08001009 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 +00001010 return false;
Dees_Troy85f44ed2013-01-09 18:42:36 +00001011 }
Dees_Troyb05ddee2013-01-28 20:24:50 +00001012 } else {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001013#endif
bigbiff bigbiff26774a02014-03-29 18:22:00 -04001014 if (!Removable && Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001015 LOGERR("Unable to mount '%s'\n", Mount_Point.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001016 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001017 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
1018 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 +00001019 return false;
1020#ifdef TW_NO_EXFAT_FUSE
Dees_Troy85f44ed2013-01-09 18:42:36 +00001021 }
1022#endif
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001023 }
1024#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
1025 string MetaEcfsFile = EXPAND(TW_EXTERNAL_STORAGE_PATH);
1026 MetaEcfsFile += "/.MetaEcfsFile";
1027 if (EcryptFS_Password.size() > 0 && PartitionManager.Mount_By_Path("/data", false) && TWFunc::Path_Exists(MetaEcfsFile)) {
1028 if (mount_ecryptfs_drive(EcryptFS_Password.c_str(), Mount_Point.c_str(), Mount_Point.c_str(), 0) != 0) {
1029 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001030 LOGERR("Unable to mount ecryptfs for '%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 ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001033 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001034 LOGINFO("Successfully mounted ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001035 Is_Decrypted = true;
Dees_Troy51127312012-09-08 13:08:49 -04001036 }
Dees_Troy066eb302013-08-23 17:20:32 +00001037 } else if (Mount_Point == EXPAND(TW_EXTERNAL_STORAGE_PATH)) {
1038 if (Is_Decrypted)
1039 LOGINFO("Mounting external storage, '%s' is not encrypted\n", Mount_Point.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001040 Is_Decrypted = false;
1041 }
1042#endif
1043 if (Removable)
1044 Update_Size(Display_Error);
1045
1046 if (!Symlink_Mount_Point.empty()) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001047 string Command = "mount '" + Symlink_Path + "' '" + Symlink_Mount_Point + "'";
1048 TWFunc::Exec_Cmd(Command);
Dees_Troy5bf43922012-09-07 16:07:55 -04001049 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001050 return true;
1051}
1052
1053bool TWPartition::UnMount(bool Display_Error) {
1054 if (Is_Mounted()) {
1055 int never_unmount_system;
1056
1057 DataManager::GetValue(TW_DONT_UNMOUNT_SYSTEM, never_unmount_system);
1058 if (never_unmount_system == 1 && Mount_Point == "/system")
1059 return true; // Never unmount system if you're not supposed to unmount it
1060
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001061 if (Is_Storage)
1062 TWFunc::Toggle_MTP(false);
1063
Dees_Troyc8bafa12013-01-10 15:43:00 +00001064#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
1065 if (EcryptFS_Password.size() > 0) {
1066 if (unmount_ecryptfs_drive(Mount_Point.c_str()) != 0) {
1067 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001068 LOGERR("Unable to unmount ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troyc8bafa12013-01-10 15:43:00 +00001069 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001070 LOGINFO("Unable to unmount ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troyc8bafa12013-01-10 15:43:00 +00001071 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001072 LOGINFO("Successfully unmounted ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troyc8bafa12013-01-10 15:43:00 +00001073 }
1074 }
1075#endif
1076
Dees_Troy38bd7602012-09-14 13:33:53 -04001077 if (!Symlink_Mount_Point.empty())
1078 umount(Symlink_Mount_Point.c_str());
1079
Dees_Troyb05ddee2013-01-28 20:24:50 +00001080 umount(Mount_Point.c_str());
1081 if (Is_Mounted()) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001082 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001083 LOGERR("Unable to unmount '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001084 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001085 LOGINFO("Unable to unmount '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001086 return false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001087 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -04001088 return true;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001089 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001090 } else {
1091 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001092 }
1093}
1094
Gary Peck43acadf2012-11-21 21:19:01 -08001095bool TWPartition::Wipe(string New_File_System) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001096 bool wiped = false, update_crypt = false, recreate_media = true, mtp_toggle = true;
Dees_Troy16c2b312013-01-15 16:51:18 +00001097 int check;
1098 string Layout_Filename = Mount_Point + "/.layout_version";
1099
Dees_Troy38bd7602012-09-14 13:33:53 -04001100 if (!Can_Be_Wiped) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001101 LOGERR("Partition '%s' cannot be wiped.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001102 return false;
1103 }
1104
Dees_Troyc51f1f92012-09-20 15:32:13 -04001105 if (Mount_Point == "/cache")
Dees_Troy2673cec2013-04-02 20:22:16 +00001106 Log_Offset = 0;
Dees_Troyc51f1f92012-09-20 15:32:13 -04001107
Dees_Troyce675462013-01-09 19:48:21 +00001108#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
1109 if (Mount_Point == "/data" && Mount(false)) {
1110 if (TWFunc::Path_Exists("/data/system/edk_p_sd"))
1111 TWFunc::copy_file("/data/system/edk_p_sd", "/tmp/edk_p_sd", 0600);
1112 }
1113#endif
1114
Dees_Troy16c2b312013-01-15 16:51:18 +00001115 if (Retain_Layout_Version && Mount(false) && TWFunc::Path_Exists(Layout_Filename))
1116 TWFunc::copy_file(Layout_Filename, "/.layout_version", 0600);
1117 else
1118 unlink("/.layout_version");
Dees_Troy38bd7602012-09-14 13:33:53 -04001119
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001120 if (Has_Data_Media && Current_File_System == New_File_System) {
Dees_Troy16c2b312013-01-15 16:51:18 +00001121 wiped = Wipe_Data_Without_Wiping_Media();
Ethan Yonker5eac2222014-06-11 12:22:55 -05001122 recreate_media = false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001123 mtp_toggle = false;
Dees_Troy16c2b312013-01-15 16:51:18 +00001124 } else {
Dees_Troy16c2b312013-01-15 16:51:18 +00001125 DataManager::GetValue(TW_RM_RF_VAR, check);
1126
Hashcodedabfd492013-08-29 22:45:30 -07001127 if (check || Use_Rm_Rf)
Dees_Troy16c2b312013-01-15 16:51:18 +00001128 wiped = Wipe_RMRF();
1129 else if (New_File_System == "ext4")
1130 wiped = Wipe_EXT4();
1131 else if (New_File_System == "ext2" || New_File_System == "ext3")
1132 wiped = Wipe_EXT23(New_File_System);
1133 else if (New_File_System == "vfat")
1134 wiped = Wipe_FAT();
1135 else if (New_File_System == "exfat")
1136 wiped = Wipe_EXFAT();
1137 else if (New_File_System == "yaffs2")
1138 wiped = Wipe_MTD();
Dees_Troye5017042013-08-29 16:38:55 +00001139 else if (New_File_System == "f2fs")
1140 wiped = Wipe_F2FS();
Dees_Troy16c2b312013-01-15 16:51:18 +00001141 else {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001142 if (Is_Storage) {
1143 TWFunc::Toggle_MTP(true);
1144 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001145 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 +00001146 unlink("/.layout_version");
1147 return false;
1148 }
1149 update_crypt = wiped;
Gary Pecke8bc5d72012-12-21 06:45:25 -08001150 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001151
Gary Pecke8bc5d72012-12-21 06:45:25 -08001152 if (wiped) {
Dees_Troyce675462013-01-09 19:48:21 +00001153#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
1154 if (Mount_Point == "/data" && Mount(false)) {
1155 if (TWFunc::Path_Exists("/tmp/edk_p_sd")) {
1156 Make_Dir("/data/system", true);
1157 TWFunc::copy_file("/tmp/edk_p_sd", "/data/system/edk_p_sd", 0600);
1158 }
1159 }
1160#endif
Dees_Troy16c2b312013-01-15 16:51:18 +00001161
Dees_Troy1c1ac442013-01-17 21:42:14 +00001162 if (Mount_Point == "/cache")
1163 DataManager::Output_Version();
1164
Dees_Troy16c2b312013-01-15 16:51:18 +00001165 if (TWFunc::Path_Exists("/.layout_version") && Mount(false))
1166 TWFunc::copy_file("/.layout_version", Layout_Filename, 0600);
1167
1168 if (update_crypt) {
1169 Setup_File_System(false);
1170 if (Is_Encrypted && !Is_Decrypted) {
1171 // just wiped an encrypted partition back to its unencrypted state
1172 Is_Encrypted = false;
1173 Is_Decrypted = false;
1174 Decrypted_Block_Device = "";
1175 if (Mount_Point == "/data") {
1176 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1177 DataManager::SetValue(TW_IS_DECRYPTED, 0);
1178 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001179 }
1180 }
Ethan Yonker5eac2222014-06-11 12:22:55 -05001181
1182 if (Mount_Point == "/data" && Has_Data_Media && recreate_media) {
1183 Recreate_Media_Folder();
1184 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001185 }
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001186 if (Is_Storage && mtp_toggle) {
1187 TWFunc::Toggle_MTP(true);
1188 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001189 return wiped;
Dees_Troy51a0e822012-09-05 15:24:24 -04001190}
1191
Gary Peck43acadf2012-11-21 21:19:01 -08001192bool TWPartition::Wipe() {
Gary Peck82599a82012-11-21 16:23:12 -08001193 if (Is_File_System(Current_File_System))
1194 return Wipe(Current_File_System);
1195 else
1196 return Wipe(Fstab_File_System);
Gary Peck43acadf2012-11-21 21:19:01 -08001197}
1198
Dees_Troye58d5262012-09-21 12:27:57 -04001199bool TWPartition::Wipe_AndSec(void) {
1200 if (!Has_Android_Secure)
1201 return false;
1202
Dees_Troye58d5262012-09-21 12:27:57 -04001203 if (!Mount(true))
1204 return false;
1205
Dees_Troy2673cec2013-04-02 20:22:16 +00001206 gui_print("Wiping %s\n", Backup_Display_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001207 TWFunc::removeDir(Mount_Point + "/.android_secure/", true);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001208 return true;
Dees_Troye58d5262012-09-21 12:27:57 -04001209}
1210
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001211bool TWPartition::Can_Repair() {
1212 if (Current_File_System == "vfat" && TWFunc::Path_Exists("/sbin/dosfsck"))
1213 return true;
1214 else if ((Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") && TWFunc::Path_Exists("/sbin/e2fsck"))
1215 return true;
1216 else if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/fsck.exfat"))
1217 return true;
1218 else if (Current_File_System == "f2fs" && TWFunc::Path_Exists("/sbin/fsck.f2fs"))
1219 return true;
1220 return false;
1221}
1222
1223bool TWPartition::Repair() {
1224 string command;
1225
1226 if (Current_File_System == "vfat") {
1227 if (!TWFunc::Path_Exists("/sbin/dosfsck")) {
1228 gui_print("dosfsck does not exist! Cannot repair!\n");
1229 return false;
1230 }
1231 if (!UnMount(true))
1232 return false;
1233 gui_print("Repairing %s using dosfsck...\n", Display_Name.c_str());
1234 Find_Actual_Block_Device();
1235 command = "/sbin/dosfsck -y " + Actual_Block_Device;
1236 LOGINFO("Repair command: %s\n", command.c_str());
1237 if (TWFunc::Exec_Cmd(command) == 0) {
1238 gui_print("Done.\n");
1239 return true;
1240 } else {
1241 LOGERR("Unable to repair '%s'.\n", Mount_Point.c_str());
1242 return false;
1243 }
1244 }
1245 if (Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") {
1246 if (!TWFunc::Path_Exists("/sbin/e2fsck")) {
1247 gui_print("e2fsck does not exist! Cannot repair!\n");
1248 return false;
1249 }
1250 if (!UnMount(true))
1251 return false;
1252 gui_print("Repairing %s using e2fsck...\n", Display_Name.c_str());
1253 Find_Actual_Block_Device();
1254 command = "/sbin/e2fsck -p " + Actual_Block_Device;
1255 LOGINFO("Repair command: %s\n", command.c_str());
1256 if (TWFunc::Exec_Cmd(command) == 0) {
1257 gui_print("Done.\n");
1258 return true;
1259 } else {
1260 LOGERR("Unable to repair '%s'.\n", Mount_Point.c_str());
1261 return false;
1262 }
1263 }
1264 if (Current_File_System == "exfat") {
1265 if (!TWFunc::Path_Exists("/sbin/fsck.exfat")) {
1266 gui_print("fsck.exfat does not exist! Cannot repair!\n");
1267 return false;
1268 }
1269 if (!UnMount(true))
1270 return false;
1271 gui_print("Repairing %s using fsck.exfat...\n", Display_Name.c_str());
1272 Find_Actual_Block_Device();
1273 command = "/sbin/fsck.exfat " + Actual_Block_Device;
1274 LOGINFO("Repair command: %s\n", command.c_str());
1275 if (TWFunc::Exec_Cmd(command) == 0) {
1276 gui_print("Done.\n");
1277 return true;
1278 } else {
1279 LOGERR("Unable to repair '%s'.\n", Mount_Point.c_str());
1280 return false;
1281 }
1282 }
1283 if (Current_File_System == "f2fs") {
1284 if (!TWFunc::Path_Exists("/sbin/fsck.f2fs")) {
1285 gui_print("fsck.f2fs does not exist! Cannot repair!\n");
1286 return false;
1287 }
1288 if (!UnMount(true))
1289 return false;
1290 gui_print("Repairing %s using fsck.f2fs...\n", Display_Name.c_str());
1291 Find_Actual_Block_Device();
1292 command = "/sbin/fsck.f2fs " + Actual_Block_Device;
1293 LOGINFO("Repair command: %s\n", command.c_str());
1294 if (TWFunc::Exec_Cmd(command) == 0) {
1295 gui_print("Done.\n");
1296 return true;
1297 } else {
1298 LOGERR("Unable to repair '%s'.\n", Mount_Point.c_str());
1299 return false;
1300 }
1301 }
1302 return false;
1303}
1304
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001305bool 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 -04001306 if (Backup_Method == FILES)
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001307 return Backup_Tar(backup_folder, overall_size, other_backups_size);
Dees_Troy38bd7602012-09-14 13:33:53 -04001308 else if (Backup_Method == DD)
1309 return Backup_DD(backup_folder);
1310 else if (Backup_Method == FLASH_UTILS)
1311 return Backup_Dump_Image(backup_folder);
Dees_Troy2673cec2013-04-02 20:22:16 +00001312 LOGERR("Unknown backup method for '%s'\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001313 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001314}
1315
Dees_Troy43d8b002012-09-17 16:00:01 -04001316bool TWPartition::Check_MD5(string restore_folder) {
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001317 string Full_Filename, md5file;
Dees_Troy43d8b002012-09-17 16:00:01 -04001318 char split_filename[512];
1319 int index = 0;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001320 twrpDigest md5sum;
Dees_Troy43d8b002012-09-17 16:00:01 -04001321
Captain Throwbackff5935f2014-09-23 16:08:34 -04001322 sync();
1323
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001324 memset(split_filename, 0, sizeof(split_filename));
Dees_Troy43d8b002012-09-17 16:00:01 -04001325 Full_Filename = restore_folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04001326 if (!TWFunc::Path_Exists(Full_Filename)) {
1327 // This is a split archive, we presume
1328 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
Dees_Troy2673cec2013-04-02 20:22:16 +00001329 LOGINFO("split_filename: %s\n", split_filename);
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001330 md5file = split_filename;
1331 md5file += ".md5";
1332 if (!TWFunc::Path_Exists(md5file)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001333 LOGERR("No md5 file found for '%s'.\n", split_filename);
1334 LOGERR("Please unselect Enable MD5 verification to restore.\n");
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001335 return false;
1336 }
1337 md5sum.setfn(split_filename);
Dees_Troy83bd4832013-05-04 12:39:56 +00001338 while (index < 1000) {
1339 if (TWFunc::Path_Exists(split_filename) && md5sum.verify_md5digest() != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001340 LOGERR("MD5 failed to match on '%s'.\n", split_filename);
Dees_Troy43d8b002012-09-17 16:00:01 -04001341 return false;
1342 }
1343 index++;
1344 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001345 md5sum.setfn(split_filename);
Dees_Troy43d8b002012-09-17 16:00:01 -04001346 }
Dees_Troy4a2a1262012-09-18 09:33:47 -04001347 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001348 } else {
1349 // Single file archive
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001350 md5file = Full_Filename + ".md5";
1351 if (!TWFunc::Path_Exists(md5file)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001352 LOGERR("No md5 file found for '%s'.\n", Full_Filename.c_str());
1353 LOGERR("Please unselect Enable MD5 verification to restore.\n");
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001354 return false;
1355 }
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001356 md5sum.setfn(Full_Filename);
1357 if (md5sum.verify_md5digest() != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001358 LOGERR("MD5 failed to match on '%s'.\n", Full_Filename.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001359 return false;
1360 } else
1361 return true;
1362 }
1363 return false;
1364}
1365
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001366bool 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 -08001367 string Restore_File_System;
1368
1369 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
Dees_Troy2673cec2013-04-02 20:22:16 +00001370 LOGINFO("Restore filename is: %s\n", Backup_FileName.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001371
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001372 Restore_File_System = Get_Restore_File_System(restore_folder);
1373
1374 if (Is_File_System(Restore_File_System))
1375 return Restore_Tar(restore_folder, Restore_File_System, total_restore_size, already_restored_size);
1376 else if (Is_Image(Restore_File_System)) {
1377 *already_restored_size += TWFunc::Get_File_Size(Backup_Name);
1378 if (Restore_File_System == "emmc")
1379 return Restore_DD(restore_folder, total_restore_size, already_restored_size);
1380 else if (Restore_File_System == "mtd" || Restore_File_System == "bml")
1381 return Restore_Flash_Image(restore_folder, total_restore_size, already_restored_size);
1382 }
1383
1384 LOGERR("Unknown restore method for '%s'\n", Mount_Point.c_str());
1385 return false;
1386}
1387
1388string TWPartition::Get_Restore_File_System(string restore_folder) {
1389 size_t first_period, second_period;
1390 string Restore_File_System;
1391
Gary Peck43acadf2012-11-21 21:19:01 -08001392 // Parse backup filename to extract the file system before wiping
1393 first_period = Backup_FileName.find(".");
1394 if (first_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001395 LOGERR("Unable to find file system (first period).\n");
thatd43bf2d2014-09-21 23:13:02 +02001396 return string();
Gary Peck43acadf2012-11-21 21:19:01 -08001397 }
1398 Restore_File_System = Backup_FileName.substr(first_period + 1, Backup_FileName.size() - first_period - 1);
1399 second_period = Restore_File_System.find(".");
1400 if (second_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001401 LOGERR("Unable to find file system (second period).\n");
thatd43bf2d2014-09-21 23:13:02 +02001402 return string();
Gary Peck43acadf2012-11-21 21:19:01 -08001403 }
1404 Restore_File_System.resize(second_period);
Dees_Troy2673cec2013-04-02 20:22:16 +00001405 LOGINFO("Restore file system is: '%s'.\n", Restore_File_System.c_str());
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001406 return Restore_File_System;
Dees_Troy51a0e822012-09-05 15:24:24 -04001407}
1408
1409string TWPartition::Backup_Method_By_Name() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001410 if (Backup_Method == NONE)
1411 return "none";
1412 else if (Backup_Method == FILES)
1413 return "files";
1414 else if (Backup_Method == DD)
1415 return "dd";
1416 else if (Backup_Method == FLASH_UTILS)
1417 return "flash_utils";
1418 else
1419 return "undefined";
1420 return "ERROR!";
Dees_Troy51a0e822012-09-05 15:24:24 -04001421}
1422
1423bool TWPartition::Decrypt(string Password) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001424 LOGINFO("STUB TWPartition::Decrypt, password: '%s'\n", Password.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001425 // Is this needed?
Dees_Troy51a0e822012-09-05 15:24:24 -04001426 return 1;
1427}
1428
1429bool TWPartition::Wipe_Encryption() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001430 bool Save_Data_Media = Has_Data_Media;
1431
1432 if (!UnMount(true))
1433 return false;
1434
Dees_Troy38bd7602012-09-14 13:33:53 -04001435 Has_Data_Media = false;
Dees_Troy74fb2e92013-04-15 14:35:47 +00001436 Decrypted_Block_Device = "";
1437 Is_Decrypted = false;
1438 Is_Encrypted = false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001439 Find_Actual_Block_Device();
1440 bool mtp_was_enabled = TWFunc::Toggle_MTP(false);
Gary Pecke8bc5d72012-12-21 06:45:25 -08001441 if (Wipe(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001442 Has_Data_Media = Save_Data_Media;
1443 if (Has_Data_Media && !Symlink_Mount_Point.empty()) {
1444 Recreate_Media_Folder();
1445 }
Ethan Yonker83e82572014-04-04 10:59:28 -05001446#ifndef TW_OEM_BUILD
Dees_Troy2673cec2013-04-02 20:22:16 +00001447 gui_print("You may need to reboot recovery to be able to use /data again.\n");
Ethan Yonker83e82572014-04-04 10:59:28 -05001448#endif
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001449 TWFunc::Toggle_MTP(mtp_was_enabled);
Dees_Troy38bd7602012-09-14 13:33:53 -04001450 return true;
1451 } else {
1452 Has_Data_Media = Save_Data_Media;
Dees_Troy2673cec2013-04-02 20:22:16 +00001453 LOGERR("Unable to format to remove encryption.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001454 return false;
1455 }
1456 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001457}
1458
1459void TWPartition::Check_FS_Type() {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001460 const char* type;
1461 blkid_probe pr;
Dees_Troy5bf43922012-09-07 16:07:55 -04001462
Dees_Troy68cab492012-12-12 19:29:35 +00001463 if (Fstab_File_System == "yaffs2" || Fstab_File_System == "mtd" || Fstab_File_System == "bml" || Ignore_Blkid)
1464 return; // Running blkid on some mtd devices causes a massive crash or needs to be skipped
Dees_Troy5bf43922012-09-07 16:07:55 -04001465
Dees_Troy38bd7602012-09-14 13:33:53 -04001466 Find_Actual_Block_Device();
Dees_Troy8170a922012-09-18 15:40:25 -04001467 if (!Is_Present)
1468 return;
Dees_Troy51127312012-09-08 13:08:49 -04001469
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001470 pr = blkid_new_probe_from_filename(Actual_Block_Device.c_str());
1471 if (blkid_do_fullprobe(pr)) {
1472 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00001473 LOGINFO("Can't probe device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001474 return;
Dees_Troy5bf43922012-09-07 16:07:55 -04001475 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001476
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05001477 if (blkid_probe_lookup_value(pr, "TYPE", &type, NULL) < 0) {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001478 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00001479 LOGINFO("can't find filesystem on device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001480 return;
1481 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001482
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001483 Current_File_System = type;
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001484 blkid_free_probe(pr);
Dees_Troy51a0e822012-09-05 15:24:24 -04001485}
1486
Gary Peck43acadf2012-11-21 21:19:01 -08001487bool TWPartition::Wipe_EXT23(string File_System) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001488 if (!UnMount(true))
1489 return false;
1490
Dees_Troy43d8b002012-09-17 16:00:01 -04001491 if (TWFunc::Path_Exists("/sbin/mke2fs")) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001492 string command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001493
Dees_Troy2673cec2013-04-02 20:22:16 +00001494 gui_print("Formatting %s using mke2fs...\n", Display_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001495 Find_Actual_Block_Device();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001496 command = "mke2fs -t " + File_System + " -m 0 " + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001497 LOGINFO("mke2fs command: %s\n", command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001498 if (TWFunc::Exec_Cmd(command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001499 Current_File_System = File_System;
Dees_Troye58d5262012-09-21 12:27:57 -04001500 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001501 gui_print("Done.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001502 return true;
1503 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001504 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001505 return false;
1506 }
1507 } else
1508 return Wipe_RMRF();
1509
1510 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001511}
1512
1513bool TWPartition::Wipe_EXT4() {
Ethan Yonker25f20c12014-10-14 09:04:43 -05001514 Find_Actual_Block_Device();
1515 if (!Is_Present) {
1516 LOGERR("Block device not present, cannot wipe %s.\n", Display_Name.c_str());
1517 return false;
1518 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001519 if (!UnMount(true))
1520 return false;
1521
Dees_Troyb3265ab2013-08-30 02:59:14 +00001522#if defined(HAVE_SELINUX) && defined(USE_EXT4)
Ethan Yonkerf27497f2014-02-09 11:48:33 -06001523 int ret;
1524 char *secontext = NULL;
1525
Dees_Troya95f55c2013-08-17 13:14:43 +00001526 gui_print("Formatting %s using make_ext4fs function.\n", Display_Name.c_str());
Ethan Yonkerf27497f2014-02-09 11:48:33 -06001527
Dees Troy99c8dbf2014-03-10 16:53:58 +00001528 if (!selinux_handle || selabel_lookup(selinux_handle, &secontext, Mount_Point.c_str(), S_IFDIR) < 0) {
Ethan Yonkerf27497f2014-02-09 11:48:33 -06001529 LOGINFO("Cannot lookup security context for '%s'\n", Mount_Point.c_str());
1530 ret = make_ext4fs(Actual_Block_Device.c_str(), Length, Mount_Point.c_str(), NULL);
1531 } else {
1532 ret = make_ext4fs(Actual_Block_Device.c_str(), Length, Mount_Point.c_str(), selinux_handle);
1533 }
1534 if (ret != 0) {
Dees_Troya95f55c2013-08-17 13:14:43 +00001535 LOGERR("Unable to wipe '%s' using function call.\n", Mount_Point.c_str());
1536 return false;
1537 } else {
bigbiff bigbiffc49d7062013-10-11 20:28:00 -04001538 string sedir = Mount_Point + "/lost+found";
1539 PartitionManager.Mount_By_Path(sedir.c_str(), true);
1540 rmdir(sedir.c_str());
1541 mkdir(sedir.c_str(), S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP);
Dees_Troya95f55c2013-08-17 13:14:43 +00001542 return true;
1543 }
1544#else
Dees_Troy43d8b002012-09-17 16:00:01 -04001545 if (TWFunc::Path_Exists("/sbin/make_ext4fs")) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001546 string Command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001547
Dees_Troy2673cec2013-04-02 20:22:16 +00001548 gui_print("Formatting %s using make_ext4fs...\n", Display_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001549 Find_Actual_Block_Device();
1550 Command = "make_ext4fs";
1551 if (!Is_Decrypted && Length != 0) {
1552 // Only use length if we're not decrypted
1553 char len[32];
1554 sprintf(len, "%i", Length);
1555 Command += " -l ";
1556 Command += len;
1557 }
Dees_Troy5295d582013-09-06 15:51:08 +00001558 if (TWFunc::Path_Exists("/file_contexts")) {
1559 Command += " -S /file_contexts";
1560 }
1561 Command += " -a " + Mount_Point + " " + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001562 LOGINFO("make_ext4fs command: %s\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001563 if (TWFunc::Exec_Cmd(Command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001564 Current_File_System = "ext4";
Dees_Troye58d5262012-09-21 12:27:57 -04001565 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001566 gui_print("Done.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001567 return true;
1568 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001569 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001570 return false;
1571 }
1572 } else
Gary Peck43acadf2012-11-21 21:19:01 -08001573 return Wipe_EXT23("ext4");
Dees_Troya95f55c2013-08-17 13:14:43 +00001574#endif
Dees_Troy38bd7602012-09-14 13:33:53 -04001575 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001576}
1577
1578bool TWPartition::Wipe_FAT() {
Vojtech Bocek05534202013-09-11 08:11:56 +02001579 string command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001580
Dees_Troy43d8b002012-09-17 16:00:01 -04001581 if (TWFunc::Path_Exists("/sbin/mkdosfs")) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001582 if (!UnMount(true))
1583 return false;
1584
Dees_Troy2673cec2013-04-02 20:22:16 +00001585 gui_print("Formatting %s using mkdosfs...\n", Display_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001586 Find_Actual_Block_Device();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001587 command = "mkdosfs " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02001588 if (TWFunc::Exec_Cmd(command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001589 Current_File_System = "vfat";
Dees_Troye58d5262012-09-21 12:27:57 -04001590 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001591 gui_print("Done.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001592 return true;
1593 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001594 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001595 return false;
1596 }
1597 return true;
1598 }
1599 else
1600 return Wipe_RMRF();
1601
1602 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001603}
1604
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001605bool TWPartition::Wipe_EXFAT() {
Vojtech Bocek05534202013-09-11 08:11:56 +02001606 string command;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001607
1608 if (TWFunc::Path_Exists("/sbin/mkexfatfs")) {
1609 if (!UnMount(true))
1610 return false;
1611
Dees_Troy2673cec2013-04-02 20:22:16 +00001612 gui_print("Formatting %s using mkexfatfs...\n", Display_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001613 Find_Actual_Block_Device();
1614 command = "mkexfatfs " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02001615 if (TWFunc::Exec_Cmd(command) == 0) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001616 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001617 gui_print("Done.\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001618 return true;
1619 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001620 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001621 return false;
1622 }
1623 return true;
1624 }
1625 return false;
1626}
1627
Dees_Troy38bd7602012-09-14 13:33:53 -04001628bool TWPartition::Wipe_MTD() {
1629 if (!UnMount(true))
1630 return false;
1631
Dees_Troy2673cec2013-04-02 20:22:16 +00001632 gui_print("MTD Formatting \"%s\"\n", MTD_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001633
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001634 mtd_scan_partitions();
1635 const MtdPartition* mtd = mtd_find_partition_by_name(MTD_Name.c_str());
1636 if (mtd == NULL) {
1637 LOGERR("No mtd partition named '%s'", MTD_Name.c_str());
1638 return false;
1639 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001640
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001641 MtdWriteContext* ctx = mtd_write_partition(mtd);
1642 if (ctx == NULL) {
1643 LOGERR("Can't write '%s', failed to format.", MTD_Name.c_str());
1644 return false;
1645 }
1646 if (mtd_erase_blocks(ctx, -1) == -1) {
1647 mtd_write_close(ctx);
1648 LOGERR("Failed to format '%s'", MTD_Name.c_str());
1649 return false;
1650 }
1651 if (mtd_write_close(ctx) != 0) {
1652 LOGERR("Failed to close '%s'", MTD_Name.c_str());
1653 return false;
1654 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001655 Current_File_System = "yaffs2";
Dees_Troye58d5262012-09-21 12:27:57 -04001656 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001657 gui_print("Done.\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001658 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001659}
1660
1661bool TWPartition::Wipe_RMRF() {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001662 if (Is_Storage)
1663 TWFunc::Toggle_MTP(false);
Dees_Troy38bd7602012-09-14 13:33:53 -04001664 if (!Mount(true))
1665 return false;
1666
Dees_Troy2673cec2013-04-02 20:22:16 +00001667 gui_print("Removing all files under '%s'\n", Mount_Point.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001668 TWFunc::removeDir(Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04001669 Recreate_AndSec_Folder();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001670 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001671}
1672
Dees_Troye5017042013-08-29 16:38:55 +00001673bool TWPartition::Wipe_F2FS() {
Vojtech Bocek05534202013-09-11 08:11:56 +02001674 string command;
Dees_Troye5017042013-08-29 16:38:55 +00001675
1676 if (TWFunc::Path_Exists("/sbin/mkfs.f2fs")) {
1677 if (!UnMount(true))
1678 return false;
1679
1680 gui_print("Formatting %s using mkfs.f2fs...\n", Display_Name.c_str());
1681 Find_Actual_Block_Device();
1682 command = "mkfs.f2fs " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02001683 if (TWFunc::Exec_Cmd(command) == 0) {
Dees_Troye5017042013-08-29 16:38:55 +00001684 Recreate_AndSec_Folder();
1685 gui_print("Done.\n");
1686 return true;
1687 } else {
1688 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
1689 return false;
1690 }
1691 return true;
1692 } else {
1693 gui_print("mkfs.f2fs binary not found, using rm -rf to wipe.\n");
1694 return Wipe_RMRF();
1695 }
1696 return false;
1697}
1698
Dees_Troy51a0e822012-09-05 15:24:24 -04001699bool TWPartition::Wipe_Data_Without_Wiping_Media() {
Ethan Yonker83e82572014-04-04 10:59:28 -05001700#ifdef TW_OEM_BUILD
1701 // In an OEM Build we want to do a full format
1702 return Wipe_Encryption();
1703#else
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001704 string dir;
bigbiff bigbiff6b600f92014-01-05 18:13:43 -05001705 #ifdef HAVE_SELINUX
1706 fixPermissions perms;
1707 #endif
Dees_Troy38bd7602012-09-14 13:33:53 -04001708
1709 // This handles wiping data on devices with "sdcard" in /data/media
1710 if (!Mount(true))
1711 return false;
1712
Dees_Troy2673cec2013-04-02 20:22:16 +00001713 gui_print("Wiping data without wiping /data/media ...\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001714
1715 DIR* d;
1716 d = opendir("/data");
Dees_Troy16b74352012-11-14 22:27:31 +00001717 if (d != NULL) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001718 struct dirent* de;
1719 while ((de = readdir(d)) != NULL) {
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05001720 if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) continue;
Dees_Troy16b74352012-11-14 22:27:31 +00001721 // The media folder is the "internal sdcard"
1722 // The .layout_version file is responsible for determining whether 4.2 decides up upgrade
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001723 // the media folder for multi-user.
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05001724 //TODO: convert this to use twrpDU.cpp
Dees_Troy16b74352012-11-14 22:27:31 +00001725 if (strcmp(de->d_name, "media") == 0 || strcmp(de->d_name, ".layout_version") == 0) continue;
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05001726
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001727 dir = "/data/";
1728 dir.append(de->d_name);
Dees_Troyce675462013-01-09 19:48:21 +00001729 if (de->d_type == DT_DIR) {
1730 TWFunc::removeDir(dir, false);
bigbiff bigbiff98f1f902013-01-19 18:46:13 -05001731 } 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 +00001732 if (!unlink(dir.c_str()))
Dees_Troy2673cec2013-04-02 20:22:16 +00001733 LOGINFO("Unable to unlink '%s'\n", dir.c_str());
Dees_Troyce675462013-01-09 19:48:21 +00001734 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001735 }
1736 closedir(d);
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05001737
Dees_Troy2673cec2013-04-02 20:22:16 +00001738 gui_print("Done.\n");
Dees_Troy16b74352012-11-14 22:27:31 +00001739 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -04001740 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001741 gui_print("Dirent failed to open /data, error!\n");
Dees_Troy16b74352012-11-14 22:27:31 +00001742 return false;
Ethan Yonker83e82572014-04-04 10:59:28 -05001743#endif // ifdef TW_OEM_BUILD
Dees_Troy51a0e822012-09-05 15:24:24 -04001744}
1745
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001746bool 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 -04001747 char back_name[255], split_index[5];
1748 string Full_FileName, Split_FileName, Tar_Args, Command;
Dees_Troy83bd4832013-05-04 12:39:56 +00001749 int use_compression, use_encryption = 0, index, backup_count;
Dees_Troy4a2a1262012-09-18 09:33:47 -04001750 struct stat st;
Dees_Troy7c2dec82012-09-26 09:49:14 -04001751 unsigned long long total_bsize = 0, file_size;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001752 twrpTar tar;
1753 vector <string> files;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001754
Dees_Troy43d8b002012-09-17 16:00:01 -04001755 if (!Mount(true))
1756 return false;
1757
Dees_Troya13d74f2013-03-24 08:54:55 -05001758 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Backup_Display_Name, "Backing Up");
Dees_Troy2673cec2013-04-02 20:22:16 +00001759 gui_print("Backing up %s...\n", Backup_Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001760
1761 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
Dees_Troy83bd4832013-05-04 12:39:56 +00001762 tar.use_compression = use_compression;
Matt Mowerbb81e5d2014-03-20 18:05:41 -05001763
Dees_Troy83bd4832013-05-04 12:39:56 +00001764#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
1765 DataManager::GetValue("tw_encrypt_backup", use_encryption);
1766 if (use_encryption && Can_Encrypt_Backup) {
1767 tar.use_encryption = use_encryption;
1768 if (Use_Userdata_Encryption)
1769 tar.userdata_encryption = use_encryption;
Ethan Yonker87af5632014-02-10 11:56:35 -06001770 string Password;
1771 DataManager::GetValue("tw_backup_password", Password);
1772 tar.setpassword(Password);
Dees_Troy83bd4832013-05-04 12:39:56 +00001773 } else {
1774 use_encryption = false;
1775 }
1776#endif
Dees_Troy43d8b002012-09-17 16:00:01 -04001777
1778 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1779 Backup_FileName = back_name;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001780 Full_FileName = backup_folder + "/" + Backup_FileName;
Dees_Troy83bd4832013-05-04 12:39:56 +00001781 tar.has_data_media = Has_Data_Media;
Dees Troye0a433a2013-12-02 04:10:37 +00001782 Full_FileName = backup_folder + "/" + Backup_FileName;
1783 tar.setdir(Backup_Path);
1784 tar.setfn(Full_FileName);
1785 tar.setsize(Backup_Size);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001786 tar.partition_name = Backup_Name;
1787 tar.backup_folder = backup_folder;
1788 if (tar.createTarFork(overall_size, other_backups_size) != 0)
Dees Troye0a433a2013-12-02 04:10:37 +00001789 return false;
Dees_Troy43d8b002012-09-17 16:00:01 -04001790 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001791}
1792
1793bool TWPartition::Backup_DD(string backup_folder) {
igoriok87e3d932013-01-31 21:03:53 +02001794 char back_name[255], backup_size[32];
Vojtech Bocek05534202013-09-11 08:11:56 +02001795 string Full_FileName, Command, DD_BS;
Dees_Troy43d8b002012-09-17 16:00:01 -04001796 int use_compression;
1797
igoriok87e3d932013-01-31 21:03:53 +02001798 sprintf(backup_size, "%llu", Backup_Size);
1799 DD_BS = backup_size;
1800
Dees_Troyb46a6842012-09-25 11:06:46 -04001801 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
Dees_Troy2673cec2013-04-02 20:22:16 +00001802 gui_print("Backing up %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001803
1804 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1805 Backup_FileName = back_name;
1806
1807 Full_FileName = backup_folder + "/" + Backup_FileName;
1808
igoriok87e3d932013-01-31 21:03:53 +02001809 Command = "dd if=" + Actual_Block_Device + " of='" + Full_FileName + "'" + " bs=" + DD_BS + "c count=1";
Dees_Troy2673cec2013-04-02 20:22:16 +00001810 LOGINFO("Backup command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001811 TWFunc::Exec_Cmd(Command);
Dees_Troyc154ac22012-10-12 15:36:47 -04001812 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001813 LOGERR("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001814 return false;
1815 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001816 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001817}
1818
1819bool TWPartition::Backup_Dump_Image(string backup_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001820 char back_name[255];
Vojtech Bocek05534202013-09-11 08:11:56 +02001821 string Full_FileName, Command;
Dees_Troy43d8b002012-09-17 16:00:01 -04001822 int use_compression;
1823
Dees_Troyb46a6842012-09-25 11:06:46 -04001824 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
Dees_Troy2673cec2013-04-02 20:22:16 +00001825 gui_print("Backing up %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001826
1827 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1828 Backup_FileName = back_name;
1829
1830 Full_FileName = backup_folder + "/" + Backup_FileName;
1831
1832 Command = "dump_image " + MTD_Name + " '" + Full_FileName + "'";
Dees_Troy2673cec2013-04-02 20:22:16 +00001833 LOGINFO("Backup command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001834 TWFunc::Exec_Cmd(Command);
Dees_Troy7c2dec82012-09-26 09:49:14 -04001835 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
1836 // 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 +00001837 LOGERR("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001838 return false;
1839 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001840 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001841}
1842
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001843unsigned long long TWPartition::Get_Restore_Size(string restore_folder) {
1844 InfoManager restore_info(restore_folder + "/" + Backup_Name + ".info");
1845 if (restore_info.LoadValues() == 0) {
1846 if (restore_info.GetValue("backup_size", Restore_Size) == 0) {
1847 LOGINFO("Read info file, restore size is %llu\n", Restore_Size);
1848 return Restore_Size;
1849 }
1850 }
1851 string Full_FileName, Restore_File_System = Get_Restore_File_System(restore_folder);
1852
1853 Full_FileName = restore_folder + "/" + Backup_FileName;
1854
1855 if (Is_Image(Restore_File_System)) {
1856 Restore_Size = TWFunc::Get_File_Size(Full_FileName);
1857 return Restore_Size;
1858 }
1859
1860 twrpTar tar;
1861 tar.setdir(Backup_Path);
1862 tar.setfn(Full_FileName);
1863 tar.backup_name = Backup_Name;
1864#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
1865 string Password;
1866 DataManager::GetValue("tw_restore_password", Password);
1867 if (!Password.empty())
1868 tar.setpassword(Password);
1869#endif
1870 tar.partition_name = Backup_Name;
1871 tar.backup_folder = restore_folder;
1872 Restore_Size = tar.get_size();
1873 return Restore_Size;
1874}
1875
1876bool 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 -08001877 string Full_FileName, Command;
Dees_Troy4a2a1262012-09-18 09:33:47 -04001878 int index = 0;
1879 char split_index[5];
Dees Troy4159aed2014-02-28 17:24:43 +00001880 bool ret = false;
Dees_Troy43d8b002012-09-17 16:00:01 -04001881
Dees_Troye58d5262012-09-21 12:27:57 -04001882 if (Has_Android_Secure) {
Dees_Troye58d5262012-09-21 12:27:57 -04001883 if (!Wipe_AndSec())
1884 return false;
Gary Peck43acadf2012-11-21 21:19:01 -08001885 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001886 gui_print("Wiping %s...\n", Display_Name.c_str());
Ethan Yonker5eac2222014-06-11 12:22:55 -05001887 if (Has_Data_Media && Mount_Point == "/data" && Restore_File_System != Current_File_System) {
1888 gui_print("WARNING: This /data backup was made with %s file system!\n", Restore_File_System.c_str());
1889 gui_print("The backup may not boot unless you change back to %s.\n", Restore_File_System.c_str());
1890 if (!Wipe_Data_Without_Wiping_Media())
1891 return false;
1892 } else {
1893 if (!Wipe(Restore_File_System))
1894 return false;
1895 }
Dees_Troye58d5262012-09-21 12:27:57 -04001896 }
Dees_Troya13d74f2013-03-24 08:54:55 -05001897 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Backup_Display_Name, "Restoring");
Dees_Troy2673cec2013-04-02 20:22:16 +00001898 gui_print("Restoring %s...\n", Backup_Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001899
1900 if (!Mount(true))
1901 return false;
1902
Dees_Troy4a2a1262012-09-18 09:33:47 -04001903 Full_FileName = restore_folder + "/" + Backup_FileName;
Ethan Yonker87af5632014-02-10 11:56:35 -06001904 twrpTar tar;
1905 tar.setdir(Backup_Path);
1906 tar.setfn(Full_FileName);
1907 tar.backup_name = Backup_Name;
1908#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
1909 string Password;
1910 DataManager::GetValue("tw_restore_password", Password);
1911 if (!Password.empty())
1912 tar.setpassword(Password);
1913#endif
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001914 if (tar.extractTarFork(total_restore_size, already_restored_size) != 0)
Dees Troy4159aed2014-02-28 17:24:43 +00001915 ret = false;
1916 else
1917 ret = true;
1918#ifdef HAVE_CAPABILITIES
1919 // Restore capabilities to the run-as binary
1920 if (Mount_Point == "/system" && Mount(true) && TWFunc::Path_Exists("/system/bin/run-as")) {
1921 struct vfs_cap_data cap_data;
1922 uint64_t capabilities = (1 << CAP_SETUID) | (1 << CAP_SETGID);
1923
1924 memset(&cap_data, 0, sizeof(cap_data));
1925 cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE;
1926 cap_data.data[0].permitted = (uint32_t) (capabilities & 0xffffffff);
1927 cap_data.data[0].inheritable = 0;
1928 cap_data.data[1].permitted = (uint32_t) (capabilities >> 32);
1929 cap_data.data[1].inheritable = 0;
1930 if (setxattr("/system/bin/run-as", XATTR_NAME_CAPS, &cap_data, sizeof(cap_data), 0) < 0) {
1931 LOGINFO("Failed to reset capabilities of /system/bin/run-as binary.\n");
1932 } else {
1933 LOGINFO("Reset capabilities of /system/bin/run-as binary successful.\n");
1934 }
1935 }
1936#endif
1937 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04001938}
1939
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001940bool 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 +02001941 string Full_FileName, Command;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001942 double display_percent, progress_percent;
1943 char size_progress[1024];
Dees_Troy43d8b002012-09-17 16:00:01 -04001944
Dees_Troyda8b55a2012-12-12 19:18:30 +00001945 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
Dees_Troy43d8b002012-09-17 16:00:01 -04001946 Full_FileName = restore_folder + "/" + Backup_FileName;
Gary Peck15e623d2012-11-21 21:07:58 -08001947
1948 if (!Find_Partition_Size()) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001949 LOGERR("Unable to find partition size for '%s'\n", Mount_Point.c_str());
Gary Peck15e623d2012-11-21 21:07:58 -08001950 return false;
1951 }
1952 unsigned long long backup_size = TWFunc::Get_File_Size(Full_FileName);
1953 if (backup_size > Size) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001954 LOGERR("Size (%iMB) of backup '%s' is larger than target device '%s' (%iMB)\n",
Gary Peck15e623d2012-11-21 21:07:58 -08001955 (int)(backup_size / 1048576LLU), Full_FileName.c_str(),
1956 Actual_Block_Device.c_str(), (int)(Size / 1048576LLU));
1957 return false;
1958 }
1959
Dees_Troy2673cec2013-04-02 20:22:16 +00001960 gui_print("Restoring %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001961 Command = "dd bs=4096 if='" + Full_FileName + "' of=" + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001962 LOGINFO("Restore command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001963 TWFunc::Exec_Cmd(Command);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001964 display_percent = (double)(Restore_Size + *already_restored_size) / (double)(*total_restore_size) * 100;
1965 sprintf(size_progress, "%lluMB of %lluMB, %i%%", (Restore_Size + *already_restored_size) / 1048576, *total_restore_size / 1048576, (int)(display_percent));
1966 DataManager::SetValue("tw_size_progress", size_progress);
1967 progress_percent = (display_percent / 100);
1968 DataManager::SetProgress((float)(progress_percent));
1969 *already_restored_size += Restore_Size;
Dees_Troy43d8b002012-09-17 16:00:01 -04001970 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001971}
1972
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001973bool 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 +02001974 string Full_FileName, Command;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001975 double display_percent, progress_percent;
1976 char size_progress[1024];
Dees_Troy43d8b002012-09-17 16:00:01 -04001977
Dees_Troy2673cec2013-04-02 20:22:16 +00001978 gui_print("Restoring %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001979 Full_FileName = restore_folder + "/" + Backup_FileName;
1980 // Sometimes flash image doesn't like to flash due to the first 2KB matching, so we erase first to ensure that it flashes
1981 Command = "erase_image " + MTD_Name;
Dees_Troy2673cec2013-04-02 20:22:16 +00001982 LOGINFO("Erase command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001983 TWFunc::Exec_Cmd(Command);
Dees_Troy43d8b002012-09-17 16:00:01 -04001984 Command = "flash_image " + MTD_Name + " '" + Full_FileName + "'";
Dees_Troy2673cec2013-04-02 20:22:16 +00001985 LOGINFO("Restore command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001986 TWFunc::Exec_Cmd(Command);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001987 display_percent = (double)(Restore_Size + *already_restored_size) / (double)(*total_restore_size) * 100;
1988 sprintf(size_progress, "%lluMB of %lluMB, %i%%", (Restore_Size + *already_restored_size) / 1048576, *total_restore_size / 1048576, (int)(display_percent));
1989 DataManager::SetValue("tw_size_progress", size_progress);
1990 progress_percent = (display_percent / 100);
1991 DataManager::SetProgress((float)(progress_percent));
1992 *already_restored_size += Restore_Size;
Dees_Troy43d8b002012-09-17 16:00:01 -04001993 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001994}
Dees_Troy5bf43922012-09-07 16:07:55 -04001995
1996bool TWPartition::Update_Size(bool Display_Error) {
Dees_Troy0550cfb2012-10-13 11:56:13 -04001997 bool ret = false, Was_Already_Mounted = false;
Dees_Troy51127312012-09-08 13:08:49 -04001998
Dees_Troyab10ee22012-09-21 14:27:30 -04001999 if (!Can_Be_Mounted && !Is_Encrypted)
Dees_Troy5bf43922012-09-07 16:07:55 -04002000 return false;
2001
Dees_Troy0550cfb2012-10-13 11:56:13 -04002002 Was_Already_Mounted = Is_Mounted();
Dees_Troy38bd7602012-09-14 13:33:53 -04002003 if (Removable || Is_Encrypted) {
2004 if (!Mount(false))
2005 return true;
2006 } else if (!Mount(Display_Error))
Dees_Troy5bf43922012-09-07 16:07:55 -04002007 return false;
Dees_Troy51127312012-09-08 13:08:49 -04002008
2009 ret = Get_Size_Via_statfs(Display_Error);
Dees_Troy0550cfb2012-10-13 11:56:13 -04002010 if (!ret || Size == 0) {
2011 if (!Get_Size_Via_df(Display_Error)) {
2012 if (!Was_Already_Mounted)
2013 UnMount(false);
Dees_Troy51127312012-09-08 13:08:49 -04002014 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04002015 }
2016 }
Dees_Troy51127312012-09-08 13:08:49 -04002017
Dees_Troy5bf43922012-09-07 16:07:55 -04002018 if (Has_Data_Media) {
2019 if (Mount(Display_Error)) {
Dees_Troy51127312012-09-08 13:08:49 -04002020 unsigned long long data_media_used, actual_data;
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002021 Used = du.Get_Folder_Size("/data");
2022 Backup_Size = Used;
2023 int bak = (int)(Used / 1048576LLU);
Dees_Troy51127312012-09-08 13:08:49 -04002024 int fre = (int)(Free / 1048576LLU);
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002025 LOGINFO("Data backup size is %iMB, free: %iMB.\n", bak, fre);
Dees_Troy0550cfb2012-10-13 11:56:13 -04002026 } else {
2027 if (!Was_Already_Mounted)
2028 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04002029 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04002030 }
Dees_Troye58d5262012-09-21 12:27:57 -04002031 } else if (Has_Android_Secure) {
2032 if (Mount(Display_Error))
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002033 Backup_Size = du.Get_Folder_Size(Backup_Path);
Dees_Troy0550cfb2012-10-13 11:56:13 -04002034 else {
2035 if (!Was_Already_Mounted)
2036 UnMount(false);
Dees_Troye58d5262012-09-21 12:27:57 -04002037 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04002038 }
Dees_Troy5bf43922012-09-07 16:07:55 -04002039 }
Dees_Troy0550cfb2012-10-13 11:56:13 -04002040 if (!Was_Already_Mounted)
2041 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04002042 return true;
Dees_Troy51127312012-09-08 13:08:49 -04002043}
Dees_Troy38bd7602012-09-14 13:33:53 -04002044
2045void TWPartition::Find_Actual_Block_Device(void) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002046 if (Is_Decrypted && !Decrypted_Block_Device.empty()) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002047 Actual_Block_Device = Decrypted_Block_Device;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002048 if (TWFunc::Path_Exists(Decrypted_Block_Device))
Dees_Troy38bd7602012-09-14 13:33:53 -04002049 Is_Present = true;
Dees_Troy43d8b002012-09-17 16:00:01 -04002050 } else if (TWFunc::Path_Exists(Primary_Block_Device)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04002051 Is_Present = true;
2052 Actual_Block_Device = Primary_Block_Device;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002053 return;
2054 }
2055 if (Is_Decrypted) {
Dees_Troy43d8b002012-09-17 16:00:01 -04002056 } else if (!Alternate_Block_Device.empty() && TWFunc::Path_Exists(Alternate_Block_Device)) {
Dees_Troy3f04d032012-10-07 18:20:09 -04002057 Actual_Block_Device = Alternate_Block_Device;
Dees_Troy38bd7602012-09-14 13:33:53 -04002058 Is_Present = true;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002059 } else {
Dees_Troy38bd7602012-09-14 13:33:53 -04002060 Is_Present = false;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002061 }
Dees_Troy38bd7602012-09-14 13:33:53 -04002062}
2063
2064void TWPartition::Recreate_Media_Folder(void) {
2065 string Command;
2066
bigbiff bigbiff6b600f92014-01-05 18:13:43 -05002067 #ifdef HAVE_SELINUX
2068 fixPermissions perms;
2069 #endif
2070
Dees_Troy38bd7602012-09-14 13:33:53 -04002071 if (!Mount(true)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00002072 LOGERR("Unable to recreate /data/media folder.\n");
Dees_Troyb46a6842012-09-25 11:06:46 -04002073 } else if (!TWFunc::Path_Exists("/data/media")) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002074 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Dees_Troy2673cec2013-04-02 20:22:16 +00002075 LOGINFO("Recreating /data/media folder.\n");
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05002076 mkdir("/data/media", S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
Ethan Yonker5ef301e2014-10-14 10:04:44 -05002077#ifdef HAVE_SELINUX
bigbiff bigbiff6b600f92014-01-05 18:13:43 -05002078 perms.fixDataInternalContexts();
Ethan Yonker5ef301e2014-10-14 10:04:44 -05002079#endif
Ethan Yonker5eac2222014-06-11 12:22:55 -05002080 // Toggle mount to ensure that "internal sdcard" gets mounted
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002081 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Ethan Yonker5eac2222014-06-11 12:22:55 -05002082 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Dees_Troy38bd7602012-09-14 13:33:53 -04002083 }
Dees_Troy43d8b002012-09-17 16:00:01 -04002084}
Dees_Troye58d5262012-09-21 12:27:57 -04002085
2086void TWPartition::Recreate_AndSec_Folder(void) {
Dees_Troye58d5262012-09-21 12:27:57 -04002087 if (!Has_Android_Secure)
2088 return;
Dees_Troy2673cec2013-04-02 20:22:16 +00002089 LOGINFO("Creating %s: %s\n", Backup_Display_Name.c_str(), Symlink_Path.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -04002090 if (!Mount(true)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00002091 LOGERR("Unable to recreate %s folder.\n", Backup_Name.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -04002092 } else if (!TWFunc::Path_Exists(Symlink_Path)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00002093 LOGINFO("Recreating %s folder.\n", Backup_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002094 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05002095 mkdir(Symlink_Path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
bigbiff bigbiff9c754052013-01-09 09:09:08 -05002096 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04002097 }
2098}