blob: c1e214e528b6ea7e6c4b9df425e5afe11fa2a650 [file] [log] [blame]
Dees Troy3be70a82013-10-22 14:25:12 +00001/*
2 Copyright 2012 bigbiff/Dees_Troy TeamWin
3 This file is part of TWRP/TeamWin Recovery Project.
4
5 TWRP is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 TWRP is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with TWRP. If not, see <http://www.gnu.org/licenses/>.
17*/
Dees_Troy51a0e822012-09-05 15:24:24 -040018
19#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
22#include <sys/stat.h>
23#include <sys/vfs.h>
Dees_Troy5bf43922012-09-07 16:07:55 -040024#include <sys/mount.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040025#include <unistd.h>
Dees_Troy51127312012-09-08 13:08:49 -040026#include <dirent.h>
bigbiff bigbiff9c754052013-01-09 09:09:08 -050027#include <iostream>
28#include <sstream>
Dees_Troy51a0e822012-09-05 15:24:24 -040029
Dees_Troy657c3092012-09-10 20:32:10 -040030#ifdef TW_INCLUDE_CRYPTO
31 #include "cutils/properties.h"
32#endif
33
bigbiff bigbiffe60683a2013-02-22 20:55:50 -050034#include "libblkid/blkid.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040035#include "variables.h"
Dees_Troy2673cec2013-04-02 20:22:16 +000036#include "twcommon.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040037#include "partitions.hpp"
Dees_Troy5bf43922012-09-07 16:07:55 -040038#include "data.hpp"
Dees_Troy43d8b002012-09-17 16:00:01 -040039#include "twrp-functions.hpp"
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -050040#include "twrpDigest.hpp"
bigbiff bigbiff9c754052013-01-09 09:09:08 -050041#include "twrpTar.hpp"
Dees_Troy5bf43922012-09-07 16:07:55 -040042extern "C" {
Dees_Troy38bd7602012-09-14 13:33:53 -040043 #include "mtdutils/mtdutils.h"
44 #include "mtdutils/mounts.h"
Dees_Troy85f44ed2013-01-09 18:42:36 +000045#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
46 #include "crypto/libcrypt_samsung/include/libcrypt_samsung.h"
47#endif
Dees_Troya95f55c2013-08-17 13:14:43 +000048#ifdef USE_EXT4
49 #include "make_ext4fs.h"
50#endif
Dees_Troy5bf43922012-09-07 16:07:55 -040051}
bigbiff bigbiffc49d7062013-10-11 20:28:00 -040052#ifdef HAVE_SELINUX
53#include "selinux/selinux.h"
54#endif
Dees_Troy51a0e822012-09-05 15:24:24 -040055
bigbiff bigbiff9c754052013-01-09 09:09:08 -050056using namespace std;
57
Dees_Troya95f55c2013-08-17 13:14:43 +000058extern struct selabel_handle *selinux_handle;
59
Dees_Troy51a0e822012-09-05 15:24:24 -040060TWPartition::TWPartition(void) {
61 Can_Be_Mounted = false;
62 Can_Be_Wiped = false;
Dees_Troya13d74f2013-03-24 08:54:55 -050063 Can_Be_Backed_Up = false;
Vojtech Bocek1dc30982013-08-30 21:49:30 +020064 Use_Rm_Rf = 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 Troyd932ce12013-10-18 17:12:59 +0000562 LOGERR("Unable to find partition size for '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400563 else
Dees Troyd932ce12013-10-18 17:12:59 +0000564 LOGINFO("Unable to find partition 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;
686
687 if (!Mount(Display_Error))
688 return false;
689
Dees_Troy38bd7602012-09-14 13:33:53 -0400690 min_len = Actual_Block_Device.size() + 2;
Dees_Troy51127312012-09-08 13:08:49 -0400691 sprintf(command, "df %s > /tmp/dfoutput.txt", Mount_Point.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +0200692 TWFunc::Exec_Cmd(command);
Dees_Troy51127312012-09-08 13:08:49 -0400693 fp = fopen("/tmp/dfoutput.txt", "rt");
694 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000695 LOGINFO("Unable to open /tmp/dfoutput.txt.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400696 return false;
Dees_Troy51127312012-09-08 13:08:49 -0400697 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400698
699 while (fgets(line, sizeof(line), fp) != NULL)
700 {
701 unsigned long blocks, used, available;
702 char device[64];
703 char tmpString[64];
704
705 if (strncmp(line, "Filesystem", 10) == 0)
706 continue;
707 if (strlen(line) < min_len) {
708 include_block = 0;
709 continue;
710 }
711 if (include_block) {
712 sscanf(line, "%s %lu %lu %lu", device, &blocks, &used, &available);
713 } else {
714 // The device block string is so long that the df information is on the next line
715 int space_count = 0;
Dees_Troye58d5262012-09-21 12:27:57 -0400716 sprintf(tmpString, "/dev/block/%s", Actual_Block_Device.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400717 while (tmpString[space_count] == 32)
718 space_count++;
719 sscanf(line + space_count, "%lu %lu %lu", &blocks, &used, &available);
720 }
721
722 // Adjust block size to byte size
723 Size = blocks * 1024ULL;
724 Used = used * 1024ULL;
725 Free = available * 1024ULL;
726 Backup_Size = Used;
727 }
728 fclose(fp);
729 return true;
730}
731
Dees_Troy5bf43922012-09-07 16:07:55 -0400732bool TWPartition::Find_Partition_Size(void) {
733 FILE* fp;
734 char line[512];
735 string tmpdevice;
736
igoriok87e3d932013-01-31 21:03:53 +0200737 fp = fopen("/proc/dumchar_info", "rt");
738 if (fp != NULL) {
739 while (fgets(line, sizeof(line), fp) != NULL)
740 {
741 char label[32], device[32];
742 unsigned long size = 0;
743
744 sscanf(line, "%s %lx %*lx %*lu %s", label, &size, device);
745
746 // Skip header, annotation and blank lines
747 if ((strncmp(device, "/dev/", 5) != 0) || (strlen(line) < 8))
748 continue;
749
750 tmpdevice = "/dev/";
751 tmpdevice += label;
752 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
753 Size = size;
754 fclose(fp);
755 return true;
756 }
757 }
758 }
759
Dees_Troy5bf43922012-09-07 16:07:55 -0400760 // In this case, we'll first get the partitions we care about (with labels)
761 fp = fopen("/proc/partitions", "rt");
762 if (fp == NULL)
763 return false;
764
765 while (fgets(line, sizeof(line), fp) != NULL)
766 {
767 unsigned long major, minor, blocks;
768 char device[512];
769 char tmpString[64];
770
Dees_Troy63c8df72012-09-10 14:02:05 -0400771 if (strlen(line) < 7 || line[0] == 'm') continue;
Dees_Troy5bf43922012-09-07 16:07:55 -0400772 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
773
774 tmpdevice = "/dev/block/";
775 tmpdevice += device;
Dees_Troy38bd7602012-09-14 13:33:53 -0400776 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400777 // Adjust block size to byte size
778 Size = blocks * 1024ULL;
779 fclose(fp);
780 return true;
781 }
782 }
783 fclose(fp);
784 return false;
785}
786
Dees_Troy5bf43922012-09-07 16:07:55 -0400787bool TWPartition::Is_Mounted(void) {
788 if (!Can_Be_Mounted)
789 return false;
790
791 struct stat st1, st2;
792 string test_path;
793
794 // Check to see if the mount point directory exists
795 test_path = Mount_Point + "/.";
796 if (stat(test_path.c_str(), &st1) != 0) return false;
797
798 // Check to see if the directory above the mount point exists
799 test_path = Mount_Point + "/../.";
800 if (stat(test_path.c_str(), &st2) != 0) return false;
801
802 // Compare the device IDs -- if they match then we're (probably) using tmpfs instead of an actual device
803 int ret = (st1.st_dev != st2.st_dev) ? true : false;
804
805 return ret;
806}
807
808bool TWPartition::Mount(bool Display_Error) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000809 int exfat_mounted = 0;
810
Dees_Troy5bf43922012-09-07 16:07:55 -0400811 if (Is_Mounted()) {
812 return true;
813 } else if (!Can_Be_Mounted) {
814 return false;
815 }
Dees_Troy38bd7602012-09-14 13:33:53 -0400816
817 Find_Actual_Block_Device();
818
819 // Check the current file system before mounting
820 Check_FS_Type();
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000821 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/exfat-fuse")) {
Dees_Troye34c1332013-02-06 19:13:00 +0000822 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 +0000823 LOGINFO("cmd: %s\n", cmd.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000824 string result;
825 if (TWFunc::Exec_Cmd(cmd, result) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000826 LOGINFO("exfat-fuse failed to mount with result '%s', trying vfat\n", result.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000827 Current_File_System = "vfat";
828 } else {
829#ifdef TW_NO_EXFAT_FUSE
830 UnMount(false);
831 // We'll let the kernel handle it but using exfat-fuse to detect if the file system is actually exfat
832 // Some kernels let us mount vfat as exfat which doesn't work out too well
833#else
834 exfat_mounted = 1;
835#endif
836 }
837 }
Dees_Troy22042032012-12-18 21:23:08 +0000838 if (Fstab_File_System == "yaffs2") {
839 // mount an MTD partition as a YAFFS2 filesystem.
Dees_Troy76543db2013-06-19 16:24:30 +0000840 const unsigned long flags = MS_NOATIME | MS_NODEV | MS_NODIRATIME;
841 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags, NULL) < 0) {
842 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags | MS_RDONLY, NULL) < 0) {
843 if (Display_Error)
844 LOGERR("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
845 else
846 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
847 return false;
848 } else {
849 LOGINFO("Mounted '%s' (MTD) as RO\n", Mount_Point.c_str());
850 return true;
851 }
852 } else {
853 struct stat st;
854 string test_path = Mount_Point;
855 if (stat(test_path.c_str(), &st) < 0) {
856 if (Display_Error)
857 LOGERR("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
858 else
859 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
860 return false;
861 }
862 mode_t new_mode = st.st_mode | S_IXUSR | S_IXGRP | S_IXOTH;
863 if (new_mode != st.st_mode) {
864 LOGINFO("Fixing execute permissions for %s\n", Mount_Point.c_str());
865 if (chmod(Mount_Point.c_str(), new_mode) < 0) {
866 if (Display_Error)
867 LOGERR("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
868 else
869 LOGINFO("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
870 return false;
871 }
872 }
Dees_Troy22042032012-12-18 21:23:08 +0000873 return true;
Dees_Troy76543db2013-06-19 16:24:30 +0000874 }
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000875 } else if (!exfat_mounted && mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Current_File_System.c_str(), 0, NULL) != 0) {
876#ifdef TW_NO_EXFAT_FUSE
877 if (Current_File_System == "exfat") {
Dees_Troy2673cec2013-04-02 20:22:16 +0000878 LOGINFO("Mounting exfat failed, trying vfat...\n");
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000879 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), "vfat", 0, NULL) != 0) {
Dees_Troy85f44ed2013-01-09 18:42:36 +0000880 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000881 LOGERR("Unable to mount '%s'\n", Mount_Point.c_str());
Dees_Troy85f44ed2013-01-09 18:42:36 +0000882 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000883 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
884 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 +0000885 return false;
Dees_Troy85f44ed2013-01-09 18:42:36 +0000886 }
Dees_Troyb05ddee2013-01-28 20:24:50 +0000887 } else {
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000888#endif
889 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000890 LOGERR("Unable to mount '%s'\n", Mount_Point.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000891 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000892 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
893 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 +0000894 return false;
895#ifdef TW_NO_EXFAT_FUSE
Dees_Troy85f44ed2013-01-09 18:42:36 +0000896 }
897#endif
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000898 }
899#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
900 string MetaEcfsFile = EXPAND(TW_EXTERNAL_STORAGE_PATH);
901 MetaEcfsFile += "/.MetaEcfsFile";
902 if (EcryptFS_Password.size() > 0 && PartitionManager.Mount_By_Path("/data", false) && TWFunc::Path_Exists(MetaEcfsFile)) {
903 if (mount_ecryptfs_drive(EcryptFS_Password.c_str(), Mount_Point.c_str(), Mount_Point.c_str(), 0) != 0) {
904 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000905 LOGERR("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("Unable to mount ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000908 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000909 LOGINFO("Successfully mounted ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000910 Is_Decrypted = true;
Dees_Troy51127312012-09-08 13:08:49 -0400911 }
Dees_Troy066eb302013-08-23 17:20:32 +0000912 } else if (Mount_Point == EXPAND(TW_EXTERNAL_STORAGE_PATH)) {
913 if (Is_Decrypted)
914 LOGINFO("Mounting external storage, '%s' is not encrypted\n", Mount_Point.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000915 Is_Decrypted = false;
916 }
917#endif
918 if (Removable)
919 Update_Size(Display_Error);
920
921 if (!Symlink_Mount_Point.empty()) {
Vojtech Bocek05534202013-09-11 08:11:56 +0200922 string Command = "mount '" + Symlink_Path + "' '" + Symlink_Mount_Point + "'";
923 TWFunc::Exec_Cmd(Command);
Dees_Troy5bf43922012-09-07 16:07:55 -0400924 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400925 return true;
926}
927
928bool TWPartition::UnMount(bool Display_Error) {
929 if (Is_Mounted()) {
930 int never_unmount_system;
931
932 DataManager::GetValue(TW_DONT_UNMOUNT_SYSTEM, never_unmount_system);
933 if (never_unmount_system == 1 && Mount_Point == "/system")
934 return true; // Never unmount system if you're not supposed to unmount it
935
Dees_Troyc8bafa12013-01-10 15:43:00 +0000936#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
937 if (EcryptFS_Password.size() > 0) {
938 if (unmount_ecryptfs_drive(Mount_Point.c_str()) != 0) {
939 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000940 LOGERR("Unable to unmount ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troyc8bafa12013-01-10 15:43:00 +0000941 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000942 LOGINFO("Unable to unmount ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troyc8bafa12013-01-10 15:43:00 +0000943 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000944 LOGINFO("Successfully unmounted ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troyc8bafa12013-01-10 15:43:00 +0000945 }
946 }
947#endif
948
Dees_Troy38bd7602012-09-14 13:33:53 -0400949 if (!Symlink_Mount_Point.empty())
950 umount(Symlink_Mount_Point.c_str());
951
Dees_Troyb05ddee2013-01-28 20:24:50 +0000952 umount(Mount_Point.c_str());
953 if (Is_Mounted()) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400954 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000955 LOGERR("Unable to unmount '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400956 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000957 LOGINFO("Unable to unmount '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400958 return false;
959 } else
960 return true;
961 } else {
962 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400963 }
964}
965
Gary Peck43acadf2012-11-21 21:19:01 -0800966bool TWPartition::Wipe(string New_File_System) {
Dees_Troy16c2b312013-01-15 16:51:18 +0000967 bool wiped = false, update_crypt = false;
968 int check;
969 string Layout_Filename = Mount_Point + "/.layout_version";
970
Dees_Troy38bd7602012-09-14 13:33:53 -0400971 if (!Can_Be_Wiped) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000972 LOGERR("Partition '%s' cannot be wiped.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -0400973 return false;
974 }
975
Dees_Troyc51f1f92012-09-20 15:32:13 -0400976 if (Mount_Point == "/cache")
Dees_Troy2673cec2013-04-02 20:22:16 +0000977 Log_Offset = 0;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400978
Dees_Troyce675462013-01-09 19:48:21 +0000979#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
980 if (Mount_Point == "/data" && Mount(false)) {
981 if (TWFunc::Path_Exists("/data/system/edk_p_sd"))
982 TWFunc::copy_file("/data/system/edk_p_sd", "/tmp/edk_p_sd", 0600);
983 }
984#endif
985
Dees_Troy16c2b312013-01-15 16:51:18 +0000986 if (Retain_Layout_Version && Mount(false) && TWFunc::Path_Exists(Layout_Filename))
987 TWFunc::copy_file(Layout_Filename, "/.layout_version", 0600);
988 else
989 unlink("/.layout_version");
Dees_Troy38bd7602012-09-14 13:33:53 -0400990
Dees_Troy16c2b312013-01-15 16:51:18 +0000991 if (Has_Data_Media) {
992 wiped = Wipe_Data_Without_Wiping_Media();
993 } else {
Gary Pecke8bc5d72012-12-21 06:45:25 -0800994
Dees_Troy16c2b312013-01-15 16:51:18 +0000995 DataManager::GetValue(TW_RM_RF_VAR, check);
996
Hashcodedabfd492013-08-29 22:45:30 -0700997 if (check || Use_Rm_Rf)
Dees_Troy16c2b312013-01-15 16:51:18 +0000998 wiped = Wipe_RMRF();
999 else if (New_File_System == "ext4")
1000 wiped = Wipe_EXT4();
1001 else if (New_File_System == "ext2" || New_File_System == "ext3")
1002 wiped = Wipe_EXT23(New_File_System);
1003 else if (New_File_System == "vfat")
1004 wiped = Wipe_FAT();
1005 else if (New_File_System == "exfat")
1006 wiped = Wipe_EXFAT();
1007 else if (New_File_System == "yaffs2")
1008 wiped = Wipe_MTD();
Dees_Troye5017042013-08-29 16:38:55 +00001009 else if (New_File_System == "f2fs")
1010 wiped = Wipe_F2FS();
Dees_Troy16c2b312013-01-15 16:51:18 +00001011 else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001012 LOGERR("Unable to wipe '%s' -- unknown file system '%s'\n", Mount_Point.c_str(), New_File_System.c_str());
Dees_Troy16c2b312013-01-15 16:51:18 +00001013 unlink("/.layout_version");
1014 return false;
1015 }
1016 update_crypt = wiped;
Gary Pecke8bc5d72012-12-21 06:45:25 -08001017 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001018
Gary Pecke8bc5d72012-12-21 06:45:25 -08001019 if (wiped) {
Dees_Troyce675462013-01-09 19:48:21 +00001020#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
1021 if (Mount_Point == "/data" && Mount(false)) {
1022 if (TWFunc::Path_Exists("/tmp/edk_p_sd")) {
1023 Make_Dir("/data/system", true);
1024 TWFunc::copy_file("/tmp/edk_p_sd", "/data/system/edk_p_sd", 0600);
1025 }
1026 }
1027#endif
Dees_Troy16c2b312013-01-15 16:51:18 +00001028
Dees_Troy1c1ac442013-01-17 21:42:14 +00001029 if (Mount_Point == "/cache")
1030 DataManager::Output_Version();
1031
Dees_Troy16c2b312013-01-15 16:51:18 +00001032 if (TWFunc::Path_Exists("/.layout_version") && Mount(false))
1033 TWFunc::copy_file("/.layout_version", Layout_Filename, 0600);
1034
1035 if (update_crypt) {
1036 Setup_File_System(false);
1037 if (Is_Encrypted && !Is_Decrypted) {
1038 // just wiped an encrypted partition back to its unencrypted state
1039 Is_Encrypted = false;
1040 Is_Decrypted = false;
1041 Decrypted_Block_Device = "";
1042 if (Mount_Point == "/data") {
1043 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1044 DataManager::SetValue(TW_IS_DECRYPTED, 0);
1045 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001046 }
1047 }
1048 }
1049 return wiped;
Dees_Troy51a0e822012-09-05 15:24:24 -04001050}
1051
Gary Peck43acadf2012-11-21 21:19:01 -08001052bool TWPartition::Wipe() {
Gary Peck82599a82012-11-21 16:23:12 -08001053 if (Is_File_System(Current_File_System))
1054 return Wipe(Current_File_System);
1055 else
1056 return Wipe(Fstab_File_System);
Gary Peck43acadf2012-11-21 21:19:01 -08001057}
1058
Dees_Troye58d5262012-09-21 12:27:57 -04001059bool TWPartition::Wipe_AndSec(void) {
1060 if (!Has_Android_Secure)
1061 return false;
1062
Dees_Troye58d5262012-09-21 12:27:57 -04001063 if (!Mount(true))
1064 return false;
1065
Dees_Troy2673cec2013-04-02 20:22:16 +00001066 gui_print("Wiping %s\n", Backup_Display_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001067 TWFunc::removeDir(Mount_Point + "/.android_secure/", true);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001068 return true;
Dees_Troye58d5262012-09-21 12:27:57 -04001069}
1070
Dees_Troy51a0e822012-09-05 15:24:24 -04001071bool TWPartition::Backup(string backup_folder) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001072 if (Backup_Method == FILES)
1073 return Backup_Tar(backup_folder);
1074 else if (Backup_Method == DD)
1075 return Backup_DD(backup_folder);
1076 else if (Backup_Method == FLASH_UTILS)
1077 return Backup_Dump_Image(backup_folder);
Dees_Troy2673cec2013-04-02 20:22:16 +00001078 LOGERR("Unknown backup method for '%s'\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001079 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001080}
1081
Dees_Troy43d8b002012-09-17 16:00:01 -04001082bool TWPartition::Check_MD5(string restore_folder) {
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001083 string Full_Filename, md5file;
Dees_Troy43d8b002012-09-17 16:00:01 -04001084 char split_filename[512];
1085 int index = 0;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001086 twrpDigest md5sum;
Dees_Troy43d8b002012-09-17 16:00:01 -04001087
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001088 memset(split_filename, 0, sizeof(split_filename));
Dees_Troy43d8b002012-09-17 16:00:01 -04001089 Full_Filename = restore_folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04001090 if (!TWFunc::Path_Exists(Full_Filename)) {
1091 // This is a split archive, we presume
1092 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
Dees_Troy2673cec2013-04-02 20:22:16 +00001093 LOGINFO("split_filename: %s\n", split_filename);
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001094 md5file = split_filename;
1095 md5file += ".md5";
1096 if (!TWFunc::Path_Exists(md5file)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001097 LOGERR("No md5 file found for '%s'.\n", split_filename);
1098 LOGERR("Please unselect Enable MD5 verification to restore.\n");
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001099 return false;
1100 }
1101 md5sum.setfn(split_filename);
Dees_Troy83bd4832013-05-04 12:39:56 +00001102 while (index < 1000) {
1103 if (TWFunc::Path_Exists(split_filename) && md5sum.verify_md5digest() != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001104 LOGERR("MD5 failed to match on '%s'.\n", split_filename);
Dees_Troy43d8b002012-09-17 16:00:01 -04001105 return false;
1106 }
1107 index++;
1108 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001109 md5sum.setfn(split_filename);
Dees_Troy43d8b002012-09-17 16:00:01 -04001110 }
Dees_Troy4a2a1262012-09-18 09:33:47 -04001111 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001112 } else {
1113 // Single file archive
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001114 md5file = Full_Filename + ".md5";
1115 if (!TWFunc::Path_Exists(md5file)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001116 LOGERR("No md5 file found for '%s'.\n", Full_Filename.c_str());
1117 LOGERR("Please unselect Enable MD5 verification to restore.\n");
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001118 return false;
1119 }
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001120 md5sum.setfn(Full_Filename);
1121 if (md5sum.verify_md5digest() != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001122 LOGERR("MD5 failed to match on '%s'.\n", Full_Filename.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001123 return false;
1124 } else
1125 return true;
1126 }
1127 return false;
1128}
1129
Dees_Troy51a0e822012-09-05 15:24:24 -04001130bool TWPartition::Restore(string restore_folder) {
Gary Peck43acadf2012-11-21 21:19:01 -08001131 size_t first_period, second_period;
1132 string Restore_File_System;
1133
1134 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
Dees_Troy2673cec2013-04-02 20:22:16 +00001135 LOGINFO("Restore filename is: %s\n", Backup_FileName.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001136
1137 // Parse backup filename to extract the file system before wiping
1138 first_period = Backup_FileName.find(".");
1139 if (first_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001140 LOGERR("Unable to find file system (first period).\n");
Gary Peck43acadf2012-11-21 21:19:01 -08001141 return false;
1142 }
1143 Restore_File_System = Backup_FileName.substr(first_period + 1, Backup_FileName.size() - first_period - 1);
1144 second_period = Restore_File_System.find(".");
1145 if (second_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001146 LOGERR("Unable to find file system (second period).\n");
Gary Peck43acadf2012-11-21 21:19:01 -08001147 return false;
1148 }
1149 Restore_File_System.resize(second_period);
Dees_Troy2673cec2013-04-02 20:22:16 +00001150 LOGINFO("Restore file system is: '%s'.\n", Restore_File_System.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001151
1152 if (Is_File_System(Restore_File_System))
1153 return Restore_Tar(restore_folder, Restore_File_System);
1154 else if (Is_Image(Restore_File_System)) {
1155 if (Restore_File_System == "emmc")
1156 return Restore_DD(restore_folder);
1157 else if (Restore_File_System == "mtd" || Restore_File_System == "bml")
1158 return Restore_Flash_Image(restore_folder);
1159 }
1160
Dees_Troy2673cec2013-04-02 20:22:16 +00001161 LOGERR("Unknown restore method for '%s'\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001162 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001163}
1164
1165string TWPartition::Backup_Method_By_Name() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001166 if (Backup_Method == NONE)
1167 return "none";
1168 else if (Backup_Method == FILES)
1169 return "files";
1170 else if (Backup_Method == DD)
1171 return "dd";
1172 else if (Backup_Method == FLASH_UTILS)
1173 return "flash_utils";
1174 else
1175 return "undefined";
1176 return "ERROR!";
Dees_Troy51a0e822012-09-05 15:24:24 -04001177}
1178
1179bool TWPartition::Decrypt(string Password) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001180 LOGINFO("STUB TWPartition::Decrypt, password: '%s'\n", Password.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001181 // Is this needed?
Dees_Troy51a0e822012-09-05 15:24:24 -04001182 return 1;
1183}
1184
1185bool TWPartition::Wipe_Encryption() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001186 bool Save_Data_Media = Has_Data_Media;
1187
1188 if (!UnMount(true))
1189 return false;
1190
Dees_Troy38bd7602012-09-14 13:33:53 -04001191 Has_Data_Media = false;
Dees_Troy74fb2e92013-04-15 14:35:47 +00001192 Decrypted_Block_Device = "";
1193 Is_Decrypted = false;
1194 Is_Encrypted = false;
Gary Pecke8bc5d72012-12-21 06:45:25 -08001195 if (Wipe(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001196 Has_Data_Media = Save_Data_Media;
1197 if (Has_Data_Media && !Symlink_Mount_Point.empty()) {
1198 Recreate_Media_Folder();
1199 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001200 gui_print("You may need to reboot recovery to be able to use /data again.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001201 return true;
1202 } else {
1203 Has_Data_Media = Save_Data_Media;
Dees_Troy2673cec2013-04-02 20:22:16 +00001204 LOGERR("Unable to format to remove encryption.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001205 return false;
1206 }
1207 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001208}
1209
1210void TWPartition::Check_FS_Type() {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001211 const char* type;
1212 blkid_probe pr;
Dees_Troy5bf43922012-09-07 16:07:55 -04001213
Dees_Troy68cab492012-12-12 19:29:35 +00001214 if (Fstab_File_System == "yaffs2" || Fstab_File_System == "mtd" || Fstab_File_System == "bml" || Ignore_Blkid)
1215 return; // Running blkid on some mtd devices causes a massive crash or needs to be skipped
Dees_Troy5bf43922012-09-07 16:07:55 -04001216
Dees_Troy38bd7602012-09-14 13:33:53 -04001217 Find_Actual_Block_Device();
Dees_Troy8170a922012-09-18 15:40:25 -04001218 if (!Is_Present)
1219 return;
Dees_Troy51127312012-09-08 13:08:49 -04001220
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001221 pr = blkid_new_probe_from_filename(Actual_Block_Device.c_str());
1222 if (blkid_do_fullprobe(pr)) {
1223 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00001224 LOGINFO("Can't probe device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001225 return;
Dees_Troy5bf43922012-09-07 16:07:55 -04001226 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001227
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001228 if (blkid_probe_lookup_value(pr, "TYPE", &type, NULL) < 0) {
1229 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00001230 LOGINFO("can't find filesystem on device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001231 return;
1232 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001233
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001234 Current_File_System = type;
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001235 blkid_free_probe(pr);
Dees_Troy51a0e822012-09-05 15:24:24 -04001236}
1237
Gary Peck43acadf2012-11-21 21:19:01 -08001238bool TWPartition::Wipe_EXT23(string File_System) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001239 if (!UnMount(true))
1240 return false;
1241
Dees_Troy43d8b002012-09-17 16:00:01 -04001242 if (TWFunc::Path_Exists("/sbin/mke2fs")) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001243 string command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001244
Dees_Troy2673cec2013-04-02 20:22:16 +00001245 gui_print("Formatting %s using mke2fs...\n", Display_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001246 Find_Actual_Block_Device();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001247 command = "mke2fs -t " + File_System + " -m 0 " + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001248 LOGINFO("mke2fs command: %s\n", command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001249 if (TWFunc::Exec_Cmd(command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001250 Current_File_System = File_System;
Dees_Troye58d5262012-09-21 12:27:57 -04001251 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001252 gui_print("Done.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001253 return true;
1254 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001255 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001256 return false;
1257 }
1258 } else
1259 return Wipe_RMRF();
1260
1261 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001262}
1263
1264bool TWPartition::Wipe_EXT4() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001265 if (!UnMount(true))
1266 return false;
1267
Dees_Troyb3265ab2013-08-30 02:59:14 +00001268#if defined(HAVE_SELINUX) && defined(USE_EXT4)
Dees_Troya95f55c2013-08-17 13:14:43 +00001269 gui_print("Formatting %s using make_ext4fs function.\n", Display_Name.c_str());
Dees_Troya95f55c2013-08-17 13:14:43 +00001270 if (make_ext4fs(Actual_Block_Device.c_str(), Length, Mount_Point.c_str(), selinux_handle) != 0) {
Dees_Troya95f55c2013-08-17 13:14:43 +00001271 LOGERR("Unable to wipe '%s' using function call.\n", Mount_Point.c_str());
1272 return false;
1273 } else {
bigbiff bigbiffc49d7062013-10-11 20:28:00 -04001274 #ifdef HAVE_SELINUX
1275 string sedir = Mount_Point + "/lost+found";
1276 PartitionManager.Mount_By_Path(sedir.c_str(), true);
1277 rmdir(sedir.c_str());
1278 mkdir(sedir.c_str(), S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP);
1279 #endif
Dees_Troya95f55c2013-08-17 13:14:43 +00001280 return true;
1281 }
1282#else
Dees_Troy43d8b002012-09-17 16:00:01 -04001283 if (TWFunc::Path_Exists("/sbin/make_ext4fs")) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001284 string Command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001285
Dees_Troy2673cec2013-04-02 20:22:16 +00001286 gui_print("Formatting %s using make_ext4fs...\n", Display_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001287 Find_Actual_Block_Device();
1288 Command = "make_ext4fs";
1289 if (!Is_Decrypted && Length != 0) {
1290 // Only use length if we're not decrypted
1291 char len[32];
1292 sprintf(len, "%i", Length);
1293 Command += " -l ";
1294 Command += len;
1295 }
Dees_Troy5295d582013-09-06 15:51:08 +00001296 if (TWFunc::Path_Exists("/file_contexts")) {
1297 Command += " -S /file_contexts";
1298 }
1299 Command += " -a " + Mount_Point + " " + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001300 LOGINFO("make_ext4fs command: %s\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001301 if (TWFunc::Exec_Cmd(Command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001302 Current_File_System = "ext4";
Dees_Troye58d5262012-09-21 12:27:57 -04001303 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001304 gui_print("Done.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001305 return true;
1306 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001307 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001308 return false;
1309 }
1310 } else
Gary Peck43acadf2012-11-21 21:19:01 -08001311 return Wipe_EXT23("ext4");
Dees_Troya95f55c2013-08-17 13:14:43 +00001312#endif
Dees_Troy38bd7602012-09-14 13:33:53 -04001313 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001314}
1315
1316bool TWPartition::Wipe_FAT() {
Vojtech Bocek05534202013-09-11 08:11:56 +02001317 string command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001318
Dees_Troy43d8b002012-09-17 16:00:01 -04001319 if (TWFunc::Path_Exists("/sbin/mkdosfs")) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001320 if (!UnMount(true))
1321 return false;
1322
Dees_Troy2673cec2013-04-02 20:22:16 +00001323 gui_print("Formatting %s using mkdosfs...\n", Display_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001324 Find_Actual_Block_Device();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001325 command = "mkdosfs " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02001326 if (TWFunc::Exec_Cmd(command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001327 Current_File_System = "vfat";
Dees_Troye58d5262012-09-21 12:27:57 -04001328 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001329 gui_print("Done.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001330 return true;
1331 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001332 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001333 return false;
1334 }
1335 return true;
1336 }
1337 else
1338 return Wipe_RMRF();
1339
1340 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001341}
1342
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001343bool TWPartition::Wipe_EXFAT() {
Vojtech Bocek05534202013-09-11 08:11:56 +02001344 string command;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001345
1346 if (TWFunc::Path_Exists("/sbin/mkexfatfs")) {
1347 if (!UnMount(true))
1348 return false;
1349
Dees_Troy2673cec2013-04-02 20:22:16 +00001350 gui_print("Formatting %s using mkexfatfs...\n", Display_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001351 Find_Actual_Block_Device();
1352 command = "mkexfatfs " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02001353 if (TWFunc::Exec_Cmd(command) == 0) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001354 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001355 gui_print("Done.\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001356 return true;
1357 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001358 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001359 return false;
1360 }
1361 return true;
1362 }
1363 return false;
1364}
1365
Dees_Troy38bd7602012-09-14 13:33:53 -04001366bool TWPartition::Wipe_MTD() {
1367 if (!UnMount(true))
1368 return false;
1369
Dees_Troy2673cec2013-04-02 20:22:16 +00001370 gui_print("MTD Formatting \"%s\"\n", MTD_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001371
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001372 mtd_scan_partitions();
1373 const MtdPartition* mtd = mtd_find_partition_by_name(MTD_Name.c_str());
1374 if (mtd == NULL) {
1375 LOGERR("No mtd partition named '%s'", MTD_Name.c_str());
1376 return false;
1377 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001378
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001379 MtdWriteContext* ctx = mtd_write_partition(mtd);
1380 if (ctx == NULL) {
1381 LOGERR("Can't write '%s', failed to format.", MTD_Name.c_str());
1382 return false;
1383 }
1384 if (mtd_erase_blocks(ctx, -1) == -1) {
1385 mtd_write_close(ctx);
1386 LOGERR("Failed to format '%s'", MTD_Name.c_str());
1387 return false;
1388 }
1389 if (mtd_write_close(ctx) != 0) {
1390 LOGERR("Failed to close '%s'", MTD_Name.c_str());
1391 return false;
1392 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001393 Current_File_System = "yaffs2";
Dees_Troye58d5262012-09-21 12:27:57 -04001394 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001395 gui_print("Done.\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001396 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001397}
1398
1399bool TWPartition::Wipe_RMRF() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001400 if (!Mount(true))
1401 return false;
1402
Dees_Troy2673cec2013-04-02 20:22:16 +00001403 gui_print("Removing all files under '%s'\n", Mount_Point.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001404 TWFunc::removeDir(Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04001405 Recreate_AndSec_Folder();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001406 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001407}
1408
Dees_Troye5017042013-08-29 16:38:55 +00001409bool TWPartition::Wipe_F2FS() {
Vojtech Bocek05534202013-09-11 08:11:56 +02001410 string command;
Dees_Troye5017042013-08-29 16:38:55 +00001411
1412 if (TWFunc::Path_Exists("/sbin/mkfs.f2fs")) {
1413 if (!UnMount(true))
1414 return false;
1415
1416 gui_print("Formatting %s using mkfs.f2fs...\n", Display_Name.c_str());
1417 Find_Actual_Block_Device();
1418 command = "mkfs.f2fs " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02001419 if (TWFunc::Exec_Cmd(command) == 0) {
Dees_Troye5017042013-08-29 16:38:55 +00001420 Recreate_AndSec_Folder();
1421 gui_print("Done.\n");
1422 return true;
1423 } else {
1424 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
1425 return false;
1426 }
1427 return true;
1428 } else {
1429 gui_print("mkfs.f2fs binary not found, using rm -rf to wipe.\n");
1430 return Wipe_RMRF();
1431 }
1432 return false;
1433}
1434
Dees_Troy51a0e822012-09-05 15:24:24 -04001435bool TWPartition::Wipe_Data_Without_Wiping_Media() {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001436 string dir;
Dees_Troy38bd7602012-09-14 13:33:53 -04001437
1438 // This handles wiping data on devices with "sdcard" in /data/media
1439 if (!Mount(true))
1440 return false;
1441
Dees_Troy2673cec2013-04-02 20:22:16 +00001442 gui_print("Wiping data without wiping /data/media ...\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001443
1444 DIR* d;
1445 d = opendir("/data");
Dees_Troy16b74352012-11-14 22:27:31 +00001446 if (d != NULL) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001447 struct dirent* de;
1448 while ((de = readdir(d)) != NULL) {
Dees_Troy16b74352012-11-14 22:27:31 +00001449 if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) continue;
1450 // The media folder is the "internal sdcard"
1451 // The .layout_version file is responsible for determining whether 4.2 decides up upgrade
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001452 // the media folder for multi-user.
Dees_Troy16b74352012-11-14 22:27:31 +00001453 if (strcmp(de->d_name, "media") == 0 || strcmp(de->d_name, ".layout_version") == 0) continue;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001454
1455 dir = "/data/";
1456 dir.append(de->d_name);
Dees_Troyce675462013-01-09 19:48:21 +00001457 if (de->d_type == DT_DIR) {
1458 TWFunc::removeDir(dir, false);
bigbiff bigbiff98f1f902013-01-19 18:46:13 -05001459 } 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 +00001460 if (!unlink(dir.c_str()))
Dees_Troy2673cec2013-04-02 20:22:16 +00001461 LOGINFO("Unable to unlink '%s'\n", dir.c_str());
Dees_Troyce675462013-01-09 19:48:21 +00001462 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001463 }
1464 closedir(d);
Dees_Troy2673cec2013-04-02 20:22:16 +00001465 gui_print("Done.\n");
Dees_Troy16b74352012-11-14 22:27:31 +00001466 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -04001467 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001468 gui_print("Dirent failed to open /data, error!\n");
Dees_Troy16b74352012-11-14 22:27:31 +00001469 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001470}
1471
1472bool TWPartition::Backup_Tar(string backup_folder) {
Dees_Troy4a2a1262012-09-18 09:33:47 -04001473 char back_name[255], split_index[5];
1474 string Full_FileName, Split_FileName, Tar_Args, Command;
Dees_Troy83bd4832013-05-04 12:39:56 +00001475 int use_compression, use_encryption = 0, index, backup_count;
Dees_Troy4a2a1262012-09-18 09:33:47 -04001476 struct stat st;
Dees_Troy7c2dec82012-09-26 09:49:14 -04001477 unsigned long long total_bsize = 0, file_size;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001478 twrpTar tar;
1479 vector <string> files;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001480
Dees_Troy43d8b002012-09-17 16:00:01 -04001481 if (!Mount(true))
1482 return false;
1483
Dees_Troya13d74f2013-03-24 08:54:55 -05001484 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Backup_Display_Name, "Backing Up");
Dees_Troy2673cec2013-04-02 20:22:16 +00001485 gui_print("Backing up %s...\n", Backup_Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001486
1487 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
Dees_Troy83bd4832013-05-04 12:39:56 +00001488 tar.use_compression = use_compression;
bigbiff bigbiff86e77bc2013-08-26 21:36:23 -04001489 //exclude Google Music Cache
1490 tar.setexcl("/data/data/com.google.android.music/files");
Dees_Troy83bd4832013-05-04 12:39:56 +00001491#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
1492 DataManager::GetValue("tw_encrypt_backup", use_encryption);
1493 if (use_encryption && Can_Encrypt_Backup) {
1494 tar.use_encryption = use_encryption;
1495 if (Use_Userdata_Encryption)
1496 tar.userdata_encryption = use_encryption;
1497 } else {
1498 use_encryption = false;
1499 }
1500#endif
Dees_Troy43d8b002012-09-17 16:00:01 -04001501
1502 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1503 Backup_FileName = back_name;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001504 Full_FileName = backup_folder + "/" + Backup_FileName;
Dees_Troy83bd4832013-05-04 12:39:56 +00001505 tar.has_data_media = Has_Data_Media;
1506 if (!use_encryption && Backup_Size > MAX_ARCHIVE_SIZE) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001507 // This backup needs to be split into multiple archives
Dees_Troy2673cec2013-04-02 20:22:16 +00001508 gui_print("Breaking backup file into multiple archives...\n");
Dees_Troye58d5262012-09-21 12:27:57 -04001509 sprintf(back_name, "%s", Backup_Path.c_str());
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -05001510 tar.setdir(back_name);
1511 tar.setfn(Full_FileName);
bigbiff bigbiffe6594ab2013-02-17 20:18:31 -05001512 backup_count = tar.splitArchiveFork();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001513 if (backup_count == -1) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001514 LOGERR("Error tarring split files!\n");
Dees_Troy4a2a1262012-09-18 09:33:47 -04001515 return false;
1516 }
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001517 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001518 } else {
Dees_Troy4a2a1262012-09-18 09:33:47 -04001519 Full_FileName = backup_folder + "/" + Backup_FileName;
Dees_Troy83bd4832013-05-04 12:39:56 +00001520 tar.setdir(Backup_Path);
1521 tar.setfn(Full_FileName);
1522 if (tar.createTarFork() != 0)
1523 return false;
1524 if (use_compression && !use_encryption) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001525 string gzname = Full_FileName + ".gz";
1526 rename(gzname.c_str(), Full_FileName.c_str());
1527 }
Dees_Troy83bd4832013-05-04 12:39:56 +00001528 if (use_encryption)
1529 Full_FileName += "000";
Dees_Troy7c2dec82012-09-26 09:49:14 -04001530 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001531 LOGERR("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001532 return false;
1533 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001534 }
1535 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001536}
1537
1538bool TWPartition::Backup_DD(string backup_folder) {
igoriok87e3d932013-01-31 21:03:53 +02001539 char back_name[255], backup_size[32];
Vojtech Bocek05534202013-09-11 08:11:56 +02001540 string Full_FileName, Command, DD_BS;
Dees_Troy43d8b002012-09-17 16:00:01 -04001541 int use_compression;
1542
igoriok87e3d932013-01-31 21:03:53 +02001543 sprintf(backup_size, "%llu", Backup_Size);
1544 DD_BS = backup_size;
1545
Dees_Troyb46a6842012-09-25 11:06:46 -04001546 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
Dees_Troy2673cec2013-04-02 20:22:16 +00001547 gui_print("Backing up %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001548
1549 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1550 Backup_FileName = back_name;
1551
1552 Full_FileName = backup_folder + "/" + Backup_FileName;
1553
igoriok87e3d932013-01-31 21:03:53 +02001554 Command = "dd if=" + Actual_Block_Device + " of='" + Full_FileName + "'" + " bs=" + DD_BS + "c count=1";
Dees_Troy2673cec2013-04-02 20:22:16 +00001555 LOGINFO("Backup command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001556 TWFunc::Exec_Cmd(Command);
Dees_Troyc154ac22012-10-12 15:36:47 -04001557 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001558 LOGERR("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001559 return false;
1560 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001561 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001562}
1563
1564bool TWPartition::Backup_Dump_Image(string backup_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001565 char back_name[255];
Vojtech Bocek05534202013-09-11 08:11:56 +02001566 string Full_FileName, Command;
Dees_Troy43d8b002012-09-17 16:00:01 -04001567 int use_compression;
1568
Dees_Troyb46a6842012-09-25 11:06:46 -04001569 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
Dees_Troy2673cec2013-04-02 20:22:16 +00001570 gui_print("Backing up %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001571
1572 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1573 Backup_FileName = back_name;
1574
1575 Full_FileName = backup_folder + "/" + Backup_FileName;
1576
1577 Command = "dump_image " + MTD_Name + " '" + Full_FileName + "'";
Dees_Troy2673cec2013-04-02 20:22:16 +00001578 LOGINFO("Backup command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001579 TWFunc::Exec_Cmd(Command);
Dees_Troy7c2dec82012-09-26 09:49:14 -04001580 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
1581 // 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 +00001582 LOGERR("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001583 return false;
1584 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001585 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001586}
1587
Gary Peck43acadf2012-11-21 21:19:01 -08001588bool TWPartition::Restore_Tar(string restore_folder, string Restore_File_System) {
1589 string Full_FileName, Command;
Dees_Troy4a2a1262012-09-18 09:33:47 -04001590 int index = 0;
1591 char split_index[5];
Dees_Troy43d8b002012-09-17 16:00:01 -04001592
Dees_Troye58d5262012-09-21 12:27:57 -04001593 if (Has_Android_Secure) {
Dees_Troye58d5262012-09-21 12:27:57 -04001594 if (!Wipe_AndSec())
1595 return false;
Gary Peck43acadf2012-11-21 21:19:01 -08001596 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001597 gui_print("Wiping %s...\n", Display_Name.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001598 if (!Wipe(Restore_File_System))
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001599 return false;
Dees_Troye58d5262012-09-21 12:27:57 -04001600 }
Dees_Troya13d74f2013-03-24 08:54:55 -05001601 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Backup_Display_Name, "Restoring");
Dees_Troy2673cec2013-04-02 20:22:16 +00001602 gui_print("Restoring %s...\n", Backup_Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001603
1604 if (!Mount(true))
1605 return false;
1606
Dees_Troy4a2a1262012-09-18 09:33:47 -04001607 Full_FileName = restore_folder + "/" + Backup_FileName;
Dees_Troy83bd4832013-05-04 12:39:56 +00001608 /*if (!TWFunc::Path_Exists(Full_FileName)) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001609 if (!TWFunc::Path_Exists(Full_FileName)) {
1610 // Backup is multiple archives
Dees_Troy2673cec2013-04-02 20:22:16 +00001611 LOGINFO("Backup is multiple archives.\n");
Dees_Troy4a2a1262012-09-18 09:33:47 -04001612 sprintf(split_index, "%03i", index);
1613 Full_FileName = restore_folder + "/" + Backup_FileName + split_index;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001614 while (TWFunc::Path_Exists(Full_FileName)) {
1615 index++;
Dees_Troy2673cec2013-04-02 20:22:16 +00001616 gui_print("Restoring archive %i...\n", index);
1617 LOGINFO("Restoring '%s'...\n", Full_FileName.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001618 twrpTar tar;
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -05001619 tar.setdir("/");
1620 tar.setfn(Full_FileName);
bigbiff bigbiffe6594ab2013-02-17 20:18:31 -05001621 if (tar.extractTarFork() != 0)
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001622 return false;
1623 sprintf(split_index, "%03i", index);
1624 Full_FileName = restore_folder + "/" + Backup_FileName + split_index;
1625 }
1626 if (index == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001627 LOGERR("Error locating restore file: '%s'\n", Full_FileName.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001628 return false;
1629 }
Dees_Troy4a2a1262012-09-18 09:33:47 -04001630 }
Dees_Troy83bd4832013-05-04 12:39:56 +00001631 } else {*/
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001632 twrpTar tar;
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -05001633 tar.setdir(Backup_Path);
1634 tar.setfn(Full_FileName);
Dees_Troy83bd4832013-05-04 12:39:56 +00001635 tar.backup_name = Backup_Name;
bigbiff bigbiffe6594ab2013-02-17 20:18:31 -05001636 if (tar.extractTarFork() != 0)
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001637 return false;
Dees_Troy83bd4832013-05-04 12:39:56 +00001638 //}
Dees_Troy43d8b002012-09-17 16:00:01 -04001639 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001640}
1641
1642bool TWPartition::Restore_DD(string restore_folder) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001643 string Full_FileName, Command;
Dees_Troy43d8b002012-09-17 16:00:01 -04001644
Dees_Troyda8b55a2012-12-12 19:18:30 +00001645 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
Dees_Troy43d8b002012-09-17 16:00:01 -04001646 Full_FileName = restore_folder + "/" + Backup_FileName;
Gary Peck15e623d2012-11-21 21:07:58 -08001647
1648 if (!Find_Partition_Size()) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001649 LOGERR("Unable to find partition size for '%s'\n", Mount_Point.c_str());
Gary Peck15e623d2012-11-21 21:07:58 -08001650 return false;
1651 }
1652 unsigned long long backup_size = TWFunc::Get_File_Size(Full_FileName);
1653 if (backup_size > Size) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001654 LOGERR("Size (%iMB) of backup '%s' is larger than target device '%s' (%iMB)\n",
Gary Peck15e623d2012-11-21 21:07:58 -08001655 (int)(backup_size / 1048576LLU), Full_FileName.c_str(),
1656 Actual_Block_Device.c_str(), (int)(Size / 1048576LLU));
1657 return false;
1658 }
1659
Dees_Troy2673cec2013-04-02 20:22:16 +00001660 gui_print("Restoring %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001661 Command = "dd bs=4096 if='" + Full_FileName + "' of=" + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001662 LOGINFO("Restore command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001663 TWFunc::Exec_Cmd(Command);
Dees_Troy43d8b002012-09-17 16:00:01 -04001664 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001665}
1666
1667bool TWPartition::Restore_Flash_Image(string restore_folder) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001668 string Full_FileName, Command;
Dees_Troy43d8b002012-09-17 16:00:01 -04001669
Dees_Troy2673cec2013-04-02 20:22:16 +00001670 gui_print("Restoring %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001671 Full_FileName = restore_folder + "/" + Backup_FileName;
1672 // Sometimes flash image doesn't like to flash due to the first 2KB matching, so we erase first to ensure that it flashes
1673 Command = "erase_image " + MTD_Name;
Dees_Troy2673cec2013-04-02 20:22:16 +00001674 LOGINFO("Erase command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001675 TWFunc::Exec_Cmd(Command);
Dees_Troy43d8b002012-09-17 16:00:01 -04001676 Command = "flash_image " + MTD_Name + " '" + Full_FileName + "'";
Dees_Troy2673cec2013-04-02 20:22:16 +00001677 LOGINFO("Restore command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001678 TWFunc::Exec_Cmd(Command);
Dees_Troy43d8b002012-09-17 16:00:01 -04001679 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001680}
Dees_Troy5bf43922012-09-07 16:07:55 -04001681
1682bool TWPartition::Update_Size(bool Display_Error) {
Dees_Troy0550cfb2012-10-13 11:56:13 -04001683 bool ret = false, Was_Already_Mounted = false;
Dees_Troy51127312012-09-08 13:08:49 -04001684
Dees_Troyab10ee22012-09-21 14:27:30 -04001685 if (!Can_Be_Mounted && !Is_Encrypted)
Dees_Troy5bf43922012-09-07 16:07:55 -04001686 return false;
1687
Dees_Troy0550cfb2012-10-13 11:56:13 -04001688 Was_Already_Mounted = Is_Mounted();
Dees_Troy38bd7602012-09-14 13:33:53 -04001689 if (Removable || Is_Encrypted) {
1690 if (!Mount(false))
1691 return true;
1692 } else if (!Mount(Display_Error))
Dees_Troy5bf43922012-09-07 16:07:55 -04001693 return false;
Dees_Troy51127312012-09-08 13:08:49 -04001694
1695 ret = Get_Size_Via_statfs(Display_Error);
Dees_Troy0550cfb2012-10-13 11:56:13 -04001696 if (!ret || Size == 0) {
1697 if (!Get_Size_Via_df(Display_Error)) {
1698 if (!Was_Already_Mounted)
1699 UnMount(false);
Dees_Troy51127312012-09-08 13:08:49 -04001700 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04001701 }
1702 }
Dees_Troy51127312012-09-08 13:08:49 -04001703
Dees_Troy5bf43922012-09-07 16:07:55 -04001704 if (Has_Data_Media) {
1705 if (Mount(Display_Error)) {
Dees_Troy51127312012-09-08 13:08:49 -04001706 unsigned long long data_media_used, actual_data;
Dees_Troy43d8b002012-09-17 16:00:01 -04001707 Used = TWFunc::Get_Folder_Size("/data", Display_Error);
1708 data_media_used = TWFunc::Get_Folder_Size("/data/media", Display_Error);
Dees_Troy51127312012-09-08 13:08:49 -04001709 actual_data = Used - data_media_used;
Dees_Troy5bf43922012-09-07 16:07:55 -04001710 Backup_Size = actual_data;
Dees_Troy51127312012-09-08 13:08:49 -04001711 int bak = (int)(Backup_Size / 1048576LLU);
1712 int total = (int)(Size / 1048576LLU);
1713 int us = (int)(Used / 1048576LLU);
1714 int fre = (int)(Free / 1048576LLU);
1715 int datmed = (int)(data_media_used / 1048576LLU);
Dees_Troy2673cec2013-04-02 20:22:16 +00001716 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 -04001717 } else {
1718 if (!Was_Already_Mounted)
1719 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04001720 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04001721 }
Dees_Troye58d5262012-09-21 12:27:57 -04001722 } else if (Has_Android_Secure) {
1723 if (Mount(Display_Error))
1724 Backup_Size = TWFunc::Get_Folder_Size(Backup_Path, Display_Error);
Dees_Troy0550cfb2012-10-13 11:56:13 -04001725 else {
1726 if (!Was_Already_Mounted)
1727 UnMount(false);
Dees_Troye58d5262012-09-21 12:27:57 -04001728 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04001729 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001730 }
Dees_Troy0550cfb2012-10-13 11:56:13 -04001731 if (!Was_Already_Mounted)
1732 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04001733 return true;
Dees_Troy51127312012-09-08 13:08:49 -04001734}
Dees_Troy38bd7602012-09-14 13:33:53 -04001735
1736void TWPartition::Find_Actual_Block_Device(void) {
1737 if (Is_Decrypted) {
1738 Actual_Block_Device = Decrypted_Block_Device;
Dees_Troy43d8b002012-09-17 16:00:01 -04001739 if (TWFunc::Path_Exists(Primary_Block_Device))
Dees_Troy38bd7602012-09-14 13:33:53 -04001740 Is_Present = true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001741 } else if (TWFunc::Path_Exists(Primary_Block_Device)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001742 Is_Present = true;
1743 Actual_Block_Device = Primary_Block_Device;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001744 return;
1745 }
1746 if (Is_Decrypted) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001747 } else if (!Alternate_Block_Device.empty() && TWFunc::Path_Exists(Alternate_Block_Device)) {
Dees_Troy3f04d032012-10-07 18:20:09 -04001748 Actual_Block_Device = Alternate_Block_Device;
Dees_Troy38bd7602012-09-14 13:33:53 -04001749 Is_Present = true;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001750 } else {
Dees_Troy38bd7602012-09-14 13:33:53 -04001751 Is_Present = false;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001752 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001753}
1754
1755void TWPartition::Recreate_Media_Folder(void) {
1756 string Command;
1757
1758 if (!Mount(true)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001759 LOGERR("Unable to recreate /data/media folder.\n");
Dees_Troyb46a6842012-09-25 11:06:46 -04001760 } else if (!TWFunc::Path_Exists("/data/media")) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001761 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Dees_Troy2673cec2013-04-02 20:22:16 +00001762 LOGINFO("Recreating /data/media folder.\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001763 mkdir("/data/media", S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
1764 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Dees_Troy38bd7602012-09-14 13:33:53 -04001765 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001766}
Dees_Troye58d5262012-09-21 12:27:57 -04001767
1768void TWPartition::Recreate_AndSec_Folder(void) {
Dees_Troye58d5262012-09-21 12:27:57 -04001769 if (!Has_Android_Secure)
1770 return;
Dees_Troy2673cec2013-04-02 20:22:16 +00001771 LOGINFO("Creating %s: %s\n", Backup_Display_Name.c_str(), Symlink_Path.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -04001772 if (!Mount(true)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001773 LOGERR("Unable to recreate %s folder.\n", Backup_Name.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -04001774 } else if (!TWFunc::Path_Exists(Symlink_Path)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001775 LOGINFO("Recreating %s folder.\n", Backup_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001776 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
1777 mkdir(Symlink_Path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
1778 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04001779 }
1780}