blob: e6c622177710ed4d8df62a252e97d664e481a2c8 [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;
Dees_Troy51a0e822012-09-05 15:24:24 -040065 Wipe_During_Factory_Reset = false;
66 Wipe_Available_in_GUI = false;
67 Is_SubPartition = false;
Dees_Troy2691f9d2012-09-24 11:15:49 -040068 Has_SubPartition = false;
Dees_Troy51a0e822012-09-05 15:24:24 -040069 SubPartition_Of = "";
70 Symlink_Path = "";
71 Symlink_Mount_Point = "";
72 Mount_Point = "";
Dees_Troye58d5262012-09-21 12:27:57 -040073 Backup_Path = "";
Dees_Troy38bd7602012-09-14 13:33:53 -040074 Actual_Block_Device = "";
75 Primary_Block_Device = "";
Dees_Troy51a0e822012-09-05 15:24:24 -040076 Alternate_Block_Device = "";
77 Removable = false;
78 Is_Present = false;
79 Length = 0;
80 Size = 0;
81 Used = 0;
82 Free = 0;
83 Backup_Size = 0;
84 Can_Be_Encrypted = false;
85 Is_Encrypted = false;
86 Is_Decrypted = false;
87 Decrypted_Block_Device = "";
88 Display_Name = "";
Dees_Troya13d74f2013-03-24 08:54:55 -050089 Backup_Display_Name = "";
90 Storage_Name = "";
Dees_Troy51a0e822012-09-05 15:24:24 -040091 Backup_Name = "";
Dees_Troy63c8df72012-09-10 14:02:05 -040092 Backup_FileName = "";
Dees_Troy38bd7602012-09-14 13:33:53 -040093 MTD_Name = "";
Dees_Troy51a0e822012-09-05 15:24:24 -040094 Backup_Method = NONE;
Dees_Troy83bd4832013-05-04 12:39:56 +000095 Can_Encrypt_Backup = false;
96 Use_Userdata_Encryption = false;
Dees_Troy51a0e822012-09-05 15:24:24 -040097 Has_Data_Media = false;
Dees_Troye58d5262012-09-21 12:27:57 -040098 Has_Android_Secure = false;
Dees_Troy51a0e822012-09-05 15:24:24 -040099 Is_Storage = false;
Dees_Troya13d74f2013-03-24 08:54:55 -0500100 Is_Settings_Storage = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400101 Storage_Path = "";
102 Current_File_System = "";
103 Fstab_File_System = "";
104 Format_Block_Size = 0;
Dees_Troy68cab492012-12-12 19:29:35 +0000105 Ignore_Blkid = false;
Dees_Troy16c2b312013-01-15 16:51:18 +0000106 Retain_Layout_Version = false;
Dees_Troy85f44ed2013-01-09 18:42:36 +0000107#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
108 EcryptFS_Password = "";
109#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400110}
111
112TWPartition::~TWPartition(void) {
113 // Do nothing
114}
115
Dees_Troy5bf43922012-09-07 16:07:55 -0400116bool TWPartition::Process_Fstab_Line(string Line, bool Display_Error) {
117 char full_line[MAX_FSTAB_LINE_LENGTH], item[MAX_FSTAB_LINE_LENGTH];
118 int line_len = Line.size(), index = 0, item_index = 0;
119 char* ptr;
Dees_Troy51127312012-09-08 13:08:49 -0400120 string Flags;
Dees_Troy5bf43922012-09-07 16:07:55 -0400121 strncpy(full_line, Line.c_str(), line_len);
Dees_Troya13d74f2013-03-24 08:54:55 -0500122 bool skip = false;
Dees_Troy5bf43922012-09-07 16:07:55 -0400123
Dees_Troy51127312012-09-08 13:08:49 -0400124 for (index = 0; index < line_len; index++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500125 if (full_line[index] == 34)
126 skip = !skip;
127 if (!skip && full_line[index] <= 32)
Dees_Troy5bf43922012-09-07 16:07:55 -0400128 full_line[index] = '\0';
Dees_Troy5bf43922012-09-07 16:07:55 -0400129 }
Dees_Troy7c2dec82012-09-26 09:49:14 -0400130 Mount_Point = full_line;
Dees_Troy2673cec2013-04-02 20:22:16 +0000131 LOGINFO("Processing '%s'\n", Mount_Point.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -0400132 Backup_Path = Mount_Point;
Dees_Troya13d74f2013-03-24 08:54:55 -0500133 Storage_Path = Mount_Point;
Dees_Troy70737fa2013-04-08 13:19:20 +0000134 Display_Name = full_line + 1;
135 Backup_Display_Name = Display_Name;
136 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400137 index = Mount_Point.size();
138 while (index < line_len) {
139 while (index < line_len && full_line[index] == '\0')
140 index++;
141 if (index >= line_len)
142 continue;
143 ptr = full_line + index;
144 if (item_index == 0) {
145 // File System
146 Fstab_File_System = ptr;
147 Current_File_System = ptr;
148 item_index++;
149 } else if (item_index == 1) {
150 // Primary Block Device
Dees_Troy38bd7602012-09-14 13:33:53 -0400151 if (Fstab_File_System == "mtd" || Fstab_File_System == "yaffs2") {
Dees_Troy094207a2012-09-26 12:00:39 -0400152 MTD_Name = ptr;
153 Find_MTD_Block_Device(MTD_Name);
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400154 } else if (Fstab_File_System == "bml") {
155 if (Mount_Point == "/boot")
156 MTD_Name = "boot";
157 else if (Mount_Point == "/recovery")
158 MTD_Name = "recovery";
159 Primary_Block_Device = ptr;
160 if (*ptr != '/')
Dees_Troy2673cec2013-04-02 20:22:16 +0000161 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 -0400162 } else if (*ptr != '/') {
Dees_Troy5bf43922012-09-07 16:07:55 -0400163 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000164 LOGERR("Invalid block device on '%s', '%s', %i\n", Line.c_str(), ptr, index);
Dees_Troy5bf43922012-09-07 16:07:55 -0400165 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000166 LOGINFO("Invalid block device on '%s', '%s', %i\n", Line.c_str(), ptr, index);
Dees_Troy5bf43922012-09-07 16:07:55 -0400167 return 0;
Dees_Troy38bd7602012-09-14 13:33:53 -0400168 } else {
169 Primary_Block_Device = ptr;
170 Find_Real_Block_Device(Primary_Block_Device, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400171 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400172 item_index++;
173 } else if (item_index > 1) {
174 if (*ptr == '/') {
175 // Alternate Block Device
176 Alternate_Block_Device = ptr;
177 Find_Real_Block_Device(Alternate_Block_Device, Display_Error);
178 } else if (strlen(ptr) > 7 && strncmp(ptr, "length=", 7) == 0) {
179 // Partition length
180 ptr += 7;
181 Length = atoi(ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400182 } else if (strlen(ptr) > 6 && strncmp(ptr, "flags=", 6) == 0) {
183 // Custom flags, save for later so that new values aren't overwritten by defaults
184 ptr += 6;
185 Flags = ptr;
Dees_Troy68cab492012-12-12 19:29:35 +0000186 Process_Flags(Flags, Display_Error);
Dees_Troy38bd7602012-09-14 13:33:53 -0400187 } else if (strlen(ptr) == 4 && (strncmp(ptr, "NULL", 4) == 0 || strncmp(ptr, "null", 4) == 0 || strncmp(ptr, "null", 4) == 0)) {
188 // Do nothing
Dees_Troy5bf43922012-09-07 16:07:55 -0400189 } else {
190 // Unhandled data
Dees_Troy2673cec2013-04-02 20:22:16 +0000191 LOGINFO("Unhandled fstab information: '%s', %i, line: '%s'\n", ptr, index, Line.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400192 }
193 }
194 while (index < line_len && full_line[index] != '\0')
195 index++;
196 }
197
198 if (!Is_File_System(Fstab_File_System) && !Is_Image(Fstab_File_System)) {
199 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000200 LOGERR("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400201 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000202 LOGINFO("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400203 return 0;
204 } else if (Is_File_System(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400205 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400206 Setup_File_System(Display_Error);
207 if (Mount_Point == "/system") {
208 Display_Name = "System";
Dees_Troya13d74f2013-03-24 08:54:55 -0500209 Backup_Display_Name = Display_Name;
210 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400211 Wipe_Available_in_GUI = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500212 Can_Be_Backed_Up = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400213 } else if (Mount_Point == "/data") {
214 Display_Name = "Data";
Dees_Troya13d74f2013-03-24 08:54:55 -0500215 Backup_Display_Name = Display_Name;
216 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400217 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400218 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500219 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000220 Can_Encrypt_Backup = true;
221 Use_Userdata_Encryption = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400222#ifdef RECOVERY_SDCARD_ON_DATA
Dees_Troya13d74f2013-03-24 08:54:55 -0500223 Storage_Name = "Internal Storage";
Dees_Troy5bf43922012-09-07 16:07:55 -0400224 Has_Data_Media = true;
Dees_Troy51127312012-09-08 13:08:49 -0400225 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500226 Is_Settings_Storage = true;
Dees_Troy51127312012-09-08 13:08:49 -0400227 Storage_Path = "/data/media";
Dees_Troy16b74352012-11-14 22:27:31 +0000228 Symlink_Path = Storage_Path;
Dees_Troy657c3092012-09-10 20:32:10 -0400229 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
230 Make_Dir("/emmc", Display_Error);
Dees_Troy657c3092012-09-10 20:32:10 -0400231 Symlink_Mount_Point = "/emmc";
232 } else {
233 Make_Dir("/sdcard", Display_Error);
Dees_Troy657c3092012-09-10 20:32:10 -0400234 Symlink_Mount_Point = "/sdcard";
235 }
Dees_Troy16b74352012-11-14 22:27:31 +0000236 if (Mount(false) && TWFunc::Path_Exists("/data/media/0")) {
237 Storage_Path = "/data/media/0";
238 Symlink_Path = Storage_Path;
239 DataManager::SetValue(TW_INTERNAL_PATH, "/data/media/0");
240 UnMount(true);
241 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400242#endif
243#ifdef TW_INCLUDE_CRYPTO
244 Can_Be_Encrypted = true;
Dees_Troy657c3092012-09-10 20:32:10 -0400245 char crypto_blkdev[255];
246 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
247 if (strcmp(crypto_blkdev, "error") != 0) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400248 DataManager::SetValue(TW_DATA_BLK_DEVICE, Primary_Block_Device);
Dees_Troy657c3092012-09-10 20:32:10 -0400249 DataManager::SetValue(TW_IS_DECRYPTED, 1);
250 Is_Encrypted = true;
251 Is_Decrypted = true;
252 Decrypted_Block_Device = crypto_blkdev;
Dees_Troy2673cec2013-04-02 20:22:16 +0000253 LOGINFO("Data already decrypted, new block device: '%s'\n", crypto_blkdev);
Dees_Troy657c3092012-09-10 20:32:10 -0400254 } else if (!Mount(false)) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400255 Is_Encrypted = true;
256 Is_Decrypted = false;
Gary Peck82599a82012-11-21 16:23:12 -0800257 Can_Be_Mounted = false;
258 Current_File_System = "emmc";
259 Setup_Image(Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400260 DataManager::SetValue(TW_IS_ENCRYPTED, 1);
261 DataManager::SetValue(TW_CRYPTO_PASSWORD, "");
262 DataManager::SetValue("tw_crypto_display", "");
Gary Peck82599a82012-11-21 16:23:12 -0800263 } else {
264 // Filesystem is not encrypted and the mount
265 // succeeded, so get it back to the original
266 // unmounted state
267 UnMount(false);
Dees_Troy51127312012-09-08 13:08:49 -0400268 }
Dees_Troy9b21af72012-10-01 15:51:46 -0400269 #ifdef RECOVERY_SDCARD_ON_DATA
270 if (!Is_Encrypted || (Is_Encrypted && Is_Decrypted))
271 Recreate_Media_Folder();
272 #endif
273#else
274 #ifdef RECOVERY_SDCARD_ON_DATA
275 Recreate_Media_Folder();
276 #endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400277#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400278 } else if (Mount_Point == "/cache") {
279 Display_Name = "Cache";
Dees_Troya13d74f2013-03-24 08:54:55 -0500280 Backup_Display_Name = Display_Name;
281 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400282 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400283 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500284 Can_Be_Backed_Up = true;
Dees_Troyce2fe772012-09-28 12:34:33 -0400285 if (Mount(false) && !TWFunc::Path_Exists("/cache/recovery/.")) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000286 LOGINFO("Recreating /cache/recovery folder.\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500287 if (mkdir("/cache/recovery", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0)
288 return -1;
Dees_Troyb46a6842012-09-25 11:06:46 -0400289 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400290 } else if (Mount_Point == "/datadata") {
Dees_Troy51127312012-09-08 13:08:49 -0400291 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400292 Display_Name = "DataData";
Dees_Troya13d74f2013-03-24 08:54:55 -0500293 Backup_Display_Name = Display_Name;
294 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400295 Is_SubPartition = true;
296 SubPartition_Of = "/data";
Dees_Troy5bf43922012-09-07 16:07:55 -0400297 DataManager::SetValue(TW_HAS_DATADATA, 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500298 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000299 Can_Encrypt_Backup = true;
300 Use_Userdata_Encryption = false; // This whole partition should be encrypted
Dees_Troy5bf43922012-09-07 16:07:55 -0400301 } else if (Mount_Point == "/sd-ext") {
Dees_Troy51127312012-09-08 13:08:49 -0400302 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400303 Display_Name = "SD-Ext";
Dees_Troya13d74f2013-03-24 08:54:55 -0500304 Backup_Display_Name = Display_Name;
305 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400306 Wipe_Available_in_GUI = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400307 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500308 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000309 Can_Encrypt_Backup = true;
310 Use_Userdata_Encryption = true;
Dees_Troy2c50e182012-09-26 20:05:28 -0400311 } else if (Mount_Point == "/boot") {
312 Display_Name = "Boot";
Dees_Troya13d74f2013-03-24 08:54:55 -0500313 Backup_Display_Name = Display_Name;
Dees_Troy2c50e182012-09-26 20:05:28 -0400314 DataManager::SetValue("tw_boot_is_mountable", 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500315 Can_Be_Backed_Up = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400316 }
317#ifdef TW_EXTERNAL_STORAGE_PATH
318 if (Mount_Point == EXPAND(TW_EXTERNAL_STORAGE_PATH)) {
319 Is_Storage = true;
320 Storage_Path = EXPAND(TW_EXTERNAL_STORAGE_PATH);
Dees_Troyc51f1f92012-09-20 15:32:13 -0400321 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500322 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400323#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000324 if (Mount_Point == "/sdcard" || Mount_Point == "/external_sd" || Mount_Point == "/external_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400325 Is_Storage = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400326 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500327 Wipe_Available_in_GUI = true;
Dees_Troye58d5262012-09-21 12:27:57 -0400328#ifndef RECOVERY_SDCARD_ON_DATA
329 Setup_AndSec();
Dees_Troy8e337f32012-10-13 22:07:49 -0400330 Mount_Storage_Retry();
Dees_Troye58d5262012-09-21 12:27:57 -0400331#endif
Dees_Troy8170a922012-09-18 15:40:25 -0400332#endif
Dees_Troyb05ddee2013-01-28 20:24:50 +0000333 }
Dees_Troy8170a922012-09-18 15:40:25 -0400334#ifdef TW_INTERNAL_STORAGE_PATH
335 if (Mount_Point == EXPAND(TW_INTERNAL_STORAGE_PATH)) {
336 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500337 Is_Settings_Storage = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400338 Storage_Path = EXPAND(TW_INTERNAL_STORAGE_PATH);
Dees_Troya13d74f2013-03-24 08:54:55 -0500339 Wipe_Available_in_GUI = true;
Dees_Troye58d5262012-09-21 12:27:57 -0400340#ifndef RECOVERY_SDCARD_ON_DATA
341 Setup_AndSec();
Dees_Troy8e337f32012-10-13 22:07:49 -0400342 Mount_Storage_Retry();
Dees_Troye58d5262012-09-21 12:27:57 -0400343#endif
Dees_Troy8170a922012-09-18 15:40:25 -0400344 }
345#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000346 if (Mount_Point == "/emmc" || Mount_Point == "/internal_sd" || Mount_Point == "/internal_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400347 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500348 Is_Settings_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500349 Wipe_Available_in_GUI = true;
Dees_Troye58d5262012-09-21 12:27:57 -0400350#ifndef RECOVERY_SDCARD_ON_DATA
351 Setup_AndSec();
Dees_Troy8e337f32012-10-13 22:07:49 -0400352 Mount_Storage_Retry();
Dees_Troye58d5262012-09-21 12:27:57 -0400353#endif
Dees_Troy8170a922012-09-18 15:40:25 -0400354 }
355#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400356 } else if (Is_Image(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400357 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400358 Setup_Image(Display_Error);
Dees_Troya13d74f2013-03-24 08:54:55 -0500359 if (Mount_Point == "/boot") {
360 Display_Name = "Boot";
361 Backup_Display_Name = Display_Name;
362 Can_Be_Backed_Up = true;
363 } else if (Mount_Point == "/recovery") {
364 Display_Name = "Recovery";
365 Backup_Display_Name = Display_Name;
366 Can_Be_Backed_Up = true;
367 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400368 }
369
Dees_Troy51127312012-09-08 13:08:49 -0400370 // Process any custom flags
371 if (Flags.size() > 0)
372 Process_Flags(Flags, Display_Error);
Dees_Troy51127312012-09-08 13:08:49 -0400373 return true;
374}
375
376bool TWPartition::Process_Flags(string Flags, bool Display_Error) {
377 char flags[MAX_FSTAB_LINE_LENGTH];
Dees_Troya13d74f2013-03-24 08:54:55 -0500378 int flags_len, index = 0, ptr_len;
Dees_Troy51127312012-09-08 13:08:49 -0400379 char* ptr;
Dees_Troya13d74f2013-03-24 08:54:55 -0500380 bool skip = false, has_display_name = false, has_storage_name = false, has_backup_name = false;
Dees_Troy51127312012-09-08 13:08:49 -0400381
382 strcpy(flags, Flags.c_str());
383 flags_len = Flags.size();
384 for (index = 0; index < flags_len; index++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500385 if (flags[index] == 34)
386 skip = !skip;
387 if (!skip && flags[index] == ';')
Dees_Troy51127312012-09-08 13:08:49 -0400388 flags[index] = '\0';
389 }
390
391 index = 0;
392 while (index < flags_len) {
393 while (index < flags_len && flags[index] == '\0')
394 index++;
395 if (index >= flags_len)
396 continue;
397 ptr = flags + index;
Dees_Troya13d74f2013-03-24 08:54:55 -0500398 ptr_len = strlen(ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400399 if (strcmp(ptr, "removable") == 0) {
400 Removable = true;
401 } else if (strcmp(ptr, "storage") == 0) {
402 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500403 } else if (strcmp(ptr, "settingsstorage") == 0) {
404 Is_Storage = true;
Dees_Troy63c8df72012-09-10 14:02:05 -0400405 } else if (strcmp(ptr, "canbewiped") == 0) {
406 Can_Be_Wiped = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500407 } else if (ptr_len > 7 && strncmp(ptr, "backup=", 7) == 0) {
408 ptr += 7;
409 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y')
410 Can_Be_Backed_Up = true;
411 else
412 Can_Be_Backed_Up = false;
Dees_Troy63c8df72012-09-10 14:02:05 -0400413 } else if (strcmp(ptr, "wipeingui") == 0) {
414 Can_Be_Wiped = true;
415 Wipe_Available_in_GUI = true;
416 } else if (strcmp(ptr, "wipeduringfactoryreset") == 0) {
417 Can_Be_Wiped = true;
418 Wipe_Available_in_GUI = true;
419 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500420 } else if (ptr_len > 15 && strncmp(ptr, "subpartitionof=", 15) == 0) {
Dees_Troy2c4c26f2013-01-28 15:26:43 +0000421 ptr += 15;
Dees_Troy51127312012-09-08 13:08:49 -0400422 Is_SubPartition = true;
423 SubPartition_Of = ptr;
Dees_Troy68cab492012-12-12 19:29:35 +0000424 } else if (strcmp(ptr, "ignoreblkid") == 0) {
425 Ignore_Blkid = true;
Dees_Troy16c2b312013-01-15 16:51:18 +0000426 } else if (strcmp(ptr, "retainlayoutversion") == 0) {
427 Retain_Layout_Version = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500428 } else if (ptr_len > 8 && strncmp(ptr, "symlink=", 8) == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400429 ptr += 8;
430 Symlink_Path = ptr;
Dees_Troya13d74f2013-03-24 08:54:55 -0500431 } else if (ptr_len > 8 && strncmp(ptr, "display=", 8) == 0) {
432 has_display_name = true;
Dees_Troy51127312012-09-08 13:08:49 -0400433 ptr += 8;
Dees_Troya13d74f2013-03-24 08:54:55 -0500434 if (*ptr == '\"') ptr++;
Dees_Troy51127312012-09-08 13:08:49 -0400435 Display_Name = ptr;
Dees_Troya13d74f2013-03-24 08:54:55 -0500436 if (Display_Name.substr(Display_Name.size() - 1, 1) == "\"") {
437 Display_Name.resize(Display_Name.size() - 1);
438 }
439 } else if (ptr_len > 11 && strncmp(ptr, "storagename=", 11) == 0) {
440 has_storage_name = true;
441 ptr += 11;
442 if (*ptr == '\"') ptr++;
443 Storage_Name = ptr;
444 if (Storage_Name.substr(Storage_Name.size() - 1, 1) == "\"") {
445 Storage_Name.resize(Storage_Name.size() - 1);
446 }
447 } else if (ptr_len > 11 && strncmp(ptr, "backupname=", 10) == 0) {
448 has_backup_name = true;
449 ptr += 10;
450 if (*ptr == '\"') ptr++;
451 Backup_Display_Name = ptr;
452 if (Backup_Display_Name.substr(Backup_Display_Name.size() - 1, 1) == "\"") {
453 Backup_Display_Name.resize(Backup_Display_Name.size() - 1);
454 }
455 } else if (ptr_len > 10 && strncmp(ptr, "blocksize=", 10) == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400456 ptr += 10;
457 Format_Block_Size = atoi(ptr);
Dees_Troya13d74f2013-03-24 08:54:55 -0500458 } else if (ptr_len > 7 && strncmp(ptr, "length=", 7) == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400459 ptr += 7;
460 Length = atoi(ptr);
Dees_Troy83bd4832013-05-04 12:39:56 +0000461 } else if (ptr_len > 17 && strncmp(ptr, "canencryptbackup=", 17) == 0) {
462 ptr += 17;
463 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y')
464 Can_Encrypt_Backup = true;
465 else
466 Can_Encrypt_Backup = false;
467 } else if (ptr_len > 21 && strncmp(ptr, "userdataencryptbackup=", 21) == 0) {
468 ptr += 21;
469 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y') {
470 Can_Encrypt_Backup = true;
471 Use_Userdata_Encryption = true;
472 } else {
473 Use_Userdata_Encryption = false;
474 }
Dees_Troy51127312012-09-08 13:08:49 -0400475 } else {
476 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000477 LOGERR("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400478 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000479 LOGINFO("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400480 }
481 while (index < flags_len && flags[index] != '\0')
482 index++;
483 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500484 if (has_display_name && !has_storage_name)
485 Storage_Name = Display_Name;
486 if (!has_display_name && has_storage_name)
487 Display_Name = Storage_Name;
Dees_Troy74fb2e92013-04-15 14:35:47 +0000488 if (has_display_name && !has_backup_name && Backup_Display_Name != "Android Secure")
Dees_Troya13d74f2013-03-24 08:54:55 -0500489 Backup_Display_Name = Display_Name;
490 if (!has_display_name && has_backup_name)
491 Display_Name = Backup_Display_Name;
Dees_Troy51127312012-09-08 13:08:49 -0400492 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400493}
494
Dees_Troy5bf43922012-09-07 16:07:55 -0400495bool TWPartition::Is_File_System(string File_System) {
496 if (File_System == "ext2" ||
Dees_Troy63c8df72012-09-10 14:02:05 -0400497 File_System == "ext3" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400498 File_System == "ext4" ||
499 File_System == "vfat" ||
500 File_System == "ntfs" ||
501 File_System == "yaffs2" ||
bigbiff bigbiff3e146522012-11-14 14:32:59 -0500502 File_System == "exfat" ||
Dees_Troye5017042013-08-29 16:38:55 +0000503 File_System == "f2fs" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400504 File_System == "auto")
505 return true;
506 else
507 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400508}
509
Dees_Troy5bf43922012-09-07 16:07:55 -0400510bool TWPartition::Is_Image(string File_System) {
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400511 if (File_System == "emmc" || File_System == "mtd" || File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -0400512 return true;
513 else
514 return false;
515}
516
Dees_Troy51127312012-09-08 13:08:49 -0400517bool TWPartition::Make_Dir(string Path, bool Display_Error) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400518 if (!TWFunc::Path_Exists(Path)) {
Dees_Troy51127312012-09-08 13:08:49 -0400519 if (mkdir(Path.c_str(), 0777) == -1) {
520 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000521 LOGERR("Can not create '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400522 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000523 LOGINFO("Can not create '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400524 return false;
525 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000526 LOGINFO("Created '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400527 return true;
528 }
529 }
530 return true;
531}
532
Dees_Troy5bf43922012-09-07 16:07:55 -0400533void TWPartition::Setup_File_System(bool Display_Error) {
534 struct statfs st;
535
536 Can_Be_Mounted = true;
537 Can_Be_Wiped = true;
538
Dees_Troy5bf43922012-09-07 16:07:55 -0400539 // Make the mount point folder if it doesn't exist
Dees_Troy51127312012-09-08 13:08:49 -0400540 Make_Dir(Mount_Point, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400541 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
542 Backup_Name = Display_Name;
543 Backup_Method = FILES;
544}
545
546void TWPartition::Setup_Image(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400547 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
548 Backup_Name = Display_Name;
Gary Peck82599a82012-11-21 16:23:12 -0800549 if (Current_File_System == "emmc")
Dees_Troy5bf43922012-09-07 16:07:55 -0400550 Backup_Method = DD;
Gary Peck82599a82012-11-21 16:23:12 -0800551 else if (Current_File_System == "mtd" || Current_File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -0400552 Backup_Method = FLASH_UTILS;
553 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000554 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 -0400555 if (Find_Partition_Size()) {
556 Used = Size;
557 Backup_Size = Size;
Dees_Troy51a0e822012-09-05 15:24:24 -0400558 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400559 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000560 LOGERR("Unable to find parition size for '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400561 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000562 LOGINFO("Unable to find parition size for '%s'\n", Mount_Point.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400563 }
564}
565
Dees_Troye58d5262012-09-21 12:27:57 -0400566void TWPartition::Setup_AndSec(void) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500567 Backup_Display_Name = "Android Secure";
Dees_Troye58d5262012-09-21 12:27:57 -0400568 Backup_Name = "and-sec";
Dees_Troya13d74f2013-03-24 08:54:55 -0500569 Can_Be_Backed_Up = true;
Dees_Troye58d5262012-09-21 12:27:57 -0400570 Has_Android_Secure = true;
571 Symlink_Path = Mount_Point + "/.android_secure";
572 Symlink_Mount_Point = "/and-sec";
573 Backup_Path = Symlink_Mount_Point;
574 Make_Dir("/and-sec", true);
575 Recreate_AndSec_Folder();
576}
577
Dees_Troy5bf43922012-09-07 16:07:55 -0400578void TWPartition::Find_Real_Block_Device(string& Block, bool Display_Error) {
579 char device[512], realDevice[512];
580
581 strcpy(device, Block.c_str());
582 memset(realDevice, 0, sizeof(realDevice));
583 while (readlink(device, realDevice, sizeof(realDevice)) > 0)
584 {
585 strcpy(device, realDevice);
586 memset(realDevice, 0, sizeof(realDevice));
587 }
588
589 if (device[0] != '/') {
590 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000591 LOGERR("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400592 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000593 LOGINFO("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400594 return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400595 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400596 Block = device;
597 return;
598 }
599}
600
Dees_Troy8e337f32012-10-13 22:07:49 -0400601void TWPartition::Mount_Storage_Retry(void) {
602 // On some devices, storage doesn't want to mount right away, retry and sleep
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500603 if (!Mount(true)) {
Dees_Troy8e337f32012-10-13 22:07:49 -0400604 int retry_count = 5;
605 while (retry_count > 0 && !Mount(false)) {
606 usleep(500000);
607 retry_count--;
608 }
609 Mount(true);
610 }
611}
612
Dees_Troy38bd7602012-09-14 13:33:53 -0400613bool TWPartition::Find_MTD_Block_Device(string MTD_Name) {
614 FILE *fp = NULL;
615 char line[255];
616
617 fp = fopen("/proc/mtd", "rt");
618 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000619 LOGERR("Device does not support /proc/mtd\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400620 return false;
621 }
622
623 while (fgets(line, sizeof(line), fp) != NULL)
624 {
625 char device[32], label[32];
626 unsigned long size = 0;
627 char* fstype = NULL;
628 int deviceId;
629
630 sscanf(line, "%s %lx %*s %*c%s", device, &size, label);
631
632 // Skip header and blank lines
633 if ((strcmp(device, "dev:") == 0) || (strlen(line) < 8))
634 continue;
635
636 // Strip off the trailing " from the label
637 label[strlen(label)-1] = '\0';
638
639 if (strcmp(label, MTD_Name.c_str()) == 0) {
640 // We found our device
641 // Strip off the trailing : from the device
642 device[strlen(device)-1] = '\0';
643 if (sscanf(device,"mtd%d", &deviceId) == 1) {
644 sprintf(device, "/dev/block/mtdblock%d", deviceId);
645 Primary_Block_Device = device;
Dees_Troy76543db2013-06-19 16:24:30 +0000646 fclose(fp);
647 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -0400648 }
649 }
650 }
651 fclose(fp);
652
653 return false;
654}
655
Dees_Troy51127312012-09-08 13:08:49 -0400656bool TWPartition::Get_Size_Via_statfs(bool Display_Error) {
657 struct statfs st;
658 string Local_Path = Mount_Point + "/.";
659
660 if (!Mount(Display_Error))
661 return false;
662
663 if (statfs(Local_Path.c_str(), &st) != 0) {
664 if (!Removable) {
665 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000666 LOGERR("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400667 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000668 LOGINFO("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400669 }
670 return false;
671 }
672 Size = (st.f_blocks * st.f_bsize);
673 Used = ((st.f_blocks - st.f_bfree) * st.f_bsize);
674 Free = (st.f_bfree * st.f_bsize);
675 Backup_Size = Used;
676 return true;
677}
678
679bool TWPartition::Get_Size_Via_df(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400680 FILE* fp;
681 char command[255], line[512];
682 int include_block = 1;
683 unsigned int min_len;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500684 string result;
Dees_Troy5bf43922012-09-07 16:07:55 -0400685
686 if (!Mount(Display_Error))
687 return false;
688
Dees_Troy38bd7602012-09-14 13:33:53 -0400689 min_len = Actual_Block_Device.size() + 2;
Dees_Troy51127312012-09-08 13:08:49 -0400690 sprintf(command, "df %s > /tmp/dfoutput.txt", Mount_Point.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500691 TWFunc::Exec_Cmd(command, result);
Dees_Troy51127312012-09-08 13:08:49 -0400692 fp = fopen("/tmp/dfoutput.txt", "rt");
693 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000694 LOGINFO("Unable to open /tmp/dfoutput.txt.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400695 return false;
Dees_Troy51127312012-09-08 13:08:49 -0400696 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400697
698 while (fgets(line, sizeof(line), fp) != NULL)
699 {
700 unsigned long blocks, used, available;
701 char device[64];
702 char tmpString[64];
703
704 if (strncmp(line, "Filesystem", 10) == 0)
705 continue;
706 if (strlen(line) < min_len) {
707 include_block = 0;
708 continue;
709 }
710 if (include_block) {
711 sscanf(line, "%s %lu %lu %lu", device, &blocks, &used, &available);
712 } else {
713 // The device block string is so long that the df information is on the next line
714 int space_count = 0;
Dees_Troye58d5262012-09-21 12:27:57 -0400715 sprintf(tmpString, "/dev/block/%s", Actual_Block_Device.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400716 while (tmpString[space_count] == 32)
717 space_count++;
718 sscanf(line + space_count, "%lu %lu %lu", &blocks, &used, &available);
719 }
720
721 // Adjust block size to byte size
722 Size = blocks * 1024ULL;
723 Used = used * 1024ULL;
724 Free = available * 1024ULL;
725 Backup_Size = Used;
726 }
727 fclose(fp);
728 return true;
729}
730
Dees_Troy5bf43922012-09-07 16:07:55 -0400731bool TWPartition::Find_Partition_Size(void) {
732 FILE* fp;
733 char line[512];
734 string tmpdevice;
735
igoriok87e3d932013-01-31 21:03:53 +0200736 fp = fopen("/proc/dumchar_info", "rt");
737 if (fp != NULL) {
738 while (fgets(line, sizeof(line), fp) != NULL)
739 {
740 char label[32], device[32];
741 unsigned long size = 0;
742
743 sscanf(line, "%s %lx %*lx %*lu %s", label, &size, device);
744
745 // Skip header, annotation and blank lines
746 if ((strncmp(device, "/dev/", 5) != 0) || (strlen(line) < 8))
747 continue;
748
749 tmpdevice = "/dev/";
750 tmpdevice += label;
751 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
752 Size = size;
753 fclose(fp);
754 return true;
755 }
756 }
757 }
758
Dees_Troy5bf43922012-09-07 16:07:55 -0400759 // In this case, we'll first get the partitions we care about (with labels)
760 fp = fopen("/proc/partitions", "rt");
761 if (fp == NULL)
762 return false;
763
764 while (fgets(line, sizeof(line), fp) != NULL)
765 {
766 unsigned long major, minor, blocks;
767 char device[512];
768 char tmpString[64];
769
Dees_Troy63c8df72012-09-10 14:02:05 -0400770 if (strlen(line) < 7 || line[0] == 'm') continue;
Dees_Troy5bf43922012-09-07 16:07:55 -0400771 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
772
773 tmpdevice = "/dev/block/";
774 tmpdevice += device;
Dees_Troy38bd7602012-09-14 13:33:53 -0400775 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400776 // Adjust block size to byte size
777 Size = blocks * 1024ULL;
778 fclose(fp);
779 return true;
780 }
781 }
782 fclose(fp);
783 return false;
784}
785
Dees_Troy5bf43922012-09-07 16:07:55 -0400786bool TWPartition::Is_Mounted(void) {
787 if (!Can_Be_Mounted)
788 return false;
789
790 struct stat st1, st2;
791 string test_path;
792
793 // Check to see if the mount point directory exists
794 test_path = Mount_Point + "/.";
795 if (stat(test_path.c_str(), &st1) != 0) return false;
796
797 // Check to see if the directory above the mount point exists
798 test_path = Mount_Point + "/../.";
799 if (stat(test_path.c_str(), &st2) != 0) return false;
800
801 // Compare the device IDs -- if they match then we're (probably) using tmpfs instead of an actual device
802 int ret = (st1.st_dev != st2.st_dev) ? true : false;
803
804 return ret;
805}
806
807bool TWPartition::Mount(bool Display_Error) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000808 int exfat_mounted = 0;
809
Dees_Troy5bf43922012-09-07 16:07:55 -0400810 if (Is_Mounted()) {
811 return true;
812 } else if (!Can_Be_Mounted) {
813 return false;
814 }
Dees_Troy38bd7602012-09-14 13:33:53 -0400815
816 Find_Actual_Block_Device();
817
818 // Check the current file system before mounting
819 Check_FS_Type();
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000820 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/exfat-fuse")) {
Dees_Troye34c1332013-02-06 19:13:00 +0000821 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 +0000822 LOGINFO("cmd: %s\n", cmd.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000823 string result;
824 if (TWFunc::Exec_Cmd(cmd, result) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000825 LOGINFO("exfat-fuse failed to mount with result '%s', trying vfat\n", result.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000826 Current_File_System = "vfat";
827 } else {
828#ifdef TW_NO_EXFAT_FUSE
829 UnMount(false);
830 // We'll let the kernel handle it but using exfat-fuse to detect if the file system is actually exfat
831 // Some kernels let us mount vfat as exfat which doesn't work out too well
832#else
833 exfat_mounted = 1;
834#endif
835 }
836 }
Dees_Troy22042032012-12-18 21:23:08 +0000837 if (Fstab_File_System == "yaffs2") {
838 // mount an MTD partition as a YAFFS2 filesystem.
Dees_Troy76543db2013-06-19 16:24:30 +0000839 const unsigned long flags = MS_NOATIME | MS_NODEV | MS_NODIRATIME;
840 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags, NULL) < 0) {
841 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags | MS_RDONLY, NULL) < 0) {
842 if (Display_Error)
843 LOGERR("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
844 else
845 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
846 return false;
847 } else {
848 LOGINFO("Mounted '%s' (MTD) as RO\n", Mount_Point.c_str());
849 return true;
850 }
851 } else {
852 struct stat st;
853 string test_path = Mount_Point;
854 if (stat(test_path.c_str(), &st) < 0) {
855 if (Display_Error)
856 LOGERR("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
857 else
858 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
859 return false;
860 }
861 mode_t new_mode = st.st_mode | S_IXUSR | S_IXGRP | S_IXOTH;
862 if (new_mode != st.st_mode) {
863 LOGINFO("Fixing execute permissions for %s\n", Mount_Point.c_str());
864 if (chmod(Mount_Point.c_str(), new_mode) < 0) {
865 if (Display_Error)
866 LOGERR("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
867 else
868 LOGINFO("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
869 return false;
870 }
871 }
Dees_Troy22042032012-12-18 21:23:08 +0000872 return true;
Dees_Troy76543db2013-06-19 16:24:30 +0000873 }
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000874 } else if (!exfat_mounted && mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Current_File_System.c_str(), 0, NULL) != 0) {
875#ifdef TW_NO_EXFAT_FUSE
876 if (Current_File_System == "exfat") {
Dees_Troy2673cec2013-04-02 20:22:16 +0000877 LOGINFO("Mounting exfat failed, trying vfat...\n");
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000878 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), "vfat", 0, NULL) != 0) {
Dees_Troy85f44ed2013-01-09 18:42:36 +0000879 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000880 LOGERR("Unable to mount '%s'\n", Mount_Point.c_str());
Dees_Troy85f44ed2013-01-09 18:42:36 +0000881 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000882 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
883 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 +0000884 return false;
Dees_Troy85f44ed2013-01-09 18:42:36 +0000885 }
Dees_Troyb05ddee2013-01-28 20:24:50 +0000886 } else {
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000887#endif
888 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000889 LOGERR("Unable to mount '%s'\n", Mount_Point.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000890 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000891 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
892 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 +0000893 return false;
894#ifdef TW_NO_EXFAT_FUSE
Dees_Troy85f44ed2013-01-09 18:42:36 +0000895 }
896#endif
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000897 }
898#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
899 string MetaEcfsFile = EXPAND(TW_EXTERNAL_STORAGE_PATH);
900 MetaEcfsFile += "/.MetaEcfsFile";
901 if (EcryptFS_Password.size() > 0 && PartitionManager.Mount_By_Path("/data", false) && TWFunc::Path_Exists(MetaEcfsFile)) {
902 if (mount_ecryptfs_drive(EcryptFS_Password.c_str(), Mount_Point.c_str(), Mount_Point.c_str(), 0) != 0) {
903 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000904 LOGERR("Unable to mount ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000905 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000906 LOGINFO("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("Successfully mounted ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000909 Is_Decrypted = true;
Dees_Troy51127312012-09-08 13:08:49 -0400910 }
Dees_Troy066eb302013-08-23 17:20:32 +0000911 } else if (Mount_Point == EXPAND(TW_EXTERNAL_STORAGE_PATH)) {
912 if (Is_Decrypted)
913 LOGINFO("Mounting external storage, '%s' is not encrypted\n", Mount_Point.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000914 Is_Decrypted = false;
915 }
916#endif
917 if (Removable)
918 Update_Size(Display_Error);
919
920 if (!Symlink_Mount_Point.empty()) {
921 string Command, Result;
922 Command = "mount '" + Symlink_Path + "' '" + Symlink_Mount_Point + "'";
923 TWFunc::Exec_Cmd(Command, Result);
Dees_Troy5bf43922012-09-07 16:07:55 -0400924 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400925 return true;
926}
927
928bool TWPartition::UnMount(bool Display_Error) {
929 if (Is_Mounted()) {
930 int never_unmount_system;
931
932 DataManager::GetValue(TW_DONT_UNMOUNT_SYSTEM, never_unmount_system);
933 if (never_unmount_system == 1 && Mount_Point == "/system")
934 return true; // Never unmount system if you're not supposed to unmount it
935
Dees_Troyc8bafa12013-01-10 15:43:00 +0000936#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
937 if (EcryptFS_Password.size() > 0) {
938 if (unmount_ecryptfs_drive(Mount_Point.c_str()) != 0) {
939 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000940 LOGERR("Unable to unmount ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troyc8bafa12013-01-10 15:43:00 +0000941 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000942 LOGINFO("Unable to unmount ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troyc8bafa12013-01-10 15:43:00 +0000943 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000944 LOGINFO("Successfully unmounted ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troyc8bafa12013-01-10 15:43:00 +0000945 }
946 }
947#endif
948
Dees_Troy38bd7602012-09-14 13:33:53 -0400949 if (!Symlink_Mount_Point.empty())
950 umount(Symlink_Mount_Point.c_str());
951
Dees_Troyb05ddee2013-01-28 20:24:50 +0000952 umount(Mount_Point.c_str());
953 if (Is_Mounted()) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400954 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000955 LOGERR("Unable to unmount '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400956 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000957 LOGINFO("Unable to unmount '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400958 return false;
959 } else
960 return true;
961 } else {
962 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400963 }
964}
965
Gary Peck43acadf2012-11-21 21:19:01 -0800966bool TWPartition::Wipe(string New_File_System) {
Dees_Troy16c2b312013-01-15 16:51:18 +0000967 bool wiped = false, update_crypt = false;
968 int check;
969 string Layout_Filename = Mount_Point + "/.layout_version";
970
Dees_Troy38bd7602012-09-14 13:33:53 -0400971 if (!Can_Be_Wiped) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000972 LOGERR("Partition '%s' cannot be wiped.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -0400973 return false;
974 }
975
Dees_Troyc51f1f92012-09-20 15:32:13 -0400976 if (Mount_Point == "/cache")
Dees_Troy2673cec2013-04-02 20:22:16 +0000977 Log_Offset = 0;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400978
Dees_Troyce675462013-01-09 19:48:21 +0000979#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
980 if (Mount_Point == "/data" && Mount(false)) {
981 if (TWFunc::Path_Exists("/data/system/edk_p_sd"))
982 TWFunc::copy_file("/data/system/edk_p_sd", "/tmp/edk_p_sd", 0600);
983 }
984#endif
985
Dees_Troy16c2b312013-01-15 16:51:18 +0000986 if (Retain_Layout_Version && Mount(false) && TWFunc::Path_Exists(Layout_Filename))
987 TWFunc::copy_file(Layout_Filename, "/.layout_version", 0600);
988 else
989 unlink("/.layout_version");
Dees_Troy38bd7602012-09-14 13:33:53 -0400990
Dees_Troy16c2b312013-01-15 16:51:18 +0000991 if (Has_Data_Media) {
992 wiped = Wipe_Data_Without_Wiping_Media();
993 } else {
Gary Pecke8bc5d72012-12-21 06:45:25 -0800994
Dees_Troy16c2b312013-01-15 16:51:18 +0000995 DataManager::GetValue(TW_RM_RF_VAR, check);
996
997 if (check)
998 wiped = Wipe_RMRF();
999 else if (New_File_System == "ext4")
1000 wiped = Wipe_EXT4();
1001 else if (New_File_System == "ext2" || New_File_System == "ext3")
1002 wiped = Wipe_EXT23(New_File_System);
1003 else if (New_File_System == "vfat")
1004 wiped = Wipe_FAT();
1005 else if (New_File_System == "exfat")
1006 wiped = Wipe_EXFAT();
1007 else if (New_File_System == "yaffs2")
1008 wiped = Wipe_MTD();
Dees_Troye5017042013-08-29 16:38:55 +00001009 else if (New_File_System == "f2fs")
1010 wiped = Wipe_F2FS();
Dees_Troy16c2b312013-01-15 16:51:18 +00001011 else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001012 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 +00001013 unlink("/.layout_version");
1014 return false;
1015 }
1016 update_crypt = wiped;
Gary Pecke8bc5d72012-12-21 06:45:25 -08001017 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001018
Gary Pecke8bc5d72012-12-21 06:45:25 -08001019 if (wiped) {
Dees_Troyce675462013-01-09 19:48:21 +00001020#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
1021 if (Mount_Point == "/data" && Mount(false)) {
1022 if (TWFunc::Path_Exists("/tmp/edk_p_sd")) {
1023 Make_Dir("/data/system", true);
1024 TWFunc::copy_file("/tmp/edk_p_sd", "/data/system/edk_p_sd", 0600);
1025 }
1026 }
1027#endif
Dees_Troy16c2b312013-01-15 16:51:18 +00001028
Dees_Troy1c1ac442013-01-17 21:42:14 +00001029 if (Mount_Point == "/cache")
1030 DataManager::Output_Version();
1031
Dees_Troy16c2b312013-01-15 16:51:18 +00001032 if (TWFunc::Path_Exists("/.layout_version") && Mount(false))
1033 TWFunc::copy_file("/.layout_version", Layout_Filename, 0600);
1034
1035 if (update_crypt) {
1036 Setup_File_System(false);
1037 if (Is_Encrypted && !Is_Decrypted) {
1038 // just wiped an encrypted partition back to its unencrypted state
1039 Is_Encrypted = false;
1040 Is_Decrypted = false;
1041 Decrypted_Block_Device = "";
1042 if (Mount_Point == "/data") {
1043 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1044 DataManager::SetValue(TW_IS_DECRYPTED, 0);
1045 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001046 }
1047 }
1048 }
1049 return wiped;
Dees_Troy51a0e822012-09-05 15:24:24 -04001050}
1051
Gary Peck43acadf2012-11-21 21:19:01 -08001052bool TWPartition::Wipe() {
Gary Peck82599a82012-11-21 16:23:12 -08001053 if (Is_File_System(Current_File_System))
1054 return Wipe(Current_File_System);
1055 else
1056 return Wipe(Fstab_File_System);
Gary Peck43acadf2012-11-21 21:19:01 -08001057}
1058
Dees_Troye58d5262012-09-21 12:27:57 -04001059bool TWPartition::Wipe_AndSec(void) {
1060 if (!Has_Android_Secure)
1061 return false;
1062
Dees_Troye58d5262012-09-21 12:27:57 -04001063 if (!Mount(true))
1064 return false;
1065
Dees_Troy2673cec2013-04-02 20:22:16 +00001066 gui_print("Wiping %s\n", Backup_Display_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001067 TWFunc::removeDir(Mount_Point + "/.android_secure/", true);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001068 return true;
Dees_Troye58d5262012-09-21 12:27:57 -04001069}
1070
Dees_Troy51a0e822012-09-05 15:24:24 -04001071bool TWPartition::Backup(string backup_folder) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001072 if (Backup_Method == FILES)
1073 return Backup_Tar(backup_folder);
1074 else if (Backup_Method == DD)
1075 return Backup_DD(backup_folder);
1076 else if (Backup_Method == FLASH_UTILS)
1077 return Backup_Dump_Image(backup_folder);
Dees_Troy2673cec2013-04-02 20:22:16 +00001078 LOGERR("Unknown backup method for '%s'\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001079 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001080}
1081
Dees_Troy43d8b002012-09-17 16:00:01 -04001082bool TWPartition::Check_MD5(string restore_folder) {
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001083 string Full_Filename, md5file;
Dees_Troy43d8b002012-09-17 16:00:01 -04001084 char split_filename[512];
1085 int index = 0;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001086 twrpDigest md5sum;
Dees_Troy43d8b002012-09-17 16:00:01 -04001087
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001088 memset(split_filename, 0, sizeof(split_filename));
Dees_Troy43d8b002012-09-17 16:00:01 -04001089 Full_Filename = restore_folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04001090 if (!TWFunc::Path_Exists(Full_Filename)) {
1091 // This is a split archive, we presume
1092 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
Dees_Troy2673cec2013-04-02 20:22:16 +00001093 LOGINFO("split_filename: %s\n", split_filename);
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001094 md5file = split_filename;
1095 md5file += ".md5";
1096 if (!TWFunc::Path_Exists(md5file)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001097 LOGERR("No md5 file found for '%s'.\n", split_filename);
1098 LOGERR("Please unselect Enable MD5 verification to restore.\n");
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001099 return false;
1100 }
1101 md5sum.setfn(split_filename);
Dees_Troy83bd4832013-05-04 12:39:56 +00001102 while (index < 1000) {
1103 if (TWFunc::Path_Exists(split_filename) && md5sum.verify_md5digest() != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001104 LOGERR("MD5 failed to match on '%s'.\n", split_filename);
Dees_Troy43d8b002012-09-17 16:00:01 -04001105 return false;
1106 }
1107 index++;
1108 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001109 md5sum.setfn(split_filename);
Dees_Troy43d8b002012-09-17 16:00:01 -04001110 }
Dees_Troy4a2a1262012-09-18 09:33:47 -04001111 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001112 } else {
1113 // Single file archive
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001114 md5file = Full_Filename + ".md5";
1115 if (!TWFunc::Path_Exists(md5file)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001116 LOGERR("No md5 file found for '%s'.\n", Full_Filename.c_str());
1117 LOGERR("Please unselect Enable MD5 verification to restore.\n");
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001118 return false;
1119 }
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001120 md5sum.setfn(Full_Filename);
1121 if (md5sum.verify_md5digest() != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001122 LOGERR("MD5 failed to match on '%s'.\n", Full_Filename.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001123 return false;
1124 } else
1125 return true;
1126 }
1127 return false;
1128}
1129
Dees_Troy51a0e822012-09-05 15:24:24 -04001130bool TWPartition::Restore(string restore_folder) {
Gary Peck43acadf2012-11-21 21:19:01 -08001131 size_t first_period, second_period;
1132 string Restore_File_System;
1133
1134 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
Dees_Troy2673cec2013-04-02 20:22:16 +00001135 LOGINFO("Restore filename is: %s\n", Backup_FileName.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001136
1137 // Parse backup filename to extract the file system before wiping
1138 first_period = Backup_FileName.find(".");
1139 if (first_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001140 LOGERR("Unable to find file system (first period).\n");
Gary Peck43acadf2012-11-21 21:19:01 -08001141 return false;
1142 }
1143 Restore_File_System = Backup_FileName.substr(first_period + 1, Backup_FileName.size() - first_period - 1);
1144 second_period = Restore_File_System.find(".");
1145 if (second_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001146 LOGERR("Unable to find file system (second period).\n");
Gary Peck43acadf2012-11-21 21:19:01 -08001147 return false;
1148 }
1149 Restore_File_System.resize(second_period);
Dees_Troy2673cec2013-04-02 20:22:16 +00001150 LOGINFO("Restore file system is: '%s'.\n", Restore_File_System.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001151
1152 if (Is_File_System(Restore_File_System))
1153 return Restore_Tar(restore_folder, Restore_File_System);
1154 else if (Is_Image(Restore_File_System)) {
1155 if (Restore_File_System == "emmc")
1156 return Restore_DD(restore_folder);
1157 else if (Restore_File_System == "mtd" || Restore_File_System == "bml")
1158 return Restore_Flash_Image(restore_folder);
1159 }
1160
Dees_Troy2673cec2013-04-02 20:22:16 +00001161 LOGERR("Unknown restore method for '%s'\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001162 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001163}
1164
1165string TWPartition::Backup_Method_By_Name() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001166 if (Backup_Method == NONE)
1167 return "none";
1168 else if (Backup_Method == FILES)
1169 return "files";
1170 else if (Backup_Method == DD)
1171 return "dd";
1172 else if (Backup_Method == FLASH_UTILS)
1173 return "flash_utils";
1174 else
1175 return "undefined";
1176 return "ERROR!";
Dees_Troy51a0e822012-09-05 15:24:24 -04001177}
1178
1179bool TWPartition::Decrypt(string Password) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001180 LOGINFO("STUB TWPartition::Decrypt, password: '%s'\n", Password.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001181 // Is this needed?
Dees_Troy51a0e822012-09-05 15:24:24 -04001182 return 1;
1183}
1184
1185bool TWPartition::Wipe_Encryption() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001186 bool Save_Data_Media = Has_Data_Media;
1187
1188 if (!UnMount(true))
1189 return false;
1190
Dees_Troy38bd7602012-09-14 13:33:53 -04001191 Has_Data_Media = false;
Dees_Troy74fb2e92013-04-15 14:35:47 +00001192 Decrypted_Block_Device = "";
1193 Is_Decrypted = false;
1194 Is_Encrypted = false;
Gary Pecke8bc5d72012-12-21 06:45:25 -08001195 if (Wipe(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001196 Has_Data_Media = Save_Data_Media;
1197 if (Has_Data_Media && !Symlink_Mount_Point.empty()) {
1198 Recreate_Media_Folder();
1199 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001200 gui_print("You may need to reboot recovery to be able to use /data again.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001201 return true;
1202 } else {
1203 Has_Data_Media = Save_Data_Media;
Dees_Troy2673cec2013-04-02 20:22:16 +00001204 LOGERR("Unable to format to remove encryption.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001205 return false;
1206 }
1207 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001208}
1209
1210void TWPartition::Check_FS_Type() {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001211 const char* type;
1212 blkid_probe pr;
Dees_Troy5bf43922012-09-07 16:07:55 -04001213
Dees_Troy68cab492012-12-12 19:29:35 +00001214 if (Fstab_File_System == "yaffs2" || Fstab_File_System == "mtd" || Fstab_File_System == "bml" || Ignore_Blkid)
1215 return; // Running blkid on some mtd devices causes a massive crash or needs to be skipped
Dees_Troy5bf43922012-09-07 16:07:55 -04001216
Dees_Troy38bd7602012-09-14 13:33:53 -04001217 Find_Actual_Block_Device();
Dees_Troy8170a922012-09-18 15:40:25 -04001218 if (!Is_Present)
1219 return;
Dees_Troy51127312012-09-08 13:08:49 -04001220
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001221 pr = blkid_new_probe_from_filename(Actual_Block_Device.c_str());
1222 if (blkid_do_fullprobe(pr)) {
1223 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00001224 LOGINFO("Can't probe device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001225 return;
Dees_Troy5bf43922012-09-07 16:07:55 -04001226 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001227
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001228 if (blkid_probe_lookup_value(pr, "TYPE", &type, NULL) < 0) {
1229 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00001230 LOGINFO("can't find filesystem on device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001231 return;
1232 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001233
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001234 Current_File_System = type;
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001235 blkid_free_probe(pr);
Dees_Troy51a0e822012-09-05 15:24:24 -04001236}
1237
Gary Peck43acadf2012-11-21 21:19:01 -08001238bool TWPartition::Wipe_EXT23(string File_System) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001239 if (!UnMount(true))
1240 return false;
1241
Dees_Troy43d8b002012-09-17 16:00:01 -04001242 if (TWFunc::Path_Exists("/sbin/mke2fs")) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001243 string command, result;
Dees_Troy38bd7602012-09-14 13:33:53 -04001244
Dees_Troy2673cec2013-04-02 20:22:16 +00001245 gui_print("Formatting %s using mke2fs...\n", Display_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001246 Find_Actual_Block_Device();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001247 command = "mke2fs -t " + File_System + " -m 0 " + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001248 LOGINFO("mke2fs command: %s\n", command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001249 if (TWFunc::Exec_Cmd(command, result) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001250 Current_File_System = File_System;
Dees_Troye58d5262012-09-21 12:27:57 -04001251 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001252 gui_print("Done.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001253 return true;
1254 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001255 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001256 return false;
1257 }
1258 } else
1259 return Wipe_RMRF();
1260
1261 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001262}
1263
1264bool TWPartition::Wipe_EXT4() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001265 if (!UnMount(true))
1266 return false;
1267
Dees_Troya95f55c2013-08-17 13:14:43 +00001268#ifdef USE_EXT4
1269 gui_print("Formatting %s using make_ext4fs function.\n", Display_Name.c_str());
1270#ifdef HAVE_SELINUX
1271 if (make_ext4fs(Actual_Block_Device.c_str(), Length, Mount_Point.c_str(), selinux_handle) != 0) {
1272#else
1273 if (make_ext4fs(Actual_Block_Device.c_str(), Length) != 0) {
1274#endif
1275 LOGERR("Unable to wipe '%s' using function call.\n", Mount_Point.c_str());
1276 return false;
1277 } else {
1278 return true;
1279 }
1280#else
Dees_Troy43d8b002012-09-17 16:00:01 -04001281 if (TWFunc::Path_Exists("/sbin/make_ext4fs")) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001282 string Command, result;
Dees_Troy38bd7602012-09-14 13:33:53 -04001283
Dees_Troy2673cec2013-04-02 20:22:16 +00001284 gui_print("Formatting %s using make_ext4fs...\n", Display_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001285 Find_Actual_Block_Device();
1286 Command = "make_ext4fs";
1287 if (!Is_Decrypted && Length != 0) {
1288 // Only use length if we're not decrypted
1289 char len[32];
1290 sprintf(len, "%i", Length);
1291 Command += " -l ";
1292 Command += len;
1293 }
1294 Command += " " + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001295 LOGINFO("make_ext4fs command: %s\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001296 if (TWFunc::Exec_Cmd(Command, result) == 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() {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001312 string command, result;
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;
1321 if (TWFunc::Exec_Cmd(command, result) == 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() {
1339 string command, result;
1340
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;
1348 if (TWFunc::Exec_Cmd(command, result) == 0) {
1349 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() {
1405 string command, result;
1406
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;
1414 if (TWFunc::Exec_Cmd(command, result) == 0) {
1415 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];
1535 string Full_FileName, Command, result, 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());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001551 TWFunc::Exec_Cmd(Command, result);
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];
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001561 string Full_FileName, Command, result;
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());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001574 TWFunc::Exec_Cmd(Command, result);
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) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001638 string Full_FileName, Command, result;
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());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001658 TWFunc::Exec_Cmd(Command, result);
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) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001663 string Full_FileName, Command, result;
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());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001670 TWFunc::Exec_Cmd(Command, result);
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());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001673 TWFunc::Exec_Cmd(Command, result);
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}