blob: be4c3a3ee233d9600d24908846745946f38d337a [file] [log] [blame]
Dees_Troy51a0e822012-09-05 15:24:24 -04001/* Partition class for TWRP
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15 * 02110-1301, USA.
16 *
17 * The code was written from scratch by Dees_Troy dees_troy at
18 * yahoo
19 *
20 * Copyright (c) 2012
21 */
22
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26#include <sys/stat.h>
27#include <sys/vfs.h>
Dees_Troy5bf43922012-09-07 16:07:55 -040028#include <sys/mount.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040029#include <unistd.h>
Dees_Troy51127312012-09-08 13:08:49 -040030#include <dirent.h>
bigbiff bigbiff9c754052013-01-09 09:09:08 -050031#include <iostream>
32#include <sstream>
Dees_Troy51a0e822012-09-05 15:24:24 -040033
Dees_Troy657c3092012-09-10 20:32:10 -040034#ifdef TW_INCLUDE_CRYPTO
35 #include "cutils/properties.h"
36#endif
37
bigbiff bigbiffe60683a2013-02-22 20:55:50 -050038#include "libblkid/blkid.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040039#include "variables.h"
Dees_Troy2673cec2013-04-02 20:22:16 +000040#include "twcommon.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040041#include "partitions.hpp"
Dees_Troy5bf43922012-09-07 16:07:55 -040042#include "data.hpp"
Dees_Troy43d8b002012-09-17 16:00:01 -040043#include "twrp-functions.hpp"
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -050044#include "twrpDigest.hpp"
bigbiff bigbiff9c754052013-01-09 09:09:08 -050045#include "twrpTar.hpp"
Dees_Troy5bf43922012-09-07 16:07:55 -040046extern "C" {
Dees_Troy38bd7602012-09-14 13:33:53 -040047 #include "mtdutils/mtdutils.h"
48 #include "mtdutils/mounts.h"
Dees_Troy85f44ed2013-01-09 18:42:36 +000049#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
50 #include "crypto/libcrypt_samsung/include/libcrypt_samsung.h"
51#endif
Dees_Troya95f55c2013-08-17 13:14:43 +000052#ifdef USE_EXT4
53 #include "make_ext4fs.h"
54#endif
Dees_Troy5bf43922012-09-07 16:07:55 -040055}
Dees_Troy51a0e822012-09-05 15:24:24 -040056
bigbiff bigbiff9c754052013-01-09 09:09:08 -050057using namespace std;
58
Dees_Troya95f55c2013-08-17 13:14:43 +000059extern struct selabel_handle *selinux_handle;
60
Dees_Troy51a0e822012-09-05 15:24:24 -040061TWPartition::TWPartition(void) {
62 Can_Be_Mounted = false;
63 Can_Be_Wiped = false;
Dees_Troya13d74f2013-03-24 08:54:55 -050064 Can_Be_Backed_Up = false;
Vojtech Bocek1dc30982013-08-30 21:49:30 +020065 Use_Rm_Rf = false;
Dees_Troy51a0e822012-09-05 15:24:24 -040066 Wipe_During_Factory_Reset = false;
67 Wipe_Available_in_GUI = false;
68 Is_SubPartition = false;
Dees_Troy2691f9d2012-09-24 11:15:49 -040069 Has_SubPartition = false;
Dees_Troy51a0e822012-09-05 15:24:24 -040070 SubPartition_Of = "";
71 Symlink_Path = "";
72 Symlink_Mount_Point = "";
73 Mount_Point = "";
Dees_Troye58d5262012-09-21 12:27:57 -040074 Backup_Path = "";
Dees_Troy38bd7602012-09-14 13:33:53 -040075 Actual_Block_Device = "";
76 Primary_Block_Device = "";
Dees_Troy51a0e822012-09-05 15:24:24 -040077 Alternate_Block_Device = "";
78 Removable = false;
79 Is_Present = false;
80 Length = 0;
81 Size = 0;
82 Used = 0;
83 Free = 0;
84 Backup_Size = 0;
85 Can_Be_Encrypted = false;
86 Is_Encrypted = false;
87 Is_Decrypted = false;
88 Decrypted_Block_Device = "";
89 Display_Name = "";
Dees_Troya13d74f2013-03-24 08:54:55 -050090 Backup_Display_Name = "";
91 Storage_Name = "";
Dees_Troy51a0e822012-09-05 15:24:24 -040092 Backup_Name = "";
Dees_Troy63c8df72012-09-10 14:02:05 -040093 Backup_FileName = "";
Dees_Troy38bd7602012-09-14 13:33:53 -040094 MTD_Name = "";
Dees_Troy51a0e822012-09-05 15:24:24 -040095 Backup_Method = NONE;
Dees_Troy83bd4832013-05-04 12:39:56 +000096 Can_Encrypt_Backup = false;
97 Use_Userdata_Encryption = false;
Dees_Troy51a0e822012-09-05 15:24:24 -040098 Has_Data_Media = false;
Dees_Troye58d5262012-09-21 12:27:57 -040099 Has_Android_Secure = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400100 Is_Storage = false;
Dees_Troya13d74f2013-03-24 08:54:55 -0500101 Is_Settings_Storage = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400102 Storage_Path = "";
103 Current_File_System = "";
104 Fstab_File_System = "";
105 Format_Block_Size = 0;
Dees_Troy68cab492012-12-12 19:29:35 +0000106 Ignore_Blkid = false;
Dees_Troy16c2b312013-01-15 16:51:18 +0000107 Retain_Layout_Version = false;
Dees_Troy85f44ed2013-01-09 18:42:36 +0000108#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
109 EcryptFS_Password = "";
110#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400111}
112
113TWPartition::~TWPartition(void) {
114 // Do nothing
115}
116
Dees_Troy5bf43922012-09-07 16:07:55 -0400117bool TWPartition::Process_Fstab_Line(string Line, bool Display_Error) {
118 char full_line[MAX_FSTAB_LINE_LENGTH], item[MAX_FSTAB_LINE_LENGTH];
119 int line_len = Line.size(), index = 0, item_index = 0;
120 char* ptr;
Dees_Troy51127312012-09-08 13:08:49 -0400121 string Flags;
Dees_Troy5bf43922012-09-07 16:07:55 -0400122 strncpy(full_line, Line.c_str(), line_len);
Dees_Troya13d74f2013-03-24 08:54:55 -0500123 bool skip = false;
Dees_Troy5bf43922012-09-07 16:07:55 -0400124
Dees_Troy51127312012-09-08 13:08:49 -0400125 for (index = 0; index < line_len; index++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500126 if (full_line[index] == 34)
127 skip = !skip;
128 if (!skip && full_line[index] <= 32)
Dees_Troy5bf43922012-09-07 16:07:55 -0400129 full_line[index] = '\0';
Dees_Troy5bf43922012-09-07 16:07:55 -0400130 }
Dees_Troy7c2dec82012-09-26 09:49:14 -0400131 Mount_Point = full_line;
Dees_Troy2673cec2013-04-02 20:22:16 +0000132 LOGINFO("Processing '%s'\n", Mount_Point.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -0400133 Backup_Path = Mount_Point;
Dees_Troya13d74f2013-03-24 08:54:55 -0500134 Storage_Path = Mount_Point;
Dees_Troy70737fa2013-04-08 13:19:20 +0000135 Display_Name = full_line + 1;
136 Backup_Display_Name = Display_Name;
137 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400138 index = Mount_Point.size();
139 while (index < line_len) {
140 while (index < line_len && full_line[index] == '\0')
141 index++;
142 if (index >= line_len)
143 continue;
144 ptr = full_line + index;
145 if (item_index == 0) {
146 // File System
147 Fstab_File_System = ptr;
148 Current_File_System = ptr;
149 item_index++;
150 } else if (item_index == 1) {
151 // Primary Block Device
Dees_Troy38bd7602012-09-14 13:33:53 -0400152 if (Fstab_File_System == "mtd" || Fstab_File_System == "yaffs2") {
Dees_Troy094207a2012-09-26 12:00:39 -0400153 MTD_Name = ptr;
154 Find_MTD_Block_Device(MTD_Name);
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400155 } else if (Fstab_File_System == "bml") {
156 if (Mount_Point == "/boot")
157 MTD_Name = "boot";
158 else if (Mount_Point == "/recovery")
159 MTD_Name = "recovery";
160 Primary_Block_Device = ptr;
161 if (*ptr != '/')
Dees_Troy2673cec2013-04-02 20:22:16 +0000162 LOGERR("Until we get better BML support, you will have to find and provide the full block device path to the BML devices e.g. /dev/block/bml9 instead of the partition name\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400163 } else if (*ptr != '/') {
Dees_Troy5bf43922012-09-07 16:07:55 -0400164 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000165 LOGERR("Invalid block device on '%s', '%s', %i\n", Line.c_str(), ptr, index);
Dees_Troy5bf43922012-09-07 16:07:55 -0400166 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000167 LOGINFO("Invalid block device on '%s', '%s', %i\n", Line.c_str(), ptr, index);
Dees_Troy5bf43922012-09-07 16:07:55 -0400168 return 0;
Dees_Troy38bd7602012-09-14 13:33:53 -0400169 } else {
170 Primary_Block_Device = ptr;
171 Find_Real_Block_Device(Primary_Block_Device, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400172 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400173 item_index++;
174 } else if (item_index > 1) {
175 if (*ptr == '/') {
176 // Alternate Block Device
177 Alternate_Block_Device = ptr;
178 Find_Real_Block_Device(Alternate_Block_Device, Display_Error);
179 } else if (strlen(ptr) > 7 && strncmp(ptr, "length=", 7) == 0) {
180 // Partition length
181 ptr += 7;
182 Length = atoi(ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400183 } else if (strlen(ptr) > 6 && strncmp(ptr, "flags=", 6) == 0) {
184 // Custom flags, save for later so that new values aren't overwritten by defaults
185 ptr += 6;
186 Flags = ptr;
Dees_Troy68cab492012-12-12 19:29:35 +0000187 Process_Flags(Flags, Display_Error);
Dees_Troy38bd7602012-09-14 13:33:53 -0400188 } else if (strlen(ptr) == 4 && (strncmp(ptr, "NULL", 4) == 0 || strncmp(ptr, "null", 4) == 0 || strncmp(ptr, "null", 4) == 0)) {
189 // Do nothing
Dees_Troy5bf43922012-09-07 16:07:55 -0400190 } else {
191 // Unhandled data
Dees_Troy2673cec2013-04-02 20:22:16 +0000192 LOGINFO("Unhandled fstab information: '%s', %i, line: '%s'\n", ptr, index, Line.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400193 }
194 }
195 while (index < line_len && full_line[index] != '\0')
196 index++;
197 }
198
199 if (!Is_File_System(Fstab_File_System) && !Is_Image(Fstab_File_System)) {
200 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000201 LOGERR("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400202 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000203 LOGINFO("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400204 return 0;
205 } else if (Is_File_System(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400206 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400207 Setup_File_System(Display_Error);
208 if (Mount_Point == "/system") {
209 Display_Name = "System";
Dees_Troya13d74f2013-03-24 08:54:55 -0500210 Backup_Display_Name = Display_Name;
211 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400212 Wipe_Available_in_GUI = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500213 Can_Be_Backed_Up = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400214 } else if (Mount_Point == "/data") {
215 Display_Name = "Data";
Dees_Troya13d74f2013-03-24 08:54:55 -0500216 Backup_Display_Name = Display_Name;
217 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400218 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400219 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500220 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000221 Can_Encrypt_Backup = true;
222 Use_Userdata_Encryption = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400223#ifdef RECOVERY_SDCARD_ON_DATA
Dees_Troya13d74f2013-03-24 08:54:55 -0500224 Storage_Name = "Internal Storage";
Dees_Troy5bf43922012-09-07 16:07:55 -0400225 Has_Data_Media = true;
Dees_Troy51127312012-09-08 13:08:49 -0400226 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500227 Is_Settings_Storage = true;
Dees_Troy51127312012-09-08 13:08:49 -0400228 Storage_Path = "/data/media";
Dees_Troy16b74352012-11-14 22:27:31 +0000229 Symlink_Path = Storage_Path;
Dees_Troy657c3092012-09-10 20:32:10 -0400230 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
231 Make_Dir("/emmc", Display_Error);
Dees_Troy657c3092012-09-10 20:32:10 -0400232 Symlink_Mount_Point = "/emmc";
233 } else {
234 Make_Dir("/sdcard", Display_Error);
Dees_Troy657c3092012-09-10 20:32:10 -0400235 Symlink_Mount_Point = "/sdcard";
236 }
Dees_Troy16b74352012-11-14 22:27:31 +0000237 if (Mount(false) && TWFunc::Path_Exists("/data/media/0")) {
238 Storage_Path = "/data/media/0";
239 Symlink_Path = Storage_Path;
240 DataManager::SetValue(TW_INTERNAL_PATH, "/data/media/0");
241 UnMount(true);
242 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400243#endif
244#ifdef TW_INCLUDE_CRYPTO
245 Can_Be_Encrypted = true;
Dees_Troy657c3092012-09-10 20:32:10 -0400246 char crypto_blkdev[255];
247 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
248 if (strcmp(crypto_blkdev, "error") != 0) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400249 DataManager::SetValue(TW_DATA_BLK_DEVICE, Primary_Block_Device);
Dees_Troy657c3092012-09-10 20:32:10 -0400250 DataManager::SetValue(TW_IS_DECRYPTED, 1);
251 Is_Encrypted = true;
252 Is_Decrypted = true;
253 Decrypted_Block_Device = crypto_blkdev;
Dees_Troy2673cec2013-04-02 20:22:16 +0000254 LOGINFO("Data already decrypted, new block device: '%s'\n", crypto_blkdev);
Dees_Troy657c3092012-09-10 20:32:10 -0400255 } else if (!Mount(false)) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400256 Is_Encrypted = true;
257 Is_Decrypted = false;
Gary Peck82599a82012-11-21 16:23:12 -0800258 Can_Be_Mounted = false;
259 Current_File_System = "emmc";
260 Setup_Image(Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400261 DataManager::SetValue(TW_IS_ENCRYPTED, 1);
262 DataManager::SetValue(TW_CRYPTO_PASSWORD, "");
263 DataManager::SetValue("tw_crypto_display", "");
Gary Peck82599a82012-11-21 16:23:12 -0800264 } else {
265 // Filesystem is not encrypted and the mount
266 // succeeded, so get it back to the original
267 // unmounted state
268 UnMount(false);
Dees_Troy51127312012-09-08 13:08:49 -0400269 }
Dees_Troy9b21af72012-10-01 15:51:46 -0400270 #ifdef RECOVERY_SDCARD_ON_DATA
271 if (!Is_Encrypted || (Is_Encrypted && Is_Decrypted))
272 Recreate_Media_Folder();
273 #endif
274#else
275 #ifdef RECOVERY_SDCARD_ON_DATA
276 Recreate_Media_Folder();
277 #endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400278#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400279 } else if (Mount_Point == "/cache") {
280 Display_Name = "Cache";
Dees_Troya13d74f2013-03-24 08:54:55 -0500281 Backup_Display_Name = Display_Name;
282 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400283 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400284 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500285 Can_Be_Backed_Up = true;
Dees_Troyce2fe772012-09-28 12:34:33 -0400286 if (Mount(false) && !TWFunc::Path_Exists("/cache/recovery/.")) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000287 LOGINFO("Recreating /cache/recovery folder.\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500288 if (mkdir("/cache/recovery", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0)
289 return -1;
Dees_Troyb46a6842012-09-25 11:06:46 -0400290 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400291 } else if (Mount_Point == "/datadata") {
Dees_Troy51127312012-09-08 13:08:49 -0400292 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400293 Display_Name = "DataData";
Dees_Troya13d74f2013-03-24 08:54:55 -0500294 Backup_Display_Name = Display_Name;
295 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400296 Is_SubPartition = true;
297 SubPartition_Of = "/data";
Dees_Troy5bf43922012-09-07 16:07:55 -0400298 DataManager::SetValue(TW_HAS_DATADATA, 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500299 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000300 Can_Encrypt_Backup = true;
301 Use_Userdata_Encryption = false; // This whole partition should be encrypted
Dees_Troy5bf43922012-09-07 16:07:55 -0400302 } else if (Mount_Point == "/sd-ext") {
Dees_Troy51127312012-09-08 13:08:49 -0400303 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400304 Display_Name = "SD-Ext";
Dees_Troya13d74f2013-03-24 08:54:55 -0500305 Backup_Display_Name = Display_Name;
306 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400307 Wipe_Available_in_GUI = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400308 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500309 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000310 Can_Encrypt_Backup = true;
311 Use_Userdata_Encryption = true;
Dees_Troy2c50e182012-09-26 20:05:28 -0400312 } else if (Mount_Point == "/boot") {
313 Display_Name = "Boot";
Dees_Troya13d74f2013-03-24 08:54:55 -0500314 Backup_Display_Name = Display_Name;
Dees_Troy2c50e182012-09-26 20:05:28 -0400315 DataManager::SetValue("tw_boot_is_mountable", 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500316 Can_Be_Backed_Up = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400317 }
318#ifdef TW_EXTERNAL_STORAGE_PATH
319 if (Mount_Point == EXPAND(TW_EXTERNAL_STORAGE_PATH)) {
320 Is_Storage = true;
321 Storage_Path = EXPAND(TW_EXTERNAL_STORAGE_PATH);
Dees_Troyc51f1f92012-09-20 15:32:13 -0400322 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500323 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400324#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000325 if (Mount_Point == "/sdcard" || Mount_Point == "/external_sd" || Mount_Point == "/external_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400326 Is_Storage = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400327 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500328 Wipe_Available_in_GUI = true;
Dees_Troye58d5262012-09-21 12:27:57 -0400329#ifndef RECOVERY_SDCARD_ON_DATA
330 Setup_AndSec();
Dees_Troy8e337f32012-10-13 22:07:49 -0400331 Mount_Storage_Retry();
Dees_Troye58d5262012-09-21 12:27:57 -0400332#endif
Dees_Troy8170a922012-09-18 15:40:25 -0400333#endif
Dees_Troyb05ddee2013-01-28 20:24:50 +0000334 }
Dees_Troy8170a922012-09-18 15:40:25 -0400335#ifdef TW_INTERNAL_STORAGE_PATH
336 if (Mount_Point == EXPAND(TW_INTERNAL_STORAGE_PATH)) {
337 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500338 Is_Settings_Storage = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400339 Storage_Path = EXPAND(TW_INTERNAL_STORAGE_PATH);
Dees_Troya13d74f2013-03-24 08:54:55 -0500340 Wipe_Available_in_GUI = true;
Dees_Troye58d5262012-09-21 12:27:57 -0400341#ifndef RECOVERY_SDCARD_ON_DATA
342 Setup_AndSec();
Dees_Troy8e337f32012-10-13 22:07:49 -0400343 Mount_Storage_Retry();
Dees_Troye58d5262012-09-21 12:27:57 -0400344#endif
Dees_Troy8170a922012-09-18 15:40:25 -0400345 }
346#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000347 if (Mount_Point == "/emmc" || Mount_Point == "/internal_sd" || Mount_Point == "/internal_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400348 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500349 Is_Settings_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500350 Wipe_Available_in_GUI = true;
Dees_Troye58d5262012-09-21 12:27:57 -0400351#ifndef RECOVERY_SDCARD_ON_DATA
352 Setup_AndSec();
Dees_Troy8e337f32012-10-13 22:07:49 -0400353 Mount_Storage_Retry();
Dees_Troye58d5262012-09-21 12:27:57 -0400354#endif
Dees_Troy8170a922012-09-18 15:40:25 -0400355 }
356#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400357 } else if (Is_Image(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400358 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400359 Setup_Image(Display_Error);
Dees_Troya13d74f2013-03-24 08:54:55 -0500360 if (Mount_Point == "/boot") {
361 Display_Name = "Boot";
362 Backup_Display_Name = Display_Name;
363 Can_Be_Backed_Up = true;
364 } else if (Mount_Point == "/recovery") {
365 Display_Name = "Recovery";
366 Backup_Display_Name = Display_Name;
367 Can_Be_Backed_Up = true;
368 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400369 }
370
Dees_Troy51127312012-09-08 13:08:49 -0400371 // Process any custom flags
372 if (Flags.size() > 0)
373 Process_Flags(Flags, Display_Error);
Dees_Troy51127312012-09-08 13:08:49 -0400374 return true;
375}
376
377bool TWPartition::Process_Flags(string Flags, bool Display_Error) {
378 char flags[MAX_FSTAB_LINE_LENGTH];
Dees_Troya13d74f2013-03-24 08:54:55 -0500379 int flags_len, index = 0, ptr_len;
Dees_Troy51127312012-09-08 13:08:49 -0400380 char* ptr;
Dees_Troya13d74f2013-03-24 08:54:55 -0500381 bool skip = false, has_display_name = false, has_storage_name = false, has_backup_name = false;
Dees_Troy51127312012-09-08 13:08:49 -0400382
383 strcpy(flags, Flags.c_str());
384 flags_len = Flags.size();
385 for (index = 0; index < flags_len; index++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500386 if (flags[index] == 34)
387 skip = !skip;
388 if (!skip && flags[index] == ';')
Dees_Troy51127312012-09-08 13:08:49 -0400389 flags[index] = '\0';
390 }
391
392 index = 0;
393 while (index < flags_len) {
394 while (index < flags_len && flags[index] == '\0')
395 index++;
396 if (index >= flags_len)
397 continue;
398 ptr = flags + index;
Dees_Troya13d74f2013-03-24 08:54:55 -0500399 ptr_len = strlen(ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400400 if (strcmp(ptr, "removable") == 0) {
401 Removable = true;
402 } else if (strcmp(ptr, "storage") == 0) {
403 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500404 } else if (strcmp(ptr, "settingsstorage") == 0) {
405 Is_Storage = true;
Dees_Troy63c8df72012-09-10 14:02:05 -0400406 } else if (strcmp(ptr, "canbewiped") == 0) {
407 Can_Be_Wiped = true;
Hashcodedabfd492013-08-29 22:45:30 -0700408 } else if (strcmp(ptr, "usermrf") == 0) {
409 Use_Rm_Rf = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500410 } else if (ptr_len > 7 && strncmp(ptr, "backup=", 7) == 0) {
411 ptr += 7;
412 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y')
413 Can_Be_Backed_Up = true;
414 else
415 Can_Be_Backed_Up = false;
Dees_Troy63c8df72012-09-10 14:02:05 -0400416 } else if (strcmp(ptr, "wipeingui") == 0) {
417 Can_Be_Wiped = true;
418 Wipe_Available_in_GUI = true;
419 } else if (strcmp(ptr, "wipeduringfactoryreset") == 0) {
420 Can_Be_Wiped = true;
421 Wipe_Available_in_GUI = true;
422 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500423 } else if (ptr_len > 15 && strncmp(ptr, "subpartitionof=", 15) == 0) {
Dees_Troy2c4c26f2013-01-28 15:26:43 +0000424 ptr += 15;
Dees_Troy51127312012-09-08 13:08:49 -0400425 Is_SubPartition = true;
426 SubPartition_Of = ptr;
Dees_Troy68cab492012-12-12 19:29:35 +0000427 } else if (strcmp(ptr, "ignoreblkid") == 0) {
428 Ignore_Blkid = true;
Dees_Troy16c2b312013-01-15 16:51:18 +0000429 } else if (strcmp(ptr, "retainlayoutversion") == 0) {
430 Retain_Layout_Version = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500431 } else if (ptr_len > 8 && strncmp(ptr, "symlink=", 8) == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400432 ptr += 8;
433 Symlink_Path = ptr;
Dees_Troya13d74f2013-03-24 08:54:55 -0500434 } else if (ptr_len > 8 && strncmp(ptr, "display=", 8) == 0) {
435 has_display_name = true;
Dees_Troy51127312012-09-08 13:08:49 -0400436 ptr += 8;
Dees_Troya13d74f2013-03-24 08:54:55 -0500437 if (*ptr == '\"') ptr++;
Dees_Troy51127312012-09-08 13:08:49 -0400438 Display_Name = ptr;
Dees_Troya13d74f2013-03-24 08:54:55 -0500439 if (Display_Name.substr(Display_Name.size() - 1, 1) == "\"") {
440 Display_Name.resize(Display_Name.size() - 1);
441 }
442 } else if (ptr_len > 11 && strncmp(ptr, "storagename=", 11) == 0) {
443 has_storage_name = true;
444 ptr += 11;
445 if (*ptr == '\"') ptr++;
446 Storage_Name = ptr;
447 if (Storage_Name.substr(Storage_Name.size() - 1, 1) == "\"") {
448 Storage_Name.resize(Storage_Name.size() - 1);
449 }
450 } else if (ptr_len > 11 && strncmp(ptr, "backupname=", 10) == 0) {
451 has_backup_name = true;
452 ptr += 10;
453 if (*ptr == '\"') ptr++;
454 Backup_Display_Name = ptr;
455 if (Backup_Display_Name.substr(Backup_Display_Name.size() - 1, 1) == "\"") {
456 Backup_Display_Name.resize(Backup_Display_Name.size() - 1);
457 }
458 } else if (ptr_len > 10 && strncmp(ptr, "blocksize=", 10) == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400459 ptr += 10;
460 Format_Block_Size = atoi(ptr);
Dees_Troya13d74f2013-03-24 08:54:55 -0500461 } else if (ptr_len > 7 && strncmp(ptr, "length=", 7) == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400462 ptr += 7;
463 Length = atoi(ptr);
Dees_Troy83bd4832013-05-04 12:39:56 +0000464 } else if (ptr_len > 17 && strncmp(ptr, "canencryptbackup=", 17) == 0) {
465 ptr += 17;
466 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y')
467 Can_Encrypt_Backup = true;
468 else
469 Can_Encrypt_Backup = false;
470 } else if (ptr_len > 21 && strncmp(ptr, "userdataencryptbackup=", 21) == 0) {
471 ptr += 21;
472 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y') {
473 Can_Encrypt_Backup = true;
474 Use_Userdata_Encryption = true;
475 } else {
476 Use_Userdata_Encryption = false;
477 }
Dees_Troy51127312012-09-08 13:08:49 -0400478 } else {
479 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000480 LOGERR("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400481 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000482 LOGINFO("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400483 }
484 while (index < flags_len && flags[index] != '\0')
485 index++;
486 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500487 if (has_display_name && !has_storage_name)
488 Storage_Name = Display_Name;
489 if (!has_display_name && has_storage_name)
490 Display_Name = Storage_Name;
Dees_Troy74fb2e92013-04-15 14:35:47 +0000491 if (has_display_name && !has_backup_name && Backup_Display_Name != "Android Secure")
Dees_Troya13d74f2013-03-24 08:54:55 -0500492 Backup_Display_Name = Display_Name;
493 if (!has_display_name && has_backup_name)
494 Display_Name = Backup_Display_Name;
Dees_Troy51127312012-09-08 13:08:49 -0400495 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400496}
497
Dees_Troy5bf43922012-09-07 16:07:55 -0400498bool TWPartition::Is_File_System(string File_System) {
499 if (File_System == "ext2" ||
Dees_Troy63c8df72012-09-10 14:02:05 -0400500 File_System == "ext3" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400501 File_System == "ext4" ||
502 File_System == "vfat" ||
503 File_System == "ntfs" ||
504 File_System == "yaffs2" ||
bigbiff bigbiff3e146522012-11-14 14:32:59 -0500505 File_System == "exfat" ||
Dees_Troye5017042013-08-29 16:38:55 +0000506 File_System == "f2fs" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400507 File_System == "auto")
508 return true;
509 else
510 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400511}
512
Dees_Troy5bf43922012-09-07 16:07:55 -0400513bool TWPartition::Is_Image(string File_System) {
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400514 if (File_System == "emmc" || File_System == "mtd" || File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -0400515 return true;
516 else
517 return false;
518}
519
Dees_Troy51127312012-09-08 13:08:49 -0400520bool TWPartition::Make_Dir(string Path, bool Display_Error) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400521 if (!TWFunc::Path_Exists(Path)) {
Dees_Troy51127312012-09-08 13:08:49 -0400522 if (mkdir(Path.c_str(), 0777) == -1) {
523 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000524 LOGERR("Can not create '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400525 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000526 LOGINFO("Can not create '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400527 return false;
528 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000529 LOGINFO("Created '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400530 return true;
531 }
532 }
533 return true;
534}
535
Dees_Troy5bf43922012-09-07 16:07:55 -0400536void TWPartition::Setup_File_System(bool Display_Error) {
537 struct statfs st;
538
539 Can_Be_Mounted = true;
540 Can_Be_Wiped = true;
541
Dees_Troy5bf43922012-09-07 16:07:55 -0400542 // Make the mount point folder if it doesn't exist
Dees_Troy51127312012-09-08 13:08:49 -0400543 Make_Dir(Mount_Point, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400544 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
545 Backup_Name = Display_Name;
546 Backup_Method = FILES;
547}
548
549void TWPartition::Setup_Image(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400550 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
551 Backup_Name = Display_Name;
Gary Peck82599a82012-11-21 16:23:12 -0800552 if (Current_File_System == "emmc")
Dees_Troy5bf43922012-09-07 16:07:55 -0400553 Backup_Method = DD;
Gary Peck82599a82012-11-21 16:23:12 -0800554 else if (Current_File_System == "mtd" || Current_File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -0400555 Backup_Method = FLASH_UTILS;
556 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000557 LOGINFO("Unhandled file system '%s' on image '%s'\n", Current_File_System.c_str(), Display_Name.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400558 if (Find_Partition_Size()) {
559 Used = Size;
560 Backup_Size = Size;
Dees_Troy51a0e822012-09-05 15:24:24 -0400561 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400562 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000563 LOGERR("Unable to find parition size for '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400564 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000565 LOGINFO("Unable to find parition size for '%s'\n", Mount_Point.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400566 }
567}
568
Dees_Troye58d5262012-09-21 12:27:57 -0400569void TWPartition::Setup_AndSec(void) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500570 Backup_Display_Name = "Android Secure";
Dees_Troye58d5262012-09-21 12:27:57 -0400571 Backup_Name = "and-sec";
Dees_Troya13d74f2013-03-24 08:54:55 -0500572 Can_Be_Backed_Up = true;
Dees_Troye58d5262012-09-21 12:27:57 -0400573 Has_Android_Secure = true;
574 Symlink_Path = Mount_Point + "/.android_secure";
575 Symlink_Mount_Point = "/and-sec";
576 Backup_Path = Symlink_Mount_Point;
577 Make_Dir("/and-sec", true);
578 Recreate_AndSec_Folder();
579}
580
Dees_Troy5bf43922012-09-07 16:07:55 -0400581void TWPartition::Find_Real_Block_Device(string& Block, bool Display_Error) {
582 char device[512], realDevice[512];
583
584 strcpy(device, Block.c_str());
585 memset(realDevice, 0, sizeof(realDevice));
586 while (readlink(device, realDevice, sizeof(realDevice)) > 0)
587 {
588 strcpy(device, realDevice);
589 memset(realDevice, 0, sizeof(realDevice));
590 }
591
592 if (device[0] != '/') {
593 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000594 LOGERR("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400595 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000596 LOGINFO("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400597 return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400598 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400599 Block = device;
600 return;
601 }
602}
603
Dees_Troy8e337f32012-10-13 22:07:49 -0400604void TWPartition::Mount_Storage_Retry(void) {
605 // On some devices, storage doesn't want to mount right away, retry and sleep
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500606 if (!Mount(true)) {
Dees_Troy8e337f32012-10-13 22:07:49 -0400607 int retry_count = 5;
608 while (retry_count > 0 && !Mount(false)) {
609 usleep(500000);
610 retry_count--;
611 }
612 Mount(true);
613 }
614}
615
Dees_Troy38bd7602012-09-14 13:33:53 -0400616bool TWPartition::Find_MTD_Block_Device(string MTD_Name) {
617 FILE *fp = NULL;
618 char line[255];
619
620 fp = fopen("/proc/mtd", "rt");
621 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000622 LOGERR("Device does not support /proc/mtd\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400623 return false;
624 }
625
626 while (fgets(line, sizeof(line), fp) != NULL)
627 {
628 char device[32], label[32];
629 unsigned long size = 0;
630 char* fstype = NULL;
631 int deviceId;
632
633 sscanf(line, "%s %lx %*s %*c%s", device, &size, label);
634
635 // Skip header and blank lines
636 if ((strcmp(device, "dev:") == 0) || (strlen(line) < 8))
637 continue;
638
639 // Strip off the trailing " from the label
640 label[strlen(label)-1] = '\0';
641
642 if (strcmp(label, MTD_Name.c_str()) == 0) {
643 // We found our device
644 // Strip off the trailing : from the device
645 device[strlen(device)-1] = '\0';
646 if (sscanf(device,"mtd%d", &deviceId) == 1) {
647 sprintf(device, "/dev/block/mtdblock%d", deviceId);
648 Primary_Block_Device = device;
Dees_Troy76543db2013-06-19 16:24:30 +0000649 fclose(fp);
650 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -0400651 }
652 }
653 }
654 fclose(fp);
655
656 return false;
657}
658
Dees_Troy51127312012-09-08 13:08:49 -0400659bool TWPartition::Get_Size_Via_statfs(bool Display_Error) {
660 struct statfs st;
661 string Local_Path = Mount_Point + "/.";
662
663 if (!Mount(Display_Error))
664 return false;
665
666 if (statfs(Local_Path.c_str(), &st) != 0) {
667 if (!Removable) {
668 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000669 LOGERR("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400670 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000671 LOGINFO("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400672 }
673 return false;
674 }
675 Size = (st.f_blocks * st.f_bsize);
676 Used = ((st.f_blocks - st.f_bfree) * st.f_bsize);
677 Free = (st.f_bfree * st.f_bsize);
678 Backup_Size = Used;
679 return true;
680}
681
682bool TWPartition::Get_Size_Via_df(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400683 FILE* fp;
684 char command[255], line[512];
685 int include_block = 1;
686 unsigned int min_len;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500687 string result;
Dees_Troy5bf43922012-09-07 16:07:55 -0400688
689 if (!Mount(Display_Error))
690 return false;
691
Dees_Troy38bd7602012-09-14 13:33:53 -0400692 min_len = Actual_Block_Device.size() + 2;
Dees_Troy51127312012-09-08 13:08:49 -0400693 sprintf(command, "df %s > /tmp/dfoutput.txt", Mount_Point.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500694 TWFunc::Exec_Cmd(command, result);
Dees_Troy51127312012-09-08 13:08:49 -0400695 fp = fopen("/tmp/dfoutput.txt", "rt");
696 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000697 LOGINFO("Unable to open /tmp/dfoutput.txt.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400698 return false;
Dees_Troy51127312012-09-08 13:08:49 -0400699 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400700
701 while (fgets(line, sizeof(line), fp) != NULL)
702 {
703 unsigned long blocks, used, available;
704 char device[64];
705 char tmpString[64];
706
707 if (strncmp(line, "Filesystem", 10) == 0)
708 continue;
709 if (strlen(line) < min_len) {
710 include_block = 0;
711 continue;
712 }
713 if (include_block) {
714 sscanf(line, "%s %lu %lu %lu", device, &blocks, &used, &available);
715 } else {
716 // The device block string is so long that the df information is on the next line
717 int space_count = 0;
Dees_Troye58d5262012-09-21 12:27:57 -0400718 sprintf(tmpString, "/dev/block/%s", Actual_Block_Device.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400719 while (tmpString[space_count] == 32)
720 space_count++;
721 sscanf(line + space_count, "%lu %lu %lu", &blocks, &used, &available);
722 }
723
724 // Adjust block size to byte size
725 Size = blocks * 1024ULL;
726 Used = used * 1024ULL;
727 Free = available * 1024ULL;
728 Backup_Size = Used;
729 }
730 fclose(fp);
731 return true;
732}
733
Dees_Troy5bf43922012-09-07 16:07:55 -0400734bool TWPartition::Find_Partition_Size(void) {
735 FILE* fp;
736 char line[512];
737 string tmpdevice;
738
igoriok87e3d932013-01-31 21:03:53 +0200739 fp = fopen("/proc/dumchar_info", "rt");
740 if (fp != NULL) {
741 while (fgets(line, sizeof(line), fp) != NULL)
742 {
743 char label[32], device[32];
744 unsigned long size = 0;
745
746 sscanf(line, "%s %lx %*lx %*lu %s", label, &size, device);
747
748 // Skip header, annotation and blank lines
749 if ((strncmp(device, "/dev/", 5) != 0) || (strlen(line) < 8))
750 continue;
751
752 tmpdevice = "/dev/";
753 tmpdevice += label;
754 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
755 Size = size;
756 fclose(fp);
757 return true;
758 }
759 }
760 }
761
Dees_Troy5bf43922012-09-07 16:07:55 -0400762 // In this case, we'll first get the partitions we care about (with labels)
763 fp = fopen("/proc/partitions", "rt");
764 if (fp == NULL)
765 return false;
766
767 while (fgets(line, sizeof(line), fp) != NULL)
768 {
769 unsigned long major, minor, blocks;
770 char device[512];
771 char tmpString[64];
772
Dees_Troy63c8df72012-09-10 14:02:05 -0400773 if (strlen(line) < 7 || line[0] == 'm') continue;
Dees_Troy5bf43922012-09-07 16:07:55 -0400774 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
775
776 tmpdevice = "/dev/block/";
777 tmpdevice += device;
Dees_Troy38bd7602012-09-14 13:33:53 -0400778 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400779 // Adjust block size to byte size
780 Size = blocks * 1024ULL;
781 fclose(fp);
782 return true;
783 }
784 }
785 fclose(fp);
786 return false;
787}
788
Dees_Troy5bf43922012-09-07 16:07:55 -0400789bool TWPartition::Is_Mounted(void) {
790 if (!Can_Be_Mounted)
791 return false;
792
793 struct stat st1, st2;
794 string test_path;
795
796 // Check to see if the mount point directory exists
797 test_path = Mount_Point + "/.";
798 if (stat(test_path.c_str(), &st1) != 0) return false;
799
800 // Check to see if the directory above the mount point exists
801 test_path = Mount_Point + "/../.";
802 if (stat(test_path.c_str(), &st2) != 0) return false;
803
804 // Compare the device IDs -- if they match then we're (probably) using tmpfs instead of an actual device
805 int ret = (st1.st_dev != st2.st_dev) ? true : false;
806
807 return ret;
808}
809
810bool TWPartition::Mount(bool Display_Error) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000811 int exfat_mounted = 0;
812
Dees_Troy5bf43922012-09-07 16:07:55 -0400813 if (Is_Mounted()) {
814 return true;
815 } else if (!Can_Be_Mounted) {
816 return false;
817 }
Dees_Troy38bd7602012-09-14 13:33:53 -0400818
819 Find_Actual_Block_Device();
820
821 // Check the current file system before mounting
822 Check_FS_Type();
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000823 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/exfat-fuse")) {
Dees_Troye34c1332013-02-06 19:13:00 +0000824 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 +0000825 LOGINFO("cmd: %s\n", cmd.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000826 string result;
827 if (TWFunc::Exec_Cmd(cmd, result) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000828 LOGINFO("exfat-fuse failed to mount with result '%s', trying vfat\n", result.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000829 Current_File_System = "vfat";
830 } else {
831#ifdef TW_NO_EXFAT_FUSE
832 UnMount(false);
833 // We'll let the kernel handle it but using exfat-fuse to detect if the file system is actually exfat
834 // Some kernels let us mount vfat as exfat which doesn't work out too well
835#else
836 exfat_mounted = 1;
837#endif
838 }
839 }
Dees_Troy22042032012-12-18 21:23:08 +0000840 if (Fstab_File_System == "yaffs2") {
841 // mount an MTD partition as a YAFFS2 filesystem.
Dees_Troy76543db2013-06-19 16:24:30 +0000842 const unsigned long flags = MS_NOATIME | MS_NODEV | MS_NODIRATIME;
843 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags, NULL) < 0) {
844 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags | MS_RDONLY, NULL) < 0) {
845 if (Display_Error)
846 LOGERR("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
847 else
848 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
849 return false;
850 } else {
851 LOGINFO("Mounted '%s' (MTD) as RO\n", Mount_Point.c_str());
852 return true;
853 }
854 } else {
855 struct stat st;
856 string test_path = Mount_Point;
857 if (stat(test_path.c_str(), &st) < 0) {
858 if (Display_Error)
859 LOGERR("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
860 else
861 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
862 return false;
863 }
864 mode_t new_mode = st.st_mode | S_IXUSR | S_IXGRP | S_IXOTH;
865 if (new_mode != st.st_mode) {
866 LOGINFO("Fixing execute permissions for %s\n", Mount_Point.c_str());
867 if (chmod(Mount_Point.c_str(), new_mode) < 0) {
868 if (Display_Error)
869 LOGERR("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
870 else
871 LOGINFO("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
872 return false;
873 }
874 }
Dees_Troy22042032012-12-18 21:23:08 +0000875 return true;
Dees_Troy76543db2013-06-19 16:24:30 +0000876 }
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000877 } else if (!exfat_mounted && mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Current_File_System.c_str(), 0, NULL) != 0) {
878#ifdef TW_NO_EXFAT_FUSE
879 if (Current_File_System == "exfat") {
Dees_Troy2673cec2013-04-02 20:22:16 +0000880 LOGINFO("Mounting exfat failed, trying vfat...\n");
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000881 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), "vfat", 0, NULL) != 0) {
Dees_Troy85f44ed2013-01-09 18:42:36 +0000882 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000883 LOGERR("Unable to mount '%s'\n", Mount_Point.c_str());
Dees_Troy85f44ed2013-01-09 18:42:36 +0000884 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000885 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
886 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 +0000887 return false;
Dees_Troy85f44ed2013-01-09 18:42:36 +0000888 }
Dees_Troyb05ddee2013-01-28 20:24:50 +0000889 } else {
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000890#endif
891 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000892 LOGERR("Unable to mount '%s'\n", Mount_Point.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000893 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000894 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
895 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 +0000896 return false;
897#ifdef TW_NO_EXFAT_FUSE
Dees_Troy85f44ed2013-01-09 18:42:36 +0000898 }
899#endif
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000900 }
901#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
902 string MetaEcfsFile = EXPAND(TW_EXTERNAL_STORAGE_PATH);
903 MetaEcfsFile += "/.MetaEcfsFile";
904 if (EcryptFS_Password.size() > 0 && PartitionManager.Mount_By_Path("/data", false) && TWFunc::Path_Exists(MetaEcfsFile)) {
905 if (mount_ecryptfs_drive(EcryptFS_Password.c_str(), Mount_Point.c_str(), Mount_Point.c_str(), 0) != 0) {
906 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000907 LOGERR("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("Unable to mount ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000910 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000911 LOGINFO("Successfully mounted ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000912 Is_Decrypted = true;
Dees_Troy51127312012-09-08 13:08:49 -0400913 }
Dees_Troy066eb302013-08-23 17:20:32 +0000914 } else if (Mount_Point == EXPAND(TW_EXTERNAL_STORAGE_PATH)) {
915 if (Is_Decrypted)
916 LOGINFO("Mounting external storage, '%s' is not encrypted\n", Mount_Point.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000917 Is_Decrypted = false;
918 }
919#endif
920 if (Removable)
921 Update_Size(Display_Error);
922
923 if (!Symlink_Mount_Point.empty()) {
924 string Command, Result;
925 Command = "mount '" + Symlink_Path + "' '" + Symlink_Mount_Point + "'";
926 TWFunc::Exec_Cmd(Command, Result);
Dees_Troy5bf43922012-09-07 16:07:55 -0400927 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400928 return true;
929}
930
931bool TWPartition::UnMount(bool Display_Error) {
932 if (Is_Mounted()) {
933 int never_unmount_system;
934
935 DataManager::GetValue(TW_DONT_UNMOUNT_SYSTEM, never_unmount_system);
936 if (never_unmount_system == 1 && Mount_Point == "/system")
937 return true; // Never unmount system if you're not supposed to unmount it
938
Dees_Troyc8bafa12013-01-10 15:43:00 +0000939#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
940 if (EcryptFS_Password.size() > 0) {
941 if (unmount_ecryptfs_drive(Mount_Point.c_str()) != 0) {
942 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000943 LOGERR("Unable to unmount ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troyc8bafa12013-01-10 15:43:00 +0000944 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000945 LOGINFO("Unable to unmount ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troyc8bafa12013-01-10 15:43:00 +0000946 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000947 LOGINFO("Successfully unmounted ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troyc8bafa12013-01-10 15:43:00 +0000948 }
949 }
950#endif
951
Dees_Troy38bd7602012-09-14 13:33:53 -0400952 if (!Symlink_Mount_Point.empty())
953 umount(Symlink_Mount_Point.c_str());
954
Dees_Troyb05ddee2013-01-28 20:24:50 +0000955 umount(Mount_Point.c_str());
956 if (Is_Mounted()) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400957 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000958 LOGERR("Unable to unmount '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400959 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000960 LOGINFO("Unable to unmount '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400961 return false;
962 } else
963 return true;
964 } else {
965 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400966 }
967}
968
Gary Peck43acadf2012-11-21 21:19:01 -0800969bool TWPartition::Wipe(string New_File_System) {
Dees_Troy16c2b312013-01-15 16:51:18 +0000970 bool wiped = false, update_crypt = false;
971 int check;
972 string Layout_Filename = Mount_Point + "/.layout_version";
973
Dees_Troy38bd7602012-09-14 13:33:53 -0400974 if (!Can_Be_Wiped) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000975 LOGERR("Partition '%s' cannot be wiped.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -0400976 return false;
977 }
978
Dees_Troyc51f1f92012-09-20 15:32:13 -0400979 if (Mount_Point == "/cache")
Dees_Troy2673cec2013-04-02 20:22:16 +0000980 Log_Offset = 0;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400981
Dees_Troyce675462013-01-09 19:48:21 +0000982#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
983 if (Mount_Point == "/data" && Mount(false)) {
984 if (TWFunc::Path_Exists("/data/system/edk_p_sd"))
985 TWFunc::copy_file("/data/system/edk_p_sd", "/tmp/edk_p_sd", 0600);
986 }
987#endif
988
Dees_Troy16c2b312013-01-15 16:51:18 +0000989 if (Retain_Layout_Version && Mount(false) && TWFunc::Path_Exists(Layout_Filename))
990 TWFunc::copy_file(Layout_Filename, "/.layout_version", 0600);
991 else
992 unlink("/.layout_version");
Dees_Troy38bd7602012-09-14 13:33:53 -0400993
Dees_Troy16c2b312013-01-15 16:51:18 +0000994 if (Has_Data_Media) {
995 wiped = Wipe_Data_Without_Wiping_Media();
996 } else {
Gary Pecke8bc5d72012-12-21 06:45:25 -0800997
Dees_Troy16c2b312013-01-15 16:51:18 +0000998 DataManager::GetValue(TW_RM_RF_VAR, check);
999
Hashcodedabfd492013-08-29 22:45:30 -07001000 if (check || Use_Rm_Rf)
Dees_Troy16c2b312013-01-15 16:51:18 +00001001 wiped = Wipe_RMRF();
1002 else if (New_File_System == "ext4")
1003 wiped = Wipe_EXT4();
1004 else if (New_File_System == "ext2" || New_File_System == "ext3")
1005 wiped = Wipe_EXT23(New_File_System);
1006 else if (New_File_System == "vfat")
1007 wiped = Wipe_FAT();
1008 else if (New_File_System == "exfat")
1009 wiped = Wipe_EXFAT();
1010 else if (New_File_System == "yaffs2")
1011 wiped = Wipe_MTD();
Dees_Troye5017042013-08-29 16:38:55 +00001012 else if (New_File_System == "f2fs")
1013 wiped = Wipe_F2FS();
Dees_Troy16c2b312013-01-15 16:51:18 +00001014 else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001015 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 +00001016 unlink("/.layout_version");
1017 return false;
1018 }
1019 update_crypt = wiped;
Gary Pecke8bc5d72012-12-21 06:45:25 -08001020 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001021
Gary Pecke8bc5d72012-12-21 06:45:25 -08001022 if (wiped) {
Dees_Troyce675462013-01-09 19:48:21 +00001023#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
1024 if (Mount_Point == "/data" && Mount(false)) {
1025 if (TWFunc::Path_Exists("/tmp/edk_p_sd")) {
1026 Make_Dir("/data/system", true);
1027 TWFunc::copy_file("/tmp/edk_p_sd", "/data/system/edk_p_sd", 0600);
1028 }
1029 }
1030#endif
Dees_Troy16c2b312013-01-15 16:51:18 +00001031
Dees_Troy1c1ac442013-01-17 21:42:14 +00001032 if (Mount_Point == "/cache")
1033 DataManager::Output_Version();
1034
Dees_Troy16c2b312013-01-15 16:51:18 +00001035 if (TWFunc::Path_Exists("/.layout_version") && Mount(false))
1036 TWFunc::copy_file("/.layout_version", Layout_Filename, 0600);
1037
1038 if (update_crypt) {
1039 Setup_File_System(false);
1040 if (Is_Encrypted && !Is_Decrypted) {
1041 // just wiped an encrypted partition back to its unencrypted state
1042 Is_Encrypted = false;
1043 Is_Decrypted = false;
1044 Decrypted_Block_Device = "";
1045 if (Mount_Point == "/data") {
1046 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1047 DataManager::SetValue(TW_IS_DECRYPTED, 0);
1048 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001049 }
1050 }
1051 }
1052 return wiped;
Dees_Troy51a0e822012-09-05 15:24:24 -04001053}
1054
Gary Peck43acadf2012-11-21 21:19:01 -08001055bool TWPartition::Wipe() {
Gary Peck82599a82012-11-21 16:23:12 -08001056 if (Is_File_System(Current_File_System))
1057 return Wipe(Current_File_System);
1058 else
1059 return Wipe(Fstab_File_System);
Gary Peck43acadf2012-11-21 21:19:01 -08001060}
1061
Dees_Troye58d5262012-09-21 12:27:57 -04001062bool TWPartition::Wipe_AndSec(void) {
1063 if (!Has_Android_Secure)
1064 return false;
1065
Dees_Troye58d5262012-09-21 12:27:57 -04001066 if (!Mount(true))
1067 return false;
1068
Dees_Troy2673cec2013-04-02 20:22:16 +00001069 gui_print("Wiping %s\n", Backup_Display_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001070 TWFunc::removeDir(Mount_Point + "/.android_secure/", true);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001071 return true;
Dees_Troye58d5262012-09-21 12:27:57 -04001072}
1073
Dees_Troy51a0e822012-09-05 15:24:24 -04001074bool TWPartition::Backup(string backup_folder) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001075 if (Backup_Method == FILES)
1076 return Backup_Tar(backup_folder);
1077 else if (Backup_Method == DD)
1078 return Backup_DD(backup_folder);
1079 else if (Backup_Method == FLASH_UTILS)
1080 return Backup_Dump_Image(backup_folder);
Dees_Troy2673cec2013-04-02 20:22:16 +00001081 LOGERR("Unknown backup method for '%s'\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001082 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001083}
1084
Dees_Troy43d8b002012-09-17 16:00:01 -04001085bool TWPartition::Check_MD5(string restore_folder) {
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001086 string Full_Filename, md5file;
Dees_Troy43d8b002012-09-17 16:00:01 -04001087 char split_filename[512];
1088 int index = 0;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001089 twrpDigest md5sum;
Dees_Troy43d8b002012-09-17 16:00:01 -04001090
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001091 memset(split_filename, 0, sizeof(split_filename));
Dees_Troy43d8b002012-09-17 16:00:01 -04001092 Full_Filename = restore_folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04001093 if (!TWFunc::Path_Exists(Full_Filename)) {
1094 // This is a split archive, we presume
1095 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
Dees_Troy2673cec2013-04-02 20:22:16 +00001096 LOGINFO("split_filename: %s\n", split_filename);
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001097 md5file = split_filename;
1098 md5file += ".md5";
1099 if (!TWFunc::Path_Exists(md5file)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001100 LOGERR("No md5 file found for '%s'.\n", split_filename);
1101 LOGERR("Please unselect Enable MD5 verification to restore.\n");
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001102 return false;
1103 }
1104 md5sum.setfn(split_filename);
Dees_Troy83bd4832013-05-04 12:39:56 +00001105 while (index < 1000) {
1106 if (TWFunc::Path_Exists(split_filename) && md5sum.verify_md5digest() != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001107 LOGERR("MD5 failed to match on '%s'.\n", split_filename);
Dees_Troy43d8b002012-09-17 16:00:01 -04001108 return false;
1109 }
1110 index++;
1111 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001112 md5sum.setfn(split_filename);
Dees_Troy43d8b002012-09-17 16:00:01 -04001113 }
Dees_Troy4a2a1262012-09-18 09:33:47 -04001114 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001115 } else {
1116 // Single file archive
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001117 md5file = Full_Filename + ".md5";
1118 if (!TWFunc::Path_Exists(md5file)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001119 LOGERR("No md5 file found for '%s'.\n", Full_Filename.c_str());
1120 LOGERR("Please unselect Enable MD5 verification to restore.\n");
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001121 return false;
1122 }
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001123 md5sum.setfn(Full_Filename);
1124 if (md5sum.verify_md5digest() != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001125 LOGERR("MD5 failed to match on '%s'.\n", Full_Filename.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001126 return false;
1127 } else
1128 return true;
1129 }
1130 return false;
1131}
1132
Dees_Troy51a0e822012-09-05 15:24:24 -04001133bool TWPartition::Restore(string restore_folder) {
Gary Peck43acadf2012-11-21 21:19:01 -08001134 size_t first_period, second_period;
1135 string Restore_File_System;
1136
1137 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
Dees_Troy2673cec2013-04-02 20:22:16 +00001138 LOGINFO("Restore filename is: %s\n", Backup_FileName.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001139
1140 // Parse backup filename to extract the file system before wiping
1141 first_period = Backup_FileName.find(".");
1142 if (first_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001143 LOGERR("Unable to find file system (first period).\n");
Gary Peck43acadf2012-11-21 21:19:01 -08001144 return false;
1145 }
1146 Restore_File_System = Backup_FileName.substr(first_period + 1, Backup_FileName.size() - first_period - 1);
1147 second_period = Restore_File_System.find(".");
1148 if (second_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001149 LOGERR("Unable to find file system (second period).\n");
Gary Peck43acadf2012-11-21 21:19:01 -08001150 return false;
1151 }
1152 Restore_File_System.resize(second_period);
Dees_Troy2673cec2013-04-02 20:22:16 +00001153 LOGINFO("Restore file system is: '%s'.\n", Restore_File_System.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001154
1155 if (Is_File_System(Restore_File_System))
1156 return Restore_Tar(restore_folder, Restore_File_System);
1157 else if (Is_Image(Restore_File_System)) {
1158 if (Restore_File_System == "emmc")
1159 return Restore_DD(restore_folder);
1160 else if (Restore_File_System == "mtd" || Restore_File_System == "bml")
1161 return Restore_Flash_Image(restore_folder);
1162 }
1163
Dees_Troy2673cec2013-04-02 20:22:16 +00001164 LOGERR("Unknown restore method for '%s'\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001165 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001166}
1167
1168string TWPartition::Backup_Method_By_Name() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001169 if (Backup_Method == NONE)
1170 return "none";
1171 else if (Backup_Method == FILES)
1172 return "files";
1173 else if (Backup_Method == DD)
1174 return "dd";
1175 else if (Backup_Method == FLASH_UTILS)
1176 return "flash_utils";
1177 else
1178 return "undefined";
1179 return "ERROR!";
Dees_Troy51a0e822012-09-05 15:24:24 -04001180}
1181
1182bool TWPartition::Decrypt(string Password) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001183 LOGINFO("STUB TWPartition::Decrypt, password: '%s'\n", Password.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001184 // Is this needed?
Dees_Troy51a0e822012-09-05 15:24:24 -04001185 return 1;
1186}
1187
1188bool TWPartition::Wipe_Encryption() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001189 bool Save_Data_Media = Has_Data_Media;
1190
1191 if (!UnMount(true))
1192 return false;
1193
Dees_Troy38bd7602012-09-14 13:33:53 -04001194 Has_Data_Media = false;
Dees_Troy74fb2e92013-04-15 14:35:47 +00001195 Decrypted_Block_Device = "";
1196 Is_Decrypted = false;
1197 Is_Encrypted = false;
Gary Pecke8bc5d72012-12-21 06:45:25 -08001198 if (Wipe(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001199 Has_Data_Media = Save_Data_Media;
1200 if (Has_Data_Media && !Symlink_Mount_Point.empty()) {
1201 Recreate_Media_Folder();
1202 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001203 gui_print("You may need to reboot recovery to be able to use /data again.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001204 return true;
1205 } else {
1206 Has_Data_Media = Save_Data_Media;
Dees_Troy2673cec2013-04-02 20:22:16 +00001207 LOGERR("Unable to format to remove encryption.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001208 return false;
1209 }
1210 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001211}
1212
1213void TWPartition::Check_FS_Type() {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001214 const char* type;
1215 blkid_probe pr;
Dees_Troy5bf43922012-09-07 16:07:55 -04001216
Dees_Troy68cab492012-12-12 19:29:35 +00001217 if (Fstab_File_System == "yaffs2" || Fstab_File_System == "mtd" || Fstab_File_System == "bml" || Ignore_Blkid)
1218 return; // Running blkid on some mtd devices causes a massive crash or needs to be skipped
Dees_Troy5bf43922012-09-07 16:07:55 -04001219
Dees_Troy38bd7602012-09-14 13:33:53 -04001220 Find_Actual_Block_Device();
Dees_Troy8170a922012-09-18 15:40:25 -04001221 if (!Is_Present)
1222 return;
Dees_Troy51127312012-09-08 13:08:49 -04001223
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001224 pr = blkid_new_probe_from_filename(Actual_Block_Device.c_str());
1225 if (blkid_do_fullprobe(pr)) {
1226 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00001227 LOGINFO("Can't probe device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001228 return;
Dees_Troy5bf43922012-09-07 16:07:55 -04001229 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001230
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001231 if (blkid_probe_lookup_value(pr, "TYPE", &type, NULL) < 0) {
1232 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00001233 LOGINFO("can't find filesystem on device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001234 return;
1235 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001236
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001237 Current_File_System = type;
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001238 blkid_free_probe(pr);
Dees_Troy51a0e822012-09-05 15:24:24 -04001239}
1240
Gary Peck43acadf2012-11-21 21:19:01 -08001241bool TWPartition::Wipe_EXT23(string File_System) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001242 if (!UnMount(true))
1243 return false;
1244
Dees_Troy43d8b002012-09-17 16:00:01 -04001245 if (TWFunc::Path_Exists("/sbin/mke2fs")) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001246 string command, result;
Dees_Troy38bd7602012-09-14 13:33:53 -04001247
Dees_Troy2673cec2013-04-02 20:22:16 +00001248 gui_print("Formatting %s using mke2fs...\n", Display_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001249 Find_Actual_Block_Device();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001250 command = "mke2fs -t " + File_System + " -m 0 " + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001251 LOGINFO("mke2fs command: %s\n", command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001252 if (TWFunc::Exec_Cmd(command, result) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001253 Current_File_System = File_System;
Dees_Troye58d5262012-09-21 12:27:57 -04001254 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001255 gui_print("Done.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001256 return true;
1257 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001258 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001259 return false;
1260 }
1261 } else
1262 return Wipe_RMRF();
1263
1264 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001265}
1266
1267bool TWPartition::Wipe_EXT4() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001268 if (!UnMount(true))
1269 return false;
1270
Dees_Troyb3265ab2013-08-30 02:59:14 +00001271#if defined(HAVE_SELINUX) && defined(USE_EXT4)
Dees_Troya95f55c2013-08-17 13:14:43 +00001272 gui_print("Formatting %s using make_ext4fs function.\n", Display_Name.c_str());
Dees_Troya95f55c2013-08-17 13:14:43 +00001273 if (make_ext4fs(Actual_Block_Device.c_str(), Length, Mount_Point.c_str(), selinux_handle) != 0) {
Dees_Troya95f55c2013-08-17 13:14:43 +00001274 LOGERR("Unable to wipe '%s' using function call.\n", Mount_Point.c_str());
1275 return false;
1276 } else {
1277 return true;
1278 }
1279#else
Dees_Troy43d8b002012-09-17 16:00:01 -04001280 if (TWFunc::Path_Exists("/sbin/make_ext4fs")) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001281 string Command, result;
Dees_Troy38bd7602012-09-14 13:33:53 -04001282
Dees_Troy2673cec2013-04-02 20:22:16 +00001283 gui_print("Formatting %s using make_ext4fs...\n", Display_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001284 Find_Actual_Block_Device();
1285 Command = "make_ext4fs";
1286 if (!Is_Decrypted && Length != 0) {
1287 // Only use length if we're not decrypted
1288 char len[32];
1289 sprintf(len, "%i", Length);
1290 Command += " -l ";
1291 Command += len;
1292 }
1293 Command += " " + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001294 LOGINFO("make_ext4fs command: %s\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001295 if (TWFunc::Exec_Cmd(Command, result) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001296 Current_File_System = "ext4";
Dees_Troye58d5262012-09-21 12:27:57 -04001297 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001298 gui_print("Done.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001299 return true;
1300 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001301 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001302 return false;
1303 }
1304 } else
Gary Peck43acadf2012-11-21 21:19:01 -08001305 return Wipe_EXT23("ext4");
Dees_Troya95f55c2013-08-17 13:14:43 +00001306#endif
Dees_Troy38bd7602012-09-14 13:33:53 -04001307 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001308}
1309
1310bool TWPartition::Wipe_FAT() {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001311 string command, result;
Dees_Troy38bd7602012-09-14 13:33:53 -04001312
Dees_Troy43d8b002012-09-17 16:00:01 -04001313 if (TWFunc::Path_Exists("/sbin/mkdosfs")) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001314 if (!UnMount(true))
1315 return false;
1316
Dees_Troy2673cec2013-04-02 20:22:16 +00001317 gui_print("Formatting %s using mkdosfs...\n", Display_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001318 Find_Actual_Block_Device();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001319 command = "mkdosfs " + Actual_Block_Device;
1320 if (TWFunc::Exec_Cmd(command, result) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001321 Current_File_System = "vfat";
Dees_Troye58d5262012-09-21 12:27:57 -04001322 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001323 gui_print("Done.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001324 return true;
1325 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001326 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001327 return false;
1328 }
1329 return true;
1330 }
1331 else
1332 return Wipe_RMRF();
1333
1334 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001335}
1336
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001337bool TWPartition::Wipe_EXFAT() {
1338 string command, result;
1339
1340 if (TWFunc::Path_Exists("/sbin/mkexfatfs")) {
1341 if (!UnMount(true))
1342 return false;
1343
Dees_Troy2673cec2013-04-02 20:22:16 +00001344 gui_print("Formatting %s using mkexfatfs...\n", Display_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001345 Find_Actual_Block_Device();
1346 command = "mkexfatfs " + Actual_Block_Device;
1347 if (TWFunc::Exec_Cmd(command, result) == 0) {
1348 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001349 gui_print("Done.\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001350 return true;
1351 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001352 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001353 return false;
1354 }
1355 return true;
1356 }
1357 return false;
1358}
1359
Dees_Troy38bd7602012-09-14 13:33:53 -04001360bool TWPartition::Wipe_MTD() {
1361 if (!UnMount(true))
1362 return false;
1363
Dees_Troy2673cec2013-04-02 20:22:16 +00001364 gui_print("MTD Formatting \"%s\"\n", MTD_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001365
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001366 mtd_scan_partitions();
1367 const MtdPartition* mtd = mtd_find_partition_by_name(MTD_Name.c_str());
1368 if (mtd == NULL) {
1369 LOGERR("No mtd partition named '%s'", MTD_Name.c_str());
1370 return false;
1371 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001372
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001373 MtdWriteContext* ctx = mtd_write_partition(mtd);
1374 if (ctx == NULL) {
1375 LOGERR("Can't write '%s', failed to format.", MTD_Name.c_str());
1376 return false;
1377 }
1378 if (mtd_erase_blocks(ctx, -1) == -1) {
1379 mtd_write_close(ctx);
1380 LOGERR("Failed to format '%s'", MTD_Name.c_str());
1381 return false;
1382 }
1383 if (mtd_write_close(ctx) != 0) {
1384 LOGERR("Failed to close '%s'", MTD_Name.c_str());
1385 return false;
1386 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001387 Current_File_System = "yaffs2";
Dees_Troye58d5262012-09-21 12:27:57 -04001388 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001389 gui_print("Done.\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001390 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001391}
1392
1393bool TWPartition::Wipe_RMRF() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001394 if (!Mount(true))
1395 return false;
1396
Dees_Troy2673cec2013-04-02 20:22:16 +00001397 gui_print("Removing all files under '%s'\n", Mount_Point.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001398 TWFunc::removeDir(Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04001399 Recreate_AndSec_Folder();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001400 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001401}
1402
Dees_Troye5017042013-08-29 16:38:55 +00001403bool TWPartition::Wipe_F2FS() {
1404 string command, result;
1405
1406 if (TWFunc::Path_Exists("/sbin/mkfs.f2fs")) {
1407 if (!UnMount(true))
1408 return false;
1409
1410 gui_print("Formatting %s using mkfs.f2fs...\n", Display_Name.c_str());
1411 Find_Actual_Block_Device();
1412 command = "mkfs.f2fs " + Actual_Block_Device;
1413 if (TWFunc::Exec_Cmd(command, result) == 0) {
1414 Recreate_AndSec_Folder();
1415 gui_print("Done.\n");
1416 return true;
1417 } else {
1418 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
1419 return false;
1420 }
1421 return true;
1422 } else {
1423 gui_print("mkfs.f2fs binary not found, using rm -rf to wipe.\n");
1424 return Wipe_RMRF();
1425 }
1426 return false;
1427}
1428
Dees_Troy51a0e822012-09-05 15:24:24 -04001429bool TWPartition::Wipe_Data_Without_Wiping_Media() {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001430 string dir;
Dees_Troy38bd7602012-09-14 13:33:53 -04001431
1432 // This handles wiping data on devices with "sdcard" in /data/media
1433 if (!Mount(true))
1434 return false;
1435
Dees_Troy2673cec2013-04-02 20:22:16 +00001436 gui_print("Wiping data without wiping /data/media ...\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001437
1438 DIR* d;
1439 d = opendir("/data");
Dees_Troy16b74352012-11-14 22:27:31 +00001440 if (d != NULL) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001441 struct dirent* de;
1442 while ((de = readdir(d)) != NULL) {
Dees_Troy16b74352012-11-14 22:27:31 +00001443 if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) continue;
1444 // The media folder is the "internal sdcard"
1445 // The .layout_version file is responsible for determining whether 4.2 decides up upgrade
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001446 // the media folder for multi-user.
Dees_Troy16b74352012-11-14 22:27:31 +00001447 if (strcmp(de->d_name, "media") == 0 || strcmp(de->d_name, ".layout_version") == 0) continue;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001448
1449 dir = "/data/";
1450 dir.append(de->d_name);
Dees_Troyce675462013-01-09 19:48:21 +00001451 if (de->d_type == DT_DIR) {
1452 TWFunc::removeDir(dir, false);
bigbiff bigbiff98f1f902013-01-19 18:46:13 -05001453 } 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 +00001454 if (!unlink(dir.c_str()))
Dees_Troy2673cec2013-04-02 20:22:16 +00001455 LOGINFO("Unable to unlink '%s'\n", dir.c_str());
Dees_Troyce675462013-01-09 19:48:21 +00001456 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001457 }
1458 closedir(d);
Dees_Troy2673cec2013-04-02 20:22:16 +00001459 gui_print("Done.\n");
Dees_Troy16b74352012-11-14 22:27:31 +00001460 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -04001461 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001462 gui_print("Dirent failed to open /data, error!\n");
Dees_Troy16b74352012-11-14 22:27:31 +00001463 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001464}
1465
1466bool TWPartition::Backup_Tar(string backup_folder) {
Dees_Troy4a2a1262012-09-18 09:33:47 -04001467 char back_name[255], split_index[5];
1468 string Full_FileName, Split_FileName, Tar_Args, Command;
Dees_Troy83bd4832013-05-04 12:39:56 +00001469 int use_compression, use_encryption = 0, index, backup_count;
Dees_Troy4a2a1262012-09-18 09:33:47 -04001470 struct stat st;
Dees_Troy7c2dec82012-09-26 09:49:14 -04001471 unsigned long long total_bsize = 0, file_size;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001472 twrpTar tar;
1473 vector <string> files;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001474
Dees_Troy43d8b002012-09-17 16:00:01 -04001475 if (!Mount(true))
1476 return false;
1477
Dees_Troya13d74f2013-03-24 08:54:55 -05001478 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Backup_Display_Name, "Backing Up");
Dees_Troy2673cec2013-04-02 20:22:16 +00001479 gui_print("Backing up %s...\n", Backup_Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001480
1481 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
Dees_Troy83bd4832013-05-04 12:39:56 +00001482 tar.use_compression = use_compression;
bigbiff bigbiff86e77bc2013-08-26 21:36:23 -04001483 //exclude Google Music Cache
1484 tar.setexcl("/data/data/com.google.android.music/files");
Dees_Troy83bd4832013-05-04 12:39:56 +00001485#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
1486 DataManager::GetValue("tw_encrypt_backup", use_encryption);
1487 if (use_encryption && Can_Encrypt_Backup) {
1488 tar.use_encryption = use_encryption;
1489 if (Use_Userdata_Encryption)
1490 tar.userdata_encryption = use_encryption;
1491 } else {
1492 use_encryption = false;
1493 }
1494#endif
Dees_Troy43d8b002012-09-17 16:00:01 -04001495
1496 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1497 Backup_FileName = back_name;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001498 Full_FileName = backup_folder + "/" + Backup_FileName;
Dees_Troy83bd4832013-05-04 12:39:56 +00001499 tar.has_data_media = Has_Data_Media;
1500 if (!use_encryption && Backup_Size > MAX_ARCHIVE_SIZE) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001501 // This backup needs to be split into multiple archives
Dees_Troy2673cec2013-04-02 20:22:16 +00001502 gui_print("Breaking backup file into multiple archives...\n");
Dees_Troye58d5262012-09-21 12:27:57 -04001503 sprintf(back_name, "%s", Backup_Path.c_str());
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -05001504 tar.setdir(back_name);
1505 tar.setfn(Full_FileName);
bigbiff bigbiffe6594ab2013-02-17 20:18:31 -05001506 backup_count = tar.splitArchiveFork();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001507 if (backup_count == -1) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001508 LOGERR("Error tarring split files!\n");
Dees_Troy4a2a1262012-09-18 09:33:47 -04001509 return false;
1510 }
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001511 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001512 } else {
Dees_Troy4a2a1262012-09-18 09:33:47 -04001513 Full_FileName = backup_folder + "/" + Backup_FileName;
Dees_Troy83bd4832013-05-04 12:39:56 +00001514 tar.setdir(Backup_Path);
1515 tar.setfn(Full_FileName);
1516 if (tar.createTarFork() != 0)
1517 return false;
1518 if (use_compression && !use_encryption) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001519 string gzname = Full_FileName + ".gz";
1520 rename(gzname.c_str(), Full_FileName.c_str());
1521 }
Dees_Troy83bd4832013-05-04 12:39:56 +00001522 if (use_encryption)
1523 Full_FileName += "000";
Dees_Troy7c2dec82012-09-26 09:49:14 -04001524 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001525 LOGERR("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001526 return false;
1527 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001528 }
1529 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001530}
1531
1532bool TWPartition::Backup_DD(string backup_folder) {
igoriok87e3d932013-01-31 21:03:53 +02001533 char back_name[255], backup_size[32];
1534 string Full_FileName, Command, result, DD_BS;
Dees_Troy43d8b002012-09-17 16:00:01 -04001535 int use_compression;
1536
igoriok87e3d932013-01-31 21:03:53 +02001537 sprintf(backup_size, "%llu", Backup_Size);
1538 DD_BS = backup_size;
1539
Dees_Troyb46a6842012-09-25 11:06:46 -04001540 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
Dees_Troy2673cec2013-04-02 20:22:16 +00001541 gui_print("Backing up %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001542
1543 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1544 Backup_FileName = back_name;
1545
1546 Full_FileName = backup_folder + "/" + Backup_FileName;
1547
igoriok87e3d932013-01-31 21:03:53 +02001548 Command = "dd if=" + Actual_Block_Device + " of='" + Full_FileName + "'" + " bs=" + DD_BS + "c count=1";
Dees_Troy2673cec2013-04-02 20:22:16 +00001549 LOGINFO("Backup command: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001550 TWFunc::Exec_Cmd(Command, result);
Dees_Troyc154ac22012-10-12 15:36:47 -04001551 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001552 LOGERR("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001553 return false;
1554 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001555 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001556}
1557
1558bool TWPartition::Backup_Dump_Image(string backup_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001559 char back_name[255];
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001560 string Full_FileName, Command, result;
Dees_Troy43d8b002012-09-17 16:00:01 -04001561 int use_compression;
1562
Dees_Troyb46a6842012-09-25 11:06:46 -04001563 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
Dees_Troy2673cec2013-04-02 20:22:16 +00001564 gui_print("Backing up %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001565
1566 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1567 Backup_FileName = back_name;
1568
1569 Full_FileName = backup_folder + "/" + Backup_FileName;
1570
1571 Command = "dump_image " + MTD_Name + " '" + Full_FileName + "'";
Dees_Troy2673cec2013-04-02 20:22:16 +00001572 LOGINFO("Backup command: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001573 TWFunc::Exec_Cmd(Command, result);
Dees_Troy7c2dec82012-09-26 09:49:14 -04001574 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
1575 // 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 +00001576 LOGERR("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001577 return false;
1578 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001579 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001580}
1581
Gary Peck43acadf2012-11-21 21:19:01 -08001582bool TWPartition::Restore_Tar(string restore_folder, string Restore_File_System) {
1583 string Full_FileName, Command;
Dees_Troy4a2a1262012-09-18 09:33:47 -04001584 int index = 0;
1585 char split_index[5];
Dees_Troy43d8b002012-09-17 16:00:01 -04001586
Dees_Troye58d5262012-09-21 12:27:57 -04001587 if (Has_Android_Secure) {
Dees_Troye58d5262012-09-21 12:27:57 -04001588 if (!Wipe_AndSec())
1589 return false;
Gary Peck43acadf2012-11-21 21:19:01 -08001590 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001591 gui_print("Wiping %s...\n", Display_Name.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001592 if (!Wipe(Restore_File_System))
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001593 return false;
Dees_Troye58d5262012-09-21 12:27:57 -04001594 }
Dees_Troya13d74f2013-03-24 08:54:55 -05001595 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Backup_Display_Name, "Restoring");
Dees_Troy2673cec2013-04-02 20:22:16 +00001596 gui_print("Restoring %s...\n", Backup_Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001597
1598 if (!Mount(true))
1599 return false;
1600
Dees_Troy4a2a1262012-09-18 09:33:47 -04001601 Full_FileName = restore_folder + "/" + Backup_FileName;
Dees_Troy83bd4832013-05-04 12:39:56 +00001602 /*if (!TWFunc::Path_Exists(Full_FileName)) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001603 if (!TWFunc::Path_Exists(Full_FileName)) {
1604 // Backup is multiple archives
Dees_Troy2673cec2013-04-02 20:22:16 +00001605 LOGINFO("Backup is multiple archives.\n");
Dees_Troy4a2a1262012-09-18 09:33:47 -04001606 sprintf(split_index, "%03i", index);
1607 Full_FileName = restore_folder + "/" + Backup_FileName + split_index;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001608 while (TWFunc::Path_Exists(Full_FileName)) {
1609 index++;
Dees_Troy2673cec2013-04-02 20:22:16 +00001610 gui_print("Restoring archive %i...\n", index);
1611 LOGINFO("Restoring '%s'...\n", Full_FileName.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001612 twrpTar tar;
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -05001613 tar.setdir("/");
1614 tar.setfn(Full_FileName);
bigbiff bigbiffe6594ab2013-02-17 20:18:31 -05001615 if (tar.extractTarFork() != 0)
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001616 return false;
1617 sprintf(split_index, "%03i", index);
1618 Full_FileName = restore_folder + "/" + Backup_FileName + split_index;
1619 }
1620 if (index == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001621 LOGERR("Error locating restore file: '%s'\n", Full_FileName.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001622 return false;
1623 }
Dees_Troy4a2a1262012-09-18 09:33:47 -04001624 }
Dees_Troy83bd4832013-05-04 12:39:56 +00001625 } else {*/
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001626 twrpTar tar;
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -05001627 tar.setdir(Backup_Path);
1628 tar.setfn(Full_FileName);
Dees_Troy83bd4832013-05-04 12:39:56 +00001629 tar.backup_name = Backup_Name;
bigbiff bigbiffe6594ab2013-02-17 20:18:31 -05001630 if (tar.extractTarFork() != 0)
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001631 return false;
Dees_Troy83bd4832013-05-04 12:39:56 +00001632 //}
Dees_Troy43d8b002012-09-17 16:00:01 -04001633 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001634}
1635
1636bool TWPartition::Restore_DD(string restore_folder) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001637 string Full_FileName, Command, result;
Dees_Troy43d8b002012-09-17 16:00:01 -04001638
Dees_Troyda8b55a2012-12-12 19:18:30 +00001639 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
Dees_Troy43d8b002012-09-17 16:00:01 -04001640 Full_FileName = restore_folder + "/" + Backup_FileName;
Gary Peck15e623d2012-11-21 21:07:58 -08001641
1642 if (!Find_Partition_Size()) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001643 LOGERR("Unable to find partition size for '%s'\n", Mount_Point.c_str());
Gary Peck15e623d2012-11-21 21:07:58 -08001644 return false;
1645 }
1646 unsigned long long backup_size = TWFunc::Get_File_Size(Full_FileName);
1647 if (backup_size > Size) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001648 LOGERR("Size (%iMB) of backup '%s' is larger than target device '%s' (%iMB)\n",
Gary Peck15e623d2012-11-21 21:07:58 -08001649 (int)(backup_size / 1048576LLU), Full_FileName.c_str(),
1650 Actual_Block_Device.c_str(), (int)(Size / 1048576LLU));
1651 return false;
1652 }
1653
Dees_Troy2673cec2013-04-02 20:22:16 +00001654 gui_print("Restoring %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001655 Command = "dd bs=4096 if='" + Full_FileName + "' of=" + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001656 LOGINFO("Restore command: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001657 TWFunc::Exec_Cmd(Command, result);
Dees_Troy43d8b002012-09-17 16:00:01 -04001658 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001659}
1660
1661bool TWPartition::Restore_Flash_Image(string restore_folder) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001662 string Full_FileName, Command, result;
Dees_Troy43d8b002012-09-17 16:00:01 -04001663
Dees_Troy2673cec2013-04-02 20:22:16 +00001664 gui_print("Restoring %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001665 Full_FileName = restore_folder + "/" + Backup_FileName;
1666 // Sometimes flash image doesn't like to flash due to the first 2KB matching, so we erase first to ensure that it flashes
1667 Command = "erase_image " + MTD_Name;
Dees_Troy2673cec2013-04-02 20:22:16 +00001668 LOGINFO("Erase command: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001669 TWFunc::Exec_Cmd(Command, result);
Dees_Troy43d8b002012-09-17 16:00:01 -04001670 Command = "flash_image " + MTD_Name + " '" + Full_FileName + "'";
Dees_Troy2673cec2013-04-02 20:22:16 +00001671 LOGINFO("Restore command: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001672 TWFunc::Exec_Cmd(Command, result);
Dees_Troy43d8b002012-09-17 16:00:01 -04001673 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001674}
Dees_Troy5bf43922012-09-07 16:07:55 -04001675
1676bool TWPartition::Update_Size(bool Display_Error) {
Dees_Troy0550cfb2012-10-13 11:56:13 -04001677 bool ret = false, Was_Already_Mounted = false;
Dees_Troy51127312012-09-08 13:08:49 -04001678
Dees_Troyab10ee22012-09-21 14:27:30 -04001679 if (!Can_Be_Mounted && !Is_Encrypted)
Dees_Troy5bf43922012-09-07 16:07:55 -04001680 return false;
1681
Dees_Troy0550cfb2012-10-13 11:56:13 -04001682 Was_Already_Mounted = Is_Mounted();
Dees_Troy38bd7602012-09-14 13:33:53 -04001683 if (Removable || Is_Encrypted) {
1684 if (!Mount(false))
1685 return true;
1686 } else if (!Mount(Display_Error))
Dees_Troy5bf43922012-09-07 16:07:55 -04001687 return false;
Dees_Troy51127312012-09-08 13:08:49 -04001688
1689 ret = Get_Size_Via_statfs(Display_Error);
Dees_Troy0550cfb2012-10-13 11:56:13 -04001690 if (!ret || Size == 0) {
1691 if (!Get_Size_Via_df(Display_Error)) {
1692 if (!Was_Already_Mounted)
1693 UnMount(false);
Dees_Troy51127312012-09-08 13:08:49 -04001694 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04001695 }
1696 }
Dees_Troy51127312012-09-08 13:08:49 -04001697
Dees_Troy5bf43922012-09-07 16:07:55 -04001698 if (Has_Data_Media) {
1699 if (Mount(Display_Error)) {
Dees_Troy51127312012-09-08 13:08:49 -04001700 unsigned long long data_media_used, actual_data;
Dees_Troy43d8b002012-09-17 16:00:01 -04001701 Used = TWFunc::Get_Folder_Size("/data", Display_Error);
1702 data_media_used = TWFunc::Get_Folder_Size("/data/media", Display_Error);
Dees_Troy51127312012-09-08 13:08:49 -04001703 actual_data = Used - data_media_used;
Dees_Troy5bf43922012-09-07 16:07:55 -04001704 Backup_Size = actual_data;
Dees_Troy51127312012-09-08 13:08:49 -04001705 int bak = (int)(Backup_Size / 1048576LLU);
1706 int total = (int)(Size / 1048576LLU);
1707 int us = (int)(Used / 1048576LLU);
1708 int fre = (int)(Free / 1048576LLU);
1709 int datmed = (int)(data_media_used / 1048576LLU);
Dees_Troy2673cec2013-04-02 20:22:16 +00001710 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 -04001711 } else {
1712 if (!Was_Already_Mounted)
1713 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04001714 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04001715 }
Dees_Troye58d5262012-09-21 12:27:57 -04001716 } else if (Has_Android_Secure) {
1717 if (Mount(Display_Error))
1718 Backup_Size = TWFunc::Get_Folder_Size(Backup_Path, Display_Error);
Dees_Troy0550cfb2012-10-13 11:56:13 -04001719 else {
1720 if (!Was_Already_Mounted)
1721 UnMount(false);
Dees_Troye58d5262012-09-21 12:27:57 -04001722 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04001723 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001724 }
Dees_Troy0550cfb2012-10-13 11:56:13 -04001725 if (!Was_Already_Mounted)
1726 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04001727 return true;
Dees_Troy51127312012-09-08 13:08:49 -04001728}
Dees_Troy38bd7602012-09-14 13:33:53 -04001729
1730void TWPartition::Find_Actual_Block_Device(void) {
1731 if (Is_Decrypted) {
1732 Actual_Block_Device = Decrypted_Block_Device;
Dees_Troy43d8b002012-09-17 16:00:01 -04001733 if (TWFunc::Path_Exists(Primary_Block_Device))
Dees_Troy38bd7602012-09-14 13:33:53 -04001734 Is_Present = true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001735 } else if (TWFunc::Path_Exists(Primary_Block_Device)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001736 Is_Present = true;
1737 Actual_Block_Device = Primary_Block_Device;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001738 return;
1739 }
1740 if (Is_Decrypted) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001741 } else if (!Alternate_Block_Device.empty() && TWFunc::Path_Exists(Alternate_Block_Device)) {
Dees_Troy3f04d032012-10-07 18:20:09 -04001742 Actual_Block_Device = Alternate_Block_Device;
Dees_Troy38bd7602012-09-14 13:33:53 -04001743 Is_Present = true;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001744 } else {
Dees_Troy38bd7602012-09-14 13:33:53 -04001745 Is_Present = false;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001746 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001747}
1748
1749void TWPartition::Recreate_Media_Folder(void) {
1750 string Command;
1751
1752 if (!Mount(true)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001753 LOGERR("Unable to recreate /data/media folder.\n");
Dees_Troyb46a6842012-09-25 11:06:46 -04001754 } else if (!TWFunc::Path_Exists("/data/media")) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001755 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Dees_Troy2673cec2013-04-02 20:22:16 +00001756 LOGINFO("Recreating /data/media folder.\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001757 mkdir("/data/media", S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
1758 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Dees_Troy38bd7602012-09-14 13:33:53 -04001759 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001760}
Dees_Troye58d5262012-09-21 12:27:57 -04001761
1762void TWPartition::Recreate_AndSec_Folder(void) {
Dees_Troye58d5262012-09-21 12:27:57 -04001763 if (!Has_Android_Secure)
1764 return;
Dees_Troy2673cec2013-04-02 20:22:16 +00001765 LOGINFO("Creating %s: %s\n", Backup_Display_Name.c_str(), Symlink_Path.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -04001766 if (!Mount(true)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001767 LOGERR("Unable to recreate %s folder.\n", Backup_Name.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -04001768 } else if (!TWFunc::Path_Exists(Symlink_Path)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001769 LOGINFO("Recreating %s folder.\n", Backup_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001770 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
1771 mkdir(Symlink_Path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
1772 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04001773 }
1774}