blob: e02def032a1248752d03e6e87c20d43b7b89c274 [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_Troy5bf43922012-09-07 16:07:55 -0400503 File_System == "auto")
504 return true;
505 else
506 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400507}
508
Dees_Troy5bf43922012-09-07 16:07:55 -0400509bool TWPartition::Is_Image(string File_System) {
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400510 if (File_System == "emmc" || File_System == "mtd" || File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -0400511 return true;
512 else
513 return false;
514}
515
Dees_Troy51127312012-09-08 13:08:49 -0400516bool TWPartition::Make_Dir(string Path, bool Display_Error) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400517 if (!TWFunc::Path_Exists(Path)) {
Dees_Troy51127312012-09-08 13:08:49 -0400518 if (mkdir(Path.c_str(), 0777) == -1) {
519 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000520 LOGERR("Can not create '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400521 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000522 LOGINFO("Can not create '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400523 return false;
524 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000525 LOGINFO("Created '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400526 return true;
527 }
528 }
529 return true;
530}
531
Dees_Troy5bf43922012-09-07 16:07:55 -0400532void TWPartition::Setup_File_System(bool Display_Error) {
533 struct statfs st;
534
535 Can_Be_Mounted = true;
536 Can_Be_Wiped = true;
537
Dees_Troy5bf43922012-09-07 16:07:55 -0400538 // Make the mount point folder if it doesn't exist
Dees_Troy51127312012-09-08 13:08:49 -0400539 Make_Dir(Mount_Point, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400540 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
541 Backup_Name = Display_Name;
542 Backup_Method = FILES;
543}
544
545void TWPartition::Setup_Image(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400546 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
547 Backup_Name = Display_Name;
Gary Peck82599a82012-11-21 16:23:12 -0800548 if (Current_File_System == "emmc")
Dees_Troy5bf43922012-09-07 16:07:55 -0400549 Backup_Method = DD;
Gary Peck82599a82012-11-21 16:23:12 -0800550 else if (Current_File_System == "mtd" || Current_File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -0400551 Backup_Method = FLASH_UTILS;
552 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000553 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 -0400554 if (Find_Partition_Size()) {
555 Used = Size;
556 Backup_Size = Size;
Dees_Troy51a0e822012-09-05 15:24:24 -0400557 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400558 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000559 LOGERR("Unable to find parition size for '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400560 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000561 LOGINFO("Unable to find parition size for '%s'\n", Mount_Point.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400562 }
563}
564
Dees_Troye58d5262012-09-21 12:27:57 -0400565void TWPartition::Setup_AndSec(void) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500566 Backup_Display_Name = "Android Secure";
Dees_Troye58d5262012-09-21 12:27:57 -0400567 Backup_Name = "and-sec";
Dees_Troya13d74f2013-03-24 08:54:55 -0500568 Can_Be_Backed_Up = true;
Dees_Troye58d5262012-09-21 12:27:57 -0400569 Has_Android_Secure = true;
570 Symlink_Path = Mount_Point + "/.android_secure";
571 Symlink_Mount_Point = "/and-sec";
572 Backup_Path = Symlink_Mount_Point;
573 Make_Dir("/and-sec", true);
574 Recreate_AndSec_Folder();
575}
576
Dees_Troy5bf43922012-09-07 16:07:55 -0400577void TWPartition::Find_Real_Block_Device(string& Block, bool Display_Error) {
578 char device[512], realDevice[512];
579
580 strcpy(device, Block.c_str());
581 memset(realDevice, 0, sizeof(realDevice));
582 while (readlink(device, realDevice, sizeof(realDevice)) > 0)
583 {
584 strcpy(device, realDevice);
585 memset(realDevice, 0, sizeof(realDevice));
586 }
587
588 if (device[0] != '/') {
589 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000590 LOGERR("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400591 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000592 LOGINFO("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400593 return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400594 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400595 Block = device;
596 return;
597 }
598}
599
Dees_Troy8e337f32012-10-13 22:07:49 -0400600void TWPartition::Mount_Storage_Retry(void) {
601 // On some devices, storage doesn't want to mount right away, retry and sleep
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500602 if (!Mount(true)) {
Dees_Troy8e337f32012-10-13 22:07:49 -0400603 int retry_count = 5;
604 while (retry_count > 0 && !Mount(false)) {
605 usleep(500000);
606 retry_count--;
607 }
608 Mount(true);
609 }
610}
611
Dees_Troy38bd7602012-09-14 13:33:53 -0400612bool TWPartition::Find_MTD_Block_Device(string MTD_Name) {
613 FILE *fp = NULL;
614 char line[255];
615
616 fp = fopen("/proc/mtd", "rt");
617 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000618 LOGERR("Device does not support /proc/mtd\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400619 return false;
620 }
621
622 while (fgets(line, sizeof(line), fp) != NULL)
623 {
624 char device[32], label[32];
625 unsigned long size = 0;
626 char* fstype = NULL;
627 int deviceId;
628
629 sscanf(line, "%s %lx %*s %*c%s", device, &size, label);
630
631 // Skip header and blank lines
632 if ((strcmp(device, "dev:") == 0) || (strlen(line) < 8))
633 continue;
634
635 // Strip off the trailing " from the label
636 label[strlen(label)-1] = '\0';
637
638 if (strcmp(label, MTD_Name.c_str()) == 0) {
639 // We found our device
640 // Strip off the trailing : from the device
641 device[strlen(device)-1] = '\0';
642 if (sscanf(device,"mtd%d", &deviceId) == 1) {
643 sprintf(device, "/dev/block/mtdblock%d", deviceId);
644 Primary_Block_Device = device;
Dees_Troy76543db2013-06-19 16:24:30 +0000645 fclose(fp);
646 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -0400647 }
648 }
649 }
650 fclose(fp);
651
652 return false;
653}
654
Dees_Troy51127312012-09-08 13:08:49 -0400655bool TWPartition::Get_Size_Via_statfs(bool Display_Error) {
656 struct statfs st;
657 string Local_Path = Mount_Point + "/.";
658
659 if (!Mount(Display_Error))
660 return false;
661
662 if (statfs(Local_Path.c_str(), &st) != 0) {
663 if (!Removable) {
664 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000665 LOGERR("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400666 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000667 LOGINFO("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400668 }
669 return false;
670 }
671 Size = (st.f_blocks * st.f_bsize);
672 Used = ((st.f_blocks - st.f_bfree) * st.f_bsize);
673 Free = (st.f_bfree * st.f_bsize);
674 Backup_Size = Used;
675 return true;
676}
677
678bool TWPartition::Get_Size_Via_df(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400679 FILE* fp;
680 char command[255], line[512];
681 int include_block = 1;
682 unsigned int min_len;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500683 string result;
Dees_Troy5bf43922012-09-07 16:07:55 -0400684
685 if (!Mount(Display_Error))
686 return false;
687
Dees_Troy38bd7602012-09-14 13:33:53 -0400688 min_len = Actual_Block_Device.size() + 2;
Dees_Troy51127312012-09-08 13:08:49 -0400689 sprintf(command, "df %s > /tmp/dfoutput.txt", Mount_Point.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500690 TWFunc::Exec_Cmd(command, result);
Dees_Troy51127312012-09-08 13:08:49 -0400691 fp = fopen("/tmp/dfoutput.txt", "rt");
692 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000693 LOGINFO("Unable to open /tmp/dfoutput.txt.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400694 return false;
Dees_Troy51127312012-09-08 13:08:49 -0400695 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400696
697 while (fgets(line, sizeof(line), fp) != NULL)
698 {
699 unsigned long blocks, used, available;
700 char device[64];
701 char tmpString[64];
702
703 if (strncmp(line, "Filesystem", 10) == 0)
704 continue;
705 if (strlen(line) < min_len) {
706 include_block = 0;
707 continue;
708 }
709 if (include_block) {
710 sscanf(line, "%s %lu %lu %lu", device, &blocks, &used, &available);
711 } else {
712 // The device block string is so long that the df information is on the next line
713 int space_count = 0;
Dees_Troye58d5262012-09-21 12:27:57 -0400714 sprintf(tmpString, "/dev/block/%s", Actual_Block_Device.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400715 while (tmpString[space_count] == 32)
716 space_count++;
717 sscanf(line + space_count, "%lu %lu %lu", &blocks, &used, &available);
718 }
719
720 // Adjust block size to byte size
721 Size = blocks * 1024ULL;
722 Used = used * 1024ULL;
723 Free = available * 1024ULL;
724 Backup_Size = Used;
725 }
726 fclose(fp);
727 return true;
728}
729
Dees_Troy5bf43922012-09-07 16:07:55 -0400730bool TWPartition::Find_Partition_Size(void) {
731 FILE* fp;
732 char line[512];
733 string tmpdevice;
734
igoriok87e3d932013-01-31 21:03:53 +0200735 fp = fopen("/proc/dumchar_info", "rt");
736 if (fp != NULL) {
737 while (fgets(line, sizeof(line), fp) != NULL)
738 {
739 char label[32], device[32];
740 unsigned long size = 0;
741
742 sscanf(line, "%s %lx %*lx %*lu %s", label, &size, device);
743
744 // Skip header, annotation and blank lines
745 if ((strncmp(device, "/dev/", 5) != 0) || (strlen(line) < 8))
746 continue;
747
748 tmpdevice = "/dev/";
749 tmpdevice += label;
750 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
751 Size = size;
752 fclose(fp);
753 return true;
754 }
755 }
756 }
757
Dees_Troy5bf43922012-09-07 16:07:55 -0400758 // In this case, we'll first get the partitions we care about (with labels)
759 fp = fopen("/proc/partitions", "rt");
760 if (fp == NULL)
761 return false;
762
763 while (fgets(line, sizeof(line), fp) != NULL)
764 {
765 unsigned long major, minor, blocks;
766 char device[512];
767 char tmpString[64];
768
Dees_Troy63c8df72012-09-10 14:02:05 -0400769 if (strlen(line) < 7 || line[0] == 'm') continue;
Dees_Troy5bf43922012-09-07 16:07:55 -0400770 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
771
772 tmpdevice = "/dev/block/";
773 tmpdevice += device;
Dees_Troy38bd7602012-09-14 13:33:53 -0400774 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400775 // Adjust block size to byte size
776 Size = blocks * 1024ULL;
777 fclose(fp);
778 return true;
779 }
780 }
781 fclose(fp);
782 return false;
783}
784
Dees_Troy5bf43922012-09-07 16:07:55 -0400785bool TWPartition::Is_Mounted(void) {
786 if (!Can_Be_Mounted)
787 return false;
788
789 struct stat st1, st2;
790 string test_path;
791
792 // Check to see if the mount point directory exists
793 test_path = Mount_Point + "/.";
794 if (stat(test_path.c_str(), &st1) != 0) return false;
795
796 // Check to see if the directory above the mount point exists
797 test_path = Mount_Point + "/../.";
798 if (stat(test_path.c_str(), &st2) != 0) return false;
799
800 // Compare the device IDs -- if they match then we're (probably) using tmpfs instead of an actual device
801 int ret = (st1.st_dev != st2.st_dev) ? true : false;
802
803 return ret;
804}
805
806bool TWPartition::Mount(bool Display_Error) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000807 int exfat_mounted = 0;
808
Dees_Troy5bf43922012-09-07 16:07:55 -0400809 if (Is_Mounted()) {
810 return true;
811 } else if (!Can_Be_Mounted) {
812 return false;
813 }
Dees_Troy38bd7602012-09-14 13:33:53 -0400814
815 Find_Actual_Block_Device();
816
817 // Check the current file system before mounting
818 Check_FS_Type();
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000819 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/exfat-fuse")) {
Dees_Troye34c1332013-02-06 19:13:00 +0000820 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 +0000821 LOGINFO("cmd: %s\n", cmd.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000822 string result;
823 if (TWFunc::Exec_Cmd(cmd, result) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000824 LOGINFO("exfat-fuse failed to mount with result '%s', trying vfat\n", result.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000825 Current_File_System = "vfat";
826 } else {
827#ifdef TW_NO_EXFAT_FUSE
828 UnMount(false);
829 // We'll let the kernel handle it but using exfat-fuse to detect if the file system is actually exfat
830 // Some kernels let us mount vfat as exfat which doesn't work out too well
831#else
832 exfat_mounted = 1;
833#endif
834 }
835 }
Dees_Troy22042032012-12-18 21:23:08 +0000836 if (Fstab_File_System == "yaffs2") {
837 // mount an MTD partition as a YAFFS2 filesystem.
Dees_Troy76543db2013-06-19 16:24:30 +0000838 const unsigned long flags = MS_NOATIME | MS_NODEV | MS_NODIRATIME;
839 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags, NULL) < 0) {
840 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags | MS_RDONLY, NULL) < 0) {
841 if (Display_Error)
842 LOGERR("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
843 else
844 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
845 return false;
846 } else {
847 LOGINFO("Mounted '%s' (MTD) as RO\n", Mount_Point.c_str());
848 return true;
849 }
850 } else {
851 struct stat st;
852 string test_path = Mount_Point;
853 if (stat(test_path.c_str(), &st) < 0) {
854 if (Display_Error)
855 LOGERR("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
856 else
857 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
858 return false;
859 }
860 mode_t new_mode = st.st_mode | S_IXUSR | S_IXGRP | S_IXOTH;
861 if (new_mode != st.st_mode) {
862 LOGINFO("Fixing execute permissions for %s\n", Mount_Point.c_str());
863 if (chmod(Mount_Point.c_str(), new_mode) < 0) {
864 if (Display_Error)
865 LOGERR("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
866 else
867 LOGINFO("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
868 return false;
869 }
870 }
Dees_Troy22042032012-12-18 21:23:08 +0000871 return true;
Dees_Troy76543db2013-06-19 16:24:30 +0000872 }
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000873 } else if (!exfat_mounted && mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Current_File_System.c_str(), 0, NULL) != 0) {
874#ifdef TW_NO_EXFAT_FUSE
875 if (Current_File_System == "exfat") {
Dees_Troy2673cec2013-04-02 20:22:16 +0000876 LOGINFO("Mounting exfat failed, trying vfat...\n");
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000877 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), "vfat", 0, NULL) != 0) {
Dees_Troy85f44ed2013-01-09 18:42:36 +0000878 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000879 LOGERR("Unable to mount '%s'\n", Mount_Point.c_str());
Dees_Troy85f44ed2013-01-09 18:42:36 +0000880 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000881 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
882 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 +0000883 return false;
Dees_Troy85f44ed2013-01-09 18:42:36 +0000884 }
Dees_Troyb05ddee2013-01-28 20:24:50 +0000885 } else {
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000886#endif
887 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000888 LOGERR("Unable to mount '%s'\n", Mount_Point.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000889 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000890 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
891 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 +0000892 return false;
893#ifdef TW_NO_EXFAT_FUSE
Dees_Troy85f44ed2013-01-09 18:42:36 +0000894 }
895#endif
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000896 }
897#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
898 string MetaEcfsFile = EXPAND(TW_EXTERNAL_STORAGE_PATH);
899 MetaEcfsFile += "/.MetaEcfsFile";
900 if (EcryptFS_Password.size() > 0 && PartitionManager.Mount_By_Path("/data", false) && TWFunc::Path_Exists(MetaEcfsFile)) {
901 if (mount_ecryptfs_drive(EcryptFS_Password.c_str(), Mount_Point.c_str(), Mount_Point.c_str(), 0) != 0) {
902 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000903 LOGERR("Unable to mount ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000904 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000905 LOGINFO("Unable to mount ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000906 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000907 LOGINFO("Successfully mounted ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000908 Is_Decrypted = true;
Dees_Troy51127312012-09-08 13:08:49 -0400909 }
Dees_Troy066eb302013-08-23 17:20:32 +0000910 } else if (Mount_Point == EXPAND(TW_EXTERNAL_STORAGE_PATH)) {
911 if (Is_Decrypted)
912 LOGINFO("Mounting external storage, '%s' is not encrypted\n", Mount_Point.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000913 Is_Decrypted = false;
914 }
915#endif
916 if (Removable)
917 Update_Size(Display_Error);
918
919 if (!Symlink_Mount_Point.empty()) {
920 string Command, Result;
921 Command = "mount '" + Symlink_Path + "' '" + Symlink_Mount_Point + "'";
922 TWFunc::Exec_Cmd(Command, Result);
Dees_Troy5bf43922012-09-07 16:07:55 -0400923 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400924 return true;
925}
926
927bool TWPartition::UnMount(bool Display_Error) {
928 if (Is_Mounted()) {
929 int never_unmount_system;
930
931 DataManager::GetValue(TW_DONT_UNMOUNT_SYSTEM, never_unmount_system);
932 if (never_unmount_system == 1 && Mount_Point == "/system")
933 return true; // Never unmount system if you're not supposed to unmount it
934
Dees_Troyc8bafa12013-01-10 15:43:00 +0000935#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
936 if (EcryptFS_Password.size() > 0) {
937 if (unmount_ecryptfs_drive(Mount_Point.c_str()) != 0) {
938 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000939 LOGERR("Unable to unmount ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troyc8bafa12013-01-10 15:43:00 +0000940 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000941 LOGINFO("Unable to unmount ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troyc8bafa12013-01-10 15:43:00 +0000942 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000943 LOGINFO("Successfully unmounted ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troyc8bafa12013-01-10 15:43:00 +0000944 }
945 }
946#endif
947
Dees_Troy38bd7602012-09-14 13:33:53 -0400948 if (!Symlink_Mount_Point.empty())
949 umount(Symlink_Mount_Point.c_str());
950
Dees_Troyb05ddee2013-01-28 20:24:50 +0000951 umount(Mount_Point.c_str());
952 if (Is_Mounted()) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400953 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000954 LOGERR("Unable to unmount '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400955 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000956 LOGINFO("Unable to unmount '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400957 return false;
958 } else
959 return true;
960 } else {
961 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400962 }
963}
964
Gary Peck43acadf2012-11-21 21:19:01 -0800965bool TWPartition::Wipe(string New_File_System) {
Dees_Troy16c2b312013-01-15 16:51:18 +0000966 bool wiped = false, update_crypt = false;
967 int check;
968 string Layout_Filename = Mount_Point + "/.layout_version";
969
Dees_Troy38bd7602012-09-14 13:33:53 -0400970 if (!Can_Be_Wiped) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000971 LOGERR("Partition '%s' cannot be wiped.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -0400972 return false;
973 }
974
Dees_Troyc51f1f92012-09-20 15:32:13 -0400975 if (Mount_Point == "/cache")
Dees_Troy2673cec2013-04-02 20:22:16 +0000976 Log_Offset = 0;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400977
Dees_Troyce675462013-01-09 19:48:21 +0000978#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
979 if (Mount_Point == "/data" && Mount(false)) {
980 if (TWFunc::Path_Exists("/data/system/edk_p_sd"))
981 TWFunc::copy_file("/data/system/edk_p_sd", "/tmp/edk_p_sd", 0600);
982 }
983#endif
984
Dees_Troy16c2b312013-01-15 16:51:18 +0000985 if (Retain_Layout_Version && Mount(false) && TWFunc::Path_Exists(Layout_Filename))
986 TWFunc::copy_file(Layout_Filename, "/.layout_version", 0600);
987 else
988 unlink("/.layout_version");
Dees_Troy38bd7602012-09-14 13:33:53 -0400989
Dees_Troy16c2b312013-01-15 16:51:18 +0000990 if (Has_Data_Media) {
991 wiped = Wipe_Data_Without_Wiping_Media();
992 } else {
Gary Pecke8bc5d72012-12-21 06:45:25 -0800993
Dees_Troy16c2b312013-01-15 16:51:18 +0000994 DataManager::GetValue(TW_RM_RF_VAR, check);
995
996 if (check)
997 wiped = Wipe_RMRF();
998 else if (New_File_System == "ext4")
999 wiped = Wipe_EXT4();
1000 else if (New_File_System == "ext2" || New_File_System == "ext3")
1001 wiped = Wipe_EXT23(New_File_System);
1002 else if (New_File_System == "vfat")
1003 wiped = Wipe_FAT();
1004 else if (New_File_System == "exfat")
1005 wiped = Wipe_EXFAT();
1006 else if (New_File_System == "yaffs2")
1007 wiped = Wipe_MTD();
1008 else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001009 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 +00001010 unlink("/.layout_version");
1011 return false;
1012 }
1013 update_crypt = wiped;
Gary Pecke8bc5d72012-12-21 06:45:25 -08001014 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001015
Gary Pecke8bc5d72012-12-21 06:45:25 -08001016 if (wiped) {
Dees_Troyce675462013-01-09 19:48:21 +00001017#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
1018 if (Mount_Point == "/data" && Mount(false)) {
1019 if (TWFunc::Path_Exists("/tmp/edk_p_sd")) {
1020 Make_Dir("/data/system", true);
1021 TWFunc::copy_file("/tmp/edk_p_sd", "/data/system/edk_p_sd", 0600);
1022 }
1023 }
1024#endif
Dees_Troy16c2b312013-01-15 16:51:18 +00001025
Dees_Troy1c1ac442013-01-17 21:42:14 +00001026 if (Mount_Point == "/cache")
1027 DataManager::Output_Version();
1028
Dees_Troy16c2b312013-01-15 16:51:18 +00001029 if (TWFunc::Path_Exists("/.layout_version") && Mount(false))
1030 TWFunc::copy_file("/.layout_version", Layout_Filename, 0600);
1031
1032 if (update_crypt) {
1033 Setup_File_System(false);
1034 if (Is_Encrypted && !Is_Decrypted) {
1035 // just wiped an encrypted partition back to its unencrypted state
1036 Is_Encrypted = false;
1037 Is_Decrypted = false;
1038 Decrypted_Block_Device = "";
1039 if (Mount_Point == "/data") {
1040 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1041 DataManager::SetValue(TW_IS_DECRYPTED, 0);
1042 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001043 }
1044 }
1045 }
1046 return wiped;
Dees_Troy51a0e822012-09-05 15:24:24 -04001047}
1048
Gary Peck43acadf2012-11-21 21:19:01 -08001049bool TWPartition::Wipe() {
Gary Peck82599a82012-11-21 16:23:12 -08001050 if (Is_File_System(Current_File_System))
1051 return Wipe(Current_File_System);
1052 else
1053 return Wipe(Fstab_File_System);
Gary Peck43acadf2012-11-21 21:19:01 -08001054}
1055
Dees_Troye58d5262012-09-21 12:27:57 -04001056bool TWPartition::Wipe_AndSec(void) {
1057 if (!Has_Android_Secure)
1058 return false;
1059
Dees_Troye58d5262012-09-21 12:27:57 -04001060 if (!Mount(true))
1061 return false;
1062
Dees_Troy2673cec2013-04-02 20:22:16 +00001063 gui_print("Wiping %s\n", Backup_Display_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001064 TWFunc::removeDir(Mount_Point + "/.android_secure/", true);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001065 return true;
Dees_Troye58d5262012-09-21 12:27:57 -04001066}
1067
Dees_Troy51a0e822012-09-05 15:24:24 -04001068bool TWPartition::Backup(string backup_folder) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001069 if (Backup_Method == FILES)
1070 return Backup_Tar(backup_folder);
1071 else if (Backup_Method == DD)
1072 return Backup_DD(backup_folder);
1073 else if (Backup_Method == FLASH_UTILS)
1074 return Backup_Dump_Image(backup_folder);
Dees_Troy2673cec2013-04-02 20:22:16 +00001075 LOGERR("Unknown backup method for '%s'\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001076 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001077}
1078
Dees_Troy43d8b002012-09-17 16:00:01 -04001079bool TWPartition::Check_MD5(string restore_folder) {
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001080 string Full_Filename, md5file;
Dees_Troy43d8b002012-09-17 16:00:01 -04001081 char split_filename[512];
1082 int index = 0;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001083 twrpDigest md5sum;
Dees_Troy43d8b002012-09-17 16:00:01 -04001084
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001085 memset(split_filename, 0, sizeof(split_filename));
Dees_Troy43d8b002012-09-17 16:00:01 -04001086 Full_Filename = restore_folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04001087 if (!TWFunc::Path_Exists(Full_Filename)) {
1088 // This is a split archive, we presume
1089 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
Dees_Troy2673cec2013-04-02 20:22:16 +00001090 LOGINFO("split_filename: %s\n", split_filename);
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001091 md5file = split_filename;
1092 md5file += ".md5";
1093 if (!TWFunc::Path_Exists(md5file)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001094 LOGERR("No md5 file found for '%s'.\n", split_filename);
1095 LOGERR("Please unselect Enable MD5 verification to restore.\n");
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001096 return false;
1097 }
1098 md5sum.setfn(split_filename);
Dees_Troy83bd4832013-05-04 12:39:56 +00001099 while (index < 1000) {
1100 if (TWFunc::Path_Exists(split_filename) && md5sum.verify_md5digest() != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001101 LOGERR("MD5 failed to match on '%s'.\n", split_filename);
Dees_Troy43d8b002012-09-17 16:00:01 -04001102 return false;
1103 }
1104 index++;
1105 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001106 md5sum.setfn(split_filename);
Dees_Troy43d8b002012-09-17 16:00:01 -04001107 }
Dees_Troy4a2a1262012-09-18 09:33:47 -04001108 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001109 } else {
1110 // Single file archive
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001111 md5file = Full_Filename + ".md5";
1112 if (!TWFunc::Path_Exists(md5file)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001113 LOGERR("No md5 file found for '%s'.\n", Full_Filename.c_str());
1114 LOGERR("Please unselect Enable MD5 verification to restore.\n");
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001115 return false;
1116 }
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001117 md5sum.setfn(Full_Filename);
1118 if (md5sum.verify_md5digest() != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001119 LOGERR("MD5 failed to match on '%s'.\n", Full_Filename.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001120 return false;
1121 } else
1122 return true;
1123 }
1124 return false;
1125}
1126
Dees_Troy51a0e822012-09-05 15:24:24 -04001127bool TWPartition::Restore(string restore_folder) {
Gary Peck43acadf2012-11-21 21:19:01 -08001128 size_t first_period, second_period;
1129 string Restore_File_System;
1130
1131 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
Dees_Troy2673cec2013-04-02 20:22:16 +00001132 LOGINFO("Restore filename is: %s\n", Backup_FileName.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001133
1134 // Parse backup filename to extract the file system before wiping
1135 first_period = Backup_FileName.find(".");
1136 if (first_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001137 LOGERR("Unable to find file system (first period).\n");
Gary Peck43acadf2012-11-21 21:19:01 -08001138 return false;
1139 }
1140 Restore_File_System = Backup_FileName.substr(first_period + 1, Backup_FileName.size() - first_period - 1);
1141 second_period = Restore_File_System.find(".");
1142 if (second_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001143 LOGERR("Unable to find file system (second period).\n");
Gary Peck43acadf2012-11-21 21:19:01 -08001144 return false;
1145 }
1146 Restore_File_System.resize(second_period);
Dees_Troy2673cec2013-04-02 20:22:16 +00001147 LOGINFO("Restore file system is: '%s'.\n", Restore_File_System.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001148
1149 if (Is_File_System(Restore_File_System))
1150 return Restore_Tar(restore_folder, Restore_File_System);
1151 else if (Is_Image(Restore_File_System)) {
1152 if (Restore_File_System == "emmc")
1153 return Restore_DD(restore_folder);
1154 else if (Restore_File_System == "mtd" || Restore_File_System == "bml")
1155 return Restore_Flash_Image(restore_folder);
1156 }
1157
Dees_Troy2673cec2013-04-02 20:22:16 +00001158 LOGERR("Unknown restore method for '%s'\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001159 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001160}
1161
1162string TWPartition::Backup_Method_By_Name() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001163 if (Backup_Method == NONE)
1164 return "none";
1165 else if (Backup_Method == FILES)
1166 return "files";
1167 else if (Backup_Method == DD)
1168 return "dd";
1169 else if (Backup_Method == FLASH_UTILS)
1170 return "flash_utils";
1171 else
1172 return "undefined";
1173 return "ERROR!";
Dees_Troy51a0e822012-09-05 15:24:24 -04001174}
1175
1176bool TWPartition::Decrypt(string Password) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001177 LOGINFO("STUB TWPartition::Decrypt, password: '%s'\n", Password.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001178 // Is this needed?
Dees_Troy51a0e822012-09-05 15:24:24 -04001179 return 1;
1180}
1181
1182bool TWPartition::Wipe_Encryption() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001183 bool Save_Data_Media = Has_Data_Media;
1184
1185 if (!UnMount(true))
1186 return false;
1187
Dees_Troy38bd7602012-09-14 13:33:53 -04001188 Has_Data_Media = false;
Dees_Troy74fb2e92013-04-15 14:35:47 +00001189 Decrypted_Block_Device = "";
1190 Is_Decrypted = false;
1191 Is_Encrypted = false;
Gary Pecke8bc5d72012-12-21 06:45:25 -08001192 if (Wipe(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001193 Has_Data_Media = Save_Data_Media;
1194 if (Has_Data_Media && !Symlink_Mount_Point.empty()) {
1195 Recreate_Media_Folder();
1196 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001197 gui_print("You may need to reboot recovery to be able to use /data again.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001198 return true;
1199 } else {
1200 Has_Data_Media = Save_Data_Media;
Dees_Troy2673cec2013-04-02 20:22:16 +00001201 LOGERR("Unable to format to remove encryption.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001202 return false;
1203 }
1204 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001205}
1206
1207void TWPartition::Check_FS_Type() {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001208 const char* type;
1209 blkid_probe pr;
Dees_Troy5bf43922012-09-07 16:07:55 -04001210
Dees_Troy68cab492012-12-12 19:29:35 +00001211 if (Fstab_File_System == "yaffs2" || Fstab_File_System == "mtd" || Fstab_File_System == "bml" || Ignore_Blkid)
1212 return; // Running blkid on some mtd devices causes a massive crash or needs to be skipped
Dees_Troy5bf43922012-09-07 16:07:55 -04001213
Dees_Troy38bd7602012-09-14 13:33:53 -04001214 Find_Actual_Block_Device();
Dees_Troy8170a922012-09-18 15:40:25 -04001215 if (!Is_Present)
1216 return;
Dees_Troy51127312012-09-08 13:08:49 -04001217
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001218 pr = blkid_new_probe_from_filename(Actual_Block_Device.c_str());
1219 if (blkid_do_fullprobe(pr)) {
1220 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00001221 LOGINFO("Can't probe device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001222 return;
Dees_Troy5bf43922012-09-07 16:07:55 -04001223 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001224
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001225 if (blkid_probe_lookup_value(pr, "TYPE", &type, NULL) < 0) {
1226 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00001227 LOGINFO("can't find filesystem on device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001228 return;
1229 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001230
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001231 Current_File_System = type;
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001232 blkid_free_probe(pr);
Dees_Troy51a0e822012-09-05 15:24:24 -04001233}
1234
Gary Peck43acadf2012-11-21 21:19:01 -08001235bool TWPartition::Wipe_EXT23(string File_System) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001236 if (!UnMount(true))
1237 return false;
1238
Dees_Troy43d8b002012-09-17 16:00:01 -04001239 if (TWFunc::Path_Exists("/sbin/mke2fs")) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001240 string command, result;
Dees_Troy38bd7602012-09-14 13:33:53 -04001241
Dees_Troy2673cec2013-04-02 20:22:16 +00001242 gui_print("Formatting %s using mke2fs...\n", Display_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001243 Find_Actual_Block_Device();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001244 command = "mke2fs -t " + File_System + " -m 0 " + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001245 LOGINFO("mke2fs command: %s\n", command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001246 if (TWFunc::Exec_Cmd(command, result) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001247 Current_File_System = File_System;
Dees_Troye58d5262012-09-21 12:27:57 -04001248 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001249 gui_print("Done.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001250 return true;
1251 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001252 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001253 return false;
1254 }
1255 } else
1256 return Wipe_RMRF();
1257
1258 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001259}
1260
1261bool TWPartition::Wipe_EXT4() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001262 if (!UnMount(true))
1263 return false;
1264
Dees_Troya95f55c2013-08-17 13:14:43 +00001265#ifdef USE_EXT4
1266 gui_print("Formatting %s using make_ext4fs function.\n", Display_Name.c_str());
1267#ifdef HAVE_SELINUX
1268 if (make_ext4fs(Actual_Block_Device.c_str(), Length, Mount_Point.c_str(), selinux_handle) != 0) {
1269#else
1270 if (make_ext4fs(Actual_Block_Device.c_str(), Length) != 0) {
1271#endif
1272 LOGERR("Unable to wipe '%s' using function call.\n", Mount_Point.c_str());
1273 return false;
1274 } else {
1275 return true;
1276 }
1277#else
Dees_Troy43d8b002012-09-17 16:00:01 -04001278 if (TWFunc::Path_Exists("/sbin/make_ext4fs")) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001279 string Command, result;
Dees_Troy38bd7602012-09-14 13:33:53 -04001280
Dees_Troy2673cec2013-04-02 20:22:16 +00001281 gui_print("Formatting %s using make_ext4fs...\n", Display_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001282 Find_Actual_Block_Device();
1283 Command = "make_ext4fs";
1284 if (!Is_Decrypted && Length != 0) {
1285 // Only use length if we're not decrypted
1286 char len[32];
1287 sprintf(len, "%i", Length);
1288 Command += " -l ";
1289 Command += len;
1290 }
1291 Command += " " + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001292 LOGINFO("make_ext4fs command: %s\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001293 if (TWFunc::Exec_Cmd(Command, result) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001294 Current_File_System = "ext4";
Dees_Troye58d5262012-09-21 12:27:57 -04001295 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001296 gui_print("Done.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001297 return true;
1298 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001299 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001300 return false;
1301 }
1302 } else
Gary Peck43acadf2012-11-21 21:19:01 -08001303 return Wipe_EXT23("ext4");
Dees_Troya95f55c2013-08-17 13:14:43 +00001304#endif
Dees_Troy38bd7602012-09-14 13:33:53 -04001305 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001306}
1307
1308bool TWPartition::Wipe_FAT() {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001309 string command, result;
Dees_Troy38bd7602012-09-14 13:33:53 -04001310
Dees_Troy43d8b002012-09-17 16:00:01 -04001311 if (TWFunc::Path_Exists("/sbin/mkdosfs")) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001312 if (!UnMount(true))
1313 return false;
1314
Dees_Troy2673cec2013-04-02 20:22:16 +00001315 gui_print("Formatting %s using mkdosfs...\n", Display_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001316 Find_Actual_Block_Device();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001317 command = "mkdosfs " + Actual_Block_Device;
1318 if (TWFunc::Exec_Cmd(command, result) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001319 Current_File_System = "vfat";
Dees_Troye58d5262012-09-21 12:27:57 -04001320 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001321 gui_print("Done.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001322 return true;
1323 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001324 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001325 return false;
1326 }
1327 return true;
1328 }
1329 else
1330 return Wipe_RMRF();
1331
1332 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001333}
1334
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001335bool TWPartition::Wipe_EXFAT() {
1336 string command, result;
1337
1338 if (TWFunc::Path_Exists("/sbin/mkexfatfs")) {
1339 if (!UnMount(true))
1340 return false;
1341
Dees_Troy2673cec2013-04-02 20:22:16 +00001342 gui_print("Formatting %s using mkexfatfs...\n", Display_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001343 Find_Actual_Block_Device();
1344 command = "mkexfatfs " + Actual_Block_Device;
1345 if (TWFunc::Exec_Cmd(command, result) == 0) {
1346 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001347 gui_print("Done.\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001348 return true;
1349 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001350 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001351 return false;
1352 }
1353 return true;
1354 }
1355 return false;
1356}
1357
Dees_Troy38bd7602012-09-14 13:33:53 -04001358bool TWPartition::Wipe_MTD() {
1359 if (!UnMount(true))
1360 return false;
1361
Dees_Troy2673cec2013-04-02 20:22:16 +00001362 gui_print("MTD Formatting \"%s\"\n", MTD_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001363
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001364 mtd_scan_partitions();
1365 const MtdPartition* mtd = mtd_find_partition_by_name(MTD_Name.c_str());
1366 if (mtd == NULL) {
1367 LOGERR("No mtd partition named '%s'", MTD_Name.c_str());
1368 return false;
1369 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001370
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001371 MtdWriteContext* ctx = mtd_write_partition(mtd);
1372 if (ctx == NULL) {
1373 LOGERR("Can't write '%s', failed to format.", MTD_Name.c_str());
1374 return false;
1375 }
1376 if (mtd_erase_blocks(ctx, -1) == -1) {
1377 mtd_write_close(ctx);
1378 LOGERR("Failed to format '%s'", MTD_Name.c_str());
1379 return false;
1380 }
1381 if (mtd_write_close(ctx) != 0) {
1382 LOGERR("Failed to close '%s'", MTD_Name.c_str());
1383 return false;
1384 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001385 Current_File_System = "yaffs2";
Dees_Troye58d5262012-09-21 12:27:57 -04001386 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001387 gui_print("Done.\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001388 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001389}
1390
1391bool TWPartition::Wipe_RMRF() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001392 if (!Mount(true))
1393 return false;
1394
Dees_Troy2673cec2013-04-02 20:22:16 +00001395 gui_print("Removing all files under '%s'\n", Mount_Point.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001396 TWFunc::removeDir(Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04001397 Recreate_AndSec_Folder();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001398 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001399}
1400
1401bool TWPartition::Wipe_Data_Without_Wiping_Media() {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001402 string dir;
Dees_Troy38bd7602012-09-14 13:33:53 -04001403
1404 // This handles wiping data on devices with "sdcard" in /data/media
1405 if (!Mount(true))
1406 return false;
1407
Dees_Troy2673cec2013-04-02 20:22:16 +00001408 gui_print("Wiping data without wiping /data/media ...\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001409
1410 DIR* d;
1411 d = opendir("/data");
Dees_Troy16b74352012-11-14 22:27:31 +00001412 if (d != NULL) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001413 struct dirent* de;
1414 while ((de = readdir(d)) != NULL) {
Dees_Troy16b74352012-11-14 22:27:31 +00001415 if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) continue;
1416 // The media folder is the "internal sdcard"
1417 // The .layout_version file is responsible for determining whether 4.2 decides up upgrade
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001418 // the media folder for multi-user.
Dees_Troy16b74352012-11-14 22:27:31 +00001419 if (strcmp(de->d_name, "media") == 0 || strcmp(de->d_name, ".layout_version") == 0) continue;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001420
1421 dir = "/data/";
1422 dir.append(de->d_name);
Dees_Troyce675462013-01-09 19:48:21 +00001423 if (de->d_type == DT_DIR) {
1424 TWFunc::removeDir(dir, false);
bigbiff bigbiff98f1f902013-01-19 18:46:13 -05001425 } 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 +00001426 if (!unlink(dir.c_str()))
Dees_Troy2673cec2013-04-02 20:22:16 +00001427 LOGINFO("Unable to unlink '%s'\n", dir.c_str());
Dees_Troyce675462013-01-09 19:48:21 +00001428 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001429 }
1430 closedir(d);
Dees_Troy2673cec2013-04-02 20:22:16 +00001431 gui_print("Done.\n");
Dees_Troy16b74352012-11-14 22:27:31 +00001432 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -04001433 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001434 gui_print("Dirent failed to open /data, error!\n");
Dees_Troy16b74352012-11-14 22:27:31 +00001435 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001436}
1437
1438bool TWPartition::Backup_Tar(string backup_folder) {
Dees_Troy4a2a1262012-09-18 09:33:47 -04001439 char back_name[255], split_index[5];
1440 string Full_FileName, Split_FileName, Tar_Args, Command;
Dees_Troy83bd4832013-05-04 12:39:56 +00001441 int use_compression, use_encryption = 0, index, backup_count;
Dees_Troy4a2a1262012-09-18 09:33:47 -04001442 struct stat st;
Dees_Troy7c2dec82012-09-26 09:49:14 -04001443 unsigned long long total_bsize = 0, file_size;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001444 twrpTar tar;
1445 vector <string> files;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001446
Dees_Troy43d8b002012-09-17 16:00:01 -04001447 if (!Mount(true))
1448 return false;
1449
Dees_Troya13d74f2013-03-24 08:54:55 -05001450 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Backup_Display_Name, "Backing Up");
Dees_Troy2673cec2013-04-02 20:22:16 +00001451 gui_print("Backing up %s...\n", Backup_Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001452
1453 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
Dees_Troy83bd4832013-05-04 12:39:56 +00001454 tar.use_compression = use_compression;
1455#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
1456 DataManager::GetValue("tw_encrypt_backup", use_encryption);
1457 if (use_encryption && Can_Encrypt_Backup) {
1458 tar.use_encryption = use_encryption;
1459 if (Use_Userdata_Encryption)
1460 tar.userdata_encryption = use_encryption;
1461 } else {
1462 use_encryption = false;
1463 }
1464#endif
Dees_Troy43d8b002012-09-17 16:00:01 -04001465
1466 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1467 Backup_FileName = back_name;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001468 Full_FileName = backup_folder + "/" + Backup_FileName;
Dees_Troy83bd4832013-05-04 12:39:56 +00001469 tar.has_data_media = Has_Data_Media;
1470 if (!use_encryption && Backup_Size > MAX_ARCHIVE_SIZE) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001471 // This backup needs to be split into multiple archives
Dees_Troy2673cec2013-04-02 20:22:16 +00001472 gui_print("Breaking backup file into multiple archives...\n");
Dees_Troye58d5262012-09-21 12:27:57 -04001473 sprintf(back_name, "%s", Backup_Path.c_str());
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -05001474 tar.setdir(back_name);
1475 tar.setfn(Full_FileName);
bigbiff bigbiffe6594ab2013-02-17 20:18:31 -05001476 backup_count = tar.splitArchiveFork();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001477 if (backup_count == -1) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001478 LOGERR("Error tarring split files!\n");
Dees_Troy4a2a1262012-09-18 09:33:47 -04001479 return false;
1480 }
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001481 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001482 } else {
Dees_Troy4a2a1262012-09-18 09:33:47 -04001483 Full_FileName = backup_folder + "/" + Backup_FileName;
Dees_Troy83bd4832013-05-04 12:39:56 +00001484 tar.setdir(Backup_Path);
1485 tar.setfn(Full_FileName);
1486 if (tar.createTarFork() != 0)
1487 return false;
1488 if (use_compression && !use_encryption) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001489 string gzname = Full_FileName + ".gz";
1490 rename(gzname.c_str(), Full_FileName.c_str());
1491 }
Dees_Troy83bd4832013-05-04 12:39:56 +00001492 if (use_encryption)
1493 Full_FileName += "000";
Dees_Troy7c2dec82012-09-26 09:49:14 -04001494 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001495 LOGERR("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001496 return false;
1497 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001498 }
1499 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001500}
1501
1502bool TWPartition::Backup_DD(string backup_folder) {
igoriok87e3d932013-01-31 21:03:53 +02001503 char back_name[255], backup_size[32];
1504 string Full_FileName, Command, result, DD_BS;
Dees_Troy43d8b002012-09-17 16:00:01 -04001505 int use_compression;
1506
igoriok87e3d932013-01-31 21:03:53 +02001507 sprintf(backup_size, "%llu", Backup_Size);
1508 DD_BS = backup_size;
1509
Dees_Troyb46a6842012-09-25 11:06:46 -04001510 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
Dees_Troy2673cec2013-04-02 20:22:16 +00001511 gui_print("Backing up %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001512
1513 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1514 Backup_FileName = back_name;
1515
1516 Full_FileName = backup_folder + "/" + Backup_FileName;
1517
igoriok87e3d932013-01-31 21:03:53 +02001518 Command = "dd if=" + Actual_Block_Device + " of='" + Full_FileName + "'" + " bs=" + DD_BS + "c count=1";
Dees_Troy2673cec2013-04-02 20:22:16 +00001519 LOGINFO("Backup command: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001520 TWFunc::Exec_Cmd(Command, result);
Dees_Troyc154ac22012-10-12 15:36:47 -04001521 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001522 LOGERR("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001523 return false;
1524 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001525 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001526}
1527
1528bool TWPartition::Backup_Dump_Image(string backup_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001529 char back_name[255];
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001530 string Full_FileName, Command, result;
Dees_Troy43d8b002012-09-17 16:00:01 -04001531 int use_compression;
1532
Dees_Troyb46a6842012-09-25 11:06:46 -04001533 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
Dees_Troy2673cec2013-04-02 20:22:16 +00001534 gui_print("Backing up %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001535
1536 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1537 Backup_FileName = back_name;
1538
1539 Full_FileName = backup_folder + "/" + Backup_FileName;
1540
1541 Command = "dump_image " + MTD_Name + " '" + Full_FileName + "'";
Dees_Troy2673cec2013-04-02 20:22:16 +00001542 LOGINFO("Backup command: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001543 TWFunc::Exec_Cmd(Command, result);
Dees_Troy7c2dec82012-09-26 09:49:14 -04001544 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
1545 // 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 +00001546 LOGERR("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001547 return false;
1548 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001549 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001550}
1551
Gary Peck43acadf2012-11-21 21:19:01 -08001552bool TWPartition::Restore_Tar(string restore_folder, string Restore_File_System) {
1553 string Full_FileName, Command;
Dees_Troy4a2a1262012-09-18 09:33:47 -04001554 int index = 0;
1555 char split_index[5];
Dees_Troy43d8b002012-09-17 16:00:01 -04001556
Dees_Troye58d5262012-09-21 12:27:57 -04001557 if (Has_Android_Secure) {
Dees_Troye58d5262012-09-21 12:27:57 -04001558 if (!Wipe_AndSec())
1559 return false;
Gary Peck43acadf2012-11-21 21:19:01 -08001560 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001561 gui_print("Wiping %s...\n", Display_Name.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001562 if (!Wipe(Restore_File_System))
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001563 return false;
Dees_Troye58d5262012-09-21 12:27:57 -04001564 }
Dees_Troya13d74f2013-03-24 08:54:55 -05001565 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Backup_Display_Name, "Restoring");
Dees_Troy2673cec2013-04-02 20:22:16 +00001566 gui_print("Restoring %s...\n", Backup_Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001567
1568 if (!Mount(true))
1569 return false;
1570
Dees_Troy4a2a1262012-09-18 09:33:47 -04001571 Full_FileName = restore_folder + "/" + Backup_FileName;
Dees_Troy83bd4832013-05-04 12:39:56 +00001572 /*if (!TWFunc::Path_Exists(Full_FileName)) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001573 if (!TWFunc::Path_Exists(Full_FileName)) {
1574 // Backup is multiple archives
Dees_Troy2673cec2013-04-02 20:22:16 +00001575 LOGINFO("Backup is multiple archives.\n");
Dees_Troy4a2a1262012-09-18 09:33:47 -04001576 sprintf(split_index, "%03i", index);
1577 Full_FileName = restore_folder + "/" + Backup_FileName + split_index;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001578 while (TWFunc::Path_Exists(Full_FileName)) {
1579 index++;
Dees_Troy2673cec2013-04-02 20:22:16 +00001580 gui_print("Restoring archive %i...\n", index);
1581 LOGINFO("Restoring '%s'...\n", Full_FileName.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001582 twrpTar tar;
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -05001583 tar.setdir("/");
1584 tar.setfn(Full_FileName);
bigbiff bigbiffe6594ab2013-02-17 20:18:31 -05001585 if (tar.extractTarFork() != 0)
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001586 return false;
1587 sprintf(split_index, "%03i", index);
1588 Full_FileName = restore_folder + "/" + Backup_FileName + split_index;
1589 }
1590 if (index == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001591 LOGERR("Error locating restore file: '%s'\n", Full_FileName.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001592 return false;
1593 }
Dees_Troy4a2a1262012-09-18 09:33:47 -04001594 }
Dees_Troy83bd4832013-05-04 12:39:56 +00001595 } else {*/
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001596 twrpTar tar;
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -05001597 tar.setdir(Backup_Path);
1598 tar.setfn(Full_FileName);
Dees_Troy83bd4832013-05-04 12:39:56 +00001599 tar.backup_name = Backup_Name;
bigbiff bigbiffe6594ab2013-02-17 20:18:31 -05001600 if (tar.extractTarFork() != 0)
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001601 return false;
Dees_Troy83bd4832013-05-04 12:39:56 +00001602 //}
Dees_Troy43d8b002012-09-17 16:00:01 -04001603 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001604}
1605
1606bool TWPartition::Restore_DD(string restore_folder) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001607 string Full_FileName, Command, result;
Dees_Troy43d8b002012-09-17 16:00:01 -04001608
Dees_Troyda8b55a2012-12-12 19:18:30 +00001609 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
Dees_Troy43d8b002012-09-17 16:00:01 -04001610 Full_FileName = restore_folder + "/" + Backup_FileName;
Gary Peck15e623d2012-11-21 21:07:58 -08001611
1612 if (!Find_Partition_Size()) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001613 LOGERR("Unable to find partition size for '%s'\n", Mount_Point.c_str());
Gary Peck15e623d2012-11-21 21:07:58 -08001614 return false;
1615 }
1616 unsigned long long backup_size = TWFunc::Get_File_Size(Full_FileName);
1617 if (backup_size > Size) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001618 LOGERR("Size (%iMB) of backup '%s' is larger than target device '%s' (%iMB)\n",
Gary Peck15e623d2012-11-21 21:07:58 -08001619 (int)(backup_size / 1048576LLU), Full_FileName.c_str(),
1620 Actual_Block_Device.c_str(), (int)(Size / 1048576LLU));
1621 return false;
1622 }
1623
Dees_Troy2673cec2013-04-02 20:22:16 +00001624 gui_print("Restoring %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001625 Command = "dd bs=4096 if='" + Full_FileName + "' of=" + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001626 LOGINFO("Restore command: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001627 TWFunc::Exec_Cmd(Command, result);
Dees_Troy43d8b002012-09-17 16:00:01 -04001628 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001629}
1630
1631bool TWPartition::Restore_Flash_Image(string restore_folder) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001632 string Full_FileName, Command, result;
Dees_Troy43d8b002012-09-17 16:00:01 -04001633
Dees_Troy2673cec2013-04-02 20:22:16 +00001634 gui_print("Restoring %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001635 Full_FileName = restore_folder + "/" + Backup_FileName;
1636 // Sometimes flash image doesn't like to flash due to the first 2KB matching, so we erase first to ensure that it flashes
1637 Command = "erase_image " + MTD_Name;
Dees_Troy2673cec2013-04-02 20:22:16 +00001638 LOGINFO("Erase command: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001639 TWFunc::Exec_Cmd(Command, result);
Dees_Troy43d8b002012-09-17 16:00:01 -04001640 Command = "flash_image " + MTD_Name + " '" + Full_FileName + "'";
Dees_Troy2673cec2013-04-02 20:22:16 +00001641 LOGINFO("Restore command: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001642 TWFunc::Exec_Cmd(Command, result);
Dees_Troy43d8b002012-09-17 16:00:01 -04001643 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001644}
Dees_Troy5bf43922012-09-07 16:07:55 -04001645
1646bool TWPartition::Update_Size(bool Display_Error) {
Dees_Troy0550cfb2012-10-13 11:56:13 -04001647 bool ret = false, Was_Already_Mounted = false;
Dees_Troy51127312012-09-08 13:08:49 -04001648
Dees_Troyab10ee22012-09-21 14:27:30 -04001649 if (!Can_Be_Mounted && !Is_Encrypted)
Dees_Troy5bf43922012-09-07 16:07:55 -04001650 return false;
1651
Dees_Troy0550cfb2012-10-13 11:56:13 -04001652 Was_Already_Mounted = Is_Mounted();
Dees_Troy38bd7602012-09-14 13:33:53 -04001653 if (Removable || Is_Encrypted) {
1654 if (!Mount(false))
1655 return true;
1656 } else if (!Mount(Display_Error))
Dees_Troy5bf43922012-09-07 16:07:55 -04001657 return false;
Dees_Troy51127312012-09-08 13:08:49 -04001658
1659 ret = Get_Size_Via_statfs(Display_Error);
Dees_Troy0550cfb2012-10-13 11:56:13 -04001660 if (!ret || Size == 0) {
1661 if (!Get_Size_Via_df(Display_Error)) {
1662 if (!Was_Already_Mounted)
1663 UnMount(false);
Dees_Troy51127312012-09-08 13:08:49 -04001664 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04001665 }
1666 }
Dees_Troy51127312012-09-08 13:08:49 -04001667
Dees_Troy5bf43922012-09-07 16:07:55 -04001668 if (Has_Data_Media) {
1669 if (Mount(Display_Error)) {
Dees_Troy51127312012-09-08 13:08:49 -04001670 unsigned long long data_media_used, actual_data;
Dees_Troy43d8b002012-09-17 16:00:01 -04001671 Used = TWFunc::Get_Folder_Size("/data", Display_Error);
1672 data_media_used = TWFunc::Get_Folder_Size("/data/media", Display_Error);
Dees_Troy51127312012-09-08 13:08:49 -04001673 actual_data = Used - data_media_used;
Dees_Troy5bf43922012-09-07 16:07:55 -04001674 Backup_Size = actual_data;
Dees_Troy51127312012-09-08 13:08:49 -04001675 int bak = (int)(Backup_Size / 1048576LLU);
1676 int total = (int)(Size / 1048576LLU);
1677 int us = (int)(Used / 1048576LLU);
1678 int fre = (int)(Free / 1048576LLU);
1679 int datmed = (int)(data_media_used / 1048576LLU);
Dees_Troy2673cec2013-04-02 20:22:16 +00001680 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 -04001681 } else {
1682 if (!Was_Already_Mounted)
1683 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04001684 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04001685 }
Dees_Troye58d5262012-09-21 12:27:57 -04001686 } else if (Has_Android_Secure) {
1687 if (Mount(Display_Error))
1688 Backup_Size = TWFunc::Get_Folder_Size(Backup_Path, Display_Error);
Dees_Troy0550cfb2012-10-13 11:56:13 -04001689 else {
1690 if (!Was_Already_Mounted)
1691 UnMount(false);
Dees_Troye58d5262012-09-21 12:27:57 -04001692 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04001693 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001694 }
Dees_Troy0550cfb2012-10-13 11:56:13 -04001695 if (!Was_Already_Mounted)
1696 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04001697 return true;
Dees_Troy51127312012-09-08 13:08:49 -04001698}
Dees_Troy38bd7602012-09-14 13:33:53 -04001699
1700void TWPartition::Find_Actual_Block_Device(void) {
1701 if (Is_Decrypted) {
1702 Actual_Block_Device = Decrypted_Block_Device;
Dees_Troy43d8b002012-09-17 16:00:01 -04001703 if (TWFunc::Path_Exists(Primary_Block_Device))
Dees_Troy38bd7602012-09-14 13:33:53 -04001704 Is_Present = true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001705 } else if (TWFunc::Path_Exists(Primary_Block_Device)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001706 Is_Present = true;
1707 Actual_Block_Device = Primary_Block_Device;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001708 return;
1709 }
1710 if (Is_Decrypted) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001711 } else if (!Alternate_Block_Device.empty() && TWFunc::Path_Exists(Alternate_Block_Device)) {
Dees_Troy3f04d032012-10-07 18:20:09 -04001712 Actual_Block_Device = Alternate_Block_Device;
Dees_Troy38bd7602012-09-14 13:33:53 -04001713 Is_Present = true;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001714 } else {
Dees_Troy38bd7602012-09-14 13:33:53 -04001715 Is_Present = false;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001716 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001717}
1718
1719void TWPartition::Recreate_Media_Folder(void) {
1720 string Command;
1721
1722 if (!Mount(true)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001723 LOGERR("Unable to recreate /data/media folder.\n");
Dees_Troyb46a6842012-09-25 11:06:46 -04001724 } else if (!TWFunc::Path_Exists("/data/media")) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001725 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Dees_Troy2673cec2013-04-02 20:22:16 +00001726 LOGINFO("Recreating /data/media folder.\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001727 mkdir("/data/media", S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
1728 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Dees_Troy38bd7602012-09-14 13:33:53 -04001729 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001730}
Dees_Troye58d5262012-09-21 12:27:57 -04001731
1732void TWPartition::Recreate_AndSec_Folder(void) {
Dees_Troye58d5262012-09-21 12:27:57 -04001733 if (!Has_Android_Secure)
1734 return;
Dees_Troy2673cec2013-04-02 20:22:16 +00001735 LOGINFO("Creating %s: %s\n", Backup_Display_Name.c_str(), Symlink_Path.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -04001736 if (!Mount(true)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001737 LOGERR("Unable to recreate %s folder.\n", Backup_Name.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -04001738 } else if (!TWFunc::Path_Exists(Symlink_Path)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001739 LOGINFO("Recreating %s folder.\n", Backup_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001740 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
1741 mkdir(Symlink_Path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
1742 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04001743 }
1744}