blob: 2ebb5c597be25d199bccfdeb29f1d2d3a9ad236b [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;
Dees_Troy63c8df72012-09-10 14:02:05 -0400510 } else if (strcmp(ptr, "canbewiped") == 0) {
511 Can_Be_Wiped = true;
Hashcodedabfd492013-08-29 22:45:30 -0700512 } else if (strcmp(ptr, "usermrf") == 0) {
513 Use_Rm_Rf = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500514 } else if (ptr_len > 7 && strncmp(ptr, "backup=", 7) == 0) {
515 ptr += 7;
516 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y')
517 Can_Be_Backed_Up = true;
518 else
519 Can_Be_Backed_Up = false;
Dees_Troy63c8df72012-09-10 14:02:05 -0400520 } else if (strcmp(ptr, "wipeingui") == 0) {
521 Can_Be_Wiped = true;
522 Wipe_Available_in_GUI = true;
523 } else if (strcmp(ptr, "wipeduringfactoryreset") == 0) {
524 Can_Be_Wiped = true;
525 Wipe_Available_in_GUI = true;
526 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500527 } else if (ptr_len > 15 && strncmp(ptr, "subpartitionof=", 15) == 0) {
Dees_Troy2c4c26f2013-01-28 15:26:43 +0000528 ptr += 15;
Dees_Troy51127312012-09-08 13:08:49 -0400529 Is_SubPartition = true;
530 SubPartition_Of = ptr;
Dees_Troy68cab492012-12-12 19:29:35 +0000531 } else if (strcmp(ptr, "ignoreblkid") == 0) {
532 Ignore_Blkid = true;
Dees_Troy16c2b312013-01-15 16:51:18 +0000533 } else if (strcmp(ptr, "retainlayoutversion") == 0) {
534 Retain_Layout_Version = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500535 } else if (ptr_len > 8 && strncmp(ptr, "symlink=", 8) == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400536 ptr += 8;
537 Symlink_Path = ptr;
Dees_Troya13d74f2013-03-24 08:54:55 -0500538 } else if (ptr_len > 8 && strncmp(ptr, "display=", 8) == 0) {
539 has_display_name = true;
Dees_Troy51127312012-09-08 13:08:49 -0400540 ptr += 8;
Dees_Troya13d74f2013-03-24 08:54:55 -0500541 if (*ptr == '\"') ptr++;
Dees_Troy51127312012-09-08 13:08:49 -0400542 Display_Name = ptr;
Dees_Troya13d74f2013-03-24 08:54:55 -0500543 if (Display_Name.substr(Display_Name.size() - 1, 1) == "\"") {
544 Display_Name.resize(Display_Name.size() - 1);
545 }
546 } else if (ptr_len > 11 && strncmp(ptr, "storagename=", 11) == 0) {
547 has_storage_name = true;
548 ptr += 11;
549 if (*ptr == '\"') ptr++;
550 Storage_Name = ptr;
551 if (Storage_Name.substr(Storage_Name.size() - 1, 1) == "\"") {
552 Storage_Name.resize(Storage_Name.size() - 1);
553 }
554 } else if (ptr_len > 11 && strncmp(ptr, "backupname=", 10) == 0) {
555 has_backup_name = true;
556 ptr += 10;
557 if (*ptr == '\"') ptr++;
558 Backup_Display_Name = ptr;
559 if (Backup_Display_Name.substr(Backup_Display_Name.size() - 1, 1) == "\"") {
560 Backup_Display_Name.resize(Backup_Display_Name.size() - 1);
561 }
562 } else if (ptr_len > 10 && strncmp(ptr, "blocksize=", 10) == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400563 ptr += 10;
564 Format_Block_Size = atoi(ptr);
Dees_Troya13d74f2013-03-24 08:54:55 -0500565 } else if (ptr_len > 7 && strncmp(ptr, "length=", 7) == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400566 ptr += 7;
567 Length = atoi(ptr);
Dees_Troy83bd4832013-05-04 12:39:56 +0000568 } else if (ptr_len > 17 && strncmp(ptr, "canencryptbackup=", 17) == 0) {
569 ptr += 17;
570 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y')
571 Can_Encrypt_Backup = true;
572 else
573 Can_Encrypt_Backup = false;
574 } else if (ptr_len > 21 && strncmp(ptr, "userdataencryptbackup=", 21) == 0) {
575 ptr += 21;
576 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y') {
577 Can_Encrypt_Backup = true;
578 Use_Userdata_Encryption = true;
579 } else {
580 Use_Userdata_Encryption = false;
581 }
Hashcode62bd9e02013-11-19 21:59:42 -0800582 } else if (ptr_len > 8 && strncmp(ptr, "fsflags=", 8) == 0) {
583 ptr += 8;
584 if (*ptr == '\"') ptr++;
585
586 Mount_Options = ptr;
587 if (Mount_Options.substr(Mount_Options.size() - 1, 1) == "\"") {
588 Mount_Options.resize(Mount_Options.size() - 1);
589 }
590 Process_FS_Flags(Mount_Options, Mount_Flags);
Dees_Troy51127312012-09-08 13:08:49 -0400591 } else {
592 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000593 LOGERR("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400594 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000595 LOGINFO("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400596 }
597 while (index < flags_len && flags[index] != '\0')
598 index++;
599 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500600 if (has_display_name && !has_storage_name)
601 Storage_Name = Display_Name;
602 if (!has_display_name && has_storage_name)
603 Display_Name = Storage_Name;
Dees_Troy74fb2e92013-04-15 14:35:47 +0000604 if (has_display_name && !has_backup_name && Backup_Display_Name != "Android Secure")
Dees_Troya13d74f2013-03-24 08:54:55 -0500605 Backup_Display_Name = Display_Name;
606 if (!has_display_name && has_backup_name)
607 Display_Name = Backup_Display_Name;
Dees_Troy51127312012-09-08 13:08:49 -0400608 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400609}
610
Dees_Troy5bf43922012-09-07 16:07:55 -0400611bool TWPartition::Is_File_System(string File_System) {
612 if (File_System == "ext2" ||
Dees_Troy63c8df72012-09-10 14:02:05 -0400613 File_System == "ext3" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400614 File_System == "ext4" ||
615 File_System == "vfat" ||
616 File_System == "ntfs" ||
617 File_System == "yaffs2" ||
bigbiff bigbiff3e146522012-11-14 14:32:59 -0500618 File_System == "exfat" ||
Dees_Troye5017042013-08-29 16:38:55 +0000619 File_System == "f2fs" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400620 File_System == "auto")
621 return true;
622 else
623 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400624}
625
Dees_Troy5bf43922012-09-07 16:07:55 -0400626bool TWPartition::Is_Image(string File_System) {
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400627 if (File_System == "emmc" || File_System == "mtd" || File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -0400628 return true;
629 else
630 return false;
631}
632
Dees_Troy51127312012-09-08 13:08:49 -0400633bool TWPartition::Make_Dir(string Path, bool Display_Error) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400634 if (!TWFunc::Path_Exists(Path)) {
Dees_Troy51127312012-09-08 13:08:49 -0400635 if (mkdir(Path.c_str(), 0777) == -1) {
636 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000637 LOGERR("Can not create '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400638 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000639 LOGINFO("Can not create '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400640 return false;
641 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000642 LOGINFO("Created '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400643 return true;
644 }
645 }
646 return true;
647}
648
Dees_Troy5bf43922012-09-07 16:07:55 -0400649void TWPartition::Setup_File_System(bool Display_Error) {
650 struct statfs st;
651
652 Can_Be_Mounted = true;
653 Can_Be_Wiped = true;
654
Dees_Troy5bf43922012-09-07 16:07:55 -0400655 // Make the mount point folder if it doesn't exist
Dees_Troy51127312012-09-08 13:08:49 -0400656 Make_Dir(Mount_Point, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400657 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
658 Backup_Name = Display_Name;
659 Backup_Method = FILES;
660}
661
662void TWPartition::Setup_Image(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400663 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
664 Backup_Name = Display_Name;
Gary Peck82599a82012-11-21 16:23:12 -0800665 if (Current_File_System == "emmc")
Dees_Troy5bf43922012-09-07 16:07:55 -0400666 Backup_Method = DD;
Gary Peck82599a82012-11-21 16:23:12 -0800667 else if (Current_File_System == "mtd" || Current_File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -0400668 Backup_Method = FLASH_UTILS;
669 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000670 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 -0400671 if (Find_Partition_Size()) {
672 Used = Size;
673 Backup_Size = Size;
Dees_Troy51a0e822012-09-05 15:24:24 -0400674 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400675 if (Display_Error)
Dees Troyd932ce12013-10-18 17:12:59 +0000676 LOGERR("Unable to find partition size for '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400677 else
Dees Troyd932ce12013-10-18 17:12:59 +0000678 LOGINFO("Unable to find partition size for '%s'\n", Mount_Point.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400679 }
680}
681
Dees_Troye58d5262012-09-21 12:27:57 -0400682void TWPartition::Setup_AndSec(void) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500683 Backup_Display_Name = "Android Secure";
Dees_Troye58d5262012-09-21 12:27:57 -0400684 Backup_Name = "and-sec";
Dees_Troya13d74f2013-03-24 08:54:55 -0500685 Can_Be_Backed_Up = true;
Dees_Troye58d5262012-09-21 12:27:57 -0400686 Has_Android_Secure = true;
687 Symlink_Path = Mount_Point + "/.android_secure";
688 Symlink_Mount_Point = "/and-sec";
689 Backup_Path = Symlink_Mount_Point;
690 Make_Dir("/and-sec", true);
691 Recreate_AndSec_Folder();
Ethan Yonkerd4d10732014-02-03 15:27:52 -0600692 Mount_Storage_Retry();
Dees_Troye58d5262012-09-21 12:27:57 -0400693}
694
Dees_Troy5bf43922012-09-07 16:07:55 -0400695void TWPartition::Find_Real_Block_Device(string& Block, bool Display_Error) {
696 char device[512], realDevice[512];
697
698 strcpy(device, Block.c_str());
699 memset(realDevice, 0, sizeof(realDevice));
700 while (readlink(device, realDevice, sizeof(realDevice)) > 0)
701 {
702 strcpy(device, realDevice);
703 memset(realDevice, 0, sizeof(realDevice));
704 }
705
706 if (device[0] != '/') {
707 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000708 LOGERR("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400709 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000710 LOGINFO("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400711 return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400712 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400713 Block = device;
714 return;
715 }
716}
717
Dees_Troy8e337f32012-10-13 22:07:49 -0400718void TWPartition::Mount_Storage_Retry(void) {
719 // On some devices, storage doesn't want to mount right away, retry and sleep
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500720 if (!Mount(true)) {
Dees_Troy8e337f32012-10-13 22:07:49 -0400721 int retry_count = 5;
722 while (retry_count > 0 && !Mount(false)) {
723 usleep(500000);
724 retry_count--;
725 }
726 Mount(true);
727 }
728}
729
Dees_Troy38bd7602012-09-14 13:33:53 -0400730bool TWPartition::Find_MTD_Block_Device(string MTD_Name) {
731 FILE *fp = NULL;
732 char line[255];
733
734 fp = fopen("/proc/mtd", "rt");
735 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000736 LOGERR("Device does not support /proc/mtd\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400737 return false;
738 }
739
740 while (fgets(line, sizeof(line), fp) != NULL)
741 {
742 char device[32], label[32];
743 unsigned long size = 0;
744 char* fstype = NULL;
745 int deviceId;
746
747 sscanf(line, "%s %lx %*s %*c%s", device, &size, label);
748
749 // Skip header and blank lines
750 if ((strcmp(device, "dev:") == 0) || (strlen(line) < 8))
751 continue;
752
753 // Strip off the trailing " from the label
754 label[strlen(label)-1] = '\0';
755
756 if (strcmp(label, MTD_Name.c_str()) == 0) {
757 // We found our device
758 // Strip off the trailing : from the device
759 device[strlen(device)-1] = '\0';
760 if (sscanf(device,"mtd%d", &deviceId) == 1) {
761 sprintf(device, "/dev/block/mtdblock%d", deviceId);
762 Primary_Block_Device = device;
Dees_Troy76543db2013-06-19 16:24:30 +0000763 fclose(fp);
764 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -0400765 }
766 }
767 }
768 fclose(fp);
769
770 return false;
771}
772
Dees_Troy51127312012-09-08 13:08:49 -0400773bool TWPartition::Get_Size_Via_statfs(bool Display_Error) {
774 struct statfs st;
775 string Local_Path = Mount_Point + "/.";
776
777 if (!Mount(Display_Error))
778 return false;
779
780 if (statfs(Local_Path.c_str(), &st) != 0) {
781 if (!Removable) {
782 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000783 LOGERR("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400784 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000785 LOGINFO("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400786 }
787 return false;
788 }
789 Size = (st.f_blocks * st.f_bsize);
790 Used = ((st.f_blocks - st.f_bfree) * st.f_bsize);
791 Free = (st.f_bfree * st.f_bsize);
792 Backup_Size = Used;
793 return true;
794}
795
796bool TWPartition::Get_Size_Via_df(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400797 FILE* fp;
798 char command[255], line[512];
799 int include_block = 1;
800 unsigned int min_len;
801
802 if (!Mount(Display_Error))
803 return false;
804
Dees_Troy38bd7602012-09-14 13:33:53 -0400805 min_len = Actual_Block_Device.size() + 2;
Dees_Troy51127312012-09-08 13:08:49 -0400806 sprintf(command, "df %s > /tmp/dfoutput.txt", Mount_Point.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +0200807 TWFunc::Exec_Cmd(command);
Dees_Troy51127312012-09-08 13:08:49 -0400808 fp = fopen("/tmp/dfoutput.txt", "rt");
809 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000810 LOGINFO("Unable to open /tmp/dfoutput.txt.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400811 return false;
Dees_Troy51127312012-09-08 13:08:49 -0400812 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400813
814 while (fgets(line, sizeof(line), fp) != NULL)
815 {
816 unsigned long blocks, used, available;
817 char device[64];
818 char tmpString[64];
819
820 if (strncmp(line, "Filesystem", 10) == 0)
821 continue;
822 if (strlen(line) < min_len) {
823 include_block = 0;
824 continue;
825 }
826 if (include_block) {
827 sscanf(line, "%s %lu %lu %lu", device, &blocks, &used, &available);
828 } else {
829 // The device block string is so long that the df information is on the next line
830 int space_count = 0;
Dees_Troye58d5262012-09-21 12:27:57 -0400831 sprintf(tmpString, "/dev/block/%s", Actual_Block_Device.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400832 while (tmpString[space_count] == 32)
833 space_count++;
834 sscanf(line + space_count, "%lu %lu %lu", &blocks, &used, &available);
835 }
836
837 // Adjust block size to byte size
838 Size = blocks * 1024ULL;
839 Used = used * 1024ULL;
840 Free = available * 1024ULL;
841 Backup_Size = Used;
842 }
843 fclose(fp);
844 return true;
845}
846
Dees_Troy5bf43922012-09-07 16:07:55 -0400847bool TWPartition::Find_Partition_Size(void) {
848 FILE* fp;
849 char line[512];
850 string tmpdevice;
851
igoriok87e3d932013-01-31 21:03:53 +0200852 fp = fopen("/proc/dumchar_info", "rt");
853 if (fp != NULL) {
854 while (fgets(line, sizeof(line), fp) != NULL)
855 {
856 char label[32], device[32];
857 unsigned long size = 0;
858
859 sscanf(line, "%s %lx %*lx %*lu %s", label, &size, device);
860
bigbiff bigbiff34684ff2013-12-01 21:03:45 -0500861 // Skip header, annotation and blank lines
igoriok87e3d932013-01-31 21:03:53 +0200862 if ((strncmp(device, "/dev/", 5) != 0) || (strlen(line) < 8))
863 continue;
864
865 tmpdevice = "/dev/";
866 tmpdevice += label;
867 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
868 Size = size;
869 fclose(fp);
870 return true;
871 }
872 }
873 }
874
Dees_Troy5bf43922012-09-07 16:07:55 -0400875 // In this case, we'll first get the partitions we care about (with labels)
876 fp = fopen("/proc/partitions", "rt");
877 if (fp == NULL)
878 return false;
879
880 while (fgets(line, sizeof(line), fp) != NULL)
881 {
882 unsigned long major, minor, blocks;
883 char device[512];
884 char tmpString[64];
885
Dees_Troy63c8df72012-09-10 14:02:05 -0400886 if (strlen(line) < 7 || line[0] == 'm') continue;
Dees_Troy5bf43922012-09-07 16:07:55 -0400887 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
888
889 tmpdevice = "/dev/block/";
890 tmpdevice += device;
Dees_Troy38bd7602012-09-14 13:33:53 -0400891 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400892 // Adjust block size to byte size
893 Size = blocks * 1024ULL;
894 fclose(fp);
895 return true;
896 }
897 }
898 fclose(fp);
899 return false;
900}
901
Dees_Troy5bf43922012-09-07 16:07:55 -0400902bool TWPartition::Is_Mounted(void) {
903 if (!Can_Be_Mounted)
904 return false;
905
906 struct stat st1, st2;
907 string test_path;
908
909 // Check to see if the mount point directory exists
910 test_path = Mount_Point + "/.";
911 if (stat(test_path.c_str(), &st1) != 0) return false;
912
913 // Check to see if the directory above the mount point exists
914 test_path = Mount_Point + "/../.";
915 if (stat(test_path.c_str(), &st2) != 0) return false;
916
917 // Compare the device IDs -- if they match then we're (probably) using tmpfs instead of an actual device
918 int ret = (st1.st_dev != st2.st_dev) ? true : false;
919
920 return ret;
921}
922
923bool TWPartition::Mount(bool Display_Error) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000924 int exfat_mounted = 0;
925
Dees_Troy5bf43922012-09-07 16:07:55 -0400926 if (Is_Mounted()) {
927 return true;
928 } else if (!Can_Be_Mounted) {
929 return false;
930 }
Dees_Troy38bd7602012-09-14 13:33:53 -0400931
932 Find_Actual_Block_Device();
933
934 // Check the current file system before mounting
935 Check_FS_Type();
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000936 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/exfat-fuse")) {
Dees_Troye34c1332013-02-06 19:13:00 +0000937 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 +0000938 LOGINFO("cmd: %s\n", cmd.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000939 string result;
940 if (TWFunc::Exec_Cmd(cmd, result) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000941 LOGINFO("exfat-fuse failed to mount with result '%s', trying vfat\n", result.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000942 Current_File_System = "vfat";
943 } else {
944#ifdef TW_NO_EXFAT_FUSE
945 UnMount(false);
946 // We'll let the kernel handle it but using exfat-fuse to detect if the file system is actually exfat
947 // Some kernels let us mount vfat as exfat which doesn't work out too well
948#else
949 exfat_mounted = 1;
950#endif
951 }
952 }
Dees_Troy22042032012-12-18 21:23:08 +0000953 if (Fstab_File_System == "yaffs2") {
954 // mount an MTD partition as a YAFFS2 filesystem.
Dees_Troy76543db2013-06-19 16:24:30 +0000955 const unsigned long flags = MS_NOATIME | MS_NODEV | MS_NODIRATIME;
956 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags, NULL) < 0) {
957 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags | MS_RDONLY, NULL) < 0) {
958 if (Display_Error)
959 LOGERR("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
960 else
961 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
962 return false;
963 } else {
964 LOGINFO("Mounted '%s' (MTD) as RO\n", Mount_Point.c_str());
965 return true;
966 }
967 } else {
968 struct stat st;
969 string test_path = Mount_Point;
970 if (stat(test_path.c_str(), &st) < 0) {
971 if (Display_Error)
972 LOGERR("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
973 else
974 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
975 return false;
976 }
977 mode_t new_mode = st.st_mode | S_IXUSR | S_IXGRP | S_IXOTH;
978 if (new_mode != st.st_mode) {
979 LOGINFO("Fixing execute permissions for %s\n", Mount_Point.c_str());
980 if (chmod(Mount_Point.c_str(), new_mode) < 0) {
981 if (Display_Error)
982 LOGERR("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
983 else
984 LOGINFO("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
985 return false;
986 }
987 }
Dees_Troy22042032012-12-18 21:23:08 +0000988 return true;
Dees_Troy76543db2013-06-19 16:24:30 +0000989 }
Dees Troy216e0422014-02-07 03:46:42 +0000990 } 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 +0000991#ifdef TW_NO_EXFAT_FUSE
992 if (Current_File_System == "exfat") {
Dees_Troy2673cec2013-04-02 20:22:16 +0000993 LOGINFO("Mounting exfat failed, trying vfat...\n");
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000994 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), "vfat", 0, NULL) != 0) {
Dees_Troy85f44ed2013-01-09 18:42:36 +0000995 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000996 LOGERR("Unable to mount '%s'\n", Mount_Point.c_str());
Dees_Troy85f44ed2013-01-09 18:42:36 +0000997 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000998 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
Hashcode62bd9e02013-11-19 21:59:42 -0800999 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 +00001000 return false;
Dees_Troy85f44ed2013-01-09 18:42:36 +00001001 }
Dees_Troyb05ddee2013-01-28 20:24:50 +00001002 } else {
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001003#endif
1004 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001005 LOGERR("Unable to mount '%s'\n", Mount_Point.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001006 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001007 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
1008 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 +00001009 return false;
1010#ifdef TW_NO_EXFAT_FUSE
Dees_Troy85f44ed2013-01-09 18:42:36 +00001011 }
1012#endif
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001013 }
1014#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
1015 string MetaEcfsFile = EXPAND(TW_EXTERNAL_STORAGE_PATH);
1016 MetaEcfsFile += "/.MetaEcfsFile";
1017 if (EcryptFS_Password.size() > 0 && PartitionManager.Mount_By_Path("/data", false) && TWFunc::Path_Exists(MetaEcfsFile)) {
1018 if (mount_ecryptfs_drive(EcryptFS_Password.c_str(), Mount_Point.c_str(), Mount_Point.c_str(), 0) != 0) {
1019 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001020 LOGERR("Unable to mount ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001021 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001022 LOGINFO("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("Successfully mounted ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001025 Is_Decrypted = true;
Dees_Troy51127312012-09-08 13:08:49 -04001026 }
Dees_Troy066eb302013-08-23 17:20:32 +00001027 } else if (Mount_Point == EXPAND(TW_EXTERNAL_STORAGE_PATH)) {
1028 if (Is_Decrypted)
1029 LOGINFO("Mounting external storage, '%s' is not encrypted\n", Mount_Point.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001030 Is_Decrypted = false;
1031 }
1032#endif
1033 if (Removable)
1034 Update_Size(Display_Error);
1035
1036 if (!Symlink_Mount_Point.empty()) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001037 string Command = "mount '" + Symlink_Path + "' '" + Symlink_Mount_Point + "'";
1038 TWFunc::Exec_Cmd(Command);
Dees_Troy5bf43922012-09-07 16:07:55 -04001039 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001040 return true;
1041}
1042
1043bool TWPartition::UnMount(bool Display_Error) {
1044 if (Is_Mounted()) {
1045 int never_unmount_system;
1046
1047 DataManager::GetValue(TW_DONT_UNMOUNT_SYSTEM, never_unmount_system);
1048 if (never_unmount_system == 1 && Mount_Point == "/system")
1049 return true; // Never unmount system if you're not supposed to unmount it
1050
Dees_Troyc8bafa12013-01-10 15:43:00 +00001051#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
1052 if (EcryptFS_Password.size() > 0) {
1053 if (unmount_ecryptfs_drive(Mount_Point.c_str()) != 0) {
1054 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001055 LOGERR("Unable to unmount ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troyc8bafa12013-01-10 15:43:00 +00001056 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001057 LOGINFO("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("Successfully unmounted ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troyc8bafa12013-01-10 15:43:00 +00001060 }
1061 }
1062#endif
1063
Dees_Troy38bd7602012-09-14 13:33:53 -04001064 if (!Symlink_Mount_Point.empty())
1065 umount(Symlink_Mount_Point.c_str());
1066
Dees_Troyb05ddee2013-01-28 20:24:50 +00001067 umount(Mount_Point.c_str());
1068 if (Is_Mounted()) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001069 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001070 LOGERR("Unable to unmount '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001071 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001072 LOGINFO("Unable to unmount '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001073 return false;
1074 } else
1075 return true;
1076 } else {
1077 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001078 }
1079}
1080
Gary Peck43acadf2012-11-21 21:19:01 -08001081bool TWPartition::Wipe(string New_File_System) {
Dees_Troy16c2b312013-01-15 16:51:18 +00001082 bool wiped = false, update_crypt = false;
1083 int check;
1084 string Layout_Filename = Mount_Point + "/.layout_version";
1085
Dees_Troy38bd7602012-09-14 13:33:53 -04001086 if (!Can_Be_Wiped) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001087 LOGERR("Partition '%s' cannot be wiped.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001088 return false;
1089 }
1090
Dees_Troyc51f1f92012-09-20 15:32:13 -04001091 if (Mount_Point == "/cache")
Dees_Troy2673cec2013-04-02 20:22:16 +00001092 Log_Offset = 0;
Dees_Troyc51f1f92012-09-20 15:32:13 -04001093
Dees_Troyce675462013-01-09 19:48:21 +00001094#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
1095 if (Mount_Point == "/data" && Mount(false)) {
1096 if (TWFunc::Path_Exists("/data/system/edk_p_sd"))
1097 TWFunc::copy_file("/data/system/edk_p_sd", "/tmp/edk_p_sd", 0600);
1098 }
1099#endif
1100
Dees_Troy16c2b312013-01-15 16:51:18 +00001101 if (Retain_Layout_Version && Mount(false) && TWFunc::Path_Exists(Layout_Filename))
1102 TWFunc::copy_file(Layout_Filename, "/.layout_version", 0600);
1103 else
1104 unlink("/.layout_version");
Dees_Troy38bd7602012-09-14 13:33:53 -04001105
Dees_Troy16c2b312013-01-15 16:51:18 +00001106 if (Has_Data_Media) {
1107 wiped = Wipe_Data_Without_Wiping_Media();
1108 } else {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001109
Dees_Troy16c2b312013-01-15 16:51:18 +00001110 DataManager::GetValue(TW_RM_RF_VAR, check);
1111
Hashcodedabfd492013-08-29 22:45:30 -07001112 if (check || Use_Rm_Rf)
Dees_Troy16c2b312013-01-15 16:51:18 +00001113 wiped = Wipe_RMRF();
1114 else if (New_File_System == "ext4")
1115 wiped = Wipe_EXT4();
1116 else if (New_File_System == "ext2" || New_File_System == "ext3")
1117 wiped = Wipe_EXT23(New_File_System);
1118 else if (New_File_System == "vfat")
1119 wiped = Wipe_FAT();
1120 else if (New_File_System == "exfat")
1121 wiped = Wipe_EXFAT();
1122 else if (New_File_System == "yaffs2")
1123 wiped = Wipe_MTD();
Dees_Troye5017042013-08-29 16:38:55 +00001124 else if (New_File_System == "f2fs")
1125 wiped = Wipe_F2FS();
Dees_Troy16c2b312013-01-15 16:51:18 +00001126 else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001127 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 +00001128 unlink("/.layout_version");
1129 return false;
1130 }
1131 update_crypt = wiped;
Gary Pecke8bc5d72012-12-21 06:45:25 -08001132 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001133
Gary Pecke8bc5d72012-12-21 06:45:25 -08001134 if (wiped) {
Dees_Troyce675462013-01-09 19:48:21 +00001135#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
1136 if (Mount_Point == "/data" && Mount(false)) {
1137 if (TWFunc::Path_Exists("/tmp/edk_p_sd")) {
1138 Make_Dir("/data/system", true);
1139 TWFunc::copy_file("/tmp/edk_p_sd", "/data/system/edk_p_sd", 0600);
1140 }
1141 }
1142#endif
Dees_Troy16c2b312013-01-15 16:51:18 +00001143
Dees_Troy1c1ac442013-01-17 21:42:14 +00001144 if (Mount_Point == "/cache")
1145 DataManager::Output_Version();
1146
Dees_Troy16c2b312013-01-15 16:51:18 +00001147 if (TWFunc::Path_Exists("/.layout_version") && Mount(false))
1148 TWFunc::copy_file("/.layout_version", Layout_Filename, 0600);
1149
1150 if (update_crypt) {
1151 Setup_File_System(false);
1152 if (Is_Encrypted && !Is_Decrypted) {
1153 // just wiped an encrypted partition back to its unencrypted state
1154 Is_Encrypted = false;
1155 Is_Decrypted = false;
1156 Decrypted_Block_Device = "";
1157 if (Mount_Point == "/data") {
1158 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1159 DataManager::SetValue(TW_IS_DECRYPTED, 0);
1160 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001161 }
1162 }
1163 }
1164 return wiped;
Dees_Troy51a0e822012-09-05 15:24:24 -04001165}
1166
Gary Peck43acadf2012-11-21 21:19:01 -08001167bool TWPartition::Wipe() {
Gary Peck82599a82012-11-21 16:23:12 -08001168 if (Is_File_System(Current_File_System))
1169 return Wipe(Current_File_System);
1170 else
1171 return Wipe(Fstab_File_System);
Gary Peck43acadf2012-11-21 21:19:01 -08001172}
1173
Dees_Troye58d5262012-09-21 12:27:57 -04001174bool TWPartition::Wipe_AndSec(void) {
1175 if (!Has_Android_Secure)
1176 return false;
1177
Dees_Troye58d5262012-09-21 12:27:57 -04001178 if (!Mount(true))
1179 return false;
1180
Dees_Troy2673cec2013-04-02 20:22:16 +00001181 gui_print("Wiping %s\n", Backup_Display_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001182 TWFunc::removeDir(Mount_Point + "/.android_secure/", true);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001183 return true;
Dees_Troye58d5262012-09-21 12:27:57 -04001184}
1185
Dees_Troy51a0e822012-09-05 15:24:24 -04001186bool TWPartition::Backup(string backup_folder) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001187 if (Backup_Method == FILES)
1188 return Backup_Tar(backup_folder);
1189 else if (Backup_Method == DD)
1190 return Backup_DD(backup_folder);
1191 else if (Backup_Method == FLASH_UTILS)
1192 return Backup_Dump_Image(backup_folder);
Dees_Troy2673cec2013-04-02 20:22:16 +00001193 LOGERR("Unknown backup method for '%s'\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001194 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001195}
1196
Dees_Troy43d8b002012-09-17 16:00:01 -04001197bool TWPartition::Check_MD5(string restore_folder) {
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001198 string Full_Filename, md5file;
Dees_Troy43d8b002012-09-17 16:00:01 -04001199 char split_filename[512];
1200 int index = 0;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001201 twrpDigest md5sum;
Dees_Troy43d8b002012-09-17 16:00:01 -04001202
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001203 memset(split_filename, 0, sizeof(split_filename));
Dees_Troy43d8b002012-09-17 16:00:01 -04001204 Full_Filename = restore_folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04001205 if (!TWFunc::Path_Exists(Full_Filename)) {
1206 // This is a split archive, we presume
1207 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
Dees_Troy2673cec2013-04-02 20:22:16 +00001208 LOGINFO("split_filename: %s\n", split_filename);
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001209 md5file = split_filename;
1210 md5file += ".md5";
1211 if (!TWFunc::Path_Exists(md5file)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001212 LOGERR("No md5 file found for '%s'.\n", split_filename);
1213 LOGERR("Please unselect Enable MD5 verification to restore.\n");
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001214 return false;
1215 }
1216 md5sum.setfn(split_filename);
Dees_Troy83bd4832013-05-04 12:39:56 +00001217 while (index < 1000) {
1218 if (TWFunc::Path_Exists(split_filename) && md5sum.verify_md5digest() != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001219 LOGERR("MD5 failed to match on '%s'.\n", split_filename);
Dees_Troy43d8b002012-09-17 16:00:01 -04001220 return false;
1221 }
1222 index++;
1223 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001224 md5sum.setfn(split_filename);
Dees_Troy43d8b002012-09-17 16:00:01 -04001225 }
Dees_Troy4a2a1262012-09-18 09:33:47 -04001226 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001227 } else {
1228 // Single file archive
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001229 md5file = Full_Filename + ".md5";
1230 if (!TWFunc::Path_Exists(md5file)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001231 LOGERR("No md5 file found for '%s'.\n", Full_Filename.c_str());
1232 LOGERR("Please unselect Enable MD5 verification to restore.\n");
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001233 return false;
1234 }
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001235 md5sum.setfn(Full_Filename);
1236 if (md5sum.verify_md5digest() != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001237 LOGERR("MD5 failed to match on '%s'.\n", Full_Filename.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001238 return false;
1239 } else
1240 return true;
1241 }
1242 return false;
1243}
1244
Dees_Troy51a0e822012-09-05 15:24:24 -04001245bool TWPartition::Restore(string restore_folder) {
Gary Peck43acadf2012-11-21 21:19:01 -08001246 size_t first_period, second_period;
1247 string Restore_File_System;
1248
1249 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
Dees_Troy2673cec2013-04-02 20:22:16 +00001250 LOGINFO("Restore filename is: %s\n", Backup_FileName.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001251
1252 // Parse backup filename to extract the file system before wiping
1253 first_period = Backup_FileName.find(".");
1254 if (first_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001255 LOGERR("Unable to find file system (first period).\n");
Gary Peck43acadf2012-11-21 21:19:01 -08001256 return false;
1257 }
1258 Restore_File_System = Backup_FileName.substr(first_period + 1, Backup_FileName.size() - first_period - 1);
1259 second_period = Restore_File_System.find(".");
1260 if (second_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001261 LOGERR("Unable to find file system (second period).\n");
Gary Peck43acadf2012-11-21 21:19:01 -08001262 return false;
1263 }
1264 Restore_File_System.resize(second_period);
Dees_Troy2673cec2013-04-02 20:22:16 +00001265 LOGINFO("Restore file system is: '%s'.\n", Restore_File_System.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001266
1267 if (Is_File_System(Restore_File_System))
1268 return Restore_Tar(restore_folder, Restore_File_System);
1269 else if (Is_Image(Restore_File_System)) {
1270 if (Restore_File_System == "emmc")
1271 return Restore_DD(restore_folder);
1272 else if (Restore_File_System == "mtd" || Restore_File_System == "bml")
1273 return Restore_Flash_Image(restore_folder);
1274 }
1275
Dees_Troy2673cec2013-04-02 20:22:16 +00001276 LOGERR("Unknown restore method for '%s'\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001277 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001278}
1279
1280string TWPartition::Backup_Method_By_Name() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001281 if (Backup_Method == NONE)
1282 return "none";
1283 else if (Backup_Method == FILES)
1284 return "files";
1285 else if (Backup_Method == DD)
1286 return "dd";
1287 else if (Backup_Method == FLASH_UTILS)
1288 return "flash_utils";
1289 else
1290 return "undefined";
1291 return "ERROR!";
Dees_Troy51a0e822012-09-05 15:24:24 -04001292}
1293
1294bool TWPartition::Decrypt(string Password) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001295 LOGINFO("STUB TWPartition::Decrypt, password: '%s'\n", Password.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001296 // Is this needed?
Dees_Troy51a0e822012-09-05 15:24:24 -04001297 return 1;
1298}
1299
1300bool TWPartition::Wipe_Encryption() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001301 bool Save_Data_Media = Has_Data_Media;
1302
1303 if (!UnMount(true))
1304 return false;
1305
Dees_Troy38bd7602012-09-14 13:33:53 -04001306 Has_Data_Media = false;
Dees_Troy74fb2e92013-04-15 14:35:47 +00001307 Decrypted_Block_Device = "";
1308 Is_Decrypted = false;
1309 Is_Encrypted = false;
Gary Pecke8bc5d72012-12-21 06:45:25 -08001310 if (Wipe(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001311 Has_Data_Media = Save_Data_Media;
1312 if (Has_Data_Media && !Symlink_Mount_Point.empty()) {
1313 Recreate_Media_Folder();
1314 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001315 gui_print("You may need to reboot recovery to be able to use /data again.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001316 return true;
1317 } else {
1318 Has_Data_Media = Save_Data_Media;
Dees_Troy2673cec2013-04-02 20:22:16 +00001319 LOGERR("Unable to format to remove encryption.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001320 return false;
1321 }
1322 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001323}
1324
1325void TWPartition::Check_FS_Type() {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001326 const char* type;
1327 blkid_probe pr;
Dees_Troy5bf43922012-09-07 16:07:55 -04001328
Dees_Troy68cab492012-12-12 19:29:35 +00001329 if (Fstab_File_System == "yaffs2" || Fstab_File_System == "mtd" || Fstab_File_System == "bml" || Ignore_Blkid)
1330 return; // Running blkid on some mtd devices causes a massive crash or needs to be skipped
Dees_Troy5bf43922012-09-07 16:07:55 -04001331
Dees_Troy38bd7602012-09-14 13:33:53 -04001332 Find_Actual_Block_Device();
Dees_Troy8170a922012-09-18 15:40:25 -04001333 if (!Is_Present)
1334 return;
Dees_Troy51127312012-09-08 13:08:49 -04001335
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001336 pr = blkid_new_probe_from_filename(Actual_Block_Device.c_str());
1337 if (blkid_do_fullprobe(pr)) {
1338 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00001339 LOGINFO("Can't probe device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001340 return;
Dees_Troy5bf43922012-09-07 16:07:55 -04001341 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001342
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001343 if (blkid_probe_lookup_value(pr, "TYPE", &type, NULL) < 0) {
1344 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00001345 LOGINFO("can't find filesystem on device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001346 return;
1347 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001348
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001349 Current_File_System = type;
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001350 blkid_free_probe(pr);
Dees_Troy51a0e822012-09-05 15:24:24 -04001351}
1352
Gary Peck43acadf2012-11-21 21:19:01 -08001353bool TWPartition::Wipe_EXT23(string File_System) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001354 if (!UnMount(true))
1355 return false;
1356
Dees_Troy43d8b002012-09-17 16:00:01 -04001357 if (TWFunc::Path_Exists("/sbin/mke2fs")) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001358 string command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001359
Dees_Troy2673cec2013-04-02 20:22:16 +00001360 gui_print("Formatting %s using mke2fs...\n", Display_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001361 Find_Actual_Block_Device();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001362 command = "mke2fs -t " + File_System + " -m 0 " + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001363 LOGINFO("mke2fs command: %s\n", command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001364 if (TWFunc::Exec_Cmd(command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001365 Current_File_System = File_System;
Dees_Troye58d5262012-09-21 12:27:57 -04001366 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001367 gui_print("Done.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001368 return true;
1369 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001370 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001371 return false;
1372 }
1373 } else
1374 return Wipe_RMRF();
1375
1376 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001377}
1378
1379bool TWPartition::Wipe_EXT4() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001380 if (!UnMount(true))
1381 return false;
1382
Dees_Troyb3265ab2013-08-30 02:59:14 +00001383#if defined(HAVE_SELINUX) && defined(USE_EXT4)
Ethan Yonkerf27497f2014-02-09 11:48:33 -06001384 int ret;
1385 char *secontext = NULL;
1386
Dees_Troya95f55c2013-08-17 13:14:43 +00001387 gui_print("Formatting %s using make_ext4fs function.\n", Display_Name.c_str());
Ethan Yonkerf27497f2014-02-09 11:48:33 -06001388
Dees Troy99c8dbf2014-03-10 16:53:58 +00001389 if (!selinux_handle || selabel_lookup(selinux_handle, &secontext, Mount_Point.c_str(), S_IFDIR) < 0) {
Ethan Yonkerf27497f2014-02-09 11:48:33 -06001390 LOGINFO("Cannot lookup security context for '%s'\n", Mount_Point.c_str());
1391 ret = make_ext4fs(Actual_Block_Device.c_str(), Length, Mount_Point.c_str(), NULL);
1392 } else {
1393 ret = make_ext4fs(Actual_Block_Device.c_str(), Length, Mount_Point.c_str(), selinux_handle);
1394 }
1395 if (ret != 0) {
Dees_Troya95f55c2013-08-17 13:14:43 +00001396 LOGERR("Unable to wipe '%s' using function call.\n", Mount_Point.c_str());
1397 return false;
1398 } else {
bigbiff bigbiffc49d7062013-10-11 20:28:00 -04001399 string sedir = Mount_Point + "/lost+found";
1400 PartitionManager.Mount_By_Path(sedir.c_str(), true);
1401 rmdir(sedir.c_str());
1402 mkdir(sedir.c_str(), S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP);
Dees_Troya95f55c2013-08-17 13:14:43 +00001403 return true;
1404 }
1405#else
Dees_Troy43d8b002012-09-17 16:00:01 -04001406 if (TWFunc::Path_Exists("/sbin/make_ext4fs")) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001407 string Command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001408
Dees_Troy2673cec2013-04-02 20:22:16 +00001409 gui_print("Formatting %s using make_ext4fs...\n", Display_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001410 Find_Actual_Block_Device();
1411 Command = "make_ext4fs";
1412 if (!Is_Decrypted && Length != 0) {
1413 // Only use length if we're not decrypted
1414 char len[32];
1415 sprintf(len, "%i", Length);
1416 Command += " -l ";
1417 Command += len;
1418 }
Dees_Troy5295d582013-09-06 15:51:08 +00001419 if (TWFunc::Path_Exists("/file_contexts")) {
1420 Command += " -S /file_contexts";
1421 }
1422 Command += " -a " + Mount_Point + " " + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001423 LOGINFO("make_ext4fs command: %s\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001424 if (TWFunc::Exec_Cmd(Command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001425 Current_File_System = "ext4";
Dees_Troye58d5262012-09-21 12:27:57 -04001426 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001427 gui_print("Done.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001428 return true;
1429 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001430 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001431 return false;
1432 }
1433 } else
Gary Peck43acadf2012-11-21 21:19:01 -08001434 return Wipe_EXT23("ext4");
Dees_Troya95f55c2013-08-17 13:14:43 +00001435#endif
Dees_Troy38bd7602012-09-14 13:33:53 -04001436 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001437}
1438
1439bool TWPartition::Wipe_FAT() {
Vojtech Bocek05534202013-09-11 08:11:56 +02001440 string command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001441
Dees_Troy43d8b002012-09-17 16:00:01 -04001442 if (TWFunc::Path_Exists("/sbin/mkdosfs")) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001443 if (!UnMount(true))
1444 return false;
1445
Dees_Troy2673cec2013-04-02 20:22:16 +00001446 gui_print("Formatting %s using mkdosfs...\n", Display_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001447 Find_Actual_Block_Device();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001448 command = "mkdosfs " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02001449 if (TWFunc::Exec_Cmd(command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001450 Current_File_System = "vfat";
Dees_Troye58d5262012-09-21 12:27:57 -04001451 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001452 gui_print("Done.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001453 return true;
1454 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001455 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001456 return false;
1457 }
1458 return true;
1459 }
1460 else
1461 return Wipe_RMRF();
1462
1463 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001464}
1465
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001466bool TWPartition::Wipe_EXFAT() {
Vojtech Bocek05534202013-09-11 08:11:56 +02001467 string command;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001468
1469 if (TWFunc::Path_Exists("/sbin/mkexfatfs")) {
1470 if (!UnMount(true))
1471 return false;
1472
Dees_Troy2673cec2013-04-02 20:22:16 +00001473 gui_print("Formatting %s using mkexfatfs...\n", Display_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001474 Find_Actual_Block_Device();
1475 command = "mkexfatfs " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02001476 if (TWFunc::Exec_Cmd(command) == 0) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001477 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001478 gui_print("Done.\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001479 return true;
1480 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001481 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001482 return false;
1483 }
1484 return true;
1485 }
1486 return false;
1487}
1488
Dees_Troy38bd7602012-09-14 13:33:53 -04001489bool TWPartition::Wipe_MTD() {
1490 if (!UnMount(true))
1491 return false;
1492
Dees_Troy2673cec2013-04-02 20:22:16 +00001493 gui_print("MTD Formatting \"%s\"\n", MTD_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001494
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001495 mtd_scan_partitions();
1496 const MtdPartition* mtd = mtd_find_partition_by_name(MTD_Name.c_str());
1497 if (mtd == NULL) {
1498 LOGERR("No mtd partition named '%s'", MTD_Name.c_str());
1499 return false;
1500 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001501
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001502 MtdWriteContext* ctx = mtd_write_partition(mtd);
1503 if (ctx == NULL) {
1504 LOGERR("Can't write '%s', failed to format.", MTD_Name.c_str());
1505 return false;
1506 }
1507 if (mtd_erase_blocks(ctx, -1) == -1) {
1508 mtd_write_close(ctx);
1509 LOGERR("Failed to format '%s'", MTD_Name.c_str());
1510 return false;
1511 }
1512 if (mtd_write_close(ctx) != 0) {
1513 LOGERR("Failed to close '%s'", MTD_Name.c_str());
1514 return false;
1515 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001516 Current_File_System = "yaffs2";
Dees_Troye58d5262012-09-21 12:27:57 -04001517 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001518 gui_print("Done.\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001519 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001520}
1521
1522bool TWPartition::Wipe_RMRF() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001523 if (!Mount(true))
1524 return false;
1525
Dees_Troy2673cec2013-04-02 20:22:16 +00001526 gui_print("Removing all files under '%s'\n", Mount_Point.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001527 TWFunc::removeDir(Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04001528 Recreate_AndSec_Folder();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001529 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001530}
1531
Dees_Troye5017042013-08-29 16:38:55 +00001532bool TWPartition::Wipe_F2FS() {
Vojtech Bocek05534202013-09-11 08:11:56 +02001533 string command;
Dees_Troye5017042013-08-29 16:38:55 +00001534
1535 if (TWFunc::Path_Exists("/sbin/mkfs.f2fs")) {
1536 if (!UnMount(true))
1537 return false;
1538
1539 gui_print("Formatting %s using mkfs.f2fs...\n", Display_Name.c_str());
1540 Find_Actual_Block_Device();
1541 command = "mkfs.f2fs " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02001542 if (TWFunc::Exec_Cmd(command) == 0) {
Dees_Troye5017042013-08-29 16:38:55 +00001543 Recreate_AndSec_Folder();
1544 gui_print("Done.\n");
1545 return true;
1546 } else {
1547 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
1548 return false;
1549 }
1550 return true;
1551 } else {
1552 gui_print("mkfs.f2fs binary not found, using rm -rf to wipe.\n");
1553 return Wipe_RMRF();
1554 }
1555 return false;
1556}
1557
Dees_Troy51a0e822012-09-05 15:24:24 -04001558bool TWPartition::Wipe_Data_Without_Wiping_Media() {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001559 string dir;
bigbiff bigbiff6b600f92014-01-05 18:13:43 -05001560 #ifdef HAVE_SELINUX
1561 fixPermissions perms;
1562 #endif
Dees_Troy38bd7602012-09-14 13:33:53 -04001563
1564 // This handles wiping data on devices with "sdcard" in /data/media
1565 if (!Mount(true))
1566 return false;
1567
Dees_Troy2673cec2013-04-02 20:22:16 +00001568 gui_print("Wiping data without wiping /data/media ...\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001569
1570 DIR* d;
1571 d = opendir("/data");
Dees_Troy16b74352012-11-14 22:27:31 +00001572 if (d != NULL) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001573 struct dirent* de;
1574 while ((de = readdir(d)) != NULL) {
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05001575 if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) continue;
Dees_Troy16b74352012-11-14 22:27:31 +00001576 // The media folder is the "internal sdcard"
1577 // The .layout_version file is responsible for determining whether 4.2 decides up upgrade
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001578 // the media folder for multi-user.
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05001579 //TODO: convert this to use twrpDU.cpp
Dees_Troy16b74352012-11-14 22:27:31 +00001580 if (strcmp(de->d_name, "media") == 0 || strcmp(de->d_name, ".layout_version") == 0) continue;
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05001581
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001582 dir = "/data/";
1583 dir.append(de->d_name);
Dees_Troyce675462013-01-09 19:48:21 +00001584 if (de->d_type == DT_DIR) {
1585 TWFunc::removeDir(dir, false);
bigbiff bigbiff98f1f902013-01-19 18:46:13 -05001586 } 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 +00001587 if (!unlink(dir.c_str()))
Dees_Troy2673cec2013-04-02 20:22:16 +00001588 LOGINFO("Unable to unlink '%s'\n", dir.c_str());
Dees_Troyce675462013-01-09 19:48:21 +00001589 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001590 }
1591 closedir(d);
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05001592
bigbiff bigbiff6b600f92014-01-05 18:13:43 -05001593 #ifdef HAVE_SELINUX
1594 perms.fixDataInternalContexts();
1595 #endif
1596
Dees_Troy2673cec2013-04-02 20:22:16 +00001597 gui_print("Done.\n");
Dees_Troy16b74352012-11-14 22:27:31 +00001598 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -04001599 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001600 gui_print("Dirent failed to open /data, error!\n");
Dees_Troy16b74352012-11-14 22:27:31 +00001601 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001602}
1603
1604bool TWPartition::Backup_Tar(string backup_folder) {
Dees_Troy4a2a1262012-09-18 09:33:47 -04001605 char back_name[255], split_index[5];
1606 string Full_FileName, Split_FileName, Tar_Args, Command;
Dees_Troy83bd4832013-05-04 12:39:56 +00001607 int use_compression, use_encryption = 0, index, backup_count;
Dees_Troy4a2a1262012-09-18 09:33:47 -04001608 struct stat st;
Dees_Troy7c2dec82012-09-26 09:49:14 -04001609 unsigned long long total_bsize = 0, file_size;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001610 twrpTar tar;
1611 vector <string> files;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001612
Dees_Troy43d8b002012-09-17 16:00:01 -04001613 if (!Mount(true))
1614 return false;
1615
Dees_Troya13d74f2013-03-24 08:54:55 -05001616 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Backup_Display_Name, "Backing Up");
Dees_Troy2673cec2013-04-02 20:22:16 +00001617 gui_print("Backing up %s...\n", Backup_Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001618
1619 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
Dees_Troy83bd4832013-05-04 12:39:56 +00001620 tar.use_compression = use_compression;
Matt Mowerbb81e5d2014-03-20 18:05:41 -05001621
Dees_Troy83bd4832013-05-04 12:39:56 +00001622#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
1623 DataManager::GetValue("tw_encrypt_backup", use_encryption);
1624 if (use_encryption && Can_Encrypt_Backup) {
1625 tar.use_encryption = use_encryption;
1626 if (Use_Userdata_Encryption)
1627 tar.userdata_encryption = use_encryption;
Ethan Yonker87af5632014-02-10 11:56:35 -06001628 string Password;
1629 DataManager::GetValue("tw_backup_password", Password);
1630 tar.setpassword(Password);
Dees_Troy83bd4832013-05-04 12:39:56 +00001631 } else {
1632 use_encryption = false;
1633 }
1634#endif
Dees_Troy43d8b002012-09-17 16:00:01 -04001635
1636 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1637 Backup_FileName = back_name;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001638 Full_FileName = backup_folder + "/" + Backup_FileName;
Dees_Troy83bd4832013-05-04 12:39:56 +00001639 tar.has_data_media = Has_Data_Media;
Dees Troye0a433a2013-12-02 04:10:37 +00001640 Full_FileName = backup_folder + "/" + Backup_FileName;
1641 tar.setdir(Backup_Path);
1642 tar.setfn(Full_FileName);
1643 tar.setsize(Backup_Size);
1644 if (tar.createTarFork() != 0)
1645 return false;
Dees_Troy43d8b002012-09-17 16:00:01 -04001646 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001647}
1648
1649bool TWPartition::Backup_DD(string backup_folder) {
igoriok87e3d932013-01-31 21:03:53 +02001650 char back_name[255], backup_size[32];
Vojtech Bocek05534202013-09-11 08:11:56 +02001651 string Full_FileName, Command, DD_BS;
Dees_Troy43d8b002012-09-17 16:00:01 -04001652 int use_compression;
1653
igoriok87e3d932013-01-31 21:03:53 +02001654 sprintf(backup_size, "%llu", Backup_Size);
1655 DD_BS = backup_size;
1656
Dees_Troyb46a6842012-09-25 11:06:46 -04001657 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
Dees_Troy2673cec2013-04-02 20:22:16 +00001658 gui_print("Backing up %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001659
1660 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1661 Backup_FileName = back_name;
1662
1663 Full_FileName = backup_folder + "/" + Backup_FileName;
1664
igoriok87e3d932013-01-31 21:03:53 +02001665 Command = "dd if=" + Actual_Block_Device + " of='" + Full_FileName + "'" + " bs=" + DD_BS + "c count=1";
Dees_Troy2673cec2013-04-02 20:22:16 +00001666 LOGINFO("Backup command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001667 TWFunc::Exec_Cmd(Command);
Dees_Troyc154ac22012-10-12 15:36:47 -04001668 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001669 LOGERR("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001670 return false;
1671 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001672 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001673}
1674
1675bool TWPartition::Backup_Dump_Image(string backup_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001676 char back_name[255];
Vojtech Bocek05534202013-09-11 08:11:56 +02001677 string Full_FileName, Command;
Dees_Troy43d8b002012-09-17 16:00:01 -04001678 int use_compression;
1679
Dees_Troyb46a6842012-09-25 11:06:46 -04001680 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
Dees_Troy2673cec2013-04-02 20:22:16 +00001681 gui_print("Backing up %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001682
1683 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1684 Backup_FileName = back_name;
1685
1686 Full_FileName = backup_folder + "/" + Backup_FileName;
1687
1688 Command = "dump_image " + MTD_Name + " '" + Full_FileName + "'";
Dees_Troy2673cec2013-04-02 20:22:16 +00001689 LOGINFO("Backup command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001690 TWFunc::Exec_Cmd(Command);
Dees_Troy7c2dec82012-09-26 09:49:14 -04001691 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
1692 // 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 +00001693 LOGERR("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001694 return false;
1695 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001696 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001697}
1698
Gary Peck43acadf2012-11-21 21:19:01 -08001699bool TWPartition::Restore_Tar(string restore_folder, string Restore_File_System) {
1700 string Full_FileName, Command;
Dees_Troy4a2a1262012-09-18 09:33:47 -04001701 int index = 0;
1702 char split_index[5];
Dees Troy4159aed2014-02-28 17:24:43 +00001703 bool ret = false;
Dees_Troy43d8b002012-09-17 16:00:01 -04001704
Dees_Troye58d5262012-09-21 12:27:57 -04001705 if (Has_Android_Secure) {
Dees_Troye58d5262012-09-21 12:27:57 -04001706 if (!Wipe_AndSec())
1707 return false;
Gary Peck43acadf2012-11-21 21:19:01 -08001708 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001709 gui_print("Wiping %s...\n", Display_Name.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001710 if (!Wipe(Restore_File_System))
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001711 return false;
Dees_Troye58d5262012-09-21 12:27:57 -04001712 }
Dees_Troya13d74f2013-03-24 08:54:55 -05001713 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Backup_Display_Name, "Restoring");
Dees_Troy2673cec2013-04-02 20:22:16 +00001714 gui_print("Restoring %s...\n", Backup_Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001715
1716 if (!Mount(true))
1717 return false;
1718
Dees_Troy4a2a1262012-09-18 09:33:47 -04001719 Full_FileName = restore_folder + "/" + Backup_FileName;
Ethan Yonker87af5632014-02-10 11:56:35 -06001720 twrpTar tar;
1721 tar.setdir(Backup_Path);
1722 tar.setfn(Full_FileName);
1723 tar.backup_name = Backup_Name;
1724#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
1725 string Password;
1726 DataManager::GetValue("tw_restore_password", Password);
1727 if (!Password.empty())
1728 tar.setpassword(Password);
1729#endif
1730 if (tar.extractTarFork() != 0)
Dees Troy4159aed2014-02-28 17:24:43 +00001731 ret = false;
1732 else
1733 ret = true;
1734#ifdef HAVE_CAPABILITIES
1735 // Restore capabilities to the run-as binary
1736 if (Mount_Point == "/system" && Mount(true) && TWFunc::Path_Exists("/system/bin/run-as")) {
1737 struct vfs_cap_data cap_data;
1738 uint64_t capabilities = (1 << CAP_SETUID) | (1 << CAP_SETGID);
1739
1740 memset(&cap_data, 0, sizeof(cap_data));
1741 cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE;
1742 cap_data.data[0].permitted = (uint32_t) (capabilities & 0xffffffff);
1743 cap_data.data[0].inheritable = 0;
1744 cap_data.data[1].permitted = (uint32_t) (capabilities >> 32);
1745 cap_data.data[1].inheritable = 0;
1746 if (setxattr("/system/bin/run-as", XATTR_NAME_CAPS, &cap_data, sizeof(cap_data), 0) < 0) {
1747 LOGINFO("Failed to reset capabilities of /system/bin/run-as binary.\n");
1748 } else {
1749 LOGINFO("Reset capabilities of /system/bin/run-as binary successful.\n");
1750 }
1751 }
1752#endif
1753 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04001754}
1755
1756bool TWPartition::Restore_DD(string restore_folder) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001757 string Full_FileName, Command;
Dees_Troy43d8b002012-09-17 16:00:01 -04001758
Dees_Troyda8b55a2012-12-12 19:18:30 +00001759 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
Dees_Troy43d8b002012-09-17 16:00:01 -04001760 Full_FileName = restore_folder + "/" + Backup_FileName;
Gary Peck15e623d2012-11-21 21:07:58 -08001761
1762 if (!Find_Partition_Size()) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001763 LOGERR("Unable to find partition size for '%s'\n", Mount_Point.c_str());
Gary Peck15e623d2012-11-21 21:07:58 -08001764 return false;
1765 }
1766 unsigned long long backup_size = TWFunc::Get_File_Size(Full_FileName);
1767 if (backup_size > Size) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001768 LOGERR("Size (%iMB) of backup '%s' is larger than target device '%s' (%iMB)\n",
Gary Peck15e623d2012-11-21 21:07:58 -08001769 (int)(backup_size / 1048576LLU), Full_FileName.c_str(),
1770 Actual_Block_Device.c_str(), (int)(Size / 1048576LLU));
1771 return false;
1772 }
1773
Dees_Troy2673cec2013-04-02 20:22:16 +00001774 gui_print("Restoring %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001775 Command = "dd bs=4096 if='" + Full_FileName + "' of=" + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001776 LOGINFO("Restore command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001777 TWFunc::Exec_Cmd(Command);
Dees_Troy43d8b002012-09-17 16:00:01 -04001778 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001779}
1780
1781bool TWPartition::Restore_Flash_Image(string restore_folder) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001782 string Full_FileName, Command;
Dees_Troy43d8b002012-09-17 16:00:01 -04001783
Dees_Troy2673cec2013-04-02 20:22:16 +00001784 gui_print("Restoring %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001785 Full_FileName = restore_folder + "/" + Backup_FileName;
1786 // Sometimes flash image doesn't like to flash due to the first 2KB matching, so we erase first to ensure that it flashes
1787 Command = "erase_image " + MTD_Name;
Dees_Troy2673cec2013-04-02 20:22:16 +00001788 LOGINFO("Erase command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001789 TWFunc::Exec_Cmd(Command);
Dees_Troy43d8b002012-09-17 16:00:01 -04001790 Command = "flash_image " + MTD_Name + " '" + Full_FileName + "'";
Dees_Troy2673cec2013-04-02 20:22:16 +00001791 LOGINFO("Restore command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001792 TWFunc::Exec_Cmd(Command);
Dees_Troy43d8b002012-09-17 16:00:01 -04001793 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001794}
Dees_Troy5bf43922012-09-07 16:07:55 -04001795
1796bool TWPartition::Update_Size(bool Display_Error) {
Dees_Troy0550cfb2012-10-13 11:56:13 -04001797 bool ret = false, Was_Already_Mounted = false;
Dees_Troy51127312012-09-08 13:08:49 -04001798
Dees_Troyab10ee22012-09-21 14:27:30 -04001799 if (!Can_Be_Mounted && !Is_Encrypted)
Dees_Troy5bf43922012-09-07 16:07:55 -04001800 return false;
1801
Dees_Troy0550cfb2012-10-13 11:56:13 -04001802 Was_Already_Mounted = Is_Mounted();
Dees_Troy38bd7602012-09-14 13:33:53 -04001803 if (Removable || Is_Encrypted) {
1804 if (!Mount(false))
1805 return true;
1806 } else if (!Mount(Display_Error))
Dees_Troy5bf43922012-09-07 16:07:55 -04001807 return false;
Dees_Troy51127312012-09-08 13:08:49 -04001808
1809 ret = Get_Size_Via_statfs(Display_Error);
Dees_Troy0550cfb2012-10-13 11:56:13 -04001810 if (!ret || Size == 0) {
1811 if (!Get_Size_Via_df(Display_Error)) {
1812 if (!Was_Already_Mounted)
1813 UnMount(false);
Dees_Troy51127312012-09-08 13:08:49 -04001814 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04001815 }
1816 }
Dees_Troy51127312012-09-08 13:08:49 -04001817
Dees_Troy5bf43922012-09-07 16:07:55 -04001818 if (Has_Data_Media) {
1819 if (Mount(Display_Error)) {
Dees_Troy51127312012-09-08 13:08:49 -04001820 unsigned long long data_media_used, actual_data;
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05001821 Used = du.Get_Folder_Size("/data");
1822 Backup_Size = Used;
1823 int bak = (int)(Used / 1048576LLU);
Dees_Troy51127312012-09-08 13:08:49 -04001824 int fre = (int)(Free / 1048576LLU);
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05001825 LOGINFO("Data backup size is %iMB, free: %iMB.\n", bak, fre);
Dees_Troy0550cfb2012-10-13 11:56:13 -04001826 } else {
1827 if (!Was_Already_Mounted)
1828 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04001829 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04001830 }
Dees_Troye58d5262012-09-21 12:27:57 -04001831 } else if (Has_Android_Secure) {
1832 if (Mount(Display_Error))
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05001833 Backup_Size = du.Get_Folder_Size(Backup_Path);
Dees_Troy0550cfb2012-10-13 11:56:13 -04001834 else {
1835 if (!Was_Already_Mounted)
1836 UnMount(false);
Dees_Troye58d5262012-09-21 12:27:57 -04001837 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04001838 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001839 }
Dees_Troy0550cfb2012-10-13 11:56:13 -04001840 if (!Was_Already_Mounted)
1841 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04001842 return true;
Dees_Troy51127312012-09-08 13:08:49 -04001843}
Dees_Troy38bd7602012-09-14 13:33:53 -04001844
1845void TWPartition::Find_Actual_Block_Device(void) {
1846 if (Is_Decrypted) {
1847 Actual_Block_Device = Decrypted_Block_Device;
Dees_Troy43d8b002012-09-17 16:00:01 -04001848 if (TWFunc::Path_Exists(Primary_Block_Device))
Dees_Troy38bd7602012-09-14 13:33:53 -04001849 Is_Present = true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001850 } else if (TWFunc::Path_Exists(Primary_Block_Device)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001851 Is_Present = true;
1852 Actual_Block_Device = Primary_Block_Device;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001853 return;
1854 }
1855 if (Is_Decrypted) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001856 } else if (!Alternate_Block_Device.empty() && TWFunc::Path_Exists(Alternate_Block_Device)) {
Dees_Troy3f04d032012-10-07 18:20:09 -04001857 Actual_Block_Device = Alternate_Block_Device;
Dees_Troy38bd7602012-09-14 13:33:53 -04001858 Is_Present = true;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001859 } else {
Dees_Troy38bd7602012-09-14 13:33:53 -04001860 Is_Present = false;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001861 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001862}
1863
1864void TWPartition::Recreate_Media_Folder(void) {
1865 string Command;
1866
bigbiff bigbiff6b600f92014-01-05 18:13:43 -05001867 #ifdef HAVE_SELINUX
1868 fixPermissions perms;
1869 #endif
1870
Dees_Troy38bd7602012-09-14 13:33:53 -04001871 if (!Mount(true)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001872 LOGERR("Unable to recreate /data/media folder.\n");
Dees_Troyb46a6842012-09-25 11:06:46 -04001873 } else if (!TWFunc::Path_Exists("/data/media")) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001874 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Dees_Troy2673cec2013-04-02 20:22:16 +00001875 LOGINFO("Recreating /data/media folder.\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001876 mkdir("/data/media", S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
bigbiff bigbiff6b600f92014-01-05 18:13:43 -05001877 #ifdef HAVE_SELINUX
1878 perms.fixDataInternalContexts();
1879 #endif
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001880 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Dees_Troy38bd7602012-09-14 13:33:53 -04001881 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001882}
Dees_Troye58d5262012-09-21 12:27:57 -04001883
1884void TWPartition::Recreate_AndSec_Folder(void) {
Dees_Troye58d5262012-09-21 12:27:57 -04001885 if (!Has_Android_Secure)
1886 return;
Dees_Troy2673cec2013-04-02 20:22:16 +00001887 LOGINFO("Creating %s: %s\n", Backup_Display_Name.c_str(), Symlink_Path.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -04001888 if (!Mount(true)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001889 LOGERR("Unable to recreate %s folder.\n", Backup_Name.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -04001890 } else if (!TWFunc::Path_Exists(Symlink_Path)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001891 LOGINFO("Recreating %s folder.\n", Backup_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001892 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
1893 mkdir(Symlink_Path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
1894 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04001895 }
1896}