blob: 74271922f1e1ac568ab7c3c195035fccd86fe43a [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"
Ethan Yonkerf27497f2014-02-09 11:48:33 -060056#include <selinux/label.h>
bigbiff bigbiffc49d7062013-10-11 20:28:00 -040057#endif
Dees_Troy51a0e822012-09-05 15:24:24 -040058
bigbiff bigbiff9c754052013-01-09 09:09:08 -050059using namespace std;
60
Dees_Troya95f55c2013-08-17 13:14:43 +000061extern struct selabel_handle *selinux_handle;
62
Hashcode62bd9e02013-11-19 21:59:42 -080063struct flag_list {
64 const char *name;
65 unsigned flag;
66};
67
68static struct flag_list mount_flags[] = {
69 { "noatime", MS_NOATIME },
70 { "noexec", MS_NOEXEC },
71 { "nosuid", MS_NOSUID },
72 { "nodev", MS_NODEV },
73 { "nodiratime", MS_NODIRATIME },
74 { "ro", MS_RDONLY },
75 { "rw", 0 },
76 { "remount", MS_REMOUNT },
77 { "bind", MS_BIND },
78 { "rec", MS_REC },
Dees Troyc4bc30e2014-02-03 15:04:19 +000079#ifdef MS_UNBINDABLE
Hashcode62bd9e02013-11-19 21:59:42 -080080 { "unbindable", MS_UNBINDABLE },
Dees Troyc4bc30e2014-02-03 15:04:19 +000081#endif
82#ifdef MS_PRIVATE
Hashcode62bd9e02013-11-19 21:59:42 -080083 { "private", MS_PRIVATE },
Dees Troyc4bc30e2014-02-03 15:04:19 +000084#endif
85#ifdef MS_SLAVE
Hashcode62bd9e02013-11-19 21:59:42 -080086 { "slave", MS_SLAVE },
Dees Troyc4bc30e2014-02-03 15:04:19 +000087#endif
88#ifdef MS_SHARED
Hashcode62bd9e02013-11-19 21:59:42 -080089 { "shared", MS_SHARED },
Dees Troyc4bc30e2014-02-03 15:04:19 +000090#endif
Hashcode62bd9e02013-11-19 21:59:42 -080091 { "sync", MS_SYNCHRONOUS },
92 { "defaults", 0 },
93 { 0, 0 },
94};
95
Dees_Troy51a0e822012-09-05 15:24:24 -040096TWPartition::TWPartition(void) {
97 Can_Be_Mounted = false;
98 Can_Be_Wiped = false;
Dees_Troya13d74f2013-03-24 08:54:55 -050099 Can_Be_Backed_Up = false;
Vojtech Bocek1dc30982013-08-30 21:49:30 +0200100 Use_Rm_Rf = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400101 Wipe_During_Factory_Reset = false;
102 Wipe_Available_in_GUI = false;
103 Is_SubPartition = false;
Dees_Troy2691f9d2012-09-24 11:15:49 -0400104 Has_SubPartition = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400105 SubPartition_Of = "";
106 Symlink_Path = "";
107 Symlink_Mount_Point = "";
108 Mount_Point = "";
Dees_Troye58d5262012-09-21 12:27:57 -0400109 Backup_Path = "";
Dees_Troy38bd7602012-09-14 13:33:53 -0400110 Actual_Block_Device = "";
111 Primary_Block_Device = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400112 Alternate_Block_Device = "";
113 Removable = false;
114 Is_Present = false;
115 Length = 0;
116 Size = 0;
117 Used = 0;
118 Free = 0;
119 Backup_Size = 0;
120 Can_Be_Encrypted = false;
121 Is_Encrypted = false;
122 Is_Decrypted = false;
123 Decrypted_Block_Device = "";
124 Display_Name = "";
Dees_Troya13d74f2013-03-24 08:54:55 -0500125 Backup_Display_Name = "";
126 Storage_Name = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400127 Backup_Name = "";
Dees_Troy63c8df72012-09-10 14:02:05 -0400128 Backup_FileName = "";
Dees_Troy38bd7602012-09-14 13:33:53 -0400129 MTD_Name = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400130 Backup_Method = NONE;
Dees_Troy83bd4832013-05-04 12:39:56 +0000131 Can_Encrypt_Backup = false;
132 Use_Userdata_Encryption = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400133 Has_Data_Media = false;
Dees_Troye58d5262012-09-21 12:27:57 -0400134 Has_Android_Secure = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400135 Is_Storage = false;
Dees_Troya13d74f2013-03-24 08:54:55 -0500136 Is_Settings_Storage = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400137 Storage_Path = "";
138 Current_File_System = "";
139 Fstab_File_System = "";
Hashcode62bd9e02013-11-19 21:59:42 -0800140 Mount_Flags = 0;
141 Mount_Options = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400142 Format_Block_Size = 0;
Dees_Troy68cab492012-12-12 19:29:35 +0000143 Ignore_Blkid = false;
Dees_Troy16c2b312013-01-15 16:51:18 +0000144 Retain_Layout_Version = false;
Dees_Troy85f44ed2013-01-09 18:42:36 +0000145#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
146 EcryptFS_Password = "";
147#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400148}
149
150TWPartition::~TWPartition(void) {
151 // Do nothing
152}
153
Dees_Troy5bf43922012-09-07 16:07:55 -0400154bool TWPartition::Process_Fstab_Line(string Line, bool Display_Error) {
155 char full_line[MAX_FSTAB_LINE_LENGTH], item[MAX_FSTAB_LINE_LENGTH];
156 int line_len = Line.size(), index = 0, item_index = 0;
157 char* ptr;
Dees_Troy51127312012-09-08 13:08:49 -0400158 string Flags;
Dees_Troy5bf43922012-09-07 16:07:55 -0400159 strncpy(full_line, Line.c_str(), line_len);
Dees_Troya13d74f2013-03-24 08:54:55 -0500160 bool skip = false;
Dees_Troy5bf43922012-09-07 16:07:55 -0400161
Dees_Troy51127312012-09-08 13:08:49 -0400162 for (index = 0; index < line_len; index++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500163 if (full_line[index] == 34)
164 skip = !skip;
165 if (!skip && full_line[index] <= 32)
Dees_Troy5bf43922012-09-07 16:07:55 -0400166 full_line[index] = '\0';
Dees_Troy5bf43922012-09-07 16:07:55 -0400167 }
Dees_Troy7c2dec82012-09-26 09:49:14 -0400168 Mount_Point = full_line;
Dees_Troy2673cec2013-04-02 20:22:16 +0000169 LOGINFO("Processing '%s'\n", Mount_Point.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -0400170 Backup_Path = Mount_Point;
Dees_Troya13d74f2013-03-24 08:54:55 -0500171 Storage_Path = Mount_Point;
Dees_Troy70737fa2013-04-08 13:19:20 +0000172 Display_Name = full_line + 1;
173 Backup_Display_Name = Display_Name;
174 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400175 index = Mount_Point.size();
176 while (index < line_len) {
177 while (index < line_len && full_line[index] == '\0')
178 index++;
179 if (index >= line_len)
180 continue;
181 ptr = full_line + index;
182 if (item_index == 0) {
183 // File System
184 Fstab_File_System = ptr;
185 Current_File_System = ptr;
186 item_index++;
187 } else if (item_index == 1) {
188 // Primary Block Device
Dees_Troy38bd7602012-09-14 13:33:53 -0400189 if (Fstab_File_System == "mtd" || Fstab_File_System == "yaffs2") {
Dees_Troy094207a2012-09-26 12:00:39 -0400190 MTD_Name = ptr;
191 Find_MTD_Block_Device(MTD_Name);
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400192 } else if (Fstab_File_System == "bml") {
193 if (Mount_Point == "/boot")
194 MTD_Name = "boot";
195 else if (Mount_Point == "/recovery")
196 MTD_Name = "recovery";
197 Primary_Block_Device = ptr;
198 if (*ptr != '/')
Dees_Troy2673cec2013-04-02 20:22:16 +0000199 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 -0400200 } else if (*ptr != '/') {
Dees_Troy5bf43922012-09-07 16:07:55 -0400201 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000202 LOGERR("Invalid block device on '%s', '%s', %i\n", Line.c_str(), ptr, index);
Dees_Troy5bf43922012-09-07 16:07:55 -0400203 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000204 LOGINFO("Invalid block device on '%s', '%s', %i\n", Line.c_str(), ptr, index);
Dees_Troy5bf43922012-09-07 16:07:55 -0400205 return 0;
Dees_Troy38bd7602012-09-14 13:33:53 -0400206 } else {
207 Primary_Block_Device = ptr;
208 Find_Real_Block_Device(Primary_Block_Device, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400209 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400210 item_index++;
211 } else if (item_index > 1) {
212 if (*ptr == '/') {
213 // Alternate Block Device
214 Alternate_Block_Device = ptr;
215 Find_Real_Block_Device(Alternate_Block_Device, Display_Error);
216 } else if (strlen(ptr) > 7 && strncmp(ptr, "length=", 7) == 0) {
217 // Partition length
218 ptr += 7;
219 Length = atoi(ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400220 } else if (strlen(ptr) > 6 && strncmp(ptr, "flags=", 6) == 0) {
221 // Custom flags, save for later so that new values aren't overwritten by defaults
222 ptr += 6;
223 Flags = ptr;
Dees_Troy68cab492012-12-12 19:29:35 +0000224 Process_Flags(Flags, Display_Error);
Dees_Troy38bd7602012-09-14 13:33:53 -0400225 } else if (strlen(ptr) == 4 && (strncmp(ptr, "NULL", 4) == 0 || strncmp(ptr, "null", 4) == 0 || strncmp(ptr, "null", 4) == 0)) {
226 // Do nothing
Dees_Troy5bf43922012-09-07 16:07:55 -0400227 } else {
228 // Unhandled data
Dees_Troy2673cec2013-04-02 20:22:16 +0000229 LOGINFO("Unhandled fstab information: '%s', %i, line: '%s'\n", ptr, index, Line.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400230 }
231 }
232 while (index < line_len && full_line[index] != '\0')
233 index++;
234 }
235
236 if (!Is_File_System(Fstab_File_System) && !Is_Image(Fstab_File_System)) {
237 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000238 LOGERR("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400239 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000240 LOGINFO("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400241 return 0;
242 } else if (Is_File_System(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400243 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400244 Setup_File_System(Display_Error);
245 if (Mount_Point == "/system") {
246 Display_Name = "System";
Dees_Troya13d74f2013-03-24 08:54:55 -0500247 Backup_Display_Name = Display_Name;
248 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400249 Wipe_Available_in_GUI = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500250 Can_Be_Backed_Up = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400251 } else if (Mount_Point == "/data") {
252 Display_Name = "Data";
Dees_Troya13d74f2013-03-24 08:54:55 -0500253 Backup_Display_Name = Display_Name;
254 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400255 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400256 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500257 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000258 Can_Encrypt_Backup = true;
259 Use_Userdata_Encryption = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400260#ifdef RECOVERY_SDCARD_ON_DATA
Dees_Troya13d74f2013-03-24 08:54:55 -0500261 Storage_Name = "Internal Storage";
Dees_Troy5bf43922012-09-07 16:07:55 -0400262 Has_Data_Media = true;
Dees_Troy51127312012-09-08 13:08:49 -0400263 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500264 Is_Settings_Storage = true;
Dees_Troy51127312012-09-08 13:08:49 -0400265 Storage_Path = "/data/media";
Dees_Troy16b74352012-11-14 22:27:31 +0000266 Symlink_Path = Storage_Path;
Dees_Troy657c3092012-09-10 20:32:10 -0400267 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
268 Make_Dir("/emmc", Display_Error);
Dees_Troy657c3092012-09-10 20:32:10 -0400269 Symlink_Mount_Point = "/emmc";
270 } else {
271 Make_Dir("/sdcard", Display_Error);
Dees_Troy657c3092012-09-10 20:32:10 -0400272 Symlink_Mount_Point = "/sdcard";
273 }
Dees_Troy16b74352012-11-14 22:27:31 +0000274 if (Mount(false) && TWFunc::Path_Exists("/data/media/0")) {
275 Storage_Path = "/data/media/0";
276 Symlink_Path = Storage_Path;
277 DataManager::SetValue(TW_INTERNAL_PATH, "/data/media/0");
278 UnMount(true);
279 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400280#endif
281#ifdef TW_INCLUDE_CRYPTO
282 Can_Be_Encrypted = true;
Dees_Troy657c3092012-09-10 20:32:10 -0400283 char crypto_blkdev[255];
284 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
285 if (strcmp(crypto_blkdev, "error") != 0) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400286 DataManager::SetValue(TW_DATA_BLK_DEVICE, Primary_Block_Device);
Dees_Troy657c3092012-09-10 20:32:10 -0400287 DataManager::SetValue(TW_IS_DECRYPTED, 1);
288 Is_Encrypted = true;
289 Is_Decrypted = true;
290 Decrypted_Block_Device = crypto_blkdev;
Dees_Troy2673cec2013-04-02 20:22:16 +0000291 LOGINFO("Data already decrypted, new block device: '%s'\n", crypto_blkdev);
Dees_Troy657c3092012-09-10 20:32:10 -0400292 } else if (!Mount(false)) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400293 Is_Encrypted = true;
294 Is_Decrypted = false;
Gary Peck82599a82012-11-21 16:23:12 -0800295 Can_Be_Mounted = false;
296 Current_File_System = "emmc";
297 Setup_Image(Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400298 DataManager::SetValue(TW_IS_ENCRYPTED, 1);
299 DataManager::SetValue(TW_CRYPTO_PASSWORD, "");
300 DataManager::SetValue("tw_crypto_display", "");
Gary Peck82599a82012-11-21 16:23:12 -0800301 } else {
302 // Filesystem is not encrypted and the mount
303 // succeeded, so get it back to the original
304 // unmounted state
305 UnMount(false);
Dees_Troy51127312012-09-08 13:08:49 -0400306 }
Dees_Troy9b21af72012-10-01 15:51:46 -0400307 #ifdef RECOVERY_SDCARD_ON_DATA
308 if (!Is_Encrypted || (Is_Encrypted && Is_Decrypted))
309 Recreate_Media_Folder();
310 #endif
311#else
312 #ifdef RECOVERY_SDCARD_ON_DATA
313 Recreate_Media_Folder();
314 #endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400315#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400316 } else if (Mount_Point == "/cache") {
317 Display_Name = "Cache";
Dees_Troya13d74f2013-03-24 08:54:55 -0500318 Backup_Display_Name = Display_Name;
319 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400320 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400321 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500322 Can_Be_Backed_Up = true;
Dees_Troyce2fe772012-09-28 12:34:33 -0400323 if (Mount(false) && !TWFunc::Path_Exists("/cache/recovery/.")) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000324 LOGINFO("Recreating /cache/recovery folder.\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500325 if (mkdir("/cache/recovery", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0)
326 return -1;
Dees_Troyb46a6842012-09-25 11:06:46 -0400327 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400328 } else if (Mount_Point == "/datadata") {
Dees_Troy51127312012-09-08 13:08:49 -0400329 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400330 Display_Name = "DataData";
Dees_Troya13d74f2013-03-24 08:54:55 -0500331 Backup_Display_Name = Display_Name;
332 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400333 Is_SubPartition = true;
334 SubPartition_Of = "/data";
Dees_Troy5bf43922012-09-07 16:07:55 -0400335 DataManager::SetValue(TW_HAS_DATADATA, 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500336 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000337 Can_Encrypt_Backup = true;
338 Use_Userdata_Encryption = false; // This whole partition should be encrypted
Dees_Troy5bf43922012-09-07 16:07:55 -0400339 } else if (Mount_Point == "/sd-ext") {
Dees_Troy51127312012-09-08 13:08:49 -0400340 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400341 Display_Name = "SD-Ext";
Dees_Troya13d74f2013-03-24 08:54:55 -0500342 Backup_Display_Name = Display_Name;
343 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400344 Wipe_Available_in_GUI = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400345 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500346 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000347 Can_Encrypt_Backup = true;
348 Use_Userdata_Encryption = true;
Dees_Troy2c50e182012-09-26 20:05:28 -0400349 } else if (Mount_Point == "/boot") {
350 Display_Name = "Boot";
Dees_Troya13d74f2013-03-24 08:54:55 -0500351 Backup_Display_Name = Display_Name;
Dees_Troy2c50e182012-09-26 20:05:28 -0400352 DataManager::SetValue("tw_boot_is_mountable", 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500353 Can_Be_Backed_Up = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400354 }
355#ifdef TW_EXTERNAL_STORAGE_PATH
356 if (Mount_Point == EXPAND(TW_EXTERNAL_STORAGE_PATH)) {
357 Is_Storage = true;
358 Storage_Path = EXPAND(TW_EXTERNAL_STORAGE_PATH);
Dees_Troyc51f1f92012-09-20 15:32:13 -0400359 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500360 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400361#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000362 if (Mount_Point == "/sdcard" || Mount_Point == "/external_sd" || Mount_Point == "/external_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400363 Is_Storage = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400364 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500365 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400366#endif
Dees_Troyb05ddee2013-01-28 20:24:50 +0000367 }
Dees_Troy8170a922012-09-18 15:40:25 -0400368#ifdef TW_INTERNAL_STORAGE_PATH
369 if (Mount_Point == EXPAND(TW_INTERNAL_STORAGE_PATH)) {
370 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500371 Is_Settings_Storage = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400372 Storage_Path = EXPAND(TW_INTERNAL_STORAGE_PATH);
Dees_Troya13d74f2013-03-24 08:54:55 -0500373 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400374 }
375#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000376 if (Mount_Point == "/emmc" || Mount_Point == "/internal_sd" || Mount_Point == "/internal_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400377 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500378 Is_Settings_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500379 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400380 }
381#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400382 } else if (Is_Image(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400383 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400384 Setup_Image(Display_Error);
Dees_Troya13d74f2013-03-24 08:54:55 -0500385 if (Mount_Point == "/boot") {
386 Display_Name = "Boot";
387 Backup_Display_Name = Display_Name;
388 Can_Be_Backed_Up = true;
389 } else if (Mount_Point == "/recovery") {
390 Display_Name = "Recovery";
391 Backup_Display_Name = Display_Name;
Dees_Troya13d74f2013-03-24 08:54:55 -0500392 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400393 }
394
Dees_Troy51127312012-09-08 13:08:49 -0400395 // Process any custom flags
396 if (Flags.size() > 0)
397 Process_Flags(Flags, Display_Error);
Dees_Troy51127312012-09-08 13:08:49 -0400398 return true;
399}
400
Hashcode62bd9e02013-11-19 21:59:42 -0800401bool TWPartition::Process_FS_Flags(string& Options, int Flags) {
402 int i;
403 char *p;
404 char *savep;
405 char fs_options[250];
406
407 strlcpy(fs_options, Options.c_str(), sizeof(fs_options));
408 Options = "";
409
410 p = strtok_r(fs_options, ",", &savep);
411 while (p) {
412 /* Look for the flag "p" in the flag list "fl"
413 * If not found, the loop exits with fl[i].name being null.
414 */
415 for (i = 0; mount_flags[i].name; i++) {
416 if (strncmp(p, mount_flags[i].name, strlen(mount_flags[i].name)) == 0) {
417 Flags |= mount_flags[i].flag;
418 break;
419 }
420 }
421
422 if (!mount_flags[i].name) {
423 if (Options.size() > 0)
424 Options += ",";
425 Options += p;
426 }
427 p = strtok_r(NULL, ",", &savep);
428 }
429
430 return true;
431}
432
Dees_Troy51127312012-09-08 13:08:49 -0400433bool TWPartition::Process_Flags(string Flags, bool Display_Error) {
434 char flags[MAX_FSTAB_LINE_LENGTH];
Dees_Troya13d74f2013-03-24 08:54:55 -0500435 int flags_len, index = 0, ptr_len;
Dees_Troy51127312012-09-08 13:08:49 -0400436 char* ptr;
Dees_Troya13d74f2013-03-24 08:54:55 -0500437 bool skip = false, has_display_name = false, has_storage_name = false, has_backup_name = false;
Dees_Troy51127312012-09-08 13:08:49 -0400438
439 strcpy(flags, Flags.c_str());
440 flags_len = Flags.size();
441 for (index = 0; index < flags_len; index++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500442 if (flags[index] == 34)
443 skip = !skip;
444 if (!skip && flags[index] == ';')
Dees_Troy51127312012-09-08 13:08:49 -0400445 flags[index] = '\0';
446 }
447
448 index = 0;
449 while (index < flags_len) {
450 while (index < flags_len && flags[index] == '\0')
451 index++;
452 if (index >= flags_len)
453 continue;
454 ptr = flags + index;
Dees_Troya13d74f2013-03-24 08:54:55 -0500455 ptr_len = strlen(ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400456 if (strcmp(ptr, "removable") == 0) {
457 Removable = true;
Ethan Yonker06c3f932014-02-02 22:11:14 -0600458 } else if (strncmp(ptr, "storage", 7) == 0) {
459 if (ptr_len == 7) {
460 LOGINFO("ptr_len is 7, storage set to true\n");
461 Is_Storage = true;
462 } else if (ptr_len == 9) {
463 ptr += 9;
464 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y') {
465 LOGINFO("storage set to true\n");
466 Is_Storage = true;
467 } else {
468 LOGINFO("storage set to false\n");
469 Is_Storage = false;
470 }
471 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500472 } else if (strcmp(ptr, "settingsstorage") == 0) {
473 Is_Storage = true;
Dees_Troy63c8df72012-09-10 14:02:05 -0400474 } else if (strcmp(ptr, "canbewiped") == 0) {
475 Can_Be_Wiped = true;
Hashcodedabfd492013-08-29 22:45:30 -0700476 } else if (strcmp(ptr, "usermrf") == 0) {
477 Use_Rm_Rf = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500478 } else if (ptr_len > 7 && strncmp(ptr, "backup=", 7) == 0) {
479 ptr += 7;
480 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y')
481 Can_Be_Backed_Up = true;
482 else
483 Can_Be_Backed_Up = false;
Dees_Troy63c8df72012-09-10 14:02:05 -0400484 } else if (strcmp(ptr, "wipeingui") == 0) {
485 Can_Be_Wiped = true;
486 Wipe_Available_in_GUI = true;
487 } else if (strcmp(ptr, "wipeduringfactoryreset") == 0) {
488 Can_Be_Wiped = true;
489 Wipe_Available_in_GUI = true;
490 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500491 } else if (ptr_len > 15 && strncmp(ptr, "subpartitionof=", 15) == 0) {
Dees_Troy2c4c26f2013-01-28 15:26:43 +0000492 ptr += 15;
Dees_Troy51127312012-09-08 13:08:49 -0400493 Is_SubPartition = true;
494 SubPartition_Of = ptr;
Dees_Troy68cab492012-12-12 19:29:35 +0000495 } else if (strcmp(ptr, "ignoreblkid") == 0) {
496 Ignore_Blkid = true;
Dees_Troy16c2b312013-01-15 16:51:18 +0000497 } else if (strcmp(ptr, "retainlayoutversion") == 0) {
498 Retain_Layout_Version = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500499 } else if (ptr_len > 8 && strncmp(ptr, "symlink=", 8) == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400500 ptr += 8;
501 Symlink_Path = ptr;
Dees_Troya13d74f2013-03-24 08:54:55 -0500502 } else if (ptr_len > 8 && strncmp(ptr, "display=", 8) == 0) {
503 has_display_name = true;
Dees_Troy51127312012-09-08 13:08:49 -0400504 ptr += 8;
Dees_Troya13d74f2013-03-24 08:54:55 -0500505 if (*ptr == '\"') ptr++;
Dees_Troy51127312012-09-08 13:08:49 -0400506 Display_Name = ptr;
Dees_Troya13d74f2013-03-24 08:54:55 -0500507 if (Display_Name.substr(Display_Name.size() - 1, 1) == "\"") {
508 Display_Name.resize(Display_Name.size() - 1);
509 }
510 } else if (ptr_len > 11 && strncmp(ptr, "storagename=", 11) == 0) {
511 has_storage_name = true;
512 ptr += 11;
513 if (*ptr == '\"') ptr++;
514 Storage_Name = ptr;
515 if (Storage_Name.substr(Storage_Name.size() - 1, 1) == "\"") {
516 Storage_Name.resize(Storage_Name.size() - 1);
517 }
518 } else if (ptr_len > 11 && strncmp(ptr, "backupname=", 10) == 0) {
519 has_backup_name = true;
520 ptr += 10;
521 if (*ptr == '\"') ptr++;
522 Backup_Display_Name = ptr;
523 if (Backup_Display_Name.substr(Backup_Display_Name.size() - 1, 1) == "\"") {
524 Backup_Display_Name.resize(Backup_Display_Name.size() - 1);
525 }
526 } else if (ptr_len > 10 && strncmp(ptr, "blocksize=", 10) == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400527 ptr += 10;
528 Format_Block_Size = atoi(ptr);
Dees_Troya13d74f2013-03-24 08:54:55 -0500529 } else if (ptr_len > 7 && strncmp(ptr, "length=", 7) == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400530 ptr += 7;
531 Length = atoi(ptr);
Dees_Troy83bd4832013-05-04 12:39:56 +0000532 } else if (ptr_len > 17 && strncmp(ptr, "canencryptbackup=", 17) == 0) {
533 ptr += 17;
534 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y')
535 Can_Encrypt_Backup = true;
536 else
537 Can_Encrypt_Backup = false;
538 } else if (ptr_len > 21 && strncmp(ptr, "userdataencryptbackup=", 21) == 0) {
539 ptr += 21;
540 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y') {
541 Can_Encrypt_Backup = true;
542 Use_Userdata_Encryption = true;
543 } else {
544 Use_Userdata_Encryption = false;
545 }
Hashcode62bd9e02013-11-19 21:59:42 -0800546 } else if (ptr_len > 8 && strncmp(ptr, "fsflags=", 8) == 0) {
547 ptr += 8;
548 if (*ptr == '\"') ptr++;
549
550 Mount_Options = ptr;
551 if (Mount_Options.substr(Mount_Options.size() - 1, 1) == "\"") {
552 Mount_Options.resize(Mount_Options.size() - 1);
553 }
554 Process_FS_Flags(Mount_Options, Mount_Flags);
Dees_Troy51127312012-09-08 13:08:49 -0400555 } else {
556 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000557 LOGERR("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400558 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000559 LOGINFO("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400560 }
561 while (index < flags_len && flags[index] != '\0')
562 index++;
563 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500564 if (has_display_name && !has_storage_name)
565 Storage_Name = Display_Name;
566 if (!has_display_name && has_storage_name)
567 Display_Name = Storage_Name;
Dees_Troy74fb2e92013-04-15 14:35:47 +0000568 if (has_display_name && !has_backup_name && Backup_Display_Name != "Android Secure")
Dees_Troya13d74f2013-03-24 08:54:55 -0500569 Backup_Display_Name = Display_Name;
570 if (!has_display_name && has_backup_name)
571 Display_Name = Backup_Display_Name;
Dees_Troy51127312012-09-08 13:08:49 -0400572 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400573}
574
Dees_Troy5bf43922012-09-07 16:07:55 -0400575bool TWPartition::Is_File_System(string File_System) {
576 if (File_System == "ext2" ||
Dees_Troy63c8df72012-09-10 14:02:05 -0400577 File_System == "ext3" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400578 File_System == "ext4" ||
579 File_System == "vfat" ||
580 File_System == "ntfs" ||
581 File_System == "yaffs2" ||
bigbiff bigbiff3e146522012-11-14 14:32:59 -0500582 File_System == "exfat" ||
Dees_Troye5017042013-08-29 16:38:55 +0000583 File_System == "f2fs" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400584 File_System == "auto")
585 return true;
586 else
587 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400588}
589
Dees_Troy5bf43922012-09-07 16:07:55 -0400590bool TWPartition::Is_Image(string File_System) {
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400591 if (File_System == "emmc" || File_System == "mtd" || File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -0400592 return true;
593 else
594 return false;
595}
596
Dees_Troy51127312012-09-08 13:08:49 -0400597bool TWPartition::Make_Dir(string Path, bool Display_Error) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400598 if (!TWFunc::Path_Exists(Path)) {
Dees_Troy51127312012-09-08 13:08:49 -0400599 if (mkdir(Path.c_str(), 0777) == -1) {
600 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000601 LOGERR("Can not create '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400602 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000603 LOGINFO("Can not create '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400604 return false;
605 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000606 LOGINFO("Created '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400607 return true;
608 }
609 }
610 return true;
611}
612
Dees_Troy5bf43922012-09-07 16:07:55 -0400613void TWPartition::Setup_File_System(bool Display_Error) {
614 struct statfs st;
615
616 Can_Be_Mounted = true;
617 Can_Be_Wiped = true;
618
Dees_Troy5bf43922012-09-07 16:07:55 -0400619 // Make the mount point folder if it doesn't exist
Dees_Troy51127312012-09-08 13:08:49 -0400620 Make_Dir(Mount_Point, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400621 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
622 Backup_Name = Display_Name;
623 Backup_Method = FILES;
624}
625
626void TWPartition::Setup_Image(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400627 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
628 Backup_Name = Display_Name;
Gary Peck82599a82012-11-21 16:23:12 -0800629 if (Current_File_System == "emmc")
Dees_Troy5bf43922012-09-07 16:07:55 -0400630 Backup_Method = DD;
Gary Peck82599a82012-11-21 16:23:12 -0800631 else if (Current_File_System == "mtd" || Current_File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -0400632 Backup_Method = FLASH_UTILS;
633 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000634 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 -0400635 if (Find_Partition_Size()) {
636 Used = Size;
637 Backup_Size = Size;
Dees_Troy51a0e822012-09-05 15:24:24 -0400638 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400639 if (Display_Error)
Dees Troyd932ce12013-10-18 17:12:59 +0000640 LOGERR("Unable to find partition size for '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400641 else
Dees Troyd932ce12013-10-18 17:12:59 +0000642 LOGINFO("Unable to find partition size for '%s'\n", Mount_Point.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400643 }
644}
645
Dees_Troye58d5262012-09-21 12:27:57 -0400646void TWPartition::Setup_AndSec(void) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500647 Backup_Display_Name = "Android Secure";
Dees_Troye58d5262012-09-21 12:27:57 -0400648 Backup_Name = "and-sec";
Dees_Troya13d74f2013-03-24 08:54:55 -0500649 Can_Be_Backed_Up = true;
Dees_Troye58d5262012-09-21 12:27:57 -0400650 Has_Android_Secure = true;
651 Symlink_Path = Mount_Point + "/.android_secure";
652 Symlink_Mount_Point = "/and-sec";
653 Backup_Path = Symlink_Mount_Point;
654 Make_Dir("/and-sec", true);
655 Recreate_AndSec_Folder();
Ethan Yonkerd4d10732014-02-03 15:27:52 -0600656 Mount_Storage_Retry();
Dees_Troye58d5262012-09-21 12:27:57 -0400657}
658
Dees_Troy5bf43922012-09-07 16:07:55 -0400659void TWPartition::Find_Real_Block_Device(string& Block, bool Display_Error) {
660 char device[512], realDevice[512];
661
662 strcpy(device, Block.c_str());
663 memset(realDevice, 0, sizeof(realDevice));
664 while (readlink(device, realDevice, sizeof(realDevice)) > 0)
665 {
666 strcpy(device, realDevice);
667 memset(realDevice, 0, sizeof(realDevice));
668 }
669
670 if (device[0] != '/') {
671 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000672 LOGERR("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400673 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000674 LOGINFO("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400675 return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400676 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400677 Block = device;
678 return;
679 }
680}
681
Dees_Troy8e337f32012-10-13 22:07:49 -0400682void TWPartition::Mount_Storage_Retry(void) {
683 // On some devices, storage doesn't want to mount right away, retry and sleep
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500684 if (!Mount(true)) {
Dees_Troy8e337f32012-10-13 22:07:49 -0400685 int retry_count = 5;
686 while (retry_count > 0 && !Mount(false)) {
687 usleep(500000);
688 retry_count--;
689 }
690 Mount(true);
691 }
692}
693
Dees_Troy38bd7602012-09-14 13:33:53 -0400694bool TWPartition::Find_MTD_Block_Device(string MTD_Name) {
695 FILE *fp = NULL;
696 char line[255];
697
698 fp = fopen("/proc/mtd", "rt");
699 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000700 LOGERR("Device does not support /proc/mtd\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400701 return false;
702 }
703
704 while (fgets(line, sizeof(line), fp) != NULL)
705 {
706 char device[32], label[32];
707 unsigned long size = 0;
708 char* fstype = NULL;
709 int deviceId;
710
711 sscanf(line, "%s %lx %*s %*c%s", device, &size, label);
712
713 // Skip header and blank lines
714 if ((strcmp(device, "dev:") == 0) || (strlen(line) < 8))
715 continue;
716
717 // Strip off the trailing " from the label
718 label[strlen(label)-1] = '\0';
719
720 if (strcmp(label, MTD_Name.c_str()) == 0) {
721 // We found our device
722 // Strip off the trailing : from the device
723 device[strlen(device)-1] = '\0';
724 if (sscanf(device,"mtd%d", &deviceId) == 1) {
725 sprintf(device, "/dev/block/mtdblock%d", deviceId);
726 Primary_Block_Device = device;
Dees_Troy76543db2013-06-19 16:24:30 +0000727 fclose(fp);
728 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -0400729 }
730 }
731 }
732 fclose(fp);
733
734 return false;
735}
736
Dees_Troy51127312012-09-08 13:08:49 -0400737bool TWPartition::Get_Size_Via_statfs(bool Display_Error) {
738 struct statfs st;
739 string Local_Path = Mount_Point + "/.";
740
741 if (!Mount(Display_Error))
742 return false;
743
744 if (statfs(Local_Path.c_str(), &st) != 0) {
745 if (!Removable) {
746 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000747 LOGERR("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400748 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000749 LOGINFO("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400750 }
751 return false;
752 }
753 Size = (st.f_blocks * st.f_bsize);
754 Used = ((st.f_blocks - st.f_bfree) * st.f_bsize);
755 Free = (st.f_bfree * st.f_bsize);
756 Backup_Size = Used;
757 return true;
758}
759
760bool TWPartition::Get_Size_Via_df(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400761 FILE* fp;
762 char command[255], line[512];
763 int include_block = 1;
764 unsigned int min_len;
765
766 if (!Mount(Display_Error))
767 return false;
768
Dees_Troy38bd7602012-09-14 13:33:53 -0400769 min_len = Actual_Block_Device.size() + 2;
Dees_Troy51127312012-09-08 13:08:49 -0400770 sprintf(command, "df %s > /tmp/dfoutput.txt", Mount_Point.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +0200771 TWFunc::Exec_Cmd(command);
Dees_Troy51127312012-09-08 13:08:49 -0400772 fp = fopen("/tmp/dfoutput.txt", "rt");
773 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000774 LOGINFO("Unable to open /tmp/dfoutput.txt.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400775 return false;
Dees_Troy51127312012-09-08 13:08:49 -0400776 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400777
778 while (fgets(line, sizeof(line), fp) != NULL)
779 {
780 unsigned long blocks, used, available;
781 char device[64];
782 char tmpString[64];
783
784 if (strncmp(line, "Filesystem", 10) == 0)
785 continue;
786 if (strlen(line) < min_len) {
787 include_block = 0;
788 continue;
789 }
790 if (include_block) {
791 sscanf(line, "%s %lu %lu %lu", device, &blocks, &used, &available);
792 } else {
793 // The device block string is so long that the df information is on the next line
794 int space_count = 0;
Dees_Troye58d5262012-09-21 12:27:57 -0400795 sprintf(tmpString, "/dev/block/%s", Actual_Block_Device.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400796 while (tmpString[space_count] == 32)
797 space_count++;
798 sscanf(line + space_count, "%lu %lu %lu", &blocks, &used, &available);
799 }
800
801 // Adjust block size to byte size
802 Size = blocks * 1024ULL;
803 Used = used * 1024ULL;
804 Free = available * 1024ULL;
805 Backup_Size = Used;
806 }
807 fclose(fp);
808 return true;
809}
810
Dees_Troy5bf43922012-09-07 16:07:55 -0400811bool TWPartition::Find_Partition_Size(void) {
812 FILE* fp;
813 char line[512];
814 string tmpdevice;
815
igoriok87e3d932013-01-31 21:03:53 +0200816 fp = fopen("/proc/dumchar_info", "rt");
817 if (fp != NULL) {
818 while (fgets(line, sizeof(line), fp) != NULL)
819 {
820 char label[32], device[32];
821 unsigned long size = 0;
822
823 sscanf(line, "%s %lx %*lx %*lu %s", label, &size, device);
824
bigbiff bigbiff34684ff2013-12-01 21:03:45 -0500825 // Skip header, annotation and blank lines
igoriok87e3d932013-01-31 21:03:53 +0200826 if ((strncmp(device, "/dev/", 5) != 0) || (strlen(line) < 8))
827 continue;
828
829 tmpdevice = "/dev/";
830 tmpdevice += label;
831 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
832 Size = size;
833 fclose(fp);
834 return true;
835 }
836 }
837 }
838
Dees_Troy5bf43922012-09-07 16:07:55 -0400839 // In this case, we'll first get the partitions we care about (with labels)
840 fp = fopen("/proc/partitions", "rt");
841 if (fp == NULL)
842 return false;
843
844 while (fgets(line, sizeof(line), fp) != NULL)
845 {
846 unsigned long major, minor, blocks;
847 char device[512];
848 char tmpString[64];
849
Dees_Troy63c8df72012-09-10 14:02:05 -0400850 if (strlen(line) < 7 || line[0] == 'm') continue;
Dees_Troy5bf43922012-09-07 16:07:55 -0400851 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
852
853 tmpdevice = "/dev/block/";
854 tmpdevice += device;
Dees_Troy38bd7602012-09-14 13:33:53 -0400855 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400856 // Adjust block size to byte size
857 Size = blocks * 1024ULL;
858 fclose(fp);
859 return true;
860 }
861 }
862 fclose(fp);
863 return false;
864}
865
Dees_Troy5bf43922012-09-07 16:07:55 -0400866bool TWPartition::Is_Mounted(void) {
867 if (!Can_Be_Mounted)
868 return false;
869
870 struct stat st1, st2;
871 string test_path;
872
873 // Check to see if the mount point directory exists
874 test_path = Mount_Point + "/.";
875 if (stat(test_path.c_str(), &st1) != 0) return false;
876
877 // Check to see if the directory above the mount point exists
878 test_path = Mount_Point + "/../.";
879 if (stat(test_path.c_str(), &st2) != 0) return false;
880
881 // Compare the device IDs -- if they match then we're (probably) using tmpfs instead of an actual device
882 int ret = (st1.st_dev != st2.st_dev) ? true : false;
883
884 return ret;
885}
886
887bool TWPartition::Mount(bool Display_Error) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000888 int exfat_mounted = 0;
889
Dees_Troy5bf43922012-09-07 16:07:55 -0400890 if (Is_Mounted()) {
891 return true;
892 } else if (!Can_Be_Mounted) {
893 return false;
894 }
Dees_Troy38bd7602012-09-14 13:33:53 -0400895
896 Find_Actual_Block_Device();
897
898 // Check the current file system before mounting
899 Check_FS_Type();
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000900 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/exfat-fuse")) {
Dees_Troye34c1332013-02-06 19:13:00 +0000901 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 +0000902 LOGINFO("cmd: %s\n", cmd.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000903 string result;
904 if (TWFunc::Exec_Cmd(cmd, result) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000905 LOGINFO("exfat-fuse failed to mount with result '%s', trying vfat\n", result.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000906 Current_File_System = "vfat";
907 } else {
908#ifdef TW_NO_EXFAT_FUSE
909 UnMount(false);
910 // We'll let the kernel handle it but using exfat-fuse to detect if the file system is actually exfat
911 // Some kernels let us mount vfat as exfat which doesn't work out too well
912#else
913 exfat_mounted = 1;
914#endif
915 }
916 }
Dees_Troy22042032012-12-18 21:23:08 +0000917 if (Fstab_File_System == "yaffs2") {
918 // mount an MTD partition as a YAFFS2 filesystem.
Dees_Troy76543db2013-06-19 16:24:30 +0000919 const unsigned long flags = MS_NOATIME | MS_NODEV | MS_NODIRATIME;
920 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags, NULL) < 0) {
921 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags | MS_RDONLY, NULL) < 0) {
922 if (Display_Error)
923 LOGERR("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
924 else
925 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
926 return false;
927 } else {
928 LOGINFO("Mounted '%s' (MTD) as RO\n", Mount_Point.c_str());
929 return true;
930 }
931 } else {
932 struct stat st;
933 string test_path = Mount_Point;
934 if (stat(test_path.c_str(), &st) < 0) {
935 if (Display_Error)
936 LOGERR("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
937 else
938 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
939 return false;
940 }
941 mode_t new_mode = st.st_mode | S_IXUSR | S_IXGRP | S_IXOTH;
942 if (new_mode != st.st_mode) {
943 LOGINFO("Fixing execute permissions for %s\n", Mount_Point.c_str());
944 if (chmod(Mount_Point.c_str(), new_mode) < 0) {
945 if (Display_Error)
946 LOGERR("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
947 else
948 LOGINFO("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
949 return false;
950 }
951 }
Dees_Troy22042032012-12-18 21:23:08 +0000952 return true;
Dees_Troy76543db2013-06-19 16:24:30 +0000953 }
Dees Troy216e0422014-02-07 03:46:42 +0000954 } 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 +0000955#ifdef TW_NO_EXFAT_FUSE
956 if (Current_File_System == "exfat") {
Dees_Troy2673cec2013-04-02 20:22:16 +0000957 LOGINFO("Mounting exfat failed, trying vfat...\n");
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000958 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), "vfat", 0, NULL) != 0) {
Dees_Troy85f44ed2013-01-09 18:42:36 +0000959 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000960 LOGERR("Unable to mount '%s'\n", Mount_Point.c_str());
Dees_Troy85f44ed2013-01-09 18:42:36 +0000961 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000962 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
Hashcode62bd9e02013-11-19 21:59:42 -0800963 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 +0000964 return false;
Dees_Troy85f44ed2013-01-09 18:42:36 +0000965 }
Dees_Troyb05ddee2013-01-28 20:24:50 +0000966 } else {
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000967#endif
968 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000969 LOGERR("Unable to mount '%s'\n", Mount_Point.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000970 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000971 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
972 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 +0000973 return false;
974#ifdef TW_NO_EXFAT_FUSE
Dees_Troy85f44ed2013-01-09 18:42:36 +0000975 }
976#endif
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000977 }
978#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
979 string MetaEcfsFile = EXPAND(TW_EXTERNAL_STORAGE_PATH);
980 MetaEcfsFile += "/.MetaEcfsFile";
981 if (EcryptFS_Password.size() > 0 && PartitionManager.Mount_By_Path("/data", false) && TWFunc::Path_Exists(MetaEcfsFile)) {
982 if (mount_ecryptfs_drive(EcryptFS_Password.c_str(), Mount_Point.c_str(), Mount_Point.c_str(), 0) != 0) {
983 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000984 LOGERR("Unable to mount ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000985 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000986 LOGINFO("Unable to mount ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000987 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000988 LOGINFO("Successfully mounted ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000989 Is_Decrypted = true;
Dees_Troy51127312012-09-08 13:08:49 -0400990 }
Dees_Troy066eb302013-08-23 17:20:32 +0000991 } else if (Mount_Point == EXPAND(TW_EXTERNAL_STORAGE_PATH)) {
992 if (Is_Decrypted)
993 LOGINFO("Mounting external storage, '%s' is not encrypted\n", Mount_Point.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000994 Is_Decrypted = false;
995 }
996#endif
997 if (Removable)
998 Update_Size(Display_Error);
999
1000 if (!Symlink_Mount_Point.empty()) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001001 string Command = "mount '" + Symlink_Path + "' '" + Symlink_Mount_Point + "'";
1002 TWFunc::Exec_Cmd(Command);
Dees_Troy5bf43922012-09-07 16:07:55 -04001003 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001004 return true;
1005}
1006
1007bool TWPartition::UnMount(bool Display_Error) {
1008 if (Is_Mounted()) {
1009 int never_unmount_system;
1010
1011 DataManager::GetValue(TW_DONT_UNMOUNT_SYSTEM, never_unmount_system);
1012 if (never_unmount_system == 1 && Mount_Point == "/system")
1013 return true; // Never unmount system if you're not supposed to unmount it
1014
Dees_Troyc8bafa12013-01-10 15:43:00 +00001015#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
1016 if (EcryptFS_Password.size() > 0) {
1017 if (unmount_ecryptfs_drive(Mount_Point.c_str()) != 0) {
1018 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001019 LOGERR("Unable to unmount ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troyc8bafa12013-01-10 15:43:00 +00001020 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001021 LOGINFO("Unable to unmount ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troyc8bafa12013-01-10 15:43:00 +00001022 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001023 LOGINFO("Successfully unmounted ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troyc8bafa12013-01-10 15:43:00 +00001024 }
1025 }
1026#endif
1027
Dees_Troy38bd7602012-09-14 13:33:53 -04001028 if (!Symlink_Mount_Point.empty())
1029 umount(Symlink_Mount_Point.c_str());
1030
Dees_Troyb05ddee2013-01-28 20:24:50 +00001031 umount(Mount_Point.c_str());
1032 if (Is_Mounted()) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001033 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001034 LOGERR("Unable to unmount '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001035 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001036 LOGINFO("Unable to unmount '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001037 return false;
1038 } else
1039 return true;
1040 } else {
1041 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001042 }
1043}
1044
Gary Peck43acadf2012-11-21 21:19:01 -08001045bool TWPartition::Wipe(string New_File_System) {
Dees_Troy16c2b312013-01-15 16:51:18 +00001046 bool wiped = false, update_crypt = false;
1047 int check;
1048 string Layout_Filename = Mount_Point + "/.layout_version";
1049
Dees_Troy38bd7602012-09-14 13:33:53 -04001050 if (!Can_Be_Wiped) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001051 LOGERR("Partition '%s' cannot be wiped.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001052 return false;
1053 }
1054
Dees_Troyc51f1f92012-09-20 15:32:13 -04001055 if (Mount_Point == "/cache")
Dees_Troy2673cec2013-04-02 20:22:16 +00001056 Log_Offset = 0;
Dees_Troyc51f1f92012-09-20 15:32:13 -04001057
Dees_Troyce675462013-01-09 19:48:21 +00001058#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
1059 if (Mount_Point == "/data" && Mount(false)) {
1060 if (TWFunc::Path_Exists("/data/system/edk_p_sd"))
1061 TWFunc::copy_file("/data/system/edk_p_sd", "/tmp/edk_p_sd", 0600);
1062 }
1063#endif
1064
Dees_Troy16c2b312013-01-15 16:51:18 +00001065 if (Retain_Layout_Version && Mount(false) && TWFunc::Path_Exists(Layout_Filename))
1066 TWFunc::copy_file(Layout_Filename, "/.layout_version", 0600);
1067 else
1068 unlink("/.layout_version");
Dees_Troy38bd7602012-09-14 13:33:53 -04001069
Dees_Troy16c2b312013-01-15 16:51:18 +00001070 if (Has_Data_Media) {
1071 wiped = Wipe_Data_Without_Wiping_Media();
1072 } else {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001073
Dees_Troy16c2b312013-01-15 16:51:18 +00001074 DataManager::GetValue(TW_RM_RF_VAR, check);
1075
Hashcodedabfd492013-08-29 22:45:30 -07001076 if (check || Use_Rm_Rf)
Dees_Troy16c2b312013-01-15 16:51:18 +00001077 wiped = Wipe_RMRF();
1078 else if (New_File_System == "ext4")
1079 wiped = Wipe_EXT4();
1080 else if (New_File_System == "ext2" || New_File_System == "ext3")
1081 wiped = Wipe_EXT23(New_File_System);
1082 else if (New_File_System == "vfat")
1083 wiped = Wipe_FAT();
1084 else if (New_File_System == "exfat")
1085 wiped = Wipe_EXFAT();
1086 else if (New_File_System == "yaffs2")
1087 wiped = Wipe_MTD();
Dees_Troye5017042013-08-29 16:38:55 +00001088 else if (New_File_System == "f2fs")
1089 wiped = Wipe_F2FS();
Dees_Troy16c2b312013-01-15 16:51:18 +00001090 else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001091 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 +00001092 unlink("/.layout_version");
1093 return false;
1094 }
1095 update_crypt = wiped;
Gary Pecke8bc5d72012-12-21 06:45:25 -08001096 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001097
Gary Pecke8bc5d72012-12-21 06:45:25 -08001098 if (wiped) {
Dees_Troyce675462013-01-09 19:48:21 +00001099#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
1100 if (Mount_Point == "/data" && Mount(false)) {
1101 if (TWFunc::Path_Exists("/tmp/edk_p_sd")) {
1102 Make_Dir("/data/system", true);
1103 TWFunc::copy_file("/tmp/edk_p_sd", "/data/system/edk_p_sd", 0600);
1104 }
1105 }
1106#endif
Dees_Troy16c2b312013-01-15 16:51:18 +00001107
Dees_Troy1c1ac442013-01-17 21:42:14 +00001108 if (Mount_Point == "/cache")
1109 DataManager::Output_Version();
1110
Dees_Troy16c2b312013-01-15 16:51:18 +00001111 if (TWFunc::Path_Exists("/.layout_version") && Mount(false))
1112 TWFunc::copy_file("/.layout_version", Layout_Filename, 0600);
1113
1114 if (update_crypt) {
1115 Setup_File_System(false);
1116 if (Is_Encrypted && !Is_Decrypted) {
1117 // just wiped an encrypted partition back to its unencrypted state
1118 Is_Encrypted = false;
1119 Is_Decrypted = false;
1120 Decrypted_Block_Device = "";
1121 if (Mount_Point == "/data") {
1122 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1123 DataManager::SetValue(TW_IS_DECRYPTED, 0);
1124 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001125 }
1126 }
1127 }
1128 return wiped;
Dees_Troy51a0e822012-09-05 15:24:24 -04001129}
1130
Gary Peck43acadf2012-11-21 21:19:01 -08001131bool TWPartition::Wipe() {
Gary Peck82599a82012-11-21 16:23:12 -08001132 if (Is_File_System(Current_File_System))
1133 return Wipe(Current_File_System);
1134 else
1135 return Wipe(Fstab_File_System);
Gary Peck43acadf2012-11-21 21:19:01 -08001136}
1137
Dees_Troye58d5262012-09-21 12:27:57 -04001138bool TWPartition::Wipe_AndSec(void) {
1139 if (!Has_Android_Secure)
1140 return false;
1141
Dees_Troye58d5262012-09-21 12:27:57 -04001142 if (!Mount(true))
1143 return false;
1144
Dees_Troy2673cec2013-04-02 20:22:16 +00001145 gui_print("Wiping %s\n", Backup_Display_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001146 TWFunc::removeDir(Mount_Point + "/.android_secure/", true);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001147 return true;
Dees_Troye58d5262012-09-21 12:27:57 -04001148}
1149
Dees_Troy51a0e822012-09-05 15:24:24 -04001150bool TWPartition::Backup(string backup_folder) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001151 if (Backup_Method == FILES)
1152 return Backup_Tar(backup_folder);
1153 else if (Backup_Method == DD)
1154 return Backup_DD(backup_folder);
1155 else if (Backup_Method == FLASH_UTILS)
1156 return Backup_Dump_Image(backup_folder);
Dees_Troy2673cec2013-04-02 20:22:16 +00001157 LOGERR("Unknown backup method for '%s'\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001158 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001159}
1160
Dees_Troy43d8b002012-09-17 16:00:01 -04001161bool TWPartition::Check_MD5(string restore_folder) {
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001162 string Full_Filename, md5file;
Dees_Troy43d8b002012-09-17 16:00:01 -04001163 char split_filename[512];
1164 int index = 0;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001165 twrpDigest md5sum;
Dees_Troy43d8b002012-09-17 16:00:01 -04001166
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001167 memset(split_filename, 0, sizeof(split_filename));
Dees_Troy43d8b002012-09-17 16:00:01 -04001168 Full_Filename = restore_folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04001169 if (!TWFunc::Path_Exists(Full_Filename)) {
1170 // This is a split archive, we presume
1171 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
Dees_Troy2673cec2013-04-02 20:22:16 +00001172 LOGINFO("split_filename: %s\n", split_filename);
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001173 md5file = split_filename;
1174 md5file += ".md5";
1175 if (!TWFunc::Path_Exists(md5file)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001176 LOGERR("No md5 file found for '%s'.\n", split_filename);
1177 LOGERR("Please unselect Enable MD5 verification to restore.\n");
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001178 return false;
1179 }
1180 md5sum.setfn(split_filename);
Dees_Troy83bd4832013-05-04 12:39:56 +00001181 while (index < 1000) {
1182 if (TWFunc::Path_Exists(split_filename) && md5sum.verify_md5digest() != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001183 LOGERR("MD5 failed to match on '%s'.\n", split_filename);
Dees_Troy43d8b002012-09-17 16:00:01 -04001184 return false;
1185 }
1186 index++;
1187 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001188 md5sum.setfn(split_filename);
Dees_Troy43d8b002012-09-17 16:00:01 -04001189 }
Dees_Troy4a2a1262012-09-18 09:33:47 -04001190 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001191 } else {
1192 // Single file archive
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001193 md5file = Full_Filename + ".md5";
1194 if (!TWFunc::Path_Exists(md5file)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001195 LOGERR("No md5 file found for '%s'.\n", Full_Filename.c_str());
1196 LOGERR("Please unselect Enable MD5 verification to restore.\n");
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001197 return false;
1198 }
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001199 md5sum.setfn(Full_Filename);
1200 if (md5sum.verify_md5digest() != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001201 LOGERR("MD5 failed to match on '%s'.\n", Full_Filename.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001202 return false;
1203 } else
1204 return true;
1205 }
1206 return false;
1207}
1208
Dees_Troy51a0e822012-09-05 15:24:24 -04001209bool TWPartition::Restore(string restore_folder) {
Gary Peck43acadf2012-11-21 21:19:01 -08001210 size_t first_period, second_period;
1211 string Restore_File_System;
1212
1213 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
Dees_Troy2673cec2013-04-02 20:22:16 +00001214 LOGINFO("Restore filename is: %s\n", Backup_FileName.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001215
1216 // Parse backup filename to extract the file system before wiping
1217 first_period = Backup_FileName.find(".");
1218 if (first_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001219 LOGERR("Unable to find file system (first period).\n");
Gary Peck43acadf2012-11-21 21:19:01 -08001220 return false;
1221 }
1222 Restore_File_System = Backup_FileName.substr(first_period + 1, Backup_FileName.size() - first_period - 1);
1223 second_period = Restore_File_System.find(".");
1224 if (second_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001225 LOGERR("Unable to find file system (second period).\n");
Gary Peck43acadf2012-11-21 21:19:01 -08001226 return false;
1227 }
1228 Restore_File_System.resize(second_period);
Dees_Troy2673cec2013-04-02 20:22:16 +00001229 LOGINFO("Restore file system is: '%s'.\n", Restore_File_System.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001230
1231 if (Is_File_System(Restore_File_System))
1232 return Restore_Tar(restore_folder, Restore_File_System);
1233 else if (Is_Image(Restore_File_System)) {
1234 if (Restore_File_System == "emmc")
1235 return Restore_DD(restore_folder);
1236 else if (Restore_File_System == "mtd" || Restore_File_System == "bml")
1237 return Restore_Flash_Image(restore_folder);
1238 }
1239
Dees_Troy2673cec2013-04-02 20:22:16 +00001240 LOGERR("Unknown restore method for '%s'\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001241 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001242}
1243
1244string TWPartition::Backup_Method_By_Name() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001245 if (Backup_Method == NONE)
1246 return "none";
1247 else if (Backup_Method == FILES)
1248 return "files";
1249 else if (Backup_Method == DD)
1250 return "dd";
1251 else if (Backup_Method == FLASH_UTILS)
1252 return "flash_utils";
1253 else
1254 return "undefined";
1255 return "ERROR!";
Dees_Troy51a0e822012-09-05 15:24:24 -04001256}
1257
1258bool TWPartition::Decrypt(string Password) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001259 LOGINFO("STUB TWPartition::Decrypt, password: '%s'\n", Password.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001260 // Is this needed?
Dees_Troy51a0e822012-09-05 15:24:24 -04001261 return 1;
1262}
1263
1264bool TWPartition::Wipe_Encryption() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001265 bool Save_Data_Media = Has_Data_Media;
1266
1267 if (!UnMount(true))
1268 return false;
1269
Dees_Troy38bd7602012-09-14 13:33:53 -04001270 Has_Data_Media = false;
Dees_Troy74fb2e92013-04-15 14:35:47 +00001271 Decrypted_Block_Device = "";
1272 Is_Decrypted = false;
1273 Is_Encrypted = false;
Gary Pecke8bc5d72012-12-21 06:45:25 -08001274 if (Wipe(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001275 Has_Data_Media = Save_Data_Media;
1276 if (Has_Data_Media && !Symlink_Mount_Point.empty()) {
1277 Recreate_Media_Folder();
1278 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001279 gui_print("You may need to reboot recovery to be able to use /data again.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001280 return true;
1281 } else {
1282 Has_Data_Media = Save_Data_Media;
Dees_Troy2673cec2013-04-02 20:22:16 +00001283 LOGERR("Unable to format to remove encryption.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001284 return false;
1285 }
1286 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001287}
1288
1289void TWPartition::Check_FS_Type() {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001290 const char* type;
1291 blkid_probe pr;
Dees_Troy5bf43922012-09-07 16:07:55 -04001292
Dees_Troy68cab492012-12-12 19:29:35 +00001293 if (Fstab_File_System == "yaffs2" || Fstab_File_System == "mtd" || Fstab_File_System == "bml" || Ignore_Blkid)
1294 return; // Running blkid on some mtd devices causes a massive crash or needs to be skipped
Dees_Troy5bf43922012-09-07 16:07:55 -04001295
Dees_Troy38bd7602012-09-14 13:33:53 -04001296 Find_Actual_Block_Device();
Dees_Troy8170a922012-09-18 15:40:25 -04001297 if (!Is_Present)
1298 return;
Dees_Troy51127312012-09-08 13:08:49 -04001299
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001300 pr = blkid_new_probe_from_filename(Actual_Block_Device.c_str());
1301 if (blkid_do_fullprobe(pr)) {
1302 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00001303 LOGINFO("Can't probe device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001304 return;
Dees_Troy5bf43922012-09-07 16:07:55 -04001305 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001306
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001307 if (blkid_probe_lookup_value(pr, "TYPE", &type, NULL) < 0) {
1308 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00001309 LOGINFO("can't find filesystem on device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001310 return;
1311 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001312
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001313 Current_File_System = type;
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001314 blkid_free_probe(pr);
Dees_Troy51a0e822012-09-05 15:24:24 -04001315}
1316
Gary Peck43acadf2012-11-21 21:19:01 -08001317bool TWPartition::Wipe_EXT23(string File_System) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001318 if (!UnMount(true))
1319 return false;
1320
Dees_Troy43d8b002012-09-17 16:00:01 -04001321 if (TWFunc::Path_Exists("/sbin/mke2fs")) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001322 string command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001323
Dees_Troy2673cec2013-04-02 20:22:16 +00001324 gui_print("Formatting %s using mke2fs...\n", Display_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001325 Find_Actual_Block_Device();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001326 command = "mke2fs -t " + File_System + " -m 0 " + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001327 LOGINFO("mke2fs command: %s\n", command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001328 if (TWFunc::Exec_Cmd(command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001329 Current_File_System = File_System;
Dees_Troye58d5262012-09-21 12:27:57 -04001330 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001331 gui_print("Done.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001332 return true;
1333 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001334 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001335 return false;
1336 }
1337 } else
1338 return Wipe_RMRF();
1339
1340 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001341}
1342
1343bool TWPartition::Wipe_EXT4() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001344 if (!UnMount(true))
1345 return false;
1346
Dees_Troyb3265ab2013-08-30 02:59:14 +00001347#if defined(HAVE_SELINUX) && defined(USE_EXT4)
Ethan Yonkerf27497f2014-02-09 11:48:33 -06001348 int ret;
1349 char *secontext = NULL;
1350
Dees_Troya95f55c2013-08-17 13:14:43 +00001351 gui_print("Formatting %s using make_ext4fs function.\n", Display_Name.c_str());
Ethan Yonkerf27497f2014-02-09 11:48:33 -06001352
1353 if (selabel_lookup(selinux_handle, &secontext, Mount_Point.c_str(), S_IFDIR) < 0) {
1354 LOGINFO("Cannot lookup security context for '%s'\n", Mount_Point.c_str());
1355 ret = make_ext4fs(Actual_Block_Device.c_str(), Length, Mount_Point.c_str(), NULL);
1356 } else {
1357 ret = make_ext4fs(Actual_Block_Device.c_str(), Length, Mount_Point.c_str(), selinux_handle);
1358 }
1359 if (ret != 0) {
Dees_Troya95f55c2013-08-17 13:14:43 +00001360 LOGERR("Unable to wipe '%s' using function call.\n", Mount_Point.c_str());
1361 return false;
1362 } else {
bigbiff bigbiffc49d7062013-10-11 20:28:00 -04001363 string sedir = Mount_Point + "/lost+found";
1364 PartitionManager.Mount_By_Path(sedir.c_str(), true);
1365 rmdir(sedir.c_str());
1366 mkdir(sedir.c_str(), S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP);
Dees_Troya95f55c2013-08-17 13:14:43 +00001367 return true;
1368 }
1369#else
Dees_Troy43d8b002012-09-17 16:00:01 -04001370 if (TWFunc::Path_Exists("/sbin/make_ext4fs")) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001371 string Command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001372
Dees_Troy2673cec2013-04-02 20:22:16 +00001373 gui_print("Formatting %s using make_ext4fs...\n", Display_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001374 Find_Actual_Block_Device();
1375 Command = "make_ext4fs";
1376 if (!Is_Decrypted && Length != 0) {
1377 // Only use length if we're not decrypted
1378 char len[32];
1379 sprintf(len, "%i", Length);
1380 Command += " -l ";
1381 Command += len;
1382 }
Dees_Troy5295d582013-09-06 15:51:08 +00001383 if (TWFunc::Path_Exists("/file_contexts")) {
1384 Command += " -S /file_contexts";
1385 }
1386 Command += " -a " + Mount_Point + " " + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001387 LOGINFO("make_ext4fs command: %s\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001388 if (TWFunc::Exec_Cmd(Command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001389 Current_File_System = "ext4";
Dees_Troye58d5262012-09-21 12:27:57 -04001390 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001391 gui_print("Done.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001392 return true;
1393 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001394 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001395 return false;
1396 }
1397 } else
Gary Peck43acadf2012-11-21 21:19:01 -08001398 return Wipe_EXT23("ext4");
Dees_Troya95f55c2013-08-17 13:14:43 +00001399#endif
Dees_Troy38bd7602012-09-14 13:33:53 -04001400 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001401}
1402
1403bool TWPartition::Wipe_FAT() {
Vojtech Bocek05534202013-09-11 08:11:56 +02001404 string command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001405
Dees_Troy43d8b002012-09-17 16:00:01 -04001406 if (TWFunc::Path_Exists("/sbin/mkdosfs")) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001407 if (!UnMount(true))
1408 return false;
1409
Dees_Troy2673cec2013-04-02 20:22:16 +00001410 gui_print("Formatting %s using mkdosfs...\n", Display_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001411 Find_Actual_Block_Device();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001412 command = "mkdosfs " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02001413 if (TWFunc::Exec_Cmd(command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001414 Current_File_System = "vfat";
Dees_Troye58d5262012-09-21 12:27:57 -04001415 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001416 gui_print("Done.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001417 return true;
1418 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001419 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001420 return false;
1421 }
1422 return true;
1423 }
1424 else
1425 return Wipe_RMRF();
1426
1427 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001428}
1429
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001430bool TWPartition::Wipe_EXFAT() {
Vojtech Bocek05534202013-09-11 08:11:56 +02001431 string command;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001432
1433 if (TWFunc::Path_Exists("/sbin/mkexfatfs")) {
1434 if (!UnMount(true))
1435 return false;
1436
Dees_Troy2673cec2013-04-02 20:22:16 +00001437 gui_print("Formatting %s using mkexfatfs...\n", Display_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001438 Find_Actual_Block_Device();
1439 command = "mkexfatfs " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02001440 if (TWFunc::Exec_Cmd(command) == 0) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001441 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001442 gui_print("Done.\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001443 return true;
1444 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001445 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001446 return false;
1447 }
1448 return true;
1449 }
1450 return false;
1451}
1452
Dees_Troy38bd7602012-09-14 13:33:53 -04001453bool TWPartition::Wipe_MTD() {
1454 if (!UnMount(true))
1455 return false;
1456
Dees_Troy2673cec2013-04-02 20:22:16 +00001457 gui_print("MTD Formatting \"%s\"\n", MTD_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001458
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001459 mtd_scan_partitions();
1460 const MtdPartition* mtd = mtd_find_partition_by_name(MTD_Name.c_str());
1461 if (mtd == NULL) {
1462 LOGERR("No mtd partition named '%s'", MTD_Name.c_str());
1463 return false;
1464 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001465
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001466 MtdWriteContext* ctx = mtd_write_partition(mtd);
1467 if (ctx == NULL) {
1468 LOGERR("Can't write '%s', failed to format.", MTD_Name.c_str());
1469 return false;
1470 }
1471 if (mtd_erase_blocks(ctx, -1) == -1) {
1472 mtd_write_close(ctx);
1473 LOGERR("Failed to format '%s'", MTD_Name.c_str());
1474 return false;
1475 }
1476 if (mtd_write_close(ctx) != 0) {
1477 LOGERR("Failed to close '%s'", MTD_Name.c_str());
1478 return false;
1479 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001480 Current_File_System = "yaffs2";
Dees_Troye58d5262012-09-21 12:27:57 -04001481 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001482 gui_print("Done.\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001483 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001484}
1485
1486bool TWPartition::Wipe_RMRF() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001487 if (!Mount(true))
1488 return false;
1489
Dees_Troy2673cec2013-04-02 20:22:16 +00001490 gui_print("Removing all files under '%s'\n", Mount_Point.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001491 TWFunc::removeDir(Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04001492 Recreate_AndSec_Folder();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001493 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001494}
1495
Dees_Troye5017042013-08-29 16:38:55 +00001496bool TWPartition::Wipe_F2FS() {
Vojtech Bocek05534202013-09-11 08:11:56 +02001497 string command;
Dees_Troye5017042013-08-29 16:38:55 +00001498
1499 if (TWFunc::Path_Exists("/sbin/mkfs.f2fs")) {
1500 if (!UnMount(true))
1501 return false;
1502
1503 gui_print("Formatting %s using mkfs.f2fs...\n", Display_Name.c_str());
1504 Find_Actual_Block_Device();
1505 command = "mkfs.f2fs " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02001506 if (TWFunc::Exec_Cmd(command) == 0) {
Dees_Troye5017042013-08-29 16:38:55 +00001507 Recreate_AndSec_Folder();
1508 gui_print("Done.\n");
1509 return true;
1510 } else {
1511 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
1512 return false;
1513 }
1514 return true;
1515 } else {
1516 gui_print("mkfs.f2fs binary not found, using rm -rf to wipe.\n");
1517 return Wipe_RMRF();
1518 }
1519 return false;
1520}
1521
Dees_Troy51a0e822012-09-05 15:24:24 -04001522bool TWPartition::Wipe_Data_Without_Wiping_Media() {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001523 string dir;
bigbiff bigbiff6b600f92014-01-05 18:13:43 -05001524 #ifdef HAVE_SELINUX
1525 fixPermissions perms;
1526 #endif
Dees_Troy38bd7602012-09-14 13:33:53 -04001527
1528 // This handles wiping data on devices with "sdcard" in /data/media
1529 if (!Mount(true))
1530 return false;
1531
Dees_Troy2673cec2013-04-02 20:22:16 +00001532 gui_print("Wiping data without wiping /data/media ...\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001533
1534 DIR* d;
1535 d = opendir("/data");
Dees_Troy16b74352012-11-14 22:27:31 +00001536 if (d != NULL) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001537 struct dirent* de;
1538 while ((de = readdir(d)) != NULL) {
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05001539 if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) continue;
Dees_Troy16b74352012-11-14 22:27:31 +00001540 // The media folder is the "internal sdcard"
1541 // The .layout_version file is responsible for determining whether 4.2 decides up upgrade
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001542 // the media folder for multi-user.
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05001543 //TODO: convert this to use twrpDU.cpp
Dees_Troy16b74352012-11-14 22:27:31 +00001544 if (strcmp(de->d_name, "media") == 0 || strcmp(de->d_name, ".layout_version") == 0) continue;
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05001545
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001546 dir = "/data/";
1547 dir.append(de->d_name);
Dees_Troyce675462013-01-09 19:48:21 +00001548 if (de->d_type == DT_DIR) {
1549 TWFunc::removeDir(dir, false);
bigbiff bigbiff98f1f902013-01-19 18:46:13 -05001550 } 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 +00001551 if (!unlink(dir.c_str()))
Dees_Troy2673cec2013-04-02 20:22:16 +00001552 LOGINFO("Unable to unlink '%s'\n", dir.c_str());
Dees_Troyce675462013-01-09 19:48:21 +00001553 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001554 }
1555 closedir(d);
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05001556
bigbiff bigbiff6b600f92014-01-05 18:13:43 -05001557 #ifdef HAVE_SELINUX
1558 perms.fixDataInternalContexts();
1559 #endif
1560
Dees_Troy2673cec2013-04-02 20:22:16 +00001561 gui_print("Done.\n");
Dees_Troy16b74352012-11-14 22:27:31 +00001562 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -04001563 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001564 gui_print("Dirent failed to open /data, error!\n");
Dees_Troy16b74352012-11-14 22:27:31 +00001565 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001566}
1567
1568bool TWPartition::Backup_Tar(string backup_folder) {
Dees_Troy4a2a1262012-09-18 09:33:47 -04001569 char back_name[255], split_index[5];
1570 string Full_FileName, Split_FileName, Tar_Args, Command;
Dees_Troy83bd4832013-05-04 12:39:56 +00001571 int use_compression, use_encryption = 0, index, backup_count;
Dees_Troy4a2a1262012-09-18 09:33:47 -04001572 struct stat st;
Dees_Troy7c2dec82012-09-26 09:49:14 -04001573 unsigned long long total_bsize = 0, file_size;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001574 twrpTar tar;
1575 vector <string> files;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001576
Dees_Troy43d8b002012-09-17 16:00:01 -04001577 if (!Mount(true))
1578 return false;
1579
Dees_Troya13d74f2013-03-24 08:54:55 -05001580 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Backup_Display_Name, "Backing Up");
Dees_Troy2673cec2013-04-02 20:22:16 +00001581 gui_print("Backing up %s...\n", Backup_Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001582
1583 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
Dees_Troy83bd4832013-05-04 12:39:56 +00001584 tar.use_compression = use_compression;
bigbiff bigbiff86e77bc2013-08-26 21:36:23 -04001585 //exclude Google Music Cache
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05001586 vector<string> excludedirs = du.get_absolute_dirs();
1587 for (int i = 0; i < excludedirs.size(); ++i) {
1588 tar.setexcl(excludedirs.at(i));
1589 }
Dees_Troy83bd4832013-05-04 12:39:56 +00001590#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
1591 DataManager::GetValue("tw_encrypt_backup", use_encryption);
1592 if (use_encryption && Can_Encrypt_Backup) {
1593 tar.use_encryption = use_encryption;
1594 if (Use_Userdata_Encryption)
1595 tar.userdata_encryption = use_encryption;
Ethan Yonker87af5632014-02-10 11:56:35 -06001596 string Password;
1597 DataManager::GetValue("tw_backup_password", Password);
1598 tar.setpassword(Password);
Dees_Troy83bd4832013-05-04 12:39:56 +00001599 } else {
1600 use_encryption = false;
1601 }
1602#endif
Dees_Troy43d8b002012-09-17 16:00:01 -04001603
1604 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1605 Backup_FileName = back_name;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001606 Full_FileName = backup_folder + "/" + Backup_FileName;
Dees_Troy83bd4832013-05-04 12:39:56 +00001607 tar.has_data_media = Has_Data_Media;
Dees Troye0a433a2013-12-02 04:10:37 +00001608 Full_FileName = backup_folder + "/" + Backup_FileName;
1609 tar.setdir(Backup_Path);
1610 tar.setfn(Full_FileName);
1611 tar.setsize(Backup_Size);
1612 if (tar.createTarFork() != 0)
1613 return false;
Dees_Troy43d8b002012-09-17 16:00:01 -04001614 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001615}
1616
1617bool TWPartition::Backup_DD(string backup_folder) {
igoriok87e3d932013-01-31 21:03:53 +02001618 char back_name[255], backup_size[32];
Vojtech Bocek05534202013-09-11 08:11:56 +02001619 string Full_FileName, Command, DD_BS;
Dees_Troy43d8b002012-09-17 16:00:01 -04001620 int use_compression;
1621
igoriok87e3d932013-01-31 21:03:53 +02001622 sprintf(backup_size, "%llu", Backup_Size);
1623 DD_BS = backup_size;
1624
Dees_Troyb46a6842012-09-25 11:06:46 -04001625 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
Dees_Troy2673cec2013-04-02 20:22:16 +00001626 gui_print("Backing up %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001627
1628 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1629 Backup_FileName = back_name;
1630
1631 Full_FileName = backup_folder + "/" + Backup_FileName;
1632
igoriok87e3d932013-01-31 21:03:53 +02001633 Command = "dd if=" + Actual_Block_Device + " of='" + Full_FileName + "'" + " bs=" + DD_BS + "c count=1";
Dees_Troy2673cec2013-04-02 20:22:16 +00001634 LOGINFO("Backup command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001635 TWFunc::Exec_Cmd(Command);
Dees_Troyc154ac22012-10-12 15:36:47 -04001636 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001637 LOGERR("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001638 return false;
1639 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001640 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001641}
1642
1643bool TWPartition::Backup_Dump_Image(string backup_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001644 char back_name[255];
Vojtech Bocek05534202013-09-11 08:11:56 +02001645 string Full_FileName, Command;
Dees_Troy43d8b002012-09-17 16:00:01 -04001646 int use_compression;
1647
Dees_Troyb46a6842012-09-25 11:06:46 -04001648 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
Dees_Troy2673cec2013-04-02 20:22:16 +00001649 gui_print("Backing up %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001650
1651 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1652 Backup_FileName = back_name;
1653
1654 Full_FileName = backup_folder + "/" + Backup_FileName;
1655
1656 Command = "dump_image " + MTD_Name + " '" + Full_FileName + "'";
Dees_Troy2673cec2013-04-02 20:22:16 +00001657 LOGINFO("Backup command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001658 TWFunc::Exec_Cmd(Command);
Dees_Troy7c2dec82012-09-26 09:49:14 -04001659 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
1660 // 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 +00001661 LOGERR("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001662 return false;
1663 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001664 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001665}
1666
Gary Peck43acadf2012-11-21 21:19:01 -08001667bool TWPartition::Restore_Tar(string restore_folder, string Restore_File_System) {
1668 string Full_FileName, Command;
Dees_Troy4a2a1262012-09-18 09:33:47 -04001669 int index = 0;
1670 char split_index[5];
Dees_Troy43d8b002012-09-17 16:00:01 -04001671
Dees_Troye58d5262012-09-21 12:27:57 -04001672 if (Has_Android_Secure) {
Dees_Troye58d5262012-09-21 12:27:57 -04001673 if (!Wipe_AndSec())
1674 return false;
Gary Peck43acadf2012-11-21 21:19:01 -08001675 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001676 gui_print("Wiping %s...\n", Display_Name.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001677 if (!Wipe(Restore_File_System))
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001678 return false;
Dees_Troye58d5262012-09-21 12:27:57 -04001679 }
Dees_Troya13d74f2013-03-24 08:54:55 -05001680 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Backup_Display_Name, "Restoring");
Dees_Troy2673cec2013-04-02 20:22:16 +00001681 gui_print("Restoring %s...\n", Backup_Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001682
1683 if (!Mount(true))
1684 return false;
1685
Dees_Troy4a2a1262012-09-18 09:33:47 -04001686 Full_FileName = restore_folder + "/" + Backup_FileName;
Ethan Yonker87af5632014-02-10 11:56:35 -06001687 twrpTar tar;
1688 tar.setdir(Backup_Path);
1689 tar.setfn(Full_FileName);
1690 tar.backup_name = Backup_Name;
1691#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
1692 string Password;
1693 DataManager::GetValue("tw_restore_password", Password);
1694 if (!Password.empty())
1695 tar.setpassword(Password);
1696#endif
1697 if (tar.extractTarFork() != 0)
1698 return false;
Dees_Troy43d8b002012-09-17 16:00:01 -04001699 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001700}
1701
1702bool TWPartition::Restore_DD(string restore_folder) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001703 string Full_FileName, Command;
Dees_Troy43d8b002012-09-17 16:00:01 -04001704
Dees_Troyda8b55a2012-12-12 19:18:30 +00001705 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
Dees_Troy43d8b002012-09-17 16:00:01 -04001706 Full_FileName = restore_folder + "/" + Backup_FileName;
Gary Peck15e623d2012-11-21 21:07:58 -08001707
1708 if (!Find_Partition_Size()) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001709 LOGERR("Unable to find partition size for '%s'\n", Mount_Point.c_str());
Gary Peck15e623d2012-11-21 21:07:58 -08001710 return false;
1711 }
1712 unsigned long long backup_size = TWFunc::Get_File_Size(Full_FileName);
1713 if (backup_size > Size) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001714 LOGERR("Size (%iMB) of backup '%s' is larger than target device '%s' (%iMB)\n",
Gary Peck15e623d2012-11-21 21:07:58 -08001715 (int)(backup_size / 1048576LLU), Full_FileName.c_str(),
1716 Actual_Block_Device.c_str(), (int)(Size / 1048576LLU));
1717 return false;
1718 }
1719
Dees_Troy2673cec2013-04-02 20:22:16 +00001720 gui_print("Restoring %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001721 Command = "dd bs=4096 if='" + Full_FileName + "' of=" + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001722 LOGINFO("Restore command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001723 TWFunc::Exec_Cmd(Command);
Dees_Troy43d8b002012-09-17 16:00:01 -04001724 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001725}
1726
1727bool TWPartition::Restore_Flash_Image(string restore_folder) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001728 string Full_FileName, Command;
Dees_Troy43d8b002012-09-17 16:00:01 -04001729
Dees_Troy2673cec2013-04-02 20:22:16 +00001730 gui_print("Restoring %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001731 Full_FileName = restore_folder + "/" + Backup_FileName;
1732 // Sometimes flash image doesn't like to flash due to the first 2KB matching, so we erase first to ensure that it flashes
1733 Command = "erase_image " + MTD_Name;
Dees_Troy2673cec2013-04-02 20:22:16 +00001734 LOGINFO("Erase command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001735 TWFunc::Exec_Cmd(Command);
Dees_Troy43d8b002012-09-17 16:00:01 -04001736 Command = "flash_image " + MTD_Name + " '" + Full_FileName + "'";
Dees_Troy2673cec2013-04-02 20:22:16 +00001737 LOGINFO("Restore command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001738 TWFunc::Exec_Cmd(Command);
Dees_Troy43d8b002012-09-17 16:00:01 -04001739 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001740}
Dees_Troy5bf43922012-09-07 16:07:55 -04001741
1742bool TWPartition::Update_Size(bool Display_Error) {
Dees_Troy0550cfb2012-10-13 11:56:13 -04001743 bool ret = false, Was_Already_Mounted = false;
Dees_Troy51127312012-09-08 13:08:49 -04001744
Dees_Troyab10ee22012-09-21 14:27:30 -04001745 if (!Can_Be_Mounted && !Is_Encrypted)
Dees_Troy5bf43922012-09-07 16:07:55 -04001746 return false;
1747
Dees_Troy0550cfb2012-10-13 11:56:13 -04001748 Was_Already_Mounted = Is_Mounted();
Dees_Troy38bd7602012-09-14 13:33:53 -04001749 if (Removable || Is_Encrypted) {
1750 if (!Mount(false))
1751 return true;
1752 } else if (!Mount(Display_Error))
Dees_Troy5bf43922012-09-07 16:07:55 -04001753 return false;
Dees_Troy51127312012-09-08 13:08:49 -04001754
1755 ret = Get_Size_Via_statfs(Display_Error);
Dees_Troy0550cfb2012-10-13 11:56:13 -04001756 if (!ret || Size == 0) {
1757 if (!Get_Size_Via_df(Display_Error)) {
1758 if (!Was_Already_Mounted)
1759 UnMount(false);
Dees_Troy51127312012-09-08 13:08:49 -04001760 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04001761 }
1762 }
Dees_Troy51127312012-09-08 13:08:49 -04001763
Dees_Troy5bf43922012-09-07 16:07:55 -04001764 if (Has_Data_Media) {
1765 if (Mount(Display_Error)) {
Dees_Troy51127312012-09-08 13:08:49 -04001766 unsigned long long data_media_used, actual_data;
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05001767 du.add_relative_dir("media");
1768 Used = du.Get_Folder_Size("/data");
bigbiff bigbiffc7360dd2014-01-25 15:02:57 -05001769 du.clear_relative_dir("media");
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05001770 Backup_Size = Used;
1771 int bak = (int)(Used / 1048576LLU);
Dees_Troy51127312012-09-08 13:08:49 -04001772 int fre = (int)(Free / 1048576LLU);
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05001773 LOGINFO("Data backup size is %iMB, free: %iMB.\n", bak, fre);
Dees_Troy0550cfb2012-10-13 11:56:13 -04001774 } else {
1775 if (!Was_Already_Mounted)
1776 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04001777 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04001778 }
Dees_Troye58d5262012-09-21 12:27:57 -04001779 } else if (Has_Android_Secure) {
1780 if (Mount(Display_Error))
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05001781 Backup_Size = du.Get_Folder_Size(Backup_Path);
Dees_Troy0550cfb2012-10-13 11:56:13 -04001782 else {
1783 if (!Was_Already_Mounted)
1784 UnMount(false);
Dees_Troye58d5262012-09-21 12:27:57 -04001785 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04001786 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001787 }
Dees_Troy0550cfb2012-10-13 11:56:13 -04001788 if (!Was_Already_Mounted)
1789 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04001790 return true;
Dees_Troy51127312012-09-08 13:08:49 -04001791}
Dees_Troy38bd7602012-09-14 13:33:53 -04001792
1793void TWPartition::Find_Actual_Block_Device(void) {
1794 if (Is_Decrypted) {
1795 Actual_Block_Device = Decrypted_Block_Device;
Dees_Troy43d8b002012-09-17 16:00:01 -04001796 if (TWFunc::Path_Exists(Primary_Block_Device))
Dees_Troy38bd7602012-09-14 13:33:53 -04001797 Is_Present = true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001798 } else if (TWFunc::Path_Exists(Primary_Block_Device)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001799 Is_Present = true;
1800 Actual_Block_Device = Primary_Block_Device;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001801 return;
1802 }
1803 if (Is_Decrypted) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001804 } else if (!Alternate_Block_Device.empty() && TWFunc::Path_Exists(Alternate_Block_Device)) {
Dees_Troy3f04d032012-10-07 18:20:09 -04001805 Actual_Block_Device = Alternate_Block_Device;
Dees_Troy38bd7602012-09-14 13:33:53 -04001806 Is_Present = true;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001807 } else {
Dees_Troy38bd7602012-09-14 13:33:53 -04001808 Is_Present = false;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001809 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001810}
1811
1812void TWPartition::Recreate_Media_Folder(void) {
1813 string Command;
1814
bigbiff bigbiff6b600f92014-01-05 18:13:43 -05001815 #ifdef HAVE_SELINUX
1816 fixPermissions perms;
1817 #endif
1818
Dees_Troy38bd7602012-09-14 13:33:53 -04001819 if (!Mount(true)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001820 LOGERR("Unable to recreate /data/media folder.\n");
Dees_Troyb46a6842012-09-25 11:06:46 -04001821 } else if (!TWFunc::Path_Exists("/data/media")) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001822 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Dees_Troy2673cec2013-04-02 20:22:16 +00001823 LOGINFO("Recreating /data/media folder.\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001824 mkdir("/data/media", S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
bigbiff bigbiff6b600f92014-01-05 18:13:43 -05001825 #ifdef HAVE_SELINUX
1826 perms.fixDataInternalContexts();
1827 #endif
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001828 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Dees_Troy38bd7602012-09-14 13:33:53 -04001829 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001830}
Dees_Troye58d5262012-09-21 12:27:57 -04001831
1832void TWPartition::Recreate_AndSec_Folder(void) {
Dees_Troye58d5262012-09-21 12:27:57 -04001833 if (!Has_Android_Secure)
1834 return;
Dees_Troy2673cec2013-04-02 20:22:16 +00001835 LOGINFO("Creating %s: %s\n", Backup_Display_Name.c_str(), Symlink_Path.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -04001836 if (!Mount(true)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001837 LOGERR("Unable to recreate %s folder.\n", Backup_Name.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -04001838 } else if (!TWFunc::Path_Exists(Symlink_Path)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001839 LOGINFO("Recreating %s folder.\n", Backup_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001840 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
1841 mkdir(Symlink_Path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
1842 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04001843 }
1844}