blob: 8b2e09798f72c54a791c4df1a426a58b69da1d0a [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;
Hashcodedabfd492013-08-29 22:45:30 -0700407 } else if (strcmp(ptr, "usermrf") == 0) {
408 Use_Rm_Rf = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500409 } else if (ptr_len > 7 && strncmp(ptr, "backup=", 7) == 0) {
410 ptr += 7;
411 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y')
412 Can_Be_Backed_Up = true;
413 else
414 Can_Be_Backed_Up = false;
Dees_Troy63c8df72012-09-10 14:02:05 -0400415 } else if (strcmp(ptr, "wipeingui") == 0) {
416 Can_Be_Wiped = true;
417 Wipe_Available_in_GUI = true;
418 } else if (strcmp(ptr, "wipeduringfactoryreset") == 0) {
419 Can_Be_Wiped = true;
420 Wipe_Available_in_GUI = true;
421 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500422 } else if (ptr_len > 15 && strncmp(ptr, "subpartitionof=", 15) == 0) {
Dees_Troy2c4c26f2013-01-28 15:26:43 +0000423 ptr += 15;
Dees_Troy51127312012-09-08 13:08:49 -0400424 Is_SubPartition = true;
425 SubPartition_Of = ptr;
Dees_Troy68cab492012-12-12 19:29:35 +0000426 } else if (strcmp(ptr, "ignoreblkid") == 0) {
427 Ignore_Blkid = true;
Dees_Troy16c2b312013-01-15 16:51:18 +0000428 } else if (strcmp(ptr, "retainlayoutversion") == 0) {
429 Retain_Layout_Version = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500430 } else if (ptr_len > 8 && strncmp(ptr, "symlink=", 8) == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400431 ptr += 8;
432 Symlink_Path = ptr;
Dees_Troya13d74f2013-03-24 08:54:55 -0500433 } else if (ptr_len > 8 && strncmp(ptr, "display=", 8) == 0) {
434 has_display_name = true;
Dees_Troy51127312012-09-08 13:08:49 -0400435 ptr += 8;
Dees_Troya13d74f2013-03-24 08:54:55 -0500436 if (*ptr == '\"') ptr++;
Dees_Troy51127312012-09-08 13:08:49 -0400437 Display_Name = ptr;
Dees_Troya13d74f2013-03-24 08:54:55 -0500438 if (Display_Name.substr(Display_Name.size() - 1, 1) == "\"") {
439 Display_Name.resize(Display_Name.size() - 1);
440 }
441 } else if (ptr_len > 11 && strncmp(ptr, "storagename=", 11) == 0) {
442 has_storage_name = true;
443 ptr += 11;
444 if (*ptr == '\"') ptr++;
445 Storage_Name = ptr;
446 if (Storage_Name.substr(Storage_Name.size() - 1, 1) == "\"") {
447 Storage_Name.resize(Storage_Name.size() - 1);
448 }
449 } else if (ptr_len > 11 && strncmp(ptr, "backupname=", 10) == 0) {
450 has_backup_name = true;
451 ptr += 10;
452 if (*ptr == '\"') ptr++;
453 Backup_Display_Name = ptr;
454 if (Backup_Display_Name.substr(Backup_Display_Name.size() - 1, 1) == "\"") {
455 Backup_Display_Name.resize(Backup_Display_Name.size() - 1);
456 }
457 } else if (ptr_len > 10 && strncmp(ptr, "blocksize=", 10) == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400458 ptr += 10;
459 Format_Block_Size = atoi(ptr);
Dees_Troya13d74f2013-03-24 08:54:55 -0500460 } else if (ptr_len > 7 && strncmp(ptr, "length=", 7) == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400461 ptr += 7;
462 Length = atoi(ptr);
Dees_Troy83bd4832013-05-04 12:39:56 +0000463 } else if (ptr_len > 17 && strncmp(ptr, "canencryptbackup=", 17) == 0) {
464 ptr += 17;
465 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y')
466 Can_Encrypt_Backup = true;
467 else
468 Can_Encrypt_Backup = false;
469 } else if (ptr_len > 21 && strncmp(ptr, "userdataencryptbackup=", 21) == 0) {
470 ptr += 21;
471 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y') {
472 Can_Encrypt_Backup = true;
473 Use_Userdata_Encryption = true;
474 } else {
475 Use_Userdata_Encryption = false;
476 }
Dees_Troy51127312012-09-08 13:08:49 -0400477 } else {
478 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000479 LOGERR("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400480 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000481 LOGINFO("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400482 }
483 while (index < flags_len && flags[index] != '\0')
484 index++;
485 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500486 if (has_display_name && !has_storage_name)
487 Storage_Name = Display_Name;
488 if (!has_display_name && has_storage_name)
489 Display_Name = Storage_Name;
Dees_Troy74fb2e92013-04-15 14:35:47 +0000490 if (has_display_name && !has_backup_name && Backup_Display_Name != "Android Secure")
Dees_Troya13d74f2013-03-24 08:54:55 -0500491 Backup_Display_Name = Display_Name;
492 if (!has_display_name && has_backup_name)
493 Display_Name = Backup_Display_Name;
Dees_Troy51127312012-09-08 13:08:49 -0400494 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400495}
496
Dees_Troy5bf43922012-09-07 16:07:55 -0400497bool TWPartition::Is_File_System(string File_System) {
498 if (File_System == "ext2" ||
Dees_Troy63c8df72012-09-10 14:02:05 -0400499 File_System == "ext3" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400500 File_System == "ext4" ||
501 File_System == "vfat" ||
502 File_System == "ntfs" ||
503 File_System == "yaffs2" ||
bigbiff bigbiff3e146522012-11-14 14:32:59 -0500504 File_System == "exfat" ||
Dees_Troye5017042013-08-29 16:38:55 +0000505 File_System == "f2fs" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400506 File_System == "auto")
507 return true;
508 else
509 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400510}
511
Dees_Troy5bf43922012-09-07 16:07:55 -0400512bool TWPartition::Is_Image(string File_System) {
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400513 if (File_System == "emmc" || File_System == "mtd" || File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -0400514 return true;
515 else
516 return false;
517}
518
Dees_Troy51127312012-09-08 13:08:49 -0400519bool TWPartition::Make_Dir(string Path, bool Display_Error) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400520 if (!TWFunc::Path_Exists(Path)) {
Dees_Troy51127312012-09-08 13:08:49 -0400521 if (mkdir(Path.c_str(), 0777) == -1) {
522 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000523 LOGERR("Can not create '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400524 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000525 LOGINFO("Can not create '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400526 return false;
527 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000528 LOGINFO("Created '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400529 return true;
530 }
531 }
532 return true;
533}
534
Dees_Troy5bf43922012-09-07 16:07:55 -0400535void TWPartition::Setup_File_System(bool Display_Error) {
536 struct statfs st;
537
538 Can_Be_Mounted = true;
539 Can_Be_Wiped = true;
540
Dees_Troy5bf43922012-09-07 16:07:55 -0400541 // Make the mount point folder if it doesn't exist
Dees_Troy51127312012-09-08 13:08:49 -0400542 Make_Dir(Mount_Point, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400543 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
544 Backup_Name = Display_Name;
545 Backup_Method = FILES;
546}
547
548void TWPartition::Setup_Image(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400549 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
550 Backup_Name = Display_Name;
Gary Peck82599a82012-11-21 16:23:12 -0800551 if (Current_File_System == "emmc")
Dees_Troy5bf43922012-09-07 16:07:55 -0400552 Backup_Method = DD;
Gary Peck82599a82012-11-21 16:23:12 -0800553 else if (Current_File_System == "mtd" || Current_File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -0400554 Backup_Method = FLASH_UTILS;
555 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000556 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 -0400557 if (Find_Partition_Size()) {
558 Used = Size;
559 Backup_Size = Size;
Dees_Troy51a0e822012-09-05 15:24:24 -0400560 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400561 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000562 LOGERR("Unable to find parition size for '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400563 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000564 LOGINFO("Unable to find parition size for '%s'\n", Mount_Point.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400565 }
566}
567
Dees_Troye58d5262012-09-21 12:27:57 -0400568void TWPartition::Setup_AndSec(void) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500569 Backup_Display_Name = "Android Secure";
Dees_Troye58d5262012-09-21 12:27:57 -0400570 Backup_Name = "and-sec";
Dees_Troya13d74f2013-03-24 08:54:55 -0500571 Can_Be_Backed_Up = true;
Dees_Troye58d5262012-09-21 12:27:57 -0400572 Has_Android_Secure = true;
573 Symlink_Path = Mount_Point + "/.android_secure";
574 Symlink_Mount_Point = "/and-sec";
575 Backup_Path = Symlink_Mount_Point;
576 Make_Dir("/and-sec", true);
577 Recreate_AndSec_Folder();
578}
579
Dees_Troy5bf43922012-09-07 16:07:55 -0400580void TWPartition::Find_Real_Block_Device(string& Block, bool Display_Error) {
581 char device[512], realDevice[512];
582
583 strcpy(device, Block.c_str());
584 memset(realDevice, 0, sizeof(realDevice));
585 while (readlink(device, realDevice, sizeof(realDevice)) > 0)
586 {
587 strcpy(device, realDevice);
588 memset(realDevice, 0, sizeof(realDevice));
589 }
590
591 if (device[0] != '/') {
592 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000593 LOGERR("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400594 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000595 LOGINFO("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400596 return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400597 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400598 Block = device;
599 return;
600 }
601}
602
Dees_Troy8e337f32012-10-13 22:07:49 -0400603void TWPartition::Mount_Storage_Retry(void) {
604 // On some devices, storage doesn't want to mount right away, retry and sleep
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500605 if (!Mount(true)) {
Dees_Troy8e337f32012-10-13 22:07:49 -0400606 int retry_count = 5;
607 while (retry_count > 0 && !Mount(false)) {
608 usleep(500000);
609 retry_count--;
610 }
611 Mount(true);
612 }
613}
614
Dees_Troy38bd7602012-09-14 13:33:53 -0400615bool TWPartition::Find_MTD_Block_Device(string MTD_Name) {
616 FILE *fp = NULL;
617 char line[255];
618
619 fp = fopen("/proc/mtd", "rt");
620 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000621 LOGERR("Device does not support /proc/mtd\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400622 return false;
623 }
624
625 while (fgets(line, sizeof(line), fp) != NULL)
626 {
627 char device[32], label[32];
628 unsigned long size = 0;
629 char* fstype = NULL;
630 int deviceId;
631
632 sscanf(line, "%s %lx %*s %*c%s", device, &size, label);
633
634 // Skip header and blank lines
635 if ((strcmp(device, "dev:") == 0) || (strlen(line) < 8))
636 continue;
637
638 // Strip off the trailing " from the label
639 label[strlen(label)-1] = '\0';
640
641 if (strcmp(label, MTD_Name.c_str()) == 0) {
642 // We found our device
643 // Strip off the trailing : from the device
644 device[strlen(device)-1] = '\0';
645 if (sscanf(device,"mtd%d", &deviceId) == 1) {
646 sprintf(device, "/dev/block/mtdblock%d", deviceId);
647 Primary_Block_Device = device;
Dees_Troy76543db2013-06-19 16:24:30 +0000648 fclose(fp);
649 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -0400650 }
651 }
652 }
653 fclose(fp);
654
655 return false;
656}
657
Dees_Troy51127312012-09-08 13:08:49 -0400658bool TWPartition::Get_Size_Via_statfs(bool Display_Error) {
659 struct statfs st;
660 string Local_Path = Mount_Point + "/.";
661
662 if (!Mount(Display_Error))
663 return false;
664
665 if (statfs(Local_Path.c_str(), &st) != 0) {
666 if (!Removable) {
667 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000668 LOGERR("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400669 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000670 LOGINFO("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400671 }
672 return false;
673 }
674 Size = (st.f_blocks * st.f_bsize);
675 Used = ((st.f_blocks - st.f_bfree) * st.f_bsize);
676 Free = (st.f_bfree * st.f_bsize);
677 Backup_Size = Used;
678 return true;
679}
680
681bool TWPartition::Get_Size_Via_df(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400682 FILE* fp;
683 char command[255], line[512];
684 int include_block = 1;
685 unsigned int min_len;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500686 string result;
Dees_Troy5bf43922012-09-07 16:07:55 -0400687
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());
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500693 TWFunc::Exec_Cmd(command, result);
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()) {
923 string Command, Result;
924 Command = "mount '" + Symlink_Path + "' '" + Symlink_Mount_Point + "'";
925 TWFunc::Exec_Cmd(Command, Result);
Dees_Troy5bf43922012-09-07 16:07:55 -0400926 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400927 return true;
928}
929
930bool TWPartition::UnMount(bool Display_Error) {
931 if (Is_Mounted()) {
932 int never_unmount_system;
933
934 DataManager::GetValue(TW_DONT_UNMOUNT_SYSTEM, never_unmount_system);
935 if (never_unmount_system == 1 && Mount_Point == "/system")
936 return true; // Never unmount system if you're not supposed to unmount it
937
Dees_Troyc8bafa12013-01-10 15:43:00 +0000938#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
939 if (EcryptFS_Password.size() > 0) {
940 if (unmount_ecryptfs_drive(Mount_Point.c_str()) != 0) {
941 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000942 LOGERR("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("Unable to unmount ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troyc8bafa12013-01-10 15:43:00 +0000945 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000946 LOGINFO("Successfully unmounted ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troyc8bafa12013-01-10 15:43:00 +0000947 }
948 }
949#endif
950
Dees_Troy38bd7602012-09-14 13:33:53 -0400951 if (!Symlink_Mount_Point.empty())
952 umount(Symlink_Mount_Point.c_str());
953
Dees_Troyb05ddee2013-01-28 20:24:50 +0000954 umount(Mount_Point.c_str());
955 if (Is_Mounted()) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400956 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000957 LOGERR("Unable to unmount '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400958 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000959 LOGINFO("Unable to unmount '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400960 return false;
961 } else
962 return true;
963 } else {
964 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400965 }
966}
967
Gary Peck43acadf2012-11-21 21:19:01 -0800968bool TWPartition::Wipe(string New_File_System) {
Dees_Troy16c2b312013-01-15 16:51:18 +0000969 bool wiped = false, update_crypt = false;
970 int check;
971 string Layout_Filename = Mount_Point + "/.layout_version";
972
Dees_Troy38bd7602012-09-14 13:33:53 -0400973 if (!Can_Be_Wiped) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000974 LOGERR("Partition '%s' cannot be wiped.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -0400975 return false;
976 }
977
Dees_Troyc51f1f92012-09-20 15:32:13 -0400978 if (Mount_Point == "/cache")
Dees_Troy2673cec2013-04-02 20:22:16 +0000979 Log_Offset = 0;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400980
Dees_Troyce675462013-01-09 19:48:21 +0000981#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
982 if (Mount_Point == "/data" && Mount(false)) {
983 if (TWFunc::Path_Exists("/data/system/edk_p_sd"))
984 TWFunc::copy_file("/data/system/edk_p_sd", "/tmp/edk_p_sd", 0600);
985 }
986#endif
987
Dees_Troy16c2b312013-01-15 16:51:18 +0000988 if (Retain_Layout_Version && Mount(false) && TWFunc::Path_Exists(Layout_Filename))
989 TWFunc::copy_file(Layout_Filename, "/.layout_version", 0600);
990 else
991 unlink("/.layout_version");
Dees_Troy38bd7602012-09-14 13:33:53 -0400992
Dees_Troy16c2b312013-01-15 16:51:18 +0000993 if (Has_Data_Media) {
994 wiped = Wipe_Data_Without_Wiping_Media();
995 } else {
Gary Pecke8bc5d72012-12-21 06:45:25 -0800996
Dees_Troy16c2b312013-01-15 16:51:18 +0000997 DataManager::GetValue(TW_RM_RF_VAR, check);
998
Hashcodedabfd492013-08-29 22:45:30 -0700999 if (check || Use_Rm_Rf)
Dees_Troy16c2b312013-01-15 16:51:18 +00001000 wiped = Wipe_RMRF();
1001 else if (New_File_System == "ext4")
1002 wiped = Wipe_EXT4();
1003 else if (New_File_System == "ext2" || New_File_System == "ext3")
1004 wiped = Wipe_EXT23(New_File_System);
1005 else if (New_File_System == "vfat")
1006 wiped = Wipe_FAT();
1007 else if (New_File_System == "exfat")
1008 wiped = Wipe_EXFAT();
1009 else if (New_File_System == "yaffs2")
1010 wiped = Wipe_MTD();
Dees_Troye5017042013-08-29 16:38:55 +00001011 else if (New_File_System == "f2fs")
1012 wiped = Wipe_F2FS();
Dees_Troy16c2b312013-01-15 16:51:18 +00001013 else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001014 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 +00001015 unlink("/.layout_version");
1016 return false;
1017 }
1018 update_crypt = wiped;
Gary Pecke8bc5d72012-12-21 06:45:25 -08001019 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001020
Gary Pecke8bc5d72012-12-21 06:45:25 -08001021 if (wiped) {
Dees_Troyce675462013-01-09 19:48:21 +00001022#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
1023 if (Mount_Point == "/data" && Mount(false)) {
1024 if (TWFunc::Path_Exists("/tmp/edk_p_sd")) {
1025 Make_Dir("/data/system", true);
1026 TWFunc::copy_file("/tmp/edk_p_sd", "/data/system/edk_p_sd", 0600);
1027 }
1028 }
1029#endif
Dees_Troy16c2b312013-01-15 16:51:18 +00001030
Dees_Troy1c1ac442013-01-17 21:42:14 +00001031 if (Mount_Point == "/cache")
1032 DataManager::Output_Version();
1033
Dees_Troy16c2b312013-01-15 16:51:18 +00001034 if (TWFunc::Path_Exists("/.layout_version") && Mount(false))
1035 TWFunc::copy_file("/.layout_version", Layout_Filename, 0600);
1036
1037 if (update_crypt) {
1038 Setup_File_System(false);
1039 if (Is_Encrypted && !Is_Decrypted) {
1040 // just wiped an encrypted partition back to its unencrypted state
1041 Is_Encrypted = false;
1042 Is_Decrypted = false;
1043 Decrypted_Block_Device = "";
1044 if (Mount_Point == "/data") {
1045 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1046 DataManager::SetValue(TW_IS_DECRYPTED, 0);
1047 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001048 }
1049 }
1050 }
1051 return wiped;
Dees_Troy51a0e822012-09-05 15:24:24 -04001052}
1053
Gary Peck43acadf2012-11-21 21:19:01 -08001054bool TWPartition::Wipe() {
Gary Peck82599a82012-11-21 16:23:12 -08001055 if (Is_File_System(Current_File_System))
1056 return Wipe(Current_File_System);
1057 else
1058 return Wipe(Fstab_File_System);
Gary Peck43acadf2012-11-21 21:19:01 -08001059}
1060
Dees_Troye58d5262012-09-21 12:27:57 -04001061bool TWPartition::Wipe_AndSec(void) {
1062 if (!Has_Android_Secure)
1063 return false;
1064
Dees_Troye58d5262012-09-21 12:27:57 -04001065 if (!Mount(true))
1066 return false;
1067
Dees_Troy2673cec2013-04-02 20:22:16 +00001068 gui_print("Wiping %s\n", Backup_Display_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001069 TWFunc::removeDir(Mount_Point + "/.android_secure/", true);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001070 return true;
Dees_Troye58d5262012-09-21 12:27:57 -04001071}
1072
Dees_Troy51a0e822012-09-05 15:24:24 -04001073bool TWPartition::Backup(string backup_folder) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001074 if (Backup_Method == FILES)
1075 return Backup_Tar(backup_folder);
1076 else if (Backup_Method == DD)
1077 return Backup_DD(backup_folder);
1078 else if (Backup_Method == FLASH_UTILS)
1079 return Backup_Dump_Image(backup_folder);
Dees_Troy2673cec2013-04-02 20:22:16 +00001080 LOGERR("Unknown backup method for '%s'\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001081 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001082}
1083
Dees_Troy43d8b002012-09-17 16:00:01 -04001084bool TWPartition::Check_MD5(string restore_folder) {
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001085 string Full_Filename, md5file;
Dees_Troy43d8b002012-09-17 16:00:01 -04001086 char split_filename[512];
1087 int index = 0;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001088 twrpDigest md5sum;
Dees_Troy43d8b002012-09-17 16:00:01 -04001089
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001090 memset(split_filename, 0, sizeof(split_filename));
Dees_Troy43d8b002012-09-17 16:00:01 -04001091 Full_Filename = restore_folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04001092 if (!TWFunc::Path_Exists(Full_Filename)) {
1093 // This is a split archive, we presume
1094 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
Dees_Troy2673cec2013-04-02 20:22:16 +00001095 LOGINFO("split_filename: %s\n", split_filename);
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001096 md5file = split_filename;
1097 md5file += ".md5";
1098 if (!TWFunc::Path_Exists(md5file)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001099 LOGERR("No md5 file found for '%s'.\n", split_filename);
1100 LOGERR("Please unselect Enable MD5 verification to restore.\n");
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001101 return false;
1102 }
1103 md5sum.setfn(split_filename);
Dees_Troy83bd4832013-05-04 12:39:56 +00001104 while (index < 1000) {
1105 if (TWFunc::Path_Exists(split_filename) && md5sum.verify_md5digest() != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001106 LOGERR("MD5 failed to match on '%s'.\n", split_filename);
Dees_Troy43d8b002012-09-17 16:00:01 -04001107 return false;
1108 }
1109 index++;
1110 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001111 md5sum.setfn(split_filename);
Dees_Troy43d8b002012-09-17 16:00:01 -04001112 }
Dees_Troy4a2a1262012-09-18 09:33:47 -04001113 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001114 } else {
1115 // Single file archive
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001116 md5file = Full_Filename + ".md5";
1117 if (!TWFunc::Path_Exists(md5file)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001118 LOGERR("No md5 file found for '%s'.\n", Full_Filename.c_str());
1119 LOGERR("Please unselect Enable MD5 verification to restore.\n");
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001120 return false;
1121 }
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001122 md5sum.setfn(Full_Filename);
1123 if (md5sum.verify_md5digest() != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001124 LOGERR("MD5 failed to match on '%s'.\n", Full_Filename.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001125 return false;
1126 } else
1127 return true;
1128 }
1129 return false;
1130}
1131
Dees_Troy51a0e822012-09-05 15:24:24 -04001132bool TWPartition::Restore(string restore_folder) {
Gary Peck43acadf2012-11-21 21:19:01 -08001133 size_t first_period, second_period;
1134 string Restore_File_System;
1135
1136 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
Dees_Troy2673cec2013-04-02 20:22:16 +00001137 LOGINFO("Restore filename is: %s\n", Backup_FileName.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001138
1139 // Parse backup filename to extract the file system before wiping
1140 first_period = Backup_FileName.find(".");
1141 if (first_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001142 LOGERR("Unable to find file system (first period).\n");
Gary Peck43acadf2012-11-21 21:19:01 -08001143 return false;
1144 }
1145 Restore_File_System = Backup_FileName.substr(first_period + 1, Backup_FileName.size() - first_period - 1);
1146 second_period = Restore_File_System.find(".");
1147 if (second_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001148 LOGERR("Unable to find file system (second period).\n");
Gary Peck43acadf2012-11-21 21:19:01 -08001149 return false;
1150 }
1151 Restore_File_System.resize(second_period);
Dees_Troy2673cec2013-04-02 20:22:16 +00001152 LOGINFO("Restore file system is: '%s'.\n", Restore_File_System.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001153
1154 if (Is_File_System(Restore_File_System))
1155 return Restore_Tar(restore_folder, Restore_File_System);
1156 else if (Is_Image(Restore_File_System)) {
1157 if (Restore_File_System == "emmc")
1158 return Restore_DD(restore_folder);
1159 else if (Restore_File_System == "mtd" || Restore_File_System == "bml")
1160 return Restore_Flash_Image(restore_folder);
1161 }
1162
Dees_Troy2673cec2013-04-02 20:22:16 +00001163 LOGERR("Unknown restore method for '%s'\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001164 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001165}
1166
1167string TWPartition::Backup_Method_By_Name() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001168 if (Backup_Method == NONE)
1169 return "none";
1170 else if (Backup_Method == FILES)
1171 return "files";
1172 else if (Backup_Method == DD)
1173 return "dd";
1174 else if (Backup_Method == FLASH_UTILS)
1175 return "flash_utils";
1176 else
1177 return "undefined";
1178 return "ERROR!";
Dees_Troy51a0e822012-09-05 15:24:24 -04001179}
1180
1181bool TWPartition::Decrypt(string Password) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001182 LOGINFO("STUB TWPartition::Decrypt, password: '%s'\n", Password.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001183 // Is this needed?
Dees_Troy51a0e822012-09-05 15:24:24 -04001184 return 1;
1185}
1186
1187bool TWPartition::Wipe_Encryption() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001188 bool Save_Data_Media = Has_Data_Media;
1189
1190 if (!UnMount(true))
1191 return false;
1192
Dees_Troy38bd7602012-09-14 13:33:53 -04001193 Has_Data_Media = false;
Dees_Troy74fb2e92013-04-15 14:35:47 +00001194 Decrypted_Block_Device = "";
1195 Is_Decrypted = false;
1196 Is_Encrypted = false;
Gary Pecke8bc5d72012-12-21 06:45:25 -08001197 if (Wipe(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001198 Has_Data_Media = Save_Data_Media;
1199 if (Has_Data_Media && !Symlink_Mount_Point.empty()) {
1200 Recreate_Media_Folder();
1201 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001202 gui_print("You may need to reboot recovery to be able to use /data again.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001203 return true;
1204 } else {
1205 Has_Data_Media = Save_Data_Media;
Dees_Troy2673cec2013-04-02 20:22:16 +00001206 LOGERR("Unable to format to remove encryption.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001207 return false;
1208 }
1209 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001210}
1211
1212void TWPartition::Check_FS_Type() {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001213 const char* type;
1214 blkid_probe pr;
Dees_Troy5bf43922012-09-07 16:07:55 -04001215
Dees_Troy68cab492012-12-12 19:29:35 +00001216 if (Fstab_File_System == "yaffs2" || Fstab_File_System == "mtd" || Fstab_File_System == "bml" || Ignore_Blkid)
1217 return; // Running blkid on some mtd devices causes a massive crash or needs to be skipped
Dees_Troy5bf43922012-09-07 16:07:55 -04001218
Dees_Troy38bd7602012-09-14 13:33:53 -04001219 Find_Actual_Block_Device();
Dees_Troy8170a922012-09-18 15:40:25 -04001220 if (!Is_Present)
1221 return;
Dees_Troy51127312012-09-08 13:08:49 -04001222
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001223 pr = blkid_new_probe_from_filename(Actual_Block_Device.c_str());
1224 if (blkid_do_fullprobe(pr)) {
1225 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00001226 LOGINFO("Can't probe device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001227 return;
Dees_Troy5bf43922012-09-07 16:07:55 -04001228 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001229
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001230 if (blkid_probe_lookup_value(pr, "TYPE", &type, NULL) < 0) {
1231 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00001232 LOGINFO("can't find filesystem on device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001233 return;
1234 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001235
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001236 Current_File_System = type;
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001237 blkid_free_probe(pr);
Dees_Troy51a0e822012-09-05 15:24:24 -04001238}
1239
Gary Peck43acadf2012-11-21 21:19:01 -08001240bool TWPartition::Wipe_EXT23(string File_System) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001241 if (!UnMount(true))
1242 return false;
1243
Dees_Troy43d8b002012-09-17 16:00:01 -04001244 if (TWFunc::Path_Exists("/sbin/mke2fs")) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001245 string command, result;
Dees_Troy38bd7602012-09-14 13:33:53 -04001246
Dees_Troy2673cec2013-04-02 20:22:16 +00001247 gui_print("Formatting %s using mke2fs...\n", Display_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001248 Find_Actual_Block_Device();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001249 command = "mke2fs -t " + File_System + " -m 0 " + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001250 LOGINFO("mke2fs command: %s\n", command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001251 if (TWFunc::Exec_Cmd(command, result) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001252 Current_File_System = File_System;
Dees_Troye58d5262012-09-21 12:27:57 -04001253 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001254 gui_print("Done.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001255 return true;
1256 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001257 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001258 return false;
1259 }
1260 } else
1261 return Wipe_RMRF();
1262
1263 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001264}
1265
1266bool TWPartition::Wipe_EXT4() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001267 if (!UnMount(true))
1268 return false;
1269
Dees_Troyb3265ab2013-08-30 02:59:14 +00001270#if defined(HAVE_SELINUX) && defined(USE_EXT4)
Dees_Troya95f55c2013-08-17 13:14:43 +00001271 gui_print("Formatting %s using make_ext4fs function.\n", Display_Name.c_str());
Dees_Troya95f55c2013-08-17 13:14:43 +00001272 if (make_ext4fs(Actual_Block_Device.c_str(), Length, Mount_Point.c_str(), selinux_handle) != 0) {
Dees_Troya95f55c2013-08-17 13:14:43 +00001273 LOGERR("Unable to wipe '%s' using function call.\n", Mount_Point.c_str());
1274 return false;
1275 } else {
1276 return true;
1277 }
1278#else
Dees_Troy43d8b002012-09-17 16:00:01 -04001279 if (TWFunc::Path_Exists("/sbin/make_ext4fs")) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001280 string Command, result;
Dees_Troy38bd7602012-09-14 13:33:53 -04001281
Dees_Troy2673cec2013-04-02 20:22:16 +00001282 gui_print("Formatting %s using make_ext4fs...\n", Display_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001283 Find_Actual_Block_Device();
1284 Command = "make_ext4fs";
1285 if (!Is_Decrypted && Length != 0) {
1286 // Only use length if we're not decrypted
1287 char len[32];
1288 sprintf(len, "%i", Length);
1289 Command += " -l ";
1290 Command += len;
1291 }
1292 Command += " " + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001293 LOGINFO("make_ext4fs command: %s\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001294 if (TWFunc::Exec_Cmd(Command, result) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001295 Current_File_System = "ext4";
Dees_Troye58d5262012-09-21 12:27:57 -04001296 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001297 gui_print("Done.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001298 return true;
1299 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001300 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001301 return false;
1302 }
1303 } else
Gary Peck43acadf2012-11-21 21:19:01 -08001304 return Wipe_EXT23("ext4");
Dees_Troya95f55c2013-08-17 13:14:43 +00001305#endif
Dees_Troy38bd7602012-09-14 13:33:53 -04001306 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001307}
1308
1309bool TWPartition::Wipe_FAT() {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001310 string command, result;
Dees_Troy38bd7602012-09-14 13:33:53 -04001311
Dees_Troy43d8b002012-09-17 16:00:01 -04001312 if (TWFunc::Path_Exists("/sbin/mkdosfs")) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001313 if (!UnMount(true))
1314 return false;
1315
Dees_Troy2673cec2013-04-02 20:22:16 +00001316 gui_print("Formatting %s using mkdosfs...\n", Display_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001317 Find_Actual_Block_Device();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001318 command = "mkdosfs " + Actual_Block_Device;
1319 if (TWFunc::Exec_Cmd(command, result) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001320 Current_File_System = "vfat";
Dees_Troye58d5262012-09-21 12:27:57 -04001321 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001322 gui_print("Done.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001323 return true;
1324 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001325 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001326 return false;
1327 }
1328 return true;
1329 }
1330 else
1331 return Wipe_RMRF();
1332
1333 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001334}
1335
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001336bool TWPartition::Wipe_EXFAT() {
1337 string command, result;
1338
1339 if (TWFunc::Path_Exists("/sbin/mkexfatfs")) {
1340 if (!UnMount(true))
1341 return false;
1342
Dees_Troy2673cec2013-04-02 20:22:16 +00001343 gui_print("Formatting %s using mkexfatfs...\n", Display_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001344 Find_Actual_Block_Device();
1345 command = "mkexfatfs " + Actual_Block_Device;
1346 if (TWFunc::Exec_Cmd(command, result) == 0) {
1347 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001348 gui_print("Done.\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001349 return true;
1350 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001351 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001352 return false;
1353 }
1354 return true;
1355 }
1356 return false;
1357}
1358
Dees_Troy38bd7602012-09-14 13:33:53 -04001359bool TWPartition::Wipe_MTD() {
1360 if (!UnMount(true))
1361 return false;
1362
Dees_Troy2673cec2013-04-02 20:22:16 +00001363 gui_print("MTD Formatting \"%s\"\n", MTD_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001364
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001365 mtd_scan_partitions();
1366 const MtdPartition* mtd = mtd_find_partition_by_name(MTD_Name.c_str());
1367 if (mtd == NULL) {
1368 LOGERR("No mtd partition named '%s'", MTD_Name.c_str());
1369 return false;
1370 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001371
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001372 MtdWriteContext* ctx = mtd_write_partition(mtd);
1373 if (ctx == NULL) {
1374 LOGERR("Can't write '%s', failed to format.", MTD_Name.c_str());
1375 return false;
1376 }
1377 if (mtd_erase_blocks(ctx, -1) == -1) {
1378 mtd_write_close(ctx);
1379 LOGERR("Failed to format '%s'", MTD_Name.c_str());
1380 return false;
1381 }
1382 if (mtd_write_close(ctx) != 0) {
1383 LOGERR("Failed to close '%s'", MTD_Name.c_str());
1384 return false;
1385 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001386 Current_File_System = "yaffs2";
Dees_Troye58d5262012-09-21 12:27:57 -04001387 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001388 gui_print("Done.\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001389 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001390}
1391
1392bool TWPartition::Wipe_RMRF() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001393 if (!Mount(true))
1394 return false;
1395
Dees_Troy2673cec2013-04-02 20:22:16 +00001396 gui_print("Removing all files under '%s'\n", Mount_Point.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001397 TWFunc::removeDir(Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04001398 Recreate_AndSec_Folder();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001399 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001400}
1401
Dees_Troye5017042013-08-29 16:38:55 +00001402bool TWPartition::Wipe_F2FS() {
1403 string command, result;
1404
1405 if (TWFunc::Path_Exists("/sbin/mkfs.f2fs")) {
1406 if (!UnMount(true))
1407 return false;
1408
1409 gui_print("Formatting %s using mkfs.f2fs...\n", Display_Name.c_str());
1410 Find_Actual_Block_Device();
1411 command = "mkfs.f2fs " + Actual_Block_Device;
1412 if (TWFunc::Exec_Cmd(command, result) == 0) {
1413 Recreate_AndSec_Folder();
1414 gui_print("Done.\n");
1415 return true;
1416 } else {
1417 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
1418 return false;
1419 }
1420 return true;
1421 } else {
1422 gui_print("mkfs.f2fs binary not found, using rm -rf to wipe.\n");
1423 return Wipe_RMRF();
1424 }
1425 return false;
1426}
1427
Dees_Troy51a0e822012-09-05 15:24:24 -04001428bool TWPartition::Wipe_Data_Without_Wiping_Media() {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001429 string dir;
Dees_Troy38bd7602012-09-14 13:33:53 -04001430
1431 // This handles wiping data on devices with "sdcard" in /data/media
1432 if (!Mount(true))
1433 return false;
1434
Dees_Troy2673cec2013-04-02 20:22:16 +00001435 gui_print("Wiping data without wiping /data/media ...\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001436
1437 DIR* d;
1438 d = opendir("/data");
Dees_Troy16b74352012-11-14 22:27:31 +00001439 if (d != NULL) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001440 struct dirent* de;
1441 while ((de = readdir(d)) != NULL) {
Dees_Troy16b74352012-11-14 22:27:31 +00001442 if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) continue;
1443 // The media folder is the "internal sdcard"
1444 // The .layout_version file is responsible for determining whether 4.2 decides up upgrade
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001445 // the media folder for multi-user.
Dees_Troy16b74352012-11-14 22:27:31 +00001446 if (strcmp(de->d_name, "media") == 0 || strcmp(de->d_name, ".layout_version") == 0) continue;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001447
1448 dir = "/data/";
1449 dir.append(de->d_name);
Dees_Troyce675462013-01-09 19:48:21 +00001450 if (de->d_type == DT_DIR) {
1451 TWFunc::removeDir(dir, false);
bigbiff bigbiff98f1f902013-01-19 18:46:13 -05001452 } 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 +00001453 if (!unlink(dir.c_str()))
Dees_Troy2673cec2013-04-02 20:22:16 +00001454 LOGINFO("Unable to unlink '%s'\n", dir.c_str());
Dees_Troyce675462013-01-09 19:48:21 +00001455 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001456 }
1457 closedir(d);
Dees_Troy2673cec2013-04-02 20:22:16 +00001458 gui_print("Done.\n");
Dees_Troy16b74352012-11-14 22:27:31 +00001459 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -04001460 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001461 gui_print("Dirent failed to open /data, error!\n");
Dees_Troy16b74352012-11-14 22:27:31 +00001462 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001463}
1464
1465bool TWPartition::Backup_Tar(string backup_folder) {
Dees_Troy4a2a1262012-09-18 09:33:47 -04001466 char back_name[255], split_index[5];
1467 string Full_FileName, Split_FileName, Tar_Args, Command;
Dees_Troy83bd4832013-05-04 12:39:56 +00001468 int use_compression, use_encryption = 0, index, backup_count;
Dees_Troy4a2a1262012-09-18 09:33:47 -04001469 struct stat st;
Dees_Troy7c2dec82012-09-26 09:49:14 -04001470 unsigned long long total_bsize = 0, file_size;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001471 twrpTar tar;
1472 vector <string> files;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001473
Dees_Troy43d8b002012-09-17 16:00:01 -04001474 if (!Mount(true))
1475 return false;
1476
Dees_Troya13d74f2013-03-24 08:54:55 -05001477 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Backup_Display_Name, "Backing Up");
Dees_Troy2673cec2013-04-02 20:22:16 +00001478 gui_print("Backing up %s...\n", Backup_Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001479
1480 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
Dees_Troy83bd4832013-05-04 12:39:56 +00001481 tar.use_compression = use_compression;
bigbiff bigbiff86e77bc2013-08-26 21:36:23 -04001482 //exclude Google Music Cache
1483 tar.setexcl("/data/data/com.google.android.music/files");
Dees_Troy83bd4832013-05-04 12:39:56 +00001484#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
1485 DataManager::GetValue("tw_encrypt_backup", use_encryption);
1486 if (use_encryption && Can_Encrypt_Backup) {
1487 tar.use_encryption = use_encryption;
1488 if (Use_Userdata_Encryption)
1489 tar.userdata_encryption = use_encryption;
1490 } else {
1491 use_encryption = false;
1492 }
1493#endif
Dees_Troy43d8b002012-09-17 16:00:01 -04001494
1495 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1496 Backup_FileName = back_name;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001497 Full_FileName = backup_folder + "/" + Backup_FileName;
Dees_Troy83bd4832013-05-04 12:39:56 +00001498 tar.has_data_media = Has_Data_Media;
1499 if (!use_encryption && Backup_Size > MAX_ARCHIVE_SIZE) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001500 // This backup needs to be split into multiple archives
Dees_Troy2673cec2013-04-02 20:22:16 +00001501 gui_print("Breaking backup file into multiple archives...\n");
Dees_Troye58d5262012-09-21 12:27:57 -04001502 sprintf(back_name, "%s", Backup_Path.c_str());
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -05001503 tar.setdir(back_name);
1504 tar.setfn(Full_FileName);
bigbiff bigbiffe6594ab2013-02-17 20:18:31 -05001505 backup_count = tar.splitArchiveFork();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001506 if (backup_count == -1) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001507 LOGERR("Error tarring split files!\n");
Dees_Troy4a2a1262012-09-18 09:33:47 -04001508 return false;
1509 }
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001510 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001511 } else {
Dees_Troy4a2a1262012-09-18 09:33:47 -04001512 Full_FileName = backup_folder + "/" + Backup_FileName;
Dees_Troy83bd4832013-05-04 12:39:56 +00001513 tar.setdir(Backup_Path);
1514 tar.setfn(Full_FileName);
1515 if (tar.createTarFork() != 0)
1516 return false;
1517 if (use_compression && !use_encryption) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001518 string gzname = Full_FileName + ".gz";
1519 rename(gzname.c_str(), Full_FileName.c_str());
1520 }
Dees_Troy83bd4832013-05-04 12:39:56 +00001521 if (use_encryption)
1522 Full_FileName += "000";
Dees_Troy7c2dec82012-09-26 09:49:14 -04001523 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001524 LOGERR("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001525 return false;
1526 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001527 }
1528 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001529}
1530
1531bool TWPartition::Backup_DD(string backup_folder) {
igoriok87e3d932013-01-31 21:03:53 +02001532 char back_name[255], backup_size[32];
1533 string Full_FileName, Command, result, DD_BS;
Dees_Troy43d8b002012-09-17 16:00:01 -04001534 int use_compression;
1535
igoriok87e3d932013-01-31 21:03:53 +02001536 sprintf(backup_size, "%llu", Backup_Size);
1537 DD_BS = backup_size;
1538
Dees_Troyb46a6842012-09-25 11:06:46 -04001539 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
Dees_Troy2673cec2013-04-02 20:22:16 +00001540 gui_print("Backing up %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001541
1542 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1543 Backup_FileName = back_name;
1544
1545 Full_FileName = backup_folder + "/" + Backup_FileName;
1546
igoriok87e3d932013-01-31 21:03:53 +02001547 Command = "dd if=" + Actual_Block_Device + " of='" + Full_FileName + "'" + " bs=" + DD_BS + "c count=1";
Dees_Troy2673cec2013-04-02 20:22:16 +00001548 LOGINFO("Backup command: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001549 TWFunc::Exec_Cmd(Command, result);
Dees_Troyc154ac22012-10-12 15:36:47 -04001550 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001551 LOGERR("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001552 return false;
1553 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001554 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001555}
1556
1557bool TWPartition::Backup_Dump_Image(string backup_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001558 char back_name[255];
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001559 string Full_FileName, Command, result;
Dees_Troy43d8b002012-09-17 16:00:01 -04001560 int use_compression;
1561
Dees_Troyb46a6842012-09-25 11:06:46 -04001562 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
Dees_Troy2673cec2013-04-02 20:22:16 +00001563 gui_print("Backing up %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001564
1565 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1566 Backup_FileName = back_name;
1567
1568 Full_FileName = backup_folder + "/" + Backup_FileName;
1569
1570 Command = "dump_image " + MTD_Name + " '" + Full_FileName + "'";
Dees_Troy2673cec2013-04-02 20:22:16 +00001571 LOGINFO("Backup command: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001572 TWFunc::Exec_Cmd(Command, result);
Dees_Troy7c2dec82012-09-26 09:49:14 -04001573 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
1574 // 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 +00001575 LOGERR("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001576 return false;
1577 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001578 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001579}
1580
Gary Peck43acadf2012-11-21 21:19:01 -08001581bool TWPartition::Restore_Tar(string restore_folder, string Restore_File_System) {
1582 string Full_FileName, Command;
Dees_Troy4a2a1262012-09-18 09:33:47 -04001583 int index = 0;
1584 char split_index[5];
Dees_Troy43d8b002012-09-17 16:00:01 -04001585
Dees_Troye58d5262012-09-21 12:27:57 -04001586 if (Has_Android_Secure) {
Dees_Troye58d5262012-09-21 12:27:57 -04001587 if (!Wipe_AndSec())
1588 return false;
Gary Peck43acadf2012-11-21 21:19:01 -08001589 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001590 gui_print("Wiping %s...\n", Display_Name.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001591 if (!Wipe(Restore_File_System))
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001592 return false;
Dees_Troye58d5262012-09-21 12:27:57 -04001593 }
Dees_Troya13d74f2013-03-24 08:54:55 -05001594 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Backup_Display_Name, "Restoring");
Dees_Troy2673cec2013-04-02 20:22:16 +00001595 gui_print("Restoring %s...\n", Backup_Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001596
1597 if (!Mount(true))
1598 return false;
1599
Dees_Troy4a2a1262012-09-18 09:33:47 -04001600 Full_FileName = restore_folder + "/" + Backup_FileName;
Dees_Troy83bd4832013-05-04 12:39:56 +00001601 /*if (!TWFunc::Path_Exists(Full_FileName)) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001602 if (!TWFunc::Path_Exists(Full_FileName)) {
1603 // Backup is multiple archives
Dees_Troy2673cec2013-04-02 20:22:16 +00001604 LOGINFO("Backup is multiple archives.\n");
Dees_Troy4a2a1262012-09-18 09:33:47 -04001605 sprintf(split_index, "%03i", index);
1606 Full_FileName = restore_folder + "/" + Backup_FileName + split_index;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001607 while (TWFunc::Path_Exists(Full_FileName)) {
1608 index++;
Dees_Troy2673cec2013-04-02 20:22:16 +00001609 gui_print("Restoring archive %i...\n", index);
1610 LOGINFO("Restoring '%s'...\n", Full_FileName.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001611 twrpTar tar;
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -05001612 tar.setdir("/");
1613 tar.setfn(Full_FileName);
bigbiff bigbiffe6594ab2013-02-17 20:18:31 -05001614 if (tar.extractTarFork() != 0)
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001615 return false;
1616 sprintf(split_index, "%03i", index);
1617 Full_FileName = restore_folder + "/" + Backup_FileName + split_index;
1618 }
1619 if (index == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001620 LOGERR("Error locating restore file: '%s'\n", Full_FileName.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001621 return false;
1622 }
Dees_Troy4a2a1262012-09-18 09:33:47 -04001623 }
Dees_Troy83bd4832013-05-04 12:39:56 +00001624 } else {*/
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001625 twrpTar tar;
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -05001626 tar.setdir(Backup_Path);
1627 tar.setfn(Full_FileName);
Dees_Troy83bd4832013-05-04 12:39:56 +00001628 tar.backup_name = Backup_Name;
bigbiff bigbiffe6594ab2013-02-17 20:18:31 -05001629 if (tar.extractTarFork() != 0)
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001630 return false;
Dees_Troy83bd4832013-05-04 12:39:56 +00001631 //}
Dees_Troy43d8b002012-09-17 16:00:01 -04001632 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001633}
1634
1635bool TWPartition::Restore_DD(string restore_folder) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001636 string Full_FileName, Command, result;
Dees_Troy43d8b002012-09-17 16:00:01 -04001637
Dees_Troyda8b55a2012-12-12 19:18:30 +00001638 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
Dees_Troy43d8b002012-09-17 16:00:01 -04001639 Full_FileName = restore_folder + "/" + Backup_FileName;
Gary Peck15e623d2012-11-21 21:07:58 -08001640
1641 if (!Find_Partition_Size()) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001642 LOGERR("Unable to find partition size for '%s'\n", Mount_Point.c_str());
Gary Peck15e623d2012-11-21 21:07:58 -08001643 return false;
1644 }
1645 unsigned long long backup_size = TWFunc::Get_File_Size(Full_FileName);
1646 if (backup_size > Size) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001647 LOGERR("Size (%iMB) of backup '%s' is larger than target device '%s' (%iMB)\n",
Gary Peck15e623d2012-11-21 21:07:58 -08001648 (int)(backup_size / 1048576LLU), Full_FileName.c_str(),
1649 Actual_Block_Device.c_str(), (int)(Size / 1048576LLU));
1650 return false;
1651 }
1652
Dees_Troy2673cec2013-04-02 20:22:16 +00001653 gui_print("Restoring %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001654 Command = "dd bs=4096 if='" + Full_FileName + "' of=" + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001655 LOGINFO("Restore command: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001656 TWFunc::Exec_Cmd(Command, result);
Dees_Troy43d8b002012-09-17 16:00:01 -04001657 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001658}
1659
1660bool TWPartition::Restore_Flash_Image(string restore_folder) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001661 string Full_FileName, Command, result;
Dees_Troy43d8b002012-09-17 16:00:01 -04001662
Dees_Troy2673cec2013-04-02 20:22:16 +00001663 gui_print("Restoring %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001664 Full_FileName = restore_folder + "/" + Backup_FileName;
1665 // Sometimes flash image doesn't like to flash due to the first 2KB matching, so we erase first to ensure that it flashes
1666 Command = "erase_image " + MTD_Name;
Dees_Troy2673cec2013-04-02 20:22:16 +00001667 LOGINFO("Erase command: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001668 TWFunc::Exec_Cmd(Command, result);
Dees_Troy43d8b002012-09-17 16:00:01 -04001669 Command = "flash_image " + MTD_Name + " '" + Full_FileName + "'";
Dees_Troy2673cec2013-04-02 20:22:16 +00001670 LOGINFO("Restore command: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001671 TWFunc::Exec_Cmd(Command, result);
Dees_Troy43d8b002012-09-17 16:00:01 -04001672 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001673}
Dees_Troy5bf43922012-09-07 16:07:55 -04001674
1675bool TWPartition::Update_Size(bool Display_Error) {
Dees_Troy0550cfb2012-10-13 11:56:13 -04001676 bool ret = false, Was_Already_Mounted = false;
Dees_Troy51127312012-09-08 13:08:49 -04001677
Dees_Troyab10ee22012-09-21 14:27:30 -04001678 if (!Can_Be_Mounted && !Is_Encrypted)
Dees_Troy5bf43922012-09-07 16:07:55 -04001679 return false;
1680
Dees_Troy0550cfb2012-10-13 11:56:13 -04001681 Was_Already_Mounted = Is_Mounted();
Dees_Troy38bd7602012-09-14 13:33:53 -04001682 if (Removable || Is_Encrypted) {
1683 if (!Mount(false))
1684 return true;
1685 } else if (!Mount(Display_Error))
Dees_Troy5bf43922012-09-07 16:07:55 -04001686 return false;
Dees_Troy51127312012-09-08 13:08:49 -04001687
1688 ret = Get_Size_Via_statfs(Display_Error);
Dees_Troy0550cfb2012-10-13 11:56:13 -04001689 if (!ret || Size == 0) {
1690 if (!Get_Size_Via_df(Display_Error)) {
1691 if (!Was_Already_Mounted)
1692 UnMount(false);
Dees_Troy51127312012-09-08 13:08:49 -04001693 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04001694 }
1695 }
Dees_Troy51127312012-09-08 13:08:49 -04001696
Dees_Troy5bf43922012-09-07 16:07:55 -04001697 if (Has_Data_Media) {
1698 if (Mount(Display_Error)) {
Dees_Troy51127312012-09-08 13:08:49 -04001699 unsigned long long data_media_used, actual_data;
Dees_Troy43d8b002012-09-17 16:00:01 -04001700 Used = TWFunc::Get_Folder_Size("/data", Display_Error);
1701 data_media_used = TWFunc::Get_Folder_Size("/data/media", Display_Error);
Dees_Troy51127312012-09-08 13:08:49 -04001702 actual_data = Used - data_media_used;
Dees_Troy5bf43922012-09-07 16:07:55 -04001703 Backup_Size = actual_data;
Dees_Troy51127312012-09-08 13:08:49 -04001704 int bak = (int)(Backup_Size / 1048576LLU);
1705 int total = (int)(Size / 1048576LLU);
1706 int us = (int)(Used / 1048576LLU);
1707 int fre = (int)(Free / 1048576LLU);
1708 int datmed = (int)(data_media_used / 1048576LLU);
Dees_Troy2673cec2013-04-02 20:22:16 +00001709 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 -04001710 } else {
1711 if (!Was_Already_Mounted)
1712 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04001713 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04001714 }
Dees_Troye58d5262012-09-21 12:27:57 -04001715 } else if (Has_Android_Secure) {
1716 if (Mount(Display_Error))
1717 Backup_Size = TWFunc::Get_Folder_Size(Backup_Path, Display_Error);
Dees_Troy0550cfb2012-10-13 11:56:13 -04001718 else {
1719 if (!Was_Already_Mounted)
1720 UnMount(false);
Dees_Troye58d5262012-09-21 12:27:57 -04001721 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04001722 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001723 }
Dees_Troy0550cfb2012-10-13 11:56:13 -04001724 if (!Was_Already_Mounted)
1725 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04001726 return true;
Dees_Troy51127312012-09-08 13:08:49 -04001727}
Dees_Troy38bd7602012-09-14 13:33:53 -04001728
1729void TWPartition::Find_Actual_Block_Device(void) {
1730 if (Is_Decrypted) {
1731 Actual_Block_Device = Decrypted_Block_Device;
Dees_Troy43d8b002012-09-17 16:00:01 -04001732 if (TWFunc::Path_Exists(Primary_Block_Device))
Dees_Troy38bd7602012-09-14 13:33:53 -04001733 Is_Present = true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001734 } else if (TWFunc::Path_Exists(Primary_Block_Device)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001735 Is_Present = true;
1736 Actual_Block_Device = Primary_Block_Device;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001737 return;
1738 }
1739 if (Is_Decrypted) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001740 } else if (!Alternate_Block_Device.empty() && TWFunc::Path_Exists(Alternate_Block_Device)) {
Dees_Troy3f04d032012-10-07 18:20:09 -04001741 Actual_Block_Device = Alternate_Block_Device;
Dees_Troy38bd7602012-09-14 13:33:53 -04001742 Is_Present = true;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001743 } else {
Dees_Troy38bd7602012-09-14 13:33:53 -04001744 Is_Present = false;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001745 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001746}
1747
1748void TWPartition::Recreate_Media_Folder(void) {
1749 string Command;
1750
1751 if (!Mount(true)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001752 LOGERR("Unable to recreate /data/media folder.\n");
Dees_Troyb46a6842012-09-25 11:06:46 -04001753 } else if (!TWFunc::Path_Exists("/data/media")) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001754 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Dees_Troy2673cec2013-04-02 20:22:16 +00001755 LOGINFO("Recreating /data/media folder.\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001756 mkdir("/data/media", S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
1757 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Dees_Troy38bd7602012-09-14 13:33:53 -04001758 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001759}
Dees_Troye58d5262012-09-21 12:27:57 -04001760
1761void TWPartition::Recreate_AndSec_Folder(void) {
Dees_Troye58d5262012-09-21 12:27:57 -04001762 if (!Has_Android_Secure)
1763 return;
Dees_Troy2673cec2013-04-02 20:22:16 +00001764 LOGINFO("Creating %s: %s\n", Backup_Display_Name.c_str(), Symlink_Path.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -04001765 if (!Mount(true)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001766 LOGERR("Unable to recreate %s folder.\n", Backup_Name.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -04001767 } else if (!TWFunc::Path_Exists(Symlink_Path)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001768 LOGINFO("Recreating %s folder.\n", Backup_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001769 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
1770 mkdir(Symlink_Path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
1771 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04001772 }
1773}