blob: c1c99f6f2451fac7bb5f9cd6381c852d4f235947 [file] [log] [blame]
Dees_Troy51a0e822012-09-05 15:24:24 -04001/* Partition class for TWRP
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15 * 02110-1301, USA.
16 *
17 * The code was written from scratch by Dees_Troy dees_troy at
18 * yahoo
19 *
20 * Copyright (c) 2012
21 */
22
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26#include <sys/stat.h>
27#include <sys/vfs.h>
Dees_Troy5bf43922012-09-07 16:07:55 -040028#include <sys/mount.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040029#include <unistd.h>
Dees_Troy51127312012-09-08 13:08:49 -040030#include <dirent.h>
bigbiff bigbiff9c754052013-01-09 09:09:08 -050031#include <iostream>
32#include <sstream>
Dees_Troy51a0e822012-09-05 15:24:24 -040033
Dees_Troy657c3092012-09-10 20:32:10 -040034#ifdef TW_INCLUDE_CRYPTO
35 #include "cutils/properties.h"
36#endif
37
bigbiff bigbiffe60683a2013-02-22 20:55:50 -050038#include "libblkid/blkid.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040039#include "variables.h"
Dees_Troy2673cec2013-04-02 20:22:16 +000040#include "twcommon.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040041#include "partitions.hpp"
Dees_Troy5bf43922012-09-07 16:07:55 -040042#include "data.hpp"
Dees_Troy43d8b002012-09-17 16:00:01 -040043#include "twrp-functions.hpp"
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -050044#include "twrpDigest.hpp"
bigbiff bigbiff9c754052013-01-09 09:09:08 -050045#include "twrpTar.hpp"
Dees_Troy5bf43922012-09-07 16:07:55 -040046extern "C" {
Dees_Troy38bd7602012-09-14 13:33:53 -040047 #include "mtdutils/mtdutils.h"
48 #include "mtdutils/mounts.h"
Dees_Troy85f44ed2013-01-09 18:42:36 +000049#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
50 #include "crypto/libcrypt_samsung/include/libcrypt_samsung.h"
51#endif
Dees_Troya95f55c2013-08-17 13:14:43 +000052#ifdef USE_EXT4
53 #include "make_ext4fs.h"
54#endif
Dees_Troy5bf43922012-09-07 16:07:55 -040055}
Dees_Troy51a0e822012-09-05 15:24:24 -040056
bigbiff bigbiff9c754052013-01-09 09:09:08 -050057using namespace std;
58
Dees_Troya95f55c2013-08-17 13:14:43 +000059extern struct selabel_handle *selinux_handle;
60
Dees_Troy51a0e822012-09-05 15:24:24 -040061TWPartition::TWPartition(void) {
62 Can_Be_Mounted = false;
63 Can_Be_Wiped = false;
Dees_Troya13d74f2013-03-24 08:54:55 -050064 Can_Be_Backed_Up = false;
Vojtech Bocek1dc30982013-08-30 21:49:30 +020065 Use_Rm_Rf = false;
Dees_Troy51a0e822012-09-05 15:24:24 -040066 Wipe_During_Factory_Reset = false;
67 Wipe_Available_in_GUI = false;
68 Is_SubPartition = false;
Dees_Troy2691f9d2012-09-24 11:15:49 -040069 Has_SubPartition = false;
Dees_Troy51a0e822012-09-05 15:24:24 -040070 SubPartition_Of = "";
71 Symlink_Path = "";
72 Symlink_Mount_Point = "";
73 Mount_Point = "";
Dees_Troye58d5262012-09-21 12:27:57 -040074 Backup_Path = "";
Dees_Troy38bd7602012-09-14 13:33:53 -040075 Actual_Block_Device = "";
76 Primary_Block_Device = "";
Dees_Troy51a0e822012-09-05 15:24:24 -040077 Alternate_Block_Device = "";
78 Removable = false;
79 Is_Present = false;
80 Length = 0;
81 Size = 0;
82 Used = 0;
83 Free = 0;
84 Backup_Size = 0;
85 Can_Be_Encrypted = false;
86 Is_Encrypted = false;
87 Is_Decrypted = false;
88 Decrypted_Block_Device = "";
89 Display_Name = "";
Dees_Troya13d74f2013-03-24 08:54:55 -050090 Backup_Display_Name = "";
91 Storage_Name = "";
Dees_Troy51a0e822012-09-05 15:24:24 -040092 Backup_Name = "";
Dees_Troy63c8df72012-09-10 14:02:05 -040093 Backup_FileName = "";
Dees_Troy38bd7602012-09-14 13:33:53 -040094 MTD_Name = "";
Dees_Troy51a0e822012-09-05 15:24:24 -040095 Backup_Method = NONE;
Dees_Troy83bd4832013-05-04 12:39:56 +000096 Can_Encrypt_Backup = false;
97 Use_Userdata_Encryption = false;
Dees_Troy51a0e822012-09-05 15:24:24 -040098 Has_Data_Media = false;
Dees_Troye58d5262012-09-21 12:27:57 -040099 Has_Android_Secure = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400100 Is_Storage = false;
Dees_Troya13d74f2013-03-24 08:54:55 -0500101 Is_Settings_Storage = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400102 Storage_Path = "";
103 Current_File_System = "";
104 Fstab_File_System = "";
105 Format_Block_Size = 0;
Dees_Troy68cab492012-12-12 19:29:35 +0000106 Ignore_Blkid = false;
Dees_Troy16c2b312013-01-15 16:51:18 +0000107 Retain_Layout_Version = false;
Dees_Troy85f44ed2013-01-09 18:42:36 +0000108#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
109 EcryptFS_Password = "";
110#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400111}
112
113TWPartition::~TWPartition(void) {
114 // Do nothing
115}
116
Dees_Troy5bf43922012-09-07 16:07:55 -0400117bool TWPartition::Process_Fstab_Line(string Line, bool Display_Error) {
118 char full_line[MAX_FSTAB_LINE_LENGTH], item[MAX_FSTAB_LINE_LENGTH];
119 int line_len = Line.size(), index = 0, item_index = 0;
120 char* ptr;
Dees_Troy51127312012-09-08 13:08:49 -0400121 string Flags;
Dees_Troy5bf43922012-09-07 16:07:55 -0400122 strncpy(full_line, Line.c_str(), line_len);
Dees_Troya13d74f2013-03-24 08:54:55 -0500123 bool skip = false;
Dees_Troy5bf43922012-09-07 16:07:55 -0400124
Dees_Troy51127312012-09-08 13:08:49 -0400125 for (index = 0; index < line_len; index++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500126 if (full_line[index] == 34)
127 skip = !skip;
128 if (!skip && full_line[index] <= 32)
Dees_Troy5bf43922012-09-07 16:07:55 -0400129 full_line[index] = '\0';
Dees_Troy5bf43922012-09-07 16:07:55 -0400130 }
Dees_Troy7c2dec82012-09-26 09:49:14 -0400131 Mount_Point = full_line;
Dees_Troy2673cec2013-04-02 20:22:16 +0000132 LOGINFO("Processing '%s'\n", Mount_Point.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -0400133 Backup_Path = Mount_Point;
Dees_Troya13d74f2013-03-24 08:54:55 -0500134 Storage_Path = Mount_Point;
Dees_Troy70737fa2013-04-08 13:19:20 +0000135 Display_Name = full_line + 1;
136 Backup_Display_Name = Display_Name;
137 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400138 index = Mount_Point.size();
139 while (index < line_len) {
140 while (index < line_len && full_line[index] == '\0')
141 index++;
142 if (index >= line_len)
143 continue;
144 ptr = full_line + index;
145 if (item_index == 0) {
146 // File System
147 Fstab_File_System = ptr;
148 Current_File_System = ptr;
149 item_index++;
150 } else if (item_index == 1) {
151 // Primary Block Device
Dees_Troy38bd7602012-09-14 13:33:53 -0400152 if (Fstab_File_System == "mtd" || Fstab_File_System == "yaffs2") {
Dees_Troy094207a2012-09-26 12:00:39 -0400153 MTD_Name = ptr;
154 Find_MTD_Block_Device(MTD_Name);
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400155 } else if (Fstab_File_System == "bml") {
156 if (Mount_Point == "/boot")
157 MTD_Name = "boot";
158 else if (Mount_Point == "/recovery")
159 MTD_Name = "recovery";
160 Primary_Block_Device = ptr;
161 if (*ptr != '/')
Dees_Troy2673cec2013-04-02 20:22:16 +0000162 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 -0400163 } else if (*ptr != '/') {
Dees_Troy5bf43922012-09-07 16:07:55 -0400164 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000165 LOGERR("Invalid block device on '%s', '%s', %i\n", Line.c_str(), ptr, index);
Dees_Troy5bf43922012-09-07 16:07:55 -0400166 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000167 LOGINFO("Invalid block device on '%s', '%s', %i\n", Line.c_str(), ptr, index);
Dees_Troy5bf43922012-09-07 16:07:55 -0400168 return 0;
Dees_Troy38bd7602012-09-14 13:33:53 -0400169 } else {
170 Primary_Block_Device = ptr;
171 Find_Real_Block_Device(Primary_Block_Device, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400172 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400173 item_index++;
174 } else if (item_index > 1) {
175 if (*ptr == '/') {
176 // Alternate Block Device
177 Alternate_Block_Device = ptr;
178 Find_Real_Block_Device(Alternate_Block_Device, Display_Error);
179 } else if (strlen(ptr) > 7 && strncmp(ptr, "length=", 7) == 0) {
180 // Partition length
181 ptr += 7;
182 Length = atoi(ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400183 } else if (strlen(ptr) > 6 && strncmp(ptr, "flags=", 6) == 0) {
184 // Custom flags, save for later so that new values aren't overwritten by defaults
185 ptr += 6;
186 Flags = ptr;
Dees_Troy68cab492012-12-12 19:29:35 +0000187 Process_Flags(Flags, Display_Error);
Dees_Troy38bd7602012-09-14 13:33:53 -0400188 } else if (strlen(ptr) == 4 && (strncmp(ptr, "NULL", 4) == 0 || strncmp(ptr, "null", 4) == 0 || strncmp(ptr, "null", 4) == 0)) {
189 // Do nothing
Dees_Troy5bf43922012-09-07 16:07:55 -0400190 } else {
191 // Unhandled data
Dees_Troy2673cec2013-04-02 20:22:16 +0000192 LOGINFO("Unhandled fstab information: '%s', %i, line: '%s'\n", ptr, index, Line.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400193 }
194 }
195 while (index < line_len && full_line[index] != '\0')
196 index++;
197 }
198
199 if (!Is_File_System(Fstab_File_System) && !Is_Image(Fstab_File_System)) {
200 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000201 LOGERR("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400202 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000203 LOGINFO("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400204 return 0;
205 } else if (Is_File_System(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400206 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400207 Setup_File_System(Display_Error);
208 if (Mount_Point == "/system") {
209 Display_Name = "System";
Dees_Troya13d74f2013-03-24 08:54:55 -0500210 Backup_Display_Name = Display_Name;
211 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400212 Wipe_Available_in_GUI = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500213 Can_Be_Backed_Up = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400214 } else if (Mount_Point == "/data") {
215 Display_Name = "Data";
Dees_Troya13d74f2013-03-24 08:54:55 -0500216 Backup_Display_Name = Display_Name;
217 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400218 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400219 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500220 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000221 Can_Encrypt_Backup = true;
222 Use_Userdata_Encryption = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400223#ifdef RECOVERY_SDCARD_ON_DATA
Dees_Troya13d74f2013-03-24 08:54:55 -0500224 Storage_Name = "Internal Storage";
Dees_Troy5bf43922012-09-07 16:07:55 -0400225 Has_Data_Media = true;
Dees_Troy51127312012-09-08 13:08:49 -0400226 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500227 Is_Settings_Storage = true;
Dees_Troy51127312012-09-08 13:08:49 -0400228 Storage_Path = "/data/media";
Dees_Troy16b74352012-11-14 22:27:31 +0000229 Symlink_Path = Storage_Path;
Dees_Troy657c3092012-09-10 20:32:10 -0400230 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
231 Make_Dir("/emmc", Display_Error);
Dees_Troy657c3092012-09-10 20:32:10 -0400232 Symlink_Mount_Point = "/emmc";
233 } else {
234 Make_Dir("/sdcard", Display_Error);
Dees_Troy657c3092012-09-10 20:32:10 -0400235 Symlink_Mount_Point = "/sdcard";
236 }
Dees_Troy16b74352012-11-14 22:27:31 +0000237 if (Mount(false) && TWFunc::Path_Exists("/data/media/0")) {
238 Storage_Path = "/data/media/0";
239 Symlink_Path = Storage_Path;
240 DataManager::SetValue(TW_INTERNAL_PATH, "/data/media/0");
241 UnMount(true);
242 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400243#endif
244#ifdef TW_INCLUDE_CRYPTO
245 Can_Be_Encrypted = true;
Dees_Troy657c3092012-09-10 20:32:10 -0400246 char crypto_blkdev[255];
247 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
248 if (strcmp(crypto_blkdev, "error") != 0) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400249 DataManager::SetValue(TW_DATA_BLK_DEVICE, Primary_Block_Device);
Dees_Troy657c3092012-09-10 20:32:10 -0400250 DataManager::SetValue(TW_IS_DECRYPTED, 1);
251 Is_Encrypted = true;
252 Is_Decrypted = true;
253 Decrypted_Block_Device = crypto_blkdev;
Dees_Troy2673cec2013-04-02 20:22:16 +0000254 LOGINFO("Data already decrypted, new block device: '%s'\n", crypto_blkdev);
Dees_Troy657c3092012-09-10 20:32:10 -0400255 } else if (!Mount(false)) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400256 Is_Encrypted = true;
257 Is_Decrypted = false;
Gary Peck82599a82012-11-21 16:23:12 -0800258 Can_Be_Mounted = false;
259 Current_File_System = "emmc";
260 Setup_Image(Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400261 DataManager::SetValue(TW_IS_ENCRYPTED, 1);
262 DataManager::SetValue(TW_CRYPTO_PASSWORD, "");
263 DataManager::SetValue("tw_crypto_display", "");
Gary Peck82599a82012-11-21 16:23:12 -0800264 } else {
265 // Filesystem is not encrypted and the mount
266 // succeeded, so get it back to the original
267 // unmounted state
268 UnMount(false);
Dees_Troy51127312012-09-08 13:08:49 -0400269 }
Dees_Troy9b21af72012-10-01 15:51:46 -0400270 #ifdef RECOVERY_SDCARD_ON_DATA
271 if (!Is_Encrypted || (Is_Encrypted && Is_Decrypted))
272 Recreate_Media_Folder();
273 #endif
274#else
275 #ifdef RECOVERY_SDCARD_ON_DATA
276 Recreate_Media_Folder();
277 #endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400278#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400279 } else if (Mount_Point == "/cache") {
280 Display_Name = "Cache";
Dees_Troya13d74f2013-03-24 08:54:55 -0500281 Backup_Display_Name = Display_Name;
282 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400283 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400284 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500285 Can_Be_Backed_Up = true;
Dees_Troyce2fe772012-09-28 12:34:33 -0400286 if (Mount(false) && !TWFunc::Path_Exists("/cache/recovery/.")) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000287 LOGINFO("Recreating /cache/recovery folder.\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500288 if (mkdir("/cache/recovery", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0)
289 return -1;
Dees_Troyb46a6842012-09-25 11:06:46 -0400290 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400291 } else if (Mount_Point == "/datadata") {
Dees_Troy51127312012-09-08 13:08:49 -0400292 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400293 Display_Name = "DataData";
Dees_Troya13d74f2013-03-24 08:54:55 -0500294 Backup_Display_Name = Display_Name;
295 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400296 Is_SubPartition = true;
297 SubPartition_Of = "/data";
Dees_Troy5bf43922012-09-07 16:07:55 -0400298 DataManager::SetValue(TW_HAS_DATADATA, 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500299 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000300 Can_Encrypt_Backup = true;
301 Use_Userdata_Encryption = false; // This whole partition should be encrypted
Dees_Troy5bf43922012-09-07 16:07:55 -0400302 } else if (Mount_Point == "/sd-ext") {
Dees_Troy51127312012-09-08 13:08:49 -0400303 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400304 Display_Name = "SD-Ext";
Dees_Troya13d74f2013-03-24 08:54:55 -0500305 Backup_Display_Name = Display_Name;
306 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400307 Wipe_Available_in_GUI = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400308 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500309 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000310 Can_Encrypt_Backup = true;
311 Use_Userdata_Encryption = true;
Dees_Troy2c50e182012-09-26 20:05:28 -0400312 } else if (Mount_Point == "/boot") {
313 Display_Name = "Boot";
Dees_Troya13d74f2013-03-24 08:54:55 -0500314 Backup_Display_Name = Display_Name;
Dees_Troy2c50e182012-09-26 20:05:28 -0400315 DataManager::SetValue("tw_boot_is_mountable", 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500316 Can_Be_Backed_Up = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400317 }
318#ifdef TW_EXTERNAL_STORAGE_PATH
319 if (Mount_Point == EXPAND(TW_EXTERNAL_STORAGE_PATH)) {
320 Is_Storage = true;
321 Storage_Path = EXPAND(TW_EXTERNAL_STORAGE_PATH);
Dees_Troyc51f1f92012-09-20 15:32:13 -0400322 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500323 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400324#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000325 if (Mount_Point == "/sdcard" || Mount_Point == "/external_sd" || Mount_Point == "/external_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400326 Is_Storage = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400327 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500328 Wipe_Available_in_GUI = true;
Dees_Troye58d5262012-09-21 12:27:57 -0400329#ifndef RECOVERY_SDCARD_ON_DATA
330 Setup_AndSec();
Dees_Troy8e337f32012-10-13 22:07:49 -0400331 Mount_Storage_Retry();
Dees_Troye58d5262012-09-21 12:27:57 -0400332#endif
Dees_Troy8170a922012-09-18 15:40:25 -0400333#endif
Dees_Troyb05ddee2013-01-28 20:24:50 +0000334 }
Dees_Troy8170a922012-09-18 15:40:25 -0400335#ifdef TW_INTERNAL_STORAGE_PATH
336 if (Mount_Point == EXPAND(TW_INTERNAL_STORAGE_PATH)) {
337 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500338 Is_Settings_Storage = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400339 Storage_Path = EXPAND(TW_INTERNAL_STORAGE_PATH);
Dees_Troya13d74f2013-03-24 08:54:55 -0500340 Wipe_Available_in_GUI = true;
Dees_Troye58d5262012-09-21 12:27:57 -0400341#ifndef RECOVERY_SDCARD_ON_DATA
342 Setup_AndSec();
Dees_Troy8e337f32012-10-13 22:07:49 -0400343 Mount_Storage_Retry();
Dees_Troye58d5262012-09-21 12:27:57 -0400344#endif
Dees_Troy8170a922012-09-18 15:40:25 -0400345 }
346#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000347 if (Mount_Point == "/emmc" || Mount_Point == "/internal_sd" || Mount_Point == "/internal_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400348 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500349 Is_Settings_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500350 Wipe_Available_in_GUI = true;
Dees_Troye58d5262012-09-21 12:27:57 -0400351#ifndef RECOVERY_SDCARD_ON_DATA
352 Setup_AndSec();
Dees_Troy8e337f32012-10-13 22:07:49 -0400353 Mount_Storage_Retry();
Dees_Troye58d5262012-09-21 12:27:57 -0400354#endif
Dees_Troy8170a922012-09-18 15:40:25 -0400355 }
356#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400357 } else if (Is_Image(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400358 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400359 Setup_Image(Display_Error);
Dees_Troya13d74f2013-03-24 08:54:55 -0500360 if (Mount_Point == "/boot") {
361 Display_Name = "Boot";
362 Backup_Display_Name = Display_Name;
363 Can_Be_Backed_Up = true;
364 } else if (Mount_Point == "/recovery") {
365 Display_Name = "Recovery";
366 Backup_Display_Name = Display_Name;
367 Can_Be_Backed_Up = true;
368 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400369 }
370
Dees_Troy51127312012-09-08 13:08:49 -0400371 // Process any custom flags
372 if (Flags.size() > 0)
373 Process_Flags(Flags, Display_Error);
Dees_Troy51127312012-09-08 13:08:49 -0400374 return true;
375}
376
377bool TWPartition::Process_Flags(string Flags, bool Display_Error) {
378 char flags[MAX_FSTAB_LINE_LENGTH];
Dees_Troya13d74f2013-03-24 08:54:55 -0500379 int flags_len, index = 0, ptr_len;
Dees_Troy51127312012-09-08 13:08:49 -0400380 char* ptr;
Dees_Troya13d74f2013-03-24 08:54:55 -0500381 bool skip = false, has_display_name = false, has_storage_name = false, has_backup_name = false;
Dees_Troy51127312012-09-08 13:08:49 -0400382
383 strcpy(flags, Flags.c_str());
384 flags_len = Flags.size();
385 for (index = 0; index < flags_len; index++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500386 if (flags[index] == 34)
387 skip = !skip;
388 if (!skip && flags[index] == ';')
Dees_Troy51127312012-09-08 13:08:49 -0400389 flags[index] = '\0';
390 }
391
392 index = 0;
393 while (index < flags_len) {
394 while (index < flags_len && flags[index] == '\0')
395 index++;
396 if (index >= flags_len)
397 continue;
398 ptr = flags + index;
Dees_Troya13d74f2013-03-24 08:54:55 -0500399 ptr_len = strlen(ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400400 if (strcmp(ptr, "removable") == 0) {
401 Removable = true;
402 } else if (strcmp(ptr, "storage") == 0) {
403 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500404 } else if (strcmp(ptr, "settingsstorage") == 0) {
405 Is_Storage = true;
Dees_Troy63c8df72012-09-10 14:02:05 -0400406 } else if (strcmp(ptr, "canbewiped") == 0) {
407 Can_Be_Wiped = true;
Hashcodedabfd492013-08-29 22:45:30 -0700408 } else if (strcmp(ptr, "usermrf") == 0) {
409 Use_Rm_Rf = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500410 } else if (ptr_len > 7 && strncmp(ptr, "backup=", 7) == 0) {
411 ptr += 7;
412 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y')
413 Can_Be_Backed_Up = true;
414 else
415 Can_Be_Backed_Up = false;
Dees_Troy63c8df72012-09-10 14:02:05 -0400416 } else if (strcmp(ptr, "wipeingui") == 0) {
417 Can_Be_Wiped = true;
418 Wipe_Available_in_GUI = true;
419 } else if (strcmp(ptr, "wipeduringfactoryreset") == 0) {
420 Can_Be_Wiped = true;
421 Wipe_Available_in_GUI = true;
422 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500423 } else if (ptr_len > 15 && strncmp(ptr, "subpartitionof=", 15) == 0) {
Dees_Troy2c4c26f2013-01-28 15:26:43 +0000424 ptr += 15;
Dees_Troy51127312012-09-08 13:08:49 -0400425 Is_SubPartition = true;
426 SubPartition_Of = ptr;
Dees_Troy68cab492012-12-12 19:29:35 +0000427 } else if (strcmp(ptr, "ignoreblkid") == 0) {
428 Ignore_Blkid = true;
Dees_Troy16c2b312013-01-15 16:51:18 +0000429 } else if (strcmp(ptr, "retainlayoutversion") == 0) {
430 Retain_Layout_Version = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500431 } else if (ptr_len > 8 && strncmp(ptr, "symlink=", 8) == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400432 ptr += 8;
433 Symlink_Path = ptr;
Dees_Troya13d74f2013-03-24 08:54:55 -0500434 } else if (ptr_len > 8 && strncmp(ptr, "display=", 8) == 0) {
435 has_display_name = true;
Dees_Troy51127312012-09-08 13:08:49 -0400436 ptr += 8;
Dees_Troya13d74f2013-03-24 08:54:55 -0500437 if (*ptr == '\"') ptr++;
Dees_Troy51127312012-09-08 13:08:49 -0400438 Display_Name = ptr;
Dees_Troya13d74f2013-03-24 08:54:55 -0500439 if (Display_Name.substr(Display_Name.size() - 1, 1) == "\"") {
440 Display_Name.resize(Display_Name.size() - 1);
441 }
442 } else if (ptr_len > 11 && strncmp(ptr, "storagename=", 11) == 0) {
443 has_storage_name = true;
444 ptr += 11;
445 if (*ptr == '\"') ptr++;
446 Storage_Name = ptr;
447 if (Storage_Name.substr(Storage_Name.size() - 1, 1) == "\"") {
448 Storage_Name.resize(Storage_Name.size() - 1);
449 }
450 } else if (ptr_len > 11 && strncmp(ptr, "backupname=", 10) == 0) {
451 has_backup_name = true;
452 ptr += 10;
453 if (*ptr == '\"') ptr++;
454 Backup_Display_Name = ptr;
455 if (Backup_Display_Name.substr(Backup_Display_Name.size() - 1, 1) == "\"") {
456 Backup_Display_Name.resize(Backup_Display_Name.size() - 1);
457 }
458 } else if (ptr_len > 10 && strncmp(ptr, "blocksize=", 10) == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400459 ptr += 10;
460 Format_Block_Size = atoi(ptr);
Dees_Troya13d74f2013-03-24 08:54:55 -0500461 } else if (ptr_len > 7 && strncmp(ptr, "length=", 7) == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400462 ptr += 7;
463 Length = atoi(ptr);
Dees_Troy83bd4832013-05-04 12:39:56 +0000464 } else if (ptr_len > 17 && strncmp(ptr, "canencryptbackup=", 17) == 0) {
465 ptr += 17;
466 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y')
467 Can_Encrypt_Backup = true;
468 else
469 Can_Encrypt_Backup = false;
470 } else if (ptr_len > 21 && strncmp(ptr, "userdataencryptbackup=", 21) == 0) {
471 ptr += 21;
472 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y') {
473 Can_Encrypt_Backup = true;
474 Use_Userdata_Encryption = true;
475 } else {
476 Use_Userdata_Encryption = false;
477 }
Dees_Troy51127312012-09-08 13:08:49 -0400478 } else {
479 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000480 LOGERR("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400481 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000482 LOGINFO("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400483 }
484 while (index < flags_len && flags[index] != '\0')
485 index++;
486 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500487 if (has_display_name && !has_storage_name)
488 Storage_Name = Display_Name;
489 if (!has_display_name && has_storage_name)
490 Display_Name = Storage_Name;
Dees_Troy74fb2e92013-04-15 14:35:47 +0000491 if (has_display_name && !has_backup_name && Backup_Display_Name != "Android Secure")
Dees_Troya13d74f2013-03-24 08:54:55 -0500492 Backup_Display_Name = Display_Name;
493 if (!has_display_name && has_backup_name)
494 Display_Name = Backup_Display_Name;
Dees_Troy51127312012-09-08 13:08:49 -0400495 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400496}
497
Dees_Troy5bf43922012-09-07 16:07:55 -0400498bool TWPartition::Is_File_System(string File_System) {
499 if (File_System == "ext2" ||
Dees_Troy63c8df72012-09-10 14:02:05 -0400500 File_System == "ext3" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400501 File_System == "ext4" ||
502 File_System == "vfat" ||
503 File_System == "ntfs" ||
504 File_System == "yaffs2" ||
bigbiff bigbiff3e146522012-11-14 14:32:59 -0500505 File_System == "exfat" ||
Dees_Troye5017042013-08-29 16:38:55 +0000506 File_System == "f2fs" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400507 File_System == "auto")
508 return true;
509 else
510 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400511}
512
Dees_Troy5bf43922012-09-07 16:07:55 -0400513bool TWPartition::Is_Image(string File_System) {
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400514 if (File_System == "emmc" || File_System == "mtd" || File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -0400515 return true;
516 else
517 return false;
518}
519
Dees_Troy51127312012-09-08 13:08:49 -0400520bool TWPartition::Make_Dir(string Path, bool Display_Error) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400521 if (!TWFunc::Path_Exists(Path)) {
Dees_Troy51127312012-09-08 13:08:49 -0400522 if (mkdir(Path.c_str(), 0777) == -1) {
523 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000524 LOGERR("Can not create '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400525 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000526 LOGINFO("Can not create '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400527 return false;
528 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000529 LOGINFO("Created '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400530 return true;
531 }
532 }
533 return true;
534}
535
Dees_Troy5bf43922012-09-07 16:07:55 -0400536void TWPartition::Setup_File_System(bool Display_Error) {
537 struct statfs st;
538
539 Can_Be_Mounted = true;
540 Can_Be_Wiped = true;
541
Dees_Troy5bf43922012-09-07 16:07:55 -0400542 // Make the mount point folder if it doesn't exist
Dees_Troy51127312012-09-08 13:08:49 -0400543 Make_Dir(Mount_Point, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400544 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
545 Backup_Name = Display_Name;
546 Backup_Method = FILES;
547}
548
549void TWPartition::Setup_Image(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400550 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
551 Backup_Name = Display_Name;
Gary Peck82599a82012-11-21 16:23:12 -0800552 if (Current_File_System == "emmc")
Dees_Troy5bf43922012-09-07 16:07:55 -0400553 Backup_Method = DD;
Gary Peck82599a82012-11-21 16:23:12 -0800554 else if (Current_File_System == "mtd" || Current_File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -0400555 Backup_Method = FLASH_UTILS;
556 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000557 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 -0400558 if (Find_Partition_Size()) {
559 Used = Size;
560 Backup_Size = Size;
Dees_Troy51a0e822012-09-05 15:24:24 -0400561 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400562 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000563 LOGERR("Unable to find parition size for '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400564 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000565 LOGINFO("Unable to find parition size for '%s'\n", Mount_Point.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400566 }
567}
568
Dees_Troye58d5262012-09-21 12:27:57 -0400569void TWPartition::Setup_AndSec(void) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500570 Backup_Display_Name = "Android Secure";
Dees_Troye58d5262012-09-21 12:27:57 -0400571 Backup_Name = "and-sec";
Dees_Troya13d74f2013-03-24 08:54:55 -0500572 Can_Be_Backed_Up = true;
Dees_Troye58d5262012-09-21 12:27:57 -0400573 Has_Android_Secure = true;
574 Symlink_Path = Mount_Point + "/.android_secure";
575 Symlink_Mount_Point = "/and-sec";
576 Backup_Path = Symlink_Mount_Point;
577 Make_Dir("/and-sec", true);
578 Recreate_AndSec_Folder();
579}
580
Dees_Troy5bf43922012-09-07 16:07:55 -0400581void TWPartition::Find_Real_Block_Device(string& Block, bool Display_Error) {
582 char device[512], realDevice[512];
583
584 strcpy(device, Block.c_str());
585 memset(realDevice, 0, sizeof(realDevice));
586 while (readlink(device, realDevice, sizeof(realDevice)) > 0)
587 {
588 strcpy(device, realDevice);
589 memset(realDevice, 0, sizeof(realDevice));
590 }
591
592 if (device[0] != '/') {
593 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000594 LOGERR("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400595 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000596 LOGINFO("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400597 return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400598 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400599 Block = device;
600 return;
601 }
602}
603
Dees_Troy8e337f32012-10-13 22:07:49 -0400604void TWPartition::Mount_Storage_Retry(void) {
605 // On some devices, storage doesn't want to mount right away, retry and sleep
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500606 if (!Mount(true)) {
Dees_Troy8e337f32012-10-13 22:07:49 -0400607 int retry_count = 5;
608 while (retry_count > 0 && !Mount(false)) {
609 usleep(500000);
610 retry_count--;
611 }
612 Mount(true);
613 }
614}
615
Dees_Troy38bd7602012-09-14 13:33:53 -0400616bool TWPartition::Find_MTD_Block_Device(string MTD_Name) {
617 FILE *fp = NULL;
618 char line[255];
619
620 fp = fopen("/proc/mtd", "rt");
621 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000622 LOGERR("Device does not support /proc/mtd\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400623 return false;
624 }
625
626 while (fgets(line, sizeof(line), fp) != NULL)
627 {
628 char device[32], label[32];
629 unsigned long size = 0;
630 char* fstype = NULL;
631 int deviceId;
632
633 sscanf(line, "%s %lx %*s %*c%s", device, &size, label);
634
635 // Skip header and blank lines
636 if ((strcmp(device, "dev:") == 0) || (strlen(line) < 8))
637 continue;
638
639 // Strip off the trailing " from the label
640 label[strlen(label)-1] = '\0';
641
642 if (strcmp(label, MTD_Name.c_str()) == 0) {
643 // We found our device
644 // Strip off the trailing : from the device
645 device[strlen(device)-1] = '\0';
646 if (sscanf(device,"mtd%d", &deviceId) == 1) {
647 sprintf(device, "/dev/block/mtdblock%d", deviceId);
648 Primary_Block_Device = device;
Dees_Troy76543db2013-06-19 16:24:30 +0000649 fclose(fp);
650 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -0400651 }
652 }
653 }
654 fclose(fp);
655
656 return false;
657}
658
Dees_Troy51127312012-09-08 13:08:49 -0400659bool TWPartition::Get_Size_Via_statfs(bool Display_Error) {
660 struct statfs st;
661 string Local_Path = Mount_Point + "/.";
662
663 if (!Mount(Display_Error))
664 return false;
665
666 if (statfs(Local_Path.c_str(), &st) != 0) {
667 if (!Removable) {
668 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000669 LOGERR("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400670 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000671 LOGINFO("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400672 }
673 return false;
674 }
675 Size = (st.f_blocks * st.f_bsize);
676 Used = ((st.f_blocks - st.f_bfree) * st.f_bsize);
677 Free = (st.f_bfree * st.f_bsize);
678 Backup_Size = Used;
679 return true;
680}
681
682bool TWPartition::Get_Size_Via_df(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400683 FILE* fp;
684 char command[255], line[512];
685 int include_block = 1;
686 unsigned int min_len;
687
688 if (!Mount(Display_Error))
689 return false;
690
Dees_Troy38bd7602012-09-14 13:33:53 -0400691 min_len = Actual_Block_Device.size() + 2;
Dees_Troy51127312012-09-08 13:08:49 -0400692 sprintf(command, "df %s > /tmp/dfoutput.txt", Mount_Point.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +0200693 TWFunc::Exec_Cmd(command);
Dees_Troy51127312012-09-08 13:08:49 -0400694 fp = fopen("/tmp/dfoutput.txt", "rt");
695 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000696 LOGINFO("Unable to open /tmp/dfoutput.txt.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400697 return false;
Dees_Troy51127312012-09-08 13:08:49 -0400698 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400699
700 while (fgets(line, sizeof(line), fp) != NULL)
701 {
702 unsigned long blocks, used, available;
703 char device[64];
704 char tmpString[64];
705
706 if (strncmp(line, "Filesystem", 10) == 0)
707 continue;
708 if (strlen(line) < min_len) {
709 include_block = 0;
710 continue;
711 }
712 if (include_block) {
713 sscanf(line, "%s %lu %lu %lu", device, &blocks, &used, &available);
714 } else {
715 // The device block string is so long that the df information is on the next line
716 int space_count = 0;
Dees_Troye58d5262012-09-21 12:27:57 -0400717 sprintf(tmpString, "/dev/block/%s", Actual_Block_Device.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400718 while (tmpString[space_count] == 32)
719 space_count++;
720 sscanf(line + space_count, "%lu %lu %lu", &blocks, &used, &available);
721 }
722
723 // Adjust block size to byte size
724 Size = blocks * 1024ULL;
725 Used = used * 1024ULL;
726 Free = available * 1024ULL;
727 Backup_Size = Used;
728 }
729 fclose(fp);
730 return true;
731}
732
Dees_Troy5bf43922012-09-07 16:07:55 -0400733bool TWPartition::Find_Partition_Size(void) {
734 FILE* fp;
735 char line[512];
736 string tmpdevice;
737
igoriok87e3d932013-01-31 21:03:53 +0200738 fp = fopen("/proc/dumchar_info", "rt");
739 if (fp != NULL) {
740 while (fgets(line, sizeof(line), fp) != NULL)
741 {
742 char label[32], device[32];
743 unsigned long size = 0;
744
745 sscanf(line, "%s %lx %*lx %*lu %s", label, &size, device);
746
747 // Skip header, annotation and blank lines
748 if ((strncmp(device, "/dev/", 5) != 0) || (strlen(line) < 8))
749 continue;
750
751 tmpdevice = "/dev/";
752 tmpdevice += label;
753 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
754 Size = size;
755 fclose(fp);
756 return true;
757 }
758 }
759 }
760
Dees_Troy5bf43922012-09-07 16:07:55 -0400761 // In this case, we'll first get the partitions we care about (with labels)
762 fp = fopen("/proc/partitions", "rt");
763 if (fp == NULL)
764 return false;
765
766 while (fgets(line, sizeof(line), fp) != NULL)
767 {
768 unsigned long major, minor, blocks;
769 char device[512];
770 char tmpString[64];
771
Dees_Troy63c8df72012-09-10 14:02:05 -0400772 if (strlen(line) < 7 || line[0] == 'm') continue;
Dees_Troy5bf43922012-09-07 16:07:55 -0400773 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
774
775 tmpdevice = "/dev/block/";
776 tmpdevice += device;
Dees_Troy38bd7602012-09-14 13:33:53 -0400777 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400778 // Adjust block size to byte size
779 Size = blocks * 1024ULL;
780 fclose(fp);
781 return true;
782 }
783 }
784 fclose(fp);
785 return false;
786}
787
Dees_Troy5bf43922012-09-07 16:07:55 -0400788bool TWPartition::Is_Mounted(void) {
789 if (!Can_Be_Mounted)
790 return false;
791
792 struct stat st1, st2;
793 string test_path;
794
795 // Check to see if the mount point directory exists
796 test_path = Mount_Point + "/.";
797 if (stat(test_path.c_str(), &st1) != 0) return false;
798
799 // Check to see if the directory above the mount point exists
800 test_path = Mount_Point + "/../.";
801 if (stat(test_path.c_str(), &st2) != 0) return false;
802
803 // Compare the device IDs -- if they match then we're (probably) using tmpfs instead of an actual device
804 int ret = (st1.st_dev != st2.st_dev) ? true : false;
805
806 return ret;
807}
808
809bool TWPartition::Mount(bool Display_Error) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000810 int exfat_mounted = 0;
811
Dees_Troy5bf43922012-09-07 16:07:55 -0400812 if (Is_Mounted()) {
813 return true;
814 } else if (!Can_Be_Mounted) {
815 return false;
816 }
Dees_Troy38bd7602012-09-14 13:33:53 -0400817
818 Find_Actual_Block_Device();
819
820 // Check the current file system before mounting
821 Check_FS_Type();
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000822 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/exfat-fuse")) {
Dees_Troye34c1332013-02-06 19:13:00 +0000823 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 +0000824 LOGINFO("cmd: %s\n", cmd.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000825 string result;
826 if (TWFunc::Exec_Cmd(cmd, result) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000827 LOGINFO("exfat-fuse failed to mount with result '%s', trying vfat\n", result.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000828 Current_File_System = "vfat";
829 } else {
830#ifdef TW_NO_EXFAT_FUSE
831 UnMount(false);
832 // We'll let the kernel handle it but using exfat-fuse to detect if the file system is actually exfat
833 // Some kernels let us mount vfat as exfat which doesn't work out too well
834#else
835 exfat_mounted = 1;
836#endif
837 }
838 }
Dees_Troy22042032012-12-18 21:23:08 +0000839 if (Fstab_File_System == "yaffs2") {
840 // mount an MTD partition as a YAFFS2 filesystem.
Dees_Troy76543db2013-06-19 16:24:30 +0000841 const unsigned long flags = MS_NOATIME | MS_NODEV | MS_NODIRATIME;
842 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags, NULL) < 0) {
843 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags | MS_RDONLY, NULL) < 0) {
844 if (Display_Error)
845 LOGERR("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
846 else
847 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
848 return false;
849 } else {
850 LOGINFO("Mounted '%s' (MTD) as RO\n", Mount_Point.c_str());
851 return true;
852 }
853 } else {
854 struct stat st;
855 string test_path = Mount_Point;
856 if (stat(test_path.c_str(), &st) < 0) {
857 if (Display_Error)
858 LOGERR("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
859 else
860 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
861 return false;
862 }
863 mode_t new_mode = st.st_mode | S_IXUSR | S_IXGRP | S_IXOTH;
864 if (new_mode != st.st_mode) {
865 LOGINFO("Fixing execute permissions for %s\n", Mount_Point.c_str());
866 if (chmod(Mount_Point.c_str(), new_mode) < 0) {
867 if (Display_Error)
868 LOGERR("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
869 else
870 LOGINFO("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
871 return false;
872 }
873 }
Dees_Troy22042032012-12-18 21:23:08 +0000874 return true;
Dees_Troy76543db2013-06-19 16:24:30 +0000875 }
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000876 } else if (!exfat_mounted && mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Current_File_System.c_str(), 0, NULL) != 0) {
877#ifdef TW_NO_EXFAT_FUSE
878 if (Current_File_System == "exfat") {
Dees_Troy2673cec2013-04-02 20:22:16 +0000879 LOGINFO("Mounting exfat failed, trying vfat...\n");
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000880 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), "vfat", 0, NULL) != 0) {
Dees_Troy85f44ed2013-01-09 18:42:36 +0000881 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000882 LOGERR("Unable to mount '%s'\n", Mount_Point.c_str());
Dees_Troy85f44ed2013-01-09 18:42:36 +0000883 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000884 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
885 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 +0000886 return false;
Dees_Troy85f44ed2013-01-09 18:42:36 +0000887 }
Dees_Troyb05ddee2013-01-28 20:24:50 +0000888 } else {
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000889#endif
890 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000891 LOGERR("Unable to mount '%s'\n", Mount_Point.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000892 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000893 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
894 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 +0000895 return false;
896#ifdef TW_NO_EXFAT_FUSE
Dees_Troy85f44ed2013-01-09 18:42:36 +0000897 }
898#endif
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000899 }
900#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
901 string MetaEcfsFile = EXPAND(TW_EXTERNAL_STORAGE_PATH);
902 MetaEcfsFile += "/.MetaEcfsFile";
903 if (EcryptFS_Password.size() > 0 && PartitionManager.Mount_By_Path("/data", false) && TWFunc::Path_Exists(MetaEcfsFile)) {
904 if (mount_ecryptfs_drive(EcryptFS_Password.c_str(), Mount_Point.c_str(), Mount_Point.c_str(), 0) != 0) {
905 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000906 LOGERR("Unable to mount ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000907 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000908 LOGINFO("Unable to mount ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000909 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000910 LOGINFO("Successfully mounted ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000911 Is_Decrypted = true;
Dees_Troy51127312012-09-08 13:08:49 -0400912 }
Dees_Troy066eb302013-08-23 17:20:32 +0000913 } else if (Mount_Point == EXPAND(TW_EXTERNAL_STORAGE_PATH)) {
914 if (Is_Decrypted)
915 LOGINFO("Mounting external storage, '%s' is not encrypted\n", Mount_Point.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000916 Is_Decrypted = false;
917 }
918#endif
919 if (Removable)
920 Update_Size(Display_Error);
921
922 if (!Symlink_Mount_Point.empty()) {
Vojtech Bocek05534202013-09-11 08:11:56 +0200923 string Command = "mount '" + Symlink_Path + "' '" + Symlink_Mount_Point + "'";
924 TWFunc::Exec_Cmd(Command);
Dees_Troy5bf43922012-09-07 16:07:55 -0400925 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400926 return true;
927}
928
929bool TWPartition::UnMount(bool Display_Error) {
930 if (Is_Mounted()) {
931 int never_unmount_system;
932
933 DataManager::GetValue(TW_DONT_UNMOUNT_SYSTEM, never_unmount_system);
934 if (never_unmount_system == 1 && Mount_Point == "/system")
935 return true; // Never unmount system if you're not supposed to unmount it
936
Dees_Troyc8bafa12013-01-10 15:43:00 +0000937#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
938 if (EcryptFS_Password.size() > 0) {
939 if (unmount_ecryptfs_drive(Mount_Point.c_str()) != 0) {
940 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000941 LOGERR("Unable to unmount ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troyc8bafa12013-01-10 15:43:00 +0000942 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000943 LOGINFO("Unable to unmount ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troyc8bafa12013-01-10 15:43:00 +0000944 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000945 LOGINFO("Successfully unmounted ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troyc8bafa12013-01-10 15:43:00 +0000946 }
947 }
948#endif
949
Dees_Troy38bd7602012-09-14 13:33:53 -0400950 if (!Symlink_Mount_Point.empty())
951 umount(Symlink_Mount_Point.c_str());
952
Dees_Troyb05ddee2013-01-28 20:24:50 +0000953 umount(Mount_Point.c_str());
954 if (Is_Mounted()) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400955 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000956 LOGERR("Unable to unmount '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400957 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000958 LOGINFO("Unable to unmount '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400959 return false;
960 } else
961 return true;
962 } else {
963 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400964 }
965}
966
Gary Peck43acadf2012-11-21 21:19:01 -0800967bool TWPartition::Wipe(string New_File_System) {
Dees_Troy16c2b312013-01-15 16:51:18 +0000968 bool wiped = false, update_crypt = false;
969 int check;
970 string Layout_Filename = Mount_Point + "/.layout_version";
971
Dees_Troy38bd7602012-09-14 13:33:53 -0400972 if (!Can_Be_Wiped) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000973 LOGERR("Partition '%s' cannot be wiped.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -0400974 return false;
975 }
976
Dees_Troyc51f1f92012-09-20 15:32:13 -0400977 if (Mount_Point == "/cache")
Dees_Troy2673cec2013-04-02 20:22:16 +0000978 Log_Offset = 0;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400979
Dees_Troyce675462013-01-09 19:48:21 +0000980#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
981 if (Mount_Point == "/data" && Mount(false)) {
982 if (TWFunc::Path_Exists("/data/system/edk_p_sd"))
983 TWFunc::copy_file("/data/system/edk_p_sd", "/tmp/edk_p_sd", 0600);
984 }
985#endif
986
Dees_Troy16c2b312013-01-15 16:51:18 +0000987 if (Retain_Layout_Version && Mount(false) && TWFunc::Path_Exists(Layout_Filename))
988 TWFunc::copy_file(Layout_Filename, "/.layout_version", 0600);
989 else
990 unlink("/.layout_version");
Dees_Troy38bd7602012-09-14 13:33:53 -0400991
Dees_Troy16c2b312013-01-15 16:51:18 +0000992 if (Has_Data_Media) {
993 wiped = Wipe_Data_Without_Wiping_Media();
994 } else {
Gary Pecke8bc5d72012-12-21 06:45:25 -0800995
Dees_Troy16c2b312013-01-15 16:51:18 +0000996 DataManager::GetValue(TW_RM_RF_VAR, check);
997
Hashcodedabfd492013-08-29 22:45:30 -0700998 if (check || Use_Rm_Rf)
Dees_Troy16c2b312013-01-15 16:51:18 +0000999 wiped = Wipe_RMRF();
1000 else if (New_File_System == "ext4")
1001 wiped = Wipe_EXT4();
1002 else if (New_File_System == "ext2" || New_File_System == "ext3")
1003 wiped = Wipe_EXT23(New_File_System);
1004 else if (New_File_System == "vfat")
1005 wiped = Wipe_FAT();
1006 else if (New_File_System == "exfat")
1007 wiped = Wipe_EXFAT();
1008 else if (New_File_System == "yaffs2")
1009 wiped = Wipe_MTD();
Dees_Troye5017042013-08-29 16:38:55 +00001010 else if (New_File_System == "f2fs")
1011 wiped = Wipe_F2FS();
Dees_Troy16c2b312013-01-15 16:51:18 +00001012 else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001013 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 +00001014 unlink("/.layout_version");
1015 return false;
1016 }
1017 update_crypt = wiped;
Gary Pecke8bc5d72012-12-21 06:45:25 -08001018 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001019
Gary Pecke8bc5d72012-12-21 06:45:25 -08001020 if (wiped) {
Dees_Troyce675462013-01-09 19:48:21 +00001021#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
1022 if (Mount_Point == "/data" && Mount(false)) {
1023 if (TWFunc::Path_Exists("/tmp/edk_p_sd")) {
1024 Make_Dir("/data/system", true);
1025 TWFunc::copy_file("/tmp/edk_p_sd", "/data/system/edk_p_sd", 0600);
1026 }
1027 }
1028#endif
Dees_Troy16c2b312013-01-15 16:51:18 +00001029
Dees_Troy1c1ac442013-01-17 21:42:14 +00001030 if (Mount_Point == "/cache")
1031 DataManager::Output_Version();
1032
Dees_Troy16c2b312013-01-15 16:51:18 +00001033 if (TWFunc::Path_Exists("/.layout_version") && Mount(false))
1034 TWFunc::copy_file("/.layout_version", Layout_Filename, 0600);
1035
1036 if (update_crypt) {
1037 Setup_File_System(false);
1038 if (Is_Encrypted && !Is_Decrypted) {
1039 // just wiped an encrypted partition back to its unencrypted state
1040 Is_Encrypted = false;
1041 Is_Decrypted = false;
1042 Decrypted_Block_Device = "";
1043 if (Mount_Point == "/data") {
1044 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1045 DataManager::SetValue(TW_IS_DECRYPTED, 0);
1046 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001047 }
1048 }
1049 }
1050 return wiped;
Dees_Troy51a0e822012-09-05 15:24:24 -04001051}
1052
Gary Peck43acadf2012-11-21 21:19:01 -08001053bool TWPartition::Wipe() {
Gary Peck82599a82012-11-21 16:23:12 -08001054 if (Is_File_System(Current_File_System))
1055 return Wipe(Current_File_System);
1056 else
1057 return Wipe(Fstab_File_System);
Gary Peck43acadf2012-11-21 21:19:01 -08001058}
1059
Dees_Troye58d5262012-09-21 12:27:57 -04001060bool TWPartition::Wipe_AndSec(void) {
1061 if (!Has_Android_Secure)
1062 return false;
1063
Dees_Troye58d5262012-09-21 12:27:57 -04001064 if (!Mount(true))
1065 return false;
1066
Dees_Troy2673cec2013-04-02 20:22:16 +00001067 gui_print("Wiping %s\n", Backup_Display_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001068 TWFunc::removeDir(Mount_Point + "/.android_secure/", true);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001069 return true;
Dees_Troye58d5262012-09-21 12:27:57 -04001070}
1071
Dees_Troy51a0e822012-09-05 15:24:24 -04001072bool TWPartition::Backup(string backup_folder) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001073 if (Backup_Method == FILES)
1074 return Backup_Tar(backup_folder);
1075 else if (Backup_Method == DD)
1076 return Backup_DD(backup_folder);
1077 else if (Backup_Method == FLASH_UTILS)
1078 return Backup_Dump_Image(backup_folder);
Dees_Troy2673cec2013-04-02 20:22:16 +00001079 LOGERR("Unknown backup method for '%s'\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001080 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001081}
1082
Dees_Troy43d8b002012-09-17 16:00:01 -04001083bool TWPartition::Check_MD5(string restore_folder) {
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001084 string Full_Filename, md5file;
Dees_Troy43d8b002012-09-17 16:00:01 -04001085 char split_filename[512];
1086 int index = 0;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001087 twrpDigest md5sum;
Dees_Troy43d8b002012-09-17 16:00:01 -04001088
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001089 memset(split_filename, 0, sizeof(split_filename));
Dees_Troy43d8b002012-09-17 16:00:01 -04001090 Full_Filename = restore_folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04001091 if (!TWFunc::Path_Exists(Full_Filename)) {
1092 // This is a split archive, we presume
1093 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
Dees_Troy2673cec2013-04-02 20:22:16 +00001094 LOGINFO("split_filename: %s\n", split_filename);
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001095 md5file = split_filename;
1096 md5file += ".md5";
1097 if (!TWFunc::Path_Exists(md5file)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001098 LOGERR("No md5 file found for '%s'.\n", split_filename);
1099 LOGERR("Please unselect Enable MD5 verification to restore.\n");
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001100 return false;
1101 }
1102 md5sum.setfn(split_filename);
Dees_Troy83bd4832013-05-04 12:39:56 +00001103 while (index < 1000) {
1104 if (TWFunc::Path_Exists(split_filename) && md5sum.verify_md5digest() != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001105 LOGERR("MD5 failed to match on '%s'.\n", split_filename);
Dees_Troy43d8b002012-09-17 16:00:01 -04001106 return false;
1107 }
1108 index++;
1109 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001110 md5sum.setfn(split_filename);
Dees_Troy43d8b002012-09-17 16:00:01 -04001111 }
Dees_Troy4a2a1262012-09-18 09:33:47 -04001112 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001113 } else {
1114 // Single file archive
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001115 md5file = Full_Filename + ".md5";
1116 if (!TWFunc::Path_Exists(md5file)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001117 LOGERR("No md5 file found for '%s'.\n", Full_Filename.c_str());
1118 LOGERR("Please unselect Enable MD5 verification to restore.\n");
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001119 return false;
1120 }
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001121 md5sum.setfn(Full_Filename);
1122 if (md5sum.verify_md5digest() != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001123 LOGERR("MD5 failed to match on '%s'.\n", Full_Filename.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001124 return false;
1125 } else
1126 return true;
1127 }
1128 return false;
1129}
1130
Dees_Troy51a0e822012-09-05 15:24:24 -04001131bool TWPartition::Restore(string restore_folder) {
Gary Peck43acadf2012-11-21 21:19:01 -08001132 size_t first_period, second_period;
1133 string Restore_File_System;
1134
1135 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
Dees_Troy2673cec2013-04-02 20:22:16 +00001136 LOGINFO("Restore filename is: %s\n", Backup_FileName.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001137
1138 // Parse backup filename to extract the file system before wiping
1139 first_period = Backup_FileName.find(".");
1140 if (first_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001141 LOGERR("Unable to find file system (first period).\n");
Gary Peck43acadf2012-11-21 21:19:01 -08001142 return false;
1143 }
1144 Restore_File_System = Backup_FileName.substr(first_period + 1, Backup_FileName.size() - first_period - 1);
1145 second_period = Restore_File_System.find(".");
1146 if (second_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001147 LOGERR("Unable to find file system (second period).\n");
Gary Peck43acadf2012-11-21 21:19:01 -08001148 return false;
1149 }
1150 Restore_File_System.resize(second_period);
Dees_Troy2673cec2013-04-02 20:22:16 +00001151 LOGINFO("Restore file system is: '%s'.\n", Restore_File_System.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001152
1153 if (Is_File_System(Restore_File_System))
1154 return Restore_Tar(restore_folder, Restore_File_System);
1155 else if (Is_Image(Restore_File_System)) {
1156 if (Restore_File_System == "emmc")
1157 return Restore_DD(restore_folder);
1158 else if (Restore_File_System == "mtd" || Restore_File_System == "bml")
1159 return Restore_Flash_Image(restore_folder);
1160 }
1161
Dees_Troy2673cec2013-04-02 20:22:16 +00001162 LOGERR("Unknown restore method for '%s'\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001163 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001164}
1165
1166string TWPartition::Backup_Method_By_Name() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001167 if (Backup_Method == NONE)
1168 return "none";
1169 else if (Backup_Method == FILES)
1170 return "files";
1171 else if (Backup_Method == DD)
1172 return "dd";
1173 else if (Backup_Method == FLASH_UTILS)
1174 return "flash_utils";
1175 else
1176 return "undefined";
1177 return "ERROR!";
Dees_Troy51a0e822012-09-05 15:24:24 -04001178}
1179
1180bool TWPartition::Decrypt(string Password) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001181 LOGINFO("STUB TWPartition::Decrypt, password: '%s'\n", Password.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001182 // Is this needed?
Dees_Troy51a0e822012-09-05 15:24:24 -04001183 return 1;
1184}
1185
1186bool TWPartition::Wipe_Encryption() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001187 bool Save_Data_Media = Has_Data_Media;
1188
1189 if (!UnMount(true))
1190 return false;
1191
Dees_Troy38bd7602012-09-14 13:33:53 -04001192 Has_Data_Media = false;
Dees_Troy74fb2e92013-04-15 14:35:47 +00001193 Decrypted_Block_Device = "";
1194 Is_Decrypted = false;
1195 Is_Encrypted = false;
Gary Pecke8bc5d72012-12-21 06:45:25 -08001196 if (Wipe(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001197 Has_Data_Media = Save_Data_Media;
1198 if (Has_Data_Media && !Symlink_Mount_Point.empty()) {
1199 Recreate_Media_Folder();
1200 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001201 gui_print("You may need to reboot recovery to be able to use /data again.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001202 return true;
1203 } else {
1204 Has_Data_Media = Save_Data_Media;
Dees_Troy2673cec2013-04-02 20:22:16 +00001205 LOGERR("Unable to format to remove encryption.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001206 return false;
1207 }
1208 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001209}
1210
1211void TWPartition::Check_FS_Type() {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001212 const char* type;
1213 blkid_probe pr;
Dees_Troy5bf43922012-09-07 16:07:55 -04001214
Dees_Troy68cab492012-12-12 19:29:35 +00001215 if (Fstab_File_System == "yaffs2" || Fstab_File_System == "mtd" || Fstab_File_System == "bml" || Ignore_Blkid)
1216 return; // Running blkid on some mtd devices causes a massive crash or needs to be skipped
Dees_Troy5bf43922012-09-07 16:07:55 -04001217
Dees_Troy38bd7602012-09-14 13:33:53 -04001218 Find_Actual_Block_Device();
Dees_Troy8170a922012-09-18 15:40:25 -04001219 if (!Is_Present)
1220 return;
Dees_Troy51127312012-09-08 13:08:49 -04001221
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001222 pr = blkid_new_probe_from_filename(Actual_Block_Device.c_str());
1223 if (blkid_do_fullprobe(pr)) {
1224 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00001225 LOGINFO("Can't probe device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001226 return;
Dees_Troy5bf43922012-09-07 16:07:55 -04001227 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001228
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001229 if (blkid_probe_lookup_value(pr, "TYPE", &type, NULL) < 0) {
1230 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00001231 LOGINFO("can't find filesystem on device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001232 return;
1233 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001234
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001235 Current_File_System = type;
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001236 blkid_free_probe(pr);
Dees_Troy51a0e822012-09-05 15:24:24 -04001237}
1238
Gary Peck43acadf2012-11-21 21:19:01 -08001239bool TWPartition::Wipe_EXT23(string File_System) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001240 if (!UnMount(true))
1241 return false;
1242
Dees_Troy43d8b002012-09-17 16:00:01 -04001243 if (TWFunc::Path_Exists("/sbin/mke2fs")) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001244 string command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001245
Dees_Troy2673cec2013-04-02 20:22:16 +00001246 gui_print("Formatting %s using mke2fs...\n", Display_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001247 Find_Actual_Block_Device();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001248 command = "mke2fs -t " + File_System + " -m 0 " + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001249 LOGINFO("mke2fs command: %s\n", command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001250 if (TWFunc::Exec_Cmd(command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001251 Current_File_System = File_System;
Dees_Troye58d5262012-09-21 12:27:57 -04001252 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001253 gui_print("Done.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001254 return true;
1255 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001256 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001257 return false;
1258 }
1259 } else
1260 return Wipe_RMRF();
1261
1262 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001263}
1264
1265bool TWPartition::Wipe_EXT4() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001266 if (!UnMount(true))
1267 return false;
1268
Dees_Troyb3265ab2013-08-30 02:59:14 +00001269#if defined(HAVE_SELINUX) && defined(USE_EXT4)
Dees_Troya95f55c2013-08-17 13:14:43 +00001270 gui_print("Formatting %s using make_ext4fs function.\n", Display_Name.c_str());
Dees_Troya95f55c2013-08-17 13:14:43 +00001271 if (make_ext4fs(Actual_Block_Device.c_str(), Length, Mount_Point.c_str(), selinux_handle) != 0) {
Dees_Troya95f55c2013-08-17 13:14:43 +00001272 LOGERR("Unable to wipe '%s' using function call.\n", Mount_Point.c_str());
1273 return false;
1274 } else {
1275 return true;
1276 }
1277#else
Dees_Troy43d8b002012-09-17 16:00:01 -04001278 if (TWFunc::Path_Exists("/sbin/make_ext4fs")) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001279 string Command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001280
Dees_Troy2673cec2013-04-02 20:22:16 +00001281 gui_print("Formatting %s using make_ext4fs...\n", Display_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001282 Find_Actual_Block_Device();
1283 Command = "make_ext4fs";
1284 if (!Is_Decrypted && Length != 0) {
1285 // Only use length if we're not decrypted
1286 char len[32];
1287 sprintf(len, "%i", Length);
1288 Command += " -l ";
1289 Command += len;
1290 }
Dees_Troy5295d582013-09-06 15:51:08 +00001291 if (TWFunc::Path_Exists("/file_contexts")) {
1292 Command += " -S /file_contexts";
1293 }
1294 Command += " -a " + Mount_Point + " " + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001295 LOGINFO("make_ext4fs command: %s\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001296 if (TWFunc::Exec_Cmd(Command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001297 Current_File_System = "ext4";
Dees_Troye58d5262012-09-21 12:27:57 -04001298 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001299 gui_print("Done.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001300 return true;
1301 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001302 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001303 return false;
1304 }
1305 } else
Gary Peck43acadf2012-11-21 21:19:01 -08001306 return Wipe_EXT23("ext4");
Dees_Troya95f55c2013-08-17 13:14:43 +00001307#endif
Dees_Troy38bd7602012-09-14 13:33:53 -04001308 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001309}
1310
1311bool TWPartition::Wipe_FAT() {
Vojtech Bocek05534202013-09-11 08:11:56 +02001312 string command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001313
Dees_Troy43d8b002012-09-17 16:00:01 -04001314 if (TWFunc::Path_Exists("/sbin/mkdosfs")) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001315 if (!UnMount(true))
1316 return false;
1317
Dees_Troy2673cec2013-04-02 20:22:16 +00001318 gui_print("Formatting %s using mkdosfs...\n", Display_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001319 Find_Actual_Block_Device();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001320 command = "mkdosfs " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02001321 if (TWFunc::Exec_Cmd(command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001322 Current_File_System = "vfat";
Dees_Troye58d5262012-09-21 12:27:57 -04001323 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001324 gui_print("Done.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001325 return true;
1326 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001327 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001328 return false;
1329 }
1330 return true;
1331 }
1332 else
1333 return Wipe_RMRF();
1334
1335 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001336}
1337
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001338bool TWPartition::Wipe_EXFAT() {
Vojtech Bocek05534202013-09-11 08:11:56 +02001339 string command;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001340
1341 if (TWFunc::Path_Exists("/sbin/mkexfatfs")) {
1342 if (!UnMount(true))
1343 return false;
1344
Dees_Troy2673cec2013-04-02 20:22:16 +00001345 gui_print("Formatting %s using mkexfatfs...\n", Display_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001346 Find_Actual_Block_Device();
1347 command = "mkexfatfs " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02001348 if (TWFunc::Exec_Cmd(command) == 0) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001349 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001350 gui_print("Done.\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001351 return true;
1352 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001353 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001354 return false;
1355 }
1356 return true;
1357 }
1358 return false;
1359}
1360
Dees_Troy38bd7602012-09-14 13:33:53 -04001361bool TWPartition::Wipe_MTD() {
1362 if (!UnMount(true))
1363 return false;
1364
Dees_Troy2673cec2013-04-02 20:22:16 +00001365 gui_print("MTD Formatting \"%s\"\n", MTD_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001366
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001367 mtd_scan_partitions();
1368 const MtdPartition* mtd = mtd_find_partition_by_name(MTD_Name.c_str());
1369 if (mtd == NULL) {
1370 LOGERR("No mtd partition named '%s'", MTD_Name.c_str());
1371 return false;
1372 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001373
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001374 MtdWriteContext* ctx = mtd_write_partition(mtd);
1375 if (ctx == NULL) {
1376 LOGERR("Can't write '%s', failed to format.", MTD_Name.c_str());
1377 return false;
1378 }
1379 if (mtd_erase_blocks(ctx, -1) == -1) {
1380 mtd_write_close(ctx);
1381 LOGERR("Failed to format '%s'", MTD_Name.c_str());
1382 return false;
1383 }
1384 if (mtd_write_close(ctx) != 0) {
1385 LOGERR("Failed to close '%s'", MTD_Name.c_str());
1386 return false;
1387 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001388 Current_File_System = "yaffs2";
Dees_Troye58d5262012-09-21 12:27:57 -04001389 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001390 gui_print("Done.\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001391 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001392}
1393
1394bool TWPartition::Wipe_RMRF() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001395 if (!Mount(true))
1396 return false;
1397
Dees_Troy2673cec2013-04-02 20:22:16 +00001398 gui_print("Removing all files under '%s'\n", Mount_Point.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001399 TWFunc::removeDir(Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04001400 Recreate_AndSec_Folder();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001401 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001402}
1403
Dees_Troye5017042013-08-29 16:38:55 +00001404bool TWPartition::Wipe_F2FS() {
Vojtech Bocek05534202013-09-11 08:11:56 +02001405 string command;
Dees_Troye5017042013-08-29 16:38:55 +00001406
1407 if (TWFunc::Path_Exists("/sbin/mkfs.f2fs")) {
1408 if (!UnMount(true))
1409 return false;
1410
1411 gui_print("Formatting %s using mkfs.f2fs...\n", Display_Name.c_str());
1412 Find_Actual_Block_Device();
1413 command = "mkfs.f2fs " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02001414 if (TWFunc::Exec_Cmd(command) == 0) {
Dees_Troye5017042013-08-29 16:38:55 +00001415 Recreate_AndSec_Folder();
1416 gui_print("Done.\n");
1417 return true;
1418 } else {
1419 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
1420 return false;
1421 }
1422 return true;
1423 } else {
1424 gui_print("mkfs.f2fs binary not found, using rm -rf to wipe.\n");
1425 return Wipe_RMRF();
1426 }
1427 return false;
1428}
1429
Dees_Troy51a0e822012-09-05 15:24:24 -04001430bool TWPartition::Wipe_Data_Without_Wiping_Media() {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001431 string dir;
Dees_Troy38bd7602012-09-14 13:33:53 -04001432
1433 // This handles wiping data on devices with "sdcard" in /data/media
1434 if (!Mount(true))
1435 return false;
1436
Dees_Troy2673cec2013-04-02 20:22:16 +00001437 gui_print("Wiping data without wiping /data/media ...\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001438
1439 DIR* d;
1440 d = opendir("/data");
Dees_Troy16b74352012-11-14 22:27:31 +00001441 if (d != NULL) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001442 struct dirent* de;
1443 while ((de = readdir(d)) != NULL) {
Dees_Troy16b74352012-11-14 22:27:31 +00001444 if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) continue;
1445 // The media folder is the "internal sdcard"
1446 // The .layout_version file is responsible for determining whether 4.2 decides up upgrade
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001447 // the media folder for multi-user.
Dees_Troy16b74352012-11-14 22:27:31 +00001448 if (strcmp(de->d_name, "media") == 0 || strcmp(de->d_name, ".layout_version") == 0) continue;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001449
1450 dir = "/data/";
1451 dir.append(de->d_name);
Dees_Troyce675462013-01-09 19:48:21 +00001452 if (de->d_type == DT_DIR) {
1453 TWFunc::removeDir(dir, false);
bigbiff bigbiff98f1f902013-01-19 18:46:13 -05001454 } 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 +00001455 if (!unlink(dir.c_str()))
Dees_Troy2673cec2013-04-02 20:22:16 +00001456 LOGINFO("Unable to unlink '%s'\n", dir.c_str());
Dees_Troyce675462013-01-09 19:48:21 +00001457 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001458 }
1459 closedir(d);
Dees_Troy2673cec2013-04-02 20:22:16 +00001460 gui_print("Done.\n");
Dees_Troy16b74352012-11-14 22:27:31 +00001461 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -04001462 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001463 gui_print("Dirent failed to open /data, error!\n");
Dees_Troy16b74352012-11-14 22:27:31 +00001464 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001465}
1466
1467bool TWPartition::Backup_Tar(string backup_folder) {
Dees_Troy4a2a1262012-09-18 09:33:47 -04001468 char back_name[255], split_index[5];
1469 string Full_FileName, Split_FileName, Tar_Args, Command;
Dees_Troy83bd4832013-05-04 12:39:56 +00001470 int use_compression, use_encryption = 0, index, backup_count;
Dees_Troy4a2a1262012-09-18 09:33:47 -04001471 struct stat st;
Dees_Troy7c2dec82012-09-26 09:49:14 -04001472 unsigned long long total_bsize = 0, file_size;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001473 twrpTar tar;
1474 vector <string> files;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001475
Dees_Troy43d8b002012-09-17 16:00:01 -04001476 if (!Mount(true))
1477 return false;
1478
Dees_Troya13d74f2013-03-24 08:54:55 -05001479 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Backup_Display_Name, "Backing Up");
Dees_Troy2673cec2013-04-02 20:22:16 +00001480 gui_print("Backing up %s...\n", Backup_Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001481
1482 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
Dees_Troy83bd4832013-05-04 12:39:56 +00001483 tar.use_compression = use_compression;
bigbiff bigbiff86e77bc2013-08-26 21:36:23 -04001484 //exclude Google Music Cache
1485 tar.setexcl("/data/data/com.google.android.music/files");
Dees_Troy83bd4832013-05-04 12:39:56 +00001486#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
1487 DataManager::GetValue("tw_encrypt_backup", use_encryption);
1488 if (use_encryption && Can_Encrypt_Backup) {
1489 tar.use_encryption = use_encryption;
1490 if (Use_Userdata_Encryption)
1491 tar.userdata_encryption = use_encryption;
1492 } else {
1493 use_encryption = false;
1494 }
1495#endif
Dees_Troy43d8b002012-09-17 16:00:01 -04001496
1497 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1498 Backup_FileName = back_name;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001499 Full_FileName = backup_folder + "/" + Backup_FileName;
Dees_Troy83bd4832013-05-04 12:39:56 +00001500 tar.has_data_media = Has_Data_Media;
1501 if (!use_encryption && Backup_Size > MAX_ARCHIVE_SIZE) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001502 // This backup needs to be split into multiple archives
Dees_Troy2673cec2013-04-02 20:22:16 +00001503 gui_print("Breaking backup file into multiple archives...\n");
Dees_Troye58d5262012-09-21 12:27:57 -04001504 sprintf(back_name, "%s", Backup_Path.c_str());
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -05001505 tar.setdir(back_name);
1506 tar.setfn(Full_FileName);
bigbiff bigbiffe6594ab2013-02-17 20:18:31 -05001507 backup_count = tar.splitArchiveFork();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001508 if (backup_count == -1) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001509 LOGERR("Error tarring split files!\n");
Dees_Troy4a2a1262012-09-18 09:33:47 -04001510 return false;
1511 }
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001512 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001513 } else {
Dees_Troy4a2a1262012-09-18 09:33:47 -04001514 Full_FileName = backup_folder + "/" + Backup_FileName;
Dees_Troy83bd4832013-05-04 12:39:56 +00001515 tar.setdir(Backup_Path);
1516 tar.setfn(Full_FileName);
1517 if (tar.createTarFork() != 0)
1518 return false;
1519 if (use_compression && !use_encryption) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001520 string gzname = Full_FileName + ".gz";
1521 rename(gzname.c_str(), Full_FileName.c_str());
1522 }
Dees_Troy83bd4832013-05-04 12:39:56 +00001523 if (use_encryption)
1524 Full_FileName += "000";
Dees_Troy7c2dec82012-09-26 09:49:14 -04001525 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001526 LOGERR("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001527 return false;
1528 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001529 }
1530 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001531}
1532
1533bool TWPartition::Backup_DD(string backup_folder) {
igoriok87e3d932013-01-31 21:03:53 +02001534 char back_name[255], backup_size[32];
Vojtech Bocek05534202013-09-11 08:11:56 +02001535 string Full_FileName, Command, DD_BS;
Dees_Troy43d8b002012-09-17 16:00:01 -04001536 int use_compression;
1537
igoriok87e3d932013-01-31 21:03:53 +02001538 sprintf(backup_size, "%llu", Backup_Size);
1539 DD_BS = backup_size;
1540
Dees_Troyb46a6842012-09-25 11:06:46 -04001541 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
Dees_Troy2673cec2013-04-02 20:22:16 +00001542 gui_print("Backing up %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001543
1544 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1545 Backup_FileName = back_name;
1546
1547 Full_FileName = backup_folder + "/" + Backup_FileName;
1548
igoriok87e3d932013-01-31 21:03:53 +02001549 Command = "dd if=" + Actual_Block_Device + " of='" + Full_FileName + "'" + " bs=" + DD_BS + "c count=1";
Dees_Troy2673cec2013-04-02 20:22:16 +00001550 LOGINFO("Backup command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001551 TWFunc::Exec_Cmd(Command);
Dees_Troyc154ac22012-10-12 15:36:47 -04001552 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001553 LOGERR("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001554 return false;
1555 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001556 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001557}
1558
1559bool TWPartition::Backup_Dump_Image(string backup_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001560 char back_name[255];
Vojtech Bocek05534202013-09-11 08:11:56 +02001561 string Full_FileName, Command;
Dees_Troy43d8b002012-09-17 16:00:01 -04001562 int use_compression;
1563
Dees_Troyb46a6842012-09-25 11:06:46 -04001564 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
Dees_Troy2673cec2013-04-02 20:22:16 +00001565 gui_print("Backing up %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001566
1567 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1568 Backup_FileName = back_name;
1569
1570 Full_FileName = backup_folder + "/" + Backup_FileName;
1571
1572 Command = "dump_image " + MTD_Name + " '" + Full_FileName + "'";
Dees_Troy2673cec2013-04-02 20:22:16 +00001573 LOGINFO("Backup command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001574 TWFunc::Exec_Cmd(Command);
Dees_Troy7c2dec82012-09-26 09:49:14 -04001575 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
1576 // 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 +00001577 LOGERR("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001578 return false;
1579 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001580 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001581}
1582
Gary Peck43acadf2012-11-21 21:19:01 -08001583bool TWPartition::Restore_Tar(string restore_folder, string Restore_File_System) {
1584 string Full_FileName, Command;
Dees_Troy4a2a1262012-09-18 09:33:47 -04001585 int index = 0;
1586 char split_index[5];
Dees_Troy43d8b002012-09-17 16:00:01 -04001587
Dees_Troye58d5262012-09-21 12:27:57 -04001588 if (Has_Android_Secure) {
Dees_Troye58d5262012-09-21 12:27:57 -04001589 if (!Wipe_AndSec())
1590 return false;
Gary Peck43acadf2012-11-21 21:19:01 -08001591 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001592 gui_print("Wiping %s...\n", Display_Name.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001593 if (!Wipe(Restore_File_System))
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001594 return false;
Dees_Troye58d5262012-09-21 12:27:57 -04001595 }
Dees_Troya13d74f2013-03-24 08:54:55 -05001596 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Backup_Display_Name, "Restoring");
Dees_Troy2673cec2013-04-02 20:22:16 +00001597 gui_print("Restoring %s...\n", Backup_Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001598
1599 if (!Mount(true))
1600 return false;
1601
Dees_Troy4a2a1262012-09-18 09:33:47 -04001602 Full_FileName = restore_folder + "/" + Backup_FileName;
Dees_Troy83bd4832013-05-04 12:39:56 +00001603 /*if (!TWFunc::Path_Exists(Full_FileName)) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001604 if (!TWFunc::Path_Exists(Full_FileName)) {
1605 // Backup is multiple archives
Dees_Troy2673cec2013-04-02 20:22:16 +00001606 LOGINFO("Backup is multiple archives.\n");
Dees_Troy4a2a1262012-09-18 09:33:47 -04001607 sprintf(split_index, "%03i", index);
1608 Full_FileName = restore_folder + "/" + Backup_FileName + split_index;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001609 while (TWFunc::Path_Exists(Full_FileName)) {
1610 index++;
Dees_Troy2673cec2013-04-02 20:22:16 +00001611 gui_print("Restoring archive %i...\n", index);
1612 LOGINFO("Restoring '%s'...\n", Full_FileName.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001613 twrpTar tar;
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -05001614 tar.setdir("/");
1615 tar.setfn(Full_FileName);
bigbiff bigbiffe6594ab2013-02-17 20:18:31 -05001616 if (tar.extractTarFork() != 0)
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001617 return false;
1618 sprintf(split_index, "%03i", index);
1619 Full_FileName = restore_folder + "/" + Backup_FileName + split_index;
1620 }
1621 if (index == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001622 LOGERR("Error locating restore file: '%s'\n", Full_FileName.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001623 return false;
1624 }
Dees_Troy4a2a1262012-09-18 09:33:47 -04001625 }
Dees_Troy83bd4832013-05-04 12:39:56 +00001626 } else {*/
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001627 twrpTar tar;
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -05001628 tar.setdir(Backup_Path);
1629 tar.setfn(Full_FileName);
Dees_Troy83bd4832013-05-04 12:39:56 +00001630 tar.backup_name = Backup_Name;
bigbiff bigbiffe6594ab2013-02-17 20:18:31 -05001631 if (tar.extractTarFork() != 0)
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001632 return false;
Dees_Troy83bd4832013-05-04 12:39:56 +00001633 //}
Dees_Troy43d8b002012-09-17 16:00:01 -04001634 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001635}
1636
1637bool TWPartition::Restore_DD(string restore_folder) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001638 string Full_FileName, Command;
Dees_Troy43d8b002012-09-17 16:00:01 -04001639
Dees_Troyda8b55a2012-12-12 19:18:30 +00001640 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
Dees_Troy43d8b002012-09-17 16:00:01 -04001641 Full_FileName = restore_folder + "/" + Backup_FileName;
Gary Peck15e623d2012-11-21 21:07:58 -08001642
1643 if (!Find_Partition_Size()) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001644 LOGERR("Unable to find partition size for '%s'\n", Mount_Point.c_str());
Gary Peck15e623d2012-11-21 21:07:58 -08001645 return false;
1646 }
1647 unsigned long long backup_size = TWFunc::Get_File_Size(Full_FileName);
1648 if (backup_size > Size) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001649 LOGERR("Size (%iMB) of backup '%s' is larger than target device '%s' (%iMB)\n",
Gary Peck15e623d2012-11-21 21:07:58 -08001650 (int)(backup_size / 1048576LLU), Full_FileName.c_str(),
1651 Actual_Block_Device.c_str(), (int)(Size / 1048576LLU));
1652 return false;
1653 }
1654
Dees_Troy2673cec2013-04-02 20:22:16 +00001655 gui_print("Restoring %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001656 Command = "dd bs=4096 if='" + Full_FileName + "' of=" + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001657 LOGINFO("Restore command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001658 TWFunc::Exec_Cmd(Command);
Dees_Troy43d8b002012-09-17 16:00:01 -04001659 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001660}
1661
1662bool TWPartition::Restore_Flash_Image(string restore_folder) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001663 string Full_FileName, Command;
Dees_Troy43d8b002012-09-17 16:00:01 -04001664
Dees_Troy2673cec2013-04-02 20:22:16 +00001665 gui_print("Restoring %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001666 Full_FileName = restore_folder + "/" + Backup_FileName;
1667 // Sometimes flash image doesn't like to flash due to the first 2KB matching, so we erase first to ensure that it flashes
1668 Command = "erase_image " + MTD_Name;
Dees_Troy2673cec2013-04-02 20:22:16 +00001669 LOGINFO("Erase command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001670 TWFunc::Exec_Cmd(Command);
Dees_Troy43d8b002012-09-17 16:00:01 -04001671 Command = "flash_image " + MTD_Name + " '" + Full_FileName + "'";
Dees_Troy2673cec2013-04-02 20:22:16 +00001672 LOGINFO("Restore command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001673 TWFunc::Exec_Cmd(Command);
Dees_Troy43d8b002012-09-17 16:00:01 -04001674 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001675}
Dees_Troy5bf43922012-09-07 16:07:55 -04001676
1677bool TWPartition::Update_Size(bool Display_Error) {
Dees_Troy0550cfb2012-10-13 11:56:13 -04001678 bool ret = false, Was_Already_Mounted = false;
Dees_Troy51127312012-09-08 13:08:49 -04001679
Dees_Troyab10ee22012-09-21 14:27:30 -04001680 if (!Can_Be_Mounted && !Is_Encrypted)
Dees_Troy5bf43922012-09-07 16:07:55 -04001681 return false;
1682
Dees_Troy0550cfb2012-10-13 11:56:13 -04001683 Was_Already_Mounted = Is_Mounted();
Dees_Troy38bd7602012-09-14 13:33:53 -04001684 if (Removable || Is_Encrypted) {
1685 if (!Mount(false))
1686 return true;
1687 } else if (!Mount(Display_Error))
Dees_Troy5bf43922012-09-07 16:07:55 -04001688 return false;
Dees_Troy51127312012-09-08 13:08:49 -04001689
1690 ret = Get_Size_Via_statfs(Display_Error);
Dees_Troy0550cfb2012-10-13 11:56:13 -04001691 if (!ret || Size == 0) {
1692 if (!Get_Size_Via_df(Display_Error)) {
1693 if (!Was_Already_Mounted)
1694 UnMount(false);
Dees_Troy51127312012-09-08 13:08:49 -04001695 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04001696 }
1697 }
Dees_Troy51127312012-09-08 13:08:49 -04001698
Dees_Troy5bf43922012-09-07 16:07:55 -04001699 if (Has_Data_Media) {
1700 if (Mount(Display_Error)) {
Dees_Troy51127312012-09-08 13:08:49 -04001701 unsigned long long data_media_used, actual_data;
Dees_Troy43d8b002012-09-17 16:00:01 -04001702 Used = TWFunc::Get_Folder_Size("/data", Display_Error);
1703 data_media_used = TWFunc::Get_Folder_Size("/data/media", Display_Error);
Dees_Troy51127312012-09-08 13:08:49 -04001704 actual_data = Used - data_media_used;
Dees_Troy5bf43922012-09-07 16:07:55 -04001705 Backup_Size = actual_data;
Dees_Troy51127312012-09-08 13:08:49 -04001706 int bak = (int)(Backup_Size / 1048576LLU);
1707 int total = (int)(Size / 1048576LLU);
1708 int us = (int)(Used / 1048576LLU);
1709 int fre = (int)(Free / 1048576LLU);
1710 int datmed = (int)(data_media_used / 1048576LLU);
Dees_Troy2673cec2013-04-02 20:22:16 +00001711 LOGINFO("Data backup size is %iMB, size: %iMB, used: %iMB, free: %iMB, in data/media: %iMB.\n", bak, total, us, fre, datmed);
Dees_Troy0550cfb2012-10-13 11:56:13 -04001712 } else {
1713 if (!Was_Already_Mounted)
1714 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04001715 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04001716 }
Dees_Troye58d5262012-09-21 12:27:57 -04001717 } else if (Has_Android_Secure) {
1718 if (Mount(Display_Error))
1719 Backup_Size = TWFunc::Get_Folder_Size(Backup_Path, Display_Error);
Dees_Troy0550cfb2012-10-13 11:56:13 -04001720 else {
1721 if (!Was_Already_Mounted)
1722 UnMount(false);
Dees_Troye58d5262012-09-21 12:27:57 -04001723 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04001724 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001725 }
Dees_Troy0550cfb2012-10-13 11:56:13 -04001726 if (!Was_Already_Mounted)
1727 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04001728 return true;
Dees_Troy51127312012-09-08 13:08:49 -04001729}
Dees_Troy38bd7602012-09-14 13:33:53 -04001730
1731void TWPartition::Find_Actual_Block_Device(void) {
1732 if (Is_Decrypted) {
1733 Actual_Block_Device = Decrypted_Block_Device;
Dees_Troy43d8b002012-09-17 16:00:01 -04001734 if (TWFunc::Path_Exists(Primary_Block_Device))
Dees_Troy38bd7602012-09-14 13:33:53 -04001735 Is_Present = true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001736 } else if (TWFunc::Path_Exists(Primary_Block_Device)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001737 Is_Present = true;
1738 Actual_Block_Device = Primary_Block_Device;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001739 return;
1740 }
1741 if (Is_Decrypted) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001742 } else if (!Alternate_Block_Device.empty() && TWFunc::Path_Exists(Alternate_Block_Device)) {
Dees_Troy3f04d032012-10-07 18:20:09 -04001743 Actual_Block_Device = Alternate_Block_Device;
Dees_Troy38bd7602012-09-14 13:33:53 -04001744 Is_Present = true;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001745 } else {
Dees_Troy38bd7602012-09-14 13:33:53 -04001746 Is_Present = false;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001747 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001748}
1749
1750void TWPartition::Recreate_Media_Folder(void) {
1751 string Command;
1752
1753 if (!Mount(true)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001754 LOGERR("Unable to recreate /data/media folder.\n");
Dees_Troyb46a6842012-09-25 11:06:46 -04001755 } else if (!TWFunc::Path_Exists("/data/media")) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001756 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Dees_Troy2673cec2013-04-02 20:22:16 +00001757 LOGINFO("Recreating /data/media folder.\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001758 mkdir("/data/media", S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
1759 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Dees_Troy38bd7602012-09-14 13:33:53 -04001760 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001761}
Dees_Troye58d5262012-09-21 12:27:57 -04001762
1763void TWPartition::Recreate_AndSec_Folder(void) {
Dees_Troye58d5262012-09-21 12:27:57 -04001764 if (!Has_Android_Secure)
1765 return;
Dees_Troy2673cec2013-04-02 20:22:16 +00001766 LOGINFO("Creating %s: %s\n", Backup_Display_Name.c_str(), Symlink_Path.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -04001767 if (!Mount(true)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001768 LOGERR("Unable to recreate %s folder.\n", Backup_Name.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -04001769 } else if (!TWFunc::Path_Exists(Symlink_Path)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001770 LOGINFO("Recreating %s folder.\n", Backup_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001771 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
1772 mkdir(Symlink_Path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
1773 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04001774 }
1775}