blob: 57a7d8de5cefac7d443d9b5160c556778306ad9f [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
Dees_Troy5bf43922012-09-07 16:07:55 -040053}
bigbiff bigbiffc49d7062013-10-11 20:28:00 -040054#ifdef HAVE_SELINUX
55#include "selinux/selinux.h"
56#endif
Dees_Troy51a0e822012-09-05 15:24:24 -040057
bigbiff bigbiff9c754052013-01-09 09:09:08 -050058using namespace std;
59
Dees_Troya95f55c2013-08-17 13:14:43 +000060extern struct selabel_handle *selinux_handle;
61
Hashcode62bd9e02013-11-19 21:59:42 -080062struct flag_list {
63 const char *name;
64 unsigned flag;
65};
66
67static struct flag_list mount_flags[] = {
68 { "noatime", MS_NOATIME },
69 { "noexec", MS_NOEXEC },
70 { "nosuid", MS_NOSUID },
71 { "nodev", MS_NODEV },
72 { "nodiratime", MS_NODIRATIME },
73 { "ro", MS_RDONLY },
74 { "rw", 0 },
75 { "remount", MS_REMOUNT },
76 { "bind", MS_BIND },
77 { "rec", MS_REC },
Dees Troyc4bc30e2014-02-03 15:04:19 +000078#ifdef MS_UNBINDABLE
Hashcode62bd9e02013-11-19 21:59:42 -080079 { "unbindable", MS_UNBINDABLE },
Dees Troyc4bc30e2014-02-03 15:04:19 +000080#endif
81#ifdef MS_PRIVATE
Hashcode62bd9e02013-11-19 21:59:42 -080082 { "private", MS_PRIVATE },
Dees Troyc4bc30e2014-02-03 15:04:19 +000083#endif
84#ifdef MS_SLAVE
Hashcode62bd9e02013-11-19 21:59:42 -080085 { "slave", MS_SLAVE },
Dees Troyc4bc30e2014-02-03 15:04:19 +000086#endif
87#ifdef MS_SHARED
Hashcode62bd9e02013-11-19 21:59:42 -080088 { "shared", MS_SHARED },
Dees Troyc4bc30e2014-02-03 15:04:19 +000089#endif
Hashcode62bd9e02013-11-19 21:59:42 -080090 { "sync", MS_SYNCHRONOUS },
91 { "defaults", 0 },
92 { 0, 0 },
93};
94
Dees_Troy51a0e822012-09-05 15:24:24 -040095TWPartition::TWPartition(void) {
96 Can_Be_Mounted = false;
97 Can_Be_Wiped = false;
Dees_Troya13d74f2013-03-24 08:54:55 -050098 Can_Be_Backed_Up = false;
Vojtech Bocek1dc30982013-08-30 21:49:30 +020099 Use_Rm_Rf = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400100 Wipe_During_Factory_Reset = false;
101 Wipe_Available_in_GUI = false;
102 Is_SubPartition = false;
Dees_Troy2691f9d2012-09-24 11:15:49 -0400103 Has_SubPartition = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400104 SubPartition_Of = "";
105 Symlink_Path = "";
106 Symlink_Mount_Point = "";
107 Mount_Point = "";
Dees_Troye58d5262012-09-21 12:27:57 -0400108 Backup_Path = "";
Dees_Troy38bd7602012-09-14 13:33:53 -0400109 Actual_Block_Device = "";
110 Primary_Block_Device = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400111 Alternate_Block_Device = "";
112 Removable = false;
113 Is_Present = false;
114 Length = 0;
115 Size = 0;
116 Used = 0;
117 Free = 0;
118 Backup_Size = 0;
119 Can_Be_Encrypted = false;
120 Is_Encrypted = false;
121 Is_Decrypted = false;
122 Decrypted_Block_Device = "";
123 Display_Name = "";
Dees_Troya13d74f2013-03-24 08:54:55 -0500124 Backup_Display_Name = "";
125 Storage_Name = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400126 Backup_Name = "";
Dees_Troy63c8df72012-09-10 14:02:05 -0400127 Backup_FileName = "";
Dees_Troy38bd7602012-09-14 13:33:53 -0400128 MTD_Name = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400129 Backup_Method = NONE;
Dees_Troy83bd4832013-05-04 12:39:56 +0000130 Can_Encrypt_Backup = false;
131 Use_Userdata_Encryption = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400132 Has_Data_Media = false;
Dees_Troye58d5262012-09-21 12:27:57 -0400133 Has_Android_Secure = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400134 Is_Storage = false;
Dees_Troya13d74f2013-03-24 08:54:55 -0500135 Is_Settings_Storage = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400136 Storage_Path = "";
137 Current_File_System = "";
138 Fstab_File_System = "";
Hashcode62bd9e02013-11-19 21:59:42 -0800139 Mount_Flags = 0;
140 Mount_Options = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400141 Format_Block_Size = 0;
Dees_Troy68cab492012-12-12 19:29:35 +0000142 Ignore_Blkid = false;
Dees_Troy16c2b312013-01-15 16:51:18 +0000143 Retain_Layout_Version = false;
Dees_Troy85f44ed2013-01-09 18:42:36 +0000144#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
145 EcryptFS_Password = "";
146#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400147}
148
149TWPartition::~TWPartition(void) {
150 // Do nothing
151}
152
Dees_Troy5bf43922012-09-07 16:07:55 -0400153bool TWPartition::Process_Fstab_Line(string Line, bool Display_Error) {
154 char full_line[MAX_FSTAB_LINE_LENGTH], item[MAX_FSTAB_LINE_LENGTH];
155 int line_len = Line.size(), index = 0, item_index = 0;
156 char* ptr;
Dees_Troy51127312012-09-08 13:08:49 -0400157 string Flags;
Dees_Troy5bf43922012-09-07 16:07:55 -0400158 strncpy(full_line, Line.c_str(), line_len);
Dees_Troya13d74f2013-03-24 08:54:55 -0500159 bool skip = false;
Dees_Troy5bf43922012-09-07 16:07:55 -0400160
Dees_Troy51127312012-09-08 13:08:49 -0400161 for (index = 0; index < line_len; index++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500162 if (full_line[index] == 34)
163 skip = !skip;
164 if (!skip && full_line[index] <= 32)
Dees_Troy5bf43922012-09-07 16:07:55 -0400165 full_line[index] = '\0';
Dees_Troy5bf43922012-09-07 16:07:55 -0400166 }
Dees_Troy7c2dec82012-09-26 09:49:14 -0400167 Mount_Point = full_line;
Dees_Troy2673cec2013-04-02 20:22:16 +0000168 LOGINFO("Processing '%s'\n", Mount_Point.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -0400169 Backup_Path = Mount_Point;
Dees_Troya13d74f2013-03-24 08:54:55 -0500170 Storage_Path = Mount_Point;
Dees_Troy70737fa2013-04-08 13:19:20 +0000171 Display_Name = full_line + 1;
172 Backup_Display_Name = Display_Name;
173 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400174 index = Mount_Point.size();
175 while (index < line_len) {
176 while (index < line_len && full_line[index] == '\0')
177 index++;
178 if (index >= line_len)
179 continue;
180 ptr = full_line + index;
181 if (item_index == 0) {
182 // File System
183 Fstab_File_System = ptr;
184 Current_File_System = ptr;
185 item_index++;
186 } else if (item_index == 1) {
187 // Primary Block Device
Dees_Troy38bd7602012-09-14 13:33:53 -0400188 if (Fstab_File_System == "mtd" || Fstab_File_System == "yaffs2") {
Dees_Troy094207a2012-09-26 12:00:39 -0400189 MTD_Name = ptr;
190 Find_MTD_Block_Device(MTD_Name);
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400191 } else if (Fstab_File_System == "bml") {
192 if (Mount_Point == "/boot")
193 MTD_Name = "boot";
194 else if (Mount_Point == "/recovery")
195 MTD_Name = "recovery";
196 Primary_Block_Device = ptr;
197 if (*ptr != '/')
Dees_Troy2673cec2013-04-02 20:22:16 +0000198 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 -0400199 } else if (*ptr != '/') {
Dees_Troy5bf43922012-09-07 16:07:55 -0400200 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000201 LOGERR("Invalid block device on '%s', '%s', %i\n", Line.c_str(), ptr, index);
Dees_Troy5bf43922012-09-07 16:07:55 -0400202 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000203 LOGINFO("Invalid block device on '%s', '%s', %i\n", Line.c_str(), ptr, index);
Dees_Troy5bf43922012-09-07 16:07:55 -0400204 return 0;
Dees_Troy38bd7602012-09-14 13:33:53 -0400205 } else {
206 Primary_Block_Device = ptr;
207 Find_Real_Block_Device(Primary_Block_Device, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400208 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400209 item_index++;
210 } else if (item_index > 1) {
211 if (*ptr == '/') {
212 // Alternate Block Device
213 Alternate_Block_Device = ptr;
214 Find_Real_Block_Device(Alternate_Block_Device, Display_Error);
215 } else if (strlen(ptr) > 7 && strncmp(ptr, "length=", 7) == 0) {
216 // Partition length
217 ptr += 7;
218 Length = atoi(ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400219 } else if (strlen(ptr) > 6 && strncmp(ptr, "flags=", 6) == 0) {
220 // Custom flags, save for later so that new values aren't overwritten by defaults
221 ptr += 6;
222 Flags = ptr;
Dees_Troy68cab492012-12-12 19:29:35 +0000223 Process_Flags(Flags, Display_Error);
Dees_Troy38bd7602012-09-14 13:33:53 -0400224 } else if (strlen(ptr) == 4 && (strncmp(ptr, "NULL", 4) == 0 || strncmp(ptr, "null", 4) == 0 || strncmp(ptr, "null", 4) == 0)) {
225 // Do nothing
Dees_Troy5bf43922012-09-07 16:07:55 -0400226 } else {
227 // Unhandled data
Dees_Troy2673cec2013-04-02 20:22:16 +0000228 LOGINFO("Unhandled fstab information: '%s', %i, line: '%s'\n", ptr, index, Line.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400229 }
230 }
231 while (index < line_len && full_line[index] != '\0')
232 index++;
233 }
234
235 if (!Is_File_System(Fstab_File_System) && !Is_Image(Fstab_File_System)) {
236 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000237 LOGERR("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400238 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000239 LOGINFO("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400240 return 0;
241 } else if (Is_File_System(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400242 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400243 Setup_File_System(Display_Error);
244 if (Mount_Point == "/system") {
245 Display_Name = "System";
Dees_Troya13d74f2013-03-24 08:54:55 -0500246 Backup_Display_Name = Display_Name;
247 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400248 Wipe_Available_in_GUI = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500249 Can_Be_Backed_Up = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400250 } else if (Mount_Point == "/data") {
251 Display_Name = "Data";
Dees_Troya13d74f2013-03-24 08:54:55 -0500252 Backup_Display_Name = Display_Name;
253 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400254 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400255 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500256 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000257 Can_Encrypt_Backup = true;
258 Use_Userdata_Encryption = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400259#ifdef RECOVERY_SDCARD_ON_DATA
Dees_Troya13d74f2013-03-24 08:54:55 -0500260 Storage_Name = "Internal Storage";
Dees_Troy5bf43922012-09-07 16:07:55 -0400261 Has_Data_Media = true;
Dees_Troy51127312012-09-08 13:08:49 -0400262 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500263 Is_Settings_Storage = true;
Dees_Troy51127312012-09-08 13:08:49 -0400264 Storage_Path = "/data/media";
Dees_Troy16b74352012-11-14 22:27:31 +0000265 Symlink_Path = Storage_Path;
Dees_Troy657c3092012-09-10 20:32:10 -0400266 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
267 Make_Dir("/emmc", Display_Error);
Dees_Troy657c3092012-09-10 20:32:10 -0400268 Symlink_Mount_Point = "/emmc";
269 } else {
270 Make_Dir("/sdcard", Display_Error);
Dees_Troy657c3092012-09-10 20:32:10 -0400271 Symlink_Mount_Point = "/sdcard";
272 }
Dees_Troy16b74352012-11-14 22:27:31 +0000273 if (Mount(false) && TWFunc::Path_Exists("/data/media/0")) {
274 Storage_Path = "/data/media/0";
275 Symlink_Path = Storage_Path;
276 DataManager::SetValue(TW_INTERNAL_PATH, "/data/media/0");
277 UnMount(true);
278 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400279#endif
280#ifdef TW_INCLUDE_CRYPTO
281 Can_Be_Encrypted = true;
Dees_Troy657c3092012-09-10 20:32:10 -0400282 char crypto_blkdev[255];
283 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
284 if (strcmp(crypto_blkdev, "error") != 0) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400285 DataManager::SetValue(TW_DATA_BLK_DEVICE, Primary_Block_Device);
Dees_Troy657c3092012-09-10 20:32:10 -0400286 DataManager::SetValue(TW_IS_DECRYPTED, 1);
287 Is_Encrypted = true;
288 Is_Decrypted = true;
289 Decrypted_Block_Device = crypto_blkdev;
Dees_Troy2673cec2013-04-02 20:22:16 +0000290 LOGINFO("Data already decrypted, new block device: '%s'\n", crypto_blkdev);
Dees_Troy657c3092012-09-10 20:32:10 -0400291 } else if (!Mount(false)) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400292 Is_Encrypted = true;
293 Is_Decrypted = false;
Gary Peck82599a82012-11-21 16:23:12 -0800294 Can_Be_Mounted = false;
295 Current_File_System = "emmc";
296 Setup_Image(Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400297 DataManager::SetValue(TW_IS_ENCRYPTED, 1);
298 DataManager::SetValue(TW_CRYPTO_PASSWORD, "");
299 DataManager::SetValue("tw_crypto_display", "");
Gary Peck82599a82012-11-21 16:23:12 -0800300 } else {
301 // Filesystem is not encrypted and the mount
302 // succeeded, so get it back to the original
303 // unmounted state
304 UnMount(false);
Dees_Troy51127312012-09-08 13:08:49 -0400305 }
Dees_Troy9b21af72012-10-01 15:51:46 -0400306 #ifdef RECOVERY_SDCARD_ON_DATA
307 if (!Is_Encrypted || (Is_Encrypted && Is_Decrypted))
308 Recreate_Media_Folder();
309 #endif
310#else
311 #ifdef RECOVERY_SDCARD_ON_DATA
312 Recreate_Media_Folder();
313 #endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400314#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400315 } else if (Mount_Point == "/cache") {
316 Display_Name = "Cache";
Dees_Troya13d74f2013-03-24 08:54:55 -0500317 Backup_Display_Name = Display_Name;
318 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400319 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400320 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500321 Can_Be_Backed_Up = true;
Dees_Troyce2fe772012-09-28 12:34:33 -0400322 if (Mount(false) && !TWFunc::Path_Exists("/cache/recovery/.")) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000323 LOGINFO("Recreating /cache/recovery folder.\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500324 if (mkdir("/cache/recovery", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0)
325 return -1;
Dees_Troyb46a6842012-09-25 11:06:46 -0400326 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400327 } else if (Mount_Point == "/datadata") {
Dees_Troy51127312012-09-08 13:08:49 -0400328 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400329 Display_Name = "DataData";
Dees_Troya13d74f2013-03-24 08:54:55 -0500330 Backup_Display_Name = Display_Name;
331 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400332 Is_SubPartition = true;
333 SubPartition_Of = "/data";
Dees_Troy5bf43922012-09-07 16:07:55 -0400334 DataManager::SetValue(TW_HAS_DATADATA, 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500335 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000336 Can_Encrypt_Backup = true;
337 Use_Userdata_Encryption = false; // This whole partition should be encrypted
Dees_Troy5bf43922012-09-07 16:07:55 -0400338 } else if (Mount_Point == "/sd-ext") {
Dees_Troy51127312012-09-08 13:08:49 -0400339 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400340 Display_Name = "SD-Ext";
Dees_Troya13d74f2013-03-24 08:54:55 -0500341 Backup_Display_Name = Display_Name;
342 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400343 Wipe_Available_in_GUI = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400344 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500345 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000346 Can_Encrypt_Backup = true;
347 Use_Userdata_Encryption = true;
Dees_Troy2c50e182012-09-26 20:05:28 -0400348 } else if (Mount_Point == "/boot") {
349 Display_Name = "Boot";
Dees_Troya13d74f2013-03-24 08:54:55 -0500350 Backup_Display_Name = Display_Name;
Dees_Troy2c50e182012-09-26 20:05:28 -0400351 DataManager::SetValue("tw_boot_is_mountable", 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500352 Can_Be_Backed_Up = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400353 }
354#ifdef TW_EXTERNAL_STORAGE_PATH
355 if (Mount_Point == EXPAND(TW_EXTERNAL_STORAGE_PATH)) {
356 Is_Storage = true;
357 Storage_Path = EXPAND(TW_EXTERNAL_STORAGE_PATH);
Dees_Troyc51f1f92012-09-20 15:32:13 -0400358 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500359 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400360#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000361 if (Mount_Point == "/sdcard" || Mount_Point == "/external_sd" || Mount_Point == "/external_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400362 Is_Storage = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400363 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500364 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400365#endif
Dees_Troyb05ddee2013-01-28 20:24:50 +0000366 }
Dees_Troy8170a922012-09-18 15:40:25 -0400367#ifdef TW_INTERNAL_STORAGE_PATH
368 if (Mount_Point == EXPAND(TW_INTERNAL_STORAGE_PATH)) {
369 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500370 Is_Settings_Storage = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400371 Storage_Path = EXPAND(TW_INTERNAL_STORAGE_PATH);
Dees_Troya13d74f2013-03-24 08:54:55 -0500372 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400373 }
374#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000375 if (Mount_Point == "/emmc" || Mount_Point == "/internal_sd" || Mount_Point == "/internal_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400376 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500377 Is_Settings_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500378 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400379 }
380#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400381 } else if (Is_Image(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400382 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400383 Setup_Image(Display_Error);
Dees_Troya13d74f2013-03-24 08:54:55 -0500384 if (Mount_Point == "/boot") {
385 Display_Name = "Boot";
386 Backup_Display_Name = Display_Name;
387 Can_Be_Backed_Up = true;
388 } else if (Mount_Point == "/recovery") {
389 Display_Name = "Recovery";
390 Backup_Display_Name = Display_Name;
Dees_Troya13d74f2013-03-24 08:54:55 -0500391 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400392 }
393
Dees_Troy51127312012-09-08 13:08:49 -0400394 // Process any custom flags
395 if (Flags.size() > 0)
396 Process_Flags(Flags, Display_Error);
Dees_Troy51127312012-09-08 13:08:49 -0400397 return true;
398}
399
Hashcode62bd9e02013-11-19 21:59:42 -0800400bool TWPartition::Process_FS_Flags(string& Options, int Flags) {
401 int i;
402 char *p;
403 char *savep;
404 char fs_options[250];
405
406 strlcpy(fs_options, Options.c_str(), sizeof(fs_options));
407 Options = "";
408
409 p = strtok_r(fs_options, ",", &savep);
410 while (p) {
411 /* Look for the flag "p" in the flag list "fl"
412 * If not found, the loop exits with fl[i].name being null.
413 */
414 for (i = 0; mount_flags[i].name; i++) {
415 if (strncmp(p, mount_flags[i].name, strlen(mount_flags[i].name)) == 0) {
416 Flags |= mount_flags[i].flag;
417 break;
418 }
419 }
420
421 if (!mount_flags[i].name) {
422 if (Options.size() > 0)
423 Options += ",";
424 Options += p;
425 }
426 p = strtok_r(NULL, ",", &savep);
427 }
428
429 return true;
430}
431
Dees_Troy51127312012-09-08 13:08:49 -0400432bool TWPartition::Process_Flags(string Flags, bool Display_Error) {
433 char flags[MAX_FSTAB_LINE_LENGTH];
Dees_Troya13d74f2013-03-24 08:54:55 -0500434 int flags_len, index = 0, ptr_len;
Dees_Troy51127312012-09-08 13:08:49 -0400435 char* ptr;
Dees_Troya13d74f2013-03-24 08:54:55 -0500436 bool skip = false, has_display_name = false, has_storage_name = false, has_backup_name = false;
Dees_Troy51127312012-09-08 13:08:49 -0400437
438 strcpy(flags, Flags.c_str());
439 flags_len = Flags.size();
440 for (index = 0; index < flags_len; index++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500441 if (flags[index] == 34)
442 skip = !skip;
443 if (!skip && flags[index] == ';')
Dees_Troy51127312012-09-08 13:08:49 -0400444 flags[index] = '\0';
445 }
446
447 index = 0;
448 while (index < flags_len) {
449 while (index < flags_len && flags[index] == '\0')
450 index++;
451 if (index >= flags_len)
452 continue;
453 ptr = flags + index;
Dees_Troya13d74f2013-03-24 08:54:55 -0500454 ptr_len = strlen(ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400455 if (strcmp(ptr, "removable") == 0) {
456 Removable = true;
Ethan Yonker06c3f932014-02-02 22:11:14 -0600457 } else if (strncmp(ptr, "storage", 7) == 0) {
458 if (ptr_len == 7) {
459 LOGINFO("ptr_len is 7, storage set to true\n");
460 Is_Storage = true;
461 } else if (ptr_len == 9) {
462 ptr += 9;
463 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y') {
464 LOGINFO("storage set to true\n");
465 Is_Storage = true;
466 } else {
467 LOGINFO("storage set to false\n");
468 Is_Storage = false;
469 }
470 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500471 } else if (strcmp(ptr, "settingsstorage") == 0) {
472 Is_Storage = true;
Dees_Troy63c8df72012-09-10 14:02:05 -0400473 } else if (strcmp(ptr, "canbewiped") == 0) {
474 Can_Be_Wiped = true;
Hashcodedabfd492013-08-29 22:45:30 -0700475 } else if (strcmp(ptr, "usermrf") == 0) {
476 Use_Rm_Rf = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500477 } else if (ptr_len > 7 && strncmp(ptr, "backup=", 7) == 0) {
478 ptr += 7;
479 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y')
480 Can_Be_Backed_Up = true;
481 else
482 Can_Be_Backed_Up = false;
Dees_Troy63c8df72012-09-10 14:02:05 -0400483 } else if (strcmp(ptr, "wipeingui") == 0) {
484 Can_Be_Wiped = true;
485 Wipe_Available_in_GUI = true;
486 } else if (strcmp(ptr, "wipeduringfactoryreset") == 0) {
487 Can_Be_Wiped = true;
488 Wipe_Available_in_GUI = true;
489 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500490 } else if (ptr_len > 15 && strncmp(ptr, "subpartitionof=", 15) == 0) {
Dees_Troy2c4c26f2013-01-28 15:26:43 +0000491 ptr += 15;
Dees_Troy51127312012-09-08 13:08:49 -0400492 Is_SubPartition = true;
493 SubPartition_Of = ptr;
Dees_Troy68cab492012-12-12 19:29:35 +0000494 } else if (strcmp(ptr, "ignoreblkid") == 0) {
495 Ignore_Blkid = true;
Dees_Troy16c2b312013-01-15 16:51:18 +0000496 } else if (strcmp(ptr, "retainlayoutversion") == 0) {
497 Retain_Layout_Version = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500498 } else if (ptr_len > 8 && strncmp(ptr, "symlink=", 8) == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400499 ptr += 8;
500 Symlink_Path = ptr;
Dees_Troya13d74f2013-03-24 08:54:55 -0500501 } else if (ptr_len > 8 && strncmp(ptr, "display=", 8) == 0) {
502 has_display_name = true;
Dees_Troy51127312012-09-08 13:08:49 -0400503 ptr += 8;
Dees_Troya13d74f2013-03-24 08:54:55 -0500504 if (*ptr == '\"') ptr++;
Dees_Troy51127312012-09-08 13:08:49 -0400505 Display_Name = ptr;
Dees_Troya13d74f2013-03-24 08:54:55 -0500506 if (Display_Name.substr(Display_Name.size() - 1, 1) == "\"") {
507 Display_Name.resize(Display_Name.size() - 1);
508 }
509 } else if (ptr_len > 11 && strncmp(ptr, "storagename=", 11) == 0) {
510 has_storage_name = true;
511 ptr += 11;
512 if (*ptr == '\"') ptr++;
513 Storage_Name = ptr;
514 if (Storage_Name.substr(Storage_Name.size() - 1, 1) == "\"") {
515 Storage_Name.resize(Storage_Name.size() - 1);
516 }
517 } else if (ptr_len > 11 && strncmp(ptr, "backupname=", 10) == 0) {
518 has_backup_name = true;
519 ptr += 10;
520 if (*ptr == '\"') ptr++;
521 Backup_Display_Name = ptr;
522 if (Backup_Display_Name.substr(Backup_Display_Name.size() - 1, 1) == "\"") {
523 Backup_Display_Name.resize(Backup_Display_Name.size() - 1);
524 }
525 } else if (ptr_len > 10 && strncmp(ptr, "blocksize=", 10) == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400526 ptr += 10;
527 Format_Block_Size = atoi(ptr);
Dees_Troya13d74f2013-03-24 08:54:55 -0500528 } else if (ptr_len > 7 && strncmp(ptr, "length=", 7) == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400529 ptr += 7;
530 Length = atoi(ptr);
Dees_Troy83bd4832013-05-04 12:39:56 +0000531 } else if (ptr_len > 17 && strncmp(ptr, "canencryptbackup=", 17) == 0) {
532 ptr += 17;
533 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y')
534 Can_Encrypt_Backup = true;
535 else
536 Can_Encrypt_Backup = false;
537 } else if (ptr_len > 21 && strncmp(ptr, "userdataencryptbackup=", 21) == 0) {
538 ptr += 21;
539 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y') {
540 Can_Encrypt_Backup = true;
541 Use_Userdata_Encryption = true;
542 } else {
543 Use_Userdata_Encryption = false;
544 }
Hashcode62bd9e02013-11-19 21:59:42 -0800545 } else if (ptr_len > 8 && strncmp(ptr, "fsflags=", 8) == 0) {
546 ptr += 8;
547 if (*ptr == '\"') ptr++;
548
549 Mount_Options = ptr;
550 if (Mount_Options.substr(Mount_Options.size() - 1, 1) == "\"") {
551 Mount_Options.resize(Mount_Options.size() - 1);
552 }
553 Process_FS_Flags(Mount_Options, Mount_Flags);
Dees_Troy51127312012-09-08 13:08:49 -0400554 } else {
555 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000556 LOGERR("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400557 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000558 LOGINFO("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400559 }
560 while (index < flags_len && flags[index] != '\0')
561 index++;
562 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500563 if (has_display_name && !has_storage_name)
564 Storage_Name = Display_Name;
565 if (!has_display_name && has_storage_name)
566 Display_Name = Storage_Name;
Dees_Troy74fb2e92013-04-15 14:35:47 +0000567 if (has_display_name && !has_backup_name && Backup_Display_Name != "Android Secure")
Dees_Troya13d74f2013-03-24 08:54:55 -0500568 Backup_Display_Name = Display_Name;
569 if (!has_display_name && has_backup_name)
570 Display_Name = Backup_Display_Name;
Dees_Troy51127312012-09-08 13:08:49 -0400571 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400572}
573
Dees_Troy5bf43922012-09-07 16:07:55 -0400574bool TWPartition::Is_File_System(string File_System) {
575 if (File_System == "ext2" ||
Dees_Troy63c8df72012-09-10 14:02:05 -0400576 File_System == "ext3" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400577 File_System == "ext4" ||
578 File_System == "vfat" ||
579 File_System == "ntfs" ||
580 File_System == "yaffs2" ||
bigbiff bigbiff3e146522012-11-14 14:32:59 -0500581 File_System == "exfat" ||
Dees_Troye5017042013-08-29 16:38:55 +0000582 File_System == "f2fs" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400583 File_System == "auto")
584 return true;
585 else
586 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400587}
588
Dees_Troy5bf43922012-09-07 16:07:55 -0400589bool TWPartition::Is_Image(string File_System) {
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400590 if (File_System == "emmc" || File_System == "mtd" || File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -0400591 return true;
592 else
593 return false;
594}
595
Dees_Troy51127312012-09-08 13:08:49 -0400596bool TWPartition::Make_Dir(string Path, bool Display_Error) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400597 if (!TWFunc::Path_Exists(Path)) {
Dees_Troy51127312012-09-08 13:08:49 -0400598 if (mkdir(Path.c_str(), 0777) == -1) {
599 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000600 LOGERR("Can not create '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400601 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000602 LOGINFO("Can not create '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400603 return false;
604 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000605 LOGINFO("Created '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400606 return true;
607 }
608 }
609 return true;
610}
611
Dees_Troy5bf43922012-09-07 16:07:55 -0400612void TWPartition::Setup_File_System(bool Display_Error) {
613 struct statfs st;
614
615 Can_Be_Mounted = true;
616 Can_Be_Wiped = true;
617
Dees_Troy5bf43922012-09-07 16:07:55 -0400618 // Make the mount point folder if it doesn't exist
Dees_Troy51127312012-09-08 13:08:49 -0400619 Make_Dir(Mount_Point, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400620 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
621 Backup_Name = Display_Name;
622 Backup_Method = FILES;
623}
624
625void TWPartition::Setup_Image(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400626 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
627 Backup_Name = Display_Name;
Gary Peck82599a82012-11-21 16:23:12 -0800628 if (Current_File_System == "emmc")
Dees_Troy5bf43922012-09-07 16:07:55 -0400629 Backup_Method = DD;
Gary Peck82599a82012-11-21 16:23:12 -0800630 else if (Current_File_System == "mtd" || Current_File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -0400631 Backup_Method = FLASH_UTILS;
632 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000633 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 -0400634 if (Find_Partition_Size()) {
635 Used = Size;
636 Backup_Size = Size;
Dees_Troy51a0e822012-09-05 15:24:24 -0400637 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400638 if (Display_Error)
Dees Troyd932ce12013-10-18 17:12:59 +0000639 LOGERR("Unable to find partition size for '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400640 else
Dees Troyd932ce12013-10-18 17:12:59 +0000641 LOGINFO("Unable to find partition size for '%s'\n", Mount_Point.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400642 }
643}
644
Dees_Troye58d5262012-09-21 12:27:57 -0400645void TWPartition::Setup_AndSec(void) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500646 Backup_Display_Name = "Android Secure";
Dees_Troye58d5262012-09-21 12:27:57 -0400647 Backup_Name = "and-sec";
Dees_Troya13d74f2013-03-24 08:54:55 -0500648 Can_Be_Backed_Up = true;
Dees_Troye58d5262012-09-21 12:27:57 -0400649 Has_Android_Secure = true;
650 Symlink_Path = Mount_Point + "/.android_secure";
651 Symlink_Mount_Point = "/and-sec";
652 Backup_Path = Symlink_Mount_Point;
653 Make_Dir("/and-sec", true);
654 Recreate_AndSec_Folder();
Ethan Yonkerd4d10732014-02-03 15:27:52 -0600655 Mount_Storage_Retry();
Dees_Troye58d5262012-09-21 12:27:57 -0400656}
657
Dees_Troy5bf43922012-09-07 16:07:55 -0400658void TWPartition::Find_Real_Block_Device(string& Block, bool Display_Error) {
659 char device[512], realDevice[512];
660
661 strcpy(device, Block.c_str());
662 memset(realDevice, 0, sizeof(realDevice));
663 while (readlink(device, realDevice, sizeof(realDevice)) > 0)
664 {
665 strcpy(device, realDevice);
666 memset(realDevice, 0, sizeof(realDevice));
667 }
668
669 if (device[0] != '/') {
670 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000671 LOGERR("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400672 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000673 LOGINFO("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400674 return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400675 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400676 Block = device;
677 return;
678 }
679}
680
Dees_Troy8e337f32012-10-13 22:07:49 -0400681void TWPartition::Mount_Storage_Retry(void) {
682 // On some devices, storage doesn't want to mount right away, retry and sleep
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500683 if (!Mount(true)) {
Dees_Troy8e337f32012-10-13 22:07:49 -0400684 int retry_count = 5;
685 while (retry_count > 0 && !Mount(false)) {
686 usleep(500000);
687 retry_count--;
688 }
689 Mount(true);
690 }
691}
692
Dees_Troy38bd7602012-09-14 13:33:53 -0400693bool TWPartition::Find_MTD_Block_Device(string MTD_Name) {
694 FILE *fp = NULL;
695 char line[255];
696
697 fp = fopen("/proc/mtd", "rt");
698 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000699 LOGERR("Device does not support /proc/mtd\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400700 return false;
701 }
702
703 while (fgets(line, sizeof(line), fp) != NULL)
704 {
705 char device[32], label[32];
706 unsigned long size = 0;
707 char* fstype = NULL;
708 int deviceId;
709
710 sscanf(line, "%s %lx %*s %*c%s", device, &size, label);
711
712 // Skip header and blank lines
713 if ((strcmp(device, "dev:") == 0) || (strlen(line) < 8))
714 continue;
715
716 // Strip off the trailing " from the label
717 label[strlen(label)-1] = '\0';
718
719 if (strcmp(label, MTD_Name.c_str()) == 0) {
720 // We found our device
721 // Strip off the trailing : from the device
722 device[strlen(device)-1] = '\0';
723 if (sscanf(device,"mtd%d", &deviceId) == 1) {
724 sprintf(device, "/dev/block/mtdblock%d", deviceId);
725 Primary_Block_Device = device;
Dees_Troy76543db2013-06-19 16:24:30 +0000726 fclose(fp);
727 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -0400728 }
729 }
730 }
731 fclose(fp);
732
733 return false;
734}
735
Dees_Troy51127312012-09-08 13:08:49 -0400736bool TWPartition::Get_Size_Via_statfs(bool Display_Error) {
737 struct statfs st;
738 string Local_Path = Mount_Point + "/.";
739
740 if (!Mount(Display_Error))
741 return false;
742
743 if (statfs(Local_Path.c_str(), &st) != 0) {
744 if (!Removable) {
745 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000746 LOGERR("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400747 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000748 LOGINFO("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400749 }
750 return false;
751 }
752 Size = (st.f_blocks * st.f_bsize);
753 Used = ((st.f_blocks - st.f_bfree) * st.f_bsize);
754 Free = (st.f_bfree * st.f_bsize);
755 Backup_Size = Used;
756 return true;
757}
758
759bool TWPartition::Get_Size_Via_df(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400760 FILE* fp;
761 char command[255], line[512];
762 int include_block = 1;
763 unsigned int min_len;
764
765 if (!Mount(Display_Error))
766 return false;
767
Dees_Troy38bd7602012-09-14 13:33:53 -0400768 min_len = Actual_Block_Device.size() + 2;
Dees_Troy51127312012-09-08 13:08:49 -0400769 sprintf(command, "df %s > /tmp/dfoutput.txt", Mount_Point.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +0200770 TWFunc::Exec_Cmd(command);
Dees_Troy51127312012-09-08 13:08:49 -0400771 fp = fopen("/tmp/dfoutput.txt", "rt");
772 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000773 LOGINFO("Unable to open /tmp/dfoutput.txt.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400774 return false;
Dees_Troy51127312012-09-08 13:08:49 -0400775 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400776
777 while (fgets(line, sizeof(line), fp) != NULL)
778 {
779 unsigned long blocks, used, available;
780 char device[64];
781 char tmpString[64];
782
783 if (strncmp(line, "Filesystem", 10) == 0)
784 continue;
785 if (strlen(line) < min_len) {
786 include_block = 0;
787 continue;
788 }
789 if (include_block) {
790 sscanf(line, "%s %lu %lu %lu", device, &blocks, &used, &available);
791 } else {
792 // The device block string is so long that the df information is on the next line
793 int space_count = 0;
Dees_Troye58d5262012-09-21 12:27:57 -0400794 sprintf(tmpString, "/dev/block/%s", Actual_Block_Device.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400795 while (tmpString[space_count] == 32)
796 space_count++;
797 sscanf(line + space_count, "%lu %lu %lu", &blocks, &used, &available);
798 }
799
800 // Adjust block size to byte size
801 Size = blocks * 1024ULL;
802 Used = used * 1024ULL;
803 Free = available * 1024ULL;
804 Backup_Size = Used;
805 }
806 fclose(fp);
807 return true;
808}
809
Dees_Troy5bf43922012-09-07 16:07:55 -0400810bool TWPartition::Find_Partition_Size(void) {
811 FILE* fp;
812 char line[512];
813 string tmpdevice;
814
igoriok87e3d932013-01-31 21:03:53 +0200815 fp = fopen("/proc/dumchar_info", "rt");
816 if (fp != NULL) {
817 while (fgets(line, sizeof(line), fp) != NULL)
818 {
819 char label[32], device[32];
820 unsigned long size = 0;
821
822 sscanf(line, "%s %lx %*lx %*lu %s", label, &size, device);
823
bigbiff bigbiff34684ff2013-12-01 21:03:45 -0500824 // Skip header, annotation and blank lines
igoriok87e3d932013-01-31 21:03:53 +0200825 if ((strncmp(device, "/dev/", 5) != 0) || (strlen(line) < 8))
826 continue;
827
828 tmpdevice = "/dev/";
829 tmpdevice += label;
830 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
831 Size = size;
832 fclose(fp);
833 return true;
834 }
835 }
836 }
837
Dees_Troy5bf43922012-09-07 16:07:55 -0400838 // In this case, we'll first get the partitions we care about (with labels)
839 fp = fopen("/proc/partitions", "rt");
840 if (fp == NULL)
841 return false;
842
843 while (fgets(line, sizeof(line), fp) != NULL)
844 {
845 unsigned long major, minor, blocks;
846 char device[512];
847 char tmpString[64];
848
Dees_Troy63c8df72012-09-10 14:02:05 -0400849 if (strlen(line) < 7 || line[0] == 'm') continue;
Dees_Troy5bf43922012-09-07 16:07:55 -0400850 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
851
852 tmpdevice = "/dev/block/";
853 tmpdevice += device;
Dees_Troy38bd7602012-09-14 13:33:53 -0400854 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400855 // Adjust block size to byte size
856 Size = blocks * 1024ULL;
857 fclose(fp);
858 return true;
859 }
860 }
861 fclose(fp);
862 return false;
863}
864
Dees_Troy5bf43922012-09-07 16:07:55 -0400865bool TWPartition::Is_Mounted(void) {
866 if (!Can_Be_Mounted)
867 return false;
868
869 struct stat st1, st2;
870 string test_path;
871
872 // Check to see if the mount point directory exists
873 test_path = Mount_Point + "/.";
874 if (stat(test_path.c_str(), &st1) != 0) return false;
875
876 // Check to see if the directory above the mount point exists
877 test_path = Mount_Point + "/../.";
878 if (stat(test_path.c_str(), &st2) != 0) return false;
879
880 // Compare the device IDs -- if they match then we're (probably) using tmpfs instead of an actual device
881 int ret = (st1.st_dev != st2.st_dev) ? true : false;
882
883 return ret;
884}
885
886bool TWPartition::Mount(bool Display_Error) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000887 int exfat_mounted = 0;
888
Dees_Troy5bf43922012-09-07 16:07:55 -0400889 if (Is_Mounted()) {
890 return true;
891 } else if (!Can_Be_Mounted) {
892 return false;
893 }
Dees_Troy38bd7602012-09-14 13:33:53 -0400894
895 Find_Actual_Block_Device();
896
897 // Check the current file system before mounting
898 Check_FS_Type();
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000899 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/exfat-fuse")) {
Dees_Troye34c1332013-02-06 19:13:00 +0000900 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 +0000901 LOGINFO("cmd: %s\n", cmd.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000902 string result;
903 if (TWFunc::Exec_Cmd(cmd, result) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000904 LOGINFO("exfat-fuse failed to mount with result '%s', trying vfat\n", result.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000905 Current_File_System = "vfat";
906 } else {
907#ifdef TW_NO_EXFAT_FUSE
908 UnMount(false);
909 // We'll let the kernel handle it but using exfat-fuse to detect if the file system is actually exfat
910 // Some kernels let us mount vfat as exfat which doesn't work out too well
911#else
912 exfat_mounted = 1;
913#endif
914 }
915 }
Dees_Troy22042032012-12-18 21:23:08 +0000916 if (Fstab_File_System == "yaffs2") {
917 // mount an MTD partition as a YAFFS2 filesystem.
Dees_Troy76543db2013-06-19 16:24:30 +0000918 const unsigned long flags = MS_NOATIME | MS_NODEV | MS_NODIRATIME;
919 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags, NULL) < 0) {
920 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags | MS_RDONLY, NULL) < 0) {
921 if (Display_Error)
922 LOGERR("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
923 else
924 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
925 return false;
926 } else {
927 LOGINFO("Mounted '%s' (MTD) as RO\n", Mount_Point.c_str());
928 return true;
929 }
930 } else {
931 struct stat st;
932 string test_path = Mount_Point;
933 if (stat(test_path.c_str(), &st) < 0) {
934 if (Display_Error)
935 LOGERR("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
936 else
937 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
938 return false;
939 }
940 mode_t new_mode = st.st_mode | S_IXUSR | S_IXGRP | S_IXOTH;
941 if (new_mode != st.st_mode) {
942 LOGINFO("Fixing execute permissions for %s\n", Mount_Point.c_str());
943 if (chmod(Mount_Point.c_str(), new_mode) < 0) {
944 if (Display_Error)
945 LOGERR("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
946 else
947 LOGINFO("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
948 return false;
949 }
950 }
Dees_Troy22042032012-12-18 21:23:08 +0000951 return true;
Dees_Troy76543db2013-06-19 16:24:30 +0000952 }
Dees Troy216e0422014-02-07 03:46:42 +0000953 } 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 +0000954#ifdef TW_NO_EXFAT_FUSE
955 if (Current_File_System == "exfat") {
Dees_Troy2673cec2013-04-02 20:22:16 +0000956 LOGINFO("Mounting exfat failed, trying vfat...\n");
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000957 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), "vfat", 0, NULL) != 0) {
Dees_Troy85f44ed2013-01-09 18:42:36 +0000958 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000959 LOGERR("Unable to mount '%s'\n", Mount_Point.c_str());
Dees_Troy85f44ed2013-01-09 18:42:36 +0000960 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000961 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
Hashcode62bd9e02013-11-19 21:59:42 -0800962 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 +0000963 return false;
Dees_Troy85f44ed2013-01-09 18:42:36 +0000964 }
Dees_Troyb05ddee2013-01-28 20:24:50 +0000965 } else {
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000966#endif
967 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000968 LOGERR("Unable to mount '%s'\n", Mount_Point.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000969 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000970 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
971 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 +0000972 return false;
973#ifdef TW_NO_EXFAT_FUSE
Dees_Troy85f44ed2013-01-09 18:42:36 +0000974 }
975#endif
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000976 }
977#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
978 string MetaEcfsFile = EXPAND(TW_EXTERNAL_STORAGE_PATH);
979 MetaEcfsFile += "/.MetaEcfsFile";
980 if (EcryptFS_Password.size() > 0 && PartitionManager.Mount_By_Path("/data", false) && TWFunc::Path_Exists(MetaEcfsFile)) {
981 if (mount_ecryptfs_drive(EcryptFS_Password.c_str(), Mount_Point.c_str(), Mount_Point.c_str(), 0) != 0) {
982 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000983 LOGERR("Unable to mount ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000984 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000985 LOGINFO("Unable to mount ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000986 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000987 LOGINFO("Successfully mounted ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000988 Is_Decrypted = true;
Dees_Troy51127312012-09-08 13:08:49 -0400989 }
Dees_Troy066eb302013-08-23 17:20:32 +0000990 } else if (Mount_Point == EXPAND(TW_EXTERNAL_STORAGE_PATH)) {
991 if (Is_Decrypted)
992 LOGINFO("Mounting external storage, '%s' is not encrypted\n", Mount_Point.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000993 Is_Decrypted = false;
994 }
995#endif
996 if (Removable)
997 Update_Size(Display_Error);
998
999 if (!Symlink_Mount_Point.empty()) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001000 string Command = "mount '" + Symlink_Path + "' '" + Symlink_Mount_Point + "'";
1001 TWFunc::Exec_Cmd(Command);
Dees_Troy5bf43922012-09-07 16:07:55 -04001002 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001003 return true;
1004}
1005
1006bool TWPartition::UnMount(bool Display_Error) {
1007 if (Is_Mounted()) {
1008 int never_unmount_system;
1009
1010 DataManager::GetValue(TW_DONT_UNMOUNT_SYSTEM, never_unmount_system);
1011 if (never_unmount_system == 1 && Mount_Point == "/system")
1012 return true; // Never unmount system if you're not supposed to unmount it
1013
Dees_Troyc8bafa12013-01-10 15:43:00 +00001014#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
1015 if (EcryptFS_Password.size() > 0) {
1016 if (unmount_ecryptfs_drive(Mount_Point.c_str()) != 0) {
1017 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001018 LOGERR("Unable to unmount ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troyc8bafa12013-01-10 15:43:00 +00001019 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001020 LOGINFO("Unable to unmount ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troyc8bafa12013-01-10 15:43:00 +00001021 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001022 LOGINFO("Successfully unmounted ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troyc8bafa12013-01-10 15:43:00 +00001023 }
1024 }
1025#endif
1026
Dees_Troy38bd7602012-09-14 13:33:53 -04001027 if (!Symlink_Mount_Point.empty())
1028 umount(Symlink_Mount_Point.c_str());
1029
Dees_Troyb05ddee2013-01-28 20:24:50 +00001030 umount(Mount_Point.c_str());
1031 if (Is_Mounted()) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001032 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001033 LOGERR("Unable to unmount '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001034 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001035 LOGINFO("Unable to unmount '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001036 return false;
1037 } else
1038 return true;
1039 } else {
1040 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001041 }
1042}
1043
Gary Peck43acadf2012-11-21 21:19:01 -08001044bool TWPartition::Wipe(string New_File_System) {
Dees_Troy16c2b312013-01-15 16:51:18 +00001045 bool wiped = false, update_crypt = false;
1046 int check;
1047 string Layout_Filename = Mount_Point + "/.layout_version";
1048
Dees_Troy38bd7602012-09-14 13:33:53 -04001049 if (!Can_Be_Wiped) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001050 LOGERR("Partition '%s' cannot be wiped.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001051 return false;
1052 }
1053
Dees_Troyc51f1f92012-09-20 15:32:13 -04001054 if (Mount_Point == "/cache")
Dees_Troy2673cec2013-04-02 20:22:16 +00001055 Log_Offset = 0;
Dees_Troyc51f1f92012-09-20 15:32:13 -04001056
Dees_Troyce675462013-01-09 19:48:21 +00001057#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
1058 if (Mount_Point == "/data" && Mount(false)) {
1059 if (TWFunc::Path_Exists("/data/system/edk_p_sd"))
1060 TWFunc::copy_file("/data/system/edk_p_sd", "/tmp/edk_p_sd", 0600);
1061 }
1062#endif
1063
Dees_Troy16c2b312013-01-15 16:51:18 +00001064 if (Retain_Layout_Version && Mount(false) && TWFunc::Path_Exists(Layout_Filename))
1065 TWFunc::copy_file(Layout_Filename, "/.layout_version", 0600);
1066 else
1067 unlink("/.layout_version");
Dees_Troy38bd7602012-09-14 13:33:53 -04001068
Dees_Troy16c2b312013-01-15 16:51:18 +00001069 if (Has_Data_Media) {
1070 wiped = Wipe_Data_Without_Wiping_Media();
1071 } else {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001072
Dees_Troy16c2b312013-01-15 16:51:18 +00001073 DataManager::GetValue(TW_RM_RF_VAR, check);
1074
Hashcodedabfd492013-08-29 22:45:30 -07001075 if (check || Use_Rm_Rf)
Dees_Troy16c2b312013-01-15 16:51:18 +00001076 wiped = Wipe_RMRF();
1077 else if (New_File_System == "ext4")
1078 wiped = Wipe_EXT4();
1079 else if (New_File_System == "ext2" || New_File_System == "ext3")
1080 wiped = Wipe_EXT23(New_File_System);
1081 else if (New_File_System == "vfat")
1082 wiped = Wipe_FAT();
1083 else if (New_File_System == "exfat")
1084 wiped = Wipe_EXFAT();
1085 else if (New_File_System == "yaffs2")
1086 wiped = Wipe_MTD();
Dees_Troye5017042013-08-29 16:38:55 +00001087 else if (New_File_System == "f2fs")
1088 wiped = Wipe_F2FS();
Dees_Troy16c2b312013-01-15 16:51:18 +00001089 else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001090 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 +00001091 unlink("/.layout_version");
1092 return false;
1093 }
1094 update_crypt = wiped;
Gary Pecke8bc5d72012-12-21 06:45:25 -08001095 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001096
Gary Pecke8bc5d72012-12-21 06:45:25 -08001097 if (wiped) {
Dees_Troyce675462013-01-09 19:48:21 +00001098#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
1099 if (Mount_Point == "/data" && Mount(false)) {
1100 if (TWFunc::Path_Exists("/tmp/edk_p_sd")) {
1101 Make_Dir("/data/system", true);
1102 TWFunc::copy_file("/tmp/edk_p_sd", "/data/system/edk_p_sd", 0600);
1103 }
1104 }
1105#endif
Dees_Troy16c2b312013-01-15 16:51:18 +00001106
Dees_Troy1c1ac442013-01-17 21:42:14 +00001107 if (Mount_Point == "/cache")
1108 DataManager::Output_Version();
1109
Dees_Troy16c2b312013-01-15 16:51:18 +00001110 if (TWFunc::Path_Exists("/.layout_version") && Mount(false))
1111 TWFunc::copy_file("/.layout_version", Layout_Filename, 0600);
1112
1113 if (update_crypt) {
1114 Setup_File_System(false);
1115 if (Is_Encrypted && !Is_Decrypted) {
1116 // just wiped an encrypted partition back to its unencrypted state
1117 Is_Encrypted = false;
1118 Is_Decrypted = false;
1119 Decrypted_Block_Device = "";
1120 if (Mount_Point == "/data") {
1121 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1122 DataManager::SetValue(TW_IS_DECRYPTED, 0);
1123 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001124 }
1125 }
1126 }
1127 return wiped;
Dees_Troy51a0e822012-09-05 15:24:24 -04001128}
1129
Gary Peck43acadf2012-11-21 21:19:01 -08001130bool TWPartition::Wipe() {
Gary Peck82599a82012-11-21 16:23:12 -08001131 if (Is_File_System(Current_File_System))
1132 return Wipe(Current_File_System);
1133 else
1134 return Wipe(Fstab_File_System);
Gary Peck43acadf2012-11-21 21:19:01 -08001135}
1136
Dees_Troye58d5262012-09-21 12:27:57 -04001137bool TWPartition::Wipe_AndSec(void) {
1138 if (!Has_Android_Secure)
1139 return false;
1140
Dees_Troye58d5262012-09-21 12:27:57 -04001141 if (!Mount(true))
1142 return false;
1143
Dees_Troy2673cec2013-04-02 20:22:16 +00001144 gui_print("Wiping %s\n", Backup_Display_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001145 TWFunc::removeDir(Mount_Point + "/.android_secure/", true);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001146 return true;
Dees_Troye58d5262012-09-21 12:27:57 -04001147}
1148
Dees_Troy51a0e822012-09-05 15:24:24 -04001149bool TWPartition::Backup(string backup_folder) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001150 if (Backup_Method == FILES)
1151 return Backup_Tar(backup_folder);
1152 else if (Backup_Method == DD)
1153 return Backup_DD(backup_folder);
1154 else if (Backup_Method == FLASH_UTILS)
1155 return Backup_Dump_Image(backup_folder);
Dees_Troy2673cec2013-04-02 20:22:16 +00001156 LOGERR("Unknown backup method for '%s'\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001157 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001158}
1159
Dees_Troy43d8b002012-09-17 16:00:01 -04001160bool TWPartition::Check_MD5(string restore_folder) {
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001161 string Full_Filename, md5file;
Dees_Troy43d8b002012-09-17 16:00:01 -04001162 char split_filename[512];
1163 int index = 0;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001164 twrpDigest md5sum;
Dees_Troy43d8b002012-09-17 16:00:01 -04001165
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001166 memset(split_filename, 0, sizeof(split_filename));
Dees_Troy43d8b002012-09-17 16:00:01 -04001167 Full_Filename = restore_folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04001168 if (!TWFunc::Path_Exists(Full_Filename)) {
1169 // This is a split archive, we presume
1170 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
Dees_Troy2673cec2013-04-02 20:22:16 +00001171 LOGINFO("split_filename: %s\n", split_filename);
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001172 md5file = split_filename;
1173 md5file += ".md5";
1174 if (!TWFunc::Path_Exists(md5file)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001175 LOGERR("No md5 file found for '%s'.\n", split_filename);
1176 LOGERR("Please unselect Enable MD5 verification to restore.\n");
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001177 return false;
1178 }
1179 md5sum.setfn(split_filename);
Dees_Troy83bd4832013-05-04 12:39:56 +00001180 while (index < 1000) {
1181 if (TWFunc::Path_Exists(split_filename) && md5sum.verify_md5digest() != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001182 LOGERR("MD5 failed to match on '%s'.\n", split_filename);
Dees_Troy43d8b002012-09-17 16:00:01 -04001183 return false;
1184 }
1185 index++;
1186 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001187 md5sum.setfn(split_filename);
Dees_Troy43d8b002012-09-17 16:00:01 -04001188 }
Dees_Troy4a2a1262012-09-18 09:33:47 -04001189 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001190 } else {
1191 // Single file archive
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001192 md5file = Full_Filename + ".md5";
1193 if (!TWFunc::Path_Exists(md5file)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001194 LOGERR("No md5 file found for '%s'.\n", Full_Filename.c_str());
1195 LOGERR("Please unselect Enable MD5 verification to restore.\n");
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001196 return false;
1197 }
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001198 md5sum.setfn(Full_Filename);
1199 if (md5sum.verify_md5digest() != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001200 LOGERR("MD5 failed to match on '%s'.\n", Full_Filename.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001201 return false;
1202 } else
1203 return true;
1204 }
1205 return false;
1206}
1207
Dees_Troy51a0e822012-09-05 15:24:24 -04001208bool TWPartition::Restore(string restore_folder) {
Gary Peck43acadf2012-11-21 21:19:01 -08001209 size_t first_period, second_period;
1210 string Restore_File_System;
1211
1212 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
Dees_Troy2673cec2013-04-02 20:22:16 +00001213 LOGINFO("Restore filename is: %s\n", Backup_FileName.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001214
1215 // Parse backup filename to extract the file system before wiping
1216 first_period = Backup_FileName.find(".");
1217 if (first_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001218 LOGERR("Unable to find file system (first period).\n");
Gary Peck43acadf2012-11-21 21:19:01 -08001219 return false;
1220 }
1221 Restore_File_System = Backup_FileName.substr(first_period + 1, Backup_FileName.size() - first_period - 1);
1222 second_period = Restore_File_System.find(".");
1223 if (second_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001224 LOGERR("Unable to find file system (second period).\n");
Gary Peck43acadf2012-11-21 21:19:01 -08001225 return false;
1226 }
1227 Restore_File_System.resize(second_period);
Dees_Troy2673cec2013-04-02 20:22:16 +00001228 LOGINFO("Restore file system is: '%s'.\n", Restore_File_System.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001229
1230 if (Is_File_System(Restore_File_System))
1231 return Restore_Tar(restore_folder, Restore_File_System);
1232 else if (Is_Image(Restore_File_System)) {
1233 if (Restore_File_System == "emmc")
1234 return Restore_DD(restore_folder);
1235 else if (Restore_File_System == "mtd" || Restore_File_System == "bml")
1236 return Restore_Flash_Image(restore_folder);
1237 }
1238
Dees_Troy2673cec2013-04-02 20:22:16 +00001239 LOGERR("Unknown restore method for '%s'\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001240 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001241}
1242
1243string TWPartition::Backup_Method_By_Name() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001244 if (Backup_Method == NONE)
1245 return "none";
1246 else if (Backup_Method == FILES)
1247 return "files";
1248 else if (Backup_Method == DD)
1249 return "dd";
1250 else if (Backup_Method == FLASH_UTILS)
1251 return "flash_utils";
1252 else
1253 return "undefined";
1254 return "ERROR!";
Dees_Troy51a0e822012-09-05 15:24:24 -04001255}
1256
1257bool TWPartition::Decrypt(string Password) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001258 LOGINFO("STUB TWPartition::Decrypt, password: '%s'\n", Password.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001259 // Is this needed?
Dees_Troy51a0e822012-09-05 15:24:24 -04001260 return 1;
1261}
1262
1263bool TWPartition::Wipe_Encryption() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001264 bool Save_Data_Media = Has_Data_Media;
1265
1266 if (!UnMount(true))
1267 return false;
1268
Dees_Troy38bd7602012-09-14 13:33:53 -04001269 Has_Data_Media = false;
Dees_Troy74fb2e92013-04-15 14:35:47 +00001270 Decrypted_Block_Device = "";
1271 Is_Decrypted = false;
1272 Is_Encrypted = false;
Gary Pecke8bc5d72012-12-21 06:45:25 -08001273 if (Wipe(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001274 Has_Data_Media = Save_Data_Media;
1275 if (Has_Data_Media && !Symlink_Mount_Point.empty()) {
1276 Recreate_Media_Folder();
1277 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001278 gui_print("You may need to reboot recovery to be able to use /data again.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001279 return true;
1280 } else {
1281 Has_Data_Media = Save_Data_Media;
Dees_Troy2673cec2013-04-02 20:22:16 +00001282 LOGERR("Unable to format to remove encryption.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001283 return false;
1284 }
1285 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001286}
1287
1288void TWPartition::Check_FS_Type() {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001289 const char* type;
1290 blkid_probe pr;
Dees_Troy5bf43922012-09-07 16:07:55 -04001291
Dees_Troy68cab492012-12-12 19:29:35 +00001292 if (Fstab_File_System == "yaffs2" || Fstab_File_System == "mtd" || Fstab_File_System == "bml" || Ignore_Blkid)
1293 return; // Running blkid on some mtd devices causes a massive crash or needs to be skipped
Dees_Troy5bf43922012-09-07 16:07:55 -04001294
Dees_Troy38bd7602012-09-14 13:33:53 -04001295 Find_Actual_Block_Device();
Dees_Troy8170a922012-09-18 15:40:25 -04001296 if (!Is_Present)
1297 return;
Dees_Troy51127312012-09-08 13:08:49 -04001298
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001299 pr = blkid_new_probe_from_filename(Actual_Block_Device.c_str());
1300 if (blkid_do_fullprobe(pr)) {
1301 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00001302 LOGINFO("Can't probe device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001303 return;
Dees_Troy5bf43922012-09-07 16:07:55 -04001304 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001305
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001306 if (blkid_probe_lookup_value(pr, "TYPE", &type, NULL) < 0) {
1307 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00001308 LOGINFO("can't find filesystem on device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001309 return;
1310 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001311
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001312 Current_File_System = type;
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001313 blkid_free_probe(pr);
Dees_Troy51a0e822012-09-05 15:24:24 -04001314}
1315
Gary Peck43acadf2012-11-21 21:19:01 -08001316bool TWPartition::Wipe_EXT23(string File_System) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001317 if (!UnMount(true))
1318 return false;
1319
Dees_Troy43d8b002012-09-17 16:00:01 -04001320 if (TWFunc::Path_Exists("/sbin/mke2fs")) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001321 string command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001322
Dees_Troy2673cec2013-04-02 20:22:16 +00001323 gui_print("Formatting %s using mke2fs...\n", Display_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001324 Find_Actual_Block_Device();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001325 command = "mke2fs -t " + File_System + " -m 0 " + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001326 LOGINFO("mke2fs command: %s\n", command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001327 if (TWFunc::Exec_Cmd(command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001328 Current_File_System = File_System;
Dees_Troye58d5262012-09-21 12:27:57 -04001329 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001330 gui_print("Done.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001331 return true;
1332 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001333 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001334 return false;
1335 }
1336 } else
1337 return Wipe_RMRF();
1338
1339 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001340}
1341
1342bool TWPartition::Wipe_EXT4() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001343 if (!UnMount(true))
1344 return false;
1345
Dees_Troyb3265ab2013-08-30 02:59:14 +00001346#if defined(HAVE_SELINUX) && defined(USE_EXT4)
Dees_Troya95f55c2013-08-17 13:14:43 +00001347 gui_print("Formatting %s using make_ext4fs function.\n", Display_Name.c_str());
Dees_Troya95f55c2013-08-17 13:14:43 +00001348 if (make_ext4fs(Actual_Block_Device.c_str(), Length, Mount_Point.c_str(), selinux_handle) != 0) {
Dees_Troya95f55c2013-08-17 13:14:43 +00001349 LOGERR("Unable to wipe '%s' using function call.\n", Mount_Point.c_str());
1350 return false;
1351 } else {
bigbiff bigbiffc49d7062013-10-11 20:28:00 -04001352 #ifdef HAVE_SELINUX
1353 string sedir = Mount_Point + "/lost+found";
1354 PartitionManager.Mount_By_Path(sedir.c_str(), true);
1355 rmdir(sedir.c_str());
1356 mkdir(sedir.c_str(), S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP);
1357 #endif
Dees_Troya95f55c2013-08-17 13:14:43 +00001358 return true;
1359 }
1360#else
Dees_Troy43d8b002012-09-17 16:00:01 -04001361 if (TWFunc::Path_Exists("/sbin/make_ext4fs")) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001362 string Command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001363
Dees_Troy2673cec2013-04-02 20:22:16 +00001364 gui_print("Formatting %s using make_ext4fs...\n", Display_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001365 Find_Actual_Block_Device();
1366 Command = "make_ext4fs";
1367 if (!Is_Decrypted && Length != 0) {
1368 // Only use length if we're not decrypted
1369 char len[32];
1370 sprintf(len, "%i", Length);
1371 Command += " -l ";
1372 Command += len;
1373 }
Dees_Troy5295d582013-09-06 15:51:08 +00001374 if (TWFunc::Path_Exists("/file_contexts")) {
1375 Command += " -S /file_contexts";
1376 }
1377 Command += " -a " + Mount_Point + " " + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001378 LOGINFO("make_ext4fs command: %s\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001379 if (TWFunc::Exec_Cmd(Command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001380 Current_File_System = "ext4";
Dees_Troye58d5262012-09-21 12:27:57 -04001381 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001382 gui_print("Done.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001383 return true;
1384 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001385 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001386 return false;
1387 }
1388 } else
Gary Peck43acadf2012-11-21 21:19:01 -08001389 return Wipe_EXT23("ext4");
Dees_Troya95f55c2013-08-17 13:14:43 +00001390#endif
Dees_Troy38bd7602012-09-14 13:33:53 -04001391 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001392}
1393
1394bool TWPartition::Wipe_FAT() {
Vojtech Bocek05534202013-09-11 08:11:56 +02001395 string command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001396
Dees_Troy43d8b002012-09-17 16:00:01 -04001397 if (TWFunc::Path_Exists("/sbin/mkdosfs")) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001398 if (!UnMount(true))
1399 return false;
1400
Dees_Troy2673cec2013-04-02 20:22:16 +00001401 gui_print("Formatting %s using mkdosfs...\n", Display_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001402 Find_Actual_Block_Device();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001403 command = "mkdosfs " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02001404 if (TWFunc::Exec_Cmd(command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001405 Current_File_System = "vfat";
Dees_Troye58d5262012-09-21 12:27:57 -04001406 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001407 gui_print("Done.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001408 return true;
1409 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001410 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001411 return false;
1412 }
1413 return true;
1414 }
1415 else
1416 return Wipe_RMRF();
1417
1418 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001419}
1420
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001421bool TWPartition::Wipe_EXFAT() {
Vojtech Bocek05534202013-09-11 08:11:56 +02001422 string command;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001423
1424 if (TWFunc::Path_Exists("/sbin/mkexfatfs")) {
1425 if (!UnMount(true))
1426 return false;
1427
Dees_Troy2673cec2013-04-02 20:22:16 +00001428 gui_print("Formatting %s using mkexfatfs...\n", Display_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001429 Find_Actual_Block_Device();
1430 command = "mkexfatfs " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02001431 if (TWFunc::Exec_Cmd(command) == 0) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001432 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001433 gui_print("Done.\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001434 return true;
1435 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001436 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001437 return false;
1438 }
1439 return true;
1440 }
1441 return false;
1442}
1443
Dees_Troy38bd7602012-09-14 13:33:53 -04001444bool TWPartition::Wipe_MTD() {
1445 if (!UnMount(true))
1446 return false;
1447
Dees_Troy2673cec2013-04-02 20:22:16 +00001448 gui_print("MTD Formatting \"%s\"\n", MTD_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001449
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001450 mtd_scan_partitions();
1451 const MtdPartition* mtd = mtd_find_partition_by_name(MTD_Name.c_str());
1452 if (mtd == NULL) {
1453 LOGERR("No mtd partition named '%s'", MTD_Name.c_str());
1454 return false;
1455 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001456
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001457 MtdWriteContext* ctx = mtd_write_partition(mtd);
1458 if (ctx == NULL) {
1459 LOGERR("Can't write '%s', failed to format.", MTD_Name.c_str());
1460 return false;
1461 }
1462 if (mtd_erase_blocks(ctx, -1) == -1) {
1463 mtd_write_close(ctx);
1464 LOGERR("Failed to format '%s'", MTD_Name.c_str());
1465 return false;
1466 }
1467 if (mtd_write_close(ctx) != 0) {
1468 LOGERR("Failed to close '%s'", MTD_Name.c_str());
1469 return false;
1470 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001471 Current_File_System = "yaffs2";
Dees_Troye58d5262012-09-21 12:27:57 -04001472 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001473 gui_print("Done.\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001474 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001475}
1476
1477bool TWPartition::Wipe_RMRF() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001478 if (!Mount(true))
1479 return false;
1480
Dees_Troy2673cec2013-04-02 20:22:16 +00001481 gui_print("Removing all files under '%s'\n", Mount_Point.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001482 TWFunc::removeDir(Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04001483 Recreate_AndSec_Folder();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001484 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001485}
1486
Dees_Troye5017042013-08-29 16:38:55 +00001487bool TWPartition::Wipe_F2FS() {
Vojtech Bocek05534202013-09-11 08:11:56 +02001488 string command;
Dees_Troye5017042013-08-29 16:38:55 +00001489
1490 if (TWFunc::Path_Exists("/sbin/mkfs.f2fs")) {
1491 if (!UnMount(true))
1492 return false;
1493
1494 gui_print("Formatting %s using mkfs.f2fs...\n", Display_Name.c_str());
1495 Find_Actual_Block_Device();
1496 command = "mkfs.f2fs " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02001497 if (TWFunc::Exec_Cmd(command) == 0) {
Dees_Troye5017042013-08-29 16:38:55 +00001498 Recreate_AndSec_Folder();
1499 gui_print("Done.\n");
1500 return true;
1501 } else {
1502 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
1503 return false;
1504 }
1505 return true;
1506 } else {
1507 gui_print("mkfs.f2fs binary not found, using rm -rf to wipe.\n");
1508 return Wipe_RMRF();
1509 }
1510 return false;
1511}
1512
Dees_Troy51a0e822012-09-05 15:24:24 -04001513bool TWPartition::Wipe_Data_Without_Wiping_Media() {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001514 string dir;
bigbiff bigbiff6b600f92014-01-05 18:13:43 -05001515 #ifdef HAVE_SELINUX
1516 fixPermissions perms;
1517 #endif
Dees_Troy38bd7602012-09-14 13:33:53 -04001518
1519 // This handles wiping data on devices with "sdcard" in /data/media
1520 if (!Mount(true))
1521 return false;
1522
Dees_Troy2673cec2013-04-02 20:22:16 +00001523 gui_print("Wiping data without wiping /data/media ...\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001524
1525 DIR* d;
1526 d = opendir("/data");
Dees_Troy16b74352012-11-14 22:27:31 +00001527 if (d != NULL) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001528 struct dirent* de;
1529 while ((de = readdir(d)) != NULL) {
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05001530 if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) continue;
Dees_Troy16b74352012-11-14 22:27:31 +00001531 // The media folder is the "internal sdcard"
1532 // The .layout_version file is responsible for determining whether 4.2 decides up upgrade
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001533 // the media folder for multi-user.
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05001534 //TODO: convert this to use twrpDU.cpp
Dees_Troy16b74352012-11-14 22:27:31 +00001535 if (strcmp(de->d_name, "media") == 0 || strcmp(de->d_name, ".layout_version") == 0) continue;
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05001536
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001537 dir = "/data/";
1538 dir.append(de->d_name);
Dees_Troyce675462013-01-09 19:48:21 +00001539 if (de->d_type == DT_DIR) {
1540 TWFunc::removeDir(dir, false);
bigbiff bigbiff98f1f902013-01-19 18:46:13 -05001541 } 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 +00001542 if (!unlink(dir.c_str()))
Dees_Troy2673cec2013-04-02 20:22:16 +00001543 LOGINFO("Unable to unlink '%s'\n", dir.c_str());
Dees_Troyce675462013-01-09 19:48:21 +00001544 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001545 }
1546 closedir(d);
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05001547
bigbiff bigbiff6b600f92014-01-05 18:13:43 -05001548 #ifdef HAVE_SELINUX
1549 perms.fixDataInternalContexts();
1550 #endif
1551
Dees_Troy2673cec2013-04-02 20:22:16 +00001552 gui_print("Done.\n");
Dees_Troy16b74352012-11-14 22:27:31 +00001553 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -04001554 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001555 gui_print("Dirent failed to open /data, error!\n");
Dees_Troy16b74352012-11-14 22:27:31 +00001556 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001557}
1558
1559bool TWPartition::Backup_Tar(string backup_folder) {
Dees_Troy4a2a1262012-09-18 09:33:47 -04001560 char back_name[255], split_index[5];
1561 string Full_FileName, Split_FileName, Tar_Args, Command;
Dees_Troy83bd4832013-05-04 12:39:56 +00001562 int use_compression, use_encryption = 0, index, backup_count;
Dees_Troy4a2a1262012-09-18 09:33:47 -04001563 struct stat st;
Dees_Troy7c2dec82012-09-26 09:49:14 -04001564 unsigned long long total_bsize = 0, file_size;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001565 twrpTar tar;
1566 vector <string> files;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001567
Dees_Troy43d8b002012-09-17 16:00:01 -04001568 if (!Mount(true))
1569 return false;
1570
Dees_Troya13d74f2013-03-24 08:54:55 -05001571 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Backup_Display_Name, "Backing Up");
Dees_Troy2673cec2013-04-02 20:22:16 +00001572 gui_print("Backing up %s...\n", Backup_Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001573
1574 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
Dees_Troy83bd4832013-05-04 12:39:56 +00001575 tar.use_compression = use_compression;
bigbiff bigbiff86e77bc2013-08-26 21:36:23 -04001576 //exclude Google Music Cache
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05001577 vector<string> excludedirs = du.get_absolute_dirs();
1578 for (int i = 0; i < excludedirs.size(); ++i) {
1579 tar.setexcl(excludedirs.at(i));
1580 }
Dees_Troy83bd4832013-05-04 12:39:56 +00001581#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
1582 DataManager::GetValue("tw_encrypt_backup", use_encryption);
1583 if (use_encryption && Can_Encrypt_Backup) {
1584 tar.use_encryption = use_encryption;
1585 if (Use_Userdata_Encryption)
1586 tar.userdata_encryption = use_encryption;
Ethan Yonker87af5632014-02-10 11:56:35 -06001587 string Password;
1588 DataManager::GetValue("tw_backup_password", Password);
1589 tar.setpassword(Password);
Dees_Troy83bd4832013-05-04 12:39:56 +00001590 } else {
1591 use_encryption = false;
1592 }
1593#endif
Dees_Troy43d8b002012-09-17 16:00:01 -04001594
1595 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1596 Backup_FileName = back_name;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001597 Full_FileName = backup_folder + "/" + Backup_FileName;
Dees_Troy83bd4832013-05-04 12:39:56 +00001598 tar.has_data_media = Has_Data_Media;
Dees Troye0a433a2013-12-02 04:10:37 +00001599 Full_FileName = backup_folder + "/" + Backup_FileName;
1600 tar.setdir(Backup_Path);
1601 tar.setfn(Full_FileName);
1602 tar.setsize(Backup_Size);
1603 if (tar.createTarFork() != 0)
1604 return false;
Dees_Troy43d8b002012-09-17 16:00:01 -04001605 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001606}
1607
1608bool TWPartition::Backup_DD(string backup_folder) {
igoriok87e3d932013-01-31 21:03:53 +02001609 char back_name[255], backup_size[32];
Vojtech Bocek05534202013-09-11 08:11:56 +02001610 string Full_FileName, Command, DD_BS;
Dees_Troy43d8b002012-09-17 16:00:01 -04001611 int use_compression;
1612
igoriok87e3d932013-01-31 21:03:53 +02001613 sprintf(backup_size, "%llu", Backup_Size);
1614 DD_BS = backup_size;
1615
Dees_Troyb46a6842012-09-25 11:06:46 -04001616 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
Dees_Troy2673cec2013-04-02 20:22:16 +00001617 gui_print("Backing up %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001618
1619 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1620 Backup_FileName = back_name;
1621
1622 Full_FileName = backup_folder + "/" + Backup_FileName;
1623
igoriok87e3d932013-01-31 21:03:53 +02001624 Command = "dd if=" + Actual_Block_Device + " of='" + Full_FileName + "'" + " bs=" + DD_BS + "c count=1";
Dees_Troy2673cec2013-04-02 20:22:16 +00001625 LOGINFO("Backup command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001626 TWFunc::Exec_Cmd(Command);
Dees_Troyc154ac22012-10-12 15:36:47 -04001627 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001628 LOGERR("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001629 return false;
1630 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001631 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001632}
1633
1634bool TWPartition::Backup_Dump_Image(string backup_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001635 char back_name[255];
Vojtech Bocek05534202013-09-11 08:11:56 +02001636 string Full_FileName, Command;
Dees_Troy43d8b002012-09-17 16:00:01 -04001637 int use_compression;
1638
Dees_Troyb46a6842012-09-25 11:06:46 -04001639 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
Dees_Troy2673cec2013-04-02 20:22:16 +00001640 gui_print("Backing up %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001641
1642 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1643 Backup_FileName = back_name;
1644
1645 Full_FileName = backup_folder + "/" + Backup_FileName;
1646
1647 Command = "dump_image " + MTD_Name + " '" + Full_FileName + "'";
Dees_Troy2673cec2013-04-02 20:22:16 +00001648 LOGINFO("Backup command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001649 TWFunc::Exec_Cmd(Command);
Dees_Troy7c2dec82012-09-26 09:49:14 -04001650 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
1651 // 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 +00001652 LOGERR("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001653 return false;
1654 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001655 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001656}
1657
Gary Peck43acadf2012-11-21 21:19:01 -08001658bool TWPartition::Restore_Tar(string restore_folder, string Restore_File_System) {
1659 string Full_FileName, Command;
Dees_Troy4a2a1262012-09-18 09:33:47 -04001660 int index = 0;
1661 char split_index[5];
Dees_Troy43d8b002012-09-17 16:00:01 -04001662
Dees_Troye58d5262012-09-21 12:27:57 -04001663 if (Has_Android_Secure) {
Dees_Troye58d5262012-09-21 12:27:57 -04001664 if (!Wipe_AndSec())
1665 return false;
Gary Peck43acadf2012-11-21 21:19:01 -08001666 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001667 gui_print("Wiping %s...\n", Display_Name.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001668 if (!Wipe(Restore_File_System))
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001669 return false;
Dees_Troye58d5262012-09-21 12:27:57 -04001670 }
Dees_Troya13d74f2013-03-24 08:54:55 -05001671 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Backup_Display_Name, "Restoring");
Dees_Troy2673cec2013-04-02 20:22:16 +00001672 gui_print("Restoring %s...\n", Backup_Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001673
1674 if (!Mount(true))
1675 return false;
1676
Dees_Troy4a2a1262012-09-18 09:33:47 -04001677 Full_FileName = restore_folder + "/" + Backup_FileName;
Ethan Yonker87af5632014-02-10 11:56:35 -06001678 twrpTar tar;
1679 tar.setdir(Backup_Path);
1680 tar.setfn(Full_FileName);
1681 tar.backup_name = Backup_Name;
1682#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
1683 string Password;
1684 DataManager::GetValue("tw_restore_password", Password);
1685 if (!Password.empty())
1686 tar.setpassword(Password);
1687#endif
1688 if (tar.extractTarFork() != 0)
1689 return false;
Dees_Troy43d8b002012-09-17 16:00:01 -04001690 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001691}
1692
1693bool TWPartition::Restore_DD(string restore_folder) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001694 string Full_FileName, Command;
Dees_Troy43d8b002012-09-17 16:00:01 -04001695
Dees_Troyda8b55a2012-12-12 19:18:30 +00001696 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
Dees_Troy43d8b002012-09-17 16:00:01 -04001697 Full_FileName = restore_folder + "/" + Backup_FileName;
Gary Peck15e623d2012-11-21 21:07:58 -08001698
1699 if (!Find_Partition_Size()) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001700 LOGERR("Unable to find partition size for '%s'\n", Mount_Point.c_str());
Gary Peck15e623d2012-11-21 21:07:58 -08001701 return false;
1702 }
1703 unsigned long long backup_size = TWFunc::Get_File_Size(Full_FileName);
1704 if (backup_size > Size) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001705 LOGERR("Size (%iMB) of backup '%s' is larger than target device '%s' (%iMB)\n",
Gary Peck15e623d2012-11-21 21:07:58 -08001706 (int)(backup_size / 1048576LLU), Full_FileName.c_str(),
1707 Actual_Block_Device.c_str(), (int)(Size / 1048576LLU));
1708 return false;
1709 }
1710
Dees_Troy2673cec2013-04-02 20:22:16 +00001711 gui_print("Restoring %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001712 Command = "dd bs=4096 if='" + Full_FileName + "' of=" + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001713 LOGINFO("Restore command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001714 TWFunc::Exec_Cmd(Command);
Dees_Troy43d8b002012-09-17 16:00:01 -04001715 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001716}
1717
1718bool TWPartition::Restore_Flash_Image(string restore_folder) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001719 string Full_FileName, Command;
Dees_Troy43d8b002012-09-17 16:00:01 -04001720
Dees_Troy2673cec2013-04-02 20:22:16 +00001721 gui_print("Restoring %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001722 Full_FileName = restore_folder + "/" + Backup_FileName;
1723 // Sometimes flash image doesn't like to flash due to the first 2KB matching, so we erase first to ensure that it flashes
1724 Command = "erase_image " + MTD_Name;
Dees_Troy2673cec2013-04-02 20:22:16 +00001725 LOGINFO("Erase command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001726 TWFunc::Exec_Cmd(Command);
Dees_Troy43d8b002012-09-17 16:00:01 -04001727 Command = "flash_image " + MTD_Name + " '" + Full_FileName + "'";
Dees_Troy2673cec2013-04-02 20:22:16 +00001728 LOGINFO("Restore command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001729 TWFunc::Exec_Cmd(Command);
Dees_Troy43d8b002012-09-17 16:00:01 -04001730 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001731}
Dees_Troy5bf43922012-09-07 16:07:55 -04001732
1733bool TWPartition::Update_Size(bool Display_Error) {
Dees_Troy0550cfb2012-10-13 11:56:13 -04001734 bool ret = false, Was_Already_Mounted = false;
Dees_Troy51127312012-09-08 13:08:49 -04001735
Dees_Troyab10ee22012-09-21 14:27:30 -04001736 if (!Can_Be_Mounted && !Is_Encrypted)
Dees_Troy5bf43922012-09-07 16:07:55 -04001737 return false;
1738
Dees_Troy0550cfb2012-10-13 11:56:13 -04001739 Was_Already_Mounted = Is_Mounted();
Dees_Troy38bd7602012-09-14 13:33:53 -04001740 if (Removable || Is_Encrypted) {
1741 if (!Mount(false))
1742 return true;
1743 } else if (!Mount(Display_Error))
Dees_Troy5bf43922012-09-07 16:07:55 -04001744 return false;
Dees_Troy51127312012-09-08 13:08:49 -04001745
1746 ret = Get_Size_Via_statfs(Display_Error);
Dees_Troy0550cfb2012-10-13 11:56:13 -04001747 if (!ret || Size == 0) {
1748 if (!Get_Size_Via_df(Display_Error)) {
1749 if (!Was_Already_Mounted)
1750 UnMount(false);
Dees_Troy51127312012-09-08 13:08:49 -04001751 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04001752 }
1753 }
Dees_Troy51127312012-09-08 13:08:49 -04001754
Dees_Troy5bf43922012-09-07 16:07:55 -04001755 if (Has_Data_Media) {
1756 if (Mount(Display_Error)) {
Dees_Troy51127312012-09-08 13:08:49 -04001757 unsigned long long data_media_used, actual_data;
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05001758 du.add_relative_dir("media");
1759 Used = du.Get_Folder_Size("/data");
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05001760 du.clear_relative_dir("media");
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05001761 Backup_Size = Used;
1762 int bak = (int)(Used / 1048576LLU);
Dees_Troy51127312012-09-08 13:08:49 -04001763 int fre = (int)(Free / 1048576LLU);
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05001764 LOGINFO("Data backup size is %iMB, free: %iMB.\n", bak, fre);
Dees_Troy0550cfb2012-10-13 11:56:13 -04001765 } else {
1766 if (!Was_Already_Mounted)
1767 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04001768 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04001769 }
Dees_Troye58d5262012-09-21 12:27:57 -04001770 } else if (Has_Android_Secure) {
1771 if (Mount(Display_Error))
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05001772 Backup_Size = du.Get_Folder_Size(Backup_Path);
Dees_Troy0550cfb2012-10-13 11:56:13 -04001773 else {
1774 if (!Was_Already_Mounted)
1775 UnMount(false);
Dees_Troye58d5262012-09-21 12:27:57 -04001776 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04001777 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001778 }
Dees_Troy0550cfb2012-10-13 11:56:13 -04001779 if (!Was_Already_Mounted)
1780 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04001781 return true;
Dees_Troy51127312012-09-08 13:08:49 -04001782}
Dees_Troy38bd7602012-09-14 13:33:53 -04001783
1784void TWPartition::Find_Actual_Block_Device(void) {
1785 if (Is_Decrypted) {
1786 Actual_Block_Device = Decrypted_Block_Device;
Dees_Troy43d8b002012-09-17 16:00:01 -04001787 if (TWFunc::Path_Exists(Primary_Block_Device))
Dees_Troy38bd7602012-09-14 13:33:53 -04001788 Is_Present = true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001789 } else if (TWFunc::Path_Exists(Primary_Block_Device)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001790 Is_Present = true;
1791 Actual_Block_Device = Primary_Block_Device;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001792 return;
1793 }
1794 if (Is_Decrypted) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001795 } else if (!Alternate_Block_Device.empty() && TWFunc::Path_Exists(Alternate_Block_Device)) {
Dees_Troy3f04d032012-10-07 18:20:09 -04001796 Actual_Block_Device = Alternate_Block_Device;
Dees_Troy38bd7602012-09-14 13:33:53 -04001797 Is_Present = true;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001798 } else {
Dees_Troy38bd7602012-09-14 13:33:53 -04001799 Is_Present = false;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001800 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001801}
1802
1803void TWPartition::Recreate_Media_Folder(void) {
1804 string Command;
1805
bigbiff bigbiff6b600f92014-01-05 18:13:43 -05001806 #ifdef HAVE_SELINUX
1807 fixPermissions perms;
1808 #endif
1809
Dees_Troy38bd7602012-09-14 13:33:53 -04001810 if (!Mount(true)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001811 LOGERR("Unable to recreate /data/media folder.\n");
Dees_Troyb46a6842012-09-25 11:06:46 -04001812 } else if (!TWFunc::Path_Exists("/data/media")) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001813 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Dees_Troy2673cec2013-04-02 20:22:16 +00001814 LOGINFO("Recreating /data/media folder.\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001815 mkdir("/data/media", S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
bigbiff bigbiff6b600f92014-01-05 18:13:43 -05001816 #ifdef HAVE_SELINUX
1817 perms.fixDataInternalContexts();
1818 #endif
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001819 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Dees_Troy38bd7602012-09-14 13:33:53 -04001820 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001821}
Dees_Troye58d5262012-09-21 12:27:57 -04001822
1823void TWPartition::Recreate_AndSec_Folder(void) {
Dees_Troye58d5262012-09-21 12:27:57 -04001824 if (!Has_Android_Secure)
1825 return;
Dees_Troy2673cec2013-04-02 20:22:16 +00001826 LOGINFO("Creating %s: %s\n", Backup_Display_Name.c_str(), Symlink_Path.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -04001827 if (!Mount(true)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001828 LOGERR("Unable to recreate %s folder.\n", Backup_Name.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -04001829 } else if (!TWFunc::Path_Exists(Symlink_Path)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001830 LOGINFO("Recreating %s folder.\n", Backup_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001831 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
1832 mkdir(Symlink_Path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
1833 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04001834 }
1835}