blob: e92fccfd2be3aefa34fe577d8bf68dc2718d3518 [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"
Dees_Troy5bf43922012-09-07 16:07:55 -040044extern "C" {
Dees_Troy38bd7602012-09-14 13:33:53 -040045 #include "mtdutils/mtdutils.h"
46 #include "mtdutils/mounts.h"
Dees_Troy85f44ed2013-01-09 18:42:36 +000047#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
48 #include "crypto/libcrypt_samsung/include/libcrypt_samsung.h"
49#endif
Dees_Troya95f55c2013-08-17 13:14:43 +000050#ifdef USE_EXT4
51 #include "make_ext4fs.h"
52#endif
Ethan Yonker71413f42014-02-26 13:36:08 -060053
54#ifdef TW_INCLUDE_CRYPTO
55 #ifdef TW_INCLUDE_JB_CRYPTO
56 #include "crypto/jb/cryptfs.h"
57 #else
58 #include "crypto/ics/cryptfs.h"
59 #endif
60#endif
Dees_Troy5bf43922012-09-07 16:07:55 -040061}
bigbiff bigbiffc49d7062013-10-11 20:28:00 -040062#ifdef HAVE_SELINUX
63#include "selinux/selinux.h"
Ethan Yonkerf27497f2014-02-09 11:48:33 -060064#include <selinux/label.h>
bigbiff bigbiffc49d7062013-10-11 20:28:00 -040065#endif
Dees Troy4159aed2014-02-28 17:24:43 +000066#ifdef HAVE_CAPABILITIES
67#include <sys/capability.h>
68#include <sys/xattr.h>
69#include <linux/xattr.h>
70#endif
Dees_Troy51a0e822012-09-05 15:24:24 -040071
bigbiff bigbiff9c754052013-01-09 09:09:08 -050072using namespace std;
73
Dees_Troya95f55c2013-08-17 13:14:43 +000074extern struct selabel_handle *selinux_handle;
75
Hashcode62bd9e02013-11-19 21:59:42 -080076struct flag_list {
77 const char *name;
78 unsigned flag;
79};
80
81static struct flag_list mount_flags[] = {
82 { "noatime", MS_NOATIME },
83 { "noexec", MS_NOEXEC },
84 { "nosuid", MS_NOSUID },
85 { "nodev", MS_NODEV },
86 { "nodiratime", MS_NODIRATIME },
87 { "ro", MS_RDONLY },
88 { "rw", 0 },
89 { "remount", MS_REMOUNT },
90 { "bind", MS_BIND },
91 { "rec", MS_REC },
Dees Troyc4bc30e2014-02-03 15:04:19 +000092#ifdef MS_UNBINDABLE
Hashcode62bd9e02013-11-19 21:59:42 -080093 { "unbindable", MS_UNBINDABLE },
Dees Troyc4bc30e2014-02-03 15:04:19 +000094#endif
95#ifdef MS_PRIVATE
Hashcode62bd9e02013-11-19 21:59:42 -080096 { "private", MS_PRIVATE },
Dees Troyc4bc30e2014-02-03 15:04:19 +000097#endif
98#ifdef MS_SLAVE
Hashcode62bd9e02013-11-19 21:59:42 -080099 { "slave", MS_SLAVE },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000100#endif
101#ifdef MS_SHARED
Hashcode62bd9e02013-11-19 21:59:42 -0800102 { "shared", MS_SHARED },
Dees Troyc4bc30e2014-02-03 15:04:19 +0000103#endif
Hashcode62bd9e02013-11-19 21:59:42 -0800104 { "sync", MS_SYNCHRONOUS },
105 { "defaults", 0 },
106 { 0, 0 },
107};
108
Dees_Troy51a0e822012-09-05 15:24:24 -0400109TWPartition::TWPartition(void) {
110 Can_Be_Mounted = false;
111 Can_Be_Wiped = false;
Dees_Troya13d74f2013-03-24 08:54:55 -0500112 Can_Be_Backed_Up = false;
Vojtech Bocek1dc30982013-08-30 21:49:30 +0200113 Use_Rm_Rf = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400114 Wipe_During_Factory_Reset = false;
115 Wipe_Available_in_GUI = false;
116 Is_SubPartition = false;
Dees_Troy2691f9d2012-09-24 11:15:49 -0400117 Has_SubPartition = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400118 SubPartition_Of = "";
119 Symlink_Path = "";
120 Symlink_Mount_Point = "";
121 Mount_Point = "";
Dees_Troye58d5262012-09-21 12:27:57 -0400122 Backup_Path = "";
Dees_Troy38bd7602012-09-14 13:33:53 -0400123 Actual_Block_Device = "";
124 Primary_Block_Device = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400125 Alternate_Block_Device = "";
126 Removable = false;
127 Is_Present = false;
128 Length = 0;
129 Size = 0;
130 Used = 0;
131 Free = 0;
132 Backup_Size = 0;
133 Can_Be_Encrypted = false;
134 Is_Encrypted = false;
135 Is_Decrypted = false;
136 Decrypted_Block_Device = "";
137 Display_Name = "";
Dees_Troya13d74f2013-03-24 08:54:55 -0500138 Backup_Display_Name = "";
139 Storage_Name = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400140 Backup_Name = "";
Dees_Troy63c8df72012-09-10 14:02:05 -0400141 Backup_FileName = "";
Dees_Troy38bd7602012-09-14 13:33:53 -0400142 MTD_Name = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400143 Backup_Method = NONE;
Dees_Troy83bd4832013-05-04 12:39:56 +0000144 Can_Encrypt_Backup = false;
145 Use_Userdata_Encryption = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400146 Has_Data_Media = false;
Dees_Troye58d5262012-09-21 12:27:57 -0400147 Has_Android_Secure = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400148 Is_Storage = false;
Dees_Troya13d74f2013-03-24 08:54:55 -0500149 Is_Settings_Storage = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400150 Storage_Path = "";
151 Current_File_System = "";
152 Fstab_File_System = "";
Hashcode62bd9e02013-11-19 21:59:42 -0800153 Mount_Flags = 0;
154 Mount_Options = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400155 Format_Block_Size = 0;
Dees_Troy68cab492012-12-12 19:29:35 +0000156 Ignore_Blkid = false;
Dees_Troy16c2b312013-01-15 16:51:18 +0000157 Retain_Layout_Version = false;
Dees_Troy85f44ed2013-01-09 18:42:36 +0000158#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
159 EcryptFS_Password = "";
160#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400161}
162
163TWPartition::~TWPartition(void) {
164 // Do nothing
165}
166
Dees_Troy5bf43922012-09-07 16:07:55 -0400167bool TWPartition::Process_Fstab_Line(string Line, bool Display_Error) {
168 char full_line[MAX_FSTAB_LINE_LENGTH], item[MAX_FSTAB_LINE_LENGTH];
169 int line_len = Line.size(), index = 0, item_index = 0;
170 char* ptr;
Dees_Troy51127312012-09-08 13:08:49 -0400171 string Flags;
Dees_Troy5bf43922012-09-07 16:07:55 -0400172 strncpy(full_line, Line.c_str(), line_len);
Dees_Troya13d74f2013-03-24 08:54:55 -0500173 bool skip = false;
Dees_Troy5bf43922012-09-07 16:07:55 -0400174
Dees_Troy51127312012-09-08 13:08:49 -0400175 for (index = 0; index < line_len; index++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500176 if (full_line[index] == 34)
177 skip = !skip;
178 if (!skip && full_line[index] <= 32)
Dees_Troy5bf43922012-09-07 16:07:55 -0400179 full_line[index] = '\0';
Dees_Troy5bf43922012-09-07 16:07:55 -0400180 }
Dees_Troy7c2dec82012-09-26 09:49:14 -0400181 Mount_Point = full_line;
Dees_Troy2673cec2013-04-02 20:22:16 +0000182 LOGINFO("Processing '%s'\n", Mount_Point.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -0400183 Backup_Path = Mount_Point;
Dees_Troya13d74f2013-03-24 08:54:55 -0500184 Storage_Path = Mount_Point;
Dees_Troy70737fa2013-04-08 13:19:20 +0000185 Display_Name = full_line + 1;
186 Backup_Display_Name = Display_Name;
187 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400188 index = Mount_Point.size();
189 while (index < line_len) {
190 while (index < line_len && full_line[index] == '\0')
191 index++;
192 if (index >= line_len)
193 continue;
194 ptr = full_line + index;
195 if (item_index == 0) {
196 // File System
197 Fstab_File_System = ptr;
198 Current_File_System = ptr;
199 item_index++;
200 } else if (item_index == 1) {
201 // Primary Block Device
Dees_Troy38bd7602012-09-14 13:33:53 -0400202 if (Fstab_File_System == "mtd" || Fstab_File_System == "yaffs2") {
Dees_Troy094207a2012-09-26 12:00:39 -0400203 MTD_Name = ptr;
204 Find_MTD_Block_Device(MTD_Name);
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400205 } else if (Fstab_File_System == "bml") {
206 if (Mount_Point == "/boot")
207 MTD_Name = "boot";
208 else if (Mount_Point == "/recovery")
209 MTD_Name = "recovery";
210 Primary_Block_Device = ptr;
211 if (*ptr != '/')
Dees_Troy2673cec2013-04-02 20:22:16 +0000212 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 -0400213 } else if (*ptr != '/') {
Dees_Troy5bf43922012-09-07 16:07:55 -0400214 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000215 LOGERR("Invalid block device on '%s', '%s', %i\n", Line.c_str(), ptr, index);
Dees_Troy5bf43922012-09-07 16:07:55 -0400216 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000217 LOGINFO("Invalid block device on '%s', '%s', %i\n", Line.c_str(), ptr, index);
Dees_Troy5bf43922012-09-07 16:07:55 -0400218 return 0;
Dees_Troy38bd7602012-09-14 13:33:53 -0400219 } else {
220 Primary_Block_Device = ptr;
221 Find_Real_Block_Device(Primary_Block_Device, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400222 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400223 item_index++;
224 } else if (item_index > 1) {
225 if (*ptr == '/') {
226 // Alternate Block Device
227 Alternate_Block_Device = ptr;
228 Find_Real_Block_Device(Alternate_Block_Device, Display_Error);
229 } else if (strlen(ptr) > 7 && strncmp(ptr, "length=", 7) == 0) {
230 // Partition length
231 ptr += 7;
232 Length = atoi(ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400233 } else if (strlen(ptr) > 6 && strncmp(ptr, "flags=", 6) == 0) {
234 // Custom flags, save for later so that new values aren't overwritten by defaults
235 ptr += 6;
236 Flags = ptr;
Dees_Troy68cab492012-12-12 19:29:35 +0000237 Process_Flags(Flags, Display_Error);
Dees_Troy38bd7602012-09-14 13:33:53 -0400238 } else if (strlen(ptr) == 4 && (strncmp(ptr, "NULL", 4) == 0 || strncmp(ptr, "null", 4) == 0 || strncmp(ptr, "null", 4) == 0)) {
239 // Do nothing
Dees_Troy5bf43922012-09-07 16:07:55 -0400240 } else {
241 // Unhandled data
Dees_Troy2673cec2013-04-02 20:22:16 +0000242 LOGINFO("Unhandled fstab information: '%s', %i, line: '%s'\n", ptr, index, Line.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400243 }
244 }
245 while (index < line_len && full_line[index] != '\0')
246 index++;
247 }
248
249 if (!Is_File_System(Fstab_File_System) && !Is_Image(Fstab_File_System)) {
250 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000251 LOGERR("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400252 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000253 LOGINFO("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400254 return 0;
255 } else if (Is_File_System(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400256 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400257 Setup_File_System(Display_Error);
258 if (Mount_Point == "/system") {
259 Display_Name = "System";
Dees_Troya13d74f2013-03-24 08:54:55 -0500260 Backup_Display_Name = Display_Name;
261 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400262 Wipe_Available_in_GUI = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500263 Can_Be_Backed_Up = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400264 } else if (Mount_Point == "/data") {
265 Display_Name = "Data";
Dees_Troya13d74f2013-03-24 08:54:55 -0500266 Backup_Display_Name = Display_Name;
267 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400268 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400269 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500270 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000271 Can_Encrypt_Backup = true;
272 Use_Userdata_Encryption = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400273#ifdef RECOVERY_SDCARD_ON_DATA
Dees_Troya13d74f2013-03-24 08:54:55 -0500274 Storage_Name = "Internal Storage";
Dees_Troy5bf43922012-09-07 16:07:55 -0400275 Has_Data_Media = true;
Dees_Troy51127312012-09-08 13:08:49 -0400276 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500277 Is_Settings_Storage = true;
Dees_Troy51127312012-09-08 13:08:49 -0400278 Storage_Path = "/data/media";
Dees_Troy16b74352012-11-14 22:27:31 +0000279 Symlink_Path = Storage_Path;
Dees_Troy657c3092012-09-10 20:32:10 -0400280 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
281 Make_Dir("/emmc", Display_Error);
Dees_Troy657c3092012-09-10 20:32:10 -0400282 Symlink_Mount_Point = "/emmc";
283 } else {
284 Make_Dir("/sdcard", Display_Error);
Dees_Troy657c3092012-09-10 20:32:10 -0400285 Symlink_Mount_Point = "/sdcard";
286 }
Dees_Troy16b74352012-11-14 22:27:31 +0000287 if (Mount(false) && TWFunc::Path_Exists("/data/media/0")) {
288 Storage_Path = "/data/media/0";
289 Symlink_Path = Storage_Path;
290 DataManager::SetValue(TW_INTERNAL_PATH, "/data/media/0");
291 UnMount(true);
292 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400293#endif
294#ifdef TW_INCLUDE_CRYPTO
295 Can_Be_Encrypted = true;
Dees_Troy657c3092012-09-10 20:32:10 -0400296 char crypto_blkdev[255];
297 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
298 if (strcmp(crypto_blkdev, "error") != 0) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400299 DataManager::SetValue(TW_DATA_BLK_DEVICE, Primary_Block_Device);
Dees_Troy657c3092012-09-10 20:32:10 -0400300 DataManager::SetValue(TW_IS_DECRYPTED, 1);
301 Is_Encrypted = true;
302 Is_Decrypted = true;
303 Decrypted_Block_Device = crypto_blkdev;
Dees_Troy2673cec2013-04-02 20:22:16 +0000304 LOGINFO("Data already decrypted, new block device: '%s'\n", crypto_blkdev);
Dees_Troy657c3092012-09-10 20:32:10 -0400305 } else if (!Mount(false)) {
Ethan Yonker71413f42014-02-26 13:36:08 -0600306 if (Is_Present) {
307#ifdef TW_INCLUDE_JB_CRYPTO
308 // No extra flags needed
309#else
310 property_set("ro.crypto.fs_type", CRYPTO_FS_TYPE);
311 property_set("ro.crypto.fs_real_blkdev", CRYPTO_REAL_BLKDEV);
312 property_set("ro.crypto.fs_mnt_point", CRYPTO_MNT_POINT);
313 property_set("ro.crypto.fs_options", CRYPTO_FS_OPTIONS);
314 property_set("ro.crypto.fs_flags", CRYPTO_FS_FLAGS);
315 property_set("ro.crypto.keyfile.userdata", CRYPTO_KEY_LOC);
316#ifdef CRYPTO_SD_FS_TYPE
317 property_set("ro.crypto.sd_fs_type", CRYPTO_SD_FS_TYPE);
318 property_set("ro.crypto.sd_fs_real_blkdev", CRYPTO_SD_REAL_BLKDEV);
319 property_set("ro.crypto.sd_fs_mnt_point", EXPAND(TW_INTERNAL_STORAGE_PATH));
320#endif
321 property_set("rw.km_fips_status", "ready");
322#endif
323 if (cryptfs_check_footer() == 0) {
324 Is_Encrypted = true;
325 Is_Decrypted = false;
326 Can_Be_Mounted = false;
327 Current_File_System = "emmc";
328 Setup_Image(Display_Error);
329 DataManager::SetValue(TW_IS_ENCRYPTED, 1);
330 DataManager::SetValue(TW_CRYPTO_PASSWORD, "");
331 DataManager::SetValue("tw_crypto_display", "");
332 } else {
333 LOGERR("Could not mount /data and unable to find crypto footer.\n");
334 }
335 } else {
336 LOGERR("Primary block device '%s' for mount point '%s' is not present!\n", Primary_Block_Device.c_str(), Mount_Point.c_str());
337 }
Gary Peck82599a82012-11-21 16:23:12 -0800338 } else {
339 // Filesystem is not encrypted and the mount
340 // succeeded, so get it back to the original
341 // unmounted state
342 UnMount(false);
Dees_Troy51127312012-09-08 13:08:49 -0400343 }
Dees_Troy9b21af72012-10-01 15:51:46 -0400344 #ifdef RECOVERY_SDCARD_ON_DATA
345 if (!Is_Encrypted || (Is_Encrypted && Is_Decrypted))
346 Recreate_Media_Folder();
347 #endif
348#else
349 #ifdef RECOVERY_SDCARD_ON_DATA
350 Recreate_Media_Folder();
351 #endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400352#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400353 } else if (Mount_Point == "/cache") {
354 Display_Name = "Cache";
Dees_Troya13d74f2013-03-24 08:54:55 -0500355 Backup_Display_Name = Display_Name;
356 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400357 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400358 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500359 Can_Be_Backed_Up = true;
Dees_Troyce2fe772012-09-28 12:34:33 -0400360 if (Mount(false) && !TWFunc::Path_Exists("/cache/recovery/.")) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000361 LOGINFO("Recreating /cache/recovery folder.\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500362 if (mkdir("/cache/recovery", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0)
363 return -1;
Dees_Troyb46a6842012-09-25 11:06:46 -0400364 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400365 } else if (Mount_Point == "/datadata") {
Dees_Troy51127312012-09-08 13:08:49 -0400366 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400367 Display_Name = "DataData";
Dees_Troya13d74f2013-03-24 08:54:55 -0500368 Backup_Display_Name = Display_Name;
369 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400370 Is_SubPartition = true;
371 SubPartition_Of = "/data";
Dees_Troy5bf43922012-09-07 16:07:55 -0400372 DataManager::SetValue(TW_HAS_DATADATA, 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500373 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000374 Can_Encrypt_Backup = true;
375 Use_Userdata_Encryption = false; // This whole partition should be encrypted
Dees_Troy5bf43922012-09-07 16:07:55 -0400376 } else if (Mount_Point == "/sd-ext") {
Dees_Troy51127312012-09-08 13:08:49 -0400377 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400378 Display_Name = "SD-Ext";
Dees_Troya13d74f2013-03-24 08:54:55 -0500379 Backup_Display_Name = Display_Name;
380 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400381 Wipe_Available_in_GUI = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400382 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500383 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000384 Can_Encrypt_Backup = true;
385 Use_Userdata_Encryption = true;
Dees_Troy2c50e182012-09-26 20:05:28 -0400386 } else if (Mount_Point == "/boot") {
387 Display_Name = "Boot";
Dees_Troya13d74f2013-03-24 08:54:55 -0500388 Backup_Display_Name = Display_Name;
Dees_Troy2c50e182012-09-26 20:05:28 -0400389 DataManager::SetValue("tw_boot_is_mountable", 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500390 Can_Be_Backed_Up = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400391 }
392#ifdef TW_EXTERNAL_STORAGE_PATH
393 if (Mount_Point == EXPAND(TW_EXTERNAL_STORAGE_PATH)) {
394 Is_Storage = true;
395 Storage_Path = EXPAND(TW_EXTERNAL_STORAGE_PATH);
Dees_Troyc51f1f92012-09-20 15:32:13 -0400396 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500397 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400398#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000399 if (Mount_Point == "/sdcard" || Mount_Point == "/external_sd" || Mount_Point == "/external_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400400 Is_Storage = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400401 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500402 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400403#endif
Dees_Troyb05ddee2013-01-28 20:24:50 +0000404 }
Dees_Troy8170a922012-09-18 15:40:25 -0400405#ifdef TW_INTERNAL_STORAGE_PATH
406 if (Mount_Point == EXPAND(TW_INTERNAL_STORAGE_PATH)) {
407 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500408 Is_Settings_Storage = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400409 Storage_Path = EXPAND(TW_INTERNAL_STORAGE_PATH);
Dees_Troya13d74f2013-03-24 08:54:55 -0500410 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400411 }
412#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000413 if (Mount_Point == "/emmc" || Mount_Point == "/internal_sd" || Mount_Point == "/internal_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400414 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500415 Is_Settings_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500416 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400417 }
418#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400419 } else if (Is_Image(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400420 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400421 Setup_Image(Display_Error);
Dees_Troya13d74f2013-03-24 08:54:55 -0500422 if (Mount_Point == "/boot") {
423 Display_Name = "Boot";
424 Backup_Display_Name = Display_Name;
425 Can_Be_Backed_Up = true;
426 } else if (Mount_Point == "/recovery") {
427 Display_Name = "Recovery";
428 Backup_Display_Name = Display_Name;
Dees_Troya13d74f2013-03-24 08:54:55 -0500429 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400430 }
431
Dees_Troy51127312012-09-08 13:08:49 -0400432 // Process any custom flags
433 if (Flags.size() > 0)
434 Process_Flags(Flags, Display_Error);
Dees_Troy51127312012-09-08 13:08:49 -0400435 return true;
436}
437
Hashcode62bd9e02013-11-19 21:59:42 -0800438bool TWPartition::Process_FS_Flags(string& Options, int Flags) {
439 int i;
440 char *p;
441 char *savep;
442 char fs_options[250];
443
444 strlcpy(fs_options, Options.c_str(), sizeof(fs_options));
445 Options = "";
446
447 p = strtok_r(fs_options, ",", &savep);
448 while (p) {
449 /* Look for the flag "p" in the flag list "fl"
450 * If not found, the loop exits with fl[i].name being null.
451 */
452 for (i = 0; mount_flags[i].name; i++) {
453 if (strncmp(p, mount_flags[i].name, strlen(mount_flags[i].name)) == 0) {
454 Flags |= mount_flags[i].flag;
455 break;
456 }
457 }
458
459 if (!mount_flags[i].name) {
460 if (Options.size() > 0)
461 Options += ",";
462 Options += p;
463 }
464 p = strtok_r(NULL, ",", &savep);
465 }
466
467 return true;
468}
469
Dees_Troy51127312012-09-08 13:08:49 -0400470bool TWPartition::Process_Flags(string Flags, bool Display_Error) {
471 char flags[MAX_FSTAB_LINE_LENGTH];
Dees_Troya13d74f2013-03-24 08:54:55 -0500472 int flags_len, index = 0, ptr_len;
Dees_Troy51127312012-09-08 13:08:49 -0400473 char* ptr;
Dees_Troya13d74f2013-03-24 08:54:55 -0500474 bool skip = false, has_display_name = false, has_storage_name = false, has_backup_name = false;
Dees_Troy51127312012-09-08 13:08:49 -0400475
476 strcpy(flags, Flags.c_str());
477 flags_len = Flags.size();
478 for (index = 0; index < flags_len; index++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500479 if (flags[index] == 34)
480 skip = !skip;
481 if (!skip && flags[index] == ';')
Dees_Troy51127312012-09-08 13:08:49 -0400482 flags[index] = '\0';
483 }
484
485 index = 0;
486 while (index < flags_len) {
487 while (index < flags_len && flags[index] == '\0')
488 index++;
489 if (index >= flags_len)
490 continue;
491 ptr = flags + index;
Dees_Troya13d74f2013-03-24 08:54:55 -0500492 ptr_len = strlen(ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400493 if (strcmp(ptr, "removable") == 0) {
494 Removable = true;
Ethan Yonker06c3f932014-02-02 22:11:14 -0600495 } else if (strncmp(ptr, "storage", 7) == 0) {
496 if (ptr_len == 7) {
Ethan Yonker06c3f932014-02-02 22:11:14 -0600497 Is_Storage = true;
498 } else if (ptr_len == 9) {
499 ptr += 9;
500 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y') {
501 LOGINFO("storage set to true\n");
502 Is_Storage = true;
503 } else {
504 LOGINFO("storage set to false\n");
505 Is_Storage = false;
506 }
507 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500508 } else if (strcmp(ptr, "settingsstorage") == 0) {
509 Is_Storage = true;
Matt Mowerbf4efa32014-04-14 23:25:26 -0500510 } else if (strcmp(ptr, "andsec") == 0) {
511 Has_Android_Secure = true;
Dees_Troy63c8df72012-09-10 14:02:05 -0400512 } else if (strcmp(ptr, "canbewiped") == 0) {
513 Can_Be_Wiped = true;
Hashcodedabfd492013-08-29 22:45:30 -0700514 } else if (strcmp(ptr, "usermrf") == 0) {
515 Use_Rm_Rf = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500516 } else if (ptr_len > 7 && strncmp(ptr, "backup=", 7) == 0) {
517 ptr += 7;
518 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y')
519 Can_Be_Backed_Up = true;
520 else
521 Can_Be_Backed_Up = false;
Dees_Troy63c8df72012-09-10 14:02:05 -0400522 } else if (strcmp(ptr, "wipeingui") == 0) {
523 Can_Be_Wiped = true;
524 Wipe_Available_in_GUI = true;
525 } else if (strcmp(ptr, "wipeduringfactoryreset") == 0) {
526 Can_Be_Wiped = true;
527 Wipe_Available_in_GUI = true;
528 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500529 } else if (ptr_len > 15 && strncmp(ptr, "subpartitionof=", 15) == 0) {
Dees_Troy2c4c26f2013-01-28 15:26:43 +0000530 ptr += 15;
Dees_Troy51127312012-09-08 13:08:49 -0400531 Is_SubPartition = true;
532 SubPartition_Of = ptr;
Dees_Troy68cab492012-12-12 19:29:35 +0000533 } else if (strcmp(ptr, "ignoreblkid") == 0) {
534 Ignore_Blkid = true;
Dees_Troy16c2b312013-01-15 16:51:18 +0000535 } else if (strcmp(ptr, "retainlayoutversion") == 0) {
536 Retain_Layout_Version = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500537 } else if (ptr_len > 8 && strncmp(ptr, "symlink=", 8) == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400538 ptr += 8;
539 Symlink_Path = ptr;
Dees_Troya13d74f2013-03-24 08:54:55 -0500540 } else if (ptr_len > 8 && strncmp(ptr, "display=", 8) == 0) {
541 has_display_name = true;
Dees_Troy51127312012-09-08 13:08:49 -0400542 ptr += 8;
Dees_Troya13d74f2013-03-24 08:54:55 -0500543 if (*ptr == '\"') ptr++;
Dees_Troy51127312012-09-08 13:08:49 -0400544 Display_Name = ptr;
Dees_Troya13d74f2013-03-24 08:54:55 -0500545 if (Display_Name.substr(Display_Name.size() - 1, 1) == "\"") {
546 Display_Name.resize(Display_Name.size() - 1);
547 }
548 } else if (ptr_len > 11 && strncmp(ptr, "storagename=", 11) == 0) {
549 has_storage_name = true;
550 ptr += 11;
551 if (*ptr == '\"') ptr++;
552 Storage_Name = ptr;
553 if (Storage_Name.substr(Storage_Name.size() - 1, 1) == "\"") {
554 Storage_Name.resize(Storage_Name.size() - 1);
555 }
556 } else if (ptr_len > 11 && strncmp(ptr, "backupname=", 10) == 0) {
557 has_backup_name = true;
558 ptr += 10;
559 if (*ptr == '\"') ptr++;
560 Backup_Display_Name = ptr;
561 if (Backup_Display_Name.substr(Backup_Display_Name.size() - 1, 1) == "\"") {
562 Backup_Display_Name.resize(Backup_Display_Name.size() - 1);
563 }
564 } else if (ptr_len > 10 && strncmp(ptr, "blocksize=", 10) == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400565 ptr += 10;
566 Format_Block_Size = atoi(ptr);
Dees_Troya13d74f2013-03-24 08:54:55 -0500567 } else if (ptr_len > 7 && strncmp(ptr, "length=", 7) == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400568 ptr += 7;
569 Length = atoi(ptr);
Dees_Troy83bd4832013-05-04 12:39:56 +0000570 } else if (ptr_len > 17 && strncmp(ptr, "canencryptbackup=", 17) == 0) {
571 ptr += 17;
572 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y')
573 Can_Encrypt_Backup = true;
574 else
575 Can_Encrypt_Backup = false;
576 } else if (ptr_len > 21 && strncmp(ptr, "userdataencryptbackup=", 21) == 0) {
577 ptr += 21;
578 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y') {
579 Can_Encrypt_Backup = true;
580 Use_Userdata_Encryption = true;
581 } else {
582 Use_Userdata_Encryption = false;
583 }
Hashcode62bd9e02013-11-19 21:59:42 -0800584 } else if (ptr_len > 8 && strncmp(ptr, "fsflags=", 8) == 0) {
585 ptr += 8;
586 if (*ptr == '\"') ptr++;
587
588 Mount_Options = ptr;
589 if (Mount_Options.substr(Mount_Options.size() - 1, 1) == "\"") {
590 Mount_Options.resize(Mount_Options.size() - 1);
591 }
592 Process_FS_Flags(Mount_Options, Mount_Flags);
Dees_Troy51127312012-09-08 13:08:49 -0400593 } else {
594 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000595 LOGERR("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400596 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000597 LOGINFO("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400598 }
599 while (index < flags_len && flags[index] != '\0')
600 index++;
601 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500602 if (has_display_name && !has_storage_name)
603 Storage_Name = Display_Name;
604 if (!has_display_name && has_storage_name)
605 Display_Name = Storage_Name;
Dees_Troy74fb2e92013-04-15 14:35:47 +0000606 if (has_display_name && !has_backup_name && Backup_Display_Name != "Android Secure")
Dees_Troya13d74f2013-03-24 08:54:55 -0500607 Backup_Display_Name = Display_Name;
608 if (!has_display_name && has_backup_name)
609 Display_Name = Backup_Display_Name;
Dees_Troy51127312012-09-08 13:08:49 -0400610 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400611}
612
Dees_Troy5bf43922012-09-07 16:07:55 -0400613bool TWPartition::Is_File_System(string File_System) {
614 if (File_System == "ext2" ||
Dees_Troy63c8df72012-09-10 14:02:05 -0400615 File_System == "ext3" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400616 File_System == "ext4" ||
617 File_System == "vfat" ||
618 File_System == "ntfs" ||
619 File_System == "yaffs2" ||
bigbiff bigbiff3e146522012-11-14 14:32:59 -0500620 File_System == "exfat" ||
Dees_Troye5017042013-08-29 16:38:55 +0000621 File_System == "f2fs" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400622 File_System == "auto")
623 return true;
624 else
625 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400626}
627
Dees_Troy5bf43922012-09-07 16:07:55 -0400628bool TWPartition::Is_Image(string File_System) {
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400629 if (File_System == "emmc" || File_System == "mtd" || File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -0400630 return true;
631 else
632 return false;
633}
634
Dees_Troy51127312012-09-08 13:08:49 -0400635bool TWPartition::Make_Dir(string Path, bool Display_Error) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400636 if (!TWFunc::Path_Exists(Path)) {
Dees_Troy51127312012-09-08 13:08:49 -0400637 if (mkdir(Path.c_str(), 0777) == -1) {
638 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000639 LOGERR("Can not create '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400640 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000641 LOGINFO("Can not create '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400642 return false;
643 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000644 LOGINFO("Created '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400645 return true;
646 }
647 }
648 return true;
649}
650
Dees_Troy5bf43922012-09-07 16:07:55 -0400651void TWPartition::Setup_File_System(bool Display_Error) {
652 struct statfs st;
653
654 Can_Be_Mounted = true;
655 Can_Be_Wiped = true;
656
Dees_Troy5bf43922012-09-07 16:07:55 -0400657 // Make the mount point folder if it doesn't exist
Dees_Troy51127312012-09-08 13:08:49 -0400658 Make_Dir(Mount_Point, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400659 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
660 Backup_Name = Display_Name;
661 Backup_Method = FILES;
662}
663
664void TWPartition::Setup_Image(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400665 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
666 Backup_Name = Display_Name;
Gary Peck82599a82012-11-21 16:23:12 -0800667 if (Current_File_System == "emmc")
Dees_Troy5bf43922012-09-07 16:07:55 -0400668 Backup_Method = DD;
Gary Peck82599a82012-11-21 16:23:12 -0800669 else if (Current_File_System == "mtd" || Current_File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -0400670 Backup_Method = FLASH_UTILS;
671 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000672 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 -0400673 if (Find_Partition_Size()) {
674 Used = Size;
675 Backup_Size = Size;
Dees_Troy51a0e822012-09-05 15:24:24 -0400676 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400677 if (Display_Error)
Dees Troyd932ce12013-10-18 17:12:59 +0000678 LOGERR("Unable to find partition size for '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400679 else
Dees Troyd932ce12013-10-18 17:12:59 +0000680 LOGINFO("Unable to find partition size for '%s'\n", Mount_Point.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400681 }
682}
683
Dees_Troye58d5262012-09-21 12:27:57 -0400684void TWPartition::Setup_AndSec(void) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500685 Backup_Display_Name = "Android Secure";
Dees_Troye58d5262012-09-21 12:27:57 -0400686 Backup_Name = "and-sec";
Dees_Troya13d74f2013-03-24 08:54:55 -0500687 Can_Be_Backed_Up = true;
Dees_Troye58d5262012-09-21 12:27:57 -0400688 Has_Android_Secure = true;
689 Symlink_Path = Mount_Point + "/.android_secure";
690 Symlink_Mount_Point = "/and-sec";
691 Backup_Path = Symlink_Mount_Point;
692 Make_Dir("/and-sec", true);
693 Recreate_AndSec_Folder();
Ethan Yonkerd4d10732014-02-03 15:27:52 -0600694 Mount_Storage_Retry();
Dees_Troye58d5262012-09-21 12:27:57 -0400695}
696
Dees_Troy5bf43922012-09-07 16:07:55 -0400697void TWPartition::Find_Real_Block_Device(string& Block, bool Display_Error) {
698 char device[512], realDevice[512];
699
700 strcpy(device, Block.c_str());
701 memset(realDevice, 0, sizeof(realDevice));
702 while (readlink(device, realDevice, sizeof(realDevice)) > 0)
703 {
704 strcpy(device, realDevice);
705 memset(realDevice, 0, sizeof(realDevice));
706 }
707
708 if (device[0] != '/') {
709 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000710 LOGERR("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400711 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000712 LOGINFO("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400713 return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400714 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400715 Block = device;
716 return;
717 }
718}
719
Dees_Troy8e337f32012-10-13 22:07:49 -0400720void TWPartition::Mount_Storage_Retry(void) {
721 // On some devices, storage doesn't want to mount right away, retry and sleep
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500722 if (!Mount(true)) {
Dees_Troy8e337f32012-10-13 22:07:49 -0400723 int retry_count = 5;
724 while (retry_count > 0 && !Mount(false)) {
725 usleep(500000);
726 retry_count--;
727 }
728 Mount(true);
729 }
730}
731
Dees_Troy38bd7602012-09-14 13:33:53 -0400732bool TWPartition::Find_MTD_Block_Device(string MTD_Name) {
733 FILE *fp = NULL;
734 char line[255];
735
736 fp = fopen("/proc/mtd", "rt");
737 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000738 LOGERR("Device does not support /proc/mtd\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400739 return false;
740 }
741
742 while (fgets(line, sizeof(line), fp) != NULL)
743 {
744 char device[32], label[32];
745 unsigned long size = 0;
746 char* fstype = NULL;
747 int deviceId;
748
749 sscanf(line, "%s %lx %*s %*c%s", device, &size, label);
750
751 // Skip header and blank lines
752 if ((strcmp(device, "dev:") == 0) || (strlen(line) < 8))
753 continue;
754
755 // Strip off the trailing " from the label
756 label[strlen(label)-1] = '\0';
757
758 if (strcmp(label, MTD_Name.c_str()) == 0) {
759 // We found our device
760 // Strip off the trailing : from the device
761 device[strlen(device)-1] = '\0';
762 if (sscanf(device,"mtd%d", &deviceId) == 1) {
763 sprintf(device, "/dev/block/mtdblock%d", deviceId);
764 Primary_Block_Device = device;
Dees_Troy76543db2013-06-19 16:24:30 +0000765 fclose(fp);
766 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -0400767 }
768 }
769 }
770 fclose(fp);
771
772 return false;
773}
774
Dees_Troy51127312012-09-08 13:08:49 -0400775bool TWPartition::Get_Size_Via_statfs(bool Display_Error) {
776 struct statfs st;
777 string Local_Path = Mount_Point + "/.";
778
779 if (!Mount(Display_Error))
780 return false;
781
782 if (statfs(Local_Path.c_str(), &st) != 0) {
783 if (!Removable) {
784 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000785 LOGERR("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400786 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000787 LOGINFO("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400788 }
789 return false;
790 }
791 Size = (st.f_blocks * st.f_bsize);
792 Used = ((st.f_blocks - st.f_bfree) * st.f_bsize);
793 Free = (st.f_bfree * st.f_bsize);
794 Backup_Size = Used;
795 return true;
796}
797
798bool TWPartition::Get_Size_Via_df(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400799 FILE* fp;
800 char command[255], line[512];
801 int include_block = 1;
802 unsigned int min_len;
803
804 if (!Mount(Display_Error))
805 return false;
806
Dees_Troy38bd7602012-09-14 13:33:53 -0400807 min_len = Actual_Block_Device.size() + 2;
Dees_Troy51127312012-09-08 13:08:49 -0400808 sprintf(command, "df %s > /tmp/dfoutput.txt", Mount_Point.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +0200809 TWFunc::Exec_Cmd(command);
Dees_Troy51127312012-09-08 13:08:49 -0400810 fp = fopen("/tmp/dfoutput.txt", "rt");
811 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000812 LOGINFO("Unable to open /tmp/dfoutput.txt.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400813 return false;
Dees_Troy51127312012-09-08 13:08:49 -0400814 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400815
816 while (fgets(line, sizeof(line), fp) != NULL)
817 {
818 unsigned long blocks, used, available;
819 char device[64];
820 char tmpString[64];
821
822 if (strncmp(line, "Filesystem", 10) == 0)
823 continue;
824 if (strlen(line) < min_len) {
825 include_block = 0;
826 continue;
827 }
828 if (include_block) {
829 sscanf(line, "%s %lu %lu %lu", device, &blocks, &used, &available);
830 } else {
831 // The device block string is so long that the df information is on the next line
832 int space_count = 0;
Dees_Troye58d5262012-09-21 12:27:57 -0400833 sprintf(tmpString, "/dev/block/%s", Actual_Block_Device.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400834 while (tmpString[space_count] == 32)
835 space_count++;
836 sscanf(line + space_count, "%lu %lu %lu", &blocks, &used, &available);
837 }
838
839 // Adjust block size to byte size
840 Size = blocks * 1024ULL;
841 Used = used * 1024ULL;
842 Free = available * 1024ULL;
843 Backup_Size = Used;
844 }
845 fclose(fp);
846 return true;
847}
848
Dees_Troy5bf43922012-09-07 16:07:55 -0400849bool TWPartition::Find_Partition_Size(void) {
850 FILE* fp;
851 char line[512];
852 string tmpdevice;
853
igoriok87e3d932013-01-31 21:03:53 +0200854 fp = fopen("/proc/dumchar_info", "rt");
855 if (fp != NULL) {
856 while (fgets(line, sizeof(line), fp) != NULL)
857 {
858 char label[32], device[32];
859 unsigned long size = 0;
860
861 sscanf(line, "%s %lx %*lx %*lu %s", label, &size, device);
862
bigbiff bigbiff34684ff2013-12-01 21:03:45 -0500863 // Skip header, annotation and blank lines
igoriok87e3d932013-01-31 21:03:53 +0200864 if ((strncmp(device, "/dev/", 5) != 0) || (strlen(line) < 8))
865 continue;
866
867 tmpdevice = "/dev/";
868 tmpdevice += label;
869 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
870 Size = size;
871 fclose(fp);
872 return true;
873 }
874 }
875 }
876
Dees_Troy5bf43922012-09-07 16:07:55 -0400877 // In this case, we'll first get the partitions we care about (with labels)
878 fp = fopen("/proc/partitions", "rt");
879 if (fp == NULL)
880 return false;
881
882 while (fgets(line, sizeof(line), fp) != NULL)
883 {
884 unsigned long major, minor, blocks;
885 char device[512];
886 char tmpString[64];
887
Dees_Troy63c8df72012-09-10 14:02:05 -0400888 if (strlen(line) < 7 || line[0] == 'm') continue;
Dees_Troy5bf43922012-09-07 16:07:55 -0400889 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
890
891 tmpdevice = "/dev/block/";
892 tmpdevice += device;
Dees_Troy38bd7602012-09-14 13:33:53 -0400893 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400894 // Adjust block size to byte size
895 Size = blocks * 1024ULL;
896 fclose(fp);
897 return true;
898 }
899 }
900 fclose(fp);
901 return false;
902}
903
Dees_Troy5bf43922012-09-07 16:07:55 -0400904bool TWPartition::Is_Mounted(void) {
905 if (!Can_Be_Mounted)
906 return false;
907
908 struct stat st1, st2;
909 string test_path;
910
911 // Check to see if the mount point directory exists
912 test_path = Mount_Point + "/.";
913 if (stat(test_path.c_str(), &st1) != 0) return false;
914
915 // Check to see if the directory above the mount point exists
916 test_path = Mount_Point + "/../.";
917 if (stat(test_path.c_str(), &st2) != 0) return false;
918
919 // Compare the device IDs -- if they match then we're (probably) using tmpfs instead of an actual device
920 int ret = (st1.st_dev != st2.st_dev) ? true : false;
921
922 return ret;
923}
924
925bool TWPartition::Mount(bool Display_Error) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000926 int exfat_mounted = 0;
927
Dees_Troy5bf43922012-09-07 16:07:55 -0400928 if (Is_Mounted()) {
929 return true;
930 } else if (!Can_Be_Mounted) {
931 return false;
932 }
Dees_Troy38bd7602012-09-14 13:33:53 -0400933
934 Find_Actual_Block_Device();
935
936 // Check the current file system before mounting
937 Check_FS_Type();
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000938 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/exfat-fuse")) {
Dees_Troye34c1332013-02-06 19:13:00 +0000939 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 +0000940 LOGINFO("cmd: %s\n", cmd.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000941 string result;
942 if (TWFunc::Exec_Cmd(cmd, result) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000943 LOGINFO("exfat-fuse failed to mount with result '%s', trying vfat\n", result.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000944 Current_File_System = "vfat";
945 } else {
946#ifdef TW_NO_EXFAT_FUSE
947 UnMount(false);
948 // We'll let the kernel handle it but using exfat-fuse to detect if the file system is actually exfat
949 // Some kernels let us mount vfat as exfat which doesn't work out too well
950#else
951 exfat_mounted = 1;
952#endif
953 }
954 }
Dees_Troy22042032012-12-18 21:23:08 +0000955 if (Fstab_File_System == "yaffs2") {
956 // mount an MTD partition as a YAFFS2 filesystem.
Dees_Troy76543db2013-06-19 16:24:30 +0000957 const unsigned long flags = MS_NOATIME | MS_NODEV | MS_NODIRATIME;
958 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags, NULL) < 0) {
959 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags | MS_RDONLY, NULL) < 0) {
960 if (Display_Error)
961 LOGERR("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
962 else
963 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
964 return false;
965 } else {
966 LOGINFO("Mounted '%s' (MTD) as RO\n", Mount_Point.c_str());
967 return true;
968 }
969 } else {
970 struct stat st;
971 string test_path = Mount_Point;
972 if (stat(test_path.c_str(), &st) < 0) {
973 if (Display_Error)
974 LOGERR("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
975 else
976 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
977 return false;
978 }
979 mode_t new_mode = st.st_mode | S_IXUSR | S_IXGRP | S_IXOTH;
980 if (new_mode != st.st_mode) {
981 LOGINFO("Fixing execute permissions for %s\n", Mount_Point.c_str());
982 if (chmod(Mount_Point.c_str(), new_mode) < 0) {
983 if (Display_Error)
984 LOGERR("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
985 else
986 LOGINFO("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
987 return false;
988 }
989 }
Dees_Troy22042032012-12-18 21:23:08 +0000990 return true;
Dees_Troy76543db2013-06-19 16:24:30 +0000991 }
Dees Troy216e0422014-02-07 03:46:42 +0000992 } 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 +0000993#ifdef TW_NO_EXFAT_FUSE
994 if (Current_File_System == "exfat") {
Dees_Troy2673cec2013-04-02 20:22:16 +0000995 LOGINFO("Mounting exfat failed, trying vfat...\n");
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000996 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), "vfat", 0, NULL) != 0) {
Dees_Troy85f44ed2013-01-09 18:42:36 +0000997 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000998 LOGERR("Unable to mount '%s'\n", Mount_Point.c_str());
Dees_Troy85f44ed2013-01-09 18:42:36 +0000999 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001000 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
Hashcode62bd9e02013-11-19 21:59:42 -08001001 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 +00001002 return false;
Dees_Troy85f44ed2013-01-09 18:42:36 +00001003 }
Dees_Troyb05ddee2013-01-28 20:24:50 +00001004 } else {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001005#endif
bigbiff bigbiff26774a02014-03-29 18:22:00 -04001006 if (!Removable && Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001007 LOGERR("Unable to mount '%s'\n", Mount_Point.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001008 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001009 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
1010 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 +00001011 return false;
1012#ifdef TW_NO_EXFAT_FUSE
Dees_Troy85f44ed2013-01-09 18:42:36 +00001013 }
1014#endif
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001015 }
1016#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
1017 string MetaEcfsFile = EXPAND(TW_EXTERNAL_STORAGE_PATH);
1018 MetaEcfsFile += "/.MetaEcfsFile";
1019 if (EcryptFS_Password.size() > 0 && PartitionManager.Mount_By_Path("/data", false) && TWFunc::Path_Exists(MetaEcfsFile)) {
1020 if (mount_ecryptfs_drive(EcryptFS_Password.c_str(), Mount_Point.c_str(), Mount_Point.c_str(), 0) != 0) {
1021 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001022 LOGERR("Unable to mount ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001023 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001024 LOGINFO("Unable to mount ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001025 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001026 LOGINFO("Successfully mounted ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001027 Is_Decrypted = true;
Dees_Troy51127312012-09-08 13:08:49 -04001028 }
Dees_Troy066eb302013-08-23 17:20:32 +00001029 } else if (Mount_Point == EXPAND(TW_EXTERNAL_STORAGE_PATH)) {
1030 if (Is_Decrypted)
1031 LOGINFO("Mounting external storage, '%s' is not encrypted\n", Mount_Point.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001032 Is_Decrypted = false;
1033 }
1034#endif
1035 if (Removable)
1036 Update_Size(Display_Error);
1037
1038 if (!Symlink_Mount_Point.empty()) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001039 string Command = "mount '" + Symlink_Path + "' '" + Symlink_Mount_Point + "'";
1040 TWFunc::Exec_Cmd(Command);
Dees_Troy5bf43922012-09-07 16:07:55 -04001041 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001042 return true;
1043}
1044
1045bool TWPartition::UnMount(bool Display_Error) {
1046 if (Is_Mounted()) {
1047 int never_unmount_system;
1048
1049 DataManager::GetValue(TW_DONT_UNMOUNT_SYSTEM, never_unmount_system);
1050 if (never_unmount_system == 1 && Mount_Point == "/system")
1051 return true; // Never unmount system if you're not supposed to unmount it
1052
Dees_Troyc8bafa12013-01-10 15:43:00 +00001053#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
1054 if (EcryptFS_Password.size() > 0) {
1055 if (unmount_ecryptfs_drive(Mount_Point.c_str()) != 0) {
1056 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001057 LOGERR("Unable to unmount ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troyc8bafa12013-01-10 15:43:00 +00001058 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001059 LOGINFO("Unable to unmount ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troyc8bafa12013-01-10 15:43:00 +00001060 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001061 LOGINFO("Successfully unmounted ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troyc8bafa12013-01-10 15:43:00 +00001062 }
1063 }
1064#endif
1065
Dees_Troy38bd7602012-09-14 13:33:53 -04001066 if (!Symlink_Mount_Point.empty())
1067 umount(Symlink_Mount_Point.c_str());
1068
Dees_Troyb05ddee2013-01-28 20:24:50 +00001069 umount(Mount_Point.c_str());
1070 if (Is_Mounted()) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001071 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001072 LOGERR("Unable to unmount '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001073 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001074 LOGINFO("Unable to unmount '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001075 return false;
1076 } else
1077 return true;
1078 } else {
1079 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001080 }
1081}
1082
Gary Peck43acadf2012-11-21 21:19:01 -08001083bool TWPartition::Wipe(string New_File_System) {
Dees_Troy16c2b312013-01-15 16:51:18 +00001084 bool wiped = false, update_crypt = false;
1085 int check;
1086 string Layout_Filename = Mount_Point + "/.layout_version";
1087
Dees_Troy38bd7602012-09-14 13:33:53 -04001088 if (!Can_Be_Wiped) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001089 LOGERR("Partition '%s' cannot be wiped.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001090 return false;
1091 }
1092
Dees_Troyc51f1f92012-09-20 15:32:13 -04001093 if (Mount_Point == "/cache")
Dees_Troy2673cec2013-04-02 20:22:16 +00001094 Log_Offset = 0;
Dees_Troyc51f1f92012-09-20 15:32:13 -04001095
Dees_Troyce675462013-01-09 19:48:21 +00001096#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
1097 if (Mount_Point == "/data" && Mount(false)) {
1098 if (TWFunc::Path_Exists("/data/system/edk_p_sd"))
1099 TWFunc::copy_file("/data/system/edk_p_sd", "/tmp/edk_p_sd", 0600);
1100 }
1101#endif
1102
Dees_Troy16c2b312013-01-15 16:51:18 +00001103 if (Retain_Layout_Version && Mount(false) && TWFunc::Path_Exists(Layout_Filename))
1104 TWFunc::copy_file(Layout_Filename, "/.layout_version", 0600);
1105 else
1106 unlink("/.layout_version");
Dees_Troy38bd7602012-09-14 13:33:53 -04001107
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001108 if (Has_Data_Media && Current_File_System == New_File_System) {
Dees_Troy16c2b312013-01-15 16:51:18 +00001109 wiped = Wipe_Data_Without_Wiping_Media();
1110 } else {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001111
Dees_Troy16c2b312013-01-15 16:51:18 +00001112 DataManager::GetValue(TW_RM_RF_VAR, check);
1113
Hashcodedabfd492013-08-29 22:45:30 -07001114 if (check || Use_Rm_Rf)
Dees_Troy16c2b312013-01-15 16:51:18 +00001115 wiped = Wipe_RMRF();
1116 else if (New_File_System == "ext4")
1117 wiped = Wipe_EXT4();
1118 else if (New_File_System == "ext2" || New_File_System == "ext3")
1119 wiped = Wipe_EXT23(New_File_System);
1120 else if (New_File_System == "vfat")
1121 wiped = Wipe_FAT();
1122 else if (New_File_System == "exfat")
1123 wiped = Wipe_EXFAT();
1124 else if (New_File_System == "yaffs2")
1125 wiped = Wipe_MTD();
Dees_Troye5017042013-08-29 16:38:55 +00001126 else if (New_File_System == "f2fs")
1127 wiped = Wipe_F2FS();
Dees_Troy16c2b312013-01-15 16:51:18 +00001128 else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001129 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 +00001130 unlink("/.layout_version");
1131 return false;
1132 }
1133 update_crypt = wiped;
Gary Pecke8bc5d72012-12-21 06:45:25 -08001134 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001135
Gary Pecke8bc5d72012-12-21 06:45:25 -08001136 if (wiped) {
Dees_Troyce675462013-01-09 19:48:21 +00001137#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
1138 if (Mount_Point == "/data" && Mount(false)) {
1139 if (TWFunc::Path_Exists("/tmp/edk_p_sd")) {
1140 Make_Dir("/data/system", true);
1141 TWFunc::copy_file("/tmp/edk_p_sd", "/data/system/edk_p_sd", 0600);
1142 }
1143 }
1144#endif
Dees_Troy16c2b312013-01-15 16:51:18 +00001145
Dees_Troy1c1ac442013-01-17 21:42:14 +00001146 if (Mount_Point == "/cache")
1147 DataManager::Output_Version();
1148
Dees_Troy16c2b312013-01-15 16:51:18 +00001149 if (TWFunc::Path_Exists("/.layout_version") && Mount(false))
1150 TWFunc::copy_file("/.layout_version", Layout_Filename, 0600);
1151
1152 if (update_crypt) {
1153 Setup_File_System(false);
1154 if (Is_Encrypted && !Is_Decrypted) {
1155 // just wiped an encrypted partition back to its unencrypted state
1156 Is_Encrypted = false;
1157 Is_Decrypted = false;
1158 Decrypted_Block_Device = "";
1159 if (Mount_Point == "/data") {
1160 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1161 DataManager::SetValue(TW_IS_DECRYPTED, 0);
1162 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001163 }
1164 }
1165 }
1166 return wiped;
Dees_Troy51a0e822012-09-05 15:24:24 -04001167}
1168
Gary Peck43acadf2012-11-21 21:19:01 -08001169bool TWPartition::Wipe() {
Gary Peck82599a82012-11-21 16:23:12 -08001170 if (Is_File_System(Current_File_System))
1171 return Wipe(Current_File_System);
1172 else
1173 return Wipe(Fstab_File_System);
Gary Peck43acadf2012-11-21 21:19:01 -08001174}
1175
Dees_Troye58d5262012-09-21 12:27:57 -04001176bool TWPartition::Wipe_AndSec(void) {
1177 if (!Has_Android_Secure)
1178 return false;
1179
Dees_Troye58d5262012-09-21 12:27:57 -04001180 if (!Mount(true))
1181 return false;
1182
Dees_Troy2673cec2013-04-02 20:22:16 +00001183 gui_print("Wiping %s\n", Backup_Display_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001184 TWFunc::removeDir(Mount_Point + "/.android_secure/", true);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001185 return true;
Dees_Troye58d5262012-09-21 12:27:57 -04001186}
1187
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001188bool TWPartition::Can_Repair() {
1189 if (Current_File_System == "vfat" && TWFunc::Path_Exists("/sbin/dosfsck"))
1190 return true;
1191 else if ((Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") && TWFunc::Path_Exists("/sbin/e2fsck"))
1192 return true;
1193 else if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/fsck.exfat"))
1194 return true;
1195 else if (Current_File_System == "f2fs" && TWFunc::Path_Exists("/sbin/fsck.f2fs"))
1196 return true;
1197 return false;
1198}
1199
1200bool TWPartition::Repair() {
1201 string command;
1202
1203 if (Current_File_System == "vfat") {
1204 if (!TWFunc::Path_Exists("/sbin/dosfsck")) {
1205 gui_print("dosfsck does not exist! Cannot repair!\n");
1206 return false;
1207 }
1208 if (!UnMount(true))
1209 return false;
1210 gui_print("Repairing %s using dosfsck...\n", Display_Name.c_str());
1211 Find_Actual_Block_Device();
1212 command = "/sbin/dosfsck -y " + Actual_Block_Device;
1213 LOGINFO("Repair command: %s\n", command.c_str());
1214 if (TWFunc::Exec_Cmd(command) == 0) {
1215 gui_print("Done.\n");
1216 return true;
1217 } else {
1218 LOGERR("Unable to repair '%s'.\n", Mount_Point.c_str());
1219 return false;
1220 }
1221 }
1222 if (Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") {
1223 if (!TWFunc::Path_Exists("/sbin/e2fsck")) {
1224 gui_print("e2fsck does not exist! Cannot repair!\n");
1225 return false;
1226 }
1227 if (!UnMount(true))
1228 return false;
1229 gui_print("Repairing %s using e2fsck...\n", Display_Name.c_str());
1230 Find_Actual_Block_Device();
1231 command = "/sbin/e2fsck -p " + Actual_Block_Device;
1232 LOGINFO("Repair command: %s\n", command.c_str());
1233 if (TWFunc::Exec_Cmd(command) == 0) {
1234 gui_print("Done.\n");
1235 return true;
1236 } else {
1237 LOGERR("Unable to repair '%s'.\n", Mount_Point.c_str());
1238 return false;
1239 }
1240 }
1241 if (Current_File_System == "exfat") {
1242 if (!TWFunc::Path_Exists("/sbin/fsck.exfat")) {
1243 gui_print("fsck.exfat does not exist! Cannot repair!\n");
1244 return false;
1245 }
1246 if (!UnMount(true))
1247 return false;
1248 gui_print("Repairing %s using fsck.exfat...\n", Display_Name.c_str());
1249 Find_Actual_Block_Device();
1250 command = "/sbin/fsck.exfat " + Actual_Block_Device;
1251 LOGINFO("Repair command: %s\n", command.c_str());
1252 if (TWFunc::Exec_Cmd(command) == 0) {
1253 gui_print("Done.\n");
1254 return true;
1255 } else {
1256 LOGERR("Unable to repair '%s'.\n", Mount_Point.c_str());
1257 return false;
1258 }
1259 }
1260 if (Current_File_System == "f2fs") {
1261 if (!TWFunc::Path_Exists("/sbin/fsck.f2fs")) {
1262 gui_print("fsck.f2fs does not exist! Cannot repair!\n");
1263 return false;
1264 }
1265 if (!UnMount(true))
1266 return false;
1267 gui_print("Repairing %s using fsck.f2fs...\n", Display_Name.c_str());
1268 Find_Actual_Block_Device();
1269 command = "/sbin/fsck.f2fs " + Actual_Block_Device;
1270 LOGINFO("Repair command: %s\n", command.c_str());
1271 if (TWFunc::Exec_Cmd(command) == 0) {
1272 gui_print("Done.\n");
1273 return true;
1274 } else {
1275 LOGERR("Unable to repair '%s'.\n", Mount_Point.c_str());
1276 return false;
1277 }
1278 }
1279 return false;
1280}
1281
Dees_Troy51a0e822012-09-05 15:24:24 -04001282bool TWPartition::Backup(string backup_folder) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001283 if (Backup_Method == FILES)
1284 return Backup_Tar(backup_folder);
1285 else if (Backup_Method == DD)
1286 return Backup_DD(backup_folder);
1287 else if (Backup_Method == FLASH_UTILS)
1288 return Backup_Dump_Image(backup_folder);
Dees_Troy2673cec2013-04-02 20:22:16 +00001289 LOGERR("Unknown backup method for '%s'\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001290 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001291}
1292
Dees_Troy43d8b002012-09-17 16:00:01 -04001293bool TWPartition::Check_MD5(string restore_folder) {
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001294 string Full_Filename, md5file;
Dees_Troy43d8b002012-09-17 16:00:01 -04001295 char split_filename[512];
1296 int index = 0;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001297 twrpDigest md5sum;
Dees_Troy43d8b002012-09-17 16:00:01 -04001298
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001299 memset(split_filename, 0, sizeof(split_filename));
Dees_Troy43d8b002012-09-17 16:00:01 -04001300 Full_Filename = restore_folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04001301 if (!TWFunc::Path_Exists(Full_Filename)) {
1302 // This is a split archive, we presume
1303 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
Dees_Troy2673cec2013-04-02 20:22:16 +00001304 LOGINFO("split_filename: %s\n", split_filename);
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001305 md5file = split_filename;
1306 md5file += ".md5";
1307 if (!TWFunc::Path_Exists(md5file)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001308 LOGERR("No md5 file found for '%s'.\n", split_filename);
1309 LOGERR("Please unselect Enable MD5 verification to restore.\n");
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001310 return false;
1311 }
1312 md5sum.setfn(split_filename);
Dees_Troy83bd4832013-05-04 12:39:56 +00001313 while (index < 1000) {
1314 if (TWFunc::Path_Exists(split_filename) && md5sum.verify_md5digest() != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001315 LOGERR("MD5 failed to match on '%s'.\n", split_filename);
Dees_Troy43d8b002012-09-17 16:00:01 -04001316 return false;
1317 }
1318 index++;
1319 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001320 md5sum.setfn(split_filename);
Dees_Troy43d8b002012-09-17 16:00:01 -04001321 }
Dees_Troy4a2a1262012-09-18 09:33:47 -04001322 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001323 } else {
1324 // Single file archive
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001325 md5file = Full_Filename + ".md5";
1326 if (!TWFunc::Path_Exists(md5file)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001327 LOGERR("No md5 file found for '%s'.\n", Full_Filename.c_str());
1328 LOGERR("Please unselect Enable MD5 verification to restore.\n");
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001329 return false;
1330 }
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001331 md5sum.setfn(Full_Filename);
1332 if (md5sum.verify_md5digest() != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001333 LOGERR("MD5 failed to match on '%s'.\n", Full_Filename.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001334 return false;
1335 } else
1336 return true;
1337 }
1338 return false;
1339}
1340
Dees_Troy51a0e822012-09-05 15:24:24 -04001341bool TWPartition::Restore(string restore_folder) {
Gary Peck43acadf2012-11-21 21:19:01 -08001342 size_t first_period, second_period;
1343 string Restore_File_System;
1344
1345 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
Dees_Troy2673cec2013-04-02 20:22:16 +00001346 LOGINFO("Restore filename is: %s\n", Backup_FileName.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001347
1348 // Parse backup filename to extract the file system before wiping
1349 first_period = Backup_FileName.find(".");
1350 if (first_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001351 LOGERR("Unable to find file system (first period).\n");
Gary Peck43acadf2012-11-21 21:19:01 -08001352 return false;
1353 }
1354 Restore_File_System = Backup_FileName.substr(first_period + 1, Backup_FileName.size() - first_period - 1);
1355 second_period = Restore_File_System.find(".");
1356 if (second_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001357 LOGERR("Unable to find file system (second period).\n");
Gary Peck43acadf2012-11-21 21:19:01 -08001358 return false;
1359 }
1360 Restore_File_System.resize(second_period);
Dees_Troy2673cec2013-04-02 20:22:16 +00001361 LOGINFO("Restore file system is: '%s'.\n", Restore_File_System.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001362
1363 if (Is_File_System(Restore_File_System))
1364 return Restore_Tar(restore_folder, Restore_File_System);
1365 else if (Is_Image(Restore_File_System)) {
1366 if (Restore_File_System == "emmc")
1367 return Restore_DD(restore_folder);
1368 else if (Restore_File_System == "mtd" || Restore_File_System == "bml")
1369 return Restore_Flash_Image(restore_folder);
1370 }
1371
Dees_Troy2673cec2013-04-02 20:22:16 +00001372 LOGERR("Unknown restore method for '%s'\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001373 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001374}
1375
1376string TWPartition::Backup_Method_By_Name() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001377 if (Backup_Method == NONE)
1378 return "none";
1379 else if (Backup_Method == FILES)
1380 return "files";
1381 else if (Backup_Method == DD)
1382 return "dd";
1383 else if (Backup_Method == FLASH_UTILS)
1384 return "flash_utils";
1385 else
1386 return "undefined";
1387 return "ERROR!";
Dees_Troy51a0e822012-09-05 15:24:24 -04001388}
1389
1390bool TWPartition::Decrypt(string Password) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001391 LOGINFO("STUB TWPartition::Decrypt, password: '%s'\n", Password.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001392 // Is this needed?
Dees_Troy51a0e822012-09-05 15:24:24 -04001393 return 1;
1394}
1395
1396bool TWPartition::Wipe_Encryption() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001397 bool Save_Data_Media = Has_Data_Media;
1398
1399 if (!UnMount(true))
1400 return false;
1401
Dees_Troy38bd7602012-09-14 13:33:53 -04001402 Has_Data_Media = false;
Dees_Troy74fb2e92013-04-15 14:35:47 +00001403 Decrypted_Block_Device = "";
1404 Is_Decrypted = false;
1405 Is_Encrypted = false;
Gary Pecke8bc5d72012-12-21 06:45:25 -08001406 if (Wipe(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001407 Has_Data_Media = Save_Data_Media;
1408 if (Has_Data_Media && !Symlink_Mount_Point.empty()) {
1409 Recreate_Media_Folder();
1410 }
Ethan Yonker83e82572014-04-04 10:59:28 -05001411#ifndef TW_OEM_BUILD
Dees_Troy2673cec2013-04-02 20:22:16 +00001412 gui_print("You may need to reboot recovery to be able to use /data again.\n");
Ethan Yonker83e82572014-04-04 10:59:28 -05001413#endif
Dees_Troy38bd7602012-09-14 13:33:53 -04001414 return true;
1415 } else {
1416 Has_Data_Media = Save_Data_Media;
Dees_Troy2673cec2013-04-02 20:22:16 +00001417 LOGERR("Unable to format to remove encryption.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001418 return false;
1419 }
1420 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001421}
1422
1423void TWPartition::Check_FS_Type() {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001424 const char* type;
1425 blkid_probe pr;
Dees_Troy5bf43922012-09-07 16:07:55 -04001426
Dees_Troy68cab492012-12-12 19:29:35 +00001427 if (Fstab_File_System == "yaffs2" || Fstab_File_System == "mtd" || Fstab_File_System == "bml" || Ignore_Blkid)
1428 return; // Running blkid on some mtd devices causes a massive crash or needs to be skipped
Dees_Troy5bf43922012-09-07 16:07:55 -04001429
Dees_Troy38bd7602012-09-14 13:33:53 -04001430 Find_Actual_Block_Device();
Dees_Troy8170a922012-09-18 15:40:25 -04001431 if (!Is_Present)
1432 return;
Dees_Troy51127312012-09-08 13:08:49 -04001433
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001434 pr = blkid_new_probe_from_filename(Actual_Block_Device.c_str());
1435 if (blkid_do_fullprobe(pr)) {
1436 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00001437 LOGINFO("Can't probe device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001438 return;
Dees_Troy5bf43922012-09-07 16:07:55 -04001439 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001440
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001441 if (blkid_probe_lookup_value(pr, "TYPE", &type, NULL) < 0) {
1442 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00001443 LOGINFO("can't find filesystem on device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001444 return;
1445 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001446
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001447 Current_File_System = type;
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001448 blkid_free_probe(pr);
Dees_Troy51a0e822012-09-05 15:24:24 -04001449}
1450
Gary Peck43acadf2012-11-21 21:19:01 -08001451bool TWPartition::Wipe_EXT23(string File_System) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001452 if (!UnMount(true))
1453 return false;
1454
Dees_Troy43d8b002012-09-17 16:00:01 -04001455 if (TWFunc::Path_Exists("/sbin/mke2fs")) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001456 string command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001457
Dees_Troy2673cec2013-04-02 20:22:16 +00001458 gui_print("Formatting %s using mke2fs...\n", Display_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001459 Find_Actual_Block_Device();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001460 command = "mke2fs -t " + File_System + " -m 0 " + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001461 LOGINFO("mke2fs command: %s\n", command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001462 if (TWFunc::Exec_Cmd(command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001463 Current_File_System = File_System;
Dees_Troye58d5262012-09-21 12:27:57 -04001464 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001465 gui_print("Done.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001466 return true;
1467 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001468 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001469 return false;
1470 }
1471 } else
1472 return Wipe_RMRF();
1473
1474 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001475}
1476
1477bool TWPartition::Wipe_EXT4() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001478 if (!UnMount(true))
1479 return false;
1480
Dees_Troyb3265ab2013-08-30 02:59:14 +00001481#if defined(HAVE_SELINUX) && defined(USE_EXT4)
Ethan Yonkerf27497f2014-02-09 11:48:33 -06001482 int ret;
1483 char *secontext = NULL;
1484
Dees_Troya95f55c2013-08-17 13:14:43 +00001485 gui_print("Formatting %s using make_ext4fs function.\n", Display_Name.c_str());
Ethan Yonkerf27497f2014-02-09 11:48:33 -06001486
Dees Troy99c8dbf2014-03-10 16:53:58 +00001487 if (!selinux_handle || selabel_lookup(selinux_handle, &secontext, Mount_Point.c_str(), S_IFDIR) < 0) {
Ethan Yonkerf27497f2014-02-09 11:48:33 -06001488 LOGINFO("Cannot lookup security context for '%s'\n", Mount_Point.c_str());
1489 ret = make_ext4fs(Actual_Block_Device.c_str(), Length, Mount_Point.c_str(), NULL);
1490 } else {
1491 ret = make_ext4fs(Actual_Block_Device.c_str(), Length, Mount_Point.c_str(), selinux_handle);
1492 }
1493 if (ret != 0) {
Dees_Troya95f55c2013-08-17 13:14:43 +00001494 LOGERR("Unable to wipe '%s' using function call.\n", Mount_Point.c_str());
1495 return false;
1496 } else {
bigbiff bigbiffc49d7062013-10-11 20:28:00 -04001497 string sedir = Mount_Point + "/lost+found";
1498 PartitionManager.Mount_By_Path(sedir.c_str(), true);
1499 rmdir(sedir.c_str());
1500 mkdir(sedir.c_str(), S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP);
Dees_Troya95f55c2013-08-17 13:14:43 +00001501 return true;
1502 }
1503#else
Dees_Troy43d8b002012-09-17 16:00:01 -04001504 if (TWFunc::Path_Exists("/sbin/make_ext4fs")) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001505 string Command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001506
Dees_Troy2673cec2013-04-02 20:22:16 +00001507 gui_print("Formatting %s using make_ext4fs...\n", Display_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001508 Find_Actual_Block_Device();
1509 Command = "make_ext4fs";
1510 if (!Is_Decrypted && Length != 0) {
1511 // Only use length if we're not decrypted
1512 char len[32];
1513 sprintf(len, "%i", Length);
1514 Command += " -l ";
1515 Command += len;
1516 }
Dees_Troy5295d582013-09-06 15:51:08 +00001517 if (TWFunc::Path_Exists("/file_contexts")) {
1518 Command += " -S /file_contexts";
1519 }
1520 Command += " -a " + Mount_Point + " " + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001521 LOGINFO("make_ext4fs command: %s\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001522 if (TWFunc::Exec_Cmd(Command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001523 Current_File_System = "ext4";
Dees_Troye58d5262012-09-21 12:27:57 -04001524 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001525 gui_print("Done.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001526 return true;
1527 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001528 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001529 return false;
1530 }
1531 } else
Gary Peck43acadf2012-11-21 21:19:01 -08001532 return Wipe_EXT23("ext4");
Dees_Troya95f55c2013-08-17 13:14:43 +00001533#endif
Dees_Troy38bd7602012-09-14 13:33:53 -04001534 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001535}
1536
1537bool TWPartition::Wipe_FAT() {
Vojtech Bocek05534202013-09-11 08:11:56 +02001538 string command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001539
Dees_Troy43d8b002012-09-17 16:00:01 -04001540 if (TWFunc::Path_Exists("/sbin/mkdosfs")) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001541 if (!UnMount(true))
1542 return false;
1543
Dees_Troy2673cec2013-04-02 20:22:16 +00001544 gui_print("Formatting %s using mkdosfs...\n", Display_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001545 Find_Actual_Block_Device();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001546 command = "mkdosfs " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02001547 if (TWFunc::Exec_Cmd(command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001548 Current_File_System = "vfat";
Dees_Troye58d5262012-09-21 12:27:57 -04001549 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001550 gui_print("Done.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001551 return true;
1552 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001553 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001554 return false;
1555 }
1556 return true;
1557 }
1558 else
1559 return Wipe_RMRF();
1560
1561 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001562}
1563
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001564bool TWPartition::Wipe_EXFAT() {
Vojtech Bocek05534202013-09-11 08:11:56 +02001565 string command;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001566
1567 if (TWFunc::Path_Exists("/sbin/mkexfatfs")) {
1568 if (!UnMount(true))
1569 return false;
1570
Dees_Troy2673cec2013-04-02 20:22:16 +00001571 gui_print("Formatting %s using mkexfatfs...\n", Display_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001572 Find_Actual_Block_Device();
1573 command = "mkexfatfs " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02001574 if (TWFunc::Exec_Cmd(command) == 0) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001575 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001576 gui_print("Done.\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001577 return true;
1578 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001579 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001580 return false;
1581 }
1582 return true;
1583 }
1584 return false;
1585}
1586
Dees_Troy38bd7602012-09-14 13:33:53 -04001587bool TWPartition::Wipe_MTD() {
1588 if (!UnMount(true))
1589 return false;
1590
Dees_Troy2673cec2013-04-02 20:22:16 +00001591 gui_print("MTD Formatting \"%s\"\n", MTD_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001592
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001593 mtd_scan_partitions();
1594 const MtdPartition* mtd = mtd_find_partition_by_name(MTD_Name.c_str());
1595 if (mtd == NULL) {
1596 LOGERR("No mtd partition named '%s'", MTD_Name.c_str());
1597 return false;
1598 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001599
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001600 MtdWriteContext* ctx = mtd_write_partition(mtd);
1601 if (ctx == NULL) {
1602 LOGERR("Can't write '%s', failed to format.", MTD_Name.c_str());
1603 return false;
1604 }
1605 if (mtd_erase_blocks(ctx, -1) == -1) {
1606 mtd_write_close(ctx);
1607 LOGERR("Failed to format '%s'", MTD_Name.c_str());
1608 return false;
1609 }
1610 if (mtd_write_close(ctx) != 0) {
1611 LOGERR("Failed to close '%s'", MTD_Name.c_str());
1612 return false;
1613 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001614 Current_File_System = "yaffs2";
Dees_Troye58d5262012-09-21 12:27:57 -04001615 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001616 gui_print("Done.\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001617 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001618}
1619
1620bool TWPartition::Wipe_RMRF() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001621 if (!Mount(true))
1622 return false;
1623
Dees_Troy2673cec2013-04-02 20:22:16 +00001624 gui_print("Removing all files under '%s'\n", Mount_Point.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001625 TWFunc::removeDir(Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04001626 Recreate_AndSec_Folder();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001627 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001628}
1629
Dees_Troye5017042013-08-29 16:38:55 +00001630bool TWPartition::Wipe_F2FS() {
Vojtech Bocek05534202013-09-11 08:11:56 +02001631 string command;
Dees_Troye5017042013-08-29 16:38:55 +00001632
1633 if (TWFunc::Path_Exists("/sbin/mkfs.f2fs")) {
1634 if (!UnMount(true))
1635 return false;
1636
1637 gui_print("Formatting %s using mkfs.f2fs...\n", Display_Name.c_str());
1638 Find_Actual_Block_Device();
1639 command = "mkfs.f2fs " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02001640 if (TWFunc::Exec_Cmd(command) == 0) {
Dees_Troye5017042013-08-29 16:38:55 +00001641 Recreate_AndSec_Folder();
1642 gui_print("Done.\n");
1643 return true;
1644 } else {
1645 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
1646 return false;
1647 }
1648 return true;
1649 } else {
1650 gui_print("mkfs.f2fs binary not found, using rm -rf to wipe.\n");
1651 return Wipe_RMRF();
1652 }
1653 return false;
1654}
1655
Dees_Troy51a0e822012-09-05 15:24:24 -04001656bool TWPartition::Wipe_Data_Without_Wiping_Media() {
Ethan Yonker83e82572014-04-04 10:59:28 -05001657#ifdef TW_OEM_BUILD
1658 // In an OEM Build we want to do a full format
1659 return Wipe_Encryption();
1660#else
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001661 string dir;
bigbiff bigbiff6b600f92014-01-05 18:13:43 -05001662 #ifdef HAVE_SELINUX
1663 fixPermissions perms;
1664 #endif
Dees_Troy38bd7602012-09-14 13:33:53 -04001665
1666 // This handles wiping data on devices with "sdcard" in /data/media
1667 if (!Mount(true))
1668 return false;
1669
Dees_Troy2673cec2013-04-02 20:22:16 +00001670 gui_print("Wiping data without wiping /data/media ...\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001671
1672 DIR* d;
1673 d = opendir("/data");
Dees_Troy16b74352012-11-14 22:27:31 +00001674 if (d != NULL) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001675 struct dirent* de;
1676 while ((de = readdir(d)) != NULL) {
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05001677 if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) continue;
Dees_Troy16b74352012-11-14 22:27:31 +00001678 // The media folder is the "internal sdcard"
1679 // The .layout_version file is responsible for determining whether 4.2 decides up upgrade
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001680 // the media folder for multi-user.
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05001681 //TODO: convert this to use twrpDU.cpp
Dees_Troy16b74352012-11-14 22:27:31 +00001682 if (strcmp(de->d_name, "media") == 0 || strcmp(de->d_name, ".layout_version") == 0) continue;
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05001683
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001684 dir = "/data/";
1685 dir.append(de->d_name);
Dees_Troyce675462013-01-09 19:48:21 +00001686 if (de->d_type == DT_DIR) {
1687 TWFunc::removeDir(dir, false);
bigbiff bigbiff98f1f902013-01-19 18:46:13 -05001688 } 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 +00001689 if (!unlink(dir.c_str()))
Dees_Troy2673cec2013-04-02 20:22:16 +00001690 LOGINFO("Unable to unlink '%s'\n", dir.c_str());
Dees_Troyce675462013-01-09 19:48:21 +00001691 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001692 }
1693 closedir(d);
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05001694
bigbiff bigbiff6b600f92014-01-05 18:13:43 -05001695 #ifdef HAVE_SELINUX
1696 perms.fixDataInternalContexts();
1697 #endif
1698
Dees_Troy2673cec2013-04-02 20:22:16 +00001699 gui_print("Done.\n");
Dees_Troy16b74352012-11-14 22:27:31 +00001700 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -04001701 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001702 gui_print("Dirent failed to open /data, error!\n");
Dees_Troy16b74352012-11-14 22:27:31 +00001703 return false;
Ethan Yonker83e82572014-04-04 10:59:28 -05001704#endif // ifdef TW_OEM_BUILD
Dees_Troy51a0e822012-09-05 15:24:24 -04001705}
1706
1707bool TWPartition::Backup_Tar(string backup_folder) {
Dees_Troy4a2a1262012-09-18 09:33:47 -04001708 char back_name[255], split_index[5];
1709 string Full_FileName, Split_FileName, Tar_Args, Command;
Dees_Troy83bd4832013-05-04 12:39:56 +00001710 int use_compression, use_encryption = 0, index, backup_count;
Dees_Troy4a2a1262012-09-18 09:33:47 -04001711 struct stat st;
Dees_Troy7c2dec82012-09-26 09:49:14 -04001712 unsigned long long total_bsize = 0, file_size;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001713 twrpTar tar;
1714 vector <string> files;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001715
Dees_Troy43d8b002012-09-17 16:00:01 -04001716 if (!Mount(true))
1717 return false;
1718
Dees_Troya13d74f2013-03-24 08:54:55 -05001719 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Backup_Display_Name, "Backing Up");
Dees_Troy2673cec2013-04-02 20:22:16 +00001720 gui_print("Backing up %s...\n", Backup_Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001721
1722 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
Dees_Troy83bd4832013-05-04 12:39:56 +00001723 tar.use_compression = use_compression;
Matt Mowerbb81e5d2014-03-20 18:05:41 -05001724
Dees_Troy83bd4832013-05-04 12:39:56 +00001725#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
1726 DataManager::GetValue("tw_encrypt_backup", use_encryption);
1727 if (use_encryption && Can_Encrypt_Backup) {
1728 tar.use_encryption = use_encryption;
1729 if (Use_Userdata_Encryption)
1730 tar.userdata_encryption = use_encryption;
Ethan Yonker87af5632014-02-10 11:56:35 -06001731 string Password;
1732 DataManager::GetValue("tw_backup_password", Password);
1733 tar.setpassword(Password);
Dees_Troy83bd4832013-05-04 12:39:56 +00001734 } else {
1735 use_encryption = false;
1736 }
1737#endif
Dees_Troy43d8b002012-09-17 16:00:01 -04001738
1739 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1740 Backup_FileName = back_name;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001741 Full_FileName = backup_folder + "/" + Backup_FileName;
Dees_Troy83bd4832013-05-04 12:39:56 +00001742 tar.has_data_media = Has_Data_Media;
Dees Troye0a433a2013-12-02 04:10:37 +00001743 Full_FileName = backup_folder + "/" + Backup_FileName;
1744 tar.setdir(Backup_Path);
1745 tar.setfn(Full_FileName);
1746 tar.setsize(Backup_Size);
1747 if (tar.createTarFork() != 0)
1748 return false;
Dees_Troy43d8b002012-09-17 16:00:01 -04001749 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001750}
1751
1752bool TWPartition::Backup_DD(string backup_folder) {
igoriok87e3d932013-01-31 21:03:53 +02001753 char back_name[255], backup_size[32];
Vojtech Bocek05534202013-09-11 08:11:56 +02001754 string Full_FileName, Command, DD_BS;
Dees_Troy43d8b002012-09-17 16:00:01 -04001755 int use_compression;
1756
igoriok87e3d932013-01-31 21:03:53 +02001757 sprintf(backup_size, "%llu", Backup_Size);
1758 DD_BS = backup_size;
1759
Dees_Troyb46a6842012-09-25 11:06:46 -04001760 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
Dees_Troy2673cec2013-04-02 20:22:16 +00001761 gui_print("Backing up %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001762
1763 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1764 Backup_FileName = back_name;
1765
1766 Full_FileName = backup_folder + "/" + Backup_FileName;
1767
igoriok87e3d932013-01-31 21:03:53 +02001768 Command = "dd if=" + Actual_Block_Device + " of='" + Full_FileName + "'" + " bs=" + DD_BS + "c count=1";
Dees_Troy2673cec2013-04-02 20:22:16 +00001769 LOGINFO("Backup command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001770 TWFunc::Exec_Cmd(Command);
Dees_Troyc154ac22012-10-12 15:36:47 -04001771 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001772 LOGERR("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001773 return false;
1774 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001775 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001776}
1777
1778bool TWPartition::Backup_Dump_Image(string backup_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001779 char back_name[255];
Vojtech Bocek05534202013-09-11 08:11:56 +02001780 string Full_FileName, Command;
Dees_Troy43d8b002012-09-17 16:00:01 -04001781 int use_compression;
1782
Dees_Troyb46a6842012-09-25 11:06:46 -04001783 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
Dees_Troy2673cec2013-04-02 20:22:16 +00001784 gui_print("Backing up %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001785
1786 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1787 Backup_FileName = back_name;
1788
1789 Full_FileName = backup_folder + "/" + Backup_FileName;
1790
1791 Command = "dump_image " + MTD_Name + " '" + Full_FileName + "'";
Dees_Troy2673cec2013-04-02 20:22:16 +00001792 LOGINFO("Backup command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001793 TWFunc::Exec_Cmd(Command);
Dees_Troy7c2dec82012-09-26 09:49:14 -04001794 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
1795 // 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 +00001796 LOGERR("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001797 return false;
1798 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001799 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001800}
1801
Gary Peck43acadf2012-11-21 21:19:01 -08001802bool TWPartition::Restore_Tar(string restore_folder, string Restore_File_System) {
1803 string Full_FileName, Command;
Dees_Troy4a2a1262012-09-18 09:33:47 -04001804 int index = 0;
1805 char split_index[5];
Dees Troy4159aed2014-02-28 17:24:43 +00001806 bool ret = false;
Dees_Troy43d8b002012-09-17 16:00:01 -04001807
Dees_Troye58d5262012-09-21 12:27:57 -04001808 if (Has_Android_Secure) {
Dees_Troye58d5262012-09-21 12:27:57 -04001809 if (!Wipe_AndSec())
1810 return false;
Gary Peck43acadf2012-11-21 21:19:01 -08001811 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001812 gui_print("Wiping %s...\n", Display_Name.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001813 if (!Wipe(Restore_File_System))
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001814 return false;
Dees_Troye58d5262012-09-21 12:27:57 -04001815 }
Dees_Troya13d74f2013-03-24 08:54:55 -05001816 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Backup_Display_Name, "Restoring");
Dees_Troy2673cec2013-04-02 20:22:16 +00001817 gui_print("Restoring %s...\n", Backup_Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001818
1819 if (!Mount(true))
1820 return false;
1821
Dees_Troy4a2a1262012-09-18 09:33:47 -04001822 Full_FileName = restore_folder + "/" + Backup_FileName;
Ethan Yonker87af5632014-02-10 11:56:35 -06001823 twrpTar tar;
1824 tar.setdir(Backup_Path);
1825 tar.setfn(Full_FileName);
1826 tar.backup_name = Backup_Name;
1827#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
1828 string Password;
1829 DataManager::GetValue("tw_restore_password", Password);
1830 if (!Password.empty())
1831 tar.setpassword(Password);
1832#endif
1833 if (tar.extractTarFork() != 0)
Dees Troy4159aed2014-02-28 17:24:43 +00001834 ret = false;
1835 else
1836 ret = true;
1837#ifdef HAVE_CAPABILITIES
1838 // Restore capabilities to the run-as binary
1839 if (Mount_Point == "/system" && Mount(true) && TWFunc::Path_Exists("/system/bin/run-as")) {
1840 struct vfs_cap_data cap_data;
1841 uint64_t capabilities = (1 << CAP_SETUID) | (1 << CAP_SETGID);
1842
1843 memset(&cap_data, 0, sizeof(cap_data));
1844 cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE;
1845 cap_data.data[0].permitted = (uint32_t) (capabilities & 0xffffffff);
1846 cap_data.data[0].inheritable = 0;
1847 cap_data.data[1].permitted = (uint32_t) (capabilities >> 32);
1848 cap_data.data[1].inheritable = 0;
1849 if (setxattr("/system/bin/run-as", XATTR_NAME_CAPS, &cap_data, sizeof(cap_data), 0) < 0) {
1850 LOGINFO("Failed to reset capabilities of /system/bin/run-as binary.\n");
1851 } else {
1852 LOGINFO("Reset capabilities of /system/bin/run-as binary successful.\n");
1853 }
1854 }
1855#endif
1856 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04001857}
1858
1859bool TWPartition::Restore_DD(string restore_folder) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001860 string Full_FileName, Command;
Dees_Troy43d8b002012-09-17 16:00:01 -04001861
Dees_Troyda8b55a2012-12-12 19:18:30 +00001862 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
Dees_Troy43d8b002012-09-17 16:00:01 -04001863 Full_FileName = restore_folder + "/" + Backup_FileName;
Gary Peck15e623d2012-11-21 21:07:58 -08001864
1865 if (!Find_Partition_Size()) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001866 LOGERR("Unable to find partition size for '%s'\n", Mount_Point.c_str());
Gary Peck15e623d2012-11-21 21:07:58 -08001867 return false;
1868 }
1869 unsigned long long backup_size = TWFunc::Get_File_Size(Full_FileName);
1870 if (backup_size > Size) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001871 LOGERR("Size (%iMB) of backup '%s' is larger than target device '%s' (%iMB)\n",
Gary Peck15e623d2012-11-21 21:07:58 -08001872 (int)(backup_size / 1048576LLU), Full_FileName.c_str(),
1873 Actual_Block_Device.c_str(), (int)(Size / 1048576LLU));
1874 return false;
1875 }
1876
Dees_Troy2673cec2013-04-02 20:22:16 +00001877 gui_print("Restoring %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001878 Command = "dd bs=4096 if='" + Full_FileName + "' of=" + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001879 LOGINFO("Restore command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001880 TWFunc::Exec_Cmd(Command);
Dees_Troy43d8b002012-09-17 16:00:01 -04001881 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001882}
1883
1884bool TWPartition::Restore_Flash_Image(string restore_folder) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001885 string Full_FileName, Command;
Dees_Troy43d8b002012-09-17 16:00:01 -04001886
Dees_Troy2673cec2013-04-02 20:22:16 +00001887 gui_print("Restoring %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001888 Full_FileName = restore_folder + "/" + Backup_FileName;
1889 // Sometimes flash image doesn't like to flash due to the first 2KB matching, so we erase first to ensure that it flashes
1890 Command = "erase_image " + MTD_Name;
Dees_Troy2673cec2013-04-02 20:22:16 +00001891 LOGINFO("Erase command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001892 TWFunc::Exec_Cmd(Command);
Dees_Troy43d8b002012-09-17 16:00:01 -04001893 Command = "flash_image " + MTD_Name + " '" + Full_FileName + "'";
Dees_Troy2673cec2013-04-02 20:22:16 +00001894 LOGINFO("Restore command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001895 TWFunc::Exec_Cmd(Command);
Dees_Troy43d8b002012-09-17 16:00:01 -04001896 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001897}
Dees_Troy5bf43922012-09-07 16:07:55 -04001898
1899bool TWPartition::Update_Size(bool Display_Error) {
Dees_Troy0550cfb2012-10-13 11:56:13 -04001900 bool ret = false, Was_Already_Mounted = false;
Dees_Troy51127312012-09-08 13:08:49 -04001901
Dees_Troyab10ee22012-09-21 14:27:30 -04001902 if (!Can_Be_Mounted && !Is_Encrypted)
Dees_Troy5bf43922012-09-07 16:07:55 -04001903 return false;
1904
Dees_Troy0550cfb2012-10-13 11:56:13 -04001905 Was_Already_Mounted = Is_Mounted();
Dees_Troy38bd7602012-09-14 13:33:53 -04001906 if (Removable || Is_Encrypted) {
1907 if (!Mount(false))
1908 return true;
1909 } else if (!Mount(Display_Error))
Dees_Troy5bf43922012-09-07 16:07:55 -04001910 return false;
Dees_Troy51127312012-09-08 13:08:49 -04001911
1912 ret = Get_Size_Via_statfs(Display_Error);
Dees_Troy0550cfb2012-10-13 11:56:13 -04001913 if (!ret || Size == 0) {
1914 if (!Get_Size_Via_df(Display_Error)) {
1915 if (!Was_Already_Mounted)
1916 UnMount(false);
Dees_Troy51127312012-09-08 13:08:49 -04001917 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04001918 }
1919 }
Dees_Troy51127312012-09-08 13:08:49 -04001920
Dees_Troy5bf43922012-09-07 16:07:55 -04001921 if (Has_Data_Media) {
1922 if (Mount(Display_Error)) {
Dees_Troy51127312012-09-08 13:08:49 -04001923 unsigned long long data_media_used, actual_data;
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05001924 Used = du.Get_Folder_Size("/data");
1925 Backup_Size = Used;
1926 int bak = (int)(Used / 1048576LLU);
Dees_Troy51127312012-09-08 13:08:49 -04001927 int fre = (int)(Free / 1048576LLU);
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05001928 LOGINFO("Data backup size is %iMB, free: %iMB.\n", bak, fre);
Dees_Troy0550cfb2012-10-13 11:56:13 -04001929 } else {
1930 if (!Was_Already_Mounted)
1931 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04001932 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04001933 }
Dees_Troye58d5262012-09-21 12:27:57 -04001934 } else if (Has_Android_Secure) {
1935 if (Mount(Display_Error))
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05001936 Backup_Size = du.Get_Folder_Size(Backup_Path);
Dees_Troy0550cfb2012-10-13 11:56:13 -04001937 else {
1938 if (!Was_Already_Mounted)
1939 UnMount(false);
Dees_Troye58d5262012-09-21 12:27:57 -04001940 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04001941 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001942 }
Dees_Troy0550cfb2012-10-13 11:56:13 -04001943 if (!Was_Already_Mounted)
1944 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04001945 return true;
Dees_Troy51127312012-09-08 13:08:49 -04001946}
Dees_Troy38bd7602012-09-14 13:33:53 -04001947
1948void TWPartition::Find_Actual_Block_Device(void) {
1949 if (Is_Decrypted) {
1950 Actual_Block_Device = Decrypted_Block_Device;
Dees_Troy43d8b002012-09-17 16:00:01 -04001951 if (TWFunc::Path_Exists(Primary_Block_Device))
Dees_Troy38bd7602012-09-14 13:33:53 -04001952 Is_Present = true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001953 } else if (TWFunc::Path_Exists(Primary_Block_Device)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001954 Is_Present = true;
1955 Actual_Block_Device = Primary_Block_Device;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001956 return;
1957 }
1958 if (Is_Decrypted) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001959 } else if (!Alternate_Block_Device.empty() && TWFunc::Path_Exists(Alternate_Block_Device)) {
Dees_Troy3f04d032012-10-07 18:20:09 -04001960 Actual_Block_Device = Alternate_Block_Device;
Dees_Troy38bd7602012-09-14 13:33:53 -04001961 Is_Present = true;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001962 } else {
Dees_Troy38bd7602012-09-14 13:33:53 -04001963 Is_Present = false;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001964 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001965}
1966
1967void TWPartition::Recreate_Media_Folder(void) {
1968 string Command;
1969
bigbiff bigbiff6b600f92014-01-05 18:13:43 -05001970 #ifdef HAVE_SELINUX
1971 fixPermissions perms;
1972 #endif
1973
Dees_Troy38bd7602012-09-14 13:33:53 -04001974 if (!Mount(true)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001975 LOGERR("Unable to recreate /data/media folder.\n");
Dees_Troyb46a6842012-09-25 11:06:46 -04001976 } else if (!TWFunc::Path_Exists("/data/media")) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001977 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Dees_Troy2673cec2013-04-02 20:22:16 +00001978 LOGINFO("Recreating /data/media folder.\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001979 mkdir("/data/media", S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
bigbiff bigbiff6b600f92014-01-05 18:13:43 -05001980 #ifdef HAVE_SELINUX
1981 perms.fixDataInternalContexts();
1982 #endif
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001983 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Dees_Troy38bd7602012-09-14 13:33:53 -04001984 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001985}
Dees_Troye58d5262012-09-21 12:27:57 -04001986
1987void TWPartition::Recreate_AndSec_Folder(void) {
Dees_Troye58d5262012-09-21 12:27:57 -04001988 if (!Has_Android_Secure)
1989 return;
Dees_Troy2673cec2013-04-02 20:22:16 +00001990 LOGINFO("Creating %s: %s\n", Backup_Display_Name.c_str(), Symlink_Path.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -04001991 if (!Mount(true)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001992 LOGERR("Unable to recreate %s folder.\n", Backup_Name.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -04001993 } else if (!TWFunc::Path_Exists(Symlink_Path)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001994 LOGINFO("Recreating %s folder.\n", Backup_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001995 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
1996 mkdir(Symlink_Path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
1997 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04001998 }
1999}